xref: /openbmc/qemu/hw/9pfs/9p.h (revision 0c4e9931)
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 "fsdev/file-op-9p.h"
8 #include "fsdev/9p-iov-marshal.h"
9 #include "qemu/thread.h"
10 #include "qemu/coroutine.h"
11 #include "qemu/qht.h"
12 
13 enum {
14     P9_TLERROR = 6,
15     P9_RLERROR,
16     P9_TSTATFS = 8,
17     P9_RSTATFS,
18     P9_TLOPEN = 12,
19     P9_RLOPEN,
20     P9_TLCREATE = 14,
21     P9_RLCREATE,
22     P9_TSYMLINK = 16,
23     P9_RSYMLINK,
24     P9_TMKNOD = 18,
25     P9_RMKNOD,
26     P9_TRENAME = 20,
27     P9_RRENAME,
28     P9_TREADLINK = 22,
29     P9_RREADLINK,
30     P9_TGETATTR = 24,
31     P9_RGETATTR,
32     P9_TSETATTR = 26,
33     P9_RSETATTR,
34     P9_TXATTRWALK = 30,
35     P9_RXATTRWALK,
36     P9_TXATTRCREATE = 32,
37     P9_RXATTRCREATE,
38     P9_TREADDIR = 40,
39     P9_RREADDIR,
40     P9_TFSYNC = 50,
41     P9_RFSYNC,
42     P9_TLOCK = 52,
43     P9_RLOCK,
44     P9_TGETLOCK = 54,
45     P9_RGETLOCK,
46     P9_TLINK = 70,
47     P9_RLINK,
48     P9_TMKDIR = 72,
49     P9_RMKDIR,
50     P9_TRENAMEAT = 74,
51     P9_RRENAMEAT,
52     P9_TUNLINKAT = 76,
53     P9_RUNLINKAT,
54     P9_TVERSION = 100,
55     P9_RVERSION,
56     P9_TAUTH = 102,
57     P9_RAUTH,
58     P9_TATTACH = 104,
59     P9_RATTACH,
60     P9_TERROR = 106,
61     P9_RERROR,
62     P9_TFLUSH = 108,
63     P9_RFLUSH,
64     P9_TWALK = 110,
65     P9_RWALK,
66     P9_TOPEN = 112,
67     P9_ROPEN,
68     P9_TCREATE = 114,
69     P9_RCREATE,
70     P9_TREAD = 116,
71     P9_RREAD,
72     P9_TWRITE = 118,
73     P9_RWRITE,
74     P9_TCLUNK = 120,
75     P9_RCLUNK,
76     P9_TREMOVE = 122,
77     P9_RREMOVE,
78     P9_TSTAT = 124,
79     P9_RSTAT,
80     P9_TWSTAT = 126,
81     P9_RWSTAT,
82 };
83 
84 
85 /* qid.types */
86 enum {
87     P9_QTDIR = 0x80,
88     P9_QTAPPEND = 0x40,
89     P9_QTEXCL = 0x20,
90     P9_QTMOUNT = 0x10,
91     P9_QTAUTH = 0x08,
92     P9_QTTMP = 0x04,
93     P9_QTSYMLINK = 0x02,
94     P9_QTLINK = 0x01,
95     P9_QTFILE = 0x00,
96 };
97 
98 typedef enum P9ProtoVersion {
99     V9FS_PROTO_2000U = 0x01,
100     V9FS_PROTO_2000L = 0x02,
101 } P9ProtoVersion;
102 
103 /**
104  * @brief Minimum message size supported by this 9pfs server.
105  *
106  * A client establishes a session by sending a Tversion request along with a
107  * 'msize' parameter which suggests the server a maximum message size ever to be
108  * used for communication (for both requests and replies) between client and
109  * server during that session. If client suggests a 'msize' smaller than this
110  * value then session is denied by server with an error response.
111  */
112 #define P9_MIN_MSIZE    4096
113 
114 #define P9_NOTAG    UINT16_MAX
115 #define P9_NOFID    UINT32_MAX
116 #define P9_MAXWELEM 16
117 
118 #define FID_REFERENCED          0x1
119 #define FID_NON_RECLAIMABLE     0x2
120 static inline char *rpath(FsContext *ctx, const char *path)
121 {
122     return g_strdup_printf("%s/%s", ctx->fs_root, path);
123 }
124 
125 /*
126  * ample room for Twrite/Rread header
127  * size[4] Tread/Twrite tag[2] fid[4] offset[8] count[4]
128  */
129 #define P9_IOHDRSZ 24
130 
131 typedef struct V9fsPDU V9fsPDU;
132 typedef struct V9fsState V9fsState;
133 typedef struct V9fsTransport V9fsTransport;
134 
135 typedef struct {
136     uint32_t size_le;
137     uint8_t id;
138     uint16_t tag_le;
139 } QEMU_PACKED P9MsgHeader;
140 /* According to the specification, 9p messages start with a 7-byte header.
141  * Since most of the code uses this header size in literal form, we must be
142  * sure this is indeed the case.
143  */
144 QEMU_BUILD_BUG_ON(sizeof(P9MsgHeader) != 7);
145 
146 struct V9fsPDU
147 {
148     uint32_t size;
149     uint16_t tag;
150     uint8_t id;
151     uint8_t cancelled;
152     CoQueue complete;
153     V9fsState *s;
154     QLIST_ENTRY(V9fsPDU) next;
155     uint32_t idx;
156 };
157 
158 
159 /* FIXME
160  * 1) change user needs to set groups and stuff
161  */
162 
163 #define MAX_REQ         128
164 #define MAX_TAG_LEN     32
165 
166 #define BUG_ON(cond) assert(!(cond))
167 
168 typedef struct V9fsFidState V9fsFidState;
169 
170 enum {
171     P9_FID_NONE = 0,
172     P9_FID_FILE,
173     P9_FID_DIR,
174     P9_FID_XATTR,
175 };
176 
177 typedef struct V9fsConf
178 {
179     /* tag name for the device */
180     char *tag;
181     char *fsdev_id;
182 } V9fsConf;
183 
184 /* 9p2000.L xattr flags (matches Linux values) */
185 #define P9_XATTR_CREATE 1
186 #define P9_XATTR_REPLACE 2
187 
188 typedef struct V9fsXattr
189 {
190     uint64_t copied_len;
191     uint64_t len;
192     void *value;
193     V9fsString name;
194     int flags;
195     bool xattrwalk_fid;
196 } V9fsXattr;
197 
198 typedef struct V9fsDir {
199     DIR *stream;
200     CoMutex readdir_mutex;
201 } V9fsDir;
202 
203 static inline void v9fs_readdir_lock(V9fsDir *dir)
204 {
205     qemu_co_mutex_lock(&dir->readdir_mutex);
206 }
207 
208 static inline void v9fs_readdir_unlock(V9fsDir *dir)
209 {
210     qemu_co_mutex_unlock(&dir->readdir_mutex);
211 }
212 
213 static inline void v9fs_readdir_init(V9fsDir *dir)
214 {
215     qemu_co_mutex_init(&dir->readdir_mutex);
216 }
217 
218 /*
219  * Filled by fs driver on open and other
220  * calls.
221  */
222 union V9fsFidOpenState {
223     int fd;
224     V9fsDir dir;
225     V9fsXattr xattr;
226     /*
227      * private pointer for fs drivers, that
228      * have its own internal representation of
229      * open files.
230      */
231     void *private;
232 };
233 
234 struct V9fsFidState
235 {
236     int fid_type;
237     int32_t fid;
238     V9fsPath path;
239     V9fsFidOpenState fs;
240     V9fsFidOpenState fs_reclaim;
241     int flags;
242     int open_flags;
243     uid_t uid;
244     int ref;
245     int clunked;
246     V9fsFidState *next;
247     V9fsFidState *rclm_lst;
248 };
249 
250 typedef enum AffixType_t {
251     AffixType_Prefix,
252     AffixType_Suffix, /* A.k.a. postfix. */
253 } AffixType_t;
254 
255 /**
256  * @brief Unique affix of variable length.
257  *
258  * An affix is (currently) either a suffix or a prefix, which is either
259  * going to be prepended (prefix) or appended (suffix) with some other
260  * number for the goal to generate unique numbers. Accordingly the
261  * suffixes (or prefixes) we generate @b must all have the mathematical
262  * property of being suffix-free (or prefix-free in case of prefixes)
263  * so that no matter what number we concatenate the affix with, that we
264  * always reliably get unique numbers as result after concatenation.
265  */
266 typedef struct VariLenAffix {
267     AffixType_t type; /* Whether this affix is a suffix or a prefix. */
268     uint64_t value; /* Actual numerical value of this affix. */
269     /*
270      * Lenght of the affix, that is how many (of the lowest) bits of @c value
271      * must be used for appending/prepending this affix to its final resulting,
272      * unique number.
273      */
274     int bits;
275 } VariLenAffix;
276 
277 /* See qid_inode_prefix_hash_bits(). */
278 typedef struct {
279     dev_t dev; /* FS device on host. */
280     /*
281      * How many (high) bits of the original inode number shall be used for
282      * hashing.
283      */
284     int prefix_bits;
285 } QpdEntry;
286 
287 /* QID path prefix entry, see stat_to_qid */
288 typedef struct {
289     dev_t dev;
290     uint16_t ino_prefix;
291     uint32_t qp_affix_index;
292     VariLenAffix qp_affix;
293 } QppEntry;
294 
295 /* QID path full entry, as above */
296 typedef struct {
297     dev_t dev;
298     ino_t ino;
299     uint64_t path;
300 } QpfEntry;
301 
302 struct V9fsState
303 {
304     QLIST_HEAD(, V9fsPDU) free_list;
305     QLIST_HEAD(, V9fsPDU) active_list;
306     V9fsFidState *fid_list;
307     FileOperations *ops;
308     FsContext ctx;
309     char *tag;
310     P9ProtoVersion proto_version;
311     int32_t msize;
312     V9fsPDU pdus[MAX_REQ];
313     const V9fsTransport *transport;
314     /*
315      * lock ensuring atomic path update
316      * on rename.
317      */
318     CoRwlock rename_lock;
319     int32_t root_fid;
320     Error *migration_blocker;
321     V9fsConf fsconf;
322     V9fsQID root_qid;
323     dev_t dev_id;
324     struct qht qpd_table;
325     struct qht qpp_table;
326     struct qht qpf_table;
327     uint64_t qp_ndevices; /* Amount of entries in qpd_table. */
328     uint16_t qp_affix_next;
329     uint64_t qp_fullpath_next;
330 };
331 
332 /* 9p2000.L open flags */
333 #define P9_DOTL_RDONLY        00000000
334 #define P9_DOTL_WRONLY        00000001
335 #define P9_DOTL_RDWR          00000002
336 #define P9_DOTL_NOACCESS      00000003
337 #define P9_DOTL_CREATE        00000100
338 #define P9_DOTL_EXCL          00000200
339 #define P9_DOTL_NOCTTY        00000400
340 #define P9_DOTL_TRUNC         00001000
341 #define P9_DOTL_APPEND        00002000
342 #define P9_DOTL_NONBLOCK      00004000
343 #define P9_DOTL_DSYNC         00010000
344 #define P9_DOTL_FASYNC        00020000
345 #define P9_DOTL_DIRECT        00040000
346 #define P9_DOTL_LARGEFILE     00100000
347 #define P9_DOTL_DIRECTORY     00200000
348 #define P9_DOTL_NOFOLLOW      00400000
349 #define P9_DOTL_NOATIME       01000000
350 #define P9_DOTL_CLOEXEC       02000000
351 #define P9_DOTL_SYNC          04000000
352 
353 /* 9p2000.L at flags */
354 #define P9_DOTL_AT_REMOVEDIR         0x200
355 
356 /* 9P2000.L lock type */
357 #define P9_LOCK_TYPE_RDLCK 0
358 #define P9_LOCK_TYPE_WRLCK 1
359 #define P9_LOCK_TYPE_UNLCK 2
360 
361 #define P9_LOCK_SUCCESS 0
362 #define P9_LOCK_BLOCKED 1
363 #define P9_LOCK_ERROR 2
364 #define P9_LOCK_GRACE 3
365 
366 #define P9_LOCK_FLAGS_BLOCK 1
367 #define P9_LOCK_FLAGS_RECLAIM 2
368 
369 typedef struct V9fsFlock
370 {
371     uint8_t type;
372     uint32_t flags;
373     uint64_t start; /* absolute offset */
374     uint64_t length;
375     uint32_t proc_id;
376     V9fsString client_id;
377 } V9fsFlock;
378 
379 typedef struct V9fsGetlock
380 {
381     uint8_t type;
382     uint64_t start; /* absolute offset */
383     uint64_t length;
384     uint32_t proc_id;
385     V9fsString client_id;
386 } V9fsGetlock;
387 
388 extern int open_fd_hw;
389 extern int total_open_fd;
390 
391 static inline void v9fs_path_write_lock(V9fsState *s)
392 {
393     if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
394         qemu_co_rwlock_wrlock(&s->rename_lock);
395     }
396 }
397 
398 static inline void v9fs_path_read_lock(V9fsState *s)
399 {
400     if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
401         qemu_co_rwlock_rdlock(&s->rename_lock);
402     }
403 }
404 
405 static inline void v9fs_path_unlock(V9fsState *s)
406 {
407     if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
408         qemu_co_rwlock_unlock(&s->rename_lock);
409     }
410 }
411 
412 static inline uint8_t v9fs_request_cancelled(V9fsPDU *pdu)
413 {
414     return pdu->cancelled;
415 }
416 
417 void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu);
418 void v9fs_path_init(V9fsPath *path);
419 void v9fs_path_free(V9fsPath *path);
420 void v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...);
421 void v9fs_path_copy(V9fsPath *dst, const V9fsPath *src);
422 int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
423                       const char *name, V9fsPath *path);
424 int v9fs_device_realize_common(V9fsState *s, const V9fsTransport *t,
425                                Error **errp);
426 void v9fs_device_unrealize_common(V9fsState *s);
427 
428 V9fsPDU *pdu_alloc(V9fsState *s);
429 void pdu_free(V9fsPDU *pdu);
430 void pdu_submit(V9fsPDU *pdu, P9MsgHeader *hdr);
431 void v9fs_reset(V9fsState *s);
432 
433 struct V9fsTransport {
434     ssize_t     (*pdu_vmarshal)(V9fsPDU *pdu, size_t offset, const char *fmt,
435                                 va_list ap);
436     ssize_t     (*pdu_vunmarshal)(V9fsPDU *pdu, size_t offset, const char *fmt,
437                                   va_list ap);
438     void        (*init_in_iov_from_pdu)(V9fsPDU *pdu, struct iovec **piov,
439                                         unsigned int *pniov, size_t size);
440     void        (*init_out_iov_from_pdu)(V9fsPDU *pdu, struct iovec **piov,
441                                          unsigned int *pniov, size_t size);
442     void        (*push_and_notify)(V9fsPDU *pdu);
443 };
444 
445 #endif
446