file_table.c 10.0 KB

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