inode.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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/file.h>
  14. #include <linux/limits.h>
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/smp_lock.h>
  18. #include <asm/system.h>
  19. #include <asm/uaccess.h>
  20. #include "internal.h"
  21. struct proc_dir_entry *de_get(struct proc_dir_entry *de)
  22. {
  23. if (de)
  24. atomic_inc(&de->count);
  25. return de;
  26. }
  27. /*
  28. * Decrements the use count and checks for deferred deletion.
  29. */
  30. void de_put(struct proc_dir_entry *de)
  31. {
  32. if (de) {
  33. lock_kernel();
  34. if (!atomic_read(&de->count)) {
  35. printk("de_put: entry %s already free!\n", de->name);
  36. unlock_kernel();
  37. return;
  38. }
  39. if (atomic_dec_and_test(&de->count)) {
  40. if (de->deleted) {
  41. printk("de_put: deferred delete of %s\n",
  42. de->name);
  43. free_proc_entry(de);
  44. }
  45. }
  46. unlock_kernel();
  47. }
  48. }
  49. /*
  50. * Decrement the use count of the proc_dir_entry.
  51. */
  52. static void proc_delete_inode(struct inode *inode)
  53. {
  54. struct proc_dir_entry *de;
  55. truncate_inode_pages(&inode->i_data, 0);
  56. /* Stop tracking associated processes */
  57. put_pid(PROC_I(inode)->pid);
  58. /* Let go of any associated proc directory entry */
  59. de = PROC_I(inode)->pde;
  60. if (de) {
  61. if (de->owner)
  62. module_put(de->owner);
  63. de_put(de);
  64. }
  65. clear_inode(inode);
  66. }
  67. struct vfsmount *proc_mnt;
  68. static void proc_read_inode(struct inode * inode)
  69. {
  70. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  71. }
  72. static struct kmem_cache * proc_inode_cachep;
  73. static struct inode *proc_alloc_inode(struct super_block *sb)
  74. {
  75. struct proc_inode *ei;
  76. struct inode *inode;
  77. ei = (struct proc_inode *)kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL);
  78. if (!ei)
  79. return NULL;
  80. ei->pid = NULL;
  81. ei->fd = 0;
  82. ei->op.proc_get_link = NULL;
  83. ei->pde = NULL;
  84. inode = &ei->vfs_inode;
  85. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  86. return inode;
  87. }
  88. static void proc_destroy_inode(struct inode *inode)
  89. {
  90. kmem_cache_free(proc_inode_cachep, PROC_I(inode));
  91. }
  92. static void init_once(void * foo, struct kmem_cache * cachep, unsigned long flags)
  93. {
  94. struct proc_inode *ei = (struct proc_inode *) foo;
  95. inode_init_once(&ei->vfs_inode);
  96. }
  97. int __init proc_init_inodecache(void)
  98. {
  99. proc_inode_cachep = kmem_cache_create("proc_inode_cache",
  100. sizeof(struct proc_inode),
  101. 0, (SLAB_RECLAIM_ACCOUNT|
  102. SLAB_MEM_SPREAD),
  103. init_once, NULL);
  104. if (proc_inode_cachep == NULL)
  105. return -ENOMEM;
  106. return 0;
  107. }
  108. static int proc_remount(struct super_block *sb, int *flags, char *data)
  109. {
  110. *flags |= MS_NODIRATIME;
  111. return 0;
  112. }
  113. static const struct super_operations proc_sops = {
  114. .alloc_inode = proc_alloc_inode,
  115. .destroy_inode = proc_destroy_inode,
  116. .read_inode = proc_read_inode,
  117. .drop_inode = generic_delete_inode,
  118. .delete_inode = proc_delete_inode,
  119. .statfs = simple_statfs,
  120. .remount_fs = proc_remount,
  121. };
  122. static void pde_users_dec(struct proc_dir_entry *pde)
  123. {
  124. spin_lock(&pde->pde_unload_lock);
  125. pde->pde_users--;
  126. if (pde->pde_unload_completion && pde->pde_users == 0)
  127. complete(pde->pde_unload_completion);
  128. spin_unlock(&pde->pde_unload_lock);
  129. }
  130. static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
  131. {
  132. struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
  133. loff_t rv = -EINVAL;
  134. loff_t (*llseek)(struct file *, loff_t, int);
  135. spin_lock(&pde->pde_unload_lock);
  136. /*
  137. * remove_proc_entry() is going to delete PDE (as part of module
  138. * cleanup sequence). No new callers into module allowed.
  139. */
  140. if (!pde->proc_fops) {
  141. spin_unlock(&pde->pde_unload_lock);
  142. return rv;
  143. }
  144. /*
  145. * Bump refcount so that remove_proc_entry will wail for ->llseek to
  146. * complete.
  147. */
  148. pde->pde_users++;
  149. /*
  150. * Save function pointer under lock, to protect against ->proc_fops
  151. * NULL'ifying right after ->pde_unload_lock is dropped.
  152. */
  153. llseek = pde->proc_fops->llseek;
  154. spin_unlock(&pde->pde_unload_lock);
  155. if (!llseek)
  156. llseek = default_llseek;
  157. rv = llseek(file, offset, whence);
  158. pde_users_dec(pde);
  159. return rv;
  160. }
  161. static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  162. {
  163. struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
  164. ssize_t rv = -EIO;
  165. ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
  166. spin_lock(&pde->pde_unload_lock);
  167. if (!pde->proc_fops) {
  168. spin_unlock(&pde->pde_unload_lock);
  169. return rv;
  170. }
  171. pde->pde_users++;
  172. read = pde->proc_fops->read;
  173. spin_unlock(&pde->pde_unload_lock);
  174. if (read)
  175. rv = read(file, buf, count, ppos);
  176. pde_users_dec(pde);
  177. return rv;
  178. }
  179. static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  180. {
  181. struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
  182. ssize_t rv = -EIO;
  183. ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
  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. write = pde->proc_fops->write;
  191. spin_unlock(&pde->pde_unload_lock);
  192. if (write)
  193. rv = write(file, buf, count, ppos);
  194. pde_users_dec(pde);
  195. return rv;
  196. }
  197. static unsigned int proc_reg_poll(struct file *file, struct poll_table_struct *pts)
  198. {
  199. struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
  200. unsigned int rv = 0;
  201. unsigned int (*poll)(struct file *, struct poll_table_struct *);
  202. spin_lock(&pde->pde_unload_lock);
  203. if (!pde->proc_fops) {
  204. spin_unlock(&pde->pde_unload_lock);
  205. return rv;
  206. }
  207. pde->pde_users++;
  208. poll = pde->proc_fops->poll;
  209. spin_unlock(&pde->pde_unload_lock);
  210. if (poll)
  211. rv = poll(file, pts);
  212. pde_users_dec(pde);
  213. return rv;
  214. }
  215. static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  216. {
  217. struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
  218. long rv = -ENOTTY;
  219. long (*unlocked_ioctl)(struct file *, unsigned int, unsigned long);
  220. int (*ioctl)(struct inode *, struct file *, unsigned int, unsigned long);
  221. spin_lock(&pde->pde_unload_lock);
  222. if (!pde->proc_fops) {
  223. spin_unlock(&pde->pde_unload_lock);
  224. return rv;
  225. }
  226. pde->pde_users++;
  227. unlocked_ioctl = pde->proc_fops->unlocked_ioctl;
  228. ioctl = pde->proc_fops->ioctl;
  229. spin_unlock(&pde->pde_unload_lock);
  230. if (unlocked_ioctl) {
  231. rv = unlocked_ioctl(file, cmd, arg);
  232. if (rv == -ENOIOCTLCMD)
  233. rv = -EINVAL;
  234. } else if (ioctl) {
  235. lock_kernel();
  236. rv = ioctl(file->f_path.dentry->d_inode, file, cmd, arg);
  237. unlock_kernel();
  238. }
  239. pde_users_dec(pde);
  240. return rv;
  241. }
  242. #ifdef CONFIG_COMPAT
  243. static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  244. {
  245. struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
  246. long rv = -ENOTTY;
  247. long (*compat_ioctl)(struct file *, unsigned int, unsigned long);
  248. spin_lock(&pde->pde_unload_lock);
  249. if (!pde->proc_fops) {
  250. spin_unlock(&pde->pde_unload_lock);
  251. return rv;
  252. }
  253. pde->pde_users++;
  254. compat_ioctl = pde->proc_fops->compat_ioctl;
  255. spin_unlock(&pde->pde_unload_lock);
  256. if (compat_ioctl)
  257. rv = compat_ioctl(file, cmd, arg);
  258. pde_users_dec(pde);
  259. return rv;
  260. }
  261. #endif
  262. static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
  263. {
  264. struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
  265. int rv = -EIO;
  266. int (*mmap)(struct file *, struct vm_area_struct *);
  267. spin_lock(&pde->pde_unload_lock);
  268. if (!pde->proc_fops) {
  269. spin_unlock(&pde->pde_unload_lock);
  270. return rv;
  271. }
  272. pde->pde_users++;
  273. mmap = pde->proc_fops->mmap;
  274. spin_unlock(&pde->pde_unload_lock);
  275. if (mmap)
  276. rv = mmap(file, vma);
  277. pde_users_dec(pde);
  278. return rv;
  279. }
  280. static int proc_reg_open(struct inode *inode, struct file *file)
  281. {
  282. struct proc_dir_entry *pde = PDE(inode);
  283. int rv = 0;
  284. int (*open)(struct inode *, struct file *);
  285. spin_lock(&pde->pde_unload_lock);
  286. if (!pde->proc_fops) {
  287. spin_unlock(&pde->pde_unload_lock);
  288. return rv;
  289. }
  290. pde->pde_users++;
  291. open = pde->proc_fops->open;
  292. spin_unlock(&pde->pde_unload_lock);
  293. if (open)
  294. rv = open(inode, file);
  295. pde_users_dec(pde);
  296. return rv;
  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. spin_lock(&pde->pde_unload_lock);
  304. if (!pde->proc_fops) {
  305. spin_unlock(&pde->pde_unload_lock);
  306. return rv;
  307. }
  308. pde->pde_users++;
  309. release = pde->proc_fops->release;
  310. spin_unlock(&pde->pde_unload_lock);
  311. if (release)
  312. rv = release(inode, file);
  313. pde_users_dec(pde);
  314. return rv;
  315. }
  316. static const struct file_operations proc_reg_file_ops = {
  317. .llseek = proc_reg_llseek,
  318. .read = proc_reg_read,
  319. .write = proc_reg_write,
  320. .poll = proc_reg_poll,
  321. .unlocked_ioctl = proc_reg_unlocked_ioctl,
  322. #ifdef CONFIG_COMPAT
  323. .compat_ioctl = proc_reg_compat_ioctl,
  324. #endif
  325. .mmap = proc_reg_mmap,
  326. .open = proc_reg_open,
  327. .release = proc_reg_release,
  328. };
  329. struct inode *proc_get_inode(struct super_block *sb, unsigned int ino,
  330. struct proc_dir_entry *de)
  331. {
  332. struct inode * inode;
  333. if (de != NULL && !try_module_get(de->owner))
  334. goto out_mod;
  335. inode = iget(sb, ino);
  336. if (!inode)
  337. goto out_ino;
  338. PROC_I(inode)->fd = 0;
  339. PROC_I(inode)->pde = de;
  340. if (de) {
  341. if (de->mode) {
  342. inode->i_mode = de->mode;
  343. inode->i_uid = de->uid;
  344. inode->i_gid = de->gid;
  345. }
  346. if (de->size)
  347. inode->i_size = de->size;
  348. if (de->nlink)
  349. inode->i_nlink = de->nlink;
  350. if (de->proc_iops)
  351. inode->i_op = de->proc_iops;
  352. if (de->proc_fops) {
  353. if (S_ISREG(inode->i_mode))
  354. inode->i_fop = &proc_reg_file_ops;
  355. else
  356. inode->i_fop = de->proc_fops;
  357. }
  358. }
  359. return inode;
  360. out_ino:
  361. if (de != NULL)
  362. module_put(de->owner);
  363. out_mod:
  364. return NULL;
  365. }
  366. int proc_fill_super(struct super_block *s, void *data, int silent)
  367. {
  368. struct inode * root_inode;
  369. s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
  370. s->s_blocksize = 1024;
  371. s->s_blocksize_bits = 10;
  372. s->s_magic = PROC_SUPER_MAGIC;
  373. s->s_op = &proc_sops;
  374. s->s_time_gran = 1;
  375. de_get(&proc_root);
  376. root_inode = proc_get_inode(s, PROC_ROOT_INO, &proc_root);
  377. if (!root_inode)
  378. goto out_no_root;
  379. root_inode->i_uid = 0;
  380. root_inode->i_gid = 0;
  381. s->s_root = d_alloc_root(root_inode);
  382. if (!s->s_root)
  383. goto out_no_root;
  384. return 0;
  385. out_no_root:
  386. printk("proc_read_super: get root inode failed\n");
  387. iput(root_inode);
  388. de_put(&proc_root);
  389. return -ENOMEM;
  390. }
  391. MODULE_LICENSE("GPL");