xfs_defer.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. struct xfs_inode *ip)
  235. {
  236. int i;
  237. int error;
  238. /* Log all the joined inodes except the one we passed in. */
  239. for (i = 0; i < XFS_DEFER_OPS_NR_INODES && dop->dop_inodes[i]; i++) {
  240. if (dop->dop_inodes[i] == ip)
  241. continue;
  242. xfs_trans_log_inode(*tp, dop->dop_inodes[i], XFS_ILOG_CORE);
  243. }
  244. trace_xfs_defer_trans_roll((*tp)->t_mountp, dop);
  245. /* Roll the transaction. */
  246. error = xfs_trans_roll(tp, ip);
  247. if (error) {
  248. trace_xfs_defer_trans_roll_error((*tp)->t_mountp, dop, error);
  249. xfs_defer_trans_abort(*tp, dop, error);
  250. return error;
  251. }
  252. dop->dop_committed = true;
  253. /* Rejoin the joined inodes except the one we passed in. */
  254. for (i = 0; i < XFS_DEFER_OPS_NR_INODES && dop->dop_inodes[i]; i++) {
  255. if (dop->dop_inodes[i] == ip)
  256. continue;
  257. xfs_trans_ijoin(*tp, dop->dop_inodes[i], 0);
  258. }
  259. return error;
  260. }
  261. /* Do we have any work items to finish? */
  262. bool
  263. xfs_defer_has_unfinished_work(
  264. struct xfs_defer_ops *dop)
  265. {
  266. return !list_empty(&dop->dop_pending) || !list_empty(&dop->dop_intake);
  267. }
  268. /*
  269. * Add this inode to the deferred op. Each joined inode is relogged
  270. * each time we roll the transaction, in addition to any inode passed
  271. * to xfs_defer_finish().
  272. */
  273. int
  274. xfs_defer_join(
  275. struct xfs_defer_ops *dop,
  276. struct xfs_inode *ip)
  277. {
  278. int i;
  279. for (i = 0; i < XFS_DEFER_OPS_NR_INODES; i++) {
  280. if (dop->dop_inodes[i] == ip)
  281. return 0;
  282. else if (dop->dop_inodes[i] == NULL) {
  283. dop->dop_inodes[i] = ip;
  284. return 0;
  285. }
  286. }
  287. return -EFSCORRUPTED;
  288. }
  289. /*
  290. * Finish all the pending work. This involves logging intent items for
  291. * any work items that wandered in since the last transaction roll (if
  292. * one has even happened), rolling the transaction, and finishing the
  293. * work items in the first item on the logged-and-pending list.
  294. *
  295. * If an inode is provided, relog it to the new transaction.
  296. */
  297. int
  298. xfs_defer_finish(
  299. struct xfs_trans **tp,
  300. struct xfs_defer_ops *dop,
  301. struct xfs_inode *ip)
  302. {
  303. struct xfs_defer_pending *dfp;
  304. struct list_head *li;
  305. struct list_head *n;
  306. void *state;
  307. int error = 0;
  308. void (*cleanup_fn)(struct xfs_trans *, void *, int);
  309. ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
  310. trace_xfs_defer_finish((*tp)->t_mountp, dop);
  311. /* Until we run out of pending work to finish... */
  312. while (xfs_defer_has_unfinished_work(dop)) {
  313. /* Log intents for work items sitting in the intake. */
  314. xfs_defer_intake_work(*tp, dop);
  315. /* Roll the transaction. */
  316. error = xfs_defer_trans_roll(tp, dop, ip);
  317. if (error)
  318. goto out;
  319. /* Log an intent-done item for the first pending item. */
  320. dfp = list_first_entry(&dop->dop_pending,
  321. struct xfs_defer_pending, dfp_list);
  322. trace_xfs_defer_pending_finish((*tp)->t_mountp, dfp);
  323. dfp->dfp_done = dfp->dfp_type->create_done(*tp, dfp->dfp_intent,
  324. dfp->dfp_count);
  325. cleanup_fn = dfp->dfp_type->finish_cleanup;
  326. /* Finish the work items. */
  327. state = NULL;
  328. list_for_each_safe(li, n, &dfp->dfp_work) {
  329. list_del(li);
  330. dfp->dfp_count--;
  331. error = dfp->dfp_type->finish_item(*tp, dop, li,
  332. dfp->dfp_done, &state);
  333. if (error == -EAGAIN) {
  334. /*
  335. * Caller wants a fresh transaction;
  336. * put the work item back on the list
  337. * and jump out.
  338. */
  339. list_add(li, &dfp->dfp_work);
  340. dfp->dfp_count++;
  341. break;
  342. } else if (error) {
  343. /*
  344. * Clean up after ourselves and jump out.
  345. * xfs_defer_cancel will take care of freeing
  346. * all these lists and stuff.
  347. */
  348. if (cleanup_fn)
  349. cleanup_fn(*tp, state, error);
  350. xfs_defer_trans_abort(*tp, dop, error);
  351. goto out;
  352. }
  353. }
  354. if (error == -EAGAIN) {
  355. /*
  356. * Caller wants a fresh transaction, so log a
  357. * new log intent item to replace the old one
  358. * and roll the transaction. See "Requesting
  359. * a Fresh Transaction while Finishing
  360. * Deferred Work" above.
  361. */
  362. dfp->dfp_intent = dfp->dfp_type->create_intent(*tp,
  363. dfp->dfp_count);
  364. dfp->dfp_done = NULL;
  365. list_for_each(li, &dfp->dfp_work)
  366. dfp->dfp_type->log_item(*tp, dfp->dfp_intent,
  367. li);
  368. } else {
  369. /* Done with the dfp, free it. */
  370. list_del(&dfp->dfp_list);
  371. kmem_free(dfp);
  372. }
  373. if (cleanup_fn)
  374. cleanup_fn(*tp, state, error);
  375. }
  376. out:
  377. if (error)
  378. trace_xfs_defer_finish_error((*tp)->t_mountp, dop, error);
  379. else
  380. trace_xfs_defer_finish_done((*tp)->t_mountp, dop);
  381. return error;
  382. }
  383. /*
  384. * Free up any items left in the list.
  385. */
  386. void
  387. xfs_defer_cancel(
  388. struct xfs_defer_ops *dop)
  389. {
  390. struct xfs_defer_pending *dfp;
  391. struct xfs_defer_pending *pli;
  392. struct list_head *pwi;
  393. struct list_head *n;
  394. trace_xfs_defer_cancel(NULL, dop);
  395. /*
  396. * Free the pending items. Caller should already have arranged
  397. * for the intent items to be released.
  398. */
  399. list_for_each_entry_safe(dfp, pli, &dop->dop_intake, dfp_list) {
  400. trace_xfs_defer_intake_cancel(NULL, dfp);
  401. list_del(&dfp->dfp_list);
  402. list_for_each_safe(pwi, n, &dfp->dfp_work) {
  403. list_del(pwi);
  404. dfp->dfp_count--;
  405. dfp->dfp_type->cancel_item(pwi);
  406. }
  407. ASSERT(dfp->dfp_count == 0);
  408. kmem_free(dfp);
  409. }
  410. list_for_each_entry_safe(dfp, pli, &dop->dop_pending, dfp_list) {
  411. trace_xfs_defer_pending_cancel(NULL, dfp);
  412. list_del(&dfp->dfp_list);
  413. list_for_each_safe(pwi, n, &dfp->dfp_work) {
  414. list_del(pwi);
  415. dfp->dfp_count--;
  416. dfp->dfp_type->cancel_item(pwi);
  417. }
  418. ASSERT(dfp->dfp_count == 0);
  419. kmem_free(dfp);
  420. }
  421. }
  422. /* Add an item for later deferred processing. */
  423. void
  424. xfs_defer_add(
  425. struct xfs_defer_ops *dop,
  426. enum xfs_defer_ops_type type,
  427. struct list_head *li)
  428. {
  429. struct xfs_defer_pending *dfp = NULL;
  430. /*
  431. * Add the item to a pending item at the end of the intake list.
  432. * If the last pending item has the same type, reuse it. Else,
  433. * create a new pending item at the end of the intake list.
  434. */
  435. if (!list_empty(&dop->dop_intake)) {
  436. dfp = list_last_entry(&dop->dop_intake,
  437. struct xfs_defer_pending, dfp_list);
  438. if (dfp->dfp_type->type != type ||
  439. (dfp->dfp_type->max_items &&
  440. dfp->dfp_count >= dfp->dfp_type->max_items))
  441. dfp = NULL;
  442. }
  443. if (!dfp) {
  444. dfp = kmem_alloc(sizeof(struct xfs_defer_pending),
  445. KM_SLEEP | KM_NOFS);
  446. dfp->dfp_type = defer_op_types[type];
  447. dfp->dfp_intent = NULL;
  448. dfp->dfp_done = NULL;
  449. dfp->dfp_count = 0;
  450. INIT_LIST_HEAD(&dfp->dfp_work);
  451. list_add_tail(&dfp->dfp_list, &dop->dop_intake);
  452. }
  453. list_add_tail(li, &dfp->dfp_work);
  454. dfp->dfp_count++;
  455. }
  456. /* Initialize a deferred operation list. */
  457. void
  458. xfs_defer_init_op_type(
  459. const struct xfs_defer_op_type *type)
  460. {
  461. defer_op_types[type->type] = type;
  462. }
  463. /* Initialize a deferred operation. */
  464. void
  465. xfs_defer_init(
  466. struct xfs_defer_ops *dop,
  467. xfs_fsblock_t *fbp)
  468. {
  469. dop->dop_committed = false;
  470. dop->dop_low = false;
  471. memset(&dop->dop_inodes, 0, sizeof(dop->dop_inodes));
  472. *fbp = NULLFSBLOCK;
  473. INIT_LIST_HEAD(&dop->dop_intake);
  474. INIT_LIST_HEAD(&dop->dop_pending);
  475. trace_xfs_defer_init(NULL, dop);
  476. }