Skip to content

Commit

Permalink
fs: Preserve error code in get_empty_filp(), part 2
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: "Theodore Ts'o" <[email protected]>
Signed-off-by: Al Viro <[email protected]>
  • Loading branch information
anatol authored and Al Viro committed Feb 23, 2013
1 parent 1afc99b commit 39b6525
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 22 deletions.
4 changes: 2 additions & 2 deletions arch/ia64/kernel/perfmon.c
Original file line number Diff line number Diff line change
Expand Up @@ -2221,9 +2221,9 @@ pfm_alloc_file(pfm_context_t *ctx)
d_add(path.dentry, inode);

file = alloc_file(&path, FMODE_READ, &pfm_file_ops);
if (!file) {
if (IS_ERR(file)) {
path_put(&path);
return ERR_PTR(-ENFILE);
return file;
}

file->f_flags = O_RDONLY;
Expand Down
8 changes: 3 additions & 5 deletions fs/anon_inodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ struct file *anon_inode_getfile(const char *name,
struct qstr this;
struct path path;
struct file *file;
int error;

if (IS_ERR(anon_inode_inode))
return ERR_PTR(-ENODEV);
Expand All @@ -143,7 +142,7 @@ struct file *anon_inode_getfile(const char *name,
* Link the inode to a directory entry by creating a unique name
* using the inode sequence number.
*/
error = -ENOMEM;
file = ERR_PTR(-ENOMEM);
this.name = name;
this.len = strlen(name);
this.hash = 0;
Expand All @@ -160,9 +159,8 @@ struct file *anon_inode_getfile(const char *name,

d_instantiate(path.dentry, anon_inode_inode);

error = -ENFILE;
file = alloc_file(&path, OPEN_FMODE(flags), fops);
if (!file)
if (IS_ERR(file))
goto err_dput;
file->f_mapping = anon_inode_inode->i_mapping;

Expand All @@ -177,7 +175,7 @@ struct file *anon_inode_getfile(const char *name,
path_put(&path);
err_module:
module_put(fops->owner);
return ERR_PTR(error);
return file;
}
EXPORT_SYMBOL_GPL(anon_inode_getfile);

Expand Down
2 changes: 1 addition & 1 deletion fs/file_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ struct file *alloc_file(struct path *path, fmode_t mode,

file = get_empty_filp();
if (IS_ERR(file))
return NULL;
return file;

file->f_path = *path;
file->f_mapping = path->dentry->d_inode->i_mapping;
Expand Down
12 changes: 5 additions & 7 deletions fs/hugetlbfs/inode.c
Original file line number Diff line number Diff line change
Expand Up @@ -923,8 +923,7 @@ struct file *hugetlb_file_setup(const char *name, unsigned long addr,
struct user_struct **user,
int creat_flags, int page_size_log)
{
int error = -ENOMEM;
struct file *file;
struct file *file = ERR_PTR(-ENOMEM);
struct inode *inode;
struct path path;
struct dentry *root;
Expand Down Expand Up @@ -964,7 +963,7 @@ struct file *hugetlb_file_setup(const char *name, unsigned long addr,
goto out_shm_unlock;

path.mnt = mntget(hugetlbfs_vfsmount[hstate_idx]);
error = -ENOSPC;
file = ERR_PTR(-ENOSPC);
inode = hugetlbfs_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0);
if (!inode)
goto out_dentry;
Expand All @@ -973,18 +972,17 @@ struct file *hugetlb_file_setup(const char *name, unsigned long addr,
size += addr & ~huge_page_mask(hstate);
num_pages = ALIGN(size, huge_page_size(hstate)) >>
huge_page_shift(hstate);
error = -ENOMEM;
file = ERR_PTR(-ENOMEM);
if (hugetlb_reserve_pages(inode, 0, num_pages, NULL, acctflag))
goto out_inode;

d_instantiate(path.dentry, inode);
inode->i_size = size;
clear_nlink(inode);

error = -ENFILE;
file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
&hugetlbfs_file_operations);
if (!file)
if (IS_ERR(file))
goto out_dentry; /* inode is already attached */

return file;
Expand All @@ -998,7 +996,7 @@ struct file *hugetlb_file_setup(const char *name, unsigned long addr,
user_shm_unlock(size, *user);
*user = NULL;
}
return ERR_PTR(error);
return file;
}

static int __init init_hugetlbfs_fs(void)
Expand Down
4 changes: 2 additions & 2 deletions fs/pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -1037,13 +1037,13 @@ int create_pipe_files(struct file **res, int flags)

err = -ENFILE;
f = alloc_file(&path, FMODE_WRITE, &write_pipefifo_fops);
if (!f)
if (IS_ERR(f))
goto err_dentry;

f->f_flags = O_WRONLY | (flags & (O_NONBLOCK | O_DIRECT));

res[0] = alloc_file(&path, FMODE_READ, &read_pipefifo_fops);
if (!res[0])
if (IS_ERR(res[0]))
goto err_file;

path_get(&path);
Expand Down
3 changes: 2 additions & 1 deletion ipc/shm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,8 @@ long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr,
is_file_hugepages(shp->shm_file) ?
&shm_file_operations_huge :
&shm_file_operations);
if (!file)
err = PTR_ERR(file);
if (IS_ERR(file))
goto out_free;

file->private_data = sfd;
Expand Down
4 changes: 2 additions & 2 deletions mm/shmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -2913,10 +2913,10 @@ struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags
goto put_dentry;
#endif

error = -ENFILE;
file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
&shmem_file_operations);
if (!file)
error = PTR_ERR(file);
if (IS_ERR(file))
goto put_dentry;

return file;
Expand Down
4 changes: 2 additions & 2 deletions net/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,11 +370,11 @@ struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)

file = alloc_file(&path, FMODE_READ | FMODE_WRITE,
&socket_file_ops);
if (unlikely(!file)) {
if (unlikely(IS_ERR(file))) {
/* drop dentry, keep inode */
ihold(path.dentry->d_inode);
path_put(&path);
return ERR_PTR(-ENFILE);
return file;
}

sock->file = file;
Expand Down

0 comments on commit 39b6525

Please sign in to comment.