file_table.c 9.8 KB

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