file_table.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. goto over;
  65. f = kmem_cache_alloc(filp_cachep, GFP_KERNEL);
  66. if (f == NULL)
  67. goto fail;
  68. memset(f, 0, sizeof(*f));
  69. if (security_file_alloc(f))
  70. goto fail_sec;
  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. over:
  81. /* Ran out of filps - report that */
  82. if (files_stat.nr_files > old_max) {
  83. printk(KERN_INFO "VFS: file-max limit %d reached\n",
  84. files_stat.max_files);
  85. old_max = files_stat.nr_files;
  86. }
  87. goto fail;
  88. fail_sec:
  89. file_free(f);
  90. fail:
  91. return NULL;
  92. }
  93. EXPORT_SYMBOL(get_empty_filp);
  94. void fastcall fput(struct file *file)
  95. {
  96. if (atomic_dec_and_test(&file->f_count))
  97. __fput(file);
  98. }
  99. EXPORT_SYMBOL(fput);
  100. /* __fput is called from task context when aio completion releases the last
  101. * last use of a struct file *. Do not use otherwise.
  102. */
  103. void fastcall __fput(struct file *file)
  104. {
  105. struct dentry *dentry = file->f_dentry;
  106. struct vfsmount *mnt = file->f_vfsmnt;
  107. struct inode *inode = dentry->d_inode;
  108. might_sleep();
  109. /*
  110. * The function eventpoll_release() should be the first called
  111. * in the file cleanup chain.
  112. */
  113. eventpoll_release(file);
  114. locks_remove_flock(file);
  115. if (file->f_op && file->f_op->release)
  116. file->f_op->release(inode, file);
  117. security_file_free(file);
  118. if (unlikely(inode->i_cdev != NULL))
  119. cdev_put(inode->i_cdev);
  120. fops_put(file->f_op);
  121. if (file->f_mode & FMODE_WRITE)
  122. put_write_access(inode);
  123. file_kill(file);
  124. file->f_dentry = NULL;
  125. file->f_vfsmnt = NULL;
  126. file_free(file);
  127. dput(dentry);
  128. mntput(mnt);
  129. }
  130. struct file fastcall *fget(unsigned int fd)
  131. {
  132. struct file *file;
  133. struct files_struct *files = current->files;
  134. spin_lock(&files->file_lock);
  135. file = fcheck_files(files, fd);
  136. if (file)
  137. get_file(file);
  138. spin_unlock(&files->file_lock);
  139. return file;
  140. }
  141. EXPORT_SYMBOL(fget);
  142. /*
  143. * Lightweight file lookup - no refcnt increment if fd table isn't shared.
  144. * You can use this only if it is guranteed that the current task already
  145. * holds a refcnt to that file. That check has to be done at fget() only
  146. * and a flag is returned to be passed to the corresponding fput_light().
  147. * There must not be a cloning between an fget_light/fput_light pair.
  148. */
  149. struct file fastcall *fget_light(unsigned int fd, int *fput_needed)
  150. {
  151. struct file *file;
  152. struct files_struct *files = current->files;
  153. *fput_needed = 0;
  154. if (likely((atomic_read(&files->count) == 1))) {
  155. file = fcheck_files(files, fd);
  156. } else {
  157. spin_lock(&files->file_lock);
  158. file = fcheck_files(files, fd);
  159. if (file) {
  160. get_file(file);
  161. *fput_needed = 1;
  162. }
  163. spin_unlock(&files->file_lock);
  164. }
  165. return file;
  166. }
  167. void put_filp(struct file *file)
  168. {
  169. if (atomic_dec_and_test(&file->f_count)) {
  170. security_file_free(file);
  171. file_kill(file);
  172. file_free(file);
  173. }
  174. }
  175. void file_move(struct file *file, struct list_head *list)
  176. {
  177. if (!list)
  178. return;
  179. file_list_lock();
  180. list_move(&file->f_list, list);
  181. file_list_unlock();
  182. }
  183. void file_kill(struct file *file)
  184. {
  185. if (!list_empty(&file->f_list)) {
  186. file_list_lock();
  187. list_del_init(&file->f_list);
  188. file_list_unlock();
  189. }
  190. }
  191. int fs_may_remount_ro(struct super_block *sb)
  192. {
  193. struct list_head *p;
  194. /* Check that no files are currently opened for writing. */
  195. file_list_lock();
  196. list_for_each(p, &sb->s_files) {
  197. struct file *file = list_entry(p, struct file, f_list);
  198. struct inode *inode = file->f_dentry->d_inode;
  199. /* File with pending delete? */
  200. if (inode->i_nlink == 0)
  201. goto too_bad;
  202. /* Writeable file? */
  203. if (S_ISREG(inode->i_mode) && (file->f_mode & FMODE_WRITE))
  204. goto too_bad;
  205. }
  206. file_list_unlock();
  207. return 1; /* Tis' cool bro. */
  208. too_bad:
  209. file_list_unlock();
  210. return 0;
  211. }
  212. void __init files_init(unsigned long mempages)
  213. {
  214. int n;
  215. /* One file with associated inode and dcache is very roughly 1K.
  216. * Per default don't use more than 10% of our memory for files.
  217. */
  218. n = (mempages * (PAGE_SIZE / 1024)) / 10;
  219. files_stat.max_files = n;
  220. if (files_stat.max_files < NR_FILE)
  221. files_stat.max_files = NR_FILE;
  222. }