xref: /freebsd/sys/fs/fuse/fuse_file.h (revision cddbc3b40812213ff00041f79174cac0be360a2a)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2007-2009 Google Inc. and Amit Singh
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are
9  * met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  *   notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  *   copyright notice, this list of conditions and the following disclaimer
15  *   in the documentation and/or other materials provided with the
16  *   distribution.
17  * * Neither the name of Google Inc. nor the names of its
18  *   contributors may be used to endorse or promote products derived from
19  *   this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Copyright (C) 2005 Csaba Henk.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  *
45  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  *
57  * $FreeBSD$
58  */
59 
60 #ifndef _FUSE_FILE_H_
61 #define _FUSE_FILE_H_
62 
63 #include <sys/types.h>
64 #include <sys/fcntl.h>
65 #include <sys/stat.h>
66 #include <sys/mman.h>
67 #include <sys/vnode.h>
68 
69 typedef enum fufh_type {
70 	FUFH_INVALID = -1,
71 	FUFH_RDONLY  = 0,
72 	FUFH_WRONLY  = 1,
73 	FUFH_RDWR    = 2,
74 	FUFH_MAXTYPE = 3,
75 } fufh_type_t;
76 _Static_assert(FUFH_RDONLY == O_RDONLY, "RDONLY");
77 _Static_assert(FUFH_WRONLY == O_WRONLY, "WRONLY");
78 _Static_assert(FUFH_RDWR == O_RDWR, "RDWR");
79 
80 struct fuse_filehandle {
81 	uint64_t fh_id;
82 	fufh_type_t fh_type;
83 };
84 
85 #define FUFH_IS_VALID(f)  ((f)->fh_type != FUFH_INVALID)
86 
87 static inline fufh_type_t
88 fuse_filehandle_xlate_from_mmap(int fflags)
89 {
90 	if (fflags & (PROT_READ | PROT_WRITE))
91 		return FUFH_RDWR;
92 	else if (fflags & (PROT_WRITE))
93 		return FUFH_WRONLY;
94 	else if ((fflags & PROT_READ) || (fflags & PROT_EXEC))
95 		return FUFH_RDONLY;
96 	else
97 		return FUFH_INVALID;
98 }
99 
100 static inline fufh_type_t
101 fuse_filehandle_xlate_from_fflags(int fflags)
102 {
103 	if ((fflags & FREAD) && (fflags & FWRITE))
104 		return FUFH_RDWR;
105 	else if (fflags & (FWRITE))
106 		return FUFH_WRONLY;
107 	else if (fflags & (FREAD))
108 		return FUFH_RDONLY;
109 	else
110 		panic("FUSE: What kind of a flag is this (%x)?", fflags);
111 }
112 
113 static inline int
114 fuse_filehandle_xlate_to_oflags(fufh_type_t type)
115 {
116 	int oflags = -1;
117 
118 	switch (type) {
119 	case FUFH_RDONLY:
120 	case FUFH_WRONLY:
121 	case FUFH_RDWR:
122 		oflags = type;
123 		break;
124 	default:
125 		break;
126 	}
127 
128 	return oflags;
129 }
130 
131 int fuse_filehandle_valid(struct vnode *vp, fufh_type_t fufh_type);
132 fufh_type_t fuse_filehandle_validrw(struct vnode *vp, fufh_type_t fufh_type);
133 int fuse_filehandle_get(struct vnode *vp, fufh_type_t fufh_type,
134                         struct fuse_filehandle **fufhp);
135 int fuse_filehandle_getrw(struct vnode *vp, fufh_type_t fufh_type,
136                           struct fuse_filehandle **fufhp);
137 
138 void fuse_filehandle_init(struct vnode *vp, fufh_type_t fufh_type,
139 		          struct fuse_filehandle **fufhp, uint64_t fh_id);
140 int fuse_filehandle_open(struct vnode *vp, fufh_type_t fufh_type,
141                          struct fuse_filehandle **fufhp, struct thread *td,
142                          struct ucred *cred);
143 int fuse_filehandle_close(struct vnode *vp, fufh_type_t fufh_type,
144                           struct thread *td, struct ucred *cred);
145 
146 #endif /* _FUSE_FILE_H_ */
147