psdev.c 10 KB

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