xfs: refactor xfs_defer_finish_noroll
[linux.git] / fs / xfs / xfs_bmap_item.c
CommitLineData
0b61f8a4 1// SPDX-License-Identifier: GPL-2.0+
6413a014
DW
2/*
3 * Copyright (C) 2016 Oracle. All Rights Reserved.
6413a014 4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
6413a014
DW
5 */
6#include "xfs.h"
7#include "xfs_fs.h"
8#include "xfs_format.h"
9#include "xfs_log_format.h"
10#include "xfs_trans_resv.h"
77d61fe4 11#include "xfs_bit.h"
5467b34b 12#include "xfs_shared.h"
6413a014 13#include "xfs_mount.h"
77d61fe4
DW
14#include "xfs_defer.h"
15#include "xfs_inode.h"
6413a014
DW
16#include "xfs_trans.h"
17#include "xfs_trans_priv.h"
6413a014
DW
18#include "xfs_bmap_item.h"
19#include "xfs_log.h"
77d61fe4
DW
20#include "xfs_bmap.h"
21#include "xfs_icache.h"
fe0be23e
DW
22#include "xfs_bmap_btree.h"
23#include "xfs_trans_space.h"
a5155b87 24#include "xfs_error.h"
6413a014
DW
25
26kmem_zone_t *xfs_bui_zone;
27kmem_zone_t *xfs_bud_zone;
28
29static inline struct xfs_bui_log_item *BUI_ITEM(struct xfs_log_item *lip)
30{
31 return container_of(lip, struct xfs_bui_log_item, bui_item);
32}
33
34void
35xfs_bui_item_free(
36 struct xfs_bui_log_item *buip)
37{
377bcd5f 38 kmem_cache_free(xfs_bui_zone, buip);
6413a014
DW
39}
40
0612d116
DC
41/*
42 * Freeing the BUI requires that we remove it from the AIL if it has already
43 * been placed there. However, the BUI may not yet have been placed in the AIL
44 * when called by xfs_bui_release() from BUD processing due to the ordering of
45 * committed vs unpin operations in bulk insert operations. Hence the reference
46 * count to ensure only the last caller frees the BUI.
47 */
48void
49xfs_bui_release(
50 struct xfs_bui_log_item *buip)
51{
52 ASSERT(atomic_read(&buip->bui_refcount) > 0);
53 if (atomic_dec_and_test(&buip->bui_refcount)) {
54 xfs_trans_ail_remove(&buip->bui_item, SHUTDOWN_LOG_IO_ERROR);
55 xfs_bui_item_free(buip);
56 }
57}
58
59
6413a014
DW
60STATIC void
61xfs_bui_item_size(
62 struct xfs_log_item *lip,
63 int *nvecs,
64 int *nbytes)
65{
66 struct xfs_bui_log_item *buip = BUI_ITEM(lip);
67
68 *nvecs += 1;
69 *nbytes += xfs_bui_log_format_sizeof(buip->bui_format.bui_nextents);
70}
71
72/*
73 * This is called to fill in the vector of log iovecs for the
74 * given bui log item. We use only 1 iovec, and we point that
75 * at the bui_log_format structure embedded in the bui item.
76 * It is at this point that we assert that all of the extent
77 * slots in the bui item have been filled.
78 */
79STATIC void
80xfs_bui_item_format(
81 struct xfs_log_item *lip,
82 struct xfs_log_vec *lv)
83{
84 struct xfs_bui_log_item *buip = BUI_ITEM(lip);
85 struct xfs_log_iovec *vecp = NULL;
86
87 ASSERT(atomic_read(&buip->bui_next_extent) ==
88 buip->bui_format.bui_nextents);
89
90 buip->bui_format.bui_type = XFS_LI_BUI;
91 buip->bui_format.bui_size = 1;
92
93 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_BUI_FORMAT, &buip->bui_format,
94 xfs_bui_log_format_sizeof(buip->bui_format.bui_nextents));
95}
96
6413a014
DW
97/*
98 * The unpin operation is the last place an BUI is manipulated in the log. It is
99 * either inserted in the AIL or aborted in the event of a log I/O error. In
100 * either case, the BUI transaction has been successfully committed to make it
101 * this far. Therefore, we expect whoever committed the BUI to either construct
102 * and commit the BUD or drop the BUD's reference in the event of error. Simply
103 * drop the log's BUI reference now that the log is done with it.
104 */
105STATIC void
106xfs_bui_item_unpin(
107 struct xfs_log_item *lip,
108 int remove)
109{
110 struct xfs_bui_log_item *buip = BUI_ITEM(lip);
111
112 xfs_bui_release(buip);
113}
114
6413a014
DW
115/*
116 * The BUI has been either committed or aborted if the transaction has been
117 * cancelled. If the transaction was cancelled, an BUD isn't going to be
118 * constructed and thus we free the BUI here directly.
119 */
120STATIC void
ddf92053 121xfs_bui_item_release(
6413a014
DW
122 struct xfs_log_item *lip)
123{
ddf92053 124 xfs_bui_release(BUI_ITEM(lip));
6413a014
DW
125}
126
6413a014
DW
127static const struct xfs_item_ops xfs_bui_item_ops = {
128 .iop_size = xfs_bui_item_size,
129 .iop_format = xfs_bui_item_format,
6413a014 130 .iop_unpin = xfs_bui_item_unpin,
ddf92053 131 .iop_release = xfs_bui_item_release,
6413a014
DW
132};
133
134/*
135 * Allocate and initialize an bui item with the given number of extents.
136 */
137struct xfs_bui_log_item *
138xfs_bui_init(
139 struct xfs_mount *mp)
140
141{
142 struct xfs_bui_log_item *buip;
143
707e0dda 144 buip = kmem_zone_zalloc(xfs_bui_zone, 0);
6413a014
DW
145
146 xfs_log_item_init(mp, &buip->bui_item, XFS_LI_BUI, &xfs_bui_item_ops);
147 buip->bui_format.bui_nextents = XFS_BUI_MAX_FAST_EXTENTS;
148 buip->bui_format.bui_id = (uintptr_t)(void *)buip;
149 atomic_set(&buip->bui_next_extent, 0);
150 atomic_set(&buip->bui_refcount, 2);
151
152 return buip;
153}
154
6413a014
DW
155static inline struct xfs_bud_log_item *BUD_ITEM(struct xfs_log_item *lip)
156{
157 return container_of(lip, struct xfs_bud_log_item, bud_item);
158}
159
160STATIC void
161xfs_bud_item_size(
162 struct xfs_log_item *lip,
163 int *nvecs,
164 int *nbytes)
165{
166 *nvecs += 1;
167 *nbytes += sizeof(struct xfs_bud_log_format);
168}
169
170/*
171 * This is called to fill in the vector of log iovecs for the
172 * given bud log item. We use only 1 iovec, and we point that
173 * at the bud_log_format structure embedded in the bud item.
174 * It is at this point that we assert that all of the extent
175 * slots in the bud item have been filled.
176 */
177STATIC void
178xfs_bud_item_format(
179 struct xfs_log_item *lip,
180 struct xfs_log_vec *lv)
181{
182 struct xfs_bud_log_item *budp = BUD_ITEM(lip);
183 struct xfs_log_iovec *vecp = NULL;
184
185 budp->bud_format.bud_type = XFS_LI_BUD;
186 budp->bud_format.bud_size = 1;
187
188 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_BUD_FORMAT, &budp->bud_format,
189 sizeof(struct xfs_bud_log_format));
190}
191
6413a014
DW
192/*
193 * The BUD is either committed or aborted if the transaction is cancelled. If
194 * the transaction is cancelled, drop our reference to the BUI and free the
195 * BUD.
196 */
197STATIC void
ddf92053 198xfs_bud_item_release(
6413a014
DW
199 struct xfs_log_item *lip)
200{
201 struct xfs_bud_log_item *budp = BUD_ITEM(lip);
202
ddf92053 203 xfs_bui_release(budp->bud_buip);
377bcd5f 204 kmem_cache_free(xfs_bud_zone, budp);
6413a014
DW
205}
206
6413a014 207static const struct xfs_item_ops xfs_bud_item_ops = {
9ce632a2 208 .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED,
6413a014
DW
209 .iop_size = xfs_bud_item_size,
210 .iop_format = xfs_bud_item_format,
ddf92053 211 .iop_release = xfs_bud_item_release,
6413a014
DW
212};
213
caeaea98 214static struct xfs_bud_log_item *
73f0d236
CH
215xfs_trans_get_bud(
216 struct xfs_trans *tp,
6413a014 217 struct xfs_bui_log_item *buip)
6413a014 218{
73f0d236 219 struct xfs_bud_log_item *budp;
6413a014 220
707e0dda 221 budp = kmem_zone_zalloc(xfs_bud_zone, 0);
73f0d236
CH
222 xfs_log_item_init(tp->t_mountp, &budp->bud_item, XFS_LI_BUD,
223 &xfs_bud_item_ops);
6413a014
DW
224 budp->bud_buip = buip;
225 budp->bud_format.bud_bui_id = buip->bui_format.bui_id;
226
73f0d236 227 xfs_trans_add_item(tp, &budp->bud_item);
6413a014
DW
228 return budp;
229}
77d61fe4 230
caeaea98
CH
231/*
232 * Finish an bmap update and log it to the BUD. Note that the
233 * transaction is marked dirty regardless of whether the bmap update
234 * succeeds or fails to support the BUI/BUD lifecycle rules.
235 */
236static int
237xfs_trans_log_finish_bmap_update(
238 struct xfs_trans *tp,
239 struct xfs_bud_log_item *budp,
240 enum xfs_bmap_intent_type type,
241 struct xfs_inode *ip,
242 int whichfork,
243 xfs_fileoff_t startoff,
244 xfs_fsblock_t startblock,
245 xfs_filblks_t *blockcount,
246 xfs_exntst_t state)
247{
248 int error;
249
250 error = xfs_bmap_finish_one(tp, ip, type, whichfork, startoff,
251 startblock, blockcount, state);
252
253 /*
254 * Mark the transaction dirty, even on error. This ensures the
255 * transaction is aborted, which:
256 *
257 * 1.) releases the BUI and frees the BUD
258 * 2.) shuts down the filesystem
259 */
260 tp->t_flags |= XFS_TRANS_DIRTY;
261 set_bit(XFS_LI_DIRTY, &budp->bud_item.li_flags);
262
263 return error;
264}
265
266/* Sort bmap intents by inode. */
267static int
268xfs_bmap_update_diff_items(
269 void *priv,
270 struct list_head *a,
271 struct list_head *b)
272{
273 struct xfs_bmap_intent *ba;
274 struct xfs_bmap_intent *bb;
275
276 ba = container_of(a, struct xfs_bmap_intent, bi_list);
277 bb = container_of(b, struct xfs_bmap_intent, bi_list);
278 return ba->bi_owner->i_ino - bb->bi_owner->i_ino;
279}
280
caeaea98
CH
281/* Set the map extent flags for this mapping. */
282static void
283xfs_trans_set_bmap_flags(
284 struct xfs_map_extent *bmap,
285 enum xfs_bmap_intent_type type,
286 int whichfork,
287 xfs_exntst_t state)
288{
289 bmap->me_flags = 0;
290 switch (type) {
291 case XFS_BMAP_MAP:
292 case XFS_BMAP_UNMAP:
293 bmap->me_flags = type;
294 break;
295 default:
296 ASSERT(0);
297 }
298 if (state == XFS_EXT_UNWRITTEN)
299 bmap->me_flags |= XFS_BMAP_EXTENT_UNWRITTEN;
300 if (whichfork == XFS_ATTR_FORK)
301 bmap->me_flags |= XFS_BMAP_EXTENT_ATTR_FORK;
302}
303
304/* Log bmap updates in the intent item. */
305STATIC void
306xfs_bmap_update_log_item(
307 struct xfs_trans *tp,
c1f09188
CH
308 struct xfs_bui_log_item *buip,
309 struct xfs_bmap_intent *bmap)
caeaea98 310{
caeaea98
CH
311 uint next_extent;
312 struct xfs_map_extent *map;
313
caeaea98
CH
314 tp->t_flags |= XFS_TRANS_DIRTY;
315 set_bit(XFS_LI_DIRTY, &buip->bui_item.li_flags);
316
317 /*
318 * atomic_inc_return gives us the value after the increment;
319 * we want to use it as an array index so we need to subtract 1 from
320 * it.
321 */
322 next_extent = atomic_inc_return(&buip->bui_next_extent) - 1;
323 ASSERT(next_extent < buip->bui_format.bui_nextents);
324 map = &buip->bui_format.bui_extents[next_extent];
325 map->me_owner = bmap->bi_owner->i_ino;
326 map->me_startblock = bmap->bi_bmap.br_startblock;
327 map->me_startoff = bmap->bi_bmap.br_startoff;
328 map->me_len = bmap->bi_bmap.br_blockcount;
329 xfs_trans_set_bmap_flags(map, bmap->bi_type, bmap->bi_whichfork,
330 bmap->bi_bmap.br_state);
331}
332
13a83333 333static struct xfs_log_item *
c1f09188
CH
334xfs_bmap_update_create_intent(
335 struct xfs_trans *tp,
336 struct list_head *items,
d367a868
CH
337 unsigned int count,
338 bool sort)
c1f09188 339{
d367a868
CH
340 struct xfs_mount *mp = tp->t_mountp;
341 struct xfs_bui_log_item *buip = xfs_bui_init(mp);
c1f09188
CH
342 struct xfs_bmap_intent *bmap;
343
344 ASSERT(count == XFS_BUI_MAX_FAST_EXTENTS);
345
346 xfs_trans_add_item(tp, &buip->bui_item);
d367a868
CH
347 if (sort)
348 list_sort(mp, items, xfs_bmap_update_diff_items);
c1f09188
CH
349 list_for_each_entry(bmap, items, bi_list)
350 xfs_bmap_update_log_item(tp, buip, bmap);
13a83333 351 return &buip->bui_item;
c1f09188
CH
352}
353
caeaea98
CH
354/* Get an BUD so we can process all the deferred rmap updates. */
355STATIC void *
356xfs_bmap_update_create_done(
357 struct xfs_trans *tp,
13a83333 358 struct xfs_log_item *intent,
caeaea98
CH
359 unsigned int count)
360{
13a83333 361 return xfs_trans_get_bud(tp, BUI_ITEM(intent));
caeaea98
CH
362}
363
364/* Process a deferred rmap update. */
365STATIC int
366xfs_bmap_update_finish_item(
367 struct xfs_trans *tp,
368 struct list_head *item,
369 void *done_item,
370 void **state)
371{
372 struct xfs_bmap_intent *bmap;
373 xfs_filblks_t count;
374 int error;
375
376 bmap = container_of(item, struct xfs_bmap_intent, bi_list);
377 count = bmap->bi_bmap.br_blockcount;
378 error = xfs_trans_log_finish_bmap_update(tp, done_item,
379 bmap->bi_type,
380 bmap->bi_owner, bmap->bi_whichfork,
381 bmap->bi_bmap.br_startoff,
382 bmap->bi_bmap.br_startblock,
383 &count,
384 bmap->bi_bmap.br_state);
385 if (!error && count > 0) {
386 ASSERT(bmap->bi_type == XFS_BMAP_UNMAP);
387 bmap->bi_bmap.br_blockcount = count;
388 return -EAGAIN;
389 }
390 kmem_free(bmap);
391 return error;
392}
393
394/* Abort all pending BUIs. */
395STATIC void
396xfs_bmap_update_abort_intent(
13a83333 397 struct xfs_log_item *intent)
caeaea98 398{
13a83333 399 xfs_bui_release(BUI_ITEM(intent));
caeaea98
CH
400}
401
402/* Cancel a deferred rmap update. */
403STATIC void
404xfs_bmap_update_cancel_item(
405 struct list_head *item)
406{
407 struct xfs_bmap_intent *bmap;
408
409 bmap = container_of(item, struct xfs_bmap_intent, bi_list);
410 kmem_free(bmap);
411}
412
413const struct xfs_defer_op_type xfs_bmap_update_defer_type = {
414 .max_items = XFS_BUI_MAX_FAST_EXTENTS,
caeaea98
CH
415 .create_intent = xfs_bmap_update_create_intent,
416 .abort_intent = xfs_bmap_update_abort_intent,
caeaea98
CH
417 .create_done = xfs_bmap_update_create_done,
418 .finish_item = xfs_bmap_update_finish_item,
419 .cancel_item = xfs_bmap_update_cancel_item,
420};
421
77d61fe4
DW
422/*
423 * Process a bmap update intent item that was recovered from the log.
424 * We need to update some inode's bmbt.
425 */
426int
427xfs_bui_recover(
fbfa977d
BF
428 struct xfs_trans *parent_tp,
429 struct xfs_bui_log_item *buip)
77d61fe4
DW
430{
431 int error = 0;
9f3afb57 432 unsigned int bui_type;
77d61fe4
DW
433 struct xfs_map_extent *bmap;
434 xfs_fsblock_t startblock_fsb;
435 xfs_fsblock_t inode_fsb;
e1a4e37c 436 xfs_filblks_t count;
77d61fe4 437 bool op_ok;
9f3afb57
DW
438 struct xfs_bud_log_item *budp;
439 enum xfs_bmap_intent_type type;
440 int whichfork;
441 xfs_exntst_t state;
442 struct xfs_trans *tp;
443 struct xfs_inode *ip = NULL;
e1a4e37c 444 struct xfs_bmbt_irec irec;
fbfa977d 445 struct xfs_mount *mp = parent_tp->t_mountp;
77d61fe4
DW
446
447 ASSERT(!test_bit(XFS_BUI_RECOVERED, &buip->bui_flags));
448
449 /* Only one mapping operation per BUI... */
450 if (buip->bui_format.bui_nextents != XFS_BUI_MAX_FAST_EXTENTS) {
451 set_bit(XFS_BUI_RECOVERED, &buip->bui_flags);
452 xfs_bui_release(buip);
895e196f 453 return -EFSCORRUPTED;
77d61fe4
DW
454 }
455
456 /*
457 * First check the validity of the extent described by the
458 * BUI. If anything is bad, then toss the BUI.
459 */
460 bmap = &buip->bui_format.bui_extents[0];
461 startblock_fsb = XFS_BB_TO_FSB(mp,
462 XFS_FSB_TO_DADDR(mp, bmap->me_startblock));
463 inode_fsb = XFS_BB_TO_FSB(mp, XFS_FSB_TO_DADDR(mp,
464 XFS_INO_TO_FSB(mp, bmap->me_owner)));
465 switch (bmap->me_flags & XFS_BMAP_EXTENT_TYPE_MASK) {
466 case XFS_BMAP_MAP:
467 case XFS_BMAP_UNMAP:
468 op_ok = true;
469 break;
470 default:
471 op_ok = false;
472 break;
473 }
474 if (!op_ok || startblock_fsb == 0 ||
475 bmap->me_len == 0 ||
476 inode_fsb == 0 ||
477 startblock_fsb >= mp->m_sb.sb_dblocks ||
478 bmap->me_len >= mp->m_sb.sb_agblocks ||
479 inode_fsb >= mp->m_sb.sb_dblocks ||
480 (bmap->me_flags & ~XFS_BMAP_EXTENT_FLAGS)) {
481 /*
482 * This will pull the BUI from the AIL and
483 * free the memory associated with it.
484 */
485 set_bit(XFS_BUI_RECOVERED, &buip->bui_flags);
486 xfs_bui_release(buip);
895e196f 487 return -EFSCORRUPTED;
77d61fe4
DW
488 }
489
fe0be23e
DW
490 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate,
491 XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK), 0, 0, &tp);
9f3afb57
DW
492 if (error)
493 return error;
91ef75b6
BF
494 /*
495 * Recovery stashes all deferred ops during intent processing and
496 * finishes them on completion. Transfer current dfops state to this
497 * transaction and transfer the result back before we return.
498 */
ce356d64 499 xfs_defer_move(tp, parent_tp);
9f3afb57
DW
500 budp = xfs_trans_get_bud(tp, buip);
501
502 /* Grab the inode. */
503 error = xfs_iget(mp, tp, bmap->me_owner, 0, XFS_ILOCK_EXCL, &ip);
504 if (error)
505 goto err_inode;
506
17c12bcd
DW
507 if (VFS_I(ip)->i_nlink == 0)
508 xfs_iflags_set(ip, XFS_IRECOVERY);
9f3afb57
DW
509
510 /* Process deferred bmap item. */
511 state = (bmap->me_flags & XFS_BMAP_EXTENT_UNWRITTEN) ?
512 XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
513 whichfork = (bmap->me_flags & XFS_BMAP_EXTENT_ATTR_FORK) ?
514 XFS_ATTR_FORK : XFS_DATA_FORK;
515 bui_type = bmap->me_flags & XFS_BMAP_EXTENT_TYPE_MASK;
516 switch (bui_type) {
517 case XFS_BMAP_MAP:
518 case XFS_BMAP_UNMAP:
519 type = bui_type;
520 break;
521 default:
a5155b87 522 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
9f3afb57 523 error = -EFSCORRUPTED;
50995582 524 goto err_inode;
9f3afb57
DW
525 }
526 xfs_trans_ijoin(tp, ip, 0);
527
e1a4e37c 528 count = bmap->me_len;
7dbddbac
BF
529 error = xfs_trans_log_finish_bmap_update(tp, budp, type, ip, whichfork,
530 bmap->me_startoff, bmap->me_startblock, &count, state);
9f3afb57 531 if (error)
50995582 532 goto err_inode;
9f3afb57 533
e1a4e37c
DW
534 if (count > 0) {
535 ASSERT(type == XFS_BMAP_UNMAP);
536 irec.br_startblock = bmap->me_startblock;
537 irec.br_blockcount = count;
538 irec.br_startoff = bmap->me_startoff;
539 irec.br_state = state;
3e08f42a 540 xfs_bmap_unmap_extent(tp, ip, &irec);
e1a4e37c
DW
541 }
542
77d61fe4 543 set_bit(XFS_BUI_RECOVERED, &buip->bui_flags);
ce356d64 544 xfs_defer_move(parent_tp, tp);
9f3afb57
DW
545 error = xfs_trans_commit(tp);
546 xfs_iunlock(ip, XFS_ILOCK_EXCL);
44a8736b 547 xfs_irele(ip);
9f3afb57
DW
548
549 return error;
550
9f3afb57 551err_inode:
ce356d64 552 xfs_defer_move(parent_tp, tp);
9f3afb57
DW
553 xfs_trans_cancel(tp);
554 if (ip) {
555 xfs_iunlock(ip, XFS_ILOCK_EXCL);
44a8736b 556 xfs_irele(ip);
9f3afb57 557 }
77d61fe4
DW
558 return error;
559}
This page took 0.352706 seconds and 4 git commands to generate.