Commit | Line | Data |
---|---|---|
0b61f8a4 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
6413a014 DW |
2 | /* |
3 | * Copyright (C) 2016 Oracle. All Rights Reserved. | |
6413a014 | 4 | * Author: Darrick J. Wong <darrick.wong@oracle.com> |
6413a014 DW |
5 | */ |
6 | #include "xfs.h" | |
7 | #include "xfs_fs.h" | |
8 | #include "xfs_format.h" | |
9 | #include "xfs_log_format.h" | |
10 | #include "xfs_trans_resv.h" | |
77d61fe4 | 11 | #include "xfs_bit.h" |
5467b34b | 12 | #include "xfs_shared.h" |
6413a014 | 13 | #include "xfs_mount.h" |
77d61fe4 DW |
14 | #include "xfs_defer.h" |
15 | #include "xfs_inode.h" | |
6413a014 DW |
16 | #include "xfs_trans.h" |
17 | #include "xfs_trans_priv.h" | |
6413a014 DW |
18 | #include "xfs_bmap_item.h" |
19 | #include "xfs_log.h" | |
77d61fe4 DW |
20 | #include "xfs_bmap.h" |
21 | #include "xfs_icache.h" | |
fe0be23e DW |
22 | #include "xfs_bmap_btree.h" |
23 | #include "xfs_trans_space.h" | |
a5155b87 | 24 | #include "xfs_error.h" |
3c6ba3cf | 25 | #include "xfs_log_priv.h" |
86ffa471 | 26 | #include "xfs_log_recover.h" |
774a99b4 | 27 | #include "xfs_ag.h" |
6413a014 | 28 | |
182696fb DW |
29 | struct kmem_cache *xfs_bui_cache; |
30 | struct kmem_cache *xfs_bud_cache; | |
6413a014 | 31 | |
9329ba89 DW |
32 | static const struct xfs_item_ops xfs_bui_item_ops; |
33 | ||
6413a014 DW |
34 | static inline struct xfs_bui_log_item *BUI_ITEM(struct xfs_log_item *lip) |
35 | { | |
36 | return container_of(lip, struct xfs_bui_log_item, bui_item); | |
37 | } | |
38 | ||
3c6ba3cf | 39 | STATIC void |
6413a014 DW |
40 | xfs_bui_item_free( |
41 | struct xfs_bui_log_item *buip) | |
42 | { | |
c230a4a8 | 43 | kmem_free(buip->bui_item.li_lv_shadow); |
182696fb | 44 | kmem_cache_free(xfs_bui_cache, buip); |
6413a014 DW |
45 | } |
46 | ||
0612d116 DC |
47 | /* |
48 | * Freeing the BUI requires that we remove it from the AIL if it has already | |
49 | * been placed there. However, the BUI may not yet have been placed in the AIL | |
50 | * when called by xfs_bui_release() from BUD processing due to the ordering of | |
51 | * committed vs unpin operations in bulk insert operations. Hence the reference | |
52 | * count to ensure only the last caller frees the BUI. | |
53 | */ | |
9329ba89 | 54 | STATIC void |
0612d116 DC |
55 | xfs_bui_release( |
56 | struct xfs_bui_log_item *buip) | |
57 | { | |
58 | ASSERT(atomic_read(&buip->bui_refcount) > 0); | |
3512fc1e DC |
59 | if (!atomic_dec_and_test(&buip->bui_refcount)) |
60 | return; | |
61 | ||
62 | xfs_trans_ail_delete(&buip->bui_item, 0); | |
63 | xfs_bui_item_free(buip); | |
0612d116 DC |
64 | } |
65 | ||
66 | ||
6413a014 DW |
67 | STATIC void |
68 | xfs_bui_item_size( | |
69 | struct xfs_log_item *lip, | |
70 | int *nvecs, | |
71 | int *nbytes) | |
72 | { | |
73 | struct xfs_bui_log_item *buip = BUI_ITEM(lip); | |
74 | ||
75 | *nvecs += 1; | |
76 | *nbytes += xfs_bui_log_format_sizeof(buip->bui_format.bui_nextents); | |
77 | } | |
78 | ||
79 | /* | |
80 | * This is called to fill in the vector of log iovecs for the | |
81 | * given bui log item. We use only 1 iovec, and we point that | |
82 | * at the bui_log_format structure embedded in the bui item. | |
83 | * It is at this point that we assert that all of the extent | |
84 | * slots in the bui item have been filled. | |
85 | */ | |
86 | STATIC void | |
87 | xfs_bui_item_format( | |
88 | struct xfs_log_item *lip, | |
89 | struct xfs_log_vec *lv) | |
90 | { | |
91 | struct xfs_bui_log_item *buip = BUI_ITEM(lip); | |
92 | struct xfs_log_iovec *vecp = NULL; | |
93 | ||
94 | ASSERT(atomic_read(&buip->bui_next_extent) == | |
95 | buip->bui_format.bui_nextents); | |
96 | ||
97 | buip->bui_format.bui_type = XFS_LI_BUI; | |
98 | buip->bui_format.bui_size = 1; | |
99 | ||
100 | xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_BUI_FORMAT, &buip->bui_format, | |
101 | xfs_bui_log_format_sizeof(buip->bui_format.bui_nextents)); | |
102 | } | |
103 | ||
6413a014 DW |
104 | /* |
105 | * The unpin operation is the last place an BUI is manipulated in the log. It is | |
106 | * either inserted in the AIL or aborted in the event of a log I/O error. In | |
107 | * either case, the BUI transaction has been successfully committed to make it | |
108 | * this far. Therefore, we expect whoever committed the BUI to either construct | |
109 | * and commit the BUD or drop the BUD's reference in the event of error. Simply | |
110 | * drop the log's BUI reference now that the log is done with it. | |
111 | */ | |
112 | STATIC void | |
113 | xfs_bui_item_unpin( | |
114 | struct xfs_log_item *lip, | |
115 | int remove) | |
116 | { | |
117 | struct xfs_bui_log_item *buip = BUI_ITEM(lip); | |
118 | ||
119 | xfs_bui_release(buip); | |
120 | } | |
121 | ||
6413a014 DW |
122 | /* |
123 | * The BUI has been either committed or aborted if the transaction has been | |
124 | * cancelled. If the transaction was cancelled, an BUD isn't going to be | |
125 | * constructed and thus we free the BUI here directly. | |
126 | */ | |
127 | STATIC void | |
ddf92053 | 128 | xfs_bui_item_release( |
6413a014 DW |
129 | struct xfs_log_item *lip) |
130 | { | |
ddf92053 | 131 | xfs_bui_release(BUI_ITEM(lip)); |
6413a014 DW |
132 | } |
133 | ||
6413a014 DW |
134 | /* |
135 | * Allocate and initialize an bui item with the given number of extents. | |
136 | */ | |
3c6ba3cf | 137 | STATIC struct xfs_bui_log_item * |
6413a014 DW |
138 | xfs_bui_init( |
139 | struct xfs_mount *mp) | |
140 | ||
141 | { | |
142 | struct xfs_bui_log_item *buip; | |
143 | ||
182696fb | 144 | buip = kmem_cache_zalloc(xfs_bui_cache, GFP_KERNEL | __GFP_NOFAIL); |
6413a014 DW |
145 | |
146 | xfs_log_item_init(mp, &buip->bui_item, XFS_LI_BUI, &xfs_bui_item_ops); | |
147 | buip->bui_format.bui_nextents = XFS_BUI_MAX_FAST_EXTENTS; | |
148 | buip->bui_format.bui_id = (uintptr_t)(void *)buip; | |
149 | atomic_set(&buip->bui_next_extent, 0); | |
150 | atomic_set(&buip->bui_refcount, 2); | |
151 | ||
152 | return buip; | |
153 | } | |
154 | ||
6413a014 DW |
155 | static inline struct xfs_bud_log_item *BUD_ITEM(struct xfs_log_item *lip) |
156 | { | |
157 | return container_of(lip, struct xfs_bud_log_item, bud_item); | |
158 | } | |
159 | ||
160 | STATIC void | |
161 | xfs_bud_item_size( | |
162 | struct xfs_log_item *lip, | |
163 | int *nvecs, | |
164 | int *nbytes) | |
165 | { | |
166 | *nvecs += 1; | |
167 | *nbytes += sizeof(struct xfs_bud_log_format); | |
168 | } | |
169 | ||
170 | /* | |
171 | * This is called to fill in the vector of log iovecs for the | |
172 | * given bud log item. We use only 1 iovec, and we point that | |
173 | * at the bud_log_format structure embedded in the bud item. | |
174 | * It is at this point that we assert that all of the extent | |
175 | * slots in the bud item have been filled. | |
176 | */ | |
177 | STATIC void | |
178 | xfs_bud_item_format( | |
179 | struct xfs_log_item *lip, | |
180 | struct xfs_log_vec *lv) | |
181 | { | |
182 | struct xfs_bud_log_item *budp = BUD_ITEM(lip); | |
183 | struct xfs_log_iovec *vecp = NULL; | |
184 | ||
185 | budp->bud_format.bud_type = XFS_LI_BUD; | |
186 | budp->bud_format.bud_size = 1; | |
187 | ||
188 | xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_BUD_FORMAT, &budp->bud_format, | |
189 | sizeof(struct xfs_bud_log_format)); | |
190 | } | |
191 | ||
6413a014 DW |
192 | /* |
193 | * The BUD is either committed or aborted if the transaction is cancelled. If | |
194 | * the transaction is cancelled, drop our reference to the BUI and free the | |
195 | * BUD. | |
196 | */ | |
197 | STATIC void | |
ddf92053 | 198 | xfs_bud_item_release( |
6413a014 DW |
199 | struct xfs_log_item *lip) |
200 | { | |
201 | struct xfs_bud_log_item *budp = BUD_ITEM(lip); | |
202 | ||
ddf92053 | 203 | xfs_bui_release(budp->bud_buip); |
c230a4a8 | 204 | kmem_free(budp->bud_item.li_lv_shadow); |
182696fb | 205 | kmem_cache_free(xfs_bud_cache, budp); |
6413a014 DW |
206 | } |
207 | ||
c23ab603 DC |
208 | static struct xfs_log_item * |
209 | xfs_bud_item_intent( | |
210 | struct xfs_log_item *lip) | |
211 | { | |
212 | return &BUD_ITEM(lip)->bud_buip->bui_item; | |
213 | } | |
214 | ||
6413a014 | 215 | static const struct xfs_item_ops xfs_bud_item_ops = { |
f5b81200 DC |
216 | .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED | |
217 | XFS_ITEM_INTENT_DONE, | |
6413a014 DW |
218 | .iop_size = xfs_bud_item_size, |
219 | .iop_format = xfs_bud_item_format, | |
ddf92053 | 220 | .iop_release = xfs_bud_item_release, |
c23ab603 | 221 | .iop_intent = xfs_bud_item_intent, |
6413a014 DW |
222 | }; |
223 | ||
caeaea98 | 224 | static struct xfs_bud_log_item * |
73f0d236 CH |
225 | xfs_trans_get_bud( |
226 | struct xfs_trans *tp, | |
6413a014 | 227 | struct xfs_bui_log_item *buip) |
6413a014 | 228 | { |
73f0d236 | 229 | struct xfs_bud_log_item *budp; |
6413a014 | 230 | |
182696fb | 231 | budp = kmem_cache_zalloc(xfs_bud_cache, GFP_KERNEL | __GFP_NOFAIL); |
73f0d236 CH |
232 | xfs_log_item_init(tp->t_mountp, &budp->bud_item, XFS_LI_BUD, |
233 | &xfs_bud_item_ops); | |
6413a014 DW |
234 | budp->bud_buip = buip; |
235 | budp->bud_format.bud_bui_id = buip->bui_format.bui_id; | |
236 | ||
73f0d236 | 237 | xfs_trans_add_item(tp, &budp->bud_item); |
6413a014 DW |
238 | return budp; |
239 | } | |
77d61fe4 | 240 | |
caeaea98 CH |
241 | /* |
242 | * Finish an bmap update and log it to the BUD. Note that the | |
243 | * transaction is marked dirty regardless of whether the bmap update | |
244 | * succeeds or fails to support the BUI/BUD lifecycle rules. | |
245 | */ | |
246 | static int | |
247 | xfs_trans_log_finish_bmap_update( | |
248 | struct xfs_trans *tp, | |
249 | struct xfs_bud_log_item *budp, | |
ddccb81b | 250 | struct xfs_bmap_intent *bi) |
caeaea98 CH |
251 | { |
252 | int error; | |
253 | ||
ddccb81b | 254 | error = xfs_bmap_finish_one(tp, bi); |
caeaea98 CH |
255 | |
256 | /* | |
257 | * Mark the transaction dirty, even on error. This ensures the | |
258 | * transaction is aborted, which: | |
259 | * | |
260 | * 1.) releases the BUI and frees the BUD | |
261 | * 2.) shuts down the filesystem | |
262 | */ | |
bb7b1c9c | 263 | tp->t_flags |= XFS_TRANS_DIRTY | XFS_TRANS_HAS_INTENT_DONE; |
caeaea98 CH |
264 | set_bit(XFS_LI_DIRTY, &budp->bud_item.li_flags); |
265 | ||
266 | return error; | |
267 | } | |
268 | ||
269 | /* Sort bmap intents by inode. */ | |
270 | static int | |
271 | xfs_bmap_update_diff_items( | |
272 | void *priv, | |
4f0f586b ST |
273 | const struct list_head *a, |
274 | const struct list_head *b) | |
caeaea98 CH |
275 | { |
276 | struct xfs_bmap_intent *ba; | |
277 | struct xfs_bmap_intent *bb; | |
278 | ||
279 | ba = container_of(a, struct xfs_bmap_intent, bi_list); | |
280 | bb = container_of(b, struct xfs_bmap_intent, bi_list); | |
281 | return ba->bi_owner->i_ino - bb->bi_owner->i_ino; | |
282 | } | |
283 | ||
caeaea98 CH |
284 | /* Set the map extent flags for this mapping. */ |
285 | static void | |
286 | xfs_trans_set_bmap_flags( | |
f3ebac4c | 287 | struct xfs_map_extent *map, |
caeaea98 CH |
288 | enum xfs_bmap_intent_type type, |
289 | int whichfork, | |
290 | xfs_exntst_t state) | |
291 | { | |
f3ebac4c | 292 | map->me_flags = 0; |
caeaea98 CH |
293 | switch (type) { |
294 | case XFS_BMAP_MAP: | |
295 | case XFS_BMAP_UNMAP: | |
f3ebac4c | 296 | map->me_flags = type; |
caeaea98 CH |
297 | break; |
298 | default: | |
299 | ASSERT(0); | |
300 | } | |
301 | if (state == XFS_EXT_UNWRITTEN) | |
f3ebac4c | 302 | map->me_flags |= XFS_BMAP_EXTENT_UNWRITTEN; |
caeaea98 | 303 | if (whichfork == XFS_ATTR_FORK) |
f3ebac4c | 304 | map->me_flags |= XFS_BMAP_EXTENT_ATTR_FORK; |
caeaea98 CH |
305 | } |
306 | ||
307 | /* Log bmap updates in the intent item. */ | |
308 | STATIC void | |
309 | xfs_bmap_update_log_item( | |
310 | struct xfs_trans *tp, | |
c1f09188 | 311 | struct xfs_bui_log_item *buip, |
f3ebac4c | 312 | struct xfs_bmap_intent *bi) |
caeaea98 | 313 | { |
caeaea98 CH |
314 | uint next_extent; |
315 | struct xfs_map_extent *map; | |
316 | ||
caeaea98 CH |
317 | tp->t_flags |= XFS_TRANS_DIRTY; |
318 | set_bit(XFS_LI_DIRTY, &buip->bui_item.li_flags); | |
319 | ||
320 | /* | |
321 | * atomic_inc_return gives us the value after the increment; | |
322 | * we want to use it as an array index so we need to subtract 1 from | |
323 | * it. | |
324 | */ | |
325 | next_extent = atomic_inc_return(&buip->bui_next_extent) - 1; | |
326 | ASSERT(next_extent < buip->bui_format.bui_nextents); | |
327 | map = &buip->bui_format.bui_extents[next_extent]; | |
f3ebac4c DW |
328 | map->me_owner = bi->bi_owner->i_ino; |
329 | map->me_startblock = bi->bi_bmap.br_startblock; | |
330 | map->me_startoff = bi->bi_bmap.br_startoff; | |
331 | map->me_len = bi->bi_bmap.br_blockcount; | |
332 | xfs_trans_set_bmap_flags(map, bi->bi_type, bi->bi_whichfork, | |
333 | bi->bi_bmap.br_state); | |
caeaea98 CH |
334 | } |
335 | ||
13a83333 | 336 | static struct xfs_log_item * |
c1f09188 CH |
337 | xfs_bmap_update_create_intent( |
338 | struct xfs_trans *tp, | |
339 | struct list_head *items, | |
d367a868 CH |
340 | unsigned int count, |
341 | bool sort) | |
c1f09188 | 342 | { |
d367a868 CH |
343 | struct xfs_mount *mp = tp->t_mountp; |
344 | struct xfs_bui_log_item *buip = xfs_bui_init(mp); | |
f3ebac4c | 345 | struct xfs_bmap_intent *bi; |
c1f09188 CH |
346 | |
347 | ASSERT(count == XFS_BUI_MAX_FAST_EXTENTS); | |
348 | ||
349 | xfs_trans_add_item(tp, &buip->bui_item); | |
d367a868 CH |
350 | if (sort) |
351 | list_sort(mp, items, xfs_bmap_update_diff_items); | |
f3ebac4c DW |
352 | list_for_each_entry(bi, items, bi_list) |
353 | xfs_bmap_update_log_item(tp, buip, bi); | |
13a83333 | 354 | return &buip->bui_item; |
c1f09188 CH |
355 | } |
356 | ||
caeaea98 | 357 | /* Get an BUD so we can process all the deferred rmap updates. */ |
f09d167c | 358 | static struct xfs_log_item * |
caeaea98 CH |
359 | xfs_bmap_update_create_done( |
360 | struct xfs_trans *tp, | |
13a83333 | 361 | struct xfs_log_item *intent, |
caeaea98 CH |
362 | unsigned int count) |
363 | { | |
f09d167c | 364 | return &xfs_trans_get_bud(tp, BUI_ITEM(intent))->bud_item; |
caeaea98 CH |
365 | } |
366 | ||
774a99b4 DW |
367 | /* Take a passive ref to the AG containing the space we're mapping. */ |
368 | void | |
369 | xfs_bmap_update_get_group( | |
370 | struct xfs_mount *mp, | |
371 | struct xfs_bmap_intent *bi) | |
372 | { | |
373 | xfs_agnumber_t agno; | |
374 | ||
375 | agno = XFS_FSB_TO_AGNO(mp, bi->bi_bmap.br_startblock); | |
d5c88131 DW |
376 | |
377 | /* | |
378 | * Bump the intent count on behalf of the deferred rmap and refcount | |
379 | * intent items that that we can queue when we finish this bmap work. | |
380 | * This new intent item will bump the intent count before the bmap | |
381 | * intent drops the intent count, ensuring that the intent count | |
382 | * remains nonzero across the transaction roll. | |
383 | */ | |
384 | bi->bi_pag = xfs_perag_intent_get(mp, agno); | |
774a99b4 DW |
385 | } |
386 | ||
387 | /* Release a passive AG ref after finishing mapping work. */ | |
388 | static inline void | |
389 | xfs_bmap_update_put_group( | |
390 | struct xfs_bmap_intent *bi) | |
391 | { | |
d5c88131 | 392 | xfs_perag_intent_put(bi->bi_pag); |
774a99b4 DW |
393 | } |
394 | ||
caeaea98 CH |
395 | /* Process a deferred rmap update. */ |
396 | STATIC int | |
397 | xfs_bmap_update_finish_item( | |
398 | struct xfs_trans *tp, | |
f09d167c | 399 | struct xfs_log_item *done, |
caeaea98 | 400 | struct list_head *item, |
3ec1b26c | 401 | struct xfs_btree_cur **state) |
caeaea98 | 402 | { |
ddccb81b | 403 | struct xfs_bmap_intent *bi; |
caeaea98 CH |
404 | int error; |
405 | ||
ddccb81b DW |
406 | bi = container_of(item, struct xfs_bmap_intent, bi_list); |
407 | ||
408 | error = xfs_trans_log_finish_bmap_update(tp, BUD_ITEM(done), bi); | |
409 | if (!error && bi->bi_bmap.br_blockcount > 0) { | |
410 | ASSERT(bi->bi_type == XFS_BMAP_UNMAP); | |
caeaea98 CH |
411 | return -EAGAIN; |
412 | } | |
774a99b4 DW |
413 | |
414 | xfs_bmap_update_put_group(bi); | |
ddccb81b | 415 | kmem_cache_free(xfs_bmap_intent_cache, bi); |
caeaea98 CH |
416 | return error; |
417 | } | |
418 | ||
419 | /* Abort all pending BUIs. */ | |
420 | STATIC void | |
421 | xfs_bmap_update_abort_intent( | |
13a83333 | 422 | struct xfs_log_item *intent) |
caeaea98 | 423 | { |
13a83333 | 424 | xfs_bui_release(BUI_ITEM(intent)); |
caeaea98 CH |
425 | } |
426 | ||
774a99b4 | 427 | /* Cancel a deferred bmap update. */ |
caeaea98 CH |
428 | STATIC void |
429 | xfs_bmap_update_cancel_item( | |
430 | struct list_head *item) | |
431 | { | |
f3ebac4c | 432 | struct xfs_bmap_intent *bi; |
caeaea98 | 433 | |
f3ebac4c | 434 | bi = container_of(item, struct xfs_bmap_intent, bi_list); |
774a99b4 DW |
435 | |
436 | xfs_bmap_update_put_group(bi); | |
f3ebac4c | 437 | kmem_cache_free(xfs_bmap_intent_cache, bi); |
caeaea98 CH |
438 | } |
439 | ||
440 | const struct xfs_defer_op_type xfs_bmap_update_defer_type = { | |
441 | .max_items = XFS_BUI_MAX_FAST_EXTENTS, | |
caeaea98 CH |
442 | .create_intent = xfs_bmap_update_create_intent, |
443 | .abort_intent = xfs_bmap_update_abort_intent, | |
caeaea98 CH |
444 | .create_done = xfs_bmap_update_create_done, |
445 | .finish_item = xfs_bmap_update_finish_item, | |
446 | .cancel_item = xfs_bmap_update_cancel_item, | |
447 | }; | |
448 | ||
bc525cf4 DW |
449 | /* Is this recovered BUI ok? */ |
450 | static inline bool | |
451 | xfs_bui_validate( | |
452 | struct xfs_mount *mp, | |
453 | struct xfs_bui_log_item *buip) | |
454 | { | |
f3ebac4c | 455 | struct xfs_map_extent *map; |
bc525cf4 DW |
456 | |
457 | /* Only one mapping operation per BUI... */ | |
458 | if (buip->bui_format.bui_nextents != XFS_BUI_MAX_FAST_EXTENTS) | |
459 | return false; | |
460 | ||
f3ebac4c | 461 | map = &buip->bui_format.bui_extents[0]; |
bc525cf4 | 462 | |
f3ebac4c | 463 | if (map->me_flags & ~XFS_BMAP_EXTENT_FLAGS) |
bc525cf4 DW |
464 | return false; |
465 | ||
f3ebac4c | 466 | switch (map->me_flags & XFS_BMAP_EXTENT_TYPE_MASK) { |
bc525cf4 DW |
467 | case XFS_BMAP_MAP: |
468 | case XFS_BMAP_UNMAP: | |
469 | break; | |
470 | default: | |
471 | return false; | |
472 | } | |
473 | ||
f3ebac4c | 474 | if (!xfs_verify_ino(mp, map->me_owner)) |
67d8679b DW |
475 | return false; |
476 | ||
f3ebac4c | 477 | if (!xfs_verify_fileext(mp, map->me_startoff, map->me_len)) |
67d8679b DW |
478 | return false; |
479 | ||
f3ebac4c | 480 | return xfs_verify_fsbext(mp, map->me_startblock, map->me_len); |
bc525cf4 DW |
481 | } |
482 | ||
77d61fe4 DW |
483 | /* |
484 | * Process a bmap update intent item that was recovered from the log. | |
485 | * We need to update some inode's bmbt. | |
486 | */ | |
9329ba89 | 487 | STATIC int |
96b60f82 DW |
488 | xfs_bui_item_recover( |
489 | struct xfs_log_item *lip, | |
e6fff81e | 490 | struct list_head *capture_list) |
77d61fe4 | 491 | { |
ddccb81b | 492 | struct xfs_bmap_intent fake = { }; |
3c919b09 | 493 | struct xfs_trans_res resv; |
96b60f82 DW |
494 | struct xfs_bui_log_item *buip = BUI_ITEM(lip); |
495 | struct xfs_trans *tp; | |
496 | struct xfs_inode *ip = NULL; | |
d86142dd | 497 | struct xfs_mount *mp = lip->li_log->l_mp; |
ddccb81b | 498 | struct xfs_map_extent *map; |
96b60f82 | 499 | struct xfs_bud_log_item *budp; |
85ef08b5 | 500 | int iext_delta; |
96b60f82 | 501 | int error = 0; |
77d61fe4 | 502 | |
bc525cf4 DW |
503 | if (!xfs_bui_validate(mp, buip)) { |
504 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, | |
505 | &buip->bui_format, sizeof(buip->bui_format)); | |
895e196f | 506 | return -EFSCORRUPTED; |
bc525cf4 | 507 | } |
77d61fe4 | 508 | |
ddccb81b DW |
509 | map = &buip->bui_format.bui_extents[0]; |
510 | fake.bi_whichfork = (map->me_flags & XFS_BMAP_EXTENT_ATTR_FORK) ? | |
919522e8 | 511 | XFS_ATTR_FORK : XFS_DATA_FORK; |
ddccb81b | 512 | fake.bi_type = map->me_flags & XFS_BMAP_EXTENT_TYPE_MASK; |
77d61fe4 | 513 | |
ddccb81b | 514 | error = xlog_recover_iget(mp, map->me_owner, &ip); |
9f3afb57 | 515 | if (error) |
64a3f331 | 516 | return error; |
9f3afb57 | 517 | |
64a3f331 | 518 | /* Allocate transaction and do the work. */ |
3c919b09 DW |
519 | resv = xlog_recover_resv(&M_RES(mp)->tr_itruncate); |
520 | error = xfs_trans_alloc(mp, &resv, | |
64a3f331 DW |
521 | XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK), 0, 0, &tp); |
522 | if (error) | |
523 | goto err_rele; | |
524 | ||
525 | budp = xfs_trans_get_bud(tp, buip); | |
526 | xfs_ilock(ip, XFS_ILOCK_EXCL); | |
9f3afb57 DW |
527 | xfs_trans_ijoin(tp, ip, 0); |
528 | ||
ddccb81b | 529 | if (fake.bi_type == XFS_BMAP_MAP) |
85ef08b5 CB |
530 | iext_delta = XFS_IEXT_ADD_NOSPLIT_CNT; |
531 | else | |
532 | iext_delta = XFS_IEXT_PUNCH_HOLE_CNT; | |
533 | ||
ddccb81b | 534 | error = xfs_iext_count_may_overflow(ip, fake.bi_whichfork, iext_delta); |
4f86bb4b CB |
535 | if (error == -EFBIG) |
536 | error = xfs_iext_count_upgrade(tp, ip, iext_delta); | |
85ef08b5 CB |
537 | if (error) |
538 | goto err_cancel; | |
727e1acd | 539 | |
ddccb81b DW |
540 | fake.bi_owner = ip; |
541 | fake.bi_bmap.br_startblock = map->me_startblock; | |
542 | fake.bi_bmap.br_startoff = map->me_startoff; | |
543 | fake.bi_bmap.br_blockcount = map->me_len; | |
544 | fake.bi_bmap.br_state = (map->me_flags & XFS_BMAP_EXTENT_UNWRITTEN) ? | |
545 | XFS_EXT_UNWRITTEN : XFS_EXT_NORM; | |
546 | ||
774a99b4 | 547 | xfs_bmap_update_get_group(mp, &fake); |
ddccb81b | 548 | error = xfs_trans_log_finish_bmap_update(tp, budp, &fake); |
43059d54 | 549 | if (error == -EFSCORRUPTED) |
ddccb81b DW |
550 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, map, |
551 | sizeof(*map)); | |
774a99b4 | 552 | xfs_bmap_update_put_group(&fake); |
9f3afb57 | 553 | if (error) |
64a3f331 | 554 | goto err_cancel; |
9f3afb57 | 555 | |
ddccb81b DW |
556 | if (fake.bi_bmap.br_blockcount > 0) { |
557 | ASSERT(fake.bi_type == XFS_BMAP_UNMAP); | |
558 | xfs_bmap_unmap_extent(tp, ip, &fake.bi_bmap); | |
e1a4e37c DW |
559 | } |
560 | ||
ff4ab5e0 DW |
561 | /* |
562 | * Commit transaction, which frees the transaction and saves the inode | |
563 | * for later replay activities. | |
564 | */ | |
512edfac | 565 | error = xfs_defer_ops_capture_and_commit(tp, capture_list); |
64a3f331 DW |
566 | if (error) |
567 | goto err_unlock; | |
568 | ||
9f3afb57 | 569 | xfs_iunlock(ip, XFS_ILOCK_EXCL); |
44a8736b | 570 | xfs_irele(ip); |
64a3f331 | 571 | return 0; |
9f3afb57 | 572 | |
64a3f331 | 573 | err_cancel: |
9f3afb57 | 574 | xfs_trans_cancel(tp); |
64a3f331 DW |
575 | err_unlock: |
576 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
577 | err_rele: | |
578 | xfs_irele(ip); | |
77d61fe4 DW |
579 | return error; |
580 | } | |
86ffa471 | 581 | |
154c733a DW |
582 | STATIC bool |
583 | xfs_bui_item_match( | |
584 | struct xfs_log_item *lip, | |
585 | uint64_t intent_id) | |
586 | { | |
587 | return BUI_ITEM(lip)->bui_format.bui_id == intent_id; | |
588 | } | |
589 | ||
4e919af7 DW |
590 | /* Relog an intent item to push the log tail forward. */ |
591 | static struct xfs_log_item * | |
592 | xfs_bui_item_relog( | |
593 | struct xfs_log_item *intent, | |
594 | struct xfs_trans *tp) | |
595 | { | |
596 | struct xfs_bud_log_item *budp; | |
597 | struct xfs_bui_log_item *buip; | |
f3ebac4c | 598 | struct xfs_map_extent *map; |
4e919af7 DW |
599 | unsigned int count; |
600 | ||
601 | count = BUI_ITEM(intent)->bui_format.bui_nextents; | |
f3ebac4c | 602 | map = BUI_ITEM(intent)->bui_format.bui_extents; |
4e919af7 DW |
603 | |
604 | tp->t_flags |= XFS_TRANS_DIRTY; | |
605 | budp = xfs_trans_get_bud(tp, BUI_ITEM(intent)); | |
606 | set_bit(XFS_LI_DIRTY, &budp->bud_item.li_flags); | |
607 | ||
608 | buip = xfs_bui_init(tp->t_mountp); | |
f3ebac4c | 609 | memcpy(buip->bui_format.bui_extents, map, count * sizeof(*map)); |
4e919af7 DW |
610 | atomic_set(&buip->bui_next_extent, count); |
611 | xfs_trans_add_item(tp, &buip->bui_item); | |
612 | set_bit(XFS_LI_DIRTY, &buip->bui_item.li_flags); | |
613 | return &buip->bui_item; | |
614 | } | |
615 | ||
9329ba89 | 616 | static const struct xfs_item_ops xfs_bui_item_ops = { |
f5b81200 | 617 | .flags = XFS_ITEM_INTENT, |
9329ba89 DW |
618 | .iop_size = xfs_bui_item_size, |
619 | .iop_format = xfs_bui_item_format, | |
620 | .iop_unpin = xfs_bui_item_unpin, | |
621 | .iop_release = xfs_bui_item_release, | |
622 | .iop_recover = xfs_bui_item_recover, | |
154c733a | 623 | .iop_match = xfs_bui_item_match, |
4e919af7 | 624 | .iop_relog = xfs_bui_item_relog, |
9329ba89 DW |
625 | }; |
626 | ||
a38ebce1 | 627 | static inline void |
3c6ba3cf | 628 | xfs_bui_copy_format( |
a38ebce1 DW |
629 | struct xfs_bui_log_format *dst, |
630 | const struct xfs_bui_log_format *src) | |
3c6ba3cf | 631 | { |
a38ebce1 | 632 | unsigned int i; |
3c6ba3cf | 633 | |
a38ebce1 | 634 | memcpy(dst, src, offsetof(struct xfs_bui_log_format, bui_extents)); |
3c6ba3cf | 635 | |
a38ebce1 DW |
636 | for (i = 0; i < src->bui_nextents; i++) |
637 | memcpy(&dst->bui_extents[i], &src->bui_extents[i], | |
638 | sizeof(struct xfs_map_extent)); | |
3c6ba3cf DW |
639 | } |
640 | ||
641 | /* | |
642 | * This routine is called to create an in-core extent bmap update | |
643 | * item from the bui format structure which was logged on disk. | |
644 | * It allocates an in-core bui, copies the extents from the format | |
645 | * structure into it, and adds the bui to the AIL with the given | |
646 | * LSN. | |
647 | */ | |
648 | STATIC int | |
649 | xlog_recover_bui_commit_pass2( | |
650 | struct xlog *log, | |
651 | struct list_head *buffer_list, | |
652 | struct xlog_recover_item *item, | |
653 | xfs_lsn_t lsn) | |
654 | { | |
3c6ba3cf DW |
655 | struct xfs_mount *mp = log->l_mp; |
656 | struct xfs_bui_log_item *buip; | |
657 | struct xfs_bui_log_format *bui_formatp; | |
a38ebce1 | 658 | size_t len; |
3c6ba3cf DW |
659 | |
660 | bui_formatp = item->ri_buf[0].i_addr; | |
661 | ||
a38ebce1 | 662 | if (item->ri_buf[0].i_len < xfs_bui_log_format_sizeof(0)) { |
950f0d50 DW |
663 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, |
664 | item->ri_buf[0].i_addr, item->ri_buf[0].i_len); | |
a38ebce1 DW |
665 | return -EFSCORRUPTED; |
666 | } | |
667 | ||
3c6ba3cf | 668 | if (bui_formatp->bui_nextents != XFS_BUI_MAX_FAST_EXTENTS) { |
950f0d50 DW |
669 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, |
670 | item->ri_buf[0].i_addr, item->ri_buf[0].i_len); | |
3c6ba3cf DW |
671 | return -EFSCORRUPTED; |
672 | } | |
a38ebce1 DW |
673 | |
674 | len = xfs_bui_log_format_sizeof(bui_formatp->bui_nextents); | |
675 | if (item->ri_buf[0].i_len != len) { | |
950f0d50 DW |
676 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, |
677 | item->ri_buf[0].i_addr, item->ri_buf[0].i_len); | |
a38ebce1 | 678 | return -EFSCORRUPTED; |
3c6ba3cf | 679 | } |
a38ebce1 DW |
680 | |
681 | buip = xfs_bui_init(mp); | |
682 | xfs_bui_copy_format(&buip->bui_format, bui_formatp); | |
3c6ba3cf | 683 | atomic_set(&buip->bui_next_extent, bui_formatp->bui_nextents); |
3c6ba3cf | 684 | /* |
86a37174 DW |
685 | * Insert the intent into the AIL directly and drop one reference so |
686 | * that finishing or canceling the work will drop the other. | |
3c6ba3cf | 687 | */ |
86a37174 | 688 | xfs_trans_ail_insert(log->l_ailp, &buip->bui_item, lsn); |
3c6ba3cf DW |
689 | xfs_bui_release(buip); |
690 | return 0; | |
691 | } | |
692 | ||
86ffa471 DW |
693 | const struct xlog_recover_item_ops xlog_bui_item_ops = { |
694 | .item_type = XFS_LI_BUI, | |
3c6ba3cf | 695 | .commit_pass2 = xlog_recover_bui_commit_pass2, |
86ffa471 DW |
696 | }; |
697 | ||
3c6ba3cf DW |
698 | /* |
699 | * This routine is called when an BUD format structure is found in a committed | |
700 | * transaction in the log. Its purpose is to cancel the corresponding BUI if it | |
701 | * was still in the log. To do this it searches the AIL for the BUI with an id | |
702 | * equal to that in the BUD format structure. If we find it we drop the BUD | |
703 | * reference, which removes the BUI from the AIL and frees it. | |
704 | */ | |
705 | STATIC int | |
706 | xlog_recover_bud_commit_pass2( | |
707 | struct xlog *log, | |
708 | struct list_head *buffer_list, | |
709 | struct xlog_recover_item *item, | |
710 | xfs_lsn_t lsn) | |
711 | { | |
712 | struct xfs_bud_log_format *bud_formatp; | |
3c6ba3cf DW |
713 | |
714 | bud_formatp = item->ri_buf[0].i_addr; | |
715 | if (item->ri_buf[0].i_len != sizeof(struct xfs_bud_log_format)) { | |
950f0d50 DW |
716 | XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp, |
717 | item->ri_buf[0].i_addr, item->ri_buf[0].i_len); | |
3c6ba3cf DW |
718 | return -EFSCORRUPTED; |
719 | } | |
3c6ba3cf | 720 | |
154c733a | 721 | xlog_recover_release_intent(log, XFS_LI_BUI, bud_formatp->bud_bui_id); |
3c6ba3cf DW |
722 | return 0; |
723 | } | |
724 | ||
86ffa471 DW |
725 | const struct xlog_recover_item_ops xlog_bud_item_ops = { |
726 | .item_type = XFS_LI_BUD, | |
3c6ba3cf | 727 | .commit_pass2 = xlog_recover_bud_commit_pass2, |
86ffa471 | 728 | }; |