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