psdev.c 10 KB

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