LCOV - code coverage report
Current view: top level - fs/xfs - xfs_refcount_item.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc3-achx @ Mon Jul 31 20:08:12 PDT 2023 Lines: 227 267 85.0 %
Date: 2023-07-31 20:08:12 Functions: 29 29 100.0 %

          Line data    Source code
       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_trans.h"
      16             : #include "xfs_trans_priv.h"
      17             : #include "xfs_refcount_item.h"
      18             : #include "xfs_log.h"
      19             : #include "xfs_refcount.h"
      20             : #include "xfs_error.h"
      21             : #include "xfs_log_priv.h"
      22             : #include "xfs_log_recover.h"
      23             : #include "xfs_ag.h"
      24             : 
      25             : struct kmem_cache       *xfs_cui_cache;
      26             : struct kmem_cache       *xfs_cud_cache;
      27             : 
      28             : static const struct xfs_item_ops xfs_cui_item_ops;
      29             : 
      30             : static inline struct xfs_cui_log_item *CUI_ITEM(struct xfs_log_item *lip)
      31             : {
      32             :         return container_of(lip, struct xfs_cui_log_item, cui_item);
      33             : }
      34             : 
      35             : STATIC void
      36   191180482 : xfs_cui_item_free(
      37             :         struct xfs_cui_log_item *cuip)
      38             : {
      39   191180482 :         kmem_free(cuip->cui_item.li_lv_shadow);
      40   191180672 :         if (cuip->cui_format.cui_nextents > XFS_CUI_MAX_FAST_EXTENTS)
      41           0 :                 kmem_free(cuip);
      42             :         else
      43   191180672 :                 kmem_cache_free(xfs_cui_cache, cuip);
      44   191180532 : }
      45             : 
      46             : /*
      47             :  * Freeing the CUI requires that we remove it from the AIL if it has already
      48             :  * been placed there. However, the CUI may not yet have been placed in the AIL
      49             :  * when called by xfs_cui_release() from CUD processing due to the ordering of
      50             :  * committed vs unpin operations in bulk insert operations. Hence the reference
      51             :  * count to ensure only the last caller frees the CUI.
      52             :  */
      53             : STATIC void
      54   382324189 : xfs_cui_release(
      55             :         struct xfs_cui_log_item *cuip)
      56             : {
      57   382324189 :         ASSERT(atomic_read(&cuip->cui_refcount) > 0);
      58   382324189 :         if (!atomic_dec_and_test(&cuip->cui_refcount))
      59             :                 return;
      60             : 
      61   191180593 :         xfs_trans_ail_delete(&cuip->cui_item, 0);
      62   191180725 :         xfs_cui_item_free(cuip);
      63             : }
      64             : 
      65             : 
      66             : STATIC void
      67   191140352 : xfs_cui_item_size(
      68             :         struct xfs_log_item     *lip,
      69             :         int                     *nvecs,
      70             :         int                     *nbytes)
      71             : {
      72   191140352 :         struct xfs_cui_log_item *cuip = CUI_ITEM(lip);
      73             : 
      74   191140352 :         *nvecs += 1;
      75   191140352 :         *nbytes += xfs_cui_log_format_sizeof(cuip->cui_format.cui_nextents);
      76   191140352 : }
      77             : 
      78             : /*
      79             :  * This is called to fill in the vector of log iovecs for the
      80             :  * given cui log item. We use only 1 iovec, and we point that
      81             :  * at the cui_log_format structure embedded in the cui item.
      82             :  * It is at this point that we assert that all of the extent
      83             :  * slots in the cui item have been filled.
      84             :  */
      85             : STATIC void
      86   191137471 : xfs_cui_item_format(
      87             :         struct xfs_log_item     *lip,
      88             :         struct xfs_log_vec      *lv)
      89             : {
      90   191137471 :         struct xfs_cui_log_item *cuip = CUI_ITEM(lip);
      91   191137471 :         struct xfs_log_iovec    *vecp = NULL;
      92             : 
      93   191137471 :         ASSERT(atomic_read(&cuip->cui_next_extent) ==
      94             :                         cuip->cui_format.cui_nextents);
      95             : 
      96   191137471 :         cuip->cui_format.cui_type = XFS_LI_CUI;
      97   191137471 :         cuip->cui_format.cui_size = 1;
      98             : 
      99   191137471 :         xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_CUI_FORMAT, &cuip->cui_format,
     100   191137471 :                         xfs_cui_log_format_sizeof(cuip->cui_format.cui_nextents));
     101   191132281 : }
     102             : 
     103             : /*
     104             :  * The unpin operation is the last place an CUI is manipulated in the log. It is
     105             :  * either inserted in the AIL or aborted in the event of a log I/O error. In
     106             :  * either case, the CUI transaction has been successfully committed to make it
     107             :  * this far. Therefore, we expect whoever committed the CUI to either construct
     108             :  * and commit the CUD or drop the CUD's reference in the event of error. Simply
     109             :  * drop the log's CUI reference now that the log is done with it.
     110             :  */
     111             : STATIC void
     112   191142482 : xfs_cui_item_unpin(
     113             :         struct xfs_log_item     *lip,
     114             :         int                     remove)
     115             : {
     116   191142482 :         struct xfs_cui_log_item *cuip = CUI_ITEM(lip);
     117             : 
     118   191142482 :         xfs_cui_release(cuip);
     119   191142961 : }
     120             : 
     121             : /*
     122             :  * The CUI has been either committed or aborted if the transaction has been
     123             :  * cancelled. If the transaction was cancelled, an CUD isn't going to be
     124             :  * constructed and thus we free the CUI here directly.
     125             :  */
     126             : STATIC void
     127       35668 : xfs_cui_item_release(
     128             :         struct xfs_log_item     *lip)
     129             : {
     130       35668 :         xfs_cui_release(CUI_ITEM(lip));
     131       35668 : }
     132             : 
     133             : /*
     134             :  * Allocate and initialize an cui item with the given number of extents.
     135             :  */
     136             : STATIC struct xfs_cui_log_item *
     137   191169420 : xfs_cui_init(
     138             :         struct xfs_mount                *mp,
     139             :         uint                            nextents)
     140             : 
     141             : {
     142   191169420 :         struct xfs_cui_log_item         *cuip;
     143             : 
     144   191169420 :         ASSERT(nextents > 0);
     145   191169420 :         if (nextents > XFS_CUI_MAX_FAST_EXTENTS)
     146           0 :                 cuip = kmem_zalloc(xfs_cui_log_item_sizeof(nextents),
     147             :                                 0);
     148             :         else
     149   191169420 :                 cuip = kmem_cache_zalloc(xfs_cui_cache,
     150             :                                          GFP_KERNEL | __GFP_NOFAIL);
     151             : 
     152   191178027 :         xfs_log_item_init(mp, &cuip->cui_item, XFS_LI_CUI, &xfs_cui_item_ops);
     153   191175027 :         cuip->cui_format.cui_nextents = nextents;
     154   191175027 :         cuip->cui_format.cui_id = (uintptr_t)(void *)cuip;
     155   191175027 :         atomic_set(&cuip->cui_next_extent, 0);
     156   191175027 :         atomic_set(&cuip->cui_refcount, 2);
     157             : 
     158   191175027 :         return cuip;
     159             : }
     160             : 
     161             : static inline struct xfs_cud_log_item *CUD_ITEM(struct xfs_log_item *lip)
     162             : {
     163             :         return container_of(lip, struct xfs_cud_log_item, cud_item);
     164             : }
     165             : 
     166             : STATIC void
     167   191137573 : xfs_cud_item_size(
     168             :         struct xfs_log_item     *lip,
     169             :         int                     *nvecs,
     170             :         int                     *nbytes)
     171             : {
     172   191137573 :         *nvecs += 1;
     173   191137573 :         *nbytes += sizeof(struct xfs_cud_log_format);
     174   191137573 : }
     175             : 
     176             : /*
     177             :  * This is called to fill in the vector of log iovecs for the
     178             :  * given cud log item. We use only 1 iovec, and we point that
     179             :  * at the cud_log_format structure embedded in the cud item.
     180             :  * It is at this point that we assert that all of the extent
     181             :  * slots in the cud item have been filled.
     182             :  */
     183             : STATIC void
     184      291523 : xfs_cud_item_format(
     185             :         struct xfs_log_item     *lip,
     186             :         struct xfs_log_vec      *lv)
     187             : {
     188      291523 :         struct xfs_cud_log_item *cudp = CUD_ITEM(lip);
     189      291523 :         struct xfs_log_iovec    *vecp = NULL;
     190             : 
     191      291523 :         cudp->cud_format.cud_type = XFS_LI_CUD;
     192      291523 :         cudp->cud_format.cud_size = 1;
     193             : 
     194      291523 :         xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_CUD_FORMAT, &cudp->cud_format,
     195             :                         sizeof(struct xfs_cud_log_format));
     196      291517 : }
     197             : 
     198             : /*
     199             :  * The CUD is either committed or aborted if the transaction is cancelled. If
     200             :  * the transaction is cancelled, drop our reference to the CUI and free the
     201             :  * CUD.
     202             :  */
     203             : STATIC void
     204   191139104 : xfs_cud_item_release(
     205             :         struct xfs_log_item     *lip)
     206             : {
     207   191139104 :         struct xfs_cud_log_item *cudp = CUD_ITEM(lip);
     208             : 
     209   191139104 :         xfs_cui_release(cudp->cud_cuip);
     210   191141217 :         kmem_free(cudp->cud_item.li_lv_shadow);
     211   191140279 :         kmem_cache_free(xfs_cud_cache, cudp);
     212   191139507 : }
     213             : 
     214             : static struct xfs_log_item *
     215   191111726 : xfs_cud_item_intent(
     216             :         struct xfs_log_item     *lip)
     217             : {
     218   191111726 :         return &CUD_ITEM(lip)->cud_cuip->cui_item;
     219             : }
     220             : 
     221             : static const struct xfs_item_ops xfs_cud_item_ops = {
     222             :         .flags          = XFS_ITEM_RELEASE_WHEN_COMMITTED |
     223             :                           XFS_ITEM_INTENT_DONE,
     224             :         .iop_size       = xfs_cud_item_size,
     225             :         .iop_format     = xfs_cud_item_format,
     226             :         .iop_release    = xfs_cud_item_release,
     227             :         .iop_intent     = xfs_cud_item_intent,
     228             : };
     229             : 
     230             : static struct xfs_cud_log_item *
     231   191136376 : xfs_trans_get_cud(
     232             :         struct xfs_trans                *tp,
     233             :         struct xfs_cui_log_item         *cuip)
     234             : {
     235   191136376 :         struct xfs_cud_log_item         *cudp;
     236             : 
     237   191136376 :         cudp = kmem_cache_zalloc(xfs_cud_cache, GFP_KERNEL | __GFP_NOFAIL);
     238   191137051 :         xfs_log_item_init(tp->t_mountp, &cudp->cud_item, XFS_LI_CUD,
     239             :                           &xfs_cud_item_ops);
     240   191132895 :         cudp->cud_cuip = cuip;
     241   191132895 :         cudp->cud_format.cud_cui_id = cuip->cui_format.cui_id;
     242             : 
     243   191132895 :         xfs_trans_add_item(tp, &cudp->cud_item);
     244   191137042 :         return cudp;
     245             : }
     246             : 
     247             : /*
     248             :  * Finish an refcount update and log it to the CUD. Note that the
     249             :  * transaction is marked dirty regardless of whether the refcount
     250             :  * update succeeds or fails to support the CUI/CUD lifecycle rules.
     251             :  */
     252             : static int
     253   193135479 : xfs_trans_log_finish_refcount_update(
     254             :         struct xfs_trans                *tp,
     255             :         struct xfs_cud_log_item         *cudp,
     256             :         struct xfs_refcount_intent      *ri,
     257             :         struct xfs_btree_cur            **pcur)
     258             : {
     259   193135479 :         int                             error;
     260             : 
     261   193135479 :         error = xfs_refcount_finish_one(tp, ri, pcur);
     262             : 
     263             :         /*
     264             :          * Mark the transaction dirty, even on error. This ensures the
     265             :          * transaction is aborted, which:
     266             :          *
     267             :          * 1.) releases the CUI and frees the CUD
     268             :          * 2.) shuts down the filesystem
     269             :          */
     270   193139640 :         tp->t_flags |= XFS_TRANS_DIRTY | XFS_TRANS_HAS_INTENT_DONE;
     271   193139640 :         set_bit(XFS_LI_DIRTY, &cudp->cud_item.li_flags);
     272             : 
     273   193143706 :         return error;
     274             : }
     275             : 
     276             : /* Sort refcount intents by AG. */
     277             : static int
     278     2027914 : xfs_refcount_update_diff_items(
     279             :         void                            *priv,
     280             :         const struct list_head          *a,
     281             :         const struct list_head          *b)
     282             : {
     283     2027914 :         struct xfs_refcount_intent      *ra;
     284     2027914 :         struct xfs_refcount_intent      *rb;
     285             : 
     286     2027914 :         ra = container_of(a, struct xfs_refcount_intent, ri_list);
     287     2027914 :         rb = container_of(b, struct xfs_refcount_intent, ri_list);
     288             : 
     289     2027914 :         return ra->ri_pag->pag_agno - rb->ri_pag->pag_agno;
     290             : }
     291             : 
     292             : /* Set the phys extent flags for this reverse mapping. */
     293             : static void
     294   193138405 : xfs_trans_set_refcount_flags(
     295             :         struct xfs_phys_extent          *pmap,
     296             :         enum xfs_refcount_intent_type   type)
     297             : {
     298   193138405 :         pmap->pe_flags = 0;
     299   193138405 :         switch (type) {
     300   193138405 :         case XFS_REFCOUNT_INCREASE:
     301             :         case XFS_REFCOUNT_DECREASE:
     302             :         case XFS_REFCOUNT_ALLOC_COW:
     303             :         case XFS_REFCOUNT_FREE_COW:
     304   193138405 :                 pmap->pe_flags |= type;
     305   193138405 :                 break;
     306           0 :         default:
     307           0 :                 ASSERT(0);
     308             :         }
     309   193138405 : }
     310             : 
     311             : /* Log refcount updates in the intent item. */
     312             : STATIC void
     313   193137663 : xfs_refcount_update_log_item(
     314             :         struct xfs_trans                *tp,
     315             :         struct xfs_cui_log_item         *cuip,
     316             :         struct xfs_refcount_intent      *ri)
     317             : {
     318   193137663 :         uint                            next_extent;
     319   193137663 :         struct xfs_phys_extent          *pmap;
     320             : 
     321   193137663 :         tp->t_flags |= XFS_TRANS_DIRTY;
     322   193137663 :         set_bit(XFS_LI_DIRTY, &cuip->cui_item.li_flags);
     323             : 
     324             :         /*
     325             :          * atomic_inc_return gives us the value after the increment;
     326             :          * we want to use it as an array index so we need to subtract 1 from
     327             :          * it.
     328             :          */
     329   193142084 :         next_extent = atomic_inc_return(&cuip->cui_next_extent) - 1;
     330   193138824 :         ASSERT(next_extent < cuip->cui_format.cui_nextents);
     331   193138824 :         pmap = &cuip->cui_format.cui_extents[next_extent];
     332   193138824 :         pmap->pe_startblock = ri->ri_startblock;
     333   193138824 :         pmap->pe_len = ri->ri_blockcount;
     334   193138824 :         xfs_trans_set_refcount_flags(pmap, ri->ri_type);
     335   193138028 : }
     336             : 
     337             : static struct xfs_log_item *
     338   191108397 : xfs_refcount_update_create_intent(
     339             :         struct xfs_trans                *tp,
     340             :         struct list_head                *items,
     341             :         unsigned int                    count,
     342             :         bool                            sort)
     343             : {
     344   191108397 :         struct xfs_mount                *mp = tp->t_mountp;
     345   191108397 :         struct xfs_cui_log_item         *cuip = xfs_cui_init(mp, count);
     346   191111743 :         struct xfs_refcount_intent      *ri;
     347             : 
     348   191111743 :         ASSERT(count > 0);
     349             : 
     350   191111743 :         xfs_trans_add_item(tp, &cuip->cui_item);
     351   191111694 :         if (sort)
     352   191110553 :                 list_sort(mp, items, xfs_refcount_update_diff_items);
     353   384251496 :         list_for_each_entry(ri, items, ri_list)
     354   193139953 :                 xfs_refcount_update_log_item(tp, cuip, ri);
     355   191111543 :         return &cuip->cui_item;
     356             : }
     357             : 
     358             : /* Get an CUD so we can process all the deferred refcount updates. */
     359             : static struct xfs_log_item *
     360   191111673 : xfs_refcount_update_create_done(
     361             :         struct xfs_trans                *tp,
     362             :         struct xfs_log_item             *intent,
     363             :         unsigned int                    count)
     364             : {
     365   191111673 :         return &xfs_trans_get_cud(tp, CUI_ITEM(intent))->cud_item;
     366             : }
     367             : 
     368             : /* Take a passive ref to the AG containing the space we're refcounting. */
     369             : void
     370   193138751 : xfs_refcount_update_get_group(
     371             :         struct xfs_mount                *mp,
     372             :         struct xfs_refcount_intent      *ri)
     373             : {
     374   193138751 :         xfs_agnumber_t                  agno;
     375             : 
     376   193138751 :         agno = XFS_FSB_TO_AGNO(mp, ri->ri_startblock);
     377   193138751 :         ri->ri_pag = xfs_perag_intent_get(mp, agno);
     378   193141708 : }
     379             : 
     380             : /* Release a passive AG ref after finishing refcounting work. */
     381             : static inline void
     382             : xfs_refcount_update_put_group(
     383             :         struct xfs_refcount_intent      *ri)
     384             : {
     385   193143141 :         xfs_perag_intent_put(ri->ri_pag);
     386        1743 : }
     387             : 
     388             : /* Process a deferred refcount update. */
     389             : STATIC int
     390   193134792 : xfs_refcount_update_finish_item(
     391             :         struct xfs_trans                *tp,
     392             :         struct xfs_log_item             *done,
     393             :         struct list_head                *item,
     394             :         struct xfs_btree_cur            **state)
     395             : {
     396   193134792 :         struct xfs_refcount_intent      *ri;
     397   193134792 :         int                             error;
     398             : 
     399   193134792 :         ri = container_of(item, struct xfs_refcount_intent, ri_list);
     400   193134792 :         error = xfs_trans_log_finish_refcount_update(tp, CUD_ITEM(done), ri,
     401             :                         state);
     402             : 
     403             :         /* Did we run out of reservation?  Requeue what we didn't finish. */
     404   193141055 :         if (!error && ri->ri_blockcount > 0) {
     405        1406 :                 ASSERT(ri->ri_type == XFS_REFCOUNT_INCREASE ||
     406             :                        ri->ri_type == XFS_REFCOUNT_DECREASE);
     407        1406 :                 return -EAGAIN;
     408             :         }
     409             : 
     410   193139649 :         xfs_refcount_update_put_group(ri);
     411   193139673 :         kmem_cache_free(xfs_refcount_intent_cache, ri);
     412   193139673 :         return error;
     413             : }
     414             : 
     415             : /* Abort all pending CUIs. */
     416             : STATIC void
     417        1737 : xfs_refcount_update_abort_intent(
     418             :         struct xfs_log_item             *intent)
     419             : {
     420        1737 :         xfs_cui_release(CUI_ITEM(intent));
     421        1737 : }
     422             : 
     423             : /* Cancel a deferred refcount update. */
     424             : STATIC void
     425        1749 : xfs_refcount_update_cancel_item(
     426             :         struct list_head                *item)
     427             : {
     428        1749 :         struct xfs_refcount_intent      *ri;
     429             : 
     430        1749 :         ri = container_of(item, struct xfs_refcount_intent, ri_list);
     431             : 
     432        1749 :         xfs_refcount_update_put_group(ri);
     433        1749 :         kmem_cache_free(xfs_refcount_intent_cache, ri);
     434        1749 : }
     435             : 
     436             : const struct xfs_defer_op_type xfs_refcount_update_defer_type = {
     437             :         .max_items      = XFS_CUI_MAX_FAST_EXTENTS,
     438             :         .create_intent  = xfs_refcount_update_create_intent,
     439             :         .abort_intent   = xfs_refcount_update_abort_intent,
     440             :         .create_done    = xfs_refcount_update_create_done,
     441             :         .finish_item    = xfs_refcount_update_finish_item,
     442             :         .finish_cleanup = xfs_refcount_finish_one_cleanup,
     443             :         .cancel_item    = xfs_refcount_update_cancel_item,
     444             : };
     445             : 
     446             : /* Is this recovered CUI ok? */
     447             : static inline bool
     448        1743 : xfs_cui_validate_phys(
     449             :         struct xfs_mount                *mp,
     450             :         struct xfs_phys_extent          *pmap)
     451             : {
     452        1743 :         if (!xfs_has_reflink(mp))
     453             :                 return false;
     454             : 
     455        1743 :         if (pmap->pe_flags & ~XFS_REFCOUNT_EXTENT_FLAGS)
     456             :                 return false;
     457             : 
     458        1743 :         switch (pmap->pe_flags & XFS_REFCOUNT_EXTENT_TYPE_MASK) {
     459             :         case XFS_REFCOUNT_INCREASE:
     460             :         case XFS_REFCOUNT_DECREASE:
     461             :         case XFS_REFCOUNT_ALLOC_COW:
     462             :         case XFS_REFCOUNT_FREE_COW:
     463        1743 :                 break;
     464             :         default:
     465             :                 return false;
     466             :         }
     467             : 
     468        1743 :         return xfs_verify_fsbext(mp, pmap->pe_startblock, pmap->pe_len);
     469             : }
     470             : 
     471             : /*
     472             :  * Process a refcount update intent item that was recovered from the log.
     473             :  * We need to update the refcountbt.
     474             :  */
     475             : STATIC int
     476        1735 : xfs_cui_item_recover(
     477             :         struct xfs_log_item             *lip,
     478             :         struct list_head                *capture_list)
     479             : {
     480        1735 :         struct xfs_cui_log_item         *cuip = CUI_ITEM(lip);
     481        1735 :         struct xfs_cud_log_item         *cudp;
     482        1735 :         struct xfs_trans                *tp;
     483        1735 :         struct xfs_btree_cur            *rcur = NULL;
     484        1735 :         struct xfs_mount                *mp = lip->li_log->l_mp;
     485        1735 :         unsigned int                    refc_type;
     486        1735 :         bool                            requeue_only = false;
     487        1735 :         int                             i;
     488        1735 :         int                             error = 0;
     489             : 
     490             :         /*
     491             :          * First check the validity of the extents described by the
     492             :          * CUI.  If any are bad, then assume that all are bad and
     493             :          * just toss the CUI.
     494             :          */
     495        3478 :         for (i = 0; i < cuip->cui_format.cui_nextents; i++) {
     496        1743 :                 if (!xfs_cui_validate_phys(mp,
     497             :                                         &cuip->cui_format.cui_extents[i])) {
     498           0 :                         XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
     499             :                                         &cuip->cui_format,
     500             :                                         sizeof(cuip->cui_format));
     501           0 :                         return -EFSCORRUPTED;
     502             :                 }
     503             :         }
     504             : 
     505             :         /*
     506             :          * Under normal operation, refcount updates are deferred, so we
     507             :          * wouldn't be adding them directly to a transaction.  All
     508             :          * refcount updates manage reservation usage internally and
     509             :          * dynamically by deferring work that won't fit in the
     510             :          * transaction.  Normally, any work that needs to be deferred
     511             :          * gets attached to the same defer_ops that scheduled the
     512             :          * refcount update.  However, we're in log recovery here, so we
     513             :          * use the passed in defer_ops and to finish up any work that
     514             :          * doesn't fit.  We need to reserve enough blocks to handle a
     515             :          * full btree split on either end of the refcount range.
     516             :          */
     517        1735 :         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate,
     518        1735 :                         mp->m_refc_maxlevels * 2, 0, XFS_TRANS_RESERVE, &tp);
     519        1735 :         if (error)
     520             :                 return error;
     521             : 
     522        1735 :         cudp = xfs_trans_get_cud(tp, cuip);
     523             : 
     524        5213 :         for (i = 0; i < cuip->cui_format.cui_nextents; i++) {
     525        1743 :                 struct xfs_refcount_intent      fake = { };
     526        1743 :                 struct xfs_phys_extent          *pmap;
     527             : 
     528        1743 :                 pmap = &cuip->cui_format.cui_extents[i];
     529        1743 :                 refc_type = pmap->pe_flags & XFS_REFCOUNT_EXTENT_TYPE_MASK;
     530        1743 :                 switch (refc_type) {
     531        1743 :                 case XFS_REFCOUNT_INCREASE:
     532             :                 case XFS_REFCOUNT_DECREASE:
     533             :                 case XFS_REFCOUNT_ALLOC_COW:
     534             :                 case XFS_REFCOUNT_FREE_COW:
     535        1743 :                         fake.ri_type = refc_type;
     536        1743 :                         break;
     537           0 :                 default:
     538           0 :                         XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
     539             :                                         &cuip->cui_format,
     540             :                                         sizeof(cuip->cui_format));
     541           0 :                         error = -EFSCORRUPTED;
     542           0 :                         goto abort_error;
     543             :                 }
     544             : 
     545        1743 :                 fake.ri_startblock = pmap->pe_startblock;
     546        1743 :                 fake.ri_blockcount = pmap->pe_len;
     547             : 
     548        1743 :                 if (!requeue_only) {
     549        1743 :                         xfs_refcount_update_get_group(mp, &fake);
     550        1743 :                         error = xfs_trans_log_finish_refcount_update(tp, cudp,
     551             :                                         &fake, &rcur);
     552        1743 :                         xfs_refcount_update_put_group(&fake);
     553             :                 }
     554        1743 :                 if (error == -EFSCORRUPTED)
     555           0 :                         XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
     556             :                                         &cuip->cui_format,
     557             :                                         sizeof(cuip->cui_format));
     558        1743 :                 if (error)
     559           0 :                         goto abort_error;
     560             : 
     561             :                 /* Requeue what we didn't finish. */
     562        1743 :                 if (fake.ri_blockcount > 0) {
     563           0 :                         struct xfs_bmbt_irec    irec = {
     564           0 :                                 .br_startblock  = fake.ri_startblock,
     565           0 :                                 .br_blockcount  = fake.ri_blockcount,
     566             :                         };
     567             : 
     568           0 :                         switch (fake.ri_type) {
     569           0 :                         case XFS_REFCOUNT_INCREASE:
     570           0 :                                 xfs_refcount_increase_extent(tp, &irec);
     571           0 :                                 break;
     572           0 :                         case XFS_REFCOUNT_DECREASE:
     573           0 :                                 xfs_refcount_decrease_extent(tp, &irec);
     574           0 :                                 break;
     575           0 :                         case XFS_REFCOUNT_ALLOC_COW:
     576           0 :                                 xfs_refcount_alloc_cow_extent(tp,
     577             :                                                 irec.br_startblock,
     578             :                                                 irec.br_blockcount);
     579           0 :                                 break;
     580           0 :                         case XFS_REFCOUNT_FREE_COW:
     581           0 :                                 xfs_refcount_free_cow_extent(tp,
     582             :                                                 irec.br_startblock,
     583             :                                                 irec.br_blockcount);
     584           0 :                                 break;
     585           0 :                         default:
     586           0 :                                 ASSERT(0);
     587             :                         }
     588           0 :                         requeue_only = true;
     589             :                 }
     590             :         }
     591             : 
     592        1735 :         xfs_refcount_finish_one_cleanup(tp, rcur, error);
     593        1735 :         return xfs_defer_ops_capture_and_commit(tp, capture_list);
     594             : 
     595             : abort_error:
     596           0 :         xfs_refcount_finish_one_cleanup(tp, rcur, error);
     597           0 :         xfs_trans_cancel(tp);
     598           0 :         return error;
     599             : }
     600             : 
     601             : STATIC bool
     602       37948 : xfs_cui_item_match(
     603             :         struct xfs_log_item     *lip,
     604             :         uint64_t                intent_id)
     605             : {
     606       37948 :         return CUI_ITEM(lip)->cui_format.cui_id == intent_id;
     607             : }
     608             : 
     609             : /* Relog an intent item to push the log tail forward. */
     610             : static struct xfs_log_item *
     611       26977 : xfs_cui_item_relog(
     612             :         struct xfs_log_item             *intent,
     613             :         struct xfs_trans                *tp)
     614             : {
     615       26977 :         struct xfs_cud_log_item         *cudp;
     616       26977 :         struct xfs_cui_log_item         *cuip;
     617       26977 :         struct xfs_phys_extent          *pmap;
     618       26977 :         unsigned int                    count;
     619             : 
     620       26977 :         count = CUI_ITEM(intent)->cui_format.cui_nextents;
     621       26977 :         pmap = CUI_ITEM(intent)->cui_format.cui_extents;
     622             : 
     623       26977 :         tp->t_flags |= XFS_TRANS_DIRTY;
     624       26977 :         cudp = xfs_trans_get_cud(tp, CUI_ITEM(intent));
     625       26977 :         set_bit(XFS_LI_DIRTY, &cudp->cud_item.li_flags);
     626             : 
     627       26977 :         cuip = xfs_cui_init(tp->t_mountp, count);
     628       53954 :         memcpy(cuip->cui_format.cui_extents, pmap, count * sizeof(*pmap));
     629       26977 :         atomic_set(&cuip->cui_next_extent, count);
     630       26977 :         xfs_trans_add_item(tp, &cuip->cui_item);
     631       26977 :         set_bit(XFS_LI_DIRTY, &cuip->cui_item.li_flags);
     632       26977 :         return &cuip->cui_item;
     633             : }
     634             : 
     635             : static const struct xfs_item_ops xfs_cui_item_ops = {
     636             :         .flags          = XFS_ITEM_INTENT,
     637             :         .iop_size       = xfs_cui_item_size,
     638             :         .iop_format     = xfs_cui_item_format,
     639             :         .iop_unpin      = xfs_cui_item_unpin,
     640             :         .iop_release    = xfs_cui_item_release,
     641             :         .iop_recover    = xfs_cui_item_recover,
     642             :         .iop_match      = xfs_cui_item_match,
     643             :         .iop_relog      = xfs_cui_item_relog,
     644             : };
     645             : 
     646             : static inline void
     647       37396 : xfs_cui_copy_format(
     648             :         struct xfs_cui_log_format       *dst,
     649             :         const struct xfs_cui_log_format *src)
     650             : {
     651       37396 :         unsigned int                    i;
     652             : 
     653       74792 :         memcpy(dst, src, offsetof(struct xfs_cui_log_format, cui_extents));
     654             : 
     655       74886 :         for (i = 0; i < src->cui_nextents; i++)
     656       74980 :                 memcpy(&dst->cui_extents[i], &src->cui_extents[i],
     657             :                                 sizeof(struct xfs_phys_extent));
     658       37396 : }
     659             : 
     660             : /*
     661             :  * This routine is called to create an in-core extent refcount update
     662             :  * item from the cui format structure which was logged on disk.
     663             :  * It allocates an in-core cui, copies the extents from the format
     664             :  * structure into it, and adds the cui to the AIL with the given
     665             :  * LSN.
     666             :  */
     667             : STATIC int
     668       37396 : xlog_recover_cui_commit_pass2(
     669             :         struct xlog                     *log,
     670             :         struct list_head                *buffer_list,
     671             :         struct xlog_recover_item        *item,
     672             :         xfs_lsn_t                       lsn)
     673             : {
     674       37396 :         struct xfs_mount                *mp = log->l_mp;
     675       37396 :         struct xfs_cui_log_item         *cuip;
     676       37396 :         struct xfs_cui_log_format       *cui_formatp;
     677       37396 :         size_t                          len;
     678             : 
     679       37396 :         cui_formatp = item->ri_buf[0].i_addr;
     680             : 
     681       37396 :         if (item->ri_buf[0].i_len < xfs_cui_log_format_sizeof(0)) {
     682           0 :                 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
     683             :                                 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
     684           0 :                 return -EFSCORRUPTED;
     685             :         }
     686             : 
     687       37396 :         len = xfs_cui_log_format_sizeof(cui_formatp->cui_nextents);
     688       37396 :         if (item->ri_buf[0].i_len != len) {
     689           0 :                 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
     690             :                                 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
     691           0 :                 return -EFSCORRUPTED;
     692             :         }
     693             : 
     694       37396 :         cuip = xfs_cui_init(mp, cui_formatp->cui_nextents);
     695       37396 :         xfs_cui_copy_format(&cuip->cui_format, cui_formatp);
     696       37396 :         atomic_set(&cuip->cui_next_extent, cui_formatp->cui_nextents);
     697             :         /*
     698             :          * Insert the intent into the AIL directly and drop one reference so
     699             :          * that finishing or canceling the work will drop the other.
     700             :          */
     701       37396 :         xfs_trans_ail_insert(log->l_ailp, &cuip->cui_item, lsn);
     702       37396 :         xfs_cui_release(cuip);
     703       37396 :         return 0;
     704             : }
     705             : 
     706             : const struct xlog_recover_item_ops xlog_cui_item_ops = {
     707             :         .item_type              = XFS_LI_CUI,
     708             :         .commit_pass2           = xlog_recover_cui_commit_pass2,
     709             : };
     710             : 
     711             : /*
     712             :  * This routine is called when an CUD format structure is found in a committed
     713             :  * transaction in the log. Its purpose is to cancel the corresponding CUI if it
     714             :  * was still in the log. To do this it searches the AIL for the CUI with an id
     715             :  * equal to that in the CUD format structure. If we find it we drop the CUD
     716             :  * reference, which removes the CUI from the AIL and frees it.
     717             :  */
     718             : STATIC int
     719       35786 : xlog_recover_cud_commit_pass2(
     720             :         struct xlog                     *log,
     721             :         struct list_head                *buffer_list,
     722             :         struct xlog_recover_item        *item,
     723             :         xfs_lsn_t                       lsn)
     724             : {
     725       35786 :         struct xfs_cud_log_format       *cud_formatp;
     726             : 
     727       35786 :         cud_formatp = item->ri_buf[0].i_addr;
     728       35786 :         if (item->ri_buf[0].i_len != sizeof(struct xfs_cud_log_format)) {
     729           0 :                 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
     730             :                                 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
     731           0 :                 return -EFSCORRUPTED;
     732             :         }
     733             : 
     734       35786 :         xlog_recover_release_intent(log, XFS_LI_CUI, cud_formatp->cud_cui_id);
     735       35786 :         return 0;
     736             : }
     737             : 
     738             : const struct xlog_recover_item_ops xlog_cud_item_ops = {
     739             :         .item_type              = XFS_LI_CUD,
     740             :         .commit_pass2           = xlog_recover_cud_commit_pass2,
     741             : };

Generated by: LCOV version 1.14