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"
46 * Allocation group level functions.
49 xfs_ialloc_cluster_alignment(
50 xfs_alloc_arg_t
*args
)
52 if (xfs_sb_version_hasalign(&args
->mp
->m_sb
) &&
53 args
->mp
->m_sb
.sb_inoalignmt
>=
54 XFS_B_TO_FSBT(args
->mp
, XFS_INODE_CLUSTER_SIZE(args
->mp
)))
55 return args
->mp
->m_sb
.sb_inoalignmt
;
60 * Lookup a record by ino in the btree given by cur.
64 struct xfs_btree_cur
*cur
, /* btree cursor */
65 xfs_agino_t ino
, /* starting inode of chunk */
66 xfs_lookup_t dir
, /* <=, >=, == */
67 int *stat
) /* success/failure */
69 cur
->bc_rec
.i
.ir_startino
= ino
;
70 cur
->bc_rec
.i
.ir_freecount
= 0;
71 cur
->bc_rec
.i
.ir_free
= 0;
72 return xfs_btree_lookup(cur
, dir
, stat
);
76 * Update the record referred to by cur to the value given.
77 * This either works (return 0) or gets an EFSCORRUPTED error.
79 STATIC
int /* error */
81 struct xfs_btree_cur
*cur
, /* btree cursor */
82 xfs_inobt_rec_incore_t
*irec
) /* btree record */
84 union xfs_btree_rec rec
;
86 rec
.inobt
.ir_startino
= cpu_to_be32(irec
->ir_startino
);
87 rec
.inobt
.ir_freecount
= cpu_to_be32(irec
->ir_freecount
);
88 rec
.inobt
.ir_free
= cpu_to_be64(irec
->ir_free
);
89 return xfs_btree_update(cur
, &rec
);
93 * Get the data from the pointed-to record.
97 struct xfs_btree_cur
*cur
, /* btree cursor */
98 xfs_inobt_rec_incore_t
*irec
, /* btree record */
99 int *stat
) /* output: success/failure */
101 union xfs_btree_rec
*rec
;
104 error
= xfs_btree_get_rec(cur
, &rec
, stat
);
105 if (!error
&& *stat
== 1) {
106 irec
->ir_startino
= be32_to_cpu(rec
->inobt
.ir_startino
);
107 irec
->ir_freecount
= be32_to_cpu(rec
->inobt
.ir_freecount
);
108 irec
->ir_free
= be64_to_cpu(rec
->inobt
.ir_free
);
114 * Verify that the number of free inodes in the AGI is correct.
118 xfs_check_agi_freecount(
119 struct xfs_btree_cur
*cur
,
122 if (cur
->bc_nlevels
== 1) {
123 xfs_inobt_rec_incore_t rec
;
128 error
= xfs_inobt_lookup(cur
, 0, XFS_LOOKUP_GE
, &i
);
133 error
= xfs_inobt_get_rec(cur
, &rec
, &i
);
138 freecount
+= rec
.ir_freecount
;
139 error
= xfs_btree_increment(cur
, 0, &i
);
145 if (!XFS_FORCED_SHUTDOWN(cur
->bc_mp
))
146 ASSERT(freecount
== be32_to_cpu(agi
->agi_freecount
));
151 #define xfs_check_agi_freecount(cur, agi) 0
155 * Initialise a new set of inodes.
158 xfs_ialloc_inode_init(
159 struct xfs_mount
*mp
,
160 struct xfs_trans
*tp
,
163 xfs_agblock_t length
,
166 struct xfs_buf
*fbuf
;
167 struct xfs_dinode
*free
;
168 int blks_per_cluster
, nbufs
, ninodes
;
174 * Loop over the new block(s), filling in the inodes.
175 * For small block sizes, manipulate the inodes in buffers
176 * which are multiples of the blocks size.
178 if (mp
->m_sb
.sb_blocksize
>= XFS_INODE_CLUSTER_SIZE(mp
)) {
179 blks_per_cluster
= 1;
181 ninodes
= mp
->m_sb
.sb_inopblock
;
183 blks_per_cluster
= XFS_INODE_CLUSTER_SIZE(mp
) /
184 mp
->m_sb
.sb_blocksize
;
185 nbufs
= length
/ blks_per_cluster
;
186 ninodes
= blks_per_cluster
* mp
->m_sb
.sb_inopblock
;
190 * Figure out what version number to use in the inodes we create.
191 * If the superblock version has caught up to the one that supports
192 * the new inode format, then use the new inode version. Otherwise
193 * use the old version so that old kernels will continue to be
194 * able to use the file system.
196 if (xfs_sb_version_hasnlink(&mp
->m_sb
))
201 for (j
= 0; j
< nbufs
; j
++) {
205 d
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
+ (j
* blks_per_cluster
));
206 fbuf
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
,
207 mp
->m_bsize
* blks_per_cluster
,
210 ASSERT(!XFS_BUF_GETERROR(fbuf
));
213 * Initialize all inodes in this buffer and then log them.
215 * XXX: It would be much better if we had just one transaction
216 * to log a whole cluster of inodes instead of all the
217 * individual transactions causing a lot of log traffic.
219 xfs_biozero(fbuf
, 0, ninodes
<< mp
->m_sb
.sb_inodelog
);
220 for (i
= 0; i
< ninodes
; i
++) {
221 int ioffset
= i
<< mp
->m_sb
.sb_inodelog
;
222 uint isize
= sizeof(struct xfs_dinode
);
224 free
= xfs_make_iptr(mp
, fbuf
, i
);
225 free
->di_magic
= cpu_to_be16(XFS_DINODE_MAGIC
);
226 free
->di_version
= version
;
227 free
->di_gen
= cpu_to_be32(gen
);
228 free
->di_next_unlinked
= cpu_to_be32(NULLAGINO
);
229 xfs_trans_log_buf(tp
, fbuf
, ioffset
, ioffset
+ isize
- 1);
231 xfs_trans_inode_alloc_buf(tp
, fbuf
);
236 * Allocate new inodes in the allocation group specified by agbp.
237 * Return 0 for success, else error code.
239 STATIC
int /* error code or 0 */
241 xfs_trans_t
*tp
, /* transaction pointer */
242 xfs_buf_t
*agbp
, /* alloc group buffer */
245 xfs_agi_t
*agi
; /* allocation group header */
246 xfs_alloc_arg_t args
; /* allocation argument structure */
247 xfs_btree_cur_t
*cur
; /* inode btree cursor */
251 xfs_agino_t newino
; /* new first inode's number */
252 xfs_agino_t newlen
; /* new number of inodes */
253 xfs_agino_t thisino
; /* current inode number, for loop */
254 int isaligned
= 0; /* inode allocation at stripe unit */
256 struct xfs_perag
*pag
;
259 args
.mp
= tp
->t_mountp
;
262 * Locking will ensure that we don't have two callers in here
265 newlen
= XFS_IALLOC_INODES(args
.mp
);
266 if (args
.mp
->m_maxicount
&&
267 args
.mp
->m_sb
.sb_icount
+ newlen
> args
.mp
->m_maxicount
)
268 return XFS_ERROR(ENOSPC
);
269 args
.minlen
= args
.maxlen
= XFS_IALLOC_BLOCKS(args
.mp
);
271 * First try to allocate inodes contiguous with the last-allocated
272 * chunk of inodes. If the filesystem is striped, this will fill
273 * an entire stripe unit with inodes.
275 agi
= XFS_BUF_TO_AGI(agbp
);
276 newino
= be32_to_cpu(agi
->agi_newino
);
277 agno
= be32_to_cpu(agi
->agi_seqno
);
278 args
.agbno
= XFS_AGINO_TO_AGBNO(args
.mp
, newino
) +
279 XFS_IALLOC_BLOCKS(args
.mp
);
280 if (likely(newino
!= NULLAGINO
&&
281 (args
.agbno
< be32_to_cpu(agi
->agi_length
)))) {
282 args
.fsbno
= XFS_AGB_TO_FSB(args
.mp
, agno
, args
.agbno
);
283 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
284 args
.mod
= args
.total
= args
.wasdel
= args
.isfl
=
285 args
.userdata
= args
.minalignslop
= 0;
289 * We need to take into account alignment here to ensure that
290 * we don't modify the free list if we fail to have an exact
291 * block. If we don't have an exact match, and every oher
292 * attempt allocation attempt fails, we'll end up cancelling
293 * a dirty transaction and shutting down.
295 * For an exact allocation, alignment must be 1,
296 * however we need to take cluster alignment into account when
297 * fixing up the freelist. Use the minalignslop field to
298 * indicate that extra blocks might be required for alignment,
299 * but not to use them in the actual exact allocation.
302 args
.minalignslop
= xfs_ialloc_cluster_alignment(&args
) - 1;
304 /* Allow space for the inode btree to split. */
305 args
.minleft
= args
.mp
->m_in_maxlevels
- 1;
306 if ((error
= xfs_alloc_vextent(&args
)))
309 args
.fsbno
= NULLFSBLOCK
;
311 if (unlikely(args
.fsbno
== NULLFSBLOCK
)) {
313 * Set the alignment for the allocation.
314 * If stripe alignment is turned on then align at stripe unit
316 * If the cluster size is smaller than a filesystem block
317 * then we're doing I/O for inodes in filesystem block size
318 * pieces, so don't need alignment anyway.
321 if (args
.mp
->m_sinoalign
) {
322 ASSERT(!(args
.mp
->m_flags
& XFS_MOUNT_NOALIGN
));
323 args
.alignment
= args
.mp
->m_dalign
;
326 args
.alignment
= xfs_ialloc_cluster_alignment(&args
);
328 * Need to figure out where to allocate the inode blocks.
329 * Ideally they should be spaced out through the a.g.
330 * For now, just allocate blocks up front.
332 args
.agbno
= be32_to_cpu(agi
->agi_root
);
333 args
.fsbno
= XFS_AGB_TO_FSB(args
.mp
, agno
, args
.agbno
);
335 * Allocate a fixed-size extent of inodes.
337 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
338 args
.mod
= args
.total
= args
.wasdel
= args
.isfl
=
339 args
.userdata
= args
.minalignslop
= 0;
342 * Allow space for the inode btree to split.
344 args
.minleft
= args
.mp
->m_in_maxlevels
- 1;
345 if ((error
= xfs_alloc_vextent(&args
)))
350 * If stripe alignment is turned on, then try again with cluster
353 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
354 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
355 args
.agbno
= be32_to_cpu(agi
->agi_root
);
356 args
.fsbno
= XFS_AGB_TO_FSB(args
.mp
, agno
, args
.agbno
);
357 args
.alignment
= xfs_ialloc_cluster_alignment(&args
);
358 if ((error
= xfs_alloc_vextent(&args
)))
362 if (args
.fsbno
== NULLFSBLOCK
) {
366 ASSERT(args
.len
== args
.minlen
);
369 * Stamp and write the inode buffers.
371 * Seed the new inode cluster with a random generation number. This
372 * prevents short-term reuse of generation numbers if a chunk is
373 * freed and then immediately reallocated. We use random numbers
374 * rather than a linear progression to prevent the next generation
375 * number from being easily guessable.
377 xfs_ialloc_inode_init(args
.mp
, tp
, agno
, args
.agbno
, args
.len
,
381 * Convert the results.
383 newino
= XFS_OFFBNO_TO_AGINO(args
.mp
, args
.agbno
, 0);
384 be32_add_cpu(&agi
->agi_count
, newlen
);
385 be32_add_cpu(&agi
->agi_freecount
, newlen
);
386 pag
= xfs_perag_get(args
.mp
, agno
);
387 pag
->pagi_freecount
+= newlen
;
389 agi
->agi_newino
= cpu_to_be32(newino
);
392 * Insert records describing the new inode chunk into the btree.
394 cur
= xfs_inobt_init_cursor(args
.mp
, tp
, agbp
, agno
);
395 for (thisino
= newino
;
396 thisino
< newino
+ newlen
;
397 thisino
+= XFS_INODES_PER_CHUNK
) {
398 cur
->bc_rec
.i
.ir_startino
= thisino
;
399 cur
->bc_rec
.i
.ir_freecount
= XFS_INODES_PER_CHUNK
;
400 cur
->bc_rec
.i
.ir_free
= XFS_INOBT_ALL_FREE
;
401 error
= xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, &i
);
403 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
407 error
= xfs_btree_insert(cur
, &i
);
409 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
414 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
416 * Log allocation group header fields
418 xfs_ialloc_log_agi(tp
, agbp
,
419 XFS_AGI_COUNT
| XFS_AGI_FREECOUNT
| XFS_AGI_NEWINO
);
421 * Modify/log superblock values for inode count and inode free count.
423 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_ICOUNT
, (long)newlen
);
424 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_IFREE
, (long)newlen
);
429 STATIC xfs_agnumber_t
435 spin_lock(&mp
->m_agirotor_lock
);
436 agno
= mp
->m_agirotor
;
437 if (++mp
->m_agirotor
== mp
->m_maxagi
)
439 spin_unlock(&mp
->m_agirotor_lock
);
445 * Select an allocation group to look for a free inode in, based on the parent
446 * inode and then mode. Return the allocation group buffer.
448 STATIC xfs_buf_t
* /* allocation group buffer */
449 xfs_ialloc_ag_select(
450 xfs_trans_t
*tp
, /* transaction pointer */
451 xfs_ino_t parent
, /* parent directory inode number */
452 mode_t mode
, /* bits set to indicate file type */
453 int okalloc
) /* ok to allocate more space */
455 xfs_buf_t
*agbp
; /* allocation group header buffer */
456 xfs_agnumber_t agcount
; /* number of ag's in the filesystem */
457 xfs_agnumber_t agno
; /* current ag number */
458 int flags
; /* alloc buffer locking flags */
459 xfs_extlen_t ineed
; /* blocks needed for inode allocation */
460 xfs_extlen_t longest
= 0; /* longest extent available */
461 xfs_mount_t
*mp
; /* mount point structure */
462 int needspace
; /* file mode implies space allocated */
463 xfs_perag_t
*pag
; /* per allocation group data */
464 xfs_agnumber_t pagno
; /* parent (starting) ag number */
467 * Files of these types need at least one block if length > 0
468 * (and they won't fit in the inode, but that's hard to figure out).
470 needspace
= S_ISDIR(mode
) || S_ISREG(mode
) || S_ISLNK(mode
);
472 agcount
= mp
->m_maxagi
;
474 pagno
= xfs_ialloc_next_ag(mp
);
476 pagno
= XFS_INO_TO_AGNO(mp
, parent
);
477 if (pagno
>= agcount
)
480 ASSERT(pagno
< agcount
);
482 * Loop through allocation groups, looking for one with a little
483 * free space in it. Note we don't look for free inodes, exactly.
484 * Instead, we include whether there is a need to allocate inodes
485 * to mean that blocks must be allocated for them,
486 * if none are currently free.
489 flags
= XFS_ALLOC_FLAG_TRYLOCK
;
491 pag
= xfs_perag_get(mp
, agno
);
492 if (!pag
->pagi_init
) {
493 if (xfs_ialloc_read_agi(mp
, tp
, agno
, &agbp
)) {
500 if (!pag
->pagi_inodeok
) {
501 xfs_ialloc_next_ag(mp
);
506 * Is there enough free space for the file plus a block
507 * of inodes (if we need to allocate some)?
509 ineed
= pag
->pagi_freecount
? 0 : XFS_IALLOC_BLOCKS(mp
);
510 if (ineed
&& !pag
->pagf_init
) {
512 xfs_ialloc_read_agi(mp
, tp
, agno
, &agbp
)) {
516 (void)xfs_alloc_pagf_init(mp
, tp
, agno
, flags
);
518 if (!ineed
|| pag
->pagf_init
) {
519 if (ineed
&& !(longest
= pag
->pagf_longest
))
520 longest
= pag
->pagf_flcount
> 0;
522 (pag
->pagf_freeblks
>= needspace
+ ineed
&&
526 xfs_ialloc_read_agi(mp
, tp
, agno
, &agbp
)) {
536 xfs_trans_brelse(tp
, agbp
);
540 * No point in iterating over the rest, if we're shutting
543 if (XFS_FORCED_SHUTDOWN(mp
))
557 * Try to retrieve the next record to the left/right from the current one.
561 struct xfs_btree_cur
*cur
,
562 xfs_inobt_rec_incore_t
*rec
,
570 error
= xfs_btree_decrement(cur
, 0, &i
);
572 error
= xfs_btree_increment(cur
, 0, &i
);
578 error
= xfs_inobt_get_rec(cur
, rec
, &i
);
581 XFS_WANT_CORRUPTED_RETURN(i
== 1);
589 struct xfs_btree_cur
*cur
,
591 xfs_inobt_rec_incore_t
*rec
,
598 error
= xfs_inobt_lookup(cur
, agino
, XFS_LOOKUP_EQ
, &i
);
603 error
= xfs_inobt_get_rec(cur
, rec
, &i
);
606 XFS_WANT_CORRUPTED_RETURN(i
== 1);
613 * Visible inode allocation functions.
617 * Allocate an inode on disk.
618 * Mode is used to tell whether the new inode will need space, and whether
621 * The arguments IO_agbp and alloc_done are defined to work within
622 * the constraint of one allocation per transaction.
623 * xfs_dialloc() is designed to be called twice if it has to do an
624 * allocation to make more free inodes. On the first call,
625 * IO_agbp should be set to NULL. If an inode is available,
626 * i.e., xfs_dialloc() did not need to do an allocation, an inode
627 * number is returned. In this case, IO_agbp would be set to the
628 * current ag_buf and alloc_done set to false.
629 * If an allocation needed to be done, xfs_dialloc would return
630 * the current ag_buf in IO_agbp and set alloc_done to true.
631 * The caller should then commit the current transaction, allocate a new
632 * transaction, and call xfs_dialloc() again, passing in the previous
633 * value of IO_agbp. IO_agbp should be held across the transactions.
634 * Since the agbp is locked across the two calls, the second call is
635 * guaranteed to have a free inode available.
637 * Once we successfully pick an inode its number is returned and the
638 * on-disk data structures are updated. The inode itself is not read
639 * in, since doing so would break ordering constraints with xfs_reclaim.
643 xfs_trans_t
*tp
, /* transaction pointer */
644 xfs_ino_t parent
, /* parent inode (directory) */
645 mode_t mode
, /* mode bits for new inode */
646 int okalloc
, /* ok to allocate more space */
647 xfs_buf_t
**IO_agbp
, /* in/out ag header's buffer */
648 boolean_t
*alloc_done
, /* true if we needed to replenish
650 xfs_ino_t
*inop
) /* inode number allocated */
652 xfs_agnumber_t agcount
; /* number of allocation groups */
653 xfs_buf_t
*agbp
; /* allocation group header's buffer */
654 xfs_agnumber_t agno
; /* allocation group number */
655 xfs_agi_t
*agi
; /* allocation group header structure */
656 xfs_btree_cur_t
*cur
; /* inode allocation btree cursor */
657 int error
; /* error return value */
658 int i
; /* result code */
659 int ialloced
; /* inode allocation status */
660 int noroom
= 0; /* no space for inode blk allocation */
661 xfs_ino_t ino
; /* fs-relative inode to be returned */
663 int j
; /* result code */
664 xfs_mount_t
*mp
; /* file system mount structure */
665 int offset
; /* index of inode in chunk */
666 xfs_agino_t pagino
; /* parent's AG relative inode # */
667 xfs_agnumber_t pagno
; /* parent's AG number */
668 xfs_inobt_rec_incore_t rec
; /* inode allocation record */
669 xfs_agnumber_t tagno
; /* testing allocation group number */
670 xfs_btree_cur_t
*tcur
; /* temp cursor */
671 xfs_inobt_rec_incore_t trec
; /* temp inode allocation record */
672 struct xfs_perag
*pag
;
675 if (*IO_agbp
== NULL
) {
677 * We do not have an agbp, so select an initial allocation
678 * group for inode allocation.
680 agbp
= xfs_ialloc_ag_select(tp
, parent
, mode
, okalloc
);
682 * Couldn't find an allocation group satisfying the
689 agi
= XFS_BUF_TO_AGI(agbp
);
690 ASSERT(be32_to_cpu(agi
->agi_magicnum
) == XFS_AGI_MAGIC
);
693 * Continue where we left off before. In this case, we
694 * know that the allocation group has free inodes.
697 agi
= XFS_BUF_TO_AGI(agbp
);
698 ASSERT(be32_to_cpu(agi
->agi_magicnum
) == XFS_AGI_MAGIC
);
699 ASSERT(be32_to_cpu(agi
->agi_freecount
) > 0);
702 agcount
= mp
->m_sb
.sb_agcount
;
703 agno
= be32_to_cpu(agi
->agi_seqno
);
705 pagno
= XFS_INO_TO_AGNO(mp
, parent
);
706 pagino
= XFS_INO_TO_AGINO(mp
, parent
);
709 * If we have already hit the ceiling of inode blocks then clear
710 * okalloc so we scan all available agi structures for a free
714 if (mp
->m_maxicount
&&
715 mp
->m_sb
.sb_icount
+ XFS_IALLOC_INODES(mp
) > mp
->m_maxicount
) {
721 * Loop until we find an allocation group that either has free inodes
722 * or in which we can allocate some inodes. Iterate through the
723 * allocation groups upward, wrapping at the end.
725 *alloc_done
= B_FALSE
;
726 while (!agi
->agi_freecount
) {
728 * Don't do anything if we're not supposed to allocate
729 * any blocks, just go on to the next ag.
733 * Try to allocate some new inodes in the allocation
736 if ((error
= xfs_ialloc_ag_alloc(tp
, agbp
, &ialloced
))) {
737 xfs_trans_brelse(tp
, agbp
);
738 if (error
== ENOSPC
) {
746 * We successfully allocated some inodes, return
747 * the current context to the caller so that it
748 * can commit the current transaction and call
749 * us again where we left off.
751 ASSERT(be32_to_cpu(agi
->agi_freecount
) > 0);
752 *alloc_done
= B_TRUE
;
759 * If it failed, give up on this ag.
761 xfs_trans_brelse(tp
, agbp
);
763 * Go on to the next ag: get its ag header.
766 if (++tagno
== agcount
)
770 return noroom
? ENOSPC
: 0;
772 pag
= xfs_perag_get(mp
, tagno
);
773 if (pag
->pagi_inodeok
== 0) {
777 error
= xfs_ialloc_read_agi(mp
, tp
, tagno
, &agbp
);
781 agi
= XFS_BUF_TO_AGI(agbp
);
782 ASSERT(be32_to_cpu(agi
->agi_magicnum
) == XFS_AGI_MAGIC
);
785 * Here with an allocation group that has a free inode.
786 * Reset agno since we may have chosen a new ag in the
791 pag
= xfs_perag_get(mp
, agno
);
794 cur
= xfs_inobt_init_cursor(mp
, tp
, agbp
, be32_to_cpu(agi
->agi_seqno
));
796 * If pagino is 0 (this is the root inode allocation) use newino.
797 * This must work because we've just allocated some.
800 pagino
= be32_to_cpu(agi
->agi_newino
);
802 error
= xfs_check_agi_freecount(cur
, agi
);
807 * If in the same AG as the parent, try to get near the parent.
810 int doneleft
; /* done, to the left */
811 int doneright
; /* done, to the right */
812 int searchdistance
= 10;
814 error
= xfs_inobt_lookup(cur
, pagino
, XFS_LOOKUP_LE
, &i
);
817 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
819 error
= xfs_inobt_get_rec(cur
, &rec
, &j
);
822 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
824 if (rec
.ir_freecount
> 0) {
826 * Found a free inode in the same chunk
827 * as the parent, done.
834 * In the same AG as parent, but parent's chunk is full.
837 /* duplicate the cursor, search left & right simultaneously */
838 error
= xfs_btree_dup_cursor(cur
, &tcur
);
843 * Skip to last blocks looked up if same parent inode.
845 if (pagino
!= NULLAGINO
&&
846 pag
->pagl_pagino
== pagino
&&
847 pag
->pagl_leftrec
!= NULLAGINO
&&
848 pag
->pagl_rightrec
!= NULLAGINO
) {
849 error
= xfs_ialloc_get_rec(tcur
, pag
->pagl_leftrec
,
850 &trec
, &doneleft
, 1);
854 error
= xfs_ialloc_get_rec(cur
, pag
->pagl_rightrec
,
855 &rec
, &doneright
, 0);
859 /* search left with tcur, back up 1 record */
860 error
= xfs_ialloc_next_rec(tcur
, &trec
, &doneleft
, 1);
864 /* search right with cur, go forward 1 record. */
865 error
= xfs_ialloc_next_rec(cur
, &rec
, &doneright
, 0);
871 * Loop until we find an inode chunk with a free inode.
873 while (!doneleft
|| !doneright
) {
874 int useleft
; /* using left inode chunk this time */
876 if (!--searchdistance
) {
878 * Not in range - save last search
879 * location and allocate a new inode
881 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
882 pag
->pagl_leftrec
= trec
.ir_startino
;
883 pag
->pagl_rightrec
= rec
.ir_startino
;
884 pag
->pagl_pagino
= pagino
;
888 /* figure out the closer block if both are valid. */
889 if (!doneleft
&& !doneright
) {
891 (trec
.ir_startino
+ XFS_INODES_PER_CHUNK
- 1) <
892 rec
.ir_startino
- pagino
;
897 /* free inodes to the left? */
898 if (useleft
&& trec
.ir_freecount
) {
900 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
903 pag
->pagl_leftrec
= trec
.ir_startino
;
904 pag
->pagl_rightrec
= rec
.ir_startino
;
905 pag
->pagl_pagino
= pagino
;
909 /* free inodes to the right? */
910 if (!useleft
&& rec
.ir_freecount
) {
911 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
913 pag
->pagl_leftrec
= trec
.ir_startino
;
914 pag
->pagl_rightrec
= rec
.ir_startino
;
915 pag
->pagl_pagino
= pagino
;
919 /* get next record to check */
921 error
= xfs_ialloc_next_rec(tcur
, &trec
,
924 error
= xfs_ialloc_next_rec(cur
, &rec
,
932 * We've reached the end of the btree. because
933 * we are only searching a small chunk of the
934 * btree each search, there is obviously free
935 * inodes closer to the parent inode than we
936 * are now. restart the search again.
938 pag
->pagl_pagino
= NULLAGINO
;
939 pag
->pagl_leftrec
= NULLAGINO
;
940 pag
->pagl_rightrec
= NULLAGINO
;
941 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
942 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
947 * In a different AG from the parent.
948 * See if the most recently allocated block has any free.
951 if (be32_to_cpu(agi
->agi_newino
) != NULLAGINO
) {
952 error
= xfs_inobt_lookup(cur
, be32_to_cpu(agi
->agi_newino
),
958 error
= xfs_inobt_get_rec(cur
, &rec
, &j
);
962 if (j
== 1 && rec
.ir_freecount
> 0) {
964 * The last chunk allocated in the group
965 * still has a free inode.
973 * None left in the last group, search the whole AG
975 error
= xfs_inobt_lookup(cur
, 0, XFS_LOOKUP_GE
, &i
);
978 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
981 error
= xfs_inobt_get_rec(cur
, &rec
, &i
);
984 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
985 if (rec
.ir_freecount
> 0)
987 error
= xfs_btree_increment(cur
, 0, &i
);
990 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
994 offset
= xfs_ialloc_find_free(&rec
.ir_free
);
996 ASSERT(offset
< XFS_INODES_PER_CHUNK
);
997 ASSERT((XFS_AGINO_TO_OFFSET(mp
, rec
.ir_startino
) %
998 XFS_INODES_PER_CHUNK
) == 0);
999 ino
= XFS_AGINO_TO_INO(mp
, agno
, rec
.ir_startino
+ offset
);
1000 rec
.ir_free
&= ~XFS_INOBT_MASK(offset
);
1002 error
= xfs_inobt_update(cur
, &rec
);
1005 be32_add_cpu(&agi
->agi_freecount
, -1);
1006 xfs_ialloc_log_agi(tp
, agbp
, XFS_AGI_FREECOUNT
);
1007 pag
->pagi_freecount
--;
1009 error
= xfs_check_agi_freecount(cur
, agi
);
1013 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1014 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_IFREE
, -1);
1019 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
1021 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1027 * Free disk inode. Carefully avoids touching the incore inode, all
1028 * manipulations incore are the caller's responsibility.
1029 * The on-disk inode is not changed by this operation, only the
1030 * btree (free inode mask) is changed.
1034 xfs_trans_t
*tp
, /* transaction pointer */
1035 xfs_ino_t inode
, /* inode to be freed */
1036 xfs_bmap_free_t
*flist
, /* extents to free */
1037 int *delete, /* set if inode cluster was deleted */
1038 xfs_ino_t
*first_ino
) /* first inode in deleted cluster */
1041 xfs_agblock_t agbno
; /* block number containing inode */
1042 xfs_buf_t
*agbp
; /* buffer containing allocation group header */
1043 xfs_agino_t agino
; /* inode number relative to allocation group */
1044 xfs_agnumber_t agno
; /* allocation group number */
1045 xfs_agi_t
*agi
; /* allocation group header */
1046 xfs_btree_cur_t
*cur
; /* inode btree cursor */
1047 int error
; /* error return value */
1048 int i
; /* result code */
1049 int ilen
; /* inodes in an inode cluster */
1050 xfs_mount_t
*mp
; /* mount structure for filesystem */
1051 int off
; /* offset of inode in inode chunk */
1052 xfs_inobt_rec_incore_t rec
; /* btree record */
1053 struct xfs_perag
*pag
;
1058 * Break up inode number into its components.
1060 agno
= XFS_INO_TO_AGNO(mp
, inode
);
1061 if (agno
>= mp
->m_sb
.sb_agcount
) {
1063 "xfs_difree: agno >= mp->m_sb.sb_agcount (%d >= %d) on %s. Returning EINVAL.",
1064 agno
, mp
->m_sb
.sb_agcount
, mp
->m_fsname
);
1066 return XFS_ERROR(EINVAL
);
1068 agino
= XFS_INO_TO_AGINO(mp
, inode
);
1069 if (inode
!= XFS_AGINO_TO_INO(mp
, agno
, agino
)) {
1071 "xfs_difree: inode != XFS_AGINO_TO_INO() "
1072 "(%llu != %llu) on %s. Returning EINVAL.",
1073 (unsigned long long)inode
,
1074 (unsigned long long)XFS_AGINO_TO_INO(mp
, agno
, agino
),
1077 return XFS_ERROR(EINVAL
);
1079 agbno
= XFS_AGINO_TO_AGBNO(mp
, agino
);
1080 if (agbno
>= mp
->m_sb
.sb_agblocks
) {
1082 "xfs_difree: agbno >= mp->m_sb.sb_agblocks (%d >= %d) on %s. Returning EINVAL.",
1083 agbno
, mp
->m_sb
.sb_agblocks
, mp
->m_fsname
);
1085 return XFS_ERROR(EINVAL
);
1088 * Get the allocation group header.
1090 error
= xfs_ialloc_read_agi(mp
, tp
, agno
, &agbp
);
1093 "xfs_difree: xfs_ialloc_read_agi() returned an error %d on %s. Returning error.",
1094 error
, mp
->m_fsname
);
1097 agi
= XFS_BUF_TO_AGI(agbp
);
1098 ASSERT(be32_to_cpu(agi
->agi_magicnum
) == XFS_AGI_MAGIC
);
1099 ASSERT(agbno
< be32_to_cpu(agi
->agi_length
));
1101 * Initialize the cursor.
1103 cur
= xfs_inobt_init_cursor(mp
, tp
, agbp
, agno
);
1105 error
= xfs_check_agi_freecount(cur
, agi
);
1110 * Look for the entry describing this inode.
1112 if ((error
= xfs_inobt_lookup(cur
, agino
, XFS_LOOKUP_LE
, &i
))) {
1114 "xfs_difree: xfs_inobt_lookup returned() an error %d on %s. Returning error.",
1115 error
, mp
->m_fsname
);
1118 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1119 error
= xfs_inobt_get_rec(cur
, &rec
, &i
);
1122 "xfs_difree: xfs_inobt_get_rec() returned an error %d on %s. Returning error.",
1123 error
, mp
->m_fsname
);
1126 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1128 * Get the offset in the inode chunk.
1130 off
= agino
- rec
.ir_startino
;
1131 ASSERT(off
>= 0 && off
< XFS_INODES_PER_CHUNK
);
1132 ASSERT(!(rec
.ir_free
& XFS_INOBT_MASK(off
)));
1134 * Mark the inode free & increment the count.
1136 rec
.ir_free
|= XFS_INOBT_MASK(off
);
1140 * When an inode cluster is free, it becomes eligible for removal
1142 if (!(mp
->m_flags
& XFS_MOUNT_IKEEP
) &&
1143 (rec
.ir_freecount
== XFS_IALLOC_INODES(mp
))) {
1146 *first_ino
= XFS_AGINO_TO_INO(mp
, agno
, rec
.ir_startino
);
1149 * Remove the inode cluster from the AGI B+Tree, adjust the
1150 * AGI and Superblock inode counts, and mark the disk space
1151 * to be freed when the transaction is committed.
1153 ilen
= XFS_IALLOC_INODES(mp
);
1154 be32_add_cpu(&agi
->agi_count
, -ilen
);
1155 be32_add_cpu(&agi
->agi_freecount
, -(ilen
- 1));
1156 xfs_ialloc_log_agi(tp
, agbp
, XFS_AGI_COUNT
| XFS_AGI_FREECOUNT
);
1157 pag
= xfs_perag_get(mp
, agno
);
1158 pag
->pagi_freecount
-= ilen
- 1;
1160 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_ICOUNT
, -ilen
);
1161 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_IFREE
, -(ilen
- 1));
1163 if ((error
= xfs_btree_delete(cur
, &i
))) {
1164 cmn_err(CE_WARN
, "xfs_difree: xfs_btree_delete returned an error %d on %s.\n",
1165 error
, mp
->m_fsname
);
1169 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp
,
1170 agno
, XFS_INO_TO_AGBNO(mp
,rec
.ir_startino
)),
1171 XFS_IALLOC_BLOCKS(mp
), flist
, mp
);
1175 error
= xfs_inobt_update(cur
, &rec
);
1178 "xfs_difree: xfs_inobt_update returned an error %d on %s.",
1179 error
, mp
->m_fsname
);
1184 * Change the inode free counts and log the ag/sb changes.
1186 be32_add_cpu(&agi
->agi_freecount
, 1);
1187 xfs_ialloc_log_agi(tp
, agbp
, XFS_AGI_FREECOUNT
);
1188 pag
= xfs_perag_get(mp
, agno
);
1189 pag
->pagi_freecount
++;
1191 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_IFREE
, 1);
1194 error
= xfs_check_agi_freecount(cur
, agi
);
1198 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1202 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1207 * Return the location of the inode in imap, for mapping it into a buffer.
1211 xfs_mount_t
*mp
, /* file system mount structure */
1212 xfs_trans_t
*tp
, /* transaction pointer */
1213 xfs_ino_t ino
, /* inode to locate */
1214 struct xfs_imap
*imap
, /* location map structure */
1215 uint flags
) /* flags for inode btree lookup */
1217 xfs_agblock_t agbno
; /* block number of inode in the alloc group */
1218 xfs_agino_t agino
; /* inode number within alloc group */
1219 xfs_agnumber_t agno
; /* allocation group number */
1220 int blks_per_cluster
; /* num blocks per inode cluster */
1221 xfs_agblock_t chunk_agbno
; /* first block in inode chunk */
1222 xfs_agblock_t cluster_agbno
; /* first block in inode cluster */
1223 int error
; /* error code */
1224 int offset
; /* index of inode in its buffer */
1225 int offset_agbno
; /* blks from chunk start to inode */
1227 ASSERT(ino
!= NULLFSINO
);
1230 * Split up the inode number into its parts.
1232 agno
= XFS_INO_TO_AGNO(mp
, ino
);
1233 agino
= XFS_INO_TO_AGINO(mp
, ino
);
1234 agbno
= XFS_AGINO_TO_AGBNO(mp
, agino
);
1235 if (agno
>= mp
->m_sb
.sb_agcount
|| agbno
>= mp
->m_sb
.sb_agblocks
||
1236 ino
!= XFS_AGINO_TO_INO(mp
, agno
, agino
)) {
1238 /* no diagnostics for bulkstat, ino comes from userspace */
1239 if (flags
& XFS_IGET_BULKSTAT
)
1240 return XFS_ERROR(EINVAL
);
1241 if (agno
>= mp
->m_sb
.sb_agcount
) {
1242 xfs_fs_cmn_err(CE_ALERT
, mp
,
1243 "xfs_imap: agno (%d) >= "
1244 "mp->m_sb.sb_agcount (%d)",
1245 agno
, mp
->m_sb
.sb_agcount
);
1247 if (agbno
>= mp
->m_sb
.sb_agblocks
) {
1248 xfs_fs_cmn_err(CE_ALERT
, mp
,
1249 "xfs_imap: agbno (0x%llx) >= "
1250 "mp->m_sb.sb_agblocks (0x%lx)",
1251 (unsigned long long) agbno
,
1252 (unsigned long) mp
->m_sb
.sb_agblocks
);
1254 if (ino
!= XFS_AGINO_TO_INO(mp
, agno
, agino
)) {
1255 xfs_fs_cmn_err(CE_ALERT
, mp
,
1256 "xfs_imap: ino (0x%llx) != "
1257 "XFS_AGINO_TO_INO(mp, agno, agino) "
1259 ino
, XFS_AGINO_TO_INO(mp
, agno
, agino
));
1263 return XFS_ERROR(EINVAL
);
1267 * If the inode cluster size is the same as the blocksize or
1268 * smaller we get to the buffer by simple arithmetics.
1270 if (XFS_INODE_CLUSTER_SIZE(mp
) <= mp
->m_sb
.sb_blocksize
) {
1271 offset
= XFS_INO_TO_OFFSET(mp
, ino
);
1272 ASSERT(offset
< mp
->m_sb
.sb_inopblock
);
1274 imap
->im_blkno
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
);
1275 imap
->im_len
= XFS_FSB_TO_BB(mp
, 1);
1276 imap
->im_boffset
= (ushort
)(offset
<< mp
->m_sb
.sb_inodelog
);
1280 blks_per_cluster
= XFS_INODE_CLUSTER_SIZE(mp
) >> mp
->m_sb
.sb_blocklog
;
1283 * If we get a block number passed from bulkstat we can use it to
1284 * find the buffer easily.
1286 if (imap
->im_blkno
) {
1287 offset
= XFS_INO_TO_OFFSET(mp
, ino
);
1288 ASSERT(offset
< mp
->m_sb
.sb_inopblock
);
1290 cluster_agbno
= xfs_daddr_to_agbno(mp
, imap
->im_blkno
);
1291 offset
+= (agbno
- cluster_agbno
) * mp
->m_sb
.sb_inopblock
;
1293 imap
->im_len
= XFS_FSB_TO_BB(mp
, blks_per_cluster
);
1294 imap
->im_boffset
= (ushort
)(offset
<< mp
->m_sb
.sb_inodelog
);
1299 * If the inode chunks are aligned then use simple maths to
1300 * find the location. Otherwise we have to do a btree
1301 * lookup to find the location.
1303 if (mp
->m_inoalign_mask
) {
1304 offset_agbno
= agbno
& mp
->m_inoalign_mask
;
1305 chunk_agbno
= agbno
- offset_agbno
;
1307 xfs_btree_cur_t
*cur
; /* inode btree cursor */
1308 xfs_inobt_rec_incore_t chunk_rec
;
1309 xfs_buf_t
*agbp
; /* agi buffer */
1310 int i
; /* temp state */
1312 error
= xfs_ialloc_read_agi(mp
, tp
, agno
, &agbp
);
1314 xfs_fs_cmn_err(CE_ALERT
, mp
, "xfs_imap: "
1315 "xfs_ialloc_read_agi() returned "
1316 "error %d, agno %d",
1321 cur
= xfs_inobt_init_cursor(mp
, tp
, agbp
, agno
);
1322 error
= xfs_inobt_lookup(cur
, agino
, XFS_LOOKUP_LE
, &i
);
1324 xfs_fs_cmn_err(CE_ALERT
, mp
, "xfs_imap: "
1325 "xfs_inobt_lookup() failed");
1329 error
= xfs_inobt_get_rec(cur
, &chunk_rec
, &i
);
1331 xfs_fs_cmn_err(CE_ALERT
, mp
, "xfs_imap: "
1332 "xfs_inobt_get_rec() failed");
1337 xfs_fs_cmn_err(CE_ALERT
, mp
, "xfs_imap: "
1338 "xfs_inobt_get_rec() failed");
1340 error
= XFS_ERROR(EINVAL
);
1343 xfs_trans_brelse(tp
, agbp
);
1344 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1347 chunk_agbno
= XFS_AGINO_TO_AGBNO(mp
, chunk_rec
.ir_startino
);
1348 offset_agbno
= agbno
- chunk_agbno
;
1351 ASSERT(agbno
>= chunk_agbno
);
1352 cluster_agbno
= chunk_agbno
+
1353 ((offset_agbno
/ blks_per_cluster
) * blks_per_cluster
);
1354 offset
= ((agbno
- cluster_agbno
) * mp
->m_sb
.sb_inopblock
) +
1355 XFS_INO_TO_OFFSET(mp
, ino
);
1357 imap
->im_blkno
= XFS_AGB_TO_DADDR(mp
, agno
, cluster_agbno
);
1358 imap
->im_len
= XFS_FSB_TO_BB(mp
, blks_per_cluster
);
1359 imap
->im_boffset
= (ushort
)(offset
<< mp
->m_sb
.sb_inodelog
);
1362 * If the inode number maps to a block outside the bounds
1363 * of the file system then return NULL rather than calling
1364 * read_buf and panicing when we get an error from the
1367 if ((imap
->im_blkno
+ imap
->im_len
) >
1368 XFS_FSB_TO_BB(mp
, mp
->m_sb
.sb_dblocks
)) {
1369 xfs_fs_cmn_err(CE_ALERT
, mp
, "xfs_imap: "
1370 "(imap->im_blkno (0x%llx) + imap->im_len (0x%llx)) > "
1371 " XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks) (0x%llx)",
1372 (unsigned long long) imap
->im_blkno
,
1373 (unsigned long long) imap
->im_len
,
1374 XFS_FSB_TO_BB(mp
, mp
->m_sb
.sb_dblocks
));
1375 return XFS_ERROR(EINVAL
);
1381 * Compute and fill in value of m_in_maxlevels.
1384 xfs_ialloc_compute_maxlevels(
1385 xfs_mount_t
*mp
) /* file system mount structure */
1393 maxleafents
= (1LL << XFS_INO_AGINO_BITS(mp
)) >>
1394 XFS_INODES_PER_CHUNK_LOG
;
1395 minleafrecs
= mp
->m_alloc_mnr
[0];
1396 minnoderecs
= mp
->m_alloc_mnr
[1];
1397 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
1398 for (level
= 1; maxblocks
> 1; level
++)
1399 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
1400 mp
->m_in_maxlevels
= level
;
1404 * Log specified fields for the ag hdr (inode section)
1408 xfs_trans_t
*tp
, /* transaction pointer */
1409 xfs_buf_t
*bp
, /* allocation group header buffer */
1410 int fields
) /* bitmask of fields to log */
1412 int first
; /* first byte number */
1413 int last
; /* last byte number */
1414 static const short offsets
[] = { /* field starting offsets */
1415 /* keep in sync with bit definitions */
1416 offsetof(xfs_agi_t
, agi_magicnum
),
1417 offsetof(xfs_agi_t
, agi_versionnum
),
1418 offsetof(xfs_agi_t
, agi_seqno
),
1419 offsetof(xfs_agi_t
, agi_length
),
1420 offsetof(xfs_agi_t
, agi_count
),
1421 offsetof(xfs_agi_t
, agi_root
),
1422 offsetof(xfs_agi_t
, agi_level
),
1423 offsetof(xfs_agi_t
, agi_freecount
),
1424 offsetof(xfs_agi_t
, agi_newino
),
1425 offsetof(xfs_agi_t
, agi_dirino
),
1426 offsetof(xfs_agi_t
, agi_unlinked
),
1430 xfs_agi_t
*agi
; /* allocation group header */
1432 agi
= XFS_BUF_TO_AGI(bp
);
1433 ASSERT(be32_to_cpu(agi
->agi_magicnum
) == XFS_AGI_MAGIC
);
1436 * Compute byte offsets for the first and last fields.
1438 xfs_btree_offsets(fields
, offsets
, XFS_AGI_NUM_BITS
, &first
, &last
);
1440 * Log the allocation group inode header buffer.
1442 xfs_trans_log_buf(tp
, bp
, first
, last
);
1447 xfs_check_agi_unlinked(
1448 struct xfs_agi
*agi
)
1452 for (i
= 0; i
< XFS_AGI_UNLINKED_BUCKETS
; i
++)
1453 ASSERT(agi
->agi_unlinked
[i
]);
1456 #define xfs_check_agi_unlinked(agi)
1460 * Read in the allocation group header (inode allocation section)
1464 struct xfs_mount
*mp
, /* file system mount structure */
1465 struct xfs_trans
*tp
, /* transaction pointer */
1466 xfs_agnumber_t agno
, /* allocation group number */
1467 struct xfs_buf
**bpp
) /* allocation group hdr buf */
1469 struct xfs_agi
*agi
; /* allocation group header */
1470 int agi_ok
; /* agi is consistent */
1473 ASSERT(agno
!= NULLAGNUMBER
);
1475 error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
,
1476 XFS_AG_DADDR(mp
, agno
, XFS_AGI_DADDR(mp
)),
1477 XFS_FSS_TO_BB(mp
, 1), 0, bpp
);
1481 ASSERT(*bpp
&& !XFS_BUF_GETERROR(*bpp
));
1482 agi
= XFS_BUF_TO_AGI(*bpp
);
1485 * Validate the magic number of the agi block.
1487 agi_ok
= be32_to_cpu(agi
->agi_magicnum
) == XFS_AGI_MAGIC
&&
1488 XFS_AGI_GOOD_VERSION(be32_to_cpu(agi
->agi_versionnum
)) &&
1489 be32_to_cpu(agi
->agi_seqno
) == agno
;
1490 if (unlikely(XFS_TEST_ERROR(!agi_ok
, mp
, XFS_ERRTAG_IALLOC_READ_AGI
,
1491 XFS_RANDOM_IALLOC_READ_AGI
))) {
1492 XFS_CORRUPTION_ERROR("xfs_read_agi", XFS_ERRLEVEL_LOW
,
1494 xfs_trans_brelse(tp
, *bpp
);
1495 return XFS_ERROR(EFSCORRUPTED
);
1498 XFS_BUF_SET_VTYPE_REF(*bpp
, B_FS_AGI
, XFS_AGI_REF
);
1500 xfs_check_agi_unlinked(agi
);
1505 xfs_ialloc_read_agi(
1506 struct xfs_mount
*mp
, /* file system mount structure */
1507 struct xfs_trans
*tp
, /* transaction pointer */
1508 xfs_agnumber_t agno
, /* allocation group number */
1509 struct xfs_buf
**bpp
) /* allocation group hdr buf */
1511 struct xfs_agi
*agi
; /* allocation group header */
1512 struct xfs_perag
*pag
; /* per allocation group data */
1515 error
= xfs_read_agi(mp
, tp
, agno
, bpp
);
1519 agi
= XFS_BUF_TO_AGI(*bpp
);
1520 pag
= xfs_perag_get(mp
, 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
));
1538 * Read in the agi to initialise the per-ag data in the mount structure
1541 xfs_ialloc_pagi_init(
1542 xfs_mount_t
*mp
, /* file system mount structure */
1543 xfs_trans_t
*tp
, /* transaction pointer */
1544 xfs_agnumber_t agno
) /* allocation group number */
1546 xfs_buf_t
*bp
= NULL
;
1549 error
= xfs_ialloc_read_agi(mp
, tp
, agno
, &bp
);
1553 xfs_trans_brelse(tp
, bp
);