inode.c 12 KB

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