inode.c 12 KB

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