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 93937590 : 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 93937590 : struct xfs_scrub *sc = iscan->sc;
68 93937590 : struct xfs_inobt_rec_incore rec;
69 93937590 : struct xfs_btree_cur *cur;
70 93937590 : struct xfs_mount *mp = sc->mp;
71 93937590 : struct xfs_trans *tp = sc->tp;
72 93937590 : xfs_agnumber_t agno = pag->pag_agno;
73 93937590 : xfs_agino_t lastino = NULLAGINO;
74 93937590 : xfs_agino_t first, last;
75 93937590 : xfs_agino_t agino = *cursor;
76 93937590 : int has_rec;
77 93937590 : int error;
78 :
79 : /* If the cursor is beyond the end of this AG, move to the next one. */
80 93937590 : xfs_agino_range(mp, agno, &first, &last);
81 93937568 : 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 93937568 : cur = xfs_inobt_init_cursor(pag, tp, agi_bp, XFS_BTNUM_INO);
91 93937950 : error = xfs_inobt_lookup(cur, agino, XFS_LOOKUP_LE, &has_rec);
92 93938485 : if (!error && !has_rec)
93 871684 : error = xfs_btree_increment(cur, 0, &has_rec);
94 186768385 : for (; !error; error = xfs_btree_increment(cur, 0, &has_rec)) {
95 186766806 : 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 186766806 : if (!has_rec) {
103 870041 : *cursor = NULLAGINO;
104 870041 : break;
105 : }
106 :
107 185896765 : error = xfs_inobt_get_rec(cur, &rec, &has_rec);
108 185897719 : if (error)
109 : break;
110 185897719 : if (!has_rec) {
111 : error = -EFSCORRUPTED;
112 : break;
113 : }
114 :
115 : /* Make sure that we always move forward. */
116 185897719 : if (lastino != NULLAGINO &&
117 92103554 : XFS_IS_CORRUPT(mp, lastino >= rec.ir_startino)) {
118 : error = -EFSCORRUPTED;
119 : break;
120 : }
121 185897719 : 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 185897719 : if (rec.ir_startino + XFS_INODES_PER_CHUNK <= agino)
128 875 : 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 185896844 : if (agino >= rec.ir_startino)
137 93065370 : rec.ir_free |= xfs_inobt_maskn(0,
138 93065370 : 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 185896844 : allocmask = ~rec.ir_free;
145 371793903 : if (hweight64(allocmask) > 0) {
146 93068033 : int next = xfs_lowbit64(allocmask);
147 :
148 93068033 : ASSERT(next >= 0);
149 93068033 : *cursor = rec.ir_startino + next;
150 93068033 : *allocmaskp = allocmask >> next;
151 93068033 : *nr_inodesp = XFS_INODES_PER_CHUNK - next;
152 93068033 : break;
153 : }
154 : }
155 :
156 93938074 : xfs_btree_del_cursor(cur, error);
157 93938074 : 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 93938505 : xchk_iscan_move_cursor(
171 : struct xchk_iscan *iscan,
172 : xfs_agnumber_t agno,
173 : xfs_agino_t agino)
174 : {
175 93938505 : struct xfs_scrub *sc = iscan->sc;
176 93938505 : struct xfs_mount *mp = sc->mp;
177 93938505 : xfs_ino_t cursor, visited;
178 :
179 93938505 : 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 93938505 : cursor = XFS_AGINO_TO_INO(mp, agno, agino);
187 93938505 : if (cursor == 0)
188 : visited = XFS_MAXINUMBER;
189 : else
190 93727191 : visited = cursor - 1;
191 :
192 93938505 : mutex_lock(&iscan->lock);
193 93938495 : iscan->cursor_ino = cursor;
194 93938495 : iscan->__visited_ino = visited;
195 93938495 : trace_xchk_iscan_move_cursor(iscan);
196 93938705 : mutex_unlock(&iscan->lock);
197 93938454 : }
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 423074 : xchk_iscan_finish(
206 : struct xchk_iscan *iscan)
207 : {
208 423074 : mutex_lock(&iscan->lock);
209 423075 : iscan->cursor_ino = NULLFSINO;
210 :
211 : /* All live updates will be applied from now on */
212 423075 : iscan->__visited_ino = NULLFSINO;
213 :
214 423075 : mutex_unlock(&iscan->lock);
215 423074 : }
216 :
217 : /* Mark an inode scan finished before we actually scan anything. */
218 : void
219 0 : xchk_iscan_finish_early(
220 : struct xchk_iscan *iscan)
221 : {
222 0 : ASSERT(iscan->cursor_ino == iscan->scan_start_ino);
223 0 : ASSERT(iscan->__visited_ino == iscan->scan_start_ino);
224 :
225 0 : xchk_iscan_finish(iscan);
226 0 : }
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 93279987 : 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 93279987 : struct xfs_scrub *sc = iscan->sc;
248 93279987 : struct xfs_mount *mp = sc->mp;
249 93279987 : struct xfs_buf *agi_bp;
250 93279987 : struct xfs_perag *pag;
251 93279987 : xfs_agnumber_t agno;
252 93279987 : xfs_agino_t agino;
253 93279987 : int ret;
254 :
255 93279987 : ASSERT(iscan->cursor_ino >= iscan->__visited_ino);
256 :
257 93938529 : do {
258 93938529 : if (xchk_iscan_aborted(iscan))
259 : return -ECANCELED;
260 :
261 93938513 : agno = XFS_INO_TO_AGNO(mp, iscan->cursor_ino);
262 93938513 : pag = xfs_perag_get(mp, agno);
263 93938601 : if (!pag)
264 : return -ECANCELED;
265 :
266 93938601 : ret = xfs_ialloc_read_agi(pag, sc->tp, &agi_bp);
267 93938290 : if (ret)
268 0 : goto out_pag;
269 :
270 93938290 : agino = XFS_INO_TO_AGINO(mp, iscan->cursor_ino);
271 93938290 : ret = xchk_iscan_find_next(iscan, agi_bp, pag, allocmaskp,
272 : &agino, nr_inodesp);
273 93938451 : if (ret)
274 0 : goto out_buf;
275 :
276 93938451 : 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 93068405 : xchk_iscan_move_cursor(iscan, agno, agino);
283 93068059 : *agi_bpp = agi_bp;
284 93068059 : *pagp = pag;
285 93068059 : return 1;
286 : }
287 :
288 : /*
289 : * Did not find any more inodes in this AG, move on to the next
290 : * AG.
291 : */
292 870046 : agno = (agno + 1) % mp->m_sb.sb_agcount;
293 870046 : xchk_iscan_move_cursor(iscan, agno, 0);
294 870041 : xfs_trans_brelse(sc->tp, agi_bp);
295 870045 : xfs_perag_put(pag);
296 :
297 870044 : trace_xchk_iscan_advance_ag(iscan);
298 870042 : } while (iscan->cursor_ino != iscan->scan_start_ino);
299 :
300 211500 : xchk_iscan_finish(iscan);
301 211500 : 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 125555 : xchk_iscan_iget_retry(
318 : struct xchk_iscan *iscan,
319 : bool wait)
320 : {
321 125555 : ASSERT(iscan->cursor_ino == iscan->__visited_ino + 1);
322 :
323 125555 : if (!iscan->iget_timeout ||
324 123091 : time_is_before_jiffies(iscan->__iget_deadline))
325 : return -EBUSY;
326 :
327 123091 : if (wait) {
328 123151 : 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 123151 : relax = msecs_to_jiffies(iscan->iget_retry_delay);
336 123151 : trace_xchk_iscan_iget_retry_wait(iscan);
337 :
338 246217 : if (schedule_timeout_killable(relax) ||
339 123066 : xchk_iscan_aborted(iscan))
340 30 : return -ECANCELED;
341 : }
342 :
343 123006 : iscan->cursor_ino--;
344 123006 : 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 93067594 : 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 93067594 : struct xfs_scrub *sc = iscan->sc;
365 93067594 : struct xfs_mount *mp = sc->mp;
366 93067594 : xfs_ino_t ino = iscan->cursor_ino;
367 93067594 : unsigned int idx = 0;
368 93067594 : unsigned int i;
369 93067594 : int error;
370 :
371 93067594 : ASSERT(iscan->__inodes[0] == NULL);
372 :
373 : /* Fill the first slot in the inode array. */
374 93067594 : error = xfs_iget(sc->mp, sc->tp, ino, XFS_IGET_NORETRY, 0,
375 93067594 : &iscan->__inodes[idx]);
376 :
377 93066761 : trace_xchk_iscan_iget(iscan, error);
378 :
379 93068410 : if (error == -ENOENT || error == -EAGAIN) {
380 125615 : xfs_trans_brelse(sc->tp, agi_bp);
381 125615 : 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 125615 : if (sc->tp && !(sc->tp->t_flags & XFS_TRANS_NO_WRITECOUNT))
391 0 : xfs_inodegc_push(mp);
392 : else
393 125615 : xfs_inodegc_flush(mp);
394 125614 : return xchk_iscan_iget_retry(iscan, true);
395 : }
396 :
397 92942795 : 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 92942795 : if (error) {
412 0 : xfs_trans_brelse(sc->tp, agi_bp);
413 0 : xfs_perag_put(pag);
414 0 : return error;
415 : }
416 92942795 : idx++;
417 92942795 : ino++;
418 92942795 : 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 92942795 : mutex_lock(&iscan->lock);
430 92942941 : iscan->__batch_ino = ino - 1;
431 92942941 : iscan->__skipped_inomask = 0;
432 92942941 : mutex_unlock(&iscan->lock);
433 :
434 5457981357 : for (i = 1; i < nr_inodes; i++, ino++, allocmask >>= 1) {
435 5272214046 : if (!(allocmask & 1)) {
436 441115848 : ASSERT(!(iscan->__skipped_inomask & (1ULL << i)));
437 :
438 441115848 : mutex_lock(&iscan->lock);
439 441121984 : iscan->cursor_ino = ino;
440 441121984 : iscan->__skipped_inomask |= (1ULL << i);
441 441121984 : mutex_unlock(&iscan->lock);
442 441117904 : continue;
443 : }
444 :
445 4831098198 : ASSERT(iscan->__inodes[idx] == NULL);
446 :
447 4831098198 : error = xfs_iget(sc->mp, sc->tp, ino, XFS_IGET_NORETRY, 0,
448 4831098198 : &iscan->__inodes[idx]);
449 4829784147 : if (error)
450 : break;
451 :
452 4829666398 : mutex_lock(&iscan->lock);
453 4831034453 : iscan->cursor_ino = ino;
454 4831034453 : mutex_unlock(&iscan->lock);
455 4830977945 : idx++;
456 : }
457 :
458 92942119 : trace_xchk_iscan_iget_batch(sc->mp, iscan, nr_inodes, idx);
459 92942241 : xfs_trans_brelse(sc->tp, agi_bp);
460 92943299 : xfs_perag_put(pag);
461 92943233 : return idx;
462 : }
463 :
464 : /*
465 : * Advance the visit cursor to reflect skipped inodes beyond whatever we
466 : * scanned.
467 : */
468 : STATIC void
469 93156744 : xchk_iscan_finish_batch(
470 : struct xchk_iscan *iscan)
471 : {
472 93156744 : xfs_ino_t highest_skipped;
473 :
474 93156744 : mutex_lock(&iscan->lock);
475 :
476 93157431 : if (iscan->__batch_ino != NULLFSINO) {
477 185886782 : highest_skipped = iscan->__batch_ino +
478 92943391 : xfs_highbit64(iscan->__skipped_inomask);
479 92943391 : iscan->__visited_ino = max(iscan->__visited_ino,
480 : highest_skipped);
481 :
482 92943391 : trace_xchk_iscan_skip(iscan);
483 : }
484 :
485 93157409 : iscan->__batch_ino = NULLFSINO;
486 93157409 : iscan->__skipped_inomask = 0;
487 :
488 93157409 : mutex_unlock(&iscan->lock);
489 93157263 : }
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 93156708 : xchk_iscan_iter_batch(
497 : struct xchk_iscan *iscan)
498 : {
499 93156708 : struct xfs_scrub *sc = iscan->sc;
500 93156708 : int ret;
501 :
502 93156708 : xchk_iscan_finish_batch(iscan);
503 :
504 93156998 : if (iscan->iget_timeout)
505 168539426 : iscan->__iget_deadline = jiffies +
506 : msecs_to_jiffies(iscan->iget_timeout);
507 :
508 93278768 : do {
509 93278768 : struct xfs_buf *agi_bp = NULL;
510 93278768 : struct xfs_perag *pag = NULL;
511 93278768 : xfs_inofree_t allocmask = 0;
512 93278768 : uint8_t nr_inodes = 0;
513 :
514 93278768 : ret = xchk_iscan_advance(iscan, &pag, &agi_bp, &allocmask,
515 : &nr_inodes);
516 93279728 : if (ret != 1)
517 211522 : return ret;
518 :
519 93068206 : 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 93068130 : ret = xchk_iscan_iget(iscan, pag, agi_bp, allocmask, nr_inodes);
527 93067350 : } 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 4922627586 : xchk_iscan_iter(
546 : struct xchk_iscan *iscan,
547 : struct xfs_inode **ipp)
548 : {
549 4922627586 : unsigned int i;
550 4922627586 : int error;
551 :
552 : /* Find a cached inode, or go get another batch. */
553 >14836*10^7 : for (i = 0; i < XFS_INODES_PER_CHUNK; i++) {
554 >14826*10^7 : if (iscan->__inodes[i])
555 4829470287 : goto foundit;
556 : }
557 :
558 93157299 : error = xchk_iscan_iter_batch(iscan);
559 93155158 : if (error <= 0)
560 : return error;
561 :
562 92941142 : ASSERT(iscan->__inodes[0] != NULL);
563 : i = 0;
564 :
565 4922411429 : foundit:
566 : /* Give the caller our reference. */
567 4922411429 : *ipp = iscan->__inodes[i];
568 4922411429 : iscan->__inodes[i] = NULL;
569 4922411429 : return 1;
570 : }
571 :
572 : /* Clean up an xfs_iscan_iter call by dropping any inodes that we still hold. */
573 : void
574 423144 : xchk_iscan_iter_finish(
575 : struct xchk_iscan *iscan)
576 : {
577 423144 : struct xfs_scrub *sc = iscan->sc;
578 423144 : unsigned int i;
579 :
580 27504035 : for (i = 0; i < XFS_INODES_PER_CHUNK; i++){
581 27080885 : if (iscan->__inodes[i]) {
582 834 : xchk_irele(sc, iscan->__inodes[i]);
583 840 : iscan->__inodes[i] = NULL;
584 : }
585 : }
586 423150 : }
587 :
588 : /* Mark this inode scan finished and release resources. */
589 : void
590 211573 : xchk_iscan_teardown(
591 : struct xchk_iscan *iscan)
592 : {
593 211573 : xchk_iscan_iter_finish(iscan);
594 211574 : xchk_iscan_finish(iscan);
595 211575 : mutex_destroy(&iscan->lock);
596 211574 : }
597 :
598 : /* Pick an AG from which to start a scan. */
599 : static inline xfs_ino_t
600 211575 : xchk_iscan_rotor(
601 : struct xfs_mount *mp)
602 : {
603 211575 : static atomic_t agi_rotor;
604 211575 : 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 211575 : r = (r % mp->m_sb.sb_agcount) + 1;
611 :
612 211575 : 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 211575 : 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 211575 : xfs_ino_t start_ino;
630 :
631 211575 : start_ino = xchk_iscan_rotor(sc->mp);
632 :
633 211575 : iscan->__batch_ino = NULLFSINO;
634 211575 : iscan->__skipped_inomask = 0;
635 :
636 211575 : iscan->sc = sc;
637 211575 : clear_bit(XCHK_ISCAN_OPSTATE_ABORTED, &iscan->__opstate);
638 211575 : iscan->iget_timeout = iget_timeout;
639 211575 : iscan->iget_retry_delay = iget_retry_delay;
640 211575 : iscan->__visited_ino = start_ino;
641 211575 : iscan->cursor_ino = start_ino;
642 211575 : iscan->scan_start_ino = start_ino;
643 211575 : mutex_init(&iscan->lock);
644 211575 : memset(iscan->__inodes, 0, sizeof(iscan->__inodes));
645 :
646 211575 : trace_xchk_iscan_start(iscan, start_ino);
647 211564 : }
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 4916469274 : xchk_iscan_mark_visited(
655 : struct xchk_iscan *iscan,
656 : struct xfs_inode *ip)
657 : {
658 4916469274 : mutex_lock(&iscan->lock);
659 4921309862 : iscan->__visited_ino = ip->i_ino;
660 4921309862 : trace_xchk_iscan_visit(iscan);
661 4921733069 : mutex_unlock(&iscan->lock);
662 4920249260 : }
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 10442186 : xchk_iscan_skipped(
671 : const struct xchk_iscan *iscan,
672 : xfs_ino_t ino)
673 : {
674 10442186 : if (iscan->__batch_ino == NULLFSINO)
675 : return false;
676 4708925 : if (ino < iscan->__batch_ino)
677 : return false;
678 2424581 : if (ino >= iscan->__batch_ino + XFS_INODES_PER_CHUNK)
679 : return false;
680 :
681 29926 : 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 25343492 : xchk_iscan_want_live_update(
692 : struct xchk_iscan *iscan,
693 : xfs_ino_t ino)
694 : {
695 25343492 : bool ret = false;
696 :
697 25343492 : if (xchk_iscan_aborted(iscan))
698 : return false;
699 :
700 25326770 : mutex_lock(&iscan->lock);
701 :
702 25328524 : trace_xchk_iscan_want_live_update(iscan, ino);
703 :
704 : /* Scan is finished, caller should receive all updates. */
705 25328524 : if (iscan->__visited_ino == NULLFSINO) {
706 14882937 : ret = true;
707 14882937 : 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 10445587 : if (iscan->scan_start_ino == iscan->__visited_ino) {
715 3401 : ret = false;
716 3401 : 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 10442186 : if (xchk_iscan_skipped(iscan, ino)) {
724 621 : ret = true;
725 621 : 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 10441565 : if (iscan->scan_start_ino <= iscan->__visited_ino) {
735 6255463 : if (ino >= iscan->scan_start_ino &&
736 : ino <= iscan->__visited_ino)
737 2376060 : ret = true;
738 :
739 6255463 : 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 4186102 : if (ino >= iscan->scan_start_ino || ino <= iscan->__visited_ino)
749 2959504 : ret = true;
750 :
751 1226598 : unlock:
752 25328524 : mutex_unlock(&iscan->lock);
753 25328524 : return ret;
754 : }
|