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