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 XLOG_VEC_SET_TYPE(log_vector, XLOG_REG_TYPE_EFI_FORMAT);
111 ASSERT(size >= sizeof(xfs_efi_log_format_t));
116 * Pinning has no meaning for an efi item, so just return.
120 xfs_efi_item_pin(xfs_efi_log_item_t *efip)
127 * While EFIs cannot really be pinned, the unpin operation is the
128 * last place at which the EFI is manipulated during a transaction.
129 * Here we coordinate with xfs_efi_cancel() to determine who gets to
134 xfs_efi_item_unpin(xfs_efi_log_item_t *efip, int stale)
139 mp = efip->efi_item.li_mountp;
141 if (efip->efi_flags & XFS_EFI_CANCELED) {
143 * xfs_trans_delete_ail() drops the AIL lock.
145 xfs_trans_delete_ail(mp, (xfs_log_item_t *)efip, s);
146 xfs_efi_item_free(efip);
148 efip->efi_flags |= XFS_EFI_COMMITTED;
154 * like unpin only we have to also clear the xaction descriptor
155 * pointing the log item if we free the item. This routine duplicates
156 * unpin because efi_flags is protected by the AIL lock. Freeing
157 * the descriptor and then calling unpin would force us to drop the AIL
158 * lock which would open up a race condition.
161 xfs_efi_item_unpin_remove(xfs_efi_log_item_t *efip, xfs_trans_t *tp)
164 xfs_log_item_desc_t *lidp;
167 mp = efip->efi_item.li_mountp;
169 if (efip->efi_flags & XFS_EFI_CANCELED) {
171 * free the xaction descriptor pointing to this item
173 lidp = xfs_trans_find_item(tp, (xfs_log_item_t *) efip);
174 xfs_trans_free_item(tp, lidp);
176 * pull the item off the AIL.
177 * xfs_trans_delete_ail() drops the AIL lock.
179 xfs_trans_delete_ail(mp, (xfs_log_item_t *)efip, s);
180 xfs_efi_item_free(efip);
182 efip->efi_flags |= XFS_EFI_COMMITTED;
188 * Efi items have no locking or pushing. However, since EFIs are
189 * pulled from the AIL when their corresponding EFDs are committed
190 * to disk, their situation is very similar to being pinned. Return
191 * XFS_ITEM_PINNED so that the caller will eventually flush the log.
192 * This should help in getting the EFI out of the AIL.
196 xfs_efi_item_trylock(xfs_efi_log_item_t *efip)
198 return XFS_ITEM_PINNED;
202 * Efi items have no locking, so just return.
206 xfs_efi_item_unlock(xfs_efi_log_item_t *efip)
208 if (efip->efi_item.li_flags & XFS_LI_ABORTED)
209 xfs_efi_item_abort(efip);
214 * The EFI is logged only once and cannot be moved in the log, so
215 * simply return the lsn at which it's been logged. The canceled
216 * flag is not paid any attention here. Checking for that is delayed
217 * until the EFI is unpinned.
221 xfs_efi_item_committed(xfs_efi_log_item_t *efip, xfs_lsn_t lsn)
227 * This is called when the transaction logging the EFI is aborted.
228 * Free up the EFI and return. No need to clean up the slot for
229 * the item in the transaction. That was done by the unpin code
230 * which is called prior to this routine in the abort/fs-shutdown path.
233 xfs_efi_item_abort(xfs_efi_log_item_t *efip)
235 xfs_efi_item_free(efip);
239 * There isn't much you can do to push on an efi item. It is simply
240 * stuck waiting for all of its corresponding efd items to be
245 xfs_efi_item_push(xfs_efi_log_item_t *efip)
251 * The EFI dependency tracking op doesn't do squat. It can't because
252 * it doesn't know where the free extent is coming from. The dependency
253 * tracking has to be handled by the "enclosing" metadata object. For
254 * example, for inodes, the inode is locked throughout the extent freeing
255 * so the dependency should be recorded there.
259 xfs_efi_item_committing(xfs_efi_log_item_t *efip, xfs_lsn_t lsn)
265 * This is the ops vector shared by all efi log items.
267 STATIC struct xfs_item_ops xfs_efi_item_ops = {
268 .iop_size = (uint(*)(xfs_log_item_t*))xfs_efi_item_size,
269 .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
271 .iop_pin = (void(*)(xfs_log_item_t*))xfs_efi_item_pin,
272 .iop_unpin = (void(*)(xfs_log_item_t*, int))xfs_efi_item_unpin,
273 .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t *))
274 xfs_efi_item_unpin_remove,
275 .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_efi_item_trylock,
276 .iop_unlock = (void(*)(xfs_log_item_t*))xfs_efi_item_unlock,
277 .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
278 xfs_efi_item_committed,
279 .iop_push = (void(*)(xfs_log_item_t*))xfs_efi_item_push,
280 .iop_abort = (void(*)(xfs_log_item_t*))xfs_efi_item_abort,
282 .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
283 xfs_efi_item_committing
288 * Allocate and initialize an efi item with the given number of extents.
291 xfs_efi_init(xfs_mount_t *mp,
295 xfs_efi_log_item_t *efip;
298 ASSERT(nextents > 0);
299 if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
300 size = (uint)(sizeof(xfs_efi_log_item_t) +
301 ((nextents - 1) * sizeof(xfs_extent_t)));
302 efip = (xfs_efi_log_item_t*)kmem_zalloc(size, KM_SLEEP);
304 efip = (xfs_efi_log_item_t*)kmem_zone_zalloc(xfs_efi_zone,
308 efip->efi_item.li_type = XFS_LI_EFI;
309 efip->efi_item.li_ops = &xfs_efi_item_ops;
310 efip->efi_item.li_mountp = mp;
311 efip->efi_format.efi_nextents = nextents;
312 efip->efi_format.efi_id = (__psint_t)(void*)efip;
318 * This is called by the efd item code below to release references to
319 * the given efi item. Each efd calls this with the number of
320 * extents that it has logged, and when the sum of these reaches
321 * the total number of extents logged by this efi item we can free
324 * Freeing the efi item requires that we remove it from the AIL.
325 * We'll use the AIL lock to protect our counters as well as
326 * the removal from the AIL.
329 xfs_efi_release(xfs_efi_log_item_t *efip,
336 mp = efip->efi_item.li_mountp;
337 ASSERT(efip->efi_next_extent > 0);
338 ASSERT(efip->efi_flags & XFS_EFI_COMMITTED);
341 ASSERT(efip->efi_next_extent >= nextents);
342 efip->efi_next_extent -= nextents;
343 extents_left = efip->efi_next_extent;
344 if (extents_left == 0) {
346 * xfs_trans_delete_ail() drops the AIL lock.
348 xfs_trans_delete_ail(mp, (xfs_log_item_t *)efip, s);
349 xfs_efi_item_free(efip);
356 * This is called when the transaction that should be committing the
357 * EFD corresponding to the given EFI is aborted. The committed and
358 * canceled flags are used to coordinate the freeing of the EFI and
359 * the references by the transaction that committed it.
363 xfs_efi_log_item_t *efip)
368 mp = efip->efi_item.li_mountp;
370 if (efip->efi_flags & XFS_EFI_COMMITTED) {
372 * xfs_trans_delete_ail() drops the AIL lock.
374 xfs_trans_delete_ail(mp, (xfs_log_item_t *)efip, s);
375 xfs_efi_item_free(efip);
377 efip->efi_flags |= XFS_EFI_CANCELED;
383 xfs_efd_item_free(xfs_efd_log_item_t *efdp)
385 int nexts = efdp->efd_format.efd_nextents;
387 if (nexts > XFS_EFD_MAX_FAST_EXTENTS) {
388 kmem_free(efdp, sizeof(xfs_efd_log_item_t) +
389 (nexts - 1) * sizeof(xfs_extent_t));
391 kmem_zone_free(xfs_efd_zone, efdp);
396 * This returns the number of iovecs needed to log the given efd item.
397 * We only need 1 iovec for an efd item. It just logs the efd_log_format
402 xfs_efd_item_size(xfs_efd_log_item_t *efdp)
408 * This is called to fill in the vector of log iovecs for the
409 * given efd log item. We use only 1 iovec, and we point that
410 * at the efd_log_format structure embedded in the efd item.
411 * It is at this point that we assert that all of the extent
412 * slots in the efd item have been filled.
415 xfs_efd_item_format(xfs_efd_log_item_t *efdp,
416 xfs_log_iovec_t *log_vector)
420 ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
422 efdp->efd_format.efd_type = XFS_LI_EFD;
424 size = sizeof(xfs_efd_log_format_t);
425 size += (efdp->efd_format.efd_nextents - 1) * sizeof(xfs_extent_t);
426 efdp->efd_format.efd_size = 1;
428 log_vector->i_addr = (xfs_caddr_t)&(efdp->efd_format);
429 log_vector->i_len = size;
430 XLOG_VEC_SET_TYPE(log_vector, XLOG_REG_TYPE_EFD_FORMAT);
431 ASSERT(size >= sizeof(xfs_efd_log_format_t));
436 * Pinning has no meaning for an efd item, so just return.
440 xfs_efd_item_pin(xfs_efd_log_item_t *efdp)
447 * Since pinning has no meaning for an efd item, unpinning does
452 xfs_efd_item_unpin(xfs_efd_log_item_t *efdp, int stale)
459 xfs_efd_item_unpin_remove(xfs_efd_log_item_t *efdp, xfs_trans_t *tp)
465 * Efd items have no locking, so just return success.
469 xfs_efd_item_trylock(xfs_efd_log_item_t *efdp)
471 return XFS_ITEM_LOCKED;
475 * Efd items have no locking or pushing, so return failure
476 * so that the caller doesn't bother with us.
480 xfs_efd_item_unlock(xfs_efd_log_item_t *efdp)
482 if (efdp->efd_item.li_flags & XFS_LI_ABORTED)
483 xfs_efd_item_abort(efdp);
488 * When the efd item is committed to disk, all we need to do
489 * is delete our reference to our partner efi item and then
490 * free ourselves. Since we're freeing ourselves we must
491 * return -1 to keep the transaction code from further referencing
496 xfs_efd_item_committed(xfs_efd_log_item_t *efdp, xfs_lsn_t lsn)
499 * If we got a log I/O error, it's always the case that the LR with the
500 * EFI got unpinned and freed before the EFD got aborted.
502 if ((efdp->efd_item.li_flags & XFS_LI_ABORTED) == 0)
503 xfs_efi_release(efdp->efd_efip, efdp->efd_format.efd_nextents);
505 xfs_efd_item_free(efdp);
506 return (xfs_lsn_t)-1;
510 * The transaction of which this EFD is a part has been aborted.
511 * Inform its companion EFI of this fact and then clean up after
512 * ourselves. No need to clean up the slot for the item in the
513 * transaction. That was done by the unpin code which is called
514 * prior to this routine in the abort/fs-shutdown path.
517 xfs_efd_item_abort(xfs_efd_log_item_t *efdp)
520 * If we got a log I/O error, it's always the case that the LR with the
521 * EFI got unpinned and freed before the EFD got aborted. So don't
522 * reference the EFI at all in that case.
524 if ((efdp->efd_item.li_flags & XFS_LI_ABORTED) == 0)
525 xfs_efi_cancel(efdp->efd_efip);
527 xfs_efd_item_free(efdp);
531 * There isn't much you can do to push on an efd item. It is simply
532 * stuck waiting for the log to be flushed to disk.
536 xfs_efd_item_push(xfs_efd_log_item_t *efdp)
542 * The EFD dependency tracking op doesn't do squat. It can't because
543 * it doesn't know where the free extent is coming from. The dependency
544 * tracking has to be handled by the "enclosing" metadata object. For
545 * example, for inodes, the inode is locked throughout the extent freeing
546 * so the dependency should be recorded there.
550 xfs_efd_item_committing(xfs_efd_log_item_t *efip, xfs_lsn_t lsn)
556 * This is the ops vector shared by all efd log items.
558 STATIC struct xfs_item_ops xfs_efd_item_ops = {
559 .iop_size = (uint(*)(xfs_log_item_t*))xfs_efd_item_size,
560 .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
562 .iop_pin = (void(*)(xfs_log_item_t*))xfs_efd_item_pin,
563 .iop_unpin = (void(*)(xfs_log_item_t*, int))xfs_efd_item_unpin,
564 .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t*))
565 xfs_efd_item_unpin_remove,
566 .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_efd_item_trylock,
567 .iop_unlock = (void(*)(xfs_log_item_t*))xfs_efd_item_unlock,
568 .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
569 xfs_efd_item_committed,
570 .iop_push = (void(*)(xfs_log_item_t*))xfs_efd_item_push,
571 .iop_abort = (void(*)(xfs_log_item_t*))xfs_efd_item_abort,
573 .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
574 xfs_efd_item_committing
579 * Allocate and initialize an efd item with the given number of extents.
582 xfs_efd_init(xfs_mount_t *mp,
583 xfs_efi_log_item_t *efip,
587 xfs_efd_log_item_t *efdp;
590 ASSERT(nextents > 0);
591 if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
592 size = (uint)(sizeof(xfs_efd_log_item_t) +
593 ((nextents - 1) * sizeof(xfs_extent_t)));
594 efdp = (xfs_efd_log_item_t*)kmem_zalloc(size, KM_SLEEP);
596 efdp = (xfs_efd_log_item_t*)kmem_zone_zalloc(xfs_efd_zone,
600 efdp->efd_item.li_type = XFS_LI_EFD;
601 efdp->efd_item.li_ops = &xfs_efd_item_ops;
602 efdp->efd_item.li_mountp = mp;
603 efdp->efd_efip = efip;
604 efdp->efd_format.efd_nextents = nextents;
605 efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;