xref: /linux/fs/xfs/xfs_rmap_item.c (revision f0100363d8c374bd8e9ea7c9ba02744f0b802ca4)
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_platform.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 #include "xfs_btree.h"
25 #include "xfs_trace.h"
26 #include "xfs_rtgroup.h"
27 
28 struct kmem_cache	*xfs_rui_cache;
29 struct kmem_cache	*xfs_rud_cache;
30 
31 static const struct xfs_item_ops xfs_rui_item_ops;
32 
RUI_ITEM(struct xfs_log_item * lip)33 static inline struct xfs_rui_log_item *RUI_ITEM(struct xfs_log_item *lip)
34 {
35 	return container_of(lip, struct xfs_rui_log_item, rui_item);
36 }
37 
38 STATIC void
xfs_rui_item_free(struct xfs_rui_log_item * ruip)39 xfs_rui_item_free(
40 	struct xfs_rui_log_item	*ruip)
41 {
42 	kvfree(ruip->rui_item.li_lv_shadow);
43 	if (ruip->rui_format.rui_nextents > XFS_RUI_MAX_FAST_EXTENTS)
44 		kfree(ruip);
45 	else
46 		kmem_cache_free(xfs_rui_cache, ruip);
47 }
48 
49 /*
50  * Freeing the RUI requires that we remove it from the AIL if it has already
51  * been placed there. However, the RUI may not yet have been placed in the AIL
52  * when called by xfs_rui_release() from RUD processing due to the ordering of
53  * committed vs unpin operations in bulk insert operations. Hence the reference
54  * count to ensure only the last caller frees the RUI.
55  */
56 STATIC void
xfs_rui_release(struct xfs_rui_log_item * ruip)57 xfs_rui_release(
58 	struct xfs_rui_log_item	*ruip)
59 {
60 	ASSERT(atomic_read(&ruip->rui_refcount) > 0);
61 	if (!atomic_dec_and_test(&ruip->rui_refcount))
62 		return;
63 
64 	xfs_trans_ail_delete(&ruip->rui_item, 0);
65 	xfs_rui_item_free(ruip);
66 }
67 
68 STATIC void
xfs_rui_item_size(struct xfs_log_item * lip,int * nvecs,int * nbytes)69 xfs_rui_item_size(
70 	struct xfs_log_item	*lip,
71 	int			*nvecs,
72 	int			*nbytes)
73 {
74 	struct xfs_rui_log_item	*ruip = RUI_ITEM(lip);
75 
76 	*nvecs += 1;
77 	*nbytes += xfs_rui_log_format_sizeof(ruip->rui_format.rui_nextents);
78 }
79 
xfs_rui_log_space(unsigned int nr)80 unsigned int xfs_rui_log_space(unsigned int nr)
81 {
82 	return xlog_item_space(1, xfs_rui_log_format_sizeof(nr));
83 }
84 
85 /*
86  * This is called to fill in the vector of log iovecs for the
87  * given rui log item. We use only 1 iovec, and we point that
88  * at the rui_log_format structure embedded in the rui item.
89  * It is at this point that we assert that all of the extent
90  * slots in the rui item have been filled.
91  */
92 STATIC void
xfs_rui_item_format(struct xfs_log_item * lip,struct xlog_format_buf * lfb)93 xfs_rui_item_format(
94 	struct xfs_log_item	*lip,
95 	struct xlog_format_buf	*lfb)
96 {
97 	struct xfs_rui_log_item	*ruip = RUI_ITEM(lip);
98 
99 	ASSERT(atomic_read(&ruip->rui_next_extent) ==
100 			ruip->rui_format.rui_nextents);
101 
102 	ASSERT(lip->li_type == XFS_LI_RUI || lip->li_type == XFS_LI_RUI_RT);
103 
104 	ruip->rui_format.rui_type = lip->li_type;
105 	ruip->rui_format.rui_size = 1;
106 
107 	xlog_format_copy(lfb, XLOG_REG_TYPE_RUI_FORMAT, &ruip->rui_format,
108 			xfs_rui_log_format_sizeof(ruip->rui_format.rui_nextents));
109 }
110 
111 /*
112  * The unpin operation is the last place an RUI is manipulated in the log. It is
113  * either inserted in the AIL or aborted in the event of a log I/O error. In
114  * either case, the RUI transaction has been successfully committed to make it
115  * this far. Therefore, we expect whoever committed the RUI to either construct
116  * and commit the RUD or drop the RUD's reference in the event of error. Simply
117  * drop the log's RUI reference now that the log is done with it.
118  */
119 STATIC void
xfs_rui_item_unpin(struct xfs_log_item * lip,int remove)120 xfs_rui_item_unpin(
121 	struct xfs_log_item	*lip,
122 	int			remove)
123 {
124 	struct xfs_rui_log_item	*ruip = RUI_ITEM(lip);
125 
126 	xfs_rui_release(ruip);
127 }
128 
129 /*
130  * The RUI has been either committed or aborted if the transaction has been
131  * cancelled. If the transaction was cancelled, an RUD isn't going to be
132  * constructed and thus we free the RUI here directly.
133  */
134 STATIC void
xfs_rui_item_release(struct xfs_log_item * lip)135 xfs_rui_item_release(
136 	struct xfs_log_item	*lip)
137 {
138 	xfs_rui_release(RUI_ITEM(lip));
139 }
140 
141 /*
142  * Allocate and initialize an rui item with the given number of extents.
143  */
144 STATIC struct xfs_rui_log_item *
xfs_rui_init(struct xfs_mount * mp,unsigned short item_type,uint nextents)145 xfs_rui_init(
146 	struct xfs_mount		*mp,
147 	unsigned short			item_type,
148 	uint				nextents)
149 
150 {
151 	struct xfs_rui_log_item		*ruip;
152 
153 	ASSERT(nextents > 0);
154 	ASSERT(item_type == XFS_LI_RUI || item_type == XFS_LI_RUI_RT);
155 
156 	if (nextents > XFS_RUI_MAX_FAST_EXTENTS)
157 		ruip = kzalloc(xfs_rui_log_item_sizeof(nextents),
158 				GFP_KERNEL | __GFP_NOFAIL);
159 	else
160 		ruip = kmem_cache_zalloc(xfs_rui_cache,
161 					 GFP_KERNEL | __GFP_NOFAIL);
162 
163 	xfs_log_item_init(mp, &ruip->rui_item, item_type, &xfs_rui_item_ops);
164 	ruip->rui_format.rui_nextents = nextents;
165 	ruip->rui_format.rui_id = (uintptr_t)(void *)ruip;
166 	atomic_set(&ruip->rui_next_extent, 0);
167 	atomic_set(&ruip->rui_refcount, 2);
168 
169 	return ruip;
170 }
171 
RUD_ITEM(struct xfs_log_item * lip)172 static inline struct xfs_rud_log_item *RUD_ITEM(struct xfs_log_item *lip)
173 {
174 	return container_of(lip, struct xfs_rud_log_item, rud_item);
175 }
176 
177 STATIC void
xfs_rud_item_size(struct xfs_log_item * lip,int * nvecs,int * nbytes)178 xfs_rud_item_size(
179 	struct xfs_log_item	*lip,
180 	int			*nvecs,
181 	int			*nbytes)
182 {
183 	*nvecs += 1;
184 	*nbytes += sizeof(struct xfs_rud_log_format);
185 }
186 
xfs_rud_log_space(void)187 unsigned int xfs_rud_log_space(void)
188 {
189 	return xlog_item_space(1, sizeof(struct xfs_rud_log_format));
190 }
191 
192 /*
193  * This is called to fill in the vector of log iovecs for the
194  * given rud log item. We use only 1 iovec, and we point that
195  * at the rud_log_format structure embedded in the rud item.
196  * It is at this point that we assert that all of the extent
197  * slots in the rud item have been filled.
198  */
199 STATIC void
xfs_rud_item_format(struct xfs_log_item * lip,struct xlog_format_buf * lfb)200 xfs_rud_item_format(
201 	struct xfs_log_item	*lip,
202 	struct xlog_format_buf	*lfb)
203 {
204 	struct xfs_rud_log_item	*rudp = RUD_ITEM(lip);
205 
206 	ASSERT(lip->li_type == XFS_LI_RUD || lip->li_type == XFS_LI_RUD_RT);
207 
208 	rudp->rud_format.rud_type = lip->li_type;
209 	rudp->rud_format.rud_size = 1;
210 
211 	xlog_format_copy(lfb, XLOG_REG_TYPE_RUD_FORMAT, &rudp->rud_format,
212 			sizeof(struct xfs_rud_log_format));
213 }
214 
215 /*
216  * The RUD is either committed or aborted if the transaction is cancelled. If
217  * the transaction is cancelled, drop our reference to the RUI and free the
218  * RUD.
219  */
220 STATIC void
xfs_rud_item_release(struct xfs_log_item * lip)221 xfs_rud_item_release(
222 	struct xfs_log_item	*lip)
223 {
224 	struct xfs_rud_log_item	*rudp = RUD_ITEM(lip);
225 
226 	xfs_rui_release(rudp->rud_ruip);
227 	kvfree(rudp->rud_item.li_lv_shadow);
228 	kmem_cache_free(xfs_rud_cache, rudp);
229 }
230 
231 static struct xfs_log_item *
xfs_rud_item_intent(struct xfs_log_item * lip)232 xfs_rud_item_intent(
233 	struct xfs_log_item	*lip)
234 {
235 	return &RUD_ITEM(lip)->rud_ruip->rui_item;
236 }
237 
238 static const struct xfs_item_ops xfs_rud_item_ops = {
239 	.flags		= XFS_ITEM_RELEASE_WHEN_COMMITTED |
240 			  XFS_ITEM_INTENT_DONE,
241 	.iop_size	= xfs_rud_item_size,
242 	.iop_format	= xfs_rud_item_format,
243 	.iop_release	= xfs_rud_item_release,
244 	.iop_intent	= xfs_rud_item_intent,
245 };
246 
ri_entry(const struct list_head * e)247 static inline struct xfs_rmap_intent *ri_entry(const struct list_head *e)
248 {
249 	return list_entry(e, struct xfs_rmap_intent, ri_list);
250 }
251 
252 static inline bool
xfs_rui_item_isrt(const struct xfs_log_item * lip)253 xfs_rui_item_isrt(const struct xfs_log_item *lip)
254 {
255 	ASSERT(lip->li_type == XFS_LI_RUI || lip->li_type == XFS_LI_RUI_RT);
256 
257 	return lip->li_type == XFS_LI_RUI_RT;
258 }
259 
260 /* Sort rmap intents by AG. */
261 static int
xfs_rmap_update_diff_items(void * priv,const struct list_head * a,const struct list_head * b)262 xfs_rmap_update_diff_items(
263 	void				*priv,
264 	const struct list_head		*a,
265 	const struct list_head		*b)
266 {
267 	struct xfs_rmap_intent		*ra = ri_entry(a);
268 	struct xfs_rmap_intent		*rb = ri_entry(b);
269 
270 	return cmp_int(ra->ri_group->xg_gno, rb->ri_group->xg_gno);
271 }
272 
273 /* Log rmap updates in the intent item. */
274 STATIC void
xfs_rmap_update_log_item(struct xfs_trans * tp,struct xfs_rui_log_item * ruip,struct xfs_rmap_intent * ri)275 xfs_rmap_update_log_item(
276 	struct xfs_trans		*tp,
277 	struct xfs_rui_log_item		*ruip,
278 	struct xfs_rmap_intent		*ri)
279 {
280 	uint				next_extent;
281 	struct xfs_map_extent		*map;
282 
283 	/*
284 	 * atomic_inc_return gives us the value after the increment;
285 	 * we want to use it as an array index so we need to subtract 1 from
286 	 * it.
287 	 */
288 	next_extent = atomic_inc_return(&ruip->rui_next_extent) - 1;
289 	ASSERT(next_extent < ruip->rui_format.rui_nextents);
290 	map = &ruip->rui_format.rui_extents[next_extent];
291 	map->me_owner = ri->ri_owner;
292 	map->me_startblock = ri->ri_bmap.br_startblock;
293 	map->me_startoff = ri->ri_bmap.br_startoff;
294 	map->me_len = ri->ri_bmap.br_blockcount;
295 
296 	map->me_flags = 0;
297 	if (ri->ri_bmap.br_state == XFS_EXT_UNWRITTEN)
298 		map->me_flags |= XFS_RMAP_EXTENT_UNWRITTEN;
299 	if (ri->ri_whichfork == XFS_ATTR_FORK)
300 		map->me_flags |= XFS_RMAP_EXTENT_ATTR_FORK;
301 	switch (ri->ri_type) {
302 	case XFS_RMAP_MAP:
303 		map->me_flags |= XFS_RMAP_EXTENT_MAP;
304 		break;
305 	case XFS_RMAP_MAP_SHARED:
306 		map->me_flags |= XFS_RMAP_EXTENT_MAP_SHARED;
307 		break;
308 	case XFS_RMAP_UNMAP:
309 		map->me_flags |= XFS_RMAP_EXTENT_UNMAP;
310 		break;
311 	case XFS_RMAP_UNMAP_SHARED:
312 		map->me_flags |= XFS_RMAP_EXTENT_UNMAP_SHARED;
313 		break;
314 	case XFS_RMAP_CONVERT:
315 		map->me_flags |= XFS_RMAP_EXTENT_CONVERT;
316 		break;
317 	case XFS_RMAP_CONVERT_SHARED:
318 		map->me_flags |= XFS_RMAP_EXTENT_CONVERT_SHARED;
319 		break;
320 	case XFS_RMAP_ALLOC:
321 		map->me_flags |= XFS_RMAP_EXTENT_ALLOC;
322 		break;
323 	case XFS_RMAP_FREE:
324 		map->me_flags |= XFS_RMAP_EXTENT_FREE;
325 		break;
326 	default:
327 		ASSERT(0);
328 	}
329 }
330 
331 static struct xfs_log_item *
__xfs_rmap_update_create_intent(struct xfs_trans * tp,struct list_head * items,unsigned int count,bool sort,unsigned short item_type)332 __xfs_rmap_update_create_intent(
333 	struct xfs_trans		*tp,
334 	struct list_head		*items,
335 	unsigned int			count,
336 	bool				sort,
337 	unsigned short			item_type)
338 {
339 	struct xfs_mount		*mp = tp->t_mountp;
340 	struct xfs_rui_log_item		*ruip;
341 	struct xfs_rmap_intent		*ri;
342 
343 	ASSERT(count > 0);
344 
345 	ruip = xfs_rui_init(mp, item_type, count);
346 	if (sort)
347 		list_sort(mp, items, xfs_rmap_update_diff_items);
348 	list_for_each_entry(ri, items, ri_list)
349 		xfs_rmap_update_log_item(tp, ruip, ri);
350 	return &ruip->rui_item;
351 }
352 
353 static struct xfs_log_item *
xfs_rmap_update_create_intent(struct xfs_trans * tp,struct list_head * items,unsigned int count,bool sort)354 xfs_rmap_update_create_intent(
355 	struct xfs_trans		*tp,
356 	struct list_head		*items,
357 	unsigned int			count,
358 	bool				sort)
359 {
360 	return __xfs_rmap_update_create_intent(tp, items, count, sort,
361 			XFS_LI_RUI);
362 }
363 
364 static inline unsigned short
xfs_rud_type_from_rui(const struct xfs_rui_log_item * ruip)365 xfs_rud_type_from_rui(const struct xfs_rui_log_item *ruip)
366 {
367 	return xfs_rui_item_isrt(&ruip->rui_item) ? XFS_LI_RUD_RT : XFS_LI_RUD;
368 }
369 
370 /* Get an RUD so we can process all the deferred rmap updates. */
371 static struct xfs_log_item *
xfs_rmap_update_create_done(struct xfs_trans * tp,struct xfs_log_item * intent,unsigned int count)372 xfs_rmap_update_create_done(
373 	struct xfs_trans		*tp,
374 	struct xfs_log_item		*intent,
375 	unsigned int			count)
376 {
377 	struct xfs_rui_log_item		*ruip = RUI_ITEM(intent);
378 	struct xfs_rud_log_item		*rudp;
379 
380 	rudp = kmem_cache_zalloc(xfs_rud_cache, GFP_KERNEL | __GFP_NOFAIL);
381 	xfs_log_item_init(tp->t_mountp, &rudp->rud_item,
382 			xfs_rud_type_from_rui(ruip), &xfs_rud_item_ops);
383 	rudp->rud_ruip = ruip;
384 	rudp->rud_format.rud_rui_id = ruip->rui_format.rui_id;
385 
386 	return &rudp->rud_item;
387 }
388 
389 /* Add this deferred RUI to the transaction. */
390 void
xfs_rmap_defer_add(struct xfs_trans * tp,struct xfs_rmap_intent * ri)391 xfs_rmap_defer_add(
392 	struct xfs_trans	*tp,
393 	struct xfs_rmap_intent	*ri)
394 {
395 	struct xfs_mount	*mp = tp->t_mountp;
396 
397 	/*
398 	 * Deferred rmap updates for the realtime and data sections must use
399 	 * separate transactions to finish deferred work because updates to
400 	 * realtime metadata files can lock AGFs to allocate btree blocks and
401 	 * we don't want that mixing with the AGF locks taken to finish data
402 	 * section updates.
403 	 */
404 	ri->ri_group = xfs_group_intent_get(mp, ri->ri_bmap.br_startblock,
405 			ri->ri_realtime ? XG_TYPE_RTG : XG_TYPE_AG);
406 
407 	trace_xfs_rmap_defer(mp, ri);
408 	xfs_defer_add(tp, &ri->ri_list, ri->ri_realtime ?
409 			&xfs_rtrmap_update_defer_type :
410 			&xfs_rmap_update_defer_type);
411 }
412 
413 /* Cancel a deferred rmap update. */
414 STATIC void
xfs_rmap_update_cancel_item(struct list_head * item)415 xfs_rmap_update_cancel_item(
416 	struct list_head		*item)
417 {
418 	struct xfs_rmap_intent		*ri = ri_entry(item);
419 
420 	xfs_group_intent_put(ri->ri_group);
421 	kmem_cache_free(xfs_rmap_intent_cache, ri);
422 }
423 
424 /* Process a deferred rmap update. */
425 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)426 xfs_rmap_update_finish_item(
427 	struct xfs_trans		*tp,
428 	struct xfs_log_item		*done,
429 	struct list_head		*item,
430 	struct xfs_btree_cur		**state)
431 {
432 	struct xfs_rmap_intent		*ri = ri_entry(item);
433 	int				error;
434 
435 	error = xfs_rmap_finish_one(tp, ri, state);
436 
437 	xfs_rmap_update_cancel_item(item);
438 	return error;
439 }
440 
441 /* Clean up after calling xfs_rmap_finish_one. */
442 STATIC void
xfs_rmap_finish_one_cleanup(struct xfs_trans * tp,struct xfs_btree_cur * rcur,int error)443 xfs_rmap_finish_one_cleanup(
444 	struct xfs_trans	*tp,
445 	struct xfs_btree_cur	*rcur,
446 	int			error)
447 {
448 	struct xfs_buf		*agbp = NULL;
449 
450 	if (rcur == NULL)
451 		return;
452 	agbp = rcur->bc_ag.agbp;
453 	xfs_btree_del_cursor(rcur, error);
454 	if (error && agbp)
455 		xfs_trans_brelse(tp, agbp);
456 }
457 
458 /* Abort all pending RUIs. */
459 STATIC void
xfs_rmap_update_abort_intent(struct xfs_log_item * intent)460 xfs_rmap_update_abort_intent(
461 	struct xfs_log_item	*intent)
462 {
463 	xfs_rui_release(RUI_ITEM(intent));
464 }
465 
466 /* Is this recovered RUI ok? */
467 static inline bool
xfs_rui_validate_map(struct xfs_mount * mp,bool isrt,struct xfs_map_extent * map)468 xfs_rui_validate_map(
469 	struct xfs_mount		*mp,
470 	bool				isrt,
471 	struct xfs_map_extent		*map)
472 {
473 	if (!xfs_has_rmapbt(mp))
474 		return false;
475 
476 	if (map->me_flags & ~XFS_RMAP_EXTENT_FLAGS)
477 		return false;
478 
479 	switch (map->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
480 	case XFS_RMAP_EXTENT_MAP:
481 	case XFS_RMAP_EXTENT_MAP_SHARED:
482 	case XFS_RMAP_EXTENT_UNMAP:
483 	case XFS_RMAP_EXTENT_UNMAP_SHARED:
484 	case XFS_RMAP_EXTENT_CONVERT:
485 	case XFS_RMAP_EXTENT_CONVERT_SHARED:
486 	case XFS_RMAP_EXTENT_ALLOC:
487 	case XFS_RMAP_EXTENT_FREE:
488 		break;
489 	default:
490 		return false;
491 	}
492 
493 	if (!XFS_RMAP_NON_INODE_OWNER(map->me_owner) &&
494 	    !xfs_verify_ino(mp, map->me_owner))
495 		return false;
496 
497 	if (!xfs_verify_fileext(mp, map->me_startoff, map->me_len))
498 		return false;
499 
500 	if (isrt)
501 		return xfs_verify_rtbext(mp, map->me_startblock, map->me_len);
502 
503 	return xfs_verify_fsbext(mp, map->me_startblock, map->me_len);
504 }
505 
506 static inline void
xfs_rui_recover_work(struct xfs_mount * mp,struct xfs_defer_pending * dfp,bool isrt,const struct xfs_map_extent * map)507 xfs_rui_recover_work(
508 	struct xfs_mount		*mp,
509 	struct xfs_defer_pending	*dfp,
510 	bool				isrt,
511 	const struct xfs_map_extent	*map)
512 {
513 	struct xfs_rmap_intent		*ri;
514 
515 	ri = kmem_cache_alloc(xfs_rmap_intent_cache, GFP_KERNEL | __GFP_NOFAIL);
516 
517 	switch (map->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
518 	case XFS_RMAP_EXTENT_MAP:
519 		ri->ri_type = XFS_RMAP_MAP;
520 		break;
521 	case XFS_RMAP_EXTENT_MAP_SHARED:
522 		ri->ri_type = XFS_RMAP_MAP_SHARED;
523 		break;
524 	case XFS_RMAP_EXTENT_UNMAP:
525 		ri->ri_type = XFS_RMAP_UNMAP;
526 		break;
527 	case XFS_RMAP_EXTENT_UNMAP_SHARED:
528 		ri->ri_type = XFS_RMAP_UNMAP_SHARED;
529 		break;
530 	case XFS_RMAP_EXTENT_CONVERT:
531 		ri->ri_type = XFS_RMAP_CONVERT;
532 		break;
533 	case XFS_RMAP_EXTENT_CONVERT_SHARED:
534 		ri->ri_type = XFS_RMAP_CONVERT_SHARED;
535 		break;
536 	case XFS_RMAP_EXTENT_ALLOC:
537 		ri->ri_type = XFS_RMAP_ALLOC;
538 		break;
539 	case XFS_RMAP_EXTENT_FREE:
540 		ri->ri_type = XFS_RMAP_FREE;
541 		break;
542 	default:
543 		ASSERT(0);
544 		return;
545 	}
546 
547 	ri->ri_owner = map->me_owner;
548 	ri->ri_whichfork = (map->me_flags & XFS_RMAP_EXTENT_ATTR_FORK) ?
549 			XFS_ATTR_FORK : XFS_DATA_FORK;
550 	ri->ri_bmap.br_startblock = map->me_startblock;
551 	ri->ri_bmap.br_startoff = map->me_startoff;
552 	ri->ri_bmap.br_blockcount = map->me_len;
553 	ri->ri_bmap.br_state = (map->me_flags & XFS_RMAP_EXTENT_UNWRITTEN) ?
554 			XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
555 	ri->ri_group = xfs_group_intent_get(mp, map->me_startblock,
556 			isrt ? XG_TYPE_RTG : XG_TYPE_AG);
557 	ri->ri_realtime = isrt;
558 
559 	xfs_defer_add_item(dfp, &ri->ri_list);
560 }
561 
562 /*
563  * Process an rmap update intent item that was recovered from the log.
564  * We need to update the rmapbt.
565  */
566 STATIC int
xfs_rmap_recover_work(struct xfs_defer_pending * dfp,struct list_head * capture_list)567 xfs_rmap_recover_work(
568 	struct xfs_defer_pending	*dfp,
569 	struct list_head		*capture_list)
570 {
571 	struct xfs_trans_res		resv;
572 	struct xfs_log_item		*lip = dfp->dfp_intent;
573 	struct xfs_rui_log_item		*ruip = RUI_ITEM(lip);
574 	struct xfs_trans		*tp;
575 	struct xfs_mount		*mp = lip->li_log->l_mp;
576 	unsigned int			dblocks;
577 	bool				isrt = xfs_rui_item_isrt(lip);
578 	int				i;
579 	int				error = 0;
580 
581 	/*
582 	 * First check the validity of the extents described by the
583 	 * RUI.  If any are bad, then assume that all are bad and
584 	 * just toss the RUI.
585 	 */
586 	for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
587 		if (!xfs_rui_validate_map(mp, isrt,
588 					&ruip->rui_format.rui_extents[i])) {
589 			XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
590 					&ruip->rui_format,
591 					sizeof(ruip->rui_format));
592 			return -EFSCORRUPTED;
593 		}
594 
595 		xfs_rui_recover_work(mp, dfp, isrt,
596 				&ruip->rui_format.rui_extents[i]);
597 	}
598 
599 	resv = xlog_recover_resv(&M_RES(mp)->tr_itruncate);
600 	if (isrt)
601 		dblocks = mp->m_rtrmap_maxlevels;
602 	else
603 		dblocks = mp->m_rmap_maxlevels;
604 	error = xfs_trans_alloc(mp, &resv, dblocks, 0, XFS_TRANS_RESERVE, &tp);
605 	if (error)
606 		return error;
607 
608 	error = xlog_recover_finish_intent(tp, dfp);
609 	if (error == -EFSCORRUPTED)
610 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
611 				&ruip->rui_format,
612 				sizeof(ruip->rui_format));
613 	if (error)
614 		goto abort_error;
615 
616 	return xfs_defer_ops_capture_and_commit(tp, capture_list);
617 
618 abort_error:
619 	xfs_trans_cancel(tp);
620 	return error;
621 }
622 
623 /* Relog an intent item to push the log tail forward. */
624 static struct xfs_log_item *
xfs_rmap_relog_intent(struct xfs_trans * tp,struct xfs_log_item * intent,struct xfs_log_item * done_item)625 xfs_rmap_relog_intent(
626 	struct xfs_trans		*tp,
627 	struct xfs_log_item		*intent,
628 	struct xfs_log_item		*done_item)
629 {
630 	struct xfs_rui_log_item		*ruip;
631 	struct xfs_map_extent		*map;
632 	unsigned int			count;
633 
634 	ASSERT(intent->li_type == XFS_LI_RUI ||
635 	       intent->li_type == XFS_LI_RUI_RT);
636 
637 	count = RUI_ITEM(intent)->rui_format.rui_nextents;
638 	map = RUI_ITEM(intent)->rui_format.rui_extents;
639 
640 	ruip = xfs_rui_init(tp->t_mountp, intent->li_type, count);
641 	memcpy(ruip->rui_format.rui_extents, map, count * sizeof(*map));
642 	atomic_set(&ruip->rui_next_extent, count);
643 
644 	return &ruip->rui_item;
645 }
646 
647 const struct xfs_defer_op_type xfs_rmap_update_defer_type = {
648 	.name		= "rmap",
649 	.max_items	= XFS_RUI_MAX_FAST_EXTENTS,
650 	.create_intent	= xfs_rmap_update_create_intent,
651 	.abort_intent	= xfs_rmap_update_abort_intent,
652 	.create_done	= xfs_rmap_update_create_done,
653 	.finish_item	= xfs_rmap_update_finish_item,
654 	.finish_cleanup = xfs_rmap_finish_one_cleanup,
655 	.cancel_item	= xfs_rmap_update_cancel_item,
656 	.recover_work	= xfs_rmap_recover_work,
657 	.relog_intent	= xfs_rmap_relog_intent,
658 };
659 
660 #ifdef CONFIG_XFS_RT
661 static struct xfs_log_item *
xfs_rtrmap_update_create_intent(struct xfs_trans * tp,struct list_head * items,unsigned int count,bool sort)662 xfs_rtrmap_update_create_intent(
663 	struct xfs_trans		*tp,
664 	struct list_head		*items,
665 	unsigned int			count,
666 	bool				sort)
667 {
668 	return __xfs_rmap_update_create_intent(tp, items, count, sort,
669 			XFS_LI_RUI_RT);
670 }
671 
672 /* Clean up after calling xfs_rmap_finish_one. */
673 STATIC void
xfs_rtrmap_finish_one_cleanup(struct xfs_trans * tp,struct xfs_btree_cur * rcur,int error)674 xfs_rtrmap_finish_one_cleanup(
675 	struct xfs_trans	*tp,
676 	struct xfs_btree_cur	*rcur,
677 	int			error)
678 {
679 	if (rcur)
680 		xfs_btree_del_cursor(rcur, error);
681 }
682 
683 const struct xfs_defer_op_type xfs_rtrmap_update_defer_type = {
684 	.name		= "rtrmap",
685 	.max_items	= XFS_RUI_MAX_FAST_EXTENTS,
686 	.create_intent	= xfs_rtrmap_update_create_intent,
687 	.abort_intent	= xfs_rmap_update_abort_intent,
688 	.create_done	= xfs_rmap_update_create_done,
689 	.finish_item	= xfs_rmap_update_finish_item,
690 	.finish_cleanup = xfs_rtrmap_finish_one_cleanup,
691 	.cancel_item	= xfs_rmap_update_cancel_item,
692 	.recover_work	= xfs_rmap_recover_work,
693 	.relog_intent	= xfs_rmap_relog_intent,
694 };
695 #else
696 const struct xfs_defer_op_type xfs_rtrmap_update_defer_type = {
697 	.name		= "rtrmap",
698 };
699 #endif
700 
701 STATIC bool
xfs_rui_item_match(struct xfs_log_item * lip,uint64_t intent_id)702 xfs_rui_item_match(
703 	struct xfs_log_item	*lip,
704 	uint64_t		intent_id)
705 {
706 	return RUI_ITEM(lip)->rui_format.rui_id == intent_id;
707 }
708 
709 static const struct xfs_item_ops xfs_rui_item_ops = {
710 	.flags		= XFS_ITEM_INTENT,
711 	.iop_size	= xfs_rui_item_size,
712 	.iop_format	= xfs_rui_item_format,
713 	.iop_unpin	= xfs_rui_item_unpin,
714 	.iop_release	= xfs_rui_item_release,
715 	.iop_match	= xfs_rui_item_match,
716 };
717 
718 static inline void
xfs_rui_copy_format(struct xfs_rui_log_format * dst,const struct xfs_rui_log_format * src)719 xfs_rui_copy_format(
720 	struct xfs_rui_log_format	*dst,
721 	const struct xfs_rui_log_format	*src)
722 {
723 	unsigned int			i;
724 
725 	memcpy(dst, src, offsetof(struct xfs_rui_log_format, rui_extents));
726 
727 	for (i = 0; i < src->rui_nextents; i++)
728 		memcpy(&dst->rui_extents[i], &src->rui_extents[i],
729 				sizeof(struct xfs_map_extent));
730 }
731 
732 /*
733  * This routine is called to create an in-core extent rmap update
734  * item from the rui format structure which was logged on disk.
735  * It allocates an in-core rui, copies the extents from the format
736  * structure into it, and adds the rui to the AIL with the given
737  * LSN.
738  */
739 STATIC int
xlog_recover_rui_commit_pass2(struct xlog * log,struct list_head * buffer_list,struct xlog_recover_item * item,xfs_lsn_t lsn)740 xlog_recover_rui_commit_pass2(
741 	struct xlog			*log,
742 	struct list_head		*buffer_list,
743 	struct xlog_recover_item	*item,
744 	xfs_lsn_t			lsn)
745 {
746 	struct xfs_mount		*mp = log->l_mp;
747 	struct xfs_rui_log_item		*ruip;
748 	struct xfs_rui_log_format	*rui_formatp;
749 	size_t				len;
750 
751 	rui_formatp = item->ri_buf[0].iov_base;
752 
753 	if (item->ri_buf[0].iov_len < xfs_rui_log_format_sizeof(0)) {
754 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
755 				item->ri_buf[0].iov_base, item->ri_buf[0].iov_len);
756 		return -EFSCORRUPTED;
757 	}
758 
759 	len = xfs_rui_log_format_sizeof(rui_formatp->rui_nextents);
760 	if (item->ri_buf[0].iov_len != len) {
761 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
762 				item->ri_buf[0].iov_base, item->ri_buf[0].iov_len);
763 		return -EFSCORRUPTED;
764 	}
765 
766 	ruip = xfs_rui_init(mp, ITEM_TYPE(item), rui_formatp->rui_nextents);
767 	xfs_rui_copy_format(&ruip->rui_format, rui_formatp);
768 	atomic_set(&ruip->rui_next_extent, rui_formatp->rui_nextents);
769 
770 	xlog_recover_intent_item(log, &ruip->rui_item, lsn,
771 			&xfs_rmap_update_defer_type);
772 	return 0;
773 }
774 
775 const struct xlog_recover_item_ops xlog_rui_item_ops = {
776 	.item_type		= XFS_LI_RUI,
777 	.commit_pass2		= xlog_recover_rui_commit_pass2,
778 };
779 
780 #ifdef CONFIG_XFS_RT
781 STATIC int
xlog_recover_rtrui_commit_pass2(struct xlog * log,struct list_head * buffer_list,struct xlog_recover_item * item,xfs_lsn_t lsn)782 xlog_recover_rtrui_commit_pass2(
783 	struct xlog			*log,
784 	struct list_head		*buffer_list,
785 	struct xlog_recover_item	*item,
786 	xfs_lsn_t			lsn)
787 {
788 	struct xfs_mount		*mp = log->l_mp;
789 	struct xfs_rui_log_item		*ruip;
790 	struct xfs_rui_log_format	*rui_formatp;
791 	size_t				len;
792 
793 	rui_formatp = item->ri_buf[0].iov_base;
794 
795 	if (item->ri_buf[0].iov_len < xfs_rui_log_format_sizeof(0)) {
796 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
797 				item->ri_buf[0].iov_base, item->ri_buf[0].iov_len);
798 		return -EFSCORRUPTED;
799 	}
800 
801 	len = xfs_rui_log_format_sizeof(rui_formatp->rui_nextents);
802 	if (item->ri_buf[0].iov_len != len) {
803 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
804 				item->ri_buf[0].iov_base, item->ri_buf[0].iov_len);
805 		return -EFSCORRUPTED;
806 	}
807 
808 	ruip = xfs_rui_init(mp, ITEM_TYPE(item), rui_formatp->rui_nextents);
809 	xfs_rui_copy_format(&ruip->rui_format, rui_formatp);
810 	atomic_set(&ruip->rui_next_extent, rui_formatp->rui_nextents);
811 
812 	xlog_recover_intent_item(log, &ruip->rui_item, lsn,
813 			&xfs_rtrmap_update_defer_type);
814 	return 0;
815 }
816 #else
817 STATIC int
xlog_recover_rtrui_commit_pass2(struct xlog * log,struct list_head * buffer_list,struct xlog_recover_item * item,xfs_lsn_t lsn)818 xlog_recover_rtrui_commit_pass2(
819 	struct xlog			*log,
820 	struct list_head		*buffer_list,
821 	struct xlog_recover_item	*item,
822 	xfs_lsn_t			lsn)
823 {
824 	XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
825 			item->ri_buf[0].iov_base, item->ri_buf[0].iov_len);
826 	return -EFSCORRUPTED;
827 }
828 #endif
829 
830 const struct xlog_recover_item_ops xlog_rtrui_item_ops = {
831 	.item_type		= XFS_LI_RUI_RT,
832 	.commit_pass2		= xlog_recover_rtrui_commit_pass2,
833 };
834 
835 /*
836  * This routine is called when an RUD format structure is found in a committed
837  * transaction in the log. Its purpose is to cancel the corresponding RUI if it
838  * was still in the log. To do this it searches the AIL for the RUI with an id
839  * equal to that in the RUD format structure. If we find it we drop the RUD
840  * reference, which removes the RUI from the AIL and frees it.
841  */
842 STATIC int
xlog_recover_rud_commit_pass2(struct xlog * log,struct list_head * buffer_list,struct xlog_recover_item * item,xfs_lsn_t lsn)843 xlog_recover_rud_commit_pass2(
844 	struct xlog			*log,
845 	struct list_head		*buffer_list,
846 	struct xlog_recover_item	*item,
847 	xfs_lsn_t			lsn)
848 {
849 	struct xfs_rud_log_format	*rud_formatp;
850 
851 	rud_formatp = item->ri_buf[0].iov_base;
852 	if (item->ri_buf[0].iov_len != sizeof(struct xfs_rud_log_format)) {
853 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
854 				rud_formatp, item->ri_buf[0].iov_len);
855 		return -EFSCORRUPTED;
856 	}
857 
858 	xlog_recover_release_intent(log, XFS_LI_RUI, rud_formatp->rud_rui_id);
859 	return 0;
860 }
861 
862 const struct xlog_recover_item_ops xlog_rud_item_ops = {
863 	.item_type		= XFS_LI_RUD,
864 	.commit_pass2		= xlog_recover_rud_commit_pass2,
865 };
866 
867 #ifdef CONFIG_XFS_RT
868 STATIC int
xlog_recover_rtrud_commit_pass2(struct xlog * log,struct list_head * buffer_list,struct xlog_recover_item * item,xfs_lsn_t lsn)869 xlog_recover_rtrud_commit_pass2(
870 	struct xlog			*log,
871 	struct list_head		*buffer_list,
872 	struct xlog_recover_item	*item,
873 	xfs_lsn_t			lsn)
874 {
875 	struct xfs_rud_log_format	*rud_formatp;
876 
877 	rud_formatp = item->ri_buf[0].iov_base;
878 	if (item->ri_buf[0].iov_len != sizeof(struct xfs_rud_log_format)) {
879 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
880 				rud_formatp, item->ri_buf[0].iov_len);
881 		return -EFSCORRUPTED;
882 	}
883 
884 	xlog_recover_release_intent(log, XFS_LI_RUI_RT,
885 			rud_formatp->rud_rui_id);
886 	return 0;
887 }
888 #else
889 # define xlog_recover_rtrud_commit_pass2	xlog_recover_rtrui_commit_pass2
890 #endif
891 
892 const struct xlog_recover_item_ops xlog_rtrud_item_ops = {
893 	.item_type		= XFS_LI_RUD_RT,
894 	.commit_pass2		= xlog_recover_rtrud_commit_pass2,
895 };
896