]>
Commit | Line | Data |
---|---|---|
0b61f8a4 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 | 2 | /* |
7b718769 NS |
3 | * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc. |
4 | * All Rights Reserved. | |
1da177e4 | 5 | */ |
1da177e4 | 6 | #include "xfs.h" |
a844f451 | 7 | #include "xfs_fs.h" |
4fb6e8ad | 8 | #include "xfs_format.h" |
239880ef DC |
9 | #include "xfs_log_format.h" |
10 | #include "xfs_trans_resv.h" | |
dc42375d | 11 | #include "xfs_bit.h" |
5467b34b | 12 | #include "xfs_shared.h" |
1da177e4 | 13 | #include "xfs_mount.h" |
08d3e84f | 14 | #include "xfs_ag.h" |
81f40041 | 15 | #include "xfs_defer.h" |
239880ef | 16 | #include "xfs_trans.h" |
1da177e4 LT |
17 | #include "xfs_trans_priv.h" |
18 | #include "xfs_extfree_item.h" | |
1234351c | 19 | #include "xfs_log.h" |
340785cc DW |
20 | #include "xfs_btree.h" |
21 | #include "xfs_rmap.h" | |
81f40041 CH |
22 | #include "xfs_alloc.h" |
23 | #include "xfs_bmap.h" | |
24 | #include "xfs_trace.h" | |
a5155b87 | 25 | #include "xfs_error.h" |
9817aa80 | 26 | #include "xfs_log_priv.h" |
86ffa471 | 27 | #include "xfs_log_recover.h" |
1da177e4 | 28 | |
182696fb DW |
29 | struct kmem_cache *xfs_efi_cache; |
30 | struct kmem_cache *xfs_efd_cache; | |
1da177e4 | 31 | |
10d0c6e0 DW |
32 | static const struct xfs_item_ops xfs_efi_item_ops; |
33 | ||
7bfa31d8 CH |
34 | static inline struct xfs_efi_log_item *EFI_ITEM(struct xfs_log_item *lip) |
35 | { | |
36 | return container_of(lip, struct xfs_efi_log_item, efi_item); | |
37 | } | |
1da177e4 | 38 | |
9817aa80 | 39 | STATIC void |
7bfa31d8 CH |
40 | xfs_efi_item_free( |
41 | struct xfs_efi_log_item *efip) | |
7d795ca3 | 42 | { |
b1c5ebb2 | 43 | kmem_free(efip->efi_item.li_lv_shadow); |
7bfa31d8 | 44 | if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS) |
f0e2d93c | 45 | kmem_free(efip); |
7bfa31d8 | 46 | else |
182696fb | 47 | kmem_cache_free(xfs_efi_cache, efip); |
7d795ca3 | 48 | } |
1da177e4 | 49 | |
0612d116 DC |
50 | /* |
51 | * Freeing the efi requires that we remove it from the AIL if it has already | |
52 | * been placed there. However, the EFI may not yet have been placed in the AIL | |
53 | * when called by xfs_efi_release() from EFD processing due to the ordering of | |
54 | * committed vs unpin operations in bulk insert operations. Hence the reference | |
55 | * count to ensure only the last caller frees the EFI. | |
56 | */ | |
10d0c6e0 | 57 | STATIC void |
0612d116 DC |
58 | xfs_efi_release( |
59 | struct xfs_efi_log_item *efip) | |
60 | { | |
61 | ASSERT(atomic_read(&efip->efi_refcount) > 0); | |
3512fc1e DC |
62 | if (!atomic_dec_and_test(&efip->efi_refcount)) |
63 | return; | |
64 | ||
65 | xfs_trans_ail_delete(&efip->efi_item, 0); | |
66 | xfs_efi_item_free(efip); | |
0612d116 DC |
67 | } |
68 | ||
1da177e4 LT |
69 | /* |
70 | * This returns the number of iovecs needed to log the given efi item. | |
71 | * We only need 1 iovec for an efi item. It just logs the efi_log_format | |
72 | * structure. | |
73 | */ | |
166d1368 DC |
74 | static inline int |
75 | xfs_efi_item_sizeof( | |
76 | struct xfs_efi_log_item *efip) | |
77 | { | |
78 | return sizeof(struct xfs_efi_log_format) + | |
79 | (efip->efi_format.efi_nextents - 1) * sizeof(xfs_extent_t); | |
80 | } | |
81 | ||
82 | STATIC void | |
7bfa31d8 | 83 | xfs_efi_item_size( |
166d1368 DC |
84 | struct xfs_log_item *lip, |
85 | int *nvecs, | |
86 | int *nbytes) | |
1da177e4 | 87 | { |
166d1368 DC |
88 | *nvecs += 1; |
89 | *nbytes += xfs_efi_item_sizeof(EFI_ITEM(lip)); | |
1da177e4 LT |
90 | } |
91 | ||
92 | /* | |
93 | * This is called to fill in the vector of log iovecs for the | |
94 | * given efi log item. We use only 1 iovec, and we point that | |
95 | * at the efi_log_format structure embedded in the efi item. | |
96 | * It is at this point that we assert that all of the extent | |
97 | * slots in the efi item have been filled. | |
98 | */ | |
99 | STATIC void | |
7bfa31d8 CH |
100 | xfs_efi_item_format( |
101 | struct xfs_log_item *lip, | |
bde7cff6 | 102 | struct xfs_log_vec *lv) |
1da177e4 | 103 | { |
7bfa31d8 | 104 | struct xfs_efi_log_item *efip = EFI_ITEM(lip); |
bde7cff6 | 105 | struct xfs_log_iovec *vecp = NULL; |
1da177e4 | 106 | |
b199c8a4 DC |
107 | ASSERT(atomic_read(&efip->efi_next_extent) == |
108 | efip->efi_format.efi_nextents); | |
1da177e4 LT |
109 | |
110 | efip->efi_format.efi_type = XFS_LI_EFI; | |
1da177e4 LT |
111 | efip->efi_format.efi_size = 1; |
112 | ||
bde7cff6 | 113 | xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFI_FORMAT, |
1234351c CH |
114 | &efip->efi_format, |
115 | xfs_efi_item_sizeof(efip)); | |
1da177e4 LT |
116 | } |
117 | ||
118 | ||
1da177e4 | 119 | /* |
8d99fe92 BF |
120 | * The unpin operation is the last place an EFI is manipulated in the log. It is |
121 | * either inserted in the AIL or aborted in the event of a log I/O error. In | |
122 | * either case, the EFI transaction has been successfully committed to make it | |
123 | * this far. Therefore, we expect whoever committed the EFI to either construct | |
124 | * and commit the EFD or drop the EFD's reference in the event of error. Simply | |
125 | * drop the log's EFI reference now that the log is done with it. | |
1da177e4 | 126 | */ |
1da177e4 | 127 | STATIC void |
7bfa31d8 CH |
128 | xfs_efi_item_unpin( |
129 | struct xfs_log_item *lip, | |
130 | int remove) | |
1da177e4 | 131 | { |
7bfa31d8 | 132 | struct xfs_efi_log_item *efip = EFI_ITEM(lip); |
5e4b5386 | 133 | xfs_efi_release(efip); |
1da177e4 LT |
134 | } |
135 | ||
8d99fe92 BF |
136 | /* |
137 | * The EFI has been either committed or aborted if the transaction has been | |
138 | * cancelled. If the transaction was cancelled, an EFD isn't going to be | |
139 | * constructed and thus we free the EFI here directly. | |
140 | */ | |
1da177e4 | 141 | STATIC void |
ddf92053 | 142 | xfs_efi_item_release( |
7bfa31d8 | 143 | struct xfs_log_item *lip) |
1da177e4 | 144 | { |
ddf92053 | 145 | xfs_efi_release(EFI_ITEM(lip)); |
1da177e4 LT |
146 | } |
147 | ||
1da177e4 LT |
148 | /* |
149 | * Allocate and initialize an efi item with the given number of extents. | |
150 | */ | |
9817aa80 | 151 | STATIC struct xfs_efi_log_item * |
7bfa31d8 CH |
152 | xfs_efi_init( |
153 | struct xfs_mount *mp, | |
154 | uint nextents) | |
1da177e4 LT |
155 | |
156 | { | |
7bfa31d8 | 157 | struct xfs_efi_log_item *efip; |
1da177e4 LT |
158 | uint size; |
159 | ||
160 | ASSERT(nextents > 0); | |
161 | if (nextents > XFS_EFI_MAX_FAST_EXTENTS) { | |
82ff450b | 162 | size = (uint)(sizeof(struct xfs_efi_log_item) + |
1da177e4 | 163 | ((nextents - 1) * sizeof(xfs_extent_t))); |
707e0dda | 164 | efip = kmem_zalloc(size, 0); |
1da177e4 | 165 | } else { |
182696fb | 166 | efip = kmem_cache_zalloc(xfs_efi_cache, |
32a2b11f | 167 | GFP_KERNEL | __GFP_NOFAIL); |
1da177e4 LT |
168 | } |
169 | ||
43f5efc5 | 170 | xfs_log_item_init(mp, &efip->efi_item, XFS_LI_EFI, &xfs_efi_item_ops); |
1da177e4 | 171 | efip->efi_format.efi_nextents = nextents; |
db9d67d6 | 172 | efip->efi_format.efi_id = (uintptr_t)(void *)efip; |
b199c8a4 | 173 | atomic_set(&efip->efi_next_extent, 0); |
666d644c | 174 | atomic_set(&efip->efi_refcount, 2); |
1da177e4 | 175 | |
7bfa31d8 | 176 | return efip; |
1da177e4 LT |
177 | } |
178 | ||
6d192a9b TS |
179 | /* |
180 | * Copy an EFI format buffer from the given buf, and into the destination | |
181 | * EFI format structure. | |
182 | * The given buffer can be in 32 bit or 64 bit form (which has different padding), | |
183 | * one of which will be the native format for this kernel. | |
184 | * It will handle the conversion of formats if necessary. | |
185 | */ | |
9817aa80 | 186 | STATIC int |
6d192a9b TS |
187 | xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt) |
188 | { | |
4e0d5f92 | 189 | xfs_efi_log_format_t *src_efi_fmt = buf->i_addr; |
6d192a9b | 190 | uint i; |
5e9466a5 XS |
191 | uint len = sizeof(xfs_efi_log_format_t) + |
192 | (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_t); | |
193 | uint len32 = sizeof(xfs_efi_log_format_32_t) + | |
194 | (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_32_t); | |
195 | uint len64 = sizeof(xfs_efi_log_format_64_t) + | |
196 | (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_64_t); | |
6d192a9b TS |
197 | |
198 | if (buf->i_len == len) { | |
199 | memcpy((char *)dst_efi_fmt, (char*)src_efi_fmt, len); | |
200 | return 0; | |
201 | } else if (buf->i_len == len32) { | |
4e0d5f92 | 202 | xfs_efi_log_format_32_t *src_efi_fmt_32 = buf->i_addr; |
6d192a9b TS |
203 | |
204 | dst_efi_fmt->efi_type = src_efi_fmt_32->efi_type; | |
205 | dst_efi_fmt->efi_size = src_efi_fmt_32->efi_size; | |
206 | dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents; | |
207 | dst_efi_fmt->efi_id = src_efi_fmt_32->efi_id; | |
208 | for (i = 0; i < dst_efi_fmt->efi_nextents; i++) { | |
209 | dst_efi_fmt->efi_extents[i].ext_start = | |
210 | src_efi_fmt_32->efi_extents[i].ext_start; | |
211 | dst_efi_fmt->efi_extents[i].ext_len = | |
212 | src_efi_fmt_32->efi_extents[i].ext_len; | |
213 | } | |
214 | return 0; | |
215 | } else if (buf->i_len == len64) { | |
4e0d5f92 | 216 | xfs_efi_log_format_64_t *src_efi_fmt_64 = buf->i_addr; |
6d192a9b TS |
217 | |
218 | dst_efi_fmt->efi_type = src_efi_fmt_64->efi_type; | |
219 | dst_efi_fmt->efi_size = src_efi_fmt_64->efi_size; | |
220 | dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents; | |
221 | dst_efi_fmt->efi_id = src_efi_fmt_64->efi_id; | |
222 | for (i = 0; i < dst_efi_fmt->efi_nextents; i++) { | |
223 | dst_efi_fmt->efi_extents[i].ext_start = | |
224 | src_efi_fmt_64->efi_extents[i].ext_start; | |
225 | dst_efi_fmt->efi_extents[i].ext_len = | |
226 | src_efi_fmt_64->efi_extents[i].ext_len; | |
227 | } | |
228 | return 0; | |
229 | } | |
a5155b87 | 230 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL); |
2451337d | 231 | return -EFSCORRUPTED; |
6d192a9b TS |
232 | } |
233 | ||
7bfa31d8 | 234 | static inline struct xfs_efd_log_item *EFD_ITEM(struct xfs_log_item *lip) |
7d795ca3 | 235 | { |
7bfa31d8 CH |
236 | return container_of(lip, struct xfs_efd_log_item, efd_item); |
237 | } | |
1da177e4 | 238 | |
7bfa31d8 CH |
239 | STATIC void |
240 | xfs_efd_item_free(struct xfs_efd_log_item *efdp) | |
241 | { | |
b1c5ebb2 | 242 | kmem_free(efdp->efd_item.li_lv_shadow); |
7bfa31d8 | 243 | if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS) |
f0e2d93c | 244 | kmem_free(efdp); |
7bfa31d8 | 245 | else |
182696fb | 246 | kmem_cache_free(xfs_efd_cache, efdp); |
7d795ca3 | 247 | } |
1da177e4 LT |
248 | |
249 | /* | |
250 | * This returns the number of iovecs needed to log the given efd item. | |
251 | * We only need 1 iovec for an efd item. It just logs the efd_log_format | |
252 | * structure. | |
253 | */ | |
166d1368 DC |
254 | static inline int |
255 | xfs_efd_item_sizeof( | |
256 | struct xfs_efd_log_item *efdp) | |
257 | { | |
258 | return sizeof(xfs_efd_log_format_t) + | |
259 | (efdp->efd_format.efd_nextents - 1) * sizeof(xfs_extent_t); | |
260 | } | |
261 | ||
262 | STATIC void | |
7bfa31d8 | 263 | xfs_efd_item_size( |
166d1368 DC |
264 | struct xfs_log_item *lip, |
265 | int *nvecs, | |
266 | int *nbytes) | |
1da177e4 | 267 | { |
166d1368 DC |
268 | *nvecs += 1; |
269 | *nbytes += xfs_efd_item_sizeof(EFD_ITEM(lip)); | |
1da177e4 LT |
270 | } |
271 | ||
272 | /* | |
273 | * This is called to fill in the vector of log iovecs for the | |
274 | * given efd log item. We use only 1 iovec, and we point that | |
275 | * at the efd_log_format structure embedded in the efd item. | |
276 | * It is at this point that we assert that all of the extent | |
277 | * slots in the efd item have been filled. | |
278 | */ | |
279 | STATIC void | |
7bfa31d8 CH |
280 | xfs_efd_item_format( |
281 | struct xfs_log_item *lip, | |
bde7cff6 | 282 | struct xfs_log_vec *lv) |
1da177e4 | 283 | { |
7bfa31d8 | 284 | struct xfs_efd_log_item *efdp = EFD_ITEM(lip); |
bde7cff6 | 285 | struct xfs_log_iovec *vecp = NULL; |
1da177e4 LT |
286 | |
287 | ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents); | |
288 | ||
289 | efdp->efd_format.efd_type = XFS_LI_EFD; | |
1da177e4 LT |
290 | efdp->efd_format.efd_size = 1; |
291 | ||
bde7cff6 | 292 | xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFD_FORMAT, |
1234351c CH |
293 | &efdp->efd_format, |
294 | xfs_efd_item_sizeof(efdp)); | |
1da177e4 LT |
295 | } |
296 | ||
8d99fe92 BF |
297 | /* |
298 | * The EFD is either committed or aborted if the transaction is cancelled. If | |
299 | * the transaction is cancelled, drop our reference to the EFI and free the EFD. | |
300 | */ | |
1da177e4 | 301 | STATIC void |
ddf92053 | 302 | xfs_efd_item_release( |
7bfa31d8 | 303 | struct xfs_log_item *lip) |
1da177e4 | 304 | { |
8d99fe92 BF |
305 | struct xfs_efd_log_item *efdp = EFD_ITEM(lip); |
306 | ||
ddf92053 CH |
307 | xfs_efi_release(efdp->efd_efip); |
308 | xfs_efd_item_free(efdp); | |
1da177e4 LT |
309 | } |
310 | ||
c23ab603 DC |
311 | static struct xfs_log_item * |
312 | xfs_efd_item_intent( | |
313 | struct xfs_log_item *lip) | |
314 | { | |
315 | return &EFD_ITEM(lip)->efd_efip->efi_item; | |
316 | } | |
317 | ||
272e42b2 | 318 | static const struct xfs_item_ops xfs_efd_item_ops = { |
f5b81200 DC |
319 | .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED | |
320 | XFS_ITEM_INTENT_DONE, | |
7bfa31d8 CH |
321 | .iop_size = xfs_efd_item_size, |
322 | .iop_format = xfs_efd_item_format, | |
ddf92053 | 323 | .iop_release = xfs_efd_item_release, |
c23ab603 | 324 | .iop_intent = xfs_efd_item_intent, |
1da177e4 LT |
325 | }; |
326 | ||
1da177e4 | 327 | /* |
9c5e7c2a CH |
328 | * Allocate an "extent free done" log item that will hold nextents worth of |
329 | * extents. The caller must use all nextents extents, because we are not | |
330 | * flexible about this at all. | |
1da177e4 | 331 | */ |
81f40041 | 332 | static struct xfs_efd_log_item * |
9c5e7c2a CH |
333 | xfs_trans_get_efd( |
334 | struct xfs_trans *tp, | |
335 | struct xfs_efi_log_item *efip, | |
336 | unsigned int nextents) | |
1da177e4 | 337 | { |
9c5e7c2a | 338 | struct xfs_efd_log_item *efdp; |
1da177e4 LT |
339 | |
340 | ASSERT(nextents > 0); | |
9c5e7c2a | 341 | |
1da177e4 | 342 | if (nextents > XFS_EFD_MAX_FAST_EXTENTS) { |
9c5e7c2a CH |
343 | efdp = kmem_zalloc(sizeof(struct xfs_efd_log_item) + |
344 | (nextents - 1) * sizeof(struct xfs_extent), | |
707e0dda | 345 | 0); |
1da177e4 | 346 | } else { |
182696fb | 347 | efdp = kmem_cache_zalloc(xfs_efd_cache, |
32a2b11f | 348 | GFP_KERNEL | __GFP_NOFAIL); |
1da177e4 LT |
349 | } |
350 | ||
9c5e7c2a CH |
351 | xfs_log_item_init(tp->t_mountp, &efdp->efd_item, XFS_LI_EFD, |
352 | &xfs_efd_item_ops); | |
1da177e4 LT |
353 | efdp->efd_efip = efip; |
354 | efdp->efd_format.efd_nextents = nextents; | |
355 | efdp->efd_format.efd_efi_id = efip->efi_format.efi_id; | |
356 | ||
9c5e7c2a | 357 | xfs_trans_add_item(tp, &efdp->efd_item); |
7bfa31d8 | 358 | return efdp; |
1da177e4 | 359 | } |
dc42375d | 360 | |
81f40041 CH |
361 | /* |
362 | * Free an extent and log it to the EFD. Note that the transaction is marked | |
363 | * dirty regardless of whether the extent free succeeds or fails to support the | |
364 | * EFI/EFD lifecycle rules. | |
365 | */ | |
366 | static int | |
367 | xfs_trans_free_extent( | |
368 | struct xfs_trans *tp, | |
369 | struct xfs_efd_log_item *efdp, | |
370 | xfs_fsblock_t start_block, | |
371 | xfs_extlen_t ext_len, | |
372 | const struct xfs_owner_info *oinfo, | |
373 | bool skip_discard) | |
374 | { | |
375 | struct xfs_mount *mp = tp->t_mountp; | |
376 | struct xfs_extent *extp; | |
377 | uint next_extent; | |
378 | xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, start_block); | |
379 | xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp, | |
380 | start_block); | |
381 | int error; | |
382 | ||
383 | trace_xfs_bmap_free_deferred(tp->t_mountp, agno, 0, agbno, ext_len); | |
384 | ||
385 | error = __xfs_free_extent(tp, start_block, ext_len, | |
386 | oinfo, XFS_AG_RESV_NONE, skip_discard); | |
387 | /* | |
388 | * Mark the transaction dirty, even on error. This ensures the | |
389 | * transaction is aborted, which: | |
390 | * | |
391 | * 1.) releases the EFI and frees the EFD | |
392 | * 2.) shuts down the filesystem | |
393 | */ | |
bb7b1c9c | 394 | tp->t_flags |= XFS_TRANS_DIRTY | XFS_TRANS_HAS_INTENT_DONE; |
81f40041 CH |
395 | set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags); |
396 | ||
397 | next_extent = efdp->efd_next_extent; | |
398 | ASSERT(next_extent < efdp->efd_format.efd_nextents); | |
399 | extp = &(efdp->efd_format.efd_extents[next_extent]); | |
400 | extp->ext_start = start_block; | |
401 | extp->ext_len = ext_len; | |
402 | efdp->efd_next_extent++; | |
403 | ||
404 | return error; | |
405 | } | |
406 | ||
407 | /* Sort bmap items by AG. */ | |
408 | static int | |
409 | xfs_extent_free_diff_items( | |
410 | void *priv, | |
4f0f586b ST |
411 | const struct list_head *a, |
412 | const struct list_head *b) | |
81f40041 CH |
413 | { |
414 | struct xfs_mount *mp = priv; | |
415 | struct xfs_extent_free_item *ra; | |
416 | struct xfs_extent_free_item *rb; | |
417 | ||
418 | ra = container_of(a, struct xfs_extent_free_item, xefi_list); | |
419 | rb = container_of(b, struct xfs_extent_free_item, xefi_list); | |
420 | return XFS_FSB_TO_AGNO(mp, ra->xefi_startblock) - | |
421 | XFS_FSB_TO_AGNO(mp, rb->xefi_startblock); | |
422 | } | |
423 | ||
81f40041 CH |
424 | /* Log a free extent to the intent item. */ |
425 | STATIC void | |
426 | xfs_extent_free_log_item( | |
427 | struct xfs_trans *tp, | |
c1f09188 CH |
428 | struct xfs_efi_log_item *efip, |
429 | struct xfs_extent_free_item *free) | |
81f40041 | 430 | { |
81f40041 CH |
431 | uint next_extent; |
432 | struct xfs_extent *extp; | |
433 | ||
81f40041 CH |
434 | tp->t_flags |= XFS_TRANS_DIRTY; |
435 | set_bit(XFS_LI_DIRTY, &efip->efi_item.li_flags); | |
436 | ||
437 | /* | |
438 | * atomic_inc_return gives us the value after the increment; | |
439 | * we want to use it as an array index so we need to subtract 1 from | |
440 | * it. | |
441 | */ | |
442 | next_extent = atomic_inc_return(&efip->efi_next_extent) - 1; | |
443 | ASSERT(next_extent < efip->efi_format.efi_nextents); | |
444 | extp = &efip->efi_format.efi_extents[next_extent]; | |
445 | extp->ext_start = free->xefi_startblock; | |
446 | extp->ext_len = free->xefi_blockcount; | |
447 | } | |
448 | ||
13a83333 | 449 | static struct xfs_log_item * |
c1f09188 CH |
450 | xfs_extent_free_create_intent( |
451 | struct xfs_trans *tp, | |
452 | struct list_head *items, | |
d367a868 CH |
453 | unsigned int count, |
454 | bool sort) | |
c1f09188 CH |
455 | { |
456 | struct xfs_mount *mp = tp->t_mountp; | |
457 | struct xfs_efi_log_item *efip = xfs_efi_init(mp, count); | |
458 | struct xfs_extent_free_item *free; | |
459 | ||
460 | ASSERT(count > 0); | |
461 | ||
462 | xfs_trans_add_item(tp, &efip->efi_item); | |
d367a868 CH |
463 | if (sort) |
464 | list_sort(mp, items, xfs_extent_free_diff_items); | |
c1f09188 CH |
465 | list_for_each_entry(free, items, xefi_list) |
466 | xfs_extent_free_log_item(tp, efip, free); | |
13a83333 | 467 | return &efip->efi_item; |
c1f09188 CH |
468 | } |
469 | ||
81f40041 | 470 | /* Get an EFD so we can process all the free extents. */ |
f09d167c | 471 | static struct xfs_log_item * |
81f40041 CH |
472 | xfs_extent_free_create_done( |
473 | struct xfs_trans *tp, | |
13a83333 | 474 | struct xfs_log_item *intent, |
81f40041 CH |
475 | unsigned int count) |
476 | { | |
f09d167c | 477 | return &xfs_trans_get_efd(tp, EFI_ITEM(intent), count)->efd_item; |
81f40041 CH |
478 | } |
479 | ||
480 | /* Process a free extent. */ | |
481 | STATIC int | |
482 | xfs_extent_free_finish_item( | |
483 | struct xfs_trans *tp, | |
f09d167c | 484 | struct xfs_log_item *done, |
81f40041 | 485 | struct list_head *item, |
3ec1b26c | 486 | struct xfs_btree_cur **state) |
81f40041 | 487 | { |
b3b5ff41 | 488 | struct xfs_owner_info oinfo = { }; |
81f40041 CH |
489 | struct xfs_extent_free_item *free; |
490 | int error; | |
491 | ||
492 | free = container_of(item, struct xfs_extent_free_item, xefi_list); | |
b3b5ff41 DW |
493 | oinfo.oi_owner = free->xefi_owner; |
494 | if (free->xefi_flags & XFS_EFI_ATTR_FORK) | |
495 | oinfo.oi_flags |= XFS_OWNER_INFO_ATTR_FORK; | |
496 | if (free->xefi_flags & XFS_EFI_BMBT_BLOCK) | |
497 | oinfo.oi_flags |= XFS_OWNER_INFO_BMBT_BLOCK; | |
f09d167c | 498 | error = xfs_trans_free_extent(tp, EFD_ITEM(done), |
81f40041 CH |
499 | free->xefi_startblock, |
500 | free->xefi_blockcount, | |
b3b5ff41 | 501 | &oinfo, free->xefi_flags & XFS_EFI_SKIP_DISCARD); |
c201d9ca | 502 | kmem_cache_free(xfs_extfree_item_cache, free); |
81f40041 CH |
503 | return error; |
504 | } | |
505 | ||
506 | /* Abort all pending EFIs. */ | |
507 | STATIC void | |
508 | xfs_extent_free_abort_intent( | |
13a83333 | 509 | struct xfs_log_item *intent) |
81f40041 | 510 | { |
13a83333 | 511 | xfs_efi_release(EFI_ITEM(intent)); |
81f40041 CH |
512 | } |
513 | ||
514 | /* Cancel a free extent. */ | |
515 | STATIC void | |
516 | xfs_extent_free_cancel_item( | |
517 | struct list_head *item) | |
518 | { | |
519 | struct xfs_extent_free_item *free; | |
520 | ||
521 | free = container_of(item, struct xfs_extent_free_item, xefi_list); | |
c201d9ca | 522 | kmem_cache_free(xfs_extfree_item_cache, free); |
81f40041 CH |
523 | } |
524 | ||
525 | const struct xfs_defer_op_type xfs_extent_free_defer_type = { | |
526 | .max_items = XFS_EFI_MAX_FAST_EXTENTS, | |
81f40041 CH |
527 | .create_intent = xfs_extent_free_create_intent, |
528 | .abort_intent = xfs_extent_free_abort_intent, | |
81f40041 CH |
529 | .create_done = xfs_extent_free_create_done, |
530 | .finish_item = xfs_extent_free_finish_item, | |
531 | .cancel_item = xfs_extent_free_cancel_item, | |
532 | }; | |
533 | ||
534 | /* | |
535 | * AGFL blocks are accounted differently in the reserve pools and are not | |
536 | * inserted into the busy extent list. | |
537 | */ | |
538 | STATIC int | |
539 | xfs_agfl_free_finish_item( | |
540 | struct xfs_trans *tp, | |
f09d167c | 541 | struct xfs_log_item *done, |
81f40041 | 542 | struct list_head *item, |
3ec1b26c | 543 | struct xfs_btree_cur **state) |
81f40041 | 544 | { |
b3b5ff41 | 545 | struct xfs_owner_info oinfo = { }; |
81f40041 | 546 | struct xfs_mount *mp = tp->t_mountp; |
f09d167c | 547 | struct xfs_efd_log_item *efdp = EFD_ITEM(done); |
81f40041 CH |
548 | struct xfs_extent_free_item *free; |
549 | struct xfs_extent *extp; | |
550 | struct xfs_buf *agbp; | |
551 | int error; | |
552 | xfs_agnumber_t agno; | |
553 | xfs_agblock_t agbno; | |
554 | uint next_extent; | |
08d3e84f | 555 | struct xfs_perag *pag; |
81f40041 CH |
556 | |
557 | free = container_of(item, struct xfs_extent_free_item, xefi_list); | |
558 | ASSERT(free->xefi_blockcount == 1); | |
559 | agno = XFS_FSB_TO_AGNO(mp, free->xefi_startblock); | |
560 | agbno = XFS_FSB_TO_AGBNO(mp, free->xefi_startblock); | |
b3b5ff41 | 561 | oinfo.oi_owner = free->xefi_owner; |
81f40041 CH |
562 | |
563 | trace_xfs_agfl_free_deferred(mp, agno, 0, agbno, free->xefi_blockcount); | |
564 | ||
08d3e84f DC |
565 | pag = xfs_perag_get(mp, agno); |
566 | error = xfs_alloc_read_agf(pag, tp, 0, &agbp); | |
81f40041 | 567 | if (!error) |
b3b5ff41 | 568 | error = xfs_free_agfl_block(tp, agno, agbno, agbp, &oinfo); |
08d3e84f | 569 | xfs_perag_put(pag); |
81f40041 CH |
570 | |
571 | /* | |
572 | * Mark the transaction dirty, even on error. This ensures the | |
573 | * transaction is aborted, which: | |
574 | * | |
575 | * 1.) releases the EFI and frees the EFD | |
576 | * 2.) shuts down the filesystem | |
577 | */ | |
578 | tp->t_flags |= XFS_TRANS_DIRTY; | |
579 | set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags); | |
580 | ||
581 | next_extent = efdp->efd_next_extent; | |
582 | ASSERT(next_extent < efdp->efd_format.efd_nextents); | |
583 | extp = &(efdp->efd_format.efd_extents[next_extent]); | |
584 | extp->ext_start = free->xefi_startblock; | |
585 | extp->ext_len = free->xefi_blockcount; | |
586 | efdp->efd_next_extent++; | |
587 | ||
c201d9ca | 588 | kmem_cache_free(xfs_extfree_item_cache, free); |
81f40041 CH |
589 | return error; |
590 | } | |
591 | ||
592 | /* sub-type with special handling for AGFL deferred frees */ | |
593 | const struct xfs_defer_op_type xfs_agfl_free_defer_type = { | |
594 | .max_items = XFS_EFI_MAX_FAST_EXTENTS, | |
81f40041 CH |
595 | .create_intent = xfs_extent_free_create_intent, |
596 | .abort_intent = xfs_extent_free_abort_intent, | |
81f40041 CH |
597 | .create_done = xfs_extent_free_create_done, |
598 | .finish_item = xfs_agfl_free_finish_item, | |
599 | .cancel_item = xfs_extent_free_cancel_item, | |
600 | }; | |
601 | ||
3c15df3d DW |
602 | /* Is this recovered EFI ok? */ |
603 | static inline bool | |
604 | xfs_efi_validate_ext( | |
605 | struct xfs_mount *mp, | |
606 | struct xfs_extent *extp) | |
607 | { | |
67457eb0 | 608 | return xfs_verify_fsbext(mp, extp->ext_start, extp->ext_len); |
3c15df3d DW |
609 | } |
610 | ||
dc42375d DW |
611 | /* |
612 | * Process an extent free intent item that was recovered from | |
613 | * the log. We need to free the extents that it describes. | |
614 | */ | |
10d0c6e0 | 615 | STATIC int |
96b60f82 DW |
616 | xfs_efi_item_recover( |
617 | struct xfs_log_item *lip, | |
e6fff81e | 618 | struct list_head *capture_list) |
dc42375d | 619 | { |
96b60f82 | 620 | struct xfs_efi_log_item *efip = EFI_ITEM(lip); |
d86142dd | 621 | struct xfs_mount *mp = lip->li_log->l_mp; |
96b60f82 DW |
622 | struct xfs_efd_log_item *efdp; |
623 | struct xfs_trans *tp; | |
624 | struct xfs_extent *extp; | |
96b60f82 DW |
625 | int i; |
626 | int error = 0; | |
dc42375d | 627 | |
dc42375d DW |
628 | /* |
629 | * First check the validity of the extents described by the | |
630 | * EFI. If any are bad, then assume that all are bad and | |
631 | * just toss the EFI. | |
632 | */ | |
633 | for (i = 0; i < efip->efi_format.efi_nextents; i++) { | |
3c15df3d DW |
634 | if (!xfs_efi_validate_ext(mp, |
635 | &efip->efi_format.efi_extents[i])) { | |
636 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, | |
637 | &efip->efi_format, | |
638 | sizeof(efip->efi_format)); | |
895e196f | 639 | return -EFSCORRUPTED; |
3c15df3d | 640 | } |
dc42375d DW |
641 | } |
642 | ||
643 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp); | |
644 | if (error) | |
645 | return error; | |
646 | efdp = xfs_trans_get_efd(tp, efip, efip->efi_format.efi_nextents); | |
647 | ||
648 | for (i = 0; i < efip->efi_format.efi_nextents; i++) { | |
e127fafd | 649 | extp = &efip->efi_format.efi_extents[i]; |
dc42375d | 650 | error = xfs_trans_free_extent(tp, efdp, extp->ext_start, |
7280feda DW |
651 | extp->ext_len, |
652 | &XFS_RMAP_OINFO_ANY_OWNER, false); | |
43059d54 DW |
653 | if (error == -EFSCORRUPTED) |
654 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, | |
655 | extp, sizeof(*extp)); | |
dc42375d DW |
656 | if (error) |
657 | goto abort_error; | |
658 | ||
659 | } | |
660 | ||
512edfac | 661 | return xfs_defer_ops_capture_and_commit(tp, capture_list); |
dc42375d DW |
662 | |
663 | abort_error: | |
664 | xfs_trans_cancel(tp); | |
665 | return error; | |
666 | } | |
86ffa471 | 667 | |
154c733a DW |
668 | STATIC bool |
669 | xfs_efi_item_match( | |
670 | struct xfs_log_item *lip, | |
671 | uint64_t intent_id) | |
672 | { | |
673 | return EFI_ITEM(lip)->efi_format.efi_id == intent_id; | |
674 | } | |
675 | ||
4e919af7 DW |
676 | /* Relog an intent item to push the log tail forward. */ |
677 | static struct xfs_log_item * | |
678 | xfs_efi_item_relog( | |
679 | struct xfs_log_item *intent, | |
680 | struct xfs_trans *tp) | |
681 | { | |
682 | struct xfs_efd_log_item *efdp; | |
683 | struct xfs_efi_log_item *efip; | |
684 | struct xfs_extent *extp; | |
685 | unsigned int count; | |
686 | ||
687 | count = EFI_ITEM(intent)->efi_format.efi_nextents; | |
688 | extp = EFI_ITEM(intent)->efi_format.efi_extents; | |
689 | ||
690 | tp->t_flags |= XFS_TRANS_DIRTY; | |
691 | efdp = xfs_trans_get_efd(tp, EFI_ITEM(intent), count); | |
692 | efdp->efd_next_extent = count; | |
693 | memcpy(efdp->efd_format.efd_extents, extp, count * sizeof(*extp)); | |
694 | set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags); | |
695 | ||
696 | efip = xfs_efi_init(tp->t_mountp, count); | |
697 | memcpy(efip->efi_format.efi_extents, extp, count * sizeof(*extp)); | |
698 | atomic_set(&efip->efi_next_extent, count); | |
699 | xfs_trans_add_item(tp, &efip->efi_item); | |
700 | set_bit(XFS_LI_DIRTY, &efip->efi_item.li_flags); | |
701 | return &efip->efi_item; | |
702 | } | |
703 | ||
10d0c6e0 | 704 | static const struct xfs_item_ops xfs_efi_item_ops = { |
f5b81200 | 705 | .flags = XFS_ITEM_INTENT, |
10d0c6e0 DW |
706 | .iop_size = xfs_efi_item_size, |
707 | .iop_format = xfs_efi_item_format, | |
708 | .iop_unpin = xfs_efi_item_unpin, | |
709 | .iop_release = xfs_efi_item_release, | |
710 | .iop_recover = xfs_efi_item_recover, | |
154c733a | 711 | .iop_match = xfs_efi_item_match, |
4e919af7 | 712 | .iop_relog = xfs_efi_item_relog, |
10d0c6e0 DW |
713 | }; |
714 | ||
9817aa80 DW |
715 | /* |
716 | * This routine is called to create an in-core extent free intent | |
717 | * item from the efi format structure which was logged on disk. | |
718 | * It allocates an in-core efi, copies the extents from the format | |
719 | * structure into it, and adds the efi to the AIL with the given | |
720 | * LSN. | |
721 | */ | |
722 | STATIC int | |
723 | xlog_recover_efi_commit_pass2( | |
724 | struct xlog *log, | |
725 | struct list_head *buffer_list, | |
726 | struct xlog_recover_item *item, | |
727 | xfs_lsn_t lsn) | |
728 | { | |
729 | struct xfs_mount *mp = log->l_mp; | |
730 | struct xfs_efi_log_item *efip; | |
731 | struct xfs_efi_log_format *efi_formatp; | |
732 | int error; | |
733 | ||
734 | efi_formatp = item->ri_buf[0].i_addr; | |
735 | ||
736 | efip = xfs_efi_init(mp, efi_formatp->efi_nextents); | |
737 | error = xfs_efi_copy_format(&item->ri_buf[0], &efip->efi_format); | |
738 | if (error) { | |
739 | xfs_efi_item_free(efip); | |
740 | return error; | |
741 | } | |
742 | atomic_set(&efip->efi_next_extent, efi_formatp->efi_nextents); | |
9817aa80 | 743 | /* |
86a37174 DW |
744 | * Insert the intent into the AIL directly and drop one reference so |
745 | * that finishing or canceling the work will drop the other. | |
9817aa80 | 746 | */ |
86a37174 | 747 | xfs_trans_ail_insert(log->l_ailp, &efip->efi_item, lsn); |
9817aa80 DW |
748 | xfs_efi_release(efip); |
749 | return 0; | |
750 | } | |
751 | ||
86ffa471 DW |
752 | const struct xlog_recover_item_ops xlog_efi_item_ops = { |
753 | .item_type = XFS_LI_EFI, | |
9817aa80 | 754 | .commit_pass2 = xlog_recover_efi_commit_pass2, |
86ffa471 DW |
755 | }; |
756 | ||
9817aa80 DW |
757 | /* |
758 | * This routine is called when an EFD format structure is found in a committed | |
759 | * transaction in the log. Its purpose is to cancel the corresponding EFI if it | |
760 | * was still in the log. To do this it searches the AIL for the EFI with an id | |
761 | * equal to that in the EFD format structure. If we find it we drop the EFD | |
762 | * reference, which removes the EFI from the AIL and frees it. | |
763 | */ | |
764 | STATIC int | |
765 | xlog_recover_efd_commit_pass2( | |
766 | struct xlog *log, | |
767 | struct list_head *buffer_list, | |
768 | struct xlog_recover_item *item, | |
769 | xfs_lsn_t lsn) | |
770 | { | |
9817aa80 | 771 | struct xfs_efd_log_format *efd_formatp; |
9817aa80 DW |
772 | |
773 | efd_formatp = item->ri_buf[0].i_addr; | |
774 | ASSERT((item->ri_buf[0].i_len == (sizeof(xfs_efd_log_format_32_t) + | |
775 | ((efd_formatp->efd_nextents - 1) * sizeof(xfs_extent_32_t)))) || | |
776 | (item->ri_buf[0].i_len == (sizeof(xfs_efd_log_format_64_t) + | |
777 | ((efd_formatp->efd_nextents - 1) * sizeof(xfs_extent_64_t))))); | |
9817aa80 | 778 | |
154c733a | 779 | xlog_recover_release_intent(log, XFS_LI_EFI, efd_formatp->efd_efi_id); |
9817aa80 DW |
780 | return 0; |
781 | } | |
782 | ||
86ffa471 DW |
783 | const struct xlog_recover_item_ops xlog_efd_item_ops = { |
784 | .item_type = XFS_LI_EFD, | |
9817aa80 | 785 | .commit_pass2 = xlog_recover_efd_commit_pass2, |
86ffa471 | 786 | }; |