file_table.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * linux/fs/file_table.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
  6. */
  7. #include <linux/string.h>
  8. #include <linux/slab.h>
  9. #include <linux/file.h>
  10. #include <linux/fdtable.h>
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/fs.h>
  14. #include <linux/security.h>
  15. #include <linux/eventpoll.h>
  16. #include <linux/rcupdate.h>
  17. #include <linux/mount.h>
  18. #include <linux/capability.h>
  19. #include <linux/cdev.h>
  20. #include <linux/fsnotify.h>
  21. #include <linux/sysctl.h>
  22. #include <linux/percpu_counter.h>
  23. #include <linux/percpu.h>
  24. #include <linux/hardirq.h>
  25. #include <linux/task_work.h>
  26. #include <linux/ima.h>
  27. #include <linux/atomic.h>
  28. #include "internal.h"
  29. /* sysctl tunables... */
  30. struct files_stat_struct files_stat = {
  31. .max_files = NR_FILE
  32. };
  33. /* SLAB cache for file structures */
  34. static struct kmem_cache *filp_cachep __read_mostly;
  35. static struct percpu_counter nr_files __cacheline_aligned_in_smp;
  36. static void file_free_rcu(struct rcu_head *head)
  37. {
  38. struct file *f = container_of(head, struct file, f_u.fu_rcuhead);
  39. put_cred(f->f_cred);
  40. kmem_cache_free(filp_cachep, f);
  41. }
  42. static inline void file_free(struct file *f)
  43. {
  44. percpu_counter_dec(&nr_files);
  45. call_rcu(&f->f_u.fu_rcuhead, file_free_rcu);
  46. }
  47. /*
  48. * Return the total number of open files in the system
  49. */
  50. static long get_nr_files(void)
  51. {
  52. return percpu_counter_read_positive(&nr_files);
  53. }
  54. /*
  55. * Return the maximum number of open files in the system
  56. */
  57. unsigned long get_max_files(void)
  58. {
  59. return files_stat.max_files;
  60. }
  61. EXPORT_SYMBOL_GPL(get_max_files);
  62. /*
  63. * Handle nr_files sysctl
  64. */
  65. #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
  66. int proc_nr_files(struct ctl_table *table, int write,
  67. void __user *buffer, size_t *lenp, loff_t *ppos)
  68. {
  69. files_stat.nr_files = get_nr_files();
  70. return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
  71. }
  72. #else
  73. int proc_nr_files(struct ctl_table *table, int write,
  74. void __user *buffer, size_t *lenp, loff_t *ppos)
  75. {
  76. return -ENOSYS;
  77. }
  78. #endif
  79. /* Find an unused file structure and return a pointer to it.
  80. * Returns an error pointer if some error happend e.g. we over file
  81. * structures limit, run out of memory or operation is not permitted.
  82. *
  83. * Be very careful using this. You are responsible for
  84. * getting write access to any mount that you might assign
  85. * to this filp, if it is opened for write. If this is not
  86. * done, you will imbalance int the mount's writer count
  87. * and a warning at __fput() time.
  88. */
  89. struct file *get_empty_filp(void)
  90. {
  91. const struct cred *cred = current_cred();
  92. static long old_max;
  93. struct file *f;
  94. int error;
  95. /*
  96. * Privileged users can go above max_files
  97. */
  98. if (get_nr_files() >= files_stat.max_files && !capable(CAP_SYS_ADMIN)) {
  99. /*
  100. * percpu_counters are inaccurate. Do an expensive check before
  101. * we go and fail.
  102. */
  103. if (percpu_counter_sum_positive(&nr_files) >= files_stat.max_files)
  104. goto over;
  105. }
  106. f = kmem_cache_zalloc(filp_cachep, GFP_KERNEL);
  107. if (unlikely(!f))
  108. return ERR_PTR(-ENOMEM);
  109. percpu_counter_inc(&nr_files);
  110. f->f_cred = get_cred(cred);
  111. error = security_file_alloc(f);
  112. if (unlikely(error)) {
  113. file_free(f);
  114. return ERR_PTR(error);
  115. }
  116. atomic_long_set(&f->f_count, 1);
  117. rwlock_init(&f->f_owner.lock);
  118. spin_lock_init(&f->f_lock);
  119. mutex_init(&f->f_pos_lock);
  120. eventpoll_init_file(f);
  121. /* f->f_version: 0 */
  122. return f;
  123. over:
  124. /* Ran out of filps - report that */
  125. if (get_nr_files() > old_max) {
  126. pr_info("VFS: file-max limit %lu reached\n", get_max_files());
  127. old_max = get_nr_files();
  128. }
  129. return ERR_PTR(-ENFILE);
  130. }
  131. /**
  132. * alloc_file - allocate and initialize a 'struct file'
  133. *
  134. * @path: the (dentry, vfsmount) pair for the new file
  135. * @mode: the mode with which the new file will be opened
  136. * @fop: the 'struct file_operations' for the new file
  137. */
  138. struct file *alloc_file(struct path *path, fmode_t mode,
  139. const struct file_operations *fop)
  140. {
  141. struct file *file;
  142. file = get_empty_filp();
  143. if (IS_ERR(file))
  144. return file;
  145. file->f_path = *path;
  146. file->f_inode = path->dentry->d_inode;
  147. file->f_mapping = path->dentry->d_inode->i_mapping;
  148. if ((mode & FMODE_READ) &&
  149. likely(fop->read || fop->read_iter))
  150. mode |= FMODE_CAN_READ;
  151. if ((mode & FMODE_WRITE) &&
  152. likely(fop->write || fop->write_iter))
  153. mode |= FMODE_CAN_WRITE;
  154. file->f_mode = mode;
  155. file->f_op = fop;
  156. if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  157. i_readcount_inc(path->dentry->d_inode);
  158. return file;
  159. }
  160. EXPORT_SYMBOL(alloc_file);
  161. /* the real guts of fput() - releasing the last reference to file
  162. */
  163. static void __fput(struct file *file)
  164. {
  165. struct dentry *dentry = file->f_path.dentry;
  166. struct vfsmount *mnt = file->f_path.mnt;
  167. struct inode *inode = file->f_inode;
  168. might_sleep();
  169. fsnotify_close(file);
  170. /*
  171. * The function eventpoll_release() should be the first called
  172. * in the file cleanup chain.
  173. */
  174. eventpoll_release(file);
  175. locks_remove_file(file);
  176. if (unlikely(file->f_flags & FASYNC)) {
  177. if (file->f_op->fasync)
  178. file->f_op->fasync(-1, file, 0);
  179. }
  180. ima_file_free(file);
  181. if (file->f_op->release)
  182. file->f_op->release(inode, file);
  183. security_file_free(file);
  184. if (unlikely(S_ISCHR(inode->i_mode) && inode->i_cdev != NULL &&
  185. !(file->f_mode & FMODE_PATH))) {
  186. cdev_put(inode->i_cdev);
  187. }
  188. fops_put(file->f_op);
  189. put_pid(file->f_owner.pid);
  190. if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  191. i_readcount_dec(inode);
  192. if (file->f_mode & FMODE_WRITER) {
  193. put_write_access(inode);
  194. __mnt_drop_write(mnt);
  195. }
  196. file->f_path.dentry = NULL;
  197. file->f_path.mnt = NULL;
  198. file->f_inode = NULL;
  199. file_free(file);
  200. dput(dentry);
  201. mntput(mnt);
  202. }
  203. static LLIST_HEAD(delayed_fput_list);
  204. static void delayed_fput(struct work_struct *unused)
  205. {
  206. struct llist_node *node = llist_del_all(&delayed_fput_list);
  207. struct llist_node *next;
  208. for (; node; node = next) {
  209. next = llist_next(node);
  210. __fput(llist_entry(node, struct file, f_u.fu_llist));
  211. }
  212. }
  213. static void ____fput(struct callback_head *work)
  214. {
  215. __fput(container_of(work, struct file, f_u.fu_rcuhead));
  216. }
  217. /*
  218. * If kernel thread really needs to have the final fput() it has done
  219. * to complete, call this. The only user right now is the boot - we
  220. * *do* need to make sure our writes to binaries on initramfs has
  221. * not left us with opened struct file waiting for __fput() - execve()
  222. * won't work without that. Please, don't add more callers without
  223. * very good reasons; in particular, never call that with locks
  224. * held and never call that from a thread that might need to do
  225. * some work on any kind of umount.
  226. */
  227. void flush_delayed_fput(void)
  228. {
  229. delayed_fput(NULL);
  230. }
  231. static DECLARE_DELAYED_WORK(delayed_fput_work, delayed_fput);
  232. void fput(struct file *file)
  233. {
  234. if (atomic_long_dec_and_test(&file->f_count)) {
  235. struct task_struct *task = current;
  236. if (likely(!in_interrupt() && !(task->flags & PF_KTHREAD))) {
  237. init_task_work(&file->f_u.fu_rcuhead, ____fput);
  238. if (!task_work_add(task, &file->f_u.fu_rcuhead, true))
  239. return;
  240. /*
  241. * After this task has run exit_task_work(),
  242. * task_work_add() will fail. Fall through to delayed
  243. * fput to avoid leaking *file.
  244. */
  245. }
  246. if (llist_add(&file->f_u.fu_llist, &delayed_fput_list))
  247. schedule_delayed_work(&delayed_fput_work, 1);
  248. }
  249. }
  250. /*
  251. * synchronous analog of fput(); for kernel threads that might be needed
  252. * in some umount() (and thus can't use flush_delayed_fput() without
  253. * risking deadlocks), need to wait for completion of __fput() and know
  254. * for this specific struct file it won't involve anything that would
  255. * need them. Use only if you really need it - at the very least,
  256. * don't blindly convert fput() by kernel thread to that.
  257. */
  258. void __fput_sync(struct file *file)
  259. {
  260. if (atomic_long_dec_and_test(&file->f_count)) {
  261. struct task_struct *task = current;
  262. BUG_ON(!(task->flags & PF_KTHREAD));
  263. __fput(file);
  264. }
  265. }
  266. EXPORT_SYMBOL(fput);
  267. void put_filp(struct file *file)
  268. {
  269. if (atomic_long_dec_and_test(&file->f_count)) {
  270. security_file_free(file);
  271. file_free(file);
  272. }
  273. }
  274. void __init files_init(unsigned long mempages)
  275. {
  276. unsigned long n;
  277. filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
  278. SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  279. /*
  280. * One file with associated inode and dcache is very roughly 1K.
  281. * Per default don't use more than 10% of our memory for files.
  282. */
  283. n = (mempages * (PAGE_SIZE / 1024)) / 10;
  284. files_stat.max_files = max_t(unsigned long, n, NR_FILE);
  285. percpu_counter_init(&nr_files, 0, GFP_KERNEL);
  286. }