file_table.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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/lglock.h>
  23. #include <linux/percpu_counter.h>
  24. #include <linux/percpu.h>
  25. #include <linux/hardirq.h>
  26. #include <linux/task_work.h>
  27. #include <linux/ima.h>
  28. #include <linux/atomic.h>
  29. #include "internal.h"
  30. /* sysctl tunables... */
  31. struct files_stat_struct files_stat = {
  32. .max_files = NR_FILE
  33. };
  34. DEFINE_STATIC_LGLOCK(files_lglock);
  35. /* SLAB cache for file structures */
  36. static struct kmem_cache *filp_cachep __read_mostly;
  37. static struct percpu_counter nr_files __cacheline_aligned_in_smp;
  38. static void file_free_rcu(struct rcu_head *head)
  39. {
  40. struct file *f = container_of(head, struct file, f_u.fu_rcuhead);
  41. put_cred(f->f_cred);
  42. kmem_cache_free(filp_cachep, f);
  43. }
  44. static inline void file_free(struct file *f)
  45. {
  46. percpu_counter_dec(&nr_files);
  47. file_check_state(f);
  48. call_rcu(&f->f_u.fu_rcuhead, file_free_rcu);
  49. }
  50. /*
  51. * Return the total number of open files in the system
  52. */
  53. static long get_nr_files(void)
  54. {
  55. return percpu_counter_read_positive(&nr_files);
  56. }
  57. /*
  58. * Return the maximum number of open files in the system
  59. */
  60. unsigned long get_max_files(void)
  61. {
  62. return files_stat.max_files;
  63. }
  64. EXPORT_SYMBOL_GPL(get_max_files);
  65. /*
  66. * Handle nr_files sysctl
  67. */
  68. #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
  69. int proc_nr_files(ctl_table *table, int write,
  70. void __user *buffer, size_t *lenp, loff_t *ppos)
  71. {
  72. files_stat.nr_files = get_nr_files();
  73. return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
  74. }
  75. #else
  76. int proc_nr_files(ctl_table *table, int write,
  77. void __user *buffer, size_t *lenp, loff_t *ppos)
  78. {
  79. return -ENOSYS;
  80. }
  81. #endif
  82. /* Find an unused file structure and return a pointer to it.
  83. * Returns an error pointer if some error happend e.g. we over file
  84. * structures limit, run out of memory or operation is not permitted.
  85. *
  86. * Be very careful using this. You are responsible for
  87. * getting write access to any mount that you might assign
  88. * to this filp, if it is opened for write. If this is not
  89. * done, you will imbalance int the mount's writer count
  90. * and a warning at __fput() time.
  91. */
  92. struct file *get_empty_filp(void)
  93. {
  94. const struct cred *cred = current_cred();
  95. static long old_max;
  96. struct file *f;
  97. int error;
  98. /*
  99. * Privileged users can go above max_files
  100. */
  101. if (get_nr_files() >= files_stat.max_files && !capable(CAP_SYS_ADMIN)) {
  102. /*
  103. * percpu_counters are inaccurate. Do an expensive check before
  104. * we go and fail.
  105. */
  106. if (percpu_counter_sum_positive(&nr_files) >= files_stat.max_files)
  107. goto over;
  108. }
  109. f = kmem_cache_zalloc(filp_cachep, GFP_KERNEL);
  110. if (unlikely(!f))
  111. return ERR_PTR(-ENOMEM);
  112. percpu_counter_inc(&nr_files);
  113. f->f_cred = get_cred(cred);
  114. error = security_file_alloc(f);
  115. if (unlikely(error)) {
  116. file_free(f);
  117. return ERR_PTR(error);
  118. }
  119. INIT_LIST_HEAD(&f->f_u.fu_list);
  120. atomic_long_set(&f->f_count, 1);
  121. rwlock_init(&f->f_owner.lock);
  122. spin_lock_init(&f->f_lock);
  123. eventpoll_init_file(f);
  124. /* f->f_version: 0 */
  125. return f;
  126. over:
  127. /* Ran out of filps - report that */
  128. if (get_nr_files() > old_max) {
  129. pr_info("VFS: file-max limit %lu reached\n", get_max_files());
  130. old_max = get_nr_files();
  131. }
  132. return ERR_PTR(-ENFILE);
  133. }
  134. /**
  135. * alloc_file - allocate and initialize a 'struct file'
  136. * @mnt: the vfsmount on which the file will reside
  137. * @dentry: the dentry representing the new file
  138. * @mode: the mode with which the new file will be opened
  139. * @fop: the 'struct file_operations' for the new file
  140. *
  141. * Use this instead of get_empty_filp() to get a new
  142. * 'struct file'. Do so because of the same initialization
  143. * pitfalls reasons listed for init_file(). This is a
  144. * preferred interface to using init_file().
  145. *
  146. * If all the callers of init_file() are eliminated, its
  147. * code should be moved into this function.
  148. */
  149. struct file *alloc_file(struct path *path, fmode_t mode,
  150. const struct file_operations *fop)
  151. {
  152. struct file *file;
  153. file = get_empty_filp();
  154. if (IS_ERR(file))
  155. return file;
  156. file->f_path = *path;
  157. file->f_inode = path->dentry->d_inode;
  158. file->f_mapping = path->dentry->d_inode->i_mapping;
  159. file->f_mode = mode;
  160. file->f_op = fop;
  161. /*
  162. * These mounts don't really matter in practice
  163. * for r/o bind mounts. They aren't userspace-
  164. * visible. We do this for consistency, and so
  165. * that we can do debugging checks at __fput()
  166. */
  167. if ((mode & FMODE_WRITE) && !special_file(path->dentry->d_inode->i_mode)) {
  168. file_take_write(file);
  169. WARN_ON(mnt_clone_write(path->mnt));
  170. }
  171. if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  172. i_readcount_inc(path->dentry->d_inode);
  173. return file;
  174. }
  175. EXPORT_SYMBOL(alloc_file);
  176. /**
  177. * drop_file_write_access - give up ability to write to a file
  178. * @file: the file to which we will stop writing
  179. *
  180. * This is a central place which will give up the ability
  181. * to write to @file, along with access to write through
  182. * its vfsmount.
  183. */
  184. static void drop_file_write_access(struct file *file)
  185. {
  186. struct vfsmount *mnt = file->f_path.mnt;
  187. struct dentry *dentry = file->f_path.dentry;
  188. struct inode *inode = dentry->d_inode;
  189. put_write_access(inode);
  190. if (special_file(inode->i_mode))
  191. return;
  192. if (file_check_writeable(file) != 0)
  193. return;
  194. __mnt_drop_write(mnt);
  195. file_release_write(file);
  196. }
  197. /* the real guts of fput() - releasing the last reference to file
  198. */
  199. static void __fput(struct file *file)
  200. {
  201. struct dentry *dentry = file->f_path.dentry;
  202. struct vfsmount *mnt = file->f_path.mnt;
  203. struct inode *inode = dentry->d_inode;
  204. might_sleep();
  205. fsnotify_close(file);
  206. /*
  207. * The function eventpoll_release() should be the first called
  208. * in the file cleanup chain.
  209. */
  210. eventpoll_release(file);
  211. locks_remove_flock(file);
  212. if (unlikely(file->f_flags & FASYNC)) {
  213. if (file->f_op && file->f_op->fasync)
  214. file->f_op->fasync(-1, file, 0);
  215. }
  216. ima_file_free(file);
  217. if (file->f_op && file->f_op->release)
  218. file->f_op->release(inode, file);
  219. security_file_free(file);
  220. if (unlikely(S_ISCHR(inode->i_mode) && inode->i_cdev != NULL &&
  221. !(file->f_mode & FMODE_PATH))) {
  222. cdev_put(inode->i_cdev);
  223. }
  224. fops_put(file->f_op);
  225. put_pid(file->f_owner.pid);
  226. if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  227. i_readcount_dec(inode);
  228. if (file->f_mode & FMODE_WRITE)
  229. drop_file_write_access(file);
  230. file->f_path.dentry = NULL;
  231. file->f_path.mnt = NULL;
  232. file->f_inode = NULL;
  233. file_free(file);
  234. dput(dentry);
  235. mntput(mnt);
  236. }
  237. static DEFINE_SPINLOCK(delayed_fput_lock);
  238. static LIST_HEAD(delayed_fput_list);
  239. static void delayed_fput(struct work_struct *unused)
  240. {
  241. LIST_HEAD(head);
  242. spin_lock_irq(&delayed_fput_lock);
  243. list_splice_init(&delayed_fput_list, &head);
  244. spin_unlock_irq(&delayed_fput_lock);
  245. while (!list_empty(&head)) {
  246. struct file *f = list_first_entry(&head, struct file, f_u.fu_list);
  247. list_del_init(&f->f_u.fu_list);
  248. __fput(f);
  249. }
  250. }
  251. static void ____fput(struct callback_head *work)
  252. {
  253. __fput(container_of(work, struct file, f_u.fu_rcuhead));
  254. }
  255. /*
  256. * If kernel thread really needs to have the final fput() it has done
  257. * to complete, call this. The only user right now is the boot - we
  258. * *do* need to make sure our writes to binaries on initramfs has
  259. * not left us with opened struct file waiting for __fput() - execve()
  260. * won't work without that. Please, don't add more callers without
  261. * very good reasons; in particular, never call that with locks
  262. * held and never call that from a thread that might need to do
  263. * some work on any kind of umount.
  264. */
  265. void flush_delayed_fput(void)
  266. {
  267. delayed_fput(NULL);
  268. }
  269. static DECLARE_WORK(delayed_fput_work, delayed_fput);
  270. void fput(struct file *file)
  271. {
  272. if (atomic_long_dec_and_test(&file->f_count)) {
  273. struct task_struct *task = current;
  274. unsigned long flags;
  275. file_sb_list_del(file);
  276. if (likely(!in_interrupt() && !(task->flags & PF_KTHREAD))) {
  277. init_task_work(&file->f_u.fu_rcuhead, ____fput);
  278. if (!task_work_add(task, &file->f_u.fu_rcuhead, true))
  279. return;
  280. }
  281. spin_lock_irqsave(&delayed_fput_lock, flags);
  282. list_add(&file->f_u.fu_list, &delayed_fput_list);
  283. schedule_work(&delayed_fput_work);
  284. spin_unlock_irqrestore(&delayed_fput_lock, flags);
  285. }
  286. }
  287. /*
  288. * synchronous analog of fput(); for kernel threads that might be needed
  289. * in some umount() (and thus can't use flush_delayed_fput() without
  290. * risking deadlocks), need to wait for completion of __fput() and know
  291. * for this specific struct file it won't involve anything that would
  292. * need them. Use only if you really need it - at the very least,
  293. * don't blindly convert fput() by kernel thread to that.
  294. */
  295. void __fput_sync(struct file *file)
  296. {
  297. if (atomic_long_dec_and_test(&file->f_count)) {
  298. struct task_struct *task = current;
  299. file_sb_list_del(file);
  300. BUG_ON(!(task->flags & PF_KTHREAD));
  301. __fput(file);
  302. }
  303. }
  304. EXPORT_SYMBOL(fput);
  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_sb_list_del(file);
  310. file_free(file);
  311. }
  312. }
  313. static inline int file_list_cpu(struct file *file)
  314. {
  315. #ifdef CONFIG_SMP
  316. return file->f_sb_list_cpu;
  317. #else
  318. return smp_processor_id();
  319. #endif
  320. }
  321. /* helper for file_sb_list_add to reduce ifdefs */
  322. static inline void __file_sb_list_add(struct file *file, struct super_block *sb)
  323. {
  324. struct list_head *list;
  325. #ifdef CONFIG_SMP
  326. int cpu;
  327. cpu = smp_processor_id();
  328. file->f_sb_list_cpu = cpu;
  329. list = per_cpu_ptr(sb->s_files, cpu);
  330. #else
  331. list = &sb->s_files;
  332. #endif
  333. list_add(&file->f_u.fu_list, list);
  334. }
  335. /**
  336. * file_sb_list_add - add a file to the sb's file list
  337. * @file: file to add
  338. * @sb: sb to add it to
  339. *
  340. * Use this function to associate a file with the superblock of the inode it
  341. * refers to.
  342. */
  343. void file_sb_list_add(struct file *file, struct super_block *sb)
  344. {
  345. lg_local_lock(&files_lglock);
  346. __file_sb_list_add(file, sb);
  347. lg_local_unlock(&files_lglock);
  348. }
  349. /**
  350. * file_sb_list_del - remove a file from the sb's file list
  351. * @file: file to remove
  352. * @sb: sb to remove it from
  353. *
  354. * Use this function to remove a file from its superblock.
  355. */
  356. void file_sb_list_del(struct file *file)
  357. {
  358. if (!list_empty(&file->f_u.fu_list)) {
  359. lg_local_lock_cpu(&files_lglock, file_list_cpu(file));
  360. list_del_init(&file->f_u.fu_list);
  361. lg_local_unlock_cpu(&files_lglock, file_list_cpu(file));
  362. }
  363. }
  364. #ifdef CONFIG_SMP
  365. /*
  366. * These macros iterate all files on all CPUs for a given superblock.
  367. * files_lglock must be held globally.
  368. */
  369. #define do_file_list_for_each_entry(__sb, __file) \
  370. { \
  371. int i; \
  372. for_each_possible_cpu(i) { \
  373. struct list_head *list; \
  374. list = per_cpu_ptr((__sb)->s_files, i); \
  375. list_for_each_entry((__file), list, f_u.fu_list)
  376. #define while_file_list_for_each_entry \
  377. } \
  378. }
  379. #else
  380. #define do_file_list_for_each_entry(__sb, __file) \
  381. { \
  382. struct list_head *list; \
  383. list = &(sb)->s_files; \
  384. list_for_each_entry((__file), list, f_u.fu_list)
  385. #define while_file_list_for_each_entry \
  386. }
  387. #endif
  388. /**
  389. * mark_files_ro - mark all files read-only
  390. * @sb: superblock in question
  391. *
  392. * All files are marked read-only. We don't care about pending
  393. * delete files so this should be used in 'force' mode only.
  394. */
  395. void mark_files_ro(struct super_block *sb)
  396. {
  397. struct file *f;
  398. lg_global_lock(&files_lglock);
  399. do_file_list_for_each_entry(sb, f) {
  400. if (!S_ISREG(file_inode(f)->i_mode))
  401. continue;
  402. if (!file_count(f))
  403. continue;
  404. if (!(f->f_mode & FMODE_WRITE))
  405. continue;
  406. spin_lock(&f->f_lock);
  407. f->f_mode &= ~FMODE_WRITE;
  408. spin_unlock(&f->f_lock);
  409. if (file_check_writeable(f) != 0)
  410. continue;
  411. __mnt_drop_write(f->f_path.mnt);
  412. file_release_write(f);
  413. } while_file_list_for_each_entry;
  414. lg_global_unlock(&files_lglock);
  415. }
  416. void __init files_init(unsigned long mempages)
  417. {
  418. unsigned long n;
  419. filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
  420. SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  421. /*
  422. * One file with associated inode and dcache is very roughly 1K.
  423. * Per default don't use more than 10% of our memory for files.
  424. */
  425. n = (mempages * (PAGE_SIZE / 1024)) / 10;
  426. files_stat.max_files = max_t(unsigned long, n, NR_FILE);
  427. files_defer_init();
  428. lg_lock_init(&files_lglock, "files_lglock");
  429. percpu_counter_init(&nr_files, 0);
  430. }