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_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
26 #include "xfs_mount.h"
27 #include "xfs_inode.h"
28 #include "xfs_btree.h"
29 #include "xfs_ialloc.h"
30 #include "xfs_ialloc_btree.h"
31 #include "xfs_alloc.h"
32 #include "xfs_rtalloc.h"
33 #include "xfs_error.h"
35 #include "xfs_cksum.h"
36 #include "xfs_trans.h"
37 #include "xfs_buf_item.h"
38 #include "xfs_icreate_item.h"
39 #include "xfs_icache.h"
40 #include "xfs_trace.h"
44 * Allocation group level functions.
47 xfs_ialloc_cluster_alignment(
50 if (xfs_sb_version_hasalign(&mp
->m_sb
) &&
51 mp
->m_sb
.sb_inoalignmt
>=
52 XFS_B_TO_FSBT(mp
, mp
->m_inode_cluster_size
))
53 return mp
->m_sb
.sb_inoalignmt
;
58 * Lookup a record by ino in the btree given by cur.
62 struct xfs_btree_cur
*cur
, /* btree cursor */
63 xfs_agino_t ino
, /* starting inode of chunk */
64 xfs_lookup_t dir
, /* <=, >=, == */
65 int *stat
) /* success/failure */
67 cur
->bc_rec
.i
.ir_startino
= ino
;
68 cur
->bc_rec
.i
.ir_freecount
= 0;
69 cur
->bc_rec
.i
.ir_free
= 0;
70 return xfs_btree_lookup(cur
, dir
, stat
);
74 * Update the record referred to by cur to the value given.
75 * This either works (return 0) or gets an EFSCORRUPTED error.
77 STATIC
int /* error */
79 struct xfs_btree_cur
*cur
, /* btree cursor */
80 xfs_inobt_rec_incore_t
*irec
) /* btree record */
82 union xfs_btree_rec rec
;
84 rec
.inobt
.ir_startino
= cpu_to_be32(irec
->ir_startino
);
85 rec
.inobt
.ir_freecount
= cpu_to_be32(irec
->ir_freecount
);
86 rec
.inobt
.ir_free
= cpu_to_be64(irec
->ir_free
);
87 return xfs_btree_update(cur
, &rec
);
91 * Get the data from the pointed-to record.
95 struct xfs_btree_cur
*cur
, /* btree cursor */
96 xfs_inobt_rec_incore_t
*irec
, /* btree record */
97 int *stat
) /* output: success/failure */
99 union xfs_btree_rec
*rec
;
102 error
= xfs_btree_get_rec(cur
, &rec
, stat
);
103 if (!error
&& *stat
== 1) {
104 irec
->ir_startino
= be32_to_cpu(rec
->inobt
.ir_startino
);
105 irec
->ir_freecount
= be32_to_cpu(rec
->inobt
.ir_freecount
);
106 irec
->ir_free
= be64_to_cpu(rec
->inobt
.ir_free
);
112 * Insert a single inobt record. Cursor must already point to desired location.
115 xfs_inobt_insert_rec(
116 struct xfs_btree_cur
*cur
,
121 cur
->bc_rec
.i
.ir_freecount
= freecount
;
122 cur
->bc_rec
.i
.ir_free
= free
;
123 return xfs_btree_insert(cur
, stat
);
127 * Insert records describing a newly allocated inode chunk into the inobt.
131 struct xfs_mount
*mp
,
132 struct xfs_trans
*tp
,
133 struct xfs_buf
*agbp
,
138 struct xfs_btree_cur
*cur
;
139 struct xfs_agi
*agi
= XFS_BUF_TO_AGI(agbp
);
140 xfs_agnumber_t agno
= be32_to_cpu(agi
->agi_seqno
);
145 cur
= xfs_inobt_init_cursor(mp
, tp
, agbp
, agno
, btnum
);
147 for (thisino
= newino
;
148 thisino
< newino
+ newlen
;
149 thisino
+= XFS_INODES_PER_CHUNK
) {
150 error
= xfs_inobt_lookup(cur
, thisino
, XFS_LOOKUP_EQ
, &i
);
152 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
157 error
= xfs_inobt_insert_rec(cur
, XFS_INODES_PER_CHUNK
,
158 XFS_INOBT_ALL_FREE
, &i
);
160 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
166 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
172 * Verify that the number of free inodes in the AGI is correct.
176 xfs_check_agi_freecount(
177 struct xfs_btree_cur
*cur
,
180 if (cur
->bc_nlevels
== 1) {
181 xfs_inobt_rec_incore_t rec
;
186 error
= xfs_inobt_lookup(cur
, 0, XFS_LOOKUP_GE
, &i
);
191 error
= xfs_inobt_get_rec(cur
, &rec
, &i
);
196 freecount
+= rec
.ir_freecount
;
197 error
= xfs_btree_increment(cur
, 0, &i
);
203 if (!XFS_FORCED_SHUTDOWN(cur
->bc_mp
))
204 ASSERT(freecount
== be32_to_cpu(agi
->agi_freecount
));
209 #define xfs_check_agi_freecount(cur, agi) 0
213 * Initialise a new set of inodes. When called without a transaction context
214 * (e.g. from recovery) we initiate a delayed write of the inode buffers rather
215 * than logging them (which in a transaction context puts them into the AIL
216 * for writeback rather than the xfsbufd queue).
219 xfs_ialloc_inode_init(
220 struct xfs_mount
*mp
,
221 struct xfs_trans
*tp
,
222 struct list_head
*buffer_list
,
225 xfs_agblock_t length
,
228 struct xfs_buf
*fbuf
;
229 struct xfs_dinode
*free
;
230 int nbufs
, blks_per_cluster
, inodes_per_cluster
;
237 * Loop over the new block(s), filling in the inodes. For small block
238 * sizes, manipulate the inodes in buffers which are multiples of the
241 blks_per_cluster
= xfs_icluster_size_fsb(mp
);
242 inodes_per_cluster
= blks_per_cluster
<< mp
->m_sb
.sb_inopblog
;
243 nbufs
= length
/ blks_per_cluster
;
246 * Figure out what version number to use in the inodes we create. If
247 * the superblock version has caught up to the one that supports the new
248 * inode format, then use the new inode version. Otherwise use the old
249 * version so that old kernels will continue to be able to use the file
252 * For v3 inodes, we also need to write the inode number into the inode,
253 * so calculate the first inode number of the chunk here as
254 * XFS_OFFBNO_TO_AGINO() only works within a filesystem block, not
255 * across multiple filesystem blocks (such as a cluster) and so cannot
256 * be used in the cluster buffer loop below.
258 * Further, because we are writing the inode directly into the buffer
259 * and calculating a CRC on the entire inode, we have ot log the entire
260 * inode so that the entire range the CRC covers is present in the log.
261 * That means for v3 inode we log the entire buffer rather than just the
264 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
266 ino
= XFS_AGINO_TO_INO(mp
, agno
,
267 XFS_OFFBNO_TO_AGINO(mp
, agbno
, 0));
270 * log the initialisation that is about to take place as an
271 * logical operation. This means the transaction does not
272 * need to log the physical changes to the inode buffers as log
273 * recovery will know what initialisation is actually needed.
274 * Hence we only need to log the buffers as "ordered" buffers so
275 * they track in the AIL as if they were physically logged.
278 xfs_icreate_log(tp
, agno
, agbno
, mp
->m_ialloc_inos
,
279 mp
->m_sb
.sb_inodesize
, length
, gen
);
283 for (j
= 0; j
< nbufs
; j
++) {
287 d
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
+ (j
* blks_per_cluster
));
288 fbuf
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
,
289 mp
->m_bsize
* blks_per_cluster
,
294 /* Initialize the inode buffers and log them appropriately. */
295 fbuf
->b_ops
= &xfs_inode_buf_ops
;
296 xfs_buf_zero(fbuf
, 0, BBTOB(fbuf
->b_length
));
297 for (i
= 0; i
< inodes_per_cluster
; i
++) {
298 int ioffset
= i
<< mp
->m_sb
.sb_inodelog
;
299 uint isize
= xfs_dinode_size(version
);
301 free
= xfs_make_iptr(mp
, fbuf
, i
);
302 free
->di_magic
= cpu_to_be16(XFS_DINODE_MAGIC
);
303 free
->di_version
= version
;
304 free
->di_gen
= cpu_to_be32(gen
);
305 free
->di_next_unlinked
= cpu_to_be32(NULLAGINO
);
308 free
->di_ino
= cpu_to_be64(ino
);
310 uuid_copy(&free
->di_uuid
, &mp
->m_sb
.sb_uuid
);
311 xfs_dinode_calc_crc(mp
, free
);
313 /* just log the inode core */
314 xfs_trans_log_buf(tp
, fbuf
, ioffset
,
315 ioffset
+ isize
- 1);
321 * Mark the buffer as an inode allocation buffer so it
322 * sticks in AIL at the point of this allocation
323 * transaction. This ensures the they are on disk before
324 * the tail of the log can be moved past this
325 * transaction (i.e. by preventing relogging from moving
326 * it forward in the log).
328 xfs_trans_inode_alloc_buf(tp
, fbuf
);
331 * Mark the buffer as ordered so that they are
332 * not physically logged in the transaction but
333 * still tracked in the AIL as part of the
334 * transaction and pin the log appropriately.
336 xfs_trans_ordered_buf(tp
, fbuf
);
337 xfs_trans_log_buf(tp
, fbuf
, 0,
338 BBTOB(fbuf
->b_length
) - 1);
341 fbuf
->b_flags
|= XBF_DONE
;
342 xfs_buf_delwri_queue(fbuf
, buffer_list
);
350 * Allocate new inodes in the allocation group specified by agbp.
351 * Return 0 for success, else error code.
353 STATIC
int /* error code or 0 */
355 xfs_trans_t
*tp
, /* transaction pointer */
356 xfs_buf_t
*agbp
, /* alloc group buffer */
359 xfs_agi_t
*agi
; /* allocation group header */
360 xfs_alloc_arg_t args
; /* allocation argument structure */
363 xfs_agino_t newino
; /* new first inode's number */
364 xfs_agino_t newlen
; /* new number of inodes */
365 int isaligned
= 0; /* inode allocation at stripe unit */
367 struct xfs_perag
*pag
;
369 memset(&args
, 0, sizeof(args
));
371 args
.mp
= tp
->t_mountp
;
374 * Locking will ensure that we don't have two callers in here
377 newlen
= args
.mp
->m_ialloc_inos
;
378 if (args
.mp
->m_maxicount
&&
379 percpu_counter_read(&args
.mp
->m_icount
) + newlen
>
380 args
.mp
->m_maxicount
)
382 args
.minlen
= args
.maxlen
= args
.mp
->m_ialloc_blks
;
384 * First try to allocate inodes contiguous with the last-allocated
385 * chunk of inodes. If the filesystem is striped, this will fill
386 * an entire stripe unit with inodes.
388 agi
= XFS_BUF_TO_AGI(agbp
);
389 newino
= be32_to_cpu(agi
->agi_newino
);
390 agno
= be32_to_cpu(agi
->agi_seqno
);
391 args
.agbno
= XFS_AGINO_TO_AGBNO(args
.mp
, newino
) +
392 args
.mp
->m_ialloc_blks
;
393 if (likely(newino
!= NULLAGINO
&&
394 (args
.agbno
< be32_to_cpu(agi
->agi_length
)))) {
395 args
.fsbno
= XFS_AGB_TO_FSB(args
.mp
, agno
, args
.agbno
);
396 args
.type
= XFS_ALLOCTYPE_THIS_BNO
;
400 * We need to take into account alignment here to ensure that
401 * we don't modify the free list if we fail to have an exact
402 * block. If we don't have an exact match, and every oher
403 * attempt allocation attempt fails, we'll end up cancelling
404 * a dirty transaction and shutting down.
406 * For an exact allocation, alignment must be 1,
407 * however we need to take cluster alignment into account when
408 * fixing up the freelist. Use the minalignslop field to
409 * indicate that extra blocks might be required for alignment,
410 * but not to use them in the actual exact allocation.
413 args
.minalignslop
= xfs_ialloc_cluster_alignment(args
.mp
) - 1;
415 /* Allow space for the inode btree to split. */
416 args
.minleft
= args
.mp
->m_in_maxlevels
- 1;
417 if ((error
= xfs_alloc_vextent(&args
)))
421 * This request might have dirtied the transaction if the AG can
422 * satisfy the request, but the exact block was not available.
423 * If the allocation did fail, subsequent requests will relax
424 * the exact agbno requirement and increase the alignment
425 * instead. It is critical that the total size of the request
426 * (len + alignment + slop) does not increase from this point
427 * on, so reset minalignslop to ensure it is not included in
428 * subsequent requests.
430 args
.minalignslop
= 0;
432 args
.fsbno
= NULLFSBLOCK
;
434 if (unlikely(args
.fsbno
== NULLFSBLOCK
)) {
436 * Set the alignment for the allocation.
437 * If stripe alignment is turned on then align at stripe unit
439 * If the cluster size is smaller than a filesystem block
440 * then we're doing I/O for inodes in filesystem block size
441 * pieces, so don't need alignment anyway.
444 if (args
.mp
->m_sinoalign
) {
445 ASSERT(!(args
.mp
->m_flags
& XFS_MOUNT_NOALIGN
));
446 args
.alignment
= args
.mp
->m_dalign
;
449 args
.alignment
= xfs_ialloc_cluster_alignment(args
.mp
);
451 * Need to figure out where to allocate the inode blocks.
452 * Ideally they should be spaced out through the a.g.
453 * For now, just allocate blocks up front.
455 args
.agbno
= be32_to_cpu(agi
->agi_root
);
456 args
.fsbno
= XFS_AGB_TO_FSB(args
.mp
, agno
, args
.agbno
);
458 * Allocate a fixed-size extent of inodes.
460 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
463 * Allow space for the inode btree to split.
465 args
.minleft
= args
.mp
->m_in_maxlevels
- 1;
466 if ((error
= xfs_alloc_vextent(&args
)))
471 * If stripe alignment is turned on, then try again with cluster
474 if (isaligned
&& args
.fsbno
== NULLFSBLOCK
) {
475 args
.type
= XFS_ALLOCTYPE_NEAR_BNO
;
476 args
.agbno
= be32_to_cpu(agi
->agi_root
);
477 args
.fsbno
= XFS_AGB_TO_FSB(args
.mp
, agno
, args
.agbno
);
478 args
.alignment
= xfs_ialloc_cluster_alignment(args
.mp
);
479 if ((error
= xfs_alloc_vextent(&args
)))
483 if (args
.fsbno
== NULLFSBLOCK
) {
487 ASSERT(args
.len
== args
.minlen
);
490 * Stamp and write the inode buffers.
492 * Seed the new inode cluster with a random generation number. This
493 * prevents short-term reuse of generation numbers if a chunk is
494 * freed and then immediately reallocated. We use random numbers
495 * rather than a linear progression to prevent the next generation
496 * number from being easily guessable.
498 error
= xfs_ialloc_inode_init(args
.mp
, tp
, NULL
, agno
, args
.agbno
,
499 args
.len
, prandom_u32());
504 * Convert the results.
506 newino
= XFS_OFFBNO_TO_AGINO(args
.mp
, args
.agbno
, 0);
507 be32_add_cpu(&agi
->agi_count
, newlen
);
508 be32_add_cpu(&agi
->agi_freecount
, newlen
);
509 pag
= xfs_perag_get(args
.mp
, agno
);
510 pag
->pagi_freecount
+= newlen
;
512 agi
->agi_newino
= cpu_to_be32(newino
);
515 * Insert records describing the new inode chunk into the btrees.
517 error
= xfs_inobt_insert(args
.mp
, tp
, agbp
, newino
, newlen
,
522 if (xfs_sb_version_hasfinobt(&args
.mp
->m_sb
)) {
523 error
= xfs_inobt_insert(args
.mp
, tp
, agbp
, newino
, newlen
,
529 * Log allocation group header fields
531 xfs_ialloc_log_agi(tp
, agbp
,
532 XFS_AGI_COUNT
| XFS_AGI_FREECOUNT
| XFS_AGI_NEWINO
);
534 * Modify/log superblock values for inode count and inode free count.
536 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_ICOUNT
, (long)newlen
);
537 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_IFREE
, (long)newlen
);
542 STATIC xfs_agnumber_t
548 spin_lock(&mp
->m_agirotor_lock
);
549 agno
= mp
->m_agirotor
;
550 if (++mp
->m_agirotor
>= mp
->m_maxagi
)
552 spin_unlock(&mp
->m_agirotor_lock
);
558 * Select an allocation group to look for a free inode in, based on the parent
559 * inode and the mode. Return the allocation group buffer.
561 STATIC xfs_agnumber_t
562 xfs_ialloc_ag_select(
563 xfs_trans_t
*tp
, /* transaction pointer */
564 xfs_ino_t parent
, /* parent directory inode number */
565 umode_t mode
, /* bits set to indicate file type */
566 int okalloc
) /* ok to allocate more space */
568 xfs_agnumber_t agcount
; /* number of ag's in the filesystem */
569 xfs_agnumber_t agno
; /* current ag number */
570 int flags
; /* alloc buffer locking flags */
571 xfs_extlen_t ineed
; /* blocks needed for inode allocation */
572 xfs_extlen_t longest
= 0; /* longest extent available */
573 xfs_mount_t
*mp
; /* mount point structure */
574 int needspace
; /* file mode implies space allocated */
575 xfs_perag_t
*pag
; /* per allocation group data */
576 xfs_agnumber_t pagno
; /* parent (starting) ag number */
580 * Files of these types need at least one block if length > 0
581 * (and they won't fit in the inode, but that's hard to figure out).
583 needspace
= S_ISDIR(mode
) || S_ISREG(mode
) || S_ISLNK(mode
);
585 agcount
= mp
->m_maxagi
;
587 pagno
= xfs_ialloc_next_ag(mp
);
589 pagno
= XFS_INO_TO_AGNO(mp
, parent
);
590 if (pagno
>= agcount
)
594 ASSERT(pagno
< agcount
);
597 * Loop through allocation groups, looking for one with a little
598 * free space in it. Note we don't look for free inodes, exactly.
599 * Instead, we include whether there is a need to allocate inodes
600 * to mean that blocks must be allocated for them,
601 * if none are currently free.
604 flags
= XFS_ALLOC_FLAG_TRYLOCK
;
606 pag
= xfs_perag_get(mp
, agno
);
607 if (!pag
->pagi_inodeok
) {
608 xfs_ialloc_next_ag(mp
);
612 if (!pag
->pagi_init
) {
613 error
= xfs_ialloc_pagi_init(mp
, tp
, agno
);
618 if (pag
->pagi_freecount
) {
626 if (!pag
->pagf_init
) {
627 error
= xfs_alloc_pagf_init(mp
, tp
, agno
, flags
);
633 * Check that there is enough free space for the file plus a
634 * chunk of inodes if we need to allocate some. If this is the
635 * first pass across the AGs, take into account the potential
636 * space needed for alignment of inode chunks when checking the
637 * longest contiguous free space in the AG - this prevents us
638 * from getting ENOSPC because we have free space larger than
639 * m_ialloc_blks but alignment constraints prevent us from using
642 * If we can't find an AG with space for full alignment slack to
643 * be taken into account, we must be near ENOSPC in all AGs.
644 * Hence we don't include alignment for the second pass and so
645 * if we fail allocation due to alignment issues then it is most
646 * likely a real ENOSPC condition.
648 ineed
= mp
->m_ialloc_blks
;
649 if (flags
&& ineed
> 1)
650 ineed
+= xfs_ialloc_cluster_alignment(mp
);
651 longest
= pag
->pagf_longest
;
653 longest
= pag
->pagf_flcount
> 0;
655 if (pag
->pagf_freeblks
>= needspace
+ ineed
&&
663 * No point in iterating over the rest, if we're shutting
666 if (XFS_FORCED_SHUTDOWN(mp
))
680 * Try to retrieve the next record to the left/right from the current one.
684 struct xfs_btree_cur
*cur
,
685 xfs_inobt_rec_incore_t
*rec
,
693 error
= xfs_btree_decrement(cur
, 0, &i
);
695 error
= xfs_btree_increment(cur
, 0, &i
);
701 error
= xfs_inobt_get_rec(cur
, rec
, &i
);
704 XFS_WANT_CORRUPTED_RETURN(cur
->bc_mp
, i
== 1);
712 struct xfs_btree_cur
*cur
,
714 xfs_inobt_rec_incore_t
*rec
,
720 error
= xfs_inobt_lookup(cur
, agino
, XFS_LOOKUP_EQ
, &i
);
725 error
= xfs_inobt_get_rec(cur
, rec
, &i
);
728 XFS_WANT_CORRUPTED_RETURN(cur
->bc_mp
, i
== 1);
735 * Allocate an inode using the inobt-only algorithm.
738 xfs_dialloc_ag_inobt(
739 struct xfs_trans
*tp
,
740 struct xfs_buf
*agbp
,
744 struct xfs_mount
*mp
= tp
->t_mountp
;
745 struct xfs_agi
*agi
= XFS_BUF_TO_AGI(agbp
);
746 xfs_agnumber_t agno
= be32_to_cpu(agi
->agi_seqno
);
747 xfs_agnumber_t pagno
= XFS_INO_TO_AGNO(mp
, parent
);
748 xfs_agino_t pagino
= XFS_INO_TO_AGINO(mp
, parent
);
749 struct xfs_perag
*pag
;
750 struct xfs_btree_cur
*cur
, *tcur
;
751 struct xfs_inobt_rec_incore rec
, trec
;
757 pag
= xfs_perag_get(mp
, agno
);
759 ASSERT(pag
->pagi_init
);
760 ASSERT(pag
->pagi_inodeok
);
761 ASSERT(pag
->pagi_freecount
> 0);
764 cur
= xfs_inobt_init_cursor(mp
, tp
, agbp
, agno
, XFS_BTNUM_INO
);
766 * If pagino is 0 (this is the root inode allocation) use newino.
767 * This must work because we've just allocated some.
770 pagino
= be32_to_cpu(agi
->agi_newino
);
772 error
= xfs_check_agi_freecount(cur
, agi
);
777 * If in the same AG as the parent, try to get near the parent.
780 int doneleft
; /* done, to the left */
781 int doneright
; /* done, to the right */
782 int searchdistance
= 10;
784 error
= xfs_inobt_lookup(cur
, pagino
, XFS_LOOKUP_LE
, &i
);
787 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
789 error
= xfs_inobt_get_rec(cur
, &rec
, &j
);
792 XFS_WANT_CORRUPTED_GOTO(mp
, j
== 1, error0
);
794 if (rec
.ir_freecount
> 0) {
796 * Found a free inode in the same chunk
797 * as the parent, done.
804 * In the same AG as parent, but parent's chunk is full.
807 /* duplicate the cursor, search left & right simultaneously */
808 error
= xfs_btree_dup_cursor(cur
, &tcur
);
813 * Skip to last blocks looked up if same parent inode.
815 if (pagino
!= NULLAGINO
&&
816 pag
->pagl_pagino
== pagino
&&
817 pag
->pagl_leftrec
!= NULLAGINO
&&
818 pag
->pagl_rightrec
!= NULLAGINO
) {
819 error
= xfs_ialloc_get_rec(tcur
, pag
->pagl_leftrec
,
824 error
= xfs_ialloc_get_rec(cur
, pag
->pagl_rightrec
,
829 /* search left with tcur, back up 1 record */
830 error
= xfs_ialloc_next_rec(tcur
, &trec
, &doneleft
, 1);
834 /* search right with cur, go forward 1 record. */
835 error
= xfs_ialloc_next_rec(cur
, &rec
, &doneright
, 0);
841 * Loop until we find an inode chunk with a free inode.
843 while (!doneleft
|| !doneright
) {
844 int useleft
; /* using left inode chunk this time */
846 if (!--searchdistance
) {
848 * Not in range - save last search
849 * location and allocate a new inode
851 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
852 pag
->pagl_leftrec
= trec
.ir_startino
;
853 pag
->pagl_rightrec
= rec
.ir_startino
;
854 pag
->pagl_pagino
= pagino
;
858 /* figure out the closer block if both are valid. */
859 if (!doneleft
&& !doneright
) {
861 (trec
.ir_startino
+ XFS_INODES_PER_CHUNK
- 1) <
862 rec
.ir_startino
- pagino
;
867 /* free inodes to the left? */
868 if (useleft
&& trec
.ir_freecount
) {
870 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
873 pag
->pagl_leftrec
= trec
.ir_startino
;
874 pag
->pagl_rightrec
= rec
.ir_startino
;
875 pag
->pagl_pagino
= pagino
;
879 /* free inodes to the right? */
880 if (!useleft
&& rec
.ir_freecount
) {
881 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
883 pag
->pagl_leftrec
= trec
.ir_startino
;
884 pag
->pagl_rightrec
= rec
.ir_startino
;
885 pag
->pagl_pagino
= pagino
;
889 /* get next record to check */
891 error
= xfs_ialloc_next_rec(tcur
, &trec
,
894 error
= xfs_ialloc_next_rec(cur
, &rec
,
902 * We've reached the end of the btree. because
903 * we are only searching a small chunk of the
904 * btree each search, there is obviously free
905 * inodes closer to the parent inode than we
906 * are now. restart the search again.
908 pag
->pagl_pagino
= NULLAGINO
;
909 pag
->pagl_leftrec
= NULLAGINO
;
910 pag
->pagl_rightrec
= NULLAGINO
;
911 xfs_btree_del_cursor(tcur
, XFS_BTREE_NOERROR
);
912 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
917 * In a different AG from the parent.
918 * See if the most recently allocated block has any free.
921 if (agi
->agi_newino
!= cpu_to_be32(NULLAGINO
)) {
922 error
= xfs_inobt_lookup(cur
, be32_to_cpu(agi
->agi_newino
),
928 error
= xfs_inobt_get_rec(cur
, &rec
, &j
);
932 if (j
== 1 && rec
.ir_freecount
> 0) {
934 * The last chunk allocated in the group
935 * still has a free inode.
943 * None left in the last group, search the whole AG
945 error
= xfs_inobt_lookup(cur
, 0, XFS_LOOKUP_GE
, &i
);
948 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
951 error
= xfs_inobt_get_rec(cur
, &rec
, &i
);
954 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
955 if (rec
.ir_freecount
> 0)
957 error
= xfs_btree_increment(cur
, 0, &i
);
960 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
964 offset
= xfs_lowbit64(rec
.ir_free
);
966 ASSERT(offset
< XFS_INODES_PER_CHUNK
);
967 ASSERT((XFS_AGINO_TO_OFFSET(mp
, rec
.ir_startino
) %
968 XFS_INODES_PER_CHUNK
) == 0);
969 ino
= XFS_AGINO_TO_INO(mp
, agno
, rec
.ir_startino
+ offset
);
970 rec
.ir_free
&= ~XFS_INOBT_MASK(offset
);
972 error
= xfs_inobt_update(cur
, &rec
);
975 be32_add_cpu(&agi
->agi_freecount
, -1);
976 xfs_ialloc_log_agi(tp
, agbp
, XFS_AGI_FREECOUNT
);
977 pag
->pagi_freecount
--;
979 error
= xfs_check_agi_freecount(cur
, agi
);
983 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
984 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_IFREE
, -1);
989 xfs_btree_del_cursor(tcur
, XFS_BTREE_ERROR
);
991 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
997 * Use the free inode btree to allocate an inode based on distance from the
998 * parent. Note that the provided cursor may be deleted and replaced.
1001 xfs_dialloc_ag_finobt_near(
1003 struct xfs_btree_cur
**ocur
,
1004 struct xfs_inobt_rec_incore
*rec
)
1006 struct xfs_btree_cur
*lcur
= *ocur
; /* left search cursor */
1007 struct xfs_btree_cur
*rcur
; /* right search cursor */
1008 struct xfs_inobt_rec_incore rrec
;
1012 error
= xfs_inobt_lookup(lcur
, pagino
, XFS_LOOKUP_LE
, &i
);
1017 error
= xfs_inobt_get_rec(lcur
, rec
, &i
);
1020 XFS_WANT_CORRUPTED_RETURN(lcur
->bc_mp
, i
== 1);
1023 * See if we've landed in the parent inode record. The finobt
1024 * only tracks chunks with at least one free inode, so record
1025 * existence is enough.
1027 if (pagino
>= rec
->ir_startino
&&
1028 pagino
< (rec
->ir_startino
+ XFS_INODES_PER_CHUNK
))
1032 error
= xfs_btree_dup_cursor(lcur
, &rcur
);
1036 error
= xfs_inobt_lookup(rcur
, pagino
, XFS_LOOKUP_GE
, &j
);
1040 error
= xfs_inobt_get_rec(rcur
, &rrec
, &j
);
1043 XFS_WANT_CORRUPTED_GOTO(lcur
->bc_mp
, j
== 1, error_rcur
);
1046 XFS_WANT_CORRUPTED_GOTO(lcur
->bc_mp
, i
== 1 || j
== 1, error_rcur
);
1047 if (i
== 1 && j
== 1) {
1049 * Both the left and right records are valid. Choose the closer
1050 * inode chunk to the target.
1052 if ((pagino
- rec
->ir_startino
+ XFS_INODES_PER_CHUNK
- 1) >
1053 (rrec
.ir_startino
- pagino
)) {
1055 xfs_btree_del_cursor(lcur
, XFS_BTREE_NOERROR
);
1058 xfs_btree_del_cursor(rcur
, XFS_BTREE_NOERROR
);
1060 } else if (j
== 1) {
1061 /* only the right record is valid */
1063 xfs_btree_del_cursor(lcur
, XFS_BTREE_NOERROR
);
1065 } else if (i
== 1) {
1066 /* only the left record is valid */
1067 xfs_btree_del_cursor(rcur
, XFS_BTREE_NOERROR
);
1073 xfs_btree_del_cursor(rcur
, XFS_BTREE_ERROR
);
1078 * Use the free inode btree to find a free inode based on a newino hint. If
1079 * the hint is NULL, find the first free inode in the AG.
1082 xfs_dialloc_ag_finobt_newino(
1083 struct xfs_agi
*agi
,
1084 struct xfs_btree_cur
*cur
,
1085 struct xfs_inobt_rec_incore
*rec
)
1090 if (agi
->agi_newino
!= cpu_to_be32(NULLAGINO
)) {
1091 error
= xfs_inobt_lookup(cur
, be32_to_cpu(agi
->agi_newino
),
1096 error
= xfs_inobt_get_rec(cur
, rec
, &i
);
1099 XFS_WANT_CORRUPTED_RETURN(cur
->bc_mp
, i
== 1);
1105 * Find the first inode available in the AG.
1107 error
= xfs_inobt_lookup(cur
, 0, XFS_LOOKUP_GE
, &i
);
1110 XFS_WANT_CORRUPTED_RETURN(cur
->bc_mp
, i
== 1);
1112 error
= xfs_inobt_get_rec(cur
, rec
, &i
);
1115 XFS_WANT_CORRUPTED_RETURN(cur
->bc_mp
, i
== 1);
1121 * Update the inobt based on a modification made to the finobt. Also ensure that
1122 * the records from both trees are equivalent post-modification.
1125 xfs_dialloc_ag_update_inobt(
1126 struct xfs_btree_cur
*cur
, /* inobt cursor */
1127 struct xfs_inobt_rec_incore
*frec
, /* finobt record */
1128 int offset
) /* inode offset */
1130 struct xfs_inobt_rec_incore rec
;
1134 error
= xfs_inobt_lookup(cur
, frec
->ir_startino
, XFS_LOOKUP_EQ
, &i
);
1137 XFS_WANT_CORRUPTED_RETURN(cur
->bc_mp
, i
== 1);
1139 error
= xfs_inobt_get_rec(cur
, &rec
, &i
);
1142 XFS_WANT_CORRUPTED_RETURN(cur
->bc_mp
, i
== 1);
1143 ASSERT((XFS_AGINO_TO_OFFSET(cur
->bc_mp
, rec
.ir_startino
) %
1144 XFS_INODES_PER_CHUNK
) == 0);
1146 rec
.ir_free
&= ~XFS_INOBT_MASK(offset
);
1149 XFS_WANT_CORRUPTED_RETURN(cur
->bc_mp
, (rec
.ir_free
== frec
->ir_free
) &&
1150 (rec
.ir_freecount
== frec
->ir_freecount
));
1152 return xfs_inobt_update(cur
, &rec
);
1156 * Allocate an inode using the free inode btree, if available. Otherwise, fall
1157 * back to the inobt search algorithm.
1159 * The caller selected an AG for us, and made sure that free inodes are
1164 struct xfs_trans
*tp
,
1165 struct xfs_buf
*agbp
,
1169 struct xfs_mount
*mp
= tp
->t_mountp
;
1170 struct xfs_agi
*agi
= XFS_BUF_TO_AGI(agbp
);
1171 xfs_agnumber_t agno
= be32_to_cpu(agi
->agi_seqno
);
1172 xfs_agnumber_t pagno
= XFS_INO_TO_AGNO(mp
, parent
);
1173 xfs_agino_t pagino
= XFS_INO_TO_AGINO(mp
, parent
);
1174 struct xfs_perag
*pag
;
1175 struct xfs_btree_cur
*cur
; /* finobt cursor */
1176 struct xfs_btree_cur
*icur
; /* inobt cursor */
1177 struct xfs_inobt_rec_incore rec
;
1183 if (!xfs_sb_version_hasfinobt(&mp
->m_sb
))
1184 return xfs_dialloc_ag_inobt(tp
, agbp
, parent
, inop
);
1186 pag
= xfs_perag_get(mp
, agno
);
1189 * If pagino is 0 (this is the root inode allocation) use newino.
1190 * This must work because we've just allocated some.
1193 pagino
= be32_to_cpu(agi
->agi_newino
);
1195 cur
= xfs_inobt_init_cursor(mp
, tp
, agbp
, agno
, XFS_BTNUM_FINO
);
1197 error
= xfs_check_agi_freecount(cur
, agi
);
1202 * The search algorithm depends on whether we're in the same AG as the
1203 * parent. If so, find the closest available inode to the parent. If
1204 * not, consider the agi hint or find the first free inode in the AG.
1207 error
= xfs_dialloc_ag_finobt_near(pagino
, &cur
, &rec
);
1209 error
= xfs_dialloc_ag_finobt_newino(agi
, cur
, &rec
);
1213 offset
= xfs_lowbit64(rec
.ir_free
);
1214 ASSERT(offset
>= 0);
1215 ASSERT(offset
< XFS_INODES_PER_CHUNK
);
1216 ASSERT((XFS_AGINO_TO_OFFSET(mp
, rec
.ir_startino
) %
1217 XFS_INODES_PER_CHUNK
) == 0);
1218 ino
= XFS_AGINO_TO_INO(mp
, agno
, rec
.ir_startino
+ offset
);
1221 * Modify or remove the finobt record.
1223 rec
.ir_free
&= ~XFS_INOBT_MASK(offset
);
1225 if (rec
.ir_freecount
)
1226 error
= xfs_inobt_update(cur
, &rec
);
1228 error
= xfs_btree_delete(cur
, &i
);
1233 * The finobt has now been updated appropriately. We haven't updated the
1234 * agi and superblock yet, so we can create an inobt cursor and validate
1235 * the original freecount. If all is well, make the equivalent update to
1236 * the inobt using the finobt record and offset information.
1238 icur
= xfs_inobt_init_cursor(mp
, tp
, agbp
, agno
, XFS_BTNUM_INO
);
1240 error
= xfs_check_agi_freecount(icur
, agi
);
1244 error
= xfs_dialloc_ag_update_inobt(icur
, &rec
, offset
);
1249 * Both trees have now been updated. We must update the perag and
1250 * superblock before we can check the freecount for each btree.
1252 be32_add_cpu(&agi
->agi_freecount
, -1);
1253 xfs_ialloc_log_agi(tp
, agbp
, XFS_AGI_FREECOUNT
);
1254 pag
->pagi_freecount
--;
1256 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_IFREE
, -1);
1258 error
= xfs_check_agi_freecount(icur
, agi
);
1261 error
= xfs_check_agi_freecount(cur
, agi
);
1265 xfs_btree_del_cursor(icur
, XFS_BTREE_NOERROR
);
1266 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1272 xfs_btree_del_cursor(icur
, XFS_BTREE_ERROR
);
1274 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1280 * Allocate an inode on disk.
1282 * Mode is used to tell whether the new inode will need space, and whether it
1285 * This function is designed to be called twice if it has to do an allocation
1286 * to make more free inodes. On the first call, *IO_agbp should be set to NULL.
1287 * If an inode is available without having to performn an allocation, an inode
1288 * number is returned. In this case, *IO_agbp is set to NULL. If an allocation
1289 * needs to be done, xfs_dialloc returns the current AGI buffer in *IO_agbp.
1290 * The caller should then commit the current transaction, allocate a
1291 * new transaction, and call xfs_dialloc() again, passing in the previous value
1292 * of *IO_agbp. IO_agbp should be held across the transactions. Since the AGI
1293 * buffer is locked across the two calls, the second call is guaranteed to have
1294 * a free inode available.
1296 * Once we successfully pick an inode its number is returned and the on-disk
1297 * data structures are updated. The inode itself is not read in, since doing so
1298 * would break ordering constraints with xfs_reclaim.
1302 struct xfs_trans
*tp
,
1306 struct xfs_buf
**IO_agbp
,
1309 struct xfs_mount
*mp
= tp
->t_mountp
;
1310 struct xfs_buf
*agbp
;
1311 xfs_agnumber_t agno
;
1315 xfs_agnumber_t start_agno
;
1316 struct xfs_perag
*pag
;
1320 * If the caller passes in a pointer to the AGI buffer,
1321 * continue where we left off before. In this case, we
1322 * know that the allocation group has free inodes.
1329 * We do not have an agbp, so select an initial allocation
1330 * group for inode allocation.
1332 start_agno
= xfs_ialloc_ag_select(tp
, parent
, mode
, okalloc
);
1333 if (start_agno
== NULLAGNUMBER
) {
1339 * If we have already hit the ceiling of inode blocks then clear
1340 * okalloc so we scan all available agi structures for a free
1343 if (mp
->m_maxicount
&&
1344 percpu_counter_read(&mp
->m_icount
) + mp
->m_ialloc_inos
>
1351 * Loop until we find an allocation group that either has free inodes
1352 * or in which we can allocate some inodes. Iterate through the
1353 * allocation groups upward, wrapping at the end.
1357 pag
= xfs_perag_get(mp
, agno
);
1358 if (!pag
->pagi_inodeok
) {
1359 xfs_ialloc_next_ag(mp
);
1363 if (!pag
->pagi_init
) {
1364 error
= xfs_ialloc_pagi_init(mp
, tp
, agno
);
1370 * Do a first racy fast path check if this AG is usable.
1372 if (!pag
->pagi_freecount
&& !okalloc
)
1376 * Then read in the AGI buffer and recheck with the AGI buffer
1379 error
= xfs_ialloc_read_agi(mp
, tp
, agno
, &agbp
);
1383 if (pag
->pagi_freecount
) {
1389 goto nextag_relse_buffer
;
1392 error
= xfs_ialloc_ag_alloc(tp
, agbp
, &ialloced
);
1394 xfs_trans_brelse(tp
, agbp
);
1396 if (error
!= -ENOSPC
)
1406 * We successfully allocated some inodes, return
1407 * the current context to the caller so that it
1408 * can commit the current transaction and call
1409 * us again where we left off.
1411 ASSERT(pag
->pagi_freecount
> 0);
1419 nextag_relse_buffer
:
1420 xfs_trans_brelse(tp
, agbp
);
1423 if (++agno
== mp
->m_sb
.sb_agcount
)
1425 if (agno
== start_agno
) {
1427 return noroom
? -ENOSPC
: 0;
1433 return xfs_dialloc_ag(tp
, agbp
, parent
, inop
);
1441 struct xfs_mount
*mp
,
1442 struct xfs_trans
*tp
,
1443 struct xfs_buf
*agbp
,
1445 struct xfs_bmap_free
*flist
,
1447 xfs_ino_t
*first_ino
,
1448 struct xfs_inobt_rec_incore
*orec
)
1450 struct xfs_agi
*agi
= XFS_BUF_TO_AGI(agbp
);
1451 xfs_agnumber_t agno
= be32_to_cpu(agi
->agi_seqno
);
1452 struct xfs_perag
*pag
;
1453 struct xfs_btree_cur
*cur
;
1454 struct xfs_inobt_rec_incore rec
;
1460 ASSERT(agi
->agi_magicnum
== cpu_to_be32(XFS_AGI_MAGIC
));
1461 ASSERT(XFS_AGINO_TO_AGBNO(mp
, agino
) < be32_to_cpu(agi
->agi_length
));
1464 * Initialize the cursor.
1466 cur
= xfs_inobt_init_cursor(mp
, tp
, agbp
, agno
, XFS_BTNUM_INO
);
1468 error
= xfs_check_agi_freecount(cur
, agi
);
1473 * Look for the entry describing this inode.
1475 if ((error
= xfs_inobt_lookup(cur
, agino
, XFS_LOOKUP_LE
, &i
))) {
1476 xfs_warn(mp
, "%s: xfs_inobt_lookup() returned error %d.",
1480 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1481 error
= xfs_inobt_get_rec(cur
, &rec
, &i
);
1483 xfs_warn(mp
, "%s: xfs_inobt_get_rec() returned error %d.",
1487 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error0
);
1489 * Get the offset in the inode chunk.
1491 off
= agino
- rec
.ir_startino
;
1492 ASSERT(off
>= 0 && off
< XFS_INODES_PER_CHUNK
);
1493 ASSERT(!(rec
.ir_free
& XFS_INOBT_MASK(off
)));
1495 * Mark the inode free & increment the count.
1497 rec
.ir_free
|= XFS_INOBT_MASK(off
);
1501 * When an inode cluster is free, it becomes eligible for removal
1503 if (!(mp
->m_flags
& XFS_MOUNT_IKEEP
) &&
1504 (rec
.ir_freecount
== mp
->m_ialloc_inos
)) {
1507 *first_ino
= XFS_AGINO_TO_INO(mp
, agno
, rec
.ir_startino
);
1510 * Remove the inode cluster from the AGI B+Tree, adjust the
1511 * AGI and Superblock inode counts, and mark the disk space
1512 * to be freed when the transaction is committed.
1514 ilen
= mp
->m_ialloc_inos
;
1515 be32_add_cpu(&agi
->agi_count
, -ilen
);
1516 be32_add_cpu(&agi
->agi_freecount
, -(ilen
- 1));
1517 xfs_ialloc_log_agi(tp
, agbp
, XFS_AGI_COUNT
| XFS_AGI_FREECOUNT
);
1518 pag
= xfs_perag_get(mp
, agno
);
1519 pag
->pagi_freecount
-= ilen
- 1;
1521 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_ICOUNT
, -ilen
);
1522 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_IFREE
, -(ilen
- 1));
1524 if ((error
= xfs_btree_delete(cur
, &i
))) {
1525 xfs_warn(mp
, "%s: xfs_btree_delete returned error %d.",
1530 xfs_bmap_add_free(XFS_AGB_TO_FSB(mp
, agno
,
1531 XFS_AGINO_TO_AGBNO(mp
, rec
.ir_startino
)),
1532 mp
->m_ialloc_blks
, flist
, mp
);
1536 error
= xfs_inobt_update(cur
, &rec
);
1538 xfs_warn(mp
, "%s: xfs_inobt_update returned error %d.",
1544 * Change the inode free counts and log the ag/sb changes.
1546 be32_add_cpu(&agi
->agi_freecount
, 1);
1547 xfs_ialloc_log_agi(tp
, agbp
, XFS_AGI_FREECOUNT
);
1548 pag
= xfs_perag_get(mp
, agno
);
1549 pag
->pagi_freecount
++;
1551 xfs_trans_mod_sb(tp
, XFS_TRANS_SB_IFREE
, 1);
1554 error
= xfs_check_agi_freecount(cur
, agi
);
1559 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1563 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1568 * Free an inode in the free inode btree.
1572 struct xfs_mount
*mp
,
1573 struct xfs_trans
*tp
,
1574 struct xfs_buf
*agbp
,
1576 struct xfs_inobt_rec_incore
*ibtrec
) /* inobt record */
1578 struct xfs_agi
*agi
= XFS_BUF_TO_AGI(agbp
);
1579 xfs_agnumber_t agno
= be32_to_cpu(agi
->agi_seqno
);
1580 struct xfs_btree_cur
*cur
;
1581 struct xfs_inobt_rec_incore rec
;
1582 int offset
= agino
- ibtrec
->ir_startino
;
1586 cur
= xfs_inobt_init_cursor(mp
, tp
, agbp
, agno
, XFS_BTNUM_FINO
);
1588 error
= xfs_inobt_lookup(cur
, ibtrec
->ir_startino
, XFS_LOOKUP_EQ
, &i
);
1593 * If the record does not exist in the finobt, we must have just
1594 * freed an inode in a previously fully allocated chunk. If not,
1595 * something is out of sync.
1597 XFS_WANT_CORRUPTED_GOTO(mp
, ibtrec
->ir_freecount
== 1, error
);
1599 error
= xfs_inobt_insert_rec(cur
, ibtrec
->ir_freecount
,
1600 ibtrec
->ir_free
, &i
);
1609 * Read and update the existing record. We could just copy the ibtrec
1610 * across here, but that would defeat the purpose of having redundant
1611 * metadata. By making the modifications independently, we can catch
1612 * corruptions that we wouldn't see if we just copied from one record
1615 error
= xfs_inobt_get_rec(cur
, &rec
, &i
);
1618 XFS_WANT_CORRUPTED_GOTO(mp
, i
== 1, error
);
1620 rec
.ir_free
|= XFS_INOBT_MASK(offset
);
1623 XFS_WANT_CORRUPTED_GOTO(mp
, (rec
.ir_free
== ibtrec
->ir_free
) &&
1624 (rec
.ir_freecount
== ibtrec
->ir_freecount
),
1628 * The content of inobt records should always match between the inobt
1629 * and finobt. The lifecycle of records in the finobt is different from
1630 * the inobt in that the finobt only tracks records with at least one
1631 * free inode. Hence, if all of the inodes are free and we aren't
1632 * keeping inode chunks permanently on disk, remove the record.
1633 * Otherwise, update the record with the new information.
1635 if (rec
.ir_freecount
== mp
->m_ialloc_inos
&&
1636 !(mp
->m_flags
& XFS_MOUNT_IKEEP
)) {
1637 error
= xfs_btree_delete(cur
, &i
);
1642 error
= xfs_inobt_update(cur
, &rec
);
1648 error
= xfs_check_agi_freecount(cur
, agi
);
1652 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1656 xfs_btree_del_cursor(cur
, XFS_BTREE_ERROR
);
1661 * Free disk inode. Carefully avoids touching the incore inode, all
1662 * manipulations incore are the caller's responsibility.
1663 * The on-disk inode is not changed by this operation, only the
1664 * btree (free inode mask) is changed.
1668 struct xfs_trans
*tp
, /* transaction pointer */
1669 xfs_ino_t inode
, /* inode to be freed */
1670 struct xfs_bmap_free
*flist
, /* extents to free */
1671 int *deleted
,/* set if inode cluster was deleted */
1672 xfs_ino_t
*first_ino
)/* first inode in deleted cluster */
1675 xfs_agblock_t agbno
; /* block number containing inode */
1676 struct xfs_buf
*agbp
; /* buffer for allocation group header */
1677 xfs_agino_t agino
; /* allocation group inode number */
1678 xfs_agnumber_t agno
; /* allocation group number */
1679 int error
; /* error return value */
1680 struct xfs_mount
*mp
; /* mount structure for filesystem */
1681 struct xfs_inobt_rec_incore rec
;/* btree record */
1686 * Break up inode number into its components.
1688 agno
= XFS_INO_TO_AGNO(mp
, inode
);
1689 if (agno
>= mp
->m_sb
.sb_agcount
) {
1690 xfs_warn(mp
, "%s: agno >= mp->m_sb.sb_agcount (%d >= %d).",
1691 __func__
, agno
, mp
->m_sb
.sb_agcount
);
1695 agino
= XFS_INO_TO_AGINO(mp
, inode
);
1696 if (inode
!= XFS_AGINO_TO_INO(mp
, agno
, agino
)) {
1697 xfs_warn(mp
, "%s: inode != XFS_AGINO_TO_INO() (%llu != %llu).",
1698 __func__
, (unsigned long long)inode
,
1699 (unsigned long long)XFS_AGINO_TO_INO(mp
, agno
, agino
));
1703 agbno
= XFS_AGINO_TO_AGBNO(mp
, agino
);
1704 if (agbno
>= mp
->m_sb
.sb_agblocks
) {
1705 xfs_warn(mp
, "%s: agbno >= mp->m_sb.sb_agblocks (%d >= %d).",
1706 __func__
, agbno
, mp
->m_sb
.sb_agblocks
);
1711 * Get the allocation group header.
1713 error
= xfs_ialloc_read_agi(mp
, tp
, agno
, &agbp
);
1715 xfs_warn(mp
, "%s: xfs_ialloc_read_agi() returned error %d.",
1721 * Fix up the inode allocation btree.
1723 error
= xfs_difree_inobt(mp
, tp
, agbp
, agino
, flist
, deleted
, first_ino
,
1729 * Fix up the free inode btree.
1731 if (xfs_sb_version_hasfinobt(&mp
->m_sb
)) {
1732 error
= xfs_difree_finobt(mp
, tp
, agbp
, agino
, &rec
);
1745 struct xfs_mount
*mp
,
1746 struct xfs_trans
*tp
,
1747 xfs_agnumber_t agno
,
1749 xfs_agblock_t agbno
,
1750 xfs_agblock_t
*chunk_agbno
,
1751 xfs_agblock_t
*offset_agbno
,
1754 struct xfs_inobt_rec_incore rec
;
1755 struct xfs_btree_cur
*cur
;
1756 struct xfs_buf
*agbp
;
1760 error
= xfs_ialloc_read_agi(mp
, tp
, agno
, &agbp
);
1763 "%s: xfs_ialloc_read_agi() returned error %d, agno %d",
1764 __func__
, error
, agno
);
1769 * Lookup the inode record for the given agino. If the record cannot be
1770 * found, then it's an invalid inode number and we should abort. Once
1771 * we have a record, we need to ensure it contains the inode number
1772 * we are looking up.
1774 cur
= xfs_inobt_init_cursor(mp
, tp
, agbp
, agno
, XFS_BTNUM_INO
);
1775 error
= xfs_inobt_lookup(cur
, agino
, XFS_LOOKUP_LE
, &i
);
1778 error
= xfs_inobt_get_rec(cur
, &rec
, &i
);
1779 if (!error
&& i
== 0)
1783 xfs_trans_brelse(tp
, agbp
);
1784 xfs_btree_del_cursor(cur
, XFS_BTREE_NOERROR
);
1788 /* check that the returned record contains the required inode */
1789 if (rec
.ir_startino
> agino
||
1790 rec
.ir_startino
+ mp
->m_ialloc_inos
<= agino
)
1793 /* for untrusted inodes check it is allocated first */
1794 if ((flags
& XFS_IGET_UNTRUSTED
) &&
1795 (rec
.ir_free
& XFS_INOBT_MASK(agino
- rec
.ir_startino
)))
1798 *chunk_agbno
= XFS_AGINO_TO_AGBNO(mp
, rec
.ir_startino
);
1799 *offset_agbno
= agbno
- *chunk_agbno
;
1804 * Return the location of the inode in imap, for mapping it into a buffer.
1808 xfs_mount_t
*mp
, /* file system mount structure */
1809 xfs_trans_t
*tp
, /* transaction pointer */
1810 xfs_ino_t ino
, /* inode to locate */
1811 struct xfs_imap
*imap
, /* location map structure */
1812 uint flags
) /* flags for inode btree lookup */
1814 xfs_agblock_t agbno
; /* block number of inode in the alloc group */
1815 xfs_agino_t agino
; /* inode number within alloc group */
1816 xfs_agnumber_t agno
; /* allocation group number */
1817 int blks_per_cluster
; /* num blocks per inode cluster */
1818 xfs_agblock_t chunk_agbno
; /* first block in inode chunk */
1819 xfs_agblock_t cluster_agbno
; /* first block in inode cluster */
1820 int error
; /* error code */
1821 int offset
; /* index of inode in its buffer */
1822 xfs_agblock_t offset_agbno
; /* blks from chunk start to inode */
1824 ASSERT(ino
!= NULLFSINO
);
1827 * Split up the inode number into its parts.
1829 agno
= XFS_INO_TO_AGNO(mp
, ino
);
1830 agino
= XFS_INO_TO_AGINO(mp
, ino
);
1831 agbno
= XFS_AGINO_TO_AGBNO(mp
, agino
);
1832 if (agno
>= mp
->m_sb
.sb_agcount
|| agbno
>= mp
->m_sb
.sb_agblocks
||
1833 ino
!= XFS_AGINO_TO_INO(mp
, agno
, agino
)) {
1836 * Don't output diagnostic information for untrusted inodes
1837 * as they can be invalid without implying corruption.
1839 if (flags
& XFS_IGET_UNTRUSTED
)
1841 if (agno
>= mp
->m_sb
.sb_agcount
) {
1843 "%s: agno (%d) >= mp->m_sb.sb_agcount (%d)",
1844 __func__
, agno
, mp
->m_sb
.sb_agcount
);
1846 if (agbno
>= mp
->m_sb
.sb_agblocks
) {
1848 "%s: agbno (0x%llx) >= mp->m_sb.sb_agblocks (0x%lx)",
1849 __func__
, (unsigned long long)agbno
,
1850 (unsigned long)mp
->m_sb
.sb_agblocks
);
1852 if (ino
!= XFS_AGINO_TO_INO(mp
, agno
, agino
)) {
1854 "%s: ino (0x%llx) != XFS_AGINO_TO_INO() (0x%llx)",
1856 XFS_AGINO_TO_INO(mp
, agno
, agino
));
1863 blks_per_cluster
= xfs_icluster_size_fsb(mp
);
1866 * For bulkstat and handle lookups, we have an untrusted inode number
1867 * that we have to verify is valid. We cannot do this just by reading
1868 * the inode buffer as it may have been unlinked and removed leaving
1869 * inodes in stale state on disk. Hence we have to do a btree lookup
1870 * in all cases where an untrusted inode number is passed.
1872 if (flags
& XFS_IGET_UNTRUSTED
) {
1873 error
= xfs_imap_lookup(mp
, tp
, agno
, agino
, agbno
,
1874 &chunk_agbno
, &offset_agbno
, flags
);
1881 * If the inode cluster size is the same as the blocksize or
1882 * smaller we get to the buffer by simple arithmetics.
1884 if (blks_per_cluster
== 1) {
1885 offset
= XFS_INO_TO_OFFSET(mp
, ino
);
1886 ASSERT(offset
< mp
->m_sb
.sb_inopblock
);
1888 imap
->im_blkno
= XFS_AGB_TO_DADDR(mp
, agno
, agbno
);
1889 imap
->im_len
= XFS_FSB_TO_BB(mp
, 1);
1890 imap
->im_boffset
= (ushort
)(offset
<< mp
->m_sb
.sb_inodelog
);
1895 * If the inode chunks are aligned then use simple maths to
1896 * find the location. Otherwise we have to do a btree
1897 * lookup to find the location.
1899 if (mp
->m_inoalign_mask
) {
1900 offset_agbno
= agbno
& mp
->m_inoalign_mask
;
1901 chunk_agbno
= agbno
- offset_agbno
;
1903 error
= xfs_imap_lookup(mp
, tp
, agno
, agino
, agbno
,
1904 &chunk_agbno
, &offset_agbno
, flags
);
1910 ASSERT(agbno
>= chunk_agbno
);
1911 cluster_agbno
= chunk_agbno
+
1912 ((offset_agbno
/ blks_per_cluster
) * blks_per_cluster
);
1913 offset
= ((agbno
- cluster_agbno
) * mp
->m_sb
.sb_inopblock
) +
1914 XFS_INO_TO_OFFSET(mp
, ino
);
1916 imap
->im_blkno
= XFS_AGB_TO_DADDR(mp
, agno
, cluster_agbno
);
1917 imap
->im_len
= XFS_FSB_TO_BB(mp
, blks_per_cluster
);
1918 imap
->im_boffset
= (ushort
)(offset
<< mp
->m_sb
.sb_inodelog
);
1921 * If the inode number maps to a block outside the bounds
1922 * of the file system then return NULL rather than calling
1923 * read_buf and panicing when we get an error from the
1926 if ((imap
->im_blkno
+ imap
->im_len
) >
1927 XFS_FSB_TO_BB(mp
, mp
->m_sb
.sb_dblocks
)) {
1929 "%s: (im_blkno (0x%llx) + im_len (0x%llx)) > sb_dblocks (0x%llx)",
1930 __func__
, (unsigned long long) imap
->im_blkno
,
1931 (unsigned long long) imap
->im_len
,
1932 XFS_FSB_TO_BB(mp
, mp
->m_sb
.sb_dblocks
));
1939 * Compute and fill in value of m_in_maxlevels.
1942 xfs_ialloc_compute_maxlevels(
1943 xfs_mount_t
*mp
) /* file system mount structure */
1951 maxleafents
= (1LL << XFS_INO_AGINO_BITS(mp
)) >>
1952 XFS_INODES_PER_CHUNK_LOG
;
1953 minleafrecs
= mp
->m_alloc_mnr
[0];
1954 minnoderecs
= mp
->m_alloc_mnr
[1];
1955 maxblocks
= (maxleafents
+ minleafrecs
- 1) / minleafrecs
;
1956 for (level
= 1; maxblocks
> 1; level
++)
1957 maxblocks
= (maxblocks
+ minnoderecs
- 1) / minnoderecs
;
1958 mp
->m_in_maxlevels
= level
;
1962 * Log specified fields for the ag hdr (inode section). The growth of the agi
1963 * structure over time requires that we interpret the buffer as two logical
1964 * regions delineated by the end of the unlinked list. This is due to the size
1965 * of the hash table and its location in the middle of the agi.
1967 * For example, a request to log a field before agi_unlinked and a field after
1968 * agi_unlinked could cause us to log the entire hash table and use an excessive
1969 * amount of log space. To avoid this behavior, log the region up through
1970 * agi_unlinked in one call and the region after agi_unlinked through the end of
1971 * the structure in another.
1975 xfs_trans_t
*tp
, /* transaction pointer */
1976 xfs_buf_t
*bp
, /* allocation group header buffer */
1977 int fields
) /* bitmask of fields to log */
1979 int first
; /* first byte number */
1980 int last
; /* last byte number */
1981 static const short offsets
[] = { /* field starting offsets */
1982 /* keep in sync with bit definitions */
1983 offsetof(xfs_agi_t
, agi_magicnum
),
1984 offsetof(xfs_agi_t
, agi_versionnum
),
1985 offsetof(xfs_agi_t
, agi_seqno
),
1986 offsetof(xfs_agi_t
, agi_length
),
1987 offsetof(xfs_agi_t
, agi_count
),
1988 offsetof(xfs_agi_t
, agi_root
),
1989 offsetof(xfs_agi_t
, agi_level
),
1990 offsetof(xfs_agi_t
, agi_freecount
),
1991 offsetof(xfs_agi_t
, agi_newino
),
1992 offsetof(xfs_agi_t
, agi_dirino
),
1993 offsetof(xfs_agi_t
, agi_unlinked
),
1994 offsetof(xfs_agi_t
, agi_free_root
),
1995 offsetof(xfs_agi_t
, agi_free_level
),
1999 xfs_agi_t
*agi
; /* allocation group header */
2001 agi
= XFS_BUF_TO_AGI(bp
);
2002 ASSERT(agi
->agi_magicnum
== cpu_to_be32(XFS_AGI_MAGIC
));
2005 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_AGI_BUF
);
2008 * Compute byte offsets for the first and last fields in the first
2009 * region and log the agi buffer. This only logs up through
2012 if (fields
& XFS_AGI_ALL_BITS_R1
) {
2013 xfs_btree_offsets(fields
, offsets
, XFS_AGI_NUM_BITS_R1
,
2015 xfs_trans_log_buf(tp
, bp
, first
, last
);
2019 * Mask off the bits in the first region and calculate the first and
2020 * last field offsets for any bits in the second region.
2022 fields
&= ~XFS_AGI_ALL_BITS_R1
;
2024 xfs_btree_offsets(fields
, offsets
, XFS_AGI_NUM_BITS_R2
,
2026 xfs_trans_log_buf(tp
, bp
, first
, last
);
2032 xfs_check_agi_unlinked(
2033 struct xfs_agi
*agi
)
2037 for (i
= 0; i
< XFS_AGI_UNLINKED_BUCKETS
; i
++)
2038 ASSERT(agi
->agi_unlinked
[i
]);
2041 #define xfs_check_agi_unlinked(agi)
2048 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
2049 struct xfs_agi
*agi
= XFS_BUF_TO_AGI(bp
);
2051 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
2052 !uuid_equal(&agi
->agi_uuid
, &mp
->m_sb
.sb_uuid
))
2055 * Validate the magic number of the agi block.
2057 if (agi
->agi_magicnum
!= cpu_to_be32(XFS_AGI_MAGIC
))
2059 if (!XFS_AGI_GOOD_VERSION(be32_to_cpu(agi
->agi_versionnum
)))
2062 if (be32_to_cpu(agi
->agi_level
) > XFS_BTREE_MAXLEVELS
)
2065 * during growfs operations, the perag is not fully initialised,
2066 * so we can't use it for any useful checking. growfs ensures we can't
2067 * use it by using uncached buffers that don't have the perag attached
2068 * so we can detect and avoid this problem.
2070 if (bp
->b_pag
&& be32_to_cpu(agi
->agi_seqno
) != bp
->b_pag
->pag_agno
)
2073 xfs_check_agi_unlinked(agi
);
2078 xfs_agi_read_verify(
2081 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
2083 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
2084 !xfs_buf_verify_cksum(bp
, XFS_AGI_CRC_OFF
))
2085 xfs_buf_ioerror(bp
, -EFSBADCRC
);
2086 else if (XFS_TEST_ERROR(!xfs_agi_verify(bp
), mp
,
2087 XFS_ERRTAG_IALLOC_READ_AGI
,
2088 XFS_RANDOM_IALLOC_READ_AGI
))
2089 xfs_buf_ioerror(bp
, -EFSCORRUPTED
);
2092 xfs_verifier_error(bp
);
2096 xfs_agi_write_verify(
2099 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
2100 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
2102 if (!xfs_agi_verify(bp
)) {
2103 xfs_buf_ioerror(bp
, -EFSCORRUPTED
);
2104 xfs_verifier_error(bp
);
2108 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
2112 XFS_BUF_TO_AGI(bp
)->agi_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
2113 xfs_buf_update_cksum(bp
, XFS_AGI_CRC_OFF
);
2116 const struct xfs_buf_ops xfs_agi_buf_ops
= {
2117 .verify_read
= xfs_agi_read_verify
,
2118 .verify_write
= xfs_agi_write_verify
,
2122 * Read in the allocation group header (inode allocation section)
2126 struct xfs_mount
*mp
, /* file system mount structure */
2127 struct xfs_trans
*tp
, /* transaction pointer */
2128 xfs_agnumber_t agno
, /* allocation group number */
2129 struct xfs_buf
**bpp
) /* allocation group hdr buf */
2133 trace_xfs_read_agi(mp
, agno
);
2135 ASSERT(agno
!= NULLAGNUMBER
);
2136 error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
,
2137 XFS_AG_DADDR(mp
, agno
, XFS_AGI_DADDR(mp
)),
2138 XFS_FSS_TO_BB(mp
, 1), 0, bpp
, &xfs_agi_buf_ops
);
2142 xfs_buf_set_ref(*bpp
, XFS_AGI_REF
);
2147 xfs_ialloc_read_agi(
2148 struct xfs_mount
*mp
, /* file system mount structure */
2149 struct xfs_trans
*tp
, /* transaction pointer */
2150 xfs_agnumber_t agno
, /* allocation group number */
2151 struct xfs_buf
**bpp
) /* allocation group hdr buf */
2153 struct xfs_agi
*agi
; /* allocation group header */
2154 struct xfs_perag
*pag
; /* per allocation group data */
2157 trace_xfs_ialloc_read_agi(mp
, agno
);
2159 error
= xfs_read_agi(mp
, tp
, agno
, bpp
);
2163 agi
= XFS_BUF_TO_AGI(*bpp
);
2164 pag
= xfs_perag_get(mp
, agno
);
2165 if (!pag
->pagi_init
) {
2166 pag
->pagi_freecount
= be32_to_cpu(agi
->agi_freecount
);
2167 pag
->pagi_count
= be32_to_cpu(agi
->agi_count
);
2172 * It's possible for these to be out of sync if
2173 * we are in the middle of a forced shutdown.
2175 ASSERT(pag
->pagi_freecount
== be32_to_cpu(agi
->agi_freecount
) ||
2176 XFS_FORCED_SHUTDOWN(mp
));
2182 * Read in the agi to initialise the per-ag data in the mount structure
2185 xfs_ialloc_pagi_init(
2186 xfs_mount_t
*mp
, /* file system mount structure */
2187 xfs_trans_t
*tp
, /* transaction pointer */
2188 xfs_agnumber_t agno
) /* allocation group number */
2190 xfs_buf_t
*bp
= NULL
;
2193 error
= xfs_ialloc_read_agi(mp
, tp
, agno
, &bp
);
2197 xfs_trans_brelse(tp
, bp
);