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