file_table.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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/fs.h>
  13. #include <linux/security.h>
  14. #include <linux/eventpoll.h>
  15. #include <linux/rcupdate.h>
  16. #include <linux/mount.h>
  17. #include <linux/capability.h>
  18. #include <linux/cdev.h>
  19. #include <linux/fsnotify.h>
  20. #include <linux/sysctl.h>
  21. #include <linux/percpu_counter.h>
  22. #include <asm/atomic.h>
  23. /* sysctl tunables... */
  24. struct files_stat_struct files_stat = {
  25. .max_files = NR_FILE
  26. };
  27. /* public. Not pretty! */
  28. __cacheline_aligned_in_smp DEFINE_SPINLOCK(files_lock);
  29. static struct percpu_counter nr_files __cacheline_aligned_in_smp;
  30. static inline void file_free_rcu(struct rcu_head *head)
  31. {
  32. struct file *f = container_of(head, struct file, f_u.fu_rcuhead);
  33. kmem_cache_free(filp_cachep, f);
  34. }
  35. static inline void file_free(struct file *f)
  36. {
  37. percpu_counter_dec(&nr_files);
  38. call_rcu(&f->f_u.fu_rcuhead, file_free_rcu);
  39. }
  40. /*
  41. * Return the total number of open files in the system
  42. */
  43. static int get_nr_files(void)
  44. {
  45. return percpu_counter_read_positive(&nr_files);
  46. }
  47. /*
  48. * Return the maximum number of open files in the system
  49. */
  50. int get_max_files(void)
  51. {
  52. return files_stat.max_files;
  53. }
  54. EXPORT_SYMBOL_GPL(get_max_files);
  55. /*
  56. * Handle nr_files sysctl
  57. */
  58. #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
  59. int proc_nr_files(ctl_table *table, int write, struct file *filp,
  60. void __user *buffer, size_t *lenp, loff_t *ppos)
  61. {
  62. files_stat.nr_files = get_nr_files();
  63. return proc_dointvec(table, write, filp, buffer, lenp, ppos);
  64. }
  65. #else
  66. int proc_nr_files(ctl_table *table, int write, struct file *filp,
  67. void __user *buffer, size_t *lenp, loff_t *ppos)
  68. {
  69. return -ENOSYS;
  70. }
  71. #endif
  72. /* Find an unused file structure and return a pointer to it.
  73. * Returns NULL, if there are no more free file structures or
  74. * we run out of memory.
  75. *
  76. * Be very careful using this. You are responsible for
  77. * getting write access to any mount that you might assign
  78. * to this filp, if it is opened for write. If this is not
  79. * done, you will imbalance int the mount's writer count
  80. * and a warning at __fput() time.
  81. */
  82. struct file *get_empty_filp(void)
  83. {
  84. struct task_struct *tsk;
  85. static int old_max;
  86. struct file * f;
  87. /*
  88. * Privileged users can go above max_files
  89. */
  90. if (get_nr_files() >= files_stat.max_files && !capable(CAP_SYS_ADMIN)) {
  91. /*
  92. * percpu_counters are inaccurate. Do an expensive check before
  93. * we go and fail.
  94. */
  95. if (percpu_counter_sum_positive(&nr_files) >= files_stat.max_files)
  96. goto over;
  97. }
  98. f = kmem_cache_zalloc(filp_cachep, GFP_KERNEL);
  99. if (f == NULL)
  100. goto fail;
  101. percpu_counter_inc(&nr_files);
  102. if (security_file_alloc(f))
  103. goto fail_sec;
  104. tsk = current;
  105. INIT_LIST_HEAD(&f->f_u.fu_list);
  106. atomic_set(&f->f_count, 1);
  107. rwlock_init(&f->f_owner.lock);
  108. f->f_uid = tsk->fsuid;
  109. f->f_gid = tsk->fsgid;
  110. eventpoll_init_file(f);
  111. /* f->f_version: 0 */
  112. return f;
  113. over:
  114. /* Ran out of filps - report that */
  115. if (get_nr_files() > old_max) {
  116. printk(KERN_INFO "VFS: file-max limit %d reached\n",
  117. get_max_files());
  118. old_max = get_nr_files();
  119. }
  120. goto fail;
  121. fail_sec:
  122. file_free(f);
  123. fail:
  124. return NULL;
  125. }
  126. EXPORT_SYMBOL(get_empty_filp);
  127. /**
  128. * alloc_file - allocate and initialize a 'struct file'
  129. * @mnt: the vfsmount on which the file will reside
  130. * @dentry: the dentry representing the new file
  131. * @mode: the mode with which the new file will be opened
  132. * @fop: the 'struct file_operations' for the new file
  133. *
  134. * Use this instead of get_empty_filp() to get a new
  135. * 'struct file'. Do so because of the same initialization
  136. * pitfalls reasons listed for init_file(). This is a
  137. * preferred interface to using init_file().
  138. *
  139. * If all the callers of init_file() are eliminated, its
  140. * code should be moved into this function.
  141. */
  142. struct file *alloc_file(struct vfsmount *mnt, struct dentry *dentry,
  143. mode_t mode, const struct file_operations *fop)
  144. {
  145. struct file *file;
  146. struct path;
  147. file = get_empty_filp();
  148. if (!file)
  149. return NULL;
  150. init_file(file, mnt, dentry, mode, fop);
  151. return file;
  152. }
  153. EXPORT_SYMBOL(alloc_file);
  154. /**
  155. * init_file - initialize a 'struct file'
  156. * @file: the already allocated 'struct file' to initialized
  157. * @mnt: the vfsmount on which the file resides
  158. * @dentry: the dentry representing this file
  159. * @mode: the mode the file is opened with
  160. * @fop: the 'struct file_operations' for this file
  161. *
  162. * Use this instead of setting the members directly. Doing so
  163. * avoids making mistakes like forgetting the mntget() or
  164. * forgetting to take a write on the mnt.
  165. *
  166. * Note: This is a crappy interface. It is here to make
  167. * merging with the existing users of get_empty_filp()
  168. * who have complex failure logic easier. All users
  169. * of this should be moving to alloc_file().
  170. */
  171. int init_file(struct file *file, struct vfsmount *mnt, struct dentry *dentry,
  172. mode_t mode, const struct file_operations *fop)
  173. {
  174. int error = 0;
  175. file->f_path.dentry = dentry;
  176. file->f_path.mnt = mntget(mnt);
  177. file->f_mapping = dentry->d_inode->i_mapping;
  178. file->f_mode = mode;
  179. file->f_op = fop;
  180. return error;
  181. }
  182. EXPORT_SYMBOL(init_file);
  183. void fput(struct file *file)
  184. {
  185. if (atomic_dec_and_test(&file->f_count))
  186. __fput(file);
  187. }
  188. EXPORT_SYMBOL(fput);
  189. /**
  190. * drop_file_write_access - give up ability to write to a file
  191. * @file: the file to which we will stop writing
  192. *
  193. * This is a central place which will give up the ability
  194. * to write to @file, along with access to write through
  195. * its vfsmount.
  196. */
  197. void drop_file_write_access(struct file *file)
  198. {
  199. struct dentry *dentry = file->f_path.dentry;
  200. struct inode *inode = dentry->d_inode;
  201. put_write_access(inode);
  202. }
  203. EXPORT_SYMBOL_GPL(drop_file_write_access);
  204. /* __fput is called from task context when aio completion releases the last
  205. * last use of a struct file *. Do not use otherwise.
  206. */
  207. void __fput(struct file *file)
  208. {
  209. struct dentry *dentry = file->f_path.dentry;
  210. struct vfsmount *mnt = file->f_path.mnt;
  211. struct inode *inode = dentry->d_inode;
  212. might_sleep();
  213. fsnotify_close(file);
  214. /*
  215. * The function eventpoll_release() should be the first called
  216. * in the file cleanup chain.
  217. */
  218. eventpoll_release(file);
  219. locks_remove_flock(file);
  220. if (file->f_op && file->f_op->release)
  221. file->f_op->release(inode, file);
  222. security_file_free(file);
  223. if (unlikely(S_ISCHR(inode->i_mode) && inode->i_cdev != NULL))
  224. cdev_put(inode->i_cdev);
  225. fops_put(file->f_op);
  226. put_pid(file->f_owner.pid);
  227. file_kill(file);
  228. if (file->f_mode & FMODE_WRITE)
  229. drop_file_write_access(file);
  230. file->f_path.dentry = NULL;
  231. file->f_path.mnt = NULL;
  232. file_free(file);
  233. dput(dentry);
  234. mntput(mnt);
  235. }
  236. struct file *fget(unsigned int fd)
  237. {
  238. struct file *file;
  239. struct files_struct *files = current->files;
  240. rcu_read_lock();
  241. file = fcheck_files(files, fd);
  242. if (file) {
  243. if (!atomic_inc_not_zero(&file->f_count)) {
  244. /* File object ref couldn't be taken */
  245. rcu_read_unlock();
  246. return NULL;
  247. }
  248. }
  249. rcu_read_unlock();
  250. return file;
  251. }
  252. EXPORT_SYMBOL(fget);
  253. /*
  254. * Lightweight file lookup - no refcnt increment if fd table isn't shared.
  255. * You can use this only if it is guranteed that the current task already
  256. * holds a refcnt to that file. That check has to be done at fget() only
  257. * and a flag is returned to be passed to the corresponding fput_light().
  258. * There must not be a cloning between an fget_light/fput_light pair.
  259. */
  260. struct file *fget_light(unsigned int fd, int *fput_needed)
  261. {
  262. struct file *file;
  263. struct files_struct *files = current->files;
  264. *fput_needed = 0;
  265. if (likely((atomic_read(&files->count) == 1))) {
  266. file = fcheck_files(files, fd);
  267. } else {
  268. rcu_read_lock();
  269. file = fcheck_files(files, fd);
  270. if (file) {
  271. if (atomic_inc_not_zero(&file->f_count))
  272. *fput_needed = 1;
  273. else
  274. /* Didn't get the reference, someone's freed */
  275. file = NULL;
  276. }
  277. rcu_read_unlock();
  278. }
  279. return file;
  280. }
  281. void put_filp(struct file *file)
  282. {
  283. if (atomic_dec_and_test(&file->f_count)) {
  284. security_file_free(file);
  285. file_kill(file);
  286. file_free(file);
  287. }
  288. }
  289. void file_move(struct file *file, struct list_head *list)
  290. {
  291. if (!list)
  292. return;
  293. file_list_lock();
  294. list_move(&file->f_u.fu_list, list);
  295. file_list_unlock();
  296. }
  297. void file_kill(struct file *file)
  298. {
  299. if (!list_empty(&file->f_u.fu_list)) {
  300. file_list_lock();
  301. list_del_init(&file->f_u.fu_list);
  302. file_list_unlock();
  303. }
  304. }
  305. int fs_may_remount_ro(struct super_block *sb)
  306. {
  307. struct file *file;
  308. /* Check that no files are currently opened for writing. */
  309. file_list_lock();
  310. list_for_each_entry(file, &sb->s_files, f_u.fu_list) {
  311. struct inode *inode = file->f_path.dentry->d_inode;
  312. /* File with pending delete? */
  313. if (inode->i_nlink == 0)
  314. goto too_bad;
  315. /* Writeable file? */
  316. if (S_ISREG(inode->i_mode) && (file->f_mode & FMODE_WRITE))
  317. goto too_bad;
  318. }
  319. file_list_unlock();
  320. return 1; /* Tis' cool bro. */
  321. too_bad:
  322. file_list_unlock();
  323. return 0;
  324. }
  325. void __init files_init(unsigned long mempages)
  326. {
  327. int n;
  328. /* One file with associated inode and dcache is very roughly 1K.
  329. * Per default don't use more than 10% of our memory for files.
  330. */
  331. n = (mempages * (PAGE_SIZE / 1024)) / 10;
  332. files_stat.max_files = n;
  333. if (files_stat.max_files < NR_FILE)
  334. files_stat.max_files = NR_FILE;
  335. files_defer_init();
  336. percpu_counter_init(&nr_files, 0);
  337. }