xfs_defer.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. * Copyright (C) 2016 Oracle. All Rights Reserved.
  3. *
  4. * Author: Darrick J. Wong <darrick.wong@oracle.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it would be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write the Free Software Foundation,
  18. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. #include "xfs.h"
  21. #include "xfs_fs.h"
  22. #include "xfs_shared.h"
  23. #include "xfs_format.h"
  24. #include "xfs_log_format.h"
  25. #include "xfs_trans_resv.h"
  26. #include "xfs_bit.h"
  27. #include "xfs_sb.h"
  28. #include "xfs_mount.h"
  29. #include "xfs_defer.h"
  30. #include "xfs_trans.h"
  31. #include "xfs_trace.h"
  32. /*
  33. * Deferred Operations in XFS
  34. *
  35. * Due to the way locking rules work in XFS, certain transactions (block
  36. * mapping and unmapping, typically) have permanent reservations so that
  37. * we can roll the transaction to adhere to AG locking order rules and
  38. * to unlock buffers between metadata updates. Prior to rmap/reflink,
  39. * the mapping code had a mechanism to perform these deferrals for
  40. * extents that were going to be freed; this code makes that facility
  41. * more generic.
  42. *
  43. * When adding the reverse mapping and reflink features, it became
  44. * necessary to perform complex remapping multi-transactions to comply
  45. * with AG locking order rules, and to be able to spread a single
  46. * refcount update operation (an operation on an n-block extent can
  47. * update as many as n records!) among multiple transactions. XFS can
  48. * roll a transaction to facilitate this, but using this facility
  49. * requires us to log "intent" items in case log recovery needs to
  50. * redo the operation, and to log "done" items to indicate that redo
  51. * is not necessary.
  52. *
  53. * Deferred work is tracked in xfs_defer_pending items. Each pending
  54. * item tracks one type of deferred work. Incoming work items (which
  55. * have not yet had an intent logged) are attached to a pending item
  56. * on the dop_intake list, where they wait for the caller to finish
  57. * the deferred operations.
  58. *
  59. * Finishing a set of deferred operations is an involved process. To
  60. * start, we define "rolling a deferred-op transaction" as follows:
  61. *
  62. * > For each xfs_defer_pending item on the dop_intake list,
  63. * - Sort the work items in AG order. XFS locking
  64. * order rules require us to lock buffers in AG order.
  65. * - Create a log intent item for that type.
  66. * - Attach it to the pending item.
  67. * - Move the pending item from the dop_intake list to the
  68. * dop_pending list.
  69. * > Roll the transaction.
  70. *
  71. * NOTE: To avoid exceeding the transaction reservation, we limit the
  72. * number of items that we attach to a given xfs_defer_pending.
  73. *
  74. * The actual finishing process looks like this:
  75. *
  76. * > For each xfs_defer_pending in the dop_pending list,
  77. * - Roll the deferred-op transaction as above.
  78. * - Create a log done item for that type, and attach it to the
  79. * log intent item.
  80. * - For each work item attached to the log intent item,
  81. * * Perform the described action.
  82. * * Attach the work item to the log done item.
  83. * * If the result of doing the work was -EAGAIN, ->finish work
  84. * wants a new transaction. See the "Requesting a Fresh
  85. * Transaction while Finishing Deferred Work" section below for
  86. * details.
  87. *
  88. * The key here is that we must log an intent item for all pending
  89. * work items every time we roll the transaction, and that we must log
  90. * a done item as soon as the work is completed. With this mechanism
  91. * we can perform complex remapping operations, chaining intent items
  92. * as needed.
  93. *
  94. * Requesting a Fresh Transaction while Finishing Deferred Work
  95. *
  96. * If ->finish_item decides that it needs a fresh transaction to
  97. * finish the work, it must ask its caller (xfs_defer_finish) for a
  98. * continuation. The most likely cause of this circumstance are the
  99. * refcount adjust functions deciding that they've logged enough items
  100. * to be at risk of exceeding the transaction reservation.
  101. *
  102. * To get a fresh transaction, we want to log the existing log done
  103. * item to prevent the log intent item from replaying, immediately log
  104. * a new log intent item with the unfinished work items, roll the
  105. * transaction, and re-call ->finish_item wherever it left off. The
  106. * log done item and the new log intent item must be in the same
  107. * transaction or atomicity cannot be guaranteed; defer_finish ensures
  108. * that this happens.
  109. *
  110. * This requires some coordination between ->finish_item and
  111. * defer_finish. Upon deciding to request a new transaction,
  112. * ->finish_item should update the current work item to reflect the
  113. * unfinished work. Next, it should reset the log done item's list
  114. * count to the number of items finished, and return -EAGAIN.
  115. * defer_finish sees the -EAGAIN, logs the new log intent item
  116. * with the remaining work items, and leaves the xfs_defer_pending
  117. * item at the head of the dop_work queue. Then it rolls the
  118. * transaction and picks up processing where it left off. It is
  119. * required that ->finish_item must be careful to leave enough
  120. * transaction reservation to fit the new log intent item.
  121. *
  122. * This is an example of remapping the extent (E, E+B) into file X at
  123. * offset A and dealing with the extent (C, C+B) already being mapped
  124. * there:
  125. * +-------------------------------------------------+
  126. * | Unmap file X startblock C offset A length B | t0
  127. * | Intent to reduce refcount for extent (C, B) |
  128. * | Intent to remove rmap (X, C, A, B) |
  129. * | Intent to free extent (D, 1) (bmbt block) |
  130. * | Intent to map (X, A, B) at startblock E |
  131. * +-------------------------------------------------+
  132. * | Map file X startblock E offset A length B | t1
  133. * | Done mapping (X, E, A, B) |
  134. * | Intent to increase refcount for extent (E, B) |
  135. * | Intent to add rmap (X, E, A, B) |
  136. * +-------------------------------------------------+
  137. * | Reduce refcount for extent (C, B) | t2
  138. * | Done reducing refcount for extent (C, 9) |
  139. * | Intent to reduce refcount for extent (C+9, B-9) |
  140. * | (ran out of space after 9 refcount updates) |
  141. * +-------------------------------------------------+
  142. * | Reduce refcount for extent (C+9, B+9) | t3
  143. * | Done reducing refcount for extent (C+9, B-9) |
  144. * | Increase refcount for extent (E, B) |
  145. * | Done increasing refcount for extent (E, B) |
  146. * | Intent to free extent (C, B) |
  147. * | Intent to free extent (F, 1) (refcountbt block) |
  148. * | Intent to remove rmap (F, 1, REFC) |
  149. * +-------------------------------------------------+
  150. * | Remove rmap (X, C, A, B) | t4
  151. * | Done removing rmap (X, C, A, B) |
  152. * | Add rmap (X, E, A, B) |
  153. * | Done adding rmap (X, E, A, B) |
  154. * | Remove rmap (F, 1, REFC) |
  155. * | Done removing rmap (F, 1, REFC) |
  156. * +-------------------------------------------------+
  157. * | Free extent (C, B) | t5
  158. * | Done freeing extent (C, B) |
  159. * | Free extent (D, 1) |
  160. * | Done freeing extent (D, 1) |
  161. * | Free extent (F, 1) |
  162. * | Done freeing extent (F, 1) |
  163. * +-------------------------------------------------+
  164. *
  165. * If we should crash before t2 commits, log recovery replays
  166. * the following intent items:
  167. *
  168. * - Intent to reduce refcount for extent (C, B)
  169. * - Intent to remove rmap (X, C, A, B)
  170. * - Intent to free extent (D, 1) (bmbt block)
  171. * - Intent to increase refcount for extent (E, B)
  172. * - Intent to add rmap (X, E, A, B)
  173. *
  174. * In the process of recovering, it should also generate and take care
  175. * of these intent items:
  176. *
  177. * - Intent to free extent (C, B)
  178. * - Intent to free extent (F, 1) (refcountbt block)
  179. * - Intent to remove rmap (F, 1, REFC)
  180. *
  181. * Note that the continuation requested between t2 and t3 is likely to
  182. * reoccur.
  183. */
  184. static const struct xfs_defer_op_type *defer_op_types[XFS_DEFER_OPS_TYPE_MAX];
  185. /*
  186. * For each pending item in the intake list, log its intent item and the
  187. * associated extents, then add the entire intake list to the end of
  188. * the pending list.
  189. */
  190. STATIC void
  191. xfs_defer_intake_work(
  192. struct xfs_trans *tp,
  193. struct xfs_defer_ops *dop)
  194. {
  195. struct list_head *li;
  196. struct xfs_defer_pending *dfp;
  197. list_for_each_entry(dfp, &dop->dop_intake, dfp_list) {
  198. dfp->dfp_intent = dfp->dfp_type->create_intent(tp,
  199. dfp->dfp_count);
  200. trace_xfs_defer_intake_work(tp->t_mountp, dfp);
  201. list_sort(tp->t_mountp, &dfp->dfp_work,
  202. dfp->dfp_type->diff_items);
  203. list_for_each(li, &dfp->dfp_work)
  204. dfp->dfp_type->log_item(tp, dfp->dfp_intent, li);
  205. }
  206. list_splice_tail_init(&dop->dop_intake, &dop->dop_pending);
  207. }
  208. /* Abort all the intents that were committed. */
  209. STATIC void
  210. xfs_defer_trans_abort(
  211. struct xfs_trans *tp,
  212. struct xfs_defer_ops *dop,
  213. int error)
  214. {
  215. struct xfs_defer_pending *dfp;
  216. trace_xfs_defer_trans_abort(tp->t_mountp, dop);
  217. /* Abort intent items that don't have a done item. */
  218. list_for_each_entry(dfp, &dop->dop_pending, dfp_list) {
  219. trace_xfs_defer_pending_abort(tp->t_mountp, dfp);
  220. if (dfp->dfp_intent && !dfp->dfp_done) {
  221. dfp->dfp_type->abort_intent(dfp->dfp_intent);
  222. dfp->dfp_intent = NULL;
  223. }
  224. }
  225. /* Shut down FS. */
  226. xfs_force_shutdown(tp->t_mountp, (error == -EFSCORRUPTED) ?
  227. SHUTDOWN_CORRUPT_INCORE : SHUTDOWN_META_IO_ERROR);
  228. }
  229. /* Roll a transaction so we can do some deferred op processing. */
  230. STATIC int
  231. xfs_defer_trans_roll(
  232. struct xfs_trans **tp,
  233. struct xfs_defer_ops *dop)
  234. {
  235. int i;
  236. int error;
  237. /* Log all the joined inodes. */
  238. for (i = 0; i < XFS_DEFER_OPS_NR_INODES && dop->dop_inodes[i]; i++)
  239. xfs_trans_log_inode(*tp, dop->dop_inodes[i], XFS_ILOG_CORE);
  240. trace_xfs_defer_trans_roll((*tp)->t_mountp, dop);
  241. /* Roll the transaction. */
  242. error = xfs_trans_roll(tp);
  243. if (error) {
  244. trace_xfs_defer_trans_roll_error((*tp)->t_mountp, dop, error);
  245. xfs_defer_trans_abort(*tp, dop, error);
  246. return error;
  247. }
  248. dop->dop_committed = true;
  249. /* Rejoin the joined inodes. */
  250. for (i = 0; i < XFS_DEFER_OPS_NR_INODES && dop->dop_inodes[i]; i++)
  251. xfs_trans_ijoin(*tp, dop->dop_inodes[i], 0);
  252. return error;
  253. }
  254. /* Do we have any work items to finish? */
  255. bool
  256. xfs_defer_has_unfinished_work(
  257. struct xfs_defer_ops *dop)
  258. {
  259. return !list_empty(&dop->dop_pending) || !list_empty(&dop->dop_intake);
  260. }
  261. /*
  262. * Add this inode to the deferred op. Each joined inode is relogged
  263. * each time we roll the transaction.
  264. */
  265. int
  266. xfs_defer_ijoin(
  267. struct xfs_defer_ops *dop,
  268. struct xfs_inode *ip)
  269. {
  270. int i;
  271. for (i = 0; i < XFS_DEFER_OPS_NR_INODES; i++) {
  272. if (dop->dop_inodes[i] == ip)
  273. return 0;
  274. else if (dop->dop_inodes[i] == NULL) {
  275. dop->dop_inodes[i] = ip;
  276. return 0;
  277. }
  278. }
  279. return -EFSCORRUPTED;
  280. }
  281. /*
  282. * Finish all the pending work. This involves logging intent items for
  283. * any work items that wandered in since the last transaction roll (if
  284. * one has even happened), rolling the transaction, and finishing the
  285. * work items in the first item on the logged-and-pending list.
  286. *
  287. * If an inode is provided, relog it to the new transaction.
  288. */
  289. int
  290. xfs_defer_finish(
  291. struct xfs_trans **tp,
  292. struct xfs_defer_ops *dop)
  293. {
  294. struct xfs_defer_pending *dfp;
  295. struct list_head *li;
  296. struct list_head *n;
  297. void *state;
  298. int error = 0;
  299. void (*cleanup_fn)(struct xfs_trans *, void *, int);
  300. ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
  301. trace_xfs_defer_finish((*tp)->t_mountp, dop);
  302. /* Until we run out of pending work to finish... */
  303. while (xfs_defer_has_unfinished_work(dop)) {
  304. /* Log intents for work items sitting in the intake. */
  305. xfs_defer_intake_work(*tp, dop);
  306. /* Roll the transaction. */
  307. error = xfs_defer_trans_roll(tp, dop);
  308. if (error)
  309. goto out;
  310. /* Log an intent-done item for the first pending item. */
  311. dfp = list_first_entry(&dop->dop_pending,
  312. struct xfs_defer_pending, dfp_list);
  313. trace_xfs_defer_pending_finish((*tp)->t_mountp, dfp);
  314. dfp->dfp_done = dfp->dfp_type->create_done(*tp, dfp->dfp_intent,
  315. dfp->dfp_count);
  316. cleanup_fn = dfp->dfp_type->finish_cleanup;
  317. /* Finish the work items. */
  318. state = NULL;
  319. list_for_each_safe(li, n, &dfp->dfp_work) {
  320. list_del(li);
  321. dfp->dfp_count--;
  322. error = dfp->dfp_type->finish_item(*tp, dop, li,
  323. dfp->dfp_done, &state);
  324. if (error == -EAGAIN) {
  325. /*
  326. * Caller wants a fresh transaction;
  327. * put the work item back on the list
  328. * and jump out.
  329. */
  330. list_add(li, &dfp->dfp_work);
  331. dfp->dfp_count++;
  332. break;
  333. } else if (error) {
  334. /*
  335. * Clean up after ourselves and jump out.
  336. * xfs_defer_cancel will take care of freeing
  337. * all these lists and stuff.
  338. */
  339. if (cleanup_fn)
  340. cleanup_fn(*tp, state, error);
  341. xfs_defer_trans_abort(*tp, dop, error);
  342. goto out;
  343. }
  344. }
  345. if (error == -EAGAIN) {
  346. /*
  347. * Caller wants a fresh transaction, so log a
  348. * new log intent item to replace the old one
  349. * and roll the transaction. See "Requesting
  350. * a Fresh Transaction while Finishing
  351. * Deferred Work" above.
  352. */
  353. dfp->dfp_intent = dfp->dfp_type->create_intent(*tp,
  354. dfp->dfp_count);
  355. dfp->dfp_done = NULL;
  356. list_for_each(li, &dfp->dfp_work)
  357. dfp->dfp_type->log_item(*tp, dfp->dfp_intent,
  358. li);
  359. } else {
  360. /* Done with the dfp, free it. */
  361. list_del(&dfp->dfp_list);
  362. kmem_free(dfp);
  363. }
  364. if (cleanup_fn)
  365. cleanup_fn(*tp, state, error);
  366. }
  367. out:
  368. if (error)
  369. trace_xfs_defer_finish_error((*tp)->t_mountp, dop, error);
  370. else
  371. trace_xfs_defer_finish_done((*tp)->t_mountp, dop);
  372. return error;
  373. }
  374. /*
  375. * Free up any items left in the list.
  376. */
  377. void
  378. xfs_defer_cancel(
  379. struct xfs_defer_ops *dop)
  380. {
  381. struct xfs_defer_pending *dfp;
  382. struct xfs_defer_pending *pli;
  383. struct list_head *pwi;
  384. struct list_head *n;
  385. trace_xfs_defer_cancel(NULL, dop);
  386. /*
  387. * Free the pending items. Caller should already have arranged
  388. * for the intent items to be released.
  389. */
  390. list_for_each_entry_safe(dfp, pli, &dop->dop_intake, dfp_list) {
  391. trace_xfs_defer_intake_cancel(NULL, dfp);
  392. list_del(&dfp->dfp_list);
  393. list_for_each_safe(pwi, n, &dfp->dfp_work) {
  394. list_del(pwi);
  395. dfp->dfp_count--;
  396. dfp->dfp_type->cancel_item(pwi);
  397. }
  398. ASSERT(dfp->dfp_count == 0);
  399. kmem_free(dfp);
  400. }
  401. list_for_each_entry_safe(dfp, pli, &dop->dop_pending, dfp_list) {
  402. trace_xfs_defer_pending_cancel(NULL, dfp);
  403. list_del(&dfp->dfp_list);
  404. list_for_each_safe(pwi, n, &dfp->dfp_work) {
  405. list_del(pwi);
  406. dfp->dfp_count--;
  407. dfp->dfp_type->cancel_item(pwi);
  408. }
  409. ASSERT(dfp->dfp_count == 0);
  410. kmem_free(dfp);
  411. }
  412. }
  413. /* Add an item for later deferred processing. */
  414. void
  415. xfs_defer_add(
  416. struct xfs_defer_ops *dop,
  417. enum xfs_defer_ops_type type,
  418. struct list_head *li)
  419. {
  420. struct xfs_defer_pending *dfp = NULL;
  421. /*
  422. * Add the item to a pending item at the end of the intake list.
  423. * If the last pending item has the same type, reuse it. Else,
  424. * create a new pending item at the end of the intake list.
  425. */
  426. if (!list_empty(&dop->dop_intake)) {
  427. dfp = list_last_entry(&dop->dop_intake,
  428. struct xfs_defer_pending, dfp_list);
  429. if (dfp->dfp_type->type != type ||
  430. (dfp->dfp_type->max_items &&
  431. dfp->dfp_count >= dfp->dfp_type->max_items))
  432. dfp = NULL;
  433. }
  434. if (!dfp) {
  435. dfp = kmem_alloc(sizeof(struct xfs_defer_pending),
  436. KM_SLEEP | KM_NOFS);
  437. dfp->dfp_type = defer_op_types[type];
  438. dfp->dfp_intent = NULL;
  439. dfp->dfp_done = NULL;
  440. dfp->dfp_count = 0;
  441. INIT_LIST_HEAD(&dfp->dfp_work);
  442. list_add_tail(&dfp->dfp_list, &dop->dop_intake);
  443. }
  444. list_add_tail(li, &dfp->dfp_work);
  445. dfp->dfp_count++;
  446. }
  447. /* Initialize a deferred operation list. */
  448. void
  449. xfs_defer_init_op_type(
  450. const struct xfs_defer_op_type *type)
  451. {
  452. defer_op_types[type->type] = type;
  453. }
  454. /* Initialize a deferred operation. */
  455. void
  456. xfs_defer_init(
  457. struct xfs_defer_ops *dop,
  458. xfs_fsblock_t *fbp)
  459. {
  460. dop->dop_committed = false;
  461. dop->dop_low = false;
  462. memset(&dop->dop_inodes, 0, sizeof(dop->dop_inodes));
  463. *fbp = NULLFSBLOCK;
  464. INIT_LIST_HEAD(&dop->dop_intake);
  465. INIT_LIST_HEAD(&dop->dop_pending);
  466. trace_xfs_defer_init(NULL, dop);
  467. }