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 if (ruip->rui_format.rui_nextents > XFS_RUI_MAX_FAST_EXTENTS)
41 kmem_cache_free(xfs_rui_cache, ruip);
45 * Freeing the RUI requires that we remove it from the AIL if it has already
46 * been placed there. However, the RUI may not yet have been placed in the AIL
47 * when called by xfs_rui_release() from RUD processing due to the ordering of
48 * committed vs unpin operations in bulk insert operations. Hence the reference
49 * count to ensure only the last caller frees the RUI.
53 struct xfs_rui_log_item *ruip)
55 ASSERT(atomic_read(&ruip->rui_refcount) > 0);
56 if (atomic_dec_and_test(&ruip->rui_refcount)) {
57 xfs_trans_ail_delete(&ruip->rui_item, SHUTDOWN_LOG_IO_ERROR);
58 xfs_rui_item_free(ruip);
64 struct xfs_log_item *lip,
68 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
71 *nbytes += xfs_rui_log_format_sizeof(ruip->rui_format.rui_nextents);
75 * This is called to fill in the vector of log iovecs for the
76 * given rui log item. We use only 1 iovec, and we point that
77 * at the rui_log_format structure embedded in the rui item.
78 * It is at this point that we assert that all of the extent
79 * slots in the rui item have been filled.
83 struct xfs_log_item *lip,
84 struct xfs_log_vec *lv)
86 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
87 struct xfs_log_iovec *vecp = NULL;
89 ASSERT(atomic_read(&ruip->rui_next_extent) ==
90 ruip->rui_format.rui_nextents);
92 ruip->rui_format.rui_type = XFS_LI_RUI;
93 ruip->rui_format.rui_size = 1;
95 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUI_FORMAT, &ruip->rui_format,
96 xfs_rui_log_format_sizeof(ruip->rui_format.rui_nextents));
100 * The unpin operation is the last place an RUI is manipulated in the log. It is
101 * either inserted in the AIL or aborted in the event of a log I/O error. In
102 * either case, the RUI transaction has been successfully committed to make it
103 * this far. Therefore, we expect whoever committed the RUI to either construct
104 * and commit the RUD or drop the RUD's reference in the event of error. Simply
105 * drop the log's RUI reference now that the log is done with it.
109 struct xfs_log_item *lip,
112 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
114 xfs_rui_release(ruip);
118 * The RUI has been either committed or aborted if the transaction has been
119 * cancelled. If the transaction was cancelled, an RUD isn't going to be
120 * constructed and thus we free the RUI here directly.
123 xfs_rui_item_release(
124 struct xfs_log_item *lip)
126 xfs_rui_release(RUI_ITEM(lip));
130 * Allocate and initialize an rui item with the given number of extents.
132 STATIC struct xfs_rui_log_item *
134 struct xfs_mount *mp,
138 struct xfs_rui_log_item *ruip;
140 ASSERT(nextents > 0);
141 if (nextents > XFS_RUI_MAX_FAST_EXTENTS)
142 ruip = kmem_zalloc(xfs_rui_log_item_sizeof(nextents), 0);
144 ruip = kmem_cache_zalloc(xfs_rui_cache,
145 GFP_KERNEL | __GFP_NOFAIL);
147 xfs_log_item_init(mp, &ruip->rui_item, XFS_LI_RUI, &xfs_rui_item_ops);
148 ruip->rui_format.rui_nextents = nextents;
149 ruip->rui_format.rui_id = (uintptr_t)(void *)ruip;
150 atomic_set(&ruip->rui_next_extent, 0);
151 atomic_set(&ruip->rui_refcount, 2);
157 * Copy an RUI format buffer from the given buf, and into the destination
158 * RUI format structure. The RUI/RUD items were designed not to need any
159 * special alignment handling.
163 struct xfs_log_iovec *buf,
164 struct xfs_rui_log_format *dst_rui_fmt)
166 struct xfs_rui_log_format *src_rui_fmt;
169 src_rui_fmt = buf->i_addr;
170 len = xfs_rui_log_format_sizeof(src_rui_fmt->rui_nextents);
172 if (buf->i_len != len) {
173 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL);
174 return -EFSCORRUPTED;
177 memcpy(dst_rui_fmt, src_rui_fmt, len);
181 static inline struct xfs_rud_log_item *RUD_ITEM(struct xfs_log_item *lip)
183 return container_of(lip, struct xfs_rud_log_item, rud_item);
188 struct xfs_log_item *lip,
193 *nbytes += sizeof(struct xfs_rud_log_format);
197 * This is called to fill in the vector of log iovecs for the
198 * given rud log item. We use only 1 iovec, and we point that
199 * at the rud_log_format structure embedded in the rud item.
200 * It is at this point that we assert that all of the extent
201 * slots in the rud item have been filled.
205 struct xfs_log_item *lip,
206 struct xfs_log_vec *lv)
208 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
209 struct xfs_log_iovec *vecp = NULL;
211 rudp->rud_format.rud_type = XFS_LI_RUD;
212 rudp->rud_format.rud_size = 1;
214 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUD_FORMAT, &rudp->rud_format,
215 sizeof(struct xfs_rud_log_format));
219 * The RUD is either committed or aborted if the transaction is cancelled. If
220 * the transaction is cancelled, drop our reference to the RUI and free the
224 xfs_rud_item_release(
225 struct xfs_log_item *lip)
227 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
229 xfs_rui_release(rudp->rud_ruip);
230 kmem_cache_free(xfs_rud_cache, rudp);
233 static const struct xfs_item_ops xfs_rud_item_ops = {
234 .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED,
235 .iop_size = xfs_rud_item_size,
236 .iop_format = xfs_rud_item_format,
237 .iop_release = xfs_rud_item_release,
240 static struct xfs_rud_log_item *
242 struct xfs_trans *tp,
243 struct xfs_rui_log_item *ruip)
245 struct xfs_rud_log_item *rudp;
247 rudp = kmem_cache_zalloc(xfs_rud_cache, GFP_KERNEL | __GFP_NOFAIL);
248 xfs_log_item_init(tp->t_mountp, &rudp->rud_item, XFS_LI_RUD,
250 rudp->rud_ruip = ruip;
251 rudp->rud_format.rud_rui_id = ruip->rui_format.rui_id;
253 xfs_trans_add_item(tp, &rudp->rud_item);
257 /* Set the map extent flags for this reverse mapping. */
259 xfs_trans_set_rmap_flags(
260 struct xfs_map_extent *rmap,
261 enum xfs_rmap_intent_type type,
266 if (state == XFS_EXT_UNWRITTEN)
267 rmap->me_flags |= XFS_RMAP_EXTENT_UNWRITTEN;
268 if (whichfork == XFS_ATTR_FORK)
269 rmap->me_flags |= XFS_RMAP_EXTENT_ATTR_FORK;
272 rmap->me_flags |= XFS_RMAP_EXTENT_MAP;
274 case XFS_RMAP_MAP_SHARED:
275 rmap->me_flags |= XFS_RMAP_EXTENT_MAP_SHARED;
278 rmap->me_flags |= XFS_RMAP_EXTENT_UNMAP;
280 case XFS_RMAP_UNMAP_SHARED:
281 rmap->me_flags |= XFS_RMAP_EXTENT_UNMAP_SHARED;
283 case XFS_RMAP_CONVERT:
284 rmap->me_flags |= XFS_RMAP_EXTENT_CONVERT;
286 case XFS_RMAP_CONVERT_SHARED:
287 rmap->me_flags |= XFS_RMAP_EXTENT_CONVERT_SHARED;
290 rmap->me_flags |= XFS_RMAP_EXTENT_ALLOC;
293 rmap->me_flags |= XFS_RMAP_EXTENT_FREE;
301 * Finish an rmap update and log it to the RUD. Note that the transaction is
302 * marked dirty regardless of whether the rmap update succeeds or fails to
303 * support the RUI/RUD lifecycle rules.
306 xfs_trans_log_finish_rmap_update(
307 struct xfs_trans *tp,
308 struct xfs_rud_log_item *rudp,
309 enum xfs_rmap_intent_type type,
312 xfs_fileoff_t startoff,
313 xfs_fsblock_t startblock,
314 xfs_filblks_t blockcount,
316 struct xfs_btree_cur **pcur)
320 error = xfs_rmap_finish_one(tp, type, owner, whichfork, startoff,
321 startblock, blockcount, state, pcur);
324 * Mark the transaction dirty, even on error. This ensures the
325 * transaction is aborted, which:
327 * 1.) releases the RUI and frees the RUD
328 * 2.) shuts down the filesystem
330 tp->t_flags |= XFS_TRANS_DIRTY;
331 set_bit(XFS_LI_DIRTY, &rudp->rud_item.li_flags);
336 /* Sort rmap intents by AG. */
338 xfs_rmap_update_diff_items(
340 const struct list_head *a,
341 const struct list_head *b)
343 struct xfs_mount *mp = priv;
344 struct xfs_rmap_intent *ra;
345 struct xfs_rmap_intent *rb;
347 ra = container_of(a, struct xfs_rmap_intent, ri_list);
348 rb = container_of(b, struct xfs_rmap_intent, ri_list);
349 return XFS_FSB_TO_AGNO(mp, ra->ri_bmap.br_startblock) -
350 XFS_FSB_TO_AGNO(mp, rb->ri_bmap.br_startblock);
353 /* Log rmap updates in the intent item. */
355 xfs_rmap_update_log_item(
356 struct xfs_trans *tp,
357 struct xfs_rui_log_item *ruip,
358 struct xfs_rmap_intent *rmap)
361 struct xfs_map_extent *map;
363 tp->t_flags |= XFS_TRANS_DIRTY;
364 set_bit(XFS_LI_DIRTY, &ruip->rui_item.li_flags);
367 * atomic_inc_return gives us the value after the increment;
368 * we want to use it as an array index so we need to subtract 1 from
371 next_extent = atomic_inc_return(&ruip->rui_next_extent) - 1;
372 ASSERT(next_extent < ruip->rui_format.rui_nextents);
373 map = &ruip->rui_format.rui_extents[next_extent];
374 map->me_owner = rmap->ri_owner;
375 map->me_startblock = rmap->ri_bmap.br_startblock;
376 map->me_startoff = rmap->ri_bmap.br_startoff;
377 map->me_len = rmap->ri_bmap.br_blockcount;
378 xfs_trans_set_rmap_flags(map, rmap->ri_type, rmap->ri_whichfork,
379 rmap->ri_bmap.br_state);
382 static struct xfs_log_item *
383 xfs_rmap_update_create_intent(
384 struct xfs_trans *tp,
385 struct list_head *items,
389 struct xfs_mount *mp = tp->t_mountp;
390 struct xfs_rui_log_item *ruip = xfs_rui_init(mp, count);
391 struct xfs_rmap_intent *rmap;
395 xfs_trans_add_item(tp, &ruip->rui_item);
397 list_sort(mp, items, xfs_rmap_update_diff_items);
398 list_for_each_entry(rmap, items, ri_list)
399 xfs_rmap_update_log_item(tp, ruip, rmap);
400 return &ruip->rui_item;
403 /* Get an RUD so we can process all the deferred rmap updates. */
404 static struct xfs_log_item *
405 xfs_rmap_update_create_done(
406 struct xfs_trans *tp,
407 struct xfs_log_item *intent,
410 return &xfs_trans_get_rud(tp, RUI_ITEM(intent))->rud_item;
413 /* Process a deferred rmap update. */
415 xfs_rmap_update_finish_item(
416 struct xfs_trans *tp,
417 struct xfs_log_item *done,
418 struct list_head *item,
419 struct xfs_btree_cur **state)
421 struct xfs_rmap_intent *rmap;
424 rmap = container_of(item, struct xfs_rmap_intent, ri_list);
425 error = xfs_trans_log_finish_rmap_update(tp, RUD_ITEM(done),
426 rmap->ri_type, rmap->ri_owner, rmap->ri_whichfork,
427 rmap->ri_bmap.br_startoff, rmap->ri_bmap.br_startblock,
428 rmap->ri_bmap.br_blockcount, rmap->ri_bmap.br_state,
430 kmem_cache_free(xfs_rmap_intent_cache, rmap);
434 /* Abort all pending RUIs. */
436 xfs_rmap_update_abort_intent(
437 struct xfs_log_item *intent)
439 xfs_rui_release(RUI_ITEM(intent));
442 /* Cancel a deferred rmap update. */
444 xfs_rmap_update_cancel_item(
445 struct list_head *item)
447 struct xfs_rmap_intent *rmap;
449 rmap = container_of(item, struct xfs_rmap_intent, ri_list);
450 kmem_cache_free(xfs_rmap_intent_cache, rmap);
453 const struct xfs_defer_op_type xfs_rmap_update_defer_type = {
454 .max_items = XFS_RUI_MAX_FAST_EXTENTS,
455 .create_intent = xfs_rmap_update_create_intent,
456 .abort_intent = xfs_rmap_update_abort_intent,
457 .create_done = xfs_rmap_update_create_done,
458 .finish_item = xfs_rmap_update_finish_item,
459 .finish_cleanup = xfs_rmap_finish_one_cleanup,
460 .cancel_item = xfs_rmap_update_cancel_item,
463 /* Is this recovered RUI ok? */
465 xfs_rui_validate_map(
466 struct xfs_mount *mp,
467 struct xfs_map_extent *rmap)
469 if (!xfs_has_rmapbt(mp))
472 if (rmap->me_flags & ~XFS_RMAP_EXTENT_FLAGS)
475 switch (rmap->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
476 case XFS_RMAP_EXTENT_MAP:
477 case XFS_RMAP_EXTENT_MAP_SHARED:
478 case XFS_RMAP_EXTENT_UNMAP:
479 case XFS_RMAP_EXTENT_UNMAP_SHARED:
480 case XFS_RMAP_EXTENT_CONVERT:
481 case XFS_RMAP_EXTENT_CONVERT_SHARED:
482 case XFS_RMAP_EXTENT_ALLOC:
483 case XFS_RMAP_EXTENT_FREE:
489 if (!XFS_RMAP_NON_INODE_OWNER(rmap->me_owner) &&
490 !xfs_verify_ino(mp, rmap->me_owner))
493 if (!xfs_verify_fileext(mp, rmap->me_startoff, rmap->me_len))
496 return xfs_verify_fsbext(mp, rmap->me_startblock, rmap->me_len);
500 * Process an rmap update intent item that was recovered from the log.
501 * We need to update the rmapbt.
504 xfs_rui_item_recover(
505 struct xfs_log_item *lip,
506 struct list_head *capture_list)
508 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
509 struct xfs_map_extent *rmap;
510 struct xfs_rud_log_item *rudp;
511 struct xfs_trans *tp;
512 struct xfs_btree_cur *rcur = NULL;
513 struct xfs_mount *mp = lip->li_log->l_mp;
514 enum xfs_rmap_intent_type type;
521 * First check the validity of the extents described by the
522 * RUI. If any are bad, then assume that all are bad and
525 for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
526 if (!xfs_rui_validate_map(mp,
527 &ruip->rui_format.rui_extents[i])) {
528 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
530 sizeof(ruip->rui_format));
531 return -EFSCORRUPTED;
535 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate,
536 mp->m_rmap_maxlevels, 0, XFS_TRANS_RESERVE, &tp);
539 rudp = xfs_trans_get_rud(tp, ruip);
541 for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
542 rmap = &ruip->rui_format.rui_extents[i];
543 state = (rmap->me_flags & XFS_RMAP_EXTENT_UNWRITTEN) ?
544 XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
545 whichfork = (rmap->me_flags & XFS_RMAP_EXTENT_ATTR_FORK) ?
546 XFS_ATTR_FORK : XFS_DATA_FORK;
547 switch (rmap->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
548 case XFS_RMAP_EXTENT_MAP:
551 case XFS_RMAP_EXTENT_MAP_SHARED:
552 type = XFS_RMAP_MAP_SHARED;
554 case XFS_RMAP_EXTENT_UNMAP:
555 type = XFS_RMAP_UNMAP;
557 case XFS_RMAP_EXTENT_UNMAP_SHARED:
558 type = XFS_RMAP_UNMAP_SHARED;
560 case XFS_RMAP_EXTENT_CONVERT:
561 type = XFS_RMAP_CONVERT;
563 case XFS_RMAP_EXTENT_CONVERT_SHARED:
564 type = XFS_RMAP_CONVERT_SHARED;
566 case XFS_RMAP_EXTENT_ALLOC:
567 type = XFS_RMAP_ALLOC;
569 case XFS_RMAP_EXTENT_FREE:
570 type = XFS_RMAP_FREE;
573 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL);
574 error = -EFSCORRUPTED;
577 error = xfs_trans_log_finish_rmap_update(tp, rudp, type,
578 rmap->me_owner, whichfork,
579 rmap->me_startoff, rmap->me_startblock,
580 rmap->me_len, state, &rcur);
581 if (error == -EFSCORRUPTED)
582 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
583 rmap, sizeof(*rmap));
589 xfs_rmap_finish_one_cleanup(tp, rcur, error);
590 return xfs_defer_ops_capture_and_commit(tp, capture_list);
593 xfs_rmap_finish_one_cleanup(tp, rcur, error);
594 xfs_trans_cancel(tp);
600 struct xfs_log_item *lip,
603 return RUI_ITEM(lip)->rui_format.rui_id == intent_id;
606 /* Relog an intent item to push the log tail forward. */
607 static struct xfs_log_item *
609 struct xfs_log_item *intent,
610 struct xfs_trans *tp)
612 struct xfs_rud_log_item *rudp;
613 struct xfs_rui_log_item *ruip;
614 struct xfs_map_extent *extp;
617 count = RUI_ITEM(intent)->rui_format.rui_nextents;
618 extp = RUI_ITEM(intent)->rui_format.rui_extents;
620 tp->t_flags |= XFS_TRANS_DIRTY;
621 rudp = xfs_trans_get_rud(tp, RUI_ITEM(intent));
622 set_bit(XFS_LI_DIRTY, &rudp->rud_item.li_flags);
624 ruip = xfs_rui_init(tp->t_mountp, count);
625 memcpy(ruip->rui_format.rui_extents, extp, count * sizeof(*extp));
626 atomic_set(&ruip->rui_next_extent, count);
627 xfs_trans_add_item(tp, &ruip->rui_item);
628 set_bit(XFS_LI_DIRTY, &ruip->rui_item.li_flags);
629 return &ruip->rui_item;
632 static const struct xfs_item_ops xfs_rui_item_ops = {
633 .iop_size = xfs_rui_item_size,
634 .iop_format = xfs_rui_item_format,
635 .iop_unpin = xfs_rui_item_unpin,
636 .iop_release = xfs_rui_item_release,
637 .iop_recover = xfs_rui_item_recover,
638 .iop_match = xfs_rui_item_match,
639 .iop_relog = xfs_rui_item_relog,
643 * This routine is called to create an in-core extent rmap update
644 * item from the rui format structure which was logged on disk.
645 * It allocates an in-core rui, copies the extents from the format
646 * structure into it, and adds the rui to the AIL with the given
650 xlog_recover_rui_commit_pass2(
652 struct list_head *buffer_list,
653 struct xlog_recover_item *item,
657 struct xfs_mount *mp = log->l_mp;
658 struct xfs_rui_log_item *ruip;
659 struct xfs_rui_log_format *rui_formatp;
661 rui_formatp = item->ri_buf[0].i_addr;
663 ruip = xfs_rui_init(mp, rui_formatp->rui_nextents);
664 error = xfs_rui_copy_format(&item->ri_buf[0], &ruip->rui_format);
666 xfs_rui_item_free(ruip);
669 atomic_set(&ruip->rui_next_extent, rui_formatp->rui_nextents);
671 * Insert the intent into the AIL directly and drop one reference so
672 * that finishing or canceling the work will drop the other.
674 xfs_trans_ail_insert(log->l_ailp, &ruip->rui_item, lsn);
675 xfs_rui_release(ruip);
679 const struct xlog_recover_item_ops xlog_rui_item_ops = {
680 .item_type = XFS_LI_RUI,
681 .commit_pass2 = xlog_recover_rui_commit_pass2,
685 * This routine is called when an RUD format structure is found in a committed
686 * transaction in the log. Its purpose is to cancel the corresponding RUI if it
687 * was still in the log. To do this it searches the AIL for the RUI with an id
688 * equal to that in the RUD format structure. If we find it we drop the RUD
689 * reference, which removes the RUI from the AIL and frees it.
692 xlog_recover_rud_commit_pass2(
694 struct list_head *buffer_list,
695 struct xlog_recover_item *item,
698 struct xfs_rud_log_format *rud_formatp;
700 rud_formatp = item->ri_buf[0].i_addr;
701 ASSERT(item->ri_buf[0].i_len == sizeof(struct xfs_rud_log_format));
703 xlog_recover_release_intent(log, XFS_LI_RUI, rud_formatp->rud_rui_id);
707 const struct xlog_recover_item_ops xlog_rud_item_ops = {
708 .item_type = XFS_LI_RUD,
709 .commit_pass2 = xlog_recover_rud_commit_pass2,