inode.c 11 KB

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