inode.c 12 KB

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