file_table.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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/lglock.h>
  23. #include <linux/percpu_counter.h>
  24. #include <linux/percpu.h>
  25. #include <linux/ima.h>
  26. #include <linux/atomic.h>
  27. #include "internal.h"
  28. /* sysctl tunables... */
  29. struct files_stat_struct files_stat = {
  30. .max_files = NR_FILE
  31. };
  32. /* SLAB cache for file structures */
  33. static struct kmem_cache *filp_cachep __read_mostly;
  34. static struct percpu_counter nr_files __cacheline_aligned_in_smp;
  35. static inline void file_free_rcu(struct rcu_head *head)
  36. {
  37. struct file *f = container_of(head, struct file, f_u.fu_rcuhead);
  38. put_cred(f->f_cred);
  39. kmem_cache_free(filp_cachep, f);
  40. }
  41. static inline void file_free(struct file *f)
  42. {
  43. percpu_counter_dec(&nr_files);
  44. file_check_state(f);
  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(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(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 NULL, if there are no more free file structures or
  81. * we run out of memory.
  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. /*
  95. * Privileged users can go above max_files
  96. */
  97. if (get_nr_files() >= files_stat.max_files && !capable(CAP_SYS_ADMIN)) {
  98. /*
  99. * percpu_counters are inaccurate. Do an expensive check before
  100. * we go and fail.
  101. */
  102. if (percpu_counter_sum_positive(&nr_files) >= files_stat.max_files)
  103. goto over;
  104. }
  105. f = kmem_cache_zalloc(filp_cachep, GFP_KERNEL);
  106. if (f == NULL)
  107. goto fail;
  108. percpu_counter_inc(&nr_files);
  109. f->f_cred = get_cred(cred);
  110. if (security_file_alloc(f))
  111. goto fail_sec;
  112. atomic_long_set(&f->f_count, 1);
  113. rwlock_init(&f->f_owner.lock);
  114. spin_lock_init(&f->f_lock);
  115. eventpoll_init_file(f);
  116. /* f->f_version: 0 */
  117. return f;
  118. over:
  119. /* Ran out of filps - report that */
  120. if (get_nr_files() > old_max) {
  121. pr_info("VFS: file-max limit %lu reached\n", get_max_files());
  122. old_max = get_nr_files();
  123. }
  124. goto fail;
  125. fail_sec:
  126. file_free(f);
  127. fail:
  128. return NULL;
  129. }
  130. /**
  131. * alloc_file - allocate and initialize a 'struct file'
  132. * @mnt: the vfsmount on which the file will reside
  133. * @dentry: the dentry representing the new file
  134. * @mode: the mode with which the new file will be opened
  135. * @fop: the 'struct file_operations' for the new file
  136. *
  137. * Use this instead of get_empty_filp() to get a new
  138. * 'struct file'. Do so because of the same initialization
  139. * pitfalls reasons listed for init_file(). This is a
  140. * preferred interface to using init_file().
  141. *
  142. * If all the callers of init_file() are eliminated, its
  143. * code should be moved into this function.
  144. */
  145. struct file *alloc_file(struct path *path, fmode_t mode,
  146. const struct file_operations *fop)
  147. {
  148. struct file *file;
  149. file = get_empty_filp();
  150. if (!file)
  151. return NULL;
  152. file->f_path = *path;
  153. file->f_mapping = path->dentry->d_inode->i_mapping;
  154. file->f_mode = mode;
  155. file->f_op = fop;
  156. /*
  157. * These mounts don't really matter in practice
  158. * for r/o bind mounts. They aren't userspace-
  159. * visible. We do this for consistency, and so
  160. * that we can do debugging checks at __fput()
  161. */
  162. if ((mode & FMODE_WRITE) && !special_file(path->dentry->d_inode->i_mode)) {
  163. file_take_write(file);
  164. WARN_ON(mnt_clone_write(path->mnt));
  165. }
  166. if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  167. i_readcount_inc(path->dentry->d_inode);
  168. return file;
  169. }
  170. EXPORT_SYMBOL(alloc_file);
  171. /**
  172. * drop_file_write_access - give up ability to write to a file
  173. * @file: the file to which we will stop writing
  174. *
  175. * This is a central place which will give up the ability
  176. * to write to @file, along with access to write through
  177. * its vfsmount.
  178. */
  179. static void drop_file_write_access(struct file *file)
  180. {
  181. struct vfsmount *mnt = file->f_path.mnt;
  182. struct dentry *dentry = file->f_path.dentry;
  183. struct inode *inode = dentry->d_inode;
  184. put_write_access(inode);
  185. if (special_file(inode->i_mode))
  186. return;
  187. if (file_check_writeable(file) != 0)
  188. return;
  189. mnt_drop_write(mnt);
  190. file_release_write(file);
  191. }
  192. /* the real guts of fput() - releasing the last reference to file
  193. */
  194. static void __fput(struct file *file)
  195. {
  196. struct dentry *dentry = file->f_path.dentry;
  197. struct vfsmount *mnt = file->f_path.mnt;
  198. struct inode *inode = dentry->d_inode;
  199. might_sleep();
  200. fsnotify_close(file);
  201. /*
  202. * The function eventpoll_release() should be the first called
  203. * in the file cleanup chain.
  204. */
  205. eventpoll_release(file);
  206. locks_remove_file(file);
  207. if (unlikely(file->f_flags & FASYNC)) {
  208. if (file->f_op && file->f_op->fasync)
  209. file->f_op->fasync(-1, file, 0);
  210. }
  211. if (file->f_op && file->f_op->release)
  212. file->f_op->release(inode, file);
  213. security_file_free(file);
  214. ima_file_free(file);
  215. if (unlikely(S_ISCHR(inode->i_mode) && inode->i_cdev != NULL &&
  216. !(file->f_mode & FMODE_PATH))) {
  217. cdev_put(inode->i_cdev);
  218. }
  219. fops_put(file->f_op);
  220. put_pid(file->f_owner.pid);
  221. if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  222. i_readcount_dec(inode);
  223. if (file->f_mode & FMODE_WRITE)
  224. drop_file_write_access(file);
  225. file->f_path.dentry = NULL;
  226. file->f_path.mnt = NULL;
  227. file_free(file);
  228. dput(dentry);
  229. mntput(mnt);
  230. }
  231. void fput(struct file *file)
  232. {
  233. if (atomic_long_dec_and_test(&file->f_count))
  234. __fput(file);
  235. }
  236. EXPORT_SYMBOL(fput);
  237. void put_filp(struct file *file)
  238. {
  239. if (atomic_long_dec_and_test(&file->f_count)) {
  240. security_file_free(file);
  241. file_free(file);
  242. }
  243. }
  244. void __init files_init(unsigned long mempages)
  245. {
  246. unsigned long n;
  247. filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
  248. SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  249. /*
  250. * One file with associated inode and dcache is very roughly 1K.
  251. * Per default don't use more than 10% of our memory for files.
  252. */
  253. n = (mempages * (PAGE_SIZE / 1024)) / 10;
  254. files_stat.max_files = max_t(unsigned long, n, NR_FILE);
  255. files_defer_init();
  256. percpu_counter_init(&nr_files, 0);
  257. }