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"
47 * Allocation group level functions.
50 xfs_ialloc_cluster_alignment(
51 xfs_alloc_arg_t *args)
53 if (xfs_sb_version_hasalign(&args->mp->m_sb) &&
54 args->mp->m_sb.sb_inoalignmt >=
55 XFS_B_TO_FSBT(args->mp, XFS_INODE_CLUSTER_SIZE(args->mp)))
56 return args->mp->m_sb.sb_inoalignmt;
61 * Lookup the record equal to ino in the btree given by cur.
63 STATIC int /* error */
65 struct xfs_btree_cur *cur, /* btree cursor */
66 xfs_agino_t ino, /* starting inode of chunk */
67 __int32_t fcnt, /* free inode count */
68 xfs_inofree_t free, /* free inode mask */
69 int *stat) /* success/failure */
71 cur->bc_rec.i.ir_startino = ino;
72 cur->bc_rec.i.ir_freecount = fcnt;
73 cur->bc_rec.i.ir_free = free;
74 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
78 * Lookup the first record greater than or equal to ino
79 * in the btree given by cur.
83 struct xfs_btree_cur *cur, /* btree cursor */
84 xfs_agino_t ino, /* starting inode of chunk */
85 __int32_t fcnt, /* free inode count */
86 xfs_inofree_t free, /* free inode mask */
87 int *stat) /* success/failure */
89 cur->bc_rec.i.ir_startino = ino;
90 cur->bc_rec.i.ir_freecount = fcnt;
91 cur->bc_rec.i.ir_free = free;
92 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
96 * Lookup the first record less than or equal to ino
97 * in the btree given by cur.
101 struct xfs_btree_cur *cur, /* btree cursor */
102 xfs_agino_t ino, /* starting inode of chunk */
103 __int32_t fcnt, /* free inode count */
104 xfs_inofree_t free, /* free inode mask */
105 int *stat) /* success/failure */
107 cur->bc_rec.i.ir_startino = ino;
108 cur->bc_rec.i.ir_freecount = fcnt;
109 cur->bc_rec.i.ir_free = free;
110 return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
114 * Update the record referred to by cur to the value given
115 * by [ino, fcnt, free].
116 * This either works (return 0) or gets an EFSCORRUPTED error.
118 STATIC int /* error */
120 struct xfs_btree_cur *cur, /* btree cursor */
121 xfs_agino_t ino, /* starting inode of chunk */
122 __int32_t fcnt, /* free inode count */
123 xfs_inofree_t free) /* free inode mask */
125 union xfs_btree_rec rec;
127 rec.inobt.ir_startino = cpu_to_be32(ino);
128 rec.inobt.ir_freecount = cpu_to_be32(fcnt);
129 rec.inobt.ir_free = cpu_to_be64(free);
130 return xfs_btree_update(cur, &rec);
134 * Get the data from the pointed-to record.
138 struct xfs_btree_cur *cur, /* btree cursor */
139 xfs_agino_t *ino, /* output: starting inode of chunk */
140 __int32_t *fcnt, /* output: number of free inodes */
141 xfs_inofree_t *free, /* output: free inode mask */
142 int *stat) /* output: success/failure */
144 union xfs_btree_rec *rec;
147 error = xfs_btree_get_rec(cur, &rec, stat);
148 if (!error && *stat == 1) {
149 *ino = be32_to_cpu(rec->inobt.ir_startino);
150 *fcnt = be32_to_cpu(rec->inobt.ir_freecount);
151 *free = be64_to_cpu(rec->inobt.ir_free);
157 * Allocate new inodes in the allocation group specified by agbp.
158 * Return 0 for success, else error code.
160 STATIC int /* error code or 0 */
162 xfs_trans_t *tp, /* transaction pointer */
163 xfs_buf_t *agbp, /* alloc group buffer */
166 xfs_agi_t *agi; /* allocation group header */
167 xfs_alloc_arg_t args; /* allocation argument structure */
168 int blks_per_cluster; /* fs blocks per inode cluster */
169 xfs_btree_cur_t *cur; /* inode btree cursor */
170 xfs_daddr_t d; /* disk addr of buffer */
173 xfs_buf_t *fbuf; /* new free inodes' buffer */
174 xfs_dinode_t *free; /* new free inode structure */
175 int i; /* inode counter */
176 int j; /* block counter */
177 int nbufs; /* num bufs of new inodes */
178 xfs_agino_t newino; /* new first inode's number */
179 xfs_agino_t newlen; /* new number of inodes */
180 int ninodes; /* num inodes per buf */
181 xfs_agino_t thisino; /* current inode number, for loop */
182 int version; /* inode version number to use */
183 int isaligned = 0; /* inode allocation at stripe unit */
188 args.mp = tp->t_mountp;
191 * Locking will ensure that we don't have two callers in here
194 newlen = XFS_IALLOC_INODES(args.mp);
195 if (args.mp->m_maxicount &&
196 args.mp->m_sb.sb_icount + newlen > args.mp->m_maxicount)
197 return XFS_ERROR(ENOSPC);
198 args.minlen = args.maxlen = XFS_IALLOC_BLOCKS(args.mp);
200 * First try to allocate inodes contiguous with the last-allocated
201 * chunk of inodes. If the filesystem is striped, this will fill
202 * an entire stripe unit with inodes.
204 agi = XFS_BUF_TO_AGI(agbp);
205 newino = be32_to_cpu(agi->agi_newino);
206 args.agbno = XFS_AGINO_TO_AGBNO(args.mp, newino) +
207 XFS_IALLOC_BLOCKS(args.mp);
208 if (likely(newino != NULLAGINO &&
209 (args.agbno < be32_to_cpu(agi->agi_length)))) {
210 args.fsbno = XFS_AGB_TO_FSB(args.mp,
211 be32_to_cpu(agi->agi_seqno), args.agbno);
212 args.type = XFS_ALLOCTYPE_THIS_BNO;
213 args.mod = args.total = args.wasdel = args.isfl =
214 args.userdata = args.minalignslop = 0;
218 * We need to take into account alignment here to ensure that
219 * we don't modify the free list if we fail to have an exact
220 * block. If we don't have an exact match, and every oher
221 * attempt allocation attempt fails, we'll end up cancelling
222 * a dirty transaction and shutting down.
224 * For an exact allocation, alignment must be 1,
225 * however we need to take cluster alignment into account when
226 * fixing up the freelist. Use the minalignslop field to
227 * indicate that extra blocks might be required for alignment,
228 * but not to use them in the actual exact allocation.
231 args.minalignslop = xfs_ialloc_cluster_alignment(&args) - 1;
233 /* Allow space for the inode btree to split. */
234 args.minleft = XFS_IN_MAXLEVELS(args.mp) - 1;
235 if ((error = xfs_alloc_vextent(&args)))
238 args.fsbno = NULLFSBLOCK;
240 if (unlikely(args.fsbno == NULLFSBLOCK)) {
242 * Set the alignment for the allocation.
243 * If stripe alignment is turned on then align at stripe unit
245 * If the cluster size is smaller than a filesystem block
246 * then we're doing I/O for inodes in filesystem block size
247 * pieces, so don't need alignment anyway.
250 if (args.mp->m_sinoalign) {
251 ASSERT(!(args.mp->m_flags & XFS_MOUNT_NOALIGN));
252 args.alignment = args.mp->m_dalign;
255 args.alignment = xfs_ialloc_cluster_alignment(&args);
257 * Need to figure out where to allocate the inode blocks.
258 * Ideally they should be spaced out through the a.g.
259 * For now, just allocate blocks up front.
261 args.agbno = be32_to_cpu(agi->agi_root);
262 args.fsbno = XFS_AGB_TO_FSB(args.mp,
263 be32_to_cpu(agi->agi_seqno), args.agbno);
265 * Allocate a fixed-size extent of inodes.
267 args.type = XFS_ALLOCTYPE_NEAR_BNO;
268 args.mod = args.total = args.wasdel = args.isfl =
269 args.userdata = args.minalignslop = 0;
272 * Allow space for the inode btree to split.
274 args.minleft = XFS_IN_MAXLEVELS(args.mp) - 1;
275 if ((error = xfs_alloc_vextent(&args)))
280 * If stripe alignment is turned on, then try again with cluster
283 if (isaligned && args.fsbno == NULLFSBLOCK) {
284 args.type = XFS_ALLOCTYPE_NEAR_BNO;
285 args.agbno = be32_to_cpu(agi->agi_root);
286 args.fsbno = XFS_AGB_TO_FSB(args.mp,
287 be32_to_cpu(agi->agi_seqno), args.agbno);
288 args.alignment = xfs_ialloc_cluster_alignment(&args);
289 if ((error = xfs_alloc_vextent(&args)))
293 if (args.fsbno == NULLFSBLOCK) {
297 ASSERT(args.len == args.minlen);
299 * Convert the results.
301 newino = XFS_OFFBNO_TO_AGINO(args.mp, args.agbno, 0);
303 * Loop over the new block(s), filling in the inodes.
304 * For small block sizes, manipulate the inodes in buffers
305 * which are multiples of the blocks size.
307 if (args.mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(args.mp)) {
308 blks_per_cluster = 1;
309 nbufs = (int)args.len;
310 ninodes = args.mp->m_sb.sb_inopblock;
312 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(args.mp) /
313 args.mp->m_sb.sb_blocksize;
314 nbufs = (int)args.len / blks_per_cluster;
315 ninodes = blks_per_cluster * args.mp->m_sb.sb_inopblock;
318 * Figure out what version number to use in the inodes we create.
319 * If the superblock version has caught up to the one that supports
320 * the new inode format, then use the new inode version. Otherwise
321 * use the old version so that old kernels will continue to be
322 * able to use the file system.
324 if (xfs_sb_version_hasnlink(&args.mp->m_sb))
330 * Seed the new inode cluster with a random generation number. This
331 * prevents short-term reuse of generation numbers if a chunk is
332 * freed and then immediately reallocated. We use random numbers
333 * rather than a linear progression to prevent the next generation
334 * number from being easily guessable.
337 for (j = 0; j < nbufs; j++) {
341 d = XFS_AGB_TO_DADDR(args.mp, be32_to_cpu(agi->agi_seqno),
342 args.agbno + (j * blks_per_cluster));
343 fbuf = xfs_trans_get_buf(tp, args.mp->m_ddev_targp, d,
344 args.mp->m_bsize * blks_per_cluster,
347 ASSERT(!XFS_BUF_GETERROR(fbuf));
350 * Initialize all inodes in this buffer and then log them.
352 * XXX: It would be much better if we had just one transaction to
353 * log a whole cluster of inodes instead of all the indivdual
354 * transactions causing a lot of log traffic.
356 xfs_biozero(fbuf, 0, ninodes << args.mp->m_sb.sb_inodelog);
357 for (i = 0; i < ninodes; i++) {
358 int ioffset = i << args.mp->m_sb.sb_inodelog;
359 uint isize = sizeof(struct xfs_dinode);
361 free = XFS_MAKE_IPTR(args.mp, fbuf, i);
362 free->di_magic = cpu_to_be16(XFS_DINODE_MAGIC);
363 free->di_version = version;
364 free->di_gen = cpu_to_be32(gen);
365 free->di_next_unlinked = cpu_to_be32(NULLAGINO);
366 xfs_trans_log_buf(tp, fbuf, ioffset, ioffset + isize - 1);
368 xfs_trans_inode_alloc_buf(tp, fbuf);
370 be32_add_cpu(&agi->agi_count, newlen);
371 be32_add_cpu(&agi->agi_freecount, newlen);
372 agno = be32_to_cpu(agi->agi_seqno);
373 down_read(&args.mp->m_peraglock);
374 args.mp->m_perag[agno].pagi_freecount += newlen;
375 up_read(&args.mp->m_peraglock);
376 agi->agi_newino = cpu_to_be32(newino);
378 * Insert records describing the new inode chunk into the btree.
380 cur = xfs_inobt_init_cursor(args.mp, tp, agbp, agno);
381 for (thisino = newino;
382 thisino < newino + newlen;
383 thisino += XFS_INODES_PER_CHUNK) {
384 if ((error = xfs_inobt_lookup_eq(cur, thisino,
385 XFS_INODES_PER_CHUNK, XFS_INOBT_ALL_FREE, &i))) {
386 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
390 if ((error = xfs_btree_insert(cur, &i))) {
391 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
396 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
398 * Log allocation group header fields
400 xfs_ialloc_log_agi(tp, agbp,
401 XFS_AGI_COUNT | XFS_AGI_FREECOUNT | XFS_AGI_NEWINO);
403 * Modify/log superblock values for inode count and inode free count.
405 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, (long)newlen);
406 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, (long)newlen);
411 STATIC_INLINE xfs_agnumber_t
417 spin_lock(&mp->m_agirotor_lock);
418 agno = mp->m_agirotor;
419 if (++mp->m_agirotor == mp->m_maxagi)
421 spin_unlock(&mp->m_agirotor_lock);
427 * Select an allocation group to look for a free inode in, based on the parent
428 * inode and then mode. Return the allocation group buffer.
430 STATIC xfs_buf_t * /* allocation group buffer */
431 xfs_ialloc_ag_select(
432 xfs_trans_t *tp, /* transaction pointer */
433 xfs_ino_t parent, /* parent directory inode number */
434 mode_t mode, /* bits set to indicate file type */
435 int okalloc) /* ok to allocate more space */
437 xfs_buf_t *agbp; /* allocation group header buffer */
438 xfs_agnumber_t agcount; /* number of ag's in the filesystem */
439 xfs_agnumber_t agno; /* current ag number */
440 int flags; /* alloc buffer locking flags */
441 xfs_extlen_t ineed; /* blocks needed for inode allocation */
442 xfs_extlen_t longest = 0; /* longest extent available */
443 xfs_mount_t *mp; /* mount point structure */
444 int needspace; /* file mode implies space allocated */
445 xfs_perag_t *pag; /* per allocation group data */
446 xfs_agnumber_t pagno; /* parent (starting) ag number */
449 * Files of these types need at least one block if length > 0
450 * (and they won't fit in the inode, but that's hard to figure out).
452 needspace = S_ISDIR(mode) || S_ISREG(mode) || S_ISLNK(mode);
454 agcount = mp->m_maxagi;
456 pagno = xfs_ialloc_next_ag(mp);
458 pagno = XFS_INO_TO_AGNO(mp, parent);
459 if (pagno >= agcount)
462 ASSERT(pagno < agcount);
464 * Loop through allocation groups, looking for one with a little
465 * free space in it. Note we don't look for free inodes, exactly.
466 * Instead, we include whether there is a need to allocate inodes
467 * to mean that blocks must be allocated for them,
468 * if none are currently free.
471 flags = XFS_ALLOC_FLAG_TRYLOCK;
472 down_read(&mp->m_peraglock);
474 pag = &mp->m_perag[agno];
475 if (!pag->pagi_init) {
476 if (xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
483 if (!pag->pagi_inodeok) {
484 xfs_ialloc_next_ag(mp);
489 * Is there enough free space for the file plus a block
490 * of inodes (if we need to allocate some)?
492 ineed = pag->pagi_freecount ? 0 : XFS_IALLOC_BLOCKS(mp);
493 if (ineed && !pag->pagf_init) {
495 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
499 (void)xfs_alloc_pagf_init(mp, tp, agno, flags);
501 if (!ineed || pag->pagf_init) {
502 if (ineed && !(longest = pag->pagf_longest))
503 longest = pag->pagf_flcount > 0;
505 (pag->pagf_freeblks >= needspace + ineed &&
509 xfs_ialloc_read_agi(mp, tp, agno, &agbp)) {
513 up_read(&mp->m_peraglock);
519 xfs_trans_brelse(tp, agbp);
522 * No point in iterating over the rest, if we're shutting
525 if (XFS_FORCED_SHUTDOWN(mp)) {
526 up_read(&mp->m_peraglock);
534 up_read(&mp->m_peraglock);
543 * Visible inode allocation functions.
547 * Allocate an inode on disk.
548 * Mode is used to tell whether the new inode will need space, and whether
551 * The arguments IO_agbp and alloc_done are defined to work within
552 * the constraint of one allocation per transaction.
553 * xfs_dialloc() is designed to be called twice if it has to do an
554 * allocation to make more free inodes. On the first call,
555 * IO_agbp should be set to NULL. If an inode is available,
556 * i.e., xfs_dialloc() did not need to do an allocation, an inode
557 * number is returned. In this case, IO_agbp would be set to the
558 * current ag_buf and alloc_done set to false.
559 * If an allocation needed to be done, xfs_dialloc would return
560 * the current ag_buf in IO_agbp and set alloc_done to true.
561 * The caller should then commit the current transaction, allocate a new
562 * transaction, and call xfs_dialloc() again, passing in the previous
563 * value of IO_agbp. IO_agbp should be held across the transactions.
564 * Since the agbp is locked across the two calls, the second call is
565 * guaranteed to have a free inode available.
567 * Once we successfully pick an inode its number is returned and the
568 * on-disk data structures are updated. The inode itself is not read
569 * in, since doing so would break ordering constraints with xfs_reclaim.
573 xfs_trans_t *tp, /* transaction pointer */
574 xfs_ino_t parent, /* parent inode (directory) */
575 mode_t mode, /* mode bits for new inode */
576 int okalloc, /* ok to allocate more space */
577 xfs_buf_t **IO_agbp, /* in/out ag header's buffer */
578 boolean_t *alloc_done, /* true if we needed to replenish
580 xfs_ino_t *inop) /* inode number allocated */
582 xfs_agnumber_t agcount; /* number of allocation groups */
583 xfs_buf_t *agbp; /* allocation group header's buffer */
584 xfs_agnumber_t agno; /* allocation group number */
585 xfs_agi_t *agi; /* allocation group header structure */
586 xfs_btree_cur_t *cur; /* inode allocation btree cursor */
587 int error; /* error return value */
588 int i; /* result code */
589 int ialloced; /* inode allocation status */
590 int noroom = 0; /* no space for inode blk allocation */
591 xfs_ino_t ino; /* fs-relative inode to be returned */
593 int j; /* result code */
594 xfs_mount_t *mp; /* file system mount structure */
595 int offset; /* index of inode in chunk */
596 xfs_agino_t pagino; /* parent's a.g. relative inode # */
597 xfs_agnumber_t pagno; /* parent's allocation group number */
598 xfs_inobt_rec_incore_t rec; /* inode allocation record */
599 xfs_agnumber_t tagno; /* testing allocation group number */
600 xfs_btree_cur_t *tcur; /* temp cursor */
601 xfs_inobt_rec_incore_t trec; /* temp inode allocation record */
604 if (*IO_agbp == NULL) {
606 * We do not have an agbp, so select an initial allocation
607 * group for inode allocation.
609 agbp = xfs_ialloc_ag_select(tp, parent, mode, okalloc);
611 * Couldn't find an allocation group satisfying the
618 agi = XFS_BUF_TO_AGI(agbp);
619 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
622 * Continue where we left off before. In this case, we
623 * know that the allocation group has free inodes.
626 agi = XFS_BUF_TO_AGI(agbp);
627 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
628 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
631 agcount = mp->m_sb.sb_agcount;
632 agno = be32_to_cpu(agi->agi_seqno);
634 pagno = XFS_INO_TO_AGNO(mp, parent);
635 pagino = XFS_INO_TO_AGINO(mp, parent);
638 * If we have already hit the ceiling of inode blocks then clear
639 * okalloc so we scan all available agi structures for a free
643 if (mp->m_maxicount &&
644 mp->m_sb.sb_icount + XFS_IALLOC_INODES(mp) > mp->m_maxicount) {
650 * Loop until we find an allocation group that either has free inodes
651 * or in which we can allocate some inodes. Iterate through the
652 * allocation groups upward, wrapping at the end.
654 *alloc_done = B_FALSE;
655 while (!agi->agi_freecount) {
657 * Don't do anything if we're not supposed to allocate
658 * any blocks, just go on to the next ag.
662 * Try to allocate some new inodes in the allocation
665 if ((error = xfs_ialloc_ag_alloc(tp, agbp, &ialloced))) {
666 xfs_trans_brelse(tp, agbp);
667 if (error == ENOSPC) {
675 * We successfully allocated some inodes, return
676 * the current context to the caller so that it
677 * can commit the current transaction and call
678 * us again where we left off.
680 ASSERT(be32_to_cpu(agi->agi_freecount) > 0);
681 *alloc_done = B_TRUE;
688 * If it failed, give up on this ag.
690 xfs_trans_brelse(tp, agbp);
692 * Go on to the next ag: get its ag header.
695 if (++tagno == agcount)
699 return noroom ? ENOSPC : 0;
701 down_read(&mp->m_peraglock);
702 if (mp->m_perag[tagno].pagi_inodeok == 0) {
703 up_read(&mp->m_peraglock);
706 error = xfs_ialloc_read_agi(mp, tp, tagno, &agbp);
707 up_read(&mp->m_peraglock);
710 agi = XFS_BUF_TO_AGI(agbp);
711 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
714 * Here with an allocation group that has a free inode.
715 * Reset agno since we may have chosen a new ag in the
720 cur = xfs_inobt_init_cursor(mp, tp, agbp, be32_to_cpu(agi->agi_seqno));
722 * If pagino is 0 (this is the root inode allocation) use newino.
723 * This must work because we've just allocated some.
726 pagino = be32_to_cpu(agi->agi_newino);
728 if (cur->bc_nlevels == 1) {
731 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
733 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
735 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
736 &rec.ir_freecount, &rec.ir_free, &i)))
738 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
739 freecount += rec.ir_freecount;
740 if ((error = xfs_btree_increment(cur, 0, &i)))
744 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
745 XFS_FORCED_SHUTDOWN(mp));
749 * If in the same a.g. as the parent, try to get near the parent.
752 if ((error = xfs_inobt_lookup_le(cur, pagino, 0, 0, &i)))
755 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
756 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
758 rec.ir_freecount > 0) {
760 * Found a free inode in the same chunk
765 * In the same a.g. as parent, but parent's chunk is full.
768 int doneleft; /* done, to the left */
769 int doneright; /* done, to the right */
776 * Duplicate the cursor, search left & right
779 if ((error = xfs_btree_dup_cursor(cur, &tcur)))
782 * Search left with tcur, back up 1 record.
784 if ((error = xfs_btree_decrement(tcur, 0, &i)))
788 if ((error = xfs_inobt_get_rec(tcur,
793 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
796 * Search right with cur, go forward 1 record.
798 if ((error = xfs_btree_increment(cur, 0, &i)))
802 if ((error = xfs_inobt_get_rec(cur,
807 XFS_WANT_CORRUPTED_GOTO(i == 1, error1);
810 * Loop until we find the closest inode chunk
813 while (!doneleft || !doneright) {
814 int useleft; /* using left inode
818 * Figure out which block is closer,
821 if (!doneleft && !doneright)
825 XFS_INODES_PER_CHUNK - 1) <
826 rec.ir_startino - pagino;
830 * If checking the left, does it have
833 if (useleft && trec.ir_freecount) {
835 * Yes, set it up as the chunk to use.
838 xfs_btree_del_cursor(cur,
844 * If checking the right, does it have
847 if (!useleft && rec.ir_freecount) {
849 * Yes, it's already set up.
851 xfs_btree_del_cursor(tcur,
856 * If used the left, get another one
860 if ((error = xfs_btree_decrement(tcur, 0,
865 if ((error = xfs_inobt_get_rec(
871 XFS_WANT_CORRUPTED_GOTO(i == 1,
876 * If used the right, get another one
880 if ((error = xfs_btree_increment(cur, 0,
885 if ((error = xfs_inobt_get_rec(
891 XFS_WANT_CORRUPTED_GOTO(i == 1,
896 ASSERT(!doneleft || !doneright);
900 * In a different a.g. from the parent.
901 * See if the most recently allocated block has any free.
903 else if (be32_to_cpu(agi->agi_newino) != NULLAGINO) {
904 if ((error = xfs_inobt_lookup_eq(cur,
905 be32_to_cpu(agi->agi_newino), 0, 0, &i)))
908 (error = xfs_inobt_get_rec(cur, &rec.ir_startino,
909 &rec.ir_freecount, &rec.ir_free, &j)) == 0 &&
911 rec.ir_freecount > 0) {
913 * The last chunk allocated in the group still has
918 * None left in the last group, search the whole a.g.
923 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
927 if ((error = xfs_inobt_get_rec(cur,
929 &rec.ir_freecount, &rec.ir_free,
932 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
933 if (rec.ir_freecount > 0)
935 if ((error = xfs_btree_increment(cur, 0, &i)))
937 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
941 offset = XFS_IALLOC_FIND_FREE(&rec.ir_free);
943 ASSERT(offset < XFS_INODES_PER_CHUNK);
944 ASSERT((XFS_AGINO_TO_OFFSET(mp, rec.ir_startino) %
945 XFS_INODES_PER_CHUNK) == 0);
946 ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino + offset);
947 XFS_INOBT_CLR_FREE(&rec, offset);
949 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount,
952 be32_add_cpu(&agi->agi_freecount, -1);
953 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
954 down_read(&mp->m_peraglock);
955 mp->m_perag[tagno].pagi_freecount--;
956 up_read(&mp->m_peraglock);
958 if (cur->bc_nlevels == 1) {
961 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
964 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
965 &rec.ir_freecount, &rec.ir_free, &i)))
967 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
968 freecount += rec.ir_freecount;
969 if ((error = xfs_btree_increment(cur, 0, &i)))
972 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
973 XFS_FORCED_SHUTDOWN(mp));
976 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
977 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -1);
981 xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
983 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
988 * Free disk inode. Carefully avoids touching the incore inode, all
989 * manipulations incore are the caller's responsibility.
990 * The on-disk inode is not changed by this operation, only the
991 * btree (free inode mask) is changed.
995 xfs_trans_t *tp, /* transaction pointer */
996 xfs_ino_t inode, /* inode to be freed */
997 xfs_bmap_free_t *flist, /* extents to free */
998 int *delete, /* set if inode cluster was deleted */
999 xfs_ino_t *first_ino) /* first inode in deleted cluster */
1002 xfs_agblock_t agbno; /* block number containing inode */
1003 xfs_buf_t *agbp; /* buffer containing allocation group header */
1004 xfs_agino_t agino; /* inode number relative to allocation group */
1005 xfs_agnumber_t agno; /* allocation group number */
1006 xfs_agi_t *agi; /* allocation group header */
1007 xfs_btree_cur_t *cur; /* inode btree cursor */
1008 int error; /* error return value */
1009 int i; /* result code */
1010 int ilen; /* inodes in an inode cluster */
1011 xfs_mount_t *mp; /* mount structure for filesystem */
1012 int off; /* offset of inode in inode chunk */
1013 xfs_inobt_rec_incore_t rec; /* btree record */
1018 * Break up inode number into its components.
1020 agno = XFS_INO_TO_AGNO(mp, inode);
1021 if (agno >= mp->m_sb.sb_agcount) {
1023 "xfs_difree: agno >= mp->m_sb.sb_agcount (%d >= %d) on %s. Returning EINVAL.",
1024 agno, mp->m_sb.sb_agcount, mp->m_fsname);
1026 return XFS_ERROR(EINVAL);
1028 agino = XFS_INO_TO_AGINO(mp, inode);
1029 if (inode != XFS_AGINO_TO_INO(mp, agno, agino)) {
1031 "xfs_difree: inode != XFS_AGINO_TO_INO() "
1032 "(%llu != %llu) on %s. Returning EINVAL.",
1033 (unsigned long long)inode,
1034 (unsigned long long)XFS_AGINO_TO_INO(mp, agno, agino),
1037 return XFS_ERROR(EINVAL);
1039 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1040 if (agbno >= mp->m_sb.sb_agblocks) {
1042 "xfs_difree: agbno >= mp->m_sb.sb_agblocks (%d >= %d) on %s. Returning EINVAL.",
1043 agbno, mp->m_sb.sb_agblocks, mp->m_fsname);
1045 return XFS_ERROR(EINVAL);
1048 * Get the allocation group header.
1050 down_read(&mp->m_peraglock);
1051 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1052 up_read(&mp->m_peraglock);
1055 "xfs_difree: xfs_ialloc_read_agi() returned an error %d on %s. Returning error.",
1056 error, mp->m_fsname);
1059 agi = XFS_BUF_TO_AGI(agbp);
1060 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
1061 ASSERT(agbno < be32_to_cpu(agi->agi_length));
1063 * Initialize the cursor.
1065 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
1067 if (cur->bc_nlevels == 1) {
1070 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1073 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino,
1074 &rec.ir_freecount, &rec.ir_free, &i)))
1077 freecount += rec.ir_freecount;
1078 if ((error = xfs_btree_increment(cur, 0, &i)))
1082 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
1083 XFS_FORCED_SHUTDOWN(mp));
1087 * Look for the entry describing this inode.
1089 if ((error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i))) {
1091 "xfs_difree: xfs_inobt_lookup_le returned() an error %d on %s. Returning error.",
1092 error, mp->m_fsname);
1095 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1096 if ((error = xfs_inobt_get_rec(cur, &rec.ir_startino, &rec.ir_freecount,
1097 &rec.ir_free, &i))) {
1099 "xfs_difree: xfs_inobt_get_rec() returned an error %d on %s. Returning error.",
1100 error, mp->m_fsname);
1103 XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
1105 * Get the offset in the inode chunk.
1107 off = agino - rec.ir_startino;
1108 ASSERT(off >= 0 && off < XFS_INODES_PER_CHUNK);
1109 ASSERT(!XFS_INOBT_IS_FREE(&rec, off));
1111 * Mark the inode free & increment the count.
1113 XFS_INOBT_SET_FREE(&rec, off);
1117 * When an inode cluster is free, it becomes eligible for removal
1119 if (!(mp->m_flags & XFS_MOUNT_IKEEP) &&
1120 (rec.ir_freecount == XFS_IALLOC_INODES(mp))) {
1123 *first_ino = XFS_AGINO_TO_INO(mp, agno, rec.ir_startino);
1126 * Remove the inode cluster from the AGI B+Tree, adjust the
1127 * AGI and Superblock inode counts, and mark the disk space
1128 * to be freed when the transaction is committed.
1130 ilen = XFS_IALLOC_INODES(mp);
1131 be32_add_cpu(&agi->agi_count, -ilen);
1132 be32_add_cpu(&agi->agi_freecount, -(ilen - 1));
1133 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_COUNT | XFS_AGI_FREECOUNT);
1134 down_read(&mp->m_peraglock);
1135 mp->m_perag[agno].pagi_freecount -= ilen - 1;
1136 up_read(&mp->m_peraglock);
1137 xfs_trans_mod_sb(tp, XFS_TRANS_SB_ICOUNT, -ilen);
1138 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, -(ilen - 1));
1140 if ((error = xfs_btree_delete(cur, &i))) {
1141 cmn_err(CE_WARN, "xfs_difree: xfs_btree_delete returned an error %d on %s.\n",
1142 error, mp->m_fsname);
1146 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp,
1147 agno, XFS_INO_TO_AGBNO(mp,rec.ir_startino)),
1148 XFS_IALLOC_BLOCKS(mp), flist, mp);
1152 if ((error = xfs_inobt_update(cur, rec.ir_startino, rec.ir_freecount, rec.ir_free))) {
1154 "xfs_difree: xfs_inobt_update() returned an error %d on %s. Returning error.",
1155 error, mp->m_fsname);
1159 * Change the inode free counts and log the ag/sb changes.
1161 be32_add_cpu(&agi->agi_freecount, 1);
1162 xfs_ialloc_log_agi(tp, agbp, XFS_AGI_FREECOUNT);
1163 down_read(&mp->m_peraglock);
1164 mp->m_perag[agno].pagi_freecount++;
1165 up_read(&mp->m_peraglock);
1166 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IFREE, 1);
1170 if (cur->bc_nlevels == 1) {
1173 if ((error = xfs_inobt_lookup_ge(cur, 0, 0, 0, &i)))
1176 if ((error = xfs_inobt_get_rec(cur,
1182 freecount += rec.ir_freecount;
1183 if ((error = xfs_btree_increment(cur, 0, &i)))
1187 ASSERT(freecount == be32_to_cpu(agi->agi_freecount) ||
1188 XFS_FORCED_SHUTDOWN(mp));
1191 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1195 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1200 * Return the location of the inode in imap, for mapping it into a buffer.
1204 xfs_mount_t *mp, /* file system mount structure */
1205 xfs_trans_t *tp, /* transaction pointer */
1206 xfs_ino_t ino, /* inode to locate */
1207 struct xfs_imap *imap, /* location map structure */
1208 uint flags) /* flags for inode btree lookup */
1210 xfs_agblock_t agbno; /* block number of inode in the alloc group */
1211 xfs_agino_t agino; /* inode number within alloc group */
1212 xfs_agnumber_t agno; /* allocation group number */
1213 int blks_per_cluster; /* num blocks per inode cluster */
1214 xfs_agblock_t chunk_agbno; /* first block in inode chunk */
1215 xfs_agblock_t cluster_agbno; /* first block in inode cluster */
1216 int error; /* error code */
1217 int offset; /* index of inode in its buffer */
1218 int offset_agbno; /* blks from chunk start to inode */
1220 ASSERT(ino != NULLFSINO);
1223 * Split up the inode number into its parts.
1225 agno = XFS_INO_TO_AGNO(mp, ino);
1226 agino = XFS_INO_TO_AGINO(mp, ino);
1227 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1228 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
1229 ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1231 /* no diagnostics for bulkstat, ino comes from userspace */
1232 if (flags & XFS_IMAP_BULKSTAT)
1233 return XFS_ERROR(EINVAL);
1234 if (agno >= mp->m_sb.sb_agcount) {
1235 xfs_fs_cmn_err(CE_ALERT, mp,
1236 "xfs_imap: agno (%d) >= "
1237 "mp->m_sb.sb_agcount (%d)",
1238 agno, mp->m_sb.sb_agcount);
1240 if (agbno >= mp->m_sb.sb_agblocks) {
1241 xfs_fs_cmn_err(CE_ALERT, mp,
1242 "xfs_imap: agbno (0x%llx) >= "
1243 "mp->m_sb.sb_agblocks (0x%lx)",
1244 (unsigned long long) agbno,
1245 (unsigned long) mp->m_sb.sb_agblocks);
1247 if (ino != XFS_AGINO_TO_INO(mp, agno, agino)) {
1248 xfs_fs_cmn_err(CE_ALERT, mp,
1249 "xfs_imap: ino (0x%llx) != "
1250 "XFS_AGINO_TO_INO(mp, agno, agino) "
1252 ino, XFS_AGINO_TO_INO(mp, agno, agino));
1256 return XFS_ERROR(EINVAL);
1260 * If the inode cluster size is the same as the blocksize or
1261 * smaller we get to the buffer by simple arithmetics.
1263 if (XFS_INODE_CLUSTER_SIZE(mp) <= mp->m_sb.sb_blocksize) {
1264 offset = XFS_INO_TO_OFFSET(mp, ino);
1265 ASSERT(offset < mp->m_sb.sb_inopblock);
1267 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno);
1268 imap->im_len = XFS_FSB_TO_BB(mp, 1);
1269 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
1273 blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_blocklog;
1276 * If we get a block number passed from bulkstat we can use it to
1277 * find the buffer easily.
1279 if (imap->im_blkno) {
1280 offset = XFS_INO_TO_OFFSET(mp, ino);
1281 ASSERT(offset < mp->m_sb.sb_inopblock);
1283 cluster_agbno = XFS_DADDR_TO_AGBNO(mp, imap->im_blkno);
1284 offset += (agbno - cluster_agbno) * mp->m_sb.sb_inopblock;
1286 imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
1287 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
1292 * If the inode chunks are aligned then use simple maths to
1293 * find the location. Otherwise we have to do a btree
1294 * lookup to find the location.
1296 if (mp->m_inoalign_mask) {
1297 offset_agbno = agbno & mp->m_inoalign_mask;
1298 chunk_agbno = agbno - offset_agbno;
1300 xfs_btree_cur_t *cur; /* inode btree cursor */
1301 xfs_agino_t chunk_agino; /* first agino in inode chunk */
1302 __int32_t chunk_cnt; /* count of free inodes in chunk */
1303 xfs_inofree_t chunk_free; /* mask of free inodes in chunk */
1304 xfs_buf_t *agbp; /* agi buffer */
1305 int i; /* temp state */
1307 down_read(&mp->m_peraglock);
1308 error = xfs_ialloc_read_agi(mp, tp, agno, &agbp);
1309 up_read(&mp->m_peraglock);
1311 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
1312 "xfs_ialloc_read_agi() returned "
1313 "error %d, agno %d",
1318 cur = xfs_inobt_init_cursor(mp, tp, agbp, agno);
1319 error = xfs_inobt_lookup_le(cur, agino, 0, 0, &i);
1321 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
1322 "xfs_inobt_lookup_le() failed");
1326 error = xfs_inobt_get_rec(cur, &chunk_agino, &chunk_cnt,
1329 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
1330 "xfs_inobt_get_rec() failed");
1335 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
1336 "xfs_inobt_get_rec() failed");
1338 error = XFS_ERROR(EINVAL);
1341 xfs_trans_brelse(tp, agbp);
1342 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1345 chunk_agbno = XFS_AGINO_TO_AGBNO(mp, chunk_agino);
1346 offset_agbno = agbno - chunk_agbno;
1349 ASSERT(agbno >= chunk_agbno);
1350 cluster_agbno = chunk_agbno +
1351 ((offset_agbno / blks_per_cluster) * blks_per_cluster);
1352 offset = ((agbno - cluster_agbno) * mp->m_sb.sb_inopblock) +
1353 XFS_INO_TO_OFFSET(mp, ino);
1355 imap->im_blkno = XFS_AGB_TO_DADDR(mp, agno, cluster_agbno);
1356 imap->im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
1357 imap->im_boffset = (ushort)(offset << mp->m_sb.sb_inodelog);
1360 * If the inode number maps to a block outside the bounds
1361 * of the file system then return NULL rather than calling
1362 * read_buf and panicing when we get an error from the
1365 if ((imap->im_blkno + imap->im_len) >
1366 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks)) {
1367 xfs_fs_cmn_err(CE_ALERT, mp, "xfs_imap: "
1368 "(imap->im_blkno (0x%llx) + imap->im_len (0x%llx)) > "
1369 " XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks) (0x%llx)",
1370 (unsigned long long) imap->im_blkno,
1371 (unsigned long long) imap->im_len,
1372 XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks));
1373 return XFS_ERROR(EINVAL);
1380 * Compute and fill in value of m_in_maxlevels.
1383 xfs_ialloc_compute_maxlevels(
1384 xfs_mount_t *mp) /* file system mount structure */
1392 maxleafents = (1LL << XFS_INO_AGINO_BITS(mp)) >>
1393 XFS_INODES_PER_CHUNK_LOG;
1394 minleafrecs = mp->m_alloc_mnr[0];
1395 minnoderecs = mp->m_alloc_mnr[1];
1396 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
1397 for (level = 1; maxblocks > 1; level++)
1398 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
1399 mp->m_in_maxlevels = level;
1403 * Log specified fields for the ag hdr (inode section)
1407 xfs_trans_t *tp, /* transaction pointer */
1408 xfs_buf_t *bp, /* allocation group header buffer */
1409 int fields) /* bitmask of fields to log */
1411 int first; /* first byte number */
1412 int last; /* last byte number */
1413 static const short offsets[] = { /* field starting offsets */
1414 /* keep in sync with bit definitions */
1415 offsetof(xfs_agi_t, agi_magicnum),
1416 offsetof(xfs_agi_t, agi_versionnum),
1417 offsetof(xfs_agi_t, agi_seqno),
1418 offsetof(xfs_agi_t, agi_length),
1419 offsetof(xfs_agi_t, agi_count),
1420 offsetof(xfs_agi_t, agi_root),
1421 offsetof(xfs_agi_t, agi_level),
1422 offsetof(xfs_agi_t, agi_freecount),
1423 offsetof(xfs_agi_t, agi_newino),
1424 offsetof(xfs_agi_t, agi_dirino),
1425 offsetof(xfs_agi_t, agi_unlinked),
1429 xfs_agi_t *agi; /* allocation group header */
1431 agi = XFS_BUF_TO_AGI(bp);
1432 ASSERT(be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC);
1435 * Compute byte offsets for the first and last fields.
1437 xfs_btree_offsets(fields, offsets, XFS_AGI_NUM_BITS, &first, &last);
1439 * Log the allocation group inode header buffer.
1441 xfs_trans_log_buf(tp, bp, first, last);
1446 xfs_check_agi_unlinked(
1447 struct xfs_agi *agi)
1451 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++)
1452 ASSERT(agi->agi_unlinked[i]);
1455 #define xfs_check_agi_unlinked(agi)
1459 * Read in the allocation group header (inode allocation section)
1463 struct xfs_mount *mp, /* file system mount structure */
1464 struct xfs_trans *tp, /* transaction pointer */
1465 xfs_agnumber_t agno, /* allocation group number */
1466 struct xfs_buf **bpp) /* allocation group hdr buf */
1468 struct xfs_agi *agi; /* allocation group header */
1469 int agi_ok; /* agi is consistent */
1472 ASSERT(agno != NULLAGNUMBER);
1474 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
1475 XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
1476 XFS_FSS_TO_BB(mp, 1), 0, bpp);
1480 ASSERT(*bpp && !XFS_BUF_GETERROR(*bpp));
1481 agi = XFS_BUF_TO_AGI(*bpp);
1484 * Validate the magic number of the agi block.
1486 agi_ok = be32_to_cpu(agi->agi_magicnum) == XFS_AGI_MAGIC &&
1487 XFS_AGI_GOOD_VERSION(be32_to_cpu(agi->agi_versionnum)) &&
1488 be32_to_cpu(agi->agi_seqno) == agno;
1489 if (unlikely(XFS_TEST_ERROR(!agi_ok, mp, XFS_ERRTAG_IALLOC_READ_AGI,
1490 XFS_RANDOM_IALLOC_READ_AGI))) {
1491 XFS_CORRUPTION_ERROR("xfs_read_agi", XFS_ERRLEVEL_LOW,
1493 xfs_trans_brelse(tp, *bpp);
1494 return XFS_ERROR(EFSCORRUPTED);
1497 XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_AGI, XFS_AGI_REF);
1499 xfs_check_agi_unlinked(agi);
1504 xfs_ialloc_read_agi(
1505 struct xfs_mount *mp, /* file system mount structure */
1506 struct xfs_trans *tp, /* transaction pointer */
1507 xfs_agnumber_t agno, /* allocation group number */
1508 struct xfs_buf **bpp) /* allocation group hdr buf */
1510 struct xfs_agi *agi; /* allocation group header */
1511 struct xfs_perag *pag; /* per allocation group data */
1514 error = xfs_read_agi(mp, tp, agno, bpp);
1518 agi = XFS_BUF_TO_AGI(*bpp);
1519 pag = &mp->m_perag[agno];
1521 if (!pag->pagi_init) {
1522 pag->pagi_freecount = be32_to_cpu(agi->agi_freecount);
1523 pag->pagi_count = be32_to_cpu(agi->agi_count);
1528 * It's possible for these to be out of sync if
1529 * we are in the middle of a forced shutdown.
1531 ASSERT(pag->pagi_freecount == be32_to_cpu(agi->agi_freecount) ||
1532 XFS_FORCED_SHUTDOWN(mp));
1537 * Read in the agi to initialise the per-ag data in the mount structure
1540 xfs_ialloc_pagi_init(
1541 xfs_mount_t *mp, /* file system mount structure */
1542 xfs_trans_t *tp, /* transaction pointer */
1543 xfs_agnumber_t agno) /* allocation group number */
1545 xfs_buf_t *bp = NULL;
1548 error = xfs_ialloc_read_agi(mp, tp, agno, &bp);
1552 xfs_trans_brelse(tp, bp);