inode.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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_delete_inode(struct inode *inode)
  25. {
  26. struct proc_dir_entry *de;
  27. truncate_inode_pages(&inode->i_data, 0);
  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. clear_inode(inode);
  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. .delete_inode = proc_delete_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 (*unlocked_ioctl)(struct file *, unsigned int, unsigned long);
  183. int (*ioctl)(struct inode *, struct file *, unsigned int, unsigned long);
  184. spin_lock(&pde->pde_unload_lock);
  185. if (!pde->proc_fops) {
  186. spin_unlock(&pde->pde_unload_lock);
  187. return rv;
  188. }
  189. pde->pde_users++;
  190. unlocked_ioctl = pde->proc_fops->unlocked_ioctl;
  191. ioctl = pde->proc_fops->ioctl;
  192. spin_unlock(&pde->pde_unload_lock);
  193. if (unlocked_ioctl) {
  194. rv = unlocked_ioctl(file, cmd, arg);
  195. if (rv == -ENOIOCTLCMD)
  196. rv = -EINVAL;
  197. } else if (ioctl) {
  198. lock_kernel();
  199. rv = ioctl(file->f_path.dentry->d_inode, file, cmd, arg);
  200. unlock_kernel();
  201. }
  202. pde_users_dec(pde);
  203. return rv;
  204. }
  205. #ifdef CONFIG_COMPAT
  206. static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  207. {
  208. struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
  209. long rv = -ENOTTY;
  210. long (*compat_ioctl)(struct file *, unsigned int, unsigned long);
  211. spin_lock(&pde->pde_unload_lock);
  212. if (!pde->proc_fops) {
  213. spin_unlock(&pde->pde_unload_lock);
  214. return rv;
  215. }
  216. pde->pde_users++;
  217. compat_ioctl = pde->proc_fops->compat_ioctl;
  218. spin_unlock(&pde->pde_unload_lock);
  219. if (compat_ioctl)
  220. rv = compat_ioctl(file, cmd, arg);
  221. pde_users_dec(pde);
  222. return rv;
  223. }
  224. #endif
  225. static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
  226. {
  227. struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
  228. int rv = -EIO;
  229. int (*mmap)(struct file *, struct vm_area_struct *);
  230. spin_lock(&pde->pde_unload_lock);
  231. if (!pde->proc_fops) {
  232. spin_unlock(&pde->pde_unload_lock);
  233. return rv;
  234. }
  235. pde->pde_users++;
  236. mmap = pde->proc_fops->mmap;
  237. spin_unlock(&pde->pde_unload_lock);
  238. if (mmap)
  239. rv = mmap(file, vma);
  240. pde_users_dec(pde);
  241. return rv;
  242. }
  243. static int proc_reg_open(struct inode *inode, struct file *file)
  244. {
  245. struct proc_dir_entry *pde = PDE(inode);
  246. int rv = 0;
  247. int (*open)(struct inode *, struct file *);
  248. int (*release)(struct inode *, struct file *);
  249. struct pde_opener *pdeo;
  250. /*
  251. * What for, you ask? Well, we can have open, rmmod, remove_proc_entry
  252. * sequence. ->release won't be called because ->proc_fops will be
  253. * cleared. Depending on complexity of ->release, consequences vary.
  254. *
  255. * We can't wait for mercy when close will be done for real, it's
  256. * deadlockable: rmmod foo </proc/foo . So, we're going to do ->release
  257. * by hand in remove_proc_entry(). For this, save opener's credentials
  258. * for later.
  259. */
  260. pdeo = kmalloc(sizeof(struct pde_opener), GFP_KERNEL);
  261. if (!pdeo)
  262. return -ENOMEM;
  263. spin_lock(&pde->pde_unload_lock);
  264. if (!pde->proc_fops) {
  265. spin_unlock(&pde->pde_unload_lock);
  266. kfree(pdeo);
  267. return -EINVAL;
  268. }
  269. pde->pde_users++;
  270. open = pde->proc_fops->open;
  271. release = pde->proc_fops->release;
  272. spin_unlock(&pde->pde_unload_lock);
  273. if (open)
  274. rv = open(inode, file);
  275. spin_lock(&pde->pde_unload_lock);
  276. if (rv == 0 && release) {
  277. /* To know what to release. */
  278. pdeo->inode = inode;
  279. pdeo->file = file;
  280. /* Strictly for "too late" ->release in proc_reg_release(). */
  281. pdeo->release = release;
  282. list_add(&pdeo->lh, &pde->pde_openers);
  283. } else
  284. kfree(pdeo);
  285. __pde_users_dec(pde);
  286. spin_unlock(&pde->pde_unload_lock);
  287. return rv;
  288. }
  289. static struct pde_opener *find_pde_opener(struct proc_dir_entry *pde,
  290. struct inode *inode, struct file *file)
  291. {
  292. struct pde_opener *pdeo;
  293. list_for_each_entry(pdeo, &pde->pde_openers, lh) {
  294. if (pdeo->inode == inode && pdeo->file == file)
  295. return pdeo;
  296. }
  297. return NULL;
  298. }
  299. static int proc_reg_release(struct inode *inode, struct file *file)
  300. {
  301. struct proc_dir_entry *pde = PDE(inode);
  302. int rv = 0;
  303. int (*release)(struct inode *, struct file *);
  304. struct pde_opener *pdeo;
  305. spin_lock(&pde->pde_unload_lock);
  306. pdeo = find_pde_opener(pde, inode, file);
  307. if (!pde->proc_fops) {
  308. /*
  309. * Can't simply exit, __fput() will think that everything is OK,
  310. * and move on to freeing struct file. remove_proc_entry() will
  311. * find slacker in opener's list and will try to do non-trivial
  312. * things with struct file. Therefore, remove opener from list.
  313. *
  314. * But if opener is removed from list, who will ->release it?
  315. */
  316. if (pdeo) {
  317. list_del(&pdeo->lh);
  318. spin_unlock(&pde->pde_unload_lock);
  319. rv = pdeo->release(inode, file);
  320. kfree(pdeo);
  321. } else
  322. spin_unlock(&pde->pde_unload_lock);
  323. return rv;
  324. }
  325. pde->pde_users++;
  326. release = pde->proc_fops->release;
  327. if (pdeo) {
  328. list_del(&pdeo->lh);
  329. kfree(pdeo);
  330. }
  331. spin_unlock(&pde->pde_unload_lock);
  332. if (release)
  333. rv = release(inode, file);
  334. pde_users_dec(pde);
  335. return rv;
  336. }
  337. static const struct file_operations proc_reg_file_ops = {
  338. .llseek = proc_reg_llseek,
  339. .read = proc_reg_read,
  340. .write = proc_reg_write,
  341. .poll = proc_reg_poll,
  342. .unlocked_ioctl = proc_reg_unlocked_ioctl,
  343. #ifdef CONFIG_COMPAT
  344. .compat_ioctl = proc_reg_compat_ioctl,
  345. #endif
  346. .mmap = proc_reg_mmap,
  347. .open = proc_reg_open,
  348. .release = proc_reg_release,
  349. };
  350. #ifdef CONFIG_COMPAT
  351. static const struct file_operations proc_reg_file_ops_no_compat = {
  352. .llseek = proc_reg_llseek,
  353. .read = proc_reg_read,
  354. .write = proc_reg_write,
  355. .poll = proc_reg_poll,
  356. .unlocked_ioctl = proc_reg_unlocked_ioctl,
  357. .mmap = proc_reg_mmap,
  358. .open = proc_reg_open,
  359. .release = proc_reg_release,
  360. };
  361. #endif
  362. struct inode *proc_get_inode(struct super_block *sb, unsigned int ino,
  363. struct proc_dir_entry *de)
  364. {
  365. struct inode * inode;
  366. inode = iget_locked(sb, ino);
  367. if (!inode)
  368. return NULL;
  369. if (inode->i_state & I_NEW) {
  370. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  371. PROC_I(inode)->fd = 0;
  372. PROC_I(inode)->pde = de;
  373. if (de->mode) {
  374. inode->i_mode = de->mode;
  375. inode->i_uid = de->uid;
  376. inode->i_gid = de->gid;
  377. }
  378. if (de->size)
  379. inode->i_size = de->size;
  380. if (de->nlink)
  381. inode->i_nlink = de->nlink;
  382. if (de->proc_iops)
  383. inode->i_op = de->proc_iops;
  384. if (de->proc_fops) {
  385. if (S_ISREG(inode->i_mode)) {
  386. #ifdef CONFIG_COMPAT
  387. if (!de->proc_fops->compat_ioctl)
  388. inode->i_fop =
  389. &proc_reg_file_ops_no_compat;
  390. else
  391. #endif
  392. inode->i_fop = &proc_reg_file_ops;
  393. } else {
  394. inode->i_fop = de->proc_fops;
  395. }
  396. }
  397. unlock_new_inode(inode);
  398. } else
  399. pde_put(de);
  400. return inode;
  401. }
  402. int proc_fill_super(struct super_block *s)
  403. {
  404. struct inode * root_inode;
  405. s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
  406. s->s_blocksize = 1024;
  407. s->s_blocksize_bits = 10;
  408. s->s_magic = PROC_SUPER_MAGIC;
  409. s->s_op = &proc_sops;
  410. s->s_time_gran = 1;
  411. pde_get(&proc_root);
  412. root_inode = proc_get_inode(s, PROC_ROOT_INO, &proc_root);
  413. if (!root_inode)
  414. goto out_no_root;
  415. root_inode->i_uid = 0;
  416. root_inode->i_gid = 0;
  417. s->s_root = d_alloc_root(root_inode);
  418. if (!s->s_root)
  419. goto out_no_root;
  420. return 0;
  421. out_no_root:
  422. printk("proc_read_super: get root inode failed\n");
  423. iput(root_inode);
  424. pde_put(&proc_root);
  425. return -ENOMEM;
  426. }