Linux Audio

Check our new training course

Linux BSP development engineering services

Need help to port Linux and bootloaders to your hardware?
Loading...
v6.9.4
  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.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
 29struct kmem_cache	*xfs_efi_cache;
 30struct kmem_cache	*xfs_efd_cache;
 31
 32static const struct xfs_item_ops xfs_efi_item_ops;
 33
 34static inline struct xfs_efi_log_item *EFI_ITEM(struct xfs_log_item *lip)
 35{
 36	return container_of(lip, struct xfs_efi_log_item, efi_item);
 37}
 38
 39STATIC void
 40xfs_efi_item_free(
 41	struct xfs_efi_log_item	*efip)
 42{
 43	kvfree(efip->efi_item.li_lv_shadow);
 44	if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS)
 45		kfree(efip);
 46	else
 47		kmem_cache_free(xfs_efi_cache, efip);
 48}
 49
 50/*
 51 * Freeing the efi requires that we remove it from the AIL if it has already
 52 * been placed there. However, the EFI may not yet have been placed in the AIL
 53 * when called by xfs_efi_release() from EFD processing due to the ordering of
 54 * committed vs unpin operations in bulk insert operations. Hence the reference
 55 * count to ensure only the last caller frees the EFI.
 56 */
 57STATIC void
 58xfs_efi_release(
 59	struct xfs_efi_log_item	*efip)
 60{
 61	ASSERT(atomic_read(&efip->efi_refcount) > 0);
 62	if (!atomic_dec_and_test(&efip->efi_refcount))
 63		return;
 
 
 
 64
 65	xfs_trans_ail_delete(&efip->efi_item, 0);
 66	xfs_efi_item_free(efip);
 
 
 
 
 
 
 
 
 
 67}
 68
 69STATIC void
 70xfs_efi_item_size(
 71	struct xfs_log_item	*lip,
 72	int			*nvecs,
 73	int			*nbytes)
 74{
 75	struct xfs_efi_log_item	*efip = EFI_ITEM(lip);
 76
 77	*nvecs += 1;
 78	*nbytes += xfs_efi_log_format_sizeof(efip->efi_format.efi_nextents);
 79}
 80
 81/*
 82 * This is called to fill in the vector of log iovecs for the
 83 * given efi log item. We use only 1 iovec, and we point that
 84 * at the efi_log_format structure embedded in the efi item.
 85 * It is at this point that we assert that all of the extent
 86 * slots in the efi item have been filled.
 87 */
 88STATIC void
 89xfs_efi_item_format(
 90	struct xfs_log_item	*lip,
 91	struct xfs_log_vec	*lv)
 92{
 93	struct xfs_efi_log_item	*efip = EFI_ITEM(lip);
 94	struct xfs_log_iovec	*vecp = NULL;
 95
 96	ASSERT(atomic_read(&efip->efi_next_extent) ==
 97				efip->efi_format.efi_nextents);
 98
 99	efip->efi_format.efi_type = XFS_LI_EFI;
100	efip->efi_format.efi_size = 1;
101
102	xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFI_FORMAT,
103			&efip->efi_format,
104			xfs_efi_log_format_sizeof(efip->efi_format.efi_nextents));
105}
106
107
108/*
109 * The unpin operation is the last place an EFI is manipulated in the log. It is
110 * either inserted in the AIL or aborted in the event of a log I/O error. In
111 * either case, the EFI transaction has been successfully committed to make it
112 * this far. Therefore, we expect whoever committed the EFI to either construct
113 * and commit the EFD or drop the EFD's reference in the event of error. Simply
114 * drop the log's EFI reference now that the log is done with it.
115 */
116STATIC void
117xfs_efi_item_unpin(
118	struct xfs_log_item	*lip,
119	int			remove)
120{
121	struct xfs_efi_log_item	*efip = EFI_ITEM(lip);
122	xfs_efi_release(efip);
123}
124
125/*
126 * The EFI has been either committed or aborted if the transaction has been
127 * cancelled. If the transaction was cancelled, an EFD isn't going to be
128 * constructed and thus we free the EFI here directly.
129 */
130STATIC void
131xfs_efi_item_release(
132	struct xfs_log_item	*lip)
133{
134	xfs_efi_release(EFI_ITEM(lip));
135}
136
137/*
138 * Allocate and initialize an efi item with the given number of extents.
139 */
140STATIC struct xfs_efi_log_item *
141xfs_efi_init(
142	struct xfs_mount	*mp,
143	uint			nextents)
144
145{
146	struct xfs_efi_log_item	*efip;
 
147
148	ASSERT(nextents > 0);
149	if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
150		efip = kzalloc(xfs_efi_log_item_sizeof(nextents),
151				GFP_KERNEL | __GFP_NOFAIL);
 
152	} else {
153		efip = kmem_cache_zalloc(xfs_efi_cache,
154					 GFP_KERNEL | __GFP_NOFAIL);
155	}
156
157	xfs_log_item_init(mp, &efip->efi_item, XFS_LI_EFI, &xfs_efi_item_ops);
158	efip->efi_format.efi_nextents = nextents;
159	efip->efi_format.efi_id = (uintptr_t)(void *)efip;
160	atomic_set(&efip->efi_next_extent, 0);
161	atomic_set(&efip->efi_refcount, 2);
162
163	return efip;
164}
165
166/*
167 * Copy an EFI format buffer from the given buf, and into the destination
168 * EFI format structure.
169 * The given buffer can be in 32 bit or 64 bit form (which has different padding),
170 * one of which will be the native format for this kernel.
171 * It will handle the conversion of formats if necessary.
172 */
173STATIC int
174xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt)
175{
176	xfs_efi_log_format_t *src_efi_fmt = buf->i_addr;
177	uint i;
178	uint len = xfs_efi_log_format_sizeof(src_efi_fmt->efi_nextents);
179	uint len32 = xfs_efi_log_format32_sizeof(src_efi_fmt->efi_nextents);
180	uint len64 = xfs_efi_log_format64_sizeof(src_efi_fmt->efi_nextents);
 
 
 
181
182	if (buf->i_len == len) {
183		memcpy(dst_efi_fmt, src_efi_fmt,
184		       offsetof(struct xfs_efi_log_format, efi_extents));
185		for (i = 0; i < src_efi_fmt->efi_nextents; i++)
186			memcpy(&dst_efi_fmt->efi_extents[i],
187			       &src_efi_fmt->efi_extents[i],
188			       sizeof(struct xfs_extent));
189		return 0;
190	} else if (buf->i_len == len32) {
191		xfs_efi_log_format_32_t *src_efi_fmt_32 = buf->i_addr;
192
193		dst_efi_fmt->efi_type     = src_efi_fmt_32->efi_type;
194		dst_efi_fmt->efi_size     = src_efi_fmt_32->efi_size;
195		dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents;
196		dst_efi_fmt->efi_id       = src_efi_fmt_32->efi_id;
197		for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
198			dst_efi_fmt->efi_extents[i].ext_start =
199				src_efi_fmt_32->efi_extents[i].ext_start;
200			dst_efi_fmt->efi_extents[i].ext_len =
201				src_efi_fmt_32->efi_extents[i].ext_len;
202		}
203		return 0;
204	} else if (buf->i_len == len64) {
205		xfs_efi_log_format_64_t *src_efi_fmt_64 = buf->i_addr;
206
207		dst_efi_fmt->efi_type     = src_efi_fmt_64->efi_type;
208		dst_efi_fmt->efi_size     = src_efi_fmt_64->efi_size;
209		dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents;
210		dst_efi_fmt->efi_id       = src_efi_fmt_64->efi_id;
211		for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
212			dst_efi_fmt->efi_extents[i].ext_start =
213				src_efi_fmt_64->efi_extents[i].ext_start;
214			dst_efi_fmt->efi_extents[i].ext_len =
215				src_efi_fmt_64->efi_extents[i].ext_len;
216		}
217		return 0;
218	}
219	XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, NULL, buf->i_addr,
220			buf->i_len);
221	return -EFSCORRUPTED;
222}
223
224static inline struct xfs_efd_log_item *EFD_ITEM(struct xfs_log_item *lip)
225{
226	return container_of(lip, struct xfs_efd_log_item, efd_item);
227}
228
229STATIC void
230xfs_efd_item_free(struct xfs_efd_log_item *efdp)
231{
232	kvfree(efdp->efd_item.li_lv_shadow);
233	if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS)
234		kfree(efdp);
235	else
236		kmem_cache_free(xfs_efd_cache, efdp);
 
 
 
 
 
 
 
 
 
 
 
 
 
