]> Git Repo - qemu.git/blame - hw/9pfs/9p.c
target/arm: Split contiguous loads for endianness
[qemu.git] / hw / 9pfs / 9p.c
CommitLineData
9f107513
AL
1/*
2 * Virtio 9p backend
3 *
4 * Copyright IBM, Corp. 2010
5 *
6 * Authors:
7 * Anthony Liguori <[email protected]>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 */
13
fbc04127 14#include "qemu/osdep.h"
e3e83f2e 15#include <glib/gprintf.h>
0d09e41a 16#include "hw/virtio/virtio.h"
da34e65c 17#include "qapi/error.h"
d49b6836 18#include "qemu/error-report.h"
cd4bfbb2 19#include "qemu/iov.h"
1de7afc9 20#include "qemu/sockets.h"
9f107513
AL
21#include "virtio-9p.h"
22#include "fsdev/qemu-fsdev.h"
267ae092 23#include "9p-xattr.h"
fe52840c 24#include "coth.h"
c572f23a 25#include "trace.h"
795c40b8 26#include "migration/blocker.h"
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 }
2289be19 1394 v9fs_path_copy(&fidp->path, &path);
ff5e54c9 1395 } else {
3cc19c0c
AK
1396 newfidp = alloc_fid(s, newfid);
1397 if (newfidp == NULL) {
ff5e54c9
AL
1398 err = -EINVAL;
1399 goto out;
1400 }
3cc19c0c 1401 newfidp->uid = fidp->uid;
2289be19 1402 v9fs_path_copy(&newfidp->path, &path);
9f107513 1403 }
3cc19c0c 1404 err = v9fs_walk_marshal(pdu, nwnames, qids);
7999f7e1 1405 trace_v9fs_walk_return(pdu->tag, pdu->id, nwnames, qids);
ff5e54c9 1406out:
bccacf6c 1407 put_fid(pdu, fidp);
84dfb926 1408 if (newfidp) {
bccacf6c 1409 put_fid(pdu, newfidp);
84dfb926 1410 }
2289be19
AK
1411 v9fs_path_free(&dpath);
1412 v9fs_path_free(&path);
84dfb926 1413out_nofid:
dc295f83 1414 pdu_complete(pdu, err);
3cc19c0c
AK
1415 if (nwnames && nwnames <= P9_MAXWELEM) {
1416 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1417 v9fs_string_free(&wnames[name_idx]);
1418 }
1419 g_free(wnames);
1420 g_free(qids);
1421 }
9f107513
AL
1422}
1423
8440e22e 1424static int32_t coroutine_fn get_iounit(V9fsPDU *pdu, V9fsPath *path)
5e94c103
MK
1425{
1426 struct statfs stbuf;
1427 int32_t iounit = 0;
bccacf6c 1428 V9fsState *s = pdu->s;
5e94c103
MK
1429
1430 /*
1431 * iounit should be multiples of f_bsize (host filesystem block size
1432 * and as well as less than (client msize - P9_IOHDRSZ))
1433 */
bccacf6c 1434 if (!v9fs_co_statfs(pdu, path, &stbuf)) {
5e94c103
MK
1435 iounit = stbuf.f_bsize;
1436 iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
1437 }
5e94c103
MK
1438 if (!iounit) {
1439 iounit = s->msize - P9_IOHDRSZ;
1440 }
1441 return iounit;
1442}
1443
8440e22e 1444static void coroutine_fn v9fs_open(void *opaque)
5e94c103 1445{
857bc158 1446 int flags;
857bc158
AK
1447 int32_t fid;
1448 int32_t mode;
1449 V9fsQID qid;
7999f7e1 1450 int iounit = 0;
857bc158
AK
1451 ssize_t err = 0;
1452 size_t offset = 7;
1453 struct stat stbuf;
1454 V9fsFidState *fidp;
1455 V9fsPDU *pdu = opaque;
1456 V9fsState *s = pdu->s;
5e94c103 1457
857bc158 1458 if (s->proto_version == V9FS_PROTO_2000L) {
ddca7f86 1459 err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode);
857bc158 1460 } else {
67d6fa53
BH
1461 uint8_t modebyte;
1462 err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte);
1463 mode = modebyte;
ddca7f86
MK
1464 }
1465 if (err < 0) {
1466 goto out_nofid;
857bc158 1467 }
c572f23a
HPB
1468 trace_v9fs_open(pdu->tag, pdu->id, fid, mode);
1469
bccacf6c 1470 fidp = get_fid(pdu, fid);
857bc158
AK
1471 if (fidp == NULL) {
1472 err = -ENOENT;
84dfb926 1473 goto out_nofid;
a6568fe2 1474 }
49dd946b
GK
1475 if (fidp->fid_type != P9_FID_NONE) {
1476 err = -EINVAL;
1477 goto out;
1478 }
a6568fe2 1479
bccacf6c 1480 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
857bc158 1481 if (err < 0) {
a6568fe2
AL
1482 goto out;
1483 }
857bc158
AK
1484 stat_to_qid(&stbuf, &qid);
1485 if (S_ISDIR(stbuf.st_mode)) {
bccacf6c 1486 err = v9fs_co_opendir(pdu, fidp);
857bc158
AK
1487 if (err < 0) {
1488 goto out;
1489 }
1490 fidp->fid_type = P9_FID_DIR;
ddca7f86
MK
1491 err = pdu_marshal(pdu, offset, "Qd", &qid, 0);
1492 if (err < 0) {
1493 goto out;
1494 }
1495 err += offset;
a6568fe2 1496 } else {
771e9d4c 1497 if (s->proto_version == V9FS_PROTO_2000L) {
d3ab98e6 1498 flags = get_dotl_openflags(s, mode);
771e9d4c 1499 } else {
857bc158 1500 flags = omode_to_uflags(mode);
771e9d4c 1501 }
2c74c2cb
MK
1502 if (is_ro_export(&s->ctx)) {
1503 if (mode & O_WRONLY || mode & O_RDWR ||
1504 mode & O_APPEND || mode & O_TRUNC) {
1505 err = -EROFS;
1506 goto out;
1507 }
2c74c2cb 1508 }
bccacf6c 1509 err = v9fs_co_open(pdu, fidp, flags);
857bc158
AK
1510 if (err < 0) {
1511 goto out;
1512 }
1513 fidp->fid_type = P9_FID_FILE;
7a462745
AK
1514 fidp->open_flags = flags;
1515 if (flags & O_EXCL) {
1516 /*
1517 * We let the host file system do O_EXCL check
1518 * We should not reclaim such fd
1519 */
1520 fidp->flags |= FID_NON_RECLAIMABLE;
1521 }
bccacf6c 1522 iounit = get_iounit(pdu, &fidp->path);
ddca7f86
MK
1523 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1524 if (err < 0) {
1525 goto out;
1526 }
1527 err += offset;
a6568fe2 1528 }
7999f7e1
AK
1529 trace_v9fs_open_return(pdu->tag, pdu->id,
1530 qid.type, qid.version, qid.path, iounit);
a6568fe2 1531out:
bccacf6c 1532 put_fid(pdu, fidp);
84dfb926 1533out_nofid:
dc295f83 1534 pdu_complete(pdu, err);
a6568fe2
AL
1535}
1536
8440e22e 1537static void coroutine_fn v9fs_lcreate(void *opaque)
c1568af5
VJ
1538{
1539 int32_t dfid, flags, mode;
1540 gid_t gid;
c1568af5 1541 ssize_t err = 0;
36f8981f 1542 ssize_t offset = 7;
36f8981f
VJ
1543 V9fsString name;
1544 V9fsFidState *fidp;
1545 struct stat stbuf;
1546 V9fsQID qid;
1547 int32_t iounit;
1548 V9fsPDU *pdu = opaque;
c1568af5 1549
ddca7f86
MK
1550 v9fs_string_init(&name);
1551 err = pdu_unmarshal(pdu, offset, "dsddd", &dfid,
1552 &name, &flags, &mode, &gid);
1553 if (err < 0) {
1554 goto out_nofid;
1555 }
c572f23a 1556 trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid);
c1568af5 1557
fff39a7a
GK
1558 if (name_is_illegal(name.data)) {
1559 err = -ENOENT;
1560 goto out_nofid;
1561 }
1562
805b5d98
GK
1563 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
1564 err = -EEXIST;
1565 goto out_nofid;
1566 }
1567
bccacf6c 1568 fidp = get_fid(pdu, dfid);
36f8981f 1569 if (fidp == NULL) {
c1568af5 1570 err = -ENOENT;
84dfb926 1571 goto out_nofid;
c1568af5 1572 }
d63fb193
LQ
1573 if (fidp->fid_type != P9_FID_NONE) {
1574 err = -EINVAL;
1575 goto out;
1576 }
c1568af5 1577
d3ab98e6 1578 flags = get_dotl_openflags(pdu->s, flags);
bccacf6c 1579 err = v9fs_co_open2(pdu, fidp, &name, gid,
02cb7f3a 1580 flags | O_CREAT, mode, &stbuf);
36f8981f
VJ
1581 if (err < 0) {
1582 goto out;
1583 }
1584 fidp->fid_type = P9_FID_FILE;
7a462745
AK
1585 fidp->open_flags = flags;
1586 if (flags & O_EXCL) {
1587 /*
1588 * We let the host file system do O_EXCL check
1589 * We should not reclaim such fd
1590 */
1591 fidp->flags |= FID_NON_RECLAIMABLE;
1592 }
bccacf6c 1593 iounit = get_iounit(pdu, &fidp->path);
36f8981f 1594 stat_to_qid(&stbuf, &qid);
ddca7f86
MK
1595 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1596 if (err < 0) {
1597 goto out;
1598 }
1599 err += offset;
7999f7e1
AK
1600 trace_v9fs_lcreate_return(pdu->tag, pdu->id,
1601 qid.type, qid.version, qid.path, iounit);
c1568af5 1602out:
bccacf6c 1603 put_fid(pdu, fidp);
84dfb926 1604out_nofid:
dc295f83 1605 pdu_complete(pdu, err);
36f8981f 1606 v9fs_string_free(&name);
c1568af5
VJ
1607}
1608
a1bf8b74 1609static void coroutine_fn v9fs_fsync(void *opaque)
b41e95d3 1610{
4e9ad444 1611 int err;
b41e95d3 1612 int32_t fid;
4e9ad444 1613 int datasync;
b41e95d3
VJ
1614 size_t offset = 7;
1615 V9fsFidState *fidp;
4e9ad444 1616 V9fsPDU *pdu = opaque;
b41e95d3 1617
ddca7f86
MK
1618 err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
1619 if (err < 0) {
1620 goto out_nofid;
1621 }
c572f23a
HPB
1622 trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync);
1623
bccacf6c 1624 fidp = get_fid(pdu, fid);
b41e95d3
VJ
1625 if (fidp == NULL) {
1626 err = -ENOENT;
84dfb926 1627 goto out_nofid;
b41e95d3 1628 }
bccacf6c 1629 err = v9fs_co_fsync(pdu, fidp, datasync);
4e9ad444
AK
1630 if (!err) {
1631 err = offset;
1632 }
bccacf6c 1633 put_fid(pdu, fidp);
84dfb926 1634out_nofid:
dc295f83 1635 pdu_complete(pdu, err);
b41e95d3
VJ
1636}
1637
8440e22e 1638static void coroutine_fn v9fs_clunk(void *opaque)
a6568fe2 1639{
c540ee51 1640 int err;
bbd5697b
AL
1641 int32_t fid;
1642 size_t offset = 7;
84dfb926 1643 V9fsFidState *fidp;
c540ee51
AK
1644 V9fsPDU *pdu = opaque;
1645 V9fsState *s = pdu->s;
bbd5697b 1646
ddca7f86
MK
1647 err = pdu_unmarshal(pdu, offset, "d", &fid);
1648 if (err < 0) {
1649 goto out_nofid;
1650 }
c572f23a 1651 trace_v9fs_clunk(pdu->tag, pdu->id, fid);
84dfb926 1652
ce421a19 1653 fidp = clunk_fid(s, fid);
84dfb926
AK
1654 if (fidp == NULL) {
1655 err = -ENOENT;
1656 goto out_nofid;
1657 }
ce421a19
AK
1658 /*
1659 * Bump the ref so that put_fid will
1660 * free the fid.
1661 */
1662 fidp->ref++;
a911a182
AK
1663 err = put_fid(pdu, fidp);
1664 if (!err) {
1665 err = offset;
1666 }
84dfb926 1667out_nofid:
dc295f83 1668 pdu_complete(pdu, err);
9f107513
AL
1669}
1670
bcb8998f
SS
1671/*
1672 * Create a QEMUIOVector for a sub-region of PDU iovecs
1673 *
1674 * @qiov: uninitialized QEMUIOVector
1675 * @skip: number of bytes to skip from beginning of PDU
1676 * @size: number of bytes to include
1677 * @is_write: true - write, false - read
1678 *
1679 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1680 * with qemu_iovec_destroy().
1681 */
1682static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu,
1683 size_t skip, size_t size,
1684 bool is_write)
1685{
1686 QEMUIOVector elem;
1687 struct iovec *iov;
1688 unsigned int niov;
1689
88da0b03 1690 if (is_write) {
8d37de41 1691 pdu->s->transport->init_out_iov_from_pdu(pdu, &iov, &niov, size + skip);
88da0b03 1692 } else {
fa0eb5c5 1693 pdu->s->transport->init_in_iov_from_pdu(pdu, &iov, &niov, size + skip);
88da0b03 1694 }
bcb8998f
SS
1695
1696 qemu_iovec_init_external(&elem, iov, niov);
1697 qemu_iovec_init(qiov, niov);
1698 qemu_iovec_concat(qiov, &elem, skip, size);
1699}
1700
2f008a8c
AK
1701static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
1702 uint64_t off, uint32_t max_count)
a9231555 1703{
ddca7f86 1704 ssize_t err;
d208a0e0 1705 size_t offset = 7;
7e55d65c 1706 uint64_t read_count;
bcb8998f 1707 QEMUIOVector qiov_full;
a9231555 1708
7e55d65c
LQ
1709 if (fidp->fs.xattr.len < off) {
1710 read_count = 0;
1711 } else {
1712 read_count = fidp->fs.xattr.len - off;
1713 }
d208a0e0
AK
1714 if (read_count > max_count) {
1715 read_count = max_count;
a9231555 1716 }
ddca7f86
MK
1717 err = pdu_marshal(pdu, offset, "d", read_count);
1718 if (err < 0) {
1719 return err;
1720 }
1721 offset += err;
00588a0a 1722
fa0eb5c5
GK
1723 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, read_count, false);
1724 err = v9fs_pack(qiov_full.iov, qiov_full.niov, 0,
ddca7f86
MK
1725 ((char *)fidp->fs.xattr.value) + off,
1726 read_count);
bcb8998f 1727 qemu_iovec_destroy(&qiov_full);
ddca7f86
MK
1728 if (err < 0) {
1729 return err;
1730 }
1731 offset += err;
d208a0e0 1732 return offset;
a9231555
AL
1733}
1734
8440e22e
GK
1735static int coroutine_fn v9fs_do_readdir_with_stat(V9fsPDU *pdu,
1736 V9fsFidState *fidp,
1737 uint32_t max_count)
a9231555 1738{
2289be19 1739 V9fsPath path;
d208a0e0
AK
1740 V9fsStat v9stat;
1741 int len, err = 0;
1742 int32_t count = 0;
1743 struct stat stbuf;
1744 off_t saved_dir_pos;
635324e8 1745 struct dirent *dent;
a9231555 1746
d208a0e0 1747 /* save the directory position */
bccacf6c 1748 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
d208a0e0
AK
1749 if (saved_dir_pos < 0) {
1750 return saved_dir_pos;
a9231555 1751 }
5f524c1e 1752
d208a0e0 1753 while (1) {
2289be19 1754 v9fs_path_init(&path);
7cde47d4
GK
1755
1756 v9fs_readdir_lock(&fidp->fs.dir);
1757
635324e8
GK
1758 err = v9fs_co_readdir(pdu, fidp, &dent);
1759 if (err || !dent) {
d208a0e0
AK
1760 break;
1761 }
bccacf6c 1762 err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path);
d208a0e0 1763 if (err < 0) {
8762a46d 1764 break;
d208a0e0 1765 }
bccacf6c 1766 err = v9fs_co_lstat(pdu, &path, &stbuf);
2289be19 1767 if (err < 0) {
8762a46d 1768 break;
2289be19 1769 }
6069537f 1770 err = stat_to_v9stat(pdu, &path, dent->d_name, &stbuf, &v9stat);
d208a0e0 1771 if (err < 0) {
8762a46d 1772 break;
a9231555 1773 }
772a7369
JD
1774 if ((count + v9stat.size + 2) > max_count) {
1775 v9fs_readdir_unlock(&fidp->fs.dir);
1776
1777 /* Ran out of buffer. Set dir back to old position and return */
1778 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1779 v9fs_stat_free(&v9stat);
1780 v9fs_path_free(&path);
1781 return count;
1782 }
1783
d208a0e0
AK
1784 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1785 len = pdu_marshal(pdu, 11 + count, "S", &v9stat);
7cde47d4
GK
1786
1787 v9fs_readdir_unlock(&fidp->fs.dir);
1788
772a7369 1789 if (len < 0) {
bccacf6c 1790 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
d208a0e0 1791 v9fs_stat_free(&v9stat);
2289be19 1792 v9fs_path_free(&path);
772a7369 1793 return len;
d208a0e0
AK
1794 }
1795 count += len;
1796 v9fs_stat_free(&v9stat);
2289be19 1797 v9fs_path_free(&path);
d208a0e0 1798 saved_dir_pos = dent->d_off;
a9231555 1799 }
8762a46d 1800
7cde47d4
GK
1801 v9fs_readdir_unlock(&fidp->fs.dir);
1802
2289be19 1803 v9fs_path_free(&path);
d208a0e0
AK
1804 if (err < 0) {
1805 return err;
fa32ef88 1806 }
d208a0e0 1807 return count;
fa32ef88
AK
1808}
1809
8440e22e 1810static void coroutine_fn v9fs_read(void *opaque)
9f107513 1811{
a9231555 1812 int32_t fid;
2f008a8c 1813 uint64_t off;
a9231555 1814 ssize_t err = 0;
d208a0e0
AK
1815 int32_t count = 0;
1816 size_t offset = 7;
2f008a8c 1817 uint32_t max_count;
d208a0e0
AK
1818 V9fsFidState *fidp;
1819 V9fsPDU *pdu = opaque;
1820 V9fsState *s = pdu->s;
a9231555 1821
ddca7f86
MK
1822 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count);
1823 if (err < 0) {
1824 goto out_nofid;
1825 }
c572f23a 1826 trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count);
84dfb926 1827
bccacf6c 1828 fidp = get_fid(pdu, fid);
d208a0e0 1829 if (fidp == NULL) {
a9231555 1830 err = -EINVAL;
84dfb926 1831 goto out_nofid;
a9231555 1832 }
d208a0e0 1833 if (fidp->fid_type == P9_FID_DIR) {
a9231555 1834
d208a0e0 1835 if (off == 0) {
bccacf6c 1836 v9fs_co_rewinddir(pdu, fidp);
a9231555 1837 }
bccacf6c 1838 count = v9fs_do_readdir_with_stat(pdu, fidp, max_count);
d208a0e0
AK
1839 if (count < 0) {
1840 err = count;
1841 goto out;
56d15a53 1842 }
ddca7f86
MK
1843 err = pdu_marshal(pdu, offset, "d", count);
1844 if (err < 0) {
1845 goto out;
1846 }
1847 err += offset + count;
d208a0e0 1848 } else if (fidp->fid_type == P9_FID_FILE) {
302a0d3e
SH
1849 QEMUIOVector qiov_full;
1850 QEMUIOVector qiov;
d208a0e0 1851 int32_t len;
d208a0e0 1852
302a0d3e
SH
1853 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, max_count, false);
1854 qemu_iovec_init(&qiov, qiov_full.niov);
d208a0e0 1855 do {
302a0d3e 1856 qemu_iovec_reset(&qiov);
1b093c48 1857 qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count);
d208a0e0 1858 if (0) {
302a0d3e 1859 print_sg(qiov.iov, qiov.niov);
d208a0e0
AK
1860 }
1861 /* Loop in case of EINTR */
1862 do {
302a0d3e 1863 len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off);
d208a0e0
AK
1864 if (len >= 0) {
1865 off += len;
1866 count += len;
1867 }
bccacf6c 1868 } while (len == -EINTR && !pdu->cancelled);
d208a0e0
AK
1869 if (len < 0) {
1870 /* IO error return the error */
1871 err = len;
e95c9a49 1872 goto out_free_iovec;
d208a0e0 1873 }
d208a0e0 1874 } while (count < max_count && len > 0);
ddca7f86
MK
1875 err = pdu_marshal(pdu, offset, "d", count);
1876 if (err < 0) {
e95c9a49 1877 goto out_free_iovec;
ddca7f86
MK
1878 }
1879 err += offset + count;
e95c9a49 1880out_free_iovec:
302a0d3e
SH
1881 qemu_iovec_destroy(&qiov);
1882 qemu_iovec_destroy(&qiov_full);
d208a0e0
AK
1883 } else if (fidp->fid_type == P9_FID_XATTR) {
1884 err = v9fs_xattr_read(s, pdu, fidp, off, max_count);
a9231555
AL
1885 } else {
1886 err = -EINVAL;
9f107513 1887 }
7999f7e1 1888 trace_v9fs_read_return(pdu->tag, pdu->id, count, err);
a9231555 1889out:
bccacf6c 1890 put_fid(pdu, fidp);
84dfb926 1891out_nofid:
dc295f83 1892 pdu_complete(pdu, err);
9f107513
AL
1893}
1894
5e4eaa79 1895static size_t v9fs_readdir_data_size(V9fsString *name)
c18e2f94 1896{
5e4eaa79
AK
1897 /*
1898 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1899 * size of type (1) + size of name.size (2) + strlen(name.data)
1900 */
1901 return 24 + v9fs_string_size(name);
c18e2f94
SK
1902}
1903
8440e22e
GK
1904static int coroutine_fn v9fs_do_readdir(V9fsPDU *pdu, V9fsFidState *fidp,
1905 int32_t max_count)
c18e2f94 1906{
c18e2f94 1907 size_t size;
5e4eaa79
AK
1908 V9fsQID qid;
1909 V9fsString name;
1910 int len, err = 0;
1911 int32_t count = 0;
1912 off_t saved_dir_pos;
635324e8 1913 struct dirent *dent;
c18e2f94 1914
5e4eaa79 1915 /* save the directory position */
bccacf6c 1916 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
5e4eaa79
AK
1917 if (saved_dir_pos < 0) {
1918 return saved_dir_pos;
1919 }
5f524c1e 1920
5e4eaa79 1921 while (1) {
7cde47d4
GK
1922 v9fs_readdir_lock(&fidp->fs.dir);
1923
635324e8
GK
1924 err = v9fs_co_readdir(pdu, fidp, &dent);
1925 if (err || !dent) {
5e4eaa79
AK
1926 break;
1927 }
1928 v9fs_string_init(&name);
1929 v9fs_string_sprintf(&name, "%s", dent->d_name);
1930 if ((count + v9fs_readdir_data_size(&name)) > max_count) {
7cde47d4
GK
1931 v9fs_readdir_unlock(&fidp->fs.dir);
1932
c18e2f94 1933 /* Ran out of buffer. Set dir back to old position and return */
bccacf6c 1934 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
5e4eaa79
AK
1935 v9fs_string_free(&name);
1936 return count;
c18e2f94 1937 }
5e4eaa79
AK
1938 /*
1939 * Fill up just the path field of qid because the client uses
c18e2f94
SK
1940 * only that. To fill the entire qid structure we will have
1941 * to stat each dirent found, which is expensive
1942 */
5e4eaa79
AK
1943 size = MIN(sizeof(dent->d_ino), sizeof(qid.path));
1944 memcpy(&qid.path, &dent->d_ino, size);
c18e2f94 1945 /* Fill the other fields with dummy values */
5e4eaa79
AK
1946 qid.type = 0;
1947 qid.version = 0;
c18e2f94 1948
5e4eaa79
AK
1949 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1950 len = pdu_marshal(pdu, 11 + count, "Qqbs",
1951 &qid, dent->d_off,
1952 dent->d_type, &name);
7cde47d4
GK
1953
1954 v9fs_readdir_unlock(&fidp->fs.dir);
1955
ddca7f86
MK
1956 if (len < 0) {
1957 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1958 v9fs_string_free(&name);
ddca7f86
MK
1959 return len;
1960 }
5e4eaa79
AK
1961 count += len;
1962 v9fs_string_free(&name);
1963 saved_dir_pos = dent->d_off;
1964 }
7cde47d4
GK
1965
1966 v9fs_readdir_unlock(&fidp->fs.dir);
1967
5e4eaa79
AK
1968 if (err < 0) {
1969 return err;
1970 }
1971 return count;
c18e2f94
SK
1972}
1973
8440e22e 1974static void coroutine_fn v9fs_readdir(void *opaque)
c18e2f94
SK
1975{
1976 int32_t fid;
5e4eaa79
AK
1977 V9fsFidState *fidp;
1978 ssize_t retval = 0;
c18e2f94 1979 size_t offset = 7;
2f008a8c
AK
1980 uint64_t initial_offset;
1981 int32_t count;
1982 uint32_t max_count;
5e4eaa79 1983 V9fsPDU *pdu = opaque;
c18e2f94 1984
ddca7f86
MK
1985 retval = pdu_unmarshal(pdu, offset, "dqd", &fid,
1986 &initial_offset, &max_count);
1987 if (retval < 0) {
1988 goto out_nofid;
1989 }
c572f23a
HPB
1990 trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count);
1991
bccacf6c 1992 fidp = get_fid(pdu, fid);
84dfb926
AK
1993 if (fidp == NULL) {
1994 retval = -EINVAL;
1995 goto out_nofid;
1996 }
f314ea4e 1997 if (!fidp->fs.dir.stream) {
5e4eaa79 1998 retval = -EINVAL;
c18e2f94
SK
1999 goto out;
2000 }
5e4eaa79 2001 if (initial_offset == 0) {
bccacf6c 2002 v9fs_co_rewinddir(pdu, fidp);
c18e2f94 2003 } else {
bccacf6c 2004 v9fs_co_seekdir(pdu, fidp, initial_offset);
c18e2f94 2005 }
bccacf6c 2006 count = v9fs_do_readdir(pdu, fidp, max_count);
5e4eaa79
AK
2007 if (count < 0) {
2008 retval = count;
2009 goto out;
2010 }
ddca7f86
MK
2011 retval = pdu_marshal(pdu, offset, "d", count);
2012 if (retval < 0) {
2013 goto out;
2014 }
2015 retval += count + offset;
7999f7e1 2016 trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval);
c18e2f94 2017out:
bccacf6c 2018 put_fid(pdu, fidp);
84dfb926 2019out_nofid:
dc295f83 2020 pdu_complete(pdu, retval);
c18e2f94
SK
2021}
2022
d7a90491 2023static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
2f008a8c 2024 uint64_t off, uint32_t count,
d7a90491 2025 struct iovec *sg, int cnt)
10b468bd
AK
2026{
2027 int i, to_copy;
2028 ssize_t err = 0;
7e55d65c 2029 uint64_t write_count;
d7a90491 2030 size_t offset = 7;
10b468bd 2031
d7a90491 2032
7e55d65c 2033 if (fidp->fs.xattr.len < off) {
10b468bd
AK
2034 err = -ENOSPC;
2035 goto out;
2036 }
7e55d65c
LQ
2037 write_count = fidp->fs.xattr.len - off;
2038 if (write_count > count) {
2039 write_count = count;
2040 }
ddca7f86
MK
2041 err = pdu_marshal(pdu, offset, "d", write_count);
2042 if (err < 0) {
2043 return err;
2044 }
2045 err += offset;
d7a90491 2046 fidp->fs.xattr.copied_len += write_count;
10b468bd
AK
2047 /*
2048 * Now copy the content from sg list
2049 */
d7a90491
AK
2050 for (i = 0; i < cnt; i++) {
2051 if (write_count > sg[i].iov_len) {
2052 to_copy = sg[i].iov_len;
10b468bd
AK
2053 } else {
2054 to_copy = write_count;
2055 }
d7a90491 2056 memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy);
10b468bd 2057 /* updating vs->off since we are not using below */
d7a90491 2058 off += to_copy;
10b468bd
AK
2059 write_count -= to_copy;
2060 }
2061out:
d7a90491 2062 return err;
10b468bd
AK
2063}
2064
8440e22e 2065static void coroutine_fn v9fs_write(void *opaque)
9f107513 2066{
d7a90491
AK
2067 ssize_t err;
2068 int32_t fid;
2f008a8c
AK
2069 uint64_t off;
2070 uint32_t count;
d7a90491
AK
2071 int32_t len = 0;
2072 int32_t total = 0;
2073 size_t offset = 7;
2074 V9fsFidState *fidp;
ff06030f
VJ
2075 V9fsPDU *pdu = opaque;
2076 V9fsState *s = pdu->s;
302a0d3e
SH
2077 QEMUIOVector qiov_full;
2078 QEMUIOVector qiov;
8449360c 2079
ddca7f86
MK
2080 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count);
2081 if (err < 0) {
dc295f83 2082 pdu_complete(pdu, err);
0289a412 2083 return;
ddca7f86
MK
2084 }
2085 offset += err;
302a0d3e
SH
2086 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, count, true);
2087 trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov);
84dfb926 2088
bccacf6c 2089 fidp = get_fid(pdu, fid);
d7a90491 2090 if (fidp == NULL) {
8449360c 2091 err = -EINVAL;
84dfb926 2092 goto out_nofid;
9f107513 2093 }
d7a90491
AK
2094 if (fidp->fid_type == P9_FID_FILE) {
2095 if (fidp->fs.fd == -1) {
10b468bd
AK
2096 err = -EINVAL;
2097 goto out;
2098 }
d7a90491 2099 } else if (fidp->fid_type == P9_FID_XATTR) {
10b468bd
AK
2100 /*
2101 * setxattr operation
2102 */
302a0d3e
SH
2103 err = v9fs_xattr_write(s, pdu, fidp, off, count,
2104 qiov_full.iov, qiov_full.niov);
d7a90491 2105 goto out;
10b468bd 2106 } else {
8449360c
AL
2107 err = -EINVAL;
2108 goto out;
2109 }
302a0d3e 2110 qemu_iovec_init(&qiov, qiov_full.niov);
d7a90491 2111 do {
302a0d3e 2112 qemu_iovec_reset(&qiov);
1b093c48 2113 qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total);
d7a90491 2114 if (0) {
302a0d3e 2115 print_sg(qiov.iov, qiov.niov);
56d15a53 2116 }
d7a90491
AK
2117 /* Loop in case of EINTR */
2118 do {
302a0d3e 2119 len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off);
d7a90491
AK
2120 if (len >= 0) {
2121 off += len;
2122 total += len;
2123 }
bccacf6c 2124 } while (len == -EINTR && !pdu->cancelled);
d7a90491
AK
2125 if (len < 0) {
2126 /* IO error return the error */
2127 err = len;
302a0d3e 2128 goto out_qiov;
d7a90491 2129 }
d7a90491 2130 } while (total < count && len > 0);
302a0d3e
SH
2131
2132 offset = 7;
ddca7f86
MK
2133 err = pdu_marshal(pdu, offset, "d", total);
2134 if (err < 0) {
fdfcc9ae 2135 goto out_qiov;
ddca7f86
MK
2136 }
2137 err += offset;
7999f7e1 2138 trace_v9fs_write_return(pdu->tag, pdu->id, total, err);
302a0d3e
SH
2139out_qiov:
2140 qemu_iovec_destroy(&qiov);
8449360c 2141out:
bccacf6c 2142 put_fid(pdu, fidp);
84dfb926 2143out_nofid:
302a0d3e 2144 qemu_iovec_destroy(&qiov_full);
dc295f83 2145 pdu_complete(pdu, err);
9f107513
AL
2146}
2147
8440e22e 2148static void coroutine_fn v9fs_create(void *opaque)
5e94c103 2149{
baaa86d9
VJ
2150 int32_t fid;
2151 int err = 0;
2152 size_t offset = 7;
2153 V9fsFidState *fidp;
2154 V9fsQID qid;
2155 int32_t perm;
2156 int8_t mode;
2289be19 2157 V9fsPath path;
baaa86d9
VJ
2158 struct stat stbuf;
2159 V9fsString name;
2160 V9fsString extension;
baaa86d9
VJ
2161 int iounit;
2162 V9fsPDU *pdu = opaque;
c494dd6f 2163
2289be19 2164 v9fs_path_init(&path);
ddca7f86
MK
2165 v9fs_string_init(&name);
2166 v9fs_string_init(&extension);
2167 err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name,
2168 &perm, &mode, &extension);
2169 if (err < 0) {
2170 goto out_nofid;
2171 }
c572f23a
HPB
2172 trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode);
2173
fff39a7a
GK
2174 if (name_is_illegal(name.data)) {
2175 err = -ENOENT;
2176 goto out_nofid;
2177 }
2178
805b5d98
GK
2179 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2180 err = -EEXIST;
2181 goto out_nofid;
2182 }
2183
bccacf6c 2184 fidp = get_fid(pdu, fid);
baaa86d9
VJ
2185 if (fidp == NULL) {
2186 err = -EINVAL;
84dfb926 2187 goto out_nofid;
c494dd6f 2188 }
d63fb193
LQ
2189 if (fidp->fid_type != P9_FID_NONE) {
2190 err = -EINVAL;
2191 goto out;
2192 }
baaa86d9 2193 if (perm & P9_STAT_MODE_DIR) {
bccacf6c 2194 err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777,
02cb7f3a 2195 fidp->uid, -1, &stbuf);
baaa86d9
VJ
2196 if (err < 0) {
2197 goto out;
2198 }
bccacf6c 2199 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2289be19
AK
2200 if (err < 0) {
2201 goto out;
2202 }
2203 v9fs_path_copy(&fidp->path, &path);
bccacf6c 2204 err = v9fs_co_opendir(pdu, fidp);
baaa86d9
VJ
2205 if (err < 0) {
2206 goto out;
2207 }
2208 fidp->fid_type = P9_FID_DIR;
2209 } else if (perm & P9_STAT_MODE_SYMLINK) {
bccacf6c 2210 err = v9fs_co_symlink(pdu, fidp, &name,
02cb7f3a 2211 extension.data, -1 , &stbuf);
baaa86d9 2212 if (err < 0) {
baaa86d9
VJ
2213 goto out;
2214 }
bccacf6c 2215 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2289be19
AK
2216 if (err < 0) {
2217 goto out;
2218 }
2219 v9fs_path_copy(&fidp->path, &path);
baaa86d9 2220 } else if (perm & P9_STAT_MODE_LINK) {
2289be19 2221 int32_t ofid = atoi(extension.data);
bccacf6c 2222 V9fsFidState *ofidp = get_fid(pdu, ofid);
2289be19 2223 if (ofidp == NULL) {
baaa86d9
VJ
2224 err = -EINVAL;
2225 goto out;
2226 }
bccacf6c
AK
2227 err = v9fs_co_link(pdu, ofidp, fidp, &name);
2228 put_fid(pdu, ofidp);
2289be19
AK
2229 if (err < 0) {
2230 goto out;
2231 }
bccacf6c 2232 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
baaa86d9 2233 if (err < 0) {
2289be19 2234 fidp->fid_type = P9_FID_NONE;
baaa86d9 2235 goto out;
c494dd6f 2236 }
2289be19 2237 v9fs_path_copy(&fidp->path, &path);
bccacf6c 2238 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
02cb7f3a
AK
2239 if (err < 0) {
2240 fidp->fid_type = P9_FID_NONE;
2241 goto out;
2242 }
baaa86d9 2243 } else if (perm & P9_STAT_MODE_DEVICE) {
c494dd6f
AL
2244 char ctype;
2245 uint32_t major, minor;
2246 mode_t nmode = 0;
2247
baaa86d9 2248 if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) {
c494dd6f 2249 err = -errno;
baaa86d9 2250 goto out;
c494dd6f
AL
2251 }
2252
2253 switch (ctype) {
2254 case 'c':
2255 nmode = S_IFCHR;
2256 break;
2257 case 'b':
2258 nmode = S_IFBLK;
2259 break;
2260 default:
2261 err = -EIO;
baaa86d9
VJ
2262 goto out;
2263 }
c1568af5 2264
baaa86d9 2265 nmode |= perm & 0777;
bccacf6c 2266 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
02cb7f3a 2267 makedev(major, minor), nmode, &stbuf);
baaa86d9
VJ
2268 if (err < 0) {
2269 goto out;
2270 }
bccacf6c 2271 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2289be19
AK
2272 if (err < 0) {
2273 goto out;
2274 }
2275 v9fs_path_copy(&fidp->path, &path);
baaa86d9 2276 } else if (perm & P9_STAT_MODE_NAMED_PIPE) {
bccacf6c 2277 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
02cb7f3a 2278 0, S_IFIFO | (perm & 0777), &stbuf);
baaa86d9
VJ
2279 if (err < 0) {
2280 goto out;
2281 }
bccacf6c 2282 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2289be19
AK
2283 if (err < 0) {
2284 goto out;
2285 }
2286 v9fs_path_copy(&fidp->path, &path);
baaa86d9 2287 } else if (perm & P9_STAT_MODE_SOCKET) {
bccacf6c 2288 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
02cb7f3a 2289 0, S_IFSOCK | (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 }
2297 v9fs_path_copy(&fidp->path, &path);
baaa86d9 2298 } else {
bccacf6c 2299 err = v9fs_co_open2(pdu, fidp, &name, -1,
02cb7f3a 2300 omode_to_uflags(mode)|O_CREAT, perm, &stbuf);
baaa86d9
VJ
2301 if (err < 0) {
2302 goto out;
2303 }
2304 fidp->fid_type = P9_FID_FILE;
7a462745
AK
2305 fidp->open_flags = omode_to_uflags(mode);
2306 if (fidp->open_flags & O_EXCL) {
2307 /*
2308 * We let the host file system do O_EXCL check
2309 * We should not reclaim such fd
2310 */
2311 fidp->flags |= FID_NON_RECLAIMABLE;
2312 }
c494dd6f 2313 }
bccacf6c 2314 iounit = get_iounit(pdu, &fidp->path);
baaa86d9 2315 stat_to_qid(&stbuf, &qid);
ddca7f86
MK
2316 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
2317 if (err < 0) {
2318 goto out;
2319 }
2320 err += offset;
7999f7e1
AK
2321 trace_v9fs_create_return(pdu->tag, pdu->id,
2322 qid.type, qid.version, qid.path, iounit);
c494dd6f 2323out:
bccacf6c 2324 put_fid(pdu, fidp);
84dfb926 2325out_nofid:
dc295f83 2326 pdu_complete(pdu, err);
baaa86d9
VJ
2327 v9fs_string_free(&name);
2328 v9fs_string_free(&extension);
2289be19 2329 v9fs_path_free(&path);
9f107513
AL
2330}
2331
8440e22e 2332static void coroutine_fn v9fs_symlink(void *opaque)
08c60fc9 2333{
ff06030f 2334 V9fsPDU *pdu = opaque;
3fa2a8d1
VJ
2335 V9fsString name;
2336 V9fsString symname;
3fa2a8d1
VJ
2337 V9fsFidState *dfidp;
2338 V9fsQID qid;
2339 struct stat stbuf;
08c60fc9 2340 int32_t dfid;
08c60fc9
VJ
2341 int err = 0;
2342 gid_t gid;
3fa2a8d1 2343 size_t offset = 7;
08c60fc9 2344
ddca7f86
MK
2345 v9fs_string_init(&name);
2346 v9fs_string_init(&symname);
2347 err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid);
2348 if (err < 0) {
2349 goto out_nofid;
2350 }
c572f23a 2351 trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid);
08c60fc9 2352
fff39a7a
GK
2353 if (name_is_illegal(name.data)) {
2354 err = -ENOENT;
2355 goto out_nofid;
2356 }
2357
805b5d98
GK
2358 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2359 err = -EEXIST;
2360 goto out_nofid;
2361 }
2362
bccacf6c 2363 dfidp = get_fid(pdu, dfid);
3fa2a8d1 2364 if (dfidp == NULL) {
08c60fc9 2365 err = -EINVAL;
84dfb926 2366 goto out_nofid;
08c60fc9 2367 }
bccacf6c 2368 err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf);
3fa2a8d1
VJ
2369 if (err < 0) {
2370 goto out;
2371 }
2372 stat_to_qid(&stbuf, &qid);
ddca7f86
MK
2373 err = pdu_marshal(pdu, offset, "Q", &qid);
2374 if (err < 0) {
2375 goto out;
2376 }
2377 err += offset;
7999f7e1
AK
2378 trace_v9fs_symlink_return(pdu->tag, pdu->id,
2379 qid.type, qid.version, qid.path);
08c60fc9 2380out:
bccacf6c 2381 put_fid(pdu, dfidp);
84dfb926 2382out_nofid:
dc295f83 2383 pdu_complete(pdu, err);
3fa2a8d1
VJ
2384 v9fs_string_free(&name);
2385 v9fs_string_free(&symname);
08c60fc9
VJ
2386}
2387
a1bf8b74 2388static void coroutine_fn v9fs_flush(void *opaque)
9f107513 2389{
ddca7f86 2390 ssize_t err;
bccacf6c
AK
2391 int16_t tag;
2392 size_t offset = 7;
d5f2af7b 2393 V9fsPDU *cancel_pdu = NULL;
ff06030f
VJ
2394 V9fsPDU *pdu = opaque;
2395 V9fsState *s = pdu->s;
bccacf6c 2396
ddca7f86
MK
2397 err = pdu_unmarshal(pdu, offset, "w", &tag);
2398 if (err < 0) {
dc295f83 2399 pdu_complete(pdu, err);
ddca7f86
MK
2400 return;
2401 }
c572f23a 2402 trace_v9fs_flush(pdu->tag, pdu->id, tag);
bccacf6c 2403
d5f2af7b 2404 if (pdu->tag == tag) {
3dc6f869 2405 warn_report("the guest sent a self-referencing 9P flush request");
d5f2af7b
GK
2406 } else {
2407 QLIST_FOREACH(cancel_pdu, &s->active_list, next) {
2408 if (cancel_pdu->tag == tag) {
2409 break;
2410 }
bccacf6c
AK
2411 }
2412 }
2413 if (cancel_pdu) {
2414 cancel_pdu->cancelled = 1;
2415 /*
2416 * Wait for pdu to complete.
2417 */
1ace7cea 2418 qemu_co_queue_wait(&cancel_pdu->complete, NULL);
18adde86
GK
2419 if (!qemu_co_queue_next(&cancel_pdu->complete)) {
2420 cancel_pdu->cancelled = 0;
2421 pdu_free(cancel_pdu);
2422 }
bccacf6c 2423 }
dc295f83 2424 pdu_complete(pdu, 7);
9f107513
AL
2425}
2426
8440e22e 2427static void coroutine_fn v9fs_link(void *opaque)
b2c224be 2428{
ff06030f 2429 V9fsPDU *pdu = opaque;
b2c224be
VJ
2430 int32_t dfid, oldfid;
2431 V9fsFidState *dfidp, *oldfidp;
3a93113a 2432 V9fsString name;
b2c224be
VJ
2433 size_t offset = 7;
2434 int err = 0;
2435
ddca7f86
MK
2436 v9fs_string_init(&name);
2437 err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
2438 if (err < 0) {
2439 goto out_nofid;
2440 }
c572f23a 2441 trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data);
b2c224be 2442
fff39a7a
GK
2443 if (name_is_illegal(name.data)) {
2444 err = -ENOENT;
2445 goto out_nofid;
2446 }
2447
805b5d98
GK
2448 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2449 err = -EEXIST;
2450 goto out_nofid;
2451 }
2452
bccacf6c 2453 dfidp = get_fid(pdu, dfid);
b2c224be 2454 if (dfidp == NULL) {
ffd66876 2455 err = -ENOENT;
84dfb926 2456 goto out_nofid;
b2c224be
VJ
2457 }
2458
bccacf6c 2459 oldfidp = get_fid(pdu, oldfid);
b2c224be 2460 if (oldfidp == NULL) {
ffd66876 2461 err = -ENOENT;
b2c224be
VJ
2462 goto out;
2463 }
bccacf6c 2464 err = v9fs_co_link(pdu, oldfidp, dfidp, &name);
ffd66876
VJ
2465 if (!err) {
2466 err = offset;
b2c224be 2467 }
4c158678 2468 put_fid(pdu, oldfidp);
b2c224be 2469out:
bccacf6c 2470 put_fid(pdu, dfidp);
84dfb926 2471out_nofid:
b2c224be 2472 v9fs_string_free(&name);
dc295f83 2473 pdu_complete(pdu, err);
b2c224be
VJ
2474}
2475
532decb7 2476/* Only works with path name based fid */
8440e22e 2477static void coroutine_fn v9fs_remove(void *opaque)
9f107513 2478{
5bae1900 2479 int32_t fid;
5bae1900 2480 int err = 0;
ae1ef571
VJ
2481 size_t offset = 7;
2482 V9fsFidState *fidp;
2483 V9fsPDU *pdu = opaque;
5bae1900 2484
ddca7f86
MK
2485 err = pdu_unmarshal(pdu, offset, "d", &fid);
2486 if (err < 0) {
2487 goto out_nofid;
2488 }
c572f23a 2489 trace_v9fs_remove(pdu->tag, pdu->id, fid);
5bae1900 2490
bccacf6c 2491 fidp = get_fid(pdu, fid);
ae1ef571 2492 if (fidp == NULL) {
5bae1900 2493 err = -EINVAL;
84dfb926 2494 goto out_nofid;
9f107513 2495 }
532decb7 2496 /* if fs driver is not path based, return EOPNOTSUPP */
c98f1d4a 2497 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
532decb7
AK
2498 err = -EOPNOTSUPP;
2499 goto out_err;
2500 }
7a462745
AK
2501 /*
2502 * IF the file is unlinked, we cannot reopen
2503 * the file later. So don't reclaim fd
2504 */
bccacf6c 2505 err = v9fs_mark_fids_unreclaim(pdu, &fidp->path);
7a462745
AK
2506 if (err < 0) {
2507 goto out_err;
2508 }
bccacf6c 2509 err = v9fs_co_remove(pdu, &fidp->path);
ae1ef571
VJ
2510 if (!err) {
2511 err = offset;
2512 }
7a462745 2513out_err:
ae1ef571 2514 /* For TREMOVE we need to clunk the fid even on failed remove */
84dfb926 2515 clunk_fid(pdu->s, fidp->fid);
bccacf6c 2516 put_fid(pdu, fidp);
84dfb926 2517out_nofid:
dc295f83 2518 pdu_complete(pdu, err);
9f107513
AL
2519}
2520
8440e22e 2521static void coroutine_fn v9fs_unlinkat(void *opaque)
7834cf77
AK
2522{
2523 int err = 0;
2524 V9fsString name;
67e87345 2525 int32_t dfid, flags, rflags = 0;
7834cf77 2526 size_t offset = 7;
2289be19 2527 V9fsPath path;
7834cf77
AK
2528 V9fsFidState *dfidp;
2529 V9fsPDU *pdu = opaque;
7834cf77 2530
ddca7f86
MK
2531 v9fs_string_init(&name);
2532 err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags);
2533 if (err < 0) {
2534 goto out_nofid;
2535 }
fff39a7a
GK
2536
2537 if (name_is_illegal(name.data)) {
2538 err = -ENOENT;
2539 goto out_nofid;
2540 }
2541
805b5d98
GK
2542 if (!strcmp(".", name.data)) {
2543 err = -EINVAL;
2544 goto out_nofid;
2545 }
2546
2547 if (!strcmp("..", name.data)) {
2548 err = -ENOTEMPTY;
2549 goto out_nofid;
2550 }
2551
67e87345
KF
2552 if (flags & ~P9_DOTL_AT_REMOVEDIR) {
2553 err = -EINVAL;
2554 goto out_nofid;
2555 }
2556
2557 if (flags & P9_DOTL_AT_REMOVEDIR) {
2558 rflags |= AT_REMOVEDIR;
2559 }
2560
bccacf6c 2561 dfidp = get_fid(pdu, dfid);
7834cf77
AK
2562 if (dfidp == NULL) {
2563 err = -EINVAL;
2564 goto out_nofid;
2565 }
7834cf77
AK
2566 /*
2567 * IF the file is unlinked, we cannot reopen
2568 * the file later. So don't reclaim fd
2569 */
2289be19 2570 v9fs_path_init(&path);
bccacf6c 2571 err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path);
2289be19
AK
2572 if (err < 0) {
2573 goto out_err;
2574 }
bccacf6c 2575 err = v9fs_mark_fids_unreclaim(pdu, &path);
7834cf77
AK
2576 if (err < 0) {
2577 goto out_err;
2578 }
67e87345 2579 err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, rflags);
7834cf77
AK
2580 if (!err) {
2581 err = offset;
2582 }
2583out_err:
bccacf6c 2584 put_fid(pdu, dfidp);
2289be19 2585 v9fs_path_free(&path);
7834cf77 2586out_nofid:
dc295f83 2587 pdu_complete(pdu, err);
7834cf77
AK
2588 v9fs_string_free(&name);
2589}
2590
2289be19
AK
2591
2592/* Only works with path name based fid */
8440e22e
GK
2593static int coroutine_fn v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp,
2594 int32_t newdirfid,
2595 V9fsString *name)
8cf89e00 2596{
c7b4b0b3 2597 int err = 0;
2289be19
AK
2598 V9fsPath new_path;
2599 V9fsFidState *tfidp;
bccacf6c 2600 V9fsState *s = pdu->s;
84dfb926 2601 V9fsFidState *dirfidp = NULL;
8cf89e00 2602
2289be19 2603 v9fs_path_init(&new_path);
930b1e17 2604 if (newdirfid != -1) {
bccacf6c 2605 dirfidp = get_fid(pdu, newdirfid);
c7b4b0b3
MK
2606 if (dirfidp == NULL) {
2607 err = -ENOENT;
84dfb926 2608 goto out_nofid;
c7b4b0b3 2609 }
49dd946b
GK
2610 if (fidp->fid_type != P9_FID_NONE) {
2611 err = -EINVAL;
2612 goto out;
2613 }
4fa62005
GK
2614 err = v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path);
2615 if (err < 0) {
2616 goto out;
2617 }
c7b4b0b3 2618 } else {
4d8bc733
JD
2619 char *dir_name = g_path_get_dirname(fidp->path.data);
2620 V9fsPath dir_path;
2621
2622 v9fs_path_init(&dir_path);
2623 v9fs_path_sprintf(&dir_path, "%s", dir_name);
2624 g_free(dir_name);
2625
2626 err = v9fs_co_name_to_path(pdu, &dir_path, name->data, &new_path);
2627 v9fs_path_free(&dir_path);
4fa62005
GK
2628 if (err < 0) {
2629 goto out;
2630 }
c7b4b0b3 2631 }
bccacf6c 2632 err = v9fs_co_rename(pdu, &fidp->path, &new_path);
2289be19
AK
2633 if (err < 0) {
2634 goto out;
2635 }
2636 /*
2637 * Fixup fid's pointing to the old name to
2638 * start pointing to the new name
2639 */
2640 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2641 if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) {
2642 /* replace the name */
2643 v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data));
8cf89e00
AL
2644 }
2645 }
c7b4b0b3 2646out:
84dfb926 2647 if (dirfidp) {
bccacf6c 2648 put_fid(pdu, dirfidp);
84dfb926 2649 }
2289be19 2650 v9fs_path_free(&new_path);
84dfb926 2651out_nofid:
c7b4b0b3
MK
2652 return err;
2653}
2654
532decb7 2655/* Only works with path name based fid */
8440e22e 2656static void coroutine_fn v9fs_rename(void *opaque)
c7b4b0b3
MK
2657{
2658 int32_t fid;
c7b4b0b3 2659 ssize_t err = 0;
930b1e17
AK
2660 size_t offset = 7;
2661 V9fsString name;
2662 int32_t newdirfid;
2663 V9fsFidState *fidp;
2664 V9fsPDU *pdu = opaque;
2665 V9fsState *s = pdu->s;
c7b4b0b3 2666
ddca7f86
MK
2667 v9fs_string_init(&name);
2668 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name);
2669 if (err < 0) {
2670 goto out_nofid;
2671 }
fff39a7a
GK
2672
2673 if (name_is_illegal(name.data)) {
2674 err = -ENOENT;
2675 goto out_nofid;
2676 }
2677
805b5d98
GK
2678 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2679 err = -EISDIR;
2680 goto out_nofid;
2681 }
2682
bccacf6c 2683 fidp = get_fid(pdu, fid);
930b1e17 2684 if (fidp == NULL) {
c7b4b0b3 2685 err = -ENOENT;
84dfb926 2686 goto out_nofid;
c7b4b0b3 2687 }
49dd946b
GK
2688 if (fidp->fid_type != P9_FID_NONE) {
2689 err = -EINVAL;
2690 goto out;
2691 }
532decb7 2692 /* if fs driver is not path based, return EOPNOTSUPP */
c98f1d4a 2693 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
532decb7
AK
2694 err = -EOPNOTSUPP;
2695 goto out;
2696 }
2697 v9fs_path_write_lock(s);
bccacf6c 2698 err = v9fs_complete_rename(pdu, fidp, newdirfid, &name);
532decb7 2699 v9fs_path_unlock(s);
930b1e17
AK
2700 if (!err) {
2701 err = offset;
2702 }
532decb7 2703out:
bccacf6c 2704 put_fid(pdu, fidp);
84dfb926 2705out_nofid:
dc295f83 2706 pdu_complete(pdu, err);
930b1e17 2707 v9fs_string_free(&name);
c7b4b0b3
MK
2708}
2709
4fa62005
GK
2710static int coroutine_fn v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir,
2711 V9fsString *old_name,
2712 V9fsPath *newdir,
2713 V9fsString *new_name)
2289be19
AK
2714{
2715 V9fsFidState *tfidp;
2716 V9fsPath oldpath, newpath;
bccacf6c 2717 V9fsState *s = pdu->s;
4fa62005 2718 int err;
2289be19
AK
2719
2720 v9fs_path_init(&oldpath);
2721 v9fs_path_init(&newpath);
4fa62005
GK
2722 err = v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath);
2723 if (err < 0) {
2724 goto out;
2725 }
2726 err = v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath);
2727 if (err < 0) {
2728 goto out;
2729 }
2289be19
AK
2730
2731 /*
2732 * Fixup fid's pointing to the old name to
2733 * start pointing to the new name
2734 */
2735 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2736 if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) {
2737 /* replace the name */
2738 v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data));
2739 }
2740 }
4fa62005 2741out:
2289be19
AK
2742 v9fs_path_free(&oldpath);
2743 v9fs_path_free(&newpath);
4fa62005 2744 return err;
2289be19
AK
2745}
2746
8440e22e
GK
2747static int coroutine_fn v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid,
2748 V9fsString *old_name,
2749 int32_t newdirfid,
2750 V9fsString *new_name)
89bf6593
AK
2751{
2752 int err = 0;
bccacf6c 2753 V9fsState *s = pdu->s;
89bf6593
AK
2754 V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL;
2755
bccacf6c 2756 olddirfidp = get_fid(pdu, olddirfid);
89bf6593
AK
2757 if (olddirfidp == NULL) {
2758 err = -ENOENT;
2759 goto out;
2760 }
89bf6593 2761 if (newdirfid != -1) {
bccacf6c 2762 newdirfidp = get_fid(pdu, newdirfid);
89bf6593
AK
2763 if (newdirfidp == NULL) {
2764 err = -ENOENT;
2765 goto out;
2766 }
89bf6593 2767 } else {
bccacf6c 2768 newdirfidp = get_fid(pdu, olddirfid);
89bf6593
AK
2769 }
2770
bccacf6c 2771 err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name,
2289be19
AK
2772 &newdirfidp->path, new_name);
2773 if (err < 0) {
2774 goto out;
89bf6593 2775 }
c98f1d4a 2776 if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
532decb7 2777 /* Only for path based fid we need to do the below fixup */
4fa62005
GK
2778 err = v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name,
2779 &newdirfidp->path, new_name);
532decb7 2780 }
89bf6593
AK
2781out:
2782 if (olddirfidp) {
bccacf6c 2783 put_fid(pdu, olddirfidp);
89bf6593
AK
2784 }
2785 if (newdirfidp) {
bccacf6c 2786 put_fid(pdu, newdirfidp);
89bf6593 2787 }
89bf6593
AK
2788 return err;
2789}
2790
8440e22e 2791static void coroutine_fn v9fs_renameat(void *opaque)
89bf6593
AK
2792{
2793 ssize_t err = 0;
2794 size_t offset = 7;
2795 V9fsPDU *pdu = opaque;
2796 V9fsState *s = pdu->s;
2797 int32_t olddirfid, newdirfid;
2798 V9fsString old_name, new_name;
2799
ddca7f86
MK
2800 v9fs_string_init(&old_name);
2801 v9fs_string_init(&new_name);
2802 err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid,
2803 &old_name, &newdirfid, &new_name);
2804 if (err < 0) {
2805 goto out_err;
2806 }
89bf6593 2807
fff39a7a
GK
2808 if (name_is_illegal(old_name.data) || name_is_illegal(new_name.data)) {
2809 err = -ENOENT;
2810 goto out_err;
2811 }
2812
805b5d98
GK
2813 if (!strcmp(".", old_name.data) || !strcmp("..", old_name.data) ||
2814 !strcmp(".", new_name.data) || !strcmp("..", new_name.data)) {
2815 err = -EISDIR;
2816 goto out_err;
2817 }
2818
532decb7 2819 v9fs_path_write_lock(s);
bccacf6c
AK
2820 err = v9fs_complete_renameat(pdu, olddirfid,
2821 &old_name, newdirfid, &new_name);
532decb7 2822 v9fs_path_unlock(s);
89bf6593
AK
2823 if (!err) {
2824 err = offset;
2825 }
ddca7f86
MK
2826
2827out_err:
dc295f83 2828 pdu_complete(pdu, err);
89bf6593
AK
2829 v9fs_string_free(&old_name);
2830 v9fs_string_free(&new_name);
2831}
2832
8440e22e 2833static void coroutine_fn v9fs_wstat(void *opaque)
8cf89e00 2834{
b81d685e
AK
2835 int32_t fid;
2836 int err = 0;
2837 int16_t unused;
2838 V9fsStat v9stat;
2839 size_t offset = 7;
2840 struct stat stbuf;
2841 V9fsFidState *fidp;
2842 V9fsPDU *pdu = opaque;
8cf89e00 2843
ddca7f86
MK
2844 v9fs_stat_init(&v9stat);
2845 err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat);
2846 if (err < 0) {
2847 goto out_nofid;
2848 }
c572f23a
HPB
2849 trace_v9fs_wstat(pdu->tag, pdu->id, fid,
2850 v9stat.mode, v9stat.atime, v9stat.mtime);
84dfb926 2851
bccacf6c 2852 fidp = get_fid(pdu, fid);
b81d685e
AK
2853 if (fidp == NULL) {
2854 err = -EINVAL;
84dfb926 2855 goto out_nofid;
8cf89e00 2856 }
b81d685e
AK
2857 /* do we need to sync the file? */
2858 if (donttouch_stat(&v9stat)) {
bccacf6c 2859 err = v9fs_co_fsync(pdu, fidp, 0);
8cf89e00
AL
2860 goto out;
2861 }
b81d685e
AK
2862 if (v9stat.mode != -1) {
2863 uint32_t v9_mode;
bccacf6c 2864 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
b81d685e
AK
2865 if (err < 0) {
2866 goto out;
2867 }
2868 v9_mode = stat_to_v9mode(&stbuf);
2869 if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2870 (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2871 /* Attempting to change the type */
2872 err = -EIO;
2873 goto out;
2874 }
bccacf6c 2875 err = v9fs_co_chmod(pdu, &fidp->path,
b81d685e
AK
2876 v9mode_to_mode(v9stat.mode,
2877 &v9stat.extension));
2878 if (err < 0) {
2879 goto out;
2880 }
2881 }
2882 if (v9stat.mtime != -1 || v9stat.atime != -1) {
8fc39ae4 2883 struct timespec times[2];
b81d685e
AK
2884 if (v9stat.atime != -1) {
2885 times[0].tv_sec = v9stat.atime;
8fc39ae4
SK
2886 times[0].tv_nsec = 0;
2887 } else {
2888 times[0].tv_nsec = UTIME_OMIT;
2889 }
b81d685e
AK
2890 if (v9stat.mtime != -1) {
2891 times[1].tv_sec = v9stat.mtime;
8fc39ae4
SK
2892 times[1].tv_nsec = 0;
2893 } else {
2894 times[1].tv_nsec = UTIME_OMIT;
2895 }
bccacf6c 2896 err = v9fs_co_utimensat(pdu, &fidp->path, times);
b81d685e
AK
2897 if (err < 0) {
2898 goto out;
8cf89e00
AL
2899 }
2900 }
b81d685e 2901 if (v9stat.n_gid != -1 || v9stat.n_uid != -1) {
bccacf6c 2902 err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid);
b81d685e 2903 if (err < 0) {
8cf89e00 2904 goto out;
b81d685e 2905 }
8cf89e00 2906 }
b81d685e 2907 if (v9stat.name.size != 0) {
bccacf6c 2908 err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name);
b81d685e
AK
2909 if (err < 0) {
2910 goto out;
2911 }
8cf89e00 2912 }
b81d685e 2913 if (v9stat.length != -1) {
bccacf6c 2914 err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length);
b81d685e
AK
2915 if (err < 0) {
2916 goto out;
2917 }
8cf89e00 2918 }
b81d685e 2919 err = offset;
8cf89e00 2920out:
bccacf6c 2921 put_fid(pdu, fidp);
84dfb926 2922out_nofid:
b81d685e 2923 v9fs_stat_free(&v9stat);
dc295f83 2924 pdu_complete(pdu, err);
9f107513
AL
2925}
2926
88a4763e
AK
2927static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf)
2928{
2929 uint32_t f_type;
2930 uint32_t f_bsize;
2931 uint64_t f_blocks;
2932 uint64_t f_bfree;
2933 uint64_t f_bavail;
2934 uint64_t f_files;
2935 uint64_t f_ffree;
2936 uint64_t fsid_val;
2937 uint32_t f_namelen;
2938 size_t offset = 7;
5e94c103
MK
2939 int32_t bsize_factor;
2940
5e94c103
MK
2941 /*
2942 * compute bsize factor based on host file system block size
2943 * and client msize
2944 */
88a4763e 2945 bsize_factor = (s->msize - P9_IOHDRSZ)/stbuf->f_bsize;
5e94c103
MK
2946 if (!bsize_factor) {
2947 bsize_factor = 1;
2948 }
88a4763e
AK
2949 f_type = stbuf->f_type;
2950 f_bsize = stbuf->f_bsize;
2951 f_bsize *= bsize_factor;
5e94c103
MK
2952 /*
2953 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2954 * adjust(divide) the number of blocks, free blocks and available
2955 * blocks by bsize factor
2956 */
88a4763e
AK
2957 f_blocks = stbuf->f_blocks/bsize_factor;
2958 f_bfree = stbuf->f_bfree/bsize_factor;
2959 f_bavail = stbuf->f_bavail/bsize_factor;
2960 f_files = stbuf->f_files;
2961 f_ffree = stbuf->f_ffree;
2962 fsid_val = (unsigned int) stbuf->f_fsid.__val[0] |
2963 (unsigned long long)stbuf->f_fsid.__val[1] << 32;
2964 f_namelen = stbuf->f_namelen;
be940c87 2965
88a4763e
AK
2966 return pdu_marshal(pdu, offset, "ddqqqqqqd",
2967 f_type, f_bsize, f_blocks, f_bfree,
2968 f_bavail, f_files, f_ffree,
2969 fsid_val, f_namelen);
be940c87
MK
2970}
2971
8440e22e 2972static void coroutine_fn v9fs_statfs(void *opaque)
be940c87 2973{
88a4763e
AK
2974 int32_t fid;
2975 ssize_t retval = 0;
2976 size_t offset = 7;
2977 V9fsFidState *fidp;
2978 struct statfs stbuf;
ff06030f
VJ
2979 V9fsPDU *pdu = opaque;
2980 V9fsState *s = pdu->s;
be940c87 2981
ddca7f86
MK
2982 retval = pdu_unmarshal(pdu, offset, "d", &fid);
2983 if (retval < 0) {
2984 goto out_nofid;
2985 }
bccacf6c 2986 fidp = get_fid(pdu, fid);
88a4763e
AK
2987 if (fidp == NULL) {
2988 retval = -ENOENT;
84dfb926 2989 goto out_nofid;
be940c87 2990 }
bccacf6c 2991 retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf);
88a4763e
AK
2992 if (retval < 0) {
2993 goto out;
2994 }
ddca7f86
MK
2995 retval = v9fs_fill_statfs(s, pdu, &stbuf);
2996 if (retval < 0) {
2997 goto out;
2998 }
2999 retval += offset;
be940c87 3000out:
bccacf6c 3001 put_fid(pdu, fidp);
84dfb926 3002out_nofid:
dc295f83 3003 pdu_complete(pdu, retval);
be940c87
MK
3004}
3005
8440e22e 3006static void coroutine_fn v9fs_mknod(void *opaque)
5268cecc 3007{
1b733fed
AK
3008
3009 int mode;
3010 gid_t gid;
5268cecc 3011 int32_t fid;
1b733fed 3012 V9fsQID qid;
5268cecc 3013 int err = 0;
5268cecc 3014 int major, minor;
1b733fed
AK
3015 size_t offset = 7;
3016 V9fsString name;
3017 struct stat stbuf;
1b733fed
AK
3018 V9fsFidState *fidp;
3019 V9fsPDU *pdu = opaque;
5268cecc 3020
ddca7f86
MK
3021 v9fs_string_init(&name);
3022 err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode,
3023 &major, &minor, &gid);
3024 if (err < 0) {
3025 goto out_nofid;
3026 }
c572f23a 3027 trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor);
5268cecc 3028
fff39a7a
GK
3029 if (name_is_illegal(name.data)) {
3030 err = -ENOENT;
3031 goto out_nofid;
3032 }
3033
805b5d98
GK
3034 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3035 err = -EEXIST;
3036 goto out_nofid;
3037 }
3038
bccacf6c 3039 fidp = get_fid(pdu, fid);
5268cecc
MK
3040 if (fidp == NULL) {
3041 err = -ENOENT;
84dfb926 3042 goto out_nofid;
5268cecc 3043 }
bccacf6c 3044 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid,
02cb7f3a 3045 makedev(major, minor), mode, &stbuf);
1b733fed
AK
3046 if (err < 0) {
3047 goto out;
3048 }
3049 stat_to_qid(&stbuf, &qid);
ddca7f86
MK
3050 err = pdu_marshal(pdu, offset, "Q", &qid);
3051 if (err < 0) {
3052 goto out;
3053 }
3054 err += offset;
7999f7e1
AK
3055 trace_v9fs_mknod_return(pdu->tag, pdu->id,
3056 qid.type, qid.version, qid.path);
5268cecc 3057out:
bccacf6c 3058 put_fid(pdu, fidp);
84dfb926 3059out_nofid:
dc295f83 3060 pdu_complete(pdu, err);
1b733fed 3061 v9fs_string_free(&name);
5268cecc
MK
3062}
3063
82cc3ee8
MK
3064/*
3065 * Implement posix byte range locking code
3066 * Server side handling of locking code is very simple, because 9p server in
3067 * QEMU can handle only one client. And most of the lock handling
3068 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
3069 * do any thing in * qemu 9p server side lock code path.
3070 * So when a TLOCK request comes, always return success
3071 */
8440e22e 3072static void coroutine_fn v9fs_lock(void *opaque)
82cc3ee8 3073{
ddca7f86 3074 V9fsFlock flock;
0c27bf2a
AK
3075 size_t offset = 7;
3076 struct stat stbuf;
3077 V9fsFidState *fidp;
3078 int32_t fid, err = 0;
ff06030f 3079 V9fsPDU *pdu = opaque;
82cc3ee8 3080
ddca7f86
MK
3081 v9fs_string_init(&flock.client_id);
3082 err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type,
3083 &flock.flags, &flock.start, &flock.length,
3084 &flock.proc_id, &flock.client_id);
3085 if (err < 0) {
3086 goto out_nofid;
3087 }
c572f23a 3088 trace_v9fs_lock(pdu->tag, pdu->id, fid,
ddca7f86 3089 flock.type, flock.start, flock.length);
c572f23a 3090
82cc3ee8
MK
3091
3092 /* We support only block flag now (that too ignored currently) */
ddca7f86 3093 if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) {
82cc3ee8 3094 err = -EINVAL;
84dfb926 3095 goto out_nofid;
82cc3ee8 3096 }
bccacf6c 3097 fidp = get_fid(pdu, fid);
0c27bf2a 3098 if (fidp == NULL) {
82cc3ee8 3099 err = -ENOENT;
84dfb926 3100 goto out_nofid;
82cc3ee8 3101 }
cc720ddb 3102 err = v9fs_co_fstat(pdu, fidp, &stbuf);
82cc3ee8 3103 if (err < 0) {
82cc3ee8
MK
3104 goto out;
3105 }
4bae2b39
PB
3106 err = pdu_marshal(pdu, offset, "b", P9_LOCK_SUCCESS);
3107 if (err < 0) {
3108 goto out;
3109 }
3110 err += offset;
3111 trace_v9fs_lock_return(pdu->tag, pdu->id, P9_LOCK_SUCCESS);
82cc3ee8 3112out:
bccacf6c 3113 put_fid(pdu, fidp);
84dfb926 3114out_nofid:
dc295f83 3115 pdu_complete(pdu, err);
ddca7f86 3116 v9fs_string_free(&flock.client_id);
82cc3ee8
MK
3117}
3118
8f354003
MK
3119/*
3120 * When a TGETLOCK request comes, always return success because all lock
3121 * handling is done by client's VFS layer.
3122 */
8440e22e 3123static void coroutine_fn v9fs_getlock(void *opaque)
8f354003 3124{
e4e414a4
AK
3125 size_t offset = 7;
3126 struct stat stbuf;
3127 V9fsFidState *fidp;
ddca7f86 3128 V9fsGetlock glock;
e4e414a4 3129 int32_t fid, err = 0;
ff06030f 3130 V9fsPDU *pdu = opaque;
8f354003 3131
ddca7f86
MK
3132 v9fs_string_init(&glock.client_id);
3133 err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type,
3134 &glock.start, &glock.length, &glock.proc_id,
3135 &glock.client_id);
3136 if (err < 0) {
3137 goto out_nofid;
3138 }
c572f23a 3139 trace_v9fs_getlock(pdu->tag, pdu->id, fid,
ddca7f86 3140 glock.type, glock.start, glock.length);
c572f23a 3141
bccacf6c 3142 fidp = get_fid(pdu, fid);
e4e414a4 3143 if (fidp == NULL) {
8f354003 3144 err = -ENOENT;
84dfb926 3145 goto out_nofid;
8f354003 3146 }
cc720ddb 3147 err = v9fs_co_fstat(pdu, fidp, &stbuf);
8f354003 3148 if (err < 0) {
8f354003
MK
3149 goto out;
3150 }
ddca7f86
MK
3151 glock.type = P9_LOCK_TYPE_UNLCK;
3152 err = pdu_marshal(pdu, offset, "bqqds", glock.type,
3153 glock.start, glock.length, glock.proc_id,
3154 &glock.client_id);
3155 if (err < 0) {
3156 goto out;
3157 }
3158 err += offset;
3159 trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start,
3160 glock.length, glock.proc_id);
8f354003 3161out:
bccacf6c 3162 put_fid(pdu, fidp);
84dfb926 3163out_nofid:
dc295f83 3164 pdu_complete(pdu, err);
ddca7f86 3165 v9fs_string_free(&glock.client_id);
8f354003
MK
3166}
3167
8440e22e 3168static void coroutine_fn v9fs_mkdir(void *opaque)
b67592ea 3169{
ff06030f 3170 V9fsPDU *pdu = opaque;
e84861f7 3171 size_t offset = 7;
b67592ea 3172 int32_t fid;
e84861f7 3173 struct stat stbuf;
e84861f7 3174 V9fsQID qid;
02cb7f3a 3175 V9fsString name;
b67592ea
MK
3176 V9fsFidState *fidp;
3177 gid_t gid;
3178 int mode;
e84861f7 3179 int err = 0;
b67592ea 3180
ddca7f86
MK
3181 v9fs_string_init(&name);
3182 err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid);
3183 if (err < 0) {
3184 goto out_nofid;
3185 }
c572f23a
HPB
3186 trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid);
3187
fff39a7a
GK
3188 if (name_is_illegal(name.data)) {
3189 err = -ENOENT;
3190 goto out_nofid;
3191 }
3192
805b5d98
GK
3193 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3194 err = -EEXIST;
3195 goto out_nofid;
3196 }
3197
bccacf6c 3198 fidp = get_fid(pdu, fid);
b67592ea
MK
3199 if (fidp == NULL) {
3200 err = -ENOENT;
84dfb926 3201 goto out_nofid;
b67592ea 3202 }
bccacf6c 3203 err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf);
e84861f7
VJ
3204 if (err < 0) {
3205 goto out;
3206 }
3207 stat_to_qid(&stbuf, &qid);
ddca7f86
MK
3208 err = pdu_marshal(pdu, offset, "Q", &qid);
3209 if (err < 0) {
3210 goto out;
3211 }
3212 err += offset;
7999f7e1
AK
3213 trace_v9fs_mkdir_return(pdu->tag, pdu->id,
3214 qid.type, qid.version, qid.path, err);
b67592ea 3215out:
bccacf6c 3216 put_fid(pdu, fidp);
84dfb926 3217out_nofid:
dc295f83 3218 pdu_complete(pdu, err);
e84861f7 3219 v9fs_string_free(&name);
b67592ea
MK
3220}
3221
8440e22e 3222static void coroutine_fn v9fs_xattrwalk(void *opaque)
fa32ef88 3223{
670185a6
AK
3224 int64_t size;
3225 V9fsString name;
fa32ef88 3226 ssize_t err = 0;
670185a6 3227 size_t offset = 7;
fa32ef88 3228 int32_t fid, newfid;
670185a6 3229 V9fsFidState *file_fidp;
84dfb926 3230 V9fsFidState *xattr_fidp = NULL;
670185a6
AK
3231 V9fsPDU *pdu = opaque;
3232 V9fsState *s = pdu->s;
fa32ef88 3233
ddca7f86
MK
3234 v9fs_string_init(&name);
3235 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name);
3236 if (err < 0) {
3237 goto out_nofid;
3238 }
c572f23a
HPB
3239 trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data);
3240
bccacf6c 3241 file_fidp = get_fid(pdu, fid);
670185a6 3242 if (file_fidp == NULL) {
fa32ef88 3243 err = -ENOENT;
84dfb926 3244 goto out_nofid;
fa32ef88 3245 }
670185a6
AK
3246 xattr_fidp = alloc_fid(s, newfid);
3247 if (xattr_fidp == NULL) {
fa32ef88
AK
3248 err = -EINVAL;
3249 goto out;
3250 }
2289be19 3251 v9fs_path_copy(&xattr_fidp->path, &file_fidp->path);
ba42ebb8 3252 if (!v9fs_string_size(&name)) {
fa32ef88
AK
3253 /*
3254 * listxattr request. Get the size first
3255 */
bccacf6c 3256 size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0);
670185a6
AK
3257 if (size < 0) {
3258 err = size;
84dfb926 3259 clunk_fid(s, xattr_fidp->fid);
670185a6 3260 goto out;
fa32ef88 3261 }
670185a6
AK
3262 /*
3263 * Read the xattr value
3264 */
3265 xattr_fidp->fs.xattr.len = size;
3266 xattr_fidp->fid_type = P9_FID_XATTR;
dd28fbbc 3267 xattr_fidp->fs.xattr.xattrwalk_fid = true;
a647502c 3268 xattr_fidp->fs.xattr.value = g_malloc0(size);
670185a6 3269 if (size) {
bccacf6c 3270 err = v9fs_co_llistxattr(pdu, &xattr_fidp->path,
670185a6
AK
3271 xattr_fidp->fs.xattr.value,
3272 xattr_fidp->fs.xattr.len);
3273 if (err < 0) {
84dfb926 3274 clunk_fid(s, xattr_fidp->fid);
670185a6
AK
3275 goto out;
3276 }
3277 }
ddca7f86
MK
3278 err = pdu_marshal(pdu, offset, "q", size);
3279 if (err < 0) {
3280 goto out;
3281 }
3282 err += offset;
fa32ef88
AK
3283 } else {
3284 /*
3285 * specific xattr fid. We check for xattr
3286 * presence also collect the xattr size
3287 */
bccacf6c 3288 size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
670185a6
AK
3289 &name, NULL, 0);
3290 if (size < 0) {
3291 err = size;
84dfb926 3292 clunk_fid(s, xattr_fidp->fid);
670185a6 3293 goto out;
fa32ef88 3294 }
670185a6
AK
3295 /*
3296 * Read the xattr value
3297 */
3298 xattr_fidp->fs.xattr.len = size;
3299 xattr_fidp->fid_type = P9_FID_XATTR;
dd28fbbc 3300 xattr_fidp->fs.xattr.xattrwalk_fid = true;
a647502c 3301 xattr_fidp->fs.xattr.value = g_malloc0(size);
670185a6 3302 if (size) {
bccacf6c 3303 err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
670185a6
AK
3304 &name, xattr_fidp->fs.xattr.value,
3305 xattr_fidp->fs.xattr.len);
3306 if (err < 0) {
84dfb926 3307 clunk_fid(s, xattr_fidp->fid);
670185a6
AK
3308 goto out;
3309 }
3310 }
ddca7f86
MK
3311 err = pdu_marshal(pdu, offset, "q", size);
3312 if (err < 0) {
3313 goto out;
3314 }
3315 err += offset;
fa32ef88 3316 }
7999f7e1 3317 trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size);
fa32ef88 3318out:
bccacf6c 3319 put_fid(pdu, file_fidp);
84dfb926 3320 if (xattr_fidp) {
bccacf6c 3321 put_fid(pdu, xattr_fidp);
84dfb926
AK
3322 }
3323out_nofid:
dc295f83 3324 pdu_complete(pdu, err);
670185a6 3325 v9fs_string_free(&name);
fa32ef88
AK
3326}
3327
8440e22e 3328static void coroutine_fn v9fs_xattrcreate(void *opaque)
10b468bd 3329{
aca6897f 3330 int flags, rflags = 0;
10b468bd 3331 int32_t fid;
3b79ef2c 3332 uint64_t size;
10b468bd 3333 ssize_t err = 0;
f10ff58d
AK
3334 V9fsString name;
3335 size_t offset = 7;
3336 V9fsFidState *file_fidp;
3337 V9fsFidState *xattr_fidp;
3338 V9fsPDU *pdu = opaque;
10b468bd 3339
ddca7f86
MK
3340 v9fs_string_init(&name);
3341 err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags);
3342 if (err < 0) {
3343 goto out_nofid;
3344 }
c572f23a 3345 trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags);
10b468bd 3346
aca6897f
KF
3347 if (flags & ~(P9_XATTR_CREATE | P9_XATTR_REPLACE)) {
3348 err = -EINVAL;
3349 goto out_nofid;
3350 }
3351
3352 if (flags & P9_XATTR_CREATE) {
3353 rflags |= XATTR_CREATE;
3354 }
3355
3356 if (flags & P9_XATTR_REPLACE) {
3357 rflags |= XATTR_REPLACE;
3358 }
3359
3b79ef2c
GK
3360 if (size > XATTR_SIZE_MAX) {
3361 err = -E2BIG;
3362 goto out_nofid;
3363 }
3364
bccacf6c 3365 file_fidp = get_fid(pdu, fid);
f10ff58d 3366 if (file_fidp == NULL) {
10b468bd 3367 err = -EINVAL;
84dfb926 3368 goto out_nofid;
10b468bd 3369 }
dd654e03
GK
3370 if (file_fidp->fid_type != P9_FID_NONE) {
3371 err = -EINVAL;
3372 goto out_put_fid;
3373 }
3374
10b468bd 3375 /* Make the file fid point to xattr */
f10ff58d
AK
3376 xattr_fidp = file_fidp;
3377 xattr_fidp->fid_type = P9_FID_XATTR;
3378 xattr_fidp->fs.xattr.copied_len = 0;
dd28fbbc 3379 xattr_fidp->fs.xattr.xattrwalk_fid = false;
f10ff58d 3380 xattr_fidp->fs.xattr.len = size;
aca6897f 3381 xattr_fidp->fs.xattr.flags = rflags;
f10ff58d
AK
3382 v9fs_string_init(&xattr_fidp->fs.xattr.name);
3383 v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
eb687602 3384 xattr_fidp->fs.xattr.value = g_malloc0(size);
f10ff58d 3385 err = offset;
dd654e03 3386out_put_fid:
bccacf6c 3387 put_fid(pdu, file_fidp);
84dfb926 3388out_nofid:
dc295f83 3389 pdu_complete(pdu, err);
f10ff58d 3390 v9fs_string_free(&name);
10b468bd 3391}
fa32ef88 3392
8440e22e 3393static void coroutine_fn v9fs_readlink(void *opaque)
df0973a4 3394{
ff06030f 3395 V9fsPDU *pdu = opaque;
7a5ca31e
VJ
3396 size_t offset = 7;
3397 V9fsString target;
df0973a4 3398 int32_t fid;
df0973a4
MK
3399 int err = 0;
3400 V9fsFidState *fidp;
3401
ddca7f86
MK
3402 err = pdu_unmarshal(pdu, offset, "d", &fid);
3403 if (err < 0) {
3404 goto out_nofid;
3405 }
c572f23a 3406 trace_v9fs_readlink(pdu->tag, pdu->id, fid);
bccacf6c 3407 fidp = get_fid(pdu, fid);
df0973a4
MK
3408 if (fidp == NULL) {
3409 err = -ENOENT;
84dfb926 3410 goto out_nofid;
df0973a4
MK
3411 }
3412
7a5ca31e 3413 v9fs_string_init(&target);
bccacf6c 3414 err = v9fs_co_readlink(pdu, &fidp->path, &target);
7a5ca31e
VJ
3415 if (err < 0) {
3416 goto out;
3417 }
ddca7f86
MK
3418 err = pdu_marshal(pdu, offset, "s", &target);
3419 if (err < 0) {
3420 v9fs_string_free(&target);
3421 goto out;
3422 }
3423 err += offset;
7999f7e1 3424 trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data);
7a5ca31e 3425 v9fs_string_free(&target);
df0973a4 3426out:
bccacf6c 3427 put_fid(pdu, fidp);
84dfb926 3428out_nofid:
dc295f83 3429 pdu_complete(pdu, err);
df0973a4
MK
3430}
3431
ff06030f 3432static CoroutineEntry *pdu_co_handlers[] = {
c18e2f94 3433 [P9_TREADDIR] = v9fs_readdir,
be940c87 3434 [P9_TSTATFS] = v9fs_statfs,
00ede4c2 3435 [P9_TGETATTR] = v9fs_getattr,
c79ce737 3436 [P9_TSETATTR] = v9fs_setattr,
fa32ef88 3437 [P9_TXATTRWALK] = v9fs_xattrwalk,
10b468bd 3438 [P9_TXATTRCREATE] = v9fs_xattrcreate,
5268cecc 3439 [P9_TMKNOD] = v9fs_mknod,
c7b4b0b3 3440 [P9_TRENAME] = v9fs_rename,
82cc3ee8 3441 [P9_TLOCK] = v9fs_lock,
8f354003 3442 [P9_TGETLOCK] = v9fs_getlock,
89bf6593 3443 [P9_TRENAMEAT] = v9fs_renameat,
df0973a4 3444 [P9_TREADLINK] = v9fs_readlink,
7834cf77 3445 [P9_TUNLINKAT] = v9fs_unlinkat,
b67592ea 3446 [P9_TMKDIR] = v9fs_mkdir,
9f107513 3447 [P9_TVERSION] = v9fs_version,
771e9d4c 3448 [P9_TLOPEN] = v9fs_open,
9f107513
AL
3449 [P9_TATTACH] = v9fs_attach,
3450 [P9_TSTAT] = v9fs_stat,
3451 [P9_TWALK] = v9fs_walk,
3452 [P9_TCLUNK] = v9fs_clunk,
b41e95d3 3453 [P9_TFSYNC] = v9fs_fsync,
9f107513
AL
3454 [P9_TOPEN] = v9fs_open,
3455 [P9_TREAD] = v9fs_read,
3456#if 0
3457 [P9_TAUTH] = v9fs_auth,
3458#endif
3459 [P9_TFLUSH] = v9fs_flush,
b2c224be 3460 [P9_TLINK] = v9fs_link,
08c60fc9 3461 [P9_TSYMLINK] = v9fs_symlink,
9f107513 3462 [P9_TCREATE] = v9fs_create,
c1568af5 3463 [P9_TLCREATE] = v9fs_lcreate,
9f107513
AL
3464 [P9_TWRITE] = v9fs_write,
3465 [P9_TWSTAT] = v9fs_wstat,
3466 [P9_TREMOVE] = v9fs_remove,
3467};
3468
8440e22e 3469static void coroutine_fn v9fs_op_not_supp(void *opaque)
5c3234c6 3470{
ff06030f 3471 V9fsPDU *pdu = opaque;
dc295f83 3472 pdu_complete(pdu, -EOPNOTSUPP);
5c3234c6
AK
3473}
3474
8440e22e 3475static void coroutine_fn v9fs_fs_ro(void *opaque)
2c74c2cb
MK
3476{
3477 V9fsPDU *pdu = opaque;
dc295f83 3478 pdu_complete(pdu, -EROFS);
2c74c2cb
MK
3479}
3480
3481static inline bool is_read_only_op(V9fsPDU *pdu)
3482{
3483 switch (pdu->id) {
3484 case P9_TREADDIR:
3485 case P9_TSTATFS:
3486 case P9_TGETATTR:
3487 case P9_TXATTRWALK:
3488 case P9_TLOCK:
3489 case P9_TGETLOCK:
3490 case P9_TREADLINK:
3491 case P9_TVERSION:
3492 case P9_TLOPEN:
3493 case P9_TATTACH:
3494 case P9_TSTAT:
3495 case P9_TWALK:
3496 case P9_TCLUNK:
3497 case P9_TFSYNC:
3498 case P9_TOPEN:
3499 case P9_TREAD:
3500 case P9_TAUTH:
3501 case P9_TFLUSH:
3502 return 1;
3503 default:
3504 return 0;
3505 }
3506}
3507
506f3275 3508void pdu_submit(V9fsPDU *pdu, P9MsgHeader *hdr)
9f107513 3509{
ff06030f
VJ
3510 Coroutine *co;
3511 CoroutineEntry *handler;
ad38ce9e 3512 V9fsState *s = pdu->s;
9f107513 3513
506f3275
GK
3514 pdu->size = le32_to_cpu(hdr->size_le);
3515 pdu->id = hdr->id;
3516 pdu->tag = le16_to_cpu(hdr->tag_le);
3517
ff06030f
VJ
3518 if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) ||
3519 (pdu_co_handlers[pdu->id] == NULL)) {
5c3234c6 3520 handler = v9fs_op_not_supp;
d1471233
GK
3521 } else if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) {
3522 handler = v9fs_fs_ro;
5c3234c6 3523 } else {
ff06030f 3524 handler = pdu_co_handlers[pdu->id];
5c3234c6 3525 }
2c74c2cb 3526
506f3275 3527 qemu_co_queue_init(&pdu->complete);
0b8b8753
PB
3528 co = qemu_coroutine_create(handler, pdu);
3529 qemu_coroutine_enter(co);
9f107513
AL
3530}
3531
2a0c56aa 3532/* Returns 0 on success, 1 on failure. */
066eb006
GK
3533int v9fs_device_realize_common(V9fsState *s, const V9fsTransport *t,
3534 Error **errp)
2a0c56aa
WL
3535{
3536 int i, len;
3537 struct stat stat;
3538 FsDriverEntry *fse;
3539 V9fsPath path;
3540 int rc = 1;
3541
066eb006
GK
3542 assert(!s->transport);
3543 s->transport = t;
3544
2a0c56aa
WL
3545 /* initialize pdu allocator */
3546 QLIST_INIT(&s->free_list);
3547 QLIST_INIT(&s->active_list);
0d78289c 3548 for (i = 0; i < MAX_REQ; i++) {
583f21f8
SS
3549 QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
3550 s->pdus[i].s = s;
3551 s->pdus[i].idx = i;
2a0c56aa
WL
3552 }
3553
3554 v9fs_path_init(&path);
3555
3556 fse = get_fsdev_fsentry(s->fsconf.fsdev_id);
3557
3558 if (!fse) {
3559 /* We don't have a fsdev identified by fsdev_id */
3560 error_setg(errp, "9pfs device couldn't find fsdev with the "
3561 "id = %s",
3562 s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL");
3563 goto out;
3564 }
3565
3566 if (!s->fsconf.tag) {
3567 /* we haven't specified a mount_tag */
3568 error_setg(errp, "fsdev with id %s needs mount_tag arguments",
3569 s->fsconf.fsdev_id);
3570 goto out;
3571 }
3572
3573 s->ctx.export_flags = fse->export_flags;
3574 s->ctx.fs_root = g_strdup(fse->path);
3575 s->ctx.exops.get_st_gen = NULL;
3576 len = strlen(s->fsconf.tag);
3577 if (len > MAX_TAG_LEN - 1) {
3578 error_setg(errp, "mount tag '%s' (%d bytes) is longer than "
3579 "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1);
3580 goto out;
3581 }
3582
3583 s->tag = g_strdup(s->fsconf.tag);
3584 s->ctx.uid = -1;
3585
3586 s->ops = fse->ops;
3587
b96feb2c
TS
3588 s->ctx.fmode = fse->fmode;
3589 s->ctx.dmode = fse->dmode;
3590
2a0c56aa
WL
3591 s->fid_list = NULL;
3592 qemu_co_rwlock_init(&s->rename_lock);
3593
65603a80
GK
3594 if (s->ops->init(&s->ctx, errp) < 0) {
3595 error_prepend(errp, "cannot initialize fsdev '%s': ",
3596 s->fsconf.fsdev_id);
2a0c56aa
WL
3597 goto out;
3598 }
3599
3600 /*
3601 * Check details of export path, We need to use fs driver
3602 * call back to do that. Since we are in the init path, we don't
3603 * use co-routines here.
3604 */
3605 if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) {
3606 error_setg(errp,
3607 "error in converting name to path %s", strerror(errno));
3608 goto out;
3609 }
3610 if (s->ops->lstat(&s->ctx, &path, &stat)) {
3611 error_setg(errp, "share path %s does not exist", fse->path);
3612 goto out;
3613 } else if (!S_ISDIR(stat.st_mode)) {
3614 error_setg(errp, "share path %s is not a directory", fse->path);
3615 goto out;
3616 }
b8bbdb88
PJ
3617
3618 s->ctx.fst = &fse->fst;
3619 fsdev_throttle_init(s->ctx.fst);
3620
2a0c56aa
WL
3621 v9fs_path_free(&path);
3622
3623 rc = 0;
3624out:
3625 if (rc) {
f2b58c43 3626 if (s->ops && s->ops->cleanup && s->ctx.private) {
702dbcc2
LQ
3627 s->ops->cleanup(&s->ctx);
3628 }
2a0c56aa 3629 g_free(s->tag);
4774718e 3630 g_free(s->ctx.fs_root);
2a0c56aa
WL
3631 v9fs_path_free(&path);
3632 }
3633 return rc;
3634}
3635
3636void v9fs_device_unrealize_common(V9fsState *s, Error **errp)
3637{
702dbcc2
LQ
3638 if (s->ops->cleanup) {
3639 s->ops->cleanup(&s->ctx);
3640 }
b8bbdb88 3641 fsdev_throttle_cleanup(s->ctx.fst);
2a0c56aa 3642 g_free(s->tag);
4774718e 3643 g_free(s->ctx.fs_root);
2a0c56aa
WL
3644}
3645
0e44a0fd
GK
3646typedef struct VirtfsCoResetData {
3647 V9fsPDU pdu;
3648 bool done;
3649} VirtfsCoResetData;
3650
3651static void coroutine_fn virtfs_co_reset(void *opaque)
3652{
3653 VirtfsCoResetData *data = opaque;
3654
3655 virtfs_reset(&data->pdu);
3656 data->done = true;
3657}
3658
3659void v9fs_reset(V9fsState *s)
3660{
3661 VirtfsCoResetData data = { .pdu = { .s = s }, .done = false };
3662 Coroutine *co;
3663
3664 while (!QLIST_EMPTY(&s->active_list)) {
3665 aio_poll(qemu_get_aio_context(), true);
3666 }
3667
3668 co = qemu_coroutine_create(virtfs_co_reset, &data);
3669 qemu_coroutine_enter(co);
3670
3671 while (!data.done) {
3672 aio_poll(qemu_get_aio_context(), true);
3673 }
3674}
3675
72a18977 3676static void __attribute__((__constructor__)) v9fs_set_fd_limit(void)
7a462745
AK
3677{
3678 struct rlimit rlim;
3679 if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
63325b18 3680 error_report("Failed to get the resource limit");
7a462745
AK
3681 exit(1);
3682 }
3683 open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur/3);
3684 open_fd_rc = rlim.rlim_cur/2;
3685}
This page took 1.17328 seconds and 4 git commands to generate.