1
2
3
4
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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48struct xfs_iwalk_ag {
49
50 struct xfs_pwork pwork;
51
52 struct xfs_mount *mp;
53 struct xfs_trans *tp;
54
55
56 xfs_ino_t startino;
57
58
59 xfs_ino_t lastino;
60
61
62 struct xfs_inobt_rec_incore *recs;
63
64
65 unsigned int sz_recs;
66
67
68 unsigned int nr_recs;
69
70
71 xfs_iwalk_fn iwalk_fn;
72 xfs_inobt_walk_fn inobt_walk_fn;
73 void *data;
74
75
76
77
78
79
80 unsigned int trim_start:1;
81
82
83 unsigned int skip_empty:1;
84};
85
86
87
88
89
90STATIC void
91xfs_iwalk_ichunk_ra(
92 struct xfs_mount *mp,
93 xfs_agnumber_t agno,
94 struct xfs_inobt_rec_incore *irec)
95{
96 struct xfs_ino_geometry *igeo = M_IGEO(mp);
97 xfs_agblock_t agbno;
98 struct blk_plug plug;
99 int i;
100
101 agbno = XFS_AGINO_TO_AGBNO(mp, irec->ir_startino);
102
103 blk_start_plug(&plug);
104 for (i = 0; i < XFS_INODES_PER_CHUNK; i += igeo->inodes_per_cluster) {
105 xfs_inofree_t imask;
106
107 imask = xfs_inobt_maskn(i, igeo->inodes_per_cluster);
108 if (imask & ~irec->ir_free) {
109 xfs_btree_reada_bufs(mp, agno, agbno,
110 igeo->blocks_per_cluster,
111 &xfs_inode_buf_ops);
112 }
113 agbno += igeo->blocks_per_cluster;
114 }
115 blk_finish_plug(&plug);
116}
117
118
119
120
121
122
123STATIC void
124xfs_iwalk_adjust_start(
125 xfs_agino_t agino,
126 struct xfs_inobt_rec_incore *irec)
127{
128 int idx;
129 int i;
130
131 idx = agino - irec->ir_startino;
132
133
134
135
136
137
138 for (i = 0; i < idx; i++) {
139 if (XFS_INOBT_MASK(i) & ~irec->ir_free)
140 irec->ir_freecount++;
141 }
142
143 irec->ir_free |= xfs_inobt_maskn(0, idx);
144}
145
146
147STATIC int
148xfs_iwalk_alloc(
149 struct xfs_iwalk_ag *iwag)
150{
151 size_t size;
152
153 ASSERT(iwag->recs == NULL);
154 iwag->nr_recs = 0;
155
156
157 size = iwag->sz_recs * sizeof(struct xfs_inobt_rec_incore);
158 iwag->recs = kmem_alloc(size, KM_MAYFAIL);
159 if (iwag->recs == NULL)
160 return -ENOMEM;
161
162 return 0;
163}
164
165
166STATIC void
167xfs_iwalk_free(
168 struct xfs_iwalk_ag *iwag)
169{
170 kmem_free(iwag->recs);
171 iwag->recs = NULL;
172}
173
174
175STATIC int
176xfs_iwalk_ag_recs(
177 struct xfs_iwalk_ag *iwag)
178{
179 struct xfs_mount *mp = iwag->mp;
180 struct xfs_trans *tp = iwag->tp;
181 xfs_ino_t ino;
182 unsigned int i, j;
183 xfs_agnumber_t agno;
184 int error;
185
186 agno = XFS_INO_TO_AGNO(mp, iwag->startino);
187 for (i = 0; i < iwag->nr_recs; i++) {
188 struct xfs_inobt_rec_incore *irec = &iwag->recs[i];
189
190 trace_xfs_iwalk_ag_rec(mp, agno, irec);
191
192 if (xfs_pwork_want_abort(&iwag->pwork))
193 return 0;
194
195 if (iwag->inobt_walk_fn) {
196 error = iwag->inobt_walk_fn(mp, tp, agno, irec,
197 iwag->data);
198 if (error)
199 return error;
200 }
201
202 if (!iwag->iwalk_fn)
203 continue;
204
205 for (j = 0; j < XFS_INODES_PER_CHUNK; j++) {
206 if (xfs_pwork_want_abort(&iwag->pwork))
207 return 0;
208
209
210 if (XFS_INOBT_MASK(j) & irec->ir_free)
211 continue;
212
213
214 ino = XFS_AGINO_TO_INO(mp, agno, irec->ir_startino + j);
215 error = iwag->iwalk_fn(mp, tp, ino, iwag->data);
216 if (error)
217 return error;
218 }
219 }
220
221 return 0;
222}
223
224
225static inline void
226xfs_iwalk_del_inobt(
227 struct xfs_trans *tp,
228 struct xfs_btree_cur **curpp,
229 struct xfs_buf **agi_bpp,
230 int error)
231{
232 if (*curpp) {
233 xfs_btree_del_cursor(*curpp, error);
234 *curpp = NULL;
235 }
236 if (*agi_bpp) {
237 xfs_trans_brelse(tp, *agi_bpp);
238 *agi_bpp = NULL;
239 }
240}
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257STATIC int
258xfs_iwalk_ag_start(
259 struct xfs_iwalk_ag *iwag,
260 xfs_agnumber_t agno,
261 xfs_agino_t agino,
262 struct xfs_btree_cur **curpp,
263 struct xfs_buf **agi_bpp,
264 int *has_more)
265{
266 struct xfs_mount *mp = iwag->mp;
267 struct xfs_trans *tp = iwag->tp;
268 struct xfs_inobt_rec_incore *irec;
269 int error;
270
271
272 iwag->nr_recs = 0;
273 error = xfs_inobt_cur(mp, tp, agno, XFS_BTNUM_INO, curpp, agi_bpp);
274 if (error)
275 return error;
276
277
278 if (agino == 0)
279 return xfs_inobt_lookup(*curpp, 0, XFS_LOOKUP_GE, has_more);
280
281
282
283
284
285
286
287
288 error = xfs_inobt_lookup(*curpp, agino, XFS_LOOKUP_LE, has_more);
289 if (error)
290 return error;
291
292
293
294
295
296 if (!*has_more)
297 goto out_advance;
298
299
300 irec = &iwag->recs[iwag->nr_recs];
301 error = xfs_inobt_get_rec(*curpp, irec, has_more);
302 if (error)
303 return error;
304 if (XFS_IS_CORRUPT(mp, *has_more != 1))
305 return -EFSCORRUPTED;
306
307 iwag->lastino = XFS_AGINO_TO_INO(mp, agno,
308 irec->ir_startino + XFS_INODES_PER_CHUNK - 1);
309
310
311
312
313
314 if (irec->ir_startino + XFS_INODES_PER_CHUNK <= agino)
315 goto out_advance;
316
317
318
319
320
321 if (iwag->trim_start)
322 xfs_iwalk_adjust_start(agino, irec);
323
324
325
326
327
328
329
330 iwag->nr_recs++;
331 ASSERT(iwag->nr_recs < iwag->sz_recs);
332
333out_advance:
334 return xfs_btree_increment(*curpp, 0, has_more);
335}
336
337
338
339
340
341
342
343
344
345STATIC int
346xfs_iwalk_run_callbacks(
347 struct xfs_iwalk_ag *iwag,
348 xfs_agnumber_t agno,
349 struct xfs_btree_cur **curpp,
350 struct xfs_buf **agi_bpp,
351 int *has_more)
352{
353 struct xfs_mount *mp = iwag->mp;
354 struct xfs_trans *tp = iwag->tp;
355 struct xfs_inobt_rec_incore *irec;
356 xfs_agino_t next_agino;
357 int error;
358
359 next_agino = XFS_INO_TO_AGINO(mp, iwag->lastino) + 1;
360
361 ASSERT(iwag->nr_recs > 0);
362
363
364 xfs_iwalk_del_inobt(tp, curpp, agi_bpp, 0);
365 irec = &iwag->recs[iwag->nr_recs - 1];
366 ASSERT(next_agino >= irec->ir_startino + XFS_INODES_PER_CHUNK);
367
368 error = xfs_iwalk_ag_recs(iwag);
369 if (error)
370 return error;
371
372
373 iwag->nr_recs = 0;
374
375 if (!has_more)
376 return 0;
377
378
379 error = xfs_inobt_cur(mp, tp, agno, XFS_BTNUM_INO, curpp, agi_bpp);
380 if (error)
381 return error;
382
383 return xfs_inobt_lookup(*curpp, next_agino, XFS_LOOKUP_GE, has_more);
384}
385
386
387STATIC int
388xfs_iwalk_ag(
389 struct xfs_iwalk_ag *iwag)
390{
391 struct xfs_mount *mp = iwag->mp;
392 struct xfs_trans *tp = iwag->tp;
393 struct xfs_buf *agi_bp = NULL;
394 struct xfs_btree_cur *cur = NULL;
395 xfs_agnumber_t agno;
396 xfs_agino_t agino;
397 int has_more;
398 int error = 0;
399
400
401 agno = XFS_INO_TO_AGNO(mp, iwag->startino);
402 agino = XFS_INO_TO_AGINO(mp, iwag->startino);
403 error = xfs_iwalk_ag_start(iwag, agno, agino, &cur, &agi_bp, &has_more);
404
405 while (!error && has_more) {
406 struct xfs_inobt_rec_incore *irec;
407 xfs_ino_t rec_fsino;
408
409 cond_resched();
410 if (xfs_pwork_want_abort(&iwag->pwork))
411 goto out;
412
413
414 irec = &iwag->recs[iwag->nr_recs];
415 error = xfs_inobt_get_rec(cur, irec, &has_more);
416 if (error || !has_more)
417 break;
418
419
420 rec_fsino = XFS_AGINO_TO_INO(mp, agno, irec->ir_startino);
421 if (iwag->lastino != NULLFSINO &&
422 XFS_IS_CORRUPT(mp, iwag->lastino >= rec_fsino)) {
423 error = -EFSCORRUPTED;
424 goto out;
425 }
426 iwag->lastino = rec_fsino + XFS_INODES_PER_CHUNK - 1;
427
428
429 if (iwag->skip_empty && irec->ir_freecount == irec->ir_count) {
430 error = xfs_btree_increment(cur, 0, &has_more);
431 if (error)
432 break;
433 continue;
434 }
435
436
437
438
439
440 if (iwag->iwalk_fn)
441 xfs_iwalk_ichunk_ra(mp, agno, irec);
442
443
444
445
446
447 if (++iwag->nr_recs < iwag->sz_recs) {
448 error = xfs_btree_increment(cur, 0, &has_more);
449 if (error || !has_more)
450 break;
451 continue;
452 }
453
454
455
456
457
458
459
460 ASSERT(has_more);
461 error = xfs_iwalk_run_callbacks(iwag, agno, &cur, &agi_bp,
462 &has_more);
463 }
464
465 if (iwag->nr_recs == 0 || error)
466 goto out;
467
468
469 error = xfs_iwalk_run_callbacks(iwag, agno, &cur, &agi_bp, &has_more);
470
471out:
472 xfs_iwalk_del_inobt(tp, &cur, &agi_bp, error);
473 return error;
474}
475
476
477
478
479
480
481#define IWALK_MAX_INODE_PREFETCH (2048U)
482
483
484
485
486
487
488static inline unsigned int
489xfs_iwalk_prefetch(
490 unsigned int inodes)
491{
492 unsigned int inobt_records;
493
494
495
496
497
498
499
500 if (inodes == 0)
501 inodes = IWALK_MAX_INODE_PREFETCH;
502 inodes = min(inodes, IWALK_MAX_INODE_PREFETCH);
503
504
505 inodes = round_up(inodes, XFS_INODES_PER_CHUNK);
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520 inobt_records = (inodes * 5) / (4 * XFS_INODES_PER_CHUNK);
521
522
523
524
525
526
527 return max(inobt_records, 2U);
528}
529
530
531
532
533
534
535
536int
537xfs_iwalk(
538 struct xfs_mount *mp,
539 struct xfs_trans *tp,
540 xfs_ino_t startino,
541 unsigned int flags,
542 xfs_iwalk_fn iwalk_fn,
543 unsigned int inode_records,
544 void *data)
545{
546 struct xfs_iwalk_ag iwag = {
547 .mp = mp,
548 .tp = tp,
549 .iwalk_fn = iwalk_fn,
550 .data = data,
551 .startino = startino,
552 .sz_recs = xfs_iwalk_prefetch(inode_records),
553 .trim_start = 1,
554 .skip_empty = 1,
555 .pwork = XFS_PWORK_SINGLE_THREADED,
556 .lastino = NULLFSINO,
557 };
558 xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, startino);
559 int error;
560
561 ASSERT(agno < mp->m_sb.sb_agcount);
562 ASSERT(!(flags & ~XFS_IWALK_FLAGS_ALL));
563
564 error = xfs_iwalk_alloc(&iwag);
565 if (error)
566 return error;
567
568 for (; agno < mp->m_sb.sb_agcount; agno++) {
569 error = xfs_iwalk_ag(&iwag);
570 if (error)
571 break;
572 iwag.startino = XFS_AGINO_TO_INO(mp, agno + 1, 0);
573 if (flags & XFS_INOBT_WALK_SAME_AG)
574 break;
575 }
576
577 xfs_iwalk_free(&iwag);
578 return error;
579}
580
581
582static int
583xfs_iwalk_ag_work(
584 struct xfs_mount *mp,
585 struct xfs_pwork *pwork)
586{
587 struct xfs_iwalk_ag *iwag;
588 int error = 0;
589
590 iwag = container_of(pwork, struct xfs_iwalk_ag, pwork);
591 if (xfs_pwork_want_abort(pwork))
592 goto out;
593
594 error = xfs_iwalk_alloc(iwag);
595 if (error)
596 goto out;
597
598 error = xfs_iwalk_ag(iwag);
599 xfs_iwalk_free(iwag);
600out:
601 kmem_free(iwag);
602 return error;
603}
604
605
606
607
608
609int
610xfs_iwalk_threaded(
611 struct xfs_mount *mp,
612 xfs_ino_t startino,
613 unsigned int flags,
614 xfs_iwalk_fn iwalk_fn,
615 unsigned int inode_records,
616 bool polled,
617 void *data)
618{
619 struct xfs_pwork_ctl pctl;
620 xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, startino);
621 unsigned int nr_threads;
622 int error;
623
624 ASSERT(agno < mp->m_sb.sb_agcount);
625 ASSERT(!(flags & ~XFS_IWALK_FLAGS_ALL));
626
627 nr_threads = xfs_pwork_guess_datadev_parallelism(mp);
628 error = xfs_pwork_init(mp, &pctl, xfs_iwalk_ag_work, "xfs_iwalk",
629 nr_threads);
630 if (error)
631 return error;
632
633 for (; agno < mp->m_sb.sb_agcount; agno++) {
634 struct xfs_iwalk_ag *iwag;
635
636 if (xfs_pwork_ctl_want_abort(&pctl))
637 break;
638
639 iwag = kmem_zalloc(sizeof(struct xfs_iwalk_ag), 0);
640 iwag->mp = mp;
641 iwag->iwalk_fn = iwalk_fn;
642 iwag->data = data;
643 iwag->startino = startino;
644 iwag->sz_recs = xfs_iwalk_prefetch(inode_records);
645 iwag->lastino = NULLFSINO;
646 xfs_pwork_queue(&pctl, &iwag->pwork);
647 startino = XFS_AGINO_TO_INO(mp, agno + 1, 0);
648 if (flags & XFS_INOBT_WALK_SAME_AG)
649 break;
650 }
651
652 if (polled)
653 xfs_pwork_poll(&pctl);
654 return xfs_pwork_destroy(&pctl);
655}
656
657
658
659
660
661
662
663#define MAX_INOBT_WALK_PREFETCH \
664 (PAGE_SIZE / sizeof(struct xfs_inobt_rec_incore))
665
666
667
668
669
670static inline unsigned int
671xfs_inobt_walk_prefetch(
672 unsigned int inobt_records)
673{
674
675
676
677
678 if (inobt_records == 0)
679 inobt_records = MAX_INOBT_WALK_PREFETCH;
680
681
682
683
684
685
686 inobt_records = max(inobt_records, 2U);
687
688
689
690
691
692 return min_t(unsigned int, inobt_records, MAX_INOBT_WALK_PREFETCH);
693}
694
695
696
697
698
699
700
701int
702xfs_inobt_walk(
703 struct xfs_mount *mp,
704 struct xfs_trans *tp,
705 xfs_ino_t startino,
706 unsigned int flags,
707 xfs_inobt_walk_fn inobt_walk_fn,
708 unsigned int inobt_records,
709 void *data)
710{
711 struct xfs_iwalk_ag iwag = {
712 .mp = mp,
713 .tp = tp,
714 .inobt_walk_fn = inobt_walk_fn,
715 .data = data,
716 .startino = startino,
717 .sz_recs = xfs_inobt_walk_prefetch(inobt_records),
718 .pwork = XFS_PWORK_SINGLE_THREADED,
719 .lastino = NULLFSINO,
720 };
721 xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, startino);
722 int error;
723
724 ASSERT(agno < mp->m_sb.sb_agcount);
725 ASSERT(!(flags & ~XFS_INOBT_WALK_FLAGS_ALL));
726
727 error = xfs_iwalk_alloc(&iwag);
728 if (error)
729 return error;
730
731 for (; agno < mp->m_sb.sb_agcount; agno++) {
732 error = xfs_iwalk_ag(&iwag);
733 if (error)
734 break;
735 iwag.startino = XFS_AGINO_TO_INO(mp, agno + 1, 0);
736 if (flags & XFS_INOBT_WALK_SAME_AG)
737 break;
738 }
739
740 xfs_iwalk_free(&iwag);
741 return error;
742}
743