xfs: refactor all the EFI/EFD log item sizeof logic
[platform/kernel/linux-starfive.git] / fs / xfs / xfs_extfree_item.c
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
29 struct kmem_cache       *xfs_efi_cache;
30 struct kmem_cache       *xfs_efd_cache;
31
32 static const struct xfs_item_ops xfs_efi_item_ops;
33
34 static 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
39 STATIC void
40 xfs_efi_item_free(
41         struct xfs_efi_log_item *efip)
42 {
43         kmem_free(efip->efi_item.li_lv_shadow);
44         if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS)
45                 kmem_free(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  */
57 STATIC void
58 xfs_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
69 STATIC void
70 xfs_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  */
88 STATIC void
89 xfs_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  */
116 STATIC void
117 xfs_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  */
130 STATIC void
131 xfs_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  */
140 STATIC struct xfs_efi_log_item *
141 xfs_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  */
173 STATIC int
174 xfs_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_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL);
220         return -EFSCORRUPTED;
221 }
222
223 static inline struct xfs_efd_log_item *EFD_ITEM(struct xfs_log_item *lip)
224 {
225         return container_of(lip, struct xfs_efd_log_item, efd_item);
226 }
227
228 STATIC void
229 xfs_efd_item_free(struct xfs_efd_log_item *efdp)
230 {
231         kmem_free(efdp->efd_item.li_lv_shadow);
232         if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS)
233                 kmem_free(efdp);
234         else
235                 kmem_cache_free(xfs_efd_cache, efdp);
236 }
237
238 STATIC void
239 xfs_efd_item_size(
240         struct xfs_log_item     *lip,
241         int                     *nvecs,
242         int                     *nbytes)
243 {
244         struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
245
246         *nvecs += 1;
247         *nbytes += xfs_efd_log_format_sizeof(efdp->efd_format.efd_nextents);
248 }
249
250 /*
251  * This is called to fill in the vector of log iovecs for the
252  * given efd log item. We use only 1 iovec, and we point that
253  * at the efd_log_format structure embedded in the efd item.
254  * It is at this point that we assert that all of the extent
255  * slots in the efd item have been filled.
256  */
257 STATIC void
258 xfs_efd_item_format(
259         struct xfs_log_item     *lip,
260         struct xfs_log_vec      *lv)
261 {
262         struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
263         struct xfs_log_iovec    *vecp = NULL;
264
265         ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
266
267         efdp->efd_format.efd_type = XFS_LI_EFD;
268         efdp->efd_format.efd_size = 1;
269
270         xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFD_FORMAT,
271                         &efdp->efd_format,
272                         xfs_efd_log_format_sizeof(efdp->efd_format.efd_nextents));
273 }
274
275 /*
276  * The EFD is either committed or aborted if the transaction is cancelled. If
277  * the transaction is cancelled, drop our reference to the EFI and free the EFD.
278  */
279 STATIC void
280 xfs_efd_item_release(
281         struct xfs_log_item     *lip)
282 {
283         struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
284
285         xfs_efi_release(efdp->efd_efip);
286         xfs_efd_item_free(efdp);
287 }
288
289 static struct xfs_log_item *
290 xfs_efd_item_intent(
291         struct xfs_log_item     *lip)
292 {
293         return &EFD_ITEM(lip)->efd_efip->efi_item;
294 }
295
296 static const struct xfs_item_ops xfs_efd_item_ops = {
297         .flags          = XFS_ITEM_RELEASE_WHEN_COMMITTED |
298                           XFS_ITEM_INTENT_DONE,
299         .iop_size       = xfs_efd_item_size,
300         .iop_format     = xfs_efd_item_format,
301         .iop_release    = xfs_efd_item_release,
302         .iop_intent     = xfs_efd_item_intent,
303 };
304
305 /*
306  * Allocate an "extent free done" log item that will hold nextents worth of
307  * extents.  The caller must use all nextents extents, because we are not
308  * flexible about this at all.
309  */
310 static struct xfs_efd_log_item *
311 xfs_trans_get_efd(
312         struct xfs_trans                *tp,
313         struct xfs_efi_log_item         *efip,
314         unsigned int                    nextents)
315 {
316         struct xfs_efd_log_item         *efdp;
317
318         ASSERT(nextents > 0);
319
320         if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
321                 efdp = kzalloc(xfs_efd_log_item_sizeof(nextents),
322                                 GFP_KERNEL | __GFP_NOFAIL);
323         } else {
324                 efdp = kmem_cache_zalloc(xfs_efd_cache,
325                                         GFP_KERNEL | __GFP_NOFAIL);
326         }
327
328         xfs_log_item_init(tp->t_mountp, &efdp->efd_item, XFS_LI_EFD,
329                           &xfs_efd_item_ops);
330         efdp->efd_efip = efip;
331         efdp->efd_format.efd_nextents = nextents;
332         efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;
333
334         xfs_trans_add_item(tp, &efdp->efd_item);
335         return efdp;
336 }
337
338 /*
339  * Free an extent and log it to the EFD. Note that the transaction is marked
340  * dirty regardless of whether the extent free succeeds or fails to support the
341  * EFI/EFD lifecycle rules.
342  */
343 static int
344 xfs_trans_free_extent(
345         struct xfs_trans                *tp,
346         struct xfs_efd_log_item         *efdp,
347         xfs_fsblock_t                   start_block,
348         xfs_extlen_t                    ext_len,
349         const struct xfs_owner_info     *oinfo,
350         bool                            skip_discard)
351 {
352         struct xfs_mount                *mp = tp->t_mountp;
353         struct xfs_extent               *extp;
354         uint                            next_extent;
355         xfs_agnumber_t                  agno = XFS_FSB_TO_AGNO(mp, start_block);
356         xfs_agblock_t                   agbno = XFS_FSB_TO_AGBNO(mp,
357                                                                 start_block);
358         int                             error;
359
360         trace_xfs_bmap_free_deferred(tp->t_mountp, agno, 0, agbno, ext_len);
361
362         error = __xfs_free_extent(tp, start_block, ext_len,
363                                   oinfo, XFS_AG_RESV_NONE, skip_discard);
364         /*
365          * Mark the transaction dirty, even on error. This ensures the
366          * transaction is aborted, which:
367          *
368          * 1.) releases the EFI and frees the EFD
369          * 2.) shuts down the filesystem
370          */
371         tp->t_flags |= XFS_TRANS_DIRTY | XFS_TRANS_HAS_INTENT_DONE;
372         set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
373
374         next_extent = efdp->efd_next_extent;
375         ASSERT(next_extent < efdp->efd_format.efd_nextents);
376         extp = &(efdp->efd_format.efd_extents[next_extent]);
377         extp->ext_start = start_block;
378         extp->ext_len = ext_len;
379         efdp->efd_next_extent++;
380
381         return error;
382 }
383
384 /* Sort bmap items by AG. */
385 static int
386 xfs_extent_free_diff_items(
387         void                            *priv,
388         const struct list_head          *a,
389         const struct list_head          *b)
390 {
391         struct xfs_mount                *mp = priv;
392         struct xfs_extent_free_item     *ra;
393         struct xfs_extent_free_item     *rb;
394
395         ra = container_of(a, struct xfs_extent_free_item, xefi_list);
396         rb = container_of(b, struct xfs_extent_free_item, xefi_list);
397         return  XFS_FSB_TO_AGNO(mp, ra->xefi_startblock) -
398                 XFS_FSB_TO_AGNO(mp, rb->xefi_startblock);
399 }
400
401 /* Log a free extent to the intent item. */
402 STATIC void
403 xfs_extent_free_log_item(
404         struct xfs_trans                *tp,
405         struct xfs_efi_log_item         *efip,
406         struct xfs_extent_free_item     *free)
407 {
408         uint                            next_extent;
409         struct xfs_extent               *extp;
410
411         tp->t_flags |= XFS_TRANS_DIRTY;
412         set_bit(XFS_LI_DIRTY, &efip->efi_item.li_flags);
413
414         /*
415          * atomic_inc_return gives us the value after the increment;
416          * we want to use it as an array index so we need to subtract 1 from
417          * it.
418          */
419         next_extent = atomic_inc_return(&efip->efi_next_extent) - 1;
420         ASSERT(next_extent < efip->efi_format.efi_nextents);
421         extp = &efip->efi_format.efi_extents[next_extent];
422         extp->ext_start = free->xefi_startblock;
423         extp->ext_len = free->xefi_blockcount;
424 }
425
426 static struct xfs_log_item *
427 xfs_extent_free_create_intent(
428         struct xfs_trans                *tp,
429         struct list_head                *items,
430         unsigned int                    count,
431         bool                            sort)
432 {
433         struct xfs_mount                *mp = tp->t_mountp;
434         struct xfs_efi_log_item         *efip = xfs_efi_init(mp, count);
435         struct xfs_extent_free_item     *free;
436
437         ASSERT(count > 0);
438
439         xfs_trans_add_item(tp, &efip->efi_item);
440         if (sort)
441                 list_sort(mp, items, xfs_extent_free_diff_items);
442         list_for_each_entry(free, items, xefi_list)
443                 xfs_extent_free_log_item(tp, efip, free);
444         return &efip->efi_item;
445 }
446
447 /* Get an EFD so we can process all the free extents. */
448 static struct xfs_log_item *
449 xfs_extent_free_create_done(
450         struct xfs_trans                *tp,
451         struct xfs_log_item             *intent,
452         unsigned int                    count)
453 {
454         return &xfs_trans_get_efd(tp, EFI_ITEM(intent), count)->efd_item;
455 }
456
457 /* Process a free extent. */
458 STATIC int
459 xfs_extent_free_finish_item(
460         struct xfs_trans                *tp,
461         struct xfs_log_item             *done,
462         struct list_head                *item,
463         struct xfs_btree_cur            **state)
464 {
465         struct xfs_owner_info           oinfo = { };
466         struct xfs_extent_free_item     *free;
467         int                             error;
468
469         free = container_of(item, struct xfs_extent_free_item, xefi_list);
470         oinfo.oi_owner = free->xefi_owner;
471         if (free->xefi_flags & XFS_EFI_ATTR_FORK)
472                 oinfo.oi_flags |= XFS_OWNER_INFO_ATTR_FORK;
473         if (free->xefi_flags & XFS_EFI_BMBT_BLOCK)
474                 oinfo.oi_flags |= XFS_OWNER_INFO_BMBT_BLOCK;
475         error = xfs_trans_free_extent(tp, EFD_ITEM(done),
476                         free->xefi_startblock,
477                         free->xefi_blockcount,
478                         &oinfo, free->xefi_flags & XFS_EFI_SKIP_DISCARD);
479         kmem_cache_free(xfs_extfree_item_cache, free);
480         return error;
481 }
482
483 /* Abort all pending EFIs. */
484 STATIC void
485 xfs_extent_free_abort_intent(
486         struct xfs_log_item             *intent)
487 {
488         xfs_efi_release(EFI_ITEM(intent));
489 }
490
491 /* Cancel a free extent. */
492 STATIC void
493 xfs_extent_free_cancel_item(
494         struct list_head                *item)
495 {
496         struct xfs_extent_free_item     *free;
497
498         free = container_of(item, struct xfs_extent_free_item, xefi_list);
499         kmem_cache_free(xfs_extfree_item_cache, free);
500 }
501
502 const struct xfs_defer_op_type xfs_extent_free_defer_type = {
503         .max_items      = XFS_EFI_MAX_FAST_EXTENTS,
504         .create_intent  = xfs_extent_free_create_intent,
505         .abort_intent   = xfs_extent_free_abort_intent,
506         .create_done    = xfs_extent_free_create_done,
507         .finish_item    = xfs_extent_free_finish_item,
508         .cancel_item    = xfs_extent_free_cancel_item,
509 };
510
511 /*
512  * AGFL blocks are accounted differently in the reserve pools and are not
513  * inserted into the busy extent list.
514  */
515 STATIC int
516 xfs_agfl_free_finish_item(
517         struct xfs_trans                *tp,
518         struct xfs_log_item             *done,
519         struct list_head                *item,
520         struct xfs_btree_cur            **state)
521 {
522         struct xfs_owner_info           oinfo = { };
523         struct xfs_mount                *mp = tp->t_mountp;
524         struct xfs_efd_log_item         *efdp = EFD_ITEM(done);
525         struct xfs_extent_free_item     *free;
526         struct xfs_extent               *extp;
527         struct xfs_buf                  *agbp;
528         int                             error;
529         xfs_agnumber_t                  agno;
530         xfs_agblock_t                   agbno;
531         uint                            next_extent;
532         struct xfs_perag                *pag;
533
534         free = container_of(item, struct xfs_extent_free_item, xefi_list);
535         ASSERT(free->xefi_blockcount == 1);
536         agno = XFS_FSB_TO_AGNO(mp, free->xefi_startblock);
537         agbno = XFS_FSB_TO_AGBNO(mp, free->xefi_startblock);
538         oinfo.oi_owner = free->xefi_owner;
539
540         trace_xfs_agfl_free_deferred(mp, agno, 0, agbno, free->xefi_blockcount);
541
542         pag = xfs_perag_get(mp, agno);
543         error = xfs_alloc_read_agf(pag, tp, 0, &agbp);
544         if (!error)
545                 error = xfs_free_agfl_block(tp, agno, agbno, agbp, &oinfo);
546         xfs_perag_put(pag);
547
548         /*
549          * Mark the transaction dirty, even on error. This ensures the
550          * transaction is aborted, which:
551          *
552          * 1.) releases the EFI and frees the EFD
553          * 2.) shuts down the filesystem
554          */
555         tp->t_flags |= XFS_TRANS_DIRTY;
556         set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
557
558         next_extent = efdp->efd_next_extent;
559         ASSERT(next_extent < efdp->efd_format.efd_nextents);
560         extp = &(efdp->efd_format.efd_extents[next_extent]);
561         extp->ext_start = free->xefi_startblock;
562         extp->ext_len = free->xefi_blockcount;
563         efdp->efd_next_extent++;
564
565         kmem_cache_free(xfs_extfree_item_cache, free);
566         return error;
567 }
568
569 /* sub-type with special handling for AGFL deferred frees */
570 const struct xfs_defer_op_type xfs_agfl_free_defer_type = {
571         .max_items      = XFS_EFI_MAX_FAST_EXTENTS,
572         .create_intent  = xfs_extent_free_create_intent,
573         .abort_intent   = xfs_extent_free_abort_intent,
574         .create_done    = xfs_extent_free_create_done,
575         .finish_item    = xfs_agfl_free_finish_item,
576         .cancel_item    = xfs_extent_free_cancel_item,
577 };
578
579 /* Is this recovered EFI ok? */
580 static inline bool
581 xfs_efi_validate_ext(
582         struct xfs_mount                *mp,
583         struct xfs_extent               *extp)
584 {
585         return xfs_verify_fsbext(mp, extp->ext_start, extp->ext_len);
586 }
587
588 /*
589  * Process an extent free intent item that was recovered from
590  * the log.  We need to free the extents that it describes.
591  */
592 STATIC int
593 xfs_efi_item_recover(
594         struct xfs_log_item             *lip,
595         struct list_head                *capture_list)
596 {
597         struct xfs_efi_log_item         *efip = EFI_ITEM(lip);
598         struct xfs_mount                *mp = lip->li_log->l_mp;
599         struct xfs_efd_log_item         *efdp;
600         struct xfs_trans                *tp;
601         struct xfs_extent               *extp;
602         int                             i;
603         int                             error = 0;
604
605         /*
606          * First check the validity of the extents described by the
607          * EFI.  If any are bad, then assume that all are bad and
608          * just toss the EFI.
609          */
610         for (i = 0; i < efip->efi_format.efi_nextents; i++) {
611                 if (!xfs_efi_validate_ext(mp,
612                                         &efip->efi_format.efi_extents[i])) {
613                         XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
614                                         &efip->efi_format,
615                                         sizeof(efip->efi_format));
616                         return -EFSCORRUPTED;
617                 }
618         }
619
620         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
621         if (error)
622                 return error;
623         efdp = xfs_trans_get_efd(tp, efip, efip->efi_format.efi_nextents);
624
625         for (i = 0; i < efip->efi_format.efi_nextents; i++) {
626                 extp = &efip->efi_format.efi_extents[i];
627                 error = xfs_trans_free_extent(tp, efdp, extp->ext_start,
628                                               extp->ext_len,
629                                               &XFS_RMAP_OINFO_ANY_OWNER, false);
630                 if (error == -EFSCORRUPTED)
631                         XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
632                                         extp, sizeof(*extp));
633                 if (error)
634                         goto abort_error;
635
636         }
637
638         return xfs_defer_ops_capture_and_commit(tp, capture_list);
639
640 abort_error:
641         xfs_trans_cancel(tp);
642         return error;
643 }
644
645 STATIC bool
646 xfs_efi_item_match(
647         struct xfs_log_item     *lip,
648         uint64_t                intent_id)
649 {
650         return EFI_ITEM(lip)->efi_format.efi_id == intent_id;
651 }
652
653 /* Relog an intent item to push the log tail forward. */
654 static struct xfs_log_item *
655 xfs_efi_item_relog(
656         struct xfs_log_item             *intent,
657         struct xfs_trans                *tp)
658 {
659         struct xfs_efd_log_item         *efdp;
660         struct xfs_efi_log_item         *efip;
661         struct xfs_extent               *extp;
662         unsigned int                    count;
663
664         count = EFI_ITEM(intent)->efi_format.efi_nextents;
665         extp = EFI_ITEM(intent)->efi_format.efi_extents;
666
667         tp->t_flags |= XFS_TRANS_DIRTY;
668         efdp = xfs_trans_get_efd(tp, EFI_ITEM(intent), count);
669         efdp->efd_next_extent = count;
670         memcpy(efdp->efd_format.efd_extents, extp, count * sizeof(*extp));
671         set_bit(XFS_LI_DIRTY, &efdp->efd_item.li_flags);
672
673         efip = xfs_efi_init(tp->t_mountp, count);
674         memcpy(efip->efi_format.efi_extents, extp, count * sizeof(*extp));
675         atomic_set(&efip->efi_next_extent, count);
676         xfs_trans_add_item(tp, &efip->efi_item);
677         set_bit(XFS_LI_DIRTY, &efip->efi_item.li_flags);
678         return &efip->efi_item;
679 }
680
681 static const struct xfs_item_ops xfs_efi_item_ops = {
682         .flags          = XFS_ITEM_INTENT,
683         .iop_size       = xfs_efi_item_size,
684         .iop_format     = xfs_efi_item_format,
685         .iop_unpin      = xfs_efi_item_unpin,
686         .iop_release    = xfs_efi_item_release,
687         .iop_recover    = xfs_efi_item_recover,
688         .iop_match      = xfs_efi_item_match,
689         .iop_relog      = xfs_efi_item_relog,
690 };
691
692 /*
693  * This routine is called to create an in-core extent free intent
694  * item from the efi format structure which was logged on disk.
695  * It allocates an in-core efi, copies the extents from the format
696  * structure into it, and adds the efi to the AIL with the given
697  * LSN.
698  */
699 STATIC int
700 xlog_recover_efi_commit_pass2(
701         struct xlog                     *log,
702         struct list_head                *buffer_list,
703         struct xlog_recover_item        *item,
704         xfs_lsn_t                       lsn)
705 {
706         struct xfs_mount                *mp = log->l_mp;
707         struct xfs_efi_log_item         *efip;
708         struct xfs_efi_log_format       *efi_formatp;
709         int                             error;
710
711         efi_formatp = item->ri_buf[0].i_addr;
712
713         if (item->ri_buf[0].i_len < xfs_efi_log_format_sizeof(0)) {
714                 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, log->l_mp);
715                 return -EFSCORRUPTED;
716         }
717
718         efip = xfs_efi_init(mp, efi_formatp->efi_nextents);
719         error = xfs_efi_copy_format(&item->ri_buf[0], &efip->efi_format);
720         if (error) {
721                 xfs_efi_item_free(efip);
722                 return error;
723         }
724         atomic_set(&efip->efi_next_extent, efi_formatp->efi_nextents);
725         /*
726          * Insert the intent into the AIL directly and drop one reference so
727          * that finishing or canceling the work will drop the other.
728          */
729         xfs_trans_ail_insert(log->l_ailp, &efip->efi_item, lsn);
730         xfs_efi_release(efip);
731         return 0;
732 }
733
734 const struct xlog_recover_item_ops xlog_efi_item_ops = {
735         .item_type              = XFS_LI_EFI,
736         .commit_pass2           = xlog_recover_efi_commit_pass2,
737 };
738
739 /*
740  * This routine is called when an EFD format structure is found in a committed
741  * transaction in the log. Its purpose is to cancel the corresponding EFI if it
742  * was still in the log. To do this it searches the AIL for the EFI with an id
743  * equal to that in the EFD format structure. If we find it we drop the EFD
744  * reference, which removes the EFI from the AIL and frees it.
745  */
746 STATIC int
747 xlog_recover_efd_commit_pass2(
748         struct xlog                     *log,
749         struct list_head                *buffer_list,
750         struct xlog_recover_item        *item,
751         xfs_lsn_t                       lsn)
752 {
753         struct xfs_efd_log_format       *efd_formatp;
754
755         efd_formatp = item->ri_buf[0].i_addr;
756         ASSERT(item->ri_buf[0].i_len == xfs_efd_log_format32_sizeof(
757                                                 efd_formatp->efd_nextents) ||
758                item->ri_buf[0].i_len == xfs_efd_log_format64_sizeof(
759                                                 efd_formatp->efd_nextents));
760
761         xlog_recover_release_intent(log, XFS_LI_EFI, efd_formatp->efd_efi_id);
762         return 0;
763 }
764
765 const struct xlog_recover_item_ops xlog_efd_item_ops = {
766         .item_type              = XFS_LI_EFD,
767         .commit_pass2           = xlog_recover_efd_commit_pass2,
768 };