inode.c 11 KB

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