file_table.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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_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 NULL, if there are no more free file structures or
  84. * we run out of memory.
  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. /*
  98. * Privileged users can go above max_files
  99. */
  100. if (get_nr_files() >= files_stat.max_files && !capable(CAP_SYS_ADMIN)) {
  101. /*
  102. * percpu_counters are inaccurate. Do an expensive check before
  103. * we go and fail.
  104. */
  105. if (percpu_counter_sum_positive(&nr_files) >= files_stat.max_files)
  106. goto over;
  107. }
  108. f = kmem_cache_zalloc(filp_cachep, GFP_KERNEL);
  109. if (f == NULL)
  110. goto fail;
  111. percpu_counter_inc(&nr_files);
  112. f->f_cred = get_cred(cred);
  113. if (security_file_alloc(f))
  114. goto fail_sec;
  115. INIT_LIST_HEAD(&f->f_u.fu_list);
  116. atomic_long_set(&f->f_count, 1);
  117. rwlock_init(&f->f_owner.lock);
  118. spin_lock_init(&f->f_lock);
  119. eventpoll_init_file(f);
  120. /* f->f_version: 0 */
  121. return f;
  122. over:
  123. /* Ran out of filps - report that */
  124. if (get_nr_files() > old_max) {
  125. pr_info("VFS: file-max limit %lu reached\n", get_max_files());
  126. old_max = get_nr_files();
  127. }
  128. goto fail;
  129. fail_sec:
  130. file_free(f);
  131. fail:
  132. return NULL;
  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 (!file)
  155. return NULL;
  156. file->f_path = *path;
  157. file->f_mapping = path->dentry->d_inode->i_mapping;
  158. file->f_mode = mode;
  159. file->f_op = fop;
  160. /*
  161. * These mounts don't really matter in practice
  162. * for r/o bind mounts. They aren't userspace-
  163. * visible. We do this for consistency, and so
  164. * that we can do debugging checks at __fput()
  165. */
  166. if ((mode & FMODE_WRITE) && !special_file(path->dentry->d_inode->i_mode)) {
  167. file_take_write(file);
  168. WARN_ON(mnt_clone_write(path->mnt));
  169. }
  170. if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  171. i_readcount_inc(path->dentry->d_inode);
  172. return file;
  173. }
  174. EXPORT_SYMBOL(alloc_file);
  175. /**
  176. * drop_file_write_access - give up ability to write to a file
  177. * @file: the file to which we will stop writing
  178. *
  179. * This is a central place which will give up the ability
  180. * to write to @file, along with access to write through
  181. * its vfsmount.
  182. */
  183. static void drop_file_write_access(struct file *file)
  184. {
  185. struct vfsmount *mnt = file->f_path.mnt;
  186. struct dentry *dentry = file->f_path.dentry;
  187. struct inode *inode = dentry->d_inode;
  188. put_write_access(inode);
  189. if (special_file(inode->i_mode))
  190. return;
  191. if (file_check_writeable(file) != 0)
  192. return;
  193. __mnt_drop_write(mnt);
  194. file_release_write(file);
  195. }
  196. /* the real guts of fput() - releasing the last reference to file
  197. */
  198. static void __fput(struct file *file)
  199. {
  200. struct dentry *dentry = file->f_path.dentry;
  201. struct vfsmount *mnt = file->f_path.mnt;
  202. struct inode *inode = dentry->d_inode;
  203. might_sleep();
  204. fsnotify_close(file);
  205. /*
  206. * The function eventpoll_release() should be the first called
  207. * in the file cleanup chain.
  208. */
  209. eventpoll_release(file);
  210. locks_remove_flock(file);
  211. if (unlikely(file->f_flags & FASYNC)) {
  212. if (file->f_op && file->f_op->fasync)
  213. file->f_op->fasync(-1, file, 0);
  214. }
  215. if (file->f_op && file->f_op->release)
  216. file->f_op->release(inode, file);
  217. security_file_free(file);
  218. ima_file_free(file);
  219. if (unlikely(S_ISCHR(inode->i_mode) && inode->i_cdev != NULL &&
  220. !(file->f_mode & FMODE_PATH))) {
  221. cdev_put(inode->i_cdev);
  222. }
  223. fops_put(file->f_op);
  224. put_pid(file->f_owner.pid);
  225. if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  226. i_readcount_dec(inode);
  227. if (file->f_mode & FMODE_WRITE)
  228. drop_file_write_access(file);
  229. file->f_path.dentry = NULL;
  230. file->f_path.mnt = NULL;
  231. file_free(file);
  232. dput(dentry);
  233. mntput(mnt);
  234. }
  235. static DEFINE_SPINLOCK(delayed_fput_lock);
  236. static LIST_HEAD(delayed_fput_list);
  237. static void delayed_fput(struct work_struct *unused)
  238. {
  239. LIST_HEAD(head);
  240. spin_lock_irq(&delayed_fput_lock);
  241. list_splice_init(&delayed_fput_list, &head);
  242. spin_unlock_irq(&delayed_fput_lock);
  243. while (!list_empty(&head)) {
  244. struct file *f = list_first_entry(&head, struct file, f_u.fu_list);
  245. list_del_init(&f->f_u.fu_list);
  246. __fput(f);
  247. }
  248. }
  249. static void ____fput(struct callback_head *work)
  250. {
  251. __fput(container_of(work, struct file, f_u.fu_rcuhead));
  252. }
  253. /*
  254. * If kernel thread really needs to have the final fput() it has done
  255. * to complete, call this. The only user right now is the boot - we
  256. * *do* need to make sure our writes to binaries on initramfs has
  257. * not left us with opened struct file waiting for __fput() - execve()
  258. * won't work without that. Please, don't add more callers without
  259. * very good reasons; in particular, never call that with locks
  260. * held and never call that from a thread that might need to do
  261. * some work on any kind of umount.
  262. */
  263. void flush_delayed_fput(void)
  264. {
  265. delayed_fput(NULL);
  266. }
  267. static DECLARE_WORK(delayed_fput_work, delayed_fput);
  268. void fput(struct file *file)
  269. {
  270. if (atomic_long_dec_and_test(&file->f_count)) {
  271. struct task_struct *task = current;
  272. file_sb_list_del(file);
  273. if (unlikely(in_interrupt() || task->flags & PF_KTHREAD)) {
  274. unsigned long flags;
  275. spin_lock_irqsave(&delayed_fput_lock, flags);
  276. list_add(&file->f_u.fu_list, &delayed_fput_list);
  277. schedule_work(&delayed_fput_work);
  278. spin_unlock_irqrestore(&delayed_fput_lock, flags);
  279. return;
  280. }
  281. init_task_work(&file->f_u.fu_rcuhead, ____fput);
  282. task_work_add(task, &file->f_u.fu_rcuhead, true);
  283. }
  284. }
  285. /*
  286. * synchronous analog of fput(); for kernel threads that might be needed
  287. * in some umount() (and thus can't use flush_delayed_fput() without
  288. * risking deadlocks), need to wait for completion of __fput() and know
  289. * for this specific struct file it won't involve anything that would
  290. * need them. Use only if you really need it - at the very least,
  291. * don't blindly convert fput() by kernel thread to that.
  292. */
  293. void __fput_sync(struct file *file)
  294. {
  295. if (atomic_long_dec_and_test(&file->f_count)) {
  296. struct task_struct *task = current;
  297. file_sb_list_del(file);
  298. BUG_ON(!(task->flags & PF_KTHREAD));
  299. __fput(file);
  300. }
  301. }
  302. EXPORT_SYMBOL(fput);
  303. struct file *fget(unsigned int fd)
  304. {
  305. struct file *file;
  306. struct files_struct *files = current->files;
  307. rcu_read_lock();
  308. file = fcheck_files(files, fd);
  309. if (file) {
  310. /* File object ref couldn't be taken */
  311. if (file->f_mode & FMODE_PATH ||
  312. !atomic_long_inc_not_zero(&file->f_count))
  313. file = NULL;
  314. }
  315. rcu_read_unlock();
  316. return file;
  317. }
  318. EXPORT_SYMBOL(fget);
  319. struct file *fget_raw(unsigned int fd)
  320. {
  321. struct file *file;
  322. struct files_struct *files = current->files;
  323. rcu_read_lock();
  324. file = fcheck_files(files, fd);
  325. if (file) {
  326. /* File object ref couldn't be taken */
  327. if (!atomic_long_inc_not_zero(&file->f_count))
  328. file = NULL;
  329. }
  330. rcu_read_unlock();
  331. return file;
  332. }
  333. EXPORT_SYMBOL(fget_raw);
  334. /*
  335. * Lightweight file lookup - no refcnt increment if fd table isn't shared.
  336. *
  337. * You can use this instead of fget if you satisfy all of the following
  338. * conditions:
  339. * 1) You must call fput_light before exiting the syscall and returning control
  340. * to userspace (i.e. you cannot remember the returned struct file * after
  341. * returning to userspace).
  342. * 2) You must not call filp_close on the returned struct file * in between
  343. * calls to fget_light and fput_light.
  344. * 3) You must not clone the current task in between the calls to fget_light
  345. * and fput_light.
  346. *
  347. * The fput_needed flag returned by fget_light should be passed to the
  348. * corresponding fput_light.
  349. */
  350. struct file *fget_light(unsigned int fd, int *fput_needed)
  351. {
  352. struct file *file;
  353. struct files_struct *files = current->files;
  354. *fput_needed = 0;
  355. if (atomic_read(&files->count) == 1) {
  356. file = fcheck_files(files, fd);
  357. if (file && (file->f_mode & FMODE_PATH))
  358. file = NULL;
  359. } else {
  360. rcu_read_lock();
  361. file = fcheck_files(files, fd);
  362. if (file) {
  363. if (!(file->f_mode & FMODE_PATH) &&
  364. atomic_long_inc_not_zero(&file->f_count))
  365. *fput_needed = 1;
  366. else
  367. /* Didn't get the reference, someone's freed */
  368. file = NULL;
  369. }
  370. rcu_read_unlock();
  371. }
  372. return file;
  373. }
  374. struct file *fget_raw_light(unsigned int fd, int *fput_needed)
  375. {
  376. struct file *file;
  377. struct files_struct *files = current->files;
  378. *fput_needed = 0;
  379. if (atomic_read(&files->count) == 1) {
  380. file = fcheck_files(files, fd);
  381. } else {
  382. rcu_read_lock();
  383. file = fcheck_files(files, fd);
  384. if (file) {
  385. if (atomic_long_inc_not_zero(&file->f_count))
  386. *fput_needed = 1;
  387. else
  388. /* Didn't get the reference, someone's freed */
  389. file = NULL;
  390. }
  391. rcu_read_unlock();
  392. }
  393. return file;
  394. }
  395. void put_filp(struct file *file)
  396. {
  397. if (atomic_long_dec_and_test(&file->f_count)) {
  398. security_file_free(file);
  399. file_sb_list_del(file);
  400. file_free(file);
  401. }
  402. }
  403. static inline int file_list_cpu(struct file *file)
  404. {
  405. #ifdef CONFIG_SMP
  406. return file->f_sb_list_cpu;
  407. #else
  408. return smp_processor_id();
  409. #endif
  410. }
  411. /* helper for file_sb_list_add to reduce ifdefs */
  412. static inline void __file_sb_list_add(struct file *file, struct super_block *sb)
  413. {
  414. struct list_head *list;
  415. #ifdef CONFIG_SMP
  416. int cpu;
  417. cpu = smp_processor_id();
  418. file->f_sb_list_cpu = cpu;
  419. list = per_cpu_ptr(sb->s_files, cpu);
  420. #else
  421. list = &sb->s_files;
  422. #endif
  423. list_add(&file->f_u.fu_list, list);
  424. }
  425. /**
  426. * file_sb_list_add - add a file to the sb's file list
  427. * @file: file to add
  428. * @sb: sb to add it to
  429. *
  430. * Use this function to associate a file with the superblock of the inode it
  431. * refers to.
  432. */
  433. void file_sb_list_add(struct file *file, struct super_block *sb)
  434. {
  435. lg_local_lock(&files_lglock);
  436. __file_sb_list_add(file, sb);
  437. lg_local_unlock(&files_lglock);
  438. }
  439. /**
  440. * file_sb_list_del - remove a file from the sb's file list
  441. * @file: file to remove
  442. * @sb: sb to remove it from
  443. *
  444. * Use this function to remove a file from its superblock.
  445. */
  446. void file_sb_list_del(struct file *file)
  447. {
  448. if (!list_empty(&file->f_u.fu_list)) {
  449. lg_local_lock_cpu(&files_lglock, file_list_cpu(file));
  450. list_del_init(&file->f_u.fu_list);
  451. lg_local_unlock_cpu(&files_lglock, file_list_cpu(file));
  452. }
  453. }
  454. #ifdef CONFIG_SMP
  455. /*
  456. * These macros iterate all files on all CPUs for a given superblock.
  457. * files_lglock must be held globally.
  458. */
  459. #define do_file_list_for_each_entry(__sb, __file) \
  460. { \
  461. int i; \
  462. for_each_possible_cpu(i) { \
  463. struct list_head *list; \
  464. list = per_cpu_ptr((__sb)->s_files, i); \
  465. list_for_each_entry((__file), list, f_u.fu_list)
  466. #define while_file_list_for_each_entry \
  467. } \
  468. }
  469. #else
  470. #define do_file_list_for_each_entry(__sb, __file) \
  471. { \
  472. struct list_head *list; \
  473. list = &(sb)->s_files; \
  474. list_for_each_entry((__file), list, f_u.fu_list)
  475. #define while_file_list_for_each_entry \
  476. }
  477. #endif
  478. /**
  479. * mark_files_ro - mark all files read-only
  480. * @sb: superblock in question
  481. *
  482. * All files are marked read-only. We don't care about pending
  483. * delete files so this should be used in 'force' mode only.
  484. */
  485. void mark_files_ro(struct super_block *sb)
  486. {
  487. struct file *f;
  488. lg_global_lock(&files_lglock);
  489. do_file_list_for_each_entry(sb, f) {
  490. if (!S_ISREG(f->f_path.dentry->d_inode->i_mode))
  491. continue;
  492. if (!file_count(f))
  493. continue;
  494. if (!(f->f_mode & FMODE_WRITE))
  495. continue;
  496. spin_lock(&f->f_lock);
  497. f->f_mode &= ~FMODE_WRITE;
  498. spin_unlock(&f->f_lock);
  499. if (file_check_writeable(f) != 0)
  500. continue;
  501. file_release_write(f);
  502. mnt_drop_write_file(f);
  503. } while_file_list_for_each_entry;
  504. lg_global_unlock(&files_lglock);
  505. }
  506. void __init files_init(unsigned long mempages)
  507. {
  508. unsigned long n;
  509. filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
  510. SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  511. /*
  512. * One file with associated inode and dcache is very roughly 1K.
  513. * Per default don't use more than 10% of our memory for files.
  514. */
  515. n = (mempages * (PAGE_SIZE / 1024)) / 10;
  516. files_stat.max_files = max_t(unsigned long, n, NR_FILE);
  517. files_defer_init();
  518. lg_lock_init(&files_lglock, "files_lglock");
  519. percpu_counter_init(&nr_files, 0);
  520. }