inode.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. lock_kernel();
  34. if (!atomic_read(&de->count)) {
  35. printk("de_put: entry %s already free!\n", de->name);
  36. unlock_kernel();
  37. return;
  38. }
  39. if (atomic_dec_and_test(&de->count))
  40. free_proc_entry(de);
  41. unlock_kernel();
  42. }
  43. /*
  44. * Decrement the use count of the proc_dir_entry.
  45. */
  46. static void proc_delete_inode(struct inode *inode)
  47. {
  48. struct proc_dir_entry *de;
  49. truncate_inode_pages(&inode->i_data, 0);
  50. /* Stop tracking associated processes */
  51. put_pid(PROC_I(inode)->pid);
  52. /* Let go of any associated proc directory entry */
  53. de = PROC_I(inode)->pde;
  54. if (de) {
  55. if (de->owner)
  56. module_put(de->owner);
  57. de_put(de);
  58. }
  59. if (PROC_I(inode)->sysctl)
  60. sysctl_head_put(PROC_I(inode)->sysctl);
  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. ei->sysctl = NULL;
  77. ei->sysctl_entry = NULL;
  78. inode = &ei->vfs_inode;
  79. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  80. return inode;
  81. }
  82. static void proc_destroy_inode(struct inode *inode)
  83. {
  84. kmem_cache_free(proc_inode_cachep, PROC_I(inode));
  85. }
  86. static void init_once(void *foo)
  87. {
  88. struct proc_inode *ei = (struct proc_inode *) foo;
  89. inode_init_once(&ei->vfs_inode);
  90. }
  91. int __init proc_init_inodecache(void)
  92. {
  93. proc_inode_cachep = kmem_cache_create("proc_inode_cache",
  94. sizeof(struct proc_inode),
  95. 0, (SLAB_RECLAIM_ACCOUNT|
  96. SLAB_MEM_SPREAD|SLAB_PANIC),
  97. init_once);
  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. };
  107. static void __pde_users_dec(struct proc_dir_entry *pde)
  108. {
  109. pde->pde_users--;
  110. if (pde->pde_unload_completion && pde->pde_users == 0)
  111. complete(pde->pde_unload_completion);
  112. }
  113. static void pde_users_dec(struct proc_dir_entry *pde)
  114. {
  115. spin_lock(&pde->pde_unload_lock);
  116. __pde_users_dec(pde);
  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. int (*release)(struct inode *, struct file *);
  275. struct pde_opener *pdeo;
  276. /*
  277. * What for, you ask? Well, we can have open, rmmod, remove_proc_entry
  278. * sequence. ->release won't be called because ->proc_fops will be
  279. * cleared. Depending on complexity of ->release, consequences vary.
  280. *
  281. * We can't wait for mercy when close will be done for real, it's
  282. * deadlockable: rmmod foo </proc/foo . So, we're going to do ->release
  283. * by hand in remove_proc_entry(). For this, save opener's credentials
  284. * for later.
  285. */
  286. pdeo = kmalloc(sizeof(struct pde_opener), GFP_KERNEL);
  287. if (!pdeo)
  288. return -ENOMEM;
  289. spin_lock(&pde->pde_unload_lock);
  290. if (!pde->proc_fops) {
  291. spin_unlock(&pde->pde_unload_lock);
  292. kfree(pdeo);
  293. return rv;
  294. }
  295. pde->pde_users++;
  296. open = pde->proc_fops->open;
  297. release = pde->proc_fops->release;
  298. spin_unlock(&pde->pde_unload_lock);
  299. if (open)
  300. rv = open(inode, file);
  301. spin_lock(&pde->pde_unload_lock);
  302. if (rv == 0 && release) {
  303. /* To know what to release. */
  304. pdeo->inode = inode;
  305. pdeo->file = file;
  306. /* Strictly for "too late" ->release in proc_reg_release(). */
  307. pdeo->release = release;
  308. list_add(&pdeo->lh, &pde->pde_openers);
  309. } else
  310. kfree(pdeo);
  311. __pde_users_dec(pde);
  312. spin_unlock(&pde->pde_unload_lock);
  313. return rv;
  314. }
  315. static struct pde_opener *find_pde_opener(struct proc_dir_entry *pde,
  316. struct inode *inode, struct file *file)
  317. {
  318. struct pde_opener *pdeo;
  319. list_for_each_entry(pdeo, &pde->pde_openers, lh) {
  320. if (pdeo->inode == inode && pdeo->file == file)
  321. return pdeo;
  322. }
  323. return NULL;
  324. }
  325. static int proc_reg_release(struct inode *inode, struct file *file)
  326. {
  327. struct proc_dir_entry *pde = PDE(inode);
  328. int rv = 0;
  329. int (*release)(struct inode *, struct file *);
  330. struct pde_opener *pdeo;
  331. spin_lock(&pde->pde_unload_lock);
  332. pdeo = find_pde_opener(pde, inode, file);
  333. if (!pde->proc_fops) {
  334. /*
  335. * Can't simply exit, __fput() will think that everything is OK,
  336. * and move on to freeing struct file. remove_proc_entry() will
  337. * find slacker in opener's list and will try to do non-trivial
  338. * things with struct file. Therefore, remove opener from list.
  339. *
  340. * But if opener is removed from list, who will ->release it?
  341. */
  342. if (pdeo) {
  343. list_del(&pdeo->lh);
  344. spin_unlock(&pde->pde_unload_lock);
  345. rv = pdeo->release(inode, file);
  346. kfree(pdeo);
  347. } else
  348. spin_unlock(&pde->pde_unload_lock);
  349. return rv;
  350. }
  351. pde->pde_users++;
  352. release = pde->proc_fops->release;
  353. if (pdeo) {
  354. list_del(&pdeo->lh);
  355. kfree(pdeo);
  356. }
  357. spin_unlock(&pde->pde_unload_lock);
  358. if (release)
  359. rv = release(inode, file);
  360. pde_users_dec(pde);
  361. return rv;
  362. }
  363. static const struct file_operations proc_reg_file_ops = {
  364. .llseek = proc_reg_llseek,
  365. .read = proc_reg_read,
  366. .write = proc_reg_write,
  367. .poll = proc_reg_poll,
  368. .unlocked_ioctl = proc_reg_unlocked_ioctl,
  369. #ifdef CONFIG_COMPAT
  370. .compat_ioctl = proc_reg_compat_ioctl,
  371. #endif
  372. .mmap = proc_reg_mmap,
  373. .open = proc_reg_open,
  374. .release = proc_reg_release,
  375. };
  376. #ifdef CONFIG_COMPAT
  377. static const struct file_operations proc_reg_file_ops_no_compat = {
  378. .llseek = proc_reg_llseek,
  379. .read = proc_reg_read,
  380. .write = proc_reg_write,
  381. .poll = proc_reg_poll,
  382. .unlocked_ioctl = proc_reg_unlocked_ioctl,
  383. .mmap = proc_reg_mmap,
  384. .open = proc_reg_open,
  385. .release = proc_reg_release,
  386. };
  387. #endif
  388. struct inode *proc_get_inode(struct super_block *sb, unsigned int ino,
  389. struct proc_dir_entry *de)
  390. {
  391. struct inode * inode;
  392. if (!try_module_get(de->owner))
  393. goto out_mod;
  394. inode = iget_locked(sb, ino);
  395. if (!inode)
  396. goto out_ino;
  397. if (inode->i_state & I_NEW) {
  398. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  399. PROC_I(inode)->fd = 0;
  400. PROC_I(inode)->pde = de;
  401. if (de->mode) {
  402. inode->i_mode = de->mode;
  403. inode->i_uid = de->uid;
  404. inode->i_gid = de->gid;
  405. }
  406. if (de->size)
  407. inode->i_size = de->size;
  408. if (de->nlink)
  409. inode->i_nlink = de->nlink;
  410. if (de->proc_iops)
  411. inode->i_op = de->proc_iops;
  412. if (de->proc_fops) {
  413. if (S_ISREG(inode->i_mode)) {
  414. #ifdef CONFIG_COMPAT
  415. if (!de->proc_fops->compat_ioctl)
  416. inode->i_fop =
  417. &proc_reg_file_ops_no_compat;
  418. else
  419. #endif
  420. inode->i_fop = &proc_reg_file_ops;
  421. } else {
  422. inode->i_fop = de->proc_fops;
  423. }
  424. }
  425. unlock_new_inode(inode);
  426. } else
  427. module_put(de->owner);
  428. return inode;
  429. out_ino:
  430. module_put(de->owner);
  431. out_mod:
  432. return NULL;
  433. }
  434. int proc_fill_super(struct super_block *s)
  435. {
  436. struct inode * root_inode;
  437. s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
  438. s->s_blocksize = 1024;
  439. s->s_blocksize_bits = 10;
  440. s->s_magic = PROC_SUPER_MAGIC;
  441. s->s_op = &proc_sops;
  442. s->s_time_gran = 1;
  443. de_get(&proc_root);
  444. root_inode = proc_get_inode(s, PROC_ROOT_INO, &proc_root);
  445. if (!root_inode)
  446. goto out_no_root;
  447. root_inode->i_uid = 0;
  448. root_inode->i_gid = 0;
  449. s->s_root = d_alloc_root(root_inode);
  450. if (!s->s_root)
  451. goto out_no_root;
  452. return 0;
  453. out_no_root:
  454. printk("proc_read_super: get root inode failed\n");
  455. iput(root_inode);
  456. de_put(&proc_root);
  457. return -ENOMEM;
  458. }