]> git.proxmox.com Git - mirror_qemu.git/blame - hw/9pfs/9p.c
Drop more @errp parameters after previous commit
[mirror_qemu.git] / hw / 9pfs / 9p.c
CommitLineData
9f107513
AL
1/*
2 * Virtio 9p backend
3 *
4 * Copyright IBM, Corp. 2010
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 */
13
fbc04127 14#include "qemu/osdep.h"
e3e83f2e 15#include <glib/gprintf.h>
0d09e41a 16#include "hw/virtio/virtio.h"
da34e65c 17#include "qapi/error.h"
d49b6836 18#include "qemu/error-report.h"
cd4bfbb2 19#include "qemu/iov.h"
db725815 20#include "qemu/main-loop.h"
1de7afc9 21#include "qemu/sockets.h"
9f107513
AL
22#include "virtio-9p.h"
23#include "fsdev/qemu-fsdev.h"
267ae092 24#include "9p-xattr.h"
fe52840c 25#include "coth.h"
c572f23a 26#include "trace.h"
795c40b8 27#include "migration/blocker.h"
357e2f7f 28#include "sysemu/qtest.h"
1a6ed33c 29#include "qemu/xxhash.h"
6b6aa828 30#include <math.h>
9f107513 31
7a462745
AK
32int open_fd_hw;
33int total_open_fd;
34static int open_fd_rc;
9f107513 35
fac4f111
VJ
36enum {
37 Oread = 0x00,
38 Owrite = 0x01,
39 Ordwr = 0x02,
40 Oexec = 0x03,
41 Oexcl = 0x04,
42 Otrunc = 0x10,
43 Orexec = 0x20,
44 Orclose = 0x40,
45 Oappend = 0x80,
46};
47
75673590 48static ssize_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
0e2082d9
WL
49{
50 ssize_t ret;
51 va_list ap;
52
53 va_start(ap, fmt);
ea83441c 54 ret = pdu->s->transport->pdu_vmarshal(pdu, offset, fmt, ap);
0e2082d9
WL
55 va_end(ap);
56
57 return ret;
58}
59
75673590 60static ssize_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
0e2082d9
WL
61{
62 ssize_t ret;
63 va_list ap;
64
65 va_start(ap, fmt);
ea83441c 66 ret = pdu->s->transport->pdu_vunmarshal(pdu, offset, fmt, ap);
0e2082d9
WL
67 va_end(ap);
68
69 return ret;
70}
71
fac4f111
VJ
72static int omode_to_uflags(int8_t mode)
73{
74 int ret = 0;
75
76 switch (mode & 3) {
77 case Oread:
78 ret = O_RDONLY;
79 break;
80 case Ordwr:
81 ret = O_RDWR;
82 break;
83 case Owrite:
84 ret = O_WRONLY;
85 break;
86 case Oexec:
87 ret = O_RDONLY;
88 break;
89 }
90
91 if (mode & Otrunc) {
92 ret |= O_TRUNC;
93 }
94
95 if (mode & Oappend) {
96 ret |= O_APPEND;
97 }
98
99 if (mode & Oexcl) {
100 ret |= O_EXCL;
101 }
102
103 return ret;
104}
105
8e71b96c 106typedef struct DotlOpenflagMap {
9844081b
MK
107 int dotl_flag;
108 int open_flag;
8e71b96c 109} DotlOpenflagMap;
9844081b
MK
110
111static int dotl_to_open_flags(int flags)
112{
113 int i;
114 /*
115 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
116 * and P9_DOTL_NOACCESS
117 */
118 int oflags = flags & O_ACCMODE;
119
8e71b96c 120 DotlOpenflagMap dotl_oflag_map[] = {
9844081b
MK
121 { P9_DOTL_CREATE, O_CREAT },
122 { P9_DOTL_EXCL, O_EXCL },
123 { P9_DOTL_NOCTTY , O_NOCTTY },
124 { P9_DOTL_TRUNC, O_TRUNC },
125 { P9_DOTL_APPEND, O_APPEND },
126 { P9_DOTL_NONBLOCK, O_NONBLOCK } ,
127 { P9_DOTL_DSYNC, O_DSYNC },
128 { P9_DOTL_FASYNC, FASYNC },
129 { P9_DOTL_DIRECT, O_DIRECT },
130 { P9_DOTL_LARGEFILE, O_LARGEFILE },
131 { P9_DOTL_DIRECTORY, O_DIRECTORY },
132 { P9_DOTL_NOFOLLOW, O_NOFOLLOW },
133 { P9_DOTL_NOATIME, O_NOATIME },
134 { P9_DOTL_SYNC, O_SYNC },
135 };
136
137 for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) {
138 if (flags & dotl_oflag_map[i].dotl_flag) {
139 oflags |= dotl_oflag_map[i].open_flag;
140 }
141 }
142
143 return oflags;
144}
145
758e8e38 146void cred_init(FsCred *credp)
131dcb25 147{
758e8e38
VJ
148 credp->fc_uid = -1;
149 credp->fc_gid = -1;
150 credp->fc_mode = -1;
151 credp->fc_rdev = -1;
131dcb25
AL
152}
153
d3ab98e6
AK
154static int get_dotl_openflags(V9fsState *s, int oflags)
155{
156 int flags;
157 /*
158 * Filter the client open flags
159 */
9844081b
MK
160 flags = dotl_to_open_flags(oflags);
161 flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT);
d3ab98e6
AK
162 /*
163 * Ignore direct disk access hint until the server supports it.
164 */
165 flags &= ~O_DIRECT;
166 return flags;
167}
168
2289be19
AK
169void v9fs_path_init(V9fsPath *path)
170{
171 path->data = NULL;
172 path->size = 0;
173}
174
175void v9fs_path_free(V9fsPath *path)
176{
177 g_free(path->data);
178 path->data = NULL;
179 path->size = 0;
180}
181
e3e83f2e
GK
182
183void GCC_FMT_ATTR(2, 3)
184v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...)
185{
186 va_list ap;
187
188 v9fs_path_free(path);
189
190 va_start(ap, fmt);
191 /* Bump the size for including terminating NULL */
192 path->size = g_vasprintf(&path->data, fmt, ap) + 1;
193 va_end(ap);
194}
195
e446a1eb 196void v9fs_path_copy(V9fsPath *dst, const V9fsPath *src)
2289be19 197{
e446a1eb
MAL
198 v9fs_path_free(dst);
199 dst->size = src->size;
200 dst->data = g_memdup(src->data, src->size);
2289be19
AK
201}
202
203int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
204 const char *name, V9fsPath *path)
205{
206 int err;
207 err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
208 if (err < 0) {
209 err = -errno;
210 }
211 return err;
212}
213
936532a4
MN
214/*
215 * Return TRUE if s1 is an ancestor of s2.
216 *
217 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
218 * As a special case, We treat s1 as ancestor of s2 if they are same!
219 */
2289be19 220static int v9fs_path_is_ancestor(V9fsPath *s1, V9fsPath *s2)
936532a4 221{
2289be19
AK
222 if (!strncmp(s1->data, s2->data, s1->size - 1)) {
223 if (s2->data[s1->size - 1] == '\0' || s2->data[s1->size - 1] == '/') {
936532a4
MN
224 return 1;
225 }
226 }
227 return 0;
228}
229
a03f7874
AL
230static size_t v9fs_string_size(V9fsString *str)
231{
232 return str->size;
233}
234
b9cb88b0
AK
235/*
236 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
8440e22e 237static int coroutine_fn v9fs_reopen_fid(V9fsPDU *pdu, V9fsFidState *f)
b9cb88b0
AK
238{
239 int err = 1;
240 if (f->fid_type == P9_FID_FILE) {
241 if (f->fs.fd == -1) {
242 do {
bccacf6c
AK
243 err = v9fs_co_open(pdu, f, f->open_flags);
244 } while (err == -EINTR && !pdu->cancelled);
b9cb88b0
AK
245 }
246 } else if (f->fid_type == P9_FID_DIR) {
f314ea4e 247 if (f->fs.dir.stream == NULL) {
b9cb88b0 248 do {
bccacf6c
AK
249 err = v9fs_co_opendir(pdu, f);
250 } while (err == -EINTR && !pdu->cancelled);
b9cb88b0
AK
251 }
252 }
253 return err;
254}
255
8440e22e 256static V9fsFidState *coroutine_fn get_fid(V9fsPDU *pdu, int32_t fid)
286d5652 257{
7a462745 258 int err;
286d5652 259 V9fsFidState *f;
bccacf6c 260 V9fsState *s = pdu->s;
286d5652
AL
261
262 for (f = s->fid_list; f; f = f->next) {
84dfb926 263 BUG_ON(f->clunked);
286d5652 264 if (f->fid == fid) {
7a462745
AK
265 /*
266 * Update the fid ref upfront so that
267 * we don't get reclaimed when we yield
268 * in open later.
269 */
84dfb926 270 f->ref++;
7a462745
AK
271 /*
272 * check whether we need to reopen the
273 * file. We might have closed the fd
274 * while trying to free up some file
275 * descriptors.
276 */
bccacf6c 277 err = v9fs_reopen_fid(pdu, f);
b9cb88b0
AK
278 if (err < 0) {
279 f->ref--;
280 return NULL;
281 }
7a462745
AK
282 /*
283 * Mark the fid as referenced so that the LRU
284 * reclaim won't close the file descriptor
285 */
286 f->flags |= FID_REFERENCED;
286d5652
AL
287 return f;
288 }
289 }
286d5652
AL
290 return NULL;
291}
292
293static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
294{
295 V9fsFidState *f;
296
84dfb926
AK
297 for (f = s->fid_list; f; f = f->next) {
298 /* If fid is already there return NULL */
299 BUG_ON(f->clunked);
300 if (f->fid == fid) {
301 return NULL;
302 }
286d5652 303 }
7267c094 304 f = g_malloc0(sizeof(V9fsFidState));
286d5652 305 f->fid = fid;
d62dbb51 306 f->fid_type = P9_FID_NONE;
84dfb926 307 f->ref = 1;
7a462745
AK
308 /*
309 * Mark the fid as referenced so that the LRU
310 * reclaim won't close the file descriptor
311 */
312 f->flags |= FID_REFERENCED;
286d5652
AL
313 f->next = s->fid_list;
314 s->fid_list = f;
315
7cde47d4
GK
316 v9fs_readdir_init(&f->fs.dir);
317 v9fs_readdir_init(&f->fs_reclaim.dir);
318
286d5652
AL
319 return f;
320}
321
8440e22e 322static int coroutine_fn v9fs_xattr_fid_clunk(V9fsPDU *pdu, V9fsFidState *fidp)
10b468bd
AK
323{
324 int retval = 0;
325
dd28fbbc 326 if (fidp->fs.xattr.xattrwalk_fid) {
10b468bd
AK
327 /* getxattr/listxattr fid */
328 goto free_value;
329 }
330 /*
331 * if this is fid for setxattr. clunk should
332 * result in setxattr localcall
333 */
334 if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) {
335 /* clunk after partial write */
336 retval = -EINVAL;
337 goto free_out;
338 }
9ed3ef26 339 if (fidp->fs.xattr.len) {
bccacf6c 340 retval = v9fs_co_lsetxattr(pdu, &fidp->path, &fidp->fs.xattr.name,
9ed3ef26
AK
341 fidp->fs.xattr.value,
342 fidp->fs.xattr.len,
343 fidp->fs.xattr.flags);
344 } else {
bccacf6c 345 retval = v9fs_co_lremovexattr(pdu, &fidp->path, &fidp->fs.xattr.name);
9ed3ef26 346 }
10b468bd
AK
347free_out:
348 v9fs_string_free(&fidp->fs.xattr.name);
349free_value:
9e288406 350 g_free(fidp->fs.xattr.value);
10b468bd
AK
351 return retval;
352}
353
8440e22e 354static int coroutine_fn free_fid(V9fsPDU *pdu, V9fsFidState *fidp)
286d5652 355{
10b468bd 356 int retval = 0;
84dfb926
AK
357
358 if (fidp->fid_type == P9_FID_FILE) {
7a462745
AK
359 /* If we reclaimed the fd no need to close */
360 if (fidp->fs.fd != -1) {
cc720ddb 361 retval = v9fs_co_close(pdu, &fidp->fs);
7a462745 362 }
84dfb926 363 } else if (fidp->fid_type == P9_FID_DIR) {
f314ea4e 364 if (fidp->fs.dir.stream != NULL) {
cc720ddb 365 retval = v9fs_co_closedir(pdu, &fidp->fs);
95f65511 366 }
84dfb926 367 } else if (fidp->fid_type == P9_FID_XATTR) {
bccacf6c 368 retval = v9fs_xattr_fid_clunk(pdu, fidp);
84dfb926 369 }
2289be19 370 v9fs_path_free(&fidp->path);
84dfb926
AK
371 g_free(fidp);
372 return retval;
373}
374
8440e22e 375static int coroutine_fn put_fid(V9fsPDU *pdu, V9fsFidState *fidp)
84dfb926
AK
376{
377 BUG_ON(!fidp->ref);
378 fidp->ref--;
7a462745
AK
379 /*
380 * Don't free the fid if it is in reclaim list
381 */
84dfb926 382 if (!fidp->ref && fidp->clunked) {
e9a0152b
AK
383 if (fidp->fid == pdu->s->root_fid) {
384 /*
385 * if the clunked fid is root fid then we
386 * have unmounted the fs on the client side.
387 * delete the migration blocker. Ideally, this
388 * should be hooked to transport close notification
389 */
390 if (pdu->s->migration_blocker) {
391 migrate_del_blocker(pdu->s->migration_blocker);
392 error_free(pdu->s->migration_blocker);
393 pdu->s->migration_blocker = NULL;
394 }
395 }
a911a182 396 return free_fid(pdu, fidp);
84dfb926 397 }
a911a182 398 return 0;
84dfb926
AK
399}
400
ce421a19 401static V9fsFidState *clunk_fid(V9fsState *s, int32_t fid)
84dfb926 402{
286d5652
AL
403 V9fsFidState **fidpp, *fidp;
404
405 for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
406 if ((*fidpp)->fid == fid) {
407 break;
408 }
409 }
286d5652 410 if (*fidpp == NULL) {
ce421a19 411 return NULL;
286d5652 412 }
286d5652
AL
413 fidp = *fidpp;
414 *fidpp = fidp->next;
84dfb926 415 fidp->clunked = 1;
ce421a19 416 return fidp;
286d5652
AL
417}
418
8440e22e 419void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu)
7a462745
AK
420{
421 int reclaim_count = 0;
bccacf6c 422 V9fsState *s = pdu->s;
7a462745
AK
423 V9fsFidState *f, *reclaim_list = NULL;
424
425 for (f = s->fid_list; f; f = f->next) {
426 /*
427 * Unlink fids cannot be reclaimed. Check
428 * for them and skip them. Also skip fids
429 * currently being operated on.
430 */
431 if (f->ref || f->flags & FID_NON_RECLAIMABLE) {
432 continue;
433 }
434 /*
435 * if it is a recently referenced fid
436 * we leave the fid untouched and clear the
437 * reference bit. We come back to it later
438 * in the next iteration. (a simple LRU without
439 * moving list elements around)
440 */
441 if (f->flags & FID_REFERENCED) {
442 f->flags &= ~FID_REFERENCED;
443 continue;
444 }
445 /*
446 * Add fids to reclaim list.
447 */
448 if (f->fid_type == P9_FID_FILE) {
449 if (f->fs.fd != -1) {
450 /*
451 * Up the reference count so that
452 * a clunk request won't free this fid
453 */
454 f->ref++;
455 f->rclm_lst = reclaim_list;
456 reclaim_list = f;
457 f->fs_reclaim.fd = f->fs.fd;
458 f->fs.fd = -1;
459 reclaim_count++;
460 }
95f65511 461 } else if (f->fid_type == P9_FID_DIR) {
f314ea4e 462 if (f->fs.dir.stream != NULL) {
95f65511
AK
463 /*
464 * Up the reference count so that
465 * a clunk request won't free this fid
466 */
467 f->ref++;
468 f->rclm_lst = reclaim_list;
469 reclaim_list = f;
f314ea4e
GK
470 f->fs_reclaim.dir.stream = f->fs.dir.stream;
471 f->fs.dir.stream = NULL;
95f65511
AK
472 reclaim_count++;
473 }
7a462745
AK
474 }
475 if (reclaim_count >= open_fd_rc) {
476 break;
477 }
478 }
479 /*
480 * Now close the fid in reclaim list. Free them if they
481 * are already clunked.
482 */
483 while (reclaim_list) {
484 f = reclaim_list;
485 reclaim_list = f->rclm_lst;
486 if (f->fid_type == P9_FID_FILE) {
cc720ddb 487 v9fs_co_close(pdu, &f->fs_reclaim);
95f65511 488 } else if (f->fid_type == P9_FID_DIR) {
cc720ddb 489 v9fs_co_closedir(pdu, &f->fs_reclaim);
7a462745
AK
490 }
491 f->rclm_lst = NULL;
492 /*
493 * Now drop the fid reference, free it
494 * if clunked.
495 */
bccacf6c 496 put_fid(pdu, f);
7a462745
AK
497 }
498}
499
8440e22e 500static int coroutine_fn v9fs_mark_fids_unreclaim(V9fsPDU *pdu, V9fsPath *path)
7a462745
AK
501{
502 int err;
bccacf6c 503 V9fsState *s = pdu->s;
7a462745
AK
504 V9fsFidState *fidp, head_fid;
505
506 head_fid.next = s->fid_list;
507 for (fidp = s->fid_list; fidp; fidp = fidp->next) {
2289be19
AK
508 if (fidp->path.size != path->size) {
509 continue;
510 }
511 if (!memcmp(fidp->path.data, path->data, path->size)) {
7a462745
AK
512 /* Mark the fid non reclaimable. */
513 fidp->flags |= FID_NON_RECLAIMABLE;
b9cb88b0
AK
514
515 /* reopen the file/dir if already closed */
bccacf6c 516 err = v9fs_reopen_fid(pdu, fidp);
b9cb88b0 517 if (err < 0) {
267fcadf 518 return err;
b9cb88b0
AK
519 }
520 /*
521 * Go back to head of fid list because
522 * the list could have got updated when
523 * switched to the worker thread
524 */
525 if (err == 0) {
7a462745
AK
526 fidp = &head_fid;
527 }
528 }
529 }
530 return 0;
531}
532
8440e22e 533static void coroutine_fn virtfs_reset(V9fsPDU *pdu)
b41e2992
DS
534{
535 V9fsState *s = pdu->s;
79decce3 536 V9fsFidState *fidp;
b41e2992
DS
537
538 /* Free all fids */
539 while (s->fid_list) {
6d54af0e 540 /* Get fid */
b41e2992 541 fidp = s->fid_list;
6d54af0e
GK
542 fidp->ref++;
543
544 /* Clunk fid */
b41e2992 545 s->fid_list = fidp->next;
6d54af0e 546 fidp->clunked = 1;
b41e2992 547
6d54af0e 548 put_fid(pdu, fidp);
b41e2992 549 }
b41e2992
DS
550}
551
286d5652
AL
552#define P9_QID_TYPE_DIR 0x80
553#define P9_QID_TYPE_SYMLINK 0x02
554
555#define P9_STAT_MODE_DIR 0x80000000
556#define P9_STAT_MODE_APPEND 0x40000000
557#define P9_STAT_MODE_EXCL 0x20000000
558#define P9_STAT_MODE_MOUNT 0x10000000
559#define P9_STAT_MODE_AUTH 0x08000000
560#define P9_STAT_MODE_TMP 0x04000000
561#define P9_STAT_MODE_SYMLINK 0x02000000
562#define P9_STAT_MODE_LINK 0x01000000
563#define P9_STAT_MODE_DEVICE 0x00800000
564#define P9_STAT_MODE_NAMED_PIPE 0x00200000
565#define P9_STAT_MODE_SOCKET 0x00100000
566#define P9_STAT_MODE_SETUID 0x00080000
567#define P9_STAT_MODE_SETGID 0x00040000
568#define P9_STAT_MODE_SETVTX 0x00010000
569
570#define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
571 P9_STAT_MODE_SYMLINK | \
572 P9_STAT_MODE_LINK | \
573 P9_STAT_MODE_DEVICE | \
574 P9_STAT_MODE_NAMED_PIPE | \
575 P9_STAT_MODE_SOCKET)
576
6b6aa828
CS
577/* Mirrors all bits of a byte. So e.g. binary 10100000 would become 00000101. */
578static inline uint8_t mirror8bit(uint8_t byte)
579{
580 return (byte * 0x0202020202ULL & 0x010884422010ULL) % 1023;
581}
582
583/* Same as mirror8bit() just for a 64 bit data type instead for a byte. */
584static inline uint64_t mirror64bit(uint64_t value)
585{
586 return ((uint64_t)mirror8bit(value & 0xff) << 56) |
587 ((uint64_t)mirror8bit((value >> 8) & 0xff) << 48) |
588 ((uint64_t)mirror8bit((value >> 16) & 0xff) << 40) |
589 ((uint64_t)mirror8bit((value >> 24) & 0xff) << 32) |
590 ((uint64_t)mirror8bit((value >> 32) & 0xff) << 24) |
591 ((uint64_t)mirror8bit((value >> 40) & 0xff) << 16) |
592 ((uint64_t)mirror8bit((value >> 48) & 0xff) << 8) |
593 ((uint64_t)mirror8bit((value >> 56) & 0xff));
594}
595
596/**
597 * @brief Parameter k for the Exponential Golomb algorihm to be used.
598 *
599 * The smaller this value, the smaller the minimum bit count for the Exp.
600 * Golomb generated affixes will be (at lowest index) however for the
601 * price of having higher maximum bit count of generated affixes (at highest
602 * index). Likewise increasing this parameter yields in smaller maximum bit
603 * count for the price of having higher minimum bit count.
604 *
605 * In practice that means: a good value for k depends on the expected amount
606 * of devices to be exposed by one export. For a small amount of devices k
607 * should be small, for a large amount of devices k might be increased
608 * instead. The default of k=0 should be fine for most users though.
609 *
610 * @b IMPORTANT: In case this ever becomes a runtime parameter; the value of
611 * k should not change as long as guest is still running! Because that would
612 * cause completely different inode numbers to be generated on guest.
613 */
614#define EXP_GOLOMB_K 0
615
616/**
617 * @brief Exponential Golomb algorithm for arbitrary k (including k=0).
618 *
619 * The Exponential Golomb algorithm generates @b prefixes (@b not suffixes!)
620 * with growing length and with the mathematical property of being
621 * "prefix-free". The latter means the generated prefixes can be prepended
622 * in front of arbitrary numbers and the resulting concatenated numbers are
623 * guaranteed to be always unique.
624 *
625 * This is a minor adjustment to the original Exp. Golomb algorithm in the
626 * sense that lowest allowed index (@param n) starts with 1, not with zero.
627 *
628 * @param n - natural number (or index) of the prefix to be generated
629 * (1, 2, 3, ...)
630 * @param k - parameter k of Exp. Golomb algorithm to be used
631 * (see comment on EXP_GOLOMB_K macro for details about k)
632 */
633static VariLenAffix expGolombEncode(uint64_t n, int k)
634{
635 const uint64_t value = n + (1 << k) - 1;
636 const int bits = (int) log2(value) + 1;
637 return (VariLenAffix) {
638 .type = AffixType_Prefix,
639 .value = value,
640 .bits = bits + MAX((bits - 1 - k), 0)
641 };
642}
643
644/**
645 * @brief Converts a suffix into a prefix, or a prefix into a suffix.
646 *
647 * Simply mirror all bits of the affix value, for the purpose to preserve
648 * respectively the mathematical "prefix-free" or "suffix-free" property
649 * after the conversion.
650 *
651 * If a passed prefix is suitable to create unique numbers, then the
652 * returned suffix is suitable to create unique numbers as well (and vice
653 * versa).
654 */
655static VariLenAffix invertAffix(const VariLenAffix *affix)
656{
657 return (VariLenAffix) {
658 .type =
659 (affix->type == AffixType_Suffix) ?
660 AffixType_Prefix : AffixType_Suffix,
661 .value =
662 mirror64bit(affix->value) >>
663 ((sizeof(affix->value) * 8) - affix->bits),
664 .bits = affix->bits
665 };
666}
667
668/**
669 * @brief Generates suffix numbers with "suffix-free" property.
670 *
671 * This is just a wrapper function on top of the Exp. Golomb algorithm.
672 *
673 * Since the Exp. Golomb algorithm generates prefixes, but we need suffixes,
674 * this function converts the Exp. Golomb prefixes into appropriate suffixes
675 * which are still suitable for generating unique numbers.
676 *
677 * @param n - natural number (or index) of the suffix to be generated
678 * (1, 2, 3, ...)
679 */
680static VariLenAffix affixForIndex(uint64_t index)
681{
682 VariLenAffix prefix;
683 prefix = expGolombEncode(index, EXP_GOLOMB_K);
684 return invertAffix(&prefix); /* convert prefix to suffix */
685}
686
1a6ed33c
AM
687/* creative abuse of tb_hash_func7, which is based on xxhash */
688static uint32_t qpp_hash(QppEntry e)
689{
690 return qemu_xxhash7(e.ino_prefix, e.dev, 0, 0, 0);
691}
692
f3fe4a2d
AM
693static uint32_t qpf_hash(QpfEntry e)
694{
695 return qemu_xxhash7(e.ino, e.dev, 0, 0, 0);
696}
697
6b6aa828
CS
698static bool qpd_cmp_func(const void *obj, const void *userp)
699{
700 const QpdEntry *e1 = obj, *e2 = userp;
701 return e1->dev == e2->dev;
702}
703
704static bool qpp_cmp_func(const void *obj, const void *userp)
1a6ed33c
AM
705{
706 const QppEntry *e1 = obj, *e2 = userp;
707 return e1->dev == e2->dev && e1->ino_prefix == e2->ino_prefix;
708}
709
6b6aa828 710static bool qpf_cmp_func(const void *obj, const void *userp)
f3fe4a2d
AM
711{
712 const QpfEntry *e1 = obj, *e2 = userp;
713 return e1->dev == e2->dev && e1->ino == e2->ino;
714}
715
716static void qp_table_remove(void *p, uint32_t h, void *up)
1a6ed33c
AM
717{
718 g_free(p);
719}
720
f3fe4a2d 721static void qp_table_destroy(struct qht *ht)
1a6ed33c
AM
722{
723 if (!ht || !ht->map) {
724 return;
725 }
f3fe4a2d 726 qht_iter(ht, qp_table_remove, NULL);
1a6ed33c
AM
727 qht_destroy(ht);
728}
729
6b6aa828
CS
730static void qpd_table_init(struct qht *ht)
731{
732 qht_init(ht, qpd_cmp_func, 1, QHT_MODE_AUTO_RESIZE);
733}
734
1a6ed33c
AM
735static void qpp_table_init(struct qht *ht)
736{
6b6aa828 737 qht_init(ht, qpp_cmp_func, 1, QHT_MODE_AUTO_RESIZE);
1a6ed33c
AM
738}
739
f3fe4a2d
AM
740static void qpf_table_init(struct qht *ht)
741{
6b6aa828
CS
742 qht_init(ht, qpf_cmp_func, 1 << 16, QHT_MODE_AUTO_RESIZE);
743}
744
745/*
746 * Returns how many (high end) bits of inode numbers of the passed fs
747 * device shall be used (in combination with the device number) to
748 * generate hash values for qpp_table entries.
749 *
750 * This function is required if variable length suffixes are used for inode
751 * number mapping on guest level. Since a device may end up having multiple
752 * entries in qpp_table, each entry most probably with a different suffix
753 * length, we thus need this function in conjunction with qpd_table to
754 * "agree" about a fix amount of bits (per device) to be always used for
755 * generating hash values for the purpose of accessing qpp_table in order
756 * get consistent behaviour when accessing qpp_table.
757 */
758static int qid_inode_prefix_hash_bits(V9fsPDU *pdu, dev_t dev)
759{
760 QpdEntry lookup = {
761 .dev = dev
762 }, *val;
763 uint32_t hash = dev;
764 VariLenAffix affix;
765
766 val = qht_lookup(&pdu->s->qpd_table, &lookup, hash);
767 if (!val) {
768 val = g_malloc0(sizeof(QpdEntry));
769 *val = lookup;
770 affix = affixForIndex(pdu->s->qp_affix_next);
771 val->prefix_bits = affix.bits;
772 qht_insert(&pdu->s->qpd_table, val, hash, NULL);
773 pdu->s->qp_ndevices++;
774 }
775 return val->prefix_bits;
f3fe4a2d
AM
776}
777
6b6aa828
CS
778/**
779 * @brief Slow / full mapping host inode nr -> guest inode nr.
780 *
781 * This function performs a slower and much more costly remapping of an
782 * original file inode number on host to an appropriate different inode
783 * number on guest. For every (dev, inode) combination on host a new
784 * sequential number is generated, cached and exposed as inode number on
785 * guest.
786 *
787 * This is just a "last resort" fallback solution if the much faster/cheaper
788 * qid_path_suffixmap() failed. In practice this slow / full mapping is not
789 * expected ever to be used at all though.
790 *
791 * @see qid_path_suffixmap() for details
792 *
793 */
f3fe4a2d
AM
794static int qid_path_fullmap(V9fsPDU *pdu, const struct stat *stbuf,
795 uint64_t *path)
796{
797 QpfEntry lookup = {
798 .dev = stbuf->st_dev,
799 .ino = stbuf->st_ino
800 }, *val;
801 uint32_t hash = qpf_hash(lookup);
6b6aa828 802 VariLenAffix affix;
f3fe4a2d
AM
803
804 val = qht_lookup(&pdu->s->qpf_table, &lookup, hash);
805
806 if (!val) {
807 if (pdu->s->qp_fullpath_next == 0) {
808 /* no more files can be mapped :'( */
809 error_report_once(
810 "9p: No more prefixes available for remapping inodes from "
811 "host to guest."
812 );
813 return -ENFILE;
814 }
815
816 val = g_malloc0(sizeof(QppEntry));
817 *val = lookup;
818
819 /* new unique inode and device combo */
6b6aa828
CS
820 affix = affixForIndex(
821 1ULL << (sizeof(pdu->s->qp_affix_next) * 8)
822 );
823 val->path = (pdu->s->qp_fullpath_next++ << affix.bits) | affix.value;
824 pdu->s->qp_fullpath_next &= ((1ULL << (64 - affix.bits)) - 1);
f3fe4a2d
AM
825 qht_insert(&pdu->s->qpf_table, val, hash, NULL);
826 }
827
828 *path = val->path;
829 return 0;
830}
831
6b6aa828
CS
832/**
833 * @brief Quick mapping host inode nr -> guest inode nr.
1a6ed33c 834 *
6b6aa828
CS
835 * This function performs quick remapping of an original file inode number
836 * on host to an appropriate different inode number on guest. This remapping
837 * of inodes is required to avoid inode nr collisions on guest which would
838 * happen if the 9p export contains more than 1 exported file system (or
839 * more than 1 file system data set), because unlike on host level where the
840 * files would have different device nrs, all files exported by 9p would
841 * share the same device nr on guest (the device nr of the virtual 9p device
842 * that is).
843 *
844 * Inode remapping is performed by chopping off high end bits of the original
845 * inode number from host, shifting the result upwards and then assigning a
846 * generated suffix number for the low end bits, where the same suffix number
847 * will be shared by all inodes with the same device id AND the same high end
848 * bits that have been chopped off. That approach utilizes the fact that inode
849 * numbers very likely share the same high end bits (i.e. due to their common
850 * sequential generation by file systems) and hence we only have to generate
851 * and track a very limited amount of suffixes in practice due to that.
852 *
853 * We generate variable size suffixes for that purpose. The 1st generated
854 * suffix will only have 1 bit and hence we only need to chop off 1 bit from
855 * the original inode number. The subsequent suffixes being generated will
856 * grow in (bit) size subsequently, i.e. the 2nd and 3rd suffix being
857 * generated will have 3 bits and hence we have to chop off 3 bits from their
858 * original inodes, and so on. That approach of using variable length suffixes
859 * (i.e. over fixed size ones) utilizes the fact that in practice only a very
860 * limited amount of devices are shared by the same export (e.g. typically
861 * less than 2 dozen devices per 9p export), so in practice we need to chop
862 * off less bits than with fixed size prefixes and yet are flexible to add
863 * new devices at runtime below host's export directory at any time without
864 * having to reboot guest nor requiring to reconfigure guest for that. And due
865 * to the very limited amount of original high end bits that we chop off that
866 * way, the total amount of suffixes we need to generate is less than by using
867 * fixed size prefixes and hence it also improves performance of the inode
868 * remapping algorithm, and finally has the nice side effect that the inode
869 * numbers on guest will be much smaller & human friendly. ;-)
1a6ed33c 870 */
6b6aa828 871static int qid_path_suffixmap(V9fsPDU *pdu, const struct stat *stbuf,
1a6ed33c
AM
872 uint64_t *path)
873{
6b6aa828 874 const int ino_hash_bits = qid_inode_prefix_hash_bits(pdu, stbuf->st_dev);
1a6ed33c
AM
875 QppEntry lookup = {
876 .dev = stbuf->st_dev,
6b6aa828 877 .ino_prefix = (uint16_t) (stbuf->st_ino >> (64 - ino_hash_bits))
1a6ed33c
AM
878 }, *val;
879 uint32_t hash = qpp_hash(lookup);
880
881 val = qht_lookup(&pdu->s->qpp_table, &lookup, hash);
882
883 if (!val) {
6b6aa828
CS
884 if (pdu->s->qp_affix_next == 0) {
885 /* we ran out of affixes */
f3fe4a2d
AM
886 warn_report_once(
887 "9p: Potential degraded performance of inode remapping"
1a6ed33c
AM
888 );
889 return -ENFILE;
890 }
891
892 val = g_malloc0(sizeof(QppEntry));
893 *val = lookup;
894
6b6aa828
CS
895 /* new unique inode affix and device combo */
896 val->qp_affix_index = pdu->s->qp_affix_next++;
897 val->qp_affix = affixForIndex(val->qp_affix_index);
1a6ed33c
AM
898 qht_insert(&pdu->s->qpp_table, val, hash, NULL);
899 }
6b6aa828
CS
900 /* assuming generated affix to be suffix type, not prefix */
901 *path = (stbuf->st_ino << val->qp_affix.bits) | val->qp_affix.value;
1a6ed33c
AM
902 return 0;
903}
904
3b5ee9e8 905static int stat_to_qid(V9fsPDU *pdu, const struct stat *stbuf, V9fsQID *qidp)
286d5652 906{
1a6ed33c 907 int err;
286d5652
AL
908 size_t size;
909
1a6ed33c
AM
910 if (pdu->s->ctx.export_flags & V9FS_REMAP_INODES) {
911 /* map inode+device to qid path (fast path) */
6b6aa828 912 err = qid_path_suffixmap(pdu, stbuf, &qidp->path);
f3fe4a2d
AM
913 if (err == -ENFILE) {
914 /* fast path didn't work, fall back to full map */
915 err = qid_path_fullmap(pdu, stbuf, &qidp->path);
916 }
1a6ed33c
AM
917 if (err) {
918 return err;
919 }
920 } else {
921 if (pdu->s->dev_id != stbuf->st_dev) {
922 if (pdu->s->ctx.export_flags & V9FS_FORBID_MULTIDEVS) {
923 error_report_once(
924 "9p: Multiple devices detected in same VirtFS export. "
925 "Access of guest to additional devices is (partly) "
926 "denied due to virtfs option 'multidevs=forbid' being "
927 "effective."
928 );
929 return -ENODEV;
930 } else {
931 warn_report_once(
932 "9p: Multiple devices detected in same VirtFS export, "
933 "which might lead to file ID collisions and severe "
934 "misbehaviours on guest! You should either use a "
935 "separate export for each device shared from host or "
936 "use virtfs option 'multidevs=remap'!"
937 );
938 }
939 }
940 memset(&qidp->path, 0, sizeof(qidp->path));
941 size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
942 memcpy(&qidp->path, &stbuf->st_ino, size);
3b5ee9e8
AM
943 }
944
286d5652
AL
945 qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
946 qidp->type = 0;
947 if (S_ISDIR(stbuf->st_mode)) {
948 qidp->type |= P9_QID_TYPE_DIR;
949 }
950 if (S_ISLNK(stbuf->st_mode)) {
951 qidp->type |= P9_QID_TYPE_SYMLINK;
952 }
3b5ee9e8
AM
953
954 return 0;
286d5652
AL
955}
956
8440e22e
GK
957static int coroutine_fn fid_to_qid(V9fsPDU *pdu, V9fsFidState *fidp,
958 V9fsQID *qidp)
286d5652
AL
959{
960 struct stat stbuf;
961 int err;
962
bccacf6c 963 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
8c158561 964 if (err < 0) {
286d5652
AL
965 return err;
966 }
3b5ee9e8
AM
967 err = stat_to_qid(pdu, &stbuf, qidp);
968 if (err < 0) {
969 return err;
970 }
286d5652
AL
971 return 0;
972}
973
1a6ed33c
AM
974static int coroutine_fn dirent_to_qid(V9fsPDU *pdu, V9fsFidState *fidp,
975 struct dirent *dent, V9fsQID *qidp)
976{
977 struct stat stbuf;
978 V9fsPath path;
979 int err;
980
981 v9fs_path_init(&path);
982
983 err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path);
984 if (err < 0) {
985 goto out;
986 }
987 err = v9fs_co_lstat(pdu, &path, &stbuf);
988 if (err < 0) {
989 goto out;
990 }
991 err = stat_to_qid(pdu, &stbuf, qidp);
992
993out:
994 v9fs_path_free(&path);
995 return err;
996}
997
4b311c5f 998V9fsPDU *pdu_alloc(V9fsState *s)
9f107513
AL
999{
1000 V9fsPDU *pdu = NULL;
1001
1002 if (!QLIST_EMPTY(&s->free_list)) {
bccacf6c
AK
1003 pdu = QLIST_FIRST(&s->free_list);
1004 QLIST_REMOVE(pdu, next);
1005 QLIST_INSERT_HEAD(&s->active_list, pdu, next);
9f107513
AL
1006 }
1007 return pdu;
1008}
1009
4b311c5f 1010void pdu_free(V9fsPDU *pdu)
9f107513 1011{
6868a420 1012 V9fsState *s = pdu->s;
f74e27bf
GK
1013
1014 g_assert(!pdu->cancelled);
1015 QLIST_REMOVE(pdu, next);
1016 QLIST_INSERT_HEAD(&s->free_list, pdu, next);
9f107513
AL
1017}
1018
8440e22e 1019static void coroutine_fn pdu_complete(V9fsPDU *pdu, ssize_t len)
405a549a
AL
1020{
1021 int8_t id = pdu->id + 1; /* Response */
ad38ce9e 1022 V9fsState *s = pdu->s;
06a37db7 1023 int ret;
405a549a 1024
fc78d5ee
KF
1025 /*
1026 * The 9p spec requires that successfully cancelled pdus receive no reply.
1027 * Sending a reply would confuse clients because they would
1028 * assume that any EINTR is the actual result of the operation,
1029 * rather than a consequence of the cancellation. However, if
1030 * the operation completed (succesfully or with an error other
1031 * than caused be cancellation), we do send out that reply, both
1032 * for efficiency and to avoid confusing the rest of the state machine
1033 * that assumes passing a non-error here will mean a successful
1034 * transmission of the reply.
1035 */
1036 bool discard = pdu->cancelled && len == -EINTR;
1037 if (discard) {
1038 trace_v9fs_rcancel(pdu->tag, pdu->id);
1039 pdu->size = 0;
1040 goto out_notify;
1041 }
1042
405a549a 1043 if (len < 0) {
405a549a 1044 int err = -len;
8f4d1ca5 1045 len = 7;
405a549a 1046
8f4d1ca5
AB
1047 if (s->proto_version != V9FS_PROTO_2000L) {
1048 V9fsString str;
1049
1050 str.data = strerror(err);
1051 str.size = strlen(str.data);
1052
06a37db7
GK
1053 ret = pdu_marshal(pdu, len, "s", &str);
1054 if (ret < 0) {
1055 goto out_notify;
1056 }
1057 len += ret;
8f4d1ca5
AB
1058 id = P9_RERROR;
1059 }
405a549a 1060
06a37db7
GK
1061 ret = pdu_marshal(pdu, len, "d", err);
1062 if (ret < 0) {
1063 goto out_notify;
1064 }
1065 len += ret;
405a549a 1066
8f4d1ca5
AB
1067 if (s->proto_version == V9FS_PROTO_2000L) {
1068 id = P9_RLERROR;
1069 }
7999f7e1 1070 trace_v9fs_rerror(pdu->tag, pdu->id, err); /* Trace ERROR */
405a549a
AL
1071 }
1072
1073 /* fill out the header */
06a37db7
GK
1074 if (pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag) < 0) {
1075 goto out_notify;
1076 }
405a549a
AL
1077
1078 /* keep these in sync */
1079 pdu->size = len;
1080 pdu->id = id;
1081
06a37db7 1082out_notify:
a17d8659 1083 pdu->s->transport->push_and_notify(pdu);
405a549a 1084
bccacf6c 1085 /* Now wakeup anybody waiting in flush for this request */
f74e27bf
GK
1086 if (!qemu_co_queue_next(&pdu->complete)) {
1087 pdu_free(pdu);
1088 }
405a549a
AL
1089}
1090
bb9e3216
AL
1091static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
1092{
1093 mode_t ret;
1094
1095 ret = mode & 0777;
1096 if (mode & P9_STAT_MODE_DIR) {
1097 ret |= S_IFDIR;
1098 }
1099
cf03eb2c
AB
1100 if (mode & P9_STAT_MODE_SYMLINK) {
1101 ret |= S_IFLNK;
1102 }
1103 if (mode & P9_STAT_MODE_SOCKET) {
1104 ret |= S_IFSOCK;
1105 }
1106 if (mode & P9_STAT_MODE_NAMED_PIPE) {
1107 ret |= S_IFIFO;
1108 }
1109 if (mode & P9_STAT_MODE_DEVICE) {
c7e587b7 1110 if (extension->size && extension->data[0] == 'c') {
cf03eb2c
AB
1111 ret |= S_IFCHR;
1112 } else {
1113 ret |= S_IFBLK;
bb9e3216
AL
1114 }
1115 }
1116
1117 if (!(ret&~0777)) {
1118 ret |= S_IFREG;
1119 }
1120
1121 if (mode & P9_STAT_MODE_SETUID) {
1122 ret |= S_ISUID;
1123 }
1124 if (mode & P9_STAT_MODE_SETGID) {
1125 ret |= S_ISGID;
1126 }
1127 if (mode & P9_STAT_MODE_SETVTX) {
1128 ret |= S_ISVTX;
1129 }
1130
1131 return ret;
1132}
1133
1134static int donttouch_stat(V9fsStat *stat)
1135{
1136 if (stat->type == -1 &&
1137 stat->dev == -1 &&
87032833
AM
1138 stat->qid.type == 0xff &&
1139 stat->qid.version == (uint32_t) -1 &&
1140 stat->qid.path == (uint64_t) -1 &&
bb9e3216
AL
1141 stat->mode == -1 &&
1142 stat->atime == -1 &&
1143 stat->mtime == -1 &&
1144 stat->length == -1 &&
1145 !stat->name.size &&
1146 !stat->uid.size &&
1147 !stat->gid.size &&
1148 !stat->muid.size &&
1149 stat->n_uid == -1 &&
1150 stat->n_gid == -1 &&
1151 stat->n_muid == -1) {
1152 return 1;
1153 }
1154
1155 return 0;
1156}
1157
ddca7f86
MK
1158static void v9fs_stat_init(V9fsStat *stat)
1159{
1160 v9fs_string_init(&stat->name);
1161 v9fs_string_init(&stat->uid);
1162 v9fs_string_init(&stat->gid);
1163 v9fs_string_init(&stat->muid);
1164 v9fs_string_init(&stat->extension);
1165}
1166
bb9e3216
AL
1167static void v9fs_stat_free(V9fsStat *stat)
1168{
1169 v9fs_string_free(&stat->name);
1170 v9fs_string_free(&stat->uid);
1171 v9fs_string_free(&stat->gid);
1172 v9fs_string_free(&stat->muid);
1173 v9fs_string_free(&stat->extension);
1174}
1175
1176static uint32_t stat_to_v9mode(const struct stat *stbuf)
1177{
1178 uint32_t mode;
1179
1180 mode = stbuf->st_mode & 0777;
1181 if (S_ISDIR(stbuf->st_mode)) {
1182 mode |= P9_STAT_MODE_DIR;
1183 }
1184
cf03eb2c
AB
1185 if (S_ISLNK(stbuf->st_mode)) {
1186 mode |= P9_STAT_MODE_SYMLINK;
1187 }
bb9e3216 1188
cf03eb2c
AB
1189 if (S_ISSOCK(stbuf->st_mode)) {
1190 mode |= P9_STAT_MODE_SOCKET;
1191 }
bb9e3216 1192
cf03eb2c
AB
1193 if (S_ISFIFO(stbuf->st_mode)) {
1194 mode |= P9_STAT_MODE_NAMED_PIPE;
1195 }
bb9e3216 1196
cf03eb2c
AB
1197 if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
1198 mode |= P9_STAT_MODE_DEVICE;
1199 }
bb9e3216 1200
cf03eb2c
AB
1201 if (stbuf->st_mode & S_ISUID) {
1202 mode |= P9_STAT_MODE_SETUID;
1203 }
bb9e3216 1204
cf03eb2c
AB
1205 if (stbuf->st_mode & S_ISGID) {
1206 mode |= P9_STAT_MODE_SETGID;
1207 }
bb9e3216 1208
cf03eb2c
AB
1209 if (stbuf->st_mode & S_ISVTX) {
1210 mode |= P9_STAT_MODE_SETVTX;
bb9e3216
AL
1211 }
1212
1213 return mode;
1214}
1215
6069537f
JD
1216static int coroutine_fn stat_to_v9stat(V9fsPDU *pdu, V9fsPath *path,
1217 const char *basename,
8440e22e
GK
1218 const struct stat *stbuf,
1219 V9fsStat *v9stat)
bb9e3216
AL
1220{
1221 int err;
bb9e3216
AL
1222
1223 memset(v9stat, 0, sizeof(*v9stat));
1224
3b5ee9e8
AM
1225 err = stat_to_qid(pdu, stbuf, &v9stat->qid);
1226 if (err < 0) {
1227 return err;
1228 }
bb9e3216
AL
1229 v9stat->mode = stat_to_v9mode(stbuf);
1230 v9stat->atime = stbuf->st_atime;
1231 v9stat->mtime = stbuf->st_mtime;
1232 v9stat->length = stbuf->st_size;
1233
abdf0086
GK
1234 v9fs_string_free(&v9stat->uid);
1235 v9fs_string_free(&v9stat->gid);
1236 v9fs_string_free(&v9stat->muid);
bb9e3216 1237
cf03eb2c
AB
1238 v9stat->n_uid = stbuf->st_uid;
1239 v9stat->n_gid = stbuf->st_gid;
1240 v9stat->n_muid = 0;
bb9e3216 1241
abdf0086 1242 v9fs_string_free(&v9stat->extension);
bb9e3216 1243
cf03eb2c 1244 if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
6069537f 1245 err = v9fs_co_readlink(pdu, path, &v9stat->extension);
7a5ca31e 1246 if (err < 0) {
cf03eb2c 1247 return err;
bb9e3216 1248 }
cf03eb2c
AB
1249 } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
1250 v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
1251 S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
1252 major(stbuf->st_rdev), minor(stbuf->st_rdev));
1253 } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
c9ba47dc
SW
1254 v9fs_string_sprintf(&v9stat->extension, "%s %lu",
1255 "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink);
bb9e3216
AL
1256 }
1257
6069537f 1258 v9fs_string_sprintf(&v9stat->name, "%s", basename);
bb9e3216
AL
1259
1260 v9stat->size = 61 +
1261 v9fs_string_size(&v9stat->name) +
1262 v9fs_string_size(&v9stat->uid) +
1263 v9fs_string_size(&v9stat->gid) +
1264 v9fs_string_size(&v9stat->muid) +
1265 v9fs_string_size(&v9stat->extension);
1266 return 0;
1267}
1268
00ede4c2
SK
1269#define P9_STATS_MODE 0x00000001ULL
1270#define P9_STATS_NLINK 0x00000002ULL
1271#define P9_STATS_UID 0x00000004ULL
1272#define P9_STATS_GID 0x00000008ULL
1273#define P9_STATS_RDEV 0x00000010ULL
1274#define P9_STATS_ATIME 0x00000020ULL
1275#define P9_STATS_MTIME 0x00000040ULL
1276#define P9_STATS_CTIME 0x00000080ULL
1277#define P9_STATS_INO 0x00000100ULL
1278#define P9_STATS_SIZE 0x00000200ULL
1279#define P9_STATS_BLOCKS 0x00000400ULL
1280
1281#define P9_STATS_BTIME 0x00000800ULL
1282#define P9_STATS_GEN 0x00001000ULL
1283#define P9_STATS_DATA_VERSION 0x00002000ULL
1284
1285#define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
1286#define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
1287
1288
3b5ee9e8 1289static int stat_to_v9stat_dotl(V9fsPDU *pdu, const struct stat *stbuf,
8db21ce7 1290 V9fsStatDotl *v9lstat)
00ede4c2
SK
1291{
1292 memset(v9lstat, 0, sizeof(*v9lstat));
1293
1294 v9lstat->st_mode = stbuf->st_mode;
1295 v9lstat->st_nlink = stbuf->st_nlink;
1296 v9lstat->st_uid = stbuf->st_uid;
1297 v9lstat->st_gid = stbuf->st_gid;
1298 v9lstat->st_rdev = stbuf->st_rdev;
1299 v9lstat->st_size = stbuf->st_size;
1300 v9lstat->st_blksize = stbuf->st_blksize;
1301 v9lstat->st_blocks = stbuf->st_blocks;
1302 v9lstat->st_atime_sec = stbuf->st_atime;
1303 v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
1304 v9lstat->st_mtime_sec = stbuf->st_mtime;
1305 v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
1306 v9lstat->st_ctime_sec = stbuf->st_ctime;
1307 v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
1308 /* Currently we only support BASIC fields in stat */
1309 v9lstat->st_result_mask = P9_STATS_BASIC;
1310
3b5ee9e8 1311 return stat_to_qid(pdu, stbuf, &v9lstat->qid);
00ede4c2
SK
1312}
1313
1f5a89bf
AL
1314static void print_sg(struct iovec *sg, int cnt)
1315{
1316 int i;
1317
1318 printf("sg[%d]: {", cnt);
1319 for (i = 0; i < cnt; i++) {
1320 if (i) {
1321 printf(", ");
1322 }
1323 printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
1324 }
1325 printf("}\n");
1326}
1327
2289be19
AK
1328/* Will call this only for path name based fid */
1329static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len)
8cf89e00 1330{
2289be19
AK
1331 V9fsPath str;
1332 v9fs_path_init(&str);
1333 v9fs_path_copy(&str, dst);
e3e83f2e 1334 v9fs_path_sprintf(dst, "%s%s", src->data, str.data + len);
2289be19 1335 v9fs_path_free(&str);
8cf89e00
AL
1336}
1337
2c74c2cb
MK
1338static inline bool is_ro_export(FsContext *ctx)
1339{
1340 return ctx->export_flags & V9FS_RDONLY;
1341}
1342
8440e22e 1343static void coroutine_fn v9fs_version(void *opaque)
9f107513 1344{
ddca7f86 1345 ssize_t err;
ff06030f
VJ
1346 V9fsPDU *pdu = opaque;
1347 V9fsState *s = pdu->s;
92c1ad03
AL
1348 V9fsString version;
1349 size_t offset = 7;
1350
ddca7f86
MK
1351 v9fs_string_init(&version);
1352 err = pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
1353 if (err < 0) {
ddca7f86
MK
1354 goto out;
1355 }
c572f23a 1356 trace_v9fs_version(pdu->tag, pdu->id, s->msize, version.data);
92c1ad03 1357
b41e2992
DS
1358 virtfs_reset(pdu);
1359
84151514
MK
1360 if (!strcmp(version.data, "9P2000.u")) {
1361 s->proto_version = V9FS_PROTO_2000U;
1362 } else if (!strcmp(version.data, "9P2000.L")) {
1363 s->proto_version = V9FS_PROTO_2000L;
1364 } else {
92c1ad03 1365 v9fs_string_sprintf(&version, "unknown");
e16453a3
CS
1366 /* skip min. msize check, reporting invalid version has priority */
1367 goto marshal;
9f107513 1368 }
92c1ad03 1369
e16453a3
CS
1370 if (s->msize < P9_MIN_MSIZE) {
1371 err = -EMSGSIZE;
1372 error_report(
1373 "9pfs: Client requested msize < minimum msize ("
1374 stringify(P9_MIN_MSIZE) ") supported by this server."
1375 );
1376 goto out;
1377 }
1378
1379marshal:
ddca7f86
MK
1380 err = pdu_marshal(pdu, offset, "ds", s->msize, &version);
1381 if (err < 0) {
ddca7f86
MK
1382 goto out;
1383 }
403a905b 1384 err += offset;
c572f23a 1385 trace_v9fs_version_return(pdu->tag, pdu->id, s->msize, version.data);
ddca7f86 1386out:
403a905b 1387 pdu_complete(pdu, err);
92c1ad03 1388 v9fs_string_free(&version);
9f107513
AL
1389}
1390
8440e22e 1391static void coroutine_fn v9fs_attach(void *opaque)
9f107513 1392{
ff06030f
VJ
1393 V9fsPDU *pdu = opaque;
1394 V9fsState *s = pdu->s;
955efc47
AL
1395 int32_t fid, afid, n_uname;
1396 V9fsString uname, aname;
1397 V9fsFidState *fidp;
955efc47 1398 size_t offset = 7;
8c158561 1399 V9fsQID qid;
955efc47 1400 ssize_t err;
fe44dc91 1401 Error *local_err = NULL;
955efc47 1402
ddca7f86
MK
1403 v9fs_string_init(&uname);
1404 v9fs_string_init(&aname);
1405 err = pdu_unmarshal(pdu, offset, "ddssd", &fid,
1406 &afid, &uname, &aname, &n_uname);
1407 if (err < 0) {
1408 goto out_nofid;
1409 }
c572f23a 1410 trace_v9fs_attach(pdu->tag, pdu->id, fid, afid, uname.data, aname.data);
955efc47
AL
1411
1412 fidp = alloc_fid(s, fid);
1413 if (fidp == NULL) {
1414 err = -EINVAL;
84dfb926 1415 goto out_nofid;
9f107513 1416 }
955efc47 1417 fidp->uid = n_uname;
bccacf6c 1418 err = v9fs_co_name_to_path(pdu, NULL, "/", &fidp->path);
2289be19
AK
1419 if (err < 0) {
1420 err = -EINVAL;
1421 clunk_fid(s, fid);
1422 goto out;
1423 }
bccacf6c 1424 err = fid_to_qid(pdu, fidp, &qid);
8c158561 1425 if (err < 0) {
955efc47 1426 err = -EINVAL;
84dfb926 1427 clunk_fid(s, fid);
955efc47
AL
1428 goto out;
1429 }
fe44dc91 1430
4cdc0789
AK
1431 /*
1432 * disable migration if we haven't done already.
1433 * attach could get called multiple times for the same export.
1434 */
1435 if (!s->migration_blocker) {
f231b88d
CR
1436 error_setg(&s->migration_blocker,
1437 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1438 s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag);
fe44dc91
AA
1439 err = migrate_add_blocker(s->migration_blocker, &local_err);
1440 if (local_err) {
1441 error_free(local_err);
1442 error_free(s->migration_blocker);
1443 s->migration_blocker = NULL;
1444 clunk_fid(s, fid);
1445 goto out;
1446 }
1447 s->root_fid = fid;
1448 }
1449
1450 err = pdu_marshal(pdu, offset, "Q", &qid);
1451 if (err < 0) {
1452 clunk_fid(s, fid);
1453 goto out;
4cdc0789 1454 }
fe44dc91
AA
1455 err += offset;
1456
1457 memcpy(&s->root_qid, &qid, sizeof(qid));
1458 trace_v9fs_attach_return(pdu->tag, pdu->id,
1459 qid.type, qid.version, qid.path);
955efc47 1460out:
bccacf6c 1461 put_fid(pdu, fidp);
84dfb926 1462out_nofid:
dc295f83 1463 pdu_complete(pdu, err);
955efc47
AL
1464 v9fs_string_free(&uname);
1465 v9fs_string_free(&aname);
9f107513
AL
1466}
1467
8440e22e 1468static void coroutine_fn v9fs_stat(void *opaque)
9f107513 1469{
4da7d3fa 1470 int32_t fid;
d8e0c29e 1471 V9fsStat v9stat;
4da7d3fa 1472 ssize_t err = 0;
d8e0c29e
AK
1473 size_t offset = 7;
1474 struct stat stbuf;
1475 V9fsFidState *fidp;
1476 V9fsPDU *pdu = opaque;
6069537f 1477 char *basename;
4da7d3fa 1478
ddca7f86
MK
1479 err = pdu_unmarshal(pdu, offset, "d", &fid);
1480 if (err < 0) {
1481 goto out_nofid;
1482 }
c572f23a 1483 trace_v9fs_stat(pdu->tag, pdu->id, fid);
84dfb926 1484
bccacf6c 1485 fidp = get_fid(pdu, fid);
d8e0c29e 1486 if (fidp == NULL) {
4da7d3fa 1487 err = -ENOENT;
84dfb926 1488 goto out_nofid;
9f107513 1489 }
bccacf6c 1490 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
d8e0c29e
AK
1491 if (err < 0) {
1492 goto out;
1493 }
6069537f
JD
1494 basename = g_path_get_basename(fidp->path.data);
1495 err = stat_to_v9stat(pdu, &fidp->path, basename, &stbuf, &v9stat);
1496 g_free(basename);
d8e0c29e
AK
1497 if (err < 0) {
1498 goto out;
1499 }
ddca7f86
MK
1500 err = pdu_marshal(pdu, offset, "wS", 0, &v9stat);
1501 if (err < 0) {
1502 v9fs_stat_free(&v9stat);
1503 goto out;
1504 }
7999f7e1
AK
1505 trace_v9fs_stat_return(pdu->tag, pdu->id, v9stat.mode,
1506 v9stat.atime, v9stat.mtime, v9stat.length);
ddca7f86 1507 err += offset;
d8e0c29e 1508 v9fs_stat_free(&v9stat);
4da7d3fa 1509out:
bccacf6c 1510 put_fid(pdu, fidp);
84dfb926 1511out_nofid:
dc295f83 1512 pdu_complete(pdu, err);
9f107513
AL
1513}
1514
8440e22e 1515static void coroutine_fn v9fs_getattr(void *opaque)
00ede4c2
SK
1516{
1517 int32_t fid;
8db21ce7
AK
1518 size_t offset = 7;
1519 ssize_t retval = 0;
1520 struct stat stbuf;
00ede4c2
SK
1521 V9fsFidState *fidp;
1522 uint64_t request_mask;
8db21ce7
AK
1523 V9fsStatDotl v9stat_dotl;
1524 V9fsPDU *pdu = opaque;
00ede4c2 1525
ddca7f86
MK
1526 retval = pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask);
1527 if (retval < 0) {
1528 goto out_nofid;
1529 }
c572f23a 1530 trace_v9fs_getattr(pdu->tag, pdu->id, fid, request_mask);
00ede4c2 1531
bccacf6c 1532 fidp = get_fid(pdu, fid);
00ede4c2 1533 if (fidp == NULL) {
8db21ce7 1534 retval = -ENOENT;
84dfb926 1535 goto out_nofid;
00ede4c2 1536 }
8db21ce7
AK
1537 /*
1538 * Currently we only support BASIC fields in stat, so there is no
00ede4c2
SK
1539 * need to look at request_mask.
1540 */
bccacf6c 1541 retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
8db21ce7
AK
1542 if (retval < 0) {
1543 goto out;
1544 }
3b5ee9e8
AM
1545 retval = stat_to_v9stat_dotl(pdu, &stbuf, &v9stat_dotl);
1546 if (retval < 0) {
1547 goto out;
1548 }
e06a765e
HPB
1549
1550 /* fill st_gen if requested and supported by underlying fs */
1551 if (request_mask & P9_STATS_GEN) {
1552 retval = v9fs_co_st_gen(pdu, &fidp->path, stbuf.st_mode, &v9stat_dotl);
f8b7ee38
KS
1553 switch (retval) {
1554 case 0:
1555 /* we have valid st_gen: update result mask */
1556 v9stat_dotl.st_result_mask |= P9_STATS_GEN;
1557 break;
1558 case -EINTR:
1559 /* request cancelled, e.g. by Tflush */
e06a765e 1560 goto out;
f8b7ee38
KS
1561 default:
1562 /* failed to get st_gen: not fatal, ignore */
1563 break;
e06a765e 1564 }
e06a765e 1565 }
ddca7f86
MK
1566 retval = pdu_marshal(pdu, offset, "A", &v9stat_dotl);
1567 if (retval < 0) {
1568 goto out;
1569 }
1570 retval += offset;
c572f23a
HPB
1571 trace_v9fs_getattr_return(pdu->tag, pdu->id, v9stat_dotl.st_result_mask,
1572 v9stat_dotl.st_mode, v9stat_dotl.st_uid,
1573 v9stat_dotl.st_gid);
7999f7e1
AK
1574out:
1575 put_fid(pdu, fidp);
1576out_nofid:
dc295f83 1577 pdu_complete(pdu, retval);
00ede4c2
SK
1578}
1579
e4027caf
AK
1580/* Attribute flags */
1581#define P9_ATTR_MODE (1 << 0)
1582#define P9_ATTR_UID (1 << 1)
1583#define P9_ATTR_GID (1 << 2)
1584#define P9_ATTR_SIZE (1 << 3)
1585#define P9_ATTR_ATIME (1 << 4)
1586#define P9_ATTR_MTIME (1 << 5)
1587#define P9_ATTR_CTIME (1 << 6)
1588#define P9_ATTR_ATIME_SET (1 << 7)
1589#define P9_ATTR_MTIME_SET (1 << 8)
1590
1591#define P9_ATTR_MASK 127
c79ce737 1592
8440e22e 1593static void coroutine_fn v9fs_setattr(void *opaque)
c79ce737 1594{
65c05f9a
AK
1595 int err = 0;
1596 int32_t fid;
1597 V9fsFidState *fidp;
1598 size_t offset = 7;
1599 V9fsIattr v9iattr;
1600 V9fsPDU *pdu = opaque;
c79ce737 1601
ddca7f86
MK
1602 err = pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr);
1603 if (err < 0) {
1604 goto out_nofid;
1605 }
c79ce737 1606
8f9c64bf
GK
1607 trace_v9fs_setattr(pdu->tag, pdu->id, fid,
1608 v9iattr.valid, v9iattr.mode, v9iattr.uid, v9iattr.gid,
1609 v9iattr.size, v9iattr.atime_sec, v9iattr.mtime_sec);
1610
bccacf6c 1611 fidp = get_fid(pdu, fid);
65c05f9a
AK
1612 if (fidp == NULL) {
1613 err = -EINVAL;
84dfb926 1614 goto out_nofid;
c79ce737 1615 }
e4027caf 1616 if (v9iattr.valid & P9_ATTR_MODE) {
bccacf6c 1617 err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode);
65c05f9a
AK
1618 if (err < 0) {
1619 goto out;
c79ce737 1620 }
c79ce737 1621 }
e4027caf 1622 if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) {
c79ce737 1623 struct timespec times[2];
e4027caf
AK
1624 if (v9iattr.valid & P9_ATTR_ATIME) {
1625 if (v9iattr.valid & P9_ATTR_ATIME_SET) {
65c05f9a
AK
1626 times[0].tv_sec = v9iattr.atime_sec;
1627 times[0].tv_nsec = v9iattr.atime_nsec;
c79ce737
SK
1628 } else {
1629 times[0].tv_nsec = UTIME_NOW;
1630 }
1631 } else {
1632 times[0].tv_nsec = UTIME_OMIT;
1633 }
e4027caf
AK
1634 if (v9iattr.valid & P9_ATTR_MTIME) {
1635 if (v9iattr.valid & P9_ATTR_MTIME_SET) {
65c05f9a
AK
1636 times[1].tv_sec = v9iattr.mtime_sec;
1637 times[1].tv_nsec = v9iattr.mtime_nsec;
c79ce737
SK
1638 } else {
1639 times[1].tv_nsec = UTIME_NOW;
1640 }
1641 } else {
1642 times[1].tv_nsec = UTIME_OMIT;
1643 }
bccacf6c 1644 err = v9fs_co_utimensat(pdu, &fidp->path, times);
65c05f9a
AK
1645 if (err < 0) {
1646 goto out;
1647 }
c79ce737 1648 }
65c05f9a
AK
1649 /*
1650 * If the only valid entry in iattr is ctime we can call
1651 * chown(-1,-1) to update the ctime of the file
1652 */
e4027caf
AK
1653 if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) ||
1654 ((v9iattr.valid & P9_ATTR_CTIME)
1655 && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) {
1656 if (!(v9iattr.valid & P9_ATTR_UID)) {
65c05f9a
AK
1657 v9iattr.uid = -1;
1658 }
e4027caf 1659 if (!(v9iattr.valid & P9_ATTR_GID)) {
65c05f9a
AK
1660 v9iattr.gid = -1;
1661 }
bccacf6c 1662 err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid,
65c05f9a
AK
1663 v9iattr.gid);
1664 if (err < 0) {
1665 goto out;
1666 }
c79ce737 1667 }
e4027caf 1668 if (v9iattr.valid & (P9_ATTR_SIZE)) {
bccacf6c 1669 err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size);
65c05f9a
AK
1670 if (err < 0) {
1671 goto out;
1672 }
c79ce737 1673 }
65c05f9a 1674 err = offset;
8f9c64bf 1675 trace_v9fs_setattr_return(pdu->tag, pdu->id);
c79ce737 1676out:
bccacf6c 1677 put_fid(pdu, fidp);
84dfb926 1678out_nofid:
dc295f83 1679 pdu_complete(pdu, err);
c79ce737
SK
1680}
1681
3cc19c0c 1682static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids)
ff5e54c9
AL
1683{
1684 int i;
ddca7f86 1685 ssize_t err;
3cc19c0c 1686 size_t offset = 7;
ddca7f86
MK
1687
1688 err = pdu_marshal(pdu, offset, "w", nwnames);
1689 if (err < 0) {
1690 return err;
1691 }
1692 offset += err;
3cc19c0c 1693 for (i = 0; i < nwnames; i++) {
ddca7f86
MK
1694 err = pdu_marshal(pdu, offset, "Q", &qids[i]);
1695 if (err < 0) {
1696 return err;
1697 }
1698 offset += err;
ff5e54c9 1699 }
3cc19c0c 1700 return offset;
ff5e54c9
AL
1701}
1702
fff39a7a
GK
1703static bool name_is_illegal(const char *name)
1704{
1705 return !*name || strchr(name, '/') != NULL;
1706}
1707
56f101ec
GK
1708static bool not_same_qid(const V9fsQID *qid1, const V9fsQID *qid2)
1709{
1710 return
1711 qid1->type != qid2->type ||
1712 qid1->version != qid2->version ||
1713 qid1->path != qid2->path;
1714}
1715
8440e22e 1716static void coroutine_fn v9fs_walk(void *opaque)
9f107513 1717{
3cc19c0c
AK
1718 int name_idx;
1719 V9fsQID *qids = NULL;
1720 int i, err = 0;
2289be19 1721 V9fsPath dpath, path;
3cc19c0c
AK
1722 uint16_t nwnames;
1723 struct stat stbuf;
1724 size_t offset = 7;
1725 int32_t fid, newfid;
1726 V9fsString *wnames = NULL;
1727 V9fsFidState *fidp;
3a93113a 1728 V9fsFidState *newfidp = NULL;
ff06030f
VJ
1729 V9fsPDU *pdu = opaque;
1730 V9fsState *s = pdu->s;
56f101ec 1731 V9fsQID qid;
ff5e54c9 1732
ddca7f86
MK
1733 err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames);
1734 if (err < 0) {
dc295f83 1735 pdu_complete(pdu, err);
ddca7f86
MK
1736 return ;
1737 }
1738 offset += err;
ff5e54c9 1739
c572f23a
HPB
1740 trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames);
1741
3cc19c0c 1742 if (nwnames && nwnames <= P9_MAXWELEM) {
1923923b
GK
1743 wnames = g_new0(V9fsString, nwnames);
1744 qids = g_new0(V9fsQID, nwnames);
3cc19c0c 1745 for (i = 0; i < nwnames; i++) {
ddca7f86
MK
1746 err = pdu_unmarshal(pdu, offset, "s", &wnames[i]);
1747 if (err < 0) {
1748 goto out_nofid;
1749 }
fff39a7a
GK
1750 if (name_is_illegal(wnames[i].data)) {
1751 err = -ENOENT;
1752 goto out_nofid;
1753 }
ddca7f86 1754 offset += err;
ff5e54c9 1755 }
3cc19c0c 1756 } else if (nwnames > P9_MAXWELEM) {
4f8dee2d 1757 err = -EINVAL;
84dfb926 1758 goto out_nofid;
ff5e54c9 1759 }
bccacf6c 1760 fidp = get_fid(pdu, fid);
3cc19c0c 1761 if (fidp == NULL) {
ff5e54c9 1762 err = -ENOENT;
84dfb926 1763 goto out_nofid;
ff5e54c9 1764 }
56f101ec 1765
13fd08e6
GK
1766 v9fs_path_init(&dpath);
1767 v9fs_path_init(&path);
1768
56f101ec
GK
1769 err = fid_to_qid(pdu, fidp, &qid);
1770 if (err < 0) {
1771 goto out;
1772 }
1773
2289be19
AK
1774 /*
1775 * Both dpath and path initially poin to fidp.
1776 * Needed to handle request with nwnames == 0
1777 */
1778 v9fs_path_copy(&dpath, &fidp->path);
1779 v9fs_path_copy(&path, &fidp->path);
1780 for (name_idx = 0; name_idx < nwnames; name_idx++) {
56f101ec
GK
1781 if (not_same_qid(&pdu->s->root_qid, &qid) ||
1782 strcmp("..", wnames[name_idx].data)) {
1783 err = v9fs_co_name_to_path(pdu, &dpath, wnames[name_idx].data,
1784 &path);
1785 if (err < 0) {
1786 goto out;
1787 }
1788
1789 err = v9fs_co_lstat(pdu, &path, &stbuf);
1790 if (err < 0) {
1791 goto out;
1792 }
3b5ee9e8
AM
1793 err = stat_to_qid(pdu, &stbuf, &qid);
1794 if (err < 0) {
1795 goto out;
1796 }
56f101ec 1797 v9fs_path_copy(&dpath, &path);
2289be19 1798 }
56f101ec 1799 memcpy(&qids[name_idx], &qid, sizeof(qid));
2289be19 1800 }
ff5e54c9 1801 if (fid == newfid) {
49dd946b
GK
1802 if (fidp->fid_type != P9_FID_NONE) {
1803 err = -EINVAL;
1804 goto out;
1805 }
5b3c77aa 1806 v9fs_path_write_lock(s);
2289be19 1807 v9fs_path_copy(&fidp->path, &path);
5b3c77aa 1808 v9fs_path_unlock(s);
ff5e54c9 1809 } else {
3cc19c0c
AK
1810 newfidp = alloc_fid(s, newfid);
1811 if (newfidp == NULL) {
ff5e54c9
AL
1812 err = -EINVAL;
1813 goto out;
1814 }
3cc19c0c 1815 newfidp->uid = fidp->uid;
2289be19 1816 v9fs_path_copy(&newfidp->path, &path);
9f107513 1817 }
3cc19c0c 1818 err = v9fs_walk_marshal(pdu, nwnames, qids);
7999f7e1 1819 trace_v9fs_walk_return(pdu->tag, pdu->id, nwnames, qids);
ff5e54c9 1820out:
bccacf6c 1821 put_fid(pdu, fidp);
84dfb926 1822 if (newfidp) {
bccacf6c 1823 put_fid(pdu, newfidp);
84dfb926 1824 }
2289be19
AK
1825 v9fs_path_free(&dpath);
1826 v9fs_path_free(&path);
84dfb926 1827out_nofid:
dc295f83 1828 pdu_complete(pdu, err);
3cc19c0c
AK
1829 if (nwnames && nwnames <= P9_MAXWELEM) {
1830 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1831 v9fs_string_free(&wnames[name_idx]);
1832 }
1833 g_free(wnames);
1834 g_free(qids);
1835 }
9f107513
AL
1836}
1837
8440e22e 1838static int32_t coroutine_fn get_iounit(V9fsPDU *pdu, V9fsPath *path)
5e94c103
MK
1839{
1840 struct statfs stbuf;
1841 int32_t iounit = 0;
bccacf6c 1842 V9fsState *s = pdu->s;
5e94c103
MK
1843
1844 /*
1845 * iounit should be multiples of f_bsize (host filesystem block size
1846 * and as well as less than (client msize - P9_IOHDRSZ))
1847 */
bccacf6c 1848 if (!v9fs_co_statfs(pdu, path, &stbuf)) {
68d654da
DS
1849 if (stbuf.f_bsize) {
1850 iounit = stbuf.f_bsize;
1851 iounit *= (s->msize - P9_IOHDRSZ) / stbuf.f_bsize;
1852 }
5e94c103 1853 }
5e94c103
MK
1854 if (!iounit) {
1855 iounit = s->msize - P9_IOHDRSZ;
1856 }
1857 return iounit;
1858}
1859
8440e22e 1860static void coroutine_fn v9fs_open(void *opaque)
5e94c103 1861{
857bc158 1862 int flags;
857bc158
AK
1863 int32_t fid;
1864 int32_t mode;
1865 V9fsQID qid;
7999f7e1 1866 int iounit = 0;
857bc158
AK
1867 ssize_t err = 0;
1868 size_t offset = 7;
1869 struct stat stbuf;
1870 V9fsFidState *fidp;
1871 V9fsPDU *pdu = opaque;
1872 V9fsState *s = pdu->s;
5e94c103 1873
857bc158 1874 if (s->proto_version == V9FS_PROTO_2000L) {
ddca7f86 1875 err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode);
857bc158 1876 } else {
67d6fa53
BH
1877 uint8_t modebyte;
1878 err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte);
1879 mode = modebyte;
ddca7f86
MK
1880 }
1881 if (err < 0) {
1882 goto out_nofid;
857bc158 1883 }
c572f23a
HPB
1884 trace_v9fs_open(pdu->tag, pdu->id, fid, mode);
1885
bccacf6c 1886 fidp = get_fid(pdu, fid);
857bc158
AK
1887 if (fidp == NULL) {
1888 err = -ENOENT;
84dfb926 1889 goto out_nofid;
a6568fe2 1890 }
49dd946b
GK
1891 if (fidp->fid_type != P9_FID_NONE) {
1892 err = -EINVAL;
1893 goto out;
1894 }
a6568fe2 1895
bccacf6c 1896 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
857bc158 1897 if (err < 0) {
a6568fe2
AL
1898 goto out;
1899 }
3b5ee9e8
AM
1900 err = stat_to_qid(pdu, &stbuf, &qid);
1901 if (err < 0) {
1902 goto out;
1903 }
857bc158 1904 if (S_ISDIR(stbuf.st_mode)) {
bccacf6c 1905 err = v9fs_co_opendir(pdu, fidp);
857bc158
AK
1906 if (err < 0) {
1907 goto out;
1908 }
1909 fidp->fid_type = P9_FID_DIR;
ddca7f86
MK
1910 err = pdu_marshal(pdu, offset, "Qd", &qid, 0);
1911 if (err < 0) {
1912 goto out;
1913 }
1914 err += offset;
a6568fe2 1915 } else {
771e9d4c 1916 if (s->proto_version == V9FS_PROTO_2000L) {
d3ab98e6 1917 flags = get_dotl_openflags(s, mode);
771e9d4c 1918 } else {
857bc158 1919 flags = omode_to_uflags(mode);
771e9d4c 1920 }
2c74c2cb
MK
1921 if (is_ro_export(&s->ctx)) {
1922 if (mode & O_WRONLY || mode & O_RDWR ||
1923 mode & O_APPEND || mode & O_TRUNC) {
1924 err = -EROFS;
1925 goto out;
1926 }
2c74c2cb 1927 }
bccacf6c 1928 err = v9fs_co_open(pdu, fidp, flags);
857bc158
AK
1929 if (err < 0) {
1930 goto out;
1931 }
1932 fidp->fid_type = P9_FID_FILE;
7a462745
AK
1933 fidp->open_flags = flags;
1934 if (flags & O_EXCL) {
1935 /*
1936 * We let the host file system do O_EXCL check
1937 * We should not reclaim such fd
1938 */
1939 fidp->flags |= FID_NON_RECLAIMABLE;
1940 }
bccacf6c 1941 iounit = get_iounit(pdu, &fidp->path);
ddca7f86
MK
1942 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1943 if (err < 0) {
1944 goto out;
1945 }
1946 err += offset;
a6568fe2 1947 }
7999f7e1
AK
1948 trace_v9fs_open_return(pdu->tag, pdu->id,
1949 qid.type, qid.version, qid.path, iounit);
a6568fe2 1950out:
bccacf6c 1951 put_fid(pdu, fidp);
84dfb926 1952out_nofid:
dc295f83 1953 pdu_complete(pdu, err);
a6568fe2
AL
1954}
1955
8440e22e 1956static void coroutine_fn v9fs_lcreate(void *opaque)
c1568af5
VJ
1957{
1958 int32_t dfid, flags, mode;
1959 gid_t gid;
c1568af5 1960 ssize_t err = 0;
36f8981f 1961 ssize_t offset = 7;
36f8981f
VJ
1962 V9fsString name;
1963 V9fsFidState *fidp;
1964 struct stat stbuf;
1965 V9fsQID qid;
1966 int32_t iounit;
1967 V9fsPDU *pdu = opaque;
c1568af5 1968
ddca7f86
MK
1969 v9fs_string_init(&name);
1970 err = pdu_unmarshal(pdu, offset, "dsddd", &dfid,
1971 &name, &flags, &mode, &gid);
1972 if (err < 0) {
1973 goto out_nofid;
1974 }
c572f23a 1975 trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid);
c1568af5 1976
fff39a7a
GK
1977 if (name_is_illegal(name.data)) {
1978 err = -ENOENT;
1979 goto out_nofid;
1980 }
1981
805b5d98
GK
1982 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
1983 err = -EEXIST;
1984 goto out_nofid;
1985 }
1986
bccacf6c 1987 fidp = get_fid(pdu, dfid);
36f8981f 1988 if (fidp == NULL) {
c1568af5 1989 err = -ENOENT;
84dfb926 1990 goto out_nofid;
c1568af5 1991 }
d63fb193
LQ
1992 if (fidp->fid_type != P9_FID_NONE) {
1993 err = -EINVAL;
1994 goto out;
1995 }
c1568af5 1996
d3ab98e6 1997 flags = get_dotl_openflags(pdu->s, flags);
bccacf6c 1998 err = v9fs_co_open2(pdu, fidp, &name, gid,
02cb7f3a 1999 flags | O_CREAT, mode, &stbuf);
36f8981f
VJ
2000 if (err < 0) {
2001 goto out;
2002 }
2003 fidp->fid_type = P9_FID_FILE;
7a462745
AK
2004 fidp->open_flags = flags;
2005 if (flags & O_EXCL) {
2006 /*
2007 * We let the host file system do O_EXCL check
2008 * We should not reclaim such fd
2009 */
2010 fidp->flags |= FID_NON_RECLAIMABLE;
2011 }
bccacf6c 2012 iounit = get_iounit(pdu, &fidp->path);
3b5ee9e8
AM
2013 err = stat_to_qid(pdu, &stbuf, &qid);
2014 if (err < 0) {
2015 goto out;
2016 }
ddca7f86
MK
2017 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
2018 if (err < 0) {
2019 goto out;
2020 }
2021 err += offset;
7999f7e1
AK
2022 trace_v9fs_lcreate_return(pdu->tag, pdu->id,
2023 qid.type, qid.version, qid.path, iounit);
c1568af5 2024out:
bccacf6c 2025 put_fid(pdu, fidp);
84dfb926 2026out_nofid:
dc295f83 2027 pdu_complete(pdu, err);
36f8981f 2028 v9fs_string_free(&name);
c1568af5
VJ
2029}
2030
a1bf8b74 2031static void coroutine_fn v9fs_fsync(void *opaque)
b41e95d3 2032{
4e9ad444 2033 int err;
b41e95d3 2034 int32_t fid;
4e9ad444 2035 int datasync;
b41e95d3
VJ
2036 size_t offset = 7;
2037 V9fsFidState *fidp;
4e9ad444 2038 V9fsPDU *pdu = opaque;
b41e95d3 2039
ddca7f86
MK
2040 err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
2041 if (err < 0) {
2042 goto out_nofid;
2043 }
c572f23a
HPB
2044 trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync);
2045
bccacf6c 2046 fidp = get_fid(pdu, fid);
b41e95d3
VJ
2047 if (fidp == NULL) {
2048 err = -ENOENT;
84dfb926 2049 goto out_nofid;
b41e95d3 2050 }
bccacf6c 2051 err = v9fs_co_fsync(pdu, fidp, datasync);
4e9ad444
AK
2052 if (!err) {
2053 err = offset;
2054 }
bccacf6c 2055 put_fid(pdu, fidp);
84dfb926 2056out_nofid:
dc295f83 2057 pdu_complete(pdu, err);
b41e95d3
VJ
2058}
2059
8440e22e 2060static void coroutine_fn v9fs_clunk(void *opaque)
a6568fe2 2061{
c540ee51 2062 int err;
bbd5697b
AL
2063 int32_t fid;
2064 size_t offset = 7;
84dfb926 2065 V9fsFidState *fidp;
c540ee51
AK
2066 V9fsPDU *pdu = opaque;
2067 V9fsState *s = pdu->s;
bbd5697b 2068
ddca7f86
MK
2069 err = pdu_unmarshal(pdu, offset, "d", &fid);
2070 if (err < 0) {
2071 goto out_nofid;
2072 }
c572f23a 2073 trace_v9fs_clunk(pdu->tag, pdu->id, fid);
84dfb926 2074
ce421a19 2075 fidp = clunk_fid(s, fid);
84dfb926
AK
2076 if (fidp == NULL) {
2077 err = -ENOENT;
2078 goto out_nofid;
2079 }
ce421a19
AK
2080 /*
2081 * Bump the ref so that put_fid will
2082 * free the fid.
2083 */
2084 fidp->ref++;
a911a182
AK
2085 err = put_fid(pdu, fidp);
2086 if (!err) {
2087 err = offset;
2088 }
84dfb926 2089out_nofid:
dc295f83 2090 pdu_complete(pdu, err);
9f107513
AL
2091}
2092
bcb8998f
SS
2093/*
2094 * Create a QEMUIOVector for a sub-region of PDU iovecs
2095 *
2096 * @qiov: uninitialized QEMUIOVector
2097 * @skip: number of bytes to skip from beginning of PDU
2098 * @size: number of bytes to include
2099 * @is_write: true - write, false - read
2100 *
2101 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
2102 * with qemu_iovec_destroy().
2103 */
2104static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu,
16724a17 2105 size_t skip, size_t *size,
bcb8998f
SS
2106 bool is_write)
2107{
2108 QEMUIOVector elem;
2109 struct iovec *iov;
2110 unsigned int niov;
16724a17 2111 size_t alloc_size = *size + skip;
bcb8998f 2112
88da0b03 2113 if (is_write) {
16724a17 2114 pdu->s->transport->init_out_iov_from_pdu(pdu, &iov, &niov, alloc_size);
88da0b03 2115 } else {
16724a17
GK
2116 pdu->s->transport->init_in_iov_from_pdu(pdu, &iov, &niov, &alloc_size);
2117 }
2118
2119 if (alloc_size < skip) {
2120 *size = 0;
2121 } else {
2122 *size = alloc_size - skip;
88da0b03 2123 }
bcb8998f
SS
2124
2125 qemu_iovec_init_external(&elem, iov, niov);
2126 qemu_iovec_init(qiov, niov);
16724a17 2127 qemu_iovec_concat(qiov, &elem, skip, *size);
bcb8998f
SS
2128}
2129
2f008a8c
AK
2130static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
2131 uint64_t off, uint32_t max_count)
a9231555 2132{
ddca7f86 2133 ssize_t err;
d208a0e0 2134 size_t offset = 7;
16724a17 2135 size_t read_count;
bcb8998f 2136 QEMUIOVector qiov_full;
a9231555 2137
7e55d65c
LQ
2138 if (fidp->fs.xattr.len < off) {
2139 read_count = 0;
16724a17 2140 } else if (fidp->fs.xattr.len - off < max_count) {
7e55d65c 2141 read_count = fidp->fs.xattr.len - off;
16724a17 2142 } else {
d208a0e0 2143 read_count = max_count;
a9231555 2144 }
ddca7f86
MK
2145 err = pdu_marshal(pdu, offset, "d", read_count);
2146 if (err < 0) {
2147 return err;
2148 }
2149 offset += err;
00588a0a 2150
16724a17 2151 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, &read_count, false);
fa0eb5c5 2152 err = v9fs_pack(qiov_full.iov, qiov_full.niov, 0,
ddca7f86
MK
2153 ((char *)fidp->fs.xattr.value) + off,
2154 read_count);
bcb8998f 2155 qemu_iovec_destroy(&qiov_full);
ddca7f86
MK
2156 if (err < 0) {
2157 return err;
2158 }
2159 offset += err;
d208a0e0 2160 return offset;
a9231555
AL
2161}
2162
8440e22e
GK
2163static int coroutine_fn v9fs_do_readdir_with_stat(V9fsPDU *pdu,
2164 V9fsFidState *fidp,
2165 uint32_t max_count)
a9231555 2166{
2289be19 2167 V9fsPath path;
d208a0e0
AK
2168 V9fsStat v9stat;
2169 int len, err = 0;
2170 int32_t count = 0;
2171 struct stat stbuf;
2172 off_t saved_dir_pos;
635324e8 2173 struct dirent *dent;
a9231555 2174
d208a0e0 2175 /* save the directory position */
bccacf6c 2176 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
d208a0e0
AK
2177 if (saved_dir_pos < 0) {
2178 return saved_dir_pos;
a9231555 2179 }
5f524c1e 2180
d208a0e0 2181 while (1) {
2289be19 2182 v9fs_path_init(&path);
7cde47d4
GK
2183
2184 v9fs_readdir_lock(&fidp->fs.dir);
2185
635324e8
GK
2186 err = v9fs_co_readdir(pdu, fidp, &dent);
2187 if (err || !dent) {
d208a0e0
AK
2188 break;
2189 }
bccacf6c 2190 err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path);
d208a0e0 2191 if (err < 0) {
8762a46d 2192 break;
d208a0e0 2193 }
bccacf6c 2194 err = v9fs_co_lstat(pdu, &path, &stbuf);
2289be19 2195 if (err < 0) {
8762a46d 2196 break;
2289be19 2197 }
6069537f 2198 err = stat_to_v9stat(pdu, &path, dent->d_name, &stbuf, &v9stat);
d208a0e0 2199 if (err < 0) {
8762a46d 2200 break;
a9231555 2201 }
772a7369
JD
2202 if ((count + v9stat.size + 2) > max_count) {
2203 v9fs_readdir_unlock(&fidp->fs.dir);
2204
2205 /* Ran out of buffer. Set dir back to old position and return */
2206 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
2207 v9fs_stat_free(&v9stat);
2208 v9fs_path_free(&path);
2209 return count;
2210 }
2211
d208a0e0
AK
2212 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
2213 len = pdu_marshal(pdu, 11 + count, "S", &v9stat);
7cde47d4
GK
2214
2215 v9fs_readdir_unlock(&fidp->fs.dir);
2216
772a7369 2217 if (len < 0) {
bccacf6c 2218 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
d208a0e0 2219 v9fs_stat_free(&v9stat);
2289be19 2220 v9fs_path_free(&path);
772a7369 2221 return len;
d208a0e0
AK
2222 }
2223 count += len;
2224 v9fs_stat_free(&v9stat);
2289be19 2225 v9fs_path_free(&path);
d208a0e0 2226 saved_dir_pos = dent->d_off;
a9231555 2227 }
8762a46d 2228
7cde47d4
GK
2229 v9fs_readdir_unlock(&fidp->fs.dir);
2230
2289be19 2231 v9fs_path_free(&path);
d208a0e0
AK
2232 if (err < 0) {
2233 return err;
fa32ef88 2234 }
d208a0e0 2235 return count;
fa32ef88
AK
2236}
2237
8440e22e 2238static void coroutine_fn v9fs_read(void *opaque)
9f107513 2239{
a9231555 2240 int32_t fid;
2f008a8c 2241 uint64_t off;
a9231555 2242 ssize_t err = 0;
d208a0e0
AK
2243 int32_t count = 0;
2244 size_t offset = 7;
2f008a8c 2245 uint32_t max_count;
d208a0e0
AK
2246 V9fsFidState *fidp;
2247 V9fsPDU *pdu = opaque;
2248 V9fsState *s = pdu->s;
a9231555 2249
ddca7f86
MK
2250 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count);
2251 if (err < 0) {
2252 goto out_nofid;
2253 }
c572f23a 2254 trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count);
84dfb926 2255
bccacf6c 2256 fidp = get_fid(pdu, fid);
d208a0e0 2257 if (fidp == NULL) {
a9231555 2258 err = -EINVAL;
84dfb926 2259 goto out_nofid;
a9231555 2260 }
d208a0e0 2261 if (fidp->fid_type == P9_FID_DIR) {
a9231555 2262
d208a0e0 2263 if (off == 0) {
bccacf6c 2264 v9fs_co_rewinddir(pdu, fidp);
a9231555 2265 }
bccacf6c 2266 count = v9fs_do_readdir_with_stat(pdu, fidp, max_count);
d208a0e0
AK
2267 if (count < 0) {
2268 err = count;
2269 goto out;
56d15a53 2270 }
ddca7f86
MK
2271 err = pdu_marshal(pdu, offset, "d", count);
2272 if (err < 0) {
2273 goto out;
2274 }
2275 err += offset + count;
d208a0e0 2276 } else if (fidp->fid_type == P9_FID_FILE) {
302a0d3e
SH
2277 QEMUIOVector qiov_full;
2278 QEMUIOVector qiov;
d208a0e0 2279 int32_t len;
16724a17 2280 size_t size = max_count;
d208a0e0 2281
16724a17 2282 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, &size, false);
302a0d3e 2283 qemu_iovec_init(&qiov, qiov_full.niov);
16724a17 2284 max_count = size;
d208a0e0 2285 do {
302a0d3e 2286 qemu_iovec_reset(&qiov);
1b093c48 2287 qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count);
d208a0e0 2288 if (0) {
302a0d3e 2289 print_sg(qiov.iov, qiov.niov);
d208a0e0
AK
2290 }
2291 /* Loop in case of EINTR */
2292 do {
302a0d3e 2293 len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off);
d208a0e0
AK
2294 if (len >= 0) {
2295 off += len;
2296 count += len;
2297 }
bccacf6c 2298 } while (len == -EINTR && !pdu->cancelled);
d208a0e0
AK
2299 if (len < 0) {
2300 /* IO error return the error */
2301 err = len;
e95c9a49 2302 goto out_free_iovec;
d208a0e0 2303 }
d208a0e0 2304 } while (count < max_count && len > 0);
ddca7f86
MK
2305 err = pdu_marshal(pdu, offset, "d", count);
2306 if (err < 0) {
e95c9a49 2307 goto out_free_iovec;
ddca7f86
MK
2308 }
2309 err += offset + count;
e95c9a49 2310out_free_iovec:
302a0d3e
SH
2311 qemu_iovec_destroy(&qiov);
2312 qemu_iovec_destroy(&qiov_full);
d208a0e0
AK
2313 } else if (fidp->fid_type == P9_FID_XATTR) {
2314 err = v9fs_xattr_read(s, pdu, fidp, off, max_count);
a9231555
AL
2315 } else {
2316 err = -EINVAL;
9f107513 2317 }
7999f7e1 2318 trace_v9fs_read_return(pdu->tag, pdu->id, count, err);
a9231555 2319out:
bccacf6c 2320 put_fid(pdu, fidp);
84dfb926 2321out_nofid:
dc295f83 2322 pdu_complete(pdu, err);
9f107513
AL
2323}
2324
5e4eaa79 2325static size_t v9fs_readdir_data_size(V9fsString *name)
c18e2f94 2326{
5e4eaa79
AK
2327 /*
2328 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
2329 * size of type (1) + size of name.size (2) + strlen(name.data)
2330 */
2331 return 24 + v9fs_string_size(name);
c18e2f94
SK
2332}
2333
8440e22e
GK
2334static int coroutine_fn v9fs_do_readdir(V9fsPDU *pdu, V9fsFidState *fidp,
2335 int32_t max_count)
c18e2f94 2336{
c18e2f94 2337 size_t size;
5e4eaa79
AK
2338 V9fsQID qid;
2339 V9fsString name;
2340 int len, err = 0;
2341 int32_t count = 0;
2342 off_t saved_dir_pos;
635324e8 2343 struct dirent *dent;
c18e2f94 2344
5e4eaa79 2345 /* save the directory position */
bccacf6c 2346 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
5e4eaa79
AK
2347 if (saved_dir_pos < 0) {
2348 return saved_dir_pos;
2349 }
5f524c1e 2350
5e4eaa79 2351 while (1) {
7cde47d4
GK
2352 v9fs_readdir_lock(&fidp->fs.dir);
2353
635324e8
GK
2354 err = v9fs_co_readdir(pdu, fidp, &dent);
2355 if (err || !dent) {
5e4eaa79
AK
2356 break;
2357 }
2358 v9fs_string_init(&name);
2359 v9fs_string_sprintf(&name, "%s", dent->d_name);
2360 if ((count + v9fs_readdir_data_size(&name)) > max_count) {
7cde47d4
GK
2361 v9fs_readdir_unlock(&fidp->fs.dir);
2362
c18e2f94 2363 /* Ran out of buffer. Set dir back to old position and return */
bccacf6c 2364 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
5e4eaa79
AK
2365 v9fs_string_free(&name);
2366 return count;
c18e2f94 2367 }
1a6ed33c
AM
2368
2369 if (pdu->s->ctx.export_flags & V9FS_REMAP_INODES) {
2370 /*
2371 * dirent_to_qid() implies expensive stat call for each entry,
2372 * we must do that here though since inode remapping requires
2373 * the device id, which in turn might be different for
2374 * different entries; we cannot make any assumption to avoid
2375 * that here.
2376 */
2377 err = dirent_to_qid(pdu, fidp, dent, &qid);
2378 if (err < 0) {
2379 v9fs_readdir_unlock(&fidp->fs.dir);
2380 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
2381 v9fs_string_free(&name);
2382 return err;
2383 }
2384 } else {
2385 /*
2386 * Fill up just the path field of qid because the client uses
2387 * only that. To fill the entire qid structure we will have
2388 * to stat each dirent found, which is expensive. For the
2389 * latter reason we don't call dirent_to_qid() here. Only drawback
2390 * is that no multi-device export detection of stat_to_qid()
2391 * would be done and provided as error to the user here. But
2392 * user would get that error anyway when accessing those
2393 * files/dirs through other ways.
2394 */
2395 size = MIN(sizeof(dent->d_ino), sizeof(qid.path));
2396 memcpy(&qid.path, &dent->d_ino, size);
2397 /* Fill the other fields with dummy values */
2398 qid.type = 0;
2399 qid.version = 0;
2400 }
c18e2f94 2401
5e4eaa79
AK
2402 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
2403 len = pdu_marshal(pdu, 11 + count, "Qqbs",
2404 &qid, dent->d_off,
2405 dent->d_type, &name);
7cde47d4
GK
2406
2407 v9fs_readdir_unlock(&fidp->fs.dir);
2408
ddca7f86
MK
2409 if (len < 0) {
2410 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
2411 v9fs_string_free(&name);
ddca7f86
MK
2412 return len;
2413 }
5e4eaa79
AK
2414 count += len;
2415 v9fs_string_free(&name);
2416 saved_dir_pos = dent->d_off;
2417 }
7cde47d4
GK
2418
2419 v9fs_readdir_unlock(&fidp->fs.dir);
2420
5e4eaa79
AK
2421 if (err < 0) {
2422 return err;
2423 }
2424 return count;
c18e2f94
SK
2425}
2426
8440e22e 2427static void coroutine_fn v9fs_readdir(void *opaque)
c18e2f94
SK
2428{
2429 int32_t fid;
5e4eaa79
AK
2430 V9fsFidState *fidp;
2431 ssize_t retval = 0;
c18e2f94 2432 size_t offset = 7;
2f008a8c
AK
2433 uint64_t initial_offset;
2434 int32_t count;
2435 uint32_t max_count;
5e4eaa79 2436 V9fsPDU *pdu = opaque;
d36a5c22 2437 V9fsState *s = pdu->s;
c18e2f94 2438
ddca7f86
MK
2439 retval = pdu_unmarshal(pdu, offset, "dqd", &fid,
2440 &initial_offset, &max_count);
2441 if (retval < 0) {
2442 goto out_nofid;
2443 }
c572f23a
HPB
2444 trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count);
2445
d36a5c22
CS
2446 /* Enough space for a R_readdir header: size[4] Rreaddir tag[2] count[4] */
2447 if (max_count > s->msize - 11) {
2448 max_count = s->msize - 11;
2449 warn_report_once(
2450 "9p: bad client: T_readdir with count > msize - 11"
2451 );
2452 }
2453
bccacf6c 2454 fidp = get_fid(pdu, fid);
84dfb926
AK
2455 if (fidp == NULL) {
2456 retval = -EINVAL;
2457 goto out_nofid;
2458 }
f314ea4e 2459 if (!fidp->fs.dir.stream) {
5e4eaa79 2460 retval = -EINVAL;
c18e2f94
SK
2461 goto out;
2462 }
5e4eaa79 2463 if (initial_offset == 0) {
bccacf6c 2464 v9fs_co_rewinddir(pdu, fidp);
c18e2f94 2465 } else {
bccacf6c 2466 v9fs_co_seekdir(pdu, fidp, initial_offset);
c18e2f94 2467 }
bccacf6c 2468 count = v9fs_do_readdir(pdu, fidp, max_count);
5e4eaa79
AK
2469 if (count < 0) {
2470 retval = count;
2471 goto out;
2472 }
ddca7f86
MK
2473 retval = pdu_marshal(pdu, offset, "d", count);
2474 if (retval < 0) {
2475 goto out;
2476 }
2477 retval += count + offset;
7999f7e1 2478 trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval);
c18e2f94 2479out:
bccacf6c 2480 put_fid(pdu, fidp);
84dfb926 2481out_nofid:
dc295f83 2482 pdu_complete(pdu, retval);
c18e2f94
SK
2483}
2484
d7a90491 2485static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
2f008a8c 2486 uint64_t off, uint32_t count,
d7a90491 2487 struct iovec *sg, int cnt)
10b468bd
AK
2488{
2489 int i, to_copy;
2490 ssize_t err = 0;
7e55d65c 2491 uint64_t write_count;
d7a90491 2492 size_t offset = 7;
10b468bd 2493
d7a90491 2494
7e55d65c 2495 if (fidp->fs.xattr.len < off) {
b858e80a 2496 return -ENOSPC;
10b468bd 2497 }
7e55d65c
LQ
2498 write_count = fidp->fs.xattr.len - off;
2499 if (write_count > count) {
2500 write_count = count;
2501 }
ddca7f86
MK
2502 err = pdu_marshal(pdu, offset, "d", write_count);
2503 if (err < 0) {
2504 return err;
2505 }
2506 err += offset;
d7a90491 2507 fidp->fs.xattr.copied_len += write_count;
10b468bd
AK
2508 /*
2509 * Now copy the content from sg list
2510 */
d7a90491
AK
2511 for (i = 0; i < cnt; i++) {
2512 if (write_count > sg[i].iov_len) {
2513 to_copy = sg[i].iov_len;
10b468bd
AK
2514 } else {
2515 to_copy = write_count;
2516 }
d7a90491 2517 memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy);
10b468bd 2518 /* updating vs->off since we are not using below */
d7a90491 2519 off += to_copy;
10b468bd
AK
2520 write_count -= to_copy;
2521 }
b858e80a 2522
d7a90491 2523 return err;
10b468bd
AK
2524}
2525
8440e22e 2526static void coroutine_fn v9fs_write(void *opaque)
9f107513 2527{
d7a90491
AK
2528 ssize_t err;
2529 int32_t fid;
2f008a8c
AK
2530 uint64_t off;
2531 uint32_t count;
d7a90491
AK
2532 int32_t len = 0;
2533 int32_t total = 0;
2534 size_t offset = 7;
16724a17 2535 size_t size;
d7a90491 2536 V9fsFidState *fidp;
ff06030f
VJ
2537 V9fsPDU *pdu = opaque;
2538 V9fsState *s = pdu->s;
302a0d3e
SH
2539 QEMUIOVector qiov_full;
2540 QEMUIOVector qiov;
8449360c 2541
ddca7f86
MK
2542 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count);
2543 if (err < 0) {
dc295f83 2544 pdu_complete(pdu, err);
0289a412 2545 return;
ddca7f86
MK
2546 }
2547 offset += err;
16724a17
GK
2548 size = count;
2549 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, &size, true);
2550 count = size;
302a0d3e 2551 trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov);
84dfb926 2552
bccacf6c 2553 fidp = get_fid(pdu, fid);
d7a90491 2554 if (fidp == NULL) {
8449360c 2555 err = -EINVAL;
84dfb926 2556 goto out_nofid;
9f107513 2557 }
d7a90491
AK
2558 if (fidp->fid_type == P9_FID_FILE) {
2559 if (fidp->fs.fd == -1) {
10b468bd
AK
2560 err = -EINVAL;
2561 goto out;
2562 }
d7a90491 2563 } else if (fidp->fid_type == P9_FID_XATTR) {
10b468bd
AK
2564 /*
2565 * setxattr operation
2566 */
302a0d3e
SH
2567 err = v9fs_xattr_write(s, pdu, fidp, off, count,
2568 qiov_full.iov, qiov_full.niov);
d7a90491 2569 goto out;
10b468bd 2570 } else {
8449360c
AL
2571 err = -EINVAL;
2572 goto out;
2573 }
302a0d3e 2574 qemu_iovec_init(&qiov, qiov_full.niov);
d7a90491 2575 do {
302a0d3e 2576 qemu_iovec_reset(&qiov);
1b093c48 2577 qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total);
d7a90491 2578 if (0) {
302a0d3e 2579 print_sg(qiov.iov, qiov.niov);
56d15a53 2580 }
d7a90491
AK
2581 /* Loop in case of EINTR */
2582 do {
302a0d3e 2583 len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off);
d7a90491
AK
2584 if (len >= 0) {
2585 off += len;
2586 total += len;
2587 }
bccacf6c 2588 } while (len == -EINTR && !pdu->cancelled);
d7a90491
AK
2589 if (len < 0) {
2590 /* IO error return the error */
2591 err = len;
302a0d3e 2592 goto out_qiov;
d7a90491 2593 }
d7a90491 2594 } while (total < count && len > 0);
302a0d3e
SH
2595
2596 offset = 7;
ddca7f86
MK
2597 err = pdu_marshal(pdu, offset, "d", total);
2598 if (err < 0) {
fdfcc9ae 2599 goto out_qiov;
ddca7f86
MK
2600 }
2601 err += offset;
7999f7e1 2602 trace_v9fs_write_return(pdu->tag, pdu->id, total, err);
302a0d3e
SH
2603out_qiov:
2604 qemu_iovec_destroy(&qiov);
8449360c 2605out:
bccacf6c 2606 put_fid(pdu, fidp);
84dfb926 2607out_nofid:
302a0d3e 2608 qemu_iovec_destroy(&qiov_full);
dc295f83 2609 pdu_complete(pdu, err);
9f107513
AL
2610}
2611
8440e22e 2612static void coroutine_fn v9fs_create(void *opaque)
5e94c103 2613{
baaa86d9
VJ
2614 int32_t fid;
2615 int err = 0;
2616 size_t offset = 7;
2617 V9fsFidState *fidp;
2618 V9fsQID qid;
2619 int32_t perm;
2620 int8_t mode;
2289be19 2621 V9fsPath path;
baaa86d9
VJ
2622 struct stat stbuf;
2623 V9fsString name;
2624 V9fsString extension;
baaa86d9
VJ
2625 int iounit;
2626 V9fsPDU *pdu = opaque;
5b3c77aa 2627 V9fsState *s = pdu->s;
c494dd6f 2628
2289be19 2629 v9fs_path_init(&path);
ddca7f86
MK
2630 v9fs_string_init(&name);
2631 v9fs_string_init(&extension);
2632 err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name,
2633 &perm, &mode, &extension);
2634 if (err < 0) {
2635 goto out_nofid;
2636 }
c572f23a
HPB
2637 trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode);
2638
fff39a7a
GK
2639 if (name_is_illegal(name.data)) {
2640 err = -ENOENT;
2641 goto out_nofid;
2642 }
2643
805b5d98
GK
2644 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2645 err = -EEXIST;
2646 goto out_nofid;
2647 }
2648
bccacf6c 2649 fidp = get_fid(pdu, fid);
baaa86d9
VJ
2650 if (fidp == NULL) {
2651 err = -EINVAL;
84dfb926 2652 goto out_nofid;
c494dd6f 2653 }
d63fb193
LQ
2654 if (fidp->fid_type != P9_FID_NONE) {
2655 err = -EINVAL;
2656 goto out;
2657 }
baaa86d9 2658 if (perm & P9_STAT_MODE_DIR) {
bccacf6c 2659 err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777,
02cb7f3a 2660 fidp->uid, -1, &stbuf);
baaa86d9
VJ
2661 if (err < 0) {
2662 goto out;
2663 }
bccacf6c 2664 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2289be19
AK
2665 if (err < 0) {
2666 goto out;
2667 }
5b3c77aa 2668 v9fs_path_write_lock(s);
2289be19 2669 v9fs_path_copy(&fidp->path, &path);
5b3c77aa 2670 v9fs_path_unlock(s);
bccacf6c 2671 err = v9fs_co_opendir(pdu, fidp);
baaa86d9
VJ
2672 if (err < 0) {
2673 goto out;
2674 }
2675 fidp->fid_type = P9_FID_DIR;
2676 } else if (perm & P9_STAT_MODE_SYMLINK) {
bccacf6c 2677 err = v9fs_co_symlink(pdu, fidp, &name,
02cb7f3a 2678 extension.data, -1 , &stbuf);
baaa86d9 2679 if (err < 0) {
baaa86d9
VJ
2680 goto out;
2681 }
bccacf6c 2682 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2289be19
AK
2683 if (err < 0) {
2684 goto out;
2685 }
5b3c77aa 2686 v9fs_path_write_lock(s);
2289be19 2687 v9fs_path_copy(&fidp->path, &path);
5b3c77aa 2688 v9fs_path_unlock(s);
baaa86d9 2689 } else if (perm & P9_STAT_MODE_LINK) {
2289be19 2690 int32_t ofid = atoi(extension.data);
bccacf6c 2691 V9fsFidState *ofidp = get_fid(pdu, ofid);
2289be19 2692 if (ofidp == NULL) {
baaa86d9
VJ
2693 err = -EINVAL;
2694 goto out;
2695 }
bccacf6c
AK
2696 err = v9fs_co_link(pdu, ofidp, fidp, &name);
2697 put_fid(pdu, ofidp);
2289be19
AK
2698 if (err < 0) {
2699 goto out;
2700 }
bccacf6c 2701 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
baaa86d9 2702 if (err < 0) {
2289be19 2703 fidp->fid_type = P9_FID_NONE;
baaa86d9 2704 goto out;
c494dd6f 2705 }
5b3c77aa 2706 v9fs_path_write_lock(s);
2289be19 2707 v9fs_path_copy(&fidp->path, &path);
5b3c77aa 2708 v9fs_path_unlock(s);
bccacf6c 2709 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
02cb7f3a
AK
2710 if (err < 0) {
2711 fidp->fid_type = P9_FID_NONE;
2712 goto out;
2713 }
baaa86d9 2714 } else if (perm & P9_STAT_MODE_DEVICE) {
c494dd6f
AL
2715 char ctype;
2716 uint32_t major, minor;
2717 mode_t nmode = 0;
2718
baaa86d9 2719 if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) {
c494dd6f 2720 err = -errno;
baaa86d9 2721 goto out;
c494dd6f
AL
2722 }
2723
2724 switch (ctype) {
2725 case 'c':
2726 nmode = S_IFCHR;
2727 break;
2728 case 'b':
2729 nmode = S_IFBLK;
2730 break;
2731 default:
2732 err = -EIO;
baaa86d9
VJ
2733 goto out;
2734 }
c1568af5 2735
baaa86d9 2736 nmode |= perm & 0777;
bccacf6c 2737 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
02cb7f3a 2738 makedev(major, minor), nmode, &stbuf);
baaa86d9
VJ
2739 if (err < 0) {
2740 goto out;
2741 }
bccacf6c 2742 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2289be19
AK
2743 if (err < 0) {
2744 goto out;
2745 }
5b3c77aa 2746 v9fs_path_write_lock(s);
2289be19 2747 v9fs_path_copy(&fidp->path, &path);
5b3c77aa 2748 v9fs_path_unlock(s);
baaa86d9 2749 } else if (perm & P9_STAT_MODE_NAMED_PIPE) {
bccacf6c 2750 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
02cb7f3a 2751 0, S_IFIFO | (perm & 0777), &stbuf);
baaa86d9
VJ
2752 if (err < 0) {
2753 goto out;
2754 }
bccacf6c 2755 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2289be19
AK
2756 if (err < 0) {
2757 goto out;
2758 }
5b3c77aa 2759 v9fs_path_write_lock(s);
2289be19 2760 v9fs_path_copy(&fidp->path, &path);
5b3c77aa 2761 v9fs_path_unlock(s);
baaa86d9 2762 } else if (perm & P9_STAT_MODE_SOCKET) {
bccacf6c 2763 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
02cb7f3a 2764 0, S_IFSOCK | (perm & 0777), &stbuf);
baaa86d9
VJ
2765 if (err < 0) {
2766 goto out;
2767 }
bccacf6c 2768 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2289be19
AK
2769 if (err < 0) {
2770 goto out;
2771 }
5b3c77aa 2772 v9fs_path_write_lock(s);
2289be19 2773 v9fs_path_copy(&fidp->path, &path);
5b3c77aa 2774 v9fs_path_unlock(s);
baaa86d9 2775 } else {
bccacf6c 2776 err = v9fs_co_open2(pdu, fidp, &name, -1,
02cb7f3a 2777 omode_to_uflags(mode)|O_CREAT, perm, &stbuf);
baaa86d9
VJ
2778 if (err < 0) {
2779 goto out;
2780 }
2781 fidp->fid_type = P9_FID_FILE;
7a462745
AK
2782 fidp->open_flags = omode_to_uflags(mode);
2783 if (fidp->open_flags & O_EXCL) {
2784 /*
2785 * We let the host file system do O_EXCL check
2786 * We should not reclaim such fd
2787 */
2788 fidp->flags |= FID_NON_RECLAIMABLE;
2789 }
c494dd6f 2790 }
bccacf6c 2791 iounit = get_iounit(pdu, &fidp->path);
3b5ee9e8
AM
2792 err = stat_to_qid(pdu, &stbuf, &qid);
2793 if (err < 0) {
2794 goto out;
2795 }
ddca7f86
MK
2796 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
2797 if (err < 0) {
2798 goto out;
2799 }
2800 err += offset;
7999f7e1
AK
2801 trace_v9fs_create_return(pdu->tag, pdu->id,
2802 qid.type, qid.version, qid.path, iounit);
c494dd6f 2803out:
bccacf6c 2804 put_fid(pdu, fidp);
84dfb926 2805out_nofid:
dc295f83 2806 pdu_complete(pdu, err);
baaa86d9
VJ
2807 v9fs_string_free(&name);
2808 v9fs_string_free(&extension);
2289be19 2809 v9fs_path_free(&path);
9f107513
AL
2810}
2811
8440e22e 2812static void coroutine_fn v9fs_symlink(void *opaque)
08c60fc9 2813{
ff06030f 2814 V9fsPDU *pdu = opaque;
3fa2a8d1
VJ
2815 V9fsString name;
2816 V9fsString symname;
3fa2a8d1
VJ
2817 V9fsFidState *dfidp;
2818 V9fsQID qid;
2819 struct stat stbuf;
08c60fc9 2820 int32_t dfid;
08c60fc9
VJ
2821 int err = 0;
2822 gid_t gid;
3fa2a8d1 2823 size_t offset = 7;
08c60fc9 2824
ddca7f86
MK
2825 v9fs_string_init(&name);
2826 v9fs_string_init(&symname);
2827 err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid);
2828 if (err < 0) {
2829 goto out_nofid;
2830 }
c572f23a 2831 trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid);
08c60fc9 2832
fff39a7a
GK
2833 if (name_is_illegal(name.data)) {
2834 err = -ENOENT;
2835 goto out_nofid;
2836 }
2837
805b5d98
GK
2838 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2839 err = -EEXIST;
2840 goto out_nofid;
2841 }
2842
bccacf6c 2843 dfidp = get_fid(pdu, dfid);
3fa2a8d1 2844 if (dfidp == NULL) {
08c60fc9 2845 err = -EINVAL;
84dfb926 2846 goto out_nofid;
08c60fc9 2847 }
bccacf6c 2848 err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf);
3fa2a8d1
VJ
2849 if (err < 0) {
2850 goto out;
2851 }
3b5ee9e8
AM
2852 err = stat_to_qid(pdu, &stbuf, &qid);
2853 if (err < 0) {
2854 goto out;
2855 }
ddca7f86
MK
2856 err = pdu_marshal(pdu, offset, "Q", &qid);
2857 if (err < 0) {
2858 goto out;
2859 }
2860 err += offset;
7999f7e1
AK
2861 trace_v9fs_symlink_return(pdu->tag, pdu->id,
2862 qid.type, qid.version, qid.path);
08c60fc9 2863out:
bccacf6c 2864 put_fid(pdu, dfidp);
84dfb926 2865out_nofid:
dc295f83 2866 pdu_complete(pdu, err);
3fa2a8d1
VJ
2867 v9fs_string_free(&name);
2868 v9fs_string_free(&symname);
08c60fc9
VJ
2869}
2870
a1bf8b74 2871static void coroutine_fn v9fs_flush(void *opaque)
9f107513 2872{
ddca7f86 2873 ssize_t err;
bccacf6c
AK
2874 int16_t tag;
2875 size_t offset = 7;
d5f2af7b 2876 V9fsPDU *cancel_pdu = NULL;
ff06030f
VJ
2877 V9fsPDU *pdu = opaque;
2878 V9fsState *s = pdu->s;
bccacf6c 2879
ddca7f86
MK
2880 err = pdu_unmarshal(pdu, offset, "w", &tag);
2881 if (err < 0) {
dc295f83 2882 pdu_complete(pdu, err);
ddca7f86
MK
2883 return;
2884 }
c572f23a 2885 trace_v9fs_flush(pdu->tag, pdu->id, tag);
bccacf6c 2886
d5f2af7b 2887 if (pdu->tag == tag) {
3dc6f869 2888 warn_report("the guest sent a self-referencing 9P flush request");
d5f2af7b
GK
2889 } else {
2890 QLIST_FOREACH(cancel_pdu, &s->active_list, next) {
2891 if (cancel_pdu->tag == tag) {
2892 break;
2893 }
bccacf6c
AK
2894 }
2895 }
2896 if (cancel_pdu) {
2897 cancel_pdu->cancelled = 1;
2898 /*
2899 * Wait for pdu to complete.
2900 */
1ace7cea 2901 qemu_co_queue_wait(&cancel_pdu->complete, NULL);
18adde86
GK
2902 if (!qemu_co_queue_next(&cancel_pdu->complete)) {
2903 cancel_pdu->cancelled = 0;
2904 pdu_free(cancel_pdu);
2905 }
bccacf6c 2906 }
dc295f83 2907 pdu_complete(pdu, 7);
9f107513
AL
2908}
2909
8440e22e 2910static void coroutine_fn v9fs_link(void *opaque)
b2c224be 2911{
ff06030f 2912 V9fsPDU *pdu = opaque;
b2c224be
VJ
2913 int32_t dfid, oldfid;
2914 V9fsFidState *dfidp, *oldfidp;
3a93113a 2915 V9fsString name;
b2c224be
VJ
2916 size_t offset = 7;
2917 int err = 0;
2918
ddca7f86
MK
2919 v9fs_string_init(&name);
2920 err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
2921 if (err < 0) {
2922 goto out_nofid;
2923 }
c572f23a 2924 trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data);
b2c224be 2925
fff39a7a
GK
2926 if (name_is_illegal(name.data)) {
2927 err = -ENOENT;
2928 goto out_nofid;
2929 }
2930
805b5d98
GK
2931 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2932 err = -EEXIST;
2933 goto out_nofid;
2934 }
2935
bccacf6c 2936 dfidp = get_fid(pdu, dfid);
b2c224be 2937 if (dfidp == NULL) {
ffd66876 2938 err = -ENOENT;
84dfb926 2939 goto out_nofid;
b2c224be
VJ
2940 }
2941
bccacf6c 2942 oldfidp = get_fid(pdu, oldfid);
b2c224be 2943 if (oldfidp == NULL) {
ffd66876 2944 err = -ENOENT;
b2c224be
VJ
2945 goto out;
2946 }
bccacf6c 2947 err = v9fs_co_link(pdu, oldfidp, dfidp, &name);
ffd66876
VJ
2948 if (!err) {
2949 err = offset;
b2c224be 2950 }
4c158678 2951 put_fid(pdu, oldfidp);
b2c224be 2952out:
bccacf6c 2953 put_fid(pdu, dfidp);
84dfb926 2954out_nofid:
b2c224be 2955 v9fs_string_free(&name);
dc295f83 2956 pdu_complete(pdu, err);
b2c224be
VJ
2957}
2958
532decb7 2959/* Only works with path name based fid */
8440e22e 2960static void coroutine_fn v9fs_remove(void *opaque)
9f107513 2961{
5bae1900 2962 int32_t fid;
5bae1900 2963 int err = 0;
ae1ef571
VJ
2964 size_t offset = 7;
2965 V9fsFidState *fidp;
2966 V9fsPDU *pdu = opaque;
5bae1900 2967
ddca7f86
MK
2968 err = pdu_unmarshal(pdu, offset, "d", &fid);
2969 if (err < 0) {
2970 goto out_nofid;
2971 }
c572f23a 2972 trace_v9fs_remove(pdu->tag, pdu->id, fid);
5bae1900 2973
bccacf6c 2974 fidp = get_fid(pdu, fid);
ae1ef571 2975 if (fidp == NULL) {
5bae1900 2976 err = -EINVAL;
84dfb926 2977 goto out_nofid;
9f107513 2978 }
532decb7 2979 /* if fs driver is not path based, return EOPNOTSUPP */
c98f1d4a 2980 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
532decb7
AK
2981 err = -EOPNOTSUPP;
2982 goto out_err;
2983 }
7a462745
AK
2984 /*
2985 * IF the file is unlinked, we cannot reopen
2986 * the file later. So don't reclaim fd
2987 */
bccacf6c 2988 err = v9fs_mark_fids_unreclaim(pdu, &fidp->path);
7a462745
AK
2989 if (err < 0) {
2990 goto out_err;
2991 }
bccacf6c 2992 err = v9fs_co_remove(pdu, &fidp->path);
ae1ef571
VJ
2993 if (!err) {
2994 err = offset;
2995 }
7a462745 2996out_err:
ae1ef571 2997 /* For TREMOVE we need to clunk the fid even on failed remove */
84dfb926 2998 clunk_fid(pdu->s, fidp->fid);
bccacf6c 2999 put_fid(pdu, fidp);
84dfb926 3000out_nofid:
dc295f83 3001 pdu_complete(pdu, err);
9f107513
AL
3002}
3003
8440e22e 3004static void coroutine_fn v9fs_unlinkat(void *opaque)
7834cf77
AK
3005{
3006 int err = 0;
3007 V9fsString name;
67e87345 3008 int32_t dfid, flags, rflags = 0;
7834cf77 3009 size_t offset = 7;
2289be19 3010 V9fsPath path;
7834cf77
AK
3011 V9fsFidState *dfidp;
3012 V9fsPDU *pdu = opaque;
7834cf77 3013
ddca7f86
MK
3014 v9fs_string_init(&name);
3015 err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags);
3016 if (err < 0) {
3017 goto out_nofid;
3018 }
fff39a7a
GK
3019
3020 if (name_is_illegal(name.data)) {
3021 err = -ENOENT;
3022 goto out_nofid;
3023 }
3024
805b5d98
GK
3025 if (!strcmp(".", name.data)) {
3026 err = -EINVAL;
3027 goto out_nofid;
3028 }
3029
3030 if (!strcmp("..", name.data)) {
3031 err = -ENOTEMPTY;
3032 goto out_nofid;
3033 }
3034
67e87345
KF
3035 if (flags & ~P9_DOTL_AT_REMOVEDIR) {
3036 err = -EINVAL;
3037 goto out_nofid;
3038 }
3039
3040 if (flags & P9_DOTL_AT_REMOVEDIR) {
3041 rflags |= AT_REMOVEDIR;
3042 }
3043
bccacf6c 3044 dfidp = get_fid(pdu, dfid);
7834cf77
AK
3045 if (dfidp == NULL) {
3046 err = -EINVAL;
3047 goto out_nofid;
3048 }
7834cf77
AK
3049 /*
3050 * IF the file is unlinked, we cannot reopen
3051 * the file later. So don't reclaim fd
3052 */
2289be19 3053 v9fs_path_init(&path);
bccacf6c 3054 err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path);
2289be19
AK
3055 if (err < 0) {
3056 goto out_err;
3057 }
bccacf6c 3058 err = v9fs_mark_fids_unreclaim(pdu, &path);
7834cf77
AK
3059 if (err < 0) {
3060 goto out_err;
3061 }
67e87345 3062 err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, rflags);
7834cf77
AK
3063 if (!err) {
3064 err = offset;
3065 }
3066out_err:
bccacf6c 3067 put_fid(pdu, dfidp);
2289be19 3068 v9fs_path_free(&path);
7834cf77 3069out_nofid:
dc295f83 3070 pdu_complete(pdu, err);
7834cf77
AK
3071 v9fs_string_free(&name);
3072}
3073
2289be19
AK
3074
3075/* Only works with path name based fid */
8440e22e
GK
3076static int coroutine_fn v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp,
3077 int32_t newdirfid,
3078 V9fsString *name)
8cf89e00 3079{
c7b4b0b3 3080 int err = 0;
2289be19
AK
3081 V9fsPath new_path;
3082 V9fsFidState *tfidp;
bccacf6c 3083 V9fsState *s = pdu->s;
84dfb926 3084 V9fsFidState *dirfidp = NULL;
8cf89e00 3085
2289be19 3086 v9fs_path_init(&new_path);
930b1e17 3087 if (newdirfid != -1) {
bccacf6c 3088 dirfidp = get_fid(pdu, newdirfid);
c7b4b0b3 3089 if (dirfidp == NULL) {
b858e80a 3090 return -ENOENT;
c7b4b0b3 3091 }
49dd946b
GK
3092 if (fidp->fid_type != P9_FID_NONE) {
3093 err = -EINVAL;
3094 goto out;
3095 }
4fa62005
GK
3096 err = v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path);
3097 if (err < 0) {
3098 goto out;
3099 }
c7b4b0b3 3100 } else {
4d8bc733
JD
3101 char *dir_name = g_path_get_dirname(fidp->path.data);
3102 V9fsPath dir_path;
3103
3104 v9fs_path_init(&dir_path);
3105 v9fs_path_sprintf(&dir_path, "%s", dir_name);
3106 g_free(dir_name);
3107
3108 err = v9fs_co_name_to_path(pdu, &dir_path, name->data, &new_path);
3109 v9fs_path_free(&dir_path);
4fa62005
GK
3110 if (err < 0) {
3111 goto out;
3112 }
c7b4b0b3 3113 }
bccacf6c 3114 err = v9fs_co_rename(pdu, &fidp->path, &new_path);
2289be19
AK
3115 if (err < 0) {
3116 goto out;
3117 }
3118 /*
3119 * Fixup fid's pointing to the old name to
3120 * start pointing to the new name
3121 */
3122 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
3123 if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) {
3124 /* replace the name */
3125 v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data));
8cf89e00
AL
3126 }
3127 }
c7b4b0b3 3128out:
84dfb926 3129 if (dirfidp) {
bccacf6c 3130 put_fid(pdu, dirfidp);
84dfb926 3131 }
2289be19 3132 v9fs_path_free(&new_path);
c7b4b0b3
MK
3133 return err;
3134}
3135
532decb7 3136/* Only works with path name based fid */
8440e22e 3137static void coroutine_fn v9fs_rename(void *opaque)
c7b4b0b3
MK
3138{
3139 int32_t fid;
c7b4b0b3 3140 ssize_t err = 0;
930b1e17
AK
3141 size_t offset = 7;
3142 V9fsString name;
3143 int32_t newdirfid;
3144 V9fsFidState *fidp;
3145 V9fsPDU *pdu = opaque;
3146 V9fsState *s = pdu->s;
c7b4b0b3 3147
ddca7f86
MK
3148 v9fs_string_init(&name);
3149 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name);
3150 if (err < 0) {
3151 goto out_nofid;
3152 }
fff39a7a
GK
3153
3154 if (name_is_illegal(name.data)) {
3155 err = -ENOENT;
3156 goto out_nofid;
3157 }
3158
805b5d98
GK
3159 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3160 err = -EISDIR;
3161 goto out_nofid;
3162 }
3163
bccacf6c 3164 fidp = get_fid(pdu, fid);
930b1e17 3165 if (fidp == NULL) {
c7b4b0b3 3166 err = -ENOENT;
84dfb926 3167 goto out_nofid;
c7b4b0b3 3168 }
49dd946b
GK
3169 if (fidp->fid_type != P9_FID_NONE) {
3170 err = -EINVAL;
3171 goto out;
3172 }
532decb7 3173 /* if fs driver is not path based, return EOPNOTSUPP */
c98f1d4a 3174 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
532decb7
AK
3175 err = -EOPNOTSUPP;
3176 goto out;
3177 }
3178 v9fs_path_write_lock(s);
bccacf6c 3179 err = v9fs_complete_rename(pdu, fidp, newdirfid, &name);
532decb7 3180 v9fs_path_unlock(s);
930b1e17
AK
3181 if (!err) {
3182 err = offset;
3183 }
532decb7 3184out:
bccacf6c 3185 put_fid(pdu, fidp);
84dfb926 3186out_nofid:
dc295f83 3187 pdu_complete(pdu, err);
930b1e17 3188 v9fs_string_free(&name);
c7b4b0b3
MK
3189}
3190
4fa62005
GK
3191static int coroutine_fn v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir,
3192 V9fsString *old_name,
3193 V9fsPath *newdir,
3194 V9fsString *new_name)
2289be19
AK
3195{
3196 V9fsFidState *tfidp;
3197 V9fsPath oldpath, newpath;
bccacf6c 3198 V9fsState *s = pdu->s;
4fa62005 3199 int err;
2289be19
AK
3200
3201 v9fs_path_init(&oldpath);
3202 v9fs_path_init(&newpath);
4fa62005
GK
3203 err = v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath);
3204 if (err < 0) {
3205 goto out;
3206 }
3207 err = v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath);
3208 if (err < 0) {
3209 goto out;
3210 }
2289be19
AK
3211
3212 /*
3213 * Fixup fid's pointing to the old name to
3214 * start pointing to the new name
3215 */
3216 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
3217 if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) {
3218 /* replace the name */
3219 v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data));
3220 }
3221 }
4fa62005 3222out:
2289be19
AK
3223 v9fs_path_free(&oldpath);
3224 v9fs_path_free(&newpath);
4fa62005 3225 return err;
2289be19
AK
3226}
3227
8440e22e
GK
3228static int coroutine_fn v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid,
3229 V9fsString *old_name,
3230 int32_t newdirfid,
3231 V9fsString *new_name)
89bf6593
AK
3232{
3233 int err = 0;
bccacf6c 3234 V9fsState *s = pdu->s;
89bf6593
AK
3235 V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL;
3236
bccacf6c 3237 olddirfidp = get_fid(pdu, olddirfid);
89bf6593
AK
3238 if (olddirfidp == NULL) {
3239 err = -ENOENT;
3240 goto out;
3241 }
89bf6593 3242 if (newdirfid != -1) {
bccacf6c 3243 newdirfidp = get_fid(pdu, newdirfid);
89bf6593
AK
3244 if (newdirfidp == NULL) {
3245 err = -ENOENT;
3246 goto out;
3247 }
89bf6593 3248 } else {
bccacf6c 3249 newdirfidp = get_fid(pdu, olddirfid);
89bf6593
AK
3250 }
3251
bccacf6c 3252 err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name,
2289be19
AK
3253 &newdirfidp->path, new_name);
3254 if (err < 0) {
3255 goto out;
89bf6593 3256 }
c98f1d4a 3257 if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
532decb7 3258 /* Only for path based fid we need to do the below fixup */
4fa62005
GK
3259 err = v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name,
3260 &newdirfidp->path, new_name);
532decb7 3261 }
89bf6593
AK
3262out:
3263 if (olddirfidp) {
bccacf6c 3264 put_fid(pdu, olddirfidp);
89bf6593
AK
3265 }
3266 if (newdirfidp) {
bccacf6c 3267 put_fid(pdu, newdirfidp);
89bf6593 3268 }
89bf6593
AK
3269 return err;
3270}
3271
8440e22e 3272static void coroutine_fn v9fs_renameat(void *opaque)
89bf6593
AK
3273{
3274 ssize_t err = 0;
3275 size_t offset = 7;
3276 V9fsPDU *pdu = opaque;
3277 V9fsState *s = pdu->s;
3278 int32_t olddirfid, newdirfid;
3279 V9fsString old_name, new_name;
3280
ddca7f86
MK
3281 v9fs_string_init(&old_name);
3282 v9fs_string_init(&new_name);
3283 err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid,
3284 &old_name, &newdirfid, &new_name);
3285 if (err < 0) {
3286 goto out_err;
3287 }
89bf6593 3288
fff39a7a
GK
3289 if (name_is_illegal(old_name.data) || name_is_illegal(new_name.data)) {
3290 err = -ENOENT;
3291 goto out_err;
3292 }
3293
805b5d98
GK
3294 if (!strcmp(".", old_name.data) || !strcmp("..", old_name.data) ||
3295 !strcmp(".", new_name.data) || !strcmp("..", new_name.data)) {
3296 err = -EISDIR;
3297 goto out_err;
3298 }
3299
532decb7 3300 v9fs_path_write_lock(s);
bccacf6c
AK
3301 err = v9fs_complete_renameat(pdu, olddirfid,
3302 &old_name, newdirfid, &new_name);
532decb7 3303 v9fs_path_unlock(s);
89bf6593
AK
3304 if (!err) {
3305 err = offset;
3306 }
ddca7f86
MK
3307
3308out_err:
dc295f83 3309 pdu_complete(pdu, err);
89bf6593
AK
3310 v9fs_string_free(&old_name);
3311 v9fs_string_free(&new_name);
3312}
3313
8440e22e 3314static void coroutine_fn v9fs_wstat(void *opaque)
8cf89e00 3315{
b81d685e
AK
3316 int32_t fid;
3317 int err = 0;
3318 int16_t unused;
3319 V9fsStat v9stat;
3320 size_t offset = 7;
3321 struct stat stbuf;
3322 V9fsFidState *fidp;
3323 V9fsPDU *pdu = opaque;
1d203986 3324 V9fsState *s = pdu->s;
8cf89e00 3325
ddca7f86
MK
3326 v9fs_stat_init(&v9stat);
3327 err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat);
3328 if (err < 0) {
3329 goto out_nofid;
3330 }
c572f23a
HPB
3331 trace_v9fs_wstat(pdu->tag, pdu->id, fid,
3332 v9stat.mode, v9stat.atime, v9stat.mtime);
84dfb926 3333
bccacf6c 3334 fidp = get_fid(pdu, fid);
b81d685e
AK
3335 if (fidp == NULL) {
3336 err = -EINVAL;
84dfb926 3337 goto out_nofid;
8cf89e00 3338 }
b81d685e
AK
3339 /* do we need to sync the file? */
3340 if (donttouch_stat(&v9stat)) {
bccacf6c 3341 err = v9fs_co_fsync(pdu, fidp, 0);
8cf89e00
AL
3342 goto out;
3343 }
b81d685e
AK
3344 if (v9stat.mode != -1) {
3345 uint32_t v9_mode;
bccacf6c 3346 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
b81d685e
AK
3347 if (err < 0) {
3348 goto out;
3349 }
3350 v9_mode = stat_to_v9mode(&stbuf);
3351 if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
3352 (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
3353 /* Attempting to change the type */
3354 err = -EIO;
3355 goto out;
3356 }
bccacf6c 3357 err = v9fs_co_chmod(pdu, &fidp->path,
b81d685e
AK
3358 v9mode_to_mode(v9stat.mode,
3359 &v9stat.extension));
3360 if (err < 0) {
3361 goto out;
3362 }
3363 }
3364 if (v9stat.mtime != -1 || v9stat.atime != -1) {
8fc39ae4 3365 struct timespec times[2];
b81d685e
AK
3366 if (v9stat.atime != -1) {
3367 times[0].tv_sec = v9stat.atime;
8fc39ae4
SK
3368 times[0].tv_nsec = 0;
3369 } else {
3370 times[0].tv_nsec = UTIME_OMIT;
3371 }
b81d685e
AK
3372 if (v9stat.mtime != -1) {
3373 times[1].tv_sec = v9stat.mtime;
8fc39ae4
SK
3374 times[1].tv_nsec = 0;
3375 } else {
3376 times[1].tv_nsec = UTIME_OMIT;
3377 }
bccacf6c 3378 err = v9fs_co_utimensat(pdu, &fidp->path, times);
b81d685e
AK
3379 if (err < 0) {
3380 goto out;
8cf89e00
AL
3381 }
3382 }
b81d685e 3383 if (v9stat.n_gid != -1 || v9stat.n_uid != -1) {
bccacf6c 3384 err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid);
b81d685e 3385 if (err < 0) {
8cf89e00 3386 goto out;
b81d685e 3387 }
8cf89e00 3388 }
b81d685e 3389 if (v9stat.name.size != 0) {
1d203986 3390 v9fs_path_write_lock(s);
bccacf6c 3391 err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name);
1d203986 3392 v9fs_path_unlock(s);
b81d685e
AK
3393 if (err < 0) {
3394 goto out;
3395 }
8cf89e00 3396 }
b81d685e 3397 if (v9stat.length != -1) {
bccacf6c 3398 err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length);
b81d685e
AK
3399 if (err < 0) {
3400 goto out;
3401 }
8cf89e00 3402 }
b81d685e 3403 err = offset;
8cf89e00 3404out:
bccacf6c 3405 put_fid(pdu, fidp);
84dfb926 3406out_nofid:
b81d685e 3407 v9fs_stat_free(&v9stat);
dc295f83 3408 pdu_complete(pdu, err);
9f107513
AL
3409}
3410
88a4763e
AK
3411static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf)
3412{
3413 uint32_t f_type;
3414 uint32_t f_bsize;
3415 uint64_t f_blocks;
3416 uint64_t f_bfree;
3417 uint64_t f_bavail;
3418 uint64_t f_files;
3419 uint64_t f_ffree;
3420 uint64_t fsid_val;
3421 uint32_t f_namelen;
3422 size_t offset = 7;
5e94c103
MK
3423 int32_t bsize_factor;
3424
5e94c103
MK
3425 /*
3426 * compute bsize factor based on host file system block size
3427 * and client msize
3428 */
88a4763e 3429 bsize_factor = (s->msize - P9_IOHDRSZ)/stbuf->f_bsize;
5e94c103
MK
3430 if (!bsize_factor) {
3431 bsize_factor = 1;
3432 }
88a4763e
AK
3433 f_type = stbuf->f_type;
3434 f_bsize = stbuf->f_bsize;
3435 f_bsize *= bsize_factor;
5e94c103
MK
3436 /*
3437 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
3438 * adjust(divide) the number of blocks, free blocks and available
3439 * blocks by bsize factor
3440 */
88a4763e
AK
3441 f_blocks = stbuf->f_blocks/bsize_factor;
3442 f_bfree = stbuf->f_bfree/bsize_factor;
3443 f_bavail = stbuf->f_bavail/bsize_factor;
3444 f_files = stbuf->f_files;
3445 f_ffree = stbuf->f_ffree;
3446 fsid_val = (unsigned int) stbuf->f_fsid.__val[0] |
3447 (unsigned long long)stbuf->f_fsid.__val[1] << 32;
3448 f_namelen = stbuf->f_namelen;
be940c87 3449
88a4763e
AK
3450 return pdu_marshal(pdu, offset, "ddqqqqqqd",
3451 f_type, f_bsize, f_blocks, f_bfree,
3452 f_bavail, f_files, f_ffree,
3453 fsid_val, f_namelen);
be940c87
MK
3454}
3455
8440e22e 3456static void coroutine_fn v9fs_statfs(void *opaque)
be940c87 3457{
88a4763e
AK
3458 int32_t fid;
3459 ssize_t retval = 0;
3460 size_t offset = 7;
3461 V9fsFidState *fidp;
3462 struct statfs stbuf;
ff06030f
VJ
3463 V9fsPDU *pdu = opaque;
3464 V9fsState *s = pdu->s;
be940c87 3465
ddca7f86
MK
3466 retval = pdu_unmarshal(pdu, offset, "d", &fid);
3467 if (retval < 0) {
3468 goto out_nofid;
3469 }
bccacf6c 3470 fidp = get_fid(pdu, fid);
88a4763e
AK
3471 if (fidp == NULL) {
3472 retval = -ENOENT;
84dfb926 3473 goto out_nofid;
be940c87 3474 }
bccacf6c 3475 retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf);
88a4763e
AK
3476 if (retval < 0) {
3477 goto out;
3478 }
ddca7f86
MK
3479 retval = v9fs_fill_statfs(s, pdu, &stbuf);
3480 if (retval < 0) {
3481 goto out;
3482 }
3483 retval += offset;
be940c87 3484out:
bccacf6c 3485 put_fid(pdu, fidp);
84dfb926 3486out_nofid:
dc295f83 3487 pdu_complete(pdu, retval);
be940c87
MK
3488}
3489
8440e22e 3490static void coroutine_fn v9fs_mknod(void *opaque)
5268cecc 3491{
1b733fed
AK
3492
3493 int mode;
3494 gid_t gid;
5268cecc 3495 int32_t fid;
1b733fed 3496 V9fsQID qid;
5268cecc 3497 int err = 0;
5268cecc 3498 int major, minor;
1b733fed
AK
3499 size_t offset = 7;
3500 V9fsString name;
3501 struct stat stbuf;
1b733fed
AK
3502 V9fsFidState *fidp;
3503 V9fsPDU *pdu = opaque;
5268cecc 3504
ddca7f86
MK
3505 v9fs_string_init(&name);
3506 err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode,
3507 &major, &minor, &gid);
3508 if (err < 0) {
3509 goto out_nofid;
3510 }
c572f23a 3511 trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor);
5268cecc 3512
fff39a7a
GK
3513 if (name_is_illegal(name.data)) {
3514 err = -ENOENT;
3515 goto out_nofid;
3516 }
3517
805b5d98
GK
3518 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3519 err = -EEXIST;
3520 goto out_nofid;
3521 }
3522
bccacf6c 3523 fidp = get_fid(pdu, fid);
5268cecc
MK
3524 if (fidp == NULL) {
3525 err = -ENOENT;
84dfb926 3526 goto out_nofid;
5268cecc 3527 }
bccacf6c 3528 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid,
02cb7f3a 3529 makedev(major, minor), mode, &stbuf);
1b733fed
AK
3530 if (err < 0) {
3531 goto out;
3532 }
3b5ee9e8
AM
3533 err = stat_to_qid(pdu, &stbuf, &qid);
3534 if (err < 0) {
3535 goto out;
3536 }
ddca7f86
MK
3537 err = pdu_marshal(pdu, offset, "Q", &qid);
3538 if (err < 0) {
3539 goto out;
3540 }
3541 err += offset;
7999f7e1
AK
3542 trace_v9fs_mknod_return(pdu->tag, pdu->id,
3543 qid.type, qid.version, qid.path);
5268cecc 3544out:
bccacf6c 3545 put_fid(pdu, fidp);
84dfb926 3546out_nofid:
dc295f83 3547 pdu_complete(pdu, err);
1b733fed 3548 v9fs_string_free(&name);
5268cecc
MK
3549}
3550
82cc3ee8
MK
3551/*
3552 * Implement posix byte range locking code
3553 * Server side handling of locking code is very simple, because 9p server in
3554 * QEMU can handle only one client. And most of the lock handling
3555 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
3556 * do any thing in * qemu 9p server side lock code path.
3557 * So when a TLOCK request comes, always return success
3558 */
8440e22e 3559static void coroutine_fn v9fs_lock(void *opaque)
82cc3ee8 3560{
ddca7f86 3561 V9fsFlock flock;
0c27bf2a
AK
3562 size_t offset = 7;
3563 struct stat stbuf;
3564 V9fsFidState *fidp;
3565 int32_t fid, err = 0;
ff06030f 3566 V9fsPDU *pdu = opaque;
82cc3ee8 3567
ddca7f86
MK
3568 v9fs_string_init(&flock.client_id);
3569 err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type,
3570 &flock.flags, &flock.start, &flock.length,
3571 &flock.proc_id, &flock.client_id);
3572 if (err < 0) {
3573 goto out_nofid;
3574 }
c572f23a 3575 trace_v9fs_lock(pdu->tag, pdu->id, fid,
ddca7f86 3576 flock.type, flock.start, flock.length);
c572f23a 3577
82cc3ee8
MK
3578
3579 /* We support only block flag now (that too ignored currently) */
ddca7f86 3580 if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) {
82cc3ee8 3581 err = -EINVAL;
84dfb926 3582 goto out_nofid;
82cc3ee8 3583 }
bccacf6c 3584 fidp = get_fid(pdu, fid);
0c27bf2a 3585 if (fidp == NULL) {
82cc3ee8 3586 err = -ENOENT;
84dfb926 3587 goto out_nofid;
82cc3ee8 3588 }
cc720ddb 3589 err = v9fs_co_fstat(pdu, fidp, &stbuf);
82cc3ee8 3590 if (err < 0) {
82cc3ee8
MK
3591 goto out;
3592 }
4bae2b39
PB
3593 err = pdu_marshal(pdu, offset, "b", P9_LOCK_SUCCESS);
3594 if (err < 0) {
3595 goto out;
3596 }
3597 err += offset;
3598 trace_v9fs_lock_return(pdu->tag, pdu->id, P9_LOCK_SUCCESS);
82cc3ee8 3599out:
bccacf6c 3600 put_fid(pdu, fidp);
84dfb926 3601out_nofid:
dc295f83 3602 pdu_complete(pdu, err);
ddca7f86 3603 v9fs_string_free(&flock.client_id);
82cc3ee8
MK
3604}
3605
8f354003
MK
3606/*
3607 * When a TGETLOCK request comes, always return success because all lock
3608 * handling is done by client's VFS layer.
3609 */
8440e22e 3610static void coroutine_fn v9fs_getlock(void *opaque)
8f354003 3611{
e4e414a4
AK
3612 size_t offset = 7;
3613 struct stat stbuf;
3614 V9fsFidState *fidp;
ddca7f86 3615 V9fsGetlock glock;
e4e414a4 3616 int32_t fid, err = 0;
ff06030f 3617 V9fsPDU *pdu = opaque;
8f354003 3618
ddca7f86
MK
3619 v9fs_string_init(&glock.client_id);
3620 err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type,
3621 &glock.start, &glock.length, &glock.proc_id,
3622 &glock.client_id);
3623 if (err < 0) {
3624 goto out_nofid;
3625 }
c572f23a 3626 trace_v9fs_getlock(pdu->tag, pdu->id, fid,
ddca7f86 3627 glock.type, glock.start, glock.length);
c572f23a 3628
bccacf6c 3629 fidp = get_fid(pdu, fid);
e4e414a4 3630 if (fidp == NULL) {
8f354003 3631 err = -ENOENT;
84dfb926 3632 goto out_nofid;
8f354003 3633 }
cc720ddb 3634 err = v9fs_co_fstat(pdu, fidp, &stbuf);
8f354003 3635 if (err < 0) {
8f354003
MK
3636 goto out;
3637 }
ddca7f86
MK
3638 glock.type = P9_LOCK_TYPE_UNLCK;
3639 err = pdu_marshal(pdu, offset, "bqqds", glock.type,
3640 glock.start, glock.length, glock.proc_id,
3641 &glock.client_id);
3642 if (err < 0) {
3643 goto out;
3644 }
3645 err += offset;
3646 trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start,
3647 glock.length, glock.proc_id);
8f354003 3648out:
bccacf6c 3649 put_fid(pdu, fidp);
84dfb926 3650out_nofid:
dc295f83 3651 pdu_complete(pdu, err);
ddca7f86 3652 v9fs_string_free(&glock.client_id);
8f354003
MK
3653}
3654
8440e22e 3655static void coroutine_fn v9fs_mkdir(void *opaque)
b67592ea 3656{
ff06030f 3657 V9fsPDU *pdu = opaque;
e84861f7 3658 size_t offset = 7;
b67592ea 3659 int32_t fid;
e84861f7 3660 struct stat stbuf;
e84861f7 3661 V9fsQID qid;
02cb7f3a 3662 V9fsString name;
b67592ea
MK
3663 V9fsFidState *fidp;
3664 gid_t gid;
3665 int mode;
e84861f7 3666 int err = 0;
b67592ea 3667
ddca7f86
MK
3668 v9fs_string_init(&name);
3669 err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid);
3670 if (err < 0) {
3671 goto out_nofid;
3672 }
c572f23a
HPB
3673 trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid);
3674
fff39a7a
GK
3675 if (name_is_illegal(name.data)) {
3676 err = -ENOENT;
3677 goto out_nofid;
3678 }
3679
805b5d98
GK
3680 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3681 err = -EEXIST;
3682 goto out_nofid;
3683 }
3684
bccacf6c 3685 fidp = get_fid(pdu, fid);
b67592ea
MK
3686 if (fidp == NULL) {
3687 err = -ENOENT;
84dfb926 3688 goto out_nofid;
b67592ea 3689 }
bccacf6c 3690 err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf);
e84861f7
VJ
3691 if (err < 0) {
3692 goto out;
3693 }
3b5ee9e8
AM
3694 err = stat_to_qid(pdu, &stbuf, &qid);
3695 if (err < 0) {
3696 goto out;
3697 }
ddca7f86
MK
3698 err = pdu_marshal(pdu, offset, "Q", &qid);
3699 if (err < 0) {
3700 goto out;
3701 }
3702 err += offset;
7999f7e1
AK
3703 trace_v9fs_mkdir_return(pdu->tag, pdu->id,
3704 qid.type, qid.version, qid.path, err);
b67592ea 3705out:
bccacf6c 3706 put_fid(pdu, fidp);
84dfb926 3707out_nofid:
dc295f83 3708 pdu_complete(pdu, err);
e84861f7 3709 v9fs_string_free(&name);
b67592ea
MK
3710}
3711
8440e22e 3712static void coroutine_fn v9fs_xattrwalk(void *opaque)
fa32ef88 3713{
670185a6
AK
3714 int64_t size;
3715 V9fsString name;
fa32ef88 3716 ssize_t err = 0;
670185a6 3717 size_t offset = 7;
fa32ef88 3718 int32_t fid, newfid;
670185a6 3719 V9fsFidState *file_fidp;
84dfb926 3720 V9fsFidState *xattr_fidp = NULL;
670185a6
AK
3721 V9fsPDU *pdu = opaque;
3722 V9fsState *s = pdu->s;
fa32ef88 3723
ddca7f86
MK
3724 v9fs_string_init(&name);
3725 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name);
3726 if (err < 0) {
3727 goto out_nofid;
3728 }
c572f23a
HPB
3729 trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data);
3730
bccacf6c 3731 file_fidp = get_fid(pdu, fid);
670185a6 3732 if (file_fidp == NULL) {
fa32ef88 3733 err = -ENOENT;
84dfb926 3734 goto out_nofid;
fa32ef88 3735 }
670185a6
AK
3736 xattr_fidp = alloc_fid(s, newfid);
3737 if (xattr_fidp == NULL) {
fa32ef88
AK
3738 err = -EINVAL;
3739 goto out;
3740 }
2289be19 3741 v9fs_path_copy(&xattr_fidp->path, &file_fidp->path);
ba42ebb8 3742 if (!v9fs_string_size(&name)) {
fa32ef88
AK
3743 /*
3744 * listxattr request. Get the size first
3745 */
bccacf6c 3746 size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0);
670185a6
AK
3747 if (size < 0) {
3748 err = size;
84dfb926 3749 clunk_fid(s, xattr_fidp->fid);
670185a6 3750 goto out;
fa32ef88 3751 }
670185a6
AK
3752 /*
3753 * Read the xattr value
3754 */
3755 xattr_fidp->fs.xattr.len = size;
3756 xattr_fidp->fid_type = P9_FID_XATTR;
dd28fbbc 3757 xattr_fidp->fs.xattr.xattrwalk_fid = true;
a647502c 3758 xattr_fidp->fs.xattr.value = g_malloc0(size);
670185a6 3759 if (size) {
bccacf6c 3760 err = v9fs_co_llistxattr(pdu, &xattr_fidp->path,
670185a6
AK
3761 xattr_fidp->fs.xattr.value,
3762 xattr_fidp->fs.xattr.len);
3763 if (err < 0) {
84dfb926 3764 clunk_fid(s, xattr_fidp->fid);
670185a6
AK
3765 goto out;
3766 }
3767 }
ddca7f86
MK
3768 err = pdu_marshal(pdu, offset, "q", size);
3769 if (err < 0) {
3770 goto out;
3771 }
3772 err += offset;
fa32ef88
AK
3773 } else {
3774 /*
3775 * specific xattr fid. We check for xattr
3776 * presence also collect the xattr size
3777 */
bccacf6c 3778 size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
670185a6
AK
3779 &name, NULL, 0);
3780 if (size < 0) {
3781 err = size;
84dfb926 3782 clunk_fid(s, xattr_fidp->fid);
670185a6 3783 goto out;
fa32ef88 3784 }
670185a6
AK
3785 /*
3786 * Read the xattr value
3787 */
3788 xattr_fidp->fs.xattr.len = size;
3789 xattr_fidp->fid_type = P9_FID_XATTR;
dd28fbbc 3790 xattr_fidp->fs.xattr.xattrwalk_fid = true;
a647502c 3791 xattr_fidp->fs.xattr.value = g_malloc0(size);
670185a6 3792 if (size) {
bccacf6c 3793 err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
670185a6
AK
3794 &name, xattr_fidp->fs.xattr.value,
3795 xattr_fidp->fs.xattr.len);
3796 if (err < 0) {
84dfb926 3797 clunk_fid(s, xattr_fidp->fid);
670185a6
AK
3798 goto out;
3799 }
3800 }
ddca7f86
MK
3801 err = pdu_marshal(pdu, offset, "q", size);
3802 if (err < 0) {
3803 goto out;
3804 }
3805 err += offset;
fa32ef88 3806 }
7999f7e1 3807 trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size);
fa32ef88 3808out:
bccacf6c 3809 put_fid(pdu, file_fidp);
84dfb926 3810 if (xattr_fidp) {
bccacf6c 3811 put_fid(pdu, xattr_fidp);
84dfb926
AK
3812 }
3813out_nofid:
dc295f83 3814 pdu_complete(pdu, err);
670185a6 3815 v9fs_string_free(&name);
fa32ef88
AK
3816}
3817
8440e22e 3818static void coroutine_fn v9fs_xattrcreate(void *opaque)
10b468bd 3819{
aca6897f 3820 int flags, rflags = 0;
10b468bd 3821 int32_t fid;
3b79ef2c 3822 uint64_t size;
10b468bd 3823 ssize_t err = 0;
f10ff58d
AK
3824 V9fsString name;
3825 size_t offset = 7;
3826 V9fsFidState *file_fidp;
3827 V9fsFidState *xattr_fidp;
3828 V9fsPDU *pdu = opaque;
10b468bd 3829
ddca7f86
MK
3830 v9fs_string_init(&name);
3831 err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags);
3832 if (err < 0) {
3833 goto out_nofid;
3834 }
c572f23a 3835 trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags);
10b468bd 3836
aca6897f
KF
3837 if (flags & ~(P9_XATTR_CREATE | P9_XATTR_REPLACE)) {
3838 err = -EINVAL;
3839 goto out_nofid;
3840 }
3841
3842 if (flags & P9_XATTR_CREATE) {
3843 rflags |= XATTR_CREATE;
3844 }
3845
3846 if (flags & P9_XATTR_REPLACE) {
3847 rflags |= XATTR_REPLACE;
3848 }
3849
3b79ef2c
GK
3850 if (size > XATTR_SIZE_MAX) {
3851 err = -E2BIG;
3852 goto out_nofid;
3853 }
3854
bccacf6c 3855 file_fidp = get_fid(pdu, fid);
f10ff58d 3856 if (file_fidp == NULL) {
10b468bd 3857 err = -EINVAL;
84dfb926 3858 goto out_nofid;
10b468bd 3859 }
dd654e03
GK
3860 if (file_fidp->fid_type != P9_FID_NONE) {
3861 err = -EINVAL;
3862 goto out_put_fid;
3863 }
3864
10b468bd 3865 /* Make the file fid point to xattr */
f10ff58d
AK
3866 xattr_fidp = file_fidp;
3867 xattr_fidp->fid_type = P9_FID_XATTR;
3868 xattr_fidp->fs.xattr.copied_len = 0;
dd28fbbc 3869 xattr_fidp->fs.xattr.xattrwalk_fid = false;
f10ff58d 3870 xattr_fidp->fs.xattr.len = size;
aca6897f 3871 xattr_fidp->fs.xattr.flags = rflags;
f10ff58d
AK
3872 v9fs_string_init(&xattr_fidp->fs.xattr.name);
3873 v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
eb687602 3874 xattr_fidp->fs.xattr.value = g_malloc0(size);
f10ff58d 3875 err = offset;
dd654e03 3876out_put_fid:
bccacf6c 3877 put_fid(pdu, file_fidp);
84dfb926 3878out_nofid:
dc295f83 3879 pdu_complete(pdu, err);
f10ff58d 3880 v9fs_string_free(&name);
10b468bd 3881}
fa32ef88 3882
8440e22e 3883static void coroutine_fn v9fs_readlink(void *opaque)
df0973a4 3884{
ff06030f 3885 V9fsPDU *pdu = opaque;
7a5ca31e
VJ
3886 size_t offset = 7;
3887 V9fsString target;
df0973a4 3888 int32_t fid;
df0973a4
MK
3889 int err = 0;
3890 V9fsFidState *fidp;
3891
ddca7f86
MK
3892 err = pdu_unmarshal(pdu, offset, "d", &fid);
3893 if (err < 0) {
3894 goto out_nofid;
3895 }
c572f23a 3896 trace_v9fs_readlink(pdu->tag, pdu->id, fid);
bccacf6c 3897 fidp = get_fid(pdu, fid);
df0973a4
MK
3898 if (fidp == NULL) {
3899 err = -ENOENT;
84dfb926 3900 goto out_nofid;
df0973a4
MK
3901 }
3902
7a5ca31e 3903 v9fs_string_init(&target);
bccacf6c 3904 err = v9fs_co_readlink(pdu, &fidp->path, &target);
7a5ca31e
VJ
3905 if (err < 0) {
3906 goto out;
3907 }
ddca7f86
MK
3908 err = pdu_marshal(pdu, offset, "s", &target);
3909 if (err < 0) {
3910 v9fs_string_free(&target);
3911 goto out;
3912 }
3913 err += offset;
7999f7e1 3914 trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data);
7a5ca31e 3915 v9fs_string_free(&target);
df0973a4 3916out:
bccacf6c 3917 put_fid(pdu, fidp);
84dfb926 3918out_nofid:
dc295f83 3919 pdu_complete(pdu, err);
df0973a4
MK
3920}
3921
ff06030f 3922static CoroutineEntry *pdu_co_handlers[] = {
c18e2f94 3923 [P9_TREADDIR] = v9fs_readdir,
be940c87 3924 [P9_TSTATFS] = v9fs_statfs,
00ede4c2 3925 [P9_TGETATTR] = v9fs_getattr,
c79ce737 3926 [P9_TSETATTR] = v9fs_setattr,
fa32ef88 3927 [P9_TXATTRWALK] = v9fs_xattrwalk,
10b468bd 3928 [P9_TXATTRCREATE] = v9fs_xattrcreate,
5268cecc 3929 [P9_TMKNOD] = v9fs_mknod,
c7b4b0b3 3930 [P9_TRENAME] = v9fs_rename,
82cc3ee8 3931 [P9_TLOCK] = v9fs_lock,
8f354003 3932 [P9_TGETLOCK] = v9fs_getlock,
89bf6593 3933 [P9_TRENAMEAT] = v9fs_renameat,
df0973a4 3934 [P9_TREADLINK] = v9fs_readlink,
7834cf77 3935 [P9_TUNLINKAT] = v9fs_unlinkat,
b67592ea 3936 [P9_TMKDIR] = v9fs_mkdir,
9f107513 3937 [P9_TVERSION] = v9fs_version,
771e9d4c 3938 [P9_TLOPEN] = v9fs_open,
9f107513
AL
3939 [P9_TATTACH] = v9fs_attach,
3940 [P9_TSTAT] = v9fs_stat,
3941 [P9_TWALK] = v9fs_walk,
3942 [P9_TCLUNK] = v9fs_clunk,
b41e95d3 3943 [P9_TFSYNC] = v9fs_fsync,
9f107513
AL
3944 [P9_TOPEN] = v9fs_open,
3945 [P9_TREAD] = v9fs_read,
3946#if 0
3947 [P9_TAUTH] = v9fs_auth,
3948#endif
3949 [P9_TFLUSH] = v9fs_flush,
b2c224be 3950 [P9_TLINK] = v9fs_link,
08c60fc9 3951 [P9_TSYMLINK] = v9fs_symlink,
9f107513 3952 [P9_TCREATE] = v9fs_create,
c1568af5 3953 [P9_TLCREATE] = v9fs_lcreate,
9f107513
AL
3954 [P9_TWRITE] = v9fs_write,
3955 [P9_TWSTAT] = v9fs_wstat,
3956 [P9_TREMOVE] = v9fs_remove,
3957};
3958
8440e22e 3959static void coroutine_fn v9fs_op_not_supp(void *opaque)
5c3234c6 3960{
ff06030f 3961 V9fsPDU *pdu = opaque;
dc295f83 3962 pdu_complete(pdu, -EOPNOTSUPP);
5c3234c6
AK
3963}
3964
8440e22e 3965static void coroutine_fn v9fs_fs_ro(void *opaque)
2c74c2cb
MK
3966{
3967 V9fsPDU *pdu = opaque;
dc295f83 3968 pdu_complete(pdu, -EROFS);
2c74c2cb
MK
3969}
3970
3971static inline bool is_read_only_op(V9fsPDU *pdu)
3972{
3973 switch (pdu->id) {
3974 case P9_TREADDIR:
3975 case P9_TSTATFS:
3976 case P9_TGETATTR:
3977 case P9_TXATTRWALK:
3978 case P9_TLOCK:
3979 case P9_TGETLOCK:
3980 case P9_TREADLINK:
3981 case P9_TVERSION:
3982 case P9_TLOPEN:
3983 case P9_TATTACH:
3984 case P9_TSTAT:
3985 case P9_TWALK:
3986 case P9_TCLUNK:
3987 case P9_TFSYNC:
3988 case P9_TOPEN:
3989 case P9_TREAD:
3990 case P9_TAUTH:
3991 case P9_TFLUSH:
3992 return 1;
3993 default:
3994 return 0;
3995 }
3996}
3997
506f3275 3998void pdu_submit(V9fsPDU *pdu, P9MsgHeader *hdr)
9f107513 3999{
ff06030f
VJ
4000 Coroutine *co;
4001 CoroutineEntry *handler;
ad38ce9e 4002 V9fsState *s = pdu->s;
9f107513 4003
506f3275
GK
4004 pdu->size = le32_to_cpu(hdr->size_le);
4005 pdu->id = hdr->id;
4006 pdu->tag = le16_to_cpu(hdr->tag_le);
4007
ff06030f
VJ
4008 if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) ||
4009 (pdu_co_handlers[pdu->id] == NULL)) {
5c3234c6 4010 handler = v9fs_op_not_supp;
d1471233
GK
4011 } else if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) {
4012 handler = v9fs_fs_ro;
5c3234c6 4013 } else {
ff06030f 4014 handler = pdu_co_handlers[pdu->id];
5c3234c6 4015 }
2c74c2cb 4016
506f3275 4017 qemu_co_queue_init(&pdu->complete);
0b8b8753
PB
4018 co = qemu_coroutine_create(handler, pdu);
4019 qemu_coroutine_enter(co);
9f107513
AL
4020}
4021
2a0c56aa 4022/* Returns 0 on success, 1 on failure. */
066eb006
GK
4023int v9fs_device_realize_common(V9fsState *s, const V9fsTransport *t,
4024 Error **errp)
2a0c56aa
WL
4025{
4026 int i, len;
4027 struct stat stat;
4028 FsDriverEntry *fse;
4029 V9fsPath path;
4030 int rc = 1;
4031
066eb006
GK
4032 assert(!s->transport);
4033 s->transport = t;
4034
2a0c56aa
WL
4035 /* initialize pdu allocator */
4036 QLIST_INIT(&s->free_list);
4037 QLIST_INIT(&s->active_list);
0d78289c 4038 for (i = 0; i < MAX_REQ; i++) {
583f21f8
SS
4039 QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
4040 s->pdus[i].s = s;
4041 s->pdus[i].idx = i;
2a0c56aa
WL
4042 }
4043
4044 v9fs_path_init(&path);
4045
4046 fse = get_fsdev_fsentry(s->fsconf.fsdev_id);
4047
4048 if (!fse) {
4049 /* We don't have a fsdev identified by fsdev_id */
4050 error_setg(errp, "9pfs device couldn't find fsdev with the "
4051 "id = %s",
4052 s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL");
4053 goto out;
4054 }
4055
4056 if (!s->fsconf.tag) {
4057 /* we haven't specified a mount_tag */
4058 error_setg(errp, "fsdev with id %s needs mount_tag arguments",
4059 s->fsconf.fsdev_id);
4060 goto out;
4061 }
4062
4063 s->ctx.export_flags = fse->export_flags;
4064 s->ctx.fs_root = g_strdup(fse->path);
4065 s->ctx.exops.get_st_gen = NULL;
4066 len = strlen(s->fsconf.tag);
4067 if (len > MAX_TAG_LEN - 1) {
4068 error_setg(errp, "mount tag '%s' (%d bytes) is longer than "
4069 "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1);
4070 goto out;
4071 }
4072
4073 s->tag = g_strdup(s->fsconf.tag);
4074 s->ctx.uid = -1;
4075
4076 s->ops = fse->ops;
4077
b96feb2c
TS
4078 s->ctx.fmode = fse->fmode;
4079 s->ctx.dmode = fse->dmode;
4080
2a0c56aa
WL
4081 s->fid_list = NULL;
4082 qemu_co_rwlock_init(&s->rename_lock);
4083
65603a80
GK
4084 if (s->ops->init(&s->ctx, errp) < 0) {
4085 error_prepend(errp, "cannot initialize fsdev '%s': ",
4086 s->fsconf.fsdev_id);
2a0c56aa
WL
4087 goto out;
4088 }
4089
4090 /*
4091 * Check details of export path, We need to use fs driver
4092 * call back to do that. Since we are in the init path, we don't
4093 * use co-routines here.
4094 */
4095 if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) {
4096 error_setg(errp,
4097 "error in converting name to path %s", strerror(errno));
4098 goto out;
4099 }
4100 if (s->ops->lstat(&s->ctx, &path, &stat)) {
4101 error_setg(errp, "share path %s does not exist", fse->path);
4102 goto out;
4103 } else if (!S_ISDIR(stat.st_mode)) {
4104 error_setg(errp, "share path %s is not a directory", fse->path);
4105 goto out;
4106 }
b8bbdb88 4107
3b5ee9e8
AM
4108 s->dev_id = stat.st_dev;
4109
6b6aa828
CS
4110 /* init inode remapping : */
4111 /* hash table for variable length inode suffixes */
4112 qpd_table_init(&s->qpd_table);
4113 /* hash table for slow/full inode remapping (most users won't need it) */
4114 qpf_table_init(&s->qpf_table);
4115 /* hash table for quick inode remapping */
1a6ed33c 4116 qpp_table_init(&s->qpp_table);
6b6aa828
CS
4117 s->qp_ndevices = 0;
4118 s->qp_affix_next = 1; /* reserve 0 to detect overflow */
f3fe4a2d 4119 s->qp_fullpath_next = 1;
1a6ed33c 4120
b8bbdb88
PJ
4121 s->ctx.fst = &fse->fst;
4122 fsdev_throttle_init(s->ctx.fst);
4123
2a0c56aa
WL
4124 rc = 0;
4125out:
4126 if (rc) {
c0da0cb7 4127 v9fs_device_unrealize_common(s, NULL);
2a0c56aa 4128 }
c0da0cb7 4129 v9fs_path_free(&path);
2a0c56aa
WL
4130 return rc;
4131}
4132
4133void v9fs_device_unrealize_common(V9fsState *s, Error **errp)
4134{
c0da0cb7 4135 if (s->ops && s->ops->cleanup) {
702dbcc2
LQ
4136 s->ops->cleanup(&s->ctx);
4137 }
c0da0cb7
GK
4138 if (s->ctx.fst) {
4139 fsdev_throttle_cleanup(s->ctx.fst);
4140 }
2a0c56aa 4141 g_free(s->tag);
6b6aa828 4142 qp_table_destroy(&s->qpd_table);
f3fe4a2d
AM
4143 qp_table_destroy(&s->qpp_table);
4144 qp_table_destroy(&s->qpf_table);
4774718e 4145 g_free(s->ctx.fs_root);
2a0c56aa
WL
4146}
4147
0e44a0fd
GK
4148typedef struct VirtfsCoResetData {
4149 V9fsPDU pdu;
4150 bool done;
4151} VirtfsCoResetData;
4152
4153static void coroutine_fn virtfs_co_reset(void *opaque)
4154{
4155 VirtfsCoResetData *data = opaque;
4156
4157 virtfs_reset(&data->pdu);
4158 data->done = true;
4159}
4160
4161void v9fs_reset(V9fsState *s)
4162{
4163 VirtfsCoResetData data = { .pdu = { .s = s }, .done = false };
4164 Coroutine *co;
4165
4166 while (!QLIST_EMPTY(&s->active_list)) {
4167 aio_poll(qemu_get_aio_context(), true);
4168 }
4169
4170 co = qemu_coroutine_create(virtfs_co_reset, &data);
4171 qemu_coroutine_enter(co);
4172
4173 while (!data.done) {
4174 aio_poll(qemu_get_aio_context(), true);
4175 }
4176}
4177
72a18977 4178static void __attribute__((__constructor__)) v9fs_set_fd_limit(void)
7a462745
AK
4179{
4180 struct rlimit rlim;
4181 if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
63325b18 4182 error_report("Failed to get the resource limit");
7a462745
AK
4183 exit(1);
4184 }
4185 open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur/3);
4186 open_fd_rc = rlim.rlim_cur/2;
4187}