file_table.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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/percpu_counter.h>
  23. #include <asm/atomic.h>
  24. /* sysctl tunables... */
  25. struct files_stat_struct files_stat = {
  26. .max_files = NR_FILE
  27. };
  28. /* public. Not pretty! */
  29. __cacheline_aligned_in_smp DEFINE_SPINLOCK(files_lock);
  30. /* SLAB cache for file structures */
  31. static struct kmem_cache *filp_cachep __read_mostly;
  32. static struct percpu_counter nr_files __cacheline_aligned_in_smp;
  33. static inline void file_free_rcu(struct rcu_head *head)
  34. {
  35. struct file *f = container_of(head, struct file, f_u.fu_rcuhead);
  36. put_cred(f->f_cred);
  37. kmem_cache_free(filp_cachep, f);
  38. }
  39. static inline void file_free(struct file *f)
  40. {
  41. percpu_counter_dec(&nr_files);
  42. file_check_state(f);
  43. call_rcu(&f->f_u.fu_rcuhead, file_free_rcu);
  44. }
  45. /*
  46. * Return the total number of open files in the system
  47. */
  48. static int get_nr_files(void)
  49. {
  50. return percpu_counter_read_positive(&nr_files);
  51. }
  52. /*
  53. * Return the maximum number of open files in the system
  54. */
  55. int get_max_files(void)
  56. {
  57. return files_stat.max_files;
  58. }
  59. EXPORT_SYMBOL_GPL(get_max_files);
  60. /*
  61. * Handle nr_files sysctl
  62. */
  63. #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
  64. int proc_nr_files(ctl_table *table, int write,
  65. void __user *buffer, size_t *lenp, loff_t *ppos)
  66. {
  67. files_stat.nr_files = get_nr_files();
  68. return proc_dointvec(table, write, buffer, lenp, ppos);
  69. }
  70. #else
  71. int proc_nr_files(ctl_table *table, int write,
  72. void __user *buffer, size_t *lenp, loff_t *ppos)
  73. {
  74. return -ENOSYS;
  75. }
  76. #endif
  77. /* Find an unused file structure and return a pointer to it.
  78. * Returns NULL, if there are no more free file structures or
  79. * we run out of memory.
  80. *
  81. * Be very careful using this. You are responsible for
  82. * getting write access to any mount that you might assign
  83. * to this filp, if it is opened for write. If this is not
  84. * done, you will imbalance int the mount's writer count
  85. * and a warning at __fput() time.
  86. */
  87. struct file *get_empty_filp(void)
  88. {
  89. const struct cred *cred = current_cred();
  90. static int old_max;
  91. struct file * f;
  92. /*
  93. * Privileged users can go above max_files
  94. */
  95. if (get_nr_files() >= files_stat.max_files && !capable(CAP_SYS_ADMIN)) {
  96. /*
  97. * percpu_counters are inaccurate. Do an expensive check before
  98. * we go and fail.
  99. */
  100. if (percpu_counter_sum_positive(&nr_files) >= files_stat.max_files)
  101. goto over;
  102. }
  103. f = kmem_cache_zalloc(filp_cachep, GFP_KERNEL);
  104. if (f == NULL)
  105. goto fail;
  106. percpu_counter_inc(&nr_files);
  107. if (security_file_alloc(f))
  108. goto fail_sec;
  109. INIT_LIST_HEAD(&f->f_u.fu_list);
  110. atomic_long_set(&f->f_count, 1);
  111. rwlock_init(&f->f_owner.lock);
  112. f->f_cred = get_cred(cred);
  113. spin_lock_init(&f->f_lock);
  114. eventpoll_init_file(f);
  115. /* f->f_version: 0 */
  116. return f;
  117. over:
  118. /* Ran out of filps - report that */
  119. if (get_nr_files() > old_max) {
  120. printk(KERN_INFO "VFS: file-max limit %d reached\n",
  121. get_max_files());
  122. old_max = get_nr_files();
  123. }
  124. goto fail;
  125. fail_sec:
  126. file_free(f);
  127. fail:
  128. return NULL;
  129. }
  130. /**
  131. * alloc_file - allocate and initialize a 'struct file'
  132. * @mnt: the vfsmount on which the file will reside
  133. * @dentry: the dentry representing the new file
  134. * @mode: the mode with which the new file will be opened
  135. * @fop: the 'struct file_operations' for the new file
  136. *
  137. * Use this instead of get_empty_filp() to get a new
  138. * 'struct file'. Do so because of the same initialization
  139. * pitfalls reasons listed for init_file(). This is a
  140. * preferred interface to using init_file().
  141. *
  142. * If all the callers of init_file() are eliminated, its
  143. * code should be moved into this function.
  144. */
  145. struct file *alloc_file(struct path *path, fmode_t mode,
  146. const struct file_operations *fop)
  147. {
  148. struct file *file;
  149. file = get_empty_filp();
  150. if (!file)
  151. return NULL;
  152. file->f_path = *path;
  153. file->f_mapping = path->dentry->d_inode->i_mapping;
  154. file->f_mode = mode;
  155. file->f_op = fop;
  156. /*
  157. * These mounts don't really matter in practice
  158. * for r/o bind mounts. They aren't userspace-
  159. * visible. We do this for consistency, and so
  160. * that we can do debugging checks at __fput()
  161. */
  162. if ((mode & FMODE_WRITE) && !special_file(path->dentry->d_inode->i_mode)) {
  163. int error = 0;
  164. file_take_write(file);
  165. error = mnt_clone_write(path->mnt);
  166. WARN_ON(error);
  167. }
  168. return file;
  169. }
  170. void fput(struct file *file)
  171. {
  172. if (atomic_long_dec_and_test(&file->f_count))
  173. __fput(file);
  174. }
  175. EXPORT_SYMBOL(fput);
  176. /**
  177. * drop_file_write_access - give up ability to write to a file
  178. * @file: the file to which we will stop writing
  179. *
  180. * This is a central place which will give up the ability
  181. * to write to @file, along with access to write through
  182. * its vfsmount.
  183. */
  184. void drop_file_write_access(struct file *file)
  185. {
  186. struct vfsmount *mnt = file->f_path.mnt;
  187. struct dentry *dentry = file->f_path.dentry;
  188. struct inode *inode = dentry->d_inode;
  189. put_write_access(inode);
  190. if (special_file(inode->i_mode))
  191. return;
  192. if (file_check_writeable(file) != 0)
  193. return;
  194. mnt_drop_write(mnt);
  195. file_release_write(file);
  196. }
  197. EXPORT_SYMBOL_GPL(drop_file_write_access);
  198. /* __fput is called from task context when aio completion releases the last
  199. * last use of a struct file *. Do not use otherwise.
  200. */
  201. void __fput(struct file *file)
  202. {
  203. struct dentry *dentry = file->f_path.dentry;
  204. struct vfsmount *mnt = file->f_path.mnt;
  205. struct inode *inode = dentry->d_inode;
  206. might_sleep();
  207. fsnotify_close(file);
  208. /*
  209. * The function eventpoll_release() should be the first called
  210. * in the file cleanup chain.
  211. */
  212. eventpoll_release(file);
  213. locks_remove_flock(file);
  214. if (unlikely(file->f_flags & FASYNC)) {
  215. if (file->f_op && file->f_op->fasync)
  216. file->f_op->fasync(-1, file, 0);
  217. }
  218. if (file->f_op && file->f_op->release)
  219. file->f_op->release(inode, file);
  220. security_file_free(file);
  221. if (unlikely(S_ISCHR(inode->i_mode) && inode->i_cdev != NULL))
  222. cdev_put(inode->i_cdev);
  223. fops_put(file->f_op);
  224. put_pid(file->f_owner.pid);
  225. file_kill(file);
  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. struct file *fget(unsigned int fd)
  235. {
  236. struct file *file;
  237. struct files_struct *files = current->files;
  238. rcu_read_lock();
  239. file = fcheck_files(files, fd);
  240. if (file) {
  241. if (!atomic_long_inc_not_zero(&file->f_count)) {
  242. /* File object ref couldn't be taken */
  243. rcu_read_unlock();
  244. return NULL;
  245. }
  246. }
  247. rcu_read_unlock();
  248. return file;
  249. }
  250. EXPORT_SYMBOL(fget);
  251. /*
  252. * Lightweight file lookup - no refcnt increment if fd table isn't shared.
  253. * You can use this only if it is guranteed that the current task already
  254. * holds a refcnt to that file. That check has to be done at fget() only
  255. * and a flag is returned to be passed to the corresponding fput_light().
  256. * There must not be a cloning between an fget_light/fput_light pair.
  257. */
  258. struct file *fget_light(unsigned int fd, int *fput_needed)
  259. {
  260. struct file *file;
  261. struct files_struct *files = current->files;
  262. *fput_needed = 0;
  263. if (likely((atomic_read(&files->count) == 1))) {
  264. file = fcheck_files(files, fd);
  265. } else {
  266. rcu_read_lock();
  267. file = fcheck_files(files, fd);
  268. if (file) {
  269. if (atomic_long_inc_not_zero(&file->f_count))
  270. *fput_needed = 1;
  271. else
  272. /* Didn't get the reference, someone's freed */
  273. file = NULL;
  274. }
  275. rcu_read_unlock();
  276. }
  277. return file;
  278. }
  279. void put_filp(struct file *file)
  280. {
  281. if (atomic_long_dec_and_test(&file->f_count)) {
  282. security_file_free(file);
  283. file_kill(file);
  284. file_free(file);
  285. }
  286. }
  287. void file_move(struct file *file, struct list_head *list)
  288. {
  289. if (!list)
  290. return;
  291. file_list_lock();
  292. list_move(&file->f_u.fu_list, list);
  293. file_list_unlock();
  294. }
  295. void file_kill(struct file *file)
  296. {
  297. if (!list_empty(&file->f_u.fu_list)) {
  298. file_list_lock();
  299. list_del_init(&file->f_u.fu_list);
  300. file_list_unlock();
  301. }
  302. }
  303. int fs_may_remount_ro(struct super_block *sb)
  304. {
  305. struct file *file;
  306. /* Check that no files are currently opened for writing. */
  307. file_list_lock();
  308. list_for_each_entry(file, &sb->s_files, f_u.fu_list) {
  309. struct inode *inode = file->f_path.dentry->d_inode;
  310. /* File with pending delete? */
  311. if (inode->i_nlink == 0)
  312. goto too_bad;
  313. /* Writeable file? */
  314. if (S_ISREG(inode->i_mode) && (file->f_mode & FMODE_WRITE))
  315. goto too_bad;
  316. }
  317. file_list_unlock();
  318. return 1; /* Tis' cool bro. */
  319. too_bad:
  320. file_list_unlock();
  321. return 0;
  322. }
  323. /**
  324. * mark_files_ro - mark all files read-only
  325. * @sb: superblock in question
  326. *
  327. * All files are marked read-only. We don't care about pending
  328. * delete files so this should be used in 'force' mode only.
  329. */
  330. void mark_files_ro(struct super_block *sb)
  331. {
  332. struct file *f;
  333. retry:
  334. file_list_lock();
  335. list_for_each_entry(f, &sb->s_files, f_u.fu_list) {
  336. struct vfsmount *mnt;
  337. if (!S_ISREG(f->f_path.dentry->d_inode->i_mode))
  338. continue;
  339. if (!file_count(f))
  340. continue;
  341. if (!(f->f_mode & FMODE_WRITE))
  342. continue;
  343. f->f_mode &= ~FMODE_WRITE;
  344. if (file_check_writeable(f) != 0)
  345. continue;
  346. file_release_write(f);
  347. mnt = mntget(f->f_path.mnt);
  348. file_list_unlock();
  349. /*
  350. * This can sleep, so we can't hold
  351. * the file_list_lock() spinlock.
  352. */
  353. mnt_drop_write(mnt);
  354. mntput(mnt);
  355. goto retry;
  356. }
  357. file_list_unlock();
  358. }
  359. void __init files_init(unsigned long mempages)
  360. {
  361. int n;
  362. filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
  363. SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  364. /*
  365. * One file with associated inode and dcache is very roughly 1K.
  366. * Per default don't use more than 10% of our memory for files.
  367. */
  368. n = (mempages * (PAGE_SIZE / 1024)) / 10;
  369. files_stat.max_files = n;
  370. if (files_stat.max_files < NR_FILE)
  371. files_stat.max_files = NR_FILE;
  372. files_defer_init();
  373. percpu_counter_init(&nr_files, 0);
  374. }