9pfs: fix potential host memory leak in v9fs_read
[qemu/ar7.git] / hw / 9pfs / 9p.c
blobff94a6272c3ce427c14c1b61faa6dfa276b11e27
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 "qemu/osdep.h"
15 #include <glib/gprintf.h>
16 #include "hw/virtio/virtio.h"
17 #include "qapi/error.h"
18 #include "qemu/error-report.h"
19 #include "qemu/iov.h"
20 #include "qemu/sockets.h"
21 #include "virtio-9p.h"
22 #include "fsdev/qemu-fsdev.h"
23 #include "9p-xattr.h"
24 #include "coth.h"
25 #include "trace.h"
26 #include "migration/migration.h"
28 int open_fd_hw;
29 int total_open_fd;
30 static int open_fd_rc;
32 enum {
33 Oread = 0x00,
34 Owrite = 0x01,
35 Ordwr = 0x02,
36 Oexec = 0x03,
37 Oexcl = 0x04,
38 Otrunc = 0x10,
39 Orexec = 0x20,
40 Orclose = 0x40,
41 Oappend = 0x80,
44 ssize_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
46 ssize_t ret;
47 va_list ap;
49 va_start(ap, fmt);
50 ret = virtio_pdu_vmarshal(pdu, offset, fmt, ap);
51 va_end(ap);
53 return ret;
56 ssize_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
58 ssize_t ret;
59 va_list ap;
61 va_start(ap, fmt);
62 ret = virtio_pdu_vunmarshal(pdu, offset, fmt, ap);
63 va_end(ap);
65 return ret;
68 static void pdu_push_and_notify(V9fsPDU *pdu)
70 virtio_9p_push_and_notify(pdu);
73 static int omode_to_uflags(int8_t mode)
75 int ret = 0;
77 switch (mode & 3) {
78 case Oread:
79 ret = O_RDONLY;
80 break;
81 case Ordwr:
82 ret = O_RDWR;
83 break;
84 case Owrite:
85 ret = O_WRONLY;
86 break;
87 case Oexec:
88 ret = O_RDONLY;
89 break;
92 if (mode & Otrunc) {
93 ret |= O_TRUNC;
96 if (mode & Oappend) {
97 ret |= O_APPEND;
100 if (mode & Oexcl) {
101 ret |= O_EXCL;
104 return ret;
107 struct dotl_openflag_map {
108 int dotl_flag;
109 int open_flag;
112 static int dotl_to_open_flags(int flags)
114 int i;
116 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
117 * and P9_DOTL_NOACCESS
119 int oflags = flags & O_ACCMODE;
121 struct dotl_openflag_map dotl_oflag_map[] = {
122 { P9_DOTL_CREATE, O_CREAT },
123 { P9_DOTL_EXCL, O_EXCL },
124 { P9_DOTL_NOCTTY , O_NOCTTY },
125 { P9_DOTL_TRUNC, O_TRUNC },
126 { P9_DOTL_APPEND, O_APPEND },
127 { P9_DOTL_NONBLOCK, O_NONBLOCK } ,
128 { P9_DOTL_DSYNC, O_DSYNC },
129 { P9_DOTL_FASYNC, FASYNC },
130 { P9_DOTL_DIRECT, O_DIRECT },
131 { P9_DOTL_LARGEFILE, O_LARGEFILE },
132 { P9_DOTL_DIRECTORY, O_DIRECTORY },
133 { P9_DOTL_NOFOLLOW, O_NOFOLLOW },
134 { P9_DOTL_NOATIME, O_NOATIME },
135 { P9_DOTL_SYNC, O_SYNC },
138 for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) {
139 if (flags & dotl_oflag_map[i].dotl_flag) {
140 oflags |= dotl_oflag_map[i].open_flag;
144 return oflags;
147 void cred_init(FsCred *credp)
149 credp->fc_uid = -1;
150 credp->fc_gid = -1;
151 credp->fc_mode = -1;
152 credp->fc_rdev = -1;
155 static int get_dotl_openflags(V9fsState *s, int oflags)
157 int flags;
159 * Filter the client open flags
161 flags = dotl_to_open_flags(oflags);
162 flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT);
164 * Ignore direct disk access hint until the server supports it.
166 flags &= ~O_DIRECT;
167 return flags;
170 void v9fs_path_init(V9fsPath *path)
172 path->data = NULL;
173 path->size = 0;
176 void v9fs_path_free(V9fsPath *path)
178 g_free(path->data);
179 path->data = NULL;
180 path->size = 0;
184 void GCC_FMT_ATTR(2, 3)
185 v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...)
187 va_list ap;
189 v9fs_path_free(path);
191 va_start(ap, fmt);
192 /* Bump the size for including terminating NULL */
193 path->size = g_vasprintf(&path->data, fmt, ap) + 1;
194 va_end(ap);
197 void v9fs_path_copy(V9fsPath *lhs, V9fsPath *rhs)
199 v9fs_path_free(lhs);
200 lhs->data = g_malloc(rhs->size);
201 memcpy(lhs->data, rhs->data, rhs->size);
202 lhs->size = rhs->size;
205 int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
206 const char *name, V9fsPath *path)
208 int err;
209 err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
210 if (err < 0) {
211 err = -errno;
213 return err;
217 * Return TRUE if s1 is an ancestor of s2.
219 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
220 * As a special case, We treat s1 as ancestor of s2 if they are same!
222 static int v9fs_path_is_ancestor(V9fsPath *s1, V9fsPath *s2)
224 if (!strncmp(s1->data, s2->data, s1->size - 1)) {
225 if (s2->data[s1->size - 1] == '\0' || s2->data[s1->size - 1] == '/') {
226 return 1;
229 return 0;
232 static size_t v9fs_string_size(V9fsString *str)
234 return str->size;
238 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
239 static int v9fs_reopen_fid(V9fsPDU *pdu, V9fsFidState *f)
241 int err = 1;
242 if (f->fid_type == P9_FID_FILE) {
243 if (f->fs.fd == -1) {
244 do {
245 err = v9fs_co_open(pdu, f, f->open_flags);
246 } while (err == -EINTR && !pdu->cancelled);
248 } else if (f->fid_type == P9_FID_DIR) {
249 if (f->fs.dir.stream == NULL) {
250 do {
251 err = v9fs_co_opendir(pdu, f);
252 } while (err == -EINTR && !pdu->cancelled);
255 return err;
258 static V9fsFidState *get_fid(V9fsPDU *pdu, int32_t fid)
260 int err;
261 V9fsFidState *f;
262 V9fsState *s = pdu->s;
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(pdu, 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 v9fs_readdir_init(&f->fs.dir);
319 v9fs_readdir_init(&f->fs_reclaim.dir);
321 return f;
324 static int v9fs_xattr_fid_clunk(V9fsPDU *pdu, V9fsFidState *fidp)
326 int retval = 0;
328 if (fidp->fs.xattr.copied_len == -1) {
329 /* getxattr/listxattr fid */
330 goto free_value;
333 * if this is fid for setxattr. clunk should
334 * result in setxattr localcall
336 if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) {
337 /* clunk after partial write */
338 retval = -EINVAL;
339 goto free_out;
341 if (fidp->fs.xattr.len) {
342 retval = v9fs_co_lsetxattr(pdu, &fidp->path, &fidp->fs.xattr.name,
343 fidp->fs.xattr.value,
344 fidp->fs.xattr.len,
345 fidp->fs.xattr.flags);
346 } else {
347 retval = v9fs_co_lremovexattr(pdu, &fidp->path, &fidp->fs.xattr.name);
349 free_out:
350 v9fs_string_free(&fidp->fs.xattr.name);
351 free_value:
352 g_free(fidp->fs.xattr.value);
353 return retval;
356 static int free_fid(V9fsPDU *pdu, V9fsFidState *fidp)
358 int retval = 0;
360 if (fidp->fid_type == P9_FID_FILE) {
361 /* If we reclaimed the fd no need to close */
362 if (fidp->fs.fd != -1) {
363 retval = v9fs_co_close(pdu, &fidp->fs);
365 } else if (fidp->fid_type == P9_FID_DIR) {
366 if (fidp->fs.dir.stream != NULL) {
367 retval = v9fs_co_closedir(pdu, &fidp->fs);
369 } else if (fidp->fid_type == P9_FID_XATTR) {
370 retval = v9fs_xattr_fid_clunk(pdu, fidp);
372 v9fs_path_free(&fidp->path);
373 g_free(fidp);
374 return retval;
377 static int put_fid(V9fsPDU *pdu, V9fsFidState *fidp)
379 BUG_ON(!fidp->ref);
380 fidp->ref--;
382 * Don't free the fid if it is in reclaim list
384 if (!fidp->ref && fidp->clunked) {
385 if (fidp->fid == pdu->s->root_fid) {
387 * if the clunked fid is root fid then we
388 * have unmounted the fs on the client side.
389 * delete the migration blocker. Ideally, this
390 * should be hooked to transport close notification
392 if (pdu->s->migration_blocker) {
393 migrate_del_blocker(pdu->s->migration_blocker);
394 error_free(pdu->s->migration_blocker);
395 pdu->s->migration_blocker = NULL;
398 return free_fid(pdu, fidp);
400 return 0;
403 static V9fsFidState *clunk_fid(V9fsState *s, int32_t fid)
405 V9fsFidState **fidpp, *fidp;
407 for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
408 if ((*fidpp)->fid == fid) {
409 break;
412 if (*fidpp == NULL) {
413 return NULL;
415 fidp = *fidpp;
416 *fidpp = fidp->next;
417 fidp->clunked = 1;
418 return fidp;
421 void v9fs_reclaim_fd(V9fsPDU *pdu)
423 int reclaim_count = 0;
424 V9fsState *s = pdu->s;
425 V9fsFidState *f, *reclaim_list = NULL;
427 for (f = s->fid_list; f; f = f->next) {
429 * Unlink fids cannot be reclaimed. Check
430 * for them and skip them. Also skip fids
431 * currently being operated on.
433 if (f->ref || f->flags & FID_NON_RECLAIMABLE) {
434 continue;
437 * if it is a recently referenced fid
438 * we leave the fid untouched and clear the
439 * reference bit. We come back to it later
440 * in the next iteration. (a simple LRU without
441 * moving list elements around)
443 if (f->flags & FID_REFERENCED) {
444 f->flags &= ~FID_REFERENCED;
445 continue;
448 * Add fids to reclaim list.
450 if (f->fid_type == P9_FID_FILE) {
451 if (f->fs.fd != -1) {
453 * Up the reference count so that
454 * a clunk request won't free this fid
456 f->ref++;
457 f->rclm_lst = reclaim_list;
458 reclaim_list = f;
459 f->fs_reclaim.fd = f->fs.fd;
460 f->fs.fd = -1;
461 reclaim_count++;
463 } else if (f->fid_type == P9_FID_DIR) {
464 if (f->fs.dir.stream != NULL) {
466 * Up the reference count so that
467 * a clunk request won't free this fid
469 f->ref++;
470 f->rclm_lst = reclaim_list;
471 reclaim_list = f;
472 f->fs_reclaim.dir.stream = f->fs.dir.stream;
473 f->fs.dir.stream = NULL;
474 reclaim_count++;
477 if (reclaim_count >= open_fd_rc) {
478 break;
482 * Now close the fid in reclaim list. Free them if they
483 * are already clunked.
485 while (reclaim_list) {
486 f = reclaim_list;
487 reclaim_list = f->rclm_lst;
488 if (f->fid_type == P9_FID_FILE) {
489 v9fs_co_close(pdu, &f->fs_reclaim);
490 } else if (f->fid_type == P9_FID_DIR) {
491 v9fs_co_closedir(pdu, &f->fs_reclaim);
493 f->rclm_lst = NULL;
495 * Now drop the fid reference, free it
496 * if clunked.
498 put_fid(pdu, f);
502 static int v9fs_mark_fids_unreclaim(V9fsPDU *pdu, V9fsPath *path)
504 int err;
505 V9fsState *s = pdu->s;
506 V9fsFidState *fidp, head_fid;
508 head_fid.next = s->fid_list;
509 for (fidp = s->fid_list; fidp; fidp = fidp->next) {
510 if (fidp->path.size != path->size) {
511 continue;
513 if (!memcmp(fidp->path.data, path->data, path->size)) {
514 /* Mark the fid non reclaimable. */
515 fidp->flags |= FID_NON_RECLAIMABLE;
517 /* reopen the file/dir if already closed */
518 err = v9fs_reopen_fid(pdu, fidp);
519 if (err < 0) {
520 return -1;
523 * Go back to head of fid list because
524 * the list could have got updated when
525 * switched to the worker thread
527 if (err == 0) {
528 fidp = &head_fid;
532 return 0;
535 static void virtfs_reset(V9fsPDU *pdu)
537 V9fsState *s = pdu->s;
538 V9fsFidState *fidp = NULL;
540 /* Free all fids */
541 while (s->fid_list) {
542 fidp = s->fid_list;
543 s->fid_list = fidp->next;
545 if (fidp->ref) {
546 fidp->clunked = 1;
547 } else {
548 free_fid(pdu, fidp);
551 if (fidp) {
552 /* One or more unclunked fids found... */
553 error_report("9pfs:%s: One or more uncluncked fids "
554 "found during reset", __func__);
558 #define P9_QID_TYPE_DIR 0x80
559 #define P9_QID_TYPE_SYMLINK 0x02
561 #define P9_STAT_MODE_DIR 0x80000000
562 #define P9_STAT_MODE_APPEND 0x40000000
563 #define P9_STAT_MODE_EXCL 0x20000000
564 #define P9_STAT_MODE_MOUNT 0x10000000
565 #define P9_STAT_MODE_AUTH 0x08000000
566 #define P9_STAT_MODE_TMP 0x04000000
567 #define P9_STAT_MODE_SYMLINK 0x02000000
568 #define P9_STAT_MODE_LINK 0x01000000
569 #define P9_STAT_MODE_DEVICE 0x00800000
570 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
571 #define P9_STAT_MODE_SOCKET 0x00100000
572 #define P9_STAT_MODE_SETUID 0x00080000
573 #define P9_STAT_MODE_SETGID 0x00040000
574 #define P9_STAT_MODE_SETVTX 0x00010000
576 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
577 P9_STAT_MODE_SYMLINK | \
578 P9_STAT_MODE_LINK | \
579 P9_STAT_MODE_DEVICE | \
580 P9_STAT_MODE_NAMED_PIPE | \
581 P9_STAT_MODE_SOCKET)
583 /* This is the algorithm from ufs in spfs */
584 static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
586 size_t size;
588 memset(&qidp->path, 0, sizeof(qidp->path));
589 size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
590 memcpy(&qidp->path, &stbuf->st_ino, size);
591 qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
592 qidp->type = 0;
593 if (S_ISDIR(stbuf->st_mode)) {
594 qidp->type |= P9_QID_TYPE_DIR;
596 if (S_ISLNK(stbuf->st_mode)) {
597 qidp->type |= P9_QID_TYPE_SYMLINK;
601 static int fid_to_qid(V9fsPDU *pdu, V9fsFidState *fidp, V9fsQID *qidp)
603 struct stat stbuf;
604 int err;
606 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
607 if (err < 0) {
608 return err;
610 stat_to_qid(&stbuf, qidp);
611 return 0;
614 V9fsPDU *pdu_alloc(V9fsState *s)
616 V9fsPDU *pdu = NULL;
618 if (!QLIST_EMPTY(&s->free_list)) {
619 pdu = QLIST_FIRST(&s->free_list);
620 QLIST_REMOVE(pdu, next);
621 QLIST_INSERT_HEAD(&s->active_list, pdu, next);
623 return pdu;
626 void pdu_free(V9fsPDU *pdu)
628 if (pdu) {
629 V9fsState *s = pdu->s;
631 * Cancelled pdu are added back to the freelist
632 * by flush request .
634 if (!pdu->cancelled) {
635 QLIST_REMOVE(pdu, next);
636 QLIST_INSERT_HEAD(&s->free_list, pdu, next);
642 * We don't do error checking for pdu_marshal/unmarshal here
643 * because we always expect to have enough space to encode
644 * error details
646 static void pdu_complete(V9fsPDU *pdu, ssize_t len)
648 int8_t id = pdu->id + 1; /* Response */
649 V9fsState *s = pdu->s;
651 if (len < 0) {
652 int err = -len;
653 len = 7;
655 if (s->proto_version != V9FS_PROTO_2000L) {
656 V9fsString str;
658 str.data = strerror(err);
659 str.size = strlen(str.data);
661 len += pdu_marshal(pdu, len, "s", &str);
662 id = P9_RERROR;
665 len += pdu_marshal(pdu, len, "d", err);
667 if (s->proto_version == V9FS_PROTO_2000L) {
668 id = P9_RLERROR;
670 trace_v9fs_rerror(pdu->tag, pdu->id, err); /* Trace ERROR */
673 /* fill out the header */
674 pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
676 /* keep these in sync */
677 pdu->size = len;
678 pdu->id = id;
680 pdu_push_and_notify(pdu);
682 /* Now wakeup anybody waiting in flush for this request */
683 qemu_co_queue_next(&pdu->complete);
685 pdu_free(pdu);
688 static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
690 mode_t ret;
692 ret = mode & 0777;
693 if (mode & P9_STAT_MODE_DIR) {
694 ret |= S_IFDIR;
697 if (mode & P9_STAT_MODE_SYMLINK) {
698 ret |= S_IFLNK;
700 if (mode & P9_STAT_MODE_SOCKET) {
701 ret |= S_IFSOCK;
703 if (mode & P9_STAT_MODE_NAMED_PIPE) {
704 ret |= S_IFIFO;
706 if (mode & P9_STAT_MODE_DEVICE) {
707 if (extension->size && extension->data[0] == 'c') {
708 ret |= S_IFCHR;
709 } else {
710 ret |= S_IFBLK;
714 if (!(ret&~0777)) {
715 ret |= S_IFREG;
718 if (mode & P9_STAT_MODE_SETUID) {
719 ret |= S_ISUID;
721 if (mode & P9_STAT_MODE_SETGID) {
722 ret |= S_ISGID;
724 if (mode & P9_STAT_MODE_SETVTX) {
725 ret |= S_ISVTX;
728 return ret;
731 static int donttouch_stat(V9fsStat *stat)
733 if (stat->type == -1 &&
734 stat->dev == -1 &&
735 stat->qid.type == -1 &&
736 stat->qid.version == -1 &&
737 stat->qid.path == -1 &&
738 stat->mode == -1 &&
739 stat->atime == -1 &&
740 stat->mtime == -1 &&
741 stat->length == -1 &&
742 !stat->name.size &&
743 !stat->uid.size &&
744 !stat->gid.size &&
745 !stat->muid.size &&
746 stat->n_uid == -1 &&
747 stat->n_gid == -1 &&
748 stat->n_muid == -1) {
749 return 1;
752 return 0;
755 static void v9fs_stat_init(V9fsStat *stat)
757 v9fs_string_init(&stat->name);
758 v9fs_string_init(&stat->uid);
759 v9fs_string_init(&stat->gid);
760 v9fs_string_init(&stat->muid);
761 v9fs_string_init(&stat->extension);
764 static void v9fs_stat_free(V9fsStat *stat)
766 v9fs_string_free(&stat->name);
767 v9fs_string_free(&stat->uid);
768 v9fs_string_free(&stat->gid);
769 v9fs_string_free(&stat->muid);
770 v9fs_string_free(&stat->extension);
773 static uint32_t stat_to_v9mode(const struct stat *stbuf)
775 uint32_t mode;
777 mode = stbuf->st_mode & 0777;
778 if (S_ISDIR(stbuf->st_mode)) {
779 mode |= P9_STAT_MODE_DIR;
782 if (S_ISLNK(stbuf->st_mode)) {
783 mode |= P9_STAT_MODE_SYMLINK;
786 if (S_ISSOCK(stbuf->st_mode)) {
787 mode |= P9_STAT_MODE_SOCKET;
790 if (S_ISFIFO(stbuf->st_mode)) {
791 mode |= P9_STAT_MODE_NAMED_PIPE;
794 if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
795 mode |= P9_STAT_MODE_DEVICE;
798 if (stbuf->st_mode & S_ISUID) {
799 mode |= P9_STAT_MODE_SETUID;
802 if (stbuf->st_mode & S_ISGID) {
803 mode |= P9_STAT_MODE_SETGID;
806 if (stbuf->st_mode & S_ISVTX) {
807 mode |= P9_STAT_MODE_SETVTX;
810 return mode;
813 static int stat_to_v9stat(V9fsPDU *pdu, V9fsPath *name,
814 const struct stat *stbuf,
815 V9fsStat *v9stat)
817 int err;
818 const char *str;
820 memset(v9stat, 0, sizeof(*v9stat));
822 stat_to_qid(stbuf, &v9stat->qid);
823 v9stat->mode = stat_to_v9mode(stbuf);
824 v9stat->atime = stbuf->st_atime;
825 v9stat->mtime = stbuf->st_mtime;
826 v9stat->length = stbuf->st_size;
828 v9fs_string_free(&v9stat->uid);
829 v9fs_string_free(&v9stat->gid);
830 v9fs_string_free(&v9stat->muid);
832 v9stat->n_uid = stbuf->st_uid;
833 v9stat->n_gid = stbuf->st_gid;
834 v9stat->n_muid = 0;
836 v9fs_string_free(&v9stat->extension);
838 if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
839 err = v9fs_co_readlink(pdu, name, &v9stat->extension);
840 if (err < 0) {
841 return err;
843 } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
844 v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
845 S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
846 major(stbuf->st_rdev), minor(stbuf->st_rdev));
847 } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
848 v9fs_string_sprintf(&v9stat->extension, "%s %lu",
849 "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink);
852 str = strrchr(name->data, '/');
853 if (str) {
854 str += 1;
855 } else {
856 str = name->data;
859 v9fs_string_sprintf(&v9stat->name, "%s", str);
861 v9stat->size = 61 +
862 v9fs_string_size(&v9stat->name) +
863 v9fs_string_size(&v9stat->uid) +
864 v9fs_string_size(&v9stat->gid) +
865 v9fs_string_size(&v9stat->muid) +
866 v9fs_string_size(&v9stat->extension);
867 return 0;
870 #define P9_STATS_MODE 0x00000001ULL
871 #define P9_STATS_NLINK 0x00000002ULL
872 #define P9_STATS_UID 0x00000004ULL
873 #define P9_STATS_GID 0x00000008ULL
874 #define P9_STATS_RDEV 0x00000010ULL
875 #define P9_STATS_ATIME 0x00000020ULL
876 #define P9_STATS_MTIME 0x00000040ULL
877 #define P9_STATS_CTIME 0x00000080ULL
878 #define P9_STATS_INO 0x00000100ULL
879 #define P9_STATS_SIZE 0x00000200ULL
880 #define P9_STATS_BLOCKS 0x00000400ULL
882 #define P9_STATS_BTIME 0x00000800ULL
883 #define P9_STATS_GEN 0x00001000ULL
884 #define P9_STATS_DATA_VERSION 0x00002000ULL
886 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
887 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
890 static void stat_to_v9stat_dotl(V9fsState *s, const struct stat *stbuf,
891 V9fsStatDotl *v9lstat)
893 memset(v9lstat, 0, sizeof(*v9lstat));
895 v9lstat->st_mode = stbuf->st_mode;
896 v9lstat->st_nlink = stbuf->st_nlink;
897 v9lstat->st_uid = stbuf->st_uid;
898 v9lstat->st_gid = stbuf->st_gid;
899 v9lstat->st_rdev = stbuf->st_rdev;
900 v9lstat->st_size = stbuf->st_size;
901 v9lstat->st_blksize = stbuf->st_blksize;
902 v9lstat->st_blocks = stbuf->st_blocks;
903 v9lstat->st_atime_sec = stbuf->st_atime;
904 v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
905 v9lstat->st_mtime_sec = stbuf->st_mtime;
906 v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
907 v9lstat->st_ctime_sec = stbuf->st_ctime;
908 v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
909 /* Currently we only support BASIC fields in stat */
910 v9lstat->st_result_mask = P9_STATS_BASIC;
912 stat_to_qid(stbuf, &v9lstat->qid);
915 static void print_sg(struct iovec *sg, int cnt)
917 int i;
919 printf("sg[%d]: {", cnt);
920 for (i = 0; i < cnt; i++) {
921 if (i) {
922 printf(", ");
924 printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
926 printf("}\n");
929 /* Will call this only for path name based fid */
930 static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len)
932 V9fsPath str;
933 v9fs_path_init(&str);
934 v9fs_path_copy(&str, dst);
935 v9fs_path_sprintf(dst, "%s%s", src->data, str.data + len);
936 v9fs_path_free(&str);
939 static inline bool is_ro_export(FsContext *ctx)
941 return ctx->export_flags & V9FS_RDONLY;
944 static void v9fs_version(void *opaque)
946 ssize_t err;
947 V9fsPDU *pdu = opaque;
948 V9fsState *s = pdu->s;
949 V9fsString version;
950 size_t offset = 7;
952 v9fs_string_init(&version);
953 err = pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
954 if (err < 0) {
955 offset = err;
956 goto out;
958 trace_v9fs_version(pdu->tag, pdu->id, s->msize, version.data);
960 virtfs_reset(pdu);
962 if (!strcmp(version.data, "9P2000.u")) {
963 s->proto_version = V9FS_PROTO_2000U;
964 } else if (!strcmp(version.data, "9P2000.L")) {
965 s->proto_version = V9FS_PROTO_2000L;
966 } else {
967 v9fs_string_sprintf(&version, "unknown");
970 err = pdu_marshal(pdu, offset, "ds", s->msize, &version);
971 if (err < 0) {
972 offset = err;
973 goto out;
975 offset += err;
976 trace_v9fs_version_return(pdu->tag, pdu->id, s->msize, version.data);
977 out:
978 pdu_complete(pdu, offset);
979 v9fs_string_free(&version);
982 static void v9fs_attach(void *opaque)
984 V9fsPDU *pdu = opaque;
985 V9fsState *s = pdu->s;
986 int32_t fid, afid, n_uname;
987 V9fsString uname, aname;
988 V9fsFidState *fidp;
989 size_t offset = 7;
990 V9fsQID qid;
991 ssize_t err;
993 v9fs_string_init(&uname);
994 v9fs_string_init(&aname);
995 err = pdu_unmarshal(pdu, offset, "ddssd", &fid,
996 &afid, &uname, &aname, &n_uname);
997 if (err < 0) {
998 goto out_nofid;
1000 trace_v9fs_attach(pdu->tag, pdu->id, fid, afid, uname.data, aname.data);
1002 fidp = alloc_fid(s, fid);
1003 if (fidp == NULL) {
1004 err = -EINVAL;
1005 goto out_nofid;
1007 fidp->uid = n_uname;
1008 err = v9fs_co_name_to_path(pdu, NULL, "/", &fidp->path);
1009 if (err < 0) {
1010 err = -EINVAL;
1011 clunk_fid(s, fid);
1012 goto out;
1014 err = fid_to_qid(pdu, fidp, &qid);
1015 if (err < 0) {
1016 err = -EINVAL;
1017 clunk_fid(s, fid);
1018 goto out;
1020 err = pdu_marshal(pdu, offset, "Q", &qid);
1021 if (err < 0) {
1022 clunk_fid(s, fid);
1023 goto out;
1025 err += offset;
1026 memcpy(&s->root_qid, &qid, sizeof(qid));
1027 trace_v9fs_attach_return(pdu->tag, pdu->id,
1028 qid.type, qid.version, qid.path);
1030 * disable migration if we haven't done already.
1031 * attach could get called multiple times for the same export.
1033 if (!s->migration_blocker) {
1034 s->root_fid = fid;
1035 error_setg(&s->migration_blocker,
1036 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1037 s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag);
1038 migrate_add_blocker(s->migration_blocker);
1040 out:
1041 put_fid(pdu, fidp);
1042 out_nofid:
1043 pdu_complete(pdu, err);
1044 v9fs_string_free(&uname);
1045 v9fs_string_free(&aname);
1048 static void v9fs_stat(void *opaque)
1050 int32_t fid;
1051 V9fsStat v9stat;
1052 ssize_t err = 0;
1053 size_t offset = 7;
1054 struct stat stbuf;
1055 V9fsFidState *fidp;
1056 V9fsPDU *pdu = opaque;
1058 err = pdu_unmarshal(pdu, offset, "d", &fid);
1059 if (err < 0) {
1060 goto out_nofid;
1062 trace_v9fs_stat(pdu->tag, pdu->id, fid);
1064 fidp = get_fid(pdu, fid);
1065 if (fidp == NULL) {
1066 err = -ENOENT;
1067 goto out_nofid;
1069 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1070 if (err < 0) {
1071 goto out;
1073 err = stat_to_v9stat(pdu, &fidp->path, &stbuf, &v9stat);
1074 if (err < 0) {
1075 goto out;
1077 err = pdu_marshal(pdu, offset, "wS", 0, &v9stat);
1078 if (err < 0) {
1079 v9fs_stat_free(&v9stat);
1080 goto out;
1082 trace_v9fs_stat_return(pdu->tag, pdu->id, v9stat.mode,
1083 v9stat.atime, v9stat.mtime, v9stat.length);
1084 err += offset;
1085 v9fs_stat_free(&v9stat);
1086 out:
1087 put_fid(pdu, fidp);
1088 out_nofid:
1089 pdu_complete(pdu, err);
1092 static void v9fs_getattr(void *opaque)
1094 int32_t fid;
1095 size_t offset = 7;
1096 ssize_t retval = 0;
1097 struct stat stbuf;
1098 V9fsFidState *fidp;
1099 uint64_t request_mask;
1100 V9fsStatDotl v9stat_dotl;
1101 V9fsPDU *pdu = opaque;
1102 V9fsState *s = pdu->s;
1104 retval = pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask);
1105 if (retval < 0) {
1106 goto out_nofid;
1108 trace_v9fs_getattr(pdu->tag, pdu->id, fid, request_mask);
1110 fidp = get_fid(pdu, fid);
1111 if (fidp == NULL) {
1112 retval = -ENOENT;
1113 goto out_nofid;
1116 * Currently we only support BASIC fields in stat, so there is no
1117 * need to look at request_mask.
1119 retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1120 if (retval < 0) {
1121 goto out;
1123 stat_to_v9stat_dotl(s, &stbuf, &v9stat_dotl);
1125 /* fill st_gen if requested and supported by underlying fs */
1126 if (request_mask & P9_STATS_GEN) {
1127 retval = v9fs_co_st_gen(pdu, &fidp->path, stbuf.st_mode, &v9stat_dotl);
1128 switch (retval) {
1129 case 0:
1130 /* we have valid st_gen: update result mask */
1131 v9stat_dotl.st_result_mask |= P9_STATS_GEN;
1132 break;
1133 case -EINTR:
1134 /* request cancelled, e.g. by Tflush */
1135 goto out;
1136 default:
1137 /* failed to get st_gen: not fatal, ignore */
1138 break;
1141 retval = pdu_marshal(pdu, offset, "A", &v9stat_dotl);
1142 if (retval < 0) {
1143 goto out;
1145 retval += offset;
1146 trace_v9fs_getattr_return(pdu->tag, pdu->id, v9stat_dotl.st_result_mask,
1147 v9stat_dotl.st_mode, v9stat_dotl.st_uid,
1148 v9stat_dotl.st_gid);
1149 out:
1150 put_fid(pdu, fidp);
1151 out_nofid:
1152 pdu_complete(pdu, retval);
1155 /* Attribute flags */
1156 #define P9_ATTR_MODE (1 << 0)
1157 #define P9_ATTR_UID (1 << 1)
1158 #define P9_ATTR_GID (1 << 2)
1159 #define P9_ATTR_SIZE (1 << 3)
1160 #define P9_ATTR_ATIME (1 << 4)
1161 #define P9_ATTR_MTIME (1 << 5)
1162 #define P9_ATTR_CTIME (1 << 6)
1163 #define P9_ATTR_ATIME_SET (1 << 7)
1164 #define P9_ATTR_MTIME_SET (1 << 8)
1166 #define P9_ATTR_MASK 127
1168 static void v9fs_setattr(void *opaque)
1170 int err = 0;
1171 int32_t fid;
1172 V9fsFidState *fidp;
1173 size_t offset = 7;
1174 V9fsIattr v9iattr;
1175 V9fsPDU *pdu = opaque;
1177 err = pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr);
1178 if (err < 0) {
1179 goto out_nofid;
1182 fidp = get_fid(pdu, fid);
1183 if (fidp == NULL) {
1184 err = -EINVAL;
1185 goto out_nofid;
1187 if (v9iattr.valid & P9_ATTR_MODE) {
1188 err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode);
1189 if (err < 0) {
1190 goto out;
1193 if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) {
1194 struct timespec times[2];
1195 if (v9iattr.valid & P9_ATTR_ATIME) {
1196 if (v9iattr.valid & P9_ATTR_ATIME_SET) {
1197 times[0].tv_sec = v9iattr.atime_sec;
1198 times[0].tv_nsec = v9iattr.atime_nsec;
1199 } else {
1200 times[0].tv_nsec = UTIME_NOW;
1202 } else {
1203 times[0].tv_nsec = UTIME_OMIT;
1205 if (v9iattr.valid & P9_ATTR_MTIME) {
1206 if (v9iattr.valid & P9_ATTR_MTIME_SET) {
1207 times[1].tv_sec = v9iattr.mtime_sec;
1208 times[1].tv_nsec = v9iattr.mtime_nsec;
1209 } else {
1210 times[1].tv_nsec = UTIME_NOW;
1212 } else {
1213 times[1].tv_nsec = UTIME_OMIT;
1215 err = v9fs_co_utimensat(pdu, &fidp->path, times);
1216 if (err < 0) {
1217 goto out;
1221 * If the only valid entry in iattr is ctime we can call
1222 * chown(-1,-1) to update the ctime of the file
1224 if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) ||
1225 ((v9iattr.valid & P9_ATTR_CTIME)
1226 && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) {
1227 if (!(v9iattr.valid & P9_ATTR_UID)) {
1228 v9iattr.uid = -1;
1230 if (!(v9iattr.valid & P9_ATTR_GID)) {
1231 v9iattr.gid = -1;
1233 err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid,
1234 v9iattr.gid);
1235 if (err < 0) {
1236 goto out;
1239 if (v9iattr.valid & (P9_ATTR_SIZE)) {
1240 err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size);
1241 if (err < 0) {
1242 goto out;
1245 err = offset;
1246 out:
1247 put_fid(pdu, fidp);
1248 out_nofid:
1249 pdu_complete(pdu, err);
1252 static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids)
1254 int i;
1255 ssize_t err;
1256 size_t offset = 7;
1258 err = pdu_marshal(pdu, offset, "w", nwnames);
1259 if (err < 0) {
1260 return err;
1262 offset += err;
1263 for (i = 0; i < nwnames; i++) {
1264 err = pdu_marshal(pdu, offset, "Q", &qids[i]);
1265 if (err < 0) {
1266 return err;
1268 offset += err;
1270 return offset;
1273 static bool name_is_illegal(const char *name)
1275 return !*name || strchr(name, '/') != NULL;
1278 static bool not_same_qid(const V9fsQID *qid1, const V9fsQID *qid2)
1280 return
1281 qid1->type != qid2->type ||
1282 qid1->version != qid2->version ||
1283 qid1->path != qid2->path;
1286 static void v9fs_walk(void *opaque)
1288 int name_idx;
1289 V9fsQID *qids = NULL;
1290 int i, err = 0;
1291 V9fsPath dpath, path;
1292 uint16_t nwnames;
1293 struct stat stbuf;
1294 size_t offset = 7;
1295 int32_t fid, newfid;
1296 V9fsString *wnames = NULL;
1297 V9fsFidState *fidp;
1298 V9fsFidState *newfidp = NULL;
1299 V9fsPDU *pdu = opaque;
1300 V9fsState *s = pdu->s;
1301 V9fsQID qid;
1303 err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames);
1304 if (err < 0) {
1305 pdu_complete(pdu, err);
1306 return ;
1308 offset += err;
1310 trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames);
1312 if (nwnames && nwnames <= P9_MAXWELEM) {
1313 wnames = g_malloc0(sizeof(wnames[0]) * nwnames);
1314 qids = g_malloc0(sizeof(qids[0]) * nwnames);
1315 for (i = 0; i < nwnames; i++) {
1316 err = pdu_unmarshal(pdu, offset, "s", &wnames[i]);
1317 if (err < 0) {
1318 goto out_nofid;
1320 if (name_is_illegal(wnames[i].data)) {
1321 err = -ENOENT;
1322 goto out_nofid;
1324 offset += err;
1326 } else if (nwnames > P9_MAXWELEM) {
1327 err = -EINVAL;
1328 goto out_nofid;
1330 fidp = get_fid(pdu, fid);
1331 if (fidp == NULL) {
1332 err = -ENOENT;
1333 goto out_nofid;
1336 v9fs_path_init(&dpath);
1337 v9fs_path_init(&path);
1339 err = fid_to_qid(pdu, fidp, &qid);
1340 if (err < 0) {
1341 goto out;
1345 * Both dpath and path initially poin to fidp.
1346 * Needed to handle request with nwnames == 0
1348 v9fs_path_copy(&dpath, &fidp->path);
1349 v9fs_path_copy(&path, &fidp->path);
1350 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1351 if (not_same_qid(&pdu->s->root_qid, &qid) ||
1352 strcmp("..", wnames[name_idx].data)) {
1353 err = v9fs_co_name_to_path(pdu, &dpath, wnames[name_idx].data,
1354 &path);
1355 if (err < 0) {
1356 goto out;
1359 err = v9fs_co_lstat(pdu, &path, &stbuf);
1360 if (err < 0) {
1361 goto out;
1363 stat_to_qid(&stbuf, &qid);
1364 v9fs_path_copy(&dpath, &path);
1366 memcpy(&qids[name_idx], &qid, sizeof(qid));
1368 if (fid == newfid) {
1369 BUG_ON(fidp->fid_type != P9_FID_NONE);
1370 v9fs_path_copy(&fidp->path, &path);
1371 } else {
1372 newfidp = alloc_fid(s, newfid);
1373 if (newfidp == NULL) {
1374 err = -EINVAL;
1375 goto out;
1377 newfidp->uid = fidp->uid;
1378 v9fs_path_copy(&newfidp->path, &path);
1380 err = v9fs_walk_marshal(pdu, nwnames, qids);
1381 trace_v9fs_walk_return(pdu->tag, pdu->id, nwnames, qids);
1382 out:
1383 put_fid(pdu, fidp);
1384 if (newfidp) {
1385 put_fid(pdu, newfidp);
1387 v9fs_path_free(&dpath);
1388 v9fs_path_free(&path);
1389 out_nofid:
1390 pdu_complete(pdu, err);
1391 if (nwnames && nwnames <= P9_MAXWELEM) {
1392 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1393 v9fs_string_free(&wnames[name_idx]);
1395 g_free(wnames);
1396 g_free(qids);
1400 static int32_t get_iounit(V9fsPDU *pdu, V9fsPath *path)
1402 struct statfs stbuf;
1403 int32_t iounit = 0;
1404 V9fsState *s = pdu->s;
1407 * iounit should be multiples of f_bsize (host filesystem block size
1408 * and as well as less than (client msize - P9_IOHDRSZ))
1410 if (!v9fs_co_statfs(pdu, path, &stbuf)) {
1411 iounit = stbuf.f_bsize;
1412 iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
1414 if (!iounit) {
1415 iounit = s->msize - P9_IOHDRSZ;
1417 return iounit;
1420 static void v9fs_open(void *opaque)
1422 int flags;
1423 int32_t fid;
1424 int32_t mode;
1425 V9fsQID qid;
1426 int iounit = 0;
1427 ssize_t err = 0;
1428 size_t offset = 7;
1429 struct stat stbuf;
1430 V9fsFidState *fidp;
1431 V9fsPDU *pdu = opaque;
1432 V9fsState *s = pdu->s;
1434 if (s->proto_version == V9FS_PROTO_2000L) {
1435 err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode);
1436 } else {
1437 uint8_t modebyte;
1438 err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte);
1439 mode = modebyte;
1441 if (err < 0) {
1442 goto out_nofid;
1444 trace_v9fs_open(pdu->tag, pdu->id, fid, mode);
1446 fidp = get_fid(pdu, fid);
1447 if (fidp == NULL) {
1448 err = -ENOENT;
1449 goto out_nofid;
1451 BUG_ON(fidp->fid_type != P9_FID_NONE);
1453 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1454 if (err < 0) {
1455 goto out;
1457 stat_to_qid(&stbuf, &qid);
1458 if (S_ISDIR(stbuf.st_mode)) {
1459 err = v9fs_co_opendir(pdu, fidp);
1460 if (err < 0) {
1461 goto out;
1463 fidp->fid_type = P9_FID_DIR;
1464 err = pdu_marshal(pdu, offset, "Qd", &qid, 0);
1465 if (err < 0) {
1466 goto out;
1468 err += offset;
1469 } else {
1470 if (s->proto_version == V9FS_PROTO_2000L) {
1471 flags = get_dotl_openflags(s, mode);
1472 } else {
1473 flags = omode_to_uflags(mode);
1475 if (is_ro_export(&s->ctx)) {
1476 if (mode & O_WRONLY || mode & O_RDWR ||
1477 mode & O_APPEND || mode & O_TRUNC) {
1478 err = -EROFS;
1479 goto out;
1482 err = v9fs_co_open(pdu, fidp, flags);
1483 if (err < 0) {
1484 goto out;
1486 fidp->fid_type = P9_FID_FILE;
1487 fidp->open_flags = flags;
1488 if (flags & O_EXCL) {
1490 * We let the host file system do O_EXCL check
1491 * We should not reclaim such fd
1493 fidp->flags |= FID_NON_RECLAIMABLE;
1495 iounit = get_iounit(pdu, &fidp->path);
1496 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1497 if (err < 0) {
1498 goto out;
1500 err += offset;
1502 trace_v9fs_open_return(pdu->tag, pdu->id,
1503 qid.type, qid.version, qid.path, iounit);
1504 out:
1505 put_fid(pdu, fidp);
1506 out_nofid:
1507 pdu_complete(pdu, err);
1510 static void v9fs_lcreate(void *opaque)
1512 int32_t dfid, flags, mode;
1513 gid_t gid;
1514 ssize_t err = 0;
1515 ssize_t offset = 7;
1516 V9fsString name;
1517 V9fsFidState *fidp;
1518 struct stat stbuf;
1519 V9fsQID qid;
1520 int32_t iounit;
1521 V9fsPDU *pdu = opaque;
1523 v9fs_string_init(&name);
1524 err = pdu_unmarshal(pdu, offset, "dsddd", &dfid,
1525 &name, &flags, &mode, &gid);
1526 if (err < 0) {
1527 goto out_nofid;
1529 trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid);
1531 if (name_is_illegal(name.data)) {
1532 err = -ENOENT;
1533 goto out_nofid;
1536 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
1537 err = -EEXIST;
1538 goto out_nofid;
1541 fidp = get_fid(pdu, dfid);
1542 if (fidp == NULL) {
1543 err = -ENOENT;
1544 goto out_nofid;
1547 flags = get_dotl_openflags(pdu->s, flags);
1548 err = v9fs_co_open2(pdu, fidp, &name, gid,
1549 flags | O_CREAT, mode, &stbuf);
1550 if (err < 0) {
1551 goto out;
1553 fidp->fid_type = P9_FID_FILE;
1554 fidp->open_flags = flags;
1555 if (flags & O_EXCL) {
1557 * We let the host file system do O_EXCL check
1558 * We should not reclaim such fd
1560 fidp->flags |= FID_NON_RECLAIMABLE;
1562 iounit = get_iounit(pdu, &fidp->path);
1563 stat_to_qid(&stbuf, &qid);
1564 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1565 if (err < 0) {
1566 goto out;
1568 err += offset;
1569 trace_v9fs_lcreate_return(pdu->tag, pdu->id,
1570 qid.type, qid.version, qid.path, iounit);
1571 out:
1572 put_fid(pdu, fidp);
1573 out_nofid:
1574 pdu_complete(pdu, err);
1575 v9fs_string_free(&name);
1578 static void v9fs_fsync(void *opaque)
1580 int err;
1581 int32_t fid;
1582 int datasync;
1583 size_t offset = 7;
1584 V9fsFidState *fidp;
1585 V9fsPDU *pdu = opaque;
1587 err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
1588 if (err < 0) {
1589 goto out_nofid;
1591 trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync);
1593 fidp = get_fid(pdu, fid);
1594 if (fidp == NULL) {
1595 err = -ENOENT;
1596 goto out_nofid;
1598 err = v9fs_co_fsync(pdu, fidp, datasync);
1599 if (!err) {
1600 err = offset;
1602 put_fid(pdu, fidp);
1603 out_nofid:
1604 pdu_complete(pdu, err);
1607 static void v9fs_clunk(void *opaque)
1609 int err;
1610 int32_t fid;
1611 size_t offset = 7;
1612 V9fsFidState *fidp;
1613 V9fsPDU *pdu = opaque;
1614 V9fsState *s = pdu->s;
1616 err = pdu_unmarshal(pdu, offset, "d", &fid);
1617 if (err < 0) {
1618 goto out_nofid;
1620 trace_v9fs_clunk(pdu->tag, pdu->id, fid);
1622 fidp = clunk_fid(s, fid);
1623 if (fidp == NULL) {
1624 err = -ENOENT;
1625 goto out_nofid;
1628 * Bump the ref so that put_fid will
1629 * free the fid.
1631 fidp->ref++;
1632 err = put_fid(pdu, fidp);
1633 if (!err) {
1634 err = offset;
1636 out_nofid:
1637 pdu_complete(pdu, err);
1640 static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
1641 uint64_t off, uint32_t max_count)
1643 ssize_t err;
1644 size_t offset = 7;
1645 int read_count;
1646 int64_t xattr_len;
1647 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
1648 VirtQueueElement *elem = v->elems[pdu->idx];
1650 xattr_len = fidp->fs.xattr.len;
1651 read_count = xattr_len - off;
1652 if (read_count > max_count) {
1653 read_count = max_count;
1654 } else if (read_count < 0) {
1656 * read beyond XATTR value
1658 read_count = 0;
1660 err = pdu_marshal(pdu, offset, "d", read_count);
1661 if (err < 0) {
1662 return err;
1664 offset += err;
1666 err = v9fs_pack(elem->in_sg, elem->in_num, offset,
1667 ((char *)fidp->fs.xattr.value) + off,
1668 read_count);
1669 if (err < 0) {
1670 return err;
1672 offset += err;
1673 return offset;
1676 static int v9fs_do_readdir_with_stat(V9fsPDU *pdu,
1677 V9fsFidState *fidp, uint32_t max_count)
1679 V9fsPath path;
1680 V9fsStat v9stat;
1681 int len, err = 0;
1682 int32_t count = 0;
1683 struct stat stbuf;
1684 off_t saved_dir_pos;
1685 struct dirent *dent;
1687 /* save the directory position */
1688 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1689 if (saved_dir_pos < 0) {
1690 return saved_dir_pos;
1693 while (1) {
1694 v9fs_path_init(&path);
1696 v9fs_readdir_lock(&fidp->fs.dir);
1698 err = v9fs_co_readdir(pdu, fidp, &dent);
1699 if (err || !dent) {
1700 break;
1702 err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path);
1703 if (err < 0) {
1704 break;
1706 err = v9fs_co_lstat(pdu, &path, &stbuf);
1707 if (err < 0) {
1708 break;
1710 err = stat_to_v9stat(pdu, &path, &stbuf, &v9stat);
1711 if (err < 0) {
1712 break;
1714 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1715 len = pdu_marshal(pdu, 11 + count, "S", &v9stat);
1717 v9fs_readdir_unlock(&fidp->fs.dir);
1719 if ((len != (v9stat.size + 2)) || ((count + len) > max_count)) {
1720 /* Ran out of buffer. Set dir back to old position and return */
1721 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1722 v9fs_stat_free(&v9stat);
1723 v9fs_path_free(&path);
1724 return count;
1726 count += len;
1727 v9fs_stat_free(&v9stat);
1728 v9fs_path_free(&path);
1729 saved_dir_pos = dent->d_off;
1732 v9fs_readdir_unlock(&fidp->fs.dir);
1734 v9fs_path_free(&path);
1735 if (err < 0) {
1736 return err;
1738 return count;
1742 * Create a QEMUIOVector for a sub-region of PDU iovecs
1744 * @qiov: uninitialized QEMUIOVector
1745 * @skip: number of bytes to skip from beginning of PDU
1746 * @size: number of bytes to include
1747 * @is_write: true - write, false - read
1749 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1750 * with qemu_iovec_destroy().
1752 static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu,
1753 size_t skip, size_t size,
1754 bool is_write)
1756 QEMUIOVector elem;
1757 struct iovec *iov;
1758 unsigned int niov;
1760 virtio_init_iov_from_pdu(pdu, &iov, &niov, is_write);
1762 qemu_iovec_init_external(&elem, iov, niov);
1763 qemu_iovec_init(qiov, niov);
1764 qemu_iovec_concat(qiov, &elem, skip, size);
1767 static void v9fs_read(void *opaque)
1769 int32_t fid;
1770 uint64_t off;
1771 ssize_t err = 0;
1772 int32_t count = 0;
1773 size_t offset = 7;
1774 uint32_t max_count;
1775 V9fsFidState *fidp;
1776 V9fsPDU *pdu = opaque;
1777 V9fsState *s = pdu->s;
1779 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count);
1780 if (err < 0) {
1781 goto out_nofid;
1783 trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count);
1785 fidp = get_fid(pdu, fid);
1786 if (fidp == NULL) {
1787 err = -EINVAL;
1788 goto out_nofid;
1790 if (fidp->fid_type == P9_FID_DIR) {
1792 if (off == 0) {
1793 v9fs_co_rewinddir(pdu, fidp);
1795 count = v9fs_do_readdir_with_stat(pdu, fidp, max_count);
1796 if (count < 0) {
1797 err = count;
1798 goto out;
1800 err = pdu_marshal(pdu, offset, "d", count);
1801 if (err < 0) {
1802 goto out;
1804 err += offset + count;
1805 } else if (fidp->fid_type == P9_FID_FILE) {
1806 QEMUIOVector qiov_full;
1807 QEMUIOVector qiov;
1808 int32_t len;
1810 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, max_count, false);
1811 qemu_iovec_init(&qiov, qiov_full.niov);
1812 do {
1813 qemu_iovec_reset(&qiov);
1814 qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count);
1815 if (0) {
1816 print_sg(qiov.iov, qiov.niov);
1818 /* Loop in case of EINTR */
1819 do {
1820 len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off);
1821 if (len >= 0) {
1822 off += len;
1823 count += len;
1825 } while (len == -EINTR && !pdu->cancelled);
1826 if (len < 0) {
1827 /* IO error return the error */
1828 err = len;
1829 goto out_free_iovec;
1831 } while (count < max_count && len > 0);
1832 err = pdu_marshal(pdu, offset, "d", count);
1833 if (err < 0) {
1834 goto out_free_iovec;
1836 err += offset + count;
1837 out_free_iovec:
1838 qemu_iovec_destroy(&qiov);
1839 qemu_iovec_destroy(&qiov_full);
1840 } else if (fidp->fid_type == P9_FID_XATTR) {
1841 err = v9fs_xattr_read(s, pdu, fidp, off, max_count);
1842 } else {
1843 err = -EINVAL;
1845 trace_v9fs_read_return(pdu->tag, pdu->id, count, err);
1846 out:
1847 put_fid(pdu, fidp);
1848 out_nofid:
1849 pdu_complete(pdu, err);
1852 static size_t v9fs_readdir_data_size(V9fsString *name)
1855 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1856 * size of type (1) + size of name.size (2) + strlen(name.data)
1858 return 24 + v9fs_string_size(name);
1861 static int v9fs_do_readdir(V9fsPDU *pdu,
1862 V9fsFidState *fidp, int32_t max_count)
1864 size_t size;
1865 V9fsQID qid;
1866 V9fsString name;
1867 int len, err = 0;
1868 int32_t count = 0;
1869 off_t saved_dir_pos;
1870 struct dirent *dent;
1872 /* save the directory position */
1873 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1874 if (saved_dir_pos < 0) {
1875 return saved_dir_pos;
1878 while (1) {
1879 v9fs_readdir_lock(&fidp->fs.dir);
1881 err = v9fs_co_readdir(pdu, fidp, &dent);
1882 if (err || !dent) {
1883 break;
1885 v9fs_string_init(&name);
1886 v9fs_string_sprintf(&name, "%s", dent->d_name);
1887 if ((count + v9fs_readdir_data_size(&name)) > max_count) {
1888 v9fs_readdir_unlock(&fidp->fs.dir);
1890 /* Ran out of buffer. Set dir back to old position and return */
1891 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1892 v9fs_string_free(&name);
1893 return count;
1896 * Fill up just the path field of qid because the client uses
1897 * only that. To fill the entire qid structure we will have
1898 * to stat each dirent found, which is expensive
1900 size = MIN(sizeof(dent->d_ino), sizeof(qid.path));
1901 memcpy(&qid.path, &dent->d_ino, size);
1902 /* Fill the other fields with dummy values */
1903 qid.type = 0;
1904 qid.version = 0;
1906 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1907 len = pdu_marshal(pdu, 11 + count, "Qqbs",
1908 &qid, dent->d_off,
1909 dent->d_type, &name);
1911 v9fs_readdir_unlock(&fidp->fs.dir);
1913 if (len < 0) {
1914 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1915 v9fs_string_free(&name);
1916 return len;
1918 count += len;
1919 v9fs_string_free(&name);
1920 saved_dir_pos = dent->d_off;
1923 v9fs_readdir_unlock(&fidp->fs.dir);
1925 if (err < 0) {
1926 return err;
1928 return count;
1931 static void v9fs_readdir(void *opaque)
1933 int32_t fid;
1934 V9fsFidState *fidp;
1935 ssize_t retval = 0;
1936 size_t offset = 7;
1937 uint64_t initial_offset;
1938 int32_t count;
1939 uint32_t max_count;
1940 V9fsPDU *pdu = opaque;
1942 retval = pdu_unmarshal(pdu, offset, "dqd", &fid,
1943 &initial_offset, &max_count);
1944 if (retval < 0) {
1945 goto out_nofid;
1947 trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count);
1949 fidp = get_fid(pdu, fid);
1950 if (fidp == NULL) {
1951 retval = -EINVAL;
1952 goto out_nofid;
1954 if (!fidp->fs.dir.stream) {
1955 retval = -EINVAL;
1956 goto out;
1958 if (initial_offset == 0) {
1959 v9fs_co_rewinddir(pdu, fidp);
1960 } else {
1961 v9fs_co_seekdir(pdu, fidp, initial_offset);
1963 count = v9fs_do_readdir(pdu, fidp, max_count);
1964 if (count < 0) {
1965 retval = count;
1966 goto out;
1968 retval = pdu_marshal(pdu, offset, "d", count);
1969 if (retval < 0) {
1970 goto out;
1972 retval += count + offset;
1973 trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval);
1974 out:
1975 put_fid(pdu, fidp);
1976 out_nofid:
1977 pdu_complete(pdu, retval);
1980 static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
1981 uint64_t off, uint32_t count,
1982 struct iovec *sg, int cnt)
1984 int i, to_copy;
1985 ssize_t err = 0;
1986 int write_count;
1987 int64_t xattr_len;
1988 size_t offset = 7;
1991 xattr_len = fidp->fs.xattr.len;
1992 write_count = xattr_len - off;
1993 if (write_count > count) {
1994 write_count = count;
1995 } else if (write_count < 0) {
1997 * write beyond XATTR value len specified in
1998 * xattrcreate
2000 err = -ENOSPC;
2001 goto out;
2003 err = pdu_marshal(pdu, offset, "d", write_count);
2004 if (err < 0) {
2005 return err;
2007 err += offset;
2008 fidp->fs.xattr.copied_len += write_count;
2010 * Now copy the content from sg list
2012 for (i = 0; i < cnt; i++) {
2013 if (write_count > sg[i].iov_len) {
2014 to_copy = sg[i].iov_len;
2015 } else {
2016 to_copy = write_count;
2018 memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy);
2019 /* updating vs->off since we are not using below */
2020 off += to_copy;
2021 write_count -= to_copy;
2023 out:
2024 return err;
2027 static void v9fs_write(void *opaque)
2029 ssize_t err;
2030 int32_t fid;
2031 uint64_t off;
2032 uint32_t count;
2033 int32_t len = 0;
2034 int32_t total = 0;
2035 size_t offset = 7;
2036 V9fsFidState *fidp;
2037 V9fsPDU *pdu = opaque;
2038 V9fsState *s = pdu->s;
2039 QEMUIOVector qiov_full;
2040 QEMUIOVector qiov;
2042 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count);
2043 if (err < 0) {
2044 pdu_complete(pdu, err);
2045 return;
2047 offset += err;
2048 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, count, true);
2049 trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov);
2051 fidp = get_fid(pdu, fid);
2052 if (fidp == NULL) {
2053 err = -EINVAL;
2054 goto out_nofid;
2056 if (fidp->fid_type == P9_FID_FILE) {
2057 if (fidp->fs.fd == -1) {
2058 err = -EINVAL;
2059 goto out;
2061 } else if (fidp->fid_type == P9_FID_XATTR) {
2063 * setxattr operation
2065 err = v9fs_xattr_write(s, pdu, fidp, off, count,
2066 qiov_full.iov, qiov_full.niov);
2067 goto out;
2068 } else {
2069 err = -EINVAL;
2070 goto out;
2072 qemu_iovec_init(&qiov, qiov_full.niov);
2073 do {
2074 qemu_iovec_reset(&qiov);
2075 qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total);
2076 if (0) {
2077 print_sg(qiov.iov, qiov.niov);
2079 /* Loop in case of EINTR */
2080 do {
2081 len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off);
2082 if (len >= 0) {
2083 off += len;
2084 total += len;
2086 } while (len == -EINTR && !pdu->cancelled);
2087 if (len < 0) {
2088 /* IO error return the error */
2089 err = len;
2090 goto out_qiov;
2092 } while (total < count && len > 0);
2094 offset = 7;
2095 err = pdu_marshal(pdu, offset, "d", total);
2096 if (err < 0) {
2097 goto out;
2099 err += offset;
2100 trace_v9fs_write_return(pdu->tag, pdu->id, total, err);
2101 out_qiov:
2102 qemu_iovec_destroy(&qiov);
2103 out:
2104 put_fid(pdu, fidp);
2105 out_nofid:
2106 qemu_iovec_destroy(&qiov_full);
2107 pdu_complete(pdu, err);
2110 static void v9fs_create(void *opaque)
2112 int32_t fid;
2113 int err = 0;
2114 size_t offset = 7;
2115 V9fsFidState *fidp;
2116 V9fsQID qid;
2117 int32_t perm;
2118 int8_t mode;
2119 V9fsPath path;
2120 struct stat stbuf;
2121 V9fsString name;
2122 V9fsString extension;
2123 int iounit;
2124 V9fsPDU *pdu = opaque;
2126 v9fs_path_init(&path);
2127 v9fs_string_init(&name);
2128 v9fs_string_init(&extension);
2129 err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name,
2130 &perm, &mode, &extension);
2131 if (err < 0) {
2132 goto out_nofid;
2134 trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode);
2136 if (name_is_illegal(name.data)) {
2137 err = -ENOENT;
2138 goto out_nofid;
2141 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2142 err = -EEXIST;
2143 goto out_nofid;
2146 fidp = get_fid(pdu, fid);
2147 if (fidp == NULL) {
2148 err = -EINVAL;
2149 goto out_nofid;
2151 if (perm & P9_STAT_MODE_DIR) {
2152 err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777,
2153 fidp->uid, -1, &stbuf);
2154 if (err < 0) {
2155 goto out;
2157 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2158 if (err < 0) {
2159 goto out;
2161 v9fs_path_copy(&fidp->path, &path);
2162 err = v9fs_co_opendir(pdu, fidp);
2163 if (err < 0) {
2164 goto out;
2166 fidp->fid_type = P9_FID_DIR;
2167 } else if (perm & P9_STAT_MODE_SYMLINK) {
2168 err = v9fs_co_symlink(pdu, fidp, &name,
2169 extension.data, -1 , &stbuf);
2170 if (err < 0) {
2171 goto out;
2173 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2174 if (err < 0) {
2175 goto out;
2177 v9fs_path_copy(&fidp->path, &path);
2178 } else if (perm & P9_STAT_MODE_LINK) {
2179 int32_t ofid = atoi(extension.data);
2180 V9fsFidState *ofidp = get_fid(pdu, ofid);
2181 if (ofidp == NULL) {
2182 err = -EINVAL;
2183 goto out;
2185 err = v9fs_co_link(pdu, ofidp, fidp, &name);
2186 put_fid(pdu, ofidp);
2187 if (err < 0) {
2188 goto out;
2190 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2191 if (err < 0) {
2192 fidp->fid_type = P9_FID_NONE;
2193 goto out;
2195 v9fs_path_copy(&fidp->path, &path);
2196 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
2197 if (err < 0) {
2198 fidp->fid_type = P9_FID_NONE;
2199 goto out;
2201 } else if (perm & P9_STAT_MODE_DEVICE) {
2202 char ctype;
2203 uint32_t major, minor;
2204 mode_t nmode = 0;
2206 if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) {
2207 err = -errno;
2208 goto out;
2211 switch (ctype) {
2212 case 'c':
2213 nmode = S_IFCHR;
2214 break;
2215 case 'b':
2216 nmode = S_IFBLK;
2217 break;
2218 default:
2219 err = -EIO;
2220 goto out;
2223 nmode |= perm & 0777;
2224 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2225 makedev(major, minor), nmode, &stbuf);
2226 if (err < 0) {
2227 goto out;
2229 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2230 if (err < 0) {
2231 goto out;
2233 v9fs_path_copy(&fidp->path, &path);
2234 } else if (perm & P9_STAT_MODE_NAMED_PIPE) {
2235 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2236 0, S_IFIFO | (perm & 0777), &stbuf);
2237 if (err < 0) {
2238 goto out;
2240 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2241 if (err < 0) {
2242 goto out;
2244 v9fs_path_copy(&fidp->path, &path);
2245 } else if (perm & P9_STAT_MODE_SOCKET) {
2246 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2247 0, S_IFSOCK | (perm & 0777), &stbuf);
2248 if (err < 0) {
2249 goto out;
2251 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2252 if (err < 0) {
2253 goto out;
2255 v9fs_path_copy(&fidp->path, &path);
2256 } else {
2257 err = v9fs_co_open2(pdu, fidp, &name, -1,
2258 omode_to_uflags(mode)|O_CREAT, perm, &stbuf);
2259 if (err < 0) {
2260 goto out;
2262 fidp->fid_type = P9_FID_FILE;
2263 fidp->open_flags = omode_to_uflags(mode);
2264 if (fidp->open_flags & O_EXCL) {
2266 * We let the host file system do O_EXCL check
2267 * We should not reclaim such fd
2269 fidp->flags |= FID_NON_RECLAIMABLE;
2272 iounit = get_iounit(pdu, &fidp->path);
2273 stat_to_qid(&stbuf, &qid);
2274 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
2275 if (err < 0) {
2276 goto out;
2278 err += offset;
2279 trace_v9fs_create_return(pdu->tag, pdu->id,
2280 qid.type, qid.version, qid.path, iounit);
2281 out:
2282 put_fid(pdu, fidp);
2283 out_nofid:
2284 pdu_complete(pdu, err);
2285 v9fs_string_free(&name);
2286 v9fs_string_free(&extension);
2287 v9fs_path_free(&path);
2290 static void v9fs_symlink(void *opaque)
2292 V9fsPDU *pdu = opaque;
2293 V9fsString name;
2294 V9fsString symname;
2295 V9fsFidState *dfidp;
2296 V9fsQID qid;
2297 struct stat stbuf;
2298 int32_t dfid;
2299 int err = 0;
2300 gid_t gid;
2301 size_t offset = 7;
2303 v9fs_string_init(&name);
2304 v9fs_string_init(&symname);
2305 err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid);
2306 if (err < 0) {
2307 goto out_nofid;
2309 trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid);
2311 if (name_is_illegal(name.data)) {
2312 err = -ENOENT;
2313 goto out_nofid;
2316 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2317 err = -EEXIST;
2318 goto out_nofid;
2321 dfidp = get_fid(pdu, dfid);
2322 if (dfidp == NULL) {
2323 err = -EINVAL;
2324 goto out_nofid;
2326 err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf);
2327 if (err < 0) {
2328 goto out;
2330 stat_to_qid(&stbuf, &qid);
2331 err = pdu_marshal(pdu, offset, "Q", &qid);
2332 if (err < 0) {
2333 goto out;
2335 err += offset;
2336 trace_v9fs_symlink_return(pdu->tag, pdu->id,
2337 qid.type, qid.version, qid.path);
2338 out:
2339 put_fid(pdu, dfidp);
2340 out_nofid:
2341 pdu_complete(pdu, err);
2342 v9fs_string_free(&name);
2343 v9fs_string_free(&symname);
2346 static void v9fs_flush(void *opaque)
2348 ssize_t err;
2349 int16_t tag;
2350 size_t offset = 7;
2351 V9fsPDU *cancel_pdu;
2352 V9fsPDU *pdu = opaque;
2353 V9fsState *s = pdu->s;
2355 err = pdu_unmarshal(pdu, offset, "w", &tag);
2356 if (err < 0) {
2357 pdu_complete(pdu, err);
2358 return;
2360 trace_v9fs_flush(pdu->tag, pdu->id, tag);
2362 QLIST_FOREACH(cancel_pdu, &s->active_list, next) {
2363 if (cancel_pdu->tag == tag) {
2364 break;
2367 if (cancel_pdu) {
2368 cancel_pdu->cancelled = 1;
2370 * Wait for pdu to complete.
2372 qemu_co_queue_wait(&cancel_pdu->complete);
2373 cancel_pdu->cancelled = 0;
2374 pdu_free(cancel_pdu);
2376 pdu_complete(pdu, 7);
2379 static void v9fs_link(void *opaque)
2381 V9fsPDU *pdu = opaque;
2382 int32_t dfid, oldfid;
2383 V9fsFidState *dfidp, *oldfidp;
2384 V9fsString name;
2385 size_t offset = 7;
2386 int err = 0;
2388 v9fs_string_init(&name);
2389 err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
2390 if (err < 0) {
2391 goto out_nofid;
2393 trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data);
2395 if (name_is_illegal(name.data)) {
2396 err = -ENOENT;
2397 goto out_nofid;
2400 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2401 err = -EEXIST;
2402 goto out_nofid;
2405 dfidp = get_fid(pdu, dfid);
2406 if (dfidp == NULL) {
2407 err = -ENOENT;
2408 goto out_nofid;
2411 oldfidp = get_fid(pdu, oldfid);
2412 if (oldfidp == NULL) {
2413 err = -ENOENT;
2414 goto out;
2416 err = v9fs_co_link(pdu, oldfidp, dfidp, &name);
2417 if (!err) {
2418 err = offset;
2420 out:
2421 put_fid(pdu, dfidp);
2422 out_nofid:
2423 v9fs_string_free(&name);
2424 pdu_complete(pdu, err);
2427 /* Only works with path name based fid */
2428 static void v9fs_remove(void *opaque)
2430 int32_t fid;
2431 int err = 0;
2432 size_t offset = 7;
2433 V9fsFidState *fidp;
2434 V9fsPDU *pdu = opaque;
2436 err = pdu_unmarshal(pdu, offset, "d", &fid);
2437 if (err < 0) {
2438 goto out_nofid;
2440 trace_v9fs_remove(pdu->tag, pdu->id, fid);
2442 fidp = get_fid(pdu, fid);
2443 if (fidp == NULL) {
2444 err = -EINVAL;
2445 goto out_nofid;
2447 /* if fs driver is not path based, return EOPNOTSUPP */
2448 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2449 err = -EOPNOTSUPP;
2450 goto out_err;
2453 * IF the file is unlinked, we cannot reopen
2454 * the file later. So don't reclaim fd
2456 err = v9fs_mark_fids_unreclaim(pdu, &fidp->path);
2457 if (err < 0) {
2458 goto out_err;
2460 err = v9fs_co_remove(pdu, &fidp->path);
2461 if (!err) {
2462 err = offset;
2464 out_err:
2465 /* For TREMOVE we need to clunk the fid even on failed remove */
2466 clunk_fid(pdu->s, fidp->fid);
2467 put_fid(pdu, fidp);
2468 out_nofid:
2469 pdu_complete(pdu, err);
2472 static void v9fs_unlinkat(void *opaque)
2474 int err = 0;
2475 V9fsString name;
2476 int32_t dfid, flags;
2477 size_t offset = 7;
2478 V9fsPath path;
2479 V9fsFidState *dfidp;
2480 V9fsPDU *pdu = opaque;
2482 v9fs_string_init(&name);
2483 err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags);
2484 if (err < 0) {
2485 goto out_nofid;
2488 if (name_is_illegal(name.data)) {
2489 err = -ENOENT;
2490 goto out_nofid;
2493 if (!strcmp(".", name.data)) {
2494 err = -EINVAL;
2495 goto out_nofid;
2498 if (!strcmp("..", name.data)) {
2499 err = -ENOTEMPTY;
2500 goto out_nofid;
2503 dfidp = get_fid(pdu, dfid);
2504 if (dfidp == NULL) {
2505 err = -EINVAL;
2506 goto out_nofid;
2509 * IF the file is unlinked, we cannot reopen
2510 * the file later. So don't reclaim fd
2512 v9fs_path_init(&path);
2513 err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path);
2514 if (err < 0) {
2515 goto out_err;
2517 err = v9fs_mark_fids_unreclaim(pdu, &path);
2518 if (err < 0) {
2519 goto out_err;
2521 err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, flags);
2522 if (!err) {
2523 err = offset;
2525 out_err:
2526 put_fid(pdu, dfidp);
2527 v9fs_path_free(&path);
2528 out_nofid:
2529 pdu_complete(pdu, err);
2530 v9fs_string_free(&name);
2534 /* Only works with path name based fid */
2535 static int v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp,
2536 int32_t newdirfid, V9fsString *name)
2538 char *end;
2539 int err = 0;
2540 V9fsPath new_path;
2541 V9fsFidState *tfidp;
2542 V9fsState *s = pdu->s;
2543 V9fsFidState *dirfidp = NULL;
2544 char *old_name, *new_name;
2546 v9fs_path_init(&new_path);
2547 if (newdirfid != -1) {
2548 dirfidp = get_fid(pdu, newdirfid);
2549 if (dirfidp == NULL) {
2550 err = -ENOENT;
2551 goto out_nofid;
2553 BUG_ON(dirfidp->fid_type != P9_FID_NONE);
2554 v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path);
2555 } else {
2556 old_name = fidp->path.data;
2557 end = strrchr(old_name, '/');
2558 if (end) {
2559 end++;
2560 } else {
2561 end = old_name;
2563 new_name = g_malloc0(end - old_name + name->size + 1);
2564 strncat(new_name, old_name, end - old_name);
2565 strncat(new_name + (end - old_name), name->data, name->size);
2566 v9fs_co_name_to_path(pdu, NULL, new_name, &new_path);
2567 g_free(new_name);
2569 err = v9fs_co_rename(pdu, &fidp->path, &new_path);
2570 if (err < 0) {
2571 goto out;
2574 * Fixup fid's pointing to the old name to
2575 * start pointing to the new name
2577 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2578 if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) {
2579 /* replace the name */
2580 v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data));
2583 out:
2584 if (dirfidp) {
2585 put_fid(pdu, dirfidp);
2587 v9fs_path_free(&new_path);
2588 out_nofid:
2589 return err;
2592 /* Only works with path name based fid */
2593 static void v9fs_rename(void *opaque)
2595 int32_t fid;
2596 ssize_t err = 0;
2597 size_t offset = 7;
2598 V9fsString name;
2599 int32_t newdirfid;
2600 V9fsFidState *fidp;
2601 V9fsPDU *pdu = opaque;
2602 V9fsState *s = pdu->s;
2604 v9fs_string_init(&name);
2605 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name);
2606 if (err < 0) {
2607 goto out_nofid;
2610 if (name_is_illegal(name.data)) {
2611 err = -ENOENT;
2612 goto out_nofid;
2615 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2616 err = -EISDIR;
2617 goto out_nofid;
2620 fidp = get_fid(pdu, fid);
2621 if (fidp == NULL) {
2622 err = -ENOENT;
2623 goto out_nofid;
2625 BUG_ON(fidp->fid_type != P9_FID_NONE);
2626 /* if fs driver is not path based, return EOPNOTSUPP */
2627 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2628 err = -EOPNOTSUPP;
2629 goto out;
2631 v9fs_path_write_lock(s);
2632 err = v9fs_complete_rename(pdu, fidp, newdirfid, &name);
2633 v9fs_path_unlock(s);
2634 if (!err) {
2635 err = offset;
2637 out:
2638 put_fid(pdu, fidp);
2639 out_nofid:
2640 pdu_complete(pdu, err);
2641 v9fs_string_free(&name);
2644 static void v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir,
2645 V9fsString *old_name, V9fsPath *newdir,
2646 V9fsString *new_name)
2648 V9fsFidState *tfidp;
2649 V9fsPath oldpath, newpath;
2650 V9fsState *s = pdu->s;
2653 v9fs_path_init(&oldpath);
2654 v9fs_path_init(&newpath);
2655 v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath);
2656 v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath);
2659 * Fixup fid's pointing to the old name to
2660 * start pointing to the new name
2662 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2663 if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) {
2664 /* replace the name */
2665 v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data));
2668 v9fs_path_free(&oldpath);
2669 v9fs_path_free(&newpath);
2672 static int v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid,
2673 V9fsString *old_name, int32_t newdirfid,
2674 V9fsString *new_name)
2676 int err = 0;
2677 V9fsState *s = pdu->s;
2678 V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL;
2680 olddirfidp = get_fid(pdu, olddirfid);
2681 if (olddirfidp == NULL) {
2682 err = -ENOENT;
2683 goto out;
2685 if (newdirfid != -1) {
2686 newdirfidp = get_fid(pdu, newdirfid);
2687 if (newdirfidp == NULL) {
2688 err = -ENOENT;
2689 goto out;
2691 } else {
2692 newdirfidp = get_fid(pdu, olddirfid);
2695 err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name,
2696 &newdirfidp->path, new_name);
2697 if (err < 0) {
2698 goto out;
2700 if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
2701 /* Only for path based fid we need to do the below fixup */
2702 v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name,
2703 &newdirfidp->path, new_name);
2705 out:
2706 if (olddirfidp) {
2707 put_fid(pdu, olddirfidp);
2709 if (newdirfidp) {
2710 put_fid(pdu, newdirfidp);
2712 return err;
2715 static void v9fs_renameat(void *opaque)
2717 ssize_t err = 0;
2718 size_t offset = 7;
2719 V9fsPDU *pdu = opaque;
2720 V9fsState *s = pdu->s;
2721 int32_t olddirfid, newdirfid;
2722 V9fsString old_name, new_name;
2724 v9fs_string_init(&old_name);
2725 v9fs_string_init(&new_name);
2726 err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid,
2727 &old_name, &newdirfid, &new_name);
2728 if (err < 0) {
2729 goto out_err;
2732 if (name_is_illegal(old_name.data) || name_is_illegal(new_name.data)) {
2733 err = -ENOENT;
2734 goto out_err;
2737 if (!strcmp(".", old_name.data) || !strcmp("..", old_name.data) ||
2738 !strcmp(".", new_name.data) || !strcmp("..", new_name.data)) {
2739 err = -EISDIR;
2740 goto out_err;
2743 v9fs_path_write_lock(s);
2744 err = v9fs_complete_renameat(pdu, olddirfid,
2745 &old_name, newdirfid, &new_name);
2746 v9fs_path_unlock(s);
2747 if (!err) {
2748 err = offset;
2751 out_err:
2752 pdu_complete(pdu, err);
2753 v9fs_string_free(&old_name);
2754 v9fs_string_free(&new_name);
2757 static void v9fs_wstat(void *opaque)
2759 int32_t fid;
2760 int err = 0;
2761 int16_t unused;
2762 V9fsStat v9stat;
2763 size_t offset = 7;
2764 struct stat stbuf;
2765 V9fsFidState *fidp;
2766 V9fsPDU *pdu = opaque;
2768 v9fs_stat_init(&v9stat);
2769 err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat);
2770 if (err < 0) {
2771 goto out_nofid;
2773 trace_v9fs_wstat(pdu->tag, pdu->id, fid,
2774 v9stat.mode, v9stat.atime, v9stat.mtime);
2776 fidp = get_fid(pdu, fid);
2777 if (fidp == NULL) {
2778 err = -EINVAL;
2779 goto out_nofid;
2781 /* do we need to sync the file? */
2782 if (donttouch_stat(&v9stat)) {
2783 err = v9fs_co_fsync(pdu, fidp, 0);
2784 goto out;
2786 if (v9stat.mode != -1) {
2787 uint32_t v9_mode;
2788 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
2789 if (err < 0) {
2790 goto out;
2792 v9_mode = stat_to_v9mode(&stbuf);
2793 if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2794 (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2795 /* Attempting to change the type */
2796 err = -EIO;
2797 goto out;
2799 err = v9fs_co_chmod(pdu, &fidp->path,
2800 v9mode_to_mode(v9stat.mode,
2801 &v9stat.extension));
2802 if (err < 0) {
2803 goto out;
2806 if (v9stat.mtime != -1 || v9stat.atime != -1) {
2807 struct timespec times[2];
2808 if (v9stat.atime != -1) {
2809 times[0].tv_sec = v9stat.atime;
2810 times[0].tv_nsec = 0;
2811 } else {
2812 times[0].tv_nsec = UTIME_OMIT;
2814 if (v9stat.mtime != -1) {
2815 times[1].tv_sec = v9stat.mtime;
2816 times[1].tv_nsec = 0;
2817 } else {
2818 times[1].tv_nsec = UTIME_OMIT;
2820 err = v9fs_co_utimensat(pdu, &fidp->path, times);
2821 if (err < 0) {
2822 goto out;
2825 if (v9stat.n_gid != -1 || v9stat.n_uid != -1) {
2826 err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid);
2827 if (err < 0) {
2828 goto out;
2831 if (v9stat.name.size != 0) {
2832 err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name);
2833 if (err < 0) {
2834 goto out;
2837 if (v9stat.length != -1) {
2838 err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length);
2839 if (err < 0) {
2840 goto out;
2843 err = offset;
2844 out:
2845 put_fid(pdu, fidp);
2846 out_nofid:
2847 v9fs_stat_free(&v9stat);
2848 pdu_complete(pdu, err);
2851 static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf)
2853 uint32_t f_type;
2854 uint32_t f_bsize;
2855 uint64_t f_blocks;
2856 uint64_t f_bfree;
2857 uint64_t f_bavail;
2858 uint64_t f_files;
2859 uint64_t f_ffree;
2860 uint64_t fsid_val;
2861 uint32_t f_namelen;
2862 size_t offset = 7;
2863 int32_t bsize_factor;
2866 * compute bsize factor based on host file system block size
2867 * and client msize
2869 bsize_factor = (s->msize - P9_IOHDRSZ)/stbuf->f_bsize;
2870 if (!bsize_factor) {
2871 bsize_factor = 1;
2873 f_type = stbuf->f_type;
2874 f_bsize = stbuf->f_bsize;
2875 f_bsize *= bsize_factor;
2877 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2878 * adjust(divide) the number of blocks, free blocks and available
2879 * blocks by bsize factor
2881 f_blocks = stbuf->f_blocks/bsize_factor;
2882 f_bfree = stbuf->f_bfree/bsize_factor;
2883 f_bavail = stbuf->f_bavail/bsize_factor;
2884 f_files = stbuf->f_files;
2885 f_ffree = stbuf->f_ffree;
2886 fsid_val = (unsigned int) stbuf->f_fsid.__val[0] |
2887 (unsigned long long)stbuf->f_fsid.__val[1] << 32;
2888 f_namelen = stbuf->f_namelen;
2890 return pdu_marshal(pdu, offset, "ddqqqqqqd",
2891 f_type, f_bsize, f_blocks, f_bfree,
2892 f_bavail, f_files, f_ffree,
2893 fsid_val, f_namelen);
2896 static void v9fs_statfs(void *opaque)
2898 int32_t fid;
2899 ssize_t retval = 0;
2900 size_t offset = 7;
2901 V9fsFidState *fidp;
2902 struct statfs stbuf;
2903 V9fsPDU *pdu = opaque;
2904 V9fsState *s = pdu->s;
2906 retval = pdu_unmarshal(pdu, offset, "d", &fid);
2907 if (retval < 0) {
2908 goto out_nofid;
2910 fidp = get_fid(pdu, fid);
2911 if (fidp == NULL) {
2912 retval = -ENOENT;
2913 goto out_nofid;
2915 retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf);
2916 if (retval < 0) {
2917 goto out;
2919 retval = v9fs_fill_statfs(s, pdu, &stbuf);
2920 if (retval < 0) {
2921 goto out;
2923 retval += offset;
2924 out:
2925 put_fid(pdu, fidp);
2926 out_nofid:
2927 pdu_complete(pdu, retval);
2930 static void v9fs_mknod(void *opaque)
2933 int mode;
2934 gid_t gid;
2935 int32_t fid;
2936 V9fsQID qid;
2937 int err = 0;
2938 int major, minor;
2939 size_t offset = 7;
2940 V9fsString name;
2941 struct stat stbuf;
2942 V9fsFidState *fidp;
2943 V9fsPDU *pdu = opaque;
2945 v9fs_string_init(&name);
2946 err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode,
2947 &major, &minor, &gid);
2948 if (err < 0) {
2949 goto out_nofid;
2951 trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor);
2953 if (name_is_illegal(name.data)) {
2954 err = -ENOENT;
2955 goto out_nofid;
2958 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2959 err = -EEXIST;
2960 goto out_nofid;
2963 fidp = get_fid(pdu, fid);
2964 if (fidp == NULL) {
2965 err = -ENOENT;
2966 goto out_nofid;
2968 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid,
2969 makedev(major, minor), mode, &stbuf);
2970 if (err < 0) {
2971 goto out;
2973 stat_to_qid(&stbuf, &qid);
2974 err = pdu_marshal(pdu, offset, "Q", &qid);
2975 if (err < 0) {
2976 goto out;
2978 err += offset;
2979 trace_v9fs_mknod_return(pdu->tag, pdu->id,
2980 qid.type, qid.version, qid.path);
2981 out:
2982 put_fid(pdu, fidp);
2983 out_nofid:
2984 pdu_complete(pdu, err);
2985 v9fs_string_free(&name);
2989 * Implement posix byte range locking code
2990 * Server side handling of locking code is very simple, because 9p server in
2991 * QEMU can handle only one client. And most of the lock handling
2992 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
2993 * do any thing in * qemu 9p server side lock code path.
2994 * So when a TLOCK request comes, always return success
2996 static void v9fs_lock(void *opaque)
2998 int8_t status;
2999 V9fsFlock flock;
3000 size_t offset = 7;
3001 struct stat stbuf;
3002 V9fsFidState *fidp;
3003 int32_t fid, err = 0;
3004 V9fsPDU *pdu = opaque;
3006 status = P9_LOCK_ERROR;
3007 v9fs_string_init(&flock.client_id);
3008 err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type,
3009 &flock.flags, &flock.start, &flock.length,
3010 &flock.proc_id, &flock.client_id);
3011 if (err < 0) {
3012 goto out_nofid;
3014 trace_v9fs_lock(pdu->tag, pdu->id, fid,
3015 flock.type, flock.start, flock.length);
3018 /* We support only block flag now (that too ignored currently) */
3019 if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) {
3020 err = -EINVAL;
3021 goto out_nofid;
3023 fidp = get_fid(pdu, fid);
3024 if (fidp == NULL) {
3025 err = -ENOENT;
3026 goto out_nofid;
3028 err = v9fs_co_fstat(pdu, fidp, &stbuf);
3029 if (err < 0) {
3030 goto out;
3032 status = P9_LOCK_SUCCESS;
3033 out:
3034 put_fid(pdu, fidp);
3035 out_nofid:
3036 err = pdu_marshal(pdu, offset, "b", status);
3037 if (err > 0) {
3038 err += offset;
3040 trace_v9fs_lock_return(pdu->tag, pdu->id, status);
3041 pdu_complete(pdu, err);
3042 v9fs_string_free(&flock.client_id);
3046 * When a TGETLOCK request comes, always return success because all lock
3047 * handling is done by client's VFS layer.
3049 static void v9fs_getlock(void *opaque)
3051 size_t offset = 7;
3052 struct stat stbuf;
3053 V9fsFidState *fidp;
3054 V9fsGetlock glock;
3055 int32_t fid, err = 0;
3056 V9fsPDU *pdu = opaque;
3058 v9fs_string_init(&glock.client_id);
3059 err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type,
3060 &glock.start, &glock.length, &glock.proc_id,
3061 &glock.client_id);
3062 if (err < 0) {
3063 goto out_nofid;
3065 trace_v9fs_getlock(pdu->tag, pdu->id, fid,
3066 glock.type, glock.start, glock.length);
3068 fidp = get_fid(pdu, fid);
3069 if (fidp == NULL) {
3070 err = -ENOENT;
3071 goto out_nofid;
3073 err = v9fs_co_fstat(pdu, fidp, &stbuf);
3074 if (err < 0) {
3075 goto out;
3077 glock.type = P9_LOCK_TYPE_UNLCK;
3078 err = pdu_marshal(pdu, offset, "bqqds", glock.type,
3079 glock.start, glock.length, glock.proc_id,
3080 &glock.client_id);
3081 if (err < 0) {
3082 goto out;
3084 err += offset;
3085 trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start,
3086 glock.length, glock.proc_id);
3087 out:
3088 put_fid(pdu, fidp);
3089 out_nofid:
3090 pdu_complete(pdu, err);
3091 v9fs_string_free(&glock.client_id);
3094 static void v9fs_mkdir(void *opaque)
3096 V9fsPDU *pdu = opaque;
3097 size_t offset = 7;
3098 int32_t fid;
3099 struct stat stbuf;
3100 V9fsQID qid;
3101 V9fsString name;
3102 V9fsFidState *fidp;
3103 gid_t gid;
3104 int mode;
3105 int err = 0;
3107 v9fs_string_init(&name);
3108 err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid);
3109 if (err < 0) {
3110 goto out_nofid;
3112 trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid);
3114 if (name_is_illegal(name.data)) {
3115 err = -ENOENT;
3116 goto out_nofid;
3119 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3120 err = -EEXIST;
3121 goto out_nofid;
3124 fidp = get_fid(pdu, fid);
3125 if (fidp == NULL) {
3126 err = -ENOENT;
3127 goto out_nofid;
3129 err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf);
3130 if (err < 0) {
3131 goto out;
3133 stat_to_qid(&stbuf, &qid);
3134 err = pdu_marshal(pdu, offset, "Q", &qid);
3135 if (err < 0) {
3136 goto out;
3138 err += offset;
3139 trace_v9fs_mkdir_return(pdu->tag, pdu->id,
3140 qid.type, qid.version, qid.path, err);
3141 out:
3142 put_fid(pdu, fidp);
3143 out_nofid:
3144 pdu_complete(pdu, err);
3145 v9fs_string_free(&name);
3148 static void v9fs_xattrwalk(void *opaque)
3150 int64_t size;
3151 V9fsString name;
3152 ssize_t err = 0;
3153 size_t offset = 7;
3154 int32_t fid, newfid;
3155 V9fsFidState *file_fidp;
3156 V9fsFidState *xattr_fidp = NULL;
3157 V9fsPDU *pdu = opaque;
3158 V9fsState *s = pdu->s;
3160 v9fs_string_init(&name);
3161 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name);
3162 if (err < 0) {
3163 goto out_nofid;
3165 trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data);
3167 file_fidp = get_fid(pdu, fid);
3168 if (file_fidp == NULL) {
3169 err = -ENOENT;
3170 goto out_nofid;
3172 xattr_fidp = alloc_fid(s, newfid);
3173 if (xattr_fidp == NULL) {
3174 err = -EINVAL;
3175 goto out;
3177 v9fs_path_copy(&xattr_fidp->path, &file_fidp->path);
3178 if (!v9fs_string_size(&name)) {
3180 * listxattr request. Get the size first
3182 size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0);
3183 if (size < 0) {
3184 err = size;
3185 clunk_fid(s, xattr_fidp->fid);
3186 goto out;
3189 * Read the xattr value
3191 xattr_fidp->fs.xattr.len = size;
3192 xattr_fidp->fid_type = P9_FID_XATTR;
3193 xattr_fidp->fs.xattr.copied_len = -1;
3194 if (size) {
3195 xattr_fidp->fs.xattr.value = g_malloc(size);
3196 err = v9fs_co_llistxattr(pdu, &xattr_fidp->path,
3197 xattr_fidp->fs.xattr.value,
3198 xattr_fidp->fs.xattr.len);
3199 if (err < 0) {
3200 clunk_fid(s, xattr_fidp->fid);
3201 goto out;
3204 err = pdu_marshal(pdu, offset, "q", size);
3205 if (err < 0) {
3206 goto out;
3208 err += offset;
3209 } else {
3211 * specific xattr fid. We check for xattr
3212 * presence also collect the xattr size
3214 size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3215 &name, NULL, 0);
3216 if (size < 0) {
3217 err = size;
3218 clunk_fid(s, xattr_fidp->fid);
3219 goto out;
3222 * Read the xattr value
3224 xattr_fidp->fs.xattr.len = size;
3225 xattr_fidp->fid_type = P9_FID_XATTR;
3226 xattr_fidp->fs.xattr.copied_len = -1;
3227 if (size) {
3228 xattr_fidp->fs.xattr.value = g_malloc(size);
3229 err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3230 &name, xattr_fidp->fs.xattr.value,
3231 xattr_fidp->fs.xattr.len);
3232 if (err < 0) {
3233 clunk_fid(s, xattr_fidp->fid);
3234 goto out;
3237 err = pdu_marshal(pdu, offset, "q", size);
3238 if (err < 0) {
3239 goto out;
3241 err += offset;
3243 trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size);
3244 out:
3245 put_fid(pdu, file_fidp);
3246 if (xattr_fidp) {
3247 put_fid(pdu, xattr_fidp);
3249 out_nofid:
3250 pdu_complete(pdu, err);
3251 v9fs_string_free(&name);
3254 static void v9fs_xattrcreate(void *opaque)
3256 int flags;
3257 int32_t fid;
3258 int64_t size;
3259 ssize_t err = 0;
3260 V9fsString name;
3261 size_t offset = 7;
3262 V9fsFidState *file_fidp;
3263 V9fsFidState *xattr_fidp;
3264 V9fsPDU *pdu = opaque;
3266 v9fs_string_init(&name);
3267 err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags);
3268 if (err < 0) {
3269 goto out_nofid;
3271 trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags);
3273 file_fidp = get_fid(pdu, fid);
3274 if (file_fidp == NULL) {
3275 err = -EINVAL;
3276 goto out_nofid;
3278 /* Make the file fid point to xattr */
3279 xattr_fidp = file_fidp;
3280 xattr_fidp->fid_type = P9_FID_XATTR;
3281 xattr_fidp->fs.xattr.copied_len = 0;
3282 xattr_fidp->fs.xattr.len = size;
3283 xattr_fidp->fs.xattr.flags = flags;
3284 v9fs_string_init(&xattr_fidp->fs.xattr.name);
3285 v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
3286 xattr_fidp->fs.xattr.value = g_malloc(size);
3287 err = offset;
3288 put_fid(pdu, file_fidp);
3289 out_nofid:
3290 pdu_complete(pdu, err);
3291 v9fs_string_free(&name);
3294 static void v9fs_readlink(void *opaque)
3296 V9fsPDU *pdu = opaque;
3297 size_t offset = 7;
3298 V9fsString target;
3299 int32_t fid;
3300 int err = 0;
3301 V9fsFidState *fidp;
3303 err = pdu_unmarshal(pdu, offset, "d", &fid);
3304 if (err < 0) {
3305 goto out_nofid;
3307 trace_v9fs_readlink(pdu->tag, pdu->id, fid);
3308 fidp = get_fid(pdu, fid);
3309 if (fidp == NULL) {
3310 err = -ENOENT;
3311 goto out_nofid;
3314 v9fs_string_init(&target);
3315 err = v9fs_co_readlink(pdu, &fidp->path, &target);
3316 if (err < 0) {
3317 goto out;
3319 err = pdu_marshal(pdu, offset, "s", &target);
3320 if (err < 0) {
3321 v9fs_string_free(&target);
3322 goto out;
3324 err += offset;
3325 trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data);
3326 v9fs_string_free(&target);
3327 out:
3328 put_fid(pdu, fidp);
3329 out_nofid:
3330 pdu_complete(pdu, err);
3333 static CoroutineEntry *pdu_co_handlers[] = {
3334 [P9_TREADDIR] = v9fs_readdir,
3335 [P9_TSTATFS] = v9fs_statfs,
3336 [P9_TGETATTR] = v9fs_getattr,
3337 [P9_TSETATTR] = v9fs_setattr,
3338 [P9_TXATTRWALK] = v9fs_xattrwalk,
3339 [P9_TXATTRCREATE] = v9fs_xattrcreate,
3340 [P9_TMKNOD] = v9fs_mknod,
3341 [P9_TRENAME] = v9fs_rename,
3342 [P9_TLOCK] = v9fs_lock,
3343 [P9_TGETLOCK] = v9fs_getlock,
3344 [P9_TRENAMEAT] = v9fs_renameat,
3345 [P9_TREADLINK] = v9fs_readlink,
3346 [P9_TUNLINKAT] = v9fs_unlinkat,
3347 [P9_TMKDIR] = v9fs_mkdir,
3348 [P9_TVERSION] = v9fs_version,
3349 [P9_TLOPEN] = v9fs_open,
3350 [P9_TATTACH] = v9fs_attach,
3351 [P9_TSTAT] = v9fs_stat,
3352 [P9_TWALK] = v9fs_walk,
3353 [P9_TCLUNK] = v9fs_clunk,
3354 [P9_TFSYNC] = v9fs_fsync,
3355 [P9_TOPEN] = v9fs_open,
3356 [P9_TREAD] = v9fs_read,
3357 #if 0
3358 [P9_TAUTH] = v9fs_auth,
3359 #endif
3360 [P9_TFLUSH] = v9fs_flush,
3361 [P9_TLINK] = v9fs_link,
3362 [P9_TSYMLINK] = v9fs_symlink,
3363 [P9_TCREATE] = v9fs_create,
3364 [P9_TLCREATE] = v9fs_lcreate,
3365 [P9_TWRITE] = v9fs_write,
3366 [P9_TWSTAT] = v9fs_wstat,
3367 [P9_TREMOVE] = v9fs_remove,
3370 static void v9fs_op_not_supp(void *opaque)
3372 V9fsPDU *pdu = opaque;
3373 pdu_complete(pdu, -EOPNOTSUPP);
3376 static void v9fs_fs_ro(void *opaque)
3378 V9fsPDU *pdu = opaque;
3379 pdu_complete(pdu, -EROFS);
3382 static inline bool is_read_only_op(V9fsPDU *pdu)
3384 switch (pdu->id) {
3385 case P9_TREADDIR:
3386 case P9_TSTATFS:
3387 case P9_TGETATTR:
3388 case P9_TXATTRWALK:
3389 case P9_TLOCK:
3390 case P9_TGETLOCK:
3391 case P9_TREADLINK:
3392 case P9_TVERSION:
3393 case P9_TLOPEN:
3394 case P9_TATTACH:
3395 case P9_TSTAT:
3396 case P9_TWALK:
3397 case P9_TCLUNK:
3398 case P9_TFSYNC:
3399 case P9_TOPEN:
3400 case P9_TREAD:
3401 case P9_TAUTH:
3402 case P9_TFLUSH:
3403 return 1;
3404 default:
3405 return 0;
3409 void pdu_submit(V9fsPDU *pdu)
3411 Coroutine *co;
3412 CoroutineEntry *handler;
3413 V9fsState *s = pdu->s;
3415 if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) ||
3416 (pdu_co_handlers[pdu->id] == NULL)) {
3417 handler = v9fs_op_not_supp;
3418 } else {
3419 handler = pdu_co_handlers[pdu->id];
3422 if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) {
3423 handler = v9fs_fs_ro;
3425 co = qemu_coroutine_create(handler, pdu);
3426 qemu_coroutine_enter(co);
3429 /* Returns 0 on success, 1 on failure. */
3430 int v9fs_device_realize_common(V9fsState *s, Error **errp)
3432 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
3433 int i, len;
3434 struct stat stat;
3435 FsDriverEntry *fse;
3436 V9fsPath path;
3437 int rc = 1;
3439 /* initialize pdu allocator */
3440 QLIST_INIT(&s->free_list);
3441 QLIST_INIT(&s->active_list);
3442 for (i = 0; i < (MAX_REQ - 1); i++) {
3443 QLIST_INSERT_HEAD(&s->free_list, &v->pdus[i], next);
3444 v->pdus[i].s = s;
3445 v->pdus[i].idx = i;
3448 v9fs_path_init(&path);
3450 fse = get_fsdev_fsentry(s->fsconf.fsdev_id);
3452 if (!fse) {
3453 /* We don't have a fsdev identified by fsdev_id */
3454 error_setg(errp, "9pfs device couldn't find fsdev with the "
3455 "id = %s",
3456 s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL");
3457 goto out;
3460 if (!s->fsconf.tag) {
3461 /* we haven't specified a mount_tag */
3462 error_setg(errp, "fsdev with id %s needs mount_tag arguments",
3463 s->fsconf.fsdev_id);
3464 goto out;
3467 s->ctx.export_flags = fse->export_flags;
3468 s->ctx.fs_root = g_strdup(fse->path);
3469 s->ctx.exops.get_st_gen = NULL;
3470 len = strlen(s->fsconf.tag);
3471 if (len > MAX_TAG_LEN - 1) {
3472 error_setg(errp, "mount tag '%s' (%d bytes) is longer than "
3473 "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1);
3474 goto out;
3477 s->tag = g_strdup(s->fsconf.tag);
3478 s->ctx.uid = -1;
3480 s->ops = fse->ops;
3482 s->fid_list = NULL;
3483 qemu_co_rwlock_init(&s->rename_lock);
3485 if (s->ops->init(&s->ctx) < 0) {
3486 error_setg(errp, "9pfs Failed to initialize fs-driver with id:%s"
3487 " and export path:%s", s->fsconf.fsdev_id, s->ctx.fs_root);
3488 goto out;
3492 * Check details of export path, We need to use fs driver
3493 * call back to do that. Since we are in the init path, we don't
3494 * use co-routines here.
3496 if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) {
3497 error_setg(errp,
3498 "error in converting name to path %s", strerror(errno));
3499 goto out;
3501 if (s->ops->lstat(&s->ctx, &path, &stat)) {
3502 error_setg(errp, "share path %s does not exist", fse->path);
3503 goto out;
3504 } else if (!S_ISDIR(stat.st_mode)) {
3505 error_setg(errp, "share path %s is not a directory", fse->path);
3506 goto out;
3508 v9fs_path_free(&path);
3510 rc = 0;
3511 out:
3512 if (rc) {
3513 g_free(s->ctx.fs_root);
3514 g_free(s->tag);
3515 v9fs_path_free(&path);
3517 return rc;
3520 void v9fs_device_unrealize_common(V9fsState *s, Error **errp)
3522 g_free(s->ctx.fs_root);
3523 g_free(s->tag);
3526 static void __attribute__((__constructor__)) v9fs_set_fd_limit(void)
3528 struct rlimit rlim;
3529 if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
3530 error_report("Failed to get the resource limit");
3531 exit(1);
3533 open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur/3);
3534 open_fd_rc = rlim.rlim_cur/2;