LCOV - code coverage report
Current view: top level - fs/xfs/libxfs - xfs_rtrefcount_btree.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc4-xfsa @ Mon Jul 31 20:08:27 PDT 2023 Lines: 295 317 93.1 %
Date: 2023-07-31 20:08:27 Functions: 32 35 91.4 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0-or-later
       2             : /*
       3             :  * Copyright (C) 2021-2023 Oracle.  All Rights Reserved.
       4             :  * Author: Darrick J. Wong <djwong@kernel.org>
       5             :  */
       6             : #include "xfs.h"
       7             : #include "xfs_fs.h"
       8             : #include "xfs_shared.h"
       9             : #include "xfs_format.h"
      10             : #include "xfs_log_format.h"
      11             : #include "xfs_trans_resv.h"
      12             : #include "xfs_bit.h"
      13             : #include "xfs_sb.h"
      14             : #include "xfs_mount.h"
      15             : #include "xfs_defer.h"
      16             : #include "xfs_inode.h"
      17             : #include "xfs_trans.h"
      18             : #include "xfs_alloc.h"
      19             : #include "xfs_btree.h"
      20             : #include "xfs_btree_staging.h"
      21             : #include "xfs_rtrefcount_btree.h"
      22             : #include "xfs_refcount.h"
      23             : #include "xfs_trace.h"
      24             : #include "xfs_cksum.h"
      25             : #include "xfs_error.h"
      26             : #include "xfs_extent_busy.h"
      27             : #include "xfs_rtgroup.h"
      28             : #include "xfs_rtbitmap.h"
      29             : #include "xfs_imeta.h"
      30             : #include "xfs_health.h"
      31             : 
      32             : static struct kmem_cache        *xfs_rtrefcountbt_cur_cache;
      33             : 
      34             : /*
      35             :  * Realtime Reference Count btree.
      36             :  *
      37             :  * This is a btree used to track the owner(s) of a given extent in the realtime
      38             :  * device.  See the comments in xfs_refcount_btree.c for more information.
      39             :  *
      40             :  * This tree is basically the same as the regular refcount btree except that
      41             :  * it's rooted in an inode.
      42             :  */
      43             : 
      44             : static struct xfs_btree_cur *
      45      668190 : xfs_rtrefcountbt_dup_cursor(
      46             :         struct xfs_btree_cur    *cur)
      47             : {
      48      668190 :         struct xfs_btree_cur    *new;
      49             : 
      50      668190 :         new = xfs_rtrefcountbt_init_cursor(cur->bc_mp, cur->bc_tp,
      51             :                         cur->bc_ino.rtg, cur->bc_ino.ip);
      52             : 
      53             :         /* Copy the flags values since init cursor doesn't get them. */
      54      668190 :         new->bc_ino.flags = cur->bc_ino.flags;
      55             : 
      56      668190 :         return new;
      57             : }
      58             : 
      59             : STATIC int
      60     2531408 : xfs_rtrefcountbt_get_minrecs(
      61             :         struct xfs_btree_cur    *cur,
      62             :         int                     level)
      63             : {
      64     2531408 :         if (level == cur->bc_nlevels - 1) {
      65       85809 :                 struct xfs_ifork        *ifp = xfs_btree_ifork_ptr(cur);
      66             : 
      67       85809 :                 return xfs_rtrefcountbt_maxrecs(cur->bc_mp, ifp->if_broot_bytes,
      68       85809 :                                 level == 0) / 2;
      69             :         }
      70             : 
      71     2445599 :         return cur->bc_mp->m_rtrefc_mnr[level != 0];
      72             : }
      73             : 
      74             : STATIC int
      75  2953115125 : xfs_rtrefcountbt_get_maxrecs(
      76             :         struct xfs_btree_cur    *cur,
      77             :         int                     level)
      78             : {
      79  2953115125 :         if (level == cur->bc_nlevels - 1) {
      80   823559188 :                 struct xfs_ifork        *ifp = xfs_btree_ifork_ptr(cur);
      81             : 
      82   823572143 :                 return xfs_rtrefcountbt_maxrecs(cur->bc_mp, ifp->if_broot_bytes,
      83             :                                 level == 0);
      84             :         }
      85             : 
      86  2129555937 :         return cur->bc_mp->m_rtrefc_mxr[level != 0];
      87             : }
      88             : 
      89             : /*
      90             :  * Calculate number of records in a realtime refcount btree inode root.
      91             :  */
      92             : unsigned int
      93           0 : xfs_rtrefcountbt_droot_maxrecs(
      94             :         unsigned int            blocklen,
      95             :         bool                    leaf)
      96             : {
      97      626624 :         blocklen -= sizeof(struct xfs_rtrefcount_root);
      98             : 
      99           0 :         if (leaf)
     100      451777 :                 return blocklen / sizeof(struct xfs_refcount_rec);
     101      152800 :         return blocklen / (2 * sizeof(struct xfs_refcount_key) +
     102             :                         sizeof(xfs_rtrefcount_ptr_t));
     103             : }
     104             : 
     105             : /*
     106             :  * Get the maximum records we could store in the on-disk format.
     107             :  *
     108             :  * For non-root nodes this is equivalent to xfs_rtrefcountbt_get_maxrecs, but
     109             :  * for the root node this checks the available space in the dinode fork so that
     110             :  * we can resize the in-memory buffer to match it.  After a resize to the
     111             :  * maximum size this function returns the same value as
     112             :  * xfs_rtrefcountbt_get_maxrecs for the root node, too.
     113             :  */
     114             : STATIC int
     115      621709 : xfs_rtrefcountbt_get_dmaxrecs(
     116             :         struct xfs_btree_cur    *cur,
     117             :         int                     level)
     118             : {
     119      621709 :         if (level != cur->bc_nlevels - 1)
     120       17132 :                 return cur->bc_mp->m_rtrefc_mxr[level != 0];
     121     1209154 :         return xfs_rtrefcountbt_droot_maxrecs(cur->bc_ino.forksize, level == 0);
     122             : }
     123             : 
     124             : STATIC void
     125  6767994363 : xfs_rtrefcountbt_init_key_from_rec(
     126             :         union xfs_btree_key             *key,
     127             :         const union xfs_btree_rec       *rec)
     128             : {
     129  6767994363 :         key->refc.rc_startblock = rec->refc.rc_startblock;
     130  6767994363 : }
     131             : 
     132             : STATIC void
     133    43309726 : xfs_rtrefcountbt_init_high_key_from_rec(
     134             :         union xfs_btree_key             *key,
     135             :         const union xfs_btree_rec       *rec)
     136             : {
     137    43309726 :         __u32                           x;
     138             : 
     139    43309726 :         x = be32_to_cpu(rec->refc.rc_startblock);
     140    43309726 :         x += be32_to_cpu(rec->refc.rc_blockcount) - 1;
     141    43309726 :         key->refc.rc_startblock = cpu_to_be32(x);
     142    43309726 : }
     143             : 
     144             : STATIC void
     145   181524577 : xfs_rtrefcountbt_init_rec_from_cur(
     146             :         struct xfs_btree_cur    *cur,
     147             :         union xfs_btree_rec     *rec)
     148             : {
     149   181524577 :         const struct xfs_refcount_irec *irec = &cur->bc_rec.rc;
     150   181524577 :         uint32_t                start;
     151             : 
     152   181524577 :         start = xfs_refcount_encode_startblock(irec->rc_startblock,
     153   181524577 :                         irec->rc_domain);
     154   181524577 :         rec->refc.rc_startblock = cpu_to_be32(start);
     155   181524577 :         rec->refc.rc_blockcount = cpu_to_be32(cur->bc_rec.rc.rc_blockcount);
     156   181524577 :         rec->refc.rc_refcount = cpu_to_be32(cur->bc_rec.rc.rc_refcount);
     157   181524577 : }
     158             : 
     159             : STATIC void
     160   829932056 : xfs_rtrefcountbt_init_ptr_from_cur(
     161             :         struct xfs_btree_cur    *cur,
     162             :         union xfs_btree_ptr     *ptr)
     163             : {
     164   829932056 :         ptr->l = 0;
     165   829932056 : }
     166             : 
     167             : STATIC int64_t
     168  8722781657 : xfs_rtrefcountbt_key_diff(
     169             :         struct xfs_btree_cur            *cur,
     170             :         const union xfs_btree_key       *key)
     171             : {
     172  8722781657 :         const struct xfs_refcount_key   *kp = &key->refc;
     173  8722781657 :         const struct xfs_refcount_irec  *irec = &cur->bc_rec.rc;
     174  8722781657 :         uint32_t                        start;
     175             : 
     176  8722781657 :         start = xfs_refcount_encode_startblock(irec->rc_startblock,
     177  8722781657 :                         irec->rc_domain);
     178  8722781657 :         return (int64_t)be32_to_cpu(kp->rc_startblock) - start;
     179             : }
     180             : 
     181             : STATIC int64_t
     182   170460846 : xfs_rtrefcountbt_diff_two_keys(
     183             :         struct xfs_btree_cur            *cur,
     184             :         const union xfs_btree_key       *k1,
     185             :         const union xfs_btree_key       *k2,
     186             :         const union xfs_btree_key       *mask)
     187             : {
     188   170460846 :         ASSERT(!mask || mask->refc.rc_startblock);
     189             : 
     190   170460846 :         return (int64_t)be32_to_cpu(k1->refc.rc_startblock) -
     191   170460846 :                         be32_to_cpu(k2->refc.rc_startblock);
     192             : }
     193             : 
     194             : static xfs_failaddr_t
     195      518777 : xfs_rtrefcountbt_verify(
     196             :         struct xfs_buf          *bp)
     197             : {
     198      518777 :         struct xfs_mount        *mp = bp->b_target->bt_mount;
     199      518777 :         struct xfs_btree_block  *block = XFS_BUF_TO_BLOCK(bp);
     200      518777 :         xfs_failaddr_t          fa;
     201      518777 :         int                     level;
     202             : 
     203      518777 :         if (!xfs_verify_magic(bp, block->bb_magic))
     204           0 :                 return __this_address;
     205             : 
     206      518777 :         if (!xfs_has_reflink(mp))
     207           0 :                 return __this_address;
     208      518777 :         fa = xfs_btree_lblock_v5hdr_verify(bp, XFS_RMAP_OWN_UNKNOWN);
     209      518777 :         if (fa)
     210             :                 return fa;
     211      518777 :         level = be16_to_cpu(block->bb_level);
     212      518777 :         if (level > mp->m_rtrefc_maxlevels)
     213           0 :                 return __this_address;
     214             : 
     215      518777 :         return xfs_btree_lblock_verify(bp, mp->m_rtrefc_mxr[level != 0]);
     216             : }
     217             : 
     218             : static void
     219        5475 : xfs_rtrefcountbt_read_verify(
     220             :         struct xfs_buf  *bp)
     221             : {
     222        5475 :         xfs_failaddr_t  fa;
     223             : 
     224        5475 :         if (!xfs_btree_lblock_verify_crc(bp))
     225           0 :                 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
     226             :         else {
     227        5475 :                 fa = xfs_rtrefcountbt_verify(bp);
     228        5475 :                 if (fa)
     229           0 :                         xfs_verifier_error(bp, -EFSCORRUPTED, fa);
     230             :         }
     231             : 
     232        5475 :         if (bp->b_error)
     233           0 :                 trace_xfs_btree_corrupt(bp, _RET_IP_);
     234        5475 : }
     235             : 
     236             : static void
     237      226839 : xfs_rtrefcountbt_write_verify(
     238             :         struct xfs_buf  *bp)
     239             : {
     240      226839 :         xfs_failaddr_t  fa;
     241             : 
     242      226839 :         fa = xfs_rtrefcountbt_verify(bp);
     243      226839 :         if (fa) {
     244           0 :                 trace_xfs_btree_corrupt(bp, _RET_IP_);
     245           0 :                 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
     246           0 :                 return;
     247             :         }
     248      226839 :         xfs_btree_lblock_calc_crc(bp);
     249             : 
     250             : }
     251             : 
     252             : const struct xfs_buf_ops xfs_rtrefcountbt_buf_ops = {
     253             :         .name                   = "xfs_rtrefcountbt",
     254             :         .magic                  = { 0, cpu_to_be32(XFS_RTREFC_CRC_MAGIC) },
     255             :         .verify_read            = xfs_rtrefcountbt_read_verify,
     256             :         .verify_write           = xfs_rtrefcountbt_write_verify,
     257             :         .verify_struct          = xfs_rtrefcountbt_verify,
     258             : };
     259             : 
     260             : STATIC int
     261      146076 : xfs_rtrefcountbt_keys_inorder(
     262             :         struct xfs_btree_cur            *cur,
     263             :         const union xfs_btree_key       *k1,
     264             :         const union xfs_btree_key       *k2)
     265             : {
     266      146076 :         return be32_to_cpu(k1->refc.rc_startblock) <
     267      146076 :                be32_to_cpu(k2->refc.rc_startblock);
     268             : }
     269             : 
     270             : STATIC int
     271    56935885 : xfs_rtrefcountbt_recs_inorder(
     272             :         struct xfs_btree_cur            *cur,
     273             :         const union xfs_btree_rec       *r1,
     274             :         const union xfs_btree_rec       *r2)
     275             : {
     276    56935885 :         return  be32_to_cpu(r1->refc.rc_startblock) +
     277    56935885 :                 be32_to_cpu(r1->refc.rc_blockcount) <=
     278    56935885 :                 be32_to_cpu(r2->refc.rc_startblock);
     279             : }
     280             : 
     281             : STATIC enum xbtree_key_contig
     282           0 : xfs_rtrefcountbt_keys_contiguous(
     283             :         struct xfs_btree_cur            *cur,
     284             :         const union xfs_btree_key       *key1,
     285             :         const union xfs_btree_key       *key2,
     286             :         const union xfs_btree_key       *mask)
     287             : {
     288           0 :         ASSERT(!mask || mask->refc.rc_startblock);
     289             : 
     290           0 :         return xbtree_key_contig(be32_to_cpu(key1->refc.rc_startblock),
     291           0 :                                  be32_to_cpu(key2->refc.rc_startblock));
     292             : }
     293             : 
     294             : /* Move the rt refcount btree root from one incore buffer to another. */
     295             : static void
     296      814235 : xfs_rtrefcountbt_broot_move(
     297             :         struct xfs_inode        *ip,
     298             :         int                     whichfork,
     299             :         struct xfs_btree_block  *dst_broot,
     300             :         size_t                  dst_bytes,
     301             :         struct xfs_btree_block  *src_broot,
     302             :         size_t                  src_bytes,
     303             :         unsigned int            level,
     304             :         unsigned int            numrecs)
     305             : {
     306      814235 :         struct xfs_mount        *mp = ip->i_mount;
     307      814235 :         void                    *dptr;
     308      814235 :         void                    *sptr;
     309             : 
     310      814235 :         ASSERT(xfs_rtrefcount_droot_space(src_broot) <=
     311             :                         xfs_inode_fork_size(ip, whichfork));
     312             : 
     313             :         /*
     314             :          * We always have to move the pointers because they are not butted
     315             :          * against the btree block header.
     316             :          */
     317      814235 :         if (numrecs && level > 0) {
     318        2570 :                 sptr = xfs_rtrefcount_broot_ptr_addr(mp, src_broot, 1,
     319             :                                 src_bytes);
     320        2570 :                 dptr = xfs_rtrefcount_broot_ptr_addr(mp, dst_broot, 1,
     321             :                                 dst_bytes);
     322        5140 :                 memmove(dptr, sptr, numrecs * sizeof(xfs_fsblock_t));
     323             :         }
     324             : 
     325      814235 :         if (src_broot == dst_broot)
     326             :                 return;
     327             : 
     328             :         /*
     329             :          * If the root is being totally relocated, we have to migrate the block
     330             :          * header and the keys/records that come after it.
     331             :          */
     332      809520 :         memcpy(dst_broot, src_broot, XFS_RTREFCOUNT_BLOCK_LEN);
     333             : 
     334      404760 :         if (!numrecs)
     335             :                 return;
     336             : 
     337      366493 :         if (level == 0) {
     338      365702 :                 sptr = xfs_rtrefcount_rec_addr(src_broot, 1);
     339      365702 :                 dptr = xfs_rtrefcount_rec_addr(dst_broot, 1);
     340      731404 :                 memcpy(dptr, sptr,
     341             :                                 numrecs * sizeof(struct xfs_refcount_rec));
     342             :         } else {
     343         791 :                 sptr = xfs_rtrefcount_key_addr(src_broot, 1);
     344         791 :                 dptr = xfs_rtrefcount_key_addr(dst_broot, 1);
     345        1582 :                 memcpy(dptr, sptr,
     346             :                                 numrecs * sizeof(struct xfs_refcount_key));
     347             :         }
     348             : }
     349             : 
     350             : static const struct xfs_ifork_broot_ops xfs_rtrefcountbt_iroot_ops = {
     351             :         .maxrecs                = xfs_rtrefcountbt_maxrecs,
     352             :         .size                   = xfs_rtrefcount_broot_space_calc,
     353             :         .move                   = xfs_rtrefcountbt_broot_move,
     354             : };
     355             : 
     356             : const struct xfs_btree_ops xfs_rtrefcountbt_ops = {
     357             :         .rec_len                = sizeof(struct xfs_refcount_rec),
     358             :         .key_len                = sizeof(struct xfs_refcount_key),
     359             :         .lru_refs               = XFS_REFC_BTREE_REF,
     360             :         .geom_flags             = XFS_BTREE_LONG_PTRS | XFS_BTREE_ROOT_IN_INODE |
     361             :                                   XFS_BTREE_CRC_BLOCKS | XFS_BTREE_IROOT_RECORDS,
     362             : 
     363             :         .dup_cursor             = xfs_rtrefcountbt_dup_cursor,
     364             :         .alloc_block            = xfs_btree_alloc_imeta_block,
     365             :         .free_block             = xfs_btree_free_imeta_block,
     366             :         .get_minrecs            = xfs_rtrefcountbt_get_minrecs,
     367             :         .get_maxrecs            = xfs_rtrefcountbt_get_maxrecs,
     368             :         .get_dmaxrecs           = xfs_rtrefcountbt_get_dmaxrecs,
     369             :         .init_key_from_rec      = xfs_rtrefcountbt_init_key_from_rec,
     370             :         .init_high_key_from_rec = xfs_rtrefcountbt_init_high_key_from_rec,
     371             :         .init_rec_from_cur      = xfs_rtrefcountbt_init_rec_from_cur,
     372             :         .init_ptr_from_cur      = xfs_rtrefcountbt_init_ptr_from_cur,
     373             :         .key_diff               = xfs_rtrefcountbt_key_diff,
     374             :         .buf_ops                = &xfs_rtrefcountbt_buf_ops,
     375             :         .diff_two_keys          = xfs_rtrefcountbt_diff_two_keys,
     376             :         .keys_inorder           = xfs_rtrefcountbt_keys_inorder,
     377             :         .recs_inorder           = xfs_rtrefcountbt_recs_inorder,
     378             :         .keys_contiguous        = xfs_rtrefcountbt_keys_contiguous,
     379             :         .iroot_ops              = &xfs_rtrefcountbt_iroot_ops,
     380             : };
     381             : 
     382             : /* Initialize a new rt refcount btree cursor. */
     383             : static struct xfs_btree_cur *
     384    92943567 : xfs_rtrefcountbt_init_common(
     385             :         struct xfs_mount        *mp,
     386             :         struct xfs_trans        *tp,
     387             :         struct xfs_rtgroup      *rtg,
     388             :         struct xfs_inode        *ip)
     389             : {
     390    92943567 :         struct xfs_btree_cur    *cur;
     391             : 
     392    92943567 :         ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
     393             : 
     394    92962296 :         cur = xfs_btree_alloc_cursor(mp, tp, XFS_BTNUM_RTREFC,
     395    92962296 :                         &xfs_rtrefcountbt_ops, mp->m_rtrefc_maxlevels,
     396             :                         xfs_rtrefcountbt_cur_cache);
     397    92985183 :         cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_refcbt_2);
     398             : 
     399    92985183 :         cur->bc_ino.ip = ip;
     400    92985183 :         cur->bc_ino.allocated = 0;
     401    92985183 :         cur->bc_ino.flags = 0;
     402    92985183 :         cur->bc_ino.refc.nr_ops = 0;
     403    92985183 :         cur->bc_ino.refc.shape_changes = 0;
     404             : 
     405    92985183 :         cur->bc_ino.rtg = xfs_rtgroup_hold(rtg);
     406    92980726 :         return cur;
     407             : }
     408             : 
     409             : /* Allocate a new rt refcount btree cursor. */
     410             : struct xfs_btree_cur *
     411    92959425 : xfs_rtrefcountbt_init_cursor(
     412             :         struct xfs_mount        *mp,
     413             :         struct xfs_trans        *tp,
     414             :         struct xfs_rtgroup      *rtg,
     415             :         struct xfs_inode        *ip)
     416             : {
     417    92959425 :         struct xfs_btree_cur    *cur;
     418    92959425 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK);
     419             : 
     420    92959425 :         cur = xfs_rtrefcountbt_init_common(mp, tp, rtg, ip);
     421    92953656 :         cur->bc_nlevels = be16_to_cpu(ifp->if_broot->bb_level) + 1;
     422    92953656 :         cur->bc_ino.forksize = xfs_inode_fork_size(ip, XFS_DATA_FORK);
     423    92953656 :         cur->bc_ino.whichfork = XFS_DATA_FORK;
     424    92953656 :         return cur;
     425             : }
     426             : 
     427             : /* Create a new rt reverse mapping btree cursor with a fake root for staging. */
     428             : struct xfs_btree_cur *
     429       15241 : xfs_rtrefcountbt_stage_cursor(
     430             :         struct xfs_mount        *mp,
     431             :         struct xfs_rtgroup      *rtg,
     432             :         struct xfs_inode        *ip,
     433             :         struct xbtree_ifakeroot *ifake)
     434             : {
     435       15241 :         struct xfs_btree_cur    *cur;
     436             : 
     437       15241 :         cur = xfs_rtrefcountbt_init_common(mp, NULL, rtg, ip);
     438       15240 :         cur->bc_nlevels = ifake->if_levels;
     439       15240 :         cur->bc_ino.forksize = ifake->if_fork_size;
     440       15240 :         cur->bc_ino.whichfork = -1;
     441       15240 :         xfs_btree_stage_ifakeroot(cur, ifake, NULL);
     442       15240 :         return cur;
     443             : }
     444             : 
     445             : /*
     446             :  * Install a new rt reverse mapping btree root.  Caller is responsible for
     447             :  * invalidating and freeing the old btree blocks.
     448             :  */
     449             : void
     450       15234 : xfs_rtrefcountbt_commit_staged_btree(
     451             :         struct xfs_btree_cur    *cur,
     452             :         struct xfs_trans        *tp)
     453             : {
     454       15234 :         struct xbtree_ifakeroot *ifake = cur->bc_ino.ifake;
     455       15234 :         struct xfs_ifork        *ifp;
     456       15234 :         int                     flags = XFS_ILOG_CORE | XFS_ILOG_DBROOT;
     457             : 
     458       15234 :         ASSERT(cur->bc_flags & XFS_BTREE_STAGING);
     459       15234 :         ASSERT(ifake->if_fork->if_format == XFS_DINODE_FMT_REFCOUNT);
     460             : 
     461             :         /*
     462             :          * Free any resources hanging off the real fork, then shallow-copy the
     463             :          * staging fork's contents into the real fork to transfer everything
     464             :          * we just built.
     465             :          */
     466       15234 :         ifp = xfs_ifork_ptr(cur->bc_ino.ip, XFS_DATA_FORK);
     467       15234 :         xfs_idestroy_fork(ifp);
     468       30472 :         memcpy(ifp, ifake->if_fork, sizeof(struct xfs_ifork));
     469             : 
     470       15236 :         xfs_trans_log_inode(tp, cur->bc_ino.ip, flags);
     471       15240 :         xfs_btree_commit_ifakeroot(cur, tp, XFS_DATA_FORK,
     472             :                         &xfs_rtrefcountbt_ops);
     473       15239 : }
     474             : 
     475             : /* Calculate number of records in a realtime refcount btree block. */
     476             : static inline unsigned int
     477             : xfs_rtrefcountbt_block_maxrecs(
     478             :         unsigned int            blocklen,
     479             :         bool                    leaf)
     480             : {
     481             : 
     482   824545490 :         if (leaf)
     483     1304995 :                 return blocklen / sizeof(struct xfs_refcount_rec);
     484   823215878 :         return blocklen / (sizeof(struct xfs_refcount_key) +
     485             :                            sizeof(xfs_rtrefcount_ptr_t));
     486             : }
     487             : 
     488             : /*
     489             :  * Calculate number of records in an refcount btree block.
     490             :  */
     491             : unsigned int
     492      862921 : xfs_rtrefcountbt_maxrecs(
     493             :         struct xfs_mount        *mp,
     494             :         unsigned int            blocklen,
     495             :         bool                    leaf)
     496             : {
     497   824545490 :         blocklen -= XFS_RTREFCOUNT_BLOCK_LEN;
     498   824545490 :         return xfs_rtrefcountbt_block_maxrecs(blocklen, leaf);
     499             : }
     500             : 
     501             : /* Compute the max possible height for realtime refcount btrees. */
     502             : unsigned int
     503          12 : xfs_rtrefcountbt_maxlevels_ondisk(void)
     504             : {
     505          12 :         unsigned int            minrecs[2];
     506          12 :         unsigned int            blocklen;
     507             : 
     508          12 :         blocklen = XFS_MIN_CRC_BLOCKSIZE - XFS_BTREE_LBLOCK_CRC_LEN;
     509             : 
     510          12 :         minrecs[0] = xfs_rtrefcountbt_block_maxrecs(blocklen, true) / 2;
     511          12 :         minrecs[1] = xfs_rtrefcountbt_block_maxrecs(blocklen, false) / 2;
     512             : 
     513             :         /* We need at most one record for every block in an rt group. */
     514          12 :         return xfs_btree_compute_maxlevels(minrecs, XFS_MAX_RGBLOCKS);
     515             : }
     516             : 
     517             : int __init
     518          12 : xfs_rtrefcountbt_init_cur_cache(void)
     519             : {
     520          12 :         xfs_rtrefcountbt_cur_cache = kmem_cache_create("xfs_rtrefcountbt_cur",
     521          12 :                         xfs_btree_cur_sizeof(
     522             :                                         xfs_rtrefcountbt_maxlevels_ondisk()),
     523             :                         0, 0, NULL);
     524             : 
     525          12 :         if (!xfs_rtrefcountbt_cur_cache)
     526           0 :                 return -ENOMEM;
     527             :         return 0;
     528             : }
     529             : 
     530             : void
     531          12 : xfs_rtrefcountbt_destroy_cur_cache(void)
     532             : {
     533          12 :         kmem_cache_destroy(xfs_rtrefcountbt_cur_cache);
     534          12 :         xfs_rtrefcountbt_cur_cache = NULL;
     535          12 : }
     536             : 
     537             : /* Compute the maximum height of a realtime refcount btree. */
     538             : void
     539       24771 : xfs_rtrefcountbt_compute_maxlevels(
     540             :         struct xfs_mount        *mp)
     541             : {
     542       24771 :         unsigned int            d_maxlevels, r_maxlevels;
     543             : 
     544       24771 :         if (!xfs_has_rtreflink(mp)) {
     545       24525 :                 mp->m_rtrefc_maxlevels = 0;
     546       24525 :                 return;
     547             :         }
     548             : 
     549             :         /*
     550             :          * The realtime refcountbt lives on the data device, which means that
     551             :          * its maximum height is constrained by the size of the data device and
     552             :          * the height required to store one refcount record for each rtextent
     553             :          * in an rt group.
     554             :          */
     555         246 :         d_maxlevels = xfs_btree_space_to_height(mp->m_rtrefc_mnr,
     556             :                                 mp->m_sb.sb_dblocks);
     557         246 :         r_maxlevels = xfs_btree_compute_maxlevels(mp->m_rtrefc_mnr,
     558         246 :                                 xfs_rtb_to_rtxt(mp, mp->m_sb.sb_rgblocks));
     559             : 
     560             :         /* Add one level to handle the inode root level. */
     561         246 :         mp->m_rtrefc_maxlevels = min(d_maxlevels, r_maxlevels) + 1;
     562             : }
     563             : 
     564             : #define XFS_RTREFC_NAMELEN              21
     565             : 
     566             : /* Create the metadata directory path for an rtrefcount btree inode. */
     567             : int
     568        1221 : xfs_rtrefcountbt_create_path(
     569             :         struct xfs_mount        *mp,
     570             :         xfs_rgnumber_t          rgno,
     571             :         struct xfs_imeta_path   **pathp)
     572             : {
     573        1221 :         struct xfs_imeta_path   *path;
     574        1221 :         unsigned char           *fname;
     575        1221 :         int                     error;
     576             : 
     577        1221 :         error = xfs_imeta_create_file_path(mp, 2, &path);
     578        1221 :         if (error)
     579             :                 return error;
     580             : 
     581        1221 :         fname = kmalloc(XFS_RTREFC_NAMELEN, GFP_KERNEL);
     582        1221 :         if (!fname) {
     583           0 :                 xfs_imeta_free_path(path);
     584           0 :                 return -ENOMEM;
     585             :         }
     586             : 
     587        1221 :         snprintf(fname, XFS_RTREFC_NAMELEN, "%u.refcount", rgno);
     588        1221 :         path->im_path[0] = "realtime";
     589        1221 :         path->im_path[1] = fname;
     590        1221 :         path->im_dynamicmask = 0x2;
     591        1221 :         *pathp = path;
     592        1221 :         return 0;
     593             : }
     594             : 
     595             : /* Calculate the rtrefcount btree size for some records. */
     596             : unsigned long long
     597           0 : xfs_rtrefcountbt_calc_size(
     598             :         struct xfs_mount        *mp,
     599             :         unsigned long long      len)
     600             : {
     601           0 :         return xfs_btree_calc_size(mp->m_rtrefc_mnr, len);
     602             : }
     603             : 
     604             : /*
     605             :  * Calculate the maximum refcount btree size.
     606             :  */
     607             : static unsigned long long
     608             : xfs_rtrefcountbt_max_size(
     609             :         struct xfs_mount        *mp,
     610             :         xfs_rtblock_t           rtblocks)
     611             : {
     612             :         /* Bail out if we're uninitialized, which can happen in mkfs. */
     613        2876 :         if (mp->m_rtrefc_mxr[0] == 0)
     614             :                 return 0;
     615             : 
     616        2876 :         return xfs_rtrefcountbt_calc_size(mp, rtblocks);
     617             : }
     618             : 
     619             : /*
     620             :  * Figure out how many blocks to reserve and how many are used by this btree.
     621             :  * We need enough space to hold one record for every rt extent in the rtgroup.
     622             :  */
     623             : xfs_filblks_t
     624        2876 : xfs_rtrefcountbt_calc_reserves(
     625             :         struct xfs_mount        *mp)
     626             : {
     627        2876 :         if (!xfs_has_rtreflink(mp))
     628             :                 return 0;
     629             : 
     630        2876 :         return xfs_rtrefcountbt_max_size(mp,
     631        2876 :                         xfs_rtb_to_rtxt(mp, mp->m_sb.sb_rgblocks));
     632             : }
     633             : 
     634             : /*
     635             :  * Convert on-disk form of btree root to in-memory form.
     636             :  */
     637             : STATIC void
     638        1211 : xfs_rtrefcountbt_from_disk(
     639             :         struct xfs_inode                *ip,
     640             :         struct xfs_rtrefcount_root      *dblock,
     641             :         int                             dblocklen,
     642             :         struct xfs_btree_block          *rblock)
     643             : {
     644        1211 :         struct xfs_mount                *mp = ip->i_mount;
     645        1211 :         struct xfs_refcount_key *fkp;
     646        1211 :         __be64                          *fpp;
     647        1211 :         struct xfs_refcount_key *tkp;
     648        1211 :         __be64                          *tpp;
     649        1211 :         struct xfs_refcount_rec *frp;
     650        1211 :         struct xfs_refcount_rec *trp;
     651        1211 :         unsigned int                    numrecs;
     652        1211 :         unsigned int                    maxrecs;
     653        1211 :         unsigned int                    rblocklen;
     654             : 
     655        1211 :         rblocklen = xfs_rtrefcount_broot_space(mp, dblock);
     656             : 
     657        1211 :         xfs_btree_init_block(mp, rblock, &xfs_rtrefcountbt_ops, 0, 0,
     658             :                         ip->i_ino);
     659             : 
     660        1211 :         rblock->bb_level = dblock->bb_level;
     661        1211 :         rblock->bb_numrecs = dblock->bb_numrecs;
     662             : 
     663        1211 :         if (be16_to_cpu(rblock->bb_level) > 0) {
     664         240 :                 maxrecs = xfs_rtrefcountbt_droot_maxrecs(dblocklen, false);
     665         240 :                 fkp = xfs_rtrefcount_droot_key_addr(dblock, 1);
     666         240 :                 tkp = xfs_rtrefcount_key_addr(rblock, 1);
     667         240 :                 fpp = xfs_rtrefcount_droot_ptr_addr(dblock, 1, maxrecs);
     668         240 :                 tpp = xfs_rtrefcount_broot_ptr_addr(mp, rblock, 1, rblocklen);
     669         240 :                 numrecs = be16_to_cpu(dblock->bb_numrecs);
     670         480 :                 memcpy(tkp, fkp, 2 * sizeof(*fkp) * numrecs);
     671         480 :                 memcpy(tpp, fpp, sizeof(*fpp) * numrecs);
     672             :         } else {
     673         971 :                 frp = xfs_rtrefcount_droot_rec_addr(dblock, 1);
     674         971 :                 trp = xfs_rtrefcount_rec_addr(rblock, 1);
     675         971 :                 numrecs = be16_to_cpu(dblock->bb_numrecs);
     676        1942 :                 memcpy(trp, frp, sizeof(*frp) * numrecs);
     677             :         }
     678        1211 : }
     679             : 
     680             : /* Load a realtime reference count btree root in from disk. */
     681             : int
     682        1211 : xfs_iformat_rtrefcount(
     683             :         struct xfs_inode        *ip,
     684             :         struct xfs_dinode       *dip)
     685             : {
     686        1211 :         struct xfs_mount        *mp = ip->i_mount;
     687        1211 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK);
     688        1211 :         struct xfs_rtrefcount_root *dfp = XFS_DFORK_PTR(dip, XFS_DATA_FORK);
     689        1211 :         unsigned int            numrecs;
     690        1211 :         unsigned int            level;
     691        1211 :         int                     dsize;
     692             : 
     693        1211 :         dsize = XFS_DFORK_SIZE(dip, mp, XFS_DATA_FORK);
     694        1211 :         numrecs = be16_to_cpu(dfp->bb_numrecs);
     695        1211 :         level = be16_to_cpu(dfp->bb_level);
     696             : 
     697        1211 :         if (level > mp->m_rtrefc_maxlevels ||
     698        1211 :             xfs_rtrefcount_droot_space_calc(level, numrecs) > dsize) {
     699           0 :                 xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
     700           0 :                 return -EFSCORRUPTED;
     701             :         }
     702             : 
     703        2422 :         xfs_iroot_alloc(ip, XFS_DATA_FORK,
     704             :                         xfs_rtrefcount_broot_space_calc(mp, level, numrecs));
     705        1211 :         xfs_rtrefcountbt_from_disk(ip, dfp, dsize, ifp->if_broot);
     706        1211 :         return 0;
     707             : }
     708             : 
     709             : /*
     710             :  * Convert in-memory form of btree root to on-disk form.
     711             :  */
     712             : void
     713       27922 : xfs_rtrefcountbt_to_disk(
     714             :         struct xfs_mount                *mp,
     715             :         struct xfs_btree_block          *rblock,
     716             :         int                             rblocklen,
     717             :         struct xfs_rtrefcount_root      *dblock,
     718             :         int                             dblocklen)
     719             : {
     720       27922 :         struct xfs_refcount_key *fkp;
     721       27922 :         __be64                          *fpp;
     722       27922 :         struct xfs_refcount_key *tkp;
     723       27922 :         __be64                          *tpp;
     724       27922 :         struct xfs_refcount_rec *frp;
     725       27922 :         struct xfs_refcount_rec *trp;
     726       27922 :         unsigned int                    maxrecs;
     727       27922 :         unsigned int                    numrecs;
     728             : 
     729       27922 :         ASSERT(rblock->bb_magic == cpu_to_be32(XFS_RTREFC_CRC_MAGIC));
     730       27922 :         ASSERT(uuid_equal(&rblock->bb_u.l.bb_uuid, &mp->m_sb.sb_meta_uuid));
     731       27922 :         ASSERT(rblock->bb_u.l.bb_blkno == cpu_to_be64(XFS_BUF_DADDR_NULL));
     732       27922 :         ASSERT(rblock->bb_u.l.bb_leftsib == cpu_to_be64(NULLFSBLOCK));
     733       27922 :         ASSERT(rblock->bb_u.l.bb_rightsib == cpu_to_be64(NULLFSBLOCK));
     734             : 
     735       27922 :         dblock->bb_level = rblock->bb_level;
     736       27922 :         dblock->bb_numrecs = rblock->bb_numrecs;
     737             : 
     738       27922 :         if (be16_to_cpu(rblock->bb_level) > 0) {
     739       21807 :                 maxrecs = xfs_rtrefcountbt_droot_maxrecs(dblocklen, false);
     740       21807 :                 fkp = xfs_rtrefcount_key_addr(rblock, 1);
     741       21807 :                 tkp = xfs_rtrefcount_droot_key_addr(dblock, 1);
     742       21807 :                 fpp = xfs_rtrefcount_broot_ptr_addr(mp, rblock, 1, rblocklen);
     743       21807 :                 tpp = xfs_rtrefcount_droot_ptr_addr(dblock, 1, maxrecs);
     744       21807 :                 numrecs = be16_to_cpu(rblock->bb_numrecs);
     745       43614 :                 memcpy(tkp, fkp, 2 * sizeof(*fkp) * numrecs);
     746       43614 :                 memcpy(tpp, fpp, sizeof(*fpp) * numrecs);
     747             :         } else {
     748        6115 :                 frp = xfs_rtrefcount_rec_addr(rblock, 1);
     749        6115 :                 trp = xfs_rtrefcount_droot_rec_addr(dblock, 1);
     750        6115 :                 numrecs = be16_to_cpu(rblock->bb_numrecs);
     751       12230 :                 memcpy(trp, frp, sizeof(*frp) * numrecs);
     752             :         }
     753       27922 : }
     754             : 
     755             : /* Flush a realtime reference count btree root out to disk. */
     756             : void
     757       27922 : xfs_iflush_rtrefcount(
     758             :         struct xfs_inode        *ip,
     759             :         struct xfs_dinode       *dip)
     760             : {
     761       27922 :         struct xfs_ifork        *ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK);
     762       27922 :         struct xfs_rtrefcount_root *dfp = XFS_DFORK_PTR(dip, XFS_DATA_FORK);
     763             : 
     764       27922 :         ASSERT(ifp->if_broot != NULL);
     765       27922 :         ASSERT(ifp->if_broot_bytes > 0);
     766       27922 :         ASSERT(xfs_rtrefcount_droot_space(ifp->if_broot) <=
     767             :                         xfs_inode_fork_size(ip, XFS_DATA_FORK));
     768       27922 :         xfs_rtrefcountbt_to_disk(ip->i_mount, ifp->if_broot,
     769       27922 :                         ifp->if_broot_bytes, dfp,
     770       27922 :                         XFS_DFORK_SIZE(dip, ip->i_mount, XFS_DATA_FORK));
     771       27922 : }
     772             : 
     773             : /*
     774             :  * Create a realtime refcount btree inode.
     775             :  *
     776             :  * Regardless of the return value, the caller must clean up @upd.  If a new
     777             :  * inode is returned through *ipp, the caller must finish setting up the incore
     778             :  * inode and release it.
     779             :  */
     780             : int
     781          10 : xfs_rtrefcountbt_create(
     782             :         struct xfs_imeta_update *upd,
     783             :         struct xfs_inode        **ipp)
     784             : {
     785          10 :         struct xfs_mount        *mp = upd->mp;
     786          10 :         struct xfs_ifork        *ifp;
     787          10 :         int                     error;
     788             : 
     789          10 :         error = xfs_imeta_create(upd, S_IFREG, ipp);
     790          10 :         if (error)
     791             :                 return error;
     792             : 
     793          10 :         ifp = xfs_ifork_ptr(upd->ip, XFS_DATA_FORK);
     794          10 :         ifp->if_format = XFS_DINODE_FMT_REFCOUNT;
     795          10 :         ASSERT(ifp->if_broot_bytes == 0);
     796          10 :         ASSERT(ifp->if_bytes == 0);
     797             : 
     798             :         /* Initialize the empty incore btree root. */
     799          10 :         xfs_iroot_alloc(upd->ip, XFS_DATA_FORK,
     800             :                         xfs_rtrefcount_broot_space_calc(mp, 0, 0));
     801          10 :         xfs_btree_init_block(mp, ifp->if_broot, &xfs_rtrefcountbt_ops,
     802          10 :                         0, 0, upd->ip->i_ino);
     803          10 :         xfs_trans_log_inode(upd->tp, upd->ip, XFS_ILOG_CORE | XFS_ILOG_DBROOT);
     804          10 :         return 0;
     805             : }

Generated by: LCOV version 1.14