psdev.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * An implementation of a loadable kernel mode driver providing
  3. * multiple kernel/user space bidirectional communications links.
  4. *
  5. * Author: Alan Cox <alan@redhat.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. * Adapted to become the Linux 2.0 Coda pseudo device
  13. * Peter Braam <braam@maths.ox.ac.uk>
  14. * Michael Callahan <mjc@emmy.smith.edu>
  15. *
  16. * Changes for Linux 2.1
  17. * Copyright (c) 1997 Carnegie-Mellon University
  18. */
  19. #include <linux/module.h>
  20. #include <linux/errno.h>
  21. #include <linux/kernel.h>
  22. #include <linux/major.h>
  23. #include <linux/time.h>
  24. #include <linux/slab.h>
  25. #include <linux/ioport.h>
  26. #include <linux/fcntl.h>
  27. #include <linux/delay.h>
  28. #include <linux/skbuff.h>
  29. #include <linux/proc_fs.h>
  30. #include <linux/devfs_fs_kernel.h>
  31. #include <linux/vmalloc.h>
  32. #include <linux/fs.h>
  33. #include <linux/file.h>
  34. #include <linux/poll.h>
  35. #include <linux/init.h>
  36. #include <linux/list.h>
  37. #include <linux/smp_lock.h>
  38. #include <linux/device.h>
  39. #include <asm/io.h>
  40. #include <asm/system.h>
  41. #include <asm/poll.h>
  42. #include <asm/uaccess.h>
  43. #include <linux/coda.h>
  44. #include <linux/coda_linux.h>
  45. #include <linux/coda_fs_i.h>
  46. #include <linux/coda_psdev.h>
  47. #include <linux/coda_proc.h>
  48. #include "coda_int.h"
  49. #define upc_free(r) kfree(r)
  50. /* statistics */
  51. int coda_hard; /* allows signals during upcalls */
  52. unsigned long coda_timeout = 30; /* .. secs, then signals will dequeue */
  53. struct venus_comm coda_comms[MAX_CODADEVS];
  54. static struct class *coda_psdev_class;
  55. /*
  56. * Device operations
  57. */
  58. static unsigned int coda_psdev_poll(struct file *file, poll_table * wait)
  59. {
  60. struct venus_comm *vcp = (struct venus_comm *) file->private_data;
  61. unsigned int mask = POLLOUT | POLLWRNORM;
  62. poll_wait(file, &vcp->vc_waitq, wait);
  63. if (!list_empty(&vcp->vc_pending))
  64. mask |= POLLIN | POLLRDNORM;
  65. return mask;
  66. }
  67. static int coda_psdev_ioctl(struct inode * inode, struct file * filp,
  68. unsigned int cmd, unsigned long arg)
  69. {
  70. unsigned int data;
  71. switch(cmd) {
  72. case CIOC_KERNEL_VERSION:
  73. data = CODA_KERNEL_VERSION;
  74. return put_user(data, (int __user *) arg);
  75. default:
  76. return -ENOTTY;
  77. }
  78. return 0;
  79. }
  80. /*
  81. * Receive a message written by Venus to the psdev
  82. */
  83. static ssize_t coda_psdev_write(struct file *file, const char __user *buf,
  84. size_t nbytes, loff_t *off)
  85. {
  86. struct venus_comm *vcp = (struct venus_comm *) file->private_data;
  87. struct upc_req *req = NULL;
  88. struct upc_req *tmp;
  89. struct list_head *lh;
  90. struct coda_in_hdr hdr;
  91. ssize_t retval = 0, count = 0;
  92. int error;
  93. /* Peek at the opcode, uniquefier */
  94. if (copy_from_user(&hdr, buf, 2 * sizeof(u_long)))
  95. return -EFAULT;
  96. if (DOWNCALL(hdr.opcode)) {
  97. struct super_block *sb = NULL;
  98. union outputArgs *dcbuf;
  99. int size = sizeof(*dcbuf);
  100. sb = vcp->vc_sb;
  101. if ( !sb ) {
  102. count = nbytes;
  103. goto out;
  104. }
  105. if ( nbytes < sizeof(struct coda_out_hdr) ) {
  106. printk("coda_downcall opc %d uniq %d, not enough!\n",
  107. hdr.opcode, hdr.unique);
  108. count = nbytes;
  109. goto out;
  110. }
  111. if ( nbytes > size ) {
  112. printk("Coda: downcall opc %d, uniq %d, too much!",
  113. hdr.opcode, hdr.unique);
  114. nbytes = size;
  115. }
  116. CODA_ALLOC(dcbuf, union outputArgs *, nbytes);
  117. if (copy_from_user(dcbuf, buf, nbytes)) {
  118. CODA_FREE(dcbuf, nbytes);
  119. retval = -EFAULT;
  120. goto out;
  121. }
  122. /* what downcall errors does Venus handle ? */
  123. lock_kernel();
  124. error = coda_downcall(hdr.opcode, dcbuf, sb);
  125. unlock_kernel();
  126. CODA_FREE(dcbuf, nbytes);
  127. if (error) {
  128. printk("psdev_write: coda_downcall error: %d\n", error);
  129. retval = error;
  130. goto out;
  131. }
  132. count = nbytes;
  133. goto out;
  134. }
  135. /* Look for the message on the processing queue. */
  136. lock_kernel();
  137. list_for_each(lh, &vcp->vc_processing) {
  138. tmp = list_entry(lh, struct upc_req , uc_chain);
  139. if (tmp->uc_unique == hdr.unique) {
  140. req = tmp;
  141. list_del(&req->uc_chain);
  142. break;
  143. }
  144. }
  145. unlock_kernel();
  146. if (!req) {
  147. printk("psdev_write: msg (%d, %d) not found\n",
  148. hdr.opcode, hdr.unique);
  149. retval = -ESRCH;
  150. goto out;
  151. }
  152. /* move data into response buffer. */
  153. if (req->uc_outSize < nbytes) {
  154. printk("psdev_write: too much cnt: %d, cnt: %ld, opc: %d, uniq: %d.\n",
  155. req->uc_outSize, (long)nbytes, hdr.opcode, hdr.unique);
  156. nbytes = req->uc_outSize; /* don't have more space! */
  157. }
  158. if (copy_from_user(req->uc_data, buf, nbytes)) {
  159. req->uc_flags |= REQ_ABORT;
  160. wake_up(&req->uc_sleep);
  161. retval = -EFAULT;
  162. goto out;
  163. }
  164. /* adjust outsize. is this useful ?? */
  165. req->uc_outSize = nbytes;
  166. req->uc_flags |= REQ_WRITE;
  167. count = nbytes;
  168. /* Convert filedescriptor into a file handle */
  169. if (req->uc_opcode == CODA_OPEN_BY_FD) {
  170. struct coda_open_by_fd_out *outp =
  171. (struct coda_open_by_fd_out *)req->uc_data;
  172. outp->fh = fget(outp->fd);
  173. }
  174. wake_up(&req->uc_sleep);
  175. out:
  176. return(count ? count : retval);
  177. }
  178. /*
  179. * Read a message from the kernel to Venus
  180. */
  181. static ssize_t coda_psdev_read(struct file * file, char __user * buf,
  182. size_t nbytes, loff_t *off)
  183. {
  184. DECLARE_WAITQUEUE(wait, current);
  185. struct venus_comm *vcp = (struct venus_comm *) file->private_data;
  186. struct upc_req *req;
  187. ssize_t retval = 0, count = 0;
  188. if (nbytes == 0)
  189. return 0;
  190. lock_kernel();
  191. add_wait_queue(&vcp->vc_waitq, &wait);
  192. set_current_state(TASK_INTERRUPTIBLE);
  193. while (list_empty(&vcp->vc_pending)) {
  194. if (file->f_flags & O_NONBLOCK) {
  195. retval = -EAGAIN;
  196. break;
  197. }
  198. if (signal_pending(current)) {
  199. retval = -ERESTARTSYS;
  200. break;
  201. }
  202. schedule();
  203. }
  204. set_current_state(TASK_RUNNING);
  205. remove_wait_queue(&vcp->vc_waitq, &wait);
  206. if (retval)
  207. goto out;
  208. req = list_entry(vcp->vc_pending.next, struct upc_req,uc_chain);
  209. list_del(&req->uc_chain);
  210. /* Move the input args into userspace */
  211. count = req->uc_inSize;
  212. if (nbytes < req->uc_inSize) {
  213. printk ("psdev_read: Venus read %ld bytes of %d in message\n",
  214. (long)nbytes, req->uc_inSize);
  215. count = nbytes;
  216. }
  217. if (copy_to_user(buf, req->uc_data, count))
  218. retval = -EFAULT;
  219. /* If request was not a signal, enqueue and don't free */
  220. if (!(req->uc_flags & REQ_ASYNC)) {
  221. req->uc_flags |= REQ_READ;
  222. list_add(&(req->uc_chain), vcp->vc_processing.prev);
  223. goto out;
  224. }
  225. CODA_FREE(req->uc_data, sizeof(struct coda_in_hdr));
  226. upc_free(req);
  227. out:
  228. unlock_kernel();
  229. return (count ? count : retval);
  230. }
  231. static int coda_psdev_open(struct inode * inode, struct file * file)
  232. {
  233. struct venus_comm *vcp;
  234. int idx;
  235. lock_kernel();
  236. idx = iminor(inode);
  237. if(idx >= MAX_CODADEVS) {
  238. unlock_kernel();
  239. return -ENODEV;
  240. }
  241. vcp = &coda_comms[idx];
  242. if(vcp->vc_inuse) {
  243. unlock_kernel();
  244. return -EBUSY;
  245. }
  246. if (!vcp->vc_inuse++) {
  247. INIT_LIST_HEAD(&vcp->vc_pending);
  248. INIT_LIST_HEAD(&vcp->vc_processing);
  249. init_waitqueue_head(&vcp->vc_waitq);
  250. vcp->vc_sb = NULL;
  251. vcp->vc_seq = 0;
  252. }
  253. file->private_data = vcp;
  254. unlock_kernel();
  255. return 0;
  256. }
  257. static int coda_psdev_release(struct inode * inode, struct file * file)
  258. {
  259. struct venus_comm *vcp = (struct venus_comm *) file->private_data;
  260. struct upc_req *req, *tmp;
  261. lock_kernel();
  262. if ( !vcp->vc_inuse ) {
  263. unlock_kernel();
  264. printk("psdev_release: Not open.\n");
  265. return -1;
  266. }
  267. if (--vcp->vc_inuse) {
  268. unlock_kernel();
  269. return 0;
  270. }
  271. /* Wakeup clients so they can return. */
  272. list_for_each_entry_safe(req, tmp, &vcp->vc_pending, uc_chain) {
  273. /* Async requests need to be freed here */
  274. if (req->uc_flags & REQ_ASYNC) {
  275. CODA_FREE(req->uc_data, sizeof(struct coda_in_hdr));
  276. upc_free(req);
  277. continue;
  278. }
  279. req->uc_flags |= REQ_ABORT;
  280. wake_up(&req->uc_sleep);
  281. }
  282. list_for_each_entry(req, &vcp->vc_processing, uc_chain) {
  283. req->uc_flags |= REQ_ABORT;
  284. wake_up(&req->uc_sleep);
  285. }
  286. unlock_kernel();
  287. return 0;
  288. }
  289. static const struct file_operations coda_psdev_fops = {
  290. .owner = THIS_MODULE,
  291. .read = coda_psdev_read,
  292. .write = coda_psdev_write,
  293. .poll = coda_psdev_poll,
  294. .ioctl = coda_psdev_ioctl,
  295. .open = coda_psdev_open,
  296. .release = coda_psdev_release,
  297. };
  298. static int init_coda_psdev(void)
  299. {
  300. int i, err = 0;
  301. if (register_chrdev(CODA_PSDEV_MAJOR, "coda", &coda_psdev_fops)) {
  302. printk(KERN_ERR "coda_psdev: unable to get major %d\n",
  303. CODA_PSDEV_MAJOR);
  304. return -EIO;
  305. }
  306. coda_psdev_class = class_create(THIS_MODULE, "coda");
  307. if (IS_ERR(coda_psdev_class)) {
  308. err = PTR_ERR(coda_psdev_class);
  309. goto out_chrdev;
  310. }
  311. devfs_mk_dir ("coda");
  312. for (i = 0; i < MAX_CODADEVS; i++) {
  313. class_device_create(coda_psdev_class, NULL,
  314. MKDEV(CODA_PSDEV_MAJOR,i), NULL, "cfs%d", i);
  315. err = devfs_mk_cdev(MKDEV(CODA_PSDEV_MAJOR, i),
  316. S_IFCHR|S_IRUSR|S_IWUSR, "coda/%d", i);
  317. if (err)
  318. goto out_class;
  319. }
  320. coda_sysctl_init();
  321. goto out;
  322. out_class:
  323. for (i = 0; i < MAX_CODADEVS; i++)
  324. class_device_destroy(coda_psdev_class, MKDEV(CODA_PSDEV_MAJOR, i));
  325. class_destroy(coda_psdev_class);
  326. out_chrdev:
  327. unregister_chrdev(CODA_PSDEV_MAJOR, "coda");
  328. out:
  329. return err;
  330. }
  331. MODULE_AUTHOR("Peter J. Braam <braam@cs.cmu.edu>");
  332. MODULE_LICENSE("GPL");
  333. static int __init init_coda(void)
  334. {
  335. int status;
  336. int i;
  337. printk(KERN_INFO "Coda Kernel/Venus communications, "
  338. #ifdef CONFIG_CODA_FS_OLD_API
  339. "v5.3.20"
  340. #else
  341. "v6.0.0"
  342. #endif
  343. ", coda@cs.cmu.edu\n");
  344. status = coda_init_inodecache();
  345. if (status)
  346. goto out2;
  347. status = init_coda_psdev();
  348. if ( status ) {
  349. printk("Problem (%d) in init_coda_psdev\n", status);
  350. goto out1;
  351. }
  352. status = register_filesystem(&coda_fs_type);
  353. if (status) {
  354. printk("coda: failed to register filesystem!\n");
  355. goto out;
  356. }
  357. return 0;
  358. out:
  359. for (i = 0; i < MAX_CODADEVS; i++) {
  360. class_device_destroy(coda_psdev_class, MKDEV(CODA_PSDEV_MAJOR, i));
  361. devfs_remove("coda/%d", i);
  362. }
  363. class_destroy(coda_psdev_class);
  364. devfs_remove("coda");
  365. unregister_chrdev(CODA_PSDEV_MAJOR, "coda");
  366. coda_sysctl_clean();
  367. out1:
  368. coda_destroy_inodecache();
  369. out2:
  370. return status;
  371. }
  372. static void __exit exit_coda(void)
  373. {
  374. int err, i;
  375. err = unregister_filesystem(&coda_fs_type);
  376. if ( err != 0 ) {
  377. printk("coda: failed to unregister filesystem\n");
  378. }
  379. for (i = 0; i < MAX_CODADEVS; i++) {
  380. class_device_destroy(coda_psdev_class, MKDEV(CODA_PSDEV_MAJOR, i));
  381. devfs_remove("coda/%d", i);
  382. }
  383. class_destroy(coda_psdev_class);
  384. devfs_remove("coda");
  385. unregister_chrdev(CODA_PSDEV_MAJOR, "coda");
  386. coda_sysctl_clean();
  387. coda_destroy_inodecache();
  388. }
  389. module_init(init_coda);
  390. module_exit(exit_coda);