file_table.c 10 KB

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