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