inode.c 12 KB

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