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