237}
238
239STATIC void
240xfs_efd_item_size(
241	struct xfs_log_item	*lip,
242	int			*nvecs,
243	int			*nbytes)
244{
245	struct xfs_efd_log_item	*efdp = EFD_ITEM(lip);
246
247	*nvecs += 1;
248	*nbytes += xfs_efd_log_format_sizeof(efdp->efd_format.efd_nextents);
249}
250
251/*
252 * This is called to fill in the vector of log iovecs for the
253 * given efd log item. We use only 1 iovec, and we point that
254 * at the efd_log_format structure embedded in the efd item.
255 * It is at this point that we assert that all of the extent
256 * slots in the efd item have been filled.
257 */
258STATIC void
259xfs_efd_item_format(
260	struct xfs_log_item	*lip,
261	struct xfs_log_vec	*lv)
262{
263	struct xfs_efd_log_item	*efdp = EFD_ITEM(lip);
264	struct xfs_log_iovec	*vecp = NULL;
265
266	ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
267
268	efdp->efd_format.efd_type = XFS_LI_EFD;
269	efdp->efd_format.efd_size = 1;
270
271	xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFD_FORMAT,
272			&efdp->efd_format,
273			xfs_efd_log_format_sizeof(efdp->efd_format.efd_nextents));
274}
275
276/*
277 * The EFD is either committed or aborted if the transaction is cancelled. If
278 * the transaction is cancelled, drop our reference to the EFI and free the EFD.
279 */
280STATIC void
281xfs_efd_item_release(
282	struct xfs_log_item	*lip)
283{
284	struct xfs_efd_log_item	*efdp = EFD_ITEM(lip);
285
286	xfs_efi_release(efdp->efd_efip);
287	xfs_efd_item_free(efdp);
288}
289
290static struct xfs_log_item *
291xfs_efd_item_intent(
292	struct xfs_log_item	*lip)
293{
294	return &EFD_ITEM(lip)->efd_efip->efi_item;
295}
296
297static const struct xfs_item_ops xfs_efd_item_ops = {
298	.flags		= XFS_ITEM_RELEASE_WHEN_COMMITTED |
299			  XFS_ITEM_INTENT_DONE,
300	.iop_size	= xfs_efd_item_size,
301	.iop_format	= xfs_efd_item_format,
302	.iop_release	= xfs_efd_item_release,
303	.iop_intent	= xfs_efd_item_intent,
304};
305
306/*
307 * Fill the EFD with all extents from the EFI when we need to roll the
308 * transaction and continue with a new EFI.
309 *
310 * This simply copies all the extents in the EFI to the EFD rather than make
311 * assumptions about which extents in the EFI have already been processed. We
312 * currently keep the xefi list in the same order as the EFI extent list, but
313 * that may not always be the case. Copying everything avoids leaving a landmine
314 * were we fail to cancel all the extents in an EFI if the xefi list is
315 * processed in a different order to the extents in the EFI.
316 */
317static void
318xfs_efd_from_efi(
319	struct xfs_efd_log_item	*efdp)
320{
321	struct xfs_efi_log_item *efip = efdp->efd_efip;
322	uint                    i;
323
324	ASSERT(efip->efi_format.efi_nextents > 0);
325	ASSERT(efdp->efd_next_extent < efip->efi_format.efi_nextents);
326
327	for (i = 0; i < efip->efi_format.efi_nextents; i++) {
328	       efdp->efd_format.efd_extents[i] =
329		       efip->efi_format.efi_extents[i];
 
 
 
 
330	}
331	efdp->efd_next_extent = efip->efi_format.efi_nextents;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
332}
333
334/* Sort bmap items by AG. */
335static int
336xfs_extent_free_diff_items(
337	void				*priv,
338	const struct list_head		*a,
339	const struct list_head		*b)
340{
 
341	struct xfs_extent_free_item	*ra;
342	struct xfs_extent_free_item	*rb;
343
344	ra = container_of(a, struct xfs_extent_free_item, xefi_list);
345	rb = container_of(b, struct xfs_extent_free_item, xefi_list);
346
347	return ra->xefi_pag->pag_agno - rb->xefi_pag->pag_agno;
348}
349
350/* Log a free extent to the intent item. */
351STATIC void
352xfs_extent_free_log_item(
353	struct xfs_trans		*tp,
354	struct xfs_efi_log_item		*efip,
355	struct xfs_extent_free_item	*xefi)
356{
357	uint				next_extent;
358	struct xfs_extent		*extp;
359
 
 
 
360	/*
361	 * atomic_inc_return gives us the value after the increment;
362	 * we want to use it as an array index so we need to subtract 1 from
363	 * it.
364	 */
365	next_extent = atomic_inc_return(&efip->efi_next_extent) - 1;
366	ASSERT(next_extent < efip->efi_format.efi_nextents);
367	extp = &efip->efi_format.efi_extents[next_extent];
368	extp->ext_start = xefi->xefi_startblock;
369	extp->ext_len = xefi->xefi_blockcount;
370}
371
372static struct xfs_log_item *
373xfs_extent_free_create_intent(
374	struct xfs_trans		*tp,
375	struct list_head		*items,
376	unsigned int			count,
377	bool				sort)
378{
379	struct xfs_mount		*mp = tp->t_mountp;
380	struct xfs_efi_log_item		*efip = xfs_efi_init(mp, count);
381	struct xfs_extent_free_item	*xefi;
382
383	ASSERT(count > 0);
384
 
385	if (sort)
386		list_sort(mp, items, xfs_extent_free_diff_items);
387	list_for_each_entry(xefi, items, xefi_list)
388		xfs_extent_free_log_item(tp, efip, xefi);
389	return &efip->efi_item;
390}
391
392/* Get an EFD so we can process all the free extents. */
393static struct xfs_log_item *
394xfs_extent_free_create_done(
395	struct xfs_trans		*tp,
396	struct xfs_log_item		*intent,
397	unsigned int			count)
398{
399	struct xfs_efi_log_item		*efip = EFI_ITEM(intent);
400	struct xfs_efd_log_item		*efdp;
401
402	ASSERT(count > 0);
403
404	if (count > XFS_EFD_MAX_FAST_EXTENTS) {
405		efdp = kzalloc(xfs_efd_log_item_sizeof(count),
406				GFP_KERNEL | __GFP_NOFAIL);
407	} else {
408		efdp = kmem_cache_zalloc(xfs_efd_cache,
409					GFP_KERNEL | __GFP_NOFAIL);
410	}
411
412	xfs_log_item_init(tp->t_mountp, &efdp->efd_item, XFS_LI_EFD,
413			  &xfs_efd_item_ops);
414	efdp->efd_efip = efip;
415	efdp->efd_format.efd_nextents = count;
416	efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;
417
418	return &efdp->efd_item;
419}
420
421/* Take a passive ref to the AG containing the space we're freeing. */
422void
423xfs_extent_free_get_group(
424	struct xfs_mount		*mp,
425	struct xfs_extent_free_item	*xefi)
426{
427	xfs_agnumber_t			agno;
428
429	agno = XFS_FSB_TO_AGNO(mp, xefi->xefi_startblock);
430	xefi->xefi_pag = xfs_perag_intent_get(mp, agno);
431}
432
433/* Release a passive AG ref after some freeing work. */
434static inline void
435xfs_extent_free_put_group(
436	struct xfs_extent_free_item	*xefi)
437{
438	xfs_perag_intent_put(xefi->xefi_pag);
439}
440
441/* Process a free extent. */
442STATIC int
443xfs_extent_free_finish_item(
444	struct xfs_trans		*tp,
445	struct xfs_log_item		*done,
446	struct list_head		*item,
447	struct xfs_btree_cur		**state)
448{
449	struct xfs_owner_info		oinfo = { };
450	struct xfs_extent_free_item	*xefi;
451	struct xfs_efd_log_item		*efdp = EFD_ITEM(done);
452	struct xfs_mount		*mp = tp->t_mountp;
453	struct xfs_extent		*extp;
454	uint				next_extent;
455	xfs_agblock_t			agbno;
456	int				error = 0;
457
458	xefi = container_of(item, struct xfs_extent_free_item, xefi_list);
459	agbno = XFS_FSB_TO_AGBNO(mp, xefi->xefi_startblock);
460
461	oinfo.oi_owner = xefi->xefi_owner;
462	if (xefi->xefi_flags & XFS_EFI_ATTR_FORK)
463		oinfo.oi_flags |= XFS_OWNER_INFO_ATTR_FORK;
464	if (xefi->xefi_flags & XFS_EFI_BMBT_BLOCK)
465		oinfo.oi_flags |= XFS_OWNER_INFO_BMBT_BLOCK;
466
467	trace_xfs_bmap_free_deferred(tp->t_mountp, xefi->xefi_pag->pag_agno, 0,
468			agbno, xefi->xefi_blockcount);
469
470	/*
471	 * If we need a new transaction to make progress, the caller will log a
472	 * new EFI with the current contents. It will also log an EFD to cancel
473	 * the existing EFI, and so we need to copy all the unprocessed extents
474	 * in this EFI to the EFD so this works correctly.
475	 */
476	if (!(xefi->xefi_flags & XFS_EFI_CANCELLED))
477		error = __xfs_free_extent(tp, xefi->xefi_pag, agbno,
478				xefi->xefi_blockcount, &oinfo, xefi->xefi_agresv,
479				xefi->xefi_flags & XFS_EFI_SKIP_DISCARD);
480	if (error == -EAGAIN) {
481		xfs_efd_from_efi(efdp);
482		return error;
483	}
484
485	/* Add the work we finished to the EFD, even though nobody uses that */
486	next_extent = efdp->efd_next_extent;
487	ASSERT(next_extent < efdp->efd_format.efd_nextents);
488	extp = &(efdp->efd_format.efd_extents[next_extent]);
489	extp->ext_start = xefi->xefi_startblock;
490	extp->ext_len = xefi->xefi_blockcount;
491	efdp->efd_next_extent++;
492
493	xfs_extent_free_put_group(xefi);
494	kmem_cache_free(xfs_extfree_item_cache, xefi);
 
 
 
 
495	return error;
496}
497
498/* Abort all pending EFIs. */
499STATIC void
500xfs_extent_free_abort_intent(
501	struct xfs_log_item		*intent)
502{
503	xfs_efi_release(EFI_ITEM(intent));
504}
505
506/* Cancel a free extent. */
507STATIC void
508xfs_extent_free_cancel_item(
509	struct list_head		*item)
510{
511	struct xfs_extent_free_item	*xefi;
512
513	xefi = container_of(item, struct xfs_extent_free_item, xefi_list);
514
515	xfs_extent_free_put_group(xefi);
516	kmem_cache_free(xfs_extfree_item_cache, xefi);
517}
518
 
 
 
 
 
 
 
 
 
