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 if (ruip->rui_format.rui_nextents > XFS_RUI_MAX_FAST_EXTENTS)
41 kmem_cache_free(xfs_rui_zone, 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_zone_zalloc(xfs_rui_zone, 0);
146 xfs_log_item_init(mp, &ruip->rui_item, XFS_LI_RUI, &xfs_rui_item_ops);
147 ruip->rui_format.rui_nextents = nextents;
148 ruip->rui_format.rui_id = (uintptr_t)(void *)ruip;
149 atomic_set(&ruip->rui_next_extent, 0);
150 atomic_set(&ruip->rui_refcount, 2);
156 * Copy an RUI format buffer from the given buf, and into the destination
157 * RUI format structure. The RUI/RUD items were designed not to need any
158 * special alignment handling.
162 struct xfs_log_iovec *buf,
163 struct xfs_rui_log_format *dst_rui_fmt)
165 struct xfs_rui_log_format *src_rui_fmt;
168 src_rui_fmt = buf->i_addr;
169 len = xfs_rui_log_format_sizeof(src_rui_fmt->rui_nextents);
171 if (buf->i_len != len) {
172 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL);
173 return -EFSCORRUPTED;
176 memcpy(dst_rui_fmt, src_rui_fmt, len);
180 static inline struct xfs_rud_log_item *RUD_ITEM(struct xfs_log_item *lip)
182 return container_of(lip, struct xfs_rud_log_item, rud_item);
187 struct xfs_log_item *lip,
192 *nbytes += sizeof(struct xfs_rud_log_format);
196 * This is called to fill in the vector of log iovecs for the
197 * given rud log item. We use only 1 iovec, and we point that
198 * at the rud_log_format structure embedded in the rud item.
199 * It is at this point that we assert that all of the extent
200 * slots in the rud item have been filled.
204 struct xfs_log_item *lip,
205 struct xfs_log_vec *lv)
207 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
208 struct xfs_log_iovec *vecp = NULL;
210 rudp->rud_format.rud_type = XFS_LI_RUD;
211 rudp->rud_format.rud_size = 1;
213 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUD_FORMAT, &rudp->rud_format,
214 sizeof(struct xfs_rud_log_format));
218 * The RUD is either committed or aborted if the transaction is cancelled. If
219 * the transaction is cancelled, drop our reference to the RUI and free the
223 xfs_rud_item_release(
224 struct xfs_log_item *lip)
226 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
228 xfs_rui_release(rudp->rud_ruip);
229 kmem_cache_free(xfs_rud_zone, rudp);
232 static const struct xfs_item_ops xfs_rud_item_ops = {
233 .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED,
234 .iop_size = xfs_rud_item_size,
235 .iop_format = xfs_rud_item_format,
236 .iop_release = xfs_rud_item_release,
239 static struct xfs_rud_log_item *
241 struct xfs_trans *tp,
242 struct xfs_rui_log_item *ruip)
244 struct xfs_rud_log_item *rudp;
246 rudp = kmem_zone_zalloc(xfs_rud_zone, 0);
247 xfs_log_item_init(tp->t_mountp, &rudp->rud_item, XFS_LI_RUD,
249 rudp->rud_ruip = ruip;
250 rudp->rud_format.rud_rui_id = ruip->rui_format.rui_id;
252 xfs_trans_add_item(tp, &rudp->rud_item);
256 /* Set the map extent flags for this reverse mapping. */
258 xfs_trans_set_rmap_flags(
259 struct xfs_map_extent *rmap,
260 enum xfs_rmap_intent_type type,
265 if (state == XFS_EXT_UNWRITTEN)
266 rmap->me_flags |= XFS_RMAP_EXTENT_UNWRITTEN;
267 if (whichfork == XFS_ATTR_FORK)
268 rmap->me_flags |= XFS_RMAP_EXTENT_ATTR_FORK;
271 rmap->me_flags |= XFS_RMAP_EXTENT_MAP;
273 case XFS_RMAP_MAP_SHARED:
274 rmap->me_flags |= XFS_RMAP_EXTENT_MAP_SHARED;
277 rmap->me_flags |= XFS_RMAP_EXTENT_UNMAP;
279 case XFS_RMAP_UNMAP_SHARED:
280 rmap->me_flags |= XFS_RMAP_EXTENT_UNMAP_SHARED;
282 case XFS_RMAP_CONVERT:
283 rmap->me_flags |= XFS_RMAP_EXTENT_CONVERT;
285 case XFS_RMAP_CONVERT_SHARED:
286 rmap->me_flags |= XFS_RMAP_EXTENT_CONVERT_SHARED;
289 rmap->me_flags |= XFS_RMAP_EXTENT_ALLOC;
292 rmap->me_flags |= XFS_RMAP_EXTENT_FREE;
300 * Finish an rmap update and log it to the RUD. Note that the transaction is
301 * marked dirty regardless of whether the rmap update succeeds or fails to
302 * support the RUI/RUD lifecycle rules.
305 xfs_trans_log_finish_rmap_update(
306 struct xfs_trans *tp,
307 struct xfs_rud_log_item *rudp,
308 enum xfs_rmap_intent_type type,
311 xfs_fileoff_t startoff,
312 xfs_fsblock_t startblock,
313 xfs_filblks_t blockcount,
315 struct xfs_btree_cur **pcur)
319 error = xfs_rmap_finish_one(tp, type, owner, whichfork, startoff,
320 startblock, blockcount, state, pcur);
323 * Mark the transaction dirty, even on error. This ensures the
324 * transaction is aborted, which:
326 * 1.) releases the RUI and frees the RUD
327 * 2.) shuts down the filesystem
329 tp->t_flags |= XFS_TRANS_DIRTY;
330 set_bit(XFS_LI_DIRTY, &rudp->rud_item.li_flags);
335 /* Sort rmap intents by AG. */
337 xfs_rmap_update_diff_items(
342 struct xfs_mount *mp = priv;
343 struct xfs_rmap_intent *ra;
344 struct xfs_rmap_intent *rb;
346 ra = container_of(a, struct xfs_rmap_intent, ri_list);
347 rb = container_of(b, struct xfs_rmap_intent, ri_list);
348 return XFS_FSB_TO_AGNO(mp, ra->ri_bmap.br_startblock) -
349 XFS_FSB_TO_AGNO(mp, rb->ri_bmap.br_startblock);
352 /* Log rmap updates in the intent item. */
354 xfs_rmap_update_log_item(
355 struct xfs_trans *tp,
356 struct xfs_rui_log_item *ruip,
357 struct xfs_rmap_intent *rmap)
360 struct xfs_map_extent *map;
362 tp->t_flags |= XFS_TRANS_DIRTY;
363 set_bit(XFS_LI_DIRTY, &ruip->rui_item.li_flags);
366 * atomic_inc_return gives us the value after the increment;
367 * we want to use it as an array index so we need to subtract 1 from
370 next_extent = atomic_inc_return(&ruip->rui_next_extent) - 1;
371 ASSERT(next_extent < ruip->rui_format.rui_nextents);
372 map = &ruip->rui_format.rui_extents[next_extent];
373 map->me_owner = rmap->ri_owner;
374 map->me_startblock = rmap->ri_bmap.br_startblock;
375 map->me_startoff = rmap->ri_bmap.br_startoff;
376 map->me_len = rmap->ri_bmap.br_blockcount;
377 xfs_trans_set_rmap_flags(map, rmap->ri_type, rmap->ri_whichfork,
378 rmap->ri_bmap.br_state);
381 static struct xfs_log_item *
382 xfs_rmap_update_create_intent(
383 struct xfs_trans *tp,
384 struct list_head *items,
388 struct xfs_mount *mp = tp->t_mountp;
389 struct xfs_rui_log_item *ruip = xfs_rui_init(mp, count);
390 struct xfs_rmap_intent *rmap;
394 xfs_trans_add_item(tp, &ruip->rui_item);
396 list_sort(mp, items, xfs_rmap_update_diff_items);
397 list_for_each_entry(rmap, items, ri_list)
398 xfs_rmap_update_log_item(tp, ruip, rmap);
399 return &ruip->rui_item;
402 /* Get an RUD so we can process all the deferred rmap updates. */
403 static struct xfs_log_item *
404 xfs_rmap_update_create_done(
405 struct xfs_trans *tp,
406 struct xfs_log_item *intent,
409 return &xfs_trans_get_rud(tp, RUI_ITEM(intent))->rud_item;
412 /* Process a deferred rmap update. */
414 xfs_rmap_update_finish_item(
415 struct xfs_trans *tp,
416 struct xfs_log_item *done,
417 struct list_head *item,
418 struct xfs_btree_cur **state)
420 struct xfs_rmap_intent *rmap;
423 rmap = container_of(item, struct xfs_rmap_intent, ri_list);
424 error = xfs_trans_log_finish_rmap_update(tp, RUD_ITEM(done),
425 rmap->ri_type, rmap->ri_owner, rmap->ri_whichfork,
426 rmap->ri_bmap.br_startoff, rmap->ri_bmap.br_startblock,
427 rmap->ri_bmap.br_blockcount, rmap->ri_bmap.br_state,
433 /* Abort all pending RUIs. */
435 xfs_rmap_update_abort_intent(
436 struct xfs_log_item *intent)
438 xfs_rui_release(RUI_ITEM(intent));
441 /* Cancel a deferred rmap update. */
443 xfs_rmap_update_cancel_item(
444 struct list_head *item)
446 struct xfs_rmap_intent *rmap;
448 rmap = container_of(item, struct xfs_rmap_intent, ri_list);
452 const struct xfs_defer_op_type xfs_rmap_update_defer_type = {
453 .max_items = XFS_RUI_MAX_FAST_EXTENTS,
454 .create_intent = xfs_rmap_update_create_intent,
455 .abort_intent = xfs_rmap_update_abort_intent,
456 .create_done = xfs_rmap_update_create_done,
457 .finish_item = xfs_rmap_update_finish_item,
458 .finish_cleanup = xfs_rmap_finish_one_cleanup,
459 .cancel_item = xfs_rmap_update_cancel_item,
463 * Process an rmap update intent item that was recovered from the log.
464 * We need to update the rmapbt.
467 xfs_rui_item_recover(
468 struct xfs_log_item *lip,
469 struct xfs_trans *parent_tp)
471 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
472 struct xfs_map_extent *rmap;
473 struct xfs_rud_log_item *rudp;
474 struct xfs_trans *tp;
475 struct xfs_btree_cur *rcur = NULL;
476 struct xfs_mount *mp = parent_tp->t_mountp;
477 xfs_fsblock_t startblock_fsb;
478 enum xfs_rmap_intent_type type;
486 * First check the validity of the extents described by the
487 * RUI. If any are bad, then assume that all are bad and
490 for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
491 rmap = &ruip->rui_format.rui_extents[i];
492 startblock_fsb = XFS_BB_TO_FSB(mp,
493 XFS_FSB_TO_DADDR(mp, rmap->me_startblock));
494 switch (rmap->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
495 case XFS_RMAP_EXTENT_MAP:
496 case XFS_RMAP_EXTENT_MAP_SHARED:
497 case XFS_RMAP_EXTENT_UNMAP:
498 case XFS_RMAP_EXTENT_UNMAP_SHARED:
499 case XFS_RMAP_EXTENT_CONVERT:
500 case XFS_RMAP_EXTENT_CONVERT_SHARED:
501 case XFS_RMAP_EXTENT_ALLOC:
502 case XFS_RMAP_EXTENT_FREE:
509 if (!op_ok || startblock_fsb == 0 ||
511 startblock_fsb >= mp->m_sb.sb_dblocks ||
512 rmap->me_len >= mp->m_sb.sb_agblocks ||
513 (rmap->me_flags & ~XFS_RMAP_EXTENT_FLAGS)) {
515 * This will pull the RUI from the AIL and
516 * free the memory associated with it.
518 xfs_rui_release(ruip);
519 return -EFSCORRUPTED;
523 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate,
524 mp->m_rmap_maxlevels, 0, XFS_TRANS_RESERVE, &tp);
527 rudp = xfs_trans_get_rud(tp, ruip);
529 for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
530 rmap = &ruip->rui_format.rui_extents[i];
531 state = (rmap->me_flags & XFS_RMAP_EXTENT_UNWRITTEN) ?
532 XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
533 whichfork = (rmap->me_flags & XFS_RMAP_EXTENT_ATTR_FORK) ?
534 XFS_ATTR_FORK : XFS_DATA_FORK;
535 switch (rmap->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
536 case XFS_RMAP_EXTENT_MAP:
539 case XFS_RMAP_EXTENT_MAP_SHARED:
540 type = XFS_RMAP_MAP_SHARED;
542 case XFS_RMAP_EXTENT_UNMAP:
543 type = XFS_RMAP_UNMAP;
545 case XFS_RMAP_EXTENT_UNMAP_SHARED:
546 type = XFS_RMAP_UNMAP_SHARED;
548 case XFS_RMAP_EXTENT_CONVERT:
549 type = XFS_RMAP_CONVERT;
551 case XFS_RMAP_EXTENT_CONVERT_SHARED:
552 type = XFS_RMAP_CONVERT_SHARED;
554 case XFS_RMAP_EXTENT_ALLOC:
555 type = XFS_RMAP_ALLOC;
557 case XFS_RMAP_EXTENT_FREE:
558 type = XFS_RMAP_FREE;
561 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL);
562 error = -EFSCORRUPTED;
565 error = xfs_trans_log_finish_rmap_update(tp, rudp, type,
566 rmap->me_owner, whichfork,
567 rmap->me_startoff, rmap->me_startblock,
568 rmap->me_len, state, &rcur);
574 xfs_rmap_finish_one_cleanup(tp, rcur, error);
575 error = xfs_trans_commit(tp);
579 xfs_rmap_finish_one_cleanup(tp, rcur, error);
580 xfs_trans_cancel(tp);
586 struct xfs_log_item *lip,
589 return RUI_ITEM(lip)->rui_format.rui_id == intent_id;
592 static const struct xfs_item_ops xfs_rui_item_ops = {
593 .iop_size = xfs_rui_item_size,
594 .iop_format = xfs_rui_item_format,
595 .iop_unpin = xfs_rui_item_unpin,
596 .iop_release = xfs_rui_item_release,
597 .iop_recover = xfs_rui_item_recover,
598 .iop_match = xfs_rui_item_match,
602 * This routine is called to create an in-core extent rmap update
603 * item from the rui format structure which was logged on disk.
604 * It allocates an in-core rui, copies the extents from the format
605 * structure into it, and adds the rui to the AIL with the given
609 xlog_recover_rui_commit_pass2(
611 struct list_head *buffer_list,
612 struct xlog_recover_item *item,
616 struct xfs_mount *mp = log->l_mp;
617 struct xfs_rui_log_item *ruip;
618 struct xfs_rui_log_format *rui_formatp;
620 rui_formatp = item->ri_buf[0].i_addr;
622 ruip = xfs_rui_init(mp, rui_formatp->rui_nextents);
623 error = xfs_rui_copy_format(&item->ri_buf[0], &ruip->rui_format);
625 xfs_rui_item_free(ruip);
628 atomic_set(&ruip->rui_next_extent, rui_formatp->rui_nextents);
630 * Insert the intent into the AIL directly and drop one reference so
631 * that finishing or canceling the work will drop the other.
633 xfs_trans_ail_insert(log->l_ailp, &ruip->rui_item, lsn);
634 xfs_rui_release(ruip);
638 const struct xlog_recover_item_ops xlog_rui_item_ops = {
639 .item_type = XFS_LI_RUI,
640 .commit_pass2 = xlog_recover_rui_commit_pass2,
644 * This routine is called when an RUD format structure is found in a committed
645 * transaction in the log. Its purpose is to cancel the corresponding RUI if it
646 * was still in the log. To do this it searches the AIL for the RUI with an id
647 * equal to that in the RUD format structure. If we find it we drop the RUD
648 * reference, which removes the RUI from the AIL and frees it.
651 xlog_recover_rud_commit_pass2(
653 struct list_head *buffer_list,
654 struct xlog_recover_item *item,
657 struct xfs_rud_log_format *rud_formatp;
659 rud_formatp = item->ri_buf[0].i_addr;
660 ASSERT(item->ri_buf[0].i_len == sizeof(struct xfs_rud_log_format));
662 xlog_recover_release_intent(log, XFS_LI_RUI, rud_formatp->rud_rui_id);
666 const struct xlog_recover_item_ops xlog_rud_item_ops = {
667 .item_type = XFS_LI_RUD,
668 .commit_pass2 = xlog_recover_rud_commit_pass2,