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_types.h"
23 #include "xfs_trans.h"
24 #include "xfs_buf_item.h"
27 #include "xfs_dmapi.h"
28 #include "xfs_mount.h"
29 #include "xfs_trans_priv.h"
30 #include "xfs_extfree_item.h"
33 kmem_zone_t *xfs_efi_zone;
34 kmem_zone_t *xfs_efd_zone;
36 STATIC void xfs_efi_item_unlock(xfs_efi_log_item_t *);
37 STATIC void xfs_efi_item_abort(xfs_efi_log_item_t *);
38 STATIC void xfs_efd_item_abort(xfs_efd_log_item_t *);
42 xfs_efi_item_free(xfs_efi_log_item_t *efip)
44 int nexts = efip->efi_format.efi_nextents;
46 if (nexts > XFS_EFI_MAX_FAST_EXTENTS) {
47 kmem_free(efip, sizeof(xfs_efi_log_item_t) +
48 (nexts - 1) * sizeof(xfs_extent_t));
50 kmem_zone_free(xfs_efi_zone, efip);
55 * This returns the number of iovecs needed to log the given efi item.
56 * We only need 1 iovec for an efi item. It just logs the efi_log_format
61 xfs_efi_item_size(xfs_efi_log_item_t *efip)
67 * This is called to fill in the vector of log iovecs for the
68 * given efi log item. We use only 1 iovec, and we point that
69 * at the efi_log_format structure embedded in the efi item.
70 * It is at this point that we assert that all of the extent
71 * slots in the efi item have been filled.
74 xfs_efi_item_format(xfs_efi_log_item_t *efip,
75 xfs_log_iovec_t *log_vector)
79 ASSERT(efip->efi_next_extent == efip->efi_format.efi_nextents);
81 efip->efi_format.efi_type = XFS_LI_EFI;
83 size = sizeof(xfs_efi_log_format_t);
84 size += (efip->efi_format.efi_nextents - 1) * sizeof(xfs_extent_t);
85 efip->efi_format.efi_size = 1;
87 log_vector->i_addr = (xfs_caddr_t)&(efip->efi_format);
88 log_vector->i_len = size;
89 XLOG_VEC_SET_TYPE(log_vector, XLOG_REG_TYPE_EFI_FORMAT);
90 ASSERT(size >= sizeof(xfs_efi_log_format_t));
95 * Pinning has no meaning for an efi item, so just return.
99 xfs_efi_item_pin(xfs_efi_log_item_t *efip)
106 * While EFIs cannot really be pinned, the unpin operation is the
107 * last place at which the EFI is manipulated during a transaction.
108 * Here we coordinate with xfs_efi_cancel() to determine who gets to
113 xfs_efi_item_unpin(xfs_efi_log_item_t *efip, int stale)
118 mp = efip->efi_item.li_mountp;
120 if (efip->efi_flags & XFS_EFI_CANCELED) {
122 * xfs_trans_delete_ail() drops the AIL lock.
124 xfs_trans_delete_ail(mp, (xfs_log_item_t *)efip, s);
125 xfs_efi_item_free(efip);
127 efip->efi_flags |= XFS_EFI_COMMITTED;
133 * like unpin only we have to also clear the xaction descriptor
134 * pointing the log item if we free the item. This routine duplicates
135 * unpin because efi_flags is protected by the AIL lock. Freeing
136 * the descriptor and then calling unpin would force us to drop the AIL
137 * lock which would open up a race condition.
140 xfs_efi_item_unpin_remove(xfs_efi_log_item_t *efip, xfs_trans_t *tp)
143 xfs_log_item_desc_t *lidp;
146 mp = efip->efi_item.li_mountp;
148 if (efip->efi_flags & XFS_EFI_CANCELED) {
150 * free the xaction descriptor pointing to this item
152 lidp = xfs_trans_find_item(tp, (xfs_log_item_t *) efip);
153 xfs_trans_free_item(tp, lidp);
155 * pull the item off the AIL.
156 * xfs_trans_delete_ail() drops the AIL lock.
158 xfs_trans_delete_ail(mp, (xfs_log_item_t *)efip, s);
159 xfs_efi_item_free(efip);
161 efip->efi_flags |= XFS_EFI_COMMITTED;
167 * Efi items have no locking or pushing. However, since EFIs are
168 * pulled from the AIL when their corresponding EFDs are committed
169 * to disk, their situation is very similar to being pinned. Return
170 * XFS_ITEM_PINNED so that the caller will eventually flush the log.
171 * This should help in getting the EFI out of the AIL.
175 xfs_efi_item_trylock(xfs_efi_log_item_t *efip)
177 return XFS_ITEM_PINNED;
181 * Efi items have no locking, so just return.
185 xfs_efi_item_unlock(xfs_efi_log_item_t *efip)
187 if (efip->efi_item.li_flags & XFS_LI_ABORTED)
188 xfs_efi_item_abort(efip);
193 * The EFI is logged only once and cannot be moved in the log, so
194 * simply return the lsn at which it's been logged. The canceled
195 * flag is not paid any attention here. Checking for that is delayed
196 * until the EFI is unpinned.
200 xfs_efi_item_committed(xfs_efi_log_item_t *efip, xfs_lsn_t lsn)
206 * This is called when the transaction logging the EFI is aborted.
207 * Free up the EFI and return. No need to clean up the slot for
208 * the item in the transaction. That was done by the unpin code
209 * which is called prior to this routine in the abort/fs-shutdown path.
212 xfs_efi_item_abort(xfs_efi_log_item_t *efip)
214 xfs_efi_item_free(efip);
218 * There isn't much you can do to push on an efi item. It is simply
219 * stuck waiting for all of its corresponding efd items to be
224 xfs_efi_item_push(xfs_efi_log_item_t *efip)
230 * The EFI dependency tracking op doesn't do squat. It can't because
231 * it doesn't know where the free extent is coming from. The dependency
232 * tracking has to be handled by the "enclosing" metadata object. For
233 * example, for inodes, the inode is locked throughout the extent freeing
234 * so the dependency should be recorded there.
238 xfs_efi_item_committing(xfs_efi_log_item_t *efip, xfs_lsn_t lsn)
244 * This is the ops vector shared by all efi log items.
246 STATIC struct xfs_item_ops xfs_efi_item_ops = {
247 .iop_size = (uint(*)(xfs_log_item_t*))xfs_efi_item_size,
248 .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
250 .iop_pin = (void(*)(xfs_log_item_t*))xfs_efi_item_pin,
251 .iop_unpin = (void(*)(xfs_log_item_t*, int))xfs_efi_item_unpin,
252 .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t *))
253 xfs_efi_item_unpin_remove,
254 .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_efi_item_trylock,
255 .iop_unlock = (void(*)(xfs_log_item_t*))xfs_efi_item_unlock,
256 .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
257 xfs_efi_item_committed,
258 .iop_push = (void(*)(xfs_log_item_t*))xfs_efi_item_push,
259 .iop_abort = (void(*)(xfs_log_item_t*))xfs_efi_item_abort,
261 .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
262 xfs_efi_item_committing
267 * Allocate and initialize an efi item with the given number of extents.
270 xfs_efi_init(xfs_mount_t *mp,
274 xfs_efi_log_item_t *efip;
277 ASSERT(nextents > 0);
278 if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
279 size = (uint)(sizeof(xfs_efi_log_item_t) +
280 ((nextents - 1) * sizeof(xfs_extent_t)));
281 efip = (xfs_efi_log_item_t*)kmem_zalloc(size, KM_SLEEP);
283 efip = (xfs_efi_log_item_t*)kmem_zone_zalloc(xfs_efi_zone,
287 efip->efi_item.li_type = XFS_LI_EFI;
288 efip->efi_item.li_ops = &xfs_efi_item_ops;
289 efip->efi_item.li_mountp = mp;
290 efip->efi_format.efi_nextents = nextents;
291 efip->efi_format.efi_id = (__psint_t)(void*)efip;
297 * Copy an EFI format buffer from the given buf, and into the destination
298 * EFI format structure.
299 * The given buffer can be in 32 bit or 64 bit form (which has different padding),
300 * one of which will be the native format for this kernel.
301 * It will handle the conversion of formats if necessary.
304 xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt)
306 xfs_efi_log_format_t *src_efi_fmt = (xfs_efi_log_format_t *)buf->i_addr;
308 uint len = sizeof(xfs_efi_log_format_t) +
309 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_t);
310 uint len32 = sizeof(xfs_efi_log_format_32_t) +
311 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_32_t);
312 uint len64 = sizeof(xfs_efi_log_format_64_t) +
313 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_64_t);
315 if (buf->i_len == len) {
316 memcpy((char *)dst_efi_fmt, (char*)src_efi_fmt, len);
318 } else if (buf->i_len == len32) {
319 xfs_efi_log_format_32_t *src_efi_fmt_32 =
320 (xfs_efi_log_format_32_t *)buf->i_addr;
322 dst_efi_fmt->efi_type = src_efi_fmt_32->efi_type;
323 dst_efi_fmt->efi_size = src_efi_fmt_32->efi_size;
324 dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents;
325 dst_efi_fmt->efi_id = src_efi_fmt_32->efi_id;
326 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
327 dst_efi_fmt->efi_extents[i].ext_start =
328 src_efi_fmt_32->efi_extents[i].ext_start;
329 dst_efi_fmt->efi_extents[i].ext_len =
330 src_efi_fmt_32->efi_extents[i].ext_len;
333 } else if (buf->i_len == len64) {
334 xfs_efi_log_format_64_t *src_efi_fmt_64 =
335 (xfs_efi_log_format_64_t *)buf->i_addr;
337 dst_efi_fmt->efi_type = src_efi_fmt_64->efi_type;
338 dst_efi_fmt->efi_size = src_efi_fmt_64->efi_size;
339 dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents;
340 dst_efi_fmt->efi_id = src_efi_fmt_64->efi_id;
341 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
342 dst_efi_fmt->efi_extents[i].ext_start =
343 src_efi_fmt_64->efi_extents[i].ext_start;
344 dst_efi_fmt->efi_extents[i].ext_len =
345 src_efi_fmt_64->efi_extents[i].ext_len;
353 * This is called by the efd item code below to release references to
354 * the given efi item. Each efd calls this with the number of
355 * extents that it has logged, and when the sum of these reaches
356 * the total number of extents logged by this efi item we can free
359 * Freeing the efi item requires that we remove it from the AIL.
360 * We'll use the AIL lock to protect our counters as well as
361 * the removal from the AIL.
364 xfs_efi_release(xfs_efi_log_item_t *efip,
371 mp = efip->efi_item.li_mountp;
372 ASSERT(efip->efi_next_extent > 0);
373 ASSERT(efip->efi_flags & XFS_EFI_COMMITTED);
376 ASSERT(efip->efi_next_extent >= nextents);
377 efip->efi_next_extent -= nextents;
378 extents_left = efip->efi_next_extent;
379 if (extents_left == 0) {
381 * xfs_trans_delete_ail() drops the AIL lock.
383 xfs_trans_delete_ail(mp, (xfs_log_item_t *)efip, s);
384 xfs_efi_item_free(efip);
391 * This is called when the transaction that should be committing the
392 * EFD corresponding to the given EFI is aborted. The committed and
393 * canceled flags are used to coordinate the freeing of the EFI and
394 * the references by the transaction that committed it.
398 xfs_efi_log_item_t *efip)
403 mp = efip->efi_item.li_mountp;
405 if (efip->efi_flags & XFS_EFI_COMMITTED) {
407 * xfs_trans_delete_ail() drops the AIL lock.
409 xfs_trans_delete_ail(mp, (xfs_log_item_t *)efip, s);
410 xfs_efi_item_free(efip);
412 efip->efi_flags |= XFS_EFI_CANCELED;
418 xfs_efd_item_free(xfs_efd_log_item_t *efdp)
420 int nexts = efdp->efd_format.efd_nextents;
422 if (nexts > XFS_EFD_MAX_FAST_EXTENTS) {
423 kmem_free(efdp, sizeof(xfs_efd_log_item_t) +
424 (nexts - 1) * sizeof(xfs_extent_t));
426 kmem_zone_free(xfs_efd_zone, efdp);
431 * This returns the number of iovecs needed to log the given efd item.
432 * We only need 1 iovec for an efd item. It just logs the efd_log_format
437 xfs_efd_item_size(xfs_efd_log_item_t *efdp)
443 * This is called to fill in the vector of log iovecs for the
444 * given efd log item. We use only 1 iovec, and we point that
445 * at the efd_log_format structure embedded in the efd item.
446 * It is at this point that we assert that all of the extent
447 * slots in the efd item have been filled.
450 xfs_efd_item_format(xfs_efd_log_item_t *efdp,
451 xfs_log_iovec_t *log_vector)
455 ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
457 efdp->efd_format.efd_type = XFS_LI_EFD;
459 size = sizeof(xfs_efd_log_format_t);
460 size += (efdp->efd_format.efd_nextents - 1) * sizeof(xfs_extent_t);
461 efdp->efd_format.efd_size = 1;
463 log_vector->i_addr = (xfs_caddr_t)&(efdp->efd_format);
464 log_vector->i_len = size;
465 XLOG_VEC_SET_TYPE(log_vector, XLOG_REG_TYPE_EFD_FORMAT);
466 ASSERT(size >= sizeof(xfs_efd_log_format_t));
471 * Pinning has no meaning for an efd item, so just return.
475 xfs_efd_item_pin(xfs_efd_log_item_t *efdp)
482 * Since pinning has no meaning for an efd item, unpinning does
487 xfs_efd_item_unpin(xfs_efd_log_item_t *efdp, int stale)
494 xfs_efd_item_unpin_remove(xfs_efd_log_item_t *efdp, xfs_trans_t *tp)
500 * Efd items have no locking, so just return success.
504 xfs_efd_item_trylock(xfs_efd_log_item_t *efdp)
506 return XFS_ITEM_LOCKED;
510 * Efd items have no locking or pushing, so return failure
511 * so that the caller doesn't bother with us.
515 xfs_efd_item_unlock(xfs_efd_log_item_t *efdp)
517 if (efdp->efd_item.li_flags & XFS_LI_ABORTED)
518 xfs_efd_item_abort(efdp);
523 * When the efd item is committed to disk, all we need to do
524 * is delete our reference to our partner efi item and then
525 * free ourselves. Since we're freeing ourselves we must
526 * return -1 to keep the transaction code from further referencing
531 xfs_efd_item_committed(xfs_efd_log_item_t *efdp, xfs_lsn_t lsn)
534 * If we got a log I/O error, it's always the case that the LR with the
535 * EFI got unpinned and freed before the EFD got aborted.
537 if ((efdp->efd_item.li_flags & XFS_LI_ABORTED) == 0)
538 xfs_efi_release(efdp->efd_efip, efdp->efd_format.efd_nextents);
540 xfs_efd_item_free(efdp);
541 return (xfs_lsn_t)-1;
545 * The transaction of which this EFD is a part has been aborted.
546 * Inform its companion EFI of this fact and then clean up after
547 * ourselves. No need to clean up the slot for the item in the
548 * transaction. That was done by the unpin code which is called
549 * prior to this routine in the abort/fs-shutdown path.
552 xfs_efd_item_abort(xfs_efd_log_item_t *efdp)
555 * If we got a log I/O error, it's always the case that the LR with the
556 * EFI got unpinned and freed before the EFD got aborted. So don't
557 * reference the EFI at all in that case.
559 if ((efdp->efd_item.li_flags & XFS_LI_ABORTED) == 0)
560 xfs_efi_cancel(efdp->efd_efip);
562 xfs_efd_item_free(efdp);
566 * There isn't much you can do to push on an efd item. It is simply
567 * stuck waiting for the log to be flushed to disk.
571 xfs_efd_item_push(xfs_efd_log_item_t *efdp)
577 * The EFD dependency tracking op doesn't do squat. It can't because
578 * it doesn't know where the free extent is coming from. The dependency
579 * tracking has to be handled by the "enclosing" metadata object. For
580 * example, for inodes, the inode is locked throughout the extent freeing
581 * so the dependency should be recorded there.
585 xfs_efd_item_committing(xfs_efd_log_item_t *efip, xfs_lsn_t lsn)
591 * This is the ops vector shared by all efd log items.
593 STATIC struct xfs_item_ops xfs_efd_item_ops = {
594 .iop_size = (uint(*)(xfs_log_item_t*))xfs_efd_item_size,
595 .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
597 .iop_pin = (void(*)(xfs_log_item_t*))xfs_efd_item_pin,
598 .iop_unpin = (void(*)(xfs_log_item_t*, int))xfs_efd_item_unpin,
599 .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t*))
600 xfs_efd_item_unpin_remove,
601 .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_efd_item_trylock,
602 .iop_unlock = (void(*)(xfs_log_item_t*))xfs_efd_item_unlock,
603 .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
604 xfs_efd_item_committed,
605 .iop_push = (void(*)(xfs_log_item_t*))xfs_efd_item_push,
606 .iop_abort = (void(*)(xfs_log_item_t*))xfs_efd_item_abort,
608 .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
609 xfs_efd_item_committing
614 * Allocate and initialize an efd item with the given number of extents.
617 xfs_efd_init(xfs_mount_t *mp,
618 xfs_efi_log_item_t *efip,
622 xfs_efd_log_item_t *efdp;
625 ASSERT(nextents > 0);
626 if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
627 size = (uint)(sizeof(xfs_efd_log_item_t) +
628 ((nextents - 1) * sizeof(xfs_extent_t)));
629 efdp = (xfs_efd_log_item_t*)kmem_zalloc(size, KM_SLEEP);
631 efdp = (xfs_efd_log_item_t*)kmem_zone_zalloc(xfs_efd_zone,
635 efdp->efd_item.li_type = XFS_LI_EFD;
636 efdp->efd_item.li_ops = &xfs_efd_item_ops;
637 efdp->efd_item.li_mountp = mp;
638 efdp->efd_efip = efip;
639 efdp->efd_format.efd_nextents = nextents;
640 efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;