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