file_table.c 8.5 KB

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