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_log_priv.h"
26 #include "xfs_log_recover.h"
28 #include "xfs_trace.h"
30 struct kmem_cache *xfs_bui_cache;
31 struct kmem_cache *xfs_bud_cache;
33 static const struct xfs_item_ops xfs_bui_item_ops;
35 static inline struct xfs_bui_log_item *BUI_ITEM(struct xfs_log_item *lip)
37 return container_of(lip, struct xfs_bui_log_item, bui_item);
42 struct xfs_bui_log_item *buip)
44 kvfree(buip->bui_item.li_lv_shadow);
45 kmem_cache_free(xfs_bui_cache, buip);
49 * Freeing the BUI requires that we remove it from the AIL if it has already
50 * been placed there. However, the BUI may not yet have been placed in the AIL
51 * when called by xfs_bui_release() from BUD processing due to the ordering of
52 * committed vs unpin operations in bulk insert operations. Hence the reference
53 * count to ensure only the last caller frees the BUI.
57 struct xfs_bui_log_item *buip)
59 ASSERT(atomic_read(&buip->bui_refcount) > 0);
60 if (!atomic_dec_and_test(&buip->bui_refcount))
63 xfs_trans_ail_delete(&buip->bui_item, 0);
64 xfs_bui_item_free(buip);
70 struct xfs_log_item *lip,
74 struct xfs_bui_log_item *buip = BUI_ITEM(lip);
77 *nbytes += xfs_bui_log_format_sizeof(buip->bui_format.bui_nextents);
81 * This is called to fill in the vector of log iovecs for the
82 * given bui log item. We use only 1 iovec, and we point that
83 * at the bui_log_format structure embedded in the bui item.
84 * It is at this point that we assert that all of the extent
85 * slots in the bui item have been filled.
89 struct xfs_log_item *lip,
90 struct xfs_log_vec *lv)
92 struct xfs_bui_log_item *buip = BUI_ITEM(lip);
93 struct xfs_log_iovec *vecp = NULL;
95 ASSERT(atomic_read(&buip->bui_next_extent) ==
96 buip->bui_format.bui_nextents);
98 buip->bui_format.bui_type = XFS_LI_BUI;
99 buip->bui_format.bui_size = 1;
101 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_BUI_FORMAT, &buip->bui_format,
102 xfs_bui_log_format_sizeof(buip->bui_format.bui_nextents));
106 * The unpin operation is the last place an BUI is manipulated in the log. It is
107 * either inserted in the AIL or aborted in the event of a log I/O error. In
108 * either case, the BUI transaction has been successfully committed to make it
109 * this far. Therefore, we expect whoever committed the BUI to either construct
110 * and commit the BUD or drop the BUD's reference in the event of error. Simply
111 * drop the log's BUI reference now that the log is done with it.
115 struct xfs_log_item *lip,
118 struct xfs_bui_log_item *buip = BUI_ITEM(lip);
120 xfs_bui_release(buip);
124 * The BUI has been either committed or aborted if the transaction has been
125 * cancelled. If the transaction was cancelled, an BUD isn't going to be
126 * constructed and thus we free the BUI here directly.
129 xfs_bui_item_release(
130 struct xfs_log_item *lip)
132 xfs_bui_release(BUI_ITEM(lip));
136 * Allocate and initialize an bui item with the given number of extents.
138 STATIC struct xfs_bui_log_item *
140 struct xfs_mount *mp)
143 struct xfs_bui_log_item *buip;
145 buip = kmem_cache_zalloc(xfs_bui_cache, GFP_KERNEL | __GFP_NOFAIL);
147 xfs_log_item_init(mp, &buip->bui_item, XFS_LI_BUI, &xfs_bui_item_ops);
148 buip->bui_format.bui_nextents = XFS_BUI_MAX_FAST_EXTENTS;
149 buip->bui_format.bui_id = (uintptr_t)(void *)buip;
150 atomic_set(&buip->bui_next_extent, 0);
151 atomic_set(&buip->bui_refcount, 2);
156 static inline struct xfs_bud_log_item *BUD_ITEM(struct xfs_log_item *lip)
158 return container_of(lip, struct xfs_bud_log_item, bud_item);
163 struct xfs_log_item *lip,
168 *nbytes += sizeof(struct xfs_bud_log_format);
172 * This is called to fill in the vector of log iovecs for the
173 * given bud log item. We use only 1 iovec, and we point that
174 * at the bud_log_format structure embedded in the bud item.
175 * It is at this point that we assert that all of the extent
176 * slots in the bud item have been filled.
180 struct xfs_log_item *lip,
181 struct xfs_log_vec *lv)
183 struct xfs_bud_log_item *budp = BUD_ITEM(lip);
184 struct xfs_log_iovec *vecp = NULL;
186 budp->bud_format.bud_type = XFS_LI_BUD;
187 budp->bud_format.bud_size = 1;
189 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_BUD_FORMAT, &budp->bud_format,
190 sizeof(struct xfs_bud_log_format));
194 * The BUD is either committed or aborted if the transaction is cancelled. If
195 * the transaction is cancelled, drop our reference to the BUI and free the
199 xfs_bud_item_release(
200 struct xfs_log_item *lip)
202 struct xfs_bud_log_item *budp = BUD_ITEM(lip);
204 xfs_bui_release(budp->bud_buip);
205 kvfree(budp->bud_item.li_lv_shadow);
206 kmem_cache_free(xfs_bud_cache, budp);
209 static struct xfs_log_item *
211 struct xfs_log_item *lip)
213 return &BUD_ITEM(lip)->bud_buip->bui_item;
216 static const struct xfs_item_ops xfs_bud_item_ops = {
217 .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED |
218 XFS_ITEM_INTENT_DONE,
219 .iop_size = xfs_bud_item_size,
220 .iop_format = xfs_bud_item_format,
221 .iop_release = xfs_bud_item_release,
222 .iop_intent = xfs_bud_item_intent,
225 static inline struct xfs_bmap_intent *bi_entry(const struct list_head *e)
227 return list_entry(e, struct xfs_bmap_intent, bi_list);
230 /* Sort bmap intents by inode. */
232 xfs_bmap_update_diff_items(
234 const struct list_head *a,
235 const struct list_head *b)
237 struct xfs_bmap_intent *ba = bi_entry(a);
238 struct xfs_bmap_intent *bb = bi_entry(b);
240 return ba->bi_owner->i_ino - bb->bi_owner->i_ino;
243 /* Log bmap updates in the intent item. */
245 xfs_bmap_update_log_item(
246 struct xfs_trans *tp,
247 struct xfs_bui_log_item *buip,
248 struct xfs_bmap_intent *bi)
251 struct xfs_map_extent *map;
254 * atomic_inc_return gives us the value after the increment;
255 * we want to use it as an array index so we need to subtract 1 from
258 next_extent = atomic_inc_return(&buip->bui_next_extent) - 1;
259 ASSERT(next_extent < buip->bui_format.bui_nextents);
260 map = &buip->bui_format.bui_extents[next_extent];
261 map->me_owner = bi->bi_owner->i_ino;
262 map->me_startblock = bi->bi_bmap.br_startblock;
263 map->me_startoff = bi->bi_bmap.br_startoff;
264 map->me_len = bi->bi_bmap.br_blockcount;
266 switch (bi->bi_type) {
269 map->me_flags = bi->bi_type;
274 if (bi->bi_bmap.br_state == XFS_EXT_UNWRITTEN)
275 map->me_flags |= XFS_BMAP_EXTENT_UNWRITTEN;
276 if (bi->bi_whichfork == XFS_ATTR_FORK)
277 map->me_flags |= XFS_BMAP_EXTENT_ATTR_FORK;
278 if (xfs_ifork_is_realtime(bi->bi_owner, bi->bi_whichfork))
279 map->me_flags |= XFS_BMAP_EXTENT_REALTIME;
282 static struct xfs_log_item *
283 xfs_bmap_update_create_intent(
284 struct xfs_trans *tp,
285 struct list_head *items,
289 struct xfs_mount *mp = tp->t_mountp;
290 struct xfs_bui_log_item *buip = xfs_bui_init(mp);
291 struct xfs_bmap_intent *bi;
293 ASSERT(count == XFS_BUI_MAX_FAST_EXTENTS);
296 list_sort(mp, items, xfs_bmap_update_diff_items);
297 list_for_each_entry(bi, items, bi_list)
298 xfs_bmap_update_log_item(tp, buip, bi);
299 return &buip->bui_item;
302 /* Get an BUD so we can process all the deferred bmap updates. */
303 static struct xfs_log_item *
304 xfs_bmap_update_create_done(
305 struct xfs_trans *tp,
306 struct xfs_log_item *intent,
309 struct xfs_bui_log_item *buip = BUI_ITEM(intent);
310 struct xfs_bud_log_item *budp;
312 budp = kmem_cache_zalloc(xfs_bud_cache, GFP_KERNEL | __GFP_NOFAIL);
313 xfs_log_item_init(tp->t_mountp, &budp->bud_item, XFS_LI_BUD,
315 budp->bud_buip = buip;
316 budp->bud_format.bud_bui_id = buip->bui_format.bui_id;
318 return &budp->bud_item;
321 /* Take a passive ref to the AG containing the space we're mapping. */
323 xfs_bmap_update_get_group(
324 struct xfs_mount *mp,
325 struct xfs_bmap_intent *bi)
329 if (xfs_ifork_is_realtime(bi->bi_owner, bi->bi_whichfork))
332 agno = XFS_FSB_TO_AGNO(mp, bi->bi_bmap.br_startblock);
335 * Bump the intent count on behalf of the deferred rmap and refcount
336 * intent items that that we can queue when we finish this bmap work.
337 * This new intent item will bump the intent count before the bmap
338 * intent drops the intent count, ensuring that the intent count
339 * remains nonzero across the transaction roll.
341 bi->bi_pag = xfs_perag_intent_get(mp, agno);
344 /* Add this deferred BUI to the transaction. */
347 struct xfs_trans *tp,
348 struct xfs_bmap_intent *bi)
350 trace_xfs_bmap_defer(bi);
352 xfs_bmap_update_get_group(tp->t_mountp, bi);
353 xfs_defer_add(tp, &bi->bi_list, &xfs_bmap_update_defer_type);
356 /* Release a passive AG ref after finishing mapping work. */
358 xfs_bmap_update_put_group(
359 struct xfs_bmap_intent *bi)
361 if (xfs_ifork_is_realtime(bi->bi_owner, bi->bi_whichfork))
364 xfs_perag_intent_put(bi->bi_pag);
367 /* Cancel a deferred bmap update. */
369 xfs_bmap_update_cancel_item(
370 struct list_head *item)
372 struct xfs_bmap_intent *bi = bi_entry(item);
374 xfs_bmap_update_put_group(bi);
375 kmem_cache_free(xfs_bmap_intent_cache, bi);
378 /* Process a deferred bmap update. */
380 xfs_bmap_update_finish_item(
381 struct xfs_trans *tp,
382 struct xfs_log_item *done,
383 struct list_head *item,
384 struct xfs_btree_cur **state)
386 struct xfs_bmap_intent *bi = bi_entry(item);
389 error = xfs_bmap_finish_one(tp, bi);
390 if (!error && bi->bi_bmap.br_blockcount > 0) {
391 ASSERT(bi->bi_type == XFS_BMAP_UNMAP);
395 xfs_bmap_update_cancel_item(item);
399 /* Abort all pending BUIs. */
401 xfs_bmap_update_abort_intent(
402 struct xfs_log_item *intent)
404 xfs_bui_release(BUI_ITEM(intent));
407 /* Is this recovered BUI ok? */
410 struct xfs_mount *mp,
411 struct xfs_bui_log_item *buip)
413 struct xfs_map_extent *map;
415 /* Only one mapping operation per BUI... */
416 if (buip->bui_format.bui_nextents != XFS_BUI_MAX_FAST_EXTENTS)
419 map = &buip->bui_format.bui_extents[0];
421 if (map->me_flags & ~XFS_BMAP_EXTENT_FLAGS)
424 switch (map->me_flags & XFS_BMAP_EXTENT_TYPE_MASK) {
432 if (!xfs_verify_ino(mp, map->me_owner))
435 if (!xfs_verify_fileext(mp, map->me_startoff, map->me_len))
438 if (map->me_flags & XFS_BMAP_EXTENT_REALTIME)
439 return xfs_verify_rtbext(mp, map->me_startblock, map->me_len);
441 return xfs_verify_fsbext(mp, map->me_startblock, map->me_len);
444 static inline struct xfs_bmap_intent *
445 xfs_bui_recover_work(
446 struct xfs_mount *mp,
447 struct xfs_defer_pending *dfp,
448 struct xfs_inode **ipp,
449 struct xfs_map_extent *map)
451 struct xfs_bmap_intent *bi;
454 error = xlog_recover_iget(mp, map->me_owner, ipp);
456 return ERR_PTR(error);
458 bi = kmem_cache_zalloc(xfs_bmap_intent_cache,
459 GFP_KERNEL | __GFP_NOFAIL);
460 bi->bi_whichfork = (map->me_flags & XFS_BMAP_EXTENT_ATTR_FORK) ?
461 XFS_ATTR_FORK : XFS_DATA_FORK;
462 bi->bi_type = map->me_flags & XFS_BMAP_EXTENT_TYPE_MASK;
463 bi->bi_bmap.br_startblock = map->me_startblock;
464 bi->bi_bmap.br_startoff = map->me_startoff;
465 bi->bi_bmap.br_blockcount = map->me_len;
466 bi->bi_bmap.br_state = (map->me_flags & XFS_BMAP_EXTENT_UNWRITTEN) ?
467 XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
469 xfs_bmap_update_get_group(mp, bi);
471 xfs_defer_add_item(dfp, &bi->bi_list);
476 * Process a bmap update intent item that was recovered from the log.
477 * We need to update some inode's bmbt.
480 xfs_bmap_recover_work(
481 struct xfs_defer_pending *dfp,
482 struct list_head *capture_list)
484 struct xfs_trans_res resv;
485 struct xfs_log_item *lip = dfp->dfp_intent;
486 struct xfs_bui_log_item *buip = BUI_ITEM(lip);
487 struct xfs_trans *tp;
488 struct xfs_inode *ip = NULL;
489 struct xfs_mount *mp = lip->li_log->l_mp;
490 struct xfs_map_extent *map;
491 struct xfs_bmap_intent *work;
495 if (!xfs_bui_validate(mp, buip)) {
496 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
497 &buip->bui_format, sizeof(buip->bui_format));
498 return -EFSCORRUPTED;
501 map = &buip->bui_format.bui_extents[0];
502 work = xfs_bui_recover_work(mp, dfp, &ip, map);
504 return PTR_ERR(work);
506 /* Allocate transaction and do the work. */
507 resv = xlog_recover_resv(&M_RES(mp)->tr_itruncate);
508 error = xfs_trans_alloc(mp, &resv,
509 XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK), 0, 0, &tp);
513 xfs_ilock(ip, XFS_ILOCK_EXCL);
514 xfs_trans_ijoin(tp, ip, 0);
516 if (!!(map->me_flags & XFS_BMAP_EXTENT_REALTIME) !=
517 xfs_ifork_is_realtime(ip, work->bi_whichfork)) {
518 error = -EFSCORRUPTED;
522 if (work->bi_type == XFS_BMAP_MAP)
523 iext_delta = XFS_IEXT_ADD_NOSPLIT_CNT;
525 iext_delta = XFS_IEXT_PUNCH_HOLE_CNT;
527 error = xfs_iext_count_may_overflow(ip, work->bi_whichfork, iext_delta);
529 error = xfs_iext_count_upgrade(tp, ip, iext_delta);
533 error = xlog_recover_finish_intent(tp, dfp);
534 if (error == -EFSCORRUPTED)
535 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
536 &buip->bui_format, sizeof(buip->bui_format));
541 * Commit transaction, which frees the transaction and saves the inode
542 * for later replay activities.
544 error = xfs_defer_ops_capture_and_commit(tp, capture_list);
548 xfs_iunlock(ip, XFS_ILOCK_EXCL);
553 xfs_trans_cancel(tp);
555 xfs_iunlock(ip, XFS_ILOCK_EXCL);
561 /* Relog an intent item to push the log tail forward. */
562 static struct xfs_log_item *
563 xfs_bmap_relog_intent(
564 struct xfs_trans *tp,
565 struct xfs_log_item *intent,
566 struct xfs_log_item *done_item)
568 struct xfs_bui_log_item *buip;
569 struct xfs_map_extent *map;
572 count = BUI_ITEM(intent)->bui_format.bui_nextents;
573 map = BUI_ITEM(intent)->bui_format.bui_extents;
575 buip = xfs_bui_init(tp->t_mountp);
576 memcpy(buip->bui_format.bui_extents, map, count * sizeof(*map));
577 atomic_set(&buip->bui_next_extent, count);
579 return &buip->bui_item;
582 const struct xfs_defer_op_type xfs_bmap_update_defer_type = {
584 .max_items = XFS_BUI_MAX_FAST_EXTENTS,
585 .create_intent = xfs_bmap_update_create_intent,
586 .abort_intent = xfs_bmap_update_abort_intent,
587 .create_done = xfs_bmap_update_create_done,
588 .finish_item = xfs_bmap_update_finish_item,
589 .cancel_item = xfs_bmap_update_cancel_item,
590 .recover_work = xfs_bmap_recover_work,
591 .relog_intent = xfs_bmap_relog_intent,
596 struct xfs_log_item *lip,
599 return BUI_ITEM(lip)->bui_format.bui_id == intent_id;
602 static const struct xfs_item_ops xfs_bui_item_ops = {
603 .flags = XFS_ITEM_INTENT,
604 .iop_size = xfs_bui_item_size,
605 .iop_format = xfs_bui_item_format,
606 .iop_unpin = xfs_bui_item_unpin,
607 .iop_release = xfs_bui_item_release,
608 .iop_match = xfs_bui_item_match,
613 struct xfs_bui_log_format *dst,
614 const struct xfs_bui_log_format *src)
618 memcpy(dst, src, offsetof(struct xfs_bui_log_format, bui_extents));
620 for (i = 0; i < src->bui_nextents; i++)
621 memcpy(&dst->bui_extents[i], &src->bui_extents[i],
622 sizeof(struct xfs_map_extent));
626 * This routine is called to create an in-core extent bmap update
627 * item from the bui format structure which was logged on disk.
628 * It allocates an in-core bui, copies the extents from the format
629 * structure into it, and adds the bui to the AIL with the given
633 xlog_recover_bui_commit_pass2(
635 struct list_head *buffer_list,
636 struct xlog_recover_item *item,
639 struct xfs_mount *mp = log->l_mp;
640 struct xfs_bui_log_item *buip;
641 struct xfs_bui_log_format *bui_formatp;
644 bui_formatp = item->ri_buf[0].i_addr;
646 if (item->ri_buf[0].i_len < xfs_bui_log_format_sizeof(0)) {
647 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
648 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
649 return -EFSCORRUPTED;
652 if (bui_formatp->bui_nextents != XFS_BUI_MAX_FAST_EXTENTS) {
653 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
654 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
655 return -EFSCORRUPTED;
658 len = xfs_bui_log_format_sizeof(bui_formatp->bui_nextents);
659 if (item->ri_buf[0].i_len != len) {
660 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
661 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
662 return -EFSCORRUPTED;
665 buip = xfs_bui_init(mp);
666 xfs_bui_copy_format(&buip->bui_format, bui_formatp);
667 atomic_set(&buip->bui_next_extent, bui_formatp->bui_nextents);
669 xlog_recover_intent_item(log, &buip->bui_item, lsn,
670 &xfs_bmap_update_defer_type);
674 const struct xlog_recover_item_ops xlog_bui_item_ops = {
675 .item_type = XFS_LI_BUI,
676 .commit_pass2 = xlog_recover_bui_commit_pass2,
680 * This routine is called when an BUD format structure is found in a committed
681 * transaction in the log. Its purpose is to cancel the corresponding BUI if it
682 * was still in the log. To do this it searches the AIL for the BUI with an id
683 * equal to that in the BUD format structure. If we find it we drop the BUD
684 * reference, which removes the BUI from the AIL and frees it.
687 xlog_recover_bud_commit_pass2(
689 struct list_head *buffer_list,
690 struct xlog_recover_item *item,
693 struct xfs_bud_log_format *bud_formatp;
695 bud_formatp = item->ri_buf[0].i_addr;
696 if (item->ri_buf[0].i_len != sizeof(struct xfs_bud_log_format)) {
697 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
698 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
699 return -EFSCORRUPTED;
702 xlog_recover_release_intent(log, XFS_LI_BUI, bud_formatp->bud_bui_id);
706 const struct xlog_recover_item_ops xlog_bud_item_ops = {
707 .item_type = XFS_LI_BUD,
708 .commit_pass2 = xlog_recover_bud_commit_pass2,