bmap_repair.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2018-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_defer.h"
  13. #include "xfs_btree.h"
  14. #include "xfs_btree_staging.h"
  15. #include "xfs_bit.h"
  16. #include "xfs_log_format.h"
  17. #include "xfs_trans.h"
  18. #include "xfs_sb.h"
  19. #include "xfs_inode.h"
  20. #include "xfs_inode_fork.h"
  21. #include "xfs_alloc.h"
  22. #include "xfs_rtalloc.h"
  23. #include "xfs_bmap.h"
  24. #include "xfs_bmap_util.h"
  25. #include "xfs_bmap_btree.h"
  26. #include "xfs_rmap.h"
  27. #include "xfs_rmap_btree.h"
  28. #include "xfs_refcount.h"
  29. #include "xfs_quota.h"
  30. #include "xfs_ialloc.h"
  31. #include "xfs_ag.h"
  32. #include "xfs_reflink.h"
  33. #include "scrub/xfs_scrub.h"
  34. #include "scrub/scrub.h"
  35. #include "scrub/common.h"
  36. #include "scrub/btree.h"
  37. #include "scrub/trace.h"
  38. #include "scrub/repair.h"
  39. #include "scrub/bitmap.h"
  40. #include "scrub/fsb_bitmap.h"
  41. #include "scrub/xfile.h"
  42. #include "scrub/xfarray.h"
  43. #include "scrub/newbt.h"
  44. #include "scrub/reap.h"
  45. /*
  46. * Inode Fork Block Mapping (BMBT) Repair
  47. * ======================================
  48. *
  49. * Gather all the rmap records for the inode and fork we're fixing, reset the
  50. * incore fork, then recreate the btree.
  51. */
  52. enum reflink_scan_state {
  53. RLS_IRRELEVANT = -1, /* not applicable to this file */
  54. RLS_UNKNOWN, /* shared extent scans required */
  55. RLS_SET_IFLAG, /* iflag must be set */
  56. };
  57. struct xrep_bmap {
  58. /* Old bmbt blocks */
  59. struct xfsb_bitmap old_bmbt_blocks;
  60. /* New fork. */
  61. struct xrep_newbt new_bmapbt;
  62. /* List of new bmap records. */
  63. struct xfarray *bmap_records;
  64. struct xfs_scrub *sc;
  65. /* How many blocks did we find allocated to this file? */
  66. xfs_rfsblock_t nblocks;
  67. /* How many bmbt blocks did we find for this fork? */
  68. xfs_rfsblock_t old_bmbt_block_count;
  69. /* get_records()'s position in the free space record array. */
  70. xfarray_idx_t array_cur;
  71. /* How many real (non-hole, non-delalloc) mappings do we have? */
  72. uint64_t real_mappings;
  73. /* Which fork are we fixing? */
  74. int whichfork;
  75. /* What d the REFLINK flag be set when the repair is over? */
  76. enum reflink_scan_state reflink_scan;
  77. /* Do we allow unwritten extents? */
  78. bool allow_unwritten;
  79. };
  80. /* Is this space extent shared? Flag the inode if it is. */
  81. STATIC int
  82. xrep_bmap_discover_shared(
  83. struct xrep_bmap *rb,
  84. xfs_fsblock_t startblock,
  85. xfs_filblks_t blockcount)
  86. {
  87. struct xfs_scrub *sc = rb->sc;
  88. xfs_agblock_t agbno;
  89. xfs_agblock_t fbno;
  90. xfs_extlen_t flen;
  91. int error;
  92. agbno = XFS_FSB_TO_AGBNO(sc->mp, startblock);
  93. error = xfs_refcount_find_shared(sc->sa.refc_cur, agbno, blockcount,
  94. &fbno, &flen, false);
  95. if (error)
  96. return error;
  97. if (fbno != NULLAGBLOCK)
  98. rb->reflink_scan = RLS_SET_IFLAG;
  99. return 0;
  100. }
  101. /* Remember this reverse-mapping as a series of bmap records. */
  102. STATIC int
  103. xrep_bmap_from_rmap(
  104. struct xrep_bmap *rb,
  105. xfs_fileoff_t startoff,
  106. xfs_fsblock_t startblock,
  107. xfs_filblks_t blockcount,
  108. bool unwritten)
  109. {
  110. struct xfs_bmbt_irec irec = {
  111. .br_startoff = startoff,
  112. .br_startblock = startblock,
  113. .br_state = unwritten ? XFS_EXT_UNWRITTEN : XFS_EXT_NORM,
  114. };
  115. struct xfs_bmbt_rec rbe;
  116. struct xfs_scrub *sc = rb->sc;
  117. int error = 0;
  118. /*
  119. * If we're repairing the data fork of a non-reflinked regular file on
  120. * a reflink filesystem, we need to figure out if this space extent is
  121. * shared.
  122. */
  123. if (rb->reflink_scan == RLS_UNKNOWN && !unwritten) {
  124. error = xrep_bmap_discover_shared(rb, startblock, blockcount);
  125. if (error)
  126. return error;
  127. }
  128. do {
  129. xfs_failaddr_t fa;
  130. irec.br_blockcount = min_t(xfs_filblks_t, blockcount,
  131. XFS_MAX_BMBT_EXTLEN);
  132. fa = xfs_bmap_validate_extent(sc->ip, rb->whichfork, &irec);
  133. if (fa)
  134. return -EFSCORRUPTED;
  135. xfs_bmbt_disk_set_all(&rbe, &irec);
  136. trace_xrep_bmap_found(sc->ip, rb->whichfork, &irec);
  137. if (xchk_should_terminate(sc, &error))
  138. return error;
  139. error = xfarray_append(rb->bmap_records, &rbe);
  140. if (error)
  141. return error;
  142. rb->real_mappings++;
  143. irec.br_startblock += irec.br_blockcount;
  144. irec.br_startoff += irec.br_blockcount;
  145. blockcount -= irec.br_blockcount;
  146. } while (blockcount > 0);
  147. return 0;
  148. }
  149. /* Check for any obvious errors or conflicts in the file mapping. */
  150. STATIC int
  151. xrep_bmap_check_fork_rmap(
  152. struct xrep_bmap *rb,
  153. struct xfs_btree_cur *cur,
  154. const struct xfs_rmap_irec *rec)
  155. {
  156. struct xfs_scrub *sc = rb->sc;
  157. enum xbtree_recpacking outcome;
  158. int error;
  159. /*
  160. * Data extents for rt files are never stored on the data device, but
  161. * everything else (xattrs, bmbt blocks) can be.
  162. */
  163. if (XFS_IS_REALTIME_INODE(sc->ip) &&
  164. !(rec->rm_flags & (XFS_RMAP_ATTR_FORK | XFS_RMAP_BMBT_BLOCK)))
  165. return -EFSCORRUPTED;
  166. /* Check that this is within the AG. */
  167. if (!xfs_verify_agbext(cur->bc_ag.pag, rec->rm_startblock,
  168. rec->rm_blockcount))
  169. return -EFSCORRUPTED;
  170. /* Check the file offset range. */
  171. if (!(rec->rm_flags & XFS_RMAP_BMBT_BLOCK) &&
  172. !xfs_verify_fileext(sc->mp, rec->rm_offset, rec->rm_blockcount))
  173. return -EFSCORRUPTED;
  174. /* No contradictory flags. */
  175. if ((rec->rm_flags & (XFS_RMAP_ATTR_FORK | XFS_RMAP_BMBT_BLOCK)) &&
  176. (rec->rm_flags & XFS_RMAP_UNWRITTEN))
  177. return -EFSCORRUPTED;
  178. /* Make sure this isn't free space. */
  179. error = xfs_alloc_has_records(sc->sa.bno_cur, rec->rm_startblock,
  180. rec->rm_blockcount, &outcome);
  181. if (error)
  182. return error;
  183. if (outcome != XBTREE_RECPACKING_EMPTY)
  184. return -EFSCORRUPTED;
  185. /* Must not be an inode chunk. */
  186. error = xfs_ialloc_has_inodes_at_extent(sc->sa.ino_cur,
  187. rec->rm_startblock, rec->rm_blockcount, &outcome);
  188. if (error)
  189. return error;
  190. if (outcome != XBTREE_RECPACKING_EMPTY)
  191. return -EFSCORRUPTED;
  192. return 0;
  193. }
  194. /* Record extents that belong to this inode's fork. */
  195. STATIC int
  196. xrep_bmap_walk_rmap(
  197. struct xfs_btree_cur *cur,
  198. const struct xfs_rmap_irec *rec,
  199. void *priv)
  200. {
  201. struct xrep_bmap *rb = priv;
  202. struct xfs_mount *mp = cur->bc_mp;
  203. xfs_fsblock_t fsbno;
  204. int error = 0;
  205. if (xchk_should_terminate(rb->sc, &error))
  206. return error;
  207. if (rec->rm_owner != rb->sc->ip->i_ino)
  208. return 0;
  209. error = xrep_bmap_check_fork_rmap(rb, cur, rec);
  210. if (error)
  211. return error;
  212. /*
  213. * Record all blocks allocated to this file even if the extent isn't
  214. * for the fork we're rebuilding so that we can reset di_nblocks later.
  215. */
  216. rb->nblocks += rec->rm_blockcount;
  217. /* If this rmap isn't for the fork we want, we're done. */
  218. if (rb->whichfork == XFS_DATA_FORK &&
  219. (rec->rm_flags & XFS_RMAP_ATTR_FORK))
  220. return 0;
  221. if (rb->whichfork == XFS_ATTR_FORK &&
  222. !(rec->rm_flags & XFS_RMAP_ATTR_FORK))
  223. return 0;
  224. /* Reject unwritten extents if we don't allow those. */
  225. if ((rec->rm_flags & XFS_RMAP_UNWRITTEN) && !rb->allow_unwritten)
  226. return -EFSCORRUPTED;
  227. fsbno = XFS_AGB_TO_FSB(mp, cur->bc_ag.pag->pag_agno,
  228. rec->rm_startblock);
  229. if (rec->rm_flags & XFS_RMAP_BMBT_BLOCK) {
  230. rb->old_bmbt_block_count += rec->rm_blockcount;
  231. return xfsb_bitmap_set(&rb->old_bmbt_blocks, fsbno,
  232. rec->rm_blockcount);
  233. }
  234. return xrep_bmap_from_rmap(rb, rec->rm_offset, fsbno,
  235. rec->rm_blockcount,
  236. rec->rm_flags & XFS_RMAP_UNWRITTEN);
  237. }
  238. /*
  239. * Compare two block mapping records. We want to sort in order of increasing
  240. * file offset.
  241. */
  242. static int
  243. xrep_bmap_extent_cmp(
  244. const void *a,
  245. const void *b)
  246. {
  247. const struct xfs_bmbt_rec *ba = a;
  248. const struct xfs_bmbt_rec *bb = b;
  249. xfs_fileoff_t ao = xfs_bmbt_disk_get_startoff(ba);
  250. xfs_fileoff_t bo = xfs_bmbt_disk_get_startoff(bb);
  251. if (ao > bo)
  252. return 1;
  253. else if (ao < bo)
  254. return -1;
  255. return 0;
  256. }
  257. /*
  258. * Sort the bmap extents by fork offset or else the records will be in the
  259. * wrong order. Ensure there are no overlaps in the file offset ranges.
  260. */
  261. STATIC int
  262. xrep_bmap_sort_records(
  263. struct xrep_bmap *rb)
  264. {
  265. struct xfs_bmbt_irec irec;
  266. xfs_fileoff_t next_off = 0;
  267. xfarray_idx_t array_cur;
  268. int error;
  269. error = xfarray_sort(rb->bmap_records, xrep_bmap_extent_cmp,
  270. XFARRAY_SORT_KILLABLE);
  271. if (error)
  272. return error;
  273. foreach_xfarray_idx(rb->bmap_records, array_cur) {
  274. struct xfs_bmbt_rec rec;
  275. if (xchk_should_terminate(rb->sc, &error))
  276. return error;
  277. error = xfarray_load(rb->bmap_records, array_cur, &rec);
  278. if (error)
  279. return error;
  280. xfs_bmbt_disk_get_all(&rec, &irec);
  281. if (irec.br_startoff < next_off)
  282. return -EFSCORRUPTED;
  283. next_off = irec.br_startoff + irec.br_blockcount;
  284. }
  285. return 0;
  286. }
  287. /* Scan one AG for reverse mappings that we can turn into extent maps. */
  288. STATIC int
  289. xrep_bmap_scan_ag(
  290. struct xrep_bmap *rb,
  291. struct xfs_perag *pag)
  292. {
  293. struct xfs_scrub *sc = rb->sc;
  294. int error;
  295. error = xrep_ag_init(sc, pag, &sc->sa);
  296. if (error)
  297. return error;
  298. error = xfs_rmap_query_all(sc->sa.rmap_cur, xrep_bmap_walk_rmap, rb);
  299. xchk_ag_free(sc, &sc->sa);
  300. return error;
  301. }
  302. /* Find the delalloc extents from the old incore extent tree. */
  303. STATIC int
  304. xrep_bmap_find_delalloc(
  305. struct xrep_bmap *rb)
  306. {
  307. struct xfs_bmbt_irec irec;
  308. struct xfs_iext_cursor icur;
  309. struct xfs_bmbt_rec rbe;
  310. struct xfs_inode *ip = rb->sc->ip;
  311. struct xfs_ifork *ifp = xfs_ifork_ptr(ip, rb->whichfork);
  312. int error = 0;
  313. /*
  314. * Skip this scan if we don't expect to find delayed allocation
  315. * reservations in this fork.
  316. */
  317. if (rb->whichfork == XFS_ATTR_FORK || ip->i_delayed_blks == 0)
  318. return 0;
  319. for_each_xfs_iext(ifp, &icur, &irec) {
  320. if (!isnullstartblock(irec.br_startblock))
  321. continue;
  322. xfs_bmbt_disk_set_all(&rbe, &irec);
  323. trace_xrep_bmap_found(ip, rb->whichfork, &irec);
  324. if (xchk_should_terminate(rb->sc, &error))
  325. return error;
  326. error = xfarray_append(rb->bmap_records, &rbe);
  327. if (error)
  328. return error;
  329. }
  330. return 0;
  331. }
  332. /*
  333. * Collect block mappings for this fork of this inode and decide if we have
  334. * enough space to rebuild. Caller is responsible for cleaning up the list if
  335. * anything goes wrong.
  336. */
  337. STATIC int
  338. xrep_bmap_find_mappings(
  339. struct xrep_bmap *rb)
  340. {
  341. struct xfs_scrub *sc = rb->sc;
  342. struct xfs_perag *pag;
  343. xfs_agnumber_t agno;
  344. int error = 0;
  345. /* Iterate the rmaps for extents. */
  346. for_each_perag(sc->mp, agno, pag) {
  347. error = xrep_bmap_scan_ag(rb, pag);
  348. if (error) {
  349. xfs_perag_rele(pag);
  350. return error;
  351. }
  352. }
  353. return xrep_bmap_find_delalloc(rb);
  354. }
  355. /* Retrieve real extent mappings for bulk loading the bmap btree. */
  356. STATIC int
  357. xrep_bmap_get_records(
  358. struct xfs_btree_cur *cur,
  359. unsigned int idx,
  360. struct xfs_btree_block *block,
  361. unsigned int nr_wanted,
  362. void *priv)
  363. {
  364. struct xfs_bmbt_rec rec;
  365. struct xfs_bmbt_irec *irec = &cur->bc_rec.b;
  366. struct xrep_bmap *rb = priv;
  367. union xfs_btree_rec *block_rec;
  368. unsigned int loaded;
  369. int error;
  370. for (loaded = 0; loaded < nr_wanted; loaded++, idx++) {
  371. do {
  372. error = xfarray_load(rb->bmap_records, rb->array_cur++,
  373. &rec);
  374. if (error)
  375. return error;
  376. xfs_bmbt_disk_get_all(&rec, irec);
  377. } while (isnullstartblock(irec->br_startblock));
  378. block_rec = xfs_btree_rec_addr(cur, idx, block);
  379. cur->bc_ops->init_rec_from_cur(cur, block_rec);
  380. }
  381. return loaded;
  382. }
  383. /* Feed one of the new btree blocks to the bulk loader. */
  384. STATIC int
  385. xrep_bmap_claim_block(
  386. struct xfs_btree_cur *cur,
  387. union xfs_btree_ptr *ptr,
  388. void *priv)
  389. {
  390. struct xrep_bmap *rb = priv;
  391. return xrep_newbt_claim_block(cur, &rb->new_bmapbt, ptr);
  392. }
  393. /* Figure out how much space we need to create the incore btree root block. */
  394. STATIC size_t
  395. xrep_bmap_iroot_size(
  396. struct xfs_btree_cur *cur,
  397. unsigned int level,
  398. unsigned int nr_this_level,
  399. void *priv)
  400. {
  401. ASSERT(level > 0);
  402. return xfs_bmap_broot_space_calc(cur->bc_mp, nr_this_level);
  403. }
  404. /* Update the inode counters. */
  405. STATIC int
  406. xrep_bmap_reset_counters(
  407. struct xrep_bmap *rb)
  408. {
  409. struct xfs_scrub *sc = rb->sc;
  410. struct xbtree_ifakeroot *ifake = &rb->new_bmapbt.ifake;
  411. int64_t delta;
  412. if (rb->reflink_scan == RLS_SET_IFLAG)
  413. sc->ip->i_diflags2 |= XFS_DIFLAG2_REFLINK;
  414. /*
  415. * Update the inode block counts to reflect the extents we found in the
  416. * rmapbt.
  417. */
  418. delta = ifake->if_blocks - rb->old_bmbt_block_count;
  419. sc->ip->i_nblocks = rb->nblocks + delta;
  420. xfs_trans_log_inode(sc->tp, sc->ip, XFS_ILOG_CORE);
  421. /*
  422. * Adjust the quota counts by the difference in size between the old
  423. * and new bmbt.
  424. */
  425. xfs_trans_mod_dquot_byino(sc->tp, sc->ip, XFS_TRANS_DQ_BCOUNT, delta);
  426. return 0;
  427. }
  428. /*
  429. * Create a new iext tree and load it with block mappings. If the inode is
  430. * in extents format, that's all we need to do to commit the new mappings.
  431. * If it is in btree format, this takes care of preloading the incore tree.
  432. */
  433. STATIC int
  434. xrep_bmap_extents_load(
  435. struct xrep_bmap *rb)
  436. {
  437. struct xfs_iext_cursor icur;
  438. struct xfs_bmbt_irec irec;
  439. struct xfs_ifork *ifp = rb->new_bmapbt.ifake.if_fork;
  440. xfarray_idx_t array_cur;
  441. int error;
  442. ASSERT(ifp->if_bytes == 0);
  443. /* Add all the mappings (incl. delalloc) to the incore extent tree. */
  444. xfs_iext_first(ifp, &icur);
  445. foreach_xfarray_idx(rb->bmap_records, array_cur) {
  446. struct xfs_bmbt_rec rec;
  447. error = xfarray_load(rb->bmap_records, array_cur, &rec);
  448. if (error)
  449. return error;
  450. xfs_bmbt_disk_get_all(&rec, &irec);
  451. xfs_iext_insert_raw(ifp, &icur, &irec);
  452. if (!isnullstartblock(irec.br_startblock))
  453. ifp->if_nextents++;
  454. xfs_iext_next(ifp, &icur);
  455. }
  456. return xrep_ino_ensure_extent_count(rb->sc, rb->whichfork,
  457. ifp->if_nextents);
  458. }
  459. /*
  460. * Reserve new btree blocks, bulk load the bmap records into the ondisk btree,
  461. * and load the incore extent tree.
  462. */
  463. STATIC int
  464. xrep_bmap_btree_load(
  465. struct xrep_bmap *rb,
  466. struct xfs_btree_cur *bmap_cur)
  467. {
  468. struct xfs_scrub *sc = rb->sc;
  469. int error;
  470. /* Compute how many blocks we'll need. */
  471. error = xfs_btree_bload_compute_geometry(bmap_cur,
  472. &rb->new_bmapbt.bload, rb->real_mappings);
  473. if (error)
  474. return error;
  475. /* Last chance to abort before we start committing fixes. */
  476. if (xchk_should_terminate(sc, &error))
  477. return error;
  478. /*
  479. * Guess how many blocks we're going to need to rebuild an entire bmap
  480. * from the number of extents we found, and pump up our transaction to
  481. * have sufficient block reservation. We're allowed to exceed file
  482. * quota to repair inconsistent metadata.
  483. */
  484. error = xfs_trans_reserve_more_inode(sc->tp, sc->ip,
  485. rb->new_bmapbt.bload.nr_blocks, 0, true);
  486. if (error)
  487. return error;
  488. /* Reserve the space we'll need for the new btree. */
  489. error = xrep_newbt_alloc_blocks(&rb->new_bmapbt,
  490. rb->new_bmapbt.bload.nr_blocks);
  491. if (error)
  492. return error;
  493. /* Add all observed bmap records. */
  494. rb->array_cur = XFARRAY_CURSOR_INIT;
  495. error = xfs_btree_bload(bmap_cur, &rb->new_bmapbt.bload, rb);
  496. if (error)
  497. return error;
  498. /*
  499. * Load the new bmap records into the new incore extent tree to
  500. * preserve delalloc reservations for regular files. The directory
  501. * code loads the extent tree during xfs_dir_open and assumes
  502. * thereafter that it remains loaded, so we must not violate that
  503. * assumption.
  504. */
  505. return xrep_bmap_extents_load(rb);
  506. }
  507. /*
  508. * Use the collected bmap information to stage a new bmap fork. If this is
  509. * successful we'll return with the new fork information logged to the repair
  510. * transaction but not yet committed. The caller must ensure that the inode
  511. * is joined to the transaction; the inode will be joined to a clean
  512. * transaction when the function returns.
  513. */
  514. STATIC int
  515. xrep_bmap_build_new_fork(
  516. struct xrep_bmap *rb)
  517. {
  518. struct xfs_owner_info oinfo;
  519. struct xfs_scrub *sc = rb->sc;
  520. struct xfs_btree_cur *bmap_cur;
  521. struct xbtree_ifakeroot *ifake = &rb->new_bmapbt.ifake;
  522. int error;
  523. error = xrep_bmap_sort_records(rb);
  524. if (error)
  525. return error;
  526. /*
  527. * Prepare to construct the new fork by initializing the new btree
  528. * structure and creating a fake ifork in the ifakeroot structure.
  529. */
  530. xfs_rmap_ino_bmbt_owner(&oinfo, sc->ip->i_ino, rb->whichfork);
  531. error = xrep_newbt_init_inode(&rb->new_bmapbt, sc, rb->whichfork,
  532. &oinfo);
  533. if (error)
  534. return error;
  535. rb->new_bmapbt.bload.get_records = xrep_bmap_get_records;
  536. rb->new_bmapbt.bload.claim_block = xrep_bmap_claim_block;
  537. rb->new_bmapbt.bload.iroot_size = xrep_bmap_iroot_size;
  538. /*
  539. * Allocate a new bmap btree cursor for reloading an inode block mapping
  540. * data structure.
  541. */
  542. bmap_cur = xfs_bmbt_init_cursor(sc->mp, NULL, sc->ip, XFS_STAGING_FORK);
  543. xfs_btree_stage_ifakeroot(bmap_cur, ifake);
  544. /*
  545. * Figure out the size and format of the new fork, then fill it with
  546. * all the bmap records we've found. Join the inode to the transaction
  547. * so that we can roll the transaction while holding the inode locked.
  548. */
  549. if (rb->real_mappings <= XFS_IFORK_MAXEXT(sc->ip, rb->whichfork)) {
  550. ifake->if_fork->if_format = XFS_DINODE_FMT_EXTENTS;
  551. error = xrep_bmap_extents_load(rb);
  552. } else {
  553. ifake->if_fork->if_format = XFS_DINODE_FMT_BTREE;
  554. error = xrep_bmap_btree_load(rb, bmap_cur);
  555. }
  556. if (error)
  557. goto err_cur;
  558. /*
  559. * Install the new fork in the inode. After this point the old mapping
  560. * data are no longer accessible and the new tree is live. We delete
  561. * the cursor immediately after committing the staged root because the
  562. * staged fork might be in extents format.
  563. */
  564. xfs_bmbt_commit_staged_btree(bmap_cur, sc->tp, rb->whichfork);
  565. xfs_btree_del_cursor(bmap_cur, 0);
  566. /* Reset the inode counters now that we've changed the fork. */
  567. error = xrep_bmap_reset_counters(rb);
  568. if (error)
  569. goto err_newbt;
  570. /* Dispose of any unused blocks and the accounting information. */
  571. error = xrep_newbt_commit(&rb->new_bmapbt);
  572. if (error)
  573. return error;
  574. return xrep_roll_trans(sc);
  575. err_cur:
  576. if (bmap_cur)
  577. xfs_btree_del_cursor(bmap_cur, error);
  578. err_newbt:
  579. xrep_newbt_cancel(&rb->new_bmapbt);
  580. return error;
  581. }
  582. /*
  583. * Now that we've logged the new inode btree, invalidate all of the old blocks
  584. * and free them, if there were any.
  585. */
  586. STATIC int
  587. xrep_bmap_remove_old_tree(
  588. struct xrep_bmap *rb)
  589. {
  590. struct xfs_scrub *sc = rb->sc;
  591. struct xfs_owner_info oinfo;
  592. /* Free the old bmbt blocks if they're not in use. */
  593. xfs_rmap_ino_bmbt_owner(&oinfo, sc->ip->i_ino, rb->whichfork);
  594. return xrep_reap_fsblocks(sc, &rb->old_bmbt_blocks, &oinfo);
  595. }
  596. /* Check for garbage inputs. Returns -ECANCELED if there's nothing to do. */
  597. STATIC int
  598. xrep_bmap_check_inputs(
  599. struct xfs_scrub *sc,
  600. int whichfork)
  601. {
  602. struct xfs_ifork *ifp = xfs_ifork_ptr(sc->ip, whichfork);
  603. ASSERT(whichfork == XFS_DATA_FORK || whichfork == XFS_ATTR_FORK);
  604. if (!xfs_has_rmapbt(sc->mp))
  605. return -EOPNOTSUPP;
  606. /* No fork means nothing to rebuild. */
  607. if (!ifp)
  608. return -ECANCELED;
  609. /*
  610. * We only know how to repair extent mappings, which is to say that we
  611. * only support extents and btree fork format. Repairs to a local
  612. * format fork require a higher level repair function, so we do not
  613. * have any work to do here.
  614. */
  615. switch (ifp->if_format) {
  616. case XFS_DINODE_FMT_DEV:
  617. case XFS_DINODE_FMT_LOCAL:
  618. case XFS_DINODE_FMT_UUID:
  619. return -ECANCELED;
  620. case XFS_DINODE_FMT_EXTENTS:
  621. case XFS_DINODE_FMT_BTREE:
  622. break;
  623. default:
  624. return -EFSCORRUPTED;
  625. }
  626. if (whichfork == XFS_ATTR_FORK)
  627. return 0;
  628. /* Only files, symlinks, and directories get to have data forks. */
  629. switch (VFS_I(sc->ip)->i_mode & S_IFMT) {
  630. case S_IFREG:
  631. case S_IFDIR:
  632. case S_IFLNK:
  633. /* ok */
  634. break;
  635. default:
  636. return -EINVAL;
  637. }
  638. /* Don't know how to rebuild realtime data forks. */
  639. if (XFS_IS_REALTIME_INODE(sc->ip))
  640. return -EOPNOTSUPP;
  641. return 0;
  642. }
  643. /* Set up the initial state of the reflink scan. */
  644. static inline enum reflink_scan_state
  645. xrep_bmap_init_reflink_scan(
  646. struct xfs_scrub *sc,
  647. int whichfork)
  648. {
  649. /* cannot share on non-reflink filesystem */
  650. if (!xfs_has_reflink(sc->mp))
  651. return RLS_IRRELEVANT;
  652. /* preserve flag if it's already set */
  653. if (xfs_is_reflink_inode(sc->ip))
  654. return RLS_SET_IFLAG;
  655. /* can only share regular files */
  656. if (!S_ISREG(VFS_I(sc->ip)->i_mode))
  657. return RLS_IRRELEVANT;
  658. /* cannot share attr fork extents */
  659. if (whichfork != XFS_DATA_FORK)
  660. return RLS_IRRELEVANT;
  661. /* cannot share realtime extents */
  662. if (XFS_IS_REALTIME_INODE(sc->ip))
  663. return RLS_IRRELEVANT;
  664. return RLS_UNKNOWN;
  665. }
  666. /* Repair an inode fork. */
  667. int
  668. xrep_bmap(
  669. struct xfs_scrub *sc,
  670. int whichfork,
  671. bool allow_unwritten)
  672. {
  673. struct xrep_bmap *rb;
  674. char *descr;
  675. xfs_extnum_t max_bmbt_recs;
  676. bool large_extcount;
  677. int error = 0;
  678. error = xrep_bmap_check_inputs(sc, whichfork);
  679. if (error == -ECANCELED)
  680. return 0;
  681. if (error)
  682. return error;
  683. rb = kzalloc(sizeof(struct xrep_bmap), XCHK_GFP_FLAGS);
  684. if (!rb)
  685. return -ENOMEM;
  686. rb->sc = sc;
  687. rb->whichfork = whichfork;
  688. rb->reflink_scan = xrep_bmap_init_reflink_scan(sc, whichfork);
  689. rb->allow_unwritten = allow_unwritten;
  690. /* Set up enough storage to handle the max records for this fork. */
  691. large_extcount = xfs_has_large_extent_counts(sc->mp);
  692. max_bmbt_recs = xfs_iext_max_nextents(large_extcount, whichfork);
  693. descr = xchk_xfile_ino_descr(sc, "%s fork mapping records",
  694. whichfork == XFS_DATA_FORK ? "data" : "attr");
  695. error = xfarray_create(descr, max_bmbt_recs,
  696. sizeof(struct xfs_bmbt_rec), &rb->bmap_records);
  697. kfree(descr);
  698. if (error)
  699. goto out_rb;
  700. /* Collect all reverse mappings for this fork's extents. */
  701. xfsb_bitmap_init(&rb->old_bmbt_blocks);
  702. error = xrep_bmap_find_mappings(rb);
  703. if (error)
  704. goto out_bitmap;
  705. xfs_trans_ijoin(sc->tp, sc->ip, 0);
  706. /* Rebuild the bmap information. */
  707. error = xrep_bmap_build_new_fork(rb);
  708. if (error)
  709. goto out_bitmap;
  710. /* Kill the old tree. */
  711. error = xrep_bmap_remove_old_tree(rb);
  712. if (error)
  713. goto out_bitmap;
  714. out_bitmap:
  715. xfsb_bitmap_destroy(&rb->old_bmbt_blocks);
  716. xfarray_destroy(rb->bmap_records);
  717. out_rb:
  718. kfree(rb);
  719. return error;
  720. }
  721. /* Repair an inode's data fork. */
  722. int
  723. xrep_bmap_data(
  724. struct xfs_scrub *sc)
  725. {
  726. return xrep_bmap(sc, XFS_DATA_FORK, true);
  727. }
  728. /* Repair an inode's attr fork. */
  729. int
  730. xrep_bmap_attr(
  731. struct xfs_scrub *sc)
  732. {
  733. return xrep_bmap(sc, XFS_ATTR_FORK, false);
  734. }