519/*
520 * AGFL blocks are accounted differently in the reserve pools and are not
521 * inserted into the busy extent list.
522 */
523STATIC int
524xfs_agfl_free_finish_item(
525	struct xfs_trans		*tp,
526	struct xfs_log_item		*done,
527	struct list_head		*item,
528	struct xfs_btree_cur		**state)
529{
530	struct xfs_owner_info		oinfo = { };
531	struct xfs_mount		*mp = tp->t_mountp;
532	struct xfs_efd_log_item		*efdp = EFD_ITEM(done);
533	struct xfs_extent_free_item	*xefi;
534	struct xfs_extent		*extp;
535	struct xfs_buf			*agbp;
536	int				error;
 
537	xfs_agblock_t			agbno;
538	uint				next_extent;
539
540	xefi = container_of(item, struct xfs_extent_free_item, xefi_list);
541	ASSERT(xefi->xefi_blockcount == 1);
542	agbno = XFS_FSB_TO_AGBNO(mp, xefi->xefi_startblock);
543	oinfo.oi_owner = xefi->xefi_owner;
544
545	trace_xfs_agfl_free_deferred(mp, xefi->xefi_pag->pag_agno, 0, agbno,
546			xefi->xefi_blockcount);
547
548	error = xfs_alloc_read_agf(xefi->xefi_pag, tp, 0, &agbp);
549	if (!error)
550		error = xfs_free_agfl_block(tp, xefi->xefi_pag->pag_agno,
551				agbno, agbp, &oinfo);
 
 
 
 
 
 
 
 
 
 
552
553	next_extent = efdp->efd_next_extent;
554	ASSERT(next_extent < efdp->efd_format.efd_nextents);
555	extp = &(efdp->efd_format.efd_extents[next_extent]);
556	extp->ext_start = xefi->xefi_startblock;
557	extp->ext_len = xefi->xefi_blockcount;
558	efdp->efd_next_extent++;
559
560	xfs_extent_free_put_group(xefi);
561	kmem_cache_free(xfs_extfree_item_cache, xefi);
562	return error;
563}
564
 
 
 
 
 
 
 
 
 
 
565/* Is this recovered EFI ok? */
566static inline bool
567xfs_efi_validate_ext(
568	struct xfs_mount		*mp,
569	struct xfs_extent		*extp)
570{
571	return xfs_verify_fsbext(mp, extp->ext_start, extp->ext_len);
572}
573
574static inline void
575xfs_efi_recover_work(
576	struct xfs_mount		*mp,
577	struct xfs_defer_pending	*dfp,
578	struct xfs_extent		*extp)
579{
580	struct xfs_extent_free_item	*xefi;
581
582	xefi = kmem_cache_zalloc(xfs_extfree_item_cache,
583			       GFP_KERNEL | __GFP_NOFAIL);
584	xefi->xefi_startblock = extp->ext_start;
585	xefi->xefi_blockcount = extp->ext_len;
586	xefi->xefi_agresv = XFS_AG_RESV_NONE;
587	xefi->xefi_owner = XFS_RMAP_OWN_UNKNOWN;
588	xfs_extent_free_get_group(mp, xefi);
589
590	xfs_defer_add_item(dfp, &xefi->xefi_list);
591}
592
593/*
594 * Process an extent free intent item that was recovered from
595 * the log.  We need to free the extents that it describes.
596 */
597STATIC int
598xfs_extent_free_recover_work(
599	struct xfs_defer_pending	*dfp,
600	struct list_head		*capture_list)
601{
602	struct xfs_trans_res		resv;
603	struct xfs_log_item		*lip = dfp->dfp_intent;
604	struct xfs_efi_log_item		*efip = EFI_ITEM(lip);
605	struct xfs_mount		*mp = lip->li_log->l_mp;
 
606	struct xfs_trans		*tp;
 
607	int				i;
608	int				error = 0;
609
610	/*
611	 * First check the validity of the extents described by the
612	 * EFI.  If any are bad, then assume that all are bad and
613	 * just toss the EFI.
614	 */
615	for (i = 0; i < efip->efi_format.efi_nextents; i++) {
616		if (!xfs_efi_validate_ext(mp,
617					&efip->efi_format.efi_extents[i])) {
618			XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
619					&efip->efi_format,
620					sizeof(efip->efi_format));
621			return -EFSCORRUPTED;
622		}
623
624		xfs_efi_recover_work(mp, dfp, &efip->efi_format.efi_extents[i]);
625	}
626
627	resv = xlog_recover_resv(&M_RES(mp)->tr_itruncate);
628	error = xfs_trans_alloc(mp, &resv, 0, 0, 0, &tp);
629	if (error)
630		return error;
 
