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