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