2 * Copyright (C) 2016 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it would be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "xfs_format.h"
23 #include "xfs_log_format.h"
24 #include "xfs_trans_resv.h"
26 #include "xfs_mount.h"
27 #include "xfs_defer.h"
28 #include "xfs_inode.h"
29 #include "xfs_trans.h"
30 #include "xfs_trans_priv.h"
31 #include "xfs_buf_item.h"
32 #include "xfs_bmap_item.h"
35 #include "xfs_icache.h"
36 #include "xfs_trace.h"
37 #include "xfs_bmap_btree.h"
38 #include "xfs_trans_space.h"
41 kmem_zone_t
*xfs_bui_zone
;
42 kmem_zone_t
*xfs_bud_zone
;
44 static inline struct xfs_bui_log_item
*BUI_ITEM(struct xfs_log_item
*lip
)
46 return container_of(lip
, struct xfs_bui_log_item
, bui_item
);
51 struct xfs_bui_log_item
*buip
)
53 kmem_zone_free(xfs_bui_zone
, buip
);
58 struct xfs_log_item
*lip
,
62 struct xfs_bui_log_item
*buip
= BUI_ITEM(lip
);
65 *nbytes
+= xfs_bui_log_format_sizeof(buip
->bui_format
.bui_nextents
);
69 * This is called to fill in the vector of log iovecs for the
70 * given bui log item. We use only 1 iovec, and we point that
71 * at the bui_log_format structure embedded in the bui item.
72 * It is at this point that we assert that all of the extent
73 * slots in the bui item have been filled.
77 struct xfs_log_item
*lip
,
78 struct xfs_log_vec
*lv
)
80 struct xfs_bui_log_item
*buip
= BUI_ITEM(lip
);
81 struct xfs_log_iovec
*vecp
= NULL
;
83 ASSERT(atomic_read(&buip
->bui_next_extent
) ==
84 buip
->bui_format
.bui_nextents
);
86 buip
->bui_format
.bui_type
= XFS_LI_BUI
;
87 buip
->bui_format
.bui_size
= 1;
89 xlog_copy_iovec(lv
, &vecp
, XLOG_REG_TYPE_BUI_FORMAT
, &buip
->bui_format
,
90 xfs_bui_log_format_sizeof(buip
->bui_format
.bui_nextents
));
94 * Pinning has no meaning for an bui item, so just return.
98 struct xfs_log_item
*lip
)
103 * The unpin operation is the last place an BUI is manipulated in the log. It is
104 * either inserted in the AIL or aborted in the event of a log I/O error. In
105 * either case, the BUI transaction has been successfully committed to make it
106 * this far. Therefore, we expect whoever committed the BUI to either construct
107 * and commit the BUD or drop the BUD's reference in the event of error. Simply
108 * drop the log's BUI reference now that the log is done with it.
112 struct xfs_log_item
*lip
,
115 struct xfs_bui_log_item
*buip
= BUI_ITEM(lip
);
117 xfs_bui_release(buip
);
121 * BUI items have no locking or pushing. However, since BUIs are pulled from
122 * the AIL when their corresponding BUDs are committed to disk, their situation
123 * is very similar to being pinned. Return XFS_ITEM_PINNED so that the caller
124 * will eventually flush the log. This should help in getting the BUI out of
129 struct xfs_log_item
*lip
,
130 struct list_head
*buffer_list
)
132 return XFS_ITEM_PINNED
;
136 * The BUI has been either committed or aborted if the transaction has been
137 * cancelled. If the transaction was cancelled, an BUD isn't going to be
138 * constructed and thus we free the BUI here directly.
142 struct xfs_log_item
*lip
)
144 if (lip
->li_flags
& XFS_LI_ABORTED
)
145 xfs_bui_item_free(BUI_ITEM(lip
));
149 * The BUI is logged only once and cannot be moved in the log, so simply return
150 * the lsn at which it's been logged.
153 xfs_bui_item_committed(
154 struct xfs_log_item
*lip
,
161 * The BUI dependency tracking op doesn't do squat. It can't because
162 * it doesn't know where the free extent is coming from. The dependency
163 * tracking has to be handled by the "enclosing" metadata object. For
164 * example, for inodes, the inode is locked throughout the extent freeing
165 * so the dependency should be recorded there.
168 xfs_bui_item_committing(
169 struct xfs_log_item
*lip
,
175 * This is the ops vector shared by all bui log items.
177 static const struct xfs_item_ops xfs_bui_item_ops
= {
178 .iop_size
= xfs_bui_item_size
,
179 .iop_format
= xfs_bui_item_format
,
180 .iop_pin
= xfs_bui_item_pin
,
181 .iop_unpin
= xfs_bui_item_unpin
,
182 .iop_unlock
= xfs_bui_item_unlock
,
183 .iop_committed
= xfs_bui_item_committed
,
184 .iop_push
= xfs_bui_item_push
,
185 .iop_committing
= xfs_bui_item_committing
,
189 * Allocate and initialize an bui item with the given number of extents.
191 struct xfs_bui_log_item
*
193 struct xfs_mount
*mp
)
196 struct xfs_bui_log_item
*buip
;
198 buip
= kmem_zone_zalloc(xfs_bui_zone
, KM_SLEEP
);
200 xfs_log_item_init(mp
, &buip
->bui_item
, XFS_LI_BUI
, &xfs_bui_item_ops
);
201 buip
->bui_format
.bui_nextents
= XFS_BUI_MAX_FAST_EXTENTS
;
202 buip
->bui_format
.bui_id
= (uintptr_t)(void *)buip
;
203 atomic_set(&buip
->bui_next_extent
, 0);
204 atomic_set(&buip
->bui_refcount
, 2);
210 * Freeing the BUI requires that we remove it from the AIL if it has already
211 * been placed there. However, the BUI may not yet have been placed in the AIL
212 * when called by xfs_bui_release() from BUD processing due to the ordering of
213 * committed vs unpin operations in bulk insert operations. Hence the reference
214 * count to ensure only the last caller frees the BUI.
218 struct xfs_bui_log_item
*buip
)
220 ASSERT(atomic_read(&buip
->bui_refcount
) > 0);
221 if (atomic_dec_and_test(&buip
->bui_refcount
)) {
222 xfs_trans_ail_remove(&buip
->bui_item
, SHUTDOWN_LOG_IO_ERROR
);
223 xfs_bui_item_free(buip
);
227 static inline struct xfs_bud_log_item
*BUD_ITEM(struct xfs_log_item
*lip
)
229 return container_of(lip
, struct xfs_bud_log_item
, bud_item
);
234 struct xfs_log_item
*lip
,
239 *nbytes
+= sizeof(struct xfs_bud_log_format
);
243 * This is called to fill in the vector of log iovecs for the
244 * given bud log item. We use only 1 iovec, and we point that
245 * at the bud_log_format structure embedded in the bud item.
246 * It is at this point that we assert that all of the extent
247 * slots in the bud item have been filled.
251 struct xfs_log_item
*lip
,
252 struct xfs_log_vec
*lv
)
254 struct xfs_bud_log_item
*budp
= BUD_ITEM(lip
);
255 struct xfs_log_iovec
*vecp
= NULL
;
257 budp
->bud_format
.bud_type
= XFS_LI_BUD
;
258 budp
->bud_format
.bud_size
= 1;
260 xlog_copy_iovec(lv
, &vecp
, XLOG_REG_TYPE_BUD_FORMAT
, &budp
->bud_format
,
261 sizeof(struct xfs_bud_log_format
));
265 * Pinning has no meaning for an bud item, so just return.
269 struct xfs_log_item
*lip
)
274 * Since pinning has no meaning for an bud item, unpinning does
279 struct xfs_log_item
*lip
,
285 * There isn't much you can do to push on an bud item. It is simply stuck
286 * waiting for the log to be flushed to disk.
290 struct xfs_log_item
*lip
,
291 struct list_head
*buffer_list
)
293 return XFS_ITEM_PINNED
;
297 * The BUD is either committed or aborted if the transaction is cancelled. If
298 * the transaction is cancelled, drop our reference to the BUI and free the
303 struct xfs_log_item
*lip
)
305 struct xfs_bud_log_item
*budp
= BUD_ITEM(lip
);
307 if (lip
->li_flags
& XFS_LI_ABORTED
) {
308 xfs_bui_release(budp
->bud_buip
);
309 kmem_zone_free(xfs_bud_zone
, budp
);
314 * When the bud item is committed to disk, all we need to do is delete our
315 * reference to our partner bui item and then free ourselves. Since we're
316 * freeing ourselves we must return -1 to keep the transaction code from
317 * further referencing this item.
320 xfs_bud_item_committed(
321 struct xfs_log_item
*lip
,
324 struct xfs_bud_log_item
*budp
= BUD_ITEM(lip
);
327 * Drop the BUI reference regardless of whether the BUD has been
328 * aborted. Once the BUD transaction is constructed, it is the sole
329 * responsibility of the BUD to release the BUI (even if the BUI is
330 * aborted due to log I/O error).
332 xfs_bui_release(budp
->bud_buip
);
333 kmem_zone_free(xfs_bud_zone
, budp
);
335 return (xfs_lsn_t
)-1;
339 * The BUD dependency tracking op doesn't do squat. It can't because
340 * it doesn't know where the free extent is coming from. The dependency
341 * tracking has to be handled by the "enclosing" metadata object. For
342 * example, for inodes, the inode is locked throughout the extent freeing
343 * so the dependency should be recorded there.
346 xfs_bud_item_committing(
347 struct xfs_log_item
*lip
,
353 * This is the ops vector shared by all bud log items.
355 static const struct xfs_item_ops xfs_bud_item_ops
= {
356 .iop_size
= xfs_bud_item_size
,
357 .iop_format
= xfs_bud_item_format
,
358 .iop_pin
= xfs_bud_item_pin
,
359 .iop_unpin
= xfs_bud_item_unpin
,
360 .iop_unlock
= xfs_bud_item_unlock
,
361 .iop_committed
= xfs_bud_item_committed
,
362 .iop_push
= xfs_bud_item_push
,
363 .iop_committing
= xfs_bud_item_committing
,
367 * Allocate and initialize an bud item with the given number of extents.
369 struct xfs_bud_log_item
*
371 struct xfs_mount
*mp
,
372 struct xfs_bui_log_item
*buip
)
375 struct xfs_bud_log_item
*budp
;
377 budp
= kmem_zone_zalloc(xfs_bud_zone
, KM_SLEEP
);
378 xfs_log_item_init(mp
, &budp
->bud_item
, XFS_LI_BUD
, &xfs_bud_item_ops
);
379 budp
->bud_buip
= buip
;
380 budp
->bud_format
.bud_bui_id
= buip
->bui_format
.bui_id
;
386 * Process a bmap update intent item that was recovered from the log.
387 * We need to update some inode's bmbt.
391 struct xfs_mount
*mp
,
392 struct xfs_bui_log_item
*buip
)
395 unsigned int bui_type
;
396 struct xfs_map_extent
*bmap
;
397 xfs_fsblock_t startblock_fsb
;
398 xfs_fsblock_t inode_fsb
;
400 struct xfs_bud_log_item
*budp
;
401 enum xfs_bmap_intent_type type
;
404 struct xfs_trans
*tp
;
405 struct xfs_inode
*ip
= NULL
;
406 struct xfs_defer_ops dfops
;
407 xfs_fsblock_t firstfsb
;
409 ASSERT(!test_bit(XFS_BUI_RECOVERED
, &buip
->bui_flags
));
411 /* Only one mapping operation per BUI... */
412 if (buip
->bui_format
.bui_nextents
!= XFS_BUI_MAX_FAST_EXTENTS
) {
413 set_bit(XFS_BUI_RECOVERED
, &buip
->bui_flags
);
414 xfs_bui_release(buip
);
419 * First check the validity of the extent described by the
420 * BUI. If anything is bad, then toss the BUI.
422 bmap
= &buip
->bui_format
.bui_extents
[0];
423 startblock_fsb
= XFS_BB_TO_FSB(mp
,
424 XFS_FSB_TO_DADDR(mp
, bmap
->me_startblock
));
425 inode_fsb
= XFS_BB_TO_FSB(mp
, XFS_FSB_TO_DADDR(mp
,
426 XFS_INO_TO_FSB(mp
, bmap
->me_owner
)));
427 switch (bmap
->me_flags
& XFS_BMAP_EXTENT_TYPE_MASK
) {
436 if (!op_ok
|| startblock_fsb
== 0 ||
439 startblock_fsb
>= mp
->m_sb
.sb_dblocks
||
440 bmap
->me_len
>= mp
->m_sb
.sb_agblocks
||
441 inode_fsb
>= mp
->m_sb
.sb_dblocks
||
442 (bmap
->me_flags
& ~XFS_BMAP_EXTENT_FLAGS
)) {
444 * This will pull the BUI from the AIL and
445 * free the memory associated with it.
447 set_bit(XFS_BUI_RECOVERED
, &buip
->bui_flags
);
448 xfs_bui_release(buip
);
452 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_itruncate
,
453 XFS_EXTENTADD_SPACE_RES(mp
, XFS_DATA_FORK
), 0, 0, &tp
);
456 budp
= xfs_trans_get_bud(tp
, buip
);
458 /* Grab the inode. */
459 error
= xfs_iget(mp
, tp
, bmap
->me_owner
, 0, XFS_ILOCK_EXCL
, &ip
);
463 if (VFS_I(ip
)->i_nlink
== 0)
464 xfs_iflags_set(ip
, XFS_IRECOVERY
);
465 xfs_defer_init(&dfops
, &firstfsb
);
467 /* Process deferred bmap item. */
468 state
= (bmap
->me_flags
& XFS_BMAP_EXTENT_UNWRITTEN
) ?
469 XFS_EXT_UNWRITTEN
: XFS_EXT_NORM
;
470 whichfork
= (bmap
->me_flags
& XFS_BMAP_EXTENT_ATTR_FORK
) ?
471 XFS_ATTR_FORK
: XFS_DATA_FORK
;
472 bui_type
= bmap
->me_flags
& XFS_BMAP_EXTENT_TYPE_MASK
;
479 error
= -EFSCORRUPTED
;
482 xfs_trans_ijoin(tp
, ip
, 0);
484 error
= xfs_trans_log_finish_bmap_update(tp
, budp
, &dfops
, type
,
485 ip
, whichfork
, bmap
->me_startoff
,
486 bmap
->me_startblock
, bmap
->me_len
,
491 /* Finish transaction, free inodes. */
492 error
= xfs_defer_finish(&tp
, &dfops
, NULL
);
496 set_bit(XFS_BUI_RECOVERED
, &buip
->bui_flags
);
497 error
= xfs_trans_commit(tp
);
498 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
504 xfs_defer_cancel(&dfops
);
506 xfs_trans_cancel(tp
);
508 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);