]> Git Repo - qemu.git/blame - hw/9pfs/9p.c
Merge remote-tracking branch 'remotes/juanquintela/tags/pull-migration-pull-request...
[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 <[email protected]>
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");
9f107513 1366 }
92c1ad03 1367
ddca7f86
MK
1368 err = pdu_marshal(pdu, offset, "ds", s->msize, &version);
1369 if (err < 0) {
ddca7f86
MK
1370 goto out;
1371 }
403a905b 1372 err += offset;
c572f23a 1373 trace_v9fs_version_return(pdu->tag, pdu->id, s->msize, version.data);
ddca7f86 1374out:
403a905b 1375 pdu_complete(pdu, err);
92c1ad03 1376 v9fs_string_free(&version);
9f107513
AL
1377}
1378
8440e22e 1379static void coroutine_fn v9fs_attach(void *opaque)
9f107513 1380{
ff06030f
VJ
1381 V9fsPDU *pdu = opaque;
1382 V9fsState *s = pdu->s;
955efc47
AL
1383 int32_t fid, afid, n_uname;
1384 V9fsString uname, aname;
1385 V9fsFidState *fidp;
955efc47 1386 size_t offset = 7;
8c158561 1387 V9fsQID qid;
955efc47 1388 ssize_t err;
fe44dc91 1389 Error *local_err = NULL;
955efc47 1390
ddca7f86
MK
1391 v9fs_string_init(&uname);
1392 v9fs_string_init(&aname);
1393 err = pdu_unmarshal(pdu, offset, "ddssd", &fid,
1394 &afid, &uname, &aname, &n_uname);
1395 if (err < 0) {
1396 goto out_nofid;
1397 }
c572f23a 1398 trace_v9fs_attach(pdu->tag, pdu->id, fid, afid, uname.data, aname.data);
955efc47
AL
1399
1400 fidp = alloc_fid(s, fid);
1401 if (fidp == NULL) {
1402 err = -EINVAL;
84dfb926 1403 goto out_nofid;
9f107513 1404 }
955efc47 1405 fidp->uid = n_uname;
bccacf6c 1406 err = v9fs_co_name_to_path(pdu, NULL, "/", &fidp->path);
2289be19
AK
1407 if (err < 0) {
1408 err = -EINVAL;
1409 clunk_fid(s, fid);
1410 goto out;
1411 }
bccacf6c 1412 err = fid_to_qid(pdu, fidp, &qid);
8c158561 1413 if (err < 0) {
955efc47 1414 err = -EINVAL;
84dfb926 1415 clunk_fid(s, fid);
955efc47
AL
1416 goto out;
1417 }
fe44dc91 1418
4cdc0789
AK
1419 /*
1420 * disable migration if we haven't done already.
1421 * attach could get called multiple times for the same export.
1422 */
1423 if (!s->migration_blocker) {
f231b88d
CR
1424 error_setg(&s->migration_blocker,
1425 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1426 s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag);
fe44dc91
AA
1427 err = migrate_add_blocker(s->migration_blocker, &local_err);
1428 if (local_err) {
1429 error_free(local_err);
1430 error_free(s->migration_blocker);
1431 s->migration_blocker = NULL;
1432 clunk_fid(s, fid);
1433 goto out;
1434 }
1435 s->root_fid = fid;
1436 }
1437
1438 err = pdu_marshal(pdu, offset, "Q", &qid);
1439 if (err < 0) {
1440 clunk_fid(s, fid);
1441 goto out;
4cdc0789 1442 }
fe44dc91
AA
1443 err += offset;
1444
1445 memcpy(&s->root_qid, &qid, sizeof(qid));
1446 trace_v9fs_attach_return(pdu->tag, pdu->id,
1447 qid.type, qid.version, qid.path);
955efc47 1448out:
bccacf6c 1449 put_fid(pdu, fidp);
84dfb926 1450out_nofid:
dc295f83 1451 pdu_complete(pdu, err);
955efc47
AL
1452 v9fs_string_free(&uname);
1453 v9fs_string_free(&aname);
9f107513
AL
1454}
1455
8440e22e 1456static void coroutine_fn v9fs_stat(void *opaque)
9f107513 1457{
4da7d3fa 1458 int32_t fid;
d8e0c29e 1459 V9fsStat v9stat;
4da7d3fa 1460 ssize_t err = 0;
d8e0c29e
AK
1461 size_t offset = 7;
1462 struct stat stbuf;
1463 V9fsFidState *fidp;
1464 V9fsPDU *pdu = opaque;
6069537f 1465 char *basename;
4da7d3fa 1466
ddca7f86
MK
1467 err = pdu_unmarshal(pdu, offset, "d", &fid);
1468 if (err < 0) {
1469 goto out_nofid;
1470 }
c572f23a 1471 trace_v9fs_stat(pdu->tag, pdu->id, fid);
84dfb926 1472
bccacf6c 1473 fidp = get_fid(pdu, fid);
d8e0c29e 1474 if (fidp == NULL) {
4da7d3fa 1475 err = -ENOENT;
84dfb926 1476 goto out_nofid;
9f107513 1477 }
bccacf6c 1478 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
d8e0c29e
AK
1479 if (err < 0) {
1480 goto out;
1481 }
6069537f
JD
1482 basename = g_path_get_basename(fidp->path.data);
1483 err = stat_to_v9stat(pdu, &fidp->path, basename, &stbuf, &v9stat);
1484 g_free(basename);
d8e0c29e
AK
1485 if (err < 0) {
1486 goto out;
1487 }
ddca7f86
MK
1488 err = pdu_marshal(pdu, offset, "wS", 0, &v9stat);
1489 if (err < 0) {
1490 v9fs_stat_free(&v9stat);
1491 goto out;
1492 }
7999f7e1
AK
1493 trace_v9fs_stat_return(pdu->tag, pdu->id, v9stat.mode,
1494 v9stat.atime, v9stat.mtime, v9stat.length);
ddca7f86 1495 err += offset;
d8e0c29e 1496 v9fs_stat_free(&v9stat);
4da7d3fa 1497out:
bccacf6c 1498 put_fid(pdu, fidp);
84dfb926 1499out_nofid:
dc295f83 1500 pdu_complete(pdu, err);
9f107513
AL
1501}
1502
8440e22e 1503static void coroutine_fn v9fs_getattr(void *opaque)
00ede4c2
SK
1504{
1505 int32_t fid;
8db21ce7
AK
1506 size_t offset = 7;
1507 ssize_t retval = 0;
1508 struct stat stbuf;
00ede4c2
SK
1509 V9fsFidState *fidp;
1510 uint64_t request_mask;
8db21ce7
AK
1511 V9fsStatDotl v9stat_dotl;
1512 V9fsPDU *pdu = opaque;
00ede4c2 1513
ddca7f86
MK
1514 retval = pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask);
1515 if (retval < 0) {
1516 goto out_nofid;
1517 }
c572f23a 1518 trace_v9fs_getattr(pdu->tag, pdu->id, fid, request_mask);
00ede4c2 1519
bccacf6c 1520 fidp = get_fid(pdu, fid);
00ede4c2 1521 if (fidp == NULL) {
8db21ce7 1522 retval = -ENOENT;
84dfb926 1523 goto out_nofid;
00ede4c2 1524 }
8db21ce7
AK
1525 /*
1526 * Currently we only support BASIC fields in stat, so there is no
00ede4c2
SK
1527 * need to look at request_mask.
1528 */
bccacf6c 1529 retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
8db21ce7
AK
1530 if (retval < 0) {
1531 goto out;
1532 }
3b5ee9e8
AM
1533 retval = stat_to_v9stat_dotl(pdu, &stbuf, &v9stat_dotl);
1534 if (retval < 0) {
1535 goto out;
1536 }
e06a765e
HPB
1537
1538 /* fill st_gen if requested and supported by underlying fs */
1539 if (request_mask & P9_STATS_GEN) {
1540 retval = v9fs_co_st_gen(pdu, &fidp->path, stbuf.st_mode, &v9stat_dotl);
f8b7ee38
KS
1541 switch (retval) {
1542 case 0:
1543 /* we have valid st_gen: update result mask */
1544 v9stat_dotl.st_result_mask |= P9_STATS_GEN;
1545 break;
1546 case -EINTR:
1547 /* request cancelled, e.g. by Tflush */
e06a765e 1548 goto out;
f8b7ee38
KS
1549 default:
1550 /* failed to get st_gen: not fatal, ignore */
1551 break;
e06a765e 1552 }
e06a765e 1553 }
ddca7f86
MK
1554 retval = pdu_marshal(pdu, offset, "A", &v9stat_dotl);
1555 if (retval < 0) {
1556 goto out;
1557 }
1558 retval += offset;
c572f23a
HPB
1559 trace_v9fs_getattr_return(pdu->tag, pdu->id, v9stat_dotl.st_result_mask,
1560 v9stat_dotl.st_mode, v9stat_dotl.st_uid,
1561 v9stat_dotl.st_gid);
7999f7e1
AK
1562out:
1563 put_fid(pdu, fidp);
1564out_nofid:
dc295f83 1565 pdu_complete(pdu, retval);
00ede4c2
SK
1566}
1567
e4027caf
AK
1568/* Attribute flags */
1569#define P9_ATTR_MODE (1 << 0)
1570#define P9_ATTR_UID (1 << 1)
1571#define P9_ATTR_GID (1 << 2)
1572#define P9_ATTR_SIZE (1 << 3)
1573#define P9_ATTR_ATIME (1 << 4)
1574#define P9_ATTR_MTIME (1 << 5)
1575#define P9_ATTR_CTIME (1 << 6)
1576#define P9_ATTR_ATIME_SET (1 << 7)
1577#define P9_ATTR_MTIME_SET (1 << 8)
1578
1579#define P9_ATTR_MASK 127
c79ce737 1580
8440e22e 1581static void coroutine_fn v9fs_setattr(void *opaque)
c79ce737 1582{
65c05f9a
AK
1583 int err = 0;
1584 int32_t fid;
1585 V9fsFidState *fidp;
1586 size_t offset = 7;
1587 V9fsIattr v9iattr;
1588 V9fsPDU *pdu = opaque;
c79ce737 1589
ddca7f86
MK
1590 err = pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr);
1591 if (err < 0) {
1592 goto out_nofid;
1593 }
c79ce737 1594
8f9c64bf
GK
1595 trace_v9fs_setattr(pdu->tag, pdu->id, fid,
1596 v9iattr.valid, v9iattr.mode, v9iattr.uid, v9iattr.gid,
1597 v9iattr.size, v9iattr.atime_sec, v9iattr.mtime_sec);
1598
bccacf6c 1599 fidp = get_fid(pdu, fid);
65c05f9a
AK
1600 if (fidp == NULL) {
1601 err = -EINVAL;
84dfb926 1602 goto out_nofid;
c79ce737 1603 }
e4027caf 1604 if (v9iattr.valid & P9_ATTR_MODE) {
bccacf6c 1605 err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode);
65c05f9a
AK
1606 if (err < 0) {
1607 goto out;
c79ce737 1608 }
c79ce737 1609 }
e4027caf 1610 if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) {
c79ce737 1611 struct timespec times[2];
e4027caf
AK
1612 if (v9iattr.valid & P9_ATTR_ATIME) {
1613 if (v9iattr.valid & P9_ATTR_ATIME_SET) {
65c05f9a
AK
1614 times[0].tv_sec = v9iattr.atime_sec;
1615 times[0].tv_nsec = v9iattr.atime_nsec;
c79ce737
SK
1616 } else {
1617 times[0].tv_nsec = UTIME_NOW;
1618 }
1619 } else {
1620 times[0].tv_nsec = UTIME_OMIT;
1621 }
e4027caf
AK
1622 if (v9iattr.valid & P9_ATTR_MTIME) {
1623 if (v9iattr.valid & P9_ATTR_MTIME_SET) {
65c05f9a
AK
1624 times[1].tv_sec = v9iattr.mtime_sec;
1625 times[1].tv_nsec = v9iattr.mtime_nsec;
c79ce737
SK
1626 } else {
1627 times[1].tv_nsec = UTIME_NOW;
1628 }
1629 } else {
1630 times[1].tv_nsec = UTIME_OMIT;
1631 }
bccacf6c 1632 err = v9fs_co_utimensat(pdu, &fidp->path, times);
65c05f9a
AK
1633 if (err < 0) {
1634 goto out;
1635 }
c79ce737 1636 }
65c05f9a
AK
1637 /*
1638 * If the only valid entry in iattr is ctime we can call
1639 * chown(-1,-1) to update the ctime of the file
1640 */
e4027caf
AK
1641 if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) ||
1642 ((v9iattr.valid & P9_ATTR_CTIME)
1643 && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) {
1644 if (!(v9iattr.valid & P9_ATTR_UID)) {
65c05f9a
AK
1645 v9iattr.uid = -1;
1646 }
e4027caf 1647 if (!(v9iattr.valid & P9_ATTR_GID)) {
65c05f9a
AK
1648 v9iattr.gid = -1;
1649 }
bccacf6c 1650 err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid,
65c05f9a
AK
1651 v9iattr.gid);
1652 if (err < 0) {
1653 goto out;
1654 }
c79ce737 1655 }
e4027caf 1656 if (v9iattr.valid & (P9_ATTR_SIZE)) {
bccacf6c 1657 err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size);
65c05f9a
AK
1658 if (err < 0) {
1659 goto out;
1660 }
c79ce737 1661 }
65c05f9a 1662 err = offset;
8f9c64bf 1663 trace_v9fs_setattr_return(pdu->tag, pdu->id);
c79ce737 1664out:
bccacf6c 1665 put_fid(pdu, fidp);
84dfb926 1666out_nofid:
dc295f83 1667 pdu_complete(pdu, err);
c79ce737
SK
1668}
1669
3cc19c0c 1670static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids)
ff5e54c9
AL
1671{
1672 int i;
ddca7f86 1673 ssize_t err;
3cc19c0c 1674 size_t offset = 7;
ddca7f86
MK
1675
1676 err = pdu_marshal(pdu, offset, "w", nwnames);
1677 if (err < 0) {
1678 return err;
1679 }
1680 offset += err;
3cc19c0c 1681 for (i = 0; i < nwnames; i++) {
ddca7f86
MK
1682 err = pdu_marshal(pdu, offset, "Q", &qids[i]);
1683 if (err < 0) {
1684 return err;
1685 }
1686 offset += err;
ff5e54c9 1687 }
3cc19c0c 1688 return offset;
ff5e54c9
AL
1689}
1690
fff39a7a
GK
1691static bool name_is_illegal(const char *name)
1692{
1693 return !*name || strchr(name, '/') != NULL;
1694}
1695
56f101ec
GK
1696static bool not_same_qid(const V9fsQID *qid1, const V9fsQID *qid2)
1697{
1698 return
1699 qid1->type != qid2->type ||
1700 qid1->version != qid2->version ||
1701 qid1->path != qid2->path;
1702}
1703
8440e22e 1704static void coroutine_fn v9fs_walk(void *opaque)
9f107513 1705{
3cc19c0c
AK
1706 int name_idx;
1707 V9fsQID *qids = NULL;
1708 int i, err = 0;
2289be19 1709 V9fsPath dpath, path;
3cc19c0c
AK
1710 uint16_t nwnames;
1711 struct stat stbuf;
1712 size_t offset = 7;
1713 int32_t fid, newfid;
1714 V9fsString *wnames = NULL;
1715 V9fsFidState *fidp;
3a93113a 1716 V9fsFidState *newfidp = NULL;
ff06030f
VJ
1717 V9fsPDU *pdu = opaque;
1718 V9fsState *s = pdu->s;
56f101ec 1719 V9fsQID qid;
ff5e54c9 1720
ddca7f86
MK
1721 err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames);
1722 if (err < 0) {
dc295f83 1723 pdu_complete(pdu, err);
ddca7f86
MK
1724 return ;
1725 }
1726 offset += err;
ff5e54c9 1727
c572f23a
HPB
1728 trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames);
1729
3cc19c0c 1730 if (nwnames && nwnames <= P9_MAXWELEM) {
1923923b
GK
1731 wnames = g_new0(V9fsString, nwnames);
1732 qids = g_new0(V9fsQID, nwnames);
3cc19c0c 1733 for (i = 0; i < nwnames; i++) {
ddca7f86
MK
1734 err = pdu_unmarshal(pdu, offset, "s", &wnames[i]);
1735 if (err < 0) {
1736 goto out_nofid;
1737 }
fff39a7a
GK
1738 if (name_is_illegal(wnames[i].data)) {
1739 err = -ENOENT;
1740 goto out_nofid;
1741 }
ddca7f86 1742 offset += err;
ff5e54c9 1743 }
3cc19c0c 1744 } else if (nwnames > P9_MAXWELEM) {
4f8dee2d 1745 err = -EINVAL;
84dfb926 1746 goto out_nofid;
ff5e54c9 1747 }
bccacf6c 1748 fidp = get_fid(pdu, fid);
3cc19c0c 1749 if (fidp == NULL) {
ff5e54c9 1750 err = -ENOENT;
84dfb926 1751 goto out_nofid;
ff5e54c9 1752 }
56f101ec 1753
13fd08e6
GK
1754 v9fs_path_init(&dpath);
1755 v9fs_path_init(&path);
1756
56f101ec
GK
1757 err = fid_to_qid(pdu, fidp, &qid);
1758 if (err < 0) {
1759 goto out;
1760 }
1761
2289be19
AK
1762 /*
1763 * Both dpath and path initially poin to fidp.
1764 * Needed to handle request with nwnames == 0
1765 */
1766 v9fs_path_copy(&dpath, &fidp->path);
1767 v9fs_path_copy(&path, &fidp->path);
1768 for (name_idx = 0; name_idx < nwnames; name_idx++) {
56f101ec
GK
1769 if (not_same_qid(&pdu->s->root_qid, &qid) ||
1770 strcmp("..", wnames[name_idx].data)) {
1771 err = v9fs_co_name_to_path(pdu, &dpath, wnames[name_idx].data,
1772 &path);
1773 if (err < 0) {
1774 goto out;
1775 }
1776
1777 err = v9fs_co_lstat(pdu, &path, &stbuf);
1778 if (err < 0) {
1779 goto out;
1780 }
3b5ee9e8
AM
1781 err = stat_to_qid(pdu, &stbuf, &qid);
1782 if (err < 0) {
1783 goto out;
1784 }
56f101ec 1785 v9fs_path_copy(&dpath, &path);
2289be19 1786 }
56f101ec 1787 memcpy(&qids[name_idx], &qid, sizeof(qid));
2289be19 1788 }
ff5e54c9 1789 if (fid == newfid) {
49dd946b
GK
1790 if (fidp->fid_type != P9_FID_NONE) {
1791 err = -EINVAL;
1792 goto out;
1793 }
5b3c77aa 1794 v9fs_path_write_lock(s);
2289be19 1795 v9fs_path_copy(&fidp->path, &path);
5b3c77aa 1796 v9fs_path_unlock(s);
ff5e54c9 1797 } else {
3cc19c0c
AK
1798 newfidp = alloc_fid(s, newfid);
1799 if (newfidp == NULL) {
ff5e54c9
AL
1800 err = -EINVAL;
1801 goto out;
1802 }
3cc19c0c 1803 newfidp->uid = fidp->uid;
2289be19 1804 v9fs_path_copy(&newfidp->path, &path);
9f107513 1805 }
3cc19c0c 1806 err = v9fs_walk_marshal(pdu, nwnames, qids);
7999f7e1 1807 trace_v9fs_walk_return(pdu->tag, pdu->id, nwnames, qids);
ff5e54c9 1808out:
bccacf6c 1809 put_fid(pdu, fidp);
84dfb926 1810 if (newfidp) {
bccacf6c 1811 put_fid(pdu, newfidp);
84dfb926 1812 }
2289be19
AK
1813 v9fs_path_free(&dpath);
1814 v9fs_path_free(&path);
84dfb926 1815out_nofid:
dc295f83 1816 pdu_complete(pdu, err);
3cc19c0c
AK
1817 if (nwnames && nwnames <= P9_MAXWELEM) {
1818 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1819 v9fs_string_free(&wnames[name_idx]);
1820 }
1821 g_free(wnames);
1822 g_free(qids);
1823 }
9f107513
AL
1824}
1825
8440e22e 1826static int32_t coroutine_fn get_iounit(V9fsPDU *pdu, V9fsPath *path)
5e94c103
MK
1827{
1828 struct statfs stbuf;
1829 int32_t iounit = 0;
bccacf6c 1830 V9fsState *s = pdu->s;
5e94c103
MK
1831
1832 /*
1833 * iounit should be multiples of f_bsize (host filesystem block size
1834 * and as well as less than (client msize - P9_IOHDRSZ))
1835 */
bccacf6c 1836 if (!v9fs_co_statfs(pdu, path, &stbuf)) {
68d654da
DS
1837 if (stbuf.f_bsize) {
1838 iounit = stbuf.f_bsize;
1839 iounit *= (s->msize - P9_IOHDRSZ) / stbuf.f_bsize;
1840 }
5e94c103 1841 }
5e94c103
MK
1842 if (!iounit) {
1843 iounit = s->msize - P9_IOHDRSZ;
1844 }
1845 return iounit;
1846}
1847
8440e22e 1848static void coroutine_fn v9fs_open(void *opaque)
5e94c103 1849{
857bc158 1850 int flags;
857bc158
AK
1851 int32_t fid;
1852 int32_t mode;
1853 V9fsQID qid;
7999f7e1 1854 int iounit = 0;
857bc158
AK
1855 ssize_t err = 0;
1856 size_t offset = 7;
1857 struct stat stbuf;
1858 V9fsFidState *fidp;
1859 V9fsPDU *pdu = opaque;
1860 V9fsState *s = pdu->s;
5e94c103 1861
857bc158 1862 if (s->proto_version == V9FS_PROTO_2000L) {
ddca7f86 1863 err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode);
857bc158 1864 } else {
67d6fa53
BH
1865 uint8_t modebyte;
1866 err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte);
1867 mode = modebyte;
ddca7f86
MK
1868 }
1869 if (err < 0) {
1870 goto out_nofid;
857bc158 1871 }
c572f23a
HPB
1872 trace_v9fs_open(pdu->tag, pdu->id, fid, mode);
1873
bccacf6c 1874 fidp = get_fid(pdu, fid);
857bc158
AK
1875 if (fidp == NULL) {
1876 err = -ENOENT;
84dfb926 1877 goto out_nofid;
a6568fe2 1878 }
49dd946b
GK
1879 if (fidp->fid_type != P9_FID_NONE) {
1880 err = -EINVAL;
1881 goto out;
1882 }
a6568fe2 1883
bccacf6c 1884 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
857bc158 1885 if (err < 0) {
a6568fe2
AL
1886 goto out;
1887 }
3b5ee9e8
AM
1888 err = stat_to_qid(pdu, &stbuf, &qid);
1889 if (err < 0) {
1890 goto out;
1891 }
857bc158 1892 if (S_ISDIR(stbuf.st_mode)) {
bccacf6c 1893 err = v9fs_co_opendir(pdu, fidp);
857bc158
AK
1894 if (err < 0) {
1895 goto out;
1896 }
1897 fidp->fid_type = P9_FID_DIR;
ddca7f86
MK
1898 err = pdu_marshal(pdu, offset, "Qd", &qid, 0);
1899 if (err < 0) {
1900 goto out;
1901 }
1902 err += offset;
a6568fe2 1903 } else {
771e9d4c 1904 if (s->proto_version == V9FS_PROTO_2000L) {
d3ab98e6 1905 flags = get_dotl_openflags(s, mode);
771e9d4c 1906 } else {
857bc158 1907 flags = omode_to_uflags(mode);
771e9d4c 1908 }
2c74c2cb
MK
1909 if (is_ro_export(&s->ctx)) {
1910 if (mode & O_WRONLY || mode & O_RDWR ||
1911 mode & O_APPEND || mode & O_TRUNC) {
1912 err = -EROFS;
1913 goto out;
1914 }
2c74c2cb 1915 }
bccacf6c 1916 err = v9fs_co_open(pdu, fidp, flags);
857bc158
AK
1917 if (err < 0) {
1918 goto out;
1919 }
1920 fidp->fid_type = P9_FID_FILE;
7a462745
AK
1921 fidp->open_flags = flags;
1922 if (flags & O_EXCL) {
1923 /*
1924 * We let the host file system do O_EXCL check
1925 * We should not reclaim such fd
1926 */
1927 fidp->flags |= FID_NON_RECLAIMABLE;
1928 }
bccacf6c 1929 iounit = get_iounit(pdu, &fidp->path);
ddca7f86
MK
1930 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1931 if (err < 0) {
1932 goto out;
1933 }
1934 err += offset;
a6568fe2 1935 }
7999f7e1
AK
1936 trace_v9fs_open_return(pdu->tag, pdu->id,
1937 qid.type, qid.version, qid.path, iounit);
a6568fe2 1938out:
bccacf6c 1939 put_fid(pdu, fidp);
84dfb926 1940out_nofid:
dc295f83 1941 pdu_complete(pdu, err);
a6568fe2
AL
1942}
1943
8440e22e 1944static void coroutine_fn v9fs_lcreate(void *opaque)
c1568af5
VJ
1945{
1946 int32_t dfid, flags, mode;
1947 gid_t gid;
c1568af5 1948 ssize_t err = 0;
36f8981f 1949 ssize_t offset = 7;
36f8981f
VJ
1950 V9fsString name;
1951 V9fsFidState *fidp;
1952 struct stat stbuf;
1953 V9fsQID qid;
1954 int32_t iounit;
1955 V9fsPDU *pdu = opaque;
c1568af5 1956
ddca7f86
MK
1957 v9fs_string_init(&name);
1958 err = pdu_unmarshal(pdu, offset, "dsddd", &dfid,
1959 &name, &flags, &mode, &gid);
1960 if (err < 0) {
1961 goto out_nofid;
1962 }
c572f23a 1963 trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid);
c1568af5 1964
fff39a7a
GK
1965 if (name_is_illegal(name.data)) {
1966 err = -ENOENT;
1967 goto out_nofid;
1968 }
1969
805b5d98
GK
1970 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
1971 err = -EEXIST;
1972 goto out_nofid;
1973 }
1974
bccacf6c 1975 fidp = get_fid(pdu, dfid);
36f8981f 1976 if (fidp == NULL) {
c1568af5 1977 err = -ENOENT;
84dfb926 1978 goto out_nofid;
c1568af5 1979 }
d63fb193
LQ
1980 if (fidp->fid_type != P9_FID_NONE) {
1981 err = -EINVAL;
1982 goto out;
1983 }
c1568af5 1984
d3ab98e6 1985 flags = get_dotl_openflags(pdu->s, flags);
bccacf6c 1986 err = v9fs_co_open2(pdu, fidp, &name, gid,
02cb7f3a 1987 flags | O_CREAT, mode, &stbuf);
36f8981f
VJ
1988 if (err < 0) {
1989 goto out;
1990 }
1991 fidp->fid_type = P9_FID_FILE;
7a462745
AK
1992 fidp->open_flags = flags;
1993 if (flags & O_EXCL) {
1994 /*
1995 * We let the host file system do O_EXCL check
1996 * We should not reclaim such fd
1997 */
1998 fidp->flags |= FID_NON_RECLAIMABLE;
1999 }
bccacf6c 2000 iounit = get_iounit(pdu, &fidp->path);
3b5ee9e8
AM
2001 err = stat_to_qid(pdu, &stbuf, &qid);
2002 if (err < 0) {
2003 goto out;
2004 }
ddca7f86
MK
2005 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
2006 if (err < 0) {
2007 goto out;
2008 }
2009 err += offset;
7999f7e1
AK
2010 trace_v9fs_lcreate_return(pdu->tag, pdu->id,
2011 qid.type, qid.version, qid.path, iounit);
c1568af5 2012out:
bccacf6c 2013 put_fid(pdu, fidp);
84dfb926 2014out_nofid:
dc295f83 2015 pdu_complete(pdu, err);
36f8981f 2016 v9fs_string_free(&name);
c1568af5
VJ
2017}
2018
a1bf8b74 2019static void coroutine_fn v9fs_fsync(void *opaque)
b41e95d3 2020{
4e9ad444 2021 int err;
b41e95d3 2022 int32_t fid;
4e9ad444 2023 int datasync;
b41e95d3
VJ
2024 size_t offset = 7;
2025 V9fsFidState *fidp;
4e9ad444 2026 V9fsPDU *pdu = opaque;
b41e95d3 2027
ddca7f86
MK
2028 err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
2029 if (err < 0) {
2030 goto out_nofid;
2031 }
c572f23a
HPB
2032 trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync);
2033
bccacf6c 2034 fidp = get_fid(pdu, fid);
b41e95d3
VJ
2035 if (fidp == NULL) {
2036 err = -ENOENT;
84dfb926 2037 goto out_nofid;
b41e95d3 2038 }
bccacf6c 2039 err = v9fs_co_fsync(pdu, fidp, datasync);
4e9ad444
AK
2040 if (!err) {
2041 err = offset;
2042 }
bccacf6c 2043 put_fid(pdu, fidp);
84dfb926 2044out_nofid:
dc295f83 2045 pdu_complete(pdu, err);
b41e95d3
VJ
2046}
2047
8440e22e 2048static void coroutine_fn v9fs_clunk(void *opaque)
a6568fe2 2049{
c540ee51 2050 int err;
bbd5697b
AL
2051 int32_t fid;
2052 size_t offset = 7;
84dfb926 2053 V9fsFidState *fidp;
c540ee51
AK
2054 V9fsPDU *pdu = opaque;
2055 V9fsState *s = pdu->s;
bbd5697b 2056
ddca7f86
MK
2057 err = pdu_unmarshal(pdu, offset, "d", &fid);
2058 if (err < 0) {
2059 goto out_nofid;
2060 }
c572f23a 2061 trace_v9fs_clunk(pdu->tag, pdu->id, fid);
84dfb926 2062
ce421a19 2063 fidp = clunk_fid(s, fid);
84dfb926
AK
2064 if (fidp == NULL) {
2065 err = -ENOENT;
2066 goto out_nofid;
2067 }
ce421a19
AK
2068 /*
2069 * Bump the ref so that put_fid will
2070 * free the fid.
2071 */
2072 fidp->ref++;
a911a182
AK
2073 err = put_fid(pdu, fidp);
2074 if (!err) {
2075 err = offset;
2076 }
84dfb926 2077out_nofid:
dc295f83 2078 pdu_complete(pdu, err);
9f107513
AL
2079}
2080
bcb8998f
SS
2081/*
2082 * Create a QEMUIOVector for a sub-region of PDU iovecs
2083 *
2084 * @qiov: uninitialized QEMUIOVector
2085 * @skip: number of bytes to skip from beginning of PDU
2086 * @size: number of bytes to include
2087 * @is_write: true - write, false - read
2088 *
2089 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
2090 * with qemu_iovec_destroy().
2091 */
2092static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu,
16724a17 2093 size_t skip, size_t *size,
bcb8998f
SS
2094 bool is_write)
2095{
2096 QEMUIOVector elem;
2097 struct iovec *iov;
2098 unsigned int niov;
16724a17 2099 size_t alloc_size = *size + skip;
bcb8998f 2100
88da0b03 2101 if (is_write) {
16724a17 2102 pdu->s->transport->init_out_iov_from_pdu(pdu, &iov, &niov, alloc_size);
88da0b03 2103 } else {
16724a17
GK
2104 pdu->s->transport->init_in_iov_from_pdu(pdu, &iov, &niov, &alloc_size);
2105 }
2106
2107 if (alloc_size < skip) {
2108 *size = 0;
2109 } else {
2110 *size = alloc_size - skip;
88da0b03 2111 }
bcb8998f
SS
2112
2113 qemu_iovec_init_external(&elem, iov, niov);
2114 qemu_iovec_init(qiov, niov);
16724a17 2115 qemu_iovec_concat(qiov, &elem, skip, *size);
bcb8998f
SS
2116}
2117
2f008a8c
AK
2118static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
2119 uint64_t off, uint32_t max_count)
a9231555 2120{
ddca7f86 2121 ssize_t err;
d208a0e0 2122 size_t offset = 7;
16724a17 2123 size_t read_count;
bcb8998f 2124 QEMUIOVector qiov_full;
a9231555 2125
7e55d65c
LQ
2126 if (fidp->fs.xattr.len < off) {
2127 read_count = 0;
16724a17 2128 } else if (fidp->fs.xattr.len - off < max_count) {
7e55d65c 2129 read_count = fidp->fs.xattr.len - off;
16724a17 2130 } else {
d208a0e0 2131 read_count = max_count;
a9231555 2132 }
ddca7f86
MK
2133 err = pdu_marshal(pdu, offset, "d", read_count);
2134 if (err < 0) {
2135 return err;
2136 }
2137 offset += err;
00588a0a 2138
16724a17 2139 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, &read_count, false);
fa0eb5c5 2140 err = v9fs_pack(qiov_full.iov, qiov_full.niov, 0,
ddca7f86
MK
2141 ((char *)fidp->fs.xattr.value) + off,
2142 read_count);
bcb8998f 2143 qemu_iovec_destroy(&qiov_full);
ddca7f86
MK
2144 if (err < 0) {
2145 return err;
2146 }
2147 offset += err;
d208a0e0 2148 return offset;
a9231555
AL
2149}
2150
8440e22e
GK
2151static int coroutine_fn v9fs_do_readdir_with_stat(V9fsPDU *pdu,
2152 V9fsFidState *fidp,
2153 uint32_t max_count)
a9231555 2154{
2289be19 2155 V9fsPath path;
d208a0e0
AK
2156 V9fsStat v9stat;
2157 int len, err = 0;
2158 int32_t count = 0;
2159 struct stat stbuf;
2160 off_t saved_dir_pos;
635324e8 2161 struct dirent *dent;
a9231555 2162
d208a0e0 2163 /* save the directory position */
bccacf6c 2164 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
d208a0e0
AK
2165 if (saved_dir_pos < 0) {
2166 return saved_dir_pos;
a9231555 2167 }
5f524c1e 2168
d208a0e0 2169 while (1) {
2289be19 2170 v9fs_path_init(&path);
7cde47d4
GK
2171
2172 v9fs_readdir_lock(&fidp->fs.dir);
2173
635324e8
GK
2174 err = v9fs_co_readdir(pdu, fidp, &dent);
2175 if (err || !dent) {
d208a0e0
AK
2176 break;
2177 }
bccacf6c 2178 err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path);
d208a0e0 2179 if (err < 0) {
8762a46d 2180 break;
d208a0e0 2181 }
bccacf6c 2182 err = v9fs_co_lstat(pdu, &path, &stbuf);
2289be19 2183 if (err < 0) {
8762a46d 2184 break;
2289be19 2185 }
6069537f 2186 err = stat_to_v9stat(pdu, &path, dent->d_name, &stbuf, &v9stat);
d208a0e0 2187 if (err < 0) {
8762a46d 2188 break;
a9231555 2189 }
772a7369
JD
2190 if ((count + v9stat.size + 2) > max_count) {
2191 v9fs_readdir_unlock(&fidp->fs.dir);
2192
2193 /* Ran out of buffer. Set dir back to old position and return */
2194 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
2195 v9fs_stat_free(&v9stat);
2196 v9fs_path_free(&path);
2197 return count;
2198 }
2199
d208a0e0
AK
2200 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
2201 len = pdu_marshal(pdu, 11 + count, "S", &v9stat);
7cde47d4
GK
2202
2203 v9fs_readdir_unlock(&fidp->fs.dir);
2204
772a7369 2205 if (len < 0) {
bccacf6c 2206 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
d208a0e0 2207 v9fs_stat_free(&v9stat);
2289be19 2208 v9fs_path_free(&path);
772a7369 2209 return len;
d208a0e0
AK
2210 }
2211 count += len;
2212 v9fs_stat_free(&v9stat);
2289be19 2213 v9fs_path_free(&path);
d208a0e0 2214 saved_dir_pos = dent->d_off;
a9231555 2215 }
8762a46d 2216
7cde47d4
GK
2217 v9fs_readdir_unlock(&fidp->fs.dir);
2218
2289be19 2219 v9fs_path_free(&path);
d208a0e0
AK
2220 if (err < 0) {
2221 return err;
fa32ef88 2222 }
d208a0e0 2223 return count;
fa32ef88
AK
2224}
2225
8440e22e 2226static void coroutine_fn v9fs_read(void *opaque)
9f107513 2227{
a9231555 2228 int32_t fid;
2f008a8c 2229 uint64_t off;
a9231555 2230 ssize_t err = 0;
d208a0e0
AK
2231 int32_t count = 0;
2232 size_t offset = 7;
2f008a8c 2233 uint32_t max_count;
d208a0e0
AK
2234 V9fsFidState *fidp;
2235 V9fsPDU *pdu = opaque;
2236 V9fsState *s = pdu->s;
a9231555 2237
ddca7f86
MK
2238 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count);
2239 if (err < 0) {
2240 goto out_nofid;
2241 }
c572f23a 2242 trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count);
84dfb926 2243
bccacf6c 2244 fidp = get_fid(pdu, fid);
d208a0e0 2245 if (fidp == NULL) {
a9231555 2246 err = -EINVAL;
84dfb926 2247 goto out_nofid;
a9231555 2248 }
d208a0e0 2249 if (fidp->fid_type == P9_FID_DIR) {
a9231555 2250
d208a0e0 2251 if (off == 0) {
bccacf6c 2252 v9fs_co_rewinddir(pdu, fidp);
a9231555 2253 }
bccacf6c 2254 count = v9fs_do_readdir_with_stat(pdu, fidp, max_count);
d208a0e0
AK
2255 if (count < 0) {
2256 err = count;
2257 goto out;
56d15a53 2258 }
ddca7f86
MK
2259 err = pdu_marshal(pdu, offset, "d", count);
2260 if (err < 0) {
2261 goto out;
2262 }
2263 err += offset + count;
d208a0e0 2264 } else if (fidp->fid_type == P9_FID_FILE) {
302a0d3e
SH
2265 QEMUIOVector qiov_full;
2266 QEMUIOVector qiov;
d208a0e0 2267 int32_t len;
16724a17 2268 size_t size = max_count;
d208a0e0 2269
16724a17 2270 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, &size, false);
302a0d3e 2271 qemu_iovec_init(&qiov, qiov_full.niov);
16724a17 2272 max_count = size;
d208a0e0 2273 do {
302a0d3e 2274 qemu_iovec_reset(&qiov);
1b093c48 2275 qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count);
d208a0e0 2276 if (0) {
302a0d3e 2277 print_sg(qiov.iov, qiov.niov);
d208a0e0
AK
2278 }
2279 /* Loop in case of EINTR */
2280 do {
302a0d3e 2281 len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off);
d208a0e0
AK
2282 if (len >= 0) {
2283 off += len;
2284 count += len;
2285 }
bccacf6c 2286 } while (len == -EINTR && !pdu->cancelled);
d208a0e0
AK
2287 if (len < 0) {
2288 /* IO error return the error */
2289 err = len;
e95c9a49 2290 goto out_free_iovec;
d208a0e0 2291 }
d208a0e0 2292 } while (count < max_count && len > 0);
ddca7f86
MK
2293 err = pdu_marshal(pdu, offset, "d", count);
2294 if (err < 0) {
e95c9a49 2295 goto out_free_iovec;
ddca7f86
MK
2296 }
2297 err += offset + count;
e95c9a49 2298out_free_iovec:
302a0d3e
SH
2299 qemu_iovec_destroy(&qiov);
2300 qemu_iovec_destroy(&qiov_full);
d208a0e0
AK
2301 } else if (fidp->fid_type == P9_FID_XATTR) {
2302 err = v9fs_xattr_read(s, pdu, fidp, off, max_count);
a9231555
AL
2303 } else {
2304 err = -EINVAL;
9f107513 2305 }
7999f7e1 2306 trace_v9fs_read_return(pdu->tag, pdu->id, count, err);
a9231555 2307out:
bccacf6c 2308 put_fid(pdu, fidp);
84dfb926 2309out_nofid:
dc295f83 2310 pdu_complete(pdu, err);
9f107513
AL
2311}
2312
5e4eaa79 2313static size_t v9fs_readdir_data_size(V9fsString *name)
c18e2f94 2314{
5e4eaa79
AK
2315 /*
2316 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
2317 * size of type (1) + size of name.size (2) + strlen(name.data)
2318 */
2319 return 24 + v9fs_string_size(name);
c18e2f94
SK
2320}
2321
8440e22e
GK
2322static int coroutine_fn v9fs_do_readdir(V9fsPDU *pdu, V9fsFidState *fidp,
2323 int32_t max_count)
c18e2f94 2324{
c18e2f94 2325 size_t size;
5e4eaa79
AK
2326 V9fsQID qid;
2327 V9fsString name;
2328 int len, err = 0;
2329 int32_t count = 0;
2330 off_t saved_dir_pos;
635324e8 2331 struct dirent *dent;
c18e2f94 2332
5e4eaa79 2333 /* save the directory position */
bccacf6c 2334 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
5e4eaa79
AK
2335 if (saved_dir_pos < 0) {
2336 return saved_dir_pos;
2337 }
5f524c1e 2338
5e4eaa79 2339 while (1) {
7cde47d4
GK
2340 v9fs_readdir_lock(&fidp->fs.dir);
2341
635324e8
GK
2342 err = v9fs_co_readdir(pdu, fidp, &dent);
2343 if (err || !dent) {
5e4eaa79
AK
2344 break;
2345 }
2346 v9fs_string_init(&name);
2347 v9fs_string_sprintf(&name, "%s", dent->d_name);
2348 if ((count + v9fs_readdir_data_size(&name)) > max_count) {
7cde47d4
GK
2349 v9fs_readdir_unlock(&fidp->fs.dir);
2350
c18e2f94 2351 /* Ran out of buffer. Set dir back to old position and return */
bccacf6c 2352 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
5e4eaa79
AK
2353 v9fs_string_free(&name);
2354 return count;
c18e2f94 2355 }
1a6ed33c
AM
2356
2357 if (pdu->s->ctx.export_flags & V9FS_REMAP_INODES) {
2358 /*
2359 * dirent_to_qid() implies expensive stat call for each entry,
2360 * we must do that here though since inode remapping requires
2361 * the device id, which in turn might be different for
2362 * different entries; we cannot make any assumption to avoid
2363 * that here.
2364 */
2365 err = dirent_to_qid(pdu, fidp, dent, &qid);
2366 if (err < 0) {
2367 v9fs_readdir_unlock(&fidp->fs.dir);
2368 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
2369 v9fs_string_free(&name);
2370 return err;
2371 }
2372 } else {
2373 /*
2374 * Fill up just the path field of qid because the client uses
2375 * only that. To fill the entire qid structure we will have
2376 * to stat each dirent found, which is expensive. For the
2377 * latter reason we don't call dirent_to_qid() here. Only drawback
2378 * is that no multi-device export detection of stat_to_qid()
2379 * would be done and provided as error to the user here. But
2380 * user would get that error anyway when accessing those
2381 * files/dirs through other ways.
2382 */
2383 size = MIN(sizeof(dent->d_ino), sizeof(qid.path));
2384 memcpy(&qid.path, &dent->d_ino, size);
2385 /* Fill the other fields with dummy values */
2386 qid.type = 0;
2387 qid.version = 0;
2388 }
c18e2f94 2389
5e4eaa79
AK
2390 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
2391 len = pdu_marshal(pdu, 11 + count, "Qqbs",
2392 &qid, dent->d_off,
2393 dent->d_type, &name);
7cde47d4
GK
2394
2395 v9fs_readdir_unlock(&fidp->fs.dir);
2396
ddca7f86
MK
2397 if (len < 0) {
2398 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
2399 v9fs_string_free(&name);
ddca7f86
MK
2400 return len;
2401 }
5e4eaa79
AK
2402 count += len;
2403 v9fs_string_free(&name);
2404 saved_dir_pos = dent->d_off;
2405 }
7cde47d4
GK
2406
2407 v9fs_readdir_unlock(&fidp->fs.dir);
2408
5e4eaa79
AK
2409 if (err < 0) {
2410 return err;
2411 }
2412 return count;
c18e2f94
SK
2413}
2414
8440e22e 2415static void coroutine_fn v9fs_readdir(void *opaque)
c18e2f94
SK
2416{
2417 int32_t fid;
5e4eaa79
AK
2418 V9fsFidState *fidp;
2419 ssize_t retval = 0;
c18e2f94 2420 size_t offset = 7;
2f008a8c
AK
2421 uint64_t initial_offset;
2422 int32_t count;
2423 uint32_t max_count;
5e4eaa79 2424 V9fsPDU *pdu = opaque;
c18e2f94 2425
ddca7f86
MK
2426 retval = pdu_unmarshal(pdu, offset, "dqd", &fid,
2427 &initial_offset, &max_count);
2428 if (retval < 0) {
2429 goto out_nofid;
2430 }
c572f23a
HPB
2431 trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count);
2432
bccacf6c 2433 fidp = get_fid(pdu, fid);
84dfb926
AK
2434 if (fidp == NULL) {
2435 retval = -EINVAL;
2436 goto out_nofid;
2437 }
f314ea4e 2438 if (!fidp->fs.dir.stream) {
5e4eaa79 2439 retval = -EINVAL;
c18e2f94
SK
2440 goto out;
2441 }
5e4eaa79 2442 if (initial_offset == 0) {
bccacf6c 2443 v9fs_co_rewinddir(pdu, fidp);
c18e2f94 2444 } else {
bccacf6c 2445 v9fs_co_seekdir(pdu, fidp, initial_offset);
c18e2f94 2446 }
bccacf6c 2447 count = v9fs_do_readdir(pdu, fidp, max_count);
5e4eaa79
AK
2448 if (count < 0) {
2449 retval = count;
2450 goto out;
2451 }
ddca7f86
MK
2452 retval = pdu_marshal(pdu, offset, "d", count);
2453 if (retval < 0) {
2454 goto out;
2455 }
2456 retval += count + offset;
7999f7e1 2457 trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval);
c18e2f94 2458out:
bccacf6c 2459 put_fid(pdu, fidp);
84dfb926 2460out_nofid:
dc295f83 2461 pdu_complete(pdu, retval);
c18e2f94
SK
2462}
2463
d7a90491 2464static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
2f008a8c 2465 uint64_t off, uint32_t count,
d7a90491 2466 struct iovec *sg, int cnt)
10b468bd
AK
2467{
2468 int i, to_copy;
2469 ssize_t err = 0;
7e55d65c 2470 uint64_t write_count;
d7a90491 2471 size_t offset = 7;
10b468bd 2472
d7a90491 2473
7e55d65c 2474 if (fidp->fs.xattr.len < off) {
b858e80a 2475 return -ENOSPC;
10b468bd 2476 }
7e55d65c
LQ
2477 write_count = fidp->fs.xattr.len - off;
2478 if (write_count > count) {
2479 write_count = count;
2480 }
ddca7f86
MK
2481 err = pdu_marshal(pdu, offset, "d", write_count);
2482 if (err < 0) {
2483 return err;
2484 }
2485 err += offset;
d7a90491 2486 fidp->fs.xattr.copied_len += write_count;
10b468bd
AK
2487 /*
2488 * Now copy the content from sg list
2489 */
d7a90491
AK
2490 for (i = 0; i < cnt; i++) {
2491 if (write_count > sg[i].iov_len) {
2492 to_copy = sg[i].iov_len;
10b468bd
AK
2493 } else {
2494 to_copy = write_count;
2495 }
d7a90491 2496 memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy);
10b468bd 2497 /* updating vs->off since we are not using below */
d7a90491 2498 off += to_copy;
10b468bd
AK
2499 write_count -= to_copy;
2500 }
b858e80a 2501
d7a90491 2502 return err;
10b468bd
AK
2503}
2504
8440e22e 2505static void coroutine_fn v9fs_write(void *opaque)
9f107513 2506{
d7a90491
AK
2507 ssize_t err;
2508 int32_t fid;
2f008a8c
AK
2509 uint64_t off;
2510 uint32_t count;
d7a90491
AK
2511 int32_t len = 0;
2512 int32_t total = 0;
2513 size_t offset = 7;
16724a17 2514 size_t size;
d7a90491 2515 V9fsFidState *fidp;
ff06030f
VJ
2516 V9fsPDU *pdu = opaque;
2517 V9fsState *s = pdu->s;
302a0d3e
SH
2518 QEMUIOVector qiov_full;
2519 QEMUIOVector qiov;
8449360c 2520
ddca7f86
MK
2521 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count);
2522 if (err < 0) {
dc295f83 2523 pdu_complete(pdu, err);
0289a412 2524 return;
ddca7f86
MK
2525 }
2526 offset += err;
16724a17
GK
2527 size = count;
2528 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, &size, true);
2529 count = size;
302a0d3e 2530 trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov);
84dfb926 2531
bccacf6c 2532 fidp = get_fid(pdu, fid);
d7a90491 2533 if (fidp == NULL) {
8449360c 2534 err = -EINVAL;
84dfb926 2535 goto out_nofid;
9f107513 2536 }
d7a90491
AK
2537 if (fidp->fid_type == P9_FID_FILE) {
2538 if (fidp->fs.fd == -1) {
10b468bd
AK
2539 err = -EINVAL;
2540 goto out;
2541 }
d7a90491 2542 } else if (fidp->fid_type == P9_FID_XATTR) {
10b468bd
AK
2543 /*
2544 * setxattr operation
2545 */
302a0d3e
SH
2546 err = v9fs_xattr_write(s, pdu, fidp, off, count,
2547 qiov_full.iov, qiov_full.niov);
d7a90491 2548 goto out;
10b468bd 2549 } else {
8449360c
AL
2550 err = -EINVAL;
2551 goto out;
2552 }
302a0d3e 2553 qemu_iovec_init(&qiov, qiov_full.niov);
d7a90491 2554 do {
302a0d3e 2555 qemu_iovec_reset(&qiov);
1b093c48 2556 qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total);
d7a90491 2557 if (0) {
302a0d3e 2558 print_sg(qiov.iov, qiov.niov);
56d15a53 2559 }
d7a90491
AK
2560 /* Loop in case of EINTR */
2561 do {
302a0d3e 2562 len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off);
d7a90491
AK
2563 if (len >= 0) {
2564 off += len;
2565 total += len;
2566 }
bccacf6c 2567 } while (len == -EINTR && !pdu->cancelled);
d7a90491
AK
2568 if (len < 0) {
2569 /* IO error return the error */
2570 err = len;
302a0d3e 2571 goto out_qiov;
d7a90491 2572 }
d7a90491 2573 } while (total < count && len > 0);
302a0d3e
SH
2574
2575 offset = 7;
ddca7f86
MK
2576 err = pdu_marshal(pdu, offset, "d", total);
2577 if (err < 0) {
fdfcc9ae 2578 goto out_qiov;
ddca7f86
MK
2579 }
2580 err += offset;
7999f7e1 2581 trace_v9fs_write_return(pdu->tag, pdu->id, total, err);
302a0d3e
SH
2582out_qiov:
2583 qemu_iovec_destroy(&qiov);
8449360c 2584out:
bccacf6c 2585 put_fid(pdu, fidp);
84dfb926 2586out_nofid:
302a0d3e 2587 qemu_iovec_destroy(&qiov_full);
dc295f83 2588 pdu_complete(pdu, err);
9f107513
AL
2589}
2590
8440e22e 2591static void coroutine_fn v9fs_create(void *opaque)
5e94c103 2592{
baaa86d9
VJ
2593 int32_t fid;
2594 int err = 0;
2595 size_t offset = 7;
2596 V9fsFidState *fidp;
2597 V9fsQID qid;
2598 int32_t perm;
2599 int8_t mode;
2289be19 2600 V9fsPath path;
baaa86d9
VJ
2601 struct stat stbuf;
2602 V9fsString name;
2603 V9fsString extension;
baaa86d9
VJ
2604 int iounit;
2605 V9fsPDU *pdu = opaque;
5b3c77aa 2606 V9fsState *s = pdu->s;
c494dd6f 2607
2289be19 2608 v9fs_path_init(&path);
ddca7f86
MK
2609 v9fs_string_init(&name);
2610 v9fs_string_init(&extension);
2611 err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name,
2612 &perm, &mode, &extension);
2613 if (err < 0) {
2614 goto out_nofid;
2615 }
c572f23a
HPB
2616 trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode);
2617
fff39a7a
GK
2618 if (name_is_illegal(name.data)) {
2619 err = -ENOENT;
2620 goto out_nofid;
2621 }
2622
805b5d98
GK
2623 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2624 err = -EEXIST;
2625 goto out_nofid;
2626 }
2627
bccacf6c 2628 fidp = get_fid(pdu, fid);
baaa86d9
VJ
2629 if (fidp == NULL) {
2630 err = -EINVAL;
84dfb926 2631 goto out_nofid;
c494dd6f 2632 }
d63fb193
LQ
2633 if (fidp->fid_type != P9_FID_NONE) {
2634 err = -EINVAL;
2635 goto out;
2636 }
baaa86d9 2637 if (perm & P9_STAT_MODE_DIR) {
bccacf6c 2638 err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777,
02cb7f3a 2639 fidp->uid, -1, &stbuf);
baaa86d9
VJ
2640 if (err < 0) {
2641 goto out;
2642 }
bccacf6c 2643 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2289be19
AK
2644 if (err < 0) {
2645 goto out;
2646 }
5b3c77aa 2647 v9fs_path_write_lock(s);
2289be19 2648 v9fs_path_copy(&fidp->path, &path);
5b3c77aa 2649 v9fs_path_unlock(s);
bccacf6c 2650 err = v9fs_co_opendir(pdu, fidp);
baaa86d9
VJ
2651 if (err < 0) {
2652 goto out;
2653 }
2654 fidp->fid_type = P9_FID_DIR;
2655 } else if (perm & P9_STAT_MODE_SYMLINK) {
bccacf6c 2656 err = v9fs_co_symlink(pdu, fidp, &name,
02cb7f3a 2657 extension.data, -1 , &stbuf);
baaa86d9 2658 if (err < 0) {
baaa86d9
VJ
2659 goto out;
2660 }
bccacf6c 2661 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2289be19
AK
2662 if (err < 0) {
2663 goto out;
2664 }
5b3c77aa 2665 v9fs_path_write_lock(s);
2289be19 2666 v9fs_path_copy(&fidp->path, &path);
5b3c77aa 2667 v9fs_path_unlock(s);
baaa86d9 2668 } else if (perm & P9_STAT_MODE_LINK) {
2289be19 2669 int32_t ofid = atoi(extension.data);
bccacf6c 2670 V9fsFidState *ofidp = get_fid(pdu, ofid);
2289be19 2671 if (ofidp == NULL) {
baaa86d9
VJ
2672 err = -EINVAL;
2673 goto out;
2674 }
bccacf6c
AK
2675 err = v9fs_co_link(pdu, ofidp, fidp, &name);
2676 put_fid(pdu, ofidp);
2289be19
AK
2677 if (err < 0) {
2678 goto out;
2679 }
bccacf6c 2680 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
baaa86d9 2681 if (err < 0) {
2289be19 2682 fidp->fid_type = P9_FID_NONE;
baaa86d9 2683 goto out;
c494dd6f 2684 }
5b3c77aa 2685 v9fs_path_write_lock(s);
2289be19 2686 v9fs_path_copy(&fidp->path, &path);
5b3c77aa 2687 v9fs_path_unlock(s);
bccacf6c 2688 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
02cb7f3a
AK
2689 if (err < 0) {
2690 fidp->fid_type = P9_FID_NONE;
2691 goto out;
2692 }
baaa86d9 2693 } else if (perm & P9_STAT_MODE_DEVICE) {
c494dd6f
AL
2694 char ctype;
2695 uint32_t major, minor;
2696 mode_t nmode = 0;
2697
baaa86d9 2698 if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) {
c494dd6f 2699 err = -errno;
baaa86d9 2700 goto out;
c494dd6f
AL
2701 }
2702
2703 switch (ctype) {
2704 case 'c':
2705 nmode = S_IFCHR;
2706 break;
2707 case 'b':
2708 nmode = S_IFBLK;
2709 break;
2710 default:
2711 err = -EIO;
baaa86d9
VJ
2712 goto out;
2713 }
c1568af5 2714
baaa86d9 2715 nmode |= perm & 0777;
bccacf6c 2716 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
02cb7f3a 2717 makedev(major, minor), nmode, &stbuf);
baaa86d9
VJ
2718 if (err < 0) {
2719 goto out;
2720 }
bccacf6c 2721 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2289be19
AK
2722 if (err < 0) {
2723 goto out;
2724 }
5b3c77aa 2725 v9fs_path_write_lock(s);
2289be19 2726 v9fs_path_copy(&fidp->path, &path);
5b3c77aa 2727 v9fs_path_unlock(s);
baaa86d9 2728 } else if (perm & P9_STAT_MODE_NAMED_PIPE) {
bccacf6c 2729 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
02cb7f3a 2730 0, S_IFIFO | (perm & 0777), &stbuf);
baaa86d9
VJ
2731 if (err < 0) {
2732 goto out;
2733 }
bccacf6c 2734 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2289be19
AK
2735 if (err < 0) {
2736 goto out;
2737 }
5b3c77aa 2738 v9fs_path_write_lock(s);
2289be19 2739 v9fs_path_copy(&fidp->path, &path);
5b3c77aa 2740 v9fs_path_unlock(s);
baaa86d9 2741 } else if (perm & P9_STAT_MODE_SOCKET) {
bccacf6c 2742 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
02cb7f3a 2743 0, S_IFSOCK | (perm & 0777), &stbuf);
baaa86d9
VJ
2744 if (err < 0) {
2745 goto out;
2746 }
bccacf6c 2747 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2289be19
AK
2748 if (err < 0) {
2749 goto out;
2750 }
5b3c77aa 2751 v9fs_path_write_lock(s);
2289be19 2752 v9fs_path_copy(&fidp->path, &path);
5b3c77aa 2753 v9fs_path_unlock(s);
baaa86d9 2754 } else {
bccacf6c 2755 err = v9fs_co_open2(pdu, fidp, &name, -1,
02cb7f3a 2756 omode_to_uflags(mode)|O_CREAT, perm, &stbuf);
baaa86d9
VJ
2757 if (err < 0) {
2758 goto out;
2759 }
2760 fidp->fid_type = P9_FID_FILE;
7a462745
AK
2761 fidp->open_flags = omode_to_uflags(mode);
2762 if (fidp->open_flags & O_EXCL) {
2763 /*
2764 * We let the host file system do O_EXCL check
2765 * We should not reclaim such fd
2766 */
2767 fidp->flags |= FID_NON_RECLAIMABLE;
2768 }
c494dd6f 2769 }
bccacf6c 2770 iounit = get_iounit(pdu, &fidp->path);
3b5ee9e8
AM
2771 err = stat_to_qid(pdu, &stbuf, &qid);
2772 if (err < 0) {
2773 goto out;
2774 }
ddca7f86
MK
2775 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
2776 if (err < 0) {
2777 goto out;
2778 }
2779 err += offset;
7999f7e1
AK
2780 trace_v9fs_create_return(pdu->tag, pdu->id,
2781 qid.type, qid.version, qid.path, iounit);
c494dd6f 2782out:
bccacf6c 2783 put_fid(pdu, fidp);
84dfb926 2784out_nofid:
dc295f83 2785 pdu_complete(pdu, err);
baaa86d9
VJ
2786 v9fs_string_free(&name);
2787 v9fs_string_free(&extension);
2289be19 2788 v9fs_path_free(&path);
9f107513
AL
2789}
2790
8440e22e 2791static void coroutine_fn v9fs_symlink(void *opaque)
08c60fc9 2792{
ff06030f 2793 V9fsPDU *pdu = opaque;
3fa2a8d1
VJ
2794 V9fsString name;
2795 V9fsString symname;
3fa2a8d1
VJ
2796 V9fsFidState *dfidp;
2797 V9fsQID qid;
2798 struct stat stbuf;
08c60fc9 2799 int32_t dfid;
08c60fc9
VJ
2800 int err = 0;
2801 gid_t gid;
3fa2a8d1 2802 size_t offset = 7;
08c60fc9 2803
ddca7f86
MK
2804 v9fs_string_init(&name);
2805 v9fs_string_init(&symname);
2806 err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid);
2807 if (err < 0) {
2808 goto out_nofid;
2809 }
c572f23a 2810 trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid);
08c60fc9 2811
fff39a7a
GK
2812 if (name_is_illegal(name.data)) {
2813 err = -ENOENT;
2814 goto out_nofid;
2815 }
2816
805b5d98
GK
2817 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2818 err = -EEXIST;
2819 goto out_nofid;
2820 }
2821
bccacf6c 2822 dfidp = get_fid(pdu, dfid);
3fa2a8d1 2823 if (dfidp == NULL) {
08c60fc9 2824 err = -EINVAL;
84dfb926 2825 goto out_nofid;
08c60fc9 2826 }
bccacf6c 2827 err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf);
3fa2a8d1
VJ
2828 if (err < 0) {
2829 goto out;
2830 }
3b5ee9e8
AM
2831 err = stat_to_qid(pdu, &stbuf, &qid);
2832 if (err < 0) {
2833 goto out;
2834 }
ddca7f86
MK
2835 err = pdu_marshal(pdu, offset, "Q", &qid);
2836 if (err < 0) {
2837 goto out;
2838 }
2839 err += offset;
7999f7e1
AK
2840 trace_v9fs_symlink_return(pdu->tag, pdu->id,
2841 qid.type, qid.version, qid.path);
08c60fc9 2842out:
bccacf6c 2843 put_fid(pdu, dfidp);
84dfb926 2844out_nofid:
dc295f83 2845 pdu_complete(pdu, err);
3fa2a8d1
VJ
2846 v9fs_string_free(&name);
2847 v9fs_string_free(&symname);
08c60fc9
VJ
2848}
2849
a1bf8b74 2850static void coroutine_fn v9fs_flush(void *opaque)
9f107513 2851{
ddca7f86 2852 ssize_t err;
bccacf6c
AK
2853 int16_t tag;
2854 size_t offset = 7;
d5f2af7b 2855 V9fsPDU *cancel_pdu = NULL;
ff06030f
VJ
2856 V9fsPDU *pdu = opaque;
2857 V9fsState *s = pdu->s;
bccacf6c 2858
ddca7f86
MK
2859 err = pdu_unmarshal(pdu, offset, "w", &tag);
2860 if (err < 0) {
dc295f83 2861 pdu_complete(pdu, err);
ddca7f86
MK
2862 return;
2863 }
c572f23a 2864 trace_v9fs_flush(pdu->tag, pdu->id, tag);
bccacf6c 2865
d5f2af7b 2866 if (pdu->tag == tag) {
3dc6f869 2867 warn_report("the guest sent a self-referencing 9P flush request");
d5f2af7b
GK
2868 } else {
2869 QLIST_FOREACH(cancel_pdu, &s->active_list, next) {
2870 if (cancel_pdu->tag == tag) {
2871 break;
2872 }
bccacf6c
AK
2873 }
2874 }
2875 if (cancel_pdu) {
2876 cancel_pdu->cancelled = 1;
2877 /*
2878 * Wait for pdu to complete.
2879 */
1ace7cea 2880 qemu_co_queue_wait(&cancel_pdu->complete, NULL);
18adde86
GK
2881 if (!qemu_co_queue_next(&cancel_pdu->complete)) {
2882 cancel_pdu->cancelled = 0;
2883 pdu_free(cancel_pdu);
2884 }
bccacf6c 2885 }
dc295f83 2886 pdu_complete(pdu, 7);
9f107513
AL
2887}
2888
8440e22e 2889static void coroutine_fn v9fs_link(void *opaque)
b2c224be 2890{
ff06030f 2891 V9fsPDU *pdu = opaque;
b2c224be
VJ
2892 int32_t dfid, oldfid;
2893 V9fsFidState *dfidp, *oldfidp;
3a93113a 2894 V9fsString name;
b2c224be
VJ
2895 size_t offset = 7;
2896 int err = 0;
2897
ddca7f86
MK
2898 v9fs_string_init(&name);
2899 err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
2900 if (err < 0) {
2901 goto out_nofid;
2902 }
c572f23a 2903 trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data);
b2c224be 2904
fff39a7a
GK
2905 if (name_is_illegal(name.data)) {
2906 err = -ENOENT;
2907 goto out_nofid;
2908 }
2909
805b5d98
GK
2910 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2911 err = -EEXIST;
2912 goto out_nofid;
2913 }
2914
bccacf6c 2915 dfidp = get_fid(pdu, dfid);
b2c224be 2916 if (dfidp == NULL) {
ffd66876 2917 err = -ENOENT;
84dfb926 2918 goto out_nofid;
b2c224be
VJ
2919 }
2920
bccacf6c 2921 oldfidp = get_fid(pdu, oldfid);
b2c224be 2922 if (oldfidp == NULL) {
ffd66876 2923 err = -ENOENT;
b2c224be
VJ
2924 goto out;
2925 }
bccacf6c 2926 err = v9fs_co_link(pdu, oldfidp, dfidp, &name);
ffd66876
VJ
2927 if (!err) {
2928 err = offset;
b2c224be 2929 }
4c158678 2930 put_fid(pdu, oldfidp);
b2c224be 2931out:
bccacf6c 2932 put_fid(pdu, dfidp);
84dfb926 2933out_nofid:
b2c224be 2934 v9fs_string_free(&name);
dc295f83 2935 pdu_complete(pdu, err);
b2c224be
VJ
2936}
2937
532decb7 2938/* Only works with path name based fid */
8440e22e 2939static void coroutine_fn v9fs_remove(void *opaque)
9f107513 2940{
5bae1900 2941 int32_t fid;
5bae1900 2942 int err = 0;
ae1ef571
VJ
2943 size_t offset = 7;
2944 V9fsFidState *fidp;
2945 V9fsPDU *pdu = opaque;
5bae1900 2946
ddca7f86
MK
2947 err = pdu_unmarshal(pdu, offset, "d", &fid);
2948 if (err < 0) {
2949 goto out_nofid;
2950 }
c572f23a 2951 trace_v9fs_remove(pdu->tag, pdu->id, fid);
5bae1900 2952
bccacf6c 2953 fidp = get_fid(pdu, fid);
ae1ef571 2954 if (fidp == NULL) {
5bae1900 2955 err = -EINVAL;
84dfb926 2956 goto out_nofid;
9f107513 2957 }
532decb7 2958 /* if fs driver is not path based, return EOPNOTSUPP */
c98f1d4a 2959 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
532decb7
AK
2960 err = -EOPNOTSUPP;
2961 goto out_err;
2962 }
7a462745
AK
2963 /*
2964 * IF the file is unlinked, we cannot reopen
2965 * the file later. So don't reclaim fd
2966 */
bccacf6c 2967 err = v9fs_mark_fids_unreclaim(pdu, &fidp->path);
7a462745
AK
2968 if (err < 0) {
2969 goto out_err;
2970 }
bccacf6c 2971 err = v9fs_co_remove(pdu, &fidp->path);
ae1ef571
VJ
2972 if (!err) {
2973 err = offset;
2974 }
7a462745 2975out_err:
ae1ef571 2976 /* For TREMOVE we need to clunk the fid even on failed remove */
84dfb926 2977 clunk_fid(pdu->s, fidp->fid);
bccacf6c 2978 put_fid(pdu, fidp);
84dfb926 2979out_nofid:
dc295f83 2980 pdu_complete(pdu, err);
9f107513
AL
2981}
2982
8440e22e 2983static void coroutine_fn v9fs_unlinkat(void *opaque)
7834cf77
AK
2984{
2985 int err = 0;
2986 V9fsString name;
67e87345 2987 int32_t dfid, flags, rflags = 0;
7834cf77 2988 size_t offset = 7;
2289be19 2989 V9fsPath path;
7834cf77
AK
2990 V9fsFidState *dfidp;
2991 V9fsPDU *pdu = opaque;
7834cf77 2992
ddca7f86
MK
2993 v9fs_string_init(&name);
2994 err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags);
2995 if (err < 0) {
2996 goto out_nofid;
2997 }
fff39a7a
GK
2998
2999 if (name_is_illegal(name.data)) {
3000 err = -ENOENT;
3001 goto out_nofid;
3002 }
3003
805b5d98
GK
3004 if (!strcmp(".", name.data)) {
3005 err = -EINVAL;
3006 goto out_nofid;
3007 }
3008
3009 if (!strcmp("..", name.data)) {
3010 err = -ENOTEMPTY;
3011 goto out_nofid;
3012 }
3013
67e87345
KF
3014 if (flags & ~P9_DOTL_AT_REMOVEDIR) {
3015 err = -EINVAL;
3016 goto out_nofid;
3017 }
3018
3019 if (flags & P9_DOTL_AT_REMOVEDIR) {
3020 rflags |= AT_REMOVEDIR;
3021 }
3022
bccacf6c 3023 dfidp = get_fid(pdu, dfid);
7834cf77
AK
3024 if (dfidp == NULL) {
3025 err = -EINVAL;
3026 goto out_nofid;
3027 }
7834cf77
AK
3028 /*
3029 * IF the file is unlinked, we cannot reopen
3030 * the file later. So don't reclaim fd
3031 */
2289be19 3032 v9fs_path_init(&path);
bccacf6c 3033 err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path);
2289be19
AK
3034 if (err < 0) {
3035 goto out_err;
3036 }
bccacf6c 3037 err = v9fs_mark_fids_unreclaim(pdu, &path);
7834cf77
AK
3038 if (err < 0) {
3039 goto out_err;
3040 }
67e87345 3041 err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, rflags);
7834cf77
AK
3042 if (!err) {
3043 err = offset;
3044 }
3045out_err:
bccacf6c 3046 put_fid(pdu, dfidp);
2289be19 3047 v9fs_path_free(&path);
7834cf77 3048out_nofid:
dc295f83 3049 pdu_complete(pdu, err);
7834cf77
AK
3050 v9fs_string_free(&name);
3051}
3052
2289be19
AK
3053
3054/* Only works with path name based fid */
8440e22e
GK
3055static int coroutine_fn v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp,
3056 int32_t newdirfid,
3057 V9fsString *name)
8cf89e00 3058{
c7b4b0b3 3059 int err = 0;
2289be19
AK
3060 V9fsPath new_path;
3061 V9fsFidState *tfidp;
bccacf6c 3062 V9fsState *s = pdu->s;
84dfb926 3063 V9fsFidState *dirfidp = NULL;
8cf89e00 3064
2289be19 3065 v9fs_path_init(&new_path);
930b1e17 3066 if (newdirfid != -1) {
bccacf6c 3067 dirfidp = get_fid(pdu, newdirfid);
c7b4b0b3 3068 if (dirfidp == NULL) {
b858e80a 3069 return -ENOENT;
c7b4b0b3 3070 }
49dd946b
GK
3071 if (fidp->fid_type != P9_FID_NONE) {
3072 err = -EINVAL;
3073 goto out;
3074 }
4fa62005
GK
3075 err = v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path);
3076 if (err < 0) {
3077 goto out;
3078 }
c7b4b0b3 3079 } else {
4d8bc733
JD
3080 char *dir_name = g_path_get_dirname(fidp->path.data);
3081 V9fsPath dir_path;
3082
3083 v9fs_path_init(&dir_path);
3084 v9fs_path_sprintf(&dir_path, "%s", dir_name);
3085 g_free(dir_name);
3086
3087 err = v9fs_co_name_to_path(pdu, &dir_path, name->data, &new_path);
3088 v9fs_path_free(&dir_path);
4fa62005
GK
3089 if (err < 0) {
3090 goto out;
3091 }
c7b4b0b3 3092 }
bccacf6c 3093 err = v9fs_co_rename(pdu, &fidp->path, &new_path);
2289be19
AK
3094 if (err < 0) {
3095 goto out;
3096 }
3097 /*
3098 * Fixup fid's pointing to the old name to
3099 * start pointing to the new name
3100 */
3101 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
3102 if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) {
3103 /* replace the name */
3104 v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data));
8cf89e00
AL
3105 }
3106 }
c7b4b0b3 3107out:
84dfb926 3108 if (dirfidp) {
bccacf6c 3109 put_fid(pdu, dirfidp);
84dfb926 3110 }
2289be19 3111 v9fs_path_free(&new_path);
c7b4b0b3
MK
3112 return err;
3113}
3114
532decb7 3115/* Only works with path name based fid */
8440e22e 3116static void coroutine_fn v9fs_rename(void *opaque)
c7b4b0b3
MK
3117{
3118 int32_t fid;
c7b4b0b3 3119 ssize_t err = 0;
930b1e17
AK
3120 size_t offset = 7;
3121 V9fsString name;
3122 int32_t newdirfid;
3123 V9fsFidState *fidp;
3124 V9fsPDU *pdu = opaque;
3125 V9fsState *s = pdu->s;
c7b4b0b3 3126
ddca7f86
MK
3127 v9fs_string_init(&name);
3128 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name);
3129 if (err < 0) {
3130 goto out_nofid;
3131 }
fff39a7a
GK
3132
3133 if (name_is_illegal(name.data)) {
3134 err = -ENOENT;
3135 goto out_nofid;
3136 }
3137
805b5d98
GK
3138 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3139 err = -EISDIR;
3140 goto out_nofid;
3141 }
3142
bccacf6c 3143 fidp = get_fid(pdu, fid);
930b1e17 3144 if (fidp == NULL) {
c7b4b0b3 3145 err = -ENOENT;
84dfb926 3146 goto out_nofid;
c7b4b0b3 3147 }
49dd946b
GK
3148 if (fidp->fid_type != P9_FID_NONE) {
3149 err = -EINVAL;
3150 goto out;
3151 }
532decb7 3152 /* if fs driver is not path based, return EOPNOTSUPP */
c98f1d4a 3153 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
532decb7
AK
3154 err = -EOPNOTSUPP;
3155 goto out;
3156 }
3157 v9fs_path_write_lock(s);
bccacf6c 3158 err = v9fs_complete_rename(pdu, fidp, newdirfid, &name);
532decb7 3159 v9fs_path_unlock(s);
930b1e17
AK
3160 if (!err) {
3161 err = offset;
3162 }
532decb7 3163out:
bccacf6c 3164 put_fid(pdu, fidp);
84dfb926 3165out_nofid:
dc295f83 3166 pdu_complete(pdu, err);
930b1e17 3167 v9fs_string_free(&name);
c7b4b0b3
MK
3168}
3169
4fa62005
GK
3170static int coroutine_fn v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir,
3171 V9fsString *old_name,
3172 V9fsPath *newdir,
3173 V9fsString *new_name)
2289be19
AK
3174{
3175 V9fsFidState *tfidp;
3176 V9fsPath oldpath, newpath;
bccacf6c 3177 V9fsState *s = pdu->s;
4fa62005 3178 int err;
2289be19
AK
3179
3180 v9fs_path_init(&oldpath);
3181 v9fs_path_init(&newpath);
4fa62005
GK
3182 err = v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath);
3183 if (err < 0) {
3184 goto out;
3185 }
3186 err = v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath);
3187 if (err < 0) {
3188 goto out;
3189 }
2289be19
AK
3190
3191 /*
3192 * Fixup fid's pointing to the old name to
3193 * start pointing to the new name
3194 */
3195 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
3196 if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) {
3197 /* replace the name */
3198 v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data));
3199 }
3200 }
4fa62005 3201out:
2289be19
AK
3202 v9fs_path_free(&oldpath);
3203 v9fs_path_free(&newpath);
4fa62005 3204 return err;
2289be19
AK
3205}
3206
8440e22e
GK
3207static int coroutine_fn v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid,
3208 V9fsString *old_name,
3209 int32_t newdirfid,
3210 V9fsString *new_name)
89bf6593
AK
3211{
3212 int err = 0;
bccacf6c 3213 V9fsState *s = pdu->s;
89bf6593
AK
3214 V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL;
3215
bccacf6c 3216 olddirfidp = get_fid(pdu, olddirfid);
89bf6593
AK
3217 if (olddirfidp == NULL) {
3218 err = -ENOENT;
3219 goto out;
3220 }
89bf6593 3221 if (newdirfid != -1) {
bccacf6c 3222 newdirfidp = get_fid(pdu, newdirfid);
89bf6593
AK
3223 if (newdirfidp == NULL) {
3224 err = -ENOENT;
3225 goto out;
3226 }
89bf6593 3227 } else {
bccacf6c 3228 newdirfidp = get_fid(pdu, olddirfid);
89bf6593
AK
3229 }
3230
bccacf6c 3231 err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name,
2289be19
AK
3232 &newdirfidp->path, new_name);
3233 if (err < 0) {
3234 goto out;
89bf6593 3235 }
c98f1d4a 3236 if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
532decb7 3237 /* Only for path based fid we need to do the below fixup */
4fa62005
GK
3238 err = v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name,
3239 &newdirfidp->path, new_name);
532decb7 3240 }
89bf6593
AK
3241out:
3242 if (olddirfidp) {
bccacf6c 3243 put_fid(pdu, olddirfidp);
89bf6593
AK
3244 }
3245 if (newdirfidp) {
bccacf6c 3246 put_fid(pdu, newdirfidp);
89bf6593 3247 }
89bf6593
AK
3248 return err;
3249}
3250
8440e22e 3251static void coroutine_fn v9fs_renameat(void *opaque)
89bf6593
AK
3252{
3253 ssize_t err = 0;
3254 size_t offset = 7;
3255 V9fsPDU *pdu = opaque;
3256 V9fsState *s = pdu->s;
3257 int32_t olddirfid, newdirfid;
3258 V9fsString old_name, new_name;
3259
ddca7f86
MK
3260 v9fs_string_init(&old_name);
3261 v9fs_string_init(&new_name);
3262 err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid,
3263 &old_name, &newdirfid, &new_name);
3264 if (err < 0) {
3265 goto out_err;
3266 }
89bf6593 3267
fff39a7a
GK
3268 if (name_is_illegal(old_name.data) || name_is_illegal(new_name.data)) {
3269 err = -ENOENT;
3270 goto out_err;
3271 }
3272
805b5d98
GK
3273 if (!strcmp(".", old_name.data) || !strcmp("..", old_name.data) ||
3274 !strcmp(".", new_name.data) || !strcmp("..", new_name.data)) {
3275 err = -EISDIR;
3276 goto out_err;
3277 }
3278
532decb7 3279 v9fs_path_write_lock(s);
bccacf6c
AK
3280 err = v9fs_complete_renameat(pdu, olddirfid,
3281 &old_name, newdirfid, &new_name);
532decb7 3282 v9fs_path_unlock(s);
89bf6593
AK
3283 if (!err) {
3284 err = offset;
3285 }
ddca7f86
MK
3286
3287out_err:
dc295f83 3288 pdu_complete(pdu, err);
89bf6593
AK
3289 v9fs_string_free(&old_name);
3290 v9fs_string_free(&new_name);
3291}
3292
8440e22e 3293static void coroutine_fn v9fs_wstat(void *opaque)
8cf89e00 3294{
b81d685e
AK
3295 int32_t fid;
3296 int err = 0;
3297 int16_t unused;
3298 V9fsStat v9stat;
3299 size_t offset = 7;
3300 struct stat stbuf;
3301 V9fsFidState *fidp;
3302 V9fsPDU *pdu = opaque;
1d203986 3303 V9fsState *s = pdu->s;
8cf89e00 3304
ddca7f86
MK
3305 v9fs_stat_init(&v9stat);
3306 err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat);
3307 if (err < 0) {
3308 goto out_nofid;
3309 }
c572f23a
HPB
3310 trace_v9fs_wstat(pdu->tag, pdu->id, fid,
3311 v9stat.mode, v9stat.atime, v9stat.mtime);
84dfb926 3312
bccacf6c 3313 fidp = get_fid(pdu, fid);
b81d685e
AK
3314 if (fidp == NULL) {
3315 err = -EINVAL;
84dfb926 3316 goto out_nofid;
8cf89e00 3317 }
b81d685e
AK
3318 /* do we need to sync the file? */
3319 if (donttouch_stat(&v9stat)) {
bccacf6c 3320 err = v9fs_co_fsync(pdu, fidp, 0);
8cf89e00
AL
3321 goto out;
3322 }
b81d685e
AK
3323 if (v9stat.mode != -1) {
3324 uint32_t v9_mode;
bccacf6c 3325 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
b81d685e
AK
3326 if (err < 0) {
3327 goto out;
3328 }
3329 v9_mode = stat_to_v9mode(&stbuf);
3330 if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
3331 (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
3332 /* Attempting to change the type */
3333 err = -EIO;
3334 goto out;
3335 }
bccacf6c 3336 err = v9fs_co_chmod(pdu, &fidp->path,
b81d685e
AK
3337 v9mode_to_mode(v9stat.mode,
3338 &v9stat.extension));
3339 if (err < 0) {
3340 goto out;
3341 }
3342 }
3343 if (v9stat.mtime != -1 || v9stat.atime != -1) {
8fc39ae4 3344 struct timespec times[2];
b81d685e
AK
3345 if (v9stat.atime != -1) {
3346 times[0].tv_sec = v9stat.atime;
8fc39ae4
SK
3347 times[0].tv_nsec = 0;
3348 } else {
3349 times[0].tv_nsec = UTIME_OMIT;
3350 }
b81d685e
AK
3351 if (v9stat.mtime != -1) {
3352 times[1].tv_sec = v9stat.mtime;
8fc39ae4
SK
3353 times[1].tv_nsec = 0;
3354 } else {
3355 times[1].tv_nsec = UTIME_OMIT;
3356 }
bccacf6c 3357 err = v9fs_co_utimensat(pdu, &fidp->path, times);
b81d685e
AK
3358 if (err < 0) {
3359 goto out;
8cf89e00
AL
3360 }
3361 }
b81d685e 3362 if (v9stat.n_gid != -1 || v9stat.n_uid != -1) {
bccacf6c 3363 err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid);
b81d685e 3364 if (err < 0) {
8cf89e00 3365 goto out;
b81d685e 3366 }
8cf89e00 3367 }
b81d685e 3368 if (v9stat.name.size != 0) {
1d203986 3369 v9fs_path_write_lock(s);
bccacf6c 3370 err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name);
1d203986 3371 v9fs_path_unlock(s);
b81d685e
AK
3372 if (err < 0) {
3373 goto out;
3374 }
8cf89e00 3375 }
b81d685e 3376 if (v9stat.length != -1) {
bccacf6c 3377 err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length);
b81d685e
AK
3378 if (err < 0) {
3379 goto out;
3380 }
8cf89e00 3381 }
b81d685e 3382 err = offset;
8cf89e00 3383out:
bccacf6c 3384 put_fid(pdu, fidp);
84dfb926 3385out_nofid:
b81d685e 3386 v9fs_stat_free(&v9stat);
dc295f83 3387 pdu_complete(pdu, err);
9f107513
AL
3388}
3389
88a4763e
AK
3390static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf)
3391{
3392 uint32_t f_type;
3393 uint32_t f_bsize;
3394 uint64_t f_blocks;
3395 uint64_t f_bfree;
3396 uint64_t f_bavail;
3397 uint64_t f_files;
3398 uint64_t f_ffree;
3399 uint64_t fsid_val;
3400 uint32_t f_namelen;
3401 size_t offset = 7;
5e94c103
MK
3402 int32_t bsize_factor;
3403
5e94c103
MK
3404 /*
3405 * compute bsize factor based on host file system block size
3406 * and client msize
3407 */
88a4763e 3408 bsize_factor = (s->msize - P9_IOHDRSZ)/stbuf->f_bsize;
5e94c103
MK
3409 if (!bsize_factor) {
3410 bsize_factor = 1;
3411 }
88a4763e
AK
3412 f_type = stbuf->f_type;
3413 f_bsize = stbuf->f_bsize;
3414 f_bsize *= bsize_factor;
5e94c103
MK
3415 /*
3416 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
3417 * adjust(divide) the number of blocks, free blocks and available
3418 * blocks by bsize factor
3419 */
88a4763e
AK
3420 f_blocks = stbuf->f_blocks/bsize_factor;
3421 f_bfree = stbuf->f_bfree/bsize_factor;
3422 f_bavail = stbuf->f_bavail/bsize_factor;
3423 f_files = stbuf->f_files;
3424 f_ffree = stbuf->f_ffree;
3425 fsid_val = (unsigned int) stbuf->f_fsid.__val[0] |
3426 (unsigned long long)stbuf->f_fsid.__val[1] << 32;
3427 f_namelen = stbuf->f_namelen;
be940c87 3428
88a4763e
AK
3429 return pdu_marshal(pdu, offset, "ddqqqqqqd",
3430 f_type, f_bsize, f_blocks, f_bfree,
3431 f_bavail, f_files, f_ffree,
3432 fsid_val, f_namelen);
be940c87
MK
3433}
3434
8440e22e 3435static void coroutine_fn v9fs_statfs(void *opaque)
be940c87 3436{
88a4763e
AK
3437 int32_t fid;
3438 ssize_t retval = 0;
3439 size_t offset = 7;
3440 V9fsFidState *fidp;
3441 struct statfs stbuf;
ff06030f
VJ
3442 V9fsPDU *pdu = opaque;
3443 V9fsState *s = pdu->s;
be940c87 3444
ddca7f86
MK
3445 retval = pdu_unmarshal(pdu, offset, "d", &fid);
3446 if (retval < 0) {
3447 goto out_nofid;
3448 }
bccacf6c 3449 fidp = get_fid(pdu, fid);
88a4763e
AK
3450 if (fidp == NULL) {
3451 retval = -ENOENT;
84dfb926 3452 goto out_nofid;
be940c87 3453 }
bccacf6c 3454 retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf);
88a4763e
AK
3455 if (retval < 0) {
3456 goto out;
3457 }
ddca7f86
MK
3458 retval = v9fs_fill_statfs(s, pdu, &stbuf);
3459 if (retval < 0) {
3460 goto out;
3461 }
3462 retval += offset;
be940c87 3463out:
bccacf6c 3464 put_fid(pdu, fidp);
84dfb926 3465out_nofid:
dc295f83 3466 pdu_complete(pdu, retval);
be940c87
MK
3467}
3468
8440e22e 3469static void coroutine_fn v9fs_mknod(void *opaque)
5268cecc 3470{
1b733fed
AK
3471
3472 int mode;
3473 gid_t gid;
5268cecc 3474 int32_t fid;
1b733fed 3475 V9fsQID qid;
5268cecc 3476 int err = 0;
5268cecc 3477 int major, minor;
1b733fed
AK
3478 size_t offset = 7;
3479 V9fsString name;
3480 struct stat stbuf;
1b733fed
AK
3481 V9fsFidState *fidp;
3482 V9fsPDU *pdu = opaque;
5268cecc 3483
ddca7f86
MK
3484 v9fs_string_init(&name);
3485 err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode,
3486 &major, &minor, &gid);
3487 if (err < 0) {
3488 goto out_nofid;
3489 }
c572f23a 3490 trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor);
5268cecc 3491
fff39a7a
GK
3492 if (name_is_illegal(name.data)) {
3493 err = -ENOENT;
3494 goto out_nofid;
3495 }
3496
805b5d98
GK
3497 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3498 err = -EEXIST;
3499 goto out_nofid;
3500 }
3501
bccacf6c 3502 fidp = get_fid(pdu, fid);
5268cecc
MK
3503 if (fidp == NULL) {
3504 err = -ENOENT;
84dfb926 3505 goto out_nofid;
5268cecc 3506 }
bccacf6c 3507 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid,
02cb7f3a 3508 makedev(major, minor), mode, &stbuf);
1b733fed
AK
3509 if (err < 0) {
3510 goto out;
3511 }
3b5ee9e8
AM
3512 err = stat_to_qid(pdu, &stbuf, &qid);
3513 if (err < 0) {
3514 goto out;
3515 }
ddca7f86
MK
3516 err = pdu_marshal(pdu, offset, "Q", &qid);
3517 if (err < 0) {
3518 goto out;
3519 }
3520 err += offset;
7999f7e1
AK
3521 trace_v9fs_mknod_return(pdu->tag, pdu->id,
3522 qid.type, qid.version, qid.path);
5268cecc 3523out:
bccacf6c 3524 put_fid(pdu, fidp);
84dfb926 3525out_nofid:
dc295f83 3526 pdu_complete(pdu, err);
1b733fed 3527 v9fs_string_free(&name);
5268cecc
MK
3528}
3529
82cc3ee8
MK
3530/*
3531 * Implement posix byte range locking code
3532 * Server side handling of locking code is very simple, because 9p server in
3533 * QEMU can handle only one client. And most of the lock handling
3534 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
3535 * do any thing in * qemu 9p server side lock code path.
3536 * So when a TLOCK request comes, always return success
3537 */
8440e22e 3538static void coroutine_fn v9fs_lock(void *opaque)
82cc3ee8 3539{
ddca7f86 3540 V9fsFlock flock;
0c27bf2a
AK
3541 size_t offset = 7;
3542 struct stat stbuf;
3543 V9fsFidState *fidp;
3544 int32_t fid, err = 0;
ff06030f 3545 V9fsPDU *pdu = opaque;
82cc3ee8 3546
ddca7f86
MK
3547 v9fs_string_init(&flock.client_id);
3548 err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type,
3549 &flock.flags, &flock.start, &flock.length,
3550 &flock.proc_id, &flock.client_id);
3551 if (err < 0) {
3552 goto out_nofid;
3553 }
c572f23a 3554 trace_v9fs_lock(pdu->tag, pdu->id, fid,
ddca7f86 3555 flock.type, flock.start, flock.length);
c572f23a 3556
82cc3ee8
MK
3557
3558 /* We support only block flag now (that too ignored currently) */
ddca7f86 3559 if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) {
82cc3ee8 3560 err = -EINVAL;
84dfb926 3561 goto out_nofid;
82cc3ee8 3562 }
bccacf6c 3563 fidp = get_fid(pdu, fid);
0c27bf2a 3564 if (fidp == NULL) {
82cc3ee8 3565 err = -ENOENT;
84dfb926 3566 goto out_nofid;
82cc3ee8 3567 }
cc720ddb 3568 err = v9fs_co_fstat(pdu, fidp, &stbuf);
82cc3ee8 3569 if (err < 0) {
82cc3ee8
MK
3570 goto out;
3571 }
4bae2b39
PB
3572 err = pdu_marshal(pdu, offset, "b", P9_LOCK_SUCCESS);
3573 if (err < 0) {
3574 goto out;
3575 }
3576 err += offset;
3577 trace_v9fs_lock_return(pdu->tag, pdu->id, P9_LOCK_SUCCESS);
82cc3ee8 3578out:
bccacf6c 3579 put_fid(pdu, fidp);
84dfb926 3580out_nofid:
dc295f83 3581 pdu_complete(pdu, err);
ddca7f86 3582 v9fs_string_free(&flock.client_id);
82cc3ee8
MK
3583}
3584
8f354003
MK
3585/*
3586 * When a TGETLOCK request comes, always return success because all lock
3587 * handling is done by client's VFS layer.
3588 */
8440e22e 3589static void coroutine_fn v9fs_getlock(void *opaque)
8f354003 3590{
e4e414a4
AK
3591 size_t offset = 7;
3592 struct stat stbuf;
3593 V9fsFidState *fidp;
ddca7f86 3594 V9fsGetlock glock;
e4e414a4 3595 int32_t fid, err = 0;
ff06030f 3596 V9fsPDU *pdu = opaque;
8f354003 3597
ddca7f86
MK
3598 v9fs_string_init(&glock.client_id);
3599 err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type,
3600 &glock.start, &glock.length, &glock.proc_id,
3601 &glock.client_id);
3602 if (err < 0) {
3603 goto out_nofid;
3604 }
c572f23a 3605 trace_v9fs_getlock(pdu->tag, pdu->id, fid,
ddca7f86 3606 glock.type, glock.start, glock.length);
c572f23a 3607
bccacf6c 3608 fidp = get_fid(pdu, fid);
e4e414a4 3609 if (fidp == NULL) {
8f354003 3610 err = -ENOENT;
84dfb926 3611 goto out_nofid;
8f354003 3612 }
cc720ddb 3613 err = v9fs_co_fstat(pdu, fidp, &stbuf);
8f354003 3614 if (err < 0) {
8f354003
MK
3615 goto out;
3616 }
ddca7f86
MK
3617 glock.type = P9_LOCK_TYPE_UNLCK;
3618 err = pdu_marshal(pdu, offset, "bqqds", glock.type,
3619 glock.start, glock.length, glock.proc_id,
3620 &glock.client_id);
3621 if (err < 0) {
3622 goto out;
3623 }
3624 err += offset;
3625 trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start,
3626 glock.length, glock.proc_id);
8f354003 3627out:
bccacf6c 3628 put_fid(pdu, fidp);
84dfb926 3629out_nofid:
dc295f83 3630 pdu_complete(pdu, err);
ddca7f86 3631 v9fs_string_free(&glock.client_id);
8f354003
MK
3632}
3633
8440e22e 3634static void coroutine_fn v9fs_mkdir(void *opaque)
b67592ea 3635{
ff06030f 3636 V9fsPDU *pdu = opaque;
e84861f7 3637 size_t offset = 7;
b67592ea 3638 int32_t fid;
e84861f7 3639 struct stat stbuf;
e84861f7 3640 V9fsQID qid;
02cb7f3a 3641 V9fsString name;
b67592ea
MK
3642 V9fsFidState *fidp;
3643 gid_t gid;
3644 int mode;
e84861f7 3645 int err = 0;
b67592ea 3646
ddca7f86
MK
3647 v9fs_string_init(&name);
3648 err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid);
3649 if (err < 0) {
3650 goto out_nofid;
3651 }
c572f23a
HPB
3652 trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid);
3653
fff39a7a
GK
3654 if (name_is_illegal(name.data)) {
3655 err = -ENOENT;
3656 goto out_nofid;
3657 }
3658
805b5d98
GK
3659 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3660 err = -EEXIST;
3661 goto out_nofid;
3662 }
3663
bccacf6c 3664 fidp = get_fid(pdu, fid);
b67592ea
MK
3665 if (fidp == NULL) {
3666 err = -ENOENT;
84dfb926 3667 goto out_nofid;
b67592ea 3668 }
bccacf6c 3669 err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf);
e84861f7
VJ
3670 if (err < 0) {
3671 goto out;
3672 }
3b5ee9e8
AM
3673 err = stat_to_qid(pdu, &stbuf, &qid);
3674 if (err < 0) {
3675 goto out;
3676 }
ddca7f86
MK
3677 err = pdu_marshal(pdu, offset, "Q", &qid);
3678 if (err < 0) {
3679 goto out;
3680 }
3681 err += offset;
7999f7e1
AK
3682 trace_v9fs_mkdir_return(pdu->tag, pdu->id,
3683 qid.type, qid.version, qid.path, err);
b67592ea 3684out:
bccacf6c 3685 put_fid(pdu, fidp);
84dfb926 3686out_nofid:
dc295f83 3687 pdu_complete(pdu, err);
e84861f7 3688 v9fs_string_free(&name);
b67592ea
MK
3689}
3690
8440e22e 3691static void coroutine_fn v9fs_xattrwalk(void *opaque)
fa32ef88 3692{
670185a6
AK
3693 int64_t size;
3694 V9fsString name;
fa32ef88 3695 ssize_t err = 0;
670185a6 3696 size_t offset = 7;
fa32ef88 3697 int32_t fid, newfid;
670185a6 3698 V9fsFidState *file_fidp;
84dfb926 3699 V9fsFidState *xattr_fidp = NULL;
670185a6
AK
3700 V9fsPDU *pdu = opaque;
3701 V9fsState *s = pdu->s;
fa32ef88 3702
ddca7f86
MK
3703 v9fs_string_init(&name);
3704 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name);
3705 if (err < 0) {
3706 goto out_nofid;
3707 }
c572f23a
HPB
3708 trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data);
3709
bccacf6c 3710 file_fidp = get_fid(pdu, fid);
670185a6 3711 if (file_fidp == NULL) {
fa32ef88 3712 err = -ENOENT;
84dfb926 3713 goto out_nofid;
fa32ef88 3714 }
670185a6
AK
3715 xattr_fidp = alloc_fid(s, newfid);
3716 if (xattr_fidp == NULL) {
fa32ef88
AK
3717 err = -EINVAL;
3718 goto out;
3719 }
2289be19 3720 v9fs_path_copy(&xattr_fidp->path, &file_fidp->path);
ba42ebb8 3721 if (!v9fs_string_size(&name)) {
fa32ef88
AK
3722 /*
3723 * listxattr request. Get the size first
3724 */
bccacf6c 3725 size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0);
670185a6
AK
3726 if (size < 0) {
3727 err = size;
84dfb926 3728 clunk_fid(s, xattr_fidp->fid);
670185a6 3729 goto out;
fa32ef88 3730 }
670185a6
AK
3731 /*
3732 * Read the xattr value
3733 */
3734 xattr_fidp->fs.xattr.len = size;
3735 xattr_fidp->fid_type = P9_FID_XATTR;
dd28fbbc 3736 xattr_fidp->fs.xattr.xattrwalk_fid = true;
a647502c 3737 xattr_fidp->fs.xattr.value = g_malloc0(size);
670185a6 3738 if (size) {
bccacf6c 3739 err = v9fs_co_llistxattr(pdu, &xattr_fidp->path,
670185a6
AK
3740 xattr_fidp->fs.xattr.value,
3741 xattr_fidp->fs.xattr.len);
3742 if (err < 0) {
84dfb926 3743 clunk_fid(s, xattr_fidp->fid);
670185a6
AK
3744 goto out;
3745 }
3746 }
ddca7f86
MK
3747 err = pdu_marshal(pdu, offset, "q", size);
3748 if (err < 0) {
3749 goto out;
3750 }
3751 err += offset;
fa32ef88
AK
3752 } else {
3753 /*
3754 * specific xattr fid. We check for xattr
3755 * presence also collect the xattr size
3756 */
bccacf6c 3757 size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
670185a6
AK
3758 &name, NULL, 0);
3759 if (size < 0) {
3760 err = size;
84dfb926 3761 clunk_fid(s, xattr_fidp->fid);
670185a6 3762 goto out;
fa32ef88 3763 }
670185a6
AK
3764 /*
3765 * Read the xattr value
3766 */
3767 xattr_fidp->fs.xattr.len = size;
3768 xattr_fidp->fid_type = P9_FID_XATTR;
dd28fbbc 3769 xattr_fidp->fs.xattr.xattrwalk_fid = true;
a647502c 3770 xattr_fidp->fs.xattr.value = g_malloc0(size);
670185a6 3771 if (size) {
bccacf6c 3772 err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
670185a6
AK
3773 &name, xattr_fidp->fs.xattr.value,
3774 xattr_fidp->fs.xattr.len);
3775 if (err < 0) {
84dfb926 3776 clunk_fid(s, xattr_fidp->fid);
670185a6
AK
3777 goto out;
3778 }
3779 }
ddca7f86
MK
3780 err = pdu_marshal(pdu, offset, "q", size);
3781 if (err < 0) {
3782 goto out;
3783 }
3784 err += offset;
fa32ef88 3785 }
7999f7e1 3786 trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size);
fa32ef88 3787out:
bccacf6c 3788 put_fid(pdu, file_fidp);
84dfb926 3789 if (xattr_fidp) {
bccacf6c 3790 put_fid(pdu, xattr_fidp);
84dfb926
AK
3791 }
3792out_nofid:
dc295f83 3793 pdu_complete(pdu, err);
670185a6 3794 v9fs_string_free(&name);
fa32ef88
AK
3795}
3796
8440e22e 3797static void coroutine_fn v9fs_xattrcreate(void *opaque)
10b468bd 3798{
aca6897f 3799 int flags, rflags = 0;
10b468bd 3800 int32_t fid;
3b79ef2c 3801 uint64_t size;
10b468bd 3802 ssize_t err = 0;
f10ff58d
AK
3803 V9fsString name;
3804 size_t offset = 7;
3805 V9fsFidState *file_fidp;
3806 V9fsFidState *xattr_fidp;
3807 V9fsPDU *pdu = opaque;
10b468bd 3808
ddca7f86
MK
3809 v9fs_string_init(&name);
3810 err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags);
3811 if (err < 0) {
3812 goto out_nofid;
3813 }
c572f23a 3814 trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags);
10b468bd 3815
aca6897f
KF
3816 if (flags & ~(P9_XATTR_CREATE | P9_XATTR_REPLACE)) {
3817 err = -EINVAL;
3818 goto out_nofid;
3819 }
3820
3821 if (flags & P9_XATTR_CREATE) {
3822 rflags |= XATTR_CREATE;
3823 }
3824
3825 if (flags & P9_XATTR_REPLACE) {
3826 rflags |= XATTR_REPLACE;
3827 }
3828
3b79ef2c
GK
3829 if (size > XATTR_SIZE_MAX) {
3830 err = -E2BIG;
3831 goto out_nofid;
3832 }
3833
bccacf6c 3834 file_fidp = get_fid(pdu, fid);
f10ff58d 3835 if (file_fidp == NULL) {
10b468bd 3836 err = -EINVAL;
84dfb926 3837 goto out_nofid;
10b468bd 3838 }
dd654e03
GK
3839 if (file_fidp->fid_type != P9_FID_NONE) {
3840 err = -EINVAL;
3841 goto out_put_fid;
3842 }
3843
10b468bd 3844 /* Make the file fid point to xattr */
f10ff58d
AK
3845 xattr_fidp = file_fidp;
3846 xattr_fidp->fid_type = P9_FID_XATTR;
3847 xattr_fidp->fs.xattr.copied_len = 0;
dd28fbbc 3848 xattr_fidp->fs.xattr.xattrwalk_fid = false;
f10ff58d 3849 xattr_fidp->fs.xattr.len = size;
aca6897f 3850 xattr_fidp->fs.xattr.flags = rflags;
f10ff58d
AK
3851 v9fs_string_init(&xattr_fidp->fs.xattr.name);
3852 v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
eb687602 3853 xattr_fidp->fs.xattr.value = g_malloc0(size);
f10ff58d 3854 err = offset;
dd654e03 3855out_put_fid:
bccacf6c 3856 put_fid(pdu, file_fidp);
84dfb926 3857out_nofid:
dc295f83 3858 pdu_complete(pdu, err);
f10ff58d 3859 v9fs_string_free(&name);
10b468bd 3860}
fa32ef88 3861
8440e22e 3862static void coroutine_fn v9fs_readlink(void *opaque)
df0973a4 3863{
ff06030f 3864 V9fsPDU *pdu = opaque;
7a5ca31e
VJ
3865 size_t offset = 7;
3866 V9fsString target;
df0973a4 3867 int32_t fid;
df0973a4
MK
3868 int err = 0;
3869 V9fsFidState *fidp;
3870
ddca7f86
MK
3871 err = pdu_unmarshal(pdu, offset, "d", &fid);
3872 if (err < 0) {
3873 goto out_nofid;
3874 }
c572f23a 3875 trace_v9fs_readlink(pdu->tag, pdu->id, fid);
bccacf6c 3876 fidp = get_fid(pdu, fid);
df0973a4
MK
3877 if (fidp == NULL) {
3878 err = -ENOENT;
84dfb926 3879 goto out_nofid;
df0973a4
MK
3880 }
3881
7a5ca31e 3882 v9fs_string_init(&target);
bccacf6c 3883 err = v9fs_co_readlink(pdu, &fidp->path, &target);
7a5ca31e
VJ
3884 if (err < 0) {
3885 goto out;
3886 }
ddca7f86
MK
3887 err = pdu_marshal(pdu, offset, "s", &target);
3888 if (err < 0) {
3889 v9fs_string_free(&target);
3890 goto out;
3891 }
3892 err += offset;
7999f7e1 3893 trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data);
7a5ca31e 3894 v9fs_string_free(&target);
df0973a4 3895out:
bccacf6c 3896 put_fid(pdu, fidp);
84dfb926 3897out_nofid:
dc295f83 3898 pdu_complete(pdu, err);
df0973a4
MK
3899}
3900
ff06030f 3901static CoroutineEntry *pdu_co_handlers[] = {
c18e2f94 3902 [P9_TREADDIR] = v9fs_readdir,
be940c87 3903 [P9_TSTATFS] = v9fs_statfs,
00ede4c2 3904 [P9_TGETATTR] = v9fs_getattr,
c79ce737 3905 [P9_TSETATTR] = v9fs_setattr,
fa32ef88 3906 [P9_TXATTRWALK] = v9fs_xattrwalk,
10b468bd 3907 [P9_TXATTRCREATE] = v9fs_xattrcreate,
5268cecc 3908 [P9_TMKNOD] = v9fs_mknod,
c7b4b0b3 3909 [P9_TRENAME] = v9fs_rename,
82cc3ee8 3910 [P9_TLOCK] = v9fs_lock,
8f354003 3911 [P9_TGETLOCK] = v9fs_getlock,
89bf6593 3912 [P9_TRENAMEAT] = v9fs_renameat,
df0973a4 3913 [P9_TREADLINK] = v9fs_readlink,
7834cf77 3914 [P9_TUNLINKAT] = v9fs_unlinkat,
b67592ea 3915 [P9_TMKDIR] = v9fs_mkdir,
9f107513 3916 [P9_TVERSION] = v9fs_version,
771e9d4c 3917 [P9_TLOPEN] = v9fs_open,
9f107513
AL
3918 [P9_TATTACH] = v9fs_attach,
3919 [P9_TSTAT] = v9fs_stat,
3920 [P9_TWALK] = v9fs_walk,
3921 [P9_TCLUNK] = v9fs_clunk,
b41e95d3 3922 [P9_TFSYNC] = v9fs_fsync,
9f107513
AL
3923 [P9_TOPEN] = v9fs_open,
3924 [P9_TREAD] = v9fs_read,
3925#if 0
3926 [P9_TAUTH] = v9fs_auth,
3927#endif
3928 [P9_TFLUSH] = v9fs_flush,
b2c224be 3929 [P9_TLINK] = v9fs_link,
08c60fc9 3930 [P9_TSYMLINK] = v9fs_symlink,
9f107513 3931 [P9_TCREATE] = v9fs_create,
c1568af5 3932 [P9_TLCREATE] = v9fs_lcreate,
9f107513
AL
3933 [P9_TWRITE] = v9fs_write,
3934 [P9_TWSTAT] = v9fs_wstat,
3935 [P9_TREMOVE] = v9fs_remove,
3936};
3937
8440e22e 3938static void coroutine_fn v9fs_op_not_supp(void *opaque)
5c3234c6 3939{
ff06030f 3940 V9fsPDU *pdu = opaque;
dc295f83 3941 pdu_complete(pdu, -EOPNOTSUPP);
5c3234c6
AK
3942}
3943
8440e22e 3944static void coroutine_fn v9fs_fs_ro(void *opaque)
2c74c2cb
MK
3945{
3946 V9fsPDU *pdu = opaque;
dc295f83 3947 pdu_complete(pdu, -EROFS);
2c74c2cb
MK
3948}
3949
3950static inline bool is_read_only_op(V9fsPDU *pdu)
3951{
3952 switch (pdu->id) {
3953 case P9_TREADDIR:
3954 case P9_TSTATFS:
3955 case P9_TGETATTR:
3956 case P9_TXATTRWALK:
3957 case P9_TLOCK:
3958 case P9_TGETLOCK:
3959 case P9_TREADLINK:
3960 case P9_TVERSION:
3961 case P9_TLOPEN:
3962 case P9_TATTACH:
3963 case P9_TSTAT:
3964 case P9_TWALK:
3965 case P9_TCLUNK:
3966 case P9_TFSYNC:
3967 case P9_TOPEN:
3968 case P9_TREAD:
3969 case P9_TAUTH:
3970 case P9_TFLUSH:
3971 return 1;
3972 default:
3973 return 0;
3974 }
3975}
3976
506f3275 3977void pdu_submit(V9fsPDU *pdu, P9MsgHeader *hdr)
9f107513 3978{
ff06030f
VJ
3979 Coroutine *co;
3980 CoroutineEntry *handler;
ad38ce9e 3981 V9fsState *s = pdu->s;
9f107513 3982
506f3275
GK
3983 pdu->size = le32_to_cpu(hdr->size_le);
3984 pdu->id = hdr->id;
3985 pdu->tag = le16_to_cpu(hdr->tag_le);
3986
ff06030f
VJ
3987 if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) ||
3988 (pdu_co_handlers[pdu->id] == NULL)) {
5c3234c6 3989 handler = v9fs_op_not_supp;
d1471233
GK
3990 } else if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) {
3991 handler = v9fs_fs_ro;
5c3234c6 3992 } else {
ff06030f 3993 handler = pdu_co_handlers[pdu->id];
5c3234c6 3994 }
2c74c2cb 3995
506f3275 3996 qemu_co_queue_init(&pdu->complete);
0b8b8753
PB
3997 co = qemu_coroutine_create(handler, pdu);
3998 qemu_coroutine_enter(co);
9f107513
AL
3999}
4000
2a0c56aa 4001/* Returns 0 on success, 1 on failure. */
066eb006
GK
4002int v9fs_device_realize_common(V9fsState *s, const V9fsTransport *t,
4003 Error **errp)
2a0c56aa
WL
4004{
4005 int i, len;
4006 struct stat stat;
4007 FsDriverEntry *fse;
4008 V9fsPath path;
4009 int rc = 1;
4010
066eb006
GK
4011 assert(!s->transport);
4012 s->transport = t;
4013
2a0c56aa
WL
4014 /* initialize pdu allocator */
4015 QLIST_INIT(&s->free_list);
4016 QLIST_INIT(&s->active_list);
0d78289c 4017 for (i = 0; i < MAX_REQ; i++) {
583f21f8
SS
4018 QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
4019 s->pdus[i].s = s;
4020 s->pdus[i].idx = i;
2a0c56aa
WL
4021 }
4022
4023 v9fs_path_init(&path);
4024
4025 fse = get_fsdev_fsentry(s->fsconf.fsdev_id);
4026
4027 if (!fse) {
4028 /* We don't have a fsdev identified by fsdev_id */
4029 error_setg(errp, "9pfs device couldn't find fsdev with the "
4030 "id = %s",
4031 s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL");
4032 goto out;
4033 }
4034
4035 if (!s->fsconf.tag) {
4036 /* we haven't specified a mount_tag */
4037 error_setg(errp, "fsdev with id %s needs mount_tag arguments",
4038 s->fsconf.fsdev_id);
4039 goto out;
4040 }
4041
4042 s->ctx.export_flags = fse->export_flags;
4043 s->ctx.fs_root = g_strdup(fse->path);
4044 s->ctx.exops.get_st_gen = NULL;
4045 len = strlen(s->fsconf.tag);
4046 if (len > MAX_TAG_LEN - 1) {
4047 error_setg(errp, "mount tag '%s' (%d bytes) is longer than "
4048 "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1);
4049 goto out;
4050 }
4051
4052 s->tag = g_strdup(s->fsconf.tag);
4053 s->ctx.uid = -1;
4054
4055 s->ops = fse->ops;
4056
b96feb2c
TS
4057 s->ctx.fmode = fse->fmode;
4058 s->ctx.dmode = fse->dmode;
4059
2a0c56aa
WL
4060 s->fid_list = NULL;
4061 qemu_co_rwlock_init(&s->rename_lock);
4062
65603a80
GK
4063 if (s->ops->init(&s->ctx, errp) < 0) {
4064 error_prepend(errp, "cannot initialize fsdev '%s': ",
4065 s->fsconf.fsdev_id);
2a0c56aa
WL
4066 goto out;
4067 }
4068
4069 /*
4070 * Check details of export path, We need to use fs driver
4071 * call back to do that. Since we are in the init path, we don't
4072 * use co-routines here.
4073 */
4074 if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) {
4075 error_setg(errp,
4076 "error in converting name to path %s", strerror(errno));
4077 goto out;
4078 }
4079 if (s->ops->lstat(&s->ctx, &path, &stat)) {
4080 error_setg(errp, "share path %s does not exist", fse->path);
4081 goto out;
4082 } else if (!S_ISDIR(stat.st_mode)) {
4083 error_setg(errp, "share path %s is not a directory", fse->path);
4084 goto out;
4085 }
b8bbdb88 4086
3b5ee9e8
AM
4087 s->dev_id = stat.st_dev;
4088
6b6aa828
CS
4089 /* init inode remapping : */
4090 /* hash table for variable length inode suffixes */
4091 qpd_table_init(&s->qpd_table);
4092 /* hash table for slow/full inode remapping (most users won't need it) */
4093 qpf_table_init(&s->qpf_table);
4094 /* hash table for quick inode remapping */
1a6ed33c 4095 qpp_table_init(&s->qpp_table);
6b6aa828
CS
4096 s->qp_ndevices = 0;
4097 s->qp_affix_next = 1; /* reserve 0 to detect overflow */
f3fe4a2d 4098 s->qp_fullpath_next = 1;
1a6ed33c 4099
b8bbdb88
PJ
4100 s->ctx.fst = &fse->fst;
4101 fsdev_throttle_init(s->ctx.fst);
4102
2a0c56aa
WL
4103 rc = 0;
4104out:
4105 if (rc) {
c0da0cb7 4106 v9fs_device_unrealize_common(s, NULL);
2a0c56aa 4107 }
c0da0cb7 4108 v9fs_path_free(&path);
2a0c56aa
WL
4109 return rc;
4110}
4111
4112void v9fs_device_unrealize_common(V9fsState *s, Error **errp)
4113{
c0da0cb7 4114 if (s->ops && s->ops->cleanup) {
702dbcc2
LQ
4115 s->ops->cleanup(&s->ctx);
4116 }
c0da0cb7
GK
4117 if (s->ctx.fst) {
4118 fsdev_throttle_cleanup(s->ctx.fst);
4119 }
2a0c56aa 4120 g_free(s->tag);
6b6aa828 4121 qp_table_destroy(&s->qpd_table);
f3fe4a2d
AM
4122 qp_table_destroy(&s->qpp_table);
4123 qp_table_destroy(&s->qpf_table);
4774718e 4124 g_free(s->ctx.fs_root);
2a0c56aa
WL
4125}
4126
0e44a0fd
GK
4127typedef struct VirtfsCoResetData {
4128 V9fsPDU pdu;
4129 bool done;
4130} VirtfsCoResetData;
4131
4132static void coroutine_fn virtfs_co_reset(void *opaque)
4133{
4134 VirtfsCoResetData *data = opaque;
4135
4136 virtfs_reset(&data->pdu);
4137 data->done = true;
4138}
4139
4140void v9fs_reset(V9fsState *s)
4141{
4142 VirtfsCoResetData data = { .pdu = { .s = s }, .done = false };
4143 Coroutine *co;
4144
4145 while (!QLIST_EMPTY(&s->active_list)) {
4146 aio_poll(qemu_get_aio_context(), true);
4147 }
4148
4149 co = qemu_coroutine_create(virtfs_co_reset, &data);
4150 qemu_coroutine_enter(co);
4151
4152 while (!data.done) {
4153 aio_poll(qemu_get_aio_context(), true);
4154 }
4155}
4156
72a18977 4157static void __attribute__((__constructor__)) v9fs_set_fd_limit(void)
7a462745
AK
4158{
4159 struct rlimit rlim;
4160 if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
63325b18 4161 error_report("Failed to get the resource limit");
7a462745
AK
4162 exit(1);
4163 }
4164 open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur/3);
4165 open_fd_rc = rlim.rlim_cur/2;
4166}
This page took 1.33749 seconds and 4 git commands to generate.