xref: /openbmc/qemu/hw/9pfs/9p.h (revision b23197f9)
1 #ifndef _QEMU_9P_H
2 #define _QEMU_9P_H
3 
4 #include <dirent.h>
5 #include <utime.h>
6 #include <sys/resource.h>
7 #include <glib.h>
8 #include "standard-headers/linux/virtio_9p.h"
9 #include "hw/virtio/virtio.h"
10 #include "fsdev/file-op-9p.h"
11 #include "fsdev/9p-iov-marshal.h"
12 #include "qemu/thread.h"
13 #include "qemu/coroutine.h"
14 
15 enum {
16     P9_TLERROR = 6,
17     P9_RLERROR,
18     P9_TSTATFS = 8,
19     P9_RSTATFS,
20     P9_TLOPEN = 12,
21     P9_RLOPEN,
22     P9_TLCREATE = 14,
23     P9_RLCREATE,
24     P9_TSYMLINK = 16,
25     P9_RSYMLINK,
26     P9_TMKNOD = 18,
27     P9_RMKNOD,
28     P9_TRENAME = 20,
29     P9_RRENAME,
30     P9_TREADLINK = 22,
31     P9_RREADLINK,
32     P9_TGETATTR = 24,
33     P9_RGETATTR,
34     P9_TSETATTR = 26,
35     P9_RSETATTR,
36     P9_TXATTRWALK = 30,
37     P9_RXATTRWALK,
38     P9_TXATTRCREATE = 32,
39     P9_RXATTRCREATE,
40     P9_TREADDIR = 40,
41     P9_RREADDIR,
42     P9_TFSYNC = 50,
43     P9_RFSYNC,
44     P9_TLOCK = 52,
45     P9_RLOCK,
46     P9_TGETLOCK = 54,
47     P9_RGETLOCK,
48     P9_TLINK = 70,
49     P9_RLINK,
50     P9_TMKDIR = 72,
51     P9_RMKDIR,
52     P9_TRENAMEAT = 74,
53     P9_RRENAMEAT,
54     P9_TUNLINKAT = 76,
55     P9_RUNLINKAT,
56     P9_TVERSION = 100,
57     P9_RVERSION,
58     P9_TAUTH = 102,
59     P9_RAUTH,
60     P9_TATTACH = 104,
61     P9_RATTACH,
62     P9_TERROR = 106,
63     P9_RERROR,
64     P9_TFLUSH = 108,
65     P9_RFLUSH,
66     P9_TWALK = 110,
67     P9_RWALK,
68     P9_TOPEN = 112,
69     P9_ROPEN,
70     P9_TCREATE = 114,
71     P9_RCREATE,
72     P9_TREAD = 116,
73     P9_RREAD,
74     P9_TWRITE = 118,
75     P9_RWRITE,
76     P9_TCLUNK = 120,
77     P9_RCLUNK,
78     P9_TREMOVE = 122,
79     P9_RREMOVE,
80     P9_TSTAT = 124,
81     P9_RSTAT,
82     P9_TWSTAT = 126,
83     P9_RWSTAT,
84 };
85 
86 
87 /* qid.types */
88 enum {
89     P9_QTDIR = 0x80,
90     P9_QTAPPEND = 0x40,
91     P9_QTEXCL = 0x20,
92     P9_QTMOUNT = 0x10,
93     P9_QTAUTH = 0x08,
94     P9_QTTMP = 0x04,
95     P9_QTSYMLINK = 0x02,
96     P9_QTLINK = 0x01,
97     P9_QTFILE = 0x00,
98 };
99 
100 enum p9_proto_version {
101     V9FS_PROTO_2000U = 0x01,
102     V9FS_PROTO_2000L = 0x02,
103 };
104 
105 #define P9_NOTAG    (u16)(~0)
106 #define P9_NOFID    (u32)(~0)
107 #define P9_MAXWELEM 16
108 
109 #define FID_REFERENCED          0x1
110 #define FID_NON_RECLAIMABLE     0x2
111 static inline char *rpath(FsContext *ctx, const char *path)
112 {
113     return g_strdup_printf("%s/%s", ctx->fs_root, path);
114 }
115 
116 /*
117  * ample room for Twrite/Rread header
118  * size[4] Tread/Twrite tag[2] fid[4] offset[8] count[4]
119  */
120 #define P9_IOHDRSZ 24
121 
122 typedef struct V9fsPDU V9fsPDU;
123 struct V9fsState;
124 
125 struct V9fsPDU
126 {
127     uint32_t size;
128     uint16_t tag;
129     uint8_t id;
130     uint8_t cancelled;
131     CoQueue complete;
132     struct V9fsState *s;
133     QLIST_ENTRY(V9fsPDU) next;
134     uint32_t idx;
135 };
136 
137 
138 /* FIXME
139  * 1) change user needs to set groups and stuff
140  */
141 
142 #define MAX_REQ         128
143 #define MAX_TAG_LEN     32
144 
145 #define BUG_ON(cond) assert(!(cond))
146 
147 typedef struct V9fsFidState V9fsFidState;
148 
149 enum {
150     P9_FID_NONE = 0,
151     P9_FID_FILE,
152     P9_FID_DIR,
153     P9_FID_XATTR,
154 };
155 
156 typedef struct V9fsConf
157 {
158     /* tag name for the device */
159     char *tag;
160     char *fsdev_id;
161 } V9fsConf;
162 
163 typedef struct V9fsXattr
164 {
165     int64_t copied_len;
166     int64_t len;
167     void *value;
168     V9fsString name;
169     int flags;
170 } V9fsXattr;
171 
172 /*
173  * Filled by fs driver on open and other
174  * calls.
175  */
176 union V9fsFidOpenState {
177     int fd;
178     DIR *dir;
179     V9fsXattr xattr;
180     /*
181      * private pointer for fs drivers, that
182      * have its own internal representation of
183      * open files.
184      */
185     void *private;
186 };
187 
188 struct V9fsFidState
189 {
190     int fid_type;
191     int32_t fid;
192     V9fsPath path;
193     V9fsFidOpenState fs;
194     V9fsFidOpenState fs_reclaim;
195     int flags;
196     int open_flags;
197     uid_t uid;
198     int ref;
199     int clunked;
200     V9fsFidState *next;
201     V9fsFidState *rclm_lst;
202 };
203 
204 typedef struct V9fsState
205 {
206     QLIST_HEAD(, V9fsPDU) free_list;
207     QLIST_HEAD(, V9fsPDU) active_list;
208     V9fsFidState *fid_list;
209     FileOperations *ops;
210     FsContext ctx;
211     char *tag;
212     enum p9_proto_version proto_version;
213     int32_t msize;
214     /*
215      * lock ensuring atomic path update
216      * on rename.
217      */
218     CoRwlock rename_lock;
219     int32_t root_fid;
220     Error *migration_blocker;
221     V9fsConf fsconf;
222 } V9fsState;
223 
224 /* 9p2000.L open flags */
225 #define P9_DOTL_RDONLY        00000000
226 #define P9_DOTL_WRONLY        00000001
227 #define P9_DOTL_RDWR          00000002
228 #define P9_DOTL_NOACCESS      00000003
229 #define P9_DOTL_CREATE        00000100
230 #define P9_DOTL_EXCL          00000200
231 #define P9_DOTL_NOCTTY        00000400
232 #define P9_DOTL_TRUNC         00001000
233 #define P9_DOTL_APPEND        00002000
234 #define P9_DOTL_NONBLOCK      00004000
235 #define P9_DOTL_DSYNC         00010000
236 #define P9_DOTL_FASYNC        00020000
237 #define P9_DOTL_DIRECT        00040000
238 #define P9_DOTL_LARGEFILE     00100000
239 #define P9_DOTL_DIRECTORY     00200000
240 #define P9_DOTL_NOFOLLOW      00400000
241 #define P9_DOTL_NOATIME       01000000
242 #define P9_DOTL_CLOEXEC       02000000
243 #define P9_DOTL_SYNC          04000000
244 
245 /* 9p2000.L at flags */
246 #define P9_DOTL_AT_REMOVEDIR         0x200
247 
248 /* 9P2000.L lock type */
249 #define P9_LOCK_TYPE_RDLCK 0
250 #define P9_LOCK_TYPE_WRLCK 1
251 #define P9_LOCK_TYPE_UNLCK 2
252 
253 #define P9_LOCK_SUCCESS 0
254 #define P9_LOCK_BLOCKED 1
255 #define P9_LOCK_ERROR 2
256 #define P9_LOCK_GRACE 3
257 
258 #define P9_LOCK_FLAGS_BLOCK 1
259 #define P9_LOCK_FLAGS_RECLAIM 2
260 
261 typedef struct V9fsFlock
262 {
263     uint8_t type;
264     uint32_t flags;
265     uint64_t start; /* absolute offset */
266     uint64_t length;
267     uint32_t proc_id;
268     V9fsString client_id;
269 } V9fsFlock;
270 
271 typedef struct V9fsGetlock
272 {
273     uint8_t type;
274     uint64_t start; /* absolute offset */
275     uint64_t length;
276     uint32_t proc_id;
277     V9fsString client_id;
278 } V9fsGetlock;
279 
280 extern int open_fd_hw;
281 extern int total_open_fd;
282 
283 static inline void v9fs_path_write_lock(V9fsState *s)
284 {
285     if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
286         qemu_co_rwlock_wrlock(&s->rename_lock);
287     }
288 }
289 
290 static inline void v9fs_path_read_lock(V9fsState *s)
291 {
292     if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
293         qemu_co_rwlock_rdlock(&s->rename_lock);
294     }
295 }
296 
297 static inline void v9fs_path_unlock(V9fsState *s)
298 {
299     if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
300         qemu_co_rwlock_unlock(&s->rename_lock);
301     }
302 }
303 
304 static inline uint8_t v9fs_request_cancelled(V9fsPDU *pdu)
305 {
306     return pdu->cancelled;
307 }
308 
309 extern void v9fs_reclaim_fd(V9fsPDU *pdu);
310 extern void v9fs_path_init(V9fsPath *path);
311 extern void v9fs_path_free(V9fsPath *path);
312 extern void v9fs_path_copy(V9fsPath *lhs, V9fsPath *rhs);
313 extern int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
314                              const char *name, V9fsPath *path);
315 extern int v9fs_device_realize_common(V9fsState *s, Error **errp);
316 extern void v9fs_device_unrealize_common(V9fsState *s, Error **errp);
317 
318 ssize_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...);
319 ssize_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...);
320 V9fsPDU *pdu_alloc(V9fsState *s);
321 void pdu_free(V9fsPDU *pdu);
322 void pdu_submit(V9fsPDU *pdu);
323 
324 #endif
325