file_table.c 5.9 KB

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