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 *map,
248 enum xfs_rmap_intent_type type,
253 if (state == XFS_EXT_UNWRITTEN)
254 map->me_flags |= XFS_RMAP_EXTENT_UNWRITTEN;
255 if (whichfork == XFS_ATTR_FORK)
256 map->me_flags |= XFS_RMAP_EXTENT_ATTR_FORK;
259 map->me_flags |= XFS_RMAP_EXTENT_MAP;
261 case XFS_RMAP_MAP_SHARED:
262 map->me_flags |= XFS_RMAP_EXTENT_MAP_SHARED;
265 map->me_flags |= XFS_RMAP_EXTENT_UNMAP;
267 case XFS_RMAP_UNMAP_SHARED:
268 map->me_flags |= XFS_RMAP_EXTENT_UNMAP_SHARED;
270 case XFS_RMAP_CONVERT:
271 map->me_flags |= XFS_RMAP_EXTENT_CONVERT;
273 case XFS_RMAP_CONVERT_SHARED:
274 map->me_flags |= XFS_RMAP_EXTENT_CONVERT_SHARED;
277 map->me_flags |= XFS_RMAP_EXTENT_ALLOC;
280 map->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 struct xfs_rmap_intent *ri,
297 struct xfs_btree_cur **pcur)
301 error = xfs_rmap_finish_one(tp, ri, pcur);
304 * Mark the transaction dirty, even on error. This ensures the
305 * transaction is aborted, which:
307 * 1.) releases the RUI and frees the RUD
308 * 2.) shuts down the filesystem
310 tp->t_flags |= XFS_TRANS_DIRTY | XFS_TRANS_HAS_INTENT_DONE;
311 set_bit(XFS_LI_DIRTY, &rudp->rud_item.li_flags);
316 /* Sort rmap intents by AG. */
318 xfs_rmap_update_diff_items(
320 const struct list_head *a,
321 const struct list_head *b)
323 struct xfs_mount *mp = priv;
324 struct xfs_rmap_intent *ra;
325 struct xfs_rmap_intent *rb;
327 ra = container_of(a, struct xfs_rmap_intent, ri_list);
328 rb = container_of(b, struct xfs_rmap_intent, ri_list);
329 return XFS_FSB_TO_AGNO(mp, ra->ri_bmap.br_startblock) -
330 XFS_FSB_TO_AGNO(mp, rb->ri_bmap.br_startblock);
333 /* Log rmap updates in the intent item. */
335 xfs_rmap_update_log_item(
336 struct xfs_trans *tp,
337 struct xfs_rui_log_item *ruip,
338 struct xfs_rmap_intent *ri)
341 struct xfs_map_extent *map;
343 tp->t_flags |= XFS_TRANS_DIRTY;
344 set_bit(XFS_LI_DIRTY, &ruip->rui_item.li_flags);
347 * atomic_inc_return gives us the value after the increment;
348 * we want to use it as an array index so we need to subtract 1 from
351 next_extent = atomic_inc_return(&ruip->rui_next_extent) - 1;
352 ASSERT(next_extent < ruip->rui_format.rui_nextents);
353 map = &ruip->rui_format.rui_extents[next_extent];
354 map->me_owner = ri->ri_owner;
355 map->me_startblock = ri->ri_bmap.br_startblock;
356 map->me_startoff = ri->ri_bmap.br_startoff;
357 map->me_len = ri->ri_bmap.br_blockcount;
358 xfs_trans_set_rmap_flags(map, ri->ri_type, ri->ri_whichfork,
359 ri->ri_bmap.br_state);
362 static struct xfs_log_item *
363 xfs_rmap_update_create_intent(
364 struct xfs_trans *tp,
365 struct list_head *items,
369 struct xfs_mount *mp = tp->t_mountp;
370 struct xfs_rui_log_item *ruip = xfs_rui_init(mp, count);
371 struct xfs_rmap_intent *ri;
375 xfs_trans_add_item(tp, &ruip->rui_item);
377 list_sort(mp, items, xfs_rmap_update_diff_items);
378 list_for_each_entry(ri, items, ri_list)
379 xfs_rmap_update_log_item(tp, ruip, ri);
380 return &ruip->rui_item;
383 /* Get an RUD so we can process all the deferred rmap updates. */
384 static struct xfs_log_item *
385 xfs_rmap_update_create_done(
386 struct xfs_trans *tp,
387 struct xfs_log_item *intent,
390 return &xfs_trans_get_rud(tp, RUI_ITEM(intent))->rud_item;
393 /* Process a deferred rmap update. */
395 xfs_rmap_update_finish_item(
396 struct xfs_trans *tp,
397 struct xfs_log_item *done,
398 struct list_head *item,
399 struct xfs_btree_cur **state)
401 struct xfs_rmap_intent *ri;
404 ri = container_of(item, struct xfs_rmap_intent, ri_list);
406 error = xfs_trans_log_finish_rmap_update(tp, RUD_ITEM(done), ri,
408 kmem_cache_free(xfs_rmap_intent_cache, ri);
412 /* Abort all pending RUIs. */
414 xfs_rmap_update_abort_intent(
415 struct xfs_log_item *intent)
417 xfs_rui_release(RUI_ITEM(intent));
420 /* Cancel a deferred rmap update. */
422 xfs_rmap_update_cancel_item(
423 struct list_head *item)
425 struct xfs_rmap_intent *ri;
427 ri = container_of(item, struct xfs_rmap_intent, ri_list);
428 kmem_cache_free(xfs_rmap_intent_cache, ri);
431 const struct xfs_defer_op_type xfs_rmap_update_defer_type = {
432 .max_items = XFS_RUI_MAX_FAST_EXTENTS,
433 .create_intent = xfs_rmap_update_create_intent,
434 .abort_intent = xfs_rmap_update_abort_intent,
435 .create_done = xfs_rmap_update_create_done,
436 .finish_item = xfs_rmap_update_finish_item,
437 .finish_cleanup = xfs_rmap_finish_one_cleanup,
438 .cancel_item = xfs_rmap_update_cancel_item,
441 /* Is this recovered RUI ok? */
443 xfs_rui_validate_map(
444 struct xfs_mount *mp,
445 struct xfs_map_extent *map)
447 if (!xfs_has_rmapbt(mp))
450 if (map->me_flags & ~XFS_RMAP_EXTENT_FLAGS)
453 switch (map->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
454 case XFS_RMAP_EXTENT_MAP:
455 case XFS_RMAP_EXTENT_MAP_SHARED:
456 case XFS_RMAP_EXTENT_UNMAP:
457 case XFS_RMAP_EXTENT_UNMAP_SHARED:
458 case XFS_RMAP_EXTENT_CONVERT:
459 case XFS_RMAP_EXTENT_CONVERT_SHARED:
460 case XFS_RMAP_EXTENT_ALLOC:
461 case XFS_RMAP_EXTENT_FREE:
467 if (!XFS_RMAP_NON_INODE_OWNER(map->me_owner) &&
468 !xfs_verify_ino(mp, map->me_owner))
471 if (!xfs_verify_fileext(mp, map->me_startoff, map->me_len))
474 return xfs_verify_fsbext(mp, map->me_startblock, map->me_len);
478 * Process an rmap update intent item that was recovered from the log.
479 * We need to update the rmapbt.
482 xfs_rui_item_recover(
483 struct xfs_log_item *lip,
484 struct list_head *capture_list)
486 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
487 struct xfs_rud_log_item *rudp;
488 struct xfs_trans *tp;
489 struct xfs_btree_cur *rcur = NULL;
490 struct xfs_mount *mp = lip->li_log->l_mp;
495 * First check the validity of the extents described by the
496 * RUI. If any are bad, then assume that all are bad and
499 for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
500 if (!xfs_rui_validate_map(mp,
501 &ruip->rui_format.rui_extents[i])) {
502 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
504 sizeof(ruip->rui_format));
505 return -EFSCORRUPTED;
509 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate,
510 mp->m_rmap_maxlevels, 0, XFS_TRANS_RESERVE, &tp);
513 rudp = xfs_trans_get_rud(tp, ruip);
515 for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
516 struct xfs_rmap_intent fake = { };
517 struct xfs_map_extent *map;
519 map = &ruip->rui_format.rui_extents[i];
520 switch (map->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
521 case XFS_RMAP_EXTENT_MAP:
522 fake.ri_type = XFS_RMAP_MAP;
524 case XFS_RMAP_EXTENT_MAP_SHARED:
525 fake.ri_type = XFS_RMAP_MAP_SHARED;
527 case XFS_RMAP_EXTENT_UNMAP:
528 fake.ri_type = XFS_RMAP_UNMAP;
530 case XFS_RMAP_EXTENT_UNMAP_SHARED:
531 fake.ri_type = XFS_RMAP_UNMAP_SHARED;
533 case XFS_RMAP_EXTENT_CONVERT:
534 fake.ri_type = XFS_RMAP_CONVERT;
536 case XFS_RMAP_EXTENT_CONVERT_SHARED:
537 fake.ri_type = XFS_RMAP_CONVERT_SHARED;
539 case XFS_RMAP_EXTENT_ALLOC:
540 fake.ri_type = XFS_RMAP_ALLOC;
542 case XFS_RMAP_EXTENT_FREE:
543 fake.ri_type = XFS_RMAP_FREE;
546 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
548 sizeof(ruip->rui_format));
549 error = -EFSCORRUPTED;
553 fake.ri_owner = map->me_owner;
554 fake.ri_whichfork = (map->me_flags & XFS_RMAP_EXTENT_ATTR_FORK) ?
555 XFS_ATTR_FORK : XFS_DATA_FORK;
556 fake.ri_bmap.br_startblock = map->me_startblock;
557 fake.ri_bmap.br_startoff = map->me_startoff;
558 fake.ri_bmap.br_blockcount = map->me_len;
559 fake.ri_bmap.br_state = (map->me_flags & XFS_RMAP_EXTENT_UNWRITTEN) ?
560 XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
562 error = xfs_trans_log_finish_rmap_update(tp, rudp, &fake,
564 if (error == -EFSCORRUPTED)
565 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
572 xfs_rmap_finish_one_cleanup(tp, rcur, error);
573 return xfs_defer_ops_capture_and_commit(tp, capture_list);
576 xfs_rmap_finish_one_cleanup(tp, rcur, error);
577 xfs_trans_cancel(tp);
583 struct xfs_log_item *lip,
586 return RUI_ITEM(lip)->rui_format.rui_id == intent_id;
589 /* Relog an intent item to push the log tail forward. */
590 static struct xfs_log_item *
592 struct xfs_log_item *intent,
593 struct xfs_trans *tp)
595 struct xfs_rud_log_item *rudp;
596 struct xfs_rui_log_item *ruip;
597 struct xfs_map_extent *map;
600 count = RUI_ITEM(intent)->rui_format.rui_nextents;
601 map = RUI_ITEM(intent)->rui_format.rui_extents;
603 tp->t_flags |= XFS_TRANS_DIRTY;
604 rudp = xfs_trans_get_rud(tp, RUI_ITEM(intent));
605 set_bit(XFS_LI_DIRTY, &rudp->rud_item.li_flags);
607 ruip = xfs_rui_init(tp->t_mountp, count);
608 memcpy(ruip->rui_format.rui_extents, map, count * sizeof(*map));
609 atomic_set(&ruip->rui_next_extent, count);
610 xfs_trans_add_item(tp, &ruip->rui_item);
611 set_bit(XFS_LI_DIRTY, &ruip->rui_item.li_flags);
612 return &ruip->rui_item;
615 static const struct xfs_item_ops xfs_rui_item_ops = {
616 .flags = XFS_ITEM_INTENT,
617 .iop_size = xfs_rui_item_size,
618 .iop_format = xfs_rui_item_format,
619 .iop_unpin = xfs_rui_item_unpin,
620 .iop_release = xfs_rui_item_release,
621 .iop_recover = xfs_rui_item_recover,
622 .iop_match = xfs_rui_item_match,
623 .iop_relog = xfs_rui_item_relog,
628 struct xfs_rui_log_format *dst,
629 const struct xfs_rui_log_format *src)
633 memcpy(dst, src, offsetof(struct xfs_rui_log_format, rui_extents));
635 for (i = 0; i < src->rui_nextents; i++)
636 memcpy(&dst->rui_extents[i], &src->rui_extents[i],
637 sizeof(struct xfs_map_extent));
641 * This routine is called to create an in-core extent rmap update
642 * item from the rui format structure which was logged on disk.
643 * It allocates an in-core rui, copies the extents from the format
644 * structure into it, and adds the rui to the AIL with the given
648 xlog_recover_rui_commit_pass2(
650 struct list_head *buffer_list,
651 struct xlog_recover_item *item,
654 struct xfs_mount *mp = log->l_mp;
655 struct xfs_rui_log_item *ruip;
656 struct xfs_rui_log_format *rui_formatp;
659 rui_formatp = item->ri_buf[0].i_addr;
661 if (item->ri_buf[0].i_len < xfs_rui_log_format_sizeof(0)) {
662 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
663 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
664 return -EFSCORRUPTED;
667 len = xfs_rui_log_format_sizeof(rui_formatp->rui_nextents);
668 if (item->ri_buf[0].i_len != len) {
669 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
670 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
671 return -EFSCORRUPTED;
674 ruip = xfs_rui_init(mp, rui_formatp->rui_nextents);
675 xfs_rui_copy_format(&ruip->rui_format, rui_formatp);
676 atomic_set(&ruip->rui_next_extent, rui_formatp->rui_nextents);
678 * Insert the intent into the AIL directly and drop one reference so
679 * that finishing or canceling the work will drop the other.
681 xfs_trans_ail_insert(log->l_ailp, &ruip->rui_item, lsn);
682 xfs_rui_release(ruip);
686 const struct xlog_recover_item_ops xlog_rui_item_ops = {
687 .item_type = XFS_LI_RUI,
688 .commit_pass2 = xlog_recover_rui_commit_pass2,
692 * This routine is called when an RUD format structure is found in a committed
693 * transaction in the log. Its purpose is to cancel the corresponding RUI if it
694 * was still in the log. To do this it searches the AIL for the RUI with an id
695 * equal to that in the RUD format structure. If we find it we drop the RUD
696 * reference, which removes the RUI from the AIL and frees it.
699 xlog_recover_rud_commit_pass2(
701 struct list_head *buffer_list,
702 struct xlog_recover_item *item,
705 struct xfs_rud_log_format *rud_formatp;
707 rud_formatp = item->ri_buf[0].i_addr;
708 if (item->ri_buf[0].i_len != sizeof(struct xfs_rud_log_format)) {
709 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
710 rud_formatp, item->ri_buf[0].i_len);
711 return -EFSCORRUPTED;
714 xlog_recover_release_intent(log, XFS_LI_RUI, rud_formatp->rud_rui_id);
718 const struct xlog_recover_item_ops xlog_rud_item_ops = {
719 .item_type = XFS_LI_RUD,
720 .commit_pass2 = xlog_recover_rud_commit_pass2,