当前位置: 技术问答>linux和unix
看书遇到点问题,请指教
来源: 互联网 发布时间:2016-01-19
本文导语: 在看内核源代码情景分析(2.4版本)看到共享内存时,碰到了一个奇怪的问题,就是当创建一个shm文件系统文件inode节点时,有这么一段代码: struct inode *shmem_get_inode(struct super_block *sb, int mode, int dev) { struct inode * inode;...
在看内核源代码情景分析(2.4版本)看到共享内存时,碰到了一个奇怪的问题,就是当创建一个shm文件系统文件inode节点时,有这么一段代码:
struct inode *shmem_get_inode(struct super_block *sb, int mode, int dev)
{
struct inode * inode;
spin_lock (&sb->u.shmem_sb.stat_lock);
if (!sb->u.shmem_sb.free_inodes) {
spin_unlock (&sb->u.shmem_sb.stat_lock);
return NULL;
}
sb->u.shmem_sb.free_inodes--;
spin_unlock (&sb->u.shmem_sb.stat_lock);
inode = new_inode(sb);
if (inode) {
inode->i_mode = mode;
inode->i_uid = current->fsuid;
inode->i_gid = current->fsgid;
inode->i_blksize = PAGE_CACHE_SIZE;
inode->i_blocks = 0;
inode->i_rdev = to_kdev_t(dev);
inode->i_mapping->a_ops = &shmem_aops;
inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
spin_lock_init (&inode->u.shmem_i.lock);
switch (mode & S_IFMT) {
default:
init_special_inode(inode, mode, dev);
break;
case S_IFREG:
inode->i_op = &shmem_inode_operations;
inode->i_fop = &shmem_file_operations;
break;
case S_IFDIR:
inode->i_op = &shmem_dir_inode_operations;
inode->i_fop = &shmem_dir_operations;
break;
case S_IFLNK:
inode->i_op = &page_symlink_inode_operations;
break;
}
spin_lock (&shmem_ilock);
list_add (&inode->u.shmem_i.list, &shmem_inodes);
spin_unlock (&shmem_ilock);
}
return inode;
}
以下这行我不能理解:
inode->i_mapping->a_ops = &shmem_aops;
inode有个address_space指针i_mapping,上面代码中都没有为i_mapping指向的address_space分配空间,为什么就能对a_ops赋值呢?
谢谢~
struct inode *shmem_get_inode(struct super_block *sb, int mode, int dev)
{
struct inode * inode;
spin_lock (&sb->u.shmem_sb.stat_lock);
if (!sb->u.shmem_sb.free_inodes) {
spin_unlock (&sb->u.shmem_sb.stat_lock);
return NULL;
}
sb->u.shmem_sb.free_inodes--;
spin_unlock (&sb->u.shmem_sb.stat_lock);
inode = new_inode(sb);
if (inode) {
inode->i_mode = mode;
inode->i_uid = current->fsuid;
inode->i_gid = current->fsgid;
inode->i_blksize = PAGE_CACHE_SIZE;
inode->i_blocks = 0;
inode->i_rdev = to_kdev_t(dev);
inode->i_mapping->a_ops = &shmem_aops;
inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
spin_lock_init (&inode->u.shmem_i.lock);
switch (mode & S_IFMT) {
default:
init_special_inode(inode, mode, dev);
break;
case S_IFREG:
inode->i_op = &shmem_inode_operations;
inode->i_fop = &shmem_file_operations;
break;
case S_IFDIR:
inode->i_op = &shmem_dir_inode_operations;
inode->i_fop = &shmem_dir_operations;
break;
case S_IFLNK:
inode->i_op = &page_symlink_inode_operations;
break;
}
spin_lock (&shmem_ilock);
list_add (&inode->u.shmem_i.list, &shmem_inodes);
spin_unlock (&shmem_ilock);
}
return inode;
}
以下这行我不能理解:
inode->i_mapping->a_ops = &shmem_aops;
inode有个address_space指针i_mapping,上面代码中都没有为i_mapping指向的address_space分配空间,为什么就能对a_ops赋值呢?
谢谢~
|
shmem_aops 是一个静态的文件范围内的变量。你看看它的定义:
static const struct address_space_operation shem_aops={
...
}
在这个地方已经给它分配空间了。
我是在2.6内核中看到的,代码应该差不多。
static const struct address_space_operation shem_aops={
...
}
在这个地方已经给它分配空间了。
我是在2.6内核中看到的,代码应该差不多。