inode.c 11 KB

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