3 * Copyright (c) 2010-2012 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/types.h>
38 #include <sys/queue.h>
39 #include <sys/fcntl.h>
41 #include <sys/param.h>
43 #include <fs/cuse/cuse_ioctl.h>
50 static const char *cuse_cmd_str(int cmd);
52 #define DPRINTF(...) do { \
53 if (cuse_debug_level != 0) \
54 printf(__VA_ARGS__); \
57 #define DPRINTF(...) do { } while (0)
60 struct cuse_vm_allocation {
65 struct cuse_dev_entered {
66 TAILQ_ENTRY(cuse_dev_entered) entry;
68 void *per_file_handle;
69 struct cuse_dev *cdev;
76 TAILQ_ENTRY(cuse_dev) entry;
77 const struct cuse_methods *mtod;
82 static int f_cuse = -1;
84 static pthread_mutex_t m_cuse;
85 static TAILQ_HEAD(, cuse_dev) h_cuse __guarded_by(m_cuse);
86 static TAILQ_HEAD(, cuse_dev_entered) h_cuse_entered __guarded_by(m_cuse);
87 static struct cuse_vm_allocation a_cuse[CUSE_ALLOC_UNIT_MAX]
91 pthread_mutex_lock(&m_cuse)
93 #define CUSE_UNLOCK() \
94 pthread_mutex_unlock(&m_cuse)
99 pthread_mutexattr_t attr;
101 f_cuse = open("/dev/cuse", O_RDWR);
103 if (feature_present("cuse") == 0)
104 return (CUSE_ERR_NOT_LOADED);
106 return (CUSE_ERR_INVALID);
108 pthread_mutexattr_init(&attr);
109 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
110 pthread_mutex_init(&m_cuse, &attr);
113 TAILQ_INIT(&h_cuse_entered);
124 return (CUSE_ERR_INVALID);
131 pthread_mutex_destroy(&m_cuse);
133 memset(a_cuse, 0, sizeof(a_cuse));
139 cuse_vmoffset(void *_ptr)
144 unsigned long remainder;
148 for (n = 0; n != CUSE_ALLOC_UNIT_MAX; n++) {
149 if (a_cuse[n].ptr == NULL)
152 ptr_min = a_cuse[n].ptr;
153 ptr_max = a_cuse[n].ptr + a_cuse[n].size - 1;
155 if ((ptr >= ptr_min) && (ptr <= ptr_max)) {
159 remainder = (ptr - ptr_min);
161 remainder -= remainder % PAGE_SIZE;
163 return ((n * PAGE_SIZE * CUSE_ALLOC_PAGES_MAX) + remainder);
168 return (0x80000000UL); /* failure */
172 cuse_vmalloc(int size)
174 struct cuse_alloc_info info;
182 memset(&info, 0, sizeof(info));
187 info.page_count = howmany(size, PAGE_SIZE);
190 for (n = 0; n != CUSE_ALLOC_UNIT_MAX; n++) {
192 if (a_cuse[n].ptr != NULL)
195 a_cuse[n].ptr = ((uint8_t *)1); /* reserve */
202 error = ioctl(f_cuse, CUSE_IOCTL_ALLOC_MEMORY, &info);
208 a_cuse[n].ptr = NULL;
215 ptr = mmap(NULL, info.page_count * PAGE_SIZE,
216 PROT_READ | PROT_WRITE,
217 MAP_SHARED, f_cuse, CUSE_ALLOC_PAGES_MAX *
220 if (ptr == MAP_FAILED) {
222 error = ioctl(f_cuse, CUSE_IOCTL_FREE_MEMORY, &info);
229 a_cuse[n].ptr = NULL;
235 a_cuse[n].size = size;
238 return (ptr); /* success */
241 return (NULL); /* failure */
245 cuse_is_vmalloc_addr(void *ptr)
249 if (f_cuse < 0 || ptr == NULL)
250 return (0); /* false */
253 for (n = 0; n != CUSE_ALLOC_UNIT_MAX; n++) {
254 if (a_cuse[n].ptr == ptr)
259 return (n != CUSE_ALLOC_UNIT_MAX);
263 cuse_vmfree(void *ptr)
265 struct cuse_vm_allocation temp;
266 struct cuse_alloc_info info;
274 for (n = 0; n != CUSE_ALLOC_UNIT_MAX; n++) {
275 if (a_cuse[n].ptr != ptr)
282 munmap(temp.ptr, temp.size);
284 memset(&info, 0, sizeof(info));
288 error = ioctl(f_cuse, CUSE_IOCTL_FREE_MEMORY, &info);
291 /* ignore any errors */
292 DPRINTF("Freeing memory failed: %d\n", errno);
296 a_cuse[n].ptr = NULL;
305 cuse_alloc_unit_number_by_id(int *pnum, int id)
310 return (CUSE_ERR_INVALID);
312 *pnum = (id & CUSE_ID_MASK);
314 error = ioctl(f_cuse, CUSE_IOCTL_ALLOC_UNIT_BY_ID, pnum);
316 return (CUSE_ERR_NO_MEMORY);
323 cuse_free_unit_number_by_id(int num, int id)
328 return (CUSE_ERR_INVALID);
330 if (num != -1 || id != -1)
331 num = (id & CUSE_ID_MASK) | (num & 0xFF);
333 error = ioctl(f_cuse, CUSE_IOCTL_FREE_UNIT_BY_ID, &num);
335 return (CUSE_ERR_NO_MEMORY);
341 cuse_alloc_unit_number(int *pnum)
346 return (CUSE_ERR_INVALID);
348 error = ioctl(f_cuse, CUSE_IOCTL_ALLOC_UNIT, pnum);
350 return (CUSE_ERR_NO_MEMORY);
356 cuse_free_unit_number(int num)
361 return (CUSE_ERR_INVALID);
363 error = ioctl(f_cuse, CUSE_IOCTL_FREE_UNIT, &num);
365 return (CUSE_ERR_NO_MEMORY);
371 cuse_dev_create(const struct cuse_methods *mtod, void *priv0, void *priv1,
372 uid_t _uid, gid_t _gid, int _perms, const char *_fmt,...)
374 struct cuse_create_dev info;
375 struct cuse_dev *cdev;
382 cdev = malloc(sizeof(*cdev));
386 memset(cdev, 0, sizeof(*cdev));
392 memset(&info, 0, sizeof(info));
396 info.group_id = _gid;
397 info.permissions = _perms;
399 va_start(args, _fmt);
400 vsnprintf(info.devname, sizeof(info.devname), _fmt, args);
403 error = ioctl(f_cuse, CUSE_IOCTL_CREATE_DEV, &info);
409 TAILQ_INSERT_TAIL(&h_cuse, cdev, entry);
417 cuse_dev_destroy(struct cuse_dev *cdev)
425 TAILQ_REMOVE(&h_cuse, cdev, entry);
428 error = ioctl(f_cuse, CUSE_IOCTL_DESTROY_DEV, &cdev);
436 cuse_dev_get_priv0(struct cuse_dev *cdev)
438 return (cdev->priv0);
442 cuse_dev_get_priv1(struct cuse_dev *cdev)
444 return (cdev->priv1);
448 cuse_dev_set_priv0(struct cuse_dev *cdev, void *priv)
454 cuse_dev_set_priv1(struct cuse_dev *cdev, void *priv)
460 cuse_wait_and_process(void)
462 pthread_t curr = pthread_self();
463 struct cuse_dev_entered *pe;
464 struct cuse_dev_entered enter;
465 struct cuse_command info;
466 struct cuse_dev *cdev;
470 return (CUSE_ERR_INVALID);
472 error = ioctl(f_cuse, CUSE_IOCTL_GET_COMMAND, &info);
474 return (CUSE_ERR_OTHER);
480 enter.per_file_handle = (void *)info.per_file_handle;
481 enter.cmd = info.command;
483 enter.got_signal = 0;
485 TAILQ_INSERT_TAIL(&h_cuse_entered, &enter, entry);
488 DPRINTF("cuse: Command = %d = %s, flags = %d, arg = 0x%08x, ptr = 0x%08x\n",
489 (int)info.command, cuse_cmd_str(info.command), (int)info.fflags,
490 (int)info.argument, (int)info.data_pointer);
492 switch (info.command) {
494 if (cdev->mtod->cm_open != NULL)
495 error = (cdev->mtod->cm_open) (cdev, (int)info.fflags);
502 /* wait for other threads to stop */
509 TAILQ_FOREACH(pe, &h_cuse_entered, entry) {
510 if (pe->cdev != cdev)
512 if (pe->thread == curr)
514 if (pe->per_file_handle !=
515 enter.per_file_handle)
518 pthread_kill(pe->thread, SIGHUP);
519 error = CUSE_ERR_BUSY;
529 if (cdev->mtod->cm_close != NULL)
530 error = (cdev->mtod->cm_close) (cdev, (int)info.fflags);
536 if (cdev->mtod->cm_read != NULL) {
537 error = (cdev->mtod->cm_read) (cdev, (int)info.fflags,
538 (void *)info.data_pointer, (int)info.argument);
540 error = CUSE_ERR_INVALID;
545 if (cdev->mtod->cm_write != NULL) {
546 error = (cdev->mtod->cm_write) (cdev, (int)info.fflags,
547 (void *)info.data_pointer, (int)info.argument);
549 error = CUSE_ERR_INVALID;
554 if (cdev->mtod->cm_ioctl != NULL) {
555 error = (cdev->mtod->cm_ioctl) (cdev, (int)info.fflags,
556 (unsigned int)info.argument, (void *)info.data_pointer);
558 error = CUSE_ERR_INVALID;
563 if (cdev->mtod->cm_poll != NULL) {
564 error = (cdev->mtod->cm_poll) (cdev, (int)info.fflags,
567 error = CUSE_POLL_ERROR;
571 case CUSE_CMD_SIGNAL:
573 TAILQ_FOREACH(pe, &h_cuse_entered, entry) {
574 if (pe->cdev != cdev)
576 if (pe->thread == curr)
578 if (pe->per_file_handle !=
579 enter.per_file_handle)
582 pthread_kill(pe->thread, SIGHUP);
588 error = CUSE_ERR_INVALID;
592 DPRINTF("cuse: Command error = %d for %s\n",
593 error, cuse_cmd_str(info.command));
596 TAILQ_REMOVE(&h_cuse_entered, &enter, entry);
599 /* we ignore any sync command failures */
600 ioctl(f_cuse, CUSE_IOCTL_SYNC_COMMAND, &error);
605 static struct cuse_dev_entered *
606 cuse_dev_get_entered(void)
608 struct cuse_dev_entered *pe;
609 pthread_t curr = pthread_self();
612 TAILQ_FOREACH(pe, &h_cuse_entered, entry) {
613 if (pe->thread == curr)
621 cuse_dev_set_per_file_handle(struct cuse_dev *cdev, void *handle)
623 struct cuse_dev_entered *pe;
625 pe = cuse_dev_get_entered();
626 if (pe == NULL || pe->cdev != cdev)
629 pe->per_file_handle = handle;
630 ioctl(f_cuse, CUSE_IOCTL_SET_PFH, &handle);
634 cuse_dev_get_per_file_handle(struct cuse_dev *cdev)
636 struct cuse_dev_entered *pe;
638 pe = cuse_dev_get_entered();
639 if (pe == NULL || pe->cdev != cdev)
642 return (pe->per_file_handle);
646 cuse_set_local(int val)
648 struct cuse_dev_entered *pe;
650 pe = cuse_dev_get_entered();
659 cuse_cmd_str(int cmd)
661 static const char *str[CUSE_CMD_MAX] = {
662 [CUSE_CMD_NONE] = "none",
663 [CUSE_CMD_OPEN] = "open",
664 [CUSE_CMD_CLOSE] = "close",
665 [CUSE_CMD_READ] = "read",
666 [CUSE_CMD_WRITE] = "write",
667 [CUSE_CMD_IOCTL] = "ioctl",
668 [CUSE_CMD_POLL] = "poll",
669 [CUSE_CMD_SIGNAL] = "signal",
670 [CUSE_CMD_SYNC] = "sync",
673 if ((cmd >= 0) && (cmd < CUSE_CMD_MAX) &&
685 struct cuse_dev_entered *pe;
687 pe = cuse_dev_get_entered();
691 return (pe->is_local);
695 cuse_copy_out(const void *src, void *user_dst, int len)
697 struct cuse_data_chunk info;
698 struct cuse_dev_entered *pe;
701 if ((f_cuse < 0) || (len < 0))
702 return (CUSE_ERR_INVALID);
704 pe = cuse_dev_get_entered();
706 return (CUSE_ERR_INVALID);
708 DPRINTF("cuse: copy_out(%p,%p,%d), cmd = %d = %s\n",
709 src, user_dst, len, pe->cmd, cuse_cmd_str(pe->cmd));
712 memcpy(user_dst, src, len);
714 info.local_ptr = (uintptr_t)src;
715 info.peer_ptr = (uintptr_t)user_dst;
718 error = ioctl(f_cuse, CUSE_IOCTL_WRITE_DATA, &info);
720 DPRINTF("cuse: copy_out() error = %d\n", errno);
721 return (CUSE_ERR_FAULT);
728 cuse_copy_in(const void *user_src, void *dst, int len)
730 struct cuse_data_chunk info;
731 struct cuse_dev_entered *pe;
734 if ((f_cuse < 0) || (len < 0))
735 return (CUSE_ERR_INVALID);
737 pe = cuse_dev_get_entered();
739 return (CUSE_ERR_INVALID);
741 DPRINTF("cuse: copy_in(%p,%p,%d), cmd = %d = %s\n",
742 user_src, dst, len, pe->cmd, cuse_cmd_str(pe->cmd));
745 memcpy(dst, user_src, len);
747 info.local_ptr = (uintptr_t)dst;
748 info.peer_ptr = (uintptr_t)user_src;
751 error = ioctl(f_cuse, CUSE_IOCTL_READ_DATA, &info);
753 DPRINTF("cuse: copy_in() error = %d\n", errno);
754 return (CUSE_ERR_FAULT);
761 cuse_dev_get_current(int *pcmd)
763 struct cuse_dev_entered *pe;
765 pe = cuse_dev_get_entered();
777 cuse_got_peer_signal(void)
779 struct cuse_dev_entered *pe;
781 pe = cuse_dev_get_entered();
783 return (CUSE_ERR_INVALID);
788 return (CUSE_ERR_OTHER);
792 cuse_poll_wakeup(void)
799 ioctl(f_cuse, CUSE_IOCTL_SELWAKEUP, &error);