2 * Copyright (c) 2000-2001 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
34 * This file contains the implementation of the xfs_efi_log_item
35 * and xfs_efd_log_item items.
40 #include "xfs_macros.h"
41 #include "xfs_types.h"
44 #include "xfs_trans.h"
45 #include "xfs_buf_item.h"
48 #include "xfs_dmapi.h"
49 #include "xfs_mount.h"
50 #include "xfs_trans_priv.h"
51 #include "xfs_extfree_item.h"
54 kmem_zone_t *xfs_efi_zone;
55 kmem_zone_t *xfs_efd_zone;
57 STATIC void xfs_efi_item_unlock(xfs_efi_log_item_t *);
58 STATIC void xfs_efi_item_abort(xfs_efi_log_item_t *);
59 STATIC void xfs_efd_item_abort(xfs_efd_log_item_t *);
64 * This returns the number of iovecs needed to log the given efi item.
65 * We only need 1 iovec for an efi item. It just logs the efi_log_format
70 xfs_efi_item_size(xfs_efi_log_item_t *efip)
76 * This is called to fill in the vector of log iovecs for the
77 * given efi log item. We use only 1 iovec, and we point that
78 * at the efi_log_format structure embedded in the efi item.
79 * It is at this point that we assert that all of the extent
80 * slots in the efi item have been filled.
83 xfs_efi_item_format(xfs_efi_log_item_t *efip,
84 xfs_log_iovec_t *log_vector)
88 ASSERT(efip->efi_next_extent == efip->efi_format.efi_nextents);
90 efip->efi_format.efi_type = XFS_LI_EFI;
92 size = sizeof(xfs_efi_log_format_t);
93 size += (efip->efi_format.efi_nextents - 1) * sizeof(xfs_extent_t);
94 efip->efi_format.efi_size = 1;
96 log_vector->i_addr = (xfs_caddr_t)&(efip->efi_format);
97 log_vector->i_len = size;
98 ASSERT(size >= sizeof(xfs_efi_log_format_t));
103 * Pinning has no meaning for an efi item, so just return.
107 xfs_efi_item_pin(xfs_efi_log_item_t *efip)
114 * While EFIs cannot really be pinned, the unpin operation is the
115 * last place at which the EFI is manipulated during a transaction.
116 * Here we coordinate with xfs_efi_cancel() to determine who gets to
121 xfs_efi_item_unpin(xfs_efi_log_item_t *efip, int stale)
128 mp = efip->efi_item.li_mountp;
130 if (efip->efi_flags & XFS_EFI_CANCELED) {
132 * xfs_trans_delete_ail() drops the AIL lock.
134 xfs_trans_delete_ail(mp, (xfs_log_item_t *)efip, s);
136 nexts = efip->efi_format.efi_nextents;
137 if (nexts > XFS_EFI_MAX_FAST_EXTENTS) {
138 size = sizeof(xfs_efi_log_item_t);
139 size += (nexts - 1) * sizeof(xfs_extent_t);
140 kmem_free(efip, size);
142 kmem_zone_free(xfs_efi_zone, efip);
145 efip->efi_flags |= XFS_EFI_COMMITTED;
153 * like unpin only we have to also clear the xaction descriptor
154 * pointing the log item if we free the item. This routine duplicates
155 * unpin because efi_flags is protected by the AIL lock. Freeing
156 * the descriptor and then calling unpin would force us to drop the AIL
157 * lock which would open up a race condition.
160 xfs_efi_item_unpin_remove(xfs_efi_log_item_t *efip, xfs_trans_t *tp)
165 xfs_log_item_desc_t *lidp;
168 mp = efip->efi_item.li_mountp;
170 if (efip->efi_flags & XFS_EFI_CANCELED) {
172 * free the xaction descriptor pointing to this item
174 lidp = xfs_trans_find_item(tp, (xfs_log_item_t *) efip);
175 xfs_trans_free_item(tp, lidp);
177 * pull the item off the AIL.
178 * xfs_trans_delete_ail() drops the AIL lock.
180 xfs_trans_delete_ail(mp, (xfs_log_item_t *)efip, s);
182 * now free the item itself
184 nexts = efip->efi_format.efi_nextents;
185 if (nexts > XFS_EFI_MAX_FAST_EXTENTS) {
186 size = sizeof(xfs_efi_log_item_t);
187 size += (nexts - 1) * sizeof(xfs_extent_t);
188 kmem_free(efip, size);
190 kmem_zone_free(xfs_efi_zone, efip);
193 efip->efi_flags |= XFS_EFI_COMMITTED;
201 * Efi items have no locking or pushing. However, since EFIs are
202 * pulled from the AIL when their corresponding EFDs are committed
203 * to disk, their situation is very similar to being pinned. Return
204 * XFS_ITEM_PINNED so that the caller will eventually flush the log.
205 * This should help in getting the EFI out of the AIL.
209 xfs_efi_item_trylock(xfs_efi_log_item_t *efip)
211 return XFS_ITEM_PINNED;
215 * Efi items have no locking, so just return.
219 xfs_efi_item_unlock(xfs_efi_log_item_t *efip)
221 if (efip->efi_item.li_flags & XFS_LI_ABORTED)
222 xfs_efi_item_abort(efip);
227 * The EFI is logged only once and cannot be moved in the log, so
228 * simply return the lsn at which it's been logged. The canceled
229 * flag is not paid any attention here. Checking for that is delayed
230 * until the EFI is unpinned.
234 xfs_efi_item_committed(xfs_efi_log_item_t *efip, xfs_lsn_t lsn)
240 * This is called when the transaction logging the EFI is aborted.
241 * Free up the EFI and return. No need to clean up the slot for
242 * the item in the transaction. That was done by the unpin code
243 * which is called prior to this routine in the abort/fs-shutdown path.
246 xfs_efi_item_abort(xfs_efi_log_item_t *efip)
251 nexts = efip->efi_format.efi_nextents;
252 if (nexts > XFS_EFI_MAX_FAST_EXTENTS) {
253 size = sizeof(xfs_efi_log_item_t);
254 size += (nexts - 1) * sizeof(xfs_extent_t);
255 kmem_free(efip, size);
257 kmem_zone_free(xfs_efi_zone, efip);
263 * There isn't much you can do to push on an efi item. It is simply
264 * stuck waiting for all of its corresponding efd items to be
269 xfs_efi_item_push(xfs_efi_log_item_t *efip)
275 * The EFI dependency tracking op doesn't do squat. It can't because
276 * it doesn't know where the free extent is coming from. The dependency
277 * tracking has to be handled by the "enclosing" metadata object. For
278 * example, for inodes, the inode is locked throughout the extent freeing
279 * so the dependency should be recorded there.
283 xfs_efi_item_committing(xfs_efi_log_item_t *efip, xfs_lsn_t lsn)
289 * This is the ops vector shared by all efi log items.
291 struct xfs_item_ops xfs_efi_item_ops = {
292 .iop_size = (uint(*)(xfs_log_item_t*))xfs_efi_item_size,
293 .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
295 .iop_pin = (void(*)(xfs_log_item_t*))xfs_efi_item_pin,
296 .iop_unpin = (void(*)(xfs_log_item_t*, int))xfs_efi_item_unpin,
297 .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t *))
298 xfs_efi_item_unpin_remove,
299 .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_efi_item_trylock,
300 .iop_unlock = (void(*)(xfs_log_item_t*))xfs_efi_item_unlock,
301 .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
302 xfs_efi_item_committed,
303 .iop_push = (void(*)(xfs_log_item_t*))xfs_efi_item_push,
304 .iop_abort = (void(*)(xfs_log_item_t*))xfs_efi_item_abort,
306 .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
307 xfs_efi_item_committing
312 * Allocate and initialize an efi item with the given number of extents.
315 xfs_efi_init(xfs_mount_t *mp,
319 xfs_efi_log_item_t *efip;
322 ASSERT(nextents > 0);
323 if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
324 size = (uint)(sizeof(xfs_efi_log_item_t) +
325 ((nextents - 1) * sizeof(xfs_extent_t)));
326 efip = (xfs_efi_log_item_t*)kmem_zalloc(size, KM_SLEEP);
328 efip = (xfs_efi_log_item_t*)kmem_zone_zalloc(xfs_efi_zone,
332 efip->efi_item.li_type = XFS_LI_EFI;
333 efip->efi_item.li_ops = &xfs_efi_item_ops;
334 efip->efi_item.li_mountp = mp;
335 efip->efi_format.efi_nextents = nextents;
336 efip->efi_format.efi_id = (__psint_t)(void*)efip;
342 * This is called by the efd item code below to release references to
343 * the given efi item. Each efd calls this with the number of
344 * extents that it has logged, and when the sum of these reaches
345 * the total number of extents logged by this efi item we can free
348 * Freeing the efi item requires that we remove it from the AIL.
349 * We'll use the AIL lock to protect our counters as well as
350 * the removal from the AIL.
353 xfs_efi_release(xfs_efi_log_item_t *efip,
362 mp = efip->efi_item.li_mountp;
363 ASSERT(efip->efi_next_extent > 0);
364 ASSERT(efip->efi_flags & XFS_EFI_COMMITTED);
367 ASSERT(efip->efi_next_extent >= nextents);
368 efip->efi_next_extent -= nextents;
369 extents_left = efip->efi_next_extent;
370 if (extents_left == 0) {
372 * xfs_trans_delete_ail() drops the AIL lock.
374 xfs_trans_delete_ail(mp, (xfs_log_item_t *)efip, s);
379 if (extents_left == 0) {
380 nexts = efip->efi_format.efi_nextents;
381 if (nexts > XFS_EFI_MAX_FAST_EXTENTS) {
382 size = sizeof(xfs_efi_log_item_t);
383 size += (nexts - 1) * sizeof(xfs_extent_t);
384 kmem_free(efip, size);
386 kmem_zone_free(xfs_efi_zone, efip);
392 * This is called when the transaction that should be committing the
393 * EFD corresponding to the given EFI is aborted. The committed and
394 * canceled flags are used to coordinate the freeing of the EFI and
395 * the references by the transaction that committed it.
399 xfs_efi_log_item_t *efip)
406 mp = efip->efi_item.li_mountp;
408 if (efip->efi_flags & XFS_EFI_COMMITTED) {
410 * xfs_trans_delete_ail() drops the AIL lock.
412 xfs_trans_delete_ail(mp, (xfs_log_item_t *)efip, s);
414 nexts = efip->efi_format.efi_nextents;
415 if (nexts > XFS_EFI_MAX_FAST_EXTENTS) {
416 size = sizeof(xfs_efi_log_item_t);
417 size += (nexts - 1) * sizeof(xfs_extent_t);
418 kmem_free(efip, size);
420 kmem_zone_free(xfs_efi_zone, efip);
423 efip->efi_flags |= XFS_EFI_CANCELED;
435 * This returns the number of iovecs needed to log the given efd item.
436 * We only need 1 iovec for an efd item. It just logs the efd_log_format
441 xfs_efd_item_size(xfs_efd_log_item_t *efdp)
447 * This is called to fill in the vector of log iovecs for the
448 * given efd log item. We use only 1 iovec, and we point that
449 * at the efd_log_format structure embedded in the efd item.
450 * It is at this point that we assert that all of the extent
451 * slots in the efd item have been filled.
454 xfs_efd_item_format(xfs_efd_log_item_t *efdp,
455 xfs_log_iovec_t *log_vector)
459 ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
461 efdp->efd_format.efd_type = XFS_LI_EFD;
463 size = sizeof(xfs_efd_log_format_t);
464 size += (efdp->efd_format.efd_nextents - 1) * sizeof(xfs_extent_t);
465 efdp->efd_format.efd_size = 1;
467 log_vector->i_addr = (xfs_caddr_t)&(efdp->efd_format);
468 log_vector->i_len = size;
469 ASSERT(size >= sizeof(xfs_efd_log_format_t));
474 * Pinning has no meaning for an efd item, so just return.
478 xfs_efd_item_pin(xfs_efd_log_item_t *efdp)
485 * Since pinning has no meaning for an efd item, unpinning does
490 xfs_efd_item_unpin(xfs_efd_log_item_t *efdp, int stale)
497 xfs_efd_item_unpin_remove(xfs_efd_log_item_t *efdp, xfs_trans_t *tp)
503 * Efd items have no locking, so just return success.
507 xfs_efd_item_trylock(xfs_efd_log_item_t *efdp)
509 return XFS_ITEM_LOCKED;
513 * Efd items have no locking or pushing, so return failure
514 * so that the caller doesn't bother with us.
518 xfs_efd_item_unlock(xfs_efd_log_item_t *efdp)
520 if (efdp->efd_item.li_flags & XFS_LI_ABORTED)
521 xfs_efd_item_abort(efdp);
526 * When the efd item is committed to disk, all we need to do
527 * is delete our reference to our partner efi item and then
528 * free ourselves. Since we're freeing ourselves we must
529 * return -1 to keep the transaction code from further referencing
534 xfs_efd_item_committed(xfs_efd_log_item_t *efdp, xfs_lsn_t lsn)
540 * If we got a log I/O error, it's always the case that the LR with the
541 * EFI got unpinned and freed before the EFD got aborted.
543 if ((efdp->efd_item.li_flags & XFS_LI_ABORTED) == 0)
544 xfs_efi_release(efdp->efd_efip, efdp->efd_format.efd_nextents);
546 nexts = efdp->efd_format.efd_nextents;
547 if (nexts > XFS_EFD_MAX_FAST_EXTENTS) {
548 size = sizeof(xfs_efd_log_item_t);
549 size += (nexts - 1) * sizeof(xfs_extent_t);
550 kmem_free(efdp, size);
552 kmem_zone_free(xfs_efd_zone, efdp);
555 return (xfs_lsn_t)-1;
559 * The transaction of which this EFD is a part has been aborted.
560 * Inform its companion EFI of this fact and then clean up after
561 * ourselves. No need to clean up the slot for the item in the
562 * transaction. That was done by the unpin code which is called
563 * prior to this routine in the abort/fs-shutdown path.
566 xfs_efd_item_abort(xfs_efd_log_item_t *efdp)
572 * If we got a log I/O error, it's always the case that the LR with the
573 * EFI got unpinned and freed before the EFD got aborted. So don't
574 * reference the EFI at all in that case.
576 if ((efdp->efd_item.li_flags & XFS_LI_ABORTED) == 0)
577 xfs_efi_cancel(efdp->efd_efip);
579 nexts = efdp->efd_format.efd_nextents;
580 if (nexts > XFS_EFD_MAX_FAST_EXTENTS) {
581 size = sizeof(xfs_efd_log_item_t);
582 size += (nexts - 1) * sizeof(xfs_extent_t);
583 kmem_free(efdp, size);
585 kmem_zone_free(xfs_efd_zone, efdp);
591 * There isn't much you can do to push on an efd item. It is simply
592 * stuck waiting for the log to be flushed to disk.
596 xfs_efd_item_push(xfs_efd_log_item_t *efdp)
602 * The EFD dependency tracking op doesn't do squat. It can't because
603 * it doesn't know where the free extent is coming from. The dependency
604 * tracking has to be handled by the "enclosing" metadata object. For
605 * example, for inodes, the inode is locked throughout the extent freeing
606 * so the dependency should be recorded there.
610 xfs_efd_item_committing(xfs_efd_log_item_t *efip, xfs_lsn_t lsn)
616 * This is the ops vector shared by all efd log items.
618 struct xfs_item_ops xfs_efd_item_ops = {
619 .iop_size = (uint(*)(xfs_log_item_t*))xfs_efd_item_size,
620 .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
622 .iop_pin = (void(*)(xfs_log_item_t*))xfs_efd_item_pin,
623 .iop_unpin = (void(*)(xfs_log_item_t*, int))xfs_efd_item_unpin,
624 .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t*))
625 xfs_efd_item_unpin_remove,
626 .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_efd_item_trylock,
627 .iop_unlock = (void(*)(xfs_log_item_t*))xfs_efd_item_unlock,
628 .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
629 xfs_efd_item_committed,
630 .iop_push = (void(*)(xfs_log_item_t*))xfs_efd_item_push,
631 .iop_abort = (void(*)(xfs_log_item_t*))xfs_efd_item_abort,
633 .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
634 xfs_efd_item_committing
639 * Allocate and initialize an efd item with the given number of extents.
642 xfs_efd_init(xfs_mount_t *mp,
643 xfs_efi_log_item_t *efip,
647 xfs_efd_log_item_t *efdp;
650 ASSERT(nextents > 0);
651 if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
652 size = (uint)(sizeof(xfs_efd_log_item_t) +
653 ((nextents - 1) * sizeof(xfs_extent_t)));
654 efdp = (xfs_efd_log_item_t*)kmem_zalloc(size, KM_SLEEP);
656 efdp = (xfs_efd_log_item_t*)kmem_zone_zalloc(xfs_efd_zone,
660 efdp->efd_item.li_type = XFS_LI_EFD;
661 efdp->efd_item.li_ops = &xfs_efd_item_ops;
662 efdp->efd_item.li_mountp = mp;
663 efdp->efd_efip = efip;
664 efdp->efd_format.efd_nextents = nextents;
665 efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;