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