xref: /linux/fs/xfs/xfs_rmap_item.c (revision 119ff04864a24470b1e531bb53e5c141aa8fefb0)
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_trans.h"
16 #include "xfs_trans_priv.h"
17 #include "xfs_rmap_item.h"
18 #include "xfs_log.h"
19 #include "xfs_rmap.h"
20 #include "xfs_error.h"
21 #include "xfs_log_priv.h"
22 #include "xfs_log_recover.h"
23 #include "xfs_ag.h"
24 
25 struct kmem_cache	*xfs_rui_cache;
26 struct kmem_cache	*xfs_rud_cache;
27 
28 static const struct xfs_item_ops xfs_rui_item_ops;
29 
30 static inline struct xfs_rui_log_item *RUI_ITEM(struct xfs_log_item *lip)
31 {
32 	return container_of(lip, struct xfs_rui_log_item, rui_item);
33 }
34 
35 STATIC void
36 xfs_rui_item_free(
37 	struct xfs_rui_log_item	*ruip)
38 {
39 	kmem_free(ruip->rui_item.li_lv_shadow);
40 	if (ruip->rui_format.rui_nextents > XFS_RUI_MAX_FAST_EXTENTS)
41 		kmem_free(ruip);
42 	else
43 		kmem_cache_free(xfs_rui_cache, ruip);
44 }
45 
46 /*
47  * Freeing the RUI requires that we remove it from the AIL if it has already
48  * been placed there. However, the RUI may not yet have been placed in the AIL
49  * when called by xfs_rui_release() from RUD 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 RUI.
52  */
53 STATIC void
54 xfs_rui_release(
55 	struct xfs_rui_log_item	*ruip)
56 {
57 	ASSERT(atomic_read(&ruip->rui_refcount) > 0);
58 	if (!atomic_dec_and_test(&ruip->rui_refcount))
59 		return;
60 
61 	xfs_trans_ail_delete(&ruip->rui_item, 0);
62 	xfs_rui_item_free(ruip);
63 }
64 
65 STATIC void
66 xfs_rui_item_size(
67 	struct xfs_log_item	*lip,
68 	int			*nvecs,
69 	int			*nbytes)
70 {
71 	struct xfs_rui_log_item	*ruip = RUI_ITEM(lip);
72 
73 	*nvecs += 1;
74 	*nbytes += xfs_rui_log_format_sizeof(ruip->rui_format.rui_nextents);
75 }
76 
77 /*
78  * This is called to fill in the vector of log iovecs for the
79  * given rui log item. We use only 1 iovec, and we point that
80  * at the rui_log_format structure embedded in the rui item.
81  * It is at this point that we assert that all of the extent
82  * slots in the rui item have been filled.
83  */
84 STATIC void
85 xfs_rui_item_format(
86 	struct xfs_log_item	*lip,
87 	struct xfs_log_vec	*lv)
88 {
89 	struct xfs_rui_log_item	*ruip = RUI_ITEM(lip);
90 	struct xfs_log_iovec	*vecp = NULL;
91 
92 	ASSERT(atomic_read(&ruip->rui_next_extent) ==
93 			ruip->rui_format.rui_nextents);
94 
95 	ruip->rui_format.rui_type = XFS_LI_RUI;
96 	ruip->rui_format.rui_size = 1;
97 
98 	xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUI_FORMAT, &ruip->rui_format,
99 			xfs_rui_log_format_sizeof(ruip->rui_format.rui_nextents));
100 }
101 
102 /*
103  * The unpin operation is the last place an RUI is manipulated in the log. It is
104  * either inserted in the AIL or aborted in the event of a log I/O error. In
105  * either case, the RUI transaction has been successfully committed to make it
106  * this far. Therefore, we expect whoever committed the RUI to either construct
107  * and commit the RUD or drop the RUD's reference in the event of error. Simply
108  * drop the log's RUI reference now that the log is done with it.
109  */
110 STATIC void
111 xfs_rui_item_unpin(
112 	struct xfs_log_item	*lip,
113 	int			remove)
114 {
115 	struct xfs_rui_log_item	*ruip = RUI_ITEM(lip);
116 
117 	xfs_rui_release(ruip);
118 }
119 
120 /*
121  * The RUI has been either committed or aborted if the transaction has been
122  * cancelled. If the transaction was cancelled, an RUD isn't going to be
123  * constructed and thus we free the RUI here directly.
124  */
125 STATIC void
126 xfs_rui_item_release(
127 	struct xfs_log_item	*lip)
128 {
129 	xfs_rui_release(RUI_ITEM(lip));
130 }
131 
132 /*
133  * Allocate and initialize an rui item with the given number of extents.
134  */
135 STATIC struct xfs_rui_log_item *
136 xfs_rui_init(
137 	struct xfs_mount		*mp,
138 	uint				nextents)
139 
140 {
141 	struct xfs_rui_log_item		*ruip;
142 
143 	ASSERT(nextents > 0);
144 	if (nextents > XFS_RUI_MAX_FAST_EXTENTS)
145 		ruip = kmem_zalloc(xfs_rui_log_item_sizeof(nextents), 0);
146 	else
147 		ruip = kmem_cache_zalloc(xfs_rui_cache,
148 					 GFP_KERNEL | __GFP_NOFAIL);
149 
150 	xfs_log_item_init(mp, &ruip->rui_item, XFS_LI_RUI, &xfs_rui_item_ops);
151 	ruip->rui_format.rui_nextents = nextents;
152 	ruip->rui_format.rui_id = (uintptr_t)(void *)ruip;
153 	atomic_set(&ruip->rui_next_extent, 0);
154 	atomic_set(&ruip->rui_refcount, 2);
155 
156 	return ruip;
157 }
158 
159 static inline struct xfs_rud_log_item *RUD_ITEM(struct xfs_log_item *lip)
160 {
161 	return container_of(lip, struct xfs_rud_log_item, rud_item);
162 }
163 
164 STATIC void
165 xfs_rud_item_size(
166 	struct xfs_log_item	*lip,
167 	int			*nvecs,
168 	int			*nbytes)
169 {
170 	*nvecs += 1;
171 	*nbytes += sizeof(struct xfs_rud_log_format);
172 }
173 
174 /*
175  * This is called to fill in the vector of log iovecs for the
176  * given rud log item. We use only 1 iovec, and we point that
177  * at the rud_log_format structure embedded in the rud item.
178  * It is at this point that we assert that all of the extent
179  * slots in the rud item have been filled.
180  */
181 STATIC void
182 xfs_rud_item_format(
183 	struct xfs_log_item	*lip,
184 	struct xfs_log_vec	*lv)
185 {
186 	struct xfs_rud_log_item	*rudp = RUD_ITEM(lip);
187 	struct xfs_log_iovec	*vecp = NULL;
188 
189 	rudp->rud_format.rud_type = XFS_LI_RUD;
190 	rudp->rud_format.rud_size = 1;
191 
192 	xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUD_FORMAT, &rudp->rud_format,
193 			sizeof(struct xfs_rud_log_format));
194 }
195 
196 /*
197  * The RUD is either committed or aborted if the transaction is cancelled. If
198  * the transaction is cancelled, drop our reference to the RUI and free the
199  * RUD.
200  */
201 STATIC void
202 xfs_rud_item_release(
203 	struct xfs_log_item	*lip)
204 {
205 	struct xfs_rud_log_item	*rudp = RUD_ITEM(lip);
206 
207 	xfs_rui_release(rudp->rud_ruip);
208 	kmem_free(rudp->rud_item.li_lv_shadow);
209 	kmem_cache_free(xfs_rud_cache, rudp);
210 }
211 
212 static struct xfs_log_item *
213 xfs_rud_item_intent(
214 	struct xfs_log_item	*lip)
215 {
216 	return &RUD_ITEM(lip)->rud_ruip->rui_item;
217 }
218 
219 static const struct xfs_item_ops xfs_rud_item_ops = {
220 	.flags		= XFS_ITEM_RELEASE_WHEN_COMMITTED |
221 			  XFS_ITEM_INTENT_DONE,
222 	.iop_size	= xfs_rud_item_size,
223 	.iop_format	= xfs_rud_item_format,
224 	.iop_release	= xfs_rud_item_release,
225 	.iop_intent	= xfs_rud_item_intent,
226 };
227 
228 /* Set the map extent flags for this reverse mapping. */
229 static void
230 xfs_trans_set_rmap_flags(
231 	struct xfs_map_extent		*map,
232 	enum xfs_rmap_intent_type	type,
233 	int				whichfork,
234 	xfs_exntst_t			state)
235 {
236 	map->me_flags = 0;
237 	if (state == XFS_EXT_UNWRITTEN)
238 		map->me_flags |= XFS_RMAP_EXTENT_UNWRITTEN;
239 	if (whichfork == XFS_ATTR_FORK)
240 		map->me_flags |= XFS_RMAP_EXTENT_ATTR_FORK;
241 	switch (type) {
242 	case XFS_RMAP_MAP:
243 		map->me_flags |= XFS_RMAP_EXTENT_MAP;
244 		break;
245 	case XFS_RMAP_MAP_SHARED:
246 		map->me_flags |= XFS_RMAP_EXTENT_MAP_SHARED;
247 		break;
248 	case XFS_RMAP_UNMAP:
249 		map->me_flags |= XFS_RMAP_EXTENT_UNMAP;
250 		break;
251 	case XFS_RMAP_UNMAP_SHARED:
252 		map->me_flags |= XFS_RMAP_EXTENT_UNMAP_SHARED;
253 		break;
254 	case XFS_RMAP_CONVERT:
255 		map->me_flags |= XFS_RMAP_EXTENT_CONVERT;
256 		break;
257 	case XFS_RMAP_CONVERT_SHARED:
258 		map->me_flags |= XFS_RMAP_EXTENT_CONVERT_SHARED;
259 		break;
260 	case XFS_RMAP_ALLOC:
261 		map->me_flags |= XFS_RMAP_EXTENT_ALLOC;
262 		break;
263 	case XFS_RMAP_FREE:
264 		map->me_flags |= XFS_RMAP_EXTENT_FREE;
265 		break;
266 	default:
267 		ASSERT(0);
268 	}
269 }
270 
271 /* Sort rmap intents by AG. */
272 static int
273 xfs_rmap_update_diff_items(
274 	void				*priv,
275 	const struct list_head		*a,
276 	const struct list_head		*b)
277 {
278 	struct xfs_rmap_intent		*ra;
279 	struct xfs_rmap_intent		*rb;
280 
281 	ra = container_of(a, struct xfs_rmap_intent, ri_list);
282 	rb = container_of(b, struct xfs_rmap_intent, ri_list);
283 
284 	return ra->ri_pag->pag_agno - rb->ri_pag->pag_agno;
285 }
286 
287 /* Log rmap updates in the intent item. */
288 STATIC void
289 xfs_rmap_update_log_item(
290 	struct xfs_trans		*tp,
291 	struct xfs_rui_log_item		*ruip,
292 	struct xfs_rmap_intent		*ri)
293 {
294 	uint				next_extent;
295 	struct xfs_map_extent		*map;
296 
297 	/*
298 	 * atomic_inc_return gives us the value after the increment;
299 	 * we want to use it as an array index so we need to subtract 1 from
300 	 * it.
301 	 */
302 	next_extent = atomic_inc_return(&ruip->rui_next_extent) - 1;
303 	ASSERT(next_extent < ruip->rui_format.rui_nextents);
304 	map = &ruip->rui_format.rui_extents[next_extent];
305 	map->me_owner = ri->ri_owner;
306 	map->me_startblock = ri->ri_bmap.br_startblock;
307 	map->me_startoff = ri->ri_bmap.br_startoff;
308 	map->me_len = ri->ri_bmap.br_blockcount;
309 	xfs_trans_set_rmap_flags(map, ri->ri_type, ri->ri_whichfork,
310 			ri->ri_bmap.br_state);
311 }
312 
313 static struct xfs_log_item *
314 xfs_rmap_update_create_intent(
315 	struct xfs_trans		*tp,
316 	struct list_head		*items,
317 	unsigned int			count,
318 	bool				sort)
319 {
320 	struct xfs_mount		*mp = tp->t_mountp;
321 	struct xfs_rui_log_item		*ruip = xfs_rui_init(mp, count);
322 	struct xfs_rmap_intent		*ri;
323 
324 	ASSERT(count > 0);
325 
326 	if (sort)
327 		list_sort(mp, items, xfs_rmap_update_diff_items);
328 	list_for_each_entry(ri, items, ri_list)
329 		xfs_rmap_update_log_item(tp, ruip, ri);
330 	return &ruip->rui_item;
331 }
332 
333 /* Get an RUD so we can process all the deferred rmap updates. */
334 static struct xfs_log_item *
335 xfs_rmap_update_create_done(
336 	struct xfs_trans		*tp,
337 	struct xfs_log_item		*intent,
338 	unsigned int			count)
339 {
340 	struct xfs_rui_log_item		*ruip = RUI_ITEM(intent);
341 	struct xfs_rud_log_item		*rudp;
342 
343 	rudp = kmem_cache_zalloc(xfs_rud_cache, GFP_KERNEL | __GFP_NOFAIL);
344 	xfs_log_item_init(tp->t_mountp, &rudp->rud_item, XFS_LI_RUD,
345 			  &xfs_rud_item_ops);
346 	rudp->rud_ruip = ruip;
347 	rudp->rud_format.rud_rui_id = ruip->rui_format.rui_id;
348 
349 	return &rudp->rud_item;
350 }
351 
352 /* Take a passive ref to the AG containing the space we're rmapping. */
353 void
354 xfs_rmap_update_get_group(
355 	struct xfs_mount	*mp,
356 	struct xfs_rmap_intent	*ri)
357 {
358 	xfs_agnumber_t		agno;
359 
360 	agno = XFS_FSB_TO_AGNO(mp, ri->ri_bmap.br_startblock);
361 	ri->ri_pag = xfs_perag_intent_get(mp, agno);
362 }
363 
364 /* Release a passive AG ref after finishing rmapping work. */
365 static inline void
366 xfs_rmap_update_put_group(
367 	struct xfs_rmap_intent	*ri)
368 {
369 	xfs_perag_intent_put(ri->ri_pag);
370 }
371 
372 /* Process a deferred rmap update. */
373 STATIC int
374 xfs_rmap_update_finish_item(
375 	struct xfs_trans		*tp,
376 	struct xfs_log_item		*done,
377 	struct list_head		*item,
378 	struct xfs_btree_cur		**state)
379 {
380 	struct xfs_rmap_intent		*ri;
381 	int				error;
382 
383 	ri = container_of(item, struct xfs_rmap_intent, ri_list);
384 
385 	error = xfs_rmap_finish_one(tp, ri, state);
386 
387 	xfs_rmap_update_put_group(ri);
388 	kmem_cache_free(xfs_rmap_intent_cache, ri);
389 	return error;
390 }
391 
392 /* Abort all pending RUIs. */
393 STATIC void
394 xfs_rmap_update_abort_intent(
395 	struct xfs_log_item	*intent)
396 {
397 	xfs_rui_release(RUI_ITEM(intent));
398 }
399 
400 /* Cancel a deferred rmap update. */
401 STATIC void
402 xfs_rmap_update_cancel_item(
403 	struct list_head		*item)
404 {
405 	struct xfs_rmap_intent		*ri;
406 
407 	ri = container_of(item, struct xfs_rmap_intent, ri_list);
408 
409 	xfs_rmap_update_put_group(ri);
410 	kmem_cache_free(xfs_rmap_intent_cache, ri);
411 }
412 
413 /* Is this recovered RUI ok? */
414 static inline bool
415 xfs_rui_validate_map(
416 	struct xfs_mount		*mp,
417 	struct xfs_map_extent		*map)
418 {
419 	if (!xfs_has_rmapbt(mp))
420 		return false;
421 
422 	if (map->me_flags & ~XFS_RMAP_EXTENT_FLAGS)
423 		return false;
424 
425 	switch (map->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
426 	case XFS_RMAP_EXTENT_MAP:
427 	case XFS_RMAP_EXTENT_MAP_SHARED:
428 	case XFS_RMAP_EXTENT_UNMAP:
429 	case XFS_RMAP_EXTENT_UNMAP_SHARED:
430 	case XFS_RMAP_EXTENT_CONVERT:
431 	case XFS_RMAP_EXTENT_CONVERT_SHARED:
432 	case XFS_RMAP_EXTENT_ALLOC:
433 	case XFS_RMAP_EXTENT_FREE:
434 		break;
435 	default:
436 		return false;
437 	}
438 
439 	if (!XFS_RMAP_NON_INODE_OWNER(map->me_owner) &&
440 	    !xfs_verify_ino(mp, map->me_owner))
441 		return false;
442 
443 	if (!xfs_verify_fileext(mp, map->me_startoff, map->me_len))
444 		return false;
445 
446 	return xfs_verify_fsbext(mp, map->me_startblock, map->me_len);
447 }
448 
449 static inline void
450 xfs_rui_recover_work(
451 	struct xfs_mount		*mp,
452 	struct xfs_defer_pending	*dfp,
453 	const struct xfs_map_extent	*map)
454 {
455 	struct xfs_rmap_intent		*ri;
456 
457 	ri = kmem_cache_alloc(xfs_rmap_intent_cache, GFP_NOFS | __GFP_NOFAIL);
458 
459 	switch (map->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
460 	case XFS_RMAP_EXTENT_MAP:
461 		ri->ri_type = XFS_RMAP_MAP;
462 		break;
463 	case XFS_RMAP_EXTENT_MAP_SHARED:
464 		ri->ri_type = XFS_RMAP_MAP_SHARED;
465 		break;
466 	case XFS_RMAP_EXTENT_UNMAP:
467 		ri->ri_type = XFS_RMAP_UNMAP;
468 		break;
469 	case XFS_RMAP_EXTENT_UNMAP_SHARED:
470 		ri->ri_type = XFS_RMAP_UNMAP_SHARED;
471 		break;
472 	case XFS_RMAP_EXTENT_CONVERT:
473 		ri->ri_type = XFS_RMAP_CONVERT;
474 		break;
475 	case XFS_RMAP_EXTENT_CONVERT_SHARED:
476 		ri->ri_type = XFS_RMAP_CONVERT_SHARED;
477 		break;
478 	case XFS_RMAP_EXTENT_ALLOC:
479 		ri->ri_type = XFS_RMAP_ALLOC;
480 		break;
481 	case XFS_RMAP_EXTENT_FREE:
482 		ri->ri_type = XFS_RMAP_FREE;
483 		break;
484 	default:
485 		ASSERT(0);
486 		return;
487 	}
488 
489 	ri->ri_owner = map->me_owner;
490 	ri->ri_whichfork = (map->me_flags & XFS_RMAP_EXTENT_ATTR_FORK) ?
491 			XFS_ATTR_FORK : XFS_DATA_FORK;
492 	ri->ri_bmap.br_startblock = map->me_startblock;
493 	ri->ri_bmap.br_startoff = map->me_startoff;
494 	ri->ri_bmap.br_blockcount = map->me_len;
495 	ri->ri_bmap.br_state = (map->me_flags & XFS_RMAP_EXTENT_UNWRITTEN) ?
496 			XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
497 	xfs_rmap_update_get_group(mp, ri);
498 
499 	xfs_defer_add_item(dfp, &ri->ri_list);
500 }
501 
502 /*
503  * Process an rmap update intent item that was recovered from the log.
504  * We need to update the rmapbt.
505  */
506 STATIC int
507 xfs_rmap_recover_work(
508 	struct xfs_defer_pending	*dfp,
509 	struct list_head		*capture_list)
510 {
511 	struct xfs_trans_res		resv;
512 	struct xfs_log_item		*lip = dfp->dfp_intent;
513 	struct xfs_rui_log_item		*ruip = RUI_ITEM(lip);
514 	struct xfs_trans		*tp;
515 	struct xfs_mount		*mp = lip->li_log->l_mp;
516 	int				i;
517 	int				error = 0;
518 
519 	/*
520 	 * First check the validity of the extents described by the
521 	 * RUI.  If any are bad, then assume that all are bad and
522 	 * just toss the RUI.
523 	 */
524 	for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
525 		if (!xfs_rui_validate_map(mp,
526 					&ruip->rui_format.rui_extents[i])) {
527 			XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
528 					&ruip->rui_format,
529 					sizeof(ruip->rui_format));
530 			return -EFSCORRUPTED;
531 		}
532 
533 		xfs_rui_recover_work(mp, dfp, &ruip->rui_format.rui_extents[i]);
534 	}
535 
536 	resv = xlog_recover_resv(&M_RES(mp)->tr_itruncate);
537 	error = xfs_trans_alloc(mp, &resv, mp->m_rmap_maxlevels, 0,
538 			XFS_TRANS_RESERVE, &tp);
539 	if (error)
540 		return error;
541 
542 	error = xlog_recover_finish_intent(tp, dfp);
543 	if (error == -EFSCORRUPTED)
544 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
545 				&ruip->rui_format,
546 				sizeof(ruip->rui_format));
547 	if (error)
548 		goto abort_error;
549 
550 	return xfs_defer_ops_capture_and_commit(tp, capture_list);
551 
552 abort_error:
553 	xfs_trans_cancel(tp);
554 	return error;
555 }
556 
557 /* Relog an intent item to push the log tail forward. */
558 static struct xfs_log_item *
559 xfs_rmap_relog_intent(
560 	struct xfs_trans		*tp,
561 	struct xfs_log_item		*intent,
562 	struct xfs_log_item		*done_item)
563 {
564 	struct xfs_rui_log_item		*ruip;
565 	struct xfs_map_extent		*map;
566 	unsigned int			count;
567 
568 	count = RUI_ITEM(intent)->rui_format.rui_nextents;
569 	map = RUI_ITEM(intent)->rui_format.rui_extents;
570 
571 	ruip = xfs_rui_init(tp->t_mountp, count);
572 	memcpy(ruip->rui_format.rui_extents, map, count * sizeof(*map));
573 	atomic_set(&ruip->rui_next_extent, count);
574 
575 	return &ruip->rui_item;
576 }
577 
578 const struct xfs_defer_op_type xfs_rmap_update_defer_type = {
579 	.name		= "rmap",
580 	.max_items	= XFS_RUI_MAX_FAST_EXTENTS,
581 	.create_intent	= xfs_rmap_update_create_intent,
582 	.abort_intent	= xfs_rmap_update_abort_intent,
583 	.create_done	= xfs_rmap_update_create_done,
584 	.finish_item	= xfs_rmap_update_finish_item,
585 	.finish_cleanup = xfs_rmap_finish_one_cleanup,
586 	.cancel_item	= xfs_rmap_update_cancel_item,
587 	.recover_work	= xfs_rmap_recover_work,
588 	.relog_intent	= xfs_rmap_relog_intent,
589 };
590 
591 STATIC bool
592 xfs_rui_item_match(
593 	struct xfs_log_item	*lip,
594 	uint64_t		intent_id)
595 {
596 	return RUI_ITEM(lip)->rui_format.rui_id == intent_id;
597 }
598 
599 static const struct xfs_item_ops xfs_rui_item_ops = {
600 	.flags		= XFS_ITEM_INTENT,
601 	.iop_size	= xfs_rui_item_size,
602 	.iop_format	= xfs_rui_item_format,
603 	.iop_unpin	= xfs_rui_item_unpin,
604 	.iop_release	= xfs_rui_item_release,
605 	.iop_match	= xfs_rui_item_match,
606 };
607 
608 static inline void
609 xfs_rui_copy_format(
610 	struct xfs_rui_log_format	*dst,
611 	const struct xfs_rui_log_format	*src)
612 {
613 	unsigned int			i;
614 
615 	memcpy(dst, src, offsetof(struct xfs_rui_log_format, rui_extents));
616 
617 	for (i = 0; i < src->rui_nextents; i++)
618 		memcpy(&dst->rui_extents[i], &src->rui_extents[i],
619 				sizeof(struct xfs_map_extent));
620 }
621 
622 /*
623  * This routine is called to create an in-core extent rmap update
624  * item from the rui format structure which was logged on disk.
625  * It allocates an in-core rui, copies the extents from the format
626  * structure into it, and adds the rui to the AIL with the given
627  * LSN.
628  */
629 STATIC int
630 xlog_recover_rui_commit_pass2(
631 	struct xlog			*log,
632 	struct list_head		*buffer_list,
633 	struct xlog_recover_item	*item,
634 	xfs_lsn_t			lsn)
635 {
636 	struct xfs_mount		*mp = log->l_mp;
637 	struct xfs_rui_log_item		*ruip;
638 	struct xfs_rui_log_format	*rui_formatp;
639 	size_t				len;
640 
641 	rui_formatp = item->ri_buf[0].i_addr;
642 
643 	if (item->ri_buf[0].i_len < xfs_rui_log_format_sizeof(0)) {
644 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
645 				item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
646 		return -EFSCORRUPTED;
647 	}
648 
649 	len = xfs_rui_log_format_sizeof(rui_formatp->rui_nextents);
650 	if (item->ri_buf[0].i_len != len) {
651 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
652 				item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
653 		return -EFSCORRUPTED;
654 	}
655 
656 	ruip = xfs_rui_init(mp, rui_formatp->rui_nextents);
657 	xfs_rui_copy_format(&ruip->rui_format, rui_formatp);
658 	atomic_set(&ruip->rui_next_extent, rui_formatp->rui_nextents);
659 
660 	xlog_recover_intent_item(log, &ruip->rui_item, lsn,
661 			&xfs_rmap_update_defer_type);
662 	return 0;
663 }
664 
665 const struct xlog_recover_item_ops xlog_rui_item_ops = {
666 	.item_type		= XFS_LI_RUI,
667 	.commit_pass2		= xlog_recover_rui_commit_pass2,
668 };
669 
670 /*
671  * This routine is called when an RUD format structure is found in a committed
672  * transaction in the log. Its purpose is to cancel the corresponding RUI if it
673  * was still in the log. To do this it searches the AIL for the RUI with an id
674  * equal to that in the RUD format structure. If we find it we drop the RUD
675  * reference, which removes the RUI from the AIL and frees it.
676  */
677 STATIC int
678 xlog_recover_rud_commit_pass2(
679 	struct xlog			*log,
680 	struct list_head		*buffer_list,
681 	struct xlog_recover_item	*item,
682 	xfs_lsn_t			lsn)
683 {
684 	struct xfs_rud_log_format	*rud_formatp;
685 
686 	rud_formatp = item->ri_buf[0].i_addr;
687 	if (item->ri_buf[0].i_len != sizeof(struct xfs_rud_log_format)) {
688 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
689 				rud_formatp, item->ri_buf[0].i_len);
690 		return -EFSCORRUPTED;
691 	}
692 
693 	xlog_recover_release_intent(log, XFS_LI_RUI, rud_formatp->rud_rui_id);
694 	return 0;
695 }
696 
697 const struct xlog_recover_item_ops xlog_rud_item_ops = {
698 	.item_type		= XFS_LI_RUD,
699 	.commit_pass2		= xlog_recover_rud_commit_pass2,
700 };
701