file_table.c 12 KB

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