psdev.c 11 KB

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