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