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 #include "xfs_types.h"
37 #include "xfs_trans.h"
38 #include "xfs_buf_item.h"
41 #include "xfs_dmapi.h"
42 #include "xfs_mount.h"
43 #include "xfs_trans_priv.h"
44 #include "xfs_extfree_item.h"
47 kmem_zone_t
*xfs_efi_zone
;
48 kmem_zone_t
*xfs_efd_zone
;
50 STATIC
void xfs_efi_item_unlock(xfs_efi_log_item_t
*);
51 STATIC
void xfs_efi_item_abort(xfs_efi_log_item_t
*);
52 STATIC
void xfs_efd_item_abort(xfs_efd_log_item_t
*);
56 xfs_efi_item_free(xfs_efi_log_item_t
*efip
)
58 int nexts
= efip
->efi_format
.efi_nextents
;
60 if (nexts
> XFS_EFI_MAX_FAST_EXTENTS
) {
61 kmem_free(efip
, sizeof(xfs_efi_log_item_t
) +
62 (nexts
- 1) * sizeof(xfs_extent_t
));
64 kmem_zone_free(xfs_efi_zone
, efip
);
69 * This returns the number of iovecs needed to log the given efi item.
70 * We only need 1 iovec for an efi item. It just logs the efi_log_format
75 xfs_efi_item_size(xfs_efi_log_item_t
*efip
)
81 * This is called to fill in the vector of log iovecs for the
82 * given efi log item. We use only 1 iovec, and we point that
83 * at the efi_log_format structure embedded in the efi item.
84 * It is at this point that we assert that all of the extent
85 * slots in the efi item have been filled.
88 xfs_efi_item_format(xfs_efi_log_item_t
*efip
,
89 xfs_log_iovec_t
*log_vector
)
93 ASSERT(efip
->efi_next_extent
== efip
->efi_format
.efi_nextents
);
95 efip
->efi_format
.efi_type
= XFS_LI_EFI
;
97 size
= sizeof(xfs_efi_log_format_t
);
98 size
+= (efip
->efi_format
.efi_nextents
- 1) * sizeof(xfs_extent_t
);
99 efip
->efi_format
.efi_size
= 1;
101 log_vector
->i_addr
= (xfs_caddr_t
)&(efip
->efi_format
);
102 log_vector
->i_len
= size
;
103 XLOG_VEC_SET_TYPE(log_vector
, XLOG_REG_TYPE_EFI_FORMAT
);
104 ASSERT(size
>= sizeof(xfs_efi_log_format_t
));
109 * Pinning has no meaning for an efi item, so just return.
113 xfs_efi_item_pin(xfs_efi_log_item_t
*efip
)
120 * While EFIs cannot really be pinned, the unpin operation is the
121 * last place at which the EFI is manipulated during a transaction.
122 * Here we coordinate with xfs_efi_cancel() to determine who gets to
127 xfs_efi_item_unpin(xfs_efi_log_item_t
*efip
, int stale
)
132 mp
= efip
->efi_item
.li_mountp
;
134 if (efip
->efi_flags
& XFS_EFI_CANCELED
) {
136 * xfs_trans_delete_ail() drops the AIL lock.
138 xfs_trans_delete_ail(mp
, (xfs_log_item_t
*)efip
, s
);
139 xfs_efi_item_free(efip
);
141 efip
->efi_flags
|= XFS_EFI_COMMITTED
;
147 * like unpin only we have to also clear the xaction descriptor
148 * pointing the log item if we free the item. This routine duplicates
149 * unpin because efi_flags is protected by the AIL lock. Freeing
150 * the descriptor and then calling unpin would force us to drop the AIL
151 * lock which would open up a race condition.
154 xfs_efi_item_unpin_remove(xfs_efi_log_item_t
*efip
, xfs_trans_t
*tp
)
157 xfs_log_item_desc_t
*lidp
;
160 mp
= efip
->efi_item
.li_mountp
;
162 if (efip
->efi_flags
& XFS_EFI_CANCELED
) {
164 * free the xaction descriptor pointing to this item
166 lidp
= xfs_trans_find_item(tp
, (xfs_log_item_t
*) efip
);
167 xfs_trans_free_item(tp
, lidp
);
169 * pull the item off the AIL.
170 * xfs_trans_delete_ail() drops the AIL lock.
172 xfs_trans_delete_ail(mp
, (xfs_log_item_t
*)efip
, s
);
173 xfs_efi_item_free(efip
);
175 efip
->efi_flags
|= XFS_EFI_COMMITTED
;
181 * Efi items have no locking or pushing. However, since EFIs are
182 * pulled from the AIL when their corresponding EFDs are committed
183 * to disk, their situation is very similar to being pinned. Return
184 * XFS_ITEM_PINNED so that the caller will eventually flush the log.
185 * This should help in getting the EFI out of the AIL.
189 xfs_efi_item_trylock(xfs_efi_log_item_t
*efip
)
191 return XFS_ITEM_PINNED
;
195 * Efi items have no locking, so just return.
199 xfs_efi_item_unlock(xfs_efi_log_item_t
*efip
)
201 if (efip
->efi_item
.li_flags
& XFS_LI_ABORTED
)
202 xfs_efi_item_abort(efip
);
207 * The EFI is logged only once and cannot be moved in the log, so
208 * simply return the lsn at which it's been logged. The canceled
209 * flag is not paid any attention here. Checking for that is delayed
210 * until the EFI is unpinned.
214 xfs_efi_item_committed(xfs_efi_log_item_t
*efip
, xfs_lsn_t lsn
)
220 * This is called when the transaction logging the EFI is aborted.
221 * Free up the EFI and return. No need to clean up the slot for
222 * the item in the transaction. That was done by the unpin code
223 * which is called prior to this routine in the abort/fs-shutdown path.
226 xfs_efi_item_abort(xfs_efi_log_item_t
*efip
)
228 xfs_efi_item_free(efip
);
232 * There isn't much you can do to push on an efi item. It is simply
233 * stuck waiting for all of its corresponding efd items to be
238 xfs_efi_item_push(xfs_efi_log_item_t
*efip
)
244 * The EFI dependency tracking op doesn't do squat. It can't because
245 * it doesn't know where the free extent is coming from. The dependency
246 * tracking has to be handled by the "enclosing" metadata object. For
247 * example, for inodes, the inode is locked throughout the extent freeing
248 * so the dependency should be recorded there.
252 xfs_efi_item_committing(xfs_efi_log_item_t
*efip
, xfs_lsn_t lsn
)
258 * This is the ops vector shared by all efi log items.
260 STATIC
struct xfs_item_ops xfs_efi_item_ops
= {
261 .iop_size
= (uint(*)(xfs_log_item_t
*))xfs_efi_item_size
,
262 .iop_format
= (void(*)(xfs_log_item_t
*, xfs_log_iovec_t
*))
264 .iop_pin
= (void(*)(xfs_log_item_t
*))xfs_efi_item_pin
,
265 .iop_unpin
= (void(*)(xfs_log_item_t
*, int))xfs_efi_item_unpin
,
266 .iop_unpin_remove
= (void(*)(xfs_log_item_t
*, xfs_trans_t
*))
267 xfs_efi_item_unpin_remove
,
268 .iop_trylock
= (uint(*)(xfs_log_item_t
*))xfs_efi_item_trylock
,
269 .iop_unlock
= (void(*)(xfs_log_item_t
*))xfs_efi_item_unlock
,
270 .iop_committed
= (xfs_lsn_t(*)(xfs_log_item_t
*, xfs_lsn_t
))
271 xfs_efi_item_committed
,
272 .iop_push
= (void(*)(xfs_log_item_t
*))xfs_efi_item_push
,
273 .iop_abort
= (void(*)(xfs_log_item_t
*))xfs_efi_item_abort
,
275 .iop_committing
= (void(*)(xfs_log_item_t
*, xfs_lsn_t
))
276 xfs_efi_item_committing
281 * Allocate and initialize an efi item with the given number of extents.
284 xfs_efi_init(xfs_mount_t
*mp
,
288 xfs_efi_log_item_t
*efip
;
291 ASSERT(nextents
> 0);
292 if (nextents
> XFS_EFI_MAX_FAST_EXTENTS
) {
293 size
= (uint
)(sizeof(xfs_efi_log_item_t
) +
294 ((nextents
- 1) * sizeof(xfs_extent_t
)));
295 efip
= (xfs_efi_log_item_t
*)kmem_zalloc(size
, KM_SLEEP
);
297 efip
= (xfs_efi_log_item_t
*)kmem_zone_zalloc(xfs_efi_zone
,
301 efip
->efi_item
.li_type
= XFS_LI_EFI
;
302 efip
->efi_item
.li_ops
= &xfs_efi_item_ops
;
303 efip
->efi_item
.li_mountp
= mp
;
304 efip
->efi_format
.efi_nextents
= nextents
;
305 efip
->efi_format
.efi_id
= (__psint_t
)(void*)efip
;
311 * This is called by the efd item code below to release references to
312 * the given efi item. Each efd calls this with the number of
313 * extents that it has logged, and when the sum of these reaches
314 * the total number of extents logged by this efi item we can free
317 * Freeing the efi item requires that we remove it from the AIL.
318 * We'll use the AIL lock to protect our counters as well as
319 * the removal from the AIL.
322 xfs_efi_release(xfs_efi_log_item_t
*efip
,
329 mp
= efip
->efi_item
.li_mountp
;
330 ASSERT(efip
->efi_next_extent
> 0);
331 ASSERT(efip
->efi_flags
& XFS_EFI_COMMITTED
);
334 ASSERT(efip
->efi_next_extent
>= nextents
);
335 efip
->efi_next_extent
-= nextents
;
336 extents_left
= efip
->efi_next_extent
;
337 if (extents_left
== 0) {
339 * xfs_trans_delete_ail() drops the AIL lock.
341 xfs_trans_delete_ail(mp
, (xfs_log_item_t
*)efip
, s
);
342 xfs_efi_item_free(efip
);
349 * This is called when the transaction that should be committing the
350 * EFD corresponding to the given EFI is aborted. The committed and
351 * canceled flags are used to coordinate the freeing of the EFI and
352 * the references by the transaction that committed it.
356 xfs_efi_log_item_t
*efip
)
361 mp
= efip
->efi_item
.li_mountp
;
363 if (efip
->efi_flags
& XFS_EFI_COMMITTED
) {
365 * xfs_trans_delete_ail() drops the AIL lock.
367 xfs_trans_delete_ail(mp
, (xfs_log_item_t
*)efip
, s
);
368 xfs_efi_item_free(efip
);
370 efip
->efi_flags
|= XFS_EFI_CANCELED
;
376 xfs_efd_item_free(xfs_efd_log_item_t
*efdp
)
378 int nexts
= efdp
->efd_format
.efd_nextents
;
380 if (nexts
> XFS_EFD_MAX_FAST_EXTENTS
) {
381 kmem_free(efdp
, sizeof(xfs_efd_log_item_t
) +
382 (nexts
- 1) * sizeof(xfs_extent_t
));
384 kmem_zone_free(xfs_efd_zone
, efdp
);
389 * This returns the number of iovecs needed to log the given efd item.
390 * We only need 1 iovec for an efd item. It just logs the efd_log_format
395 xfs_efd_item_size(xfs_efd_log_item_t
*efdp
)
401 * This is called to fill in the vector of log iovecs for the
402 * given efd log item. We use only 1 iovec, and we point that
403 * at the efd_log_format structure embedded in the efd item.
404 * It is at this point that we assert that all of the extent
405 * slots in the efd item have been filled.
408 xfs_efd_item_format(xfs_efd_log_item_t
*efdp
,
409 xfs_log_iovec_t
*log_vector
)
413 ASSERT(efdp
->efd_next_extent
== efdp
->efd_format
.efd_nextents
);
415 efdp
->efd_format
.efd_type
= XFS_LI_EFD
;
417 size
= sizeof(xfs_efd_log_format_t
);
418 size
+= (efdp
->efd_format
.efd_nextents
- 1) * sizeof(xfs_extent_t
);
419 efdp
->efd_format
.efd_size
= 1;
421 log_vector
->i_addr
= (xfs_caddr_t
)&(efdp
->efd_format
);
422 log_vector
->i_len
= size
;
423 XLOG_VEC_SET_TYPE(log_vector
, XLOG_REG_TYPE_EFD_FORMAT
);
424 ASSERT(size
>= sizeof(xfs_efd_log_format_t
));
429 * Pinning has no meaning for an efd item, so just return.
433 xfs_efd_item_pin(xfs_efd_log_item_t
*efdp
)
440 * Since pinning has no meaning for an efd item, unpinning does
445 xfs_efd_item_unpin(xfs_efd_log_item_t
*efdp
, int stale
)
452 xfs_efd_item_unpin_remove(xfs_efd_log_item_t
*efdp
, xfs_trans_t
*tp
)
458 * Efd items have no locking, so just return success.
462 xfs_efd_item_trylock(xfs_efd_log_item_t
*efdp
)
464 return XFS_ITEM_LOCKED
;
468 * Efd items have no locking or pushing, so return failure
469 * so that the caller doesn't bother with us.
473 xfs_efd_item_unlock(xfs_efd_log_item_t
*efdp
)
475 if (efdp
->efd_item
.li_flags
& XFS_LI_ABORTED
)
476 xfs_efd_item_abort(efdp
);
481 * When the efd item is committed to disk, all we need to do
482 * is delete our reference to our partner efi item and then
483 * free ourselves. Since we're freeing ourselves we must
484 * return -1 to keep the transaction code from further referencing
489 xfs_efd_item_committed(xfs_efd_log_item_t
*efdp
, xfs_lsn_t lsn
)
492 * If we got a log I/O error, it's always the case that the LR with the
493 * EFI got unpinned and freed before the EFD got aborted.
495 if ((efdp
->efd_item
.li_flags
& XFS_LI_ABORTED
) == 0)
496 xfs_efi_release(efdp
->efd_efip
, efdp
->efd_format
.efd_nextents
);
498 xfs_efd_item_free(efdp
);
499 return (xfs_lsn_t
)-1;
503 * The transaction of which this EFD is a part has been aborted.
504 * Inform its companion EFI of this fact and then clean up after
505 * ourselves. No need to clean up the slot for the item in the
506 * transaction. That was done by the unpin code which is called
507 * prior to this routine in the abort/fs-shutdown path.
510 xfs_efd_item_abort(xfs_efd_log_item_t
*efdp
)
513 * If we got a log I/O error, it's always the case that the LR with the
514 * EFI got unpinned and freed before the EFD got aborted. So don't
515 * reference the EFI at all in that case.
517 if ((efdp
->efd_item
.li_flags
& XFS_LI_ABORTED
) == 0)
518 xfs_efi_cancel(efdp
->efd_efip
);
520 xfs_efd_item_free(efdp
);
524 * There isn't much you can do to push on an efd item. It is simply
525 * stuck waiting for the log to be flushed to disk.
529 xfs_efd_item_push(xfs_efd_log_item_t
*efdp
)
535 * The EFD dependency tracking op doesn't do squat. It can't because
536 * it doesn't know where the free extent is coming from. The dependency
537 * tracking has to be handled by the "enclosing" metadata object. For
538 * example, for inodes, the inode is locked throughout the extent freeing
539 * so the dependency should be recorded there.
543 xfs_efd_item_committing(xfs_efd_log_item_t
*efip
, xfs_lsn_t lsn
)
549 * This is the ops vector shared by all efd log items.
551 STATIC
struct xfs_item_ops xfs_efd_item_ops
= {
552 .iop_size
= (uint(*)(xfs_log_item_t
*))xfs_efd_item_size
,
553 .iop_format
= (void(*)(xfs_log_item_t
*, xfs_log_iovec_t
*))
555 .iop_pin
= (void(*)(xfs_log_item_t
*))xfs_efd_item_pin
,
556 .iop_unpin
= (void(*)(xfs_log_item_t
*, int))xfs_efd_item_unpin
,
557 .iop_unpin_remove
= (void(*)(xfs_log_item_t
*, xfs_trans_t
*))
558 xfs_efd_item_unpin_remove
,
559 .iop_trylock
= (uint(*)(xfs_log_item_t
*))xfs_efd_item_trylock
,
560 .iop_unlock
= (void(*)(xfs_log_item_t
*))xfs_efd_item_unlock
,
561 .iop_committed
= (xfs_lsn_t(*)(xfs_log_item_t
*, xfs_lsn_t
))
562 xfs_efd_item_committed
,
563 .iop_push
= (void(*)(xfs_log_item_t
*))xfs_efd_item_push
,
564 .iop_abort
= (void(*)(xfs_log_item_t
*))xfs_efd_item_abort
,
566 .iop_committing
= (void(*)(xfs_log_item_t
*, xfs_lsn_t
))
567 xfs_efd_item_committing
572 * Allocate and initialize an efd item with the given number of extents.
575 xfs_efd_init(xfs_mount_t
*mp
,
576 xfs_efi_log_item_t
*efip
,
580 xfs_efd_log_item_t
*efdp
;
583 ASSERT(nextents
> 0);
584 if (nextents
> XFS_EFD_MAX_FAST_EXTENTS
) {
585 size
= (uint
)(sizeof(xfs_efd_log_item_t
) +
586 ((nextents
- 1) * sizeof(xfs_extent_t
)));
587 efdp
= (xfs_efd_log_item_t
*)kmem_zalloc(size
, KM_SLEEP
);
589 efdp
= (xfs_efd_log_item_t
*)kmem_zone_zalloc(xfs_efd_zone
,
593 efdp
->efd_item
.li_type
= XFS_LI_EFD
;
594 efdp
->efd_item
.li_ops
= &xfs_efd_item_ops
;
595 efdp
->efd_item
.li_mountp
= mp
;
596 efdp
->efd_efip
= efip
;
597 efdp
->efd_format
.efd_nextents
= nextents
;
598 efdp
->efd_format
.efd_efi_id
= efip
->efi_format
.efi_id
;