xfs: give xfs_bmap_intent its own perag reference
[platform/kernel/linux-rpi.git] / fs / xfs / xfs_bmap_item.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2016 Oracle.  All Rights Reserved.
4  * Author: Darrick J. Wong <darrick.wong@oracle.com>
5  */
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_format.h"
9 #include "xfs_log_format.h"
10 #include "xfs_trans_resv.h"
11 #include "xfs_bit.h"
12 #include "xfs_shared.h"
13 #include "xfs_mount.h"
14 #include "xfs_defer.h"
15 #include "xfs_inode.h"
16 #include "xfs_trans.h"
17 #include "xfs_trans_priv.h"
18 #include "xfs_bmap_item.h"
19 #include "xfs_log.h"
20 #include "xfs_bmap.h"
21 #include "xfs_icache.h"
22 #include "xfs_bmap_btree.h"
23 #include "xfs_trans_space.h"
24 #include "xfs_error.h"
25 #include "xfs_log_priv.h"
26 #include "xfs_log_recover.h"
27 #include "xfs_ag.h"
28
29 struct kmem_cache       *xfs_bui_cache;
30 struct kmem_cache       *xfs_bud_cache;
31
32 static const struct xfs_item_ops xfs_bui_item_ops;
33
34 static inline struct xfs_bui_log_item *BUI_ITEM(struct xfs_log_item *lip)
35 {
36         return container_of(lip, struct xfs_bui_log_item, bui_item);
37 }
38
39 STATIC void
40 xfs_bui_item_free(
41         struct xfs_bui_log_item *buip)
42 {
43         kmem_free(buip->bui_item.li_lv_shadow);
44         kmem_cache_free(xfs_bui_cache, buip);
45 }
46
47 /*
48  * Freeing the BUI requires that we remove it from the AIL if it has already
49  * been placed there. However, the BUI may not yet have been placed in the AIL
50  * when called by xfs_bui_release() from BUD processing due to the ordering of
51  * committed vs unpin operations in bulk insert operations. Hence the reference
52  * count to ensure only the last caller frees the BUI.
53  */
54 STATIC void
55 xfs_bui_release(
56         struct xfs_bui_log_item *buip)
57 {
58         ASSERT(atomic_read(&buip->bui_refcount) > 0);
59         if (!atomic_dec_and_test(&buip->bui_refcount))
60                 return;
61
62         xfs_trans_ail_delete(&buip->bui_item, 0);
63         xfs_bui_item_free(buip);
64 }
65
66
67 STATIC void
68 xfs_bui_item_size(
69         struct xfs_log_item     *lip,
70         int                     *nvecs,
71         int                     *nbytes)
72 {
73         struct xfs_bui_log_item *buip = BUI_ITEM(lip);
74
75         *nvecs += 1;
76         *nbytes += xfs_bui_log_format_sizeof(buip->bui_format.bui_nextents);
77 }
78
79 /*
80  * This is called to fill in the vector of log iovecs for the
81  * given bui log item. We use only 1 iovec, and we point that
82  * at the bui_log_format structure embedded in the bui item.
83  * It is at this point that we assert that all of the extent
84  * slots in the bui item have been filled.
85  */
86 STATIC void
87 xfs_bui_item_format(
88         struct xfs_log_item     *lip,
89         struct xfs_log_vec      *lv)
90 {
91         struct xfs_bui_log_item *buip = BUI_ITEM(lip);
92         struct xfs_log_iovec    *vecp = NULL;
93
94         ASSERT(atomic_read(&buip->bui_next_extent) ==
95                         buip->bui_format.bui_nextents);
96
97         buip->bui_format.bui_type = XFS_LI_BUI;
98         buip->bui_format.bui_size = 1;
99
100         xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_BUI_FORMAT, &buip->bui_format,
101                         xfs_bui_log_format_sizeof(buip->bui_format.bui_nextents));
102 }
103
104 /*
105  * The unpin operation is the last place an BUI is manipulated in the log. It is
106  * either inserted in the AIL or aborted in the event of a log I/O error. In
107  * either case, the BUI transaction has been successfully committed to make it
108  * this far. Therefore, we expect whoever committed the BUI to either construct
109  * and commit the BUD or drop the BUD's reference in the event of error. Simply
110  * drop the log's BUI reference now that the log is done with it.
111  */
112 STATIC void
113 xfs_bui_item_unpin(
114         struct xfs_log_item     *lip,
115         int                     remove)
116 {
117         struct xfs_bui_log_item *buip = BUI_ITEM(lip);
118
119         xfs_bui_release(buip);
120 }
121
122 /*
123  * The BUI has been either committed or aborted if the transaction has been
124  * cancelled. If the transaction was cancelled, an BUD isn't going to be
125  * constructed and thus we free the BUI here directly.
126  */
127 STATIC void
128 xfs_bui_item_release(
129         struct xfs_log_item     *lip)
130 {
131         xfs_bui_release(BUI_ITEM(lip));
132 }
133
134 /*
135  * Allocate and initialize an bui item with the given number of extents.
136  */
137 STATIC struct xfs_bui_log_item *
138 xfs_bui_init(
139         struct xfs_mount                *mp)
140
141 {
142         struct xfs_bui_log_item         *buip;
143
144         buip = kmem_cache_zalloc(xfs_bui_cache, GFP_KERNEL | __GFP_NOFAIL);
145
146         xfs_log_item_init(mp, &buip->bui_item, XFS_LI_BUI, &xfs_bui_item_ops);
147         buip->bui_format.bui_nextents = XFS_BUI_MAX_FAST_EXTENTS;
148         buip->bui_format.bui_id = (uintptr_t)(void *)buip;
149         atomic_set(&buip->bui_next_extent, 0);
150         atomic_set(&buip->bui_refcount, 2);
151
152         return buip;
153 }
154
155 static inline struct xfs_bud_log_item *BUD_ITEM(struct xfs_log_item *lip)
156 {
157         return container_of(lip, struct xfs_bud_log_item, bud_item);
158 }
159
160 STATIC void
161 xfs_bud_item_size(
162         struct xfs_log_item     *lip,
163         int                     *nvecs,
164         int                     *nbytes)
165 {
166         *nvecs += 1;
167         *nbytes += sizeof(struct xfs_bud_log_format);
168 }
169
170 /*
171  * This is called to fill in the vector of log iovecs for the
172  * given bud log item. We use only 1 iovec, and we point that
173  * at the bud_log_format structure embedded in the bud item.
174  * It is at this point that we assert that all of the extent
175  * slots in the bud item have been filled.
176  */
177 STATIC void
178 xfs_bud_item_format(
179         struct xfs_log_item     *lip,
180         struct xfs_log_vec      *lv)
181 {
182         struct xfs_bud_log_item *budp = BUD_ITEM(lip);
183         struct xfs_log_iovec    *vecp = NULL;
184
185         budp->bud_format.bud_type = XFS_LI_BUD;
186         budp->bud_format.bud_size = 1;
187
188         xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_BUD_FORMAT, &budp->bud_format,
189                         sizeof(struct xfs_bud_log_format));
190 }
191
192 /*
193  * The BUD is either committed or aborted if the transaction is cancelled. If
194  * the transaction is cancelled, drop our reference to the BUI and free the
195  * BUD.
196  */
197 STATIC void
198 xfs_bud_item_release(
199         struct xfs_log_item     *lip)
200 {
201         struct xfs_bud_log_item *budp = BUD_ITEM(lip);
202
203         xfs_bui_release(budp->bud_buip);
204         kmem_free(budp->bud_item.li_lv_shadow);
205         kmem_cache_free(xfs_bud_cache, budp);
206 }
207
208 static struct xfs_log_item *
209 xfs_bud_item_intent(
210         struct xfs_log_item     *lip)
211 {
212         return &BUD_ITEM(lip)->bud_buip->bui_item;
213 }
214
215 static const struct xfs_item_ops xfs_bud_item_ops = {
216         .flags          = XFS_ITEM_RELEASE_WHEN_COMMITTED |
217                           XFS_ITEM_INTENT_DONE,
218         .iop_size       = xfs_bud_item_size,
219         .iop_format     = xfs_bud_item_format,
220         .iop_release    = xfs_bud_item_release,
221         .iop_intent     = xfs_bud_item_intent,
222 };
223
224 static struct xfs_bud_log_item *
225 xfs_trans_get_bud(
226         struct xfs_trans                *tp,
227         struct xfs_bui_log_item         *buip)
228 {
229         struct xfs_bud_log_item         *budp;
230
231         budp = kmem_cache_zalloc(xfs_bud_cache, GFP_KERNEL | __GFP_NOFAIL);
232         xfs_log_item_init(tp->t_mountp, &budp->bud_item, XFS_LI_BUD,
233                           &xfs_bud_item_ops);
234         budp->bud_buip = buip;
235         budp->bud_format.bud_bui_id = buip->bui_format.bui_id;
236
237         xfs_trans_add_item(tp, &budp->bud_item);
238         return budp;
239 }
240
241 /*
242  * Finish an bmap update and log it to the BUD. Note that the
243  * transaction is marked dirty regardless of whether the bmap update
244  * succeeds or fails to support the BUI/BUD lifecycle rules.
245  */
246 static int
247 xfs_trans_log_finish_bmap_update(
248         struct xfs_trans                *tp,
249         struct xfs_bud_log_item         *budp,
250         struct xfs_bmap_intent          *bi)
251 {
252         int                             error;
253
254         error = xfs_bmap_finish_one(tp, bi);
255
256         /*
257          * Mark the transaction dirty, even on error. This ensures the
258          * transaction is aborted, which:
259          *
260          * 1.) releases the BUI and frees the BUD
261          * 2.) shuts down the filesystem
262          */
263         tp->t_flags |= XFS_TRANS_DIRTY | XFS_TRANS_HAS_INTENT_DONE;
264         set_bit(XFS_LI_DIRTY, &budp->bud_item.li_flags);
265
266         return error;
267 }
268
269 /* Sort bmap intents by inode. */
270 static int
271 xfs_bmap_update_diff_items(
272         void                            *priv,
273         const struct list_head          *a,
274         const struct list_head          *b)
275 {
276         struct xfs_bmap_intent          *ba;
277         struct xfs_bmap_intent          *bb;
278
279         ba = container_of(a, struct xfs_bmap_intent, bi_list);
280         bb = container_of(b, struct xfs_bmap_intent, bi_list);
281         return ba->bi_owner->i_ino - bb->bi_owner->i_ino;
282 }
283
284 /* Set the map extent flags for this mapping. */
285 static void
286 xfs_trans_set_bmap_flags(
287         struct xfs_map_extent           *map,
288         enum xfs_bmap_intent_type       type,
289         int                             whichfork,
290         xfs_exntst_t                    state)
291 {
292         map->me_flags = 0;
293         switch (type) {
294         case XFS_BMAP_MAP:
295         case XFS_BMAP_UNMAP:
296                 map->me_flags = type;
297                 break;
298         default:
299                 ASSERT(0);
300         }
301         if (state == XFS_EXT_UNWRITTEN)
302                 map->me_flags |= XFS_BMAP_EXTENT_UNWRITTEN;
303         if (whichfork == XFS_ATTR_FORK)
304                 map->me_flags |= XFS_BMAP_EXTENT_ATTR_FORK;
305 }
306
307 /* Log bmap updates in the intent item. */
308 STATIC void
309 xfs_bmap_update_log_item(
310         struct xfs_trans                *tp,
311         struct xfs_bui_log_item         *buip,
312         struct xfs_bmap_intent          *bi)
313 {
314         uint                            next_extent;
315         struct xfs_map_extent           *map;
316
317         tp->t_flags |= XFS_TRANS_DIRTY;
318         set_bit(XFS_LI_DIRTY, &buip->bui_item.li_flags);
319
320         /*
321          * atomic_inc_return gives us the value after the increment;
322          * we want to use it as an array index so we need to subtract 1 from
323          * it.
324          */
325         next_extent = atomic_inc_return(&buip->bui_next_extent) - 1;
326         ASSERT(next_extent < buip->bui_format.bui_nextents);
327         map = &buip->bui_format.bui_extents[next_extent];
328         map->me_owner = bi->bi_owner->i_ino;
329         map->me_startblock = bi->bi_bmap.br_startblock;
330         map->me_startoff = bi->bi_bmap.br_startoff;
331         map->me_len = bi->bi_bmap.br_blockcount;
332         xfs_trans_set_bmap_flags(map, bi->bi_type, bi->bi_whichfork,
333                         bi->bi_bmap.br_state);
334 }
335
336 static struct xfs_log_item *
337 xfs_bmap_update_create_intent(
338         struct xfs_trans                *tp,
339         struct list_head                *items,
340         unsigned int                    count,
341         bool                            sort)
342 {
343         struct xfs_mount                *mp = tp->t_mountp;
344         struct xfs_bui_log_item         *buip = xfs_bui_init(mp);
345         struct xfs_bmap_intent          *bi;
346
347         ASSERT(count == XFS_BUI_MAX_FAST_EXTENTS);
348
349         xfs_trans_add_item(tp, &buip->bui_item);
350         if (sort)
351                 list_sort(mp, items, xfs_bmap_update_diff_items);
352         list_for_each_entry(bi, items, bi_list)
353                 xfs_bmap_update_log_item(tp, buip, bi);
354         return &buip->bui_item;
355 }
356
357 /* Get an BUD so we can process all the deferred rmap updates. */
358 static struct xfs_log_item *
359 xfs_bmap_update_create_done(
360         struct xfs_trans                *tp,
361         struct xfs_log_item             *intent,
362         unsigned int                    count)
363 {
364         return &xfs_trans_get_bud(tp, BUI_ITEM(intent))->bud_item;
365 }
366
367 /* Take a passive ref to the AG containing the space we're mapping. */
368 void
369 xfs_bmap_update_get_group(
370         struct xfs_mount        *mp,
371         struct xfs_bmap_intent  *bi)
372 {
373         xfs_agnumber_t          agno;
374
375         agno = XFS_FSB_TO_AGNO(mp, bi->bi_bmap.br_startblock);
376         bi->bi_pag = xfs_perag_get(mp, agno);
377 }
378
379 /* Release a passive AG ref after finishing mapping work. */
380 static inline void
381 xfs_bmap_update_put_group(
382         struct xfs_bmap_intent  *bi)
383 {
384         xfs_perag_put(bi->bi_pag);
385 }
386
387 /* Process a deferred rmap update. */
388 STATIC int
389 xfs_bmap_update_finish_item(
390         struct xfs_trans                *tp,
391         struct xfs_log_item             *done,
392         struct list_head                *item,
393         struct xfs_btree_cur            **state)
394 {
395         struct xfs_bmap_intent          *bi;
396         int                             error;
397
398         bi = container_of(item, struct xfs_bmap_intent, bi_list);
399
400         error = xfs_trans_log_finish_bmap_update(tp, BUD_ITEM(done), bi);
401         if (!error && bi->bi_bmap.br_blockcount > 0) {
402                 ASSERT(bi->bi_type == XFS_BMAP_UNMAP);
403                 return -EAGAIN;
404         }
405
406         xfs_bmap_update_put_group(bi);
407         kmem_cache_free(xfs_bmap_intent_cache, bi);
408         return error;
409 }
410
411 /* Abort all pending BUIs. */
412 STATIC void
413 xfs_bmap_update_abort_intent(
414         struct xfs_log_item             *intent)
415 {
416         xfs_bui_release(BUI_ITEM(intent));
417 }
418
419 /* Cancel a deferred bmap update. */
420 STATIC void
421 xfs_bmap_update_cancel_item(
422         struct list_head                *item)
423 {
424         struct xfs_bmap_intent          *bi;
425
426         bi = container_of(item, struct xfs_bmap_intent, bi_list);
427
428         xfs_bmap_update_put_group(bi);
429         kmem_cache_free(xfs_bmap_intent_cache, bi);
430 }
431
432 const struct xfs_defer_op_type xfs_bmap_update_defer_type = {
433         .max_items      = XFS_BUI_MAX_FAST_EXTENTS,
434         .create_intent  = xfs_bmap_update_create_intent,
435         .abort_intent   = xfs_bmap_update_abort_intent,
436         .create_done    = xfs_bmap_update_create_done,
437         .finish_item    = xfs_bmap_update_finish_item,
438         .cancel_item    = xfs_bmap_update_cancel_item,
439 };
440
441 /* Is this recovered BUI ok? */
442 static inline bool
443 xfs_bui_validate(
444         struct xfs_mount                *mp,
445         struct xfs_bui_log_item         *buip)
446 {
447         struct xfs_map_extent           *map;
448
449         /* Only one mapping operation per BUI... */
450         if (buip->bui_format.bui_nextents != XFS_BUI_MAX_FAST_EXTENTS)
451                 return false;
452
453         map = &buip->bui_format.bui_extents[0];
454
455         if (map->me_flags & ~XFS_BMAP_EXTENT_FLAGS)
456                 return false;
457
458         switch (map->me_flags & XFS_BMAP_EXTENT_TYPE_MASK) {
459         case XFS_BMAP_MAP:
460         case XFS_BMAP_UNMAP:
461                 break;
462         default:
463                 return false;
464         }
465
466         if (!xfs_verify_ino(mp, map->me_owner))
467                 return false;
468
469         if (!xfs_verify_fileext(mp, map->me_startoff, map->me_len))
470                 return false;
471
472         return xfs_verify_fsbext(mp, map->me_startblock, map->me_len);
473 }
474
475 /*
476  * Process a bmap update intent item that was recovered from the log.
477  * We need to update some inode's bmbt.
478  */
479 STATIC int
480 xfs_bui_item_recover(
481         struct xfs_log_item             *lip,
482         struct list_head                *capture_list)
483 {
484         struct xfs_bmap_intent          fake = { };
485         struct xfs_bui_log_item         *buip = BUI_ITEM(lip);
486         struct xfs_trans                *tp;
487         struct xfs_inode                *ip = NULL;
488         struct xfs_mount                *mp = lip->li_log->l_mp;
489         struct xfs_map_extent           *map;
490         struct xfs_bud_log_item         *budp;
491         int                             iext_delta;
492         int                             error = 0;
493
494         if (!xfs_bui_validate(mp, buip)) {
495                 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
496                                 &buip->bui_format, sizeof(buip->bui_format));
497                 return -EFSCORRUPTED;
498         }
499
500         map = &buip->bui_format.bui_extents[0];
501         fake.bi_whichfork = (map->me_flags & XFS_BMAP_EXTENT_ATTR_FORK) ?
502                         XFS_ATTR_FORK : XFS_DATA_FORK;
503         fake.bi_type = map->me_flags & XFS_BMAP_EXTENT_TYPE_MASK;
504
505         error = xlog_recover_iget(mp, map->me_owner, &ip);
506         if (error)
507                 return error;
508
509         /* Allocate transaction and do the work. */
510         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate,
511                         XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK), 0, 0, &tp);
512         if (error)
513                 goto err_rele;
514
515         budp = xfs_trans_get_bud(tp, buip);
516         xfs_ilock(ip, XFS_ILOCK_EXCL);
517         xfs_trans_ijoin(tp, ip, 0);
518
519         if (fake.bi_type == XFS_BMAP_MAP)
520                 iext_delta = XFS_IEXT_ADD_NOSPLIT_CNT;
521         else
522                 iext_delta = XFS_IEXT_PUNCH_HOLE_CNT;
523
524         error = xfs_iext_count_may_overflow(ip, fake.bi_whichfork, iext_delta);
525         if (error == -EFBIG)
526                 error = xfs_iext_count_upgrade(tp, ip, iext_delta);
527         if (error)
528                 goto err_cancel;
529
530         fake.bi_owner = ip;
531         fake.bi_bmap.br_startblock = map->me_startblock;
532         fake.bi_bmap.br_startoff = map->me_startoff;
533         fake.bi_bmap.br_blockcount = map->me_len;
534         fake.bi_bmap.br_state = (map->me_flags & XFS_BMAP_EXTENT_UNWRITTEN) ?
535                         XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
536
537         xfs_bmap_update_get_group(mp, &fake);
538         error = xfs_trans_log_finish_bmap_update(tp, budp, &fake);
539         if (error == -EFSCORRUPTED)
540                 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, map,
541                                 sizeof(*map));
542         xfs_bmap_update_put_group(&fake);
543         if (error)
544                 goto err_cancel;
545
546         if (fake.bi_bmap.br_blockcount > 0) {
547                 ASSERT(fake.bi_type == XFS_BMAP_UNMAP);
548                 xfs_bmap_unmap_extent(tp, ip, &fake.bi_bmap);
549         }
550
551         /*
552          * Commit transaction, which frees the transaction and saves the inode
553          * for later replay activities.
554          */
555         error = xfs_defer_ops_capture_and_commit(tp, capture_list);
556         if (error)
557                 goto err_unlock;
558
559         xfs_iunlock(ip, XFS_ILOCK_EXCL);
560         xfs_irele(ip);
561         return 0;
562
563 err_cancel:
564         xfs_trans_cancel(tp);
565 err_unlock:
566         xfs_iunlock(ip, XFS_ILOCK_EXCL);
567 err_rele:
568         xfs_irele(ip);
569         return error;
570 }
571
572 STATIC bool
573 xfs_bui_item_match(
574         struct xfs_log_item     *lip,
575         uint64_t                intent_id)
576 {
577         return BUI_ITEM(lip)->bui_format.bui_id == intent_id;
578 }
579
580 /* Relog an intent item to push the log tail forward. */
581 static struct xfs_log_item *
582 xfs_bui_item_relog(
583         struct xfs_log_item             *intent,
584         struct xfs_trans                *tp)
585 {
586         struct xfs_bud_log_item         *budp;
587         struct xfs_bui_log_item         *buip;
588         struct xfs_map_extent           *map;
589         unsigned int                    count;
590
591         count = BUI_ITEM(intent)->bui_format.bui_nextents;
592         map = BUI_ITEM(intent)->bui_format.bui_extents;
593
594         tp->t_flags |= XFS_TRANS_DIRTY;
595         budp = xfs_trans_get_bud(tp, BUI_ITEM(intent));
596         set_bit(XFS_LI_DIRTY, &budp->bud_item.li_flags);
597
598         buip = xfs_bui_init(tp->t_mountp);
599         memcpy(buip->bui_format.bui_extents, map, count * sizeof(*map));
600         atomic_set(&buip->bui_next_extent, count);
601         xfs_trans_add_item(tp, &buip->bui_item);
602         set_bit(XFS_LI_DIRTY, &buip->bui_item.li_flags);
603         return &buip->bui_item;
604 }
605
606 static const struct xfs_item_ops xfs_bui_item_ops = {
607         .flags          = XFS_ITEM_INTENT,
608         .iop_size       = xfs_bui_item_size,
609         .iop_format     = xfs_bui_item_format,
610         .iop_unpin      = xfs_bui_item_unpin,
611         .iop_release    = xfs_bui_item_release,
612         .iop_recover    = xfs_bui_item_recover,
613         .iop_match      = xfs_bui_item_match,
614         .iop_relog      = xfs_bui_item_relog,
615 };
616
617 static inline void
618 xfs_bui_copy_format(
619         struct xfs_bui_log_format       *dst,
620         const struct xfs_bui_log_format *src)
621 {
622         unsigned int                    i;
623
624         memcpy(dst, src, offsetof(struct xfs_bui_log_format, bui_extents));
625
626         for (i = 0; i < src->bui_nextents; i++)
627                 memcpy(&dst->bui_extents[i], &src->bui_extents[i],
628                                 sizeof(struct xfs_map_extent));
629 }
630
631 /*
632  * This routine is called to create an in-core extent bmap update
633  * item from the bui format structure which was logged on disk.
634  * It allocates an in-core bui, copies the extents from the format
635  * structure into it, and adds the bui to the AIL with the given
636  * LSN.
637  */
638 STATIC int
639 xlog_recover_bui_commit_pass2(
640         struct xlog                     *log,
641         struct list_head                *buffer_list,
642         struct xlog_recover_item        *item,
643         xfs_lsn_t                       lsn)
644 {
645         struct xfs_mount                *mp = log->l_mp;
646         struct xfs_bui_log_item         *buip;
647         struct xfs_bui_log_format       *bui_formatp;
648         size_t                          len;
649
650         bui_formatp = item->ri_buf[0].i_addr;
651
652         if (item->ri_buf[0].i_len < xfs_bui_log_format_sizeof(0)) {
653                 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
654                                 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
655                 return -EFSCORRUPTED;
656         }
657
658         if (bui_formatp->bui_nextents != XFS_BUI_MAX_FAST_EXTENTS) {
659                 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
660                                 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
661                 return -EFSCORRUPTED;
662         }
663
664         len = xfs_bui_log_format_sizeof(bui_formatp->bui_nextents);
665         if (item->ri_buf[0].i_len != len) {
666                 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
667                                 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
668                 return -EFSCORRUPTED;
669         }
670
671         buip = xfs_bui_init(mp);
672         xfs_bui_copy_format(&buip->bui_format, bui_formatp);
673         atomic_set(&buip->bui_next_extent, bui_formatp->bui_nextents);
674         /*
675          * Insert the intent into the AIL directly and drop one reference so
676          * that finishing or canceling the work will drop the other.
677          */
678         xfs_trans_ail_insert(log->l_ailp, &buip->bui_item, lsn);
679         xfs_bui_release(buip);
680         return 0;
681 }
682
683 const struct xlog_recover_item_ops xlog_bui_item_ops = {
684         .item_type              = XFS_LI_BUI,
685         .commit_pass2           = xlog_recover_bui_commit_pass2,
686 };
687
688 /*
689  * This routine is called when an BUD format structure is found in a committed
690  * transaction in the log. Its purpose is to cancel the corresponding BUI if it
691  * was still in the log. To do this it searches the AIL for the BUI with an id
692  * equal to that in the BUD format structure. If we find it we drop the BUD
693  * reference, which removes the BUI from the AIL and frees it.
694  */
695 STATIC int
696 xlog_recover_bud_commit_pass2(
697         struct xlog                     *log,
698         struct list_head                *buffer_list,
699         struct xlog_recover_item        *item,
700         xfs_lsn_t                       lsn)
701 {
702         struct xfs_bud_log_format       *bud_formatp;
703
704         bud_formatp = item->ri_buf[0].i_addr;
705         if (item->ri_buf[0].i_len != sizeof(struct xfs_bud_log_format)) {
706                 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
707                                 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
708                 return -EFSCORRUPTED;
709         }
710
711         xlog_recover_release_intent(log, XFS_LI_BUI, bud_formatp->bud_bui_id);
712         return 0;
713 }
714
715 const struct xlog_recover_item_ops xlog_bud_item_ops = {
716         .item_type              = XFS_LI_BUD,
717         .commit_pass2           = xlog_recover_bud_commit_pass2,
718 };