inode.c 10 KB

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