3 * Copyright (c) 2010-2020 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>
68 cuse_modevent(module_t mod, int type, void *data)
79 static moduledata_t cuse_mod = {
81 .evhand = &cuse_modevent,
84 DECLARE_MODULE(cuse, cuse_mod, SI_SUB_DEVFS, SI_ORDER_FIRST);
85 MODULE_VERSION(cuse, 1);
88 * Prevent cuse4bsd.ko and cuse.ko from loading at the same time by
89 * declaring support for the cuse4bsd interface in cuse.ko:
91 MODULE_VERSION(cuse4bsd, 1);
94 FEATURE(cuse, "Userspace character devices");
101 struct cuse_client_command {
102 TAILQ_ENTRY(cuse_client_command) entry;
103 struct cuse_command sub;
106 struct thread *entered;
107 struct cuse_client *client;
108 struct proc *proc_curr;
116 TAILQ_ENTRY(cuse_memory) entry;
122 struct cuse_server_dev {
123 TAILQ_ENTRY(cuse_server_dev) entry;
124 struct cuse_server *server;
125 struct cdev *kern_dev;
126 struct cuse_dev *user_dev;
130 TAILQ_ENTRY(cuse_server) entry;
131 TAILQ_HEAD(, cuse_client_command) head;
132 TAILQ_HEAD(, cuse_server_dev) hdev;
133 TAILQ_HEAD(, cuse_client) hcli;
134 TAILQ_HEAD(, cuse_memory) hmem;
137 struct selinfo selinfo;
144 TAILQ_ENTRY(cuse_client) entry;
145 TAILQ_ENTRY(cuse_client) entry_ref;
146 struct cuse_client_command cmds[CUSE_CMD_MAX];
147 struct cuse_server *server;
148 struct cuse_server_dev *server_dev;
150 uint8_t ioctl_buffer[CUSE_BUFFER_MAX] __aligned(4);
152 int fflags; /* file flags */
153 int cflags; /* client flags */
154 #define CUSE_CLI_IS_CLOSING 0x01
155 #define CUSE_CLI_KNOTE_NEED_READ 0x02
156 #define CUSE_CLI_KNOTE_NEED_WRITE 0x04
157 #define CUSE_CLI_KNOTE_HAS_READ 0x08
158 #define CUSE_CLI_KNOTE_HAS_WRITE 0x10
161 #define CUSE_CLIENT_CLOSING(pcc) \
162 ((pcc)->cflags & CUSE_CLI_IS_CLOSING)
164 static MALLOC_DEFINE(M_CUSE, "cuse", "CUSE memory");
166 static TAILQ_HEAD(, cuse_server) cuse_server_head;
167 static struct mtx cuse_global_mtx;
168 static struct cdev *cuse_dev;
169 static struct cuse_server *cuse_alloc_unit[CUSE_DEVICES_MAX];
170 static int cuse_alloc_unit_id[CUSE_DEVICES_MAX];
172 static void cuse_server_wakeup_all_client_locked(struct cuse_server *pcs);
173 static void cuse_client_kqfilter_read_detach(struct knote *kn);
174 static void cuse_client_kqfilter_write_detach(struct knote *kn);
175 static int cuse_client_kqfilter_read_event(struct knote *kn, long hint);
176 static int cuse_client_kqfilter_write_event(struct knote *kn, long hint);
178 static struct filterops cuse_client_kqfilter_read_ops = {
180 .f_detach = cuse_client_kqfilter_read_detach,
181 .f_event = cuse_client_kqfilter_read_event,
184 static struct filterops cuse_client_kqfilter_write_ops = {
186 .f_detach = cuse_client_kqfilter_write_detach,
187 .f_event = cuse_client_kqfilter_write_event,
190 static d_open_t cuse_client_open;
191 static d_close_t cuse_client_close;
192 static d_ioctl_t cuse_client_ioctl;
193 static d_read_t cuse_client_read;
194 static d_write_t cuse_client_write;
195 static d_poll_t cuse_client_poll;
196 static d_mmap_single_t cuse_client_mmap_single;
197 static d_kqfilter_t cuse_client_kqfilter;
199 static struct cdevsw cuse_client_devsw = {
200 .d_version = D_VERSION,
201 .d_open = cuse_client_open,
202 .d_close = cuse_client_close,
203 .d_ioctl = cuse_client_ioctl,
204 .d_name = "cuse_client",
205 .d_flags = D_TRACKCLOSE,
206 .d_read = cuse_client_read,
207 .d_write = cuse_client_write,
208 .d_poll = cuse_client_poll,
209 .d_mmap_single = cuse_client_mmap_single,
210 .d_kqfilter = cuse_client_kqfilter,
213 static d_open_t cuse_server_open;
214 static d_close_t cuse_server_close;
215 static d_ioctl_t cuse_server_ioctl;
216 static d_read_t cuse_server_read;
217 static d_write_t cuse_server_write;
218 static d_poll_t cuse_server_poll;
219 static d_mmap_single_t cuse_server_mmap_single;
221 static struct cdevsw cuse_server_devsw = {
222 .d_version = D_VERSION,
223 .d_open = cuse_server_open,
224 .d_close = cuse_server_close,
225 .d_ioctl = cuse_server_ioctl,
226 .d_name = "cuse_server",
227 .d_flags = D_TRACKCLOSE,
228 .d_read = cuse_server_read,
229 .d_write = cuse_server_write,
230 .d_poll = cuse_server_poll,
231 .d_mmap_single = cuse_server_mmap_single,
234 static void cuse_client_is_closing(struct cuse_client *);
235 static int cuse_free_unit_by_id_locked(struct cuse_server *, int);
238 cuse_global_lock(void)
240 mtx_lock(&cuse_global_mtx);
244 cuse_global_unlock(void)
246 mtx_unlock(&cuse_global_mtx);
250 cuse_server_lock(struct cuse_server *pcs)
256 cuse_server_unlock(struct cuse_server *pcs)
258 mtx_unlock(&pcs->mtx);
262 cuse_cmd_lock(struct cuse_client_command *pccmd)
264 sx_xlock(&pccmd->sx);
268 cuse_cmd_unlock(struct cuse_client_command *pccmd)
270 sx_xunlock(&pccmd->sx);
274 cuse_kern_init(void *arg)
276 TAILQ_INIT(&cuse_server_head);
278 mtx_init(&cuse_global_mtx, "cuse-global-mtx", NULL, MTX_DEF);
280 cuse_dev = make_dev(&cuse_server_devsw, 0,
281 UID_ROOT, GID_OPERATOR, 0600, "cuse");
283 printf("Cuse v%d.%d.%d @ /dev/cuse\n",
284 (CUSE_VERSION >> 16) & 0xFF, (CUSE_VERSION >> 8) & 0xFF,
285 (CUSE_VERSION >> 0) & 0xFF);
287 SYSINIT(cuse_kern_init, SI_SUB_DEVFS, SI_ORDER_ANY, cuse_kern_init, NULL);
290 cuse_kern_uninit(void *arg)
295 printf("Cuse: Please exit all /dev/cuse instances "
296 "and processes which have used this device.\n");
298 pause("DRAIN", 2 * hz);
301 ptr = TAILQ_FIRST(&cuse_server_head);
302 cuse_global_unlock();
308 if (cuse_dev != NULL)
309 destroy_dev(cuse_dev);
311 mtx_destroy(&cuse_global_mtx);
313 SYSUNINIT(cuse_kern_uninit, SI_SUB_DEVFS, SI_ORDER_ANY, cuse_kern_uninit, 0);
316 cuse_server_get(struct cuse_server **ppcs)
318 struct cuse_server *pcs;
321 error = devfs_get_cdevpriv((void **)&pcs);
326 if (pcs->is_closing) {
335 cuse_server_is_closing(struct cuse_server *pcs)
337 struct cuse_client *pcc;
344 TAILQ_FOREACH(pcc, &pcs->hcli, entry) {
345 cuse_client_is_closing(pcc);
349 static struct cuse_client_command *
350 cuse_server_find_command(struct cuse_server *pcs, struct thread *td)
352 struct cuse_client *pcc;
358 TAILQ_FOREACH(pcc, &pcs->hcli, entry) {
359 if (CUSE_CLIENT_CLOSING(pcc))
361 for (n = 0; n != CUSE_CMD_MAX; n++) {
362 if (pcc->cmds[n].entered == td)
363 return (&pcc->cmds[n]);
371 cuse_str_filter(char *ptr)
375 while (((c = *ptr) != 0)) {
376 if ((c >= 'a') && (c <= 'z')) {
380 if ((c >= 'A') && (c <= 'Z')) {
384 if ((c >= '0') && (c <= '9')) {
388 if ((c == '.') || (c == '_') || (c == '/')) {
399 cuse_convert_error(int error)
407 case CUSE_ERR_WOULDBLOCK:
408 return (EWOULDBLOCK);
409 case CUSE_ERR_INVALID:
411 case CUSE_ERR_NO_MEMORY:
415 case CUSE_ERR_SIGNAL:
417 case CUSE_ERR_NO_DEVICE:
425 cuse_vm_memory_free(struct cuse_memory *mem)
427 /* last user is gone - free */
428 vm_object_deallocate(mem->object);
430 /* free CUSE memory */
435 cuse_server_alloc_memory(struct cuse_server *pcs, uint32_t alloc_nr,
438 struct cuse_memory *temp;
439 struct cuse_memory *mem;
443 mem = malloc(sizeof(*mem), M_CUSE, M_WAITOK | M_ZERO);
445 object = vm_pager_allocate(OBJT_SWAP, NULL, PAGE_SIZE * page_count,
446 VM_PROT_DEFAULT, 0, curthread->td_ucred);
447 if (object == NULL) {
452 cuse_server_lock(pcs);
453 /* check if allocation number already exists */
454 TAILQ_FOREACH(temp, &pcs->hmem, entry) {
455 if (temp->alloc_nr == alloc_nr)
459 cuse_server_unlock(pcs);
463 mem->object = object;
464 mem->page_count = page_count;
465 mem->alloc_nr = alloc_nr;
466 TAILQ_INSERT_TAIL(&pcs->hmem, mem, entry);
467 cuse_server_unlock(pcs);
472 vm_object_deallocate(object);
479 cuse_server_free_memory(struct cuse_server *pcs, uint32_t alloc_nr)
481 struct cuse_memory *mem;
483 cuse_server_lock(pcs);
484 TAILQ_FOREACH(mem, &pcs->hmem, entry) {
485 if (mem->alloc_nr == alloc_nr)
489 cuse_server_unlock(pcs);
492 TAILQ_REMOVE(&pcs->hmem, mem, entry);
493 cuse_server_unlock(pcs);
495 cuse_vm_memory_free(mem);
501 cuse_client_get(struct cuse_client **ppcc)
503 struct cuse_client *pcc;
506 /* try to get private data */
507 error = devfs_get_cdevpriv((void **)&pcc);
512 if (CUSE_CLIENT_CLOSING(pcc) || pcc->server->is_closing) {
521 cuse_client_is_closing(struct cuse_client *pcc)
523 struct cuse_client_command *pccmd;
526 if (CUSE_CLIENT_CLOSING(pcc))
529 pcc->cflags |= CUSE_CLI_IS_CLOSING;
530 pcc->server_dev = NULL;
532 for (n = 0; n != CUSE_CMD_MAX; n++) {
533 pccmd = &pcc->cmds[n];
535 if (pccmd->entry.tqe_prev != NULL) {
536 TAILQ_REMOVE(&pcc->server->head, pccmd, entry);
537 pccmd->entry.tqe_prev = NULL;
539 cv_broadcast(&pccmd->cv);
544 cuse_client_send_command_locked(struct cuse_client_command *pccmd,
545 uintptr_t data_ptr, unsigned long arg, int fflags, int ioflag)
547 unsigned long cuse_fflags = 0;
548 struct cuse_server *pcs;
551 cuse_fflags |= CUSE_FFLAG_READ;
554 cuse_fflags |= CUSE_FFLAG_WRITE;
556 if (ioflag & IO_NDELAY)
557 cuse_fflags |= CUSE_FFLAG_NONBLOCK;
558 #if defined(__LP64__)
559 if (SV_CURPROC_FLAG(SV_ILP32))
560 cuse_fflags |= CUSE_FFLAG_COMPAT32;
562 pccmd->sub.fflags = cuse_fflags;
563 pccmd->sub.data_pointer = data_ptr;
564 pccmd->sub.argument = arg;
566 pcs = pccmd->client->server;
568 if ((pccmd->entry.tqe_prev == NULL) &&
569 (CUSE_CLIENT_CLOSING(pccmd->client) == 0) &&
570 (pcs->is_closing == 0)) {
571 TAILQ_INSERT_TAIL(&pcs->head, pccmd, entry);
577 cuse_client_got_signal(struct cuse_client_command *pccmd)
579 struct cuse_server *pcs;
581 pccmd->got_signal = 1;
583 pccmd = &pccmd->client->cmds[CUSE_CMD_SIGNAL];
585 pcs = pccmd->client->server;
587 if ((pccmd->entry.tqe_prev == NULL) &&
588 (CUSE_CLIENT_CLOSING(pccmd->client) == 0) &&
589 (pcs->is_closing == 0)) {
590 TAILQ_INSERT_TAIL(&pcs->head, pccmd, entry);
596 cuse_client_receive_command_locked(struct cuse_client_command *pccmd,
597 uint8_t *arg_ptr, uint32_t arg_len)
599 struct cuse_server *pcs;
602 pcs = pccmd->client->server;
605 pccmd->proc_curr = curthread->td_proc;
607 if (CUSE_CLIENT_CLOSING(pccmd->client) || pcs->is_closing) {
608 error = CUSE_ERR_OTHER;
611 while (pccmd->command == CUSE_CMD_NONE) {
613 cv_wait(&pccmd->cv, &pcs->mtx);
615 error = cv_wait_sig(&pccmd->cv, &pcs->mtx);
618 cuse_client_got_signal(pccmd);
620 if (CUSE_CLIENT_CLOSING(pccmd->client) || pcs->is_closing) {
621 error = CUSE_ERR_OTHER;
626 error = pccmd->error;
627 pccmd->command = CUSE_CMD_NONE;
628 cv_signal(&pccmd->cv);
632 /* wait until all process references are gone */
634 pccmd->proc_curr = NULL;
636 while (pccmd->proc_refs != 0)
637 cv_wait(&pccmd->cv, &pcs->mtx);
642 /*------------------------------------------------------------------------*
644 *------------------------------------------------------------------------*/
647 cuse_server_free_dev(struct cuse_server_dev *pcsd)
649 struct cuse_server *pcs;
650 struct cuse_client *pcc;
652 /* get server pointer */
655 /* prevent creation of more devices */
656 cuse_server_lock(pcs);
657 if (pcsd->kern_dev != NULL)
658 pcsd->kern_dev->si_drv1 = NULL;
660 TAILQ_FOREACH(pcc, &pcs->hcli, entry) {
661 if (pcc->server_dev == pcsd)
662 cuse_client_is_closing(pcc);
664 cuse_server_unlock(pcs);
666 /* destroy device, if any */
667 if (pcsd->kern_dev != NULL) {
668 /* destroy device synchronously */
669 destroy_dev(pcsd->kern_dev);
675 cuse_server_unref(struct cuse_server *pcs)
677 struct cuse_server_dev *pcsd;
678 struct cuse_memory *mem;
680 cuse_server_lock(pcs);
681 if (--(pcs->refs) != 0) {
682 cuse_server_unlock(pcs);
685 cuse_server_is_closing(pcs);
686 /* final client wakeup, if any */
687 cuse_server_wakeup_all_client_locked(pcs);
690 TAILQ_REMOVE(&cuse_server_head, pcs, entry);
691 cuse_global_unlock();
693 while ((pcsd = TAILQ_FIRST(&pcs->hdev)) != NULL) {
694 TAILQ_REMOVE(&pcs->hdev, pcsd, entry);
695 cuse_server_unlock(pcs);
696 cuse_server_free_dev(pcsd);
697 cuse_server_lock(pcs);
700 cuse_free_unit_by_id_locked(pcs, -1);
702 while ((mem = TAILQ_FIRST(&pcs->hmem)) != NULL) {
703 TAILQ_REMOVE(&pcs->hmem, mem, entry);
704 cuse_server_unlock(pcs);
705 cuse_vm_memory_free(mem);
706 cuse_server_lock(pcs);
709 knlist_clear(&pcs->selinfo.si_note, 1);
710 knlist_destroy(&pcs->selinfo.si_note);
712 cuse_server_unlock(pcs);
714 seldrain(&pcs->selinfo);
716 cv_destroy(&pcs->cv);
718 mtx_destroy(&pcs->mtx);
724 cuse_server_do_close(struct cuse_server *pcs)
728 cuse_server_lock(pcs);
729 cuse_server_is_closing(pcs);
730 /* final client wakeup, if any */
731 cuse_server_wakeup_all_client_locked(pcs);
733 knlist_clear(&pcs->selinfo.si_note, 1);
736 cuse_server_unlock(pcs);
742 cuse_server_free(void *arg)
744 struct cuse_server *pcs = arg;
747 * The final server unref should be done by the server thread
748 * to prevent deadlock in the client cdevpriv destructor,
749 * which cannot destroy itself.
751 while (cuse_server_do_close(pcs) != 1)
754 /* drop final refcount */
755 cuse_server_unref(pcs);
759 cuse_server_open(struct cdev *dev, int fflags, int devtype, struct thread *td)
761 struct cuse_server *pcs;
763 pcs = malloc(sizeof(*pcs), M_CUSE, M_WAITOK | M_ZERO);
765 if (devfs_set_cdevpriv(pcs, &cuse_server_free)) {
766 printf("Cuse: Cannot set cdevpriv.\n");
770 /* store current process ID */
771 pcs->pid = curproc->p_pid;
773 TAILQ_INIT(&pcs->head);
774 TAILQ_INIT(&pcs->hdev);
775 TAILQ_INIT(&pcs->hcli);
776 TAILQ_INIT(&pcs->hmem);
778 cv_init(&pcs->cv, "cuse-server-cv");
780 mtx_init(&pcs->mtx, "cuse-server-mtx", NULL, MTX_DEF);
782 knlist_init_mtx(&pcs->selinfo.si_note, &pcs->mtx);
786 TAILQ_INSERT_TAIL(&cuse_server_head, pcs, entry);
787 cuse_global_unlock();
793 cuse_server_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
795 struct cuse_server *pcs;
797 if (cuse_server_get(&pcs) == 0)
798 cuse_server_do_close(pcs);
804 cuse_server_read(struct cdev *dev, struct uio *uio, int ioflag)
810 cuse_server_write(struct cdev *dev, struct uio *uio, int ioflag)
816 cuse_server_ioctl_copy_locked(struct cuse_server *pcs,
817 struct cuse_client_command *pccmd,
818 struct cuse_data_chunk *pchk, int isread)
824 offset = pchk->peer_ptr - CUSE_BUF_MIN_PTR;
826 if (pchk->length > CUSE_BUFFER_MAX)
829 if (offset >= CUSE_BUFFER_MAX)
832 if ((offset + pchk->length) > CUSE_BUFFER_MAX)
835 p_proc = pccmd->proc_curr;
839 if (pccmd->proc_refs < 0)
844 cuse_server_unlock(pcs);
848 (void *)pchk->local_ptr,
849 pccmd->client->ioctl_buffer + offset,
853 pccmd->client->ioctl_buffer + offset,
854 (void *)pchk->local_ptr,
858 cuse_server_lock(pcs);
862 if (pccmd->proc_curr == NULL)
863 cv_signal(&pccmd->cv);
869 cuse_proc2proc_copy(struct proc *proc_s, vm_offset_t data_s,
870 struct proc *proc_d, vm_offset_t data_d, size_t len)
873 struct proc *proc_cur;
877 proc_cur = td->td_proc;
879 if (proc_cur == proc_d) {
881 .iov_base = (caddr_t)data_d,
887 .uio_offset = (off_t)data_s,
889 .uio_segflg = UIO_USERSPACE,
895 error = proc_rwmem(proc_s, &uio);
898 } else if (proc_cur == proc_s) {
900 .iov_base = (caddr_t)data_s,
906 .uio_offset = (off_t)data_d,
908 .uio_segflg = UIO_USERSPACE,
914 error = proc_rwmem(proc_d, &uio);
923 cuse_server_data_copy_locked(struct cuse_server *pcs,
924 struct cuse_client_command *pccmd,
925 struct cuse_data_chunk *pchk, int isread)
930 p_proc = pccmd->proc_curr;
934 if (pccmd->proc_refs < 0)
939 cuse_server_unlock(pcs);
942 error = cuse_proc2proc_copy(
943 curthread->td_proc, pchk->local_ptr,
944 p_proc, pchk->peer_ptr,
947 error = cuse_proc2proc_copy(
948 p_proc, pchk->peer_ptr,
949 curthread->td_proc, pchk->local_ptr,
953 cuse_server_lock(pcs);
957 if (pccmd->proc_curr == NULL)
958 cv_signal(&pccmd->cv);
964 cuse_alloc_unit_by_id_locked(struct cuse_server *pcs, int id)
971 for (match = n = 0; n != CUSE_DEVICES_MAX; n++) {
972 if (cuse_alloc_unit[n] != NULL) {
973 if ((cuse_alloc_unit_id[n] ^ id) & CUSE_ID_MASK)
975 if ((cuse_alloc_unit_id[n] & ~CUSE_ID_MASK) == x) {
984 for (n = 0; n != CUSE_DEVICES_MAX; n++) {
985 if (cuse_alloc_unit[n] == NULL) {
986 cuse_alloc_unit[n] = pcs;
987 cuse_alloc_unit_id[n] = id | x;
996 cuse_server_wakeup_locked(struct cuse_server *pcs)
998 selwakeup(&pcs->selinfo);
999 KNOTE_LOCKED(&pcs->selinfo.si_note, 0);
1003 cuse_server_wakeup_all_client_locked(struct cuse_server *pcs)
1005 struct cuse_client *pcc;
1007 TAILQ_FOREACH(pcc, &pcs->hcli, entry) {
1008 pcc->cflags |= (CUSE_CLI_KNOTE_NEED_READ |
1009 CUSE_CLI_KNOTE_NEED_WRITE);
1011 cuse_server_wakeup_locked(pcs);
1015 cuse_free_unit_by_id_locked(struct cuse_server *pcs, int id)
1020 for (n = 0; n != CUSE_DEVICES_MAX; n++) {
1021 if (cuse_alloc_unit[n] == pcs) {
1022 if (cuse_alloc_unit_id[n] == id || id == -1) {
1023 cuse_alloc_unit[n] = NULL;
1024 cuse_alloc_unit_id[n] = 0;
1030 return (found ? 0 : EINVAL);
1034 cuse_server_ioctl(struct cdev *dev, unsigned long cmd,
1035 caddr_t data, int fflag, struct thread *td)
1037 struct cuse_server *pcs;
1040 error = cuse_server_get(&pcs);
1045 struct cuse_client_command *pccmd;
1046 struct cuse_client *pcc;
1047 struct cuse_command *pcmd;
1048 struct cuse_alloc_info *pai;
1049 struct cuse_create_dev *pcd;
1050 struct cuse_server_dev *pcsd;
1051 struct cuse_data_chunk *pchk;
1054 case CUSE_IOCTL_GET_COMMAND:
1055 pcmd = (void *)data;
1057 cuse_server_lock(pcs);
1059 while ((pccmd = TAILQ_FIRST(&pcs->head)) == NULL) {
1060 error = cv_wait_sig(&pcs->cv, &pcs->mtx);
1062 if (pcs->is_closing)
1066 cuse_server_unlock(pcs);
1071 TAILQ_REMOVE(&pcs->head, pccmd, entry);
1072 pccmd->entry.tqe_prev = NULL;
1074 pccmd->entered = curthread;
1078 cuse_server_unlock(pcs);
1082 case CUSE_IOCTL_SYNC_COMMAND:
1084 cuse_server_lock(pcs);
1085 while ((pccmd = cuse_server_find_command(pcs, curthread)) != NULL) {
1086 /* send sync command */
1087 pccmd->entered = NULL;
1088 pccmd->error = *(int *)data;
1089 pccmd->command = CUSE_CMD_SYNC;
1091 /* signal peer, if any */
1092 cv_signal(&pccmd->cv);
1094 cuse_server_unlock(pcs);
1098 case CUSE_IOCTL_ALLOC_UNIT:
1100 cuse_server_lock(pcs);
1101 n = cuse_alloc_unit_by_id_locked(pcs,
1102 CUSE_ID_DEFAULT(0));
1103 cuse_server_unlock(pcs);
1111 case CUSE_IOCTL_ALLOC_UNIT_BY_ID:
1115 n = (n & CUSE_ID_MASK);
1117 cuse_server_lock(pcs);
1118 n = cuse_alloc_unit_by_id_locked(pcs, n);
1119 cuse_server_unlock(pcs);
1127 case CUSE_IOCTL_FREE_UNIT:
1131 n = CUSE_ID_DEFAULT(n);
1133 cuse_server_lock(pcs);
1134 error = cuse_free_unit_by_id_locked(pcs, n);
1135 cuse_server_unlock(pcs);
1138 case CUSE_IOCTL_FREE_UNIT_BY_ID:
1142 cuse_server_lock(pcs);
1143 error = cuse_free_unit_by_id_locked(pcs, n);
1144 cuse_server_unlock(pcs);
1147 case CUSE_IOCTL_ALLOC_MEMORY:
1151 if (pai->alloc_nr >= CUSE_ALLOC_UNIT_MAX) {
1155 if (pai->page_count >= CUSE_ALLOC_PAGES_MAX) {
1159 error = cuse_server_alloc_memory(pcs,
1160 pai->alloc_nr, pai->page_count);
1163 case CUSE_IOCTL_FREE_MEMORY:
1166 if (pai->alloc_nr >= CUSE_ALLOC_UNIT_MAX) {
1170 error = cuse_server_free_memory(pcs, pai->alloc_nr);
1173 case CUSE_IOCTL_GET_SIG:
1175 cuse_server_lock(pcs);
1176 pccmd = cuse_server_find_command(pcs, curthread);
1178 if (pccmd != NULL) {
1179 n = pccmd->got_signal;
1180 pccmd->got_signal = 0;
1184 cuse_server_unlock(pcs);
1190 case CUSE_IOCTL_SET_PFH:
1192 cuse_server_lock(pcs);
1193 pccmd = cuse_server_find_command(pcs, curthread);
1195 if (pccmd != NULL) {
1196 pcc = pccmd->client;
1197 for (n = 0; n != CUSE_CMD_MAX; n++) {
1198 pcc->cmds[n].sub.per_file_handle = *(uintptr_t *)data;
1203 cuse_server_unlock(pcs);
1206 case CUSE_IOCTL_CREATE_DEV:
1208 error = priv_check(curthread, PRIV_DRIVER);
1216 pcd->devname[sizeof(pcd->devname) - 1] = 0;
1218 if (pcd->devname[0] == 0) {
1222 cuse_str_filter(pcd->devname);
1224 pcd->permissions &= 0777;
1226 /* try to allocate a character device */
1228 pcsd = malloc(sizeof(*pcsd), M_CUSE, M_WAITOK | M_ZERO);
1232 pcsd->user_dev = pcd->dev;
1234 pcsd->kern_dev = make_dev_credf(MAKEDEV_CHECKNAME,
1235 &cuse_client_devsw, 0, NULL, pcd->user_id, pcd->group_id,
1236 pcd->permissions, "%s", pcd->devname);
1238 if (pcsd->kern_dev == NULL) {
1243 pcsd->kern_dev->si_drv1 = pcsd;
1245 cuse_server_lock(pcs);
1246 TAILQ_INSERT_TAIL(&pcs->hdev, pcsd, entry);
1247 cuse_server_unlock(pcs);
1251 case CUSE_IOCTL_DESTROY_DEV:
1253 error = priv_check(curthread, PRIV_DRIVER);
1257 cuse_server_lock(pcs);
1261 pcsd = TAILQ_FIRST(&pcs->hdev);
1262 while (pcsd != NULL) {
1263 if (pcsd->user_dev == *(struct cuse_dev **)data) {
1264 TAILQ_REMOVE(&pcs->hdev, pcsd, entry);
1265 cuse_server_unlock(pcs);
1266 cuse_server_free_dev(pcsd);
1267 cuse_server_lock(pcs);
1269 pcsd = TAILQ_FIRST(&pcs->hdev);
1271 pcsd = TAILQ_NEXT(pcsd, entry);
1275 cuse_server_unlock(pcs);
1278 case CUSE_IOCTL_WRITE_DATA:
1279 case CUSE_IOCTL_READ_DATA:
1281 cuse_server_lock(pcs);
1282 pchk = (struct cuse_data_chunk *)data;
1284 pccmd = cuse_server_find_command(pcs, curthread);
1286 if (pccmd == NULL) {
1287 error = ENXIO; /* invalid request */
1288 } else if (pchk->peer_ptr < CUSE_BUF_MIN_PTR) {
1289 error = EFAULT; /* NULL pointer */
1290 } else if (pchk->peer_ptr < CUSE_BUF_MAX_PTR) {
1291 error = cuse_server_ioctl_copy_locked(pcs, pccmd,
1292 pchk, cmd == CUSE_IOCTL_READ_DATA);
1294 error = cuse_server_data_copy_locked(pcs, pccmd,
1295 pchk, cmd == CUSE_IOCTL_READ_DATA);
1297 cuse_server_unlock(pcs);
1300 case CUSE_IOCTL_SELWAKEUP:
1301 cuse_server_lock(pcs);
1303 * We don't know which direction caused the event.
1306 cuse_server_wakeup_all_client_locked(pcs);
1307 cuse_server_unlock(pcs);
1318 cuse_server_poll(struct cdev *dev, int events, struct thread *td)
1320 return (events & (POLLHUP | POLLPRI | POLLIN |
1321 POLLRDNORM | POLLOUT | POLLWRNORM));
1325 cuse_server_mmap_single(struct cdev *dev, vm_ooffset_t *offset,
1326 vm_size_t size, struct vm_object **object, int nprot)
1328 uint32_t page_nr = *offset / PAGE_SIZE;
1329 uint32_t alloc_nr = page_nr / CUSE_ALLOC_PAGES_MAX;
1330 struct cuse_memory *mem;
1331 struct cuse_server *pcs;
1334 error = cuse_server_get(&pcs);
1338 cuse_server_lock(pcs);
1339 /* lookup memory structure */
1340 TAILQ_FOREACH(mem, &pcs->hmem, entry) {
1341 if (mem->alloc_nr == alloc_nr)
1345 cuse_server_unlock(pcs);
1348 /* verify page offset */
1349 page_nr %= CUSE_ALLOC_PAGES_MAX;
1350 if (page_nr >= mem->page_count) {
1351 cuse_server_unlock(pcs);
1354 /* verify mmap size */
1355 if ((size % PAGE_SIZE) != 0 || (size < PAGE_SIZE) ||
1356 (size > ((mem->page_count - page_nr) * PAGE_SIZE))) {
1357 cuse_server_unlock(pcs);
1360 vm_object_reference(mem->object);
1361 *object = mem->object;
1362 cuse_server_unlock(pcs);
1364 /* set new VM object offset to use */
1365 *offset = page_nr * PAGE_SIZE;
1371 /*------------------------------------------------------------------------*
1373 *------------------------------------------------------------------------*/
1375 cuse_client_free(void *arg)
1377 struct cuse_client *pcc = arg;
1378 struct cuse_client_command *pccmd;
1379 struct cuse_server *pcs;
1384 cuse_server_lock(pcs);
1385 cuse_client_is_closing(pcc);
1386 TAILQ_REMOVE(&pcs->hcli, pcc, entry);
1387 cuse_server_unlock(pcs);
1389 for (n = 0; n != CUSE_CMD_MAX; n++) {
1390 pccmd = &pcc->cmds[n];
1392 sx_destroy(&pccmd->sx);
1393 cv_destroy(&pccmd->cv);
1398 /* drop reference on server */
1399 cuse_server_unref(pcs);
1403 cuse_client_open(struct cdev *dev, int fflags, int devtype, struct thread *td)
1405 struct cuse_client_command *pccmd;
1406 struct cuse_server_dev *pcsd;
1407 struct cuse_client *pcc;
1408 struct cuse_server *pcs;
1409 struct cuse_dev *pcd;
1413 pcsd = dev->si_drv1;
1416 pcd = pcsd->user_dev;
1418 cuse_server_lock(pcs);
1420 * Check that the refcount didn't wrap and that the
1421 * same process is not both client and server. This
1422 * can easily lead to deadlocks when destroying the
1423 * CUSE character device nodes:
1426 if (pcs->refs < 0 || pcs->pid == curproc->p_pid) {
1427 /* overflow or wrong PID */
1429 cuse_server_unlock(pcs);
1432 cuse_server_unlock(pcs);
1437 pcc = malloc(sizeof(*pcc), M_CUSE, M_WAITOK | M_ZERO);
1438 if (devfs_set_cdevpriv(pcc, &cuse_client_free)) {
1439 printf("Cuse: Cannot set cdevpriv.\n");
1440 /* drop reference on server */
1441 cuse_server_unref(pcs);
1445 pcc->fflags = fflags;
1446 pcc->server_dev = pcsd;
1449 for (n = 0; n != CUSE_CMD_MAX; n++) {
1450 pccmd = &pcc->cmds[n];
1452 pccmd->sub.dev = pcd;
1453 pccmd->sub.command = n;
1454 pccmd->client = pcc;
1456 sx_init(&pccmd->sx, "cuse-client-sx");
1457 cv_init(&pccmd->cv, "cuse-client-cv");
1460 cuse_server_lock(pcs);
1462 /* cuse_client_free() assumes that the client is listed somewhere! */
1463 /* always enqueue */
1465 TAILQ_INSERT_TAIL(&pcs->hcli, pcc, entry);
1467 /* check if server is closing */
1468 if ((pcs->is_closing != 0) || (dev->si_drv1 == NULL)) {
1473 cuse_server_unlock(pcs);
1476 devfs_clear_cdevpriv(); /* XXX bugfix */
1479 pccmd = &pcc->cmds[CUSE_CMD_OPEN];
1481 cuse_cmd_lock(pccmd);
1483 cuse_server_lock(pcs);
1484 cuse_client_send_command_locked(pccmd, 0, 0, pcc->fflags, 0);
1486 error = cuse_client_receive_command_locked(pccmd, 0, 0);
1487 cuse_server_unlock(pcs);
1490 error = cuse_convert_error(error);
1495 cuse_cmd_unlock(pccmd);
1498 devfs_clear_cdevpriv(); /* XXX bugfix */
1504 cuse_client_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
1506 struct cuse_client_command *pccmd;
1507 struct cuse_client *pcc;
1508 struct cuse_server *pcs;
1511 error = cuse_client_get(&pcc);
1515 pccmd = &pcc->cmds[CUSE_CMD_CLOSE];
1518 cuse_cmd_lock(pccmd);
1520 cuse_server_lock(pcs);
1521 cuse_client_send_command_locked(pccmd, 0, 0, pcc->fflags, 0);
1523 error = cuse_client_receive_command_locked(pccmd, 0, 0);
1524 cuse_cmd_unlock(pccmd);
1526 cuse_client_is_closing(pcc);
1527 cuse_server_unlock(pcs);
1533 cuse_client_kqfilter_poll(struct cdev *dev, struct cuse_client *pcc)
1535 struct cuse_server *pcs = pcc->server;
1538 cuse_server_lock(pcs);
1539 temp = (pcc->cflags & (CUSE_CLI_KNOTE_HAS_READ |
1540 CUSE_CLI_KNOTE_HAS_WRITE));
1541 pcc->cflags &= ~(CUSE_CLI_KNOTE_NEED_READ |
1542 CUSE_CLI_KNOTE_NEED_WRITE);
1543 cuse_server_unlock(pcs);
1546 /* get the latest polling state from the server */
1547 temp = cuse_client_poll(dev, POLLIN | POLLOUT, NULL);
1549 if (temp & (POLLIN | POLLOUT)) {
1550 cuse_server_lock(pcs);
1552 pcc->cflags |= CUSE_CLI_KNOTE_NEED_READ;
1554 pcc->cflags |= CUSE_CLI_KNOTE_NEED_WRITE;
1556 /* make sure the "knote" gets woken up */
1557 cuse_server_wakeup_locked(pcc->server);
1558 cuse_server_unlock(pcs);
1564 cuse_client_read(struct cdev *dev, struct uio *uio, int ioflag)
1566 struct cuse_client_command *pccmd;
1567 struct cuse_client *pcc;
1568 struct cuse_server *pcs;
1572 error = cuse_client_get(&pcc);
1576 pccmd = &pcc->cmds[CUSE_CMD_READ];
1579 if (uio->uio_segflg != UIO_USERSPACE) {
1582 uio->uio_segflg = UIO_NOCOPY;
1584 cuse_cmd_lock(pccmd);
1586 while (uio->uio_resid != 0) {
1587 if (uio->uio_iov->iov_len > CUSE_LENGTH_MAX) {
1591 len = uio->uio_iov->iov_len;
1593 cuse_server_lock(pcs);
1594 cuse_client_send_command_locked(pccmd,
1595 (uintptr_t)uio->uio_iov->iov_base,
1596 (unsigned long)(unsigned int)len, pcc->fflags, ioflag);
1598 error = cuse_client_receive_command_locked(pccmd, 0, 0);
1599 cuse_server_unlock(pcs);
1602 error = cuse_convert_error(error);
1604 } else if (error == len) {
1605 error = uiomove(NULL, error, uio);
1609 error = uiomove(NULL, error, uio);
1613 cuse_cmd_unlock(pccmd);
1615 uio->uio_segflg = UIO_USERSPACE;/* restore segment flag */
1617 if (error == EWOULDBLOCK)
1618 cuse_client_kqfilter_poll(dev, pcc);
1624 cuse_client_write(struct cdev *dev, struct uio *uio, int ioflag)
1626 struct cuse_client_command *pccmd;
1627 struct cuse_client *pcc;
1628 struct cuse_server *pcs;
1632 error = cuse_client_get(&pcc);
1636 pccmd = &pcc->cmds[CUSE_CMD_WRITE];
1639 if (uio->uio_segflg != UIO_USERSPACE) {
1642 uio->uio_segflg = UIO_NOCOPY;
1644 cuse_cmd_lock(pccmd);
1646 while (uio->uio_resid != 0) {
1647 if (uio->uio_iov->iov_len > CUSE_LENGTH_MAX) {
1651 len = uio->uio_iov->iov_len;
1653 cuse_server_lock(pcs);
1654 cuse_client_send_command_locked(pccmd,
1655 (uintptr_t)uio->uio_iov->iov_base,
1656 (unsigned long)(unsigned int)len, pcc->fflags, ioflag);
1658 error = cuse_client_receive_command_locked(pccmd, 0, 0);
1659 cuse_server_unlock(pcs);
1662 error = cuse_convert_error(error);
1664 } else if (error == len) {
1665 error = uiomove(NULL, error, uio);
1669 error = uiomove(NULL, error, uio);
1673 cuse_cmd_unlock(pccmd);
1675 uio->uio_segflg = UIO_USERSPACE;/* restore segment flag */
1677 if (error == EWOULDBLOCK)
1678 cuse_client_kqfilter_poll(dev, pcc);
1684 cuse_client_ioctl(struct cdev *dev, unsigned long cmd,
1685 caddr_t data, int fflag, struct thread *td)
1687 struct cuse_client_command *pccmd;
1688 struct cuse_client *pcc;
1689 struct cuse_server *pcs;
1693 error = cuse_client_get(&pcc);
1697 len = IOCPARM_LEN(cmd);
1698 if (len > CUSE_BUFFER_MAX)
1701 pccmd = &pcc->cmds[CUSE_CMD_IOCTL];
1704 cuse_cmd_lock(pccmd);
1706 if (cmd & (IOC_IN | IOC_VOID))
1707 memcpy(pcc->ioctl_buffer, data, len);
1710 * When the ioctl-length is zero drivers can pass information
1711 * through the data pointer of the ioctl. Make sure this information
1712 * is forwarded to the driver.
1715 cuse_server_lock(pcs);
1716 cuse_client_send_command_locked(pccmd,
1717 (len == 0) ? *(long *)data : CUSE_BUF_MIN_PTR,
1718 (unsigned long)cmd, pcc->fflags,
1719 (fflag & O_NONBLOCK) ? IO_NDELAY : 0);
1721 error = cuse_client_receive_command_locked(pccmd, data, len);
1722 cuse_server_unlock(pcs);
1725 error = cuse_convert_error(error);
1731 memcpy(data, pcc->ioctl_buffer, len);
1733 cuse_cmd_unlock(pccmd);
1735 if (error == EWOULDBLOCK)
1736 cuse_client_kqfilter_poll(dev, pcc);
1742 cuse_client_poll(struct cdev *dev, int events, struct thread *td)
1744 struct cuse_client_command *pccmd;
1745 struct cuse_client *pcc;
1746 struct cuse_server *pcs;
1751 error = cuse_client_get(&pcc);
1758 if (events & (POLLPRI | POLLIN | POLLRDNORM))
1759 temp |= CUSE_POLL_READ;
1761 if (events & (POLLOUT | POLLWRNORM))
1762 temp |= CUSE_POLL_WRITE;
1764 if (events & POLLHUP)
1765 temp |= CUSE_POLL_ERROR;
1767 pccmd = &pcc->cmds[CUSE_CMD_POLL];
1769 cuse_cmd_lock(pccmd);
1771 /* Need to selrecord() first to not loose any events. */
1772 if (temp != 0 && td != NULL)
1773 selrecord(td, &pcs->selinfo);
1775 cuse_server_lock(pcs);
1776 cuse_client_send_command_locked(pccmd,
1777 0, temp, pcc->fflags, IO_NDELAY);
1779 error = cuse_client_receive_command_locked(pccmd, 0, 0);
1780 cuse_server_unlock(pcs);
1782 cuse_cmd_unlock(pccmd);
1788 if (error & CUSE_POLL_READ)
1789 revents |= (events & (POLLPRI | POLLIN | POLLRDNORM));
1790 if (error & CUSE_POLL_WRITE)
1791 revents |= (events & (POLLOUT | POLLWRNORM));
1792 if (error & CUSE_POLL_ERROR)
1793 revents |= (events & POLLHUP);
1798 /* XXX many clients don't understand POLLNVAL */
1799 return (events & (POLLHUP | POLLPRI | POLLIN |
1800 POLLRDNORM | POLLOUT | POLLWRNORM));
1804 cuse_client_mmap_single(struct cdev *dev, vm_ooffset_t *offset,
1805 vm_size_t size, struct vm_object **object, int nprot)
1807 uint32_t page_nr = *offset / PAGE_SIZE;
1808 uint32_t alloc_nr = page_nr / CUSE_ALLOC_PAGES_MAX;
1809 struct cuse_memory *mem;
1810 struct cuse_client *pcc;
1811 struct cuse_server *pcs;
1814 error = cuse_client_get(&pcc);
1820 cuse_server_lock(pcs);
1821 /* lookup memory structure */
1822 TAILQ_FOREACH(mem, &pcs->hmem, entry) {
1823 if (mem->alloc_nr == alloc_nr)
1827 cuse_server_unlock(pcs);
1830 /* verify page offset */
1831 page_nr %= CUSE_ALLOC_PAGES_MAX;
1832 if (page_nr >= mem->page_count) {
1833 cuse_server_unlock(pcs);
1836 /* verify mmap size */
1837 if ((size % PAGE_SIZE) != 0 || (size < PAGE_SIZE) ||
1838 (size > ((mem->page_count - page_nr) * PAGE_SIZE))) {
1839 cuse_server_unlock(pcs);
1842 vm_object_reference(mem->object);
1843 *object = mem->object;
1844 cuse_server_unlock(pcs);
1846 /* set new VM object offset to use */
1847 *offset = page_nr * PAGE_SIZE;
1854 cuse_client_kqfilter_read_detach(struct knote *kn)
1856 struct cuse_client *pcc;
1857 struct cuse_server *pcs;
1862 cuse_server_lock(pcs);
1863 knlist_remove(&pcs->selinfo.si_note, kn, 1);
1864 cuse_server_unlock(pcs);
1868 cuse_client_kqfilter_write_detach(struct knote *kn)
1870 struct cuse_client *pcc;
1871 struct cuse_server *pcs;
1876 cuse_server_lock(pcs);
1877 knlist_remove(&pcs->selinfo.si_note, kn, 1);
1878 cuse_server_unlock(pcs);
1882 cuse_client_kqfilter_read_event(struct knote *kn, long hint)
1884 struct cuse_client *pcc;
1888 mtx_assert(&pcc->server->mtx, MA_OWNED);
1890 return ((pcc->cflags & CUSE_CLI_KNOTE_NEED_READ) ? 1 : 0);
1894 cuse_client_kqfilter_write_event(struct knote *kn, long hint)
1896 struct cuse_client *pcc;
1900 mtx_assert(&pcc->server->mtx, MA_OWNED);
1902 return ((pcc->cflags & CUSE_CLI_KNOTE_NEED_WRITE) ? 1 : 0);
1906 cuse_client_kqfilter(struct cdev *dev, struct knote *kn)
1908 struct cuse_client *pcc;
1909 struct cuse_server *pcs;
1912 error = cuse_client_get(&pcc);
1918 cuse_server_lock(pcs);
1919 switch (kn->kn_filter) {
1921 pcc->cflags |= CUSE_CLI_KNOTE_HAS_READ;
1923 kn->kn_fop = &cuse_client_kqfilter_read_ops;
1924 knlist_add(&pcs->selinfo.si_note, kn, 1);
1927 pcc->cflags |= CUSE_CLI_KNOTE_HAS_WRITE;
1929 kn->kn_fop = &cuse_client_kqfilter_write_ops;
1930 knlist_add(&pcs->selinfo.si_note, kn, 1);
1936 cuse_server_unlock(pcs);
1939 cuse_client_kqfilter_poll(dev, pcc);