file_table.c 6.3 KB

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