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_inobt_init_cursor(args.mp, tp, agbp, agno);
339 for (thisino = newino;
340 thisino < newino + newlen;
341 thisino += XFS_INODES_PER_CHUNK) {
342 if ((error = xfs_inobt_lookup_eq(cur, thisino,
343 XFS_INODES_PER_CHUNK, XFS_INOBT_ALL_FREE, &i))) {
344 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
348 if ((error = xfs_inobt_insert(cur, &i))) {
349 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
354 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
356 * Log allocation group header fields
358 xfs_ialloc_log_agi(tp, agbp,
359 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
361 * Modify/log superblock values for inode count and inode free count.
363 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
364 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
369 STATIC_INLINE xfs_agnumber_t
375 spin_lock(&mp->m_agirotor_lock);
376 agno = mp->m_agirotor;
377 if (++mp->m_agirotor == mp->m_maxagi)
379 spin_unlock(&mp->m_agirotor_lock);
385 * Select an allocation group to look for a free inode in, based on the parent
386 * inode and then mode. Return the allocation group buffer.
388 STATIC xfs_buf_t * /* allocation group buffer */
389 xfs_ialloc_ag_select(
390 xfs_trans_t *tp, /* transaction pointer */
391 xfs_ino_t parent, /* parent directory inode number */
392 mode_t mode, /* bits set to indicate file type */
393 int okalloc) /* ok to allocate more space */
395 xfs_buf_t *agbp; /* allocation group header buffer */
396 xfs_agnumber_t agcount; /* number of ag's in the filesystem */
397 xfs_agnumber_t agno; /* current ag number */
398 int flags; /* alloc buffer locking flags */
399 xfs_extlen_t ineed; /* blocks needed for inode allocation */
400 xfs_extlen_t longest = 0; /* longest extent available */
401 xfs_mount_t *mp; /* mount point structure */
402 int needspace; /* file mode implies space allocated */
403 xfs_perag_t *pag; /* per allocation group data */
404 xfs_agnumber_t pagno; /* parent (starting) ag number */
407 * Files of these types need at least one block if length > 0
408 * (and they won't fit in the inode, but that's hard to figure out).
410 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
412 agcount = mp->m_maxagi;
414 pagno = xfs_ialloc_next_ag(mp);
416 pagno = XFS_INO_TO_AGNO(mp, parent);
417 if (pagno >= agcount)
420 ASSERT(pagno < agcount);
422 * Loop through allocation groups, looking for one with a little
423 * free space in it. Note we don't look for free inodes, exactly.
424 * Instead, we include whether there is a need to allocate inodes
425 * to mean that blocks must be allocated for them,
426 * if none are currently free.
429 flags = XFS_ALLOC_FLAG_TRYLOCK;
430 down_read(&mp->m_peraglock);
432 pag = &mp->m_perag[agno];
433 if (!pag->pagi_init) {
434 if (xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
441 if (!pag->pagi_inodeok) {
442 xfs_ialloc_next_ag(mp);
447 * Is there enough free space for the file plus a block
448 * of inodes (if we need to allocate some)?
450 ineed = pag->pagi_freecount ? 0 : XFS_IALLOC_BLOCKS(mp);
451 if (ineed && !pag->pagf_init) {
453 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
457 (void)xfs_alloc_pagf_init(mp, tp, agno, flags);
459 if (!ineed || pag->pagf_init) {
460 if (ineed && !(longest = pag->pagf_longest))
461 longest = pag->pagf_flcount > 0;
463 (pag->pagf_freeblks >= needspace + ineed &&
467 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
471 up_read(&mp->m_peraglock);
477 xfs_trans_brelse(tp, agbp);
480 * No point in iterating over the rest, if we're shutting
483 if (XFS_FORCED_SHUTDOWN(mp)) {
484 up_read(&mp->m_peraglock);
492 up_read(&mp->m_peraglock);
501 * Visible inode allocation functions.
505 * Allocate an inode on disk.
506 * Mode is used to tell whether the new inode will need space, and whether
509 * The arguments IO_agbp and alloc_done are defined to work within
510 * the constraint of one allocation per transaction.
511 * xfs_dialloc() is designed to be called twice if it has to do an
512 * allocation to make more free inodes. On the first call,
513 * IO_agbp should be set to NULL. If an inode is available,
514 * i.e., xfs_dialloc() did not need to do an allocation, an inode
515 * number is returned. In this case, IO_agbp would be set to the
516 * current ag_buf and alloc_done set to false.
517 * If an allocation needed to be done, xfs_dialloc would return
518 * the current ag_buf in IO_agbp and set alloc_done to true.
519 * The caller should then commit the current transaction, allocate a new
520 * transaction, and call xfs_dialloc() again, passing in the previous
521 * value of IO_agbp. IO_agbp should be held across the transactions.
522 * Since the agbp is locked across the two calls, the second call is
523 * guaranteed to have a free inode available.
525 * Once we successfully pick an inode its number is returned and the
526 * on-disk data structures are updated. The inode itself is not read
527 * in, since doing so would break ordering constraints with xfs_reclaim.
531 xfs_trans_t *tp, /* transaction pointer */
532 xfs_ino_t parent, /* parent inode (directory) */
533 mode_t mode, /* mode bits for new inode */
534 int okalloc, /* ok to allocate more space */
535 xfs_buf_t **IO_agbp, /* in/out ag header's buffer */
536 boolean_t *alloc_done, /* true if we needed to replenish
538 xfs_ino_t *inop) /* inode number allocated */
540 xfs_agnumber_t agcount; /* number of allocation groups */
541 xfs_buf_t *agbp; /* allocation group header's buffer */
542 xfs_agnumber_t agno; /* allocation group number */
543 xfs_agi_t *agi; /* allocation group header structure */
544 xfs_btree_cur_t *cur; /* inode allocation btree cursor */
545 int error; /* error return value */
546 int i; /* result code */
547 int ialloced; /* inode allocation status */
548 int noroom = 0; /* no space for inode blk allocation */
549 xfs_ino_t ino; /* fs-relative inode to be returned */
551 int j; /* result code */
552 xfs_mount_t *mp; /* file system mount structure */
553 int offset; /* index of inode in chunk */
554 xfs_agino_t pagino; /* parent's a.g. relative inode # */
555 xfs_agnumber_t pagno; /* parent's allocation group number */
556 xfs_inobt_rec_incore_t rec; /* inode allocation record */
557 xfs_agnumber_t tagno; /* testing allocation group number */
558 xfs_btree_cur_t *tcur; /* temp cursor */
559 xfs_inobt_rec_incore_t trec; /* temp inode allocation record */
562 if (*IO_agbp == NULL) {
564 * We do not have an agbp, so select an initial allocation
565 * group for inode allocation.
567 agbp = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
569 * Couldn't find an allocation group satisfying the
576 agi = XFS_BUF_TO_AGI(agbp);
577 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
580 * Continue where we left off before. In this case, we
581 * know that the allocation group has free inodes.
584 agi = XFS_BUF_TO_AGI(agbp);
585 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
586 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
589 agcount = mp->m_sb.sb_agcount;
590 agno = be32_to_cpu(agi->agi_seqno);
592 pagno = XFS_INO_TO_AGNO(mp, parent);
593 pagino = XFS_INO_TO_AGINO(mp, parent);
596 * If we have already hit the ceiling of inode blocks then clear
597 * okalloc so we scan all available agi structures for a free
601 if (mp->m_maxicount &&
602 mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) {
608 * Loop until we find an allocation group that either has free inodes
609 * or in which we can allocate some inodes. Iterate through the
610 * allocation groups upward, wrapping at the end.
612 *alloc_done = B_FALSE;
613 while (!agi->agi_freecount) {
615 * Don't do anything if we're not supposed to allocate
616 * any blocks, just go on to the next ag.
620 * Try to allocate some new inodes in the allocation
623 if ((error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced))) {
624 xfs_trans_brelse(tp, agbp);
625 if (error == ENOSPC) {
633 * We successfully allocated some inodes, return
634 * the current context to the caller so that it
635 * can commit the current transaction and call
636 * us again where we left off.
638 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
639 *alloc_done = B_TRUE;
646 * If it failed, give up on this ag.
648 xfs_trans_brelse(tp, agbp);
650 * Go on to the next ag: get its ag header.
653 if (++tagno == agcount)
657 return noroom ? ENOSPC : 0;
659 down_read(&mp->m_peraglock);
660 if (mp->m_perag[tagno].pagi_inodeok == 0) {
661 up_read(&mp->m_peraglock);
664 error = xfs_ialloc_read_agi(mp, tp, tagno, &agbp);
665 up_read(&mp->m_peraglock);
668 agi = XFS_BUF_TO_AGI(agbp);
669 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
672 * Here with an allocation group that has a free inode.
673 * Reset agno since we may have chosen a new ag in the
678 cur = xfs_inobt_init_cursor(mp, tp, agbp, be32_to_cpu(agi->agi_seqno));
680 * If pagino is 0 (this is the root inode allocation) use newino.
681 * This must work because we've just allocated some.
684 pagino = be32_to_cpu(agi->agi_newino);
686 if (cur->bc_nlevels == 1) {
689 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
691 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
693 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
694 &rec.ir_freecount, &rec.ir_free, &i)))
696 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
697 freecount += rec.ir_freecount;
698 if ((error = xfs_inobt_increment(cur, 0, &i)))
702 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
703 XFS_FORCED_SHUTDOWN(mp));
707 * If in the same a.g. as the parent, try to get near the parent.
710 if ((error = xfs_inobt_lookup_le(cur, pagino, 0, 0, &i)))
713 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
714 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
716 rec.ir_freecount > 0) {
718 * Found a free inode in the same chunk
723 * In the same a.g. as parent, but parent's chunk is full.
726 int doneleft; /* done, to the left */
727 int doneright; /* done, to the right */
734 * Duplicate the cursor, search left & right
737 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
740 * Search left with tcur, back up 1 record.
742 if ((error = xfs_inobt_decrement(tcur, 0, &i)))
746 if ((error = xfs_inobt_get_rec(tcur,
751 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
754 * Search right with cur, go forward 1 record.
756 if ((error = xfs_inobt_increment(cur, 0, &i)))
760 if ((error = xfs_inobt_get_rec(cur,
765 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
768 * Loop until we find the closest inode chunk
771 while (!doneleft || !doneright) {
772 int useleft; /* using left inode
776 * Figure out which block is closer,
779 if (!doneleft && !doneright)
783 XFS_INODES_PER_CHUNK - 1) <
784 rec.ir_startino - pagino;
788 * If checking the left, does it have
791 if (useleft && trec.ir_freecount) {
793 * Yes, set it up as the chunk to use.
796 xfs_btree_del_cursor(cur,
802 * If checking the right, does it have
805 if (!useleft && rec.ir_freecount) {
807 * Yes, it's already set up.
809 xfs_btree_del_cursor(tcur,
814 * If used the left, get another one
818 if ((error = xfs_inobt_decrement(tcur, 0,
823 if ((error = xfs_inobt_get_rec(
829 XFS_WANT_CORRUPTED_GOTO(i == 1,
834 * If used the right, get another one
838 if ((error = xfs_inobt_increment(cur, 0,
843 if ((error = xfs_inobt_get_rec(
849 XFS_WANT_CORRUPTED_GOTO(i == 1,
854 ASSERT(!doneleft || !doneright);
858 * In a different a.g. from the parent.
859 * See if the most recently allocated block has any free.
861 else if (be32_to_cpu(agi->agi_newino) != NULLAGINO) {
862 if ((error = xfs_inobt_lookup_eq(cur,
863 be32_to_cpu(agi->agi_newino), 0, 0, &i)))
866 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
867 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
869 rec.ir_freecount > 0) {
871 * The last chunk allocated in the group still has
876 * None left in the last group, search the whole a.g.
881 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
885 if ((error = xfs_inobt_get_rec(cur,
887 &rec.ir_freecount, &rec.ir_free,
890 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
891 if (rec.ir_freecount > 0)
893 if ((error = xfs_inobt_increment(cur, 0, &i)))
895 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
899 offset = XFS_IALLOC_FIND_FREE(&rec.ir_free);
901 ASSERT(offset < XFS_INODES_PER_CHUNK);
902 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
903 XFS_INODES_PER_CHUNK) == 0);
904 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
905 XFS_INOBT_CLR_FREE(&rec, offset);
907 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount,
910 be32_add_cpu(&agi->agi_freecount, -1);
911 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
912 down_read(&mp->m_peraglock);
913 mp->m_perag[tagno].pagi_freecount--;
914 up_read(&mp->m_peraglock);
916 if (cur->bc_nlevels == 1) {
919 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
922 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
923 &rec.ir_freecount, &rec.ir_free, &i)))
925 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
926 freecount += rec.ir_freecount;
927 if ((error = xfs_inobt_increment(cur, 0, &i)))
930 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
931 XFS_FORCED_SHUTDOWN(mp));
934 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
935 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
939 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
941 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
946 * Free disk inode. Carefully avoids touching the incore inode, all
947 * manipulations incore are the caller's responsibility.
948 * The on-disk inode is not changed by this operation, only the
949 * btree (free inode mask) is changed.
953 xfs_trans_t *tp, /* transaction pointer */
954 xfs_ino_t inode, /* inode to be freed */
955 xfs_bmap_free_t *flist, /* extents to free */
956 int *delete, /* set if inode cluster was deleted */
957 xfs_ino_t *first_ino) /* first inode in deleted cluster */
960 xfs_agblock_t agbno; /* block number containing inode */
961 xfs_buf_t *agbp; /* buffer containing allocation group header */
962 xfs_agino_t agino; /* inode number relative to allocation group */
963 xfs_agnumber_t agno; /* allocation group number */
964 xfs_agi_t *agi; /* allocation group header */
965 xfs_btree_cur_t *cur; /* inode btree cursor */
966 int error; /* error return value */
967 int i; /* result code */
968 int ilen; /* inodes in an inode cluster */
969 xfs_mount_t *mp; /* mount structure for filesystem */
970 int off; /* offset of inode in inode chunk */
971 xfs_inobt_rec_incore_t rec; /* btree record */
976 * Break up inode number into its components.
978 agno = XFS_INO_TO_AGNO(mp, inode);
979 if (agno >= mp->m_sb.sb_agcount) {
981 "xfs_difree: agno >= mp->m_sb.sb_agcount (%d >= %d) on %s. Returning EINVAL.",
982 agno, mp->m_sb.sb_agcount, mp->m_fsname);
984 return XFS_ERROR(EINVAL);
986 agino = XFS_INO_TO_AGINO(mp, inode);
987 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
989 "xfs_difree: inode != XFS_AGINO_TO_INO() "
990 "(%llu != %llu) on %s. Returning EINVAL.",
991 (unsigned long long)inode,
992 (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino),
995 return XFS_ERROR(EINVAL);
997 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
998 if (agbno >= mp->m_sb.sb_agblocks) {
1000 "xfs_difree: agbno >= mp->m_sb.sb_agblocks (%d >= %d) on %s. Returning EINVAL.",
1001 agbno, mp->m_sb.sb_agblocks, mp->m_fsname);
1003 return XFS_ERROR(EINVAL);
1006 * Get the allocation group header.
1008 down_read(&mp->m_peraglock);
1009 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1010 up_read(&mp->m_peraglock);
1013 "xfs_difree: xfs_ialloc_read_agi() returned an error %d on %s. Returning error.",
1014 error, mp->m_fsname);
1017 agi = XFS_BUF_TO_AGI(agbp);
1018 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
1019 ASSERT(agbno < be32_to_cpu(agi->agi_length));
1021 * Initialize the cursor.
1023 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
1025 if (cur->bc_nlevels == 1) {
1028 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1031 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
1032 &rec.ir_freecount, &rec.ir_free, &i)))
1035 freecount += rec.ir_freecount;
1036 if ((error = xfs_inobt_increment(cur, 0, &i)))
1040 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
1041 XFS_FORCED_SHUTDOWN(mp));
1045 * Look for the entry describing this inode.
1047 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1049 "xfs_difree: xfs_inobt_lookup_le returned() an error %d on %s. Returning error.",
1050 error, mp->m_fsname);
1053 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1054 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, &rec.ir_freecount,
1055 &rec.ir_free, &i))) {
1057 "xfs_difree: xfs_inobt_get_rec() returned an error %d on %s. Returning error.",
1058 error, mp->m_fsname);
1061 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1063 * Get the offset in the inode chunk.
1065 off = agino - rec.ir_startino;
1066 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
1067 ASSERT(!XFS_INOBT_IS_FREE(&rec, off));
1069 * Mark the inode free & increment the count.
1071 XFS_INOBT_SET_FREE(&rec, off);
1075 * When an inode cluster is free, it becomes eligible for removal
1077 if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
1078 (rec.ir_freecount == XFS_IALLOC_INODES(mp))) {
1081 *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1084 * Remove the inode cluster from the AGI B+Tree, adjust the
1085 * AGI and Superblock inode counts, and mark the disk space
1086 * to be freed when the transaction is committed.
1088 ilen = XFS_IALLOC_INODES(mp);
1089 be32_add_cpu(&agi->agi_count, -ilen);
1090 be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
1091 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
1092 down_read(&mp->m_peraglock);
1093 mp->m_perag[agno].pagi_freecount -= ilen - 1;
1094 up_read(&mp->m_peraglock);
1095 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1096 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1098 if ((error = xfs_inobt_delete(cur, &i))) {
1099 cmn_err(CE_WARN, "xfs_difree: xfs_inobt_delete returned an error %d on %s.\n",
1100 error, mp->m_fsname);
1104 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp,
1105 agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)),
1106 XFS_IALLOC_BLOCKS(mp), flist, mp);
1110 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount, rec.ir_free))) {
1112 "xfs_difree: xfs_inobt_update() returned an error %d on %s. Returning error.",
1113 error, mp->m_fsname);
1117 * Change the inode free counts and log the ag/sb changes.
1119 be32_add_cpu(&agi->agi_freecount, 1);
1120 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1121 down_read(&mp->m_peraglock);
1122 mp->m_perag[agno].pagi_freecount++;
1123 up_read(&mp->m_peraglock);
1124 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
1128 if (cur->bc_nlevels == 1) {
1131 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1134 if ((error = xfs_inobt_get_rec(cur,
1140 freecount += rec.ir_freecount;
1141 if ((error = xfs_inobt_increment(cur, 0, &i)))
1145 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
1146 XFS_FORCED_SHUTDOWN(mp));
1149 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1153 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1158 * Return the location of the inode in bno/off, for mapping it into a buffer.
1163 xfs_mount_t *mp, /* file system mount structure */
1164 xfs_trans_t *tp, /* transaction pointer */
1165 xfs_ino_t ino, /* inode to locate */
1166 xfs_fsblock_t *bno, /* output: block containing inode */
1167 int *len, /* output: num blocks in inode cluster */
1168 int *off, /* output: index in block of inode */
1169 uint flags) /* flags concerning inode lookup */
1171 xfs_agblock_t agbno; /* block number of inode in the alloc group */
1172 xfs_buf_t *agbp; /* agi buffer */
1173 xfs_agino_t agino; /* inode number within alloc group */
1174 xfs_agnumber_t agno; /* allocation group number */
1175 int blks_per_cluster; /* num blocks per inode cluster */
1176 xfs_agblock_t chunk_agbno; /* first block in inode chunk */
1177 xfs_agino_t chunk_agino; /* first agino in inode chunk */
1178 __int32_t chunk_cnt; /* count of free inodes in chunk */
1179 xfs_inofree_t chunk_free; /* mask of free inodes in chunk */
1180 xfs_agblock_t cluster_agbno; /* first block in inode cluster */
1181 xfs_btree_cur_t *cur; /* inode btree cursor */
1182 int error; /* error code */
1183 int i; /* temp state */
1184 int offset; /* index of inode in its buffer */
1185 int offset_agbno; /* blks from chunk start to inode */
1187 ASSERT(ino != NULLFSINO);
1189 * Split up the inode number into its parts.
1191 agno = XFS_INO_TO_AGNO(mp, ino);
1192 agino = XFS_INO_TO_AGINO(mp, ino);
1193 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1194 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
1195 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1197 /* no diagnostics for bulkstat, ino comes from userspace */
1198 if (flags & XFS_IMAP_BULKSTAT)
1199 return XFS_ERROR(EINVAL);
1200 if (agno >= mp->m_sb.sb_agcount) {
1201 xfs_fs_cmn_err(CE_ALERT, mp,
1202 "xfs_dilocate: agno (%d) >= "
1203 "mp->m_sb.sb_agcount (%d)",
1204 agno, mp->m_sb.sb_agcount);
1206 if (agbno >= mp->m_sb.sb_agblocks) {
1207 xfs_fs_cmn_err(CE_ALERT, mp,
1208 "xfs_dilocate: agbno (0x%llx) >= "
1209 "mp->m_sb.sb_agblocks (0x%lx)",
1210 (unsigned long long) agbno,
1211 (unsigned long) mp->m_sb.sb_agblocks);
1213 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1214 xfs_fs_cmn_err(CE_ALERT, mp,
1215 "xfs_dilocate: ino (0x%llx) != "
1216 "XFS_AGINO_TO_INO(mp, agno, agino) "
1218 ino, XFS_AGINO_TO_INO(mp, agno, agino));
1222 return XFS_ERROR(EINVAL);
1224 if ((mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) ||
1225 !(flags & XFS_IMAP_LOOKUP)) {
1226 offset = XFS_INO_TO_OFFSET(mp, ino);
1227 ASSERT(offset < mp->m_sb.sb_inopblock);
1228 *bno = XFS_AGB_TO_FSB(mp, agno, agbno);
1233 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog;
1234 if (*bno != NULLFSBLOCK) {
1235 offset = XFS_INO_TO_OFFSET(mp, ino);
1236 ASSERT(offset < mp->m_sb.sb_inopblock);
1237 cluster_agbno = XFS_FSB_TO_AGBNO(mp, *bno);
1238 *off = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1240 *len = blks_per_cluster;
1243 if (mp->m_inoalign_mask) {
1244 offset_agbno = agbno & mp->m_inoalign_mask;
1245 chunk_agbno = agbno - offset_agbno;
1247 down_read(&mp->m_peraglock);
1248 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1249 up_read(&mp->m_peraglock);
1252 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1253 "xfs_ialloc_read_agi() returned "
1254 "error %d, agno %d",
1259 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
1260 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1262 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1263 "xfs_inobt_lookup_le() failed");
1267 if ((error = xfs_inobt_get_rec(cur, &chunk_agino, &chunk_cnt,
1268 &chunk_free, &i))) {
1270 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1271 "xfs_inobt_get_rec() failed");
1277 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_dilocate: "
1278 "xfs_inobt_get_rec() failed");
1280 error = XFS_ERROR(EINVAL);
1282 xfs_trans_brelse(tp, agbp);
1283 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1286 chunk_agbno = XFS_AGINO_TO_AGBNO(mp, chunk_agino);
1287 offset_agbno = agbno - chunk_agbno;
1289 ASSERT(agbno >= chunk_agbno);
1290 cluster_agbno = chunk_agbno +
1291 ((offset_agbno / blks_per_cluster) * blks_per_cluster);
1292 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1293 XFS_INO_TO_OFFSET(mp, ino);
1294 *bno = XFS_AGB_TO_FSB(mp, agno, cluster_agbno);
1296 *len = blks_per_cluster;
1299 xfs_trans_brelse(tp, agbp);
1300 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1305 * Compute and fill in value of m_in_maxlevels.
1308 xfs_ialloc_compute_maxlevels(
1309 xfs_mount_t *mp) /* file system mount structure */
1317 maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
1318 XFS_INODES_PER_CHUNK_LOG;
1319 minleafrecs = mp->m_alloc_mnr[0];
1320 minnoderecs = mp->m_alloc_mnr[1];
1321 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1322 for (level = 1; maxblocks > 1; level++)
1323 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1324 mp->m_in_maxlevels = level;
1328 * Log specified fields for the ag hdr (inode section)
1332 xfs_trans_t *tp, /* transaction pointer */
1333 xfs_buf_t *bp, /* allocation group header buffer */
1334 int fields) /* bitmask of fields to log */
1336 int first; /* first byte number */
1337 int last; /* last byte number */
1338 static const short offsets[] = { /* field starting offsets */
1339 /* keep in sync with bit definitions */
1340 offsetof(xfs_agi_t, agi_magicnum),
1341 offsetof(xfs_agi_t, agi_versionnum),
1342 offsetof(xfs_agi_t, agi_seqno),
1343 offsetof(xfs_agi_t, agi_length),
1344 offsetof(xfs_agi_t, agi_count),
1345 offsetof(xfs_agi_t, agi_root),
1346 offsetof(xfs_agi_t, agi_level),
1347 offsetof(xfs_agi_t, agi_freecount),
1348 offsetof(xfs_agi_t, agi_newino),
1349 offsetof(xfs_agi_t, agi_dirino),
1350 offsetof(xfs_agi_t, agi_unlinked),
1354 xfs_agi_t *agi; /* allocation group header */
1356 agi = XFS_BUF_TO_AGI(bp);
1357 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
1360 * Compute byte offsets for the first and last fields.
1362 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last);
1364 * Log the allocation group inode header buffer.
1366 xfs_trans_log_buf(tp, bp, first, last);
1370 * Read in the allocation group header (inode allocation section)
1373 xfs_ialloc_read_agi(
1374 xfs_mount_t *mp, /* file system mount structure */
1375 xfs_trans_t *tp, /* transaction pointer */
1376 xfs_agnumber_t agno, /* allocation group number */
1377 xfs_buf_t **bpp) /* allocation group hdr buf */
1379 xfs_agi_t *agi; /* allocation group header */
1380 int agi_ok; /* agi is consistent */
1381 xfs_buf_t *bp; /* allocation group hdr buf */
1382 xfs_perag_t *pag; /* per allocation group data */
1385 ASSERT(agno != NULLAGNUMBER);
1386 error = xfs_trans_read_buf(
1387 mp, tp, mp->m_ddev_targp,
1388 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
1389 XFS_FSS_TO_BB(mp, 1), 0, &bp);
1392 ASSERT(bp && !XFS_BUF_GETERROR(bp));
1395 * Validate the magic number of the agi block.
1397 agi = XFS_BUF_TO_AGI(bp);
1399 be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC &&
1400 XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum));
1401 if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI,
1402 XFS_RANDOM_IALLOC_READ_AGI))) {
1403 XFS_CORRUPTION_ERROR("xfs_ialloc_read_agi", XFS_ERRLEVEL_LOW,
1405 xfs_trans_brelse(tp, bp);
1406 return XFS_ERROR(EFSCORRUPTED);
1408 pag = &mp->m_perag[agno];
1409 if (!pag->pagi_init) {
1410 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
1411 pag->pagi_count = be32_to_cpu(agi->agi_count);
1415 * It's possible for these to be out of sync if
1416 * we are in the middle of a forced shutdown.
1418 ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
1419 XFS_FORCED_SHUTDOWN(mp));
1426 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
1427 ASSERT(agi->agi_unlinked[i]);
1431 XFS_BUF_SET_VTYPE_REF(bp, B_FS_AGI, XFS_AGI_REF);
1437 * Read in the agi to initialise the per-ag data in the mount structure
1440 xfs_ialloc_pagi_init(
1441 xfs_mount_t *mp, /* file system mount structure */
1442 xfs_trans_t *tp, /* transaction pointer */
1443 xfs_agnumber_t agno) /* allocation group number */
1445 xfs_buf_t *bp = NULL;
1448 error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
1452 xfs_trans_brelse(tp, bp);