Commit 39b652527457452f09b35044fb4f8b3b0eabafdf

Authored by Anatol Pomozov
Committed by Al Viro
1 parent 1afc99beaf

fs: Preserve error code in get_empty_filp(), part 2

Allocating a file structure in function get_empty_filp() might fail because
of several reasons:
 - not enough memory for file structures
 - operation is not allowed
 - user is over its limit

Currently the function returns NULL in all cases and we loose the exact
reason of the error. All callers of get_empty_filp() assume that the function
can fail with ENFILE only.

Return error through pointer. Change all callers to preserve this error code.

[AV: cleaned up a bit, carved the get_empty_filp() part out into a separate commit
(things remaining here deal with alloc_file()), removed pipe(2) behaviour change]

Signed-off-by: Anatol Pomozov <anatol.pomozov@gmail.com>
Reviewed-by: "Theodore Ts'o" <tytso@mit.edu>
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>

Showing 8 changed files with 19 additions and 22 deletions Side-by-side Diff

arch/ia64/kernel/perfmon.c
... ... @@ -2221,9 +2221,9 @@
2221 2221 d_add(path.dentry, inode);
2222 2222  
2223 2223 file = alloc_file(&path, FMODE_READ, &pfm_file_ops);
2224   - if (!file) {
  2224 + if (IS_ERR(file)) {
2225 2225 path_put(&path);
2226   - return ERR_PTR(-ENFILE);
  2226 + return file;
2227 2227 }
2228 2228  
2229 2229 file->f_flags = O_RDONLY;
... ... @@ -131,7 +131,6 @@
131 131 struct qstr this;
132 132 struct path path;
133 133 struct file *file;
134   - int error;
135 134  
136 135 if (IS_ERR(anon_inode_inode))
137 136 return ERR_PTR(-ENODEV);
... ... @@ -143,7 +142,7 @@
143 142 * Link the inode to a directory entry by creating a unique name
144 143 * using the inode sequence number.
145 144 */
146   - error = -ENOMEM;
  145 + file = ERR_PTR(-ENOMEM);
147 146 this.name = name;
148 147 this.len = strlen(name);
149 148 this.hash = 0;
150 149  
... ... @@ -160,9 +159,8 @@
160 159  
161 160 d_instantiate(path.dentry, anon_inode_inode);
162 161  
163   - error = -ENFILE;
164 162 file = alloc_file(&path, OPEN_FMODE(flags), fops);
165   - if (!file)
  163 + if (IS_ERR(file))
166 164 goto err_dput;
167 165 file->f_mapping = anon_inode_inode->i_mapping;
168 166  
... ... @@ -177,7 +175,7 @@
177 175 path_put(&path);
178 176 err_module:
179 177 module_put(fops->owner);
180   - return ERR_PTR(error);
  178 + return file;
181 179 }
182 180 EXPORT_SYMBOL_GPL(anon_inode_getfile);
183 181  
... ... @@ -173,7 +173,7 @@
173 173  
174 174 file = get_empty_filp();
175 175 if (IS_ERR(file))
176   - return NULL;
  176 + return file;
177 177  
178 178 file->f_path = *path;
179 179 file->f_mapping = path->dentry->d_inode->i_mapping;
fs/hugetlbfs/inode.c
... ... @@ -923,8 +923,7 @@
923 923 struct user_struct **user,
924 924 int creat_flags, int page_size_log)
925 925 {
926   - int error = -ENOMEM;
927   - struct file *file;
  926 + struct file *file = ERR_PTR(-ENOMEM);
928 927 struct inode *inode;
929 928 struct path path;
930 929 struct dentry *root;
... ... @@ -964,7 +963,7 @@
964 963 goto out_shm_unlock;
965 964  
966 965 path.mnt = mntget(hugetlbfs_vfsmount[hstate_idx]);
967   - error = -ENOSPC;
  966 + file = ERR_PTR(-ENOSPC);
968 967 inode = hugetlbfs_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0);
969 968 if (!inode)
970 969 goto out_dentry;
... ... @@ -973,7 +972,7 @@
973 972 size += addr & ~huge_page_mask(hstate);
974 973 num_pages = ALIGN(size, huge_page_size(hstate)) >>
975 974 huge_page_shift(hstate);
976   - error = -ENOMEM;
  975 + file = ERR_PTR(-ENOMEM);
977 976 if (hugetlb_reserve_pages(inode, 0, num_pages, NULL, acctflag))
978 977 goto out_inode;
979 978  
980 979  
... ... @@ -981,10 +980,9 @@
981 980 inode->i_size = size;
982 981 clear_nlink(inode);
983 982  
984   - error = -ENFILE;
985 983 file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
986 984 &hugetlbfs_file_operations);
987   - if (!file)
  985 + if (IS_ERR(file))
988 986 goto out_dentry; /* inode is already attached */
989 987  
990 988 return file;
... ... @@ -998,7 +996,7 @@
998 996 user_shm_unlock(size, *user);
999 997 *user = NULL;
1000 998 }
1001   - return ERR_PTR(error);
  999 + return file;
1002 1000 }
1003 1001  
1004 1002 static int __init init_hugetlbfs_fs(void)
... ... @@ -1037,13 +1037,13 @@
1037 1037  
1038 1038 err = -ENFILE;
1039 1039 f = alloc_file(&path, FMODE_WRITE, &write_pipefifo_fops);
1040   - if (!f)
  1040 + if (IS_ERR(f))
1041 1041 goto err_dentry;
1042 1042  
1043 1043 f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT));
1044 1044  
1045 1045 res[0] = alloc_file(&path, FMODE_READ, &read_pipefifo_fops);
1046   - if (!res[0])
  1046 + if (IS_ERR(res[0]))
1047 1047 goto err_file;
1048 1048  
1049 1049 path_get(&path);
... ... @@ -1042,7 +1042,8 @@
1042 1042 is_file_hugepages(shp->shm_file) ?
1043 1043 &shm_file_operations_huge :
1044 1044 &shm_file_operations);
1045   - if (!file)
  1045 + err = PTR_ERR(file);
  1046 + if (IS_ERR(file))
1046 1047 goto out_free;
1047 1048  
1048 1049 file->private_data = sfd;
... ... @@ -2913,10 +2913,10 @@
2913 2913 goto put_dentry;
2914 2914 #endif
2915 2915  
2916   - error = -ENFILE;
2917 2916 file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
2918 2917 &shmem_file_operations);
2919   - if (!file)
  2918 + error = PTR_ERR(file);
  2919 + if (IS_ERR(file))
2920 2920 goto put_dentry;
2921 2921  
2922 2922 return file;
... ... @@ -370,11 +370,11 @@
370 370  
371 371 file = alloc_file(&path, FMODE_READ | FMODE_WRITE,
372 372 &socket_file_ops);
373   - if (unlikely(!file)) {
  373 + if (unlikely(IS_ERR(file))) {
374 374 /* drop dentry, keep inode */
375 375 ihold(path.dentry->d_inode);
376 376 path_put(&path);
377   - return ERR_PTR(-ENFILE);
  377 + return file;
378 378 }
379 379  
380 380 sock->file = file;