631
632	error = xlog_recover_finish_intent(tp, dfp);
633	if (error == -EFSCORRUPTED)
634		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
635				&efip->efi_format,
636				sizeof(efip->efi_format));
637	if (error)
638		goto abort_error;
 
 
639
640	return xfs_defer_ops_capture_and_commit(tp, capture_list);
641
642abort_error:
643	xfs_trans_cancel(tp);
644	return error;
645}
646
 
 
 
 
 
 
 
 
647/* Relog an intent item to push the log tail forward. */
648static struct xfs_log_item *
649xfs_extent_free_relog_intent(
650	struct xfs_trans		*tp,
651	struct xfs_log_item		*intent,
652	struct xfs_log_item		*done_item)
653{
654	struct xfs_efd_log_item		*efdp = EFD_ITEM(done_item);
655	struct xfs_efi_log_item		*efip;
656	struct xfs_extent		*extp;
657	unsigned int			count;
658
659	count = EFI_ITEM(intent)->efi_format.efi_nextents;
660	extp = EFI_ITEM(intent)->efi_format.efi_extents;
661
 
 
662	efdp->efd_next_extent = count;
663	memcpy(efdp->efd_format.efd_extents, extp, count * sizeof(*extp));
 
664
665	efip = xfs_efi_init(tp->t_mountp, count);
666	memcpy(efip->efi_format.efi_extents, extp, count * sizeof(*extp));
667	atomic_set(&efip->efi_next_extent, count);
668
 
669	return &efip->efi_item;
670}
671
672const struct xfs_defer_op_type xfs_extent_free_defer_type = {
673	.name		= "extent_free",
674	.max_items	= XFS_EFI_MAX_FAST_EXTENTS,
675	.create_intent	= xfs_extent_free_create_intent,
676	.abort_intent	= xfs_extent_free_abort_intent,
677	.create_done	= xfs_extent_free_create_done,
678	.finish_item	= xfs_extent_free_finish_item,
679	.cancel_item	= xfs_extent_free_cancel_item,
680	.recover_work	= xfs_extent_free_recover_work,
681	.relog_intent	= xfs_extent_free_relog_intent,
682};
683
684/* sub-type with special handling for AGFL deferred frees */
685const struct xfs_defer_op_type xfs_agfl_free_defer_type = {
686	.name		= "agfl_free",
687	.max_items	= XFS_EFI_MAX_FAST_EXTENTS,
688	.create_intent	= xfs_extent_free_create_intent,
689	.abort_intent	= xfs_extent_free_abort_intent,
690	.create_done	= xfs_extent_free_create_done,
691	.finish_item	= xfs_agfl_free_finish_item,
692	.cancel_item	= xfs_extent_free_cancel_item,
693	.recover_work	= xfs_extent_free_recover_work,
694	.relog_intent	= xfs_extent_free_relog_intent,
695};
696
697STATIC bool
698xfs_efi_item_match(
699	struct xfs_log_item	*lip,
700	uint64_t		intent_id)
701{
702	return EFI_ITEM(lip)->efi_format.efi_id == intent_id;
703}
704
705static const struct xfs_item_ops xfs_efi_item_ops = {
706	.flags		= XFS_ITEM_INTENT,
707	.iop_size	= xfs_efi_item_size,
708	.iop_format	= xfs_efi_item_format,
709	.iop_unpin	= xfs_efi_item_unpin,
710	.iop_release	= xfs_efi_item_release,
 
711	.iop_match	= xfs_efi_item_match,
 
712};
713
714/*
715 * This routine is called to create an in-core extent free intent
716 * item from the efi format structure which was logged on disk.
717 * It allocates an in-core efi, copies the extents from the format
718 * structure into it, and adds the efi to the AIL with the given
719 * LSN.
720 */
721STATIC int
722xlog_recover_efi_commit_pass2(
723	struct xlog			*log,
724	struct list_head		*buffer_list,
725	struct xlog_recover_item	*item,
726	xfs_lsn_t			lsn)
727{
728	struct xfs_mount		*mp = log->l_mp;
729	struct xfs_efi_log_item		*efip;
730	struct xfs_efi_log_format	*efi_formatp;
731	int				error;
732
733	efi_formatp = item->ri_buf[0].i_addr;
734
735	if (item->ri_buf[0].i_len < xfs_efi_log_format_sizeof(0)) {
736		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
737				item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
738		return -EFSCORRUPTED;
739	}
740
741	efip = xfs_efi_init(mp, efi_formatp->efi_nextents);
742	error = xfs_efi_copy_format(&item->ri_buf[0], &efip->efi_format);
743	if (error) {
744		xfs_efi_item_free(efip);
745		return error;
746	}
747	atomic_set(&efip->efi_next_extent, efi_formatp->efi_nextents);
748
749	xlog_recover_intent_item(log, &efip->efi_item, lsn,
750			&xfs_extent_free_defer_type);
 
 
 
751	return 0;
752}
753
754const struct xlog_recover_item_ops xlog_efi_item_ops = {
755	.item_type		= XFS_LI_EFI,
756	.commit_pass2		= xlog_recover_efi_commit_pass2,
757};
758
759/*
760 * This routine is called when an EFD format structure is found in a committed
761 * transaction in the log. Its purpose is to cancel the corresponding EFI if it
762 * was still in the log. To do this it searches the AIL for the EFI with an id
763 * equal to that in the EFD format structure. If we find it we drop the EFD
764 * reference, which removes the EFI from the AIL and frees it.
765 */
766STATIC int
767xlog_recover_efd_commit_pass2(
768	struct xlog			*log,
769	struct list_head		*buffer_list,
770	struct xlog_recover_item	*item,
771	xfs_lsn_t			lsn)
772{
773	struct xfs_efd_log_format	*efd_formatp;
774	int				buflen = item->ri_buf[0].i_len;
775
776	efd_formatp = item->ri_buf[0].i_addr;
777
778	if (buflen < sizeof(struct xfs_efd_log_format)) {
779		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
780				efd_formatp, buflen);
781		return -EFSCORRUPTED;
782	}
783
784	if (item->ri_buf[0].i_len != xfs_efd_log_format32_sizeof(
785						efd_formatp->efd_nextents) &&
786	    item->ri_buf[0].i_len != xfs_efd_log_format64_sizeof(
787						efd_formatp->efd_nextents)) {
788		XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
789				efd_formatp, buflen);
790		return -EFSCORRUPTED;
791	}
792
793	xlog_recover_release_intent(log, XFS_LI_EFI, efd_formatp->efd_efi_id);
794	return 0;
795}
796
797const struct xlog_recover_item_ops xlog_efd_item_ops = {
798	.item_type		= XFS_LI_EFD,
799	.commit_pass2		= xlog_recover_efd_commit_pass2,
800};
v5.14.15
  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.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_extfree_item.h"
 18#include "xfs_log.h"
 19#include "xfs_btree.h"
 20#include "xfs_rmap.h"
 21#include "xfs_alloc.h"
 22#include "xfs_bmap.h"
 23#include "xfs_trace.h"
 24#include "xfs_error.h"
 25#include "xfs_log_priv.h"
 26#include "xfs_log_recover.h"
 27
 28kmem_zone_t	*xfs_efi_zone;
 29kmem_zone_t	*xfs_efd_zone;
 30
 31static const struct xfs_item_ops xfs_efi_item_ops;
 32
 33static inline struct xfs_efi_log_item *EFI_ITEM(struct xfs_log_item *lip)
 34{
 35	return container_of(lip, struct xfs_efi_log_item, efi_item);
 36}
 37
 38STATIC void
 39xfs_efi_item_free(
 40	struct xfs_efi_log_item	*efip)
 41{
 42	kmem_free(efip->efi_item.li_lv_shadow);
 43	if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS)
 44		kmem_free(efip);
 45	else
 46		kmem_cache_free(xfs_efi_zone, efip);
 47}
 48
 49/*
 50 * Freeing the efi requires that we remove it from the AIL if it has already
 51 * been placed there. However, the EFI may not yet have been placed in the AIL
 52 * when called by xfs_efi_release() from EFD 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 EFI.
 55 */
 56STATIC void
 57xfs_efi_release(
 58	struct xfs_efi_log_item	*efip)
 59{
 60	ASSERT(atomic_read(&efip->efi_refcount) > 0);
 61	if (atomic_dec_and_test(&efip->efi_refcount)) {
 62		xfs_trans_ail_delete(&efip->efi_item, SHUTDOWN_LOG_IO_ERROR);
 63		xfs_efi_item_free(efip);
 64	}
 65}
 66
 67/*
 68 * This returns the number of iovecs needed to log the given efi item.
 69 * We only need 1 iovec for an efi item.  It just logs the efi_log_format
 70 * structure.
 71 */
 72static inline int
 73xfs_efi_item_sizeof(
 74	struct xfs_efi_log_item *efip)
 75{
 76	return sizeof(struct xfs_efi_log_format) +
 77	       (efip->efi_format.efi_nextents - 1) * sizeof(xfs_extent_t);
 78}
 79
 80STATIC void
 81xfs_efi_item_size(
 82	struct xfs_log_item	*lip,
 83	int			*nvecs,
 84	int			*nbytes)
 85{
 
 
 86	*nvecs += 1;
 87	*nbytes += xfs_efi_item_sizeof(EFI_ITEM(lip));
 88}
 89
 90/*
 91 * This is called to fill in the vector of log iovecs for the
 92 * given efi log item. We use only 1 iovec, and we point that
 93 * at the efi_log_format structure embedded in the efi item.
 94 * It is at this point that we assert that all of the extent
 95 * slots in the efi item have been filled.
 96 */
 97STATIC void
 98xfs_efi_item_format(
 99	struct xfs_log_item	*lip,
100	struct xfs_log_vec	*lv)
101{
102	struct xfs_efi_log_item	*efip = EFI_ITEM(lip);
103	struct xfs_log_iovec	*vecp = NULL;
104
105	ASSERT(atomic_read(&efip->efi_next_extent) ==
106				efip->efi_format.efi_nextents);
107
108	efip->efi_format.efi_type = XFS_LI_EFI;
109	efip->efi_format.efi_size = 1;
110
111	xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFI_FORMAT,
112			&efip->efi_format,
113			xfs_efi_item_sizeof(efip));
114}
115
116
117/*
118 * The unpin operation is the last place an EFI is manipulated in the log. It is
119 * either inserted in the AIL or aborted in the event of a log I/O error. In
120 * either case, the EFI transaction has been successfully committed to make it
121 * this far. Therefore, we expect whoever committed the EFI to either construct
122 * and commit the EFD or drop the EFD's reference in the event of error. Simply
123 * drop the log's EFI reference now that the log is done with it.
124 */
125STATIC void
126xfs_efi_item_unpin(
127	struct xfs_log_item	*lip,
128	int			remove)
129{
130	struct xfs_efi_log_item	*efip = EFI_ITEM(lip);
131	xfs_efi_release(efip);
132}
133
134/*
135 * The EFI has been either committed or aborted if the transaction has been
136 * cancelled. If the transaction was cancelled, an EFD isn't going to be
137 * constructed and thus we free the EFI here directly.
138 */
139STATIC void
140xfs_efi_item_release(
141	struct xfs_log_item	*lip)
142{
143	xfs_efi_release(EFI_ITEM(lip));
144}
145
146/*
147 * Allocate and initialize an efi item with the given number of extents.
148 */
149STATIC struct xfs_efi_log_item *
150xfs_efi_init(
151	struct xfs_mount	*mp,
152	uint			nextents)
153
154{
155	struct xfs_efi_log_item	*efip;
156	uint			size;
157
158	ASSERT(nextents > 0);
159	if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
160		size = (uint)(sizeof(struct xfs_efi_log_item) +
161			((nextents - 1) * sizeof(xfs_extent_t)));
162		efip = kmem_zalloc(size, 0);
163	} else {
164		efip = kmem_cache_zalloc(xfs_efi_zone,
165					 GFP_KERNEL | __GFP_NOFAIL);
166	}
167
168	xfs_log_item_init(mp, &efip->efi_item, XFS_LI_EFI, &xfs_efi_item_ops);
169	efip->efi_format.efi_nextents = nextents;
170	efip->efi_format.efi_id = (uintptr_t)(void *)efip;
171	atomic_set(&efip->efi_next_extent, 0);
172	atomic_set(&efip->efi_refcount, 2);
173
174	return efip;
175}
176
177/*
178 * Copy an EFI format buffer from the given buf, and into the destination
179 * EFI format structure.
180 * The given buffer can be in 32 bit or 64 bit form (which has different padding),
181 * one of which will be the native format for this kernel.
182 * It will handle the conversion of formats if necessary.
183 */
184STATIC int
185xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt)
186{
187	xfs_efi_log_format_t *src_efi_fmt = buf->i_addr;
188	uint i;
189	uint len = sizeof(xfs_efi_log_format_t) + 
190		(src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_t);  
191	uint len32 = sizeof(xfs_efi_log_format_32_t) + 
192		(src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_32_t);  
193	uint len64 = sizeof(xfs_efi_log_format_64_t) + 
194		(src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_64_t);  
195
196	if (buf->i_len == len) {
197		memcpy((char *)dst_efi_fmt, (char*)src_efi_fmt, len);
 
 
 
 
 
198		return 0;
199	} else if (buf->i_len == len32) {
200		xfs_efi_log_format_32_t *src_efi_fmt_32 = buf->i_addr;
201
202		dst_efi_fmt->efi_type     = src_efi_fmt_32->efi_type;
203		dst_efi_fmt->efi_size     = src_efi_fmt_32->efi_size;
204		dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents;
205		dst_efi_fmt->efi_id       = src_efi_fmt_32->efi_id;
206		for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
207			dst_efi_fmt->efi_extents[i].ext_start =
208				src_efi_fmt_32->efi_extents[i].ext_start;
209			dst_efi_fmt->efi_extents[i].ext_len =
210				src_efi_fmt_32->efi_extents[i].ext_len;
211		}
212		return 0;
213	} else if (buf->i_len == len64) {
214		xfs_efi_log_format_64_t *src_efi_fmt_64 = buf->i_addr;
215
216		dst_efi_fmt->efi_type     = src_efi_fmt_64->efi_type;
217		dst_efi_fmt->efi_size     = src_efi_fmt_64->efi_size;
218		dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents;
219		dst_efi_fmt->efi_id       = src_efi_fmt_64->efi_id;
220		for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
221			dst_efi_fmt->efi_extents[i].ext_start =
222				src_efi_fmt_64->efi_extents[i].ext_start;
223			dst_efi_fmt->efi_extents[i].ext_len =
224				src_efi_fmt_64->efi_extents[i].ext_len;
225		}
226		return 0;
227	}
228	XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL);
 
