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 the record equal to ino in the btree given by cur.
62 STATIC
int /* error */
64 struct xfs_btree_cur
*cur
, /* btree cursor */
65 xfs_agino_t ino
, /* starting inode of chunk */
66 __int32_t fcnt
, /* free inode count */
67 xfs_inofree_t free
, /* free inode mask */
68 int *stat
) /* success/failure */
70 cur
->bc_rec
.i
.ir_startino
= ino
;
71 cur
->bc_rec
.i
.ir_freecount
= fcnt
;
72 cur
->bc_rec
.i
.ir_free
= free
;
73 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
77 * Lookup the first record greater than or equal to ino
78 * in the btree given by cur.
82 struct xfs_btree_cur
*cur
, /* btree cursor */
83 xfs_agino_t ino
, /* starting inode of chunk */
84 __int32_t fcnt
, /* free inode count */
85 xfs_inofree_t free
, /* free inode mask */
86 int *stat
) /* success/failure */
88 cur
->bc_rec
.i
.ir_startino
= ino
;
89 cur
->bc_rec
.i
.ir_freecount
= fcnt
;
90 cur
->bc_rec
.i
.ir_free
= free
;
91 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
95 * Lookup the first record less than or equal to ino
96 * in the btree given by cur.
100 struct xfs_btree_cur
*cur
, /* btree cursor */
101 xfs_agino_t ino
, /* starting inode of chunk */
102 __int32_t fcnt
, /* free inode count */
103 xfs_inofree_t free
, /* free inode mask */
104 int *stat
) /* success/failure */
106 cur
->bc_rec
.i
.ir_startino
= ino
;
107 cur
->bc_rec
.i
.ir_freecount
= fcnt
;
108 cur
->bc_rec
.i
.ir_free
= free
;
109 return xfs_btree_lookup(cur
, XFS_LOOKUP_LE
, stat
);
113 * Update the record referred to by cur to the value given
114 * by [ino, fcnt, free].
115 * This either works (return 0) or gets an EFSCORRUPTED error.
117 STATIC
int /* error */
119 struct xfs_btree_cur
*cur
, /* btree cursor */
120 xfs_agino_t ino
, /* starting inode of chunk */
121 __int32_t fcnt
, /* free inode count */
122 xfs_inofree_t free
) /* free inode mask */
124 union xfs_btree_rec rec
;
126 rec
.inobt
.ir_startino
= cpu_to_be32(ino
);
127 rec
.inobt
.ir_freecount
= cpu_to_be32(fcnt
);
128 rec
.inobt
.ir_free
= cpu_to_be64(free
);
129 return xfs_btree_update(cur
, &rec
);
133 * Get the data from the pointed-to record.
137 struct xfs_btree_cur
*cur
, /* btree cursor */
138 xfs_agino_t
*ino
, /* output: starting inode of chunk */
139 __int32_t
*fcnt
, /* output: number of free inodes */
140 xfs_inofree_t
*free
, /* output: free inode mask */
141 int *stat
) /* output: success/failure */
143 union xfs_btree_rec
*rec
;
146 error
= xfs_btree_get_rec(cur
, &rec
, stat
);
147 if (!error
&& *stat
== 1) {
148 *ino
= be32_to_cpu(rec
->inobt
.ir_startino
);
149 *fcnt
= be32_to_cpu(rec
->inobt
.ir_freecount
);
150 *free
= be64_to_cpu(rec
->inobt
.ir_free
);
156 * Allocate new inodes in the allocation group specified by agbp.
157 * Return 0 for success, else error code.
159 STATIC
int /* error code or 0 */
161 xfs_trans_t
*tp
, /* transaction pointer */
162 xfs_buf_t
*agbp
, /* alloc group buffer */
165 xfs_agi_t
*agi
; /* allocation group header */
166 xfs_alloc_arg_t args
; /* allocation argument structure */
167 int blks_per_cluster
; /* fs blocks per inode cluster */
168 xfs_btree_cur_t
*cur
; /* inode btree cursor */
169 xfs_daddr_t d
; /* disk addr of buffer */
172 xfs_buf_t
*fbuf
; /* new free inodes' buffer */
173 xfs_dinode_t
*free
; /* new free inode structure */
174 int i
; /* inode counter */
175 int j
; /* block counter */
176 int nbufs
; /* num bufs of new inodes */
177 xfs_agino_t newino
; /* new first inode's number */
178 xfs_agino_t newlen
; /* new number of inodes */
179 int ninodes
; /* num inodes per buf */
180 xfs_agino_t thisino
; /* current inode number, for loop */
181 int version
; /* inode version number to use */
182 int isaligned
= 0; /* inode allocation at stripe unit */
187 args
.mp
= tp
->t_mountp
;
190 * Locking will ensure that we don't have two callers in here
193 newlen
= XFS_IALLOC_INODES(args
.mp
);
194 if (args
.mp
->m_maxicount
&&
195 args
.mp
->m_sb
.sb_icount
+ newlen
> args
.mp
->m_maxicount
)
196 return XFS_ERROR(ENOSPC
);
197 args
.minlen
= args
.maxlen
= XFS_IALLOC_BLOCKS(args
.mp
);
199 * First try to allocate inodes contiguous with the last-allocated
200 * chunk of inodes. If the filesystem is striped, this will fill
201 * an entire stripe unit with inodes.
203 agi
= XFS_BUF_TO_AGI(agbp
);
204 newino
= be32_to_cpu(agi
->agi_newino
);
205 args
.agbno
= XFS_AGINO_TO_AGBNO(args
.mp
, newino
) +
206 XFS_IALLOC_BLOCKS(args
.mp
);
207 if (likely(newino
!= NULLAGINO
&&
208 (args
.agbno
< be32_to_cpu(agi
->agi_length
)))) {
209 args
.fsbno
= XFS_AGB_TO_FSB(args
.mp
,
210 be32_to_cpu(agi
->agi_seqno
), args
.agbno
);
211 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
212 args
.mod
= args
.total
= args
.wasdel
= args
.isfl
=
213 args
.userdata
= args
.minalignslop
= 0;
217 * We need to take into account alignment here to ensure that
218 * we don't modify the free list if we fail to have an exact
219 * block. If we don't have an exact match, and every oher
220 * attempt allocation attempt fails, we'll end up cancelling
221 * a dirty transaction and shutting down.
223 * For an exact allocation, alignment must be 1,
224 * however we need to take cluster alignment into account when
225 * fixing up the freelist. Use the minalignslop field to
226 * indicate that extra blocks might be required for alignment,
227 * but not to use them in the actual exact allocation.
230 args
.minalignslop
= xfs_ialloc_cluster_alignment(&args
) - 1;
232 /* Allow space for the inode btree to split. */
233 args
.minleft
= args
.mp
->m_in_maxlevels
- 1;
234 if ((error
= xfs_alloc_vextent(&args
)))
237 args
.fsbno
= NULLFSBLOCK
;
239 if (unlikely(args
.fsbno
== NULLFSBLOCK
)) {
241 * Set the alignment for the allocation.
242 * If stripe alignment is turned on then align at stripe unit
244 * If the cluster size is smaller than a filesystem block
245 * then we're doing I/O for inodes in filesystem block size
246 * pieces, so don't need alignment anyway.
249 if (args
.mp
->m_sinoalign
) {
250 ASSERT(!(args
.mp
->m_flags
& XFS_MOUNT_NOALIGN
));
251 args
.alignment
= args
.mp
->m_dalign
;
254 args
.alignment
= xfs_ialloc_cluster_alignment(&args
);
256 * Need to figure out where to allocate the inode blocks.
257 * Ideally they should be spaced out through the a.g.
258 * For now, just allocate blocks up front.
260 args
.agbno
= be32_to_cpu(agi
->agi_root
);
261 args
.fsbno
= XFS_AGB_TO_FSB(args
.mp
,
262 be32_to_cpu(agi
->agi_seqno
), args
.agbno
);
264 * Allocate a fixed-size extent of inodes.
266 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
267 args
.mod
= args
.total
= args
.wasdel
= args
.isfl
=
268 args
.userdata
= args
.minalignslop
= 0;
271 * Allow space for the inode btree to split.
273 args
.minleft
= args
.mp
->m_in_maxlevels
- 1;
274 if ((error
= xfs_alloc_vextent(&args
)))
279 * If stripe alignment is turned on, then try again with cluster
282 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
283 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
284 args
.agbno
= be32_to_cpu(agi
->agi_root
);
285 args
.fsbno
= XFS_AGB_TO_FSB(args
.mp
,
286 be32_to_cpu(agi
->agi_seqno
), args
.agbno
);
287 args
.alignment
= xfs_ialloc_cluster_alignment(&args
);
288 if ((error
= xfs_alloc_vextent(&args
)))
292 if (args
.fsbno
== NULLFSBLOCK
) {
296 ASSERT(args
.len
== args
.minlen
);
298 * Convert the results.
300 newino
= XFS_OFFBNO_TO_AGINO(args
.mp
, args
.agbno
, 0);
302 * Loop over the new block(s), filling in the inodes.
303 * For small block sizes, manipulate the inodes in buffers
304 * which are multiples of the blocks size.
306 if (args
.mp
->m_sb
.sb_blocksize
>= XFS_INODE_CLUSTER_SIZE(args
.mp
)) {
307 blks_per_cluster
= 1;
308 nbufs
= (int)args
.len
;
309 ninodes
= args
.mp
->m_sb
.sb_inopblock
;
311 blks_per_cluster
= XFS_INODE_CLUSTER_SIZE(args
.mp
) /
312 args
.mp
->m_sb
.sb_blocksize
;
313 nbufs
= (int)args
.len
/ blks_per_cluster
;
314 ninodes
= blks_per_cluster
* args
.mp
->m_sb
.sb_inopblock
;
317 * Figure out what version number to use in the inodes we create.
318 * If the superblock version has caught up to the one that supports
319 * the new inode format, then use the new inode version. Otherwise
320 * use the old version so that old kernels will continue to be
321 * able to use the file system.
323 if (xfs_sb_version_hasnlink(&args
.mp
->m_sb
))
329 * Seed the new inode cluster with a random generation number. This
330 * prevents short-term reuse of generation numbers if a chunk is
331 * freed and then immediately reallocated. We use random numbers
332 * rather than a linear progression to prevent the next generation
333 * number from being easily guessable.
336 for (j
= 0; j
< nbufs
; j
++) {
340 d
= XFS_AGB_TO_DADDR(args
.mp
, be32_to_cpu(agi
->agi_seqno
),
341 args
.agbno
+ (j
* blks_per_cluster
));
342 fbuf
= xfs_trans_get_buf(tp
, args
.mp
->m_ddev_targp
, d
,
343 args
.mp
->m_bsize
* blks_per_cluster
,
346 ASSERT(!XFS_BUF_GETERROR(fbuf
));
349 * Initialize all inodes in this buffer and then log them.
351 * XXX: It would be much better if we had just one transaction to
352 * log a whole cluster of inodes instead of all the individual
353 * transactions causing a lot of log traffic.
355 xfs_biozero(fbuf
, 0, ninodes
<< args
.mp
->m_sb
.sb_inodelog
);
356 for (i
= 0; i
< ninodes
; i
++) {
357 int ioffset
= i
<< args
.mp
->m_sb
.sb_inodelog
;
358 uint isize
= sizeof(struct xfs_dinode
);
360 free
= xfs_make_iptr(args
.mp
, fbuf
, i
);
361 free
->di_magic
= cpu_to_be16(XFS_DINODE_MAGIC
);
362 free
->di_version
= version
;
363 free
->di_gen
= cpu_to_be32(gen
);
364 free
->di_next_unlinked
= cpu_to_be32(NULLAGINO
);
365 xfs_trans_log_buf(tp
, fbuf
, ioffset
, ioffset
+ isize
- 1);
367 xfs_trans_inode_alloc_buf(tp
, fbuf
);
369 be32_add_cpu(&agi
->agi_count
, newlen
);
370 be32_add_cpu(&agi
->agi_freecount
, newlen
);
371 agno
= be32_to_cpu(agi
->agi_seqno
);
372 down_read(&args
.mp
->m_peraglock
);
373 args
.mp
->m_perag
[agno
].pagi_freecount
+= newlen
;
374 up_read(&args
.mp
->m_peraglock
);
375 agi
->agi_newino
= cpu_to_be32(newino
);
377 * Insert records describing the new inode chunk into the btree.
379 cur
= xfs_inobt_init_cursor(args
.mp
, tp
, agbp
, agno
);
380 for (thisino
= newino
;
381 thisino
< newino
+ newlen
;
382 thisino
+= XFS_INODES_PER_CHUNK
) {
383 if ((error
= xfs_inobt_lookup_eq(cur
, thisino
,
384 XFS_INODES_PER_CHUNK
, XFS_INOBT_ALL_FREE
, &i
))) {
385 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
389 if ((error
= xfs_btree_insert(cur
, &i
))) {
390 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
395 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
397 * Log allocation group header fields
399 xfs_ialloc_log_agi(tp
, agbp
,
400 XFS_AGI_COUNT
| XFS_AGI_FREECOUNT
| XFS_AGI_NEWINO
);
402 * Modify/log superblock values for inode count and inode free count.
404 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_ICOUNT
, (long)newlen
);
405 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_IFREE
, (long)newlen
);
410 STATIC_INLINE xfs_agnumber_t
416 spin_lock(&mp
->m_agirotor_lock
);
417 agno
= mp
->m_agirotor
;
418 if (++mp
->m_agirotor
== mp
->m_maxagi
)
420 spin_unlock(&mp
->m_agirotor_lock
);
426 * Select an allocation group to look for a free inode in, based on the parent
427 * inode and then mode. Return the allocation group buffer.
429 STATIC xfs_buf_t
* /* allocation group buffer */
430 xfs_ialloc_ag_select(
431 xfs_trans_t
*tp
, /* transaction pointer */
432 xfs_ino_t parent
, /* parent directory inode number */
433 mode_t mode
, /* bits set to indicate file type */
434 int okalloc
) /* ok to allocate more space */
436 xfs_buf_t
*agbp
; /* allocation group header buffer */
437 xfs_agnumber_t agcount
; /* number of ag's in the filesystem */
438 xfs_agnumber_t agno
; /* current ag number */
439 int flags
; /* alloc buffer locking flags */
440 xfs_extlen_t ineed
; /* blocks needed for inode allocation */
441 xfs_extlen_t longest
= 0; /* longest extent available */
442 xfs_mount_t
*mp
; /* mount point structure */
443 int needspace
; /* file mode implies space allocated */
444 xfs_perag_t
*pag
; /* per allocation group data */
445 xfs_agnumber_t pagno
; /* parent (starting) ag number */
448 * Files of these types need at least one block if length > 0
449 * (and they won't fit in the inode, but that's hard to figure out).
451 needspace
= S_ISDIR(mode
) || S_ISREG(mode
) || S_ISLNK(mode
);
453 agcount
= mp
->m_maxagi
;
455 pagno
= xfs_ialloc_next_ag(mp
);
457 pagno
= XFS_INO_TO_AGNO(mp
, parent
);
458 if (pagno
>= agcount
)
461 ASSERT(pagno
< agcount
);
463 * Loop through allocation groups, looking for one with a little
464 * free space in it. Note we don't look for free inodes, exactly.
465 * Instead, we include whether there is a need to allocate inodes
466 * to mean that blocks must be allocated for them,
467 * if none are currently free.
470 flags
= XFS_ALLOC_FLAG_TRYLOCK
;
471 down_read(&mp
->m_peraglock
);
473 pag
= &mp
->m_perag
[agno
];
474 if (!pag
->pagi_init
) {
475 if (xfs_ialloc_read_agi(mp
, tp
, agno
, &agbp
)) {
482 if (!pag
->pagi_inodeok
) {
483 xfs_ialloc_next_ag(mp
);
488 * Is there enough free space for the file plus a block
489 * of inodes (if we need to allocate some)?
491 ineed
= pag
->pagi_freecount
? 0 : XFS_IALLOC_BLOCKS(mp
);
492 if (ineed
&& !pag
->pagf_init
) {
494 xfs_ialloc_read_agi(mp
, tp
, agno
, &agbp
)) {
498 (void)xfs_alloc_pagf_init(mp
, tp
, agno
, flags
);
500 if (!ineed
|| pag
->pagf_init
) {
501 if (ineed
&& !(longest
= pag
->pagf_longest
))
502 longest
= pag
->pagf_flcount
> 0;
504 (pag
->pagf_freeblks
>= needspace
+ ineed
&&
508 xfs_ialloc_read_agi(mp
, tp
, agno
, &agbp
)) {
512 up_read(&mp
->m_peraglock
);
518 xfs_trans_brelse(tp
, agbp
);
521 * No point in iterating over the rest, if we're shutting
524 if (XFS_FORCED_SHUTDOWN(mp
)) {
525 up_read(&mp
->m_peraglock
);
533 up_read(&mp
->m_peraglock
);
542 * Visible inode allocation functions.
546 * Allocate an inode on disk.
547 * Mode is used to tell whether the new inode will need space, and whether
550 * The arguments IO_agbp and alloc_done are defined to work within
551 * the constraint of one allocation per transaction.
552 * xfs_dialloc() is designed to be called twice if it has to do an
553 * allocation to make more free inodes. On the first call,
554 * IO_agbp should be set to NULL. If an inode is available,
555 * i.e., xfs_dialloc() did not need to do an allocation, an inode
556 * number is returned. In this case, IO_agbp would be set to the
557 * current ag_buf and alloc_done set to false.
558 * If an allocation needed to be done, xfs_dialloc would return
559 * the current ag_buf in IO_agbp and set alloc_done to true.
560 * The caller should then commit the current transaction, allocate a new
561 * transaction, and call xfs_dialloc() again, passing in the previous
562 * value of IO_agbp. IO_agbp should be held across the transactions.
563 * Since the agbp is locked across the two calls, the second call is
564 * guaranteed to have a free inode available.
566 * Once we successfully pick an inode its number is returned and the
567 * on-disk data structures are updated. The inode itself is not read
568 * in, since doing so would break ordering constraints with xfs_reclaim.
572 xfs_trans_t
*tp
, /* transaction pointer */
573 xfs_ino_t parent
, /* parent inode (directory) */
574 mode_t mode
, /* mode bits for new inode */
575 int okalloc
, /* ok to allocate more space */
576 xfs_buf_t
**IO_agbp
, /* in/out ag header's buffer */
577 boolean_t
*alloc_done
, /* true if we needed to replenish
579 xfs_ino_t
*inop
) /* inode number allocated */
581 xfs_agnumber_t agcount
; /* number of allocation groups */
582 xfs_buf_t
*agbp
; /* allocation group header's buffer */
583 xfs_agnumber_t agno
; /* allocation group number */
584 xfs_agi_t
*agi
; /* allocation group header structure */
585 xfs_btree_cur_t
*cur
; /* inode allocation btree cursor */
586 int error
; /* error return value */
587 int i
; /* result code */
588 int ialloced
; /* inode allocation status */
589 int noroom
= 0; /* no space for inode blk allocation */
590 xfs_ino_t ino
; /* fs-relative inode to be returned */
592 int j
; /* result code */
593 xfs_mount_t
*mp
; /* file system mount structure */
594 int offset
; /* index of inode in chunk */
595 xfs_agino_t pagino
; /* parent's a.g. relative inode # */
596 xfs_agnumber_t pagno
; /* parent's allocation group number */
597 xfs_inobt_rec_incore_t rec
; /* inode allocation record */
598 xfs_agnumber_t tagno
; /* testing allocation group number */
599 xfs_btree_cur_t
*tcur
; /* temp cursor */
600 xfs_inobt_rec_incore_t trec
; /* temp inode allocation record */
603 if (*IO_agbp
== NULL
) {
605 * We do not have an agbp, so select an initial allocation
606 * group for inode allocation.
608 agbp
= xfs_ialloc_ag_select(tp
, parent
, mode
, okalloc
);
610 * Couldn't find an allocation group satisfying the
617 agi
= XFS_BUF_TO_AGI(agbp
);
618 ASSERT(be32_to_cpu(agi
->agi_magicnum
) == XFS_AGI_MAGIC
);
621 * Continue where we left off before. In this case, we
622 * know that the allocation group has free inodes.
625 agi
= XFS_BUF_TO_AGI(agbp
);
626 ASSERT(be32_to_cpu(agi
->agi_magicnum
) == XFS_AGI_MAGIC
);
627 ASSERT(be32_to_cpu(agi
->agi_freecount
) > 0);
630 agcount
= mp
->m_sb
.sb_agcount
;
631 agno
= be32_to_cpu(agi
->agi_seqno
);
633 pagno
= XFS_INO_TO_AGNO(mp
, parent
);
634 pagino
= XFS_INO_TO_AGINO(mp
, parent
);
637 * If we have already hit the ceiling of inode blocks then clear
638 * okalloc so we scan all available agi structures for a free
642 if (mp
->m_maxicount
&&
643 mp
->m_sb
.sb_icount
+ XFS_IALLOC_INODES(mp
) > mp
->m_maxicount
) {
649 * Loop until we find an allocation group that either has free inodes
650 * or in which we can allocate some inodes. Iterate through the
651 * allocation groups upward, wrapping at the end.
653 *alloc_done
= B_FALSE
;
654 while (!agi
->agi_freecount
) {
656 * Don't do anything if we're not supposed to allocate
657 * any blocks, just go on to the next ag.
661 * Try to allocate some new inodes in the allocation
664 if ((error
= xfs_ialloc_ag_alloc(tp
, agbp
, &ialloced
))) {
665 xfs_trans_brelse(tp
, agbp
);
666 if (error
== ENOSPC
) {
674 * We successfully allocated some inodes, return
675 * the current context to the caller so that it
676 * can commit the current transaction and call
677 * us again where we left off.
679 ASSERT(be32_to_cpu(agi
->agi_freecount
) > 0);
680 *alloc_done
= B_TRUE
;
687 * If it failed, give up on this ag.
689 xfs_trans_brelse(tp
, agbp
);
691 * Go on to the next ag: get its ag header.
694 if (++tagno
== agcount
)
698 return noroom
? ENOSPC
: 0;
700 down_read(&mp
->m_peraglock
);
701 if (mp
->m_perag
[tagno
].pagi_inodeok
== 0) {
702 up_read(&mp
->m_peraglock
);
705 error
= xfs_ialloc_read_agi(mp
, tp
, tagno
, &agbp
);
706 up_read(&mp
->m_peraglock
);
709 agi
= XFS_BUF_TO_AGI(agbp
);
710 ASSERT(be32_to_cpu(agi
->agi_magicnum
) == XFS_AGI_MAGIC
);
713 * Here with an allocation group that has a free inode.
714 * Reset agno since we may have chosen a new ag in the
719 cur
= xfs_inobt_init_cursor(mp
, tp
, agbp
, be32_to_cpu(agi
->agi_seqno
));
721 * If pagino is 0 (this is the root inode allocation) use newino.
722 * This must work because we've just allocated some.
725 pagino
= be32_to_cpu(agi
->agi_newino
);
727 if (cur
->bc_nlevels
== 1) {
730 if ((error
= xfs_inobt_lookup_ge(cur
, 0, 0, 0, &i
)))
732 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
734 if ((error
= xfs_inobt_get_rec(cur
, &rec
.ir_startino
,
735 &rec
.ir_freecount
, &rec
.ir_free
, &i
)))
737 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
738 freecount
+= rec
.ir_freecount
;
739 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
743 ASSERT(freecount
== be32_to_cpu(agi
->agi_freecount
) ||
744 XFS_FORCED_SHUTDOWN(mp
));
748 * If in the same a.g. as the parent, try to get near the parent.
751 if ((error
= xfs_inobt_lookup_le(cur
, pagino
, 0, 0, &i
)))
754 (error
= xfs_inobt_get_rec(cur
, &rec
.ir_startino
,
755 &rec
.ir_freecount
, &rec
.ir_free
, &j
)) == 0 &&
757 rec
.ir_freecount
> 0) {
759 * Found a free inode in the same chunk
764 * In the same a.g. as parent, but parent's chunk is full.
767 int doneleft
; /* done, to the left */
768 int doneright
; /* done, to the right */
775 * Duplicate the cursor, search left & right
778 if ((error
= xfs_btree_dup_cursor(cur
, &tcur
)))
781 * Search left with tcur, back up 1 record.
783 if ((error
= xfs_btree_decrement(tcur
, 0, &i
)))
787 if ((error
= xfs_inobt_get_rec(tcur
,
792 XFS_WANT_CORRUPTED_GOTO(i
== 1, error1
);
795 * Search right with cur, go forward 1 record.
797 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
801 if ((error
= xfs_inobt_get_rec(cur
,
806 XFS_WANT_CORRUPTED_GOTO(i
== 1, error1
);
809 * Loop until we find the closest inode chunk
812 while (!doneleft
|| !doneright
) {
813 int useleft
; /* using left inode
817 * Figure out which block is closer,
820 if (!doneleft
&& !doneright
)
824 XFS_INODES_PER_CHUNK
- 1) <
825 rec
.ir_startino
- pagino
;
829 * If checking the left, does it have
832 if (useleft
&& trec
.ir_freecount
) {
834 * Yes, set it up as the chunk to use.
837 xfs_btree_del_cursor(cur
,
843 * If checking the right, does it have
846 if (!useleft
&& rec
.ir_freecount
) {
848 * Yes, it's already set up.
850 xfs_btree_del_cursor(tcur
,
855 * If used the left, get another one
859 if ((error
= xfs_btree_decrement(tcur
, 0,
864 if ((error
= xfs_inobt_get_rec(
870 XFS_WANT_CORRUPTED_GOTO(i
== 1,
875 * If used the right, get another one
879 if ((error
= xfs_btree_increment(cur
, 0,
884 if ((error
= xfs_inobt_get_rec(
890 XFS_WANT_CORRUPTED_GOTO(i
== 1,
895 ASSERT(!doneleft
|| !doneright
);
899 * In a different a.g. from the parent.
900 * See if the most recently allocated block has any free.
902 else if (be32_to_cpu(agi
->agi_newino
) != NULLAGINO
) {
903 if ((error
= xfs_inobt_lookup_eq(cur
,
904 be32_to_cpu(agi
->agi_newino
), 0, 0, &i
)))
907 (error
= xfs_inobt_get_rec(cur
, &rec
.ir_startino
,
908 &rec
.ir_freecount
, &rec
.ir_free
, &j
)) == 0 &&
910 rec
.ir_freecount
> 0) {
912 * The last chunk allocated in the group still has
917 * None left in the last group, search the whole a.g.
922 if ((error
= xfs_inobt_lookup_ge(cur
, 0, 0, 0, &i
)))
926 if ((error
= xfs_inobt_get_rec(cur
,
928 &rec
.ir_freecount
, &rec
.ir_free
,
931 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
932 if (rec
.ir_freecount
> 0)
934 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
936 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
940 offset
= xfs_ialloc_find_free(&rec
.ir_free
);
942 ASSERT(offset
< XFS_INODES_PER_CHUNK
);
943 ASSERT((XFS_AGINO_TO_OFFSET(mp
, rec
.ir_startino
) %
944 XFS_INODES_PER_CHUNK
) == 0);
945 ino
= XFS_AGINO_TO_INO(mp
, agno
, rec
.ir_startino
+ offset
);
946 rec
.ir_free
&= ~XFS_INOBT_MASK(offset
);
948 if ((error
= xfs_inobt_update(cur
, rec
.ir_startino
, rec
.ir_freecount
,
951 be32_add_cpu(&agi
->agi_freecount
, -1);
952 xfs_ialloc_log_agi(tp
, agbp
, XFS_AGI_FREECOUNT
);
953 down_read(&mp
->m_peraglock
);
954 mp
->m_perag
[tagno
].pagi_freecount
--;
955 up_read(&mp
->m_peraglock
);
957 if (cur
->bc_nlevels
== 1) {
960 if ((error
= xfs_inobt_lookup_ge(cur
, 0, 0, 0, &i
)))
963 if ((error
= xfs_inobt_get_rec(cur
, &rec
.ir_startino
,
964 &rec
.ir_freecount
, &rec
.ir_free
, &i
)))
966 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
967 freecount
+= rec
.ir_freecount
;
968 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
971 ASSERT(freecount
== be32_to_cpu(agi
->agi_freecount
) ||
972 XFS_FORCED_SHUTDOWN(mp
));
975 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
976 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_IFREE
, -1);
980 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
982 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
987 * Free disk inode. Carefully avoids touching the incore inode, all
988 * manipulations incore are the caller's responsibility.
989 * The on-disk inode is not changed by this operation, only the
990 * btree (free inode mask) is changed.
994 xfs_trans_t
*tp
, /* transaction pointer */
995 xfs_ino_t inode
, /* inode to be freed */
996 xfs_bmap_free_t
*flist
, /* extents to free */
997 int *delete, /* set if inode cluster was deleted */
998 xfs_ino_t
*first_ino
) /* first inode in deleted cluster */
1001 xfs_agblock_t agbno
; /* block number containing inode */
1002 xfs_buf_t
*agbp
; /* buffer containing allocation group header */
1003 xfs_agino_t agino
; /* inode number relative to allocation group */
1004 xfs_agnumber_t agno
; /* allocation group number */
1005 xfs_agi_t
*agi
; /* allocation group header */
1006 xfs_btree_cur_t
*cur
; /* inode btree cursor */
1007 int error
; /* error return value */
1008 int i
; /* result code */
1009 int ilen
; /* inodes in an inode cluster */
1010 xfs_mount_t
*mp
; /* mount structure for filesystem */
1011 int off
; /* offset of inode in inode chunk */
1012 xfs_inobt_rec_incore_t rec
; /* btree record */
1017 * Break up inode number into its components.
1019 agno
= XFS_INO_TO_AGNO(mp
, inode
);
1020 if (agno
>= mp
->m_sb
.sb_agcount
) {
1022 "xfs_difree: agno >= mp->m_sb.sb_agcount (%d >= %d) on %s. Returning EINVAL.",
1023 agno
, mp
->m_sb
.sb_agcount
, mp
->m_fsname
);
1025 return XFS_ERROR(EINVAL
);
1027 agino
= XFS_INO_TO_AGINO(mp
, inode
);
1028 if (inode
!= XFS_AGINO_TO_INO(mp
, agno
, agino
)) {
1030 "xfs_difree: inode != XFS_AGINO_TO_INO() "
1031 "(%llu != %llu) on %s. Returning EINVAL.",
1032 (unsigned long long)inode
,
1033 (unsigned long long)XFS_AGINO_TO_INO(mp
, agno
, agino
),
1036 return XFS_ERROR(EINVAL
);
1038 agbno
= XFS_AGINO_TO_AGBNO(mp
, agino
);
1039 if (agbno
>= mp
->m_sb
.sb_agblocks
) {
1041 "xfs_difree: agbno >= mp->m_sb.sb_agblocks (%d >= %d) on %s. Returning EINVAL.",
1042 agbno
, mp
->m_sb
.sb_agblocks
, mp
->m_fsname
);
1044 return XFS_ERROR(EINVAL
);
1047 * Get the allocation group header.
1049 down_read(&mp
->m_peraglock
);
1050 error
= xfs_ialloc_read_agi(mp
, tp
, agno
, &agbp
);
1051 up_read(&mp
->m_peraglock
);
1054 "xfs_difree: xfs_ialloc_read_agi() returned an error %d on %s. Returning error.",
1055 error
, mp
->m_fsname
);
1058 agi
= XFS_BUF_TO_AGI(agbp
);
1059 ASSERT(be32_to_cpu(agi
->agi_magicnum
) == XFS_AGI_MAGIC
);
1060 ASSERT(agbno
< be32_to_cpu(agi
->agi_length
));
1062 * Initialize the cursor.
1064 cur
= xfs_inobt_init_cursor(mp
, tp
, agbp
, agno
);
1066 if (cur
->bc_nlevels
== 1) {
1069 if ((error
= xfs_inobt_lookup_ge(cur
, 0, 0, 0, &i
)))
1072 if ((error
= xfs_inobt_get_rec(cur
, &rec
.ir_startino
,
1073 &rec
.ir_freecount
, &rec
.ir_free
, &i
)))
1076 freecount
+= rec
.ir_freecount
;
1077 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
1081 ASSERT(freecount
== be32_to_cpu(agi
->agi_freecount
) ||
1082 XFS_FORCED_SHUTDOWN(mp
));
1086 * Look for the entry describing this inode.
1088 if ((error
= xfs_inobt_lookup_le(cur
, agino
, 0, 0, &i
))) {
1090 "xfs_difree: xfs_inobt_lookup_le returned() an error %d on %s. Returning error.",
1091 error
, mp
->m_fsname
);
1094 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1095 if ((error
= xfs_inobt_get_rec(cur
, &rec
.ir_startino
, &rec
.ir_freecount
,
1096 &rec
.ir_free
, &i
))) {
1098 "xfs_difree: xfs_inobt_get_rec() returned an error %d on %s. Returning error.",
1099 error
, mp
->m_fsname
);
1102 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1104 * Get the offset in the inode chunk.
1106 off
= agino
- rec
.ir_startino
;
1107 ASSERT(off
>= 0 && off
< XFS_INODES_PER_CHUNK
);
1108 ASSERT(!(rec
.ir_free
& XFS_INOBT_MASK(off
)));
1110 * Mark the inode free & increment the count.
1112 rec
.ir_free
|= XFS_INOBT_MASK(off
);
1116 * When an inode cluster is free, it becomes eligible for removal
1118 if (!(mp
->m_flags
& XFS_MOUNT_IKEEP
) &&
1119 (rec
.ir_freecount
== XFS_IALLOC_INODES(mp
))) {
1122 *first_ino
= XFS_AGINO_TO_INO(mp
, agno
, rec
.ir_startino
);
1125 * Remove the inode cluster from the AGI B+Tree, adjust the
1126 * AGI and Superblock inode counts, and mark the disk space
1127 * to be freed when the transaction is committed.
1129 ilen
= XFS_IALLOC_INODES(mp
);
1130 be32_add_cpu(&agi
->agi_count
, -ilen
);
1131 be32_add_cpu(&agi
->agi_freecount
, -(ilen
- 1));
1132 xfs_ialloc_log_agi(tp
, agbp
, XFS_AGI_COUNT
| XFS_AGI_FREECOUNT
);
1133 down_read(&mp
->m_peraglock
);
1134 mp
->m_perag
[agno
].pagi_freecount
-= ilen
- 1;
1135 up_read(&mp
->m_peraglock
);
1136 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_ICOUNT
, -ilen
);
1137 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_IFREE
, -(ilen
- 1));
1139 if ((error
= xfs_btree_delete(cur
, &i
))) {
1140 cmn_err(CE_WARN
, "xfs_difree: xfs_btree_delete returned an error %d on %s.\n",
1141 error
, mp
->m_fsname
);
1145 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp
,
1146 agno
, XFS_INO_TO_AGBNO(mp
,rec
.ir_startino
)),
1147 XFS_IALLOC_BLOCKS(mp
), flist
, mp
);
1151 if ((error
= xfs_inobt_update(cur
, rec
.ir_startino
, rec
.ir_freecount
, rec
.ir_free
))) {
1153 "xfs_difree: xfs_inobt_update() returned an error %d on %s. Returning error.",
1154 error
, mp
->m_fsname
);
1158 * Change the inode free counts and log the ag/sb changes.
1160 be32_add_cpu(&agi
->agi_freecount
, 1);
1161 xfs_ialloc_log_agi(tp
, agbp
, XFS_AGI_FREECOUNT
);
1162 down_read(&mp
->m_peraglock
);
1163 mp
->m_perag
[agno
].pagi_freecount
++;
1164 up_read(&mp
->m_peraglock
);
1165 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_IFREE
, 1);
1169 if (cur
->bc_nlevels
== 1) {
1172 if ((error
= xfs_inobt_lookup_ge(cur
, 0, 0, 0, &i
)))
1175 if ((error
= xfs_inobt_get_rec(cur
,
1181 freecount
+= rec
.ir_freecount
;
1182 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
1186 ASSERT(freecount
== be32_to_cpu(agi
->agi_freecount
) ||
1187 XFS_FORCED_SHUTDOWN(mp
));
1190 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1194 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1199 * Return the location of the inode in imap, for mapping it into a buffer.
1203 xfs_mount_t
*mp
, /* file system mount structure */
1204 xfs_trans_t
*tp
, /* transaction pointer */
1205 xfs_ino_t ino
, /* inode to locate */
1206 struct xfs_imap
*imap
, /* location map structure */
1207 uint flags
) /* flags for inode btree lookup */
1209 xfs_agblock_t agbno
; /* block number of inode in the alloc group */
1210 xfs_agino_t agino
; /* inode number within alloc group */
1211 xfs_agnumber_t agno
; /* allocation group number */
1212 int blks_per_cluster
; /* num blocks per inode cluster */
1213 xfs_agblock_t chunk_agbno
; /* first block in inode chunk */
1214 xfs_agblock_t cluster_agbno
; /* first block in inode cluster */
1215 int error
; /* error code */
1216 int offset
; /* index of inode in its buffer */
1217 int offset_agbno
; /* blks from chunk start to inode */
1219 ASSERT(ino
!= NULLFSINO
);
1222 * Split up the inode number into its parts.
1224 agno
= XFS_INO_TO_AGNO(mp
, ino
);
1225 agino
= XFS_INO_TO_AGINO(mp
, ino
);
1226 agbno
= XFS_AGINO_TO_AGBNO(mp
, agino
);
1227 if (agno
>= mp
->m_sb
.sb_agcount
|| agbno
>= mp
->m_sb
.sb_agblocks
||
1228 ino
!= XFS_AGINO_TO_INO(mp
, agno
, agino
)) {
1230 /* no diagnostics for bulkstat, ino comes from userspace */
1231 if (flags
& XFS_IGET_BULKSTAT
)
1232 return XFS_ERROR(EINVAL
);
1233 if (agno
>= mp
->m_sb
.sb_agcount
) {
1234 xfs_fs_cmn_err(CE_ALERT
, mp
,
1235 "xfs_imap: agno (%d) >= "
1236 "mp->m_sb.sb_agcount (%d)",
1237 agno
, mp
->m_sb
.sb_agcount
);
1239 if (agbno
>= mp
->m_sb
.sb_agblocks
) {
1240 xfs_fs_cmn_err(CE_ALERT
, mp
,
1241 "xfs_imap: agbno (0x%llx) >= "
1242 "mp->m_sb.sb_agblocks (0x%lx)",
1243 (unsigned long long) agbno
,
1244 (unsigned long) mp
->m_sb
.sb_agblocks
);
1246 if (ino
!= XFS_AGINO_TO_INO(mp
, agno
, agino
)) {
1247 xfs_fs_cmn_err(CE_ALERT
, mp
,
1248 "xfs_imap: ino (0x%llx) != "
1249 "XFS_AGINO_TO_INO(mp, agno, agino) "
1251 ino
, XFS_AGINO_TO_INO(mp
, agno
, agino
));
1255 return XFS_ERROR(EINVAL
);
1259 * If the inode cluster size is the same as the blocksize or
1260 * smaller we get to the buffer by simple arithmetics.
1262 if (XFS_INODE_CLUSTER_SIZE(mp
) <= mp
->m_sb
.sb_blocksize
) {
1263 offset
= XFS_INO_TO_OFFSET(mp
, ino
);
1264 ASSERT(offset
< mp
->m_sb
.sb_inopblock
);
1266 imap
->im_blkno
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
);
1267 imap
->im_len
= XFS_FSB_TO_BB(mp
, 1);
1268 imap
->im_boffset
= (ushort
)(offset
<< mp
->m_sb
.sb_inodelog
);
1272 blks_per_cluster
= XFS_INODE_CLUSTER_SIZE(mp
) >> mp
->m_sb
.sb_blocklog
;
1275 * If we get a block number passed from bulkstat we can use it to
1276 * find the buffer easily.
1278 if (imap
->im_blkno
) {
1279 offset
= XFS_INO_TO_OFFSET(mp
, ino
);
1280 ASSERT(offset
< mp
->m_sb
.sb_inopblock
);
1282 cluster_agbno
= xfs_daddr_to_agbno(mp
, imap
->im_blkno
);
1283 offset
+= (agbno
- cluster_agbno
) * mp
->m_sb
.sb_inopblock
;
1285 imap
->im_len
= XFS_FSB_TO_BB(mp
, blks_per_cluster
);
1286 imap
->im_boffset
= (ushort
)(offset
<< mp
->m_sb
.sb_inodelog
);
1291 * If the inode chunks are aligned then use simple maths to
1292 * find the location. Otherwise we have to do a btree
1293 * lookup to find the location.
1295 if (mp
->m_inoalign_mask
) {
1296 offset_agbno
= agbno
& mp
->m_inoalign_mask
;
1297 chunk_agbno
= agbno
- offset_agbno
;
1299 xfs_btree_cur_t
*cur
; /* inode btree cursor */
1300 xfs_agino_t chunk_agino
; /* first agino in inode chunk */
1301 __int32_t chunk_cnt
; /* count of free inodes in chunk */
1302 xfs_inofree_t chunk_free
; /* mask of free inodes in chunk */
1303 xfs_buf_t
*agbp
; /* agi buffer */
1304 int i
; /* temp state */
1306 down_read(&mp
->m_peraglock
);
1307 error
= xfs_ialloc_read_agi(mp
, tp
, agno
, &agbp
);
1308 up_read(&mp
->m_peraglock
);
1310 xfs_fs_cmn_err(CE_ALERT
, mp
, "xfs_imap: "
1311 "xfs_ialloc_read_agi() returned "
1312 "error %d, agno %d",
1317 cur
= xfs_inobt_init_cursor(mp
, tp
, agbp
, agno
);
1318 error
= xfs_inobt_lookup_le(cur
, agino
, 0, 0, &i
);
1320 xfs_fs_cmn_err(CE_ALERT
, mp
, "xfs_imap: "
1321 "xfs_inobt_lookup_le() failed");
1325 error
= xfs_inobt_get_rec(cur
, &chunk_agino
, &chunk_cnt
,
1328 xfs_fs_cmn_err(CE_ALERT
, mp
, "xfs_imap: "
1329 "xfs_inobt_get_rec() failed");
1334 xfs_fs_cmn_err(CE_ALERT
, mp
, "xfs_imap: "
1335 "xfs_inobt_get_rec() failed");
1337 error
= XFS_ERROR(EINVAL
);
1340 xfs_trans_brelse(tp
, agbp
);
1341 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1344 chunk_agbno
= XFS_AGINO_TO_AGBNO(mp
, chunk_agino
);
1345 offset_agbno
= agbno
- chunk_agbno
;
1348 ASSERT(agbno
>= chunk_agbno
);
1349 cluster_agbno
= chunk_agbno
+
1350 ((offset_agbno
/ blks_per_cluster
) * blks_per_cluster
);
1351 offset
= ((agbno
- cluster_agbno
) * mp
->m_sb
.sb_inopblock
) +
1352 XFS_INO_TO_OFFSET(mp
, ino
);
1354 imap
->im_blkno
= XFS_AGB_TO_DADDR(mp
, agno
, cluster_agbno
);
1355 imap
->im_len
= XFS_FSB_TO_BB(mp
, blks_per_cluster
);
1356 imap
->im_boffset
= (ushort
)(offset
<< mp
->m_sb
.sb_inodelog
);
1359 * If the inode number maps to a block outside the bounds
1360 * of the file system then return NULL rather than calling
1361 * read_buf and panicing when we get an error from the
1364 if ((imap
->im_blkno
+ imap
->im_len
) >
1365 XFS_FSB_TO_BB(mp
, mp
->m_sb
.sb_dblocks
)) {
1366 xfs_fs_cmn_err(CE_ALERT
, mp
, "xfs_imap: "
1367 "(imap->im_blkno (0x%llx) + imap->im_len (0x%llx)) > "
1368 " XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks) (0x%llx)",
1369 (unsigned long long) imap
->im_blkno
,
1370 (unsigned long long) imap
->im_len
,
1371 XFS_FSB_TO_BB(mp
, mp
->m_sb
.sb_dblocks
));
1372 return XFS_ERROR(EINVAL
);
1379 * Compute and fill in value of m_in_maxlevels.
1382 xfs_ialloc_compute_maxlevels(
1383 xfs_mount_t
*mp
) /* file system mount structure */
1391 maxleafents
= (1LL << XFS_INO_AGINO_BITS(mp
)) >>
1392 XFS_INODES_PER_CHUNK_LOG
;
1393 minleafrecs
= mp
->m_alloc_mnr
[0];
1394 minnoderecs
= mp
->m_alloc_mnr
[1];
1395 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
1396 for (level
= 1; maxblocks
> 1; level
++)
1397 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
1398 mp
->m_in_maxlevels
= level
;
1402 * Log specified fields for the ag hdr (inode section)
1406 xfs_trans_t
*tp
, /* transaction pointer */
1407 xfs_buf_t
*bp
, /* allocation group header buffer */
1408 int fields
) /* bitmask of fields to log */
1410 int first
; /* first byte number */
1411 int last
; /* last byte number */
1412 static const short offsets
[] = { /* field starting offsets */
1413 /* keep in sync with bit definitions */
1414 offsetof(xfs_agi_t
, agi_magicnum
),
1415 offsetof(xfs_agi_t
, agi_versionnum
),
1416 offsetof(xfs_agi_t
, agi_seqno
),
1417 offsetof(xfs_agi_t
, agi_length
),
1418 offsetof(xfs_agi_t
, agi_count
),
1419 offsetof(xfs_agi_t
, agi_root
),
1420 offsetof(xfs_agi_t
, agi_level
),
1421 offsetof(xfs_agi_t
, agi_freecount
),
1422 offsetof(xfs_agi_t
, agi_newino
),
1423 offsetof(xfs_agi_t
, agi_dirino
),
1424 offsetof(xfs_agi_t
, agi_unlinked
),
1428 xfs_agi_t
*agi
; /* allocation group header */
1430 agi
= XFS_BUF_TO_AGI(bp
);
1431 ASSERT(be32_to_cpu(agi
->agi_magicnum
) == XFS_AGI_MAGIC
);
1434 * Compute byte offsets for the first and last fields.
1436 xfs_btree_offsets(fields
, offsets
, XFS_AGI_NUM_BITS
, &first
, &last
);
1438 * Log the allocation group inode header buffer.
1440 xfs_trans_log_buf(tp
, bp
, first
, last
);
1445 xfs_check_agi_unlinked(
1446 struct xfs_agi
*agi
)
1450 for (i
= 0; i
< XFS_AGI_UNLINKED_BUCKETS
; i
++)
1451 ASSERT(agi
->agi_unlinked
[i
]);
1454 #define xfs_check_agi_unlinked(agi)
1458 * Read in the allocation group header (inode allocation section)
1462 struct xfs_mount
*mp
, /* file system mount structure */
1463 struct xfs_trans
*tp
, /* transaction pointer */
1464 xfs_agnumber_t agno
, /* allocation group number */
1465 struct xfs_buf
**bpp
) /* allocation group hdr buf */
1467 struct xfs_agi
*agi
; /* allocation group header */
1468 int agi_ok
; /* agi is consistent */
1471 ASSERT(agno
!= NULLAGNUMBER
);
1473 error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
,
1474 XFS_AG_DADDR(mp
, agno
, XFS_AGI_DADDR(mp
)),
1475 XFS_FSS_TO_BB(mp
, 1), 0, bpp
);
1479 ASSERT(*bpp
&& !XFS_BUF_GETERROR(*bpp
));
1480 agi
= XFS_BUF_TO_AGI(*bpp
);
1483 * Validate the magic number of the agi block.
1485 agi_ok
= be32_to_cpu(agi
->agi_magicnum
) == XFS_AGI_MAGIC
&&
1486 XFS_AGI_GOOD_VERSION(be32_to_cpu(agi
->agi_versionnum
)) &&
1487 be32_to_cpu(agi
->agi_seqno
) == agno
;
1488 if (unlikely(XFS_TEST_ERROR(!agi_ok
, mp
, XFS_ERRTAG_IALLOC_READ_AGI
,
1489 XFS_RANDOM_IALLOC_READ_AGI
))) {
1490 XFS_CORRUPTION_ERROR("xfs_read_agi", XFS_ERRLEVEL_LOW
,
1492 xfs_trans_brelse(tp
, *bpp
);
1493 return XFS_ERROR(EFSCORRUPTED
);
1496 XFS_BUF_SET_VTYPE_REF(*bpp
, B_FS_AGI
, XFS_AGI_REF
);
1498 xfs_check_agi_unlinked(agi
);
1503 xfs_ialloc_read_agi(
1504 struct xfs_mount
*mp
, /* file system mount structure */
1505 struct xfs_trans
*tp
, /* transaction pointer */
1506 xfs_agnumber_t agno
, /* allocation group number */
1507 struct xfs_buf
**bpp
) /* allocation group hdr buf */
1509 struct xfs_agi
*agi
; /* allocation group header */
1510 struct xfs_perag
*pag
; /* per allocation group data */
1513 error
= xfs_read_agi(mp
, tp
, agno
, bpp
);
1517 agi
= XFS_BUF_TO_AGI(*bpp
);
1518 pag
= &mp
->m_perag
[agno
];
1520 if (!pag
->pagi_init
) {
1521 pag
->pagi_freecount
= be32_to_cpu(agi
->agi_freecount
);
1522 pag
->pagi_count
= be32_to_cpu(agi
->agi_count
);
1527 * It's possible for these to be out of sync if
1528 * we are in the middle of a forced shutdown.
1530 ASSERT(pag
->pagi_freecount
== be32_to_cpu(agi
->agi_freecount
) ||
1531 XFS_FORCED_SHUTDOWN(mp
));
1536 * Read in the agi to initialise the per-ag data in the mount structure
1539 xfs_ialloc_pagi_init(
1540 xfs_mount_t
*mp
, /* file system mount structure */
1541 xfs_trans_t
*tp
, /* transaction pointer */
1542 xfs_agnumber_t agno
) /* allocation group number */
1544 xfs_buf_t
*bp
= NULL
;
1547 error
= xfs_ialloc_read_agi(mp
, tp
, agno
, &bp
);
1551 xfs_trans_brelse(tp
, bp
);