file_table.c 5.9 KB

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