Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0-or-later
2 : /*
3 : * Copyright (C) 2019 Oracle. All Rights Reserved.
4 : * Author: Darrick J. Wong <darrick.wong@oracle.com>
5 : */
6 : #include "xfs.h"
7 : #include "xfs_fs.h"
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"
24 : #include "xfs_ag.h"
25 : #include "xfs_bit.h"
26 :
27 : /*
28 : * Walking Inodes in the Filesystem
29 : * ================================
30 : *
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
37 : * caller.
38 : *
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.
44 : *
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
47 : * acted upon.
48 : */
49 :
50 : struct xfs_iwalk_ag {
51 : /* parallel work control data; will be null if single threaded */
52 : struct xfs_pwork pwork;
53 :
54 : struct xfs_mount *mp;
55 : struct xfs_trans *tp;
56 : struct xfs_perag *pag;
57 :
58 : /* Where do we start the traversal? */
59 : xfs_ino_t startino;
60 :
61 : /* What was the last inode number we saw when iterating the inobt? */
62 : xfs_ino_t lastino;
63 :
64 : /* Array of inobt records we cache. */
65 : struct xfs_inobt_rec_incore *recs;
66 :
67 : /* Number of entries allocated for the @recs array. */
68 : unsigned int sz_recs;
69 :
70 : /* Number of entries in the @recs array that are in use. */
71 : unsigned int nr_recs;
72 :
73 : /* Inode walk function and data pointer. */
74 : xfs_iwalk_fn iwalk_fn;
75 : xfs_inobt_walk_fn inobt_walk_fn;
76 : void *data;
77 :
78 : /*
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.
82 : */
83 : unsigned int trim_start:1;
84 :
85 : /* Skip empty inobt records? */
86 : unsigned int skip_empty:1;
87 :
88 : /* Drop the (hopefully empty) transaction when calling iwalk_fn. */
89 : unsigned int drop_trans:1;
90 : };
91 :
92 : /*
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.
95 : */
96 : STATIC void
97 2009111079 : xfs_iwalk_ichunk_ra(
98 : struct xfs_mount *mp,
99 : struct xfs_perag *pag,
100 : struct xfs_inobt_rec_incore *irec)
101 : {
102 2009111079 : struct xfs_ino_geometry *igeo = M_IGEO(mp);
103 2009111079 : xfs_agblock_t agbno;
104 2009111079 : struct blk_plug plug;
105 2009111079 : int i; /* inode chunk index */
106 :
107 2009111079 : agbno = XFS_AGINO_TO_AGBNO(mp, irec->ir_startino);
108 :
109 2009111079 : blk_start_plug(&plug);
110 8036150127 : for (i = 0; i < XFS_INODES_PER_CHUNK; i += igeo->inodes_per_cluster) {
111 4017688083 : xfs_inofree_t imask;
112 :
113 4017688083 : imask = xfs_inobt_maskn(i, igeo->inodes_per_cluster);
114 4017688083 : if (imask & ~irec->ir_free) {
115 3038141030 : xfs_btree_reada_bufs(mp, pag->pag_agno, agbno,
116 : igeo->blocks_per_cluster,
117 : &xfs_inode_buf_ops);
118 : }
119 4017918381 : agbno += igeo->blocks_per_cluster;
120 : }
121 2009350965 : blk_finish_plug(&plug);
122 2009405155 : }
123 :
124 : /*
125 : * Set the bits in @irec's free mask that correspond to the inodes before
126 : * @agino so that we skip them. This is how we restart an inode walk that was
127 : * interrupted in the middle of an inode record.
128 : */
129 : STATIC void
130 393442458 : xfs_iwalk_adjust_start(
131 : xfs_agino_t agino, /* starting inode of chunk */
132 : struct xfs_inobt_rec_incore *irec) /* btree record */
133 : {
134 393442458 : int idx; /* index into inode chunk */
135 :
136 393442458 : idx = agino - irec->ir_startino;
137 :
138 393442458 : irec->ir_free |= xfs_inobt_maskn(0, idx);
139 786885699 : irec->ir_freecount = hweight64(irec->ir_free);
140 393443241 : }
141 :
142 : /* Allocate memory for a walk. */
143 : STATIC int
144 402932939 : xfs_iwalk_alloc(
145 : struct xfs_iwalk_ag *iwag)
146 : {
147 402932939 : size_t size;
148 :
149 402932939 : ASSERT(iwag->recs == NULL);
150 402932939 : iwag->nr_recs = 0;
151 :
152 : /* Allocate a prefetch buffer for inobt records. */
153 402932939 : size = iwag->sz_recs * sizeof(struct xfs_inobt_rec_incore);
154 402932939 : iwag->recs = kmem_alloc(size, KM_MAYFAIL);
155 402913351 : if (iwag->recs == NULL)
156 0 : return -ENOMEM;
157 :
158 : return 0;
159 : }
160 :
161 : /* Free memory we allocated for a walk. */
162 : STATIC void
163 402955065 : xfs_iwalk_free(
164 : struct xfs_iwalk_ag *iwag)
165 : {
166 402955065 : kmem_free(iwag->recs);
167 402948541 : iwag->recs = NULL;
168 402948541 : }
169 :
170 : /* For each inuse inode in each cached inobt record, call our function. */
171 : STATIC int
172 525234068 : xfs_iwalk_ag_recs(
173 : struct xfs_iwalk_ag *iwag)
174 : {
175 525234068 : struct xfs_mount *mp = iwag->mp;
176 525234068 : struct xfs_trans *tp = iwag->tp;
177 525234068 : struct xfs_perag *pag = iwag->pag;
178 525234068 : xfs_ino_t ino;
179 525234068 : unsigned int i, j;
180 525234068 : int error;
181 :
182 1766909746 : for (i = 0; i < iwag->nr_recs; i++) {
183 1642165559 : struct xfs_inobt_rec_incore *irec = &iwag->recs[i];
184 :
185 1642165559 : trace_xfs_iwalk_ag_rec(mp, pag->pag_agno, irec);
186 :
187 3283629792 : if (xfs_pwork_want_abort(&iwag->pwork))
188 : return 0;
189 :
190 1641814896 : if (iwag->inobt_walk_fn) {
191 4699908 : error = iwag->inobt_walk_fn(mp, tp, pag->pag_agno, irec,
192 : iwag->data);
193 4697951 : if (error)
194 2927059 : return error;
195 : }
196 :
197 1638885880 : if (!iwag->iwalk_fn)
198 1772814 : continue;
199 :
200 93580421025 : for (j = 0; j < XFS_INODES_PER_CHUNK; j++) {
201 >18468*10^7 : if (xfs_pwork_want_abort(&iwag->pwork))
202 : return 0;
203 :
204 : /* Skip if this inode is free */
205 92340518161 : if (XFS_INOBT_MASK(j) & irec->ir_free)
206 31927018171 : continue;
207 :
208 : /* Otherwise call our function. */
209 60413499990 : ino = XFS_AGINO_TO_INO(mp, pag->pag_agno,
210 : irec->ir_startino + j);
211 60413499990 : error = iwag->iwalk_fn(mp, tp, ino, iwag->data);
212 60413847447 : if (error)
213 397557659 : return error;
214 : }
215 : }
216 :
217 : return 0;
218 : }
219 :
220 : /* Delete cursor and let go of AGI. */
221 : static inline void
222 931697581 : xfs_iwalk_del_inobt(
223 : struct xfs_trans *tp,
224 : struct xfs_btree_cur **curpp,
225 : struct xfs_buf **agi_bpp,
226 : int error)
227 : {
228 931697581 : if (*curpp) {
229 531206042 : xfs_btree_del_cursor(*curpp, error);
230 531215369 : *curpp = NULL;
231 : }
232 931706908 : if (*agi_bpp) {
233 531217210 : xfs_trans_brelse(tp, *agi_bpp);
234 531219526 : *agi_bpp = NULL;
235 : }
236 931709224 : }
237 :
238 : /*
239 : * Set ourselves up for walking inobt records starting from a given point in
240 : * the filesystem.
241 : *
242 : * If caller passed in a nonzero start inode number, load the record from the
243 : * inobt and make the record look like all the inodes before agino are free so
244 : * that we skip them, and then move the cursor to the next inobt record. This
245 : * is how we support starting an iwalk in the middle of an inode chunk.
246 : *
247 : * If the caller passed in a start number of zero, move the cursor to the first
248 : * inobt record.
249 : *
250 : * The caller is responsible for cleaning up the cursor and buffer pointer
251 : * regardless of the error status.
252 : */
253 : STATIC int
254 406461604 : xfs_iwalk_ag_start(
255 : struct xfs_iwalk_ag *iwag,
256 : xfs_agino_t agino,
257 : struct xfs_btree_cur **curpp,
258 : struct xfs_buf **agi_bpp,
259 : int *has_more)
260 : {
261 406461604 : struct xfs_mount *mp = iwag->mp;
262 406461604 : struct xfs_trans *tp = iwag->tp;
263 406461604 : struct xfs_perag *pag = iwag->pag;
264 406461604 : struct xfs_inobt_rec_incore *irec;
265 406461604 : int error;
266 :
267 : /* Set up a fresh cursor and empty the inobt cache. */
268 406461604 : iwag->nr_recs = 0;
269 406461604 : error = xfs_inobt_cur(pag, tp, XFS_BTNUM_INO, curpp, agi_bpp);
270 406469232 : if (error)
271 : return error;
272 :
273 : /* Starting at the beginning of the AG? That's easy! */
274 406468429 : if (agino == 0)
275 4823190 : return xfs_inobt_lookup(*curpp, 0, XFS_LOOKUP_GE, has_more);
276 :
277 : /*
278 : * Otherwise, we have to grab the inobt record where we left off, stuff
279 : * the record into our cache, and then see if there are more records.
280 : * We require a lookup cache of at least two elements so that the
281 : * caller doesn't have to deal with tearing down the cursor to walk the
282 : * records.
283 : */
284 401645239 : error = xfs_inobt_lookup(*curpp, agino, XFS_LOOKUP_LE, has_more);
285 401646048 : if (error)
286 : return error;
287 :
288 : /*
289 : * If the LE lookup at @agino yields no records, jump ahead to the
290 : * inobt cursor increment to see if there are more records to process.
291 : */
292 401646000 : if (!*has_more)
293 235 : goto out_advance;
294 :
295 : /* Get the record, should always work */
296 401645765 : irec = &iwag->recs[iwag->nr_recs];
297 401645765 : error = xfs_inobt_get_rec(*curpp, irec, has_more);
298 401646995 : if (error)
299 : return error;
300 401646995 : if (XFS_IS_CORRUPT(mp, *has_more != 1)) {
301 0 : xfs_btree_mark_sick(*curpp);
302 0 : return -EFSCORRUPTED;
303 : }
304 :
305 401646995 : iwag->lastino = XFS_AGINO_TO_INO(mp, pag->pag_agno,
306 : irec->ir_startino + XFS_INODES_PER_CHUNK - 1);
307 :
308 : /*
309 : * If the LE lookup yielded an inobt record before the cursor position,
310 : * skip it and see if there's another one after it.
311 : */
312 401646995 : if (irec->ir_startino + XFS_INODES_PER_CHUNK <= agino)
313 6536976 : goto out_advance;
314 :
315 : /*
316 : * If agino fell in the middle of the inode record, make it look like
317 : * the inodes up to agino are free so that we don't return them again.
318 : */
319 395110019 : if (iwag->trim_start)
320 393442636 : xfs_iwalk_adjust_start(agino, irec);
321 :
322 : /*
323 : * The prefetch calculation is supposed to give us a large enough inobt
324 : * record cache that grab_ichunk can stage a partial first record and
325 : * the loop body can cache a record without having to check for cache
326 : * space until after it reads an inobt record.
327 : */
328 395113046 : iwag->nr_recs++;
329 395113046 : ASSERT(iwag->nr_recs < iwag->sz_recs);
330 :
331 395113046 : out_advance:
332 401650257 : return xfs_btree_increment(*curpp, 0, has_more);
333 : }
334 :
335 : /*
336 : * The inobt record cache is full, so preserve the inobt cursor state and
337 : * run callbacks on the cached inobt records. When we're done, restore the
338 : * cursor state to wherever the cursor would have been had the cache not been
339 : * full (and therefore we could've just incremented the cursor) if *@has_more
340 : * is true. On exit, *@has_more will indicate whether or not the caller should
341 : * try for more inode records.
342 : */
343 : STATIC int
344 525216928 : xfs_iwalk_run_callbacks(
345 : struct xfs_iwalk_ag *iwag,
346 : struct xfs_btree_cur **curpp,
347 : struct xfs_buf **agi_bpp,
348 : int *has_more)
349 : {
350 525216928 : struct xfs_mount *mp = iwag->mp;
351 525216928 : struct xfs_inobt_rec_incore *irec;
352 525216928 : xfs_agino_t next_agino;
353 525216928 : int error;
354 :
355 525216928 : next_agino = XFS_INO_TO_AGINO(mp, iwag->lastino) + 1;
356 :
357 525216928 : ASSERT(iwag->nr_recs > 0);
358 :
359 : /* Delete cursor but remember the last record we cached... */
360 525216928 : xfs_iwalk_del_inobt(iwag->tp, curpp, agi_bpp, 0);
361 525216470 : irec = &iwag->recs[iwag->nr_recs - 1];
362 525216470 : ASSERT(next_agino >= irec->ir_startino + XFS_INODES_PER_CHUNK);
363 :
364 525216470 : if (iwag->drop_trans) {
365 3047 : xfs_trans_cancel(iwag->tp);
366 3047 : iwag->tp = NULL;
367 : }
368 :
369 525216470 : error = xfs_iwalk_ag_recs(iwag);
370 525219109 : if (error)
371 : return error;
372 :
373 : /* ...empty the cache... */
374 124742736 : iwag->nr_recs = 0;
375 :
376 124742736 : if (!has_more)
377 : return 0;
378 :
379 124742736 : if (iwag->drop_trans) {
380 3047 : error = xfs_trans_alloc_empty(mp, &iwag->tp);
381 3047 : if (error)
382 : return error;
383 : }
384 :
385 : /* ...and recreate the cursor just past where we left off. */
386 124742736 : error = xfs_inobt_cur(iwag->pag, iwag->tp, XFS_BTNUM_INO, curpp,
387 : agi_bpp);
388 124742223 : if (error)
389 : return error;
390 :
391 124735565 : return xfs_inobt_lookup(*curpp, next_agino, XFS_LOOKUP_GE, has_more);
392 : }
393 :
394 : /* Walk all inodes in a single AG, from @iwag->startino to the end of the AG. */
395 : STATIC int
396 406464790 : xfs_iwalk_ag(
397 : struct xfs_iwalk_ag *iwag)
398 : {
399 406464790 : struct xfs_mount *mp = iwag->mp;
400 406464790 : struct xfs_perag *pag = iwag->pag;
401 406464790 : struct xfs_buf *agi_bp = NULL;
402 406464790 : struct xfs_btree_cur *cur = NULL;
403 406464790 : xfs_agino_t agino;
404 406464790 : int has_more;
405 406464790 : int error = 0;
406 :
407 : /* Set up our cursor at the right place in the inode btree. */
408 406464790 : ASSERT(pag->pag_agno == XFS_INO_TO_AGNO(mp, iwag->startino));
409 406464790 : agino = XFS_INO_TO_AGINO(mp, iwag->startino);
410 406464790 : error = xfs_iwalk_ag_start(iwag, agino, &cur, &agi_bp, &has_more);
411 :
412 2416436789 : while (!error && has_more) {
413 2014824970 : struct xfs_inobt_rec_incore *irec;
414 2014824970 : xfs_ino_t rec_fsino;
415 :
416 2014824970 : cond_resched();
417 4030216920 : if (xfs_pwork_want_abort(&iwag->pwork))
418 0 : goto out;
419 :
420 : /* Fetch the inobt record. */
421 2015108460 : irec = &iwag->recs[iwag->nr_recs];
422 2015108460 : error = xfs_inobt_get_rec(cur, irec, &has_more);
423 2015004489 : if (error || !has_more)
424 : break;
425 :
426 : /* Make sure that we always move forward. */
427 2015004489 : rec_fsino = XFS_AGINO_TO_INO(mp, pag->pag_agno, irec->ir_startino);
428 2015004489 : if (iwag->lastino != NULLFSINO &&
429 2013776025 : XFS_IS_CORRUPT(mp, iwag->lastino >= rec_fsino)) {
430 0 : xfs_btree_mark_sick(cur);
431 0 : error = -EFSCORRUPTED;
432 0 : goto out;
433 : }
434 2015004489 : iwag->lastino = rec_fsino + XFS_INODES_PER_CHUNK - 1;
435 :
436 : /* No allocated inodes in this chunk; skip it. */
437 2015004489 : if (iwag->skip_empty && irec->ir_freecount == irec->ir_count) {
438 84 : error = xfs_btree_increment(cur, 0, &has_more);
439 84 : if (error)
440 : break;
441 84 : continue;
442 : }
443 :
444 : /*
445 : * Start readahead for this inode chunk in anticipation of
446 : * walking the inodes.
447 : */
448 2015004405 : if (iwag->iwalk_fn)
449 2009164183 : xfs_iwalk_ichunk_ra(mp, pag, irec);
450 :
451 : /*
452 : * If there's space in the buffer for more records, increment
453 : * the btree cursor and grab more.
454 : */
455 2015275868 : if (++iwag->nr_recs < iwag->sz_recs) {
456 1497028199 : error = xfs_btree_increment(cur, 0, &has_more);
457 1497009961 : if (error || !has_more)
458 : break;
459 1492141480 : continue;
460 : }
461 :
462 : /*
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.
467 : */
468 518247669 : ASSERT(has_more);
469 518247669 : error = xfs_iwalk_run_callbacks(iwag, &cur, &agi_bp, &has_more);
470 : }
471 :
472 406477728 : if (iwag->nr_recs == 0 || error)
473 399497335 : goto out;
474 :
475 : /* Walk the unprocessed records in the cache. */
476 6980393 : error = xfs_iwalk_run_callbacks(iwag, &cur, &agi_bp, &has_more);
477 :
478 406477715 : out:
479 406477715 : xfs_iwalk_del_inobt(iwag->tp, &cur, &agi_bp, error);
480 406475871 : return error;
481 : }
482 :
483 : /*
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.
487 : */
488 : #define IWALK_MAX_INODE_PREFETCH (2048U)
489 :
490 : /*
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.
494 : */
495 : static inline unsigned int
496 : xfs_iwalk_prefetch(
497 : unsigned int inodes)
498 : {
499 399807605 : unsigned int inobt_records;
500 :
501 : /*
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.
506 : */
507 399807605 : if (inodes == 0)
508 16393 : inodes = IWALK_MAX_INODE_PREFETCH;
509 399807605 : inodes = min(inodes, IWALK_MAX_INODE_PREFETCH);
510 :
511 : /* Round the inode count up to a full chunk. */
512 399807605 : inodes = round_up(inodes, XFS_INODES_PER_CHUNK);
513 :
514 : /*
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:
522 : *
523 : * inobt = inodes / (INODES_PER_CHUNK * (4 / 5));
524 : *
525 : * The funny math is to avoid integer division.
526 : */
527 399807605 : inobt_records = (inodes * 5) / (4 * XFS_INODES_PER_CHUNK);
528 :
529 : /*
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.
533 : */
534 399807605 : return max(inobt_records, 2U);
535 : }
536 :
537 : /*
538 : * Walk all inodes in the filesystem starting from @startino. The @iwalk_fn
539 : * will be called for each allocated inode, being passed the inode's number and
540 : * @data. @max_prefetch controls how many inobt records' worth of inodes we
541 : * try to readahead.
542 : */
543 : int
544 399791212 : xfs_iwalk(
545 : struct xfs_mount *mp,
546 : struct xfs_trans *tp,
547 : xfs_ino_t startino,
548 : unsigned int flags,
549 : xfs_iwalk_fn iwalk_fn,
550 : unsigned int inode_records,
551 : void *data)
552 : {
553 399791212 : struct xfs_iwalk_ag iwag = {
554 : .mp = mp,
555 : .tp = tp,
556 : .iwalk_fn = iwalk_fn,
557 : .data = data,
558 : .startino = startino,
559 : .sz_recs = xfs_iwalk_prefetch(inode_records),
560 : .trim_start = 1,
561 : .skip_empty = 1,
562 : .pwork = XFS_PWORK_SINGLE_THREADED,
563 : .lastino = NULLFSINO,
564 : };
565 399791212 : struct xfs_perag *pag;
566 399791212 : xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, startino);
567 399791212 : int error;
568 :
569 399791212 : ASSERT(agno < mp->m_sb.sb_agcount);
570 399791212 : ASSERT(!(flags & ~XFS_IWALK_FLAGS_ALL));
571 :
572 399791212 : error = xfs_iwalk_alloc(&iwag);
573 399803665 : if (error)
574 : return error;
575 :
576 405567450 : for_each_perag_from(mp, agno, pag) {
577 403327963 : iwag.pag = pag;
578 403327963 : error = xfs_iwalk_ag(&iwag);
579 403318102 : if (error)
580 : break;
581 5766270 : iwag.startino = XFS_AGINO_TO_INO(mp, agno + 1, 0);
582 5766270 : if (flags & XFS_INOBT_WALK_SAME_AG)
583 : break;
584 5765374 : iwag.pag = NULL;
585 : }
586 :
587 399803562 : if (iwag.pag)
588 397564967 : xfs_perag_rele(pag);
589 399809837 : xfs_iwalk_free(&iwag);
590 399809837 : return error;
591 : }
592 :
593 : /* Run per-thread iwalk work. */
594 : static int
595 16393 : xfs_iwalk_ag_work(
596 : struct xfs_mount *mp,
597 : struct xfs_pwork *pwork)
598 : {
599 16393 : struct xfs_iwalk_ag *iwag;
600 16393 : int error = 0;
601 :
602 16393 : iwag = container_of(pwork, struct xfs_iwalk_ag, pwork);
603 32786 : if (xfs_pwork_want_abort(pwork))
604 0 : goto out;
605 :
606 16393 : error = xfs_iwalk_alloc(iwag);
607 16393 : if (error)
608 0 : goto out;
609 : /*
610 : * Grab an empty transaction so that we can use its recursive buffer
611 : * locking abilities to detect cycles in the inobt without deadlocking.
612 : */
613 16393 : error = xfs_trans_alloc_empty(mp, &iwag->tp);
614 16391 : if (error)
615 0 : goto out;
616 16391 : iwag->drop_trans = 1;
617 :
618 16391 : error = xfs_iwalk_ag(iwag);
619 16389 : if (iwag->tp)
620 16389 : xfs_trans_cancel(iwag->tp);
621 16375 : xfs_iwalk_free(iwag);
622 16377 : out:
623 16377 : xfs_perag_put(iwag->pag);
624 16378 : kmem_free(iwag);
625 16379 : return error;
626 : }
627 :
628 : /*
629 : * Walk all the inodes in the filesystem using multiple threads to process each
630 : * AG.
631 : */
632 : int
633 2965 : xfs_iwalk_threaded(
634 : struct xfs_mount *mp,
635 : xfs_ino_t startino,
636 : unsigned int flags,
637 : xfs_iwalk_fn iwalk_fn,
638 : unsigned int inode_records,
639 : bool polled,
640 : void *data)
641 : {
642 2965 : struct xfs_pwork_ctl pctl;
643 2965 : struct xfs_perag *pag;
644 2965 : xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, startino);
645 2965 : int error;
646 :
647 2965 : ASSERT(agno < mp->m_sb.sb_agcount);
648 2965 : ASSERT(!(flags & ~XFS_IWALK_FLAGS_ALL));
649 :
650 2965 : error = xfs_pwork_init(mp, &pctl, xfs_iwalk_ag_work, "xfs_iwalk");
651 2965 : if (error)
652 : return error;
653 :
654 19358 : for_each_perag_from(mp, agno, pag) {
655 16393 : struct xfs_iwalk_ag *iwag;
656 :
657 32786 : if (xfs_pwork_ctl_want_abort(&pctl))
658 : break;
659 :
660 16393 : iwag = kmem_zalloc(sizeof(struct xfs_iwalk_ag), 0);
661 16393 : iwag->mp = mp;
662 :
663 : /*
664 : * perag is being handed off to async work, so take a passive
665 : * reference for the async work to release.
666 : */
667 16393 : iwag->pag = xfs_perag_hold(pag);
668 16393 : iwag->iwalk_fn = iwalk_fn;
669 16393 : iwag->data = data;
670 16393 : iwag->startino = startino;
671 16393 : iwag->sz_recs = xfs_iwalk_prefetch(inode_records);
672 16393 : iwag->lastino = NULLFSINO;
673 16393 : xfs_pwork_queue(&pctl, &iwag->pwork);
674 16393 : startino = XFS_AGINO_TO_INO(mp, pag->pag_agno + 1, 0);
675 16393 : if (flags & XFS_INOBT_WALK_SAME_AG)
676 : break;
677 : }
678 2965 : if (pag)
679 0 : xfs_perag_rele(pag);
680 2965 : if (polled)
681 2965 : xfs_pwork_poll(&pctl);
682 2965 : return xfs_pwork_destroy(&pctl);
683 : }
684 :
685 : /*
686 : * Allow callers to cache up to a page's worth of inobt records. This reflects
687 : * the existing inumbers prefetching behavior. Since the inobt walk does not
688 : * itself do anything with the inobt records, we can set a fairly high limit
689 : * here.
690 : */
691 : #define MAX_INOBT_WALK_PREFETCH \
692 : (PAGE_SIZE / sizeof(struct xfs_inobt_rec_incore))
693 :
694 : /*
695 : * Given the number of records that the user wanted, set the number of inobt
696 : * records that we buffer in memory. Set the maximum if @inobt_records == 0.
697 : */
698 : static inline unsigned int
699 : xfs_inobt_walk_prefetch(
700 : unsigned int inobt_records)
701 : {
702 : /*
703 : * If the caller didn't tell us the number of inobt records they
704 : * wanted, assume the maximum prefetch possible for best performance.
705 : */
706 3120064 : if (inobt_records == 0)
707 0 : inobt_records = MAX_INOBT_WALK_PREFETCH;
708 :
709 : /*
710 : * Allocate enough space to prefetch at least two inobt records so that
711 : * we can cache both the record where the iwalk started and the next
712 : * record. This simplifies the AG inode walk loop setup code.
713 : */
714 3120064 : inobt_records = max(inobt_records, 2U);
715 :
716 : /*
717 : * Cap prefetch at that maximum so that we don't use an absurd amount
718 : * of memory.
719 : */
720 3120064 : return min_t(unsigned int, inobt_records, MAX_INOBT_WALK_PREFETCH);
721 : }
722 :
723 : /*
724 : * Walk all inode btree records in the filesystem starting from @startino. The
725 : * @inobt_walk_fn will be called for each btree record, being passed the incore
726 : * record and @data. @max_prefetch controls how many inobt records we try to
727 : * cache ahead of time.
728 : */
729 : int
730 3120064 : xfs_inobt_walk(
731 : struct xfs_mount *mp,
732 : struct xfs_trans *tp,
733 : xfs_ino_t startino,
734 : unsigned int flags,
735 : xfs_inobt_walk_fn inobt_walk_fn,
736 : unsigned int inobt_records,
737 : void *data)
738 : {
739 3120064 : struct xfs_iwalk_ag iwag = {
740 : .mp = mp,
741 : .tp = tp,
742 : .inobt_walk_fn = inobt_walk_fn,
743 : .data = data,
744 : .startino = startino,
745 : .sz_recs = xfs_inobt_walk_prefetch(inobt_records),
746 : .pwork = XFS_PWORK_SINGLE_THREADED,
747 : .lastino = NULLFSINO,
748 : };
749 3120064 : struct xfs_perag *pag;
750 3120064 : xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, startino);
751 3120064 : int error;
752 :
753 3120064 : ASSERT(agno < mp->m_sb.sb_agcount);
754 3120064 : ASSERT(!(flags & ~XFS_INOBT_WALK_FLAGS_ALL));
755 :
756 3120064 : error = xfs_iwalk_alloc(&iwag);
757 3120044 : if (error)
758 : return error;
759 :
760 3127932 : for_each_perag_from(mp, agno, pag) {
761 3126354 : iwag.pag = pag;
762 3126354 : error = xfs_iwalk_ag(&iwag);
763 3126175 : if (error)
764 : break;
765 199137 : iwag.startino = XFS_AGINO_TO_INO(mp, pag->pag_agno + 1, 0);
766 199137 : if (flags & XFS_INOBT_WALK_SAME_AG)
767 : break;
768 7832 : iwag.pag = NULL;
769 : }
770 :
771 3119719 : if (iwag.pag)
772 3118239 : xfs_perag_rele(pag);
773 3119974 : xfs_iwalk_free(&iwag);
774 3119974 : return error;
775 : }
|