xref: /linux/fs/xfs/xfs_extfree_item.c (revision cf9b52fa7d65362b648927d1d752ec99659f5c43)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
4  * All Rights Reserved.
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_ag.h"
15 #include "xfs_defer.h"
16 #include "xfs_trans.h"
17 #include "xfs_trans_priv.h"
18 #include "xfs_extfree_item.h"
19 #include "xfs_log.h"
20 #include "xfs_btree.h"
21 #include "xfs_rmap.h"
22 #include "xfs_alloc.h"
23 #include "xfs_bmap.h"
24 #include "xfs_trace.h"
25 #include "xfs_error.h"
26 #include "xfs_log_priv.h"
27 #include "xfs_log_recover.h"
28 #include "xfs_rtalloc.h"
29 #include "xfs_inode.h"
30 #include "xfs_rtbitmap.h"
31 #include "xfs_rtgroup.h"
32 #include "xfs_zone_alloc.h"
33 
34 struct kmem_cache	*xfs_efi_cache;
35 struct kmem_cache	*xfs_efd_cache;
36 
37 static const struct xfs_item_ops xfs_efi_item_ops;
38 
39 static inline struct xfs_efi_log_item *EFI_ITEM(struct xfs_log_item *lip)
40 {
41 	return container_of(lip, struct xfs_efi_log_item, efi_item);
42 }
43 
44 STATIC void
45 xfs_efi_item_free(
46 	struct xfs_efi_log_item	*efip)
47 {
48 	kvfree(efip->efi_item.li_lv_shadow);
49 	if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS)
50 		kfree(efip);
51 	else
52 		kmem_cache_free(xfs_efi_cache, efip);
53 }
54 
55 /*
56  * Freeing the efi requires that we remove it from the AIL if it has already
57  * been placed there. However, the EFI may not yet have been placed in the AIL
58  * when called by xfs_efi_release() from EFD processing due to the ordering of
59  * committed vs unpin operations in bulk insert operations. Hence the reference
60  * count to ensure only the last caller frees the EFI.
61  */
62 STATIC void
63 xfs_efi_release(
64 	struct xfs_efi_log_item	*efip)
65 {
66 	ASSERT(atomic_read(&efip->efi_refcount) > 0);
67 	if (!atomic_dec_and_test(&efip->efi_refcount))
68 		return;
69 
70 	xfs_trans_ail_delete(&efip->efi_item, 0);
71 	xfs_efi_item_free(efip);
72 }
73 
74 STATIC void
75 xfs_efi_item_size(
76 	struct xfs_log_item	*lip,
77 	int			*nvecs,
78 	int			*nbytes)
79 {
80 	struct xfs_efi_log_item	*efip = EFI_ITEM(lip);
81 
82 	*nvecs += 1;
83 	*nbytes += xfs_efi_log_format_sizeof(efip->efi_format.efi_nextents);
84 }
85 
86 unsigned int xfs_efi_log_space(unsigned int nr)
87 {
88 	return xlog_item_space(1, xfs_efi_log_format_sizeof(nr));
89 }
90 
91 /*
92  * This is called to fill in the vector of log iovecs for the
93  * given efi log item. We use only 1 iovec, and we point that
94  * at the efi_log_format structure embedded in the efi item.
95  * It is at this point that we assert that all of the extent
96  * slots in the efi item have been filled.
97  */
98 STATIC void
99 xfs_efi_item_format(
100 	struct xfs_log_item	*lip,
101 	struct xlog_format_buf	*lfb)
102 {
103 	struct xfs_efi_log_item	*efip = EFI_ITEM(lip);
104 
105 	ASSERT(atomic_read(&efip->efi_next_extent) ==
106 				efip->efi_format.efi_nextents);
107 	ASSERT(lip->li_type == XFS_LI_EFI || lip->li_type == XFS_LI_EFI_RT);
108 
109 	efip->efi_format.efi_type = lip->li_type;
110 	efip->efi_format.efi_size = 1;
111 
112 	xlog_format_copy(lfb, XLOG_REG_TYPE_EFI_FORMAT, &efip->efi_format,
113 			xfs_efi_log_format_sizeof(efip->efi_format.efi_nextents));
114 }
115 
116 /*
117  * The unpin operation is the last place an EFI is manipulated in the log. It is
118  * either inserted in the AIL or aborted in the event of a log I/O error. In
119  * either case, the EFI transaction has been successfully committed to make it
120  * this far. Therefore, we expect whoever committed the EFI to either construct
121  * and commit the EFD or drop the EFD's reference in the event of error. Simply
122  * drop the log's EFI reference now that the log is done with it.
123  */
124 STATIC void
125 xfs_efi_item_unpin(
126 	struct xfs_log_item	*lip,
127 	int			remove)
128 {
129 	struct xfs_efi_log_item	*efip = EFI_ITEM(lip);
130 	xfs_efi_release(efip);
131 }
132 
133 /*
134  * The EFI has been either committed or aborted if the transaction has been
135  * cancelled. If the transaction was cancelled, an EFD isn't going to be
136  * constructed and thus we free the EFI here directly.
137  */
138 STATIC void
139 xfs_efi_item_release(
140 	struct xfs_log_item	*lip)
141 {
142 	xfs_efi_release(EFI_ITEM(lip));
143 }
144 
145 /*
146  * Allocate and initialize an efi item with the given number of extents.
147  */
148 STATIC struct xfs_efi_log_item *
149 xfs_efi_init(
150 	struct xfs_mount	*mp,
151 	unsigned short		item_type,
152 	uint			nextents)
153 {
154 	struct xfs_efi_log_item	*efip;
155 
156 	ASSERT(item_type == XFS_LI_EFI || item_type == XFS_LI_EFI_RT);
157 	ASSERT(nextents > 0);
158 
159 	if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
160 		efip = kzalloc(xfs_efi_log_item_sizeof(nextents),
161 				GFP_KERNEL | __GFP_NOFAIL);
162 	} else {
163 		efip = kmem_cache_zalloc(xfs_efi_cache,
164 					 GFP_KERNEL | __GFP_NOFAIL);
165 	}
166 
167 	xfs_log_item_init(mp, &efip->efi_item, item_type, &xfs_efi_item_ops);
168 	efip->efi_format.efi_nextents = nextents;
169 	efip->efi_format.efi_id = (uintptr_t)(void *)efip;
170 	atomic_set(&efip->efi_next_extent, 0);
171 	atomic_set(&efip->efi_refcount, 2);
172 
173 	return efip;
174 }
175 
176 /*
177  * Copy an EFI format buffer from the given buf, and into the destination
178  * EFI format structure.
179  * The given buffer can be in 32 bit or 64 bit form (which has different padding),
180  * one of which will be the native format for this kernel.
181  * It will handle the conversion of formats if necessary.
182  */
183 STATIC int
184 xfs_efi_copy_format(
185 	struct kvec			*buf,
186 	struct xfs_efi_log_format	*dst_efi_fmt)
187 {
188 	struct xfs_efi_log_format	*src_efi_fmt = buf->iov_base;
189 	uint				len, len32, len64, i;
190 
191 	len = xfs_efi_log_format_sizeof(src_efi_fmt->efi_nextents);
192 	len32 = xfs_efi_log_format32_sizeof(src_efi_fmt->efi_nextents);
193 	len64 = xfs_efi_log_format64_sizeof(src_efi_fmt->efi_nextents);
194 
195 	if (buf->iov_len == len) {
196 		memcpy(dst_efi_fmt, src_efi_fmt,
197 		       offsetof(struct xfs_efi_log_format, efi_extents));
198 		for (i = 0; i < src_efi_fmt->efi_nextents; i++)
199 			memcpy(&dst_efi_fmt->efi_extents[i],
200 			       &src_efi_fmt->efi_extents[i],
201 			       sizeof(struct xfs_extent));
202 		return 0;
203 	} else if (buf->iov_len == len32) {
204 		struct xfs_efi_log_format_32 *src_efi_fmt_32 = buf->iov_base;
205 
206 		dst_efi_fmt->efi_type     = src_efi_fmt_32->efi_type;
207 		dst_efi_fmt->efi_size     = src_efi_fmt_32->efi_size;
208 		dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents;
209 		dst_efi_fmt->efi_id       = src_efi_fmt_32->efi_id;
210 		for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
211 			dst_efi_fmt->efi_extents[i].ext_start =
212 				src_efi_fmt_32->efi_extents[i].ext_start;
213 			dst_efi_fmt->efi_extents[i].ext_len =
214 				src_efi_fmt_32->efi_extents[i].ext_len;
215 		}
216 		return 0;
217 	} else if (buf->iov_len == len64) {
218 		struct xfs_efi_log_format_64 *src_efi_fmt_64 = buf->iov_base;
219 
220 		dst_efi_fmt->efi_type     = src_efi_fmt_64->efi_type;
221 		dst_efi_fmt->efi_size     = src_efi_fmt_64->efi_size;
222 		dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents;
223 		dst_efi_fmt->efi_id       = src_efi_fmt_64->efi_id;
224 		for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
225 			dst_efi_fmt->efi_extents[i].ext_start =
226 				src_efi_fmt_64->efi_extents[i].ext_start;
227 			dst_efi_fmt->efi_extents[i].ext_len =
228 				src_efi_fmt_64->efi_extents[i].ext_len;
229 		}
230 		return 0;
231 	}
232 	XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, NULL, buf->iov_base,
233 			buf->iov_len);
234 	return -EFSCORRUPTED;
235 }
236 
237 static inline struct xfs_efd_log_item *EFD_ITEM(struct xfs_log_item *lip)
238 {
239 	return container_of(lip, struct xfs_efd_log_item, efd_item);
240 }
241 
242 STATIC void
243 xfs_efd_item_free(struct xfs_efd_log_item *efdp)
244 {
245 	kvfree(efdp->efd_item.li_lv_shadow);
246 	if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS)
247 		kfree(efdp);
248 	else
249 		kmem_cache_free(xfs_efd_cache, efdp);
250 }
251 
252 STATIC void
253 xfs_efd_item_size(
254 	struct xfs_log_item	*lip,
255 	int			*nvecs,
256 	int			*nbytes)
257 {
258 	struct xfs_efd_log_item	*efdp = EFD_ITEM(lip);
259 
260 	*nvecs += 1;
261 	*nbytes += xfs_efd_log_format_sizeof(efdp->efd_format.efd_nextents);
262 }
263 
264 unsigned int xfs_efd_log_space(unsigned int nr)
265 {
266 	return xlog_item_space(1, xfs_efd_log_format_sizeof(nr));
267 }
268 
269 /*
270  * This is called to fill in the vector of log iovecs for the
271  * given efd log item. We use only 1 iovec, and we point that
272  * at the efd_log_format structure embedded in the efd item.
273  * It is at this point that we assert that all of the extent
274  * slots in the efd item have been filled.
275  */
276 STATIC void
277 xfs_efd_item_format(
278 	struct xfs_log_item	*lip,
279 	struct xlog_format_buf	*lfb)
280 {
281 	struct xfs_efd_log_item	*efdp = EFD_ITEM(lip);
282 
283 	ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
284 	ASSERT(lip->li_type == XFS_LI_EFD || lip->li_type == XFS_LI_EFD_RT);
285 
286 	efdp->efd_format.efd_type = lip->li_type;
287 	efdp->efd_format.efd_size = 1;
288 
289 	xlog_format_copy(lfb, XLOG_REG_TYPE_EFD_FORMAT, &efdp->efd_format,
290 			xfs_efd_log_format_sizeof(efdp->efd_format.efd_nextents));
291 }
292 
293 /*
294  * The EFD is either committed or aborted if the transaction is cancelled. If
295  * the transaction is cancelled, drop our reference to the EFI and free the EFD.
296  */
297 STATIC void
298 xfs_efd_item_release(
299 	struct xfs_log_item	*lip)
300 {
301 	struct xfs_efd_log_item	*efdp = EFD_ITEM(lip);
302 
303 	xfs_efi_release(efdp->efd_efip);
304 	xfs_efd_item_free(efdp);
305 }
306 
307 static struct xfs_log_item *
308 xfs_efd_item_intent(
309 	struct xfs_log_item	*lip)
310 {
311 	return &EFD_ITEM(lip)->efd_efip->efi_item;
312 }
313 
314 static const struct xfs_item_ops xfs_efd_item_ops = {
315 	.flags		= XFS_ITEM_RELEASE_WHEN_COMMITTED |
316 			  XFS_ITEM_INTENT_DONE,
317 	.iop_size	= xfs_efd_item_size,
318 	.iop_format	= xfs_efd_item_format,
319 	.iop_release	= xfs_efd_item_release,
320 	.iop_intent	= xfs_efd_item_intent,
321 };
322 
323 static inline struct xfs_extent_free_item *xefi_entry(const struct list_head *e)
324 {
325 	return list_entry(e, struct xfs_extent_free_item, xefi_list);
326 }
327 
328 static inline bool
329 xfs_efi_item_isrt(const struct xfs_log_item *lip)
330 {
331 	ASSERT(lip->li_type == XFS_LI_EFI || lip->li_type == XFS_LI_EFI_RT);
332 
333 	return lip->li_type == XFS_LI_EFI_RT;
334 }
335 
336 /*
337  * Fill the EFD with all extents from the EFI when we need to roll the
338  * transaction and continue with a new EFI.
339  *
340  * This simply copies all the extents in the EFI to the EFD rather than make
341  * assumptions about which extents in the EFI have already been processed. We
342  * currently keep the xefi list in the same order as the EFI extent list, but
343  * that may not always be the case. Copying everything avoids leaving a landmine
344  * were we fail to cancel all the extents in an EFI if the xefi list is
345  * processed in a different order to the extents in the EFI.
346  */
347 static void
348 xfs_efd_from_efi(
349 	struct xfs_efd_log_item	*efdp)
350 {
351 	struct xfs_efi_log_item *efip = efdp->efd_efip;
352 	uint                    i;
353 
354 	ASSERT(efip->efi_format.efi_nextents > 0);
355 	ASSERT(efdp->efd_next_extent < efip->efi_format.efi_nextents);
356 
357 	for (i = 0; i < efip->efi_format.efi_nextents; i++) {
358 	       efdp->efd_format.efd_extents[i] =
359 		       efip->efi_format.efi_extents[i];
360 	}
361 	efdp->efd_next_extent = efip->efi_format.efi_nextents;
362 }
363 
364 static void
365 xfs_efd_add_extent(
366 	struct xfs_efd_log_item		*efdp,
367 	struct xfs_extent_free_item	*xefi)
368 {
369 	struct xfs_extent		*extp;
370 
371 	ASSERT(efdp->efd_next_extent < efdp->efd_format.efd_nextents);
372 
373 	extp = &efdp->efd_format.efd_extents[efdp->efd_next_extent];
374 	extp->ext_start = xefi->xefi_startblock;
375 	extp->ext_len = xefi->xefi_blockcount;
376 
377 	efdp->efd_next_extent++;
378 }
379 
380 /* Sort bmap items by AG. */
381 static int
382 xfs_extent_free_diff_items(
383 	void				*priv,
384 	const struct list_head		*a,
385 	const struct list_head		*b)
386 {
387 	struct xfs_extent_free_item	*ra = xefi_entry(a);
388 	struct xfs_extent_free_item	*rb = xefi_entry(b);
389 
390 	return ra->xefi_group->xg_gno - rb->xefi_group->xg_gno;
391 }
392 
393 /* Log a free extent to the intent item. */
394 STATIC void
395 xfs_extent_free_log_item(
396 	struct xfs_trans		*tp,
397 	struct xfs_efi_log_item		*efip,
398 	struct xfs_extent_free_item	*xefi)
399 {
400 	uint				next_extent;
401 	struct xfs_extent		*extp;
402 
403 	/*
404 	 * atomic_inc_return gives us the value after the increment;
405 	 * we want to use it as an array index so we need to subtract 1 from
406 	 * it.
407 	 */
408 	next_extent = atomic_inc_return(&efip->efi_next_extent) - 1;
409 	ASSERT(next_extent < efip->efi_format.efi_nextents);
410 	extp = &efip->efi_format.efi_extents[next_extent];
411 	extp->ext_start = xefi->xefi_startblock;
412 	extp->ext_len = xefi->xefi_blockcount;
413 }
414 
415 static struct xfs_log_item *
416 __xfs_extent_free_create_intent(
417 	struct xfs_trans		*tp,
418 	struct list_head		*items,
419 	unsigned int			count,
420 	bool				sort,
421 	unsigned short			item_type)
422 {
423 	struct xfs_mount		*mp = tp->t_mountp;
424 	struct xfs_efi_log_item		*efip;
425 	struct xfs_extent_free_item	*xefi;
426 
427 	ASSERT(count > 0);
428 
429 	efip = xfs_efi_init(mp, item_type, count);
430 	if (sort)
431 		list_sort(mp, items, xfs_extent_free_diff_items);
432 	list_for_each_entry(xefi, items, xefi_list)
433 		xfs_extent_free_log_item(tp, efip, xefi);
434 	return &efip->efi_item;
435 }
436 
437 static struct xfs_log_item *
438 xfs_extent_free_create_intent(
439 	struct xfs_trans		*tp,
440 	struct list_head		*items,
441 	unsigned int			count,
442 	bool				sort)
443 {
444 	return __xfs_extent_free_create_intent(tp, items, count, sort,
445 			XFS_LI_EFI);
446 }
447 
448 static inline unsigned short
449 xfs_efd_type_from_efi(const struct xfs_efi_log_item *efip)
450 {
451 	return xfs_efi_item_isrt(&efip->efi_item) ?  XFS_LI_EFD_RT : XFS_LI_EFD;
452 }
453 
454 /* Get an EFD so we can process all the free extents. */
455 static struct xfs_log_item *
456 xfs_extent_free_create_done(
457 	struct xfs_trans		*tp,
458 	struct xfs_log_item		*intent,
459 	unsigned int			count)
460 {
461 	struct xfs_efi_log_item		*efip = EFI_ITEM(intent);
462 	struct xfs_efd_log_item		*efdp;
463 
464 	ASSERT(count > 0);
465 
466 	if (count > XFS_EFD_MAX_FAST_EXTENTS) {
467 		efdp = kzalloc(xfs_efd_log_item_sizeof(count),
468 				GFP_KERNEL | __GFP_NOFAIL);
469 	} else {
470 		efdp = kmem_cache_zalloc(xfs_efd_cache,
471 					GFP_KERNEL | __GFP_NOFAIL);
472 	}
473 
474 	xfs_log_item_init(tp->t_mountp, &efdp->efd_item,
475 			xfs_efd_type_from_efi(efip), &xfs_efd_item_ops);
476 	efdp->efd_efip = efip;
477 	efdp->efd_format.efd_nextents = count;
478 	efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;
479 
480 	return &efdp->efd_item;
481 }
482 
483 static inline const struct xfs_defer_op_type *
484 xefi_ops(
485 	struct xfs_extent_free_item	*xefi)
486 {
487 	if (xfs_efi_is_realtime(xefi))
488 		return &xfs_rtextent_free_defer_type;
489 	if (xefi->xefi_agresv == XFS_AG_RESV_AGFL)
490 		return &xfs_agfl_free_defer_type;
491 	return &xfs_extent_free_defer_type;
492 }
493 
494 /* Add this deferred EFI to the transaction. */
495 void
496 xfs_extent_free_defer_add(
497 	struct xfs_trans		*tp,
498 	struct xfs_extent_free_item	*xefi,
499 	struct xfs_defer_pending	**dfpp)
500 {
501 	struct xfs_mount		*mp = tp->t_mountp;
502 
503 	xefi->xefi_group = xfs_group_intent_get(mp, xefi->xefi_startblock,
504 			xfs_efi_is_realtime(xefi) ? XG_TYPE_RTG : XG_TYPE_AG);
505 
506 	trace_xfs_extent_free_defer(mp, xefi);
507 	*dfpp = xfs_defer_add(tp, &xefi->xefi_list, xefi_ops(xefi));
508 }
509 
510 /* Cancel a free extent. */
511 STATIC void
512 xfs_extent_free_cancel_item(
513 	struct list_head		*item)
514 {
515 	struct xfs_extent_free_item	*xefi = xefi_entry(item);
516 
517 	xfs_group_intent_put(xefi->xefi_group);
518 	kmem_cache_free(xfs_extfree_item_cache, xefi);
519 }
520 
521 /* Process a free extent. */
522 STATIC int
523 xfs_extent_free_finish_item(
524 	struct xfs_trans		*tp,
525 	struct xfs_log_item		*done,
526 	struct list_head		*item,
527 	struct xfs_btree_cur		**state)
528 {
529 	struct xfs_owner_info		oinfo = { };
530 	struct xfs_extent_free_item	*xefi = xefi_entry(item);
531 	struct xfs_efd_log_item		*efdp = EFD_ITEM(done);
532 	struct xfs_mount		*mp = tp->t_mountp;
533 	xfs_agblock_t			agbno;
534 	int				error = 0;
535 
536 	agbno = XFS_FSB_TO_AGBNO(mp, xefi->xefi_startblock);
537 
538 	oinfo.oi_owner = xefi->xefi_owner;
539 	if (xefi->xefi_flags & XFS_EFI_ATTR_FORK)
540 		oinfo.oi_flags |= XFS_OWNER_INFO_ATTR_FORK;
541 	if (xefi->xefi_flags & XFS_EFI_BMBT_BLOCK)
542 		oinfo.oi_flags |= XFS_OWNER_INFO_BMBT_BLOCK;
543 
544 	trace_xfs_extent_free_deferred(mp, xefi);
545 
546 	/*
547 	 * If we need a new transaction to make progress, the caller will log a
548 	 * new EFI with the current contents. It will also log an EFD to cancel
549 	 * the existing EFI, and so we need to copy all the unprocessed extents
550 	 * in this EFI to the EFD so this works correctly.
551 	 */
552 	if (!(xefi->xefi_flags & XFS_EFI_CANCELLED))
553 		error = __xfs_free_extent(tp, to_perag(xefi->xefi_group), agbno,
554 				xefi->xefi_blockcount, &oinfo, xefi->xefi_agresv,
555 				xefi->xefi_flags & XFS_EFI_SKIP_DISCARD);
556 	if (error == -EAGAIN) {
557 		xfs_efd_from_efi(efdp);
558 		return error;
559 	}
560 
561 	xfs_efd_add_extent(efdp, xefi);
562 	xfs_extent_free_cancel_item(item);
563 	return error;
564 }
565 
566 /* Abort all pending EFIs. */
567 STATIC void
568 xfs_extent_free_abort_intent(
569 	struct xfs_log_item		*intent)
570 {
571 	xfs_efi_release(EFI_ITEM(intent));
572 }
573 
574 /*
575  * AGFL blocks are accounted differently in the reserve pools and are not
576  * inserted into the busy extent list.
577  */
578 STATIC int
579 xfs_agfl_free_finish_item(
580 	struct xfs_trans		*tp,
581 	struct xfs_log_item		*done,
582 	struct list_head		*item,
583 	struct xfs_btree_cur		**state)
584 {
585 	struct xfs_owner_info		oinfo = { };
586 	struct xfs_mount		*mp = tp->t_mountp;
587 	struct xfs_efd_log_item		*efdp = EFD_ITEM(done);
588 	struct xfs_extent_free_item	*xefi = xefi_entry(item);
589 	struct xfs_buf			*agbp;
590 	int				error;
591 	xfs_agblock_t			agbno;
592 
593 	ASSERT(xefi->xefi_blockcount == 1);
594 	agbno = XFS_FSB_TO_AGBNO(mp, xefi->xefi_startblock);
595 	oinfo.oi_owner = xefi->xefi_owner;
596 
597 	trace_xfs_agfl_free_deferred(mp, xefi);
598 
599 	error = xfs_alloc_read_agf(to_perag(xefi->xefi_group), tp, 0, &agbp);
600 	if (!error)
601 		error = xfs_free_ag_extent(tp, agbp, agbno, 1, &oinfo,
602 				XFS_AG_RESV_AGFL);
603 
604 	xfs_efd_add_extent(efdp, xefi);
605 	xfs_extent_free_cancel_item(&xefi->xefi_list);
606 	return error;
607 }
608 
609 /* Is this recovered EFI ok? */
610 static inline bool
611 xfs_efi_validate_ext(
612 	struct xfs_mount		*mp,
613 	bool				isrt,
614 	struct xfs_extent		*extp)
615 {
616 	if (isrt)
617 		return xfs_verify_rtbext(mp, extp->ext_start, extp->ext_len);
618 
619 	return xfs_verify_fsbext(mp, extp->ext_start, extp->ext_len);
620 }
621 
622 static inline void
623 xfs_efi_recover_work(
624 	struct xfs_mount		*mp,
625 	struct xfs_defer_pending	*dfp,
626 	bool				isrt,
627 	struct xfs_extent		*extp)
628 {
629 	struct xfs_extent_free_item	*xefi;
630 
631 	xefi = kmem_cache_zalloc(xfs_extfree_item_cache,
632 			       GFP_KERNEL | __GFP_NOFAIL);
633 	xefi->xefi_startblock = extp->ext_start;
634 	xefi->xefi_blockcount = extp->ext_len;
635 	xefi->xefi_agresv = XFS_AG_RESV_NONE;
636 	xefi->xefi_owner = XFS_RMAP_OWN_UNKNOWN;
637 	xefi->xefi_group = xfs_group_intent_get(mp, extp->ext_start,
638 			isrt ? XG_TYPE_RTG : XG_TYPE_AG);
639 	if (isrt)
640 		xefi->xefi_flags |= XFS_EFI_REALTIME;
641 
642 	xfs_defer_add_item(dfp, &xefi->xefi_list);
643 }
644 
645 /*
646  * Process an extent free intent item that was recovered from
647  * the log.  We need to free the extents that it describes.
648  */
649 STATIC int
650 xfs_extent_free_recover_work(
651 	struct xfs_defer_pending	*dfp,
652 	struct list_head		*capture_list)
653 {
654 	struct xfs_trans_res		resv;
655 	struct xfs_log_item		*lip = dfp->dfp_intent;
656 	struct xfs_efi_log_item		*efip = EFI_ITEM(lip);
657 	struct xfs_mount		*mp = lip->li_log->l_mp;
658 	struct xfs_trans		*tp;
659 	int				i;
660 	int				error = 0;
661 	bool				isrt = xfs_efi_item_isrt(lip);
662 
663 	/*
664 	 * First check the validity of the extents described by the EFI.  If
665 	 * any are bad, then assume that all are bad and just toss the EFI.
666 	 * Mixing RT and non-RT extents in the same EFI item is not allowed.
667 	 */
668 	for (i = 0; i < efip->efi_format.efi_nextents; i++) {
669 		if (!xfs_efi_validate_ext(mp, isrt,
670 					&efip->efi_format.efi_extents[i])) {
671 			XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
672 					&efip->efi_format,
673 					sizeof(efip->efi_format));
674 			return -EFSCORRUPTED;
675 		}
676 
677 		xfs_efi_recover_work(mp, dfp, isrt,
678 				&efip->efi_format.efi_extents[i]);
679 	}
680 
681 	resv = xlog_recover_resv(&M_RES(mp)->tr_itruncate);
682 	error = xfs_trans_alloc(mp, &resv, 0, 0, 0, &tp);
683 	if (error)
684 		return error;
685 
686 	error = xlog_recover_finish_intent(tp, dfp);
687 	if (error == -EFSCORRUPTED)
688 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
689 				&efip->efi_format,
690 				sizeof(efip->efi_format));
691 	if (error)
692 		goto abort_error;
693 
694 	return xfs_defer_ops_capture_and_commit(tp, capture_list);
695 
696 abort_error:
697 	xfs_trans_cancel(tp);
698 	return error;
699 }
700 
701 /* Relog an intent item to push the log tail forward. */
702 static struct xfs_log_item *
703 xfs_extent_free_relog_intent(
704 	struct xfs_trans		*tp,
705 	struct xfs_log_item		*intent,
706 	struct xfs_log_item		*done_item)
707 {
708 	struct xfs_efd_log_item		*efdp = EFD_ITEM(done_item);
709 	struct xfs_efi_log_item		*efip;
710 	struct xfs_extent		*extp;
711 	unsigned int			count;
712 
713 	count = EFI_ITEM(intent)->efi_format.efi_nextents;
714 	extp = EFI_ITEM(intent)->efi_format.efi_extents;
715 
716 	ASSERT(intent->li_type == XFS_LI_EFI || intent->li_type == XFS_LI_EFI_RT);
717 
718 	efdp->efd_next_extent = count;
719 	memcpy(efdp->efd_format.efd_extents, extp, count * sizeof(*extp));
720 
721 	efip = xfs_efi_init(tp->t_mountp, intent->li_type, count);
722 	memcpy(efip->efi_format.efi_extents, extp, count * sizeof(*extp));
723 	atomic_set(&efip->efi_next_extent, count);
724 
725 	return &efip->efi_item;
726 }
727 
728 const struct xfs_defer_op_type xfs_extent_free_defer_type = {
729 	.name		= "extent_free",
730 	.max_items	= XFS_EFI_MAX_FAST_EXTENTS,
731 	.create_intent	= xfs_extent_free_create_intent,
732 	.abort_intent	= xfs_extent_free_abort_intent,
733 	.create_done	= xfs_extent_free_create_done,
734 	.finish_item	= xfs_extent_free_finish_item,
735 	.cancel_item	= xfs_extent_free_cancel_item,
736 	.recover_work	= xfs_extent_free_recover_work,
737 	.relog_intent	= xfs_extent_free_relog_intent,
738 };
739 
740 /* sub-type with special handling for AGFL deferred frees */
741 const struct xfs_defer_op_type xfs_agfl_free_defer_type = {
742 	.name		= "agfl_free",
743 	.max_items	= XFS_EFI_MAX_FAST_EXTENTS,
744 	.create_intent	= xfs_extent_free_create_intent,
745 	.abort_intent	= xfs_extent_free_abort_intent,
746 	.create_done	= xfs_extent_free_create_done,
747 	.finish_item	= xfs_agfl_free_finish_item,
748 	.cancel_item	= xfs_extent_free_cancel_item,
749 	.recover_work	= xfs_extent_free_recover_work,
750 	.relog_intent	= xfs_extent_free_relog_intent,
751 };
752 
753 #ifdef CONFIG_XFS_RT
754 /* Create a realtime extent freeing */
755 static struct xfs_log_item *
756 xfs_rtextent_free_create_intent(
757 	struct xfs_trans		*tp,
758 	struct list_head		*items,
759 	unsigned int			count,
760 	bool				sort)
761 {
762 	return __xfs_extent_free_create_intent(tp, items, count, sort,
763 			XFS_LI_EFI_RT);
764 }
765 
766 /* Process a free realtime extent. */
767 STATIC int
768 xfs_rtextent_free_finish_item(
769 	struct xfs_trans		*tp,
770 	struct xfs_log_item		*done,
771 	struct list_head		*item,
772 	struct xfs_btree_cur		**state)
773 {
774 	struct xfs_mount		*mp = tp->t_mountp;
775 	struct xfs_extent_free_item	*xefi = xefi_entry(item);
776 	struct xfs_efd_log_item		*efdp = EFD_ITEM(done);
777 	struct xfs_rtgroup		**rtgp = (struct xfs_rtgroup **)state;
778 	int				error = 0;
779 
780 	trace_xfs_extent_free_deferred(mp, xefi);
781 
782 	if (xefi->xefi_flags & XFS_EFI_CANCELLED)
783 		goto done;
784 
785 	if (*rtgp != to_rtg(xefi->xefi_group)) {
786 		unsigned int		lock_flags;
787 
788 		if (xfs_has_zoned(mp))
789 			lock_flags = XFS_RTGLOCK_RMAP;
790 		else
791 			lock_flags = XFS_RTGLOCK_BITMAP;
792 
793 		*rtgp = to_rtg(xefi->xefi_group);
794 		xfs_rtgroup_lock(*rtgp, lock_flags);
795 		xfs_rtgroup_trans_join(tp, *rtgp, lock_flags);
796 	}
797 
798 	if (xfs_has_zoned(mp)) {
799 		error = xfs_zone_free_blocks(tp, *rtgp, xefi->xefi_startblock,
800 				xefi->xefi_blockcount);
801 	} else {
802 		error = xfs_rtfree_blocks(tp, *rtgp, xefi->xefi_startblock,
803 				xefi->xefi_blockcount);
804 	}
805 
806 	if (error == -EAGAIN) {
807 		xfs_efd_from_efi(efdp);
808 		return error;
809 	}
810 done:
811 	xfs_efd_add_extent(efdp, xefi);
812 	xfs_extent_free_cancel_item(item);
813 	return error;
814 }
815 
816 const struct xfs_defer_op_type xfs_rtextent_free_defer_type = {
817 	.name		= "rtextent_free",
818 	.max_items	= XFS_EFI_MAX_FAST_EXTENTS,
819 	.create_intent	= xfs_rtextent_free_create_intent,
820 	.abort_intent	= xfs_extent_free_abort_intent,
821 	.create_done	= xfs_extent_free_create_done,
822 	.finish_item	= xfs_rtextent_free_finish_item,
823 	.cancel_item	= xfs_extent_free_cancel_item,
824 	.recover_work	= xfs_extent_free_recover_work,
825 	.relog_intent	= xfs_extent_free_relog_intent,
826 };
827 #else
828 const struct xfs_defer_op_type xfs_rtextent_free_defer_type = {
829 	.name		= "rtextent_free",
830 };
831 #endif /* CONFIG_XFS_RT */
832 
833 STATIC bool
834 xfs_efi_item_match(
835 	struct xfs_log_item	*lip,
836 	uint64_t		intent_id)
837 {
838 	return EFI_ITEM(lip)->efi_format.efi_id == intent_id;
839 }
840 
841 static const struct xfs_item_ops xfs_efi_item_ops = {
842 	.flags		= XFS_ITEM_INTENT,
843 	.iop_size	= xfs_efi_item_size,
844 	.iop_format	= xfs_efi_item_format,
845 	.iop_unpin	= xfs_efi_item_unpin,
846 	.iop_release	= xfs_efi_item_release,
847 	.iop_match	= xfs_efi_item_match,
848 };
849 
850 /*
851  * This routine is called to create an in-core extent free intent
852  * item from the efi format structure which was logged on disk.
853  * It allocates an in-core efi, copies the extents from the format
854  * structure into it, and adds the efi to the AIL with the given
855  * LSN.
856  */
857 STATIC int
858 xlog_recover_efi_commit_pass2(
859 	struct xlog			*log,
860 	struct list_head		*buffer_list,
861 	struct xlog_recover_item	*item,
862 	xfs_lsn_t			lsn)
863 {
864 	struct xfs_mount		*mp = log->l_mp;
865 	struct xfs_efi_log_item		*efip;
866 	struct xfs_efi_log_format	*efi_formatp;
867 	int				error;
868 
869 	efi_formatp = item->ri_buf[0].iov_base;
870 
871 	if (item->ri_buf[0].iov_len < xfs_efi_log_format_sizeof(0)) {
872 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
873 				item->ri_buf[0].iov_base, item->ri_buf[0].iov_len);
874 		return -EFSCORRUPTED;
875 	}
876 
877 	efip = xfs_efi_init(mp, ITEM_TYPE(item), efi_formatp->efi_nextents);
878 	error = xfs_efi_copy_format(&item->ri_buf[0], &efip->efi_format);
879 	if (error) {
880 		xfs_efi_item_free(efip);
881 		return error;
882 	}
883 	atomic_set(&efip->efi_next_extent, efi_formatp->efi_nextents);
884 
885 	xlog_recover_intent_item(log, &efip->efi_item, lsn,
886 			&xfs_extent_free_defer_type);
887 	return 0;
888 }
889 
890 const struct xlog_recover_item_ops xlog_efi_item_ops = {
891 	.item_type		= XFS_LI_EFI,
892 	.commit_pass2		= xlog_recover_efi_commit_pass2,
893 };
894 
895 #ifdef CONFIG_XFS_RT
896 STATIC int
897 xlog_recover_rtefi_commit_pass2(
898 	struct xlog			*log,
899 	struct list_head		*buffer_list,
900 	struct xlog_recover_item	*item,
901 	xfs_lsn_t			lsn)
902 {
903 	struct xfs_mount		*mp = log->l_mp;
904 	struct xfs_efi_log_item		*efip;
905 	struct xfs_efi_log_format	*efi_formatp;
906 	int				error;
907 
908 	efi_formatp = item->ri_buf[0].iov_base;
909 
910 	if (item->ri_buf[0].iov_len < xfs_efi_log_format_sizeof(0)) {
911 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
912 				item->ri_buf[0].iov_base, item->ri_buf[0].iov_len);
913 		return -EFSCORRUPTED;
914 	}
915 
916 	efip = xfs_efi_init(mp, ITEM_TYPE(item), efi_formatp->efi_nextents);
917 	error = xfs_efi_copy_format(&item->ri_buf[0], &efip->efi_format);
918 	if (error) {
919 		xfs_efi_item_free(efip);
920 		return error;
921 	}
922 	atomic_set(&efip->efi_next_extent, efi_formatp->efi_nextents);
923 
924 	xlog_recover_intent_item(log, &efip->efi_item, lsn,
925 			&xfs_rtextent_free_defer_type);
926 	return 0;
927 }
928 #else
929 STATIC int
930 xlog_recover_rtefi_commit_pass2(
931 	struct xlog			*log,
932 	struct list_head		*buffer_list,
933 	struct xlog_recover_item	*item,
934 	xfs_lsn_t			lsn)
935 {
936 	XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
937 			item->ri_buf[0].iov_base, item->ri_buf[0].iov_len);
938 	return -EFSCORRUPTED;
939 }
940 #endif
941 
942 const struct xlog_recover_item_ops xlog_rtefi_item_ops = {
943 	.item_type		= XFS_LI_EFI_RT,
944 	.commit_pass2		= xlog_recover_rtefi_commit_pass2,
945 };
946 
947 /*
948  * This routine is called when an EFD format structure is found in a committed
949  * transaction in the log. Its purpose is to cancel the corresponding EFI if it
950  * was still in the log. To do this it searches the AIL for the EFI with an id
951  * equal to that in the EFD format structure. If we find it we drop the EFD
952  * reference, which removes the EFI from the AIL and frees it.
953  */
954 STATIC int
955 xlog_recover_efd_commit_pass2(
956 	struct xlog			*log,
957 	struct list_head		*buffer_list,
958 	struct xlog_recover_item	*item,
959 	xfs_lsn_t			lsn)
960 {
961 	struct xfs_efd_log_format	*efd_formatp;
962 	int				buflen = item->ri_buf[0].iov_len;
963 
964 	efd_formatp = item->ri_buf[0].iov_base;
965 
966 	if (buflen < sizeof(struct xfs_efd_log_format)) {
967 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
968 				efd_formatp, buflen);
969 		return -EFSCORRUPTED;
970 	}
971 
972 	if (item->ri_buf[0].iov_len != xfs_efd_log_format32_sizeof(
973 						efd_formatp->efd_nextents) &&
974 	    item->ri_buf[0].iov_len != xfs_efd_log_format64_sizeof(
975 						efd_formatp->efd_nextents)) {
976 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
977 				efd_formatp, buflen);
978 		return -EFSCORRUPTED;
979 	}
980 
981 	xlog_recover_release_intent(log, XFS_LI_EFI, efd_formatp->efd_efi_id);
982 	return 0;
983 }
984 
985 const struct xlog_recover_item_ops xlog_efd_item_ops = {
986 	.item_type		= XFS_LI_EFD,
987 	.commit_pass2		= xlog_recover_efd_commit_pass2,
988 };
989 
990 #ifdef CONFIG_XFS_RT
991 STATIC int
992 xlog_recover_rtefd_commit_pass2(
993 	struct xlog			*log,
994 	struct list_head		*buffer_list,
995 	struct xlog_recover_item	*item,
996 	xfs_lsn_t			lsn)
997 {
998 	struct xfs_efd_log_format	*efd_formatp;
999 	int				buflen = item->ri_buf[0].iov_len;
1000 
1001 	efd_formatp = item->ri_buf[0].iov_base;
1002 
1003 	if (buflen < sizeof(struct xfs_efd_log_format)) {
1004 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
1005 				efd_formatp, buflen);
1006 		return -EFSCORRUPTED;
1007 	}
1008 
1009 	if (item->ri_buf[0].iov_len != xfs_efd_log_format32_sizeof(
1010 						efd_formatp->efd_nextents) &&
1011 	    item->ri_buf[0].iov_len != xfs_efd_log_format64_sizeof(
1012 						efd_formatp->efd_nextents)) {
1013 		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
1014 				efd_formatp, buflen);
1015 		return -EFSCORRUPTED;
1016 	}
1017 
1018 	xlog_recover_release_intent(log, XFS_LI_EFI_RT,
1019 			efd_formatp->efd_efi_id);
1020 	return 0;
1021 }
1022 #else
1023 # define xlog_recover_rtefd_commit_pass2	xlog_recover_rtefi_commit_pass2
1024 #endif
1025 
1026 const struct xlog_recover_item_ops xlog_rtefd_item_ops = {
1027 	.item_type		= XFS_LI_EFD_RT,
1028 	.commit_pass2		= xlog_recover_rtefd_commit_pass2,
1029 };
1030