Merge git://git.kernel.org/pub/scm/linux/kernel/git/mingo/linux-2.6-sched
[linux-2.6-block.git] / fs / xfs / xfs_extfree_item.c
CommitLineData
1da177e4 1/*
7b718769
NS
2 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
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
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
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.
1da177e4 13 *
7b718769
NS
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
1da177e4 17 */
1da177e4 18#include "xfs.h"
a844f451 19#include "xfs_fs.h"
1da177e4 20#include "xfs_types.h"
1da177e4 21#include "xfs_log.h"
a844f451 22#include "xfs_inum.h"
1da177e4
LT
23#include "xfs_trans.h"
24#include "xfs_buf_item.h"
25#include "xfs_sb.h"
da353b0d 26#include "xfs_ag.h"
1da177e4
LT
27#include "xfs_dmapi.h"
28#include "xfs_mount.h"
29#include "xfs_trans_priv.h"
30#include "xfs_extfree_item.h"
31
32
33kmem_zone_t *xfs_efi_zone;
34kmem_zone_t *xfs_efd_zone;
35
36STATIC void xfs_efi_item_unlock(xfs_efi_log_item_t *);
1da177e4 37
7d795ca3
CH
38void
39xfs_efi_item_free(xfs_efi_log_item_t *efip)
40{
41 int nexts = efip->efi_format.efi_nextents;
42
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);
48 }
49}
1da177e4
LT
50
51/*
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.
55 */
56/*ARGSUSED*/
57STATIC uint
58xfs_efi_item_size(xfs_efi_log_item_t *efip)
59{
60 return 1;
61}
62
63/*
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.
69 */
70STATIC void
71xfs_efi_item_format(xfs_efi_log_item_t *efip,
72 xfs_log_iovec_t *log_vector)
73{
74 uint size;
75
76 ASSERT(efip->efi_next_extent == efip->efi_format.efi_nextents);
77
78 efip->efi_format.efi_type = XFS_LI_EFI;
79
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;
83
84 log_vector->i_addr = (xfs_caddr_t)&(efip->efi_format);
85 log_vector->i_len = size;
7e9c6396 86 XLOG_VEC_SET_TYPE(log_vector, XLOG_REG_TYPE_EFI_FORMAT);
1da177e4
LT
87 ASSERT(size >= sizeof(xfs_efi_log_format_t));
88}
89
90
91/*
92 * Pinning has no meaning for an efi item, so just return.
93 */
94/*ARGSUSED*/
95STATIC void
96xfs_efi_item_pin(xfs_efi_log_item_t *efip)
97{
98 return;
99}
100
101
102/*
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.
107 */
108/*ARGSUSED*/
109STATIC void
110xfs_efi_item_unpin(xfs_efi_log_item_t *efip, int stale)
111{
1da177e4
LT
112 xfs_mount_t *mp;
113 SPLDECL(s);
114
115 mp = efip->efi_item.li_mountp;
116 AIL_LOCK(mp, s);
117 if (efip->efi_flags & XFS_EFI_CANCELED) {
118 /*
119 * xfs_trans_delete_ail() drops the AIL lock.
120 */
121 xfs_trans_delete_ail(mp, (xfs_log_item_t *)efip, s);
7d795ca3 122 xfs_efi_item_free(efip);
1da177e4
LT
123 } else {
124 efip->efi_flags |= XFS_EFI_COMMITTED;
125 AIL_UNLOCK(mp, s);
126 }
1da177e4
LT
127}
128
129/*
130 * like unpin only we have to also clear the xaction descriptor
131 * pointing the log item if we free the item. This routine duplicates
132 * unpin because efi_flags is protected by the AIL lock. Freeing
133 * the descriptor and then calling unpin would force us to drop the AIL
134 * lock which would open up a race condition.
135 */
136STATIC void
137xfs_efi_item_unpin_remove(xfs_efi_log_item_t *efip, xfs_trans_t *tp)
138{
1da177e4
LT
139 xfs_mount_t *mp;
140 xfs_log_item_desc_t *lidp;
141 SPLDECL(s);
142
143 mp = efip->efi_item.li_mountp;
144 AIL_LOCK(mp, s);
145 if (efip->efi_flags & XFS_EFI_CANCELED) {
146 /*
147 * free the xaction descriptor pointing to this item
148 */
149 lidp = xfs_trans_find_item(tp, (xfs_log_item_t *) efip);
150 xfs_trans_free_item(tp, lidp);
151 /*
152 * pull the item off the AIL.
153 * xfs_trans_delete_ail() drops the AIL lock.
154 */
155 xfs_trans_delete_ail(mp, (xfs_log_item_t *)efip, s);
7d795ca3 156 xfs_efi_item_free(efip);
1da177e4
LT
157 } else {
158 efip->efi_flags |= XFS_EFI_COMMITTED;
159 AIL_UNLOCK(mp, s);
160 }
1da177e4
LT
161}
162
163/*
164 * Efi items have no locking or pushing. However, since EFIs are
165 * pulled from the AIL when their corresponding EFDs are committed
166 * to disk, their situation is very similar to being pinned. Return
167 * XFS_ITEM_PINNED so that the caller will eventually flush the log.
168 * This should help in getting the EFI out of the AIL.
169 */
170/*ARGSUSED*/
171STATIC uint
172xfs_efi_item_trylock(xfs_efi_log_item_t *efip)
173{
174 return XFS_ITEM_PINNED;
175}
176
177/*
178 * Efi items have no locking, so just return.
179 */
180/*ARGSUSED*/
181STATIC void
182xfs_efi_item_unlock(xfs_efi_log_item_t *efip)
183{
184 if (efip->efi_item.li_flags & XFS_LI_ABORTED)
065d312e 185 xfs_efi_item_free(efip);
1da177e4
LT
186 return;
187}
188
189/*
190 * The EFI is logged only once and cannot be moved in the log, so
191 * simply return the lsn at which it's been logged. The canceled
192 * flag is not paid any attention here. Checking for that is delayed
193 * until the EFI is unpinned.
194 */
195/*ARGSUSED*/
196STATIC xfs_lsn_t
197xfs_efi_item_committed(xfs_efi_log_item_t *efip, xfs_lsn_t lsn)
198{
199 return lsn;
200}
201
1da177e4
LT
202/*
203 * There isn't much you can do to push on an efi item. It is simply
204 * stuck waiting for all of its corresponding efd items to be
205 * committed to disk.
206 */
207/*ARGSUSED*/
208STATIC void
209xfs_efi_item_push(xfs_efi_log_item_t *efip)
210{
211 return;
212}
213
214/*
215 * The EFI dependency tracking op doesn't do squat. It can't because
216 * it doesn't know where the free extent is coming from. The dependency
217 * tracking has to be handled by the "enclosing" metadata object. For
218 * example, for inodes, the inode is locked throughout the extent freeing
219 * so the dependency should be recorded there.
220 */
221/*ARGSUSED*/
222STATIC void
223xfs_efi_item_committing(xfs_efi_log_item_t *efip, xfs_lsn_t lsn)
224{
225 return;
226}
227
228/*
229 * This is the ops vector shared by all efi log items.
230 */
7989cb8e 231static struct xfs_item_ops xfs_efi_item_ops = {
1da177e4
LT
232 .iop_size = (uint(*)(xfs_log_item_t*))xfs_efi_item_size,
233 .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
234 xfs_efi_item_format,
235 .iop_pin = (void(*)(xfs_log_item_t*))xfs_efi_item_pin,
236 .iop_unpin = (void(*)(xfs_log_item_t*, int))xfs_efi_item_unpin,
237 .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t *))
238 xfs_efi_item_unpin_remove,
239 .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_efi_item_trylock,
240 .iop_unlock = (void(*)(xfs_log_item_t*))xfs_efi_item_unlock,
241 .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
242 xfs_efi_item_committed,
243 .iop_push = (void(*)(xfs_log_item_t*))xfs_efi_item_push,
1da177e4
LT
244 .iop_pushbuf = NULL,
245 .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
246 xfs_efi_item_committing
247};
248
249
250/*
251 * Allocate and initialize an efi item with the given number of extents.
252 */
253xfs_efi_log_item_t *
254xfs_efi_init(xfs_mount_t *mp,
255 uint nextents)
256
257{
258 xfs_efi_log_item_t *efip;
259 uint size;
260
261 ASSERT(nextents > 0);
262 if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
263 size = (uint)(sizeof(xfs_efi_log_item_t) +
264 ((nextents - 1) * sizeof(xfs_extent_t)));
265 efip = (xfs_efi_log_item_t*)kmem_zalloc(size, KM_SLEEP);
266 } else {
267 efip = (xfs_efi_log_item_t*)kmem_zone_zalloc(xfs_efi_zone,
268 KM_SLEEP);
269 }
270
271 efip->efi_item.li_type = XFS_LI_EFI;
272 efip->efi_item.li_ops = &xfs_efi_item_ops;
273 efip->efi_item.li_mountp = mp;
274 efip->efi_format.efi_nextents = nextents;
275 efip->efi_format.efi_id = (__psint_t)(void*)efip;
276
277 return (efip);
278}
279
6d192a9b
TS
280/*
281 * Copy an EFI format buffer from the given buf, and into the destination
282 * EFI format structure.
283 * The given buffer can be in 32 bit or 64 bit form (which has different padding),
284 * one of which will be the native format for this kernel.
285 * It will handle the conversion of formats if necessary.
286 */
287int
288xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt)
289{
290 xfs_efi_log_format_t *src_efi_fmt = (xfs_efi_log_format_t *)buf->i_addr;
291 uint i;
292 uint len = sizeof(xfs_efi_log_format_t) +
293 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_t);
294 uint len32 = sizeof(xfs_efi_log_format_32_t) +
295 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_32_t);
296 uint len64 = sizeof(xfs_efi_log_format_64_t) +
297 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_64_t);
298
299 if (buf->i_len == len) {
300 memcpy((char *)dst_efi_fmt, (char*)src_efi_fmt, len);
301 return 0;
302 } else if (buf->i_len == len32) {
303 xfs_efi_log_format_32_t *src_efi_fmt_32 =
304 (xfs_efi_log_format_32_t *)buf->i_addr;
305
306 dst_efi_fmt->efi_type = src_efi_fmt_32->efi_type;
307 dst_efi_fmt->efi_size = src_efi_fmt_32->efi_size;
308 dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents;
309 dst_efi_fmt->efi_id = src_efi_fmt_32->efi_id;
310 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
311 dst_efi_fmt->efi_extents[i].ext_start =
312 src_efi_fmt_32->efi_extents[i].ext_start;
313 dst_efi_fmt->efi_extents[i].ext_len =
314 src_efi_fmt_32->efi_extents[i].ext_len;
315 }
316 return 0;
317 } else if (buf->i_len == len64) {
318 xfs_efi_log_format_64_t *src_efi_fmt_64 =
319 (xfs_efi_log_format_64_t *)buf->i_addr;
320
321 dst_efi_fmt->efi_type = src_efi_fmt_64->efi_type;
322 dst_efi_fmt->efi_size = src_efi_fmt_64->efi_size;
323 dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents;
324 dst_efi_fmt->efi_id = src_efi_fmt_64->efi_id;
325 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
326 dst_efi_fmt->efi_extents[i].ext_start =
327 src_efi_fmt_64->efi_extents[i].ext_start;
328 dst_efi_fmt->efi_extents[i].ext_len =
329 src_efi_fmt_64->efi_extents[i].ext_len;
330 }
331 return 0;
332 }
333 return EFSCORRUPTED;
334}
335
1da177e4
LT
336/*
337 * This is called by the efd item code below to release references to
338 * the given efi item. Each efd calls this with the number of
339 * extents that it has logged, and when the sum of these reaches
340 * the total number of extents logged by this efi item we can free
341 * the efi item.
342 *
343 * Freeing the efi item requires that we remove it from the AIL.
344 * We'll use the AIL lock to protect our counters as well as
345 * the removal from the AIL.
346 */
347void
348xfs_efi_release(xfs_efi_log_item_t *efip,
349 uint nextents)
350{
351 xfs_mount_t *mp;
352 int extents_left;
1da177e4
LT
353 SPLDECL(s);
354
355 mp = efip->efi_item.li_mountp;
356 ASSERT(efip->efi_next_extent > 0);
357 ASSERT(efip->efi_flags & XFS_EFI_COMMITTED);
358
359 AIL_LOCK(mp, s);
360 ASSERT(efip->efi_next_extent >= nextents);
361 efip->efi_next_extent -= nextents;
362 extents_left = efip->efi_next_extent;
363 if (extents_left == 0) {
364 /*
365 * xfs_trans_delete_ail() drops the AIL lock.
366 */
367 xfs_trans_delete_ail(mp, (xfs_log_item_t *)efip, s);
7d795ca3 368 xfs_efi_item_free(efip);
1da177e4
LT
369 } else {
370 AIL_UNLOCK(mp, s);
371 }
1da177e4
LT
372}
373
7d795ca3
CH
374STATIC void
375xfs_efd_item_free(xfs_efd_log_item_t *efdp)
376{
377 int nexts = efdp->efd_format.efd_nextents;
1da177e4 378
7d795ca3
CH
379 if (nexts > XFS_EFD_MAX_FAST_EXTENTS) {
380 kmem_free(efdp, sizeof(xfs_efd_log_item_t) +
381 (nexts - 1) * sizeof(xfs_extent_t));
382 } else {
383 kmem_zone_free(xfs_efd_zone, efdp);
384 }
385}
1da177e4
LT
386
387/*
388 * This returns the number of iovecs needed to log the given efd item.
389 * We only need 1 iovec for an efd item. It just logs the efd_log_format
390 * structure.
391 */
392/*ARGSUSED*/
393STATIC uint
394xfs_efd_item_size(xfs_efd_log_item_t *efdp)
395{
396 return 1;
397}
398
399/*
400 * This is called to fill in the vector of log iovecs for the
401 * given efd log item. We use only 1 iovec, and we point that
402 * at the efd_log_format structure embedded in the efd item.
403 * It is at this point that we assert that all of the extent
404 * slots in the efd item have been filled.
405 */
406STATIC void
407xfs_efd_item_format(xfs_efd_log_item_t *efdp,
408 xfs_log_iovec_t *log_vector)
409{
410 uint size;
411
412 ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
413
414 efdp->efd_format.efd_type = XFS_LI_EFD;
415
416 size = sizeof(xfs_efd_log_format_t);
417 size += (efdp->efd_format.efd_nextents - 1) * sizeof(xfs_extent_t);
418 efdp->efd_format.efd_size = 1;
419
420 log_vector->i_addr = (xfs_caddr_t)&(efdp->efd_format);
421 log_vector->i_len = size;
7e9c6396 422 XLOG_VEC_SET_TYPE(log_vector, XLOG_REG_TYPE_EFD_FORMAT);
1da177e4
LT
423 ASSERT(size >= sizeof(xfs_efd_log_format_t));
424}
425
426
427/*
428 * Pinning has no meaning for an efd item, so just return.
429 */
430/*ARGSUSED*/
431STATIC void
432xfs_efd_item_pin(xfs_efd_log_item_t *efdp)
433{
434 return;
435}
436
437
438/*
439 * Since pinning has no meaning for an efd item, unpinning does
440 * not either.
441 */
442/*ARGSUSED*/
443STATIC void
444xfs_efd_item_unpin(xfs_efd_log_item_t *efdp, int stale)
445{
446 return;
447}
448
449/*ARGSUSED*/
450STATIC void
451xfs_efd_item_unpin_remove(xfs_efd_log_item_t *efdp, xfs_trans_t *tp)
452{
453 return;
454}
455
456/*
457 * Efd items have no locking, so just return success.
458 */
459/*ARGSUSED*/
460STATIC uint
461xfs_efd_item_trylock(xfs_efd_log_item_t *efdp)
462{
463 return XFS_ITEM_LOCKED;
464}
465
466/*
467 * Efd items have no locking or pushing, so return failure
468 * so that the caller doesn't bother with us.
469 */
470/*ARGSUSED*/
471STATIC void
472xfs_efd_item_unlock(xfs_efd_log_item_t *efdp)
473{
474 if (efdp->efd_item.li_flags & XFS_LI_ABORTED)
065d312e 475 xfs_efd_item_free(efdp);
1da177e4
LT
476 return;
477}
478
479/*
480 * When the efd item is committed to disk, all we need to do
481 * is delete our reference to our partner efi item and then
482 * free ourselves. Since we're freeing ourselves we must
483 * return -1 to keep the transaction code from further referencing
484 * this item.
485 */
486/*ARGSUSED*/
487STATIC xfs_lsn_t
488xfs_efd_item_committed(xfs_efd_log_item_t *efdp, xfs_lsn_t lsn)
489{
1da177e4
LT
490 /*
491 * If we got a log I/O error, it's always the case that the LR with the
492 * EFI got unpinned and freed before the EFD got aborted.
493 */
494 if ((efdp->efd_item.li_flags & XFS_LI_ABORTED) == 0)
495 xfs_efi_release(efdp->efd_efip, efdp->efd_format.efd_nextents);
496
7d795ca3 497 xfs_efd_item_free(efdp);
1da177e4
LT
498 return (xfs_lsn_t)-1;
499}
500
1da177e4
LT
501/*
502 * There isn't much you can do to push on an efd item. It is simply
503 * stuck waiting for the log to be flushed to disk.
504 */
505/*ARGSUSED*/
506STATIC void
507xfs_efd_item_push(xfs_efd_log_item_t *efdp)
508{
509 return;
510}
511
512/*
513 * The EFD dependency tracking op doesn't do squat. It can't because
514 * it doesn't know where the free extent is coming from. The dependency
515 * tracking has to be handled by the "enclosing" metadata object. For
516 * example, for inodes, the inode is locked throughout the extent freeing
517 * so the dependency should be recorded there.
518 */
519/*ARGSUSED*/
520STATIC void
521xfs_efd_item_committing(xfs_efd_log_item_t *efip, xfs_lsn_t lsn)
522{
523 return;
524}
525
526/*
527 * This is the ops vector shared by all efd log items.
528 */
7989cb8e 529static struct xfs_item_ops xfs_efd_item_ops = {
1da177e4
LT
530 .iop_size = (uint(*)(xfs_log_item_t*))xfs_efd_item_size,
531 .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
532 xfs_efd_item_format,
533 .iop_pin = (void(*)(xfs_log_item_t*))xfs_efd_item_pin,
534 .iop_unpin = (void(*)(xfs_log_item_t*, int))xfs_efd_item_unpin,
535 .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t*))
536 xfs_efd_item_unpin_remove,
537 .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_efd_item_trylock,
538 .iop_unlock = (void(*)(xfs_log_item_t*))xfs_efd_item_unlock,
539 .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
540 xfs_efd_item_committed,
541 .iop_push = (void(*)(xfs_log_item_t*))xfs_efd_item_push,
1da177e4
LT
542 .iop_pushbuf = NULL,
543 .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
544 xfs_efd_item_committing
545};
546
547
548/*
549 * Allocate and initialize an efd item with the given number of extents.
550 */
551xfs_efd_log_item_t *
552xfs_efd_init(xfs_mount_t *mp,
553 xfs_efi_log_item_t *efip,
554 uint nextents)
555
556{
557 xfs_efd_log_item_t *efdp;
558 uint size;
559
560 ASSERT(nextents > 0);
561 if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
562 size = (uint)(sizeof(xfs_efd_log_item_t) +
563 ((nextents - 1) * sizeof(xfs_extent_t)));
564 efdp = (xfs_efd_log_item_t*)kmem_zalloc(size, KM_SLEEP);
565 } else {
566 efdp = (xfs_efd_log_item_t*)kmem_zone_zalloc(xfs_efd_zone,
567 KM_SLEEP);
568 }
569
570 efdp->efd_item.li_type = XFS_LI_EFD;
571 efdp->efd_item.li_ops = &xfs_efd_item_ops;
572 efdp->efd_item.li_mountp = mp;
573 efdp->efd_efip = efip;
574 efdp->efd_format.efd_nextents = nextents;
575 efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;
576
577 return (efdp);
578}