inode.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. return inode;
  62. }
  63. static void proc_i_callback(struct rcu_head *head)
  64. {
  65. struct inode *inode = container_of(head, struct inode, i_rcu);
  66. kmem_cache_free(proc_inode_cachep, PROC_I(inode));
  67. }
  68. static void proc_destroy_inode(struct inode *inode)
  69. {
  70. call_rcu(&inode->i_rcu, proc_i_callback);
  71. }
  72. static void init_once(void *foo)
  73. {
  74. struct proc_inode *ei = (struct proc_inode *) foo;
  75. inode_init_once(&ei->vfs_inode);
  76. }
  77. void __init proc_init_inodecache(void)
  78. {
  79. proc_inode_cachep = kmem_cache_create("proc_inode_cache",
  80. sizeof(struct proc_inode),
  81. 0, (SLAB_RECLAIM_ACCOUNT|
  82. SLAB_MEM_SPREAD|SLAB_ACCOUNT|
  83. 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 void proc_put_link(void *p)
  351. {
  352. unuse_pde(p);
  353. }
  354. static const char *proc_get_link(struct dentry *dentry,
  355. struct inode *inode,
  356. struct delayed_call *done)
  357. {
  358. struct proc_dir_entry *pde = PDE(inode);
  359. if (unlikely(!use_pde(pde)))
  360. return ERR_PTR(-EINVAL);
  361. set_delayed_call(done, proc_put_link, pde);
  362. return pde->data;
  363. }
  364. const struct inode_operations proc_link_inode_operations = {
  365. .readlink = generic_readlink,
  366. .get_link = proc_get_link,
  367. };
  368. struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de)
  369. {
  370. struct inode *inode = new_inode_pseudo(sb);
  371. if (inode) {
  372. inode->i_ino = de->low_ino;
  373. inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
  374. PROC_I(inode)->pde = de;
  375. if (is_empty_pde(de)) {
  376. make_empty_dir_inode(inode);
  377. return inode;
  378. }
  379. if (de->mode) {
  380. inode->i_mode = de->mode;
  381. inode->i_uid = de->uid;
  382. inode->i_gid = de->gid;
  383. }
  384. if (de->size)
  385. inode->i_size = de->size;
  386. if (de->nlink)
  387. set_nlink(inode, de->nlink);
  388. WARN_ON(!de->proc_iops);
  389. inode->i_op = de->proc_iops;
  390. if (de->proc_fops) {
  391. if (S_ISREG(inode->i_mode)) {
  392. #ifdef CONFIG_COMPAT
  393. if (!de->proc_fops->compat_ioctl)
  394. inode->i_fop =
  395. &proc_reg_file_ops_no_compat;
  396. else
  397. #endif
  398. inode->i_fop = &proc_reg_file_ops;
  399. } else {
  400. inode->i_fop = de->proc_fops;
  401. }
  402. }
  403. } else
  404. pde_put(de);
  405. return inode;
  406. }
  407. int proc_fill_super(struct super_block *s, void *data, int silent)
  408. {
  409. struct pid_namespace *ns = get_pid_ns(s->s_fs_info);
  410. struct inode *root_inode;
  411. int ret;
  412. if (!proc_parse_options(data, ns))
  413. return -EINVAL;
  414. /* User space would break if executables or devices appear on proc */
  415. s->s_iflags |= SB_I_USERNS_VISIBLE | SB_I_NOEXEC | SB_I_NODEV;
  416. s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
  417. s->s_blocksize = 1024;
  418. s->s_blocksize_bits = 10;
  419. s->s_magic = PROC_SUPER_MAGIC;
  420. s->s_op = &proc_sops;
  421. s->s_time_gran = 1;
  422. /*
  423. * procfs isn't actually a stacking filesystem; however, there is
  424. * too much magic going on inside it to permit stacking things on
  425. * top of it
  426. */
  427. s->s_stack_depth = FILESYSTEM_MAX_STACK_DEPTH;
  428. pde_get(&proc_root);
  429. root_inode = proc_get_inode(s, &proc_root);
  430. if (!root_inode) {
  431. pr_err("proc_fill_super: get root inode failed\n");
  432. return -ENOMEM;
  433. }
  434. s->s_root = d_make_root(root_inode);
  435. if (!s->s_root) {
  436. pr_err("proc_fill_super: allocate dentry failed\n");
  437. return -ENOMEM;
  438. }
  439. ret = proc_setup_self(s);
  440. if (ret) {
  441. return ret;
  442. }
  443. return proc_setup_thread_self(s);
  444. }