xref: /linux/fs/xfs/xfs_bmap_item.c (revision 3fd6c59042dbba50391e30862beac979491145fe)
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 #include "xfs_ag.h"
28 #include "xfs_trace.h"
29 
30 struct kmem_cache	*xfs_bui_cache;
31 struct kmem_cache	*xfs_bud_cache;
32 
33 static const struct xfs_item_ops xfs_bui_item_ops;
34 
BUI_ITEM(struct xfs_log_item * lip)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 
40 STATIC void
xfs_bui_item_free(struct xfs_bui_log_item * buip)41 xfs_bui_item_free(
42 	struct xfs_bui_log_item	*buip)
43 {
44 	kvfree(buip->bui_item.li_lv_shadow);
45 	kmem_cache_free(xfs_bui_cache, buip);
46 }
47 
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  */
55 STATIC void
xfs_bui_release(struct xfs_bui_log_item * buip)56 xfs_bui_release(
57 	struct xfs_bui_log_item	*buip)
58 {
59 	ASSERT(atomic_read(&buip->bui_refcount) > 0);
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);
65 }
66 
67 
68 STATIC void
xfs_bui_item_size(struct xfs_log_item * lip,int * nvecs,int * nbytes)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
xfs_bui_item_format(struct xfs_log_item * lip,struct xfs_log_vec * lv)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 
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
xfs_bui_item_unpin(struct xfs_log_item * lip,int remove)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 
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
xfs_bui_item_release(struct xfs_log_item * lip)129 xfs_bui_item_release(
130 	struct xfs_log_item	*lip)
131 {
132 	xfs_bui_release(BUI_ITEM(lip));
133 }
134 
135 /*
136  * Allocate and initialize an bui item with the given number of extents.
137  */
138 STATIC struct xfs_bui_log_item *
xfs_bui_init(struct xfs_mount * mp)139 xfs_bui_init(
140 	struct xfs_mount		*mp)
141 
142 {
143 	struct xfs_bui_log_item		*buip;
144 
145 	buip = kmem_cache_zalloc(xfs_bui_cache, GFP_KERNEL | __GFP_NOFAIL);
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 
BUD_ITEM(struct xfs_log_item * lip)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
xfs_bud_item_size(struct xfs_log_item * lip,int * nvecs,int * nbytes)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
xfs_bud_item_format(struct xfs_log_item * lip,struct xfs_log_vec * lv)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 
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
xfs_bud_item_release(struct xfs_log_item * lip)199 xfs_bud_item_release(
200 	struct xfs_log_item	*lip)
201 {
202 	struct xfs_bud_log_item	*budp = BUD_ITEM(lip);
203 
204 	xfs_bui_release(budp->bud_buip);
205 	kvfree(budp->bud_item.li_lv_shadow);
206 	kmem_cache_free(xfs_bud_cache, budp);
207 }
208 
209 static struct xfs_log_item *
xfs_bud_item_intent(struct xfs_log_item * lip)210 xfs_bud_item_intent(
211 	struct xfs_log_item	*lip)
212 {
213 	return &BUD_ITEM(lip)->bud_buip->bui_item;
214 }
215 
216 static const struct xfs_item_ops xfs_bud_item_ops = {
217 	.flags		= XFS_ITEM_RELEASE_WHEN_COMMITTED |
218 			  XFS_ITEM_INTENT_DONE,
219 	.iop_size	= xfs_bud_item_size,
220 	.iop_format	= xfs_bud_item_format,
221 	.iop_release	= xfs_bud_item_release,
222 	.iop_intent	= xfs_bud_item_intent,
223 };
224 
bi_entry(const struct list_head * e)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 
230 /* Sort bmap intents by inode. */
231 static int
xfs_bmap_update_diff_items(void * priv,const struct list_head * a,const struct list_head * b)232 xfs_bmap_update_diff_items(
233 	void				*priv,
234 	const struct list_head		*a,
235 	const struct list_head		*b)
236 {
237 	struct xfs_bmap_intent		*ba = bi_entry(a);
238 	struct xfs_bmap_intent		*bb = bi_entry(b);
239 
240 	return ba->bi_owner->i_ino - bb->bi_owner->i_ino;
241 }
242 
243 /* Log bmap updates in the intent item. */
244 STATIC void
xfs_bmap_update_log_item(struct xfs_trans * tp,struct xfs_bui_log_item * buip,struct xfs_bmap_intent * bi)245 xfs_bmap_update_log_item(
246 	struct xfs_trans		*tp,
247 	struct xfs_bui_log_item		*buip,
248 	struct xfs_bmap_intent		*bi)
249 {
250 	uint				next_extent;
251 	struct xfs_map_extent		*map;
252 
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];
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;
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;
278 	if (xfs_ifork_is_realtime(bi->bi_owner, bi->bi_whichfork))
279 		map->me_flags |= XFS_BMAP_EXTENT_REALTIME;
280 }
281 
282 static struct xfs_log_item *
xfs_bmap_update_create_intent(struct xfs_trans * tp,struct list_head * items,unsigned int count,bool sort)283 xfs_bmap_update_create_intent(
284 	struct xfs_trans		*tp,
285 	struct list_head		*items,
286 	unsigned int			count,
287 	bool				sort)
288 {
289 	struct xfs_mount		*mp = tp->t_mountp;
290 	struct xfs_bui_log_item		*buip = xfs_bui_init(mp);
291 	struct xfs_bmap_intent		*bi;
292 
293 	ASSERT(count == XFS_BUI_MAX_FAST_EXTENTS);
294 
295 	if (sort)
296 		list_sort(mp, items, xfs_bmap_update_diff_items);
297 	list_for_each_entry(bi, items, bi_list)
298 		xfs_bmap_update_log_item(tp, buip, bi);
299 	return &buip->bui_item;
300 }
301 
302 /* Get an BUD so we can process all the deferred bmap updates. */
303 static struct xfs_log_item *
xfs_bmap_update_create_done(struct xfs_trans * tp,struct xfs_log_item * intent,unsigned int count)304 xfs_bmap_update_create_done(
305 	struct xfs_trans		*tp,
306 	struct xfs_log_item		*intent,
307 	unsigned int			count)
308 {
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;
319 }
320 
321 /* Take a passive ref to the group containing the space we're mapping. */
322 static inline void
xfs_bmap_update_get_group(struct xfs_mount * mp,struct xfs_bmap_intent * bi)323 xfs_bmap_update_get_group(
324 	struct xfs_mount	*mp,
325 	struct xfs_bmap_intent	*bi)
326 {
327 	enum xfs_group_type	type = XG_TYPE_AG;
328 
329 	if (xfs_ifork_is_realtime(bi->bi_owner, bi->bi_whichfork))
330 		type = XG_TYPE_RTG;
331 
332 	/*
333 	 * Bump the intent count on behalf of the deferred rmap and refcount
334 	 * intent items that that we can queue when we finish this bmap work.
335 	 * This new intent item will bump the intent count before the bmap
336 	 * intent drops the intent count, ensuring that the intent count
337 	 * remains nonzero across the transaction roll.
338 	 */
339 	bi->bi_group = xfs_group_intent_get(mp, bi->bi_bmap.br_startblock,
340 				type);
341 }
342 
343 /* Add this deferred BUI to the transaction. */
344 void
xfs_bmap_defer_add(struct xfs_trans * tp,struct xfs_bmap_intent * bi)345 xfs_bmap_defer_add(
346 	struct xfs_trans	*tp,
347 	struct xfs_bmap_intent	*bi)
348 {
349 	xfs_bmap_update_get_group(tp->t_mountp, bi);
350 
351 	/*
352 	 * Ensure the deferred mapping is pre-recorded in i_delayed_blks.
353 	 *
354 	 * Otherwise stat can report zero blocks for an inode that actually has
355 	 * data when the entire mapping is in the process of being overwritten
356 	 * using the out of place write path. This is undone in xfs_bmapi_remap
357 	 * after it has incremented di_nblocks for a successful operation.
358 	 */
359 	if (bi->bi_type == XFS_BMAP_MAP)
360 		bi->bi_owner->i_delayed_blks += bi->bi_bmap.br_blockcount;
361 
362 	trace_xfs_bmap_defer(bi);
363 	xfs_defer_add(tp, &bi->bi_list, &xfs_bmap_update_defer_type);
364 }
365 
366 /* Cancel a deferred bmap update. */
367 STATIC void
xfs_bmap_update_cancel_item(struct list_head * item)368 xfs_bmap_update_cancel_item(
369 	struct list_head		*item)
370 {
371 	struct xfs_bmap_intent		*bi = bi_entry(item);
372 
373 	if (bi->bi_type == XFS_BMAP_MAP)
374 		bi->bi_owner->i_delayed_blks -= bi->bi_bmap.br_blockcount;
375 
376 	xfs_group_intent_put(bi->bi_group);
377 	kmem_cache_free(xfs_bmap_intent_cache, bi);
378 }
379 
380 /* Process a deferred bmap update. */
381 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)382 xfs_bmap_update_finish_item(
383 	struct xfs_trans		*tp,
384 	struct xfs_log_item		*done,
385 	struct list_head		*item,
386 	struct xfs_btree_cur		**state)
387 {
388 	struct xfs_bmap_intent		*bi = bi_entry(item);
389 	int				error;
390 
391 	error = xfs_bmap_finish_one(tp, bi);
392 	if (!error && bi->bi_bmap.br_blockcount > 0) {
393 		ASSERT(bi->bi_type == XFS_BMAP_UNMAP);
394 		return -EAGAIN;
395 	}
396 
397 	xfs_bmap_update_cancel_item(item);
398 	return error;
399 }
400 
401 /* Abort all pending BUIs. */
402 STATIC void
xfs_bmap_update_abort_intent(struct xfs_log_item * intent)403 xfs_bmap_update_abort_intent(
404 	struct xfs_log_item		*intent)
405 {
406 	xfs_bui_release(BUI_ITEM(intent));
407 }
408 
409 /* Is this recovered BUI ok? */
410 static inline bool
xfs_bui_validate(struct xfs_mount * mp,struct xfs_bui_log_item * buip)411 xfs_bui_validate(
412 	struct xfs_mount		*mp,
413 	struct xfs_bui_log_item		*buip)
414 {
415 	struct xfs_map_extent		*map;
416 
417 	/* Only one mapping operation per BUI... */
418 	if (buip->bui_format.bui_nextents != XFS_BUI_MAX_FAST_EXTENTS)
419 		return false;
420 
421 	map = &buip->bui_format.bui_extents[0];
422 
423 	if (map->me_flags & ~XFS_BMAP_EXTENT_FLAGS)
424 		return false;
425 
426 	switch (map->me_flags & XFS_BMAP_EXTENT_TYPE_MASK) {
427 	case XFS_BMAP_MAP:
428 	case XFS_BMAP_UNMAP:
429 		break;
430 	default:
431 		return false;
432 	}
433 
434 	if (!xfs_verify_ino(mp, map->me_owner))
435 		return false;
436 
437 	if (!xfs_verify_fileext(mp, map->me_startoff, map->me_len))
438 		return false;
439 
440 	if (map->me_flags & XFS_BMAP_EXTENT_REALTIME)
441 		return xfs_verify_rtbext(mp, map->me_startblock, map->me_len);
442 
443 	return xfs_verify_fsbext(mp, map->me_startblock, map->me_len);
444 }
445 
446 static inline struct xfs_bmap_intent *
xfs_bui_recover_work(struct xfs_mount * mp,struct xfs_defer_pending * dfp,struct xfs_inode ** ipp,struct xfs_map_extent * map)447 xfs_bui_recover_work(
448 	struct xfs_mount		*mp,
449 	struct xfs_defer_pending	*dfp,
450 	struct xfs_inode		**ipp,
451 	struct xfs_map_extent		*map)
452 {
453 	struct xfs_bmap_intent		*bi;
454 	int				error;
455 
456 	error = xlog_recover_iget(mp, map->me_owner, ipp);
457 	if (error)
458 		return ERR_PTR(error);
459 
460 	bi = kmem_cache_zalloc(xfs_bmap_intent_cache,
461 			GFP_KERNEL | __GFP_NOFAIL);
462 	bi->bi_whichfork = (map->me_flags & XFS_BMAP_EXTENT_ATTR_FORK) ?
463 			XFS_ATTR_FORK : XFS_DATA_FORK;
464 	bi->bi_type = map->me_flags & XFS_BMAP_EXTENT_TYPE_MASK;
465 	bi->bi_bmap.br_startblock = map->me_startblock;
466 	bi->bi_bmap.br_startoff = map->me_startoff;
467 	bi->bi_bmap.br_blockcount = map->me_len;
468 	bi->bi_bmap.br_state = (map->me_flags & XFS_BMAP_EXTENT_UNWRITTEN) ?
469 			XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
470 	bi->bi_owner = *ipp;
471 	xfs_bmap_update_get_group(mp, bi);
472 
473 	/* see xfs_bmap_defer_add for details */
474 	if (bi->bi_type == XFS_BMAP_MAP)
475 		bi->bi_owner->i_delayed_blks += bi->bi_bmap.br_blockcount;
476 	xfs_defer_add_item(dfp, &bi->bi_list);
477 	return bi;
478 }
479 
480 /*
481  * Process a bmap update intent item that was recovered from the log.
482  * We need to update some inode's bmbt.
483  */
484 STATIC int
xfs_bmap_recover_work(struct xfs_defer_pending * dfp,struct list_head * capture_list)485 xfs_bmap_recover_work(
486 	struct xfs_defer_pending	*dfp,
487 	struct list_head		*capture_list)
488 {
489 	struct xfs_trans_res		resv;
490 	struct xfs_log_item		*lip = dfp->dfp_intent;
491 	struct xfs_bui_log_item		*buip = BUI_ITEM(lip);
492 	struct xfs_trans		*tp;
493 	struct xfs_inode		*ip = NULL;
494 	struct xfs_mount		*mp = lip->li_log->l_mp;
495 	struct xfs_map_extent		*map;
496 	struct xfs_bmap_intent		*work;
497 	int				iext_delta;
498 	int				error = 0;
499 
500 	if (!xfs_bui_validate(mp, buip)) {
501 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
502 				&buip->bui_format, sizeof(buip->bui_format));
503 		return -EFSCORRUPTED;
504 	}
505 
506 	map = &buip->bui_format.bui_extents[0];
507 	work = xfs_bui_recover_work(mp, dfp, &ip, map);
508 	if (IS_ERR(work))
509 		return PTR_ERR(work);
510 
511 	/* Allocate transaction and do the work. */
512 	resv = xlog_recover_resv(&M_RES(mp)->tr_itruncate);
513 	error = xfs_trans_alloc(mp, &resv,
514 			XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK), 0, 0, &tp);
515 	if (error)
516 		goto err_rele;
517 
518 	xfs_ilock(ip, XFS_ILOCK_EXCL);
519 	xfs_trans_ijoin(tp, ip, 0);
520 
521 	if (!!(map->me_flags & XFS_BMAP_EXTENT_REALTIME) !=
522 	    xfs_ifork_is_realtime(ip, work->bi_whichfork)) {
523 		error = -EFSCORRUPTED;
524 		goto err_cancel;
525 	}
526 
527 	if (work->bi_type == XFS_BMAP_MAP)
528 		iext_delta = XFS_IEXT_ADD_NOSPLIT_CNT;
529 	else
530 		iext_delta = XFS_IEXT_PUNCH_HOLE_CNT;
531 
532 	error = xfs_iext_count_extend(tp, ip, work->bi_whichfork, iext_delta);
533 	if (error)
534 		goto err_cancel;
535 
536 	error = xlog_recover_finish_intent(tp, dfp);
537 	if (error == -EFSCORRUPTED)
538 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
539 				&buip->bui_format, sizeof(buip->bui_format));
540 	if (error)
541 		goto err_cancel;
542 
543 	/*
544 	 * Commit transaction, which frees the transaction and saves the inode
545 	 * for later replay activities.
546 	 */
547 	error = xfs_defer_ops_capture_and_commit(tp, capture_list);
548 	if (error)
549 		goto err_unlock;
550 
551 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
552 	xfs_irele(ip);
553 	return 0;
554 
555 err_cancel:
556 	xfs_trans_cancel(tp);
557 err_unlock:
558 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
559 err_rele:
560 	xfs_irele(ip);
561 	return error;
562 }
563 
564 /* Relog an intent item to push the log tail forward. */
565 static struct xfs_log_item *
xfs_bmap_relog_intent(struct xfs_trans * tp,struct xfs_log_item * intent,struct xfs_log_item * done_item)566 xfs_bmap_relog_intent(
567 	struct xfs_trans		*tp,
568 	struct xfs_log_item		*intent,
569 	struct xfs_log_item		*done_item)
570 {
571 	struct xfs_bui_log_item		*buip;
572 	struct xfs_map_extent		*map;
573 	unsigned int			count;
574 
575 	count = BUI_ITEM(intent)->bui_format.bui_nextents;
576 	map = BUI_ITEM(intent)->bui_format.bui_extents;
577 
578 	buip = xfs_bui_init(tp->t_mountp);
579 	memcpy(buip->bui_format.bui_extents, map, count * sizeof(*map));
580 	atomic_set(&buip->bui_next_extent, count);
581 
582 	return &buip->bui_item;
583 }
584 
585 const struct xfs_defer_op_type xfs_bmap_update_defer_type = {
586 	.name		= "bmap",
587 	.max_items	= XFS_BUI_MAX_FAST_EXTENTS,
588 	.create_intent	= xfs_bmap_update_create_intent,
589 	.abort_intent	= xfs_bmap_update_abort_intent,
590 	.create_done	= xfs_bmap_update_create_done,
591 	.finish_item	= xfs_bmap_update_finish_item,
592 	.cancel_item	= xfs_bmap_update_cancel_item,
593 	.recover_work	= xfs_bmap_recover_work,
594 	.relog_intent	= xfs_bmap_relog_intent,
595 };
596 
597 STATIC bool
xfs_bui_item_match(struct xfs_log_item * lip,uint64_t intent_id)598 xfs_bui_item_match(
599 	struct xfs_log_item	*lip,
600 	uint64_t		intent_id)
601 {
602 	return BUI_ITEM(lip)->bui_format.bui_id == intent_id;
603 }
604 
605 static const struct xfs_item_ops xfs_bui_item_ops = {
606 	.flags		= XFS_ITEM_INTENT,
607 	.iop_size	= xfs_bui_item_size,
608 	.iop_format	= xfs_bui_item_format,
609 	.iop_unpin	= xfs_bui_item_unpin,
610 	.iop_release	= xfs_bui_item_release,
611 	.iop_match	= xfs_bui_item_match,
612 };
613 
614 static inline void
xfs_bui_copy_format(struct xfs_bui_log_format * dst,const struct xfs_bui_log_format * src)615 xfs_bui_copy_format(
616 	struct xfs_bui_log_format	*dst,
617 	const struct xfs_bui_log_format	*src)
618 {
619 	unsigned int			i;
620 
621 	memcpy(dst, src, offsetof(struct xfs_bui_log_format, bui_extents));
622 
623 	for (i = 0; i < src->bui_nextents; i++)
624 		memcpy(&dst->bui_extents[i], &src->bui_extents[i],
625 				sizeof(struct xfs_map_extent));
626 }
627 
628 /*
629  * This routine is called to create an in-core extent bmap update
630  * item from the bui format structure which was logged on disk.
631  * It allocates an in-core bui, copies the extents from the format
632  * structure into it, and adds the bui to the AIL with the given
633  * LSN.
634  */
635 STATIC int
xlog_recover_bui_commit_pass2(struct xlog * log,struct list_head * buffer_list,struct xlog_recover_item * item,xfs_lsn_t lsn)636 xlog_recover_bui_commit_pass2(
637 	struct xlog			*log,
638 	struct list_head		*buffer_list,
639 	struct xlog_recover_item	*item,
640 	xfs_lsn_t			lsn)
641 {
642 	struct xfs_mount		*mp = log->l_mp;
643 	struct xfs_bui_log_item		*buip;
644 	struct xfs_bui_log_format	*bui_formatp;
645 	size_t				len;
646 
647 	bui_formatp = item->ri_buf[0].i_addr;
648 
649 	if (item->ri_buf[0].i_len < xfs_bui_log_format_sizeof(0)) {
650 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
651 				item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
652 		return -EFSCORRUPTED;
653 	}
654 
655 	if (bui_formatp->bui_nextents != XFS_BUI_MAX_FAST_EXTENTS) {
656 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
657 				item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
658 		return -EFSCORRUPTED;
659 	}
660 
661 	len = xfs_bui_log_format_sizeof(bui_formatp->bui_nextents);
662 	if (item->ri_buf[0].i_len != len) {
663 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
664 				item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
665 		return -EFSCORRUPTED;
666 	}
667 
668 	buip = xfs_bui_init(mp);
669 	xfs_bui_copy_format(&buip->bui_format, bui_formatp);
670 	atomic_set(&buip->bui_next_extent, bui_formatp->bui_nextents);
671 
672 	xlog_recover_intent_item(log, &buip->bui_item, lsn,
673 			&xfs_bmap_update_defer_type);
674 	return 0;
675 }
676 
677 const struct xlog_recover_item_ops xlog_bui_item_ops = {
678 	.item_type		= XFS_LI_BUI,
679 	.commit_pass2		= xlog_recover_bui_commit_pass2,
680 };
681 
682 /*
683  * This routine is called when an BUD format structure is found in a committed
684  * transaction in the log. Its purpose is to cancel the corresponding BUI if it
685  * was still in the log. To do this it searches the AIL for the BUI with an id
686  * equal to that in the BUD format structure. If we find it we drop the BUD
687  * reference, which removes the BUI from the AIL and frees it.
688  */
689 STATIC int
xlog_recover_bud_commit_pass2(struct xlog * log,struct list_head * buffer_list,struct xlog_recover_item * item,xfs_lsn_t lsn)690 xlog_recover_bud_commit_pass2(
691 	struct xlog			*log,
692 	struct list_head		*buffer_list,
693 	struct xlog_recover_item	*item,
694 	xfs_lsn_t			lsn)
695 {
696 	struct xfs_bud_log_format	*bud_formatp;
697 
698 	bud_formatp = item->ri_buf[0].i_addr;
699 	if (item->ri_buf[0].i_len != sizeof(struct xfs_bud_log_format)) {
700 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
701 				item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
702 		return -EFSCORRUPTED;
703 	}
704 
705 	xlog_recover_release_intent(log, XFS_LI_BUI, bud_formatp->bud_bui_id);
706 	return 0;
707 }
708 
709 const struct xlog_recover_item_ops xlog_bud_item_ops = {
710 	.item_type		= XFS_LI_BUD,
711 	.commit_pass2		= xlog_recover_bud_commit_pass2,
712 };
713