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