[PATCH] Kexec: Kexec on panic fix with nmi watchdog enabled
[linux-2.6/sactl.git] / fs / xfs / xfs_extfree_item.c
blobdb7cbd1bc8573e81d5f05699032b7731573a5bf2
1 /*
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:
26 * http://www.sgi.com
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.
38 #include "xfs.h"
40 #include "xfs_macros.h"
41 #include "xfs_types.h"
42 #include "xfs_inum.h"
43 #include "xfs_log.h"
44 #include "xfs_trans.h"
45 #include "xfs_buf_item.h"
46 #include "xfs_sb.h"
47 #include "xfs_dir.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 *);
62 void
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));
70 } else {
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
78 * structure.
80 /*ARGSUSED*/
81 STATIC uint
82 xfs_efi_item_size(xfs_efi_log_item_t *efip)
84 return 1;
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.
94 STATIC void
95 xfs_efi_item_format(xfs_efi_log_item_t *efip,
96 xfs_log_iovec_t *log_vector)
98 uint size;
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.
117 /*ARGSUSED*/
118 STATIC void
119 xfs_efi_item_pin(xfs_efi_log_item_t *efip)
121 return;
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
129 * free the EFI.
131 /*ARGSUSED*/
132 STATIC void
133 xfs_efi_item_unpin(xfs_efi_log_item_t *efip, int stale)
135 xfs_mount_t *mp;
136 SPLDECL(s);
138 mp = efip->efi_item.li_mountp;
139 AIL_LOCK(mp, s);
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);
146 } else {
147 efip->efi_flags |= XFS_EFI_COMMITTED;
148 AIL_UNLOCK(mp, s);
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.
159 STATIC void
160 xfs_efi_item_unpin_remove(xfs_efi_log_item_t *efip, xfs_trans_t *tp)
162 xfs_mount_t *mp;
163 xfs_log_item_desc_t *lidp;
164 SPLDECL(s);
166 mp = efip->efi_item.li_mountp;
167 AIL_LOCK(mp, s);
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);
180 } else {
181 efip->efi_flags |= XFS_EFI_COMMITTED;
182 AIL_UNLOCK(mp, s);
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.
193 /*ARGSUSED*/
194 STATIC uint
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.
203 /*ARGSUSED*/
204 STATIC void
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);
209 return;
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.
218 /*ARGSUSED*/
219 STATIC xfs_lsn_t
220 xfs_efi_item_committed(xfs_efi_log_item_t *efip, xfs_lsn_t lsn)
222 return 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.
231 STATIC void
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
240 * committed to disk.
242 /*ARGSUSED*/
243 STATIC void
244 xfs_efi_item_push(xfs_efi_log_item_t *efip)
246 return;
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.
256 /*ARGSUSED*/
257 STATIC void
258 xfs_efi_item_committing(xfs_efi_log_item_t *efip, xfs_lsn_t lsn)
260 return;
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*))
269 xfs_efi_item_format,
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,
280 .iop_pushbuf = NULL,
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.
289 xfs_efi_log_item_t *
290 xfs_efi_init(xfs_mount_t *mp,
291 uint nextents)
294 xfs_efi_log_item_t *efip;
295 uint size;
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);
302 } else {
303 efip = (xfs_efi_log_item_t*)kmem_zone_zalloc(xfs_efi_zone,
304 KM_SLEEP);
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;
313 return (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
321 * the efi item.
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.
327 void
328 xfs_efi_release(xfs_efi_log_item_t *efip,
329 uint nextents)
331 xfs_mount_t *mp;
332 int extents_left;
333 SPLDECL(s);
335 mp = efip->efi_item.li_mountp;
336 ASSERT(efip->efi_next_extent > 0);
337 ASSERT(efip->efi_flags & XFS_EFI_COMMITTED);
339 AIL_LOCK(mp, s);
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);
349 } else {
350 AIL_UNLOCK(mp, s);
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.
360 STATIC void
361 xfs_efi_cancel(
362 xfs_efi_log_item_t *efip)
364 xfs_mount_t *mp;
365 SPLDECL(s);
367 mp = efip->efi_item.li_mountp;
368 AIL_LOCK(mp, s);
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);
375 } else {
376 efip->efi_flags |= XFS_EFI_CANCELED;
377 AIL_UNLOCK(mp, s);
381 STATIC void
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));
389 } else {
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
397 * structure.
399 /*ARGSUSED*/
400 STATIC uint
401 xfs_efd_item_size(xfs_efd_log_item_t *efdp)
403 return 1;
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.
413 STATIC void
414 xfs_efd_item_format(xfs_efd_log_item_t *efdp,
415 xfs_log_iovec_t *log_vector)
417 uint size;
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.
436 /*ARGSUSED*/
437 STATIC void
438 xfs_efd_item_pin(xfs_efd_log_item_t *efdp)
440 return;
445 * Since pinning has no meaning for an efd item, unpinning does
446 * not either.
448 /*ARGSUSED*/
449 STATIC void
450 xfs_efd_item_unpin(xfs_efd_log_item_t *efdp, int stale)
452 return;
455 /*ARGSUSED*/
456 STATIC void
457 xfs_efd_item_unpin_remove(xfs_efd_log_item_t *efdp, xfs_trans_t *tp)
459 return;
463 * Efd items have no locking, so just return success.
465 /*ARGSUSED*/
466 STATIC uint
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.
476 /*ARGSUSED*/
477 STATIC void
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);
482 return;
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
490 * this item.
492 /*ARGSUSED*/
493 STATIC xfs_lsn_t
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.
514 STATIC void
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.
532 /*ARGSUSED*/
533 STATIC void
534 xfs_efd_item_push(xfs_efd_log_item_t *efdp)
536 return;
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.
546 /*ARGSUSED*/
547 STATIC void
548 xfs_efd_item_committing(xfs_efd_log_item_t *efip, xfs_lsn_t lsn)
550 return;
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*))
559 xfs_efd_item_format,
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,
570 .iop_pushbuf = NULL,
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.
579 xfs_efd_log_item_t *
580 xfs_efd_init(xfs_mount_t *mp,
581 xfs_efi_log_item_t *efip,
582 uint nextents)
585 xfs_efd_log_item_t *efdp;
586 uint size;
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);
593 } else {
594 efdp = (xfs_efd_log_item_t*)kmem_zone_zalloc(xfs_efd_zone,
595 KM_SLEEP);
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;
605 return (efdp);