file_table.c 6.2 KB

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