1 /* $OpenBSD: fusebuf.c,v 1.7 2014/07/12 18:43:52 tedu Exp $ */ 2 /* 3 * Copyright (c) 2012-2013 Sylvestre Gallon <ccna.syl@gmail.com> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <sys/param.h> 19 #include <sys/kernel.h> 20 #include <sys/malloc.h> 21 #include <sys/mount.h> 22 #include <sys/pool.h> 23 #include <sys/proc.h> 24 #include <sys/statvfs.h> 25 #include <sys/systm.h> 26 #include <sys/vnode.h> 27 #include <sys/fusebuf.h> 28 29 #include <dev/rndvar.h> 30 31 #include "fusefs_node.h" 32 #include "fusefs.h" 33 34 struct fusebuf * 35 fb_setup(size_t len, ino_t ino, int op, struct proc *p) 36 { 37 struct fusebuf *fbuf; 38 39 fbuf = pool_get(&fusefs_fbuf_pool, PR_WAITOK | PR_ZERO); 40 fbuf->fb_len = len; 41 fbuf->fb_err = 0; 42 fbuf->fb_uuid = ((uint64_t)arc4random() << 32 | arc4random()); 43 fbuf->fb_type = op; 44 fbuf->fb_ino = ino; 45 if (len == 0) 46 fbuf->fb_dat = NULL; 47 else 48 fbuf->fb_dat = (uint8_t *)malloc(len, M_FUSEFS, 49 M_WAITOK | M_ZERO); 50 51 return (fbuf); 52 } 53 54 int 55 fb_queue(dev_t dev, struct fusebuf *fbuf) 56 { 57 int error = 0; 58 59 fuse_device_queue_fbuf(dev, fbuf); 60 61 if ((error = tsleep(fbuf, PWAIT, "fuse msg", TSLEEP_TIMEOUT * hz))) { 62 fuse_device_cleanup(dev, fbuf); 63 return (error); 64 } 65 66 return (fbuf->fb_err); 67 } 68 69 void 70 fb_delete(struct fusebuf *fbuf) 71 { 72 if (fbuf != NULL) { 73 free(fbuf->fb_dat, M_FUSEFS, 0); 74 pool_put(&fusefs_fbuf_pool, fbuf); 75 } 76 } 77