xref: /freebsd/sys/fs/cuse/cuse.c (revision a71074e0af2b37a3f9082bafb8a0a0feb3cd4515)
1fa0f6e62SHans Petter Selasky /* $FreeBSD$ */
2fa0f6e62SHans Petter Selasky /*-
398029019SHans Petter Selasky  * Copyright (c) 2010-2020 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 <sys/stdint.h>
28fa0f6e62SHans Petter Selasky #include <sys/stddef.h>
29fa0f6e62SHans Petter Selasky #include <sys/param.h>
30fa0f6e62SHans Petter Selasky #include <sys/types.h>
31fa0f6e62SHans Petter Selasky #include <sys/systm.h>
32fa0f6e62SHans Petter Selasky #include <sys/conf.h>
33fa0f6e62SHans Petter Selasky #include <sys/kernel.h>
34fa0f6e62SHans Petter Selasky #include <sys/bus.h>
35fa0f6e62SHans Petter Selasky #include <sys/linker_set.h>
36fa0f6e62SHans Petter Selasky #include <sys/module.h>
37fa0f6e62SHans Petter Selasky #include <sys/lock.h>
38fa0f6e62SHans Petter Selasky #include <sys/mutex.h>
39fa0f6e62SHans Petter Selasky #include <sys/condvar.h>
40fa0f6e62SHans Petter Selasky #include <sys/sysctl.h>
41fa0f6e62SHans Petter Selasky #include <sys/unistd.h>
42fa0f6e62SHans Petter Selasky #include <sys/malloc.h>
43fa0f6e62SHans Petter Selasky #include <sys/priv.h>
44fa0f6e62SHans Petter Selasky #include <sys/uio.h>
45fa0f6e62SHans Petter Selasky #include <sys/poll.h>
46fa0f6e62SHans Petter Selasky #include <sys/sx.h>
4756c49c63SHans Petter Selasky #include <sys/rwlock.h>
48fa0f6e62SHans Petter Selasky #include <sys/queue.h>
49fa0f6e62SHans Petter Selasky #include <sys/fcntl.h>
50fa0f6e62SHans Petter Selasky #include <sys/proc.h>
51fa0f6e62SHans Petter Selasky #include <sys/vnode.h>
52fa0f6e62SHans Petter Selasky #include <sys/selinfo.h>
53fa0f6e62SHans Petter Selasky #include <sys/ptrace.h>
54db92a6cdSHans Petter Selasky #include <sys/sysent.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>
6056c49c63SHans Petter Selasky #include <vm/vm_object.h>
6156c49c63SHans Petter Selasky #include <vm/vm_page.h>
6256c49c63SHans Petter Selasky #include <vm/vm_pager.h>
63fa0f6e62SHans Petter Selasky 
64fa0f6e62SHans Petter Selasky #include <fs/cuse/cuse_defs.h>
65fa0f6e62SHans Petter Selasky #include <fs/cuse/cuse_ioctl.h>
66fa0f6e62SHans Petter Selasky 
67*a71074e0SHans Petter Selasky static int
68*a71074e0SHans Petter Selasky cuse_modevent(module_t mod, int type, void *data)
69*a71074e0SHans Petter Selasky {
70*a71074e0SHans Petter Selasky 	switch (type) {
71*a71074e0SHans Petter Selasky 	case MOD_LOAD:
72*a71074e0SHans Petter Selasky 	case MOD_UNLOAD:
73*a71074e0SHans Petter Selasky 		return (0);
74*a71074e0SHans Petter Selasky 	default:
75*a71074e0SHans Petter Selasky 		return (EOPNOTSUPP);
76*a71074e0SHans Petter Selasky 	}
77*a71074e0SHans Petter Selasky }
78*a71074e0SHans Petter Selasky 
79*a71074e0SHans Petter Selasky static moduledata_t cuse_mod = {
80*a71074e0SHans Petter Selasky 	.name = "cuse",
81*a71074e0SHans Petter Selasky 	.evhand = &cuse_modevent,
82*a71074e0SHans Petter Selasky };
83*a71074e0SHans Petter Selasky 
84*a71074e0SHans Petter Selasky DECLARE_MODULE(cuse, cuse_mod, SI_SUB_DEVFS, SI_ORDER_FIRST);
85fa0f6e62SHans Petter Selasky MODULE_VERSION(cuse, 1);
86fa0f6e62SHans Petter Selasky 
8719f1b6fbSHans Petter Selasky /*
8819f1b6fbSHans Petter Selasky  * Prevent cuse4bsd.ko and cuse.ko from loading at the same time by
8919f1b6fbSHans Petter Selasky  * declaring support for the cuse4bsd interface in cuse.ko:
9019f1b6fbSHans Petter Selasky  */
9119f1b6fbSHans Petter Selasky MODULE_VERSION(cuse4bsd, 1);
9219f1b6fbSHans Petter Selasky 
93fa0f6e62SHans Petter Selasky #ifdef FEATURE
94fa0f6e62SHans Petter Selasky FEATURE(cuse, "Userspace character devices");
95fa0f6e62SHans Petter Selasky #endif
96fa0f6e62SHans Petter Selasky 
97fa0f6e62SHans Petter Selasky struct cuse_command;
98fa0f6e62SHans Petter Selasky struct cuse_server;
99fa0f6e62SHans Petter Selasky struct cuse_client;
100fa0f6e62SHans Petter Selasky 
101fa0f6e62SHans Petter Selasky struct cuse_client_command {
102fa0f6e62SHans Petter Selasky 	TAILQ_ENTRY(cuse_client_command) entry;
103fa0f6e62SHans Petter Selasky 	struct cuse_command sub;
104fa0f6e62SHans Petter Selasky 	struct sx sx;
105fa0f6e62SHans Petter Selasky 	struct cv cv;
106fa0f6e62SHans Petter Selasky 	struct thread *entered;
107fa0f6e62SHans Petter Selasky 	struct cuse_client *client;
108fa0f6e62SHans Petter Selasky 	struct proc *proc_curr;
109fa0f6e62SHans Petter Selasky 	int	proc_refs;
110fa0f6e62SHans Petter Selasky 	int	got_signal;
111fa0f6e62SHans Petter Selasky 	int	error;
112fa0f6e62SHans Petter Selasky 	int	command;
113fa0f6e62SHans Petter Selasky };
114fa0f6e62SHans Petter Selasky 
115fa0f6e62SHans Petter Selasky struct cuse_memory {
11656c49c63SHans Petter Selasky 	TAILQ_ENTRY(cuse_memory) entry;
11756c49c63SHans Petter Selasky 	vm_object_t object;
118fa0f6e62SHans Petter Selasky 	uint32_t page_count;
11956c49c63SHans Petter Selasky 	uint32_t alloc_nr;
120fa0f6e62SHans Petter Selasky };
121fa0f6e62SHans Petter Selasky 
122fa0f6e62SHans Petter Selasky struct cuse_server_dev {
123fa0f6e62SHans Petter Selasky 	TAILQ_ENTRY(cuse_server_dev) entry;
124fa0f6e62SHans Petter Selasky 	struct cuse_server *server;
125fa0f6e62SHans Petter Selasky 	struct cdev *kern_dev;
126fa0f6e62SHans Petter Selasky 	struct cuse_dev *user_dev;
127fa0f6e62SHans Petter Selasky };
128fa0f6e62SHans Petter Selasky 
129fa0f6e62SHans Petter Selasky struct cuse_server {
130fa0f6e62SHans Petter Selasky 	TAILQ_ENTRY(cuse_server) entry;
131fa0f6e62SHans Petter Selasky 	TAILQ_HEAD(, cuse_client_command) head;
132fa0f6e62SHans Petter Selasky 	TAILQ_HEAD(, cuse_server_dev) hdev;
133fa0f6e62SHans Petter Selasky 	TAILQ_HEAD(, cuse_client) hcli;
13456c49c63SHans Petter Selasky 	TAILQ_HEAD(, cuse_memory) hmem;
13598029019SHans Petter Selasky 	struct mtx mtx;
136fa0f6e62SHans Petter Selasky 	struct cv cv;
137fa0f6e62SHans Petter Selasky 	struct selinfo selinfo;
138ac60e801SHans Petter Selasky 	pid_t	pid;
139fa0f6e62SHans Petter Selasky 	int	is_closing;
140fa0f6e62SHans Petter Selasky 	int	refs;
141fa0f6e62SHans Petter Selasky };
142fa0f6e62SHans Petter Selasky 
143fa0f6e62SHans Petter Selasky struct cuse_client {
144fa0f6e62SHans Petter Selasky 	TAILQ_ENTRY(cuse_client) entry;
145fa0f6e62SHans Petter Selasky 	TAILQ_ENTRY(cuse_client) entry_ref;
146fa0f6e62SHans Petter Selasky 	struct cuse_client_command cmds[CUSE_CMD_MAX];
147fa0f6e62SHans Petter Selasky 	struct cuse_server *server;
148fa0f6e62SHans Petter Selasky 	struct cuse_server_dev *server_dev;
149fa0f6e62SHans Petter Selasky 
150fa0f6e62SHans Petter Selasky 	uint8_t	ioctl_buffer[CUSE_BUFFER_MAX] __aligned(4);
151fa0f6e62SHans Petter Selasky 
152fa0f6e62SHans Petter Selasky 	int	fflags;			/* file flags */
153fa0f6e62SHans Petter Selasky 	int	cflags;			/* client flags */
154fa0f6e62SHans Petter Selasky #define	CUSE_CLI_IS_CLOSING 0x01
155fa0f6e62SHans Petter Selasky #define	CUSE_CLI_KNOTE_NEED_READ 0x02
156fa0f6e62SHans Petter Selasky #define	CUSE_CLI_KNOTE_NEED_WRITE 0x04
157fa0f6e62SHans Petter Selasky #define	CUSE_CLI_KNOTE_HAS_READ 0x08
158fa0f6e62SHans Petter Selasky #define	CUSE_CLI_KNOTE_HAS_WRITE 0x10
159fa0f6e62SHans Petter Selasky };
160fa0f6e62SHans Petter Selasky 
161fa0f6e62SHans Petter Selasky #define	CUSE_CLIENT_CLOSING(pcc) \
162fa0f6e62SHans Petter Selasky     ((pcc)->cflags & CUSE_CLI_IS_CLOSING)
163fa0f6e62SHans Petter Selasky 
164fa0f6e62SHans Petter Selasky static	MALLOC_DEFINE(M_CUSE, "cuse", "CUSE memory");
165fa0f6e62SHans Petter Selasky 
166fa0f6e62SHans Petter Selasky static TAILQ_HEAD(, cuse_server) cuse_server_head;
16798029019SHans Petter Selasky static struct mtx cuse_global_mtx;
168fa0f6e62SHans Petter Selasky static struct cdev *cuse_dev;
169fa0f6e62SHans Petter Selasky static struct cuse_server *cuse_alloc_unit[CUSE_DEVICES_MAX];
170fa0f6e62SHans Petter Selasky static int cuse_alloc_unit_id[CUSE_DEVICES_MAX];
171fa0f6e62SHans Petter Selasky 
17295700043SHans Petter Selasky static void cuse_server_wakeup_all_client_locked(struct cuse_server *pcs);
173fa0f6e62SHans Petter Selasky static void cuse_client_kqfilter_read_detach(struct knote *kn);
174fa0f6e62SHans Petter Selasky static void cuse_client_kqfilter_write_detach(struct knote *kn);
175fa0f6e62SHans Petter Selasky static int cuse_client_kqfilter_read_event(struct knote *kn, long hint);
176fa0f6e62SHans Petter Selasky static int cuse_client_kqfilter_write_event(struct knote *kn, long hint);
177fa0f6e62SHans Petter Selasky 
178fa0f6e62SHans Petter Selasky static struct filterops cuse_client_kqfilter_read_ops = {
179fa0f6e62SHans Petter Selasky 	.f_isfd = 1,
180fa0f6e62SHans Petter Selasky 	.f_detach = cuse_client_kqfilter_read_detach,
181fa0f6e62SHans Petter Selasky 	.f_event = cuse_client_kqfilter_read_event,
182fa0f6e62SHans Petter Selasky };
183fa0f6e62SHans Petter Selasky 
184fa0f6e62SHans Petter Selasky static struct filterops cuse_client_kqfilter_write_ops = {
185fa0f6e62SHans Petter Selasky 	.f_isfd = 1,
186fa0f6e62SHans Petter Selasky 	.f_detach = cuse_client_kqfilter_write_detach,
187fa0f6e62SHans Petter Selasky 	.f_event = cuse_client_kqfilter_write_event,
188fa0f6e62SHans Petter Selasky };
189fa0f6e62SHans Petter Selasky 
190fa0f6e62SHans Petter Selasky static d_open_t cuse_client_open;
191fa0f6e62SHans Petter Selasky static d_close_t cuse_client_close;
192fa0f6e62SHans Petter Selasky static d_ioctl_t cuse_client_ioctl;
193fa0f6e62SHans Petter Selasky static d_read_t cuse_client_read;
194fa0f6e62SHans Petter Selasky static d_write_t cuse_client_write;
195fa0f6e62SHans Petter Selasky static d_poll_t cuse_client_poll;
19656c49c63SHans Petter Selasky static d_mmap_single_t cuse_client_mmap_single;
197fa0f6e62SHans Petter Selasky static d_kqfilter_t cuse_client_kqfilter;
198fa0f6e62SHans Petter Selasky 
199fa0f6e62SHans Petter Selasky static struct cdevsw cuse_client_devsw = {
200fa0f6e62SHans Petter Selasky 	.d_version = D_VERSION,
201fa0f6e62SHans Petter Selasky 	.d_open = cuse_client_open,
202fa0f6e62SHans Petter Selasky 	.d_close = cuse_client_close,
203fa0f6e62SHans Petter Selasky 	.d_ioctl = cuse_client_ioctl,
204fa0f6e62SHans Petter Selasky 	.d_name = "cuse_client",
205fa0f6e62SHans Petter Selasky 	.d_flags = D_TRACKCLOSE,
206fa0f6e62SHans Petter Selasky 	.d_read = cuse_client_read,
207fa0f6e62SHans Petter Selasky 	.d_write = cuse_client_write,
208fa0f6e62SHans Petter Selasky 	.d_poll = cuse_client_poll,
20956c49c63SHans Petter Selasky 	.d_mmap_single = cuse_client_mmap_single,
210fa0f6e62SHans Petter Selasky 	.d_kqfilter = cuse_client_kqfilter,
211fa0f6e62SHans Petter Selasky };
212fa0f6e62SHans Petter Selasky 
213fa0f6e62SHans Petter Selasky static d_open_t cuse_server_open;
214fa0f6e62SHans Petter Selasky static d_close_t cuse_server_close;
215fa0f6e62SHans Petter Selasky static d_ioctl_t cuse_server_ioctl;
216fa0f6e62SHans Petter Selasky static d_read_t cuse_server_read;
217fa0f6e62SHans Petter Selasky static d_write_t cuse_server_write;
218fa0f6e62SHans Petter Selasky static d_poll_t cuse_server_poll;
21956c49c63SHans Petter Selasky static d_mmap_single_t cuse_server_mmap_single;
220fa0f6e62SHans Petter Selasky 
221fa0f6e62SHans Petter Selasky static struct cdevsw cuse_server_devsw = {
222fa0f6e62SHans Petter Selasky 	.d_version = D_VERSION,
223fa0f6e62SHans Petter Selasky 	.d_open = cuse_server_open,
224fa0f6e62SHans Petter Selasky 	.d_close = cuse_server_close,
225fa0f6e62SHans Petter Selasky 	.d_ioctl = cuse_server_ioctl,
226fa0f6e62SHans Petter Selasky 	.d_name = "cuse_server",
227fa0f6e62SHans Petter Selasky 	.d_flags = D_TRACKCLOSE,
228fa0f6e62SHans Petter Selasky 	.d_read = cuse_server_read,
229fa0f6e62SHans Petter Selasky 	.d_write = cuse_server_write,
230fa0f6e62SHans Petter Selasky 	.d_poll = cuse_server_poll,
23156c49c63SHans Petter Selasky 	.d_mmap_single = cuse_server_mmap_single,
232fa0f6e62SHans Petter Selasky };
233fa0f6e62SHans Petter Selasky 
234fa0f6e62SHans Petter Selasky static void cuse_client_is_closing(struct cuse_client *);
235fa0f6e62SHans Petter Selasky static int cuse_free_unit_by_id_locked(struct cuse_server *, int);
236fa0f6e62SHans Petter Selasky 
237fa0f6e62SHans Petter Selasky static void
23898029019SHans Petter Selasky cuse_global_lock(void)
239fa0f6e62SHans Petter Selasky {
24098029019SHans Petter Selasky 	mtx_lock(&cuse_global_mtx);
241fa0f6e62SHans Petter Selasky }
242fa0f6e62SHans Petter Selasky 
243fa0f6e62SHans Petter Selasky static void
24498029019SHans Petter Selasky cuse_global_unlock(void)
245fa0f6e62SHans Petter Selasky {
24698029019SHans Petter Selasky 	mtx_unlock(&cuse_global_mtx);
24798029019SHans Petter Selasky }
24898029019SHans Petter Selasky 
24998029019SHans Petter Selasky static void
25098029019SHans Petter Selasky cuse_server_lock(struct cuse_server *pcs)
25198029019SHans Petter Selasky {
25298029019SHans Petter Selasky 	mtx_lock(&pcs->mtx);
25398029019SHans Petter Selasky }
25498029019SHans Petter Selasky 
25598029019SHans Petter Selasky static void
25698029019SHans Petter Selasky cuse_server_unlock(struct cuse_server *pcs)
25798029019SHans Petter Selasky {
25898029019SHans Petter Selasky 	mtx_unlock(&pcs->mtx);
259fa0f6e62SHans Petter Selasky }
260fa0f6e62SHans Petter Selasky 
261fa0f6e62SHans Petter Selasky static void
262fa0f6e62SHans Petter Selasky cuse_cmd_lock(struct cuse_client_command *pccmd)
263fa0f6e62SHans Petter Selasky {
264fa0f6e62SHans Petter Selasky 	sx_xlock(&pccmd->sx);
265fa0f6e62SHans Petter Selasky }
266fa0f6e62SHans Petter Selasky 
267fa0f6e62SHans Petter Selasky static void
268fa0f6e62SHans Petter Selasky cuse_cmd_unlock(struct cuse_client_command *pccmd)
269fa0f6e62SHans Petter Selasky {
270fa0f6e62SHans Petter Selasky 	sx_xunlock(&pccmd->sx);
271fa0f6e62SHans Petter Selasky }
272fa0f6e62SHans Petter Selasky 
273fa0f6e62SHans Petter Selasky static void
274fa0f6e62SHans Petter Selasky cuse_kern_init(void *arg)
275fa0f6e62SHans Petter Selasky {
276fa0f6e62SHans Petter Selasky 	TAILQ_INIT(&cuse_server_head);
277fa0f6e62SHans Petter Selasky 
27898029019SHans Petter Selasky 	mtx_init(&cuse_global_mtx, "cuse-global-mtx", NULL, MTX_DEF);
279fa0f6e62SHans Petter Selasky 
280fa0f6e62SHans Petter Selasky 	cuse_dev = make_dev(&cuse_server_devsw, 0,
281fa0f6e62SHans Petter Selasky 	    UID_ROOT, GID_OPERATOR, 0600, "cuse");
282fa0f6e62SHans Petter Selasky 
283fa0f6e62SHans Petter Selasky 	printf("Cuse v%d.%d.%d @ /dev/cuse\n",
284fa0f6e62SHans Petter Selasky 	    (CUSE_VERSION >> 16) & 0xFF, (CUSE_VERSION >> 8) & 0xFF,
285fa0f6e62SHans Petter Selasky 	    (CUSE_VERSION >> 0) & 0xFF);
286fa0f6e62SHans Petter Selasky }
287891cf3edSEd Maste SYSINIT(cuse_kern_init, SI_SUB_DEVFS, SI_ORDER_ANY, cuse_kern_init, NULL);
288fa0f6e62SHans Petter Selasky 
289fa0f6e62SHans Petter Selasky static void
290fa0f6e62SHans Petter Selasky cuse_kern_uninit(void *arg)
291fa0f6e62SHans Petter Selasky {
292fa0f6e62SHans Petter Selasky 	void *ptr;
293fa0f6e62SHans Petter Selasky 
294fa0f6e62SHans Petter Selasky 	while (1) {
295fa0f6e62SHans Petter Selasky 		printf("Cuse: Please exit all /dev/cuse instances "
296fa0f6e62SHans Petter Selasky 		    "and processes which have used this device.\n");
297fa0f6e62SHans Petter Selasky 
298fa0f6e62SHans Petter Selasky 		pause("DRAIN", 2 * hz);
299fa0f6e62SHans Petter Selasky 
30098029019SHans Petter Selasky 		cuse_global_lock();
301fa0f6e62SHans Petter Selasky 		ptr = TAILQ_FIRST(&cuse_server_head);
30298029019SHans Petter Selasky 		cuse_global_unlock();
303fa0f6e62SHans Petter Selasky 
304fa0f6e62SHans Petter Selasky 		if (ptr == NULL)
305fa0f6e62SHans Petter Selasky 			break;
306fa0f6e62SHans Petter Selasky 	}
307fa0f6e62SHans Petter Selasky 
308fa0f6e62SHans Petter Selasky 	if (cuse_dev != NULL)
309fa0f6e62SHans Petter Selasky 		destroy_dev(cuse_dev);
310fa0f6e62SHans Petter Selasky 
31198029019SHans Petter Selasky 	mtx_destroy(&cuse_global_mtx);
312fa0f6e62SHans Petter Selasky }
313fa0f6e62SHans Petter Selasky SYSUNINIT(cuse_kern_uninit, SI_SUB_DEVFS, SI_ORDER_ANY, cuse_kern_uninit, 0);
314fa0f6e62SHans Petter Selasky 
315fa0f6e62SHans Petter Selasky static int
316fa0f6e62SHans Petter Selasky cuse_server_get(struct cuse_server **ppcs)
317fa0f6e62SHans Petter Selasky {
318fa0f6e62SHans Petter Selasky 	struct cuse_server *pcs;
319fa0f6e62SHans Petter Selasky 	int error;
320fa0f6e62SHans Petter Selasky 
321fa0f6e62SHans Petter Selasky 	error = devfs_get_cdevpriv((void **)&pcs);
322fa0f6e62SHans Petter Selasky 	if (error != 0) {
323fa0f6e62SHans Petter Selasky 		*ppcs = NULL;
324fa0f6e62SHans Petter Selasky 		return (error);
325fa0f6e62SHans Petter Selasky 	}
326fa0f6e62SHans Petter Selasky 	if (pcs->is_closing) {
327fa0f6e62SHans Petter Selasky 		*ppcs = NULL;
328fa0f6e62SHans Petter Selasky 		return (EINVAL);
329fa0f6e62SHans Petter Selasky 	}
330fa0f6e62SHans Petter Selasky 	*ppcs = pcs;
331fa0f6e62SHans Petter Selasky 	return (0);
332fa0f6e62SHans Petter Selasky }
333fa0f6e62SHans Petter Selasky 
334fa0f6e62SHans Petter Selasky static void
335fa0f6e62SHans Petter Selasky cuse_server_is_closing(struct cuse_server *pcs)
336fa0f6e62SHans Petter Selasky {
337fa0f6e62SHans Petter Selasky 	struct cuse_client *pcc;
338fa0f6e62SHans Petter Selasky 
339fa0f6e62SHans Petter Selasky 	if (pcs->is_closing)
340fa0f6e62SHans Petter Selasky 		return;
341fa0f6e62SHans Petter Selasky 
342fa0f6e62SHans Petter Selasky 	pcs->is_closing = 1;
343fa0f6e62SHans Petter Selasky 
344fa0f6e62SHans Petter Selasky 	TAILQ_FOREACH(pcc, &pcs->hcli, entry) {
345fa0f6e62SHans Petter Selasky 		cuse_client_is_closing(pcc);
346fa0f6e62SHans Petter Selasky 	}
347fa0f6e62SHans Petter Selasky }
348fa0f6e62SHans Petter Selasky 
349fa0f6e62SHans Petter Selasky static struct cuse_client_command *
350fa0f6e62SHans Petter Selasky cuse_server_find_command(struct cuse_server *pcs, struct thread *td)
351fa0f6e62SHans Petter Selasky {
352fa0f6e62SHans Petter Selasky 	struct cuse_client *pcc;
353fa0f6e62SHans Petter Selasky 	int n;
354fa0f6e62SHans Petter Selasky 
355fa0f6e62SHans Petter Selasky 	if (pcs->is_closing)
356fa0f6e62SHans Petter Selasky 		goto done;
357fa0f6e62SHans Petter Selasky 
358fa0f6e62SHans Petter Selasky 	TAILQ_FOREACH(pcc, &pcs->hcli, entry) {
359fa0f6e62SHans Petter Selasky 		if (CUSE_CLIENT_CLOSING(pcc))
360fa0f6e62SHans Petter Selasky 			continue;
361fa0f6e62SHans Petter Selasky 		for (n = 0; n != CUSE_CMD_MAX; n++) {
362fa0f6e62SHans Petter Selasky 			if (pcc->cmds[n].entered == td)
363fa0f6e62SHans Petter Selasky 				return (&pcc->cmds[n]);
364fa0f6e62SHans Petter Selasky 		}
365fa0f6e62SHans Petter Selasky 	}
366fa0f6e62SHans Petter Selasky done:
367fa0f6e62SHans Petter Selasky 	return (NULL);
368fa0f6e62SHans Petter Selasky }
369fa0f6e62SHans Petter Selasky 
370fa0f6e62SHans Petter Selasky static void
371fa0f6e62SHans Petter Selasky cuse_str_filter(char *ptr)
372fa0f6e62SHans Petter Selasky {
373fa0f6e62SHans Petter Selasky 	int c;
374fa0f6e62SHans Petter Selasky 
375fa0f6e62SHans Petter Selasky 	while (((c = *ptr) != 0)) {
376fa0f6e62SHans Petter Selasky 		if ((c >= 'a') && (c <= 'z')) {
377fa0f6e62SHans Petter Selasky 			ptr++;
378fa0f6e62SHans Petter Selasky 			continue;
379fa0f6e62SHans Petter Selasky 		}
380fa0f6e62SHans Petter Selasky 		if ((c >= 'A') && (c <= 'Z')) {
381fa0f6e62SHans Petter Selasky 			ptr++;
382fa0f6e62SHans Petter Selasky 			continue;
383fa0f6e62SHans Petter Selasky 		}
384fa0f6e62SHans Petter Selasky 		if ((c >= '0') && (c <= '9')) {
385fa0f6e62SHans Petter Selasky 			ptr++;
386fa0f6e62SHans Petter Selasky 			continue;
387fa0f6e62SHans Petter Selasky 		}
388fa0f6e62SHans Petter Selasky 		if ((c == '.') || (c == '_') || (c == '/')) {
389fa0f6e62SHans Petter Selasky 			ptr++;
390fa0f6e62SHans Petter Selasky 			continue;
391fa0f6e62SHans Petter Selasky 		}
392fa0f6e62SHans Petter Selasky 		*ptr = '_';
393fa0f6e62SHans Petter Selasky 
394fa0f6e62SHans Petter Selasky 		ptr++;
395fa0f6e62SHans Petter Selasky 	}
396fa0f6e62SHans Petter Selasky }
397fa0f6e62SHans Petter Selasky 
398fa0f6e62SHans Petter Selasky static int
399fa0f6e62SHans Petter Selasky cuse_convert_error(int error)
400fa0f6e62SHans Petter Selasky {
401fa0f6e62SHans Petter Selasky 	;				/* indent fix */
402fa0f6e62SHans Petter Selasky 	switch (error) {
403fa0f6e62SHans Petter Selasky 	case CUSE_ERR_NONE:
404fa0f6e62SHans Petter Selasky 		return (0);
405fa0f6e62SHans Petter Selasky 	case CUSE_ERR_BUSY:
406fa0f6e62SHans Petter Selasky 		return (EBUSY);
407fa0f6e62SHans Petter Selasky 	case CUSE_ERR_WOULDBLOCK:
408fa0f6e62SHans Petter Selasky 		return (EWOULDBLOCK);
409fa0f6e62SHans Petter Selasky 	case CUSE_ERR_INVALID:
410fa0f6e62SHans Petter Selasky 		return (EINVAL);
411fa0f6e62SHans Petter Selasky 	case CUSE_ERR_NO_MEMORY:
412fa0f6e62SHans Petter Selasky 		return (ENOMEM);
413fa0f6e62SHans Petter Selasky 	case CUSE_ERR_FAULT:
414fa0f6e62SHans Petter Selasky 		return (EFAULT);
415fa0f6e62SHans Petter Selasky 	case CUSE_ERR_SIGNAL:
416fa0f6e62SHans Petter Selasky 		return (EINTR);
4179f16d9c9SHans Petter Selasky 	case CUSE_ERR_NO_DEVICE:
4189f16d9c9SHans Petter Selasky 		return (ENODEV);
419fa0f6e62SHans Petter Selasky 	default:
420fa0f6e62SHans Petter Selasky 		return (ENXIO);
421fa0f6e62SHans Petter Selasky 	}
422fa0f6e62SHans Petter Selasky }
423fa0f6e62SHans Petter Selasky 
424fa0f6e62SHans Petter Selasky static void
42556c49c63SHans Petter Selasky cuse_vm_memory_free(struct cuse_memory *mem)
426fa0f6e62SHans Petter Selasky {
42756c49c63SHans Petter Selasky 	/* last user is gone - free */
42856c49c63SHans Petter Selasky 	vm_object_deallocate(mem->object);
429fa0f6e62SHans Petter Selasky 
43056c49c63SHans Petter Selasky 	/* free CUSE memory */
43156c49c63SHans Petter Selasky 	free(mem, M_CUSE);
432fa0f6e62SHans Petter Selasky }
433fa0f6e62SHans Petter Selasky 
434fa0f6e62SHans Petter Selasky static int
43556c49c63SHans Petter Selasky cuse_server_alloc_memory(struct cuse_server *pcs, uint32_t alloc_nr,
43656c49c63SHans Petter Selasky     uint32_t page_count)
437fa0f6e62SHans Petter Selasky {
43856c49c63SHans Petter Selasky 	struct cuse_memory *temp;
43956c49c63SHans Petter Selasky 	struct cuse_memory *mem;
44056c49c63SHans Petter Selasky 	vm_object_t object;
441fa0f6e62SHans Petter Selasky 	int error;
442fa0f6e62SHans Petter Selasky 
44356c49c63SHans Petter Selasky 	mem = malloc(sizeof(*mem), M_CUSE, M_WAITOK | M_ZERO);
444fa0f6e62SHans Petter Selasky 
44556c49c63SHans Petter Selasky 	object = vm_pager_allocate(OBJT_SWAP, NULL, PAGE_SIZE * page_count,
44656c49c63SHans Petter Selasky 	    VM_PROT_DEFAULT, 0, curthread->td_ucred);
44756c49c63SHans Petter Selasky 	if (object == NULL) {
448fa0f6e62SHans Petter Selasky 		error = ENOMEM;
44956c49c63SHans Petter Selasky 		goto error_0;
45056c49c63SHans Petter Selasky 	}
451fa0f6e62SHans Petter Selasky 
45298029019SHans Petter Selasky 	cuse_server_lock(pcs);
45356c49c63SHans Petter Selasky 	/* check if allocation number already exists */
45456c49c63SHans Petter Selasky 	TAILQ_FOREACH(temp, &pcs->hmem, entry) {
45556c49c63SHans Petter Selasky 		if (temp->alloc_nr == alloc_nr)
45656c49c63SHans Petter Selasky 			break;
45756c49c63SHans Petter Selasky 	}
45856c49c63SHans Petter Selasky 	if (temp != NULL) {
45998029019SHans Petter Selasky 		cuse_server_unlock(pcs);
46056c49c63SHans Petter Selasky 		error = EBUSY;
46156c49c63SHans Petter Selasky 		goto error_1;
46256c49c63SHans Petter Selasky 	}
46356c49c63SHans Petter Selasky 	mem->object = object;
46456c49c63SHans Petter Selasky 	mem->page_count = page_count;
46556c49c63SHans Petter Selasky 	mem->alloc_nr = alloc_nr;
46656c49c63SHans Petter Selasky 	TAILQ_INSERT_TAIL(&pcs->hmem, mem, entry);
46798029019SHans Petter Selasky 	cuse_server_unlock(pcs);
46856c49c63SHans Petter Selasky 
46956c49c63SHans Petter Selasky 	return (0);
47056c49c63SHans Petter Selasky 
47156c49c63SHans Petter Selasky error_1:
47256c49c63SHans Petter Selasky 	vm_object_deallocate(object);
47356c49c63SHans Petter Selasky error_0:
47456c49c63SHans Petter Selasky 	free(mem, M_CUSE);
475fa0f6e62SHans Petter Selasky 	return (error);
476fa0f6e62SHans Petter Selasky }
47756c49c63SHans Petter Selasky 
47856c49c63SHans Petter Selasky static int
47956c49c63SHans Petter Selasky cuse_server_free_memory(struct cuse_server *pcs, uint32_t alloc_nr)
48056c49c63SHans Petter Selasky {
48156c49c63SHans Petter Selasky 	struct cuse_memory *mem;
48256c49c63SHans Petter Selasky 
48398029019SHans Petter Selasky 	cuse_server_lock(pcs);
48456c49c63SHans Petter Selasky 	TAILQ_FOREACH(mem, &pcs->hmem, entry) {
48556c49c63SHans Petter Selasky 		if (mem->alloc_nr == alloc_nr)
48656c49c63SHans Petter Selasky 			break;
48756c49c63SHans Petter Selasky 	}
48856c49c63SHans Petter Selasky 	if (mem == NULL) {
48998029019SHans Petter Selasky 		cuse_server_unlock(pcs);
49056c49c63SHans Petter Selasky 		return (EINVAL);
49156c49c63SHans Petter Selasky 	}
49256c49c63SHans Petter Selasky 	TAILQ_REMOVE(&pcs->hmem, mem, entry);
49398029019SHans Petter Selasky 	cuse_server_unlock(pcs);
49456c49c63SHans Petter Selasky 
49556c49c63SHans Petter Selasky 	cuse_vm_memory_free(mem);
496fa0f6e62SHans Petter Selasky 
497fa0f6e62SHans Petter Selasky 	return (0);
498fa0f6e62SHans Petter Selasky }
499fa0f6e62SHans Petter Selasky 
500fa0f6e62SHans Petter Selasky static int
501fa0f6e62SHans Petter Selasky cuse_client_get(struct cuse_client **ppcc)
502fa0f6e62SHans Petter Selasky {
503fa0f6e62SHans Petter Selasky 	struct cuse_client *pcc;
504fa0f6e62SHans Petter Selasky 	int error;
505fa0f6e62SHans Petter Selasky 
506fa0f6e62SHans Petter Selasky 	/* try to get private data */
507fa0f6e62SHans Petter Selasky 	error = devfs_get_cdevpriv((void **)&pcc);
508fa0f6e62SHans Petter Selasky 	if (error != 0) {
509fa0f6e62SHans Petter Selasky 		*ppcc = NULL;
510fa0f6e62SHans Petter Selasky 		return (error);
511fa0f6e62SHans Petter Selasky 	}
512fa0f6e62SHans Petter Selasky 	if (CUSE_CLIENT_CLOSING(pcc) || pcc->server->is_closing) {
513fa0f6e62SHans Petter Selasky 		*ppcc = NULL;
514fa0f6e62SHans Petter Selasky 		return (EINVAL);
515fa0f6e62SHans Petter Selasky 	}
516fa0f6e62SHans Petter Selasky 	*ppcc = pcc;
517fa0f6e62SHans Petter Selasky 	return (0);
518fa0f6e62SHans Petter Selasky }
519fa0f6e62SHans Petter Selasky 
520fa0f6e62SHans Petter Selasky static void
521fa0f6e62SHans Petter Selasky cuse_client_is_closing(struct cuse_client *pcc)
522fa0f6e62SHans Petter Selasky {
523fa0f6e62SHans Petter Selasky 	struct cuse_client_command *pccmd;
524fa0f6e62SHans Petter Selasky 	uint32_t n;
525fa0f6e62SHans Petter Selasky 
526fa0f6e62SHans Petter Selasky 	if (CUSE_CLIENT_CLOSING(pcc))
527fa0f6e62SHans Petter Selasky 		return;
528fa0f6e62SHans Petter Selasky 
529fa0f6e62SHans Petter Selasky 	pcc->cflags |= CUSE_CLI_IS_CLOSING;
530fa0f6e62SHans Petter Selasky 	pcc->server_dev = NULL;
531fa0f6e62SHans Petter Selasky 
532fa0f6e62SHans Petter Selasky 	for (n = 0; n != CUSE_CMD_MAX; n++) {
533fa0f6e62SHans Petter Selasky 		pccmd = &pcc->cmds[n];
534fa0f6e62SHans Petter Selasky 
535fa0f6e62SHans Petter Selasky 		if (pccmd->entry.tqe_prev != NULL) {
536fa0f6e62SHans Petter Selasky 			TAILQ_REMOVE(&pcc->server->head, pccmd, entry);
537fa0f6e62SHans Petter Selasky 			pccmd->entry.tqe_prev = NULL;
538fa0f6e62SHans Petter Selasky 		}
539fa0f6e62SHans Petter Selasky 		cv_broadcast(&pccmd->cv);
540fa0f6e62SHans Petter Selasky 	}
541fa0f6e62SHans Petter Selasky }
542fa0f6e62SHans Petter Selasky 
543fa0f6e62SHans Petter Selasky static void
544fa0f6e62SHans Petter Selasky cuse_client_send_command_locked(struct cuse_client_command *pccmd,
545beebd9aaSHans Petter Selasky     uintptr_t data_ptr, unsigned long arg, int fflags, int ioflag)
546fa0f6e62SHans Petter Selasky {
547fa0f6e62SHans Petter Selasky 	unsigned long cuse_fflags = 0;
548fa0f6e62SHans Petter Selasky 	struct cuse_server *pcs;
549fa0f6e62SHans Petter Selasky 
550fa0f6e62SHans Petter Selasky 	if (fflags & FREAD)
551fa0f6e62SHans Petter Selasky 		cuse_fflags |= CUSE_FFLAG_READ;
552fa0f6e62SHans Petter Selasky 
553fa0f6e62SHans Petter Selasky 	if (fflags & FWRITE)
554fa0f6e62SHans Petter Selasky 		cuse_fflags |= CUSE_FFLAG_WRITE;
555fa0f6e62SHans Petter Selasky 
556fa0f6e62SHans Petter Selasky 	if (ioflag & IO_NDELAY)
557fa0f6e62SHans Petter Selasky 		cuse_fflags |= CUSE_FFLAG_NONBLOCK;
558db92a6cdSHans Petter Selasky #if defined(__LP64__)
559db92a6cdSHans Petter Selasky 	if (SV_CURPROC_FLAG(SV_ILP32))
560db92a6cdSHans Petter Selasky 		cuse_fflags |= CUSE_FFLAG_COMPAT32;
561db92a6cdSHans Petter Selasky #endif
562fa0f6e62SHans Petter Selasky 	pccmd->sub.fflags = cuse_fflags;
563fa0f6e62SHans Petter Selasky 	pccmd->sub.data_pointer = data_ptr;
564fa0f6e62SHans Petter Selasky 	pccmd->sub.argument = arg;
565fa0f6e62SHans Petter Selasky 
566fa0f6e62SHans Petter Selasky 	pcs = pccmd->client->server;
567fa0f6e62SHans Petter Selasky 
568fa0f6e62SHans Petter Selasky 	if ((pccmd->entry.tqe_prev == NULL) &&
569fa0f6e62SHans Petter Selasky 	    (CUSE_CLIENT_CLOSING(pccmd->client) == 0) &&
570fa0f6e62SHans Petter Selasky 	    (pcs->is_closing == 0)) {
571fa0f6e62SHans Petter Selasky 		TAILQ_INSERT_TAIL(&pcs->head, pccmd, entry);
572fa0f6e62SHans Petter Selasky 		cv_signal(&pcs->cv);
573fa0f6e62SHans Petter Selasky 	}
574fa0f6e62SHans Petter Selasky }
575fa0f6e62SHans Petter Selasky 
576fa0f6e62SHans Petter Selasky static void
577fa0f6e62SHans Petter Selasky cuse_client_got_signal(struct cuse_client_command *pccmd)
578fa0f6e62SHans Petter Selasky {
579fa0f6e62SHans Petter Selasky 	struct cuse_server *pcs;
580fa0f6e62SHans Petter Selasky 
581fa0f6e62SHans Petter Selasky 	pccmd->got_signal = 1;
582fa0f6e62SHans Petter Selasky 
583fa0f6e62SHans Petter Selasky 	pccmd = &pccmd->client->cmds[CUSE_CMD_SIGNAL];
584fa0f6e62SHans Petter Selasky 
585fa0f6e62SHans Petter Selasky 	pcs = pccmd->client->server;
586fa0f6e62SHans Petter Selasky 
587fa0f6e62SHans Petter Selasky 	if ((pccmd->entry.tqe_prev == NULL) &&
588fa0f6e62SHans Petter Selasky 	    (CUSE_CLIENT_CLOSING(pccmd->client) == 0) &&
589fa0f6e62SHans Petter Selasky 	    (pcs->is_closing == 0)) {
590fa0f6e62SHans Petter Selasky 		TAILQ_INSERT_TAIL(&pcs->head, pccmd, entry);
591fa0f6e62SHans Petter Selasky 		cv_signal(&pcs->cv);
592fa0f6e62SHans Petter Selasky 	}
593fa0f6e62SHans Petter Selasky }
594fa0f6e62SHans Petter Selasky 
595fa0f6e62SHans Petter Selasky static int
596fa0f6e62SHans Petter Selasky cuse_client_receive_command_locked(struct cuse_client_command *pccmd,
597fa0f6e62SHans Petter Selasky     uint8_t *arg_ptr, uint32_t arg_len)
598fa0f6e62SHans Petter Selasky {
59998029019SHans Petter Selasky 	struct cuse_server *pcs;
600fa0f6e62SHans Petter Selasky 	int error;
601fa0f6e62SHans Petter Selasky 
60298029019SHans Petter Selasky 	pcs = pccmd->client->server;
603fa0f6e62SHans Petter Selasky 	error = 0;
604fa0f6e62SHans Petter Selasky 
605fa0f6e62SHans Petter Selasky 	pccmd->proc_curr = curthread->td_proc;
606fa0f6e62SHans Petter Selasky 
60798029019SHans Petter Selasky 	if (CUSE_CLIENT_CLOSING(pccmd->client) || pcs->is_closing) {
608fa0f6e62SHans Petter Selasky 		error = CUSE_ERR_OTHER;
609fa0f6e62SHans Petter Selasky 		goto done;
610fa0f6e62SHans Petter Selasky 	}
611fa0f6e62SHans Petter Selasky 	while (pccmd->command == CUSE_CMD_NONE) {
612fa0f6e62SHans Petter Selasky 		if (error != 0) {
61398029019SHans Petter Selasky 			cv_wait(&pccmd->cv, &pcs->mtx);
614fa0f6e62SHans Petter Selasky 		} else {
61598029019SHans Petter Selasky 			error = cv_wait_sig(&pccmd->cv, &pcs->mtx);
616fa0f6e62SHans Petter Selasky 
617fa0f6e62SHans Petter Selasky 			if (error != 0)
618fa0f6e62SHans Petter Selasky 				cuse_client_got_signal(pccmd);
619fa0f6e62SHans Petter Selasky 		}
62098029019SHans Petter Selasky 		if (CUSE_CLIENT_CLOSING(pccmd->client) || pcs->is_closing) {
621fa0f6e62SHans Petter Selasky 			error = CUSE_ERR_OTHER;
622fa0f6e62SHans Petter Selasky 			goto done;
623fa0f6e62SHans Petter Selasky 		}
624fa0f6e62SHans Petter Selasky 	}
625fa0f6e62SHans Petter Selasky 
626fa0f6e62SHans Petter Selasky 	error = pccmd->error;
627fa0f6e62SHans Petter Selasky 	pccmd->command = CUSE_CMD_NONE;
628fa0f6e62SHans Petter Selasky 	cv_signal(&pccmd->cv);
629fa0f6e62SHans Petter Selasky 
630fa0f6e62SHans Petter Selasky done:
631fa0f6e62SHans Petter Selasky 
632fa0f6e62SHans Petter Selasky 	/* wait until all process references are gone */
633fa0f6e62SHans Petter Selasky 
634fa0f6e62SHans Petter Selasky 	pccmd->proc_curr = NULL;
635fa0f6e62SHans Petter Selasky 
636fa0f6e62SHans Petter Selasky 	while (pccmd->proc_refs != 0)
63798029019SHans Petter Selasky 		cv_wait(&pccmd->cv, &pcs->mtx);
638fa0f6e62SHans Petter Selasky 
639fa0f6e62SHans Petter Selasky 	return (error);
640fa0f6e62SHans Petter Selasky }
641fa0f6e62SHans Petter Selasky 
642fa0f6e62SHans Petter Selasky /*------------------------------------------------------------------------*
643fa0f6e62SHans Petter Selasky  *	CUSE SERVER PART
644fa0f6e62SHans Petter Selasky  *------------------------------------------------------------------------*/
645fa0f6e62SHans Petter Selasky 
646fa0f6e62SHans Petter Selasky static void
647fa0f6e62SHans Petter Selasky cuse_server_free_dev(struct cuse_server_dev *pcsd)
648fa0f6e62SHans Petter Selasky {
649fa0f6e62SHans Petter Selasky 	struct cuse_server *pcs;
650fa0f6e62SHans Petter Selasky 	struct cuse_client *pcc;
651fa0f6e62SHans Petter Selasky 
652fa0f6e62SHans Petter Selasky 	/* get server pointer */
653fa0f6e62SHans Petter Selasky 	pcs = pcsd->server;
654fa0f6e62SHans Petter Selasky 
655fa0f6e62SHans Petter Selasky 	/* prevent creation of more devices */
65698029019SHans Petter Selasky 	cuse_server_lock(pcs);
657fa0f6e62SHans Petter Selasky 	if (pcsd->kern_dev != NULL)
658fa0f6e62SHans Petter Selasky 		pcsd->kern_dev->si_drv1 = NULL;
659fa0f6e62SHans Petter Selasky 
660fa0f6e62SHans Petter Selasky 	TAILQ_FOREACH(pcc, &pcs->hcli, entry) {
661fa0f6e62SHans Petter Selasky 		if (pcc->server_dev == pcsd)
662fa0f6e62SHans Petter Selasky 			cuse_client_is_closing(pcc);
663fa0f6e62SHans Petter Selasky 	}
66498029019SHans Petter Selasky 	cuse_server_unlock(pcs);
665fa0f6e62SHans Petter Selasky 
666fa0f6e62SHans Petter Selasky 	/* destroy device, if any */
667fa0f6e62SHans Petter Selasky 	if (pcsd->kern_dev != NULL) {
668fa0f6e62SHans Petter Selasky 		/* destroy device synchronously */
669fa0f6e62SHans Petter Selasky 		destroy_dev(pcsd->kern_dev);
670fa0f6e62SHans Petter Selasky 	}
671fa0f6e62SHans Petter Selasky 	free(pcsd, M_CUSE);
672fa0f6e62SHans Petter Selasky }
673fa0f6e62SHans Petter Selasky 
674fa0f6e62SHans Petter Selasky static void
67556c49c63SHans Petter Selasky cuse_server_unref(struct cuse_server *pcs)
676fa0f6e62SHans Petter Selasky {
677fa0f6e62SHans Petter Selasky 	struct cuse_server_dev *pcsd;
67856c49c63SHans Petter Selasky 	struct cuse_memory *mem;
679fa0f6e62SHans Petter Selasky 
68098029019SHans Petter Selasky 	cuse_server_lock(pcs);
68198029019SHans Petter Selasky 	if (--(pcs->refs) != 0) {
68298029019SHans Petter Selasky 		cuse_server_unlock(pcs);
683fa0f6e62SHans Petter Selasky 		return;
684fa0f6e62SHans Petter Selasky 	}
685fa0f6e62SHans Petter Selasky 	cuse_server_is_closing(pcs);
68695700043SHans Petter Selasky 	/* final client wakeup, if any */
68795700043SHans Petter Selasky 	cuse_server_wakeup_all_client_locked(pcs);
688fa0f6e62SHans Petter Selasky 
68998029019SHans Petter Selasky 	cuse_global_lock();
690fa0f6e62SHans Petter Selasky 	TAILQ_REMOVE(&cuse_server_head, pcs, entry);
69198029019SHans Petter Selasky 	cuse_global_unlock();
692fa0f6e62SHans Petter Selasky 
693fa0f6e62SHans Petter Selasky 	while ((pcsd = TAILQ_FIRST(&pcs->hdev)) != NULL) {
694fa0f6e62SHans Petter Selasky 		TAILQ_REMOVE(&pcs->hdev, pcsd, entry);
69598029019SHans Petter Selasky 		cuse_server_unlock(pcs);
696fa0f6e62SHans Petter Selasky 		cuse_server_free_dev(pcsd);
69798029019SHans Petter Selasky 		cuse_server_lock(pcs);
698fa0f6e62SHans Petter Selasky 	}
699fa0f6e62SHans Petter Selasky 
70043a9329eSHans Petter Selasky 	cuse_free_unit_by_id_locked(pcs, -1);
70143a9329eSHans Petter Selasky 
70256c49c63SHans Petter Selasky 	while ((mem = TAILQ_FIRST(&pcs->hmem)) != NULL) {
70356c49c63SHans Petter Selasky 		TAILQ_REMOVE(&pcs->hmem, mem, entry);
70498029019SHans Petter Selasky 		cuse_server_unlock(pcs);
70556c49c63SHans Petter Selasky 		cuse_vm_memory_free(mem);
70698029019SHans Petter Selasky 		cuse_server_lock(pcs);
70756c49c63SHans Petter Selasky 	}
708fa0f6e62SHans Petter Selasky 
709fa0f6e62SHans Petter Selasky 	knlist_clear(&pcs->selinfo.si_note, 1);
710fa0f6e62SHans Petter Selasky 	knlist_destroy(&pcs->selinfo.si_note);
711fa0f6e62SHans Petter Selasky 
71298029019SHans Petter Selasky 	cuse_server_unlock(pcs);
713fa0f6e62SHans Petter Selasky 
714fa0f6e62SHans Petter Selasky 	seldrain(&pcs->selinfo);
715fa0f6e62SHans Petter Selasky 
716fa0f6e62SHans Petter Selasky 	cv_destroy(&pcs->cv);
717fa0f6e62SHans Petter Selasky 
71898029019SHans Petter Selasky 	mtx_destroy(&pcs->mtx);
71998029019SHans Petter Selasky 
720fa0f6e62SHans Petter Selasky 	free(pcs, M_CUSE);
721fa0f6e62SHans Petter Selasky }
722fa0f6e62SHans Petter Selasky 
723c7ffaed9SHans Petter Selasky static int
724c7ffaed9SHans Petter Selasky cuse_server_do_close(struct cuse_server *pcs)
725c7ffaed9SHans Petter Selasky {
726c7ffaed9SHans Petter Selasky 	int retval;
727c7ffaed9SHans Petter Selasky 
72898029019SHans Petter Selasky 	cuse_server_lock(pcs);
729c7ffaed9SHans Petter Selasky 	cuse_server_is_closing(pcs);
730c7ffaed9SHans Petter Selasky 	/* final client wakeup, if any */
731c7ffaed9SHans Petter Selasky 	cuse_server_wakeup_all_client_locked(pcs);
732c7ffaed9SHans Petter Selasky 
733c7ffaed9SHans Petter Selasky 	knlist_clear(&pcs->selinfo.si_note, 1);
734c7ffaed9SHans Petter Selasky 
735c7ffaed9SHans Petter Selasky 	retval = pcs->refs;
73698029019SHans Petter Selasky 	cuse_server_unlock(pcs);
737c7ffaed9SHans Petter Selasky 
738c7ffaed9SHans Petter Selasky 	return (retval);
739c7ffaed9SHans Petter Selasky }
740c7ffaed9SHans Petter Selasky 
74156c49c63SHans Petter Selasky static void
74256c49c63SHans Petter Selasky cuse_server_free(void *arg)
74356c49c63SHans Petter Selasky {
74456c49c63SHans Petter Selasky 	struct cuse_server *pcs = arg;
74556c49c63SHans Petter Selasky 
746c7ffaed9SHans Petter Selasky 	/*
747c7ffaed9SHans Petter Selasky 	 * The final server unref should be done by the server thread
748c7ffaed9SHans Petter Selasky 	 * to prevent deadlock in the client cdevpriv destructor,
749c7ffaed9SHans Petter Selasky 	 * which cannot destroy itself.
750c7ffaed9SHans Petter Selasky 	 */
751c7ffaed9SHans Petter Selasky 	while (cuse_server_do_close(pcs) != 1)
752c7ffaed9SHans Petter Selasky 		pause("W", hz);
753c7ffaed9SHans Petter Selasky 
754c7ffaed9SHans Petter Selasky 	/* drop final refcount */
75556c49c63SHans Petter Selasky 	cuse_server_unref(pcs);
75656c49c63SHans Petter Selasky }
75756c49c63SHans Petter Selasky 
758fa0f6e62SHans Petter Selasky static int
759fa0f6e62SHans Petter Selasky cuse_server_open(struct cdev *dev, int fflags, int devtype, struct thread *td)
760fa0f6e62SHans Petter Selasky {
761fa0f6e62SHans Petter Selasky 	struct cuse_server *pcs;
762fa0f6e62SHans Petter Selasky 
763fa0f6e62SHans Petter Selasky 	pcs = malloc(sizeof(*pcs), M_CUSE, M_WAITOK | M_ZERO);
764fa0f6e62SHans Petter Selasky 
765fa0f6e62SHans Petter Selasky 	if (devfs_set_cdevpriv(pcs, &cuse_server_free)) {
766fa0f6e62SHans Petter Selasky 		printf("Cuse: Cannot set cdevpriv.\n");
767fa0f6e62SHans Petter Selasky 		free(pcs, M_CUSE);
768fa0f6e62SHans Petter Selasky 		return (ENOMEM);
769fa0f6e62SHans Petter Selasky 	}
770ac60e801SHans Petter Selasky 	/* store current process ID */
771ac60e801SHans Petter Selasky 	pcs->pid = curproc->p_pid;
772ac60e801SHans Petter Selasky 
773fa0f6e62SHans Petter Selasky 	TAILQ_INIT(&pcs->head);
774fa0f6e62SHans Petter Selasky 	TAILQ_INIT(&pcs->hdev);
775fa0f6e62SHans Petter Selasky 	TAILQ_INIT(&pcs->hcli);
77656c49c63SHans Petter Selasky 	TAILQ_INIT(&pcs->hmem);
777fa0f6e62SHans Petter Selasky 
778fa0f6e62SHans Petter Selasky 	cv_init(&pcs->cv, "cuse-server-cv");
779fa0f6e62SHans Petter Selasky 
78098029019SHans Petter Selasky 	mtx_init(&pcs->mtx, "cuse-server-mtx", NULL, MTX_DEF);
781fa0f6e62SHans Petter Selasky 
78298029019SHans Petter Selasky 	knlist_init_mtx(&pcs->selinfo.si_note, &pcs->mtx);
78398029019SHans Petter Selasky 
78498029019SHans Petter Selasky 	cuse_global_lock();
785fa0f6e62SHans Petter Selasky 	pcs->refs++;
786fa0f6e62SHans Petter Selasky 	TAILQ_INSERT_TAIL(&cuse_server_head, pcs, entry);
78798029019SHans Petter Selasky 	cuse_global_unlock();
788fa0f6e62SHans Petter Selasky 
789fa0f6e62SHans Petter Selasky 	return (0);
790fa0f6e62SHans Petter Selasky }
791fa0f6e62SHans Petter Selasky 
792fa0f6e62SHans Petter Selasky static int
793fa0f6e62SHans Petter Selasky cuse_server_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
794fa0f6e62SHans Petter Selasky {
795fa0f6e62SHans Petter Selasky 	struct cuse_server *pcs;
796fa0f6e62SHans Petter Selasky 
797c7ffaed9SHans Petter Selasky 	if (cuse_server_get(&pcs) == 0)
798c7ffaed9SHans Petter Selasky 		cuse_server_do_close(pcs);
799fa0f6e62SHans Petter Selasky 
800fa0f6e62SHans Petter Selasky 	return (0);
801fa0f6e62SHans Petter Selasky }
802fa0f6e62SHans Petter Selasky 
803fa0f6e62SHans Petter Selasky static int
804fa0f6e62SHans Petter Selasky cuse_server_read(struct cdev *dev, struct uio *uio, int ioflag)
805fa0f6e62SHans Petter Selasky {
806fa0f6e62SHans Petter Selasky 	return (ENXIO);
807fa0f6e62SHans Petter Selasky }
808fa0f6e62SHans Petter Selasky 
809fa0f6e62SHans Petter Selasky static int
810fa0f6e62SHans Petter Selasky cuse_server_write(struct cdev *dev, struct uio *uio, int ioflag)
811fa0f6e62SHans Petter Selasky {
812fa0f6e62SHans Petter Selasky 	return (ENXIO);
813fa0f6e62SHans Petter Selasky }
814fa0f6e62SHans Petter Selasky 
815fa0f6e62SHans Petter Selasky static int
81698029019SHans Petter Selasky cuse_server_ioctl_copy_locked(struct cuse_server *pcs,
81798029019SHans Petter Selasky     struct cuse_client_command *pccmd,
818fa0f6e62SHans Petter Selasky     struct cuse_data_chunk *pchk, int isread)
819fa0f6e62SHans Petter Selasky {
820fa0f6e62SHans Petter Selasky 	struct proc *p_proc;
821fa0f6e62SHans Petter Selasky 	uint32_t offset;
822fa0f6e62SHans Petter Selasky 	int error;
823fa0f6e62SHans Petter Selasky 
824fa0f6e62SHans Petter Selasky 	offset = pchk->peer_ptr - CUSE_BUF_MIN_PTR;
825fa0f6e62SHans Petter Selasky 
826fa0f6e62SHans Petter Selasky 	if (pchk->length > CUSE_BUFFER_MAX)
827fa0f6e62SHans Petter Selasky 		return (EFAULT);
828fa0f6e62SHans Petter Selasky 
829fa0f6e62SHans Petter Selasky 	if (offset >= CUSE_BUFFER_MAX)
830fa0f6e62SHans Petter Selasky 		return (EFAULT);
831fa0f6e62SHans Petter Selasky 
832fa0f6e62SHans Petter Selasky 	if ((offset + pchk->length) > CUSE_BUFFER_MAX)
833fa0f6e62SHans Petter Selasky 		return (EFAULT);
834fa0f6e62SHans Petter Selasky 
835fa0f6e62SHans Petter Selasky 	p_proc = pccmd->proc_curr;
836fa0f6e62SHans Petter Selasky 	if (p_proc == NULL)
837fa0f6e62SHans Petter Selasky 		return (ENXIO);
838fa0f6e62SHans Petter Selasky 
839fa0f6e62SHans Petter Selasky 	if (pccmd->proc_refs < 0)
840fa0f6e62SHans Petter Selasky 		return (ENOMEM);
841fa0f6e62SHans Petter Selasky 
842fa0f6e62SHans Petter Selasky 	pccmd->proc_refs++;
843fa0f6e62SHans Petter Selasky 
84498029019SHans Petter Selasky 	cuse_server_unlock(pcs);
845fa0f6e62SHans Petter Selasky 
846fa0f6e62SHans Petter Selasky 	if (isread == 0) {
847fa0f6e62SHans Petter Selasky 		error = copyin(
848fa0f6e62SHans Petter Selasky 		    (void *)pchk->local_ptr,
849fa0f6e62SHans Petter Selasky 		    pccmd->client->ioctl_buffer + offset,
850fa0f6e62SHans Petter Selasky 		    pchk->length);
851fa0f6e62SHans Petter Selasky 	} else {
852fa0f6e62SHans Petter Selasky 		error = copyout(
853fa0f6e62SHans Petter Selasky 		    pccmd->client->ioctl_buffer + offset,
854fa0f6e62SHans Petter Selasky 		    (void *)pchk->local_ptr,
855fa0f6e62SHans Petter Selasky 		    pchk->length);
856fa0f6e62SHans Petter Selasky 	}
857fa0f6e62SHans Petter Selasky 
85898029019SHans Petter Selasky 	cuse_server_lock(pcs);
859fa0f6e62SHans Petter Selasky 
860fa0f6e62SHans Petter Selasky 	pccmd->proc_refs--;
861fa0f6e62SHans Petter Selasky 
862fa0f6e62SHans Petter Selasky 	if (pccmd->proc_curr == NULL)
863fa0f6e62SHans Petter Selasky 		cv_signal(&pccmd->cv);
864fa0f6e62SHans Petter Selasky 
865fa0f6e62SHans Petter Selasky 	return (error);
866fa0f6e62SHans Petter Selasky }
867fa0f6e62SHans Petter Selasky 
868fa0f6e62SHans Petter Selasky static int
869fa0f6e62SHans Petter Selasky cuse_proc2proc_copy(struct proc *proc_s, vm_offset_t data_s,
870fa0f6e62SHans Petter Selasky     struct proc *proc_d, vm_offset_t data_d, size_t len)
871fa0f6e62SHans Petter Selasky {
872fa0f6e62SHans Petter Selasky 	struct thread *td;
873fa0f6e62SHans Petter Selasky 	struct proc *proc_cur;
874fa0f6e62SHans Petter Selasky 	int error;
875fa0f6e62SHans Petter Selasky 
876fa0f6e62SHans Petter Selasky 	td = curthread;
877fa0f6e62SHans Petter Selasky 	proc_cur = td->td_proc;
878fa0f6e62SHans Petter Selasky 
879fa0f6e62SHans Petter Selasky 	if (proc_cur == proc_d) {
880fa0f6e62SHans Petter Selasky 		struct iovec iov = {
881fa0f6e62SHans Petter Selasky 			.iov_base = (caddr_t)data_d,
882fa0f6e62SHans Petter Selasky 			.iov_len = len,
883fa0f6e62SHans Petter Selasky 		};
884fa0f6e62SHans Petter Selasky 		struct uio uio = {
885fa0f6e62SHans Petter Selasky 			.uio_iov = &iov,
886fa0f6e62SHans Petter Selasky 			.uio_iovcnt = 1,
887fa0f6e62SHans Petter Selasky 			.uio_offset = (off_t)data_s,
888fa0f6e62SHans Petter Selasky 			.uio_resid = len,
889fa0f6e62SHans Petter Selasky 			.uio_segflg = UIO_USERSPACE,
890fa0f6e62SHans Petter Selasky 			.uio_rw = UIO_READ,
891fa0f6e62SHans Petter Selasky 			.uio_td = td,
892fa0f6e62SHans Petter Selasky 		};
893fa0f6e62SHans Petter Selasky 
8945928e3b9SHans Petter Selasky 		PHOLD(proc_s);
895fa0f6e62SHans Petter Selasky 		error = proc_rwmem(proc_s, &uio);
8965928e3b9SHans Petter Selasky 		PRELE(proc_s);
897fa0f6e62SHans Petter Selasky 
898fa0f6e62SHans Petter Selasky 	} else if (proc_cur == proc_s) {
899fa0f6e62SHans Petter Selasky 		struct iovec iov = {
900fa0f6e62SHans Petter Selasky 			.iov_base = (caddr_t)data_s,
901fa0f6e62SHans Petter Selasky 			.iov_len = len,
902fa0f6e62SHans Petter Selasky 		};
903fa0f6e62SHans Petter Selasky 		struct uio uio = {
904fa0f6e62SHans Petter Selasky 			.uio_iov = &iov,
905fa0f6e62SHans Petter Selasky 			.uio_iovcnt = 1,
906fa0f6e62SHans Petter Selasky 			.uio_offset = (off_t)data_d,
907fa0f6e62SHans Petter Selasky 			.uio_resid = len,
908fa0f6e62SHans Petter Selasky 			.uio_segflg = UIO_USERSPACE,
909fa0f6e62SHans Petter Selasky 			.uio_rw = UIO_WRITE,
910fa0f6e62SHans Petter Selasky 			.uio_td = td,
911fa0f6e62SHans Petter Selasky 		};
912fa0f6e62SHans Petter Selasky 
9135928e3b9SHans Petter Selasky 		PHOLD(proc_d);
914fa0f6e62SHans Petter Selasky 		error = proc_rwmem(proc_d, &uio);
9155928e3b9SHans Petter Selasky 		PRELE(proc_d);
916fa0f6e62SHans Petter Selasky 	} else {
917fa0f6e62SHans Petter Selasky 		error = EINVAL;
918fa0f6e62SHans Petter Selasky 	}
919fa0f6e62SHans Petter Selasky 	return (error);
920fa0f6e62SHans Petter Selasky }
921fa0f6e62SHans Petter Selasky 
922fa0f6e62SHans Petter Selasky static int
92398029019SHans Petter Selasky cuse_server_data_copy_locked(struct cuse_server *pcs,
92498029019SHans Petter Selasky     struct cuse_client_command *pccmd,
925fa0f6e62SHans Petter Selasky     struct cuse_data_chunk *pchk, int isread)
926fa0f6e62SHans Petter Selasky {
927fa0f6e62SHans Petter Selasky 	struct proc *p_proc;
928fa0f6e62SHans Petter Selasky 	int error;
929fa0f6e62SHans Petter Selasky 
930fa0f6e62SHans Petter Selasky 	p_proc = pccmd->proc_curr;
931fa0f6e62SHans Petter Selasky 	if (p_proc == NULL)
932fa0f6e62SHans Petter Selasky 		return (ENXIO);
933fa0f6e62SHans Petter Selasky 
934fa0f6e62SHans Petter Selasky 	if (pccmd->proc_refs < 0)
935fa0f6e62SHans Petter Selasky 		return (ENOMEM);
936fa0f6e62SHans Petter Selasky 
937fa0f6e62SHans Petter Selasky 	pccmd->proc_refs++;
938fa0f6e62SHans Petter Selasky 
93998029019SHans Petter Selasky 	cuse_server_unlock(pcs);
940fa0f6e62SHans Petter Selasky 
941fa0f6e62SHans Petter Selasky 	if (isread == 0) {
942fa0f6e62SHans Petter Selasky 		error = cuse_proc2proc_copy(
943fa0f6e62SHans Petter Selasky 		    curthread->td_proc, pchk->local_ptr,
944fa0f6e62SHans Petter Selasky 		    p_proc, pchk->peer_ptr,
945fa0f6e62SHans Petter Selasky 		    pchk->length);
946fa0f6e62SHans Petter Selasky 	} else {
947fa0f6e62SHans Petter Selasky 		error = cuse_proc2proc_copy(
948fa0f6e62SHans Petter Selasky 		    p_proc, pchk->peer_ptr,
949fa0f6e62SHans Petter Selasky 		    curthread->td_proc, pchk->local_ptr,
950fa0f6e62SHans Petter Selasky 		    pchk->length);
951fa0f6e62SHans Petter Selasky 	}
952fa0f6e62SHans Petter Selasky 
95398029019SHans Petter Selasky 	cuse_server_lock(pcs);
954fa0f6e62SHans Petter Selasky 
955fa0f6e62SHans Petter Selasky 	pccmd->proc_refs--;
956fa0f6e62SHans Petter Selasky 
957fa0f6e62SHans Petter Selasky 	if (pccmd->proc_curr == NULL)
958fa0f6e62SHans Petter Selasky 		cv_signal(&pccmd->cv);
959fa0f6e62SHans Petter Selasky 
960fa0f6e62SHans Petter Selasky 	return (error);
961fa0f6e62SHans Petter Selasky }
962fa0f6e62SHans Petter Selasky 
963fa0f6e62SHans Petter Selasky static int
964fa0f6e62SHans Petter Selasky cuse_alloc_unit_by_id_locked(struct cuse_server *pcs, int id)
965fa0f6e62SHans Petter Selasky {
966fa0f6e62SHans Petter Selasky 	int n;
967fa0f6e62SHans Petter Selasky 	int x = 0;
968fa0f6e62SHans Petter Selasky 	int match;
969fa0f6e62SHans Petter Selasky 
970fa0f6e62SHans Petter Selasky 	do {
971fa0f6e62SHans Petter Selasky 		for (match = n = 0; n != CUSE_DEVICES_MAX; n++) {
972fa0f6e62SHans Petter Selasky 			if (cuse_alloc_unit[n] != NULL) {
973fa0f6e62SHans Petter Selasky 				if ((cuse_alloc_unit_id[n] ^ id) & CUSE_ID_MASK)
974fa0f6e62SHans Petter Selasky 					continue;
975fa0f6e62SHans Petter Selasky 				if ((cuse_alloc_unit_id[n] & ~CUSE_ID_MASK) == x) {
976fa0f6e62SHans Petter Selasky 					x++;
977fa0f6e62SHans Petter Selasky 					match = 1;
978fa0f6e62SHans Petter Selasky 				}
979fa0f6e62SHans Petter Selasky 			}
980fa0f6e62SHans Petter Selasky 		}
981fa0f6e62SHans Petter Selasky 	} while (match);
982fa0f6e62SHans Petter Selasky 
983fa0f6e62SHans Petter Selasky 	if (x < 256) {
984fa0f6e62SHans Petter Selasky 		for (n = 0; n != CUSE_DEVICES_MAX; n++) {
985fa0f6e62SHans Petter Selasky 			if (cuse_alloc_unit[n] == NULL) {
986fa0f6e62SHans Petter Selasky 				cuse_alloc_unit[n] = pcs;
987fa0f6e62SHans Petter Selasky 				cuse_alloc_unit_id[n] = id | x;
988fa0f6e62SHans Petter Selasky 				return (x);
989fa0f6e62SHans Petter Selasky 			}
990fa0f6e62SHans Petter Selasky 		}
991fa0f6e62SHans Petter Selasky 	}
992fa0f6e62SHans Petter Selasky 	return (-1);
993fa0f6e62SHans Petter Selasky }
994fa0f6e62SHans Petter Selasky 
995fa0f6e62SHans Petter Selasky static void
996fa0f6e62SHans Petter Selasky cuse_server_wakeup_locked(struct cuse_server *pcs)
997fa0f6e62SHans Petter Selasky {
998fa0f6e62SHans Petter Selasky 	selwakeup(&pcs->selinfo);
999fa0f6e62SHans Petter Selasky 	KNOTE_LOCKED(&pcs->selinfo.si_note, 0);
1000fa0f6e62SHans Petter Selasky }
1001fa0f6e62SHans Petter Selasky 
100295700043SHans Petter Selasky static void
100395700043SHans Petter Selasky cuse_server_wakeup_all_client_locked(struct cuse_server *pcs)
100495700043SHans Petter Selasky {
100595700043SHans Petter Selasky 	struct cuse_client *pcc;
100695700043SHans Petter Selasky 
100795700043SHans Petter Selasky 	TAILQ_FOREACH(pcc, &pcs->hcli, entry) {
100895700043SHans Petter Selasky 		pcc->cflags |= (CUSE_CLI_KNOTE_NEED_READ |
100995700043SHans Petter Selasky 		    CUSE_CLI_KNOTE_NEED_WRITE);
101095700043SHans Petter Selasky 	}
101195700043SHans Petter Selasky 	cuse_server_wakeup_locked(pcs);
101295700043SHans Petter Selasky }
101395700043SHans Petter Selasky 
1014fa0f6e62SHans Petter Selasky static int
1015fa0f6e62SHans Petter Selasky cuse_free_unit_by_id_locked(struct cuse_server *pcs, int id)
1016fa0f6e62SHans Petter Selasky {
1017fa0f6e62SHans Petter Selasky 	int n;
1018fa0f6e62SHans Petter Selasky 	int found = 0;
1019fa0f6e62SHans Petter Selasky 
1020fa0f6e62SHans Petter Selasky 	for (n = 0; n != CUSE_DEVICES_MAX; n++) {
1021fa0f6e62SHans Petter Selasky 		if (cuse_alloc_unit[n] == pcs) {
1022fa0f6e62SHans Petter Selasky 			if (cuse_alloc_unit_id[n] == id || id == -1) {
1023fa0f6e62SHans Petter Selasky 				cuse_alloc_unit[n] = NULL;
1024fa0f6e62SHans Petter Selasky 				cuse_alloc_unit_id[n] = 0;
1025fa0f6e62SHans Petter Selasky 				found = 1;
1026fa0f6e62SHans Petter Selasky 			}
1027fa0f6e62SHans Petter Selasky 		}
1028fa0f6e62SHans Petter Selasky 	}
1029fa0f6e62SHans Petter Selasky 
1030fa0f6e62SHans Petter Selasky 	return (found ? 0 : EINVAL);
1031fa0f6e62SHans Petter Selasky }
1032fa0f6e62SHans Petter Selasky 
1033fa0f6e62SHans Petter Selasky static int
1034fa0f6e62SHans Petter Selasky cuse_server_ioctl(struct cdev *dev, unsigned long cmd,
1035fa0f6e62SHans Petter Selasky     caddr_t data, int fflag, struct thread *td)
1036fa0f6e62SHans Petter Selasky {
1037fa0f6e62SHans Petter Selasky 	struct cuse_server *pcs;
1038fa0f6e62SHans Petter Selasky 	int error;
1039fa0f6e62SHans Petter Selasky 
1040fa0f6e62SHans Petter Selasky 	error = cuse_server_get(&pcs);
1041fa0f6e62SHans Petter Selasky 	if (error != 0)
1042fa0f6e62SHans Petter Selasky 		return (error);
1043fa0f6e62SHans Petter Selasky 
1044fa0f6e62SHans Petter Selasky 	switch (cmd) {
1045fa0f6e62SHans Petter Selasky 		struct cuse_client_command *pccmd;
1046fa0f6e62SHans Petter Selasky 		struct cuse_client *pcc;
1047fa0f6e62SHans Petter Selasky 		struct cuse_command *pcmd;
1048fa0f6e62SHans Petter Selasky 		struct cuse_alloc_info *pai;
1049fa0f6e62SHans Petter Selasky 		struct cuse_create_dev *pcd;
1050fa0f6e62SHans Petter Selasky 		struct cuse_server_dev *pcsd;
1051fa0f6e62SHans Petter Selasky 		struct cuse_data_chunk *pchk;
1052fa0f6e62SHans Petter Selasky 		int n;
1053fa0f6e62SHans Petter Selasky 
1054fa0f6e62SHans Petter Selasky 	case CUSE_IOCTL_GET_COMMAND:
1055fa0f6e62SHans Petter Selasky 		pcmd = (void *)data;
1056fa0f6e62SHans Petter Selasky 
105798029019SHans Petter Selasky 		cuse_server_lock(pcs);
1058fa0f6e62SHans Petter Selasky 
1059fa0f6e62SHans Petter Selasky 		while ((pccmd = TAILQ_FIRST(&pcs->head)) == NULL) {
106098029019SHans Petter Selasky 			error = cv_wait_sig(&pcs->cv, &pcs->mtx);
1061fa0f6e62SHans Petter Selasky 
1062fa0f6e62SHans Petter Selasky 			if (pcs->is_closing)
1063fa0f6e62SHans Petter Selasky 				error = ENXIO;
1064fa0f6e62SHans Petter Selasky 
1065fa0f6e62SHans Petter Selasky 			if (error) {
106698029019SHans Petter Selasky 				cuse_server_unlock(pcs);
1067fa0f6e62SHans Petter Selasky 				return (error);
1068fa0f6e62SHans Petter Selasky 			}
1069fa0f6e62SHans Petter Selasky 		}
1070fa0f6e62SHans Petter Selasky 
1071fa0f6e62SHans Petter Selasky 		TAILQ_REMOVE(&pcs->head, pccmd, entry);
1072fa0f6e62SHans Petter Selasky 		pccmd->entry.tqe_prev = NULL;
1073fa0f6e62SHans Petter Selasky 
1074fa0f6e62SHans Petter Selasky 		pccmd->entered = curthread;
1075fa0f6e62SHans Petter Selasky 
1076fa0f6e62SHans Petter Selasky 		*pcmd = pccmd->sub;
1077fa0f6e62SHans Petter Selasky 
107898029019SHans Petter Selasky 		cuse_server_unlock(pcs);
1079fa0f6e62SHans Petter Selasky 
1080fa0f6e62SHans Petter Selasky 		break;
1081fa0f6e62SHans Petter Selasky 
1082fa0f6e62SHans Petter Selasky 	case CUSE_IOCTL_SYNC_COMMAND:
1083fa0f6e62SHans Petter Selasky 
108498029019SHans Petter Selasky 		cuse_server_lock(pcs);
1085fa0f6e62SHans Petter Selasky 		while ((pccmd = cuse_server_find_command(pcs, curthread)) != NULL) {
1086fa0f6e62SHans Petter Selasky 			/* send sync command */
1087fa0f6e62SHans Petter Selasky 			pccmd->entered = NULL;
1088fa0f6e62SHans Petter Selasky 			pccmd->error = *(int *)data;
1089fa0f6e62SHans Petter Selasky 			pccmd->command = CUSE_CMD_SYNC;
1090fa0f6e62SHans Petter Selasky 
1091fa0f6e62SHans Petter Selasky 			/* signal peer, if any */
1092fa0f6e62SHans Petter Selasky 			cv_signal(&pccmd->cv);
1093fa0f6e62SHans Petter Selasky 		}
109498029019SHans Petter Selasky 		cuse_server_unlock(pcs);
1095fa0f6e62SHans Petter Selasky 
1096fa0f6e62SHans Petter Selasky 		break;
1097fa0f6e62SHans Petter Selasky 
1098fa0f6e62SHans Petter Selasky 	case CUSE_IOCTL_ALLOC_UNIT:
1099fa0f6e62SHans Petter Selasky 
110098029019SHans Petter Selasky 		cuse_server_lock(pcs);
1101fa0f6e62SHans Petter Selasky 		n = cuse_alloc_unit_by_id_locked(pcs,
1102fa0f6e62SHans Petter Selasky 		    CUSE_ID_DEFAULT(0));
110398029019SHans Petter Selasky 		cuse_server_unlock(pcs);
1104fa0f6e62SHans Petter Selasky 
1105fa0f6e62SHans Petter Selasky 		if (n < 0)
1106fa0f6e62SHans Petter Selasky 			error = ENOMEM;
1107fa0f6e62SHans Petter Selasky 		else
1108fa0f6e62SHans Petter Selasky 			*(int *)data = n;
1109fa0f6e62SHans Petter Selasky 		break;
1110fa0f6e62SHans Petter Selasky 
1111fa0f6e62SHans Petter Selasky 	case CUSE_IOCTL_ALLOC_UNIT_BY_ID:
1112fa0f6e62SHans Petter Selasky 
1113fa0f6e62SHans Petter Selasky 		n = *(int *)data;
1114fa0f6e62SHans Petter Selasky 
1115fa0f6e62SHans Petter Selasky 		n = (n & CUSE_ID_MASK);
1116fa0f6e62SHans Petter Selasky 
111798029019SHans Petter Selasky 		cuse_server_lock(pcs);
1118fa0f6e62SHans Petter Selasky 		n = cuse_alloc_unit_by_id_locked(pcs, n);
111998029019SHans Petter Selasky 		cuse_server_unlock(pcs);
1120fa0f6e62SHans Petter Selasky 
1121fa0f6e62SHans Petter Selasky 		if (n < 0)
1122fa0f6e62SHans Petter Selasky 			error = ENOMEM;
1123fa0f6e62SHans Petter Selasky 		else
1124fa0f6e62SHans Petter Selasky 			*(int *)data = n;
1125fa0f6e62SHans Petter Selasky 		break;
1126fa0f6e62SHans Petter Selasky 
1127fa0f6e62SHans Petter Selasky 	case CUSE_IOCTL_FREE_UNIT:
1128fa0f6e62SHans Petter Selasky 
1129fa0f6e62SHans Petter Selasky 		n = *(int *)data;
1130fa0f6e62SHans Petter Selasky 
1131fa0f6e62SHans Petter Selasky 		n = CUSE_ID_DEFAULT(n);
1132fa0f6e62SHans Petter Selasky 
113398029019SHans Petter Selasky 		cuse_server_lock(pcs);
1134fa0f6e62SHans Petter Selasky 		error = cuse_free_unit_by_id_locked(pcs, n);
113598029019SHans Petter Selasky 		cuse_server_unlock(pcs);
1136fa0f6e62SHans Petter Selasky 		break;
1137fa0f6e62SHans Petter Selasky 
1138fa0f6e62SHans Petter Selasky 	case CUSE_IOCTL_FREE_UNIT_BY_ID:
1139fa0f6e62SHans Petter Selasky 
1140fa0f6e62SHans Petter Selasky 		n = *(int *)data;
1141fa0f6e62SHans Petter Selasky 
114298029019SHans Petter Selasky 		cuse_server_lock(pcs);
1143fa0f6e62SHans Petter Selasky 		error = cuse_free_unit_by_id_locked(pcs, n);
114498029019SHans Petter Selasky 		cuse_server_unlock(pcs);
1145fa0f6e62SHans Petter Selasky 		break;
1146fa0f6e62SHans Petter Selasky 
1147fa0f6e62SHans Petter Selasky 	case CUSE_IOCTL_ALLOC_MEMORY:
1148fa0f6e62SHans Petter Selasky 
1149fa0f6e62SHans Petter Selasky 		pai = (void *)data;
1150fa0f6e62SHans Petter Selasky 
1151fa0f6e62SHans Petter Selasky 		if (pai->alloc_nr >= CUSE_ALLOC_UNIT_MAX) {
1152fa0f6e62SHans Petter Selasky 			error = ENOMEM;
1153fa0f6e62SHans Petter Selasky 			break;
1154fa0f6e62SHans Petter Selasky 		}
115556c49c63SHans Petter Selasky 		if (pai->page_count >= CUSE_ALLOC_PAGES_MAX) {
1156fa0f6e62SHans Petter Selasky 			error = ENOMEM;
1157fa0f6e62SHans Petter Selasky 			break;
1158fa0f6e62SHans Petter Selasky 		}
1159fa0f6e62SHans Petter Selasky 		error = cuse_server_alloc_memory(pcs,
116056c49c63SHans Petter Selasky 		    pai->alloc_nr, pai->page_count);
1161fa0f6e62SHans Petter Selasky 		break;
1162fa0f6e62SHans Petter Selasky 
1163fa0f6e62SHans Petter Selasky 	case CUSE_IOCTL_FREE_MEMORY:
1164fa0f6e62SHans Petter Selasky 		pai = (void *)data;
1165fa0f6e62SHans Petter Selasky 
1166fa0f6e62SHans Petter Selasky 		if (pai->alloc_nr >= CUSE_ALLOC_UNIT_MAX) {
1167fa0f6e62SHans Petter Selasky 			error = ENOMEM;
1168fa0f6e62SHans Petter Selasky 			break;
1169fa0f6e62SHans Petter Selasky 		}
117056c49c63SHans Petter Selasky 		error = cuse_server_free_memory(pcs, pai->alloc_nr);
1171fa0f6e62SHans Petter Selasky 		break;
1172fa0f6e62SHans Petter Selasky 
1173fa0f6e62SHans Petter Selasky 	case CUSE_IOCTL_GET_SIG:
1174fa0f6e62SHans Petter Selasky 
117598029019SHans Petter Selasky 		cuse_server_lock(pcs);
1176fa0f6e62SHans Petter Selasky 		pccmd = cuse_server_find_command(pcs, curthread);
1177fa0f6e62SHans Petter Selasky 
1178fa0f6e62SHans Petter Selasky 		if (pccmd != NULL) {
1179fa0f6e62SHans Petter Selasky 			n = pccmd->got_signal;
1180fa0f6e62SHans Petter Selasky 			pccmd->got_signal = 0;
1181fa0f6e62SHans Petter Selasky 		} else {
1182fa0f6e62SHans Petter Selasky 			n = 0;
1183fa0f6e62SHans Petter Selasky 		}
118498029019SHans Petter Selasky 		cuse_server_unlock(pcs);
1185fa0f6e62SHans Petter Selasky 
1186fa0f6e62SHans Petter Selasky 		*(int *)data = n;
1187fa0f6e62SHans Petter Selasky 
1188fa0f6e62SHans Petter Selasky 		break;
1189fa0f6e62SHans Petter Selasky 
1190fa0f6e62SHans Petter Selasky 	case CUSE_IOCTL_SET_PFH:
1191fa0f6e62SHans Petter Selasky 
119298029019SHans Petter Selasky 		cuse_server_lock(pcs);
1193fa0f6e62SHans Petter Selasky 		pccmd = cuse_server_find_command(pcs, curthread);
1194fa0f6e62SHans Petter Selasky 
1195fa0f6e62SHans Petter Selasky 		if (pccmd != NULL) {
1196fa0f6e62SHans Petter Selasky 			pcc = pccmd->client;
1197fa0f6e62SHans Petter Selasky 			for (n = 0; n != CUSE_CMD_MAX; n++) {
11989fb69c9dSHans Petter Selasky 				pcc->cmds[n].sub.per_file_handle = *(uintptr_t *)data;
1199fa0f6e62SHans Petter Selasky 			}
1200fa0f6e62SHans Petter Selasky 		} else {
1201fa0f6e62SHans Petter Selasky 			error = ENXIO;
1202fa0f6e62SHans Petter Selasky 		}
120398029019SHans Petter Selasky 		cuse_server_unlock(pcs);
1204fa0f6e62SHans Petter Selasky 		break;
1205fa0f6e62SHans Petter Selasky 
1206fa0f6e62SHans Petter Selasky 	case CUSE_IOCTL_CREATE_DEV:
1207fa0f6e62SHans Petter Selasky 
1208fa0f6e62SHans Petter Selasky 		error = priv_check(curthread, PRIV_DRIVER);
1209fa0f6e62SHans Petter Selasky 		if (error)
1210fa0f6e62SHans Petter Selasky 			break;
1211fa0f6e62SHans Petter Selasky 
1212fa0f6e62SHans Petter Selasky 		pcd = (void *)data;
1213fa0f6e62SHans Petter Selasky 
1214fa0f6e62SHans Petter Selasky 		/* filter input */
1215fa0f6e62SHans Petter Selasky 
1216fa0f6e62SHans Petter Selasky 		pcd->devname[sizeof(pcd->devname) - 1] = 0;
1217fa0f6e62SHans Petter Selasky 
1218fa0f6e62SHans Petter Selasky 		if (pcd->devname[0] == 0) {
1219fa0f6e62SHans Petter Selasky 			error = EINVAL;
1220fa0f6e62SHans Petter Selasky 			break;
1221fa0f6e62SHans Petter Selasky 		}
1222fa0f6e62SHans Petter Selasky 		cuse_str_filter(pcd->devname);
1223fa0f6e62SHans Petter Selasky 
1224fa0f6e62SHans Petter Selasky 		pcd->permissions &= 0777;
1225fa0f6e62SHans Petter Selasky 
1226fa0f6e62SHans Petter Selasky 		/* try to allocate a character device */
1227fa0f6e62SHans Petter Selasky 
1228fa0f6e62SHans Petter Selasky 		pcsd = malloc(sizeof(*pcsd), M_CUSE, M_WAITOK | M_ZERO);
1229fa0f6e62SHans Petter Selasky 
1230fa0f6e62SHans Petter Selasky 		pcsd->server = pcs;
1231fa0f6e62SHans Petter Selasky 
1232fa0f6e62SHans Petter Selasky 		pcsd->user_dev = pcd->dev;
1233fa0f6e62SHans Petter Selasky 
1234fa0f6e62SHans Petter Selasky 		pcsd->kern_dev = make_dev_credf(MAKEDEV_CHECKNAME,
1235fa0f6e62SHans Petter Selasky 		    &cuse_client_devsw, 0, NULL, pcd->user_id, pcd->group_id,
1236fa0f6e62SHans Petter Selasky 		    pcd->permissions, "%s", pcd->devname);
1237fa0f6e62SHans Petter Selasky 
1238fa0f6e62SHans Petter Selasky 		if (pcsd->kern_dev == NULL) {
1239fa0f6e62SHans Petter Selasky 			free(pcsd, M_CUSE);
1240fa0f6e62SHans Petter Selasky 			error = ENOMEM;
1241fa0f6e62SHans Petter Selasky 			break;
1242fa0f6e62SHans Petter Selasky 		}
1243fa0f6e62SHans Petter Selasky 		pcsd->kern_dev->si_drv1 = pcsd;
1244fa0f6e62SHans Petter Selasky 
124598029019SHans Petter Selasky 		cuse_server_lock(pcs);
1246fa0f6e62SHans Petter Selasky 		TAILQ_INSERT_TAIL(&pcs->hdev, pcsd, entry);
124798029019SHans Petter Selasky 		cuse_server_unlock(pcs);
1248fa0f6e62SHans Petter Selasky 
1249fa0f6e62SHans Petter Selasky 		break;
1250fa0f6e62SHans Petter Selasky 
1251fa0f6e62SHans Petter Selasky 	case CUSE_IOCTL_DESTROY_DEV:
1252fa0f6e62SHans Petter Selasky 
1253fa0f6e62SHans Petter Selasky 		error = priv_check(curthread, PRIV_DRIVER);
1254fa0f6e62SHans Petter Selasky 		if (error)
1255fa0f6e62SHans Petter Selasky 			break;
1256fa0f6e62SHans Petter Selasky 
125798029019SHans Petter Selasky 		cuse_server_lock(pcs);
1258fa0f6e62SHans Petter Selasky 
1259fa0f6e62SHans Petter Selasky 		error = EINVAL;
1260fa0f6e62SHans Petter Selasky 
1261fa0f6e62SHans Petter Selasky 		pcsd = TAILQ_FIRST(&pcs->hdev);
1262fa0f6e62SHans Petter Selasky 		while (pcsd != NULL) {
1263fa0f6e62SHans Petter Selasky 			if (pcsd->user_dev == *(struct cuse_dev **)data) {
1264fa0f6e62SHans Petter Selasky 				TAILQ_REMOVE(&pcs->hdev, pcsd, entry);
126598029019SHans Petter Selasky 				cuse_server_unlock(pcs);
1266fa0f6e62SHans Petter Selasky 				cuse_server_free_dev(pcsd);
126798029019SHans Petter Selasky 				cuse_server_lock(pcs);
1268fa0f6e62SHans Petter Selasky 				error = 0;
1269fa0f6e62SHans Petter Selasky 				pcsd = TAILQ_FIRST(&pcs->hdev);
1270fa0f6e62SHans Petter Selasky 			} else {
1271fa0f6e62SHans Petter Selasky 				pcsd = TAILQ_NEXT(pcsd, entry);
1272fa0f6e62SHans Petter Selasky 			}
1273fa0f6e62SHans Petter Selasky 		}
1274fa0f6e62SHans Petter Selasky 
127598029019SHans Petter Selasky 		cuse_server_unlock(pcs);
1276fa0f6e62SHans Petter Selasky 		break;
1277fa0f6e62SHans Petter Selasky 
1278fa0f6e62SHans Petter Selasky 	case CUSE_IOCTL_WRITE_DATA:
1279fa0f6e62SHans Petter Selasky 	case CUSE_IOCTL_READ_DATA:
1280fa0f6e62SHans Petter Selasky 
128198029019SHans Petter Selasky 		cuse_server_lock(pcs);
1282fa0f6e62SHans Petter Selasky 		pchk = (struct cuse_data_chunk *)data;
1283fa0f6e62SHans Petter Selasky 
1284fa0f6e62SHans Petter Selasky 		pccmd = cuse_server_find_command(pcs, curthread);
1285fa0f6e62SHans Petter Selasky 
1286fa0f6e62SHans Petter Selasky 		if (pccmd == NULL) {
1287fa0f6e62SHans Petter Selasky 			error = ENXIO;	/* invalid request */
1288fa0f6e62SHans Petter Selasky 		} else if (pchk->peer_ptr < CUSE_BUF_MIN_PTR) {
1289fa0f6e62SHans Petter Selasky 			error = EFAULT;	/* NULL pointer */
1290fa0f6e62SHans Petter Selasky 		} else if (pchk->peer_ptr < CUSE_BUF_MAX_PTR) {
129198029019SHans Petter Selasky 			error = cuse_server_ioctl_copy_locked(pcs, pccmd,
1292fa0f6e62SHans Petter Selasky 			    pchk, cmd == CUSE_IOCTL_READ_DATA);
1293fa0f6e62SHans Petter Selasky 		} else {
129498029019SHans Petter Selasky 			error = cuse_server_data_copy_locked(pcs, pccmd,
1295fa0f6e62SHans Petter Selasky 			    pchk, cmd == CUSE_IOCTL_READ_DATA);
1296fa0f6e62SHans Petter Selasky 		}
129798029019SHans Petter Selasky 		cuse_server_unlock(pcs);
1298fa0f6e62SHans Petter Selasky 		break;
1299fa0f6e62SHans Petter Selasky 
1300fa0f6e62SHans Petter Selasky 	case CUSE_IOCTL_SELWAKEUP:
130198029019SHans Petter Selasky 		cuse_server_lock(pcs);
1302fa0f6e62SHans Petter Selasky 		/*
1303fa0f6e62SHans Petter Selasky 		 * We don't know which direction caused the event.
1304fa0f6e62SHans Petter Selasky 		 * Wakeup both!
1305fa0f6e62SHans Petter Selasky 		 */
130695700043SHans Petter Selasky 		cuse_server_wakeup_all_client_locked(pcs);
130798029019SHans Petter Selasky 		cuse_server_unlock(pcs);
1308fa0f6e62SHans Petter Selasky 		break;
1309fa0f6e62SHans Petter Selasky 
1310fa0f6e62SHans Petter Selasky 	default:
1311fa0f6e62SHans Petter Selasky 		error = ENXIO;
1312fa0f6e62SHans Petter Selasky 		break;
1313fa0f6e62SHans Petter Selasky 	}
1314fa0f6e62SHans Petter Selasky 	return (error);
1315fa0f6e62SHans Petter Selasky }
1316fa0f6e62SHans Petter Selasky 
1317fa0f6e62SHans Petter Selasky static int
1318fa0f6e62SHans Petter Selasky cuse_server_poll(struct cdev *dev, int events, struct thread *td)
1319fa0f6e62SHans Petter Selasky {
1320fa0f6e62SHans Petter Selasky 	return (events & (POLLHUP | POLLPRI | POLLIN |
1321fa0f6e62SHans Petter Selasky 	    POLLRDNORM | POLLOUT | POLLWRNORM));
1322fa0f6e62SHans Petter Selasky }
1323fa0f6e62SHans Petter Selasky 
1324fa0f6e62SHans Petter Selasky static int
132556c49c63SHans Petter Selasky cuse_server_mmap_single(struct cdev *dev, vm_ooffset_t *offset,
132656c49c63SHans Petter Selasky     vm_size_t size, struct vm_object **object, int nprot)
1327fa0f6e62SHans Petter Selasky {
132856c49c63SHans Petter Selasky 	uint32_t page_nr = *offset / PAGE_SIZE;
1329fa0f6e62SHans Petter Selasky 	uint32_t alloc_nr = page_nr / CUSE_ALLOC_PAGES_MAX;
1330fa0f6e62SHans Petter Selasky 	struct cuse_memory *mem;
1331fa0f6e62SHans Petter Selasky 	struct cuse_server *pcs;
1332fa0f6e62SHans Petter Selasky 	int error;
1333fa0f6e62SHans Petter Selasky 
1334fa0f6e62SHans Petter Selasky 	error = cuse_server_get(&pcs);
1335fa0f6e62SHans Petter Selasky 	if (error != 0)
133656c49c63SHans Petter Selasky 		return (error);
1337fa0f6e62SHans Petter Selasky 
133898029019SHans Petter Selasky 	cuse_server_lock(pcs);
133956c49c63SHans Petter Selasky 	/* lookup memory structure */
134056c49c63SHans Petter Selasky 	TAILQ_FOREACH(mem, &pcs->hmem, entry) {
134156c49c63SHans Petter Selasky 		if (mem->alloc_nr == alloc_nr)
134256c49c63SHans Petter Selasky 			break;
1343fa0f6e62SHans Petter Selasky 	}
134456c49c63SHans Petter Selasky 	if (mem == NULL) {
134598029019SHans Petter Selasky 		cuse_server_unlock(pcs);
1346fa0f6e62SHans Petter Selasky 		return (ENOMEM);
1347fa0f6e62SHans Petter Selasky 	}
134856c49c63SHans Petter Selasky 	/* verify page offset */
1349fa0f6e62SHans Petter Selasky 	page_nr %= CUSE_ALLOC_PAGES_MAX;
1350fa0f6e62SHans Petter Selasky 	if (page_nr >= mem->page_count) {
135198029019SHans Petter Selasky 		cuse_server_unlock(pcs);
1352fa0f6e62SHans Petter Selasky 		return (ENXIO);
1353fa0f6e62SHans Petter Selasky 	}
135456c49c63SHans Petter Selasky 	/* verify mmap size */
135556c49c63SHans Petter Selasky 	if ((size % PAGE_SIZE) != 0 || (size < PAGE_SIZE) ||
135656c49c63SHans Petter Selasky 	    (size > ((mem->page_count - page_nr) * PAGE_SIZE))) {
135798029019SHans Petter Selasky 		cuse_server_unlock(pcs);
135856c49c63SHans Petter Selasky 		return (EINVAL);
135956c49c63SHans Petter Selasky 	}
136056c49c63SHans Petter Selasky 	vm_object_reference(mem->object);
136156c49c63SHans Petter Selasky 	*object = mem->object;
136298029019SHans Petter Selasky 	cuse_server_unlock(pcs);
1363fa0f6e62SHans Petter Selasky 
136456c49c63SHans Petter Selasky 	/* set new VM object offset to use */
136556c49c63SHans Petter Selasky 	*offset = page_nr * PAGE_SIZE;
1366fa0f6e62SHans Petter Selasky 
136756c49c63SHans Petter Selasky 	/* success */
1368fa0f6e62SHans Petter Selasky 	return (0);
1369fa0f6e62SHans Petter Selasky }
1370fa0f6e62SHans Petter Selasky 
1371fa0f6e62SHans Petter Selasky /*------------------------------------------------------------------------*
1372fa0f6e62SHans Petter Selasky  *	CUSE CLIENT PART
1373fa0f6e62SHans Petter Selasky  *------------------------------------------------------------------------*/
1374fa0f6e62SHans Petter Selasky static void
1375fa0f6e62SHans Petter Selasky cuse_client_free(void *arg)
1376fa0f6e62SHans Petter Selasky {
1377fa0f6e62SHans Petter Selasky 	struct cuse_client *pcc = arg;
1378fa0f6e62SHans Petter Selasky 	struct cuse_client_command *pccmd;
1379fa0f6e62SHans Petter Selasky 	struct cuse_server *pcs;
1380fa0f6e62SHans Petter Selasky 	int n;
1381fa0f6e62SHans Petter Selasky 
138298029019SHans Petter Selasky 	pcs = pcc->server;
138398029019SHans Petter Selasky 
138498029019SHans Petter Selasky 	cuse_server_lock(pcs);
1385fa0f6e62SHans Petter Selasky 	cuse_client_is_closing(pcc);
138698029019SHans Petter Selasky 	TAILQ_REMOVE(&pcs->hcli, pcc, entry);
138798029019SHans Petter Selasky 	cuse_server_unlock(pcs);
1388fa0f6e62SHans Petter Selasky 
1389fa0f6e62SHans Petter Selasky 	for (n = 0; n != CUSE_CMD_MAX; n++) {
1390fa0f6e62SHans Petter Selasky 		pccmd = &pcc->cmds[n];
1391fa0f6e62SHans Petter Selasky 
1392fa0f6e62SHans Petter Selasky 		sx_destroy(&pccmd->sx);
1393fa0f6e62SHans Petter Selasky 		cv_destroy(&pccmd->cv);
1394fa0f6e62SHans Petter Selasky 	}
1395fa0f6e62SHans Petter Selasky 
1396fa0f6e62SHans Petter Selasky 	free(pcc, M_CUSE);
1397fa0f6e62SHans Petter Selasky 
1398fa0f6e62SHans Petter Selasky 	/* drop reference on server */
139956c49c63SHans Petter Selasky 	cuse_server_unref(pcs);
1400fa0f6e62SHans Petter Selasky }
1401fa0f6e62SHans Petter Selasky 
1402fa0f6e62SHans Petter Selasky static int
1403fa0f6e62SHans Petter Selasky cuse_client_open(struct cdev *dev, int fflags, int devtype, struct thread *td)
1404fa0f6e62SHans Petter Selasky {
1405fa0f6e62SHans Petter Selasky 	struct cuse_client_command *pccmd;
1406fa0f6e62SHans Petter Selasky 	struct cuse_server_dev *pcsd;
1407fa0f6e62SHans Petter Selasky 	struct cuse_client *pcc;
1408fa0f6e62SHans Petter Selasky 	struct cuse_server *pcs;
1409fa0f6e62SHans Petter Selasky 	struct cuse_dev *pcd;
1410fa0f6e62SHans Petter Selasky 	int error;
1411fa0f6e62SHans Petter Selasky 	int n;
1412fa0f6e62SHans Petter Selasky 
1413fa0f6e62SHans Petter Selasky 	pcsd = dev->si_drv1;
1414fa0f6e62SHans Petter Selasky 	if (pcsd != NULL) {
1415fa0f6e62SHans Petter Selasky 		pcs = pcsd->server;
1416fa0f6e62SHans Petter Selasky 		pcd = pcsd->user_dev;
141798029019SHans Petter Selasky 
141898029019SHans Petter Selasky 		cuse_server_lock(pcs);
1419ac60e801SHans Petter Selasky 		/*
1420ac60e801SHans Petter Selasky 		 * Check that the refcount didn't wrap and that the
1421ac60e801SHans Petter Selasky 		 * same process is not both client and server. This
1422ac60e801SHans Petter Selasky 		 * can easily lead to deadlocks when destroying the
1423ac60e801SHans Petter Selasky 		 * CUSE character device nodes:
1424ac60e801SHans Petter Selasky 		 */
1425fa0f6e62SHans Petter Selasky 		pcs->refs++;
1426ac60e801SHans Petter Selasky 		if (pcs->refs < 0 || pcs->pid == curproc->p_pid) {
1427ac60e801SHans Petter Selasky 			/* overflow or wrong PID */
1428fa0f6e62SHans Petter Selasky 			pcs->refs--;
142998029019SHans Petter Selasky 			cuse_server_unlock(pcs);
1430fa0f6e62SHans Petter Selasky 			return (EINVAL);
143198029019SHans Petter Selasky 		}
143298029019SHans Petter Selasky 		cuse_server_unlock(pcs);
143398029019SHans Petter Selasky 	} else {
143498029019SHans Petter Selasky 		return (EINVAL);
143598029019SHans Petter Selasky 	}
1436fa0f6e62SHans Petter Selasky 
1437fa0f6e62SHans Petter Selasky 	pcc = malloc(sizeof(*pcc), M_CUSE, M_WAITOK | M_ZERO);
1438fa0f6e62SHans Petter Selasky 	if (devfs_set_cdevpriv(pcc, &cuse_client_free)) {
1439fa0f6e62SHans Petter Selasky 		printf("Cuse: Cannot set cdevpriv.\n");
1440fa0f6e62SHans Petter Selasky 		/* drop reference on server */
144156c49c63SHans Petter Selasky 		cuse_server_unref(pcs);
1442fa0f6e62SHans Petter Selasky 		free(pcc, M_CUSE);
1443fa0f6e62SHans Petter Selasky 		return (ENOMEM);
1444fa0f6e62SHans Petter Selasky 	}
1445fa0f6e62SHans Petter Selasky 	pcc->fflags = fflags;
1446fa0f6e62SHans Petter Selasky 	pcc->server_dev = pcsd;
1447fa0f6e62SHans Petter Selasky 	pcc->server = pcs;
1448fa0f6e62SHans Petter Selasky 
1449fa0f6e62SHans Petter Selasky 	for (n = 0; n != CUSE_CMD_MAX; n++) {
1450fa0f6e62SHans Petter Selasky 		pccmd = &pcc->cmds[n];
1451fa0f6e62SHans Petter Selasky 
1452fa0f6e62SHans Petter Selasky 		pccmd->sub.dev = pcd;
1453fa0f6e62SHans Petter Selasky 		pccmd->sub.command = n;
1454fa0f6e62SHans Petter Selasky 		pccmd->client = pcc;
1455fa0f6e62SHans Petter Selasky 
1456fa0f6e62SHans Petter Selasky 		sx_init(&pccmd->sx, "cuse-client-sx");
1457fa0f6e62SHans Petter Selasky 		cv_init(&pccmd->cv, "cuse-client-cv");
1458fa0f6e62SHans Petter Selasky 	}
1459fa0f6e62SHans Petter Selasky 
146098029019SHans Petter Selasky 	cuse_server_lock(pcs);
1461fa0f6e62SHans Petter Selasky 
1462fa0f6e62SHans Petter Selasky 	/* cuse_client_free() assumes that the client is listed somewhere! */
1463fa0f6e62SHans Petter Selasky 	/* always enqueue */
1464fa0f6e62SHans Petter Selasky 
1465fa0f6e62SHans Petter Selasky 	TAILQ_INSERT_TAIL(&pcs->hcli, pcc, entry);
1466fa0f6e62SHans Petter Selasky 
1467fa0f6e62SHans Petter Selasky 	/* check if server is closing */
1468fa0f6e62SHans Petter Selasky 	if ((pcs->is_closing != 0) || (dev->si_drv1 == NULL)) {
1469fa0f6e62SHans Petter Selasky 		error = EINVAL;
1470fa0f6e62SHans Petter Selasky 	} else {
1471fa0f6e62SHans Petter Selasky 		error = 0;
1472fa0f6e62SHans Petter Selasky 	}
147398029019SHans Petter Selasky 	cuse_server_unlock(pcs);
1474fa0f6e62SHans Petter Selasky 
1475fa0f6e62SHans Petter Selasky 	if (error) {
1476fa0f6e62SHans Petter Selasky 		devfs_clear_cdevpriv();	/* XXX bugfix */
1477fa0f6e62SHans Petter Selasky 		return (error);
1478fa0f6e62SHans Petter Selasky 	}
1479fa0f6e62SHans Petter Selasky 	pccmd = &pcc->cmds[CUSE_CMD_OPEN];
1480fa0f6e62SHans Petter Selasky 
1481fa0f6e62SHans Petter Selasky 	cuse_cmd_lock(pccmd);
1482fa0f6e62SHans Petter Selasky 
148398029019SHans Petter Selasky 	cuse_server_lock(pcs);
1484fa0f6e62SHans Petter Selasky 	cuse_client_send_command_locked(pccmd, 0, 0, pcc->fflags, 0);
1485fa0f6e62SHans Petter Selasky 
1486fa0f6e62SHans Petter Selasky 	error = cuse_client_receive_command_locked(pccmd, 0, 0);
148798029019SHans Petter Selasky 	cuse_server_unlock(pcs);
1488fa0f6e62SHans Petter Selasky 
1489fa0f6e62SHans Petter Selasky 	if (error < 0) {
1490fa0f6e62SHans Petter Selasky 		error = cuse_convert_error(error);
1491fa0f6e62SHans Petter Selasky 	} else {
1492fa0f6e62SHans Petter Selasky 		error = 0;
1493fa0f6e62SHans Petter Selasky 	}
1494fa0f6e62SHans Petter Selasky 
1495fa0f6e62SHans Petter Selasky 	cuse_cmd_unlock(pccmd);
1496fa0f6e62SHans Petter Selasky 
1497fa0f6e62SHans Petter Selasky 	if (error)
1498fa0f6e62SHans Petter Selasky 		devfs_clear_cdevpriv();	/* XXX bugfix */
1499fa0f6e62SHans Petter Selasky 
1500fa0f6e62SHans Petter Selasky 	return (error);
1501fa0f6e62SHans Petter Selasky }
1502fa0f6e62SHans Petter Selasky 
1503fa0f6e62SHans Petter Selasky static int
1504fa0f6e62SHans Petter Selasky cuse_client_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
1505fa0f6e62SHans Petter Selasky {
1506fa0f6e62SHans Petter Selasky 	struct cuse_client_command *pccmd;
1507fa0f6e62SHans Petter Selasky 	struct cuse_client *pcc;
150898029019SHans Petter Selasky 	struct cuse_server *pcs;
1509fa0f6e62SHans Petter Selasky 	int error;
1510fa0f6e62SHans Petter Selasky 
1511fa0f6e62SHans Petter Selasky 	error = cuse_client_get(&pcc);
1512fa0f6e62SHans Petter Selasky 	if (error != 0)
1513fa0f6e62SHans Petter Selasky 		return (0);
1514fa0f6e62SHans Petter Selasky 
1515fa0f6e62SHans Petter Selasky 	pccmd = &pcc->cmds[CUSE_CMD_CLOSE];
151698029019SHans Petter Selasky 	pcs = pcc->server;
1517fa0f6e62SHans Petter Selasky 
1518fa0f6e62SHans Petter Selasky 	cuse_cmd_lock(pccmd);
1519fa0f6e62SHans Petter Selasky 
152098029019SHans Petter Selasky 	cuse_server_lock(pcs);
1521fa0f6e62SHans Petter Selasky 	cuse_client_send_command_locked(pccmd, 0, 0, pcc->fflags, 0);
1522fa0f6e62SHans Petter Selasky 
1523fa0f6e62SHans Petter Selasky 	error = cuse_client_receive_command_locked(pccmd, 0, 0);
1524fa0f6e62SHans Petter Selasky 	cuse_cmd_unlock(pccmd);
1525fa0f6e62SHans Petter Selasky 
1526fa0f6e62SHans Petter Selasky 	cuse_client_is_closing(pcc);
152798029019SHans Petter Selasky 	cuse_server_unlock(pcs);
1528fa0f6e62SHans Petter Selasky 
1529fa0f6e62SHans Petter Selasky 	return (0);
1530fa0f6e62SHans Petter Selasky }
1531fa0f6e62SHans Petter Selasky 
1532fa0f6e62SHans Petter Selasky static void
1533fa0f6e62SHans Petter Selasky cuse_client_kqfilter_poll(struct cdev *dev, struct cuse_client *pcc)
1534fa0f6e62SHans Petter Selasky {
153598029019SHans Petter Selasky 	struct cuse_server *pcs = pcc->server;
1536fa0f6e62SHans Petter Selasky 	int temp;
1537fa0f6e62SHans Petter Selasky 
153898029019SHans Petter Selasky 	cuse_server_lock(pcs);
1539fa0f6e62SHans Petter Selasky 	temp = (pcc->cflags & (CUSE_CLI_KNOTE_HAS_READ |
1540fa0f6e62SHans Petter Selasky 	    CUSE_CLI_KNOTE_HAS_WRITE));
1541fa0f6e62SHans Petter Selasky 	pcc->cflags &= ~(CUSE_CLI_KNOTE_NEED_READ |
1542fa0f6e62SHans Petter Selasky 	    CUSE_CLI_KNOTE_NEED_WRITE);
154398029019SHans Petter Selasky 	cuse_server_unlock(pcs);
1544fa0f6e62SHans Petter Selasky 
1545fa0f6e62SHans Petter Selasky 	if (temp != 0) {
1546fa0f6e62SHans Petter Selasky 		/* get the latest polling state from the server */
1547fa0f6e62SHans Petter Selasky 		temp = cuse_client_poll(dev, POLLIN | POLLOUT, NULL);
1548fa0f6e62SHans Petter Selasky 
1549fa0f6e62SHans Petter Selasky 		if (temp & (POLLIN | POLLOUT)) {
155098029019SHans Petter Selasky 			cuse_server_lock(pcs);
1551fa0f6e62SHans Petter Selasky 			if (temp & POLLIN)
1552fa0f6e62SHans Petter Selasky 				pcc->cflags |= CUSE_CLI_KNOTE_NEED_READ;
1553fa0f6e62SHans Petter Selasky 			if (temp & POLLOUT)
1554fa0f6e62SHans Petter Selasky 				pcc->cflags |= CUSE_CLI_KNOTE_NEED_WRITE;
1555fa0f6e62SHans Petter Selasky 
1556fa0f6e62SHans Petter Selasky 			/* make sure the "knote" gets woken up */
1557fa0f6e62SHans Petter Selasky 			cuse_server_wakeup_locked(pcc->server);
155898029019SHans Petter Selasky 			cuse_server_unlock(pcs);
1559fa0f6e62SHans Petter Selasky 		}
1560fa0f6e62SHans Petter Selasky 	}
15619a83b097SHans Petter Selasky }
1562fa0f6e62SHans Petter Selasky 
1563fa0f6e62SHans Petter Selasky static int
1564fa0f6e62SHans Petter Selasky cuse_client_read(struct cdev *dev, struct uio *uio, int ioflag)
1565fa0f6e62SHans Petter Selasky {
1566fa0f6e62SHans Petter Selasky 	struct cuse_client_command *pccmd;
1567fa0f6e62SHans Petter Selasky 	struct cuse_client *pcc;
156898029019SHans Petter Selasky 	struct cuse_server *pcs;
1569fa0f6e62SHans Petter Selasky 	int error;
1570fa0f6e62SHans Petter Selasky 	int len;
1571fa0f6e62SHans Petter Selasky 
1572fa0f6e62SHans Petter Selasky 	error = cuse_client_get(&pcc);
1573fa0f6e62SHans Petter Selasky 	if (error != 0)
1574fa0f6e62SHans Petter Selasky 		return (error);
1575fa0f6e62SHans Petter Selasky 
1576fa0f6e62SHans Petter Selasky 	pccmd = &pcc->cmds[CUSE_CMD_READ];
157798029019SHans Petter Selasky 	pcs = pcc->server;
1578fa0f6e62SHans Petter Selasky 
1579fa0f6e62SHans Petter Selasky 	if (uio->uio_segflg != UIO_USERSPACE) {
1580fa0f6e62SHans Petter Selasky 		return (EINVAL);
1581fa0f6e62SHans Petter Selasky 	}
1582fa0f6e62SHans Petter Selasky 	uio->uio_segflg = UIO_NOCOPY;
1583fa0f6e62SHans Petter Selasky 
1584fa0f6e62SHans Petter Selasky 	cuse_cmd_lock(pccmd);
1585fa0f6e62SHans Petter Selasky 
1586fa0f6e62SHans Petter Selasky 	while (uio->uio_resid != 0) {
1587fa0f6e62SHans Petter Selasky 		if (uio->uio_iov->iov_len > CUSE_LENGTH_MAX) {
1588fa0f6e62SHans Petter Selasky 			error = ENOMEM;
1589fa0f6e62SHans Petter Selasky 			break;
1590fa0f6e62SHans Petter Selasky 		}
1591fa0f6e62SHans Petter Selasky 		len = uio->uio_iov->iov_len;
1592fa0f6e62SHans Petter Selasky 
159398029019SHans Petter Selasky 		cuse_server_lock(pcs);
1594fa0f6e62SHans Petter Selasky 		cuse_client_send_command_locked(pccmd,
1595beebd9aaSHans Petter Selasky 		    (uintptr_t)uio->uio_iov->iov_base,
1596fa0f6e62SHans Petter Selasky 		    (unsigned long)(unsigned int)len, pcc->fflags, ioflag);
1597fa0f6e62SHans Petter Selasky 
1598fa0f6e62SHans Petter Selasky 		error = cuse_client_receive_command_locked(pccmd, 0, 0);
159998029019SHans Petter Selasky 		cuse_server_unlock(pcs);
1600fa0f6e62SHans Petter Selasky 
1601fa0f6e62SHans Petter Selasky 		if (error < 0) {
1602fa0f6e62SHans Petter Selasky 			error = cuse_convert_error(error);
1603fa0f6e62SHans Petter Selasky 			break;
1604fa0f6e62SHans Petter Selasky 		} else if (error == len) {
1605fa0f6e62SHans Petter Selasky 			error = uiomove(NULL, error, uio);
1606fa0f6e62SHans Petter Selasky 			if (error)
1607fa0f6e62SHans Petter Selasky 				break;
1608fa0f6e62SHans Petter Selasky 		} else {
1609fa0f6e62SHans Petter Selasky 			error = uiomove(NULL, error, uio);
1610fa0f6e62SHans Petter Selasky 			break;
1611fa0f6e62SHans Petter Selasky 		}
1612fa0f6e62SHans Petter Selasky 	}
1613fa0f6e62SHans Petter Selasky 	cuse_cmd_unlock(pccmd);
1614fa0f6e62SHans Petter Selasky 
1615fa0f6e62SHans Petter Selasky 	uio->uio_segflg = UIO_USERSPACE;/* restore segment flag */
1616fa0f6e62SHans Petter Selasky 
1617fa0f6e62SHans Petter Selasky 	if (error == EWOULDBLOCK)
1618fa0f6e62SHans Petter Selasky 		cuse_client_kqfilter_poll(dev, pcc);
1619fa0f6e62SHans Petter Selasky 
1620fa0f6e62SHans Petter Selasky 	return (error);
1621fa0f6e62SHans Petter Selasky }
1622fa0f6e62SHans Petter Selasky 
1623fa0f6e62SHans Petter Selasky static int
1624fa0f6e62SHans Petter Selasky cuse_client_write(struct cdev *dev, struct uio *uio, int ioflag)
1625fa0f6e62SHans Petter Selasky {
1626fa0f6e62SHans Petter Selasky 	struct cuse_client_command *pccmd;
1627fa0f6e62SHans Petter Selasky 	struct cuse_client *pcc;
162898029019SHans Petter Selasky 	struct cuse_server *pcs;
1629fa0f6e62SHans Petter Selasky 	int error;
1630fa0f6e62SHans Petter Selasky 	int len;
1631fa0f6e62SHans Petter Selasky 
1632fa0f6e62SHans Petter Selasky 	error = cuse_client_get(&pcc);
1633fa0f6e62SHans Petter Selasky 	if (error != 0)
1634fa0f6e62SHans Petter Selasky 		return (error);
1635fa0f6e62SHans Petter Selasky 
1636fa0f6e62SHans Petter Selasky 	pccmd = &pcc->cmds[CUSE_CMD_WRITE];
163798029019SHans Petter Selasky 	pcs = pcc->server;
1638fa0f6e62SHans Petter Selasky 
1639fa0f6e62SHans Petter Selasky 	if (uio->uio_segflg != UIO_USERSPACE) {
1640fa0f6e62SHans Petter Selasky 		return (EINVAL);
1641fa0f6e62SHans Petter Selasky 	}
1642fa0f6e62SHans Petter Selasky 	uio->uio_segflg = UIO_NOCOPY;
1643fa0f6e62SHans Petter Selasky 
1644fa0f6e62SHans Petter Selasky 	cuse_cmd_lock(pccmd);
1645fa0f6e62SHans Petter Selasky 
1646fa0f6e62SHans Petter Selasky 	while (uio->uio_resid != 0) {
1647fa0f6e62SHans Petter Selasky 		if (uio->uio_iov->iov_len > CUSE_LENGTH_MAX) {
1648fa0f6e62SHans Petter Selasky 			error = ENOMEM;
1649fa0f6e62SHans Petter Selasky 			break;
1650fa0f6e62SHans Petter Selasky 		}
1651fa0f6e62SHans Petter Selasky 		len = uio->uio_iov->iov_len;
1652fa0f6e62SHans Petter Selasky 
165398029019SHans Petter Selasky 		cuse_server_lock(pcs);
1654fa0f6e62SHans Petter Selasky 		cuse_client_send_command_locked(pccmd,
1655beebd9aaSHans Petter Selasky 		    (uintptr_t)uio->uio_iov->iov_base,
1656fa0f6e62SHans Petter Selasky 		    (unsigned long)(unsigned int)len, pcc->fflags, ioflag);
1657fa0f6e62SHans Petter Selasky 
1658fa0f6e62SHans Petter Selasky 		error = cuse_client_receive_command_locked(pccmd, 0, 0);
165998029019SHans Petter Selasky 		cuse_server_unlock(pcs);
1660fa0f6e62SHans Petter Selasky 
1661fa0f6e62SHans Petter Selasky 		if (error < 0) {
1662fa0f6e62SHans Petter Selasky 			error = cuse_convert_error(error);
1663fa0f6e62SHans Petter Selasky 			break;
1664fa0f6e62SHans Petter Selasky 		} else if (error == len) {
1665fa0f6e62SHans Petter Selasky 			error = uiomove(NULL, error, uio);
1666fa0f6e62SHans Petter Selasky 			if (error)
1667fa0f6e62SHans Petter Selasky 				break;
1668fa0f6e62SHans Petter Selasky 		} else {
1669fa0f6e62SHans Petter Selasky 			error = uiomove(NULL, error, uio);
1670fa0f6e62SHans Petter Selasky 			break;
1671fa0f6e62SHans Petter Selasky 		}
1672fa0f6e62SHans Petter Selasky 	}
1673fa0f6e62SHans Petter Selasky 	cuse_cmd_unlock(pccmd);
1674fa0f6e62SHans Petter Selasky 
1675fa0f6e62SHans Petter Selasky 	uio->uio_segflg = UIO_USERSPACE;/* restore segment flag */
1676fa0f6e62SHans Petter Selasky 
1677fa0f6e62SHans Petter Selasky 	if (error == EWOULDBLOCK)
1678fa0f6e62SHans Petter Selasky 		cuse_client_kqfilter_poll(dev, pcc);
1679fa0f6e62SHans Petter Selasky 
1680fa0f6e62SHans Petter Selasky 	return (error);
1681fa0f6e62SHans Petter Selasky }
1682fa0f6e62SHans Petter Selasky 
1683fa0f6e62SHans Petter Selasky int
1684fa0f6e62SHans Petter Selasky cuse_client_ioctl(struct cdev *dev, unsigned long cmd,
1685fa0f6e62SHans Petter Selasky     caddr_t data, int fflag, struct thread *td)
1686fa0f6e62SHans Petter Selasky {
1687fa0f6e62SHans Petter Selasky 	struct cuse_client_command *pccmd;
1688fa0f6e62SHans Petter Selasky 	struct cuse_client *pcc;
168998029019SHans Petter Selasky 	struct cuse_server *pcs;
1690fa0f6e62SHans Petter Selasky 	int error;
1691fa0f6e62SHans Petter Selasky 	int len;
1692fa0f6e62SHans Petter Selasky 
1693fa0f6e62SHans Petter Selasky 	error = cuse_client_get(&pcc);
1694fa0f6e62SHans Petter Selasky 	if (error != 0)
1695fa0f6e62SHans Petter Selasky 		return (error);
1696fa0f6e62SHans Petter Selasky 
1697fa0f6e62SHans Petter Selasky 	len = IOCPARM_LEN(cmd);
1698fa0f6e62SHans Petter Selasky 	if (len > CUSE_BUFFER_MAX)
1699fa0f6e62SHans Petter Selasky 		return (ENOMEM);
1700fa0f6e62SHans Petter Selasky 
1701fa0f6e62SHans Petter Selasky 	pccmd = &pcc->cmds[CUSE_CMD_IOCTL];
170298029019SHans Petter Selasky 	pcs = pcc->server;
1703fa0f6e62SHans Petter Selasky 
1704fa0f6e62SHans Petter Selasky 	cuse_cmd_lock(pccmd);
1705fa0f6e62SHans Petter Selasky 
1706010638abSHans Petter Selasky 	if (cmd & (IOC_IN | IOC_VOID))
1707fa0f6e62SHans Petter Selasky 		memcpy(pcc->ioctl_buffer, data, len);
1708fa0f6e62SHans Petter Selasky 
1709fa0f6e62SHans Petter Selasky 	/*
1710fa0f6e62SHans Petter Selasky 	 * When the ioctl-length is zero drivers can pass information
1711fa0f6e62SHans Petter Selasky 	 * through the data pointer of the ioctl. Make sure this information
1712fa0f6e62SHans Petter Selasky 	 * is forwarded to the driver.
1713fa0f6e62SHans Petter Selasky 	 */
1714fa0f6e62SHans Petter Selasky 
171598029019SHans Petter Selasky 	cuse_server_lock(pcs);
1716fa0f6e62SHans Petter Selasky 	cuse_client_send_command_locked(pccmd,
1717fa0f6e62SHans Petter Selasky 	    (len == 0) ? *(long *)data : CUSE_BUF_MIN_PTR,
1718fa0f6e62SHans Petter Selasky 	    (unsigned long)cmd, pcc->fflags,
1719fa0f6e62SHans Petter Selasky 	    (fflag & O_NONBLOCK) ? IO_NDELAY : 0);
1720fa0f6e62SHans Petter Selasky 
1721fa0f6e62SHans Petter Selasky 	error = cuse_client_receive_command_locked(pccmd, data, len);
172298029019SHans Petter Selasky 	cuse_server_unlock(pcs);
1723fa0f6e62SHans Petter Selasky 
1724fa0f6e62SHans Petter Selasky 	if (error < 0) {
1725fa0f6e62SHans Petter Selasky 		error = cuse_convert_error(error);
1726fa0f6e62SHans Petter Selasky 	} else {
1727fa0f6e62SHans Petter Selasky 		error = 0;
1728fa0f6e62SHans Petter Selasky 	}
1729fa0f6e62SHans Petter Selasky 
1730fa0f6e62SHans Petter Selasky 	if (cmd & IOC_OUT)
1731fa0f6e62SHans Petter Selasky 		memcpy(data, pcc->ioctl_buffer, len);
1732fa0f6e62SHans Petter Selasky 
1733fa0f6e62SHans Petter Selasky 	cuse_cmd_unlock(pccmd);
1734fa0f6e62SHans Petter Selasky 
1735fa0f6e62SHans Petter Selasky 	if (error == EWOULDBLOCK)
1736fa0f6e62SHans Petter Selasky 		cuse_client_kqfilter_poll(dev, pcc);
1737fa0f6e62SHans Petter Selasky 
1738fa0f6e62SHans Petter Selasky 	return (error);
1739fa0f6e62SHans Petter Selasky }
1740fa0f6e62SHans Petter Selasky 
1741fa0f6e62SHans Petter Selasky static int
1742fa0f6e62SHans Petter Selasky cuse_client_poll(struct cdev *dev, int events, struct thread *td)
1743fa0f6e62SHans Petter Selasky {
1744fa0f6e62SHans Petter Selasky 	struct cuse_client_command *pccmd;
1745fa0f6e62SHans Petter Selasky 	struct cuse_client *pcc;
174698029019SHans Petter Selasky 	struct cuse_server *pcs;
1747fa0f6e62SHans Petter Selasky 	unsigned long temp;
1748fa0f6e62SHans Petter Selasky 	int error;
1749fa0f6e62SHans Petter Selasky 	int revents;
1750fa0f6e62SHans Petter Selasky 
1751fa0f6e62SHans Petter Selasky 	error = cuse_client_get(&pcc);
1752fa0f6e62SHans Petter Selasky 	if (error != 0)
175395700043SHans Petter Selasky 		goto pollnval;
1754fa0f6e62SHans Petter Selasky 
1755fa0f6e62SHans Petter Selasky 	temp = 0;
175698029019SHans Petter Selasky 	pcs = pcc->server;
1757fa0f6e62SHans Petter Selasky 
1758fa0f6e62SHans Petter Selasky 	if (events & (POLLPRI | POLLIN | POLLRDNORM))
1759fa0f6e62SHans Petter Selasky 		temp |= CUSE_POLL_READ;
1760fa0f6e62SHans Petter Selasky 
1761fa0f6e62SHans Petter Selasky 	if (events & (POLLOUT | POLLWRNORM))
1762fa0f6e62SHans Petter Selasky 		temp |= CUSE_POLL_WRITE;
1763fa0f6e62SHans Petter Selasky 
1764fa0f6e62SHans Petter Selasky 	if (events & POLLHUP)
1765fa0f6e62SHans Petter Selasky 		temp |= CUSE_POLL_ERROR;
1766fa0f6e62SHans Petter Selasky 
1767fa0f6e62SHans Petter Selasky 	pccmd = &pcc->cmds[CUSE_CMD_POLL];
1768fa0f6e62SHans Petter Selasky 
1769fa0f6e62SHans Petter Selasky 	cuse_cmd_lock(pccmd);
1770fa0f6e62SHans Petter Selasky 
1771fa0f6e62SHans Petter Selasky 	/* Need to selrecord() first to not loose any events. */
1772fa0f6e62SHans Petter Selasky 	if (temp != 0 && td != NULL)
177398029019SHans Petter Selasky 		selrecord(td, &pcs->selinfo);
1774fa0f6e62SHans Petter Selasky 
177598029019SHans Petter Selasky 	cuse_server_lock(pcs);
1776fa0f6e62SHans Petter Selasky 	cuse_client_send_command_locked(pccmd,
1777fa0f6e62SHans Petter Selasky 	    0, temp, pcc->fflags, IO_NDELAY);
1778fa0f6e62SHans Petter Selasky 
1779fa0f6e62SHans Petter Selasky 	error = cuse_client_receive_command_locked(pccmd, 0, 0);
178098029019SHans Petter Selasky 	cuse_server_unlock(pcs);
1781fa0f6e62SHans Petter Selasky 
178295700043SHans Petter Selasky 	cuse_cmd_unlock(pccmd);
178395700043SHans Petter Selasky 
1784fa0f6e62SHans Petter Selasky 	if (error < 0) {
178595700043SHans Petter Selasky 		goto pollnval;
1786fa0f6e62SHans Petter Selasky 	} else {
1787fa0f6e62SHans Petter Selasky 		revents = 0;
1788fa0f6e62SHans Petter Selasky 		if (error & CUSE_POLL_READ)
1789fa0f6e62SHans Petter Selasky 			revents |= (events & (POLLPRI | POLLIN | POLLRDNORM));
1790fa0f6e62SHans Petter Selasky 		if (error & CUSE_POLL_WRITE)
1791fa0f6e62SHans Petter Selasky 			revents |= (events & (POLLOUT | POLLWRNORM));
1792fa0f6e62SHans Petter Selasky 		if (error & CUSE_POLL_ERROR)
1793fa0f6e62SHans Petter Selasky 			revents |= (events & POLLHUP);
1794fa0f6e62SHans Petter Selasky 	}
1795fa0f6e62SHans Petter Selasky 	return (revents);
179695700043SHans Petter Selasky 
179795700043SHans Petter Selasky pollnval:
179895700043SHans Petter Selasky 	/* XXX many clients don't understand POLLNVAL */
179995700043SHans Petter Selasky 	return (events & (POLLHUP | POLLPRI | POLLIN |
180095700043SHans Petter Selasky 	    POLLRDNORM | POLLOUT | POLLWRNORM));
1801fa0f6e62SHans Petter Selasky }
1802fa0f6e62SHans Petter Selasky 
1803fa0f6e62SHans Petter Selasky static int
180456c49c63SHans Petter Selasky cuse_client_mmap_single(struct cdev *dev, vm_ooffset_t *offset,
180556c49c63SHans Petter Selasky     vm_size_t size, struct vm_object **object, int nprot)
1806fa0f6e62SHans Petter Selasky {
180756c49c63SHans Petter Selasky 	uint32_t page_nr = *offset / PAGE_SIZE;
1808fa0f6e62SHans Petter Selasky 	uint32_t alloc_nr = page_nr / CUSE_ALLOC_PAGES_MAX;
1809fa0f6e62SHans Petter Selasky 	struct cuse_memory *mem;
1810fa0f6e62SHans Petter Selasky 	struct cuse_client *pcc;
181198029019SHans Petter Selasky 	struct cuse_server *pcs;
1812fa0f6e62SHans Petter Selasky 	int error;
1813fa0f6e62SHans Petter Selasky 
1814fa0f6e62SHans Petter Selasky 	error = cuse_client_get(&pcc);
1815fa0f6e62SHans Petter Selasky 	if (error != 0)
181656c49c63SHans Petter Selasky 		return (error);
1817fa0f6e62SHans Petter Selasky 
181898029019SHans Petter Selasky 	pcs = pcc->server;
181998029019SHans Petter Selasky 
182098029019SHans Petter Selasky 	cuse_server_lock(pcs);
182156c49c63SHans Petter Selasky 	/* lookup memory structure */
182298029019SHans Petter Selasky 	TAILQ_FOREACH(mem, &pcs->hmem, entry) {
182356c49c63SHans Petter Selasky 		if (mem->alloc_nr == alloc_nr)
182456c49c63SHans Petter Selasky 			break;
1825fa0f6e62SHans Petter Selasky 	}
182656c49c63SHans Petter Selasky 	if (mem == NULL) {
182798029019SHans Petter Selasky 		cuse_server_unlock(pcs);
1828fa0f6e62SHans Petter Selasky 		return (ENOMEM);
1829fa0f6e62SHans Petter Selasky 	}
183056c49c63SHans Petter Selasky 	/* verify page offset */
1831fa0f6e62SHans Petter Selasky 	page_nr %= CUSE_ALLOC_PAGES_MAX;
1832fa0f6e62SHans Petter Selasky 	if (page_nr >= mem->page_count) {
183398029019SHans Petter Selasky 		cuse_server_unlock(pcs);
1834fa0f6e62SHans Petter Selasky 		return (ENXIO);
1835fa0f6e62SHans Petter Selasky 	}
183656c49c63SHans Petter Selasky 	/* verify mmap size */
183756c49c63SHans Petter Selasky 	if ((size % PAGE_SIZE) != 0 || (size < PAGE_SIZE) ||
183856c49c63SHans Petter Selasky 	    (size > ((mem->page_count - page_nr) * PAGE_SIZE))) {
183998029019SHans Petter Selasky 		cuse_server_unlock(pcs);
184056c49c63SHans Petter Selasky 		return (EINVAL);
184156c49c63SHans Petter Selasky 	}
184256c49c63SHans Petter Selasky 	vm_object_reference(mem->object);
184356c49c63SHans Petter Selasky 	*object = mem->object;
184498029019SHans Petter Selasky 	cuse_server_unlock(pcs);
1845fa0f6e62SHans Petter Selasky 
184656c49c63SHans Petter Selasky 	/* set new VM object offset to use */
184756c49c63SHans Petter Selasky 	*offset = page_nr * PAGE_SIZE;
1848fa0f6e62SHans Petter Selasky 
184956c49c63SHans Petter Selasky 	/* success */
1850fa0f6e62SHans Petter Selasky 	return (0);
1851fa0f6e62SHans Petter Selasky }
1852fa0f6e62SHans Petter Selasky 
1853fa0f6e62SHans Petter Selasky static void
1854fa0f6e62SHans Petter Selasky cuse_client_kqfilter_read_detach(struct knote *kn)
1855fa0f6e62SHans Petter Selasky {
1856fa0f6e62SHans Petter Selasky 	struct cuse_client *pcc;
185798029019SHans Petter Selasky 	struct cuse_server *pcs;
1858fa0f6e62SHans Petter Selasky 
1859fa0f6e62SHans Petter Selasky 	pcc = kn->kn_hook;
186098029019SHans Petter Selasky 	pcs = pcc->server;
186198029019SHans Petter Selasky 
186298029019SHans Petter Selasky 	cuse_server_lock(pcs);
186398029019SHans Petter Selasky 	knlist_remove(&pcs->selinfo.si_note, kn, 1);
186498029019SHans Petter Selasky 	cuse_server_unlock(pcs);
1865fa0f6e62SHans Petter Selasky }
1866fa0f6e62SHans Petter Selasky 
1867fa0f6e62SHans Petter Selasky static void
1868fa0f6e62SHans Petter Selasky cuse_client_kqfilter_write_detach(struct knote *kn)
1869fa0f6e62SHans Petter Selasky {
1870fa0f6e62SHans Petter Selasky 	struct cuse_client *pcc;
187198029019SHans Petter Selasky 	struct cuse_server *pcs;
1872fa0f6e62SHans Petter Selasky 
1873fa0f6e62SHans Petter Selasky 	pcc = kn->kn_hook;
187498029019SHans Petter Selasky 	pcs = pcc->server;
187598029019SHans Petter Selasky 
187698029019SHans Petter Selasky 	cuse_server_lock(pcs);
187798029019SHans Petter Selasky 	knlist_remove(&pcs->selinfo.si_note, kn, 1);
187898029019SHans Petter Selasky 	cuse_server_unlock(pcs);
1879fa0f6e62SHans Petter Selasky }
1880fa0f6e62SHans Petter Selasky 
1881fa0f6e62SHans Petter Selasky static int
1882fa0f6e62SHans Petter Selasky cuse_client_kqfilter_read_event(struct knote *kn, long hint)
1883fa0f6e62SHans Petter Selasky {
1884fa0f6e62SHans Petter Selasky 	struct cuse_client *pcc;
1885fa0f6e62SHans Petter Selasky 
1886fa0f6e62SHans Petter Selasky 	pcc = kn->kn_hook;
188798029019SHans Petter Selasky 
188898029019SHans Petter Selasky 	mtx_assert(&pcc->server->mtx, MA_OWNED);
188998029019SHans Petter Selasky 
1890fa0f6e62SHans Petter Selasky 	return ((pcc->cflags & CUSE_CLI_KNOTE_NEED_READ) ? 1 : 0);
1891fa0f6e62SHans Petter Selasky }
1892fa0f6e62SHans Petter Selasky 
1893fa0f6e62SHans Petter Selasky static int
1894fa0f6e62SHans Petter Selasky cuse_client_kqfilter_write_event(struct knote *kn, long hint)
1895fa0f6e62SHans Petter Selasky {
1896fa0f6e62SHans Petter Selasky 	struct cuse_client *pcc;
1897fa0f6e62SHans Petter Selasky 
1898fa0f6e62SHans Petter Selasky 	pcc = kn->kn_hook;
189998029019SHans Petter Selasky 
190098029019SHans Petter Selasky 	mtx_assert(&pcc->server->mtx, MA_OWNED);
190198029019SHans Petter Selasky 
1902fa0f6e62SHans Petter Selasky 	return ((pcc->cflags & CUSE_CLI_KNOTE_NEED_WRITE) ? 1 : 0);
1903fa0f6e62SHans Petter Selasky }
1904fa0f6e62SHans Petter Selasky 
1905fa0f6e62SHans Petter Selasky static int
1906fa0f6e62SHans Petter Selasky cuse_client_kqfilter(struct cdev *dev, struct knote *kn)
1907fa0f6e62SHans Petter Selasky {
1908fa0f6e62SHans Petter Selasky 	struct cuse_client *pcc;
1909fa0f6e62SHans Petter Selasky 	struct cuse_server *pcs;
1910fa0f6e62SHans Petter Selasky 	int error;
1911fa0f6e62SHans Petter Selasky 
1912fa0f6e62SHans Petter Selasky 	error = cuse_client_get(&pcc);
1913fa0f6e62SHans Petter Selasky 	if (error != 0)
1914fa0f6e62SHans Petter Selasky 		return (error);
1915fa0f6e62SHans Petter Selasky 
1916fa0f6e62SHans Petter Selasky 	pcs = pcc->server;
191798029019SHans Petter Selasky 
191898029019SHans Petter Selasky 	cuse_server_lock(pcs);
1919fa0f6e62SHans Petter Selasky 	switch (kn->kn_filter) {
1920fa0f6e62SHans Petter Selasky 	case EVFILT_READ:
1921fa0f6e62SHans Petter Selasky 		pcc->cflags |= CUSE_CLI_KNOTE_HAS_READ;
1922fa0f6e62SHans Petter Selasky 		kn->kn_hook = pcc;
1923fa0f6e62SHans Petter Selasky 		kn->kn_fop = &cuse_client_kqfilter_read_ops;
1924fa0f6e62SHans Petter Selasky 		knlist_add(&pcs->selinfo.si_note, kn, 1);
1925fa0f6e62SHans Petter Selasky 		break;
1926fa0f6e62SHans Petter Selasky 	case EVFILT_WRITE:
1927fa0f6e62SHans Petter Selasky 		pcc->cflags |= CUSE_CLI_KNOTE_HAS_WRITE;
1928fa0f6e62SHans Petter Selasky 		kn->kn_hook = pcc;
1929fa0f6e62SHans Petter Selasky 		kn->kn_fop = &cuse_client_kqfilter_write_ops;
1930fa0f6e62SHans Petter Selasky 		knlist_add(&pcs->selinfo.si_note, kn, 1);
1931fa0f6e62SHans Petter Selasky 		break;
1932fa0f6e62SHans Petter Selasky 	default:
1933fa0f6e62SHans Petter Selasky 		error = EINVAL;
1934fa0f6e62SHans Petter Selasky 		break;
1935fa0f6e62SHans Petter Selasky 	}
193698029019SHans Petter Selasky 	cuse_server_unlock(pcs);
1937fa0f6e62SHans Petter Selasky 
1938fa0f6e62SHans Petter Selasky 	if (error == 0)
1939fa0f6e62SHans Petter Selasky 		cuse_client_kqfilter_poll(dev, pcc);
1940fa0f6e62SHans Petter Selasky 	return (error);
1941fa0f6e62SHans Petter Selasky }
1942