xfs_iwalk.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2019 Oracle. All Rights Reserved.
  4. * Author: Darrick J. Wong <[email protected]>
  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. /*
  26. * Walking Inodes in the Filesystem
  27. * ================================
  28. *
  29. * This iterator function walks a subset of filesystem inodes in increasing
  30. * order from @startino until there are no more inodes. For each allocated
  31. * inode it finds, it calls a walk function with the relevant inode number and
  32. * a pointer to caller-provided data. The walk function can return the usual
  33. * negative error code to stop the iteration; 0 to continue the iteration; or
  34. * -ECANCELED to stop the iteration. This return value is returned to the
  35. * caller.
  36. *
  37. * Internally, we allow the walk function to do anything, which means that we
  38. * cannot maintain the inobt cursor or our lock on the AGI buffer. We
  39. * therefore cache the inobt records in kernel memory and only call the walk
  40. * function when our memory buffer is full. @nr_recs is the number of records
  41. * that we've cached, and @sz_recs is the size of our cache.
  42. *
  43. * It is the responsibility of the walk function to ensure it accesses
  44. * allocated inodes, as the inobt records may be stale by the time they are
  45. * acted upon.
  46. */
  47. struct xfs_iwalk_ag {
  48. /* parallel work control data; will be null if single threaded */
  49. struct xfs_pwork pwork;
  50. struct xfs_mount *mp;
  51. struct xfs_trans *tp;
  52. struct xfs_perag *pag;
  53. /* Where do we start the traversal? */
  54. xfs_ino_t startino;
  55. /* What was the last inode number we saw when iterating the inobt? */
  56. xfs_ino_t lastino;
  57. /* Array of inobt records we cache. */
  58. struct xfs_inobt_rec_incore *recs;
  59. /* Number of entries allocated for the @recs array. */
  60. unsigned int sz_recs;
  61. /* Number of entries in the @recs array that are in use. */
  62. unsigned int nr_recs;
  63. /* Inode walk function and data pointer. */
  64. xfs_iwalk_fn iwalk_fn;
  65. xfs_inobt_walk_fn inobt_walk_fn;
  66. void *data;
  67. /*
  68. * Make it look like the inodes up to startino are free so that
  69. * bulkstat can start its inode iteration at the correct place without
  70. * needing to special case everywhere.
  71. */
  72. unsigned int trim_start:1;
  73. /* Skip empty inobt records? */
  74. unsigned int skip_empty:1;
  75. /* Drop the (hopefully empty) transaction when calling iwalk_fn. */
  76. unsigned int drop_trans:1;
  77. };
  78. /*
  79. * Loop over all clusters in a chunk for a given incore inode allocation btree
  80. * record. Do a readahead if there are any allocated inodes in that cluster.
  81. */
  82. STATIC void
  83. xfs_iwalk_ichunk_ra(
  84. struct xfs_mount *mp,
  85. struct xfs_perag *pag,
  86. struct xfs_inobt_rec_incore *irec)
  87. {
  88. struct xfs_ino_geometry *igeo = M_IGEO(mp);
  89. xfs_agblock_t agbno;
  90. struct blk_plug plug;
  91. int i; /* inode chunk index */
  92. agbno = XFS_AGINO_TO_AGBNO(mp, irec->ir_startino);
  93. blk_start_plug(&plug);
  94. for (i = 0; i < XFS_INODES_PER_CHUNK; i += igeo->inodes_per_cluster) {
  95. xfs_inofree_t imask;
  96. imask = xfs_inobt_maskn(i, igeo->inodes_per_cluster);
  97. if (imask & ~irec->ir_free) {
  98. xfs_btree_reada_bufs(mp, pag->pag_agno, agbno,
  99. igeo->blocks_per_cluster,
  100. &xfs_inode_buf_ops);
  101. }
  102. agbno += igeo->blocks_per_cluster;
  103. }
  104. blk_finish_plug(&plug);
  105. }
  106. /*
  107. * Set the bits in @irec's free mask that correspond to the inodes before
  108. * @agino so that we skip them. This is how we restart an inode walk that was
  109. * interrupted in the middle of an inode record.
  110. */
  111. STATIC void
  112. xfs_iwalk_adjust_start(
  113. xfs_agino_t agino, /* starting inode of chunk */
  114. struct xfs_inobt_rec_incore *irec) /* btree record */
  115. {
  116. int idx; /* index into inode chunk */
  117. int i;
  118. idx = agino - irec->ir_startino;
  119. /*
  120. * We got a right chunk with some left inodes allocated at it. Grab
  121. * the chunk record. Mark all the uninteresting inodes free because
  122. * they're before our start point.
  123. */
  124. for (i = 0; i < idx; i++) {
  125. if (XFS_INOBT_MASK(i) & ~irec->ir_free)
  126. irec->ir_freecount++;
  127. }
  128. irec->ir_free |= xfs_inobt_maskn(0, idx);
  129. }
  130. /* Allocate memory for a walk. */
  131. STATIC int
  132. xfs_iwalk_alloc(
  133. struct xfs_iwalk_ag *iwag)
  134. {
  135. size_t size;
  136. ASSERT(iwag->recs == NULL);
  137. iwag->nr_recs = 0;
  138. /* Allocate a prefetch buffer for inobt records. */
  139. size = iwag->sz_recs * sizeof(struct xfs_inobt_rec_incore);
  140. iwag->recs = kmem_alloc(size, KM_MAYFAIL);
  141. if (iwag->recs == NULL)
  142. return -ENOMEM;
  143. return 0;
  144. }
  145. /* Free memory we allocated for a walk. */
  146. STATIC void
  147. xfs_iwalk_free(
  148. struct xfs_iwalk_ag *iwag)
  149. {
  150. kmem_free(iwag->recs);
  151. iwag->recs = NULL;
  152. }
  153. /* For each inuse inode in each cached inobt record, call our function. */
  154. STATIC int
  155. xfs_iwalk_ag_recs(
  156. struct xfs_iwalk_ag *iwag)
  157. {
  158. struct xfs_mount *mp = iwag->mp;
  159. struct xfs_trans *tp = iwag->tp;
  160. struct xfs_perag *pag = iwag->pag;
  161. xfs_ino_t ino;
  162. unsigned int i, j;
  163. int error;
  164. for (i = 0; i < iwag->nr_recs; i++) {
  165. struct xfs_inobt_rec_incore *irec = &iwag->recs[i];
  166. trace_xfs_iwalk_ag_rec(mp, pag->pag_agno, irec);
  167. if (xfs_pwork_want_abort(&iwag->pwork))
  168. return 0;
  169. if (iwag->inobt_walk_fn) {
  170. error = iwag->inobt_walk_fn(mp, tp, pag->pag_agno, irec,
  171. iwag->data);
  172. if (error)
  173. return error;
  174. }
  175. if (!iwag->iwalk_fn)
  176. continue;
  177. for (j = 0; j < XFS_INODES_PER_CHUNK; j++) {
  178. if (xfs_pwork_want_abort(&iwag->pwork))
  179. return 0;
  180. /* Skip if this inode is free */
  181. if (XFS_INOBT_MASK(j) & irec->ir_free)
  182. continue;
  183. /* Otherwise call our function. */
  184. ino = XFS_AGINO_TO_INO(mp, pag->pag_agno,
  185. irec->ir_startino + j);
  186. error = iwag->iwalk_fn(mp, tp, ino, iwag->data);
  187. if (error)
  188. return error;
  189. }
  190. }
  191. return 0;
  192. }
  193. /* Delete cursor and let go of AGI. */
  194. static inline void
  195. xfs_iwalk_del_inobt(
  196. struct xfs_trans *tp,
  197. struct xfs_btree_cur **curpp,
  198. struct xfs_buf **agi_bpp,
  199. int error)
  200. {
  201. if (*curpp) {
  202. xfs_btree_del_cursor(*curpp, error);
  203. *curpp = NULL;
  204. }
  205. if (*agi_bpp) {
  206. xfs_trans_brelse(tp, *agi_bpp);
  207. *agi_bpp = NULL;
  208. }
  209. }
  210. /*
  211. * Set ourselves up for walking inobt records starting from a given point in
  212. * the filesystem.
  213. *
  214. * If caller passed in a nonzero start inode number, load the record from the
  215. * inobt and make the record look like all the inodes before agino are free so
  216. * that we skip them, and then move the cursor to the next inobt record. This
  217. * is how we support starting an iwalk in the middle of an inode chunk.
  218. *
  219. * If the caller passed in a start number of zero, move the cursor to the first
  220. * inobt record.
  221. *
  222. * The caller is responsible for cleaning up the cursor and buffer pointer
  223. * regardless of the error status.
  224. */
  225. STATIC int
  226. xfs_iwalk_ag_start(
  227. struct xfs_iwalk_ag *iwag,
  228. xfs_agino_t agino,
  229. struct xfs_btree_cur **curpp,
  230. struct xfs_buf **agi_bpp,
  231. int *has_more)
  232. {
  233. struct xfs_mount *mp = iwag->mp;
  234. struct xfs_trans *tp = iwag->tp;
  235. struct xfs_perag *pag = iwag->pag;
  236. struct xfs_inobt_rec_incore *irec;
  237. int error;
  238. /* Set up a fresh cursor and empty the inobt cache. */
  239. iwag->nr_recs = 0;
  240. error = xfs_inobt_cur(mp, tp, pag, XFS_BTNUM_INO, curpp, agi_bpp);
  241. if (error)
  242. return error;
  243. /* Starting at the beginning of the AG? That's easy! */
  244. if (agino == 0)
  245. return xfs_inobt_lookup(*curpp, 0, XFS_LOOKUP_GE, has_more);
  246. /*
  247. * Otherwise, we have to grab the inobt record where we left off, stuff
  248. * the record into our cache, and then see if there are more records.
  249. * We require a lookup cache of at least two elements so that the
  250. * caller doesn't have to deal with tearing down the cursor to walk the
  251. * records.
  252. */
  253. error = xfs_inobt_lookup(*curpp, agino, XFS_LOOKUP_LE, has_more);
  254. if (error)
  255. return error;
  256. /*
  257. * If the LE lookup at @agino yields no records, jump ahead to the
  258. * inobt cursor increment to see if there are more records to process.
  259. */
  260. if (!*has_more)
  261. goto out_advance;
  262. /* Get the record, should always work */
  263. irec = &iwag->recs[iwag->nr_recs];
  264. error = xfs_inobt_get_rec(*curpp, irec, has_more);
  265. if (error)
  266. return error;
  267. if (XFS_IS_CORRUPT(mp, *has_more != 1))
  268. return -EFSCORRUPTED;
  269. iwag->lastino = XFS_AGINO_TO_INO(mp, pag->pag_agno,
  270. irec->ir_startino + XFS_INODES_PER_CHUNK - 1);
  271. /*
  272. * If the LE lookup yielded an inobt record before the cursor position,
  273. * skip it and see if there's another one after it.
  274. */
  275. if (irec->ir_startino + XFS_INODES_PER_CHUNK <= agino)
  276. goto out_advance;
  277. /*
  278. * If agino fell in the middle of the inode record, make it look like
  279. * the inodes up to agino are free so that we don't return them again.
  280. */
  281. if (iwag->trim_start)
  282. xfs_iwalk_adjust_start(agino, irec);
  283. /*
  284. * The prefetch calculation is supposed to give us a large enough inobt
  285. * record cache that grab_ichunk can stage a partial first record and
  286. * the loop body can cache a record without having to check for cache
  287. * space until after it reads an inobt record.
  288. */
  289. iwag->nr_recs++;
  290. ASSERT(iwag->nr_recs < iwag->sz_recs);
  291. out_advance:
  292. return xfs_btree_increment(*curpp, 0, has_more);
  293. }
  294. /*
  295. * The inobt record cache is full, so preserve the inobt cursor state and
  296. * run callbacks on the cached inobt records. When we're done, restore the
  297. * cursor state to wherever the cursor would have been had the cache not been
  298. * full (and therefore we could've just incremented the cursor) if *@has_more
  299. * is true. On exit, *@has_more will indicate whether or not the caller should
  300. * try for more inode records.
  301. */
  302. STATIC int
  303. xfs_iwalk_run_callbacks(
  304. struct xfs_iwalk_ag *iwag,
  305. struct xfs_btree_cur **curpp,
  306. struct xfs_buf **agi_bpp,
  307. int *has_more)
  308. {
  309. struct xfs_mount *mp = iwag->mp;
  310. struct xfs_inobt_rec_incore *irec;
  311. xfs_agino_t next_agino;
  312. int error;
  313. next_agino = XFS_INO_TO_AGINO(mp, iwag->lastino) + 1;
  314. ASSERT(iwag->nr_recs > 0);
  315. /* Delete cursor but remember the last record we cached... */
  316. xfs_iwalk_del_inobt(iwag->tp, curpp, agi_bpp, 0);
  317. irec = &iwag->recs[iwag->nr_recs - 1];
  318. ASSERT(next_agino >= irec->ir_startino + XFS_INODES_PER_CHUNK);
  319. if (iwag->drop_trans) {
  320. xfs_trans_cancel(iwag->tp);
  321. iwag->tp = NULL;
  322. }
  323. error = xfs_iwalk_ag_recs(iwag);
  324. if (error)
  325. return error;
  326. /* ...empty the cache... */
  327. iwag->nr_recs = 0;
  328. if (!has_more)
  329. return 0;
  330. if (iwag->drop_trans) {
  331. error = xfs_trans_alloc_empty(mp, &iwag->tp);
  332. if (error)
  333. return error;
  334. }
  335. /* ...and recreate the cursor just past where we left off. */
  336. error = xfs_inobt_cur(mp, iwag->tp, iwag->pag, XFS_BTNUM_INO, curpp,
  337. agi_bpp);
  338. if (error)
  339. return error;
  340. return xfs_inobt_lookup(*curpp, next_agino, XFS_LOOKUP_GE, has_more);
  341. }
  342. /* Walk all inodes in a single AG, from @iwag->startino to the end of the AG. */
  343. STATIC int
  344. xfs_iwalk_ag(
  345. struct xfs_iwalk_ag *iwag)
  346. {
  347. struct xfs_mount *mp = iwag->mp;
  348. struct xfs_perag *pag = iwag->pag;
  349. struct xfs_buf *agi_bp = NULL;
  350. struct xfs_btree_cur *cur = NULL;
  351. xfs_agino_t agino;
  352. int has_more;
  353. int error = 0;
  354. /* Set up our cursor at the right place in the inode btree. */
  355. ASSERT(pag->pag_agno == XFS_INO_TO_AGNO(mp, iwag->startino));
  356. agino = XFS_INO_TO_AGINO(mp, iwag->startino);
  357. error = xfs_iwalk_ag_start(iwag, agino, &cur, &agi_bp, &has_more);
  358. while (!error && has_more) {
  359. struct xfs_inobt_rec_incore *irec;
  360. xfs_ino_t rec_fsino;
  361. cond_resched();
  362. if (xfs_pwork_want_abort(&iwag->pwork))
  363. goto out;
  364. /* Fetch the inobt record. */
  365. irec = &iwag->recs[iwag->nr_recs];
  366. error = xfs_inobt_get_rec(cur, irec, &has_more);
  367. if (error || !has_more)
  368. break;
  369. /* Make sure that we always move forward. */
  370. rec_fsino = XFS_AGINO_TO_INO(mp, pag->pag_agno, irec->ir_startino);
  371. if (iwag->lastino != NULLFSINO &&
  372. XFS_IS_CORRUPT(mp, iwag->lastino >= rec_fsino)) {
  373. error = -EFSCORRUPTED;
  374. goto out;
  375. }
  376. iwag->lastino = rec_fsino + XFS_INODES_PER_CHUNK - 1;
  377. /* No allocated inodes in this chunk; skip it. */
  378. if (iwag->skip_empty && irec->ir_freecount == irec->ir_count) {
  379. error = xfs_btree_increment(cur, 0, &has_more);
  380. if (error)
  381. break;
  382. continue;
  383. }
  384. /*
  385. * Start readahead for this inode chunk in anticipation of
  386. * walking the inodes.
  387. */
  388. if (iwag->iwalk_fn)
  389. xfs_iwalk_ichunk_ra(mp, pag, irec);
  390. /*
  391. * If there's space in the buffer for more records, increment
  392. * the btree cursor and grab more.
  393. */
  394. if (++iwag->nr_recs < iwag->sz_recs) {
  395. error = xfs_btree_increment(cur, 0, &has_more);
  396. if (error || !has_more)
  397. break;
  398. continue;
  399. }
  400. /*
  401. * Otherwise, we need to save cursor state and run the callback
  402. * function on the cached records. The run_callbacks function
  403. * is supposed to return a cursor pointing to the record where
  404. * we would be if we had been able to increment like above.
  405. */
  406. ASSERT(has_more);
  407. error = xfs_iwalk_run_callbacks(iwag, &cur, &agi_bp, &has_more);
  408. }
  409. if (iwag->nr_recs == 0 || error)
  410. goto out;
  411. /* Walk the unprocessed records in the cache. */
  412. error = xfs_iwalk_run_callbacks(iwag, &cur, &agi_bp, &has_more);
  413. out:
  414. xfs_iwalk_del_inobt(iwag->tp, &cur, &agi_bp, error);
  415. return error;
  416. }
  417. /*
  418. * We experimentally determined that the reduction in ioctl call overhead
  419. * diminishes when userspace asks for more than 2048 inodes, so we'll cap
  420. * prefetch at this point.
  421. */
  422. #define IWALK_MAX_INODE_PREFETCH (2048U)
  423. /*
  424. * Given the number of inodes to prefetch, set the number of inobt records that
  425. * we cache in memory, which controls the number of inodes we try to read
  426. * ahead. Set the maximum if @inodes == 0.
  427. */
  428. static inline unsigned int
  429. xfs_iwalk_prefetch(
  430. unsigned int inodes)
  431. {
  432. unsigned int inobt_records;
  433. /*
  434. * If the caller didn't tell us the number of inodes they wanted,
  435. * assume the maximum prefetch possible for best performance.
  436. * Otherwise, cap prefetch at that maximum so that we don't start an
  437. * absurd amount of prefetch.
  438. */
  439. if (inodes == 0)
  440. inodes = IWALK_MAX_INODE_PREFETCH;
  441. inodes = min(inodes, IWALK_MAX_INODE_PREFETCH);
  442. /* Round the inode count up to a full chunk. */
  443. inodes = round_up(inodes, XFS_INODES_PER_CHUNK);
  444. /*
  445. * In order to convert the number of inodes to prefetch into an
  446. * estimate of the number of inobt records to cache, we require a
  447. * conversion factor that reflects our expectations of the average
  448. * loading factor of an inode chunk. Based on data gathered, most
  449. * (but not all) filesystems manage to keep the inode chunks totally
  450. * full, so we'll underestimate slightly so that our readahead will
  451. * still deliver the performance we want on aging filesystems:
  452. *
  453. * inobt = inodes / (INODES_PER_CHUNK * (4 / 5));
  454. *
  455. * The funny math is to avoid integer division.
  456. */
  457. inobt_records = (inodes * 5) / (4 * XFS_INODES_PER_CHUNK);
  458. /*
  459. * Allocate enough space to prefetch at least two inobt records so that
  460. * we can cache both the record where the iwalk started and the next
  461. * record. This simplifies the AG inode walk loop setup code.
  462. */
  463. return max(inobt_records, 2U);
  464. }
  465. /*
  466. * Walk all inodes in the filesystem starting from @startino. The @iwalk_fn
  467. * will be called for each allocated inode, being passed the inode's number and
  468. * @data. @max_prefetch controls how many inobt records' worth of inodes we
  469. * try to readahead.
  470. */
  471. int
  472. xfs_iwalk(
  473. struct xfs_mount *mp,
  474. struct xfs_trans *tp,
  475. xfs_ino_t startino,
  476. unsigned int flags,
  477. xfs_iwalk_fn iwalk_fn,
  478. unsigned int inode_records,
  479. void *data)
  480. {
  481. struct xfs_iwalk_ag iwag = {
  482. .mp = mp,
  483. .tp = tp,
  484. .iwalk_fn = iwalk_fn,
  485. .data = data,
  486. .startino = startino,
  487. .sz_recs = xfs_iwalk_prefetch(inode_records),
  488. .trim_start = 1,
  489. .skip_empty = 1,
  490. .pwork = XFS_PWORK_SINGLE_THREADED,
  491. .lastino = NULLFSINO,
  492. };
  493. struct xfs_perag *pag;
  494. xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, startino);
  495. int error;
  496. ASSERT(agno < mp->m_sb.sb_agcount);
  497. ASSERT(!(flags & ~XFS_IWALK_FLAGS_ALL));
  498. error = xfs_iwalk_alloc(&iwag);
  499. if (error)
  500. return error;
  501. for_each_perag_from(mp, agno, pag) {
  502. iwag.pag = pag;
  503. error = xfs_iwalk_ag(&iwag);
  504. if (error)
  505. break;
  506. iwag.startino = XFS_AGINO_TO_INO(mp, agno + 1, 0);
  507. if (flags & XFS_INOBT_WALK_SAME_AG)
  508. break;
  509. iwag.pag = NULL;
  510. }
  511. if (iwag.pag)
  512. xfs_perag_put(pag);
  513. xfs_iwalk_free(&iwag);
  514. return error;
  515. }
  516. /* Run per-thread iwalk work. */
  517. static int
  518. xfs_iwalk_ag_work(
  519. struct xfs_mount *mp,
  520. struct xfs_pwork *pwork)
  521. {
  522. struct xfs_iwalk_ag *iwag;
  523. int error = 0;
  524. iwag = container_of(pwork, struct xfs_iwalk_ag, pwork);
  525. if (xfs_pwork_want_abort(pwork))
  526. goto out;
  527. error = xfs_iwalk_alloc(iwag);
  528. if (error)
  529. goto out;
  530. /*
  531. * Grab an empty transaction so that we can use its recursive buffer
  532. * locking abilities to detect cycles in the inobt without deadlocking.
  533. */
  534. error = xfs_trans_alloc_empty(mp, &iwag->tp);
  535. if (error)
  536. goto out;
  537. iwag->drop_trans = 1;
  538. error = xfs_iwalk_ag(iwag);
  539. if (iwag->tp)
  540. xfs_trans_cancel(iwag->tp);
  541. xfs_iwalk_free(iwag);
  542. out:
  543. xfs_perag_put(iwag->pag);
  544. kmem_free(iwag);
  545. return error;
  546. }
  547. /*
  548. * Walk all the inodes in the filesystem using multiple threads to process each
  549. * AG.
  550. */
  551. int
  552. xfs_iwalk_threaded(
  553. struct xfs_mount *mp,
  554. xfs_ino_t startino,
  555. unsigned int flags,
  556. xfs_iwalk_fn iwalk_fn,
  557. unsigned int inode_records,
  558. bool polled,
  559. void *data)
  560. {
  561. struct xfs_pwork_ctl pctl;
  562. struct xfs_perag *pag;
  563. xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, startino);
  564. int error;
  565. ASSERT(agno < mp->m_sb.sb_agcount);
  566. ASSERT(!(flags & ~XFS_IWALK_FLAGS_ALL));
  567. error = xfs_pwork_init(mp, &pctl, xfs_iwalk_ag_work, "xfs_iwalk");
  568. if (error)
  569. return error;
  570. for_each_perag_from(mp, agno, pag) {
  571. struct xfs_iwalk_ag *iwag;
  572. if (xfs_pwork_ctl_want_abort(&pctl))
  573. break;
  574. iwag = kmem_zalloc(sizeof(struct xfs_iwalk_ag), 0);
  575. iwag->mp = mp;
  576. /*
  577. * perag is being handed off to async work, so take another
  578. * reference for the async work to release.
  579. */
  580. atomic_inc(&pag->pag_ref);
  581. iwag->pag = pag;
  582. iwag->iwalk_fn = iwalk_fn;
  583. iwag->data = data;
  584. iwag->startino = startino;
  585. iwag->sz_recs = xfs_iwalk_prefetch(inode_records);
  586. iwag->lastino = NULLFSINO;
  587. xfs_pwork_queue(&pctl, &iwag->pwork);
  588. startino = XFS_AGINO_TO_INO(mp, pag->pag_agno + 1, 0);
  589. if (flags & XFS_INOBT_WALK_SAME_AG)
  590. break;
  591. }
  592. if (pag)
  593. xfs_perag_put(pag);
  594. if (polled)
  595. xfs_pwork_poll(&pctl);
  596. return xfs_pwork_destroy(&pctl);
  597. }
  598. /*
  599. * Allow callers to cache up to a page's worth of inobt records. This reflects
  600. * the existing inumbers prefetching behavior. Since the inobt walk does not
  601. * itself do anything with the inobt records, we can set a fairly high limit
  602. * here.
  603. */
  604. #define MAX_INOBT_WALK_PREFETCH \
  605. (PAGE_SIZE / sizeof(struct xfs_inobt_rec_incore))
  606. /*
  607. * Given the number of records that the user wanted, set the number of inobt
  608. * records that we buffer in memory. Set the maximum if @inobt_records == 0.
  609. */
  610. static inline unsigned int
  611. xfs_inobt_walk_prefetch(
  612. unsigned int inobt_records)
  613. {
  614. /*
  615. * If the caller didn't tell us the number of inobt records they
  616. * wanted, assume the maximum prefetch possible for best performance.
  617. */
  618. if (inobt_records == 0)
  619. inobt_records = MAX_INOBT_WALK_PREFETCH;
  620. /*
  621. * Allocate enough space to prefetch at least two inobt records so that
  622. * we can cache both the record where the iwalk started and the next
  623. * record. This simplifies the AG inode walk loop setup code.
  624. */
  625. inobt_records = max(inobt_records, 2U);
  626. /*
  627. * Cap prefetch at that maximum so that we don't use an absurd amount
  628. * of memory.
  629. */
  630. return min_t(unsigned int, inobt_records, MAX_INOBT_WALK_PREFETCH);
  631. }
  632. /*
  633. * Walk all inode btree records in the filesystem starting from @startino. The
  634. * @inobt_walk_fn will be called for each btree record, being passed the incore
  635. * record and @data. @max_prefetch controls how many inobt records we try to
  636. * cache ahead of time.
  637. */
  638. int
  639. xfs_inobt_walk(
  640. struct xfs_mount *mp,
  641. struct xfs_trans *tp,
  642. xfs_ino_t startino,
  643. unsigned int flags,
  644. xfs_inobt_walk_fn inobt_walk_fn,
  645. unsigned int inobt_records,
  646. void *data)
  647. {
  648. struct xfs_iwalk_ag iwag = {
  649. .mp = mp,
  650. .tp = tp,
  651. .inobt_walk_fn = inobt_walk_fn,
  652. .data = data,
  653. .startino = startino,
  654. .sz_recs = xfs_inobt_walk_prefetch(inobt_records),
  655. .pwork = XFS_PWORK_SINGLE_THREADED,
  656. .lastino = NULLFSINO,
  657. };
  658. struct xfs_perag *pag;
  659. xfs_agnumber_t agno = XFS_INO_TO_AGNO(mp, startino);
  660. int error;
  661. ASSERT(agno < mp->m_sb.sb_agcount);
  662. ASSERT(!(flags & ~XFS_INOBT_WALK_FLAGS_ALL));
  663. error = xfs_iwalk_alloc(&iwag);
  664. if (error)
  665. return error;
  666. for_each_perag_from(mp, agno, pag) {
  667. iwag.pag = pag;
  668. error = xfs_iwalk_ag(&iwag);
  669. if (error)
  670. break;
  671. iwag.startino = XFS_AGINO_TO_INO(mp, pag->pag_agno + 1, 0);
  672. if (flags & XFS_INOBT_WALK_SAME_AG)
  673. break;
  674. iwag.pag = NULL;
  675. }
  676. if (iwag.pag)
  677. xfs_perag_put(pag);
  678. xfs_iwalk_free(&iwag);
  679. return error;
  680. }