hw/9pfs: Use read-write lock for protecting fid path.
[qemu/kevin.git] / hw / 9pfs / virtio-9p.c
blob356bb3b0958a39fe6cc2849d1257fd4bee646999
1 /*
2 * Virtio 9p backend
4 * Copyright IBM, Corp. 2010
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
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"
24 int debug_9p_pdu;
25 int open_fd_hw;
26 int total_open_fd;
27 static int open_fd_rc;
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,
41 static int omode_to_uflags(int8_t mode)
43 int ret = 0;
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;
60 if (mode & Otrunc) {
61 ret |= O_TRUNC;
64 if (mode & Oappend) {
65 ret |= O_APPEND;
68 if (mode & Oexcl) {
69 ret |= O_EXCL;
72 return ret;
75 void cred_init(FsCred *credp)
77 credp->fc_uid = -1;
78 credp->fc_gid = -1;
79 credp->fc_mode = -1;
80 credp->fc_rdev = -1;
83 void v9fs_string_init(V9fsString *str)
85 str->data = NULL;
86 str->size = 0;
89 void v9fs_string_free(V9fsString *str)
91 g_free(str->data);
92 str->data = NULL;
93 str->size = 0;
96 void v9fs_string_null(V9fsString *str)
98 v9fs_string_free(str);
101 static int number_to_string(void *arg, char type)
103 unsigned int ret = 0;
105 switch (type) {
106 case 'u': {
107 unsigned int num = *(unsigned int *)arg;
109 do {
110 ret++;
111 num = num/10;
112 } while (num);
113 break;
115 case 'U': {
116 unsigned long num = *(unsigned long *)arg;
117 do {
118 ret++;
119 num = num/10;
120 } while (num);
121 break;
123 default:
124 printf("Number_to_string: Unknown number format\n");
125 return -1;
128 return ret;
131 static int GCC_FMT_ATTR(2, 0)
132 v9fs_string_alloc_printf(char **strp, const char *fmt, va_list ap)
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;
142 /* Find the number of %'s that denotes an argument */
143 for (iter = strstr(iter, "%"); iter; iter = strstr(iter, "%")) {
144 nr_args++;
145 iter++;
148 len = strlen(fmt) - 2*nr_args;
150 if (!nr_args) {
151 goto alloc_print;
154 va_copy(ap2, ap);
156 iter = (char *)fmt;
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;
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;
186 iter++;
189 alloc_print:
190 *strp = g_malloc((len + 1) * sizeof(**strp));
192 return vsprintf(*strp, fmt, ap);
195 void GCC_FMT_ATTR(2, 3)
196 v9fs_string_sprintf(V9fsString *str, const char *fmt, ...)
198 va_list ap;
199 int err;
201 v9fs_string_free(str);
203 va_start(ap, fmt);
204 err = v9fs_string_alloc_printf(&str->data, fmt, ap);
205 BUG_ON(err == -1);
206 va_end(ap);
208 str->size = err;
211 void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs)
213 v9fs_string_free(lhs);
214 v9fs_string_sprintf(lhs, "%s", rhs->data);
218 * Return TRUE if s1 is an ancestor of s2.
220 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
221 * As a special case, We treat s1 as ancestor of s2 if they are same!
223 static int v9fs_path_is_ancestor(V9fsString *s1, V9fsString *s2)
225 if (!strncmp(s1->data, s2->data, s1->size)) {
226 if (s2->data[s1->size] == '\0' || s2->data[s1->size] == '/') {
227 return 1;
230 return 0;
233 static size_t v9fs_string_size(V9fsString *str)
235 return str->size;
239 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
240 static int v9fs_reopen_fid(V9fsState *s, V9fsFidState *f)
242 int err = 1;
243 if (f->fid_type == P9_FID_FILE) {
244 if (f->fs.fd == -1) {
245 do {
246 err = v9fs_co_open(s, f, f->open_flags);
247 } while (err == -EINTR);
249 } else if (f->fid_type == P9_FID_DIR) {
250 if (f->fs.dir == NULL) {
251 do {
252 err = v9fs_co_opendir(s, f);
253 } while (err == -EINTR);
256 return err;
259 static V9fsFidState *get_fid(V9fsState *s, int32_t fid)
261 int err;
262 V9fsFidState *f;
264 for (f = s->fid_list; f; f = f->next) {
265 BUG_ON(f->clunked);
266 if (f->fid == fid) {
268 * Update the fid ref upfront so that
269 * we don't get reclaimed when we yield
270 * in open later.
272 f->ref++;
274 * check whether we need to reopen the
275 * file. We might have closed the fd
276 * while trying to free up some file
277 * descriptors.
279 err = v9fs_reopen_fid(s, f);
280 if (err < 0) {
281 f->ref--;
282 return NULL;
285 * Mark the fid as referenced so that the LRU
286 * reclaim won't close the file descriptor
288 f->flags |= FID_REFERENCED;
289 return f;
292 return NULL;
295 static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
297 V9fsFidState *f;
299 for (f = s->fid_list; f; f = f->next) {
300 /* If fid is already there return NULL */
301 BUG_ON(f->clunked);
302 if (f->fid == fid) {
303 return NULL;
306 f = g_malloc0(sizeof(V9fsFidState));
307 f->fid = fid;
308 f->fid_type = P9_FID_NONE;
309 f->ref = 1;
311 * Mark the fid as referenced so that the LRU
312 * reclaim won't close the file descriptor
314 f->flags |= FID_REFERENCED;
315 f->next = s->fid_list;
316 s->fid_list = f;
318 return f;
321 static int v9fs_xattr_fid_clunk(V9fsState *s, V9fsFidState *fidp)
323 int retval = 0;
325 if (fidp->fs.xattr.copied_len == -1) {
326 /* getxattr/listxattr fid */
327 goto free_value;
330 * if this is fid for setxattr. clunk should
331 * result in setxattr localcall
333 if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) {
334 /* clunk after partial write */
335 retval = -EINVAL;
336 goto free_out;
338 if (fidp->fs.xattr.len) {
339 retval = v9fs_co_lsetxattr(s, &fidp->path, &fidp->fs.xattr.name,
340 fidp->fs.xattr.value,
341 fidp->fs.xattr.len,
342 fidp->fs.xattr.flags);
343 } else {
344 retval = v9fs_co_lremovexattr(s, &fidp->path, &fidp->fs.xattr.name);
346 free_out:
347 v9fs_string_free(&fidp->fs.xattr.name);
348 free_value:
349 if (fidp->fs.xattr.value) {
350 g_free(fidp->fs.xattr.value);
352 return retval;
355 static int free_fid(V9fsState *s, V9fsFidState *fidp)
357 int retval = 0;
359 if (fidp->fid_type == P9_FID_FILE) {
360 /* If we reclaimed the fd no need to close */
361 if (fidp->fs.fd != -1) {
362 retval = v9fs_co_close(s, fidp->fs.fd);
364 } else if (fidp->fid_type == P9_FID_DIR) {
365 if (fidp->fs.dir != NULL) {
366 retval = v9fs_co_closedir(s, fidp->fs.dir);
368 } else if (fidp->fid_type == P9_FID_XATTR) {
369 retval = v9fs_xattr_fid_clunk(s, fidp);
371 v9fs_string_free(&fidp->path);
372 g_free(fidp);
373 return retval;
376 static void put_fid(V9fsState *s, V9fsFidState *fidp)
378 BUG_ON(!fidp->ref);
379 fidp->ref--;
381 * Don't free the fid if it is in reclaim list
383 if (!fidp->ref && fidp->clunked) {
384 free_fid(s, fidp);
388 static int clunk_fid(V9fsState *s, int32_t fid)
390 V9fsFidState **fidpp, *fidp;
392 for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
393 if ((*fidpp)->fid == fid) {
394 break;
398 if (*fidpp == NULL) {
399 return -ENOENT;
401 fidp = *fidpp;
402 *fidpp = fidp->next;
403 fidp->clunked = 1;
404 return 0;
407 void v9fs_reclaim_fd(V9fsState *s)
409 int reclaim_count = 0;
410 V9fsFidState *f, *reclaim_list = NULL;
412 for (f = s->fid_list; f; f = f->next) {
414 * Unlink fids cannot be reclaimed. Check
415 * for them and skip them. Also skip fids
416 * currently being operated on.
418 if (f->ref || f->flags & FID_NON_RECLAIMABLE) {
419 continue;
422 * if it is a recently referenced fid
423 * we leave the fid untouched and clear the
424 * reference bit. We come back to it later
425 * in the next iteration. (a simple LRU without
426 * moving list elements around)
428 if (f->flags & FID_REFERENCED) {
429 f->flags &= ~FID_REFERENCED;
430 continue;
433 * Add fids to reclaim list.
435 if (f->fid_type == P9_FID_FILE) {
436 if (f->fs.fd != -1) {
438 * Up the reference count so that
439 * a clunk request won't free this fid
441 f->ref++;
442 f->rclm_lst = reclaim_list;
443 reclaim_list = f;
444 f->fs_reclaim.fd = f->fs.fd;
445 f->fs.fd = -1;
446 reclaim_count++;
448 } else if (f->fid_type == P9_FID_DIR) {
449 if (f->fs.dir != NULL) {
451 * Up the reference count so that
452 * a clunk request won't free this fid
454 f->ref++;
455 f->rclm_lst = reclaim_list;
456 reclaim_list = f;
457 f->fs_reclaim.dir = f->fs.dir;
458 f->fs.dir = NULL;
459 reclaim_count++;
462 if (reclaim_count >= open_fd_rc) {
463 break;
467 * Now close the fid in reclaim list. Free them if they
468 * are already clunked.
470 while (reclaim_list) {
471 f = reclaim_list;
472 reclaim_list = f->rclm_lst;
473 if (f->fid_type == P9_FID_FILE) {
474 v9fs_co_close(s, f->fs_reclaim.fd);
475 } else if (f->fid_type == P9_FID_DIR) {
476 v9fs_co_closedir(s, f->fs_reclaim.dir);
478 f->rclm_lst = NULL;
480 * Now drop the fid reference, free it
481 * if clunked.
483 put_fid(s, f);
487 static int v9fs_mark_fids_unreclaim(V9fsState *s, V9fsString *str)
489 int err;
490 V9fsFidState *fidp, head_fid;
492 head_fid.next = s->fid_list;
493 for (fidp = s->fid_list; fidp; fidp = fidp->next) {
494 if (!strcmp(fidp->path.data, str->data)) {
495 /* Mark the fid non reclaimable. */
496 fidp->flags |= FID_NON_RECLAIMABLE;
498 /* reopen the file/dir if already closed */
499 err = v9fs_reopen_fid(s, fidp);
500 if (err < 0) {
501 return -1;
504 * Go back to head of fid list because
505 * the list could have got updated when
506 * switched to the worker thread
508 if (err == 0) {
509 fidp = &head_fid;
513 return 0;
516 #define P9_QID_TYPE_DIR 0x80
517 #define P9_QID_TYPE_SYMLINK 0x02
519 #define P9_STAT_MODE_DIR 0x80000000
520 #define P9_STAT_MODE_APPEND 0x40000000
521 #define P9_STAT_MODE_EXCL 0x20000000
522 #define P9_STAT_MODE_MOUNT 0x10000000
523 #define P9_STAT_MODE_AUTH 0x08000000
524 #define P9_STAT_MODE_TMP 0x04000000
525 #define P9_STAT_MODE_SYMLINK 0x02000000
526 #define P9_STAT_MODE_LINK 0x01000000
527 #define P9_STAT_MODE_DEVICE 0x00800000
528 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
529 #define P9_STAT_MODE_SOCKET 0x00100000
530 #define P9_STAT_MODE_SETUID 0x00080000
531 #define P9_STAT_MODE_SETGID 0x00040000
532 #define P9_STAT_MODE_SETVTX 0x00010000
534 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
535 P9_STAT_MODE_SYMLINK | \
536 P9_STAT_MODE_LINK | \
537 P9_STAT_MODE_DEVICE | \
538 P9_STAT_MODE_NAMED_PIPE | \
539 P9_STAT_MODE_SOCKET)
541 /* This is the algorithm from ufs in spfs */
542 static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
544 size_t size;
546 memset(&qidp->path, 0, sizeof(qidp->path));
547 size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
548 memcpy(&qidp->path, &stbuf->st_ino, size);
549 qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
550 qidp->type = 0;
551 if (S_ISDIR(stbuf->st_mode)) {
552 qidp->type |= P9_QID_TYPE_DIR;
554 if (S_ISLNK(stbuf->st_mode)) {
555 qidp->type |= P9_QID_TYPE_SYMLINK;
559 static int fid_to_qid(V9fsState *s, V9fsFidState *fidp, V9fsQID *qidp)
561 struct stat stbuf;
562 int err;
564 err = v9fs_co_lstat(s, &fidp->path, &stbuf);
565 if (err < 0) {
566 return err;
568 stat_to_qid(&stbuf, qidp);
569 return 0;
572 static V9fsPDU *alloc_pdu(V9fsState *s)
574 V9fsPDU *pdu = NULL;
576 if (!QLIST_EMPTY(&s->free_list)) {
577 pdu = QLIST_FIRST(&s->free_list);
578 QLIST_REMOVE(pdu, next);
580 return pdu;
583 static void free_pdu(V9fsState *s, V9fsPDU *pdu)
585 if (pdu) {
586 if (debug_9p_pdu) {
587 pprint_pdu(pdu);
589 QLIST_INSERT_HEAD(&s->free_list, pdu, next);
593 size_t pdu_packunpack(void *addr, struct iovec *sg, int sg_count,
594 size_t offset, size_t size, int pack)
596 int i = 0;
597 size_t copied = 0;
599 for (i = 0; size && i < sg_count; i++) {
600 size_t len;
601 if (offset >= sg[i].iov_len) {
602 /* skip this sg */
603 offset -= sg[i].iov_len;
604 continue;
605 } else {
606 len = MIN(sg[i].iov_len - offset, size);
607 if (pack) {
608 memcpy(sg[i].iov_base + offset, addr, len);
609 } else {
610 memcpy(addr, sg[i].iov_base + offset, len);
612 size -= len;
613 copied += len;
614 addr += len;
615 if (size) {
616 offset = 0;
617 continue;
622 return copied;
625 static size_t pdu_unpack(void *dst, V9fsPDU *pdu, size_t offset, size_t size)
627 return pdu_packunpack(dst, pdu->elem.out_sg, pdu->elem.out_num,
628 offset, size, 0);
631 static size_t pdu_pack(V9fsPDU *pdu, size_t offset, const void *src,
632 size_t size)
634 return pdu_packunpack((void *)src, pdu->elem.in_sg, pdu->elem.in_num,
635 offset, size, 1);
638 static int pdu_copy_sg(V9fsPDU *pdu, size_t offset, int rx, struct iovec *sg)
640 size_t pos = 0;
641 int i, j;
642 struct iovec *src_sg;
643 unsigned int num;
645 if (rx) {
646 src_sg = pdu->elem.in_sg;
647 num = pdu->elem.in_num;
648 } else {
649 src_sg = pdu->elem.out_sg;
650 num = pdu->elem.out_num;
653 j = 0;
654 for (i = 0; i < num; i++) {
655 if (offset <= pos) {
656 sg[j].iov_base = src_sg[i].iov_base;
657 sg[j].iov_len = src_sg[i].iov_len;
658 j++;
659 } else if (offset < (src_sg[i].iov_len + pos)) {
660 sg[j].iov_base = src_sg[i].iov_base;
661 sg[j].iov_len = src_sg[i].iov_len;
662 sg[j].iov_base += (offset - pos);
663 sg[j].iov_len -= (offset - pos);
664 j++;
666 pos += src_sg[i].iov_len;
669 return j;
672 static size_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
674 size_t old_offset = offset;
675 va_list ap;
676 int i;
678 va_start(ap, fmt);
679 for (i = 0; fmt[i]; i++) {
680 switch (fmt[i]) {
681 case 'b': {
682 uint8_t *valp = va_arg(ap, uint8_t *);
683 offset += pdu_unpack(valp, pdu, offset, sizeof(*valp));
684 break;
686 case 'w': {
687 uint16_t val, *valp;
688 valp = va_arg(ap, uint16_t *);
689 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
690 *valp = le16_to_cpu(val);
691 break;
693 case 'd': {
694 uint32_t val, *valp;
695 valp = va_arg(ap, uint32_t *);
696 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
697 *valp = le32_to_cpu(val);
698 break;
700 case 'q': {
701 uint64_t val, *valp;
702 valp = va_arg(ap, uint64_t *);
703 offset += pdu_unpack(&val, pdu, offset, sizeof(val));
704 *valp = le64_to_cpu(val);
705 break;
707 case 'v': {
708 struct iovec *iov = va_arg(ap, struct iovec *);
709 int *iovcnt = va_arg(ap, int *);
710 *iovcnt = pdu_copy_sg(pdu, offset, 0, iov);
711 break;
713 case 's': {
714 V9fsString *str = va_arg(ap, V9fsString *);
715 offset += pdu_unmarshal(pdu, offset, "w", &str->size);
716 /* FIXME: sanity check str->size */
717 str->data = g_malloc(str->size + 1);
718 offset += pdu_unpack(str->data, pdu, offset, str->size);
719 str->data[str->size] = 0;
720 break;
722 case 'Q': {
723 V9fsQID *qidp = va_arg(ap, V9fsQID *);
724 offset += pdu_unmarshal(pdu, offset, "bdq",
725 &qidp->type, &qidp->version, &qidp->path);
726 break;
728 case 'S': {
729 V9fsStat *statp = va_arg(ap, V9fsStat *);
730 offset += pdu_unmarshal(pdu, offset, "wwdQdddqsssssddd",
731 &statp->size, &statp->type, &statp->dev,
732 &statp->qid, &statp->mode, &statp->atime,
733 &statp->mtime, &statp->length,
734 &statp->name, &statp->uid, &statp->gid,
735 &statp->muid, &statp->extension,
736 &statp->n_uid, &statp->n_gid,
737 &statp->n_muid);
738 break;
740 case 'I': {
741 V9fsIattr *iattr = va_arg(ap, V9fsIattr *);
742 offset += pdu_unmarshal(pdu, offset, "ddddqqqqq",
743 &iattr->valid, &iattr->mode,
744 &iattr->uid, &iattr->gid, &iattr->size,
745 &iattr->atime_sec, &iattr->atime_nsec,
746 &iattr->mtime_sec, &iattr->mtime_nsec);
747 break;
749 default:
750 break;
754 va_end(ap);
756 return offset - old_offset;
759 static size_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
761 size_t old_offset = offset;
762 va_list ap;
763 int i;
765 va_start(ap, fmt);
766 for (i = 0; fmt[i]; i++) {
767 switch (fmt[i]) {
768 case 'b': {
769 uint8_t val = va_arg(ap, int);
770 offset += pdu_pack(pdu, offset, &val, sizeof(val));
771 break;
773 case 'w': {
774 uint16_t val;
775 cpu_to_le16w(&val, va_arg(ap, int));
776 offset += pdu_pack(pdu, offset, &val, sizeof(val));
777 break;
779 case 'd': {
780 uint32_t val;
781 cpu_to_le32w(&val, va_arg(ap, uint32_t));
782 offset += pdu_pack(pdu, offset, &val, sizeof(val));
783 break;
785 case 'q': {
786 uint64_t val;
787 cpu_to_le64w(&val, va_arg(ap, uint64_t));
788 offset += pdu_pack(pdu, offset, &val, sizeof(val));
789 break;
791 case 'v': {
792 struct iovec *iov = va_arg(ap, struct iovec *);
793 int *iovcnt = va_arg(ap, int *);
794 *iovcnt = pdu_copy_sg(pdu, offset, 1, iov);
795 break;
797 case 's': {
798 V9fsString *str = va_arg(ap, V9fsString *);
799 offset += pdu_marshal(pdu, offset, "w", str->size);
800 offset += pdu_pack(pdu, offset, str->data, str->size);
801 break;
803 case 'Q': {
804 V9fsQID *qidp = va_arg(ap, V9fsQID *);
805 offset += pdu_marshal(pdu, offset, "bdq",
806 qidp->type, qidp->version, qidp->path);
807 break;
809 case 'S': {
810 V9fsStat *statp = va_arg(ap, V9fsStat *);
811 offset += pdu_marshal(pdu, offset, "wwdQdddqsssssddd",
812 statp->size, statp->type, statp->dev,
813 &statp->qid, statp->mode, statp->atime,
814 statp->mtime, statp->length, &statp->name,
815 &statp->uid, &statp->gid, &statp->muid,
816 &statp->extension, statp->n_uid,
817 statp->n_gid, statp->n_muid);
818 break;
820 case 'A': {
821 V9fsStatDotl *statp = va_arg(ap, V9fsStatDotl *);
822 offset += pdu_marshal(pdu, offset, "qQdddqqqqqqqqqqqqqqq",
823 statp->st_result_mask,
824 &statp->qid, statp->st_mode,
825 statp->st_uid, statp->st_gid,
826 statp->st_nlink, statp->st_rdev,
827 statp->st_size, statp->st_blksize, statp->st_blocks,
828 statp->st_atime_sec, statp->st_atime_nsec,
829 statp->st_mtime_sec, statp->st_mtime_nsec,
830 statp->st_ctime_sec, statp->st_ctime_nsec,
831 statp->st_btime_sec, statp->st_btime_nsec,
832 statp->st_gen, statp->st_data_version);
833 break;
835 default:
836 break;
839 va_end(ap);
841 return offset - old_offset;
844 static void complete_pdu(V9fsState *s, V9fsPDU *pdu, ssize_t len)
846 int8_t id = pdu->id + 1; /* Response */
848 if (len < 0) {
849 int err = -len;
850 len = 7;
852 if (s->proto_version != V9FS_PROTO_2000L) {
853 V9fsString str;
855 str.data = strerror(err);
856 str.size = strlen(str.data);
858 len += pdu_marshal(pdu, len, "s", &str);
859 id = P9_RERROR;
862 len += pdu_marshal(pdu, len, "d", err);
864 if (s->proto_version == V9FS_PROTO_2000L) {
865 id = P9_RLERROR;
869 /* fill out the header */
870 pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
872 /* keep these in sync */
873 pdu->size = len;
874 pdu->id = id;
876 /* push onto queue and notify */
877 virtqueue_push(s->vq, &pdu->elem, len);
879 /* FIXME: we should batch these completions */
880 virtio_notify(&s->vdev, s->vq);
882 free_pdu(s, pdu);
885 static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
887 mode_t ret;
889 ret = mode & 0777;
890 if (mode & P9_STAT_MODE_DIR) {
891 ret |= S_IFDIR;
894 if (mode & P9_STAT_MODE_SYMLINK) {
895 ret |= S_IFLNK;
897 if (mode & P9_STAT_MODE_SOCKET) {
898 ret |= S_IFSOCK;
900 if (mode & P9_STAT_MODE_NAMED_PIPE) {
901 ret |= S_IFIFO;
903 if (mode & P9_STAT_MODE_DEVICE) {
904 if (extension && extension->data[0] == 'c') {
905 ret |= S_IFCHR;
906 } else {
907 ret |= S_IFBLK;
911 if (!(ret&~0777)) {
912 ret |= S_IFREG;
915 if (mode & P9_STAT_MODE_SETUID) {
916 ret |= S_ISUID;
918 if (mode & P9_STAT_MODE_SETGID) {
919 ret |= S_ISGID;
921 if (mode & P9_STAT_MODE_SETVTX) {
922 ret |= S_ISVTX;
925 return ret;
928 static int donttouch_stat(V9fsStat *stat)
930 if (stat->type == -1 &&
931 stat->dev == -1 &&
932 stat->qid.type == -1 &&
933 stat->qid.version == -1 &&
934 stat->qid.path == -1 &&
935 stat->mode == -1 &&
936 stat->atime == -1 &&
937 stat->mtime == -1 &&
938 stat->length == -1 &&
939 !stat->name.size &&
940 !stat->uid.size &&
941 !stat->gid.size &&
942 !stat->muid.size &&
943 stat->n_uid == -1 &&
944 stat->n_gid == -1 &&
945 stat->n_muid == -1) {
946 return 1;
949 return 0;
952 static void v9fs_stat_free(V9fsStat *stat)
954 v9fs_string_free(&stat->name);
955 v9fs_string_free(&stat->uid);
956 v9fs_string_free(&stat->gid);
957 v9fs_string_free(&stat->muid);
958 v9fs_string_free(&stat->extension);
961 static uint32_t stat_to_v9mode(const struct stat *stbuf)
963 uint32_t mode;
965 mode = stbuf->st_mode & 0777;
966 if (S_ISDIR(stbuf->st_mode)) {
967 mode |= P9_STAT_MODE_DIR;
970 if (S_ISLNK(stbuf->st_mode)) {
971 mode |= P9_STAT_MODE_SYMLINK;
974 if (S_ISSOCK(stbuf->st_mode)) {
975 mode |= P9_STAT_MODE_SOCKET;
978 if (S_ISFIFO(stbuf->st_mode)) {
979 mode |= P9_STAT_MODE_NAMED_PIPE;
982 if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
983 mode |= P9_STAT_MODE_DEVICE;
986 if (stbuf->st_mode & S_ISUID) {
987 mode |= P9_STAT_MODE_SETUID;
990 if (stbuf->st_mode & S_ISGID) {
991 mode |= P9_STAT_MODE_SETGID;
994 if (stbuf->st_mode & S_ISVTX) {
995 mode |= P9_STAT_MODE_SETVTX;
998 return mode;
1001 static int stat_to_v9stat(V9fsState *s, V9fsString *name,
1002 const struct stat *stbuf,
1003 V9fsStat *v9stat)
1005 int err;
1006 const char *str;
1008 memset(v9stat, 0, sizeof(*v9stat));
1010 stat_to_qid(stbuf, &v9stat->qid);
1011 v9stat->mode = stat_to_v9mode(stbuf);
1012 v9stat->atime = stbuf->st_atime;
1013 v9stat->mtime = stbuf->st_mtime;
1014 v9stat->length = stbuf->st_size;
1016 v9fs_string_null(&v9stat->uid);
1017 v9fs_string_null(&v9stat->gid);
1018 v9fs_string_null(&v9stat->muid);
1020 v9stat->n_uid = stbuf->st_uid;
1021 v9stat->n_gid = stbuf->st_gid;
1022 v9stat->n_muid = 0;
1024 v9fs_string_null(&v9stat->extension);
1026 if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
1027 err = v9fs_co_readlink(s, name, &v9stat->extension);
1028 if (err < 0) {
1029 return err;
1031 } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
1032 v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
1033 S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
1034 major(stbuf->st_rdev), minor(stbuf->st_rdev));
1035 } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
1036 v9fs_string_sprintf(&v9stat->extension, "%s %lu",
1037 "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink);
1040 str = strrchr(name->data, '/');
1041 if (str) {
1042 str += 1;
1043 } else {
1044 str = name->data;
1047 v9fs_string_sprintf(&v9stat->name, "%s", str);
1049 v9stat->size = 61 +
1050 v9fs_string_size(&v9stat->name) +
1051 v9fs_string_size(&v9stat->uid) +
1052 v9fs_string_size(&v9stat->gid) +
1053 v9fs_string_size(&v9stat->muid) +
1054 v9fs_string_size(&v9stat->extension);
1055 return 0;
1058 #define P9_STATS_MODE 0x00000001ULL
1059 #define P9_STATS_NLINK 0x00000002ULL
1060 #define P9_STATS_UID 0x00000004ULL
1061 #define P9_STATS_GID 0x00000008ULL
1062 #define P9_STATS_RDEV 0x00000010ULL
1063 #define P9_STATS_ATIME 0x00000020ULL
1064 #define P9_STATS_MTIME 0x00000040ULL
1065 #define P9_STATS_CTIME 0x00000080ULL
1066 #define P9_STATS_INO 0x00000100ULL
1067 #define P9_STATS_SIZE 0x00000200ULL
1068 #define P9_STATS_BLOCKS 0x00000400ULL
1070 #define P9_STATS_BTIME 0x00000800ULL
1071 #define P9_STATS_GEN 0x00001000ULL
1072 #define P9_STATS_DATA_VERSION 0x00002000ULL
1074 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
1075 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
1078 static void stat_to_v9stat_dotl(V9fsState *s, const struct stat *stbuf,
1079 V9fsStatDotl *v9lstat)
1081 memset(v9lstat, 0, sizeof(*v9lstat));
1083 v9lstat->st_mode = stbuf->st_mode;
1084 v9lstat->st_nlink = stbuf->st_nlink;
1085 v9lstat->st_uid = stbuf->st_uid;
1086 v9lstat->st_gid = stbuf->st_gid;
1087 v9lstat->st_rdev = stbuf->st_rdev;
1088 v9lstat->st_size = stbuf->st_size;
1089 v9lstat->st_blksize = stbuf->st_blksize;
1090 v9lstat->st_blocks = stbuf->st_blocks;
1091 v9lstat->st_atime_sec = stbuf->st_atime;
1092 v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
1093 v9lstat->st_mtime_sec = stbuf->st_mtime;
1094 v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
1095 v9lstat->st_ctime_sec = stbuf->st_ctime;
1096 v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
1097 /* Currently we only support BASIC fields in stat */
1098 v9lstat->st_result_mask = P9_STATS_BASIC;
1100 stat_to_qid(stbuf, &v9lstat->qid);
1103 static struct iovec *adjust_sg(struct iovec *sg, int len, int *iovcnt)
1105 while (len && *iovcnt) {
1106 if (len < sg->iov_len) {
1107 sg->iov_len -= len;
1108 sg->iov_base += len;
1109 len = 0;
1110 } else {
1111 len -= sg->iov_len;
1112 sg++;
1113 *iovcnt -= 1;
1117 return sg;
1120 static struct iovec *cap_sg(struct iovec *sg, int cap, int *cnt)
1122 int i;
1123 int total = 0;
1125 for (i = 0; i < *cnt; i++) {
1126 if ((total + sg[i].iov_len) > cap) {
1127 sg[i].iov_len -= ((total + sg[i].iov_len) - cap);
1128 i++;
1129 break;
1131 total += sg[i].iov_len;
1134 *cnt = i;
1136 return sg;
1139 static void print_sg(struct iovec *sg, int cnt)
1141 int i;
1143 printf("sg[%d]: {", cnt);
1144 for (i = 0; i < cnt; i++) {
1145 if (i) {
1146 printf(", ");
1148 printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
1150 printf("}\n");
1153 static void v9fs_fix_path(V9fsString *dst, V9fsString *src, int len)
1155 V9fsString str;
1156 v9fs_string_init(&str);
1157 v9fs_string_copy(&str, dst);
1158 v9fs_string_sprintf(dst, "%s%s", src->data, str.data+len);
1159 v9fs_string_free(&str);
1162 static void v9fs_version(void *opaque)
1164 V9fsPDU *pdu = opaque;
1165 V9fsState *s = pdu->s;
1166 V9fsString version;
1167 size_t offset = 7;
1169 pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
1171 if (!strcmp(version.data, "9P2000.u")) {
1172 s->proto_version = V9FS_PROTO_2000U;
1173 } else if (!strcmp(version.data, "9P2000.L")) {
1174 s->proto_version = V9FS_PROTO_2000L;
1175 } else {
1176 v9fs_string_sprintf(&version, "unknown");
1179 offset += pdu_marshal(pdu, offset, "ds", s->msize, &version);
1180 complete_pdu(s, pdu, offset);
1182 v9fs_string_free(&version);
1183 return;
1186 static void v9fs_attach(void *opaque)
1188 V9fsPDU *pdu = opaque;
1189 V9fsState *s = pdu->s;
1190 int32_t fid, afid, n_uname;
1191 V9fsString uname, aname;
1192 V9fsFidState *fidp;
1193 size_t offset = 7;
1194 V9fsQID qid;
1195 ssize_t err;
1197 pdu_unmarshal(pdu, offset, "ddssd", &fid, &afid, &uname, &aname, &n_uname);
1199 fidp = alloc_fid(s, fid);
1200 if (fidp == NULL) {
1201 err = -EINVAL;
1202 goto out_nofid;
1204 fidp->uid = n_uname;
1205 v9fs_string_sprintf(&fidp->path, "%s", "/");
1206 err = fid_to_qid(s, fidp, &qid);
1207 if (err < 0) {
1208 err = -EINVAL;
1209 clunk_fid(s, fid);
1210 goto out;
1212 offset += pdu_marshal(pdu, offset, "Q", &qid);
1213 err = offset;
1214 out:
1215 put_fid(s, fidp);
1216 out_nofid:
1217 complete_pdu(s, pdu, err);
1218 v9fs_string_free(&uname);
1219 v9fs_string_free(&aname);
1222 static void v9fs_stat(void *opaque)
1224 int32_t fid;
1225 V9fsStat v9stat;
1226 ssize_t err = 0;
1227 size_t offset = 7;
1228 struct stat stbuf;
1229 V9fsFidState *fidp;
1230 V9fsPDU *pdu = opaque;
1231 V9fsState *s = pdu->s;
1233 pdu_unmarshal(pdu, offset, "d", &fid);
1235 fidp = get_fid(s, fid);
1236 if (fidp == NULL) {
1237 err = -ENOENT;
1238 goto out_nofid;
1240 err = v9fs_co_lstat(s, &fidp->path, &stbuf);
1241 if (err < 0) {
1242 goto out;
1244 err = stat_to_v9stat(s, &fidp->path, &stbuf, &v9stat);
1245 if (err < 0) {
1246 goto out;
1248 offset += pdu_marshal(pdu, offset, "wS", 0, &v9stat);
1249 err = offset;
1250 v9fs_stat_free(&v9stat);
1251 out:
1252 put_fid(s, fidp);
1253 out_nofid:
1254 complete_pdu(s, pdu, err);
1257 static void v9fs_getattr(void *opaque)
1259 int32_t fid;
1260 size_t offset = 7;
1261 ssize_t retval = 0;
1262 struct stat stbuf;
1263 V9fsFidState *fidp;
1264 uint64_t request_mask;
1265 V9fsStatDotl v9stat_dotl;
1266 V9fsPDU *pdu = opaque;
1267 V9fsState *s = pdu->s;
1269 pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask);
1271 fidp = get_fid(s, fid);
1272 if (fidp == NULL) {
1273 retval = -ENOENT;
1274 goto out_nofid;
1277 * Currently we only support BASIC fields in stat, so there is no
1278 * need to look at request_mask.
1280 retval = v9fs_co_lstat(s, &fidp->path, &stbuf);
1281 if (retval < 0) {
1282 goto out;
1284 stat_to_v9stat_dotl(s, &stbuf, &v9stat_dotl);
1285 retval = offset;
1286 retval += pdu_marshal(pdu, offset, "A", &v9stat_dotl);
1287 out:
1288 put_fid(s, fidp);
1289 out_nofid:
1290 complete_pdu(s, pdu, retval);
1293 /* From Linux kernel code */
1294 #define ATTR_MODE (1 << 0)
1295 #define ATTR_UID (1 << 1)
1296 #define ATTR_GID (1 << 2)
1297 #define ATTR_SIZE (1 << 3)
1298 #define ATTR_ATIME (1 << 4)
1299 #define ATTR_MTIME (1 << 5)
1300 #define ATTR_CTIME (1 << 6)
1301 #define ATTR_MASK 127
1302 #define ATTR_ATIME_SET (1 << 7)
1303 #define ATTR_MTIME_SET (1 << 8)
1305 static void v9fs_setattr(void *opaque)
1307 int err = 0;
1308 int32_t fid;
1309 V9fsFidState *fidp;
1310 size_t offset = 7;
1311 V9fsIattr v9iattr;
1312 V9fsPDU *pdu = opaque;
1313 V9fsState *s = pdu->s;
1315 pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr);
1317 fidp = get_fid(s, fid);
1318 if (fidp == NULL) {
1319 err = -EINVAL;
1320 goto out_nofid;
1322 if (v9iattr.valid & ATTR_MODE) {
1323 err = v9fs_co_chmod(s, &fidp->path, v9iattr.mode);
1324 if (err < 0) {
1325 goto out;
1328 if (v9iattr.valid & (ATTR_ATIME | ATTR_MTIME)) {
1329 struct timespec times[2];
1330 if (v9iattr.valid & ATTR_ATIME) {
1331 if (v9iattr.valid & ATTR_ATIME_SET) {
1332 times[0].tv_sec = v9iattr.atime_sec;
1333 times[0].tv_nsec = v9iattr.atime_nsec;
1334 } else {
1335 times[0].tv_nsec = UTIME_NOW;
1337 } else {
1338 times[0].tv_nsec = UTIME_OMIT;
1340 if (v9iattr.valid & ATTR_MTIME) {
1341 if (v9iattr.valid & ATTR_MTIME_SET) {
1342 times[1].tv_sec = v9iattr.mtime_sec;
1343 times[1].tv_nsec = v9iattr.mtime_nsec;
1344 } else {
1345 times[1].tv_nsec = UTIME_NOW;
1347 } else {
1348 times[1].tv_nsec = UTIME_OMIT;
1350 err = v9fs_co_utimensat(s, &fidp->path, times);
1351 if (err < 0) {
1352 goto out;
1356 * If the only valid entry in iattr is ctime we can call
1357 * chown(-1,-1) to update the ctime of the file
1359 if ((v9iattr.valid & (ATTR_UID | ATTR_GID)) ||
1360 ((v9iattr.valid & ATTR_CTIME)
1361 && !((v9iattr.valid & ATTR_MASK) & ~ATTR_CTIME))) {
1362 if (!(v9iattr.valid & ATTR_UID)) {
1363 v9iattr.uid = -1;
1365 if (!(v9iattr.valid & ATTR_GID)) {
1366 v9iattr.gid = -1;
1368 err = v9fs_co_chown(s, &fidp->path, v9iattr.uid,
1369 v9iattr.gid);
1370 if (err < 0) {
1371 goto out;
1374 if (v9iattr.valid & (ATTR_SIZE)) {
1375 err = v9fs_co_truncate(s, &fidp->path, v9iattr.size);
1376 if (err < 0) {
1377 goto out;
1380 err = offset;
1381 out:
1382 put_fid(s, fidp);
1383 out_nofid:
1384 complete_pdu(s, pdu, err);
1387 static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids)
1389 int i;
1390 size_t offset = 7;
1391 offset += pdu_marshal(pdu, offset, "w", nwnames);
1392 for (i = 0; i < nwnames; i++) {
1393 offset += pdu_marshal(pdu, offset, "Q", &qids[i]);
1395 return offset;
1398 static void v9fs_walk(void *opaque)
1400 int name_idx;
1401 V9fsQID *qids = NULL;
1402 int i, err = 0;
1403 V9fsString path;
1404 uint16_t nwnames;
1405 struct stat stbuf;
1406 size_t offset = 7;
1407 int32_t fid, newfid;
1408 V9fsString *wnames = NULL;
1409 V9fsFidState *fidp;
1410 V9fsFidState *newfidp = NULL;;
1411 V9fsPDU *pdu = opaque;
1412 V9fsState *s = pdu->s;
1414 offset += pdu_unmarshal(pdu, offset, "ddw", &fid,
1415 &newfid, &nwnames);
1417 if (nwnames && nwnames <= P9_MAXWELEM) {
1418 wnames = g_malloc0(sizeof(wnames[0]) * nwnames);
1419 qids = g_malloc0(sizeof(qids[0]) * nwnames);
1420 for (i = 0; i < nwnames; i++) {
1421 offset += pdu_unmarshal(pdu, offset, "s", &wnames[i]);
1424 } else if (nwnames > P9_MAXWELEM) {
1425 err = -EINVAL;
1426 goto out_nofid;
1428 fidp = get_fid(s, fid);
1429 if (fidp == NULL) {
1430 err = -ENOENT;
1431 goto out_nofid;
1433 if (fid == newfid) {
1434 BUG_ON(fidp->fid_type != P9_FID_NONE);
1435 v9fs_string_init(&path);
1436 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1437 v9fs_string_sprintf(&path, "%s/%s",
1438 fidp->path.data, wnames[name_idx].data);
1439 v9fs_string_copy(&fidp->path, &path);
1441 err = v9fs_co_lstat(s, &fidp->path, &stbuf);
1442 if (err < 0) {
1443 v9fs_string_free(&path);
1444 goto out;
1446 stat_to_qid(&stbuf, &qids[name_idx]);
1448 v9fs_string_free(&path);
1449 } else {
1450 newfidp = alloc_fid(s, newfid);
1451 if (newfidp == NULL) {
1452 err = -EINVAL;
1453 goto out;
1455 newfidp->uid = fidp->uid;
1456 v9fs_string_init(&path);
1457 v9fs_string_copy(&newfidp->path, &fidp->path);
1458 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1459 v9fs_string_sprintf(&path, "%s/%s", newfidp->path.data,
1460 wnames[name_idx].data);
1461 v9fs_string_copy(&newfidp->path, &path);
1462 err = v9fs_co_lstat(s, &newfidp->path, &stbuf);
1463 if (err < 0) {
1464 clunk_fid(s, newfidp->fid);
1465 v9fs_string_free(&path);
1466 goto out;
1468 stat_to_qid(&stbuf, &qids[name_idx]);
1470 v9fs_string_free(&path);
1472 err = v9fs_walk_marshal(pdu, nwnames, qids);
1473 out:
1474 put_fid(s, fidp);
1475 if (newfidp) {
1476 put_fid(s, newfidp);
1478 out_nofid:
1479 complete_pdu(s, pdu, err);
1480 if (nwnames && nwnames <= P9_MAXWELEM) {
1481 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1482 v9fs_string_free(&wnames[name_idx]);
1484 g_free(wnames);
1485 g_free(qids);
1489 static int32_t get_iounit(V9fsState *s, V9fsString *name)
1491 struct statfs stbuf;
1492 int32_t iounit = 0;
1495 * iounit should be multiples of f_bsize (host filesystem block size
1496 * and as well as less than (client msize - P9_IOHDRSZ))
1498 if (!v9fs_co_statfs(s, name, &stbuf)) {
1499 iounit = stbuf.f_bsize;
1500 iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
1502 if (!iounit) {
1503 iounit = s->msize - P9_IOHDRSZ;
1505 return iounit;
1508 static void v9fs_open(void *opaque)
1510 int flags;
1511 int iounit;
1512 int32_t fid;
1513 int32_t mode;
1514 V9fsQID qid;
1515 ssize_t err = 0;
1516 size_t offset = 7;
1517 struct stat stbuf;
1518 V9fsFidState *fidp;
1519 V9fsPDU *pdu = opaque;
1520 V9fsState *s = pdu->s;
1522 if (s->proto_version == V9FS_PROTO_2000L) {
1523 pdu_unmarshal(pdu, offset, "dd", &fid, &mode);
1524 } else {
1525 pdu_unmarshal(pdu, offset, "db", &fid, &mode);
1527 fidp = get_fid(s, fid);
1528 if (fidp == NULL) {
1529 err = -ENOENT;
1530 goto out_nofid;
1532 BUG_ON(fidp->fid_type != P9_FID_NONE);
1534 err = v9fs_co_lstat(s, &fidp->path, &stbuf);
1535 if (err < 0) {
1536 goto out;
1538 stat_to_qid(&stbuf, &qid);
1539 if (S_ISDIR(stbuf.st_mode)) {
1540 err = v9fs_co_opendir(s, fidp);
1541 if (err < 0) {
1542 goto out;
1544 fidp->fid_type = P9_FID_DIR;
1545 offset += pdu_marshal(pdu, offset, "Qd", &qid, 0);
1546 err = offset;
1547 } else {
1548 if (s->proto_version == V9FS_PROTO_2000L) {
1549 flags = mode;
1550 flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT);
1551 /* Ignore direct disk access hint until the server supports it. */
1552 flags &= ~O_DIRECT;
1553 } else {
1554 flags = omode_to_uflags(mode);
1556 err = v9fs_co_open(s, fidp, flags);
1557 if (err < 0) {
1558 goto out;
1560 fidp->fid_type = P9_FID_FILE;
1561 fidp->open_flags = flags;
1562 if (flags & O_EXCL) {
1564 * We let the host file system do O_EXCL check
1565 * We should not reclaim such fd
1567 fidp->flags |= FID_NON_RECLAIMABLE;
1569 iounit = get_iounit(s, &fidp->path);
1570 offset += pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1571 err = offset;
1573 out:
1574 put_fid(s, fidp);
1575 out_nofid:
1576 complete_pdu(s, pdu, err);
1579 static void v9fs_lcreate(void *opaque)
1581 int32_t dfid, flags, mode;
1582 gid_t gid;
1583 ssize_t err = 0;
1584 ssize_t offset = 7;
1585 V9fsString name;
1586 V9fsFidState *fidp;
1587 struct stat stbuf;
1588 V9fsQID qid;
1589 int32_t iounit;
1590 V9fsPDU *pdu = opaque;
1592 pdu_unmarshal(pdu, offset, "dsddd", &dfid, &name, &flags,
1593 &mode, &gid);
1595 fidp = get_fid(pdu->s, dfid);
1596 if (fidp == NULL) {
1597 err = -ENOENT;
1598 goto out_nofid;
1601 /* Ignore direct disk access hint until the server supports it. */
1602 flags &= ~O_DIRECT;
1604 err = v9fs_co_open2(pdu->s, fidp, &name, gid,
1605 flags | O_CREAT, mode, &stbuf);
1606 if (err < 0) {
1607 goto out;
1609 fidp->fid_type = P9_FID_FILE;
1610 fidp->open_flags = flags;
1611 if (flags & O_EXCL) {
1613 * We let the host file system do O_EXCL check
1614 * We should not reclaim such fd
1616 fidp->flags |= FID_NON_RECLAIMABLE;
1618 iounit = get_iounit(pdu->s, &fidp->path);
1619 stat_to_qid(&stbuf, &qid);
1620 offset += pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1621 err = offset;
1622 out:
1623 put_fid(pdu->s, fidp);
1624 out_nofid:
1625 complete_pdu(pdu->s, pdu, err);
1626 v9fs_string_free(&name);
1629 static void v9fs_fsync(void *opaque)
1631 int err;
1632 int32_t fid;
1633 int datasync;
1634 size_t offset = 7;
1635 V9fsFidState *fidp;
1636 V9fsPDU *pdu = opaque;
1637 V9fsState *s = pdu->s;
1639 pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
1640 fidp = get_fid(s, fid);
1641 if (fidp == NULL) {
1642 err = -ENOENT;
1643 goto out_nofid;
1645 err = v9fs_co_fsync(s, fidp, datasync);
1646 if (!err) {
1647 err = offset;
1649 put_fid(s, fidp);
1650 out_nofid:
1651 complete_pdu(s, pdu, err);
1654 static void v9fs_clunk(void *opaque)
1656 int err;
1657 int32_t fid;
1658 size_t offset = 7;
1659 V9fsFidState *fidp;
1660 V9fsPDU *pdu = opaque;
1661 V9fsState *s = pdu->s;
1663 pdu_unmarshal(pdu, offset, "d", &fid);
1665 fidp = get_fid(s, fid);
1666 if (fidp == NULL) {
1667 err = -ENOENT;
1668 goto out_nofid;
1670 err = clunk_fid(s, fidp->fid);
1671 if (err < 0) {
1672 goto out;
1674 err = offset;
1675 out:
1676 put_fid(s, fidp);
1677 out_nofid:
1678 complete_pdu(s, pdu, err);
1681 static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu,
1682 V9fsFidState *fidp, int64_t off, int32_t max_count)
1684 size_t offset = 7;
1685 int read_count;
1686 int64_t xattr_len;
1688 xattr_len = fidp->fs.xattr.len;
1689 read_count = xattr_len - off;
1690 if (read_count > max_count) {
1691 read_count = max_count;
1692 } else if (read_count < 0) {
1694 * read beyond XATTR value
1696 read_count = 0;
1698 offset += pdu_marshal(pdu, offset, "d", read_count);
1699 offset += pdu_pack(pdu, offset,
1700 ((char *)fidp->fs.xattr.value) + off,
1701 read_count);
1702 return offset;
1705 static int v9fs_do_readdir_with_stat(V9fsState *s, V9fsPDU *pdu,
1706 V9fsFidState *fidp, int32_t max_count)
1708 V9fsString name;
1709 V9fsStat v9stat;
1710 int len, err = 0;
1711 int32_t count = 0;
1712 struct stat stbuf;
1713 off_t saved_dir_pos;
1714 struct dirent *dent, *result;
1716 /* save the directory position */
1717 saved_dir_pos = v9fs_co_telldir(s, fidp);
1718 if (saved_dir_pos < 0) {
1719 return saved_dir_pos;
1722 dent = g_malloc(sizeof(struct dirent));
1724 while (1) {
1725 v9fs_string_init(&name);
1726 err = v9fs_co_readdir_r(s, fidp, dent, &result);
1727 if (err || !result) {
1728 break;
1730 v9fs_string_sprintf(&name, "%s/%s", fidp->path.data, dent->d_name);
1731 err = v9fs_co_lstat(s, &name, &stbuf);
1732 if (err < 0) {
1733 goto out;
1735 err = stat_to_v9stat(s, &name, &stbuf, &v9stat);
1736 if (err < 0) {
1737 goto out;
1739 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1740 len = pdu_marshal(pdu, 11 + count, "S", &v9stat);
1741 if ((len != (v9stat.size + 2)) || ((count + len) > max_count)) {
1742 /* Ran out of buffer. Set dir back to old position and return */
1743 v9fs_co_seekdir(s, fidp, saved_dir_pos);
1744 v9fs_stat_free(&v9stat);
1745 v9fs_string_free(&name);
1746 g_free(dent);
1747 return count;
1749 count += len;
1750 v9fs_stat_free(&v9stat);
1751 v9fs_string_free(&name);
1752 saved_dir_pos = dent->d_off;
1754 out:
1755 g_free(dent);
1756 v9fs_string_free(&name);
1757 if (err < 0) {
1758 return err;
1760 return count;
1763 static void v9fs_read(void *opaque)
1765 int32_t fid;
1766 int64_t off;
1767 ssize_t err = 0;
1768 int32_t count = 0;
1769 size_t offset = 7;
1770 int32_t max_count;
1771 V9fsFidState *fidp;
1772 V9fsPDU *pdu = opaque;
1773 V9fsState *s = pdu->s;
1775 pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count);
1777 fidp = get_fid(s, fid);
1778 if (fidp == NULL) {
1779 err = -EINVAL;
1780 goto out_nofid;
1782 if (fidp->fid_type == P9_FID_DIR) {
1784 if (off == 0) {
1785 v9fs_co_rewinddir(s, fidp);
1787 count = v9fs_do_readdir_with_stat(s, pdu, fidp, max_count);
1788 if (count < 0) {
1789 err = count;
1790 goto out;
1792 err = offset;
1793 err += pdu_marshal(pdu, offset, "d", count);
1794 err += count;
1795 } else if (fidp->fid_type == P9_FID_FILE) {
1796 int32_t cnt;
1797 int32_t len;
1798 struct iovec *sg;
1799 struct iovec iov[128]; /* FIXME: bad, bad, bad */
1801 sg = iov;
1802 pdu_marshal(pdu, offset + 4, "v", sg, &cnt);
1803 sg = cap_sg(sg, max_count, &cnt);
1804 do {
1805 if (0) {
1806 print_sg(sg, cnt);
1808 /* Loop in case of EINTR */
1809 do {
1810 len = v9fs_co_preadv(s, fidp, sg, cnt, off);
1811 if (len >= 0) {
1812 off += len;
1813 count += len;
1815 } while (len == -EINTR);
1816 if (len < 0) {
1817 /* IO error return the error */
1818 err = len;
1819 goto out;
1821 sg = adjust_sg(sg, len, &cnt);
1822 } while (count < max_count && len > 0);
1823 err = offset;
1824 err += pdu_marshal(pdu, offset, "d", count);
1825 err += count;
1826 } else if (fidp->fid_type == P9_FID_XATTR) {
1827 err = v9fs_xattr_read(s, pdu, fidp, off, max_count);
1828 } else {
1829 err = -EINVAL;
1831 out:
1832 put_fid(s, fidp);
1833 out_nofid:
1834 complete_pdu(s, pdu, err);
1837 static size_t v9fs_readdir_data_size(V9fsString *name)
1840 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1841 * size of type (1) + size of name.size (2) + strlen(name.data)
1843 return 24 + v9fs_string_size(name);
1846 static int v9fs_do_readdir(V9fsState *s, V9fsPDU *pdu,
1847 V9fsFidState *fidp, int32_t max_count)
1849 size_t size;
1850 V9fsQID qid;
1851 V9fsString name;
1852 int len, err = 0;
1853 int32_t count = 0;
1854 off_t saved_dir_pos;
1855 struct dirent *dent, *result;
1857 /* save the directory position */
1858 saved_dir_pos = v9fs_co_telldir(s, fidp);
1859 if (saved_dir_pos < 0) {
1860 return saved_dir_pos;
1863 dent = g_malloc(sizeof(struct dirent));
1865 while (1) {
1866 err = v9fs_co_readdir_r(s, fidp, dent, &result);
1867 if (err || !result) {
1868 break;
1870 v9fs_string_init(&name);
1871 v9fs_string_sprintf(&name, "%s", dent->d_name);
1872 if ((count + v9fs_readdir_data_size(&name)) > max_count) {
1873 /* Ran out of buffer. Set dir back to old position and return */
1874 v9fs_co_seekdir(s, fidp, saved_dir_pos);
1875 v9fs_string_free(&name);
1876 g_free(dent);
1877 return count;
1880 * Fill up just the path field of qid because the client uses
1881 * only that. To fill the entire qid structure we will have
1882 * to stat each dirent found, which is expensive
1884 size = MIN(sizeof(dent->d_ino), sizeof(qid.path));
1885 memcpy(&qid.path, &dent->d_ino, size);
1886 /* Fill the other fields with dummy values */
1887 qid.type = 0;
1888 qid.version = 0;
1890 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1891 len = pdu_marshal(pdu, 11 + count, "Qqbs",
1892 &qid, dent->d_off,
1893 dent->d_type, &name);
1894 count += len;
1895 v9fs_string_free(&name);
1896 saved_dir_pos = dent->d_off;
1898 g_free(dent);
1899 if (err < 0) {
1900 return err;
1902 return count;
1905 static void v9fs_readdir(void *opaque)
1907 int32_t fid;
1908 V9fsFidState *fidp;
1909 ssize_t retval = 0;
1910 size_t offset = 7;
1911 int64_t initial_offset;
1912 int32_t count, max_count;
1913 V9fsPDU *pdu = opaque;
1914 V9fsState *s = pdu->s;
1916 pdu_unmarshal(pdu, offset, "dqd", &fid, &initial_offset, &max_count);
1918 fidp = get_fid(s, fid);
1919 if (fidp == NULL) {
1920 retval = -EINVAL;
1921 goto out_nofid;
1923 if (!fidp->fs.dir) {
1924 retval = -EINVAL;
1925 goto out;
1927 if (initial_offset == 0) {
1928 v9fs_co_rewinddir(s, fidp);
1929 } else {
1930 v9fs_co_seekdir(s, fidp, initial_offset);
1932 count = v9fs_do_readdir(s, pdu, fidp, max_count);
1933 if (count < 0) {
1934 retval = count;
1935 goto out;
1937 retval = offset;
1938 retval += pdu_marshal(pdu, offset, "d", count);
1939 retval += count;
1940 out:
1941 put_fid(s, fidp);
1942 out_nofid:
1943 complete_pdu(s, pdu, retval);
1946 static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
1947 int64_t off, int32_t count,
1948 struct iovec *sg, int cnt)
1950 int i, to_copy;
1951 ssize_t err = 0;
1952 int write_count;
1953 int64_t xattr_len;
1954 size_t offset = 7;
1957 xattr_len = fidp->fs.xattr.len;
1958 write_count = xattr_len - off;
1959 if (write_count > count) {
1960 write_count = count;
1961 } else if (write_count < 0) {
1963 * write beyond XATTR value len specified in
1964 * xattrcreate
1966 err = -ENOSPC;
1967 goto out;
1969 offset += pdu_marshal(pdu, offset, "d", write_count);
1970 err = offset;
1971 fidp->fs.xattr.copied_len += write_count;
1973 * Now copy the content from sg list
1975 for (i = 0; i < cnt; i++) {
1976 if (write_count > sg[i].iov_len) {
1977 to_copy = sg[i].iov_len;
1978 } else {
1979 to_copy = write_count;
1981 memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy);
1982 /* updating vs->off since we are not using below */
1983 off += to_copy;
1984 write_count -= to_copy;
1986 out:
1987 return err;
1990 static void v9fs_write(void *opaque)
1992 int cnt;
1993 ssize_t err;
1994 int32_t fid;
1995 int64_t off;
1996 int32_t count;
1997 int32_t len = 0;
1998 int32_t total = 0;
1999 size_t offset = 7;
2000 V9fsFidState *fidp;
2001 struct iovec iov[128]; /* FIXME: bad, bad, bad */
2002 struct iovec *sg = iov;
2003 V9fsPDU *pdu = opaque;
2004 V9fsState *s = pdu->s;
2006 pdu_unmarshal(pdu, offset, "dqdv", &fid, &off, &count, sg, &cnt);
2008 fidp = get_fid(s, fid);
2009 if (fidp == NULL) {
2010 err = -EINVAL;
2011 goto out_nofid;
2013 if (fidp->fid_type == P9_FID_FILE) {
2014 if (fidp->fs.fd == -1) {
2015 err = -EINVAL;
2016 goto out;
2018 } else if (fidp->fid_type == P9_FID_XATTR) {
2020 * setxattr operation
2022 err = v9fs_xattr_write(s, pdu, fidp, off, count, sg, cnt);
2023 goto out;
2024 } else {
2025 err = -EINVAL;
2026 goto out;
2028 sg = cap_sg(sg, count, &cnt);
2029 do {
2030 if (0) {
2031 print_sg(sg, cnt);
2033 /* Loop in case of EINTR */
2034 do {
2035 len = v9fs_co_pwritev(s, fidp, sg, cnt, off);
2036 if (len >= 0) {
2037 off += len;
2038 total += len;
2040 } while (len == -EINTR);
2041 if (len < 0) {
2042 /* IO error return the error */
2043 err = len;
2044 goto out;
2046 sg = adjust_sg(sg, len, &cnt);
2047 } while (total < count && len > 0);
2048 offset += pdu_marshal(pdu, offset, "d", total);
2049 err = offset;
2050 out:
2051 put_fid(s, fidp);
2052 out_nofid:
2053 complete_pdu(s, pdu, err);
2056 static void v9fs_create(void *opaque)
2058 int32_t fid;
2059 int err = 0;
2060 size_t offset = 7;
2061 V9fsFidState *fidp;
2062 V9fsQID qid;
2063 int32_t perm;
2064 int8_t mode;
2065 struct stat stbuf;
2066 V9fsString name;
2067 V9fsString extension;
2068 V9fsString fullname;
2069 int iounit;
2070 V9fsPDU *pdu = opaque;
2072 v9fs_string_init(&fullname);
2074 pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name,
2075 &perm, &mode, &extension);
2077 fidp = get_fid(pdu->s, fid);
2078 if (fidp == NULL) {
2079 err = -EINVAL;
2080 goto out_nofid;
2083 v9fs_string_sprintf(&fullname, "%s/%s", fidp->path.data, name.data);
2084 err = v9fs_co_lstat(pdu->s, &fullname, &stbuf);
2085 if (!err) {
2086 err = -EEXIST;
2087 goto out;
2088 } else if (err != -ENOENT) {
2089 goto out;
2091 if (perm & P9_STAT_MODE_DIR) {
2092 err = v9fs_co_mkdir(pdu->s, fidp, &name, perm & 0777,
2093 fidp->uid, -1, &stbuf);
2094 if (err < 0) {
2095 goto out;
2097 v9fs_string_sprintf(&fullname, "%s/%s", fidp->path.data, name.data);
2098 v9fs_string_copy(&fidp->path, &fullname);
2099 err = v9fs_co_opendir(pdu->s, fidp);
2100 if (err < 0) {
2101 goto out;
2103 fidp->fid_type = P9_FID_DIR;
2104 } else if (perm & P9_STAT_MODE_SYMLINK) {
2105 err = v9fs_co_symlink(pdu->s, fidp, &name,
2106 extension.data, -1 , &stbuf);
2107 if (err < 0) {
2108 goto out;
2110 v9fs_string_sprintf(&fullname, "%s/%s", fidp->path.data, name.data);
2111 v9fs_string_copy(&fidp->path, &fullname);
2112 } else if (perm & P9_STAT_MODE_LINK) {
2113 int32_t nfid = atoi(extension.data);
2114 V9fsFidState *nfidp = get_fid(pdu->s, nfid);
2115 if (nfidp == NULL) {
2116 err = -EINVAL;
2117 goto out;
2119 v9fs_string_sprintf(&fullname, "%s/%s", fidp->path.data, name.data);
2120 err = v9fs_co_link(pdu->s, &nfidp->path, &fullname);
2121 put_fid(pdu->s, nfidp);
2122 if (err < 0) {
2123 goto out;
2125 v9fs_string_sprintf(&fullname, "%s/%s", fidp->path.data, name.data);
2126 v9fs_string_copy(&fidp->path, &fullname);
2127 err = v9fs_co_lstat(pdu->s, &fidp->path, &stbuf);
2128 if (err < 0) {
2129 fidp->fid_type = P9_FID_NONE;
2130 goto out;
2132 } else if (perm & P9_STAT_MODE_DEVICE) {
2133 char ctype;
2134 uint32_t major, minor;
2135 mode_t nmode = 0;
2137 if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) {
2138 err = -errno;
2139 goto out;
2142 switch (ctype) {
2143 case 'c':
2144 nmode = S_IFCHR;
2145 break;
2146 case 'b':
2147 nmode = S_IFBLK;
2148 break;
2149 default:
2150 err = -EIO;
2151 goto out;
2154 nmode |= perm & 0777;
2155 err = v9fs_co_mknod(pdu->s, fidp, &name, fidp->uid, -1,
2156 makedev(major, minor), nmode, &stbuf);
2157 if (err < 0) {
2158 goto out;
2160 v9fs_string_sprintf(&fullname, "%s/%s", fidp->path.data, name.data);
2161 v9fs_string_copy(&fidp->path, &fullname);
2162 } else if (perm & P9_STAT_MODE_NAMED_PIPE) {
2163 err = v9fs_co_mknod(pdu->s, fidp, &name, fidp->uid, -1,
2164 0, S_IFIFO | (perm & 0777), &stbuf);
2165 if (err < 0) {
2166 goto out;
2168 v9fs_string_sprintf(&fullname, "%s/%s", fidp->path.data, name.data);
2169 v9fs_string_copy(&fidp->path, &fullname);
2170 } else if (perm & P9_STAT_MODE_SOCKET) {
2171 err = v9fs_co_mknod(pdu->s, fidp, &name, fidp->uid, -1,
2172 0, S_IFSOCK | (perm & 0777), &stbuf);
2173 if (err < 0) {
2174 goto out;
2176 v9fs_string_sprintf(&fullname, "%s/%s", fidp->path.data, name.data);
2177 v9fs_string_copy(&fidp->path, &fullname);
2178 } else {
2179 err = v9fs_co_open2(pdu->s, fidp, &name, -1,
2180 omode_to_uflags(mode)|O_CREAT, perm, &stbuf);
2181 if (err < 0) {
2182 goto out;
2184 fidp->fid_type = P9_FID_FILE;
2185 fidp->open_flags = omode_to_uflags(mode);
2186 if (fidp->open_flags & O_EXCL) {
2188 * We let the host file system do O_EXCL check
2189 * We should not reclaim such fd
2191 fidp->flags |= FID_NON_RECLAIMABLE;
2194 iounit = get_iounit(pdu->s, &fidp->path);
2195 stat_to_qid(&stbuf, &qid);
2196 offset += pdu_marshal(pdu, offset, "Qd", &qid, iounit);
2197 err = offset;
2198 out:
2199 put_fid(pdu->s, fidp);
2200 out_nofid:
2201 complete_pdu(pdu->s, pdu, err);
2202 v9fs_string_free(&name);
2203 v9fs_string_free(&extension);
2204 v9fs_string_free(&fullname);
2207 static void v9fs_symlink(void *opaque)
2209 V9fsPDU *pdu = opaque;
2210 V9fsString name;
2211 V9fsString symname;
2212 V9fsFidState *dfidp;
2213 V9fsQID qid;
2214 struct stat stbuf;
2215 int32_t dfid;
2216 int err = 0;
2217 gid_t gid;
2218 size_t offset = 7;
2220 pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid);
2222 dfidp = get_fid(pdu->s, dfid);
2223 if (dfidp == NULL) {
2224 err = -EINVAL;
2225 goto out_nofid;
2227 err = v9fs_co_symlink(pdu->s, dfidp, &name, symname.data, gid, &stbuf);
2228 if (err < 0) {
2229 goto out;
2231 stat_to_qid(&stbuf, &qid);
2232 offset += pdu_marshal(pdu, offset, "Q", &qid);
2233 err = offset;
2234 out:
2235 put_fid(pdu->s, dfidp);
2236 out_nofid:
2237 complete_pdu(pdu->s, pdu, err);
2238 v9fs_string_free(&name);
2239 v9fs_string_free(&symname);
2242 static void v9fs_flush(void *opaque)
2244 V9fsPDU *pdu = opaque;
2245 V9fsState *s = pdu->s;
2246 /* A nop call with no return */
2247 complete_pdu(s, pdu, 7);
2248 return;
2251 static void v9fs_link(void *opaque)
2253 V9fsPDU *pdu = opaque;
2254 V9fsState *s = pdu->s;
2255 int32_t dfid, oldfid;
2256 V9fsFidState *dfidp, *oldfidp;
2257 V9fsString name, fullname;
2258 size_t offset = 7;
2259 int err = 0;
2261 v9fs_string_init(&fullname);
2263 pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
2265 dfidp = get_fid(s, dfid);
2266 if (dfidp == NULL) {
2267 err = -ENOENT;
2268 goto out_nofid;
2271 oldfidp = get_fid(s, oldfid);
2272 if (oldfidp == NULL) {
2273 err = -ENOENT;
2274 goto out;
2277 v9fs_string_sprintf(&fullname, "%s/%s", dfidp->path.data, name.data);
2278 err = v9fs_co_link(s, &oldfidp->path, &fullname);
2279 if (!err) {
2280 err = offset;
2282 v9fs_string_free(&fullname);
2284 out:
2285 put_fid(s, dfidp);
2286 out_nofid:
2287 v9fs_string_free(&name);
2288 complete_pdu(s, pdu, err);
2291 static void v9fs_remove(void *opaque)
2293 int32_t fid;
2294 int err = 0;
2295 size_t offset = 7;
2296 V9fsFidState *fidp;
2297 V9fsPDU *pdu = opaque;
2299 pdu_unmarshal(pdu, offset, "d", &fid);
2301 fidp = get_fid(pdu->s, fid);
2302 if (fidp == NULL) {
2303 err = -EINVAL;
2304 goto out_nofid;
2307 * IF the file is unlinked, we cannot reopen
2308 * the file later. So don't reclaim fd
2310 err = v9fs_mark_fids_unreclaim(pdu->s, &fidp->path);
2311 if (err < 0) {
2312 goto out_err;
2314 err = v9fs_co_remove(pdu->s, &fidp->path);
2315 if (!err) {
2316 err = offset;
2318 out_err:
2319 /* For TREMOVE we need to clunk the fid even on failed remove */
2320 clunk_fid(pdu->s, fidp->fid);
2321 put_fid(pdu->s, fidp);
2322 out_nofid:
2323 complete_pdu(pdu->s, pdu, err);
2326 static void v9fs_unlinkat(void *opaque)
2328 int err = 0;
2329 V9fsString name;
2330 int32_t dfid, flags;
2331 size_t offset = 7;
2332 V9fsFidState *dfidp;
2333 V9fsPDU *pdu = opaque;
2334 V9fsString full_name;
2336 pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags);
2338 dfidp = get_fid(pdu->s, dfid);
2339 if (dfidp == NULL) {
2340 err = -EINVAL;
2341 goto out_nofid;
2343 v9fs_string_init(&full_name);
2344 v9fs_string_sprintf(&full_name, "%s/%s", dfidp->path.data, name.data);
2346 * IF the file is unlinked, we cannot reopen
2347 * the file later. So don't reclaim fd
2349 err = v9fs_mark_fids_unreclaim(pdu->s, &full_name);
2350 if (err < 0) {
2351 goto out_err;
2353 err = v9fs_co_remove(pdu->s, &full_name);
2354 if (!err) {
2355 err = offset;
2357 out_err:
2358 put_fid(pdu->s, dfidp);
2359 v9fs_string_free(&full_name);
2360 out_nofid:
2361 complete_pdu(pdu->s, pdu, err);
2362 v9fs_string_free(&name);
2365 static int v9fs_complete_rename(V9fsState *s, V9fsFidState *fidp,
2366 int32_t newdirfid, V9fsString *name)
2368 char *end;
2369 int err = 0;
2370 V9fsFidState *dirfidp = NULL;
2371 char *old_name, *new_name;
2373 if (newdirfid != -1) {
2374 dirfidp = get_fid(s, newdirfid);
2375 if (dirfidp == NULL) {
2376 err = -ENOENT;
2377 goto out_nofid;
2379 BUG_ON(dirfidp->fid_type != P9_FID_NONE);
2381 new_name = g_malloc0(dirfidp->path.size + name->size + 2);
2383 strcpy(new_name, dirfidp->path.data);
2384 strcat(new_name, "/");
2385 strcat(new_name + dirfidp->path.size, name->data);
2386 } else {
2387 old_name = fidp->path.data;
2388 end = strrchr(old_name, '/');
2389 if (end) {
2390 end++;
2391 } else {
2392 end = old_name;
2394 new_name = g_malloc0(end - old_name + name->size + 1);
2396 strncat(new_name, old_name, end - old_name);
2397 strncat(new_name + (end - old_name), name->data, name->size);
2400 v9fs_string_free(name);
2401 name->data = new_name;
2402 name->size = strlen(new_name);
2404 if (strcmp(new_name, fidp->path.data) != 0) {
2405 err = v9fs_co_rename(s, &fidp->path, name);
2406 if (err < 0) {
2407 goto out;
2409 V9fsFidState *tfidp;
2411 * Fixup fid's pointing to the old name to
2412 * start pointing to the new name
2414 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2415 if (fidp == tfidp) {
2417 * we replace name of this fid towards the end
2418 * so that our below strcmp will work
2420 continue;
2422 if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) {
2423 /* replace the name */
2424 v9fs_fix_path(&tfidp->path, name, strlen(fidp->path.data));
2427 v9fs_string_copy(&fidp->path, name);
2429 out:
2430 if (dirfidp) {
2431 put_fid(s, dirfidp);
2433 out_nofid:
2434 return err;
2437 static void v9fs_rename(void *opaque)
2439 int32_t fid;
2440 ssize_t err = 0;
2441 size_t offset = 7;
2442 V9fsString name;
2443 int32_t newdirfid;
2444 V9fsFidState *fidp;
2445 V9fsPDU *pdu = opaque;
2446 V9fsState *s = pdu->s;
2448 pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name);
2450 fidp = get_fid(s, fid);
2451 if (fidp == NULL) {
2452 err = -ENOENT;
2453 goto out_nofid;
2455 BUG_ON(fidp->fid_type != P9_FID_NONE);
2457 qemu_co_rwlock_wrlock(&s->rename_lock);
2458 err = v9fs_complete_rename(s, fidp, newdirfid, &name);
2459 qemu_co_rwlock_unlock(&s->rename_lock);
2460 if (!err) {
2461 err = offset;
2463 put_fid(s, fidp);
2464 out_nofid:
2465 complete_pdu(s, pdu, err);
2466 v9fs_string_free(&name);
2469 static int v9fs_complete_renameat(V9fsState *s, int32_t olddirfid,
2470 V9fsString *old_name, int32_t newdirfid,
2471 V9fsString *new_name)
2473 int err = 0;
2474 V9fsString old_full_name, new_full_name;
2475 V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL;
2477 olddirfidp = get_fid(s, olddirfid);
2478 if (olddirfidp == NULL) {
2479 err = -ENOENT;
2480 goto out;
2482 v9fs_string_init(&old_full_name);
2483 v9fs_string_init(&new_full_name);
2485 v9fs_string_sprintf(&old_full_name, "%s/%s",
2486 olddirfidp->path.data, old_name->data);
2487 if (newdirfid != -1) {
2488 newdirfidp = get_fid(s, newdirfid);
2489 if (newdirfidp == NULL) {
2490 err = -ENOENT;
2491 goto out;
2493 v9fs_string_sprintf(&new_full_name, "%s/%s",
2494 newdirfidp->path.data, new_name->data);
2495 } else {
2496 v9fs_string_sprintf(&new_full_name, "%s/%s",
2497 olddirfidp->path.data, new_name->data);
2500 if (strcmp(old_full_name.data, new_full_name.data) != 0) {
2501 V9fsFidState *tfidp;
2502 err = v9fs_co_rename(s, &old_full_name, &new_full_name);
2503 if (err < 0) {
2504 goto out;
2507 * Fixup fid's pointing to the old name to
2508 * start pointing to the new name
2510 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2511 if (v9fs_path_is_ancestor(&old_full_name, &tfidp->path)) {
2512 /* replace the name */
2513 v9fs_fix_path(&tfidp->path, &new_full_name, old_full_name.size);
2517 out:
2518 if (olddirfidp) {
2519 put_fid(s, olddirfidp);
2521 if (newdirfidp) {
2522 put_fid(s, newdirfidp);
2524 v9fs_string_free(&old_full_name);
2525 v9fs_string_free(&new_full_name);
2526 return err;
2529 static void v9fs_renameat(void *opaque)
2531 ssize_t err = 0;
2532 size_t offset = 7;
2533 V9fsPDU *pdu = opaque;
2534 V9fsState *s = pdu->s;
2535 int32_t olddirfid, newdirfid;
2536 V9fsString old_name, new_name;
2538 pdu_unmarshal(pdu, offset, "dsds", &olddirfid,
2539 &old_name, &newdirfid, &new_name);
2541 qemu_co_rwlock_wrlock(&s->rename_lock);
2542 err = v9fs_complete_renameat(s, olddirfid, &old_name, newdirfid, &new_name);
2543 qemu_co_rwlock_unlock(&s->rename_lock);
2544 if (!err) {
2545 err = offset;
2547 complete_pdu(s, pdu, err);
2548 v9fs_string_free(&old_name);
2549 v9fs_string_free(&new_name);
2552 static void v9fs_wstat(void *opaque)
2554 int32_t fid;
2555 int err = 0;
2556 int16_t unused;
2557 V9fsStat v9stat;
2558 size_t offset = 7;
2559 struct stat stbuf;
2560 V9fsFidState *fidp;
2561 V9fsPDU *pdu = opaque;
2562 V9fsState *s = pdu->s;
2564 pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat);
2566 fidp = get_fid(s, fid);
2567 if (fidp == NULL) {
2568 err = -EINVAL;
2569 goto out_nofid;
2571 /* do we need to sync the file? */
2572 if (donttouch_stat(&v9stat)) {
2573 err = v9fs_co_fsync(s, fidp, 0);
2574 goto out;
2576 if (v9stat.mode != -1) {
2577 uint32_t v9_mode;
2578 err = v9fs_co_lstat(s, &fidp->path, &stbuf);
2579 if (err < 0) {
2580 goto out;
2582 v9_mode = stat_to_v9mode(&stbuf);
2583 if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2584 (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2585 /* Attempting to change the type */
2586 err = -EIO;
2587 goto out;
2589 err = v9fs_co_chmod(s, &fidp->path,
2590 v9mode_to_mode(v9stat.mode,
2591 &v9stat.extension));
2592 if (err < 0) {
2593 goto out;
2596 if (v9stat.mtime != -1 || v9stat.atime != -1) {
2597 struct timespec times[2];
2598 if (v9stat.atime != -1) {
2599 times[0].tv_sec = v9stat.atime;
2600 times[0].tv_nsec = 0;
2601 } else {
2602 times[0].tv_nsec = UTIME_OMIT;
2604 if (v9stat.mtime != -1) {
2605 times[1].tv_sec = v9stat.mtime;
2606 times[1].tv_nsec = 0;
2607 } else {
2608 times[1].tv_nsec = UTIME_OMIT;
2610 err = v9fs_co_utimensat(s, &fidp->path, times);
2611 if (err < 0) {
2612 goto out;
2615 if (v9stat.n_gid != -1 || v9stat.n_uid != -1) {
2616 err = v9fs_co_chown(s, &fidp->path, v9stat.n_uid, v9stat.n_gid);
2617 if (err < 0) {
2618 goto out;
2621 if (v9stat.name.size != 0) {
2622 err = v9fs_complete_rename(s, fidp, -1, &v9stat.name);
2623 if (err < 0) {
2624 goto out;
2627 if (v9stat.length != -1) {
2628 err = v9fs_co_truncate(s, &fidp->path, v9stat.length);
2629 if (err < 0) {
2630 goto out;
2633 err = offset;
2634 out:
2635 put_fid(s, fidp);
2636 out_nofid:
2637 v9fs_stat_free(&v9stat);
2638 complete_pdu(s, pdu, err);
2641 static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf)
2643 uint32_t f_type;
2644 uint32_t f_bsize;
2645 uint64_t f_blocks;
2646 uint64_t f_bfree;
2647 uint64_t f_bavail;
2648 uint64_t f_files;
2649 uint64_t f_ffree;
2650 uint64_t fsid_val;
2651 uint32_t f_namelen;
2652 size_t offset = 7;
2653 int32_t bsize_factor;
2656 * compute bsize factor based on host file system block size
2657 * and client msize
2659 bsize_factor = (s->msize - P9_IOHDRSZ)/stbuf->f_bsize;
2660 if (!bsize_factor) {
2661 bsize_factor = 1;
2663 f_type = stbuf->f_type;
2664 f_bsize = stbuf->f_bsize;
2665 f_bsize *= bsize_factor;
2667 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2668 * adjust(divide) the number of blocks, free blocks and available
2669 * blocks by bsize factor
2671 f_blocks = stbuf->f_blocks/bsize_factor;
2672 f_bfree = stbuf->f_bfree/bsize_factor;
2673 f_bavail = stbuf->f_bavail/bsize_factor;
2674 f_files = stbuf->f_files;
2675 f_ffree = stbuf->f_ffree;
2676 fsid_val = (unsigned int) stbuf->f_fsid.__val[0] |
2677 (unsigned long long)stbuf->f_fsid.__val[1] << 32;
2678 f_namelen = stbuf->f_namelen;
2680 return pdu_marshal(pdu, offset, "ddqqqqqqd",
2681 f_type, f_bsize, f_blocks, f_bfree,
2682 f_bavail, f_files, f_ffree,
2683 fsid_val, f_namelen);
2686 static void v9fs_statfs(void *opaque)
2688 int32_t fid;
2689 ssize_t retval = 0;
2690 size_t offset = 7;
2691 V9fsFidState *fidp;
2692 struct statfs stbuf;
2693 V9fsPDU *pdu = opaque;
2694 V9fsState *s = pdu->s;
2696 pdu_unmarshal(pdu, offset, "d", &fid);
2697 fidp = get_fid(s, fid);
2698 if (fidp == NULL) {
2699 retval = -ENOENT;
2700 goto out_nofid;
2702 retval = v9fs_co_statfs(s, &fidp->path, &stbuf);
2703 if (retval < 0) {
2704 goto out;
2706 retval = offset;
2707 retval += v9fs_fill_statfs(s, pdu, &stbuf);
2708 out:
2709 put_fid(s, fidp);
2710 out_nofid:
2711 complete_pdu(s, pdu, retval);
2712 return;
2715 static void v9fs_mknod(void *opaque)
2718 int mode;
2719 gid_t gid;
2720 int32_t fid;
2721 V9fsQID qid;
2722 int err = 0;
2723 int major, minor;
2724 size_t offset = 7;
2725 V9fsString name;
2726 struct stat stbuf;
2727 V9fsFidState *fidp;
2728 V9fsPDU *pdu = opaque;
2729 V9fsState *s = pdu->s;
2731 pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode,
2732 &major, &minor, &gid);
2734 fidp = get_fid(s, fid);
2735 if (fidp == NULL) {
2736 err = -ENOENT;
2737 goto out_nofid;
2739 err = v9fs_co_mknod(s, fidp, &name, fidp->uid, gid,
2740 makedev(major, minor), mode, &stbuf);
2741 if (err < 0) {
2742 goto out;
2744 stat_to_qid(&stbuf, &qid);
2745 err = offset;
2746 err += pdu_marshal(pdu, offset, "Q", &qid);
2747 out:
2748 put_fid(s, fidp);
2749 out_nofid:
2750 complete_pdu(s, pdu, err);
2751 v9fs_string_free(&name);
2755 * Implement posix byte range locking code
2756 * Server side handling of locking code is very simple, because 9p server in
2757 * QEMU can handle only one client. And most of the lock handling
2758 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
2759 * do any thing in * qemu 9p server side lock code path.
2760 * So when a TLOCK request comes, always return success
2762 static void v9fs_lock(void *opaque)
2764 int8_t status;
2765 V9fsFlock *flock;
2766 size_t offset = 7;
2767 struct stat stbuf;
2768 V9fsFidState *fidp;
2769 int32_t fid, err = 0;
2770 V9fsPDU *pdu = opaque;
2771 V9fsState *s = pdu->s;
2773 flock = g_malloc(sizeof(*flock));
2774 pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock->type,
2775 &flock->flags, &flock->start, &flock->length,
2776 &flock->proc_id, &flock->client_id);
2777 status = P9_LOCK_ERROR;
2779 /* We support only block flag now (that too ignored currently) */
2780 if (flock->flags & ~P9_LOCK_FLAGS_BLOCK) {
2781 err = -EINVAL;
2782 goto out_nofid;
2784 fidp = get_fid(s, fid);
2785 if (fidp == NULL) {
2786 err = -ENOENT;
2787 goto out_nofid;
2789 err = v9fs_co_fstat(s, fidp->fs.fd, &stbuf);
2790 if (err < 0) {
2791 goto out;
2793 status = P9_LOCK_SUCCESS;
2794 out:
2795 put_fid(s, fidp);
2796 out_nofid:
2797 err = offset;
2798 err += pdu_marshal(pdu, offset, "b", status);
2799 complete_pdu(s, pdu, err);
2800 v9fs_string_free(&flock->client_id);
2801 g_free(flock);
2805 * When a TGETLOCK request comes, always return success because all lock
2806 * handling is done by client's VFS layer.
2808 static void v9fs_getlock(void *opaque)
2810 size_t offset = 7;
2811 struct stat stbuf;
2812 V9fsFidState *fidp;
2813 V9fsGetlock *glock;
2814 int32_t fid, err = 0;
2815 V9fsPDU *pdu = opaque;
2816 V9fsState *s = pdu->s;
2818 glock = g_malloc(sizeof(*glock));
2819 pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock->type,
2820 &glock->start, &glock->length, &glock->proc_id,
2821 &glock->client_id);
2823 fidp = get_fid(s, fid);
2824 if (fidp == NULL) {
2825 err = -ENOENT;
2826 goto out_nofid;
2828 err = v9fs_co_fstat(s, fidp->fs.fd, &stbuf);
2829 if (err < 0) {
2830 goto out;
2832 glock->type = F_UNLCK;
2833 offset += pdu_marshal(pdu, offset, "bqqds", glock->type,
2834 glock->start, glock->length, glock->proc_id,
2835 &glock->client_id);
2836 err = offset;
2837 out:
2838 put_fid(s, fidp);
2839 out_nofid:
2840 complete_pdu(s, pdu, err);
2841 v9fs_string_free(&glock->client_id);
2842 g_free(glock);
2845 static void v9fs_mkdir(void *opaque)
2847 V9fsPDU *pdu = opaque;
2848 size_t offset = 7;
2849 int32_t fid;
2850 struct stat stbuf;
2851 V9fsQID qid;
2852 V9fsString name;
2853 V9fsFidState *fidp;
2854 gid_t gid;
2855 int mode;
2856 int err = 0;
2858 pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid);
2860 fidp = get_fid(pdu->s, fid);
2861 if (fidp == NULL) {
2862 err = -ENOENT;
2863 goto out_nofid;
2865 err = v9fs_co_mkdir(pdu->s, fidp, &name, mode, fidp->uid, gid, &stbuf);
2866 if (err < 0) {
2867 goto out;
2869 stat_to_qid(&stbuf, &qid);
2870 offset += pdu_marshal(pdu, offset, "Q", &qid);
2871 err = offset;
2872 out:
2873 put_fid(pdu->s, fidp);
2874 out_nofid:
2875 complete_pdu(pdu->s, pdu, err);
2876 v9fs_string_free(&name);
2879 static void v9fs_xattrwalk(void *opaque)
2881 int64_t size;
2882 V9fsString name;
2883 ssize_t err = 0;
2884 size_t offset = 7;
2885 int32_t fid, newfid;
2886 V9fsFidState *file_fidp;
2887 V9fsFidState *xattr_fidp = NULL;
2888 V9fsPDU *pdu = opaque;
2889 V9fsState *s = pdu->s;
2891 pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name);
2892 file_fidp = get_fid(s, fid);
2893 if (file_fidp == NULL) {
2894 err = -ENOENT;
2895 goto out_nofid;
2897 xattr_fidp = alloc_fid(s, newfid);
2898 if (xattr_fidp == NULL) {
2899 err = -EINVAL;
2900 goto out;
2902 v9fs_string_copy(&xattr_fidp->path, &file_fidp->path);
2903 if (name.data[0] == 0) {
2905 * listxattr request. Get the size first
2907 size = v9fs_co_llistxattr(s, &xattr_fidp->path, NULL, 0);
2908 if (size < 0) {
2909 err = size;
2910 clunk_fid(s, xattr_fidp->fid);
2911 goto out;
2914 * Read the xattr value
2916 xattr_fidp->fs.xattr.len = size;
2917 xattr_fidp->fid_type = P9_FID_XATTR;
2918 xattr_fidp->fs.xattr.copied_len = -1;
2919 if (size) {
2920 xattr_fidp->fs.xattr.value = g_malloc(size);
2921 err = v9fs_co_llistxattr(s, &xattr_fidp->path,
2922 xattr_fidp->fs.xattr.value,
2923 xattr_fidp->fs.xattr.len);
2924 if (err < 0) {
2925 clunk_fid(s, xattr_fidp->fid);
2926 goto out;
2929 offset += pdu_marshal(pdu, offset, "q", size);
2930 err = offset;
2931 } else {
2933 * specific xattr fid. We check for xattr
2934 * presence also collect the xattr size
2936 size = v9fs_co_lgetxattr(s, &xattr_fidp->path,
2937 &name, NULL, 0);
2938 if (size < 0) {
2939 err = size;
2940 clunk_fid(s, xattr_fidp->fid);
2941 goto out;
2944 * Read the xattr value
2946 xattr_fidp->fs.xattr.len = size;
2947 xattr_fidp->fid_type = P9_FID_XATTR;
2948 xattr_fidp->fs.xattr.copied_len = -1;
2949 if (size) {
2950 xattr_fidp->fs.xattr.value = g_malloc(size);
2951 err = v9fs_co_lgetxattr(s, &xattr_fidp->path,
2952 &name, xattr_fidp->fs.xattr.value,
2953 xattr_fidp->fs.xattr.len);
2954 if (err < 0) {
2955 clunk_fid(s, xattr_fidp->fid);
2956 goto out;
2959 offset += pdu_marshal(pdu, offset, "q", size);
2960 err = offset;
2962 out:
2963 put_fid(s, file_fidp);
2964 if (xattr_fidp) {
2965 put_fid(s, xattr_fidp);
2967 out_nofid:
2968 complete_pdu(s, pdu, err);
2969 v9fs_string_free(&name);
2972 static void v9fs_xattrcreate(void *opaque)
2974 int flags;
2975 int32_t fid;
2976 int64_t size;
2977 ssize_t err = 0;
2978 V9fsString name;
2979 size_t offset = 7;
2980 V9fsFidState *file_fidp;
2981 V9fsFidState *xattr_fidp;
2982 V9fsPDU *pdu = opaque;
2983 V9fsState *s = pdu->s;
2985 pdu_unmarshal(pdu, offset, "dsqd",
2986 &fid, &name, &size, &flags);
2988 file_fidp = get_fid(s, fid);
2989 if (file_fidp == NULL) {
2990 err = -EINVAL;
2991 goto out_nofid;
2993 /* Make the file fid point to xattr */
2994 xattr_fidp = file_fidp;
2995 xattr_fidp->fid_type = P9_FID_XATTR;
2996 xattr_fidp->fs.xattr.copied_len = 0;
2997 xattr_fidp->fs.xattr.len = size;
2998 xattr_fidp->fs.xattr.flags = flags;
2999 v9fs_string_init(&xattr_fidp->fs.xattr.name);
3000 v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
3001 if (size) {
3002 xattr_fidp->fs.xattr.value = g_malloc(size);
3003 } else {
3004 xattr_fidp->fs.xattr.value = NULL;
3006 err = offset;
3007 put_fid(s, file_fidp);
3008 out_nofid:
3009 complete_pdu(s, pdu, err);
3010 v9fs_string_free(&name);
3013 static void v9fs_readlink(void *opaque)
3015 V9fsPDU *pdu = opaque;
3016 size_t offset = 7;
3017 V9fsString target;
3018 int32_t fid;
3019 int err = 0;
3020 V9fsFidState *fidp;
3022 pdu_unmarshal(pdu, offset, "d", &fid);
3023 fidp = get_fid(pdu->s, fid);
3024 if (fidp == NULL) {
3025 err = -ENOENT;
3026 goto out_nofid;
3029 v9fs_string_init(&target);
3030 err = v9fs_co_readlink(pdu->s, &fidp->path, &target);
3031 if (err < 0) {
3032 goto out;
3034 offset += pdu_marshal(pdu, offset, "s", &target);
3035 err = offset;
3036 v9fs_string_free(&target);
3037 out:
3038 put_fid(pdu->s, fidp);
3039 out_nofid:
3040 complete_pdu(pdu->s, pdu, err);
3043 static CoroutineEntry *pdu_co_handlers[] = {
3044 [P9_TREADDIR] = v9fs_readdir,
3045 [P9_TSTATFS] = v9fs_statfs,
3046 [P9_TGETATTR] = v9fs_getattr,
3047 [P9_TSETATTR] = v9fs_setattr,
3048 [P9_TXATTRWALK] = v9fs_xattrwalk,
3049 [P9_TXATTRCREATE] = v9fs_xattrcreate,
3050 [P9_TMKNOD] = v9fs_mknod,
3051 [P9_TRENAME] = v9fs_rename,
3052 [P9_TLOCK] = v9fs_lock,
3053 [P9_TGETLOCK] = v9fs_getlock,
3054 [P9_TRENAMEAT] = v9fs_renameat,
3055 [P9_TREADLINK] = v9fs_readlink,
3056 [P9_TUNLINKAT] = v9fs_unlinkat,
3057 [P9_TMKDIR] = v9fs_mkdir,
3058 [P9_TVERSION] = v9fs_version,
3059 [P9_TLOPEN] = v9fs_open,
3060 [P9_TATTACH] = v9fs_attach,
3061 [P9_TSTAT] = v9fs_stat,
3062 [P9_TWALK] = v9fs_walk,
3063 [P9_TCLUNK] = v9fs_clunk,
3064 [P9_TFSYNC] = v9fs_fsync,
3065 [P9_TOPEN] = v9fs_open,
3066 [P9_TREAD] = v9fs_read,
3067 #if 0
3068 [P9_TAUTH] = v9fs_auth,
3069 #endif
3070 [P9_TFLUSH] = v9fs_flush,
3071 [P9_TLINK] = v9fs_link,
3072 [P9_TSYMLINK] = v9fs_symlink,
3073 [P9_TCREATE] = v9fs_create,
3074 [P9_TLCREATE] = v9fs_lcreate,
3075 [P9_TWRITE] = v9fs_write,
3076 [P9_TWSTAT] = v9fs_wstat,
3077 [P9_TREMOVE] = v9fs_remove,
3080 static void v9fs_op_not_supp(void *opaque)
3082 V9fsPDU *pdu = opaque;
3083 complete_pdu(pdu->s, pdu, -EOPNOTSUPP);
3086 static void submit_pdu(V9fsState *s, V9fsPDU *pdu)
3088 Coroutine *co;
3089 CoroutineEntry *handler;
3091 if (debug_9p_pdu) {
3092 pprint_pdu(pdu);
3094 if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) ||
3095 (pdu_co_handlers[pdu->id] == NULL)) {
3096 handler = v9fs_op_not_supp;
3097 } else {
3098 handler = pdu_co_handlers[pdu->id];
3100 co = qemu_coroutine_create(handler);
3101 qemu_coroutine_enter(co, pdu);
3104 void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq)
3106 V9fsState *s = (V9fsState *)vdev;
3107 V9fsPDU *pdu;
3108 ssize_t len;
3110 while ((pdu = alloc_pdu(s)) &&
3111 (len = virtqueue_pop(vq, &pdu->elem)) != 0) {
3112 uint8_t *ptr;
3113 pdu->s = s;
3114 BUG_ON(pdu->elem.out_num == 0 || pdu->elem.in_num == 0);
3115 BUG_ON(pdu->elem.out_sg[0].iov_len < 7);
3117 ptr = pdu->elem.out_sg[0].iov_base;
3119 memcpy(&pdu->size, ptr, 4);
3120 pdu->id = ptr[4];
3121 memcpy(&pdu->tag, ptr + 5, 2);
3122 submit_pdu(s, pdu);
3124 free_pdu(s, pdu);
3127 void virtio_9p_set_fd_limit(void)
3129 struct rlimit rlim;
3130 if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
3131 fprintf(stderr, "Failed to get the resource limit\n");
3132 exit(1);
3134 open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur/3);
3135 open_fd_rc = rlim.rlim_cur/2;