file_table.c 10 KB

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