xref: /openbsd-src/sys/miscfs/fuse/fuse_file.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /* $OpenBSD: fuse_file.c,v 1.8 2014/03/18 08:51:53 mpi 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/systm.h>
20 #include <sys/statvfs.h>
21 #include <sys/vnode.h>
22 #include <sys/fusebuf.h>
23 
24 #include "fusefs_node.h"
25 #include "fusefs.h"
26 
27 int
28 fusefs_file_open(struct fusefs_mnt *fmp, struct fusefs_node *ip,
29     enum fufh_type fufh_type, int flags, int isdir, struct proc *p)
30 {
31 	struct fusebuf *fbuf;
32 	int error = 0;
33 
34 	if (!fmp->sess_init)
35 		return (0);
36 
37 	fbuf = fb_setup(0, ip->ufs_ino.i_number,
38 	    ((isdir) ? FBT_OPENDIR : FBT_OPEN), p);
39 	fbuf->fb_io_flags = flags;
40 
41 	error = fb_queue(fmp->dev, fbuf);
42 	if (error) {
43 		fb_delete(fbuf);
44 		return (error);
45 	}
46 
47 	ip->fufh[fufh_type].fh_id = fbuf->fb_io_fd;
48 	ip->fufh[fufh_type].fh_type = fufh_type;
49 
50 	fb_delete(fbuf);
51 	return (0);
52 }
53 
54 int
55 fusefs_file_close(struct fusefs_mnt *fmp, struct fusefs_node * ip,
56     enum fufh_type fufh_type, int flags, int isdir, struct proc *p)
57 {
58 	struct fusebuf *fbuf;
59 	int error = 0;
60 
61 	if (fmp->sess_init) {
62 		fbuf = fb_setup(0, ip->ufs_ino.i_number,
63 		    ((isdir) ? FBT_RELEASEDIR : FBT_RELEASE), p);
64 		fbuf->fb_io_fd  = ip->fufh[fufh_type].fh_id;
65 		fbuf->fb_io_flags = flags;
66 
67 		error = fb_queue(fmp->dev, fbuf);
68 		if (error && (error != ENOSYS))
69 			printf("fusefs: file error %d\n", error);
70 
71 		fb_delete(fbuf);
72 	}
73 
74 	ip->fufh[fufh_type].fh_id = (uint64_t)-1;
75 	ip->fufh[fufh_type].fh_type = FUFH_INVALID;
76 
77 	return (error);
78 }
79 
80 uint64_t
81 fusefs_fd_get(struct fusefs_node *ip, enum fufh_type type)
82 {
83 	if (ip->fufh[type].fh_type == FUFH_INVALID)
84 		type = FUFH_RDWR;
85 
86 	return (ip->fufh[type].fh_id);
87 }
88