psdev.c 10 KB

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