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);
159 * Copy an RUI format buffer from the given buf, and into the destination
160 * RUI format structure. The RUI/RUD items were designed not to need any
161 * special alignment handling.
165 struct xfs_log_iovec *buf,
166 struct xfs_rui_log_format *dst_rui_fmt)
168 struct xfs_rui_log_format *src_rui_fmt;
171 src_rui_fmt = buf->i_addr;
172 len = xfs_rui_log_format_sizeof(src_rui_fmt->rui_nextents);
174 if (buf->i_len != len) {
175 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL);
176 return -EFSCORRUPTED;
179 memcpy(dst_rui_fmt, src_rui_fmt, len);
183 static inline struct xfs_rud_log_item *RUD_ITEM(struct xfs_log_item *lip)
185 return container_of(lip, struct xfs_rud_log_item, rud_item);
190 struct xfs_log_item *lip,
195 *nbytes += sizeof(struct xfs_rud_log_format);
199 * This is called to fill in the vector of log iovecs for the
200 * given rud log item. We use only 1 iovec, and we point that
201 * at the rud_log_format structure embedded in the rud item.
202 * It is at this point that we assert that all of the extent
203 * slots in the rud item have been filled.
207 struct xfs_log_item *lip,
208 struct xfs_log_vec *lv)
210 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
211 struct xfs_log_iovec *vecp = NULL;
213 rudp->rud_format.rud_type = XFS_LI_RUD;
214 rudp->rud_format.rud_size = 1;
216 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUD_FORMAT, &rudp->rud_format,
217 sizeof(struct xfs_rud_log_format));
221 * The RUD is either committed or aborted if the transaction is cancelled. If
222 * the transaction is cancelled, drop our reference to the RUI and free the
226 xfs_rud_item_release(
227 struct xfs_log_item *lip)
229 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
231 xfs_rui_release(rudp->rud_ruip);
232 kmem_free(rudp->rud_item.li_lv_shadow);
233 kmem_cache_free(xfs_rud_cache, rudp);
236 static struct xfs_log_item *
238 struct xfs_log_item *lip)
240 return &RUD_ITEM(lip)->rud_ruip->rui_item;
243 static const struct xfs_item_ops xfs_rud_item_ops = {
244 .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED |
245 XFS_ITEM_INTENT_DONE,
246 .iop_size = xfs_rud_item_size,
247 .iop_format = xfs_rud_item_format,
248 .iop_release = xfs_rud_item_release,
249 .iop_intent = xfs_rud_item_intent,
252 static struct xfs_rud_log_item *
254 struct xfs_trans *tp,
255 struct xfs_rui_log_item *ruip)
257 struct xfs_rud_log_item *rudp;
259 rudp = kmem_cache_zalloc(xfs_rud_cache, GFP_KERNEL | __GFP_NOFAIL);
260 xfs_log_item_init(tp->t_mountp, &rudp->rud_item, XFS_LI_RUD,
262 rudp->rud_ruip = ruip;
263 rudp->rud_format.rud_rui_id = ruip->rui_format.rui_id;
265 xfs_trans_add_item(tp, &rudp->rud_item);
269 /* Set the map extent flags for this reverse mapping. */
271 xfs_trans_set_rmap_flags(
272 struct xfs_map_extent *rmap,
273 enum xfs_rmap_intent_type type,
278 if (state == XFS_EXT_UNWRITTEN)
279 rmap->me_flags |= XFS_RMAP_EXTENT_UNWRITTEN;
280 if (whichfork == XFS_ATTR_FORK)
281 rmap->me_flags |= XFS_RMAP_EXTENT_ATTR_FORK;
284 rmap->me_flags |= XFS_RMAP_EXTENT_MAP;
286 case XFS_RMAP_MAP_SHARED:
287 rmap->me_flags |= XFS_RMAP_EXTENT_MAP_SHARED;
290 rmap->me_flags |= XFS_RMAP_EXTENT_UNMAP;
292 case XFS_RMAP_UNMAP_SHARED:
293 rmap->me_flags |= XFS_RMAP_EXTENT_UNMAP_SHARED;
295 case XFS_RMAP_CONVERT:
296 rmap->me_flags |= XFS_RMAP_EXTENT_CONVERT;
298 case XFS_RMAP_CONVERT_SHARED:
299 rmap->me_flags |= XFS_RMAP_EXTENT_CONVERT_SHARED;
302 rmap->me_flags |= XFS_RMAP_EXTENT_ALLOC;
305 rmap->me_flags |= XFS_RMAP_EXTENT_FREE;
313 * Finish an rmap update and log it to the RUD. Note that the transaction is
314 * marked dirty regardless of whether the rmap update succeeds or fails to
315 * support the RUI/RUD lifecycle rules.
318 xfs_trans_log_finish_rmap_update(
319 struct xfs_trans *tp,
320 struct xfs_rud_log_item *rudp,
321 enum xfs_rmap_intent_type type,
324 xfs_fileoff_t startoff,
325 xfs_fsblock_t startblock,
326 xfs_filblks_t blockcount,
328 struct xfs_btree_cur **pcur)
332 error = xfs_rmap_finish_one(tp, type, owner, whichfork, startoff,
333 startblock, blockcount, state, pcur);
336 * Mark the transaction dirty, even on error. This ensures the
337 * transaction is aborted, which:
339 * 1.) releases the RUI and frees the RUD
340 * 2.) shuts down the filesystem
342 tp->t_flags |= XFS_TRANS_DIRTY | XFS_TRANS_HAS_INTENT_DONE;
343 set_bit(XFS_LI_DIRTY, &rudp->rud_item.li_flags);
348 /* Sort rmap intents by AG. */
350 xfs_rmap_update_diff_items(
352 const struct list_head *a,
353 const struct list_head *b)
355 struct xfs_mount *mp = priv;
356 struct xfs_rmap_intent *ra;
357 struct xfs_rmap_intent *rb;
359 ra = container_of(a, struct xfs_rmap_intent, ri_list);
360 rb = container_of(b, struct xfs_rmap_intent, ri_list);
361 return XFS_FSB_TO_AGNO(mp, ra->ri_bmap.br_startblock) -
362 XFS_FSB_TO_AGNO(mp, rb->ri_bmap.br_startblock);
365 /* Log rmap updates in the intent item. */
367 xfs_rmap_update_log_item(
368 struct xfs_trans *tp,
369 struct xfs_rui_log_item *ruip,
370 struct xfs_rmap_intent *rmap)
373 struct xfs_map_extent *map;
375 tp->t_flags |= XFS_TRANS_DIRTY;
376 set_bit(XFS_LI_DIRTY, &ruip->rui_item.li_flags);
379 * atomic_inc_return gives us the value after the increment;
380 * we want to use it as an array index so we need to subtract 1 from
383 next_extent = atomic_inc_return(&ruip->rui_next_extent) - 1;
384 ASSERT(next_extent < ruip->rui_format.rui_nextents);
385 map = &ruip->rui_format.rui_extents[next_extent];
386 map->me_owner = rmap->ri_owner;
387 map->me_startblock = rmap->ri_bmap.br_startblock;
388 map->me_startoff = rmap->ri_bmap.br_startoff;
389 map->me_len = rmap->ri_bmap.br_blockcount;
390 xfs_trans_set_rmap_flags(map, rmap->ri_type, rmap->ri_whichfork,
391 rmap->ri_bmap.br_state);
394 static struct xfs_log_item *
395 xfs_rmap_update_create_intent(
396 struct xfs_trans *tp,
397 struct list_head *items,
401 struct xfs_mount *mp = tp->t_mountp;
402 struct xfs_rui_log_item *ruip = xfs_rui_init(mp, count);
403 struct xfs_rmap_intent *rmap;
407 xfs_trans_add_item(tp, &ruip->rui_item);
409 list_sort(mp, items, xfs_rmap_update_diff_items);
410 list_for_each_entry(rmap, items, ri_list)
411 xfs_rmap_update_log_item(tp, ruip, rmap);
412 return &ruip->rui_item;
415 /* Get an RUD so we can process all the deferred rmap updates. */
416 static struct xfs_log_item *
417 xfs_rmap_update_create_done(
418 struct xfs_trans *tp,
419 struct xfs_log_item *intent,
422 return &xfs_trans_get_rud(tp, RUI_ITEM(intent))->rud_item;
425 /* Process a deferred rmap update. */
427 xfs_rmap_update_finish_item(
428 struct xfs_trans *tp,
429 struct xfs_log_item *done,
430 struct list_head *item,
431 struct xfs_btree_cur **state)
433 struct xfs_rmap_intent *rmap;
436 rmap = container_of(item, struct xfs_rmap_intent, ri_list);
437 error = xfs_trans_log_finish_rmap_update(tp, RUD_ITEM(done),
438 rmap->ri_type, rmap->ri_owner, rmap->ri_whichfork,
439 rmap->ri_bmap.br_startoff, rmap->ri_bmap.br_startblock,
440 rmap->ri_bmap.br_blockcount, rmap->ri_bmap.br_state,
442 kmem_cache_free(xfs_rmap_intent_cache, rmap);
446 /* Abort all pending RUIs. */
448 xfs_rmap_update_abort_intent(
449 struct xfs_log_item *intent)
451 xfs_rui_release(RUI_ITEM(intent));
454 /* Cancel a deferred rmap update. */
456 xfs_rmap_update_cancel_item(
457 struct list_head *item)
459 struct xfs_rmap_intent *rmap;
461 rmap = container_of(item, struct xfs_rmap_intent, ri_list);
462 kmem_cache_free(xfs_rmap_intent_cache, rmap);
465 const struct xfs_defer_op_type xfs_rmap_update_defer_type = {
466 .max_items = XFS_RUI_MAX_FAST_EXTENTS,
467 .create_intent = xfs_rmap_update_create_intent,
468 .abort_intent = xfs_rmap_update_abort_intent,
469 .create_done = xfs_rmap_update_create_done,
470 .finish_item = xfs_rmap_update_finish_item,
471 .finish_cleanup = xfs_rmap_finish_one_cleanup,
472 .cancel_item = xfs_rmap_update_cancel_item,
475 /* Is this recovered RUI ok? */
477 xfs_rui_validate_map(
478 struct xfs_mount *mp,
479 struct xfs_map_extent *rmap)
481 if (!xfs_has_rmapbt(mp))
484 if (rmap->me_flags & ~XFS_RMAP_EXTENT_FLAGS)
487 switch (rmap->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
488 case XFS_RMAP_EXTENT_MAP:
489 case XFS_RMAP_EXTENT_MAP_SHARED:
490 case XFS_RMAP_EXTENT_UNMAP:
491 case XFS_RMAP_EXTENT_UNMAP_SHARED:
492 case XFS_RMAP_EXTENT_CONVERT:
493 case XFS_RMAP_EXTENT_CONVERT_SHARED:
494 case XFS_RMAP_EXTENT_ALLOC:
495 case XFS_RMAP_EXTENT_FREE:
501 if (!XFS_RMAP_NON_INODE_OWNER(rmap->me_owner) &&
502 !xfs_verify_ino(mp, rmap->me_owner))
505 if (!xfs_verify_fileext(mp, rmap->me_startoff, rmap->me_len))
508 return xfs_verify_fsbext(mp, rmap->me_startblock, rmap->me_len);
512 * Process an rmap update intent item that was recovered from the log.
513 * We need to update the rmapbt.
516 xfs_rui_item_recover(
517 struct xfs_log_item *lip,
518 struct list_head *capture_list)
520 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
521 struct xfs_map_extent *rmap;
522 struct xfs_rud_log_item *rudp;
523 struct xfs_trans *tp;
524 struct xfs_btree_cur *rcur = NULL;
525 struct xfs_mount *mp = lip->li_log->l_mp;
526 enum xfs_rmap_intent_type type;
533 * First check the validity of the extents described by the
534 * RUI. If any are bad, then assume that all are bad and
537 for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
538 if (!xfs_rui_validate_map(mp,
539 &ruip->rui_format.rui_extents[i])) {
540 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
542 sizeof(ruip->rui_format));
543 return -EFSCORRUPTED;
547 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate,
548 mp->m_rmap_maxlevels, 0, XFS_TRANS_RESERVE, &tp);
551 rudp = xfs_trans_get_rud(tp, ruip);
553 for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
554 rmap = &ruip->rui_format.rui_extents[i];
555 state = (rmap->me_flags & XFS_RMAP_EXTENT_UNWRITTEN) ?
556 XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
557 whichfork = (rmap->me_flags & XFS_RMAP_EXTENT_ATTR_FORK) ?
558 XFS_ATTR_FORK : XFS_DATA_FORK;
559 switch (rmap->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
560 case XFS_RMAP_EXTENT_MAP:
563 case XFS_RMAP_EXTENT_MAP_SHARED:
564 type = XFS_RMAP_MAP_SHARED;
566 case XFS_RMAP_EXTENT_UNMAP:
567 type = XFS_RMAP_UNMAP;
569 case XFS_RMAP_EXTENT_UNMAP_SHARED:
570 type = XFS_RMAP_UNMAP_SHARED;
572 case XFS_RMAP_EXTENT_CONVERT:
573 type = XFS_RMAP_CONVERT;
575 case XFS_RMAP_EXTENT_CONVERT_SHARED:
576 type = XFS_RMAP_CONVERT_SHARED;
578 case XFS_RMAP_EXTENT_ALLOC:
579 type = XFS_RMAP_ALLOC;
581 case XFS_RMAP_EXTENT_FREE:
582 type = XFS_RMAP_FREE;
585 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL);
586 error = -EFSCORRUPTED;
589 error = xfs_trans_log_finish_rmap_update(tp, rudp, type,
590 rmap->me_owner, whichfork,
591 rmap->me_startoff, rmap->me_startblock,
592 rmap->me_len, state, &rcur);
593 if (error == -EFSCORRUPTED)
594 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
595 rmap, sizeof(*rmap));
601 xfs_rmap_finish_one_cleanup(tp, rcur, error);
602 return xfs_defer_ops_capture_and_commit(tp, capture_list);
605 xfs_rmap_finish_one_cleanup(tp, rcur, error);
606 xfs_trans_cancel(tp);
612 struct xfs_log_item *lip,
615 return RUI_ITEM(lip)->rui_format.rui_id == intent_id;
618 /* Relog an intent item to push the log tail forward. */
619 static struct xfs_log_item *
621 struct xfs_log_item *intent,
622 struct xfs_trans *tp)
624 struct xfs_rud_log_item *rudp;
625 struct xfs_rui_log_item *ruip;
626 struct xfs_map_extent *extp;
629 count = RUI_ITEM(intent)->rui_format.rui_nextents;
630 extp = RUI_ITEM(intent)->rui_format.rui_extents;
632 tp->t_flags |= XFS_TRANS_DIRTY;
633 rudp = xfs_trans_get_rud(tp, RUI_ITEM(intent));
634 set_bit(XFS_LI_DIRTY, &rudp->rud_item.li_flags);
636 ruip = xfs_rui_init(tp->t_mountp, count);
637 memcpy(ruip->rui_format.rui_extents, extp, count * sizeof(*extp));
638 atomic_set(&ruip->rui_next_extent, count);
639 xfs_trans_add_item(tp, &ruip->rui_item);
640 set_bit(XFS_LI_DIRTY, &ruip->rui_item.li_flags);
641 return &ruip->rui_item;
644 static const struct xfs_item_ops xfs_rui_item_ops = {
645 .flags = XFS_ITEM_INTENT,
646 .iop_size = xfs_rui_item_size,
647 .iop_format = xfs_rui_item_format,
648 .iop_unpin = xfs_rui_item_unpin,
649 .iop_release = xfs_rui_item_release,
650 .iop_recover = xfs_rui_item_recover,
651 .iop_match = xfs_rui_item_match,
652 .iop_relog = xfs_rui_item_relog,
656 * This routine is called to create an in-core extent rmap update
657 * item from the rui format structure which was logged on disk.
658 * It allocates an in-core rui, copies the extents from the format
659 * structure into it, and adds the rui to the AIL with the given
663 xlog_recover_rui_commit_pass2(
665 struct list_head *buffer_list,
666 struct xlog_recover_item *item,
670 struct xfs_mount *mp = log->l_mp;
671 struct xfs_rui_log_item *ruip;
672 struct xfs_rui_log_format *rui_formatp;
674 rui_formatp = item->ri_buf[0].i_addr;
676 ruip = xfs_rui_init(mp, rui_formatp->rui_nextents);
677 error = xfs_rui_copy_format(&item->ri_buf[0], &ruip->rui_format);
679 xfs_rui_item_free(ruip);
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 ASSERT(item->ri_buf[0].i_len == sizeof(struct xfs_rud_log_format));
716 xlog_recover_release_intent(log, XFS_LI_RUI, rud_formatp->rud_rui_id);
720 const struct xlog_recover_item_ops xlog_rud_item_ops = {
721 .item_type = XFS_LI_RUD,
722 .commit_pass2 = xlog_recover_rud_commit_pass2,