]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/xfs_defer.c
libxfs: refactor manage_zones()
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_defer.c
CommitLineData
37b3b4d6 1// SPDX-License-Identifier: GPL-2.0+
a18e1f79
DW
2/*
3 * Copyright (C) 2016 Oracle. All Rights Reserved.
a18e1f79 4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
a18e1f79
DW
5 */
6#include "libxfs_priv.h"
7#include "xfs_fs.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_bit.h"
13#include "xfs_sb.h"
14#include "xfs_mount.h"
15#include "xfs_defer.h"
16#include "xfs_trans.h"
c0860494 17#include "xfs_inode.h"
a18e1f79
DW
18#include "xfs_trace.h"
19
20/*
21 * Deferred Operations in XFS
22 *
23 * Due to the way locking rules work in XFS, certain transactions (block
24 * mapping and unmapping, typically) have permanent reservations so that
25 * we can roll the transaction to adhere to AG locking order rules and
26 * to unlock buffers between metadata updates. Prior to rmap/reflink,
27 * the mapping code had a mechanism to perform these deferrals for
28 * extents that were going to be freed; this code makes that facility
29 * more generic.
30 *
31 * When adding the reverse mapping and reflink features, it became
32 * necessary to perform complex remapping multi-transactions to comply
33 * with AG locking order rules, and to be able to spread a single
34 * refcount update operation (an operation on an n-block extent can
35 * update as many as n records!) among multiple transactions. XFS can
36 * roll a transaction to facilitate this, but using this facility
37 * requires us to log "intent" items in case log recovery needs to
38 * redo the operation, and to log "done" items to indicate that redo
39 * is not necessary.
40 *
41 * Deferred work is tracked in xfs_defer_pending items. Each pending
42 * item tracks one type of deferred work. Incoming work items (which
43 * have not yet had an intent logged) are attached to a pending item
44 * on the dop_intake list, where they wait for the caller to finish
45 * the deferred operations.
46 *
47 * Finishing a set of deferred operations is an involved process. To
48 * start, we define "rolling a deferred-op transaction" as follows:
49 *
50 * > For each xfs_defer_pending item on the dop_intake list,
51 * - Sort the work items in AG order. XFS locking
52 * order rules require us to lock buffers in AG order.
53 * - Create a log intent item for that type.
54 * - Attach it to the pending item.
55 * - Move the pending item from the dop_intake list to the
56 * dop_pending list.
57 * > Roll the transaction.
58 *
59 * NOTE: To avoid exceeding the transaction reservation, we limit the
60 * number of items that we attach to a given xfs_defer_pending.
61 *
62 * The actual finishing process looks like this:
63 *
64 * > For each xfs_defer_pending in the dop_pending list,
65 * - Roll the deferred-op transaction as above.
66 * - Create a log done item for that type, and attach it to the
67 * log intent item.
68 * - For each work item attached to the log intent item,
69 * * Perform the described action.
70 * * Attach the work item to the log done item.
0590692e
DW
71 * * If the result of doing the work was -EAGAIN, ->finish work
72 * wants a new transaction. See the "Requesting a Fresh
73 * Transaction while Finishing Deferred Work" section below for
74 * details.
a18e1f79
DW
75 *
76 * The key here is that we must log an intent item for all pending
77 * work items every time we roll the transaction, and that we must log
78 * a done item as soon as the work is completed. With this mechanism
79 * we can perform complex remapping operations, chaining intent items
80 * as needed.
81 *
0590692e
DW
82 * Requesting a Fresh Transaction while Finishing Deferred Work
83 *
84 * If ->finish_item decides that it needs a fresh transaction to
85 * finish the work, it must ask its caller (xfs_defer_finish) for a
86 * continuation. The most likely cause of this circumstance are the
87 * refcount adjust functions deciding that they've logged enough items
88 * to be at risk of exceeding the transaction reservation.
89 *
90 * To get a fresh transaction, we want to log the existing log done
91 * item to prevent the log intent item from replaying, immediately log
92 * a new log intent item with the unfinished work items, roll the
93 * transaction, and re-call ->finish_item wherever it left off. The
94 * log done item and the new log intent item must be in the same
95 * transaction or atomicity cannot be guaranteed; defer_finish ensures
96 * that this happens.
97 *
98 * This requires some coordination between ->finish_item and
99 * defer_finish. Upon deciding to request a new transaction,
100 * ->finish_item should update the current work item to reflect the
101 * unfinished work. Next, it should reset the log done item's list
102 * count to the number of items finished, and return -EAGAIN.
103 * defer_finish sees the -EAGAIN, logs the new log intent item
104 * with the remaining work items, and leaves the xfs_defer_pending
105 * item at the head of the dop_work queue. Then it rolls the
106 * transaction and picks up processing where it left off. It is
107 * required that ->finish_item must be careful to leave enough
108 * transaction reservation to fit the new log intent item.
109 *
a18e1f79
DW
110 * This is an example of remapping the extent (E, E+B) into file X at
111 * offset A and dealing with the extent (C, C+B) already being mapped
112 * there:
113 * +-------------------------------------------------+
114 * | Unmap file X startblock C offset A length B | t0
115 * | Intent to reduce refcount for extent (C, B) |
116 * | Intent to remove rmap (X, C, A, B) |
117 * | Intent to free extent (D, 1) (bmbt block) |
118 * | Intent to map (X, A, B) at startblock E |
119 * +-------------------------------------------------+
120 * | Map file X startblock E offset A length B | t1
121 * | Done mapping (X, E, A, B) |
122 * | Intent to increase refcount for extent (E, B) |
123 * | Intent to add rmap (X, E, A, B) |
124 * +-------------------------------------------------+
125 * | Reduce refcount for extent (C, B) | t2
0590692e
DW
126 * | Done reducing refcount for extent (C, 9) |
127 * | Intent to reduce refcount for extent (C+9, B-9) |
128 * | (ran out of space after 9 refcount updates) |
129 * +-------------------------------------------------+
130 * | Reduce refcount for extent (C+9, B+9) | t3
131 * | Done reducing refcount for extent (C+9, B-9) |
a18e1f79
DW
132 * | Increase refcount for extent (E, B) |
133 * | Done increasing refcount for extent (E, B) |
134 * | Intent to free extent (C, B) |
135 * | Intent to free extent (F, 1) (refcountbt block) |
136 * | Intent to remove rmap (F, 1, REFC) |
137 * +-------------------------------------------------+
0590692e 138 * | Remove rmap (X, C, A, B) | t4
a18e1f79
DW
139 * | Done removing rmap (X, C, A, B) |
140 * | Add rmap (X, E, A, B) |
141 * | Done adding rmap (X, E, A, B) |
142 * | Remove rmap (F, 1, REFC) |
143 * | Done removing rmap (F, 1, REFC) |
144 * +-------------------------------------------------+
0590692e 145 * | Free extent (C, B) | t5
a18e1f79
DW
146 * | Done freeing extent (C, B) |
147 * | Free extent (D, 1) |
148 * | Done freeing extent (D, 1) |
149 * | Free extent (F, 1) |
150 * | Done freeing extent (F, 1) |
151 * +-------------------------------------------------+
152 *
153 * If we should crash before t2 commits, log recovery replays
154 * the following intent items:
155 *
156 * - Intent to reduce refcount for extent (C, B)
157 * - Intent to remove rmap (X, C, A, B)
158 * - Intent to free extent (D, 1) (bmbt block)
159 * - Intent to increase refcount for extent (E, B)
160 * - Intent to add rmap (X, E, A, B)
161 *
162 * In the process of recovering, it should also generate and take care
163 * of these intent items:
164 *
165 * - Intent to free extent (C, B)
166 * - Intent to free extent (F, 1) (refcountbt block)
167 * - Intent to remove rmap (F, 1, REFC)
0590692e
DW
168 *
169 * Note that the continuation requested between t2 and t3 is likely to
170 * reoccur.
a18e1f79
DW
171 */
172
29ce8c42
DW
173static const struct xfs_defer_op_type *defer_op_types[] = {
174 [XFS_DEFER_OPS_TYPE_BMAP] = &xfs_bmap_update_defer_type,
175 [XFS_DEFER_OPS_TYPE_REFCOUNT] = &xfs_refcount_update_defer_type,
176 [XFS_DEFER_OPS_TYPE_RMAP] = &xfs_rmap_update_defer_type,
177 [XFS_DEFER_OPS_TYPE_FREE] = &xfs_extent_free_defer_type,
178 [XFS_DEFER_OPS_TYPE_AGFL_FREE] = &xfs_agfl_free_defer_type,
179};
a18e1f79
DW
180
181/*
182 * For each pending item in the intake list, log its intent item and the
183 * associated extents, then add the entire intake list to the end of
184 * the pending list.
185 */
186STATIC void
3e1da129 187xfs_defer_create_intents(
1e6b0a71 188 struct xfs_trans *tp)
a18e1f79
DW
189{
190 struct list_head *li;
191 struct xfs_defer_pending *dfp;
c3514397 192 const struct xfs_defer_op_type *ops;
a18e1f79 193
92a8736e 194 list_for_each_entry(dfp, &tp->t_dfops, dfp_list) {
c3514397
DW
195 ops = defer_op_types[dfp->dfp_type];
196 dfp->dfp_intent = ops->create_intent(tp, dfp->dfp_count);
3e1da129 197 trace_xfs_defer_create_intent(tp->t_mountp, dfp);
c3514397 198 list_sort(tp->t_mountp, &dfp->dfp_work, ops->diff_items);
a18e1f79 199 list_for_each(li, &dfp->dfp_work)
c3514397 200 ops->log_item(tp, dfp->dfp_intent, li);
a18e1f79 201 }
a18e1f79
DW
202}
203
204/* Abort all the intents that were committed. */
205STATIC void
206xfs_defer_trans_abort(
207 struct xfs_trans *tp,
3e1da129 208 struct list_head *dop_pending)
a18e1f79
DW
209{
210 struct xfs_defer_pending *dfp;
c3514397 211 const struct xfs_defer_op_type *ops;
a18e1f79 212
92a8736e 213 trace_xfs_defer_trans_abort(tp, _RET_IP_);
a18e1f79 214
077dd509 215 /* Abort intent items that don't have a done item. */
3e1da129 216 list_for_each_entry(dfp, dop_pending, dfp_list) {
c3514397 217 ops = defer_op_types[dfp->dfp_type];
30ad7d6e 218 trace_xfs_defer_pending_abort(tp->t_mountp, dfp);
077dd509 219 if (dfp->dfp_intent && !dfp->dfp_done) {
c3514397 220 ops->abort_intent(dfp->dfp_intent);
077dd509
DW
221 dfp->dfp_intent = NULL;
222 }
a18e1f79 223 }
a18e1f79
DW
224}
225
226/* Roll a transaction so we can do some deferred op processing. */
227STATIC int
228xfs_defer_trans_roll(
3e1da129 229 struct xfs_trans **tpp)
a18e1f79 230{
3e1da129 231 struct xfs_trans *tp = *tpp;
f2ea6bf0 232 struct xfs_buf_log_item *bli;
c0860494 233 struct xfs_inode_log_item *ili;
f2ea6bf0
BF
234 struct xfs_log_item *lip;
235 struct xfs_buf *bplist[XFS_DEFER_OPS_NR_BUFS];
c0860494
BF
236 struct xfs_inode *iplist[XFS_DEFER_OPS_NR_INODES];
237 int bpcount = 0, ipcount = 0;
a18e1f79
DW
238 int i;
239 int error;
240
3e1da129 241 list_for_each_entry(lip, &tp->t_items, li_trans) {
f2ea6bf0
BF
242 switch (lip->li_type) {
243 case XFS_LI_BUF:
244 bli = container_of(lip, struct xfs_buf_log_item,
245 bli_item);
246 if (bli->bli_flags & XFS_BLI_HOLD) {
247 if (bpcount >= XFS_DEFER_OPS_NR_BUFS) {
248 ASSERT(0);
249 return -EFSCORRUPTED;
250 }
3e1da129 251 xfs_trans_dirty_buf(tp, bli->bli_buf);
f2ea6bf0
BF
252 bplist[bpcount++] = bli->bli_buf;
253 }
254 break;
c0860494
BF
255 case XFS_LI_INODE:
256 ili = container_of(lip, struct xfs_inode_log_item,
257 ili_item);
258 if (ili->ili_lock_flags == 0) {
259 if (ipcount >= XFS_DEFER_OPS_NR_INODES) {
260 ASSERT(0);
261 return -EFSCORRUPTED;
262 }
3e1da129 263 xfs_trans_log_inode(tp, ili->ili_inode,
c0860494
BF
264 XFS_ILOG_CORE);
265 iplist[ipcount++] = ili->ili_inode;
266 }
267 break;
f2ea6bf0
BF
268 default:
269 break;
270 }
271 }
6f67c32d 272
92a8736e 273 trace_xfs_defer_trans_roll(tp, _RET_IP_);
30ad7d6e 274
a18e1f79 275 /* Roll the transaction. */
3e1da129
BF
276 error = xfs_trans_roll(tpp);
277 tp = *tpp;
a18e1f79 278 if (error) {
92a8736e 279 trace_xfs_defer_trans_roll_error(tp, error);
a18e1f79
DW
280 return error;
281 }
a18e1f79 282
d67406c9 283 /* Rejoin the joined inodes. */
c0860494 284 for (i = 0; i < ipcount; i++)
3e1da129 285 xfs_trans_ijoin(tp, iplist[i], 0);
a18e1f79 286
6f67c32d 287 /* Rejoin the buffers and dirty them so the log moves forward. */
f2ea6bf0 288 for (i = 0; i < bpcount; i++) {
3e1da129
BF
289 xfs_trans_bjoin(tp, bplist[i]);
290 xfs_trans_bhold(tp, bplist[i]);
6f67c32d
DW
291 }
292
a18e1f79
DW
293 return error;
294}
295
1a3fd2f9
BF
296/*
297 * Reset an already used dfops after finish.
298 */
299static void
300xfs_defer_reset(
76a3c33d 301 struct xfs_trans *tp)
1a3fd2f9 302{
92a8736e 303 ASSERT(list_empty(&tp->t_dfops));
565e96c6
BF
304
305 /*
306 * Low mode state transfers across transaction rolls to mirror dfops
307 * lifetime. Clear it now that dfops is reset.
308 */
309 tp->t_flags &= ~XFS_TRANS_LOWMODE;
1a3fd2f9
BF
310}
311
3e1da129
BF
312/*
313 * Free up any items left in the list.
314 */
315static void
316xfs_defer_cancel_list(
317 struct xfs_mount *mp,
318 struct list_head *dop_list)
319{
320 struct xfs_defer_pending *dfp;
321 struct xfs_defer_pending *pli;
322 struct list_head *pwi;
323 struct list_head *n;
c3514397 324 const struct xfs_defer_op_type *ops;
3e1da129
BF
325
326 /*
327 * Free the pending items. Caller should already have arranged
328 * for the intent items to be released.
329 */
330 list_for_each_entry_safe(dfp, pli, dop_list, dfp_list) {
c3514397 331 ops = defer_op_types[dfp->dfp_type];
3e1da129
BF
332 trace_xfs_defer_cancel_list(mp, dfp);
333 list_del(&dfp->dfp_list);
334 list_for_each_safe(pwi, n, &dfp->dfp_work) {
335 list_del(pwi);
336 dfp->dfp_count--;
c3514397 337 ops->cancel_item(pwi);
3e1da129
BF
338 }
339 ASSERT(dfp->dfp_count == 0);
340 kmem_free(dfp);
341 }
342}
343
a18e1f79
DW
344/*
345 * Finish all the pending work. This involves logging intent items for
346 * any work items that wandered in since the last transaction roll (if
347 * one has even happened), rolling the transaction, and finishing the
348 * work items in the first item on the logged-and-pending list.
349 *
350 * If an inode is provided, relog it to the new transaction.
351 */
352int
ca7e896f 353xfs_defer_finish_noroll(
ac0a2228 354 struct xfs_trans **tp)
a18e1f79
DW
355{
356 struct xfs_defer_pending *dfp;
357 struct list_head *li;
358 struct list_head *n;
a18e1f79
DW
359 void *state;
360 int error = 0;
c3514397 361 const struct xfs_defer_op_type *ops;
3e1da129 362 LIST_HEAD(dop_pending);
a18e1f79
DW
363
364 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
365
92a8736e 366 trace_xfs_defer_finish(*tp, _RET_IP_);
30ad7d6e 367
a18e1f79 368 /* Until we run out of pending work to finish... */
92a8736e 369 while (!list_empty(&dop_pending) || !list_empty(&(*tp)->t_dfops)) {
3e1da129
BF
370 /* log intents and pull in intake items */
371 xfs_defer_create_intents(*tp);
92a8736e 372 list_splice_tail_init(&(*tp)->t_dfops, &dop_pending);
a18e1f79 373
ad84d074 374 /*
1e6b0a71 375 * Roll the transaction.
ad84d074
BF
376 */
377 error = xfs_defer_trans_roll(tp);
a18e1f79
DW
378 if (error)
379 goto out;
380
a18e1f79 381 /* Log an intent-done item for the first pending item. */
3e1da129
BF
382 dfp = list_first_entry(&dop_pending, struct xfs_defer_pending,
383 dfp_list);
c3514397 384 ops = defer_op_types[dfp->dfp_type];
30ad7d6e 385 trace_xfs_defer_pending_finish((*tp)->t_mountp, dfp);
c3514397 386 dfp->dfp_done = ops->create_done(*tp, dfp->dfp_intent,
a18e1f79 387 dfp->dfp_count);
a18e1f79
DW
388
389 /* Finish the work items. */
390 state = NULL;
391 list_for_each_safe(li, n, &dfp->dfp_work) {
392 list_del(li);
393 dfp->dfp_count--;
c3514397
DW
394 error = ops->finish_item(*tp, li, dfp->dfp_done,
395 &state);
0590692e
DW
396 if (error == -EAGAIN) {
397 /*
398 * Caller wants a fresh transaction;
399 * put the work item back on the list
400 * and jump out.
401 */
402 list_add(li, &dfp->dfp_work);
403 dfp->dfp_count++;
404 break;
405 } else if (error) {
a18e1f79
DW
406 /*
407 * Clean up after ourselves and jump out.
408 * xfs_defer_cancel will take care of freeing
409 * all these lists and stuff.
410 */
c3514397
DW
411 if (ops->finish_cleanup)
412 ops->finish_cleanup(*tp, state, error);
a18e1f79
DW
413 goto out;
414 }
415 }
0590692e
DW
416 if (error == -EAGAIN) {
417 /*
418 * Caller wants a fresh transaction, so log a
419 * new log intent item to replace the old one
420 * and roll the transaction. See "Requesting
421 * a Fresh Transaction while Finishing
422 * Deferred Work" above.
423 */
c3514397 424 dfp->dfp_intent = ops->create_intent(*tp,
0590692e
DW
425 dfp->dfp_count);
426 dfp->dfp_done = NULL;
427 list_for_each(li, &dfp->dfp_work)
c3514397 428 ops->log_item(*tp, dfp->dfp_intent, li);
0590692e
DW
429 } else {
430 /* Done with the dfp, free it. */
431 list_del(&dfp->dfp_list);
432 kmem_free(dfp);
433 }
a18e1f79 434
c3514397
DW
435 if (ops->finish_cleanup)
436 ops->finish_cleanup(*tp, state, error);
a18e1f79
DW
437 }
438
439out:
9f5a828b 440 if (error) {
3e1da129
BF
441 xfs_defer_trans_abort(*tp, &dop_pending);
442 xfs_force_shutdown((*tp)->t_mountp, SHUTDOWN_CORRUPT_INCORE);
92a8736e 443 trace_xfs_defer_finish_error(*tp, error);
3e1da129 444 xfs_defer_cancel_list((*tp)->t_mountp, &dop_pending);
9f5a828b
BF
445 xfs_defer_cancel(*tp);
446 return error;
447 }
1a3fd2f9 448
92a8736e 449 trace_xfs_defer_finish_done(*tp, _RET_IP_);
9f5a828b 450 return 0;
a18e1f79
DW
451}
452
ca7e896f
BF
453int
454xfs_defer_finish(
455 struct xfs_trans **tp)
456{
457 int error;
458
459 /*
460 * Finish and roll the transaction once more to avoid returning to the
461 * caller with a dirty transaction.
462 */
463 error = xfs_defer_finish_noroll(tp);
464 if (error)
465 return error;
466 if ((*tp)->t_flags & XFS_TRANS_DIRTY) {
467 error = xfs_defer_trans_roll(tp);
3e1da129
BF
468 if (error) {
469 xfs_force_shutdown((*tp)->t_mountp,
470 SHUTDOWN_CORRUPT_INCORE);
ca7e896f 471 return error;
3e1da129 472 }
ca7e896f 473 }
76a3c33d 474 xfs_defer_reset(*tp);
ca7e896f
BF
475 return 0;
476}
477
a18e1f79 478void
22913550 479xfs_defer_cancel(
3e1da129 480 struct xfs_trans *tp)
a18e1f79 481{
3e1da129 482 struct xfs_mount *mp = tp->t_mountp;
a18e1f79 483
92a8736e
BF
484 trace_xfs_defer_cancel(tp, _RET_IP_);
485 xfs_defer_cancel_list(mp, &tp->t_dfops);
a18e1f79
DW
486}
487
488/* Add an item for later deferred processing. */
489void
490xfs_defer_add(
21375e5d 491 struct xfs_trans *tp,
a18e1f79
DW
492 enum xfs_defer_ops_type type,
493 struct list_head *li)
494{
495 struct xfs_defer_pending *dfp = NULL;
c3514397 496 const struct xfs_defer_op_type *ops;
a18e1f79 497
21375e5d 498 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
29ce8c42 499 BUILD_BUG_ON(ARRAY_SIZE(defer_op_types) != XFS_DEFER_OPS_TYPE_MAX);
21375e5d 500
a18e1f79
DW
501 /*
502 * Add the item to a pending item at the end of the intake list.
503 * If the last pending item has the same type, reuse it. Else,
504 * create a new pending item at the end of the intake list.
505 */
92a8736e
BF
506 if (!list_empty(&tp->t_dfops)) {
507 dfp = list_last_entry(&tp->t_dfops,
a18e1f79 508 struct xfs_defer_pending, dfp_list);
c3514397
DW
509 ops = defer_op_types[dfp->dfp_type];
510 if (dfp->dfp_type != type ||
511 (ops->max_items && dfp->dfp_count >= ops->max_items))
a18e1f79
DW
512 dfp = NULL;
513 }
514 if (!dfp) {
515 dfp = kmem_alloc(sizeof(struct xfs_defer_pending),
516 KM_SLEEP | KM_NOFS);
c3514397 517 dfp->dfp_type = type;
a18e1f79 518 dfp->dfp_intent = NULL;
11d87237 519 dfp->dfp_done = NULL;
a18e1f79
DW
520 dfp->dfp_count = 0;
521 INIT_LIST_HEAD(&dfp->dfp_work);
92a8736e 522 list_add_tail(&dfp->dfp_list, &tp->t_dfops);
a18e1f79
DW
523 }
524
525 list_add_tail(li, &dfp->dfp_work);
526 dfp->dfp_count++;
527}
528
f5f4497f 529/*
92a8736e
BF
530 * Move deferred ops from one transaction to another and reset the source to
531 * initial state. This is primarily used to carry state forward across
532 * transaction rolls with pending dfops.
f5f4497f
BF
533 */
534void
535xfs_defer_move(
76a3c33d
BF
536 struct xfs_trans *dtp,
537 struct xfs_trans *stp)
f5f4497f 538{
92a8736e 539 list_splice_init(&stp->t_dfops, &dtp->t_dfops);
f5f4497f 540
565e96c6
BF
541 /*
542 * Low free space mode was historically controlled by a dfops field.
543 * This meant that low mode state potentially carried across multiple
544 * transaction rolls. Transfer low mode on a dfops move to preserve
545 * that behavior.
546 */
547 dtp->t_flags |= (stp->t_flags & XFS_TRANS_LOWMODE);
f5f4497f 548
76a3c33d 549 xfs_defer_reset(stp);
f5f4497f 550}