1fa0f6e62SHans Petter Selasky /* $FreeBSD$ */ 2fa0f6e62SHans Petter Selasky /*- 3*56c49c63SHans Petter Selasky * Copyright (c) 2010-2017 Hans Petter Selasky. All rights reserved. 4fa0f6e62SHans Petter Selasky * 5fa0f6e62SHans Petter Selasky * Redistribution and use in source and binary forms, with or without 6fa0f6e62SHans Petter Selasky * modification, are permitted provided that the following conditions 7fa0f6e62SHans Petter Selasky * are met: 8fa0f6e62SHans Petter Selasky * 1. Redistributions of source code must retain the above copyright 9fa0f6e62SHans Petter Selasky * notice, this list of conditions and the following disclaimer. 10fa0f6e62SHans Petter Selasky * 2. Redistributions in binary form must reproduce the above copyright 11fa0f6e62SHans Petter Selasky * notice, this list of conditions and the following disclaimer in the 12fa0f6e62SHans Petter Selasky * documentation and/or other materials provided with the distribution. 13fa0f6e62SHans Petter Selasky * 14fa0f6e62SHans Petter Selasky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15fa0f6e62SHans Petter Selasky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16fa0f6e62SHans Petter Selasky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17fa0f6e62SHans Petter Selasky * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18fa0f6e62SHans Petter Selasky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19fa0f6e62SHans Petter Selasky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20fa0f6e62SHans Petter Selasky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21fa0f6e62SHans Petter Selasky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22fa0f6e62SHans Petter Selasky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23fa0f6e62SHans Petter Selasky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24fa0f6e62SHans Petter Selasky * SUCH DAMAGE. 25fa0f6e62SHans Petter Selasky */ 26fa0f6e62SHans Petter Selasky 27fa0f6e62SHans Petter Selasky #include "opt_compat.h" 28fa0f6e62SHans Petter Selasky 29fa0f6e62SHans Petter Selasky #include <sys/stdint.h> 30fa0f6e62SHans Petter Selasky #include <sys/stddef.h> 31fa0f6e62SHans Petter Selasky #include <sys/param.h> 32fa0f6e62SHans Petter Selasky #include <sys/types.h> 33fa0f6e62SHans Petter Selasky #include <sys/systm.h> 34fa0f6e62SHans Petter Selasky #include <sys/conf.h> 35fa0f6e62SHans Petter Selasky #include <sys/kernel.h> 36fa0f6e62SHans Petter Selasky #include <sys/bus.h> 37fa0f6e62SHans Petter Selasky #include <sys/linker_set.h> 38fa0f6e62SHans Petter Selasky #include <sys/module.h> 39fa0f6e62SHans Petter Selasky #include <sys/lock.h> 40fa0f6e62SHans Petter Selasky #include <sys/mutex.h> 41fa0f6e62SHans Petter Selasky #include <sys/condvar.h> 42fa0f6e62SHans Petter Selasky #include <sys/sysctl.h> 43fa0f6e62SHans Petter Selasky #include <sys/unistd.h> 44fa0f6e62SHans Petter Selasky #include <sys/malloc.h> 45fa0f6e62SHans Petter Selasky #include <sys/priv.h> 46fa0f6e62SHans Petter Selasky #include <sys/uio.h> 47fa0f6e62SHans Petter Selasky #include <sys/poll.h> 48fa0f6e62SHans Petter Selasky #include <sys/sx.h> 49*56c49c63SHans Petter Selasky #include <sys/rwlock.h> 50fa0f6e62SHans Petter Selasky #include <sys/queue.h> 51fa0f6e62SHans Petter Selasky #include <sys/fcntl.h> 52fa0f6e62SHans Petter Selasky #include <sys/proc.h> 53fa0f6e62SHans Petter Selasky #include <sys/vnode.h> 54fa0f6e62SHans Petter Selasky #include <sys/selinfo.h> 55fa0f6e62SHans Petter Selasky #include <sys/ptrace.h> 56fa0f6e62SHans Petter Selasky 57fa0f6e62SHans Petter Selasky #include <machine/bus.h> 58fa0f6e62SHans Petter Selasky 59fa0f6e62SHans Petter Selasky #include <vm/vm.h> 60fa0f6e62SHans Petter Selasky #include <vm/pmap.h> 61*56c49c63SHans Petter Selasky #include <vm/vm_object.h> 62*56c49c63SHans Petter Selasky #include <vm/vm_page.h> 63*56c49c63SHans Petter Selasky #include <vm/vm_pager.h> 64fa0f6e62SHans Petter Selasky 65fa0f6e62SHans Petter Selasky #include <fs/cuse/cuse_defs.h> 66fa0f6e62SHans Petter Selasky #include <fs/cuse/cuse_ioctl.h> 67fa0f6e62SHans Petter Selasky 68fa0f6e62SHans Petter Selasky MODULE_VERSION(cuse, 1); 69fa0f6e62SHans Petter Selasky 7019f1b6fbSHans Petter Selasky /* 7119f1b6fbSHans Petter Selasky * Prevent cuse4bsd.ko and cuse.ko from loading at the same time by 7219f1b6fbSHans Petter Selasky * declaring support for the cuse4bsd interface in cuse.ko: 7319f1b6fbSHans Petter Selasky */ 7419f1b6fbSHans Petter Selasky MODULE_VERSION(cuse4bsd, 1); 7519f1b6fbSHans Petter Selasky 76fa0f6e62SHans Petter Selasky #ifdef FEATURE 77fa0f6e62SHans Petter Selasky FEATURE(cuse, "Userspace character devices"); 78fa0f6e62SHans Petter Selasky #endif 79fa0f6e62SHans Petter Selasky 80fa0f6e62SHans Petter Selasky struct cuse_command; 81fa0f6e62SHans Petter Selasky struct cuse_server; 82fa0f6e62SHans Petter Selasky struct cuse_client; 83fa0f6e62SHans Petter Selasky 84fa0f6e62SHans Petter Selasky struct cuse_client_command { 85fa0f6e62SHans Petter Selasky TAILQ_ENTRY(cuse_client_command) entry; 86fa0f6e62SHans Petter Selasky struct cuse_command sub; 87fa0f6e62SHans Petter Selasky struct sx sx; 88fa0f6e62SHans Petter Selasky struct cv cv; 89fa0f6e62SHans Petter Selasky struct thread *entered; 90fa0f6e62SHans Petter Selasky struct cuse_client *client; 91fa0f6e62SHans Petter Selasky struct proc *proc_curr; 92fa0f6e62SHans Petter Selasky int proc_refs; 93fa0f6e62SHans Petter Selasky int got_signal; 94fa0f6e62SHans Petter Selasky int error; 95fa0f6e62SHans Petter Selasky int command; 96fa0f6e62SHans Petter Selasky }; 97fa0f6e62SHans Petter Selasky 98fa0f6e62SHans Petter Selasky struct cuse_memory { 99*56c49c63SHans Petter Selasky TAILQ_ENTRY(cuse_memory) entry; 100*56c49c63SHans Petter Selasky vm_object_t object; 101fa0f6e62SHans Petter Selasky uint32_t page_count; 102*56c49c63SHans Petter Selasky uint32_t alloc_nr; 103fa0f6e62SHans Petter Selasky }; 104fa0f6e62SHans Petter Selasky 105fa0f6e62SHans Petter Selasky struct cuse_server_dev { 106fa0f6e62SHans Petter Selasky TAILQ_ENTRY(cuse_server_dev) entry; 107fa0f6e62SHans Petter Selasky struct cuse_server *server; 108fa0f6e62SHans Petter Selasky struct cdev *kern_dev; 109fa0f6e62SHans Petter Selasky struct cuse_dev *user_dev; 110fa0f6e62SHans Petter Selasky }; 111fa0f6e62SHans Petter Selasky 112fa0f6e62SHans Petter Selasky struct cuse_server { 113fa0f6e62SHans Petter Selasky TAILQ_ENTRY(cuse_server) entry; 114fa0f6e62SHans Petter Selasky TAILQ_HEAD(, cuse_client_command) head; 115fa0f6e62SHans Petter Selasky TAILQ_HEAD(, cuse_server_dev) hdev; 116fa0f6e62SHans Petter Selasky TAILQ_HEAD(, cuse_client) hcli; 117*56c49c63SHans Petter Selasky TAILQ_HEAD(, cuse_memory) hmem; 118fa0f6e62SHans Petter Selasky struct cv cv; 119fa0f6e62SHans Petter Selasky struct selinfo selinfo; 120ac60e801SHans Petter Selasky pid_t pid; 121fa0f6e62SHans Petter Selasky int is_closing; 122fa0f6e62SHans Petter Selasky int refs; 123fa0f6e62SHans Petter Selasky }; 124fa0f6e62SHans Petter Selasky 125fa0f6e62SHans Petter Selasky struct cuse_client { 126fa0f6e62SHans Petter Selasky TAILQ_ENTRY(cuse_client) entry; 127fa0f6e62SHans Petter Selasky TAILQ_ENTRY(cuse_client) entry_ref; 128fa0f6e62SHans Petter Selasky struct cuse_client_command cmds[CUSE_CMD_MAX]; 129fa0f6e62SHans Petter Selasky struct cuse_server *server; 130fa0f6e62SHans Petter Selasky struct cuse_server_dev *server_dev; 131fa0f6e62SHans Petter Selasky 132fa0f6e62SHans Petter Selasky uint8_t ioctl_buffer[CUSE_BUFFER_MAX] __aligned(4); 133fa0f6e62SHans Petter Selasky 134fa0f6e62SHans Petter Selasky int fflags; /* file flags */ 135fa0f6e62SHans Petter Selasky int cflags; /* client flags */ 136fa0f6e62SHans Petter Selasky #define CUSE_CLI_IS_CLOSING 0x01 137fa0f6e62SHans Petter Selasky #define CUSE_CLI_KNOTE_NEED_READ 0x02 138fa0f6e62SHans Petter Selasky #define CUSE_CLI_KNOTE_NEED_WRITE 0x04 139fa0f6e62SHans Petter Selasky #define CUSE_CLI_KNOTE_HAS_READ 0x08 140fa0f6e62SHans Petter Selasky #define CUSE_CLI_KNOTE_HAS_WRITE 0x10 141fa0f6e62SHans Petter Selasky }; 142fa0f6e62SHans Petter Selasky 143fa0f6e62SHans Petter Selasky #define CUSE_CLIENT_CLOSING(pcc) \ 144fa0f6e62SHans Petter Selasky ((pcc)->cflags & CUSE_CLI_IS_CLOSING) 145fa0f6e62SHans Petter Selasky 146fa0f6e62SHans Petter Selasky static MALLOC_DEFINE(M_CUSE, "cuse", "CUSE memory"); 147fa0f6e62SHans Petter Selasky 148fa0f6e62SHans Petter Selasky static TAILQ_HEAD(, cuse_server) cuse_server_head; 149fa0f6e62SHans Petter Selasky static struct mtx cuse_mtx; 150fa0f6e62SHans Petter Selasky static struct cdev *cuse_dev; 151fa0f6e62SHans Petter Selasky static struct cuse_server *cuse_alloc_unit[CUSE_DEVICES_MAX]; 152fa0f6e62SHans Petter Selasky static int cuse_alloc_unit_id[CUSE_DEVICES_MAX]; 153fa0f6e62SHans Petter Selasky 15495700043SHans Petter Selasky static void cuse_server_wakeup_all_client_locked(struct cuse_server *pcs); 155fa0f6e62SHans Petter Selasky static void cuse_client_kqfilter_read_detach(struct knote *kn); 156fa0f6e62SHans Petter Selasky static void cuse_client_kqfilter_write_detach(struct knote *kn); 157fa0f6e62SHans Petter Selasky static int cuse_client_kqfilter_read_event(struct knote *kn, long hint); 158fa0f6e62SHans Petter Selasky static int cuse_client_kqfilter_write_event(struct knote *kn, long hint); 159fa0f6e62SHans Petter Selasky 160fa0f6e62SHans Petter Selasky static struct filterops cuse_client_kqfilter_read_ops = { 161fa0f6e62SHans Petter Selasky .f_isfd = 1, 162fa0f6e62SHans Petter Selasky .f_detach = cuse_client_kqfilter_read_detach, 163fa0f6e62SHans Petter Selasky .f_event = cuse_client_kqfilter_read_event, 164fa0f6e62SHans Petter Selasky }; 165fa0f6e62SHans Petter Selasky 166fa0f6e62SHans Petter Selasky static struct filterops cuse_client_kqfilter_write_ops = { 167fa0f6e62SHans Petter Selasky .f_isfd = 1, 168fa0f6e62SHans Petter Selasky .f_detach = cuse_client_kqfilter_write_detach, 169fa0f6e62SHans Petter Selasky .f_event = cuse_client_kqfilter_write_event, 170fa0f6e62SHans Petter Selasky }; 171fa0f6e62SHans Petter Selasky 172fa0f6e62SHans Petter Selasky static d_open_t cuse_client_open; 173fa0f6e62SHans Petter Selasky static d_close_t cuse_client_close; 174fa0f6e62SHans Petter Selasky static d_ioctl_t cuse_client_ioctl; 175fa0f6e62SHans Petter Selasky static d_read_t cuse_client_read; 176fa0f6e62SHans Petter Selasky static d_write_t cuse_client_write; 177fa0f6e62SHans Petter Selasky static d_poll_t cuse_client_poll; 178*56c49c63SHans Petter Selasky static d_mmap_single_t cuse_client_mmap_single; 179fa0f6e62SHans Petter Selasky static d_kqfilter_t cuse_client_kqfilter; 180fa0f6e62SHans Petter Selasky 181fa0f6e62SHans Petter Selasky static struct cdevsw cuse_client_devsw = { 182fa0f6e62SHans Petter Selasky .d_version = D_VERSION, 183fa0f6e62SHans Petter Selasky .d_open = cuse_client_open, 184fa0f6e62SHans Petter Selasky .d_close = cuse_client_close, 185fa0f6e62SHans Petter Selasky .d_ioctl = cuse_client_ioctl, 186fa0f6e62SHans Petter Selasky .d_name = "cuse_client", 187fa0f6e62SHans Petter Selasky .d_flags = D_TRACKCLOSE, 188fa0f6e62SHans Petter Selasky .d_read = cuse_client_read, 189fa0f6e62SHans Petter Selasky .d_write = cuse_client_write, 190fa0f6e62SHans Petter Selasky .d_poll = cuse_client_poll, 191*56c49c63SHans Petter Selasky .d_mmap_single = cuse_client_mmap_single, 192fa0f6e62SHans Petter Selasky .d_kqfilter = cuse_client_kqfilter, 193fa0f6e62SHans Petter Selasky }; 194fa0f6e62SHans Petter Selasky 195fa0f6e62SHans Petter Selasky static d_open_t cuse_server_open; 196fa0f6e62SHans Petter Selasky static d_close_t cuse_server_close; 197fa0f6e62SHans Petter Selasky static d_ioctl_t cuse_server_ioctl; 198fa0f6e62SHans Petter Selasky static d_read_t cuse_server_read; 199fa0f6e62SHans Petter Selasky static d_write_t cuse_server_write; 200fa0f6e62SHans Petter Selasky static d_poll_t cuse_server_poll; 201*56c49c63SHans Petter Selasky static d_mmap_single_t cuse_server_mmap_single; 202fa0f6e62SHans Petter Selasky 203fa0f6e62SHans Petter Selasky static struct cdevsw cuse_server_devsw = { 204fa0f6e62SHans Petter Selasky .d_version = D_VERSION, 205fa0f6e62SHans Petter Selasky .d_open = cuse_server_open, 206fa0f6e62SHans Petter Selasky .d_close = cuse_server_close, 207fa0f6e62SHans Petter Selasky .d_ioctl = cuse_server_ioctl, 208fa0f6e62SHans Petter Selasky .d_name = "cuse_server", 209fa0f6e62SHans Petter Selasky .d_flags = D_TRACKCLOSE, 210fa0f6e62SHans Petter Selasky .d_read = cuse_server_read, 211fa0f6e62SHans Petter Selasky .d_write = cuse_server_write, 212fa0f6e62SHans Petter Selasky .d_poll = cuse_server_poll, 213*56c49c63SHans Petter Selasky .d_mmap_single = cuse_server_mmap_single, 214fa0f6e62SHans Petter Selasky }; 215fa0f6e62SHans Petter Selasky 216fa0f6e62SHans Petter Selasky static void cuse_client_is_closing(struct cuse_client *); 217fa0f6e62SHans Petter Selasky static int cuse_free_unit_by_id_locked(struct cuse_server *, int); 218fa0f6e62SHans Petter Selasky 219fa0f6e62SHans Petter Selasky static void 220fa0f6e62SHans Petter Selasky cuse_lock(void) 221fa0f6e62SHans Petter Selasky { 222fa0f6e62SHans Petter Selasky mtx_lock(&cuse_mtx); 223fa0f6e62SHans Petter Selasky } 224fa0f6e62SHans Petter Selasky 225fa0f6e62SHans Petter Selasky static void 226fa0f6e62SHans Petter Selasky cuse_unlock(void) 227fa0f6e62SHans Petter Selasky { 228fa0f6e62SHans Petter Selasky mtx_unlock(&cuse_mtx); 229fa0f6e62SHans Petter Selasky } 230fa0f6e62SHans Petter Selasky 231fa0f6e62SHans Petter Selasky static void 232fa0f6e62SHans Petter Selasky cuse_cmd_lock(struct cuse_client_command *pccmd) 233fa0f6e62SHans Petter Selasky { 234fa0f6e62SHans Petter Selasky sx_xlock(&pccmd->sx); 235fa0f6e62SHans Petter Selasky } 236fa0f6e62SHans Petter Selasky 237fa0f6e62SHans Petter Selasky static void 238fa0f6e62SHans Petter Selasky cuse_cmd_unlock(struct cuse_client_command *pccmd) 239fa0f6e62SHans Petter Selasky { 240fa0f6e62SHans Petter Selasky sx_xunlock(&pccmd->sx); 241fa0f6e62SHans Petter Selasky } 242fa0f6e62SHans Petter Selasky 243fa0f6e62SHans Petter Selasky static void 244fa0f6e62SHans Petter Selasky cuse_kern_init(void *arg) 245fa0f6e62SHans Petter Selasky { 246fa0f6e62SHans Petter Selasky TAILQ_INIT(&cuse_server_head); 247fa0f6e62SHans Petter Selasky 248fa0f6e62SHans Petter Selasky mtx_init(&cuse_mtx, "cuse-mtx", NULL, MTX_DEF); 249fa0f6e62SHans Petter Selasky 250fa0f6e62SHans Petter Selasky cuse_dev = make_dev(&cuse_server_devsw, 0, 251fa0f6e62SHans Petter Selasky UID_ROOT, GID_OPERATOR, 0600, "cuse"); 252fa0f6e62SHans Petter Selasky 253fa0f6e62SHans Petter Selasky printf("Cuse v%d.%d.%d @ /dev/cuse\n", 254fa0f6e62SHans Petter Selasky (CUSE_VERSION >> 16) & 0xFF, (CUSE_VERSION >> 8) & 0xFF, 255fa0f6e62SHans Petter Selasky (CUSE_VERSION >> 0) & 0xFF); 256fa0f6e62SHans Petter Selasky } 257fa0f6e62SHans Petter Selasky SYSINIT(cuse_kern_init, SI_SUB_DEVFS, SI_ORDER_ANY, cuse_kern_init, 0); 258fa0f6e62SHans Petter Selasky 259fa0f6e62SHans Petter Selasky static void 260fa0f6e62SHans Petter Selasky cuse_kern_uninit(void *arg) 261fa0f6e62SHans Petter Selasky { 262fa0f6e62SHans Petter Selasky void *ptr; 263fa0f6e62SHans Petter Selasky 264fa0f6e62SHans Petter Selasky while (1) { 265fa0f6e62SHans Petter Selasky 266fa0f6e62SHans Petter Selasky printf("Cuse: Please exit all /dev/cuse instances " 267fa0f6e62SHans Petter Selasky "and processes which have used this device.\n"); 268fa0f6e62SHans Petter Selasky 269fa0f6e62SHans Petter Selasky pause("DRAIN", 2 * hz); 270fa0f6e62SHans Petter Selasky 271fa0f6e62SHans Petter Selasky cuse_lock(); 272fa0f6e62SHans Petter Selasky ptr = TAILQ_FIRST(&cuse_server_head); 273fa0f6e62SHans Petter Selasky cuse_unlock(); 274fa0f6e62SHans Petter Selasky 275fa0f6e62SHans Petter Selasky if (ptr == NULL) 276fa0f6e62SHans Petter Selasky break; 277fa0f6e62SHans Petter Selasky } 278fa0f6e62SHans Petter Selasky 279fa0f6e62SHans Petter Selasky if (cuse_dev != NULL) 280fa0f6e62SHans Petter Selasky destroy_dev(cuse_dev); 281fa0f6e62SHans Petter Selasky 282fa0f6e62SHans Petter Selasky mtx_destroy(&cuse_mtx); 283fa0f6e62SHans Petter Selasky } 284fa0f6e62SHans Petter Selasky SYSUNINIT(cuse_kern_uninit, SI_SUB_DEVFS, SI_ORDER_ANY, cuse_kern_uninit, 0); 285fa0f6e62SHans Petter Selasky 286fa0f6e62SHans Petter Selasky static int 287fa0f6e62SHans Petter Selasky cuse_server_get(struct cuse_server **ppcs) 288fa0f6e62SHans Petter Selasky { 289fa0f6e62SHans Petter Selasky struct cuse_server *pcs; 290fa0f6e62SHans Petter Selasky int error; 291fa0f6e62SHans Petter Selasky 292fa0f6e62SHans Petter Selasky error = devfs_get_cdevpriv((void **)&pcs); 293fa0f6e62SHans Petter Selasky if (error != 0) { 294fa0f6e62SHans Petter Selasky *ppcs = NULL; 295fa0f6e62SHans Petter Selasky return (error); 296fa0f6e62SHans Petter Selasky } 297fa0f6e62SHans Petter Selasky /* check if closing */ 298fa0f6e62SHans Petter Selasky cuse_lock(); 299fa0f6e62SHans Petter Selasky if (pcs->is_closing) { 300fa0f6e62SHans Petter Selasky cuse_unlock(); 301fa0f6e62SHans Petter Selasky *ppcs = NULL; 302fa0f6e62SHans Petter Selasky return (EINVAL); 303fa0f6e62SHans Petter Selasky } 304fa0f6e62SHans Petter Selasky cuse_unlock(); 305fa0f6e62SHans Petter Selasky *ppcs = pcs; 306fa0f6e62SHans Petter Selasky return (0); 307fa0f6e62SHans Petter Selasky } 308fa0f6e62SHans Petter Selasky 309fa0f6e62SHans Petter Selasky static void 310fa0f6e62SHans Petter Selasky cuse_server_is_closing(struct cuse_server *pcs) 311fa0f6e62SHans Petter Selasky { 312fa0f6e62SHans Petter Selasky struct cuse_client *pcc; 313fa0f6e62SHans Petter Selasky 314fa0f6e62SHans Petter Selasky if (pcs->is_closing) 315fa0f6e62SHans Petter Selasky return; 316fa0f6e62SHans Petter Selasky 317fa0f6e62SHans Petter Selasky pcs->is_closing = 1; 318fa0f6e62SHans Petter Selasky 319fa0f6e62SHans Petter Selasky TAILQ_FOREACH(pcc, &pcs->hcli, entry) { 320fa0f6e62SHans Petter Selasky cuse_client_is_closing(pcc); 321fa0f6e62SHans Petter Selasky } 322fa0f6e62SHans Petter Selasky } 323fa0f6e62SHans Petter Selasky 324fa0f6e62SHans Petter Selasky static struct cuse_client_command * 325fa0f6e62SHans Petter Selasky cuse_server_find_command(struct cuse_server *pcs, struct thread *td) 326fa0f6e62SHans Petter Selasky { 327fa0f6e62SHans Petter Selasky struct cuse_client *pcc; 328fa0f6e62SHans Petter Selasky int n; 329fa0f6e62SHans Petter Selasky 330fa0f6e62SHans Petter Selasky if (pcs->is_closing) 331fa0f6e62SHans Petter Selasky goto done; 332fa0f6e62SHans Petter Selasky 333fa0f6e62SHans Petter Selasky TAILQ_FOREACH(pcc, &pcs->hcli, entry) { 334fa0f6e62SHans Petter Selasky if (CUSE_CLIENT_CLOSING(pcc)) 335fa0f6e62SHans Petter Selasky continue; 336fa0f6e62SHans Petter Selasky for (n = 0; n != CUSE_CMD_MAX; n++) { 337fa0f6e62SHans Petter Selasky if (pcc->cmds[n].entered == td) 338fa0f6e62SHans Petter Selasky return (&pcc->cmds[n]); 339fa0f6e62SHans Petter Selasky } 340fa0f6e62SHans Petter Selasky } 341fa0f6e62SHans Petter Selasky done: 342fa0f6e62SHans Petter Selasky return (NULL); 343fa0f6e62SHans Petter Selasky } 344fa0f6e62SHans Petter Selasky 345fa0f6e62SHans Petter Selasky static void 346fa0f6e62SHans Petter Selasky cuse_str_filter(char *ptr) 347fa0f6e62SHans Petter Selasky { 348fa0f6e62SHans Petter Selasky int c; 349fa0f6e62SHans Petter Selasky 350fa0f6e62SHans Petter Selasky while (((c = *ptr) != 0)) { 351fa0f6e62SHans Petter Selasky 352fa0f6e62SHans Petter Selasky if ((c >= 'a') && (c <= 'z')) { 353fa0f6e62SHans Petter Selasky ptr++; 354fa0f6e62SHans Petter Selasky continue; 355fa0f6e62SHans Petter Selasky } 356fa0f6e62SHans Petter Selasky if ((c >= 'A') && (c <= 'Z')) { 357fa0f6e62SHans Petter Selasky ptr++; 358fa0f6e62SHans Petter Selasky continue; 359fa0f6e62SHans Petter Selasky } 360fa0f6e62SHans Petter Selasky if ((c >= '0') && (c <= '9')) { 361fa0f6e62SHans Petter Selasky ptr++; 362fa0f6e62SHans Petter Selasky continue; 363fa0f6e62SHans Petter Selasky } 364fa0f6e62SHans Petter Selasky if ((c == '.') || (c == '_') || (c == '/')) { 365fa0f6e62SHans Petter Selasky ptr++; 366fa0f6e62SHans Petter Selasky continue; 367fa0f6e62SHans Petter Selasky } 368fa0f6e62SHans Petter Selasky *ptr = '_'; 369fa0f6e62SHans Petter Selasky 370fa0f6e62SHans Petter Selasky ptr++; 371fa0f6e62SHans Petter Selasky } 372fa0f6e62SHans Petter Selasky } 373fa0f6e62SHans Petter Selasky 374fa0f6e62SHans Petter Selasky static int 375fa0f6e62SHans Petter Selasky cuse_convert_error(int error) 376fa0f6e62SHans Petter Selasky { 377fa0f6e62SHans Petter Selasky ; /* indent fix */ 378fa0f6e62SHans Petter Selasky switch (error) { 379fa0f6e62SHans Petter Selasky case CUSE_ERR_NONE: 380fa0f6e62SHans Petter Selasky return (0); 381fa0f6e62SHans Petter Selasky case CUSE_ERR_BUSY: 382fa0f6e62SHans Petter Selasky return (EBUSY); 383fa0f6e62SHans Petter Selasky case CUSE_ERR_WOULDBLOCK: 384fa0f6e62SHans Petter Selasky return (EWOULDBLOCK); 385fa0f6e62SHans Petter Selasky case CUSE_ERR_INVALID: 386fa0f6e62SHans Petter Selasky return (EINVAL); 387fa0f6e62SHans Petter Selasky case CUSE_ERR_NO_MEMORY: 388fa0f6e62SHans Petter Selasky return (ENOMEM); 389fa0f6e62SHans Petter Selasky case CUSE_ERR_FAULT: 390fa0f6e62SHans Petter Selasky return (EFAULT); 391fa0f6e62SHans Petter Selasky case CUSE_ERR_SIGNAL: 392fa0f6e62SHans Petter Selasky return (EINTR); 3939f16d9c9SHans Petter Selasky case CUSE_ERR_NO_DEVICE: 3949f16d9c9SHans Petter Selasky return (ENODEV); 395fa0f6e62SHans Petter Selasky default: 396fa0f6e62SHans Petter Selasky return (ENXIO); 397fa0f6e62SHans Petter Selasky } 398fa0f6e62SHans Petter Selasky } 399fa0f6e62SHans Petter Selasky 400fa0f6e62SHans Petter Selasky static void 401*56c49c63SHans Petter Selasky cuse_vm_memory_free(struct cuse_memory *mem) 402fa0f6e62SHans Petter Selasky { 403*56c49c63SHans Petter Selasky /* last user is gone - free */ 404*56c49c63SHans Petter Selasky vm_object_deallocate(mem->object); 405fa0f6e62SHans Petter Selasky 406*56c49c63SHans Petter Selasky /* free CUSE memory */ 407*56c49c63SHans Petter Selasky free(mem, M_CUSE); 408fa0f6e62SHans Petter Selasky } 409fa0f6e62SHans Petter Selasky 410fa0f6e62SHans Petter Selasky static int 411*56c49c63SHans Petter Selasky cuse_server_alloc_memory(struct cuse_server *pcs, uint32_t alloc_nr, 412*56c49c63SHans Petter Selasky uint32_t page_count) 413fa0f6e62SHans Petter Selasky { 414*56c49c63SHans Petter Selasky struct cuse_memory *temp; 415*56c49c63SHans Petter Selasky struct cuse_memory *mem; 416*56c49c63SHans Petter Selasky vm_object_t object; 417fa0f6e62SHans Petter Selasky int error; 418fa0f6e62SHans Petter Selasky 419*56c49c63SHans Petter Selasky mem = malloc(sizeof(*mem), M_CUSE, M_WAITOK | M_ZERO); 420*56c49c63SHans Petter Selasky if (mem == NULL) 421*56c49c63SHans Petter Selasky return (ENOMEM); 422fa0f6e62SHans Petter Selasky 423*56c49c63SHans Petter Selasky object = vm_pager_allocate(OBJT_SWAP, NULL, PAGE_SIZE * page_count, 424*56c49c63SHans Petter Selasky VM_PROT_DEFAULT, 0, curthread->td_ucred); 425*56c49c63SHans Petter Selasky if (object == NULL) { 426fa0f6e62SHans Petter Selasky error = ENOMEM; 427*56c49c63SHans Petter Selasky goto error_0; 428*56c49c63SHans Petter Selasky } 429fa0f6e62SHans Petter Selasky 430fa0f6e62SHans Petter Selasky cuse_lock(); 431*56c49c63SHans Petter Selasky /* check if allocation number already exists */ 432*56c49c63SHans Petter Selasky TAILQ_FOREACH(temp, &pcs->hmem, entry) { 433*56c49c63SHans Petter Selasky if (temp->alloc_nr == alloc_nr) 434*56c49c63SHans Petter Selasky break; 435*56c49c63SHans Petter Selasky } 436*56c49c63SHans Petter Selasky if (temp != NULL) { 437fa0f6e62SHans Petter Selasky cuse_unlock(); 438*56c49c63SHans Petter Selasky error = EBUSY; 439*56c49c63SHans Petter Selasky goto error_1; 440*56c49c63SHans Petter Selasky } 441*56c49c63SHans Petter Selasky mem->object = object; 442*56c49c63SHans Petter Selasky mem->page_count = page_count; 443*56c49c63SHans Petter Selasky mem->alloc_nr = alloc_nr; 444*56c49c63SHans Petter Selasky TAILQ_INSERT_TAIL(&pcs->hmem, mem, entry); 445*56c49c63SHans Petter Selasky cuse_unlock(); 446*56c49c63SHans Petter Selasky 447*56c49c63SHans Petter Selasky return (0); 448*56c49c63SHans Petter Selasky 449*56c49c63SHans Petter Selasky error_1: 450*56c49c63SHans Petter Selasky vm_object_deallocate(object); 451*56c49c63SHans Petter Selasky error_0: 452*56c49c63SHans Petter Selasky free(mem, M_CUSE); 453fa0f6e62SHans Petter Selasky return (error); 454fa0f6e62SHans Petter Selasky } 455*56c49c63SHans Petter Selasky 456*56c49c63SHans Petter Selasky static int 457*56c49c63SHans Petter Selasky cuse_server_free_memory(struct cuse_server *pcs, uint32_t alloc_nr) 458*56c49c63SHans Petter Selasky { 459*56c49c63SHans Petter Selasky struct cuse_memory *mem; 460*56c49c63SHans Petter Selasky 461*56c49c63SHans Petter Selasky cuse_lock(); 462*56c49c63SHans Petter Selasky TAILQ_FOREACH(mem, &pcs->hmem, entry) { 463*56c49c63SHans Petter Selasky if (mem->alloc_nr == alloc_nr) 464*56c49c63SHans Petter Selasky break; 465*56c49c63SHans Petter Selasky } 466*56c49c63SHans Petter Selasky if (mem == NULL) { 467fa0f6e62SHans Petter Selasky cuse_unlock(); 468*56c49c63SHans Petter Selasky return (EINVAL); 469*56c49c63SHans Petter Selasky } 470*56c49c63SHans Petter Selasky TAILQ_REMOVE(&pcs->hmem, mem, entry); 471*56c49c63SHans Petter Selasky cuse_unlock(); 472*56c49c63SHans Petter Selasky 473*56c49c63SHans Petter Selasky cuse_vm_memory_free(mem); 474fa0f6e62SHans Petter Selasky 475fa0f6e62SHans Petter Selasky return (0); 476fa0f6e62SHans Petter Selasky } 477fa0f6e62SHans Petter Selasky 478fa0f6e62SHans Petter Selasky static int 479fa0f6e62SHans Petter Selasky cuse_client_get(struct cuse_client **ppcc) 480fa0f6e62SHans Petter Selasky { 481fa0f6e62SHans Petter Selasky struct cuse_client *pcc; 482fa0f6e62SHans Petter Selasky int error; 483fa0f6e62SHans Petter Selasky 484fa0f6e62SHans Petter Selasky /* try to get private data */ 485fa0f6e62SHans Petter Selasky error = devfs_get_cdevpriv((void **)&pcc); 486fa0f6e62SHans Petter Selasky if (error != 0) { 487fa0f6e62SHans Petter Selasky *ppcc = NULL; 488fa0f6e62SHans Petter Selasky return (error); 489fa0f6e62SHans Petter Selasky } 490fa0f6e62SHans Petter Selasky /* check if closing */ 491fa0f6e62SHans Petter Selasky cuse_lock(); 492fa0f6e62SHans Petter Selasky if (CUSE_CLIENT_CLOSING(pcc) || pcc->server->is_closing) { 493fa0f6e62SHans Petter Selasky cuse_unlock(); 494fa0f6e62SHans Petter Selasky *ppcc = NULL; 495fa0f6e62SHans Petter Selasky return (EINVAL); 496fa0f6e62SHans Petter Selasky } 497fa0f6e62SHans Petter Selasky cuse_unlock(); 498fa0f6e62SHans Petter Selasky *ppcc = pcc; 499fa0f6e62SHans Petter Selasky return (0); 500fa0f6e62SHans Petter Selasky } 501fa0f6e62SHans Petter Selasky 502fa0f6e62SHans Petter Selasky static void 503fa0f6e62SHans Petter Selasky cuse_client_is_closing(struct cuse_client *pcc) 504fa0f6e62SHans Petter Selasky { 505fa0f6e62SHans Petter Selasky struct cuse_client_command *pccmd; 506fa0f6e62SHans Petter Selasky uint32_t n; 507fa0f6e62SHans Petter Selasky 508fa0f6e62SHans Petter Selasky if (CUSE_CLIENT_CLOSING(pcc)) 509fa0f6e62SHans Petter Selasky return; 510fa0f6e62SHans Petter Selasky 511fa0f6e62SHans Petter Selasky pcc->cflags |= CUSE_CLI_IS_CLOSING; 512fa0f6e62SHans Petter Selasky pcc->server_dev = NULL; 513fa0f6e62SHans Petter Selasky 514fa0f6e62SHans Petter Selasky for (n = 0; n != CUSE_CMD_MAX; n++) { 515fa0f6e62SHans Petter Selasky 516fa0f6e62SHans Petter Selasky pccmd = &pcc->cmds[n]; 517fa0f6e62SHans Petter Selasky 518fa0f6e62SHans Petter Selasky if (pccmd->entry.tqe_prev != NULL) { 519fa0f6e62SHans Petter Selasky TAILQ_REMOVE(&pcc->server->head, pccmd, entry); 520fa0f6e62SHans Petter Selasky pccmd->entry.tqe_prev = NULL; 521fa0f6e62SHans Petter Selasky } 522fa0f6e62SHans Petter Selasky cv_broadcast(&pccmd->cv); 523fa0f6e62SHans Petter Selasky } 524fa0f6e62SHans Petter Selasky } 525fa0f6e62SHans Petter Selasky 526fa0f6e62SHans Petter Selasky static void 527fa0f6e62SHans Petter Selasky cuse_client_send_command_locked(struct cuse_client_command *pccmd, 528beebd9aaSHans Petter Selasky uintptr_t data_ptr, unsigned long arg, int fflags, int ioflag) 529fa0f6e62SHans Petter Selasky { 530fa0f6e62SHans Petter Selasky unsigned long cuse_fflags = 0; 531fa0f6e62SHans Petter Selasky struct cuse_server *pcs; 532fa0f6e62SHans Petter Selasky 533fa0f6e62SHans Petter Selasky if (fflags & FREAD) 534fa0f6e62SHans Petter Selasky cuse_fflags |= CUSE_FFLAG_READ; 535fa0f6e62SHans Petter Selasky 536fa0f6e62SHans Petter Selasky if (fflags & FWRITE) 537fa0f6e62SHans Petter Selasky cuse_fflags |= CUSE_FFLAG_WRITE; 538fa0f6e62SHans Petter Selasky 539fa0f6e62SHans Petter Selasky if (ioflag & IO_NDELAY) 540fa0f6e62SHans Petter Selasky cuse_fflags |= CUSE_FFLAG_NONBLOCK; 541fa0f6e62SHans Petter Selasky 542fa0f6e62SHans Petter Selasky pccmd->sub.fflags = cuse_fflags; 543fa0f6e62SHans Petter Selasky pccmd->sub.data_pointer = data_ptr; 544fa0f6e62SHans Petter Selasky pccmd->sub.argument = arg; 545fa0f6e62SHans Petter Selasky 546fa0f6e62SHans Petter Selasky pcs = pccmd->client->server; 547fa0f6e62SHans Petter Selasky 548fa0f6e62SHans Petter Selasky if ((pccmd->entry.tqe_prev == NULL) && 549fa0f6e62SHans Petter Selasky (CUSE_CLIENT_CLOSING(pccmd->client) == 0) && 550fa0f6e62SHans Petter Selasky (pcs->is_closing == 0)) { 551fa0f6e62SHans Petter Selasky TAILQ_INSERT_TAIL(&pcs->head, pccmd, entry); 552fa0f6e62SHans Petter Selasky cv_signal(&pcs->cv); 553fa0f6e62SHans Petter Selasky } 554fa0f6e62SHans Petter Selasky } 555fa0f6e62SHans Petter Selasky 556fa0f6e62SHans Petter Selasky static void 557fa0f6e62SHans Petter Selasky cuse_client_got_signal(struct cuse_client_command *pccmd) 558fa0f6e62SHans Petter Selasky { 559fa0f6e62SHans Petter Selasky struct cuse_server *pcs; 560fa0f6e62SHans Petter Selasky 561fa0f6e62SHans Petter Selasky pccmd->got_signal = 1; 562fa0f6e62SHans Petter Selasky 563fa0f6e62SHans Petter Selasky pccmd = &pccmd->client->cmds[CUSE_CMD_SIGNAL]; 564fa0f6e62SHans Petter Selasky 565fa0f6e62SHans Petter Selasky pcs = pccmd->client->server; 566fa0f6e62SHans Petter Selasky 567fa0f6e62SHans Petter Selasky if ((pccmd->entry.tqe_prev == NULL) && 568fa0f6e62SHans Petter Selasky (CUSE_CLIENT_CLOSING(pccmd->client) == 0) && 569fa0f6e62SHans Petter Selasky (pcs->is_closing == 0)) { 570fa0f6e62SHans Petter Selasky TAILQ_INSERT_TAIL(&pcs->head, pccmd, entry); 571fa0f6e62SHans Petter Selasky cv_signal(&pcs->cv); 572fa0f6e62SHans Petter Selasky } 573fa0f6e62SHans Petter Selasky } 574fa0f6e62SHans Petter Selasky 575fa0f6e62SHans Petter Selasky static int 576fa0f6e62SHans Petter Selasky cuse_client_receive_command_locked(struct cuse_client_command *pccmd, 577fa0f6e62SHans Petter Selasky uint8_t *arg_ptr, uint32_t arg_len) 578fa0f6e62SHans Petter Selasky { 579fa0f6e62SHans Petter Selasky int error; 580fa0f6e62SHans Petter Selasky 581fa0f6e62SHans Petter Selasky error = 0; 582fa0f6e62SHans Petter Selasky 583fa0f6e62SHans Petter Selasky pccmd->proc_curr = curthread->td_proc; 584fa0f6e62SHans Petter Selasky 585fa0f6e62SHans Petter Selasky if (CUSE_CLIENT_CLOSING(pccmd->client) || 586fa0f6e62SHans Petter Selasky pccmd->client->server->is_closing) { 587fa0f6e62SHans Petter Selasky error = CUSE_ERR_OTHER; 588fa0f6e62SHans Petter Selasky goto done; 589fa0f6e62SHans Petter Selasky } 590fa0f6e62SHans Petter Selasky while (pccmd->command == CUSE_CMD_NONE) { 591fa0f6e62SHans Petter Selasky if (error != 0) { 592fa0f6e62SHans Petter Selasky cv_wait(&pccmd->cv, &cuse_mtx); 593fa0f6e62SHans Petter Selasky } else { 594fa0f6e62SHans Petter Selasky error = cv_wait_sig(&pccmd->cv, &cuse_mtx); 595fa0f6e62SHans Petter Selasky 596fa0f6e62SHans Petter Selasky if (error != 0) 597fa0f6e62SHans Petter Selasky cuse_client_got_signal(pccmd); 598fa0f6e62SHans Petter Selasky } 599fa0f6e62SHans Petter Selasky if (CUSE_CLIENT_CLOSING(pccmd->client) || 600fa0f6e62SHans Petter Selasky pccmd->client->server->is_closing) { 601fa0f6e62SHans Petter Selasky error = CUSE_ERR_OTHER; 602fa0f6e62SHans Petter Selasky goto done; 603fa0f6e62SHans Petter Selasky } 604fa0f6e62SHans Petter Selasky } 605fa0f6e62SHans Petter Selasky 606fa0f6e62SHans Petter Selasky error = pccmd->error; 607fa0f6e62SHans Petter Selasky pccmd->command = CUSE_CMD_NONE; 608fa0f6e62SHans Petter Selasky cv_signal(&pccmd->cv); 609fa0f6e62SHans Petter Selasky 610fa0f6e62SHans Petter Selasky done: 611fa0f6e62SHans Petter Selasky 612fa0f6e62SHans Petter Selasky /* wait until all process references are gone */ 613fa0f6e62SHans Petter Selasky 614fa0f6e62SHans Petter Selasky pccmd->proc_curr = NULL; 615fa0f6e62SHans Petter Selasky 616fa0f6e62SHans Petter Selasky while (pccmd->proc_refs != 0) 617fa0f6e62SHans Petter Selasky cv_wait(&pccmd->cv, &cuse_mtx); 618fa0f6e62SHans Petter Selasky 619fa0f6e62SHans Petter Selasky return (error); 620fa0f6e62SHans Petter Selasky } 621fa0f6e62SHans Petter Selasky 622fa0f6e62SHans Petter Selasky /*------------------------------------------------------------------------* 623fa0f6e62SHans Petter Selasky * CUSE SERVER PART 624fa0f6e62SHans Petter Selasky *------------------------------------------------------------------------*/ 625fa0f6e62SHans Petter Selasky 626fa0f6e62SHans Petter Selasky static void 627fa0f6e62SHans Petter Selasky cuse_server_free_dev(struct cuse_server_dev *pcsd) 628fa0f6e62SHans Petter Selasky { 629fa0f6e62SHans Petter Selasky struct cuse_server *pcs; 630fa0f6e62SHans Petter Selasky struct cuse_client *pcc; 631fa0f6e62SHans Petter Selasky 632fa0f6e62SHans Petter Selasky /* get server pointer */ 633fa0f6e62SHans Petter Selasky pcs = pcsd->server; 634fa0f6e62SHans Petter Selasky 635fa0f6e62SHans Petter Selasky /* prevent creation of more devices */ 636fa0f6e62SHans Petter Selasky cuse_lock(); 637fa0f6e62SHans Petter Selasky if (pcsd->kern_dev != NULL) 638fa0f6e62SHans Petter Selasky pcsd->kern_dev->si_drv1 = NULL; 639fa0f6e62SHans Petter Selasky 640fa0f6e62SHans Petter Selasky TAILQ_FOREACH(pcc, &pcs->hcli, entry) { 641fa0f6e62SHans Petter Selasky if (pcc->server_dev == pcsd) 642fa0f6e62SHans Petter Selasky cuse_client_is_closing(pcc); 643fa0f6e62SHans Petter Selasky } 644fa0f6e62SHans Petter Selasky cuse_unlock(); 645fa0f6e62SHans Petter Selasky 646fa0f6e62SHans Petter Selasky /* destroy device, if any */ 647fa0f6e62SHans Petter Selasky if (pcsd->kern_dev != NULL) { 648fa0f6e62SHans Petter Selasky /* destroy device synchronously */ 649fa0f6e62SHans Petter Selasky destroy_dev(pcsd->kern_dev); 650fa0f6e62SHans Petter Selasky } 651fa0f6e62SHans Petter Selasky free(pcsd, M_CUSE); 652fa0f6e62SHans Petter Selasky } 653fa0f6e62SHans Petter Selasky 654fa0f6e62SHans Petter Selasky static void 655*56c49c63SHans Petter Selasky cuse_server_unref(struct cuse_server *pcs) 656fa0f6e62SHans Petter Selasky { 657fa0f6e62SHans Petter Selasky struct cuse_server_dev *pcsd; 658*56c49c63SHans Petter Selasky struct cuse_memory *mem; 659fa0f6e62SHans Petter Selasky 660fa0f6e62SHans Petter Selasky cuse_lock(); 661fa0f6e62SHans Petter Selasky pcs->refs--; 662fa0f6e62SHans Petter Selasky if (pcs->refs != 0) { 663fa0f6e62SHans Petter Selasky cuse_unlock(); 664fa0f6e62SHans Petter Selasky return; 665fa0f6e62SHans Petter Selasky } 666fa0f6e62SHans Petter Selasky cuse_server_is_closing(pcs); 66795700043SHans Petter Selasky /* final client wakeup, if any */ 66895700043SHans Petter Selasky cuse_server_wakeup_all_client_locked(pcs); 669fa0f6e62SHans Petter Selasky 670fa0f6e62SHans Petter Selasky TAILQ_REMOVE(&cuse_server_head, pcs, entry); 671fa0f6e62SHans Petter Selasky 672fa0f6e62SHans Petter Selasky cuse_free_unit_by_id_locked(pcs, -1); 673fa0f6e62SHans Petter Selasky 674fa0f6e62SHans Petter Selasky while ((pcsd = TAILQ_FIRST(&pcs->hdev)) != NULL) { 675fa0f6e62SHans Petter Selasky TAILQ_REMOVE(&pcs->hdev, pcsd, entry); 676fa0f6e62SHans Petter Selasky cuse_unlock(); 677fa0f6e62SHans Petter Selasky cuse_server_free_dev(pcsd); 678fa0f6e62SHans Petter Selasky cuse_lock(); 679fa0f6e62SHans Petter Selasky } 680fa0f6e62SHans Petter Selasky 681*56c49c63SHans Petter Selasky while ((mem = TAILQ_FIRST(&pcs->hmem)) != NULL) { 682*56c49c63SHans Petter Selasky TAILQ_REMOVE(&pcs->hmem, mem, entry); 683*56c49c63SHans Petter Selasky cuse_unlock(); 684*56c49c63SHans Petter Selasky cuse_vm_memory_free(mem); 685*56c49c63SHans Petter Selasky cuse_lock(); 686*56c49c63SHans Petter Selasky } 687fa0f6e62SHans Petter Selasky 688fa0f6e62SHans Petter Selasky knlist_clear(&pcs->selinfo.si_note, 1); 689fa0f6e62SHans Petter Selasky knlist_destroy(&pcs->selinfo.si_note); 690fa0f6e62SHans Petter Selasky 691fa0f6e62SHans Petter Selasky cuse_unlock(); 692fa0f6e62SHans Petter Selasky 693fa0f6e62SHans Petter Selasky seldrain(&pcs->selinfo); 694fa0f6e62SHans Petter Selasky 695fa0f6e62SHans Petter Selasky cv_destroy(&pcs->cv); 696fa0f6e62SHans Petter Selasky 697fa0f6e62SHans Petter Selasky free(pcs, M_CUSE); 698fa0f6e62SHans Petter Selasky } 699fa0f6e62SHans Petter Selasky 700*56c49c63SHans Petter Selasky static void 701*56c49c63SHans Petter Selasky cuse_server_free(void *arg) 702*56c49c63SHans Petter Selasky { 703*56c49c63SHans Petter Selasky struct cuse_server *pcs = arg; 704*56c49c63SHans Petter Selasky 705*56c49c63SHans Petter Selasky /* drop refcount */ 706*56c49c63SHans Petter Selasky cuse_server_unref(pcs); 707*56c49c63SHans Petter Selasky } 708*56c49c63SHans Petter Selasky 709fa0f6e62SHans Petter Selasky static int 710fa0f6e62SHans Petter Selasky cuse_server_open(struct cdev *dev, int fflags, int devtype, struct thread *td) 711fa0f6e62SHans Petter Selasky { 712fa0f6e62SHans Petter Selasky struct cuse_server *pcs; 713fa0f6e62SHans Petter Selasky 714fa0f6e62SHans Petter Selasky pcs = malloc(sizeof(*pcs), M_CUSE, M_WAITOK | M_ZERO); 715fa0f6e62SHans Petter Selasky if (pcs == NULL) 716fa0f6e62SHans Petter Selasky return (ENOMEM); 717fa0f6e62SHans Petter Selasky 718fa0f6e62SHans Petter Selasky if (devfs_set_cdevpriv(pcs, &cuse_server_free)) { 719fa0f6e62SHans Petter Selasky printf("Cuse: Cannot set cdevpriv.\n"); 720fa0f6e62SHans Petter Selasky free(pcs, M_CUSE); 721fa0f6e62SHans Petter Selasky return (ENOMEM); 722fa0f6e62SHans Petter Selasky } 723ac60e801SHans Petter Selasky /* store current process ID */ 724ac60e801SHans Petter Selasky pcs->pid = curproc->p_pid; 725ac60e801SHans Petter Selasky 726fa0f6e62SHans Petter Selasky TAILQ_INIT(&pcs->head); 727fa0f6e62SHans Petter Selasky TAILQ_INIT(&pcs->hdev); 728fa0f6e62SHans Petter Selasky TAILQ_INIT(&pcs->hcli); 729*56c49c63SHans Petter Selasky TAILQ_INIT(&pcs->hmem); 730fa0f6e62SHans Petter Selasky 731fa0f6e62SHans Petter Selasky cv_init(&pcs->cv, "cuse-server-cv"); 732fa0f6e62SHans Petter Selasky 733fa0f6e62SHans Petter Selasky knlist_init_mtx(&pcs->selinfo.si_note, &cuse_mtx); 734fa0f6e62SHans Petter Selasky 735fa0f6e62SHans Petter Selasky cuse_lock(); 736fa0f6e62SHans Petter Selasky pcs->refs++; 737fa0f6e62SHans Petter Selasky TAILQ_INSERT_TAIL(&cuse_server_head, pcs, entry); 738fa0f6e62SHans Petter Selasky cuse_unlock(); 739fa0f6e62SHans Petter Selasky 740fa0f6e62SHans Petter Selasky return (0); 741fa0f6e62SHans Petter Selasky } 742fa0f6e62SHans Petter Selasky 743fa0f6e62SHans Petter Selasky static int 744fa0f6e62SHans Petter Selasky cuse_server_close(struct cdev *dev, int fflag, int devtype, struct thread *td) 745fa0f6e62SHans Petter Selasky { 746fa0f6e62SHans Petter Selasky struct cuse_server *pcs; 747fa0f6e62SHans Petter Selasky int error; 748fa0f6e62SHans Petter Selasky 749fa0f6e62SHans Petter Selasky error = cuse_server_get(&pcs); 750fa0f6e62SHans Petter Selasky if (error != 0) 751fa0f6e62SHans Petter Selasky goto done; 752fa0f6e62SHans Petter Selasky 753fa0f6e62SHans Petter Selasky cuse_lock(); 754fa0f6e62SHans Petter Selasky cuse_server_is_closing(pcs); 75595700043SHans Petter Selasky /* final client wakeup, if any */ 75695700043SHans Petter Selasky cuse_server_wakeup_all_client_locked(pcs); 75795700043SHans Petter Selasky 758fa0f6e62SHans Petter Selasky knlist_clear(&pcs->selinfo.si_note, 1); 759fa0f6e62SHans Petter Selasky cuse_unlock(); 760fa0f6e62SHans Petter Selasky 761fa0f6e62SHans Petter Selasky done: 762fa0f6e62SHans Petter Selasky return (0); 763fa0f6e62SHans Petter Selasky } 764fa0f6e62SHans Petter Selasky 765fa0f6e62SHans Petter Selasky static int 766fa0f6e62SHans Petter Selasky cuse_server_read(struct cdev *dev, struct uio *uio, int ioflag) 767fa0f6e62SHans Petter Selasky { 768fa0f6e62SHans Petter Selasky return (ENXIO); 769fa0f6e62SHans Petter Selasky } 770fa0f6e62SHans Petter Selasky 771fa0f6e62SHans Petter Selasky static int 772fa0f6e62SHans Petter Selasky cuse_server_write(struct cdev *dev, struct uio *uio, int ioflag) 773fa0f6e62SHans Petter Selasky { 774fa0f6e62SHans Petter Selasky return (ENXIO); 775fa0f6e62SHans Petter Selasky } 776fa0f6e62SHans Petter Selasky 777fa0f6e62SHans Petter Selasky static int 778fa0f6e62SHans Petter Selasky cuse_server_ioctl_copy_locked(struct cuse_client_command *pccmd, 779fa0f6e62SHans Petter Selasky struct cuse_data_chunk *pchk, int isread) 780fa0f6e62SHans Petter Selasky { 781fa0f6e62SHans Petter Selasky struct proc *p_proc; 782fa0f6e62SHans Petter Selasky uint32_t offset; 783fa0f6e62SHans Petter Selasky int error; 784fa0f6e62SHans Petter Selasky 785fa0f6e62SHans Petter Selasky offset = pchk->peer_ptr - CUSE_BUF_MIN_PTR; 786fa0f6e62SHans Petter Selasky 787fa0f6e62SHans Petter Selasky if (pchk->length > CUSE_BUFFER_MAX) 788fa0f6e62SHans Petter Selasky return (EFAULT); 789fa0f6e62SHans Petter Selasky 790fa0f6e62SHans Petter Selasky if (offset >= CUSE_BUFFER_MAX) 791fa0f6e62SHans Petter Selasky return (EFAULT); 792fa0f6e62SHans Petter Selasky 793fa0f6e62SHans Petter Selasky if ((offset + pchk->length) > CUSE_BUFFER_MAX) 794fa0f6e62SHans Petter Selasky return (EFAULT); 795fa0f6e62SHans Petter Selasky 796fa0f6e62SHans Petter Selasky p_proc = pccmd->proc_curr; 797fa0f6e62SHans Petter Selasky if (p_proc == NULL) 798fa0f6e62SHans Petter Selasky return (ENXIO); 799fa0f6e62SHans Petter Selasky 800fa0f6e62SHans Petter Selasky if (pccmd->proc_refs < 0) 801fa0f6e62SHans Petter Selasky return (ENOMEM); 802fa0f6e62SHans Petter Selasky 803fa0f6e62SHans Petter Selasky pccmd->proc_refs++; 804fa0f6e62SHans Petter Selasky 805fa0f6e62SHans Petter Selasky cuse_unlock(); 806fa0f6e62SHans Petter Selasky 807fa0f6e62SHans Petter Selasky if (isread == 0) { 808fa0f6e62SHans Petter Selasky error = copyin( 809fa0f6e62SHans Petter Selasky (void *)pchk->local_ptr, 810fa0f6e62SHans Petter Selasky pccmd->client->ioctl_buffer + offset, 811fa0f6e62SHans Petter Selasky pchk->length); 812fa0f6e62SHans Petter Selasky } else { 813fa0f6e62SHans Petter Selasky error = copyout( 814fa0f6e62SHans Petter Selasky pccmd->client->ioctl_buffer + offset, 815fa0f6e62SHans Petter Selasky (void *)pchk->local_ptr, 816fa0f6e62SHans Petter Selasky pchk->length); 817fa0f6e62SHans Petter Selasky } 818fa0f6e62SHans Petter Selasky 819fa0f6e62SHans Petter Selasky cuse_lock(); 820fa0f6e62SHans Petter Selasky 821fa0f6e62SHans Petter Selasky pccmd->proc_refs--; 822fa0f6e62SHans Petter Selasky 823fa0f6e62SHans Petter Selasky if (pccmd->proc_curr == NULL) 824fa0f6e62SHans Petter Selasky cv_signal(&pccmd->cv); 825fa0f6e62SHans Petter Selasky 826fa0f6e62SHans Petter Selasky return (error); 827fa0f6e62SHans Petter Selasky } 828fa0f6e62SHans Petter Selasky 829fa0f6e62SHans Petter Selasky static int 830fa0f6e62SHans Petter Selasky cuse_proc2proc_copy(struct proc *proc_s, vm_offset_t data_s, 831fa0f6e62SHans Petter Selasky struct proc *proc_d, vm_offset_t data_d, size_t len) 832fa0f6e62SHans Petter Selasky { 833fa0f6e62SHans Petter Selasky struct thread *td; 834fa0f6e62SHans Petter Selasky struct proc *proc_cur; 835fa0f6e62SHans Petter Selasky int error; 836fa0f6e62SHans Petter Selasky 837fa0f6e62SHans Petter Selasky td = curthread; 838fa0f6e62SHans Petter Selasky proc_cur = td->td_proc; 839fa0f6e62SHans Petter Selasky 840fa0f6e62SHans Petter Selasky if (proc_cur == proc_d) { 841fa0f6e62SHans Petter Selasky struct iovec iov = { 842fa0f6e62SHans Petter Selasky .iov_base = (caddr_t)data_d, 843fa0f6e62SHans Petter Selasky .iov_len = len, 844fa0f6e62SHans Petter Selasky }; 845fa0f6e62SHans Petter Selasky struct uio uio = { 846fa0f6e62SHans Petter Selasky .uio_iov = &iov, 847fa0f6e62SHans Petter Selasky .uio_iovcnt = 1, 848fa0f6e62SHans Petter Selasky .uio_offset = (off_t)data_s, 849fa0f6e62SHans Petter Selasky .uio_resid = len, 850fa0f6e62SHans Petter Selasky .uio_segflg = UIO_USERSPACE, 851fa0f6e62SHans Petter Selasky .uio_rw = UIO_READ, 852fa0f6e62SHans Petter Selasky .uio_td = td, 853fa0f6e62SHans Petter Selasky }; 854fa0f6e62SHans Petter Selasky 8555928e3b9SHans Petter Selasky PHOLD(proc_s); 856fa0f6e62SHans Petter Selasky error = proc_rwmem(proc_s, &uio); 8575928e3b9SHans Petter Selasky PRELE(proc_s); 858fa0f6e62SHans Petter Selasky 859fa0f6e62SHans Petter Selasky } else if (proc_cur == proc_s) { 860fa0f6e62SHans Petter Selasky struct iovec iov = { 861fa0f6e62SHans Petter Selasky .iov_base = (caddr_t)data_s, 862fa0f6e62SHans Petter Selasky .iov_len = len, 863fa0f6e62SHans Petter Selasky }; 864fa0f6e62SHans Petter Selasky struct uio uio = { 865fa0f6e62SHans Petter Selasky .uio_iov = &iov, 866fa0f6e62SHans Petter Selasky .uio_iovcnt = 1, 867fa0f6e62SHans Petter Selasky .uio_offset = (off_t)data_d, 868fa0f6e62SHans Petter Selasky .uio_resid = len, 869fa0f6e62SHans Petter Selasky .uio_segflg = UIO_USERSPACE, 870fa0f6e62SHans Petter Selasky .uio_rw = UIO_WRITE, 871fa0f6e62SHans Petter Selasky .uio_td = td, 872fa0f6e62SHans Petter Selasky }; 873fa0f6e62SHans Petter Selasky 8745928e3b9SHans Petter Selasky PHOLD(proc_d); 875fa0f6e62SHans Petter Selasky error = proc_rwmem(proc_d, &uio); 8765928e3b9SHans Petter Selasky PRELE(proc_d); 877fa0f6e62SHans Petter Selasky } else { 878fa0f6e62SHans Petter Selasky error = EINVAL; 879fa0f6e62SHans Petter Selasky } 880fa0f6e62SHans Petter Selasky return (error); 881fa0f6e62SHans Petter Selasky } 882fa0f6e62SHans Petter Selasky 883fa0f6e62SHans Petter Selasky static int 884fa0f6e62SHans Petter Selasky cuse_server_data_copy_locked(struct cuse_client_command *pccmd, 885fa0f6e62SHans Petter Selasky struct cuse_data_chunk *pchk, int isread) 886fa0f6e62SHans Petter Selasky { 887fa0f6e62SHans Petter Selasky struct proc *p_proc; 888fa0f6e62SHans Petter Selasky int error; 889fa0f6e62SHans Petter Selasky 890fa0f6e62SHans Petter Selasky p_proc = pccmd->proc_curr; 891fa0f6e62SHans Petter Selasky if (p_proc == NULL) 892fa0f6e62SHans Petter Selasky return (ENXIO); 893fa0f6e62SHans Petter Selasky 894fa0f6e62SHans Petter Selasky if (pccmd->proc_refs < 0) 895fa0f6e62SHans Petter Selasky return (ENOMEM); 896fa0f6e62SHans Petter Selasky 897fa0f6e62SHans Petter Selasky pccmd->proc_refs++; 898fa0f6e62SHans Petter Selasky 899fa0f6e62SHans Petter Selasky cuse_unlock(); 900fa0f6e62SHans Petter Selasky 901fa0f6e62SHans Petter Selasky if (isread == 0) { 902fa0f6e62SHans Petter Selasky error = cuse_proc2proc_copy( 903fa0f6e62SHans Petter Selasky curthread->td_proc, pchk->local_ptr, 904fa0f6e62SHans Petter Selasky p_proc, pchk->peer_ptr, 905fa0f6e62SHans Petter Selasky pchk->length); 906fa0f6e62SHans Petter Selasky } else { 907fa0f6e62SHans Petter Selasky error = cuse_proc2proc_copy( 908fa0f6e62SHans Petter Selasky p_proc, pchk->peer_ptr, 909fa0f6e62SHans Petter Selasky curthread->td_proc, pchk->local_ptr, 910fa0f6e62SHans Petter Selasky pchk->length); 911fa0f6e62SHans Petter Selasky } 912fa0f6e62SHans Petter Selasky 913fa0f6e62SHans Petter Selasky cuse_lock(); 914fa0f6e62SHans Petter Selasky 915fa0f6e62SHans Petter Selasky pccmd->proc_refs--; 916fa0f6e62SHans Petter Selasky 917fa0f6e62SHans Petter Selasky if (pccmd->proc_curr == NULL) 918fa0f6e62SHans Petter Selasky cv_signal(&pccmd->cv); 919fa0f6e62SHans Petter Selasky 920fa0f6e62SHans Petter Selasky return (error); 921fa0f6e62SHans Petter Selasky } 922fa0f6e62SHans Petter Selasky 923fa0f6e62SHans Petter Selasky static int 924fa0f6e62SHans Petter Selasky cuse_alloc_unit_by_id_locked(struct cuse_server *pcs, int id) 925fa0f6e62SHans Petter Selasky { 926fa0f6e62SHans Petter Selasky int n; 927fa0f6e62SHans Petter Selasky int x = 0; 928fa0f6e62SHans Petter Selasky int match; 929fa0f6e62SHans Petter Selasky 930fa0f6e62SHans Petter Selasky do { 931fa0f6e62SHans Petter Selasky for (match = n = 0; n != CUSE_DEVICES_MAX; n++) { 932fa0f6e62SHans Petter Selasky if (cuse_alloc_unit[n] != NULL) { 933fa0f6e62SHans Petter Selasky if ((cuse_alloc_unit_id[n] ^ id) & CUSE_ID_MASK) 934fa0f6e62SHans Petter Selasky continue; 935fa0f6e62SHans Petter Selasky if ((cuse_alloc_unit_id[n] & ~CUSE_ID_MASK) == x) { 936fa0f6e62SHans Petter Selasky x++; 937fa0f6e62SHans Petter Selasky match = 1; 938fa0f6e62SHans Petter Selasky } 939fa0f6e62SHans Petter Selasky } 940fa0f6e62SHans Petter Selasky } 941fa0f6e62SHans Petter Selasky } while (match); 942fa0f6e62SHans Petter Selasky 943fa0f6e62SHans Petter Selasky if (x < 256) { 944fa0f6e62SHans Petter Selasky for (n = 0; n != CUSE_DEVICES_MAX; n++) { 945fa0f6e62SHans Petter Selasky if (cuse_alloc_unit[n] == NULL) { 946fa0f6e62SHans Petter Selasky cuse_alloc_unit[n] = pcs; 947fa0f6e62SHans Petter Selasky cuse_alloc_unit_id[n] = id | x; 948fa0f6e62SHans Petter Selasky return (x); 949fa0f6e62SHans Petter Selasky } 950fa0f6e62SHans Petter Selasky } 951fa0f6e62SHans Petter Selasky } 952fa0f6e62SHans Petter Selasky return (-1); 953fa0f6e62SHans Petter Selasky } 954fa0f6e62SHans Petter Selasky 955fa0f6e62SHans Petter Selasky static void 956fa0f6e62SHans Petter Selasky cuse_server_wakeup_locked(struct cuse_server *pcs) 957fa0f6e62SHans Petter Selasky { 958fa0f6e62SHans Petter Selasky selwakeup(&pcs->selinfo); 959fa0f6e62SHans Petter Selasky KNOTE_LOCKED(&pcs->selinfo.si_note, 0); 960fa0f6e62SHans Petter Selasky } 961fa0f6e62SHans Petter Selasky 96295700043SHans Petter Selasky static void 96395700043SHans Petter Selasky cuse_server_wakeup_all_client_locked(struct cuse_server *pcs) 96495700043SHans Petter Selasky { 96595700043SHans Petter Selasky struct cuse_client *pcc; 96695700043SHans Petter Selasky 96795700043SHans Petter Selasky TAILQ_FOREACH(pcc, &pcs->hcli, entry) { 96895700043SHans Petter Selasky pcc->cflags |= (CUSE_CLI_KNOTE_NEED_READ | 96995700043SHans Petter Selasky CUSE_CLI_KNOTE_NEED_WRITE); 97095700043SHans Petter Selasky } 97195700043SHans Petter Selasky cuse_server_wakeup_locked(pcs); 97295700043SHans Petter Selasky } 97395700043SHans Petter Selasky 974fa0f6e62SHans Petter Selasky static int 975fa0f6e62SHans Petter Selasky cuse_free_unit_by_id_locked(struct cuse_server *pcs, int id) 976fa0f6e62SHans Petter Selasky { 977fa0f6e62SHans Petter Selasky int n; 978fa0f6e62SHans Petter Selasky int found = 0; 979fa0f6e62SHans Petter Selasky 980fa0f6e62SHans Petter Selasky for (n = 0; n != CUSE_DEVICES_MAX; n++) { 981fa0f6e62SHans Petter Selasky if (cuse_alloc_unit[n] == pcs) { 982fa0f6e62SHans Petter Selasky if (cuse_alloc_unit_id[n] == id || id == -1) { 983fa0f6e62SHans Petter Selasky cuse_alloc_unit[n] = NULL; 984fa0f6e62SHans Petter Selasky cuse_alloc_unit_id[n] = 0; 985fa0f6e62SHans Petter Selasky found = 1; 986fa0f6e62SHans Petter Selasky } 987fa0f6e62SHans Petter Selasky } 988fa0f6e62SHans Petter Selasky } 989fa0f6e62SHans Petter Selasky 990fa0f6e62SHans Petter Selasky return (found ? 0 : EINVAL); 991fa0f6e62SHans Petter Selasky } 992fa0f6e62SHans Petter Selasky 993fa0f6e62SHans Petter Selasky static int 994fa0f6e62SHans Petter Selasky cuse_server_ioctl(struct cdev *dev, unsigned long cmd, 995fa0f6e62SHans Petter Selasky caddr_t data, int fflag, struct thread *td) 996fa0f6e62SHans Petter Selasky { 997fa0f6e62SHans Petter Selasky struct cuse_server *pcs; 998fa0f6e62SHans Petter Selasky int error; 999fa0f6e62SHans Petter Selasky 1000fa0f6e62SHans Petter Selasky error = cuse_server_get(&pcs); 1001fa0f6e62SHans Petter Selasky if (error != 0) 1002fa0f6e62SHans Petter Selasky return (error); 1003fa0f6e62SHans Petter Selasky 1004fa0f6e62SHans Petter Selasky switch (cmd) { 1005fa0f6e62SHans Petter Selasky struct cuse_client_command *pccmd; 1006fa0f6e62SHans Petter Selasky struct cuse_client *pcc; 1007fa0f6e62SHans Petter Selasky struct cuse_command *pcmd; 1008fa0f6e62SHans Petter Selasky struct cuse_alloc_info *pai; 1009fa0f6e62SHans Petter Selasky struct cuse_create_dev *pcd; 1010fa0f6e62SHans Petter Selasky struct cuse_server_dev *pcsd; 1011fa0f6e62SHans Petter Selasky struct cuse_data_chunk *pchk; 1012fa0f6e62SHans Petter Selasky int n; 1013fa0f6e62SHans Petter Selasky 1014fa0f6e62SHans Petter Selasky case CUSE_IOCTL_GET_COMMAND: 1015fa0f6e62SHans Petter Selasky pcmd = (void *)data; 1016fa0f6e62SHans Petter Selasky 1017fa0f6e62SHans Petter Selasky cuse_lock(); 1018fa0f6e62SHans Petter Selasky 1019fa0f6e62SHans Petter Selasky while ((pccmd = TAILQ_FIRST(&pcs->head)) == NULL) { 1020fa0f6e62SHans Petter Selasky error = cv_wait_sig(&pcs->cv, &cuse_mtx); 1021fa0f6e62SHans Petter Selasky 1022fa0f6e62SHans Petter Selasky if (pcs->is_closing) 1023fa0f6e62SHans Petter Selasky error = ENXIO; 1024fa0f6e62SHans Petter Selasky 1025fa0f6e62SHans Petter Selasky if (error) { 1026fa0f6e62SHans Petter Selasky cuse_unlock(); 1027fa0f6e62SHans Petter Selasky return (error); 1028fa0f6e62SHans Petter Selasky } 1029fa0f6e62SHans Petter Selasky } 1030fa0f6e62SHans Petter Selasky 1031fa0f6e62SHans Petter Selasky TAILQ_REMOVE(&pcs->head, pccmd, entry); 1032fa0f6e62SHans Petter Selasky pccmd->entry.tqe_prev = NULL; 1033fa0f6e62SHans Petter Selasky 1034fa0f6e62SHans Petter Selasky pccmd->entered = curthread; 1035fa0f6e62SHans Petter Selasky 1036fa0f6e62SHans Petter Selasky *pcmd = pccmd->sub; 1037fa0f6e62SHans Petter Selasky 1038fa0f6e62SHans Petter Selasky cuse_unlock(); 1039fa0f6e62SHans Petter Selasky 1040fa0f6e62SHans Petter Selasky break; 1041fa0f6e62SHans Petter Selasky 1042fa0f6e62SHans Petter Selasky case CUSE_IOCTL_SYNC_COMMAND: 1043fa0f6e62SHans Petter Selasky 1044fa0f6e62SHans Petter Selasky cuse_lock(); 1045fa0f6e62SHans Petter Selasky while ((pccmd = cuse_server_find_command(pcs, curthread)) != NULL) { 1046fa0f6e62SHans Petter Selasky 1047fa0f6e62SHans Petter Selasky /* send sync command */ 1048fa0f6e62SHans Petter Selasky pccmd->entered = NULL; 1049fa0f6e62SHans Petter Selasky pccmd->error = *(int *)data; 1050fa0f6e62SHans Petter Selasky pccmd->command = CUSE_CMD_SYNC; 1051fa0f6e62SHans Petter Selasky 1052fa0f6e62SHans Petter Selasky /* signal peer, if any */ 1053fa0f6e62SHans Petter Selasky cv_signal(&pccmd->cv); 1054fa0f6e62SHans Petter Selasky } 1055fa0f6e62SHans Petter Selasky cuse_unlock(); 1056fa0f6e62SHans Petter Selasky 1057fa0f6e62SHans Petter Selasky break; 1058fa0f6e62SHans Petter Selasky 1059fa0f6e62SHans Petter Selasky case CUSE_IOCTL_ALLOC_UNIT: 1060fa0f6e62SHans Petter Selasky 1061fa0f6e62SHans Petter Selasky cuse_lock(); 1062fa0f6e62SHans Petter Selasky n = cuse_alloc_unit_by_id_locked(pcs, 1063fa0f6e62SHans Petter Selasky CUSE_ID_DEFAULT(0)); 1064fa0f6e62SHans Petter Selasky cuse_unlock(); 1065fa0f6e62SHans Petter Selasky 1066fa0f6e62SHans Petter Selasky if (n < 0) 1067fa0f6e62SHans Petter Selasky error = ENOMEM; 1068fa0f6e62SHans Petter Selasky else 1069fa0f6e62SHans Petter Selasky *(int *)data = n; 1070fa0f6e62SHans Petter Selasky break; 1071fa0f6e62SHans Petter Selasky 1072fa0f6e62SHans Petter Selasky case CUSE_IOCTL_ALLOC_UNIT_BY_ID: 1073fa0f6e62SHans Petter Selasky 1074fa0f6e62SHans Petter Selasky n = *(int *)data; 1075fa0f6e62SHans Petter Selasky 1076fa0f6e62SHans Petter Selasky n = (n & CUSE_ID_MASK); 1077fa0f6e62SHans Petter Selasky 1078fa0f6e62SHans Petter Selasky cuse_lock(); 1079fa0f6e62SHans Petter Selasky n = cuse_alloc_unit_by_id_locked(pcs, n); 1080fa0f6e62SHans Petter Selasky cuse_unlock(); 1081fa0f6e62SHans Petter Selasky 1082fa0f6e62SHans Petter Selasky if (n < 0) 1083fa0f6e62SHans Petter Selasky error = ENOMEM; 1084fa0f6e62SHans Petter Selasky else 1085fa0f6e62SHans Petter Selasky *(int *)data = n; 1086fa0f6e62SHans Petter Selasky break; 1087fa0f6e62SHans Petter Selasky 1088fa0f6e62SHans Petter Selasky case CUSE_IOCTL_FREE_UNIT: 1089fa0f6e62SHans Petter Selasky 1090fa0f6e62SHans Petter Selasky n = *(int *)data; 1091fa0f6e62SHans Petter Selasky 1092fa0f6e62SHans Petter Selasky n = CUSE_ID_DEFAULT(n); 1093fa0f6e62SHans Petter Selasky 1094fa0f6e62SHans Petter Selasky cuse_lock(); 1095fa0f6e62SHans Petter Selasky error = cuse_free_unit_by_id_locked(pcs, n); 1096fa0f6e62SHans Petter Selasky cuse_unlock(); 1097fa0f6e62SHans Petter Selasky break; 1098fa0f6e62SHans Petter Selasky 1099fa0f6e62SHans Petter Selasky case CUSE_IOCTL_FREE_UNIT_BY_ID: 1100fa0f6e62SHans Petter Selasky 1101fa0f6e62SHans Petter Selasky n = *(int *)data; 1102fa0f6e62SHans Petter Selasky 1103fa0f6e62SHans Petter Selasky cuse_lock(); 1104fa0f6e62SHans Petter Selasky error = cuse_free_unit_by_id_locked(pcs, n); 1105fa0f6e62SHans Petter Selasky cuse_unlock(); 1106fa0f6e62SHans Petter Selasky break; 1107fa0f6e62SHans Petter Selasky 1108fa0f6e62SHans Petter Selasky case CUSE_IOCTL_ALLOC_MEMORY: 1109fa0f6e62SHans Petter Selasky 1110fa0f6e62SHans Petter Selasky pai = (void *)data; 1111fa0f6e62SHans Petter Selasky 1112fa0f6e62SHans Petter Selasky if (pai->alloc_nr >= CUSE_ALLOC_UNIT_MAX) { 1113fa0f6e62SHans Petter Selasky error = ENOMEM; 1114fa0f6e62SHans Petter Selasky break; 1115fa0f6e62SHans Petter Selasky } 1116*56c49c63SHans Petter Selasky if (pai->page_count >= CUSE_ALLOC_PAGES_MAX) { 1117fa0f6e62SHans Petter Selasky error = ENOMEM; 1118fa0f6e62SHans Petter Selasky break; 1119fa0f6e62SHans Petter Selasky } 1120fa0f6e62SHans Petter Selasky error = cuse_server_alloc_memory(pcs, 1121*56c49c63SHans Petter Selasky pai->alloc_nr, pai->page_count); 1122fa0f6e62SHans Petter Selasky break; 1123fa0f6e62SHans Petter Selasky 1124fa0f6e62SHans Petter Selasky case CUSE_IOCTL_FREE_MEMORY: 1125fa0f6e62SHans Petter Selasky pai = (void *)data; 1126fa0f6e62SHans Petter Selasky 1127fa0f6e62SHans Petter Selasky if (pai->alloc_nr >= CUSE_ALLOC_UNIT_MAX) { 1128fa0f6e62SHans Petter Selasky error = ENOMEM; 1129fa0f6e62SHans Petter Selasky break; 1130fa0f6e62SHans Petter Selasky } 1131*56c49c63SHans Petter Selasky error = cuse_server_free_memory(pcs, pai->alloc_nr); 1132fa0f6e62SHans Petter Selasky break; 1133fa0f6e62SHans Petter Selasky 1134fa0f6e62SHans Petter Selasky case CUSE_IOCTL_GET_SIG: 1135fa0f6e62SHans Petter Selasky 1136fa0f6e62SHans Petter Selasky cuse_lock(); 1137fa0f6e62SHans Petter Selasky pccmd = cuse_server_find_command(pcs, curthread); 1138fa0f6e62SHans Petter Selasky 1139fa0f6e62SHans Petter Selasky if (pccmd != NULL) { 1140fa0f6e62SHans Petter Selasky n = pccmd->got_signal; 1141fa0f6e62SHans Petter Selasky pccmd->got_signal = 0; 1142fa0f6e62SHans Petter Selasky } else { 1143fa0f6e62SHans Petter Selasky n = 0; 1144fa0f6e62SHans Petter Selasky } 1145fa0f6e62SHans Petter Selasky cuse_unlock(); 1146fa0f6e62SHans Petter Selasky 1147fa0f6e62SHans Petter Selasky *(int *)data = n; 1148fa0f6e62SHans Petter Selasky 1149fa0f6e62SHans Petter Selasky break; 1150fa0f6e62SHans Petter Selasky 1151fa0f6e62SHans Petter Selasky case CUSE_IOCTL_SET_PFH: 1152fa0f6e62SHans Petter Selasky 1153fa0f6e62SHans Petter Selasky cuse_lock(); 1154fa0f6e62SHans Petter Selasky pccmd = cuse_server_find_command(pcs, curthread); 1155fa0f6e62SHans Petter Selasky 1156fa0f6e62SHans Petter Selasky if (pccmd != NULL) { 1157fa0f6e62SHans Petter Selasky pcc = pccmd->client; 1158fa0f6e62SHans Petter Selasky for (n = 0; n != CUSE_CMD_MAX; n++) { 11599fb69c9dSHans Petter Selasky pcc->cmds[n].sub.per_file_handle = *(uintptr_t *)data; 1160fa0f6e62SHans Petter Selasky } 1161fa0f6e62SHans Petter Selasky } else { 1162fa0f6e62SHans Petter Selasky error = ENXIO; 1163fa0f6e62SHans Petter Selasky } 1164fa0f6e62SHans Petter Selasky cuse_unlock(); 1165fa0f6e62SHans Petter Selasky break; 1166fa0f6e62SHans Petter Selasky 1167fa0f6e62SHans Petter Selasky case CUSE_IOCTL_CREATE_DEV: 1168fa0f6e62SHans Petter Selasky 1169fa0f6e62SHans Petter Selasky error = priv_check(curthread, PRIV_DRIVER); 1170fa0f6e62SHans Petter Selasky if (error) 1171fa0f6e62SHans Petter Selasky break; 1172fa0f6e62SHans Petter Selasky 1173fa0f6e62SHans Petter Selasky pcd = (void *)data; 1174fa0f6e62SHans Petter Selasky 1175fa0f6e62SHans Petter Selasky /* filter input */ 1176fa0f6e62SHans Petter Selasky 1177fa0f6e62SHans Petter Selasky pcd->devname[sizeof(pcd->devname) - 1] = 0; 1178fa0f6e62SHans Petter Selasky 1179fa0f6e62SHans Petter Selasky if (pcd->devname[0] == 0) { 1180fa0f6e62SHans Petter Selasky error = EINVAL; 1181fa0f6e62SHans Petter Selasky break; 1182fa0f6e62SHans Petter Selasky } 1183fa0f6e62SHans Petter Selasky cuse_str_filter(pcd->devname); 1184fa0f6e62SHans Petter Selasky 1185fa0f6e62SHans Petter Selasky pcd->permissions &= 0777; 1186fa0f6e62SHans Petter Selasky 1187fa0f6e62SHans Petter Selasky /* try to allocate a character device */ 1188fa0f6e62SHans Petter Selasky 1189fa0f6e62SHans Petter Selasky pcsd = malloc(sizeof(*pcsd), M_CUSE, M_WAITOK | M_ZERO); 1190fa0f6e62SHans Petter Selasky 1191fa0f6e62SHans Petter Selasky if (pcsd == NULL) { 1192fa0f6e62SHans Petter Selasky error = ENOMEM; 1193fa0f6e62SHans Petter Selasky break; 1194fa0f6e62SHans Petter Selasky } 1195fa0f6e62SHans Petter Selasky pcsd->server = pcs; 1196fa0f6e62SHans Petter Selasky 1197fa0f6e62SHans Petter Selasky pcsd->user_dev = pcd->dev; 1198fa0f6e62SHans Petter Selasky 1199fa0f6e62SHans Petter Selasky pcsd->kern_dev = make_dev_credf(MAKEDEV_CHECKNAME, 1200fa0f6e62SHans Petter Selasky &cuse_client_devsw, 0, NULL, pcd->user_id, pcd->group_id, 1201fa0f6e62SHans Petter Selasky pcd->permissions, "%s", pcd->devname); 1202fa0f6e62SHans Petter Selasky 1203fa0f6e62SHans Petter Selasky if (pcsd->kern_dev == NULL) { 1204fa0f6e62SHans Petter Selasky free(pcsd, M_CUSE); 1205fa0f6e62SHans Petter Selasky error = ENOMEM; 1206fa0f6e62SHans Petter Selasky break; 1207fa0f6e62SHans Petter Selasky } 1208fa0f6e62SHans Petter Selasky pcsd->kern_dev->si_drv1 = pcsd; 1209fa0f6e62SHans Petter Selasky 1210fa0f6e62SHans Petter Selasky cuse_lock(); 1211fa0f6e62SHans Petter Selasky TAILQ_INSERT_TAIL(&pcs->hdev, pcsd, entry); 1212fa0f6e62SHans Petter Selasky cuse_unlock(); 1213fa0f6e62SHans Petter Selasky 1214fa0f6e62SHans Petter Selasky break; 1215fa0f6e62SHans Petter Selasky 1216fa0f6e62SHans Petter Selasky case CUSE_IOCTL_DESTROY_DEV: 1217fa0f6e62SHans Petter Selasky 1218fa0f6e62SHans Petter Selasky error = priv_check(curthread, PRIV_DRIVER); 1219fa0f6e62SHans Petter Selasky if (error) 1220fa0f6e62SHans Petter Selasky break; 1221fa0f6e62SHans Petter Selasky 1222fa0f6e62SHans Petter Selasky cuse_lock(); 1223fa0f6e62SHans Petter Selasky 1224fa0f6e62SHans Petter Selasky error = EINVAL; 1225fa0f6e62SHans Petter Selasky 1226fa0f6e62SHans Petter Selasky pcsd = TAILQ_FIRST(&pcs->hdev); 1227fa0f6e62SHans Petter Selasky while (pcsd != NULL) { 1228fa0f6e62SHans Petter Selasky if (pcsd->user_dev == *(struct cuse_dev **)data) { 1229fa0f6e62SHans Petter Selasky TAILQ_REMOVE(&pcs->hdev, pcsd, entry); 1230fa0f6e62SHans Petter Selasky cuse_unlock(); 1231fa0f6e62SHans Petter Selasky cuse_server_free_dev(pcsd); 1232fa0f6e62SHans Petter Selasky cuse_lock(); 1233fa0f6e62SHans Petter Selasky error = 0; 1234fa0f6e62SHans Petter Selasky pcsd = TAILQ_FIRST(&pcs->hdev); 1235fa0f6e62SHans Petter Selasky } else { 1236fa0f6e62SHans Petter Selasky pcsd = TAILQ_NEXT(pcsd, entry); 1237fa0f6e62SHans Petter Selasky } 1238fa0f6e62SHans Petter Selasky } 1239fa0f6e62SHans Petter Selasky 1240fa0f6e62SHans Petter Selasky cuse_unlock(); 1241fa0f6e62SHans Petter Selasky break; 1242fa0f6e62SHans Petter Selasky 1243fa0f6e62SHans Petter Selasky case CUSE_IOCTL_WRITE_DATA: 1244fa0f6e62SHans Petter Selasky case CUSE_IOCTL_READ_DATA: 1245fa0f6e62SHans Petter Selasky 1246fa0f6e62SHans Petter Selasky cuse_lock(); 1247fa0f6e62SHans Petter Selasky pchk = (struct cuse_data_chunk *)data; 1248fa0f6e62SHans Petter Selasky 1249fa0f6e62SHans Petter Selasky pccmd = cuse_server_find_command(pcs, curthread); 1250fa0f6e62SHans Petter Selasky 1251fa0f6e62SHans Petter Selasky if (pccmd == NULL) { 1252fa0f6e62SHans Petter Selasky error = ENXIO; /* invalid request */ 1253fa0f6e62SHans Petter Selasky } else if (pchk->peer_ptr < CUSE_BUF_MIN_PTR) { 1254fa0f6e62SHans Petter Selasky error = EFAULT; /* NULL pointer */ 1255fa0f6e62SHans Petter Selasky } else if (pchk->peer_ptr < CUSE_BUF_MAX_PTR) { 1256fa0f6e62SHans Petter Selasky error = cuse_server_ioctl_copy_locked(pccmd, 1257fa0f6e62SHans Petter Selasky pchk, cmd == CUSE_IOCTL_READ_DATA); 1258fa0f6e62SHans Petter Selasky } else { 1259fa0f6e62SHans Petter Selasky error = cuse_server_data_copy_locked(pccmd, 1260fa0f6e62SHans Petter Selasky pchk, cmd == CUSE_IOCTL_READ_DATA); 1261fa0f6e62SHans Petter Selasky } 1262fa0f6e62SHans Petter Selasky cuse_unlock(); 1263fa0f6e62SHans Petter Selasky break; 1264fa0f6e62SHans Petter Selasky 1265fa0f6e62SHans Petter Selasky case CUSE_IOCTL_SELWAKEUP: 1266fa0f6e62SHans Petter Selasky cuse_lock(); 1267fa0f6e62SHans Petter Selasky /* 1268fa0f6e62SHans Petter Selasky * We don't know which direction caused the event. 1269fa0f6e62SHans Petter Selasky * Wakeup both! 1270fa0f6e62SHans Petter Selasky */ 127195700043SHans Petter Selasky cuse_server_wakeup_all_client_locked(pcs); 1272fa0f6e62SHans Petter Selasky cuse_unlock(); 1273fa0f6e62SHans Petter Selasky break; 1274fa0f6e62SHans Petter Selasky 1275fa0f6e62SHans Petter Selasky default: 1276fa0f6e62SHans Petter Selasky error = ENXIO; 1277fa0f6e62SHans Petter Selasky break; 1278fa0f6e62SHans Petter Selasky } 1279fa0f6e62SHans Petter Selasky return (error); 1280fa0f6e62SHans Petter Selasky } 1281fa0f6e62SHans Petter Selasky 1282fa0f6e62SHans Petter Selasky static int 1283fa0f6e62SHans Petter Selasky cuse_server_poll(struct cdev *dev, int events, struct thread *td) 1284fa0f6e62SHans Petter Selasky { 1285fa0f6e62SHans Petter Selasky return (events & (POLLHUP | POLLPRI | POLLIN | 1286fa0f6e62SHans Petter Selasky POLLRDNORM | POLLOUT | POLLWRNORM)); 1287fa0f6e62SHans Petter Selasky } 1288fa0f6e62SHans Petter Selasky 1289fa0f6e62SHans Petter Selasky static int 1290*56c49c63SHans Petter Selasky cuse_server_mmap_single(struct cdev *dev, vm_ooffset_t *offset, 1291*56c49c63SHans Petter Selasky vm_size_t size, struct vm_object **object, int nprot) 1292fa0f6e62SHans Petter Selasky { 1293*56c49c63SHans Petter Selasky uint32_t page_nr = *offset / PAGE_SIZE; 1294fa0f6e62SHans Petter Selasky uint32_t alloc_nr = page_nr / CUSE_ALLOC_PAGES_MAX; 1295fa0f6e62SHans Petter Selasky struct cuse_memory *mem; 1296fa0f6e62SHans Petter Selasky struct cuse_server *pcs; 1297fa0f6e62SHans Petter Selasky int error; 1298fa0f6e62SHans Petter Selasky 1299fa0f6e62SHans Petter Selasky error = cuse_server_get(&pcs); 1300fa0f6e62SHans Petter Selasky if (error != 0) 1301*56c49c63SHans Petter Selasky return (error); 1302fa0f6e62SHans Petter Selasky 1303fa0f6e62SHans Petter Selasky cuse_lock(); 1304*56c49c63SHans Petter Selasky /* lookup memory structure */ 1305*56c49c63SHans Petter Selasky TAILQ_FOREACH(mem, &pcs->hmem, entry) { 1306*56c49c63SHans Petter Selasky if (mem->alloc_nr == alloc_nr) 1307*56c49c63SHans Petter Selasky break; 1308fa0f6e62SHans Petter Selasky } 1309*56c49c63SHans Petter Selasky if (mem == NULL) { 1310fa0f6e62SHans Petter Selasky cuse_unlock(); 1311fa0f6e62SHans Petter Selasky return (ENOMEM); 1312fa0f6e62SHans Petter Selasky } 1313*56c49c63SHans Petter Selasky /* verify page offset */ 1314fa0f6e62SHans Petter Selasky page_nr %= CUSE_ALLOC_PAGES_MAX; 1315fa0f6e62SHans Petter Selasky if (page_nr >= mem->page_count) { 1316fa0f6e62SHans Petter Selasky cuse_unlock(); 1317fa0f6e62SHans Petter Selasky return (ENXIO); 1318fa0f6e62SHans Petter Selasky } 1319*56c49c63SHans Petter Selasky /* verify mmap size */ 1320*56c49c63SHans Petter Selasky if ((size % PAGE_SIZE) != 0 || (size < PAGE_SIZE) || 1321*56c49c63SHans Petter Selasky (size > ((mem->page_count - page_nr) * PAGE_SIZE))) { 1322*56c49c63SHans Petter Selasky cuse_unlock(); 1323*56c49c63SHans Petter Selasky return (EINVAL); 1324*56c49c63SHans Petter Selasky } 1325*56c49c63SHans Petter Selasky vm_object_reference(mem->object); 1326*56c49c63SHans Petter Selasky *object = mem->object; 1327fa0f6e62SHans Petter Selasky cuse_unlock(); 1328fa0f6e62SHans Petter Selasky 1329*56c49c63SHans Petter Selasky /* set new VM object offset to use */ 1330*56c49c63SHans Petter Selasky *offset = page_nr * PAGE_SIZE; 1331fa0f6e62SHans Petter Selasky 1332*56c49c63SHans Petter Selasky /* success */ 1333fa0f6e62SHans Petter Selasky return (0); 1334fa0f6e62SHans Petter Selasky } 1335fa0f6e62SHans Petter Selasky 1336fa0f6e62SHans Petter Selasky /*------------------------------------------------------------------------* 1337fa0f6e62SHans Petter Selasky * CUSE CLIENT PART 1338fa0f6e62SHans Petter Selasky *------------------------------------------------------------------------*/ 1339fa0f6e62SHans Petter Selasky static void 1340fa0f6e62SHans Petter Selasky cuse_client_free(void *arg) 1341fa0f6e62SHans Petter Selasky { 1342fa0f6e62SHans Petter Selasky struct cuse_client *pcc = arg; 1343fa0f6e62SHans Petter Selasky struct cuse_client_command *pccmd; 1344fa0f6e62SHans Petter Selasky struct cuse_server *pcs; 1345fa0f6e62SHans Petter Selasky int n; 1346fa0f6e62SHans Petter Selasky 1347fa0f6e62SHans Petter Selasky cuse_lock(); 1348fa0f6e62SHans Petter Selasky cuse_client_is_closing(pcc); 1349fa0f6e62SHans Petter Selasky TAILQ_REMOVE(&pcc->server->hcli, pcc, entry); 1350fa0f6e62SHans Petter Selasky cuse_unlock(); 1351fa0f6e62SHans Petter Selasky 1352fa0f6e62SHans Petter Selasky for (n = 0; n != CUSE_CMD_MAX; n++) { 1353fa0f6e62SHans Petter Selasky 1354fa0f6e62SHans Petter Selasky pccmd = &pcc->cmds[n]; 1355fa0f6e62SHans Petter Selasky 1356fa0f6e62SHans Petter Selasky sx_destroy(&pccmd->sx); 1357fa0f6e62SHans Petter Selasky cv_destroy(&pccmd->cv); 1358fa0f6e62SHans Petter Selasky } 1359fa0f6e62SHans Petter Selasky 1360fa0f6e62SHans Petter Selasky pcs = pcc->server; 1361fa0f6e62SHans Petter Selasky 1362fa0f6e62SHans Petter Selasky free(pcc, M_CUSE); 1363fa0f6e62SHans Petter Selasky 1364fa0f6e62SHans Petter Selasky /* drop reference on server */ 1365*56c49c63SHans Petter Selasky cuse_server_unref(pcs); 1366fa0f6e62SHans Petter Selasky } 1367fa0f6e62SHans Petter Selasky 1368fa0f6e62SHans Petter Selasky static int 1369fa0f6e62SHans Petter Selasky cuse_client_open(struct cdev *dev, int fflags, int devtype, struct thread *td) 1370fa0f6e62SHans Petter Selasky { 1371fa0f6e62SHans Petter Selasky struct cuse_client_command *pccmd; 1372fa0f6e62SHans Petter Selasky struct cuse_server_dev *pcsd; 1373fa0f6e62SHans Petter Selasky struct cuse_client *pcc; 1374fa0f6e62SHans Petter Selasky struct cuse_server *pcs; 1375fa0f6e62SHans Petter Selasky struct cuse_dev *pcd; 1376fa0f6e62SHans Petter Selasky int error; 1377fa0f6e62SHans Petter Selasky int n; 1378fa0f6e62SHans Petter Selasky 1379fa0f6e62SHans Petter Selasky cuse_lock(); 1380fa0f6e62SHans Petter Selasky pcsd = dev->si_drv1; 1381fa0f6e62SHans Petter Selasky if (pcsd != NULL) { 1382fa0f6e62SHans Petter Selasky pcs = pcsd->server; 1383fa0f6e62SHans Petter Selasky pcd = pcsd->user_dev; 1384ac60e801SHans Petter Selasky /* 1385ac60e801SHans Petter Selasky * Check that the refcount didn't wrap and that the 1386ac60e801SHans Petter Selasky * same process is not both client and server. This 1387ac60e801SHans Petter Selasky * can easily lead to deadlocks when destroying the 1388ac60e801SHans Petter Selasky * CUSE character device nodes: 1389ac60e801SHans Petter Selasky */ 1390fa0f6e62SHans Petter Selasky pcs->refs++; 1391ac60e801SHans Petter Selasky if (pcs->refs < 0 || pcs->pid == curproc->p_pid) { 1392ac60e801SHans Petter Selasky /* overflow or wrong PID */ 1393fa0f6e62SHans Petter Selasky pcs->refs--; 1394fa0f6e62SHans Petter Selasky pcsd = NULL; 1395fa0f6e62SHans Petter Selasky } 1396fa0f6e62SHans Petter Selasky } else { 1397fa0f6e62SHans Petter Selasky pcs = NULL; 1398fa0f6e62SHans Petter Selasky pcd = NULL; 1399fa0f6e62SHans Petter Selasky } 1400fa0f6e62SHans Petter Selasky cuse_unlock(); 1401fa0f6e62SHans Petter Selasky 1402fa0f6e62SHans Petter Selasky if (pcsd == NULL) 1403fa0f6e62SHans Petter Selasky return (EINVAL); 1404fa0f6e62SHans Petter Selasky 1405fa0f6e62SHans Petter Selasky pcc = malloc(sizeof(*pcc), M_CUSE, M_WAITOK | M_ZERO); 1406fa0f6e62SHans Petter Selasky if (pcc == NULL) { 1407fa0f6e62SHans Petter Selasky /* drop reference on server */ 1408*56c49c63SHans Petter Selasky cuse_server_unref(pcs); 1409fa0f6e62SHans Petter Selasky return (ENOMEM); 1410fa0f6e62SHans Petter Selasky } 1411fa0f6e62SHans Petter Selasky if (devfs_set_cdevpriv(pcc, &cuse_client_free)) { 1412fa0f6e62SHans Petter Selasky printf("Cuse: Cannot set cdevpriv.\n"); 1413fa0f6e62SHans Petter Selasky /* drop reference on server */ 1414*56c49c63SHans Petter Selasky cuse_server_unref(pcs); 1415fa0f6e62SHans Petter Selasky free(pcc, M_CUSE); 1416fa0f6e62SHans Petter Selasky return (ENOMEM); 1417fa0f6e62SHans Petter Selasky } 1418fa0f6e62SHans Petter Selasky pcc->fflags = fflags; 1419fa0f6e62SHans Petter Selasky pcc->server_dev = pcsd; 1420fa0f6e62SHans Petter Selasky pcc->server = pcs; 1421fa0f6e62SHans Petter Selasky 1422fa0f6e62SHans Petter Selasky for (n = 0; n != CUSE_CMD_MAX; n++) { 1423fa0f6e62SHans Petter Selasky 1424fa0f6e62SHans Petter Selasky pccmd = &pcc->cmds[n]; 1425fa0f6e62SHans Petter Selasky 1426fa0f6e62SHans Petter Selasky pccmd->sub.dev = pcd; 1427fa0f6e62SHans Petter Selasky pccmd->sub.command = n; 1428fa0f6e62SHans Petter Selasky pccmd->client = pcc; 1429fa0f6e62SHans Petter Selasky 1430fa0f6e62SHans Petter Selasky sx_init(&pccmd->sx, "cuse-client-sx"); 1431fa0f6e62SHans Petter Selasky cv_init(&pccmd->cv, "cuse-client-cv"); 1432fa0f6e62SHans Petter Selasky } 1433fa0f6e62SHans Petter Selasky 1434fa0f6e62SHans Petter Selasky cuse_lock(); 1435fa0f6e62SHans Petter Selasky 1436fa0f6e62SHans Petter Selasky /* cuse_client_free() assumes that the client is listed somewhere! */ 1437fa0f6e62SHans Petter Selasky /* always enqueue */ 1438fa0f6e62SHans Petter Selasky 1439fa0f6e62SHans Petter Selasky TAILQ_INSERT_TAIL(&pcs->hcli, pcc, entry); 1440fa0f6e62SHans Petter Selasky 1441fa0f6e62SHans Petter Selasky /* check if server is closing */ 1442fa0f6e62SHans Petter Selasky if ((pcs->is_closing != 0) || (dev->si_drv1 == NULL)) { 1443fa0f6e62SHans Petter Selasky error = EINVAL; 1444fa0f6e62SHans Petter Selasky } else { 1445fa0f6e62SHans Petter Selasky error = 0; 1446fa0f6e62SHans Petter Selasky } 1447fa0f6e62SHans Petter Selasky cuse_unlock(); 1448fa0f6e62SHans Petter Selasky 1449fa0f6e62SHans Petter Selasky if (error) { 1450fa0f6e62SHans Petter Selasky devfs_clear_cdevpriv(); /* XXX bugfix */ 1451fa0f6e62SHans Petter Selasky return (error); 1452fa0f6e62SHans Petter Selasky } 1453fa0f6e62SHans Petter Selasky pccmd = &pcc->cmds[CUSE_CMD_OPEN]; 1454fa0f6e62SHans Petter Selasky 1455fa0f6e62SHans Petter Selasky cuse_cmd_lock(pccmd); 1456fa0f6e62SHans Petter Selasky 1457fa0f6e62SHans Petter Selasky cuse_lock(); 1458fa0f6e62SHans Petter Selasky cuse_client_send_command_locked(pccmd, 0, 0, pcc->fflags, 0); 1459fa0f6e62SHans Petter Selasky 1460fa0f6e62SHans Petter Selasky error = cuse_client_receive_command_locked(pccmd, 0, 0); 1461fa0f6e62SHans Petter Selasky cuse_unlock(); 1462fa0f6e62SHans Petter Selasky 1463fa0f6e62SHans Petter Selasky if (error < 0) { 1464fa0f6e62SHans Petter Selasky error = cuse_convert_error(error); 1465fa0f6e62SHans Petter Selasky } else { 1466fa0f6e62SHans Petter Selasky error = 0; 1467fa0f6e62SHans Petter Selasky } 1468fa0f6e62SHans Petter Selasky 1469fa0f6e62SHans Petter Selasky cuse_cmd_unlock(pccmd); 1470fa0f6e62SHans Petter Selasky 1471fa0f6e62SHans Petter Selasky if (error) 1472fa0f6e62SHans Petter Selasky devfs_clear_cdevpriv(); /* XXX bugfix */ 1473fa0f6e62SHans Petter Selasky 1474fa0f6e62SHans Petter Selasky return (error); 1475fa0f6e62SHans Petter Selasky } 1476fa0f6e62SHans Petter Selasky 1477fa0f6e62SHans Petter Selasky static int 1478fa0f6e62SHans Petter Selasky cuse_client_close(struct cdev *dev, int fflag, int devtype, struct thread *td) 1479fa0f6e62SHans Petter Selasky { 1480fa0f6e62SHans Petter Selasky struct cuse_client_command *pccmd; 1481fa0f6e62SHans Petter Selasky struct cuse_client *pcc; 1482fa0f6e62SHans Petter Selasky int error; 1483fa0f6e62SHans Petter Selasky 1484fa0f6e62SHans Petter Selasky error = cuse_client_get(&pcc); 1485fa0f6e62SHans Petter Selasky if (error != 0) 1486fa0f6e62SHans Petter Selasky return (0); 1487fa0f6e62SHans Petter Selasky 1488fa0f6e62SHans Petter Selasky pccmd = &pcc->cmds[CUSE_CMD_CLOSE]; 1489fa0f6e62SHans Petter Selasky 1490fa0f6e62SHans Petter Selasky cuse_cmd_lock(pccmd); 1491fa0f6e62SHans Petter Selasky 1492fa0f6e62SHans Petter Selasky cuse_lock(); 1493fa0f6e62SHans Petter Selasky cuse_client_send_command_locked(pccmd, 0, 0, pcc->fflags, 0); 1494fa0f6e62SHans Petter Selasky 1495fa0f6e62SHans Petter Selasky error = cuse_client_receive_command_locked(pccmd, 0, 0); 1496fa0f6e62SHans Petter Selasky cuse_unlock(); 1497fa0f6e62SHans Petter Selasky 1498fa0f6e62SHans Petter Selasky cuse_cmd_unlock(pccmd); 1499fa0f6e62SHans Petter Selasky 1500fa0f6e62SHans Petter Selasky cuse_lock(); 1501fa0f6e62SHans Petter Selasky cuse_client_is_closing(pcc); 1502fa0f6e62SHans Petter Selasky cuse_unlock(); 1503fa0f6e62SHans Petter Selasky 1504fa0f6e62SHans Petter Selasky return (0); 1505fa0f6e62SHans Petter Selasky } 1506fa0f6e62SHans Petter Selasky 1507fa0f6e62SHans Petter Selasky static void 1508fa0f6e62SHans Petter Selasky cuse_client_kqfilter_poll(struct cdev *dev, struct cuse_client *pcc) 1509fa0f6e62SHans Petter Selasky { 1510fa0f6e62SHans Petter Selasky int temp; 1511fa0f6e62SHans Petter Selasky 1512fa0f6e62SHans Petter Selasky cuse_lock(); 1513fa0f6e62SHans Petter Selasky temp = (pcc->cflags & (CUSE_CLI_KNOTE_HAS_READ | 1514fa0f6e62SHans Petter Selasky CUSE_CLI_KNOTE_HAS_WRITE)); 1515fa0f6e62SHans Petter Selasky pcc->cflags &= ~(CUSE_CLI_KNOTE_NEED_READ | 1516fa0f6e62SHans Petter Selasky CUSE_CLI_KNOTE_NEED_WRITE); 1517fa0f6e62SHans Petter Selasky cuse_unlock(); 1518fa0f6e62SHans Petter Selasky 1519fa0f6e62SHans Petter Selasky if (temp != 0) { 1520fa0f6e62SHans Petter Selasky /* get the latest polling state from the server */ 1521fa0f6e62SHans Petter Selasky temp = cuse_client_poll(dev, POLLIN | POLLOUT, NULL); 1522fa0f6e62SHans Petter Selasky 1523fa0f6e62SHans Petter Selasky if (temp & (POLLIN | POLLOUT)) { 15249a83b097SHans Petter Selasky cuse_lock(); 1525fa0f6e62SHans Petter Selasky if (temp & POLLIN) 1526fa0f6e62SHans Petter Selasky pcc->cflags |= CUSE_CLI_KNOTE_NEED_READ; 1527fa0f6e62SHans Petter Selasky if (temp & POLLOUT) 1528fa0f6e62SHans Petter Selasky pcc->cflags |= CUSE_CLI_KNOTE_NEED_WRITE; 1529fa0f6e62SHans Petter Selasky 1530fa0f6e62SHans Petter Selasky /* make sure the "knote" gets woken up */ 1531fa0f6e62SHans Petter Selasky cuse_server_wakeup_locked(pcc->server); 1532fa0f6e62SHans Petter Selasky cuse_unlock(); 1533fa0f6e62SHans Petter Selasky } 1534fa0f6e62SHans Petter Selasky } 15359a83b097SHans Petter Selasky } 1536fa0f6e62SHans Petter Selasky 1537fa0f6e62SHans Petter Selasky static int 1538fa0f6e62SHans Petter Selasky cuse_client_read(struct cdev *dev, struct uio *uio, int ioflag) 1539fa0f6e62SHans Petter Selasky { 1540fa0f6e62SHans Petter Selasky struct cuse_client_command *pccmd; 1541fa0f6e62SHans Petter Selasky struct cuse_client *pcc; 1542fa0f6e62SHans Petter Selasky int error; 1543fa0f6e62SHans Petter Selasky int len; 1544fa0f6e62SHans Petter Selasky 1545fa0f6e62SHans Petter Selasky error = cuse_client_get(&pcc); 1546fa0f6e62SHans Petter Selasky if (error != 0) 1547fa0f6e62SHans Petter Selasky return (error); 1548fa0f6e62SHans Petter Selasky 1549fa0f6e62SHans Petter Selasky pccmd = &pcc->cmds[CUSE_CMD_READ]; 1550fa0f6e62SHans Petter Selasky 1551fa0f6e62SHans Petter Selasky if (uio->uio_segflg != UIO_USERSPACE) { 1552fa0f6e62SHans Petter Selasky return (EINVAL); 1553fa0f6e62SHans Petter Selasky } 1554fa0f6e62SHans Petter Selasky uio->uio_segflg = UIO_NOCOPY; 1555fa0f6e62SHans Petter Selasky 1556fa0f6e62SHans Petter Selasky cuse_cmd_lock(pccmd); 1557fa0f6e62SHans Petter Selasky 1558fa0f6e62SHans Petter Selasky while (uio->uio_resid != 0) { 1559fa0f6e62SHans Petter Selasky 1560fa0f6e62SHans Petter Selasky if (uio->uio_iov->iov_len > CUSE_LENGTH_MAX) { 1561fa0f6e62SHans Petter Selasky error = ENOMEM; 1562fa0f6e62SHans Petter Selasky break; 1563fa0f6e62SHans Petter Selasky } 1564fa0f6e62SHans Petter Selasky len = uio->uio_iov->iov_len; 1565fa0f6e62SHans Petter Selasky 1566fa0f6e62SHans Petter Selasky cuse_lock(); 1567fa0f6e62SHans Petter Selasky cuse_client_send_command_locked(pccmd, 1568beebd9aaSHans Petter Selasky (uintptr_t)uio->uio_iov->iov_base, 1569fa0f6e62SHans Petter Selasky (unsigned long)(unsigned int)len, pcc->fflags, ioflag); 1570fa0f6e62SHans Petter Selasky 1571fa0f6e62SHans Petter Selasky error = cuse_client_receive_command_locked(pccmd, 0, 0); 1572fa0f6e62SHans Petter Selasky cuse_unlock(); 1573fa0f6e62SHans Petter Selasky 1574fa0f6e62SHans Petter Selasky if (error < 0) { 1575fa0f6e62SHans Petter Selasky error = cuse_convert_error(error); 1576fa0f6e62SHans Petter Selasky break; 1577fa0f6e62SHans Petter Selasky } else if (error == len) { 1578fa0f6e62SHans Petter Selasky error = uiomove(NULL, error, uio); 1579fa0f6e62SHans Petter Selasky if (error) 1580fa0f6e62SHans Petter Selasky break; 1581fa0f6e62SHans Petter Selasky } else { 1582fa0f6e62SHans Petter Selasky error = uiomove(NULL, error, uio); 1583fa0f6e62SHans Petter Selasky break; 1584fa0f6e62SHans Petter Selasky } 1585fa0f6e62SHans Petter Selasky } 1586fa0f6e62SHans Petter Selasky cuse_cmd_unlock(pccmd); 1587fa0f6e62SHans Petter Selasky 1588fa0f6e62SHans Petter Selasky uio->uio_segflg = UIO_USERSPACE;/* restore segment flag */ 1589fa0f6e62SHans Petter Selasky 1590fa0f6e62SHans Petter Selasky if (error == EWOULDBLOCK) 1591fa0f6e62SHans Petter Selasky cuse_client_kqfilter_poll(dev, pcc); 1592fa0f6e62SHans Petter Selasky 1593fa0f6e62SHans Petter Selasky return (error); 1594fa0f6e62SHans Petter Selasky } 1595fa0f6e62SHans Petter Selasky 1596fa0f6e62SHans Petter Selasky static int 1597fa0f6e62SHans Petter Selasky cuse_client_write(struct cdev *dev, struct uio *uio, int ioflag) 1598fa0f6e62SHans Petter Selasky { 1599fa0f6e62SHans Petter Selasky struct cuse_client_command *pccmd; 1600fa0f6e62SHans Petter Selasky struct cuse_client *pcc; 1601fa0f6e62SHans Petter Selasky int error; 1602fa0f6e62SHans Petter Selasky int len; 1603fa0f6e62SHans Petter Selasky 1604fa0f6e62SHans Petter Selasky error = cuse_client_get(&pcc); 1605fa0f6e62SHans Petter Selasky if (error != 0) 1606fa0f6e62SHans Petter Selasky return (error); 1607fa0f6e62SHans Petter Selasky 1608fa0f6e62SHans Petter Selasky pccmd = &pcc->cmds[CUSE_CMD_WRITE]; 1609fa0f6e62SHans Petter Selasky 1610fa0f6e62SHans Petter Selasky if (uio->uio_segflg != UIO_USERSPACE) { 1611fa0f6e62SHans Petter Selasky return (EINVAL); 1612fa0f6e62SHans Petter Selasky } 1613fa0f6e62SHans Petter Selasky uio->uio_segflg = UIO_NOCOPY; 1614fa0f6e62SHans Petter Selasky 1615fa0f6e62SHans Petter Selasky cuse_cmd_lock(pccmd); 1616fa0f6e62SHans Petter Selasky 1617fa0f6e62SHans Petter Selasky while (uio->uio_resid != 0) { 1618fa0f6e62SHans Petter Selasky 1619fa0f6e62SHans Petter Selasky if (uio->uio_iov->iov_len > CUSE_LENGTH_MAX) { 1620fa0f6e62SHans Petter Selasky error = ENOMEM; 1621fa0f6e62SHans Petter Selasky break; 1622fa0f6e62SHans Petter Selasky } 1623fa0f6e62SHans Petter Selasky len = uio->uio_iov->iov_len; 1624fa0f6e62SHans Petter Selasky 1625fa0f6e62SHans Petter Selasky cuse_lock(); 1626fa0f6e62SHans Petter Selasky cuse_client_send_command_locked(pccmd, 1627beebd9aaSHans Petter Selasky (uintptr_t)uio->uio_iov->iov_base, 1628fa0f6e62SHans Petter Selasky (unsigned long)(unsigned int)len, pcc->fflags, ioflag); 1629fa0f6e62SHans Petter Selasky 1630fa0f6e62SHans Petter Selasky error = cuse_client_receive_command_locked(pccmd, 0, 0); 1631fa0f6e62SHans Petter Selasky cuse_unlock(); 1632fa0f6e62SHans Petter Selasky 1633fa0f6e62SHans Petter Selasky if (error < 0) { 1634fa0f6e62SHans Petter Selasky error = cuse_convert_error(error); 1635fa0f6e62SHans Petter Selasky break; 1636fa0f6e62SHans Petter Selasky } else if (error == len) { 1637fa0f6e62SHans Petter Selasky error = uiomove(NULL, error, uio); 1638fa0f6e62SHans Petter Selasky if (error) 1639fa0f6e62SHans Petter Selasky break; 1640fa0f6e62SHans Petter Selasky } else { 1641fa0f6e62SHans Petter Selasky error = uiomove(NULL, error, uio); 1642fa0f6e62SHans Petter Selasky break; 1643fa0f6e62SHans Petter Selasky } 1644fa0f6e62SHans Petter Selasky } 1645fa0f6e62SHans Petter Selasky cuse_cmd_unlock(pccmd); 1646fa0f6e62SHans Petter Selasky 1647fa0f6e62SHans Petter Selasky uio->uio_segflg = UIO_USERSPACE;/* restore segment flag */ 1648fa0f6e62SHans Petter Selasky 1649fa0f6e62SHans Petter Selasky if (error == EWOULDBLOCK) 1650fa0f6e62SHans Petter Selasky cuse_client_kqfilter_poll(dev, pcc); 1651fa0f6e62SHans Petter Selasky 1652fa0f6e62SHans Petter Selasky return (error); 1653fa0f6e62SHans Petter Selasky } 1654fa0f6e62SHans Petter Selasky 1655fa0f6e62SHans Petter Selasky int 1656fa0f6e62SHans Petter Selasky cuse_client_ioctl(struct cdev *dev, unsigned long cmd, 1657fa0f6e62SHans Petter Selasky caddr_t data, int fflag, struct thread *td) 1658fa0f6e62SHans Petter Selasky { 1659fa0f6e62SHans Petter Selasky struct cuse_client_command *pccmd; 1660fa0f6e62SHans Petter Selasky struct cuse_client *pcc; 1661fa0f6e62SHans Petter Selasky int error; 1662fa0f6e62SHans Petter Selasky int len; 1663fa0f6e62SHans Petter Selasky 1664fa0f6e62SHans Petter Selasky error = cuse_client_get(&pcc); 1665fa0f6e62SHans Petter Selasky if (error != 0) 1666fa0f6e62SHans Petter Selasky return (error); 1667fa0f6e62SHans Petter Selasky 1668fa0f6e62SHans Petter Selasky len = IOCPARM_LEN(cmd); 1669fa0f6e62SHans Petter Selasky if (len > CUSE_BUFFER_MAX) 1670fa0f6e62SHans Petter Selasky return (ENOMEM); 1671fa0f6e62SHans Petter Selasky 1672fa0f6e62SHans Petter Selasky pccmd = &pcc->cmds[CUSE_CMD_IOCTL]; 1673fa0f6e62SHans Petter Selasky 1674fa0f6e62SHans Petter Selasky cuse_cmd_lock(pccmd); 1675fa0f6e62SHans Petter Selasky 1676010638abSHans Petter Selasky if (cmd & (IOC_IN | IOC_VOID)) 1677fa0f6e62SHans Petter Selasky memcpy(pcc->ioctl_buffer, data, len); 1678fa0f6e62SHans Petter Selasky 1679fa0f6e62SHans Petter Selasky /* 1680fa0f6e62SHans Petter Selasky * When the ioctl-length is zero drivers can pass information 1681fa0f6e62SHans Petter Selasky * through the data pointer of the ioctl. Make sure this information 1682fa0f6e62SHans Petter Selasky * is forwarded to the driver. 1683fa0f6e62SHans Petter Selasky */ 1684fa0f6e62SHans Petter Selasky 1685fa0f6e62SHans Petter Selasky cuse_lock(); 1686fa0f6e62SHans Petter Selasky cuse_client_send_command_locked(pccmd, 1687fa0f6e62SHans Petter Selasky (len == 0) ? *(long *)data : CUSE_BUF_MIN_PTR, 1688fa0f6e62SHans Petter Selasky (unsigned long)cmd, pcc->fflags, 1689fa0f6e62SHans Petter Selasky (fflag & O_NONBLOCK) ? IO_NDELAY : 0); 1690fa0f6e62SHans Petter Selasky 1691fa0f6e62SHans Petter Selasky error = cuse_client_receive_command_locked(pccmd, data, len); 1692fa0f6e62SHans Petter Selasky cuse_unlock(); 1693fa0f6e62SHans Petter Selasky 1694fa0f6e62SHans Petter Selasky if (error < 0) { 1695fa0f6e62SHans Petter Selasky error = cuse_convert_error(error); 1696fa0f6e62SHans Petter Selasky } else { 1697fa0f6e62SHans Petter Selasky error = 0; 1698fa0f6e62SHans Petter Selasky } 1699fa0f6e62SHans Petter Selasky 1700fa0f6e62SHans Petter Selasky if (cmd & IOC_OUT) 1701fa0f6e62SHans Petter Selasky memcpy(data, pcc->ioctl_buffer, len); 1702fa0f6e62SHans Petter Selasky 1703fa0f6e62SHans Petter Selasky cuse_cmd_unlock(pccmd); 1704fa0f6e62SHans Petter Selasky 1705fa0f6e62SHans Petter Selasky if (error == EWOULDBLOCK) 1706fa0f6e62SHans Petter Selasky cuse_client_kqfilter_poll(dev, pcc); 1707fa0f6e62SHans Petter Selasky 1708fa0f6e62SHans Petter Selasky return (error); 1709fa0f6e62SHans Petter Selasky } 1710fa0f6e62SHans Petter Selasky 1711fa0f6e62SHans Petter Selasky static int 1712fa0f6e62SHans Petter Selasky cuse_client_poll(struct cdev *dev, int events, struct thread *td) 1713fa0f6e62SHans Petter Selasky { 1714fa0f6e62SHans Petter Selasky struct cuse_client_command *pccmd; 1715fa0f6e62SHans Petter Selasky struct cuse_client *pcc; 1716fa0f6e62SHans Petter Selasky unsigned long temp; 1717fa0f6e62SHans Petter Selasky int error; 1718fa0f6e62SHans Petter Selasky int revents; 1719fa0f6e62SHans Petter Selasky 1720fa0f6e62SHans Petter Selasky error = cuse_client_get(&pcc); 1721fa0f6e62SHans Petter Selasky if (error != 0) 172295700043SHans Petter Selasky goto pollnval; 1723fa0f6e62SHans Petter Selasky 1724fa0f6e62SHans Petter Selasky temp = 0; 1725fa0f6e62SHans Petter Selasky 1726fa0f6e62SHans Petter Selasky if (events & (POLLPRI | POLLIN | POLLRDNORM)) 1727fa0f6e62SHans Petter Selasky temp |= CUSE_POLL_READ; 1728fa0f6e62SHans Petter Selasky 1729fa0f6e62SHans Petter Selasky if (events & (POLLOUT | POLLWRNORM)) 1730fa0f6e62SHans Petter Selasky temp |= CUSE_POLL_WRITE; 1731fa0f6e62SHans Petter Selasky 1732fa0f6e62SHans Petter Selasky if (events & POLLHUP) 1733fa0f6e62SHans Petter Selasky temp |= CUSE_POLL_ERROR; 1734fa0f6e62SHans Petter Selasky 1735fa0f6e62SHans Petter Selasky pccmd = &pcc->cmds[CUSE_CMD_POLL]; 1736fa0f6e62SHans Petter Selasky 1737fa0f6e62SHans Petter Selasky cuse_cmd_lock(pccmd); 1738fa0f6e62SHans Petter Selasky 1739fa0f6e62SHans Petter Selasky /* Need to selrecord() first to not loose any events. */ 1740fa0f6e62SHans Petter Selasky if (temp != 0 && td != NULL) 1741fa0f6e62SHans Petter Selasky selrecord(td, &pcc->server->selinfo); 1742fa0f6e62SHans Petter Selasky 1743fa0f6e62SHans Petter Selasky cuse_lock(); 1744fa0f6e62SHans Petter Selasky cuse_client_send_command_locked(pccmd, 1745fa0f6e62SHans Petter Selasky 0, temp, pcc->fflags, IO_NDELAY); 1746fa0f6e62SHans Petter Selasky 1747fa0f6e62SHans Petter Selasky error = cuse_client_receive_command_locked(pccmd, 0, 0); 1748fa0f6e62SHans Petter Selasky cuse_unlock(); 1749fa0f6e62SHans Petter Selasky 175095700043SHans Petter Selasky cuse_cmd_unlock(pccmd); 175195700043SHans Petter Selasky 1752fa0f6e62SHans Petter Selasky if (error < 0) { 175395700043SHans Petter Selasky goto pollnval; 1754fa0f6e62SHans Petter Selasky } else { 1755fa0f6e62SHans Petter Selasky revents = 0; 1756fa0f6e62SHans Petter Selasky if (error & CUSE_POLL_READ) 1757fa0f6e62SHans Petter Selasky revents |= (events & (POLLPRI | POLLIN | POLLRDNORM)); 1758fa0f6e62SHans Petter Selasky if (error & CUSE_POLL_WRITE) 1759fa0f6e62SHans Petter Selasky revents |= (events & (POLLOUT | POLLWRNORM)); 1760fa0f6e62SHans Petter Selasky if (error & CUSE_POLL_ERROR) 1761fa0f6e62SHans Petter Selasky revents |= (events & POLLHUP); 1762fa0f6e62SHans Petter Selasky } 1763fa0f6e62SHans Petter Selasky return (revents); 176495700043SHans Petter Selasky 176595700043SHans Petter Selasky pollnval: 176695700043SHans Petter Selasky /* XXX many clients don't understand POLLNVAL */ 176795700043SHans Petter Selasky return (events & (POLLHUP | POLLPRI | POLLIN | 176895700043SHans Petter Selasky POLLRDNORM | POLLOUT | POLLWRNORM)); 1769fa0f6e62SHans Petter Selasky } 1770fa0f6e62SHans Petter Selasky 1771fa0f6e62SHans Petter Selasky static int 1772*56c49c63SHans Petter Selasky cuse_client_mmap_single(struct cdev *dev, vm_ooffset_t *offset, 1773*56c49c63SHans Petter Selasky vm_size_t size, struct vm_object **object, int nprot) 1774fa0f6e62SHans Petter Selasky { 1775*56c49c63SHans Petter Selasky uint32_t page_nr = *offset / PAGE_SIZE; 1776fa0f6e62SHans Petter Selasky uint32_t alloc_nr = page_nr / CUSE_ALLOC_PAGES_MAX; 1777fa0f6e62SHans Petter Selasky struct cuse_memory *mem; 1778fa0f6e62SHans Petter Selasky struct cuse_client *pcc; 1779fa0f6e62SHans Petter Selasky int error; 1780fa0f6e62SHans Petter Selasky 1781fa0f6e62SHans Petter Selasky error = cuse_client_get(&pcc); 1782fa0f6e62SHans Petter Selasky if (error != 0) 1783*56c49c63SHans Petter Selasky return (error); 1784fa0f6e62SHans Petter Selasky 1785fa0f6e62SHans Petter Selasky cuse_lock(); 1786*56c49c63SHans Petter Selasky /* lookup memory structure */ 1787*56c49c63SHans Petter Selasky TAILQ_FOREACH(mem, &pcc->server->hmem, entry) { 1788*56c49c63SHans Petter Selasky if (mem->alloc_nr == alloc_nr) 1789*56c49c63SHans Petter Selasky break; 1790fa0f6e62SHans Petter Selasky } 1791*56c49c63SHans Petter Selasky if (mem == NULL) { 1792fa0f6e62SHans Petter Selasky cuse_unlock(); 1793fa0f6e62SHans Petter Selasky return (ENOMEM); 1794fa0f6e62SHans Petter Selasky } 1795*56c49c63SHans Petter Selasky /* verify page offset */ 1796fa0f6e62SHans Petter Selasky page_nr %= CUSE_ALLOC_PAGES_MAX; 1797fa0f6e62SHans Petter Selasky if (page_nr >= mem->page_count) { 1798fa0f6e62SHans Petter Selasky cuse_unlock(); 1799fa0f6e62SHans Petter Selasky return (ENXIO); 1800fa0f6e62SHans Petter Selasky } 1801*56c49c63SHans Petter Selasky /* verify mmap size */ 1802*56c49c63SHans Petter Selasky if ((size % PAGE_SIZE) != 0 || (size < PAGE_SIZE) || 1803*56c49c63SHans Petter Selasky (size > ((mem->page_count - page_nr) * PAGE_SIZE))) { 1804*56c49c63SHans Petter Selasky cuse_unlock(); 1805*56c49c63SHans Petter Selasky return (EINVAL); 1806*56c49c63SHans Petter Selasky } 1807*56c49c63SHans Petter Selasky vm_object_reference(mem->object); 1808*56c49c63SHans Petter Selasky *object = mem->object; 1809fa0f6e62SHans Petter Selasky cuse_unlock(); 1810fa0f6e62SHans Petter Selasky 1811*56c49c63SHans Petter Selasky /* set new VM object offset to use */ 1812*56c49c63SHans Petter Selasky *offset = page_nr * PAGE_SIZE; 1813fa0f6e62SHans Petter Selasky 1814*56c49c63SHans Petter Selasky /* success */ 1815fa0f6e62SHans Petter Selasky return (0); 1816fa0f6e62SHans Petter Selasky } 1817fa0f6e62SHans Petter Selasky 1818fa0f6e62SHans Petter Selasky static void 1819fa0f6e62SHans Petter Selasky cuse_client_kqfilter_read_detach(struct knote *kn) 1820fa0f6e62SHans Petter Selasky { 1821fa0f6e62SHans Petter Selasky struct cuse_client *pcc; 1822fa0f6e62SHans Petter Selasky 1823fa0f6e62SHans Petter Selasky cuse_lock(); 1824fa0f6e62SHans Petter Selasky pcc = kn->kn_hook; 1825fa0f6e62SHans Petter Selasky knlist_remove(&pcc->server->selinfo.si_note, kn, 1); 1826fa0f6e62SHans Petter Selasky cuse_unlock(); 1827fa0f6e62SHans Petter Selasky } 1828fa0f6e62SHans Petter Selasky 1829fa0f6e62SHans Petter Selasky static void 1830fa0f6e62SHans Petter Selasky cuse_client_kqfilter_write_detach(struct knote *kn) 1831fa0f6e62SHans Petter Selasky { 1832fa0f6e62SHans Petter Selasky struct cuse_client *pcc; 1833fa0f6e62SHans Petter Selasky 1834fa0f6e62SHans Petter Selasky cuse_lock(); 1835fa0f6e62SHans Petter Selasky pcc = kn->kn_hook; 1836fa0f6e62SHans Petter Selasky knlist_remove(&pcc->server->selinfo.si_note, kn, 1); 1837fa0f6e62SHans Petter Selasky cuse_unlock(); 1838fa0f6e62SHans Petter Selasky } 1839fa0f6e62SHans Petter Selasky 1840fa0f6e62SHans Petter Selasky static int 1841fa0f6e62SHans Petter Selasky cuse_client_kqfilter_read_event(struct knote *kn, long hint) 1842fa0f6e62SHans Petter Selasky { 1843fa0f6e62SHans Petter Selasky struct cuse_client *pcc; 1844fa0f6e62SHans Petter Selasky 1845fa0f6e62SHans Petter Selasky mtx_assert(&cuse_mtx, MA_OWNED); 1846fa0f6e62SHans Petter Selasky 1847fa0f6e62SHans Petter Selasky pcc = kn->kn_hook; 1848fa0f6e62SHans Petter Selasky return ((pcc->cflags & CUSE_CLI_KNOTE_NEED_READ) ? 1 : 0); 1849fa0f6e62SHans Petter Selasky } 1850fa0f6e62SHans Petter Selasky 1851fa0f6e62SHans Petter Selasky static int 1852fa0f6e62SHans Petter Selasky cuse_client_kqfilter_write_event(struct knote *kn, long hint) 1853fa0f6e62SHans Petter Selasky { 1854fa0f6e62SHans Petter Selasky struct cuse_client *pcc; 1855fa0f6e62SHans Petter Selasky 1856fa0f6e62SHans Petter Selasky mtx_assert(&cuse_mtx, MA_OWNED); 1857fa0f6e62SHans Petter Selasky 1858fa0f6e62SHans Petter Selasky pcc = kn->kn_hook; 1859fa0f6e62SHans Petter Selasky return ((pcc->cflags & CUSE_CLI_KNOTE_NEED_WRITE) ? 1 : 0); 1860fa0f6e62SHans Petter Selasky } 1861fa0f6e62SHans Petter Selasky 1862fa0f6e62SHans Petter Selasky static int 1863fa0f6e62SHans Petter Selasky cuse_client_kqfilter(struct cdev *dev, struct knote *kn) 1864fa0f6e62SHans Petter Selasky { 1865fa0f6e62SHans Petter Selasky struct cuse_client *pcc; 1866fa0f6e62SHans Petter Selasky struct cuse_server *pcs; 1867fa0f6e62SHans Petter Selasky int error; 1868fa0f6e62SHans Petter Selasky 1869fa0f6e62SHans Petter Selasky error = cuse_client_get(&pcc); 1870fa0f6e62SHans Petter Selasky if (error != 0) 1871fa0f6e62SHans Petter Selasky return (error); 1872fa0f6e62SHans Petter Selasky 1873fa0f6e62SHans Petter Selasky cuse_lock(); 1874fa0f6e62SHans Petter Selasky pcs = pcc->server; 1875fa0f6e62SHans Petter Selasky switch (kn->kn_filter) { 1876fa0f6e62SHans Petter Selasky case EVFILT_READ: 1877fa0f6e62SHans Petter Selasky pcc->cflags |= CUSE_CLI_KNOTE_HAS_READ; 1878fa0f6e62SHans Petter Selasky kn->kn_hook = pcc; 1879fa0f6e62SHans Petter Selasky kn->kn_fop = &cuse_client_kqfilter_read_ops; 1880fa0f6e62SHans Petter Selasky knlist_add(&pcs->selinfo.si_note, kn, 1); 1881fa0f6e62SHans Petter Selasky break; 1882fa0f6e62SHans Petter Selasky case EVFILT_WRITE: 1883fa0f6e62SHans Petter Selasky pcc->cflags |= CUSE_CLI_KNOTE_HAS_WRITE; 1884fa0f6e62SHans Petter Selasky kn->kn_hook = pcc; 1885fa0f6e62SHans Petter Selasky kn->kn_fop = &cuse_client_kqfilter_write_ops; 1886fa0f6e62SHans Petter Selasky knlist_add(&pcs->selinfo.si_note, kn, 1); 1887fa0f6e62SHans Petter Selasky break; 1888fa0f6e62SHans Petter Selasky default: 1889fa0f6e62SHans Petter Selasky error = EINVAL; 1890fa0f6e62SHans Petter Selasky break; 1891fa0f6e62SHans Petter Selasky } 1892fa0f6e62SHans Petter Selasky cuse_unlock(); 1893fa0f6e62SHans Petter Selasky 1894fa0f6e62SHans Petter Selasky if (error == 0) 1895fa0f6e62SHans Petter Selasky cuse_client_kqfilter_poll(dev, pcc); 1896fa0f6e62SHans Petter Selasky return (error); 1897fa0f6e62SHans Petter Selasky } 1898