symlink_repair.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2018-2024 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_bit.h"
  15. #include "xfs_log_format.h"
  16. #include "xfs_trans.h"
  17. #include "xfs_sb.h"
  18. #include "xfs_inode.h"
  19. #include "xfs_inode_fork.h"
  20. #include "xfs_symlink.h"
  21. #include "xfs_bmap.h"
  22. #include "xfs_quota.h"
  23. #include "xfs_da_format.h"
  24. #include "xfs_da_btree.h"
  25. #include "xfs_bmap_btree.h"
  26. #include "xfs_trans_space.h"
  27. #include "xfs_symlink_remote.h"
  28. #include "xfs_exchmaps.h"
  29. #include "xfs_exchrange.h"
  30. #include "xfs_health.h"
  31. #include "scrub/xfs_scrub.h"
  32. #include "scrub/scrub.h"
  33. #include "scrub/common.h"
  34. #include "scrub/trace.h"
  35. #include "scrub/repair.h"
  36. #include "scrub/tempfile.h"
  37. #include "scrub/tempexch.h"
  38. #include "scrub/reap.h"
  39. #include "scrub/health.h"
  40. /*
  41. * Symbolic Link Repair
  42. * ====================
  43. *
  44. * We repair symbolic links by reading whatever target data we can find, up to
  45. * the first NULL byte. If the recovered target strlen matches i_size, then
  46. * we rewrite the target. In all other cases, we replace the target with an
  47. * overly long string that cannot possibly resolve. The new target is written
  48. * into a private hidden temporary file, and then a file contents exchange
  49. * commits the new symlink target to the file being repaired.
  50. */
  51. /* Set us up to repair the symlink file. */
  52. int
  53. xrep_setup_symlink(
  54. struct xfs_scrub *sc,
  55. unsigned int *resblks)
  56. {
  57. struct xfs_mount *mp = sc->mp;
  58. unsigned long long blocks;
  59. int error;
  60. error = xrep_tempfile_create(sc, S_IFLNK);
  61. if (error)
  62. return error;
  63. /*
  64. * If we're doing a repair, we reserve enough blocks to write out a
  65. * completely new symlink file, plus twice as many blocks as we would
  66. * need if we can only allocate one block per data fork mapping. This
  67. * should cover the preallocation of the temporary file and exchanging
  68. * the extent mappings.
  69. *
  70. * We cannot use xfs_exchmaps_estimate because we have not yet
  71. * constructed the replacement symlink and therefore do not know how
  72. * many extents it will use. By the time we do, we will have a dirty
  73. * transaction (which we cannot drop because we cannot drop the
  74. * symlink ILOCK) and cannot ask for more reservation.
  75. */
  76. blocks = xfs_symlink_blocks(sc->mp, XFS_SYMLINK_MAXLEN);
  77. blocks += xfs_bmbt_calc_size(mp, blocks) * 2;
  78. if (blocks > UINT_MAX)
  79. return -EOPNOTSUPP;
  80. *resblks += blocks;
  81. return 0;
  82. }
  83. /*
  84. * Try to salvage the pathname from remote blocks. Returns the number of bytes
  85. * salvaged or a negative errno.
  86. */
  87. STATIC ssize_t
  88. xrep_symlink_salvage_remote(
  89. struct xfs_scrub *sc)
  90. {
  91. struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS];
  92. struct xfs_inode *ip = sc->ip;
  93. struct xfs_buf *bp;
  94. char *target_buf = sc->buf;
  95. xfs_failaddr_t fa;
  96. xfs_filblks_t fsblocks;
  97. xfs_daddr_t d;
  98. loff_t len;
  99. loff_t offset = 0;
  100. unsigned int byte_cnt;
  101. bool magic_ok;
  102. bool hdr_ok;
  103. int n;
  104. int nmaps = XFS_SYMLINK_MAPS;
  105. int error;
  106. /* We'll only read until the buffer is full. */
  107. len = min_t(loff_t, ip->i_disk_size, XFS_SYMLINK_MAXLEN);
  108. fsblocks = xfs_symlink_blocks(sc->mp, len);
  109. error = xfs_bmapi_read(ip, 0, fsblocks, mval, &nmaps, 0);
  110. if (error)
  111. return error;
  112. for (n = 0; n < nmaps; n++) {
  113. struct xfs_dsymlink_hdr *dsl;
  114. d = XFS_FSB_TO_DADDR(sc->mp, mval[n].br_startblock);
  115. /* Read the rmt block. We'll run the verifiers manually. */
  116. error = xfs_trans_read_buf(sc->mp, sc->tp, sc->mp->m_ddev_targp,
  117. d, XFS_FSB_TO_BB(sc->mp, mval[n].br_blockcount),
  118. 0, &bp, NULL);
  119. if (error)
  120. return error;
  121. bp->b_ops = &xfs_symlink_buf_ops;
  122. /* How many bytes do we expect to get out of this buffer? */
  123. byte_cnt = XFS_FSB_TO_B(sc->mp, mval[n].br_blockcount);
  124. byte_cnt = XFS_SYMLINK_BUF_SPACE(sc->mp, byte_cnt);
  125. byte_cnt = min_t(unsigned int, byte_cnt, len);
  126. /*
  127. * See if the verifiers accept this block. We're willing to
  128. * salvage if the if the offset/byte/ino are ok and either the
  129. * verifier passed or the magic is ok. Anything else and we
  130. * stop dead in our tracks.
  131. */
  132. fa = bp->b_ops->verify_struct(bp);
  133. dsl = bp->b_addr;
  134. magic_ok = dsl->sl_magic == cpu_to_be32(XFS_SYMLINK_MAGIC);
  135. hdr_ok = xfs_symlink_hdr_ok(ip->i_ino, offset, byte_cnt, bp);
  136. if (!hdr_ok || (fa != NULL && !magic_ok))
  137. break;
  138. memcpy(target_buf + offset, dsl + 1, byte_cnt);
  139. len -= byte_cnt;
  140. offset += byte_cnt;
  141. }
  142. return offset;
  143. }
  144. /*
  145. * Try to salvage an inline symlink's contents. Returns the number of bytes
  146. * salvaged or a negative errno.
  147. */
  148. STATIC ssize_t
  149. xrep_symlink_salvage_inline(
  150. struct xfs_scrub *sc)
  151. {
  152. struct xfs_inode *ip = sc->ip;
  153. char *target_buf = sc->buf;
  154. char *old_target;
  155. struct xfs_ifork *ifp;
  156. unsigned int nr;
  157. ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK);
  158. if (!ifp->if_data)
  159. return 0;
  160. /*
  161. * If inode repair zapped the link target, pretend that we didn't find
  162. * any bytes at all so that we can replace the (now totally lost) link
  163. * target with a warning message.
  164. */
  165. old_target = ifp->if_data;
  166. if (xfs_inode_has_sickness(sc->ip, XFS_SICK_INO_SYMLINK_ZAPPED) &&
  167. sc->ip->i_disk_size == 1 && old_target[0] == '?')
  168. return 0;
  169. nr = min(XFS_SYMLINK_MAXLEN, xfs_inode_data_fork_size(ip));
  170. strncpy(target_buf, ifp->if_data, nr);
  171. return nr;
  172. }
  173. #define DUMMY_TARGET \
  174. "The target of this symbolic link could not be recovered at all and " \
  175. "has been replaced with this explanatory message. To avoid " \
  176. "accidentally pointing to an existing file path, this message is " \
  177. "longer than the maximum supported file name length. That is an " \
  178. "acceptable length for a symlink target on XFS but will produce " \
  179. "File Name Too Long errors if resolved."
  180. /* Salvage whatever we can of the target. */
  181. STATIC int
  182. xrep_symlink_salvage(
  183. struct xfs_scrub *sc)
  184. {
  185. char *target_buf = sc->buf;
  186. ssize_t buflen = 0;
  187. BUILD_BUG_ON(sizeof(DUMMY_TARGET) - 1 <= NAME_MAX);
  188. /*
  189. * Salvage the target if there weren't any corruption problems observed
  190. * while scanning it.
  191. */
  192. if (!(sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) {
  193. if (sc->ip->i_df.if_format == XFS_DINODE_FMT_LOCAL)
  194. buflen = xrep_symlink_salvage_inline(sc);
  195. else
  196. buflen = xrep_symlink_salvage_remote(sc);
  197. if (buflen < 0)
  198. return buflen;
  199. /*
  200. * NULL-terminate the buffer because the ondisk target does not
  201. * do that for us. If salvage didn't find the exact amount of
  202. * data that we expected to find, don't salvage anything.
  203. */
  204. target_buf[buflen] = 0;
  205. if (strlen(target_buf) != sc->ip->i_disk_size)
  206. buflen = 0;
  207. }
  208. /*
  209. * Change an empty target into a dummy target and clear the symlink
  210. * target zapped flag.
  211. */
  212. if (buflen == 0) {
  213. xchk_mark_healthy_if_clean(sc, XFS_SICK_INO_SYMLINK_ZAPPED);
  214. sprintf(target_buf, DUMMY_TARGET);
  215. }
  216. trace_xrep_symlink_salvage_target(sc->ip, target_buf,
  217. strlen(target_buf));
  218. return 0;
  219. }
  220. STATIC void
  221. xrep_symlink_local_to_remote(
  222. struct xfs_trans *tp,
  223. struct xfs_buf *bp,
  224. struct xfs_inode *ip,
  225. struct xfs_ifork *ifp,
  226. void *priv)
  227. {
  228. struct xfs_scrub *sc = priv;
  229. struct xfs_dsymlink_hdr *dsl = bp->b_addr;
  230. xfs_symlink_local_to_remote(tp, bp, ip, ifp, NULL);
  231. if (!xfs_has_crc(sc->mp))
  232. return;
  233. dsl->sl_owner = cpu_to_be64(sc->ip->i_ino);
  234. xfs_trans_log_buf(tp, bp, 0,
  235. sizeof(struct xfs_dsymlink_hdr) + ifp->if_bytes - 1);
  236. }
  237. /*
  238. * Prepare both links' data forks for an exchange. Promote the tempfile from
  239. * local format to extents format, and if the file being repaired has a short
  240. * format data fork, turn it into an empty extent list.
  241. */
  242. STATIC int
  243. xrep_symlink_swap_prep(
  244. struct xfs_scrub *sc,
  245. bool temp_local,
  246. bool ip_local)
  247. {
  248. int error;
  249. /*
  250. * If the temp link is in shortform format, convert that to a remote
  251. * target so that we can use the atomic mapping exchange.
  252. */
  253. if (temp_local) {
  254. int logflags = XFS_ILOG_CORE;
  255. error = xfs_bmap_local_to_extents(sc->tp, sc->tempip, 1,
  256. &logflags, XFS_DATA_FORK,
  257. xrep_symlink_local_to_remote,
  258. sc);
  259. if (error)
  260. return error;
  261. xfs_trans_log_inode(sc->tp, sc->ip, 0);
  262. error = xfs_defer_finish(&sc->tp);
  263. if (error)
  264. return error;
  265. }
  266. /*
  267. * If the file being repaired had a shortform data fork, convert that
  268. * to an empty extent list in preparation for the atomic mapping
  269. * exchange.
  270. */
  271. if (ip_local) {
  272. struct xfs_ifork *ifp;
  273. ifp = xfs_ifork_ptr(sc->ip, XFS_DATA_FORK);
  274. xfs_idestroy_fork(ifp);
  275. ifp->if_format = XFS_DINODE_FMT_EXTENTS;
  276. ifp->if_nextents = 0;
  277. ifp->if_bytes = 0;
  278. ifp->if_data = NULL;
  279. ifp->if_height = 0;
  280. xfs_trans_log_inode(sc->tp, sc->ip,
  281. XFS_ILOG_CORE | XFS_ILOG_DDATA);
  282. }
  283. return 0;
  284. }
  285. /* Exchange the temporary symlink's data fork with the one being repaired. */
  286. STATIC int
  287. xrep_symlink_swap(
  288. struct xfs_scrub *sc)
  289. {
  290. struct xrep_tempexch *tx = sc->buf;
  291. bool ip_local, temp_local;
  292. int error;
  293. ip_local = sc->ip->i_df.if_format == XFS_DINODE_FMT_LOCAL;
  294. temp_local = sc->tempip->i_df.if_format == XFS_DINODE_FMT_LOCAL;
  295. /*
  296. * If the both links have a local format data fork and the rebuilt
  297. * remote data would fit in the repaired file's data fork, copy the
  298. * contents from the tempfile and declare ourselves done.
  299. */
  300. if (ip_local && temp_local &&
  301. sc->tempip->i_disk_size <= xfs_inode_data_fork_size(sc->ip)) {
  302. xrep_tempfile_copyout_local(sc, XFS_DATA_FORK);
  303. return 0;
  304. }
  305. /* Otherwise, make sure both data forks are in block-mapping mode. */
  306. error = xrep_symlink_swap_prep(sc, temp_local, ip_local);
  307. if (error)
  308. return error;
  309. return xrep_tempexch_contents(sc, tx);
  310. }
  311. /*
  312. * Free all the remote blocks and reset the data fork. The caller must join
  313. * the inode to the transaction. This function returns with the inode joined
  314. * to a clean scrub transaction.
  315. */
  316. STATIC int
  317. xrep_symlink_reset_fork(
  318. struct xfs_scrub *sc)
  319. {
  320. struct xfs_ifork *ifp = xfs_ifork_ptr(sc->tempip, XFS_DATA_FORK);
  321. int error;
  322. /* Unmap all the remote target buffers. */
  323. if (xfs_ifork_has_extents(ifp)) {
  324. error = xrep_reap_ifork(sc, sc->tempip, XFS_DATA_FORK);
  325. if (error)
  326. return error;
  327. }
  328. trace_xrep_symlink_reset_fork(sc->tempip);
  329. /* Reset the temp symlink target to dummy content. */
  330. xfs_idestroy_fork(ifp);
  331. return xfs_symlink_write_target(sc->tp, sc->tempip, sc->tempip->i_ino,
  332. "?", 1, 0, 0);
  333. }
  334. /*
  335. * Reinitialize a link target. Caller must ensure the inode is joined to
  336. * the transaction.
  337. */
  338. STATIC int
  339. xrep_symlink_rebuild(
  340. struct xfs_scrub *sc)
  341. {
  342. struct xrep_tempexch *tx;
  343. char *target_buf = sc->buf;
  344. xfs_fsblock_t fs_blocks;
  345. unsigned int target_len;
  346. unsigned int resblks;
  347. int error;
  348. /* How many blocks do we need? */
  349. target_len = strlen(target_buf);
  350. ASSERT(target_len != 0);
  351. if (target_len == 0 || target_len > XFS_SYMLINK_MAXLEN)
  352. return -EFSCORRUPTED;
  353. trace_xrep_symlink_rebuild(sc->ip);
  354. /*
  355. * In preparation to write the new symlink target to the temporary
  356. * file, drop the ILOCK of the file being repaired (it shouldn't be
  357. * joined) and take the ILOCK of the temporary file.
  358. *
  359. * The VFS does not take the IOLOCK while reading a symlink (and new
  360. * symlinks are hidden with INEW until they've been written) so it's
  361. * possible that a readlink() could see the old corrupted contents
  362. * while we're doing this.
  363. */
  364. xchk_iunlock(sc, XFS_ILOCK_EXCL);
  365. xrep_tempfile_ilock(sc);
  366. xfs_trans_ijoin(sc->tp, sc->tempip, 0);
  367. /*
  368. * Reserve resources to reinitialize the target. We're allowed to
  369. * exceed file quota to repair inconsistent metadata, though this is
  370. * unlikely.
  371. */
  372. fs_blocks = xfs_symlink_blocks(sc->mp, target_len);
  373. resblks = xfs_symlink_space_res(sc->mp, target_len, fs_blocks);
  374. error = xfs_trans_reserve_quota_nblks(sc->tp, sc->tempip, resblks, 0,
  375. true);
  376. if (error)
  377. return error;
  378. /* Erase the dummy target set up by the tempfile initialization. */
  379. xfs_idestroy_fork(&sc->tempip->i_df);
  380. sc->tempip->i_df.if_bytes = 0;
  381. sc->tempip->i_df.if_format = XFS_DINODE_FMT_EXTENTS;
  382. /* Write the salvaged target to the temporary link. */
  383. error = xfs_symlink_write_target(sc->tp, sc->tempip, sc->ip->i_ino,
  384. target_buf, target_len, fs_blocks, resblks);
  385. if (error)
  386. return error;
  387. /*
  388. * Commit the repair transaction so that we can use the atomic mapping
  389. * exchange functions to compute the correct block reservations and
  390. * re-lock the inodes.
  391. */
  392. target_buf = NULL;
  393. error = xrep_trans_commit(sc);
  394. if (error)
  395. return error;
  396. /* Last chance to abort before we start committing fixes. */
  397. if (xchk_should_terminate(sc, &error))
  398. return error;
  399. xrep_tempfile_iunlock(sc);
  400. /*
  401. * We're done with the temporary buffer, so we can reuse it for the
  402. * tempfile contents exchange information.
  403. */
  404. tx = sc->buf;
  405. error = xrep_tempexch_trans_alloc(sc, XFS_DATA_FORK, tx);
  406. if (error)
  407. return error;
  408. /*
  409. * Exchange the temp link's data fork with the file being repaired.
  410. * This recreates the transaction and takes the ILOCKs of the file
  411. * being repaired and the temporary file.
  412. */
  413. error = xrep_symlink_swap(sc);
  414. if (error)
  415. return error;
  416. /*
  417. * Release the old symlink blocks and reset the data fork of the temp
  418. * link to an empty shortform link. This is the last repair action we
  419. * perform on the symlink, so we don't need to clean the transaction.
  420. */
  421. return xrep_symlink_reset_fork(sc);
  422. }
  423. /* Repair a symbolic link. */
  424. int
  425. xrep_symlink(
  426. struct xfs_scrub *sc)
  427. {
  428. int error;
  429. /* The rmapbt is required to reap the old data fork. */
  430. if (!xfs_has_rmapbt(sc->mp))
  431. return -EOPNOTSUPP;
  432. /* We require atomic file exchange range to rebuild anything. */
  433. if (!xfs_has_exchange_range(sc->mp))
  434. return -EOPNOTSUPP;
  435. ASSERT(sc->ilock_flags & XFS_ILOCK_EXCL);
  436. error = xrep_symlink_salvage(sc);
  437. if (error)
  438. return error;
  439. /* Now reset the target. */
  440. error = xrep_symlink_rebuild(sc);
  441. if (error)
  442. return error;
  443. return xrep_trans_commit(sc);
  444. }