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