file_table.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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/config.h>
  8. #include <linux/string.h>
  9. #include <linux/slab.h>
  10. #include <linux/file.h>
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/smp_lock.h>
  14. #include <linux/fs.h>
  15. #include <linux/security.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 <asm/atomic.h>
  25. /* sysctl tunables... */
  26. struct files_stat_struct files_stat = {
  27. .max_files = NR_FILE
  28. };
  29. /* public. Not pretty! */
  30. __cacheline_aligned_in_smp DEFINE_SPINLOCK(files_lock);
  31. static struct percpu_counter nr_files __cacheline_aligned_in_smp;
  32. static inline void file_free_rcu(struct rcu_head *head)
  33. {
  34. struct file *f = container_of(head, struct file, f_u.fu_rcuhead);
  35. kmem_cache_free(filp_cachep, f);
  36. }
  37. static inline void file_free(struct file *f)
  38. {
  39. percpu_counter_dec(&nr_files);
  40. call_rcu(&f->f_u.fu_rcuhead, file_free_rcu);
  41. }
  42. /*
  43. * Return the total number of open files in the system
  44. */
  45. static int get_nr_files(void)
  46. {
  47. return percpu_counter_read_positive(&nr_files);
  48. }
  49. /*
  50. * Return the maximum number of open files in the system
  51. */
  52. int get_max_files(void)
  53. {
  54. return files_stat.max_files;
  55. }
  56. EXPORT_SYMBOL_GPL(get_max_files);
  57. /*
  58. * Handle nr_files sysctl
  59. */
  60. #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
  61. int proc_nr_files(ctl_table *table, int write, struct file *filp,
  62. void __user *buffer, size_t *lenp, loff_t *ppos)
  63. {
  64. files_stat.nr_files = get_nr_files();
  65. return proc_dointvec(table, write, filp, buffer, lenp, ppos);
  66. }
  67. #else
  68. int proc_nr_files(ctl_table *table, int write, struct file *filp,
  69. void __user *buffer, size_t *lenp, loff_t *ppos)
  70. {
  71. return -ENOSYS;
  72. }
  73. #endif
  74. /* Find an unused file structure and return a pointer to it.
  75. * Returns NULL, if there are no more free file structures or
  76. * we run out of memory.
  77. */
  78. struct file *get_empty_filp(void)
  79. {
  80. static int old_max;
  81. struct file * f;
  82. /*
  83. * Privileged users can go above max_files
  84. */
  85. if (get_nr_files() >= files_stat.max_files && !capable(CAP_SYS_ADMIN)) {
  86. /*
  87. * percpu_counters are inaccurate. Do an expensive check before
  88. * we go and fail.
  89. */
  90. if (percpu_counter_sum(&nr_files) >= files_stat.max_files)
  91. goto over;
  92. }
  93. f = kmem_cache_alloc(filp_cachep, GFP_KERNEL);
  94. if (f == NULL)
  95. goto fail;
  96. percpu_counter_inc(&nr_files);
  97. memset(f, 0, sizeof(*f));
  98. if (security_file_alloc(f))
  99. goto fail_sec;
  100. eventpoll_init_file(f);
  101. atomic_set(&f->f_count, 1);
  102. f->f_uid = current->fsuid;
  103. f->f_gid = current->fsgid;
  104. rwlock_init(&f->f_owner.lock);
  105. /* f->f_version: 0 */
  106. INIT_LIST_HEAD(&f->f_u.fu_list);
  107. return f;
  108. over:
  109. /* Ran out of filps - report that */
  110. if (get_nr_files() > old_max) {
  111. printk(KERN_INFO "VFS: file-max limit %d reached\n",
  112. get_max_files());
  113. old_max = get_nr_files();
  114. }
  115. goto fail;
  116. fail_sec:
  117. file_free(f);
  118. fail:
  119. return NULL;
  120. }
  121. EXPORT_SYMBOL(get_empty_filp);
  122. void fastcall fput(struct file *file)
  123. {
  124. if (atomic_dec_and_test(&file->f_count))
  125. __fput(file);
  126. }
  127. EXPORT_SYMBOL(fput);
  128. /* __fput is called from task context when aio completion releases the last
  129. * last use of a struct file *. Do not use otherwise.
  130. */
  131. void fastcall __fput(struct file *file)
  132. {
  133. struct dentry *dentry = file->f_dentry;
  134. struct vfsmount *mnt = file->f_vfsmnt;
  135. struct inode *inode = dentry->d_inode;
  136. might_sleep();
  137. fsnotify_close(file);
  138. /*
  139. * The function eventpoll_release() should be the first called
  140. * in the file cleanup chain.
  141. */
  142. eventpoll_release(file);
  143. locks_remove_flock(file);
  144. if (file->f_op && file->f_op->release)
  145. file->f_op->release(inode, file);
  146. security_file_free(file);
  147. if (unlikely(inode->i_cdev != NULL))
  148. cdev_put(inode->i_cdev);
  149. fops_put(file->f_op);
  150. if (file->f_mode & FMODE_WRITE)
  151. put_write_access(inode);
  152. file_kill(file);
  153. file->f_dentry = NULL;
  154. file->f_vfsmnt = NULL;
  155. file_free(file);
  156. dput(dentry);
  157. mntput(mnt);
  158. }
  159. struct file fastcall *fget(unsigned int fd)
  160. {
  161. struct file *file;
  162. struct files_struct *files = current->files;
  163. rcu_read_lock();
  164. file = fcheck_files(files, fd);
  165. if (file) {
  166. if (!atomic_inc_not_zero(&file->f_count)) {
  167. /* File object ref couldn't be taken */
  168. rcu_read_unlock();
  169. return NULL;
  170. }
  171. }
  172. rcu_read_unlock();
  173. return file;
  174. }
  175. EXPORT_SYMBOL(fget);
  176. /*
  177. * Lightweight file lookup - no refcnt increment if fd table isn't shared.
  178. * You can use this only if it is guranteed that the current task already
  179. * holds a refcnt to that file. That check has to be done at fget() only
  180. * and a flag is returned to be passed to the corresponding fput_light().
  181. * There must not be a cloning between an fget_light/fput_light pair.
  182. */
  183. struct file fastcall *fget_light(unsigned int fd, int *fput_needed)
  184. {
  185. struct file *file;
  186. struct files_struct *files = current->files;
  187. *fput_needed = 0;
  188. if (likely((atomic_read(&files->count) == 1))) {
  189. file = fcheck_files(files, fd);
  190. } else {
  191. rcu_read_lock();
  192. file = fcheck_files(files, fd);
  193. if (file) {
  194. if (atomic_inc_not_zero(&file->f_count))
  195. *fput_needed = 1;
  196. else
  197. /* Didn't get the reference, someone's freed */
  198. file = NULL;
  199. }
  200. rcu_read_unlock();
  201. }
  202. return file;
  203. }
  204. void put_filp(struct file *file)
  205. {
  206. if (atomic_dec_and_test(&file->f_count)) {
  207. security_file_free(file);
  208. file_kill(file);
  209. file_free(file);
  210. }
  211. }
  212. void file_move(struct file *file, struct list_head *list)
  213. {
  214. if (!list)
  215. return;
  216. file_list_lock();
  217. list_move(&file->f_u.fu_list, list);
  218. file_list_unlock();
  219. }
  220. void file_kill(struct file *file)
  221. {
  222. if (!list_empty(&file->f_u.fu_list)) {
  223. file_list_lock();
  224. list_del_init(&file->f_u.fu_list);
  225. file_list_unlock();
  226. }
  227. }
  228. int fs_may_remount_ro(struct super_block *sb)
  229. {
  230. struct list_head *p;
  231. /* Check that no files are currently opened for writing. */
  232. file_list_lock();
  233. list_for_each(p, &sb->s_files) {
  234. struct file *file = list_entry(p, struct file, f_u.fu_list);
  235. struct inode *inode = file->f_dentry->d_inode;
  236. /* File with pending delete? */
  237. if (inode->i_nlink == 0)
  238. goto too_bad;
  239. /* Writeable file? */
  240. if (S_ISREG(inode->i_mode) && (file->f_mode & FMODE_WRITE))
  241. goto too_bad;
  242. }
  243. file_list_unlock();
  244. return 1; /* Tis' cool bro. */
  245. too_bad:
  246. file_list_unlock();
  247. return 0;
  248. }
  249. void __init files_init(unsigned long mempages)
  250. {
  251. int n;
  252. /* One file with associated inode and dcache is very roughly 1K.
  253. * Per default don't use more than 10% of our memory for files.
  254. */
  255. n = (mempages * (PAGE_SIZE / 1024)) / 10;
  256. files_stat.max_files = n;
  257. if (files_stat.max_files < NR_FILE)
  258. files_stat.max_files = NR_FILE;
  259. files_defer_init();
  260. percpu_counter_init(&nr_files);
  261. }