Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
0004  * All Rights Reserved.
0005  */
0006 #include "xfs.h"
0007 #include "xfs_fs.h"
0008 #include "xfs_shared.h"
0009 #include "xfs_format.h"
0010 #include "xfs_log_format.h"
0011 #include "xfs_trans_resv.h"
0012 #include "xfs_bit.h"
0013 #include "xfs_mount.h"
0014 #include "xfs_btree.h"
0015 #include "xfs_btree_staging.h"
0016 #include "xfs_ialloc.h"
0017 #include "xfs_ialloc_btree.h"
0018 #include "xfs_alloc.h"
0019 #include "xfs_error.h"
0020 #include "xfs_trace.h"
0021 #include "xfs_trans.h"
0022 #include "xfs_rmap.h"
0023 #include "xfs_ag.h"
0024 
0025 static struct kmem_cache    *xfs_inobt_cur_cache;
0026 
0027 STATIC int
0028 xfs_inobt_get_minrecs(
0029     struct xfs_btree_cur    *cur,
0030     int         level)
0031 {
0032     return M_IGEO(cur->bc_mp)->inobt_mnr[level != 0];
0033 }
0034 
0035 STATIC struct xfs_btree_cur *
0036 xfs_inobt_dup_cursor(
0037     struct xfs_btree_cur    *cur)
0038 {
0039     return xfs_inobt_init_cursor(cur->bc_mp, cur->bc_tp,
0040             cur->bc_ag.agbp, cur->bc_ag.pag, cur->bc_btnum);
0041 }
0042 
0043 STATIC void
0044 xfs_inobt_set_root(
0045     struct xfs_btree_cur        *cur,
0046     const union xfs_btree_ptr   *nptr,
0047     int             inc)    /* level change */
0048 {
0049     struct xfs_buf      *agbp = cur->bc_ag.agbp;
0050     struct xfs_agi      *agi = agbp->b_addr;
0051 
0052     agi->agi_root = nptr->s;
0053     be32_add_cpu(&agi->agi_level, inc);
0054     xfs_ialloc_log_agi(cur->bc_tp, agbp, XFS_AGI_ROOT | XFS_AGI_LEVEL);
0055 }
0056 
0057 STATIC void
0058 xfs_finobt_set_root(
0059     struct xfs_btree_cur        *cur,
0060     const union xfs_btree_ptr   *nptr,
0061     int             inc)    /* level change */
0062 {
0063     struct xfs_buf      *agbp = cur->bc_ag.agbp;
0064     struct xfs_agi      *agi = agbp->b_addr;
0065 
0066     agi->agi_free_root = nptr->s;
0067     be32_add_cpu(&agi->agi_free_level, inc);
0068     xfs_ialloc_log_agi(cur->bc_tp, agbp,
0069                XFS_AGI_FREE_ROOT | XFS_AGI_FREE_LEVEL);
0070 }
0071 
0072 /* Update the inode btree block counter for this btree. */
0073 static inline void
0074 xfs_inobt_mod_blockcount(
0075     struct xfs_btree_cur    *cur,
0076     int         howmuch)
0077 {
0078     struct xfs_buf      *agbp = cur->bc_ag.agbp;
0079     struct xfs_agi      *agi = agbp->b_addr;
0080 
0081     if (!xfs_has_inobtcounts(cur->bc_mp))
0082         return;
0083 
0084     if (cur->bc_btnum == XFS_BTNUM_FINO)
0085         be32_add_cpu(&agi->agi_fblocks, howmuch);
0086     else if (cur->bc_btnum == XFS_BTNUM_INO)
0087         be32_add_cpu(&agi->agi_iblocks, howmuch);
0088     xfs_ialloc_log_agi(cur->bc_tp, agbp, XFS_AGI_IBLOCKS);
0089 }
0090 
0091 STATIC int
0092 __xfs_inobt_alloc_block(
0093     struct xfs_btree_cur        *cur,
0094     const union xfs_btree_ptr   *start,
0095     union xfs_btree_ptr     *new,
0096     int             *stat,
0097     enum xfs_ag_resv_type       resv)
0098 {
0099     xfs_alloc_arg_t     args;       /* block allocation args */
0100     int         error;      /* error return value */
0101     xfs_agblock_t       sbno = be32_to_cpu(start->s);
0102 
0103     memset(&args, 0, sizeof(args));
0104     args.tp = cur->bc_tp;
0105     args.mp = cur->bc_mp;
0106     args.oinfo = XFS_RMAP_OINFO_INOBT;
0107     args.fsbno = XFS_AGB_TO_FSB(args.mp, cur->bc_ag.pag->pag_agno, sbno);
0108     args.minlen = 1;
0109     args.maxlen = 1;
0110     args.prod = 1;
0111     args.type = XFS_ALLOCTYPE_NEAR_BNO;
0112     args.resv = resv;
0113 
0114     error = xfs_alloc_vextent(&args);
0115     if (error)
0116         return error;
0117 
0118     if (args.fsbno == NULLFSBLOCK) {
0119         *stat = 0;
0120         return 0;
0121     }
0122     ASSERT(args.len == 1);
0123 
0124     new->s = cpu_to_be32(XFS_FSB_TO_AGBNO(args.mp, args.fsbno));
0125     *stat = 1;
0126     xfs_inobt_mod_blockcount(cur, 1);
0127     return 0;
0128 }
0129 
0130 STATIC int
0131 xfs_inobt_alloc_block(
0132     struct xfs_btree_cur        *cur,
0133     const union xfs_btree_ptr   *start,
0134     union xfs_btree_ptr     *new,
0135     int             *stat)
0136 {
0137     return __xfs_inobt_alloc_block(cur, start, new, stat, XFS_AG_RESV_NONE);
0138 }
0139 
0140 STATIC int
0141 xfs_finobt_alloc_block(
0142     struct xfs_btree_cur        *cur,
0143     const union xfs_btree_ptr   *start,
0144     union xfs_btree_ptr     *new,
0145     int             *stat)
0146 {
0147     if (cur->bc_mp->m_finobt_nores)
0148         return xfs_inobt_alloc_block(cur, start, new, stat);
0149     return __xfs_inobt_alloc_block(cur, start, new, stat,
0150             XFS_AG_RESV_METADATA);
0151 }
0152 
0153 STATIC int
0154 __xfs_inobt_free_block(
0155     struct xfs_btree_cur    *cur,
0156     struct xfs_buf      *bp,
0157     enum xfs_ag_resv_type   resv)
0158 {
0159     xfs_inobt_mod_blockcount(cur, -1);
0160     return xfs_free_extent(cur->bc_tp,
0161             XFS_DADDR_TO_FSB(cur->bc_mp, xfs_buf_daddr(bp)), 1,
0162             &XFS_RMAP_OINFO_INOBT, resv);
0163 }
0164 
0165 STATIC int
0166 xfs_inobt_free_block(
0167     struct xfs_btree_cur    *cur,
0168     struct xfs_buf      *bp)
0169 {
0170     return __xfs_inobt_free_block(cur, bp, XFS_AG_RESV_NONE);
0171 }
0172 
0173 STATIC int
0174 xfs_finobt_free_block(
0175     struct xfs_btree_cur    *cur,
0176     struct xfs_buf      *bp)
0177 {
0178     if (cur->bc_mp->m_finobt_nores)
0179         return xfs_inobt_free_block(cur, bp);
0180     return __xfs_inobt_free_block(cur, bp, XFS_AG_RESV_METADATA);
0181 }
0182 
0183 STATIC int
0184 xfs_inobt_get_maxrecs(
0185     struct xfs_btree_cur    *cur,
0186     int         level)
0187 {
0188     return M_IGEO(cur->bc_mp)->inobt_mxr[level != 0];
0189 }
0190 
0191 STATIC void
0192 xfs_inobt_init_key_from_rec(
0193     union xfs_btree_key     *key,
0194     const union xfs_btree_rec   *rec)
0195 {
0196     key->inobt.ir_startino = rec->inobt.ir_startino;
0197 }
0198 
0199 STATIC void
0200 xfs_inobt_init_high_key_from_rec(
0201     union xfs_btree_key     *key,
0202     const union xfs_btree_rec   *rec)
0203 {
0204     __u32               x;
0205 
0206     x = be32_to_cpu(rec->inobt.ir_startino);
0207     x += XFS_INODES_PER_CHUNK - 1;
0208     key->inobt.ir_startino = cpu_to_be32(x);
0209 }
0210 
0211 STATIC void
0212 xfs_inobt_init_rec_from_cur(
0213     struct xfs_btree_cur    *cur,
0214     union xfs_btree_rec *rec)
0215 {
0216     rec->inobt.ir_startino = cpu_to_be32(cur->bc_rec.i.ir_startino);
0217     if (xfs_has_sparseinodes(cur->bc_mp)) {
0218         rec->inobt.ir_u.sp.ir_holemask =
0219                     cpu_to_be16(cur->bc_rec.i.ir_holemask);
0220         rec->inobt.ir_u.sp.ir_count = cur->bc_rec.i.ir_count;
0221         rec->inobt.ir_u.sp.ir_freecount = cur->bc_rec.i.ir_freecount;
0222     } else {
0223         /* ir_holemask/ir_count not supported on-disk */
0224         rec->inobt.ir_u.f.ir_freecount =
0225                     cpu_to_be32(cur->bc_rec.i.ir_freecount);
0226     }
0227     rec->inobt.ir_free = cpu_to_be64(cur->bc_rec.i.ir_free);
0228 }
0229 
0230 /*
0231  * initial value of ptr for lookup
0232  */
0233 STATIC void
0234 xfs_inobt_init_ptr_from_cur(
0235     struct xfs_btree_cur    *cur,
0236     union xfs_btree_ptr *ptr)
0237 {
0238     struct xfs_agi      *agi = cur->bc_ag.agbp->b_addr;
0239 
0240     ASSERT(cur->bc_ag.pag->pag_agno == be32_to_cpu(agi->agi_seqno));
0241 
0242     ptr->s = agi->agi_root;
0243 }
0244 
0245 STATIC void
0246 xfs_finobt_init_ptr_from_cur(
0247     struct xfs_btree_cur    *cur,
0248     union xfs_btree_ptr *ptr)
0249 {
0250     struct xfs_agi      *agi = cur->bc_ag.agbp->b_addr;
0251 
0252     ASSERT(cur->bc_ag.pag->pag_agno == be32_to_cpu(agi->agi_seqno));
0253     ptr->s = agi->agi_free_root;
0254 }
0255 
0256 STATIC int64_t
0257 xfs_inobt_key_diff(
0258     struct xfs_btree_cur        *cur,
0259     const union xfs_btree_key   *key)
0260 {
0261     return (int64_t)be32_to_cpu(key->inobt.ir_startino) -
0262               cur->bc_rec.i.ir_startino;
0263 }
0264 
0265 STATIC int64_t
0266 xfs_inobt_diff_two_keys(
0267     struct xfs_btree_cur        *cur,
0268     const union xfs_btree_key   *k1,
0269     const union xfs_btree_key   *k2)
0270 {
0271     return (int64_t)be32_to_cpu(k1->inobt.ir_startino) -
0272               be32_to_cpu(k2->inobt.ir_startino);
0273 }
0274 
0275 static xfs_failaddr_t
0276 xfs_inobt_verify(
0277     struct xfs_buf      *bp)
0278 {
0279     struct xfs_mount    *mp = bp->b_mount;
0280     struct xfs_btree_block  *block = XFS_BUF_TO_BLOCK(bp);
0281     xfs_failaddr_t      fa;
0282     unsigned int        level;
0283 
0284     if (!xfs_verify_magic(bp, block->bb_magic))
0285         return __this_address;
0286 
0287     /*
0288      * During growfs operations, we can't verify the exact owner as the
0289      * perag is not fully initialised and hence not attached to the buffer.
0290      *
0291      * Similarly, during log recovery we will have a perag structure
0292      * attached, but the agi information will not yet have been initialised
0293      * from the on disk AGI. We don't currently use any of this information,
0294      * but beware of the landmine (i.e. need to check pag->pagi_init) if we
0295      * ever do.
0296      */
0297     if (xfs_has_crc(mp)) {
0298         fa = xfs_btree_sblock_v5hdr_verify(bp);
0299         if (fa)
0300             return fa;
0301     }
0302 
0303     /* level verification */
0304     level = be16_to_cpu(block->bb_level);
0305     if (level >= M_IGEO(mp)->inobt_maxlevels)
0306         return __this_address;
0307 
0308     return xfs_btree_sblock_verify(bp,
0309             M_IGEO(mp)->inobt_mxr[level != 0]);
0310 }
0311 
0312 static void
0313 xfs_inobt_read_verify(
0314     struct xfs_buf  *bp)
0315 {
0316     xfs_failaddr_t  fa;
0317 
0318     if (!xfs_btree_sblock_verify_crc(bp))
0319         xfs_verifier_error(bp, -EFSBADCRC, __this_address);
0320     else {
0321         fa = xfs_inobt_verify(bp);
0322         if (fa)
0323             xfs_verifier_error(bp, -EFSCORRUPTED, fa);
0324     }
0325 
0326     if (bp->b_error)
0327         trace_xfs_btree_corrupt(bp, _RET_IP_);
0328 }
0329 
0330 static void
0331 xfs_inobt_write_verify(
0332     struct xfs_buf  *bp)
0333 {
0334     xfs_failaddr_t  fa;
0335 
0336     fa = xfs_inobt_verify(bp);
0337     if (fa) {
0338         trace_xfs_btree_corrupt(bp, _RET_IP_);
0339         xfs_verifier_error(bp, -EFSCORRUPTED, fa);
0340         return;
0341     }
0342     xfs_btree_sblock_calc_crc(bp);
0343 
0344 }
0345 
0346 const struct xfs_buf_ops xfs_inobt_buf_ops = {
0347     .name = "xfs_inobt",
0348     .magic = { cpu_to_be32(XFS_IBT_MAGIC), cpu_to_be32(XFS_IBT_CRC_MAGIC) },
0349     .verify_read = xfs_inobt_read_verify,
0350     .verify_write = xfs_inobt_write_verify,
0351     .verify_struct = xfs_inobt_verify,
0352 };
0353 
0354 const struct xfs_buf_ops xfs_finobt_buf_ops = {
0355     .name = "xfs_finobt",
0356     .magic = { cpu_to_be32(XFS_FIBT_MAGIC),
0357            cpu_to_be32(XFS_FIBT_CRC_MAGIC) },
0358     .verify_read = xfs_inobt_read_verify,
0359     .verify_write = xfs_inobt_write_verify,
0360     .verify_struct = xfs_inobt_verify,
0361 };
0362 
0363 STATIC int
0364 xfs_inobt_keys_inorder(
0365     struct xfs_btree_cur        *cur,
0366     const union xfs_btree_key   *k1,
0367     const union xfs_btree_key   *k2)
0368 {
0369     return be32_to_cpu(k1->inobt.ir_startino) <
0370         be32_to_cpu(k2->inobt.ir_startino);
0371 }
0372 
0373 STATIC int
0374 xfs_inobt_recs_inorder(
0375     struct xfs_btree_cur        *cur,
0376     const union xfs_btree_rec   *r1,
0377     const union xfs_btree_rec   *r2)
0378 {
0379     return be32_to_cpu(r1->inobt.ir_startino) + XFS_INODES_PER_CHUNK <=
0380         be32_to_cpu(r2->inobt.ir_startino);
0381 }
0382 
0383 static const struct xfs_btree_ops xfs_inobt_ops = {
0384     .rec_len        = sizeof(xfs_inobt_rec_t),
0385     .key_len        = sizeof(xfs_inobt_key_t),
0386 
0387     .dup_cursor     = xfs_inobt_dup_cursor,
0388     .set_root       = xfs_inobt_set_root,
0389     .alloc_block        = xfs_inobt_alloc_block,
0390     .free_block     = xfs_inobt_free_block,
0391     .get_minrecs        = xfs_inobt_get_minrecs,
0392     .get_maxrecs        = xfs_inobt_get_maxrecs,
0393     .init_key_from_rec  = xfs_inobt_init_key_from_rec,
0394     .init_high_key_from_rec = xfs_inobt_init_high_key_from_rec,
0395     .init_rec_from_cur  = xfs_inobt_init_rec_from_cur,
0396     .init_ptr_from_cur  = xfs_inobt_init_ptr_from_cur,
0397     .key_diff       = xfs_inobt_key_diff,
0398     .buf_ops        = &xfs_inobt_buf_ops,
0399     .diff_two_keys      = xfs_inobt_diff_two_keys,
0400     .keys_inorder       = xfs_inobt_keys_inorder,
0401     .recs_inorder       = xfs_inobt_recs_inorder,
0402 };
0403 
0404 static const struct xfs_btree_ops xfs_finobt_ops = {
0405     .rec_len        = sizeof(xfs_inobt_rec_t),
0406     .key_len        = sizeof(xfs_inobt_key_t),
0407 
0408     .dup_cursor     = xfs_inobt_dup_cursor,
0409     .set_root       = xfs_finobt_set_root,
0410     .alloc_block        = xfs_finobt_alloc_block,
0411     .free_block     = xfs_finobt_free_block,
0412     .get_minrecs        = xfs_inobt_get_minrecs,
0413     .get_maxrecs        = xfs_inobt_get_maxrecs,
0414     .init_key_from_rec  = xfs_inobt_init_key_from_rec,
0415     .init_high_key_from_rec = xfs_inobt_init_high_key_from_rec,
0416     .init_rec_from_cur  = xfs_inobt_init_rec_from_cur,
0417     .init_ptr_from_cur  = xfs_finobt_init_ptr_from_cur,
0418     .key_diff       = xfs_inobt_key_diff,
0419     .buf_ops        = &xfs_finobt_buf_ops,
0420     .diff_two_keys      = xfs_inobt_diff_two_keys,
0421     .keys_inorder       = xfs_inobt_keys_inorder,
0422     .recs_inorder       = xfs_inobt_recs_inorder,
0423 };
0424 
0425 /*
0426  * Initialize a new inode btree cursor.
0427  */
0428 static struct xfs_btree_cur *
0429 xfs_inobt_init_common(
0430     struct xfs_mount    *mp,        /* file system mount point */
0431     struct xfs_trans    *tp,        /* transaction pointer */
0432     struct xfs_perag    *pag,
0433     xfs_btnum_t     btnum)      /* ialloc or free ino btree */
0434 {
0435     struct xfs_btree_cur    *cur;
0436 
0437     cur = xfs_btree_alloc_cursor(mp, tp, btnum,
0438             M_IGEO(mp)->inobt_maxlevels, xfs_inobt_cur_cache);
0439     if (btnum == XFS_BTNUM_INO) {
0440         cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_ibt_2);
0441         cur->bc_ops = &xfs_inobt_ops;
0442     } else {
0443         cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_fibt_2);
0444         cur->bc_ops = &xfs_finobt_ops;
0445     }
0446 
0447     if (xfs_has_crc(mp))
0448         cur->bc_flags |= XFS_BTREE_CRC_BLOCKS;
0449 
0450     /* take a reference for the cursor */
0451     atomic_inc(&pag->pag_ref);
0452     cur->bc_ag.pag = pag;
0453     return cur;
0454 }
0455 
0456 /* Create an inode btree cursor. */
0457 struct xfs_btree_cur *
0458 xfs_inobt_init_cursor(
0459     struct xfs_mount    *mp,
0460     struct xfs_trans    *tp,
0461     struct xfs_buf      *agbp,
0462     struct xfs_perag    *pag,
0463     xfs_btnum_t     btnum)
0464 {
0465     struct xfs_btree_cur    *cur;
0466     struct xfs_agi      *agi = agbp->b_addr;
0467 
0468     cur = xfs_inobt_init_common(mp, tp, pag, btnum);
0469     if (btnum == XFS_BTNUM_INO)
0470         cur->bc_nlevels = be32_to_cpu(agi->agi_level);
0471     else
0472         cur->bc_nlevels = be32_to_cpu(agi->agi_free_level);
0473     cur->bc_ag.agbp = agbp;
0474     return cur;
0475 }
0476 
0477 /* Create an inode btree cursor with a fake root for staging. */
0478 struct xfs_btree_cur *
0479 xfs_inobt_stage_cursor(
0480     struct xfs_mount    *mp,
0481     struct xbtree_afakeroot *afake,
0482     struct xfs_perag    *pag,
0483     xfs_btnum_t     btnum)
0484 {
0485     struct xfs_btree_cur    *cur;
0486 
0487     cur = xfs_inobt_init_common(mp, NULL, pag, btnum);
0488     xfs_btree_stage_afakeroot(cur, afake);
0489     return cur;
0490 }
0491 
0492 /*
0493  * Install a new inobt btree root.  Caller is responsible for invalidating
0494  * and freeing the old btree blocks.
0495  */
0496 void
0497 xfs_inobt_commit_staged_btree(
0498     struct xfs_btree_cur    *cur,
0499     struct xfs_trans    *tp,
0500     struct xfs_buf      *agbp)
0501 {
0502     struct xfs_agi      *agi = agbp->b_addr;
0503     struct xbtree_afakeroot *afake = cur->bc_ag.afake;
0504     int         fields;
0505 
0506     ASSERT(cur->bc_flags & XFS_BTREE_STAGING);
0507 
0508     if (cur->bc_btnum == XFS_BTNUM_INO) {
0509         fields = XFS_AGI_ROOT | XFS_AGI_LEVEL;
0510         agi->agi_root = cpu_to_be32(afake->af_root);
0511         agi->agi_level = cpu_to_be32(afake->af_levels);
0512         if (xfs_has_inobtcounts(cur->bc_mp)) {
0513             agi->agi_iblocks = cpu_to_be32(afake->af_blocks);
0514             fields |= XFS_AGI_IBLOCKS;
0515         }
0516         xfs_ialloc_log_agi(tp, agbp, fields);
0517         xfs_btree_commit_afakeroot(cur, tp, agbp, &xfs_inobt_ops);
0518     } else {
0519         fields = XFS_AGI_FREE_ROOT | XFS_AGI_FREE_LEVEL;
0520         agi->agi_free_root = cpu_to_be32(afake->af_root);
0521         agi->agi_free_level = cpu_to_be32(afake->af_levels);
0522         if (xfs_has_inobtcounts(cur->bc_mp)) {
0523             agi->agi_fblocks = cpu_to_be32(afake->af_blocks);
0524             fields |= XFS_AGI_IBLOCKS;
0525         }
0526         xfs_ialloc_log_agi(tp, agbp, fields);
0527         xfs_btree_commit_afakeroot(cur, tp, agbp, &xfs_finobt_ops);
0528     }
0529 }
0530 
0531 /* Calculate number of records in an inode btree block. */
0532 static inline unsigned int
0533 xfs_inobt_block_maxrecs(
0534     unsigned int        blocklen,
0535     bool            leaf)
0536 {
0537     if (leaf)
0538         return blocklen / sizeof(xfs_inobt_rec_t);
0539     return blocklen / (sizeof(xfs_inobt_key_t) + sizeof(xfs_inobt_ptr_t));
0540 }
0541 
0542 /*
0543  * Calculate number of records in an inobt btree block.
0544  */
0545 int
0546 xfs_inobt_maxrecs(
0547     struct xfs_mount    *mp,
0548     int         blocklen,
0549     int         leaf)
0550 {
0551     blocklen -= XFS_INOBT_BLOCK_LEN(mp);
0552     return xfs_inobt_block_maxrecs(blocklen, leaf);
0553 }
0554 
0555 /*
0556  * Maximum number of inode btree records per AG.  Pretend that we can fill an
0557  * entire AG completely full of inodes except for the AG headers.
0558  */
0559 #define XFS_MAX_INODE_RECORDS \
0560     ((XFS_MAX_AG_BYTES - (4 * BBSIZE)) / XFS_DINODE_MIN_SIZE) / \
0561             XFS_INODES_PER_CHUNK
0562 
0563 /* Compute the max possible height for the inode btree. */
0564 static inline unsigned int
0565 xfs_inobt_maxlevels_ondisk(void)
0566 {
0567     unsigned int        minrecs[2];
0568     unsigned int        blocklen;
0569 
0570     blocklen = min(XFS_MIN_BLOCKSIZE - XFS_BTREE_SBLOCK_LEN,
0571                XFS_MIN_CRC_BLOCKSIZE - XFS_BTREE_SBLOCK_CRC_LEN);
0572 
0573     minrecs[0] = xfs_inobt_block_maxrecs(blocklen, true) / 2;
0574     minrecs[1] = xfs_inobt_block_maxrecs(blocklen, false) / 2;
0575 
0576     return xfs_btree_compute_maxlevels(minrecs, XFS_MAX_INODE_RECORDS);
0577 }
0578 
0579 /* Compute the max possible height for the free inode btree. */
0580 static inline unsigned int
0581 xfs_finobt_maxlevels_ondisk(void)
0582 {
0583     unsigned int        minrecs[2];
0584     unsigned int        blocklen;
0585 
0586     blocklen = XFS_MIN_CRC_BLOCKSIZE - XFS_BTREE_SBLOCK_CRC_LEN;
0587 
0588     minrecs[0] = xfs_inobt_block_maxrecs(blocklen, true) / 2;
0589     minrecs[1] = xfs_inobt_block_maxrecs(blocklen, false) / 2;
0590 
0591     return xfs_btree_compute_maxlevels(minrecs, XFS_MAX_INODE_RECORDS);
0592 }
0593 
0594 /* Compute the max possible height for either inode btree. */
0595 unsigned int
0596 xfs_iallocbt_maxlevels_ondisk(void)
0597 {
0598     return max(xfs_inobt_maxlevels_ondisk(),
0599            xfs_finobt_maxlevels_ondisk());
0600 }
0601 
0602 /*
0603  * Convert the inode record holemask to an inode allocation bitmap. The inode
0604  * allocation bitmap is inode granularity and specifies whether an inode is
0605  * physically allocated on disk (not whether the inode is considered allocated
0606  * or free by the fs).
0607  *
0608  * A bit value of 1 means the inode is allocated, a value of 0 means it is free.
0609  */
0610 uint64_t
0611 xfs_inobt_irec_to_allocmask(
0612     struct xfs_inobt_rec_incore *rec)
0613 {
0614     uint64_t            bitmap = 0;
0615     uint64_t            inodespbit;
0616     int             nextbit;
0617     uint                allocbitmap;
0618 
0619     /*
0620      * The holemask has 16-bits for a 64 inode record. Therefore each
0621      * holemask bit represents multiple inodes. Create a mask of bits to set
0622      * in the allocmask for each holemask bit.
0623      */
0624     inodespbit = (1 << XFS_INODES_PER_HOLEMASK_BIT) - 1;
0625 
0626     /*
0627      * Allocated inodes are represented by 0 bits in holemask. Invert the 0
0628      * bits to 1 and convert to a uint so we can use xfs_next_bit(). Mask
0629      * anything beyond the 16 holemask bits since this casts to a larger
0630      * type.
0631      */
0632     allocbitmap = ~rec->ir_holemask & ((1 << XFS_INOBT_HOLEMASK_BITS) - 1);
0633 
0634     /*
0635      * allocbitmap is the inverted holemask so every set bit represents
0636      * allocated inodes. To expand from 16-bit holemask granularity to
0637      * 64-bit (e.g., bit-per-inode), set inodespbit bits in the target
0638      * bitmap for every holemask bit.
0639      */
0640     nextbit = xfs_next_bit(&allocbitmap, 1, 0);
0641     while (nextbit != -1) {
0642         ASSERT(nextbit < (sizeof(rec->ir_holemask) * NBBY));
0643 
0644         bitmap |= (inodespbit <<
0645                (nextbit * XFS_INODES_PER_HOLEMASK_BIT));
0646 
0647         nextbit = xfs_next_bit(&allocbitmap, 1, nextbit + 1);
0648     }
0649 
0650     return bitmap;
0651 }
0652 
0653 #if defined(DEBUG) || defined(XFS_WARN)
0654 /*
0655  * Verify that an in-core inode record has a valid inode count.
0656  */
0657 int
0658 xfs_inobt_rec_check_count(
0659     struct xfs_mount        *mp,
0660     struct xfs_inobt_rec_incore *rec)
0661 {
0662     int             inocount = 0;
0663     int             nextbit = 0;
0664     uint64_t            allocbmap;
0665     int             wordsz;
0666 
0667     wordsz = sizeof(allocbmap) / sizeof(unsigned int);
0668     allocbmap = xfs_inobt_irec_to_allocmask(rec);
0669 
0670     nextbit = xfs_next_bit((uint *) &allocbmap, wordsz, nextbit);
0671     while (nextbit != -1) {
0672         inocount++;
0673         nextbit = xfs_next_bit((uint *) &allocbmap, wordsz,
0674                        nextbit + 1);
0675     }
0676 
0677     if (inocount != rec->ir_count)
0678         return -EFSCORRUPTED;
0679 
0680     return 0;
0681 }
0682 #endif  /* DEBUG */
0683 
0684 static xfs_extlen_t
0685 xfs_inobt_max_size(
0686     struct xfs_perag    *pag)
0687 {
0688     struct xfs_mount    *mp = pag->pag_mount;
0689     xfs_agblock_t       agblocks = pag->block_count;
0690 
0691     /* Bail out if we're uninitialized, which can happen in mkfs. */
0692     if (M_IGEO(mp)->inobt_mxr[0] == 0)
0693         return 0;
0694 
0695     /*
0696      * The log is permanently allocated, so the space it occupies will
0697      * never be available for the kinds of things that would require btree
0698      * expansion.  We therefore can pretend the space isn't there.
0699      */
0700     if (xfs_ag_contains_log(mp, pag->pag_agno))
0701         agblocks -= mp->m_sb.sb_logblocks;
0702 
0703     return xfs_btree_calc_size(M_IGEO(mp)->inobt_mnr,
0704                 (uint64_t)agblocks * mp->m_sb.sb_inopblock /
0705                     XFS_INODES_PER_CHUNK);
0706 }
0707 
0708 /* Read AGI and create inobt cursor. */
0709 int
0710 xfs_inobt_cur(
0711     struct xfs_mount    *mp,
0712     struct xfs_trans    *tp,
0713     struct xfs_perag    *pag,
0714     xfs_btnum_t     which,
0715     struct xfs_btree_cur    **curpp,
0716     struct xfs_buf      **agi_bpp)
0717 {
0718     struct xfs_btree_cur    *cur;
0719     int         error;
0720 
0721     ASSERT(*agi_bpp == NULL);
0722     ASSERT(*curpp == NULL);
0723 
0724     error = xfs_ialloc_read_agi(pag, tp, agi_bpp);
0725     if (error)
0726         return error;
0727 
0728     cur = xfs_inobt_init_cursor(mp, tp, *agi_bpp, pag, which);
0729     *curpp = cur;
0730     return 0;
0731 }
0732 
0733 static int
0734 xfs_inobt_count_blocks(
0735     struct xfs_mount    *mp,
0736     struct xfs_trans    *tp,
0737     struct xfs_perag    *pag,
0738     xfs_btnum_t     btnum,
0739     xfs_extlen_t        *tree_blocks)
0740 {
0741     struct xfs_buf      *agbp = NULL;
0742     struct xfs_btree_cur    *cur = NULL;
0743     int         error;
0744 
0745     error = xfs_inobt_cur(mp, tp, pag, btnum, &cur, &agbp);
0746     if (error)
0747         return error;
0748 
0749     error = xfs_btree_count_blocks(cur, tree_blocks);
0750     xfs_btree_del_cursor(cur, error);
0751     xfs_trans_brelse(tp, agbp);
0752 
0753     return error;
0754 }
0755 
0756 /* Read finobt block count from AGI header. */
0757 static int
0758 xfs_finobt_read_blocks(
0759     struct xfs_perag    *pag,
0760     struct xfs_trans    *tp,
0761     xfs_extlen_t        *tree_blocks)
0762 {
0763     struct xfs_buf      *agbp;
0764     struct xfs_agi      *agi;
0765     int         error;
0766 
0767     error = xfs_ialloc_read_agi(pag, tp, &agbp);
0768     if (error)
0769         return error;
0770 
0771     agi = agbp->b_addr;
0772     *tree_blocks = be32_to_cpu(agi->agi_fblocks);
0773     xfs_trans_brelse(tp, agbp);
0774     return 0;
0775 }
0776 
0777 /*
0778  * Figure out how many blocks to reserve and how many are used by this btree.
0779  */
0780 int
0781 xfs_finobt_calc_reserves(
0782     struct xfs_mount    *mp,
0783     struct xfs_trans    *tp,
0784     struct xfs_perag    *pag,
0785     xfs_extlen_t        *ask,
0786     xfs_extlen_t        *used)
0787 {
0788     xfs_extlen_t        tree_len = 0;
0789     int         error;
0790 
0791     if (!xfs_has_finobt(mp))
0792         return 0;
0793 
0794     if (xfs_has_inobtcounts(mp))
0795         error = xfs_finobt_read_blocks(pag, tp, &tree_len);
0796     else
0797         error = xfs_inobt_count_blocks(mp, tp, pag, XFS_BTNUM_FINO,
0798                 &tree_len);
0799     if (error)
0800         return error;
0801 
0802     *ask += xfs_inobt_max_size(pag);
0803     *used += tree_len;
0804     return 0;
0805 }
0806 
0807 /* Calculate the inobt btree size for some records. */
0808 xfs_extlen_t
0809 xfs_iallocbt_calc_size(
0810     struct xfs_mount    *mp,
0811     unsigned long long  len)
0812 {
0813     return xfs_btree_calc_size(M_IGEO(mp)->inobt_mnr, len);
0814 }
0815 
0816 int __init
0817 xfs_inobt_init_cur_cache(void)
0818 {
0819     xfs_inobt_cur_cache = kmem_cache_create("xfs_inobt_cur",
0820             xfs_btree_cur_sizeof(xfs_inobt_maxlevels_ondisk()),
0821             0, 0, NULL);
0822 
0823     if (!xfs_inobt_cur_cache)
0824         return -ENOMEM;
0825     return 0;
0826 }
0827 
0828 void
0829 xfs_inobt_destroy_cur_cache(void)
0830 {
0831     kmem_cache_destroy(xfs_inobt_cur_cache);
0832     xfs_inobt_cur_cache = NULL;
0833 }