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