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