i_ino - 1"减一?-6ren"> i_ino - 1"减一?-代码是: void ext2_read_inode (struct inode * inode) { struct buffer_head * bh; struct ext2_inod-6ren">
gpt4 book ai didi

c - 为什么在 fs/ext2/inode.c 的 "inode->i_ino - 1"减一?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:36:02 25 4
gpt4 key购买 nike

代码是:

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);

谢谢。

最佳答案

使其从零开始。如您所见,fs/inode.c 中的i_ino 字段随着++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;
}

关于c - 为什么在 fs/ext2/inode.c 的 "inode->i_ino - 1"减一?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18743584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com