inode.c 12 KB

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