file_table.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. #include <linux/fsnotify.h>
  19. /* sysctl tunables... */
  20. struct files_stat_struct files_stat = {
  21. .max_files = NR_FILE
  22. };
  23. EXPORT_SYMBOL(files_stat); /* Needed by unix.o */
  24. /* public. Not pretty! */
  25. __cacheline_aligned_in_smp DEFINE_SPINLOCK(files_lock);
  26. static DEFINE_SPINLOCK(filp_count_lock);
  27. /* slab constructors and destructors are called from arbitrary
  28. * context and must be fully threaded - use a local spinlock
  29. * to protect files_stat.nr_files
  30. */
  31. void filp_ctor(void * objp, struct kmem_cache_s *cachep, unsigned long cflags)
  32. {
  33. if ((cflags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  34. SLAB_CTOR_CONSTRUCTOR) {
  35. unsigned long flags;
  36. spin_lock_irqsave(&filp_count_lock, flags);
  37. files_stat.nr_files++;
  38. spin_unlock_irqrestore(&filp_count_lock, flags);
  39. }
  40. }
  41. void filp_dtor(void * objp, struct kmem_cache_s *cachep, unsigned long dflags)
  42. {
  43. unsigned long flags;
  44. spin_lock_irqsave(&filp_count_lock, flags);
  45. files_stat.nr_files--;
  46. spin_unlock_irqrestore(&filp_count_lock, flags);
  47. }
  48. static inline void file_free(struct file *f)
  49. {
  50. kmem_cache_free(filp_cachep, f);
  51. }
  52. /* Find an unused file structure and return a pointer to it.
  53. * Returns NULL, if there are no more free file structures or
  54. * we run out of memory.
  55. */
  56. struct file *get_empty_filp(void)
  57. {
  58. static int old_max;
  59. struct file * f;
  60. /*
  61. * Privileged users can go above max_files
  62. */
  63. if (files_stat.nr_files >= files_stat.max_files &&
  64. !capable(CAP_SYS_ADMIN))
  65. goto over;
  66. f = kmem_cache_alloc(filp_cachep, GFP_KERNEL);
  67. if (f == NULL)
  68. goto fail;
  69. memset(f, 0, sizeof(*f));
  70. if (security_file_alloc(f))
  71. goto fail_sec;
  72. eventpoll_init_file(f);
  73. atomic_set(&f->f_count, 1);
  74. f->f_uid = current->fsuid;
  75. f->f_gid = current->fsgid;
  76. rwlock_init(&f->f_owner.lock);
  77. /* f->f_version: 0 */
  78. INIT_LIST_HEAD(&f->f_list);
  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. fsnotify_close(file);
  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. }