xfs_defer.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. /* Roll the transaction. */
  259. error = xfs_trans_roll(tpp);
  260. tp = *tpp;
  261. if (error) {
  262. trace_xfs_defer_trans_roll_error(tp, error);
  263. return error;
  264. }
  265. /* Rejoin the joined inodes. */
  266. for (i = 0; i < ipcount; i++)
  267. xfs_trans_ijoin(tp, iplist[i], 0);
  268. /* Rejoin the buffers and dirty them so the log moves forward. */
  269. for (i = 0; i < bpcount; i++) {
  270. xfs_trans_bjoin(tp, bplist[i]);
  271. xfs_trans_bhold(tp, bplist[i]);
  272. }
  273. return error;
  274. }
  275. /*
  276. * Reset an already used dfops after finish.
  277. */
  278. static void
  279. xfs_defer_reset(
  280. struct xfs_trans *tp)
  281. {
  282. ASSERT(list_empty(&tp->t_dfops));
  283. /*
  284. * Low mode state transfers across transaction rolls to mirror dfops
  285. * lifetime. Clear it now that dfops is reset.
  286. */
  287. tp->t_flags &= ~XFS_TRANS_LOWMODE;
  288. }
  289. /*
  290. * Free up any items left in the list.
  291. */
  292. static void
  293. xfs_defer_cancel_list(
  294. struct xfs_mount *mp,
  295. struct list_head *dop_list)
  296. {
  297. struct xfs_defer_pending *dfp;
  298. struct xfs_defer_pending *pli;
  299. struct list_head *pwi;
  300. struct list_head *n;
  301. /*
  302. * Free the pending items. Caller should already have arranged
  303. * for the intent items to be released.
  304. */
  305. list_for_each_entry_safe(dfp, pli, dop_list, dfp_list) {
  306. trace_xfs_defer_cancel_list(mp, dfp);
  307. list_del(&dfp->dfp_list);
  308. list_for_each_safe(pwi, n, &dfp->dfp_work) {
  309. list_del(pwi);
  310. dfp->dfp_count--;
  311. dfp->dfp_type->cancel_item(pwi);
  312. }
  313. ASSERT(dfp->dfp_count == 0);
  314. kmem_free(dfp);
  315. }
  316. }
  317. /*
  318. * Finish all the pending work. This involves logging intent items for
  319. * any work items that wandered in since the last transaction roll (if
  320. * one has even happened), rolling the transaction, and finishing the
  321. * work items in the first item on the logged-and-pending list.
  322. *
  323. * If an inode is provided, relog it to the new transaction.
  324. */
  325. int
  326. xfs_defer_finish_noroll(
  327. struct xfs_trans **tp)
  328. {
  329. struct xfs_defer_pending *dfp;
  330. struct list_head *li;
  331. struct list_head *n;
  332. void *state;
  333. int error = 0;
  334. void (*cleanup_fn)(struct xfs_trans *, void *, int);
  335. LIST_HEAD(dop_pending);
  336. ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
  337. trace_xfs_defer_finish(*tp, _RET_IP_);
  338. /* Until we run out of pending work to finish... */
  339. while (!list_empty(&dop_pending) || !list_empty(&(*tp)->t_dfops)) {
  340. /* log intents and pull in intake items */
  341. xfs_defer_create_intents(*tp);
  342. list_splice_tail_init(&(*tp)->t_dfops, &dop_pending);
  343. /*
  344. * Roll the transaction.
  345. */
  346. error = xfs_defer_trans_roll(tp);
  347. if (error)
  348. goto out;
  349. /* Log an intent-done item for the first pending item. */
  350. dfp = list_first_entry(&dop_pending, struct xfs_defer_pending,
  351. dfp_list);
  352. trace_xfs_defer_pending_finish((*tp)->t_mountp, dfp);
  353. dfp->dfp_done = dfp->dfp_type->create_done(*tp, dfp->dfp_intent,
  354. dfp->dfp_count);
  355. cleanup_fn = dfp->dfp_type->finish_cleanup;
  356. /* Finish the work items. */
  357. state = NULL;
  358. list_for_each_safe(li, n, &dfp->dfp_work) {
  359. list_del(li);
  360. dfp->dfp_count--;
  361. error = dfp->dfp_type->finish_item(*tp, li,
  362. dfp->dfp_done, &state);
  363. if (error == -EAGAIN) {
  364. /*
  365. * Caller wants a fresh transaction;
  366. * put the work item back on the list
  367. * and jump out.
  368. */
  369. list_add(li, &dfp->dfp_work);
  370. dfp->dfp_count++;
  371. break;
  372. } else if (error) {
  373. /*
  374. * Clean up after ourselves and jump out.
  375. * xfs_defer_cancel will take care of freeing
  376. * all these lists and stuff.
  377. */
  378. if (cleanup_fn)
  379. cleanup_fn(*tp, state, error);
  380. goto out;
  381. }
  382. }
  383. if (error == -EAGAIN) {
  384. /*
  385. * Caller wants a fresh transaction, so log a
  386. * new log intent item to replace the old one
  387. * and roll the transaction. See "Requesting
  388. * a Fresh Transaction while Finishing
  389. * Deferred Work" above.
  390. */
  391. dfp->dfp_intent = dfp->dfp_type->create_intent(*tp,
  392. dfp->dfp_count);
  393. dfp->dfp_done = NULL;
  394. list_for_each(li, &dfp->dfp_work)
  395. dfp->dfp_type->log_item(*tp, dfp->dfp_intent,
  396. li);
  397. } else {
  398. /* Done with the dfp, free it. */
  399. list_del(&dfp->dfp_list);
  400. kmem_free(dfp);
  401. }
  402. if (cleanup_fn)
  403. cleanup_fn(*tp, state, error);
  404. }
  405. out:
  406. if (error) {
  407. xfs_defer_trans_abort(*tp, &dop_pending);
  408. xfs_force_shutdown((*tp)->t_mountp, SHUTDOWN_CORRUPT_INCORE);
  409. trace_xfs_defer_finish_error(*tp, error);
  410. xfs_defer_cancel_list((*tp)->t_mountp, &dop_pending);
  411. xfs_defer_cancel(*tp);
  412. return error;
  413. }
  414. trace_xfs_defer_finish_done(*tp, _RET_IP_);
  415. return 0;
  416. }
  417. int
  418. xfs_defer_finish(
  419. struct xfs_trans **tp)
  420. {
  421. int error;
  422. /*
  423. * Finish and roll the transaction once more to avoid returning to the
  424. * caller with a dirty transaction.
  425. */
  426. error = xfs_defer_finish_noroll(tp);
  427. if (error)
  428. return error;
  429. if ((*tp)->t_flags & XFS_TRANS_DIRTY) {
  430. error = xfs_defer_trans_roll(tp);
  431. if (error) {
  432. xfs_force_shutdown((*tp)->t_mountp,
  433. SHUTDOWN_CORRUPT_INCORE);
  434. return error;
  435. }
  436. }
  437. xfs_defer_reset(*tp);
  438. return 0;
  439. }
  440. void
  441. xfs_defer_cancel(
  442. struct xfs_trans *tp)
  443. {
  444. struct xfs_mount *mp = tp->t_mountp;
  445. trace_xfs_defer_cancel(tp, _RET_IP_);
  446. xfs_defer_cancel_list(mp, &tp->t_dfops);
  447. }
  448. /* Add an item for later deferred processing. */
  449. void
  450. xfs_defer_add(
  451. struct xfs_trans *tp,
  452. enum xfs_defer_ops_type type,
  453. struct list_head *li)
  454. {
  455. struct xfs_defer_pending *dfp = NULL;
  456. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  457. /*
  458. * Add the item to a pending item at the end of the intake list.
  459. * If the last pending item has the same type, reuse it. Else,
  460. * create a new pending item at the end of the intake list.
  461. */
  462. if (!list_empty(&tp->t_dfops)) {
  463. dfp = list_last_entry(&tp->t_dfops,
  464. struct xfs_defer_pending, dfp_list);
  465. if (dfp->dfp_type->type != type ||
  466. (dfp->dfp_type->max_items &&
  467. dfp->dfp_count >= dfp->dfp_type->max_items))
  468. dfp = NULL;
  469. }
  470. if (!dfp) {
  471. dfp = kmem_alloc(sizeof(struct xfs_defer_pending),
  472. KM_SLEEP | KM_NOFS);
  473. dfp->dfp_type = defer_op_types[type];
  474. dfp->dfp_intent = NULL;
  475. dfp->dfp_done = NULL;
  476. dfp->dfp_count = 0;
  477. INIT_LIST_HEAD(&dfp->dfp_work);
  478. list_add_tail(&dfp->dfp_list, &tp->t_dfops);
  479. }
  480. list_add_tail(li, &dfp->dfp_work);
  481. dfp->dfp_count++;
  482. }
  483. /* Initialize a deferred operation list. */
  484. void
  485. xfs_defer_init_op_type(
  486. const struct xfs_defer_op_type *type)
  487. {
  488. defer_op_types[type->type] = type;
  489. }
  490. /*
  491. * Move deferred ops from one transaction to another and reset the source to
  492. * initial state. This is primarily used to carry state forward across
  493. * transaction rolls with pending dfops.
  494. */
  495. void
  496. xfs_defer_move(
  497. struct xfs_trans *dtp,
  498. struct xfs_trans *stp)
  499. {
  500. list_splice_init(&stp->t_dfops, &dtp->t_dfops);
  501. /*
  502. * Low free space mode was historically controlled by a dfops field.
  503. * This meant that low mode state potentially carried across multiple
  504. * transaction rolls. Transfer low mode on a dfops move to preserve
  505. * that behavior.
  506. */
  507. dtp->t_flags |= (stp->t_flags & XFS_TRANS_LOWMODE);
  508. xfs_defer_reset(stp);
  509. }