file_table.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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/ima.h>
  26. #include <asm/atomic.h>
  27. #include "internal.h"
  28. /* sysctl tunables... */
  29. struct files_stat_struct files_stat = {
  30. .max_files = NR_FILE
  31. };
  32. DECLARE_LGLOCK(files_lglock);
  33. DEFINE_LGLOCK(files_lglock);
  34. /* SLAB cache for file structures */
  35. static struct kmem_cache *filp_cachep __read_mostly;
  36. static struct percpu_counter nr_files __cacheline_aligned_in_smp;
  37. static inline void file_free_rcu(struct rcu_head *head)
  38. {
  39. struct file *f = container_of(head, struct file, f_u.fu_rcuhead);
  40. put_cred(f->f_cred);
  41. kmem_cache_free(filp_cachep, f);
  42. }
  43. static inline void file_free(struct file *f)
  44. {
  45. percpu_counter_dec(&nr_files);
  46. file_check_state(f);
  47. call_rcu(&f->f_u.fu_rcuhead, file_free_rcu);
  48. }
  49. /*
  50. * Return the total number of open files in the system
  51. */
  52. static long get_nr_files(void)
  53. {
  54. return percpu_counter_read_positive(&nr_files);
  55. }
  56. /*
  57. * Return the maximum number of open files in the system
  58. */
  59. unsigned long get_max_files(void)
  60. {
  61. return files_stat.max_files;
  62. }
  63. EXPORT_SYMBOL_GPL(get_max_files);
  64. /*
  65. * Handle nr_files sysctl
  66. */
  67. #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
  68. int proc_nr_files(ctl_table *table, int write,
  69. void __user *buffer, size_t *lenp, loff_t *ppos)
  70. {
  71. files_stat.nr_files = get_nr_files();
  72. return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
  73. }
  74. #else
  75. int proc_nr_files(ctl_table *table, int write,
  76. void __user *buffer, size_t *lenp, loff_t *ppos)
  77. {
  78. return -ENOSYS;
  79. }
  80. #endif
  81. /* Find an unused file structure and return a pointer to it.
  82. * Returns NULL, if there are no more free file structures or
  83. * we run out of memory.
  84. *
  85. * Be very careful using this. You are responsible for
  86. * getting write access to any mount that you might assign
  87. * to this filp, if it is opened for write. If this is not
  88. * done, you will imbalance int the mount's writer count
  89. * and a warning at __fput() time.
  90. */
  91. struct file *get_empty_filp(void)
  92. {
  93. const struct cred *cred = current_cred();
  94. static long old_max;
  95. struct file * f;
  96. /*
  97. * Privileged users can go above max_files
  98. */
  99. if (get_nr_files() >= files_stat.max_files && !capable(CAP_SYS_ADMIN)) {
  100. /*
  101. * percpu_counters are inaccurate. Do an expensive check before
  102. * we go and fail.
  103. */
  104. if (percpu_counter_sum_positive(&nr_files) >= files_stat.max_files)
  105. goto over;
  106. }
  107. f = kmem_cache_zalloc(filp_cachep, GFP_KERNEL);
  108. if (f == NULL)
  109. goto fail;
  110. percpu_counter_inc(&nr_files);
  111. f->f_cred = get_cred(cred);
  112. if (security_file_alloc(f))
  113. goto fail_sec;
  114. INIT_LIST_HEAD(&f->f_u.fu_list);
  115. atomic_long_set(&f->f_count, 1);
  116. rwlock_init(&f->f_owner.lock);
  117. spin_lock_init(&f->f_lock);
  118. eventpoll_init_file(f);
  119. /* f->f_version: 0 */
  120. return f;
  121. over:
  122. /* Ran out of filps - report that */
  123. if (get_nr_files() > old_max) {
  124. pr_info("VFS: file-max limit %lu reached\n", get_max_files());
  125. old_max = get_nr_files();
  126. }
  127. goto fail;
  128. fail_sec:
  129. file_free(f);
  130. fail:
  131. return NULL;
  132. }
  133. /**
  134. * alloc_file - allocate and initialize a 'struct file'
  135. * @mnt: the vfsmount on which the file will reside
  136. * @dentry: the dentry representing the new file
  137. * @mode: the mode with which the new file will be opened
  138. * @fop: the 'struct file_operations' for the new file
  139. *
  140. * Use this instead of get_empty_filp() to get a new
  141. * 'struct file'. Do so because of the same initialization
  142. * pitfalls reasons listed for init_file(). This is a
  143. * preferred interface to using init_file().
  144. *
  145. * If all the callers of init_file() are eliminated, its
  146. * code should be moved into this function.
  147. */
  148. struct file *alloc_file(struct path *path, fmode_t mode,
  149. const struct file_operations *fop)
  150. {
  151. struct file *file;
  152. file = get_empty_filp();
  153. if (!file)
  154. return NULL;
  155. file->f_path = *path;
  156. file->f_mapping = path->dentry->d_inode->i_mapping;
  157. file->f_mode = mode;
  158. file->f_op = fop;
  159. /*
  160. * These mounts don't really matter in practice
  161. * for r/o bind mounts. They aren't userspace-
  162. * visible. We do this for consistency, and so
  163. * that we can do debugging checks at __fput()
  164. */
  165. if ((mode & FMODE_WRITE) && !special_file(path->dentry->d_inode->i_mode)) {
  166. file_take_write(file);
  167. WARN_ON(mnt_clone_write(path->mnt));
  168. }
  169. if ((mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  170. i_readcount_inc(path->dentry->d_inode);
  171. return file;
  172. }
  173. EXPORT_SYMBOL(alloc_file);
  174. /**
  175. * drop_file_write_access - give up ability to write to a file
  176. * @file: the file to which we will stop writing
  177. *
  178. * This is a central place which will give up the ability
  179. * to write to @file, along with access to write through
  180. * its vfsmount.
  181. */
  182. void drop_file_write_access(struct file *file)
  183. {
  184. struct vfsmount *mnt = file->f_path.mnt;
  185. struct dentry *dentry = file->f_path.dentry;
  186. struct inode *inode = dentry->d_inode;
  187. put_write_access(inode);
  188. if (special_file(inode->i_mode))
  189. return;
  190. if (file_check_writeable(file) != 0)
  191. return;
  192. mnt_drop_write(mnt);
  193. file_release_write(file);
  194. }
  195. EXPORT_SYMBOL_GPL(drop_file_write_access);
  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. file_sb_list_del(file);
  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_free(file);
  233. dput(dentry);
  234. mntput(mnt);
  235. }
  236. void fput(struct file *file)
  237. {
  238. if (atomic_long_dec_and_test(&file->f_count))
  239. __fput(file);
  240. }
  241. EXPORT_SYMBOL(fput);
  242. struct file *fget(unsigned int fd)
  243. {
  244. struct file *file;
  245. struct files_struct *files = current->files;
  246. rcu_read_lock();
  247. file = fcheck_files(files, fd);
  248. if (file) {
  249. /* File object ref couldn't be taken */
  250. if (file->f_mode & FMODE_PATH ||
  251. !atomic_long_inc_not_zero(&file->f_count))
  252. file = NULL;
  253. }
  254. rcu_read_unlock();
  255. return file;
  256. }
  257. EXPORT_SYMBOL(fget);
  258. struct file *fget_raw(unsigned int fd)
  259. {
  260. struct file *file;
  261. struct files_struct *files = current->files;
  262. rcu_read_lock();
  263. file = fcheck_files(files, fd);
  264. if (file) {
  265. /* File object ref couldn't be taken */
  266. if (!atomic_long_inc_not_zero(&file->f_count))
  267. file = NULL;
  268. }
  269. rcu_read_unlock();
  270. return file;
  271. }
  272. EXPORT_SYMBOL(fget_raw);
  273. /*
  274. * Lightweight file lookup - no refcnt increment if fd table isn't shared.
  275. *
  276. * You can use this instead of fget if you satisfy all of the following
  277. * conditions:
  278. * 1) You must call fput_light before exiting the syscall and returning control
  279. * to userspace (i.e. you cannot remember the returned struct file * after
  280. * returning to userspace).
  281. * 2) You must not call filp_close on the returned struct file * in between
  282. * calls to fget_light and fput_light.
  283. * 3) You must not clone the current task in between the calls to fget_light
  284. * and fput_light.
  285. *
  286. * The fput_needed flag returned by fget_light should be passed to the
  287. * corresponding fput_light.
  288. */
  289. struct file *fget_light(unsigned int fd, int *fput_needed)
  290. {
  291. struct file *file;
  292. struct files_struct *files = current->files;
  293. *fput_needed = 0;
  294. if (atomic_read(&files->count) == 1) {
  295. file = fcheck_files(files, fd);
  296. if (file && (file->f_mode & FMODE_PATH))
  297. file = NULL;
  298. } else {
  299. rcu_read_lock();
  300. file = fcheck_files(files, fd);
  301. if (file) {
  302. if (!(file->f_mode & FMODE_PATH) &&
  303. atomic_long_inc_not_zero(&file->f_count))
  304. *fput_needed = 1;
  305. else
  306. /* Didn't get the reference, someone's freed */
  307. file = NULL;
  308. }
  309. rcu_read_unlock();
  310. }
  311. return file;
  312. }
  313. struct file *fget_raw_light(unsigned int fd, int *fput_needed)
  314. {
  315. struct file *file;
  316. struct files_struct *files = current->files;
  317. *fput_needed = 0;
  318. if (atomic_read(&files->count) == 1) {
  319. file = fcheck_files(files, fd);
  320. } else {
  321. rcu_read_lock();
  322. file = fcheck_files(files, fd);
  323. if (file) {
  324. if (atomic_long_inc_not_zero(&file->f_count))
  325. *fput_needed = 1;
  326. else
  327. /* Didn't get the reference, someone's freed */
  328. file = NULL;
  329. }
  330. rcu_read_unlock();
  331. }
  332. return file;
  333. }
  334. void put_filp(struct file *file)
  335. {
  336. if (atomic_long_dec_and_test(&file->f_count)) {
  337. security_file_free(file);
  338. file_sb_list_del(file);
  339. file_free(file);
  340. }
  341. }
  342. static inline int file_list_cpu(struct file *file)
  343. {
  344. #ifdef CONFIG_SMP
  345. return file->f_sb_list_cpu;
  346. #else
  347. return smp_processor_id();
  348. #endif
  349. }
  350. /* helper for file_sb_list_add to reduce ifdefs */
  351. static inline void __file_sb_list_add(struct file *file, struct super_block *sb)
  352. {
  353. struct list_head *list;
  354. #ifdef CONFIG_SMP
  355. int cpu;
  356. cpu = smp_processor_id();
  357. file->f_sb_list_cpu = cpu;
  358. list = per_cpu_ptr(sb->s_files, cpu);
  359. #else
  360. list = &sb->s_files;
  361. #endif
  362. list_add(&file->f_u.fu_list, list);
  363. }
  364. /**
  365. * file_sb_list_add - add a file to the sb's file list
  366. * @file: file to add
  367. * @sb: sb to add it to
  368. *
  369. * Use this function to associate a file with the superblock of the inode it
  370. * refers to.
  371. */
  372. void file_sb_list_add(struct file *file, struct super_block *sb)
  373. {
  374. lg_local_lock(files_lglock);
  375. __file_sb_list_add(file, sb);
  376. lg_local_unlock(files_lglock);
  377. }
  378. /**
  379. * file_sb_list_del - remove a file from the sb's file list
  380. * @file: file to remove
  381. * @sb: sb to remove it from
  382. *
  383. * Use this function to remove a file from its superblock.
  384. */
  385. void file_sb_list_del(struct file *file)
  386. {
  387. if (!list_empty(&file->f_u.fu_list)) {
  388. lg_local_lock_cpu(files_lglock, file_list_cpu(file));
  389. list_del_init(&file->f_u.fu_list);
  390. lg_local_unlock_cpu(files_lglock, file_list_cpu(file));
  391. }
  392. }
  393. #ifdef CONFIG_SMP
  394. /*
  395. * These macros iterate all files on all CPUs for a given superblock.
  396. * files_lglock must be held globally.
  397. */
  398. #define do_file_list_for_each_entry(__sb, __file) \
  399. { \
  400. int i; \
  401. for_each_possible_cpu(i) { \
  402. struct list_head *list; \
  403. list = per_cpu_ptr((__sb)->s_files, i); \
  404. list_for_each_entry((__file), list, f_u.fu_list)
  405. #define while_file_list_for_each_entry \
  406. } \
  407. }
  408. #else
  409. #define do_file_list_for_each_entry(__sb, __file) \
  410. { \
  411. struct list_head *list; \
  412. list = &(sb)->s_files; \
  413. list_for_each_entry((__file), list, f_u.fu_list)
  414. #define while_file_list_for_each_entry \
  415. }
  416. #endif
  417. int fs_may_remount_ro(struct super_block *sb)
  418. {
  419. struct file *file;
  420. /* Check that no files are currently opened for writing. */
  421. lg_global_lock(files_lglock);
  422. do_file_list_for_each_entry(sb, file) {
  423. struct inode *inode = file->f_path.dentry->d_inode;
  424. /* File with pending delete? */
  425. if (inode->i_nlink == 0)
  426. goto too_bad;
  427. /* Writeable file? */
  428. if (S_ISREG(inode->i_mode) && (file->f_mode & FMODE_WRITE))
  429. goto too_bad;
  430. } while_file_list_for_each_entry;
  431. lg_global_unlock(files_lglock);
  432. return 1; /* Tis' cool bro. */
  433. too_bad:
  434. lg_global_unlock(files_lglock);
  435. return 0;
  436. }
  437. /**
  438. * mark_files_ro - mark all files read-only
  439. * @sb: superblock in question
  440. *
  441. * All files are marked read-only. We don't care about pending
  442. * delete files so this should be used in 'force' mode only.
  443. */
  444. void mark_files_ro(struct super_block *sb)
  445. {
  446. struct file *f;
  447. retry:
  448. lg_global_lock(files_lglock);
  449. do_file_list_for_each_entry(sb, f) {
  450. struct vfsmount *mnt;
  451. if (!S_ISREG(f->f_path.dentry->d_inode->i_mode))
  452. continue;
  453. if (!file_count(f))
  454. continue;
  455. if (!(f->f_mode & FMODE_WRITE))
  456. continue;
  457. spin_lock(&f->f_lock);
  458. f->f_mode &= ~FMODE_WRITE;
  459. spin_unlock(&f->f_lock);
  460. if (file_check_writeable(f) != 0)
  461. continue;
  462. file_release_write(f);
  463. mnt = mntget(f->f_path.mnt);
  464. /* This can sleep, so we can't hold the spinlock. */
  465. lg_global_unlock(files_lglock);
  466. mnt_drop_write(mnt);
  467. mntput(mnt);
  468. goto retry;
  469. } while_file_list_for_each_entry;
  470. lg_global_unlock(files_lglock);
  471. }
  472. void __init files_init(unsigned long mempages)
  473. {
  474. unsigned long n;
  475. filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
  476. SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  477. /*
  478. * One file with associated inode and dcache is very roughly 1K.
  479. * Per default don't use more than 10% of our memory for files.
  480. */
  481. n = (mempages * (PAGE_SIZE / 1024)) / 10;
  482. files_stat.max_files = max_t(unsigned long, n, NR_FILE);
  483. files_defer_init();
  484. lg_lock_init(files_lglock);
  485. percpu_counter_init(&nr_files, 0);
  486. }