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 1061693864 : 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 1061693864 : struct xfs_scrub *sc = iscan->sc;
68 1061693864 : struct xfs_inobt_rec_incore rec;
69 1061693864 : struct xfs_btree_cur *cur;
70 1061693864 : struct xfs_mount *mp = sc->mp;
71 1061693864 : struct xfs_trans *tp = sc->tp;
72 1061693864 : xfs_agnumber_t agno = pag->pag_agno;
73 1061693864 : xfs_agino_t lastino = NULLAGINO;
74 1061693864 : xfs_agino_t first, last;
75 1061693864 : xfs_agino_t agino = *cursor;
76 1061693864 : int has_rec;
77 1061693864 : int error;
78 :
79 : /* If the cursor is beyond the end of this AG, move to the next one. */
80 1061693864 : xfs_agino_range(mp, agno, &first, &last);
81 1061610426 : 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 1061610426 : cur = xfs_inobt_init_cursor(pag, tp, agi_bp, XFS_BTNUM_INO);
91 1061944650 : error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &has_rec);
92 1061965959 : if (!error && !has_rec)
93 24318263 : error = xfs_btree_increment(cur, 0, &has_rec);
94 2096038202 : for (; !error; error = xfs_btree_increment(cur, 0, &has_rec)) {
95 2096259966 : 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 2096259966 : if (!has_rec) {
103 24181786 : *cursor = NULLAGINO;
104 24181786 : break;
105 : }
106 :
107 2072078180 : error = xfs_inobt_get_rec(cur, &rec, &has_rec);
108 2071792377 : if (error)
109 : break;
110 2071792377 : if (!has_rec) {
111 : error = -EFSCORRUPTED;
112 : break;
113 : }
114 :
115 : /* Make sure that we always move forward. */
116 2071792377 : if (lastino != NULLAGINO &&
117 1024847553 : XFS_IS_CORRUPT(mp, lastino >= rec.ir_startino)) {
118 : error = -EFSCORRUPTED;
119 : break;
120 : }
121 2071792377 : 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 2071792377 : if (rec.ir_startino + XFS_INODES_PER_CHUNK <= agino)
128 25193 : 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 2071767184 : if (agino >= rec.ir_startino)
137 1037243670 : rec.ir_free |= xfs_inobt_maskn(0,
138 1037243670 : 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 2071767184 : allocmask = ~rec.ir_free;
145 2071767184 : if (hweight64(allocmask) > 0) {
146 1037614436 : int next = xfs_lowbit64(allocmask);
147 :
148 1037614436 : ASSERT(next >= 0);
149 1037614436 : *cursor = rec.ir_startino + next;
150 1037614436 : *allocmaskp = allocmask >> next;
151 1037614436 : *nr_inodesp = XFS_INODES_PER_CHUNK - next;
152 1037614436 : break;
153 : }
154 : }
155 :
156 1061796222 : xfs_btree_del_cursor(cur, error);
157 1061796222 : 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 1061806880 : xchk_iscan_move_cursor(
171 : struct xchk_iscan *iscan,
172 : xfs_agnumber_t agno,
173 : xfs_agino_t agino)
174 : {
175 1061806880 : struct xfs_scrub *sc = iscan->sc;
176 1061806880 : struct xfs_mount *mp = sc->mp;
177 1061806880 : xfs_ino_t cursor, visited;
178 :
179 1061806880 : 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 1061806880 : cursor = XFS_AGINO_TO_INO(mp, agno, agino);
187 1061806880 : if (cursor == 0)
188 : visited = XFS_MAXINUMBER;
189 : else
190 1059269947 : visited = cursor - 1;
191 :
192 1061806880 : mutex_lock(&iscan->lock);
193 1062028294 : iscan->cursor_ino = cursor;
194 1062028294 : iscan->__visited_ino = visited;
195 1062028294 : trace_xchk_iscan_move_cursor(iscan);
196 1061684354 : mutex_unlock(&iscan->lock);
197 1061894470 : }
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 5289894 : xchk_iscan_finish(
206 : struct xchk_iscan *iscan)
207 : {
208 5289894 : mutex_lock(&iscan->lock);
209 5289890 : iscan->cursor_ino = NULLFSINO;
210 :
211 : /* All live updates will be applied from now on */
212 5289890 : iscan->__visited_ino = NULLFSINO;
213 :
214 5289890 : mutex_unlock(&iscan->lock);
215 5289691 : }
216 :
217 : /* Mark an inode scan finished before we actually scan anything. */
218 : void
219 44582 : xchk_iscan_finish_early(
220 : struct xchk_iscan *iscan)
221 : {
222 44582 : ASSERT(iscan->cursor_ino == iscan->scan_start_ino);
223 44582 : ASSERT(iscan->__visited_ino == iscan->scan_start_ino);
224 :
225 44582 : xchk_iscan_finish(iscan);
226 44582 : }
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 1040179995 : 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 1040179995 : struct xfs_scrub *sc = iscan->sc;
248 1040179995 : struct xfs_mount *mp = sc->mp;
249 1040179995 : struct xfs_buf *agi_bp;
250 1040179995 : struct xfs_perag *pag;
251 1040179995 : xfs_agnumber_t agno;
252 1040179995 : xfs_agino_t agino;
253 1040179995 : int ret;
254 :
255 1040179995 : ASSERT(iscan->cursor_ino >= iscan->__visited_ino);
256 :
257 1061919033 : do {
258 1061919033 : if (xchk_iscan_aborted(iscan))
259 : return -ECANCELED;
260 :
261 1061608906 : agno = XFS_INO_TO_AGNO(mp, iscan->cursor_ino);
262 1061608906 : pag = xfs_perag_get(mp, agno);
263 1062015693 : if (!pag)
264 : return -ECANCELED;
265 :
266 1062015693 : ret = xfs_ialloc_read_agi(pag, sc->tp, &agi_bp);
267 1061766924 : if (ret)
268 0 : goto out_pag;
269 :
270 1061766924 : agino = XFS_INO_TO_AGINO(mp, iscan->cursor_ino);
271 1061766924 : ret = xchk_iscan_find_next(iscan, agi_bp, pag, allocmaskp,
272 : &agino, nr_inodesp);
273 1061972830 : if (ret)
274 0 : goto out_buf;
275 :
276 1061972830 : 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 1037643375 : xchk_iscan_move_cursor(iscan, agno, agino);
283 1037570940 : *agi_bpp = agi_bp;
284 1037570940 : *pagp = pag;
285 1037570940 : return 1;
286 : }
287 :
288 : /*
289 : * Did not find any more inodes in this AG, move on to the next
290 : * AG.
291 : */
292 24329455 : agno = (agno + 1) % mp->m_sb.sb_agcount;
293 24329455 : xchk_iscan_move_cursor(iscan, agno, 0);
294 24295388 : xfs_trans_brelse(sc->tp, agi_bp);
295 24308055 : xfs_perag_put(pag);
296 :
297 24350535 : trace_xchk_iscan_advance_ag(iscan);
298 24339350 : } while (iscan->cursor_ino != iscan->scan_start_ino);
299 :
300 2600312 : xchk_iscan_finish(iscan);
301 2600312 : 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 1643166 : xchk_iscan_iget_retry(
318 : struct xchk_iscan *iscan,
319 : bool wait)
320 : {
321 1643166 : ASSERT(iscan->cursor_ino == iscan->__visited_ino + 1);
322 :
323 1643166 : if (!iscan->iget_timeout ||
324 1639747 : time_is_before_jiffies(iscan->__iget_deadline))
325 : return -EBUSY;
326 :
327 1639747 : if (wait) {
328 1639621 : 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 1639621 : relax = msecs_to_jiffies(iscan->iget_retry_delay);
336 1639620 : trace_xchk_iscan_iget_retry_wait(iscan);
337 :
338 3276597 : if (schedule_timeout_killable(relax) ||
339 1638264 : xchk_iscan_aborted(iscan))
340 163 : return -ECANCELED;
341 : }
342 :
343 1637120 : iscan->cursor_ino--;
344 1637120 : 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 1037486978 : 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 1037486978 : struct xfs_scrub *sc = iscan->sc;
365 1037486978 : struct xfs_mount *mp = sc->mp;
366 1037486978 : xfs_ino_t ino = iscan->cursor_ino;
367 1037486978 : unsigned int idx = 0;
368 1037486978 : unsigned int i;
369 1037486978 : int error;
370 :
371 1037486978 : ASSERT(iscan->__inodes[0] == NULL);
372 :
373 : /* Fill the first slot in the inode array. */
374 1037486978 : error = xfs_iget(sc->mp, sc->tp, ino, XFS_IGET_NORETRY, 0,
375 1037486978 : &iscan->__inodes[idx]);
376 :
377 1037539259 : trace_xchk_iscan_iget(iscan, error);
378 :
379 1037437344 : if (error == -ENOENT || error == -EAGAIN) {
380 1643086 : xfs_trans_brelse(sc->tp, agi_bp);
381 1643086 : 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 1643086 : if (sc->tp && !(sc->tp->t_flags & XFS_TRANS_NO_WRITECOUNT))
391 734 : xfs_inodegc_push(mp);
392 : else
393 1642352 : xfs_inodegc_flush(mp);
394 1643062 : return xchk_iscan_iget_retry(iscan, true);
395 : }
396 :
397 1035794258 : 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 1035794258 : if (error) {
412 0 : xfs_trans_brelse(sc->tp, agi_bp);
413 0 : xfs_perag_put(pag);
414 0 : return error;
415 : }
416 1035794258 : idx++;
417 1035794258 : ino++;
418 1035794258 : 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 1035794258 : mutex_lock(&iscan->lock);
430 1036026833 : iscan->__batch_ino = ino - 1;
431 1036026833 : iscan->__skipped_inomask = 0;
432 1036026833 : mutex_unlock(&iscan->lock);
433 :
434 62032749810 : for (i = 1; i < nr_inodes; i++, ino++, allocmask >>= 1) {
435 59962029463 : if (!(allocmask & 1)) {
436 5570415900 : ASSERT(!(iscan->__skipped_inomask & (1ULL << i)));
437 :
438 5570415900 : mutex_lock(&iscan->lock);
439 5571159505 : iscan->cursor_ino = ino;
440 5571159505 : iscan->__skipped_inomask |= (1ULL << i);
441 5571159505 : mutex_unlock(&iscan->lock);
442 5570438308 : continue;
443 : }
444 :
445 54391613563 : ASSERT(iscan->__inodes[idx] == NULL);
446 :
447 >10858*10^7 : error = xfs_iget(sc->mp, sc->tp, ino, XFS_IGET_NORETRY, 0,
448 54308158969 : &iscan->__inodes[idx]);
449 54342206504 : if (error)
450 : break;
451 :
452 54340808518 : mutex_lock(&iscan->lock);
453 54452150331 : iscan->cursor_ino = ino;
454 54452150331 : mutex_unlock(&iscan->lock);
455 54390320074 : idx++;
456 : }
457 :
458 1036091500 : trace_xchk_iscan_iget_batch(sc->mp, iscan, nr_inodes, idx);
459 1035996354 : xfs_trans_brelse(sc->tp, agi_bp);
460 1036048812 : xfs_perag_put(pag);
461 1036089779 : return idx;
462 : }
463 :
464 : /*
465 : * Advance the visit cursor to reflect skipped inodes beyond whatever we
466 : * scanned.
467 : */
468 : STATIC void
469 1038479030 : xchk_iscan_finish_batch(
470 : struct xchk_iscan *iscan)
471 : {
472 1038479030 : xfs_ino_t highest_skipped;
473 :
474 1038479030 : mutex_lock(&iscan->lock);
475 :
476 1038649450 : if (iscan->__batch_ino != NULLFSINO) {
477 1036045680 : highest_skipped = iscan->__batch_ino +
478 1036045680 : xfs_highbit64(iscan->__skipped_inomask);
479 1036045680 : iscan->__visited_ino = max(iscan->__visited_ino,
480 : highest_skipped);
481 :
482 1036045680 : trace_xchk_iscan_skip(iscan);
483 : }
484 :
485 1038338547 : iscan->__batch_ino = NULLFSINO;
486 1038338547 : iscan->__skipped_inomask = 0;
487 :
488 1038338547 : mutex_unlock(&iscan->lock);
489 1038547752 : }
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 1038499704 : xchk_iscan_iter_batch(
497 : struct xchk_iscan *iscan)
498 : {
499 1038499704 : struct xfs_scrub *sc = iscan->sc;
500 1038499704 : int ret;
501 :
502 1038499704 : xchk_iscan_finish_batch(iscan);
503 :
504 1038546141 : if (iscan->iget_timeout)
505 2055199701 : iscan->__iget_deadline = jiffies +
506 : msecs_to_jiffies(iscan->iget_timeout);
507 :
508 1040111879 : do {
509 1040111879 : struct xfs_buf *agi_bp = NULL;
510 1040111879 : struct xfs_perag *pag = NULL;
511 1040111879 : xfs_inofree_t allocmask = 0;
512 1040111879 : uint8_t nr_inodes = 0;
513 :
514 1040111879 : ret = xchk_iscan_advance(iscan, &pag, &agi_bp, &allocmask,
515 : &nr_inodes);
516 1040131883 : if (ret != 1)
517 2600410 : return ret;
518 :
519 1037531473 : 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 1037522652 : ret = xchk_iscan_iget(iscan, pag, agi_bp, allocmask, nr_inodes);
527 1037705708 : } 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 54968920326 : xchk_iscan_iter(
546 : struct xchk_iscan *iscan,
547 : struct xfs_inode **ipp)
548 : {
549 54968920326 : unsigned int i;
550 54968920326 : int error;
551 :
552 : /* Find a cached inode, or go get another batch. */
553 >16324*10^8 : for (i = 0; i < XFS_INODES_PER_CHUNK; i++) {
554 >16314*10^8 : if (iscan->__inodes[i])
555 54236352428 : goto foundit;
556 : }
557 :
558 1038627935 : error = xchk_iscan_iter_batch(iscan);
559 1038657687 : if (error <= 0)
560 : return error;
561 :
562 1036053697 : ASSERT(iscan->__inodes[0] != NULL);
563 : i = 0;
564 :
565 55272406125 : foundit:
566 : /* Give the caller our reference. */
567 55272406125 : *ipp = iscan->__inodes[i];
568 55174492263 : iscan->__inodes[i] = NULL;
569 55155393791 : return 1;
570 : }
571 :
572 : /* Clean up an xfs_iscan_iter call by dropping any inodes that we still hold. */
573 : void
574 5245370 : xchk_iscan_iter_finish(
575 : struct xchk_iscan *iscan)
576 : {
577 5245370 : struct xfs_scrub *sc = iscan->sc;
578 5245370 : unsigned int i;
579 :
580 340788077 : for (i = 0; i < XFS_INODES_PER_CHUNK; i++){
581 335542457 : if (iscan->__inodes[i]) {
582 2774 : xchk_irele(sc, iscan->__inodes[i]);
583 2774 : iscan->__inodes[i] = NULL;
584 : }
585 : }
586 5245620 : }
587 :
588 : /* Mark this inode scan finished and release resources. */
589 : void
590 2644920 : xchk_iscan_teardown(
591 : struct xchk_iscan *iscan)
592 : {
593 2644920 : xchk_iscan_iter_finish(iscan);
594 2645027 : xchk_iscan_finish(iscan);
595 2644771 : mutex_destroy(&iscan->lock);
596 2641668 : }
597 :
598 : /* Pick an AG from which to start a scan. */
599 : static inline xfs_ino_t
600 2644981 : xchk_iscan_rotor(
601 : struct xfs_mount *mp)
602 : {
603 2644981 : static atomic_t agi_rotor;
604 2644981 : 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 2645043 : r = (r % mp->m_sb.sb_agcount) + 1;
611 :
612 2645043 : 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 2645075 : 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 2645075 : xfs_ino_t start_ino;
630 :
631 2645075 : start_ino = xchk_iscan_rotor(sc->mp);
632 :
633 2645026 : iscan->__batch_ino = NULLFSINO;
634 2645026 : iscan->__skipped_inomask = 0;
635 :
636 2645026 : iscan->sc = sc;
637 2645026 : clear_bit(XCHK_ISCAN_OPSTATE_ABORTED, &iscan->__opstate);
638 2645179 : iscan->iget_timeout = iget_timeout;
639 2645179 : iscan->iget_retry_delay = iget_retry_delay;
640 2645179 : iscan->__visited_ino = start_ino;
641 2645179 : iscan->cursor_ino = start_ino;
642 2645179 : iscan->scan_start_ino = start_ino;
643 2645179 : mutex_init(&iscan->lock);
644 2645147 : memset(iscan->__inodes, 0, sizeof(iscan->__inodes));
645 :
646 2645147 : trace_xchk_iscan_start(iscan, start_ino);
647 2644902 : }
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 54908163546 : xchk_iscan_mark_visited(
655 : struct xchk_iscan *iscan,
656 : struct xfs_inode *ip)
657 : {
658 54908163546 : mutex_lock(&iscan->lock);
659 55292974632 : iscan->__visited_ino = ip->i_ino;
660 55292974632 : trace_xchk_iscan_visit(iscan);
661 54763103095 : mutex_unlock(&iscan->lock);
662 55173794668 : }
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 29278691 : xchk_iscan_skipped(
671 : const struct xchk_iscan *iscan,
672 : xfs_ino_t ino)
673 : {
674 29278691 : if (iscan->__batch_ino == NULLFSINO)
675 : return false;
676 13243748 : if (ino < iscan->__batch_ino)
677 : return false;
678 6807201 : if (ino >= iscan->__batch_ino + XFS_INODES_PER_CHUNK)
679 : return false;
680 :
681 117930 : 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 50615848 : xchk_iscan_want_live_update(
692 : struct xchk_iscan *iscan,
693 : xfs_ino_t ino)
694 : {
695 50615848 : bool ret = false;
696 :
697 50615848 : if (xchk_iscan_aborted(iscan))
698 : return false;
699 :
700 50605465 : mutex_lock(&iscan->lock);
701 :
702 50618686 : trace_xchk_iscan_want_live_update(iscan, ino);
703 :
704 : /* Scan is finished, caller should receive all updates. */
705 50618685 : if (iscan->__visited_ino == NULLFSINO) {
706 21334131 : ret = true;
707 21334131 : 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 29284554 : if (iscan->scan_start_ino == iscan->__visited_ino) {
715 5864 : ret = false;
716 5864 : 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 29278690 : if (xchk_iscan_skipped(iscan, ino)) {
724 2533 : ret = true;
725 2533 : 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 29276157 : if (iscan->scan_start_ino <= iscan->__visited_ino) {
735 17785041 : if (ino >= iscan->scan_start_ino &&
736 : ino <= iscan->__visited_ino)
737 6439187 : ret = true;
738 :
739 17785041 : 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 11491116 : if (ino >= iscan->scan_start_ino || ino <= iscan->__visited_ino)
749 8101130 : ret = true;
750 :
751 3389986 : unlock:
752 50618685 : mutex_unlock(&iscan->lock);
753 50618685 : return ret;
754 : }
|