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