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 *);
63 xfs_efi_item_free(xfs_efi_log_item_t *efip)
65 int nexts = efip->efi_format.efi_nextents;
67 if (nexts > XFS_EFI_MAX_FAST_EXTENTS) {
68 kmem_free(efip, sizeof(xfs_efi_log_item_t) +
69 (nexts - 1) * sizeof(xfs_extent_t));
71 kmem_zone_free(xfs_efi_zone, efip);
76 * This returns the number of iovecs needed to log the given efi item.
77 * We only need 1 iovec for an efi item. It just logs the efi_log_format
82 xfs_efi_item_size(xfs_efi_log_item_t *efip)
88 * This is called to fill in the vector of log iovecs for the
89 * given efi log item. We use only 1 iovec, and we point that
90 * at the efi_log_format structure embedded in the efi item.
91 * It is at this point that we assert that all of the extent
92 * slots in the efi item have been filled.
95 xfs_efi_item_format(xfs_efi_log_item_t *efip,
96 xfs_log_iovec_t *log_vector)
100 ASSERT(efip->efi_next_extent == efip->efi_format.efi_nextents);
102 efip->efi_format.efi_type = XFS_LI_EFI;
104 size = sizeof(xfs_efi_log_format_t);
105 size += (efip->efi_format.efi_nextents - 1) * sizeof(xfs_extent_t);
106 efip->efi_format.efi_size = 1;
108 log_vector->i_addr = (xfs_caddr_t)&(efip->efi_format);
109 log_vector->i_len = size;
110 ASSERT(size >= sizeof(xfs_efi_log_format_t));
115 * Pinning has no meaning for an efi item, so just return.
119 xfs_efi_item_pin(xfs_efi_log_item_t *efip)
126 * While EFIs cannot really be pinned, the unpin operation is the
127 * last place at which the EFI is manipulated during a transaction.
128 * Here we coordinate with xfs_efi_cancel() to determine who gets to
133 xfs_efi_item_unpin(xfs_efi_log_item_t *efip, int stale)
138 mp = efip->efi_item.li_mountp;
140 if (efip->efi_flags & XFS_EFI_CANCELED) {
142 * xfs_trans_delete_ail() drops the AIL lock.
144 xfs_trans_delete_ail(mp, (xfs_log_item_t *)efip, s);
145 xfs_efi_item_free(efip);
147 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)
163 xfs_log_item_desc_t *lidp;
166 mp = efip->efi_item.li_mountp;
168 if (efip->efi_flags & XFS_EFI_CANCELED) {
170 * free the xaction descriptor pointing to this item
172 lidp = xfs_trans_find_item(tp, (xfs_log_item_t *) efip);
173 xfs_trans_free_item(tp, lidp);
175 * pull the item off the AIL.
176 * xfs_trans_delete_ail() drops the AIL lock.
178 xfs_trans_delete_ail(mp, (xfs_log_item_t *)efip, s);
179 xfs_efi_item_free(efip);
181 efip->efi_flags |= XFS_EFI_COMMITTED;
187 * Efi items have no locking or pushing. However, since EFIs are
188 * pulled from the AIL when their corresponding EFDs are committed
189 * to disk, their situation is very similar to being pinned. Return
190 * XFS_ITEM_PINNED so that the caller will eventually flush the log.
191 * This should help in getting the EFI out of the AIL.
195 xfs_efi_item_trylock(xfs_efi_log_item_t *efip)
197 return XFS_ITEM_PINNED;
201 * Efi items have no locking, so just return.
205 xfs_efi_item_unlock(xfs_efi_log_item_t *efip)
207 if (efip->efi_item.li_flags & XFS_LI_ABORTED)
208 xfs_efi_item_abort(efip);
213 * The EFI is logged only once and cannot be moved in the log, so
214 * simply return the lsn at which it's been logged. The canceled
215 * flag is not paid any attention here. Checking for that is delayed
216 * until the EFI is unpinned.
220 xfs_efi_item_committed(xfs_efi_log_item_t *efip, xfs_lsn_t lsn)
226 * This is called when the transaction logging the EFI is aborted.
227 * Free up the EFI and return. No need to clean up the slot for
228 * the item in the transaction. That was done by the unpin code
229 * which is called prior to this routine in the abort/fs-shutdown path.
232 xfs_efi_item_abort(xfs_efi_log_item_t *efip)
234 xfs_efi_item_free(efip);
238 * There isn't much you can do to push on an efi item. It is simply
239 * stuck waiting for all of its corresponding efd items to be
244 xfs_efi_item_push(xfs_efi_log_item_t *efip)
250 * The EFI dependency tracking op doesn't do squat. It can't because
251 * it doesn't know where the free extent is coming from. The dependency
252 * tracking has to be handled by the "enclosing" metadata object. For
253 * example, for inodes, the inode is locked throughout the extent freeing
254 * so the dependency should be recorded there.
258 xfs_efi_item_committing(xfs_efi_log_item_t *efip, xfs_lsn_t lsn)
264 * This is the ops vector shared by all efi log items.
266 STATIC struct xfs_item_ops xfs_efi_item_ops = {
267 .iop_size = (uint(*)(xfs_log_item_t*))xfs_efi_item_size,
268 .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
270 .iop_pin = (void(*)(xfs_log_item_t*))xfs_efi_item_pin,
271 .iop_unpin = (void(*)(xfs_log_item_t*, int))xfs_efi_item_unpin,
272 .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t *))
273 xfs_efi_item_unpin_remove,
274 .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_efi_item_trylock,
275 .iop_unlock = (void(*)(xfs_log_item_t*))xfs_efi_item_unlock,
276 .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
277 xfs_efi_item_committed,
278 .iop_push = (void(*)(xfs_log_item_t*))xfs_efi_item_push,
279 .iop_abort = (void(*)(xfs_log_item_t*))xfs_efi_item_abort,
281 .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
282 xfs_efi_item_committing
287 * Allocate and initialize an efi item with the given number of extents.
290 xfs_efi_init(xfs_mount_t *mp,
294 xfs_efi_log_item_t *efip;
297 ASSERT(nextents > 0);
298 if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
299 size = (uint)(sizeof(xfs_efi_log_item_t) +
300 ((nextents - 1) * sizeof(xfs_extent_t)));
301 efip = (xfs_efi_log_item_t*)kmem_zalloc(size, KM_SLEEP);
303 efip = (xfs_efi_log_item_t*)kmem_zone_zalloc(xfs_efi_zone,
307 efip->efi_item.li_type = XFS_LI_EFI;
308 efip->efi_item.li_ops = &xfs_efi_item_ops;
309 efip->efi_item.li_mountp = mp;
310 efip->efi_format.efi_nextents = nextents;
311 efip->efi_format.efi_id = (__psint_t)(void*)efip;
317 * This is called by the efd item code below to release references to
318 * the given efi item. Each efd calls this with the number of
319 * extents that it has logged, and when the sum of these reaches
320 * the total number of extents logged by this efi item we can free
323 * Freeing the efi item requires that we remove it from the AIL.
324 * We'll use the AIL lock to protect our counters as well as
325 * the removal from the AIL.
328 xfs_efi_release(xfs_efi_log_item_t *efip,
335 mp = efip->efi_item.li_mountp;
336 ASSERT(efip->efi_next_extent > 0);
337 ASSERT(efip->efi_flags & XFS_EFI_COMMITTED);
340 ASSERT(efip->efi_next_extent >= nextents);
341 efip->efi_next_extent -= nextents;
342 extents_left = efip->efi_next_extent;
343 if (extents_left == 0) {
345 * xfs_trans_delete_ail() drops the AIL lock.
347 xfs_trans_delete_ail(mp, (xfs_log_item_t *)efip, s);
348 xfs_efi_item_free(efip);
355 * This is called when the transaction that should be committing the
356 * EFD corresponding to the given EFI is aborted. The committed and
357 * canceled flags are used to coordinate the freeing of the EFI and
358 * the references by the transaction that committed it.
362 xfs_efi_log_item_t *efip)
367 mp = efip->efi_item.li_mountp;
369 if (efip->efi_flags & XFS_EFI_COMMITTED) {
371 * xfs_trans_delete_ail() drops the AIL lock.
373 xfs_trans_delete_ail(mp, (xfs_log_item_t *)efip, s);
374 xfs_efi_item_free(efip);
376 efip->efi_flags |= XFS_EFI_CANCELED;
382 xfs_efd_item_free(xfs_efd_log_item_t *efdp)
384 int nexts = efdp->efd_format.efd_nextents;
386 if (nexts > XFS_EFD_MAX_FAST_EXTENTS) {
387 kmem_free(efdp, sizeof(xfs_efd_log_item_t) +
388 (nexts - 1) * sizeof(xfs_extent_t));
390 kmem_zone_free(xfs_efd_zone, efdp);
395 * This returns the number of iovecs needed to log the given efd item.
396 * We only need 1 iovec for an efd item. It just logs the efd_log_format
401 xfs_efd_item_size(xfs_efd_log_item_t *efdp)
407 * This is called to fill in the vector of log iovecs for the
408 * given efd log item. We use only 1 iovec, and we point that
409 * at the efd_log_format structure embedded in the efd item.
410 * It is at this point that we assert that all of the extent
411 * slots in the efd item have been filled.
414 xfs_efd_item_format(xfs_efd_log_item_t *efdp,
415 xfs_log_iovec_t *log_vector)
419 ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
421 efdp->efd_format.efd_type = XFS_LI_EFD;
423 size = sizeof(xfs_efd_log_format_t);
424 size += (efdp->efd_format.efd_nextents - 1) * sizeof(xfs_extent_t);
425 efdp->efd_format.efd_size = 1;
427 log_vector->i_addr = (xfs_caddr_t)&(efdp->efd_format);
428 log_vector->i_len = size;
429 ASSERT(size >= sizeof(xfs_efd_log_format_t));
434 * Pinning has no meaning for an efd item, so just return.
438 xfs_efd_item_pin(xfs_efd_log_item_t *efdp)
445 * Since pinning has no meaning for an efd item, unpinning does
450 xfs_efd_item_unpin(xfs_efd_log_item_t *efdp, int stale)
457 xfs_efd_item_unpin_remove(xfs_efd_log_item_t *efdp, xfs_trans_t *tp)
463 * Efd items have no locking, so just return success.
467 xfs_efd_item_trylock(xfs_efd_log_item_t *efdp)
469 return XFS_ITEM_LOCKED;
473 * Efd items have no locking or pushing, so return failure
474 * so that the caller doesn't bother with us.
478 xfs_efd_item_unlock(xfs_efd_log_item_t *efdp)
480 if (efdp->efd_item.li_flags & XFS_LI_ABORTED)
481 xfs_efd_item_abort(efdp);
486 * When the efd item is committed to disk, all we need to do
487 * is delete our reference to our partner efi item and then
488 * free ourselves. Since we're freeing ourselves we must
489 * return -1 to keep the transaction code from further referencing
494 xfs_efd_item_committed(xfs_efd_log_item_t *efdp, xfs_lsn_t lsn)
497 * If we got a log I/O error, it's always the case that the LR with the
498 * EFI got unpinned and freed before the EFD got aborted.
500 if ((efdp->efd_item.li_flags & XFS_LI_ABORTED) == 0)
501 xfs_efi_release(efdp->efd_efip, efdp->efd_format.efd_nextents);
503 xfs_efd_item_free(efdp);
504 return (xfs_lsn_t)-1;
508 * The transaction of which this EFD is a part has been aborted.
509 * Inform its companion EFI of this fact and then clean up after
510 * ourselves. No need to clean up the slot for the item in the
511 * transaction. That was done by the unpin code which is called
512 * prior to this routine in the abort/fs-shutdown path.
515 xfs_efd_item_abort(xfs_efd_log_item_t *efdp)
518 * If we got a log I/O error, it's always the case that the LR with the
519 * EFI got unpinned and freed before the EFD got aborted. So don't
520 * reference the EFI at all in that case.
522 if ((efdp->efd_item.li_flags & XFS_LI_ABORTED) == 0)
523 xfs_efi_cancel(efdp->efd_efip);
525 xfs_efd_item_free(efdp);
529 * There isn't much you can do to push on an efd item. It is simply
530 * stuck waiting for the log to be flushed to disk.
534 xfs_efd_item_push(xfs_efd_log_item_t *efdp)
540 * The EFD dependency tracking op doesn't do squat. It can't because
541 * it doesn't know where the free extent is coming from. The dependency
542 * tracking has to be handled by the "enclosing" metadata object. For
543 * example, for inodes, the inode is locked throughout the extent freeing
544 * so the dependency should be recorded there.
548 xfs_efd_item_committing(xfs_efd_log_item_t *efip, xfs_lsn_t lsn)
554 * This is the ops vector shared by all efd log items.
556 STATIC struct xfs_item_ops xfs_efd_item_ops = {
557 .iop_size = (uint(*)(xfs_log_item_t*))xfs_efd_item_size,
558 .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
560 .iop_pin = (void(*)(xfs_log_item_t*))xfs_efd_item_pin,
561 .iop_unpin = (void(*)(xfs_log_item_t*, int))xfs_efd_item_unpin,
562 .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t*))
563 xfs_efd_item_unpin_remove,
564 .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_efd_item_trylock,
565 .iop_unlock = (void(*)(xfs_log_item_t*))xfs_efd_item_unlock,
566 .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
567 xfs_efd_item_committed,
568 .iop_push = (void(*)(xfs_log_item_t*))xfs_efd_item_push,
569 .iop_abort = (void(*)(xfs_log_item_t*))xfs_efd_item_abort,
571 .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
572 xfs_efd_item_committing
577 * Allocate and initialize an efd item with the given number of extents.
580 xfs_efd_init(xfs_mount_t *mp,
581 xfs_efi_log_item_t *efip,
585 xfs_efd_log_item_t *efdp;
588 ASSERT(nextents > 0);
589 if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
590 size = (uint)(sizeof(xfs_efd_log_item_t) +
591 ((nextents - 1) * sizeof(xfs_extent_t)));
592 efdp = (xfs_efd_log_item_t*)kmem_zalloc(size, KM_SLEEP);
594 efdp = (xfs_efd_log_item_t*)kmem_zone_zalloc(xfs_efd_zone,
598 efdp->efd_item.li_type = XFS_LI_EFD;
599 efdp->efd_item.li_ops = &xfs_efd_item_ops;
600 efdp->efd_item.li_mountp = mp;
601 efdp->efd_efip = efip;
602 efdp->efd_format.efd_nextents = nextents;
603 efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;