inode.c 12 KB

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