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