Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / fs / xfs / xfs_extfree_item.c
blob132bd07b9bb8abb5663e97e2190f3ee075260d61
1 /*
2 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_types.h"
21 #include "xfs_log.h"
22 #include "xfs_inum.h"
23 #include "xfs_trans.h"
24 #include "xfs_buf_item.h"
25 #include "xfs_sb.h"
26 #include "xfs_ag.h"
27 #include "xfs_dmapi.h"
28 #include "xfs_mount.h"
29 #include "xfs_trans_priv.h"
30 #include "xfs_extfree_item.h"
33 kmem_zone_t *xfs_efi_zone;
34 kmem_zone_t *xfs_efd_zone;
36 STATIC void xfs_efi_item_unlock(xfs_efi_log_item_t *);
38 void
39 xfs_efi_item_free(xfs_efi_log_item_t *efip)
41 int nexts = efip->efi_format.efi_nextents;
43 if (nexts > XFS_EFI_MAX_FAST_EXTENTS) {
44 kmem_free(efip, sizeof(xfs_efi_log_item_t) +
45 (nexts - 1) * sizeof(xfs_extent_t));
46 } else {
47 kmem_zone_free(xfs_efi_zone, efip);
52 * This returns the number of iovecs needed to log the given efi item.
53 * We only need 1 iovec for an efi item. It just logs the efi_log_format
54 * structure.
56 /*ARGSUSED*/
57 STATIC uint
58 xfs_efi_item_size(xfs_efi_log_item_t *efip)
60 return 1;
64 * This is called to fill in the vector of log iovecs for the
65 * given efi log item. We use only 1 iovec, and we point that
66 * at the efi_log_format structure embedded in the efi item.
67 * It is at this point that we assert that all of the extent
68 * slots in the efi item have been filled.
70 STATIC void
71 xfs_efi_item_format(xfs_efi_log_item_t *efip,
72 xfs_log_iovec_t *log_vector)
74 uint size;
76 ASSERT(efip->efi_next_extent == efip->efi_format.efi_nextents);
78 efip->efi_format.efi_type = XFS_LI_EFI;
80 size = sizeof(xfs_efi_log_format_t);
81 size += (efip->efi_format.efi_nextents - 1) * sizeof(xfs_extent_t);
82 efip->efi_format.efi_size = 1;
84 log_vector->i_addr = (xfs_caddr_t)&(efip->efi_format);
85 log_vector->i_len = size;
86 XLOG_VEC_SET_TYPE(log_vector, XLOG_REG_TYPE_EFI_FORMAT);
87 ASSERT(size >= sizeof(xfs_efi_log_format_t));
92 * Pinning has no meaning for an efi item, so just return.
94 /*ARGSUSED*/
95 STATIC void
96 xfs_efi_item_pin(xfs_efi_log_item_t *efip)
98 return;
103 * While EFIs cannot really be pinned, the unpin operation is the
104 * last place at which the EFI is manipulated during a transaction.
105 * Here we coordinate with xfs_efi_cancel() to determine who gets to
106 * free the EFI.
108 /*ARGSUSED*/
109 STATIC void
110 xfs_efi_item_unpin(xfs_efi_log_item_t *efip, int stale)
112 xfs_mount_t *mp;
114 mp = efip->efi_item.li_mountp;
115 spin_lock(&mp->m_ail_lock);
116 if (efip->efi_flags & XFS_EFI_CANCELED) {
118 * xfs_trans_delete_ail() drops the AIL lock.
120 xfs_trans_delete_ail(mp, (xfs_log_item_t *)efip);
121 xfs_efi_item_free(efip);
122 } else {
123 efip->efi_flags |= XFS_EFI_COMMITTED;
124 spin_unlock(&mp->m_ail_lock);
129 * like unpin only we have to also clear the xaction descriptor
130 * pointing the log item if we free the item. This routine duplicates
131 * unpin because efi_flags is protected by the AIL lock. Freeing
132 * the descriptor and then calling unpin would force us to drop the AIL
133 * lock which would open up a race condition.
135 STATIC void
136 xfs_efi_item_unpin_remove(xfs_efi_log_item_t *efip, xfs_trans_t *tp)
138 xfs_mount_t *mp;
139 xfs_log_item_desc_t *lidp;
141 mp = efip->efi_item.li_mountp;
142 spin_lock(&mp->m_ail_lock);
143 if (efip->efi_flags & XFS_EFI_CANCELED) {
145 * free the xaction descriptor pointing to this item
147 lidp = xfs_trans_find_item(tp, (xfs_log_item_t *) efip);
148 xfs_trans_free_item(tp, lidp);
150 * pull the item off the AIL.
151 * xfs_trans_delete_ail() drops the AIL lock.
153 xfs_trans_delete_ail(mp, (xfs_log_item_t *)efip);
154 xfs_efi_item_free(efip);
155 } else {
156 efip->efi_flags |= XFS_EFI_COMMITTED;
157 spin_unlock(&mp->m_ail_lock);
162 * Efi items have no locking or pushing. However, since EFIs are
163 * pulled from the AIL when their corresponding EFDs are committed
164 * to disk, their situation is very similar to being pinned. Return
165 * XFS_ITEM_PINNED so that the caller will eventually flush the log.
166 * This should help in getting the EFI out of the AIL.
168 /*ARGSUSED*/
169 STATIC uint
170 xfs_efi_item_trylock(xfs_efi_log_item_t *efip)
172 return XFS_ITEM_PINNED;
176 * Efi items have no locking, so just return.
178 /*ARGSUSED*/
179 STATIC void
180 xfs_efi_item_unlock(xfs_efi_log_item_t *efip)
182 if (efip->efi_item.li_flags & XFS_LI_ABORTED)
183 xfs_efi_item_free(efip);
184 return;
188 * The EFI is logged only once and cannot be moved in the log, so
189 * simply return the lsn at which it's been logged. The canceled
190 * flag is not paid any attention here. Checking for that is delayed
191 * until the EFI is unpinned.
193 /*ARGSUSED*/
194 STATIC xfs_lsn_t
195 xfs_efi_item_committed(xfs_efi_log_item_t *efip, xfs_lsn_t lsn)
197 return lsn;
201 * There isn't much you can do to push on an efi item. It is simply
202 * stuck waiting for all of its corresponding efd items to be
203 * committed to disk.
205 /*ARGSUSED*/
206 STATIC void
207 xfs_efi_item_push(xfs_efi_log_item_t *efip)
209 return;
213 * The EFI dependency tracking op doesn't do squat. It can't because
214 * it doesn't know where the free extent is coming from. The dependency
215 * tracking has to be handled by the "enclosing" metadata object. For
216 * example, for inodes, the inode is locked throughout the extent freeing
217 * so the dependency should be recorded there.
219 /*ARGSUSED*/
220 STATIC void
221 xfs_efi_item_committing(xfs_efi_log_item_t *efip, xfs_lsn_t lsn)
223 return;
227 * This is the ops vector shared by all efi log items.
229 static struct xfs_item_ops xfs_efi_item_ops = {
230 .iop_size = (uint(*)(xfs_log_item_t*))xfs_efi_item_size,
231 .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
232 xfs_efi_item_format,
233 .iop_pin = (void(*)(xfs_log_item_t*))xfs_efi_item_pin,
234 .iop_unpin = (void(*)(xfs_log_item_t*, int))xfs_efi_item_unpin,
235 .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t *))
236 xfs_efi_item_unpin_remove,
237 .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_efi_item_trylock,
238 .iop_unlock = (void(*)(xfs_log_item_t*))xfs_efi_item_unlock,
239 .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
240 xfs_efi_item_committed,
241 .iop_push = (void(*)(xfs_log_item_t*))xfs_efi_item_push,
242 .iop_pushbuf = NULL,
243 .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
244 xfs_efi_item_committing
249 * Allocate and initialize an efi item with the given number of extents.
251 xfs_efi_log_item_t *
252 xfs_efi_init(xfs_mount_t *mp,
253 uint nextents)
256 xfs_efi_log_item_t *efip;
257 uint size;
259 ASSERT(nextents > 0);
260 if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
261 size = (uint)(sizeof(xfs_efi_log_item_t) +
262 ((nextents - 1) * sizeof(xfs_extent_t)));
263 efip = (xfs_efi_log_item_t*)kmem_zalloc(size, KM_SLEEP);
264 } else {
265 efip = (xfs_efi_log_item_t*)kmem_zone_zalloc(xfs_efi_zone,
266 KM_SLEEP);
269 efip->efi_item.li_type = XFS_LI_EFI;
270 efip->efi_item.li_ops = &xfs_efi_item_ops;
271 efip->efi_item.li_mountp = mp;
272 efip->efi_format.efi_nextents = nextents;
273 efip->efi_format.efi_id = (__psint_t)(void*)efip;
275 return (efip);
279 * Copy an EFI format buffer from the given buf, and into the destination
280 * EFI format structure.
281 * The given buffer can be in 32 bit or 64 bit form (which has different padding),
282 * one of which will be the native format for this kernel.
283 * It will handle the conversion of formats if necessary.
286 xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt)
288 xfs_efi_log_format_t *src_efi_fmt = (xfs_efi_log_format_t *)buf->i_addr;
289 uint i;
290 uint len = sizeof(xfs_efi_log_format_t) +
291 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_t);
292 uint len32 = sizeof(xfs_efi_log_format_32_t) +
293 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_32_t);
294 uint len64 = sizeof(xfs_efi_log_format_64_t) +
295 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_64_t);
297 if (buf->i_len == len) {
298 memcpy((char *)dst_efi_fmt, (char*)src_efi_fmt, len);
299 return 0;
300 } else if (buf->i_len == len32) {
301 xfs_efi_log_format_32_t *src_efi_fmt_32 =
302 (xfs_efi_log_format_32_t *)buf->i_addr;
304 dst_efi_fmt->efi_type = src_efi_fmt_32->efi_type;
305 dst_efi_fmt->efi_size = src_efi_fmt_32->efi_size;
306 dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents;
307 dst_efi_fmt->efi_id = src_efi_fmt_32->efi_id;
308 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
309 dst_efi_fmt->efi_extents[i].ext_start =
310 src_efi_fmt_32->efi_extents[i].ext_start;
311 dst_efi_fmt->efi_extents[i].ext_len =
312 src_efi_fmt_32->efi_extents[i].ext_len;
314 return 0;
315 } else if (buf->i_len == len64) {
316 xfs_efi_log_format_64_t *src_efi_fmt_64 =
317 (xfs_efi_log_format_64_t *)buf->i_addr;
319 dst_efi_fmt->efi_type = src_efi_fmt_64->efi_type;
320 dst_efi_fmt->efi_size = src_efi_fmt_64->efi_size;
321 dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents;
322 dst_efi_fmt->efi_id = src_efi_fmt_64->efi_id;
323 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
324 dst_efi_fmt->efi_extents[i].ext_start =
325 src_efi_fmt_64->efi_extents[i].ext_start;
326 dst_efi_fmt->efi_extents[i].ext_len =
327 src_efi_fmt_64->efi_extents[i].ext_len;
329 return 0;
331 return EFSCORRUPTED;
335 * This is called by the efd item code below to release references to
336 * the given efi item. Each efd calls this with the number of
337 * extents that it has logged, and when the sum of these reaches
338 * the total number of extents logged by this efi item we can free
339 * the efi item.
341 * Freeing the efi item requires that we remove it from the AIL.
342 * We'll use the AIL lock to protect our counters as well as
343 * the removal from the AIL.
345 void
346 xfs_efi_release(xfs_efi_log_item_t *efip,
347 uint nextents)
349 xfs_mount_t *mp;
350 int extents_left;
352 mp = efip->efi_item.li_mountp;
353 ASSERT(efip->efi_next_extent > 0);
354 ASSERT(efip->efi_flags & XFS_EFI_COMMITTED);
356 spin_lock(&mp->m_ail_lock);
357 ASSERT(efip->efi_next_extent >= nextents);
358 efip->efi_next_extent -= nextents;
359 extents_left = efip->efi_next_extent;
360 if (extents_left == 0) {
362 * xfs_trans_delete_ail() drops the AIL lock.
364 xfs_trans_delete_ail(mp, (xfs_log_item_t *)efip);
365 xfs_efi_item_free(efip);
366 } else {
367 spin_unlock(&mp->m_ail_lock);
371 STATIC void
372 xfs_efd_item_free(xfs_efd_log_item_t *efdp)
374 int nexts = efdp->efd_format.efd_nextents;
376 if (nexts > XFS_EFD_MAX_FAST_EXTENTS) {
377 kmem_free(efdp, sizeof(xfs_efd_log_item_t) +
378 (nexts - 1) * sizeof(xfs_extent_t));
379 } else {
380 kmem_zone_free(xfs_efd_zone, efdp);
385 * This returns the number of iovecs needed to log the given efd item.
386 * We only need 1 iovec for an efd item. It just logs the efd_log_format
387 * structure.
389 /*ARGSUSED*/
390 STATIC uint
391 xfs_efd_item_size(xfs_efd_log_item_t *efdp)
393 return 1;
397 * This is called to fill in the vector of log iovecs for the
398 * given efd log item. We use only 1 iovec, and we point that
399 * at the efd_log_format structure embedded in the efd item.
400 * It is at this point that we assert that all of the extent
401 * slots in the efd item have been filled.
403 STATIC void
404 xfs_efd_item_format(xfs_efd_log_item_t *efdp,
405 xfs_log_iovec_t *log_vector)
407 uint size;
409 ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
411 efdp->efd_format.efd_type = XFS_LI_EFD;
413 size = sizeof(xfs_efd_log_format_t);
414 size += (efdp->efd_format.efd_nextents - 1) * sizeof(xfs_extent_t);
415 efdp->efd_format.efd_size = 1;
417 log_vector->i_addr = (xfs_caddr_t)&(efdp->efd_format);
418 log_vector->i_len = size;
419 XLOG_VEC_SET_TYPE(log_vector, XLOG_REG_TYPE_EFD_FORMAT);
420 ASSERT(size >= sizeof(xfs_efd_log_format_t));
425 * Pinning has no meaning for an efd item, so just return.
427 /*ARGSUSED*/
428 STATIC void
429 xfs_efd_item_pin(xfs_efd_log_item_t *efdp)
431 return;
436 * Since pinning has no meaning for an efd item, unpinning does
437 * not either.
439 /*ARGSUSED*/
440 STATIC void
441 xfs_efd_item_unpin(xfs_efd_log_item_t *efdp, int stale)
443 return;
446 /*ARGSUSED*/
447 STATIC void
448 xfs_efd_item_unpin_remove(xfs_efd_log_item_t *efdp, xfs_trans_t *tp)
450 return;
454 * Efd items have no locking, so just return success.
456 /*ARGSUSED*/
457 STATIC uint
458 xfs_efd_item_trylock(xfs_efd_log_item_t *efdp)
460 return XFS_ITEM_LOCKED;
464 * Efd items have no locking or pushing, so return failure
465 * so that the caller doesn't bother with us.
467 /*ARGSUSED*/
468 STATIC void
469 xfs_efd_item_unlock(xfs_efd_log_item_t *efdp)
471 if (efdp->efd_item.li_flags & XFS_LI_ABORTED)
472 xfs_efd_item_free(efdp);
473 return;
477 * When the efd item is committed to disk, all we need to do
478 * is delete our reference to our partner efi item and then
479 * free ourselves. Since we're freeing ourselves we must
480 * return -1 to keep the transaction code from further referencing
481 * this item.
483 /*ARGSUSED*/
484 STATIC xfs_lsn_t
485 xfs_efd_item_committed(xfs_efd_log_item_t *efdp, xfs_lsn_t lsn)
488 * If we got a log I/O error, it's always the case that the LR with the
489 * EFI got unpinned and freed before the EFD got aborted.
491 if ((efdp->efd_item.li_flags & XFS_LI_ABORTED) == 0)
492 xfs_efi_release(efdp->efd_efip, efdp->efd_format.efd_nextents);
494 xfs_efd_item_free(efdp);
495 return (xfs_lsn_t)-1;
499 * There isn't much you can do to push on an efd item. It is simply
500 * stuck waiting for the log to be flushed to disk.
502 /*ARGSUSED*/
503 STATIC void
504 xfs_efd_item_push(xfs_efd_log_item_t *efdp)
506 return;
510 * The EFD dependency tracking op doesn't do squat. It can't because
511 * it doesn't know where the free extent is coming from. The dependency
512 * tracking has to be handled by the "enclosing" metadata object. For
513 * example, for inodes, the inode is locked throughout the extent freeing
514 * so the dependency should be recorded there.
516 /*ARGSUSED*/
517 STATIC void
518 xfs_efd_item_committing(xfs_efd_log_item_t *efip, xfs_lsn_t lsn)
520 return;
524 * This is the ops vector shared by all efd log items.
526 static struct xfs_item_ops xfs_efd_item_ops = {
527 .iop_size = (uint(*)(xfs_log_item_t*))xfs_efd_item_size,
528 .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
529 xfs_efd_item_format,
530 .iop_pin = (void(*)(xfs_log_item_t*))xfs_efd_item_pin,
531 .iop_unpin = (void(*)(xfs_log_item_t*, int))xfs_efd_item_unpin,
532 .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t*))
533 xfs_efd_item_unpin_remove,
534 .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_efd_item_trylock,
535 .iop_unlock = (void(*)(xfs_log_item_t*))xfs_efd_item_unlock,
536 .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
537 xfs_efd_item_committed,
538 .iop_push = (void(*)(xfs_log_item_t*))xfs_efd_item_push,
539 .iop_pushbuf = NULL,
540 .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
541 xfs_efd_item_committing
546 * Allocate and initialize an efd item with the given number of extents.
548 xfs_efd_log_item_t *
549 xfs_efd_init(xfs_mount_t *mp,
550 xfs_efi_log_item_t *efip,
551 uint nextents)
554 xfs_efd_log_item_t *efdp;
555 uint size;
557 ASSERT(nextents > 0);
558 if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
559 size = (uint)(sizeof(xfs_efd_log_item_t) +
560 ((nextents - 1) * sizeof(xfs_extent_t)));
561 efdp = (xfs_efd_log_item_t*)kmem_zalloc(size, KM_SLEEP);
562 } else {
563 efdp = (xfs_efd_log_item_t*)kmem_zone_zalloc(xfs_efd_zone,
564 KM_SLEEP);
567 efdp->efd_item.li_type = XFS_LI_EFD;
568 efdp->efd_item.li_ops = &xfs_efd_item_ops;
569 efdp->efd_item.li_mountp = mp;
570 efdp->efd_efip = efip;
571 efdp->efd_format.efd_nextents = nextents;
572 efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;
574 return (efdp);