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_trans.h"
16 #include "xfs_trans_priv.h"
17 #include "xfs_rmap_item.h"
20 #include "xfs_error.h"
21 #include "xfs_log_priv.h"
22 #include "xfs_log_recover.h"
24 struct kmem_cache *xfs_rui_cache;
25 struct kmem_cache *xfs_rud_cache;
27 static const struct xfs_item_ops xfs_rui_item_ops;
29 static inline struct xfs_rui_log_item *RUI_ITEM(struct xfs_log_item *lip)
31 return container_of(lip, struct xfs_rui_log_item, rui_item);
36 struct xfs_rui_log_item *ruip)
38 kmem_free(ruip->rui_item.li_lv_shadow);
39 if (ruip->rui_format.rui_nextents > XFS_RUI_MAX_FAST_EXTENTS)
42 kmem_cache_free(xfs_rui_cache, ruip);
46 * Freeing the RUI requires that we remove it from the AIL if it has already
47 * been placed there. However, the RUI may not yet have been placed in the AIL
48 * when called by xfs_rui_release() from RUD processing due to the ordering of
49 * committed vs unpin operations in bulk insert operations. Hence the reference
50 * count to ensure only the last caller frees the RUI.
54 struct xfs_rui_log_item *ruip)
56 ASSERT(atomic_read(&ruip->rui_refcount) > 0);
57 if (!atomic_dec_and_test(&ruip->rui_refcount))
60 xfs_trans_ail_delete(&ruip->rui_item, 0);
61 xfs_rui_item_free(ruip);
66 struct xfs_log_item *lip,
70 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
73 *nbytes += xfs_rui_log_format_sizeof(ruip->rui_format.rui_nextents);
77 * This is called to fill in the vector of log iovecs for the
78 * given rui log item. We use only 1 iovec, and we point that
79 * at the rui_log_format structure embedded in the rui item.
80 * It is at this point that we assert that all of the extent
81 * slots in the rui item have been filled.
85 struct xfs_log_item *lip,
86 struct xfs_log_vec *lv)
88 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
89 struct xfs_log_iovec *vecp = NULL;
91 ASSERT(atomic_read(&ruip->rui_next_extent) ==
92 ruip->rui_format.rui_nextents);
94 ruip->rui_format.rui_type = XFS_LI_RUI;
95 ruip->rui_format.rui_size = 1;
97 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUI_FORMAT, &ruip->rui_format,
98 xfs_rui_log_format_sizeof(ruip->rui_format.rui_nextents));
102 * The unpin operation is the last place an RUI is manipulated in the log. It is
103 * either inserted in the AIL or aborted in the event of a log I/O error. In
104 * either case, the RUI transaction has been successfully committed to make it
105 * this far. Therefore, we expect whoever committed the RUI to either construct
106 * and commit the RUD or drop the RUD's reference in the event of error. Simply
107 * drop the log's RUI reference now that the log is done with it.
111 struct xfs_log_item *lip,
114 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
116 xfs_rui_release(ruip);
120 * The RUI has been either committed or aborted if the transaction has been
121 * cancelled. If the transaction was cancelled, an RUD isn't going to be
122 * constructed and thus we free the RUI here directly.
125 xfs_rui_item_release(
126 struct xfs_log_item *lip)
128 xfs_rui_release(RUI_ITEM(lip));
132 * Allocate and initialize an rui item with the given number of extents.
134 STATIC struct xfs_rui_log_item *
136 struct xfs_mount *mp,
140 struct xfs_rui_log_item *ruip;
142 ASSERT(nextents > 0);
143 if (nextents > XFS_RUI_MAX_FAST_EXTENTS)
144 ruip = kmem_zalloc(xfs_rui_log_item_sizeof(nextents), 0);
146 ruip = kmem_cache_zalloc(xfs_rui_cache,
147 GFP_KERNEL | __GFP_NOFAIL);
149 xfs_log_item_init(mp, &ruip->rui_item, XFS_LI_RUI, &xfs_rui_item_ops);
150 ruip->rui_format.rui_nextents = nextents;
151 ruip->rui_format.rui_id = (uintptr_t)(void *)ruip;
152 atomic_set(&ruip->rui_next_extent, 0);
153 atomic_set(&ruip->rui_refcount, 2);
158 static inline struct xfs_rud_log_item *RUD_ITEM(struct xfs_log_item *lip)
160 return container_of(lip, struct xfs_rud_log_item, rud_item);
165 struct xfs_log_item *lip,
170 *nbytes += sizeof(struct xfs_rud_log_format);
174 * This is called to fill in the vector of log iovecs for the
175 * given rud log item. We use only 1 iovec, and we point that
176 * at the rud_log_format structure embedded in the rud item.
177 * It is at this point that we assert that all of the extent
178 * slots in the rud item have been filled.
182 struct xfs_log_item *lip,
183 struct xfs_log_vec *lv)
185 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
186 struct xfs_log_iovec *vecp = NULL;
188 rudp->rud_format.rud_type = XFS_LI_RUD;
189 rudp->rud_format.rud_size = 1;
191 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUD_FORMAT, &rudp->rud_format,
192 sizeof(struct xfs_rud_log_format));
196 * The RUD is either committed or aborted if the transaction is cancelled. If
197 * the transaction is cancelled, drop our reference to the RUI and free the
201 xfs_rud_item_release(
202 struct xfs_log_item *lip)
204 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
206 xfs_rui_release(rudp->rud_ruip);
207 kmem_free(rudp->rud_item.li_lv_shadow);
208 kmem_cache_free(xfs_rud_cache, rudp);
211 static struct xfs_log_item *
213 struct xfs_log_item *lip)
215 return &RUD_ITEM(lip)->rud_ruip->rui_item;
218 static const struct xfs_item_ops xfs_rud_item_ops = {
219 .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED |
220 XFS_ITEM_INTENT_DONE,
221 .iop_size = xfs_rud_item_size,
222 .iop_format = xfs_rud_item_format,
223 .iop_release = xfs_rud_item_release,
224 .iop_intent = xfs_rud_item_intent,
227 static struct xfs_rud_log_item *
229 struct xfs_trans *tp,
230 struct xfs_rui_log_item *ruip)
232 struct xfs_rud_log_item *rudp;
234 rudp = kmem_cache_zalloc(xfs_rud_cache, GFP_KERNEL | __GFP_NOFAIL);
235 xfs_log_item_init(tp->t_mountp, &rudp->rud_item, XFS_LI_RUD,
237 rudp->rud_ruip = ruip;
238 rudp->rud_format.rud_rui_id = ruip->rui_format.rui_id;
240 xfs_trans_add_item(tp, &rudp->rud_item);
244 /* Set the map extent flags for this reverse mapping. */
246 xfs_trans_set_rmap_flags(
247 struct xfs_map_extent *rmap,
248 enum xfs_rmap_intent_type type,
253 if (state == XFS_EXT_UNWRITTEN)
254 rmap->me_flags |= XFS_RMAP_EXTENT_UNWRITTEN;
255 if (whichfork == XFS_ATTR_FORK)
256 rmap->me_flags |= XFS_RMAP_EXTENT_ATTR_FORK;
259 rmap->me_flags |= XFS_RMAP_EXTENT_MAP;
261 case XFS_RMAP_MAP_SHARED:
262 rmap->me_flags |= XFS_RMAP_EXTENT_MAP_SHARED;
265 rmap->me_flags |= XFS_RMAP_EXTENT_UNMAP;
267 case XFS_RMAP_UNMAP_SHARED:
268 rmap->me_flags |= XFS_RMAP_EXTENT_UNMAP_SHARED;
270 case XFS_RMAP_CONVERT:
271 rmap->me_flags |= XFS_RMAP_EXTENT_CONVERT;
273 case XFS_RMAP_CONVERT_SHARED:
274 rmap->me_flags |= XFS_RMAP_EXTENT_CONVERT_SHARED;
277 rmap->me_flags |= XFS_RMAP_EXTENT_ALLOC;
280 rmap->me_flags |= XFS_RMAP_EXTENT_FREE;
288 * Finish an rmap update and log it to the RUD. Note that the transaction is
289 * marked dirty regardless of whether the rmap update succeeds or fails to
290 * support the RUI/RUD lifecycle rules.
293 xfs_trans_log_finish_rmap_update(
294 struct xfs_trans *tp,
295 struct xfs_rud_log_item *rudp,
296 enum xfs_rmap_intent_type type,
299 xfs_fileoff_t startoff,
300 xfs_fsblock_t startblock,
301 xfs_filblks_t blockcount,
303 struct xfs_btree_cur **pcur)
307 error = xfs_rmap_finish_one(tp, type, owner, whichfork, startoff,
308 startblock, blockcount, state, pcur);
311 * Mark the transaction dirty, even on error. This ensures the
312 * transaction is aborted, which:
314 * 1.) releases the RUI and frees the RUD
315 * 2.) shuts down the filesystem
317 tp->t_flags |= XFS_TRANS_DIRTY | XFS_TRANS_HAS_INTENT_DONE;
318 set_bit(XFS_LI_DIRTY, &rudp->rud_item.li_flags);
323 /* Sort rmap intents by AG. */
325 xfs_rmap_update_diff_items(
327 const struct list_head *a,
328 const struct list_head *b)
330 struct xfs_mount *mp = priv;
331 struct xfs_rmap_intent *ra;
332 struct xfs_rmap_intent *rb;
334 ra = container_of(a, struct xfs_rmap_intent, ri_list);
335 rb = container_of(b, struct xfs_rmap_intent, ri_list);
336 return XFS_FSB_TO_AGNO(mp, ra->ri_bmap.br_startblock) -
337 XFS_FSB_TO_AGNO(mp, rb->ri_bmap.br_startblock);
340 /* Log rmap updates in the intent item. */
342 xfs_rmap_update_log_item(
343 struct xfs_trans *tp,
344 struct xfs_rui_log_item *ruip,
345 struct xfs_rmap_intent *rmap)
348 struct xfs_map_extent *map;
350 tp->t_flags |= XFS_TRANS_DIRTY;
351 set_bit(XFS_LI_DIRTY, &ruip->rui_item.li_flags);
354 * atomic_inc_return gives us the value after the increment;
355 * we want to use it as an array index so we need to subtract 1 from
358 next_extent = atomic_inc_return(&ruip->rui_next_extent) - 1;
359 ASSERT(next_extent < ruip->rui_format.rui_nextents);
360 map = &ruip->rui_format.rui_extents[next_extent];
361 map->me_owner = rmap->ri_owner;
362 map->me_startblock = rmap->ri_bmap.br_startblock;
363 map->me_startoff = rmap->ri_bmap.br_startoff;
364 map->me_len = rmap->ri_bmap.br_blockcount;
365 xfs_trans_set_rmap_flags(map, rmap->ri_type, rmap->ri_whichfork,
366 rmap->ri_bmap.br_state);
369 static struct xfs_log_item *
370 xfs_rmap_update_create_intent(
371 struct xfs_trans *tp,
372 struct list_head *items,
376 struct xfs_mount *mp = tp->t_mountp;
377 struct xfs_rui_log_item *ruip = xfs_rui_init(mp, count);
378 struct xfs_rmap_intent *rmap;
382 xfs_trans_add_item(tp, &ruip->rui_item);
384 list_sort(mp, items, xfs_rmap_update_diff_items);
385 list_for_each_entry(rmap, items, ri_list)
386 xfs_rmap_update_log_item(tp, ruip, rmap);
387 return &ruip->rui_item;
390 /* Get an RUD so we can process all the deferred rmap updates. */
391 static struct xfs_log_item *
392 xfs_rmap_update_create_done(
393 struct xfs_trans *tp,
394 struct xfs_log_item *intent,
397 return &xfs_trans_get_rud(tp, RUI_ITEM(intent))->rud_item;
400 /* Process a deferred rmap update. */
402 xfs_rmap_update_finish_item(
403 struct xfs_trans *tp,
404 struct xfs_log_item *done,
405 struct list_head *item,
406 struct xfs_btree_cur **state)
408 struct xfs_rmap_intent *rmap;
411 rmap = container_of(item, struct xfs_rmap_intent, ri_list);
412 error = xfs_trans_log_finish_rmap_update(tp, RUD_ITEM(done),
413 rmap->ri_type, rmap->ri_owner, rmap->ri_whichfork,
414 rmap->ri_bmap.br_startoff, rmap->ri_bmap.br_startblock,
415 rmap->ri_bmap.br_blockcount, rmap->ri_bmap.br_state,
417 kmem_cache_free(xfs_rmap_intent_cache, rmap);
421 /* Abort all pending RUIs. */
423 xfs_rmap_update_abort_intent(
424 struct xfs_log_item *intent)
426 xfs_rui_release(RUI_ITEM(intent));
429 /* Cancel a deferred rmap update. */
431 xfs_rmap_update_cancel_item(
432 struct list_head *item)
434 struct xfs_rmap_intent *rmap;
436 rmap = container_of(item, struct xfs_rmap_intent, ri_list);
437 kmem_cache_free(xfs_rmap_intent_cache, rmap);
440 const struct xfs_defer_op_type xfs_rmap_update_defer_type = {
441 .max_items = XFS_RUI_MAX_FAST_EXTENTS,
442 .create_intent = xfs_rmap_update_create_intent,
443 .abort_intent = xfs_rmap_update_abort_intent,
444 .create_done = xfs_rmap_update_create_done,
445 .finish_item = xfs_rmap_update_finish_item,
446 .finish_cleanup = xfs_rmap_finish_one_cleanup,
447 .cancel_item = xfs_rmap_update_cancel_item,
450 /* Is this recovered RUI ok? */
452 xfs_rui_validate_map(
453 struct xfs_mount *mp,
454 struct xfs_map_extent *rmap)
456 if (!xfs_has_rmapbt(mp))
459 if (rmap->me_flags & ~XFS_RMAP_EXTENT_FLAGS)
462 switch (rmap->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
463 case XFS_RMAP_EXTENT_MAP:
464 case XFS_RMAP_EXTENT_MAP_SHARED:
465 case XFS_RMAP_EXTENT_UNMAP:
466 case XFS_RMAP_EXTENT_UNMAP_SHARED:
467 case XFS_RMAP_EXTENT_CONVERT:
468 case XFS_RMAP_EXTENT_CONVERT_SHARED:
469 case XFS_RMAP_EXTENT_ALLOC:
470 case XFS_RMAP_EXTENT_FREE:
476 if (!XFS_RMAP_NON_INODE_OWNER(rmap->me_owner) &&
477 !xfs_verify_ino(mp, rmap->me_owner))
480 if (!xfs_verify_fileext(mp, rmap->me_startoff, rmap->me_len))
483 return xfs_verify_fsbext(mp, rmap->me_startblock, rmap->me_len);
487 * Process an rmap update intent item that was recovered from the log.
488 * We need to update the rmapbt.
491 xfs_rui_item_recover(
492 struct xfs_log_item *lip,
493 struct list_head *capture_list)
495 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
496 struct xfs_map_extent *rmap;
497 struct xfs_rud_log_item *rudp;
498 struct xfs_trans *tp;
499 struct xfs_btree_cur *rcur = NULL;
500 struct xfs_mount *mp = lip->li_log->l_mp;
501 enum xfs_rmap_intent_type type;
508 * First check the validity of the extents described by the
509 * RUI. If any are bad, then assume that all are bad and
512 for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
513 if (!xfs_rui_validate_map(mp,
514 &ruip->rui_format.rui_extents[i])) {
515 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
517 sizeof(ruip->rui_format));
518 return -EFSCORRUPTED;
522 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate,
523 mp->m_rmap_maxlevels, 0, XFS_TRANS_RESERVE, &tp);
526 rudp = xfs_trans_get_rud(tp, ruip);
528 for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
529 rmap = &ruip->rui_format.rui_extents[i];
530 state = (rmap->me_flags & XFS_RMAP_EXTENT_UNWRITTEN) ?
531 XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
532 whichfork = (rmap->me_flags & XFS_RMAP_EXTENT_ATTR_FORK) ?
533 XFS_ATTR_FORK : XFS_DATA_FORK;
534 switch (rmap->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
535 case XFS_RMAP_EXTENT_MAP:
538 case XFS_RMAP_EXTENT_MAP_SHARED:
539 type = XFS_RMAP_MAP_SHARED;
541 case XFS_RMAP_EXTENT_UNMAP:
542 type = XFS_RMAP_UNMAP;
544 case XFS_RMAP_EXTENT_UNMAP_SHARED:
545 type = XFS_RMAP_UNMAP_SHARED;
547 case XFS_RMAP_EXTENT_CONVERT:
548 type = XFS_RMAP_CONVERT;
550 case XFS_RMAP_EXTENT_CONVERT_SHARED:
551 type = XFS_RMAP_CONVERT_SHARED;
553 case XFS_RMAP_EXTENT_ALLOC:
554 type = XFS_RMAP_ALLOC;
556 case XFS_RMAP_EXTENT_FREE:
557 type = XFS_RMAP_FREE;
560 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
562 sizeof(ruip->rui_format));
563 error = -EFSCORRUPTED;
566 error = xfs_trans_log_finish_rmap_update(tp, rudp, type,
567 rmap->me_owner, whichfork,
568 rmap->me_startoff, rmap->me_startblock,
569 rmap->me_len, state, &rcur);
570 if (error == -EFSCORRUPTED)
571 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
572 rmap, sizeof(*rmap));
578 xfs_rmap_finish_one_cleanup(tp, rcur, error);
579 return xfs_defer_ops_capture_and_commit(tp, capture_list);
582 xfs_rmap_finish_one_cleanup(tp, rcur, error);
583 xfs_trans_cancel(tp);
589 struct xfs_log_item *lip,
592 return RUI_ITEM(lip)->rui_format.rui_id == intent_id;
595 /* Relog an intent item to push the log tail forward. */
596 static struct xfs_log_item *
598 struct xfs_log_item *intent,
599 struct xfs_trans *tp)
601 struct xfs_rud_log_item *rudp;
602 struct xfs_rui_log_item *ruip;
603 struct xfs_map_extent *extp;
606 count = RUI_ITEM(intent)->rui_format.rui_nextents;
607 extp = RUI_ITEM(intent)->rui_format.rui_extents;
609 tp->t_flags |= XFS_TRANS_DIRTY;
610 rudp = xfs_trans_get_rud(tp, RUI_ITEM(intent));
611 set_bit(XFS_LI_DIRTY, &rudp->rud_item.li_flags);
613 ruip = xfs_rui_init(tp->t_mountp, count);
614 memcpy(ruip->rui_format.rui_extents, extp, count * sizeof(*extp));
615 atomic_set(&ruip->rui_next_extent, count);
616 xfs_trans_add_item(tp, &ruip->rui_item);
617 set_bit(XFS_LI_DIRTY, &ruip->rui_item.li_flags);
618 return &ruip->rui_item;
621 static const struct xfs_item_ops xfs_rui_item_ops = {
622 .flags = XFS_ITEM_INTENT,
623 .iop_size = xfs_rui_item_size,
624 .iop_format = xfs_rui_item_format,
625 .iop_unpin = xfs_rui_item_unpin,
626 .iop_release = xfs_rui_item_release,
627 .iop_recover = xfs_rui_item_recover,
628 .iop_match = xfs_rui_item_match,
629 .iop_relog = xfs_rui_item_relog,
634 struct xfs_rui_log_format *dst,
635 const struct xfs_rui_log_format *src)
639 memcpy(dst, src, offsetof(struct xfs_rui_log_format, rui_extents));
641 for (i = 0; i < src->rui_nextents; i++)
642 memcpy(&dst->rui_extents[i], &src->rui_extents[i],
643 sizeof(struct xfs_map_extent));
647 * This routine is called to create an in-core extent rmap update
648 * item from the rui format structure which was logged on disk.
649 * It allocates an in-core rui, copies the extents from the format
650 * structure into it, and adds the rui to the AIL with the given
654 xlog_recover_rui_commit_pass2(
656 struct list_head *buffer_list,
657 struct xlog_recover_item *item,
660 struct xfs_mount *mp = log->l_mp;
661 struct xfs_rui_log_item *ruip;
662 struct xfs_rui_log_format *rui_formatp;
665 rui_formatp = item->ri_buf[0].i_addr;
667 if (item->ri_buf[0].i_len < xfs_rui_log_format_sizeof(0)) {
668 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
669 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
670 return -EFSCORRUPTED;
673 len = xfs_rui_log_format_sizeof(rui_formatp->rui_nextents);
674 if (item->ri_buf[0].i_len != len) {
675 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
676 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
677 return -EFSCORRUPTED;
680 ruip = xfs_rui_init(mp, rui_formatp->rui_nextents);
681 xfs_rui_copy_format(&ruip->rui_format, rui_formatp);
682 atomic_set(&ruip->rui_next_extent, rui_formatp->rui_nextents);
684 * Insert the intent into the AIL directly and drop one reference so
685 * that finishing or canceling the work will drop the other.
687 xfs_trans_ail_insert(log->l_ailp, &ruip->rui_item, lsn);
688 xfs_rui_release(ruip);
692 const struct xlog_recover_item_ops xlog_rui_item_ops = {
693 .item_type = XFS_LI_RUI,
694 .commit_pass2 = xlog_recover_rui_commit_pass2,
698 * This routine is called when an RUD format structure is found in a committed
699 * transaction in the log. Its purpose is to cancel the corresponding RUI if it
700 * was still in the log. To do this it searches the AIL for the RUI with an id
701 * equal to that in the RUD format structure. If we find it we drop the RUD
702 * reference, which removes the RUI from the AIL and frees it.
705 xlog_recover_rud_commit_pass2(
707 struct list_head *buffer_list,
708 struct xlog_recover_item *item,
711 struct xfs_rud_log_format *rud_formatp;
713 rud_formatp = item->ri_buf[0].i_addr;
714 if (item->ri_buf[0].i_len != sizeof(struct xfs_rud_log_format)) {
715 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
716 rud_formatp, item->ri_buf[0].i_len);
717 return -EFSCORRUPTED;
720 xlog_recover_release_intent(log, XFS_LI_RUI, rud_formatp->rud_rui_id);
724 const struct xlog_recover_item_ops xlog_rud_item_ops = {
725 .item_type = XFS_LI_RUD,
726 .commit_pass2 = xlog_recover_rud_commit_pass2,