[PATCH] fix up new filp allocators

Some new uses of get_empty_filp() have crept in; switched
to alloc_file() to make sure that pieces of initialization
won't be missing.

We really need to kill get_empty_filp().

[AV] fixed dentry leak on failure exit in anon_inode_getfd()

Cc: Erez Zadok <ezk@cs.sunysb.edu>
Cc: Trond Myklebust <trond.myklebust@fys.uio.no>
Cc: "J Bruce Fields" <bfields@fieldses.org>
Acked-by: Al Viro <viro@ZenIV.linux.org.uk>
Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Dave Hansen <haveblue@us.ibm.com>
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>

authored by Dave Hansen and committed by Al Viro 430e285e 322ee5b3

+23 -20
+8 -10
fs/anon_inodes.c
··· 81 81 82 82 if (IS_ERR(anon_inode_inode)) 83 83 return -ENODEV; 84 - file = get_empty_filp(); 85 - if (!file) 86 - return -ENFILE; 87 84 88 85 error = get_unused_fd(); 89 86 if (error < 0) 90 - goto err_put_filp; 87 + return error; 91 88 fd = error; 92 89 93 90 /* ··· 111 114 dentry->d_flags &= ~DCACHE_UNHASHED; 112 115 d_instantiate(dentry, anon_inode_inode); 113 116 114 - file->f_path.mnt = mntget(anon_inode_mnt); 115 - file->f_path.dentry = dentry; 117 + error = -ENFILE; 118 + file = alloc_file(anon_inode_mnt, dentry, 119 + FMODE_READ | FMODE_WRITE, fops); 120 + if (!file) 121 + goto err_dput; 116 122 file->f_mapping = anon_inode_inode->i_mapping; 117 123 118 124 file->f_pos = 0; 119 125 file->f_flags = O_RDWR; 120 - file->f_op = fops; 121 - file->f_mode = FMODE_READ | FMODE_WRITE; 122 126 file->f_version = 0; 123 127 file->private_data = priv; 124 128 ··· 130 132 *pfile = file; 131 133 return 0; 132 134 135 + err_dput: 136 + dput(dentry); 133 137 err_put_unused_fd: 134 138 put_unused_fd(fd); 135 - err_put_filp: 136 - put_filp(file); 137 139 return error; 138 140 } 139 141 EXPORT_SYMBOL_GPL(anon_inode_getfd);
+6
fs/file_table.c
··· 83 83 /* Find an unused file structure and return a pointer to it. 84 84 * Returns NULL, if there are no more free file structures or 85 85 * we run out of memory. 86 + * 87 + * Be very careful using this. You are responsible for 88 + * getting write access to any mount that you might assign 89 + * to this filp, if it is opened for write. If this is not 90 + * done, you will imbalance int the mount's writer count 91 + * and a warning at __fput() time. 86 92 */ 87 93 struct file *get_empty_filp(void) 88 94 {
+9 -10
fs/pipe.c
··· 957 957 struct dentry *dentry; 958 958 struct qstr name = { .name = "" }; 959 959 960 - f = get_empty_filp(); 961 - if (!f) 962 - return ERR_PTR(-ENFILE); 963 960 err = -ENFILE; 964 961 inode = get_pipe_inode(); 965 962 if (!inode) 966 - goto err_file; 963 + goto err; 967 964 968 965 err = -ENOMEM; 969 966 dentry = d_alloc(pipe_mnt->mnt_sb->s_root, &name); ··· 975 978 */ 976 979 dentry->d_flags &= ~DCACHE_UNHASHED; 977 980 d_instantiate(dentry, inode); 978 - f->f_path.mnt = mntget(pipe_mnt); 979 - f->f_path.dentry = dentry; 981 + 982 + err = -ENFILE; 983 + f = alloc_file(pipe_mnt, dentry, FMODE_WRITE, &write_pipe_fops); 984 + if (!f) 985 + goto err_dentry; 980 986 f->f_mapping = inode->i_mapping; 981 987 982 988 f->f_flags = O_WRONLY; 983 - f->f_op = &write_pipe_fops; 984 - f->f_mode = FMODE_WRITE; 985 989 f->f_version = 0; 986 990 987 991 return f; 988 992 993 + err_dentry: 994 + dput(dentry); 989 995 err_inode: 990 996 free_pipe_info(inode); 991 997 iput(inode); 992 - err_file: 993 - put_filp(f); 998 + err: 994 999 return ERR_PTR(err); 995 1000 } 996 1001