inode.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*
  2. * linux/fs/proc/inode.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/time.h>
  7. #include <linux/proc_fs.h>
  8. #include <linux/kernel.h>
  9. #include <linux/pid_namespace.h>
  10. #include <linux/mm.h>
  11. #include <linux/string.h>
  12. #include <linux/stat.h>
  13. #include <linux/completion.h>
  14. #include <linux/poll.h>
  15. #include <linux/printk.h>
  16. #include <linux/file.h>
  17. #include <linux/limits.h>
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/sysctl.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/slab.h>
  23. #include <linux/mount.h>
  24. #include <linux/magic.h>
  25. #include <asm/uaccess.h>
  26. #include "internal.h"
  27. static void proc_evict_inode(struct inode *inode)
  28. {
  29. struct proc_dir_entry *de;
  30. struct ctl_table_header *head;
  31. truncate_inode_pages_final(&inode->i_data);
  32. clear_inode(inode);
  33. /* Stop tracking associated processes */
  34. put_pid(PROC_I(inode)->pid);
  35. /* Let go of any associated proc directory entry */
  36. de = PDE(inode);
  37. if (de)
  38. pde_put(de);
  39. head = PROC_I(inode)->sysctl;
  40. if (head) {
  41. RCU_INIT_POINTER(PROC_I(inode)->sysctl, NULL);
  42. sysctl_head_put(head);
  43. }
  44. }
  45. static struct kmem_cache * proc_inode_cachep;
  46. static struct inode *proc_alloc_inode(struct super_block *sb)
  47. {
  48. struct proc_inode *ei;
  49. struct inode *inode;
  50. ei = (struct proc_inode *)kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL);
  51. if (!ei)
  52. return NULL;
  53. ei->pid = NULL;
  54. ei->fd = 0;
  55. ei->op.proc_get_link = NULL;
  56. ei->pde = NULL;
  57. ei->sysctl = NULL;
  58. ei->sysctl_entry = NULL;
  59. ei->ns_ops = NULL;
  60. inode = &ei->vfs_inode;
  61. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  62. return inode;
  63. }
  64. static void proc_i_callback(struct rcu_head *head)
  65. {
  66. struct inode *inode = container_of(head, struct inode, i_rcu);
  67. kmem_cache_free(proc_inode_cachep, PROC_I(inode));
  68. }
  69. static void proc_destroy_inode(struct inode *inode)
  70. {
  71. call_rcu(&inode->i_rcu, proc_i_callback);
  72. }
  73. static void init_once(void *foo)
  74. {
  75. struct proc_inode *ei = (struct proc_inode *) foo;
  76. inode_init_once(&ei->vfs_inode);
  77. }
  78. void __init proc_init_inodecache(void)
  79. {
  80. proc_inode_cachep = kmem_cache_create("proc_inode_cache",
  81. sizeof(struct proc_inode),
  82. 0, (SLAB_RECLAIM_ACCOUNT|
  83. SLAB_MEM_SPREAD|SLAB_PANIC),
  84. init_once);
  85. }
  86. static int proc_show_options(struct seq_file *seq, struct dentry *root)
  87. {
  88. struct super_block *sb = root->d_sb;
  89. struct pid_namespace *pid = sb->s_fs_info;
  90. if (!gid_eq(pid->pid_gid, GLOBAL_ROOT_GID))
  91. seq_printf(seq, ",gid=%u", from_kgid_munged(&init_user_ns, pid->pid_gid));
  92. if (pid->hide_pid != 0)
  93. seq_printf(seq, ",hidepid=%u", pid->hide_pid);
  94. return 0;
  95. }
  96. static const struct super_operations proc_sops = {
  97. .alloc_inode = proc_alloc_inode,
  98. .destroy_inode = proc_destroy_inode,
  99. .drop_inode = generic_delete_inode,
  100. .evict_inode = proc_evict_inode,
  101. .statfs = simple_statfs,
  102. .remount_fs = proc_remount,
  103. .show_options = proc_show_options,
  104. };
  105. enum {BIAS = -1U<<31};
  106. static inline int use_pde(struct proc_dir_entry *pde)
  107. {
  108. return atomic_inc_unless_negative(&pde->in_use);
  109. }
  110. static void unuse_pde(struct proc_dir_entry *pde)
  111. {
  112. if (atomic_dec_return(&pde->in_use) == BIAS)
  113. complete(pde->pde_unload_completion);
  114. }
  115. /* pde is locked */
  116. static void close_pdeo(struct proc_dir_entry *pde, struct pde_opener *pdeo)
  117. {
  118. if (pdeo->closing) {
  119. /* somebody else is doing that, just wait */
  120. DECLARE_COMPLETION_ONSTACK(c);
  121. pdeo->c = &c;
  122. spin_unlock(&pde->pde_unload_lock);
  123. wait_for_completion(&c);
  124. spin_lock(&pde->pde_unload_lock);
  125. } else {
  126. struct file *file;
  127. pdeo->closing = 1;
  128. spin_unlock(&pde->pde_unload_lock);
  129. file = pdeo->file;
  130. pde->proc_fops->release(file_inode(file), file);
  131. spin_lock(&pde->pde_unload_lock);
  132. list_del_init(&pdeo->lh);
  133. if (pdeo->c)
  134. complete(pdeo->c);
  135. kfree(pdeo);
  136. }
  137. }
  138. void proc_entry_rundown(struct proc_dir_entry *de)
  139. {
  140. DECLARE_COMPLETION_ONSTACK(c);
  141. /* Wait until all existing callers into module are done. */
  142. de->pde_unload_completion = &c;
  143. if (atomic_add_return(BIAS, &de->in_use) != BIAS)
  144. wait_for_completion(&c);
  145. spin_lock(&de->pde_unload_lock);
  146. while (!list_empty(&de->pde_openers)) {
  147. struct pde_opener *pdeo;
  148. pdeo = list_first_entry(&de->pde_openers, struct pde_opener, lh);
  149. close_pdeo(de, pdeo);
  150. }
  151. spin_unlock(&de->pde_unload_lock);
  152. }
  153. static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
  154. {
  155. struct proc_dir_entry *pde = PDE(file_inode(file));
  156. loff_t rv = -EINVAL;
  157. if (use_pde(pde)) {
  158. loff_t (*llseek)(struct file *, loff_t, int);
  159. llseek = pde->proc_fops->llseek;
  160. if (!llseek)
  161. llseek = default_llseek;
  162. rv = llseek(file, offset, whence);
  163. unuse_pde(pde);
  164. }
  165. return rv;
  166. }
  167. static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  168. {
  169. ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
  170. struct proc_dir_entry *pde = PDE(file_inode(file));
  171. ssize_t rv = -EIO;
  172. if (use_pde(pde)) {
  173. read = pde->proc_fops->read;
  174. if (read)
  175. rv = read(file, buf, count, ppos);
  176. unuse_pde(pde);
  177. }
  178. return rv;
  179. }
  180. static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  181. {
  182. ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
  183. struct proc_dir_entry *pde = PDE(file_inode(file));
  184. ssize_t rv = -EIO;
  185. if (use_pde(pde)) {
  186. write = pde->proc_fops->write;
  187. if (write)
  188. rv = write(file, buf, count, ppos);
  189. unuse_pde(pde);
  190. }
  191. return rv;
  192. }
  193. static unsigned int proc_reg_poll(struct file *file, struct poll_table_struct *pts)
  194. {
  195. struct proc_dir_entry *pde = PDE(file_inode(file));
  196. unsigned int rv = DEFAULT_POLLMASK;
  197. unsigned int (*poll)(struct file *, struct poll_table_struct *);
  198. if (use_pde(pde)) {
  199. poll = pde->proc_fops->poll;
  200. if (poll)
  201. rv = poll(file, pts);
  202. unuse_pde(pde);
  203. }
  204. return rv;
  205. }
  206. static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  207. {
  208. struct proc_dir_entry *pde = PDE(file_inode(file));
  209. long rv = -ENOTTY;
  210. long (*ioctl)(struct file *, unsigned int, unsigned long);
  211. if (use_pde(pde)) {
  212. ioctl = pde->proc_fops->unlocked_ioctl;
  213. if (ioctl)
  214. rv = ioctl(file, cmd, arg);
  215. unuse_pde(pde);
  216. }
  217. return rv;
  218. }
  219. #ifdef CONFIG_COMPAT
  220. static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  221. {
  222. struct proc_dir_entry *pde = PDE(file_inode(file));
  223. long rv = -ENOTTY;
  224. long (*compat_ioctl)(struct file *, unsigned int, unsigned long);
  225. if (use_pde(pde)) {
  226. compat_ioctl = pde->proc_fops->compat_ioctl;
  227. if (compat_ioctl)
  228. rv = compat_ioctl(file, cmd, arg);
  229. unuse_pde(pde);
  230. }
  231. return rv;
  232. }
  233. #endif
  234. static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
  235. {
  236. struct proc_dir_entry *pde = PDE(file_inode(file));
  237. int rv = -EIO;
  238. int (*mmap)(struct file *, struct vm_area_struct *);
  239. if (use_pde(pde)) {
  240. mmap = pde->proc_fops->mmap;
  241. if (mmap)
  242. rv = mmap(file, vma);
  243. unuse_pde(pde);
  244. }
  245. return rv;
  246. }
  247. static unsigned long
  248. proc_reg_get_unmapped_area(struct file *file, unsigned long orig_addr,
  249. unsigned long len, unsigned long pgoff,
  250. unsigned long flags)
  251. {
  252. struct proc_dir_entry *pde = PDE(file_inode(file));
  253. unsigned long rv = -EIO;
  254. if (use_pde(pde)) {
  255. typeof(proc_reg_get_unmapped_area) *get_area;
  256. get_area = pde->proc_fops->get_unmapped_area;
  257. #ifdef CONFIG_MMU
  258. if (!get_area)
  259. get_area = current->mm->get_unmapped_area;
  260. #endif
  261. if (get_area)
  262. rv = get_area(file, orig_addr, len, pgoff, flags);
  263. else
  264. rv = orig_addr;
  265. unuse_pde(pde);
  266. }
  267. return rv;
  268. }
  269. static int proc_reg_open(struct inode *inode, struct file *file)
  270. {
  271. struct proc_dir_entry *pde = PDE(inode);
  272. int rv = 0;
  273. int (*open)(struct inode *, struct file *);
  274. int (*release)(struct inode *, struct file *);
  275. struct pde_opener *pdeo;
  276. /*
  277. * What for, you ask? Well, we can have open, rmmod, remove_proc_entry
  278. * sequence. ->release won't be called because ->proc_fops will be
  279. * cleared. Depending on complexity of ->release, consequences vary.
  280. *
  281. * We can't wait for mercy when close will be done for real, it's
  282. * deadlockable: rmmod foo </proc/foo . So, we're going to do ->release
  283. * by hand in remove_proc_entry(). For this, save opener's credentials
  284. * for later.
  285. */
  286. pdeo = kzalloc(sizeof(struct pde_opener), GFP_KERNEL);
  287. if (!pdeo)
  288. return -ENOMEM;
  289. if (!use_pde(pde)) {
  290. kfree(pdeo);
  291. return -ENOENT;
  292. }
  293. open = pde->proc_fops->open;
  294. release = pde->proc_fops->release;
  295. if (open)
  296. rv = open(inode, file);
  297. if (rv == 0 && release) {
  298. /* To know what to release. */
  299. pdeo->file = file;
  300. /* Strictly for "too late" ->release in proc_reg_release(). */
  301. spin_lock(&pde->pde_unload_lock);
  302. list_add(&pdeo->lh, &pde->pde_openers);
  303. spin_unlock(&pde->pde_unload_lock);
  304. } else
  305. kfree(pdeo);
  306. unuse_pde(pde);
  307. return rv;
  308. }
  309. static int proc_reg_release(struct inode *inode, struct file *file)
  310. {
  311. struct proc_dir_entry *pde = PDE(inode);
  312. struct pde_opener *pdeo;
  313. spin_lock(&pde->pde_unload_lock);
  314. list_for_each_entry(pdeo, &pde->pde_openers, lh) {
  315. if (pdeo->file == file) {
  316. close_pdeo(pde, pdeo);
  317. break;
  318. }
  319. }
  320. spin_unlock(&pde->pde_unload_lock);
  321. return 0;
  322. }
  323. static const struct file_operations proc_reg_file_ops = {
  324. .llseek = proc_reg_llseek,
  325. .read = proc_reg_read,
  326. .write = proc_reg_write,
  327. .poll = proc_reg_poll,
  328. .unlocked_ioctl = proc_reg_unlocked_ioctl,
  329. #ifdef CONFIG_COMPAT
  330. .compat_ioctl = proc_reg_compat_ioctl,
  331. #endif
  332. .mmap = proc_reg_mmap,
  333. .get_unmapped_area = proc_reg_get_unmapped_area,
  334. .open = proc_reg_open,
  335. .release = proc_reg_release,
  336. };
  337. #ifdef CONFIG_COMPAT
  338. static const struct file_operations proc_reg_file_ops_no_compat = {
  339. .llseek = proc_reg_llseek,
  340. .read = proc_reg_read,
  341. .write = proc_reg_write,
  342. .poll = proc_reg_poll,
  343. .unlocked_ioctl = proc_reg_unlocked_ioctl,
  344. .mmap = proc_reg_mmap,
  345. .get_unmapped_area = proc_reg_get_unmapped_area,
  346. .open = proc_reg_open,
  347. .release = proc_reg_release,
  348. };
  349. #endif
  350. static const char *proc_follow_link(struct dentry *dentry, void **cookie)
  351. {
  352. struct proc_dir_entry *pde = PDE(d_inode(dentry));
  353. if (unlikely(!use_pde(pde)))
  354. return ERR_PTR(-EINVAL);
  355. *cookie = pde;
  356. return pde->data;
  357. }
  358. static void proc_put_link(struct inode *unused, void *p)
  359. {
  360. unuse_pde(p);
  361. }
  362. const struct inode_operations proc_link_inode_operations = {
  363. .readlink = generic_readlink,
  364. .follow_link = proc_follow_link,
  365. .put_link = proc_put_link,
  366. };
  367. struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de)
  368. {
  369. struct inode *inode = new_inode_pseudo(sb);
  370. if (inode) {
  371. inode->i_ino = de->low_ino;
  372. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  373. PROC_I(inode)->pde = de;
  374. if (is_empty_pde(de)) {
  375. make_empty_dir_inode(inode);
  376. return inode;
  377. }
  378. if (de->mode) {
  379. inode->i_mode = de->mode;
  380. inode->i_uid = de->uid;
  381. inode->i_gid = de->gid;
  382. }
  383. if (de->size)
  384. inode->i_size = de->size;
  385. if (de->nlink)
  386. set_nlink(inode, de->nlink);
  387. WARN_ON(!de->proc_iops);
  388. inode->i_op = de->proc_iops;
  389. if (de->proc_fops) {
  390. if (S_ISREG(inode->i_mode)) {
  391. #ifdef CONFIG_COMPAT
  392. if (!de->proc_fops->compat_ioctl)
  393. inode->i_fop =
  394. &proc_reg_file_ops_no_compat;
  395. else
  396. #endif
  397. inode->i_fop = &proc_reg_file_ops;
  398. } else {
  399. inode->i_fop = de->proc_fops;
  400. }
  401. }
  402. } else
  403. pde_put(de);
  404. return inode;
  405. }
  406. int proc_fill_super(struct super_block *s)
  407. {
  408. struct inode *root_inode;
  409. int ret;
  410. s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
  411. s->s_blocksize = 1024;
  412. s->s_blocksize_bits = 10;
  413. s->s_magic = PROC_SUPER_MAGIC;
  414. s->s_op = &proc_sops;
  415. s->s_time_gran = 1;
  416. pde_get(&proc_root);
  417. root_inode = proc_get_inode(s, &proc_root);
  418. if (!root_inode) {
  419. pr_err("proc_fill_super: get root inode failed\n");
  420. return -ENOMEM;
  421. }
  422. s->s_root = d_make_root(root_inode);
  423. if (!s->s_root) {
  424. pr_err("proc_fill_super: allocate dentry failed\n");
  425. return -ENOMEM;
  426. }
  427. ret = proc_setup_self(s);
  428. if (ret) {
  429. return ret;
  430. }
  431. return proc_setup_thread_self(s);
  432. }