inode.c 12 KB

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