xfs_defer.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2016 Oracle. All Rights Reserved.
  4. * Author: Darrick J. Wong <darrick.wong@oracle.com>
  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_bit.h"
  13. #include "xfs_sb.h"
  14. #include "xfs_mount.h"
  15. #include "xfs_defer.h"
  16. #include "xfs_trans.h"
  17. #include "xfs_buf_item.h"
  18. #include "xfs_inode.h"
  19. #include "xfs_inode_item.h"
  20. #include "xfs_trace.h"
  21. /*
  22. * Deferred Operations in XFS
  23. *
  24. * Due to the way locking rules work in XFS, certain transactions (block
  25. * mapping and unmapping, typically) have permanent reservations so that
  26. * we can roll the transaction to adhere to AG locking order rules and
  27. * to unlock buffers between metadata updates. Prior to rmap/reflink,
  28. * the mapping code had a mechanism to perform these deferrals for
  29. * extents that were going to be freed; this code makes that facility
  30. * more generic.
  31. *
  32. * When adding the reverse mapping and reflink features, it became
  33. * necessary to perform complex remapping multi-transactions to comply
  34. * with AG locking order rules, and to be able to spread a single
  35. * refcount update operation (an operation on an n-block extent can
  36. * update as many as n records!) among multiple transactions. XFS can
  37. * roll a transaction to facilitate this, but using this facility
  38. * requires us to log "intent" items in case log recovery needs to
  39. * redo the operation, and to log "done" items to indicate that redo
  40. * is not necessary.
  41. *
  42. * Deferred work is tracked in xfs_defer_pending items. Each pending
  43. * item tracks one type of deferred work. Incoming work items (which
  44. * have not yet had an intent logged) are attached to a pending item
  45. * on the dop_intake list, where they wait for the caller to finish
  46. * the deferred operations.
  47. *
  48. * Finishing a set of deferred operations is an involved process. To
  49. * start, we define "rolling a deferred-op transaction" as follows:
  50. *
  51. * > For each xfs_defer_pending item on the dop_intake list,
  52. * - Sort the work items in AG order. XFS locking
  53. * order rules require us to lock buffers in AG order.
  54. * - Create a log intent item for that type.
  55. * - Attach it to the pending item.
  56. * - Move the pending item from the dop_intake list to the
  57. * dop_pending list.
  58. * > Roll the transaction.
  59. *
  60. * NOTE: To avoid exceeding the transaction reservation, we limit the
  61. * number of items that we attach to a given xfs_defer_pending.
  62. *
  63. * The actual finishing process looks like this:
  64. *
  65. * > For each xfs_defer_pending in the dop_pending list,
  66. * - Roll the deferred-op transaction as above.
  67. * - Create a log done item for that type, and attach it to the
  68. * log intent item.
  69. * - For each work item attached to the log intent item,
  70. * * Perform the described action.
  71. * * Attach the work item to the log done item.
  72. * * If the result of doing the work was -EAGAIN, ->finish work
  73. * wants a new transaction. See the "Requesting a Fresh
  74. * Transaction while Finishing Deferred Work" section below for
  75. * details.
  76. *
  77. * The key here is that we must log an intent item for all pending
  78. * work items every time we roll the transaction, and that we must log
  79. * a done item as soon as the work is completed. With this mechanism
  80. * we can perform complex remapping operations, chaining intent items
  81. * as needed.
  82. *
  83. * Requesting a Fresh Transaction while Finishing Deferred Work
  84. *
  85. * If ->finish_item decides that it needs a fresh transaction to
  86. * finish the work, it must ask its caller (xfs_defer_finish) for a
  87. * continuation. The most likely cause of this circumstance are the
  88. * refcount adjust functions deciding that they've logged enough items
  89. * to be at risk of exceeding the transaction reservation.
  90. *
  91. * To get a fresh transaction, we want to log the existing log done
  92. * item to prevent the log intent item from replaying, immediately log
  93. * a new log intent item with the unfinished work items, roll the
  94. * transaction, and re-call ->finish_item wherever it left off. The
  95. * log done item and the new log intent item must be in the same
  96. * transaction or atomicity cannot be guaranteed; defer_finish ensures
  97. * that this happens.
  98. *
  99. * This requires some coordination between ->finish_item and
  100. * defer_finish. Upon deciding to request a new transaction,
  101. * ->finish_item should update the current work item to reflect the
  102. * unfinished work. Next, it should reset the log done item's list
  103. * count to the number of items finished, and return -EAGAIN.
  104. * defer_finish sees the -EAGAIN, logs the new log intent item
  105. * with the remaining work items, and leaves the xfs_defer_pending
  106. * item at the head of the dop_work queue. Then it rolls the
  107. * transaction and picks up processing where it left off. It is
  108. * required that ->finish_item must be careful to leave enough
  109. * transaction reservation to fit the new log intent item.
  110. *
  111. * This is an example of remapping the extent (E, E+B) into file X at
  112. * offset A and dealing with the extent (C, C+B) already being mapped
  113. * there:
  114. * +-------------------------------------------------+
  115. * | Unmap file X startblock C offset A length B | t0
  116. * | Intent to reduce refcount for extent (C, B) |
  117. * | Intent to remove rmap (X, C, A, B) |
  118. * | Intent to free extent (D, 1) (bmbt block) |
  119. * | Intent to map (X, A, B) at startblock E |
  120. * +-------------------------------------------------+
  121. * | Map file X startblock E offset A length B | t1
  122. * | Done mapping (X, E, A, B) |
  123. * | Intent to increase refcount for extent (E, B) |
  124. * | Intent to add rmap (X, E, A, B) |
  125. * +-------------------------------------------------+
  126. * | Reduce refcount for extent (C, B) | t2
  127. * | Done reducing refcount for extent (C, 9) |
  128. * | Intent to reduce refcount for extent (C+9, B-9) |
  129. * | (ran out of space after 9 refcount updates) |
  130. * +-------------------------------------------------+
  131. * | Reduce refcount for extent (C+9, B+9) | t3
  132. * | Done reducing refcount for extent (C+9, B-9) |
  133. * | Increase refcount for extent (E, B) |
  134. * | Done increasing refcount for extent (E, B) |
  135. * | Intent to free extent (C, B) |
  136. * | Intent to free extent (F, 1) (refcountbt block) |
  137. * | Intent to remove rmap (F, 1, REFC) |
  138. * +-------------------------------------------------+
  139. * | Remove rmap (X, C, A, B) | t4
  140. * | Done removing rmap (X, C, A, B) |
  141. * | Add rmap (X, E, A, B) |
  142. * | Done adding rmap (X, E, A, B) |
  143. * | Remove rmap (F, 1, REFC) |
  144. * | Done removing rmap (F, 1, REFC) |
  145. * +-------------------------------------------------+
  146. * | Free extent (C, B) | t5
  147. * | Done freeing extent (C, B) |
  148. * | Free extent (D, 1) |
  149. * | Done freeing extent (D, 1) |
  150. * | Free extent (F, 1) |
  151. * | Done freeing extent (F, 1) |
  152. * +-------------------------------------------------+
  153. *
  154. * If we should crash before t2 commits, log recovery replays
  155. * the following intent items:
  156. *
  157. * - Intent to reduce refcount for extent (C, B)
  158. * - Intent to remove rmap (X, C, A, B)
  159. * - Intent to free extent (D, 1) (bmbt block)
  160. * - Intent to increase refcount for extent (E, B)
  161. * - Intent to add rmap (X, E, A, B)
  162. *
  163. * In the process of recovering, it should also generate and take care
  164. * of these intent items:
  165. *
  166. * - Intent to free extent (C, B)
  167. * - Intent to free extent (F, 1) (refcountbt block)
  168. * - Intent to remove rmap (F, 1, REFC)
  169. *
  170. * Note that the continuation requested between t2 and t3 is likely to
  171. * reoccur.
  172. */
  173. static const struct xfs_defer_op_type *defer_op_types[XFS_DEFER_OPS_TYPE_MAX];
  174. /*
  175. * For each pending item in the intake list, log its intent item and the
  176. * associated extents, then add the entire intake list to the end of
  177. * the pending list.
  178. */
  179. STATIC void
  180. xfs_defer_create_intents(
  181. struct xfs_trans *tp)
  182. {
  183. struct list_head *li;
  184. struct xfs_defer_pending *dfp;
  185. list_for_each_entry(dfp, &tp->t_dfops, dfp_list) {
  186. dfp->dfp_intent = dfp->dfp_type->create_intent(tp,
  187. dfp->dfp_count);
  188. trace_xfs_defer_create_intent(tp->t_mountp, dfp);
  189. list_sort(tp->t_mountp, &dfp->dfp_work,
  190. dfp->dfp_type->diff_items);
  191. list_for_each(li, &dfp->dfp_work)
  192. dfp->dfp_type->log_item(tp, dfp->dfp_intent, li);
  193. }
  194. }
  195. /* Abort all the intents that were committed. */
  196. STATIC void
  197. xfs_defer_trans_abort(
  198. struct xfs_trans *tp,
  199. struct list_head *dop_pending)
  200. {
  201. struct xfs_defer_pending *dfp;
  202. trace_xfs_defer_trans_abort(tp, _RET_IP_);
  203. /* Abort intent items that don't have a done item. */
  204. list_for_each_entry(dfp, dop_pending, dfp_list) {
  205. trace_xfs_defer_pending_abort(tp->t_mountp, dfp);
  206. if (dfp->dfp_intent && !dfp->dfp_done) {
  207. dfp->dfp_type->abort_intent(dfp->dfp_intent);
  208. dfp->dfp_intent = NULL;
  209. }
  210. }
  211. }
  212. /* Roll a transaction so we can do some deferred op processing. */
  213. STATIC int
  214. xfs_defer_trans_roll(
  215. struct xfs_trans **tpp)
  216. {
  217. struct xfs_trans *tp = *tpp;
  218. struct xfs_buf_log_item *bli;
  219. struct xfs_inode_log_item *ili;
  220. struct xfs_log_item *lip;
  221. struct xfs_buf *bplist[XFS_DEFER_OPS_NR_BUFS];
  222. struct xfs_inode *iplist[XFS_DEFER_OPS_NR_INODES];
  223. int bpcount = 0, ipcount = 0;
  224. int i;
  225. int error;
  226. list_for_each_entry(lip, &tp->t_items, li_trans) {
  227. switch (lip->li_type) {
  228. case XFS_LI_BUF:
  229. bli = container_of(lip, struct xfs_buf_log_item,
  230. bli_item);
  231. if (bli->bli_flags & XFS_BLI_HOLD) {
  232. if (bpcount >= XFS_DEFER_OPS_NR_BUFS) {
  233. ASSERT(0);
  234. return -EFSCORRUPTED;
  235. }
  236. xfs_trans_dirty_buf(tp, bli->bli_buf);
  237. bplist[bpcount++] = bli->bli_buf;
  238. }
  239. break;
  240. case XFS_LI_INODE:
  241. ili = container_of(lip, struct xfs_inode_log_item,
  242. ili_item);
  243. if (ili->ili_lock_flags == 0) {
  244. if (ipcount >= XFS_DEFER_OPS_NR_INODES) {
  245. ASSERT(0);
  246. return -EFSCORRUPTED;
  247. }
  248. xfs_trans_log_inode(tp, ili->ili_inode,
  249. XFS_ILOG_CORE);
  250. iplist[ipcount++] = ili->ili_inode;
  251. }
  252. break;
  253. default:
  254. break;
  255. }
  256. }
  257. trace_xfs_defer_trans_roll(tp, _RET_IP_);
  258. /*
  259. * Roll the transaction. Rolling always given a new transaction (even
  260. * if committing the old one fails!) to hand back to the caller, so we
  261. * join the held resources to the new transaction so that we always
  262. * return with the held resources joined to @tpp, no matter what
  263. * happened.
  264. */
  265. error = xfs_trans_roll(tpp);
  266. tp = *tpp;
  267. /* Rejoin the joined inodes. */
  268. for (i = 0; i < ipcount; i++)
  269. xfs_trans_ijoin(tp, iplist[i], 0);
  270. /* Rejoin the buffers and dirty them so the log moves forward. */
  271. for (i = 0; i < bpcount; i++) {
  272. xfs_trans_bjoin(tp, bplist[i]);
  273. xfs_trans_bhold(tp, bplist[i]);
  274. }
  275. if (error)
  276. trace_xfs_defer_trans_roll_error(tp, error);
  277. return error;
  278. }
  279. /*
  280. * Reset an already used dfops after finish.
  281. */
  282. static void
  283. xfs_defer_reset(
  284. struct xfs_trans *tp)
  285. {
  286. ASSERT(list_empty(&tp->t_dfops));
  287. /*
  288. * Low mode state transfers across transaction rolls to mirror dfops
  289. * lifetime. Clear it now that dfops is reset.
  290. */
  291. tp->t_flags &= ~XFS_TRANS_LOWMODE;
  292. }
  293. /*
  294. * Free up any items left in the list.
  295. */
  296. static void
  297. xfs_defer_cancel_list(
  298. struct xfs_mount *mp,
  299. struct list_head *dop_list)
  300. {
  301. struct xfs_defer_pending *dfp;
  302. struct xfs_defer_pending *pli;
  303. struct list_head *pwi;
  304. struct list_head *n;
  305. /*
  306. * Free the pending items. Caller should already have arranged
  307. * for the intent items to be released.
  308. */
  309. list_for_each_entry_safe(dfp, pli, dop_list, dfp_list) {
  310. trace_xfs_defer_cancel_list(mp, dfp);
  311. list_del(&dfp->dfp_list);
  312. list_for_each_safe(pwi, n, &dfp->dfp_work) {
  313. list_del(pwi);
  314. dfp->dfp_count--;
  315. dfp->dfp_type->cancel_item(pwi);
  316. }
  317. ASSERT(dfp->dfp_count == 0);
  318. kmem_free(dfp);
  319. }
  320. }
  321. /*
  322. * Finish all the pending work. This involves logging intent items for
  323. * any work items that wandered in since the last transaction roll (if
  324. * one has even happened), rolling the transaction, and finishing the
  325. * work items in the first item on the logged-and-pending list.
  326. *
  327. * If an inode is provided, relog it to the new transaction.
  328. */
  329. int
  330. xfs_defer_finish_noroll(
  331. struct xfs_trans **tp)
  332. {
  333. struct xfs_defer_pending *dfp;
  334. struct list_head *li;
  335. struct list_head *n;
  336. void *state;
  337. int error = 0;
  338. void (*cleanup_fn)(struct xfs_trans *, void *, int);
  339. LIST_HEAD(dop_pending);
  340. ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
  341. trace_xfs_defer_finish(*tp, _RET_IP_);
  342. /* Until we run out of pending work to finish... */
  343. while (!list_empty(&dop_pending) || !list_empty(&(*tp)->t_dfops)) {
  344. /* log intents and pull in intake items */
  345. xfs_defer_create_intents(*tp);
  346. list_splice_tail_init(&(*tp)->t_dfops, &dop_pending);
  347. /*
  348. * Roll the transaction.
  349. */
  350. error = xfs_defer_trans_roll(tp);
  351. if (error)
  352. goto out;
  353. /* Log an intent-done item for the first pending item. */
  354. dfp = list_first_entry(&dop_pending, struct xfs_defer_pending,
  355. dfp_list);
  356. trace_xfs_defer_pending_finish((*tp)->t_mountp, dfp);
  357. dfp->dfp_done = dfp->dfp_type->create_done(*tp, dfp->dfp_intent,
  358. dfp->dfp_count);
  359. cleanup_fn = dfp->dfp_type->finish_cleanup;
  360. /* Finish the work items. */
  361. state = NULL;
  362. list_for_each_safe(li, n, &dfp->dfp_work) {
  363. list_del(li);
  364. dfp->dfp_count--;
  365. error = dfp->dfp_type->finish_item(*tp, li,
  366. dfp->dfp_done, &state);
  367. if (error == -EAGAIN) {
  368. /*
  369. * Caller wants a fresh transaction;
  370. * put the work item back on the list
  371. * and jump out.
  372. */
  373. list_add(li, &dfp->dfp_work);
  374. dfp->dfp_count++;
  375. break;
  376. } else if (error) {
  377. /*
  378. * Clean up after ourselves and jump out.
  379. * xfs_defer_cancel will take care of freeing
  380. * all these lists and stuff.
  381. */
  382. if (cleanup_fn)
  383. cleanup_fn(*tp, state, error);
  384. goto out;
  385. }
  386. }
  387. if (error == -EAGAIN) {
  388. /*
  389. * Caller wants a fresh transaction, so log a
  390. * new log intent item to replace the old one
  391. * and roll the transaction. See "Requesting
  392. * a Fresh Transaction while Finishing
  393. * Deferred Work" above.
  394. */
  395. dfp->dfp_intent = dfp->dfp_type->create_intent(*tp,
  396. dfp->dfp_count);
  397. dfp->dfp_done = NULL;
  398. list_for_each(li, &dfp->dfp_work)
  399. dfp->dfp_type->log_item(*tp, dfp->dfp_intent,
  400. li);
  401. } else {
  402. /* Done with the dfp, free it. */
  403. list_del(&dfp->dfp_list);
  404. kmem_free(dfp);
  405. }
  406. if (cleanup_fn)
  407. cleanup_fn(*tp, state, error);
  408. }
  409. out:
  410. if (error) {
  411. xfs_defer_trans_abort(*tp, &dop_pending);
  412. xfs_force_shutdown((*tp)->t_mountp, SHUTDOWN_CORRUPT_INCORE);
  413. trace_xfs_defer_finish_error(*tp, error);
  414. xfs_defer_cancel_list((*tp)->t_mountp, &dop_pending);
  415. xfs_defer_cancel(*tp);
  416. return error;
  417. }
  418. trace_xfs_defer_finish_done(*tp, _RET_IP_);
  419. return 0;
  420. }
  421. int
  422. xfs_defer_finish(
  423. struct xfs_trans **tp)
  424. {
  425. int error;
  426. /*
  427. * Finish and roll the transaction once more to avoid returning to the
  428. * caller with a dirty transaction.
  429. */
  430. error = xfs_defer_finish_noroll(tp);
  431. if (error)
  432. return error;
  433. if ((*tp)->t_flags & XFS_TRANS_DIRTY) {
  434. error = xfs_defer_trans_roll(tp);
  435. if (error) {
  436. xfs_force_shutdown((*tp)->t_mountp,
  437. SHUTDOWN_CORRUPT_INCORE);
  438. return error;
  439. }
  440. }
  441. xfs_defer_reset(*tp);
  442. return 0;
  443. }
  444. void
  445. xfs_defer_cancel(
  446. struct xfs_trans *tp)
  447. {
  448. struct xfs_mount *mp = tp->t_mountp;
  449. trace_xfs_defer_cancel(tp, _RET_IP_);
  450. xfs_defer_cancel_list(mp, &tp->t_dfops);
  451. }
  452. /* Add an item for later deferred processing. */
  453. void
  454. xfs_defer_add(
  455. struct xfs_trans *tp,
  456. enum xfs_defer_ops_type type,
  457. struct list_head *li)
  458. {
  459. struct xfs_defer_pending *dfp = NULL;
  460. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  461. /*
  462. * Add the item to a pending item at the end of the intake list.
  463. * If the last pending item has the same type, reuse it. Else,
  464. * create a new pending item at the end of the intake list.
  465. */
  466. if (!list_empty(&tp->t_dfops)) {
  467. dfp = list_last_entry(&tp->t_dfops,
  468. struct xfs_defer_pending, dfp_list);
  469. if (dfp->dfp_type->type != type ||
  470. (dfp->dfp_type->max_items &&
  471. dfp->dfp_count >= dfp->dfp_type->max_items))
  472. dfp = NULL;
  473. }
  474. if (!dfp) {
  475. dfp = kmem_alloc(sizeof(struct xfs_defer_pending),
  476. KM_SLEEP | KM_NOFS);
  477. dfp->dfp_type = defer_op_types[type];
  478. dfp->dfp_intent = NULL;
  479. dfp->dfp_done = NULL;
  480. dfp->dfp_count = 0;
  481. INIT_LIST_HEAD(&dfp->dfp_work);
  482. list_add_tail(&dfp->dfp_list, &tp->t_dfops);
  483. }
  484. list_add_tail(li, &dfp->dfp_work);
  485. dfp->dfp_count++;
  486. }
  487. /* Initialize a deferred operation list. */
  488. void
  489. xfs_defer_init_op_type(
  490. const struct xfs_defer_op_type *type)
  491. {
  492. defer_op_types[type->type] = type;
  493. }
  494. /*
  495. * Move deferred ops from one transaction to another and reset the source to
  496. * initial state. This is primarily used to carry state forward across
  497. * transaction rolls with pending dfops.
  498. */
  499. void
  500. xfs_defer_move(
  501. struct xfs_trans *dtp,
  502. struct xfs_trans *stp)
  503. {
  504. list_splice_init(&stp->t_dfops, &dtp->t_dfops);
  505. /*
  506. * Low free space mode was historically controlled by a dfops field.
  507. * This meant that low mode state potentially carried across multiple
  508. * transaction rolls. Transfer low mode on a dfops move to preserve
  509. * that behavior.
  510. */
  511. dtp->t_flags |= (stp->t_flags & XFS_TRANS_LOWMODE);
  512. xfs_defer_reset(stp);
  513. }