quota_repair.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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_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_alloc.h"
  21. #include "xfs_bmap.h"
  22. #include "xfs_quota.h"
  23. #include "xfs_qm.h"
  24. #include "xfs_dquot.h"
  25. #include "xfs_dquot_item.h"
  26. #include "xfs_reflink.h"
  27. #include "xfs_bmap_btree.h"
  28. #include "xfs_trans_space.h"
  29. #include "scrub/xfs_scrub.h"
  30. #include "scrub/scrub.h"
  31. #include "scrub/common.h"
  32. #include "scrub/quota.h"
  33. #include "scrub/trace.h"
  34. #include "scrub/repair.h"
  35. /*
  36. * Quota Repair
  37. * ============
  38. *
  39. * Quota repairs are fairly simplistic; we fix everything that the dquot
  40. * verifiers complain about, cap any counters or limits that make no sense,
  41. * and schedule a quotacheck if we had to fix anything. We also repair any
  42. * data fork extent records that don't apply to metadata files.
  43. */
  44. struct xrep_quota_info {
  45. struct xfs_scrub *sc;
  46. bool need_quotacheck;
  47. };
  48. /*
  49. * Allocate a new block into a sparse hole in the quota file backing this
  50. * dquot, initialize the block, and commit the whole mess.
  51. */
  52. STATIC int
  53. xrep_quota_item_fill_bmap_hole(
  54. struct xfs_scrub *sc,
  55. struct xfs_dquot *dq,
  56. struct xfs_bmbt_irec *irec)
  57. {
  58. struct xfs_buf *bp;
  59. struct xfs_mount *mp = sc->mp;
  60. int nmaps = 1;
  61. int error;
  62. xfs_trans_ijoin(sc->tp, sc->ip, 0);
  63. /* Map a block into the file. */
  64. error = xfs_trans_reserve_more(sc->tp, XFS_QM_DQALLOC_SPACE_RES(mp),
  65. 0);
  66. if (error)
  67. return error;
  68. error = xfs_bmapi_write(sc->tp, sc->ip, dq->q_fileoffset,
  69. XFS_DQUOT_CLUSTER_SIZE_FSB, XFS_BMAPI_METADATA, 0,
  70. irec, &nmaps);
  71. if (error)
  72. return error;
  73. dq->q_blkno = XFS_FSB_TO_DADDR(mp, irec->br_startblock);
  74. trace_xrep_dquot_item_fill_bmap_hole(sc->mp, dq->q_type, dq->q_id);
  75. /* Initialize the new block. */
  76. error = xfs_trans_get_buf(sc->tp, mp->m_ddev_targp, dq->q_blkno,
  77. mp->m_quotainfo->qi_dqchunklen, 0, &bp);
  78. if (error)
  79. return error;
  80. bp->b_ops = &xfs_dquot_buf_ops;
  81. xfs_qm_init_dquot_blk(sc->tp, dq->q_id, dq->q_type, bp);
  82. xfs_buf_set_ref(bp, XFS_DQUOT_REF);
  83. /*
  84. * Finish the mapping transactions and roll one more time to
  85. * disconnect sc->ip from sc->tp.
  86. */
  87. error = xrep_defer_finish(sc);
  88. if (error)
  89. return error;
  90. return xfs_trans_roll(&sc->tp);
  91. }
  92. /* Make sure there's a written block backing this dquot */
  93. STATIC int
  94. xrep_quota_item_bmap(
  95. struct xfs_scrub *sc,
  96. struct xfs_dquot *dq,
  97. bool *dirty)
  98. {
  99. struct xfs_bmbt_irec irec;
  100. struct xfs_mount *mp = sc->mp;
  101. struct xfs_quotainfo *qi = mp->m_quotainfo;
  102. xfs_fileoff_t offset = dq->q_id / qi->qi_dqperchunk;
  103. int nmaps = 1;
  104. int error;
  105. /* The computed file offset should always be valid. */
  106. if (!xfs_verify_fileoff(mp, offset)) {
  107. ASSERT(xfs_verify_fileoff(mp, offset));
  108. return -EFSCORRUPTED;
  109. }
  110. dq->q_fileoffset = offset;
  111. error = xfs_bmapi_read(sc->ip, offset, 1, &irec, &nmaps, 0);
  112. if (error)
  113. return error;
  114. if (nmaps < 1 || !xfs_bmap_is_real_extent(&irec)) {
  115. /* Hole/delalloc extent; allocate a real block. */
  116. error = xrep_quota_item_fill_bmap_hole(sc, dq, &irec);
  117. if (error)
  118. return error;
  119. } else if (irec.br_state != XFS_EXT_NORM) {
  120. /* Unwritten extent, which we already took care of? */
  121. ASSERT(irec.br_state == XFS_EXT_NORM);
  122. return -EFSCORRUPTED;
  123. } else if (dq->q_blkno != XFS_FSB_TO_DADDR(mp, irec.br_startblock)) {
  124. /*
  125. * If the cached daddr is incorrect, repair probably punched a
  126. * hole out of the quota file and filled it back in with a new
  127. * block. Update the block mapping in the dquot.
  128. */
  129. dq->q_blkno = XFS_FSB_TO_DADDR(mp, irec.br_startblock);
  130. }
  131. *dirty = true;
  132. return 0;
  133. }
  134. /* Reset quota timers if incorrectly set. */
  135. static inline void
  136. xrep_quota_item_timer(
  137. struct xfs_scrub *sc,
  138. const struct xfs_dquot_res *res,
  139. bool *dirty)
  140. {
  141. if ((res->softlimit && res->count > res->softlimit) ||
  142. (res->hardlimit && res->count > res->hardlimit)) {
  143. if (!res->timer)
  144. *dirty = true;
  145. } else {
  146. if (res->timer)
  147. *dirty = true;
  148. }
  149. }
  150. /* Scrub the fields in an individual quota item. */
  151. STATIC int
  152. xrep_quota_item(
  153. struct xrep_quota_info *rqi,
  154. struct xfs_dquot *dq)
  155. {
  156. struct xfs_scrub *sc = rqi->sc;
  157. struct xfs_mount *mp = sc->mp;
  158. xfs_ino_t fs_icount;
  159. bool dirty = false;
  160. int error = 0;
  161. /* Last chance to abort before we start committing fixes. */
  162. if (xchk_should_terminate(sc, &error))
  163. return error;
  164. /*
  165. * We might need to fix holes in the bmap record for the storage
  166. * backing this dquot, so we need to lock the dquot and the quota file.
  167. * dqiterate gave us a locked dquot, so drop the dquot lock to get the
  168. * ILOCK_EXCL.
  169. */
  170. xfs_dqunlock(dq);
  171. xchk_ilock(sc, XFS_ILOCK_EXCL);
  172. xfs_dqlock(dq);
  173. error = xrep_quota_item_bmap(sc, dq, &dirty);
  174. xchk_iunlock(sc, XFS_ILOCK_EXCL);
  175. if (error)
  176. return error;
  177. /* Check the limits. */
  178. if (dq->q_blk.softlimit > dq->q_blk.hardlimit) {
  179. dq->q_blk.softlimit = dq->q_blk.hardlimit;
  180. dirty = true;
  181. }
  182. if (dq->q_ino.softlimit > dq->q_ino.hardlimit) {
  183. dq->q_ino.softlimit = dq->q_ino.hardlimit;
  184. dirty = true;
  185. }
  186. if (dq->q_rtb.softlimit > dq->q_rtb.hardlimit) {
  187. dq->q_rtb.softlimit = dq->q_rtb.hardlimit;
  188. dirty = true;
  189. }
  190. /*
  191. * Check that usage doesn't exceed physical limits. However, on
  192. * a reflink filesystem we're allowed to exceed physical space
  193. * if there are no quota limits. We don't know what the real number
  194. * is, but we can make quotacheck find out for us.
  195. */
  196. if (!xfs_has_reflink(mp) && dq->q_blk.count > mp->m_sb.sb_dblocks) {
  197. dq->q_blk.reserved -= dq->q_blk.count;
  198. dq->q_blk.reserved += mp->m_sb.sb_dblocks;
  199. dq->q_blk.count = mp->m_sb.sb_dblocks;
  200. rqi->need_quotacheck = true;
  201. dirty = true;
  202. }
  203. fs_icount = percpu_counter_sum(&mp->m_icount);
  204. if (dq->q_ino.count > fs_icount) {
  205. dq->q_ino.reserved -= dq->q_ino.count;
  206. dq->q_ino.reserved += fs_icount;
  207. dq->q_ino.count = fs_icount;
  208. rqi->need_quotacheck = true;
  209. dirty = true;
  210. }
  211. if (dq->q_rtb.count > mp->m_sb.sb_rblocks) {
  212. dq->q_rtb.reserved -= dq->q_rtb.count;
  213. dq->q_rtb.reserved += mp->m_sb.sb_rblocks;
  214. dq->q_rtb.count = mp->m_sb.sb_rblocks;
  215. rqi->need_quotacheck = true;
  216. dirty = true;
  217. }
  218. xrep_quota_item_timer(sc, &dq->q_blk, &dirty);
  219. xrep_quota_item_timer(sc, &dq->q_ino, &dirty);
  220. xrep_quota_item_timer(sc, &dq->q_rtb, &dirty);
  221. if (!dirty)
  222. return 0;
  223. trace_xrep_dquot_item(sc->mp, dq->q_type, dq->q_id);
  224. dq->q_flags |= XFS_DQFLAG_DIRTY;
  225. xfs_trans_dqjoin(sc->tp, dq);
  226. if (dq->q_id) {
  227. xfs_qm_adjust_dqlimits(dq);
  228. xfs_qm_adjust_dqtimers(dq);
  229. }
  230. xfs_trans_log_dquot(sc->tp, dq);
  231. error = xfs_trans_roll(&sc->tp);
  232. xfs_dqlock(dq);
  233. return error;
  234. }
  235. /* Fix a quota timer so that we can pass the verifier. */
  236. STATIC void
  237. xrep_quota_fix_timer(
  238. struct xfs_mount *mp,
  239. const struct xfs_disk_dquot *ddq,
  240. __be64 softlimit,
  241. __be64 countnow,
  242. __be32 *timer,
  243. time64_t timelimit)
  244. {
  245. uint64_t soft = be64_to_cpu(softlimit);
  246. uint64_t count = be64_to_cpu(countnow);
  247. time64_t new_timer;
  248. uint32_t t;
  249. if (!soft || count <= soft || *timer != 0)
  250. return;
  251. new_timer = xfs_dquot_set_timeout(mp,
  252. ktime_get_real_seconds() + timelimit);
  253. if (ddq->d_type & XFS_DQTYPE_BIGTIME)
  254. t = xfs_dq_unix_to_bigtime(new_timer);
  255. else
  256. t = new_timer;
  257. *timer = cpu_to_be32(t);
  258. }
  259. /* Fix anything the verifiers complain about. */
  260. STATIC int
  261. xrep_quota_block(
  262. struct xfs_scrub *sc,
  263. xfs_daddr_t daddr,
  264. xfs_dqtype_t dqtype,
  265. xfs_dqid_t id)
  266. {
  267. struct xfs_dqblk *dqblk;
  268. struct xfs_disk_dquot *ddq;
  269. struct xfs_quotainfo *qi = sc->mp->m_quotainfo;
  270. struct xfs_def_quota *defq = xfs_get_defquota(qi, dqtype);
  271. struct xfs_buf *bp = NULL;
  272. enum xfs_blft buftype = 0;
  273. int i;
  274. int error;
  275. error = xfs_trans_read_buf(sc->mp, sc->tp, sc->mp->m_ddev_targp, daddr,
  276. qi->qi_dqchunklen, 0, &bp, &xfs_dquot_buf_ops);
  277. switch (error) {
  278. case -EFSBADCRC:
  279. case -EFSCORRUPTED:
  280. /* Failed verifier, retry read with no ops. */
  281. error = xfs_trans_read_buf(sc->mp, sc->tp,
  282. sc->mp->m_ddev_targp, daddr, qi->qi_dqchunklen,
  283. 0, &bp, NULL);
  284. if (error)
  285. return error;
  286. break;
  287. case 0:
  288. dqblk = bp->b_addr;
  289. ddq = &dqblk[0].dd_diskdq;
  290. /*
  291. * If there's nothing that would impede a dqiterate, we're
  292. * done.
  293. */
  294. if ((ddq->d_type & XFS_DQTYPE_REC_MASK) != dqtype ||
  295. id == be32_to_cpu(ddq->d_id)) {
  296. xfs_trans_brelse(sc->tp, bp);
  297. return 0;
  298. }
  299. break;
  300. default:
  301. return error;
  302. }
  303. /* Something's wrong with the block, fix the whole thing. */
  304. dqblk = bp->b_addr;
  305. bp->b_ops = &xfs_dquot_buf_ops;
  306. for (i = 0; i < qi->qi_dqperchunk; i++, dqblk++) {
  307. ddq = &dqblk->dd_diskdq;
  308. trace_xrep_disk_dquot(sc->mp, dqtype, id + i);
  309. ddq->d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
  310. ddq->d_version = XFS_DQUOT_VERSION;
  311. ddq->d_type = dqtype;
  312. ddq->d_id = cpu_to_be32(id + i);
  313. if (xfs_has_bigtime(sc->mp) && ddq->d_id)
  314. ddq->d_type |= XFS_DQTYPE_BIGTIME;
  315. xrep_quota_fix_timer(sc->mp, ddq, ddq->d_blk_softlimit,
  316. ddq->d_bcount, &ddq->d_btimer,
  317. defq->blk.time);
  318. xrep_quota_fix_timer(sc->mp, ddq, ddq->d_ino_softlimit,
  319. ddq->d_icount, &ddq->d_itimer,
  320. defq->ino.time);
  321. xrep_quota_fix_timer(sc->mp, ddq, ddq->d_rtb_softlimit,
  322. ddq->d_rtbcount, &ddq->d_rtbtimer,
  323. defq->rtb.time);
  324. /* We only support v5 filesystems so always set these. */
  325. uuid_copy(&dqblk->dd_uuid, &sc->mp->m_sb.sb_meta_uuid);
  326. xfs_update_cksum((char *)dqblk, sizeof(struct xfs_dqblk),
  327. XFS_DQUOT_CRC_OFF);
  328. dqblk->dd_lsn = 0;
  329. }
  330. switch (dqtype) {
  331. case XFS_DQTYPE_USER:
  332. buftype = XFS_BLFT_UDQUOT_BUF;
  333. break;
  334. case XFS_DQTYPE_GROUP:
  335. buftype = XFS_BLFT_GDQUOT_BUF;
  336. break;
  337. case XFS_DQTYPE_PROJ:
  338. buftype = XFS_BLFT_PDQUOT_BUF;
  339. break;
  340. }
  341. xfs_trans_buf_set_type(sc->tp, bp, buftype);
  342. xfs_trans_log_buf(sc->tp, bp, 0, BBTOB(bp->b_length) - 1);
  343. return xrep_roll_trans(sc);
  344. }
  345. /*
  346. * Repair a quota file's data fork. The function returns with the inode
  347. * joined.
  348. */
  349. STATIC int
  350. xrep_quota_data_fork(
  351. struct xfs_scrub *sc,
  352. xfs_dqtype_t dqtype)
  353. {
  354. struct xfs_bmbt_irec irec = { 0 };
  355. struct xfs_iext_cursor icur;
  356. struct xfs_quotainfo *qi = sc->mp->m_quotainfo;
  357. struct xfs_ifork *ifp;
  358. xfs_fileoff_t max_dqid_off;
  359. xfs_fileoff_t off;
  360. xfs_fsblock_t fsbno;
  361. bool truncate = false;
  362. bool joined = false;
  363. int error = 0;
  364. error = xrep_metadata_inode_forks(sc);
  365. if (error)
  366. goto out;
  367. /* Check for data fork problems that apply only to quota files. */
  368. max_dqid_off = XFS_DQ_ID_MAX / qi->qi_dqperchunk;
  369. ifp = xfs_ifork_ptr(sc->ip, XFS_DATA_FORK);
  370. for_each_xfs_iext(ifp, &icur, &irec) {
  371. if (isnullstartblock(irec.br_startblock)) {
  372. error = -EFSCORRUPTED;
  373. goto out;
  374. }
  375. if (irec.br_startoff > max_dqid_off ||
  376. irec.br_startoff + irec.br_blockcount - 1 > max_dqid_off) {
  377. truncate = true;
  378. break;
  379. }
  380. /* Convert unwritten extents to real ones. */
  381. if (irec.br_state == XFS_EXT_UNWRITTEN) {
  382. struct xfs_bmbt_irec nrec;
  383. int nmap = 1;
  384. if (!joined) {
  385. xfs_trans_ijoin(sc->tp, sc->ip, 0);
  386. joined = true;
  387. }
  388. error = xfs_bmapi_write(sc->tp, sc->ip,
  389. irec.br_startoff, irec.br_blockcount,
  390. XFS_BMAPI_CONVERT, 0, &nrec, &nmap);
  391. if (error)
  392. goto out;
  393. ASSERT(nrec.br_startoff == irec.br_startoff);
  394. ASSERT(nrec.br_blockcount == irec.br_blockcount);
  395. error = xfs_defer_finish(&sc->tp);
  396. if (error)
  397. goto out;
  398. }
  399. }
  400. if (!joined) {
  401. xfs_trans_ijoin(sc->tp, sc->ip, 0);
  402. joined = true;
  403. }
  404. if (truncate) {
  405. /* Erase everything after the block containing the max dquot */
  406. error = xfs_bunmapi_range(&sc->tp, sc->ip, 0,
  407. max_dqid_off * sc->mp->m_sb.sb_blocksize,
  408. XFS_MAX_FILEOFF);
  409. if (error)
  410. goto out;
  411. /* Remove all CoW reservations. */
  412. error = xfs_reflink_cancel_cow_blocks(sc->ip, &sc->tp, 0,
  413. XFS_MAX_FILEOFF, true);
  414. if (error)
  415. goto out;
  416. sc->ip->i_diflags2 &= ~XFS_DIFLAG2_REFLINK;
  417. /*
  418. * Always re-log the inode so that our permanent transaction
  419. * can keep on rolling it forward in the log.
  420. */
  421. xfs_trans_log_inode(sc->tp, sc->ip, XFS_ILOG_CORE);
  422. }
  423. /* Now go fix anything that fails the verifiers. */
  424. for_each_xfs_iext(ifp, &icur, &irec) {
  425. for (fsbno = irec.br_startblock, off = irec.br_startoff;
  426. fsbno < irec.br_startblock + irec.br_blockcount;
  427. fsbno += XFS_DQUOT_CLUSTER_SIZE_FSB,
  428. off += XFS_DQUOT_CLUSTER_SIZE_FSB) {
  429. error = xrep_quota_block(sc,
  430. XFS_FSB_TO_DADDR(sc->mp, fsbno),
  431. dqtype, off * qi->qi_dqperchunk);
  432. if (error)
  433. goto out;
  434. }
  435. }
  436. out:
  437. return error;
  438. }
  439. /*
  440. * Go fix anything in the quota items that we could have been mad about. Now
  441. * that we've checked the quota inode data fork we have to drop ILOCK_EXCL to
  442. * use the regular dquot functions.
  443. */
  444. STATIC int
  445. xrep_quota_problems(
  446. struct xfs_scrub *sc,
  447. xfs_dqtype_t dqtype)
  448. {
  449. struct xchk_dqiter cursor = { };
  450. struct xrep_quota_info rqi = { .sc = sc };
  451. struct xfs_dquot *dq;
  452. int error;
  453. xchk_dqiter_init(&cursor, sc, dqtype);
  454. while ((error = xchk_dquot_iter(&cursor, &dq)) == 1) {
  455. error = xrep_quota_item(&rqi, dq);
  456. xfs_qm_dqput(dq);
  457. if (error)
  458. break;
  459. }
  460. if (error)
  461. return error;
  462. /* Make a quotacheck happen. */
  463. if (rqi.need_quotacheck)
  464. xrep_force_quotacheck(sc, dqtype);
  465. return 0;
  466. }
  467. /* Repair all of a quota type's items. */
  468. int
  469. xrep_quota(
  470. struct xfs_scrub *sc)
  471. {
  472. xfs_dqtype_t dqtype;
  473. int error;
  474. dqtype = xchk_quota_to_dqtype(sc);
  475. /*
  476. * Re-take the ILOCK so that we can fix any problems that we found
  477. * with the data fork mappings, or with the dquot bufs themselves.
  478. */
  479. if (!(sc->ilock_flags & XFS_ILOCK_EXCL))
  480. xchk_ilock(sc, XFS_ILOCK_EXCL);
  481. error = xrep_quota_data_fork(sc, dqtype);
  482. if (error)
  483. return error;
  484. /*
  485. * Finish deferred items and roll the transaction to unjoin the quota
  486. * inode from transaction so that we can unlock the quota inode; we
  487. * play only with dquots from now on.
  488. */
  489. error = xrep_defer_finish(sc);
  490. if (error)
  491. return error;
  492. error = xfs_trans_roll(&sc->tp);
  493. if (error)
  494. return error;
  495. xchk_iunlock(sc, sc->ilock_flags);
  496. /* Fix anything the dquot verifiers don't complain about. */
  497. error = xrep_quota_problems(sc, dqtype);
  498. if (error)
  499. return error;
  500. return xrep_trans_commit(sc);
  501. }