2 * Copyright (C) 2016 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
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.
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.
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.
22 #include "xfs_shared.h"
23 #include "xfs_format.h"
24 #include "xfs_log_format.h"
25 #include "xfs_trans_resv.h"
28 #include "xfs_mount.h"
29 #include "xfs_defer.h"
30 #include "xfs_trans.h"
31 #include "xfs_trace.h"
34 * Deferred Operations in XFS
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
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
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.
60 * Finishing a set of deferred operations is an involved process. To
61 * start, we define "rolling a deferred-op transaction" as follows:
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
70 * > Roll the transaction.
72 * NOTE: To avoid exceeding the transaction reservation, we limit the
73 * number of items that we attach to a given xfs_defer_pending.
75 * The actual finishing process looks like this:
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
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.
85 * The key here is that we must log an intent item for all pending
86 * work items every time we roll the transaction, and that we must log
87 * a done item as soon as the work is completed. With this mechanism
88 * we can perform complex remapping operations, chaining intent items
91 * This is an example of remapping the extent (E, E+B) into file X at
92 * offset A and dealing with the extent (C, C+B) already being mapped
94 * +-------------------------------------------------+
95 * | Unmap file X startblock C offset A length B | t0
96 * | Intent to reduce refcount for extent (C, B) |
97 * | Intent to remove rmap (X, C, A, B) |
98 * | Intent to free extent (D, 1) (bmbt block) |
99 * | Intent to map (X, A, B) at startblock E |
100 * +-------------------------------------------------+
101 * | Map file X startblock E offset A length B | t1
102 * | Done mapping (X, E, A, B) |
103 * | Intent to increase refcount for extent (E, B) |
104 * | Intent to add rmap (X, E, A, B) |
105 * +-------------------------------------------------+
106 * | Reduce refcount for extent (C, B) | t2
107 * | Done reducing refcount for extent (C, B) |
108 * | Increase refcount for extent (E, B) |
109 * | Done increasing refcount for extent (E, B) |
110 * | Intent to free extent (C, B) |
111 * | Intent to free extent (F, 1) (refcountbt block) |
112 * | Intent to remove rmap (F, 1, REFC) |
113 * +-------------------------------------------------+
114 * | Remove rmap (X, C, A, B) | t3
115 * | Done removing rmap (X, C, A, B) |
116 * | Add rmap (X, E, A, B) |
117 * | Done adding rmap (X, E, A, B) |
118 * | Remove rmap (F, 1, REFC) |
119 * | Done removing rmap (F, 1, REFC) |
120 * +-------------------------------------------------+
121 * | Free extent (C, B) | t4
122 * | Done freeing extent (C, B) |
123 * | Free extent (D, 1) |
124 * | Done freeing extent (D, 1) |
125 * | Free extent (F, 1) |
126 * | Done freeing extent (F, 1) |
127 * +-------------------------------------------------+
129 * If we should crash before t2 commits, log recovery replays
130 * the following intent items:
132 * - Intent to reduce refcount for extent (C, B)
133 * - Intent to remove rmap (X, C, A, B)
134 * - Intent to free extent (D, 1) (bmbt block)
135 * - Intent to increase refcount for extent (E, B)
136 * - Intent to add rmap (X, E, A, B)
138 * In the process of recovering, it should also generate and take care
139 * of these intent items:
141 * - Intent to free extent (C, B)
142 * - Intent to free extent (F, 1) (refcountbt block)
143 * - Intent to remove rmap (F, 1, REFC)
146 static const struct xfs_defer_op_type
*defer_op_types
[XFS_DEFER_OPS_TYPE_MAX
];
149 * For each pending item in the intake list, log its intent item and the
150 * associated extents, then add the entire intake list to the end of
154 xfs_defer_intake_work(
155 struct xfs_trans
*tp
,
156 struct xfs_defer_ops
*dop
)
158 struct list_head
*li
;
159 struct xfs_defer_pending
*dfp
;
161 list_for_each_entry(dfp
, &dop
->dop_intake
, dfp_list
) {
162 trace_xfs_defer_intake_work(tp
->t_mountp
, dfp
);
163 dfp
->dfp_intent
= dfp
->dfp_type
->create_intent(tp
,
165 list_sort(tp
->t_mountp
, &dfp
->dfp_work
,
166 dfp
->dfp_type
->diff_items
);
167 list_for_each(li
, &dfp
->dfp_work
)
168 dfp
->dfp_type
->log_item(tp
, dfp
->dfp_intent
, li
);
171 list_splice_tail_init(&dop
->dop_intake
, &dop
->dop_pending
);
174 /* Abort all the intents that were committed. */
176 xfs_defer_trans_abort(
177 struct xfs_trans
*tp
,
178 struct xfs_defer_ops
*dop
,
181 struct xfs_defer_pending
*dfp
;
183 trace_xfs_defer_trans_abort(tp
->t_mountp
, dop
);
185 * If the transaction was committed, drop the intent reference
186 * since we're bailing out of here. The other reference is
187 * dropped when the intent hits the AIL. If the transaction
188 * was not committed, the intent is freed by the intent item
189 * unlock handler on abort.
191 if (!dop
->dop_committed
)
194 /* Abort intent items. */
195 list_for_each_entry(dfp
, &dop
->dop_pending
, dfp_list
) {
196 trace_xfs_defer_pending_abort(tp
->t_mountp
, dfp
);
198 dfp
->dfp_type
->abort_intent(dfp
->dfp_intent
);
202 xfs_force_shutdown(tp
->t_mountp
, (error
== -EFSCORRUPTED
) ?
203 SHUTDOWN_CORRUPT_INCORE
: SHUTDOWN_META_IO_ERROR
);
206 /* Roll a transaction so we can do some deferred op processing. */
208 xfs_defer_trans_roll(
209 struct xfs_trans
**tp
,
210 struct xfs_defer_ops
*dop
,
211 struct xfs_inode
*ip
)
216 /* Log all the joined inodes except the one we passed in. */
217 for (i
= 0; i
< XFS_DEFER_OPS_NR_INODES
&& dop
->dop_inodes
[i
]; i
++) {
218 if (dop
->dop_inodes
[i
] == ip
)
220 xfs_trans_log_inode(*tp
, dop
->dop_inodes
[i
], XFS_ILOG_CORE
);
223 trace_xfs_defer_trans_roll((*tp
)->t_mountp
, dop
);
225 /* Roll the transaction. */
226 error
= xfs_trans_roll(tp
, ip
);
228 trace_xfs_defer_trans_roll_error((*tp
)->t_mountp
, dop
, error
);
229 xfs_defer_trans_abort(*tp
, dop
, error
);
232 dop
->dop_committed
= true;
234 /* Rejoin the joined inodes except the one we passed in. */
235 for (i
= 0; i
< XFS_DEFER_OPS_NR_INODES
&& dop
->dop_inodes
[i
]; i
++) {
236 if (dop
->dop_inodes
[i
] == ip
)
238 xfs_trans_ijoin(*tp
, dop
->dop_inodes
[i
], 0);
244 /* Do we have any work items to finish? */
246 xfs_defer_has_unfinished_work(
247 struct xfs_defer_ops
*dop
)
249 return !list_empty(&dop
->dop_pending
) || !list_empty(&dop
->dop_intake
);
253 * Add this inode to the deferred op. Each joined inode is relogged
254 * each time we roll the transaction, in addition to any inode passed
255 * to xfs_defer_finish().
259 struct xfs_defer_ops
*dop
,
260 struct xfs_inode
*ip
)
264 for (i
= 0; i
< XFS_DEFER_OPS_NR_INODES
; i
++) {
265 if (dop
->dop_inodes
[i
] == ip
)
267 else if (dop
->dop_inodes
[i
] == NULL
) {
268 dop
->dop_inodes
[i
] = ip
;
273 return -EFSCORRUPTED
;
277 * Finish all the pending work. This involves logging intent items for
278 * any work items that wandered in since the last transaction roll (if
279 * one has even happened), rolling the transaction, and finishing the
280 * work items in the first item on the logged-and-pending list.
282 * If an inode is provided, relog it to the new transaction.
286 struct xfs_trans
**tp
,
287 struct xfs_defer_ops
*dop
,
288 struct xfs_inode
*ip
)
290 struct xfs_defer_pending
*dfp
;
291 struct list_head
*li
;
295 void (*cleanup_fn
)(struct xfs_trans
*, void *, int);
297 ASSERT((*tp
)->t_flags
& XFS_TRANS_PERM_LOG_RES
);
299 trace_xfs_defer_finish((*tp
)->t_mountp
, dop
);
301 /* Until we run out of pending work to finish... */
302 while (xfs_defer_has_unfinished_work(dop
)) {
303 /* Log intents for work items sitting in the intake. */
304 xfs_defer_intake_work(*tp
, dop
);
306 /* Roll the transaction. */
307 error
= xfs_defer_trans_roll(tp
, dop
, ip
);
311 /* Log an intent-done item for the first pending item. */
312 dfp
= list_first_entry(&dop
->dop_pending
,
313 struct xfs_defer_pending
, dfp_list
);
314 trace_xfs_defer_pending_finish((*tp
)->t_mountp
, dfp
);
315 dfp
->dfp_done
= dfp
->dfp_type
->create_done(*tp
, dfp
->dfp_intent
,
317 cleanup_fn
= dfp
->dfp_type
->finish_cleanup
;
319 /* Finish the work items. */
321 list_for_each_safe(li
, n
, &dfp
->dfp_work
) {
324 error
= dfp
->dfp_type
->finish_item(*tp
, dop
, li
,
325 dfp
->dfp_done
, &state
);
328 * Clean up after ourselves and jump out.
329 * xfs_defer_cancel will take care of freeing
330 * all these lists and stuff.
333 cleanup_fn(*tp
, state
, error
);
334 xfs_defer_trans_abort(*tp
, dop
, error
);
338 /* Done with the dfp, free it. */
339 list_del(&dfp
->dfp_list
);
343 cleanup_fn(*tp
, state
, error
);
348 trace_xfs_defer_finish_error((*tp
)->t_mountp
, dop
, error
);
350 trace_xfs_defer_finish_done((*tp
)->t_mountp
, dop
);
355 * Free up any items left in the list.
359 struct xfs_defer_ops
*dop
)
361 struct xfs_defer_pending
*dfp
;
362 struct xfs_defer_pending
*pli
;
363 struct list_head
*pwi
;
366 trace_xfs_defer_cancel(NULL
, dop
);
369 * Free the pending items. Caller should already have arranged
370 * for the intent items to be released.
372 list_for_each_entry_safe(dfp
, pli
, &dop
->dop_intake
, dfp_list
) {
373 trace_xfs_defer_intake_cancel(NULL
, dfp
);
374 list_del(&dfp
->dfp_list
);
375 list_for_each_safe(pwi
, n
, &dfp
->dfp_work
) {
378 dfp
->dfp_type
->cancel_item(pwi
);
380 ASSERT(dfp
->dfp_count
== 0);
383 list_for_each_entry_safe(dfp
, pli
, &dop
->dop_pending
, dfp_list
) {
384 trace_xfs_defer_pending_cancel(NULL
, dfp
);
385 list_del(&dfp
->dfp_list
);
386 list_for_each_safe(pwi
, n
, &dfp
->dfp_work
) {
389 dfp
->dfp_type
->cancel_item(pwi
);
391 ASSERT(dfp
->dfp_count
== 0);
396 /* Add an item for later deferred processing. */
399 struct xfs_defer_ops
*dop
,
400 enum xfs_defer_ops_type type
,
401 struct list_head
*li
)
403 struct xfs_defer_pending
*dfp
= NULL
;
406 * Add the item to a pending item at the end of the intake list.
407 * If the last pending item has the same type, reuse it. Else,
408 * create a new pending item at the end of the intake list.
410 if (!list_empty(&dop
->dop_intake
)) {
411 dfp
= list_last_entry(&dop
->dop_intake
,
412 struct xfs_defer_pending
, dfp_list
);
413 if (dfp
->dfp_type
->type
!= type
||
414 (dfp
->dfp_type
->max_items
&&
415 dfp
->dfp_count
>= dfp
->dfp_type
->max_items
))
419 dfp
= kmem_alloc(sizeof(struct xfs_defer_pending
),
421 dfp
->dfp_type
= defer_op_types
[type
];
422 dfp
->dfp_intent
= NULL
;
423 dfp
->dfp_done
= NULL
;
425 INIT_LIST_HEAD(&dfp
->dfp_work
);
426 list_add_tail(&dfp
->dfp_list
, &dop
->dop_intake
);
429 list_add_tail(li
, &dfp
->dfp_work
);
433 /* Initialize a deferred operation list. */
435 xfs_defer_init_op_type(
436 const struct xfs_defer_op_type
*type
)
438 defer_op_types
[type
->type
] = type
;
441 /* Initialize a deferred operation. */
444 struct xfs_defer_ops
*dop
,
447 dop
->dop_committed
= false;
448 dop
->dop_low
= false;
449 memset(&dop
->dop_inodes
, 0, sizeof(dop
->dop_inodes
));
451 INIT_LIST_HEAD(&dop
->dop_intake
);
452 INIT_LIST_HEAD(&dop
->dop_pending
);
453 trace_xfs_defer_init(NULL
, dop
);