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