Back to home page

LXR

 
 

    


0001 /*
0002  *  linux/fs/file_table.c
0003  *
0004  *  Copyright (C) 1991, 1992  Linus Torvalds
0005  *  Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
0006  */
0007 
0008 #include <linux/string.h>
0009 #include <linux/slab.h>
0010 #include <linux/file.h>
0011 #include <linux/fdtable.h>
0012 #include <linux/init.h>
0013 #include <linux/module.h>
0014 #include <linux/fs.h>
0015 #include <linux/security.h>
0016 #include <linux/eventpoll.h>
0017 #include <linux/rcupdate.h>
0018 #include <linux/mount.h>
0019 #include <linux/capability.h>
0020 #include <linux/cdev.h>
0021 #include <linux/fsnotify.h>
0022 #include <linux/sysctl.h>
0023 #include <linux/percpu_counter.h>
0024 #include <linux/percpu.h>
0025 #include <linux/hardirq.h>
0026 #include <linux/task_work.h>
0027 #include <linux/ima.h>
0028 #include <linux/swap.h>
0029 
0030 #include <linux/atomic.h>
0031 
0032 #include "internal.h"
0033 
0034 /* sysctl tunables... */
0035 struct files_stat_struct files_stat = {
0036     .max_files = NR_FILE
0037 };
0038 
0039 /* SLAB cache for file structures */
0040 static struct kmem_cache *filp_cachep __read_mostly;
0041 
0042 static struct percpu_counter nr_files __cacheline_aligned_in_smp;
0043 
0044 static void file_free_rcu(struct rcu_head *head)
0045 {
0046     struct file *f = container_of(head, struct file, f_u.fu_rcuhead);
0047 
0048     put_cred(f->f_cred);
0049     kmem_cache_free(filp_cachep, f);
0050 }
0051 
0052 static inline void file_free(struct file *f)
0053 {
0054     percpu_counter_dec(&nr_files);
0055     call_rcu(&f->f_u.fu_rcuhead, file_free_rcu);
0056 }
0057 
0058 /*
0059  * Return the total number of open files in the system
0060  */
0061 static long get_nr_files(void)
0062 {
0063     return percpu_counter_read_positive(&nr_files);
0064 }
0065 
0066 /*
0067  * Return the maximum number of open files in the system
0068  */
0069 unsigned long get_max_files(void)
0070 {
0071     return files_stat.max_files;
0072 }
0073 EXPORT_SYMBOL_GPL(get_max_files);
0074 
0075 /*
0076  * Handle nr_files sysctl
0077  */
0078 #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
0079 int proc_nr_files(struct ctl_table *table, int write,
0080                      void __user *buffer, size_t *lenp, loff_t *ppos)
0081 {
0082     files_stat.nr_files = get_nr_files();
0083     return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
0084 }
0085 #else
0086 int proc_nr_files(struct ctl_table *table, int write,
0087                      void __user *buffer, size_t *lenp, loff_t *ppos)
0088 {
0089     return -ENOSYS;
0090 }
0091 #endif
0092 
0093 /* Find an unused file structure and return a pointer to it.
0094  * Returns an error pointer if some error happend e.g. we over file
0095  * structures limit, run out of memory or operation is not permitted.
0096  *
0097  * Be very careful using this.  You are responsible for
0098  * getting write access to any mount that you might assign
0099  * to this filp, if it is opened for write.  If this is not
0100  * done, you will imbalance int the mount's writer count
0101  * and a warning at __fput() time.
0102  */
0103 struct file *get_empty_filp(void)
0104 {
0105     const struct cred *cred = current_cred();
0106     static long old_max;
0107     struct file *f;
0108     int error;
0109 
0110     /*
0111      * Privileged users can go above max_files
0112      */
0113     if (get_nr_files() >= files_stat.max_files && !capable(CAP_SYS_ADMIN)) {
0114         /*
0115          * percpu_counters are inaccurate.  Do an expensive check before
0116          * we go and fail.
0117          */
0118         if (percpu_counter_sum_positive(&nr_files) >= files_stat.max_files)
0119             goto over;
0120     }
0121 
0122     f = kmem_cache_zalloc(filp_cachep, GFP_KERNEL);
0123     if (unlikely(!f))
0124         return ERR_PTR(-ENOMEM);
0125 
0126     percpu_counter_inc(&nr_files);
0127     f->f_cred = get_cred(cred);
0128     error = security_file_alloc(f);
0129     if (unlikely(error)) {
0130         file_free(f);
0131         return ERR_PTR(error);
0132     }
0133 
0134     atomic_long_set(&f->f_count, 1);
0135     rwlock_init(&f->f_owner.lock);
0136     spin_lock_init(&f->f_lock);
0137     mutex_init(&f->f_pos_lock);
0138     eventpoll_init_file(f);
0139     /* f->f_version: 0 */
0140     return f;
0141 
0142 over:
0143     /* Ran out of filps - report that */
0144     if (get_nr_files() > old_max) {
0145         pr_info("VFS: file-max limit %lu reached\n", get_max_files());
0146         old_max = get_nr_files();
0147     }
0148     return ERR_PTR(-ENFILE);
0149 }
0150 
0151 /**
0152  * alloc_file - allocate and initialize a 'struct file'
0153  *
0154  * @path: the (dentry, vfsmount) pair for the new file
0155  * @mode: the mode with which the new file will be opened
0156  * @fop: the 'struct file_operations' for the new file
0157  */
0158 struct file *alloc_file(const struct path *path, fmode_t mode,
0159         const struct file_operations *fop)
0160 {
0161     struct file *file;
0162 
0163     file = get_empty_filp();
0164     if (IS_ERR(file))
0165         return file;
0166 
0167     file->f_path = *path;
0168     file->f_inode = path->dentry->d_inode;
0169     file->f_mapping = path->dentry->d_inode->i_mapping;
0170     if ((mode & FMODE_READ) &&
0171          likely(fop->read || fop->read_iter))
0172         mode |= FMODE_CAN_READ;
0173     if ((mode & FMODE_WRITE) &&
0174          likely(fop->write || fop->write_iter))
0175         mode |= FMODE_CAN_WRITE;
0176     file->f_mode = mode;
0177     file->f_op = fop;
0178     if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
0179         i_readcount_inc(path->dentry->d_inode);
0180     return file;
0181 }
0182 EXPORT_SYMBOL(alloc_file);
0183 
0184 /* the real guts of fput() - releasing the last reference to file
0185  */
0186 static void __fput(struct file *file)
0187 {
0188     struct dentry *dentry = file->f_path.dentry;
0189     struct vfsmount *mnt = file->f_path.mnt;
0190     struct inode *inode = file->f_inode;
0191 
0192     might_sleep();
0193 
0194     fsnotify_close(file);
0195     /*
0196      * The function eventpoll_release() should be the first called
0197      * in the file cleanup chain.
0198      */
0199     eventpoll_release(file);
0200     locks_remove_file(file);
0201 
0202     if (unlikely(file->f_flags & FASYNC)) {
0203         if (file->f_op->fasync)
0204             file->f_op->fasync(-1, file, 0);
0205     }
0206     ima_file_free(file);
0207     if (file->f_op->release)
0208         file->f_op->release(inode, file);
0209     security_file_free(file);
0210     if (unlikely(S_ISCHR(inode->i_mode) && inode->i_cdev != NULL &&
0211              !(file->f_mode & FMODE_PATH))) {
0212         cdev_put(inode->i_cdev);
0213     }
0214     fops_put(file->f_op);
0215     put_pid(file->f_owner.pid);
0216     if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
0217         i_readcount_dec(inode);
0218     if (file->f_mode & FMODE_WRITER) {
0219         put_write_access(inode);
0220         __mnt_drop_write(mnt);
0221     }
0222     file->f_path.dentry = NULL;
0223     file->f_path.mnt = NULL;
0224     file->f_inode = NULL;
0225     file_free(file);
0226     dput(dentry);
0227     mntput(mnt);
0228 }
0229 
0230 static LLIST_HEAD(delayed_fput_list);
0231 static void delayed_fput(struct work_struct *unused)
0232 {
0233     struct llist_node *node = llist_del_all(&delayed_fput_list);
0234     struct llist_node *next;
0235 
0236     for (; node; node = next) {
0237         next = llist_next(node);
0238         __fput(llist_entry(node, struct file, f_u.fu_llist));
0239     }
0240 }
0241 
0242 static void ____fput(struct callback_head *work)
0243 {
0244     __fput(container_of(work, struct file, f_u.fu_rcuhead));
0245 }
0246 
0247 /*
0248  * If kernel thread really needs to have the final fput() it has done
0249  * to complete, call this.  The only user right now is the boot - we
0250  * *do* need to make sure our writes to binaries on initramfs has
0251  * not left us with opened struct file waiting for __fput() - execve()
0252  * won't work without that.  Please, don't add more callers without
0253  * very good reasons; in particular, never call that with locks
0254  * held and never call that from a thread that might need to do
0255  * some work on any kind of umount.
0256  */
0257 void flush_delayed_fput(void)
0258 {
0259     delayed_fput(NULL);
0260 }
0261 
0262 static DECLARE_DELAYED_WORK(delayed_fput_work, delayed_fput);
0263 
0264 void fput(struct file *file)
0265 {
0266     if (atomic_long_dec_and_test(&file->f_count)) {
0267         struct task_struct *task = current;
0268 
0269         if (likely(!in_interrupt() && !(task->flags & PF_KTHREAD))) {
0270             init_task_work(&file->f_u.fu_rcuhead, ____fput);
0271             if (!task_work_add(task, &file->f_u.fu_rcuhead, true))
0272                 return;
0273             /*
0274              * After this task has run exit_task_work(),
0275              * task_work_add() will fail.  Fall through to delayed
0276              * fput to avoid leaking *file.
0277              */
0278         }
0279 
0280         if (llist_add(&file->f_u.fu_llist, &delayed_fput_list))
0281             schedule_delayed_work(&delayed_fput_work, 1);
0282     }
0283 }
0284 
0285 /*
0286  * synchronous analog of fput(); for kernel threads that might be needed
0287  * in some umount() (and thus can't use flush_delayed_fput() without
0288  * risking deadlocks), need to wait for completion of __fput() and know
0289  * for this specific struct file it won't involve anything that would
0290  * need them.  Use only if you really need it - at the very least,
0291  * don't blindly convert fput() by kernel thread to that.
0292  */
0293 void __fput_sync(struct file *file)
0294 {
0295     if (atomic_long_dec_and_test(&file->f_count)) {
0296         struct task_struct *task = current;
0297         BUG_ON(!(task->flags & PF_KTHREAD));
0298         __fput(file);
0299     }
0300 }
0301 
0302 EXPORT_SYMBOL(fput);
0303 
0304 void put_filp(struct file *file)
0305 {
0306     if (atomic_long_dec_and_test(&file->f_count)) {
0307         security_file_free(file);
0308         file_free(file);
0309     }
0310 }
0311 
0312 void __init files_init(void)
0313 { 
0314     filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
0315             SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
0316     percpu_counter_init(&nr_files, 0, GFP_KERNEL);
0317 }
0318 
0319 /*
0320  * One file with associated inode and dcache is very roughly 1K. Per default
0321  * do not use more than 10% of our memory for files.
0322  */
0323 void __init files_maxfiles_init(void)
0324 {
0325     unsigned long n;
0326     unsigned long memreserve = (totalram_pages - nr_free_pages()) * 3/2;
0327 
0328     memreserve = min(memreserve, totalram_pages - 1);
0329     n = ((totalram_pages - memreserve) * (PAGE_SIZE / 1024)) / 10;
0330 
0331     files_stat.max_files = max_t(unsigned long, n, NR_FILE);
0332 }