file_table.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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 int 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. int 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_dointvec(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 int 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. if (security_file_alloc(f))
  112. goto fail_sec;
  113. INIT_LIST_HEAD(&f->f_u.fu_list);
  114. atomic_long_set(&f->f_count, 1);
  115. rwlock_init(&f->f_owner.lock);
  116. f->f_cred = get_cred(cred);
  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. printk(KERN_INFO "VFS: file-max limit %d reached\n",
  125. 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. ima_counts_get(file);
  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. cdev_put(inode->i_cdev);
  221. fops_put(file->f_op);
  222. put_pid(file->f_owner.pid);
  223. file_sb_list_del(file);
  224. if (file->f_mode & FMODE_WRITE)
  225. drop_file_write_access(file);
  226. file->f_path.dentry = NULL;
  227. file->f_path.mnt = NULL;
  228. file_free(file);
  229. dput(dentry);
  230. mntput(mnt);
  231. }
  232. void fput(struct file *file)
  233. {
  234. if (atomic_long_dec_and_test(&file->f_count))
  235. __fput(file);
  236. }
  237. EXPORT_SYMBOL(fput);
  238. struct file *fget(unsigned int fd)
  239. {
  240. struct file *file;
  241. struct files_struct *files = current->files;
  242. rcu_read_lock();
  243. file = fcheck_files(files, fd);
  244. if (file) {
  245. if (!atomic_long_inc_not_zero(&file->f_count)) {
  246. /* File object ref couldn't be taken */
  247. rcu_read_unlock();
  248. return NULL;
  249. }
  250. }
  251. rcu_read_unlock();
  252. return file;
  253. }
  254. EXPORT_SYMBOL(fget);
  255. /*
  256. * Lightweight file lookup - no refcnt increment if fd table isn't shared.
  257. *
  258. * You can use this instead of fget if you satisfy all of the following
  259. * conditions:
  260. * 1) You must call fput_light before exiting the syscall and returning control
  261. * to userspace (i.e. you cannot remember the returned struct file * after
  262. * returning to userspace).
  263. * 2) You must not call filp_close on the returned struct file * in between
  264. * calls to fget_light and fput_light.
  265. * 3) You must not clone the current task in between the calls to fget_light
  266. * and fput_light.
  267. *
  268. * The fput_needed flag returned by fget_light should be passed to the
  269. * corresponding fput_light.
  270. */
  271. struct file *fget_light(unsigned int fd, int *fput_needed)
  272. {
  273. struct file *file;
  274. struct files_struct *files = current->files;
  275. *fput_needed = 0;
  276. if (likely((atomic_read(&files->count) == 1))) {
  277. file = fcheck_files(files, fd);
  278. } else {
  279. rcu_read_lock();
  280. file = fcheck_files(files, fd);
  281. if (file) {
  282. if (atomic_long_inc_not_zero(&file->f_count))
  283. *fput_needed = 1;
  284. else
  285. /* Didn't get the reference, someone's freed */
  286. file = NULL;
  287. }
  288. rcu_read_unlock();
  289. }
  290. return file;
  291. }
  292. void put_filp(struct file *file)
  293. {
  294. if (atomic_long_dec_and_test(&file->f_count)) {
  295. security_file_free(file);
  296. file_sb_list_del(file);
  297. file_free(file);
  298. }
  299. }
  300. static inline int file_list_cpu(struct file *file)
  301. {
  302. #ifdef CONFIG_SMP
  303. return file->f_sb_list_cpu;
  304. #else
  305. return smp_processor_id();
  306. #endif
  307. }
  308. /* helper for file_sb_list_add to reduce ifdefs */
  309. static inline void __file_sb_list_add(struct file *file, struct super_block *sb)
  310. {
  311. struct list_head *list;
  312. #ifdef CONFIG_SMP
  313. int cpu;
  314. cpu = smp_processor_id();
  315. file->f_sb_list_cpu = cpu;
  316. list = per_cpu_ptr(sb->s_files, cpu);
  317. #else
  318. list = &sb->s_files;
  319. #endif
  320. list_add(&file->f_u.fu_list, list);
  321. }
  322. /**
  323. * file_sb_list_add - add a file to the sb's file list
  324. * @file: file to add
  325. * @sb: sb to add it to
  326. *
  327. * Use this function to associate a file with the superblock of the inode it
  328. * refers to.
  329. */
  330. void file_sb_list_add(struct file *file, struct super_block *sb)
  331. {
  332. lg_local_lock(files_lglock);
  333. __file_sb_list_add(file, sb);
  334. lg_local_unlock(files_lglock);
  335. }
  336. /**
  337. * file_sb_list_del - remove a file from the sb's file list
  338. * @file: file to remove
  339. * @sb: sb to remove it from
  340. *
  341. * Use this function to remove a file from its superblock.
  342. */
  343. void file_sb_list_del(struct file *file)
  344. {
  345. if (!list_empty(&file->f_u.fu_list)) {
  346. lg_local_lock_cpu(files_lglock, file_list_cpu(file));
  347. list_del_init(&file->f_u.fu_list);
  348. lg_local_unlock_cpu(files_lglock, file_list_cpu(file));
  349. }
  350. }
  351. #ifdef CONFIG_SMP
  352. /*
  353. * These macros iterate all files on all CPUs for a given superblock.
  354. * files_lglock must be held globally.
  355. */
  356. #define do_file_list_for_each_entry(__sb, __file) \
  357. { \
  358. int i; \
  359. for_each_possible_cpu(i) { \
  360. struct list_head *list; \
  361. list = per_cpu_ptr((__sb)->s_files, i); \
  362. list_for_each_entry((__file), list, f_u.fu_list)
  363. #define while_file_list_for_each_entry \
  364. } \
  365. }
  366. #else
  367. #define do_file_list_for_each_entry(__sb, __file) \
  368. { \
  369. struct list_head *list; \
  370. list = &(sb)->s_files; \
  371. list_for_each_entry((__file), list, f_u.fu_list)
  372. #define while_file_list_for_each_entry \
  373. }
  374. #endif
  375. int fs_may_remount_ro(struct super_block *sb)
  376. {
  377. struct file *file;
  378. /* Check that no files are currently opened for writing. */
  379. lg_global_lock(files_lglock);
  380. do_file_list_for_each_entry(sb, file) {
  381. struct inode *inode = file->f_path.dentry->d_inode;
  382. /* File with pending delete? */
  383. if (inode->i_nlink == 0)
  384. goto too_bad;
  385. /* Writeable file? */
  386. if (S_ISREG(inode->i_mode) && (file->f_mode & FMODE_WRITE))
  387. goto too_bad;
  388. } while_file_list_for_each_entry;
  389. lg_global_unlock(files_lglock);
  390. return 1; /* Tis' cool bro. */
  391. too_bad:
  392. lg_global_unlock(files_lglock);
  393. return 0;
  394. }
  395. /**
  396. * mark_files_ro - mark all files read-only
  397. * @sb: superblock in question
  398. *
  399. * All files are marked read-only. We don't care about pending
  400. * delete files so this should be used in 'force' mode only.
  401. */
  402. void mark_files_ro(struct super_block *sb)
  403. {
  404. struct file *f;
  405. retry:
  406. lg_global_lock(files_lglock);
  407. do_file_list_for_each_entry(sb, f) {
  408. struct vfsmount *mnt;
  409. if (!S_ISREG(f->f_path.dentry->d_inode->i_mode))
  410. continue;
  411. if (!file_count(f))
  412. continue;
  413. if (!(f->f_mode & FMODE_WRITE))
  414. continue;
  415. spin_lock(&f->f_lock);
  416. f->f_mode &= ~FMODE_WRITE;
  417. spin_unlock(&f->f_lock);
  418. if (file_check_writeable(f) != 0)
  419. continue;
  420. file_release_write(f);
  421. mnt = mntget(f->f_path.mnt);
  422. /* This can sleep, so we can't hold the spinlock. */
  423. lg_global_unlock(files_lglock);
  424. mnt_drop_write(mnt);
  425. mntput(mnt);
  426. goto retry;
  427. } while_file_list_for_each_entry;
  428. lg_global_unlock(files_lglock);
  429. }
  430. void __init files_init(unsigned long mempages)
  431. {
  432. int n;
  433. filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
  434. SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  435. /*
  436. * One file with associated inode and dcache is very roughly 1K.
  437. * Per default don't use more than 10% of our memory for files.
  438. */
  439. n = (mempages * (PAGE_SIZE / 1024)) / 10;
  440. files_stat.max_files = n;
  441. if (files_stat.max_files < NR_FILE)
  442. files_stat.max_files = NR_FILE;
  443. files_defer_init();
  444. lg_lock_init(files_lglock);
  445. percpu_counter_init(&nr_files, 0);
  446. }