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 kmem_zone_t *xfs_rui_zone;
25 kmem_zone_t *xfs_rud_zone;
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_zone, 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)) {
58 xfs_trans_ail_delete(&ruip->rui_item, SHUTDOWN_LOG_IO_ERROR);
59 xfs_rui_item_free(ruip);
65 struct xfs_log_item *lip,
69 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
72 *nbytes += xfs_rui_log_format_sizeof(ruip->rui_format.rui_nextents);
76 * This is called to fill in the vector of log iovecs for the
77 * given rui log item. We use only 1 iovec, and we point that
78 * at the rui_log_format structure embedded in the rui item.
79 * It is at this point that we assert that all of the extent
80 * slots in the rui item have been filled.
84 struct xfs_log_item *lip,
85 struct xfs_log_vec *lv)
87 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
88 struct xfs_log_iovec *vecp = NULL;
90 ASSERT(atomic_read(&ruip->rui_next_extent) ==
91 ruip->rui_format.rui_nextents);
93 ruip->rui_format.rui_type = XFS_LI_RUI;
94 ruip->rui_format.rui_size = 1;
96 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUI_FORMAT, &ruip->rui_format,
97 xfs_rui_log_format_sizeof(ruip->rui_format.rui_nextents));
101 * The unpin operation is the last place an RUI is manipulated in the log. It is
102 * either inserted in the AIL or aborted in the event of a log I/O error. In
103 * either case, the RUI transaction has been successfully committed to make it
104 * this far. Therefore, we expect whoever committed the RUI to either construct
105 * and commit the RUD or drop the RUD's reference in the event of error. Simply
106 * drop the log's RUI reference now that the log is done with it.
110 struct xfs_log_item *lip,
113 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
115 xfs_rui_release(ruip);
119 * The RUI has been either committed or aborted if the transaction has been
120 * cancelled. If the transaction was cancelled, an RUD isn't going to be
121 * constructed and thus we free the RUI here directly.
124 xfs_rui_item_release(
125 struct xfs_log_item *lip)
127 xfs_rui_release(RUI_ITEM(lip));
131 * Allocate and initialize an rui item with the given number of extents.
133 STATIC struct xfs_rui_log_item *
135 struct xfs_mount *mp,
139 struct xfs_rui_log_item *ruip;
141 ASSERT(nextents > 0);
142 if (nextents > XFS_RUI_MAX_FAST_EXTENTS)
143 ruip = kmem_zalloc(xfs_rui_log_item_sizeof(nextents), 0);
145 ruip = kmem_cache_zalloc(xfs_rui_zone,
146 GFP_KERNEL | __GFP_NOFAIL);
148 xfs_log_item_init(mp, &ruip->rui_item, XFS_LI_RUI, &xfs_rui_item_ops);
149 ruip->rui_format.rui_nextents = nextents;
150 ruip->rui_format.rui_id = (uintptr_t)(void *)ruip;
151 atomic_set(&ruip->rui_next_extent, 0);
152 atomic_set(&ruip->rui_refcount, 2);
158 * Copy an RUI format buffer from the given buf, and into the destination
159 * RUI format structure. The RUI/RUD items were designed not to need any
160 * special alignment handling.
164 struct xfs_log_iovec *buf,
165 struct xfs_rui_log_format *dst_rui_fmt)
167 struct xfs_rui_log_format *src_rui_fmt;
170 src_rui_fmt = buf->i_addr;
171 len = xfs_rui_log_format_sizeof(src_rui_fmt->rui_nextents);
173 if (buf->i_len != len) {
174 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL);
175 return -EFSCORRUPTED;
178 memcpy(dst_rui_fmt, src_rui_fmt, len);
182 static inline struct xfs_rud_log_item *RUD_ITEM(struct xfs_log_item *lip)
184 return container_of(lip, struct xfs_rud_log_item, rud_item);
189 struct xfs_log_item *lip,
194 *nbytes += sizeof(struct xfs_rud_log_format);
198 * This is called to fill in the vector of log iovecs for the
199 * given rud log item. We use only 1 iovec, and we point that
200 * at the rud_log_format structure embedded in the rud item.
201 * It is at this point that we assert that all of the extent
202 * slots in the rud item have been filled.
206 struct xfs_log_item *lip,
207 struct xfs_log_vec *lv)
209 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
210 struct xfs_log_iovec *vecp = NULL;
212 rudp->rud_format.rud_type = XFS_LI_RUD;
213 rudp->rud_format.rud_size = 1;
215 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUD_FORMAT, &rudp->rud_format,
216 sizeof(struct xfs_rud_log_format));
220 * The RUD is either committed or aborted if the transaction is cancelled. If
221 * the transaction is cancelled, drop our reference to the RUI and free the
225 xfs_rud_item_release(
226 struct xfs_log_item *lip)
228 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
230 xfs_rui_release(rudp->rud_ruip);
231 kmem_free(rudp->rud_item.li_lv_shadow);
232 kmem_cache_free(xfs_rud_zone, rudp);
235 static const struct xfs_item_ops xfs_rud_item_ops = {
236 .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED,
237 .iop_size = xfs_rud_item_size,
238 .iop_format = xfs_rud_item_format,
239 .iop_release = xfs_rud_item_release,
242 static struct xfs_rud_log_item *
244 struct xfs_trans *tp,
245 struct xfs_rui_log_item *ruip)
247 struct xfs_rud_log_item *rudp;
249 rudp = kmem_cache_zalloc(xfs_rud_zone, GFP_KERNEL | __GFP_NOFAIL);
250 xfs_log_item_init(tp->t_mountp, &rudp->rud_item, XFS_LI_RUD,
252 rudp->rud_ruip = ruip;
253 rudp->rud_format.rud_rui_id = ruip->rui_format.rui_id;
255 xfs_trans_add_item(tp, &rudp->rud_item);
259 /* Set the map extent flags for this reverse mapping. */
261 xfs_trans_set_rmap_flags(
262 struct xfs_map_extent *rmap,
263 enum xfs_rmap_intent_type type,
268 if (state == XFS_EXT_UNWRITTEN)
269 rmap->me_flags |= XFS_RMAP_EXTENT_UNWRITTEN;
270 if (whichfork == XFS_ATTR_FORK)
271 rmap->me_flags |= XFS_RMAP_EXTENT_ATTR_FORK;
274 rmap->me_flags |= XFS_RMAP_EXTENT_MAP;
276 case XFS_RMAP_MAP_SHARED:
277 rmap->me_flags |= XFS_RMAP_EXTENT_MAP_SHARED;
280 rmap->me_flags |= XFS_RMAP_EXTENT_UNMAP;
282 case XFS_RMAP_UNMAP_SHARED:
283 rmap->me_flags |= XFS_RMAP_EXTENT_UNMAP_SHARED;
285 case XFS_RMAP_CONVERT:
286 rmap->me_flags |= XFS_RMAP_EXTENT_CONVERT;
288 case XFS_RMAP_CONVERT_SHARED:
289 rmap->me_flags |= XFS_RMAP_EXTENT_CONVERT_SHARED;
292 rmap->me_flags |= XFS_RMAP_EXTENT_ALLOC;
295 rmap->me_flags |= XFS_RMAP_EXTENT_FREE;
303 * Finish an rmap update and log it to the RUD. Note that the transaction is
304 * marked dirty regardless of whether the rmap update succeeds or fails to
305 * support the RUI/RUD lifecycle rules.
308 xfs_trans_log_finish_rmap_update(
309 struct xfs_trans *tp,
310 struct xfs_rud_log_item *rudp,
311 enum xfs_rmap_intent_type type,
314 xfs_fileoff_t startoff,
315 xfs_fsblock_t startblock,
316 xfs_filblks_t blockcount,
318 struct xfs_btree_cur **pcur)
322 error = xfs_rmap_finish_one(tp, type, owner, whichfork, startoff,
323 startblock, blockcount, state, pcur);
326 * Mark the transaction dirty, even on error. This ensures the
327 * transaction is aborted, which:
329 * 1.) releases the RUI and frees the RUD
330 * 2.) shuts down the filesystem
332 tp->t_flags |= XFS_TRANS_DIRTY;
333 set_bit(XFS_LI_DIRTY, &rudp->rud_item.li_flags);
338 /* Sort rmap intents by AG. */
340 xfs_rmap_update_diff_items(
342 const struct list_head *a,
343 const struct list_head *b)
345 struct xfs_mount *mp = priv;
346 struct xfs_rmap_intent *ra;
347 struct xfs_rmap_intent *rb;
349 ra = container_of(a, struct xfs_rmap_intent, ri_list);
350 rb = container_of(b, struct xfs_rmap_intent, ri_list);
351 return XFS_FSB_TO_AGNO(mp, ra->ri_bmap.br_startblock) -
352 XFS_FSB_TO_AGNO(mp, rb->ri_bmap.br_startblock);
355 /* Log rmap updates in the intent item. */
357 xfs_rmap_update_log_item(
358 struct xfs_trans *tp,
359 struct xfs_rui_log_item *ruip,
360 struct xfs_rmap_intent *rmap)
363 struct xfs_map_extent *map;
365 tp->t_flags |= XFS_TRANS_DIRTY;
366 set_bit(XFS_LI_DIRTY, &ruip->rui_item.li_flags);
369 * atomic_inc_return gives us the value after the increment;
370 * we want to use it as an array index so we need to subtract 1 from
373 next_extent = atomic_inc_return(&ruip->rui_next_extent) - 1;
374 ASSERT(next_extent < ruip->rui_format.rui_nextents);
375 map = &ruip->rui_format.rui_extents[next_extent];
376 map->me_owner = rmap->ri_owner;
377 map->me_startblock = rmap->ri_bmap.br_startblock;
378 map->me_startoff = rmap->ri_bmap.br_startoff;
379 map->me_len = rmap->ri_bmap.br_blockcount;
380 xfs_trans_set_rmap_flags(map, rmap->ri_type, rmap->ri_whichfork,
381 rmap->ri_bmap.br_state);
384 static struct xfs_log_item *
385 xfs_rmap_update_create_intent(
386 struct xfs_trans *tp,
387 struct list_head *items,
391 struct xfs_mount *mp = tp->t_mountp;
392 struct xfs_rui_log_item *ruip = xfs_rui_init(mp, count);
393 struct xfs_rmap_intent *rmap;
397 xfs_trans_add_item(tp, &ruip->rui_item);
399 list_sort(mp, items, xfs_rmap_update_diff_items);
400 list_for_each_entry(rmap, items, ri_list)
401 xfs_rmap_update_log_item(tp, ruip, rmap);
402 return &ruip->rui_item;
405 /* Get an RUD so we can process all the deferred rmap updates. */
406 static struct xfs_log_item *
407 xfs_rmap_update_create_done(
408 struct xfs_trans *tp,
409 struct xfs_log_item *intent,
412 return &xfs_trans_get_rud(tp, RUI_ITEM(intent))->rud_item;
415 /* Process a deferred rmap update. */
417 xfs_rmap_update_finish_item(
418 struct xfs_trans *tp,
419 struct xfs_log_item *done,
420 struct list_head *item,
421 struct xfs_btree_cur **state)
423 struct xfs_rmap_intent *rmap;
426 rmap = container_of(item, struct xfs_rmap_intent, ri_list);
427 error = xfs_trans_log_finish_rmap_update(tp, RUD_ITEM(done),
428 rmap->ri_type, rmap->ri_owner, rmap->ri_whichfork,
429 rmap->ri_bmap.br_startoff, rmap->ri_bmap.br_startblock,
430 rmap->ri_bmap.br_blockcount, rmap->ri_bmap.br_state,
436 /* Abort all pending RUIs. */
438 xfs_rmap_update_abort_intent(
439 struct xfs_log_item *intent)
441 xfs_rui_release(RUI_ITEM(intent));
444 /* Cancel a deferred rmap update. */
446 xfs_rmap_update_cancel_item(
447 struct list_head *item)
449 struct xfs_rmap_intent *rmap;
451 rmap = container_of(item, struct xfs_rmap_intent, ri_list);
455 const struct xfs_defer_op_type xfs_rmap_update_defer_type = {
456 .max_items = XFS_RUI_MAX_FAST_EXTENTS,
457 .create_intent = xfs_rmap_update_create_intent,
458 .abort_intent = xfs_rmap_update_abort_intent,
459 .create_done = xfs_rmap_update_create_done,
460 .finish_item = xfs_rmap_update_finish_item,
461 .finish_cleanup = xfs_rmap_finish_one_cleanup,
462 .cancel_item = xfs_rmap_update_cancel_item,
465 /* Is this recovered RUI ok? */
467 xfs_rui_validate_map(
468 struct xfs_mount *mp,
469 struct xfs_map_extent *rmap)
471 if (!xfs_has_rmapbt(mp))
474 if (rmap->me_flags & ~XFS_RMAP_EXTENT_FLAGS)
477 switch (rmap->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
478 case XFS_RMAP_EXTENT_MAP:
479 case XFS_RMAP_EXTENT_MAP_SHARED:
480 case XFS_RMAP_EXTENT_UNMAP:
481 case XFS_RMAP_EXTENT_UNMAP_SHARED:
482 case XFS_RMAP_EXTENT_CONVERT:
483 case XFS_RMAP_EXTENT_CONVERT_SHARED:
484 case XFS_RMAP_EXTENT_ALLOC:
485 case XFS_RMAP_EXTENT_FREE:
491 if (!XFS_RMAP_NON_INODE_OWNER(rmap->me_owner) &&
492 !xfs_verify_ino(mp, rmap->me_owner))
495 if (!xfs_verify_fileext(mp, rmap->me_startoff, rmap->me_len))
498 return xfs_verify_fsbext(mp, rmap->me_startblock, rmap->me_len);
502 * Process an rmap update intent item that was recovered from the log.
503 * We need to update the rmapbt.
506 xfs_rui_item_recover(
507 struct xfs_log_item *lip,
508 struct list_head *capture_list)
510 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
511 struct xfs_map_extent *rmap;
512 struct xfs_rud_log_item *rudp;
513 struct xfs_trans *tp;
514 struct xfs_btree_cur *rcur = NULL;
515 struct xfs_mount *mp = lip->li_mountp;
516 enum xfs_rmap_intent_type type;
523 * First check the validity of the extents described by the
524 * RUI. If any are bad, then assume that all are bad and
527 for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
528 if (!xfs_rui_validate_map(mp,
529 &ruip->rui_format.rui_extents[i])) {
530 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
532 sizeof(ruip->rui_format));
533 return -EFSCORRUPTED;
537 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate,
538 mp->m_rmap_maxlevels, 0, XFS_TRANS_RESERVE, &tp);
541 rudp = xfs_trans_get_rud(tp, ruip);
543 for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
544 rmap = &ruip->rui_format.rui_extents[i];
545 state = (rmap->me_flags & XFS_RMAP_EXTENT_UNWRITTEN) ?
546 XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
547 whichfork = (rmap->me_flags & XFS_RMAP_EXTENT_ATTR_FORK) ?
548 XFS_ATTR_FORK : XFS_DATA_FORK;
549 switch (rmap->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
550 case XFS_RMAP_EXTENT_MAP:
553 case XFS_RMAP_EXTENT_MAP_SHARED:
554 type = XFS_RMAP_MAP_SHARED;
556 case XFS_RMAP_EXTENT_UNMAP:
557 type = XFS_RMAP_UNMAP;
559 case XFS_RMAP_EXTENT_UNMAP_SHARED:
560 type = XFS_RMAP_UNMAP_SHARED;
562 case XFS_RMAP_EXTENT_CONVERT:
563 type = XFS_RMAP_CONVERT;
565 case XFS_RMAP_EXTENT_CONVERT_SHARED:
566 type = XFS_RMAP_CONVERT_SHARED;
568 case XFS_RMAP_EXTENT_ALLOC:
569 type = XFS_RMAP_ALLOC;
571 case XFS_RMAP_EXTENT_FREE:
572 type = XFS_RMAP_FREE;
575 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL);
576 error = -EFSCORRUPTED;
579 error = xfs_trans_log_finish_rmap_update(tp, rudp, type,
580 rmap->me_owner, whichfork,
581 rmap->me_startoff, rmap->me_startblock,
582 rmap->me_len, state, &rcur);
583 if (error == -EFSCORRUPTED)
584 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
585 rmap, sizeof(*rmap));
591 xfs_rmap_finish_one_cleanup(tp, rcur, error);
592 return xfs_defer_ops_capture_and_commit(tp, NULL, capture_list);
595 xfs_rmap_finish_one_cleanup(tp, rcur, error);
596 xfs_trans_cancel(tp);
602 struct xfs_log_item *lip,
605 return RUI_ITEM(lip)->rui_format.rui_id == intent_id;
608 /* Relog an intent item to push the log tail forward. */
609 static struct xfs_log_item *
611 struct xfs_log_item *intent,
612 struct xfs_trans *tp)
614 struct xfs_rud_log_item *rudp;
615 struct xfs_rui_log_item *ruip;
616 struct xfs_map_extent *extp;
619 count = RUI_ITEM(intent)->rui_format.rui_nextents;
620 extp = RUI_ITEM(intent)->rui_format.rui_extents;
622 tp->t_flags |= XFS_TRANS_DIRTY;
623 rudp = xfs_trans_get_rud(tp, RUI_ITEM(intent));
624 set_bit(XFS_LI_DIRTY, &rudp->rud_item.li_flags);
626 ruip = xfs_rui_init(tp->t_mountp, count);
627 memcpy(ruip->rui_format.rui_extents, extp, count * sizeof(*extp));
628 atomic_set(&ruip->rui_next_extent, count);
629 xfs_trans_add_item(tp, &ruip->rui_item);
630 set_bit(XFS_LI_DIRTY, &ruip->rui_item.li_flags);
631 return &ruip->rui_item;
634 static const struct xfs_item_ops xfs_rui_item_ops = {
635 .iop_size = xfs_rui_item_size,
636 .iop_format = xfs_rui_item_format,
637 .iop_unpin = xfs_rui_item_unpin,
638 .iop_release = xfs_rui_item_release,
639 .iop_recover = xfs_rui_item_recover,
640 .iop_match = xfs_rui_item_match,
641 .iop_relog = xfs_rui_item_relog,
645 * This routine is called to create an in-core extent rmap update
646 * item from the rui format structure which was logged on disk.
647 * It allocates an in-core rui, copies the extents from the format
648 * structure into it, and adds the rui to the AIL with the given
652 xlog_recover_rui_commit_pass2(
654 struct list_head *buffer_list,
655 struct xlog_recover_item *item,
659 struct xfs_mount *mp = log->l_mp;
660 struct xfs_rui_log_item *ruip;
661 struct xfs_rui_log_format *rui_formatp;
663 rui_formatp = item->ri_buf[0].i_addr;
665 ruip = xfs_rui_init(mp, rui_formatp->rui_nextents);
666 error = xfs_rui_copy_format(&item->ri_buf[0], &ruip->rui_format);
668 xfs_rui_item_free(ruip);
671 atomic_set(&ruip->rui_next_extent, rui_formatp->rui_nextents);
673 * Insert the intent into the AIL directly and drop one reference so
674 * that finishing or canceling the work will drop the other.
676 xfs_trans_ail_insert(log->l_ailp, &ruip->rui_item, lsn);
677 xfs_rui_release(ruip);
681 const struct xlog_recover_item_ops xlog_rui_item_ops = {
682 .item_type = XFS_LI_RUI,
683 .commit_pass2 = xlog_recover_rui_commit_pass2,
687 * This routine is called when an RUD format structure is found in a committed
688 * transaction in the log. Its purpose is to cancel the corresponding RUI if it
689 * was still in the log. To do this it searches the AIL for the RUI with an id
690 * equal to that in the RUD format structure. If we find it we drop the RUD
691 * reference, which removes the RUI from the AIL and frees it.
694 xlog_recover_rud_commit_pass2(
696 struct list_head *buffer_list,
697 struct xlog_recover_item *item,
700 struct xfs_rud_log_format *rud_formatp;
702 rud_formatp = item->ri_buf[0].i_addr;
703 ASSERT(item->ri_buf[0].i_len == sizeof(struct xfs_rud_log_format));
705 xlog_recover_release_intent(log, XFS_LI_RUI, rud_formatp->rud_rui_id);
709 const struct xlog_recover_item_ops xlog_rud_item_ops = {
710 .item_type = XFS_LI_RUD,
711 .commit_pass2 = xlog_recover_rud_commit_pass2,