2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_types.h"
24 #include "xfs_trans.h"
28 #include "xfs_dmapi.h"
29 #include "xfs_mount.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_alloc_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dir2_sf.h"
34 #include "xfs_attr_sf.h"
35 #include "xfs_dinode.h"
36 #include "xfs_inode.h"
37 #include "xfs_btree.h"
38 #include "xfs_ialloc.h"
39 #include "xfs_alloc.h"
40 #include "xfs_rtalloc.h"
41 #include "xfs_error.h"
45 * Log specified fields for the inode given by bp and off.
49 xfs_trans_t *tp, /* transaction pointer */
50 xfs_buf_t *bp, /* inode buffer */
51 int off, /* index of inode in buffer */
52 int fields) /* bitmask of fields to log */
54 int first; /* first byte number */
55 int ioffset; /* off in bytes */
56 int last; /* last byte number */
57 xfs_mount_t *mp; /* mount point structure */
58 static const short offsets[] = { /* field offsets */
59 /* keep in sync with bits */
60 offsetof(xfs_dinode_core_t, di_magic),
61 offsetof(xfs_dinode_core_t, di_mode),
62 offsetof(xfs_dinode_core_t, di_version),
63 offsetof(xfs_dinode_core_t, di_format),
64 offsetof(xfs_dinode_core_t, di_onlink),
65 offsetof(xfs_dinode_core_t, di_uid),
66 offsetof(xfs_dinode_core_t, di_gid),
67 offsetof(xfs_dinode_core_t, di_nlink),
68 offsetof(xfs_dinode_core_t, di_projid),
69 offsetof(xfs_dinode_core_t, di_pad),
70 offsetof(xfs_dinode_core_t, di_atime),
71 offsetof(xfs_dinode_core_t, di_mtime),
72 offsetof(xfs_dinode_core_t, di_ctime),
73 offsetof(xfs_dinode_core_t, di_size),
74 offsetof(xfs_dinode_core_t, di_nblocks),
75 offsetof(xfs_dinode_core_t, di_extsize),
76 offsetof(xfs_dinode_core_t, di_nextents),
77 offsetof(xfs_dinode_core_t, di_anextents),
78 offsetof(xfs_dinode_core_t, di_forkoff),
79 offsetof(xfs_dinode_core_t, di_aformat),
80 offsetof(xfs_dinode_core_t, di_dmevmask),
81 offsetof(xfs_dinode_core_t, di_dmstate),
82 offsetof(xfs_dinode_core_t, di_flags),
83 offsetof(xfs_dinode_core_t, di_gen),
84 offsetof(xfs_dinode_t, di_next_unlinked),
85 offsetof(xfs_dinode_t, di_u),
86 offsetof(xfs_dinode_t, di_a),
91 ASSERT(offsetof(xfs_dinode_t, di_core) == 0);
92 ASSERT((fields & (XFS_DI_U|XFS_DI_A)) == 0);
95 * Get the inode-relative first and last bytes for these fields
97 xfs_btree_offsets(fields, offsets, XFS_DI_NUM_BITS, &first, &last);
99 * Convert to buffer offsets and log it.
101 ioffset = off << mp->m_sb.sb_inodelog;
104 xfs_trans_log_buf(tp, bp, first, last);
108 * Allocation group level functions.
111 xfs_ialloc_cluster_alignment(
112 xfs_alloc_arg_t *args)
114 if (xfs_sb_version_hasalign(&args->mp->m_sb) &&
115 args->mp->m_sb.sb_inoalignmt >=
116 XFS_B_TO_FSBT(args->mp, XFS_INODE_CLUSTER_SIZE(args->mp)))
117 return args->mp->m_sb.sb_inoalignmt;
122 * Allocate new inodes in the allocation group specified by agbp.
123 * Return 0 for success, else error code.
125 STATIC int /* error code or 0 */
127 xfs_trans_t *tp, /* transaction pointer */
128 xfs_buf_t *agbp, /* alloc group buffer */
131 xfs_agi_t *agi; /* allocation group header */
132 xfs_alloc_arg_t args; /* allocation argument structure */
133 int blks_per_cluster; /* fs blocks per inode cluster */
134 xfs_btree_cur_t *cur; /* inode btree cursor */
135 xfs_daddr_t d; /* disk addr of buffer */
138 xfs_buf_t *fbuf; /* new free inodes' buffer */
139 xfs_dinode_t *free; /* new free inode structure */
140 int i; /* inode counter */
141 int j; /* block counter */
142 int nbufs; /* num bufs of new inodes */
143 xfs_agino_t newino; /* new first inode's number */
144 xfs_agino_t newlen; /* new number of inodes */
145 int ninodes; /* num inodes per buf */
146 xfs_agino_t thisino; /* current inode number, for loop */
147 int version; /* inode version number to use */
148 int isaligned = 0; /* inode allocation at stripe unit */
153 args.mp = tp->t_mountp;
156 * Locking will ensure that we don't have two callers in here
159 newlen = XFS_IALLOC_INODES(args.mp);
160 if (args.mp->m_maxicount &&
161 args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount)
162 return XFS_ERROR(ENOSPC);
163 args.minlen = args.maxlen = XFS_IALLOC_BLOCKS(args.mp);
165 * First try to allocate inodes contiguous with the last-allocated
166 * chunk of inodes. If the filesystem is striped, this will fill
167 * an entire stripe unit with inodes.
169 agi = XFS_BUF_TO_AGI(agbp);
170 newino = be32_to_cpu(agi->agi_newino);
171 args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
172 XFS_IALLOC_BLOCKS(args.mp);
173 if (likely(newino != NULLAGINO &&
174 (args.agbno < be32_to_cpu(agi->agi_length)))) {
175 args.fsbno = XFS_AGB_TO_FSB(args.mp,
176 be32_to_cpu(agi->agi_seqno), args.agbno);
177 args.type = XFS_ALLOCTYPE_THIS_BNO;
178 args.mod = args.total = args.wasdel = args.isfl =
179 args.userdata = args.minalignslop = 0;
183 * We need to take into account alignment here to ensure that
184 * we don't modify the free list if we fail to have an exact
185 * block. If we don't have an exact match, and every oher
186 * attempt allocation attempt fails, we'll end up cancelling
187 * a dirty transaction and shutting down.
189 * For an exact allocation, alignment must be 1,
190 * however we need to take cluster alignment into account when
191 * fixing up the freelist. Use the minalignslop field to
192 * indicate that extra blocks might be required for alignment,
193 * but not to use them in the actual exact allocation.
196 args.minalignslop = xfs_ialloc_cluster_alignment(&args) - 1;
198 /* Allow space for the inode btree to split. */
199 args.minleft = XFS_IN_MAXLEVELS(args.mp) - 1;
200 if ((error = xfs_alloc_vextent(&args)))
203 args.fsbno = NULLFSBLOCK;
205 if (unlikely(args.fsbno == NULLFSBLOCK)) {
207 * Set the alignment for the allocation.
208 * If stripe alignment is turned on then align at stripe unit
210 * If the cluster size is smaller than a filesystem block
211 * then we're doing I/O for inodes in filesystem block size
212 * pieces, so don't need alignment anyway.
215 if (args.mp->m_sinoalign) {
216 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
217 args.alignment = args.mp->m_dalign;
220 args.alignment = xfs_ialloc_cluster_alignment(&args);
222 * Need to figure out where to allocate the inode blocks.
223 * Ideally they should be spaced out through the a.g.
224 * For now, just allocate blocks up front.
226 args.agbno = be32_to_cpu(agi->agi_root);
227 args.fsbno = XFS_AGB_TO_FSB(args.mp,
228 be32_to_cpu(agi->agi_seqno), args.agbno);
230 * Allocate a fixed-size extent of inodes.
232 args.type = XFS_ALLOCTYPE_NEAR_BNO;
233 args.mod = args.total = args.wasdel = args.isfl =
234 args.userdata = args.minalignslop = 0;
237 * Allow space for the inode btree to split.
239 args.minleft = XFS_IN_MAXLEVELS(args.mp) - 1;
240 if ((error = xfs_alloc_vextent(&args)))
245 * If stripe alignment is turned on, then try again with cluster
248 if (isaligned && args.fsbno == NULLFSBLOCK) {
249 args.type = XFS_ALLOCTYPE_NEAR_BNO;
250 args.agbno = be32_to_cpu(agi->agi_root);
251 args.fsbno = XFS_AGB_TO_FSB(args.mp,
252 be32_to_cpu(agi->agi_seqno), args.agbno);
253 args.alignment = xfs_ialloc_cluster_alignment(&args);
254 if ((error = xfs_alloc_vextent(&args)))
258 if (args.fsbno == NULLFSBLOCK) {
262 ASSERT(args.len == args.minlen);
264 * Convert the results.
266 newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
268 * Loop over the new block(s), filling in the inodes.
269 * For small block sizes, manipulate the inodes in buffers
270 * which are multiples of the blocks size.
272 if (args.mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(args.mp)) {
273 blks_per_cluster = 1;
274 nbufs = (int)args.len;
275 ninodes = args.mp->m_sb.sb_inopblock;
277 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(args.mp) /
278 args.mp->m_sb.sb_blocksize;
279 nbufs = (int)args.len / blks_per_cluster;
280 ninodes = blks_per_cluster * args.mp->m_sb.sb_inopblock;
283 * Figure out what version number to use in the inodes we create.
284 * If the superblock version has caught up to the one that supports
285 * the new inode format, then use the new inode version. Otherwise
286 * use the old version so that old kernels will continue to be
287 * able to use the file system.
289 if (xfs_sb_version_hasnlink(&args.mp->m_sb))
290 version = XFS_DINODE_VERSION_2;
292 version = XFS_DINODE_VERSION_1;
295 * Seed the new inode cluster with a random generation number. This
296 * prevents short-term reuse of generation numbers if a chunk is
297 * freed and then immediately reallocated. We use random numbers
298 * rather than a linear progression to prevent the next generation
299 * number from being easily guessable.
302 for (j = 0; j < nbufs; j++) {
306 d = XFS_AGB_TO_DADDR(args.mp, be32_to_cpu(agi->agi_seqno),
307 args.agbno + (j * blks_per_cluster));
308 fbuf = xfs_trans_get_buf(tp, args.mp->m_ddev_targp, d,
309 args.mp->m_bsize * blks_per_cluster,
312 ASSERT(!XFS_BUF_GETERROR(fbuf));
314 * Set initial values for the inodes in this buffer.
316 xfs_biozero(fbuf, 0, ninodes << args.mp->m_sb.sb_inodelog);
317 for (i = 0; i < ninodes; i++) {
318 free = XFS_MAKE_IPTR(args.mp, fbuf, i);
319 free->di_core.di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
320 free->di_core.di_version = version;
321 free->di_core.di_gen = cpu_to_be32(gen);
322 free->di_next_unlinked = cpu_to_be32(NULLAGINO);
323 xfs_ialloc_log_di(tp, fbuf, i,
324 XFS_DI_CORE_BITS | XFS_DI_NEXT_UNLINKED);
326 xfs_trans_inode_alloc_buf(tp, fbuf);
328 be32_add_cpu(&agi->agi_count, newlen);
329 be32_add_cpu(&agi->agi_freecount, newlen);
330 agno = be32_to_cpu(agi->agi_seqno);
331 down_read(&args.mp->m_peraglock);
332 args.mp->m_perag[agno].pagi_freecount += newlen;
333 up_read(&args.mp->m_peraglock);
334 agi->agi_newino = cpu_to_be32(newino);
336 * Insert records describing the new inode chunk into the btree.
338 cur = xfs_btree_init_cursor(args.mp, tp, agbp, agno,
339 XFS_BTNUM_INO, (xfs_inode_t *)0, 0);
340 for (thisino = newino;
341 thisino < newino + newlen;
342 thisino += XFS_INODES_PER_CHUNK) {
343 if ((error = xfs_inobt_lookup_eq(cur, thisino,
344 XFS_INODES_PER_CHUNK, XFS_INOBT_ALL_FREE, &i))) {
345 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
349 if ((error = xfs_inobt_insert(cur, &i))) {
350 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
355 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
357 * Log allocation group header fields
359 xfs_ialloc_log_agi(tp, agbp,
360 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
362 * Modify/log superblock values for inode count and inode free count.
364 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
365 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
370 STATIC_INLINE xfs_agnumber_t
376 spin_lock(&mp->m_agirotor_lock);
377 agno = mp->m_agirotor;
378 if (++mp->m_agirotor == mp->m_maxagi)
380 spin_unlock(&mp->m_agirotor_lock);
386 * Select an allocation group to look for a free inode in, based on the parent
387 * inode and then mode. Return the allocation group buffer.
389 STATIC xfs_buf_t * /* allocation group buffer */
390 xfs_ialloc_ag_select(
391 xfs_trans_t *tp, /* transaction pointer */
392 xfs_ino_t parent, /* parent directory inode number */
393 mode_t mode, /* bits set to indicate file type */
394 int okalloc) /* ok to allocate more space */
396 xfs_buf_t *agbp; /* allocation group header buffer */
397 xfs_agnumber_t agcount; /* number of ag's in the filesystem */
398 xfs_agnumber_t agno; /* current ag number */
399 int flags; /* alloc buffer locking flags */
400 xfs_extlen_t ineed; /* blocks needed for inode allocation */
401 xfs_extlen_t longest = 0; /* longest extent available */
402 xfs_mount_t *mp; /* mount point structure */
403 int needspace; /* file mode implies space allocated */
404 xfs_perag_t *pag; /* per allocation group data */
405 xfs_agnumber_t pagno; /* parent (starting) ag number */
408 * Files of these types need at least one block if length > 0
409 * (and they won't fit in the inode, but that's hard to figure out).
411 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
413 agcount = mp->m_maxagi;
415 pagno = xfs_ialloc_next_ag(mp);
417 pagno = XFS_INO_TO_AGNO(mp, parent);
418 if (pagno >= agcount)
421 ASSERT(pagno < agcount);
423 * Loop through allocation groups, looking for one with a little
424 * free space in it. Note we don't look for free inodes, exactly.
425 * Instead, we include whether there is a need to allocate inodes
426 * to mean that blocks must be allocated for them,
427 * if none are currently free.
430 flags = XFS_ALLOC_FLAG_TRYLOCK;
431 down_read(&mp->m_peraglock);
433 pag = &mp->m_perag[agno];
434 if (!pag->pagi_init) {
435 if (xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
442 if (!pag->pagi_inodeok) {
443 xfs_ialloc_next_ag(mp);
448 * Is there enough free space for the file plus a block
449 * of inodes (if we need to allocate some)?
451 ineed = pag->pagi_freecount ? 0 : XFS_IALLOC_BLOCKS(mp);
452 if (ineed && !pag->pagf_init) {
454 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
458 (void)xfs_alloc_pagf_init(mp, tp, agno, flags);
460 if (!ineed || pag->pagf_init) {
461 if (ineed && !(longest = pag->pagf_longest))
462 longest = pag->pagf_flcount > 0;
464 (pag->pagf_freeblks >= needspace + ineed &&
468 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
472 up_read(&mp->m_peraglock);
478 xfs_trans_brelse(tp, agbp);
481 * No point in iterating over the rest, if we're shutting
484 if (XFS_FORCED_SHUTDOWN(mp)) {
485 up_read(&mp->m_peraglock);
493 up_read(&mp->m_peraglock);
502 * Visible inode allocation functions.
506 * Allocate an inode on disk.
507 * Mode is used to tell whether the new inode will need space, and whether
510 * The arguments IO_agbp and alloc_done are defined to work within
511 * the constraint of one allocation per transaction.
512 * xfs_dialloc() is designed to be called twice if it has to do an
513 * allocation to make more free inodes. On the first call,
514 * IO_agbp should be set to NULL. If an inode is available,
515 * i.e., xfs_dialloc() did not need to do an allocation, an inode
516 * number is returned. In this case, IO_agbp would be set to the
517 * current ag_buf and alloc_done set to false.
518 * If an allocation needed to be done, xfs_dialloc would return
519 * the current ag_buf in IO_agbp and set alloc_done to true.
520 * The caller should then commit the current transaction, allocate a new
521 * transaction, and call xfs_dialloc() again, passing in the previous
522 * value of IO_agbp. IO_agbp should be held across the transactions.
523 * Since the agbp is locked across the two calls, the second call is
524 * guaranteed to have a free inode available.
526 * Once we successfully pick an inode its number is returned and the
527 * on-disk data structures are updated. The inode itself is not read
528 * in, since doing so would break ordering constraints with xfs_reclaim.
532 xfs_trans_t *tp, /* transaction pointer */
533 xfs_ino_t parent, /* parent inode (directory) */
534 mode_t mode, /* mode bits for new inode */
535 int okalloc, /* ok to allocate more space */
536 xfs_buf_t **IO_agbp, /* in/out ag header's buffer */
537 boolean_t *alloc_done, /* true if we needed to replenish
539 xfs_ino_t *inop) /* inode number allocated */
541 xfs_agnumber_t agcount; /* number of allocation groups */
542 xfs_buf_t *agbp; /* allocation group header's buffer */
543 xfs_agnumber_t agno; /* allocation group number */
544 xfs_agi_t *agi; /* allocation group header structure */
545 xfs_btree_cur_t *cur; /* inode allocation btree cursor */
546 int error; /* error return value */
547 int i; /* result code */
548 int ialloced; /* inode allocation status */
549 int noroom = 0; /* no space for inode blk allocation */
550 xfs_ino_t ino; /* fs-relative inode to be returned */
552 int j; /* result code */
553 xfs_mount_t *mp; /* file system mount structure */
554 int offset; /* index of inode in chunk */
555 xfs_agino_t pagino; /* parent's a.g. relative inode # */
556 xfs_agnumber_t pagno; /* parent's allocation group number */
557 xfs_inobt_rec_incore_t rec; /* inode allocation record */
558 xfs_agnumber_t tagno; /* testing allocation group number */
559 xfs_btree_cur_t *tcur; /* temp cursor */
560 xfs_inobt_rec_incore_t trec; /* temp inode allocation record */
563 if (*IO_agbp == NULL) {
565 * We do not have an agbp, so select an initial allocation
566 * group for inode allocation.
568 agbp = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
570 * Couldn't find an allocation group satisfying the
577 agi = XFS_BUF_TO_AGI(agbp);
578 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
581 * Continue where we left off before. In this case, we
582 * know that the allocation group has free inodes.
585 agi = XFS_BUF_TO_AGI(agbp);
586 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
587 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
590 agcount = mp->m_sb.sb_agcount;
591 agno = be32_to_cpu(agi->agi_seqno);
593 pagno = XFS_INO_TO_AGNO(mp, parent);
594 pagino = XFS_INO_TO_AGINO(mp, parent);
597 * If we have already hit the ceiling of inode blocks then clear
598 * okalloc so we scan all available agi structures for a free
602 if (mp->m_maxicount &&
603 mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) {
609 * Loop until we find an allocation group that either has free inodes
610 * or in which we can allocate some inodes. Iterate through the
611 * allocation groups upward, wrapping at the end.
613 *alloc_done = B_FALSE;
614 while (!agi->agi_freecount) {
616 * Don't do anything if we're not supposed to allocate
617 * any blocks, just go on to the next ag.
621 * Try to allocate some new inodes in the allocation
624 if ((error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced))) {
625 xfs_trans_brelse(tp, agbp);
626 if (error == ENOSPC) {
634 * We successfully allocated some inodes, return
635 * the current context to the caller so that it
636 * can commit the current transaction and call
637 * us again where we left off.
639 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
640 *alloc_done = B_TRUE;
647 * If it failed, give up on this ag.
649 xfs_trans_brelse(tp, agbp);
651 * Go on to the next ag: get its ag header.
654 if (++tagno == agcount)
658 return noroom ? ENOSPC : 0;
660 down_read(&mp->m_peraglock);
661 if (mp->m_perag[tagno].pagi_inodeok == 0) {
662 up_read(&mp->m_peraglock);
665 error = xfs_ialloc_read_agi(mp, tp, tagno, &agbp);
666 up_read(&mp->m_peraglock);
669 agi = XFS_BUF_TO_AGI(agbp);
670 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
673 * Here with an allocation group that has a free inode.
674 * Reset agno since we may have chosen a new ag in the
679 cur = xfs_btree_init_cursor(mp, tp, agbp, be32_to_cpu(agi->agi_seqno),
680 XFS_BTNUM_INO, (xfs_inode_t *)0, 0);
682 * If pagino is 0 (this is the root inode allocation) use newino.
683 * This must work because we've just allocated some.
686 pagino = be32_to_cpu(agi->agi_newino);
688 if (cur->bc_nlevels == 1) {
691 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
693 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
695 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
696 &rec.ir_freecount, &rec.ir_free, &i)))
698 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
699 freecount += rec.ir_freecount;
700 if ((error = xfs_inobt_increment(cur, 0, &i)))
704 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
705 XFS_FORCED_SHUTDOWN(mp));
709 * If in the same a.g. as the parent, try to get near the parent.
712 if ((error = xfs_inobt_lookup_le(cur, pagino, 0, 0, &i)))
715 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
716 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
718 rec.ir_freecount > 0) {
720 * Found a free inode in the same chunk
725 * In the same a.g. as parent, but parent's chunk is full.
728 int doneleft; /* done, to the left */
729 int doneright; /* done, to the right */
736 * Duplicate the cursor, search left & right
739 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
742 * Search left with tcur, back up 1 record.
744 if ((error = xfs_inobt_decrement(tcur, 0, &i)))
748 if ((error = xfs_inobt_get_rec(tcur,
753 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
756 * Search right with cur, go forward 1 record.
758 if ((error = xfs_inobt_increment(cur, 0, &i)))
762 if ((error = xfs_inobt_get_rec(cur,
767 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
770 * Loop until we find the closest inode chunk
773 while (!doneleft || !doneright) {
774 int useleft; /* using left inode
778 * Figure out which block is closer,
781 if (!doneleft && !doneright)
785 XFS_INODES_PER_CHUNK - 1) <
786 rec.ir_startino - pagino;
790 * If checking the left, does it have
793 if (useleft && trec.ir_freecount) {
795 * Yes, set it up as the chunk to use.
798 xfs_btree_del_cursor(cur,
804 * If checking the right, does it have
807 if (!useleft && rec.ir_freecount) {
809 * Yes, it's already set up.
811 xfs_btree_del_cursor(tcur,
816 * If used the left, get another one
820 if ((error = xfs_inobt_decrement(tcur, 0,
825 if ((error = xfs_inobt_get_rec(
831 XFS_WANT_CORRUPTED_GOTO(i == 1,
836 * If used the right, get another one
840 if ((error = xfs_inobt_increment(cur, 0,
845 if ((error = xfs_inobt_get_rec(
851 XFS_WANT_CORRUPTED_GOTO(i == 1,
856 ASSERT(!doneleft || !doneright);
860 * In a different a.g. from the parent.
861 * See if the most recently allocated block has any free.
863 else if (be32_to_cpu(agi->agi_newino) != NULLAGINO) {
864 if ((error = xfs_inobt_lookup_eq(cur,
865 be32_to_cpu(agi->agi_newino), 0, 0, &i)))
868 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
869 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
871 rec.ir_freecount > 0) {
873 * The last chunk allocated in the group still has
878 * None left in the last group, search the whole a.g.
883 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
887 if ((error = xfs_inobt_get_rec(cur,
889 &rec.ir_freecount, &rec.ir_free,
892 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
893 if (rec.ir_freecount > 0)
895 if ((error = xfs_inobt_increment(cur, 0, &i)))
897 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
901 offset = XFS_IALLOC_FIND_FREE(&rec.ir_free);
903 ASSERT(offset < XFS_INODES_PER_CHUNK);
904 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
905 XFS_INODES_PER_CHUNK) == 0);
906 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
907 XFS_INOBT_CLR_FREE(&rec, offset);
909 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount,
912 be32_add_cpu(&agi->agi_freecount, -1);
913 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
914 down_read(&mp->m_peraglock);
915 mp->m_perag[tagno].pagi_freecount--;
916 up_read(&mp->m_peraglock);
918 if (cur->bc_nlevels == 1) {
921 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
924 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
925 &rec.ir_freecount, &rec.ir_free, &i)))
927 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
928 freecount += rec.ir_freecount;
929 if ((error = xfs_inobt_increment(cur, 0, &i)))
932 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
933 XFS_FORCED_SHUTDOWN(mp));
936 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
937 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
941 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
943 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
948 * Free disk inode. Carefully avoids touching the incore inode, all
949 * manipulations incore are the caller's responsibility.
950 * The on-disk inode is not changed by this operation, only the
951 * btree (free inode mask) is changed.
955 xfs_trans_t *tp, /* transaction pointer */
956 xfs_ino_t inode, /* inode to be freed */
957 xfs_bmap_free_t *flist, /* extents to free */
958 int *delete, /* set if inode cluster was deleted */
959 xfs_ino_t *first_ino) /* first inode in deleted cluster */
962 xfs_agblock_t agbno; /* block number containing inode */
963 xfs_buf_t *agbp; /* buffer containing allocation group header */
964 xfs_agino_t agino; /* inode number relative to allocation group */
965 xfs_agnumber_t agno; /* allocation group number */
966 xfs_agi_t *agi; /* allocation group header */
967 xfs_btree_cur_t *cur; /* inode btree cursor */
968 int error; /* error return value */
969 int i; /* result code */
970 int ilen; /* inodes in an inode cluster */
971 xfs_mount_t *mp; /* mount structure for filesystem */
972 int off; /* offset of inode in inode chunk */
973 xfs_inobt_rec_incore_t rec; /* btree record */
978 * Break up inode number into its components.
980 agno = XFS_INO_TO_AGNO(mp, inode);
981 if (agno >= mp->m_sb.sb_agcount) {
983 "xfs_difree: agno >= mp->m_sb.sb_agcount (%d >= %d) on %s. Returning EINVAL.",
984 agno, mp->m_sb.sb_agcount, mp->m_fsname);
986 return XFS_ERROR(EINVAL);
988 agino = XFS_INO_TO_AGINO(mp, inode);
989 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
991 "xfs_difree: inode != XFS_AGINO_TO_INO() "
992 "(%llu != %llu) on %s. Returning EINVAL.",
993 (unsigned long long)inode,
994 (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino),
997 return XFS_ERROR(EINVAL);
999 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1000 if (agbno >= mp->m_sb.sb_agblocks) {
1002 "xfs_difree: agbno >= mp->m_sb.sb_agblocks (%d >= %d) on %s. Returning EINVAL.",
1003 agbno, mp->m_sb.sb_agblocks, mp->m_fsname);
1005 return XFS_ERROR(EINVAL);
1008 * Get the allocation group header.
1010 down_read(&mp->m_peraglock);
1011 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1012 up_read(&mp->m_peraglock);
1015 "xfs_difree: xfs_ialloc_read_agi() returned an error %d on %s. Returning error.",
1016 error, mp->m_fsname);
1019 agi = XFS_BUF_TO_AGI(agbp);
1020 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
1021 ASSERT(agbno < be32_to_cpu(agi->agi_length));
1023 * Initialize the cursor.
1025 cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO,
1026 (xfs_inode_t *)0, 0);
1028 if (cur->bc_nlevels == 1) {
1031 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1034 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
1035 &rec.ir_freecount, &rec.ir_free, &i)))
1038 freecount += rec.ir_freecount;
1039 if ((error = xfs_inobt_increment(cur, 0, &i)))
1043 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
1044 XFS_FORCED_SHUTDOWN(mp));
1048 * Look for the entry describing this inode.
1050 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1052 "xfs_difree: xfs_inobt_lookup_le returned() an error %d on %s. Returning error.",
1053 error, mp->m_fsname);
1056 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1057 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, &rec.ir_freecount,
1058 &rec.ir_free, &i))) {
1060 "xfs_difree: xfs_inobt_get_rec() returned an error %d on %s. Returning error.",
1061 error, mp->m_fsname);
1064 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1066 * Get the offset in the inode chunk.
1068 off = agino - rec.ir_startino;
1069 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
1070 ASSERT(!XFS_INOBT_IS_FREE(&rec, off));
1072 * Mark the inode free & increment the count.
1074 XFS_INOBT_SET_FREE(&rec, off);
1078 * When an inode cluster is free, it becomes eligible for removal
1080 if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
1081 (rec.ir_freecount == XFS_IALLOC_INODES(mp))) {
1084 *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1087 * Remove the inode cluster from the AGI B+Tree, adjust the
1088 * AGI and Superblock inode counts, and mark the disk space
1089 * to be freed when the transaction is committed.
1091 ilen = XFS_IALLOC_INODES(mp);
1092 be32_add_cpu(&agi->agi_count, -ilen);
1093 be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
1094 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
1095 down_read(&mp->m_peraglock);
1096 mp->m_perag[agno].pagi_freecount -= ilen - 1;
1097 up_read(&mp->m_peraglock);
1098 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1099 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1101 if ((error = xfs_inobt_delete(cur, &i))) {
1102 cmn_err(CE_WARN, "xfs_difree: xfs_inobt_delete returned an error %d on %s.\n",
1103 error, mp->m_fsname);
1107 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp,
1108 agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)),
1109 XFS_IALLOC_BLOCKS(mp), flist, mp);
1113 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount, rec.ir_free))) {
1115 "xfs_difree: xfs_inobt_update() returned an error %d on %s. Returning error.",
1116 error, mp->m_fsname);
1120 * Change the inode free counts and log the ag/sb changes.
1122 be32_add_cpu(&agi->agi_freecount, 1);
1123 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1124 down_read(&mp->m_peraglock);
1125 mp->m_perag[agno].pagi_freecount++;
1126 up_read(&mp->m_peraglock);
1127 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
1131 if (cur->bc_nlevels == 1) {
1134 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1137 if ((error = xfs_inobt_get_rec(cur,
1143 freecount += rec.ir_freecount;
1144 if ((error = xfs_inobt_increment(cur, 0, &i)))
1148 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
1149 XFS_FORCED_SHUTDOWN(mp));
1152 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1156 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1161 * Return the location of the inode in bno/off, for mapping it into a buffer.
1166 xfs_mount_t *mp, /* file system mount structure */
1167 xfs_trans_t *tp, /* transaction pointer */
1168 xfs_ino_t ino, /* inode to locate */
1169 xfs_fsblock_t *bno, /* output: block containing inode */
1170 int *len, /* output: num blocks in inode cluster */
1171 int *off, /* output: index in block of inode */
1172 uint flags) /* flags concerning inode lookup */
1174 xfs_agblock_t agbno; /* block number of inode in the alloc group */
1175 xfs_buf_t *agbp; /* agi buffer */
1176 xfs_agino_t agino; /* inode number within alloc group */
1177 xfs_agnumber_t agno; /* allocation group number */
1178 int blks_per_cluster; /* num blocks per inode cluster */
1179 xfs_agblock_t chunk_agbno; /* first block in inode chunk */
1180 xfs_agino_t chunk_agino; /* first agino in inode chunk */
1181 __int32_t chunk_cnt; /* count of free inodes in chunk */
1182 xfs_inofree_t chunk_free; /* mask of free inodes in chunk */
1183 xfs_agblock_t cluster_agbno; /* first block in inode cluster */
1184 xfs_btree_cur_t *cur; /* inode btree cursor */
1185 int error; /* error code */
1186 int i; /* temp state */
1187 int offset; /* index of inode in its buffer */
1188 int offset_agbno; /* blks from chunk start to inode */
1190 ASSERT(ino != NULLFSINO);
1192 * Split up the inode number into its parts.
1194 agno = XFS_INO_TO_AGNO(mp, ino);
1195 agino = XFS_INO_TO_AGINO(mp, ino);
1196 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1197 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
1198 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1200 /* no diagnostics for bulkstat, ino comes from userspace */
1201 if (flags & XFS_IMAP_BULKSTAT)
1202 return XFS_ERROR(EINVAL);
1203 if (agno >= mp->m_sb.sb_agcount) {
1204 xfs_fs_cmn_err(CE_ALERT, mp,
1205 "xfs_dilocate: agno (%d) >= "
1206 "mp->m_sb.sb_agcount (%d)",
1207 agno, mp->m_sb.sb_agcount);
1209 if (agbno >= mp->m_sb.sb_agblocks) {
1210 xfs_fs_cmn_err(CE_ALERT, mp,
1211 "xfs_dilocate: agbno (0x%llx) >= "
1212 "mp->m_sb.sb_agblocks (0x%lx)",
1213 (unsigned long long) agbno,
1214 (unsigned long) mp->m_sb.sb_agblocks);
1216 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1217 xfs_fs_cmn_err(CE_ALERT, mp,
1218 "xfs_dilocate: ino (0x%llx) != "
1219 "XFS_AGINO_TO_INO(mp, agno, agino) "
1221 ino, XFS_AGINO_TO_INO(mp, agno, agino));
1225 return XFS_ERROR(EINVAL);
1227 if ((mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) ||
1228 !(flags & XFS_IMAP_LOOKUP)) {
1229 offset = XFS_INO_TO_OFFSET(mp, ino);
1230 ASSERT(offset < mp->m_sb.sb_inopblock);
1231 *bno = XFS_AGB_TO_FSB(mp, agno, agbno);
1236 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog;
1237 if (*bno != NULLFSBLOCK) {
1238 offset = XFS_INO_TO_OFFSET(mp, ino);
1239 ASSERT(offset < mp->m_sb.sb_inopblock);
1240 cluster_agbno = XFS_FSB_TO_AGBNO(mp, *bno);
1241 *off = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1243 *len = blks_per_cluster;
1246 if (mp->m_inoalign_mask) {
1247 offset_agbno = agbno & mp->m_inoalign_mask;
1248 chunk_agbno = agbno - offset_agbno;
1250 down_read(&mp->m_peraglock);
1251 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1252 up_read(&mp->m_peraglock);
1255 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1256 "xfs_ialloc_read_agi() returned "
1257 "error %d, agno %d",
1262 cur = xfs_btree_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_INO,
1263 (xfs_inode_t *)0, 0);
1264 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1266 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1267 "xfs_inobt_lookup_le() failed");
1271 if ((error = xfs_inobt_get_rec(cur, &chunk_agino, &chunk_cnt,
1272 &chunk_free, &i))) {
1274 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1275 "xfs_inobt_get_rec() failed");
1281 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1282 "xfs_inobt_get_rec() failed");
1284 error = XFS_ERROR(EINVAL);
1286 xfs_trans_brelse(tp, agbp);
1287 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1290 chunk_agbno = XFS_AGINO_TO_AGBNO(mp, chunk_agino);
1291 offset_agbno = agbno - chunk_agbno;
1293 ASSERT(agbno >= chunk_agbno);
1294 cluster_agbno = chunk_agbno +
1295 ((offset_agbno / blks_per_cluster) * blks_per_cluster);
1296 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1297 XFS_INO_TO_OFFSET(mp, ino);
1298 *bno = XFS_AGB_TO_FSB(mp, agno, cluster_agbno);
1300 *len = blks_per_cluster;
1303 xfs_trans_brelse(tp, agbp);
1304 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1309 * Compute and fill in value of m_in_maxlevels.
1312 xfs_ialloc_compute_maxlevels(
1313 xfs_mount_t *mp) /* file system mount structure */
1321 maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
1322 XFS_INODES_PER_CHUNK_LOG;
1323 minleafrecs = mp->m_alloc_mnr[0];
1324 minnoderecs = mp->m_alloc_mnr[1];
1325 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1326 for (level = 1; maxblocks > 1; level++)
1327 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1328 mp->m_in_maxlevels = level;
1332 * Log specified fields for the ag hdr (inode section)
1336 xfs_trans_t *tp, /* transaction pointer */
1337 xfs_buf_t *bp, /* allocation group header buffer */
1338 int fields) /* bitmask of fields to log */
1340 int first; /* first byte number */
1341 int last; /* last byte number */
1342 static const short offsets[] = { /* field starting offsets */
1343 /* keep in sync with bit definitions */
1344 offsetof(xfs_agi_t, agi_magicnum),
1345 offsetof(xfs_agi_t, agi_versionnum),
1346 offsetof(xfs_agi_t, agi_seqno),
1347 offsetof(xfs_agi_t, agi_length),
1348 offsetof(xfs_agi_t, agi_count),
1349 offsetof(xfs_agi_t, agi_root),
1350 offsetof(xfs_agi_t, agi_level),
1351 offsetof(xfs_agi_t, agi_freecount),
1352 offsetof(xfs_agi_t, agi_newino),
1353 offsetof(xfs_agi_t, agi_dirino),
1354 offsetof(xfs_agi_t, agi_unlinked),
1358 xfs_agi_t *agi; /* allocation group header */
1360 agi = XFS_BUF_TO_AGI(bp);
1361 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
1364 * Compute byte offsets for the first and last fields.
1366 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last);
1368 * Log the allocation group inode header buffer.
1370 xfs_trans_log_buf(tp, bp, first, last);
1374 * Read in the allocation group header (inode allocation section)
1377 xfs_ialloc_read_agi(
1378 xfs_mount_t *mp, /* file system mount structure */
1379 xfs_trans_t *tp, /* transaction pointer */
1380 xfs_agnumber_t agno, /* allocation group number */
1381 xfs_buf_t **bpp) /* allocation group hdr buf */
1383 xfs_agi_t *agi; /* allocation group header */
1384 int agi_ok; /* agi is consistent */
1385 xfs_buf_t *bp; /* allocation group hdr buf */
1386 xfs_perag_t *pag; /* per allocation group data */
1389 ASSERT(agno != NULLAGNUMBER);
1390 error = xfs_trans_read_buf(
1391 mp, tp, mp->m_ddev_targp,
1392 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
1393 XFS_FSS_TO_BB(mp, 1), 0, &bp);
1396 ASSERT(bp && !XFS_BUF_GETERROR(bp));
1399 * Validate the magic number of the agi block.
1401 agi = XFS_BUF_TO_AGI(bp);
1403 be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC &&
1404 XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum));
1405 if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI,
1406 XFS_RANDOM_IALLOC_READ_AGI))) {
1407 XFS_CORRUPTION_ERROR("xfs_ialloc_read_agi", XFS_ERRLEVEL_LOW,
1409 xfs_trans_brelse(tp, bp);
1410 return XFS_ERROR(EFSCORRUPTED);
1412 pag = &mp->m_perag[agno];
1413 if (!pag->pagi_init) {
1414 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
1415 pag->pagi_count = be32_to_cpu(agi->agi_count);
1419 * It's possible for these to be out of sync if
1420 * we are in the middle of a forced shutdown.
1422 ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
1423 XFS_FORCED_SHUTDOWN(mp));
1430 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
1431 ASSERT(agi->agi_unlinked[i]);
1435 XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGI, XFS_AGI_REF);
1441 * Read in the agi to initialise the per-ag data in the mount structure
1444 xfs_ialloc_pagi_init(
1445 xfs_mount_t *mp, /* file system mount structure */
1446 xfs_trans_t *tp, /* transaction pointer */
1447 xfs_agnumber_t agno) /* allocation group number */
1449 xfs_buf_t *bp = NULL;
1452 error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
1456 xfs_trans_brelse(tp, bp);