229	return -EFSCORRUPTED;
230}
231
232static inline struct xfs_efd_log_item *EFD_ITEM(struct xfs_log_item *lip)
233{
234	return container_of(lip, struct xfs_efd_log_item, efd_item);
235}
236
237STATIC void
238xfs_efd_item_free(struct xfs_efd_log_item *efdp)
239{
240	kmem_free(efdp->efd_item.li_lv_shadow);
241	if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS)
242		kmem_free(efdp);
243	else
244		kmem_cache_free(xfs_efd_zone, efdp);
245}
246
247/*
248 * This returns the number of iovecs needed to log the given efd item.
249 * We only need 1 iovec for an efd item.  It just logs the efd_log_format
250 * structure.
251 */
252static inline int
253xfs_efd_item_sizeof(
254	struct xfs_efd_log_item *efdp)
255{
256	return sizeof(xfs_efd_log_format_t) +
257	       (efdp->efd_format.efd_nextents - 1) * sizeof(xfs_extent_t);
258}
259
260STATIC void
261xfs_efd_item_size(
262	struct xfs_log_item	*lip,
263	int			*nvecs,
264	int			*nbytes)
265{
 
 
266	*nvecs += 1;
267	*nbytes += xfs_efd_item_sizeof(EFD_ITEM(lip));
268}
269
270/*
271 * This is called to fill in the vector of log iovecs for the
272 * given efd log item. We use only 1 iovec, and we point that
273 * at the efd_log_format structure embedded in the efd item.
274 * It is at this point that we assert that all of the extent
275 * slots in the efd item have been filled.
276 */
277STATIC void
278xfs_efd_item_format(
279	struct xfs_log_item	*lip,
280	struct xfs_log_vec	*lv)
281{
282	struct xfs_efd_log_item	*efdp = EFD_ITEM(lip);
283	struct xfs_log_iovec	*vecp = NULL;
284
285	ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
286
287	efdp->efd_format.efd_type = XFS_LI_EFD;
288	efdp->efd_format.efd_size = 1;
289
290	xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFD_FORMAT,
291			&efdp->efd_format,
292			xfs_efd_item_sizeof(efdp));
293}
294
295/*
296 * The EFD is either committed or aborted if the transaction is cancelled. If
297 * the transaction is cancelled, drop our reference to the EFI and free the EFD.
298 */
299STATIC void
300xfs_efd_item_release(
301	struct xfs_log_item	*lip)
302{
303	struct xfs_efd_log_item	*efdp = EFD_ITEM(lip);
304
305	xfs_efi_release(efdp->efd_efip);
306	xfs_efd_item_free(efdp);
307}
308
 
 
 
 
 
 
 
