1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
4*4882a593Smuzhiyun * All Rights Reserved.
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun #include "xfs.h"
7*4882a593Smuzhiyun #include "xfs_fs.h"
8*4882a593Smuzhiyun #include "xfs_shared.h"
9*4882a593Smuzhiyun #include "xfs_format.h"
10*4882a593Smuzhiyun #include "xfs_log_format.h"
11*4882a593Smuzhiyun #include "xfs_trans_resv.h"
12*4882a593Smuzhiyun #include "xfs_bit.h"
13*4882a593Smuzhiyun #include "xfs_sb.h"
14*4882a593Smuzhiyun #include "xfs_mount.h"
15*4882a593Smuzhiyun #include "xfs_inode.h"
16*4882a593Smuzhiyun #include "xfs_btree.h"
17*4882a593Smuzhiyun #include "xfs_ialloc.h"
18*4882a593Smuzhiyun #include "xfs_ialloc_btree.h"
19*4882a593Smuzhiyun #include "xfs_alloc.h"
20*4882a593Smuzhiyun #include "xfs_errortag.h"
21*4882a593Smuzhiyun #include "xfs_error.h"
22*4882a593Smuzhiyun #include "xfs_bmap.h"
23*4882a593Smuzhiyun #include "xfs_trans.h"
24*4882a593Smuzhiyun #include "xfs_buf_item.h"
25*4882a593Smuzhiyun #include "xfs_icreate_item.h"
26*4882a593Smuzhiyun #include "xfs_icache.h"
27*4882a593Smuzhiyun #include "xfs_trace.h"
28*4882a593Smuzhiyun #include "xfs_log.h"
29*4882a593Smuzhiyun #include "xfs_rmap.h"
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun /*
32*4882a593Smuzhiyun * Lookup a record by ino in the btree given by cur.
33*4882a593Smuzhiyun */
34*4882a593Smuzhiyun int /* error */
xfs_inobt_lookup(struct xfs_btree_cur * cur,xfs_agino_t ino,xfs_lookup_t dir,int * stat)35*4882a593Smuzhiyun xfs_inobt_lookup(
36*4882a593Smuzhiyun struct xfs_btree_cur *cur, /* btree cursor */
37*4882a593Smuzhiyun xfs_agino_t ino, /* starting inode of chunk */
38*4882a593Smuzhiyun xfs_lookup_t dir, /* <=, >=, == */
39*4882a593Smuzhiyun int *stat) /* success/failure */
40*4882a593Smuzhiyun {
41*4882a593Smuzhiyun cur->bc_rec.i.ir_startino = ino;
42*4882a593Smuzhiyun cur->bc_rec.i.ir_holemask = 0;
43*4882a593Smuzhiyun cur->bc_rec.i.ir_count = 0;
44*4882a593Smuzhiyun cur->bc_rec.i.ir_freecount = 0;
45*4882a593Smuzhiyun cur->bc_rec.i.ir_free = 0;
46*4882a593Smuzhiyun return xfs_btree_lookup(cur, dir, stat);
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun /*
50*4882a593Smuzhiyun * Update the record referred to by cur to the value given.
51*4882a593Smuzhiyun * This either works (return 0) or gets an EFSCORRUPTED error.
52*4882a593Smuzhiyun */
53*4882a593Smuzhiyun STATIC int /* error */
xfs_inobt_update(struct xfs_btree_cur * cur,xfs_inobt_rec_incore_t * irec)54*4882a593Smuzhiyun xfs_inobt_update(
55*4882a593Smuzhiyun struct xfs_btree_cur *cur, /* btree cursor */
56*4882a593Smuzhiyun xfs_inobt_rec_incore_t *irec) /* btree record */
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun union xfs_btree_rec rec;
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun rec.inobt.ir_startino = cpu_to_be32(irec->ir_startino);
61*4882a593Smuzhiyun if (xfs_sb_version_hassparseinodes(&cur->bc_mp->m_sb)) {
62*4882a593Smuzhiyun rec.inobt.ir_u.sp.ir_holemask = cpu_to_be16(irec->ir_holemask);
63*4882a593Smuzhiyun rec.inobt.ir_u.sp.ir_count = irec->ir_count;
64*4882a593Smuzhiyun rec.inobt.ir_u.sp.ir_freecount = irec->ir_freecount;
65*4882a593Smuzhiyun } else {
66*4882a593Smuzhiyun /* ir_holemask/ir_count not supported on-disk */
67*4882a593Smuzhiyun rec.inobt.ir_u.f.ir_freecount = cpu_to_be32(irec->ir_freecount);
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun rec.inobt.ir_free = cpu_to_be64(irec->ir_free);
70*4882a593Smuzhiyun return xfs_btree_update(cur, &rec);
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun /* Convert on-disk btree record to incore inobt record. */
74*4882a593Smuzhiyun void
xfs_inobt_btrec_to_irec(struct xfs_mount * mp,union xfs_btree_rec * rec,struct xfs_inobt_rec_incore * irec)75*4882a593Smuzhiyun xfs_inobt_btrec_to_irec(
76*4882a593Smuzhiyun struct xfs_mount *mp,
77*4882a593Smuzhiyun union xfs_btree_rec *rec,
78*4882a593Smuzhiyun struct xfs_inobt_rec_incore *irec)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun irec->ir_startino = be32_to_cpu(rec->inobt.ir_startino);
81*4882a593Smuzhiyun if (xfs_sb_version_hassparseinodes(&mp->m_sb)) {
82*4882a593Smuzhiyun irec->ir_holemask = be16_to_cpu(rec->inobt.ir_u.sp.ir_holemask);
83*4882a593Smuzhiyun irec->ir_count = rec->inobt.ir_u.sp.ir_count;
84*4882a593Smuzhiyun irec->ir_freecount = rec->inobt.ir_u.sp.ir_freecount;
85*4882a593Smuzhiyun } else {
86*4882a593Smuzhiyun /*
87*4882a593Smuzhiyun * ir_holemask/ir_count not supported on-disk. Fill in hardcoded
88*4882a593Smuzhiyun * values for full inode chunks.
89*4882a593Smuzhiyun */
90*4882a593Smuzhiyun irec->ir_holemask = XFS_INOBT_HOLEMASK_FULL;
91*4882a593Smuzhiyun irec->ir_count = XFS_INODES_PER_CHUNK;
92*4882a593Smuzhiyun irec->ir_freecount =
93*4882a593Smuzhiyun be32_to_cpu(rec->inobt.ir_u.f.ir_freecount);
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun irec->ir_free = be64_to_cpu(rec->inobt.ir_free);
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun /*
99*4882a593Smuzhiyun * Get the data from the pointed-to record.
100*4882a593Smuzhiyun */
101*4882a593Smuzhiyun int
xfs_inobt_get_rec(struct xfs_btree_cur * cur,struct xfs_inobt_rec_incore * irec,int * stat)102*4882a593Smuzhiyun xfs_inobt_get_rec(
103*4882a593Smuzhiyun struct xfs_btree_cur *cur,
104*4882a593Smuzhiyun struct xfs_inobt_rec_incore *irec,
105*4882a593Smuzhiyun int *stat)
106*4882a593Smuzhiyun {
107*4882a593Smuzhiyun struct xfs_mount *mp = cur->bc_mp;
108*4882a593Smuzhiyun xfs_agnumber_t agno = cur->bc_ag.agno;
109*4882a593Smuzhiyun union xfs_btree_rec *rec;
110*4882a593Smuzhiyun int error;
111*4882a593Smuzhiyun uint64_t realfree;
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun error = xfs_btree_get_rec(cur, &rec, stat);
114*4882a593Smuzhiyun if (error || *stat == 0)
115*4882a593Smuzhiyun return error;
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun xfs_inobt_btrec_to_irec(mp, rec, irec);
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun if (!xfs_verify_agino(mp, agno, irec->ir_startino))
120*4882a593Smuzhiyun goto out_bad_rec;
121*4882a593Smuzhiyun if (irec->ir_count < XFS_INODES_PER_HOLEMASK_BIT ||
122*4882a593Smuzhiyun irec->ir_count > XFS_INODES_PER_CHUNK)
123*4882a593Smuzhiyun goto out_bad_rec;
124*4882a593Smuzhiyun if (irec->ir_freecount > XFS_INODES_PER_CHUNK)
125*4882a593Smuzhiyun goto out_bad_rec;
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun /* if there are no holes, return the first available offset */
128*4882a593Smuzhiyun if (!xfs_inobt_issparse(irec->ir_holemask))
129*4882a593Smuzhiyun realfree = irec->ir_free;
130*4882a593Smuzhiyun else
131*4882a593Smuzhiyun realfree = irec->ir_free & xfs_inobt_irec_to_allocmask(irec);
132*4882a593Smuzhiyun if (hweight64(realfree) != irec->ir_freecount)
133*4882a593Smuzhiyun goto out_bad_rec;
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun return 0;
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun out_bad_rec:
138*4882a593Smuzhiyun xfs_warn(mp,
139*4882a593Smuzhiyun "%s Inode BTree record corruption in AG %d detected!",
140*4882a593Smuzhiyun cur->bc_btnum == XFS_BTNUM_INO ? "Used" : "Free", agno);
141*4882a593Smuzhiyun xfs_warn(mp,
142*4882a593Smuzhiyun "start inode 0x%x, count 0x%x, free 0x%x freemask 0x%llx, holemask 0x%x",
143*4882a593Smuzhiyun irec->ir_startino, irec->ir_count, irec->ir_freecount,
144*4882a593Smuzhiyun irec->ir_free, irec->ir_holemask);
145*4882a593Smuzhiyun return -EFSCORRUPTED;
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun /*
149*4882a593Smuzhiyun * Insert a single inobt record. Cursor must already point to desired location.
150*4882a593Smuzhiyun */
151*4882a593Smuzhiyun int
xfs_inobt_insert_rec(struct xfs_btree_cur * cur,uint16_t holemask,uint8_t count,int32_t freecount,xfs_inofree_t free,int * stat)152*4882a593Smuzhiyun xfs_inobt_insert_rec(
153*4882a593Smuzhiyun struct xfs_btree_cur *cur,
154*4882a593Smuzhiyun uint16_t holemask,
155*4882a593Smuzhiyun uint8_t count,
156*4882a593Smuzhiyun int32_t freecount,
157*4882a593Smuzhiyun xfs_inofree_t free,
158*4882a593Smuzhiyun int *stat)
159*4882a593Smuzhiyun {
160*4882a593Smuzhiyun cur->bc_rec.i.ir_holemask = holemask;
161*4882a593Smuzhiyun cur->bc_rec.i.ir_count = count;
162*4882a593Smuzhiyun cur->bc_rec.i.ir_freecount = freecount;
163*4882a593Smuzhiyun cur->bc_rec.i.ir_free = free;
164*4882a593Smuzhiyun return xfs_btree_insert(cur, stat);
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun /*
168*4882a593Smuzhiyun * Insert records describing a newly allocated inode chunk into the inobt.
169*4882a593Smuzhiyun */
170*4882a593Smuzhiyun STATIC int
xfs_inobt_insert(struct xfs_mount * mp,struct xfs_trans * tp,struct xfs_buf * agbp,xfs_agino_t newino,xfs_agino_t newlen,xfs_btnum_t btnum)171*4882a593Smuzhiyun xfs_inobt_insert(
172*4882a593Smuzhiyun struct xfs_mount *mp,
173*4882a593Smuzhiyun struct xfs_trans *tp,
174*4882a593Smuzhiyun struct xfs_buf *agbp,
175*4882a593Smuzhiyun xfs_agino_t newino,
176*4882a593Smuzhiyun xfs_agino_t newlen,
177*4882a593Smuzhiyun xfs_btnum_t btnum)
178*4882a593Smuzhiyun {
179*4882a593Smuzhiyun struct xfs_btree_cur *cur;
180*4882a593Smuzhiyun struct xfs_agi *agi = agbp->b_addr;
181*4882a593Smuzhiyun xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
182*4882a593Smuzhiyun xfs_agino_t thisino;
183*4882a593Smuzhiyun int i;
184*4882a593Smuzhiyun int error;
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, btnum);
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun for (thisino = newino;
189*4882a593Smuzhiyun thisino < newino + newlen;
190*4882a593Smuzhiyun thisino += XFS_INODES_PER_CHUNK) {
191*4882a593Smuzhiyun error = xfs_inobt_lookup(cur, thisino, XFS_LOOKUP_EQ, &i);
192*4882a593Smuzhiyun if (error) {
193*4882a593Smuzhiyun xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
194*4882a593Smuzhiyun return error;
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun ASSERT(i == 0);
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun error = xfs_inobt_insert_rec(cur, XFS_INOBT_HOLEMASK_FULL,
199*4882a593Smuzhiyun XFS_INODES_PER_CHUNK,
200*4882a593Smuzhiyun XFS_INODES_PER_CHUNK,
201*4882a593Smuzhiyun XFS_INOBT_ALL_FREE, &i);
202*4882a593Smuzhiyun if (error) {
203*4882a593Smuzhiyun xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
204*4882a593Smuzhiyun return error;
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun ASSERT(i == 1);
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun return 0;
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun /*
215*4882a593Smuzhiyun * Verify that the number of free inodes in the AGI is correct.
216*4882a593Smuzhiyun */
217*4882a593Smuzhiyun #ifdef DEBUG
218*4882a593Smuzhiyun STATIC int
xfs_check_agi_freecount(struct xfs_btree_cur * cur,struct xfs_agi * agi)219*4882a593Smuzhiyun xfs_check_agi_freecount(
220*4882a593Smuzhiyun struct xfs_btree_cur *cur,
221*4882a593Smuzhiyun struct xfs_agi *agi)
222*4882a593Smuzhiyun {
223*4882a593Smuzhiyun if (cur->bc_nlevels == 1) {
224*4882a593Smuzhiyun xfs_inobt_rec_incore_t rec;
225*4882a593Smuzhiyun int freecount = 0;
226*4882a593Smuzhiyun int error;
227*4882a593Smuzhiyun int i;
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
230*4882a593Smuzhiyun if (error)
231*4882a593Smuzhiyun return error;
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun do {
234*4882a593Smuzhiyun error = xfs_inobt_get_rec(cur, &rec, &i);
235*4882a593Smuzhiyun if (error)
236*4882a593Smuzhiyun return error;
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun if (i) {
239*4882a593Smuzhiyun freecount += rec.ir_freecount;
240*4882a593Smuzhiyun error = xfs_btree_increment(cur, 0, &i);
241*4882a593Smuzhiyun if (error)
242*4882a593Smuzhiyun return error;
243*4882a593Smuzhiyun }
244*4882a593Smuzhiyun } while (i == 1);
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun if (!XFS_FORCED_SHUTDOWN(cur->bc_mp))
247*4882a593Smuzhiyun ASSERT(freecount == be32_to_cpu(agi->agi_freecount));
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun return 0;
250*4882a593Smuzhiyun }
251*4882a593Smuzhiyun #else
252*4882a593Smuzhiyun #define xfs_check_agi_freecount(cur, agi) 0
253*4882a593Smuzhiyun #endif
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun /*
256*4882a593Smuzhiyun * Initialise a new set of inodes. When called without a transaction context
257*4882a593Smuzhiyun * (e.g. from recovery) we initiate a delayed write of the inode buffers rather
258*4882a593Smuzhiyun * than logging them (which in a transaction context puts them into the AIL
259*4882a593Smuzhiyun * for writeback rather than the xfsbufd queue).
260*4882a593Smuzhiyun */
261*4882a593Smuzhiyun int
xfs_ialloc_inode_init(struct xfs_mount * mp,struct xfs_trans * tp,struct list_head * buffer_list,int icount,xfs_agnumber_t agno,xfs_agblock_t agbno,xfs_agblock_t length,unsigned int gen)262*4882a593Smuzhiyun xfs_ialloc_inode_init(
263*4882a593Smuzhiyun struct xfs_mount *mp,
264*4882a593Smuzhiyun struct xfs_trans *tp,
265*4882a593Smuzhiyun struct list_head *buffer_list,
266*4882a593Smuzhiyun int icount,
267*4882a593Smuzhiyun xfs_agnumber_t agno,
268*4882a593Smuzhiyun xfs_agblock_t agbno,
269*4882a593Smuzhiyun xfs_agblock_t length,
270*4882a593Smuzhiyun unsigned int gen)
271*4882a593Smuzhiyun {
272*4882a593Smuzhiyun struct xfs_buf *fbuf;
273*4882a593Smuzhiyun struct xfs_dinode *free;
274*4882a593Smuzhiyun int nbufs;
275*4882a593Smuzhiyun int version;
276*4882a593Smuzhiyun int i, j;
277*4882a593Smuzhiyun xfs_daddr_t d;
278*4882a593Smuzhiyun xfs_ino_t ino = 0;
279*4882a593Smuzhiyun int error;
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun /*
282*4882a593Smuzhiyun * Loop over the new block(s), filling in the inodes. For small block
283*4882a593Smuzhiyun * sizes, manipulate the inodes in buffers which are multiples of the
284*4882a593Smuzhiyun * blocks size.
285*4882a593Smuzhiyun */
286*4882a593Smuzhiyun nbufs = length / M_IGEO(mp)->blocks_per_cluster;
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun /*
289*4882a593Smuzhiyun * Figure out what version number to use in the inodes we create. If
290*4882a593Smuzhiyun * the superblock version has caught up to the one that supports the new
291*4882a593Smuzhiyun * inode format, then use the new inode version. Otherwise use the old
292*4882a593Smuzhiyun * version so that old kernels will continue to be able to use the file
293*4882a593Smuzhiyun * system.
294*4882a593Smuzhiyun *
295*4882a593Smuzhiyun * For v3 inodes, we also need to write the inode number into the inode,
296*4882a593Smuzhiyun * so calculate the first inode number of the chunk here as
297*4882a593Smuzhiyun * XFS_AGB_TO_AGINO() only works within a filesystem block, not
298*4882a593Smuzhiyun * across multiple filesystem blocks (such as a cluster) and so cannot
299*4882a593Smuzhiyun * be used in the cluster buffer loop below.
300*4882a593Smuzhiyun *
301*4882a593Smuzhiyun * Further, because we are writing the inode directly into the buffer
302*4882a593Smuzhiyun * and calculating a CRC on the entire inode, we have ot log the entire
303*4882a593Smuzhiyun * inode so that the entire range the CRC covers is present in the log.
304*4882a593Smuzhiyun * That means for v3 inode we log the entire buffer rather than just the
305*4882a593Smuzhiyun * inode cores.
306*4882a593Smuzhiyun */
307*4882a593Smuzhiyun if (xfs_sb_version_has_v3inode(&mp->m_sb)) {
308*4882a593Smuzhiyun version = 3;
309*4882a593Smuzhiyun ino = XFS_AGINO_TO_INO(mp, agno, XFS_AGB_TO_AGINO(mp, agbno));
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun /*
312*4882a593Smuzhiyun * log the initialisation that is about to take place as an
313*4882a593Smuzhiyun * logical operation. This means the transaction does not
314*4882a593Smuzhiyun * need to log the physical changes to the inode buffers as log
315*4882a593Smuzhiyun * recovery will know what initialisation is actually needed.
316*4882a593Smuzhiyun * Hence we only need to log the buffers as "ordered" buffers so
317*4882a593Smuzhiyun * they track in the AIL as if they were physically logged.
318*4882a593Smuzhiyun */
319*4882a593Smuzhiyun if (tp)
320*4882a593Smuzhiyun xfs_icreate_log(tp, agno, agbno, icount,
321*4882a593Smuzhiyun mp->m_sb.sb_inodesize, length, gen);
322*4882a593Smuzhiyun } else
323*4882a593Smuzhiyun version = 2;
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun for (j = 0; j < nbufs; j++) {
326*4882a593Smuzhiyun /*
327*4882a593Smuzhiyun * Get the block.
328*4882a593Smuzhiyun */
329*4882a593Smuzhiyun d = XFS_AGB_TO_DADDR(mp, agno, agbno +
330*4882a593Smuzhiyun (j * M_IGEO(mp)->blocks_per_cluster));
331*4882a593Smuzhiyun error = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
332*4882a593Smuzhiyun mp->m_bsize * M_IGEO(mp)->blocks_per_cluster,
333*4882a593Smuzhiyun XBF_UNMAPPED, &fbuf);
334*4882a593Smuzhiyun if (error)
335*4882a593Smuzhiyun return error;
336*4882a593Smuzhiyun
337*4882a593Smuzhiyun /* Initialize the inode buffers and log them appropriately. */
338*4882a593Smuzhiyun fbuf->b_ops = &xfs_inode_buf_ops;
339*4882a593Smuzhiyun xfs_buf_zero(fbuf, 0, BBTOB(fbuf->b_length));
340*4882a593Smuzhiyun for (i = 0; i < M_IGEO(mp)->inodes_per_cluster; i++) {
341*4882a593Smuzhiyun int ioffset = i << mp->m_sb.sb_inodelog;
342*4882a593Smuzhiyun uint isize = XFS_DINODE_SIZE(&mp->m_sb);
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun free = xfs_make_iptr(mp, fbuf, i);
345*4882a593Smuzhiyun free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
346*4882a593Smuzhiyun free->di_version = version;
347*4882a593Smuzhiyun free->di_gen = cpu_to_be32(gen);
348*4882a593Smuzhiyun free->di_next_unlinked = cpu_to_be32(NULLAGINO);
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun if (version == 3) {
351*4882a593Smuzhiyun free->di_ino = cpu_to_be64(ino);
352*4882a593Smuzhiyun ino++;
353*4882a593Smuzhiyun uuid_copy(&free->di_uuid,
354*4882a593Smuzhiyun &mp->m_sb.sb_meta_uuid);
355*4882a593Smuzhiyun xfs_dinode_calc_crc(mp, free);
356*4882a593Smuzhiyun } else if (tp) {
357*4882a593Smuzhiyun /* just log the inode core */
358*4882a593Smuzhiyun xfs_trans_log_buf(tp, fbuf, ioffset,
359*4882a593Smuzhiyun ioffset + isize - 1);
360*4882a593Smuzhiyun }
361*4882a593Smuzhiyun }
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun if (tp) {
364*4882a593Smuzhiyun /*
365*4882a593Smuzhiyun * Mark the buffer as an inode allocation buffer so it
366*4882a593Smuzhiyun * sticks in AIL at the point of this allocation
367*4882a593Smuzhiyun * transaction. This ensures the they are on disk before
368*4882a593Smuzhiyun * the tail of the log can be moved past this
369*4882a593Smuzhiyun * transaction (i.e. by preventing relogging from moving
370*4882a593Smuzhiyun * it forward in the log).
371*4882a593Smuzhiyun */
372*4882a593Smuzhiyun xfs_trans_inode_alloc_buf(tp, fbuf);
373*4882a593Smuzhiyun if (version == 3) {
374*4882a593Smuzhiyun /*
375*4882a593Smuzhiyun * Mark the buffer as ordered so that they are
376*4882a593Smuzhiyun * not physically logged in the transaction but
377*4882a593Smuzhiyun * still tracked in the AIL as part of the
378*4882a593Smuzhiyun * transaction and pin the log appropriately.
379*4882a593Smuzhiyun */
380*4882a593Smuzhiyun xfs_trans_ordered_buf(tp, fbuf);
381*4882a593Smuzhiyun }
382*4882a593Smuzhiyun } else {
383*4882a593Smuzhiyun fbuf->b_flags |= XBF_DONE;
384*4882a593Smuzhiyun xfs_buf_delwri_queue(fbuf, buffer_list);
385*4882a593Smuzhiyun xfs_buf_relse(fbuf);
386*4882a593Smuzhiyun }
387*4882a593Smuzhiyun }
388*4882a593Smuzhiyun return 0;
389*4882a593Smuzhiyun }
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun /*
392*4882a593Smuzhiyun * Align startino and allocmask for a recently allocated sparse chunk such that
393*4882a593Smuzhiyun * they are fit for insertion (or merge) into the on-disk inode btrees.
394*4882a593Smuzhiyun *
395*4882a593Smuzhiyun * Background:
396*4882a593Smuzhiyun *
397*4882a593Smuzhiyun * When enabled, sparse inode support increases the inode alignment from cluster
398*4882a593Smuzhiyun * size to inode chunk size. This means that the minimum range between two
399*4882a593Smuzhiyun * non-adjacent inode records in the inobt is large enough for a full inode
400*4882a593Smuzhiyun * record. This allows for cluster sized, cluster aligned block allocation
401*4882a593Smuzhiyun * without need to worry about whether the resulting inode record overlaps with
402*4882a593Smuzhiyun * another record in the tree. Without this basic rule, we would have to deal
403*4882a593Smuzhiyun * with the consequences of overlap by potentially undoing recent allocations in
404*4882a593Smuzhiyun * the inode allocation codepath.
405*4882a593Smuzhiyun *
406*4882a593Smuzhiyun * Because of this alignment rule (which is enforced on mount), there are two
407*4882a593Smuzhiyun * inobt possibilities for newly allocated sparse chunks. One is that the
408*4882a593Smuzhiyun * aligned inode record for the chunk covers a range of inodes not already
409*4882a593Smuzhiyun * covered in the inobt (i.e., it is safe to insert a new sparse record). The
410*4882a593Smuzhiyun * other is that a record already exists at the aligned startino that considers
411*4882a593Smuzhiyun * the newly allocated range as sparse. In the latter case, record content is
412*4882a593Smuzhiyun * merged in hope that sparse inode chunks fill to full chunks over time.
413*4882a593Smuzhiyun */
414*4882a593Smuzhiyun STATIC void
xfs_align_sparse_ino(struct xfs_mount * mp,xfs_agino_t * startino,uint16_t * allocmask)415*4882a593Smuzhiyun xfs_align_sparse_ino(
416*4882a593Smuzhiyun struct xfs_mount *mp,
417*4882a593Smuzhiyun xfs_agino_t *startino,
418*4882a593Smuzhiyun uint16_t *allocmask)
419*4882a593Smuzhiyun {
420*4882a593Smuzhiyun xfs_agblock_t agbno;
421*4882a593Smuzhiyun xfs_agblock_t mod;
422*4882a593Smuzhiyun int offset;
423*4882a593Smuzhiyun
424*4882a593Smuzhiyun agbno = XFS_AGINO_TO_AGBNO(mp, *startino);
425*4882a593Smuzhiyun mod = agbno % mp->m_sb.sb_inoalignmt;
426*4882a593Smuzhiyun if (!mod)
427*4882a593Smuzhiyun return;
428*4882a593Smuzhiyun
429*4882a593Smuzhiyun /* calculate the inode offset and align startino */
430*4882a593Smuzhiyun offset = XFS_AGB_TO_AGINO(mp, mod);
431*4882a593Smuzhiyun *startino -= offset;
432*4882a593Smuzhiyun
433*4882a593Smuzhiyun /*
434*4882a593Smuzhiyun * Since startino has been aligned down, left shift allocmask such that
435*4882a593Smuzhiyun * it continues to represent the same physical inodes relative to the
436*4882a593Smuzhiyun * new startino.
437*4882a593Smuzhiyun */
438*4882a593Smuzhiyun *allocmask <<= offset / XFS_INODES_PER_HOLEMASK_BIT;
439*4882a593Smuzhiyun }
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun /*
442*4882a593Smuzhiyun * Determine whether the source inode record can merge into the target. Both
443*4882a593Smuzhiyun * records must be sparse, the inode ranges must match and there must be no
444*4882a593Smuzhiyun * allocation overlap between the records.
445*4882a593Smuzhiyun */
446*4882a593Smuzhiyun STATIC bool
__xfs_inobt_can_merge(struct xfs_inobt_rec_incore * trec,struct xfs_inobt_rec_incore * srec)447*4882a593Smuzhiyun __xfs_inobt_can_merge(
448*4882a593Smuzhiyun struct xfs_inobt_rec_incore *trec, /* tgt record */
449*4882a593Smuzhiyun struct xfs_inobt_rec_incore *srec) /* src record */
450*4882a593Smuzhiyun {
451*4882a593Smuzhiyun uint64_t talloc;
452*4882a593Smuzhiyun uint64_t salloc;
453*4882a593Smuzhiyun
454*4882a593Smuzhiyun /* records must cover the same inode range */
455*4882a593Smuzhiyun if (trec->ir_startino != srec->ir_startino)
456*4882a593Smuzhiyun return false;
457*4882a593Smuzhiyun
458*4882a593Smuzhiyun /* both records must be sparse */
459*4882a593Smuzhiyun if (!xfs_inobt_issparse(trec->ir_holemask) ||
460*4882a593Smuzhiyun !xfs_inobt_issparse(srec->ir_holemask))
461*4882a593Smuzhiyun return false;
462*4882a593Smuzhiyun
463*4882a593Smuzhiyun /* both records must track some inodes */
464*4882a593Smuzhiyun if (!trec->ir_count || !srec->ir_count)
465*4882a593Smuzhiyun return false;
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun /* can't exceed capacity of a full record */
468*4882a593Smuzhiyun if (trec->ir_count + srec->ir_count > XFS_INODES_PER_CHUNK)
469*4882a593Smuzhiyun return false;
470*4882a593Smuzhiyun
471*4882a593Smuzhiyun /* verify there is no allocation overlap */
472*4882a593Smuzhiyun talloc = xfs_inobt_irec_to_allocmask(trec);
473*4882a593Smuzhiyun salloc = xfs_inobt_irec_to_allocmask(srec);
474*4882a593Smuzhiyun if (talloc & salloc)
475*4882a593Smuzhiyun return false;
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun return true;
478*4882a593Smuzhiyun }
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun /*
481*4882a593Smuzhiyun * Merge the source inode record into the target. The caller must call
482*4882a593Smuzhiyun * __xfs_inobt_can_merge() to ensure the merge is valid.
483*4882a593Smuzhiyun */
484*4882a593Smuzhiyun STATIC void
__xfs_inobt_rec_merge(struct xfs_inobt_rec_incore * trec,struct xfs_inobt_rec_incore * srec)485*4882a593Smuzhiyun __xfs_inobt_rec_merge(
486*4882a593Smuzhiyun struct xfs_inobt_rec_incore *trec, /* target */
487*4882a593Smuzhiyun struct xfs_inobt_rec_incore *srec) /* src */
488*4882a593Smuzhiyun {
489*4882a593Smuzhiyun ASSERT(trec->ir_startino == srec->ir_startino);
490*4882a593Smuzhiyun
491*4882a593Smuzhiyun /* combine the counts */
492*4882a593Smuzhiyun trec->ir_count += srec->ir_count;
493*4882a593Smuzhiyun trec->ir_freecount += srec->ir_freecount;
494*4882a593Smuzhiyun
495*4882a593Smuzhiyun /*
496*4882a593Smuzhiyun * Merge the holemask and free mask. For both fields, 0 bits refer to
497*4882a593Smuzhiyun * allocated inodes. We combine the allocated ranges with bitwise AND.
498*4882a593Smuzhiyun */
499*4882a593Smuzhiyun trec->ir_holemask &= srec->ir_holemask;
500*4882a593Smuzhiyun trec->ir_free &= srec->ir_free;
501*4882a593Smuzhiyun }
502*4882a593Smuzhiyun
503*4882a593Smuzhiyun /*
504*4882a593Smuzhiyun * Insert a new sparse inode chunk into the associated inode btree. The inode
505*4882a593Smuzhiyun * record for the sparse chunk is pre-aligned to a startino that should match
506*4882a593Smuzhiyun * any pre-existing sparse inode record in the tree. This allows sparse chunks
507*4882a593Smuzhiyun * to fill over time.
508*4882a593Smuzhiyun *
509*4882a593Smuzhiyun * This function supports two modes of handling preexisting records depending on
510*4882a593Smuzhiyun * the merge flag. If merge is true, the provided record is merged with the
511*4882a593Smuzhiyun * existing record and updated in place. The merged record is returned in nrec.
512*4882a593Smuzhiyun * If merge is false, an existing record is replaced with the provided record.
513*4882a593Smuzhiyun * If no preexisting record exists, the provided record is always inserted.
514*4882a593Smuzhiyun *
515*4882a593Smuzhiyun * It is considered corruption if a merge is requested and not possible. Given
516*4882a593Smuzhiyun * the sparse inode alignment constraints, this should never happen.
517*4882a593Smuzhiyun */
518*4882a593Smuzhiyun STATIC int
xfs_inobt_insert_sprec(struct xfs_mount * mp,struct xfs_trans * tp,struct xfs_buf * agbp,int btnum,struct xfs_inobt_rec_incore * nrec,bool merge)519*4882a593Smuzhiyun xfs_inobt_insert_sprec(
520*4882a593Smuzhiyun struct xfs_mount *mp,
521*4882a593Smuzhiyun struct xfs_trans *tp,
522*4882a593Smuzhiyun struct xfs_buf *agbp,
523*4882a593Smuzhiyun int btnum,
524*4882a593Smuzhiyun struct xfs_inobt_rec_incore *nrec, /* in/out: new/merged rec. */
525*4882a593Smuzhiyun bool merge) /* merge or replace */
526*4882a593Smuzhiyun {
527*4882a593Smuzhiyun struct xfs_btree_cur *cur;
528*4882a593Smuzhiyun struct xfs_agi *agi = agbp->b_addr;
529*4882a593Smuzhiyun xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
530*4882a593Smuzhiyun int error;
531*4882a593Smuzhiyun int i;
532*4882a593Smuzhiyun struct xfs_inobt_rec_incore rec;
533*4882a593Smuzhiyun
534*4882a593Smuzhiyun cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, btnum);
535*4882a593Smuzhiyun
536*4882a593Smuzhiyun /* the new record is pre-aligned so we know where to look */
537*4882a593Smuzhiyun error = xfs_inobt_lookup(cur, nrec->ir_startino, XFS_LOOKUP_EQ, &i);
538*4882a593Smuzhiyun if (error)
539*4882a593Smuzhiyun goto error;
540*4882a593Smuzhiyun /* if nothing there, insert a new record and return */
541*4882a593Smuzhiyun if (i == 0) {
542*4882a593Smuzhiyun error = xfs_inobt_insert_rec(cur, nrec->ir_holemask,
543*4882a593Smuzhiyun nrec->ir_count, nrec->ir_freecount,
544*4882a593Smuzhiyun nrec->ir_free, &i);
545*4882a593Smuzhiyun if (error)
546*4882a593Smuzhiyun goto error;
547*4882a593Smuzhiyun if (XFS_IS_CORRUPT(mp, i != 1)) {
548*4882a593Smuzhiyun error = -EFSCORRUPTED;
549*4882a593Smuzhiyun goto error;
550*4882a593Smuzhiyun }
551*4882a593Smuzhiyun
552*4882a593Smuzhiyun goto out;
553*4882a593Smuzhiyun }
554*4882a593Smuzhiyun
555*4882a593Smuzhiyun /*
556*4882a593Smuzhiyun * A record exists at this startino. Merge or replace the record
557*4882a593Smuzhiyun * depending on what we've been asked to do.
558*4882a593Smuzhiyun */
559*4882a593Smuzhiyun if (merge) {
560*4882a593Smuzhiyun error = xfs_inobt_get_rec(cur, &rec, &i);
561*4882a593Smuzhiyun if (error)
562*4882a593Smuzhiyun goto error;
563*4882a593Smuzhiyun if (XFS_IS_CORRUPT(mp, i != 1)) {
564*4882a593Smuzhiyun error = -EFSCORRUPTED;
565*4882a593Smuzhiyun goto error;
566*4882a593Smuzhiyun }
567*4882a593Smuzhiyun if (XFS_IS_CORRUPT(mp, rec.ir_startino != nrec->ir_startino)) {
568*4882a593Smuzhiyun error = -EFSCORRUPTED;
569*4882a593Smuzhiyun goto error;
570*4882a593Smuzhiyun }
571*4882a593Smuzhiyun
572*4882a593Smuzhiyun /*
573*4882a593Smuzhiyun * This should never fail. If we have coexisting records that
574*4882a593Smuzhiyun * cannot merge, something is seriously wrong.
575*4882a593Smuzhiyun */
576*4882a593Smuzhiyun if (XFS_IS_CORRUPT(mp, !__xfs_inobt_can_merge(nrec, &rec))) {
577*4882a593Smuzhiyun error = -EFSCORRUPTED;
578*4882a593Smuzhiyun goto error;
579*4882a593Smuzhiyun }
580*4882a593Smuzhiyun
581*4882a593Smuzhiyun trace_xfs_irec_merge_pre(mp, agno, rec.ir_startino,
582*4882a593Smuzhiyun rec.ir_holemask, nrec->ir_startino,
583*4882a593Smuzhiyun nrec->ir_holemask);
584*4882a593Smuzhiyun
585*4882a593Smuzhiyun /* merge to nrec to output the updated record */
586*4882a593Smuzhiyun __xfs_inobt_rec_merge(nrec, &rec);
587*4882a593Smuzhiyun
588*4882a593Smuzhiyun trace_xfs_irec_merge_post(mp, agno, nrec->ir_startino,
589*4882a593Smuzhiyun nrec->ir_holemask);
590*4882a593Smuzhiyun
591*4882a593Smuzhiyun error = xfs_inobt_rec_check_count(mp, nrec);
592*4882a593Smuzhiyun if (error)
593*4882a593Smuzhiyun goto error;
594*4882a593Smuzhiyun }
595*4882a593Smuzhiyun
596*4882a593Smuzhiyun error = xfs_inobt_update(cur, nrec);
597*4882a593Smuzhiyun if (error)
598*4882a593Smuzhiyun goto error;
599*4882a593Smuzhiyun
600*4882a593Smuzhiyun out:
601*4882a593Smuzhiyun xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
602*4882a593Smuzhiyun return 0;
603*4882a593Smuzhiyun error:
604*4882a593Smuzhiyun xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
605*4882a593Smuzhiyun return error;
606*4882a593Smuzhiyun }
607*4882a593Smuzhiyun
608*4882a593Smuzhiyun /*
609*4882a593Smuzhiyun * Allocate new inodes in the allocation group specified by agbp.
610*4882a593Smuzhiyun * Return 0 for success, else error code.
611*4882a593Smuzhiyun */
612*4882a593Smuzhiyun STATIC int
xfs_ialloc_ag_alloc(struct xfs_trans * tp,struct xfs_buf * agbp,int * alloc)613*4882a593Smuzhiyun xfs_ialloc_ag_alloc(
614*4882a593Smuzhiyun struct xfs_trans *tp,
615*4882a593Smuzhiyun struct xfs_buf *agbp,
616*4882a593Smuzhiyun int *alloc)
617*4882a593Smuzhiyun {
618*4882a593Smuzhiyun struct xfs_agi *agi;
619*4882a593Smuzhiyun struct xfs_alloc_arg args;
620*4882a593Smuzhiyun xfs_agnumber_t agno;
621*4882a593Smuzhiyun int error;
622*4882a593Smuzhiyun xfs_agino_t newino; /* new first inode's number */
623*4882a593Smuzhiyun xfs_agino_t newlen; /* new number of inodes */
624*4882a593Smuzhiyun int isaligned = 0; /* inode allocation at stripe */
625*4882a593Smuzhiyun /* unit boundary */
626*4882a593Smuzhiyun /* init. to full chunk */
627*4882a593Smuzhiyun uint16_t allocmask = (uint16_t) -1;
628*4882a593Smuzhiyun struct xfs_inobt_rec_incore rec;
629*4882a593Smuzhiyun struct xfs_perag *pag;
630*4882a593Smuzhiyun struct xfs_ino_geometry *igeo = M_IGEO(tp->t_mountp);
631*4882a593Smuzhiyun int do_sparse = 0;
632*4882a593Smuzhiyun
633*4882a593Smuzhiyun memset(&args, 0, sizeof(args));
634*4882a593Smuzhiyun args.tp = tp;
635*4882a593Smuzhiyun args.mp = tp->t_mountp;
636*4882a593Smuzhiyun args.fsbno = NULLFSBLOCK;
637*4882a593Smuzhiyun args.oinfo = XFS_RMAP_OINFO_INODES;
638*4882a593Smuzhiyun
639*4882a593Smuzhiyun #ifdef DEBUG
640*4882a593Smuzhiyun /* randomly do sparse inode allocations */
641*4882a593Smuzhiyun if (xfs_sb_version_hassparseinodes(&tp->t_mountp->m_sb) &&
642*4882a593Smuzhiyun igeo->ialloc_min_blks < igeo->ialloc_blks)
643*4882a593Smuzhiyun do_sparse = prandom_u32() & 1;
644*4882a593Smuzhiyun #endif
645*4882a593Smuzhiyun
646*4882a593Smuzhiyun /*
647*4882a593Smuzhiyun * Locking will ensure that we don't have two callers in here
648*4882a593Smuzhiyun * at one time.
649*4882a593Smuzhiyun */
650*4882a593Smuzhiyun newlen = igeo->ialloc_inos;
651*4882a593Smuzhiyun if (igeo->maxicount &&
652*4882a593Smuzhiyun percpu_counter_read_positive(&args.mp->m_icount) + newlen >
653*4882a593Smuzhiyun igeo->maxicount)
654*4882a593Smuzhiyun return -ENOSPC;
655*4882a593Smuzhiyun args.minlen = args.maxlen = igeo->ialloc_blks;
656*4882a593Smuzhiyun /*
657*4882a593Smuzhiyun * First try to allocate inodes contiguous with the last-allocated
658*4882a593Smuzhiyun * chunk of inodes. If the filesystem is striped, this will fill
659*4882a593Smuzhiyun * an entire stripe unit with inodes.
660*4882a593Smuzhiyun */
661*4882a593Smuzhiyun agi = agbp->b_addr;
662*4882a593Smuzhiyun newino = be32_to_cpu(agi->agi_newino);
663*4882a593Smuzhiyun agno = be32_to_cpu(agi->agi_seqno);
664*4882a593Smuzhiyun args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
665*4882a593Smuzhiyun igeo->ialloc_blks;
666*4882a593Smuzhiyun if (do_sparse)
667*4882a593Smuzhiyun goto sparse_alloc;
668*4882a593Smuzhiyun if (likely(newino != NULLAGINO &&
669*4882a593Smuzhiyun (args.agbno < be32_to_cpu(agi->agi_length)))) {
670*4882a593Smuzhiyun args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
671*4882a593Smuzhiyun args.type = XFS_ALLOCTYPE_THIS_BNO;
672*4882a593Smuzhiyun args.prod = 1;
673*4882a593Smuzhiyun
674*4882a593Smuzhiyun /*
675*4882a593Smuzhiyun * We need to take into account alignment here to ensure that
676*4882a593Smuzhiyun * we don't modify the free list if we fail to have an exact
677*4882a593Smuzhiyun * block. If we don't have an exact match, and every oher
678*4882a593Smuzhiyun * attempt allocation attempt fails, we'll end up cancelling
679*4882a593Smuzhiyun * a dirty transaction and shutting down.
680*4882a593Smuzhiyun *
681*4882a593Smuzhiyun * For an exact allocation, alignment must be 1,
682*4882a593Smuzhiyun * however we need to take cluster alignment into account when
683*4882a593Smuzhiyun * fixing up the freelist. Use the minalignslop field to
684*4882a593Smuzhiyun * indicate that extra blocks might be required for alignment,
685*4882a593Smuzhiyun * but not to use them in the actual exact allocation.
686*4882a593Smuzhiyun */
687*4882a593Smuzhiyun args.alignment = 1;
688*4882a593Smuzhiyun args.minalignslop = igeo->cluster_align - 1;
689*4882a593Smuzhiyun
690*4882a593Smuzhiyun /* Allow space for the inode btree to split. */
691*4882a593Smuzhiyun args.minleft = igeo->inobt_maxlevels;
692*4882a593Smuzhiyun if ((error = xfs_alloc_vextent(&args)))
693*4882a593Smuzhiyun return error;
694*4882a593Smuzhiyun
695*4882a593Smuzhiyun /*
696*4882a593Smuzhiyun * This request might have dirtied the transaction if the AG can
697*4882a593Smuzhiyun * satisfy the request, but the exact block was not available.
698*4882a593Smuzhiyun * If the allocation did fail, subsequent requests will relax
699*4882a593Smuzhiyun * the exact agbno requirement and increase the alignment
700*4882a593Smuzhiyun * instead. It is critical that the total size of the request
701*4882a593Smuzhiyun * (len + alignment + slop) does not increase from this point
702*4882a593Smuzhiyun * on, so reset minalignslop to ensure it is not included in
703*4882a593Smuzhiyun * subsequent requests.
704*4882a593Smuzhiyun */
705*4882a593Smuzhiyun args.minalignslop = 0;
706*4882a593Smuzhiyun }
707*4882a593Smuzhiyun
708*4882a593Smuzhiyun if (unlikely(args.fsbno == NULLFSBLOCK)) {
709*4882a593Smuzhiyun /*
710*4882a593Smuzhiyun * Set the alignment for the allocation.
711*4882a593Smuzhiyun * If stripe alignment is turned on then align at stripe unit
712*4882a593Smuzhiyun * boundary.
713*4882a593Smuzhiyun * If the cluster size is smaller than a filesystem block
714*4882a593Smuzhiyun * then we're doing I/O for inodes in filesystem block size
715*4882a593Smuzhiyun * pieces, so don't need alignment anyway.
716*4882a593Smuzhiyun */
717*4882a593Smuzhiyun isaligned = 0;
718*4882a593Smuzhiyun if (igeo->ialloc_align) {
719*4882a593Smuzhiyun ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
720*4882a593Smuzhiyun args.alignment = args.mp->m_dalign;
721*4882a593Smuzhiyun isaligned = 1;
722*4882a593Smuzhiyun } else
723*4882a593Smuzhiyun args.alignment = igeo->cluster_align;
724*4882a593Smuzhiyun /*
725*4882a593Smuzhiyun * Need to figure out where to allocate the inode blocks.
726*4882a593Smuzhiyun * Ideally they should be spaced out through the a.g.
727*4882a593Smuzhiyun * For now, just allocate blocks up front.
728*4882a593Smuzhiyun */
729*4882a593Smuzhiyun args.agbno = be32_to_cpu(agi->agi_root);
730*4882a593Smuzhiyun args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
731*4882a593Smuzhiyun /*
732*4882a593Smuzhiyun * Allocate a fixed-size extent of inodes.
733*4882a593Smuzhiyun */
734*4882a593Smuzhiyun args.type = XFS_ALLOCTYPE_NEAR_BNO;
735*4882a593Smuzhiyun args.prod = 1;
736*4882a593Smuzhiyun /*
737*4882a593Smuzhiyun * Allow space for the inode btree to split.
738*4882a593Smuzhiyun */
739*4882a593Smuzhiyun args.minleft = igeo->inobt_maxlevels;
740*4882a593Smuzhiyun if ((error = xfs_alloc_vextent(&args)))
741*4882a593Smuzhiyun return error;
742*4882a593Smuzhiyun }
743*4882a593Smuzhiyun
744*4882a593Smuzhiyun /*
745*4882a593Smuzhiyun * If stripe alignment is turned on, then try again with cluster
746*4882a593Smuzhiyun * alignment.
747*4882a593Smuzhiyun */
748*4882a593Smuzhiyun if (isaligned && args.fsbno == NULLFSBLOCK) {
749*4882a593Smuzhiyun args.type = XFS_ALLOCTYPE_NEAR_BNO;
750*4882a593Smuzhiyun args.agbno = be32_to_cpu(agi->agi_root);
751*4882a593Smuzhiyun args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
752*4882a593Smuzhiyun args.alignment = igeo->cluster_align;
753*4882a593Smuzhiyun if ((error = xfs_alloc_vextent(&args)))
754*4882a593Smuzhiyun return error;
755*4882a593Smuzhiyun }
756*4882a593Smuzhiyun
757*4882a593Smuzhiyun /*
758*4882a593Smuzhiyun * Finally, try a sparse allocation if the filesystem supports it and
759*4882a593Smuzhiyun * the sparse allocation length is smaller than a full chunk.
760*4882a593Smuzhiyun */
761*4882a593Smuzhiyun if (xfs_sb_version_hassparseinodes(&args.mp->m_sb) &&
762*4882a593Smuzhiyun igeo->ialloc_min_blks < igeo->ialloc_blks &&
763*4882a593Smuzhiyun args.fsbno == NULLFSBLOCK) {
764*4882a593Smuzhiyun sparse_alloc:
765*4882a593Smuzhiyun args.type = XFS_ALLOCTYPE_NEAR_BNO;
766*4882a593Smuzhiyun args.agbno = be32_to_cpu(agi->agi_root);
767*4882a593Smuzhiyun args.fsbno = XFS_AGB_TO_FSB(args.mp, agno, args.agbno);
768*4882a593Smuzhiyun args.alignment = args.mp->m_sb.sb_spino_align;
769*4882a593Smuzhiyun args.prod = 1;
770*4882a593Smuzhiyun
771*4882a593Smuzhiyun args.minlen = igeo->ialloc_min_blks;
772*4882a593Smuzhiyun args.maxlen = args.minlen;
773*4882a593Smuzhiyun
774*4882a593Smuzhiyun /*
775*4882a593Smuzhiyun * The inode record will be aligned to full chunk size. We must
776*4882a593Smuzhiyun * prevent sparse allocation from AG boundaries that result in
777*4882a593Smuzhiyun * invalid inode records, such as records that start at agbno 0
778*4882a593Smuzhiyun * or extend beyond the AG.
779*4882a593Smuzhiyun *
780*4882a593Smuzhiyun * Set min agbno to the first aligned, non-zero agbno and max to
781*4882a593Smuzhiyun * the last aligned agbno that is at least one full chunk from
782*4882a593Smuzhiyun * the end of the AG.
783*4882a593Smuzhiyun */
784*4882a593Smuzhiyun args.min_agbno = args.mp->m_sb.sb_inoalignmt;
785*4882a593Smuzhiyun args.max_agbno = round_down(args.mp->m_sb.sb_agblocks,
786*4882a593Smuzhiyun args.mp->m_sb.sb_inoalignmt) -
787*4882a593Smuzhiyun igeo->ialloc_blks;
788*4882a593Smuzhiyun
789*4882a593Smuzhiyun error = xfs_alloc_vextent(&args);
790*4882a593Smuzhiyun if (error)
791*4882a593Smuzhiyun return error;
792*4882a593Smuzhiyun
793*4882a593Smuzhiyun newlen = XFS_AGB_TO_AGINO(args.mp, args.len);
794*4882a593Smuzhiyun ASSERT(newlen <= XFS_INODES_PER_CHUNK);
795*4882a593Smuzhiyun allocmask = (1 << (newlen / XFS_INODES_PER_HOLEMASK_BIT)) - 1;
796*4882a593Smuzhiyun }
797*4882a593Smuzhiyun
798*4882a593Smuzhiyun if (args.fsbno == NULLFSBLOCK) {
799*4882a593Smuzhiyun *alloc = 0;
800*4882a593Smuzhiyun return 0;
801*4882a593Smuzhiyun }
802*4882a593Smuzhiyun ASSERT(args.len == args.minlen);
803*4882a593Smuzhiyun
804*4882a593Smuzhiyun /*
805*4882a593Smuzhiyun * Stamp and write the inode buffers.
806*4882a593Smuzhiyun *
807*4882a593Smuzhiyun * Seed the new inode cluster with a random generation number. This
808*4882a593Smuzhiyun * prevents short-term reuse of generation numbers if a chunk is
809*4882a593Smuzhiyun * freed and then immediately reallocated. We use random numbers
810*4882a593Smuzhiyun * rather than a linear progression to prevent the next generation
811*4882a593Smuzhiyun * number from being easily guessable.
812*4882a593Smuzhiyun */
813*4882a593Smuzhiyun error = xfs_ialloc_inode_init(args.mp, tp, NULL, newlen, agno,
814*4882a593Smuzhiyun args.agbno, args.len, prandom_u32());
815*4882a593Smuzhiyun
816*4882a593Smuzhiyun if (error)
817*4882a593Smuzhiyun return error;
818*4882a593Smuzhiyun /*
819*4882a593Smuzhiyun * Convert the results.
820*4882a593Smuzhiyun */
821*4882a593Smuzhiyun newino = XFS_AGB_TO_AGINO(args.mp, args.agbno);
822*4882a593Smuzhiyun
823*4882a593Smuzhiyun if (xfs_inobt_issparse(~allocmask)) {
824*4882a593Smuzhiyun /*
825*4882a593Smuzhiyun * We've allocated a sparse chunk. Align the startino and mask.
826*4882a593Smuzhiyun */
827*4882a593Smuzhiyun xfs_align_sparse_ino(args.mp, &newino, &allocmask);
828*4882a593Smuzhiyun
829*4882a593Smuzhiyun rec.ir_startino = newino;
830*4882a593Smuzhiyun rec.ir_holemask = ~allocmask;
831*4882a593Smuzhiyun rec.ir_count = newlen;
832*4882a593Smuzhiyun rec.ir_freecount = newlen;
833*4882a593Smuzhiyun rec.ir_free = XFS_INOBT_ALL_FREE;
834*4882a593Smuzhiyun
835*4882a593Smuzhiyun /*
836*4882a593Smuzhiyun * Insert the sparse record into the inobt and allow for a merge
837*4882a593Smuzhiyun * if necessary. If a merge does occur, rec is updated to the
838*4882a593Smuzhiyun * merged record.
839*4882a593Smuzhiyun */
840*4882a593Smuzhiyun error = xfs_inobt_insert_sprec(args.mp, tp, agbp, XFS_BTNUM_INO,
841*4882a593Smuzhiyun &rec, true);
842*4882a593Smuzhiyun if (error == -EFSCORRUPTED) {
843*4882a593Smuzhiyun xfs_alert(args.mp,
844*4882a593Smuzhiyun "invalid sparse inode record: ino 0x%llx holemask 0x%x count %u",
845*4882a593Smuzhiyun XFS_AGINO_TO_INO(args.mp, agno,
846*4882a593Smuzhiyun rec.ir_startino),
847*4882a593Smuzhiyun rec.ir_holemask, rec.ir_count);
848*4882a593Smuzhiyun xfs_force_shutdown(args.mp, SHUTDOWN_CORRUPT_INCORE);
849*4882a593Smuzhiyun }
850*4882a593Smuzhiyun if (error)
851*4882a593Smuzhiyun return error;
852*4882a593Smuzhiyun
853*4882a593Smuzhiyun /*
854*4882a593Smuzhiyun * We can't merge the part we've just allocated as for the inobt
855*4882a593Smuzhiyun * due to finobt semantics. The original record may or may not
856*4882a593Smuzhiyun * exist independent of whether physical inodes exist in this
857*4882a593Smuzhiyun * sparse chunk.
858*4882a593Smuzhiyun *
859*4882a593Smuzhiyun * We must update the finobt record based on the inobt record.
860*4882a593Smuzhiyun * rec contains the fully merged and up to date inobt record
861*4882a593Smuzhiyun * from the previous call. Set merge false to replace any
862*4882a593Smuzhiyun * existing record with this one.
863*4882a593Smuzhiyun */
864*4882a593Smuzhiyun if (xfs_sb_version_hasfinobt(&args.mp->m_sb)) {
865*4882a593Smuzhiyun error = xfs_inobt_insert_sprec(args.mp, tp, agbp,
866*4882a593Smuzhiyun XFS_BTNUM_FINO, &rec,
867*4882a593Smuzhiyun false);
868*4882a593Smuzhiyun if (error)
869*4882a593Smuzhiyun return error;
870*4882a593Smuzhiyun }
871*4882a593Smuzhiyun } else {
872*4882a593Smuzhiyun /* full chunk - insert new records to both btrees */
873*4882a593Smuzhiyun error = xfs_inobt_insert(args.mp, tp, agbp, newino, newlen,
874*4882a593Smuzhiyun XFS_BTNUM_INO);
875*4882a593Smuzhiyun if (error)
876*4882a593Smuzhiyun return error;
877*4882a593Smuzhiyun
878*4882a593Smuzhiyun if (xfs_sb_version_hasfinobt(&args.mp->m_sb)) {
879*4882a593Smuzhiyun error = xfs_inobt_insert(args.mp, tp, agbp, newino,
880*4882a593Smuzhiyun newlen, XFS_BTNUM_FINO);
881*4882a593Smuzhiyun if (error)
882*4882a593Smuzhiyun return error;
883*4882a593Smuzhiyun }
884*4882a593Smuzhiyun }
885*4882a593Smuzhiyun
886*4882a593Smuzhiyun /*
887*4882a593Smuzhiyun * Update AGI counts and newino.
888*4882a593Smuzhiyun */
889*4882a593Smuzhiyun be32_add_cpu(&agi->agi_count, newlen);
890*4882a593Smuzhiyun be32_add_cpu(&agi->agi_freecount, newlen);
891*4882a593Smuzhiyun pag = agbp->b_pag;
892*4882a593Smuzhiyun pag->pagi_freecount += newlen;
893*4882a593Smuzhiyun pag->pagi_count += newlen;
894*4882a593Smuzhiyun agi->agi_newino = cpu_to_be32(newino);
895*4882a593Smuzhiyun
896*4882a593Smuzhiyun /*
897*4882a593Smuzhiyun * Log allocation group header fields
898*4882a593Smuzhiyun */
899*4882a593Smuzhiyun xfs_ialloc_log_agi(tp, agbp,
900*4882a593Smuzhiyun XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
901*4882a593Smuzhiyun /*
902*4882a593Smuzhiyun * Modify/log superblock values for inode count and inode free count.
903*4882a593Smuzhiyun */
904*4882a593Smuzhiyun xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
905*4882a593Smuzhiyun xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
906*4882a593Smuzhiyun *alloc = 1;
907*4882a593Smuzhiyun return 0;
908*4882a593Smuzhiyun }
909*4882a593Smuzhiyun
910*4882a593Smuzhiyun STATIC xfs_agnumber_t
xfs_ialloc_next_ag(xfs_mount_t * mp)911*4882a593Smuzhiyun xfs_ialloc_next_ag(
912*4882a593Smuzhiyun xfs_mount_t *mp)
913*4882a593Smuzhiyun {
914*4882a593Smuzhiyun xfs_agnumber_t agno;
915*4882a593Smuzhiyun
916*4882a593Smuzhiyun spin_lock(&mp->m_agirotor_lock);
917*4882a593Smuzhiyun agno = mp->m_agirotor;
918*4882a593Smuzhiyun if (++mp->m_agirotor >= mp->m_maxagi)
919*4882a593Smuzhiyun mp->m_agirotor = 0;
920*4882a593Smuzhiyun spin_unlock(&mp->m_agirotor_lock);
921*4882a593Smuzhiyun
922*4882a593Smuzhiyun return agno;
923*4882a593Smuzhiyun }
924*4882a593Smuzhiyun
925*4882a593Smuzhiyun /*
926*4882a593Smuzhiyun * Select an allocation group to look for a free inode in, based on the parent
927*4882a593Smuzhiyun * inode and the mode. Return the allocation group buffer.
928*4882a593Smuzhiyun */
929*4882a593Smuzhiyun STATIC xfs_agnumber_t
xfs_ialloc_ag_select(xfs_trans_t * tp,xfs_ino_t parent,umode_t mode)930*4882a593Smuzhiyun xfs_ialloc_ag_select(
931*4882a593Smuzhiyun xfs_trans_t *tp, /* transaction pointer */
932*4882a593Smuzhiyun xfs_ino_t parent, /* parent directory inode number */
933*4882a593Smuzhiyun umode_t mode) /* bits set to indicate file type */
934*4882a593Smuzhiyun {
935*4882a593Smuzhiyun xfs_agnumber_t agcount; /* number of ag's in the filesystem */
936*4882a593Smuzhiyun xfs_agnumber_t agno; /* current ag number */
937*4882a593Smuzhiyun int flags; /* alloc buffer locking flags */
938*4882a593Smuzhiyun xfs_extlen_t ineed; /* blocks needed for inode allocation */
939*4882a593Smuzhiyun xfs_extlen_t longest = 0; /* longest extent available */
940*4882a593Smuzhiyun xfs_mount_t *mp; /* mount point structure */
941*4882a593Smuzhiyun int needspace; /* file mode implies space allocated */
942*4882a593Smuzhiyun xfs_perag_t *pag; /* per allocation group data */
943*4882a593Smuzhiyun xfs_agnumber_t pagno; /* parent (starting) ag number */
944*4882a593Smuzhiyun int error;
945*4882a593Smuzhiyun
946*4882a593Smuzhiyun /*
947*4882a593Smuzhiyun * Files of these types need at least one block if length > 0
948*4882a593Smuzhiyun * (and they won't fit in the inode, but that's hard to figure out).
949*4882a593Smuzhiyun */
950*4882a593Smuzhiyun needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
951*4882a593Smuzhiyun mp = tp->t_mountp;
952*4882a593Smuzhiyun agcount = mp->m_maxagi;
953*4882a593Smuzhiyun if (S_ISDIR(mode))
954*4882a593Smuzhiyun pagno = xfs_ialloc_next_ag(mp);
955*4882a593Smuzhiyun else {
956*4882a593Smuzhiyun pagno = XFS_INO_TO_AGNO(mp, parent);
957*4882a593Smuzhiyun if (pagno >= agcount)
958*4882a593Smuzhiyun pagno = 0;
959*4882a593Smuzhiyun }
960*4882a593Smuzhiyun
961*4882a593Smuzhiyun ASSERT(pagno < agcount);
962*4882a593Smuzhiyun
963*4882a593Smuzhiyun /*
964*4882a593Smuzhiyun * Loop through allocation groups, looking for one with a little
965*4882a593Smuzhiyun * free space in it. Note we don't look for free inodes, exactly.
966*4882a593Smuzhiyun * Instead, we include whether there is a need to allocate inodes
967*4882a593Smuzhiyun * to mean that blocks must be allocated for them,
968*4882a593Smuzhiyun * if none are currently free.
969*4882a593Smuzhiyun */
970*4882a593Smuzhiyun agno = pagno;
971*4882a593Smuzhiyun flags = XFS_ALLOC_FLAG_TRYLOCK;
972*4882a593Smuzhiyun for (;;) {
973*4882a593Smuzhiyun pag = xfs_perag_get(mp, agno);
974*4882a593Smuzhiyun if (!pag->pagi_inodeok) {
975*4882a593Smuzhiyun xfs_ialloc_next_ag(mp);
976*4882a593Smuzhiyun goto nextag;
977*4882a593Smuzhiyun }
978*4882a593Smuzhiyun
979*4882a593Smuzhiyun if (!pag->pagi_init) {
980*4882a593Smuzhiyun error = xfs_ialloc_pagi_init(mp, tp, agno);
981*4882a593Smuzhiyun if (error)
982*4882a593Smuzhiyun goto nextag;
983*4882a593Smuzhiyun }
984*4882a593Smuzhiyun
985*4882a593Smuzhiyun if (pag->pagi_freecount) {
986*4882a593Smuzhiyun xfs_perag_put(pag);
987*4882a593Smuzhiyun return agno;
988*4882a593Smuzhiyun }
989*4882a593Smuzhiyun
990*4882a593Smuzhiyun if (!pag->pagf_init) {
991*4882a593Smuzhiyun error = xfs_alloc_pagf_init(mp, tp, agno, flags);
992*4882a593Smuzhiyun if (error)
993*4882a593Smuzhiyun goto nextag;
994*4882a593Smuzhiyun }
995*4882a593Smuzhiyun
996*4882a593Smuzhiyun /*
997*4882a593Smuzhiyun * Check that there is enough free space for the file plus a
998*4882a593Smuzhiyun * chunk of inodes if we need to allocate some. If this is the
999*4882a593Smuzhiyun * first pass across the AGs, take into account the potential
1000*4882a593Smuzhiyun * space needed for alignment of inode chunks when checking the
1001*4882a593Smuzhiyun * longest contiguous free space in the AG - this prevents us
1002*4882a593Smuzhiyun * from getting ENOSPC because we have free space larger than
1003*4882a593Smuzhiyun * ialloc_blks but alignment constraints prevent us from using
1004*4882a593Smuzhiyun * it.
1005*4882a593Smuzhiyun *
1006*4882a593Smuzhiyun * If we can't find an AG with space for full alignment slack to
1007*4882a593Smuzhiyun * be taken into account, we must be near ENOSPC in all AGs.
1008*4882a593Smuzhiyun * Hence we don't include alignment for the second pass and so
1009*4882a593Smuzhiyun * if we fail allocation due to alignment issues then it is most
1010*4882a593Smuzhiyun * likely a real ENOSPC condition.
1011*4882a593Smuzhiyun */
1012*4882a593Smuzhiyun ineed = M_IGEO(mp)->ialloc_min_blks;
1013*4882a593Smuzhiyun if (flags && ineed > 1)
1014*4882a593Smuzhiyun ineed += M_IGEO(mp)->cluster_align;
1015*4882a593Smuzhiyun longest = pag->pagf_longest;
1016*4882a593Smuzhiyun if (!longest)
1017*4882a593Smuzhiyun longest = pag->pagf_flcount > 0;
1018*4882a593Smuzhiyun
1019*4882a593Smuzhiyun if (pag->pagf_freeblks >= needspace + ineed &&
1020*4882a593Smuzhiyun longest >= ineed) {
1021*4882a593Smuzhiyun xfs_perag_put(pag);
1022*4882a593Smuzhiyun return agno;
1023*4882a593Smuzhiyun }
1024*4882a593Smuzhiyun nextag:
1025*4882a593Smuzhiyun xfs_perag_put(pag);
1026*4882a593Smuzhiyun /*
1027*4882a593Smuzhiyun * No point in iterating over the rest, if we're shutting
1028*4882a593Smuzhiyun * down.
1029*4882a593Smuzhiyun */
1030*4882a593Smuzhiyun if (XFS_FORCED_SHUTDOWN(mp))
1031*4882a593Smuzhiyun return NULLAGNUMBER;
1032*4882a593Smuzhiyun agno++;
1033*4882a593Smuzhiyun if (agno >= agcount)
1034*4882a593Smuzhiyun agno = 0;
1035*4882a593Smuzhiyun if (agno == pagno) {
1036*4882a593Smuzhiyun if (flags == 0)
1037*4882a593Smuzhiyun return NULLAGNUMBER;
1038*4882a593Smuzhiyun flags = 0;
1039*4882a593Smuzhiyun }
1040*4882a593Smuzhiyun }
1041*4882a593Smuzhiyun }
1042*4882a593Smuzhiyun
1043*4882a593Smuzhiyun /*
1044*4882a593Smuzhiyun * Try to retrieve the next record to the left/right from the current one.
1045*4882a593Smuzhiyun */
1046*4882a593Smuzhiyun STATIC int
xfs_ialloc_next_rec(struct xfs_btree_cur * cur,xfs_inobt_rec_incore_t * rec,int * done,int left)1047*4882a593Smuzhiyun xfs_ialloc_next_rec(
1048*4882a593Smuzhiyun struct xfs_btree_cur *cur,
1049*4882a593Smuzhiyun xfs_inobt_rec_incore_t *rec,
1050*4882a593Smuzhiyun int *done,
1051*4882a593Smuzhiyun int left)
1052*4882a593Smuzhiyun {
1053*4882a593Smuzhiyun int error;
1054*4882a593Smuzhiyun int i;
1055*4882a593Smuzhiyun
1056*4882a593Smuzhiyun if (left)
1057*4882a593Smuzhiyun error = xfs_btree_decrement(cur, 0, &i);
1058*4882a593Smuzhiyun else
1059*4882a593Smuzhiyun error = xfs_btree_increment(cur, 0, &i);
1060*4882a593Smuzhiyun
1061*4882a593Smuzhiyun if (error)
1062*4882a593Smuzhiyun return error;
1063*4882a593Smuzhiyun *done = !i;
1064*4882a593Smuzhiyun if (i) {
1065*4882a593Smuzhiyun error = xfs_inobt_get_rec(cur, rec, &i);
1066*4882a593Smuzhiyun if (error)
1067*4882a593Smuzhiyun return error;
1068*4882a593Smuzhiyun if (XFS_IS_CORRUPT(cur->bc_mp, i != 1))
1069*4882a593Smuzhiyun return -EFSCORRUPTED;
1070*4882a593Smuzhiyun }
1071*4882a593Smuzhiyun
1072*4882a593Smuzhiyun return 0;
1073*4882a593Smuzhiyun }
1074*4882a593Smuzhiyun
1075*4882a593Smuzhiyun STATIC int
xfs_ialloc_get_rec(struct xfs_btree_cur * cur,xfs_agino_t agino,xfs_inobt_rec_incore_t * rec,int * done)1076*4882a593Smuzhiyun xfs_ialloc_get_rec(
1077*4882a593Smuzhiyun struct xfs_btree_cur *cur,
1078*4882a593Smuzhiyun xfs_agino_t agino,
1079*4882a593Smuzhiyun xfs_inobt_rec_incore_t *rec,
1080*4882a593Smuzhiyun int *done)
1081*4882a593Smuzhiyun {
1082*4882a593Smuzhiyun int error;
1083*4882a593Smuzhiyun int i;
1084*4882a593Smuzhiyun
1085*4882a593Smuzhiyun error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_EQ, &i);
1086*4882a593Smuzhiyun if (error)
1087*4882a593Smuzhiyun return error;
1088*4882a593Smuzhiyun *done = !i;
1089*4882a593Smuzhiyun if (i) {
1090*4882a593Smuzhiyun error = xfs_inobt_get_rec(cur, rec, &i);
1091*4882a593Smuzhiyun if (error)
1092*4882a593Smuzhiyun return error;
1093*4882a593Smuzhiyun if (XFS_IS_CORRUPT(cur->bc_mp, i != 1))
1094*4882a593Smuzhiyun return -EFSCORRUPTED;
1095*4882a593Smuzhiyun }
1096*4882a593Smuzhiyun
1097*4882a593Smuzhiyun return 0;
1098*4882a593Smuzhiyun }
1099*4882a593Smuzhiyun
1100*4882a593Smuzhiyun /*
1101*4882a593Smuzhiyun * Return the offset of the first free inode in the record. If the inode chunk
1102*4882a593Smuzhiyun * is sparsely allocated, we convert the record holemask to inode granularity
1103*4882a593Smuzhiyun * and mask off the unallocated regions from the inode free mask.
1104*4882a593Smuzhiyun */
1105*4882a593Smuzhiyun STATIC int
xfs_inobt_first_free_inode(struct xfs_inobt_rec_incore * rec)1106*4882a593Smuzhiyun xfs_inobt_first_free_inode(
1107*4882a593Smuzhiyun struct xfs_inobt_rec_incore *rec)
1108*4882a593Smuzhiyun {
1109*4882a593Smuzhiyun xfs_inofree_t realfree;
1110*4882a593Smuzhiyun
1111*4882a593Smuzhiyun /* if there are no holes, return the first available offset */
1112*4882a593Smuzhiyun if (!xfs_inobt_issparse(rec->ir_holemask))
1113*4882a593Smuzhiyun return xfs_lowbit64(rec->ir_free);
1114*4882a593Smuzhiyun
1115*4882a593Smuzhiyun realfree = xfs_inobt_irec_to_allocmask(rec);
1116*4882a593Smuzhiyun realfree &= rec->ir_free;
1117*4882a593Smuzhiyun
1118*4882a593Smuzhiyun return xfs_lowbit64(realfree);
1119*4882a593Smuzhiyun }
1120*4882a593Smuzhiyun
1121*4882a593Smuzhiyun /*
1122*4882a593Smuzhiyun * Allocate an inode using the inobt-only algorithm.
1123*4882a593Smuzhiyun */
1124*4882a593Smuzhiyun STATIC int
xfs_dialloc_ag_inobt(struct xfs_trans * tp,struct xfs_buf * agbp,xfs_ino_t parent,xfs_ino_t * inop)1125*4882a593Smuzhiyun xfs_dialloc_ag_inobt(
1126*4882a593Smuzhiyun struct xfs_trans *tp,
1127*4882a593Smuzhiyun struct xfs_buf *agbp,
1128*4882a593Smuzhiyun xfs_ino_t parent,
1129*4882a593Smuzhiyun xfs_ino_t *inop)
1130*4882a593Smuzhiyun {
1131*4882a593Smuzhiyun struct xfs_mount *mp = tp->t_mountp;
1132*4882a593Smuzhiyun struct xfs_agi *agi = agbp->b_addr;
1133*4882a593Smuzhiyun xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
1134*4882a593Smuzhiyun xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent);
1135*4882a593Smuzhiyun xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent);
1136*4882a593Smuzhiyun struct xfs_perag *pag = agbp->b_pag;
1137*4882a593Smuzhiyun struct xfs_btree_cur *cur, *tcur;
1138*4882a593Smuzhiyun struct xfs_inobt_rec_incore rec, trec;
1139*4882a593Smuzhiyun xfs_ino_t ino;
1140*4882a593Smuzhiyun int error;
1141*4882a593Smuzhiyun int offset;
1142*4882a593Smuzhiyun int i, j;
1143*4882a593Smuzhiyun int searchdistance = 10;
1144*4882a593Smuzhiyun
1145*4882a593Smuzhiyun ASSERT(pag->pagi_init);
1146*4882a593Smuzhiyun ASSERT(pag->pagi_inodeok);
1147*4882a593Smuzhiyun ASSERT(pag->pagi_freecount > 0);
1148*4882a593Smuzhiyun
1149*4882a593Smuzhiyun restart_pagno:
1150*4882a593Smuzhiyun cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
1151*4882a593Smuzhiyun /*
1152*4882a593Smuzhiyun * If pagino is 0 (this is the root inode allocation) use newino.
1153*4882a593Smuzhiyun * This must work because we've just allocated some.
1154*4882a593Smuzhiyun */
1155*4882a593Smuzhiyun if (!pagino)
1156*4882a593Smuzhiyun pagino = be32_to_cpu(agi->agi_newino);
1157*4882a593Smuzhiyun
1158*4882a593Smuzhiyun error = xfs_check_agi_freecount(cur, agi);
1159*4882a593Smuzhiyun if (error)
1160*4882a593Smuzhiyun goto error0;
1161*4882a593Smuzhiyun
1162*4882a593Smuzhiyun /*
1163*4882a593Smuzhiyun * If in the same AG as the parent, try to get near the parent.
1164*4882a593Smuzhiyun */
1165*4882a593Smuzhiyun if (pagno == agno) {
1166*4882a593Smuzhiyun int doneleft; /* done, to the left */
1167*4882a593Smuzhiyun int doneright; /* done, to the right */
1168*4882a593Smuzhiyun
1169*4882a593Smuzhiyun error = xfs_inobt_lookup(cur, pagino, XFS_LOOKUP_LE, &i);
1170*4882a593Smuzhiyun if (error)
1171*4882a593Smuzhiyun goto error0;
1172*4882a593Smuzhiyun if (XFS_IS_CORRUPT(mp, i != 1)) {
1173*4882a593Smuzhiyun error = -EFSCORRUPTED;
1174*4882a593Smuzhiyun goto error0;
1175*4882a593Smuzhiyun }
1176*4882a593Smuzhiyun
1177*4882a593Smuzhiyun error = xfs_inobt_get_rec(cur, &rec, &j);
1178*4882a593Smuzhiyun if (error)
1179*4882a593Smuzhiyun goto error0;
1180*4882a593Smuzhiyun if (XFS_IS_CORRUPT(mp, j != 1)) {
1181*4882a593Smuzhiyun error = -EFSCORRUPTED;
1182*4882a593Smuzhiyun goto error0;
1183*4882a593Smuzhiyun }
1184*4882a593Smuzhiyun
1185*4882a593Smuzhiyun if (rec.ir_freecount > 0) {
1186*4882a593Smuzhiyun /*
1187*4882a593Smuzhiyun * Found a free inode in the same chunk
1188*4882a593Smuzhiyun * as the parent, done.
1189*4882a593Smuzhiyun */
1190*4882a593Smuzhiyun goto alloc_inode;
1191*4882a593Smuzhiyun }
1192*4882a593Smuzhiyun
1193*4882a593Smuzhiyun
1194*4882a593Smuzhiyun /*
1195*4882a593Smuzhiyun * In the same AG as parent, but parent's chunk is full.
1196*4882a593Smuzhiyun */
1197*4882a593Smuzhiyun
1198*4882a593Smuzhiyun /* duplicate the cursor, search left & right simultaneously */
1199*4882a593Smuzhiyun error = xfs_btree_dup_cursor(cur, &tcur);
1200*4882a593Smuzhiyun if (error)
1201*4882a593Smuzhiyun goto error0;
1202*4882a593Smuzhiyun
1203*4882a593Smuzhiyun /*
1204*4882a593Smuzhiyun * Skip to last blocks looked up if same parent inode.
1205*4882a593Smuzhiyun */
1206*4882a593Smuzhiyun if (pagino != NULLAGINO &&
1207*4882a593Smuzhiyun pag->pagl_pagino == pagino &&
1208*4882a593Smuzhiyun pag->pagl_leftrec != NULLAGINO &&
1209*4882a593Smuzhiyun pag->pagl_rightrec != NULLAGINO) {
1210*4882a593Smuzhiyun error = xfs_ialloc_get_rec(tcur, pag->pagl_leftrec,
1211*4882a593Smuzhiyun &trec, &doneleft);
1212*4882a593Smuzhiyun if (error)
1213*4882a593Smuzhiyun goto error1;
1214*4882a593Smuzhiyun
1215*4882a593Smuzhiyun error = xfs_ialloc_get_rec(cur, pag->pagl_rightrec,
1216*4882a593Smuzhiyun &rec, &doneright);
1217*4882a593Smuzhiyun if (error)
1218*4882a593Smuzhiyun goto error1;
1219*4882a593Smuzhiyun } else {
1220*4882a593Smuzhiyun /* search left with tcur, back up 1 record */
1221*4882a593Smuzhiyun error = xfs_ialloc_next_rec(tcur, &trec, &doneleft, 1);
1222*4882a593Smuzhiyun if (error)
1223*4882a593Smuzhiyun goto error1;
1224*4882a593Smuzhiyun
1225*4882a593Smuzhiyun /* search right with cur, go forward 1 record. */
1226*4882a593Smuzhiyun error = xfs_ialloc_next_rec(cur, &rec, &doneright, 0);
1227*4882a593Smuzhiyun if (error)
1228*4882a593Smuzhiyun goto error1;
1229*4882a593Smuzhiyun }
1230*4882a593Smuzhiyun
1231*4882a593Smuzhiyun /*
1232*4882a593Smuzhiyun * Loop until we find an inode chunk with a free inode.
1233*4882a593Smuzhiyun */
1234*4882a593Smuzhiyun while (--searchdistance > 0 && (!doneleft || !doneright)) {
1235*4882a593Smuzhiyun int useleft; /* using left inode chunk this time */
1236*4882a593Smuzhiyun
1237*4882a593Smuzhiyun /* figure out the closer block if both are valid. */
1238*4882a593Smuzhiyun if (!doneleft && !doneright) {
1239*4882a593Smuzhiyun useleft = pagino -
1240*4882a593Smuzhiyun (trec.ir_startino + XFS_INODES_PER_CHUNK - 1) <
1241*4882a593Smuzhiyun rec.ir_startino - pagino;
1242*4882a593Smuzhiyun } else {
1243*4882a593Smuzhiyun useleft = !doneleft;
1244*4882a593Smuzhiyun }
1245*4882a593Smuzhiyun
1246*4882a593Smuzhiyun /* free inodes to the left? */
1247*4882a593Smuzhiyun if (useleft && trec.ir_freecount) {
1248*4882a593Smuzhiyun xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1249*4882a593Smuzhiyun cur = tcur;
1250*4882a593Smuzhiyun
1251*4882a593Smuzhiyun pag->pagl_leftrec = trec.ir_startino;
1252*4882a593Smuzhiyun pag->pagl_rightrec = rec.ir_startino;
1253*4882a593Smuzhiyun pag->pagl_pagino = pagino;
1254*4882a593Smuzhiyun rec = trec;
1255*4882a593Smuzhiyun goto alloc_inode;
1256*4882a593Smuzhiyun }
1257*4882a593Smuzhiyun
1258*4882a593Smuzhiyun /* free inodes to the right? */
1259*4882a593Smuzhiyun if (!useleft && rec.ir_freecount) {
1260*4882a593Smuzhiyun xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
1261*4882a593Smuzhiyun
1262*4882a593Smuzhiyun pag->pagl_leftrec = trec.ir_startino;
1263*4882a593Smuzhiyun pag->pagl_rightrec = rec.ir_startino;
1264*4882a593Smuzhiyun pag->pagl_pagino = pagino;
1265*4882a593Smuzhiyun goto alloc_inode;
1266*4882a593Smuzhiyun }
1267*4882a593Smuzhiyun
1268*4882a593Smuzhiyun /* get next record to check */
1269*4882a593Smuzhiyun if (useleft) {
1270*4882a593Smuzhiyun error = xfs_ialloc_next_rec(tcur, &trec,
1271*4882a593Smuzhiyun &doneleft, 1);
1272*4882a593Smuzhiyun } else {
1273*4882a593Smuzhiyun error = xfs_ialloc_next_rec(cur, &rec,
1274*4882a593Smuzhiyun &doneright, 0);
1275*4882a593Smuzhiyun }
1276*4882a593Smuzhiyun if (error)
1277*4882a593Smuzhiyun goto error1;
1278*4882a593Smuzhiyun }
1279*4882a593Smuzhiyun
1280*4882a593Smuzhiyun if (searchdistance <= 0) {
1281*4882a593Smuzhiyun /*
1282*4882a593Smuzhiyun * Not in range - save last search
1283*4882a593Smuzhiyun * location and allocate a new inode
1284*4882a593Smuzhiyun */
1285*4882a593Smuzhiyun xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
1286*4882a593Smuzhiyun pag->pagl_leftrec = trec.ir_startino;
1287*4882a593Smuzhiyun pag->pagl_rightrec = rec.ir_startino;
1288*4882a593Smuzhiyun pag->pagl_pagino = pagino;
1289*4882a593Smuzhiyun
1290*4882a593Smuzhiyun } else {
1291*4882a593Smuzhiyun /*
1292*4882a593Smuzhiyun * We've reached the end of the btree. because
1293*4882a593Smuzhiyun * we are only searching a small chunk of the
1294*4882a593Smuzhiyun * btree each search, there is obviously free
1295*4882a593Smuzhiyun * inodes closer to the parent inode than we
1296*4882a593Smuzhiyun * are now. restart the search again.
1297*4882a593Smuzhiyun */
1298*4882a593Smuzhiyun pag->pagl_pagino = NULLAGINO;
1299*4882a593Smuzhiyun pag->pagl_leftrec = NULLAGINO;
1300*4882a593Smuzhiyun pag->pagl_rightrec = NULLAGINO;
1301*4882a593Smuzhiyun xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
1302*4882a593Smuzhiyun xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1303*4882a593Smuzhiyun goto restart_pagno;
1304*4882a593Smuzhiyun }
1305*4882a593Smuzhiyun }
1306*4882a593Smuzhiyun
1307*4882a593Smuzhiyun /*
1308*4882a593Smuzhiyun * In a different AG from the parent.
1309*4882a593Smuzhiyun * See if the most recently allocated block has any free.
1310*4882a593Smuzhiyun */
1311*4882a593Smuzhiyun if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
1312*4882a593Smuzhiyun error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
1313*4882a593Smuzhiyun XFS_LOOKUP_EQ, &i);
1314*4882a593Smuzhiyun if (error)
1315*4882a593Smuzhiyun goto error0;
1316*4882a593Smuzhiyun
1317*4882a593Smuzhiyun if (i == 1) {
1318*4882a593Smuzhiyun error = xfs_inobt_get_rec(cur, &rec, &j);
1319*4882a593Smuzhiyun if (error)
1320*4882a593Smuzhiyun goto error0;
1321*4882a593Smuzhiyun
1322*4882a593Smuzhiyun if (j == 1 && rec.ir_freecount > 0) {
1323*4882a593Smuzhiyun /*
1324*4882a593Smuzhiyun * The last chunk allocated in the group
1325*4882a593Smuzhiyun * still has a free inode.
1326*4882a593Smuzhiyun */
1327*4882a593Smuzhiyun goto alloc_inode;
1328*4882a593Smuzhiyun }
1329*4882a593Smuzhiyun }
1330*4882a593Smuzhiyun }
1331*4882a593Smuzhiyun
1332*4882a593Smuzhiyun /*
1333*4882a593Smuzhiyun * None left in the last group, search the whole AG
1334*4882a593Smuzhiyun */
1335*4882a593Smuzhiyun error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
1336*4882a593Smuzhiyun if (error)
1337*4882a593Smuzhiyun goto error0;
1338*4882a593Smuzhiyun if (XFS_IS_CORRUPT(mp, i != 1)) {
1339*4882a593Smuzhiyun error = -EFSCORRUPTED;
1340*4882a593Smuzhiyun goto error0;
1341*4882a593Smuzhiyun }
1342*4882a593Smuzhiyun
1343*4882a593Smuzhiyun for (;;) {
1344*4882a593Smuzhiyun error = xfs_inobt_get_rec(cur, &rec, &i);
1345*4882a593Smuzhiyun if (error)
1346*4882a593Smuzhiyun goto error0;
1347*4882a593Smuzhiyun if (XFS_IS_CORRUPT(mp, i != 1)) {
1348*4882a593Smuzhiyun error = -EFSCORRUPTED;
1349*4882a593Smuzhiyun goto error0;
1350*4882a593Smuzhiyun }
1351*4882a593Smuzhiyun if (rec.ir_freecount > 0)
1352*4882a593Smuzhiyun break;
1353*4882a593Smuzhiyun error = xfs_btree_increment(cur, 0, &i);
1354*4882a593Smuzhiyun if (error)
1355*4882a593Smuzhiyun goto error0;
1356*4882a593Smuzhiyun if (XFS_IS_CORRUPT(mp, i != 1)) {
1357*4882a593Smuzhiyun error = -EFSCORRUPTED;
1358*4882a593Smuzhiyun goto error0;
1359*4882a593Smuzhiyun }
1360*4882a593Smuzhiyun }
1361*4882a593Smuzhiyun
1362*4882a593Smuzhiyun alloc_inode:
1363*4882a593Smuzhiyun offset = xfs_inobt_first_free_inode(&rec);
1364*4882a593Smuzhiyun ASSERT(offset >= 0);
1365*4882a593Smuzhiyun ASSERT(offset < XFS_INODES_PER_CHUNK);
1366*4882a593Smuzhiyun ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
1367*4882a593Smuzhiyun XFS_INODES_PER_CHUNK) == 0);
1368*4882a593Smuzhiyun ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
1369*4882a593Smuzhiyun rec.ir_free &= ~XFS_INOBT_MASK(offset);
1370*4882a593Smuzhiyun rec.ir_freecount--;
1371*4882a593Smuzhiyun error = xfs_inobt_update(cur, &rec);
1372*4882a593Smuzhiyun if (error)
1373*4882a593Smuzhiyun goto error0;
1374*4882a593Smuzhiyun be32_add_cpu(&agi->agi_freecount, -1);
1375*4882a593Smuzhiyun xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1376*4882a593Smuzhiyun pag->pagi_freecount--;
1377*4882a593Smuzhiyun
1378*4882a593Smuzhiyun error = xfs_check_agi_freecount(cur, agi);
1379*4882a593Smuzhiyun if (error)
1380*4882a593Smuzhiyun goto error0;
1381*4882a593Smuzhiyun
1382*4882a593Smuzhiyun xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1383*4882a593Smuzhiyun xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
1384*4882a593Smuzhiyun *inop = ino;
1385*4882a593Smuzhiyun return 0;
1386*4882a593Smuzhiyun error1:
1387*4882a593Smuzhiyun xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
1388*4882a593Smuzhiyun error0:
1389*4882a593Smuzhiyun xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1390*4882a593Smuzhiyun return error;
1391*4882a593Smuzhiyun }
1392*4882a593Smuzhiyun
1393*4882a593Smuzhiyun /*
1394*4882a593Smuzhiyun * Use the free inode btree to allocate an inode based on distance from the
1395*4882a593Smuzhiyun * parent. Note that the provided cursor may be deleted and replaced.
1396*4882a593Smuzhiyun */
1397*4882a593Smuzhiyun STATIC int
xfs_dialloc_ag_finobt_near(xfs_agino_t pagino,struct xfs_btree_cur ** ocur,struct xfs_inobt_rec_incore * rec)1398*4882a593Smuzhiyun xfs_dialloc_ag_finobt_near(
1399*4882a593Smuzhiyun xfs_agino_t pagino,
1400*4882a593Smuzhiyun struct xfs_btree_cur **ocur,
1401*4882a593Smuzhiyun struct xfs_inobt_rec_incore *rec)
1402*4882a593Smuzhiyun {
1403*4882a593Smuzhiyun struct xfs_btree_cur *lcur = *ocur; /* left search cursor */
1404*4882a593Smuzhiyun struct xfs_btree_cur *rcur; /* right search cursor */
1405*4882a593Smuzhiyun struct xfs_inobt_rec_incore rrec;
1406*4882a593Smuzhiyun int error;
1407*4882a593Smuzhiyun int i, j;
1408*4882a593Smuzhiyun
1409*4882a593Smuzhiyun error = xfs_inobt_lookup(lcur, pagino, XFS_LOOKUP_LE, &i);
1410*4882a593Smuzhiyun if (error)
1411*4882a593Smuzhiyun return error;
1412*4882a593Smuzhiyun
1413*4882a593Smuzhiyun if (i == 1) {
1414*4882a593Smuzhiyun error = xfs_inobt_get_rec(lcur, rec, &i);
1415*4882a593Smuzhiyun if (error)
1416*4882a593Smuzhiyun return error;
1417*4882a593Smuzhiyun if (XFS_IS_CORRUPT(lcur->bc_mp, i != 1))
1418*4882a593Smuzhiyun return -EFSCORRUPTED;
1419*4882a593Smuzhiyun
1420*4882a593Smuzhiyun /*
1421*4882a593Smuzhiyun * See if we've landed in the parent inode record. The finobt
1422*4882a593Smuzhiyun * only tracks chunks with at least one free inode, so record
1423*4882a593Smuzhiyun * existence is enough.
1424*4882a593Smuzhiyun */
1425*4882a593Smuzhiyun if (pagino >= rec->ir_startino &&
1426*4882a593Smuzhiyun pagino < (rec->ir_startino + XFS_INODES_PER_CHUNK))
1427*4882a593Smuzhiyun return 0;
1428*4882a593Smuzhiyun }
1429*4882a593Smuzhiyun
1430*4882a593Smuzhiyun error = xfs_btree_dup_cursor(lcur, &rcur);
1431*4882a593Smuzhiyun if (error)
1432*4882a593Smuzhiyun return error;
1433*4882a593Smuzhiyun
1434*4882a593Smuzhiyun error = xfs_inobt_lookup(rcur, pagino, XFS_LOOKUP_GE, &j);
1435*4882a593Smuzhiyun if (error)
1436*4882a593Smuzhiyun goto error_rcur;
1437*4882a593Smuzhiyun if (j == 1) {
1438*4882a593Smuzhiyun error = xfs_inobt_get_rec(rcur, &rrec, &j);
1439*4882a593Smuzhiyun if (error)
1440*4882a593Smuzhiyun goto error_rcur;
1441*4882a593Smuzhiyun if (XFS_IS_CORRUPT(lcur->bc_mp, j != 1)) {
1442*4882a593Smuzhiyun error = -EFSCORRUPTED;
1443*4882a593Smuzhiyun goto error_rcur;
1444*4882a593Smuzhiyun }
1445*4882a593Smuzhiyun }
1446*4882a593Smuzhiyun
1447*4882a593Smuzhiyun if (XFS_IS_CORRUPT(lcur->bc_mp, i != 1 && j != 1)) {
1448*4882a593Smuzhiyun error = -EFSCORRUPTED;
1449*4882a593Smuzhiyun goto error_rcur;
1450*4882a593Smuzhiyun }
1451*4882a593Smuzhiyun if (i == 1 && j == 1) {
1452*4882a593Smuzhiyun /*
1453*4882a593Smuzhiyun * Both the left and right records are valid. Choose the closer
1454*4882a593Smuzhiyun * inode chunk to the target.
1455*4882a593Smuzhiyun */
1456*4882a593Smuzhiyun if ((pagino - rec->ir_startino + XFS_INODES_PER_CHUNK - 1) >
1457*4882a593Smuzhiyun (rrec.ir_startino - pagino)) {
1458*4882a593Smuzhiyun *rec = rrec;
1459*4882a593Smuzhiyun xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
1460*4882a593Smuzhiyun *ocur = rcur;
1461*4882a593Smuzhiyun } else {
1462*4882a593Smuzhiyun xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
1463*4882a593Smuzhiyun }
1464*4882a593Smuzhiyun } else if (j == 1) {
1465*4882a593Smuzhiyun /* only the right record is valid */
1466*4882a593Smuzhiyun *rec = rrec;
1467*4882a593Smuzhiyun xfs_btree_del_cursor(lcur, XFS_BTREE_NOERROR);
1468*4882a593Smuzhiyun *ocur = rcur;
1469*4882a593Smuzhiyun } else if (i == 1) {
1470*4882a593Smuzhiyun /* only the left record is valid */
1471*4882a593Smuzhiyun xfs_btree_del_cursor(rcur, XFS_BTREE_NOERROR);
1472*4882a593Smuzhiyun }
1473*4882a593Smuzhiyun
1474*4882a593Smuzhiyun return 0;
1475*4882a593Smuzhiyun
1476*4882a593Smuzhiyun error_rcur:
1477*4882a593Smuzhiyun xfs_btree_del_cursor(rcur, XFS_BTREE_ERROR);
1478*4882a593Smuzhiyun return error;
1479*4882a593Smuzhiyun }
1480*4882a593Smuzhiyun
1481*4882a593Smuzhiyun /*
1482*4882a593Smuzhiyun * Use the free inode btree to find a free inode based on a newino hint. If
1483*4882a593Smuzhiyun * the hint is NULL, find the first free inode in the AG.
1484*4882a593Smuzhiyun */
1485*4882a593Smuzhiyun STATIC int
xfs_dialloc_ag_finobt_newino(struct xfs_agi * agi,struct xfs_btree_cur * cur,struct xfs_inobt_rec_incore * rec)1486*4882a593Smuzhiyun xfs_dialloc_ag_finobt_newino(
1487*4882a593Smuzhiyun struct xfs_agi *agi,
1488*4882a593Smuzhiyun struct xfs_btree_cur *cur,
1489*4882a593Smuzhiyun struct xfs_inobt_rec_incore *rec)
1490*4882a593Smuzhiyun {
1491*4882a593Smuzhiyun int error;
1492*4882a593Smuzhiyun int i;
1493*4882a593Smuzhiyun
1494*4882a593Smuzhiyun if (agi->agi_newino != cpu_to_be32(NULLAGINO)) {
1495*4882a593Smuzhiyun error = xfs_inobt_lookup(cur, be32_to_cpu(agi->agi_newino),
1496*4882a593Smuzhiyun XFS_LOOKUP_EQ, &i);
1497*4882a593Smuzhiyun if (error)
1498*4882a593Smuzhiyun return error;
1499*4882a593Smuzhiyun if (i == 1) {
1500*4882a593Smuzhiyun error = xfs_inobt_get_rec(cur, rec, &i);
1501*4882a593Smuzhiyun if (error)
1502*4882a593Smuzhiyun return error;
1503*4882a593Smuzhiyun if (XFS_IS_CORRUPT(cur->bc_mp, i != 1))
1504*4882a593Smuzhiyun return -EFSCORRUPTED;
1505*4882a593Smuzhiyun return 0;
1506*4882a593Smuzhiyun }
1507*4882a593Smuzhiyun }
1508*4882a593Smuzhiyun
1509*4882a593Smuzhiyun /*
1510*4882a593Smuzhiyun * Find the first inode available in the AG.
1511*4882a593Smuzhiyun */
1512*4882a593Smuzhiyun error = xfs_inobt_lookup(cur, 0, XFS_LOOKUP_GE, &i);
1513*4882a593Smuzhiyun if (error)
1514*4882a593Smuzhiyun return error;
1515*4882a593Smuzhiyun if (XFS_IS_CORRUPT(cur->bc_mp, i != 1))
1516*4882a593Smuzhiyun return -EFSCORRUPTED;
1517*4882a593Smuzhiyun
1518*4882a593Smuzhiyun error = xfs_inobt_get_rec(cur, rec, &i);
1519*4882a593Smuzhiyun if (error)
1520*4882a593Smuzhiyun return error;
1521*4882a593Smuzhiyun if (XFS_IS_CORRUPT(cur->bc_mp, i != 1))
1522*4882a593Smuzhiyun return -EFSCORRUPTED;
1523*4882a593Smuzhiyun
1524*4882a593Smuzhiyun return 0;
1525*4882a593Smuzhiyun }
1526*4882a593Smuzhiyun
1527*4882a593Smuzhiyun /*
1528*4882a593Smuzhiyun * Update the inobt based on a modification made to the finobt. Also ensure that
1529*4882a593Smuzhiyun * the records from both trees are equivalent post-modification.
1530*4882a593Smuzhiyun */
1531*4882a593Smuzhiyun STATIC int
xfs_dialloc_ag_update_inobt(struct xfs_btree_cur * cur,struct xfs_inobt_rec_incore * frec,int offset)1532*4882a593Smuzhiyun xfs_dialloc_ag_update_inobt(
1533*4882a593Smuzhiyun struct xfs_btree_cur *cur, /* inobt cursor */
1534*4882a593Smuzhiyun struct xfs_inobt_rec_incore *frec, /* finobt record */
1535*4882a593Smuzhiyun int offset) /* inode offset */
1536*4882a593Smuzhiyun {
1537*4882a593Smuzhiyun struct xfs_inobt_rec_incore rec;
1538*4882a593Smuzhiyun int error;
1539*4882a593Smuzhiyun int i;
1540*4882a593Smuzhiyun
1541*4882a593Smuzhiyun error = xfs_inobt_lookup(cur, frec->ir_startino, XFS_LOOKUP_EQ, &i);
1542*4882a593Smuzhiyun if (error)
1543*4882a593Smuzhiyun return error;
1544*4882a593Smuzhiyun if (XFS_IS_CORRUPT(cur->bc_mp, i != 1))
1545*4882a593Smuzhiyun return -EFSCORRUPTED;
1546*4882a593Smuzhiyun
1547*4882a593Smuzhiyun error = xfs_inobt_get_rec(cur, &rec, &i);
1548*4882a593Smuzhiyun if (error)
1549*4882a593Smuzhiyun return error;
1550*4882a593Smuzhiyun if (XFS_IS_CORRUPT(cur->bc_mp, i != 1))
1551*4882a593Smuzhiyun return -EFSCORRUPTED;
1552*4882a593Smuzhiyun ASSERT((XFS_AGINO_TO_OFFSET(cur->bc_mp, rec.ir_startino) %
1553*4882a593Smuzhiyun XFS_INODES_PER_CHUNK) == 0);
1554*4882a593Smuzhiyun
1555*4882a593Smuzhiyun rec.ir_free &= ~XFS_INOBT_MASK(offset);
1556*4882a593Smuzhiyun rec.ir_freecount--;
1557*4882a593Smuzhiyun
1558*4882a593Smuzhiyun if (XFS_IS_CORRUPT(cur->bc_mp,
1559*4882a593Smuzhiyun rec.ir_free != frec->ir_free ||
1560*4882a593Smuzhiyun rec.ir_freecount != frec->ir_freecount))
1561*4882a593Smuzhiyun return -EFSCORRUPTED;
1562*4882a593Smuzhiyun
1563*4882a593Smuzhiyun return xfs_inobt_update(cur, &rec);
1564*4882a593Smuzhiyun }
1565*4882a593Smuzhiyun
1566*4882a593Smuzhiyun /*
1567*4882a593Smuzhiyun * Allocate an inode using the free inode btree, if available. Otherwise, fall
1568*4882a593Smuzhiyun * back to the inobt search algorithm.
1569*4882a593Smuzhiyun *
1570*4882a593Smuzhiyun * The caller selected an AG for us, and made sure that free inodes are
1571*4882a593Smuzhiyun * available.
1572*4882a593Smuzhiyun */
1573*4882a593Smuzhiyun STATIC int
xfs_dialloc_ag(struct xfs_trans * tp,struct xfs_buf * agbp,xfs_ino_t parent,xfs_ino_t * inop)1574*4882a593Smuzhiyun xfs_dialloc_ag(
1575*4882a593Smuzhiyun struct xfs_trans *tp,
1576*4882a593Smuzhiyun struct xfs_buf *agbp,
1577*4882a593Smuzhiyun xfs_ino_t parent,
1578*4882a593Smuzhiyun xfs_ino_t *inop)
1579*4882a593Smuzhiyun {
1580*4882a593Smuzhiyun struct xfs_mount *mp = tp->t_mountp;
1581*4882a593Smuzhiyun struct xfs_agi *agi = agbp->b_addr;
1582*4882a593Smuzhiyun xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
1583*4882a593Smuzhiyun xfs_agnumber_t pagno = XFS_INO_TO_AGNO(mp, parent);
1584*4882a593Smuzhiyun xfs_agino_t pagino = XFS_INO_TO_AGINO(mp, parent);
1585*4882a593Smuzhiyun struct xfs_btree_cur *cur; /* finobt cursor */
1586*4882a593Smuzhiyun struct xfs_btree_cur *icur; /* inobt cursor */
1587*4882a593Smuzhiyun struct xfs_inobt_rec_incore rec;
1588*4882a593Smuzhiyun xfs_ino_t ino;
1589*4882a593Smuzhiyun int error;
1590*4882a593Smuzhiyun int offset;
1591*4882a593Smuzhiyun int i;
1592*4882a593Smuzhiyun
1593*4882a593Smuzhiyun if (!xfs_sb_version_hasfinobt(&mp->m_sb))
1594*4882a593Smuzhiyun return xfs_dialloc_ag_inobt(tp, agbp, parent, inop);
1595*4882a593Smuzhiyun
1596*4882a593Smuzhiyun /*
1597*4882a593Smuzhiyun * If pagino is 0 (this is the root inode allocation) use newino.
1598*4882a593Smuzhiyun * This must work because we've just allocated some.
1599*4882a593Smuzhiyun */
1600*4882a593Smuzhiyun if (!pagino)
1601*4882a593Smuzhiyun pagino = be32_to_cpu(agi->agi_newino);
1602*4882a593Smuzhiyun
1603*4882a593Smuzhiyun cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO);
1604*4882a593Smuzhiyun
1605*4882a593Smuzhiyun error = xfs_check_agi_freecount(cur, agi);
1606*4882a593Smuzhiyun if (error)
1607*4882a593Smuzhiyun goto error_cur;
1608*4882a593Smuzhiyun
1609*4882a593Smuzhiyun /*
1610*4882a593Smuzhiyun * The search algorithm depends on whether we're in the same AG as the
1611*4882a593Smuzhiyun * parent. If so, find the closest available inode to the parent. If
1612*4882a593Smuzhiyun * not, consider the agi hint or find the first free inode in the AG.
1613*4882a593Smuzhiyun */
1614*4882a593Smuzhiyun if (agno == pagno)
1615*4882a593Smuzhiyun error = xfs_dialloc_ag_finobt_near(pagino, &cur, &rec);
1616*4882a593Smuzhiyun else
1617*4882a593Smuzhiyun error = xfs_dialloc_ag_finobt_newino(agi, cur, &rec);
1618*4882a593Smuzhiyun if (error)
1619*4882a593Smuzhiyun goto error_cur;
1620*4882a593Smuzhiyun
1621*4882a593Smuzhiyun offset = xfs_inobt_first_free_inode(&rec);
1622*4882a593Smuzhiyun ASSERT(offset >= 0);
1623*4882a593Smuzhiyun ASSERT(offset < XFS_INODES_PER_CHUNK);
1624*4882a593Smuzhiyun ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
1625*4882a593Smuzhiyun XFS_INODES_PER_CHUNK) == 0);
1626*4882a593Smuzhiyun ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
1627*4882a593Smuzhiyun
1628*4882a593Smuzhiyun /*
1629*4882a593Smuzhiyun * Modify or remove the finobt record.
1630*4882a593Smuzhiyun */
1631*4882a593Smuzhiyun rec.ir_free &= ~XFS_INOBT_MASK(offset);
1632*4882a593Smuzhiyun rec.ir_freecount--;
1633*4882a593Smuzhiyun if (rec.ir_freecount)
1634*4882a593Smuzhiyun error = xfs_inobt_update(cur, &rec);
1635*4882a593Smuzhiyun else
1636*4882a593Smuzhiyun error = xfs_btree_delete(cur, &i);
1637*4882a593Smuzhiyun if (error)
1638*4882a593Smuzhiyun goto error_cur;
1639*4882a593Smuzhiyun
1640*4882a593Smuzhiyun /*
1641*4882a593Smuzhiyun * The finobt has now been updated appropriately. We haven't updated the
1642*4882a593Smuzhiyun * agi and superblock yet, so we can create an inobt cursor and validate
1643*4882a593Smuzhiyun * the original freecount. If all is well, make the equivalent update to
1644*4882a593Smuzhiyun * the inobt using the finobt record and offset information.
1645*4882a593Smuzhiyun */
1646*4882a593Smuzhiyun icur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
1647*4882a593Smuzhiyun
1648*4882a593Smuzhiyun error = xfs_check_agi_freecount(icur, agi);
1649*4882a593Smuzhiyun if (error)
1650*4882a593Smuzhiyun goto error_icur;
1651*4882a593Smuzhiyun
1652*4882a593Smuzhiyun error = xfs_dialloc_ag_update_inobt(icur, &rec, offset);
1653*4882a593Smuzhiyun if (error)
1654*4882a593Smuzhiyun goto error_icur;
1655*4882a593Smuzhiyun
1656*4882a593Smuzhiyun /*
1657*4882a593Smuzhiyun * Both trees have now been updated. We must update the perag and
1658*4882a593Smuzhiyun * superblock before we can check the freecount for each btree.
1659*4882a593Smuzhiyun */
1660*4882a593Smuzhiyun be32_add_cpu(&agi->agi_freecount, -1);
1661*4882a593Smuzhiyun xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1662*4882a593Smuzhiyun agbp->b_pag->pagi_freecount--;
1663*4882a593Smuzhiyun
1664*4882a593Smuzhiyun xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
1665*4882a593Smuzhiyun
1666*4882a593Smuzhiyun error = xfs_check_agi_freecount(icur, agi);
1667*4882a593Smuzhiyun if (error)
1668*4882a593Smuzhiyun goto error_icur;
1669*4882a593Smuzhiyun error = xfs_check_agi_freecount(cur, agi);
1670*4882a593Smuzhiyun if (error)
1671*4882a593Smuzhiyun goto error_icur;
1672*4882a593Smuzhiyun
1673*4882a593Smuzhiyun xfs_btree_del_cursor(icur, XFS_BTREE_NOERROR);
1674*4882a593Smuzhiyun xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1675*4882a593Smuzhiyun *inop = ino;
1676*4882a593Smuzhiyun return 0;
1677*4882a593Smuzhiyun
1678*4882a593Smuzhiyun error_icur:
1679*4882a593Smuzhiyun xfs_btree_del_cursor(icur, XFS_BTREE_ERROR);
1680*4882a593Smuzhiyun error_cur:
1681*4882a593Smuzhiyun xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1682*4882a593Smuzhiyun return error;
1683*4882a593Smuzhiyun }
1684*4882a593Smuzhiyun
1685*4882a593Smuzhiyun /*
1686*4882a593Smuzhiyun * Allocate an inode on disk.
1687*4882a593Smuzhiyun *
1688*4882a593Smuzhiyun * Mode is used to tell whether the new inode will need space, and whether it
1689*4882a593Smuzhiyun * is a directory.
1690*4882a593Smuzhiyun *
1691*4882a593Smuzhiyun * This function is designed to be called twice if it has to do an allocation
1692*4882a593Smuzhiyun * to make more free inodes. On the first call, *IO_agbp should be set to NULL.
1693*4882a593Smuzhiyun * If an inode is available without having to performn an allocation, an inode
1694*4882a593Smuzhiyun * number is returned. In this case, *IO_agbp is set to NULL. If an allocation
1695*4882a593Smuzhiyun * needs to be done, xfs_dialloc returns the current AGI buffer in *IO_agbp.
1696*4882a593Smuzhiyun * The caller should then commit the current transaction, allocate a
1697*4882a593Smuzhiyun * new transaction, and call xfs_dialloc() again, passing in the previous value
1698*4882a593Smuzhiyun * of *IO_agbp. IO_agbp should be held across the transactions. Since the AGI
1699*4882a593Smuzhiyun * buffer is locked across the two calls, the second call is guaranteed to have
1700*4882a593Smuzhiyun * a free inode available.
1701*4882a593Smuzhiyun *
1702*4882a593Smuzhiyun * Once we successfully pick an inode its number is returned and the on-disk
1703*4882a593Smuzhiyun * data structures are updated. The inode itself is not read in, since doing so
1704*4882a593Smuzhiyun * would break ordering constraints with xfs_reclaim.
1705*4882a593Smuzhiyun */
1706*4882a593Smuzhiyun int
xfs_dialloc(struct xfs_trans * tp,xfs_ino_t parent,umode_t mode,struct xfs_buf ** IO_agbp,xfs_ino_t * inop)1707*4882a593Smuzhiyun xfs_dialloc(
1708*4882a593Smuzhiyun struct xfs_trans *tp,
1709*4882a593Smuzhiyun xfs_ino_t parent,
1710*4882a593Smuzhiyun umode_t mode,
1711*4882a593Smuzhiyun struct xfs_buf **IO_agbp,
1712*4882a593Smuzhiyun xfs_ino_t *inop)
1713*4882a593Smuzhiyun {
1714*4882a593Smuzhiyun struct xfs_mount *mp = tp->t_mountp;
1715*4882a593Smuzhiyun struct xfs_buf *agbp;
1716*4882a593Smuzhiyun xfs_agnumber_t agno;
1717*4882a593Smuzhiyun int error;
1718*4882a593Smuzhiyun int ialloced;
1719*4882a593Smuzhiyun int noroom = 0;
1720*4882a593Smuzhiyun xfs_agnumber_t start_agno;
1721*4882a593Smuzhiyun struct xfs_perag *pag;
1722*4882a593Smuzhiyun struct xfs_ino_geometry *igeo = M_IGEO(mp);
1723*4882a593Smuzhiyun int okalloc = 1;
1724*4882a593Smuzhiyun
1725*4882a593Smuzhiyun if (*IO_agbp) {
1726*4882a593Smuzhiyun /*
1727*4882a593Smuzhiyun * If the caller passes in a pointer to the AGI buffer,
1728*4882a593Smuzhiyun * continue where we left off before. In this case, we
1729*4882a593Smuzhiyun * know that the allocation group has free inodes.
1730*4882a593Smuzhiyun */
1731*4882a593Smuzhiyun agbp = *IO_agbp;
1732*4882a593Smuzhiyun goto out_alloc;
1733*4882a593Smuzhiyun }
1734*4882a593Smuzhiyun
1735*4882a593Smuzhiyun /*
1736*4882a593Smuzhiyun * We do not have an agbp, so select an initial allocation
1737*4882a593Smuzhiyun * group for inode allocation.
1738*4882a593Smuzhiyun */
1739*4882a593Smuzhiyun start_agno = xfs_ialloc_ag_select(tp, parent, mode);
1740*4882a593Smuzhiyun if (start_agno == NULLAGNUMBER) {
1741*4882a593Smuzhiyun *inop = NULLFSINO;
1742*4882a593Smuzhiyun return 0;
1743*4882a593Smuzhiyun }
1744*4882a593Smuzhiyun
1745*4882a593Smuzhiyun /*
1746*4882a593Smuzhiyun * If we have already hit the ceiling of inode blocks then clear
1747*4882a593Smuzhiyun * okalloc so we scan all available agi structures for a free
1748*4882a593Smuzhiyun * inode.
1749*4882a593Smuzhiyun *
1750*4882a593Smuzhiyun * Read rough value of mp->m_icount by percpu_counter_read_positive,
1751*4882a593Smuzhiyun * which will sacrifice the preciseness but improve the performance.
1752*4882a593Smuzhiyun */
1753*4882a593Smuzhiyun if (igeo->maxicount &&
1754*4882a593Smuzhiyun percpu_counter_read_positive(&mp->m_icount) + igeo->ialloc_inos
1755*4882a593Smuzhiyun > igeo->maxicount) {
1756*4882a593Smuzhiyun noroom = 1;
1757*4882a593Smuzhiyun okalloc = 0;
1758*4882a593Smuzhiyun }
1759*4882a593Smuzhiyun
1760*4882a593Smuzhiyun /*
1761*4882a593Smuzhiyun * Loop until we find an allocation group that either has free inodes
1762*4882a593Smuzhiyun * or in which we can allocate some inodes. Iterate through the
1763*4882a593Smuzhiyun * allocation groups upward, wrapping at the end.
1764*4882a593Smuzhiyun */
1765*4882a593Smuzhiyun agno = start_agno;
1766*4882a593Smuzhiyun for (;;) {
1767*4882a593Smuzhiyun pag = xfs_perag_get(mp, agno);
1768*4882a593Smuzhiyun if (!pag->pagi_inodeok) {
1769*4882a593Smuzhiyun xfs_ialloc_next_ag(mp);
1770*4882a593Smuzhiyun goto nextag;
1771*4882a593Smuzhiyun }
1772*4882a593Smuzhiyun
1773*4882a593Smuzhiyun if (!pag->pagi_init) {
1774*4882a593Smuzhiyun error = xfs_ialloc_pagi_init(mp, tp, agno);
1775*4882a593Smuzhiyun if (error)
1776*4882a593Smuzhiyun goto out_error;
1777*4882a593Smuzhiyun }
1778*4882a593Smuzhiyun
1779*4882a593Smuzhiyun /*
1780*4882a593Smuzhiyun * Do a first racy fast path check if this AG is usable.
1781*4882a593Smuzhiyun */
1782*4882a593Smuzhiyun if (!pag->pagi_freecount && !okalloc)
1783*4882a593Smuzhiyun goto nextag;
1784*4882a593Smuzhiyun
1785*4882a593Smuzhiyun /*
1786*4882a593Smuzhiyun * Then read in the AGI buffer and recheck with the AGI buffer
1787*4882a593Smuzhiyun * lock held.
1788*4882a593Smuzhiyun */
1789*4882a593Smuzhiyun error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1790*4882a593Smuzhiyun if (error)
1791*4882a593Smuzhiyun goto out_error;
1792*4882a593Smuzhiyun
1793*4882a593Smuzhiyun if (pag->pagi_freecount) {
1794*4882a593Smuzhiyun xfs_perag_put(pag);
1795*4882a593Smuzhiyun goto out_alloc;
1796*4882a593Smuzhiyun }
1797*4882a593Smuzhiyun
1798*4882a593Smuzhiyun if (!okalloc)
1799*4882a593Smuzhiyun goto nextag_relse_buffer;
1800*4882a593Smuzhiyun
1801*4882a593Smuzhiyun
1802*4882a593Smuzhiyun error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced);
1803*4882a593Smuzhiyun if (error) {
1804*4882a593Smuzhiyun xfs_trans_brelse(tp, agbp);
1805*4882a593Smuzhiyun
1806*4882a593Smuzhiyun if (error != -ENOSPC)
1807*4882a593Smuzhiyun goto out_error;
1808*4882a593Smuzhiyun
1809*4882a593Smuzhiyun xfs_perag_put(pag);
1810*4882a593Smuzhiyun *inop = NULLFSINO;
1811*4882a593Smuzhiyun return 0;
1812*4882a593Smuzhiyun }
1813*4882a593Smuzhiyun
1814*4882a593Smuzhiyun if (ialloced) {
1815*4882a593Smuzhiyun /*
1816*4882a593Smuzhiyun * We successfully allocated some inodes, return
1817*4882a593Smuzhiyun * the current context to the caller so that it
1818*4882a593Smuzhiyun * can commit the current transaction and call
1819*4882a593Smuzhiyun * us again where we left off.
1820*4882a593Smuzhiyun */
1821*4882a593Smuzhiyun ASSERT(pag->pagi_freecount > 0);
1822*4882a593Smuzhiyun xfs_perag_put(pag);
1823*4882a593Smuzhiyun
1824*4882a593Smuzhiyun *IO_agbp = agbp;
1825*4882a593Smuzhiyun *inop = NULLFSINO;
1826*4882a593Smuzhiyun return 0;
1827*4882a593Smuzhiyun }
1828*4882a593Smuzhiyun
1829*4882a593Smuzhiyun nextag_relse_buffer:
1830*4882a593Smuzhiyun xfs_trans_brelse(tp, agbp);
1831*4882a593Smuzhiyun nextag:
1832*4882a593Smuzhiyun xfs_perag_put(pag);
1833*4882a593Smuzhiyun if (++agno == mp->m_sb.sb_agcount)
1834*4882a593Smuzhiyun agno = 0;
1835*4882a593Smuzhiyun if (agno == start_agno) {
1836*4882a593Smuzhiyun *inop = NULLFSINO;
1837*4882a593Smuzhiyun return noroom ? -ENOSPC : 0;
1838*4882a593Smuzhiyun }
1839*4882a593Smuzhiyun }
1840*4882a593Smuzhiyun
1841*4882a593Smuzhiyun out_alloc:
1842*4882a593Smuzhiyun *IO_agbp = NULL;
1843*4882a593Smuzhiyun return xfs_dialloc_ag(tp, agbp, parent, inop);
1844*4882a593Smuzhiyun out_error:
1845*4882a593Smuzhiyun xfs_perag_put(pag);
1846*4882a593Smuzhiyun return error;
1847*4882a593Smuzhiyun }
1848*4882a593Smuzhiyun
1849*4882a593Smuzhiyun /*
1850*4882a593Smuzhiyun * Free the blocks of an inode chunk. We must consider that the inode chunk
1851*4882a593Smuzhiyun * might be sparse and only free the regions that are allocated as part of the
1852*4882a593Smuzhiyun * chunk.
1853*4882a593Smuzhiyun */
1854*4882a593Smuzhiyun STATIC void
xfs_difree_inode_chunk(struct xfs_trans * tp,xfs_agnumber_t agno,struct xfs_inobt_rec_incore * rec)1855*4882a593Smuzhiyun xfs_difree_inode_chunk(
1856*4882a593Smuzhiyun struct xfs_trans *tp,
1857*4882a593Smuzhiyun xfs_agnumber_t agno,
1858*4882a593Smuzhiyun struct xfs_inobt_rec_incore *rec)
1859*4882a593Smuzhiyun {
1860*4882a593Smuzhiyun struct xfs_mount *mp = tp->t_mountp;
1861*4882a593Smuzhiyun xfs_agblock_t sagbno = XFS_AGINO_TO_AGBNO(mp,
1862*4882a593Smuzhiyun rec->ir_startino);
1863*4882a593Smuzhiyun int startidx, endidx;
1864*4882a593Smuzhiyun int nextbit;
1865*4882a593Smuzhiyun xfs_agblock_t agbno;
1866*4882a593Smuzhiyun int contigblk;
1867*4882a593Smuzhiyun DECLARE_BITMAP(holemask, XFS_INOBT_HOLEMASK_BITS);
1868*4882a593Smuzhiyun
1869*4882a593Smuzhiyun if (!xfs_inobt_issparse(rec->ir_holemask)) {
1870*4882a593Smuzhiyun /* not sparse, calculate extent info directly */
1871*4882a593Smuzhiyun xfs_bmap_add_free(tp, XFS_AGB_TO_FSB(mp, agno, sagbno),
1872*4882a593Smuzhiyun M_IGEO(mp)->ialloc_blks,
1873*4882a593Smuzhiyun &XFS_RMAP_OINFO_INODES);
1874*4882a593Smuzhiyun return;
1875*4882a593Smuzhiyun }
1876*4882a593Smuzhiyun
1877*4882a593Smuzhiyun /* holemask is only 16-bits (fits in an unsigned long) */
1878*4882a593Smuzhiyun ASSERT(sizeof(rec->ir_holemask) <= sizeof(holemask[0]));
1879*4882a593Smuzhiyun holemask[0] = rec->ir_holemask;
1880*4882a593Smuzhiyun
1881*4882a593Smuzhiyun /*
1882*4882a593Smuzhiyun * Find contiguous ranges of zeroes (i.e., allocated regions) in the
1883*4882a593Smuzhiyun * holemask and convert the start/end index of each range to an extent.
1884*4882a593Smuzhiyun * We start with the start and end index both pointing at the first 0 in
1885*4882a593Smuzhiyun * the mask.
1886*4882a593Smuzhiyun */
1887*4882a593Smuzhiyun startidx = endidx = find_first_zero_bit(holemask,
1888*4882a593Smuzhiyun XFS_INOBT_HOLEMASK_BITS);
1889*4882a593Smuzhiyun nextbit = startidx + 1;
1890*4882a593Smuzhiyun while (startidx < XFS_INOBT_HOLEMASK_BITS) {
1891*4882a593Smuzhiyun nextbit = find_next_zero_bit(holemask, XFS_INOBT_HOLEMASK_BITS,
1892*4882a593Smuzhiyun nextbit);
1893*4882a593Smuzhiyun /*
1894*4882a593Smuzhiyun * If the next zero bit is contiguous, update the end index of
1895*4882a593Smuzhiyun * the current range and continue.
1896*4882a593Smuzhiyun */
1897*4882a593Smuzhiyun if (nextbit != XFS_INOBT_HOLEMASK_BITS &&
1898*4882a593Smuzhiyun nextbit == endidx + 1) {
1899*4882a593Smuzhiyun endidx = nextbit;
1900*4882a593Smuzhiyun goto next;
1901*4882a593Smuzhiyun }
1902*4882a593Smuzhiyun
1903*4882a593Smuzhiyun /*
1904*4882a593Smuzhiyun * nextbit is not contiguous with the current end index. Convert
1905*4882a593Smuzhiyun * the current start/end to an extent and add it to the free
1906*4882a593Smuzhiyun * list.
1907*4882a593Smuzhiyun */
1908*4882a593Smuzhiyun agbno = sagbno + (startidx * XFS_INODES_PER_HOLEMASK_BIT) /
1909*4882a593Smuzhiyun mp->m_sb.sb_inopblock;
1910*4882a593Smuzhiyun contigblk = ((endidx - startidx + 1) *
1911*4882a593Smuzhiyun XFS_INODES_PER_HOLEMASK_BIT) /
1912*4882a593Smuzhiyun mp->m_sb.sb_inopblock;
1913*4882a593Smuzhiyun
1914*4882a593Smuzhiyun ASSERT(agbno % mp->m_sb.sb_spino_align == 0);
1915*4882a593Smuzhiyun ASSERT(contigblk % mp->m_sb.sb_spino_align == 0);
1916*4882a593Smuzhiyun xfs_bmap_add_free(tp, XFS_AGB_TO_FSB(mp, agno, agbno),
1917*4882a593Smuzhiyun contigblk, &XFS_RMAP_OINFO_INODES);
1918*4882a593Smuzhiyun
1919*4882a593Smuzhiyun /* reset range to current bit and carry on... */
1920*4882a593Smuzhiyun startidx = endidx = nextbit;
1921*4882a593Smuzhiyun
1922*4882a593Smuzhiyun next:
1923*4882a593Smuzhiyun nextbit++;
1924*4882a593Smuzhiyun }
1925*4882a593Smuzhiyun }
1926*4882a593Smuzhiyun
1927*4882a593Smuzhiyun STATIC int
xfs_difree_inobt(struct xfs_mount * mp,struct xfs_trans * tp,struct xfs_buf * agbp,xfs_agino_t agino,struct xfs_icluster * xic,struct xfs_inobt_rec_incore * orec)1928*4882a593Smuzhiyun xfs_difree_inobt(
1929*4882a593Smuzhiyun struct xfs_mount *mp,
1930*4882a593Smuzhiyun struct xfs_trans *tp,
1931*4882a593Smuzhiyun struct xfs_buf *agbp,
1932*4882a593Smuzhiyun xfs_agino_t agino,
1933*4882a593Smuzhiyun struct xfs_icluster *xic,
1934*4882a593Smuzhiyun struct xfs_inobt_rec_incore *orec)
1935*4882a593Smuzhiyun {
1936*4882a593Smuzhiyun struct xfs_agi *agi = agbp->b_addr;
1937*4882a593Smuzhiyun xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
1938*4882a593Smuzhiyun struct xfs_btree_cur *cur;
1939*4882a593Smuzhiyun struct xfs_inobt_rec_incore rec;
1940*4882a593Smuzhiyun int ilen;
1941*4882a593Smuzhiyun int error;
1942*4882a593Smuzhiyun int i;
1943*4882a593Smuzhiyun int off;
1944*4882a593Smuzhiyun
1945*4882a593Smuzhiyun ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
1946*4882a593Smuzhiyun ASSERT(XFS_AGINO_TO_AGBNO(mp, agino) < be32_to_cpu(agi->agi_length));
1947*4882a593Smuzhiyun
1948*4882a593Smuzhiyun /*
1949*4882a593Smuzhiyun * Initialize the cursor.
1950*4882a593Smuzhiyun */
1951*4882a593Smuzhiyun cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
1952*4882a593Smuzhiyun
1953*4882a593Smuzhiyun error = xfs_check_agi_freecount(cur, agi);
1954*4882a593Smuzhiyun if (error)
1955*4882a593Smuzhiyun goto error0;
1956*4882a593Smuzhiyun
1957*4882a593Smuzhiyun /*
1958*4882a593Smuzhiyun * Look for the entry describing this inode.
1959*4882a593Smuzhiyun */
1960*4882a593Smuzhiyun if ((error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i))) {
1961*4882a593Smuzhiyun xfs_warn(mp, "%s: xfs_inobt_lookup() returned error %d.",
1962*4882a593Smuzhiyun __func__, error);
1963*4882a593Smuzhiyun goto error0;
1964*4882a593Smuzhiyun }
1965*4882a593Smuzhiyun if (XFS_IS_CORRUPT(mp, i != 1)) {
1966*4882a593Smuzhiyun error = -EFSCORRUPTED;
1967*4882a593Smuzhiyun goto error0;
1968*4882a593Smuzhiyun }
1969*4882a593Smuzhiyun error = xfs_inobt_get_rec(cur, &rec, &i);
1970*4882a593Smuzhiyun if (error) {
1971*4882a593Smuzhiyun xfs_warn(mp, "%s: xfs_inobt_get_rec() returned error %d.",
1972*4882a593Smuzhiyun __func__, error);
1973*4882a593Smuzhiyun goto error0;
1974*4882a593Smuzhiyun }
1975*4882a593Smuzhiyun if (XFS_IS_CORRUPT(mp, i != 1)) {
1976*4882a593Smuzhiyun error = -EFSCORRUPTED;
1977*4882a593Smuzhiyun goto error0;
1978*4882a593Smuzhiyun }
1979*4882a593Smuzhiyun /*
1980*4882a593Smuzhiyun * Get the offset in the inode chunk.
1981*4882a593Smuzhiyun */
1982*4882a593Smuzhiyun off = agino - rec.ir_startino;
1983*4882a593Smuzhiyun ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
1984*4882a593Smuzhiyun ASSERT(!(rec.ir_free & XFS_INOBT_MASK(off)));
1985*4882a593Smuzhiyun /*
1986*4882a593Smuzhiyun * Mark the inode free & increment the count.
1987*4882a593Smuzhiyun */
1988*4882a593Smuzhiyun rec.ir_free |= XFS_INOBT_MASK(off);
1989*4882a593Smuzhiyun rec.ir_freecount++;
1990*4882a593Smuzhiyun
1991*4882a593Smuzhiyun /*
1992*4882a593Smuzhiyun * When an inode chunk is free, it becomes eligible for removal. Don't
1993*4882a593Smuzhiyun * remove the chunk if the block size is large enough for multiple inode
1994*4882a593Smuzhiyun * chunks (that might not be free).
1995*4882a593Smuzhiyun */
1996*4882a593Smuzhiyun if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
1997*4882a593Smuzhiyun rec.ir_free == XFS_INOBT_ALL_FREE &&
1998*4882a593Smuzhiyun mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK) {
1999*4882a593Smuzhiyun struct xfs_perag *pag = agbp->b_pag;
2000*4882a593Smuzhiyun
2001*4882a593Smuzhiyun xic->deleted = true;
2002*4882a593Smuzhiyun xic->first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
2003*4882a593Smuzhiyun xic->alloc = xfs_inobt_irec_to_allocmask(&rec);
2004*4882a593Smuzhiyun
2005*4882a593Smuzhiyun /*
2006*4882a593Smuzhiyun * Remove the inode cluster from the AGI B+Tree, adjust the
2007*4882a593Smuzhiyun * AGI and Superblock inode counts, and mark the disk space
2008*4882a593Smuzhiyun * to be freed when the transaction is committed.
2009*4882a593Smuzhiyun */
2010*4882a593Smuzhiyun ilen = rec.ir_freecount;
2011*4882a593Smuzhiyun be32_add_cpu(&agi->agi_count, -ilen);
2012*4882a593Smuzhiyun be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
2013*4882a593Smuzhiyun xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
2014*4882a593Smuzhiyun pag->pagi_freecount -= ilen - 1;
2015*4882a593Smuzhiyun pag->pagi_count -= ilen;
2016*4882a593Smuzhiyun xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
2017*4882a593Smuzhiyun xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
2018*4882a593Smuzhiyun
2019*4882a593Smuzhiyun if ((error = xfs_btree_delete(cur, &i))) {
2020*4882a593Smuzhiyun xfs_warn(mp, "%s: xfs_btree_delete returned error %d.",
2021*4882a593Smuzhiyun __func__, error);
2022*4882a593Smuzhiyun goto error0;
2023*4882a593Smuzhiyun }
2024*4882a593Smuzhiyun
2025*4882a593Smuzhiyun xfs_difree_inode_chunk(tp, agno, &rec);
2026*4882a593Smuzhiyun } else {
2027*4882a593Smuzhiyun xic->deleted = false;
2028*4882a593Smuzhiyun
2029*4882a593Smuzhiyun error = xfs_inobt_update(cur, &rec);
2030*4882a593Smuzhiyun if (error) {
2031*4882a593Smuzhiyun xfs_warn(mp, "%s: xfs_inobt_update returned error %d.",
2032*4882a593Smuzhiyun __func__, error);
2033*4882a593Smuzhiyun goto error0;
2034*4882a593Smuzhiyun }
2035*4882a593Smuzhiyun
2036*4882a593Smuzhiyun /*
2037*4882a593Smuzhiyun * Change the inode free counts and log the ag/sb changes.
2038*4882a593Smuzhiyun */
2039*4882a593Smuzhiyun be32_add_cpu(&agi->agi_freecount, 1);
2040*4882a593Smuzhiyun xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
2041*4882a593Smuzhiyun agbp->b_pag->pagi_freecount++;
2042*4882a593Smuzhiyun xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
2043*4882a593Smuzhiyun }
2044*4882a593Smuzhiyun
2045*4882a593Smuzhiyun error = xfs_check_agi_freecount(cur, agi);
2046*4882a593Smuzhiyun if (error)
2047*4882a593Smuzhiyun goto error0;
2048*4882a593Smuzhiyun
2049*4882a593Smuzhiyun *orec = rec;
2050*4882a593Smuzhiyun xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
2051*4882a593Smuzhiyun return 0;
2052*4882a593Smuzhiyun
2053*4882a593Smuzhiyun error0:
2054*4882a593Smuzhiyun xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
2055*4882a593Smuzhiyun return error;
2056*4882a593Smuzhiyun }
2057*4882a593Smuzhiyun
2058*4882a593Smuzhiyun /*
2059*4882a593Smuzhiyun * Free an inode in the free inode btree.
2060*4882a593Smuzhiyun */
2061*4882a593Smuzhiyun STATIC int
xfs_difree_finobt(struct xfs_mount * mp,struct xfs_trans * tp,struct xfs_buf * agbp,xfs_agino_t agino,struct xfs_inobt_rec_incore * ibtrec)2062*4882a593Smuzhiyun xfs_difree_finobt(
2063*4882a593Smuzhiyun struct xfs_mount *mp,
2064*4882a593Smuzhiyun struct xfs_trans *tp,
2065*4882a593Smuzhiyun struct xfs_buf *agbp,
2066*4882a593Smuzhiyun xfs_agino_t agino,
2067*4882a593Smuzhiyun struct xfs_inobt_rec_incore *ibtrec) /* inobt record */
2068*4882a593Smuzhiyun {
2069*4882a593Smuzhiyun struct xfs_agi *agi = agbp->b_addr;
2070*4882a593Smuzhiyun xfs_agnumber_t agno = be32_to_cpu(agi->agi_seqno);
2071*4882a593Smuzhiyun struct xfs_btree_cur *cur;
2072*4882a593Smuzhiyun struct xfs_inobt_rec_incore rec;
2073*4882a593Smuzhiyun int offset = agino - ibtrec->ir_startino;
2074*4882a593Smuzhiyun int error;
2075*4882a593Smuzhiyun int i;
2076*4882a593Smuzhiyun
2077*4882a593Smuzhiyun cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_FINO);
2078*4882a593Smuzhiyun
2079*4882a593Smuzhiyun error = xfs_inobt_lookup(cur, ibtrec->ir_startino, XFS_LOOKUP_EQ, &i);
2080*4882a593Smuzhiyun if (error)
2081*4882a593Smuzhiyun goto error;
2082*4882a593Smuzhiyun if (i == 0) {
2083*4882a593Smuzhiyun /*
2084*4882a593Smuzhiyun * If the record does not exist in the finobt, we must have just
2085*4882a593Smuzhiyun * freed an inode in a previously fully allocated chunk. If not,
2086*4882a593Smuzhiyun * something is out of sync.
2087*4882a593Smuzhiyun */
2088*4882a593Smuzhiyun if (XFS_IS_CORRUPT(mp, ibtrec->ir_freecount != 1)) {
2089*4882a593Smuzhiyun error = -EFSCORRUPTED;
2090*4882a593Smuzhiyun goto error;
2091*4882a593Smuzhiyun }
2092*4882a593Smuzhiyun
2093*4882a593Smuzhiyun error = xfs_inobt_insert_rec(cur, ibtrec->ir_holemask,
2094*4882a593Smuzhiyun ibtrec->ir_count,
2095*4882a593Smuzhiyun ibtrec->ir_freecount,
2096*4882a593Smuzhiyun ibtrec->ir_free, &i);
2097*4882a593Smuzhiyun if (error)
2098*4882a593Smuzhiyun goto error;
2099*4882a593Smuzhiyun ASSERT(i == 1);
2100*4882a593Smuzhiyun
2101*4882a593Smuzhiyun goto out;
2102*4882a593Smuzhiyun }
2103*4882a593Smuzhiyun
2104*4882a593Smuzhiyun /*
2105*4882a593Smuzhiyun * Read and update the existing record. We could just copy the ibtrec
2106*4882a593Smuzhiyun * across here, but that would defeat the purpose of having redundant
2107*4882a593Smuzhiyun * metadata. By making the modifications independently, we can catch
2108*4882a593Smuzhiyun * corruptions that we wouldn't see if we just copied from one record
2109*4882a593Smuzhiyun * to another.
2110*4882a593Smuzhiyun */
2111*4882a593Smuzhiyun error = xfs_inobt_get_rec(cur, &rec, &i);
2112*4882a593Smuzhiyun if (error)
2113*4882a593Smuzhiyun goto error;
2114*4882a593Smuzhiyun if (XFS_IS_CORRUPT(mp, i != 1)) {
2115*4882a593Smuzhiyun error = -EFSCORRUPTED;
2116*4882a593Smuzhiyun goto error;
2117*4882a593Smuzhiyun }
2118*4882a593Smuzhiyun
2119*4882a593Smuzhiyun rec.ir_free |= XFS_INOBT_MASK(offset);
2120*4882a593Smuzhiyun rec.ir_freecount++;
2121*4882a593Smuzhiyun
2122*4882a593Smuzhiyun if (XFS_IS_CORRUPT(mp,
2123*4882a593Smuzhiyun rec.ir_free != ibtrec->ir_free ||
2124*4882a593Smuzhiyun rec.ir_freecount != ibtrec->ir_freecount)) {
2125*4882a593Smuzhiyun error = -EFSCORRUPTED;
2126*4882a593Smuzhiyun goto error;
2127*4882a593Smuzhiyun }
2128*4882a593Smuzhiyun
2129*4882a593Smuzhiyun /*
2130*4882a593Smuzhiyun * The content of inobt records should always match between the inobt
2131*4882a593Smuzhiyun * and finobt. The lifecycle of records in the finobt is different from
2132*4882a593Smuzhiyun * the inobt in that the finobt only tracks records with at least one
2133*4882a593Smuzhiyun * free inode. Hence, if all of the inodes are free and we aren't
2134*4882a593Smuzhiyun * keeping inode chunks permanently on disk, remove the record.
2135*4882a593Smuzhiyun * Otherwise, update the record with the new information.
2136*4882a593Smuzhiyun *
2137*4882a593Smuzhiyun * Note that we currently can't free chunks when the block size is large
2138*4882a593Smuzhiyun * enough for multiple chunks. Leave the finobt record to remain in sync
2139*4882a593Smuzhiyun * with the inobt.
2140*4882a593Smuzhiyun */
2141*4882a593Smuzhiyun if (rec.ir_free == XFS_INOBT_ALL_FREE &&
2142*4882a593Smuzhiyun mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK &&
2143*4882a593Smuzhiyun !(mp->m_flags & XFS_MOUNT_IKEEP)) {
2144*4882a593Smuzhiyun error = xfs_btree_delete(cur, &i);
2145*4882a593Smuzhiyun if (error)
2146*4882a593Smuzhiyun goto error;
2147*4882a593Smuzhiyun ASSERT(i == 1);
2148*4882a593Smuzhiyun } else {
2149*4882a593Smuzhiyun error = xfs_inobt_update(cur, &rec);
2150*4882a593Smuzhiyun if (error)
2151*4882a593Smuzhiyun goto error;
2152*4882a593Smuzhiyun }
2153*4882a593Smuzhiyun
2154*4882a593Smuzhiyun out:
2155*4882a593Smuzhiyun error = xfs_check_agi_freecount(cur, agi);
2156*4882a593Smuzhiyun if (error)
2157*4882a593Smuzhiyun goto error;
2158*4882a593Smuzhiyun
2159*4882a593Smuzhiyun xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
2160*4882a593Smuzhiyun return 0;
2161*4882a593Smuzhiyun
2162*4882a593Smuzhiyun error:
2163*4882a593Smuzhiyun xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
2164*4882a593Smuzhiyun return error;
2165*4882a593Smuzhiyun }
2166*4882a593Smuzhiyun
2167*4882a593Smuzhiyun /*
2168*4882a593Smuzhiyun * Free disk inode. Carefully avoids touching the incore inode, all
2169*4882a593Smuzhiyun * manipulations incore are the caller's responsibility.
2170*4882a593Smuzhiyun * The on-disk inode is not changed by this operation, only the
2171*4882a593Smuzhiyun * btree (free inode mask) is changed.
2172*4882a593Smuzhiyun */
2173*4882a593Smuzhiyun int
xfs_difree(struct xfs_trans * tp,xfs_ino_t inode,struct xfs_icluster * xic)2174*4882a593Smuzhiyun xfs_difree(
2175*4882a593Smuzhiyun struct xfs_trans *tp, /* transaction pointer */
2176*4882a593Smuzhiyun xfs_ino_t inode, /* inode to be freed */
2177*4882a593Smuzhiyun struct xfs_icluster *xic) /* cluster info if deleted */
2178*4882a593Smuzhiyun {
2179*4882a593Smuzhiyun /* REFERENCED */
2180*4882a593Smuzhiyun xfs_agblock_t agbno; /* block number containing inode */
2181*4882a593Smuzhiyun struct xfs_buf *agbp; /* buffer for allocation group header */
2182*4882a593Smuzhiyun xfs_agino_t agino; /* allocation group inode number */
2183*4882a593Smuzhiyun xfs_agnumber_t agno; /* allocation group number */
2184*4882a593Smuzhiyun int error; /* error return value */
2185*4882a593Smuzhiyun struct xfs_mount *mp; /* mount structure for filesystem */
2186*4882a593Smuzhiyun struct xfs_inobt_rec_incore rec;/* btree record */
2187*4882a593Smuzhiyun
2188*4882a593Smuzhiyun mp = tp->t_mountp;
2189*4882a593Smuzhiyun
2190*4882a593Smuzhiyun /*
2191*4882a593Smuzhiyun * Break up inode number into its components.
2192*4882a593Smuzhiyun */
2193*4882a593Smuzhiyun agno = XFS_INO_TO_AGNO(mp, inode);
2194*4882a593Smuzhiyun if (agno >= mp->m_sb.sb_agcount) {
2195*4882a593Smuzhiyun xfs_warn(mp, "%s: agno >= mp->m_sb.sb_agcount (%d >= %d).",
2196*4882a593Smuzhiyun __func__, agno, mp->m_sb.sb_agcount);
2197*4882a593Smuzhiyun ASSERT(0);
2198*4882a593Smuzhiyun return -EINVAL;
2199*4882a593Smuzhiyun }
2200*4882a593Smuzhiyun agino = XFS_INO_TO_AGINO(mp, inode);
2201*4882a593Smuzhiyun if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
2202*4882a593Smuzhiyun xfs_warn(mp, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).",
2203*4882a593Smuzhiyun __func__, (unsigned long long)inode,
2204*4882a593Smuzhiyun (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino));
2205*4882a593Smuzhiyun ASSERT(0);
2206*4882a593Smuzhiyun return -EINVAL;
2207*4882a593Smuzhiyun }
2208*4882a593Smuzhiyun agbno = XFS_AGINO_TO_AGBNO(mp, agino);
2209*4882a593Smuzhiyun if (agbno >= mp->m_sb.sb_agblocks) {
2210*4882a593Smuzhiyun xfs_warn(mp, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).",
2211*4882a593Smuzhiyun __func__, agbno, mp->m_sb.sb_agblocks);
2212*4882a593Smuzhiyun ASSERT(0);
2213*4882a593Smuzhiyun return -EINVAL;
2214*4882a593Smuzhiyun }
2215*4882a593Smuzhiyun /*
2216*4882a593Smuzhiyun * Get the allocation group header.
2217*4882a593Smuzhiyun */
2218*4882a593Smuzhiyun error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
2219*4882a593Smuzhiyun if (error) {
2220*4882a593Smuzhiyun xfs_warn(mp, "%s: xfs_ialloc_read_agi() returned error %d.",
2221*4882a593Smuzhiyun __func__, error);
2222*4882a593Smuzhiyun return error;
2223*4882a593Smuzhiyun }
2224*4882a593Smuzhiyun
2225*4882a593Smuzhiyun /*
2226*4882a593Smuzhiyun * Fix up the inode allocation btree.
2227*4882a593Smuzhiyun */
2228*4882a593Smuzhiyun error = xfs_difree_inobt(mp, tp, agbp, agino, xic, &rec);
2229*4882a593Smuzhiyun if (error)
2230*4882a593Smuzhiyun goto error0;
2231*4882a593Smuzhiyun
2232*4882a593Smuzhiyun /*
2233*4882a593Smuzhiyun * Fix up the free inode btree.
2234*4882a593Smuzhiyun */
2235*4882a593Smuzhiyun if (xfs_sb_version_hasfinobt(&mp->m_sb)) {
2236*4882a593Smuzhiyun error = xfs_difree_finobt(mp, tp, agbp, agino, &rec);
2237*4882a593Smuzhiyun if (error)
2238*4882a593Smuzhiyun goto error0;
2239*4882a593Smuzhiyun }
2240*4882a593Smuzhiyun
2241*4882a593Smuzhiyun return 0;
2242*4882a593Smuzhiyun
2243*4882a593Smuzhiyun error0:
2244*4882a593Smuzhiyun return error;
2245*4882a593Smuzhiyun }
2246*4882a593Smuzhiyun
2247*4882a593Smuzhiyun STATIC int
xfs_imap_lookup(struct xfs_mount * mp,struct xfs_trans * tp,xfs_agnumber_t agno,xfs_agino_t agino,xfs_agblock_t agbno,xfs_agblock_t * chunk_agbno,xfs_agblock_t * offset_agbno,int flags)2248*4882a593Smuzhiyun xfs_imap_lookup(
2249*4882a593Smuzhiyun struct xfs_mount *mp,
2250*4882a593Smuzhiyun struct xfs_trans *tp,
2251*4882a593Smuzhiyun xfs_agnumber_t agno,
2252*4882a593Smuzhiyun xfs_agino_t agino,
2253*4882a593Smuzhiyun xfs_agblock_t agbno,
2254*4882a593Smuzhiyun xfs_agblock_t *chunk_agbno,
2255*4882a593Smuzhiyun xfs_agblock_t *offset_agbno,
2256*4882a593Smuzhiyun int flags)
2257*4882a593Smuzhiyun {
2258*4882a593Smuzhiyun struct xfs_inobt_rec_incore rec;
2259*4882a593Smuzhiyun struct xfs_btree_cur *cur;
2260*4882a593Smuzhiyun struct xfs_buf *agbp;
2261*4882a593Smuzhiyun int error;
2262*4882a593Smuzhiyun int i;
2263*4882a593Smuzhiyun
2264*4882a593Smuzhiyun error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
2265*4882a593Smuzhiyun if (error) {
2266*4882a593Smuzhiyun xfs_alert(mp,
2267*4882a593Smuzhiyun "%s: xfs_ialloc_read_agi() returned error %d, agno %d",
2268*4882a593Smuzhiyun __func__, error, agno);
2269*4882a593Smuzhiyun return error;
2270*4882a593Smuzhiyun }
2271*4882a593Smuzhiyun
2272*4882a593Smuzhiyun /*
2273*4882a593Smuzhiyun * Lookup the inode record for the given agino. If the record cannot be
2274*4882a593Smuzhiyun * found, then it's an invalid inode number and we should abort. Once
2275*4882a593Smuzhiyun * we have a record, we need to ensure it contains the inode number
2276*4882a593Smuzhiyun * we are looking up.
2277*4882a593Smuzhiyun */
2278*4882a593Smuzhiyun cur = xfs_inobt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO);
2279*4882a593Smuzhiyun error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &i);
2280*4882a593Smuzhiyun if (!error) {
2281*4882a593Smuzhiyun if (i)
2282*4882a593Smuzhiyun error = xfs_inobt_get_rec(cur, &rec, &i);
2283*4882a593Smuzhiyun if (!error && i == 0)
2284*4882a593Smuzhiyun error = -EINVAL;
2285*4882a593Smuzhiyun }
2286*4882a593Smuzhiyun
2287*4882a593Smuzhiyun xfs_trans_brelse(tp, agbp);
2288*4882a593Smuzhiyun xfs_btree_del_cursor(cur, error);
2289*4882a593Smuzhiyun if (error)
2290*4882a593Smuzhiyun return error;
2291*4882a593Smuzhiyun
2292*4882a593Smuzhiyun /* check that the returned record contains the required inode */
2293*4882a593Smuzhiyun if (rec.ir_startino > agino ||
2294*4882a593Smuzhiyun rec.ir_startino + M_IGEO(mp)->ialloc_inos <= agino)
2295*4882a593Smuzhiyun return -EINVAL;
2296*4882a593Smuzhiyun
2297*4882a593Smuzhiyun /* for untrusted inodes check it is allocated first */
2298*4882a593Smuzhiyun if ((flags & XFS_IGET_UNTRUSTED) &&
2299*4882a593Smuzhiyun (rec.ir_free & XFS_INOBT_MASK(agino - rec.ir_startino)))
2300*4882a593Smuzhiyun return -EINVAL;
2301*4882a593Smuzhiyun
2302*4882a593Smuzhiyun *chunk_agbno = XFS_AGINO_TO_AGBNO(mp, rec.ir_startino);
2303*4882a593Smuzhiyun *offset_agbno = agbno - *chunk_agbno;
2304*4882a593Smuzhiyun return 0;
2305*4882a593Smuzhiyun }
2306*4882a593Smuzhiyun
2307*4882a593Smuzhiyun /*
2308*4882a593Smuzhiyun * Return the location of the inode in imap, for mapping it into a buffer.
2309*4882a593Smuzhiyun */
2310*4882a593Smuzhiyun int
xfs_imap(xfs_mount_t * mp,xfs_trans_t * tp,xfs_ino_t ino,struct xfs_imap * imap,uint flags)2311*4882a593Smuzhiyun xfs_imap(
2312*4882a593Smuzhiyun xfs_mount_t *mp, /* file system mount structure */
2313*4882a593Smuzhiyun xfs_trans_t *tp, /* transaction pointer */
2314*4882a593Smuzhiyun xfs_ino_t ino, /* inode to locate */
2315*4882a593Smuzhiyun struct xfs_imap *imap, /* location map structure */
2316*4882a593Smuzhiyun uint flags) /* flags for inode btree lookup */
2317*4882a593Smuzhiyun {
2318*4882a593Smuzhiyun xfs_agblock_t agbno; /* block number of inode in the alloc group */
2319*4882a593Smuzhiyun xfs_agino_t agino; /* inode number within alloc group */
2320*4882a593Smuzhiyun xfs_agnumber_t agno; /* allocation group number */
2321*4882a593Smuzhiyun xfs_agblock_t chunk_agbno; /* first block in inode chunk */
2322*4882a593Smuzhiyun xfs_agblock_t cluster_agbno; /* first block in inode cluster */
2323*4882a593Smuzhiyun int error; /* error code */
2324*4882a593Smuzhiyun int offset; /* index of inode in its buffer */
2325*4882a593Smuzhiyun xfs_agblock_t offset_agbno; /* blks from chunk start to inode */
2326*4882a593Smuzhiyun
2327*4882a593Smuzhiyun ASSERT(ino != NULLFSINO);
2328*4882a593Smuzhiyun
2329*4882a593Smuzhiyun /*
2330*4882a593Smuzhiyun * Split up the inode number into its parts.
2331*4882a593Smuzhiyun */
2332*4882a593Smuzhiyun agno = XFS_INO_TO_AGNO(mp, ino);
2333*4882a593Smuzhiyun agino = XFS_INO_TO_AGINO(mp, ino);
2334*4882a593Smuzhiyun agbno = XFS_AGINO_TO_AGBNO(mp, agino);
2335*4882a593Smuzhiyun if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
2336*4882a593Smuzhiyun ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
2337*4882a593Smuzhiyun #ifdef DEBUG
2338*4882a593Smuzhiyun /*
2339*4882a593Smuzhiyun * Don't output diagnostic information for untrusted inodes
2340*4882a593Smuzhiyun * as they can be invalid without implying corruption.
2341*4882a593Smuzhiyun */
2342*4882a593Smuzhiyun if (flags & XFS_IGET_UNTRUSTED)
2343*4882a593Smuzhiyun return -EINVAL;
2344*4882a593Smuzhiyun if (agno >= mp->m_sb.sb_agcount) {
2345*4882a593Smuzhiyun xfs_alert(mp,
2346*4882a593Smuzhiyun "%s: agno (%d) >= mp->m_sb.sb_agcount (%d)",
2347*4882a593Smuzhiyun __func__, agno, mp->m_sb.sb_agcount);
2348*4882a593Smuzhiyun }
2349*4882a593Smuzhiyun if (agbno >= mp->m_sb.sb_agblocks) {
2350*4882a593Smuzhiyun xfs_alert(mp,
2351*4882a593Smuzhiyun "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)",
2352*4882a593Smuzhiyun __func__, (unsigned long long)agbno,
2353*4882a593Smuzhiyun (unsigned long)mp->m_sb.sb_agblocks);
2354*4882a593Smuzhiyun }
2355*4882a593Smuzhiyun if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
2356*4882a593Smuzhiyun xfs_alert(mp,
2357*4882a593Smuzhiyun "%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)",
2358*4882a593Smuzhiyun __func__, ino,
2359*4882a593Smuzhiyun XFS_AGINO_TO_INO(mp, agno, agino));
2360*4882a593Smuzhiyun }
2361*4882a593Smuzhiyun xfs_stack_trace();
2362*4882a593Smuzhiyun #endif /* DEBUG */
2363*4882a593Smuzhiyun return -EINVAL;
2364*4882a593Smuzhiyun }
2365*4882a593Smuzhiyun
2366*4882a593Smuzhiyun /*
2367*4882a593Smuzhiyun * For bulkstat and handle lookups, we have an untrusted inode number
2368*4882a593Smuzhiyun * that we have to verify is valid. We cannot do this just by reading
2369*4882a593Smuzhiyun * the inode buffer as it may have been unlinked and removed leaving
2370*4882a593Smuzhiyun * inodes in stale state on disk. Hence we have to do a btree lookup
2371*4882a593Smuzhiyun * in all cases where an untrusted inode number is passed.
2372*4882a593Smuzhiyun */
2373*4882a593Smuzhiyun if (flags & XFS_IGET_UNTRUSTED) {
2374*4882a593Smuzhiyun error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
2375*4882a593Smuzhiyun &chunk_agbno, &offset_agbno, flags);
2376*4882a593Smuzhiyun if (error)
2377*4882a593Smuzhiyun return error;
2378*4882a593Smuzhiyun goto out_map;
2379*4882a593Smuzhiyun }
2380*4882a593Smuzhiyun
2381*4882a593Smuzhiyun /*
2382*4882a593Smuzhiyun * If the inode cluster size is the same as the blocksize or
2383*4882a593Smuzhiyun * smaller we get to the buffer by simple arithmetics.
2384*4882a593Smuzhiyun */
2385*4882a593Smuzhiyun if (M_IGEO(mp)->blocks_per_cluster == 1) {
2386*4882a593Smuzhiyun offset = XFS_INO_TO_OFFSET(mp, ino);
2387*4882a593Smuzhiyun ASSERT(offset < mp->m_sb.sb_inopblock);
2388*4882a593Smuzhiyun
2389*4882a593Smuzhiyun imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno);
2390*4882a593Smuzhiyun imap->im_len = XFS_FSB_TO_BB(mp, 1);
2391*4882a593Smuzhiyun imap->im_boffset = (unsigned short)(offset <<
2392*4882a593Smuzhiyun mp->m_sb.sb_inodelog);
2393*4882a593Smuzhiyun return 0;
2394*4882a593Smuzhiyun }
2395*4882a593Smuzhiyun
2396*4882a593Smuzhiyun /*
2397*4882a593Smuzhiyun * If the inode chunks are aligned then use simple maths to
2398*4882a593Smuzhiyun * find the location. Otherwise we have to do a btree
2399*4882a593Smuzhiyun * lookup to find the location.
2400*4882a593Smuzhiyun */
2401*4882a593Smuzhiyun if (M_IGEO(mp)->inoalign_mask) {
2402*4882a593Smuzhiyun offset_agbno = agbno & M_IGEO(mp)->inoalign_mask;
2403*4882a593Smuzhiyun chunk_agbno = agbno - offset_agbno;
2404*4882a593Smuzhiyun } else {
2405*4882a593Smuzhiyun error = xfs_imap_lookup(mp, tp, agno, agino, agbno,
2406*4882a593Smuzhiyun &chunk_agbno, &offset_agbno, flags);
2407*4882a593Smuzhiyun if (error)
2408*4882a593Smuzhiyun return error;
2409*4882a593Smuzhiyun }
2410*4882a593Smuzhiyun
2411*4882a593Smuzhiyun out_map:
2412*4882a593Smuzhiyun ASSERT(agbno >= chunk_agbno);
2413*4882a593Smuzhiyun cluster_agbno = chunk_agbno +
2414*4882a593Smuzhiyun ((offset_agbno / M_IGEO(mp)->blocks_per_cluster) *
2415*4882a593Smuzhiyun M_IGEO(mp)->blocks_per_cluster);
2416*4882a593Smuzhiyun offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
2417*4882a593Smuzhiyun XFS_INO_TO_OFFSET(mp, ino);
2418*4882a593Smuzhiyun
2419*4882a593Smuzhiyun imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno);
2420*4882a593Smuzhiyun imap->im_len = XFS_FSB_TO_BB(mp, M_IGEO(mp)->blocks_per_cluster);
2421*4882a593Smuzhiyun imap->im_boffset = (unsigned short)(offset << mp->m_sb.sb_inodelog);
2422*4882a593Smuzhiyun
2423*4882a593Smuzhiyun /*
2424*4882a593Smuzhiyun * If the inode number maps to a block outside the bounds
2425*4882a593Smuzhiyun * of the file system then return NULL rather than calling
2426*4882a593Smuzhiyun * read_buf and panicing when we get an error from the
2427*4882a593Smuzhiyun * driver.
2428*4882a593Smuzhiyun */
2429*4882a593Smuzhiyun if ((imap->im_blkno + imap->im_len) >
2430*4882a593Smuzhiyun XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
2431*4882a593Smuzhiyun xfs_alert(mp,
2432*4882a593Smuzhiyun "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)",
2433*4882a593Smuzhiyun __func__, (unsigned long long) imap->im_blkno,
2434*4882a593Smuzhiyun (unsigned long long) imap->im_len,
2435*4882a593Smuzhiyun XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
2436*4882a593Smuzhiyun return -EINVAL;
2437*4882a593Smuzhiyun }
2438*4882a593Smuzhiyun return 0;
2439*4882a593Smuzhiyun }
2440*4882a593Smuzhiyun
2441*4882a593Smuzhiyun /*
2442*4882a593Smuzhiyun * Log specified fields for the ag hdr (inode section). The growth of the agi
2443*4882a593Smuzhiyun * structure over time requires that we interpret the buffer as two logical
2444*4882a593Smuzhiyun * regions delineated by the end of the unlinked list. This is due to the size
2445*4882a593Smuzhiyun * of the hash table and its location in the middle of the agi.
2446*4882a593Smuzhiyun *
2447*4882a593Smuzhiyun * For example, a request to log a field before agi_unlinked and a field after
2448*4882a593Smuzhiyun * agi_unlinked could cause us to log the entire hash table and use an excessive
2449*4882a593Smuzhiyun * amount of log space. To avoid this behavior, log the region up through
2450*4882a593Smuzhiyun * agi_unlinked in one call and the region after agi_unlinked through the end of
2451*4882a593Smuzhiyun * the structure in another.
2452*4882a593Smuzhiyun */
2453*4882a593Smuzhiyun void
xfs_ialloc_log_agi(xfs_trans_t * tp,xfs_buf_t * bp,int fields)2454*4882a593Smuzhiyun xfs_ialloc_log_agi(
2455*4882a593Smuzhiyun xfs_trans_t *tp, /* transaction pointer */
2456*4882a593Smuzhiyun xfs_buf_t *bp, /* allocation group header buffer */
2457*4882a593Smuzhiyun int fields) /* bitmask of fields to log */
2458*4882a593Smuzhiyun {
2459*4882a593Smuzhiyun int first; /* first byte number */
2460*4882a593Smuzhiyun int last; /* last byte number */
2461*4882a593Smuzhiyun static const short offsets[] = { /* field starting offsets */
2462*4882a593Smuzhiyun /* keep in sync with bit definitions */
2463*4882a593Smuzhiyun offsetof(xfs_agi_t, agi_magicnum),
2464*4882a593Smuzhiyun offsetof(xfs_agi_t, agi_versionnum),
2465*4882a593Smuzhiyun offsetof(xfs_agi_t, agi_seqno),
2466*4882a593Smuzhiyun offsetof(xfs_agi_t, agi_length),
2467*4882a593Smuzhiyun offsetof(xfs_agi_t, agi_count),
2468*4882a593Smuzhiyun offsetof(xfs_agi_t, agi_root),
2469*4882a593Smuzhiyun offsetof(xfs_agi_t, agi_level),
2470*4882a593Smuzhiyun offsetof(xfs_agi_t, agi_freecount),
2471*4882a593Smuzhiyun offsetof(xfs_agi_t, agi_newino),
2472*4882a593Smuzhiyun offsetof(xfs_agi_t, agi_dirino),
2473*4882a593Smuzhiyun offsetof(xfs_agi_t, agi_unlinked),
2474*4882a593Smuzhiyun offsetof(xfs_agi_t, agi_free_root),
2475*4882a593Smuzhiyun offsetof(xfs_agi_t, agi_free_level),
2476*4882a593Smuzhiyun offsetof(xfs_agi_t, agi_iblocks),
2477*4882a593Smuzhiyun sizeof(xfs_agi_t)
2478*4882a593Smuzhiyun };
2479*4882a593Smuzhiyun #ifdef DEBUG
2480*4882a593Smuzhiyun struct xfs_agi *agi = bp->b_addr;
2481*4882a593Smuzhiyun
2482*4882a593Smuzhiyun ASSERT(agi->agi_magicnum == cpu_to_be32(XFS_AGI_MAGIC));
2483*4882a593Smuzhiyun #endif
2484*4882a593Smuzhiyun
2485*4882a593Smuzhiyun /*
2486*4882a593Smuzhiyun * Compute byte offsets for the first and last fields in the first
2487*4882a593Smuzhiyun * region and log the agi buffer. This only logs up through
2488*4882a593Smuzhiyun * agi_unlinked.
2489*4882a593Smuzhiyun */
2490*4882a593Smuzhiyun if (fields & XFS_AGI_ALL_BITS_R1) {
2491*4882a593Smuzhiyun xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R1,
2492*4882a593Smuzhiyun &first, &last);
2493*4882a593Smuzhiyun xfs_trans_log_buf(tp, bp, first, last);
2494*4882a593Smuzhiyun }
2495*4882a593Smuzhiyun
2496*4882a593Smuzhiyun /*
2497*4882a593Smuzhiyun * Mask off the bits in the first region and calculate the first and
2498*4882a593Smuzhiyun * last field offsets for any bits in the second region.
2499*4882a593Smuzhiyun */
2500*4882a593Smuzhiyun fields &= ~XFS_AGI_ALL_BITS_R1;
2501*4882a593Smuzhiyun if (fields) {
2502*4882a593Smuzhiyun xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS_R2,
2503*4882a593Smuzhiyun &first, &last);
2504*4882a593Smuzhiyun xfs_trans_log_buf(tp, bp, first, last);
2505*4882a593Smuzhiyun }
2506*4882a593Smuzhiyun }
2507*4882a593Smuzhiyun
2508*4882a593Smuzhiyun static xfs_failaddr_t
xfs_agi_verify(struct xfs_buf * bp)2509*4882a593Smuzhiyun xfs_agi_verify(
2510*4882a593Smuzhiyun struct xfs_buf *bp)
2511*4882a593Smuzhiyun {
2512*4882a593Smuzhiyun struct xfs_mount *mp = bp->b_mount;
2513*4882a593Smuzhiyun struct xfs_agi *agi = bp->b_addr;
2514*4882a593Smuzhiyun int i;
2515*4882a593Smuzhiyun
2516*4882a593Smuzhiyun if (xfs_sb_version_hascrc(&mp->m_sb)) {
2517*4882a593Smuzhiyun if (!uuid_equal(&agi->agi_uuid, &mp->m_sb.sb_meta_uuid))
2518*4882a593Smuzhiyun return __this_address;
2519*4882a593Smuzhiyun if (!xfs_log_check_lsn(mp, be64_to_cpu(agi->agi_lsn)))
2520*4882a593Smuzhiyun return __this_address;
2521*4882a593Smuzhiyun }
2522*4882a593Smuzhiyun
2523*4882a593Smuzhiyun /*
2524*4882a593Smuzhiyun * Validate the magic number of the agi block.
2525*4882a593Smuzhiyun */
2526*4882a593Smuzhiyun if (!xfs_verify_magic(bp, agi->agi_magicnum))
2527*4882a593Smuzhiyun return __this_address;
2528*4882a593Smuzhiyun if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)))
2529*4882a593Smuzhiyun return __this_address;
2530*4882a593Smuzhiyun
2531*4882a593Smuzhiyun if (be32_to_cpu(agi->agi_level) < 1 ||
2532*4882a593Smuzhiyun be32_to_cpu(agi->agi_level) > XFS_BTREE_MAXLEVELS)
2533*4882a593Smuzhiyun return __this_address;
2534*4882a593Smuzhiyun
2535*4882a593Smuzhiyun if (xfs_sb_version_hasfinobt(&mp->m_sb) &&
2536*4882a593Smuzhiyun (be32_to_cpu(agi->agi_free_level) < 1 ||
2537*4882a593Smuzhiyun be32_to_cpu(agi->agi_free_level) > XFS_BTREE_MAXLEVELS))
2538*4882a593Smuzhiyun return __this_address;
2539*4882a593Smuzhiyun
2540*4882a593Smuzhiyun /*
2541*4882a593Smuzhiyun * during growfs operations, the perag is not fully initialised,
2542*4882a593Smuzhiyun * so we can't use it for any useful checking. growfs ensures we can't
2543*4882a593Smuzhiyun * use it by using uncached buffers that don't have the perag attached
2544*4882a593Smuzhiyun * so we can detect and avoid this problem.
2545*4882a593Smuzhiyun */
2546*4882a593Smuzhiyun if (bp->b_pag && be32_to_cpu(agi->agi_seqno) != bp->b_pag->pag_agno)
2547*4882a593Smuzhiyun return __this_address;
2548*4882a593Smuzhiyun
2549*4882a593Smuzhiyun for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) {
2550*4882a593Smuzhiyun if (agi->agi_unlinked[i] == cpu_to_be32(NULLAGINO))
2551*4882a593Smuzhiyun continue;
2552*4882a593Smuzhiyun if (!xfs_verify_ino(mp, be32_to_cpu(agi->agi_unlinked[i])))
2553*4882a593Smuzhiyun return __this_address;
2554*4882a593Smuzhiyun }
2555*4882a593Smuzhiyun
2556*4882a593Smuzhiyun return NULL;
2557*4882a593Smuzhiyun }
2558*4882a593Smuzhiyun
2559*4882a593Smuzhiyun static void
xfs_agi_read_verify(struct xfs_buf * bp)2560*4882a593Smuzhiyun xfs_agi_read_verify(
2561*4882a593Smuzhiyun struct xfs_buf *bp)
2562*4882a593Smuzhiyun {
2563*4882a593Smuzhiyun struct xfs_mount *mp = bp->b_mount;
2564*4882a593Smuzhiyun xfs_failaddr_t fa;
2565*4882a593Smuzhiyun
2566*4882a593Smuzhiyun if (xfs_sb_version_hascrc(&mp->m_sb) &&
2567*4882a593Smuzhiyun !xfs_buf_verify_cksum(bp, XFS_AGI_CRC_OFF))
2568*4882a593Smuzhiyun xfs_verifier_error(bp, -EFSBADCRC, __this_address);
2569*4882a593Smuzhiyun else {
2570*4882a593Smuzhiyun fa = xfs_agi_verify(bp);
2571*4882a593Smuzhiyun if (XFS_TEST_ERROR(fa, mp, XFS_ERRTAG_IALLOC_READ_AGI))
2572*4882a593Smuzhiyun xfs_verifier_error(bp, -EFSCORRUPTED, fa);
2573*4882a593Smuzhiyun }
2574*4882a593Smuzhiyun }
2575*4882a593Smuzhiyun
2576*4882a593Smuzhiyun static void
xfs_agi_write_verify(struct xfs_buf * bp)2577*4882a593Smuzhiyun xfs_agi_write_verify(
2578*4882a593Smuzhiyun struct xfs_buf *bp)
2579*4882a593Smuzhiyun {
2580*4882a593Smuzhiyun struct xfs_mount *mp = bp->b_mount;
2581*4882a593Smuzhiyun struct xfs_buf_log_item *bip = bp->b_log_item;
2582*4882a593Smuzhiyun struct xfs_agi *agi = bp->b_addr;
2583*4882a593Smuzhiyun xfs_failaddr_t fa;
2584*4882a593Smuzhiyun
2585*4882a593Smuzhiyun fa = xfs_agi_verify(bp);
2586*4882a593Smuzhiyun if (fa) {
2587*4882a593Smuzhiyun xfs_verifier_error(bp, -EFSCORRUPTED, fa);
2588*4882a593Smuzhiyun return;
2589*4882a593Smuzhiyun }
2590*4882a593Smuzhiyun
2591*4882a593Smuzhiyun if (!xfs_sb_version_hascrc(&mp->m_sb))
2592*4882a593Smuzhiyun return;
2593*4882a593Smuzhiyun
2594*4882a593Smuzhiyun if (bip)
2595*4882a593Smuzhiyun agi->agi_lsn = cpu_to_be64(bip->bli_item.li_lsn);
2596*4882a593Smuzhiyun xfs_buf_update_cksum(bp, XFS_AGI_CRC_OFF);
2597*4882a593Smuzhiyun }
2598*4882a593Smuzhiyun
2599*4882a593Smuzhiyun const struct xfs_buf_ops xfs_agi_buf_ops = {
2600*4882a593Smuzhiyun .name = "xfs_agi",
2601*4882a593Smuzhiyun .magic = { cpu_to_be32(XFS_AGI_MAGIC), cpu_to_be32(XFS_AGI_MAGIC) },
2602*4882a593Smuzhiyun .verify_read = xfs_agi_read_verify,
2603*4882a593Smuzhiyun .verify_write = xfs_agi_write_verify,
2604*4882a593Smuzhiyun .verify_struct = xfs_agi_verify,
2605*4882a593Smuzhiyun };
2606*4882a593Smuzhiyun
2607*4882a593Smuzhiyun /*
2608*4882a593Smuzhiyun * Read in the allocation group header (inode allocation section)
2609*4882a593Smuzhiyun */
2610*4882a593Smuzhiyun int
xfs_read_agi(struct xfs_mount * mp,struct xfs_trans * tp,xfs_agnumber_t agno,struct xfs_buf ** bpp)2611*4882a593Smuzhiyun xfs_read_agi(
2612*4882a593Smuzhiyun struct xfs_mount *mp, /* file system mount structure */
2613*4882a593Smuzhiyun struct xfs_trans *tp, /* transaction pointer */
2614*4882a593Smuzhiyun xfs_agnumber_t agno, /* allocation group number */
2615*4882a593Smuzhiyun struct xfs_buf **bpp) /* allocation group hdr buf */
2616*4882a593Smuzhiyun {
2617*4882a593Smuzhiyun int error;
2618*4882a593Smuzhiyun
2619*4882a593Smuzhiyun trace_xfs_read_agi(mp, agno);
2620*4882a593Smuzhiyun
2621*4882a593Smuzhiyun ASSERT(agno != NULLAGNUMBER);
2622*4882a593Smuzhiyun error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
2623*4882a593Smuzhiyun XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
2624*4882a593Smuzhiyun XFS_FSS_TO_BB(mp, 1), 0, bpp, &xfs_agi_buf_ops);
2625*4882a593Smuzhiyun if (error)
2626*4882a593Smuzhiyun return error;
2627*4882a593Smuzhiyun if (tp)
2628*4882a593Smuzhiyun xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_AGI_BUF);
2629*4882a593Smuzhiyun
2630*4882a593Smuzhiyun xfs_buf_set_ref(*bpp, XFS_AGI_REF);
2631*4882a593Smuzhiyun return 0;
2632*4882a593Smuzhiyun }
2633*4882a593Smuzhiyun
2634*4882a593Smuzhiyun int
xfs_ialloc_read_agi(struct xfs_mount * mp,struct xfs_trans * tp,xfs_agnumber_t agno,struct xfs_buf ** bpp)2635*4882a593Smuzhiyun xfs_ialloc_read_agi(
2636*4882a593Smuzhiyun struct xfs_mount *mp, /* file system mount structure */
2637*4882a593Smuzhiyun struct xfs_trans *tp, /* transaction pointer */
2638*4882a593Smuzhiyun xfs_agnumber_t agno, /* allocation group number */
2639*4882a593Smuzhiyun struct xfs_buf **bpp) /* allocation group hdr buf */
2640*4882a593Smuzhiyun {
2641*4882a593Smuzhiyun struct xfs_agi *agi; /* allocation group header */
2642*4882a593Smuzhiyun struct xfs_perag *pag; /* per allocation group data */
2643*4882a593Smuzhiyun int error;
2644*4882a593Smuzhiyun
2645*4882a593Smuzhiyun trace_xfs_ialloc_read_agi(mp, agno);
2646*4882a593Smuzhiyun
2647*4882a593Smuzhiyun error = xfs_read_agi(mp, tp, agno, bpp);
2648*4882a593Smuzhiyun if (error)
2649*4882a593Smuzhiyun return error;
2650*4882a593Smuzhiyun
2651*4882a593Smuzhiyun agi = (*bpp)->b_addr;
2652*4882a593Smuzhiyun pag = (*bpp)->b_pag;
2653*4882a593Smuzhiyun if (!pag->pagi_init) {
2654*4882a593Smuzhiyun pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
2655*4882a593Smuzhiyun pag->pagi_count = be32_to_cpu(agi->agi_count);
2656*4882a593Smuzhiyun pag->pagi_init = 1;
2657*4882a593Smuzhiyun }
2658*4882a593Smuzhiyun
2659*4882a593Smuzhiyun /*
2660*4882a593Smuzhiyun * It's possible for these to be out of sync if
2661*4882a593Smuzhiyun * we are in the middle of a forced shutdown.
2662*4882a593Smuzhiyun */
2663*4882a593Smuzhiyun ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
2664*4882a593Smuzhiyun XFS_FORCED_SHUTDOWN(mp));
2665*4882a593Smuzhiyun return 0;
2666*4882a593Smuzhiyun }
2667*4882a593Smuzhiyun
2668*4882a593Smuzhiyun /*
2669*4882a593Smuzhiyun * Read in the agi to initialise the per-ag data in the mount structure
2670*4882a593Smuzhiyun */
2671*4882a593Smuzhiyun int
xfs_ialloc_pagi_init(xfs_mount_t * mp,xfs_trans_t * tp,xfs_agnumber_t agno)2672*4882a593Smuzhiyun xfs_ialloc_pagi_init(
2673*4882a593Smuzhiyun xfs_mount_t *mp, /* file system mount structure */
2674*4882a593Smuzhiyun xfs_trans_t *tp, /* transaction pointer */
2675*4882a593Smuzhiyun xfs_agnumber_t agno) /* allocation group number */
2676*4882a593Smuzhiyun {
2677*4882a593Smuzhiyun xfs_buf_t *bp = NULL;
2678*4882a593Smuzhiyun int error;
2679*4882a593Smuzhiyun
2680*4882a593Smuzhiyun error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
2681*4882a593Smuzhiyun if (error)
2682*4882a593Smuzhiyun return error;
2683*4882a593Smuzhiyun if (bp)
2684*4882a593Smuzhiyun xfs_trans_brelse(tp, bp);
2685*4882a593Smuzhiyun return 0;
2686*4882a593Smuzhiyun }
2687*4882a593Smuzhiyun
2688*4882a593Smuzhiyun /* Is there an inode record covering a given range of inode numbers? */
2689*4882a593Smuzhiyun int
xfs_ialloc_has_inode_record(struct xfs_btree_cur * cur,xfs_agino_t low,xfs_agino_t high,bool * exists)2690*4882a593Smuzhiyun xfs_ialloc_has_inode_record(
2691*4882a593Smuzhiyun struct xfs_btree_cur *cur,
2692*4882a593Smuzhiyun xfs_agino_t low,
2693*4882a593Smuzhiyun xfs_agino_t high,
2694*4882a593Smuzhiyun bool *exists)
2695*4882a593Smuzhiyun {
2696*4882a593Smuzhiyun struct xfs_inobt_rec_incore irec;
2697*4882a593Smuzhiyun xfs_agino_t agino;
2698*4882a593Smuzhiyun uint16_t holemask;
2699*4882a593Smuzhiyun int has_record;
2700*4882a593Smuzhiyun int i;
2701*4882a593Smuzhiyun int error;
2702*4882a593Smuzhiyun
2703*4882a593Smuzhiyun *exists = false;
2704*4882a593Smuzhiyun error = xfs_inobt_lookup(cur, low, XFS_LOOKUP_LE, &has_record);
2705*4882a593Smuzhiyun while (error == 0 && has_record) {
2706*4882a593Smuzhiyun error = xfs_inobt_get_rec(cur, &irec, &has_record);
2707*4882a593Smuzhiyun if (error || irec.ir_startino > high)
2708*4882a593Smuzhiyun break;
2709*4882a593Smuzhiyun
2710*4882a593Smuzhiyun agino = irec.ir_startino;
2711*4882a593Smuzhiyun holemask = irec.ir_holemask;
2712*4882a593Smuzhiyun for (i = 0; i < XFS_INOBT_HOLEMASK_BITS; holemask >>= 1,
2713*4882a593Smuzhiyun i++, agino += XFS_INODES_PER_HOLEMASK_BIT) {
2714*4882a593Smuzhiyun if (holemask & 1)
2715*4882a593Smuzhiyun continue;
2716*4882a593Smuzhiyun if (agino + XFS_INODES_PER_HOLEMASK_BIT > low &&
2717*4882a593Smuzhiyun agino <= high) {
2718*4882a593Smuzhiyun *exists = true;
2719*4882a593Smuzhiyun return 0;
2720*4882a593Smuzhiyun }
2721*4882a593Smuzhiyun }
2722*4882a593Smuzhiyun
2723*4882a593Smuzhiyun error = xfs_btree_increment(cur, 0, &has_record);
2724*4882a593Smuzhiyun }
2725*4882a593Smuzhiyun return error;
2726*4882a593Smuzhiyun }
2727*4882a593Smuzhiyun
2728*4882a593Smuzhiyun /* Is there an inode record covering a given extent? */
2729*4882a593Smuzhiyun int
xfs_ialloc_has_inodes_at_extent(struct xfs_btree_cur * cur,xfs_agblock_t bno,xfs_extlen_t len,bool * exists)2730*4882a593Smuzhiyun xfs_ialloc_has_inodes_at_extent(
2731*4882a593Smuzhiyun struct xfs_btree_cur *cur,
2732*4882a593Smuzhiyun xfs_agblock_t bno,
2733*4882a593Smuzhiyun xfs_extlen_t len,
2734*4882a593Smuzhiyun bool *exists)
2735*4882a593Smuzhiyun {
2736*4882a593Smuzhiyun xfs_agino_t low;
2737*4882a593Smuzhiyun xfs_agino_t high;
2738*4882a593Smuzhiyun
2739*4882a593Smuzhiyun low = XFS_AGB_TO_AGINO(cur->bc_mp, bno);
2740*4882a593Smuzhiyun high = XFS_AGB_TO_AGINO(cur->bc_mp, bno + len) - 1;
2741*4882a593Smuzhiyun
2742*4882a593Smuzhiyun return xfs_ialloc_has_inode_record(cur, low, high, exists);
2743*4882a593Smuzhiyun }
2744*4882a593Smuzhiyun
2745*4882a593Smuzhiyun struct xfs_ialloc_count_inodes {
2746*4882a593Smuzhiyun xfs_agino_t count;
2747*4882a593Smuzhiyun xfs_agino_t freecount;
2748*4882a593Smuzhiyun };
2749*4882a593Smuzhiyun
2750*4882a593Smuzhiyun /* Record inode counts across all inobt records. */
2751*4882a593Smuzhiyun STATIC int
xfs_ialloc_count_inodes_rec(struct xfs_btree_cur * cur,union xfs_btree_rec * rec,void * priv)2752*4882a593Smuzhiyun xfs_ialloc_count_inodes_rec(
2753*4882a593Smuzhiyun struct xfs_btree_cur *cur,
2754*4882a593Smuzhiyun union xfs_btree_rec *rec,
2755*4882a593Smuzhiyun void *priv)
2756*4882a593Smuzhiyun {
2757*4882a593Smuzhiyun struct xfs_inobt_rec_incore irec;
2758*4882a593Smuzhiyun struct xfs_ialloc_count_inodes *ci = priv;
2759*4882a593Smuzhiyun
2760*4882a593Smuzhiyun xfs_inobt_btrec_to_irec(cur->bc_mp, rec, &irec);
2761*4882a593Smuzhiyun ci->count += irec.ir_count;
2762*4882a593Smuzhiyun ci->freecount += irec.ir_freecount;
2763*4882a593Smuzhiyun
2764*4882a593Smuzhiyun return 0;
2765*4882a593Smuzhiyun }
2766*4882a593Smuzhiyun
2767*4882a593Smuzhiyun /* Count allocated and free inodes under an inobt. */
2768*4882a593Smuzhiyun int
xfs_ialloc_count_inodes(struct xfs_btree_cur * cur,xfs_agino_t * count,xfs_agino_t * freecount)2769*4882a593Smuzhiyun xfs_ialloc_count_inodes(
2770*4882a593Smuzhiyun struct xfs_btree_cur *cur,
2771*4882a593Smuzhiyun xfs_agino_t *count,
2772*4882a593Smuzhiyun xfs_agino_t *freecount)
2773*4882a593Smuzhiyun {
2774*4882a593Smuzhiyun struct xfs_ialloc_count_inodes ci = {0};
2775*4882a593Smuzhiyun int error;
2776*4882a593Smuzhiyun
2777*4882a593Smuzhiyun ASSERT(cur->bc_btnum == XFS_BTNUM_INO);
2778*4882a593Smuzhiyun error = xfs_btree_query_all(cur, xfs_ialloc_count_inodes_rec, &ci);
2779*4882a593Smuzhiyun if (error)
2780*4882a593Smuzhiyun return error;
2781*4882a593Smuzhiyun
2782*4882a593Smuzhiyun *count = ci.count;
2783*4882a593Smuzhiyun *freecount = ci.freecount;
2784*4882a593Smuzhiyun return 0;
2785*4882a593Smuzhiyun }
2786*4882a593Smuzhiyun
2787*4882a593Smuzhiyun /*
2788*4882a593Smuzhiyun * Initialize inode-related geometry information.
2789*4882a593Smuzhiyun *
2790*4882a593Smuzhiyun * Compute the inode btree min and max levels and set maxicount.
2791*4882a593Smuzhiyun *
2792*4882a593Smuzhiyun * Set the inode cluster size. This may still be overridden by the file
2793*4882a593Smuzhiyun * system block size if it is larger than the chosen cluster size.
2794*4882a593Smuzhiyun *
2795*4882a593Smuzhiyun * For v5 filesystems, scale the cluster size with the inode size to keep a
2796*4882a593Smuzhiyun * constant ratio of inode per cluster buffer, but only if mkfs has set the
2797*4882a593Smuzhiyun * inode alignment value appropriately for larger cluster sizes.
2798*4882a593Smuzhiyun *
2799*4882a593Smuzhiyun * Then compute the inode cluster alignment information.
2800*4882a593Smuzhiyun */
2801*4882a593Smuzhiyun void
xfs_ialloc_setup_geometry(struct xfs_mount * mp)2802*4882a593Smuzhiyun xfs_ialloc_setup_geometry(
2803*4882a593Smuzhiyun struct xfs_mount *mp)
2804*4882a593Smuzhiyun {
2805*4882a593Smuzhiyun struct xfs_sb *sbp = &mp->m_sb;
2806*4882a593Smuzhiyun struct xfs_ino_geometry *igeo = M_IGEO(mp);
2807*4882a593Smuzhiyun uint64_t icount;
2808*4882a593Smuzhiyun uint inodes;
2809*4882a593Smuzhiyun
2810*4882a593Smuzhiyun igeo->new_diflags2 = 0;
2811*4882a593Smuzhiyun if (xfs_sb_version_hasbigtime(&mp->m_sb))
2812*4882a593Smuzhiyun igeo->new_diflags2 |= XFS_DIFLAG2_BIGTIME;
2813*4882a593Smuzhiyun
2814*4882a593Smuzhiyun /* Compute inode btree geometry. */
2815*4882a593Smuzhiyun igeo->agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
2816*4882a593Smuzhiyun igeo->inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1);
2817*4882a593Smuzhiyun igeo->inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0);
2818*4882a593Smuzhiyun igeo->inobt_mnr[0] = igeo->inobt_mxr[0] / 2;
2819*4882a593Smuzhiyun igeo->inobt_mnr[1] = igeo->inobt_mxr[1] / 2;
2820*4882a593Smuzhiyun
2821*4882a593Smuzhiyun igeo->ialloc_inos = max_t(uint16_t, XFS_INODES_PER_CHUNK,
2822*4882a593Smuzhiyun sbp->sb_inopblock);
2823*4882a593Smuzhiyun igeo->ialloc_blks = igeo->ialloc_inos >> sbp->sb_inopblog;
2824*4882a593Smuzhiyun
2825*4882a593Smuzhiyun if (sbp->sb_spino_align)
2826*4882a593Smuzhiyun igeo->ialloc_min_blks = sbp->sb_spino_align;
2827*4882a593Smuzhiyun else
2828*4882a593Smuzhiyun igeo->ialloc_min_blks = igeo->ialloc_blks;
2829*4882a593Smuzhiyun
2830*4882a593Smuzhiyun /* Compute and fill in value of m_ino_geo.inobt_maxlevels. */
2831*4882a593Smuzhiyun inodes = (1LL << XFS_INO_AGINO_BITS(mp)) >> XFS_INODES_PER_CHUNK_LOG;
2832*4882a593Smuzhiyun igeo->inobt_maxlevels = xfs_btree_compute_maxlevels(igeo->inobt_mnr,
2833*4882a593Smuzhiyun inodes);
2834*4882a593Smuzhiyun
2835*4882a593Smuzhiyun /*
2836*4882a593Smuzhiyun * Set the maximum inode count for this filesystem, being careful not
2837*4882a593Smuzhiyun * to use obviously garbage sb_inopblog/sb_inopblock values. Regular
2838*4882a593Smuzhiyun * users should never get here due to failing sb verification, but
2839*4882a593Smuzhiyun * certain users (xfs_db) need to be usable even with corrupt metadata.
2840*4882a593Smuzhiyun */
2841*4882a593Smuzhiyun if (sbp->sb_imax_pct && igeo->ialloc_blks) {
2842*4882a593Smuzhiyun /*
2843*4882a593Smuzhiyun * Make sure the maximum inode count is a multiple
2844*4882a593Smuzhiyun * of the units we allocate inodes in.
2845*4882a593Smuzhiyun */
2846*4882a593Smuzhiyun icount = sbp->sb_dblocks * sbp->sb_imax_pct;
2847*4882a593Smuzhiyun do_div(icount, 100);
2848*4882a593Smuzhiyun do_div(icount, igeo->ialloc_blks);
2849*4882a593Smuzhiyun igeo->maxicount = XFS_FSB_TO_INO(mp,
2850*4882a593Smuzhiyun icount * igeo->ialloc_blks);
2851*4882a593Smuzhiyun } else {
2852*4882a593Smuzhiyun igeo->maxicount = 0;
2853*4882a593Smuzhiyun }
2854*4882a593Smuzhiyun
2855*4882a593Smuzhiyun /*
2856*4882a593Smuzhiyun * Compute the desired size of an inode cluster buffer size, which
2857*4882a593Smuzhiyun * starts at 8K and (on v5 filesystems) scales up with larger inode
2858*4882a593Smuzhiyun * sizes.
2859*4882a593Smuzhiyun *
2860*4882a593Smuzhiyun * Preserve the desired inode cluster size because the sparse inodes
2861*4882a593Smuzhiyun * feature uses that desired size (not the actual size) to compute the
2862*4882a593Smuzhiyun * sparse inode alignment. The mount code validates this value, so we
2863*4882a593Smuzhiyun * cannot change the behavior.
2864*4882a593Smuzhiyun */
2865*4882a593Smuzhiyun igeo->inode_cluster_size_raw = XFS_INODE_BIG_CLUSTER_SIZE;
2866*4882a593Smuzhiyun if (xfs_sb_version_has_v3inode(&mp->m_sb)) {
2867*4882a593Smuzhiyun int new_size = igeo->inode_cluster_size_raw;
2868*4882a593Smuzhiyun
2869*4882a593Smuzhiyun new_size *= mp->m_sb.sb_inodesize / XFS_DINODE_MIN_SIZE;
2870*4882a593Smuzhiyun if (mp->m_sb.sb_inoalignmt >= XFS_B_TO_FSBT(mp, new_size))
2871*4882a593Smuzhiyun igeo->inode_cluster_size_raw = new_size;
2872*4882a593Smuzhiyun }
2873*4882a593Smuzhiyun
2874*4882a593Smuzhiyun /* Calculate inode cluster ratios. */
2875*4882a593Smuzhiyun if (igeo->inode_cluster_size_raw > mp->m_sb.sb_blocksize)
2876*4882a593Smuzhiyun igeo->blocks_per_cluster = XFS_B_TO_FSBT(mp,
2877*4882a593Smuzhiyun igeo->inode_cluster_size_raw);
2878*4882a593Smuzhiyun else
2879*4882a593Smuzhiyun igeo->blocks_per_cluster = 1;
2880*4882a593Smuzhiyun igeo->inode_cluster_size = XFS_FSB_TO_B(mp, igeo->blocks_per_cluster);
2881*4882a593Smuzhiyun igeo->inodes_per_cluster = XFS_FSB_TO_INO(mp, igeo->blocks_per_cluster);
2882*4882a593Smuzhiyun
2883*4882a593Smuzhiyun /* Calculate inode cluster alignment. */
2884*4882a593Smuzhiyun if (xfs_sb_version_hasalign(&mp->m_sb) &&
2885*4882a593Smuzhiyun mp->m_sb.sb_inoalignmt >= igeo->blocks_per_cluster)
2886*4882a593Smuzhiyun igeo->cluster_align = mp->m_sb.sb_inoalignmt;
2887*4882a593Smuzhiyun else
2888*4882a593Smuzhiyun igeo->cluster_align = 1;
2889*4882a593Smuzhiyun igeo->inoalign_mask = igeo->cluster_align - 1;
2890*4882a593Smuzhiyun igeo->cluster_align_inodes = XFS_FSB_TO_INO(mp, igeo->cluster_align);
2891*4882a593Smuzhiyun
2892*4882a593Smuzhiyun /*
2893*4882a593Smuzhiyun * If we are using stripe alignment, check whether
2894*4882a593Smuzhiyun * the stripe unit is a multiple of the inode alignment
2895*4882a593Smuzhiyun */
2896*4882a593Smuzhiyun if (mp->m_dalign && igeo->inoalign_mask &&
2897*4882a593Smuzhiyun !(mp->m_dalign & igeo->inoalign_mask))
2898*4882a593Smuzhiyun igeo->ialloc_align = mp->m_dalign;
2899*4882a593Smuzhiyun else
2900*4882a593Smuzhiyun igeo->ialloc_align = 0;
2901*4882a593Smuzhiyun }
2902*4882a593Smuzhiyun
2903*4882a593Smuzhiyun /* Compute the location of the root directory inode that is laid out by mkfs. */
2904*4882a593Smuzhiyun xfs_ino_t
xfs_ialloc_calc_rootino(struct xfs_mount * mp,int sunit)2905*4882a593Smuzhiyun xfs_ialloc_calc_rootino(
2906*4882a593Smuzhiyun struct xfs_mount *mp,
2907*4882a593Smuzhiyun int sunit)
2908*4882a593Smuzhiyun {
2909*4882a593Smuzhiyun struct xfs_ino_geometry *igeo = M_IGEO(mp);
2910*4882a593Smuzhiyun xfs_agblock_t first_bno;
2911*4882a593Smuzhiyun
2912*4882a593Smuzhiyun /*
2913*4882a593Smuzhiyun * Pre-calculate the geometry of AG 0. We know what it looks like
2914*4882a593Smuzhiyun * because libxfs knows how to create allocation groups now.
2915*4882a593Smuzhiyun *
2916*4882a593Smuzhiyun * first_bno is the first block in which mkfs could possibly have
2917*4882a593Smuzhiyun * allocated the root directory inode, once we factor in the metadata
2918*4882a593Smuzhiyun * that mkfs formats before it. Namely, the four AG headers...
2919*4882a593Smuzhiyun */
2920*4882a593Smuzhiyun first_bno = howmany(4 * mp->m_sb.sb_sectsize, mp->m_sb.sb_blocksize);
2921*4882a593Smuzhiyun
2922*4882a593Smuzhiyun /* ...the two free space btree roots... */
2923*4882a593Smuzhiyun first_bno += 2;
2924*4882a593Smuzhiyun
2925*4882a593Smuzhiyun /* ...the inode btree root... */
2926*4882a593Smuzhiyun first_bno += 1;
2927*4882a593Smuzhiyun
2928*4882a593Smuzhiyun /* ...the initial AGFL... */
2929*4882a593Smuzhiyun first_bno += xfs_alloc_min_freelist(mp, NULL);
2930*4882a593Smuzhiyun
2931*4882a593Smuzhiyun /* ...the free inode btree root... */
2932*4882a593Smuzhiyun if (xfs_sb_version_hasfinobt(&mp->m_sb))
2933*4882a593Smuzhiyun first_bno++;
2934*4882a593Smuzhiyun
2935*4882a593Smuzhiyun /* ...the reverse mapping btree root... */
2936*4882a593Smuzhiyun if (xfs_sb_version_hasrmapbt(&mp->m_sb))
2937*4882a593Smuzhiyun first_bno++;
2938*4882a593Smuzhiyun
2939*4882a593Smuzhiyun /* ...the reference count btree... */
2940*4882a593Smuzhiyun if (xfs_sb_version_hasreflink(&mp->m_sb))
2941*4882a593Smuzhiyun first_bno++;
2942*4882a593Smuzhiyun
2943*4882a593Smuzhiyun /*
2944*4882a593Smuzhiyun * ...and the log, if it is allocated in the first allocation group.
2945*4882a593Smuzhiyun *
2946*4882a593Smuzhiyun * This can happen with filesystems that only have a single
2947*4882a593Smuzhiyun * allocation group, or very odd geometries created by old mkfs
2948*4882a593Smuzhiyun * versions on very small filesystems.
2949*4882a593Smuzhiyun */
2950*4882a593Smuzhiyun if (mp->m_sb.sb_logstart &&
2951*4882a593Smuzhiyun XFS_FSB_TO_AGNO(mp, mp->m_sb.sb_logstart) == 0)
2952*4882a593Smuzhiyun first_bno += mp->m_sb.sb_logblocks;
2953*4882a593Smuzhiyun
2954*4882a593Smuzhiyun /*
2955*4882a593Smuzhiyun * Now round first_bno up to whatever allocation alignment is given
2956*4882a593Smuzhiyun * by the filesystem or was passed in.
2957*4882a593Smuzhiyun */
2958*4882a593Smuzhiyun if (xfs_sb_version_hasdalign(&mp->m_sb) && igeo->ialloc_align > 0)
2959*4882a593Smuzhiyun first_bno = roundup(first_bno, sunit);
2960*4882a593Smuzhiyun else if (xfs_sb_version_hasalign(&mp->m_sb) &&
2961*4882a593Smuzhiyun mp->m_sb.sb_inoalignmt > 1)
2962*4882a593Smuzhiyun first_bno = roundup(first_bno, mp->m_sb.sb_inoalignmt);
2963*4882a593Smuzhiyun
2964*4882a593Smuzhiyun return XFS_AGINO_TO_INO(mp, 0, XFS_AGB_TO_AGINO(mp, first_bno));
2965*4882a593Smuzhiyun }
2966