xfs: add owner field to extent allocation and freeing
[cascardo/linux.git] / fs / xfs / xfs_trans_extfree.c
1 /*
2  * Copyright (c) 2000,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_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
24 #include "xfs_mount.h"
25 #include "xfs_defer.h"
26 #include "xfs_trans.h"
27 #include "xfs_trans_priv.h"
28 #include "xfs_extfree_item.h"
29 #include "xfs_alloc.h"
30 #include "xfs_bmap.h"
31
32 /*
33  * This routine is called to allocate an "extent free intention"
34  * log item that will hold nextents worth of extents.  The
35  * caller must use all nextents extents, because we are not
36  * flexible about this at all.
37  */
38 STATIC struct xfs_efi_log_item *
39 xfs_trans_get_efi(struct xfs_trans      *tp,
40                   uint                  nextents)
41 {
42         struct xfs_efi_log_item         *efip;
43
44         ASSERT(tp != NULL);
45         ASSERT(nextents > 0);
46
47         efip = xfs_efi_init(tp->t_mountp, nextents);
48         ASSERT(efip != NULL);
49
50         /*
51          * Get a log_item_desc to point at the new item.
52          */
53         xfs_trans_add_item(tp, &efip->efi_item);
54         return efip;
55 }
56
57 /*
58  * This routine is called to indicate that the described
59  * extent is to be logged as needing to be freed.  It should
60  * be called once for each extent to be freed.
61  */
62 STATIC void
63 xfs_trans_log_efi_extent(struct xfs_trans               *tp,
64                          struct xfs_efi_log_item        *efip,
65                          xfs_fsblock_t                  start_block,
66                          xfs_extlen_t                   ext_len)
67 {
68         uint                                            next_extent;
69         struct xfs_extent                               *extp;
70
71         tp->t_flags |= XFS_TRANS_DIRTY;
72         efip->efi_item.li_desc->lid_flags |= XFS_LID_DIRTY;
73
74         /*
75          * atomic_inc_return gives us the value after the increment;
76          * we want to use it as an array index so we need to subtract 1 from
77          * it.
78          */
79         next_extent = atomic_inc_return(&efip->efi_next_extent) - 1;
80         ASSERT(next_extent < efip->efi_format.efi_nextents);
81         extp = &(efip->efi_format.efi_extents[next_extent]);
82         extp->ext_start = start_block;
83         extp->ext_len = ext_len;
84 }
85
86
87 /*
88  * This routine is called to allocate an "extent free done"
89  * log item that will hold nextents worth of extents.  The
90  * caller must use all nextents extents, because we are not
91  * flexible about this at all.
92  */
93 struct xfs_efd_log_item *
94 xfs_trans_get_efd(struct xfs_trans              *tp,
95                   struct xfs_efi_log_item       *efip,
96                   uint                          nextents)
97 {
98         struct xfs_efd_log_item                 *efdp;
99
100         ASSERT(tp != NULL);
101         ASSERT(nextents > 0);
102
103         efdp = xfs_efd_init(tp->t_mountp, efip, nextents);
104         ASSERT(efdp != NULL);
105
106         /*
107          * Get a log_item_desc to point at the new item.
108          */
109         xfs_trans_add_item(tp, &efdp->efd_item);
110         return efdp;
111 }
112
113 /*
114  * Free an extent and log it to the EFD. Note that the transaction is marked
115  * dirty regardless of whether the extent free succeeds or fails to support the
116  * EFI/EFD lifecycle rules.
117  */
118 int
119 xfs_trans_free_extent(
120         struct xfs_trans        *tp,
121         struct xfs_efd_log_item *efdp,
122         xfs_fsblock_t           start_block,
123         xfs_extlen_t            ext_len,
124         struct xfs_owner_info   *oinfo)
125 {
126         uint                    next_extent;
127         struct xfs_extent       *extp;
128         int                     error;
129
130         error = xfs_free_extent(tp, start_block, ext_len, oinfo);
131
132         /*
133          * Mark the transaction dirty, even on error. This ensures the
134          * transaction is aborted, which:
135          *
136          * 1.) releases the EFI and frees the EFD
137          * 2.) shuts down the filesystem
138          */
139         tp->t_flags |= XFS_TRANS_DIRTY;
140         efdp->efd_item.li_desc->lid_flags |= XFS_LID_DIRTY;
141
142         next_extent = efdp->efd_next_extent;
143         ASSERT(next_extent < efdp->efd_format.efd_nextents);
144         extp = &(efdp->efd_format.efd_extents[next_extent]);
145         extp->ext_start = start_block;
146         extp->ext_len = ext_len;
147         efdp->efd_next_extent++;
148
149         return error;
150 }
151
152 /* Sort bmap items by AG. */
153 static int
154 xfs_extent_free_diff_items(
155         void                            *priv,
156         struct list_head                *a,
157         struct list_head                *b)
158 {
159         struct xfs_mount                *mp = priv;
160         struct xfs_extent_free_item     *ra;
161         struct xfs_extent_free_item     *rb;
162
163         ra = container_of(a, struct xfs_extent_free_item, xefi_list);
164         rb = container_of(b, struct xfs_extent_free_item, xefi_list);
165         return  XFS_FSB_TO_AGNO(mp, ra->xefi_startblock) -
166                 XFS_FSB_TO_AGNO(mp, rb->xefi_startblock);
167 }
168
169 /* Get an EFI. */
170 STATIC void *
171 xfs_extent_free_create_intent(
172         struct xfs_trans                *tp,
173         unsigned int                    count)
174 {
175         return xfs_trans_get_efi(tp, count);
176 }
177
178 /* Log a free extent to the intent item. */
179 STATIC void
180 xfs_extent_free_log_item(
181         struct xfs_trans                *tp,
182         void                            *intent,
183         struct list_head                *item)
184 {
185         struct xfs_extent_free_item     *free;
186
187         free = container_of(item, struct xfs_extent_free_item, xefi_list);
188         xfs_trans_log_efi_extent(tp, intent, free->xefi_startblock,
189                         free->xefi_blockcount);
190 }
191
192 /* Get an EFD so we can process all the free extents. */
193 STATIC void *
194 xfs_extent_free_create_done(
195         struct xfs_trans                *tp,
196         void                            *intent,
197         unsigned int                    count)
198 {
199         return xfs_trans_get_efd(tp, intent, count);
200 }
201
202 /* Process a free extent. */
203 STATIC int
204 xfs_extent_free_finish_item(
205         struct xfs_trans                *tp,
206         struct xfs_defer_ops            *dop,
207         struct list_head                *item,
208         void                            *done_item,
209         void                            **state)
210 {
211         struct xfs_extent_free_item     *free;
212         int                             error;
213
214         free = container_of(item, struct xfs_extent_free_item, xefi_list);
215         error = xfs_trans_free_extent(tp, done_item,
216                         free->xefi_startblock,
217                         free->xefi_blockcount,
218                         &free->xefi_oinfo);
219         kmem_free(free);
220         return error;
221 }
222
223 /* Abort all pending EFIs. */
224 STATIC void
225 xfs_extent_free_abort_intent(
226         void                            *intent)
227 {
228         xfs_efi_release(intent);
229 }
230
231 /* Cancel a free extent. */
232 STATIC void
233 xfs_extent_free_cancel_item(
234         struct list_head                *item)
235 {
236         struct xfs_extent_free_item     *free;
237
238         free = container_of(item, struct xfs_extent_free_item, xefi_list);
239         kmem_free(free);
240 }
241
242 static const struct xfs_defer_op_type xfs_extent_free_defer_type = {
243         .type           = XFS_DEFER_OPS_TYPE_FREE,
244         .max_items      = XFS_EFI_MAX_FAST_EXTENTS,
245         .diff_items     = xfs_extent_free_diff_items,
246         .create_intent  = xfs_extent_free_create_intent,
247         .abort_intent   = xfs_extent_free_abort_intent,
248         .log_item       = xfs_extent_free_log_item,
249         .create_done    = xfs_extent_free_create_done,
250         .finish_item    = xfs_extent_free_finish_item,
251         .cancel_item    = xfs_extent_free_cancel_item,
252 };
253
254 /* Register the deferred op type. */
255 void
256 xfs_extent_free_init_defer_op(void)
257 {
258         xfs_defer_init_op_type(&xfs_extent_free_defer_type);
259 }