2 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xfs_format.h"
21 #include "xfs_log_format.h"
22 #include "xfs_trans_resv.h"
24 #include "xfs_mount.h"
25 #include "xfs_trans.h"
26 #include "xfs_trans_priv.h"
27 #include "xfs_buf_item.h"
28 #include "xfs_extfree_item.h"
32 kmem_zone_t
*xfs_efi_zone
;
33 kmem_zone_t
*xfs_efd_zone
;
35 static inline struct xfs_efi_log_item
*EFI_ITEM(struct xfs_log_item
*lip
)
37 return container_of(lip
, struct xfs_efi_log_item
, efi_item
);
42 struct xfs_efi_log_item
*efip
)
44 kmem_free(efip
->efi_item
.li_lv_shadow
);
45 if (efip
->efi_format
.efi_nextents
> XFS_EFI_MAX_FAST_EXTENTS
)
48 kmem_zone_free(xfs_efi_zone
, efip
);
52 * This returns the number of iovecs needed to log the given efi item.
53 * We only need 1 iovec for an efi item. It just logs the efi_log_format
58 struct xfs_efi_log_item
*efip
)
60 return sizeof(struct xfs_efi_log_format
) +
61 (efip
->efi_format
.efi_nextents
- 1) * sizeof(xfs_extent_t
);
66 struct xfs_log_item
*lip
,
71 *nbytes
+= xfs_efi_item_sizeof(EFI_ITEM(lip
));
75 * This is called to fill in the vector of log iovecs for the
76 * given efi log item. We use only 1 iovec, and we point that
77 * at the efi_log_format structure embedded in the efi item.
78 * It is at this point that we assert that all of the extent
79 * slots in the efi item have been filled.
83 struct xfs_log_item
*lip
,
84 struct xfs_log_vec
*lv
)
86 struct xfs_efi_log_item
*efip
= EFI_ITEM(lip
);
87 struct xfs_log_iovec
*vecp
= NULL
;
89 ASSERT(atomic_read(&efip
->efi_next_extent
) ==
90 efip
->efi_format
.efi_nextents
);
92 efip
->efi_format
.efi_type
= XFS_LI_EFI
;
93 efip
->efi_format
.efi_size
= 1;
95 xlog_copy_iovec(lv
, &vecp
, XLOG_REG_TYPE_EFI_FORMAT
,
97 xfs_efi_item_sizeof(efip
));
102 * Pinning has no meaning for an efi item, so just return.
106 struct xfs_log_item
*lip
)
111 * The unpin operation is the last place an EFI is manipulated in the log. It is
112 * either inserted in the AIL or aborted in the event of a log I/O error. In
113 * either case, the EFI transaction has been successfully committed to make it
114 * this far. Therefore, we expect whoever committed the EFI to either construct
115 * and commit the EFD or drop the EFD's reference in the event of error. Simply
116 * drop the log's EFI reference now that the log is done with it.
120 struct xfs_log_item
*lip
,
123 struct xfs_efi_log_item
*efip
= EFI_ITEM(lip
);
124 xfs_efi_release(efip
);
128 * Efi items have no locking or pushing. However, since EFIs are pulled from
129 * the AIL when their corresponding EFDs are committed to disk, their situation
130 * is very similar to being pinned. Return XFS_ITEM_PINNED so that the caller
131 * will eventually flush the log. This should help in getting the EFI out of
136 struct xfs_log_item
*lip
,
137 struct list_head
*buffer_list
)
139 return XFS_ITEM_PINNED
;
143 * The EFI has been either committed or aborted if the transaction has been
144 * cancelled. If the transaction was cancelled, an EFD isn't going to be
145 * constructed and thus we free the EFI here directly.
149 struct xfs_log_item
*lip
)
151 if (lip
->li_flags
& XFS_LI_ABORTED
)
152 xfs_efi_item_free(EFI_ITEM(lip
));
156 * The EFI is logged only once and cannot be moved in the log, so simply return
157 * the lsn at which it's been logged.
160 xfs_efi_item_committed(
161 struct xfs_log_item
*lip
,
168 * The EFI dependency tracking op doesn't do squat. It can't because
169 * it doesn't know where the free extent is coming from. The dependency
170 * tracking has to be handled by the "enclosing" metadata object. For
171 * example, for inodes, the inode is locked throughout the extent freeing
172 * so the dependency should be recorded there.
175 xfs_efi_item_committing(
176 struct xfs_log_item
*lip
,
182 * This is the ops vector shared by all efi log items.
184 static const struct xfs_item_ops xfs_efi_item_ops
= {
185 .iop_size
= xfs_efi_item_size
,
186 .iop_format
= xfs_efi_item_format
,
187 .iop_pin
= xfs_efi_item_pin
,
188 .iop_unpin
= xfs_efi_item_unpin
,
189 .iop_unlock
= xfs_efi_item_unlock
,
190 .iop_committed
= xfs_efi_item_committed
,
191 .iop_push
= xfs_efi_item_push
,
192 .iop_committing
= xfs_efi_item_committing
197 * Allocate and initialize an efi item with the given number of extents.
199 struct xfs_efi_log_item
*
201 struct xfs_mount
*mp
,
205 struct xfs_efi_log_item
*efip
;
208 ASSERT(nextents
> 0);
209 if (nextents
> XFS_EFI_MAX_FAST_EXTENTS
) {
210 size
= (uint
)(sizeof(xfs_efi_log_item_t
) +
211 ((nextents
- 1) * sizeof(xfs_extent_t
)));
212 efip
= kmem_zalloc(size
, KM_SLEEP
);
214 efip
= kmem_zone_zalloc(xfs_efi_zone
, KM_SLEEP
);
217 xfs_log_item_init(mp
, &efip
->efi_item
, XFS_LI_EFI
, &xfs_efi_item_ops
);
218 efip
->efi_format
.efi_nextents
= nextents
;
219 efip
->efi_format
.efi_id
= (uintptr_t)(void *)efip
;
220 atomic_set(&efip
->efi_next_extent
, 0);
221 atomic_set(&efip
->efi_refcount
, 2);
227 * Copy an EFI format buffer from the given buf, and into the destination
228 * EFI format structure.
229 * The given buffer can be in 32 bit or 64 bit form (which has different padding),
230 * one of which will be the native format for this kernel.
231 * It will handle the conversion of formats if necessary.
234 xfs_efi_copy_format(xfs_log_iovec_t
*buf
, xfs_efi_log_format_t
*dst_efi_fmt
)
236 xfs_efi_log_format_t
*src_efi_fmt
= buf
->i_addr
;
238 uint len
= sizeof(xfs_efi_log_format_t
) +
239 (src_efi_fmt
->efi_nextents
- 1) * sizeof(xfs_extent_t
);
240 uint len32
= sizeof(xfs_efi_log_format_32_t
) +
241 (src_efi_fmt
->efi_nextents
- 1) * sizeof(xfs_extent_32_t
);
242 uint len64
= sizeof(xfs_efi_log_format_64_t
) +
243 (src_efi_fmt
->efi_nextents
- 1) * sizeof(xfs_extent_64_t
);
245 if (buf
->i_len
== len
) {
246 memcpy((char *)dst_efi_fmt
, (char*)src_efi_fmt
, len
);
248 } else if (buf
->i_len
== len32
) {
249 xfs_efi_log_format_32_t
*src_efi_fmt_32
= buf
->i_addr
;
251 dst_efi_fmt
->efi_type
= src_efi_fmt_32
->efi_type
;
252 dst_efi_fmt
->efi_size
= src_efi_fmt_32
->efi_size
;
253 dst_efi_fmt
->efi_nextents
= src_efi_fmt_32
->efi_nextents
;
254 dst_efi_fmt
->efi_id
= src_efi_fmt_32
->efi_id
;
255 for (i
= 0; i
< dst_efi_fmt
->efi_nextents
; i
++) {
256 dst_efi_fmt
->efi_extents
[i
].ext_start
=
257 src_efi_fmt_32
->efi_extents
[i
].ext_start
;
258 dst_efi_fmt
->efi_extents
[i
].ext_len
=
259 src_efi_fmt_32
->efi_extents
[i
].ext_len
;
262 } else if (buf
->i_len
== len64
) {
263 xfs_efi_log_format_64_t
*src_efi_fmt_64
= buf
->i_addr
;
265 dst_efi_fmt
->efi_type
= src_efi_fmt_64
->efi_type
;
266 dst_efi_fmt
->efi_size
= src_efi_fmt_64
->efi_size
;
267 dst_efi_fmt
->efi_nextents
= src_efi_fmt_64
->efi_nextents
;
268 dst_efi_fmt
->efi_id
= src_efi_fmt_64
->efi_id
;
269 for (i
= 0; i
< dst_efi_fmt
->efi_nextents
; i
++) {
270 dst_efi_fmt
->efi_extents
[i
].ext_start
=
271 src_efi_fmt_64
->efi_extents
[i
].ext_start
;
272 dst_efi_fmt
->efi_extents
[i
].ext_len
=
273 src_efi_fmt_64
->efi_extents
[i
].ext_len
;
277 return -EFSCORRUPTED
;
281 * Freeing the efi requires that we remove it from the AIL if it has already
282 * been placed there. However, the EFI may not yet have been placed in the AIL
283 * when called by xfs_efi_release() from EFD processing due to the ordering of
284 * committed vs unpin operations in bulk insert operations. Hence the reference
285 * count to ensure only the last caller frees the EFI.
289 struct xfs_efi_log_item
*efip
)
291 if (atomic_dec_and_test(&efip
->efi_refcount
)) {
292 xfs_trans_ail_remove(&efip
->efi_item
, SHUTDOWN_LOG_IO_ERROR
);
293 xfs_efi_item_free(efip
);
297 static inline struct xfs_efd_log_item
*EFD_ITEM(struct xfs_log_item
*lip
)
299 return container_of(lip
, struct xfs_efd_log_item
, efd_item
);
303 xfs_efd_item_free(struct xfs_efd_log_item
*efdp
)
305 kmem_free(efdp
->efd_item
.li_lv_shadow
);
306 if (efdp
->efd_format
.efd_nextents
> XFS_EFD_MAX_FAST_EXTENTS
)
309 kmem_zone_free(xfs_efd_zone
, efdp
);
313 * This returns the number of iovecs needed to log the given efd item.
314 * We only need 1 iovec for an efd item. It just logs the efd_log_format
319 struct xfs_efd_log_item
*efdp
)
321 return sizeof(xfs_efd_log_format_t
) +
322 (efdp
->efd_format
.efd_nextents
- 1) * sizeof(xfs_extent_t
);
327 struct xfs_log_item
*lip
,
332 *nbytes
+= xfs_efd_item_sizeof(EFD_ITEM(lip
));
336 * This is called to fill in the vector of log iovecs for the
337 * given efd log item. We use only 1 iovec, and we point that
338 * at the efd_log_format structure embedded in the efd item.
339 * It is at this point that we assert that all of the extent
340 * slots in the efd item have been filled.
344 struct xfs_log_item
*lip
,
345 struct xfs_log_vec
*lv
)
347 struct xfs_efd_log_item
*efdp
= EFD_ITEM(lip
);
348 struct xfs_log_iovec
*vecp
= NULL
;
350 ASSERT(efdp
->efd_next_extent
== efdp
->efd_format
.efd_nextents
);
352 efdp
->efd_format
.efd_type
= XFS_LI_EFD
;
353 efdp
->efd_format
.efd_size
= 1;
355 xlog_copy_iovec(lv
, &vecp
, XLOG_REG_TYPE_EFD_FORMAT
,
357 xfs_efd_item_sizeof(efdp
));
361 * Pinning has no meaning for an efd item, so just return.
365 struct xfs_log_item
*lip
)
370 * Since pinning has no meaning for an efd item, unpinning does
375 struct xfs_log_item
*lip
,
381 * There isn't much you can do to push on an efd item. It is simply stuck
382 * waiting for the log to be flushed to disk.
386 struct xfs_log_item
*lip
,
387 struct list_head
*buffer_list
)
389 return XFS_ITEM_PINNED
;
393 * The EFD is either committed or aborted if the transaction is cancelled. If
394 * the transaction is cancelled, drop our reference to the EFI and free the EFD.
398 struct xfs_log_item
*lip
)
400 struct xfs_efd_log_item
*efdp
= EFD_ITEM(lip
);
402 if (lip
->li_flags
& XFS_LI_ABORTED
) {
403 xfs_efi_release(efdp
->efd_efip
);
404 xfs_efd_item_free(efdp
);
409 * When the efd item is committed to disk, all we need to do is delete our
410 * reference to our partner efi item and then free ourselves. Since we're
411 * freeing ourselves we must return -1 to keep the transaction code from further
412 * referencing this item.
415 xfs_efd_item_committed(
416 struct xfs_log_item
*lip
,
419 struct xfs_efd_log_item
*efdp
= EFD_ITEM(lip
);
422 * Drop the EFI reference regardless of whether the EFD has been
423 * aborted. Once the EFD transaction is constructed, it is the sole
424 * responsibility of the EFD to release the EFI (even if the EFI is
425 * aborted due to log I/O error).
427 xfs_efi_release(efdp
->efd_efip
);
428 xfs_efd_item_free(efdp
);
430 return (xfs_lsn_t
)-1;
434 * The EFD dependency tracking op doesn't do squat. It can't because
435 * it doesn't know where the free extent is coming from. The dependency
436 * tracking has to be handled by the "enclosing" metadata object. For
437 * example, for inodes, the inode is locked throughout the extent freeing
438 * so the dependency should be recorded there.
441 xfs_efd_item_committing(
442 struct xfs_log_item
*lip
,
448 * This is the ops vector shared by all efd log items.
450 static const struct xfs_item_ops xfs_efd_item_ops
= {
451 .iop_size
= xfs_efd_item_size
,
452 .iop_format
= xfs_efd_item_format
,
453 .iop_pin
= xfs_efd_item_pin
,
454 .iop_unpin
= xfs_efd_item_unpin
,
455 .iop_unlock
= xfs_efd_item_unlock
,
456 .iop_committed
= xfs_efd_item_committed
,
457 .iop_push
= xfs_efd_item_push
,
458 .iop_committing
= xfs_efd_item_committing
462 * Allocate and initialize an efd item with the given number of extents.
464 struct xfs_efd_log_item
*
466 struct xfs_mount
*mp
,
467 struct xfs_efi_log_item
*efip
,
471 struct xfs_efd_log_item
*efdp
;
474 ASSERT(nextents
> 0);
475 if (nextents
> XFS_EFD_MAX_FAST_EXTENTS
) {
476 size
= (uint
)(sizeof(xfs_efd_log_item_t
) +
477 ((nextents
- 1) * sizeof(xfs_extent_t
)));
478 efdp
= kmem_zalloc(size
, KM_SLEEP
);
480 efdp
= kmem_zone_zalloc(xfs_efd_zone
, KM_SLEEP
);
483 xfs_log_item_init(mp
, &efdp
->efd_item
, XFS_LI_EFD
, &xfs_efd_item_ops
);
484 efdp
->efd_efip
= efip
;
485 efdp
->efd_format
.efd_nextents
= nextents
;
486 efdp
->efd_format
.efd_efi_id
= efip
->efi_format
.efi_id
;
492 * Process an extent free intent item that was recovered from
493 * the log. We need to free the extents that it describes.
497 struct xfs_mount
*mp
,
498 struct xfs_efi_log_item
*efip
)
500 struct xfs_efd_log_item
*efdp
;
501 struct xfs_trans
*tp
;
505 xfs_fsblock_t startblock_fsb
;
507 ASSERT(!test_bit(XFS_EFI_RECOVERED
, &efip
->efi_flags
));
510 * First check the validity of the extents described by the
511 * EFI. If any are bad, then assume that all are bad and
514 for (i
= 0; i
< efip
->efi_format
.efi_nextents
; i
++) {
515 extp
= &(efip
->efi_format
.efi_extents
[i
]);
516 startblock_fsb
= XFS_BB_TO_FSB(mp
,
517 XFS_FSB_TO_DADDR(mp
, extp
->ext_start
));
518 if ((startblock_fsb
== 0) ||
519 (extp
->ext_len
== 0) ||
520 (startblock_fsb
>= mp
->m_sb
.sb_dblocks
) ||
521 (extp
->ext_len
>= mp
->m_sb
.sb_agblocks
)) {
523 * This will pull the EFI from the AIL and
524 * free the memory associated with it.
526 set_bit(XFS_EFI_RECOVERED
, &efip
->efi_flags
);
527 xfs_efi_release(efip
);
532 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_itruncate
, 0, 0, 0, &tp
);
535 efdp
= xfs_trans_get_efd(tp
, efip
, efip
->efi_format
.efi_nextents
);
537 for (i
= 0; i
< efip
->efi_format
.efi_nextents
; i
++) {
538 extp
= &(efip
->efi_format
.efi_extents
[i
]);
539 error
= xfs_trans_free_extent(tp
, efdp
, extp
->ext_start
,
546 set_bit(XFS_EFI_RECOVERED
, &efip
->efi_flags
);
547 error
= xfs_trans_commit(tp
);
551 xfs_trans_cancel(tp
);