]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_defer.c
xfs: turn dfp_done into a xfs_log_item
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_defer.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2016 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
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_mount.h"
13 #include "xfs_defer.h"
14 #include "xfs_trans.h"
15 #include "xfs_inode.h"
16 #include "xfs_trace.h"
17
18 /*
19 * Deferred Operations in XFS
20 *
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
27 * more generic.
28 *
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
37 * is not necessary.
38 *
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.
44 *
45 * Finishing a set of deferred operations is an involved process. To
46 * start, we define "rolling a deferred-op transaction" as follows:
47 *
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
54 * dop_pending list.
55 * > Roll the transaction.
56 *
57 * NOTE: To avoid exceeding the transaction reservation, we limit the
58 * number of items that we attach to a given xfs_defer_pending.
59 *
60 * The actual finishing process looks like this:
61 *
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
65 * log intent item.
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
72 * details.
73 *
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
78 * as needed.
79 *
80 * Requesting a Fresh Transaction while Finishing Deferred Work
81 *
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.
87 *
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
94 * that this happens.
95 *
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.
107 *
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
110 * there:
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 * +-------------------------------------------------+
150 *
151 * If we should crash before t2 commits, log recovery replays
152 * the following intent items:
153 *
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)
159 *
160 * In the process of recovering, it should also generate and take care
161 * of these intent items:
162 *
163 * - Intent to free extent (C, B)
164 * - Intent to free extent (F, 1) (refcountbt block)
165 * - Intent to remove rmap (F, 1, REFC)
166 *
167 * Note that the continuation requested between t2 and t3 is likely to
168 * reoccur.
169 */
170
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,
177 };
178
179 static void
180 xfs_defer_create_intent(
181 struct xfs_trans *tp,
182 struct xfs_defer_pending *dfp,
183 bool sort)
184 {
185 const struct xfs_defer_op_type *ops = defer_op_types[dfp->dfp_type];
186
187 dfp->dfp_intent = ops->create_intent(tp, &dfp->dfp_work,
188 dfp->dfp_count, sort);
189 }
190
191 /*
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
194 * the pending list.
195 */
196 STATIC void
197 xfs_defer_create_intents(
198 struct xfs_trans *tp)
199 {
200 struct xfs_defer_pending *dfp;
201
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);
205 }
206 }
207
208 /* Abort all the intents that were committed. */
209 STATIC void
210 xfs_defer_trans_abort(
211 struct xfs_trans *tp,
212 struct list_head *dop_pending)
213 {
214 struct xfs_defer_pending *dfp;
215 const struct xfs_defer_op_type *ops;
216
217 trace_xfs_defer_trans_abort(tp, _RET_IP_);
218
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;
226 }
227 }
228 }
229
230 /* Roll a transaction so we can do some deferred op processing. */
231 STATIC int
232 xfs_defer_trans_roll(
233 struct xfs_trans **tpp)
234 {
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;
242 int i;
243 int error;
244
245 list_for_each_entry(lip, &tp->t_items, li_trans) {
246 switch (lip->li_type) {
247 case XFS_LI_BUF:
248 bli = container_of(lip, struct xfs_buf_log_item,
249 bli_item);
250 if (bli->bli_flags & XFS_BLI_HOLD) {
251 if (bpcount >= XFS_DEFER_OPS_NR_BUFS) {
252 ASSERT(0);
253 return -EFSCORRUPTED;
254 }
255 xfs_trans_dirty_buf(tp, bli->bli_buf);
256 bplist[bpcount++] = bli->bli_buf;
257 }
258 break;
259 case XFS_LI_INODE:
260 ili = container_of(lip, struct xfs_inode_log_item,
261 ili_item);
262 if (ili->ili_lock_flags == 0) {
263 if (ipcount >= XFS_DEFER_OPS_NR_INODES) {
264 ASSERT(0);
265 return -EFSCORRUPTED;
266 }
267 xfs_trans_log_inode(tp, ili->ili_inode,
268 XFS_ILOG_CORE);
269 iplist[ipcount++] = ili->ili_inode;
270 }
271 break;
272 default:
273 break;
274 }
275 }
276
277 trace_xfs_defer_trans_roll(tp, _RET_IP_);
278
279 /*
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
284 * happened.
285 */
286 error = xfs_trans_roll(tpp);
287 tp = *tpp;
288
289 /* Rejoin the joined inodes. */
290 for (i = 0; i < ipcount; i++)
291 xfs_trans_ijoin(tp, iplist[i], 0);
292
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]);
297 }
298
299 if (error)
300 trace_xfs_defer_trans_roll_error(tp, error);
301 return error;
302 }
303
304 /*
305 * Reset an already used dfops after finish.
306 */
307 static void
308 xfs_defer_reset(
309 struct xfs_trans *tp)
310 {
311 ASSERT(list_empty(&tp->t_dfops));
312
313 /*
314 * Low mode state transfers across transaction rolls to mirror dfops
315 * lifetime. Clear it now that dfops is reset.
316 */
317 tp->t_flags &= ~XFS_TRANS_LOWMODE;
318 }
319
320 /*
321 * Free up any items left in the list.
322 */
323 static void
324 xfs_defer_cancel_list(
325 struct xfs_mount *mp,
326 struct list_head *dop_list)
327 {
328 struct xfs_defer_pending *dfp;
329 struct xfs_defer_pending *pli;
330 struct list_head *pwi;
331 struct list_head *n;
332 const struct xfs_defer_op_type *ops;
333
334 /*
335 * Free the pending items. Caller should already have arranged
336 * for the intent items to be released.
337 */
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) {
343 list_del(pwi);
344 dfp->dfp_count--;
345 ops->cancel_item(pwi);
346 }
347 ASSERT(dfp->dfp_count == 0);
348 kmem_free(dfp);
349 }
350 }
351
352 /*
353 * Log an intent-done item for the first pending intent, and finish the work
354 * items.
355 */
356 static int
357 xfs_defer_finish_one(
358 struct xfs_trans *tp,
359 struct xfs_defer_pending *dfp)
360 {
361 const struct xfs_defer_op_type *ops = defer_op_types[dfp->dfp_type];
362 void *state = NULL;
363 struct list_head *li, *n;
364 int error;
365
366 trace_xfs_defer_pending_finish(tp->t_mountp, dfp);
367
368 dfp->dfp_done = ops->create_done(tp, dfp->dfp_intent, dfp->dfp_count);
369 list_for_each_safe(li, n, &dfp->dfp_work) {
370 list_del(li);
371 dfp->dfp_count--;
372 error = ops->finish_item(tp, dfp->dfp_done, li, &state);
373 if (error == -EAGAIN) {
374 /*
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.
379 */
380 list_add(li, &dfp->dfp_work);
381 dfp->dfp_count++;
382 dfp->dfp_done = NULL;
383 xfs_defer_create_intent(tp, dfp, false);
384 }
385
386 if (error)
387 goto out;
388 }
389
390 /* Done with the dfp, free it. */
391 list_del(&dfp->dfp_list);
392 kmem_free(dfp);
393 out:
394 if (ops->finish_cleanup)
395 ops->finish_cleanup(tp, state, error);
396 return error;
397 }
398
399 /*
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.
404 *
405 * If an inode is provided, relog it to the new transaction.
406 */
407 int
408 xfs_defer_finish_noroll(
409 struct xfs_trans **tp)
410 {
411 struct xfs_defer_pending *dfp;
412 int error = 0;
413 LIST_HEAD(dop_pending);
414
415 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
416
417 trace_xfs_defer_finish(*tp, _RET_IP_);
418
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);
423
424 error = xfs_defer_trans_roll(tp);
425 if (error)
426 goto out_shutdown;
427
428 dfp = list_first_entry(&dop_pending, struct xfs_defer_pending,
429 dfp_list);
430 error = xfs_defer_finish_one(*tp, dfp);
431 if (error && error != -EAGAIN)
432 goto out_shutdown;
433 }
434
435 trace_xfs_defer_finish_done(*tp, _RET_IP_);
436 return 0;
437
438 out_shutdown:
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);
444 return error;
445 }
446
447 int
448 xfs_defer_finish(
449 struct xfs_trans **tp)
450 {
451 int error;
452
453 /*
454 * Finish and roll the transaction once more to avoid returning to the
455 * caller with a dirty transaction.
456 */
457 error = xfs_defer_finish_noroll(tp);
458 if (error)
459 return error;
460 if ((*tp)->t_flags & XFS_TRANS_DIRTY) {
461 error = xfs_defer_trans_roll(tp);
462 if (error) {
463 xfs_force_shutdown((*tp)->t_mountp,
464 SHUTDOWN_CORRUPT_INCORE);
465 return error;
466 }
467 }
468 xfs_defer_reset(*tp);
469 return 0;
470 }
471
472 void
473 xfs_defer_cancel(
474 struct xfs_trans *tp)
475 {
476 struct xfs_mount *mp = tp->t_mountp;
477
478 trace_xfs_defer_cancel(tp, _RET_IP_);
479 xfs_defer_cancel_list(mp, &tp->t_dfops);
480 }
481
482 /* Add an item for later deferred processing. */
483 void
484 xfs_defer_add(
485 struct xfs_trans *tp,
486 enum xfs_defer_ops_type type,
487 struct list_head *li)
488 {
489 struct xfs_defer_pending *dfp = NULL;
490 const struct xfs_defer_op_type *ops;
491
492 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
493 BUILD_BUG_ON(ARRAY_SIZE(defer_op_types) != XFS_DEFER_OPS_TYPE_MAX);
494
495 /*
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.
499 */
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))
506 dfp = NULL;
507 }
508 if (!dfp) {
509 dfp = kmem_alloc(sizeof(struct xfs_defer_pending),
510 KM_NOFS);
511 dfp->dfp_type = type;
512 dfp->dfp_intent = NULL;
513 dfp->dfp_done = NULL;
514 dfp->dfp_count = 0;
515 INIT_LIST_HEAD(&dfp->dfp_work);
516 list_add_tail(&dfp->dfp_list, &tp->t_dfops);
517 }
518
519 list_add_tail(li, &dfp->dfp_work);
520 dfp->dfp_count++;
521 }
522
523 /*
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.
527 */
528 void
529 xfs_defer_move(
530 struct xfs_trans *dtp,
531 struct xfs_trans *stp)
532 {
533 list_splice_init(&stp->t_dfops, &dtp->t_dfops);
534
535 /*
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
539 * that behavior.
540 */
541 dtp->t_flags |= (stp->t_flags & XFS_TRANS_LOWMODE);
542
543 xfs_defer_reset(stp);
544 }