blob: 6c0da24c68c9bd1b2531c1fb34439607f670bd68 [file] [log] [blame]
Darrick J. Wong4e0cc292016-08-03 11:12:25 +10001/*
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/*
34 * Deferred Operations in XFS
35 *
36 * Due to the way locking rules work in XFS, certain transactions (block
37 * mapping and unmapping, typically) have permanent reservations so that
38 * we can roll the transaction to adhere to AG locking order rules and
39 * to unlock buffers between metadata updates. Prior to rmap/reflink,
40 * the mapping code had a mechanism to perform these deferrals for
41 * extents that were going to be freed; this code makes that facility
42 * more generic.
43 *
44 * When adding the reverse mapping and reflink features, it became
45 * necessary to perform complex remapping multi-transactions to comply
46 * with AG locking order rules, and to be able to spread a single
47 * refcount update operation (an operation on an n-block extent can
48 * update as many as n records!) among multiple transactions. XFS can
49 * roll a transaction to facilitate this, but using this facility
50 * requires us to log "intent" items in case log recovery needs to
51 * redo the operation, and to log "done" items to indicate that redo
52 * is not necessary.
53 *
54 * Deferred work is tracked in xfs_defer_pending items. Each pending
55 * item tracks one type of deferred work. Incoming work items (which
56 * have not yet had an intent logged) are attached to a pending item
57 * on the dop_intake list, where they wait for the caller to finish
58 * the deferred operations.
59 *
60 * Finishing a set of deferred operations is an involved process. To
61 * start, we define "rolling a deferred-op transaction" as follows:
62 *
63 * > For each xfs_defer_pending item on the dop_intake list,
64 * - Sort the work items in AG order. XFS locking
65 * order rules require us to lock buffers in AG order.
66 * - Create a log intent item for that type.
67 * - Attach it to the pending item.
68 * - Move the pending item from the dop_intake list to the
69 * dop_pending list.
70 * > Roll the transaction.
71 *
72 * NOTE: To avoid exceeding the transaction reservation, we limit the
73 * number of items that we attach to a given xfs_defer_pending.
74 *
75 * The actual finishing process looks like this:
76 *
77 * > For each xfs_defer_pending in the dop_pending list,
78 * - Roll the deferred-op transaction as above.
79 * - Create a log done item for that type, and attach it to the
80 * log intent item.
81 * - For each work item attached to the log intent item,
82 * * Perform the described action.
83 * * Attach the work item to the log done item.
Darrick J. Wong385d65582016-09-19 10:26:25 +100084 * * If the result of doing the work was -EAGAIN, ->finish work
85 * wants a new transaction. See the "Requesting a Fresh
86 * Transaction while Finishing Deferred Work" section below for
87 * details.
Darrick J. Wong4e0cc292016-08-03 11:12:25 +100088 *
89 * The key here is that we must log an intent item for all pending
90 * work items every time we roll the transaction, and that we must log
91 * a done item as soon as the work is completed. With this mechanism
92 * we can perform complex remapping operations, chaining intent items
93 * as needed.
94 *
Darrick J. Wong385d65582016-09-19 10:26:25 +100095 * Requesting a Fresh Transaction while Finishing Deferred Work
96 *
97 * If ->finish_item decides that it needs a fresh transaction to
98 * finish the work, it must ask its caller (xfs_defer_finish) for a
99 * continuation. The most likely cause of this circumstance are the
100 * refcount adjust functions deciding that they've logged enough items
101 * to be at risk of exceeding the transaction reservation.
102 *
103 * To get a fresh transaction, we want to log the existing log done
104 * item to prevent the log intent item from replaying, immediately log
105 * a new log intent item with the unfinished work items, roll the
106 * transaction, and re-call ->finish_item wherever it left off. The
107 * log done item and the new log intent item must be in the same
108 * transaction or atomicity cannot be guaranteed; defer_finish ensures
109 * that this happens.
110 *
111 * This requires some coordination between ->finish_item and
112 * defer_finish. Upon deciding to request a new transaction,
113 * ->finish_item should update the current work item to reflect the
114 * unfinished work. Next, it should reset the log done item's list
115 * count to the number of items finished, and return -EAGAIN.
116 * defer_finish sees the -EAGAIN, logs the new log intent item
117 * with the remaining work items, and leaves the xfs_defer_pending
118 * item at the head of the dop_work queue. Then it rolls the
119 * transaction and picks up processing where it left off. It is
120 * required that ->finish_item must be careful to leave enough
121 * transaction reservation to fit the new log intent item.
122 *
Darrick J. Wong4e0cc292016-08-03 11:12:25 +1000123 * This is an example of remapping the extent (E, E+B) into file X at
124 * offset A and dealing with the extent (C, C+B) already being mapped
125 * there:
126 * +-------------------------------------------------+
127 * | Unmap file X startblock C offset A length B | t0
128 * | Intent to reduce refcount for extent (C, B) |
129 * | Intent to remove rmap (X, C, A, B) |
130 * | Intent to free extent (D, 1) (bmbt block) |
131 * | Intent to map (X, A, B) at startblock E |
132 * +-------------------------------------------------+
133 * | Map file X startblock E offset A length B | t1
134 * | Done mapping (X, E, A, B) |
135 * | Intent to increase refcount for extent (E, B) |
136 * | Intent to add rmap (X, E, A, B) |
137 * +-------------------------------------------------+
138 * | Reduce refcount for extent (C, B) | t2
Darrick J. Wong385d65582016-09-19 10:26:25 +1000139 * | Done reducing refcount for extent (C, 9) |
140 * | Intent to reduce refcount for extent (C+9, B-9) |
141 * | (ran out of space after 9 refcount updates) |
142 * +-------------------------------------------------+
143 * | Reduce refcount for extent (C+9, B+9) | t3
144 * | Done reducing refcount for extent (C+9, B-9) |
Darrick J. Wong4e0cc292016-08-03 11:12:25 +1000145 * | Increase refcount for extent (E, B) |
146 * | Done increasing refcount for extent (E, B) |
147 * | Intent to free extent (C, B) |
148 * | Intent to free extent (F, 1) (refcountbt block) |
149 * | Intent to remove rmap (F, 1, REFC) |
150 * +-------------------------------------------------+
Darrick J. Wong385d65582016-09-19 10:26:25 +1000151 * | Remove rmap (X, C, A, B) | t4
Darrick J. Wong4e0cc292016-08-03 11:12:25 +1000152 * | Done removing rmap (X, C, A, B) |
153 * | Add rmap (X, E, A, B) |
154 * | Done adding rmap (X, E, A, B) |
155 * | Remove rmap (F, 1, REFC) |
156 * | Done removing rmap (F, 1, REFC) |
157 * +-------------------------------------------------+
Darrick J. Wong385d65582016-09-19 10:26:25 +1000158 * | Free extent (C, B) | t5
Darrick J. Wong4e0cc292016-08-03 11:12:25 +1000159 * | Done freeing extent (C, B) |
160 * | Free extent (D, 1) |
161 * | Done freeing extent (D, 1) |
162 * | Free extent (F, 1) |
163 * | Done freeing extent (F, 1) |
164 * +-------------------------------------------------+
165 *
166 * If we should crash before t2 commits, log recovery replays
167 * the following intent items:
168 *
169 * - Intent to reduce refcount for extent (C, B)
170 * - Intent to remove rmap (X, C, A, B)
171 * - Intent to free extent (D, 1) (bmbt block)
172 * - Intent to increase refcount for extent (E, B)
173 * - Intent to add rmap (X, E, A, B)
174 *
175 * In the process of recovering, it should also generate and take care
176 * of these intent items:
177 *
178 * - Intent to free extent (C, B)
179 * - Intent to free extent (F, 1) (refcountbt block)
180 * - Intent to remove rmap (F, 1, REFC)
Darrick J. Wong385d65582016-09-19 10:26:25 +1000181 *
182 * Note that the continuation requested between t2 and t3 is likely to
183 * reoccur.
Darrick J. Wong4e0cc292016-08-03 11:12:25 +1000184 */
185
186static const struct xfs_defer_op_type *defer_op_types[XFS_DEFER_OPS_TYPE_MAX];
187
188/*
189 * For each pending item in the intake list, log its intent item and the
190 * associated extents, then add the entire intake list to the end of
191 * the pending list.
192 */
193STATIC void
194xfs_defer_intake_work(
195 struct xfs_trans *tp,
196 struct xfs_defer_ops *dop)
197{
198 struct list_head *li;
199 struct xfs_defer_pending *dfp;
200
201 list_for_each_entry(dfp, &dop->dop_intake, dfp_list) {
202 dfp->dfp_intent = dfp->dfp_type->create_intent(tp,
203 dfp->dfp_count);
Darrick J. Wongb77428b2016-10-24 14:21:18 +1100204 trace_xfs_defer_intake_work(tp->t_mountp, dfp);
Darrick J. Wong4e0cc292016-08-03 11:12:25 +1000205 list_sort(tp->t_mountp, &dfp->dfp_work,
206 dfp->dfp_type->diff_items);
207 list_for_each(li, &dfp->dfp_work)
208 dfp->dfp_type->log_item(tp, dfp->dfp_intent, li);
209 }
210
211 list_splice_tail_init(&dop->dop_intake, &dop->dop_pending);
212}
213
214/* Abort all the intents that were committed. */
215STATIC void
216xfs_defer_trans_abort(
217 struct xfs_trans *tp,
218 struct xfs_defer_ops *dop,
219 int error)
220{
221 struct xfs_defer_pending *dfp;
222
Darrick J. Wong3cd48ab2016-08-03 11:13:02 +1000223 trace_xfs_defer_trans_abort(tp->t_mountp, dop);
Darrick J. Wong4e0cc292016-08-03 11:12:25 +1000224
Darrick J. Wongb77428b2016-10-24 14:21:18 +1100225 /* Abort intent items that don't have a done item. */
Darrick J. Wong4e0cc292016-08-03 11:12:25 +1000226 list_for_each_entry(dfp, &dop->dop_pending, dfp_list) {
Darrick J. Wong3cd48ab2016-08-03 11:13:02 +1000227 trace_xfs_defer_pending_abort(tp->t_mountp, dfp);
Darrick J. Wongb77428b2016-10-24 14:21:18 +1100228 if (dfp->dfp_intent && !dfp->dfp_done) {
Darrick J. Wong4e0cc292016-08-03 11:12:25 +1000229 dfp->dfp_type->abort_intent(dfp->dfp_intent);
Darrick J. Wongb77428b2016-10-24 14:21:18 +1100230 dfp->dfp_intent = NULL;
231 }
Darrick J. Wong4e0cc292016-08-03 11:12:25 +1000232 }
233
234 /* Shut down FS. */
235 xfs_force_shutdown(tp->t_mountp, (error == -EFSCORRUPTED) ?
236 SHUTDOWN_CORRUPT_INCORE : SHUTDOWN_META_IO_ERROR);
237}
238
239/* Roll a transaction so we can do some deferred op processing. */
240STATIC int
241xfs_defer_trans_roll(
242 struct xfs_trans **tp,
Christoph Hellwig411350d2017-08-28 10:21:03 -0700243 struct xfs_defer_ops *dop)
Darrick J. Wong4e0cc292016-08-03 11:12:25 +1000244{
245 int i;
246 int error;
247
Christoph Hellwig411350d2017-08-28 10:21:03 -0700248 /* Log all the joined inodes. */
249 for (i = 0; i < XFS_DEFER_OPS_NR_INODES && dop->dop_inodes[i]; i++)
Darrick J. Wong4e0cc292016-08-03 11:12:25 +1000250 xfs_trans_log_inode(*tp, dop->dop_inodes[i], XFS_ILOG_CORE);
Darrick J. Wong4e0cc292016-08-03 11:12:25 +1000251
Darrick J. Wong3cd48ab2016-08-03 11:13:02 +1000252 trace_xfs_defer_trans_roll((*tp)->t_mountp, dop);
253
Darrick J. Wong4e0cc292016-08-03 11:12:25 +1000254 /* Roll the transaction. */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700255 error = xfs_trans_roll(tp);
Darrick J. Wong4e0cc292016-08-03 11:12:25 +1000256 if (error) {
Darrick J. Wong3cd48ab2016-08-03 11:13:02 +1000257 trace_xfs_defer_trans_roll_error((*tp)->t_mountp, dop, error);
Darrick J. Wong4e0cc292016-08-03 11:12:25 +1000258 xfs_defer_trans_abort(*tp, dop, error);
259 return error;
260 }
261 dop->dop_committed = true;
262
Christoph Hellwig411350d2017-08-28 10:21:03 -0700263 /* Rejoin the joined inodes. */
264 for (i = 0; i < XFS_DEFER_OPS_NR_INODES && dop->dop_inodes[i]; i++)
Darrick J. Wong4e0cc292016-08-03 11:12:25 +1000265 xfs_trans_ijoin(*tp, dop->dop_inodes[i], 0);
Darrick J. Wong4e0cc292016-08-03 11:12:25 +1000266
267 return error;
268}
269
270/* Do we have any work items to finish? */
271bool
272xfs_defer_has_unfinished_work(
273 struct xfs_defer_ops *dop)
274{
275 return !list_empty(&dop->dop_pending) || !list_empty(&dop->dop_intake);
276}
277
278/*
279 * Add this inode to the deferred op. Each joined inode is relogged
280 * each time we roll the transaction, in addition to any inode passed
281 * to xfs_defer_finish().
282 */
283int
Christoph Hellwig882d8782017-08-28 10:21:03 -0700284xfs_defer_ijoin(
Darrick J. Wong4e0cc292016-08-03 11:12:25 +1000285 struct xfs_defer_ops *dop,
286 struct xfs_inode *ip)
287{
288 int i;
289
290 for (i = 0; i < XFS_DEFER_OPS_NR_INODES; i++) {
291 if (dop->dop_inodes[i] == ip)
292 return 0;
293 else if (dop->dop_inodes[i] == NULL) {
294 dop->dop_inodes[i] = ip;
295 return 0;
296 }
297 }
298
299 return -EFSCORRUPTED;
300}
301
302/*
303 * Finish all the pending work. This involves logging intent items for
304 * any work items that wandered in since the last transaction roll (if
305 * one has even happened), rolling the transaction, and finishing the
306 * work items in the first item on the logged-and-pending list.
307 *
308 * If an inode is provided, relog it to the new transaction.
309 */
310int
311xfs_defer_finish(
312 struct xfs_trans **tp,
313 struct xfs_defer_ops *dop,
314 struct xfs_inode *ip)
315{
316 struct xfs_defer_pending *dfp;
317 struct list_head *li;
318 struct list_head *n;
Darrick J. Wong4e0cc292016-08-03 11:12:25 +1000319 void *state;
320 int error = 0;
321 void (*cleanup_fn)(struct xfs_trans *, void *, int);
322
323 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
324
Darrick J. Wong3cd48ab2016-08-03 11:13:02 +1000325 trace_xfs_defer_finish((*tp)->t_mountp, dop);
326
Christoph Hellwig882d8782017-08-28 10:21:03 -0700327 xfs_defer_ijoin(dop, ip);
Christoph Hellwig411350d2017-08-28 10:21:03 -0700328
Darrick J. Wong4e0cc292016-08-03 11:12:25 +1000329 /* Until we run out of pending work to finish... */
330 while (xfs_defer_has_unfinished_work(dop)) {
331 /* Log intents for work items sitting in the intake. */
332 xfs_defer_intake_work(*tp, dop);
333
334 /* Roll the transaction. */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700335 error = xfs_defer_trans_roll(tp, dop);
Darrick J. Wong4e0cc292016-08-03 11:12:25 +1000336 if (error)
337 goto out;
338
Darrick J. Wong4e0cc292016-08-03 11:12:25 +1000339 /* Log an intent-done item for the first pending item. */
340 dfp = list_first_entry(&dop->dop_pending,
341 struct xfs_defer_pending, dfp_list);
Darrick J. Wong3cd48ab2016-08-03 11:13:02 +1000342 trace_xfs_defer_pending_finish((*tp)->t_mountp, dfp);
Darrick J. Wongea78d802016-08-30 13:51:39 +1000343 dfp->dfp_done = dfp->dfp_type->create_done(*tp, dfp->dfp_intent,
Darrick J. Wong4e0cc292016-08-03 11:12:25 +1000344 dfp->dfp_count);
345 cleanup_fn = dfp->dfp_type->finish_cleanup;
346
347 /* Finish the work items. */
348 state = NULL;
349 list_for_each_safe(li, n, &dfp->dfp_work) {
350 list_del(li);
351 dfp->dfp_count--;
352 error = dfp->dfp_type->finish_item(*tp, dop, li,
Darrick J. Wongea78d802016-08-30 13:51:39 +1000353 dfp->dfp_done, &state);
Darrick J. Wong385d65582016-09-19 10:26:25 +1000354 if (error == -EAGAIN) {
355 /*
356 * Caller wants a fresh transaction;
357 * put the work item back on the list
358 * and jump out.
359 */
360 list_add(li, &dfp->dfp_work);
361 dfp->dfp_count++;
362 break;
363 } else if (error) {
Darrick J. Wong4e0cc292016-08-03 11:12:25 +1000364 /*
365 * Clean up after ourselves and jump out.
366 * xfs_defer_cancel will take care of freeing
367 * all these lists and stuff.
368 */
369 if (cleanup_fn)
370 cleanup_fn(*tp, state, error);
371 xfs_defer_trans_abort(*tp, dop, error);
372 goto out;
373 }
374 }
Darrick J. Wong385d65582016-09-19 10:26:25 +1000375 if (error == -EAGAIN) {
376 /*
377 * Caller wants a fresh transaction, so log a
378 * new log intent item to replace the old one
379 * and roll the transaction. See "Requesting
380 * a Fresh Transaction while Finishing
381 * Deferred Work" above.
382 */
383 dfp->dfp_intent = dfp->dfp_type->create_intent(*tp,
384 dfp->dfp_count);
385 dfp->dfp_done = NULL;
386 list_for_each(li, &dfp->dfp_work)
387 dfp->dfp_type->log_item(*tp, dfp->dfp_intent,
388 li);
389 } else {
390 /* Done with the dfp, free it. */
391 list_del(&dfp->dfp_list);
392 kmem_free(dfp);
393 }
Darrick J. Wong4e0cc292016-08-03 11:12:25 +1000394
395 if (cleanup_fn)
396 cleanup_fn(*tp, state, error);
397 }
398
399out:
Darrick J. Wong3cd48ab2016-08-03 11:13:02 +1000400 if (error)
401 trace_xfs_defer_finish_error((*tp)->t_mountp, dop, error);
402 else
403 trace_xfs_defer_finish_done((*tp)->t_mountp, dop);
Darrick J. Wong4e0cc292016-08-03 11:12:25 +1000404 return error;
405}
406
407/*
408 * Free up any items left in the list.
409 */
410void
411xfs_defer_cancel(
412 struct xfs_defer_ops *dop)
413{
414 struct xfs_defer_pending *dfp;
415 struct xfs_defer_pending *pli;
416 struct list_head *pwi;
417 struct list_head *n;
418
Darrick J. Wong3cd48ab2016-08-03 11:13:02 +1000419 trace_xfs_defer_cancel(NULL, dop);
420
Darrick J. Wong4e0cc292016-08-03 11:12:25 +1000421 /*
422 * Free the pending items. Caller should already have arranged
423 * for the intent items to be released.
424 */
425 list_for_each_entry_safe(dfp, pli, &dop->dop_intake, dfp_list) {
Darrick J. Wong3cd48ab2016-08-03 11:13:02 +1000426 trace_xfs_defer_intake_cancel(NULL, dfp);
Darrick J. Wong4e0cc292016-08-03 11:12:25 +1000427 list_del(&dfp->dfp_list);
428 list_for_each_safe(pwi, n, &dfp->dfp_work) {
429 list_del(pwi);
430 dfp->dfp_count--;
431 dfp->dfp_type->cancel_item(pwi);
432 }
433 ASSERT(dfp->dfp_count == 0);
434 kmem_free(dfp);
435 }
436 list_for_each_entry_safe(dfp, pli, &dop->dop_pending, dfp_list) {
Darrick J. Wong3cd48ab2016-08-03 11:13:02 +1000437 trace_xfs_defer_pending_cancel(NULL, dfp);
Darrick J. Wong4e0cc292016-08-03 11:12:25 +1000438 list_del(&dfp->dfp_list);
439 list_for_each_safe(pwi, n, &dfp->dfp_work) {
440 list_del(pwi);
441 dfp->dfp_count--;
442 dfp->dfp_type->cancel_item(pwi);
443 }
444 ASSERT(dfp->dfp_count == 0);
445 kmem_free(dfp);
446 }
447}
448
449/* Add an item for later deferred processing. */
450void
451xfs_defer_add(
452 struct xfs_defer_ops *dop,
453 enum xfs_defer_ops_type type,
454 struct list_head *li)
455{
456 struct xfs_defer_pending *dfp = NULL;
457
458 /*
459 * Add the item to a pending item at the end of the intake list.
460 * If the last pending item has the same type, reuse it. Else,
461 * create a new pending item at the end of the intake list.
462 */
463 if (!list_empty(&dop->dop_intake)) {
464 dfp = list_last_entry(&dop->dop_intake,
465 struct xfs_defer_pending, dfp_list);
466 if (dfp->dfp_type->type != type ||
467 (dfp->dfp_type->max_items &&
468 dfp->dfp_count >= dfp->dfp_type->max_items))
469 dfp = NULL;
470 }
471 if (!dfp) {
472 dfp = kmem_alloc(sizeof(struct xfs_defer_pending),
473 KM_SLEEP | KM_NOFS);
474 dfp->dfp_type = defer_op_types[type];
Darrick J. Wong4e0cc292016-08-03 11:12:25 +1000475 dfp->dfp_intent = NULL;
Darrick J. Wongea78d802016-08-30 13:51:39 +1000476 dfp->dfp_done = NULL;
Darrick J. Wong4e0cc292016-08-03 11:12:25 +1000477 dfp->dfp_count = 0;
478 INIT_LIST_HEAD(&dfp->dfp_work);
479 list_add_tail(&dfp->dfp_list, &dop->dop_intake);
480 }
481
482 list_add_tail(li, &dfp->dfp_work);
483 dfp->dfp_count++;
484}
485
486/* Initialize a deferred operation list. */
487void
488xfs_defer_init_op_type(
489 const struct xfs_defer_op_type *type)
490{
491 defer_op_types[type->type] = type;
492}
493
494/* Initialize a deferred operation. */
495void
496xfs_defer_init(
497 struct xfs_defer_ops *dop,
498 xfs_fsblock_t *fbp)
499{
500 dop->dop_committed = false;
501 dop->dop_low = false;
502 memset(&dop->dop_inodes, 0, sizeof(dop->dop_inodes));
503 *fbp = NULLFSBLOCK;
504 INIT_LIST_HEAD(&dop->dop_intake);
505 INIT_LIST_HEAD(&dop->dop_pending);
Darrick J. Wong3cd48ab2016-08-03 11:13:02 +1000506 trace_xfs_defer_init(NULL, dop);
Darrick J. Wong4e0cc292016-08-03 11:12:25 +1000507}