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