file_table.c 6.8 KB

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