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