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