[net/socket.c]
static int __init sock_init(void)
{
/* 初始化缓冲池,缓冲池中的节点为socket_alloc
*/
init_inodecache();
[sock_init->init_inodecache]
static int init_inodecache(void)
{
sock_inode_cachep = kmem_cache_create("sock_inode_cache",
sizeof(struct socket_alloc),
0,
(SLAB_HWCACHE_ALIGN |
SLAB_RECLAIM_ACCOUNT |
SLAB_MEM_SPREAD),
init_once);
if (sock_inode_cachep == NULL)
return -ENOMEM;
return 0;
}
sock_inode_cachep是一个缓冲池:static struct kmem_cache *sock_inode_cachep __read_mostly; 节点socket_alloc把socket与inode节点绑定在一起:[include/net/sock.h]
struct socket_alloc {
struct socket socket;
struct inode vfs_inode;
};
继续socket的初始化过程:[sock_init]
/* the kern_mount function is called which “mounts” the pseudofilesystem (of course, it does not have a filesystem mount point!).
* The struct vfsmount pointer returned by kern_mount is assigned to the static pointer sock mnt.
*/
sock_mnt = kern_mount_data(&sock_fs_type, NULL);
内核将socket实现为一个虚拟文件系统,和一般文件系统的不同之处在于它没有挂载点,不能通过mount命令进行挂载。内核把sock_mnt当作socket文件系统的虚拟挂载点:static struct vfsmount *sock_mnt __read_mostly;
[sock_init->kern_mount_data]struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
{
struct vfsmount *mnt;
mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
[sock_init->kern_mount_data->vfs_kern_mount]struct vfsmount* vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
{
struct mount *mnt;
mnt = alloc_vfsmnt(name);
这里的name为"sockfs"[sock_init->kern_mount_data->vfs_kern_mount->alloc_vfsmnt]
static struct mount *alloc_vfsmnt(const char *name)
{
struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
mnt_cache是一个缓冲池,初始化如下:[ fs/namespace.c ]
static struct kmem_cache *mnt_cache __read_mostly;
void __init mnt_init(void)
{
mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
回到alloc_vfsmnt,如果mnt分配成功,完成一些初始化的工作:[sock_init->kern_mount_data->vfs_kern_mount->alloc_vfsmnt]
static struct mount *alloc_vfsmnt(const char *name)
{
...
err = mnt_alloc_id(mnt);
为mnt分配一个ID,放在mnt->mnt_id中。 mnt->mnt_devname = kstrdup(name, GFP_KERNEL);
mnt->mnt_devname名称为"sockfs"。在内核里为这个字符串分配了新的内存。#ifdef CONFIG_SMP
mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
if (!mnt->mnt_pcp)
goto out_free_devname;
this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
#else
mnt->mnt_count = 1;
mnt->mnt_writers = 0;
#endif
如果是多CPU,为每个CPU分配一个一个变量mnt->mnt_pcp,然后将其中的计数mnt->mnt_pcp->mnt_count加1。如果是单CPU,计数变量为mnt->mnt_count,将其设为1。后面就是把当中的列表初始化为空,就返回到了到vfs_kern_mount:[sock_init->kern_mount_data->vfs_kern_mount]
struct vfsmount *vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
{
struct mount *mnt;
mnt = alloc_vfsmnt(name);
这时的mnt如果成功,它的名称mnt->mnt_devname为"sockfs",当中的引用计数为1。此时的flags为MS_KERNMOUNT,所以下面的条件是满足的: struct dentry *root;
if (flags & MS_KERNMOUNT)
mnt->mnt.mnt_flags = MNT_INTERNAL;
root = mount_fs(type, flags, name, data);
到这里,挂载点已经分配好了,在它能使用之前,先要把socket文件系统安装到内核,然后挂载到这个虚拟挂载点上。下面就是要安装文件系统,这里data为空:[sock_init->kern_mount_data->vfs_kern_mount->mount_fs]
struct dentry *mount_fs(struct file_system_type *type, int flags, const char *name, void *data)
{
struct dentry *root;
root = type->mount(type, flags, name, data);
这里的type为sock_fs_type,调用type->mount,回调的是下面的函数:[sock_init->kern_mount_data->vfs_kern_mount->mount_fs->sockfs_mount]
static struct dentry *sockfs_mount(struct file_system_type *fs_type, int flags, const char *dev_name, void *data)
{
return mount_pseudo(fs_type, "socket:", &sockfs_ops, &sockfs_dentry_operations, SOCKFS_MAGIC);
}
[sock_init->kern_mount_data->vfs_kern_mount->mount_fs->sockfs_mount->mount_pseudo]struct dentry *mount_pseudo(struct file_system_type *fs_type, char *name,
const struct super_operations *ops,
const struct dentry_operations *dops, unsigned long magic)
{
struct super_block *s;
s = sget(fs_type, NULL, set_anon_super, MS_NOUSER, NULL);
在安装文件系统时,先创建一个超级块:[sock_init->kern_mount_data->vfs_kern_mount->mount_fs->sockfs_mount->mount_pseudo->sget]
struct super_block *sget(struct file_system_type *type,
int (*test)(struct super_block *,void *),
int (*set)(struct super_block *,void *),
int flags,
void *data)
{
struct super_block *s = NULL;
s = alloc_super(type, flags);
分配创建超级块,flags为MS_NOUSER。如果创建成功,进行初始化: err = set(s, data);
...
s->s_type = type;
strlcpy(s->s_id, type->name, sizeof(s->s_id));
list_add_tail(&s->s_list, &super_blocks);
hlist_add_head(&s->s_instances, &type->fs_supers);
...
get_filesystem(type);
register_shrinker(&s->s_shrink);
调用传进来的函数set,这里是set_anon_super。设置超级块的文件类型s->s_type为sock_fs_type, s->s_id为"sockfs",将其加入全局列表super_blocks,将其加入到文件类型的fs_supers列表,get_filesystem试图加载文件类型所在的模块,将超级块加入到全局列表shrinker_list。完成这些工作,返回到mount_pseudo,继续对超级块完成一些设置:[sock_init->kern_mount_data->vfs_kern_mount->mount_fs->sockfs_mount->mount_pseudo]
s->s_maxbytes = MAX_LFS_FILESIZE;
s->s_blocksize = PAGE_SIZE;
s->s_blocksize_bits = PAGE_SHIFT;
s->s_magic = magic;
s->s_op = ops ? ops : &simple_super_operations;
s->s_time_gran = 1;
root = new_inode(s);
magic的值为SOCKFS_MAGIC,s_op指向sockfs_ops。超级块建立好后,要为文件系统创建一个根目录,先创建根目录对应的inode:[sock_init->kern_mount_data->vfs_kern_mount->mount_fs->sockfs_mount->mount_pseudo->new_inode]
struct inode *new_inode(struct super_block *sb)
{
struct inode *inode;
inode = new_inode_pseudo(sb);
[sock_init->kern_mount_data->vfs_kern_mount->mount_fs->sockfs_mount->mount_pseudo->new_inode->new_inode_pseudo]struct inode *new_inode_pseudo(struct super_block *sb)
{
struct inode *inode = alloc_inode(sb);
[sock_init->kern_mount_data->vfs_kern_mount->mount_fs->sockfs_mount->mount_pseudo->new_inode->new_inode_pseudo->alloc_inode]static struct inode *alloc_inode(struct super_block *sb)
{
struct inode *inode;
if (sb->s_op->alloc_inode)
inode = sb->s_op->alloc_inode(sb);
这里的sb->s_op指向sockfs_ops,的alloc_inode指向sock_alloc_inode:[sock_init->kern_mount_data->vfs_kern_mount->mount_fs->sockfs_mount->mount_pseudo->new_inode->new_inode_pseudo->alloc_inode->sock_alloc_inode]
static struct inode *sock_alloc_inode(struct super_block *sb)
{
struct socket_alloc *ei;
struct socket_wq *wq;
/* 从缓冲池中分配一个socket_alloc结构
*/
ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
if (!ei)
return NULL;
sock_inode_cachep在前面描述过了,返回的节点把socket和inode绑定到了一起。下面先对socket作初始化: /* 分配等待队列结构
* 初始化后,将其赋值给ei->socket.wq
*/
wq = kmalloc(sizeof(*wq), GFP_KERNEL);
if (!wq) {
kmem_cache_free(sock_inode_cachep, ei);
return NULL;
}
init_waitqueue_head(&wq->wait);
wq->fasync_list = NULL;
RCU_INIT_POINTER(ei->socket.wq, wq);
/* 初始化ei->socket
*/
ei->socket.state = SS_UNCONNECTED;
ei->socket.flags = 0;
ei->socket.ops = NULL;
ei->socket.sk = NULL;
ei->socket.file = NULL;
return &ei->vfs_inode;
}
返回值是与socket绑定在一起的inode,返回到了alloc_inode,和文件系统根目录对应的inode就分配好了:[sock_init->kern_mount_data->vfs_kern_mount->mount_fs->sockfs_mount->mount_pseudo->new_inode->new_inode_pseudo->alloc_inode]
if (!inode)
return NULL;
if (unlikely(inode_init_always(sb, inode))) {
if (inode->i_sb->s_op->destroy_inode)
inode->i_sb->s_op->destroy_inode(inode);
else
kmem_cache_free(inode_cachep, inode);
return NULL;
}
return inode;
}
完成inode的初始化,包括设置inode的超级块等,之后返回到了new_inode_pseudo:[sock_init->kern_mount_data->vfs_kern_mount->mount_fs->sockfs_mount->mount_pseudo->new_inode->new_inode_pseudo]
if (inode) {
spin_lock(&inode->i_lock);
inode->i_state = 0;
spin_unlock(&inode->i_lock);
INIT_LIST_HEAD(&inode->i_sb_list);
}
return inode;
}
作一些初始化的工作,返回到new_inode[sock_init->kern_mount_data->vfs_kern_mount->mount_fs->sockfs_mount->mount_pseudo->new_inode]
if (inode)
inode_sb_list_add(inode);
return inode;
}
将inode加入到对应的超级块中inode->i_sb->s_inodes,返回到mount_pseudo中,对新建的inode完成初始化:[sock_init->kern_mount_data->vfs_kern_mount->mount_fs->sockfs_mount->mount_pseudo]
struct dentry *dentry;
struct inode *root;
struct qstr d_name = QSTR_INIT(name, strlen(name));
...
/*
* since this is the first inode, make it number 1. New inodes created
* after this must take care not to collide with it (by passing
* max_reserved of 1 to iunique).
*/
root->i_ino = 1;
root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
root->i_atime = root->i_mtime = root->i_ctime = CURRENT_TIME;
dentry = __d_alloc(s, &d_name);
设置此inode为目录,可读写。最后访问时间,最后修改时间,创建时间都为当前时间。然后创建根目录的目录项,名字d_name的值为"socket:"。设置目录项的名字dentry->d_name.name为"socket",超级块dentry->d_sb为s,对目录的操作dentry->d_op为0。 /* 目录项与inode关联
*/
d_instantiate(dentry, root);
s->s_root = dentry;
s->s_d_op = dops;
s->s_flags |= MS_ACTIVE;
return dget(s->s_root);
目录项的dentry->d_inode指向root(刚分配的inode),超级块的根目录指向denry,超级块对目录的操作指向sockfs_dentry_operations。然后返回到sockfs_mount,这样,对socket的文件系统的安装就结束了。安排在虚拟挂载点sock_mnt上,根目录名称为:"socket:",根目录对应的inode的节点号为1。然后回到mount_fs来:
[sock_init->kern_mount_data->vfs_kern_mount->mount_fs]
sb = root->d_sb;
sb->s_flags |= MS_BORN;
...
return root;
标记超级块为MS_BORN(刚出生),返回到vfs_kern_mount[sock_init->kern_mount_data->vfs_kern_mount]
mnt->mnt.mnt_root = root;
mnt->mnt.mnt_sb = root->d_sb;
mnt->mnt_mountpoint = mnt->mnt.mnt_root;
mnt->mnt_parent = mnt;
lock_mount_hash();
list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts);
unlock_mount_hash();
return &mnt->mnt;
}
将安装好的文件系统挂载到虚拟挂载点上:设置文件系统的根目录mnt->mnt.mnt_root,这里为"socket:",相应的超级块,挂载点就是此根目录,父节点为自己,将mnt加到对应超级块的s_mounts队列中。返回kern_mount_data: if (!IS_ERR(mnt)) {
/*
* it is a longterm mount, don't release mnt until
* we unmount before file sys is unregistered
*/
real_mount(mnt)->mnt_ns = MNT_NS_INTERNAL;
}
return mnt;
}
设置mnt的网络名字空间。返回,这样socket文件系统就安装完成了。