inode.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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 <linux/sysctl.h>
  20. #include <asm/system.h>
  21. #include <asm/uaccess.h>
  22. #include "internal.h"
  23. struct proc_dir_entry *de_get(struct proc_dir_entry *de)
  24. {
  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 (!atomic_read(&de->count)) {
  34. printk("de_put: entry %s already free!\n", de->name);
  35. return;
  36. }
  37. if (atomic_dec_and_test(&de->count))
  38. free_proc_entry(de);
  39. }
  40. /*
  41. * Decrement the use count of the proc_dir_entry.
  42. */
  43. static void proc_delete_inode(struct inode *inode)
  44. {
  45. struct proc_dir_entry *de;
  46. truncate_inode_pages(&inode->i_data, 0);
  47. /* Stop tracking associated processes */
  48. put_pid(PROC_I(inode)->pid);
  49. /* Let go of any associated proc directory entry */
  50. de = PROC_I(inode)->pde;
  51. if (de)
  52. de_put(de);
  53. if (PROC_I(inode)->sysctl)
  54. sysctl_head_put(PROC_I(inode)->sysctl);
  55. clear_inode(inode);
  56. }
  57. struct vfsmount *proc_mnt;
  58. static struct kmem_cache * proc_inode_cachep;
  59. static struct inode *proc_alloc_inode(struct super_block *sb)
  60. {
  61. struct proc_inode *ei;
  62. struct inode *inode;
  63. ei = (struct proc_inode *)kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL);
  64. if (!ei)
  65. return NULL;
  66. ei->pid = NULL;
  67. ei->fd = 0;
  68. ei->op.proc_get_link = NULL;
  69. ei->pde = NULL;
  70. ei->sysctl = NULL;
  71. ei->sysctl_entry = NULL;
  72. inode = &ei->vfs_inode;
  73. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  74. return inode;
  75. }
  76. static void proc_destroy_inode(struct inode *inode)
  77. {
  78. kmem_cache_free(proc_inode_cachep, PROC_I(inode));
  79. }
  80. static void init_once(void *foo)
  81. {
  82. struct proc_inode *ei = (struct proc_inode *) foo;
  83. inode_init_once(&ei->vfs_inode);
  84. }
  85. void __init proc_init_inodecache(void)
  86. {
  87. proc_inode_cachep = kmem_cache_create("proc_inode_cache",
  88. sizeof(struct proc_inode),
  89. 0, (SLAB_RECLAIM_ACCOUNT|
  90. SLAB_MEM_SPREAD|SLAB_PANIC),
  91. init_once);
  92. }
  93. static const struct super_operations proc_sops = {
  94. .alloc_inode = proc_alloc_inode,
  95. .destroy_inode = proc_destroy_inode,
  96. .drop_inode = generic_delete_inode,
  97. .delete_inode = proc_delete_inode,
  98. .statfs = simple_statfs,
  99. };
  100. static void __pde_users_dec(struct proc_dir_entry *pde)
  101. {
  102. pde->pde_users--;
  103. if (pde->pde_unload_completion && pde->pde_users == 0)
  104. complete(pde->pde_unload_completion);
  105. }
  106. void pde_users_dec(struct proc_dir_entry *pde)
  107. {
  108. spin_lock(&pde->pde_unload_lock);
  109. __pde_users_dec(pde);
  110. spin_unlock(&pde->pde_unload_lock);
  111. }
  112. static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
  113. {
  114. struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
  115. loff_t rv = -EINVAL;
  116. loff_t (*llseek)(struct file *, loff_t, int);
  117. spin_lock(&pde->pde_unload_lock);
  118. /*
  119. * remove_proc_entry() is going to delete PDE (as part of module
  120. * cleanup sequence). No new callers into module allowed.
  121. */
  122. if (!pde->proc_fops) {
  123. spin_unlock(&pde->pde_unload_lock);
  124. return rv;
  125. }
  126. /*
  127. * Bump refcount so that remove_proc_entry will wail for ->llseek to
  128. * complete.
  129. */
  130. pde->pde_users++;
  131. /*
  132. * Save function pointer under lock, to protect against ->proc_fops
  133. * NULL'ifying right after ->pde_unload_lock is dropped.
  134. */
  135. llseek = pde->proc_fops->llseek;
  136. spin_unlock(&pde->pde_unload_lock);
  137. if (!llseek)
  138. llseek = default_llseek;
  139. rv = llseek(file, offset, whence);
  140. pde_users_dec(pde);
  141. return rv;
  142. }
  143. static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  144. {
  145. struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
  146. ssize_t rv = -EIO;
  147. ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
  148. spin_lock(&pde->pde_unload_lock);
  149. if (!pde->proc_fops) {
  150. spin_unlock(&pde->pde_unload_lock);
  151. return rv;
  152. }
  153. pde->pde_users++;
  154. read = pde->proc_fops->read;
  155. spin_unlock(&pde->pde_unload_lock);
  156. if (read)
  157. rv = read(file, buf, count, ppos);
  158. pde_users_dec(pde);
  159. return rv;
  160. }
  161. static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  162. {
  163. struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
  164. ssize_t rv = -EIO;
  165. ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
  166. spin_lock(&pde->pde_unload_lock);
  167. if (!pde->proc_fops) {
  168. spin_unlock(&pde->pde_unload_lock);
  169. return rv;
  170. }
  171. pde->pde_users++;
  172. write = pde->proc_fops->write;
  173. spin_unlock(&pde->pde_unload_lock);
  174. if (write)
  175. rv = write(file, buf, count, ppos);
  176. pde_users_dec(pde);
  177. return rv;
  178. }
  179. static unsigned int proc_reg_poll(struct file *file, struct poll_table_struct *pts)
  180. {
  181. struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
  182. unsigned int rv = DEFAULT_POLLMASK;
  183. unsigned int (*poll)(struct file *, struct poll_table_struct *);
  184. spin_lock(&pde->pde_unload_lock);
  185. if (!pde->proc_fops) {
  186. spin_unlock(&pde->pde_unload_lock);
  187. return rv;
  188. }
  189. pde->pde_users++;
  190. poll = pde->proc_fops->poll;
  191. spin_unlock(&pde->pde_unload_lock);
  192. if (poll)
  193. rv = poll(file, pts);
  194. pde_users_dec(pde);
  195. return rv;
  196. }
  197. static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  198. {
  199. struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
  200. long rv = -ENOTTY;
  201. long (*unlocked_ioctl)(struct file *, unsigned int, unsigned long);
  202. int (*ioctl)(struct inode *, struct file *, unsigned int, unsigned long);
  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. unlocked_ioctl = pde->proc_fops->unlocked_ioctl;
  210. ioctl = pde->proc_fops->ioctl;
  211. spin_unlock(&pde->pde_unload_lock);
  212. if (unlocked_ioctl) {
  213. rv = unlocked_ioctl(file, cmd, arg);
  214. if (rv == -ENOIOCTLCMD)
  215. rv = -EINVAL;
  216. } else if (ioctl) {
  217. lock_kernel();
  218. rv = ioctl(file->f_path.dentry->d_inode, file, cmd, arg);
  219. unlock_kernel();
  220. }
  221. pde_users_dec(pde);
  222. return rv;
  223. }
  224. #ifdef CONFIG_COMPAT
  225. static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  226. {
  227. struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
  228. long rv = -ENOTTY;
  229. long (*compat_ioctl)(struct file *, unsigned int, unsigned long);
  230. spin_lock(&pde->pde_unload_lock);
  231. if (!pde->proc_fops) {
  232. spin_unlock(&pde->pde_unload_lock);
  233. return rv;
  234. }
  235. pde->pde_users++;
  236. compat_ioctl = pde->proc_fops->compat_ioctl;
  237. spin_unlock(&pde->pde_unload_lock);
  238. if (compat_ioctl)
  239. rv = compat_ioctl(file, cmd, arg);
  240. pde_users_dec(pde);
  241. return rv;
  242. }
  243. #endif
  244. static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
  245. {
  246. struct proc_dir_entry *pde = PDE(file->f_path.dentry->d_inode);
  247. int rv = -EIO;
  248. int (*mmap)(struct file *, struct vm_area_struct *);
  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. mmap = pde->proc_fops->mmap;
  256. spin_unlock(&pde->pde_unload_lock);
  257. if (mmap)
  258. rv = mmap(file, vma);
  259. pde_users_dec(pde);
  260. return rv;
  261. }
  262. static int proc_reg_open(struct inode *inode, struct file *file)
  263. {
  264. struct proc_dir_entry *pde = PDE(inode);
  265. int rv = 0;
  266. int (*open)(struct inode *, struct file *);
  267. int (*release)(struct inode *, struct file *);
  268. struct pde_opener *pdeo;
  269. /*
  270. * What for, you ask? Well, we can have open, rmmod, remove_proc_entry
  271. * sequence. ->release won't be called because ->proc_fops will be
  272. * cleared. Depending on complexity of ->release, consequences vary.
  273. *
  274. * We can't wait for mercy when close will be done for real, it's
  275. * deadlockable: rmmod foo </proc/foo . So, we're going to do ->release
  276. * by hand in remove_proc_entry(). For this, save opener's credentials
  277. * for later.
  278. */
  279. pdeo = kmalloc(sizeof(struct pde_opener), GFP_KERNEL);
  280. if (!pdeo)
  281. return -ENOMEM;
  282. spin_lock(&pde->pde_unload_lock);
  283. if (!pde->proc_fops) {
  284. spin_unlock(&pde->pde_unload_lock);
  285. kfree(pdeo);
  286. return -EINVAL;
  287. }
  288. pde->pde_users++;
  289. open = pde->proc_fops->open;
  290. release = pde->proc_fops->release;
  291. spin_unlock(&pde->pde_unload_lock);
  292. if (open)
  293. rv = open(inode, file);
  294. spin_lock(&pde->pde_unload_lock);
  295. if (rv == 0 && release) {
  296. /* To know what to release. */
  297. pdeo->inode = inode;
  298. pdeo->file = file;
  299. /* Strictly for "too late" ->release in proc_reg_release(). */
  300. pdeo->release = release;
  301. list_add(&pdeo->lh, &pde->pde_openers);
  302. } else
  303. kfree(pdeo);
  304. __pde_users_dec(pde);
  305. spin_unlock(&pde->pde_unload_lock);
  306. return rv;
  307. }
  308. static struct pde_opener *find_pde_opener(struct proc_dir_entry *pde,
  309. struct inode *inode, struct file *file)
  310. {
  311. struct pde_opener *pdeo;
  312. list_for_each_entry(pdeo, &pde->pde_openers, lh) {
  313. if (pdeo->inode == inode && pdeo->file == file)
  314. return pdeo;
  315. }
  316. return NULL;
  317. }
  318. static int proc_reg_release(struct inode *inode, struct file *file)
  319. {
  320. struct proc_dir_entry *pde = PDE(inode);
  321. int rv = 0;
  322. int (*release)(struct inode *, struct file *);
  323. struct pde_opener *pdeo;
  324. spin_lock(&pde->pde_unload_lock);
  325. pdeo = find_pde_opener(pde, inode, file);
  326. if (!pde->proc_fops) {
  327. /*
  328. * Can't simply exit, __fput() will think that everything is OK,
  329. * and move on to freeing struct file. remove_proc_entry() will
  330. * find slacker in opener's list and will try to do non-trivial
  331. * things with struct file. Therefore, remove opener from list.
  332. *
  333. * But if opener is removed from list, who will ->release it?
  334. */
  335. if (pdeo) {
  336. list_del(&pdeo->lh);
  337. spin_unlock(&pde->pde_unload_lock);
  338. rv = pdeo->release(inode, file);
  339. kfree(pdeo);
  340. } else
  341. spin_unlock(&pde->pde_unload_lock);
  342. return rv;
  343. }
  344. pde->pde_users++;
  345. release = pde->proc_fops->release;
  346. if (pdeo) {
  347. list_del(&pdeo->lh);
  348. kfree(pdeo);
  349. }
  350. spin_unlock(&pde->pde_unload_lock);
  351. if (release)
  352. rv = release(inode, file);
  353. pde_users_dec(pde);
  354. return rv;
  355. }
  356. static const struct file_operations proc_reg_file_ops = {
  357. .llseek = proc_reg_llseek,
  358. .read = proc_reg_read,
  359. .write = proc_reg_write,
  360. .poll = proc_reg_poll,
  361. .unlocked_ioctl = proc_reg_unlocked_ioctl,
  362. #ifdef CONFIG_COMPAT
  363. .compat_ioctl = proc_reg_compat_ioctl,
  364. #endif
  365. .mmap = proc_reg_mmap,
  366. .open = proc_reg_open,
  367. .release = proc_reg_release,
  368. };
  369. #ifdef CONFIG_COMPAT
  370. static const struct file_operations proc_reg_file_ops_no_compat = {
  371. .llseek = proc_reg_llseek,
  372. .read = proc_reg_read,
  373. .write = proc_reg_write,
  374. .poll = proc_reg_poll,
  375. .unlocked_ioctl = proc_reg_unlocked_ioctl,
  376. .mmap = proc_reg_mmap,
  377. .open = proc_reg_open,
  378. .release = proc_reg_release,
  379. };
  380. #endif
  381. struct inode *proc_get_inode(struct super_block *sb, unsigned int ino,
  382. struct proc_dir_entry *de)
  383. {
  384. struct inode * inode;
  385. inode = iget_locked(sb, ino);
  386. if (!inode)
  387. return NULL;
  388. if (inode->i_state & I_NEW) {
  389. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  390. PROC_I(inode)->fd = 0;
  391. PROC_I(inode)->pde = de;
  392. if (de->mode) {
  393. inode->i_mode = de->mode;
  394. inode->i_uid = de->uid;
  395. inode->i_gid = de->gid;
  396. }
  397. if (de->size)
  398. inode->i_size = de->size;
  399. if (de->nlink)
  400. inode->i_nlink = de->nlink;
  401. if (de->proc_iops)
  402. inode->i_op = de->proc_iops;
  403. if (de->proc_fops) {
  404. if (S_ISREG(inode->i_mode)) {
  405. #ifdef CONFIG_COMPAT
  406. if (!de->proc_fops->compat_ioctl)
  407. inode->i_fop =
  408. &proc_reg_file_ops_no_compat;
  409. else
  410. #endif
  411. inode->i_fop = &proc_reg_file_ops;
  412. } else {
  413. inode->i_fop = de->proc_fops;
  414. }
  415. }
  416. unlock_new_inode(inode);
  417. } else
  418. de_put(de);
  419. return inode;
  420. }
  421. int proc_fill_super(struct super_block *s)
  422. {
  423. struct inode * root_inode;
  424. s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
  425. s->s_blocksize = 1024;
  426. s->s_blocksize_bits = 10;
  427. s->s_magic = PROC_SUPER_MAGIC;
  428. s->s_op = &proc_sops;
  429. s->s_time_gran = 1;
  430. de_get(&proc_root);
  431. root_inode = proc_get_inode(s, PROC_ROOT_INO, &proc_root);
  432. if (!root_inode)
  433. goto out_no_root;
  434. root_inode->i_uid = 0;
  435. root_inode->i_gid = 0;
  436. s->s_root = d_alloc_root(root_inode);
  437. if (!s->s_root)
  438. goto out_no_root;
  439. return 0;
  440. out_no_root:
  441. printk("proc_read_super: get root inode failed\n");
  442. iput(root_inode);
  443. de_put(&proc_root);
  444. return -ENOMEM;
  445. }