xfs bmap实现简析

本文深入剖析了XFS文件系统中的bmap实现,涉及延时分配、预分配策略,以及核心数据结构和操作。内容涵盖xfs_bmap_free_item_t和xfs_bmap_free_t结构,延时分配的优化,以及xfs_bmalloca_t在空间分配中的作用。此外,还介绍了xfs_bmap_tree中的关键数据结构和操作,如xfs_bmdr_block_t和xfs_bmbt_irec_t,以及bmap分配和释放的相关函数。
摘要由CSDN通过智能技术生成
1.xfs_bmap用于对用户数据进行存储管理,其中的重点有延时分配和预分配
    设计源码文件xfs_bmap.h xfs_bmap.c xfs_bmap_tree.h xfs_bmap_tree.c

2.依赖:
    xfs bmap和xfs bmap tree的实现依赖于xfs_btree的实现,独立分析。

3.头文件设计的核心数据结构及宏分析
xfs_bmap.h
    typedef struct xfs_bmap_free_item
    {
            xfs_fsblock_t           xbfi_startblock;/* starting fs block number */
            xfs_extlen_t            xbfi_blockcount;/* number of blocks in extent */
            struct xfs_bmap_free_item *xbfi_next;   /* link to next entry */
    } xfs_bmap_free_item_t;
    该结构用于稍后将释放的extent的map信息,根据xbfi_startblock排序

    typedef struct xfs_bmap_free
    {
            xfs_bmap_free_item_t    *xbf_first;     /* list of to-be-free extents */
            int                     xbf_count;      /* count of items on list */
            int                     xbf_low;        /* alloc in low mode */
    } xfs_bmap_free_t;
    即将释放的extent的链表头
    xbf_low:表示分配器启用低空间分配算法,what is lowspace algorithm?

    #define XFS_BMAP_MAX_NMAP       4

    #define XFS_BMAPI_ENTIRE        0x001   /* return entire extent, not trimmed */
    #define XFS_BMAPI_METADATA      0x002   /* mapping metadata not user data */
    #define XFS_BMAPI_ATTRFORK      0x004   /* use attribute fork not data */
    #define XFS_BMAPI_PREALLOC      0x008   /* preallocation op: unwritten space */
    #define XFS_BMAPI_IGSTATE       0x010   /* Ignore state - */
                                                                                /* combine contig. space */
    #define XFS_BMAPI_CONTIG        0x020   /* must allocate only one extent */

    /*
     * unwritten extent conversion - this needs write cache flushing and no additional
     * allocation alignments. When specified with XFS_BMAPI_PREALLOC it converts
     * from written to unwritten, otherwise convert from unwritten to written.
     */
    #define XFS_BMAPI_CONVERT       0x040
    #define XFS_BMAPI_STACK_SWITCH  0x080    /* 用于分配存储空间时调用__xfs_bmapi_allocate是直接调用
                                                                                            * 还是用分配工作队列,默认使用分配工作队列; 仅函数
                                                                                            * xfs_iomap_write_allocate使用该方法,用于延时分配
                                                                                            * 延时分配直接调用分配函数,已提高延时分配的效率 */

    #define DELAYSTARTBLOCK         ((xfs_fsblock_t)-1LL)
    #define HOLESTARTBLOCK          ((xfs_fsblock_t)-2LL)

    /*
     * Flags for xfs_bmap_add_extent*.
     */
    #define BMAP_LEFT_CONTIG        (1 << 0)
    #define BMAP_RIGHT_CONTIG       (1 << 1)
    #define BMAP_LEFT_FILLING       (1 << 2)
    #define BMAP_RIGHT_FILLING      (1 << 3)
    #define BMAP_LEFT_DELAY         (1 << 4)
    #define BMAP_RIGHT_DELAY        (1 << 5)
    #define BMAP_LEFT_VALID         (1 << 6)
    #define BMAP_RIGHT_VALID        (1 << 7)
    #define BMAP_ATTRFORK           (1 << 8)

    typedef struct xfs_bmalloca {
        xfs_fsblock_t           *firstblock; /* i/o first block allocated */
        struct xfs_bmap_free    *flist; /* bmap freelist */
        struct xfs_trans        *tp;    /* transaction pointer */
        struct xfs_inode        *ip;    /* incore inode pointer */
        struct xfs_bmbt_irec    prev;   /* extent before the new one */
        struct xfs_bmbt_irec    got;    /* extent after, or delayed */

        xfs_fileoff_t           offset; /* offset in file filling in */
        xfs_extlen_t            length; /* i/o length asked/allocated */
        xfs_fsblock_t           blkno;  /* starting block of new extent */

        struct xfs_btree_cur    *cur;   /* btree cursor */
        xfs_extnum_t            idx;    /* current extent index */
        int                     nallocs;/* number of extents alloc'd */
        int                     logflags;/* flags for transaction logging */

        xfs_extlen_t            total;  /* total blocks needed for xaction */
        xfs_extlen_t            minlen; /* minimum allocation size (blocks) */
        xfs_extlen_t            minleft; /* amount must be left after alloc */
        char                    eof;    /* set if allocating past last extent */
        char                    wasdel; /* replacing a delayed allocation */
        char                    userdata;/* set if is user data */
        char                    aeof;   /* allocated space at eof */
        char                    conv;   /* overwriting unwritten extents */
        char                    stack_switch; /* for delayed allocation, call directly function:__xfs_bmapi_allocate,
                                                                don't use allocate workqueue. Because of delayed allocation need to
                                                                response as fast as quickly */
        int                     flags;
        struct completion       *done;
        struct work_struct      work;
        int                     result;
    } xfs_bmalloca_t;

xfs_bmap_tree.h    
    /*
     * Bmap root header, on-disk form only.
     */
    typedef struct xfs_bmdr_block {
            __be16          bb_level;       /* 0 is a leaf */
            __be16          bb_numrecs;     /* current # of data records */
    } xfs_bmdr_block_t;

    /*
     * Bmap btree record and extent descriptor.
     *  l0:63 is an extent flag (value 1 indicates non-normal).
     *  l0:9-62 are startoff.
     *  l0:0-8 and l1:21-63 are startblock.
     *  l1:0-20 are blockcount.
     */
    #define BMBT_EXNTFLAG_BITLEN    1
    #define BMBT_STARTOFF_BITLEN    54
    #define BMBT_STARTBLOCK_BITLEN  52
    #define BMBT_BLOCKCOUNT_BITLEN  21

    记录在设备上的格式
    typedef struct xfs_bmbt_rec {
            __be64                  l0, l1;
    } xfs_bmbt_rec_t;
    typedef xfs_bmbt_rec_t
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值