Commit | Line | Data |
---|---|---|
0b61f8a4 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 | 2 | /* |
7b718769 NS |
3 | * Copyright (c) 2000,2005 Silicon Graphics, Inc. |
4 | * All Rights Reserved. | |
1da177e4 | 5 | */ |
1da177e4 | 6 | #include "xfs.h" |
a844f451 | 7 | #include "xfs_fs.h" |
70a9883c | 8 | #include "xfs_shared.h" |
4fb6e8ad | 9 | #include "xfs_format.h" |
239880ef DC |
10 | #include "xfs_log_format.h" |
11 | #include "xfs_trans_resv.h" | |
3481b682 | 12 | #include "xfs_bit.h" |
1da177e4 | 13 | #include "xfs_mount.h" |
9749fee8 | 14 | #include "xfs_defer.h" |
239880ef | 15 | #include "xfs_trans.h" |
1da177e4 LT |
16 | #include "xfs_trans_priv.h" |
17 | #include "xfs_extfree_item.h" | |
6bc43af3 | 18 | #include "xfs_alloc.h" |
9749fee8 | 19 | #include "xfs_bmap.h" |
3481b682 | 20 | #include "xfs_trace.h" |
1da177e4 | 21 | |
1da177e4 LT |
22 | /* |
23 | * This routine is called to allocate an "extent free done" | |
24 | * log item that will hold nextents worth of extents. The | |
25 | * caller must use all nextents extents, because we are not | |
26 | * flexible about this at all. | |
27 | */ | |
bba61cbf DW |
28 | struct xfs_efd_log_item * |
29 | xfs_trans_get_efd(struct xfs_trans *tp, | |
30 | struct xfs_efi_log_item *efip, | |
31 | uint nextents) | |
1da177e4 | 32 | { |
bba61cbf | 33 | struct xfs_efd_log_item *efdp; |
1da177e4 LT |
34 | |
35 | ASSERT(tp != NULL); | |
36 | ASSERT(nextents > 0); | |
37 | ||
38 | efdp = xfs_efd_init(tp->t_mountp, efip, nextents); | |
39 | ASSERT(efdp != NULL); | |
40 | ||
41 | /* | |
42 | * Get a log_item_desc to point at the new item. | |
43 | */ | |
e98c414f CH |
44 | xfs_trans_add_item(tp, &efdp->efd_item); |
45 | return efdp; | |
1da177e4 LT |
46 | } |
47 | ||
48 | /* | |
6bc43af3 BF |
49 | * Free an extent and log it to the EFD. Note that the transaction is marked |
50 | * dirty regardless of whether the extent free succeeds or fails to support the | |
51 | * EFI/EFD lifecycle rules. | |
1da177e4 | 52 | */ |
6bc43af3 BF |
53 | int |
54 | xfs_trans_free_extent( | |
55 | struct xfs_trans *tp, | |
56 | struct xfs_efd_log_item *efdp, | |
57 | xfs_fsblock_t start_block, | |
340785cc | 58 | xfs_extlen_t ext_len, |
fcb762f5 BF |
59 | struct xfs_owner_info *oinfo, |
60 | bool skip_discard) | |
1da177e4 | 61 | { |
3481b682 | 62 | struct xfs_mount *mp = tp->t_mountp; |
1da177e4 | 63 | uint next_extent; |
3481b682 DW |
64 | xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, start_block); |
65 | xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp, start_block); | |
6bc43af3 BF |
66 | struct xfs_extent *extp; |
67 | int error; | |
1da177e4 | 68 | |
3481b682 DW |
69 | trace_xfs_bmap_free_deferred(tp->t_mountp, agno, 0, agbno, ext_len); |
70 | ||
4e529339 BF |
71 | error = __xfs_free_extent(tp, start_block, ext_len, |
72 | oinfo, XFS_AG_RESV_NONE, skip_discard); | |
6bc43af3 BF |
73 | /* |
74 | * Mark the transaction dirty, even on error. This ensures the | |
75 | * transaction is aborted, which: | |
76 | * | |
77 | * 1.) releases the EFI and frees the EFD | |
78 | * 2.) shuts down the filesystem | |
79 | */ | |
1da177e4 | 80 | tp->t_flags |= XFS_TRANS_DIRTY; |
e6631f85 | 81 | set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags); |
1da177e4 LT |
82 | |
83 | next_extent = efdp->efd_next_extent; | |
84 | ASSERT(next_extent < efdp->efd_format.efd_nextents); | |
85 | extp = &(efdp->efd_format.efd_extents[next_extent]); | |
86 | extp->ext_start = start_block; | |
87 | extp->ext_len = ext_len; | |
88 | efdp->efd_next_extent++; | |
6bc43af3 BF |
89 | |
90 | return error; | |
1da177e4 | 91 | } |
9749fee8 DW |
92 | |
93 | /* Sort bmap items by AG. */ | |
94 | static int | |
95 | xfs_extent_free_diff_items( | |
96 | void *priv, | |
97 | struct list_head *a, | |
98 | struct list_head *b) | |
99 | { | |
100 | struct xfs_mount *mp = priv; | |
310a75a3 DW |
101 | struct xfs_extent_free_item *ra; |
102 | struct xfs_extent_free_item *rb; | |
9749fee8 | 103 | |
310a75a3 DW |
104 | ra = container_of(a, struct xfs_extent_free_item, xefi_list); |
105 | rb = container_of(b, struct xfs_extent_free_item, xefi_list); | |
106 | return XFS_FSB_TO_AGNO(mp, ra->xefi_startblock) - | |
107 | XFS_FSB_TO_AGNO(mp, rb->xefi_startblock); | |
9749fee8 DW |
108 | } |
109 | ||
110 | /* Get an EFI. */ | |
111 | STATIC void * | |
112 | xfs_extent_free_create_intent( | |
113 | struct xfs_trans *tp, | |
114 | unsigned int count) | |
115 | { | |
51ce9d00 DW |
116 | struct xfs_efi_log_item *efip; |
117 | ||
118 | ASSERT(tp != NULL); | |
119 | ASSERT(count > 0); | |
120 | ||
121 | efip = xfs_efi_init(tp->t_mountp, count); | |
122 | ASSERT(efip != NULL); | |
123 | ||
124 | /* | |
125 | * Get a log_item_desc to point at the new item. | |
126 | */ | |
127 | xfs_trans_add_item(tp, &efip->efi_item); | |
128 | return efip; | |
9749fee8 DW |
129 | } |
130 | ||
131 | /* Log a free extent to the intent item. */ | |
132 | STATIC void | |
133 | xfs_extent_free_log_item( | |
134 | struct xfs_trans *tp, | |
135 | void *intent, | |
136 | struct list_head *item) | |
137 | { | |
51ce9d00 | 138 | struct xfs_efi_log_item *efip = intent; |
310a75a3 | 139 | struct xfs_extent_free_item *free; |
51ce9d00 DW |
140 | uint next_extent; |
141 | struct xfs_extent *extp; | |
9749fee8 | 142 | |
310a75a3 | 143 | free = container_of(item, struct xfs_extent_free_item, xefi_list); |
51ce9d00 DW |
144 | |
145 | tp->t_flags |= XFS_TRANS_DIRTY; | |
e6631f85 | 146 | set_bit(XFS_LI_DIRTY, &efip->efi_item.li_flags); |
51ce9d00 DW |
147 | |
148 | /* | |
149 | * atomic_inc_return gives us the value after the increment; | |
150 | * we want to use it as an array index so we need to subtract 1 from | |
151 | * it. | |
152 | */ | |
153 | next_extent = atomic_inc_return(&efip->efi_next_extent) - 1; | |
154 | ASSERT(next_extent < efip->efi_format.efi_nextents); | |
155 | extp = &efip->efi_format.efi_extents[next_extent]; | |
156 | extp->ext_start = free->xefi_startblock; | |
157 | extp->ext_len = free->xefi_blockcount; | |
9749fee8 DW |
158 | } |
159 | ||
160 | /* Get an EFD so we can process all the free extents. */ | |
161 | STATIC void * | |
162 | xfs_extent_free_create_done( | |
163 | struct xfs_trans *tp, | |
164 | void *intent, | |
165 | unsigned int count) | |
166 | { | |
167 | return xfs_trans_get_efd(tp, intent, count); | |
168 | } | |
169 | ||
170 | /* Process a free extent. */ | |
171 | STATIC int | |
172 | xfs_extent_free_finish_item( | |
173 | struct xfs_trans *tp, | |
174 | struct xfs_defer_ops *dop, | |
175 | struct list_head *item, | |
176 | void *done_item, | |
177 | void **state) | |
178 | { | |
310a75a3 | 179 | struct xfs_extent_free_item *free; |
9749fee8 DW |
180 | int error; |
181 | ||
310a75a3 | 182 | free = container_of(item, struct xfs_extent_free_item, xefi_list); |
9749fee8 | 183 | error = xfs_trans_free_extent(tp, done_item, |
310a75a3 | 184 | free->xefi_startblock, |
340785cc | 185 | free->xefi_blockcount, |
fcb762f5 | 186 | &free->xefi_oinfo, free->xefi_skip_discard); |
9749fee8 DW |
187 | kmem_free(free); |
188 | return error; | |
189 | } | |
190 | ||
191 | /* Abort all pending EFIs. */ | |
192 | STATIC void | |
193 | xfs_extent_free_abort_intent( | |
194 | void *intent) | |
195 | { | |
196 | xfs_efi_release(intent); | |
197 | } | |
198 | ||
199 | /* Cancel a free extent. */ | |
200 | STATIC void | |
201 | xfs_extent_free_cancel_item( | |
202 | struct list_head *item) | |
203 | { | |
310a75a3 | 204 | struct xfs_extent_free_item *free; |
9749fee8 | 205 | |
310a75a3 | 206 | free = container_of(item, struct xfs_extent_free_item, xefi_list); |
9749fee8 DW |
207 | kmem_free(free); |
208 | } | |
209 | ||
210 | static const struct xfs_defer_op_type xfs_extent_free_defer_type = { | |
211 | .type = XFS_DEFER_OPS_TYPE_FREE, | |
212 | .max_items = XFS_EFI_MAX_FAST_EXTENTS, | |
213 | .diff_items = xfs_extent_free_diff_items, | |
214 | .create_intent = xfs_extent_free_create_intent, | |
215 | .abort_intent = xfs_extent_free_abort_intent, | |
216 | .log_item = xfs_extent_free_log_item, | |
217 | .create_done = xfs_extent_free_create_done, | |
218 | .finish_item = xfs_extent_free_finish_item, | |
219 | .cancel_item = xfs_extent_free_cancel_item, | |
220 | }; | |
221 | ||
f8f2835a BF |
222 | /* |
223 | * AGFL blocks are accounted differently in the reserve pools and are not | |
224 | * inserted into the busy extent list. | |
225 | */ | |
226 | STATIC int | |
227 | xfs_agfl_free_finish_item( | |
228 | struct xfs_trans *tp, | |
229 | struct xfs_defer_ops *dop, | |
230 | struct list_head *item, | |
231 | void *done_item, | |
232 | void **state) | |
233 | { | |
234 | struct xfs_mount *mp = tp->t_mountp; | |
235 | struct xfs_efd_log_item *efdp = done_item; | |
236 | struct xfs_extent_free_item *free; | |
237 | struct xfs_extent *extp; | |
238 | struct xfs_buf *agbp; | |
239 | int error; | |
240 | xfs_agnumber_t agno; | |
241 | xfs_agblock_t agbno; | |
242 | uint next_extent; | |
243 | ||
244 | free = container_of(item, struct xfs_extent_free_item, xefi_list); | |
245 | ASSERT(free->xefi_blockcount == 1); | |
246 | agno = XFS_FSB_TO_AGNO(mp, free->xefi_startblock); | |
247 | agbno = XFS_FSB_TO_AGBNO(mp, free->xefi_startblock); | |
248 | ||
249 | trace_xfs_agfl_free_deferred(mp, agno, 0, agbno, free->xefi_blockcount); | |
250 | ||
251 | error = xfs_alloc_read_agf(mp, tp, agno, 0, &agbp); | |
252 | if (!error) | |
253 | error = xfs_free_agfl_block(tp, agno, agbno, agbp, | |
254 | &free->xefi_oinfo); | |
255 | ||
256 | /* | |
257 | * Mark the transaction dirty, even on error. This ensures the | |
258 | * transaction is aborted, which: | |
259 | * | |
260 | * 1.) releases the EFI and frees the EFD | |
261 | * 2.) shuts down the filesystem | |
262 | */ | |
263 | tp->t_flags |= XFS_TRANS_DIRTY; | |
e6631f85 | 264 | set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags); |
f8f2835a BF |
265 | |
266 | next_extent = efdp->efd_next_extent; | |
267 | ASSERT(next_extent < efdp->efd_format.efd_nextents); | |
268 | extp = &(efdp->efd_format.efd_extents[next_extent]); | |
269 | extp->ext_start = free->xefi_startblock; | |
270 | extp->ext_len = free->xefi_blockcount; | |
271 | efdp->efd_next_extent++; | |
272 | ||
273 | kmem_free(free); | |
274 | return error; | |
275 | } | |
276 | ||
277 | ||
278 | /* sub-type with special handling for AGFL deferred frees */ | |
279 | static const struct xfs_defer_op_type xfs_agfl_free_defer_type = { | |
280 | .type = XFS_DEFER_OPS_TYPE_AGFL_FREE, | |
281 | .max_items = XFS_EFI_MAX_FAST_EXTENTS, | |
282 | .diff_items = xfs_extent_free_diff_items, | |
283 | .create_intent = xfs_extent_free_create_intent, | |
284 | .abort_intent = xfs_extent_free_abort_intent, | |
285 | .log_item = xfs_extent_free_log_item, | |
286 | .create_done = xfs_extent_free_create_done, | |
287 | .finish_item = xfs_agfl_free_finish_item, | |
288 | .cancel_item = xfs_extent_free_cancel_item, | |
289 | }; | |
290 | ||
9749fee8 DW |
291 | /* Register the deferred op type. */ |
292 | void | |
293 | xfs_extent_free_init_defer_op(void) | |
294 | { | |
295 | xfs_defer_init_op_type(&xfs_extent_free_defer_type); | |
f8f2835a | 296 | xfs_defer_init_op_type(&xfs_agfl_free_defer_type); |
9749fee8 | 297 | } |