file_table.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. ima_counts_get(file);
  170. return file;
  171. }
  172. EXPORT_SYMBOL(alloc_file);
  173. /**
  174. * drop_file_write_access - give up ability to write to a file
  175. * @file: the file to which we will stop writing
  176. *
  177. * This is a central place which will give up the ability
  178. * to write to @file, along with access to write through
  179. * its vfsmount.
  180. */
  181. void drop_file_write_access(struct file *file)
  182. {
  183. struct vfsmount *mnt = file->f_path.mnt;
  184. struct dentry *dentry = file->f_path.dentry;
  185. struct inode *inode = dentry->d_inode;
  186. put_write_access(inode);
  187. if (special_file(inode->i_mode))
  188. return;
  189. if (file_check_writeable(file) != 0)
  190. return;
  191. mnt_drop_write(mnt);
  192. file_release_write(file);
  193. }
  194. EXPORT_SYMBOL_GPL(drop_file_write_access);
  195. /* the real guts of fput() - releasing the last reference to file
  196. */
  197. static void __fput(struct file *file)
  198. {
  199. struct dentry *dentry = file->f_path.dentry;
  200. struct vfsmount *mnt = file->f_path.mnt;
  201. struct inode *inode = dentry->d_inode;
  202. might_sleep();
  203. fsnotify_close(file);
  204. /*
  205. * The function eventpoll_release() should be the first called
  206. * in the file cleanup chain.
  207. */
  208. eventpoll_release(file);
  209. locks_remove_flock(file);
  210. if (unlikely(file->f_flags & FASYNC)) {
  211. if (file->f_op && file->f_op->fasync)
  212. file->f_op->fasync(-1, file, 0);
  213. }
  214. if (file->f_op && file->f_op->release)
  215. file->f_op->release(inode, file);
  216. security_file_free(file);
  217. ima_file_free(file);
  218. if (unlikely(S_ISCHR(inode->i_mode) && inode->i_cdev != NULL))
  219. cdev_put(inode->i_cdev);
  220. fops_put(file->f_op);
  221. put_pid(file->f_owner.pid);
  222. file_sb_list_del(file);
  223. if (file->f_mode & FMODE_WRITE)
  224. drop_file_write_access(file);
  225. file->f_path.dentry = NULL;
  226. file->f_path.mnt = NULL;
  227. file_free(file);
  228. dput(dentry);
  229. mntput(mnt);
  230. }
  231. void fput(struct file *file)
  232. {
  233. if (atomic_long_dec_and_test(&file->f_count))
  234. __fput(file);
  235. }
  236. EXPORT_SYMBOL(fput);
  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. /* File object ref couldn't be taken */
  245. if (file->f_mode & FMODE_PATH ||
  246. !atomic_long_inc_not_zero(&file->f_count))
  247. file = NULL;
  248. }
  249. rcu_read_unlock();
  250. return file;
  251. }
  252. EXPORT_SYMBOL(fget);
  253. struct file *fget_raw(unsigned int fd)
  254. {
  255. struct file *file;
  256. struct files_struct *files = current->files;
  257. rcu_read_lock();
  258. file = fcheck_files(files, fd);
  259. if (file) {
  260. /* File object ref couldn't be taken */
  261. if (!atomic_long_inc_not_zero(&file->f_count))
  262. file = NULL;
  263. }
  264. rcu_read_unlock();
  265. return file;
  266. }
  267. EXPORT_SYMBOL(fget_raw);
  268. /*
  269. * Lightweight file lookup - no refcnt increment if fd table isn't shared.
  270. *
  271. * You can use this instead of fget if you satisfy all of the following
  272. * conditions:
  273. * 1) You must call fput_light before exiting the syscall and returning control
  274. * to userspace (i.e. you cannot remember the returned struct file * after
  275. * returning to userspace).
  276. * 2) You must not call filp_close on the returned struct file * in between
  277. * calls to fget_light and fput_light.
  278. * 3) You must not clone the current task in between the calls to fget_light
  279. * and fput_light.
  280. *
  281. * The fput_needed flag returned by fget_light should be passed to the
  282. * corresponding fput_light.
  283. */
  284. struct file *fget_light(unsigned int fd, int *fput_needed)
  285. {
  286. struct file *file;
  287. struct files_struct *files = current->files;
  288. *fput_needed = 0;
  289. if (atomic_read(&files->count) == 1) {
  290. file = fcheck_files(files, fd);
  291. if (file && (file->f_mode & FMODE_PATH))
  292. file = NULL;
  293. } else {
  294. rcu_read_lock();
  295. file = fcheck_files(files, fd);
  296. if (file) {
  297. if (!(file->f_mode & FMODE_PATH) &&
  298. atomic_long_inc_not_zero(&file->f_count))
  299. *fput_needed = 1;
  300. else
  301. /* Didn't get the reference, someone's freed */
  302. file = NULL;
  303. }
  304. rcu_read_unlock();
  305. }
  306. return file;
  307. }
  308. struct file *fget_raw_light(unsigned int fd, int *fput_needed)
  309. {
  310. struct file *file;
  311. struct files_struct *files = current->files;
  312. *fput_needed = 0;
  313. if (atomic_read(&files->count) == 1) {
  314. file = fcheck_files(files, fd);
  315. } else {
  316. rcu_read_lock();
  317. file = fcheck_files(files, fd);
  318. if (file) {
  319. if (atomic_long_inc_not_zero(&file->f_count))
  320. *fput_needed = 1;
  321. else
  322. /* Didn't get the reference, someone's freed */
  323. file = NULL;
  324. }
  325. rcu_read_unlock();
  326. }
  327. return file;
  328. }
  329. void put_filp(struct file *file)
  330. {
  331. if (atomic_long_dec_and_test(&file->f_count)) {
  332. security_file_free(file);
  333. file_sb_list_del(file);
  334. file_free(file);
  335. }
  336. }
  337. static inline int file_list_cpu(struct file *file)
  338. {
  339. #ifdef CONFIG_SMP
  340. return file->f_sb_list_cpu;
  341. #else
  342. return smp_processor_id();
  343. #endif
  344. }
  345. /* helper for file_sb_list_add to reduce ifdefs */
  346. static inline void __file_sb_list_add(struct file *file, struct super_block *sb)
  347. {
  348. struct list_head *list;
  349. #ifdef CONFIG_SMP
  350. int cpu;
  351. cpu = smp_processor_id();
  352. file->f_sb_list_cpu = cpu;
  353. list = per_cpu_ptr(sb->s_files, cpu);
  354. #else
  355. list = &sb->s_files;
  356. #endif
  357. list_add(&file->f_u.fu_list, list);
  358. }
  359. /**
  360. * file_sb_list_add - add a file to the sb's file list
  361. * @file: file to add
  362. * @sb: sb to add it to
  363. *
  364. * Use this function to associate a file with the superblock of the inode it
  365. * refers to.
  366. */
  367. void file_sb_list_add(struct file *file, struct super_block *sb)
  368. {
  369. lg_local_lock(files_lglock);
  370. __file_sb_list_add(file, sb);
  371. lg_local_unlock(files_lglock);
  372. }
  373. /**
  374. * file_sb_list_del - remove a file from the sb's file list
  375. * @file: file to remove
  376. * @sb: sb to remove it from
  377. *
  378. * Use this function to remove a file from its superblock.
  379. */
  380. void file_sb_list_del(struct file *file)
  381. {
  382. if (!list_empty(&file->f_u.fu_list)) {
  383. lg_local_lock_cpu(files_lglock, file_list_cpu(file));
  384. list_del_init(&file->f_u.fu_list);
  385. lg_local_unlock_cpu(files_lglock, file_list_cpu(file));
  386. }
  387. }
  388. #ifdef CONFIG_SMP
  389. /*
  390. * These macros iterate all files on all CPUs for a given superblock.
  391. * files_lglock must be held globally.
  392. */
  393. #define do_file_list_for_each_entry(__sb, __file) \
  394. { \
  395. int i; \
  396. for_each_possible_cpu(i) { \
  397. struct list_head *list; \
  398. list = per_cpu_ptr((__sb)->s_files, i); \
  399. list_for_each_entry((__file), list, f_u.fu_list)
  400. #define while_file_list_for_each_entry \
  401. } \
  402. }
  403. #else
  404. #define do_file_list_for_each_entry(__sb, __file) \
  405. { \
  406. struct list_head *list; \
  407. list = &(sb)->s_files; \
  408. list_for_each_entry((__file), list, f_u.fu_list)
  409. #define while_file_list_for_each_entry \
  410. }
  411. #endif
  412. int fs_may_remount_ro(struct super_block *sb)
  413. {
  414. struct file *file;
  415. /* Check that no files are currently opened for writing. */
  416. lg_global_lock(files_lglock);
  417. do_file_list_for_each_entry(sb, file) {
  418. struct inode *inode = file->f_path.dentry->d_inode;
  419. /* File with pending delete? */
  420. if (inode->i_nlink == 0)
  421. goto too_bad;
  422. /* Writeable file? */
  423. if (S_ISREG(inode->i_mode) && (file->f_mode & FMODE_WRITE))
  424. goto too_bad;
  425. } while_file_list_for_each_entry;
  426. lg_global_unlock(files_lglock);
  427. return 1; /* Tis' cool bro. */
  428. too_bad:
  429. lg_global_unlock(files_lglock);
  430. return 0;
  431. }
  432. /**
  433. * mark_files_ro - mark all files read-only
  434. * @sb: superblock in question
  435. *
  436. * All files are marked read-only. We don't care about pending
  437. * delete files so this should be used in 'force' mode only.
  438. */
  439. void mark_files_ro(struct super_block *sb)
  440. {
  441. struct file *f;
  442. retry:
  443. lg_global_lock(files_lglock);
  444. do_file_list_for_each_entry(sb, f) {
  445. struct vfsmount *mnt;
  446. if (!S_ISREG(f->f_path.dentry->d_inode->i_mode))
  447. continue;
  448. if (!file_count(f))
  449. continue;
  450. if (!(f->f_mode & FMODE_WRITE))
  451. continue;
  452. spin_lock(&f->f_lock);
  453. f->f_mode &= ~FMODE_WRITE;
  454. spin_unlock(&f->f_lock);
  455. if (file_check_writeable(f) != 0)
  456. continue;
  457. file_release_write(f);
  458. mnt = mntget(f->f_path.mnt);
  459. /* This can sleep, so we can't hold the spinlock. */
  460. lg_global_unlock(files_lglock);
  461. mnt_drop_write(mnt);
  462. mntput(mnt);
  463. goto retry;
  464. } while_file_list_for_each_entry;
  465. lg_global_unlock(files_lglock);
  466. }
  467. void __init files_init(unsigned long mempages)
  468. {
  469. unsigned long n;
  470. filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
  471. SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  472. /*
  473. * One file with associated inode and dcache is very roughly 1K.
  474. * Per default don't use more than 10% of our memory for files.
  475. */
  476. n = (mempages * (PAGE_SIZE / 1024)) / 10;
  477. files_stat.max_files = max_t(unsigned long, n, NR_FILE);
  478. files_defer_init();
  479. lg_lock_init(files_lglock);
  480. percpu_counter_init(&nr_files, 0);
  481. }