1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
6 #include "libxfs_priv.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_mount.h"
13 #include "xfs_defer.h"
14 #include "xfs_trans.h"
15 #include "xfs_inode.h"
16 #include "xfs_trace.h"
19 * Deferred Operations in XFS
21 * Due to the way locking rules work in XFS, certain transactions (block
22 * mapping and unmapping, typically) have permanent reservations so that
23 * we can roll the transaction to adhere to AG locking order rules and
24 * to unlock buffers between metadata updates. Prior to rmap/reflink,
25 * the mapping code had a mechanism to perform these deferrals for
26 * extents that were going to be freed; this code makes that facility
29 * When adding the reverse mapping and reflink features, it became
30 * necessary to perform complex remapping multi-transactions to comply
31 * with AG locking order rules, and to be able to spread a single
32 * refcount update operation (an operation on an n-block extent can
33 * update as many as n records!) among multiple transactions. XFS can
34 * roll a transaction to facilitate this, but using this facility
35 * requires us to log "intent" items in case log recovery needs to
36 * redo the operation, and to log "done" items to indicate that redo
39 * Deferred work is tracked in xfs_defer_pending items. Each pending
40 * item tracks one type of deferred work. Incoming work items (which
41 * have not yet had an intent logged) are attached to a pending item
42 * on the dop_intake list, where they wait for the caller to finish
43 * the deferred operations.
45 * Finishing a set of deferred operations is an involved process. To
46 * start, we define "rolling a deferred-op transaction" as follows:
48 * > For each xfs_defer_pending item on the dop_intake list,
49 * - Sort the work items in AG order. XFS locking
50 * order rules require us to lock buffers in AG order.
51 * - Create a log intent item for that type.
52 * - Attach it to the pending item.
53 * - Move the pending item from the dop_intake list to the
55 * > Roll the transaction.
57 * NOTE: To avoid exceeding the transaction reservation, we limit the
58 * number of items that we attach to a given xfs_defer_pending.
60 * The actual finishing process looks like this:
62 * > For each xfs_defer_pending in the dop_pending list,
63 * - Roll the deferred-op transaction as above.
64 * - Create a log done item for that type, and attach it to the
66 * - For each work item attached to the log intent item,
67 * * Perform the described action.
68 * * Attach the work item to the log done item.
69 * * If the result of doing the work was -EAGAIN, ->finish work
70 * wants a new transaction. See the "Requesting a Fresh
71 * Transaction while Finishing Deferred Work" section below for
74 * The key here is that we must log an intent item for all pending
75 * work items every time we roll the transaction, and that we must log
76 * a done item as soon as the work is completed. With this mechanism
77 * we can perform complex remapping operations, chaining intent items
80 * Requesting a Fresh Transaction while Finishing Deferred Work
82 * If ->finish_item decides that it needs a fresh transaction to
83 * finish the work, it must ask its caller (xfs_defer_finish) for a
84 * continuation. The most likely cause of this circumstance are the
85 * refcount adjust functions deciding that they've logged enough items
86 * to be at risk of exceeding the transaction reservation.
88 * To get a fresh transaction, we want to log the existing log done
89 * item to prevent the log intent item from replaying, immediately log
90 * a new log intent item with the unfinished work items, roll the
91 * transaction, and re-call ->finish_item wherever it left off. The
92 * log done item and the new log intent item must be in the same
93 * transaction or atomicity cannot be guaranteed; defer_finish ensures
96 * This requires some coordination between ->finish_item and
97 * defer_finish. Upon deciding to request a new transaction,
98 * ->finish_item should update the current work item to reflect the
99 * unfinished work. Next, it should reset the log done item's list
100 * count to the number of items finished, and return -EAGAIN.
101 * defer_finish sees the -EAGAIN, logs the new log intent item
102 * with the remaining work items, and leaves the xfs_defer_pending
103 * item at the head of the dop_work queue. Then it rolls the
104 * transaction and picks up processing where it left off. It is
105 * required that ->finish_item must be careful to leave enough
106 * transaction reservation to fit the new log intent item.
108 * This is an example of remapping the extent (E, E+B) into file X at
109 * offset A and dealing with the extent (C, C+B) already being mapped
111 * +-------------------------------------------------+
112 * | Unmap file X startblock C offset A length B | t0
113 * | Intent to reduce refcount for extent (C, B) |
114 * | Intent to remove rmap (X, C, A, B) |
115 * | Intent to free extent (D, 1) (bmbt block) |
116 * | Intent to map (X, A, B) at startblock E |
117 * +-------------------------------------------------+
118 * | Map file X startblock E offset A length B | t1
119 * | Done mapping (X, E, A, B) |
120 * | Intent to increase refcount for extent (E, B) |
121 * | Intent to add rmap (X, E, A, B) |
122 * +-------------------------------------------------+
123 * | Reduce refcount for extent (C, B) | t2
124 * | Done reducing refcount for extent (C, 9) |
125 * | Intent to reduce refcount for extent (C+9, B-9) |
126 * | (ran out of space after 9 refcount updates) |
127 * +-------------------------------------------------+
128 * | Reduce refcount for extent (C+9, B+9) | t3
129 * | Done reducing refcount for extent (C+9, B-9) |
130 * | Increase refcount for extent (E, B) |
131 * | Done increasing refcount for extent (E, B) |
132 * | Intent to free extent (C, B) |
133 * | Intent to free extent (F, 1) (refcountbt block) |
134 * | Intent to remove rmap (F, 1, REFC) |
135 * +-------------------------------------------------+
136 * | Remove rmap (X, C, A, B) | t4
137 * | Done removing rmap (X, C, A, B) |
138 * | Add rmap (X, E, A, B) |
139 * | Done adding rmap (X, E, A, B) |
140 * | Remove rmap (F, 1, REFC) |
141 * | Done removing rmap (F, 1, REFC) |
142 * +-------------------------------------------------+
143 * | Free extent (C, B) | t5
144 * | Done freeing extent (C, B) |
145 * | Free extent (D, 1) |
146 * | Done freeing extent (D, 1) |
147 * | Free extent (F, 1) |
148 * | Done freeing extent (F, 1) |
149 * +-------------------------------------------------+
151 * If we should crash before t2 commits, log recovery replays
152 * the following intent items:
154 * - Intent to reduce refcount for extent (C, B)
155 * - Intent to remove rmap (X, C, A, B)
156 * - Intent to free extent (D, 1) (bmbt block)
157 * - Intent to increase refcount for extent (E, B)
158 * - Intent to add rmap (X, E, A, B)
160 * In the process of recovering, it should also generate and take care
161 * of these intent items:
163 * - Intent to free extent (C, B)
164 * - Intent to free extent (F, 1) (refcountbt block)
165 * - Intent to remove rmap (F, 1, REFC)
167 * Note that the continuation requested between t2 and t3 is likely to
171 static const struct xfs_defer_op_type
*defer_op_types
[] = {
172 [XFS_DEFER_OPS_TYPE_BMAP
] = &xfs_bmap_update_defer_type
,
173 [XFS_DEFER_OPS_TYPE_REFCOUNT
] = &xfs_refcount_update_defer_type
,
174 [XFS_DEFER_OPS_TYPE_RMAP
] = &xfs_rmap_update_defer_type
,
175 [XFS_DEFER_OPS_TYPE_FREE
] = &xfs_extent_free_defer_type
,
176 [XFS_DEFER_OPS_TYPE_AGFL_FREE
] = &xfs_agfl_free_defer_type
,
180 xfs_defer_create_intent(
181 struct xfs_trans
*tp
,
182 struct xfs_defer_pending
*dfp
,
185 const struct xfs_defer_op_type
*ops
= defer_op_types
[dfp
->dfp_type
];
187 dfp
->dfp_intent
= ops
->create_intent(tp
, &dfp
->dfp_work
,
188 dfp
->dfp_count
, sort
);
192 * For each pending item in the intake list, log its intent item and the
193 * associated extents, then add the entire intake list to the end of
197 xfs_defer_create_intents(
198 struct xfs_trans
*tp
)
200 struct xfs_defer_pending
*dfp
;
202 list_for_each_entry(dfp
, &tp
->t_dfops
, dfp_list
) {
203 trace_xfs_defer_create_intent(tp
->t_mountp
, dfp
);
204 xfs_defer_create_intent(tp
, dfp
, true);
208 /* Abort all the intents that were committed. */
210 xfs_defer_trans_abort(
211 struct xfs_trans
*tp
,
212 struct list_head
*dop_pending
)
214 struct xfs_defer_pending
*dfp
;
215 const struct xfs_defer_op_type
*ops
;
217 trace_xfs_defer_trans_abort(tp
, _RET_IP_
);
219 /* Abort intent items that don't have a done item. */
220 list_for_each_entry(dfp
, dop_pending
, dfp_list
) {
221 ops
= defer_op_types
[dfp
->dfp_type
];
222 trace_xfs_defer_pending_abort(tp
->t_mountp
, dfp
);
223 if (dfp
->dfp_intent
&& !dfp
->dfp_done
) {
224 ops
->abort_intent(dfp
->dfp_intent
);
225 dfp
->dfp_intent
= NULL
;
230 /* Roll a transaction so we can do some deferred op processing. */
232 xfs_defer_trans_roll(
233 struct xfs_trans
**tpp
)
235 struct xfs_trans
*tp
= *tpp
;
236 struct xfs_buf_log_item
*bli
;
237 struct xfs_inode_log_item
*ili
;
238 struct xfs_log_item
*lip
;
239 struct xfs_buf
*bplist
[XFS_DEFER_OPS_NR_BUFS
];
240 struct xfs_inode
*iplist
[XFS_DEFER_OPS_NR_INODES
];
241 int bpcount
= 0, ipcount
= 0;
245 list_for_each_entry(lip
, &tp
->t_items
, li_trans
) {
246 switch (lip
->li_type
) {
248 bli
= container_of(lip
, struct xfs_buf_log_item
,
250 if (bli
->bli_flags
& XFS_BLI_HOLD
) {
251 if (bpcount
>= XFS_DEFER_OPS_NR_BUFS
) {
253 return -EFSCORRUPTED
;
255 xfs_trans_dirty_buf(tp
, bli
->bli_buf
);
256 bplist
[bpcount
++] = bli
->bli_buf
;
260 ili
= container_of(lip
, struct xfs_inode_log_item
,
262 if (ili
->ili_lock_flags
== 0) {
263 if (ipcount
>= XFS_DEFER_OPS_NR_INODES
) {
265 return -EFSCORRUPTED
;
267 xfs_trans_log_inode(tp
, ili
->ili_inode
,
269 iplist
[ipcount
++] = ili
->ili_inode
;
277 trace_xfs_defer_trans_roll(tp
, _RET_IP_
);
280 * Roll the transaction. Rolling always given a new transaction (even
281 * if committing the old one fails!) to hand back to the caller, so we
282 * join the held resources to the new transaction so that we always
283 * return with the held resources joined to @tpp, no matter what
286 error
= xfs_trans_roll(tpp
);
289 /* Rejoin the joined inodes. */
290 for (i
= 0; i
< ipcount
; i
++)
291 xfs_trans_ijoin(tp
, iplist
[i
], 0);
293 /* Rejoin the buffers and dirty them so the log moves forward. */
294 for (i
= 0; i
< bpcount
; i
++) {
295 xfs_trans_bjoin(tp
, bplist
[i
]);
296 xfs_trans_bhold(tp
, bplist
[i
]);
300 trace_xfs_defer_trans_roll_error(tp
, error
);
305 * Reset an already used dfops after finish.
309 struct xfs_trans
*tp
)
311 ASSERT(list_empty(&tp
->t_dfops
));
314 * Low mode state transfers across transaction rolls to mirror dfops
315 * lifetime. Clear it now that dfops is reset.
317 tp
->t_flags
&= ~XFS_TRANS_LOWMODE
;
321 * Free up any items left in the list.
324 xfs_defer_cancel_list(
325 struct xfs_mount
*mp
,
326 struct list_head
*dop_list
)
328 struct xfs_defer_pending
*dfp
;
329 struct xfs_defer_pending
*pli
;
330 struct list_head
*pwi
;
332 const struct xfs_defer_op_type
*ops
;
335 * Free the pending items. Caller should already have arranged
336 * for the intent items to be released.
338 list_for_each_entry_safe(dfp
, pli
, dop_list
, dfp_list
) {
339 ops
= defer_op_types
[dfp
->dfp_type
];
340 trace_xfs_defer_cancel_list(mp
, dfp
);
341 list_del(&dfp
->dfp_list
);
342 list_for_each_safe(pwi
, n
, &dfp
->dfp_work
) {
345 ops
->cancel_item(pwi
);
347 ASSERT(dfp
->dfp_count
== 0);
353 * Log an intent-done item for the first pending intent, and finish the work
357 xfs_defer_finish_one(
358 struct xfs_trans
*tp
,
359 struct xfs_defer_pending
*dfp
)
361 const struct xfs_defer_op_type
*ops
= defer_op_types
[dfp
->dfp_type
];
363 struct list_head
*li
, *n
;
366 trace_xfs_defer_pending_finish(tp
->t_mountp
, dfp
);
368 dfp
->dfp_done
= ops
->create_done(tp
, dfp
->dfp_intent
, dfp
->dfp_count
);
369 list_for_each_safe(li
, n
, &dfp
->dfp_work
) {
372 error
= ops
->finish_item(tp
, dfp
->dfp_done
, li
, &state
);
373 if (error
== -EAGAIN
) {
375 * Caller wants a fresh transaction; put the work item
376 * back on the list and log a new log intent item to
377 * replace the old one. See "Requesting a Fresh
378 * Transaction while Finishing Deferred Work" above.
380 list_add(li
, &dfp
->dfp_work
);
382 dfp
->dfp_done
= NULL
;
383 xfs_defer_create_intent(tp
, dfp
, false);
390 /* Done with the dfp, free it. */
391 list_del(&dfp
->dfp_list
);
394 if (ops
->finish_cleanup
)
395 ops
->finish_cleanup(tp
, state
, error
);
400 * Finish all the pending work. This involves logging intent items for
401 * any work items that wandered in since the last transaction roll (if
402 * one has even happened), rolling the transaction, and finishing the
403 * work items in the first item on the logged-and-pending list.
405 * If an inode is provided, relog it to the new transaction.
408 xfs_defer_finish_noroll(
409 struct xfs_trans
**tp
)
411 struct xfs_defer_pending
*dfp
;
413 LIST_HEAD(dop_pending
);
415 ASSERT((*tp
)->t_flags
& XFS_TRANS_PERM_LOG_RES
);
417 trace_xfs_defer_finish(*tp
, _RET_IP_
);
419 /* Until we run out of pending work to finish... */
420 while (!list_empty(&dop_pending
) || !list_empty(&(*tp
)->t_dfops
)) {
421 xfs_defer_create_intents(*tp
);
422 list_splice_tail_init(&(*tp
)->t_dfops
, &dop_pending
);
424 error
= xfs_defer_trans_roll(tp
);
428 dfp
= list_first_entry(&dop_pending
, struct xfs_defer_pending
,
430 error
= xfs_defer_finish_one(*tp
, dfp
);
431 if (error
&& error
!= -EAGAIN
)
435 trace_xfs_defer_finish_done(*tp
, _RET_IP_
);
439 xfs_defer_trans_abort(*tp
, &dop_pending
);
440 xfs_force_shutdown((*tp
)->t_mountp
, SHUTDOWN_CORRUPT_INCORE
);
441 trace_xfs_defer_finish_error(*tp
, error
);
442 xfs_defer_cancel_list((*tp
)->t_mountp
, &dop_pending
);
443 xfs_defer_cancel(*tp
);
449 struct xfs_trans
**tp
)
454 * Finish and roll the transaction once more to avoid returning to the
455 * caller with a dirty transaction.
457 error
= xfs_defer_finish_noroll(tp
);
460 if ((*tp
)->t_flags
& XFS_TRANS_DIRTY
) {
461 error
= xfs_defer_trans_roll(tp
);
463 xfs_force_shutdown((*tp
)->t_mountp
,
464 SHUTDOWN_CORRUPT_INCORE
);
468 xfs_defer_reset(*tp
);
474 struct xfs_trans
*tp
)
476 struct xfs_mount
*mp
= tp
->t_mountp
;
478 trace_xfs_defer_cancel(tp
, _RET_IP_
);
479 xfs_defer_cancel_list(mp
, &tp
->t_dfops
);
482 /* Add an item for later deferred processing. */
485 struct xfs_trans
*tp
,
486 enum xfs_defer_ops_type type
,
487 struct list_head
*li
)
489 struct xfs_defer_pending
*dfp
= NULL
;
490 const struct xfs_defer_op_type
*ops
;
492 ASSERT(tp
->t_flags
& XFS_TRANS_PERM_LOG_RES
);
493 BUILD_BUG_ON(ARRAY_SIZE(defer_op_types
) != XFS_DEFER_OPS_TYPE_MAX
);
496 * Add the item to a pending item at the end of the intake list.
497 * If the last pending item has the same type, reuse it. Else,
498 * create a new pending item at the end of the intake list.
500 if (!list_empty(&tp
->t_dfops
)) {
501 dfp
= list_last_entry(&tp
->t_dfops
,
502 struct xfs_defer_pending
, dfp_list
);
503 ops
= defer_op_types
[dfp
->dfp_type
];
504 if (dfp
->dfp_type
!= type
||
505 (ops
->max_items
&& dfp
->dfp_count
>= ops
->max_items
))
509 dfp
= kmem_alloc(sizeof(struct xfs_defer_pending
),
511 dfp
->dfp_type
= type
;
512 dfp
->dfp_intent
= NULL
;
513 dfp
->dfp_done
= NULL
;
515 INIT_LIST_HEAD(&dfp
->dfp_work
);
516 list_add_tail(&dfp
->dfp_list
, &tp
->t_dfops
);
519 list_add_tail(li
, &dfp
->dfp_work
);
524 * Move deferred ops from one transaction to another and reset the source to
525 * initial state. This is primarily used to carry state forward across
526 * transaction rolls with pending dfops.
530 struct xfs_trans
*dtp
,
531 struct xfs_trans
*stp
)
533 list_splice_init(&stp
->t_dfops
, &dtp
->t_dfops
);
536 * Low free space mode was historically controlled by a dfops field.
537 * This meant that low mode state potentially carried across multiple
538 * transaction rolls. Transfer low mode on a dfops move to preserve
541 dtp
->t_flags
|= (stp
->t_flags
& XFS_TRANS_LOWMODE
);
543 xfs_defer_reset(stp
);