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