file_table.c 6.8 KB

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