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