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