1

代码是:

void ext2_read_inode (struct inode * inode)
{
    struct buffer_head * bh;
    struct ext2_inode * raw_inode;
    unsigned long block_group;
    unsigned long group_desc;
    unsigned long desc;
    unsigned long block;
    unsigned long offset;
    struct ext2_group_desc * gdp;

    if (
        ( inode->i_ino != EXT2_ROOT_INO
            && inode->i_ino != EXT2_ACL_IDX_INO
            && inode->i_ino != EXT2_ACL_DATA_INO
            && inode->i_ino < EXT2_FIRST_INO(inode->i_sb)
        ) || inode->i_ino > le32_to_cpu(
                inode->i_sb->u.ext2_sb.s_es->s_inodes_count)
    )
    {
        ext2_error(inode->i_sb, "ext2_read_inode", 
            "bad inode number: %lu", inode->i_ino);
        goto bad_inode;
    }

    block_group = (inode->i_ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);

    if (block_group >= inode->i_sb->u.ext2_sb.s_groups_count) {
        ext2_error(inode->i_sb, "ext2_read_inode", "group >= groups count");
        goto bad_inode;
    }

    group_desc = block_group >> EXT2_DESC_PER_BLOCK_BITS(inode->i_sb);
    desc = block_group & (EXT2_DESC_PER_BLOCK(inode->i_sb) - 1);
    bh = inode->i_sb->u.ext2_sb.s_group_desc[group_desc];

    /* ... other code omitted ... */
}

你能解释一下为什么会出现在-1这里:

block_group = (inode->i_ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);

和这里:

desc = block_group & (EXT2_DESC_PER_BLOCK(inode->i_sb) - 1);

谢谢你。

4

2 回答 2

0

使其从零开始。如您所见, in 中的i_ino字段以fs/inode.c递增++last_ino
它使第一个i_ino等于 1。

/**
 *      new_inode       - obtain an inode
 *      @sb: superblock
 *
 *      Allocates a new inode for given superblock. The default gfp_mask
 *      for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
 *      If HIGHMEM pages are unsuitable or it is known that pages allocated
 *      for the page cache are not reclaimable or migratable,
 *      mapping_set_gfp_mask() must be called with suitable flags on the
 *      newly created inode's mapping
 *
 */
struct inode *new_inode(struct super_block *sb)
{
    /*
     * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
     * error if st_ino won't fit in target struct field. Use 32bit counter
     * here to attempt to avoid that.
     */
    static unsigned int last_ino;
    struct inode *inode;

    spin_lock_prefetch(&inode_lock);

    inode = alloc_inode(sb);
    if (inode) {
            spin_lock(&inode_lock);
            __inode_add_to_lists(sb, NULL, inode);
            inode->i_ino = ++last_ino;
            inode->i_state = 0;
            spin_unlock(&inode_lock);
    }
    return inode;
}
于 2015-03-25T02:16:17.160 回答
-1

Dave Poirier - 第二个扩展文件系统:内部布局 - 3.6。定位一个 Inode说:

知道 inode 1 是 inode 表中定义的第一个 inode,可以使用以下公式:

block group = (inode - 1) / s_inodes_per_group

在这里,我们可以说 Joachim Pileborg- 1是为了“让它从零开始”。

但随后,- 1

desc = block_group & (EXT2_DESC_PER_BLOCK(inode->i_sb) - 1);

与零基础无关;这是另一种写作方式

desc = block_group % EXT2_DESC_PER_BLOCK(inode->i_sb);

因为每个存储块的块组描述符的数量是 2 的幂。

于 2014-09-04T08:25:39.410 回答