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"
27 #include "xfs_mount.h"
28 #include "xfs_bmap_btree.h"
29 #include "xfs_alloc_btree.h"
30 #include "xfs_ialloc_btree.h"
31 #include "xfs_dinode.h"
32 #include "xfs_inode.h"
33 #include "xfs_btree.h"
34 #include "xfs_ialloc.h"
35 #include "xfs_alloc.h"
36 #include "xfs_rtalloc.h"
37 #include "xfs_error.h"
39 #include "xfs_cksum.h"
40 #include "xfs_buf_item.h"
41 #include "xfs_icreate_item.h"
45 * Allocation group level functions.
48 xfs_ialloc_cluster_alignment(
49 xfs_alloc_arg_t
*args
)
51 if (xfs_sb_version_hasalign(&args
->mp
->m_sb
) &&
52 args
->mp
->m_sb
.sb_inoalignmt
>=
53 XFS_B_TO_FSBT(args
->mp
, XFS_INODE_CLUSTER_SIZE(args
->mp
)))
54 return args
->mp
->m_sb
.sb_inoalignmt
;
59 * Lookup a record by ino in the btree given by cur.
63 struct xfs_btree_cur
*cur
, /* btree cursor */
64 xfs_agino_t ino
, /* starting inode of chunk */
65 xfs_lookup_t dir
, /* <=, >=, == */
66 int *stat
) /* success/failure */
68 cur
->bc_rec
.i
.ir_startino
= ino
;
69 cur
->bc_rec
.i
.ir_freecount
= 0;
70 cur
->bc_rec
.i
.ir_free
= 0;
71 return xfs_btree_lookup(cur
, dir
, stat
);
75 * Update the record referred to by cur to the value given.
76 * This either works (return 0) or gets an EFSCORRUPTED error.
78 STATIC
int /* error */
80 struct xfs_btree_cur
*cur
, /* btree cursor */
81 xfs_inobt_rec_incore_t
*irec
) /* btree record */
83 union xfs_btree_rec rec
;
85 rec
.inobt
.ir_startino
= cpu_to_be32(irec
->ir_startino
);
86 rec
.inobt
.ir_freecount
= cpu_to_be32(irec
->ir_freecount
);
87 rec
.inobt
.ir_free
= cpu_to_be64(irec
->ir_free
);
88 return xfs_btree_update(cur
, &rec
);
92 * Get the data from the pointed-to record.
96 struct xfs_btree_cur
*cur
, /* btree cursor */
97 xfs_inobt_rec_incore_t
*irec
, /* btree record */
98 int *stat
) /* output: success/failure */
100 union xfs_btree_rec
*rec
;
103 error
= xfs_btree_get_rec(cur
, &rec
, stat
);
104 if (!error
&& *stat
== 1) {
105 irec
->ir_startino
= be32_to_cpu(rec
->inobt
.ir_startino
);
106 irec
->ir_freecount
= be32_to_cpu(rec
->inobt
.ir_freecount
);
107 irec
->ir_free
= be64_to_cpu(rec
->inobt
.ir_free
);
113 * Verify that the number of free inodes in the AGI is correct.
117 xfs_check_agi_freecount(
118 struct xfs_btree_cur
*cur
,
121 if (cur
->bc_nlevels
== 1) {
122 xfs_inobt_rec_incore_t rec
;
127 error
= xfs_inobt_lookup(cur
, 0, XFS_LOOKUP_GE
, &i
);
132 error
= xfs_inobt_get_rec(cur
, &rec
, &i
);
137 freecount
+= rec
.ir_freecount
;
138 error
= xfs_btree_increment(cur
, 0, &i
);
144 if (!XFS_FORCED_SHUTDOWN(cur
->bc_mp
))
145 ASSERT(freecount
== be32_to_cpu(agi
->agi_freecount
));
150 #define xfs_check_agi_freecount(cur, agi) 0
154 * Initialise a new set of inodes. When called without a transaction context
155 * (e.g. from recovery) we initiate a delayed write of the inode buffers rather
156 * than logging them (which in a transaction context puts them into the AIL
157 * for writeback rather than the xfsbufd queue).
160 xfs_ialloc_inode_init(
161 struct xfs_mount
*mp
,
162 struct xfs_trans
*tp
,
163 struct list_head
*buffer_list
,
166 xfs_agblock_t length
,
169 struct xfs_buf
*fbuf
;
170 struct xfs_dinode
*free
;
171 int blks_per_cluster
, nbufs
, ninodes
;
178 * Loop over the new block(s), filling in the inodes.
179 * For small block sizes, manipulate the inodes in buffers
180 * which are multiples of the blocks size.
182 if (mp
->m_sb
.sb_blocksize
>= XFS_INODE_CLUSTER_SIZE(mp
)) {
183 blks_per_cluster
= 1;
185 ninodes
= mp
->m_sb
.sb_inopblock
;
187 blks_per_cluster
= XFS_INODE_CLUSTER_SIZE(mp
) /
188 mp
->m_sb
.sb_blocksize
;
189 nbufs
= length
/ blks_per_cluster
;
190 ninodes
= blks_per_cluster
* mp
->m_sb
.sb_inopblock
;
194 * Figure out what version number to use in the inodes we create. If
195 * the superblock version has caught up to the one that supports the new
196 * inode format, then use the new inode version. Otherwise use the old
197 * version so that old kernels will continue to be able to use the file
200 * For v3 inodes, we also need to write the inode number into the inode,
201 * so calculate the first inode number of the chunk here as
202 * XFS_OFFBNO_TO_AGINO() only works within a filesystem block, not
203 * across multiple filesystem blocks (such as a cluster) and so cannot
204 * be used in the cluster buffer loop below.
206 * Further, because we are writing the inode directly into the buffer
207 * and calculating a CRC on the entire inode, we have ot log the entire
208 * inode so that the entire range the CRC covers is present in the log.
209 * That means for v3 inode we log the entire buffer rather than just the
212 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
214 ino
= XFS_AGINO_TO_INO(mp
, agno
,
215 XFS_OFFBNO_TO_AGINO(mp
, agbno
, 0));
218 * log the initialisation that is about to take place as an
219 * logical operation. This means the transaction does not
220 * need to log the physical changes to the inode buffers as log
221 * recovery will know what initialisation is actually needed.
222 * Hence we only need to log the buffers as "ordered" buffers so
223 * they track in the AIL as if they were physically logged.
226 xfs_icreate_log(tp
, agno
, agbno
, XFS_IALLOC_INODES(mp
),
227 mp
->m_sb
.sb_inodesize
, length
, gen
);
228 } else if (xfs_sb_version_hasnlink(&mp
->m_sb
))
233 for (j
= 0; j
< nbufs
; j
++) {
237 d
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
+ (j
* blks_per_cluster
));
238 fbuf
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
,
239 mp
->m_bsize
* blks_per_cluster
,
244 /* Initialize the inode buffers and log them appropriately. */
245 fbuf
->b_ops
= &xfs_inode_buf_ops
;
246 xfs_buf_zero(fbuf
, 0, BBTOB(fbuf
->b_length
));
247 for (i
= 0; i
< ninodes
; i
++) {
248 int ioffset
= i
<< mp
->m_sb
.sb_inodelog
;
249 uint isize
= xfs_dinode_size(version
);
251 free
= xfs_make_iptr(mp
, fbuf
, i
);
252 free
->di_magic
= cpu_to_be16(XFS_DINODE_MAGIC
);
253 free
->di_version
= version
;
254 free
->di_gen
= cpu_to_be32(gen
);
255 free
->di_next_unlinked
= cpu_to_be32(NULLAGINO
);
258 free
->di_ino
= cpu_to_be64(ino
);
260 uuid_copy(&free
->di_uuid
, &mp
->m_sb
.sb_uuid
);
261 xfs_dinode_calc_crc(mp
, free
);
263 /* just log the inode core */
264 xfs_trans_log_buf(tp
, fbuf
, ioffset
,
265 ioffset
+ isize
- 1);
271 * Mark the buffer as an inode allocation buffer so it
272 * sticks in AIL at the point of this allocation
273 * transaction. This ensures the they are on disk before
274 * the tail of the log can be moved past this
275 * transaction (i.e. by preventing relogging from moving
276 * it forward in the log).
278 xfs_trans_inode_alloc_buf(tp
, fbuf
);
281 * Mark the buffer as ordered so that they are
282 * not physically logged in the transaction but
283 * still tracked in the AIL as part of the
284 * transaction and pin the log appropriately.
286 xfs_trans_ordered_buf(tp
, fbuf
);
287 xfs_trans_log_buf(tp
, fbuf
, 0,
288 BBTOB(fbuf
->b_length
) - 1);
291 fbuf
->b_flags
|= XBF_DONE
;
292 xfs_buf_delwri_queue(fbuf
, buffer_list
);
300 * Allocate new inodes in the allocation group specified by agbp.
301 * Return 0 for success, else error code.
303 STATIC
int /* error code or 0 */
305 xfs_trans_t
*tp
, /* transaction pointer */
306 xfs_buf_t
*agbp
, /* alloc group buffer */
309 xfs_agi_t
*agi
; /* allocation group header */
310 xfs_alloc_arg_t args
; /* allocation argument structure */
311 xfs_btree_cur_t
*cur
; /* inode btree cursor */
315 xfs_agino_t newino
; /* new first inode's number */
316 xfs_agino_t newlen
; /* new number of inodes */
317 xfs_agino_t thisino
; /* current inode number, for loop */
318 int isaligned
= 0; /* inode allocation at stripe unit */
320 struct xfs_perag
*pag
;
322 memset(&args
, 0, sizeof(args
));
324 args
.mp
= tp
->t_mountp
;
327 * Locking will ensure that we don't have two callers in here
330 newlen
= XFS_IALLOC_INODES(args
.mp
);
331 if (args
.mp
->m_maxicount
&&
332 args
.mp
->m_sb
.sb_icount
+ newlen
> args
.mp
->m_maxicount
)
333 return XFS_ERROR(ENOSPC
);
334 args
.minlen
= args
.maxlen
= XFS_IALLOC_BLOCKS(args
.mp
);
336 * First try to allocate inodes contiguous with the last-allocated
337 * chunk of inodes. If the filesystem is striped, this will fill
338 * an entire stripe unit with inodes.
340 agi
= XFS_BUF_TO_AGI(agbp
);
341 newino
= be32_to_cpu(agi
->agi_newino
);
342 agno
= be32_to_cpu(agi
->agi_seqno
);
343 args
.agbno
= XFS_AGINO_TO_AGBNO(args
.mp
, newino
) +
344 XFS_IALLOC_BLOCKS(args
.mp
);
345 if (likely(newino
!= NULLAGINO
&&
346 (args
.agbno
< be32_to_cpu(agi
->agi_length
)))) {
347 args
.fsbno
= XFS_AGB_TO_FSB(args
.mp
, agno
, args
.agbno
);
348 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
352 * We need to take into account alignment here to ensure that
353 * we don't modify the free list if we fail to have an exact
354 * block. If we don't have an exact match, and every oher
355 * attempt allocation attempt fails, we'll end up cancelling
356 * a dirty transaction and shutting down.
358 * For an exact allocation, alignment must be 1,
359 * however we need to take cluster alignment into account when
360 * fixing up the freelist. Use the minalignslop field to
361 * indicate that extra blocks might be required for alignment,
362 * but not to use them in the actual exact allocation.
365 args
.minalignslop
= xfs_ialloc_cluster_alignment(&args
) - 1;
367 /* Allow space for the inode btree to split. */
368 args
.minleft
= args
.mp
->m_in_maxlevels
- 1;
369 if ((error
= xfs_alloc_vextent(&args
)))
372 args
.fsbno
= NULLFSBLOCK
;
374 if (unlikely(args
.fsbno
== NULLFSBLOCK
)) {
376 * Set the alignment for the allocation.
377 * If stripe alignment is turned on then align at stripe unit
379 * If the cluster size is smaller than a filesystem block
380 * then we're doing I/O for inodes in filesystem block size
381 * pieces, so don't need alignment anyway.
384 if (args
.mp
->m_sinoalign
) {
385 ASSERT(!(args
.mp
->m_flags
& XFS_MOUNT_NOALIGN
));
386 args
.alignment
= args
.mp
->m_dalign
;
389 args
.alignment
= xfs_ialloc_cluster_alignment(&args
);
391 * Need to figure out where to allocate the inode blocks.
392 * Ideally they should be spaced out through the a.g.
393 * For now, just allocate blocks up front.
395 args
.agbno
= be32_to_cpu(agi
->agi_root
);
396 args
.fsbno
= XFS_AGB_TO_FSB(args
.mp
, agno
, args
.agbno
);
398 * Allocate a fixed-size extent of inodes.
400 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
403 * Allow space for the inode btree to split.
405 args
.minleft
= args
.mp
->m_in_maxlevels
- 1;
406 if ((error
= xfs_alloc_vextent(&args
)))
411 * If stripe alignment is turned on, then try again with cluster
414 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
415 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
416 args
.agbno
= be32_to_cpu(agi
->agi_root
);
417 args
.fsbno
= XFS_AGB_TO_FSB(args
.mp
, agno
, args
.agbno
);
418 args
.alignment
= xfs_ialloc_cluster_alignment(&args
);
419 if ((error
= xfs_alloc_vextent(&args
)))
423 if (args
.fsbno
== NULLFSBLOCK
) {
427 ASSERT(args
.len
== args
.minlen
);
430 * Stamp and write the inode buffers.
432 * Seed the new inode cluster with a random generation number. This
433 * prevents short-term reuse of generation numbers if a chunk is
434 * freed and then immediately reallocated. We use random numbers
435 * rather than a linear progression to prevent the next generation
436 * number from being easily guessable.
438 error
= xfs_ialloc_inode_init(args
.mp
, tp
, NULL
, agno
, args
.agbno
,
439 args
.len
, prandom_u32());
444 * Convert the results.
446 newino
= XFS_OFFBNO_TO_AGINO(args
.mp
, args
.agbno
, 0);
447 be32_add_cpu(&agi
->agi_count
, newlen
);
448 be32_add_cpu(&agi
->agi_freecount
, newlen
);
449 pag
= xfs_perag_get(args
.mp
, agno
);
450 pag
->pagi_freecount
+= newlen
;
452 agi
->agi_newino
= cpu_to_be32(newino
);
455 * Insert records describing the new inode chunk into the btree.
457 cur
= xfs_inobt_init_cursor(args
.mp
, tp
, agbp
, agno
);
458 for (thisino
= newino
;
459 thisino
< newino
+ newlen
;
460 thisino
+= XFS_INODES_PER_CHUNK
) {
461 cur
->bc_rec
.i
.ir_startino
= thisino
;
462 cur
->bc_rec
.i
.ir_freecount
= XFS_INODES_PER_CHUNK
;
463 cur
->bc_rec
.i
.ir_free
= XFS_INOBT_ALL_FREE
;
464 error
= xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, &i
);
466 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
470 error
= xfs_btree_insert(cur
, &i
);
472 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
477 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
479 * Log allocation group header fields
481 xfs_ialloc_log_agi(tp
, agbp
,
482 XFS_AGI_COUNT
| XFS_AGI_FREECOUNT
| XFS_AGI_NEWINO
);
484 * Modify/log superblock values for inode count and inode free count.
486 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_ICOUNT
, (long)newlen
);
487 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_IFREE
, (long)newlen
);
492 STATIC xfs_agnumber_t
498 spin_lock(&mp
->m_agirotor_lock
);
499 agno
= mp
->m_agirotor
;
500 if (++mp
->m_agirotor
>= mp
->m_maxagi
)
502 spin_unlock(&mp
->m_agirotor_lock
);
508 * Select an allocation group to look for a free inode in, based on the parent
509 * inode and then mode. Return the allocation group buffer.
511 STATIC xfs_agnumber_t
512 xfs_ialloc_ag_select(
513 xfs_trans_t
*tp
, /* transaction pointer */
514 xfs_ino_t parent
, /* parent directory inode number */
515 umode_t mode
, /* bits set to indicate file type */
516 int okalloc
) /* ok to allocate more space */
518 xfs_agnumber_t agcount
; /* number of ag's in the filesystem */
519 xfs_agnumber_t agno
; /* current ag number */
520 int flags
; /* alloc buffer locking flags */
521 xfs_extlen_t ineed
; /* blocks needed for inode allocation */
522 xfs_extlen_t longest
= 0; /* longest extent available */
523 xfs_mount_t
*mp
; /* mount point structure */
524 int needspace
; /* file mode implies space allocated */
525 xfs_perag_t
*pag
; /* per allocation group data */
526 xfs_agnumber_t pagno
; /* parent (starting) ag number */
530 * Files of these types need at least one block if length > 0
531 * (and they won't fit in the inode, but that's hard to figure out).
533 needspace
= S_ISDIR(mode
) || S_ISREG(mode
) || S_ISLNK(mode
);
535 agcount
= mp
->m_maxagi
;
537 pagno
= xfs_ialloc_next_ag(mp
);
539 pagno
= XFS_INO_TO_AGNO(mp
, parent
);
540 if (pagno
>= agcount
)
544 ASSERT(pagno
< agcount
);
547 * Loop through allocation groups, looking for one with a little
548 * free space in it. Note we don't look for free inodes, exactly.
549 * Instead, we include whether there is a need to allocate inodes
550 * to mean that blocks must be allocated for them,
551 * if none are currently free.
554 flags
= XFS_ALLOC_FLAG_TRYLOCK
;
556 pag
= xfs_perag_get(mp
, agno
);
557 if (!pag
->pagi_inodeok
) {
558 xfs_ialloc_next_ag(mp
);
562 if (!pag
->pagi_init
) {
563 error
= xfs_ialloc_pagi_init(mp
, tp
, agno
);
568 if (pag
->pagi_freecount
) {
576 if (!pag
->pagf_init
) {
577 error
= xfs_alloc_pagf_init(mp
, tp
, agno
, flags
);
583 * Is there enough free space for the file plus a block of
584 * inodes? (if we need to allocate some)?
586 ineed
= XFS_IALLOC_BLOCKS(mp
);
587 longest
= pag
->pagf_longest
;
589 longest
= pag
->pagf_flcount
> 0;
591 if (pag
->pagf_freeblks
>= needspace
+ ineed
&&
599 * No point in iterating over the rest, if we're shutting
602 if (XFS_FORCED_SHUTDOWN(mp
))
616 * Try to retrieve the next record to the left/right from the current one.
620 struct xfs_btree_cur
*cur
,
621 xfs_inobt_rec_incore_t
*rec
,
629 error
= xfs_btree_decrement(cur
, 0, &i
);
631 error
= xfs_btree_increment(cur
, 0, &i
);
637 error
= xfs_inobt_get_rec(cur
, rec
, &i
);
640 XFS_WANT_CORRUPTED_RETURN(i
== 1);
648 struct xfs_btree_cur
*cur
,
650 xfs_inobt_rec_incore_t
*rec
,
656 error
= xfs_inobt_lookup(cur
, agino
, XFS_LOOKUP_EQ
, &i
);
661 error
= xfs_inobt_get_rec(cur
, rec
, &i
);
664 XFS_WANT_CORRUPTED_RETURN(i
== 1);
673 * The caller selected an AG for us, and made sure that free inodes are
678 struct xfs_trans
*tp
,
679 struct xfs_buf
*agbp
,
683 struct xfs_mount
*mp
= tp
->t_mountp
;
684 struct xfs_agi
*agi
= XFS_BUF_TO_AGI(agbp
);
685 xfs_agnumber_t agno
= be32_to_cpu(agi
->agi_seqno
);
686 xfs_agnumber_t pagno
= XFS_INO_TO_AGNO(mp
, parent
);
687 xfs_agino_t pagino
= XFS_INO_TO_AGINO(mp
, parent
);
688 struct xfs_perag
*pag
;
689 struct xfs_btree_cur
*cur
, *tcur
;
690 struct xfs_inobt_rec_incore rec
, trec
;
696 pag
= xfs_perag_get(mp
, agno
);
698 ASSERT(pag
->pagi_init
);
699 ASSERT(pag
->pagi_inodeok
);
700 ASSERT(pag
->pagi_freecount
> 0);
703 cur
= xfs_inobt_init_cursor(mp
, tp
, agbp
, agno
);
705 * If pagino is 0 (this is the root inode allocation) use newino.
706 * This must work because we've just allocated some.
709 pagino
= be32_to_cpu(agi
->agi_newino
);
711 error
= xfs_check_agi_freecount(cur
, agi
);
716 * If in the same AG as the parent, try to get near the parent.
719 int doneleft
; /* done, to the left */
720 int doneright
; /* done, to the right */
721 int searchdistance
= 10;
723 error
= xfs_inobt_lookup(cur
, pagino
, XFS_LOOKUP_LE
, &i
);
726 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
728 error
= xfs_inobt_get_rec(cur
, &rec
, &j
);
731 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
733 if (rec
.ir_freecount
> 0) {
735 * Found a free inode in the same chunk
736 * as the parent, done.
743 * In the same AG as parent, but parent's chunk is full.
746 /* duplicate the cursor, search left & right simultaneously */
747 error
= xfs_btree_dup_cursor(cur
, &tcur
);
752 * Skip to last blocks looked up if same parent inode.
754 if (pagino
!= NULLAGINO
&&
755 pag
->pagl_pagino
== pagino
&&
756 pag
->pagl_leftrec
!= NULLAGINO
&&
757 pag
->pagl_rightrec
!= NULLAGINO
) {
758 error
= xfs_ialloc_get_rec(tcur
, pag
->pagl_leftrec
,
763 error
= xfs_ialloc_get_rec(cur
, pag
->pagl_rightrec
,
768 /* search left with tcur, back up 1 record */
769 error
= xfs_ialloc_next_rec(tcur
, &trec
, &doneleft
, 1);
773 /* search right with cur, go forward 1 record. */
774 error
= xfs_ialloc_next_rec(cur
, &rec
, &doneright
, 0);
780 * Loop until we find an inode chunk with a free inode.
782 while (!doneleft
|| !doneright
) {
783 int useleft
; /* using left inode chunk this time */
785 if (!--searchdistance
) {
787 * Not in range - save last search
788 * location and allocate a new inode
790 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
791 pag
->pagl_leftrec
= trec
.ir_startino
;
792 pag
->pagl_rightrec
= rec
.ir_startino
;
793 pag
->pagl_pagino
= pagino
;
797 /* figure out the closer block if both are valid. */
798 if (!doneleft
&& !doneright
) {
800 (trec
.ir_startino
+ XFS_INODES_PER_CHUNK
- 1) <
801 rec
.ir_startino
- pagino
;
806 /* free inodes to the left? */
807 if (useleft
&& trec
.ir_freecount
) {
809 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
812 pag
->pagl_leftrec
= trec
.ir_startino
;
813 pag
->pagl_rightrec
= rec
.ir_startino
;
814 pag
->pagl_pagino
= pagino
;
818 /* free inodes to the right? */
819 if (!useleft
&& rec
.ir_freecount
) {
820 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
822 pag
->pagl_leftrec
= trec
.ir_startino
;
823 pag
->pagl_rightrec
= rec
.ir_startino
;
824 pag
->pagl_pagino
= pagino
;
828 /* get next record to check */
830 error
= xfs_ialloc_next_rec(tcur
, &trec
,
833 error
= xfs_ialloc_next_rec(cur
, &rec
,
841 * We've reached the end of the btree. because
842 * we are only searching a small chunk of the
843 * btree each search, there is obviously free
844 * inodes closer to the parent inode than we
845 * are now. restart the search again.
847 pag
->pagl_pagino
= NULLAGINO
;
848 pag
->pagl_leftrec
= NULLAGINO
;
849 pag
->pagl_rightrec
= NULLAGINO
;
850 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
851 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
856 * In a different AG from the parent.
857 * See if the most recently allocated block has any free.
860 if (agi
->agi_newino
!= cpu_to_be32(NULLAGINO
)) {
861 error
= xfs_inobt_lookup(cur
, be32_to_cpu(agi
->agi_newino
),
867 error
= xfs_inobt_get_rec(cur
, &rec
, &j
);
871 if (j
== 1 && rec
.ir_freecount
> 0) {
873 * The last chunk allocated in the group
874 * still has a free inode.
882 * None left in the last group, search the whole AG
884 error
= xfs_inobt_lookup(cur
, 0, XFS_LOOKUP_GE
, &i
);
887 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
890 error
= xfs_inobt_get_rec(cur
, &rec
, &i
);
893 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
894 if (rec
.ir_freecount
> 0)
896 error
= xfs_btree_increment(cur
, 0, &i
);
899 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
903 offset
= xfs_lowbit64(rec
.ir_free
);
905 ASSERT(offset
< XFS_INODES_PER_CHUNK
);
906 ASSERT((XFS_AGINO_TO_OFFSET(mp
, rec
.ir_startino
) %
907 XFS_INODES_PER_CHUNK
) == 0);
908 ino
= XFS_AGINO_TO_INO(mp
, agno
, rec
.ir_startino
+ offset
);
909 rec
.ir_free
&= ~XFS_INOBT_MASK(offset
);
911 error
= xfs_inobt_update(cur
, &rec
);
914 be32_add_cpu(&agi
->agi_freecount
, -1);
915 xfs_ialloc_log_agi(tp
, agbp
, XFS_AGI_FREECOUNT
);
916 pag
->pagi_freecount
--;
918 error
= xfs_check_agi_freecount(cur
, agi
);
922 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
923 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_IFREE
, -1);
928 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
930 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
936 * Allocate an inode on disk.
938 * Mode is used to tell whether the new inode will need space, and whether it
941 * This function is designed to be called twice if it has to do an allocation
942 * to make more free inodes. On the first call, *IO_agbp should be set to NULL.
943 * If an inode is available without having to performn an allocation, an inode
944 * number is returned. In this case, *IO_agbp is set to NULL. If an allocation
945 * needs to be done, xfs_dialloc returns the current AGI buffer in *IO_agbp.
946 * The caller should then commit the current transaction, allocate a
947 * new transaction, and call xfs_dialloc() again, passing in the previous value
948 * of *IO_agbp. IO_agbp should be held across the transactions. Since the AGI
949 * buffer is locked across the two calls, the second call is guaranteed to have
950 * a free inode available.
952 * Once we successfully pick an inode its number is returned and the on-disk
953 * data structures are updated. The inode itself is not read in, since doing so
954 * would break ordering constraints with xfs_reclaim.
958 struct xfs_trans
*tp
,
962 struct xfs_buf
**IO_agbp
,
965 struct xfs_mount
*mp
= tp
->t_mountp
;
966 struct xfs_buf
*agbp
;
971 xfs_agnumber_t start_agno
;
972 struct xfs_perag
*pag
;
976 * If the caller passes in a pointer to the AGI buffer,
977 * continue where we left off before. In this case, we
978 * know that the allocation group has free inodes.
985 * We do not have an agbp, so select an initial allocation
986 * group for inode allocation.
988 start_agno
= xfs_ialloc_ag_select(tp
, parent
, mode
, okalloc
);
989 if (start_agno
== NULLAGNUMBER
) {
995 * If we have already hit the ceiling of inode blocks then clear
996 * okalloc so we scan all available agi structures for a free
999 if (mp
->m_maxicount
&&
1000 mp
->m_sb
.sb_icount
+ XFS_IALLOC_INODES(mp
) > mp
->m_maxicount
) {
1006 * Loop until we find an allocation group that either has free inodes
1007 * or in which we can allocate some inodes. Iterate through the
1008 * allocation groups upward, wrapping at the end.
1012 pag
= xfs_perag_get(mp
, agno
);
1013 if (!pag
->pagi_inodeok
) {
1014 xfs_ialloc_next_ag(mp
);
1018 if (!pag
->pagi_init
) {
1019 error
= xfs_ialloc_pagi_init(mp
, tp
, agno
);
1025 * Do a first racy fast path check if this AG is usable.
1027 if (!pag
->pagi_freecount
&& !okalloc
)
1031 * Then read in the AGI buffer and recheck with the AGI buffer
1034 error
= xfs_ialloc_read_agi(mp
, tp
, agno
, &agbp
);
1038 if (pag
->pagi_freecount
) {
1044 goto nextag_relse_buffer
;
1047 error
= xfs_ialloc_ag_alloc(tp
, agbp
, &ialloced
);
1049 xfs_trans_brelse(tp
, agbp
);
1051 if (error
!= ENOSPC
)
1061 * We successfully allocated some inodes, return
1062 * the current context to the caller so that it
1063 * can commit the current transaction and call
1064 * us again where we left off.
1066 ASSERT(pag
->pagi_freecount
> 0);
1074 nextag_relse_buffer
:
1075 xfs_trans_brelse(tp
, agbp
);
1078 if (++agno
== mp
->m_sb
.sb_agcount
)
1080 if (agno
== start_agno
) {
1082 return noroom
? ENOSPC
: 0;
1088 return xfs_dialloc_ag(tp
, agbp
, parent
, inop
);
1091 return XFS_ERROR(error
);
1095 * Free disk inode. Carefully avoids touching the incore inode, all
1096 * manipulations incore are the caller's responsibility.
1097 * The on-disk inode is not changed by this operation, only the
1098 * btree (free inode mask) is changed.
1102 xfs_trans_t
*tp
, /* transaction pointer */
1103 xfs_ino_t inode
, /* inode to be freed */
1104 xfs_bmap_free_t
*flist
, /* extents to free */
1105 int *delete, /* set if inode cluster was deleted */
1106 xfs_ino_t
*first_ino
) /* first inode in deleted cluster */
1109 xfs_agblock_t agbno
; /* block number containing inode */
1110 xfs_buf_t
*agbp
; /* buffer containing allocation group header */
1111 xfs_agino_t agino
; /* inode number relative to allocation group */
1112 xfs_agnumber_t agno
; /* allocation group number */
1113 xfs_agi_t
*agi
; /* allocation group header */
1114 xfs_btree_cur_t
*cur
; /* inode btree cursor */
1115 int error
; /* error return value */
1116 int i
; /* result code */
1117 int ilen
; /* inodes in an inode cluster */
1118 xfs_mount_t
*mp
; /* mount structure for filesystem */
1119 int off
; /* offset of inode in inode chunk */
1120 xfs_inobt_rec_incore_t rec
; /* btree record */
1121 struct xfs_perag
*pag
;
1126 * Break up inode number into its components.
1128 agno
= XFS_INO_TO_AGNO(mp
, inode
);
1129 if (agno
>= mp
->m_sb
.sb_agcount
) {
1130 xfs_warn(mp
, "%s: agno >= mp->m_sb.sb_agcount (%d >= %d).",
1131 __func__
, agno
, mp
->m_sb
.sb_agcount
);
1133 return XFS_ERROR(EINVAL
);
1135 agino
= XFS_INO_TO_AGINO(mp
, inode
);
1136 if (inode
!= XFS_AGINO_TO_INO(mp
, agno
, agino
)) {
1137 xfs_warn(mp
, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).",
1138 __func__
, (unsigned long long)inode
,
1139 (unsigned long long)XFS_AGINO_TO_INO(mp
, agno
, agino
));
1141 return XFS_ERROR(EINVAL
);
1143 agbno
= XFS_AGINO_TO_AGBNO(mp
, agino
);
1144 if (agbno
>= mp
->m_sb
.sb_agblocks
) {
1145 xfs_warn(mp
, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).",
1146 __func__
, agbno
, mp
->m_sb
.sb_agblocks
);
1148 return XFS_ERROR(EINVAL
);
1151 * Get the allocation group header.
1153 error
= xfs_ialloc_read_agi(mp
, tp
, agno
, &agbp
);
1155 xfs_warn(mp
, "%s: xfs_ialloc_read_agi() returned error %d.",
1159 agi
= XFS_BUF_TO_AGI(agbp
);
1160 ASSERT(agi
->agi_magicnum
== cpu_to_be32(XFS_AGI_MAGIC
));
1161 ASSERT(agbno
< be32_to_cpu(agi
->agi_length
));
1163 * Initialize the cursor.
1165 cur
= xfs_inobt_init_cursor(mp
, tp
, agbp
, agno
);
1167 error
= xfs_check_agi_freecount(cur
, agi
);
1172 * Look for the entry describing this inode.
1174 if ((error
= xfs_inobt_lookup(cur
, agino
, XFS_LOOKUP_LE
, &i
))) {
1175 xfs_warn(mp
, "%s: xfs_inobt_lookup() returned error %d.",
1179 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1180 error
= xfs_inobt_get_rec(cur
, &rec
, &i
);
1182 xfs_warn(mp
, "%s: xfs_inobt_get_rec() returned error %d.",
1186 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1188 * Get the offset in the inode chunk.
1190 off
= agino
- rec
.ir_startino
;
1191 ASSERT(off
>= 0 && off
< XFS_INODES_PER_CHUNK
);
1192 ASSERT(!(rec
.ir_free
& XFS_INOBT_MASK(off
)));
1194 * Mark the inode free & increment the count.
1196 rec
.ir_free
|= XFS_INOBT_MASK(off
);
1200 * When an inode cluster is free, it becomes eligible for removal
1202 if (!(mp
->m_flags
& XFS_MOUNT_IKEEP
) &&
1203 (rec
.ir_freecount
== XFS_IALLOC_INODES(mp
))) {
1206 *first_ino
= XFS_AGINO_TO_INO(mp
, agno
, rec
.ir_startino
);
1209 * Remove the inode cluster from the AGI B+Tree, adjust the
1210 * AGI and Superblock inode counts, and mark the disk space
1211 * to be freed when the transaction is committed.
1213 ilen
= XFS_IALLOC_INODES(mp
);
1214 be32_add_cpu(&agi
->agi_count
, -ilen
);
1215 be32_add_cpu(&agi
->agi_freecount
, -(ilen
- 1));
1216 xfs_ialloc_log_agi(tp
, agbp
, XFS_AGI_COUNT
| XFS_AGI_FREECOUNT
);
1217 pag
= xfs_perag_get(mp
, agno
);
1218 pag
->pagi_freecount
-= ilen
- 1;
1220 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_ICOUNT
, -ilen
);
1221 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_IFREE
, -(ilen
- 1));
1223 if ((error
= xfs_btree_delete(cur
, &i
))) {
1224 xfs_warn(mp
, "%s: xfs_btree_delete returned error %d.",
1229 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp
,
1230 agno
, XFS_INO_TO_AGBNO(mp
,rec
.ir_startino
)),
1231 XFS_IALLOC_BLOCKS(mp
), flist
, mp
);
1235 error
= xfs_inobt_update(cur
, &rec
);
1237 xfs_warn(mp
, "%s: xfs_inobt_update returned error %d.",
1243 * Change the inode free counts and log the ag/sb changes.
1245 be32_add_cpu(&agi
->agi_freecount
, 1);
1246 xfs_ialloc_log_agi(tp
, agbp
, XFS_AGI_FREECOUNT
);
1247 pag
= xfs_perag_get(mp
, agno
);
1248 pag
->pagi_freecount
++;
1250 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_IFREE
, 1);
1253 error
= xfs_check_agi_freecount(cur
, agi
);
1257 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1261 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1267 struct xfs_mount
*mp
,
1268 struct xfs_trans
*tp
,
1269 xfs_agnumber_t agno
,
1271 xfs_agblock_t agbno
,
1272 xfs_agblock_t
*chunk_agbno
,
1273 xfs_agblock_t
*offset_agbno
,
1276 struct xfs_inobt_rec_incore rec
;
1277 struct xfs_btree_cur
*cur
;
1278 struct xfs_buf
*agbp
;
1282 error
= xfs_ialloc_read_agi(mp
, tp
, agno
, &agbp
);
1285 "%s: xfs_ialloc_read_agi() returned error %d, agno %d",
1286 __func__
, error
, agno
);
1291 * Lookup the inode record for the given agino. If the record cannot be
1292 * found, then it's an invalid inode number and we should abort. Once
1293 * we have a record, we need to ensure it contains the inode number
1294 * we are looking up.
1296 cur
= xfs_inobt_init_cursor(mp
, tp
, agbp
, agno
);
1297 error
= xfs_inobt_lookup(cur
, agino
, XFS_LOOKUP_LE
, &i
);
1300 error
= xfs_inobt_get_rec(cur
, &rec
, &i
);
1301 if (!error
&& i
== 0)
1305 xfs_trans_brelse(tp
, agbp
);
1306 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1310 /* check that the returned record contains the required inode */
1311 if (rec
.ir_startino
> agino
||
1312 rec
.ir_startino
+ XFS_IALLOC_INODES(mp
) <= agino
)
1315 /* for untrusted inodes check it is allocated first */
1316 if ((flags
& XFS_IGET_UNTRUSTED
) &&
1317 (rec
.ir_free
& XFS_INOBT_MASK(agino
- rec
.ir_startino
)))
1320 *chunk_agbno
= XFS_AGINO_TO_AGBNO(mp
, rec
.ir_startino
);
1321 *offset_agbno
= agbno
- *chunk_agbno
;
1326 * Return the location of the inode in imap, for mapping it into a buffer.
1330 xfs_mount_t
*mp
, /* file system mount structure */
1331 xfs_trans_t
*tp
, /* transaction pointer */
1332 xfs_ino_t ino
, /* inode to locate */
1333 struct xfs_imap
*imap
, /* location map structure */
1334 uint flags
) /* flags for inode btree lookup */
1336 xfs_agblock_t agbno
; /* block number of inode in the alloc group */
1337 xfs_agino_t agino
; /* inode number within alloc group */
1338 xfs_agnumber_t agno
; /* allocation group number */
1339 int blks_per_cluster
; /* num blocks per inode cluster */
1340 xfs_agblock_t chunk_agbno
; /* first block in inode chunk */
1341 xfs_agblock_t cluster_agbno
; /* first block in inode cluster */
1342 int error
; /* error code */
1343 int offset
; /* index of inode in its buffer */
1344 int offset_agbno
; /* blks from chunk start to inode */
1346 ASSERT(ino
!= NULLFSINO
);
1349 * Split up the inode number into its parts.
1351 agno
= XFS_INO_TO_AGNO(mp
, ino
);
1352 agino
= XFS_INO_TO_AGINO(mp
, ino
);
1353 agbno
= XFS_AGINO_TO_AGBNO(mp
, agino
);
1354 if (agno
>= mp
->m_sb
.sb_agcount
|| agbno
>= mp
->m_sb
.sb_agblocks
||
1355 ino
!= XFS_AGINO_TO_INO(mp
, agno
, agino
)) {
1358 * Don't output diagnostic information for untrusted inodes
1359 * as they can be invalid without implying corruption.
1361 if (flags
& XFS_IGET_UNTRUSTED
)
1362 return XFS_ERROR(EINVAL
);
1363 if (agno
>= mp
->m_sb
.sb_agcount
) {
1365 "%s: agno (%d) >= mp->m_sb.sb_agcount (%d)",
1366 __func__
, agno
, mp
->m_sb
.sb_agcount
);
1368 if (agbno
>= mp
->m_sb
.sb_agblocks
) {
1370 "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)",
1371 __func__
, (unsigned long long)agbno
,
1372 (unsigned long)mp
->m_sb
.sb_agblocks
);
1374 if (ino
!= XFS_AGINO_TO_INO(mp
, agno
, agino
)) {
1376 "%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)",
1378 XFS_AGINO_TO_INO(mp
, agno
, agino
));
1382 return XFS_ERROR(EINVAL
);
1385 blks_per_cluster
= XFS_INODE_CLUSTER_SIZE(mp
) >> mp
->m_sb
.sb_blocklog
;
1388 * For bulkstat and handle lookups, we have an untrusted inode number
1389 * that we have to verify is valid. We cannot do this just by reading
1390 * the inode buffer as it may have been unlinked and removed leaving
1391 * inodes in stale state on disk. Hence we have to do a btree lookup
1392 * in all cases where an untrusted inode number is passed.
1394 if (flags
& XFS_IGET_UNTRUSTED
) {
1395 error
= xfs_imap_lookup(mp
, tp
, agno
, agino
, agbno
,
1396 &chunk_agbno
, &offset_agbno
, flags
);
1403 * If the inode cluster size is the same as the blocksize or
1404 * smaller we get to the buffer by simple arithmetics.
1406 if (XFS_INODE_CLUSTER_SIZE(mp
) <= mp
->m_sb
.sb_blocksize
) {
1407 offset
= XFS_INO_TO_OFFSET(mp
, ino
);
1408 ASSERT(offset
< mp
->m_sb
.sb_inopblock
);
1410 imap
->im_blkno
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
);
1411 imap
->im_len
= XFS_FSB_TO_BB(mp
, 1);
1412 imap
->im_boffset
= (ushort
)(offset
<< mp
->m_sb
.sb_inodelog
);
1417 * If the inode chunks are aligned then use simple maths to
1418 * find the location. Otherwise we have to do a btree
1419 * lookup to find the location.
1421 if (mp
->m_inoalign_mask
) {
1422 offset_agbno
= agbno
& mp
->m_inoalign_mask
;
1423 chunk_agbno
= agbno
- offset_agbno
;
1425 error
= xfs_imap_lookup(mp
, tp
, agno
, agino
, agbno
,
1426 &chunk_agbno
, &offset_agbno
, flags
);
1432 ASSERT(agbno
>= chunk_agbno
);
1433 cluster_agbno
= chunk_agbno
+
1434 ((offset_agbno
/ blks_per_cluster
) * blks_per_cluster
);
1435 offset
= ((agbno
- cluster_agbno
) * mp
->m_sb
.sb_inopblock
) +
1436 XFS_INO_TO_OFFSET(mp
, ino
);
1438 imap
->im_blkno
= XFS_AGB_TO_DADDR(mp
, agno
, cluster_agbno
);
1439 imap
->im_len
= XFS_FSB_TO_BB(mp
, blks_per_cluster
);
1440 imap
->im_boffset
= (ushort
)(offset
<< mp
->m_sb
.sb_inodelog
);
1443 * If the inode number maps to a block outside the bounds
1444 * of the file system then return NULL rather than calling
1445 * read_buf and panicing when we get an error from the
1448 if ((imap
->im_blkno
+ imap
->im_len
) >
1449 XFS_FSB_TO_BB(mp
, mp
->m_sb
.sb_dblocks
)) {
1451 "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)",
1452 __func__
, (unsigned long long) imap
->im_blkno
,
1453 (unsigned long long) imap
->im_len
,
1454 XFS_FSB_TO_BB(mp
, mp
->m_sb
.sb_dblocks
));
1455 return XFS_ERROR(EINVAL
);
1461 * Compute and fill in value of m_in_maxlevels.
1464 xfs_ialloc_compute_maxlevels(
1465 xfs_mount_t
*mp
) /* file system mount structure */
1473 maxleafents
= (1LL << XFS_INO_AGINO_BITS(mp
)) >>
1474 XFS_INODES_PER_CHUNK_LOG
;
1475 minleafrecs
= mp
->m_alloc_mnr
[0];
1476 minnoderecs
= mp
->m_alloc_mnr
[1];
1477 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
1478 for (level
= 1; maxblocks
> 1; level
++)
1479 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
1480 mp
->m_in_maxlevels
= level
;
1484 * Log specified fields for the ag hdr (inode section)
1488 xfs_trans_t
*tp
, /* transaction pointer */
1489 xfs_buf_t
*bp
, /* allocation group header buffer */
1490 int fields
) /* bitmask of fields to log */
1492 int first
; /* first byte number */
1493 int last
; /* last byte number */
1494 static const short offsets
[] = { /* field starting offsets */
1495 /* keep in sync with bit definitions */
1496 offsetof(xfs_agi_t
, agi_magicnum
),
1497 offsetof(xfs_agi_t
, agi_versionnum
),
1498 offsetof(xfs_agi_t
, agi_seqno
),
1499 offsetof(xfs_agi_t
, agi_length
),
1500 offsetof(xfs_agi_t
, agi_count
),
1501 offsetof(xfs_agi_t
, agi_root
),
1502 offsetof(xfs_agi_t
, agi_level
),
1503 offsetof(xfs_agi_t
, agi_freecount
),
1504 offsetof(xfs_agi_t
, agi_newino
),
1505 offsetof(xfs_agi_t
, agi_dirino
),
1506 offsetof(xfs_agi_t
, agi_unlinked
),
1510 xfs_agi_t
*agi
; /* allocation group header */
1512 agi
= XFS_BUF_TO_AGI(bp
);
1513 ASSERT(agi
->agi_magicnum
== cpu_to_be32(XFS_AGI_MAGIC
));
1516 * Compute byte offsets for the first and last fields.
1518 xfs_btree_offsets(fields
, offsets
, XFS_AGI_NUM_BITS
, &first
, &last
);
1520 * Log the allocation group inode header buffer.
1522 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_AGI_BUF
);
1523 xfs_trans_log_buf(tp
, bp
, first
, last
);
1528 xfs_check_agi_unlinked(
1529 struct xfs_agi
*agi
)
1533 for (i
= 0; i
< XFS_AGI_UNLINKED_BUCKETS
; i
++)
1534 ASSERT(agi
->agi_unlinked
[i
]);
1537 #define xfs_check_agi_unlinked(agi)
1544 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
1545 struct xfs_agi
*agi
= XFS_BUF_TO_AGI(bp
);
1547 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
1548 !uuid_equal(&agi
->agi_uuid
, &mp
->m_sb
.sb_uuid
))
1551 * Validate the magic number of the agi block.
1553 if (agi
->agi_magicnum
!= cpu_to_be32(XFS_AGI_MAGIC
))
1555 if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi
->agi_versionnum
)))
1559 * during growfs operations, the perag is not fully initialised,
1560 * so we can't use it for any useful checking. growfs ensures we can't
1561 * use it by using uncached buffers that don't have the perag attached
1562 * so we can detect and avoid this problem.
1564 if (bp
->b_pag
&& be32_to_cpu(agi
->agi_seqno
) != bp
->b_pag
->pag_agno
)
1567 xfs_check_agi_unlinked(agi
);
1572 xfs_agi_read_verify(
1575 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
1578 if (xfs_sb_version_hascrc(&mp
->m_sb
))
1579 agi_ok
= xfs_verify_cksum(bp
->b_addr
, BBTOB(bp
->b_length
),
1580 offsetof(struct xfs_agi
, agi_crc
));
1581 agi_ok
= agi_ok
&& xfs_agi_verify(bp
);
1583 if (unlikely(XFS_TEST_ERROR(!agi_ok
, mp
, XFS_ERRTAG_IALLOC_READ_AGI
,
1584 XFS_RANDOM_IALLOC_READ_AGI
))) {
1585 XFS_CORRUPTION_ERROR(__func__
, XFS_ERRLEVEL_LOW
, mp
, bp
->b_addr
);
1586 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
1591 xfs_agi_write_verify(
1594 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
1595 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
1597 if (!xfs_agi_verify(bp
)) {
1598 XFS_CORRUPTION_ERROR(__func__
, XFS_ERRLEVEL_LOW
, mp
, bp
->b_addr
);
1599 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
1603 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
1607 XFS_BUF_TO_AGI(bp
)->agi_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
1608 xfs_update_cksum(bp
->b_addr
, BBTOB(bp
->b_length
),
1609 offsetof(struct xfs_agi
, agi_crc
));
1612 const struct xfs_buf_ops xfs_agi_buf_ops
= {
1613 .verify_read
= xfs_agi_read_verify
,
1614 .verify_write
= xfs_agi_write_verify
,
1618 * Read in the allocation group header (inode allocation section)
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 */
1629 ASSERT(agno
!= NULLAGNUMBER
);
1631 error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
,
1632 XFS_AG_DADDR(mp
, agno
, XFS_AGI_DADDR(mp
)),
1633 XFS_FSS_TO_BB(mp
, 1), 0, bpp
, &xfs_agi_buf_ops
);
1637 ASSERT(!xfs_buf_geterror(*bpp
));
1638 xfs_buf_set_ref(*bpp
, XFS_AGI_REF
);
1643 xfs_ialloc_read_agi(
1644 struct xfs_mount
*mp
, /* file system mount structure */
1645 struct xfs_trans
*tp
, /* transaction pointer */
1646 xfs_agnumber_t agno
, /* allocation group number */
1647 struct xfs_buf
**bpp
) /* allocation group hdr buf */
1649 struct xfs_agi
*agi
; /* allocation group header */
1650 struct xfs_perag
*pag
; /* per allocation group data */
1653 error
= xfs_read_agi(mp
, tp
, agno
, bpp
);
1657 agi
= XFS_BUF_TO_AGI(*bpp
);
1658 pag
= xfs_perag_get(mp
, agno
);
1659 if (!pag
->pagi_init
) {
1660 pag
->pagi_freecount
= be32_to_cpu(agi
->agi_freecount
);
1661 pag
->pagi_count
= be32_to_cpu(agi
->agi_count
);
1666 * It's possible for these to be out of sync if
1667 * we are in the middle of a forced shutdown.
1669 ASSERT(pag
->pagi_freecount
== be32_to_cpu(agi
->agi_freecount
) ||
1670 XFS_FORCED_SHUTDOWN(mp
));
1676 * Read in the agi to initialise the per-ag data in the mount structure
1679 xfs_ialloc_pagi_init(
1680 xfs_mount_t
*mp
, /* file system mount structure */
1681 xfs_trans_t
*tp
, /* transaction pointer */
1682 xfs_agnumber_t agno
) /* allocation group number */
1684 xfs_buf_t
*bp
= NULL
;
1687 error
= xfs_ialloc_read_agi(mp
, tp
, agno
, &bp
);
1691 xfs_trans_brelse(tp
, bp
);