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