inode.c 11 KB

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