file_table.c 13 KB

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