309static const struct xfs_item_ops xfs_efd_item_ops = {
310	.flags		= XFS_ITEM_RELEASE_WHEN_COMMITTED,
 
311	.iop_size	= xfs_efd_item_size,
312	.iop_format	= xfs_efd_item_format,
313	.iop_release	= xfs_efd_item_release,
 
314};
315
316/*
317 * Allocate an "extent free done" log item that will hold nextents worth of
318 * extents.  The caller must use all nextents extents, because we are not
319 * flexible about this at all.
320 */
321static struct xfs_efd_log_item *
322xfs_trans_get_efd(
323	struct xfs_trans		*tp,
324	struct xfs_efi_log_item		*efip,
325	unsigned int			nextents)
 
 
 
 
326{
327	struct xfs_efd_log_item		*efdp;
 
328
329	ASSERT(nextents > 0);
 
330
331	if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
332		efdp = kmem_zalloc(sizeof(struct xfs_efd_log_item) +
333				(nextents - 1) * sizeof(struct xfs_extent),
334				0);
335	} else {
336		efdp = kmem_cache_zalloc(xfs_efd_zone,
337					GFP_KERNEL | __GFP_NOFAIL);
338	}
339
340	xfs_log_item_init(tp->t_mountp, &efdp->efd_item, XFS_LI_EFD,
341			  &xfs_efd_item_ops);
342	efdp->efd_efip = efip;
343	efdp->efd_format.efd_nextents = nextents;
344	efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;
345
346	xfs_trans_add_item(tp, &efdp->efd_item);
347	return efdp;
348}
349
350/*
351 * Free an extent and log it to the EFD. Note that the transaction is marked
352 * dirty regardless of whether the extent free succeeds or fails to support the
353 * EFI/EFD lifecycle rules.
354 */
355static int
356xfs_trans_free_extent(
357	struct xfs_trans		*tp,
358	struct xfs_efd_log_item		*efdp,
359	xfs_fsblock_t			start_block,
360	xfs_extlen_t			ext_len,
361	const struct xfs_owner_info	*oinfo,
362	bool				skip_discard)
363{
364	struct xfs_mount		*mp = tp->t_mountp;
365	struct xfs_extent		*extp;
366	uint				next_extent;
367	xfs_agnumber_t			agno = XFS_FSB_TO_AGNO(mp, start_block);
368	xfs_agblock_t			agbno = XFS_FSB_TO_AGBNO(mp,
369								start_block);
370	int				error;
371
372	trace_xfs_bmap_free_deferred(tp->t_mountp, agno, 0, agbno, ext_len);
373
374	error = __xfs_free_extent(tp, start_block, ext_len,
375				  oinfo, XFS_AG_RESV_NONE, skip_discard);
376	/*
377	 * Mark the transaction dirty, even on error. This ensures the
378	 * transaction is aborted, which:
379	 *
380	 * 1.) releases the EFI and frees the EFD
381	 * 2.) shuts down the filesystem
382	 */
383	tp->t_flags |= XFS_TRANS_DIRTY;
384	set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
385
386	next_extent = efdp->efd_next_extent;
387	ASSERT(next_extent < efdp->efd_format.efd_nextents);
388	extp = &(efdp->efd_format.efd_extents[next_extent]);
389	extp->ext_start = start_block;
390	extp->ext_len = ext_len;
391	efdp->efd_next_extent++;
392
393	return error;
394}
395
396/* Sort bmap items by AG. */
397static int
398xfs_extent_free_diff_items(
399	void				*priv,
400	const struct list_head		*a,
401	const struct list_head		*b)
402{
403	struct xfs_mount		*mp = priv;
404	struct xfs_extent_free_item	*ra;
405	struct xfs_extent_free_item	*rb;
406
407	ra = container_of(a, struct xfs_extent_free_item, xefi_list);
408	rb = container_of(b, struct xfs_extent_free_item, xefi_list);
409	return  XFS_FSB_TO_AGNO(mp, ra->xefi_startblock) -
410		XFS_FSB_TO_AGNO(mp, rb->xefi_startblock);
411}
412
413/* Log a free extent to the intent item. */
414STATIC void
415xfs_extent_free_log_item(
416	struct xfs_trans		*tp,
417	struct xfs_efi_log_item		*efip,
418	struct xfs_extent_free_item	*free)
419{
420	uint				next_extent;
421	struct xfs_extent		*extp;
422
423	tp->t_flags |= XFS_TRANS_DIRTY;
424	set_bit(XFS_LI_DIRTY, &efip->efi_item.li_flags);
425
426	/*
427	 * atomic_inc_return gives us the value after the increment;
428	 * we want to use it as an array index so we need to subtract 1 from
429	 * it.
430	 */
431	next_extent = atomic_inc_return(&efip->efi_next_extent) - 1;
432	ASSERT(next_extent < efip->efi_format.efi_nextents);
433	extp = &efip->efi_format.efi_extents[next_extent];
434	extp->ext_start = free->xefi_startblock;
435	extp->ext_len = free->xefi_blockcount;
436}
437
438static struct xfs_log_item *
439xfs_extent_free_create_intent(
440	struct xfs_trans		*tp,
441	struct list_head		*items,
442	unsigned int			count,
443	bool				sort)
444{
445	struct xfs_mount		*mp = tp->t_mountp;
446	struct xfs_efi_log_item		*efip = xfs_efi_init(mp, count);
447	struct xfs_extent_free_item	*free;
448
449	ASSERT(count > 0);
450
451	xfs_trans_add_item(tp, &efip->efi_item);
452	if (sort)
453		list_sort(mp, items, xfs_extent_free_diff_items);
454	list_for_each_entry(free, items, xefi_list)
455		xfs_extent_free_log_item(tp, efip, free);
456	return &efip->efi_item;
457}
458
459/* Get an EFD so we can process all the free extents. */
460static struct xfs_log_item *
461xfs_extent_free_create_done(
462	struct xfs_trans		*tp,
463	struct xfs_log_item		*intent,
464	unsigned int			count)
465{
466	return &xfs_trans_get_efd(tp, EFI_ITEM(intent), count)->efd_item;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
467}
468
469/* Process a free extent. */
470STATIC int
471xfs_extent_free_finish_item(
472	struct xfs_trans		*tp,
473	struct xfs_log_item		*done,
474	struct list_head		*item,
475	struct xfs_btree_cur		**state)
476{
477	struct xfs_extent_free_item	*free;
478	int				error;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
479
480	free = container_of(item, struct xfs_extent_free_item, xefi_list);
481	error = xfs_trans_free_extent(tp, EFD_ITEM(done),
482			free->xefi_startblock,
483			free->xefi_blockcount,
484			&free->xefi_oinfo, free->xefi_skip_discard);
485	kmem_free(free);
486	return error;
487}
488
489/* Abort all pending EFIs. */
490STATIC void
491xfs_extent_free_abort_intent(
492	struct xfs_log_item		*intent)
493{
494	xfs_efi_release(EFI_ITEM(intent));
495}
496
497/* Cancel a free extent. */
498STATIC void
499xfs_extent_free_cancel_item(
500	struct list_head		*item)
501{
502	struct xfs_extent_free_item	*free;
503
504	free = container_of(item, struct xfs_extent_free_item, xefi_list);
505	kmem_free(free);
 
 
506}
507
508const struct xfs_defer_op_type xfs_extent_free_defer_type = {
509	.max_items	= XFS_EFI_MAX_FAST_EXTENTS,
510	.create_intent	= xfs_extent_free_create_intent,
511	.abort_intent	= xfs_extent_free_abort_intent,
512	.create_done	= xfs_extent_free_create_done,
513	.finish_item	= xfs_extent_free_finish_item,
514	.cancel_item	= xfs_extent_free_cancel_item,
515};
516
517/*
518 * AGFL blocks are accounted differently in the reserve pools and are not
519 * inserted into the busy extent list.
520 */
521STATIC int
522xfs_agfl_free_finish_item(
523	struct xfs_trans		*tp,
524	struct xfs_log_item		*done,
525	struct list_head		*item,
526	struct xfs_btree_cur		**state)
527{
 
528	struct xfs_mount		*mp = tp->t_mountp;
529	struct xfs_efd_log_item		*efdp = EFD_ITEM(done);
530	struct xfs_extent_free_item	*free;
531	struct xfs_extent		*extp;
532	struct xfs_buf			*agbp;
533	int				error;
534	xfs_agnumber_t			agno;
535	xfs_agblock_t			agbno;
536	uint				next_extent;
537
538	free = container_of(item, struct xfs_extent_free_item, xefi_list);
539	ASSERT(free->xefi_blockcount == 1);
540	agno = XFS_FSB_TO_AGNO(mp, free->xefi_startblock);
541	agbno = XFS_FSB_TO_AGBNO(mp, free->xefi_startblock);
542
543	trace_xfs_agfl_free_deferred(mp, agno, 0, agbno, free->xefi_blockcount);
 
544
545	error = xfs_alloc_read_agf(mp, tp, agno, 0, &agbp);
546	if (!error)
547		error = xfs_free_agfl_block(tp, agno, agbno, agbp,
548					    &free->xefi_oinfo);
549
550	/*
551	 * Mark the transaction dirty, even on error. This ensures the
552	 * transaction is aborted, which:
553	 *
554	 * 1.) releases the EFI and frees the EFD
555	 * 2.) shuts down the filesystem
556	 */
557	tp->t_flags |= XFS_TRANS_DIRTY;
558	set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
559
560	next_extent = efdp->efd_next_extent;
561	ASSERT(next_extent < efdp->efd_format.efd_nextents);
562	extp = &(efdp->efd_format.efd_extents[next_extent]);
563	extp->ext_start = free->xefi_startblock;
564	extp->ext_len = free->xefi_blockcount;
565	efdp->efd_next_extent++;
566
567	kmem_free(free);
 
568	return error;
569}
570
571/* sub-type with special handling for AGFL deferred frees */
572const struct xfs_defer_op_type xfs_agfl_free_defer_type = {
573	.max_items	= XFS_EFI_MAX_FAST_EXTENTS,
574	.create_intent	= xfs_extent_free_create_intent,
575	.abort_intent	= xfs_extent_free_abort_intent,
576	.create_done	= xfs_extent_free_create_done,
577	.finish_item	= xfs_agfl_free_finish_item,
578	.cancel_item	= xfs_extent_free_cancel_item,
579};
580
581/* Is this recovered EFI ok? */
582static inline bool
583xfs_efi_validate_ext(
584	struct xfs_mount		*mp,
585	struct xfs_extent		*extp)
586{
587	return xfs_verify_fsbext(mp, extp->ext_start, extp->ext_len);
588}
589
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
590/*
591 * Process an extent free intent item that was recovered from
592 * the log.  We need to free the extents that it describes.
593 */
594STATIC int
595xfs_efi_item_recover(
596	struct xfs_log_item		*lip,
597	struct list_head		*capture_list)
598{
 
 
599	struct xfs_efi_log_item		*efip = EFI_ITEM(lip);
600	struct xfs_mount		*mp = lip->li_mountp;
601	struct xfs_efd_log_item		*efdp;
602	struct xfs_trans		*tp;
603	struct xfs_extent		*extp;
604	int				i;
605	int				error = 0;
606
607	/*
608	 * First check the validity of the extents described by the
609	 * EFI.  If any are bad, then assume that all are bad and
610	 * just toss the EFI.
611	 */
612	for (i = 0; i < efip->efi_format.efi_nextents; i++) {
613		if (!xfs_efi_validate_ext(mp,
614					&efip->efi_format.efi_extents[i])) {
615			XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
616					&efip->efi_format,
617					sizeof(efip->efi_format));
618			return -EFSCORRUPTED;
619		}
 
 
620	}
621
622	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
 
