file_table.c 10 KB

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