xref: /openbmc/linux/fs/xfs/xfs_rmap_item.c (revision 1edd0337)
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		*map,
248 	enum xfs_rmap_intent_type	type,
249 	int				whichfork,
250 	xfs_exntst_t			state)
251 {
252 	map->me_flags = 0;
253 	if (state == XFS_EXT_UNWRITTEN)
254 		map->me_flags |= XFS_RMAP_EXTENT_UNWRITTEN;
255 	if (whichfork == XFS_ATTR_FORK)
256 		map->me_flags |= XFS_RMAP_EXTENT_ATTR_FORK;
257 	switch (type) {
258 	case XFS_RMAP_MAP:
259 		map->me_flags |= XFS_RMAP_EXTENT_MAP;
260 		break;
261 	case XFS_RMAP_MAP_SHARED:
262 		map->me_flags |= XFS_RMAP_EXTENT_MAP_SHARED;
263 		break;
264 	case XFS_RMAP_UNMAP:
265 		map->me_flags |= XFS_RMAP_EXTENT_UNMAP;
266 		break;
267 	case XFS_RMAP_UNMAP_SHARED:
268 		map->me_flags |= XFS_RMAP_EXTENT_UNMAP_SHARED;
269 		break;
270 	case XFS_RMAP_CONVERT:
271 		map->me_flags |= XFS_RMAP_EXTENT_CONVERT;
272 		break;
273 	case XFS_RMAP_CONVERT_SHARED:
274 		map->me_flags |= XFS_RMAP_EXTENT_CONVERT_SHARED;
275 		break;
276 	case XFS_RMAP_ALLOC:
277 		map->me_flags |= XFS_RMAP_EXTENT_ALLOC;
278 		break;
279 	case XFS_RMAP_FREE:
280 		map->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 	struct xfs_rmap_intent		*ri,
297 	struct xfs_btree_cur		**pcur)
298 {
299 	int				error;
300 
301 	error = xfs_rmap_finish_one(tp, ri, pcur);
302 
303 	/*
304 	 * Mark the transaction dirty, even on error. This ensures the
305 	 * transaction is aborted, which:
306 	 *
307 	 * 1.) releases the RUI and frees the RUD
308 	 * 2.) shuts down the filesystem
309 	 */
310 	tp->t_flags |= XFS_TRANS_DIRTY | XFS_TRANS_HAS_INTENT_DONE;
311 	set_bit(XFS_LI_DIRTY, &rudp->rud_item.li_flags);
312 
313 	return error;
314 }
315 
316 /* Sort rmap intents by AG. */
317 static int
318 xfs_rmap_update_diff_items(
319 	void				*priv,
320 	const struct list_head		*a,
321 	const struct list_head		*b)
322 {
323 	struct xfs_mount		*mp = priv;
324 	struct xfs_rmap_intent		*ra;
325 	struct xfs_rmap_intent		*rb;
326 
327 	ra = container_of(a, struct xfs_rmap_intent, ri_list);
328 	rb = container_of(b, struct xfs_rmap_intent, ri_list);
329 	return  XFS_FSB_TO_AGNO(mp, ra->ri_bmap.br_startblock) -
330 		XFS_FSB_TO_AGNO(mp, rb->ri_bmap.br_startblock);
331 }
332 
333 /* Log rmap updates in the intent item. */
334 STATIC void
335 xfs_rmap_update_log_item(
336 	struct xfs_trans		*tp,
337 	struct xfs_rui_log_item		*ruip,
338 	struct xfs_rmap_intent		*ri)
339 {
340 	uint				next_extent;
341 	struct xfs_map_extent		*map;
342 
343 	tp->t_flags |= XFS_TRANS_DIRTY;
344 	set_bit(XFS_LI_DIRTY, &ruip->rui_item.li_flags);
345 
346 	/*
347 	 * atomic_inc_return gives us the value after the increment;
348 	 * we want to use it as an array index so we need to subtract 1 from
349 	 * it.
350 	 */
351 	next_extent = atomic_inc_return(&ruip->rui_next_extent) - 1;
352 	ASSERT(next_extent < ruip->rui_format.rui_nextents);
353 	map = &ruip->rui_format.rui_extents[next_extent];
354 	map->me_owner = ri->ri_owner;
355 	map->me_startblock = ri->ri_bmap.br_startblock;
356 	map->me_startoff = ri->ri_bmap.br_startoff;
357 	map->me_len = ri->ri_bmap.br_blockcount;
358 	xfs_trans_set_rmap_flags(map, ri->ri_type, ri->ri_whichfork,
359 			ri->ri_bmap.br_state);
360 }
361 
362 static struct xfs_log_item *
363 xfs_rmap_update_create_intent(
364 	struct xfs_trans		*tp,
365 	struct list_head		*items,
366 	unsigned int			count,
367 	bool				sort)
368 {
369 	struct xfs_mount		*mp = tp->t_mountp;
370 	struct xfs_rui_log_item		*ruip = xfs_rui_init(mp, count);
371 	struct xfs_rmap_intent		*ri;
372 
373 	ASSERT(count > 0);
374 
375 	xfs_trans_add_item(tp, &ruip->rui_item);
376 	if (sort)
377 		list_sort(mp, items, xfs_rmap_update_diff_items);
378 	list_for_each_entry(ri, items, ri_list)
379 		xfs_rmap_update_log_item(tp, ruip, ri);
380 	return &ruip->rui_item;
381 }
382 
383 /* Get an RUD so we can process all the deferred rmap updates. */
384 static struct xfs_log_item *
385 xfs_rmap_update_create_done(
386 	struct xfs_trans		*tp,
387 	struct xfs_log_item		*intent,
388 	unsigned int			count)
389 {
390 	return &xfs_trans_get_rud(tp, RUI_ITEM(intent))->rud_item;
391 }
392 
393 /* Process a deferred rmap update. */
394 STATIC int
395 xfs_rmap_update_finish_item(
396 	struct xfs_trans		*tp,
397 	struct xfs_log_item		*done,
398 	struct list_head		*item,
399 	struct xfs_btree_cur		**state)
400 {
401 	struct xfs_rmap_intent		*ri;
402 	int				error;
403 
404 	ri = container_of(item, struct xfs_rmap_intent, ri_list);
405 
406 	error = xfs_trans_log_finish_rmap_update(tp, RUD_ITEM(done), ri,
407 			state);
408 	kmem_cache_free(xfs_rmap_intent_cache, ri);
409 	return error;
410 }
411 
412 /* Abort all pending RUIs. */
413 STATIC void
414 xfs_rmap_update_abort_intent(
415 	struct xfs_log_item	*intent)
416 {
417 	xfs_rui_release(RUI_ITEM(intent));
418 }
419 
420 /* Cancel a deferred rmap update. */
421 STATIC void
422 xfs_rmap_update_cancel_item(
423 	struct list_head		*item)
424 {
425 	struct xfs_rmap_intent		*ri;
426 
427 	ri = container_of(item, struct xfs_rmap_intent, ri_list);
428 	kmem_cache_free(xfs_rmap_intent_cache, ri);
429 }
430 
431 const struct xfs_defer_op_type xfs_rmap_update_defer_type = {
432 	.max_items	= XFS_RUI_MAX_FAST_EXTENTS,
433 	.create_intent	= xfs_rmap_update_create_intent,
434 	.abort_intent	= xfs_rmap_update_abort_intent,
435 	.create_done	= xfs_rmap_update_create_done,
436 	.finish_item	= xfs_rmap_update_finish_item,
437 	.finish_cleanup = xfs_rmap_finish_one_cleanup,
438 	.cancel_item	= xfs_rmap_update_cancel_item,
439 };
440 
441 /* Is this recovered RUI ok? */
442 static inline bool
443 xfs_rui_validate_map(
444 	struct xfs_mount		*mp,
445 	struct xfs_map_extent		*map)
446 {
447 	if (!xfs_has_rmapbt(mp))
448 		return false;
449 
450 	if (map->me_flags & ~XFS_RMAP_EXTENT_FLAGS)
451 		return false;
452 
453 	switch (map->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
454 	case XFS_RMAP_EXTENT_MAP:
455 	case XFS_RMAP_EXTENT_MAP_SHARED:
456 	case XFS_RMAP_EXTENT_UNMAP:
457 	case XFS_RMAP_EXTENT_UNMAP_SHARED:
458 	case XFS_RMAP_EXTENT_CONVERT:
459 	case XFS_RMAP_EXTENT_CONVERT_SHARED:
460 	case XFS_RMAP_EXTENT_ALLOC:
461 	case XFS_RMAP_EXTENT_FREE:
462 		break;
463 	default:
464 		return false;
465 	}
466 
467 	if (!XFS_RMAP_NON_INODE_OWNER(map->me_owner) &&
468 	    !xfs_verify_ino(mp, map->me_owner))
469 		return false;
470 
471 	if (!xfs_verify_fileext(mp, map->me_startoff, map->me_len))
472 		return false;
473 
474 	return xfs_verify_fsbext(mp, map->me_startblock, map->me_len);
475 }
476 
477 /*
478  * Process an rmap update intent item that was recovered from the log.
479  * We need to update the rmapbt.
480  */
481 STATIC int
482 xfs_rui_item_recover(
483 	struct xfs_log_item		*lip,
484 	struct list_head		*capture_list)
485 {
486 	struct xfs_rui_log_item		*ruip = RUI_ITEM(lip);
487 	struct xfs_rud_log_item		*rudp;
488 	struct xfs_trans		*tp;
489 	struct xfs_btree_cur		*rcur = NULL;
490 	struct xfs_mount		*mp = lip->li_log->l_mp;
491 	int				i;
492 	int				error = 0;
493 
494 	/*
495 	 * First check the validity of the extents described by the
496 	 * RUI.  If any are bad, then assume that all are bad and
497 	 * just toss the RUI.
498 	 */
499 	for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
500 		if (!xfs_rui_validate_map(mp,
501 					&ruip->rui_format.rui_extents[i])) {
502 			XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
503 					&ruip->rui_format,
504 					sizeof(ruip->rui_format));
505 			return -EFSCORRUPTED;
506 		}
507 	}
508 
509 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate,
510 			mp->m_rmap_maxlevels, 0, XFS_TRANS_RESERVE, &tp);
511 	if (error)
512 		return error;
513 	rudp = xfs_trans_get_rud(tp, ruip);
514 
515 	for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
516 		struct xfs_rmap_intent	fake = { };
517 		struct xfs_map_extent	*map;
518 
519 		map = &ruip->rui_format.rui_extents[i];
520 		switch (map->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
521 		case XFS_RMAP_EXTENT_MAP:
522 			fake.ri_type = XFS_RMAP_MAP;
523 			break;
524 		case XFS_RMAP_EXTENT_MAP_SHARED:
525 			fake.ri_type = XFS_RMAP_MAP_SHARED;
526 			break;
527 		case XFS_RMAP_EXTENT_UNMAP:
528 			fake.ri_type = XFS_RMAP_UNMAP;
529 			break;
530 		case XFS_RMAP_EXTENT_UNMAP_SHARED:
531 			fake.ri_type = XFS_RMAP_UNMAP_SHARED;
532 			break;
533 		case XFS_RMAP_EXTENT_CONVERT:
534 			fake.ri_type = XFS_RMAP_CONVERT;
535 			break;
536 		case XFS_RMAP_EXTENT_CONVERT_SHARED:
537 			fake.ri_type = XFS_RMAP_CONVERT_SHARED;
538 			break;
539 		case XFS_RMAP_EXTENT_ALLOC:
540 			fake.ri_type = XFS_RMAP_ALLOC;
541 			break;
542 		case XFS_RMAP_EXTENT_FREE:
543 			fake.ri_type = XFS_RMAP_FREE;
544 			break;
545 		default:
546 			XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
547 					&ruip->rui_format,
548 					sizeof(ruip->rui_format));
549 			error = -EFSCORRUPTED;
550 			goto abort_error;
551 		}
552 
553 		fake.ri_owner = map->me_owner;
554 		fake.ri_whichfork = (map->me_flags & XFS_RMAP_EXTENT_ATTR_FORK) ?
555 				XFS_ATTR_FORK : XFS_DATA_FORK;
556 		fake.ri_bmap.br_startblock = map->me_startblock;
557 		fake.ri_bmap.br_startoff = map->me_startoff;
558 		fake.ri_bmap.br_blockcount = map->me_len;
559 		fake.ri_bmap.br_state = (map->me_flags & XFS_RMAP_EXTENT_UNWRITTEN) ?
560 				XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
561 
562 		error = xfs_trans_log_finish_rmap_update(tp, rudp, &fake,
563 				&rcur);
564 		if (error == -EFSCORRUPTED)
565 			XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
566 					map, sizeof(*map));
567 		if (error)
568 			goto abort_error;
569 
570 	}
571 
572 	xfs_rmap_finish_one_cleanup(tp, rcur, error);
573 	return xfs_defer_ops_capture_and_commit(tp, capture_list);
574 
575 abort_error:
576 	xfs_rmap_finish_one_cleanup(tp, rcur, error);
577 	xfs_trans_cancel(tp);
578 	return error;
579 }
580 
581 STATIC bool
582 xfs_rui_item_match(
583 	struct xfs_log_item	*lip,
584 	uint64_t		intent_id)
585 {
586 	return RUI_ITEM(lip)->rui_format.rui_id == intent_id;
587 }
588 
589 /* Relog an intent item to push the log tail forward. */
590 static struct xfs_log_item *
591 xfs_rui_item_relog(
592 	struct xfs_log_item		*intent,
593 	struct xfs_trans		*tp)
594 {
595 	struct xfs_rud_log_item		*rudp;
596 	struct xfs_rui_log_item		*ruip;
597 	struct xfs_map_extent		*map;
598 	unsigned int			count;
599 
600 	count = RUI_ITEM(intent)->rui_format.rui_nextents;
601 	map = RUI_ITEM(intent)->rui_format.rui_extents;
602 
603 	tp->t_flags |= XFS_TRANS_DIRTY;
604 	rudp = xfs_trans_get_rud(tp, RUI_ITEM(intent));
605 	set_bit(XFS_LI_DIRTY, &rudp->rud_item.li_flags);
606 
607 	ruip = xfs_rui_init(tp->t_mountp, count);
608 	memcpy(ruip->rui_format.rui_extents, map, count * sizeof(*map));
609 	atomic_set(&ruip->rui_next_extent, count);
610 	xfs_trans_add_item(tp, &ruip->rui_item);
611 	set_bit(XFS_LI_DIRTY, &ruip->rui_item.li_flags);
612 	return &ruip->rui_item;
613 }
614 
615 static const struct xfs_item_ops xfs_rui_item_ops = {
616 	.flags		= XFS_ITEM_INTENT,
617 	.iop_size	= xfs_rui_item_size,
618 	.iop_format	= xfs_rui_item_format,
619 	.iop_unpin	= xfs_rui_item_unpin,
620 	.iop_release	= xfs_rui_item_release,
621 	.iop_recover	= xfs_rui_item_recover,
622 	.iop_match	= xfs_rui_item_match,
623 	.iop_relog	= xfs_rui_item_relog,
624 };
625 
626 static inline void
627 xfs_rui_copy_format(
628 	struct xfs_rui_log_format	*dst,
629 	const struct xfs_rui_log_format	*src)
630 {
631 	unsigned int			i;
632 
633 	memcpy(dst, src, offsetof(struct xfs_rui_log_format, rui_extents));
634 
635 	for (i = 0; i < src->rui_nextents; i++)
636 		memcpy(&dst->rui_extents[i], &src->rui_extents[i],
637 				sizeof(struct xfs_map_extent));
638 }
639 
640 /*
641  * This routine is called to create an in-core extent rmap update
642  * item from the rui format structure which was logged on disk.
643  * It allocates an in-core rui, copies the extents from the format
644  * structure into it, and adds the rui to the AIL with the given
645  * LSN.
646  */
647 STATIC int
648 xlog_recover_rui_commit_pass2(
649 	struct xlog			*log,
650 	struct list_head		*buffer_list,
651 	struct xlog_recover_item	*item,
652 	xfs_lsn_t			lsn)
653 {
654 	struct xfs_mount		*mp = log->l_mp;
655 	struct xfs_rui_log_item		*ruip;
656 	struct xfs_rui_log_format	*rui_formatp;
657 	size_t				len;
658 
659 	rui_formatp = item->ri_buf[0].i_addr;
660 
661 	if (item->ri_buf[0].i_len < xfs_rui_log_format_sizeof(0)) {
662 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
663 				item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
664 		return -EFSCORRUPTED;
665 	}
666 
667 	len = xfs_rui_log_format_sizeof(rui_formatp->rui_nextents);
668 	if (item->ri_buf[0].i_len != len) {
669 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
670 				item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
671 		return -EFSCORRUPTED;
672 	}
673 
674 	ruip = xfs_rui_init(mp, rui_formatp->rui_nextents);
675 	xfs_rui_copy_format(&ruip->rui_format, rui_formatp);
676 	atomic_set(&ruip->rui_next_extent, rui_formatp->rui_nextents);
677 	/*
678 	 * Insert the intent into the AIL directly and drop one reference so
679 	 * that finishing or canceling the work will drop the other.
680 	 */
681 	xfs_trans_ail_insert(log->l_ailp, &ruip->rui_item, lsn);
682 	xfs_rui_release(ruip);
683 	return 0;
684 }
685 
686 const struct xlog_recover_item_ops xlog_rui_item_ops = {
687 	.item_type		= XFS_LI_RUI,
688 	.commit_pass2		= xlog_recover_rui_commit_pass2,
689 };
690 
691 /*
692  * This routine is called when an RUD format structure is found in a committed
693  * transaction in the log. Its purpose is to cancel the corresponding RUI if it
694  * was still in the log. To do this it searches the AIL for the RUI with an id
695  * equal to that in the RUD format structure. If we find it we drop the RUD
696  * reference, which removes the RUI from the AIL and frees it.
697  */
698 STATIC int
699 xlog_recover_rud_commit_pass2(
700 	struct xlog			*log,
701 	struct list_head		*buffer_list,
702 	struct xlog_recover_item	*item,
703 	xfs_lsn_t			lsn)
704 {
705 	struct xfs_rud_log_format	*rud_formatp;
706 
707 	rud_formatp = item->ri_buf[0].i_addr;
708 	if (item->ri_buf[0].i_len != sizeof(struct xfs_rud_log_format)) {
709 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
710 				rud_formatp, item->ri_buf[0].i_len);
711 		return -EFSCORRUPTED;
712 	}
713 
714 	xlog_recover_release_intent(log, XFS_LI_RUI, rud_formatp->rud_rui_id);
715 	return 0;
716 }
717 
718 const struct xlog_recover_item_ops xlog_rud_item_ops = {
719 	.item_type		= XFS_LI_RUD,
720 	.commit_pass2		= xlog_recover_rud_commit_pass2,
721 };
722