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