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