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