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"
45 * Log specified fields for the inode given by bp and off.
49 xfs_trans_t
*tp
, /* transaction pointer */
50 xfs_buf_t
*bp
, /* inode buffer */
51 int off
, /* index of inode in buffer */
52 int fields
) /* bitmask of fields to log */
54 int first
; /* first byte number */
55 int ioffset
; /* off in bytes */
56 int last
; /* last byte number */
57 xfs_mount_t
*mp
; /* mount point structure */
58 static const short offsets
[] = { /* field offsets */
59 /* keep in sync with bits */
60 offsetof(xfs_dinode_core_t
, di_magic
),
61 offsetof(xfs_dinode_core_t
, di_mode
),
62 offsetof(xfs_dinode_core_t
, di_version
),
63 offsetof(xfs_dinode_core_t
, di_format
),
64 offsetof(xfs_dinode_core_t
, di_onlink
),
65 offsetof(xfs_dinode_core_t
, di_uid
),
66 offsetof(xfs_dinode_core_t
, di_gid
),
67 offsetof(xfs_dinode_core_t
, di_nlink
),
68 offsetof(xfs_dinode_core_t
, di_projid
),
69 offsetof(xfs_dinode_core_t
, di_pad
),
70 offsetof(xfs_dinode_core_t
, di_atime
),
71 offsetof(xfs_dinode_core_t
, di_mtime
),
72 offsetof(xfs_dinode_core_t
, di_ctime
),
73 offsetof(xfs_dinode_core_t
, di_size
),
74 offsetof(xfs_dinode_core_t
, di_nblocks
),
75 offsetof(xfs_dinode_core_t
, di_extsize
),
76 offsetof(xfs_dinode_core_t
, di_nextents
),
77 offsetof(xfs_dinode_core_t
, di_anextents
),
78 offsetof(xfs_dinode_core_t
, di_forkoff
),
79 offsetof(xfs_dinode_core_t
, di_aformat
),
80 offsetof(xfs_dinode_core_t
, di_dmevmask
),
81 offsetof(xfs_dinode_core_t
, di_dmstate
),
82 offsetof(xfs_dinode_core_t
, di_flags
),
83 offsetof(xfs_dinode_core_t
, di_gen
),
84 offsetof(xfs_dinode_t
, di_next_unlinked
),
85 offsetof(xfs_dinode_t
, di_u
),
86 offsetof(xfs_dinode_t
, di_a
),
91 ASSERT(offsetof(xfs_dinode_t
, di_core
) == 0);
92 ASSERT((fields
& (XFS_DI_U
|XFS_DI_A
)) == 0);
95 * Get the inode-relative first and last bytes for these fields
97 xfs_btree_offsets(fields
, offsets
, XFS_DI_NUM_BITS
, &first
, &last
);
99 * Convert to buffer offsets and log it.
101 ioffset
= off
<< mp
->m_sb
.sb_inodelog
;
104 xfs_trans_log_buf(tp
, bp
, first
, last
);
108 * Allocation group level functions.
111 xfs_ialloc_cluster_alignment(
112 xfs_alloc_arg_t
*args
)
114 if (xfs_sb_version_hasalign(&args
->mp
->m_sb
) &&
115 args
->mp
->m_sb
.sb_inoalignmt
>=
116 XFS_B_TO_FSBT(args
->mp
, XFS_INODE_CLUSTER_SIZE(args
->mp
)))
117 return args
->mp
->m_sb
.sb_inoalignmt
;
122 * Lookup the record equal to ino in the btree given by cur.
124 STATIC
int /* error */
126 struct xfs_btree_cur
*cur
, /* btree cursor */
127 xfs_agino_t ino
, /* starting inode of chunk */
128 __int32_t fcnt
, /* free inode count */
129 xfs_inofree_t free
, /* free inode mask */
130 int *stat
) /* success/failure */
132 cur
->bc_rec
.i
.ir_startino
= ino
;
133 cur
->bc_rec
.i
.ir_freecount
= fcnt
;
134 cur
->bc_rec
.i
.ir_free
= free
;
135 return xfs_btree_lookup(cur
, XFS_LOOKUP_EQ
, stat
);
139 * Lookup the first record greater than or equal to ino
140 * in the btree given by cur.
144 struct xfs_btree_cur
*cur
, /* btree cursor */
145 xfs_agino_t ino
, /* starting inode of chunk */
146 __int32_t fcnt
, /* free inode count */
147 xfs_inofree_t free
, /* free inode mask */
148 int *stat
) /* success/failure */
150 cur
->bc_rec
.i
.ir_startino
= ino
;
151 cur
->bc_rec
.i
.ir_freecount
= fcnt
;
152 cur
->bc_rec
.i
.ir_free
= free
;
153 return xfs_btree_lookup(cur
, XFS_LOOKUP_GE
, stat
);
157 * Lookup the first record less than or equal to ino
158 * in the btree given by cur.
162 struct xfs_btree_cur
*cur
, /* btree cursor */
163 xfs_agino_t ino
, /* starting inode of chunk */
164 __int32_t fcnt
, /* free inode count */
165 xfs_inofree_t free
, /* free inode mask */
166 int *stat
) /* success/failure */
168 cur
->bc_rec
.i
.ir_startino
= ino
;
169 cur
->bc_rec
.i
.ir_freecount
= fcnt
;
170 cur
->bc_rec
.i
.ir_free
= free
;
171 return xfs_btree_lookup(cur
, XFS_LOOKUP_LE
, stat
);
175 * Update the record referred to by cur to the value given
176 * by [ino, fcnt, free].
177 * This either works (return 0) or gets an EFSCORRUPTED error.
179 STATIC
int /* error */
181 struct xfs_btree_cur
*cur
, /* btree cursor */
182 xfs_agino_t ino
, /* starting inode of chunk */
183 __int32_t fcnt
, /* free inode count */
184 xfs_inofree_t free
) /* free inode mask */
186 union xfs_btree_rec rec
;
188 rec
.inobt
.ir_startino
= cpu_to_be32(ino
);
189 rec
.inobt
.ir_freecount
= cpu_to_be32(fcnt
);
190 rec
.inobt
.ir_free
= cpu_to_be64(free
);
191 return xfs_btree_update(cur
, &rec
);
195 * Allocate new inodes in the allocation group specified by agbp.
196 * Return 0 for success, else error code.
198 STATIC
int /* error code or 0 */
200 xfs_trans_t
*tp
, /* transaction pointer */
201 xfs_buf_t
*agbp
, /* alloc group buffer */
204 xfs_agi_t
*agi
; /* allocation group header */
205 xfs_alloc_arg_t args
; /* allocation argument structure */
206 int blks_per_cluster
; /* fs blocks per inode cluster */
207 xfs_btree_cur_t
*cur
; /* inode btree cursor */
208 xfs_daddr_t d
; /* disk addr of buffer */
211 xfs_buf_t
*fbuf
; /* new free inodes' buffer */
212 xfs_dinode_t
*free
; /* new free inode structure */
213 int i
; /* inode counter */
214 int j
; /* block counter */
215 int nbufs
; /* num bufs of new inodes */
216 xfs_agino_t newino
; /* new first inode's number */
217 xfs_agino_t newlen
; /* new number of inodes */
218 int ninodes
; /* num inodes per buf */
219 xfs_agino_t thisino
; /* current inode number, for loop */
220 int version
; /* inode version number to use */
221 int isaligned
= 0; /* inode allocation at stripe unit */
226 args
.mp
= tp
->t_mountp
;
229 * Locking will ensure that we don't have two callers in here
232 newlen
= XFS_IALLOC_INODES(args
.mp
);
233 if (args
.mp
->m_maxicount
&&
234 args
.mp
->m_sb
.sb_icount
+ newlen
> args
.mp
->m_maxicount
)
235 return XFS_ERROR(ENOSPC
);
236 args
.minlen
= args
.maxlen
= XFS_IALLOC_BLOCKS(args
.mp
);
238 * First try to allocate inodes contiguous with the last-allocated
239 * chunk of inodes. If the filesystem is striped, this will fill
240 * an entire stripe unit with inodes.
242 agi
= XFS_BUF_TO_AGI(agbp
);
243 newino
= be32_to_cpu(agi
->agi_newino
);
244 args
.agbno
= XFS_AGINO_TO_AGBNO(args
.mp
, newino
) +
245 XFS_IALLOC_BLOCKS(args
.mp
);
246 if (likely(newino
!= NULLAGINO
&&
247 (args
.agbno
< be32_to_cpu(agi
->agi_length
)))) {
248 args
.fsbno
= XFS_AGB_TO_FSB(args
.mp
,
249 be32_to_cpu(agi
->agi_seqno
), args
.agbno
);
250 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
251 args
.mod
= args
.total
= args
.wasdel
= args
.isfl
=
252 args
.userdata
= args
.minalignslop
= 0;
256 * We need to take into account alignment here to ensure that
257 * we don't modify the free list if we fail to have an exact
258 * block. If we don't have an exact match, and every oher
259 * attempt allocation attempt fails, we'll end up cancelling
260 * a dirty transaction and shutting down.
262 * For an exact allocation, alignment must be 1,
263 * however we need to take cluster alignment into account when
264 * fixing up the freelist. Use the minalignslop field to
265 * indicate that extra blocks might be required for alignment,
266 * but not to use them in the actual exact allocation.
269 args
.minalignslop
= xfs_ialloc_cluster_alignment(&args
) - 1;
271 /* Allow space for the inode btree to split. */
272 args
.minleft
= XFS_IN_MAXLEVELS(args
.mp
) - 1;
273 if ((error
= xfs_alloc_vextent(&args
)))
276 args
.fsbno
= NULLFSBLOCK
;
278 if (unlikely(args
.fsbno
== NULLFSBLOCK
)) {
280 * Set the alignment for the allocation.
281 * If stripe alignment is turned on then align at stripe unit
283 * If the cluster size is smaller than a filesystem block
284 * then we're doing I/O for inodes in filesystem block size
285 * pieces, so don't need alignment anyway.
288 if (args
.mp
->m_sinoalign
) {
289 ASSERT(!(args
.mp
->m_flags
& XFS_MOUNT_NOALIGN
));
290 args
.alignment
= args
.mp
->m_dalign
;
293 args
.alignment
= xfs_ialloc_cluster_alignment(&args
);
295 * Need to figure out where to allocate the inode blocks.
296 * Ideally they should be spaced out through the a.g.
297 * For now, just allocate blocks up front.
299 args
.agbno
= be32_to_cpu(agi
->agi_root
);
300 args
.fsbno
= XFS_AGB_TO_FSB(args
.mp
,
301 be32_to_cpu(agi
->agi_seqno
), args
.agbno
);
303 * Allocate a fixed-size extent of inodes.
305 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
306 args
.mod
= args
.total
= args
.wasdel
= args
.isfl
=
307 args
.userdata
= args
.minalignslop
= 0;
310 * Allow space for the inode btree to split.
312 args
.minleft
= XFS_IN_MAXLEVELS(args
.mp
) - 1;
313 if ((error
= xfs_alloc_vextent(&args
)))
318 * If stripe alignment is turned on, then try again with cluster
321 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
322 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
323 args
.agbno
= be32_to_cpu(agi
->agi_root
);
324 args
.fsbno
= XFS_AGB_TO_FSB(args
.mp
,
325 be32_to_cpu(agi
->agi_seqno
), args
.agbno
);
326 args
.alignment
= xfs_ialloc_cluster_alignment(&args
);
327 if ((error
= xfs_alloc_vextent(&args
)))
331 if (args
.fsbno
== NULLFSBLOCK
) {
335 ASSERT(args
.len
== args
.minlen
);
337 * Convert the results.
339 newino
= XFS_OFFBNO_TO_AGINO(args
.mp
, args
.agbno
, 0);
341 * Loop over the new block(s), filling in the inodes.
342 * For small block sizes, manipulate the inodes in buffers
343 * which are multiples of the blocks size.
345 if (args
.mp
->m_sb
.sb_blocksize
>= XFS_INODE_CLUSTER_SIZE(args
.mp
)) {
346 blks_per_cluster
= 1;
347 nbufs
= (int)args
.len
;
348 ninodes
= args
.mp
->m_sb
.sb_inopblock
;
350 blks_per_cluster
= XFS_INODE_CLUSTER_SIZE(args
.mp
) /
351 args
.mp
->m_sb
.sb_blocksize
;
352 nbufs
= (int)args
.len
/ blks_per_cluster
;
353 ninodes
= blks_per_cluster
* args
.mp
->m_sb
.sb_inopblock
;
356 * Figure out what version number to use in the inodes we create.
357 * If the superblock version has caught up to the one that supports
358 * the new inode format, then use the new inode version. Otherwise
359 * use the old version so that old kernels will continue to be
360 * able to use the file system.
362 if (xfs_sb_version_hasnlink(&args
.mp
->m_sb
))
363 version
= XFS_DINODE_VERSION_2
;
365 version
= XFS_DINODE_VERSION_1
;
368 * Seed the new inode cluster with a random generation number. This
369 * prevents short-term reuse of generation numbers if a chunk is
370 * freed and then immediately reallocated. We use random numbers
371 * rather than a linear progression to prevent the next generation
372 * number from being easily guessable.
375 for (j
= 0; j
< nbufs
; j
++) {
379 d
= XFS_AGB_TO_DADDR(args
.mp
, be32_to_cpu(agi
->agi_seqno
),
380 args
.agbno
+ (j
* blks_per_cluster
));
381 fbuf
= xfs_trans_get_buf(tp
, args
.mp
->m_ddev_targp
, d
,
382 args
.mp
->m_bsize
* blks_per_cluster
,
385 ASSERT(!XFS_BUF_GETERROR(fbuf
));
387 * Set initial values for the inodes in this buffer.
389 xfs_biozero(fbuf
, 0, ninodes
<< args
.mp
->m_sb
.sb_inodelog
);
390 for (i
= 0; i
< ninodes
; i
++) {
391 free
= XFS_MAKE_IPTR(args
.mp
, fbuf
, i
);
392 free
->di_core
.di_magic
= cpu_to_be16(XFS_DINODE_MAGIC
);
393 free
->di_core
.di_version
= version
;
394 free
->di_core
.di_gen
= cpu_to_be32(gen
);
395 free
->di_next_unlinked
= cpu_to_be32(NULLAGINO
);
396 xfs_ialloc_log_di(tp
, fbuf
, i
,
397 XFS_DI_CORE_BITS
| XFS_DI_NEXT_UNLINKED
);
399 xfs_trans_inode_alloc_buf(tp
, fbuf
);
401 be32_add_cpu(&agi
->agi_count
, newlen
);
402 be32_add_cpu(&agi
->agi_freecount
, newlen
);
403 agno
= be32_to_cpu(agi
->agi_seqno
);
404 down_read(&args
.mp
->m_peraglock
);
405 args
.mp
->m_perag
[agno
].pagi_freecount
+= newlen
;
406 up_read(&args
.mp
->m_peraglock
);
407 agi
->agi_newino
= cpu_to_be32(newino
);
409 * Insert records describing the new inode chunk into the btree.
411 cur
= xfs_inobt_init_cursor(args
.mp
, tp
, agbp
, agno
);
412 for (thisino
= newino
;
413 thisino
< newino
+ newlen
;
414 thisino
+= XFS_INODES_PER_CHUNK
) {
415 if ((error
= xfs_inobt_lookup_eq(cur
, thisino
,
416 XFS_INODES_PER_CHUNK
, XFS_INOBT_ALL_FREE
, &i
))) {
417 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
421 if ((error
= xfs_btree_insert(cur
, &i
))) {
422 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
427 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
429 * Log allocation group header fields
431 xfs_ialloc_log_agi(tp
, agbp
,
432 XFS_AGI_COUNT
| XFS_AGI_FREECOUNT
| XFS_AGI_NEWINO
);
434 * Modify/log superblock values for inode count and inode free count.
436 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_ICOUNT
, (long)newlen
);
437 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_IFREE
, (long)newlen
);
442 STATIC_INLINE xfs_agnumber_t
448 spin_lock(&mp
->m_agirotor_lock
);
449 agno
= mp
->m_agirotor
;
450 if (++mp
->m_agirotor
== mp
->m_maxagi
)
452 spin_unlock(&mp
->m_agirotor_lock
);
458 * Select an allocation group to look for a free inode in, based on the parent
459 * inode and then mode. Return the allocation group buffer.
461 STATIC xfs_buf_t
* /* allocation group buffer */
462 xfs_ialloc_ag_select(
463 xfs_trans_t
*tp
, /* transaction pointer */
464 xfs_ino_t parent
, /* parent directory inode number */
465 mode_t mode
, /* bits set to indicate file type */
466 int okalloc
) /* ok to allocate more space */
468 xfs_buf_t
*agbp
; /* allocation group header buffer */
469 xfs_agnumber_t agcount
; /* number of ag's in the filesystem */
470 xfs_agnumber_t agno
; /* current ag number */
471 int flags
; /* alloc buffer locking flags */
472 xfs_extlen_t ineed
; /* blocks needed for inode allocation */
473 xfs_extlen_t longest
= 0; /* longest extent available */
474 xfs_mount_t
*mp
; /* mount point structure */
475 int needspace
; /* file mode implies space allocated */
476 xfs_perag_t
*pag
; /* per allocation group data */
477 xfs_agnumber_t pagno
; /* parent (starting) ag number */
480 * Files of these types need at least one block if length > 0
481 * (and they won't fit in the inode, but that's hard to figure out).
483 needspace
= S_ISDIR(mode
) || S_ISREG(mode
) || S_ISLNK(mode
);
485 agcount
= mp
->m_maxagi
;
487 pagno
= xfs_ialloc_next_ag(mp
);
489 pagno
= XFS_INO_TO_AGNO(mp
, parent
);
490 if (pagno
>= agcount
)
493 ASSERT(pagno
< agcount
);
495 * Loop through allocation groups, looking for one with a little
496 * free space in it. Note we don't look for free inodes, exactly.
497 * Instead, we include whether there is a need to allocate inodes
498 * to mean that blocks must be allocated for them,
499 * if none are currently free.
502 flags
= XFS_ALLOC_FLAG_TRYLOCK
;
503 down_read(&mp
->m_peraglock
);
505 pag
= &mp
->m_perag
[agno
];
506 if (!pag
->pagi_init
) {
507 if (xfs_ialloc_read_agi(mp
, tp
, agno
, &agbp
)) {
514 if (!pag
->pagi_inodeok
) {
515 xfs_ialloc_next_ag(mp
);
520 * Is there enough free space for the file plus a block
521 * of inodes (if we need to allocate some)?
523 ineed
= pag
->pagi_freecount
? 0 : XFS_IALLOC_BLOCKS(mp
);
524 if (ineed
&& !pag
->pagf_init
) {
526 xfs_ialloc_read_agi(mp
, tp
, agno
, &agbp
)) {
530 (void)xfs_alloc_pagf_init(mp
, tp
, agno
, flags
);
532 if (!ineed
|| pag
->pagf_init
) {
533 if (ineed
&& !(longest
= pag
->pagf_longest
))
534 longest
= pag
->pagf_flcount
> 0;
536 (pag
->pagf_freeblks
>= needspace
+ ineed
&&
540 xfs_ialloc_read_agi(mp
, tp
, agno
, &agbp
)) {
544 up_read(&mp
->m_peraglock
);
550 xfs_trans_brelse(tp
, agbp
);
553 * No point in iterating over the rest, if we're shutting
556 if (XFS_FORCED_SHUTDOWN(mp
)) {
557 up_read(&mp
->m_peraglock
);
565 up_read(&mp
->m_peraglock
);
574 * Visible inode allocation functions.
578 * Allocate an inode on disk.
579 * Mode is used to tell whether the new inode will need space, and whether
582 * The arguments IO_agbp and alloc_done are defined to work within
583 * the constraint of one allocation per transaction.
584 * xfs_dialloc() is designed to be called twice if it has to do an
585 * allocation to make more free inodes. On the first call,
586 * IO_agbp should be set to NULL. If an inode is available,
587 * i.e., xfs_dialloc() did not need to do an allocation, an inode
588 * number is returned. In this case, IO_agbp would be set to the
589 * current ag_buf and alloc_done set to false.
590 * If an allocation needed to be done, xfs_dialloc would return
591 * the current ag_buf in IO_agbp and set alloc_done to true.
592 * The caller should then commit the current transaction, allocate a new
593 * transaction, and call xfs_dialloc() again, passing in the previous
594 * value of IO_agbp. IO_agbp should be held across the transactions.
595 * Since the agbp is locked across the two calls, the second call is
596 * guaranteed to have a free inode available.
598 * Once we successfully pick an inode its number is returned and the
599 * on-disk data structures are updated. The inode itself is not read
600 * in, since doing so would break ordering constraints with xfs_reclaim.
604 xfs_trans_t
*tp
, /* transaction pointer */
605 xfs_ino_t parent
, /* parent inode (directory) */
606 mode_t mode
, /* mode bits for new inode */
607 int okalloc
, /* ok to allocate more space */
608 xfs_buf_t
**IO_agbp
, /* in/out ag header's buffer */
609 boolean_t
*alloc_done
, /* true if we needed to replenish
611 xfs_ino_t
*inop
) /* inode number allocated */
613 xfs_agnumber_t agcount
; /* number of allocation groups */
614 xfs_buf_t
*agbp
; /* allocation group header's buffer */
615 xfs_agnumber_t agno
; /* allocation group number */
616 xfs_agi_t
*agi
; /* allocation group header structure */
617 xfs_btree_cur_t
*cur
; /* inode allocation btree cursor */
618 int error
; /* error return value */
619 int i
; /* result code */
620 int ialloced
; /* inode allocation status */
621 int noroom
= 0; /* no space for inode blk allocation */
622 xfs_ino_t ino
; /* fs-relative inode to be returned */
624 int j
; /* result code */
625 xfs_mount_t
*mp
; /* file system mount structure */
626 int offset
; /* index of inode in chunk */
627 xfs_agino_t pagino
; /* parent's a.g. relative inode # */
628 xfs_agnumber_t pagno
; /* parent's allocation group number */
629 xfs_inobt_rec_incore_t rec
; /* inode allocation record */
630 xfs_agnumber_t tagno
; /* testing allocation group number */
631 xfs_btree_cur_t
*tcur
; /* temp cursor */
632 xfs_inobt_rec_incore_t trec
; /* temp inode allocation record */
635 if (*IO_agbp
== NULL
) {
637 * We do not have an agbp, so select an initial allocation
638 * group for inode allocation.
640 agbp
= xfs_ialloc_ag_select(tp
, parent
, mode
, okalloc
);
642 * Couldn't find an allocation group satisfying the
649 agi
= XFS_BUF_TO_AGI(agbp
);
650 ASSERT(be32_to_cpu(agi
->agi_magicnum
) == XFS_AGI_MAGIC
);
653 * Continue where we left off before. In this case, we
654 * know that the allocation group has free inodes.
657 agi
= XFS_BUF_TO_AGI(agbp
);
658 ASSERT(be32_to_cpu(agi
->agi_magicnum
) == XFS_AGI_MAGIC
);
659 ASSERT(be32_to_cpu(agi
->agi_freecount
) > 0);
662 agcount
= mp
->m_sb
.sb_agcount
;
663 agno
= be32_to_cpu(agi
->agi_seqno
);
665 pagno
= XFS_INO_TO_AGNO(mp
, parent
);
666 pagino
= XFS_INO_TO_AGINO(mp
, parent
);
669 * If we have already hit the ceiling of inode blocks then clear
670 * okalloc so we scan all available agi structures for a free
674 if (mp
->m_maxicount
&&
675 mp
->m_sb
.sb_icount
+ XFS_IALLOC_INODES(mp
) > mp
->m_maxicount
) {
681 * Loop until we find an allocation group that either has free inodes
682 * or in which we can allocate some inodes. Iterate through the
683 * allocation groups upward, wrapping at the end.
685 *alloc_done
= B_FALSE
;
686 while (!agi
->agi_freecount
) {
688 * Don't do anything if we're not supposed to allocate
689 * any blocks, just go on to the next ag.
693 * Try to allocate some new inodes in the allocation
696 if ((error
= xfs_ialloc_ag_alloc(tp
, agbp
, &ialloced
))) {
697 xfs_trans_brelse(tp
, agbp
);
698 if (error
== ENOSPC
) {
706 * We successfully allocated some inodes, return
707 * the current context to the caller so that it
708 * can commit the current transaction and call
709 * us again where we left off.
711 ASSERT(be32_to_cpu(agi
->agi_freecount
) > 0);
712 *alloc_done
= B_TRUE
;
719 * If it failed, give up on this ag.
721 xfs_trans_brelse(tp
, agbp
);
723 * Go on to the next ag: get its ag header.
726 if (++tagno
== agcount
)
730 return noroom
? ENOSPC
: 0;
732 down_read(&mp
->m_peraglock
);
733 if (mp
->m_perag
[tagno
].pagi_inodeok
== 0) {
734 up_read(&mp
->m_peraglock
);
737 error
= xfs_ialloc_read_agi(mp
, tp
, tagno
, &agbp
);
738 up_read(&mp
->m_peraglock
);
741 agi
= XFS_BUF_TO_AGI(agbp
);
742 ASSERT(be32_to_cpu(agi
->agi_magicnum
) == XFS_AGI_MAGIC
);
745 * Here with an allocation group that has a free inode.
746 * Reset agno since we may have chosen a new ag in the
751 cur
= xfs_inobt_init_cursor(mp
, tp
, agbp
, be32_to_cpu(agi
->agi_seqno
));
753 * If pagino is 0 (this is the root inode allocation) use newino.
754 * This must work because we've just allocated some.
757 pagino
= be32_to_cpu(agi
->agi_newino
);
759 if (cur
->bc_nlevels
== 1) {
762 if ((error
= xfs_inobt_lookup_ge(cur
, 0, 0, 0, &i
)))
764 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
766 if ((error
= xfs_inobt_get_rec(cur
, &rec
.ir_startino
,
767 &rec
.ir_freecount
, &rec
.ir_free
, &i
)))
769 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
770 freecount
+= rec
.ir_freecount
;
771 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
775 ASSERT(freecount
== be32_to_cpu(agi
->agi_freecount
) ||
776 XFS_FORCED_SHUTDOWN(mp
));
780 * If in the same a.g. as the parent, try to get near the parent.
783 if ((error
= xfs_inobt_lookup_le(cur
, pagino
, 0, 0, &i
)))
786 (error
= xfs_inobt_get_rec(cur
, &rec
.ir_startino
,
787 &rec
.ir_freecount
, &rec
.ir_free
, &j
)) == 0 &&
789 rec
.ir_freecount
> 0) {
791 * Found a free inode in the same chunk
796 * In the same a.g. as parent, but parent's chunk is full.
799 int doneleft
; /* done, to the left */
800 int doneright
; /* done, to the right */
807 * Duplicate the cursor, search left & right
810 if ((error
= xfs_btree_dup_cursor(cur
, &tcur
)))
813 * Search left with tcur, back up 1 record.
815 if ((error
= xfs_btree_decrement(tcur
, 0, &i
)))
819 if ((error
= xfs_inobt_get_rec(tcur
,
824 XFS_WANT_CORRUPTED_GOTO(i
== 1, error1
);
827 * Search right with cur, go forward 1 record.
829 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
833 if ((error
= xfs_inobt_get_rec(cur
,
838 XFS_WANT_CORRUPTED_GOTO(i
== 1, error1
);
841 * Loop until we find the closest inode chunk
844 while (!doneleft
|| !doneright
) {
845 int useleft
; /* using left inode
849 * Figure out which block is closer,
852 if (!doneleft
&& !doneright
)
856 XFS_INODES_PER_CHUNK
- 1) <
857 rec
.ir_startino
- pagino
;
861 * If checking the left, does it have
864 if (useleft
&& trec
.ir_freecount
) {
866 * Yes, set it up as the chunk to use.
869 xfs_btree_del_cursor(cur
,
875 * If checking the right, does it have
878 if (!useleft
&& rec
.ir_freecount
) {
880 * Yes, it's already set up.
882 xfs_btree_del_cursor(tcur
,
887 * If used the left, get another one
891 if ((error
= xfs_btree_decrement(tcur
, 0,
896 if ((error
= xfs_inobt_get_rec(
902 XFS_WANT_CORRUPTED_GOTO(i
== 1,
907 * If used the right, get another one
911 if ((error
= xfs_btree_increment(cur
, 0,
916 if ((error
= xfs_inobt_get_rec(
922 XFS_WANT_CORRUPTED_GOTO(i
== 1,
927 ASSERT(!doneleft
|| !doneright
);
931 * In a different a.g. from the parent.
932 * See if the most recently allocated block has any free.
934 else if (be32_to_cpu(agi
->agi_newino
) != NULLAGINO
) {
935 if ((error
= xfs_inobt_lookup_eq(cur
,
936 be32_to_cpu(agi
->agi_newino
), 0, 0, &i
)))
939 (error
= xfs_inobt_get_rec(cur
, &rec
.ir_startino
,
940 &rec
.ir_freecount
, &rec
.ir_free
, &j
)) == 0 &&
942 rec
.ir_freecount
> 0) {
944 * The last chunk allocated in the group still has
949 * None left in the last group, search the whole a.g.
954 if ((error
= xfs_inobt_lookup_ge(cur
, 0, 0, 0, &i
)))
958 if ((error
= xfs_inobt_get_rec(cur
,
960 &rec
.ir_freecount
, &rec
.ir_free
,
963 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
964 if (rec
.ir_freecount
> 0)
966 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
968 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
972 offset
= XFS_IALLOC_FIND_FREE(&rec
.ir_free
);
974 ASSERT(offset
< XFS_INODES_PER_CHUNK
);
975 ASSERT((XFS_AGINO_TO_OFFSET(mp
, rec
.ir_startino
) %
976 XFS_INODES_PER_CHUNK
) == 0);
977 ino
= XFS_AGINO_TO_INO(mp
, agno
, rec
.ir_startino
+ offset
);
978 XFS_INOBT_CLR_FREE(&rec
, offset
);
980 if ((error
= xfs_inobt_update(cur
, rec
.ir_startino
, rec
.ir_freecount
,
983 be32_add_cpu(&agi
->agi_freecount
, -1);
984 xfs_ialloc_log_agi(tp
, agbp
, XFS_AGI_FREECOUNT
);
985 down_read(&mp
->m_peraglock
);
986 mp
->m_perag
[tagno
].pagi_freecount
--;
987 up_read(&mp
->m_peraglock
);
989 if (cur
->bc_nlevels
== 1) {
992 if ((error
= xfs_inobt_lookup_ge(cur
, 0, 0, 0, &i
)))
995 if ((error
= xfs_inobt_get_rec(cur
, &rec
.ir_startino
,
996 &rec
.ir_freecount
, &rec
.ir_free
, &i
)))
998 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
999 freecount
+= rec
.ir_freecount
;
1000 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
1003 ASSERT(freecount
== be32_to_cpu(agi
->agi_freecount
) ||
1004 XFS_FORCED_SHUTDOWN(mp
));
1007 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1008 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_IFREE
, -1);
1012 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
1014 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1019 * Free disk inode. Carefully avoids touching the incore inode, all
1020 * manipulations incore are the caller's responsibility.
1021 * The on-disk inode is not changed by this operation, only the
1022 * btree (free inode mask) is changed.
1026 xfs_trans_t
*tp
, /* transaction pointer */
1027 xfs_ino_t inode
, /* inode to be freed */
1028 xfs_bmap_free_t
*flist
, /* extents to free */
1029 int *delete, /* set if inode cluster was deleted */
1030 xfs_ino_t
*first_ino
) /* first inode in deleted cluster */
1033 xfs_agblock_t agbno
; /* block number containing inode */
1034 xfs_buf_t
*agbp
; /* buffer containing allocation group header */
1035 xfs_agino_t agino
; /* inode number relative to allocation group */
1036 xfs_agnumber_t agno
; /* allocation group number */
1037 xfs_agi_t
*agi
; /* allocation group header */
1038 xfs_btree_cur_t
*cur
; /* inode btree cursor */
1039 int error
; /* error return value */
1040 int i
; /* result code */
1041 int ilen
; /* inodes in an inode cluster */
1042 xfs_mount_t
*mp
; /* mount structure for filesystem */
1043 int off
; /* offset of inode in inode chunk */
1044 xfs_inobt_rec_incore_t rec
; /* btree record */
1049 * Break up inode number into its components.
1051 agno
= XFS_INO_TO_AGNO(mp
, inode
);
1052 if (agno
>= mp
->m_sb
.sb_agcount
) {
1054 "xfs_difree: agno >= mp->m_sb.sb_agcount (%d >= %d) on %s. Returning EINVAL.",
1055 agno
, mp
->m_sb
.sb_agcount
, mp
->m_fsname
);
1057 return XFS_ERROR(EINVAL
);
1059 agino
= XFS_INO_TO_AGINO(mp
, inode
);
1060 if (inode
!= XFS_AGINO_TO_INO(mp
, agno
, agino
)) {
1062 "xfs_difree: inode != XFS_AGINO_TO_INO() "
1063 "(%llu != %llu) on %s. Returning EINVAL.",
1064 (unsigned long long)inode
,
1065 (unsigned long long)XFS_AGINO_TO_INO(mp
, agno
, agino
),
1068 return XFS_ERROR(EINVAL
);
1070 agbno
= XFS_AGINO_TO_AGBNO(mp
, agino
);
1071 if (agbno
>= mp
->m_sb
.sb_agblocks
) {
1073 "xfs_difree: agbno >= mp->m_sb.sb_agblocks (%d >= %d) on %s. Returning EINVAL.",
1074 agbno
, mp
->m_sb
.sb_agblocks
, mp
->m_fsname
);
1076 return XFS_ERROR(EINVAL
);
1079 * Get the allocation group header.
1081 down_read(&mp
->m_peraglock
);
1082 error
= xfs_ialloc_read_agi(mp
, tp
, agno
, &agbp
);
1083 up_read(&mp
->m_peraglock
);
1086 "xfs_difree: xfs_ialloc_read_agi() returned an error %d on %s. Returning error.",
1087 error
, mp
->m_fsname
);
1090 agi
= XFS_BUF_TO_AGI(agbp
);
1091 ASSERT(be32_to_cpu(agi
->agi_magicnum
) == XFS_AGI_MAGIC
);
1092 ASSERT(agbno
< be32_to_cpu(agi
->agi_length
));
1094 * Initialize the cursor.
1096 cur
= xfs_inobt_init_cursor(mp
, tp
, agbp
, agno
);
1098 if (cur
->bc_nlevels
== 1) {
1101 if ((error
= xfs_inobt_lookup_ge(cur
, 0, 0, 0, &i
)))
1104 if ((error
= xfs_inobt_get_rec(cur
, &rec
.ir_startino
,
1105 &rec
.ir_freecount
, &rec
.ir_free
, &i
)))
1108 freecount
+= rec
.ir_freecount
;
1109 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
1113 ASSERT(freecount
== be32_to_cpu(agi
->agi_freecount
) ||
1114 XFS_FORCED_SHUTDOWN(mp
));
1118 * Look for the entry describing this inode.
1120 if ((error
= xfs_inobt_lookup_le(cur
, agino
, 0, 0, &i
))) {
1122 "xfs_difree: xfs_inobt_lookup_le returned() an error %d on %s. Returning error.",
1123 error
, mp
->m_fsname
);
1126 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1127 if ((error
= xfs_inobt_get_rec(cur
, &rec
.ir_startino
, &rec
.ir_freecount
,
1128 &rec
.ir_free
, &i
))) {
1130 "xfs_difree: xfs_inobt_get_rec() returned an error %d on %s. Returning error.",
1131 error
, mp
->m_fsname
);
1134 XFS_WANT_CORRUPTED_GOTO(i
== 1, error0
);
1136 * Get the offset in the inode chunk.
1138 off
= agino
- rec
.ir_startino
;
1139 ASSERT(off
>= 0 && off
< XFS_INODES_PER_CHUNK
);
1140 ASSERT(!XFS_INOBT_IS_FREE(&rec
, off
));
1142 * Mark the inode free & increment the count.
1144 XFS_INOBT_SET_FREE(&rec
, off
);
1148 * When an inode cluster is free, it becomes eligible for removal
1150 if (!(mp
->m_flags
& XFS_MOUNT_IKEEP
) &&
1151 (rec
.ir_freecount
== XFS_IALLOC_INODES(mp
))) {
1154 *first_ino
= XFS_AGINO_TO_INO(mp
, agno
, rec
.ir_startino
);
1157 * Remove the inode cluster from the AGI B+Tree, adjust the
1158 * AGI and Superblock inode counts, and mark the disk space
1159 * to be freed when the transaction is committed.
1161 ilen
= XFS_IALLOC_INODES(mp
);
1162 be32_add_cpu(&agi
->agi_count
, -ilen
);
1163 be32_add_cpu(&agi
->agi_freecount
, -(ilen
- 1));
1164 xfs_ialloc_log_agi(tp
, agbp
, XFS_AGI_COUNT
| XFS_AGI_FREECOUNT
);
1165 down_read(&mp
->m_peraglock
);
1166 mp
->m_perag
[agno
].pagi_freecount
-= ilen
- 1;
1167 up_read(&mp
->m_peraglock
);
1168 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_ICOUNT
, -ilen
);
1169 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_IFREE
, -(ilen
- 1));
1171 if ((error
= xfs_btree_delete(cur
, &i
))) {
1172 cmn_err(CE_WARN
, "xfs_difree: xfs_btree_delete returned an error %d on %s.\n",
1173 error
, mp
->m_fsname
);
1177 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp
,
1178 agno
, XFS_INO_TO_AGBNO(mp
,rec
.ir_startino
)),
1179 XFS_IALLOC_BLOCKS(mp
), flist
, mp
);
1183 if ((error
= xfs_inobt_update(cur
, rec
.ir_startino
, rec
.ir_freecount
, rec
.ir_free
))) {
1185 "xfs_difree: xfs_inobt_update() returned an error %d on %s. Returning error.",
1186 error
, mp
->m_fsname
);
1190 * Change the inode free counts and log the ag/sb changes.
1192 be32_add_cpu(&agi
->agi_freecount
, 1);
1193 xfs_ialloc_log_agi(tp
, agbp
, XFS_AGI_FREECOUNT
);
1194 down_read(&mp
->m_peraglock
);
1195 mp
->m_perag
[agno
].pagi_freecount
++;
1196 up_read(&mp
->m_peraglock
);
1197 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_IFREE
, 1);
1201 if (cur
->bc_nlevels
== 1) {
1204 if ((error
= xfs_inobt_lookup_ge(cur
, 0, 0, 0, &i
)))
1207 if ((error
= xfs_inobt_get_rec(cur
,
1213 freecount
+= rec
.ir_freecount
;
1214 if ((error
= xfs_btree_increment(cur
, 0, &i
)))
1218 ASSERT(freecount
== be32_to_cpu(agi
->agi_freecount
) ||
1219 XFS_FORCED_SHUTDOWN(mp
));
1222 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1226 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1231 * Return the location of the inode in bno/off, for mapping it into a buffer.
1236 xfs_mount_t
*mp
, /* file system mount structure */
1237 xfs_trans_t
*tp
, /* transaction pointer */
1238 xfs_ino_t ino
, /* inode to locate */
1239 xfs_fsblock_t
*bno
, /* output: block containing inode */
1240 int *len
, /* output: num blocks in inode cluster */
1241 int *off
, /* output: index in block of inode */
1242 uint flags
) /* flags concerning inode lookup */
1244 xfs_agblock_t agbno
; /* block number of inode in the alloc group */
1245 xfs_buf_t
*agbp
; /* agi buffer */
1246 xfs_agino_t agino
; /* inode number within alloc group */
1247 xfs_agnumber_t agno
; /* allocation group number */
1248 int blks_per_cluster
; /* num blocks per inode cluster */
1249 xfs_agblock_t chunk_agbno
; /* first block in inode chunk */
1250 xfs_agino_t chunk_agino
; /* first agino in inode chunk */
1251 __int32_t chunk_cnt
; /* count of free inodes in chunk */
1252 xfs_inofree_t chunk_free
; /* mask of free inodes in chunk */
1253 xfs_agblock_t cluster_agbno
; /* first block in inode cluster */
1254 xfs_btree_cur_t
*cur
; /* inode btree cursor */
1255 int error
; /* error code */
1256 int i
; /* temp state */
1257 int offset
; /* index of inode in its buffer */
1258 int offset_agbno
; /* blks from chunk start to inode */
1260 ASSERT(ino
!= NULLFSINO
);
1262 * Split up the inode number into its parts.
1264 agno
= XFS_INO_TO_AGNO(mp
, ino
);
1265 agino
= XFS_INO_TO_AGINO(mp
, ino
);
1266 agbno
= XFS_AGINO_TO_AGBNO(mp
, agino
);
1267 if (agno
>= mp
->m_sb
.sb_agcount
|| agbno
>= mp
->m_sb
.sb_agblocks
||
1268 ino
!= XFS_AGINO_TO_INO(mp
, agno
, agino
)) {
1270 /* no diagnostics for bulkstat, ino comes from userspace */
1271 if (flags
& XFS_IMAP_BULKSTAT
)
1272 return XFS_ERROR(EINVAL
);
1273 if (agno
>= mp
->m_sb
.sb_agcount
) {
1274 xfs_fs_cmn_err(CE_ALERT
, mp
,
1275 "xfs_dilocate: agno (%d) >= "
1276 "mp->m_sb.sb_agcount (%d)",
1277 agno
, mp
->m_sb
.sb_agcount
);
1279 if (agbno
>= mp
->m_sb
.sb_agblocks
) {
1280 xfs_fs_cmn_err(CE_ALERT
, mp
,
1281 "xfs_dilocate: agbno (0x%llx) >= "
1282 "mp->m_sb.sb_agblocks (0x%lx)",
1283 (unsigned long long) agbno
,
1284 (unsigned long) mp
->m_sb
.sb_agblocks
);
1286 if (ino
!= XFS_AGINO_TO_INO(mp
, agno
, agino
)) {
1287 xfs_fs_cmn_err(CE_ALERT
, mp
,
1288 "xfs_dilocate: ino (0x%llx) != "
1289 "XFS_AGINO_TO_INO(mp, agno, agino) "
1291 ino
, XFS_AGINO_TO_INO(mp
, agno
, agino
));
1295 return XFS_ERROR(EINVAL
);
1297 if ((mp
->m_sb
.sb_blocksize
>= XFS_INODE_CLUSTER_SIZE(mp
)) ||
1298 !(flags
& XFS_IMAP_LOOKUP
)) {
1299 offset
= XFS_INO_TO_OFFSET(mp
, ino
);
1300 ASSERT(offset
< mp
->m_sb
.sb_inopblock
);
1301 *bno
= XFS_AGB_TO_FSB(mp
, agno
, agbno
);
1306 blks_per_cluster
= XFS_INODE_CLUSTER_SIZE(mp
) >> mp
->m_sb
.sb_blocklog
;
1307 if (*bno
!= NULLFSBLOCK
) {
1308 offset
= XFS_INO_TO_OFFSET(mp
, ino
);
1309 ASSERT(offset
< mp
->m_sb
.sb_inopblock
);
1310 cluster_agbno
= XFS_FSB_TO_AGBNO(mp
, *bno
);
1311 *off
= ((agbno
- cluster_agbno
) * mp
->m_sb
.sb_inopblock
) +
1313 *len
= blks_per_cluster
;
1316 if (mp
->m_inoalign_mask
) {
1317 offset_agbno
= agbno
& mp
->m_inoalign_mask
;
1318 chunk_agbno
= agbno
- offset_agbno
;
1320 down_read(&mp
->m_peraglock
);
1321 error
= xfs_ialloc_read_agi(mp
, tp
, agno
, &agbp
);
1322 up_read(&mp
->m_peraglock
);
1325 xfs_fs_cmn_err(CE_ALERT
, mp
, "xfs_dilocate: "
1326 "xfs_ialloc_read_agi() returned "
1327 "error %d, agno %d",
1332 cur
= xfs_inobt_init_cursor(mp
, tp
, agbp
, agno
);
1333 if ((error
= xfs_inobt_lookup_le(cur
, agino
, 0, 0, &i
))) {
1335 xfs_fs_cmn_err(CE_ALERT
, mp
, "xfs_dilocate: "
1336 "xfs_inobt_lookup_le() failed");
1340 if ((error
= xfs_inobt_get_rec(cur
, &chunk_agino
, &chunk_cnt
,
1341 &chunk_free
, &i
))) {
1343 xfs_fs_cmn_err(CE_ALERT
, mp
, "xfs_dilocate: "
1344 "xfs_inobt_get_rec() failed");
1350 xfs_fs_cmn_err(CE_ALERT
, mp
, "xfs_dilocate: "
1351 "xfs_inobt_get_rec() failed");
1353 error
= XFS_ERROR(EINVAL
);
1355 xfs_trans_brelse(tp
, agbp
);
1356 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1359 chunk_agbno
= XFS_AGINO_TO_AGBNO(mp
, chunk_agino
);
1360 offset_agbno
= agbno
- chunk_agbno
;
1362 ASSERT(agbno
>= chunk_agbno
);
1363 cluster_agbno
= chunk_agbno
+
1364 ((offset_agbno
/ blks_per_cluster
) * blks_per_cluster
);
1365 offset
= ((agbno
- cluster_agbno
) * mp
->m_sb
.sb_inopblock
) +
1366 XFS_INO_TO_OFFSET(mp
, ino
);
1367 *bno
= XFS_AGB_TO_FSB(mp
, agno
, cluster_agbno
);
1369 *len
= blks_per_cluster
;
1372 xfs_trans_brelse(tp
, agbp
);
1373 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1378 * Compute and fill in value of m_in_maxlevels.
1381 xfs_ialloc_compute_maxlevels(
1382 xfs_mount_t
*mp
) /* file system mount structure */
1390 maxleafents
= (1LL << XFS_INO_AGINO_BITS(mp
)) >>
1391 XFS_INODES_PER_CHUNK_LOG
;
1392 minleafrecs
= mp
->m_alloc_mnr
[0];
1393 minnoderecs
= mp
->m_alloc_mnr
[1];
1394 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
1395 for (level
= 1; maxblocks
> 1; level
++)
1396 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
1397 mp
->m_in_maxlevels
= level
;
1401 * Log specified fields for the ag hdr (inode section)
1405 xfs_trans_t
*tp
, /* transaction pointer */
1406 xfs_buf_t
*bp
, /* allocation group header buffer */
1407 int fields
) /* bitmask of fields to log */
1409 int first
; /* first byte number */
1410 int last
; /* last byte number */
1411 static const short offsets
[] = { /* field starting offsets */
1412 /* keep in sync with bit definitions */
1413 offsetof(xfs_agi_t
, agi_magicnum
),
1414 offsetof(xfs_agi_t
, agi_versionnum
),
1415 offsetof(xfs_agi_t
, agi_seqno
),
1416 offsetof(xfs_agi_t
, agi_length
),
1417 offsetof(xfs_agi_t
, agi_count
),
1418 offsetof(xfs_agi_t
, agi_root
),
1419 offsetof(xfs_agi_t
, agi_level
),
1420 offsetof(xfs_agi_t
, agi_freecount
),
1421 offsetof(xfs_agi_t
, agi_newino
),
1422 offsetof(xfs_agi_t
, agi_dirino
),
1423 offsetof(xfs_agi_t
, agi_unlinked
),
1427 xfs_agi_t
*agi
; /* allocation group header */
1429 agi
= XFS_BUF_TO_AGI(bp
);
1430 ASSERT(be32_to_cpu(agi
->agi_magicnum
) == XFS_AGI_MAGIC
);
1433 * Compute byte offsets for the first and last fields.
1435 xfs_btree_offsets(fields
, offsets
, XFS_AGI_NUM_BITS
, &first
, &last
);
1437 * Log the allocation group inode header buffer.
1439 xfs_trans_log_buf(tp
, bp
, first
, last
);
1443 * Read in the allocation group header (inode allocation section)
1446 xfs_ialloc_read_agi(
1447 xfs_mount_t
*mp
, /* file system mount structure */
1448 xfs_trans_t
*tp
, /* transaction pointer */
1449 xfs_agnumber_t agno
, /* allocation group number */
1450 xfs_buf_t
**bpp
) /* allocation group hdr buf */
1452 xfs_agi_t
*agi
; /* allocation group header */
1453 int agi_ok
; /* agi is consistent */
1454 xfs_buf_t
*bp
; /* allocation group hdr buf */
1455 xfs_perag_t
*pag
; /* per allocation group data */
1458 ASSERT(agno
!= NULLAGNUMBER
);
1459 error
= xfs_trans_read_buf(
1460 mp
, tp
, mp
->m_ddev_targp
,
1461 XFS_AG_DADDR(mp
, agno
, XFS_AGI_DADDR(mp
)),
1462 XFS_FSS_TO_BB(mp
, 1), 0, &bp
);
1465 ASSERT(bp
&& !XFS_BUF_GETERROR(bp
));
1468 * Validate the magic number of the agi block.
1470 agi
= XFS_BUF_TO_AGI(bp
);
1472 be32_to_cpu(agi
->agi_magicnum
) == XFS_AGI_MAGIC
&&
1473 XFS_AGI_GOOD_VERSION(be32_to_cpu(agi
->agi_versionnum
));
1474 if (unlikely(XFS_TEST_ERROR(!agi_ok
, mp
, XFS_ERRTAG_IALLOC_READ_AGI
,
1475 XFS_RANDOM_IALLOC_READ_AGI
))) {
1476 XFS_CORRUPTION_ERROR("xfs_ialloc_read_agi", XFS_ERRLEVEL_LOW
,
1478 xfs_trans_brelse(tp
, bp
);
1479 return XFS_ERROR(EFSCORRUPTED
);
1481 pag
= &mp
->m_perag
[agno
];
1482 if (!pag
->pagi_init
) {
1483 pag
->pagi_freecount
= be32_to_cpu(agi
->agi_freecount
);
1484 pag
->pagi_count
= be32_to_cpu(agi
->agi_count
);
1488 * It's possible for these to be out of sync if
1489 * we are in the middle of a forced shutdown.
1491 ASSERT(pag
->pagi_freecount
== be32_to_cpu(agi
->agi_freecount
) ||
1492 XFS_FORCED_SHUTDOWN(mp
));
1499 for (i
= 0; i
< XFS_AGI_UNLINKED_BUCKETS
; i
++)
1500 ASSERT(agi
->agi_unlinked
[i
]);
1504 XFS_BUF_SET_VTYPE_REF(bp
, B_FS_AGI
, XFS_AGI_REF
);
1510 * Read in the agi to initialise the per-ag data in the mount structure
1513 xfs_ialloc_pagi_init(
1514 xfs_mount_t
*mp
, /* file system mount structure */
1515 xfs_trans_t
*tp
, /* transaction pointer */
1516 xfs_agnumber_t agno
) /* allocation group number */
1518 xfs_buf_t
*bp
= NULL
;
1521 error
= xfs_ialloc_read_agi(mp
, tp
, agno
, &bp
);
1525 xfs_trans_brelse(tp
, bp
);