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