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