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_inode.h"
16 #include "xfs_trans.h"
17 #include "xfs_trans_priv.h"
18 #include "xfs_bmap_item.h"
21 #include "xfs_icache.h"
22 #include "xfs_bmap_btree.h"
23 #include "xfs_trans_space.h"
24 #include "xfs_error.h"
25 #include "xfs_log_priv.h"
26 #include "xfs_log_recover.h"
28 kmem_zone_t
*xfs_bui_zone
;
29 kmem_zone_t
*xfs_bud_zone
;
31 static const struct xfs_item_ops xfs_bui_item_ops
;
33 static inline struct xfs_bui_log_item
*BUI_ITEM(struct xfs_log_item
*lip
)
35 return container_of(lip
, struct xfs_bui_log_item
, bui_item
);
40 struct xfs_bui_log_item
*buip
)
42 kmem_cache_free(xfs_bui_zone
, buip
);
46 * Freeing the BUI requires that we remove it from the AIL if it has already
47 * been placed there. However, the BUI may not yet have been placed in the AIL
48 * when called by xfs_bui_release() from BUD 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 BUI.
54 struct xfs_bui_log_item
*buip
)
56 ASSERT(atomic_read(&buip
->bui_refcount
) > 0);
57 if (atomic_dec_and_test(&buip
->bui_refcount
)) {
58 xfs_trans_ail_delete(&buip
->bui_item
, SHUTDOWN_LOG_IO_ERROR
);
59 xfs_bui_item_free(buip
);
66 struct xfs_log_item
*lip
,
70 struct xfs_bui_log_item
*buip
= BUI_ITEM(lip
);
73 *nbytes
+= xfs_bui_log_format_sizeof(buip
->bui_format
.bui_nextents
);
77 * This is called to fill in the vector of log iovecs for the
78 * given bui log item. We use only 1 iovec, and we point that
79 * at the bui_log_format structure embedded in the bui item.
80 * It is at this point that we assert that all of the extent
81 * slots in the bui item have been filled.
85 struct xfs_log_item
*lip
,
86 struct xfs_log_vec
*lv
)
88 struct xfs_bui_log_item
*buip
= BUI_ITEM(lip
);
89 struct xfs_log_iovec
*vecp
= NULL
;
91 ASSERT(atomic_read(&buip
->bui_next_extent
) ==
92 buip
->bui_format
.bui_nextents
);
94 buip
->bui_format
.bui_type
= XFS_LI_BUI
;
95 buip
->bui_format
.bui_size
= 1;
97 xlog_copy_iovec(lv
, &vecp
, XLOG_REG_TYPE_BUI_FORMAT
, &buip
->bui_format
,
98 xfs_bui_log_format_sizeof(buip
->bui_format
.bui_nextents
));
102 * The unpin operation is the last place an BUI 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 BUI transaction has been successfully committed to make it
105 * this far. Therefore, we expect whoever committed the BUI to either construct
106 * and commit the BUD or drop the BUD's reference in the event of error. Simply
107 * drop the log's BUI reference now that the log is done with it.
111 struct xfs_log_item
*lip
,
114 struct xfs_bui_log_item
*buip
= BUI_ITEM(lip
);
116 xfs_bui_release(buip
);
120 * The BUI has been either committed or aborted if the transaction has been
121 * cancelled. If the transaction was cancelled, an BUD isn't going to be
122 * constructed and thus we free the BUI here directly.
125 xfs_bui_item_release(
126 struct xfs_log_item
*lip
)
128 xfs_bui_release(BUI_ITEM(lip
));
132 * Allocate and initialize an bui item with the given number of extents.
134 STATIC
struct xfs_bui_log_item
*
136 struct xfs_mount
*mp
)
139 struct xfs_bui_log_item
*buip
;
141 buip
= kmem_zone_zalloc(xfs_bui_zone
, 0);
143 xfs_log_item_init(mp
, &buip
->bui_item
, XFS_LI_BUI
, &xfs_bui_item_ops
);
144 buip
->bui_format
.bui_nextents
= XFS_BUI_MAX_FAST_EXTENTS
;
145 buip
->bui_format
.bui_id
= (uintptr_t)(void *)buip
;
146 atomic_set(&buip
->bui_next_extent
, 0);
147 atomic_set(&buip
->bui_refcount
, 2);
152 static inline struct xfs_bud_log_item
*BUD_ITEM(struct xfs_log_item
*lip
)
154 return container_of(lip
, struct xfs_bud_log_item
, bud_item
);
159 struct xfs_log_item
*lip
,
164 *nbytes
+= sizeof(struct xfs_bud_log_format
);
168 * This is called to fill in the vector of log iovecs for the
169 * given bud log item. We use only 1 iovec, and we point that
170 * at the bud_log_format structure embedded in the bud item.
171 * It is at this point that we assert that all of the extent
172 * slots in the bud item have been filled.
176 struct xfs_log_item
*lip
,
177 struct xfs_log_vec
*lv
)
179 struct xfs_bud_log_item
*budp
= BUD_ITEM(lip
);
180 struct xfs_log_iovec
*vecp
= NULL
;
182 budp
->bud_format
.bud_type
= XFS_LI_BUD
;
183 budp
->bud_format
.bud_size
= 1;
185 xlog_copy_iovec(lv
, &vecp
, XLOG_REG_TYPE_BUD_FORMAT
, &budp
->bud_format
,
186 sizeof(struct xfs_bud_log_format
));
190 * The BUD is either committed or aborted if the transaction is cancelled. If
191 * the transaction is cancelled, drop our reference to the BUI and free the
195 xfs_bud_item_release(
196 struct xfs_log_item
*lip
)
198 struct xfs_bud_log_item
*budp
= BUD_ITEM(lip
);
200 xfs_bui_release(budp
->bud_buip
);
201 kmem_cache_free(xfs_bud_zone
, budp
);
204 static const struct xfs_item_ops xfs_bud_item_ops
= {
205 .flags
= XFS_ITEM_RELEASE_WHEN_COMMITTED
,
206 .iop_size
= xfs_bud_item_size
,
207 .iop_format
= xfs_bud_item_format
,
208 .iop_release
= xfs_bud_item_release
,
211 static struct xfs_bud_log_item
*
213 struct xfs_trans
*tp
,
214 struct xfs_bui_log_item
*buip
)
216 struct xfs_bud_log_item
*budp
;
218 budp
= kmem_zone_zalloc(xfs_bud_zone
, 0);
219 xfs_log_item_init(tp
->t_mountp
, &budp
->bud_item
, XFS_LI_BUD
,
221 budp
->bud_buip
= buip
;
222 budp
->bud_format
.bud_bui_id
= buip
->bui_format
.bui_id
;
224 xfs_trans_add_item(tp
, &budp
->bud_item
);
229 * Finish an bmap update and log it to the BUD. Note that the
230 * transaction is marked dirty regardless of whether the bmap update
231 * succeeds or fails to support the BUI/BUD lifecycle rules.
234 xfs_trans_log_finish_bmap_update(
235 struct xfs_trans
*tp
,
236 struct xfs_bud_log_item
*budp
,
237 enum xfs_bmap_intent_type type
,
238 struct xfs_inode
*ip
,
240 xfs_fileoff_t startoff
,
241 xfs_fsblock_t startblock
,
242 xfs_filblks_t
*blockcount
,
247 error
= xfs_bmap_finish_one(tp
, ip
, type
, whichfork
, startoff
,
248 startblock
, blockcount
, state
);
251 * Mark the transaction dirty, even on error. This ensures the
252 * transaction is aborted, which:
254 * 1.) releases the BUI and frees the BUD
255 * 2.) shuts down the filesystem
257 tp
->t_flags
|= XFS_TRANS_DIRTY
;
258 set_bit(XFS_LI_DIRTY
, &budp
->bud_item
.li_flags
);
263 /* Sort bmap intents by inode. */
265 xfs_bmap_update_diff_items(
270 struct xfs_bmap_intent
*ba
;
271 struct xfs_bmap_intent
*bb
;
273 ba
= container_of(a
, struct xfs_bmap_intent
, bi_list
);
274 bb
= container_of(b
, struct xfs_bmap_intent
, bi_list
);
275 return ba
->bi_owner
->i_ino
- bb
->bi_owner
->i_ino
;
278 /* Set the map extent flags for this mapping. */
280 xfs_trans_set_bmap_flags(
281 struct xfs_map_extent
*bmap
,
282 enum xfs_bmap_intent_type type
,
290 bmap
->me_flags
= type
;
295 if (state
== XFS_EXT_UNWRITTEN
)
296 bmap
->me_flags
|= XFS_BMAP_EXTENT_UNWRITTEN
;
297 if (whichfork
== XFS_ATTR_FORK
)
298 bmap
->me_flags
|= XFS_BMAP_EXTENT_ATTR_FORK
;
301 /* Log bmap updates in the intent item. */
303 xfs_bmap_update_log_item(
304 struct xfs_trans
*tp
,
305 struct xfs_bui_log_item
*buip
,
306 struct xfs_bmap_intent
*bmap
)
309 struct xfs_map_extent
*map
;
311 tp
->t_flags
|= XFS_TRANS_DIRTY
;
312 set_bit(XFS_LI_DIRTY
, &buip
->bui_item
.li_flags
);
315 * atomic_inc_return gives us the value after the increment;
316 * we want to use it as an array index so we need to subtract 1 from
319 next_extent
= atomic_inc_return(&buip
->bui_next_extent
) - 1;
320 ASSERT(next_extent
< buip
->bui_format
.bui_nextents
);
321 map
= &buip
->bui_format
.bui_extents
[next_extent
];
322 map
->me_owner
= bmap
->bi_owner
->i_ino
;
323 map
->me_startblock
= bmap
->bi_bmap
.br_startblock
;
324 map
->me_startoff
= bmap
->bi_bmap
.br_startoff
;
325 map
->me_len
= bmap
->bi_bmap
.br_blockcount
;
326 xfs_trans_set_bmap_flags(map
, bmap
->bi_type
, bmap
->bi_whichfork
,
327 bmap
->bi_bmap
.br_state
);
330 static struct xfs_log_item
*
331 xfs_bmap_update_create_intent(
332 struct xfs_trans
*tp
,
333 struct list_head
*items
,
337 struct xfs_mount
*mp
= tp
->t_mountp
;
338 struct xfs_bui_log_item
*buip
= xfs_bui_init(mp
);
339 struct xfs_bmap_intent
*bmap
;
341 ASSERT(count
== XFS_BUI_MAX_FAST_EXTENTS
);
343 xfs_trans_add_item(tp
, &buip
->bui_item
);
345 list_sort(mp
, items
, xfs_bmap_update_diff_items
);
346 list_for_each_entry(bmap
, items
, bi_list
)
347 xfs_bmap_update_log_item(tp
, buip
, bmap
);
348 return &buip
->bui_item
;
351 /* Get an BUD so we can process all the deferred rmap updates. */
352 static struct xfs_log_item
*
353 xfs_bmap_update_create_done(
354 struct xfs_trans
*tp
,
355 struct xfs_log_item
*intent
,
358 return &xfs_trans_get_bud(tp
, BUI_ITEM(intent
))->bud_item
;
361 /* Process a deferred rmap update. */
363 xfs_bmap_update_finish_item(
364 struct xfs_trans
*tp
,
365 struct xfs_log_item
*done
,
366 struct list_head
*item
,
367 struct xfs_btree_cur
**state
)
369 struct xfs_bmap_intent
*bmap
;
373 bmap
= container_of(item
, struct xfs_bmap_intent
, bi_list
);
374 count
= bmap
->bi_bmap
.br_blockcount
;
375 error
= xfs_trans_log_finish_bmap_update(tp
, BUD_ITEM(done
),
377 bmap
->bi_owner
, bmap
->bi_whichfork
,
378 bmap
->bi_bmap
.br_startoff
,
379 bmap
->bi_bmap
.br_startblock
,
381 bmap
->bi_bmap
.br_state
);
382 if (!error
&& count
> 0) {
383 ASSERT(bmap
->bi_type
== XFS_BMAP_UNMAP
);
384 bmap
->bi_bmap
.br_blockcount
= count
;
391 /* Abort all pending BUIs. */
393 xfs_bmap_update_abort_intent(
394 struct xfs_log_item
*intent
)
396 xfs_bui_release(BUI_ITEM(intent
));
399 /* Cancel a deferred rmap update. */
401 xfs_bmap_update_cancel_item(
402 struct list_head
*item
)
404 struct xfs_bmap_intent
*bmap
;
406 bmap
= container_of(item
, struct xfs_bmap_intent
, bi_list
);
410 const struct xfs_defer_op_type xfs_bmap_update_defer_type
= {
411 .max_items
= XFS_BUI_MAX_FAST_EXTENTS
,
412 .create_intent
= xfs_bmap_update_create_intent
,
413 .abort_intent
= xfs_bmap_update_abort_intent
,
414 .create_done
= xfs_bmap_update_create_done
,
415 .finish_item
= xfs_bmap_update_finish_item
,
416 .cancel_item
= xfs_bmap_update_cancel_item
,
420 * Process a bmap update intent item that was recovered from the log.
421 * We need to update some inode's bmbt.
424 xfs_bui_item_recover(
425 struct xfs_log_item
*lip
,
426 struct xfs_trans
*parent_tp
)
428 struct xfs_bmbt_irec irec
;
429 struct xfs_bui_log_item
*buip
= BUI_ITEM(lip
);
430 struct xfs_trans
*tp
;
431 struct xfs_inode
*ip
= NULL
;
432 struct xfs_mount
*mp
= parent_tp
->t_mountp
;
433 struct xfs_map_extent
*bmap
;
434 struct xfs_bud_log_item
*budp
;
435 xfs_fsblock_t startblock_fsb
;
436 xfs_fsblock_t inode_fsb
;
439 enum xfs_bmap_intent_type type
;
441 unsigned int bui_type
;
445 /* Only one mapping operation per BUI... */
446 if (buip
->bui_format
.bui_nextents
!= XFS_BUI_MAX_FAST_EXTENTS
) {
447 xfs_bui_release(buip
);
448 return -EFSCORRUPTED
;
452 * First check the validity of the extent described by the
453 * BUI. If anything is bad, then toss the BUI.
455 bmap
= &buip
->bui_format
.bui_extents
[0];
456 startblock_fsb
= XFS_BB_TO_FSB(mp
,
457 XFS_FSB_TO_DADDR(mp
, bmap
->me_startblock
));
458 inode_fsb
= XFS_BB_TO_FSB(mp
, XFS_FSB_TO_DADDR(mp
,
459 XFS_INO_TO_FSB(mp
, bmap
->me_owner
)));
460 switch (bmap
->me_flags
& XFS_BMAP_EXTENT_TYPE_MASK
) {
469 if (!op_ok
|| startblock_fsb
== 0 ||
472 startblock_fsb
>= mp
->m_sb
.sb_dblocks
||
473 bmap
->me_len
>= mp
->m_sb
.sb_agblocks
||
474 inode_fsb
>= mp
->m_sb
.sb_dblocks
||
475 (bmap
->me_flags
& ~XFS_BMAP_EXTENT_FLAGS
)) {
477 * This will pull the BUI from the AIL and
478 * free the memory associated with it.
480 xfs_bui_release(buip
);
481 return -EFSCORRUPTED
;
484 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_itruncate
,
485 XFS_EXTENTADD_SPACE_RES(mp
, XFS_DATA_FORK
), 0, 0, &tp
);
489 * Recovery stashes all deferred ops during intent processing and
490 * finishes them on completion. Transfer current dfops state to this
491 * transaction and transfer the result back before we return.
493 xfs_defer_move(tp
, parent_tp
);
494 budp
= xfs_trans_get_bud(tp
, buip
);
496 /* Grab the inode. */
497 error
= xfs_iget(mp
, tp
, bmap
->me_owner
, 0, XFS_ILOCK_EXCL
, &ip
);
501 if (VFS_I(ip
)->i_nlink
== 0)
502 xfs_iflags_set(ip
, XFS_IRECOVERY
);
504 /* Process deferred bmap item. */
505 state
= (bmap
->me_flags
& XFS_BMAP_EXTENT_UNWRITTEN
) ?
506 XFS_EXT_UNWRITTEN
: XFS_EXT_NORM
;
507 whichfork
= (bmap
->me_flags
& XFS_BMAP_EXTENT_ATTR_FORK
) ?
508 XFS_ATTR_FORK
: XFS_DATA_FORK
;
509 bui_type
= bmap
->me_flags
& XFS_BMAP_EXTENT_TYPE_MASK
;
516 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, mp
);
517 error
= -EFSCORRUPTED
;
520 xfs_trans_ijoin(tp
, ip
, 0);
522 count
= bmap
->me_len
;
523 error
= xfs_trans_log_finish_bmap_update(tp
, budp
, type
, ip
, whichfork
,
524 bmap
->me_startoff
, bmap
->me_startblock
, &count
, state
);
529 ASSERT(type
== XFS_BMAP_UNMAP
);
530 irec
.br_startblock
= bmap
->me_startblock
;
531 irec
.br_blockcount
= count
;
532 irec
.br_startoff
= bmap
->me_startoff
;
533 irec
.br_state
= state
;
534 xfs_bmap_unmap_extent(tp
, ip
, &irec
);
537 xfs_defer_move(parent_tp
, tp
);
538 error
= xfs_trans_commit(tp
);
539 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
545 xfs_defer_move(parent_tp
, tp
);
546 xfs_trans_cancel(tp
);
548 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
556 struct xfs_log_item
*lip
,
559 return BUI_ITEM(lip
)->bui_format
.bui_id
== intent_id
;
562 static const struct xfs_item_ops xfs_bui_item_ops
= {
563 .iop_size
= xfs_bui_item_size
,
564 .iop_format
= xfs_bui_item_format
,
565 .iop_unpin
= xfs_bui_item_unpin
,
566 .iop_release
= xfs_bui_item_release
,
567 .iop_recover
= xfs_bui_item_recover
,
568 .iop_match
= xfs_bui_item_match
,
572 * Copy an BUI format buffer from the given buf, and into the destination
573 * BUI format structure. The BUI/BUD items were designed not to need any
574 * special alignment handling.
578 struct xfs_log_iovec
*buf
,
579 struct xfs_bui_log_format
*dst_bui_fmt
)
581 struct xfs_bui_log_format
*src_bui_fmt
;
584 src_bui_fmt
= buf
->i_addr
;
585 len
= xfs_bui_log_format_sizeof(src_bui_fmt
->bui_nextents
);
587 if (buf
->i_len
== len
) {
588 memcpy(dst_bui_fmt
, src_bui_fmt
, len
);
591 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, NULL
);
592 return -EFSCORRUPTED
;
596 * This routine is called to create an in-core extent bmap update
597 * item from the bui format structure which was logged on disk.
598 * It allocates an in-core bui, copies the extents from the format
599 * structure into it, and adds the bui to the AIL with the given
603 xlog_recover_bui_commit_pass2(
605 struct list_head
*buffer_list
,
606 struct xlog_recover_item
*item
,
610 struct xfs_mount
*mp
= log
->l_mp
;
611 struct xfs_bui_log_item
*buip
;
612 struct xfs_bui_log_format
*bui_formatp
;
614 bui_formatp
= item
->ri_buf
[0].i_addr
;
616 if (bui_formatp
->bui_nextents
!= XFS_BUI_MAX_FAST_EXTENTS
) {
617 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, log
->l_mp
);
618 return -EFSCORRUPTED
;
620 buip
= xfs_bui_init(mp
);
621 error
= xfs_bui_copy_format(&item
->ri_buf
[0], &buip
->bui_format
);
623 xfs_bui_item_free(buip
);
626 atomic_set(&buip
->bui_next_extent
, bui_formatp
->bui_nextents
);
628 * Insert the intent into the AIL directly and drop one reference so
629 * that finishing or canceling the work will drop the other.
631 xfs_trans_ail_insert(log
->l_ailp
, &buip
->bui_item
, lsn
);
632 xfs_bui_release(buip
);
636 const struct xlog_recover_item_ops xlog_bui_item_ops
= {
637 .item_type
= XFS_LI_BUI
,
638 .commit_pass2
= xlog_recover_bui_commit_pass2
,
642 * This routine is called when an BUD format structure is found in a committed
643 * transaction in the log. Its purpose is to cancel the corresponding BUI if it
644 * was still in the log. To do this it searches the AIL for the BUI with an id
645 * equal to that in the BUD format structure. If we find it we drop the BUD
646 * reference, which removes the BUI from the AIL and frees it.
649 xlog_recover_bud_commit_pass2(
651 struct list_head
*buffer_list
,
652 struct xlog_recover_item
*item
,
655 struct xfs_bud_log_format
*bud_formatp
;
657 bud_formatp
= item
->ri_buf
[0].i_addr
;
658 if (item
->ri_buf
[0].i_len
!= sizeof(struct xfs_bud_log_format
)) {
659 XFS_ERROR_REPORT(__func__
, XFS_ERRLEVEL_LOW
, log
->l_mp
);
660 return -EFSCORRUPTED
;
663 xlog_recover_release_intent(log
, XFS_LI_BUI
, bud_formatp
->bud_bui_id
);
667 const struct xlog_recover_item_ops xlog_bud_item_ops
= {
668 .item_type
= XFS_LI_BUD
,
669 .commit_pass2
= xlog_recover_bud_commit_pass2
,