123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- /*
- * linux/fs/file_table.c
- *
- * Copyright (C) 1991, 1992 Linus Torvalds
- * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
- */
- #include <linux/string.h>
- #include <linux/slab.h>
- #include <linux/file.h>
- #include <linux/fdtable.h>
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/fs.h>
- #include <linux/security.h>
- #include <linux/eventpoll.h>
- #include <linux/rcupdate.h>
- #include <linux/mount.h>
- #include <linux/capability.h>
- #include <linux/cdev.h>
- #include <linux/fsnotify.h>
- #include <linux/sysctl.h>
- #include <linux/lglock.h>
- #include <linux/percpu_counter.h>
- #include <linux/percpu.h>
- #include <linux/ima.h>
- #include <linux/atomic.h>
- #include "internal.h"
- /* sysctl tunables... */
- struct files_stat_struct files_stat = {
- .max_files = NR_FILE
- };
- /* SLAB cache for file structures */
- static struct kmem_cache *filp_cachep __read_mostly;
- static struct percpu_counter nr_files __cacheline_aligned_in_smp;
- static inline void file_free_rcu(struct rcu_head *head)
- {
- struct file *f = container_of(head, struct file, f_u.fu_rcuhead);
- put_cred(f->f_cred);
- kmem_cache_free(filp_cachep, f);
- }
- static inline void file_free(struct file *f)
- {
- percpu_counter_dec(&nr_files);
- file_check_state(f);
- call_rcu(&f->f_u.fu_rcuhead, file_free_rcu);
- }
- /*
- * Return the total number of open files in the system
- */
- static long get_nr_files(void)
- {
- return percpu_counter_read_positive(&nr_files);
- }
- /*
- * Return the maximum number of open files in the system
- */
- unsigned long get_max_files(void)
- {
- return files_stat.max_files;
- }
- EXPORT_SYMBOL_GPL(get_max_files);
- /*
- * Handle nr_files sysctl
- */
- #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
- int proc_nr_files(ctl_table *table, int write,
- void __user *buffer, size_t *lenp, loff_t *ppos)
- {
- files_stat.nr_files = get_nr_files();
- return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
- }
- #else
- int proc_nr_files(ctl_table *table, int write,
- void __user *buffer, size_t *lenp, loff_t *ppos)
- {
- return -ENOSYS;
- }
- #endif
- /* Find an unused file structure and return a pointer to it.
- * Returns NULL, if there are no more free file structures or
- * we run out of memory.
- *
- * Be very careful using this. You are responsible for
- * getting write access to any mount that you might assign
- * to this filp, if it is opened for write. If this is not
- * done, you will imbalance int the mount's writer count
- * and a warning at __fput() time.
- */
- struct file *get_empty_filp(void)
- {
- const struct cred *cred = current_cred();
- static long old_max;
- struct file * f;
- /*
- * Privileged users can go above max_files
- */
- if (get_nr_files() >= files_stat.max_files && !capable(CAP_SYS_ADMIN)) {
- /*
- * percpu_counters are inaccurate. Do an expensive check before
- * we go and fail.
- */
- if (percpu_counter_sum_positive(&nr_files) >= files_stat.max_files)
- goto over;
- }
- f = kmem_cache_zalloc(filp_cachep, GFP_KERNEL);
- if (f == NULL)
- goto fail;
- percpu_counter_inc(&nr_files);
- f->f_cred = get_cred(cred);
- if (security_file_alloc(f))
- goto fail_sec;
- atomic_long_set(&f->f_count, 1);
- rwlock_init(&f->f_owner.lock);
- spin_lock_init(&f->f_lock);
- eventpoll_init_file(f);
- /* f->f_version: 0 */
- return f;
- over:
- /* Ran out of filps - report that */
- if (get_nr_files() > old_max) {
- pr_info("VFS: file-max limit %lu reached\n", get_max_files());
- old_max = get_nr_files();
- }
- goto fail;
- fail_sec:
- file_free(f);
- fail:
- return NULL;
- }
- /**
- * alloc_file - allocate and initialize a 'struct file'
- * @mnt: the vfsmount on which the file will reside
- * @dentry: the dentry representing the new file
- * @mode: the mode with which the new file will be opened
- * @fop: the 'struct file_operations' for the new file
- *
- * Use this instead of get_empty_filp() to get a new
- * 'struct file'. Do so because of the same initialization
- * pitfalls reasons listed for init_file(). This is a
- * preferred interface to using init_file().
- *
- * If all the callers of init_file() are eliminated, its
- * code should be moved into this function.
- */
- struct file *alloc_file(struct path *path, fmode_t mode,
- const struct file_operations *fop)
- {
- struct file *file;
- file = get_empty_filp();
- if (!file)
- return NULL;
- file->f_path = *path;
- file->f_mapping = path->dentry->d_inode->i_mapping;
- file->f_mode = mode;
- file->f_op = fop;
- /*
- * These mounts don't really matter in practice
- * for r/o bind mounts. They aren't userspace-
- * visible. We do this for consistency, and so
- * that we can do debugging checks at __fput()
- */
- if ((mode & FMODE_WRITE) && !special_file(path->dentry->d_inode->i_mode)) {
- file_take_write(file);
- WARN_ON(mnt_clone_write(path->mnt));
- }
- if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
- i_readcount_inc(path->dentry->d_inode);
- return file;
- }
- EXPORT_SYMBOL(alloc_file);
- /**
- * drop_file_write_access - give up ability to write to a file
- * @file: the file to which we will stop writing
- *
- * This is a central place which will give up the ability
- * to write to @file, along with access to write through
- * its vfsmount.
- */
- static void drop_file_write_access(struct file *file)
- {
- struct vfsmount *mnt = file->f_path.mnt;
- struct dentry *dentry = file->f_path.dentry;
- struct inode *inode = dentry->d_inode;
- put_write_access(inode);
- if (special_file(inode->i_mode))
- return;
- if (file_check_writeable(file) != 0)
- return;
- mnt_drop_write(mnt);
- file_release_write(file);
- }
- /* the real guts of fput() - releasing the last reference to file
- */
- static void __fput(struct file *file)
- {
- struct dentry *dentry = file->f_path.dentry;
- struct vfsmount *mnt = file->f_path.mnt;
- struct inode *inode = dentry->d_inode;
- might_sleep();
- fsnotify_close(file);
- /*
- * The function eventpoll_release() should be the first called
- * in the file cleanup chain.
- */
- eventpoll_release(file);
- locks_remove_file(file);
- if (unlikely(file->f_flags & FASYNC)) {
- if (file->f_op && file->f_op->fasync)
- file->f_op->fasync(-1, file, 0);
- }
- if (file->f_op && file->f_op->release)
- file->f_op->release(inode, file);
- security_file_free(file);
- ima_file_free(file);
- if (unlikely(S_ISCHR(inode->i_mode) && inode->i_cdev != NULL &&
- !(file->f_mode & FMODE_PATH))) {
- cdev_put(inode->i_cdev);
- }
- fops_put(file->f_op);
- put_pid(file->f_owner.pid);
- if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
- i_readcount_dec(inode);
- if (file->f_mode & FMODE_WRITE)
- drop_file_write_access(file);
- file->f_path.dentry = NULL;
- file->f_path.mnt = NULL;
- file_free(file);
- dput(dentry);
- mntput(mnt);
- }
- void fput(struct file *file)
- {
- if (atomic_long_dec_and_test(&file->f_count))
- __fput(file);
- }
- EXPORT_SYMBOL(fput);
- void put_filp(struct file *file)
- {
- if (atomic_long_dec_and_test(&file->f_count)) {
- security_file_free(file);
- file_free(file);
- }
- }
- void __init files_init(unsigned long mempages)
- {
- unsigned long n;
- filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
- SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
- /*
- * One file with associated inode and dcache is very roughly 1K.
- * Per default don't use more than 10% of our memory for files.
- */
- n = (mempages * (PAGE_SIZE / 1024)) / 10;
- files_stat.max_files = max_t(unsigned long, n, NR_FILE);
- files_defer_init();
- percpu_counter_init(&nr_files, 0);
- }
|