1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
8 #include "xfs_format.h"
9 #include "xfs_log_format.h"
10 #include "xfs_trans_resv.h"
12 #include "xfs_shared.h"
13 #include "xfs_mount.h"
14 #include "xfs_defer.h"
15 #include "xfs_inode.h"
16 #include "xfs_trans.h"
17 #include "xfs_trans_priv.h"
18 #include "xfs_bmap_item.h"
21 #include "xfs_icache.h"
22 #include "xfs_bmap_btree.h"
23 #include "xfs_trans_space.h"
24 #include "xfs_error.h"
25 #include "xfs_quota.h"
27 kmem_zone_t *xfs_bui_zone;
28 kmem_zone_t *xfs_bud_zone;
30 static inline struct xfs_bui_log_item *BUI_ITEM(struct xfs_log_item *lip)
32 return container_of(lip, struct xfs_bui_log_item, bui_item);
37 struct xfs_bui_log_item *buip)
39 kmem_zone_free(xfs_bui_zone, buip);
43 * Freeing the BUI requires that we remove it from the AIL if it has already
44 * been placed there. However, the BUI may not yet have been placed in the AIL
45 * when called by xfs_bui_release() from BUD processing due to the ordering of
46 * committed vs unpin operations in bulk insert operations. Hence the reference
47 * count to ensure only the last caller frees the BUI.
51 struct xfs_bui_log_item *buip)
53 ASSERT(atomic_read(&buip->bui_refcount) > 0);
54 if (atomic_dec_and_test(&buip->bui_refcount)) {
55 xfs_trans_ail_remove(&buip->bui_item, SHUTDOWN_LOG_IO_ERROR);
56 xfs_bui_item_free(buip);
63 struct xfs_log_item *lip,
67 struct xfs_bui_log_item *buip = BUI_ITEM(lip);
70 *nbytes += xfs_bui_log_format_sizeof(buip->bui_format.bui_nextents);
74 * This is called to fill in the vector of log iovecs for the
75 * given bui log item. We use only 1 iovec, and we point that
76 * at the bui_log_format structure embedded in the bui item.
77 * It is at this point that we assert that all of the extent
78 * slots in the bui item have been filled.
82 struct xfs_log_item *lip,
83 struct xfs_log_vec *lv)
85 struct xfs_bui_log_item *buip = BUI_ITEM(lip);
86 struct xfs_log_iovec *vecp = NULL;
88 ASSERT(atomic_read(&buip->bui_next_extent) ==
89 buip->bui_format.bui_nextents);
91 buip->bui_format.bui_type = XFS_LI_BUI;
92 buip->bui_format.bui_size = 1;
94 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_BUI_FORMAT, &buip->bui_format,
95 xfs_bui_log_format_sizeof(buip->bui_format.bui_nextents));
99 * The unpin operation is the last place an BUI is manipulated in the log. It is
100 * either inserted in the AIL or aborted in the event of a log I/O error. In
101 * either case, the BUI transaction has been successfully committed to make it
102 * this far. Therefore, we expect whoever committed the BUI to either construct
103 * and commit the BUD or drop the BUD's reference in the event of error. Simply
104 * drop the log's BUI reference now that the log is done with it.
108 struct xfs_log_item *lip,
111 struct xfs_bui_log_item *buip = BUI_ITEM(lip);
113 xfs_bui_release(buip);
117 * The BUI has been either committed or aborted if the transaction has been
118 * cancelled. If the transaction was cancelled, an BUD isn't going to be
119 * constructed and thus we free the BUI here directly.
122 xfs_bui_item_release(
123 struct xfs_log_item *lip)
125 xfs_bui_release(BUI_ITEM(lip));
128 static inline struct xfs_bud_log_item *BUD_ITEM(struct xfs_log_item *lip)
130 return container_of(lip, struct xfs_bud_log_item, bud_item);
135 struct xfs_log_item *lip,
140 *nbytes += sizeof(struct xfs_bud_log_format);
144 * This is called to fill in the vector of log iovecs for the
145 * given bud log item. We use only 1 iovec, and we point that
146 * at the bud_log_format structure embedded in the bud item.
147 * It is at this point that we assert that all of the extent
148 * slots in the bud item have been filled.
152 struct xfs_log_item *lip,
153 struct xfs_log_vec *lv)
155 struct xfs_bud_log_item *budp = BUD_ITEM(lip);
156 struct xfs_log_iovec *vecp = NULL;
158 budp->bud_format.bud_type = XFS_LI_BUD;
159 budp->bud_format.bud_size = 1;
161 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_BUD_FORMAT, &budp->bud_format,
162 sizeof(struct xfs_bud_log_format));
166 * The BUD is either committed or aborted if the transaction is cancelled. If
167 * the transaction is cancelled, drop our reference to the BUI and free the
171 xfs_bud_item_release(
172 struct xfs_log_item *lip)
174 struct xfs_bud_log_item *budp = BUD_ITEM(lip);
176 xfs_bui_release(budp->bud_buip);
177 kmem_zone_free(xfs_bud_zone, budp);
180 static const struct xfs_item_ops xfs_bud_item_ops = {
181 .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED,
182 .iop_size = xfs_bud_item_size,
183 .iop_format = xfs_bud_item_format,
184 .iop_release = xfs_bud_item_release,
187 static struct xfs_bud_log_item *
189 struct xfs_trans *tp,
190 struct xfs_bui_log_item *buip)
192 struct xfs_bud_log_item *budp;
194 budp = kmem_zone_zalloc(xfs_bud_zone, 0);
195 xfs_log_item_init(tp->t_mountp, &budp->bud_item, XFS_LI_BUD,
197 budp->bud_buip = buip;
198 budp->bud_format.bud_bui_id = buip->bui_format.bui_id;
200 xfs_trans_add_item(tp, &budp->bud_item);
205 * Finish an bmap update and log it to the BUD. Note that the
206 * transaction is marked dirty regardless of whether the bmap update
207 * succeeds or fails to support the BUI/BUD lifecycle rules.
210 xfs_trans_log_finish_bmap_update(
211 struct xfs_trans *tp,
212 struct xfs_bud_log_item *budp,
213 enum xfs_bmap_intent_type type,
214 struct xfs_inode *ip,
216 xfs_fileoff_t startoff,
217 xfs_fsblock_t startblock,
218 xfs_filblks_t *blockcount,
223 error = xfs_bmap_finish_one(tp, ip, type, whichfork, startoff,
224 startblock, blockcount, state);
227 * Mark the transaction dirty, even on error. This ensures the
228 * transaction is aborted, which:
230 * 1.) releases the BUI and frees the BUD
231 * 2.) shuts down the filesystem
233 tp->t_flags |= XFS_TRANS_DIRTY;
234 set_bit(XFS_LI_DIRTY, &budp->bud_item.li_flags);
239 /* Sort bmap intents by inode. */
241 xfs_bmap_update_diff_items(
246 struct xfs_bmap_intent *ba;
247 struct xfs_bmap_intent *bb;
249 ba = container_of(a, struct xfs_bmap_intent, bi_list);
250 bb = container_of(b, struct xfs_bmap_intent, bi_list);
251 return ba->bi_owner->i_ino - bb->bi_owner->i_ino;
254 /* Set the map extent flags for this mapping. */
256 xfs_trans_set_bmap_flags(
257 struct xfs_map_extent *bmap,
258 enum xfs_bmap_intent_type type,
266 bmap->me_flags = type;
271 if (state == XFS_EXT_UNWRITTEN)
272 bmap->me_flags |= XFS_BMAP_EXTENT_UNWRITTEN;
273 if (whichfork == XFS_ATTR_FORK)
274 bmap->me_flags |= XFS_BMAP_EXTENT_ATTR_FORK;
277 /* Log bmap updates in the intent item. */
279 xfs_bmap_update_log_item(
280 struct xfs_trans *tp,
281 struct xfs_bui_log_item *buip,
282 struct xfs_bmap_intent *bmap)
285 struct xfs_map_extent *map;
287 tp->t_flags |= XFS_TRANS_DIRTY;
288 set_bit(XFS_LI_DIRTY, &buip->bui_item.li_flags);
291 * atomic_inc_return gives us the value after the increment;
292 * we want to use it as an array index so we need to subtract 1 from
295 next_extent = atomic_inc_return(&buip->bui_next_extent) - 1;
296 ASSERT(next_extent < buip->bui_format.bui_nextents);
297 map = &buip->bui_format.bui_extents[next_extent];
298 map->me_owner = bmap->bi_owner->i_ino;
299 map->me_startblock = bmap->bi_bmap.br_startblock;
300 map->me_startoff = bmap->bi_bmap.br_startoff;
301 map->me_len = bmap->bi_bmap.br_blockcount;
302 xfs_trans_set_bmap_flags(map, bmap->bi_type, bmap->bi_whichfork,
303 bmap->bi_bmap.br_state);
306 static struct xfs_log_item *
307 xfs_bmap_update_create_intent(
308 struct xfs_trans *tp,
309 struct list_head *items,
313 struct xfs_mount *mp = tp->t_mountp;
314 struct xfs_bui_log_item *buip = xfs_bui_init(mp);
315 struct xfs_bmap_intent *bmap;
317 ASSERT(count == XFS_BUI_MAX_FAST_EXTENTS);
319 xfs_trans_add_item(tp, &buip->bui_item);
321 list_sort(mp, items, xfs_bmap_update_diff_items);
322 list_for_each_entry(bmap, items, bi_list)
323 xfs_bmap_update_log_item(tp, buip, bmap);
324 return &buip->bui_item;
327 /* Get an BUD so we can process all the deferred rmap updates. */
329 xfs_bmap_update_create_done(
330 struct xfs_trans *tp,
331 struct xfs_log_item *intent,
334 return xfs_trans_get_bud(tp, BUI_ITEM(intent));
337 /* Process a deferred rmap update. */
339 xfs_bmap_update_finish_item(
340 struct xfs_trans *tp,
341 struct list_head *item,
345 struct xfs_bmap_intent *bmap;
349 bmap = container_of(item, struct xfs_bmap_intent, bi_list);
350 count = bmap->bi_bmap.br_blockcount;
351 error = xfs_trans_log_finish_bmap_update(tp, done_item,
353 bmap->bi_owner, bmap->bi_whichfork,
354 bmap->bi_bmap.br_startoff,
355 bmap->bi_bmap.br_startblock,
357 bmap->bi_bmap.br_state);
358 if (!error && count > 0) {
359 ASSERT(bmap->bi_type == XFS_BMAP_UNMAP);
360 bmap->bi_bmap.br_blockcount = count;
367 /* Abort all pending BUIs. */
369 xfs_bmap_update_abort_intent(
370 struct xfs_log_item *intent)
372 xfs_bui_release(BUI_ITEM(intent));
375 /* Cancel a deferred rmap update. */
377 xfs_bmap_update_cancel_item(
378 struct list_head *item)
380 struct xfs_bmap_intent *bmap;
382 bmap = container_of(item, struct xfs_bmap_intent, bi_list);
386 const struct xfs_defer_op_type xfs_bmap_update_defer_type = {
387 .max_items = XFS_BUI_MAX_FAST_EXTENTS,
388 .create_intent = xfs_bmap_update_create_intent,
389 .abort_intent = xfs_bmap_update_abort_intent,
390 .create_done = xfs_bmap_update_create_done,
391 .finish_item = xfs_bmap_update_finish_item,
392 .cancel_item = xfs_bmap_update_cancel_item,
396 * Process a bmap update intent item that was recovered from the log.
397 * We need to update some inode's bmbt.
401 struct xfs_bui_log_item *buip,
402 struct list_head *capture_list)
405 unsigned int bui_type;
406 struct xfs_map_extent *bmap;
407 xfs_fsblock_t startblock_fsb;
408 xfs_fsblock_t inode_fsb;
410 struct xfs_bud_log_item *budp;
413 struct xfs_trans *tp;
414 struct xfs_inode *ip = NULL;
415 struct xfs_bmbt_irec irec;
416 struct xfs_mount *mp = buip->bui_item.li_mountp;
418 ASSERT(!test_bit(XFS_BUI_RECOVERED, &buip->bui_flags));
420 /* Only one mapping operation per BUI... */
421 if (buip->bui_format.bui_nextents != XFS_BUI_MAX_FAST_EXTENTS) {
422 set_bit(XFS_BUI_RECOVERED, &buip->bui_flags);
423 xfs_bui_release(buip);
424 return -EFSCORRUPTED;
428 * First check the validity of the extent described by the
429 * BUI. If anything is bad, then toss the BUI.
431 bmap = &buip->bui_format.bui_extents[0];
432 startblock_fsb = XFS_BB_TO_FSB(mp,
433 XFS_FSB_TO_DADDR(mp, bmap->me_startblock));
434 inode_fsb = XFS_BB_TO_FSB(mp, XFS_FSB_TO_DADDR(mp,
435 XFS_INO_TO_FSB(mp, bmap->me_owner)));
436 state = (bmap->me_flags & XFS_BMAP_EXTENT_UNWRITTEN) ?
437 XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
438 whichfork = (bmap->me_flags & XFS_BMAP_EXTENT_ATTR_FORK) ?
439 XFS_ATTR_FORK : XFS_DATA_FORK;
440 bui_type = bmap->me_flags & XFS_BMAP_EXTENT_TYPE_MASK;
446 return -EFSCORRUPTED;
448 if (startblock_fsb == 0 ||
451 startblock_fsb >= mp->m_sb.sb_dblocks ||
452 bmap->me_len >= mp->m_sb.sb_agblocks ||
453 inode_fsb >= mp->m_sb.sb_dblocks ||
454 (bmap->me_flags & ~XFS_BMAP_EXTENT_FLAGS)) {
456 * This will pull the BUI from the AIL and
457 * free the memory associated with it.
459 set_bit(XFS_BUI_RECOVERED, &buip->bui_flags);
460 xfs_bui_release(buip);
461 return -EFSCORRUPTED;
464 /* Grab the inode. */
465 error = xfs_iget(mp, NULL, bmap->me_owner, 0, 0, &ip);
469 error = xfs_qm_dqattach(ip);
473 if (VFS_I(ip)->i_nlink == 0)
474 xfs_iflags_set(ip, XFS_IRECOVERY);
476 /* Allocate transaction and do the work. */
477 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate,
478 XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK), 0, 0, &tp);
482 budp = xfs_trans_get_bud(tp, buip);
483 xfs_ilock(ip, XFS_ILOCK_EXCL);
484 xfs_trans_ijoin(tp, ip, 0);
486 count = bmap->me_len;
487 error = xfs_trans_log_finish_bmap_update(tp, budp, bui_type, ip,
488 whichfork, bmap->me_startoff, bmap->me_startblock,
494 ASSERT(bui_type == XFS_BMAP_UNMAP);
495 irec.br_startblock = bmap->me_startblock;
496 irec.br_blockcount = count;
497 irec.br_startoff = bmap->me_startoff;
498 irec.br_state = state;
499 xfs_bmap_unmap_extent(tp, ip, &irec);
502 set_bit(XFS_BUI_RECOVERED, &buip->bui_flags);
504 * Commit transaction, which frees the transaction and saves the inode
505 * for later replay activities.
507 error = xfs_defer_ops_capture_and_commit(tp, ip, capture_list);
511 xfs_iunlock(ip, XFS_ILOCK_EXCL);
516 xfs_trans_cancel(tp);
518 xfs_iunlock(ip, XFS_ILOCK_EXCL);
524 /* Relog an intent item to push the log tail forward. */
525 static struct xfs_log_item *
527 struct xfs_log_item *intent,
528 struct xfs_trans *tp)
530 struct xfs_bud_log_item *budp;
531 struct xfs_bui_log_item *buip;
532 struct xfs_map_extent *extp;
535 count = BUI_ITEM(intent)->bui_format.bui_nextents;
536 extp = BUI_ITEM(intent)->bui_format.bui_extents;
538 tp->t_flags |= XFS_TRANS_DIRTY;
539 budp = xfs_trans_get_bud(tp, BUI_ITEM(intent));
540 set_bit(XFS_LI_DIRTY, &budp->bud_item.li_flags);
542 buip = xfs_bui_init(tp->t_mountp);
543 memcpy(buip->bui_format.bui_extents, extp, count * sizeof(*extp));
544 atomic_set(&buip->bui_next_extent, count);
545 xfs_trans_add_item(tp, &buip->bui_item);
546 set_bit(XFS_LI_DIRTY, &buip->bui_item.li_flags);
547 return &buip->bui_item;
550 static const struct xfs_item_ops xfs_bui_item_ops = {
551 .iop_size = xfs_bui_item_size,
552 .iop_format = xfs_bui_item_format,
553 .iop_unpin = xfs_bui_item_unpin,
554 .iop_release = xfs_bui_item_release,
555 .iop_relog = xfs_bui_item_relog,
559 * Allocate and initialize an bui item with the given number of extents.
561 struct xfs_bui_log_item *
563 struct xfs_mount *mp)
566 struct xfs_bui_log_item *buip;
568 buip = kmem_zone_zalloc(xfs_bui_zone, 0);
570 xfs_log_item_init(mp, &buip->bui_item, XFS_LI_BUI, &xfs_bui_item_ops);
571 buip->bui_format.bui_nextents = XFS_BUI_MAX_FAST_EXTENTS;
572 buip->bui_format.bui_id = (uintptr_t)(void *)buip;
573 atomic_set(&buip->bui_next_extent, 0);
574 atomic_set(&buip->bui_refcount, 2);