3 * Copyright (c) 2010-2017 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/stdint.h>
28 #include <sys/stddef.h>
29 #include <sys/param.h>
30 #include <sys/types.h>
31 #include <sys/systm.h>
33 #include <sys/kernel.h>
35 #include <sys/linker_set.h>
36 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <sys/condvar.h>
40 #include <sys/sysctl.h>
41 #include <sys/unistd.h>
42 #include <sys/malloc.h>
47 #include <sys/rwlock.h>
48 #include <sys/queue.h>
49 #include <sys/fcntl.h>
51 #include <sys/vnode.h>
52 #include <sys/selinfo.h>
53 #include <sys/ptrace.h>
54 #include <sys/sysent.h>
56 #include <machine/bus.h>
60 #include <vm/vm_object.h>
61 #include <vm/vm_page.h>
62 #include <vm/vm_pager.h>
64 #include <fs/cuse/cuse_defs.h>
65 #include <fs/cuse/cuse_ioctl.h>
67 MODULE_VERSION(cuse, 1);
70 * Prevent cuse4bsd.ko and cuse.ko from loading at the same time by
71 * declaring support for the cuse4bsd interface in cuse.ko:
73 MODULE_VERSION(cuse4bsd, 1);
76 FEATURE(cuse, "Userspace character devices");
83 struct cuse_client_command {
84 TAILQ_ENTRY(cuse_client_command) entry;
85 struct cuse_command sub;
88 struct thread *entered;
89 struct cuse_client *client;
90 struct proc *proc_curr;
98 TAILQ_ENTRY(cuse_memory) entry;
104 struct cuse_server_dev {
105 TAILQ_ENTRY(cuse_server_dev) entry;
106 struct cuse_server *server;
107 struct cdev *kern_dev;
108 struct cuse_dev *user_dev;
112 TAILQ_ENTRY(cuse_server) entry;
113 TAILQ_HEAD(, cuse_client_command) head;
114 TAILQ_HEAD(, cuse_server_dev) hdev;
115 TAILQ_HEAD(, cuse_client) hcli;
116 TAILQ_HEAD(, cuse_memory) hmem;
118 struct selinfo selinfo;
125 TAILQ_ENTRY(cuse_client) entry;
126 TAILQ_ENTRY(cuse_client) entry_ref;
127 struct cuse_client_command cmds[CUSE_CMD_MAX];
128 struct cuse_server *server;
129 struct cuse_server_dev *server_dev;
131 uint8_t ioctl_buffer[CUSE_BUFFER_MAX] __aligned(4);
133 int fflags; /* file flags */
134 int cflags; /* client flags */
135 #define CUSE_CLI_IS_CLOSING 0x01
136 #define CUSE_CLI_KNOTE_NEED_READ 0x02
137 #define CUSE_CLI_KNOTE_NEED_WRITE 0x04
138 #define CUSE_CLI_KNOTE_HAS_READ 0x08
139 #define CUSE_CLI_KNOTE_HAS_WRITE 0x10
142 #define CUSE_CLIENT_CLOSING(pcc) \
143 ((pcc)->cflags & CUSE_CLI_IS_CLOSING)
145 static MALLOC_DEFINE(M_CUSE, "cuse", "CUSE memory");
147 static TAILQ_HEAD(, cuse_server) cuse_server_head;
148 static struct mtx cuse_mtx;
149 static struct cdev *cuse_dev;
150 static struct cuse_server *cuse_alloc_unit[CUSE_DEVICES_MAX];
151 static int cuse_alloc_unit_id[CUSE_DEVICES_MAX];
153 static void cuse_server_wakeup_all_client_locked(struct cuse_server *pcs);
154 static void cuse_client_kqfilter_read_detach(struct knote *kn);
155 static void cuse_client_kqfilter_write_detach(struct knote *kn);
156 static int cuse_client_kqfilter_read_event(struct knote *kn, long hint);
157 static int cuse_client_kqfilter_write_event(struct knote *kn, long hint);
159 static struct filterops cuse_client_kqfilter_read_ops = {
161 .f_detach = cuse_client_kqfilter_read_detach,
162 .f_event = cuse_client_kqfilter_read_event,
165 static struct filterops cuse_client_kqfilter_write_ops = {
167 .f_detach = cuse_client_kqfilter_write_detach,
168 .f_event = cuse_client_kqfilter_write_event,
171 static d_open_t cuse_client_open;
172 static d_close_t cuse_client_close;
173 static d_ioctl_t cuse_client_ioctl;
174 static d_read_t cuse_client_read;
175 static d_write_t cuse_client_write;
176 static d_poll_t cuse_client_poll;
177 static d_mmap_single_t cuse_client_mmap_single;
178 static d_kqfilter_t cuse_client_kqfilter;
180 static struct cdevsw cuse_client_devsw = {
181 .d_version = D_VERSION,
182 .d_open = cuse_client_open,
183 .d_close = cuse_client_close,
184 .d_ioctl = cuse_client_ioctl,
185 .d_name = "cuse_client",
186 .d_flags = D_TRACKCLOSE,
187 .d_read = cuse_client_read,
188 .d_write = cuse_client_write,
189 .d_poll = cuse_client_poll,
190 .d_mmap_single = cuse_client_mmap_single,
191 .d_kqfilter = cuse_client_kqfilter,
194 static d_open_t cuse_server_open;
195 static d_close_t cuse_server_close;
196 static d_ioctl_t cuse_server_ioctl;
197 static d_read_t cuse_server_read;
198 static d_write_t cuse_server_write;
199 static d_poll_t cuse_server_poll;
200 static d_mmap_single_t cuse_server_mmap_single;
202 static struct cdevsw cuse_server_devsw = {
203 .d_version = D_VERSION,
204 .d_open = cuse_server_open,
205 .d_close = cuse_server_close,
206 .d_ioctl = cuse_server_ioctl,
207 .d_name = "cuse_server",
208 .d_flags = D_TRACKCLOSE,
209 .d_read = cuse_server_read,
210 .d_write = cuse_server_write,
211 .d_poll = cuse_server_poll,
212 .d_mmap_single = cuse_server_mmap_single,
215 static void cuse_client_is_closing(struct cuse_client *);
216 static int cuse_free_unit_by_id_locked(struct cuse_server *, int);
227 mtx_unlock(&cuse_mtx);
231 cuse_cmd_lock(struct cuse_client_command *pccmd)
233 sx_xlock(&pccmd->sx);
237 cuse_cmd_unlock(struct cuse_client_command *pccmd)
239 sx_xunlock(&pccmd->sx);
243 cuse_kern_init(void *arg)
245 TAILQ_INIT(&cuse_server_head);
247 mtx_init(&cuse_mtx, "cuse-mtx", NULL, MTX_DEF);
249 cuse_dev = make_dev(&cuse_server_devsw, 0,
250 UID_ROOT, GID_OPERATOR, 0600, "cuse");
252 printf("Cuse v%d.%d.%d @ /dev/cuse\n",
253 (CUSE_VERSION >> 16) & 0xFF, (CUSE_VERSION >> 8) & 0xFF,
254 (CUSE_VERSION >> 0) & 0xFF);
256 SYSINIT(cuse_kern_init, SI_SUB_DEVFS, SI_ORDER_ANY, cuse_kern_init, NULL);
259 cuse_kern_uninit(void *arg)
265 printf("Cuse: Please exit all /dev/cuse instances "
266 "and processes which have used this device.\n");
268 pause("DRAIN", 2 * hz);
271 ptr = TAILQ_FIRST(&cuse_server_head);
278 if (cuse_dev != NULL)
279 destroy_dev(cuse_dev);
281 mtx_destroy(&cuse_mtx);
283 SYSUNINIT(cuse_kern_uninit, SI_SUB_DEVFS, SI_ORDER_ANY, cuse_kern_uninit, 0);
286 cuse_server_get(struct cuse_server **ppcs)
288 struct cuse_server *pcs;
291 error = devfs_get_cdevpriv((void **)&pcs);
296 /* check if closing */
298 if (pcs->is_closing) {
309 cuse_server_is_closing(struct cuse_server *pcs)
311 struct cuse_client *pcc;
318 TAILQ_FOREACH(pcc, &pcs->hcli, entry) {
319 cuse_client_is_closing(pcc);
323 static struct cuse_client_command *
324 cuse_server_find_command(struct cuse_server *pcs, struct thread *td)
326 struct cuse_client *pcc;
332 TAILQ_FOREACH(pcc, &pcs->hcli, entry) {
333 if (CUSE_CLIENT_CLOSING(pcc))
335 for (n = 0; n != CUSE_CMD_MAX; n++) {
336 if (pcc->cmds[n].entered == td)
337 return (&pcc->cmds[n]);
345 cuse_str_filter(char *ptr)
349 while (((c = *ptr) != 0)) {
351 if ((c >= 'a') && (c <= 'z')) {
355 if ((c >= 'A') && (c <= 'Z')) {
359 if ((c >= '0') && (c <= '9')) {
363 if ((c == '.') || (c == '_') || (c == '/')) {
374 cuse_convert_error(int error)
382 case CUSE_ERR_WOULDBLOCK:
383 return (EWOULDBLOCK);
384 case CUSE_ERR_INVALID:
386 case CUSE_ERR_NO_MEMORY:
390 case CUSE_ERR_SIGNAL:
392 case CUSE_ERR_NO_DEVICE:
400 cuse_vm_memory_free(struct cuse_memory *mem)
402 /* last user is gone - free */
403 vm_object_deallocate(mem->object);
405 /* free CUSE memory */
410 cuse_server_alloc_memory(struct cuse_server *pcs, uint32_t alloc_nr,
413 struct cuse_memory *temp;
414 struct cuse_memory *mem;
418 mem = malloc(sizeof(*mem), M_CUSE, M_WAITOK | M_ZERO);
422 object = vm_pager_allocate(OBJT_SWAP, NULL, PAGE_SIZE * page_count,
423 VM_PROT_DEFAULT, 0, curthread->td_ucred);
424 if (object == NULL) {
430 /* check if allocation number already exists */
431 TAILQ_FOREACH(temp, &pcs->hmem, entry) {
432 if (temp->alloc_nr == alloc_nr)
440 mem->object = object;
441 mem->page_count = page_count;
442 mem->alloc_nr = alloc_nr;
443 TAILQ_INSERT_TAIL(&pcs->hmem, mem, entry);
449 vm_object_deallocate(object);
456 cuse_server_free_memory(struct cuse_server *pcs, uint32_t alloc_nr)
458 struct cuse_memory *mem;
461 TAILQ_FOREACH(mem, &pcs->hmem, entry) {
462 if (mem->alloc_nr == alloc_nr)
469 TAILQ_REMOVE(&pcs->hmem, mem, entry);
472 cuse_vm_memory_free(mem);
478 cuse_client_get(struct cuse_client **ppcc)
480 struct cuse_client *pcc;
483 /* try to get private data */
484 error = devfs_get_cdevpriv((void **)&pcc);
489 /* check if closing */
491 if (CUSE_CLIENT_CLOSING(pcc) || pcc->server->is_closing) {
502 cuse_client_is_closing(struct cuse_client *pcc)
504 struct cuse_client_command *pccmd;
507 if (CUSE_CLIENT_CLOSING(pcc))
510 pcc->cflags |= CUSE_CLI_IS_CLOSING;
511 pcc->server_dev = NULL;
513 for (n = 0; n != CUSE_CMD_MAX; n++) {
515 pccmd = &pcc->cmds[n];
517 if (pccmd->entry.tqe_prev != NULL) {
518 TAILQ_REMOVE(&pcc->server->head, pccmd, entry);
519 pccmd->entry.tqe_prev = NULL;
521 cv_broadcast(&pccmd->cv);
526 cuse_client_send_command_locked(struct cuse_client_command *pccmd,
527 uintptr_t data_ptr, unsigned long arg, int fflags, int ioflag)
529 unsigned long cuse_fflags = 0;
530 struct cuse_server *pcs;
533 cuse_fflags |= CUSE_FFLAG_READ;
536 cuse_fflags |= CUSE_FFLAG_WRITE;
538 if (ioflag & IO_NDELAY)
539 cuse_fflags |= CUSE_FFLAG_NONBLOCK;
540 #if defined(__LP64__)
541 if (SV_CURPROC_FLAG(SV_ILP32))
542 cuse_fflags |= CUSE_FFLAG_COMPAT32;
544 pccmd->sub.fflags = cuse_fflags;
545 pccmd->sub.data_pointer = data_ptr;
546 pccmd->sub.argument = arg;
548 pcs = pccmd->client->server;
550 if ((pccmd->entry.tqe_prev == NULL) &&
551 (CUSE_CLIENT_CLOSING(pccmd->client) == 0) &&
552 (pcs->is_closing == 0)) {
553 TAILQ_INSERT_TAIL(&pcs->head, pccmd, entry);
559 cuse_client_got_signal(struct cuse_client_command *pccmd)
561 struct cuse_server *pcs;
563 pccmd->got_signal = 1;
565 pccmd = &pccmd->client->cmds[CUSE_CMD_SIGNAL];
567 pcs = pccmd->client->server;
569 if ((pccmd->entry.tqe_prev == NULL) &&
570 (CUSE_CLIENT_CLOSING(pccmd->client) == 0) &&
571 (pcs->is_closing == 0)) {
572 TAILQ_INSERT_TAIL(&pcs->head, pccmd, entry);
578 cuse_client_receive_command_locked(struct cuse_client_command *pccmd,
579 uint8_t *arg_ptr, uint32_t arg_len)
585 pccmd->proc_curr = curthread->td_proc;
587 if (CUSE_CLIENT_CLOSING(pccmd->client) ||
588 pccmd->client->server->is_closing) {
589 error = CUSE_ERR_OTHER;
592 while (pccmd->command == CUSE_CMD_NONE) {
594 cv_wait(&pccmd->cv, &cuse_mtx);
596 error = cv_wait_sig(&pccmd->cv, &cuse_mtx);
599 cuse_client_got_signal(pccmd);
601 if (CUSE_CLIENT_CLOSING(pccmd->client) ||
602 pccmd->client->server->is_closing) {
603 error = CUSE_ERR_OTHER;
608 error = pccmd->error;
609 pccmd->command = CUSE_CMD_NONE;
610 cv_signal(&pccmd->cv);
614 /* wait until all process references are gone */
616 pccmd->proc_curr = NULL;
618 while (pccmd->proc_refs != 0)
619 cv_wait(&pccmd->cv, &cuse_mtx);
624 /*------------------------------------------------------------------------*
626 *------------------------------------------------------------------------*/
629 cuse_server_free_dev(struct cuse_server_dev *pcsd)
631 struct cuse_server *pcs;
632 struct cuse_client *pcc;
634 /* get server pointer */
637 /* prevent creation of more devices */
639 if (pcsd->kern_dev != NULL)
640 pcsd->kern_dev->si_drv1 = NULL;
642 TAILQ_FOREACH(pcc, &pcs->hcli, entry) {
643 if (pcc->server_dev == pcsd)
644 cuse_client_is_closing(pcc);
648 /* destroy device, if any */
649 if (pcsd->kern_dev != NULL) {
650 /* destroy device synchronously */
651 destroy_dev(pcsd->kern_dev);
657 cuse_server_unref(struct cuse_server *pcs)
659 struct cuse_server_dev *pcsd;
660 struct cuse_memory *mem;
664 if (pcs->refs != 0) {
668 cuse_server_is_closing(pcs);
669 /* final client wakeup, if any */
670 cuse_server_wakeup_all_client_locked(pcs);
672 TAILQ_REMOVE(&cuse_server_head, pcs, entry);
674 cuse_free_unit_by_id_locked(pcs, -1);
676 while ((pcsd = TAILQ_FIRST(&pcs->hdev)) != NULL) {
677 TAILQ_REMOVE(&pcs->hdev, pcsd, entry);
679 cuse_server_free_dev(pcsd);
683 while ((mem = TAILQ_FIRST(&pcs->hmem)) != NULL) {
684 TAILQ_REMOVE(&pcs->hmem, mem, entry);
686 cuse_vm_memory_free(mem);
690 knlist_clear(&pcs->selinfo.si_note, 1);
691 knlist_destroy(&pcs->selinfo.si_note);
695 seldrain(&pcs->selinfo);
697 cv_destroy(&pcs->cv);
703 cuse_server_free(void *arg)
705 struct cuse_server *pcs = arg;
708 cuse_server_unref(pcs);
712 cuse_server_open(struct cdev *dev, int fflags, int devtype, struct thread *td)
714 struct cuse_server *pcs;
716 pcs = malloc(sizeof(*pcs), M_CUSE, M_WAITOK | M_ZERO);
720 if (devfs_set_cdevpriv(pcs, &cuse_server_free)) {
721 printf("Cuse: Cannot set cdevpriv.\n");
725 /* store current process ID */
726 pcs->pid = curproc->p_pid;
728 TAILQ_INIT(&pcs->head);
729 TAILQ_INIT(&pcs->hdev);
730 TAILQ_INIT(&pcs->hcli);
731 TAILQ_INIT(&pcs->hmem);
733 cv_init(&pcs->cv, "cuse-server-cv");
735 knlist_init_mtx(&pcs->selinfo.si_note, &cuse_mtx);
739 TAILQ_INSERT_TAIL(&cuse_server_head, pcs, entry);
746 cuse_server_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
748 struct cuse_server *pcs;
751 error = cuse_server_get(&pcs);
756 cuse_server_is_closing(pcs);
757 /* final client wakeup, if any */
758 cuse_server_wakeup_all_client_locked(pcs);
760 knlist_clear(&pcs->selinfo.si_note, 1);
768 cuse_server_read(struct cdev *dev, struct uio *uio, int ioflag)
774 cuse_server_write(struct cdev *dev, struct uio *uio, int ioflag)
780 cuse_server_ioctl_copy_locked(struct cuse_client_command *pccmd,
781 struct cuse_data_chunk *pchk, int isread)
787 offset = pchk->peer_ptr - CUSE_BUF_MIN_PTR;
789 if (pchk->length > CUSE_BUFFER_MAX)
792 if (offset >= CUSE_BUFFER_MAX)
795 if ((offset + pchk->length) > CUSE_BUFFER_MAX)
798 p_proc = pccmd->proc_curr;
802 if (pccmd->proc_refs < 0)
811 (void *)pchk->local_ptr,
812 pccmd->client->ioctl_buffer + offset,
816 pccmd->client->ioctl_buffer + offset,
817 (void *)pchk->local_ptr,
825 if (pccmd->proc_curr == NULL)
826 cv_signal(&pccmd->cv);
832 cuse_proc2proc_copy(struct proc *proc_s, vm_offset_t data_s,
833 struct proc *proc_d, vm_offset_t data_d, size_t len)
836 struct proc *proc_cur;
840 proc_cur = td->td_proc;
842 if (proc_cur == proc_d) {
844 .iov_base = (caddr_t)data_d,
850 .uio_offset = (off_t)data_s,
852 .uio_segflg = UIO_USERSPACE,
858 error = proc_rwmem(proc_s, &uio);
861 } else if (proc_cur == proc_s) {
863 .iov_base = (caddr_t)data_s,
869 .uio_offset = (off_t)data_d,
871 .uio_segflg = UIO_USERSPACE,
877 error = proc_rwmem(proc_d, &uio);
886 cuse_server_data_copy_locked(struct cuse_client_command *pccmd,
887 struct cuse_data_chunk *pchk, int isread)
892 p_proc = pccmd->proc_curr;
896 if (pccmd->proc_refs < 0)
904 error = cuse_proc2proc_copy(
905 curthread->td_proc, pchk->local_ptr,
906 p_proc, pchk->peer_ptr,
909 error = cuse_proc2proc_copy(
910 p_proc, pchk->peer_ptr,
911 curthread->td_proc, pchk->local_ptr,
919 if (pccmd->proc_curr == NULL)
920 cv_signal(&pccmd->cv);
926 cuse_alloc_unit_by_id_locked(struct cuse_server *pcs, int id)
933 for (match = n = 0; n != CUSE_DEVICES_MAX; n++) {
934 if (cuse_alloc_unit[n] != NULL) {
935 if ((cuse_alloc_unit_id[n] ^ id) & CUSE_ID_MASK)
937 if ((cuse_alloc_unit_id[n] & ~CUSE_ID_MASK) == x) {
946 for (n = 0; n != CUSE_DEVICES_MAX; n++) {
947 if (cuse_alloc_unit[n] == NULL) {
948 cuse_alloc_unit[n] = pcs;
949 cuse_alloc_unit_id[n] = id | x;
958 cuse_server_wakeup_locked(struct cuse_server *pcs)
960 selwakeup(&pcs->selinfo);
961 KNOTE_LOCKED(&pcs->selinfo.si_note, 0);
965 cuse_server_wakeup_all_client_locked(struct cuse_server *pcs)
967 struct cuse_client *pcc;
969 TAILQ_FOREACH(pcc, &pcs->hcli, entry) {
970 pcc->cflags |= (CUSE_CLI_KNOTE_NEED_READ |
971 CUSE_CLI_KNOTE_NEED_WRITE);
973 cuse_server_wakeup_locked(pcs);
977 cuse_free_unit_by_id_locked(struct cuse_server *pcs, int id)
982 for (n = 0; n != CUSE_DEVICES_MAX; n++) {
983 if (cuse_alloc_unit[n] == pcs) {
984 if (cuse_alloc_unit_id[n] == id || id == -1) {
985 cuse_alloc_unit[n] = NULL;
986 cuse_alloc_unit_id[n] = 0;
992 return (found ? 0 : EINVAL);
996 cuse_server_ioctl(struct cdev *dev, unsigned long cmd,
997 caddr_t data, int fflag, struct thread *td)
999 struct cuse_server *pcs;
1002 error = cuse_server_get(&pcs);
1007 struct cuse_client_command *pccmd;
1008 struct cuse_client *pcc;
1009 struct cuse_command *pcmd;
1010 struct cuse_alloc_info *pai;
1011 struct cuse_create_dev *pcd;
1012 struct cuse_server_dev *pcsd;
1013 struct cuse_data_chunk *pchk;
1016 case CUSE_IOCTL_GET_COMMAND:
1017 pcmd = (void *)data;
1021 while ((pccmd = TAILQ_FIRST(&pcs->head)) == NULL) {
1022 error = cv_wait_sig(&pcs->cv, &cuse_mtx);
1024 if (pcs->is_closing)
1033 TAILQ_REMOVE(&pcs->head, pccmd, entry);
1034 pccmd->entry.tqe_prev = NULL;
1036 pccmd->entered = curthread;
1044 case CUSE_IOCTL_SYNC_COMMAND:
1047 while ((pccmd = cuse_server_find_command(pcs, curthread)) != NULL) {
1049 /* send sync command */
1050 pccmd->entered = NULL;
1051 pccmd->error = *(int *)data;
1052 pccmd->command = CUSE_CMD_SYNC;
1054 /* signal peer, if any */
1055 cv_signal(&pccmd->cv);
1061 case CUSE_IOCTL_ALLOC_UNIT:
1064 n = cuse_alloc_unit_by_id_locked(pcs,
1065 CUSE_ID_DEFAULT(0));
1074 case CUSE_IOCTL_ALLOC_UNIT_BY_ID:
1078 n = (n & CUSE_ID_MASK);
1081 n = cuse_alloc_unit_by_id_locked(pcs, n);
1090 case CUSE_IOCTL_FREE_UNIT:
1094 n = CUSE_ID_DEFAULT(n);
1097 error = cuse_free_unit_by_id_locked(pcs, n);
1101 case CUSE_IOCTL_FREE_UNIT_BY_ID:
1106 error = cuse_free_unit_by_id_locked(pcs, n);
1110 case CUSE_IOCTL_ALLOC_MEMORY:
1114 if (pai->alloc_nr >= CUSE_ALLOC_UNIT_MAX) {
1118 if (pai->page_count >= CUSE_ALLOC_PAGES_MAX) {
1122 error = cuse_server_alloc_memory(pcs,
1123 pai->alloc_nr, pai->page_count);
1126 case CUSE_IOCTL_FREE_MEMORY:
1129 if (pai->alloc_nr >= CUSE_ALLOC_UNIT_MAX) {
1133 error = cuse_server_free_memory(pcs, pai->alloc_nr);
1136 case CUSE_IOCTL_GET_SIG:
1139 pccmd = cuse_server_find_command(pcs, curthread);
1141 if (pccmd != NULL) {
1142 n = pccmd->got_signal;
1143 pccmd->got_signal = 0;
1153 case CUSE_IOCTL_SET_PFH:
1156 pccmd = cuse_server_find_command(pcs, curthread);
1158 if (pccmd != NULL) {
1159 pcc = pccmd->client;
1160 for (n = 0; n != CUSE_CMD_MAX; n++) {
1161 pcc->cmds[n].sub.per_file_handle = *(uintptr_t *)data;
1169 case CUSE_IOCTL_CREATE_DEV:
1171 error = priv_check(curthread, PRIV_DRIVER);
1179 pcd->devname[sizeof(pcd->devname) - 1] = 0;
1181 if (pcd->devname[0] == 0) {
1185 cuse_str_filter(pcd->devname);
1187 pcd->permissions &= 0777;
1189 /* try to allocate a character device */
1191 pcsd = malloc(sizeof(*pcsd), M_CUSE, M_WAITOK | M_ZERO);
1199 pcsd->user_dev = pcd->dev;
1201 pcsd->kern_dev = make_dev_credf(MAKEDEV_CHECKNAME,
1202 &cuse_client_devsw, 0, NULL, pcd->user_id, pcd->group_id,
1203 pcd->permissions, "%s", pcd->devname);
1205 if (pcsd->kern_dev == NULL) {
1210 pcsd->kern_dev->si_drv1 = pcsd;
1213 TAILQ_INSERT_TAIL(&pcs->hdev, pcsd, entry);
1218 case CUSE_IOCTL_DESTROY_DEV:
1220 error = priv_check(curthread, PRIV_DRIVER);
1228 pcsd = TAILQ_FIRST(&pcs->hdev);
1229 while (pcsd != NULL) {
1230 if (pcsd->user_dev == *(struct cuse_dev **)data) {
1231 TAILQ_REMOVE(&pcs->hdev, pcsd, entry);
1233 cuse_server_free_dev(pcsd);
1236 pcsd = TAILQ_FIRST(&pcs->hdev);
1238 pcsd = TAILQ_NEXT(pcsd, entry);
1245 case CUSE_IOCTL_WRITE_DATA:
1246 case CUSE_IOCTL_READ_DATA:
1249 pchk = (struct cuse_data_chunk *)data;
1251 pccmd = cuse_server_find_command(pcs, curthread);
1253 if (pccmd == NULL) {
1254 error = ENXIO; /* invalid request */
1255 } else if (pchk->peer_ptr < CUSE_BUF_MIN_PTR) {
1256 error = EFAULT; /* NULL pointer */
1257 } else if (pchk->peer_ptr < CUSE_BUF_MAX_PTR) {
1258 error = cuse_server_ioctl_copy_locked(pccmd,
1259 pchk, cmd == CUSE_IOCTL_READ_DATA);
1261 error = cuse_server_data_copy_locked(pccmd,
1262 pchk, cmd == CUSE_IOCTL_READ_DATA);
1267 case CUSE_IOCTL_SELWAKEUP:
1270 * We don't know which direction caused the event.
1273 cuse_server_wakeup_all_client_locked(pcs);
1285 cuse_server_poll(struct cdev *dev, int events, struct thread *td)
1287 return (events & (POLLHUP | POLLPRI | POLLIN |
1288 POLLRDNORM | POLLOUT | POLLWRNORM));
1292 cuse_server_mmap_single(struct cdev *dev, vm_ooffset_t *offset,
1293 vm_size_t size, struct vm_object **object, int nprot)
1295 uint32_t page_nr = *offset / PAGE_SIZE;
1296 uint32_t alloc_nr = page_nr / CUSE_ALLOC_PAGES_MAX;
1297 struct cuse_memory *mem;
1298 struct cuse_server *pcs;
1301 error = cuse_server_get(&pcs);
1306 /* lookup memory structure */
1307 TAILQ_FOREACH(mem, &pcs->hmem, entry) {
1308 if (mem->alloc_nr == alloc_nr)
1315 /* verify page offset */
1316 page_nr %= CUSE_ALLOC_PAGES_MAX;
1317 if (page_nr >= mem->page_count) {
1321 /* verify mmap size */
1322 if ((size % PAGE_SIZE) != 0 || (size < PAGE_SIZE) ||
1323 (size > ((mem->page_count - page_nr) * PAGE_SIZE))) {
1327 vm_object_reference(mem->object);
1328 *object = mem->object;
1331 /* set new VM object offset to use */
1332 *offset = page_nr * PAGE_SIZE;
1338 /*------------------------------------------------------------------------*
1340 *------------------------------------------------------------------------*/
1342 cuse_client_free(void *arg)
1344 struct cuse_client *pcc = arg;
1345 struct cuse_client_command *pccmd;
1346 struct cuse_server *pcs;
1350 cuse_client_is_closing(pcc);
1351 TAILQ_REMOVE(&pcc->server->hcli, pcc, entry);
1354 for (n = 0; n != CUSE_CMD_MAX; n++) {
1356 pccmd = &pcc->cmds[n];
1358 sx_destroy(&pccmd->sx);
1359 cv_destroy(&pccmd->cv);
1366 /* drop reference on server */
1367 cuse_server_unref(pcs);
1371 cuse_client_open(struct cdev *dev, int fflags, int devtype, struct thread *td)
1373 struct cuse_client_command *pccmd;
1374 struct cuse_server_dev *pcsd;
1375 struct cuse_client *pcc;
1376 struct cuse_server *pcs;
1377 struct cuse_dev *pcd;
1382 pcsd = dev->si_drv1;
1385 pcd = pcsd->user_dev;
1387 * Check that the refcount didn't wrap and that the
1388 * same process is not both client and server. This
1389 * can easily lead to deadlocks when destroying the
1390 * CUSE character device nodes:
1393 if (pcs->refs < 0 || pcs->pid == curproc->p_pid) {
1394 /* overflow or wrong PID */
1407 pcc = malloc(sizeof(*pcc), M_CUSE, M_WAITOK | M_ZERO);
1409 /* drop reference on server */
1410 cuse_server_unref(pcs);
1413 if (devfs_set_cdevpriv(pcc, &cuse_client_free)) {
1414 printf("Cuse: Cannot set cdevpriv.\n");
1415 /* drop reference on server */
1416 cuse_server_unref(pcs);
1420 pcc->fflags = fflags;
1421 pcc->server_dev = pcsd;
1424 for (n = 0; n != CUSE_CMD_MAX; n++) {
1426 pccmd = &pcc->cmds[n];
1428 pccmd->sub.dev = pcd;
1429 pccmd->sub.command = n;
1430 pccmd->client = pcc;
1432 sx_init(&pccmd->sx, "cuse-client-sx");
1433 cv_init(&pccmd->cv, "cuse-client-cv");
1438 /* cuse_client_free() assumes that the client is listed somewhere! */
1439 /* always enqueue */
1441 TAILQ_INSERT_TAIL(&pcs->hcli, pcc, entry);
1443 /* check if server is closing */
1444 if ((pcs->is_closing != 0) || (dev->si_drv1 == NULL)) {
1452 devfs_clear_cdevpriv(); /* XXX bugfix */
1455 pccmd = &pcc->cmds[CUSE_CMD_OPEN];
1457 cuse_cmd_lock(pccmd);
1460 cuse_client_send_command_locked(pccmd, 0, 0, pcc->fflags, 0);
1462 error = cuse_client_receive_command_locked(pccmd, 0, 0);
1466 error = cuse_convert_error(error);
1471 cuse_cmd_unlock(pccmd);
1474 devfs_clear_cdevpriv(); /* XXX bugfix */
1480 cuse_client_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
1482 struct cuse_client_command *pccmd;
1483 struct cuse_client *pcc;
1486 error = cuse_client_get(&pcc);
1490 pccmd = &pcc->cmds[CUSE_CMD_CLOSE];
1492 cuse_cmd_lock(pccmd);
1495 cuse_client_send_command_locked(pccmd, 0, 0, pcc->fflags, 0);
1497 error = cuse_client_receive_command_locked(pccmd, 0, 0);
1500 cuse_cmd_unlock(pccmd);
1503 cuse_client_is_closing(pcc);
1510 cuse_client_kqfilter_poll(struct cdev *dev, struct cuse_client *pcc)
1515 temp = (pcc->cflags & (CUSE_CLI_KNOTE_HAS_READ |
1516 CUSE_CLI_KNOTE_HAS_WRITE));
1517 pcc->cflags &= ~(CUSE_CLI_KNOTE_NEED_READ |
1518 CUSE_CLI_KNOTE_NEED_WRITE);
1522 /* get the latest polling state from the server */
1523 temp = cuse_client_poll(dev, POLLIN | POLLOUT, NULL);
1525 if (temp & (POLLIN | POLLOUT)) {
1528 pcc->cflags |= CUSE_CLI_KNOTE_NEED_READ;
1530 pcc->cflags |= CUSE_CLI_KNOTE_NEED_WRITE;
1532 /* make sure the "knote" gets woken up */
1533 cuse_server_wakeup_locked(pcc->server);
1540 cuse_client_read(struct cdev *dev, struct uio *uio, int ioflag)
1542 struct cuse_client_command *pccmd;
1543 struct cuse_client *pcc;
1547 error = cuse_client_get(&pcc);
1551 pccmd = &pcc->cmds[CUSE_CMD_READ];
1553 if (uio->uio_segflg != UIO_USERSPACE) {
1556 uio->uio_segflg = UIO_NOCOPY;
1558 cuse_cmd_lock(pccmd);
1560 while (uio->uio_resid != 0) {
1562 if (uio->uio_iov->iov_len > CUSE_LENGTH_MAX) {
1566 len = uio->uio_iov->iov_len;
1569 cuse_client_send_command_locked(pccmd,
1570 (uintptr_t)uio->uio_iov->iov_base,
1571 (unsigned long)(unsigned int)len, pcc->fflags, ioflag);
1573 error = cuse_client_receive_command_locked(pccmd, 0, 0);
1577 error = cuse_convert_error(error);
1579 } else if (error == len) {
1580 error = uiomove(NULL, error, uio);
1584 error = uiomove(NULL, error, uio);
1588 cuse_cmd_unlock(pccmd);
1590 uio->uio_segflg = UIO_USERSPACE;/* restore segment flag */
1592 if (error == EWOULDBLOCK)
1593 cuse_client_kqfilter_poll(dev, pcc);
1599 cuse_client_write(struct cdev *dev, struct uio *uio, int ioflag)
1601 struct cuse_client_command *pccmd;
1602 struct cuse_client *pcc;
1606 error = cuse_client_get(&pcc);
1610 pccmd = &pcc->cmds[CUSE_CMD_WRITE];
1612 if (uio->uio_segflg != UIO_USERSPACE) {
1615 uio->uio_segflg = UIO_NOCOPY;
1617 cuse_cmd_lock(pccmd);
1619 while (uio->uio_resid != 0) {
1621 if (uio->uio_iov->iov_len > CUSE_LENGTH_MAX) {
1625 len = uio->uio_iov->iov_len;
1628 cuse_client_send_command_locked(pccmd,
1629 (uintptr_t)uio->uio_iov->iov_base,
1630 (unsigned long)(unsigned int)len, pcc->fflags, ioflag);
1632 error = cuse_client_receive_command_locked(pccmd, 0, 0);
1636 error = cuse_convert_error(error);
1638 } else if (error == len) {
1639 error = uiomove(NULL, error, uio);
1643 error = uiomove(NULL, error, uio);
1647 cuse_cmd_unlock(pccmd);
1649 uio->uio_segflg = UIO_USERSPACE;/* restore segment flag */
1651 if (error == EWOULDBLOCK)
1652 cuse_client_kqfilter_poll(dev, pcc);
1658 cuse_client_ioctl(struct cdev *dev, unsigned long cmd,
1659 caddr_t data, int fflag, struct thread *td)
1661 struct cuse_client_command *pccmd;
1662 struct cuse_client *pcc;
1666 error = cuse_client_get(&pcc);
1670 len = IOCPARM_LEN(cmd);
1671 if (len > CUSE_BUFFER_MAX)
1674 pccmd = &pcc->cmds[CUSE_CMD_IOCTL];
1676 cuse_cmd_lock(pccmd);
1678 if (cmd & (IOC_IN | IOC_VOID))
1679 memcpy(pcc->ioctl_buffer, data, len);
1682 * When the ioctl-length is zero drivers can pass information
1683 * through the data pointer of the ioctl. Make sure this information
1684 * is forwarded to the driver.
1688 cuse_client_send_command_locked(pccmd,
1689 (len == 0) ? *(long *)data : CUSE_BUF_MIN_PTR,
1690 (unsigned long)cmd, pcc->fflags,
1691 (fflag & O_NONBLOCK) ? IO_NDELAY : 0);
1693 error = cuse_client_receive_command_locked(pccmd, data, len);
1697 error = cuse_convert_error(error);
1703 memcpy(data, pcc->ioctl_buffer, len);
1705 cuse_cmd_unlock(pccmd);
1707 if (error == EWOULDBLOCK)
1708 cuse_client_kqfilter_poll(dev, pcc);
1714 cuse_client_poll(struct cdev *dev, int events, struct thread *td)
1716 struct cuse_client_command *pccmd;
1717 struct cuse_client *pcc;
1722 error = cuse_client_get(&pcc);
1728 if (events & (POLLPRI | POLLIN | POLLRDNORM))
1729 temp |= CUSE_POLL_READ;
1731 if (events & (POLLOUT | POLLWRNORM))
1732 temp |= CUSE_POLL_WRITE;
1734 if (events & POLLHUP)
1735 temp |= CUSE_POLL_ERROR;
1737 pccmd = &pcc->cmds[CUSE_CMD_POLL];
1739 cuse_cmd_lock(pccmd);
1741 /* Need to selrecord() first to not loose any events. */
1742 if (temp != 0 && td != NULL)
1743 selrecord(td, &pcc->server->selinfo);
1746 cuse_client_send_command_locked(pccmd,
1747 0, temp, pcc->fflags, IO_NDELAY);
1749 error = cuse_client_receive_command_locked(pccmd, 0, 0);
1752 cuse_cmd_unlock(pccmd);
1758 if (error & CUSE_POLL_READ)
1759 revents |= (events & (POLLPRI | POLLIN | POLLRDNORM));
1760 if (error & CUSE_POLL_WRITE)
1761 revents |= (events & (POLLOUT | POLLWRNORM));
1762 if (error & CUSE_POLL_ERROR)
1763 revents |= (events & POLLHUP);
1768 /* XXX many clients don't understand POLLNVAL */
1769 return (events & (POLLHUP | POLLPRI | POLLIN |
1770 POLLRDNORM | POLLOUT | POLLWRNORM));
1774 cuse_client_mmap_single(struct cdev *dev, vm_ooffset_t *offset,
1775 vm_size_t size, struct vm_object **object, int nprot)
1777 uint32_t page_nr = *offset / PAGE_SIZE;
1778 uint32_t alloc_nr = page_nr / CUSE_ALLOC_PAGES_MAX;
1779 struct cuse_memory *mem;
1780 struct cuse_client *pcc;
1783 error = cuse_client_get(&pcc);
1788 /* lookup memory structure */
1789 TAILQ_FOREACH(mem, &pcc->server->hmem, entry) {
1790 if (mem->alloc_nr == alloc_nr)
1797 /* verify page offset */
1798 page_nr %= CUSE_ALLOC_PAGES_MAX;
1799 if (page_nr >= mem->page_count) {
1803 /* verify mmap size */
1804 if ((size % PAGE_SIZE) != 0 || (size < PAGE_SIZE) ||
1805 (size > ((mem->page_count - page_nr) * PAGE_SIZE))) {
1809 vm_object_reference(mem->object);
1810 *object = mem->object;
1813 /* set new VM object offset to use */
1814 *offset = page_nr * PAGE_SIZE;
1821 cuse_client_kqfilter_read_detach(struct knote *kn)
1823 struct cuse_client *pcc;
1827 knlist_remove(&pcc->server->selinfo.si_note, kn, 1);
1832 cuse_client_kqfilter_write_detach(struct knote *kn)
1834 struct cuse_client *pcc;
1838 knlist_remove(&pcc->server->selinfo.si_note, kn, 1);
1843 cuse_client_kqfilter_read_event(struct knote *kn, long hint)
1845 struct cuse_client *pcc;
1847 mtx_assert(&cuse_mtx, MA_OWNED);
1850 return ((pcc->cflags & CUSE_CLI_KNOTE_NEED_READ) ? 1 : 0);
1854 cuse_client_kqfilter_write_event(struct knote *kn, long hint)
1856 struct cuse_client *pcc;
1858 mtx_assert(&cuse_mtx, MA_OWNED);
1861 return ((pcc->cflags & CUSE_CLI_KNOTE_NEED_WRITE) ? 1 : 0);
1865 cuse_client_kqfilter(struct cdev *dev, struct knote *kn)
1867 struct cuse_client *pcc;
1868 struct cuse_server *pcs;
1871 error = cuse_client_get(&pcc);
1877 switch (kn->kn_filter) {
1879 pcc->cflags |= CUSE_CLI_KNOTE_HAS_READ;
1881 kn->kn_fop = &cuse_client_kqfilter_read_ops;
1882 knlist_add(&pcs->selinfo.si_note, kn, 1);
1885 pcc->cflags |= CUSE_CLI_KNOTE_HAS_WRITE;
1887 kn->kn_fop = &cuse_client_kqfilter_write_ops;
1888 knlist_add(&pcs->selinfo.si_note, kn, 1);
1897 cuse_client_kqfilter_poll(dev, pcc);