inode.c 10 KB

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