filesystem helpers for custom 'struct file's
Subsystems (6)
RFC PATCH 1/1 filesystem helpers for custom 'struct file's
Some filesystems forego the vfs and may_open() and create their own 'struct file's.
This patch creates a couple of helper functions which can be used by these filesystems, and will provide a unified place which the r/o bind mount code may patch.
---
lxc-dave/include/linux/file.h | 8 ++++++++ lxc-dave/fs/file_table.c | 30 ++++++++++++++++++++++++++++++ lxc-dave/fs/hugetlbfs/inode.c | 22 +++++++++------------- lxc-dave/mm/shmem.c | 7 ++----- lxc-dave/mm/tiny-shmem.c | 24 +++++++++--------------- lxc-dave/net/socket.c | 17 ++++++++--------- 6 files changed, 66 insertions(+), 42 deletions(-)
include/linux/file.h renamed from include/linux/file.h~B0-helpers-for-custom-struct_files +8 -0
@@ -67,6 +67,14 @@ struct files_struct {extern void FASTCALL (__fput (struct file *));extern void FASTCALL (fput (struct file *)); +struct file_operations ; +struct vfsmount ; +struct dentry ; +extern int f_init (struct file *,struct vfsmount *,struct dentry *dentry, +mode_t mode,const struct file_operations *fop); +extern struct file *f_alloc (struct vfsmount *,struct dentry *dentry, +mode_t mode,const struct file_operations *fop); +static inline void fput_light (struct file *file,int fput_needed) {if (unlikely (fput_needed))
fs/file_table.c renamed from fs/file_table.c~B0-helpers-for-custom-struct_files +30 -0
@@ -140,6 +140,36 @@ fail:EXPORT_SYMBOL (get_empty_filp); +struct file *f_alloc (struct vfsmount *mnt,struct dentry *dentry, +mode_t mode,const struct file_operations *fop) +{ +struct file *file; + + file =get_empty_filp (); +if (!file) +return NULL ; + +f_init (file, mnt, dentry, mode, fop); +return file; +} + +EXPORT_SYMBOL (f_alloc); + +int f_init (struct file *file,struct vfsmount *mnt, +struct dentry *dentry,mode_t mode, +const struct file_operations *fop) +{ +int error =0 ; + file->f_vfsmnt =mntget (mnt); + file->f_dentry = dentry; + file->f_mapping = dentry->d_inode->i_mapping; + file->f_mode = mode; + file->f_op = fop; +return error; +} + +EXPORT_SYMBOL (f_init); + void fastcall fput(struct file *file) {if (atomic_dec_and_test (&file->f_count))
fs/hugetlbfs/inode.c renamed from fs/hugetlbfs/inode.c~B0-helpers-for-custom-struct_files +9 -13
@@ -764,16 +764,11 @@ struct file *hugetlb_zero_setup(size_t sif (!dentry) goto out_shm_unlock; - error = -ENFILE ; - file =get_empty_filp (); -if (!file) - goto out_dentry; - error = -ENOSPC ; inode =hugetlbfs_get_inode (root->d_sb, current->fsuid, current->fsgid,S_IFREG |S_IRWXUGO ,0 );if (!inode) - goto out_file; + goto out_dentry; error = -ENOMEM ;if (hugetlb_reserve_pages (inode,0 , size >>HPAGE_SHIFT ))
@@ -782,17 +777,18 @@ struct file *hugetlb_zero_setup(size_t sd_instantiate (dentry, inode); inode->i_size = size; inode->i_nlink =0 ; - file->f_vfsmnt =mntget (hugetlbfs_vfsmount); - file->f_dentry = dentry; - file->f_mapping = inode->i_mapping; - file->f_op = &hugetlbfs_file_operations; - file->f_mode =FMODE_WRITE |FMODE_READ ; + + error = -ENFILE ; + file =f_alloc (hugetlbfs_vfsmount, dentry, +FMODE_WRITE |FMODE_READ , + &hugetlbfs_file_operations); +if (!file) + goto out_inode; +return file; out_inode:iput (inode); -out_file: -put_filp (file); out_dentry:dput (dentry); out_shm_unlock:
mm/shmem.c renamed from mm/shmem.c~B0-helpers-for-custom-struct_files +2 -5
@@ -2411,11 +2411,8 @@ struct file *shmem_file_setup(char *named_instantiate (dentry, inode); inode->i_size = size; inode->i_nlink =0 ;/* It is unlinked */ - file->f_vfsmnt =mntget (shm_mnt); - file->f_dentry = dentry; - file->f_mapping = inode->i_mapping; - file->f_op = &shmem_file_operations; - file->f_mode =FMODE_WRITE |FMODE_READ ; +f_init (file, shm_mnt, dentry,FMODE_WRITE |FMODE_READ , + &shmem_file_operations);return file; close_file:
mm/tiny-shmem.c renamed from mm/tiny-shmem.c~B0-helpers-for-custom-struct_files +9 -15
@@ -66,24 +66,19 @@ struct file *shmem_file_setup(char *nameif (!dentry) goto put_memory; - error = -ENFILE ; - file =get_empty_filp (); -if (!file) - goto put_dentry; - error = -ENOSPC ; inode =ramfs_get_inode (root->d_sb,S_IFREG |S_IRWXUGO ,0 );if (!inode) - goto close_file; + goto put_dentry;d_instantiate (dentry, inode); - inode->i_nlink =0 ;/* It is unlinked */ + error = -ENFILE ; + file =f_alloc (shm_mnt, dentry,FMODE_WRITE |FMODE_READ , + &ramfs_file_operations); +if (!file) + goto put_inode; - file->f_vfsmnt =mntget (shm_mnt); - file->f_dentry = dentry; - file->f_mapping = inode->i_mapping; - file->f_op = &ramfs_file_operations; - file->f_mode =FMODE_WRITE |FMODE_READ ; + inode->i_nlink =0 ;/* It is unlinked */ /* notify everyone as to the change of file size */ error =do_truncate (dentry, size,0 , file);
@@ -91,9 +86,8 @@ struct file *shmem_file_setup(char *name goto close_file;return file; - -close_file: -put_filp (file); +put_inode: +iput (inode); put_dentry:dput (dentry); put_memory:
net/socket.c renamed from net/socket.c~B0-helpers-for-custom-struct_files +8 -9
@@ -349,6 +349,7 @@ static int sock_alloc_fd(struct file **fstatic int sock_attach_fd (struct socket *sock,struct file *file) { +struct dentry *dentry;struct qstr this;char name[32 ];
@@ -356,18 +357,16 @@ static int sock_attach_fd(struct socket this.name = name; this.hash =SOCK_INODE (sock)->i_ino; - file->f_dentry =d_alloc (sock_mnt->mnt_sb->s_root, &this); -if (unlikely (!file->f_dentry)) + dentry =d_alloc (sock_mnt->mnt_sb->s_root, &this); +if (unlikely (!dentry))return -ENOMEM ; - file->f_dentry->d_op = &sockfs_dentry_operations; -d_add (file->f_dentry,SOCK_INODE (sock)); - file->f_vfsmnt =mntget (sock_mnt); - file->f_mapping = file->f_dentry->d_inode->i_mapping; - + dentry->d_op = &sockfs_dentry_operations; +d_add (dentry,SOCK_INODE (sock)); +f_init (file, sock_mnt, dentry,FMODE_READ |FMODE_WRITE , + &socket_file_ops); +SOCK_INODE (sock)->i_fop = &socket_file_ops; sock->file = file; - file->f_op =SOCK_INODE (sock)->i_fop = &socket_file_ops; - file->f_mode =FMODE_READ |FMODE_WRITE ; file->f_flags =O_RDWR ; file->f_pos =0 ; file->private_data = sock;
_
Quoted 23 lines · Dave Hansen
Some filesystems forego the vfs and
may_open()and create their own 'struct file's.This patch creates a couple of helper functions which can be used by these filesystems, and will provide a unified place which the r/o bind mount code may patch.
Signed-off-by Dave Hansen <[email protected]>---
lxc-dave/include/linux/file.h | 8 ++++++++ lxc-dave/fs/file_table.c | 30 ++++++++++++++++++++++++++++++ lxc-dave/fs/hugetlbfs/inode.c | 22 +++++++++------------- lxc-dave/mm/shmem.c | 7 ++----- lxc-dave/mm/tiny-shmem.c | 24 +++++++++--------------- lxc-dave/net/socket.c | 17 ++++++++--------- 6 files changed, 66 insertions(+), 42 deletions(-)diff -puN
include/linux/file.h~B0-helpers-for-custom-struct_filesinclude/linux/file.hinclude/linux/file.h renamed from include/linux/file.h~B0-helpers-for-custom-struct_files +0
-0@@ -67,6 +67,14 @@ struct files_struct {extern void FASTCALL (__fput (struct file *));
include/linux/file.h struct files_struct { · line 77
Quoted 10 lines
include/linux/file.h renamed from include/linux/file.h~B0-helpers-for-custom-struct_files +8
-0extern void FASTCALL (fput (struct file *)); +struct file_operations ; +struct vfsmount ; +struct dentry ; +extern int f_init (struct file *,struct vfsmount *,struct dentry *dentry, +mode_t mode,const struct file_operations *fop); +extern struct file *f_alloc (struct vfsmount *,struct dentry *dentry, +mode_t mode,const struct file_operations *fop); +
To be honest I really hate this names. init_file and alloc_file sound a lot better.
Quoted 4 lines
include/linux/file.h renamed from include/linux/file.h~B0-helpers-for-custom-struct_files +4
-0+int f_init (struct file *file,struct vfsmount *mnt, +struct dentry *dentry,mode_t mode, +const struct file_operations *fop) +{
include/linux/file.h struct files_struct {
Quoted 10 lines
include/linux/file.h renamed from include/linux/file.h~B0-helpers-for-custom-struct_files +10
-0+int error =0 ; + file->f_vfsmnt =mntget (mnt); + file->f_dentry = dentry; + file->f_mapping = dentry->d_inode->i_mapping; + file->f_mode = mode; + file->f_op = fop; +return error; +} + +EXPORT_SYMBOL (f_init);
Do we really need this one? I think we should only need the alloc version.
Quoted 6 lines
diff -puN
mm/shmem.c~B0-helpers-for-custom-struct_filesmm/shmem.cmm/shmem.c renamed from mm/shmem.c~B0-helpers-for-custom-struct_files +0
-0@@ -2411,11 +2411,8 @@ struct file *shmem_file_setup(char *named_instantiate (dentry, inode); inode->i_size = size;
mm/shmem.c struct file *shmem_file_setup(char *name · line 2417
Quoted 10 lines
mm/shmem.c renamed from mm/shmem.c~B0-helpers-for-custom-struct_files +2
-5inode->i_nlink =0 ;/* It is unlinked */ - file->f_vfsmnt =mntget (shm_mnt); - file->f_dentry = dentry; - file->f_mapping = inode->i_mapping; - file->f_op = &shmem_file_operations; - file->f_mode =FMODE_WRITE |FMODE_READ ; +f_init (file, shm_mnt, dentry,FMODE_WRITE |FMODE_READ , + &shmem_file_operations);return file;
This should be able to use the alloc variant by moving the file allocation after the inode allocation. Which makes a lot of sense anyway as shmem_get_inode can fail for accounting reasons.
Quoted 21 lines
mm/shmem.c renamed from mm/shmem.c~B0-helpers-for-custom-struct_files +4
-7{ +struct dentry *dentry;struct qstr this;char name[32 ];@@ -356,18 +357,16 @@ static int sock_attach_fd(struct socket this.name = name; this.hash =SOCK_INODE (sock)->i_ino; - file->f_dentry =d_alloc (sock_mnt->mnt_sb->s_root, &this); -if (unlikely (!file->f_dentry)) + dentry =d_alloc (sock_mnt->mnt_sb->s_root, &this); +if (unlikely (!dentry))return -ENOMEM ; - file->f_dentry->d_op = &sockfs_dentry_operations; -d_add (file->f_dentry,SOCK_INODE (sock)); - file->f_vfsmnt =mntget (sock_mnt); - file->f_mapping = file->f_dentry->d_inode->i_mapping; - + dentry->d_op = &sockfs_dentry_operations;
mm/shmem.c static int sock_attach_fd(struct socket · line 372
Quoted 10 lines
mm/shmem.c renamed from mm/shmem.c~B0-helpers-for-custom-struct_files +4
-2+d_add (dentry,SOCK_INODE (sock)); +f_init (file, sock_mnt, dentry,FMODE_READ |FMODE_WRITE , + &socket_file_ops); +SOCK_INODE (sock)->i_fop = &socket_file_ops; sock->file = file; - file->f_op =SOCK_INODE (sock)->i_fop = &socket_file_ops; - file->f_mode =FMODE_READ |FMODE_WRITE ; file->f_flags =O_RDWR ; file->f_pos =0 ; file->private_data = sock;
Similarly the socket code wants a rework. The current split into sock_alloc_fd and sock_attach_fd is highly confusing and the allocation of the file struct before the inode doesn't make a lot of sense.