Commit 1afc99be authored by Al Viro's avatar Al Viro

propagate error from get_empty_filp() to its callers

Based on parts from Anatol's patch (the rest is the next commit).
Signed-off-by: default avatarAl Viro <viro@zeniv.linux.org.uk>
parent 496ad9aa
...@@ -94,8 +94,8 @@ int proc_nr_files(ctl_table *table, int write, ...@@ -94,8 +94,8 @@ int proc_nr_files(ctl_table *table, int write,
#endif #endif
/* Find an unused file structure and return a pointer to it. /* Find an unused file structure and return a pointer to it.
* Returns NULL, if there are no more free file structures or * Returns an error pointer if some error happend e.g. we over file
* we run out of memory. * structures limit, run out of memory or operation is not permitted.
* *
* Be very careful using this. You are responsible for * Be very careful using this. You are responsible for
* getting write access to any mount that you might assign * getting write access to any mount that you might assign
...@@ -107,7 +107,8 @@ struct file *get_empty_filp(void) ...@@ -107,7 +107,8 @@ struct file *get_empty_filp(void)
{ {
const struct cred *cred = current_cred(); const struct cred *cred = current_cred();
static long old_max; static long old_max;
struct file * f; struct file *f;
int error;
/* /*
* Privileged users can go above max_files * Privileged users can go above max_files
...@@ -122,13 +123,16 @@ struct file *get_empty_filp(void) ...@@ -122,13 +123,16 @@ struct file *get_empty_filp(void)
} }
f = kmem_cache_zalloc(filp_cachep, GFP_KERNEL); f = kmem_cache_zalloc(filp_cachep, GFP_KERNEL);
if (f == NULL) if (unlikely(!f))
goto fail; return ERR_PTR(-ENOMEM);
percpu_counter_inc(&nr_files); percpu_counter_inc(&nr_files);
f->f_cred = get_cred(cred); f->f_cred = get_cred(cred);
if (security_file_alloc(f)) error = security_file_alloc(f);
goto fail_sec; if (unlikely(error)) {
file_free(f);
return ERR_PTR(error);
}
INIT_LIST_HEAD(&f->f_u.fu_list); INIT_LIST_HEAD(&f->f_u.fu_list);
atomic_long_set(&f->f_count, 1); atomic_long_set(&f->f_count, 1);
...@@ -144,12 +148,7 @@ struct file *get_empty_filp(void) ...@@ -144,12 +148,7 @@ struct file *get_empty_filp(void)
pr_info("VFS: file-max limit %lu reached\n", get_max_files()); pr_info("VFS: file-max limit %lu reached\n", get_max_files());
old_max = get_nr_files(); old_max = get_nr_files();
} }
goto fail; return ERR_PTR(-ENFILE);
fail_sec:
file_free(f);
fail:
return NULL;
} }
/** /**
...@@ -173,7 +172,7 @@ struct file *alloc_file(struct path *path, fmode_t mode, ...@@ -173,7 +172,7 @@ struct file *alloc_file(struct path *path, fmode_t mode,
struct file *file; struct file *file;
file = get_empty_filp(); file = get_empty_filp();
if (!file) if (IS_ERR(file))
return NULL; return NULL;
file->f_path = *path; file->f_path = *path;
......
...@@ -2941,8 +2941,8 @@ static struct file *path_openat(int dfd, struct filename *pathname, ...@@ -2941,8 +2941,8 @@ static struct file *path_openat(int dfd, struct filename *pathname,
int error; int error;
file = get_empty_filp(); file = get_empty_filp();
if (!file) if (IS_ERR(file))
return ERR_PTR(-ENFILE); return file;
file->f_flags = op->open_flag; file->f_flags = op->open_flag;
......
...@@ -810,23 +810,22 @@ struct file *dentry_open(const struct path *path, int flags, ...@@ -810,23 +810,22 @@ struct file *dentry_open(const struct path *path, int flags,
/* We must always pass in a valid mount pointer. */ /* We must always pass in a valid mount pointer. */
BUG_ON(!path->mnt); BUG_ON(!path->mnt);
error = -ENFILE;
f = get_empty_filp(); f = get_empty_filp();
if (f == NULL) if (!IS_ERR(f)) {
return ERR_PTR(error); f->f_flags = flags;
f->f_path = *path;
f->f_flags = flags; error = do_dentry_open(f, NULL, cred);
f->f_path = *path; if (!error) {
error = do_dentry_open(f, NULL, cred); /* from now on we need fput() to dispose of f */
if (!error) { error = open_check_o_direct(f);
error = open_check_o_direct(f); if (error) {
if (error) { fput(f);
fput(f); f = ERR_PTR(error);
}
} else {
put_filp(f);
f = ERR_PTR(error); f = ERR_PTR(error);
} }
} else {
put_filp(f);
f = ERR_PTR(error);
} }
return f; return f;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment