inode.c 10 KB

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