Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0-or-later
2 : /*
3 : * Copyright (C) 2021-2023 Oracle. All Rights Reserved.
4 : * Author: Darrick J. Wong <djwong@kernel.org>
5 : */
6 : #include "xfs.h"
7 : #include "xfs_fs.h"
8 : #include "xfs_shared.h"
9 : #include "xfs_format.h"
10 : #include "xfs_trans_resv.h"
11 : #include "xfs_mount.h"
12 : #include "xfs_log_format.h"
13 : #include "xfs_trans.h"
14 : #include "xfs_inode.h"
15 : #include "xfs_btree.h"
16 : #include "xfs_ialloc.h"
17 : #include "xfs_ialloc_btree.h"
18 : #include "xfs_ag.h"
19 : #include "xfs_error.h"
20 : #include "xfs_bit.h"
21 : #include "xfs_icache.h"
22 : #include "scrub/scrub.h"
23 : #include "scrub/iscan.h"
24 : #include "scrub/common.h"
25 : #include "scrub/trace.h"
26 :
27 : /*
28 : * Live File Scan
29 : * ==============
30 : *
31 : * Live file scans walk every inode in a live filesystem. This is more or
32 : * less like a regular iwalk, except that when we're advancing the scan cursor,
33 : * we must ensure that inodes cannot be added or deleted anywhere between the
34 : * old cursor value and the new cursor value. If we're advancing the cursor
35 : * by one inode, the caller must hold that inode; if we're finding the next
36 : * inode to scan, we must grab the AGI and hold it until we've updated the
37 : * scan cursor.
38 : *
39 : * Callers are expected to use this code to scan all files in the filesystem to
40 : * construct a new metadata index of some kind. The scan races against other
41 : * live updates, which means there must be a provision to update the new index
42 : * when updates are made to inodes that already been scanned. The iscan lock
43 : * can be used in live update hook code to stop the scan and protect this data
44 : * structure.
45 : *
46 : * To keep the new index up to date with other metadata updates being made to
47 : * the live filesystem, it is assumed that the caller will add hooks as needed
48 : * to be notified when a metadata update occurs. The inode scanner must tell
49 : * the hook code when an inode has been visited with xchk_iscan_mark_visit.
50 : * Hook functions can use xchk_iscan_want_live_update to decide if the
51 : * scanner's observations must be updated.
52 : */
53 :
54 : /*
55 : * Set *cursor to the next allocated inode after whatever it's set to now.
56 : * If there are no more inodes in this AG, cursor is set to NULLAGINO.
57 : */
58 : STATIC int
59 726332974 : xchk_iscan_find_next(
60 : struct xchk_iscan *iscan,
61 : struct xfs_buf *agi_bp,
62 : struct xfs_perag *pag,
63 : xfs_inofree_t *allocmaskp,
64 : xfs_agino_t *cursor,
65 : uint8_t *nr_inodesp)
66 : {
67 726332974 : struct xfs_scrub *sc = iscan->sc;
68 726332974 : struct xfs_inobt_rec_incore rec;
69 726332974 : struct xfs_btree_cur *cur;
70 726332974 : struct xfs_mount *mp = sc->mp;
71 726332974 : struct xfs_trans *tp = sc->tp;
72 726332974 : xfs_agnumber_t agno = pag->pag_agno;
73 726332974 : xfs_agino_t lastino = NULLAGINO;
74 726332974 : xfs_agino_t first, last;
75 726332974 : xfs_agino_t agino = *cursor;
76 726332974 : int has_rec;
77 726332974 : int error;
78 :
79 : /* If the cursor is beyond the end of this AG, move to the next one. */
80 726332974 : xfs_agino_range(mp, agno, &first, &last);
81 726297734 : if (agino > last) {
82 0 : *cursor = NULLAGINO;
83 0 : return 0;
84 : }
85 :
86 : /*
87 : * Look up the inode chunk for the current cursor position. If there
88 : * is no chunk here, we want the next one.
89 : */
90 726297734 : cur = xfs_inobt_init_cursor(pag, tp, agi_bp, XFS_BTNUM_INO);
91 726389471 : error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &has_rec);
92 726415515 : if (!error && !has_rec)
93 16180322 : error = xfs_btree_increment(cur, 0, &has_rec);
94 1433214505 : for (; !error; error = xfs_btree_increment(cur, 0, &has_rec)) {
95 1433329226 : xfs_inofree_t allocmask;
96 :
97 : /*
98 : * If we've run out of inobt records in this AG, move the
99 : * cursor on to the next AG and exit. The caller can try
100 : * again with the next AG.
101 : */
102 1433329226 : if (!has_rec) {
103 16074623 : *cursor = NULLAGINO;
104 16074623 : break;
105 : }
106 :
107 1417254603 : error = xfs_inobt_get_rec(cur, &rec, &has_rec);
108 1417139141 : if (error)
109 : break;
110 1417139141 : if (!has_rec) {
111 : error = -EFSCORRUPTED;
112 : break;
113 : }
114 :
115 : /* Make sure that we always move forward. */
116 1417139141 : if (lastino != NULLAGINO &&
117 697553145 : XFS_IS_CORRUPT(mp, lastino >= rec.ir_startino)) {
118 : error = -EFSCORRUPTED;
119 : break;
120 : }
121 1417139141 : lastino = rec.ir_startino + XFS_INODES_PER_CHUNK - 1;
122 :
123 : /*
124 : * If this record only covers inodes that come before the
125 : * cursor, advance to the next record.
126 : */
127 1417139141 : if (rec.ir_startino + XFS_INODES_PER_CHUNK <= agino)
128 31864 : continue;
129 :
130 : /*
131 : * If the incoming lookup put us in the middle of an inobt
132 : * record, mark it and the previous inodes "free" so that the
133 : * search for allocated inodes will start at the cursor.
134 : * We don't care about ir_freecount here.
135 : */
136 1417107277 : if (agino >= rec.ir_startino)
137 710072470 : rec.ir_free |= xfs_inobt_maskn(0,
138 710072470 : agino + 1 - rec.ir_startino);
139 :
140 : /*
141 : * If there are allocated inodes in this chunk, find them
142 : * and update the scan cursor.
143 : */
144 1417107277 : allocmask = ~rec.ir_free;
145 1417107277 : if (hweight64(allocmask) > 0) {
146 710231637 : int next = xfs_lowbit64(allocmask);
147 :
148 710231637 : ASSERT(next >= 0);
149 710231637 : *cursor = rec.ir_startino + next;
150 710231637 : *allocmaskp = allocmask >> next;
151 710231637 : *nr_inodesp = XFS_INODES_PER_CHUNK - next;
152 710231637 : break;
153 : }
154 : }
155 :
156 726306260 : xfs_btree_del_cursor(cur, error);
157 726306260 : return error;
158 : }
159 :
160 : /*
161 : * Advance both the scan and the visited cursors.
162 : *
163 : * The inumber address space for a given filesystem is sparse, which means that
164 : * the scan cursor can jump a long ways in a single iter() call. There are no
165 : * inodes in these sparse areas, so we must move the visited cursor forward at
166 : * the same time so that the scan user can receive live updates for inodes that
167 : * may get created once we release the AGI buffer.
168 : */
169 : static inline void
170 726310668 : xchk_iscan_move_cursor(
171 : struct xchk_iscan *iscan,
172 : xfs_agnumber_t agno,
173 : xfs_agino_t agino)
174 : {
175 726310668 : struct xfs_scrub *sc = iscan->sc;
176 726310668 : struct xfs_mount *mp = sc->mp;
177 726310668 : xfs_ino_t cursor, visited;
178 :
179 726310668 : BUILD_BUG_ON(XFS_MAXINUMBER == NULLFSINO);
180 :
181 : /*
182 : * Special-case ino == 0 here so that we never set visited_ino to
183 : * NULLFSINO when wrapping around EOFS, for that will let through all
184 : * live updates.
185 : */
186 726310668 : cursor = XFS_AGINO_TO_INO(mp, agno, agino);
187 726310668 : if (cursor == 0)
188 : visited = XFS_MAXINUMBER;
189 : else
190 723912466 : visited = cursor - 1;
191 :
192 726310668 : mutex_lock(&iscan->lock);
193 726431418 : iscan->cursor_ino = cursor;
194 726431418 : iscan->__visited_ino = visited;
195 726431418 : trace_xchk_iscan_move_cursor(iscan);
196 726228168 : mutex_unlock(&iscan->lock);
197 726366949 : }
198 :
199 : /*
200 : * Prepare to return agno/agino to the iscan caller by moving the lastino
201 : * cursor to the previous inode. Do this while we still hold the AGI so that
202 : * no other threads can create or delete inodes in this AG.
203 : */
204 : static inline void
205 4950300 : xchk_iscan_finish(
206 : struct xchk_iscan *iscan)
207 : {
208 4950300 : mutex_lock(&iscan->lock);
209 4950312 : iscan->cursor_ino = NULLFSINO;
210 :
211 : /* All live updates will be applied from now on */
212 4950312 : iscan->__visited_ino = NULLFSINO;
213 :
214 4950312 : mutex_unlock(&iscan->lock);
215 4950285 : }
216 :
217 : /* Mark an inode scan finished before we actually scan anything. */
218 : void
219 43746 : xchk_iscan_finish_early(
220 : struct xchk_iscan *iscan)
221 : {
222 43746 : ASSERT(iscan->cursor_ino == iscan->scan_start_ino);
223 43746 : ASSERT(iscan->__visited_ino == iscan->scan_start_ino);
224 :
225 43746 : xchk_iscan_finish(iscan);
226 43748 : }
227 :
228 : /*
229 : * Advance ino to the next inode that the inobt thinks is allocated, being
230 : * careful to jump to the next AG if we've reached the right end of this AG's
231 : * inode btree. Advancing ino effectively means that we've pushed the inode
232 : * scan forward, so set the iscan cursor to (ino - 1) so that our live update
233 : * predicates will track inode allocations in that part of the inode number
234 : * key space once we release the AGI buffer.
235 : *
236 : * Returns 1 if there's a new inode to examine, 0 if we've run out of inodes,
237 : * -ECANCELED if the live scan aborted, or the usual negative errno.
238 : */
239 : STATIC int
240 712640771 : xchk_iscan_advance(
241 : struct xchk_iscan *iscan,
242 : struct xfs_perag **pagp,
243 : struct xfs_buf **agi_bpp,
244 : xfs_inofree_t *allocmaskp,
245 : uint8_t *nr_inodesp)
246 : {
247 712640771 : struct xfs_scrub *sc = iscan->sc;
248 712640771 : struct xfs_mount *mp = sc->mp;
249 712640771 : struct xfs_buf *agi_bp;
250 712640771 : struct xfs_perag *pag;
251 712640771 : xfs_agnumber_t agno;
252 712640771 : xfs_agino_t agino;
253 712640771 : int ret;
254 :
255 712640771 : ASSERT(iscan->cursor_ino >= iscan->__visited_ino);
256 :
257 726405057 : do {
258 726405057 : if (xchk_iscan_aborted(iscan))
259 : return -ECANCELED;
260 :
261 726322270 : agno = XFS_INO_TO_AGNO(mp, iscan->cursor_ino);
262 726322270 : pag = xfs_perag_get(mp, agno);
263 726462813 : if (!pag)
264 : return -ECANCELED;
265 :
266 726462813 : ret = xfs_ialloc_read_agi(pag, sc->tp, &agi_bp);
267 726364576 : if (ret)
268 0 : goto out_pag;
269 :
270 726364576 : agino = XFS_INO_TO_AGINO(mp, iscan->cursor_ino);
271 726364576 : ret = xchk_iscan_find_next(iscan, agi_bp, pag, allocmaskp,
272 : &agino, nr_inodesp);
273 726397866 : if (ret)
274 0 : goto out_buf;
275 :
276 726397866 : if (agino != NULLAGINO) {
277 : /*
278 : * Found the next inode in this AG, so return it along
279 : * with the AGI buffer and the perag structure to
280 : * ensure it cannot go away.
281 : */
282 710209771 : xchk_iscan_move_cursor(iscan, agno, agino);
283 710169246 : *agi_bpp = agi_bp;
284 710169246 : *pagp = pag;
285 710169246 : return 1;
286 : }
287 :
288 : /*
289 : * Did not find any more inodes in this AG, move on to the next
290 : * AG.
291 : */
292 16188095 : agno = (agno + 1) % mp->m_sb.sb_agcount;
293 16188095 : xchk_iscan_move_cursor(iscan, agno, 0);
294 16164678 : xfs_trans_brelse(sc->tp, agi_bp);
295 16191993 : xfs_perag_put(pag);
296 :
297 16204252 : trace_xchk_iscan_advance_ag(iscan);
298 16195601 : } while (iscan->cursor_ino != iscan->scan_start_ino);
299 :
300 2431315 : xchk_iscan_finish(iscan);
301 2431315 : return 0;
302 :
303 : out_buf:
304 0 : xfs_trans_brelse(sc->tp, agi_bp);
305 0 : out_pag:
306 0 : xfs_perag_put(pag);
307 0 : return ret;
308 : }
309 :
310 : /*
311 : * Grabbing the inode failed, so we need to back up the scan and ask the caller
312 : * to try to _advance the scan again. Returns -EBUSY if we've run out of retry
313 : * opportunities, -ECANCELED if the process has a fatal signal pending, or
314 : * -EAGAIN if we should try again.
315 : */
316 : STATIC int
317 1773333 : xchk_iscan_iget_retry(
318 : struct xchk_iscan *iscan,
319 : bool wait)
320 : {
321 1773333 : ASSERT(iscan->cursor_ino == iscan->__visited_ino + 1);
322 :
323 1773333 : if (!iscan->iget_timeout ||
324 1769729 : time_is_before_jiffies(iscan->__iget_deadline))
325 : return -EBUSY;
326 :
327 1769729 : if (wait) {
328 1769873 : unsigned long relax;
329 :
330 : /*
331 : * Sleep for a period of time to let the rest of the system
332 : * catch up. If we return early, someone sent a kill signal to
333 : * the calling process.
334 : */
335 1769873 : relax = msecs_to_jiffies(iscan->iget_retry_delay);
336 1769870 : trace_xchk_iscan_iget_retry_wait(iscan);
337 :
338 3538487 : if (schedule_timeout_killable(relax) ||
339 1768988 : xchk_iscan_aborted(iscan))
340 105 : return -ECANCELED;
341 : }
342 :
343 1768484 : iscan->cursor_ino--;
344 1768484 : return -EAGAIN;
345 : }
346 :
347 : /*
348 : * Grab an inode as part of an inode scan. While scanning this inode, the
349 : * caller must ensure that no other threads can modify the inode until a call
350 : * to xchk_iscan_visit succeeds.
351 : *
352 : * Returns the number of incore inodes grabbed; -EAGAIN if the caller should
353 : * call again xchk_iscan_advance; -EBUSY if we couldn't grab an inode;
354 : * -ECANCELED if there's a fatal signal pending; or some other negative errno.
355 : */
356 : STATIC int
357 710088418 : xchk_iscan_iget(
358 : struct xchk_iscan *iscan,
359 : struct xfs_perag *pag,
360 : struct xfs_buf *agi_bp,
361 : xfs_inofree_t allocmask,
362 : uint8_t nr_inodes)
363 : {
364 710088418 : struct xfs_scrub *sc = iscan->sc;
365 710088418 : struct xfs_mount *mp = sc->mp;
366 710088418 : xfs_ino_t ino = iscan->cursor_ino;
367 710088418 : unsigned int idx = 0;
368 710088418 : unsigned int i;
369 710088418 : int error;
370 :
371 710088418 : ASSERT(iscan->__inodes[0] == NULL);
372 :
373 : /* Fill the first slot in the inode array. */
374 710088418 : error = xfs_iget(sc->mp, sc->tp, ino, XFS_IGET_NORETRY, 0,
375 710088418 : &iscan->__inodes[idx]);
376 :
377 710191652 : trace_xchk_iscan_iget(iscan, error);
378 :
379 710118677 : if (error == -ENOENT || error == -EAGAIN) {
380 1773492 : xfs_trans_brelse(sc->tp, agi_bp);
381 1773492 : xfs_perag_put(pag);
382 :
383 : /*¬
384 : * It's possible that this inode has lost all of its links but
385 : * hasn't yet been inactivated. If we don't have a transaction
386 : * or it's not writable, flush the inodegc workers and wait.
387 : * If we have a non-empty transaction, we must not block on
388 : * inodegc, which allocates its own transactions.
389 : */
390 1773492 : if (sc->tp && !(sc->tp->t_flags & XFS_TRANS_NO_WRITECOUNT))
391 583 : xfs_inodegc_push(mp);
392 : else
393 1772909 : xfs_inodegc_flush(mp);
394 1773476 : return xchk_iscan_iget_retry(iscan, true);
395 : }
396 :
397 708345185 : if (error == -EINVAL) {
398 0 : xfs_trans_brelse(sc->tp, agi_bp);
399 0 : xfs_perag_put(pag);
400 :
401 : /*
402 : * We thought the inode was allocated, but the inode btree
403 : * lookup failed, which means that it was freed since the last
404 : * time we advanced the cursor. Back up and try again. This
405 : * should never happen since still hold the AGI buffer from the
406 : * inobt check, but we need to be careful about infinite loops.
407 : */
408 0 : return xchk_iscan_iget_retry(iscan, false);
409 : }
410 :
411 708345185 : if (error) {
412 0 : xfs_trans_brelse(sc->tp, agi_bp);
413 0 : xfs_perag_put(pag);
414 0 : return error;
415 : }
416 708345185 : idx++;
417 708345185 : ino++;
418 708345185 : allocmask >>= 1;
419 :
420 : /*
421 : * Now that we've filled the first slot in __inodes, try to fill the
422 : * rest of the batch with consecutively ordered inodes. to reduce the
423 : * number of _iter calls. Make a bitmap of unallocated inodes from the
424 : * zeroes in the inuse bitmap; these inodes will not be scanned, but
425 : * the _want_live_update predicate will pass through all live updates.
426 : *
427 : * If we can't iget an allocated inode, stop and return what we have.
428 : */
429 708345185 : mutex_lock(&iscan->lock);
430 708483940 : iscan->__batch_ino = ino - 1;
431 708483940 : iscan->__skipped_inomask = 0;
432 708483940 : mutex_unlock(&iscan->lock);
433 :
434 42086330380 : for (i = 1; i < nr_inodes; i++, ino++, allocmask >>= 1) {
435 40670816264 : if (!(allocmask & 1)) {
436 3872864351 : ASSERT(!(iscan->__skipped_inomask & (1ULL << i)));
437 :
438 3872864351 : mutex_lock(&iscan->lock);
439 3873302478 : iscan->cursor_ino = ino;
440 3873302478 : iscan->__skipped_inomask |= (1ULL << i);
441 3873302478 : mutex_unlock(&iscan->lock);
442 3873034822 : continue;
443 : }
444 :
445 36797951913 : ASSERT(iscan->__inodes[idx] == NULL);
446 :
447 73539825171 : error = xfs_iget(sc->mp, sc->tp, ino, XFS_IGET_NORETRY, 0,
448 36770210321 : &iscan->__inodes[idx]);
449 36781260205 : if (error)
450 : break;
451 :
452 36779784484 : mutex_lock(&iscan->lock);
453 36826763175 : iscan->cursor_ino = ino;
454 36826763175 : mutex_unlock(&iscan->lock);
455 36796324920 : idx++;
456 : }
457 :
458 708505897 : trace_xchk_iscan_iget_batch(sc->mp, iscan, nr_inodes, idx);
459 708492497 : xfs_trans_brelse(sc->tp, agi_bp);
460 708491783 : xfs_perag_put(pag);
461 708510381 : return idx;
462 : }
463 :
464 : /*
465 : * Advance the visit cursor to reflect skipped inodes beyond whatever we
466 : * scanned.
467 : */
468 : STATIC void
469 710851736 : xchk_iscan_finish_batch(
470 : struct xchk_iscan *iscan)
471 : {
472 710851736 : xfs_ino_t highest_skipped;
473 :
474 710851736 : mutex_lock(&iscan->lock);
475 :
476 710909305 : if (iscan->__batch_ino != NULLFSINO) {
477 708478564 : highest_skipped = iscan->__batch_ino +
478 708478564 : xfs_highbit64(iscan->__skipped_inomask);
479 708478564 : iscan->__visited_ino = max(iscan->__visited_ino,
480 : highest_skipped);
481 :
482 708478564 : trace_xchk_iscan_skip(iscan);
483 : }
484 :
485 710769854 : iscan->__batch_ino = NULLFSINO;
486 710769854 : iscan->__skipped_inomask = 0;
487 :
488 710769854 : mutex_unlock(&iscan->lock);
489 710896378 : }
490 :
491 : /*
492 : * Advance the inode scan cursor to the next allocated inode and return up to
493 : * 64 consecutive allocated inodes starting with the cursor position.
494 : */
495 : STATIC int
496 710870923 : xchk_iscan_iter_batch(
497 : struct xchk_iscan *iscan)
498 : {
499 710870923 : struct xfs_scrub *sc = iscan->sc;
500 710870923 : int ret;
501 :
502 710870923 : xchk_iscan_finish_batch(iscan);
503 :
504 710877005 : if (iscan->iget_timeout)
505 1399488782 : iscan->__iget_deadline = jiffies +
506 : msecs_to_jiffies(iscan->iget_timeout);
507 :
508 712644361 : do {
509 712644361 : struct xfs_buf *agi_bp = NULL;
510 712644361 : struct xfs_perag *pag = NULL;
511 712644361 : xfs_inofree_t allocmask = 0;
512 712644361 : uint8_t nr_inodes = 0;
513 :
514 712644361 : ret = xchk_iscan_advance(iscan, &pag, &agi_bp, &allocmask,
515 : &nr_inodes);
516 712601204 : if (ret != 1)
517 2431421 : return ret;
518 :
519 710169783 : if (xchk_iscan_aborted(iscan)) {
520 0 : xfs_trans_brelse(sc->tp, agi_bp);
521 0 : xfs_perag_put(pag);
522 0 : ret = -ECANCELED;
523 0 : break;
524 : }
525 :
526 710126294 : ret = xchk_iscan_iget(iscan, pag, agi_bp, allocmask, nr_inodes);
527 710242970 : } while (ret == -EAGAIN);
528 :
529 : return ret;
530 : }
531 :
532 : /*
533 : * Advance the inode scan cursor to the next allocated inode and return the
534 : * incore inode structure associated with it.
535 : *
536 : * Returns 1 if there's a new inode to examine, 0 if we've run out of inodes,
537 : * -ECANCELED if the live scan aborted, -EBUSY if the incore inode could not be
538 : * grabbed, or the usual negative errno.
539 : *
540 : * If the function returns -EBUSY and the caller can handle skipping an inode,
541 : * it may call this function again to continue the scan with the next allocated
542 : * inode.
543 : */
544 : int
545 37243238733 : xchk_iscan_iter(
546 : struct xchk_iscan *iscan,
547 : struct xfs_inode **ipp)
548 : {
549 37243238733 : unsigned int i;
550 37243238733 : int error;
551 :
552 : /* Find a cached inode, or go get another batch. */
553 >11075*10^8 : for (i = 0; i < XFS_INODES_PER_CHUNK; i++) {
554 >11068*10^8 : if (iscan->__inodes[i])
555 36714206538 : goto foundit;
556 : }
557 :
558 710907025 : error = xchk_iscan_iter_batch(iscan);
559 710909952 : if (error <= 0)
560 : return error;
561 :
562 708474821 : ASSERT(iscan->__inodes[0] != NULL);
563 : i = 0;
564 :
565 37422681359 : foundit:
566 : /* Give the caller our reference. */
567 37422681359 : *ipp = iscan->__inodes[i];
568 37373179942 : iscan->__inodes[i] = NULL;
569 37361264642 : return 1;
570 : }
571 :
572 : /* Clean up an xfs_iscan_iter call by dropping any inodes that we still hold. */
573 : void
574 4906716 : xchk_iscan_iter_finish(
575 : struct xchk_iscan *iscan)
576 : {
577 4906716 : struct xfs_scrub *sc = iscan->sc;
578 4906716 : unsigned int i;
579 :
580 318894938 : for (i = 0; i < XFS_INODES_PER_CHUNK; i++){
581 313988114 : if (iscan->__inodes[i]) {
582 1926 : xchk_irele(sc, iscan->__inodes[i]);
583 1926 : iscan->__inodes[i] = NULL;
584 : }
585 : }
586 4906824 : }
587 :
588 : /* Mark this inode scan finished and release resources. */
589 : void
590 2475232 : xchk_iscan_teardown(
591 : struct xchk_iscan *iscan)
592 : {
593 2475232 : xchk_iscan_iter_finish(iscan);
594 2475244 : xchk_iscan_finish(iscan);
595 2475136 : mutex_destroy(&iscan->lock);
596 2474407 : }
597 :
598 : /* Pick an AG from which to start a scan. */
599 : static inline xfs_ino_t
600 2475263 : xchk_iscan_rotor(
601 : struct xfs_mount *mp)
602 : {
603 2475263 : static atomic_t agi_rotor;
604 2475263 : unsigned int r = atomic_inc_return(&agi_rotor) - 1;
605 :
606 : /*
607 : * Rotoring *backwards* through the AGs, so we add one here before
608 : * subtracting from the agcount to arrive at an AG number.
609 : */
610 2475275 : r = (r % mp->m_sb.sb_agcount) + 1;
611 :
612 2475275 : return XFS_AGINO_TO_INO(mp, mp->m_sb.sb_agcount - r, 0);
613 : }
614 :
615 : /*
616 : * Set ourselves up to start an inode scan. If the @iget_timeout and
617 : * @iget_retry_delay parameters are set, the scan will try to iget each inode
618 : * for @iget_timeout milliseconds. If an iget call indicates that the inode is
619 : * waiting to be inactivated, the CPU will relax for @iget_retry_delay
620 : * milliseconds after pushing the inactivation workers.
621 : */
622 : void
623 2475269 : xchk_iscan_start(
624 : struct xfs_scrub *sc,
625 : unsigned int iget_timeout,
626 : unsigned int iget_retry_delay,
627 : struct xchk_iscan *iscan)
628 : {
629 2475269 : xfs_ino_t start_ino;
630 :
631 2475269 : start_ino = xchk_iscan_rotor(sc->mp);
632 :
633 2475274 : iscan->__batch_ino = NULLFSINO;
634 2475274 : iscan->__skipped_inomask = 0;
635 :
636 2475274 : iscan->sc = sc;
637 2475274 : clear_bit(XCHK_ISCAN_OPSTATE_ABORTED, &iscan->__opstate);
638 2475311 : iscan->iget_timeout = iget_timeout;
639 2475311 : iscan->iget_retry_delay = iget_retry_delay;
640 2475311 : iscan->__visited_ino = start_ino;
641 2475311 : iscan->cursor_ino = start_ino;
642 2475311 : iscan->scan_start_ino = start_ino;
643 2475311 : mutex_init(&iscan->lock);
644 2475293 : memset(iscan->__inodes, 0, sizeof(iscan->__inodes));
645 :
646 2475293 : trace_xchk_iscan_start(iscan, start_ino);
647 2475202 : }
648 :
649 : /*
650 : * Mark this inode as having been visited. Callers must hold a sufficiently
651 : * exclusive lock on the inode to prevent concurrent modifications.
652 : */
653 : void
654 37233471320 : xchk_iscan_mark_visited(
655 : struct xchk_iscan *iscan,
656 : struct xfs_inode *ip)
657 : {
658 37233471320 : mutex_lock(&iscan->lock);
659 37421888938 : iscan->__visited_ino = ip->i_ino;
660 37421888938 : trace_xchk_iscan_visit(iscan);
661 37165195092 : mutex_unlock(&iscan->lock);
662 37374942620 : }
663 :
664 : /*
665 : * Did we skip this inode because it wasn't allocated when we loaded the batch?
666 : * If so, it is newly allocated and will not be scanned. All live updates to
667 : * this inode must be passed to the caller to maintain scan correctness.
668 : */
669 : static inline bool
670 22066923 : xchk_iscan_skipped(
671 : const struct xchk_iscan *iscan,
672 : xfs_ino_t ino)
673 : {
674 22066923 : if (iscan->__batch_ino == NULLFSINO)
675 : return false;
676 8091724 : if (ino < iscan->__batch_ino)
677 : return false;
678 4190323 : if (ino >= iscan->__batch_ino + XFS_INODES_PER_CHUNK)
679 : return false;
680 :
681 54290 : return iscan->__skipped_inomask & (1ULL << (ino - iscan->__batch_ino));
682 : }
683 :
684 : /*
685 : * Do we need a live update for this inode? This is true if the scanner thread
686 : * has visited this inode and the scan hasn't been aborted due to errors.
687 : * Callers must hold a sufficiently exclusive lock on the inode to prevent
688 : * scanners from reading any inode metadata.
689 : */
690 : bool
691 36925846 : xchk_iscan_want_live_update(
692 : struct xchk_iscan *iscan,
693 : xfs_ino_t ino)
694 : {
695 36925846 : bool ret = false;
696 :
697 36925846 : if (xchk_iscan_aborted(iscan))
698 : return false;
699 :
700 36913945 : mutex_lock(&iscan->lock);
701 :
702 36924366 : trace_xchk_iscan_want_live_update(iscan, ino);
703 :
704 : /* Scan is finished, caller should receive all updates. */
705 36924366 : if (iscan->__visited_ino == NULLFSINO) {
706 14828738 : ret = true;
707 14828738 : goto unlock;
708 : }
709 :
710 : /*
711 : * No inodes have been visited yet, so the visited cursor points at the
712 : * start of the scan range. The caller should not receive any updates.
713 : */
714 22095628 : if (iscan->scan_start_ino == iscan->__visited_ino) {
715 28705 : ret = false;
716 28705 : goto unlock;
717 : }
718 :
719 : /*
720 : * This inode was not allocated at the time of the iscan batch.
721 : * The caller should receive all updates.
722 : */
723 22066923 : if (xchk_iscan_skipped(iscan, ino)) {
724 1296 : ret = true;
725 1296 : goto unlock;
726 : }
727 :
728 : /*
729 : * The visited cursor hasn't yet wrapped around the end of the FS. If
730 : * @ino is inside the starred range, the caller should receive updates:
731 : *
732 : * 0 ------------ S ************ V ------------ EOFS
733 : */
734 22065627 : if (iscan->scan_start_ino <= iscan->__visited_ino) {
735 13173425 : if (ino >= iscan->scan_start_ino &&
736 : ino <= iscan->__visited_ino)
737 4795446 : ret = true;
738 :
739 13173425 : goto unlock;
740 : }
741 :
742 : /*
743 : * The visited cursor wrapped around the end of the FS. If @ino is
744 : * inside the starred range, the caller should receive updates:
745 : *
746 : * 0 ************ V ------------ S ************ EOFS
747 : */
748 8892202 : if (ino >= iscan->scan_start_ino || ino <= iscan->__visited_ino)
749 6213698 : ret = true;
750 :
751 2678504 : unlock:
752 36924366 : mutex_unlock(&iscan->lock);
753 36924366 : return ret;
754 : }
|