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_mount.h"
13 #include "xfs_defer.h"
14 #include "xfs_inode.h"
15 #include "xfs_trans.h"
16 #include "xfs_trans_priv.h"
17 #include "xfs_buf_item.h"
18 #include "xfs_bmap_item.h"
21 #include "xfs_icache.h"
22 #include "xfs_trace.h"
23 #include "xfs_bmap_btree.h"
24 #include "xfs_trans_space.h"
27 kmem_zone_t
*xfs_bui_zone
;
28 kmem_zone_t
*xfs_bud_zone
;
30 static inline struct xfs_bui_log_item
*BUI_ITEM(struct xfs_log_item
*lip
)
32 return container_of(lip
, struct xfs_bui_log_item
, bui_item
);
37 struct xfs_bui_log_item
*buip
)
39 kmem_zone_free(xfs_bui_zone
, buip
);
43 * Freeing the BUI requires that we remove it from the AIL if it has already
44 * been placed there. However, the BUI may not yet have been placed in the AIL
45 * when called by xfs_bui_release() from BUD processing due to the ordering of
46 * committed vs unpin operations in bulk insert operations. Hence the reference
47 * count to ensure only the last caller frees the BUI.
51 struct xfs_bui_log_item
*buip
)
53 ASSERT(atomic_read(&buip
->bui_refcount
) > 0);
54 if (atomic_dec_and_test(&buip
->bui_refcount
)) {
55 xfs_trans_ail_remove(&buip
->bui_item
, SHUTDOWN_LOG_IO_ERROR
);
56 xfs_bui_item_free(buip
);
63 struct xfs_log_item
*lip
,
67 struct xfs_bui_log_item
*buip
= BUI_ITEM(lip
);
70 *nbytes
+= xfs_bui_log_format_sizeof(buip
->bui_format
.bui_nextents
);
74 * This is called to fill in the vector of log iovecs for the
75 * given bui log item. We use only 1 iovec, and we point that
76 * at the bui_log_format structure embedded in the bui item.
77 * It is at this point that we assert that all of the extent
78 * slots in the bui item have been filled.
82 struct xfs_log_item
*lip
,
83 struct xfs_log_vec
*lv
)
85 struct xfs_bui_log_item
*buip
= BUI_ITEM(lip
);
86 struct xfs_log_iovec
*vecp
= NULL
;
88 ASSERT(atomic_read(&buip
->bui_next_extent
) ==
89 buip
->bui_format
.bui_nextents
);
91 buip
->bui_format
.bui_type
= XFS_LI_BUI
;
92 buip
->bui_format
.bui_size
= 1;
94 xlog_copy_iovec(lv
, &vecp
, XLOG_REG_TYPE_BUI_FORMAT
, &buip
->bui_format
,
95 xfs_bui_log_format_sizeof(buip
->bui_format
.bui_nextents
));
99 * Pinning has no meaning for an bui item, so just return.
103 struct xfs_log_item
*lip
)
108 * The unpin operation is the last place an BUI is manipulated in the log. It is
109 * either inserted in the AIL or aborted in the event of a log I/O error. In
110 * either case, the BUI transaction has been successfully committed to make it
111 * this far. Therefore, we expect whoever committed the BUI to either construct
112 * and commit the BUD or drop the BUD's reference in the event of error. Simply
113 * drop the log's BUI reference now that the log is done with it.
117 struct xfs_log_item
*lip
,
120 struct xfs_bui_log_item
*buip
= BUI_ITEM(lip
);
122 xfs_bui_release(buip
);
126 * BUI items have no locking or pushing. However, since BUIs are pulled from
127 * the AIL when their corresponding BUDs are committed to disk, their situation
128 * is very similar to being pinned. Return XFS_ITEM_PINNED so that the caller
129 * will eventually flush the log. This should help in getting the BUI out of
134 struct xfs_log_item
*lip
,
135 struct list_head
*buffer_list
)
137 return XFS_ITEM_PINNED
;
141 * The BUI has been either committed or aborted if the transaction has been
142 * cancelled. If the transaction was cancelled, an BUD isn't going to be
143 * constructed and thus we free the BUI here directly.
147 struct xfs_log_item
*lip
)
149 if (test_bit(XFS_LI_ABORTED
, &lip
->li_flags
))
150 xfs_bui_release(BUI_ITEM(lip
));
154 * The BUI is logged only once and cannot be moved in the log, so simply return
155 * the lsn at which it's been logged.
158 xfs_bui_item_committed(
159 struct xfs_log_item
*lip
,
166 * The BUI dependency tracking op doesn't do squat. It can't because
167 * it doesn't know where the free extent is coming from. The dependency
168 * tracking has to be handled by the "enclosing" metadata object. For
169 * example, for inodes, the inode is locked throughout the extent freeing
170 * so the dependency should be recorded there.
173 xfs_bui_item_committing(
174 struct xfs_log_item
*lip
,
180 * This is the ops vector shared by all bui log items.
182 static const struct xfs_item_ops xfs_bui_item_ops
= {
183 .iop_size
= xfs_bui_item_size
,
184 .iop_format
= xfs_bui_item_format
,
185 .iop_pin
= xfs_bui_item_pin
,
186 .iop_unpin
= xfs_bui_item_unpin
,
187 .iop_unlock
= xfs_bui_item_unlock
,
188 .iop_committed
= xfs_bui_item_committed
,
189 .iop_push
= xfs_bui_item_push
,
190 .iop_committing
= xfs_bui_item_committing
,
194 * Allocate and initialize an bui item with the given number of extents.
196 struct xfs_bui_log_item
*
198 struct xfs_mount
*mp
)
201 struct xfs_bui_log_item
*buip
;
203 buip
= kmem_zone_zalloc(xfs_bui_zone
, KM_SLEEP
);
205 xfs_log_item_init(mp
, &buip
->bui_item
, XFS_LI_BUI
, &xfs_bui_item_ops
);
206 buip
->bui_format
.bui_nextents
= XFS_BUI_MAX_FAST_EXTENTS
;
207 buip
->bui_format
.bui_id
= (uintptr_t)(void *)buip
;
208 atomic_set(&buip
->bui_next_extent
, 0);
209 atomic_set(&buip
->bui_refcount
, 2);
214 static inline struct xfs_bud_log_item
*BUD_ITEM(struct xfs_log_item
*lip
)
216 return container_of(lip
, struct xfs_bud_log_item
, bud_item
);
221 struct xfs_log_item
*lip
,
226 *nbytes
+= sizeof(struct xfs_bud_log_format
);
230 * This is called to fill in the vector of log iovecs for the
231 * given bud log item. We use only 1 iovec, and we point that
232 * at the bud_log_format structure embedded in the bud item.
233 * It is at this point that we assert that all of the extent
234 * slots in the bud item have been filled.
238 struct xfs_log_item
*lip
,
239 struct xfs_log_vec
*lv
)
241 struct xfs_bud_log_item
*budp
= BUD_ITEM(lip
);
242 struct xfs_log_iovec
*vecp
= NULL
;
244 budp
->bud_format
.bud_type
= XFS_LI_BUD
;
245 budp
->bud_format
.bud_size
= 1;
247 xlog_copy_iovec(lv
, &vecp
, XLOG_REG_TYPE_BUD_FORMAT
, &budp
->bud_format
,
248 sizeof(struct xfs_bud_log_format
));
252 * Pinning has no meaning for an bud item, so just return.
256 struct xfs_log_item
*lip
)
261 * Since pinning has no meaning for an bud item, unpinning does
266 struct xfs_log_item
*lip
,
272 * There isn't much you can do to push on an bud item. It is simply stuck
273 * waiting for the log to be flushed to disk.
277 struct xfs_log_item
*lip
,
278 struct list_head
*buffer_list
)
280 return XFS_ITEM_PINNED
;
284 * The BUD is either committed or aborted if the transaction is cancelled. If
285 * the transaction is cancelled, drop our reference to the BUI and free the
290 struct xfs_log_item
*lip
)
292 struct xfs_bud_log_item
*budp
= BUD_ITEM(lip
);
294 if (test_bit(XFS_LI_ABORTED
, &lip
->li_flags
)) {
295 xfs_bui_release(budp
->bud_buip
);
296 kmem_zone_free(xfs_bud_zone
, budp
);
301 * When the bud item is committed to disk, all we need to do is delete our
302 * reference to our partner bui item and then free ourselves. Since we're
303 * freeing ourselves we must return -1 to keep the transaction code from
304 * further referencing this item.
307 xfs_bud_item_committed(
308 struct xfs_log_item
*lip
,
311 struct xfs_bud_log_item
*budp
= BUD_ITEM(lip
);
314 * Drop the BUI reference regardless of whether the BUD has been
315 * aborted. Once the BUD transaction is constructed, it is the sole
316 * responsibility of the BUD to release the BUI (even if the BUI is
317 * aborted due to log I/O error).
319 xfs_bui_release(budp
->bud_buip
);
320 kmem_zone_free(xfs_bud_zone
, budp
);
322 return (xfs_lsn_t
)-1;
326 * The BUD dependency tracking op doesn't do squat. It can't because
327 * it doesn't know where the free extent is coming from. The dependency
328 * tracking has to be handled by the "enclosing" metadata object. For
329 * example, for inodes, the inode is locked throughout the extent freeing
330 * so the dependency should be recorded there.
333 xfs_bud_item_committing(
334 struct xfs_log_item
*lip
,
340 * This is the ops vector shared by all bud log items.
342 static const struct xfs_item_ops xfs_bud_item_ops
= {
343 .iop_size
= xfs_bud_item_size
,
344 .iop_format
= xfs_bud_item_format
,
345 .iop_pin
= xfs_bud_item_pin
,
346 .iop_unpin
= xfs_bud_item_unpin
,
347 .iop_unlock
= xfs_bud_item_unlock
,
348 .iop_committed
= xfs_bud_item_committed
,
349 .iop_push
= xfs_bud_item_push
,
350 .iop_committing
= xfs_bud_item_committing
,
354 * Allocate and initialize an bud item with the given number of extents.
356 struct xfs_bud_log_item
*
358 struct xfs_mount
*mp
,
359 struct xfs_bui_log_item
*buip
)
362 struct xfs_bud_log_item
*budp
;
364 budp
= kmem_zone_zalloc(xfs_bud_zone
, KM_SLEEP
);
365 xfs_log_item_init(mp
, &budp
->bud_item
, XFS_LI_BUD
, &xfs_bud_item_ops
);
366 budp
->bud_buip
= buip
;
367 budp
->bud_format
.bud_bui_id
= buip
->bui_format
.bui_id
;
373 * Process a bmap update intent item that was recovered from the log.
374 * We need to update some inode's bmbt.
378 struct xfs_trans
*parent_tp
,
379 struct xfs_bui_log_item
*buip
)
382 unsigned int bui_type
;
383 struct xfs_map_extent
*bmap
;
384 xfs_fsblock_t startblock_fsb
;
385 xfs_fsblock_t inode_fsb
;
388 struct xfs_bud_log_item
*budp
;
389 enum xfs_bmap_intent_type type
;
392 struct xfs_trans
*tp
;
393 struct xfs_inode
*ip
= NULL
;
394 struct xfs_bmbt_irec irec
;
395 struct xfs_mount
*mp
= parent_tp
->t_mountp
;
397 ASSERT(!test_bit(XFS_BUI_RECOVERED
, &buip
->bui_flags
));
399 /* Only one mapping operation per BUI... */
400 if (buip
->bui_format
.bui_nextents
!= XFS_BUI_MAX_FAST_EXTENTS
) {
401 set_bit(XFS_BUI_RECOVERED
, &buip
->bui_flags
);
402 xfs_bui_release(buip
);
407 * First check the validity of the extent described by the
408 * BUI. If anything is bad, then toss the BUI.
410 bmap
= &buip
->bui_format
.bui_extents
[0];
411 startblock_fsb
= XFS_BB_TO_FSB(mp
,
412 XFS_FSB_TO_DADDR(mp
, bmap
->me_startblock
));
413 inode_fsb
= XFS_BB_TO_FSB(mp
, XFS_FSB_TO_DADDR(mp
,
414 XFS_INO_TO_FSB(mp
, bmap
->me_owner
)));
415 switch (bmap
->me_flags
& XFS_BMAP_EXTENT_TYPE_MASK
) {
424 if (!op_ok
|| startblock_fsb
== 0 ||
427 startblock_fsb
>= mp
->m_sb
.sb_dblocks
||
428 bmap
->me_len
>= mp
->m_sb
.sb_agblocks
||
429 inode_fsb
>= mp
->m_sb
.sb_dblocks
||
430 (bmap
->me_flags
& ~XFS_BMAP_EXTENT_FLAGS
)) {
432 * This will pull the BUI from the AIL and
433 * free the memory associated with it.
435 set_bit(XFS_BUI_RECOVERED
, &buip
->bui_flags
);
436 xfs_bui_release(buip
);
440 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_itruncate
,
441 XFS_EXTENTADD_SPACE_RES(mp
, XFS_DATA_FORK
), 0, 0, &tp
);
445 * Recovery stashes all deferred ops during intent processing and
446 * finishes them on completion. Transfer current dfops state to this
447 * transaction and transfer the result back before we return.
449 xfs_defer_move(tp
, parent_tp
);
450 budp
= xfs_trans_get_bud(tp
, buip
);
452 /* Grab the inode. */
453 error
= xfs_iget(mp
, tp
, bmap
->me_owner
, 0, XFS_ILOCK_EXCL
, &ip
);
457 if (VFS_I(ip
)->i_nlink
== 0)
458 xfs_iflags_set(ip
, XFS_IRECOVERY
);
460 /* Process deferred bmap item. */
461 state
= (bmap
->me_flags
& XFS_BMAP_EXTENT_UNWRITTEN
) ?
462 XFS_EXT_UNWRITTEN
: XFS_EXT_NORM
;
463 whichfork
= (bmap
->me_flags
& XFS_BMAP_EXTENT_ATTR_FORK
) ?
464 XFS_ATTR_FORK
: XFS_DATA_FORK
;
465 bui_type
= bmap
->me_flags
& XFS_BMAP_EXTENT_TYPE_MASK
;
472 error
= -EFSCORRUPTED
;
475 xfs_trans_ijoin(tp
, ip
, 0);
477 count
= bmap
->me_len
;
478 error
= xfs_trans_log_finish_bmap_update(tp
, budp
, type
, ip
, whichfork
,
479 bmap
->me_startoff
, bmap
->me_startblock
, &count
, state
);
484 ASSERT(type
== XFS_BMAP_UNMAP
);
485 irec
.br_startblock
= bmap
->me_startblock
;
486 irec
.br_blockcount
= count
;
487 irec
.br_startoff
= bmap
->me_startoff
;
488 irec
.br_state
= state
;
489 error
= xfs_bmap_unmap_extent(tp
, ip
, &irec
);
494 set_bit(XFS_BUI_RECOVERED
, &buip
->bui_flags
);
495 xfs_defer_move(parent_tp
, tp
);
496 error
= xfs_trans_commit(tp
);
497 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
503 xfs_defer_move(parent_tp
, tp
);
504 xfs_trans_cancel(tp
);
506 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);