inode.c 12 KB

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