1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
6 #include "libxfs_priv.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
14 #include "xfs_mount.h"
15 #include "xfs_defer.h"
16 #include "xfs_inode.h"
17 #include "xfs_btree.h"
18 #include "xfs_ialloc.h"
19 #include "xfs_ialloc_btree.h"
20 #include "xfs_alloc.h"
21 #include "xfs_errortag.h"
23 #include "xfs_cksum.h"
24 #include "xfs_trans.h"
25 #include "xfs_trace.h"
30 * Allocation group level functions.
33 xfs_ialloc_cluster_alignment(
36 if (xfs_sb_version_hasalign(&mp
->m_sb
) &&
37 mp
->m_sb
.sb_inoalignmt
>= xfs_icluster_size_fsb(mp
))
38 return mp
->m_sb
.sb_inoalignmt
;
43 * Lookup a record by ino in the btree given by cur.
47 struct xfs_btree_cur
*cur
, /* btree cursor */
48 xfs_agino_t ino
, /* starting inode of chunk */
49 xfs_lookup_t dir
, /* <=, >=, == */
50 int *stat
) /* success/failure */
52 cur
->bc_rec
.i
.ir_startino
= ino
;
53 cur
->bc_rec
.i
.ir_holemask
= 0;
54 cur
->bc_rec
.i
.ir_count
= 0;
55 cur
->bc_rec
.i
.ir_freecount
= 0;
56 cur
->bc_rec
.i
.ir_free
= 0;
57 return xfs_btree_lookup(cur
, dir
, stat
);
61 * Update the record referred to by cur to the value given.
62 * This either works (return 0) or gets an EFSCORRUPTED error.
64 STATIC
int /* error */
66 struct xfs_btree_cur
*cur
, /* btree cursor */
67 xfs_inobt_rec_incore_t
*irec
) /* btree record */
69 union xfs_btree_rec rec
;
71 rec
.inobt
.ir_startino
= cpu_to_be32(irec
->ir_startino
);
72 if (xfs_sb_version_hassparseinodes(&cur
->bc_mp
->m_sb
)) {
73 rec
.inobt
.ir_u
.sp
.ir_holemask
= cpu_to_be16(irec
->ir_holemask
);
74 rec
.inobt
.ir_u
.sp
.ir_count
= irec
->ir_count
;
75 rec
.inobt
.ir_u
.sp
.ir_freecount
= irec
->ir_freecount
;
77 /* ir_holemask/ir_count not supported on-disk */
78 rec
.inobt
.ir_u
.f
.ir_freecount
= cpu_to_be32(irec
->ir_freecount
);
80 rec
.inobt
.ir_free
= cpu_to_be64(irec
->ir_free
);
81 return xfs_btree_update(cur
, &rec
);
84 /* Convert on-disk btree record to incore inobt record. */
86 xfs_inobt_btrec_to_irec(
88 union xfs_btree_rec
*rec
,
89 struct xfs_inobt_rec_incore
*irec
)
91 irec
->ir_startino
= be32_to_cpu(rec
->inobt
.ir_startino
);
92 if (xfs_sb_version_hassparseinodes(&mp
->m_sb
)) {
93 irec
->ir_holemask
= be16_to_cpu(rec
->inobt
.ir_u
.sp
.ir_holemask
);
94 irec
->ir_count
= rec
->inobt
.ir_u
.sp
.ir_count
;
95 irec
->ir_freecount
= rec
->inobt
.ir_u
.sp
.ir_freecount
;
98 * ir_holemask/ir_count not supported on-disk. Fill in hardcoded
99 * values for full inode chunks.
101 irec
->ir_holemask
= XFS_INOBT_HOLEMASK_FULL
;
102 irec
->ir_count
= XFS_INODES_PER_CHUNK
;
104 be32_to_cpu(rec
->inobt
.ir_u
.f
.ir_freecount
);
106 irec
->ir_free
= be64_to_cpu(rec
->inobt
.ir_free
);
110 * Get the data from the pointed-to record.
114 struct xfs_btree_cur
*cur
,
115 struct xfs_inobt_rec_incore
*irec
,
118 struct xfs_mount
*mp
= cur
->bc_mp
;
119 xfs_agnumber_t agno
= cur
->bc_private
.a
.agno
;
120 union xfs_btree_rec
*rec
;
124 error
= xfs_btree_get_rec(cur
, &rec
, stat
);
125 if (error
|| *stat
== 0)
128 xfs_inobt_btrec_to_irec(mp
, rec
, irec
);
130 if (!xfs_verify_agino(mp
, agno
, irec
->ir_startino
))
132 if (irec
->ir_count
< XFS_INODES_PER_HOLEMASK_BIT
||
133 irec
->ir_count
> XFS_INODES_PER_CHUNK
)
135 if (irec
->ir_freecount
> XFS_INODES_PER_CHUNK
)
138 /* if there are no holes, return the first available offset */
139 if (!xfs_inobt_issparse(irec
->ir_holemask
))
140 realfree
= irec
->ir_free
;
142 realfree
= irec
->ir_free
& xfs_inobt_irec_to_allocmask(irec
);
143 if (hweight64(realfree
) != irec
->ir_freecount
)
150 "%s Inode BTree record corruption in AG %d detected!",
151 cur
->bc_btnum
== XFS_BTNUM_INO
? "Used" : "Free", agno
);
153 "start inode 0x%x, count 0x%x, free 0x%x freemask 0x%llx, holemask 0x%x",
154 irec
->ir_startino
, irec
->ir_count
, irec
->ir_freecount
,
155 irec
->ir_free
, irec
->ir_holemask
);
156 return -EFSCORRUPTED
;
160 * Insert a single inobt record. Cursor must already point to desired location.
163 xfs_inobt_insert_rec(
164 struct xfs_btree_cur
*cur
,
171 cur
->bc_rec
.i
.ir_holemask
= holemask
;
172 cur
->bc_rec
.i
.ir_count
= count
;
173 cur
->bc_rec
.i
.ir_freecount
= freecount
;
174 cur
->bc_rec
.i
.ir_free
= free
;
175 return xfs_btree_insert(cur
, stat
);
179 * Insert records describing a newly allocated inode chunk into the inobt.
183 struct xfs_mount
*mp
,
184 struct xfs_trans
*tp
,
185 struct xfs_buf
*agbp
,
190 struct xfs_btree_cur
*cur
;
191 struct xfs_agi
*agi
= XFS_BUF_TO_AGI(agbp
);
192 xfs_agnumber_t agno
= be32_to_cpu(agi
->agi_seqno
);
197 cur
= xfs_inobt_init_cursor(mp
, tp
, agbp
, agno
, btnum
);
199 for (thisino
= newino
;
200 thisino
< newino
+ newlen
;
201 thisino
+= XFS_INODES_PER_CHUNK
) {
202 error
= xfs_inobt_lookup(cur
, thisino
, XFS_LOOKUP_EQ
, &i
);
204 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
209 error
= xfs_inobt_insert_rec(cur
, XFS_INOBT_HOLEMASK_FULL
,
210 XFS_INODES_PER_CHUNK
,
211 XFS_INODES_PER_CHUNK
,
212 XFS_INOBT_ALL_FREE
, &i
);
214 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
220 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
226 * Verify that the number of free inodes in the AGI is correct.
230 xfs_check_agi_freecount(
231 struct xfs_btree_cur
*cur
,
234 if (cur
->bc_nlevels
== 1) {
235 xfs_inobt_rec_incore_t rec
;
240 error
= xfs_inobt_lookup(cur
, 0, XFS_LOOKUP_GE
, &i
);
245 error
= xfs_inobt_get_rec(cur
, &rec
, &i
);
250 freecount
+= rec
.ir_freecount
;
251 error
= xfs_btree_increment(cur
, 0, &i
);
257 if (!XFS_FORCED_SHUTDOWN(cur
->bc_mp
))
258 ASSERT(freecount
== be32_to_cpu(agi
->agi_freecount
));
263 #define xfs_check_agi_freecount(cur, agi) 0
267 * Initialise a new set of inodes. When called without a transaction context
268 * (e.g. from recovery) we initiate a delayed write of the inode buffers rather
269 * than logging them (which in a transaction context puts them into the AIL
270 * for writeback rather than the xfsbufd queue).
273 xfs_ialloc_inode_init(
274 struct xfs_mount
*mp
,
275 struct xfs_trans
*tp
,
276 struct list_head
*buffer_list
,
280 xfs_agblock_t length
,
283 struct xfs_buf
*fbuf
;
284 struct xfs_dinode
*free
;
292 * Loop over the new block(s), filling in the inodes. For small block
293 * sizes, manipulate the inodes in buffers which are multiples of the
296 nbufs
= length
/ mp
->m_blocks_per_cluster
;
299 * Figure out what version number to use in the inodes we create. If
300 * the superblock version has caught up to the one that supports the new
301 * inode format, then use the new inode version. Otherwise use the old
302 * version so that old kernels will continue to be able to use the file
305 * For v3 inodes, we also need to write the inode number into the inode,
306 * so calculate the first inode number of the chunk here as
307 * XFS_AGB_TO_AGINO() only works within a filesystem block, not
308 * across multiple filesystem blocks (such as a cluster) and so cannot
309 * be used in the cluster buffer loop below.
311 * Further, because we are writing the inode directly into the buffer
312 * and calculating a CRC on the entire inode, we have ot log the entire
313 * inode so that the entire range the CRC covers is present in the log.
314 * That means for v3 inode we log the entire buffer rather than just the
317 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
319 ino
= XFS_AGINO_TO_INO(mp
, agno
, XFS_AGB_TO_AGINO(mp
, agbno
));
322 * log the initialisation that is about to take place as an
323 * logical operation. This means the transaction does not
324 * need to log the physical changes to the inode buffers as log
325 * recovery will know what initialisation is actually needed.
326 * Hence we only need to log the buffers as "ordered" buffers so
327 * they track in the AIL as if they were physically logged.
330 xfs_icreate_log(tp
, agno
, agbno
, icount
,
331 mp
->m_sb
.sb_inodesize
, length
, gen
);
335 for (j
= 0; j
< nbufs
; j
++) {
339 d
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
+
340 (j
* mp
->m_blocks_per_cluster
));
341 fbuf
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
,
342 mp
->m_bsize
* mp
->m_blocks_per_cluster
,
347 /* Initialize the inode buffers and log them appropriately. */
348 fbuf
->b_ops
= &xfs_inode_buf_ops
;
349 xfs_buf_zero(fbuf
, 0, BBTOB(fbuf
->b_length
));
350 for (i
= 0; i
< mp
->m_inodes_per_cluster
; i
++) {
351 int ioffset
= i
<< mp
->m_sb
.sb_inodelog
;
352 uint isize
= xfs_dinode_size(version
);
354 free
= xfs_make_iptr(mp
, fbuf
, i
);
355 free
->di_magic
= cpu_to_be16(XFS_DINODE_MAGIC
);
356 free
->di_version
= version
;
357 free
->di_gen
= cpu_to_be32(gen
);
358 free
->di_next_unlinked
= cpu_to_be32(NULLAGINO
);
361 free
->di_ino
= cpu_to_be64(ino
);
363 uuid_copy(&free
->di_uuid
,
364 &mp
->m_sb
.sb_meta_uuid
);
365 xfs_dinode_calc_crc(mp
, free
);
367 /* just log the inode core */
368 xfs_trans_log_buf(tp
, fbuf
, ioffset
,
369 ioffset
+ isize
- 1);
375 * Mark the buffer as an inode allocation buffer so it
376 * sticks in AIL at the point of this allocation
377 * transaction. This ensures the they are on disk before
378 * the tail of the log can be moved past this
379 * transaction (i.e. by preventing relogging from moving
380 * it forward in the log).
382 xfs_trans_inode_alloc_buf(tp
, fbuf
);
385 * Mark the buffer as ordered so that they are
386 * not physically logged in the transaction but
387 * still tracked in the AIL as part of the
388 * transaction and pin the log appropriately.
390 xfs_trans_ordered_buf(tp
, fbuf
);
393 fbuf
->b_flags
|= XBF_DONE
;
394 xfs_buf_delwri_queue(fbuf
, buffer_list
);
402 * Align startino and allocmask for a recently allocated sparse chunk such that
403 * they are fit for insertion (or merge) into the on-disk inode btrees.
407 * When enabled, sparse inode support increases the inode alignment from cluster
408 * size to inode chunk size. This means that the minimum range between two
409 * non-adjacent inode records in the inobt is large enough for a full inode
410 * record. This allows for cluster sized, cluster aligned block allocation
411 * without need to worry about whether the resulting inode record overlaps with
412 * another record in the tree. Without this basic rule, we would have to deal
413 * with the consequences of overlap by potentially undoing recent allocations in
414 * the inode allocation codepath.
416 * Because of this alignment rule (which is enforced on mount), there are two
417 * inobt possibilities for newly allocated sparse chunks. One is that the
418 * aligned inode record for the chunk covers a range of inodes not already
419 * covered in the inobt (i.e., it is safe to insert a new sparse record). The
420 * other is that a record already exists at the aligned startino that considers
421 * the newly allocated range as sparse. In the latter case, record content is
422 * merged in hope that sparse inode chunks fill to full chunks over time.
425 xfs_align_sparse_ino(
426 struct xfs_mount
*mp
,
427 xfs_agino_t
*startino
,
434 agbno
= XFS_AGINO_TO_AGBNO(mp
, *startino
);
435 mod
= agbno
% mp
->m_sb
.sb_inoalignmt
;
439 /* calculate the inode offset and align startino */
440 offset
= XFS_AGB_TO_AGINO(mp
, mod
);
444 * Since startino has been aligned down, left shift allocmask such that
445 * it continues to represent the same physical inodes relative to the
448 *allocmask
<<= offset
/ XFS_INODES_PER_HOLEMASK_BIT
;
452 * Determine whether the source inode record can merge into the target. Both
453 * records must be sparse, the inode ranges must match and there must be no
454 * allocation overlap between the records.
457 __xfs_inobt_can_merge(
458 struct xfs_inobt_rec_incore
*trec
, /* tgt record */
459 struct xfs_inobt_rec_incore
*srec
) /* src record */
464 /* records must cover the same inode range */
465 if (trec
->ir_startino
!= srec
->ir_startino
)
468 /* both records must be sparse */
469 if (!xfs_inobt_issparse(trec
->ir_holemask
) ||
470 !xfs_inobt_issparse(srec
->ir_holemask
))
473 /* both records must track some inodes */
474 if (!trec
->ir_count
|| !srec
->ir_count
)
477 /* can't exceed capacity of a full record */
478 if (trec
->ir_count
+ srec
->ir_count
> XFS_INODES_PER_CHUNK
)
481 /* verify there is no allocation overlap */
482 talloc
= xfs_inobt_irec_to_allocmask(trec
);
483 salloc
= xfs_inobt_irec_to_allocmask(srec
);
491 * Merge the source inode record into the target. The caller must call
492 * __xfs_inobt_can_merge() to ensure the merge is valid.
495 __xfs_inobt_rec_merge(
496 struct xfs_inobt_rec_incore
*trec
, /* target */
497 struct xfs_inobt_rec_incore
*srec
) /* src */
499 ASSERT(trec
->ir_startino
== srec
->ir_startino
);
501 /* combine the counts */
502 trec
->ir_count
+= srec
->ir_count
;
503 trec
->ir_freecount
+= srec
->ir_freecount
;
506 * Merge the holemask and free mask. For both fields, 0 bits refer to
507 * allocated inodes. We combine the allocated ranges with bitwise AND.
509 trec
->ir_holemask
&= srec
->ir_holemask
;
510 trec
->ir_free
&= srec
->ir_free
;
514 * Insert a new sparse inode chunk into the associated inode btree. The inode
515 * record for the sparse chunk is pre-aligned to a startino that should match
516 * any pre-existing sparse inode record in the tree. This allows sparse chunks
519 * This function supports two modes of handling preexisting records depending on
520 * the merge flag. If merge is true, the provided record is merged with the
521 * existing record and updated in place. The merged record is returned in nrec.
522 * If merge is false, an existing record is replaced with the provided record.
523 * If no preexisting record exists, the provided record is always inserted.
525 * It is considered corruption if a merge is requested and not possible. Given
526 * the sparse inode alignment constraints, this should never happen.
529 xfs_inobt_insert_sprec(
530 struct xfs_mount
*mp
,
531 struct xfs_trans
*tp
,
532 struct xfs_buf
*agbp
,
534 struct xfs_inobt_rec_incore
*nrec
, /* in/out: new/merged rec. */
535 bool merge
) /* merge or replace */
537 struct xfs_btree_cur
*cur
;
538 struct xfs_agi
*agi
= XFS_BUF_TO_AGI(agbp
);
539 xfs_agnumber_t agno
= be32_to_cpu(agi
->agi_seqno
);
542 struct xfs_inobt_rec_incore rec
;
544 cur
= xfs_inobt_init_cursor(mp
, tp
, agbp
, agno
, btnum
);
546 /* the new record is pre-aligned so we know where to look */
547 error
= xfs_inobt_lookup(cur
, nrec
->ir_startino
, XFS_LOOKUP_EQ
, &i
);
550 /* if nothing there, insert a new record and return */
552 error
= xfs_inobt_insert_rec(cur
, nrec
->ir_holemask
,
553 nrec
->ir_count
, nrec
->ir_freecount
,
557 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error
);
563 * A record exists at this startino. Merge or replace the record
564 * depending on what we've been asked to do.
567 error
= xfs_inobt_get_rec(cur
, &rec
, &i
);
570 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error
);
571 XFS_WANT_CORRUPTED_GOTO(mp
,
572 rec
.ir_startino
== nrec
->ir_startino
,
576 * This should never fail. If we have coexisting records that
577 * cannot merge, something is seriously wrong.
579 XFS_WANT_CORRUPTED_GOTO(mp
, __xfs_inobt_can_merge(nrec
, &rec
),
582 trace_xfs_irec_merge_pre(mp
, agno
, rec
.ir_startino
,
583 rec
.ir_holemask
, nrec
->ir_startino
,
586 /* merge to nrec to output the updated record */
587 __xfs_inobt_rec_merge(nrec
, &rec
);
589 trace_xfs_irec_merge_post(mp
, agno
, nrec
->ir_startino
,
592 error
= xfs_inobt_rec_check_count(mp
, nrec
);
597 error
= xfs_inobt_update(cur
, nrec
);
602 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
605 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
610 * Allocate new inodes in the allocation group specified by agbp.
611 * Return 0 for success, else error code.
613 STATIC
int /* error code or 0 */
615 xfs_trans_t
*tp
, /* transaction pointer */
616 xfs_buf_t
*agbp
, /* alloc group buffer */
619 xfs_agi_t
*agi
; /* allocation group header */
620 xfs_alloc_arg_t args
; /* allocation argument structure */
623 xfs_agino_t newino
; /* new first inode's number */
624 xfs_agino_t newlen
; /* new number of inodes */
625 int isaligned
= 0; /* inode allocation at stripe unit */
627 uint16_t allocmask
= (uint16_t) -1; /* init. to full chunk */
628 struct xfs_inobt_rec_incore rec
;
629 struct xfs_perag
*pag
;
632 memset(&args
, 0, sizeof(args
));
634 args
.mp
= tp
->t_mountp
;
635 args
.fsbno
= NULLFSBLOCK
;
636 args
.oinfo
= XFS_RMAP_OINFO_INODES
;
639 /* randomly do sparse inode allocations */
640 if (xfs_sb_version_hassparseinodes(&tp
->t_mountp
->m_sb
) &&
641 args
.mp
->m_ialloc_min_blks
< args
.mp
->m_ialloc_blks
)
642 do_sparse
= prandom_u32() & 1;
646 * Locking will ensure that we don't have two callers in here
649 newlen
= args
.mp
->m_ialloc_inos
;
650 if (args
.mp
->m_maxicount
&&
651 percpu_counter_read_positive(&args
.mp
->m_icount
) + newlen
>
652 args
.mp
->m_maxicount
)
654 args
.minlen
= args
.maxlen
= args
.mp
->m_ialloc_blks
;
656 * First try to allocate inodes contiguous with the last-allocated
657 * chunk of inodes. If the filesystem is striped, this will fill
658 * an entire stripe unit with inodes.
660 agi
= XFS_BUF_TO_AGI(agbp
);
661 newino
= be32_to_cpu(agi
->agi_newino
);
662 agno
= be32_to_cpu(agi
->agi_seqno
);
663 args
.agbno
= XFS_AGINO_TO_AGBNO(args
.mp
, newino
) +
664 args
.mp
->m_ialloc_blks
;
667 if (likely(newino
!= NULLAGINO
&&
668 (args
.agbno
< be32_to_cpu(agi
->agi_length
)))) {
669 args
.fsbno
= XFS_AGB_TO_FSB(args
.mp
, agno
, args
.agbno
);
670 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
674 * We need to take into account alignment here to ensure that
675 * we don't modify the free list if we fail to have an exact
676 * block. If we don't have an exact match, and every oher
677 * attempt allocation attempt fails, we'll end up cancelling
678 * a dirty transaction and shutting down.
680 * For an exact allocation, alignment must be 1,
681 * however we need to take cluster alignment into account when
682 * fixing up the freelist. Use the minalignslop field to
683 * indicate that extra blocks might be required for alignment,
684 * but not to use them in the actual exact allocation.
687 args
.minalignslop
= args
.mp
->m_cluster_align
- 1;
689 /* Allow space for the inode btree to split. */
690 args
.minleft
= args
.mp
->m_in_maxlevels
- 1;
691 if ((error
= xfs_alloc_vextent(&args
)))
695 * This request might have dirtied the transaction if the AG can
696 * satisfy the request, but the exact block was not available.
697 * If the allocation did fail, subsequent requests will relax
698 * the exact agbno requirement and increase the alignment
699 * instead. It is critical that the total size of the request
700 * (len + alignment + slop) does not increase from this point
701 * on, so reset minalignslop to ensure it is not included in
702 * subsequent requests.
704 args
.minalignslop
= 0;
707 if (unlikely(args
.fsbno
== NULLFSBLOCK
)) {
709 * Set the alignment for the allocation.
710 * If stripe alignment is turned on then align at stripe unit
712 * If the cluster size is smaller than a filesystem block
713 * then we're doing I/O for inodes in filesystem block size
714 * pieces, so don't need alignment anyway.
717 if (args
.mp
->m_sinoalign
) {
718 ASSERT(!(args
.mp
->m_flags
& XFS_MOUNT_NOALIGN
));
719 args
.alignment
= args
.mp
->m_dalign
;
722 args
.alignment
= args
.mp
->m_cluster_align
;
724 * Need to figure out where to allocate the inode blocks.
725 * Ideally they should be spaced out through the a.g.
726 * For now, just allocate blocks up front.
728 args
.agbno
= be32_to_cpu(agi
->agi_root
);
729 args
.fsbno
= XFS_AGB_TO_FSB(args
.mp
, agno
, args
.agbno
);
731 * Allocate a fixed-size extent of inodes.
733 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
736 * Allow space for the inode btree to split.
738 args
.minleft
= args
.mp
->m_in_maxlevels
- 1;
739 if ((error
= xfs_alloc_vextent(&args
)))
744 * If stripe alignment is turned on, then try again with cluster
747 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
748 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
749 args
.agbno
= be32_to_cpu(agi
->agi_root
);
750 args
.fsbno
= XFS_AGB_TO_FSB(args
.mp
, agno
, args
.agbno
);
751 args
.alignment
= args
.mp
->m_cluster_align
;
752 if ((error
= xfs_alloc_vextent(&args
)))
757 * Finally, try a sparse allocation if the filesystem supports it and
758 * the sparse allocation length is smaller than a full chunk.
760 if (xfs_sb_version_hassparseinodes(&args
.mp
->m_sb
) &&
761 args
.mp
->m_ialloc_min_blks
< args
.mp
->m_ialloc_blks
&&
762 args
.fsbno
== NULLFSBLOCK
) {
764 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
765 args
.agbno
= be32_to_cpu(agi
->agi_root
);
766 args
.fsbno
= XFS_AGB_TO_FSB(args
.mp
, agno
, args
.agbno
);
767 args
.alignment
= args
.mp
->m_sb
.sb_spino_align
;
770 args
.minlen
= args
.mp
->m_ialloc_min_blks
;
771 args
.maxlen
= args
.minlen
;
774 * The inode record will be aligned to full chunk size. We must
775 * prevent sparse allocation from AG boundaries that result in
776 * invalid inode records, such as records that start at agbno 0
777 * or extend beyond the AG.
779 * Set min agbno to the first aligned, non-zero agbno and max to
780 * the last aligned agbno that is at least one full chunk from
783 args
.min_agbno
= args
.mp
->m_sb
.sb_inoalignmt
;
784 args
.max_agbno
= round_down(args
.mp
->m_sb
.sb_agblocks
,
785 args
.mp
->m_sb
.sb_inoalignmt
) -
786 args
.mp
->m_ialloc_blks
;
788 error
= xfs_alloc_vextent(&args
);
792 newlen
= XFS_AGB_TO_AGINO(args
.mp
, args
.len
);
793 ASSERT(newlen
<= XFS_INODES_PER_CHUNK
);
794 allocmask
= (1 << (newlen
/ XFS_INODES_PER_HOLEMASK_BIT
)) - 1;
797 if (args
.fsbno
== NULLFSBLOCK
) {
801 ASSERT(args
.len
== args
.minlen
);
804 * Stamp and write the inode buffers.
806 * Seed the new inode cluster with a random generation number. This
807 * prevents short-term reuse of generation numbers if a chunk is
808 * freed and then immediately reallocated. We use random numbers
809 * rather than a linear progression to prevent the next generation
810 * number from being easily guessable.
812 error
= xfs_ialloc_inode_init(args
.mp
, tp
, NULL
, newlen
, agno
,
813 args
.agbno
, args
.len
, prandom_u32());
818 * Convert the results.
820 newino
= XFS_AGB_TO_AGINO(args
.mp
, args
.agbno
);
822 if (xfs_inobt_issparse(~allocmask
)) {
824 * We've allocated a sparse chunk. Align the startino and mask.
826 xfs_align_sparse_ino(args
.mp
, &newino
, &allocmask
);
828 rec
.ir_startino
= newino
;
829 rec
.ir_holemask
= ~allocmask
;
830 rec
.ir_count
= newlen
;
831 rec
.ir_freecount
= newlen
;
832 rec
.ir_free
= XFS_INOBT_ALL_FREE
;
835 * Insert the sparse record into the inobt and allow for a merge
836 * if necessary. If a merge does occur, rec is updated to the
839 error
= xfs_inobt_insert_sprec(args
.mp
, tp
, agbp
, XFS_BTNUM_INO
,
841 if (error
== -EFSCORRUPTED
) {
843 "invalid sparse inode record: ino 0x%llx holemask 0x%x count %u",
844 XFS_AGINO_TO_INO(args
.mp
, agno
,
846 rec
.ir_holemask
, rec
.ir_count
);
847 xfs_force_shutdown(args
.mp
, SHUTDOWN_CORRUPT_INCORE
);
853 * We can't merge the part we've just allocated as for the inobt
854 * due to finobt semantics. The original record may or may not
855 * exist independent of whether physical inodes exist in this
858 * We must update the finobt record based on the inobt record.
859 * rec contains the fully merged and up to date inobt record
860 * from the previous call. Set merge false to replace any
861 * existing record with this one.
863 if (xfs_sb_version_hasfinobt(&args
.mp
->m_sb
)) {
864 error
= xfs_inobt_insert_sprec(args
.mp
, tp
, agbp
,
865 XFS_BTNUM_FINO
, &rec
,
871 /* full chunk - insert new records to both btrees */
872 error
= xfs_inobt_insert(args
.mp
, tp
, agbp
, newino
, newlen
,
877 if (xfs_sb_version_hasfinobt(&args
.mp
->m_sb
)) {
878 error
= xfs_inobt_insert(args
.mp
, tp
, agbp
, newino
,
879 newlen
, XFS_BTNUM_FINO
);
886 * Update AGI counts and newino.
888 be32_add_cpu(&agi
->agi_count
, newlen
);
889 be32_add_cpu(&agi
->agi_freecount
, newlen
);
890 pag
= xfs_perag_get(args
.mp
, agno
);
891 pag
->pagi_freecount
+= newlen
;
892 pag
->pagi_count
+= newlen
;
894 agi
->agi_newino
= cpu_to_be32(newino
);
897 * Log allocation group header fields
899 xfs_ialloc_log_agi(tp
, agbp
,
900 XFS_AGI_COUNT
| XFS_AGI_FREECOUNT
| XFS_AGI_NEWINO
);
902 * Modify/log superblock values for inode count and inode free count.
904 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_ICOUNT
, (long)newlen
);
905 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_IFREE
, (long)newlen
);
910 STATIC xfs_agnumber_t
916 spin_lock(&mp
->m_agirotor_lock
);
917 agno
= mp
->m_agirotor
;
918 if (++mp
->m_agirotor
>= mp
->m_maxagi
)
920 spin_unlock(&mp
->m_agirotor_lock
);
926 * Select an allocation group to look for a free inode in, based on the parent
927 * inode and the mode. Return the allocation group buffer.
929 STATIC xfs_agnumber_t
930 xfs_ialloc_ag_select(
931 xfs_trans_t
*tp
, /* transaction pointer */
932 xfs_ino_t parent
, /* parent directory inode number */
933 umode_t mode
) /* bits set to indicate file type */
935 xfs_agnumber_t agcount
; /* number of ag's in the filesystem */
936 xfs_agnumber_t agno
; /* current ag number */
937 int flags
; /* alloc buffer locking flags */
938 xfs_extlen_t ineed
; /* blocks needed for inode allocation */
939 xfs_extlen_t longest
= 0; /* longest extent available */
940 xfs_mount_t
*mp
; /* mount point structure */
941 int needspace
; /* file mode implies space allocated */
942 xfs_perag_t
*pag
; /* per allocation group data */
943 xfs_agnumber_t pagno
; /* parent (starting) ag number */
947 * Files of these types need at least one block if length > 0
948 * (and they won't fit in the inode, but that's hard to figure out).
950 needspace
= S_ISDIR(mode
) || S_ISREG(mode
) || S_ISLNK(mode
);
952 agcount
= mp
->m_maxagi
;
954 pagno
= xfs_ialloc_next_ag(mp
);
956 pagno
= XFS_INO_TO_AGNO(mp
, parent
);
957 if (pagno
>= agcount
)
961 ASSERT(pagno
< agcount
);
964 * Loop through allocation groups, looking for one with a little
965 * free space in it. Note we don't look for free inodes, exactly.
966 * Instead, we include whether there is a need to allocate inodes
967 * to mean that blocks must be allocated for them,
968 * if none are currently free.
971 flags
= XFS_ALLOC_FLAG_TRYLOCK
;
973 pag
= xfs_perag_get(mp
, agno
);
974 if (!pag
->pagi_inodeok
) {
975 xfs_ialloc_next_ag(mp
);
979 if (!pag
->pagi_init
) {
980 error
= xfs_ialloc_pagi_init(mp
, tp
, agno
);
985 if (pag
->pagi_freecount
) {
990 if (!pag
->pagf_init
) {
991 error
= xfs_alloc_pagf_init(mp
, tp
, agno
, flags
);
997 * Check that there is enough free space for the file plus a
998 * chunk of inodes if we need to allocate some. If this is the
999 * first pass across the AGs, take into account the potential
1000 * space needed for alignment of inode chunks when checking the
1001 * longest contiguous free space in the AG - this prevents us
1002 * from getting ENOSPC because we have free space larger than
1003 * m_ialloc_blks but alignment constraints prevent us from using
1006 * If we can't find an AG with space for full alignment slack to
1007 * be taken into account, we must be near ENOSPC in all AGs.
1008 * Hence we don't include alignment for the second pass and so
1009 * if we fail allocation due to alignment issues then it is most
1010 * likely a real ENOSPC condition.
1012 ineed
= mp
->m_ialloc_min_blks
;
1013 if (flags
&& ineed
> 1)
1014 ineed
+= mp
->m_cluster_align
;
1015 longest
= pag
->pagf_longest
;
1017 longest
= pag
->pagf_flcount
> 0;
1019 if (pag
->pagf_freeblks
>= needspace
+ ineed
&&
1027 * No point in iterating over the rest, if we're shutting
1030 if (XFS_FORCED_SHUTDOWN(mp
))
1031 return NULLAGNUMBER
;
1033 if (agno
>= agcount
)
1035 if (agno
== pagno
) {
1037 return NULLAGNUMBER
;
1044 * Try to retrieve the next record to the left/right from the current one.
1047 xfs_ialloc_next_rec(
1048 struct xfs_btree_cur
*cur
,
1049 xfs_inobt_rec_incore_t
*rec
,
1057 error
= xfs_btree_decrement(cur
, 0, &i
);
1059 error
= xfs_btree_increment(cur
, 0, &i
);
1065 error
= xfs_inobt_get_rec(cur
, rec
, &i
);
1068 XFS_WANT_CORRUPTED_RETURN(cur
->bc_mp
, i
== 1);
1076 struct xfs_btree_cur
*cur
,
1078 xfs_inobt_rec_incore_t
*rec
,
1084 error
= xfs_inobt_lookup(cur
, agino
, XFS_LOOKUP_EQ
, &i
);
1089 error
= xfs_inobt_get_rec(cur
, rec
, &i
);
1092 XFS_WANT_CORRUPTED_RETURN(cur
->bc_mp
, i
== 1);
1099 * Return the offset of the first free inode in the record. If the inode chunk
1100 * is sparsely allocated, we convert the record holemask to inode granularity
1101 * and mask off the unallocated regions from the inode free mask.
1104 xfs_inobt_first_free_inode(
1105 struct xfs_inobt_rec_incore
*rec
)
1107 xfs_inofree_t realfree
;
1109 /* if there are no holes, return the first available offset */
1110 if (!xfs_inobt_issparse(rec
->ir_holemask
))
1111 return xfs_lowbit64(rec
->ir_free
);
1113 realfree
= xfs_inobt_irec_to_allocmask(rec
);
1114 realfree
&= rec
->ir_free
;
1116 return xfs_lowbit64(realfree
);
1120 * Allocate an inode using the inobt-only algorithm.
1123 xfs_dialloc_ag_inobt(
1124 struct xfs_trans
*tp
,
1125 struct xfs_buf
*agbp
,
1129 struct xfs_mount
*mp
= tp
->t_mountp
;
1130 struct xfs_agi
*agi
= XFS_BUF_TO_AGI(agbp
);
1131 xfs_agnumber_t agno
= be32_to_cpu(agi
->agi_seqno
);
1132 xfs_agnumber_t pagno
= XFS_INO_TO_AGNO(mp
, parent
);
1133 xfs_agino_t pagino
= XFS_INO_TO_AGINO(mp
, parent
);
1134 struct xfs_perag
*pag
;
1135 struct xfs_btree_cur
*cur
, *tcur
;
1136 struct xfs_inobt_rec_incore rec
, trec
;
1141 int searchdistance
= 10;
1143 pag
= xfs_perag_get(mp
, agno
);
1145 ASSERT(pag
->pagi_init
);
1146 ASSERT(pag
->pagi_inodeok
);
1147 ASSERT(pag
->pagi_freecount
> 0);
1150 cur
= xfs_inobt_init_cursor(mp
, tp
, agbp
, agno
, XFS_BTNUM_INO
);
1152 * If pagino is 0 (this is the root inode allocation) use newino.
1153 * This must work because we've just allocated some.
1156 pagino
= be32_to_cpu(agi
->agi_newino
);
1158 error
= xfs_check_agi_freecount(cur
, agi
);
1163 * If in the same AG as the parent, try to get near the parent.
1165 if (pagno
== agno
) {
1166 int doneleft
; /* done, to the left */
1167 int doneright
; /* done, to the right */
1169 error
= xfs_inobt_lookup(cur
, pagino
, XFS_LOOKUP_LE
, &i
);
1172 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1174 error
= xfs_inobt_get_rec(cur
, &rec
, &j
);
1177 XFS_WANT_CORRUPTED_GOTO(mp
, j
== 1, error0
);
1179 if (rec
.ir_freecount
> 0) {
1181 * Found a free inode in the same chunk
1182 * as the parent, done.
1189 * In the same AG as parent, but parent's chunk is full.
1192 /* duplicate the cursor, search left & right simultaneously */
1193 error
= xfs_btree_dup_cursor(cur
, &tcur
);
1198 * Skip to last blocks looked up if same parent inode.
1200 if (pagino
!= NULLAGINO
&&
1201 pag
->pagl_pagino
== pagino
&&
1202 pag
->pagl_leftrec
!= NULLAGINO
&&
1203 pag
->pagl_rightrec
!= NULLAGINO
) {
1204 error
= xfs_ialloc_get_rec(tcur
, pag
->pagl_leftrec
,
1209 error
= xfs_ialloc_get_rec(cur
, pag
->pagl_rightrec
,
1214 /* search left with tcur, back up 1 record */
1215 error
= xfs_ialloc_next_rec(tcur
, &trec
, &doneleft
, 1);
1219 /* search right with cur, go forward 1 record. */
1220 error
= xfs_ialloc_next_rec(cur
, &rec
, &doneright
, 0);
1226 * Loop until we find an inode chunk with a free inode.
1228 while (--searchdistance
> 0 && (!doneleft
|| !doneright
)) {
1229 int useleft
; /* using left inode chunk this time */
1231 /* figure out the closer block if both are valid. */
1232 if (!doneleft
&& !doneright
) {
1234 (trec
.ir_startino
+ XFS_INODES_PER_CHUNK
- 1) <
1235 rec
.ir_startino
- pagino
;
1237 useleft
= !doneleft
;
1240 /* free inodes to the left? */
1241 if (useleft
&& trec
.ir_freecount
) {
1242 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1245 pag
->pagl_leftrec
= trec
.ir_startino
;
1246 pag
->pagl_rightrec
= rec
.ir_startino
;
1247 pag
->pagl_pagino
= pagino
;
1252 /* free inodes to the right? */
1253 if (!useleft
&& rec
.ir_freecount
) {
1254 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
1256 pag
->pagl_leftrec
= trec
.ir_startino
;
1257 pag
->pagl_rightrec
= rec
.ir_startino
;
1258 pag
->pagl_pagino
= pagino
;
1262 /* get next record to check */
1264 error
= xfs_ialloc_next_rec(tcur
, &trec
,
1267 error
= xfs_ialloc_next_rec(cur
, &rec
,
1274 if (searchdistance
<= 0) {
1276 * Not in range - save last search
1277 * location and allocate a new inode
1279 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
1280 pag
->pagl_leftrec
= trec
.ir_startino
;
1281 pag
->pagl_rightrec
= rec
.ir_startino
;
1282 pag
->pagl_pagino
= pagino
;
1286 * We've reached the end of the btree. because
1287 * we are only searching a small chunk of the
1288 * btree each search, there is obviously free
1289 * inodes closer to the parent inode than we
1290 * are now. restart the search again.
1292 pag
->pagl_pagino
= NULLAGINO
;
1293 pag
->pagl_leftrec
= NULLAGINO
;
1294 pag
->pagl_rightrec
= NULLAGINO
;
1295 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
1296 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1302 * In a different AG from the parent.
1303 * See if the most recently allocated block has any free.
1305 if (agi
->agi_newino
!= cpu_to_be32(NULLAGINO
)) {
1306 error
= xfs_inobt_lookup(cur
, be32_to_cpu(agi
->agi_newino
),
1312 error
= xfs_inobt_get_rec(cur
, &rec
, &j
);
1316 if (j
== 1 && rec
.ir_freecount
> 0) {
1318 * The last chunk allocated in the group
1319 * still has a free inode.
1327 * None left in the last group, search the whole AG
1329 error
= xfs_inobt_lookup(cur
, 0, XFS_LOOKUP_GE
, &i
);
1332 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1335 error
= xfs_inobt_get_rec(cur
, &rec
, &i
);
1338 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1339 if (rec
.ir_freecount
> 0)
1341 error
= xfs_btree_increment(cur
, 0, &i
);
1344 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1348 offset
= xfs_inobt_first_free_inode(&rec
);
1349 ASSERT(offset
>= 0);
1350 ASSERT(offset
< XFS_INODES_PER_CHUNK
);
1351 ASSERT((XFS_AGINO_TO_OFFSET(mp
, rec
.ir_startino
) %
1352 XFS_INODES_PER_CHUNK
) == 0);
1353 ino
= XFS_AGINO_TO_INO(mp
, agno
, rec
.ir_startino
+ offset
);
1354 rec
.ir_free
&= ~XFS_INOBT_MASK(offset
);
1356 error
= xfs_inobt_update(cur
, &rec
);
1359 be32_add_cpu(&agi
->agi_freecount
, -1);
1360 xfs_ialloc_log_agi(tp
, agbp
, XFS_AGI_FREECOUNT
);
1361 pag
->pagi_freecount
--;
1363 error
= xfs_check_agi_freecount(cur
, agi
);
1367 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1368 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_IFREE
, -1);
1373 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
1375 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1381 * Use the free inode btree to allocate an inode based on distance from the
1382 * parent. Note that the provided cursor may be deleted and replaced.
1385 xfs_dialloc_ag_finobt_near(
1387 struct xfs_btree_cur
**ocur
,
1388 struct xfs_inobt_rec_incore
*rec
)
1390 struct xfs_btree_cur
*lcur
= *ocur
; /* left search cursor */
1391 struct xfs_btree_cur
*rcur
; /* right search cursor */
1392 struct xfs_inobt_rec_incore rrec
;
1396 error
= xfs_inobt_lookup(lcur
, pagino
, XFS_LOOKUP_LE
, &i
);
1401 error
= xfs_inobt_get_rec(lcur
, rec
, &i
);
1404 XFS_WANT_CORRUPTED_RETURN(lcur
->bc_mp
, i
== 1);
1407 * See if we've landed in the parent inode record. The finobt
1408 * only tracks chunks with at least one free inode, so record
1409 * existence is enough.
1411 if (pagino
>= rec
->ir_startino
&&
1412 pagino
< (rec
->ir_startino
+ XFS_INODES_PER_CHUNK
))
1416 error
= xfs_btree_dup_cursor(lcur
, &rcur
);
1420 error
= xfs_inobt_lookup(rcur
, pagino
, XFS_LOOKUP_GE
, &j
);
1424 error
= xfs_inobt_get_rec(rcur
, &rrec
, &j
);
1427 XFS_WANT_CORRUPTED_GOTO(lcur
->bc_mp
, j
== 1, error_rcur
);
1430 XFS_WANT_CORRUPTED_GOTO(lcur
->bc_mp
, i
== 1 || j
== 1, error_rcur
);
1431 if (i
== 1 && j
== 1) {
1433 * Both the left and right records are valid. Choose the closer
1434 * inode chunk to the target.
1436 if ((pagino
- rec
->ir_startino
+ XFS_INODES_PER_CHUNK
- 1) >
1437 (rrec
.ir_startino
- pagino
)) {
1439 xfs_btree_del_cursor(lcur
, XFS_BTREE_NOERROR
);
1442 xfs_btree_del_cursor(rcur
, XFS_BTREE_NOERROR
);
1444 } else if (j
== 1) {
1445 /* only the right record is valid */
1447 xfs_btree_del_cursor(lcur
, XFS_BTREE_NOERROR
);
1449 } else if (i
== 1) {
1450 /* only the left record is valid */
1451 xfs_btree_del_cursor(rcur
, XFS_BTREE_NOERROR
);
1457 xfs_btree_del_cursor(rcur
, XFS_BTREE_ERROR
);
1462 * Use the free inode btree to find a free inode based on a newino hint. If
1463 * the hint is NULL, find the first free inode in the AG.
1466 xfs_dialloc_ag_finobt_newino(
1467 struct xfs_agi
*agi
,
1468 struct xfs_btree_cur
*cur
,
1469 struct xfs_inobt_rec_incore
*rec
)
1474 if (agi
->agi_newino
!= cpu_to_be32(NULLAGINO
)) {
1475 error
= xfs_inobt_lookup(cur
, be32_to_cpu(agi
->agi_newino
),
1480 error
= xfs_inobt_get_rec(cur
, rec
, &i
);
1483 XFS_WANT_CORRUPTED_RETURN(cur
->bc_mp
, i
== 1);
1489 * Find the first inode available in the AG.
1491 error
= xfs_inobt_lookup(cur
, 0, XFS_LOOKUP_GE
, &i
);
1494 XFS_WANT_CORRUPTED_RETURN(cur
->bc_mp
, i
== 1);
1496 error
= xfs_inobt_get_rec(cur
, rec
, &i
);
1499 XFS_WANT_CORRUPTED_RETURN(cur
->bc_mp
, i
== 1);
1505 * Update the inobt based on a modification made to the finobt. Also ensure that
1506 * the records from both trees are equivalent post-modification.
1509 xfs_dialloc_ag_update_inobt(
1510 struct xfs_btree_cur
*cur
, /* inobt cursor */
1511 struct xfs_inobt_rec_incore
*frec
, /* finobt record */
1512 int offset
) /* inode offset */
1514 struct xfs_inobt_rec_incore rec
;
1518 error
= xfs_inobt_lookup(cur
, frec
->ir_startino
, XFS_LOOKUP_EQ
, &i
);
1521 XFS_WANT_CORRUPTED_RETURN(cur
->bc_mp
, i
== 1);
1523 error
= xfs_inobt_get_rec(cur
, &rec
, &i
);
1526 XFS_WANT_CORRUPTED_RETURN(cur
->bc_mp
, i
== 1);
1527 ASSERT((XFS_AGINO_TO_OFFSET(cur
->bc_mp
, rec
.ir_startino
) %
1528 XFS_INODES_PER_CHUNK
) == 0);
1530 rec
.ir_free
&= ~XFS_INOBT_MASK(offset
);
1533 XFS_WANT_CORRUPTED_RETURN(cur
->bc_mp
, (rec
.ir_free
== frec
->ir_free
) &&
1534 (rec
.ir_freecount
== frec
->ir_freecount
));
1536 return xfs_inobt_update(cur
, &rec
);
1540 * Allocate an inode using the free inode btree, if available. Otherwise, fall
1541 * back to the inobt search algorithm.
1543 * The caller selected an AG for us, and made sure that free inodes are
1548 struct xfs_trans
*tp
,
1549 struct xfs_buf
*agbp
,
1553 struct xfs_mount
*mp
= tp
->t_mountp
;
1554 struct xfs_agi
*agi
= XFS_BUF_TO_AGI(agbp
);
1555 xfs_agnumber_t agno
= be32_to_cpu(agi
->agi_seqno
);
1556 xfs_agnumber_t pagno
= XFS_INO_TO_AGNO(mp
, parent
);
1557 xfs_agino_t pagino
= XFS_INO_TO_AGINO(mp
, parent
);
1558 struct xfs_perag
*pag
;
1559 struct xfs_btree_cur
*cur
; /* finobt cursor */
1560 struct xfs_btree_cur
*icur
; /* inobt cursor */
1561 struct xfs_inobt_rec_incore rec
;
1567 if (!xfs_sb_version_hasfinobt(&mp
->m_sb
))
1568 return xfs_dialloc_ag_inobt(tp
, agbp
, parent
, inop
);
1570 pag
= xfs_perag_get(mp
, agno
);
1573 * If pagino is 0 (this is the root inode allocation) use newino.
1574 * This must work because we've just allocated some.
1577 pagino
= be32_to_cpu(agi
->agi_newino
);
1579 cur
= xfs_inobt_init_cursor(mp
, tp
, agbp
, agno
, XFS_BTNUM_FINO
);
1581 error
= xfs_check_agi_freecount(cur
, agi
);
1586 * The search algorithm depends on whether we're in the same AG as the
1587 * parent. If so, find the closest available inode to the parent. If
1588 * not, consider the agi hint or find the first free inode in the AG.
1591 error
= xfs_dialloc_ag_finobt_near(pagino
, &cur
, &rec
);
1593 error
= xfs_dialloc_ag_finobt_newino(agi
, cur
, &rec
);
1597 offset
= xfs_inobt_first_free_inode(&rec
);
1598 ASSERT(offset
>= 0);
1599 ASSERT(offset
< XFS_INODES_PER_CHUNK
);
1600 ASSERT((XFS_AGINO_TO_OFFSET(mp
, rec
.ir_startino
) %
1601 XFS_INODES_PER_CHUNK
) == 0);
1602 ino
= XFS_AGINO_TO_INO(mp
, agno
, rec
.ir_startino
+ offset
);
1605 * Modify or remove the finobt record.
1607 rec
.ir_free
&= ~XFS_INOBT_MASK(offset
);
1609 if (rec
.ir_freecount
)
1610 error
= xfs_inobt_update(cur
, &rec
);
1612 error
= xfs_btree_delete(cur
, &i
);
1617 * The finobt has now been updated appropriately. We haven't updated the
1618 * agi and superblock yet, so we can create an inobt cursor and validate
1619 * the original freecount. If all is well, make the equivalent update to
1620 * the inobt using the finobt record and offset information.
1622 icur
= xfs_inobt_init_cursor(mp
, tp
, agbp
, agno
, XFS_BTNUM_INO
);
1624 error
= xfs_check_agi_freecount(icur
, agi
);
1628 error
= xfs_dialloc_ag_update_inobt(icur
, &rec
, offset
);
1633 * Both trees have now been updated. We must update the perag and
1634 * superblock before we can check the freecount for each btree.
1636 be32_add_cpu(&agi
->agi_freecount
, -1);
1637 xfs_ialloc_log_agi(tp
, agbp
, XFS_AGI_FREECOUNT
);
1638 pag
->pagi_freecount
--;
1640 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_IFREE
, -1);
1642 error
= xfs_check_agi_freecount(icur
, agi
);
1645 error
= xfs_check_agi_freecount(cur
, agi
);
1649 xfs_btree_del_cursor(icur
, XFS_BTREE_NOERROR
);
1650 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1656 xfs_btree_del_cursor(icur
, XFS_BTREE_ERROR
);
1658 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1664 * Allocate an inode on disk.
1666 * Mode is used to tell whether the new inode will need space, and whether it
1669 * This function is designed to be called twice if it has to do an allocation
1670 * to make more free inodes. On the first call, *IO_agbp should be set to NULL.
1671 * If an inode is available without having to performn an allocation, an inode
1672 * number is returned. In this case, *IO_agbp is set to NULL. If an allocation
1673 * needs to be done, xfs_dialloc returns the current AGI buffer in *IO_agbp.
1674 * The caller should then commit the current transaction, allocate a
1675 * new transaction, and call xfs_dialloc() again, passing in the previous value
1676 * of *IO_agbp. IO_agbp should be held across the transactions. Since the AGI
1677 * buffer is locked across the two calls, the second call is guaranteed to have
1678 * a free inode available.
1680 * Once we successfully pick an inode its number is returned and the on-disk
1681 * data structures are updated. The inode itself is not read in, since doing so
1682 * would break ordering constraints with xfs_reclaim.
1686 struct xfs_trans
*tp
,
1689 struct xfs_buf
**IO_agbp
,
1692 struct xfs_mount
*mp
= tp
->t_mountp
;
1693 struct xfs_buf
*agbp
;
1694 xfs_agnumber_t agno
;
1698 xfs_agnumber_t start_agno
;
1699 struct xfs_perag
*pag
;
1704 * If the caller passes in a pointer to the AGI buffer,
1705 * continue where we left off before. In this case, we
1706 * know that the allocation group has free inodes.
1713 * We do not have an agbp, so select an initial allocation
1714 * group for inode allocation.
1716 start_agno
= xfs_ialloc_ag_select(tp
, parent
, mode
);
1717 if (start_agno
== NULLAGNUMBER
) {
1723 * If we have already hit the ceiling of inode blocks then clear
1724 * okalloc so we scan all available agi structures for a free
1727 * Read rough value of mp->m_icount by percpu_counter_read_positive,
1728 * which will sacrifice the preciseness but improve the performance.
1730 if (mp
->m_maxicount
&&
1731 percpu_counter_read_positive(&mp
->m_icount
) + mp
->m_ialloc_inos
1732 > mp
->m_maxicount
) {
1738 * Loop until we find an allocation group that either has free inodes
1739 * or in which we can allocate some inodes. Iterate through the
1740 * allocation groups upward, wrapping at the end.
1744 pag
= xfs_perag_get(mp
, agno
);
1745 if (!pag
->pagi_inodeok
) {
1746 xfs_ialloc_next_ag(mp
);
1750 if (!pag
->pagi_init
) {
1751 error
= xfs_ialloc_pagi_init(mp
, tp
, agno
);
1757 * Do a first racy fast path check if this AG is usable.
1759 if (!pag
->pagi_freecount
&& !okalloc
)
1763 * Then read in the AGI buffer and recheck with the AGI buffer
1766 error
= xfs_ialloc_read_agi(mp
, tp
, agno
, &agbp
);
1770 if (pag
->pagi_freecount
) {
1776 goto nextag_relse_buffer
;
1779 error
= xfs_ialloc_ag_alloc(tp
, agbp
, &ialloced
);
1781 xfs_trans_brelse(tp
, agbp
);
1783 if (error
!= -ENOSPC
)
1793 * We successfully allocated some inodes, return
1794 * the current context to the caller so that it
1795 * can commit the current transaction and call
1796 * us again where we left off.
1798 ASSERT(pag
->pagi_freecount
> 0);
1806 nextag_relse_buffer
:
1807 xfs_trans_brelse(tp
, agbp
);
1810 if (++agno
== mp
->m_sb
.sb_agcount
)
1812 if (agno
== start_agno
) {
1814 return noroom
? -ENOSPC
: 0;
1820 return xfs_dialloc_ag(tp
, agbp
, parent
, inop
);
1827 * Free the blocks of an inode chunk. We must consider that the inode chunk
1828 * might be sparse and only free the regions that are allocated as part of the
1832 xfs_difree_inode_chunk(
1833 struct xfs_trans
*tp
,
1834 xfs_agnumber_t agno
,
1835 struct xfs_inobt_rec_incore
*rec
)
1837 struct xfs_mount
*mp
= tp
->t_mountp
;
1838 xfs_agblock_t sagbno
= XFS_AGINO_TO_AGBNO(mp
,
1840 int startidx
, endidx
;
1842 xfs_agblock_t agbno
;
1844 DECLARE_BITMAP(holemask
, XFS_INOBT_HOLEMASK_BITS
);
1846 if (!xfs_inobt_issparse(rec
->ir_holemask
)) {
1847 /* not sparse, calculate extent info directly */
1848 xfs_bmap_add_free(tp
, XFS_AGB_TO_FSB(mp
, agno
, sagbno
),
1849 mp
->m_ialloc_blks
, &XFS_RMAP_OINFO_INODES
);
1853 /* holemask is only 16-bits (fits in an unsigned long) */
1854 ASSERT(sizeof(rec
->ir_holemask
) <= sizeof(holemask
[0]));
1855 holemask
[0] = rec
->ir_holemask
;
1858 * Find contiguous ranges of zeroes (i.e., allocated regions) in the
1859 * holemask and convert the start/end index of each range to an extent.
1860 * We start with the start and end index both pointing at the first 0 in
1863 startidx
= endidx
= find_first_zero_bit(holemask
,
1864 XFS_INOBT_HOLEMASK_BITS
);
1865 nextbit
= startidx
+ 1;
1866 while (startidx
< XFS_INOBT_HOLEMASK_BITS
) {
1867 nextbit
= find_next_zero_bit(holemask
, XFS_INOBT_HOLEMASK_BITS
,
1870 * If the next zero bit is contiguous, update the end index of
1871 * the current range and continue.
1873 if (nextbit
!= XFS_INOBT_HOLEMASK_BITS
&&
1874 nextbit
== endidx
+ 1) {
1880 * nextbit is not contiguous with the current end index. Convert
1881 * the current start/end to an extent and add it to the free
1884 agbno
= sagbno
+ (startidx
* XFS_INODES_PER_HOLEMASK_BIT
) /
1885 mp
->m_sb
.sb_inopblock
;
1886 contigblk
= ((endidx
- startidx
+ 1) *
1887 XFS_INODES_PER_HOLEMASK_BIT
) /
1888 mp
->m_sb
.sb_inopblock
;
1890 ASSERT(agbno
% mp
->m_sb
.sb_spino_align
== 0);
1891 ASSERT(contigblk
% mp
->m_sb
.sb_spino_align
== 0);
1892 xfs_bmap_add_free(tp
, XFS_AGB_TO_FSB(mp
, agno
, agbno
),
1893 contigblk
, &XFS_RMAP_OINFO_INODES
);
1895 /* reset range to current bit and carry on... */
1896 startidx
= endidx
= nextbit
;
1905 struct xfs_mount
*mp
,
1906 struct xfs_trans
*tp
,
1907 struct xfs_buf
*agbp
,
1909 struct xfs_icluster
*xic
,
1910 struct xfs_inobt_rec_incore
*orec
)
1912 struct xfs_agi
*agi
= XFS_BUF_TO_AGI(agbp
);
1913 xfs_agnumber_t agno
= be32_to_cpu(agi
->agi_seqno
);
1914 struct xfs_perag
*pag
;
1915 struct xfs_btree_cur
*cur
;
1916 struct xfs_inobt_rec_incore rec
;
1922 ASSERT(agi
->agi_magicnum
== cpu_to_be32(XFS_AGI_MAGIC
));
1923 ASSERT(XFS_AGINO_TO_AGBNO(mp
, agino
) < be32_to_cpu(agi
->agi_length
));
1926 * Initialize the cursor.
1928 cur
= xfs_inobt_init_cursor(mp
, tp
, agbp
, agno
, XFS_BTNUM_INO
);
1930 error
= xfs_check_agi_freecount(cur
, agi
);
1935 * Look for the entry describing this inode.
1937 if ((error
= xfs_inobt_lookup(cur
, agino
, XFS_LOOKUP_LE
, &i
))) {
1938 xfs_warn(mp
, "%s: xfs_inobt_lookup() returned error %d.",
1942 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1943 error
= xfs_inobt_get_rec(cur
, &rec
, &i
);
1945 xfs_warn(mp
, "%s: xfs_inobt_get_rec() returned error %d.",
1949 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1951 * Get the offset in the inode chunk.
1953 off
= agino
- rec
.ir_startino
;
1954 ASSERT(off
>= 0 && off
< XFS_INODES_PER_CHUNK
);
1955 ASSERT(!(rec
.ir_free
& XFS_INOBT_MASK(off
)));
1957 * Mark the inode free & increment the count.
1959 rec
.ir_free
|= XFS_INOBT_MASK(off
);
1963 * When an inode chunk is free, it becomes eligible for removal. Don't
1964 * remove the chunk if the block size is large enough for multiple inode
1965 * chunks (that might not be free).
1967 if (!(mp
->m_flags
& XFS_MOUNT_IKEEP
) &&
1968 rec
.ir_free
== XFS_INOBT_ALL_FREE
&&
1969 mp
->m_sb
.sb_inopblock
<= XFS_INODES_PER_CHUNK
) {
1970 xic
->deleted
= true;
1971 xic
->first_ino
= XFS_AGINO_TO_INO(mp
, agno
, rec
.ir_startino
);
1972 xic
->alloc
= xfs_inobt_irec_to_allocmask(&rec
);
1975 * Remove the inode cluster from the AGI B+Tree, adjust the
1976 * AGI and Superblock inode counts, and mark the disk space
1977 * to be freed when the transaction is committed.
1979 ilen
= rec
.ir_freecount
;
1980 be32_add_cpu(&agi
->agi_count
, -ilen
);
1981 be32_add_cpu(&agi
->agi_freecount
, -(ilen
- 1));
1982 xfs_ialloc_log_agi(tp
, agbp
, XFS_AGI_COUNT
| XFS_AGI_FREECOUNT
);
1983 pag
= xfs_perag_get(mp
, agno
);
1984 pag
->pagi_freecount
-= ilen
- 1;
1985 pag
->pagi_count
-= ilen
;
1987 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_ICOUNT
, -ilen
);
1988 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_IFREE
, -(ilen
- 1));
1990 if ((error
= xfs_btree_delete(cur
, &i
))) {
1991 xfs_warn(mp
, "%s: xfs_btree_delete returned error %d.",
1996 xfs_difree_inode_chunk(tp
, agno
, &rec
);
1998 xic
->deleted
= false;
2000 error
= xfs_inobt_update(cur
, &rec
);
2002 xfs_warn(mp
, "%s: xfs_inobt_update returned error %d.",
2008 * Change the inode free counts and log the ag/sb changes.
2010 be32_add_cpu(&agi
->agi_freecount
, 1);
2011 xfs_ialloc_log_agi(tp
, agbp
, XFS_AGI_FREECOUNT
);
2012 pag
= xfs_perag_get(mp
, agno
);
2013 pag
->pagi_freecount
++;
2015 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_IFREE
, 1);
2018 error
= xfs_check_agi_freecount(cur
, agi
);
2023 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
2027 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
2032 * Free an inode in the free inode btree.
2036 struct xfs_mount
*mp
,
2037 struct xfs_trans
*tp
,
2038 struct xfs_buf
*agbp
,
2040 struct xfs_inobt_rec_incore
*ibtrec
) /* inobt record */
2042 struct xfs_agi
*agi
= XFS_BUF_TO_AGI(agbp
);
2043 xfs_agnumber_t agno
= be32_to_cpu(agi
->agi_seqno
);
2044 struct xfs_btree_cur
*cur
;
2045 struct xfs_inobt_rec_incore rec
;
2046 int offset
= agino
- ibtrec
->ir_startino
;
2050 cur
= xfs_inobt_init_cursor(mp
, tp
, agbp
, agno
, XFS_BTNUM_FINO
);
2052 error
= xfs_inobt_lookup(cur
, ibtrec
->ir_startino
, XFS_LOOKUP_EQ
, &i
);
2057 * If the record does not exist in the finobt, we must have just
2058 * freed an inode in a previously fully allocated chunk. If not,
2059 * something is out of sync.
2061 XFS_WANT_CORRUPTED_GOTO(mp
, ibtrec
->ir_freecount
== 1, error
);
2063 error
= xfs_inobt_insert_rec(cur
, ibtrec
->ir_holemask
,
2065 ibtrec
->ir_freecount
,
2066 ibtrec
->ir_free
, &i
);
2075 * Read and update the existing record. We could just copy the ibtrec
2076 * across here, but that would defeat the purpose of having redundant
2077 * metadata. By making the modifications independently, we can catch
2078 * corruptions that we wouldn't see if we just copied from one record
2081 error
= xfs_inobt_get_rec(cur
, &rec
, &i
);
2084 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error
);
2086 rec
.ir_free
|= XFS_INOBT_MASK(offset
);
2089 XFS_WANT_CORRUPTED_GOTO(mp
, (rec
.ir_free
== ibtrec
->ir_free
) &&
2090 (rec
.ir_freecount
== ibtrec
->ir_freecount
),
2094 * The content of inobt records should always match between the inobt
2095 * and finobt. The lifecycle of records in the finobt is different from
2096 * the inobt in that the finobt only tracks records with at least one
2097 * free inode. Hence, if all of the inodes are free and we aren't
2098 * keeping inode chunks permanently on disk, remove the record.
2099 * Otherwise, update the record with the new information.
2101 * Note that we currently can't free chunks when the block size is large
2102 * enough for multiple chunks. Leave the finobt record to remain in sync
2105 if (rec
.ir_free
== XFS_INOBT_ALL_FREE
&&
2106 mp
->m_sb
.sb_inopblock
<= XFS_INODES_PER_CHUNK
&&
2107 !(mp
->m_flags
& XFS_MOUNT_IKEEP
)) {
2108 error
= xfs_btree_delete(cur
, &i
);
2113 error
= xfs_inobt_update(cur
, &rec
);
2119 error
= xfs_check_agi_freecount(cur
, agi
);
2123 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
2127 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
2132 * Free disk inode. Carefully avoids touching the incore inode, all
2133 * manipulations incore are the caller's responsibility.
2134 * The on-disk inode is not changed by this operation, only the
2135 * btree (free inode mask) is changed.
2139 struct xfs_trans
*tp
, /* transaction pointer */
2140 xfs_ino_t inode
, /* inode to be freed */
2141 struct xfs_icluster
*xic
) /* cluster info if deleted */
2144 xfs_agblock_t agbno
; /* block number containing inode */
2145 struct xfs_buf
*agbp
; /* buffer for allocation group header */
2146 xfs_agino_t agino
; /* allocation group inode number */
2147 xfs_agnumber_t agno
; /* allocation group number */
2148 int error
; /* error return value */
2149 struct xfs_mount
*mp
; /* mount structure for filesystem */
2150 struct xfs_inobt_rec_incore rec
;/* btree record */
2155 * Break up inode number into its components.
2157 agno
= XFS_INO_TO_AGNO(mp
, inode
);
2158 if (agno
>= mp
->m_sb
.sb_agcount
) {
2159 xfs_warn(mp
, "%s: agno >= mp->m_sb.sb_agcount (%d >= %d).",
2160 __func__
, agno
, mp
->m_sb
.sb_agcount
);
2164 agino
= XFS_INO_TO_AGINO(mp
, inode
);
2165 if (inode
!= XFS_AGINO_TO_INO(mp
, agno
, agino
)) {
2166 xfs_warn(mp
, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).",
2167 __func__
, (unsigned long long)inode
,
2168 (unsigned long long)XFS_AGINO_TO_INO(mp
, agno
, agino
));
2172 agbno
= XFS_AGINO_TO_AGBNO(mp
, agino
);
2173 if (agbno
>= mp
->m_sb
.sb_agblocks
) {
2174 xfs_warn(mp
, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).",
2175 __func__
, agbno
, mp
->m_sb
.sb_agblocks
);
2180 * Get the allocation group header.
2182 error
= xfs_ialloc_read_agi(mp
, tp
, agno
, &agbp
);
2184 xfs_warn(mp
, "%s: xfs_ialloc_read_agi() returned error %d.",
2190 * Fix up the inode allocation btree.
2192 error
= xfs_difree_inobt(mp
, tp
, agbp
, agino
, xic
, &rec
);
2197 * Fix up the free inode btree.
2199 if (xfs_sb_version_hasfinobt(&mp
->m_sb
)) {
2200 error
= xfs_difree_finobt(mp
, tp
, agbp
, agino
, &rec
);
2213 struct xfs_mount
*mp
,
2214 struct xfs_trans
*tp
,
2215 xfs_agnumber_t agno
,
2217 xfs_agblock_t agbno
,
2218 xfs_agblock_t
*chunk_agbno
,
2219 xfs_agblock_t
*offset_agbno
,
2222 struct xfs_inobt_rec_incore rec
;
2223 struct xfs_btree_cur
*cur
;
2224 struct xfs_buf
*agbp
;
2228 error
= xfs_ialloc_read_agi(mp
, tp
, agno
, &agbp
);
2231 "%s: xfs_ialloc_read_agi() returned error %d, agno %d",
2232 __func__
, error
, agno
);
2237 * Lookup the inode record for the given agino. If the record cannot be
2238 * found, then it's an invalid inode number and we should abort. Once
2239 * we have a record, we need to ensure it contains the inode number
2240 * we are looking up.
2242 cur
= xfs_inobt_init_cursor(mp
, tp
, agbp
, agno
, XFS_BTNUM_INO
);
2243 error
= xfs_inobt_lookup(cur
, agino
, XFS_LOOKUP_LE
, &i
);
2246 error
= xfs_inobt_get_rec(cur
, &rec
, &i
);
2247 if (!error
&& i
== 0)
2251 xfs_trans_brelse(tp
, agbp
);
2252 xfs_btree_del_cursor(cur
, error
);
2256 /* check that the returned record contains the required inode */
2257 if (rec
.ir_startino
> agino
||
2258 rec
.ir_startino
+ mp
->m_ialloc_inos
<= agino
)
2261 /* for untrusted inodes check it is allocated first */
2262 if ((flags
& XFS_IGET_UNTRUSTED
) &&
2263 (rec
.ir_free
& XFS_INOBT_MASK(agino
- rec
.ir_startino
)))
2266 *chunk_agbno
= XFS_AGINO_TO_AGBNO(mp
, rec
.ir_startino
);
2267 *offset_agbno
= agbno
- *chunk_agbno
;
2272 * Return the location of the inode in imap, for mapping it into a buffer.
2276 xfs_mount_t
*mp
, /* file system mount structure */
2277 xfs_trans_t
*tp
, /* transaction pointer */
2278 xfs_ino_t ino
, /* inode to locate */
2279 struct xfs_imap
*imap
, /* location map structure */
2280 uint flags
) /* flags for inode btree lookup */
2282 xfs_agblock_t agbno
; /* block number of inode in the alloc group */
2283 xfs_agino_t agino
; /* inode number within alloc group */
2284 xfs_agnumber_t agno
; /* allocation group number */
2285 xfs_agblock_t chunk_agbno
; /* first block in inode chunk */
2286 xfs_agblock_t cluster_agbno
; /* first block in inode cluster */
2287 int error
; /* error code */
2288 int offset
; /* index of inode in its buffer */
2289 xfs_agblock_t offset_agbno
; /* blks from chunk start to inode */
2291 ASSERT(ino
!= NULLFSINO
);
2294 * Split up the inode number into its parts.
2296 agno
= XFS_INO_TO_AGNO(mp
, ino
);
2297 agino
= XFS_INO_TO_AGINO(mp
, ino
);
2298 agbno
= XFS_AGINO_TO_AGBNO(mp
, agino
);
2299 if (agno
>= mp
->m_sb
.sb_agcount
|| agbno
>= mp
->m_sb
.sb_agblocks
||
2300 ino
!= XFS_AGINO_TO_INO(mp
, agno
, agino
)) {
2303 * Don't output diagnostic information for untrusted inodes
2304 * as they can be invalid without implying corruption.
2306 if (flags
& XFS_IGET_UNTRUSTED
)
2308 if (agno
>= mp
->m_sb
.sb_agcount
) {
2310 "%s: agno (%d) >= mp->m_sb.sb_agcount (%d)",
2311 __func__
, agno
, mp
->m_sb
.sb_agcount
);
2313 if (agbno
>= mp
->m_sb
.sb_agblocks
) {
2315 "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)",
2316 __func__
, (unsigned long long)agbno
,
2317 (unsigned long)mp
->m_sb
.sb_agblocks
);
2319 if (ino
!= XFS_AGINO_TO_INO(mp
, agno
, agino
)) {
2321 "%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)",
2323 XFS_AGINO_TO_INO(mp
, agno
, agino
));
2331 * For bulkstat and handle lookups, we have an untrusted inode number
2332 * that we have to verify is valid. We cannot do this just by reading
2333 * the inode buffer as it may have been unlinked and removed leaving
2334 * inodes in stale state on disk. Hence we have to do a btree lookup
2335 * in all cases where an untrusted inode number is passed.
2337 if (flags
& XFS_IGET_UNTRUSTED
) {
2338 error
= xfs_imap_lookup(mp
, tp
, agno
, agino
, agbno
,
2339 &chunk_agbno
, &offset_agbno
, flags
);
2346 * If the inode cluster size is the same as the blocksize or
2347 * smaller we get to the buffer by simple arithmetics.
2349 if (mp
->m_blocks_per_cluster
== 1) {
2350 offset
= XFS_INO_TO_OFFSET(mp
, ino
);
2351 ASSERT(offset
< mp
->m_sb
.sb_inopblock
);
2353 imap
->im_blkno
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
);
2354 imap
->im_len
= XFS_FSB_TO_BB(mp
, 1);
2355 imap
->im_boffset
= (unsigned short)(offset
<<
2356 mp
->m_sb
.sb_inodelog
);
2361 * If the inode chunks are aligned then use simple maths to
2362 * find the location. Otherwise we have to do a btree
2363 * lookup to find the location.
2365 if (mp
->m_inoalign_mask
) {
2366 offset_agbno
= agbno
& mp
->m_inoalign_mask
;
2367 chunk_agbno
= agbno
- offset_agbno
;
2369 error
= xfs_imap_lookup(mp
, tp
, agno
, agino
, agbno
,
2370 &chunk_agbno
, &offset_agbno
, flags
);
2376 ASSERT(agbno
>= chunk_agbno
);
2377 cluster_agbno
= chunk_agbno
+
2378 ((offset_agbno
/ mp
->m_blocks_per_cluster
) *
2379 mp
->m_blocks_per_cluster
);
2380 offset
= ((agbno
- cluster_agbno
) * mp
->m_sb
.sb_inopblock
) +
2381 XFS_INO_TO_OFFSET(mp
, ino
);
2383 imap
->im_blkno
= XFS_AGB_TO_DADDR(mp
, agno
, cluster_agbno
);
2384 imap
->im_len
= XFS_FSB_TO_BB(mp
, mp
->m_blocks_per_cluster
);
2385 imap
->im_boffset
= (unsigned short)(offset
<< mp
->m_sb
.sb_inodelog
);
2388 * If the inode number maps to a block outside the bounds
2389 * of the file system then return NULL rather than calling
2390 * read_buf and panicing when we get an error from the
2393 if ((imap
->im_blkno
+ imap
->im_len
) >
2394 XFS_FSB_TO_BB(mp
, mp
->m_sb
.sb_dblocks
)) {
2396 "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)",
2397 __func__
, (unsigned long long) imap
->im_blkno
,
2398 (unsigned long long) imap
->im_len
,
2399 XFS_FSB_TO_BB(mp
, mp
->m_sb
.sb_dblocks
));
2406 * Compute and fill in value of m_in_maxlevels.
2409 xfs_ialloc_compute_maxlevels(
2410 xfs_mount_t
*mp
) /* file system mount structure */
2414 inodes
= (1LL << XFS_INO_AGINO_BITS(mp
)) >> XFS_INODES_PER_CHUNK_LOG
;
2415 mp
->m_in_maxlevels
= xfs_btree_compute_maxlevels(mp
->m_inobt_mnr
,
2420 * Log specified fields for the ag hdr (inode section). The growth of the agi
2421 * structure over time requires that we interpret the buffer as two logical
2422 * regions delineated by the end of the unlinked list. This is due to the size
2423 * of the hash table and its location in the middle of the agi.
2425 * For example, a request to log a field before agi_unlinked and a field after
2426 * agi_unlinked could cause us to log the entire hash table and use an excessive
2427 * amount of log space. To avoid this behavior, log the region up through
2428 * agi_unlinked in one call and the region after agi_unlinked through the end of
2429 * the structure in another.
2433 xfs_trans_t
*tp
, /* transaction pointer */
2434 xfs_buf_t
*bp
, /* allocation group header buffer */
2435 int fields
) /* bitmask of fields to log */
2437 int first
; /* first byte number */
2438 int last
; /* last byte number */
2439 static const short offsets
[] = { /* field starting offsets */
2440 /* keep in sync with bit definitions */
2441 offsetof(xfs_agi_t
, agi_magicnum
),
2442 offsetof(xfs_agi_t
, agi_versionnum
),
2443 offsetof(xfs_agi_t
, agi_seqno
),
2444 offsetof(xfs_agi_t
, agi_length
),
2445 offsetof(xfs_agi_t
, agi_count
),
2446 offsetof(xfs_agi_t
, agi_root
),
2447 offsetof(xfs_agi_t
, agi_level
),
2448 offsetof(xfs_agi_t
, agi_freecount
),
2449 offsetof(xfs_agi_t
, agi_newino
),
2450 offsetof(xfs_agi_t
, agi_dirino
),
2451 offsetof(xfs_agi_t
, agi_unlinked
),
2452 offsetof(xfs_agi_t
, agi_free_root
),
2453 offsetof(xfs_agi_t
, agi_free_level
),
2457 xfs_agi_t
*agi
; /* allocation group header */
2459 agi
= XFS_BUF_TO_AGI(bp
);
2460 ASSERT(agi
->agi_magicnum
== cpu_to_be32(XFS_AGI_MAGIC
));
2464 * Compute byte offsets for the first and last fields in the first
2465 * region and log the agi buffer. This only logs up through
2468 if (fields
& XFS_AGI_ALL_BITS_R1
) {
2469 xfs_btree_offsets(fields
, offsets
, XFS_AGI_NUM_BITS_R1
,
2471 xfs_trans_log_buf(tp
, bp
, first
, last
);
2475 * Mask off the bits in the first region and calculate the first and
2476 * last field offsets for any bits in the second region.
2478 fields
&= ~XFS_AGI_ALL_BITS_R1
;
2480 xfs_btree_offsets(fields
, offsets
, XFS_AGI_NUM_BITS_R2
,
2482 xfs_trans_log_buf(tp
, bp
, first
, last
);
2486 static xfs_failaddr_t
2490 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
2491 struct xfs_agi
*agi
= XFS_BUF_TO_AGI(bp
);
2494 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
2495 if (!uuid_equal(&agi
->agi_uuid
, &mp
->m_sb
.sb_meta_uuid
))
2496 return __this_address
;
2497 if (!xfs_log_check_lsn(mp
,
2498 be64_to_cpu(XFS_BUF_TO_AGI(bp
)->agi_lsn
)))
2499 return __this_address
;
2503 * Validate the magic number of the agi block.
2505 if (agi
->agi_magicnum
!= cpu_to_be32(XFS_AGI_MAGIC
))
2506 return __this_address
;
2507 if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi
->agi_versionnum
)))
2508 return __this_address
;
2510 if (be32_to_cpu(agi
->agi_level
) < 1 ||
2511 be32_to_cpu(agi
->agi_level
) > XFS_BTREE_MAXLEVELS
)
2512 return __this_address
;
2514 if (xfs_sb_version_hasfinobt(&mp
->m_sb
) &&
2515 (be32_to_cpu(agi
->agi_free_level
) < 1 ||
2516 be32_to_cpu(agi
->agi_free_level
) > XFS_BTREE_MAXLEVELS
))
2517 return __this_address
;
2520 * during growfs operations, the perag is not fully initialised,
2521 * so we can't use it for any useful checking. growfs ensures we can't
2522 * use it by using uncached buffers that don't have the perag attached
2523 * so we can detect and avoid this problem.
2525 if (bp
->b_pag
&& be32_to_cpu(agi
->agi_seqno
) != bp
->b_pag
->pag_agno
)
2526 return __this_address
;
2528 for (i
= 0; i
< XFS_AGI_UNLINKED_BUCKETS
; i
++) {
2529 if (agi
->agi_unlinked
[i
] == cpu_to_be32(NULLAGINO
))
2531 if (!xfs_verify_ino(mp
, be32_to_cpu(agi
->agi_unlinked
[i
])))
2532 return __this_address
;
2539 xfs_agi_read_verify(
2542 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
2545 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
2546 !xfs_buf_verify_cksum(bp
, XFS_AGI_CRC_OFF
))
2547 xfs_verifier_error(bp
, -EFSBADCRC
, __this_address
);
2549 fa
= xfs_agi_verify(bp
);
2550 if (XFS_TEST_ERROR(fa
, mp
, XFS_ERRTAG_IALLOC_READ_AGI
))
2551 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
2556 xfs_agi_write_verify(
2559 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
2560 struct xfs_buf_log_item
*bip
= bp
->b_log_item
;
2563 fa
= xfs_agi_verify(bp
);
2565 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
2569 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
2573 XFS_BUF_TO_AGI(bp
)->agi_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
2574 xfs_buf_update_cksum(bp
, XFS_AGI_CRC_OFF
);
2577 const struct xfs_buf_ops xfs_agi_buf_ops
= {
2579 .verify_read
= xfs_agi_read_verify
,
2580 .verify_write
= xfs_agi_write_verify
,
2581 .verify_struct
= xfs_agi_verify
,
2585 * Read in the allocation group header (inode allocation section)
2589 struct xfs_mount
*mp
, /* file system mount structure */
2590 struct xfs_trans
*tp
, /* transaction pointer */
2591 xfs_agnumber_t agno
, /* allocation group number */
2592 struct xfs_buf
**bpp
) /* allocation group hdr buf */
2596 trace_xfs_read_agi(mp
, agno
);
2598 ASSERT(agno
!= NULLAGNUMBER
);
2599 error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
,
2600 XFS_AG_DADDR(mp
, agno
, XFS_AGI_DADDR(mp
)),
2601 XFS_FSS_TO_BB(mp
, 1), 0, bpp
, &xfs_agi_buf_ops
);
2605 xfs_trans_buf_set_type(tp
, *bpp
, XFS_BLFT_AGI_BUF
);
2607 xfs_buf_set_ref(*bpp
, XFS_AGI_REF
);
2612 xfs_ialloc_read_agi(
2613 struct xfs_mount
*mp
, /* file system mount structure */
2614 struct xfs_trans
*tp
, /* transaction pointer */
2615 xfs_agnumber_t agno
, /* allocation group number */
2616 struct xfs_buf
**bpp
) /* allocation group hdr buf */
2618 struct xfs_agi
*agi
; /* allocation group header */
2619 struct xfs_perag
*pag
; /* per allocation group data */
2622 trace_xfs_ialloc_read_agi(mp
, agno
);
2624 error
= xfs_read_agi(mp
, tp
, agno
, bpp
);
2628 agi
= XFS_BUF_TO_AGI(*bpp
);
2629 pag
= xfs_perag_get(mp
, agno
);
2630 if (!pag
->pagi_init
) {
2631 pag
->pagi_freecount
= be32_to_cpu(agi
->agi_freecount
);
2632 pag
->pagi_count
= be32_to_cpu(agi
->agi_count
);
2637 * It's possible for these to be out of sync if
2638 * we are in the middle of a forced shutdown.
2640 ASSERT(pag
->pagi_freecount
== be32_to_cpu(agi
->agi_freecount
) ||
2641 XFS_FORCED_SHUTDOWN(mp
));
2647 * Read in the agi to initialise the per-ag data in the mount structure
2650 xfs_ialloc_pagi_init(
2651 xfs_mount_t
*mp
, /* file system mount structure */
2652 xfs_trans_t
*tp
, /* transaction pointer */
2653 xfs_agnumber_t agno
) /* allocation group number */
2655 xfs_buf_t
*bp
= NULL
;
2658 error
= xfs_ialloc_read_agi(mp
, tp
, agno
, &bp
);
2662 xfs_trans_brelse(tp
, bp
);
2666 /* Is there an inode record covering a given range of inode numbers? */
2668 xfs_ialloc_has_inode_record(
2669 struct xfs_btree_cur
*cur
,
2674 struct xfs_inobt_rec_incore irec
;
2682 error
= xfs_inobt_lookup(cur
, low
, XFS_LOOKUP_LE
, &has_record
);
2683 while (error
== 0 && has_record
) {
2684 error
= xfs_inobt_get_rec(cur
, &irec
, &has_record
);
2685 if (error
|| irec
.ir_startino
> high
)
2688 agino
= irec
.ir_startino
;
2689 holemask
= irec
.ir_holemask
;
2690 for (i
= 0; i
< XFS_INOBT_HOLEMASK_BITS
; holemask
>>= 1,
2691 i
++, agino
+= XFS_INODES_PER_HOLEMASK_BIT
) {
2694 if (agino
+ XFS_INODES_PER_HOLEMASK_BIT
> low
&&
2701 error
= xfs_btree_increment(cur
, 0, &has_record
);
2706 /* Is there an inode record covering a given extent? */
2708 xfs_ialloc_has_inodes_at_extent(
2709 struct xfs_btree_cur
*cur
,
2717 low
= XFS_AGB_TO_AGINO(cur
->bc_mp
, bno
);
2718 high
= XFS_AGB_TO_AGINO(cur
->bc_mp
, bno
+ len
) - 1;
2720 return xfs_ialloc_has_inode_record(cur
, low
, high
, exists
);
2723 struct xfs_ialloc_count_inodes
{
2725 xfs_agino_t freecount
;
2728 /* Record inode counts across all inobt records. */
2730 xfs_ialloc_count_inodes_rec(
2731 struct xfs_btree_cur
*cur
,
2732 union xfs_btree_rec
*rec
,
2735 struct xfs_inobt_rec_incore irec
;
2736 struct xfs_ialloc_count_inodes
*ci
= priv
;
2738 xfs_inobt_btrec_to_irec(cur
->bc_mp
, rec
, &irec
);
2739 ci
->count
+= irec
.ir_count
;
2740 ci
->freecount
+= irec
.ir_freecount
;
2745 /* Count allocated and free inodes under an inobt. */
2747 xfs_ialloc_count_inodes(
2748 struct xfs_btree_cur
*cur
,
2750 xfs_agino_t
*freecount
)
2752 struct xfs_ialloc_count_inodes ci
= {0};
2755 ASSERT(cur
->bc_btnum
== XFS_BTNUM_INO
);
2756 error
= xfs_btree_query_all(cur
, xfs_ialloc_count_inodes_rec
, &ci
);
2761 *freecount
= ci
.freecount
;