623	if (error)
624		return error;
625	efdp = xfs_trans_get_efd(tp, efip, efip->efi_format.efi_nextents);
626
627	for (i = 0; i < efip->efi_format.efi_nextents; i++) {
628		extp = &efip->efi_format.efi_extents[i];
629		error = xfs_trans_free_extent(tp, efdp, extp->ext_start,
630					      extp->ext_len,
631					      &XFS_RMAP_OINFO_ANY_OWNER, false);
632		if (error)
633			goto abort_error;
634
635	}
636
637	return xfs_defer_ops_capture_and_commit(tp, NULL, capture_list);
638
639abort_error:
640	xfs_trans_cancel(tp);
641	return error;
642}
643
644STATIC bool
645xfs_efi_item_match(
646	struct xfs_log_item	*lip,
647	uint64_t		intent_id)
648{
649	return EFI_ITEM(lip)->efi_format.efi_id == intent_id;
650}
651
652/* Relog an intent item to push the log tail forward. */
653static struct xfs_log_item *
654xfs_efi_item_relog(
 
655	struct xfs_log_item		*intent,
656	struct xfs_trans		*tp)
657{
658	struct xfs_efd_log_item		*efdp;
659	struct xfs_efi_log_item		*efip;
660	struct xfs_extent		*extp;
661	unsigned int			count;
662
663	count = EFI_ITEM(intent)->efi_format.efi_nextents;
664	extp = EFI_ITEM(intent)->efi_format.efi_extents;
665
666	tp->t_flags |= XFS_TRANS_DIRTY;
667	efdp = xfs_trans_get_efd(tp, EFI_ITEM(intent), count);
668	efdp->efd_next_extent = count;
669	memcpy(efdp->efd_format.efd_extents, extp, count * sizeof(*extp));
670	set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
671
672	efip = xfs_efi_init(tp->t_mountp, count);
673	memcpy(efip->efi_format.efi_extents, extp, count * sizeof(*extp));
674	atomic_set(&efip->efi_next_extent, count);
675	xfs_trans_add_item(tp, &efip->efi_item);
676	set_bit(XFS_LI_DIRTY, &efip->efi_item.li_flags);
677	return &efip->efi_item;
678}
679
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
680static const struct xfs_item_ops xfs_efi_item_ops = {
 
681	.iop_size	= xfs_efi_item_size,
682	.iop_format	= xfs_efi_item_format,
683	.iop_unpin	= xfs_efi_item_unpin,
684	.iop_release	= xfs_efi_item_release,
685	.iop_recover	= xfs_efi_item_recover,
686	.iop_match	= xfs_efi_item_match,
687	.iop_relog	= xfs_efi_item_relog,
688};
689
690/*
691 * This routine is called to create an in-core extent free intent
692 * item from the efi format structure which was logged on disk.
693 * It allocates an in-core efi, copies the extents from the format
694 * structure into it, and adds the efi to the AIL with the given
695 * LSN.
696 */
697STATIC int
698xlog_recover_efi_commit_pass2(
699	struct xlog			*log,
700	struct list_head		*buffer_list,
701	struct xlog_recover_item	*item,
702	xfs_lsn_t			lsn)
703{
704	struct xfs_mount		*mp = log->l_mp;
705	struct xfs_efi_log_item		*efip;
706	struct xfs_efi_log_format	*efi_formatp;
707	int				error;
708
709	efi_formatp = item->ri_buf[0].i_addr;
710
 
 
 
 
 
 
711	efip = xfs_efi_init(mp, efi_formatp->efi_nextents);
712	error = xfs_efi_copy_format(&item->ri_buf[0], &efip->efi_format);
713	if (error) {
714		xfs_efi_item_free(efip);
715		return error;
716	}
717	atomic_set(&efip->efi_next_extent, efi_formatp->efi_nextents);
718	/*
719	 * Insert the intent into the AIL directly and drop one reference so
720	 * that finishing or canceling the work will drop the other.
721	 */
722	xfs_trans_ail_insert(log->l_ailp, &efip->efi_item, lsn);
723	xfs_efi_release(efip);
724	return 0;
725}
726
727const struct xlog_recover_item_ops xlog_efi_item_ops = {
728	.item_type		= XFS_LI_EFI,
729	.commit_pass2		= xlog_recover_efi_commit_pass2,
730};
731
732/*
733 * This routine is called when an EFD format structure is found in a committed
734 * transaction in the log. Its purpose is to cancel the corresponding EFI if it
735 * was still in the log. To do this it searches the AIL for the EFI with an id
736 * equal to that in the EFD format structure. If we find it we drop the EFD
737 * reference, which removes the EFI from the AIL and frees it.
738 */
739STATIC int
740xlog_recover_efd_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_efd_log_format	*efd_formatp;
 
747
748	efd_formatp = item->ri_buf[0].i_addr;
749	ASSERT((item->ri_buf[0].i_len == (sizeof(xfs_efd_log_format_32_t) +
750		((efd_formatp->efd_nextents - 1) * sizeof(xfs_extent_32_t)))) ||
751	       (item->ri_buf[0].i_len == (sizeof(xfs_efd_log_format_64_t) +
752		((efd_formatp->efd_nextents - 1) * sizeof(xfs_extent_64_t)))));
 
 
 
 
 
 
 
 
 
 
 
753
754	xlog_recover_release_intent(log, XFS_LI_EFI, efd_formatp->efd_efi_id);
755	return 0;
756}
757
758const struct xlog_recover_item_ops xlog_efd_item_ops = {
759	.item_type		= XFS_LI_EFD,
760	.commit_pass2		= xlog_recover_efd_commit_pass2,
761};