diff options
-rw-r--r-- | fs/file_table.c | 12 | ||||
-rw-r--r-- | fs/internal.h | 1 | ||||
-rw-r--r-- | fs/namei.c | 5 |
3 files changed, 17 insertions, 1 deletions
diff --git a/fs/file_table.c b/fs/file_table.c index ee21b3da9d08..e68e97d4f00a 100644 --- a/fs/file_table.c +++ b/fs/file_table.c @@ -82,6 +82,18 @@ static inline void file_free(struct file *f) call_rcu(&f->f_rcuhead, file_free_rcu); } +void release_empty_file(struct file *f) +{ + WARN_ON_ONCE(f->f_mode & (FMODE_BACKING | FMODE_OPENED)); + /* Uhm, we better find out who grabs references to an unopened file. */ + WARN_ON_ONCE(atomic_long_cmpxchg(&f->f_count, 1, 0) != 1); + security_file_free(f); + put_cred(f->f_cred); + if (likely(!(f->f_mode & FMODE_NOACCOUNT))) + percpu_counter_dec(&nr_files); + kmem_cache_free(filp_cachep, f); +} + /* * Return the total number of open files in the system */ diff --git a/fs/internal.h b/fs/internal.h index 8260c738980c..f08d8fe3ae5e 100644 --- a/fs/internal.h +++ b/fs/internal.h @@ -94,6 +94,7 @@ extern void chroot_fs_refs(const struct path *, const struct path *); struct file *alloc_empty_file(int flags, const struct cred *cred); struct file *alloc_empty_file_noaccount(int flags, const struct cred *cred); struct file *alloc_empty_backing_file(int flags, const struct cred *cred); +void release_empty_file(struct file *f); static inline void put_file_access(struct file *file) { diff --git a/fs/namei.c b/fs/namei.c index c36ff6f8195a..127c868a8992 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -3783,7 +3783,10 @@ static struct file *path_openat(struct nameidata *nd, WARN_ON(1); error = -EINVAL; } - fput(file); + if (unlikely(file->f_mode & FMODE_OPENED)) + fput(file); + else + release_empty_file(file); if (error == -EOPENSTALE) { if (flags & LOOKUP_RCU) error = -ECHILD; |