1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2019 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_mount.h"
13 #include "xfs_inode.h"
14 #include "xfs_btree.h"
15 #include "xfs_ialloc.h"
16 #include "xfs_ialloc_btree.h"
17 #include "xfs_iwalk.h"
18 #include "xfs_error.h"
19 #include "xfs_trace.h"
20 #include "xfs_icache.h"
21 #include "xfs_health.h"
22 #include "xfs_trans.h"
23 #include "xfs_pwork.h"
28 * Walking Inodes in the Filesystem
29 * ================================
31 * This iterator function walks a subset of filesystem inodes in increasing
32 * order from @startino until there are no more inodes. For each allocated
33 * inode it finds, it calls a walk function with the relevant inode number and
34 * a pointer to caller-provided data. The walk function can return the usual
35 * negative error code to stop the iteration; 0 to continue the iteration; or
36 * -ECANCELED to stop the iteration. This return value is returned to the
39 * Internally, we allow the walk function to do anything, which means that we
40 * cannot maintain the inobt cursor or our lock on the AGI buffer. We
41 * therefore cache the inobt records in kernel memory and only call the walk
42 * function when our memory buffer is full. @nr_recs is the number of records
43 * that we've cached, and @sz_recs is the size of our cache.
45 * It is the responsibility of the walk function to ensure it accesses
46 * allocated inodes, as the inobt records may be stale by the time they are
51 /* parallel work control data; will be null if single threaded */
52 struct xfs_pwork pwork
;
56 struct xfs_perag
*pag
;
58 /* Where do we start the traversal? */
61 /* What was the last inode number we saw when iterating the inobt? */
64 /* Array of inobt records we cache. */
65 struct xfs_inobt_rec_incore
*recs
;
67 /* Number of entries allocated for the @recs array. */
70 /* Number of entries in the @recs array that are in use. */
73 /* Inode walk function and data pointer. */
74 xfs_iwalk_fn iwalk_fn
;
75 xfs_inobt_walk_fn inobt_walk_fn
;
79 * Make it look like the inodes up to startino are free so that
80 * bulkstat can start its inode iteration at the correct place without
81 * needing to special case everywhere.
83 unsigned int trim_start
:1;
85 /* Skip empty inobt records? */
86 unsigned int skip_empty
:1;
88 /* Drop the (hopefully empty) transaction when calling iwalk_fn. */
89 unsigned int drop_trans
:1;
93 * Loop over all clusters in a chunk for a given incore inode allocation btree
94 * record. Do a readahead if there are any allocated inodes in that cluster.
99 struct xfs_perag
*pag
,
100 struct xfs_inobt_rec_incore
*irec
)
102 struct xfs_ino_geometry
*igeo
= M_IGEO(mp
);
104 struct blk_plug plug
;
105 int i
; /* inode chunk index */
107 agbno
= XFS_AGINO_TO_AGBNO(mp
, irec
->ir_startino
);
109 blk_start_plug(&plug
);
110 for (i
= 0; i
< XFS_INODES_PER_CHUNK
; i
+= igeo
->inodes_per_cluster
) {
113 imask
= xfs_inobt_maskn(i
, igeo
->inodes_per_cluster
);
114 if (imask
& ~irec
->ir_free
) {
115 xfs_buf_readahead(mp
->m_ddev_targp
,
116 xfs_agbno_to_daddr(pag
, agbno
),
117 igeo
->blocks_per_cluster
* mp
->m_bsize
,
120 agbno
+= igeo
->blocks_per_cluster
;
122 blk_finish_plug(&plug
);
126 * Set the bits in @irec's free mask that correspond to the inodes before
127 * @agino so that we skip them. This is how we restart an inode walk that was
128 * interrupted in the middle of an inode record.
131 xfs_iwalk_adjust_start(
132 xfs_agino_t agino
, /* starting inode of chunk */
133 struct xfs_inobt_rec_incore
*irec
) /* btree record */
135 int idx
; /* index into inode chunk */
137 idx
= agino
- irec
->ir_startino
;
139 irec
->ir_free
|= xfs_inobt_maskn(0, idx
);
140 irec
->ir_freecount
= hweight64(irec
->ir_free
);
143 /* Allocate memory for a walk. */
146 struct xfs_iwalk_ag
*iwag
)
150 ASSERT(iwag
->recs
== NULL
);
153 /* Allocate a prefetch buffer for inobt records. */
154 size
= iwag
->sz_recs
* sizeof(struct xfs_inobt_rec_incore
);
155 iwag
->recs
= kmalloc(size
, GFP_KERNEL
| __GFP_RETRY_MAYFAIL
);
156 if (iwag
->recs
== NULL
)
162 /* Free memory we allocated for a walk. */
165 struct xfs_iwalk_ag
*iwag
)
171 /* For each inuse inode in each cached inobt record, call our function. */
174 struct xfs_iwalk_ag
*iwag
)
176 struct xfs_mount
*mp
= iwag
->mp
;
177 struct xfs_trans
*tp
= iwag
->tp
;
178 struct xfs_perag
*pag
= iwag
->pag
;
182 for (i
= 0; i
< iwag
->nr_recs
; i
++) {
183 struct xfs_inobt_rec_incore
*irec
= &iwag
->recs
[i
];
185 trace_xfs_iwalk_ag_rec(pag
, irec
);
187 if (xfs_pwork_want_abort(&iwag
->pwork
))
190 if (iwag
->inobt_walk_fn
) {
191 error
= iwag
->inobt_walk_fn(mp
, tp
, pag_agno(pag
), irec
,
200 for (j
= 0; j
< XFS_INODES_PER_CHUNK
; j
++) {
201 if (xfs_pwork_want_abort(&iwag
->pwork
))
204 /* Skip if this inode is free */
205 if (XFS_INOBT_MASK(j
) & irec
->ir_free
)
208 /* Otherwise call our function. */
209 error
= iwag
->iwalk_fn(mp
, tp
,
210 xfs_agino_to_ino(pag
,
211 irec
->ir_startino
+ j
),
221 /* Delete cursor and let go of AGI. */
224 struct xfs_trans
*tp
,
225 struct xfs_btree_cur
**curpp
,
226 struct xfs_buf
**agi_bpp
,
230 xfs_btree_del_cursor(*curpp
, error
);
234 xfs_trans_brelse(tp
, *agi_bpp
);
240 * Set ourselves up for walking inobt records starting from a given point in
243 * If caller passed in a nonzero start inode number, load the record from the
244 * inobt and make the record look like all the inodes before agino are free so
245 * that we skip them, and then move the cursor to the next inobt record. This
246 * is how we support starting an iwalk in the middle of an inode chunk.
248 * If the caller passed in a start number of zero, move the cursor to the first
251 * The caller is responsible for cleaning up the cursor and buffer pointer
252 * regardless of the error status.
256 struct xfs_iwalk_ag
*iwag
,
258 struct xfs_btree_cur
**curpp
,
259 struct xfs_buf
**agi_bpp
,
262 struct xfs_mount
*mp
= iwag
->mp
;
263 struct xfs_trans
*tp
= iwag
->tp
;
264 struct xfs_perag
*pag
= iwag
->pag
;
265 struct xfs_inobt_rec_incore
*irec
;
268 /* Set up a fresh cursor and empty the inobt cache. */
270 error
= xfs_ialloc_read_agi(pag
, tp
, 0, agi_bpp
);
273 *curpp
= xfs_inobt_init_cursor(pag
, tp
, *agi_bpp
);
275 /* Starting at the beginning of the AG? That's easy! */
277 return xfs_inobt_lookup(*curpp
, 0, XFS_LOOKUP_GE
, has_more
);
280 * Otherwise, we have to grab the inobt record where we left off, stuff
281 * the record into our cache, and then see if there are more records.
282 * We require a lookup cache of at least two elements so that the
283 * caller doesn't have to deal with tearing down the cursor to walk the
286 error
= xfs_inobt_lookup(*curpp
, agino
, XFS_LOOKUP_LE
, has_more
);
291 * If the LE lookup at @agino yields no records, jump ahead to the
292 * inobt cursor increment to see if there are more records to process.
297 /* Get the record, should always work */
298 irec
= &iwag
->recs
[iwag
->nr_recs
];
299 error
= xfs_inobt_get_rec(*curpp
, irec
, has_more
);
302 if (XFS_IS_CORRUPT(mp
, *has_more
!= 1)) {
303 xfs_btree_mark_sick(*curpp
);
304 return -EFSCORRUPTED
;
307 iwag
->lastino
= xfs_agino_to_ino(pag
,
308 irec
->ir_startino
+ XFS_INODES_PER_CHUNK
- 1);
311 * If the LE lookup yielded an inobt record before the cursor position,
312 * skip it and see if there's another one after it.
314 if (irec
->ir_startino
+ XFS_INODES_PER_CHUNK
<= agino
)
318 * If agino fell in the middle of the inode record, make it look like
319 * the inodes up to agino are free so that we don't return them again.
321 if (iwag
->trim_start
)
322 xfs_iwalk_adjust_start(agino
, irec
);
325 * The prefetch calculation is supposed to give us a large enough inobt
326 * record cache that grab_ichunk can stage a partial first record and
327 * the loop body can cache a record without having to check for cache
328 * space until after it reads an inobt record.
331 ASSERT(iwag
->nr_recs
< iwag
->sz_recs
);
334 return xfs_btree_increment(*curpp
, 0, has_more
);
338 * The inobt record cache is full, so preserve the inobt cursor state and
339 * run callbacks on the cached inobt records. When we're done, restore the
340 * cursor state to wherever the cursor would have been had the cache not been
341 * full (and therefore we could've just incremented the cursor) if *@has_more
342 * is true. On exit, *@has_more will indicate whether or not the caller should
343 * try for more inode records.
346 xfs_iwalk_run_callbacks(
347 struct xfs_iwalk_ag
*iwag
,
348 struct xfs_btree_cur
**curpp
,
349 struct xfs_buf
**agi_bpp
,
352 struct xfs_mount
*mp
= iwag
->mp
;
353 xfs_agino_t next_agino
;
356 next_agino
= XFS_INO_TO_AGINO(mp
, iwag
->lastino
) + 1;
358 ASSERT(iwag
->nr_recs
> 0);
360 /* Delete cursor but remember the last record we cached... */
361 xfs_iwalk_del_inobt(iwag
->tp
, curpp
, agi_bpp
, 0);
362 ASSERT(next_agino
>= iwag
->recs
[iwag
->nr_recs
- 1].ir_startino
+
363 XFS_INODES_PER_CHUNK
);
365 if (iwag
->drop_trans
) {
366 xfs_trans_cancel(iwag
->tp
);
370 error
= xfs_iwalk_ag_recs(iwag
);
374 /* ...empty the cache... */
380 if (iwag
->drop_trans
) {
381 error
= xfs_trans_alloc_empty(mp
, &iwag
->tp
);
386 /* ...and recreate the cursor just past where we left off. */
387 error
= xfs_ialloc_read_agi(iwag
->pag
, iwag
->tp
, 0, agi_bpp
);
390 *curpp
= xfs_inobt_init_cursor(iwag
->pag
, iwag
->tp
, *agi_bpp
);
391 return xfs_inobt_lookup(*curpp
, next_agino
, XFS_LOOKUP_GE
, has_more
);
394 /* Walk all inodes in a single AG, from @iwag->startino to the end of the AG. */
397 struct xfs_iwalk_ag
*iwag
)
399 struct xfs_mount
*mp
= iwag
->mp
;
400 struct xfs_perag
*pag
= iwag
->pag
;
401 struct xfs_buf
*agi_bp
= NULL
;
402 struct xfs_btree_cur
*cur
= NULL
;
407 /* Set up our cursor at the right place in the inode btree. */
408 ASSERT(pag_agno(pag
) == XFS_INO_TO_AGNO(mp
, iwag
->startino
));
409 agino
= XFS_INO_TO_AGINO(mp
, iwag
->startino
);
410 error
= xfs_iwalk_ag_start(iwag
, agino
, &cur
, &agi_bp
, &has_more
);
412 while (!error
&& has_more
) {
413 struct xfs_inobt_rec_incore
*irec
;
417 if (xfs_pwork_want_abort(&iwag
->pwork
))
420 /* Fetch the inobt record. */
421 irec
= &iwag
->recs
[iwag
->nr_recs
];
422 error
= xfs_inobt_get_rec(cur
, irec
, &has_more
);
423 if (error
|| !has_more
)
426 /* Make sure that we always move forward. */
427 rec_fsino
= xfs_agino_to_ino(pag
, irec
->ir_startino
);
428 if (iwag
->lastino
!= NULLFSINO
&&
429 XFS_IS_CORRUPT(mp
, iwag
->lastino
>= rec_fsino
)) {
430 xfs_btree_mark_sick(cur
);
431 error
= -EFSCORRUPTED
;
434 iwag
->lastino
= rec_fsino
+ XFS_INODES_PER_CHUNK
- 1;
436 /* No allocated inodes in this chunk; skip it. */
437 if (iwag
->skip_empty
&& irec
->ir_freecount
== irec
->ir_count
) {
438 error
= xfs_btree_increment(cur
, 0, &has_more
);
445 * Start readahead for this inode chunk in anticipation of
446 * walking the inodes.
449 xfs_iwalk_ichunk_ra(mp
, pag
, irec
);
452 * If there's space in the buffer for more records, increment
453 * the btree cursor and grab more.
455 if (++iwag
->nr_recs
< iwag
->sz_recs
) {
456 error
= xfs_btree_increment(cur
, 0, &has_more
);
457 if (error
|| !has_more
)
463 * Otherwise, we need to save cursor state and run the callback
464 * function on the cached records. The run_callbacks function
465 * is supposed to return a cursor pointing to the record where
466 * we would be if we had been able to increment like above.
469 error
= xfs_iwalk_run_callbacks(iwag
, &cur
, &agi_bp
, &has_more
);
472 if (iwag
->nr_recs
== 0 || error
)
475 /* Walk the unprocessed records in the cache. */
476 error
= xfs_iwalk_run_callbacks(iwag
, &cur
, &agi_bp
, &has_more
);
479 xfs_iwalk_del_inobt(iwag
->tp
, &cur
, &agi_bp
, error
);
484 * We experimentally determined that the reduction in ioctl call overhead
485 * diminishes when userspace asks for more than 2048 inodes, so we'll cap
486 * prefetch at this point.
488 #define IWALK_MAX_INODE_PREFETCH (2048U)
491 * Given the number of inodes to prefetch, set the number of inobt records that
492 * we cache in memory, which controls the number of inodes we try to read
493 * ahead. Set the maximum if @inodes == 0.
495 static inline unsigned int
499 unsigned int inobt_records
;
502 * If the caller didn't tell us the number of inodes they wanted,
503 * assume the maximum prefetch possible for best performance.
504 * Otherwise, cap prefetch at that maximum so that we don't start an
505 * absurd amount of prefetch.
508 inodes
= IWALK_MAX_INODE_PREFETCH
;
509 inodes
= min(inodes
, IWALK_MAX_INODE_PREFETCH
);
511 /* Round the inode count up to a full chunk. */
512 inodes
= round_up(inodes
, XFS_INODES_PER_CHUNK
);
515 * In order to convert the number of inodes to prefetch into an
516 * estimate of the number of inobt records to cache, we require a
517 * conversion factor that reflects our expectations of the average
518 * loading factor of an inode chunk. Based on data gathered, most
519 * (but not all) filesystems manage to keep the inode chunks totally
520 * full, so we'll underestimate slightly so that our readahead will
521 * still deliver the performance we want on aging filesystems:
523 * inobt = inodes / (INODES_PER_CHUNK * (4 / 5));
525 * The funny math is to avoid integer division.
527 inobt_records
= (inodes
* 5) / (4 * XFS_INODES_PER_CHUNK
);
530 * Allocate enough space to prefetch at least two inobt records so that
531 * we can cache both the record where the iwalk started and the next
532 * record. This simplifies the AG inode walk loop setup code.
534 return max(inobt_records
, 2U);
539 struct xfs_iwalk_ag
*iwag
,
542 struct xfs_mount
*mp
= iwag
->mp
;
543 xfs_agnumber_t start_agno
;
546 start_agno
= XFS_INO_TO_AGNO(iwag
->mp
, iwag
->startino
);
547 ASSERT(start_agno
< iwag
->mp
->m_sb
.sb_agcount
);
548 ASSERT(!(flags
& ~XFS_IWALK_FLAGS_ALL
));
550 error
= xfs_iwalk_alloc(iwag
);
554 while ((iwag
->pag
= xfs_perag_next_from(mp
, iwag
->pag
, start_agno
))) {
555 error
= xfs_iwalk_ag(iwag
);
556 if (error
|| (flags
& XFS_IWALK_SAME_AG
)) {
557 xfs_perag_rele(iwag
->pag
);
561 XFS_AGINO_TO_INO(mp
, pag_agno(iwag
->pag
) + 1, 0);
564 xfs_iwalk_free(iwag
);
569 * Walk all inodes in the filesystem starting from @startino. The @iwalk_fn
570 * will be called for each allocated inode, being passed the inode's number and
571 * @data. @max_prefetch controls how many inobt records' worth of inodes we
576 struct xfs_mount
*mp
,
577 struct xfs_trans
*tp
,
580 xfs_iwalk_fn iwalk_fn
,
581 unsigned int inode_records
,
584 struct xfs_iwalk_ag iwag
= {
587 .iwalk_fn
= iwalk_fn
,
589 .startino
= startino
,
590 .sz_recs
= xfs_iwalk_prefetch(inode_records
),
593 .pwork
= XFS_PWORK_SINGLE_THREADED
,
594 .lastino
= NULLFSINO
,
597 return xfs_iwalk_args(&iwag
, flags
);
600 /* Run per-thread iwalk work. */
603 struct xfs_mount
*mp
,
604 struct xfs_pwork
*pwork
)
606 struct xfs_iwalk_ag
*iwag
;
609 iwag
= container_of(pwork
, struct xfs_iwalk_ag
, pwork
);
610 if (xfs_pwork_want_abort(pwork
))
613 error
= xfs_iwalk_alloc(iwag
);
617 * Grab an empty transaction so that we can use its recursive buffer
618 * locking abilities to detect cycles in the inobt without deadlocking.
620 error
= xfs_trans_alloc_empty(mp
, &iwag
->tp
);
623 iwag
->drop_trans
= 1;
625 error
= xfs_iwalk_ag(iwag
);
627 xfs_trans_cancel(iwag
->tp
);
628 xfs_iwalk_free(iwag
);
630 xfs_perag_put(iwag
->pag
);
636 * Walk all the inodes in the filesystem using multiple threads to process each
641 struct xfs_mount
*mp
,
644 xfs_iwalk_fn iwalk_fn
,
645 unsigned int inode_records
,
649 xfs_agnumber_t start_agno
= XFS_INO_TO_AGNO(mp
, startino
);
650 struct xfs_pwork_ctl pctl
;
651 struct xfs_perag
*pag
= NULL
;
654 ASSERT(start_agno
< mp
->m_sb
.sb_agcount
);
655 ASSERT(!(flags
& ~XFS_IWALK_FLAGS_ALL
));
657 error
= xfs_pwork_init(mp
, &pctl
, xfs_iwalk_ag_work
, "xfs_iwalk");
661 while ((pag
= xfs_perag_next_from(mp
, pag
, start_agno
))) {
662 struct xfs_iwalk_ag
*iwag
;
664 if (xfs_pwork_ctl_want_abort(&pctl
))
667 iwag
= kzalloc(sizeof(struct xfs_iwalk_ag
),
668 GFP_KERNEL
| __GFP_NOFAIL
);
672 * perag is being handed off to async work, so take a passive
673 * reference for the async work to release.
675 iwag
->pag
= xfs_perag_hold(pag
);
676 iwag
->iwalk_fn
= iwalk_fn
;
678 iwag
->startino
= startino
;
679 iwag
->sz_recs
= xfs_iwalk_prefetch(inode_records
);
680 iwag
->lastino
= NULLFSINO
;
681 xfs_pwork_queue(&pctl
, &iwag
->pwork
);
682 startino
= XFS_AGINO_TO_INO(mp
, pag_agno(pag
) + 1, 0);
683 if (flags
& XFS_IWALK_SAME_AG
)
689 xfs_pwork_poll(&pctl
);
690 return xfs_pwork_destroy(&pctl
);
694 * Allow callers to cache up to a page's worth of inobt records. This reflects
695 * the existing inumbers prefetching behavior. Since the inobt walk does not
696 * itself do anything with the inobt records, we can set a fairly high limit
699 #define MAX_INOBT_WALK_PREFETCH \
700 (PAGE_SIZE / sizeof(struct xfs_inobt_rec_incore))
703 * Given the number of records that the user wanted, set the number of inobt
704 * records that we buffer in memory. Set the maximum if @inobt_records == 0.
706 static inline unsigned int
707 xfs_inobt_walk_prefetch(
708 unsigned int inobt_records
)
711 * If the caller didn't tell us the number of inobt records they
712 * wanted, assume the maximum prefetch possible for best performance.
714 if (inobt_records
== 0)
715 inobt_records
= MAX_INOBT_WALK_PREFETCH
;
718 * Allocate enough space to prefetch at least two inobt records so that
719 * we can cache both the record where the iwalk started and the next
720 * record. This simplifies the AG inode walk loop setup code.
722 inobt_records
= max(inobt_records
, 2U);
725 * Cap prefetch at that maximum so that we don't use an absurd amount
728 return min_t(unsigned int, inobt_records
, MAX_INOBT_WALK_PREFETCH
);
732 * Walk all inode btree records in the filesystem starting from @startino. The
733 * @inobt_walk_fn will be called for each btree record, being passed the incore
734 * record and @data. @max_prefetch controls how many inobt records we try to
735 * cache ahead of time.
739 struct xfs_mount
*mp
,
740 struct xfs_trans
*tp
,
743 xfs_inobt_walk_fn inobt_walk_fn
,
744 unsigned int inobt_records
,
747 struct xfs_iwalk_ag iwag
= {
750 .inobt_walk_fn
= inobt_walk_fn
,
752 .startino
= startino
,
753 .sz_recs
= xfs_inobt_walk_prefetch(inobt_records
),
754 .pwork
= XFS_PWORK_SINGLE_THREADED
,
755 .lastino
= NULLFSINO
,
758 return xfs_iwalk_args(&iwag
, flags
);