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