file_table.c 8.6 KB

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