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
23 * Allocation group level functions.
26 xfs_ialloc_cluster_alignment(
27 xfs_alloc_arg_t
*args
)
29 if (xfs_sb_version_hasalign(&args
->mp
->m_sb
) &&
30 args
->mp
->m_sb
.sb_inoalignmt
>=
31 XFS_B_TO_FSBT(args
->mp
, XFS_INODE_CLUSTER_SIZE(args
->mp
)))
32 return args
->mp
->m_sb
.sb_inoalignmt
;
37 * Lookup a record by ino in the btree given by cur.
41 struct xfs_btree_cur
*cur
, /* btree cursor */
42 xfs_agino_t ino
, /* starting inode of chunk */
43 xfs_lookup_t dir
, /* <=, >=, == */
44 int *stat
) /* success/failure */
46 cur
->bc_rec
.i
.ir_startino
= ino
;
47 cur
->bc_rec
.i
.ir_freecount
= 0;
48 cur
->bc_rec
.i
.ir_free
= 0;
49 return xfs_btree_lookup(cur
, dir
, stat
);
53 * Update the record referred to by cur to the value given.
54 * This either works (return 0) or gets an EFSCORRUPTED error.
56 STATIC
int /* error */
58 struct xfs_btree_cur
*cur
, /* btree cursor */
59 xfs_inobt_rec_incore_t
*irec
) /* btree record */
61 union xfs_btree_rec rec
;
63 rec
.inobt
.ir_startino
= cpu_to_be32(irec
->ir_startino
);
64 rec
.inobt
.ir_freecount
= cpu_to_be32(irec
->ir_freecount
);
65 rec
.inobt
.ir_free
= cpu_to_be64(irec
->ir_free
);
66 return xfs_btree_update(cur
, &rec
);
70 * Get the data from the pointed-to record.
74 struct xfs_btree_cur
*cur
, /* btree cursor */
75 xfs_inobt_rec_incore_t
*irec
, /* btree record */
76 int *stat
) /* output: success/failure */
78 union xfs_btree_rec
*rec
;
81 error
= xfs_btree_get_rec(cur
, &rec
, stat
);
82 if (!error
&& *stat
== 1) {
83 irec
->ir_startino
= be32_to_cpu(rec
->inobt
.ir_startino
);
84 irec
->ir_freecount
= be32_to_cpu(rec
->inobt
.ir_freecount
);
85 irec
->ir_free
= be64_to_cpu(rec
->inobt
.ir_free
);
91 * Verify that the number of free inodes in the AGI is correct.
95 xfs_check_agi_freecount(
96 struct xfs_btree_cur
*cur
,
99 if (cur
->bc_nlevels
== 1) {
100 xfs_inobt_rec_incore_t rec
;
105 error
= xfs_inobt_lookup(cur
, 0, XFS_LOOKUP_GE
, &i
);
110 error
= xfs_inobt_get_rec(cur
, &rec
, &i
);
115 freecount
+= rec
.ir_freecount
;
116 error
= xfs_btree_increment(cur
, 0, &i
);
122 if (!XFS_FORCED_SHUTDOWN(cur
->bc_mp
))
123 ASSERT(freecount
== be32_to_cpu(agi
->agi_freecount
));
128 #define xfs_check_agi_freecount(cur, agi) 0
132 * Initialise a new set of inodes. When called without a transaction context
133 * (e.g. from recovery) we initiate a delayed write of the inode buffers rather
134 * than logging them (which in a transaction context puts them into the AIL
135 * for writeback rather than the xfsbufd queue).
138 xfs_ialloc_inode_init(
139 struct xfs_mount
*mp
,
140 struct xfs_trans
*tp
,
141 struct list_head
*buffer_list
,
144 xfs_agblock_t length
,
147 struct xfs_buf
*fbuf
;
148 struct xfs_dinode
*free
;
149 int blks_per_cluster
, nbufs
, ninodes
;
156 * Loop over the new block(s), filling in the inodes.
157 * For small block sizes, manipulate the inodes in buffers
158 * which are multiples of the blocks size.
160 if (mp
->m_sb
.sb_blocksize
>= XFS_INODE_CLUSTER_SIZE(mp
)) {
161 blks_per_cluster
= 1;
163 ninodes
= mp
->m_sb
.sb_inopblock
;
165 blks_per_cluster
= XFS_INODE_CLUSTER_SIZE(mp
) /
166 mp
->m_sb
.sb_blocksize
;
167 nbufs
= length
/ blks_per_cluster
;
168 ninodes
= blks_per_cluster
* mp
->m_sb
.sb_inopblock
;
172 * Figure out what version number to use in the inodes we create. If
173 * the superblock version has caught up to the one that supports the new
174 * inode format, then use the new inode version. Otherwise use the old
175 * version so that old kernels will continue to be able to use the file
178 * For v3 inodes, we also need to write the inode number into the inode,
179 * so calculate the first inode number of the chunk here as
180 * XFS_OFFBNO_TO_AGINO() only works within a filesystem block, not
181 * across multiple filesystem blocks (such as a cluster) and so cannot
182 * be used in the cluster buffer loop below.
184 * Further, because we are writing the inode directly into the buffer
185 * and calculating a CRC on the entire inode, we have ot log the entire
186 * inode so that the entire range the CRC covers is present in the log.
187 * That means for v3 inode we log the entire buffer rather than just the
190 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
192 ino
= XFS_AGINO_TO_INO(mp
, agno
,
193 XFS_OFFBNO_TO_AGINO(mp
, agbno
, 0));
196 * log the initialisation that is about to take place as an
197 * logical operation. This means the transaction does not
198 * need to log the physical changes to the inode buffers as log
199 * recovery will know what initialisation is actually needed.
200 * Hence we only need to log the buffers as "ordered" buffers so
201 * they track in the AIL as if they were physically logged.
204 xfs_icreate_log(tp
, agno
, agbno
, XFS_IALLOC_INODES(mp
),
205 mp
->m_sb
.sb_inodesize
, length
, gen
);
206 } else if (xfs_sb_version_hasnlink(&mp
->m_sb
))
211 for (j
= 0; j
< nbufs
; j
++) {
215 d
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
+ (j
* blks_per_cluster
));
216 fbuf
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
,
217 mp
->m_bsize
* blks_per_cluster
,
222 /* Initialize the inode buffers and log them appropriately. */
223 fbuf
->b_ops
= &xfs_inode_buf_ops
;
224 xfs_buf_zero(fbuf
, 0, BBTOB(fbuf
->b_length
));
225 for (i
= 0; i
< ninodes
; i
++) {
226 int ioffset
= i
<< mp
->m_sb
.sb_inodelog
;
227 uint isize
= xfs_dinode_size(version
);
229 free
= xfs_make_iptr(mp
, fbuf
, i
);
230 free
->di_magic
= cpu_to_be16(XFS_DINODE_MAGIC
);
231 free
->di_version
= version
;
232 free
->di_gen
= cpu_to_be32(gen
);
233 free
->di_next_unlinked
= cpu_to_be32(NULLAGINO
);
236 free
->di_ino
= cpu_to_be64(ino
);
238 uuid_copy(&free
->di_uuid
, &mp
->m_sb
.sb_uuid
);
239 xfs_dinode_calc_crc(mp
, free
);
241 /* just log the inode core */
242 xfs_trans_log_buf(tp
, fbuf
, ioffset
,
243 ioffset
+ isize
- 1);
249 * Mark the buffer as an inode allocation buffer so it
250 * sticks in AIL at the point of this allocation
251 * transaction. This ensures the they are on disk before
252 * the tail of the log can be moved past this
253 * transaction (i.e. by preventing relogging from moving
254 * it forward in the log).
256 xfs_trans_inode_alloc_buf(tp
, fbuf
);
259 * Mark the buffer as ordered so that they are
260 * not physically logged in the transaction but
261 * still tracked in the AIL as part of the
262 * transaction and pin the log appropriately.
264 xfs_trans_ordered_buf(tp
, fbuf
);
265 xfs_trans_log_buf(tp
, fbuf
, 0,
266 BBTOB(fbuf
->b_length
) - 1);
269 fbuf
->b_flags
|= XBF_DONE
;
270 xfs_buf_delwri_queue(fbuf
, buffer_list
);
278 * Allocate new inodes in the allocation group specified by agbp.
279 * Return 0 for success, else error code.
281 STATIC
int /* error code or 0 */
283 xfs_trans_t
*tp
, /* transaction pointer */
284 xfs_buf_t
*agbp
, /* alloc group buffer */
287 xfs_agi_t
*agi
; /* allocation group header */
288 xfs_alloc_arg_t args
; /* allocation argument structure */
289 xfs_btree_cur_t
*cur
; /* inode btree cursor */
293 xfs_agino_t newino
; /* new first inode's number */
294 xfs_agino_t newlen
; /* new number of inodes */
295 xfs_agino_t thisino
; /* current inode number, for loop */
296 int isaligned
= 0; /* inode allocation at stripe unit */
298 struct xfs_perag
*pag
;
300 memset(&args
, 0, sizeof(args
));
302 args
.mp
= tp
->t_mountp
;
305 * Locking will ensure that we don't have two callers in here
308 newlen
= XFS_IALLOC_INODES(args
.mp
);
309 if (args
.mp
->m_maxicount
&&
310 args
.mp
->m_sb
.sb_icount
+ newlen
> args
.mp
->m_maxicount
)
311 return XFS_ERROR(ENOSPC
);
312 args
.minlen
= args
.maxlen
= XFS_IALLOC_BLOCKS(args
.mp
);
314 * First try to allocate inodes contiguous with the last-allocated
315 * chunk of inodes. If the filesystem is striped, this will fill
316 * an entire stripe unit with inodes.
318 agi
= XFS_BUF_TO_AGI(agbp
);
319 newino
= be32_to_cpu(agi
->agi_newino
);
320 agno
= be32_to_cpu(agi
->agi_seqno
);
321 args
.agbno
= XFS_AGINO_TO_AGBNO(args
.mp
, newino
) +
322 XFS_IALLOC_BLOCKS(args
.mp
);
323 if (likely(newino
!= NULLAGINO
&&
324 (args
.agbno
< be32_to_cpu(agi
->agi_length
)))) {
325 args
.fsbno
= XFS_AGB_TO_FSB(args
.mp
, agno
, args
.agbno
);
326 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
330 * We need to take into account alignment here to ensure that
331 * we don't modify the free list if we fail to have an exact
332 * block. If we don't have an exact match, and every oher
333 * attempt allocation attempt fails, we'll end up cancelling
334 * a dirty transaction and shutting down.
336 * For an exact allocation, alignment must be 1,
337 * however we need to take cluster alignment into account when
338 * fixing up the freelist. Use the minalignslop field to
339 * indicate that extra blocks might be required for alignment,
340 * but not to use them in the actual exact allocation.
343 args
.minalignslop
= xfs_ialloc_cluster_alignment(&args
) - 1;
345 /* Allow space for the inode btree to split. */
346 args
.minleft
= args
.mp
->m_in_maxlevels
- 1;
347 if ((error
= xfs_alloc_vextent(&args
)))
350 args
.fsbno
= NULLFSBLOCK
;
352 if (unlikely(args
.fsbno
== NULLFSBLOCK
)) {
354 * Set the alignment for the allocation.
355 * If stripe alignment is turned on then align at stripe unit
357 * If the cluster size is smaller than a filesystem block
358 * then we're doing I/O for inodes in filesystem block size
359 * pieces, so don't need alignment anyway.
362 if (args
.mp
->m_sinoalign
) {
363 ASSERT(!(args
.mp
->m_flags
& XFS_MOUNT_NOALIGN
));
364 args
.alignment
= args
.mp
->m_dalign
;
367 args
.alignment
= xfs_ialloc_cluster_alignment(&args
);
369 * Need to figure out where to allocate the inode blocks.
370 * Ideally they should be spaced out through the a.g.
371 * For now, just allocate blocks up front.
373 args
.agbno
= be32_to_cpu(agi
->agi_root
);
374 args
.fsbno
= XFS_AGB_TO_FSB(args
.mp
, agno
, args
.agbno
);
376 * Allocate a fixed-size extent of inodes.
378 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
381 * Allow space for the inode btree to split.
383 args
.minleft
= args
.mp
->m_in_maxlevels
- 1;
384 if ((error
= xfs_alloc_vextent(&args
)))
389 * If stripe alignment is turned on, then try again with cluster
392 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
393 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
394 args
.agbno
= be32_to_cpu(agi
->agi_root
);
395 args
.fsbno
= XFS_AGB_TO_FSB(args
.mp
, agno
, args
.agbno
);
396 args
.alignment
= xfs_ialloc_cluster_alignment(&args
);
397 if ((error
= xfs_alloc_vextent(&args
)))
401 if (args
.fsbno
== NULLFSBLOCK
) {
405 ASSERT(args
.len
== args
.minlen
);
408 * Stamp and write the inode buffers.
410 * Seed the new inode cluster with a random generation number. This
411 * prevents short-term reuse of generation numbers if a chunk is
412 * freed and then immediately reallocated. We use random numbers
413 * rather than a linear progression to prevent the next generation
414 * number from being easily guessable.
416 error
= xfs_ialloc_inode_init(args
.mp
, tp
, NULL
, agno
, args
.agbno
,
417 args
.len
, prandom_u32());
422 * Convert the results.
424 newino
= XFS_OFFBNO_TO_AGINO(args
.mp
, args
.agbno
, 0);
425 be32_add_cpu(&agi
->agi_count
, newlen
);
426 be32_add_cpu(&agi
->agi_freecount
, newlen
);
427 pag
= xfs_perag_get(args
.mp
, agno
);
428 pag
->pagi_freecount
+= newlen
;
430 agi
->agi_newino
= cpu_to_be32(newino
);
433 * Insert records describing the new inode chunk into the btree.
435 cur
= xfs_inobt_init_cursor(args
.mp
, tp
, agbp
, agno
);
436 for (thisino
= newino
;
437 thisino
< newino
+ newlen
;
438 thisino
+= XFS_INODES_PER_CHUNK
) {
439 cur
->bc_rec
.i
.ir_startino
= thisino
;
440 cur
->bc_rec
.i
.ir_freecount
= XFS_INODES_PER_CHUNK
;
441 cur
->bc_rec
.i
.ir_free
= XFS_INOBT_ALL_FREE
;
442 error
= xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, &i
);
444 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
448 error
= xfs_btree_insert(cur
, &i
);
450 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
455 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
457 * Log allocation group header fields
459 xfs_ialloc_log_agi(tp
, agbp
,
460 XFS_AGI_COUNT
| XFS_AGI_FREECOUNT
| XFS_AGI_NEWINO
);
462 * Modify/log superblock values for inode count and inode free count.
464 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_ICOUNT
, (long)newlen
);
465 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_IFREE
, (long)newlen
);
470 STATIC xfs_agnumber_t
476 spin_lock(&mp
->m_agirotor_lock
);
477 agno
= mp
->m_agirotor
;
478 if (++mp
->m_agirotor
>= mp
->m_maxagi
)
480 spin_unlock(&mp
->m_agirotor_lock
);
486 * Select an allocation group to look for a free inode in, based on the parent
487 * inode and the mode. Return the allocation group buffer.
489 STATIC xfs_agnumber_t
490 xfs_ialloc_ag_select(
491 xfs_trans_t
*tp
, /* transaction pointer */
492 xfs_ino_t parent
, /* parent directory inode number */
493 umode_t mode
, /* bits set to indicate file type */
494 int okalloc
) /* ok to allocate more space */
496 xfs_agnumber_t agcount
; /* number of ag's in the filesystem */
497 xfs_agnumber_t agno
; /* current ag number */
498 int flags
; /* alloc buffer locking flags */
499 xfs_extlen_t ineed
; /* blocks needed for inode allocation */
500 xfs_extlen_t longest
= 0; /* longest extent available */
501 xfs_mount_t
*mp
; /* mount point structure */
502 int needspace
; /* file mode implies space allocated */
503 xfs_perag_t
*pag
; /* per allocation group data */
504 xfs_agnumber_t pagno
; /* parent (starting) ag number */
508 * Files of these types need at least one block if length > 0
509 * (and they won't fit in the inode, but that's hard to figure out).
511 needspace
= S_ISDIR(mode
) || S_ISREG(mode
) || S_ISLNK(mode
);
513 agcount
= mp
->m_maxagi
;
515 pagno
= xfs_ialloc_next_ag(mp
);
517 pagno
= XFS_INO_TO_AGNO(mp
, parent
);
518 if (pagno
>= agcount
)
522 ASSERT(pagno
< agcount
);
525 * Loop through allocation groups, looking for one with a little
526 * free space in it. Note we don't look for free inodes, exactly.
527 * Instead, we include whether there is a need to allocate inodes
528 * to mean that blocks must be allocated for them,
529 * if none are currently free.
532 flags
= XFS_ALLOC_FLAG_TRYLOCK
;
534 pag
= xfs_perag_get(mp
, agno
);
535 if (!pag
->pagi_inodeok
) {
536 xfs_ialloc_next_ag(mp
);
540 if (!pag
->pagi_init
) {
541 error
= xfs_ialloc_pagi_init(mp
, tp
, agno
);
546 if (pag
->pagi_freecount
) {
554 if (!pag
->pagf_init
) {
555 error
= xfs_alloc_pagf_init(mp
, tp
, agno
, flags
);
561 * Is there enough free space for the file plus a block of
562 * inodes? (if we need to allocate some)?
564 ineed
= XFS_IALLOC_BLOCKS(mp
);
565 longest
= pag
->pagf_longest
;
567 longest
= pag
->pagf_flcount
> 0;
569 if (pag
->pagf_freeblks
>= needspace
+ ineed
&&
577 * No point in iterating over the rest, if we're shutting
580 if (XFS_FORCED_SHUTDOWN(mp
))
594 * Try to retrieve the next record to the left/right from the current one.
598 struct xfs_btree_cur
*cur
,
599 xfs_inobt_rec_incore_t
*rec
,
607 error
= xfs_btree_decrement(cur
, 0, &i
);
609 error
= xfs_btree_increment(cur
, 0, &i
);
615 error
= xfs_inobt_get_rec(cur
, rec
, &i
);
618 XFS_WANT_CORRUPTED_RETURN(i
== 1);
626 struct xfs_btree_cur
*cur
,
628 xfs_inobt_rec_incore_t
*rec
,
634 error
= xfs_inobt_lookup(cur
, agino
, XFS_LOOKUP_EQ
, &i
);
639 error
= xfs_inobt_get_rec(cur
, rec
, &i
);
642 XFS_WANT_CORRUPTED_RETURN(i
== 1);
651 * The caller selected an AG for us, and made sure that free inodes are
656 struct xfs_trans
*tp
,
657 struct xfs_buf
*agbp
,
661 struct xfs_mount
*mp
= tp
->t_mountp
;
662 struct xfs_agi
*agi
= XFS_BUF_TO_AGI(agbp
);
663 xfs_agnumber_t agno
= be32_to_cpu(agi
->agi_seqno
);
664 xfs_agnumber_t pagno
= XFS_INO_TO_AGNO(mp
, parent
);
665 xfs_agino_t pagino
= XFS_INO_TO_AGINO(mp
, parent
);
666 struct xfs_perag
*pag
;
667 struct xfs_btree_cur
*cur
, *tcur
;
668 struct xfs_inobt_rec_incore rec
, trec
;
674 pag
= xfs_perag_get(mp
, agno
);
676 ASSERT(pag
->pagi_init
);
677 ASSERT(pag
->pagi_inodeok
);
678 ASSERT(pag
->pagi_freecount
> 0);
681 cur
= xfs_inobt_init_cursor(mp
, tp
, agbp
, agno
);
683 * If pagino is 0 (this is the root inode allocation) use newino.
684 * This must work because we've just allocated some.
687 pagino
= be32_to_cpu(agi
->agi_newino
);
689 error
= xfs_check_agi_freecount(cur
, agi
);
694 * If in the same AG as the parent, try to get near the parent.
697 int doneleft
; /* done, to the left */
698 int doneright
; /* done, to the right */
699 int searchdistance
= 10;
701 error
= xfs_inobt_lookup(cur
, pagino
, XFS_LOOKUP_LE
, &i
);
704 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
706 error
= xfs_inobt_get_rec(cur
, &rec
, &j
);
709 XFS_WANT_CORRUPTED_GOTO(j
== 1, error0
);
711 if (rec
.ir_freecount
> 0) {
713 * Found a free inode in the same chunk
714 * as the parent, done.
721 * In the same AG as parent, but parent's chunk is full.
724 /* duplicate the cursor, search left & right simultaneously */
725 error
= xfs_btree_dup_cursor(cur
, &tcur
);
730 * Skip to last blocks looked up if same parent inode.
732 if (pagino
!= NULLAGINO
&&
733 pag
->pagl_pagino
== pagino
&&
734 pag
->pagl_leftrec
!= NULLAGINO
&&
735 pag
->pagl_rightrec
!= NULLAGINO
) {
736 error
= xfs_ialloc_get_rec(tcur
, pag
->pagl_leftrec
,
741 error
= xfs_ialloc_get_rec(cur
, pag
->pagl_rightrec
,
746 /* search left with tcur, back up 1 record */
747 error
= xfs_ialloc_next_rec(tcur
, &trec
, &doneleft
, 1);
751 /* search right with cur, go forward 1 record. */
752 error
= xfs_ialloc_next_rec(cur
, &rec
, &doneright
, 0);
758 * Loop until we find an inode chunk with a free inode.
760 while (!doneleft
|| !doneright
) {
761 int useleft
; /* using left inode chunk this time */
763 if (!--searchdistance
) {
765 * Not in range - save last search
766 * location and allocate a new inode
768 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
769 pag
->pagl_leftrec
= trec
.ir_startino
;
770 pag
->pagl_rightrec
= rec
.ir_startino
;
771 pag
->pagl_pagino
= pagino
;
775 /* figure out the closer block if both are valid. */
776 if (!doneleft
&& !doneright
) {
778 (trec
.ir_startino
+ XFS_INODES_PER_CHUNK
- 1) <
779 rec
.ir_startino
- pagino
;
784 /* free inodes to the left? */
785 if (useleft
&& trec
.ir_freecount
) {
787 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
790 pag
->pagl_leftrec
= trec
.ir_startino
;
791 pag
->pagl_rightrec
= rec
.ir_startino
;
792 pag
->pagl_pagino
= pagino
;
796 /* free inodes to the right? */
797 if (!useleft
&& rec
.ir_freecount
) {
798 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
800 pag
->pagl_leftrec
= trec
.ir_startino
;
801 pag
->pagl_rightrec
= rec
.ir_startino
;
802 pag
->pagl_pagino
= pagino
;
806 /* get next record to check */
808 error
= xfs_ialloc_next_rec(tcur
, &trec
,
811 error
= xfs_ialloc_next_rec(cur
, &rec
,
819 * We've reached the end of the btree. because
820 * we are only searching a small chunk of the
821 * btree each search, there is obviously free
822 * inodes closer to the parent inode than we
823 * are now. restart the search again.
825 pag
->pagl_pagino
= NULLAGINO
;
826 pag
->pagl_leftrec
= NULLAGINO
;
827 pag
->pagl_rightrec
= NULLAGINO
;
828 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
829 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
834 * In a different AG from the parent.
835 * See if the most recently allocated block has any free.
838 if (agi
->agi_newino
!= cpu_to_be32(NULLAGINO
)) {
839 error
= xfs_inobt_lookup(cur
, be32_to_cpu(agi
->agi_newino
),
845 error
= xfs_inobt_get_rec(cur
, &rec
, &j
);
849 if (j
== 1 && rec
.ir_freecount
> 0) {
851 * The last chunk allocated in the group
852 * still has a free inode.
860 * None left in the last group, search the whole AG
862 error
= xfs_inobt_lookup(cur
, 0, XFS_LOOKUP_GE
, &i
);
865 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
868 error
= xfs_inobt_get_rec(cur
, &rec
, &i
);
871 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
872 if (rec
.ir_freecount
> 0)
874 error
= xfs_btree_increment(cur
, 0, &i
);
877 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
881 offset
= xfs_lowbit64(rec
.ir_free
);
883 ASSERT(offset
< XFS_INODES_PER_CHUNK
);
884 ASSERT((XFS_AGINO_TO_OFFSET(mp
, rec
.ir_startino
) %
885 XFS_INODES_PER_CHUNK
) == 0);
886 ino
= XFS_AGINO_TO_INO(mp
, agno
, rec
.ir_startino
+ offset
);
887 rec
.ir_free
&= ~XFS_INOBT_MASK(offset
);
889 error
= xfs_inobt_update(cur
, &rec
);
892 be32_add_cpu(&agi
->agi_freecount
, -1);
893 xfs_ialloc_log_agi(tp
, agbp
, XFS_AGI_FREECOUNT
);
894 pag
->pagi_freecount
--;
896 error
= xfs_check_agi_freecount(cur
, agi
);
900 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
901 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_IFREE
, -1);
906 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
908 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
914 * Allocate an inode on disk.
916 * Mode is used to tell whether the new inode will need space, and whether it
919 * This function is designed to be called twice if it has to do an allocation
920 * to make more free inodes. On the first call, *IO_agbp should be set to NULL.
921 * If an inode is available without having to performn an allocation, an inode
922 * number is returned. In this case, *IO_agbp is set to NULL. If an allocation
923 * needs to be done, xfs_dialloc returns the current AGI buffer in *IO_agbp.
924 * The caller should then commit the current transaction, allocate a
925 * new transaction, and call xfs_dialloc() again, passing in the previous value
926 * of *IO_agbp. IO_agbp should be held across the transactions. Since the AGI
927 * buffer is locked across the two calls, the second call is guaranteed to have
928 * a free inode available.
930 * Once we successfully pick an inode its number is returned and the on-disk
931 * data structures are updated. The inode itself is not read in, since doing so
932 * would break ordering constraints with xfs_reclaim.
936 struct xfs_trans
*tp
,
940 struct xfs_buf
**IO_agbp
,
943 struct xfs_mount
*mp
= tp
->t_mountp
;
944 struct xfs_buf
*agbp
;
949 xfs_agnumber_t start_agno
;
950 struct xfs_perag
*pag
;
954 * If the caller passes in a pointer to the AGI buffer,
955 * continue where we left off before. In this case, we
956 * know that the allocation group has free inodes.
963 * We do not have an agbp, so select an initial allocation
964 * group for inode allocation.
966 start_agno
= xfs_ialloc_ag_select(tp
, parent
, mode
, okalloc
);
967 if (start_agno
== NULLAGNUMBER
) {
973 * If we have already hit the ceiling of inode blocks then clear
974 * okalloc so we scan all available agi structures for a free
977 if (mp
->m_maxicount
&&
978 mp
->m_sb
.sb_icount
+ XFS_IALLOC_INODES(mp
) > mp
->m_maxicount
) {
984 * Loop until we find an allocation group that either has free inodes
985 * or in which we can allocate some inodes. Iterate through the
986 * allocation groups upward, wrapping at the end.
990 pag
= xfs_perag_get(mp
, agno
);
991 if (!pag
->pagi_inodeok
) {
992 xfs_ialloc_next_ag(mp
);
996 if (!pag
->pagi_init
) {
997 error
= xfs_ialloc_pagi_init(mp
, tp
, agno
);
1003 * Do a first racy fast path check if this AG is usable.
1005 if (!pag
->pagi_freecount
&& !okalloc
)
1009 * Then read in the AGI buffer and recheck with the AGI buffer
1012 error
= xfs_ialloc_read_agi(mp
, tp
, agno
, &agbp
);
1016 if (pag
->pagi_freecount
) {
1022 goto nextag_relse_buffer
;
1025 error
= xfs_ialloc_ag_alloc(tp
, agbp
, &ialloced
);
1027 xfs_trans_brelse(tp
, agbp
);
1029 if (error
!= ENOSPC
)
1039 * We successfully allocated some inodes, return
1040 * the current context to the caller so that it
1041 * can commit the current transaction and call
1042 * us again where we left off.
1044 ASSERT(pag
->pagi_freecount
> 0);
1052 nextag_relse_buffer
:
1053 xfs_trans_brelse(tp
, agbp
);
1056 if (++agno
== mp
->m_sb
.sb_agcount
)
1058 if (agno
== start_agno
) {
1060 return noroom
? ENOSPC
: 0;
1066 return xfs_dialloc_ag(tp
, agbp
, parent
, inop
);
1069 return XFS_ERROR(error
);
1073 * Free disk inode. Carefully avoids touching the incore inode, all
1074 * manipulations incore are the caller's responsibility.
1075 * The on-disk inode is not changed by this operation, only the
1076 * btree (free inode mask) is changed.
1080 xfs_trans_t
*tp
, /* transaction pointer */
1081 xfs_ino_t inode
, /* inode to be freed */
1082 xfs_bmap_free_t
*flist
, /* extents to free */
1083 int *delete, /* set if inode cluster was deleted */
1084 xfs_ino_t
*first_ino
) /* first inode in deleted cluster */
1087 xfs_agblock_t agbno
; /* block number containing inode */
1088 xfs_buf_t
*agbp
; /* buffer containing allocation group header */
1089 xfs_agino_t agino
; /* inode number relative to allocation group */
1090 xfs_agnumber_t agno
; /* allocation group number */
1091 xfs_agi_t
*agi
; /* allocation group header */
1092 xfs_btree_cur_t
*cur
; /* inode btree cursor */
1093 int error
; /* error return value */
1094 int i
; /* result code */
1095 int ilen
; /* inodes in an inode cluster */
1096 xfs_mount_t
*mp
; /* mount structure for filesystem */
1097 int off
; /* offset of inode in inode chunk */
1098 xfs_inobt_rec_incore_t rec
; /* btree record */
1099 struct xfs_perag
*pag
;
1104 * Break up inode number into its components.
1106 agno
= XFS_INO_TO_AGNO(mp
, inode
);
1107 if (agno
>= mp
->m_sb
.sb_agcount
) {
1108 xfs_warn(mp
, "%s: agno >= mp->m_sb.sb_agcount (%d >= %d).",
1109 __func__
, agno
, mp
->m_sb
.sb_agcount
);
1111 return XFS_ERROR(EINVAL
);
1113 agino
= XFS_INO_TO_AGINO(mp
, inode
);
1114 if (inode
!= XFS_AGINO_TO_INO(mp
, agno
, agino
)) {
1115 xfs_warn(mp
, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).",
1116 __func__
, (unsigned long long)inode
,
1117 (unsigned long long)XFS_AGINO_TO_INO(mp
, agno
, agino
));
1119 return XFS_ERROR(EINVAL
);
1121 agbno
= XFS_AGINO_TO_AGBNO(mp
, agino
);
1122 if (agbno
>= mp
->m_sb
.sb_agblocks
) {
1123 xfs_warn(mp
, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).",
1124 __func__
, agbno
, mp
->m_sb
.sb_agblocks
);
1126 return XFS_ERROR(EINVAL
);
1129 * Get the allocation group header.
1131 error
= xfs_ialloc_read_agi(mp
, tp
, agno
, &agbp
);
1133 xfs_warn(mp
, "%s: xfs_ialloc_read_agi() returned error %d.",
1137 agi
= XFS_BUF_TO_AGI(agbp
);
1138 ASSERT(agi
->agi_magicnum
== cpu_to_be32(XFS_AGI_MAGIC
));
1139 ASSERT(agbno
< be32_to_cpu(agi
->agi_length
));
1141 * Initialize the cursor.
1143 cur
= xfs_inobt_init_cursor(mp
, tp
, agbp
, agno
);
1145 error
= xfs_check_agi_freecount(cur
, agi
);
1150 * Look for the entry describing this inode.
1152 if ((error
= xfs_inobt_lookup(cur
, agino
, XFS_LOOKUP_LE
, &i
))) {
1153 xfs_warn(mp
, "%s: xfs_inobt_lookup() returned error %d.",
1157 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1158 error
= xfs_inobt_get_rec(cur
, &rec
, &i
);
1160 xfs_warn(mp
, "%s: xfs_inobt_get_rec() returned error %d.",
1164 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1166 * Get the offset in the inode chunk.
1168 off
= agino
- rec
.ir_startino
;
1169 ASSERT(off
>= 0 && off
< XFS_INODES_PER_CHUNK
);
1170 ASSERT(!(rec
.ir_free
& XFS_INOBT_MASK(off
)));
1172 * Mark the inode free & increment the count.
1174 rec
.ir_free
|= XFS_INOBT_MASK(off
);
1178 * When an inode cluster is free, it becomes eligible for removal
1180 if (!(mp
->m_flags
& XFS_MOUNT_IKEEP
) &&
1181 (rec
.ir_freecount
== XFS_IALLOC_INODES(mp
))) {
1184 *first_ino
= XFS_AGINO_TO_INO(mp
, agno
, rec
.ir_startino
);
1187 * Remove the inode cluster from the AGI B+Tree, adjust the
1188 * AGI and Superblock inode counts, and mark the disk space
1189 * to be freed when the transaction is committed.
1191 ilen
= XFS_IALLOC_INODES(mp
);
1192 be32_add_cpu(&agi
->agi_count
, -ilen
);
1193 be32_add_cpu(&agi
->agi_freecount
, -(ilen
- 1));
1194 xfs_ialloc_log_agi(tp
, agbp
, XFS_AGI_COUNT
| XFS_AGI_FREECOUNT
);
1195 pag
= xfs_perag_get(mp
, agno
);
1196 pag
->pagi_freecount
-= ilen
- 1;
1198 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_ICOUNT
, -ilen
);
1199 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_IFREE
, -(ilen
- 1));
1201 if ((error
= xfs_btree_delete(cur
, &i
))) {
1202 xfs_warn(mp
, "%s: xfs_btree_delete returned error %d.",
1207 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp
,
1208 agno
, XFS_INO_TO_AGBNO(mp
,rec
.ir_startino
)),
1209 XFS_IALLOC_BLOCKS(mp
), flist
, mp
);
1213 error
= xfs_inobt_update(cur
, &rec
);
1215 xfs_warn(mp
, "%s: xfs_inobt_update returned error %d.",
1221 * Change the inode free counts and log the ag/sb changes.
1223 be32_add_cpu(&agi
->agi_freecount
, 1);
1224 xfs_ialloc_log_agi(tp
, agbp
, XFS_AGI_FREECOUNT
);
1225 pag
= xfs_perag_get(mp
, agno
);
1226 pag
->pagi_freecount
++;
1228 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_IFREE
, 1);
1231 error
= xfs_check_agi_freecount(cur
, agi
);
1235 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1239 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1245 struct xfs_mount
*mp
,
1246 struct xfs_trans
*tp
,
1247 xfs_agnumber_t agno
,
1249 xfs_agblock_t agbno
,
1250 xfs_agblock_t
*chunk_agbno
,
1251 xfs_agblock_t
*offset_agbno
,
1254 struct xfs_inobt_rec_incore rec
;
1255 struct xfs_btree_cur
*cur
;
1256 struct xfs_buf
*agbp
;
1260 error
= xfs_ialloc_read_agi(mp
, tp
, agno
, &agbp
);
1263 "%s: xfs_ialloc_read_agi() returned error %d, agno %d",
1264 __func__
, error
, agno
);
1269 * Lookup the inode record for the given agino. If the record cannot be
1270 * found, then it's an invalid inode number and we should abort. Once
1271 * we have a record, we need to ensure it contains the inode number
1272 * we are looking up.
1274 cur
= xfs_inobt_init_cursor(mp
, tp
, agbp
, agno
);
1275 error
= xfs_inobt_lookup(cur
, agino
, XFS_LOOKUP_LE
, &i
);
1278 error
= xfs_inobt_get_rec(cur
, &rec
, &i
);
1279 if (!error
&& i
== 0)
1283 xfs_trans_brelse(tp
, agbp
);
1284 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1288 /* check that the returned record contains the required inode */
1289 if (rec
.ir_startino
> agino
||
1290 rec
.ir_startino
+ XFS_IALLOC_INODES(mp
) <= agino
)
1293 /* for untrusted inodes check it is allocated first */
1294 if ((flags
& XFS_IGET_UNTRUSTED
) &&
1295 (rec
.ir_free
& XFS_INOBT_MASK(agino
- rec
.ir_startino
)))
1298 *chunk_agbno
= XFS_AGINO_TO_AGBNO(mp
, rec
.ir_startino
);
1299 *offset_agbno
= agbno
- *chunk_agbno
;
1304 * Return the location of the inode in imap, for mapping it into a buffer.
1308 xfs_mount_t
*mp
, /* file system mount structure */
1309 xfs_trans_t
*tp
, /* transaction pointer */
1310 xfs_ino_t ino
, /* inode to locate */
1311 struct xfs_imap
*imap
, /* location map structure */
1312 uint flags
) /* flags for inode btree lookup */
1314 xfs_agblock_t agbno
; /* block number of inode in the alloc group */
1315 xfs_agino_t agino
; /* inode number within alloc group */
1316 xfs_agnumber_t agno
; /* allocation group number */
1317 int blks_per_cluster
; /* num blocks per inode cluster */
1318 xfs_agblock_t chunk_agbno
; /* first block in inode chunk */
1319 xfs_agblock_t cluster_agbno
; /* first block in inode cluster */
1320 int error
; /* error code */
1321 int offset
; /* index of inode in its buffer */
1322 xfs_agblock_t offset_agbno
; /* blks from chunk start to inode */
1324 ASSERT(ino
!= NULLFSINO
);
1327 * Split up the inode number into its parts.
1329 agno
= XFS_INO_TO_AGNO(mp
, ino
);
1330 agino
= XFS_INO_TO_AGINO(mp
, ino
);
1331 agbno
= XFS_AGINO_TO_AGBNO(mp
, agino
);
1332 if (agno
>= mp
->m_sb
.sb_agcount
|| agbno
>= mp
->m_sb
.sb_agblocks
||
1333 ino
!= XFS_AGINO_TO_INO(mp
, agno
, agino
)) {
1336 * Don't output diagnostic information for untrusted inodes
1337 * as they can be invalid without implying corruption.
1339 if (flags
& XFS_IGET_UNTRUSTED
)
1340 return XFS_ERROR(EINVAL
);
1341 if (agno
>= mp
->m_sb
.sb_agcount
) {
1343 "%s: agno (%d) >= mp->m_sb.sb_agcount (%d)",
1344 __func__
, agno
, mp
->m_sb
.sb_agcount
);
1346 if (agbno
>= mp
->m_sb
.sb_agblocks
) {
1348 "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)",
1349 __func__
, (unsigned long long)agbno
,
1350 (unsigned long)mp
->m_sb
.sb_agblocks
);
1352 if (ino
!= XFS_AGINO_TO_INO(mp
, agno
, agino
)) {
1354 "%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)",
1356 XFS_AGINO_TO_INO(mp
, agno
, agino
));
1360 return XFS_ERROR(EINVAL
);
1363 blks_per_cluster
= XFS_INODE_CLUSTER_SIZE(mp
) >> mp
->m_sb
.sb_blocklog
;
1366 * For bulkstat and handle lookups, we have an untrusted inode number
1367 * that we have to verify is valid. We cannot do this just by reading
1368 * the inode buffer as it may have been unlinked and removed leaving
1369 * inodes in stale state on disk. Hence we have to do a btree lookup
1370 * in all cases where an untrusted inode number is passed.
1372 if (flags
& XFS_IGET_UNTRUSTED
) {
1373 error
= xfs_imap_lookup(mp
, tp
, agno
, agino
, agbno
,
1374 &chunk_agbno
, &offset_agbno
, flags
);
1381 * If the inode cluster size is the same as the blocksize or
1382 * smaller we get to the buffer by simple arithmetics.
1384 if (XFS_INODE_CLUSTER_SIZE(mp
) <= mp
->m_sb
.sb_blocksize
) {
1385 offset
= XFS_INO_TO_OFFSET(mp
, ino
);
1386 ASSERT(offset
< mp
->m_sb
.sb_inopblock
);
1388 imap
->im_blkno
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
);
1389 imap
->im_len
= XFS_FSB_TO_BB(mp
, 1);
1390 imap
->im_boffset
= (ushort
)(offset
<< mp
->m_sb
.sb_inodelog
);
1395 * If the inode chunks are aligned then use simple maths to
1396 * find the location. Otherwise we have to do a btree
1397 * lookup to find the location.
1399 if (mp
->m_inoalign_mask
) {
1400 offset_agbno
= agbno
& mp
->m_inoalign_mask
;
1401 chunk_agbno
= agbno
- offset_agbno
;
1403 error
= xfs_imap_lookup(mp
, tp
, agno
, agino
, agbno
,
1404 &chunk_agbno
, &offset_agbno
, flags
);
1410 ASSERT(agbno
>= chunk_agbno
);
1411 cluster_agbno
= chunk_agbno
+
1412 ((offset_agbno
/ blks_per_cluster
) * blks_per_cluster
);
1413 offset
= ((agbno
- cluster_agbno
) * mp
->m_sb
.sb_inopblock
) +
1414 XFS_INO_TO_OFFSET(mp
, ino
);
1416 imap
->im_blkno
= XFS_AGB_TO_DADDR(mp
, agno
, cluster_agbno
);
1417 imap
->im_len
= XFS_FSB_TO_BB(mp
, blks_per_cluster
);
1418 imap
->im_boffset
= (ushort
)(offset
<< mp
->m_sb
.sb_inodelog
);
1421 * If the inode number maps to a block outside the bounds
1422 * of the file system then return NULL rather than calling
1423 * read_buf and panicing when we get an error from the
1426 if ((imap
->im_blkno
+ imap
->im_len
) >
1427 XFS_FSB_TO_BB(mp
, mp
->m_sb
.sb_dblocks
)) {
1429 "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)",
1430 __func__
, (unsigned long long) imap
->im_blkno
,
1431 (unsigned long long) imap
->im_len
,
1432 XFS_FSB_TO_BB(mp
, mp
->m_sb
.sb_dblocks
));
1433 return XFS_ERROR(EINVAL
);
1439 * Compute and fill in value of m_in_maxlevels.
1442 xfs_ialloc_compute_maxlevels(
1443 xfs_mount_t
*mp
) /* file system mount structure */
1451 maxleafents
= (1LL << XFS_INO_AGINO_BITS(mp
)) >>
1452 XFS_INODES_PER_CHUNK_LOG
;
1453 minleafrecs
= mp
->m_alloc_mnr
[0];
1454 minnoderecs
= mp
->m_alloc_mnr
[1];
1455 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
1456 for (level
= 1; maxblocks
> 1; level
++)
1457 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
1458 mp
->m_in_maxlevels
= level
;
1462 * Log specified fields for the ag hdr (inode section)
1466 xfs_trans_t
*tp
, /* transaction pointer */
1467 xfs_buf_t
*bp
, /* allocation group header buffer */
1468 int fields
) /* bitmask of fields to log */
1470 int first
; /* first byte number */
1471 int last
; /* last byte number */
1472 static const short offsets
[] = { /* field starting offsets */
1473 /* keep in sync with bit definitions */
1474 offsetof(xfs_agi_t
, agi_magicnum
),
1475 offsetof(xfs_agi_t
, agi_versionnum
),
1476 offsetof(xfs_agi_t
, agi_seqno
),
1477 offsetof(xfs_agi_t
, agi_length
),
1478 offsetof(xfs_agi_t
, agi_count
),
1479 offsetof(xfs_agi_t
, agi_root
),
1480 offsetof(xfs_agi_t
, agi_level
),
1481 offsetof(xfs_agi_t
, agi_freecount
),
1482 offsetof(xfs_agi_t
, agi_newino
),
1483 offsetof(xfs_agi_t
, agi_dirino
),
1484 offsetof(xfs_agi_t
, agi_unlinked
),
1488 xfs_agi_t
*agi
; /* allocation group header */
1490 agi
= XFS_BUF_TO_AGI(bp
);
1491 ASSERT(agi
->agi_magicnum
== cpu_to_be32(XFS_AGI_MAGIC
));
1494 * Compute byte offsets for the first and last fields.
1496 xfs_btree_offsets(fields
, offsets
, XFS_AGI_NUM_BITS
, &first
, &last
);
1498 * Log the allocation group inode header buffer.
1500 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_AGI_BUF
);
1501 xfs_trans_log_buf(tp
, bp
, first
, last
);
1506 xfs_check_agi_unlinked(
1507 struct xfs_agi
*agi
)
1511 for (i
= 0; i
< XFS_AGI_UNLINKED_BUCKETS
; i
++)
1512 ASSERT(agi
->agi_unlinked
[i
]);
1515 #define xfs_check_agi_unlinked(agi)
1522 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
1523 struct xfs_agi
*agi
= XFS_BUF_TO_AGI(bp
);
1525 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
1526 !uuid_equal(&agi
->agi_uuid
, &mp
->m_sb
.sb_uuid
))
1529 * Validate the magic number of the agi block.
1531 if (agi
->agi_magicnum
!= cpu_to_be32(XFS_AGI_MAGIC
))
1533 if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi
->agi_versionnum
)))
1537 * during growfs operations, the perag is not fully initialised,
1538 * so we can't use it for any useful checking. growfs ensures we can't
1539 * use it by using uncached buffers that don't have the perag attached
1540 * so we can detect and avoid this problem.
1542 if (bp
->b_pag
&& be32_to_cpu(agi
->agi_seqno
) != bp
->b_pag
->pag_agno
)
1545 xfs_check_agi_unlinked(agi
);
1550 xfs_agi_read_verify(
1553 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
1556 if (xfs_sb_version_hascrc(&mp
->m_sb
))
1557 agi_ok
= xfs_verify_cksum(bp
->b_addr
, BBTOB(bp
->b_length
),
1558 offsetof(struct xfs_agi
, agi_crc
));
1559 agi_ok
= agi_ok
&& xfs_agi_verify(bp
);
1561 if (unlikely(XFS_TEST_ERROR(!agi_ok
, mp
, XFS_ERRTAG_IALLOC_READ_AGI
,
1562 XFS_RANDOM_IALLOC_READ_AGI
))) {
1563 XFS_CORRUPTION_ERROR(__func__
, XFS_ERRLEVEL_LOW
, mp
, bp
->b_addr
);
1564 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
1569 xfs_agi_write_verify(
1572 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
1573 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
1575 if (!xfs_agi_verify(bp
)) {
1576 XFS_CORRUPTION_ERROR(__func__
, XFS_ERRLEVEL_LOW
, mp
, bp
->b_addr
);
1577 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
1581 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
1585 XFS_BUF_TO_AGI(bp
)->agi_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
1586 xfs_update_cksum(bp
->b_addr
, BBTOB(bp
->b_length
),
1587 offsetof(struct xfs_agi
, agi_crc
));
1590 const struct xfs_buf_ops xfs_agi_buf_ops
= {
1591 .verify_read
= xfs_agi_read_verify
,
1592 .verify_write
= xfs_agi_write_verify
,
1596 * Read in the allocation group header (inode allocation section)
1600 struct xfs_mount
*mp
, /* file system mount structure */
1601 struct xfs_trans
*tp
, /* transaction pointer */
1602 xfs_agnumber_t agno
, /* allocation group number */
1603 struct xfs_buf
**bpp
) /* allocation group hdr buf */
1607 ASSERT(agno
!= NULLAGNUMBER
);
1609 error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
,
1610 XFS_AG_DADDR(mp
, agno
, XFS_AGI_DADDR(mp
)),
1611 XFS_FSS_TO_BB(mp
, 1), 0, bpp
, &xfs_agi_buf_ops
);
1615 ASSERT(!xfs_buf_geterror(*bpp
));
1616 xfs_buf_set_ref(*bpp
, XFS_AGI_REF
);
1621 xfs_ialloc_read_agi(
1622 struct xfs_mount
*mp
, /* file system mount structure */
1623 struct xfs_trans
*tp
, /* transaction pointer */
1624 xfs_agnumber_t agno
, /* allocation group number */
1625 struct xfs_buf
**bpp
) /* allocation group hdr buf */
1627 struct xfs_agi
*agi
; /* allocation group header */
1628 struct xfs_perag
*pag
; /* per allocation group data */
1631 error
= xfs_read_agi(mp
, tp
, agno
, bpp
);
1635 agi
= XFS_BUF_TO_AGI(*bpp
);
1636 pag
= xfs_perag_get(mp
, agno
);
1637 if (!pag
->pagi_init
) {
1638 pag
->pagi_freecount
= be32_to_cpu(agi
->agi_freecount
);
1639 pag
->pagi_count
= be32_to_cpu(agi
->agi_count
);
1644 * It's possible for these to be out of sync if
1645 * we are in the middle of a forced shutdown.
1647 ASSERT(pag
->pagi_freecount
== be32_to_cpu(agi
->agi_freecount
) ||
1648 XFS_FORCED_SHUTDOWN(mp
));
1654 * Read in the agi to initialise the per-ag data in the mount structure
1657 xfs_ialloc_pagi_init(
1658 xfs_mount_t
*mp
, /* file system mount structure */
1659 xfs_trans_t
*tp
, /* transaction pointer */
1660 xfs_agnumber_t agno
) /* allocation group number */
1662 xfs_buf_t
*bp
= NULL
;
1665 error
= xfs_ialloc_read_agi(mp
, tp
, agno
, &bp
);
1669 xfs_trans_brelse(tp
, bp
);