xref: /openbmc/qemu/hw/9pfs/9p.c (revision 6b3b279b)
160ce86c7SWei Liu /*
260ce86c7SWei Liu  * Virtio 9p backend
360ce86c7SWei Liu  *
460ce86c7SWei Liu  * Copyright IBM, Corp. 2010
560ce86c7SWei Liu  *
660ce86c7SWei Liu  * Authors:
760ce86c7SWei Liu  *  Anthony Liguori   <aliguori@us.ibm.com>
860ce86c7SWei Liu  *
960ce86c7SWei Liu  * This work is licensed under the terms of the GNU GPL, version 2.  See
1060ce86c7SWei Liu  * the COPYING file in the top-level directory.
1160ce86c7SWei Liu  *
1260ce86c7SWei Liu  */
1360ce86c7SWei Liu 
146f569084SChristian Schoenebeck /*
156f569084SChristian Schoenebeck  * Not so fast! You might want to read the 9p developer docs first:
166f569084SChristian Schoenebeck  * https://wiki.qemu.org/Documentation/9p
176f569084SChristian Schoenebeck  */
186f569084SChristian Schoenebeck 
19fbc04127SPeter Maydell #include "qemu/osdep.h"
20e3e83f2eSGreg Kurz #include <glib/gprintf.h>
2160ce86c7SWei Liu #include "hw/virtio/virtio.h"
22da34e65cSMarkus Armbruster #include "qapi/error.h"
2360ce86c7SWei Liu #include "qemu/error-report.h"
2460ce86c7SWei Liu #include "qemu/iov.h"
25db725815SMarkus Armbruster #include "qemu/main-loop.h"
2660ce86c7SWei Liu #include "qemu/sockets.h"
2760ce86c7SWei Liu #include "virtio-9p.h"
2860ce86c7SWei Liu #include "fsdev/qemu-fsdev.h"
2960ce86c7SWei Liu #include "9p-xattr.h"
30*6b3b279bSKeno Fischer #include "9p-util.h"
3160ce86c7SWei Liu #include "coth.h"
3260ce86c7SWei Liu #include "trace.h"
33795c40b8SJuan Quintela #include "migration/blocker.h"
341a6ed33cSAntonios Motakis #include "qemu/xxhash.h"
356b6aa828SChristian Schoenebeck #include <math.h>
36e0bd743bSKeno Fischer #ifdef CONFIG_LINUX
3703556ea9SDan Robertson #include <linux/limits.h>
38e0bd743bSKeno Fischer #else
39e0bd743bSKeno Fischer #include <limits.h>
40e0bd743bSKeno Fischer #endif
4160ce86c7SWei Liu 
4260ce86c7SWei Liu int open_fd_hw;
4360ce86c7SWei Liu int total_open_fd;
4460ce86c7SWei Liu static int open_fd_rc;
4560ce86c7SWei Liu 
4660ce86c7SWei Liu enum {
4760ce86c7SWei Liu     Oread   = 0x00,
4860ce86c7SWei Liu     Owrite  = 0x01,
4960ce86c7SWei Liu     Ordwr   = 0x02,
5060ce86c7SWei Liu     Oexec   = 0x03,
5160ce86c7SWei Liu     Oexcl   = 0x04,
5260ce86c7SWei Liu     Otrunc  = 0x10,
5360ce86c7SWei Liu     Orexec  = 0x20,
5460ce86c7SWei Liu     Orclose = 0x40,
5560ce86c7SWei Liu     Oappend = 0x80,
5660ce86c7SWei Liu };
5760ce86c7SWei Liu 
58cc82fde9SChristian Schoenebeck P9ARRAY_DEFINE_TYPE(V9fsPath, v9fs_path_free);
59cc82fde9SChristian Schoenebeck 
6075673590SGreg Kurz static ssize_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
6160ce86c7SWei Liu {
6260ce86c7SWei Liu     ssize_t ret;
6360ce86c7SWei Liu     va_list ap;
6460ce86c7SWei Liu 
6560ce86c7SWei Liu     va_start(ap, fmt);
66ea83441cSStefano Stabellini     ret = pdu->s->transport->pdu_vmarshal(pdu, offset, fmt, ap);
6760ce86c7SWei Liu     va_end(ap);
6860ce86c7SWei Liu 
6960ce86c7SWei Liu     return ret;
7060ce86c7SWei Liu }
7160ce86c7SWei Liu 
7275673590SGreg Kurz static ssize_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
7360ce86c7SWei Liu {
7460ce86c7SWei Liu     ssize_t ret;
7560ce86c7SWei Liu     va_list ap;
7660ce86c7SWei Liu 
7760ce86c7SWei Liu     va_start(ap, fmt);
78ea83441cSStefano Stabellini     ret = pdu->s->transport->pdu_vunmarshal(pdu, offset, fmt, ap);
7960ce86c7SWei Liu     va_end(ap);
8060ce86c7SWei Liu 
8160ce86c7SWei Liu     return ret;
8260ce86c7SWei Liu }
8360ce86c7SWei Liu 
8460ce86c7SWei Liu static int omode_to_uflags(int8_t mode)
8560ce86c7SWei Liu {
8660ce86c7SWei Liu     int ret = 0;
8760ce86c7SWei Liu 
8860ce86c7SWei Liu     switch (mode & 3) {
8960ce86c7SWei Liu     case Oread:
9060ce86c7SWei Liu         ret = O_RDONLY;
9160ce86c7SWei Liu         break;
9260ce86c7SWei Liu     case Ordwr:
9360ce86c7SWei Liu         ret = O_RDWR;
9460ce86c7SWei Liu         break;
9560ce86c7SWei Liu     case Owrite:
9660ce86c7SWei Liu         ret = O_WRONLY;
9760ce86c7SWei Liu         break;
9860ce86c7SWei Liu     case Oexec:
9960ce86c7SWei Liu         ret = O_RDONLY;
10060ce86c7SWei Liu         break;
10160ce86c7SWei Liu     }
10260ce86c7SWei Liu 
10360ce86c7SWei Liu     if (mode & Otrunc) {
10460ce86c7SWei Liu         ret |= O_TRUNC;
10560ce86c7SWei Liu     }
10660ce86c7SWei Liu 
10760ce86c7SWei Liu     if (mode & Oappend) {
10860ce86c7SWei Liu         ret |= O_APPEND;
10960ce86c7SWei Liu     }
11060ce86c7SWei Liu 
11160ce86c7SWei Liu     if (mode & Oexcl) {
11260ce86c7SWei Liu         ret |= O_EXCL;
11360ce86c7SWei Liu     }
11460ce86c7SWei Liu 
11560ce86c7SWei Liu     return ret;
11660ce86c7SWei Liu }
11760ce86c7SWei Liu 
1188e71b96cSGreg Kurz typedef struct DotlOpenflagMap {
11960ce86c7SWei Liu     int dotl_flag;
12060ce86c7SWei Liu     int open_flag;
1218e71b96cSGreg Kurz } DotlOpenflagMap;
12260ce86c7SWei Liu 
12360ce86c7SWei Liu static int dotl_to_open_flags(int flags)
12460ce86c7SWei Liu {
12560ce86c7SWei Liu     int i;
12660ce86c7SWei Liu     /*
12760ce86c7SWei Liu      * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
12860ce86c7SWei Liu      * and P9_DOTL_NOACCESS
12960ce86c7SWei Liu      */
13060ce86c7SWei Liu     int oflags = flags & O_ACCMODE;
13160ce86c7SWei Liu 
1328e71b96cSGreg Kurz     DotlOpenflagMap dotl_oflag_map[] = {
13360ce86c7SWei Liu         { P9_DOTL_CREATE, O_CREAT },
13460ce86c7SWei Liu         { P9_DOTL_EXCL, O_EXCL },
13560ce86c7SWei Liu         { P9_DOTL_NOCTTY , O_NOCTTY },
13660ce86c7SWei Liu         { P9_DOTL_TRUNC, O_TRUNC },
13760ce86c7SWei Liu         { P9_DOTL_APPEND, O_APPEND },
13860ce86c7SWei Liu         { P9_DOTL_NONBLOCK, O_NONBLOCK } ,
13960ce86c7SWei Liu         { P9_DOTL_DSYNC, O_DSYNC },
14060ce86c7SWei Liu         { P9_DOTL_FASYNC, FASYNC },
14160ce86c7SWei Liu         { P9_DOTL_DIRECT, O_DIRECT },
14260ce86c7SWei Liu         { P9_DOTL_LARGEFILE, O_LARGEFILE },
14360ce86c7SWei Liu         { P9_DOTL_DIRECTORY, O_DIRECTORY },
14460ce86c7SWei Liu         { P9_DOTL_NOFOLLOW, O_NOFOLLOW },
14560ce86c7SWei Liu         { P9_DOTL_NOATIME, O_NOATIME },
14660ce86c7SWei Liu         { P9_DOTL_SYNC, O_SYNC },
14760ce86c7SWei Liu     };
14860ce86c7SWei Liu 
14960ce86c7SWei Liu     for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) {
15060ce86c7SWei Liu         if (flags & dotl_oflag_map[i].dotl_flag) {
15160ce86c7SWei Liu             oflags |= dotl_oflag_map[i].open_flag;
15260ce86c7SWei Liu         }
15360ce86c7SWei Liu     }
15460ce86c7SWei Liu 
15560ce86c7SWei Liu     return oflags;
15660ce86c7SWei Liu }
15760ce86c7SWei Liu 
15860ce86c7SWei Liu void cred_init(FsCred *credp)
15960ce86c7SWei Liu {
16060ce86c7SWei Liu     credp->fc_uid = -1;
16160ce86c7SWei Liu     credp->fc_gid = -1;
16260ce86c7SWei Liu     credp->fc_mode = -1;
16360ce86c7SWei Liu     credp->fc_rdev = -1;
16460ce86c7SWei Liu }
16560ce86c7SWei Liu 
16660ce86c7SWei Liu static int get_dotl_openflags(V9fsState *s, int oflags)
16760ce86c7SWei Liu {
16860ce86c7SWei Liu     int flags;
16960ce86c7SWei Liu     /*
17060ce86c7SWei Liu      * Filter the client open flags
17160ce86c7SWei Liu      */
17260ce86c7SWei Liu     flags = dotl_to_open_flags(oflags);
17360ce86c7SWei Liu     flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT);
17460ce86c7SWei Liu     /*
17560ce86c7SWei Liu      * Ignore direct disk access hint until the server supports it.
17660ce86c7SWei Liu      */
17760ce86c7SWei Liu     flags &= ~O_DIRECT;
17860ce86c7SWei Liu     return flags;
17960ce86c7SWei Liu }
18060ce86c7SWei Liu 
18160ce86c7SWei Liu void v9fs_path_init(V9fsPath *path)
18260ce86c7SWei Liu {
18360ce86c7SWei Liu     path->data = NULL;
18460ce86c7SWei Liu     path->size = 0;
18560ce86c7SWei Liu }
18660ce86c7SWei Liu 
18760ce86c7SWei Liu void v9fs_path_free(V9fsPath *path)
18860ce86c7SWei Liu {
18960ce86c7SWei Liu     g_free(path->data);
19060ce86c7SWei Liu     path->data = NULL;
19160ce86c7SWei Liu     path->size = 0;
19260ce86c7SWei Liu }
19360ce86c7SWei Liu 
194e3e83f2eSGreg Kurz 
195e3e83f2eSGreg Kurz void GCC_FMT_ATTR(2, 3)
196e3e83f2eSGreg Kurz v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...)
197e3e83f2eSGreg Kurz {
198e3e83f2eSGreg Kurz     va_list ap;
199e3e83f2eSGreg Kurz 
200e3e83f2eSGreg Kurz     v9fs_path_free(path);
201e3e83f2eSGreg Kurz 
202e3e83f2eSGreg Kurz     va_start(ap, fmt);
203e3e83f2eSGreg Kurz     /* Bump the size for including terminating NULL */
204e3e83f2eSGreg Kurz     path->size = g_vasprintf(&path->data, fmt, ap) + 1;
205e3e83f2eSGreg Kurz     va_end(ap);
206e3e83f2eSGreg Kurz }
207e3e83f2eSGreg Kurz 
208e446a1ebSMarc-André Lureau void v9fs_path_copy(V9fsPath *dst, const V9fsPath *src)
20960ce86c7SWei Liu {
210e446a1ebSMarc-André Lureau     v9fs_path_free(dst);
211e446a1ebSMarc-André Lureau     dst->size = src->size;
212e446a1ebSMarc-André Lureau     dst->data = g_memdup(src->data, src->size);
21360ce86c7SWei Liu }
21460ce86c7SWei Liu 
21560ce86c7SWei Liu int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
21660ce86c7SWei Liu                       const char *name, V9fsPath *path)
21760ce86c7SWei Liu {
21860ce86c7SWei Liu     int err;
21960ce86c7SWei Liu     err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
22060ce86c7SWei Liu     if (err < 0) {
22160ce86c7SWei Liu         err = -errno;
22260ce86c7SWei Liu     }
22360ce86c7SWei Liu     return err;
22460ce86c7SWei Liu }
22560ce86c7SWei Liu 
22660ce86c7SWei Liu /*
22760ce86c7SWei Liu  * Return TRUE if s1 is an ancestor of s2.
22860ce86c7SWei Liu  *
22960ce86c7SWei Liu  * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
23060ce86c7SWei Liu  * As a special case, We treat s1 as ancestor of s2 if they are same!
23160ce86c7SWei Liu  */
23260ce86c7SWei Liu static int v9fs_path_is_ancestor(V9fsPath *s1, V9fsPath *s2)
23360ce86c7SWei Liu {
23460ce86c7SWei Liu     if (!strncmp(s1->data, s2->data, s1->size - 1)) {
23560ce86c7SWei Liu         if (s2->data[s1->size - 1] == '\0' || s2->data[s1->size - 1] == '/') {
23660ce86c7SWei Liu             return 1;
23760ce86c7SWei Liu         }
23860ce86c7SWei Liu     }
23960ce86c7SWei Liu     return 0;
24060ce86c7SWei Liu }
24160ce86c7SWei Liu 
24260ce86c7SWei Liu static size_t v9fs_string_size(V9fsString *str)
24360ce86c7SWei Liu {
24460ce86c7SWei Liu     return str->size;
24560ce86c7SWei Liu }
24660ce86c7SWei Liu 
24760ce86c7SWei Liu /*
24860ce86c7SWei Liu  * returns 0 if fid got re-opened, 1 if not, < 0 on error */
2498440e22eSGreg Kurz static int coroutine_fn v9fs_reopen_fid(V9fsPDU *pdu, V9fsFidState *f)
25060ce86c7SWei Liu {
25160ce86c7SWei Liu     int err = 1;
25260ce86c7SWei Liu     if (f->fid_type == P9_FID_FILE) {
25360ce86c7SWei Liu         if (f->fs.fd == -1) {
25460ce86c7SWei Liu             do {
25560ce86c7SWei Liu                 err = v9fs_co_open(pdu, f, f->open_flags);
25660ce86c7SWei Liu             } while (err == -EINTR && !pdu->cancelled);
25760ce86c7SWei Liu         }
25860ce86c7SWei Liu     } else if (f->fid_type == P9_FID_DIR) {
259f314ea4eSGreg Kurz         if (f->fs.dir.stream == NULL) {
26060ce86c7SWei Liu             do {
26160ce86c7SWei Liu                 err = v9fs_co_opendir(pdu, f);
26260ce86c7SWei Liu             } while (err == -EINTR && !pdu->cancelled);
26360ce86c7SWei Liu         }
26460ce86c7SWei Liu     }
26560ce86c7SWei Liu     return err;
26660ce86c7SWei Liu }
26760ce86c7SWei Liu 
2688440e22eSGreg Kurz static V9fsFidState *coroutine_fn get_fid(V9fsPDU *pdu, int32_t fid)
26960ce86c7SWei Liu {
27060ce86c7SWei Liu     int err;
27160ce86c7SWei Liu     V9fsFidState *f;
27260ce86c7SWei Liu     V9fsState *s = pdu->s;
27360ce86c7SWei Liu 
274feabd6cfSGreg Kurz     QSIMPLEQ_FOREACH(f, &s->fid_list, next) {
27560ce86c7SWei Liu         BUG_ON(f->clunked);
27660ce86c7SWei Liu         if (f->fid == fid) {
27760ce86c7SWei Liu             /*
27860ce86c7SWei Liu              * Update the fid ref upfront so that
27960ce86c7SWei Liu              * we don't get reclaimed when we yield
28060ce86c7SWei Liu              * in open later.
28160ce86c7SWei Liu              */
28260ce86c7SWei Liu             f->ref++;
28360ce86c7SWei Liu             /*
28460ce86c7SWei Liu              * check whether we need to reopen the
28560ce86c7SWei Liu              * file. We might have closed the fd
28660ce86c7SWei Liu              * while trying to free up some file
28760ce86c7SWei Liu              * descriptors.
28860ce86c7SWei Liu              */
28960ce86c7SWei Liu             err = v9fs_reopen_fid(pdu, f);
29060ce86c7SWei Liu             if (err < 0) {
29160ce86c7SWei Liu                 f->ref--;
29260ce86c7SWei Liu                 return NULL;
29360ce86c7SWei Liu             }
29460ce86c7SWei Liu             /*
29560ce86c7SWei Liu              * Mark the fid as referenced so that the LRU
29660ce86c7SWei Liu              * reclaim won't close the file descriptor
29760ce86c7SWei Liu              */
29860ce86c7SWei Liu             f->flags |= FID_REFERENCED;
29960ce86c7SWei Liu             return f;
30060ce86c7SWei Liu         }
30160ce86c7SWei Liu     }
30260ce86c7SWei Liu     return NULL;
30360ce86c7SWei Liu }
30460ce86c7SWei Liu 
30560ce86c7SWei Liu static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
30660ce86c7SWei Liu {
30760ce86c7SWei Liu     V9fsFidState *f;
30860ce86c7SWei Liu 
309feabd6cfSGreg Kurz     QSIMPLEQ_FOREACH(f, &s->fid_list, next) {
31060ce86c7SWei Liu         /* If fid is already there return NULL */
31160ce86c7SWei Liu         BUG_ON(f->clunked);
31260ce86c7SWei Liu         if (f->fid == fid) {
31360ce86c7SWei Liu             return NULL;
31460ce86c7SWei Liu         }
31560ce86c7SWei Liu     }
31660ce86c7SWei Liu     f = g_malloc0(sizeof(V9fsFidState));
31760ce86c7SWei Liu     f->fid = fid;
31860ce86c7SWei Liu     f->fid_type = P9_FID_NONE;
31960ce86c7SWei Liu     f->ref = 1;
32060ce86c7SWei Liu     /*
32160ce86c7SWei Liu      * Mark the fid as referenced so that the LRU
32260ce86c7SWei Liu      * reclaim won't close the file descriptor
32360ce86c7SWei Liu      */
32460ce86c7SWei Liu     f->flags |= FID_REFERENCED;
32520b7f45bSGreg Kurz     QSIMPLEQ_INSERT_TAIL(&s->fid_list, f, next);
32660ce86c7SWei Liu 
327d2c5cf7cSChristian Schoenebeck     v9fs_readdir_init(s->proto_version, &f->fs.dir);
328d2c5cf7cSChristian Schoenebeck     v9fs_readdir_init(s->proto_version, &f->fs_reclaim.dir);
3297cde47d4SGreg Kurz 
33060ce86c7SWei Liu     return f;
33160ce86c7SWei Liu }
33260ce86c7SWei Liu 
3338440e22eSGreg Kurz static int coroutine_fn v9fs_xattr_fid_clunk(V9fsPDU *pdu, V9fsFidState *fidp)
33460ce86c7SWei Liu {
33560ce86c7SWei Liu     int retval = 0;
33660ce86c7SWei Liu 
337dd28fbbcSLi Qiang     if (fidp->fs.xattr.xattrwalk_fid) {
33860ce86c7SWei Liu         /* getxattr/listxattr fid */
33960ce86c7SWei Liu         goto free_value;
34060ce86c7SWei Liu     }
34160ce86c7SWei Liu     /*
34260ce86c7SWei Liu      * if this is fid for setxattr. clunk should
34360ce86c7SWei Liu      * result in setxattr localcall
34460ce86c7SWei Liu      */
34560ce86c7SWei Liu     if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) {
34660ce86c7SWei Liu         /* clunk after partial write */
34760ce86c7SWei Liu         retval = -EINVAL;
34860ce86c7SWei Liu         goto free_out;
34960ce86c7SWei Liu     }
35060ce86c7SWei Liu     if (fidp->fs.xattr.len) {
35160ce86c7SWei Liu         retval = v9fs_co_lsetxattr(pdu, &fidp->path, &fidp->fs.xattr.name,
35260ce86c7SWei Liu                                    fidp->fs.xattr.value,
35360ce86c7SWei Liu                                    fidp->fs.xattr.len,
35460ce86c7SWei Liu                                    fidp->fs.xattr.flags);
35560ce86c7SWei Liu     } else {
35660ce86c7SWei Liu         retval = v9fs_co_lremovexattr(pdu, &fidp->path, &fidp->fs.xattr.name);
35760ce86c7SWei Liu     }
35860ce86c7SWei Liu free_out:
35960ce86c7SWei Liu     v9fs_string_free(&fidp->fs.xattr.name);
36060ce86c7SWei Liu free_value:
36160ce86c7SWei Liu     g_free(fidp->fs.xattr.value);
36260ce86c7SWei Liu     return retval;
36360ce86c7SWei Liu }
36460ce86c7SWei Liu 
3658440e22eSGreg Kurz static int coroutine_fn free_fid(V9fsPDU *pdu, V9fsFidState *fidp)
36660ce86c7SWei Liu {
36760ce86c7SWei Liu     int retval = 0;
36860ce86c7SWei Liu 
36960ce86c7SWei Liu     if (fidp->fid_type == P9_FID_FILE) {
37060ce86c7SWei Liu         /* If we reclaimed the fd no need to close */
37160ce86c7SWei Liu         if (fidp->fs.fd != -1) {
37260ce86c7SWei Liu             retval = v9fs_co_close(pdu, &fidp->fs);
37360ce86c7SWei Liu         }
37460ce86c7SWei Liu     } else if (fidp->fid_type == P9_FID_DIR) {
375f314ea4eSGreg Kurz         if (fidp->fs.dir.stream != NULL) {
37660ce86c7SWei Liu             retval = v9fs_co_closedir(pdu, &fidp->fs);
37760ce86c7SWei Liu         }
37860ce86c7SWei Liu     } else if (fidp->fid_type == P9_FID_XATTR) {
37960ce86c7SWei Liu         retval = v9fs_xattr_fid_clunk(pdu, fidp);
38060ce86c7SWei Liu     }
38160ce86c7SWei Liu     v9fs_path_free(&fidp->path);
38260ce86c7SWei Liu     g_free(fidp);
38360ce86c7SWei Liu     return retval;
38460ce86c7SWei Liu }
38560ce86c7SWei Liu 
3868440e22eSGreg Kurz static int coroutine_fn put_fid(V9fsPDU *pdu, V9fsFidState *fidp)
38760ce86c7SWei Liu {
38860ce86c7SWei Liu     BUG_ON(!fidp->ref);
38960ce86c7SWei Liu     fidp->ref--;
39060ce86c7SWei Liu     /*
39160ce86c7SWei Liu      * Don't free the fid if it is in reclaim list
39260ce86c7SWei Liu      */
39360ce86c7SWei Liu     if (!fidp->ref && fidp->clunked) {
39460ce86c7SWei Liu         if (fidp->fid == pdu->s->root_fid) {
39560ce86c7SWei Liu             /*
39660ce86c7SWei Liu              * if the clunked fid is root fid then we
39760ce86c7SWei Liu              * have unmounted the fs on the client side.
39860ce86c7SWei Liu              * delete the migration blocker. Ideally, this
39960ce86c7SWei Liu              * should be hooked to transport close notification
40060ce86c7SWei Liu              */
40160ce86c7SWei Liu             if (pdu->s->migration_blocker) {
40260ce86c7SWei Liu                 migrate_del_blocker(pdu->s->migration_blocker);
40360ce86c7SWei Liu                 error_free(pdu->s->migration_blocker);
40460ce86c7SWei Liu                 pdu->s->migration_blocker = NULL;
40560ce86c7SWei Liu             }
40660ce86c7SWei Liu         }
40760ce86c7SWei Liu         return free_fid(pdu, fidp);
40860ce86c7SWei Liu     }
40960ce86c7SWei Liu     return 0;
41060ce86c7SWei Liu }
41160ce86c7SWei Liu 
41260ce86c7SWei Liu static V9fsFidState *clunk_fid(V9fsState *s, int32_t fid)
41360ce86c7SWei Liu {
414feabd6cfSGreg Kurz     V9fsFidState *fidp;
41560ce86c7SWei Liu 
416feabd6cfSGreg Kurz     QSIMPLEQ_FOREACH(fidp, &s->fid_list, next) {
417feabd6cfSGreg Kurz         if (fidp->fid == fid) {
418feabd6cfSGreg Kurz             QSIMPLEQ_REMOVE(&s->fid_list, fidp, V9fsFidState, next);
4192e53160fSGreg Kurz             fidp->clunked = true;
42060ce86c7SWei Liu             return fidp;
42160ce86c7SWei Liu         }
422feabd6cfSGreg Kurz     }
423feabd6cfSGreg Kurz     return NULL;
424feabd6cfSGreg Kurz }
42560ce86c7SWei Liu 
4268440e22eSGreg Kurz void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu)
42760ce86c7SWei Liu {
42860ce86c7SWei Liu     int reclaim_count = 0;
42960ce86c7SWei Liu     V9fsState *s = pdu->s;
43081f9766bSGreg Kurz     V9fsFidState *f;
43181f9766bSGreg Kurz     QSLIST_HEAD(, V9fsFidState) reclaim_list =
43281f9766bSGreg Kurz         QSLIST_HEAD_INITIALIZER(reclaim_list);
43360ce86c7SWei Liu 
434feabd6cfSGreg Kurz     QSIMPLEQ_FOREACH(f, &s->fid_list, next) {
43560ce86c7SWei Liu         /*
43660ce86c7SWei Liu          * Unlink fids cannot be reclaimed. Check
43760ce86c7SWei Liu          * for them and skip them. Also skip fids
43860ce86c7SWei Liu          * currently being operated on.
43960ce86c7SWei Liu          */
44060ce86c7SWei Liu         if (f->ref || f->flags & FID_NON_RECLAIMABLE) {
44160ce86c7SWei Liu             continue;
44260ce86c7SWei Liu         }
44360ce86c7SWei Liu         /*
44460ce86c7SWei Liu          * if it is a recently referenced fid
44560ce86c7SWei Liu          * we leave the fid untouched and clear the
44660ce86c7SWei Liu          * reference bit. We come back to it later
44760ce86c7SWei Liu          * in the next iteration. (a simple LRU without
44860ce86c7SWei Liu          * moving list elements around)
44960ce86c7SWei Liu          */
45060ce86c7SWei Liu         if (f->flags & FID_REFERENCED) {
45160ce86c7SWei Liu             f->flags &= ~FID_REFERENCED;
45260ce86c7SWei Liu             continue;
45360ce86c7SWei Liu         }
45460ce86c7SWei Liu         /*
45560ce86c7SWei Liu          * Add fids to reclaim list.
45660ce86c7SWei Liu          */
45760ce86c7SWei Liu         if (f->fid_type == P9_FID_FILE) {
45860ce86c7SWei Liu             if (f->fs.fd != -1) {
45960ce86c7SWei Liu                 /*
46060ce86c7SWei Liu                  * Up the reference count so that
46160ce86c7SWei Liu                  * a clunk request won't free this fid
46260ce86c7SWei Liu                  */
46360ce86c7SWei Liu                 f->ref++;
46481f9766bSGreg Kurz                 QSLIST_INSERT_HEAD(&reclaim_list, f, reclaim_next);
46560ce86c7SWei Liu                 f->fs_reclaim.fd = f->fs.fd;
46660ce86c7SWei Liu                 f->fs.fd = -1;
46760ce86c7SWei Liu                 reclaim_count++;
46860ce86c7SWei Liu             }
46960ce86c7SWei Liu         } else if (f->fid_type == P9_FID_DIR) {
470f314ea4eSGreg Kurz             if (f->fs.dir.stream != NULL) {
47160ce86c7SWei Liu                 /*
47260ce86c7SWei Liu                  * Up the reference count so that
47360ce86c7SWei Liu                  * a clunk request won't free this fid
47460ce86c7SWei Liu                  */
47560ce86c7SWei Liu                 f->ref++;
47681f9766bSGreg Kurz                 QSLIST_INSERT_HEAD(&reclaim_list, f, reclaim_next);
477f314ea4eSGreg Kurz                 f->fs_reclaim.dir.stream = f->fs.dir.stream;
478f314ea4eSGreg Kurz                 f->fs.dir.stream = NULL;
47960ce86c7SWei Liu                 reclaim_count++;
48060ce86c7SWei Liu             }
48160ce86c7SWei Liu         }
48260ce86c7SWei Liu         if (reclaim_count >= open_fd_rc) {
48360ce86c7SWei Liu             break;
48460ce86c7SWei Liu         }
48560ce86c7SWei Liu     }
48660ce86c7SWei Liu     /*
48760ce86c7SWei Liu      * Now close the fid in reclaim list. Free them if they
48860ce86c7SWei Liu      * are already clunked.
48960ce86c7SWei Liu      */
49081f9766bSGreg Kurz     while (!QSLIST_EMPTY(&reclaim_list)) {
49181f9766bSGreg Kurz         f = QSLIST_FIRST(&reclaim_list);
49281f9766bSGreg Kurz         QSLIST_REMOVE(&reclaim_list, f, V9fsFidState, reclaim_next);
49360ce86c7SWei Liu         if (f->fid_type == P9_FID_FILE) {
49460ce86c7SWei Liu             v9fs_co_close(pdu, &f->fs_reclaim);
49560ce86c7SWei Liu         } else if (f->fid_type == P9_FID_DIR) {
49660ce86c7SWei Liu             v9fs_co_closedir(pdu, &f->fs_reclaim);
49760ce86c7SWei Liu         }
49860ce86c7SWei Liu         /*
49960ce86c7SWei Liu          * Now drop the fid reference, free it
50060ce86c7SWei Liu          * if clunked.
50160ce86c7SWei Liu          */
50260ce86c7SWei Liu         put_fid(pdu, f);
50360ce86c7SWei Liu     }
50460ce86c7SWei Liu }
50560ce86c7SWei Liu 
5068440e22eSGreg Kurz static int coroutine_fn v9fs_mark_fids_unreclaim(V9fsPDU *pdu, V9fsPath *path)
50760ce86c7SWei Liu {
50860ce86c7SWei Liu     int err;
50960ce86c7SWei Liu     V9fsState *s = pdu->s;
51020b7f45bSGreg Kurz     V9fsFidState *fidp, *fidp_next;
51160ce86c7SWei Liu 
51220b7f45bSGreg Kurz     fidp = QSIMPLEQ_FIRST(&s->fid_list);
51320b7f45bSGreg Kurz     if (!fidp) {
51420b7f45bSGreg Kurz         return 0;
51560ce86c7SWei Liu     }
51620b7f45bSGreg Kurz 
51720b7f45bSGreg Kurz     /*
51820b7f45bSGreg Kurz      * v9fs_reopen_fid() can yield : a reference on the fid must be held
51920b7f45bSGreg Kurz      * to ensure its pointer remains valid and we can safely pass it to
52020b7f45bSGreg Kurz      * QSIMPLEQ_NEXT(). The corresponding put_fid() can also yield so
52120b7f45bSGreg Kurz      * we must keep a reference on the next fid as well. So the logic here
52220b7f45bSGreg Kurz      * is to get a reference on a fid and only put it back during the next
52320b7f45bSGreg Kurz      * iteration after we could get a reference on the next fid. Start with
52420b7f45bSGreg Kurz      * the first one.
52520b7f45bSGreg Kurz      */
52620b7f45bSGreg Kurz     for (fidp->ref++; fidp; fidp = fidp_next) {
52720b7f45bSGreg Kurz         if (fidp->path.size == path->size &&
52820b7f45bSGreg Kurz             !memcmp(fidp->path.data, path->data, path->size)) {
52960ce86c7SWei Liu             /* Mark the fid non reclaimable. */
53060ce86c7SWei Liu             fidp->flags |= FID_NON_RECLAIMABLE;
53160ce86c7SWei Liu 
53260ce86c7SWei Liu             /* reopen the file/dir if already closed */
53360ce86c7SWei Liu             err = v9fs_reopen_fid(pdu, fidp);
53460ce86c7SWei Liu             if (err < 0) {
53520b7f45bSGreg Kurz                 put_fid(pdu, fidp);
536267fcadfSGreg Kurz                 return err;
53760ce86c7SWei Liu             }
53820b7f45bSGreg Kurz         }
53920b7f45bSGreg Kurz 
54020b7f45bSGreg Kurz         fidp_next = QSIMPLEQ_NEXT(fidp, next);
54120b7f45bSGreg Kurz 
54220b7f45bSGreg Kurz         if (fidp_next) {
54360ce86c7SWei Liu             /*
54420b7f45bSGreg Kurz              * Ensure the next fid survives a potential clunk request during
54520b7f45bSGreg Kurz              * put_fid() below and v9fs_reopen_fid() in the next iteration.
54660ce86c7SWei Liu              */
54720b7f45bSGreg Kurz             fidp_next->ref++;
54860ce86c7SWei Liu         }
54920b7f45bSGreg Kurz 
55020b7f45bSGreg Kurz         /* We're done with this fid */
55120b7f45bSGreg Kurz         put_fid(pdu, fidp);
55260ce86c7SWei Liu     }
55320b7f45bSGreg Kurz 
55460ce86c7SWei Liu     return 0;
55560ce86c7SWei Liu }
55660ce86c7SWei Liu 
5578440e22eSGreg Kurz static void coroutine_fn virtfs_reset(V9fsPDU *pdu)
55860ce86c7SWei Liu {
55960ce86c7SWei Liu     V9fsState *s = pdu->s;
56079decce3SGreg Kurz     V9fsFidState *fidp;
56160ce86c7SWei Liu 
56260ce86c7SWei Liu     /* Free all fids */
563feabd6cfSGreg Kurz     while (!QSIMPLEQ_EMPTY(&s->fid_list)) {
5646d54af0eSGreg Kurz         /* Get fid */
565feabd6cfSGreg Kurz         fidp = QSIMPLEQ_FIRST(&s->fid_list);
5666d54af0eSGreg Kurz         fidp->ref++;
56760ce86c7SWei Liu 
5686d54af0eSGreg Kurz         /* Clunk fid */
569feabd6cfSGreg Kurz         QSIMPLEQ_REMOVE(&s->fid_list, fidp, V9fsFidState, next);
5702e53160fSGreg Kurz         fidp->clunked = true;
5716d54af0eSGreg Kurz 
5726d54af0eSGreg Kurz         put_fid(pdu, fidp);
57360ce86c7SWei Liu     }
57460ce86c7SWei Liu }
57560ce86c7SWei Liu 
57660ce86c7SWei Liu #define P9_QID_TYPE_DIR         0x80
57760ce86c7SWei Liu #define P9_QID_TYPE_SYMLINK     0x02
57860ce86c7SWei Liu 
57960ce86c7SWei Liu #define P9_STAT_MODE_DIR        0x80000000
58060ce86c7SWei Liu #define P9_STAT_MODE_APPEND     0x40000000
58160ce86c7SWei Liu #define P9_STAT_MODE_EXCL       0x20000000
58260ce86c7SWei Liu #define P9_STAT_MODE_MOUNT      0x10000000
58360ce86c7SWei Liu #define P9_STAT_MODE_AUTH       0x08000000
58460ce86c7SWei Liu #define P9_STAT_MODE_TMP        0x04000000
58560ce86c7SWei Liu #define P9_STAT_MODE_SYMLINK    0x02000000
58660ce86c7SWei Liu #define P9_STAT_MODE_LINK       0x01000000
58760ce86c7SWei Liu #define P9_STAT_MODE_DEVICE     0x00800000
58860ce86c7SWei Liu #define P9_STAT_MODE_NAMED_PIPE 0x00200000
58960ce86c7SWei Liu #define P9_STAT_MODE_SOCKET     0x00100000
59060ce86c7SWei Liu #define P9_STAT_MODE_SETUID     0x00080000
59160ce86c7SWei Liu #define P9_STAT_MODE_SETGID     0x00040000
59260ce86c7SWei Liu #define P9_STAT_MODE_SETVTX     0x00010000
59360ce86c7SWei Liu 
59460ce86c7SWei Liu #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR |          \
59560ce86c7SWei Liu                                 P9_STAT_MODE_SYMLINK |      \
59660ce86c7SWei Liu                                 P9_STAT_MODE_LINK |         \
59760ce86c7SWei Liu                                 P9_STAT_MODE_DEVICE |       \
59860ce86c7SWei Liu                                 P9_STAT_MODE_NAMED_PIPE |   \
59960ce86c7SWei Liu                                 P9_STAT_MODE_SOCKET)
60060ce86c7SWei Liu 
6016b6aa828SChristian Schoenebeck /* Mirrors all bits of a byte. So e.g. binary 10100000 would become 00000101. */
6026b6aa828SChristian Schoenebeck static inline uint8_t mirror8bit(uint8_t byte)
6036b6aa828SChristian Schoenebeck {
6046b6aa828SChristian Schoenebeck     return (byte * 0x0202020202ULL & 0x010884422010ULL) % 1023;
6056b6aa828SChristian Schoenebeck }
6066b6aa828SChristian Schoenebeck 
6076b6aa828SChristian Schoenebeck /* Same as mirror8bit() just for a 64 bit data type instead for a byte. */
6086b6aa828SChristian Schoenebeck static inline uint64_t mirror64bit(uint64_t value)
6096b6aa828SChristian Schoenebeck {
6106b6aa828SChristian Schoenebeck     return ((uint64_t)mirror8bit(value         & 0xff) << 56) |
6116b6aa828SChristian Schoenebeck            ((uint64_t)mirror8bit((value >> 8)  & 0xff) << 48) |
6126b6aa828SChristian Schoenebeck            ((uint64_t)mirror8bit((value >> 16) & 0xff) << 40) |
6136b6aa828SChristian Schoenebeck            ((uint64_t)mirror8bit((value >> 24) & 0xff) << 32) |
6146b6aa828SChristian Schoenebeck            ((uint64_t)mirror8bit((value >> 32) & 0xff) << 24) |
6156b6aa828SChristian Schoenebeck            ((uint64_t)mirror8bit((value >> 40) & 0xff) << 16) |
6166b6aa828SChristian Schoenebeck            ((uint64_t)mirror8bit((value >> 48) & 0xff) << 8)  |
6176b6aa828SChristian Schoenebeck            ((uint64_t)mirror8bit((value >> 56) & 0xff));
6186b6aa828SChristian Schoenebeck }
6196b6aa828SChristian Schoenebeck 
6206b6aa828SChristian Schoenebeck /**
6216b6aa828SChristian Schoenebeck  * @brief Parameter k for the Exponential Golomb algorihm to be used.
6226b6aa828SChristian Schoenebeck  *
6236b6aa828SChristian Schoenebeck  * The smaller this value, the smaller the minimum bit count for the Exp.
6246b6aa828SChristian Schoenebeck  * Golomb generated affixes will be (at lowest index) however for the
6256b6aa828SChristian Schoenebeck  * price of having higher maximum bit count of generated affixes (at highest
6266b6aa828SChristian Schoenebeck  * index). Likewise increasing this parameter yields in smaller maximum bit
6276b6aa828SChristian Schoenebeck  * count for the price of having higher minimum bit count.
6286b6aa828SChristian Schoenebeck  *
6296b6aa828SChristian Schoenebeck  * In practice that means: a good value for k depends on the expected amount
6306b6aa828SChristian Schoenebeck  * of devices to be exposed by one export. For a small amount of devices k
6316b6aa828SChristian Schoenebeck  * should be small, for a large amount of devices k might be increased
6326b6aa828SChristian Schoenebeck  * instead. The default of k=0 should be fine for most users though.
6336b6aa828SChristian Schoenebeck  *
6346b6aa828SChristian Schoenebeck  * @b IMPORTANT: In case this ever becomes a runtime parameter; the value of
6356b6aa828SChristian Schoenebeck  * k should not change as long as guest is still running! Because that would
6366b6aa828SChristian Schoenebeck  * cause completely different inode numbers to be generated on guest.
6376b6aa828SChristian Schoenebeck  */
6386b6aa828SChristian Schoenebeck #define EXP_GOLOMB_K    0
6396b6aa828SChristian Schoenebeck 
6406b6aa828SChristian Schoenebeck /**
6416b6aa828SChristian Schoenebeck  * @brief Exponential Golomb algorithm for arbitrary k (including k=0).
6426b6aa828SChristian Schoenebeck  *
6436b6aa828SChristian Schoenebeck  * The Exponential Golomb algorithm generates @b prefixes (@b not suffixes!)
6446b6aa828SChristian Schoenebeck  * with growing length and with the mathematical property of being
6456b6aa828SChristian Schoenebeck  * "prefix-free". The latter means the generated prefixes can be prepended
6466b6aa828SChristian Schoenebeck  * in front of arbitrary numbers and the resulting concatenated numbers are
6476b6aa828SChristian Schoenebeck  * guaranteed to be always unique.
6486b6aa828SChristian Schoenebeck  *
6496b6aa828SChristian Schoenebeck  * This is a minor adjustment to the original Exp. Golomb algorithm in the
6506b6aa828SChristian Schoenebeck  * sense that lowest allowed index (@param n) starts with 1, not with zero.
6516b6aa828SChristian Schoenebeck  *
6526b6aa828SChristian Schoenebeck  * @param n - natural number (or index) of the prefix to be generated
6536b6aa828SChristian Schoenebeck  *            (1, 2, 3, ...)
6546b6aa828SChristian Schoenebeck  * @param k - parameter k of Exp. Golomb algorithm to be used
6556b6aa828SChristian Schoenebeck  *            (see comment on EXP_GOLOMB_K macro for details about k)
6566b6aa828SChristian Schoenebeck  */
6576b6aa828SChristian Schoenebeck static VariLenAffix expGolombEncode(uint64_t n, int k)
6586b6aa828SChristian Schoenebeck {
6596b6aa828SChristian Schoenebeck     const uint64_t value = n + (1 << k) - 1;
6606b6aa828SChristian Schoenebeck     const int bits = (int) log2(value) + 1;
6616b6aa828SChristian Schoenebeck     return (VariLenAffix) {
6626b6aa828SChristian Schoenebeck         .type = AffixType_Prefix,
6636b6aa828SChristian Schoenebeck         .value = value,
6646b6aa828SChristian Schoenebeck         .bits = bits + MAX((bits - 1 - k), 0)
6656b6aa828SChristian Schoenebeck     };
6666b6aa828SChristian Schoenebeck }
6676b6aa828SChristian Schoenebeck 
6686b6aa828SChristian Schoenebeck /**
6696b6aa828SChristian Schoenebeck  * @brief Converts a suffix into a prefix, or a prefix into a suffix.
6706b6aa828SChristian Schoenebeck  *
6716b6aa828SChristian Schoenebeck  * Simply mirror all bits of the affix value, for the purpose to preserve
6726b6aa828SChristian Schoenebeck  * respectively the mathematical "prefix-free" or "suffix-free" property
6736b6aa828SChristian Schoenebeck  * after the conversion.
6746b6aa828SChristian Schoenebeck  *
6756b6aa828SChristian Schoenebeck  * If a passed prefix is suitable to create unique numbers, then the
6766b6aa828SChristian Schoenebeck  * returned suffix is suitable to create unique numbers as well (and vice
6776b6aa828SChristian Schoenebeck  * versa).
6786b6aa828SChristian Schoenebeck  */
6796b6aa828SChristian Schoenebeck static VariLenAffix invertAffix(const VariLenAffix *affix)
6806b6aa828SChristian Schoenebeck {
6816b6aa828SChristian Schoenebeck     return (VariLenAffix) {
6826b6aa828SChristian Schoenebeck         .type =
6836b6aa828SChristian Schoenebeck             (affix->type == AffixType_Suffix) ?
6846b6aa828SChristian Schoenebeck                 AffixType_Prefix : AffixType_Suffix,
6856b6aa828SChristian Schoenebeck         .value =
6866b6aa828SChristian Schoenebeck             mirror64bit(affix->value) >>
6876b6aa828SChristian Schoenebeck             ((sizeof(affix->value) * 8) - affix->bits),
6886b6aa828SChristian Schoenebeck         .bits = affix->bits
6896b6aa828SChristian Schoenebeck     };
6906b6aa828SChristian Schoenebeck }
6916b6aa828SChristian Schoenebeck 
6926b6aa828SChristian Schoenebeck /**
6936b6aa828SChristian Schoenebeck  * @brief Generates suffix numbers with "suffix-free" property.
6946b6aa828SChristian Schoenebeck  *
6956b6aa828SChristian Schoenebeck  * This is just a wrapper function on top of the Exp. Golomb algorithm.
6966b6aa828SChristian Schoenebeck  *
6976b6aa828SChristian Schoenebeck  * Since the Exp. Golomb algorithm generates prefixes, but we need suffixes,
6986b6aa828SChristian Schoenebeck  * this function converts the Exp. Golomb prefixes into appropriate suffixes
6996b6aa828SChristian Schoenebeck  * which are still suitable for generating unique numbers.
7006b6aa828SChristian Schoenebeck  *
7016b6aa828SChristian Schoenebeck  * @param n - natural number (or index) of the suffix to be generated
7026b6aa828SChristian Schoenebeck  *            (1, 2, 3, ...)
7036b6aa828SChristian Schoenebeck  */
7046b6aa828SChristian Schoenebeck static VariLenAffix affixForIndex(uint64_t index)
7056b6aa828SChristian Schoenebeck {
7066b6aa828SChristian Schoenebeck     VariLenAffix prefix;
7076b6aa828SChristian Schoenebeck     prefix = expGolombEncode(index, EXP_GOLOMB_K);
7086b6aa828SChristian Schoenebeck     return invertAffix(&prefix); /* convert prefix to suffix */
7096b6aa828SChristian Schoenebeck }
7106b6aa828SChristian Schoenebeck 
7111a6ed33cSAntonios Motakis /* creative abuse of tb_hash_func7, which is based on xxhash */
7121a6ed33cSAntonios Motakis static uint32_t qpp_hash(QppEntry e)
7131a6ed33cSAntonios Motakis {
7141a6ed33cSAntonios Motakis     return qemu_xxhash7(e.ino_prefix, e.dev, 0, 0, 0);
7151a6ed33cSAntonios Motakis }
7161a6ed33cSAntonios Motakis 
717f3fe4a2dSAntonios Motakis static uint32_t qpf_hash(QpfEntry e)
718f3fe4a2dSAntonios Motakis {
719f3fe4a2dSAntonios Motakis     return qemu_xxhash7(e.ino, e.dev, 0, 0, 0);
720f3fe4a2dSAntonios Motakis }
721f3fe4a2dSAntonios Motakis 
7226b6aa828SChristian Schoenebeck static bool qpd_cmp_func(const void *obj, const void *userp)
7236b6aa828SChristian Schoenebeck {
7246b6aa828SChristian Schoenebeck     const QpdEntry *e1 = obj, *e2 = userp;
7256b6aa828SChristian Schoenebeck     return e1->dev == e2->dev;
7266b6aa828SChristian Schoenebeck }
7276b6aa828SChristian Schoenebeck 
7286b6aa828SChristian Schoenebeck static bool qpp_cmp_func(const void *obj, const void *userp)
7291a6ed33cSAntonios Motakis {
7301a6ed33cSAntonios Motakis     const QppEntry *e1 = obj, *e2 = userp;
7311a6ed33cSAntonios Motakis     return e1->dev == e2->dev && e1->ino_prefix == e2->ino_prefix;
7321a6ed33cSAntonios Motakis }
7331a6ed33cSAntonios Motakis 
7346b6aa828SChristian Schoenebeck static bool qpf_cmp_func(const void *obj, const void *userp)
735f3fe4a2dSAntonios Motakis {
736f3fe4a2dSAntonios Motakis     const QpfEntry *e1 = obj, *e2 = userp;
737f3fe4a2dSAntonios Motakis     return e1->dev == e2->dev && e1->ino == e2->ino;
738f3fe4a2dSAntonios Motakis }
739f3fe4a2dSAntonios Motakis 
740f3fe4a2dSAntonios Motakis static void qp_table_remove(void *p, uint32_t h, void *up)
7411a6ed33cSAntonios Motakis {
7421a6ed33cSAntonios Motakis     g_free(p);
7431a6ed33cSAntonios Motakis }
7441a6ed33cSAntonios Motakis 
745f3fe4a2dSAntonios Motakis static void qp_table_destroy(struct qht *ht)
7461a6ed33cSAntonios Motakis {
7471a6ed33cSAntonios Motakis     if (!ht || !ht->map) {
7481a6ed33cSAntonios Motakis         return;
7491a6ed33cSAntonios Motakis     }
750f3fe4a2dSAntonios Motakis     qht_iter(ht, qp_table_remove, NULL);
7511a6ed33cSAntonios Motakis     qht_destroy(ht);
7521a6ed33cSAntonios Motakis }
7531a6ed33cSAntonios Motakis 
7546b6aa828SChristian Schoenebeck static void qpd_table_init(struct qht *ht)
7556b6aa828SChristian Schoenebeck {
7566b6aa828SChristian Schoenebeck     qht_init(ht, qpd_cmp_func, 1, QHT_MODE_AUTO_RESIZE);
7576b6aa828SChristian Schoenebeck }
7586b6aa828SChristian Schoenebeck 
7591a6ed33cSAntonios Motakis static void qpp_table_init(struct qht *ht)
7601a6ed33cSAntonios Motakis {
7616b6aa828SChristian Schoenebeck     qht_init(ht, qpp_cmp_func, 1, QHT_MODE_AUTO_RESIZE);
7621a6ed33cSAntonios Motakis }
7631a6ed33cSAntonios Motakis 
764f3fe4a2dSAntonios Motakis static void qpf_table_init(struct qht *ht)
765f3fe4a2dSAntonios Motakis {
7666b6aa828SChristian Schoenebeck     qht_init(ht, qpf_cmp_func, 1 << 16, QHT_MODE_AUTO_RESIZE);
767f3fe4a2dSAntonios Motakis }
768f3fe4a2dSAntonios Motakis 
7696b6aa828SChristian Schoenebeck /*
7706b6aa828SChristian Schoenebeck  * Returns how many (high end) bits of inode numbers of the passed fs
7716b6aa828SChristian Schoenebeck  * device shall be used (in combination with the device number) to
7726b6aa828SChristian Schoenebeck  * generate hash values for qpp_table entries.
7736b6aa828SChristian Schoenebeck  *
7746b6aa828SChristian Schoenebeck  * This function is required if variable length suffixes are used for inode
7756b6aa828SChristian Schoenebeck  * number mapping on guest level. Since a device may end up having multiple
7766b6aa828SChristian Schoenebeck  * entries in qpp_table, each entry most probably with a different suffix
7776b6aa828SChristian Schoenebeck  * length, we thus need this function in conjunction with qpd_table to
7786b6aa828SChristian Schoenebeck  * "agree" about a fix amount of bits (per device) to be always used for
7796b6aa828SChristian Schoenebeck  * generating hash values for the purpose of accessing qpp_table in order
7806b6aa828SChristian Schoenebeck  * get consistent behaviour when accessing qpp_table.
7816b6aa828SChristian Schoenebeck  */
7826b6aa828SChristian Schoenebeck static int qid_inode_prefix_hash_bits(V9fsPDU *pdu, dev_t dev)
7836b6aa828SChristian Schoenebeck {
7846b6aa828SChristian Schoenebeck     QpdEntry lookup = {
7856b6aa828SChristian Schoenebeck         .dev = dev
7866b6aa828SChristian Schoenebeck     }, *val;
7876b6aa828SChristian Schoenebeck     uint32_t hash = dev;
7886b6aa828SChristian Schoenebeck     VariLenAffix affix;
7896b6aa828SChristian Schoenebeck 
7906b6aa828SChristian Schoenebeck     val = qht_lookup(&pdu->s->qpd_table, &lookup, hash);
7916b6aa828SChristian Schoenebeck     if (!val) {
7926b6aa828SChristian Schoenebeck         val = g_malloc0(sizeof(QpdEntry));
7936b6aa828SChristian Schoenebeck         *val = lookup;
7946b6aa828SChristian Schoenebeck         affix = affixForIndex(pdu->s->qp_affix_next);
7956b6aa828SChristian Schoenebeck         val->prefix_bits = affix.bits;
7966b6aa828SChristian Schoenebeck         qht_insert(&pdu->s->qpd_table, val, hash, NULL);
7976b6aa828SChristian Schoenebeck         pdu->s->qp_ndevices++;
7986b6aa828SChristian Schoenebeck     }
7996b6aa828SChristian Schoenebeck     return val->prefix_bits;
8006b6aa828SChristian Schoenebeck }
8016b6aa828SChristian Schoenebeck 
8026b6aa828SChristian Schoenebeck /**
8036b6aa828SChristian Schoenebeck  * @brief Slow / full mapping host inode nr -> guest inode nr.
8046b6aa828SChristian Schoenebeck  *
8056b6aa828SChristian Schoenebeck  * This function performs a slower and much more costly remapping of an
8066b6aa828SChristian Schoenebeck  * original file inode number on host to an appropriate different inode
8076b6aa828SChristian Schoenebeck  * number on guest. For every (dev, inode) combination on host a new
8086b6aa828SChristian Schoenebeck  * sequential number is generated, cached and exposed as inode number on
8096b6aa828SChristian Schoenebeck  * guest.
8106b6aa828SChristian Schoenebeck  *
8116b6aa828SChristian Schoenebeck  * This is just a "last resort" fallback solution if the much faster/cheaper
8126b6aa828SChristian Schoenebeck  * qid_path_suffixmap() failed. In practice this slow / full mapping is not
8136b6aa828SChristian Schoenebeck  * expected ever to be used at all though.
8146b6aa828SChristian Schoenebeck  *
8156b6aa828SChristian Schoenebeck  * @see qid_path_suffixmap() for details
8166b6aa828SChristian Schoenebeck  *
8176b6aa828SChristian Schoenebeck  */
818f3fe4a2dSAntonios Motakis static int qid_path_fullmap(V9fsPDU *pdu, const struct stat *stbuf,
819f3fe4a2dSAntonios Motakis                             uint64_t *path)
820f3fe4a2dSAntonios Motakis {
821f3fe4a2dSAntonios Motakis     QpfEntry lookup = {
822f3fe4a2dSAntonios Motakis         .dev = stbuf->st_dev,
823f3fe4a2dSAntonios Motakis         .ino = stbuf->st_ino
824f3fe4a2dSAntonios Motakis     }, *val;
825f3fe4a2dSAntonios Motakis     uint32_t hash = qpf_hash(lookup);
8266b6aa828SChristian Schoenebeck     VariLenAffix affix;
827f3fe4a2dSAntonios Motakis 
828f3fe4a2dSAntonios Motakis     val = qht_lookup(&pdu->s->qpf_table, &lookup, hash);
829f3fe4a2dSAntonios Motakis 
830f3fe4a2dSAntonios Motakis     if (!val) {
831f3fe4a2dSAntonios Motakis         if (pdu->s->qp_fullpath_next == 0) {
832f3fe4a2dSAntonios Motakis             /* no more files can be mapped :'( */
833f3fe4a2dSAntonios Motakis             error_report_once(
834f3fe4a2dSAntonios Motakis                 "9p: No more prefixes available for remapping inodes from "
835f3fe4a2dSAntonios Motakis                 "host to guest."
836f3fe4a2dSAntonios Motakis             );
837f3fe4a2dSAntonios Motakis             return -ENFILE;
838f3fe4a2dSAntonios Motakis         }
839f3fe4a2dSAntonios Motakis 
840f3fe4a2dSAntonios Motakis         val = g_malloc0(sizeof(QppEntry));
841f3fe4a2dSAntonios Motakis         *val = lookup;
842f3fe4a2dSAntonios Motakis 
843f3fe4a2dSAntonios Motakis         /* new unique inode and device combo */
8446b6aa828SChristian Schoenebeck         affix = affixForIndex(
8456b6aa828SChristian Schoenebeck             1ULL << (sizeof(pdu->s->qp_affix_next) * 8)
8466b6aa828SChristian Schoenebeck         );
8476b6aa828SChristian Schoenebeck         val->path = (pdu->s->qp_fullpath_next++ << affix.bits) | affix.value;
8486b6aa828SChristian Schoenebeck         pdu->s->qp_fullpath_next &= ((1ULL << (64 - affix.bits)) - 1);
849f3fe4a2dSAntonios Motakis         qht_insert(&pdu->s->qpf_table, val, hash, NULL);
850f3fe4a2dSAntonios Motakis     }
851f3fe4a2dSAntonios Motakis 
852f3fe4a2dSAntonios Motakis     *path = val->path;
853f3fe4a2dSAntonios Motakis     return 0;
854f3fe4a2dSAntonios Motakis }
855f3fe4a2dSAntonios Motakis 
8566b6aa828SChristian Schoenebeck /**
8576b6aa828SChristian Schoenebeck  * @brief Quick mapping host inode nr -> guest inode nr.
8581a6ed33cSAntonios Motakis  *
8596b6aa828SChristian Schoenebeck  * This function performs quick remapping of an original file inode number
8606b6aa828SChristian Schoenebeck  * on host to an appropriate different inode number on guest. This remapping
8616b6aa828SChristian Schoenebeck  * of inodes is required to avoid inode nr collisions on guest which would
8626b6aa828SChristian Schoenebeck  * happen if the 9p export contains more than 1 exported file system (or
8636b6aa828SChristian Schoenebeck  * more than 1 file system data set), because unlike on host level where the
8646b6aa828SChristian Schoenebeck  * files would have different device nrs, all files exported by 9p would
8656b6aa828SChristian Schoenebeck  * share the same device nr on guest (the device nr of the virtual 9p device
8666b6aa828SChristian Schoenebeck  * that is).
8676b6aa828SChristian Schoenebeck  *
8686b6aa828SChristian Schoenebeck  * Inode remapping is performed by chopping off high end bits of the original
8696b6aa828SChristian Schoenebeck  * inode number from host, shifting the result upwards and then assigning a
8706b6aa828SChristian Schoenebeck  * generated suffix number for the low end bits, where the same suffix number
8716b6aa828SChristian Schoenebeck  * will be shared by all inodes with the same device id AND the same high end
8726b6aa828SChristian Schoenebeck  * bits that have been chopped off. That approach utilizes the fact that inode
8736b6aa828SChristian Schoenebeck  * numbers very likely share the same high end bits (i.e. due to their common
8746b6aa828SChristian Schoenebeck  * sequential generation by file systems) and hence we only have to generate
8756b6aa828SChristian Schoenebeck  * and track a very limited amount of suffixes in practice due to that.
8766b6aa828SChristian Schoenebeck  *
8776b6aa828SChristian Schoenebeck  * We generate variable size suffixes for that purpose. The 1st generated
8786b6aa828SChristian Schoenebeck  * suffix will only have 1 bit and hence we only need to chop off 1 bit from
8796b6aa828SChristian Schoenebeck  * the original inode number. The subsequent suffixes being generated will
8806b6aa828SChristian Schoenebeck  * grow in (bit) size subsequently, i.e. the 2nd and 3rd suffix being
8816b6aa828SChristian Schoenebeck  * generated will have 3 bits and hence we have to chop off 3 bits from their
8826b6aa828SChristian Schoenebeck  * original inodes, and so on. That approach of using variable length suffixes
8836b6aa828SChristian Schoenebeck  * (i.e. over fixed size ones) utilizes the fact that in practice only a very
8846b6aa828SChristian Schoenebeck  * limited amount of devices are shared by the same export (e.g. typically
8856b6aa828SChristian Schoenebeck  * less than 2 dozen devices per 9p export), so in practice we need to chop
8866b6aa828SChristian Schoenebeck  * off less bits than with fixed size prefixes and yet are flexible to add
8876b6aa828SChristian Schoenebeck  * new devices at runtime below host's export directory at any time without
8886b6aa828SChristian Schoenebeck  * having to reboot guest nor requiring to reconfigure guest for that. And due
8896b6aa828SChristian Schoenebeck  * to the very limited amount of original high end bits that we chop off that
8906b6aa828SChristian Schoenebeck  * way, the total amount of suffixes we need to generate is less than by using
8916b6aa828SChristian Schoenebeck  * fixed size prefixes and hence it also improves performance of the inode
8926b6aa828SChristian Schoenebeck  * remapping algorithm, and finally has the nice side effect that the inode
8936b6aa828SChristian Schoenebeck  * numbers on guest will be much smaller & human friendly. ;-)
8941a6ed33cSAntonios Motakis  */
8956b6aa828SChristian Schoenebeck static int qid_path_suffixmap(V9fsPDU *pdu, const struct stat *stbuf,
8961a6ed33cSAntonios Motakis                               uint64_t *path)
8971a6ed33cSAntonios Motakis {
8986b6aa828SChristian Schoenebeck     const int ino_hash_bits = qid_inode_prefix_hash_bits(pdu, stbuf->st_dev);
8991a6ed33cSAntonios Motakis     QppEntry lookup = {
9001a6ed33cSAntonios Motakis         .dev = stbuf->st_dev,
9016b6aa828SChristian Schoenebeck         .ino_prefix = (uint16_t) (stbuf->st_ino >> (64 - ino_hash_bits))
9021a6ed33cSAntonios Motakis     }, *val;
9031a6ed33cSAntonios Motakis     uint32_t hash = qpp_hash(lookup);
9041a6ed33cSAntonios Motakis 
9051a6ed33cSAntonios Motakis     val = qht_lookup(&pdu->s->qpp_table, &lookup, hash);
9061a6ed33cSAntonios Motakis 
9071a6ed33cSAntonios Motakis     if (!val) {
9086b6aa828SChristian Schoenebeck         if (pdu->s->qp_affix_next == 0) {
9096b6aa828SChristian Schoenebeck             /* we ran out of affixes */
910f3fe4a2dSAntonios Motakis             warn_report_once(
911f3fe4a2dSAntonios Motakis                 "9p: Potential degraded performance of inode remapping"
9121a6ed33cSAntonios Motakis             );
9131a6ed33cSAntonios Motakis             return -ENFILE;
9141a6ed33cSAntonios Motakis         }
9151a6ed33cSAntonios Motakis 
9161a6ed33cSAntonios Motakis         val = g_malloc0(sizeof(QppEntry));
9171a6ed33cSAntonios Motakis         *val = lookup;
9181a6ed33cSAntonios Motakis 
9196b6aa828SChristian Schoenebeck         /* new unique inode affix and device combo */
9206b6aa828SChristian Schoenebeck         val->qp_affix_index = pdu->s->qp_affix_next++;
9216b6aa828SChristian Schoenebeck         val->qp_affix = affixForIndex(val->qp_affix_index);
9221a6ed33cSAntonios Motakis         qht_insert(&pdu->s->qpp_table, val, hash, NULL);
9231a6ed33cSAntonios Motakis     }
9246b6aa828SChristian Schoenebeck     /* assuming generated affix to be suffix type, not prefix */
9256b6aa828SChristian Schoenebeck     *path = (stbuf->st_ino << val->qp_affix.bits) | val->qp_affix.value;
9261a6ed33cSAntonios Motakis     return 0;
9271a6ed33cSAntonios Motakis }
9281a6ed33cSAntonios Motakis 
9293b5ee9e8SAntonios Motakis static int stat_to_qid(V9fsPDU *pdu, const struct stat *stbuf, V9fsQID *qidp)
93060ce86c7SWei Liu {
9311a6ed33cSAntonios Motakis     int err;
93260ce86c7SWei Liu     size_t size;
93360ce86c7SWei Liu 
9341a6ed33cSAntonios Motakis     if (pdu->s->ctx.export_flags & V9FS_REMAP_INODES) {
9351a6ed33cSAntonios Motakis         /* map inode+device to qid path (fast path) */
9366b6aa828SChristian Schoenebeck         err = qid_path_suffixmap(pdu, stbuf, &qidp->path);
937f3fe4a2dSAntonios Motakis         if (err == -ENFILE) {
938f3fe4a2dSAntonios Motakis             /* fast path didn't work, fall back to full map */
939f3fe4a2dSAntonios Motakis             err = qid_path_fullmap(pdu, stbuf, &qidp->path);
940f3fe4a2dSAntonios Motakis         }
9411a6ed33cSAntonios Motakis         if (err) {
9421a6ed33cSAntonios Motakis             return err;
9431a6ed33cSAntonios Motakis         }
9441a6ed33cSAntonios Motakis     } else {
9453b5ee9e8SAntonios Motakis         if (pdu->s->dev_id != stbuf->st_dev) {
9461a6ed33cSAntonios Motakis             if (pdu->s->ctx.export_flags & V9FS_FORBID_MULTIDEVS) {
9471a6ed33cSAntonios Motakis                 error_report_once(
9481a6ed33cSAntonios Motakis                     "9p: Multiple devices detected in same VirtFS export. "
9491a6ed33cSAntonios Motakis                     "Access of guest to additional devices is (partly) "
9501a6ed33cSAntonios Motakis                     "denied due to virtfs option 'multidevs=forbid' being "
9511a6ed33cSAntonios Motakis                     "effective."
9521a6ed33cSAntonios Motakis                 );
9531a6ed33cSAntonios Motakis                 return -ENODEV;
9541a6ed33cSAntonios Motakis             } else {
9553b5ee9e8SAntonios Motakis                 warn_report_once(
9563b5ee9e8SAntonios Motakis                     "9p: Multiple devices detected in same VirtFS export, "
9573b5ee9e8SAntonios Motakis                     "which might lead to file ID collisions and severe "
9581a6ed33cSAntonios Motakis                     "misbehaviours on guest! You should either use a "
9591a6ed33cSAntonios Motakis                     "separate export for each device shared from host or "
9601a6ed33cSAntonios Motakis                     "use virtfs option 'multidevs=remap'!"
9613b5ee9e8SAntonios Motakis                 );
9623b5ee9e8SAntonios Motakis             }
9631a6ed33cSAntonios Motakis         }
96460ce86c7SWei Liu         memset(&qidp->path, 0, sizeof(qidp->path));
96560ce86c7SWei Liu         size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
96660ce86c7SWei Liu         memcpy(&qidp->path, &stbuf->st_ino, size);
9671a6ed33cSAntonios Motakis     }
9681a6ed33cSAntonios Motakis 
96960ce86c7SWei Liu     qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
97060ce86c7SWei Liu     qidp->type = 0;
97160ce86c7SWei Liu     if (S_ISDIR(stbuf->st_mode)) {
97260ce86c7SWei Liu         qidp->type |= P9_QID_TYPE_DIR;
97360ce86c7SWei Liu     }
97460ce86c7SWei Liu     if (S_ISLNK(stbuf->st_mode)) {
97560ce86c7SWei Liu         qidp->type |= P9_QID_TYPE_SYMLINK;
97660ce86c7SWei Liu     }
9773b5ee9e8SAntonios Motakis 
9783b5ee9e8SAntonios Motakis     return 0;
97960ce86c7SWei Liu }
98060ce86c7SWei Liu 
98160ce86c7SWei Liu V9fsPDU *pdu_alloc(V9fsState *s)
98260ce86c7SWei Liu {
98360ce86c7SWei Liu     V9fsPDU *pdu = NULL;
98460ce86c7SWei Liu 
98560ce86c7SWei Liu     if (!QLIST_EMPTY(&s->free_list)) {
98660ce86c7SWei Liu         pdu = QLIST_FIRST(&s->free_list);
98760ce86c7SWei Liu         QLIST_REMOVE(pdu, next);
98860ce86c7SWei Liu         QLIST_INSERT_HEAD(&s->active_list, pdu, next);
98960ce86c7SWei Liu     }
99060ce86c7SWei Liu     return pdu;
99160ce86c7SWei Liu }
99260ce86c7SWei Liu 
99360ce86c7SWei Liu void pdu_free(V9fsPDU *pdu)
99460ce86c7SWei Liu {
99560ce86c7SWei Liu     V9fsState *s = pdu->s;
996f74e27bfSGreg Kurz 
997f74e27bfSGreg Kurz     g_assert(!pdu->cancelled);
99860ce86c7SWei Liu     QLIST_REMOVE(pdu, next);
99960ce86c7SWei Liu     QLIST_INSERT_HEAD(&s->free_list, pdu, next);
100060ce86c7SWei Liu }
100160ce86c7SWei Liu 
10028440e22eSGreg Kurz static void coroutine_fn pdu_complete(V9fsPDU *pdu, ssize_t len)
100360ce86c7SWei Liu {
100460ce86c7SWei Liu     int8_t id = pdu->id + 1; /* Response */
100560ce86c7SWei Liu     V9fsState *s = pdu->s;
100606a37db7SGreg Kurz     int ret;
100760ce86c7SWei Liu 
1008fc78d5eeSKeno Fischer     /*
1009fc78d5eeSKeno Fischer      * The 9p spec requires that successfully cancelled pdus receive no reply.
1010fc78d5eeSKeno Fischer      * Sending a reply would confuse clients because they would
1011fc78d5eeSKeno Fischer      * assume that any EINTR is the actual result of the operation,
1012fc78d5eeSKeno Fischer      * rather than a consequence of the cancellation. However, if
1013fc78d5eeSKeno Fischer      * the operation completed (succesfully or with an error other
1014fc78d5eeSKeno Fischer      * than caused be cancellation), we do send out that reply, both
1015fc78d5eeSKeno Fischer      * for efficiency and to avoid confusing the rest of the state machine
1016fc78d5eeSKeno Fischer      * that assumes passing a non-error here will mean a successful
1017fc78d5eeSKeno Fischer      * transmission of the reply.
1018fc78d5eeSKeno Fischer      */
1019fc78d5eeSKeno Fischer     bool discard = pdu->cancelled && len == -EINTR;
1020fc78d5eeSKeno Fischer     if (discard) {
1021fc78d5eeSKeno Fischer         trace_v9fs_rcancel(pdu->tag, pdu->id);
1022fc78d5eeSKeno Fischer         pdu->size = 0;
1023fc78d5eeSKeno Fischer         goto out_notify;
1024fc78d5eeSKeno Fischer     }
1025fc78d5eeSKeno Fischer 
102660ce86c7SWei Liu     if (len < 0) {
102760ce86c7SWei Liu         int err = -len;
102860ce86c7SWei Liu         len = 7;
102960ce86c7SWei Liu 
103060ce86c7SWei Liu         if (s->proto_version != V9FS_PROTO_2000L) {
103160ce86c7SWei Liu             V9fsString str;
103260ce86c7SWei Liu 
103360ce86c7SWei Liu             str.data = strerror(err);
103460ce86c7SWei Liu             str.size = strlen(str.data);
103560ce86c7SWei Liu 
103606a37db7SGreg Kurz             ret = pdu_marshal(pdu, len, "s", &str);
103706a37db7SGreg Kurz             if (ret < 0) {
103806a37db7SGreg Kurz                 goto out_notify;
103906a37db7SGreg Kurz             }
104006a37db7SGreg Kurz             len += ret;
104160ce86c7SWei Liu             id = P9_RERROR;
104260ce86c7SWei Liu         }
104360ce86c7SWei Liu 
104406a37db7SGreg Kurz         ret = pdu_marshal(pdu, len, "d", err);
104506a37db7SGreg Kurz         if (ret < 0) {
104606a37db7SGreg Kurz             goto out_notify;
104706a37db7SGreg Kurz         }
104806a37db7SGreg Kurz         len += ret;
104960ce86c7SWei Liu 
105060ce86c7SWei Liu         if (s->proto_version == V9FS_PROTO_2000L) {
105160ce86c7SWei Liu             id = P9_RLERROR;
105260ce86c7SWei Liu         }
105360ce86c7SWei Liu         trace_v9fs_rerror(pdu->tag, pdu->id, err); /* Trace ERROR */
105460ce86c7SWei Liu     }
105560ce86c7SWei Liu 
105660ce86c7SWei Liu     /* fill out the header */
105706a37db7SGreg Kurz     if (pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag) < 0) {
105806a37db7SGreg Kurz         goto out_notify;
105906a37db7SGreg Kurz     }
106060ce86c7SWei Liu 
106160ce86c7SWei Liu     /* keep these in sync */
106260ce86c7SWei Liu     pdu->size = len;
106360ce86c7SWei Liu     pdu->id = id;
106460ce86c7SWei Liu 
106506a37db7SGreg Kurz out_notify:
1066a17d8659SGreg Kurz     pdu->s->transport->push_and_notify(pdu);
106760ce86c7SWei Liu 
106860ce86c7SWei Liu     /* Now wakeup anybody waiting in flush for this request */
1069f74e27bfSGreg Kurz     if (!qemu_co_queue_next(&pdu->complete)) {
107060ce86c7SWei Liu         pdu_free(pdu);
107160ce86c7SWei Liu     }
1072f74e27bfSGreg Kurz }
107360ce86c7SWei Liu 
107460ce86c7SWei Liu static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
107560ce86c7SWei Liu {
107660ce86c7SWei Liu     mode_t ret;
107760ce86c7SWei Liu 
107860ce86c7SWei Liu     ret = mode & 0777;
107960ce86c7SWei Liu     if (mode & P9_STAT_MODE_DIR) {
108060ce86c7SWei Liu         ret |= S_IFDIR;
108160ce86c7SWei Liu     }
108260ce86c7SWei Liu 
108360ce86c7SWei Liu     if (mode & P9_STAT_MODE_SYMLINK) {
108460ce86c7SWei Liu         ret |= S_IFLNK;
108560ce86c7SWei Liu     }
108660ce86c7SWei Liu     if (mode & P9_STAT_MODE_SOCKET) {
108760ce86c7SWei Liu         ret |= S_IFSOCK;
108860ce86c7SWei Liu     }
108960ce86c7SWei Liu     if (mode & P9_STAT_MODE_NAMED_PIPE) {
109060ce86c7SWei Liu         ret |= S_IFIFO;
109160ce86c7SWei Liu     }
109260ce86c7SWei Liu     if (mode & P9_STAT_MODE_DEVICE) {
109360ce86c7SWei Liu         if (extension->size && extension->data[0] == 'c') {
109460ce86c7SWei Liu             ret |= S_IFCHR;
109560ce86c7SWei Liu         } else {
109660ce86c7SWei Liu             ret |= S_IFBLK;
109760ce86c7SWei Liu         }
109860ce86c7SWei Liu     }
109960ce86c7SWei Liu 
110060ce86c7SWei Liu     if (!(ret & ~0777)) {
110160ce86c7SWei Liu         ret |= S_IFREG;
110260ce86c7SWei Liu     }
110360ce86c7SWei Liu 
110460ce86c7SWei Liu     if (mode & P9_STAT_MODE_SETUID) {
110560ce86c7SWei Liu         ret |= S_ISUID;
110660ce86c7SWei Liu     }
110760ce86c7SWei Liu     if (mode & P9_STAT_MODE_SETGID) {
110860ce86c7SWei Liu         ret |= S_ISGID;
110960ce86c7SWei Liu     }
111060ce86c7SWei Liu     if (mode & P9_STAT_MODE_SETVTX) {
111160ce86c7SWei Liu         ret |= S_ISVTX;
111260ce86c7SWei Liu     }
111360ce86c7SWei Liu 
111460ce86c7SWei Liu     return ret;
111560ce86c7SWei Liu }
111660ce86c7SWei Liu 
111760ce86c7SWei Liu static int donttouch_stat(V9fsStat *stat)
111860ce86c7SWei Liu {
111960ce86c7SWei Liu     if (stat->type == -1 &&
112060ce86c7SWei Liu         stat->dev == -1 &&
112187032833SAntonios Motakis         stat->qid.type == 0xff &&
112287032833SAntonios Motakis         stat->qid.version == (uint32_t) -1 &&
112387032833SAntonios Motakis         stat->qid.path == (uint64_t) -1 &&
112460ce86c7SWei Liu         stat->mode == -1 &&
112560ce86c7SWei Liu         stat->atime == -1 &&
112660ce86c7SWei Liu         stat->mtime == -1 &&
112760ce86c7SWei Liu         stat->length == -1 &&
112860ce86c7SWei Liu         !stat->name.size &&
112960ce86c7SWei Liu         !stat->uid.size &&
113060ce86c7SWei Liu         !stat->gid.size &&
113160ce86c7SWei Liu         !stat->muid.size &&
113260ce86c7SWei Liu         stat->n_uid == -1 &&
113360ce86c7SWei Liu         stat->n_gid == -1 &&
113460ce86c7SWei Liu         stat->n_muid == -1) {
113560ce86c7SWei Liu         return 1;
113660ce86c7SWei Liu     }
113760ce86c7SWei Liu 
113860ce86c7SWei Liu     return 0;
113960ce86c7SWei Liu }
114060ce86c7SWei Liu 
114160ce86c7SWei Liu static void v9fs_stat_init(V9fsStat *stat)
114260ce86c7SWei Liu {
114360ce86c7SWei Liu     v9fs_string_init(&stat->name);
114460ce86c7SWei Liu     v9fs_string_init(&stat->uid);
114560ce86c7SWei Liu     v9fs_string_init(&stat->gid);
114660ce86c7SWei Liu     v9fs_string_init(&stat->muid);
114760ce86c7SWei Liu     v9fs_string_init(&stat->extension);
114860ce86c7SWei Liu }
114960ce86c7SWei Liu 
115060ce86c7SWei Liu static void v9fs_stat_free(V9fsStat *stat)
115160ce86c7SWei Liu {
115260ce86c7SWei Liu     v9fs_string_free(&stat->name);
115360ce86c7SWei Liu     v9fs_string_free(&stat->uid);
115460ce86c7SWei Liu     v9fs_string_free(&stat->gid);
115560ce86c7SWei Liu     v9fs_string_free(&stat->muid);
115660ce86c7SWei Liu     v9fs_string_free(&stat->extension);
115760ce86c7SWei Liu }
115860ce86c7SWei Liu 
115960ce86c7SWei Liu static uint32_t stat_to_v9mode(const struct stat *stbuf)
116060ce86c7SWei Liu {
116160ce86c7SWei Liu     uint32_t mode;
116260ce86c7SWei Liu 
116360ce86c7SWei Liu     mode = stbuf->st_mode & 0777;
116460ce86c7SWei Liu     if (S_ISDIR(stbuf->st_mode)) {
116560ce86c7SWei Liu         mode |= P9_STAT_MODE_DIR;
116660ce86c7SWei Liu     }
116760ce86c7SWei Liu 
116860ce86c7SWei Liu     if (S_ISLNK(stbuf->st_mode)) {
116960ce86c7SWei Liu         mode |= P9_STAT_MODE_SYMLINK;
117060ce86c7SWei Liu     }
117160ce86c7SWei Liu 
117260ce86c7SWei Liu     if (S_ISSOCK(stbuf->st_mode)) {
117360ce86c7SWei Liu         mode |= P9_STAT_MODE_SOCKET;
117460ce86c7SWei Liu     }
117560ce86c7SWei Liu 
117660ce86c7SWei Liu     if (S_ISFIFO(stbuf->st_mode)) {
117760ce86c7SWei Liu         mode |= P9_STAT_MODE_NAMED_PIPE;
117860ce86c7SWei Liu     }
117960ce86c7SWei Liu 
118060ce86c7SWei Liu     if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
118160ce86c7SWei Liu         mode |= P9_STAT_MODE_DEVICE;
118260ce86c7SWei Liu     }
118360ce86c7SWei Liu 
118460ce86c7SWei Liu     if (stbuf->st_mode & S_ISUID) {
118560ce86c7SWei Liu         mode |= P9_STAT_MODE_SETUID;
118660ce86c7SWei Liu     }
118760ce86c7SWei Liu 
118860ce86c7SWei Liu     if (stbuf->st_mode & S_ISGID) {
118960ce86c7SWei Liu         mode |= P9_STAT_MODE_SETGID;
119060ce86c7SWei Liu     }
119160ce86c7SWei Liu 
119260ce86c7SWei Liu     if (stbuf->st_mode & S_ISVTX) {
119360ce86c7SWei Liu         mode |= P9_STAT_MODE_SETVTX;
119460ce86c7SWei Liu     }
119560ce86c7SWei Liu 
119660ce86c7SWei Liu     return mode;
119760ce86c7SWei Liu }
119860ce86c7SWei Liu 
11996069537fSJan Dakinevich static int coroutine_fn stat_to_v9stat(V9fsPDU *pdu, V9fsPath *path,
12006069537fSJan Dakinevich                                        const char *basename,
120160ce86c7SWei Liu                                        const struct stat *stbuf,
120260ce86c7SWei Liu                                        V9fsStat *v9stat)
120360ce86c7SWei Liu {
120460ce86c7SWei Liu     int err;
120560ce86c7SWei Liu 
120660ce86c7SWei Liu     memset(v9stat, 0, sizeof(*v9stat));
120760ce86c7SWei Liu 
12083b5ee9e8SAntonios Motakis     err = stat_to_qid(pdu, stbuf, &v9stat->qid);
12093b5ee9e8SAntonios Motakis     if (err < 0) {
12103b5ee9e8SAntonios Motakis         return err;
12113b5ee9e8SAntonios Motakis     }
121260ce86c7SWei Liu     v9stat->mode = stat_to_v9mode(stbuf);
121360ce86c7SWei Liu     v9stat->atime = stbuf->st_atime;
121460ce86c7SWei Liu     v9stat->mtime = stbuf->st_mtime;
121560ce86c7SWei Liu     v9stat->length = stbuf->st_size;
121660ce86c7SWei Liu 
1217abdf0086SGreg Kurz     v9fs_string_free(&v9stat->uid);
1218abdf0086SGreg Kurz     v9fs_string_free(&v9stat->gid);
1219abdf0086SGreg Kurz     v9fs_string_free(&v9stat->muid);
122060ce86c7SWei Liu 
122160ce86c7SWei Liu     v9stat->n_uid = stbuf->st_uid;
122260ce86c7SWei Liu     v9stat->n_gid = stbuf->st_gid;
122360ce86c7SWei Liu     v9stat->n_muid = 0;
122460ce86c7SWei Liu 
1225abdf0086SGreg Kurz     v9fs_string_free(&v9stat->extension);
122660ce86c7SWei Liu 
122760ce86c7SWei Liu     if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
12286069537fSJan Dakinevich         err = v9fs_co_readlink(pdu, path, &v9stat->extension);
122960ce86c7SWei Liu         if (err < 0) {
123060ce86c7SWei Liu             return err;
123160ce86c7SWei Liu         }
123260ce86c7SWei Liu     } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
123360ce86c7SWei Liu         v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
123460ce86c7SWei Liu                 S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
123560ce86c7SWei Liu                 major(stbuf->st_rdev), minor(stbuf->st_rdev));
123660ce86c7SWei Liu     } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
123760ce86c7SWei Liu         v9fs_string_sprintf(&v9stat->extension, "%s %lu",
123860ce86c7SWei Liu                 "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink);
123960ce86c7SWei Liu     }
124060ce86c7SWei Liu 
12416069537fSJan Dakinevich     v9fs_string_sprintf(&v9stat->name, "%s", basename);
124260ce86c7SWei Liu 
124360ce86c7SWei Liu     v9stat->size = 61 +
124460ce86c7SWei Liu         v9fs_string_size(&v9stat->name) +
124560ce86c7SWei Liu         v9fs_string_size(&v9stat->uid) +
124660ce86c7SWei Liu         v9fs_string_size(&v9stat->gid) +
124760ce86c7SWei Liu         v9fs_string_size(&v9stat->muid) +
124860ce86c7SWei Liu         v9fs_string_size(&v9stat->extension);
124960ce86c7SWei Liu     return 0;
125060ce86c7SWei Liu }
125160ce86c7SWei Liu 
125260ce86c7SWei Liu #define P9_STATS_MODE          0x00000001ULL
125360ce86c7SWei Liu #define P9_STATS_NLINK         0x00000002ULL
125460ce86c7SWei Liu #define P9_STATS_UID           0x00000004ULL
125560ce86c7SWei Liu #define P9_STATS_GID           0x00000008ULL
125660ce86c7SWei Liu #define P9_STATS_RDEV          0x00000010ULL
125760ce86c7SWei Liu #define P9_STATS_ATIME         0x00000020ULL
125860ce86c7SWei Liu #define P9_STATS_MTIME         0x00000040ULL
125960ce86c7SWei Liu #define P9_STATS_CTIME         0x00000080ULL
126060ce86c7SWei Liu #define P9_STATS_INO           0x00000100ULL
126160ce86c7SWei Liu #define P9_STATS_SIZE          0x00000200ULL
126260ce86c7SWei Liu #define P9_STATS_BLOCKS        0x00000400ULL
126360ce86c7SWei Liu 
126460ce86c7SWei Liu #define P9_STATS_BTIME         0x00000800ULL
126560ce86c7SWei Liu #define P9_STATS_GEN           0x00001000ULL
126660ce86c7SWei Liu #define P9_STATS_DATA_VERSION  0x00002000ULL
126760ce86c7SWei Liu 
126860ce86c7SWei Liu #define P9_STATS_BASIC         0x000007ffULL /* Mask for fields up to BLOCKS */
126960ce86c7SWei Liu #define P9_STATS_ALL           0x00003fffULL /* Mask for All fields above */
127060ce86c7SWei Liu 
127160ce86c7SWei Liu 
1272b565bccbSChristian Schoenebeck /**
1273b565bccbSChristian Schoenebeck  * Convert host filesystem's block size into an appropriate block size for
1274b565bccbSChristian Schoenebeck  * 9p client (guest OS side). The value returned suggests an "optimum" block
1275b565bccbSChristian Schoenebeck  * size for 9p I/O, i.e. to maximize performance.
1276b565bccbSChristian Schoenebeck  *
1277b565bccbSChristian Schoenebeck  * @pdu: 9p client request
1278b565bccbSChristian Schoenebeck  * @blksize: host filesystem's block size
1279b565bccbSChristian Schoenebeck  */
1280b565bccbSChristian Schoenebeck static int32_t blksize_to_iounit(const V9fsPDU *pdu, int32_t blksize)
1281669ced09SChristian Schoenebeck {
1282669ced09SChristian Schoenebeck     int32_t iounit = 0;
1283669ced09SChristian Schoenebeck     V9fsState *s = pdu->s;
1284669ced09SChristian Schoenebeck 
1285669ced09SChristian Schoenebeck     /*
1286b565bccbSChristian Schoenebeck      * iounit should be multiples of blksize (host filesystem block size)
1287669ced09SChristian Schoenebeck      * as well as less than (client msize - P9_IOHDRSZ)
1288669ced09SChristian Schoenebeck      */
1289b565bccbSChristian Schoenebeck     if (blksize) {
129004a7f9e5SChristian Schoenebeck         iounit = QEMU_ALIGN_DOWN(s->msize - P9_IOHDRSZ, blksize);
1291669ced09SChristian Schoenebeck     }
1292669ced09SChristian Schoenebeck     if (!iounit) {
1293669ced09SChristian Schoenebeck         iounit = s->msize - P9_IOHDRSZ;
1294669ced09SChristian Schoenebeck     }
1295669ced09SChristian Schoenebeck     return iounit;
1296669ced09SChristian Schoenebeck }
1297669ced09SChristian Schoenebeck 
1298b565bccbSChristian Schoenebeck static int32_t stat_to_iounit(const V9fsPDU *pdu, const struct stat *stbuf)
1299b565bccbSChristian Schoenebeck {
1300b565bccbSChristian Schoenebeck     return blksize_to_iounit(pdu, stbuf->st_blksize);
1301b565bccbSChristian Schoenebeck }
1302b565bccbSChristian Schoenebeck 
13033b5ee9e8SAntonios Motakis static int stat_to_v9stat_dotl(V9fsPDU *pdu, const struct stat *stbuf,
130460ce86c7SWei Liu                                 V9fsStatDotl *v9lstat)
130560ce86c7SWei Liu {
130660ce86c7SWei Liu     memset(v9lstat, 0, sizeof(*v9lstat));
130760ce86c7SWei Liu 
130860ce86c7SWei Liu     v9lstat->st_mode = stbuf->st_mode;
130960ce86c7SWei Liu     v9lstat->st_nlink = stbuf->st_nlink;
131060ce86c7SWei Liu     v9lstat->st_uid = stbuf->st_uid;
131160ce86c7SWei Liu     v9lstat->st_gid = stbuf->st_gid;
131260ce86c7SWei Liu     v9lstat->st_rdev = stbuf->st_rdev;
131360ce86c7SWei Liu     v9lstat->st_size = stbuf->st_size;
1314669ced09SChristian Schoenebeck     v9lstat->st_blksize = stat_to_iounit(pdu, stbuf);
131560ce86c7SWei Liu     v9lstat->st_blocks = stbuf->st_blocks;
131660ce86c7SWei Liu     v9lstat->st_atime_sec = stbuf->st_atime;
131760ce86c7SWei Liu     v9lstat->st_mtime_sec = stbuf->st_mtime;
131860ce86c7SWei Liu     v9lstat->st_ctime_sec = stbuf->st_ctime;
1319f41db099SKeno Fischer #ifdef CONFIG_DARWIN
1320f41db099SKeno Fischer     v9lstat->st_atime_nsec = stbuf->st_atimespec.tv_nsec;
1321f41db099SKeno Fischer     v9lstat->st_mtime_nsec = stbuf->st_mtimespec.tv_nsec;
1322f41db099SKeno Fischer     v9lstat->st_ctime_nsec = stbuf->st_ctimespec.tv_nsec;
1323f41db099SKeno Fischer #else
1324f41db099SKeno Fischer     v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
1325f41db099SKeno Fischer     v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
132660ce86c7SWei Liu     v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
1327f41db099SKeno Fischer #endif
132860ce86c7SWei Liu     /* Currently we only support BASIC fields in stat */
132960ce86c7SWei Liu     v9lstat->st_result_mask = P9_STATS_BASIC;
133060ce86c7SWei Liu 
13313b5ee9e8SAntonios Motakis     return stat_to_qid(pdu, stbuf, &v9lstat->qid);
133260ce86c7SWei Liu }
133360ce86c7SWei Liu 
133460ce86c7SWei Liu static void print_sg(struct iovec *sg, int cnt)
133560ce86c7SWei Liu {
133660ce86c7SWei Liu     int i;
133760ce86c7SWei Liu 
133860ce86c7SWei Liu     printf("sg[%d]: {", cnt);
133960ce86c7SWei Liu     for (i = 0; i < cnt; i++) {
134060ce86c7SWei Liu         if (i) {
134160ce86c7SWei Liu             printf(", ");
134260ce86c7SWei Liu         }
134360ce86c7SWei Liu         printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
134460ce86c7SWei Liu     }
134560ce86c7SWei Liu     printf("}\n");
134660ce86c7SWei Liu }
134760ce86c7SWei Liu 
134860ce86c7SWei Liu /* Will call this only for path name based fid */
134960ce86c7SWei Liu static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len)
135060ce86c7SWei Liu {
135160ce86c7SWei Liu     V9fsPath str;
135260ce86c7SWei Liu     v9fs_path_init(&str);
135360ce86c7SWei Liu     v9fs_path_copy(&str, dst);
1354e3e83f2eSGreg Kurz     v9fs_path_sprintf(dst, "%s%s", src->data, str.data + len);
135560ce86c7SWei Liu     v9fs_path_free(&str);
135660ce86c7SWei Liu }
135760ce86c7SWei Liu 
135860ce86c7SWei Liu static inline bool is_ro_export(FsContext *ctx)
135960ce86c7SWei Liu {
136060ce86c7SWei Liu     return ctx->export_flags & V9FS_RDONLY;
136160ce86c7SWei Liu }
136260ce86c7SWei Liu 
13638440e22eSGreg Kurz static void coroutine_fn v9fs_version(void *opaque)
136460ce86c7SWei Liu {
136560ce86c7SWei Liu     ssize_t err;
136660ce86c7SWei Liu     V9fsPDU *pdu = opaque;
136760ce86c7SWei Liu     V9fsState *s = pdu->s;
136860ce86c7SWei Liu     V9fsString version;
136960ce86c7SWei Liu     size_t offset = 7;
137060ce86c7SWei Liu 
137160ce86c7SWei Liu     v9fs_string_init(&version);
137260ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
137360ce86c7SWei Liu     if (err < 0) {
137460ce86c7SWei Liu         goto out;
137560ce86c7SWei Liu     }
137660ce86c7SWei Liu     trace_v9fs_version(pdu->tag, pdu->id, s->msize, version.data);
137760ce86c7SWei Liu 
137860ce86c7SWei Liu     virtfs_reset(pdu);
137960ce86c7SWei Liu 
138060ce86c7SWei Liu     if (!strcmp(version.data, "9P2000.u")) {
138160ce86c7SWei Liu         s->proto_version = V9FS_PROTO_2000U;
138260ce86c7SWei Liu     } else if (!strcmp(version.data, "9P2000.L")) {
138360ce86c7SWei Liu         s->proto_version = V9FS_PROTO_2000L;
138460ce86c7SWei Liu     } else {
138560ce86c7SWei Liu         v9fs_string_sprintf(&version, "unknown");
1386e16453a3SChristian Schoenebeck         /* skip min. msize check, reporting invalid version has priority */
1387e16453a3SChristian Schoenebeck         goto marshal;
138860ce86c7SWei Liu     }
138960ce86c7SWei Liu 
1390e16453a3SChristian Schoenebeck     if (s->msize < P9_MIN_MSIZE) {
1391e16453a3SChristian Schoenebeck         err = -EMSGSIZE;
1392e16453a3SChristian Schoenebeck         error_report(
1393e16453a3SChristian Schoenebeck             "9pfs: Client requested msize < minimum msize ("
1394e16453a3SChristian Schoenebeck             stringify(P9_MIN_MSIZE) ") supported by this server."
1395e16453a3SChristian Schoenebeck         );
1396e16453a3SChristian Schoenebeck         goto out;
1397e16453a3SChristian Schoenebeck     }
1398e16453a3SChristian Schoenebeck 
139962777d82SChristian Schoenebeck     /* 8192 is the default msize of Linux clients */
1400c418f935SChristian Schoenebeck     if (s->msize <= 8192 && !(s->ctx.export_flags & V9FS_NO_PERF_WARN)) {
140162777d82SChristian Schoenebeck         warn_report_once(
140262777d82SChristian Schoenebeck             "9p: degraded performance: a reasonable high msize should be "
140362777d82SChristian Schoenebeck             "chosen on client/guest side (chosen msize is <= 8192). See "
140462777d82SChristian Schoenebeck             "https://wiki.qemu.org/Documentation/9psetup#msize for details."
140562777d82SChristian Schoenebeck         );
140662777d82SChristian Schoenebeck     }
140762777d82SChristian Schoenebeck 
1408e16453a3SChristian Schoenebeck marshal:
140960ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "ds", s->msize, &version);
141060ce86c7SWei Liu     if (err < 0) {
141160ce86c7SWei Liu         goto out;
141260ce86c7SWei Liu     }
1413403a905bSPhilippe Mathieu-Daudé     err += offset;
141460ce86c7SWei Liu     trace_v9fs_version_return(pdu->tag, pdu->id, s->msize, version.data);
141560ce86c7SWei Liu out:
1416403a905bSPhilippe Mathieu-Daudé     pdu_complete(pdu, err);
141760ce86c7SWei Liu     v9fs_string_free(&version);
141860ce86c7SWei Liu }
141960ce86c7SWei Liu 
14208440e22eSGreg Kurz static void coroutine_fn v9fs_attach(void *opaque)
142160ce86c7SWei Liu {
142260ce86c7SWei Liu     V9fsPDU *pdu = opaque;
142360ce86c7SWei Liu     V9fsState *s = pdu->s;
142460ce86c7SWei Liu     int32_t fid, afid, n_uname;
142560ce86c7SWei Liu     V9fsString uname, aname;
142660ce86c7SWei Liu     V9fsFidState *fidp;
142760ce86c7SWei Liu     size_t offset = 7;
142860ce86c7SWei Liu     V9fsQID qid;
142960ce86c7SWei Liu     ssize_t err;
143011024375SChristian Schoenebeck     struct stat stbuf;
143160ce86c7SWei Liu 
143260ce86c7SWei Liu     v9fs_string_init(&uname);
143360ce86c7SWei Liu     v9fs_string_init(&aname);
143460ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "ddssd", &fid,
143560ce86c7SWei Liu                         &afid, &uname, &aname, &n_uname);
143660ce86c7SWei Liu     if (err < 0) {
143760ce86c7SWei Liu         goto out_nofid;
143860ce86c7SWei Liu     }
143960ce86c7SWei Liu     trace_v9fs_attach(pdu->tag, pdu->id, fid, afid, uname.data, aname.data);
144060ce86c7SWei Liu 
144160ce86c7SWei Liu     fidp = alloc_fid(s, fid);
144260ce86c7SWei Liu     if (fidp == NULL) {
144360ce86c7SWei Liu         err = -EINVAL;
144460ce86c7SWei Liu         goto out_nofid;
144560ce86c7SWei Liu     }
144660ce86c7SWei Liu     fidp->uid = n_uname;
144760ce86c7SWei Liu     err = v9fs_co_name_to_path(pdu, NULL, "/", &fidp->path);
144860ce86c7SWei Liu     if (err < 0) {
144960ce86c7SWei Liu         err = -EINVAL;
145060ce86c7SWei Liu         clunk_fid(s, fid);
145160ce86c7SWei Liu         goto out;
145260ce86c7SWei Liu     }
145311024375SChristian Schoenebeck     err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
145411024375SChristian Schoenebeck     if (err < 0) {
145511024375SChristian Schoenebeck         err = -EINVAL;
145611024375SChristian Schoenebeck         clunk_fid(s, fid);
145711024375SChristian Schoenebeck         goto out;
145811024375SChristian Schoenebeck     }
145911024375SChristian Schoenebeck     err = stat_to_qid(pdu, &stbuf, &qid);
146060ce86c7SWei Liu     if (err < 0) {
146160ce86c7SWei Liu         err = -EINVAL;
146260ce86c7SWei Liu         clunk_fid(s, fid);
146360ce86c7SWei Liu         goto out;
146460ce86c7SWei Liu     }
1465fe44dc91SAshijeet Acharya 
1466fe44dc91SAshijeet Acharya     /*
1467fe44dc91SAshijeet Acharya      * disable migration if we haven't done already.
1468fe44dc91SAshijeet Acharya      * attach could get called multiple times for the same export.
1469fe44dc91SAshijeet Acharya      */
1470fe44dc91SAshijeet Acharya     if (!s->migration_blocker) {
1471fe44dc91SAshijeet Acharya         error_setg(&s->migration_blocker,
1472fe44dc91SAshijeet Acharya                    "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1473fe44dc91SAshijeet Acharya                    s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag);
14749261ef5eSMarkus Armbruster         err = migrate_add_blocker(s->migration_blocker, NULL);
14759261ef5eSMarkus Armbruster         if (err < 0) {
1476fe44dc91SAshijeet Acharya             error_free(s->migration_blocker);
1477fe44dc91SAshijeet Acharya             s->migration_blocker = NULL;
1478fe44dc91SAshijeet Acharya             clunk_fid(s, fid);
1479fe44dc91SAshijeet Acharya             goto out;
1480fe44dc91SAshijeet Acharya         }
1481fe44dc91SAshijeet Acharya         s->root_fid = fid;
1482fe44dc91SAshijeet Acharya     }
1483fe44dc91SAshijeet Acharya 
148460ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "Q", &qid);
148560ce86c7SWei Liu     if (err < 0) {
148660ce86c7SWei Liu         clunk_fid(s, fid);
148760ce86c7SWei Liu         goto out;
148860ce86c7SWei Liu     }
148960ce86c7SWei Liu     err += offset;
1490fe44dc91SAshijeet Acharya 
149111024375SChristian Schoenebeck     memcpy(&s->root_st, &stbuf, sizeof(stbuf));
149260ce86c7SWei Liu     trace_v9fs_attach_return(pdu->tag, pdu->id,
149360ce86c7SWei Liu                              qid.type, qid.version, qid.path);
149460ce86c7SWei Liu out:
149560ce86c7SWei Liu     put_fid(pdu, fidp);
149660ce86c7SWei Liu out_nofid:
149760ce86c7SWei Liu     pdu_complete(pdu, err);
149860ce86c7SWei Liu     v9fs_string_free(&uname);
149960ce86c7SWei Liu     v9fs_string_free(&aname);
150060ce86c7SWei Liu }
150160ce86c7SWei Liu 
15028440e22eSGreg Kurz static void coroutine_fn v9fs_stat(void *opaque)
150360ce86c7SWei Liu {
150460ce86c7SWei Liu     int32_t fid;
150560ce86c7SWei Liu     V9fsStat v9stat;
150660ce86c7SWei Liu     ssize_t err = 0;
150760ce86c7SWei Liu     size_t offset = 7;
150860ce86c7SWei Liu     struct stat stbuf;
150960ce86c7SWei Liu     V9fsFidState *fidp;
151060ce86c7SWei Liu     V9fsPDU *pdu = opaque;
15116069537fSJan Dakinevich     char *basename;
151260ce86c7SWei Liu 
151360ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "d", &fid);
151460ce86c7SWei Liu     if (err < 0) {
151560ce86c7SWei Liu         goto out_nofid;
151660ce86c7SWei Liu     }
151760ce86c7SWei Liu     trace_v9fs_stat(pdu->tag, pdu->id, fid);
151860ce86c7SWei Liu 
151960ce86c7SWei Liu     fidp = get_fid(pdu, fid);
152060ce86c7SWei Liu     if (fidp == NULL) {
152160ce86c7SWei Liu         err = -ENOENT;
152260ce86c7SWei Liu         goto out_nofid;
152360ce86c7SWei Liu     }
152460ce86c7SWei Liu     err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
152560ce86c7SWei Liu     if (err < 0) {
152660ce86c7SWei Liu         goto out;
152760ce86c7SWei Liu     }
15286069537fSJan Dakinevich     basename = g_path_get_basename(fidp->path.data);
15296069537fSJan Dakinevich     err = stat_to_v9stat(pdu, &fidp->path, basename, &stbuf, &v9stat);
15306069537fSJan Dakinevich     g_free(basename);
153160ce86c7SWei Liu     if (err < 0) {
153260ce86c7SWei Liu         goto out;
153360ce86c7SWei Liu     }
153460ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "wS", 0, &v9stat);
153560ce86c7SWei Liu     if (err < 0) {
153660ce86c7SWei Liu         v9fs_stat_free(&v9stat);
153760ce86c7SWei Liu         goto out;
153860ce86c7SWei Liu     }
153960ce86c7SWei Liu     trace_v9fs_stat_return(pdu->tag, pdu->id, v9stat.mode,
154060ce86c7SWei Liu                            v9stat.atime, v9stat.mtime, v9stat.length);
154160ce86c7SWei Liu     err += offset;
154260ce86c7SWei Liu     v9fs_stat_free(&v9stat);
154360ce86c7SWei Liu out:
154460ce86c7SWei Liu     put_fid(pdu, fidp);
154560ce86c7SWei Liu out_nofid:
154660ce86c7SWei Liu     pdu_complete(pdu, err);
154760ce86c7SWei Liu }
154860ce86c7SWei Liu 
15498440e22eSGreg Kurz static void coroutine_fn v9fs_getattr(void *opaque)
155060ce86c7SWei Liu {
155160ce86c7SWei Liu     int32_t fid;
155260ce86c7SWei Liu     size_t offset = 7;
155360ce86c7SWei Liu     ssize_t retval = 0;
155460ce86c7SWei Liu     struct stat stbuf;
155560ce86c7SWei Liu     V9fsFidState *fidp;
155660ce86c7SWei Liu     uint64_t request_mask;
155760ce86c7SWei Liu     V9fsStatDotl v9stat_dotl;
155860ce86c7SWei Liu     V9fsPDU *pdu = opaque;
155960ce86c7SWei Liu 
156060ce86c7SWei Liu     retval = pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask);
156160ce86c7SWei Liu     if (retval < 0) {
156260ce86c7SWei Liu         goto out_nofid;
156360ce86c7SWei Liu     }
156460ce86c7SWei Liu     trace_v9fs_getattr(pdu->tag, pdu->id, fid, request_mask);
156560ce86c7SWei Liu 
156660ce86c7SWei Liu     fidp = get_fid(pdu, fid);
156760ce86c7SWei Liu     if (fidp == NULL) {
156860ce86c7SWei Liu         retval = -ENOENT;
156960ce86c7SWei Liu         goto out_nofid;
157060ce86c7SWei Liu     }
157160ce86c7SWei Liu     /*
157260ce86c7SWei Liu      * Currently we only support BASIC fields in stat, so there is no
157360ce86c7SWei Liu      * need to look at request_mask.
157460ce86c7SWei Liu      */
157560ce86c7SWei Liu     retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
157660ce86c7SWei Liu     if (retval < 0) {
157760ce86c7SWei Liu         goto out;
157860ce86c7SWei Liu     }
15793b5ee9e8SAntonios Motakis     retval = stat_to_v9stat_dotl(pdu, &stbuf, &v9stat_dotl);
15803b5ee9e8SAntonios Motakis     if (retval < 0) {
15813b5ee9e8SAntonios Motakis         goto out;
15823b5ee9e8SAntonios Motakis     }
158360ce86c7SWei Liu 
158460ce86c7SWei Liu     /*  fill st_gen if requested and supported by underlying fs */
158560ce86c7SWei Liu     if (request_mask & P9_STATS_GEN) {
158660ce86c7SWei Liu         retval = v9fs_co_st_gen(pdu, &fidp->path, stbuf.st_mode, &v9stat_dotl);
158760ce86c7SWei Liu         switch (retval) {
158860ce86c7SWei Liu         case 0:
158960ce86c7SWei Liu             /* we have valid st_gen: update result mask */
159060ce86c7SWei Liu             v9stat_dotl.st_result_mask |= P9_STATS_GEN;
159160ce86c7SWei Liu             break;
159260ce86c7SWei Liu         case -EINTR:
159360ce86c7SWei Liu             /* request cancelled, e.g. by Tflush */
159460ce86c7SWei Liu             goto out;
159560ce86c7SWei Liu         default:
159660ce86c7SWei Liu             /* failed to get st_gen: not fatal, ignore */
159760ce86c7SWei Liu             break;
159860ce86c7SWei Liu         }
159960ce86c7SWei Liu     }
160060ce86c7SWei Liu     retval = pdu_marshal(pdu, offset, "A", &v9stat_dotl);
160160ce86c7SWei Liu     if (retval < 0) {
160260ce86c7SWei Liu         goto out;
160360ce86c7SWei Liu     }
160460ce86c7SWei Liu     retval += offset;
160560ce86c7SWei Liu     trace_v9fs_getattr_return(pdu->tag, pdu->id, v9stat_dotl.st_result_mask,
160660ce86c7SWei Liu                               v9stat_dotl.st_mode, v9stat_dotl.st_uid,
160760ce86c7SWei Liu                               v9stat_dotl.st_gid);
160860ce86c7SWei Liu out:
160960ce86c7SWei Liu     put_fid(pdu, fidp);
161060ce86c7SWei Liu out_nofid:
161160ce86c7SWei Liu     pdu_complete(pdu, retval);
161260ce86c7SWei Liu }
161360ce86c7SWei Liu 
161460ce86c7SWei Liu /* Attribute flags */
161560ce86c7SWei Liu #define P9_ATTR_MODE       (1 << 0)
161660ce86c7SWei Liu #define P9_ATTR_UID        (1 << 1)
161760ce86c7SWei Liu #define P9_ATTR_GID        (1 << 2)
161860ce86c7SWei Liu #define P9_ATTR_SIZE       (1 << 3)
161960ce86c7SWei Liu #define P9_ATTR_ATIME      (1 << 4)
162060ce86c7SWei Liu #define P9_ATTR_MTIME      (1 << 5)
162160ce86c7SWei Liu #define P9_ATTR_CTIME      (1 << 6)
162260ce86c7SWei Liu #define P9_ATTR_ATIME_SET  (1 << 7)
162360ce86c7SWei Liu #define P9_ATTR_MTIME_SET  (1 << 8)
162460ce86c7SWei Liu 
162560ce86c7SWei Liu #define P9_ATTR_MASK    127
162660ce86c7SWei Liu 
16278440e22eSGreg Kurz static void coroutine_fn v9fs_setattr(void *opaque)
162860ce86c7SWei Liu {
162960ce86c7SWei Liu     int err = 0;
163060ce86c7SWei Liu     int32_t fid;
163160ce86c7SWei Liu     V9fsFidState *fidp;
163260ce86c7SWei Liu     size_t offset = 7;
163360ce86c7SWei Liu     V9fsIattr v9iattr;
163460ce86c7SWei Liu     V9fsPDU *pdu = opaque;
163560ce86c7SWei Liu 
163660ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr);
163760ce86c7SWei Liu     if (err < 0) {
163860ce86c7SWei Liu         goto out_nofid;
163960ce86c7SWei Liu     }
164060ce86c7SWei Liu 
16418f9c64bfSGreg Kurz     trace_v9fs_setattr(pdu->tag, pdu->id, fid,
16428f9c64bfSGreg Kurz                        v9iattr.valid, v9iattr.mode, v9iattr.uid, v9iattr.gid,
16438f9c64bfSGreg Kurz                        v9iattr.size, v9iattr.atime_sec, v9iattr.mtime_sec);
16448f9c64bfSGreg Kurz 
164560ce86c7SWei Liu     fidp = get_fid(pdu, fid);
164660ce86c7SWei Liu     if (fidp == NULL) {
164760ce86c7SWei Liu         err = -EINVAL;
164860ce86c7SWei Liu         goto out_nofid;
164960ce86c7SWei Liu     }
165060ce86c7SWei Liu     if (v9iattr.valid & P9_ATTR_MODE) {
165160ce86c7SWei Liu         err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode);
165260ce86c7SWei Liu         if (err < 0) {
165360ce86c7SWei Liu             goto out;
165460ce86c7SWei Liu         }
165560ce86c7SWei Liu     }
165660ce86c7SWei Liu     if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) {
165760ce86c7SWei Liu         struct timespec times[2];
165860ce86c7SWei Liu         if (v9iattr.valid & P9_ATTR_ATIME) {
165960ce86c7SWei Liu             if (v9iattr.valid & P9_ATTR_ATIME_SET) {
166060ce86c7SWei Liu                 times[0].tv_sec = v9iattr.atime_sec;
166160ce86c7SWei Liu                 times[0].tv_nsec = v9iattr.atime_nsec;
166260ce86c7SWei Liu             } else {
166360ce86c7SWei Liu                 times[0].tv_nsec = UTIME_NOW;
166460ce86c7SWei Liu             }
166560ce86c7SWei Liu         } else {
166660ce86c7SWei Liu             times[0].tv_nsec = UTIME_OMIT;
166760ce86c7SWei Liu         }
166860ce86c7SWei Liu         if (v9iattr.valid & P9_ATTR_MTIME) {
166960ce86c7SWei Liu             if (v9iattr.valid & P9_ATTR_MTIME_SET) {
167060ce86c7SWei Liu                 times[1].tv_sec = v9iattr.mtime_sec;
167160ce86c7SWei Liu                 times[1].tv_nsec = v9iattr.mtime_nsec;
167260ce86c7SWei Liu             } else {
167360ce86c7SWei Liu                 times[1].tv_nsec = UTIME_NOW;
167460ce86c7SWei Liu             }
167560ce86c7SWei Liu         } else {
167660ce86c7SWei Liu             times[1].tv_nsec = UTIME_OMIT;
167760ce86c7SWei Liu         }
167860ce86c7SWei Liu         err = v9fs_co_utimensat(pdu, &fidp->path, times);
167960ce86c7SWei Liu         if (err < 0) {
168060ce86c7SWei Liu             goto out;
168160ce86c7SWei Liu         }
168260ce86c7SWei Liu     }
168360ce86c7SWei Liu     /*
168460ce86c7SWei Liu      * If the only valid entry in iattr is ctime we can call
168560ce86c7SWei Liu      * chown(-1,-1) to update the ctime of the file
168660ce86c7SWei Liu      */
168760ce86c7SWei Liu     if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) ||
168860ce86c7SWei Liu         ((v9iattr.valid & P9_ATTR_CTIME)
168960ce86c7SWei Liu          && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) {
169060ce86c7SWei Liu         if (!(v9iattr.valid & P9_ATTR_UID)) {
169160ce86c7SWei Liu             v9iattr.uid = -1;
169260ce86c7SWei Liu         }
169360ce86c7SWei Liu         if (!(v9iattr.valid & P9_ATTR_GID)) {
169460ce86c7SWei Liu             v9iattr.gid = -1;
169560ce86c7SWei Liu         }
169660ce86c7SWei Liu         err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid,
169760ce86c7SWei Liu                             v9iattr.gid);
169860ce86c7SWei Liu         if (err < 0) {
169960ce86c7SWei Liu             goto out;
170060ce86c7SWei Liu         }
170160ce86c7SWei Liu     }
170260ce86c7SWei Liu     if (v9iattr.valid & (P9_ATTR_SIZE)) {
170360ce86c7SWei Liu         err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size);
170460ce86c7SWei Liu         if (err < 0) {
170560ce86c7SWei Liu             goto out;
170660ce86c7SWei Liu         }
170760ce86c7SWei Liu     }
170860ce86c7SWei Liu     err = offset;
17098f9c64bfSGreg Kurz     trace_v9fs_setattr_return(pdu->tag, pdu->id);
171060ce86c7SWei Liu out:
171160ce86c7SWei Liu     put_fid(pdu, fidp);
171260ce86c7SWei Liu out_nofid:
171360ce86c7SWei Liu     pdu_complete(pdu, err);
171460ce86c7SWei Liu }
171560ce86c7SWei Liu 
171660ce86c7SWei Liu static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids)
171760ce86c7SWei Liu {
171860ce86c7SWei Liu     int i;
171960ce86c7SWei Liu     ssize_t err;
172060ce86c7SWei Liu     size_t offset = 7;
172160ce86c7SWei Liu 
172260ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "w", nwnames);
172360ce86c7SWei Liu     if (err < 0) {
172460ce86c7SWei Liu         return err;
172560ce86c7SWei Liu     }
172660ce86c7SWei Liu     offset += err;
172760ce86c7SWei Liu     for (i = 0; i < nwnames; i++) {
172860ce86c7SWei Liu         err = pdu_marshal(pdu, offset, "Q", &qids[i]);
172960ce86c7SWei Liu         if (err < 0) {
173060ce86c7SWei Liu             return err;
173160ce86c7SWei Liu         }
173260ce86c7SWei Liu         offset += err;
173360ce86c7SWei Liu     }
173460ce86c7SWei Liu     return offset;
173560ce86c7SWei Liu }
173660ce86c7SWei Liu 
1737fff39a7aSGreg Kurz static bool name_is_illegal(const char *name)
1738fff39a7aSGreg Kurz {
1739fff39a7aSGreg Kurz     return !*name || strchr(name, '/') != NULL;
1740fff39a7aSGreg Kurz }
1741fff39a7aSGreg Kurz 
1742f22cad42SChristian Schoenebeck static bool same_stat_id(const struct stat *a, const struct stat *b)
174356f101ecSGreg Kurz {
1744f22cad42SChristian Schoenebeck     return a->st_dev == b->st_dev && a->st_ino == b->st_ino;
174556f101ecSGreg Kurz }
174656f101ecSGreg Kurz 
17478440e22eSGreg Kurz static void coroutine_fn v9fs_walk(void *opaque)
174860ce86c7SWei Liu {
174960ce86c7SWei Liu     int name_idx;
1750869605b5SChristian Schoenebeck     g_autofree V9fsQID *qids = NULL;
175160ce86c7SWei Liu     int i, err = 0;
17527e985780SChristian Schoenebeck     V9fsPath dpath, path;
17537e985780SChristian Schoenebeck     P9ARRAY_REF(V9fsPath) pathes = NULL;
175460ce86c7SWei Liu     uint16_t nwnames;
1755869605b5SChristian Schoenebeck     struct stat stbuf, fidst;
1756869605b5SChristian Schoenebeck     g_autofree struct stat *stbufs = NULL;
175760ce86c7SWei Liu     size_t offset = 7;
175860ce86c7SWei Liu     int32_t fid, newfid;
17597e985780SChristian Schoenebeck     P9ARRAY_REF(V9fsString) wnames = NULL;
176060ce86c7SWei Liu     V9fsFidState *fidp;
176160ce86c7SWei Liu     V9fsFidState *newfidp = NULL;
176260ce86c7SWei Liu     V9fsPDU *pdu = opaque;
176360ce86c7SWei Liu     V9fsState *s = pdu->s;
176456f101ecSGreg Kurz     V9fsQID qid;
176560ce86c7SWei Liu 
176660ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames);
176760ce86c7SWei Liu     if (err < 0) {
176860ce86c7SWei Liu         pdu_complete(pdu, err);
176960ce86c7SWei Liu         return ;
177060ce86c7SWei Liu     }
177160ce86c7SWei Liu     offset += err;
177260ce86c7SWei Liu 
177360ce86c7SWei Liu     trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames);
177460ce86c7SWei Liu 
1775232a4d2cSChristian Schoenebeck     if (nwnames > P9_MAXWELEM) {
1776232a4d2cSChristian Schoenebeck         err = -EINVAL;
1777232a4d2cSChristian Schoenebeck         goto out_nofid;
1778232a4d2cSChristian Schoenebeck     }
1779232a4d2cSChristian Schoenebeck     if (nwnames) {
17807e985780SChristian Schoenebeck         P9ARRAY_NEW(V9fsString, wnames, nwnames);
17811923923bSGreg Kurz         qids   = g_new0(V9fsQID, nwnames);
17828d6cb100SChristian Schoenebeck         stbufs = g_new0(struct stat, nwnames);
17837e985780SChristian Schoenebeck         P9ARRAY_NEW(V9fsPath, pathes, nwnames);
178460ce86c7SWei Liu         for (i = 0; i < nwnames; i++) {
178560ce86c7SWei Liu             err = pdu_unmarshal(pdu, offset, "s", &wnames[i]);
178660ce86c7SWei Liu             if (err < 0) {
178760ce86c7SWei Liu                 goto out_nofid;
178860ce86c7SWei Liu             }
1789fff39a7aSGreg Kurz             if (name_is_illegal(wnames[i].data)) {
1790fff39a7aSGreg Kurz                 err = -ENOENT;
1791fff39a7aSGreg Kurz                 goto out_nofid;
1792fff39a7aSGreg Kurz             }
179360ce86c7SWei Liu             offset += err;
179460ce86c7SWei Liu         }
179560ce86c7SWei Liu     }
179660ce86c7SWei Liu     fidp = get_fid(pdu, fid);
179760ce86c7SWei Liu     if (fidp == NULL) {
179860ce86c7SWei Liu         err = -ENOENT;
179960ce86c7SWei Liu         goto out_nofid;
180060ce86c7SWei Liu     }
180156f101ecSGreg Kurz 
180213fd08e6SGreg Kurz     v9fs_path_init(&dpath);
180313fd08e6SGreg Kurz     v9fs_path_init(&path);
180460ce86c7SWei Liu     /*
18058d6cb100SChristian Schoenebeck      * Both dpath and path initially point to fidp.
180660ce86c7SWei Liu      * Needed to handle request with nwnames == 0
180760ce86c7SWei Liu      */
180860ce86c7SWei Liu     v9fs_path_copy(&dpath, &fidp->path);
180960ce86c7SWei Liu     v9fs_path_copy(&path, &fidp->path);
18108d6cb100SChristian Schoenebeck 
18118d6cb100SChristian Schoenebeck     /*
18128d6cb100SChristian Schoenebeck      * To keep latency (i.e. overall execution time for processing this
18138d6cb100SChristian Schoenebeck      * Twalk client request) as small as possible, run all the required fs
18148d6cb100SChristian Schoenebeck      * driver code altogether inside the following block.
18158d6cb100SChristian Schoenebeck      */
18168d6cb100SChristian Schoenebeck     v9fs_co_run_in_worker({
18178d6cb100SChristian Schoenebeck         if (v9fs_request_cancelled(pdu)) {
18188d6cb100SChristian Schoenebeck             err = -EINTR;
18198d6cb100SChristian Schoenebeck             break;
18208d6cb100SChristian Schoenebeck         }
18218d6cb100SChristian Schoenebeck         err = s->ops->lstat(&s->ctx, &dpath, &fidst);
18228d6cb100SChristian Schoenebeck         if (err < 0) {
18238d6cb100SChristian Schoenebeck             err = -errno;
18248d6cb100SChristian Schoenebeck             break;
18258d6cb100SChristian Schoenebeck         }
18268d6cb100SChristian Schoenebeck         stbuf = fidst;
182760ce86c7SWei Liu         for (name_idx = 0; name_idx < nwnames; name_idx++) {
18288d6cb100SChristian Schoenebeck             if (v9fs_request_cancelled(pdu)) {
18298d6cb100SChristian Schoenebeck                 err = -EINTR;
18308d6cb100SChristian Schoenebeck                 break;
18318d6cb100SChristian Schoenebeck             }
1832f22cad42SChristian Schoenebeck             if (!same_stat_id(&pdu->s->root_st, &stbuf) ||
18338d6cb100SChristian Schoenebeck                 strcmp("..", wnames[name_idx].data))
18348d6cb100SChristian Schoenebeck             {
18358d6cb100SChristian Schoenebeck                 err = s->ops->name_to_path(&s->ctx, &dpath,
183697b1d8fdSChristian Schoenebeck                                            wnames[name_idx].data,
183797b1d8fdSChristian Schoenebeck                                            &pathes[name_idx]);
18388d6cb100SChristian Schoenebeck                 if (err < 0) {
18398d6cb100SChristian Schoenebeck                     err = -errno;
18408d6cb100SChristian Schoenebeck                     break;
18418d6cb100SChristian Schoenebeck                 }
18428d6cb100SChristian Schoenebeck                 if (v9fs_request_cancelled(pdu)) {
18438d6cb100SChristian Schoenebeck                     err = -EINTR;
18448d6cb100SChristian Schoenebeck                     break;
18458d6cb100SChristian Schoenebeck                 }
184697b1d8fdSChristian Schoenebeck                 err = s->ops->lstat(&s->ctx, &pathes[name_idx], &stbuf);
18478d6cb100SChristian Schoenebeck                 if (err < 0) {
18488d6cb100SChristian Schoenebeck                     err = -errno;
18498d6cb100SChristian Schoenebeck                     break;
18508d6cb100SChristian Schoenebeck                 }
18518d6cb100SChristian Schoenebeck                 stbufs[name_idx] = stbuf;
185297b1d8fdSChristian Schoenebeck                 v9fs_path_copy(&dpath, &pathes[name_idx]);
18538d6cb100SChristian Schoenebeck             }
18548d6cb100SChristian Schoenebeck         }
18558d6cb100SChristian Schoenebeck     });
18568d6cb100SChristian Schoenebeck     /*
18578d6cb100SChristian Schoenebeck      * Handle all the rest of this Twalk request on main thread ...
18588d6cb100SChristian Schoenebeck      */
185960ce86c7SWei Liu     if (err < 0) {
186060ce86c7SWei Liu         goto out;
186160ce86c7SWei Liu     }
186256f101ecSGreg Kurz 
18638d6cb100SChristian Schoenebeck     err = stat_to_qid(pdu, &fidst, &qid);
186460ce86c7SWei Liu     if (err < 0) {
186560ce86c7SWei Liu         goto out;
186660ce86c7SWei Liu     }
18678d6cb100SChristian Schoenebeck     stbuf = fidst;
18688d6cb100SChristian Schoenebeck 
18698d6cb100SChristian Schoenebeck     /* reset dpath and path */
18708d6cb100SChristian Schoenebeck     v9fs_path_copy(&dpath, &fidp->path);
18718d6cb100SChristian Schoenebeck     v9fs_path_copy(&path, &fidp->path);
18728d6cb100SChristian Schoenebeck 
18738d6cb100SChristian Schoenebeck     for (name_idx = 0; name_idx < nwnames; name_idx++) {
18748d6cb100SChristian Schoenebeck         if (!same_stat_id(&pdu->s->root_st, &stbuf) ||
18758d6cb100SChristian Schoenebeck             strcmp("..", wnames[name_idx].data))
18768d6cb100SChristian Schoenebeck         {
18778d6cb100SChristian Schoenebeck             stbuf = stbufs[name_idx];
18783b5ee9e8SAntonios Motakis             err = stat_to_qid(pdu, &stbuf, &qid);
18793b5ee9e8SAntonios Motakis             if (err < 0) {
18803b5ee9e8SAntonios Motakis                 goto out;
18813b5ee9e8SAntonios Motakis             }
18828d6cb100SChristian Schoenebeck             v9fs_path_copy(&path, &pathes[name_idx]);
188360ce86c7SWei Liu             v9fs_path_copy(&dpath, &path);
188460ce86c7SWei Liu         }
188556f101ecSGreg Kurz         memcpy(&qids[name_idx], &qid, sizeof(qid));
188656f101ecSGreg Kurz     }
188760ce86c7SWei Liu     if (fid == newfid) {
188849dd946bSGreg Kurz         if (fidp->fid_type != P9_FID_NONE) {
188949dd946bSGreg Kurz             err = -EINVAL;
189049dd946bSGreg Kurz             goto out;
189149dd946bSGreg Kurz         }
18925b3c77aaSGreg Kurz         v9fs_path_write_lock(s);
189360ce86c7SWei Liu         v9fs_path_copy(&fidp->path, &path);
18945b3c77aaSGreg Kurz         v9fs_path_unlock(s);
189560ce86c7SWei Liu     } else {
189660ce86c7SWei Liu         newfidp = alloc_fid(s, newfid);
189760ce86c7SWei Liu         if (newfidp == NULL) {
189860ce86c7SWei Liu             err = -EINVAL;
189960ce86c7SWei Liu             goto out;
190060ce86c7SWei Liu         }
190160ce86c7SWei Liu         newfidp->uid = fidp->uid;
190260ce86c7SWei Liu         v9fs_path_copy(&newfidp->path, &path);
190360ce86c7SWei Liu     }
190460ce86c7SWei Liu     err = v9fs_walk_marshal(pdu, nwnames, qids);
190560ce86c7SWei Liu     trace_v9fs_walk_return(pdu->tag, pdu->id, nwnames, qids);
190660ce86c7SWei Liu out:
190760ce86c7SWei Liu     put_fid(pdu, fidp);
190860ce86c7SWei Liu     if (newfidp) {
190960ce86c7SWei Liu         put_fid(pdu, newfidp);
191060ce86c7SWei Liu     }
191160ce86c7SWei Liu     v9fs_path_free(&dpath);
191260ce86c7SWei Liu     v9fs_path_free(&path);
191360ce86c7SWei Liu out_nofid:
191460ce86c7SWei Liu     pdu_complete(pdu, err);
191560ce86c7SWei Liu }
191660ce86c7SWei Liu 
19178440e22eSGreg Kurz static int32_t coroutine_fn get_iounit(V9fsPDU *pdu, V9fsPath *path)
191860ce86c7SWei Liu {
191960ce86c7SWei Liu     struct statfs stbuf;
1920b565bccbSChristian Schoenebeck     int err = v9fs_co_statfs(pdu, path, &stbuf);
192160ce86c7SWei Liu 
1922b565bccbSChristian Schoenebeck     return blksize_to_iounit(pdu, (err >= 0) ? stbuf.f_bsize : 0);
192360ce86c7SWei Liu }
192460ce86c7SWei Liu 
19258440e22eSGreg Kurz static void coroutine_fn v9fs_open(void *opaque)
192660ce86c7SWei Liu {
192760ce86c7SWei Liu     int flags;
192860ce86c7SWei Liu     int32_t fid;
192960ce86c7SWei Liu     int32_t mode;
193060ce86c7SWei Liu     V9fsQID qid;
193160ce86c7SWei Liu     int iounit = 0;
193260ce86c7SWei Liu     ssize_t err = 0;
193360ce86c7SWei Liu     size_t offset = 7;
193460ce86c7SWei Liu     struct stat stbuf;
193560ce86c7SWei Liu     V9fsFidState *fidp;
193660ce86c7SWei Liu     V9fsPDU *pdu = opaque;
193760ce86c7SWei Liu     V9fsState *s = pdu->s;
193860ce86c7SWei Liu 
193960ce86c7SWei Liu     if (s->proto_version == V9FS_PROTO_2000L) {
194060ce86c7SWei Liu         err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode);
194160ce86c7SWei Liu     } else {
194260ce86c7SWei Liu         uint8_t modebyte;
194360ce86c7SWei Liu         err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte);
194460ce86c7SWei Liu         mode = modebyte;
194560ce86c7SWei Liu     }
194660ce86c7SWei Liu     if (err < 0) {
194760ce86c7SWei Liu         goto out_nofid;
194860ce86c7SWei Liu     }
194960ce86c7SWei Liu     trace_v9fs_open(pdu->tag, pdu->id, fid, mode);
195060ce86c7SWei Liu 
195160ce86c7SWei Liu     fidp = get_fid(pdu, fid);
195260ce86c7SWei Liu     if (fidp == NULL) {
195360ce86c7SWei Liu         err = -ENOENT;
195460ce86c7SWei Liu         goto out_nofid;
195560ce86c7SWei Liu     }
195649dd946bSGreg Kurz     if (fidp->fid_type != P9_FID_NONE) {
195749dd946bSGreg Kurz         err = -EINVAL;
195849dd946bSGreg Kurz         goto out;
195949dd946bSGreg Kurz     }
196060ce86c7SWei Liu 
196160ce86c7SWei Liu     err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
196260ce86c7SWei Liu     if (err < 0) {
196360ce86c7SWei Liu         goto out;
196460ce86c7SWei Liu     }
19653b5ee9e8SAntonios Motakis     err = stat_to_qid(pdu, &stbuf, &qid);
19663b5ee9e8SAntonios Motakis     if (err < 0) {
19673b5ee9e8SAntonios Motakis         goto out;
19683b5ee9e8SAntonios Motakis     }
196960ce86c7SWei Liu     if (S_ISDIR(stbuf.st_mode)) {
197060ce86c7SWei Liu         err = v9fs_co_opendir(pdu, fidp);
197160ce86c7SWei Liu         if (err < 0) {
197260ce86c7SWei Liu             goto out;
197360ce86c7SWei Liu         }
197460ce86c7SWei Liu         fidp->fid_type = P9_FID_DIR;
197560ce86c7SWei Liu         err = pdu_marshal(pdu, offset, "Qd", &qid, 0);
197660ce86c7SWei Liu         if (err < 0) {
197760ce86c7SWei Liu             goto out;
197860ce86c7SWei Liu         }
197960ce86c7SWei Liu         err += offset;
198060ce86c7SWei Liu     } else {
198160ce86c7SWei Liu         if (s->proto_version == V9FS_PROTO_2000L) {
198260ce86c7SWei Liu             flags = get_dotl_openflags(s, mode);
198360ce86c7SWei Liu         } else {
198460ce86c7SWei Liu             flags = omode_to_uflags(mode);
198560ce86c7SWei Liu         }
198660ce86c7SWei Liu         if (is_ro_export(&s->ctx)) {
198760ce86c7SWei Liu             if (mode & O_WRONLY || mode & O_RDWR ||
198860ce86c7SWei Liu                 mode & O_APPEND || mode & O_TRUNC) {
198960ce86c7SWei Liu                 err = -EROFS;
199060ce86c7SWei Liu                 goto out;
199160ce86c7SWei Liu             }
199260ce86c7SWei Liu         }
199360ce86c7SWei Liu         err = v9fs_co_open(pdu, fidp, flags);
199460ce86c7SWei Liu         if (err < 0) {
199560ce86c7SWei Liu             goto out;
199660ce86c7SWei Liu         }
199760ce86c7SWei Liu         fidp->fid_type = P9_FID_FILE;
199860ce86c7SWei Liu         fidp->open_flags = flags;
199960ce86c7SWei Liu         if (flags & O_EXCL) {
200060ce86c7SWei Liu             /*
200160ce86c7SWei Liu              * We let the host file system do O_EXCL check
200260ce86c7SWei Liu              * We should not reclaim such fd
200360ce86c7SWei Liu              */
200460ce86c7SWei Liu             fidp->flags |= FID_NON_RECLAIMABLE;
200560ce86c7SWei Liu         }
200660ce86c7SWei Liu         iounit = get_iounit(pdu, &fidp->path);
200760ce86c7SWei Liu         err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
200860ce86c7SWei Liu         if (err < 0) {
200960ce86c7SWei Liu             goto out;
201060ce86c7SWei Liu         }
201160ce86c7SWei Liu         err += offset;
201260ce86c7SWei Liu     }
201360ce86c7SWei Liu     trace_v9fs_open_return(pdu->tag, pdu->id,
201460ce86c7SWei Liu                            qid.type, qid.version, qid.path, iounit);
201560ce86c7SWei Liu out:
201660ce86c7SWei Liu     put_fid(pdu, fidp);
201760ce86c7SWei Liu out_nofid:
201860ce86c7SWei Liu     pdu_complete(pdu, err);
201960ce86c7SWei Liu }
202060ce86c7SWei Liu 
20218440e22eSGreg Kurz static void coroutine_fn v9fs_lcreate(void *opaque)
202260ce86c7SWei Liu {
202360ce86c7SWei Liu     int32_t dfid, flags, mode;
202460ce86c7SWei Liu     gid_t gid;
202560ce86c7SWei Liu     ssize_t err = 0;
202660ce86c7SWei Liu     ssize_t offset = 7;
202760ce86c7SWei Liu     V9fsString name;
202860ce86c7SWei Liu     V9fsFidState *fidp;
202960ce86c7SWei Liu     struct stat stbuf;
203060ce86c7SWei Liu     V9fsQID qid;
203160ce86c7SWei Liu     int32_t iounit;
203260ce86c7SWei Liu     V9fsPDU *pdu = opaque;
203360ce86c7SWei Liu 
203460ce86c7SWei Liu     v9fs_string_init(&name);
203560ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dsddd", &dfid,
203660ce86c7SWei Liu                         &name, &flags, &mode, &gid);
203760ce86c7SWei Liu     if (err < 0) {
203860ce86c7SWei Liu         goto out_nofid;
203960ce86c7SWei Liu     }
204060ce86c7SWei Liu     trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid);
204160ce86c7SWei Liu 
2042fff39a7aSGreg Kurz     if (name_is_illegal(name.data)) {
2043fff39a7aSGreg Kurz         err = -ENOENT;
2044fff39a7aSGreg Kurz         goto out_nofid;
2045fff39a7aSGreg Kurz     }
2046fff39a7aSGreg Kurz 
2047805b5d98SGreg Kurz     if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2048805b5d98SGreg Kurz         err = -EEXIST;
2049805b5d98SGreg Kurz         goto out_nofid;
2050805b5d98SGreg Kurz     }
2051805b5d98SGreg Kurz 
205260ce86c7SWei Liu     fidp = get_fid(pdu, dfid);
205360ce86c7SWei Liu     if (fidp == NULL) {
205460ce86c7SWei Liu         err = -ENOENT;
205560ce86c7SWei Liu         goto out_nofid;
205660ce86c7SWei Liu     }
2057d63fb193SLi Qiang     if (fidp->fid_type != P9_FID_NONE) {
2058d63fb193SLi Qiang         err = -EINVAL;
2059d63fb193SLi Qiang         goto out;
2060d63fb193SLi Qiang     }
206160ce86c7SWei Liu 
206260ce86c7SWei Liu     flags = get_dotl_openflags(pdu->s, flags);
206360ce86c7SWei Liu     err = v9fs_co_open2(pdu, fidp, &name, gid,
206460ce86c7SWei Liu                         flags | O_CREAT, mode, &stbuf);
206560ce86c7SWei Liu     if (err < 0) {
206660ce86c7SWei Liu         goto out;
206760ce86c7SWei Liu     }
206860ce86c7SWei Liu     fidp->fid_type = P9_FID_FILE;
206960ce86c7SWei Liu     fidp->open_flags = flags;
207060ce86c7SWei Liu     if (flags & O_EXCL) {
207160ce86c7SWei Liu         /*
207260ce86c7SWei Liu          * We let the host file system do O_EXCL check
207360ce86c7SWei Liu          * We should not reclaim such fd
207460ce86c7SWei Liu          */
207560ce86c7SWei Liu         fidp->flags |= FID_NON_RECLAIMABLE;
207660ce86c7SWei Liu     }
207760ce86c7SWei Liu     iounit =  get_iounit(pdu, &fidp->path);
20783b5ee9e8SAntonios Motakis     err = stat_to_qid(pdu, &stbuf, &qid);
20793b5ee9e8SAntonios Motakis     if (err < 0) {
20803b5ee9e8SAntonios Motakis         goto out;
20813b5ee9e8SAntonios Motakis     }
208260ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
208360ce86c7SWei Liu     if (err < 0) {
208460ce86c7SWei Liu         goto out;
208560ce86c7SWei Liu     }
208660ce86c7SWei Liu     err += offset;
208760ce86c7SWei Liu     trace_v9fs_lcreate_return(pdu->tag, pdu->id,
208860ce86c7SWei Liu                               qid.type, qid.version, qid.path, iounit);
208960ce86c7SWei Liu out:
209060ce86c7SWei Liu     put_fid(pdu, fidp);
209160ce86c7SWei Liu out_nofid:
209260ce86c7SWei Liu     pdu_complete(pdu, err);
209360ce86c7SWei Liu     v9fs_string_free(&name);
209460ce86c7SWei Liu }
209560ce86c7SWei Liu 
2096a1bf8b74SGreg Kurz static void coroutine_fn v9fs_fsync(void *opaque)
209760ce86c7SWei Liu {
209860ce86c7SWei Liu     int err;
209960ce86c7SWei Liu     int32_t fid;
210060ce86c7SWei Liu     int datasync;
210160ce86c7SWei Liu     size_t offset = 7;
210260ce86c7SWei Liu     V9fsFidState *fidp;
210360ce86c7SWei Liu     V9fsPDU *pdu = opaque;
210460ce86c7SWei Liu 
210560ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
210660ce86c7SWei Liu     if (err < 0) {
210760ce86c7SWei Liu         goto out_nofid;
210860ce86c7SWei Liu     }
210960ce86c7SWei Liu     trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync);
211060ce86c7SWei Liu 
211160ce86c7SWei Liu     fidp = get_fid(pdu, fid);
211260ce86c7SWei Liu     if (fidp == NULL) {
211360ce86c7SWei Liu         err = -ENOENT;
211460ce86c7SWei Liu         goto out_nofid;
211560ce86c7SWei Liu     }
211660ce86c7SWei Liu     err = v9fs_co_fsync(pdu, fidp, datasync);
211760ce86c7SWei Liu     if (!err) {
211860ce86c7SWei Liu         err = offset;
211960ce86c7SWei Liu     }
212060ce86c7SWei Liu     put_fid(pdu, fidp);
212160ce86c7SWei Liu out_nofid:
212260ce86c7SWei Liu     pdu_complete(pdu, err);
212360ce86c7SWei Liu }
212460ce86c7SWei Liu 
21258440e22eSGreg Kurz static void coroutine_fn v9fs_clunk(void *opaque)
212660ce86c7SWei Liu {
212760ce86c7SWei Liu     int err;
212860ce86c7SWei Liu     int32_t fid;
212960ce86c7SWei Liu     size_t offset = 7;
213060ce86c7SWei Liu     V9fsFidState *fidp;
213160ce86c7SWei Liu     V9fsPDU *pdu = opaque;
213260ce86c7SWei Liu     V9fsState *s = pdu->s;
213360ce86c7SWei Liu 
213460ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "d", &fid);
213560ce86c7SWei Liu     if (err < 0) {
213660ce86c7SWei Liu         goto out_nofid;
213760ce86c7SWei Liu     }
213860ce86c7SWei Liu     trace_v9fs_clunk(pdu->tag, pdu->id, fid);
213960ce86c7SWei Liu 
214060ce86c7SWei Liu     fidp = clunk_fid(s, fid);
214160ce86c7SWei Liu     if (fidp == NULL) {
214260ce86c7SWei Liu         err = -ENOENT;
214360ce86c7SWei Liu         goto out_nofid;
214460ce86c7SWei Liu     }
214560ce86c7SWei Liu     /*
214660ce86c7SWei Liu      * Bump the ref so that put_fid will
214760ce86c7SWei Liu      * free the fid.
214860ce86c7SWei Liu      */
214960ce86c7SWei Liu     fidp->ref++;
215060ce86c7SWei Liu     err = put_fid(pdu, fidp);
215160ce86c7SWei Liu     if (!err) {
215260ce86c7SWei Liu         err = offset;
215360ce86c7SWei Liu     }
215460ce86c7SWei Liu out_nofid:
215560ce86c7SWei Liu     pdu_complete(pdu, err);
215660ce86c7SWei Liu }
215760ce86c7SWei Liu 
2158bcb8998fSStefano Stabellini /*
2159bcb8998fSStefano Stabellini  * Create a QEMUIOVector for a sub-region of PDU iovecs
2160bcb8998fSStefano Stabellini  *
2161bcb8998fSStefano Stabellini  * @qiov:       uninitialized QEMUIOVector
2162bcb8998fSStefano Stabellini  * @skip:       number of bytes to skip from beginning of PDU
2163bcb8998fSStefano Stabellini  * @size:       number of bytes to include
2164bcb8998fSStefano Stabellini  * @is_write:   true - write, false - read
2165bcb8998fSStefano Stabellini  *
2166bcb8998fSStefano Stabellini  * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
2167bcb8998fSStefano Stabellini  * with qemu_iovec_destroy().
2168bcb8998fSStefano Stabellini  */
2169bcb8998fSStefano Stabellini static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu,
2170cf45183bSStefano Stabellini                                     size_t skip, size_t size,
2171bcb8998fSStefano Stabellini                                     bool is_write)
2172bcb8998fSStefano Stabellini {
2173bcb8998fSStefano Stabellini     QEMUIOVector elem;
2174bcb8998fSStefano Stabellini     struct iovec *iov;
2175bcb8998fSStefano Stabellini     unsigned int niov;
2176bcb8998fSStefano Stabellini 
217788da0b03SStefano Stabellini     if (is_write) {
2178cf45183bSStefano Stabellini         pdu->s->transport->init_out_iov_from_pdu(pdu, &iov, &niov, size + skip);
217988da0b03SStefano Stabellini     } else {
2180cf45183bSStefano Stabellini         pdu->s->transport->init_in_iov_from_pdu(pdu, &iov, &niov, size + skip);
218188da0b03SStefano Stabellini     }
2182bcb8998fSStefano Stabellini 
2183bcb8998fSStefano Stabellini     qemu_iovec_init_external(&elem, iov, niov);
2184bcb8998fSStefano Stabellini     qemu_iovec_init(qiov, niov);
2185cf45183bSStefano Stabellini     qemu_iovec_concat(qiov, &elem, skip, size);
2186bcb8998fSStefano Stabellini }
2187bcb8998fSStefano Stabellini 
218860ce86c7SWei Liu static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
218960ce86c7SWei Liu                            uint64_t off, uint32_t max_count)
219060ce86c7SWei Liu {
219160ce86c7SWei Liu     ssize_t err;
219260ce86c7SWei Liu     size_t offset = 7;
2193cf45183bSStefano Stabellini     uint64_t read_count;
2194bcb8998fSStefano Stabellini     QEMUIOVector qiov_full;
219560ce86c7SWei Liu 
21967e55d65cSLi Qiang     if (fidp->fs.xattr.len < off) {
21977e55d65cSLi Qiang         read_count = 0;
219816724a17SGreg Kurz     } else {
2199cf45183bSStefano Stabellini         read_count = fidp->fs.xattr.len - off;
2200cf45183bSStefano Stabellini     }
2201cf45183bSStefano Stabellini     if (read_count > max_count) {
220260ce86c7SWei Liu         read_count = max_count;
220360ce86c7SWei Liu     }
220460ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "d", read_count);
220560ce86c7SWei Liu     if (err < 0) {
220660ce86c7SWei Liu         return err;
220760ce86c7SWei Liu     }
220860ce86c7SWei Liu     offset += err;
220900588a0aSWei Liu 
2210cf45183bSStefano Stabellini     v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, read_count, false);
2211fa0eb5c5SGreg Kurz     err = v9fs_pack(qiov_full.iov, qiov_full.niov, 0,
221260ce86c7SWei Liu                     ((char *)fidp->fs.xattr.value) + off,
221360ce86c7SWei Liu                     read_count);
2214bcb8998fSStefano Stabellini     qemu_iovec_destroy(&qiov_full);
221560ce86c7SWei Liu     if (err < 0) {
221660ce86c7SWei Liu         return err;
221760ce86c7SWei Liu     }
221860ce86c7SWei Liu     offset += err;
221960ce86c7SWei Liu     return offset;
222060ce86c7SWei Liu }
222160ce86c7SWei Liu 
22228440e22eSGreg Kurz static int coroutine_fn v9fs_do_readdir_with_stat(V9fsPDU *pdu,
22238440e22eSGreg Kurz                                                   V9fsFidState *fidp,
22248440e22eSGreg Kurz                                                   uint32_t max_count)
222560ce86c7SWei Liu {
222660ce86c7SWei Liu     V9fsPath path;
222760ce86c7SWei Liu     V9fsStat v9stat;
222860ce86c7SWei Liu     int len, err = 0;
222960ce86c7SWei Liu     int32_t count = 0;
223060ce86c7SWei Liu     struct stat stbuf;
223160ce86c7SWei Liu     off_t saved_dir_pos;
2232635324e8SGreg Kurz     struct dirent *dent;
223360ce86c7SWei Liu 
223460ce86c7SWei Liu     /* save the directory position */
223560ce86c7SWei Liu     saved_dir_pos = v9fs_co_telldir(pdu, fidp);
223660ce86c7SWei Liu     if (saved_dir_pos < 0) {
223760ce86c7SWei Liu         return saved_dir_pos;
223860ce86c7SWei Liu     }
223960ce86c7SWei Liu 
224060ce86c7SWei Liu     while (1) {
224160ce86c7SWei Liu         v9fs_path_init(&path);
22427cde47d4SGreg Kurz 
22437cde47d4SGreg Kurz         v9fs_readdir_lock(&fidp->fs.dir);
22447cde47d4SGreg Kurz 
2245635324e8SGreg Kurz         err = v9fs_co_readdir(pdu, fidp, &dent);
2246635324e8SGreg Kurz         if (err || !dent) {
224760ce86c7SWei Liu             break;
224860ce86c7SWei Liu         }
224960ce86c7SWei Liu         err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path);
225060ce86c7SWei Liu         if (err < 0) {
22518762a46dSGreg Kurz             break;
225260ce86c7SWei Liu         }
225360ce86c7SWei Liu         err = v9fs_co_lstat(pdu, &path, &stbuf);
225460ce86c7SWei Liu         if (err < 0) {
22558762a46dSGreg Kurz             break;
225660ce86c7SWei Liu         }
22576069537fSJan Dakinevich         err = stat_to_v9stat(pdu, &path, dent->d_name, &stbuf, &v9stat);
225860ce86c7SWei Liu         if (err < 0) {
22598762a46dSGreg Kurz             break;
226060ce86c7SWei Liu         }
2261772a7369SJan Dakinevich         if ((count + v9stat.size + 2) > max_count) {
22627cde47d4SGreg Kurz             v9fs_readdir_unlock(&fidp->fs.dir);
22637cde47d4SGreg Kurz 
226460ce86c7SWei Liu             /* Ran out of buffer. Set dir back to old position and return */
226560ce86c7SWei Liu             v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
226660ce86c7SWei Liu             v9fs_stat_free(&v9stat);
226760ce86c7SWei Liu             v9fs_path_free(&path);
226860ce86c7SWei Liu             return count;
226960ce86c7SWei Liu         }
2270772a7369SJan Dakinevich 
2271772a7369SJan Dakinevich         /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
2272772a7369SJan Dakinevich         len = pdu_marshal(pdu, 11 + count, "S", &v9stat);
2273772a7369SJan Dakinevich 
2274772a7369SJan Dakinevich         v9fs_readdir_unlock(&fidp->fs.dir);
2275772a7369SJan Dakinevich 
2276772a7369SJan Dakinevich         if (len < 0) {
2277772a7369SJan Dakinevich             v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
2278772a7369SJan Dakinevich             v9fs_stat_free(&v9stat);
2279772a7369SJan Dakinevich             v9fs_path_free(&path);
2280772a7369SJan Dakinevich             return len;
2281772a7369SJan Dakinevich         }
228260ce86c7SWei Liu         count += len;
228360ce86c7SWei Liu         v9fs_stat_free(&v9stat);
228460ce86c7SWei Liu         v9fs_path_free(&path);
2285*6b3b279bSKeno Fischer         saved_dir_pos = qemu_dirent_off(dent);
228660ce86c7SWei Liu     }
22878762a46dSGreg Kurz 
22887cde47d4SGreg Kurz     v9fs_readdir_unlock(&fidp->fs.dir);
22897cde47d4SGreg Kurz 
229060ce86c7SWei Liu     v9fs_path_free(&path);
229160ce86c7SWei Liu     if (err < 0) {
229260ce86c7SWei Liu         return err;
229360ce86c7SWei Liu     }
229460ce86c7SWei Liu     return count;
229560ce86c7SWei Liu }
229660ce86c7SWei Liu 
22978440e22eSGreg Kurz static void coroutine_fn v9fs_read(void *opaque)
229860ce86c7SWei Liu {
229960ce86c7SWei Liu     int32_t fid;
230060ce86c7SWei Liu     uint64_t off;
230160ce86c7SWei Liu     ssize_t err = 0;
230260ce86c7SWei Liu     int32_t count = 0;
230360ce86c7SWei Liu     size_t offset = 7;
230460ce86c7SWei Liu     uint32_t max_count;
230560ce86c7SWei Liu     V9fsFidState *fidp;
230660ce86c7SWei Liu     V9fsPDU *pdu = opaque;
230760ce86c7SWei Liu     V9fsState *s = pdu->s;
230860ce86c7SWei Liu 
230960ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count);
231060ce86c7SWei Liu     if (err < 0) {
231160ce86c7SWei Liu         goto out_nofid;
231260ce86c7SWei Liu     }
231360ce86c7SWei Liu     trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count);
231460ce86c7SWei Liu 
231560ce86c7SWei Liu     fidp = get_fid(pdu, fid);
231660ce86c7SWei Liu     if (fidp == NULL) {
231760ce86c7SWei Liu         err = -EINVAL;
231860ce86c7SWei Liu         goto out_nofid;
231960ce86c7SWei Liu     }
232060ce86c7SWei Liu     if (fidp->fid_type == P9_FID_DIR) {
2321d2c5cf7cSChristian Schoenebeck         if (s->proto_version != V9FS_PROTO_2000U) {
2322d2c5cf7cSChristian Schoenebeck             warn_report_once(
2323d2c5cf7cSChristian Schoenebeck                 "9p: bad client: T_read request on directory only expected "
2324d2c5cf7cSChristian Schoenebeck                 "with 9P2000.u protocol version"
2325d2c5cf7cSChristian Schoenebeck             );
2326d2c5cf7cSChristian Schoenebeck             err = -EOPNOTSUPP;
2327d2c5cf7cSChristian Schoenebeck             goto out;
2328d2c5cf7cSChristian Schoenebeck         }
232960ce86c7SWei Liu         if (off == 0) {
233060ce86c7SWei Liu             v9fs_co_rewinddir(pdu, fidp);
233160ce86c7SWei Liu         }
233260ce86c7SWei Liu         count = v9fs_do_readdir_with_stat(pdu, fidp, max_count);
233360ce86c7SWei Liu         if (count < 0) {
233460ce86c7SWei Liu             err = count;
233560ce86c7SWei Liu             goto out;
233660ce86c7SWei Liu         }
233760ce86c7SWei Liu         err = pdu_marshal(pdu, offset, "d", count);
233860ce86c7SWei Liu         if (err < 0) {
233960ce86c7SWei Liu             goto out;
234060ce86c7SWei Liu         }
234160ce86c7SWei Liu         err += offset + count;
234260ce86c7SWei Liu     } else if (fidp->fid_type == P9_FID_FILE) {
234360ce86c7SWei Liu         QEMUIOVector qiov_full;
234460ce86c7SWei Liu         QEMUIOVector qiov;
234560ce86c7SWei Liu         int32_t len;
234660ce86c7SWei Liu 
2347cf45183bSStefano Stabellini         v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, max_count, false);
234860ce86c7SWei Liu         qemu_iovec_init(&qiov, qiov_full.niov);
234960ce86c7SWei Liu         do {
235060ce86c7SWei Liu             qemu_iovec_reset(&qiov);
235160ce86c7SWei Liu             qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count);
235260ce86c7SWei Liu             if (0) {
235360ce86c7SWei Liu                 print_sg(qiov.iov, qiov.niov);
235460ce86c7SWei Liu             }
235560ce86c7SWei Liu             /* Loop in case of EINTR */
235660ce86c7SWei Liu             do {
235760ce86c7SWei Liu                 len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off);
235860ce86c7SWei Liu                 if (len >= 0) {
235960ce86c7SWei Liu                     off   += len;
236060ce86c7SWei Liu                     count += len;
236160ce86c7SWei Liu                 }
236260ce86c7SWei Liu             } while (len == -EINTR && !pdu->cancelled);
236360ce86c7SWei Liu             if (len < 0) {
236460ce86c7SWei Liu                 /* IO error return the error */
236560ce86c7SWei Liu                 err = len;
2366e95c9a49SLi Qiang                 goto out_free_iovec;
236760ce86c7SWei Liu             }
236860ce86c7SWei Liu         } while (count < max_count && len > 0);
236960ce86c7SWei Liu         err = pdu_marshal(pdu, offset, "d", count);
237060ce86c7SWei Liu         if (err < 0) {
2371e95c9a49SLi Qiang             goto out_free_iovec;
237260ce86c7SWei Liu         }
237360ce86c7SWei Liu         err += offset + count;
2374e95c9a49SLi Qiang out_free_iovec:
237560ce86c7SWei Liu         qemu_iovec_destroy(&qiov);
237660ce86c7SWei Liu         qemu_iovec_destroy(&qiov_full);
237760ce86c7SWei Liu     } else if (fidp->fid_type == P9_FID_XATTR) {
237860ce86c7SWei Liu         err = v9fs_xattr_read(s, pdu, fidp, off, max_count);
237960ce86c7SWei Liu     } else {
238060ce86c7SWei Liu         err = -EINVAL;
238160ce86c7SWei Liu     }
238260ce86c7SWei Liu     trace_v9fs_read_return(pdu->tag, pdu->id, count, err);
238360ce86c7SWei Liu out:
238460ce86c7SWei Liu     put_fid(pdu, fidp);
238560ce86c7SWei Liu out_nofid:
238660ce86c7SWei Liu     pdu_complete(pdu, err);
238760ce86c7SWei Liu }
238860ce86c7SWei Liu 
238929c9d2caSChristian Schoenebeck /**
239029c9d2caSChristian Schoenebeck  * Returns size required in Rreaddir response for the passed dirent @p name.
239129c9d2caSChristian Schoenebeck  *
239229c9d2caSChristian Schoenebeck  * @param name - directory entry's name (i.e. file name, directory name)
239329c9d2caSChristian Schoenebeck  * @returns required size in bytes
239429c9d2caSChristian Schoenebeck  */
239529c9d2caSChristian Schoenebeck size_t v9fs_readdir_response_size(V9fsString *name)
239660ce86c7SWei Liu {
239760ce86c7SWei Liu     /*
239860ce86c7SWei Liu      * Size of each dirent on the wire: size of qid (13) + size of offset (8)
239960ce86c7SWei Liu      * size of type (1) + size of name.size (2) + strlen(name.data)
240060ce86c7SWei Liu      */
240160ce86c7SWei Liu     return 24 + v9fs_string_size(name);
240260ce86c7SWei Liu }
240360ce86c7SWei Liu 
24040c4356baSChristian Schoenebeck static void v9fs_free_dirents(struct V9fsDirEnt *e)
24050c4356baSChristian Schoenebeck {
24060c4356baSChristian Schoenebeck     struct V9fsDirEnt *next = NULL;
24070c4356baSChristian Schoenebeck 
24080c4356baSChristian Schoenebeck     for (; e; e = next) {
24090c4356baSChristian Schoenebeck         next = e->next;
24100c4356baSChristian Schoenebeck         g_free(e->dent);
24110c4356baSChristian Schoenebeck         g_free(e->st);
24120c4356baSChristian Schoenebeck         g_free(e);
24130c4356baSChristian Schoenebeck     }
24140c4356baSChristian Schoenebeck }
24150c4356baSChristian Schoenebeck 
24168440e22eSGreg Kurz static int coroutine_fn v9fs_do_readdir(V9fsPDU *pdu, V9fsFidState *fidp,
24170c4356baSChristian Schoenebeck                                         off_t offset, int32_t max_count)
241860ce86c7SWei Liu {
241960ce86c7SWei Liu     size_t size;
242060ce86c7SWei Liu     V9fsQID qid;
242160ce86c7SWei Liu     V9fsString name;
242260ce86c7SWei Liu     int len, err = 0;
242360ce86c7SWei Liu     int32_t count = 0;
2424*6b3b279bSKeno Fischer     off_t off;
2425635324e8SGreg Kurz     struct dirent *dent;
24260c4356baSChristian Schoenebeck     struct stat *st;
24270c4356baSChristian Schoenebeck     struct V9fsDirEnt *entries = NULL;
242860ce86c7SWei Liu 
24290c4356baSChristian Schoenebeck     /*
24300c4356baSChristian Schoenebeck      * inode remapping requires the device id, which in turn might be
24310c4356baSChristian Schoenebeck      * different for different directory entries, so if inode remapping is
24320c4356baSChristian Schoenebeck      * enabled we have to make a full stat for each directory entry
24330c4356baSChristian Schoenebeck      */
24340c4356baSChristian Schoenebeck     const bool dostat = pdu->s->ctx.export_flags & V9FS_REMAP_INODES;
24350c4356baSChristian Schoenebeck 
24360c4356baSChristian Schoenebeck     /*
24370c4356baSChristian Schoenebeck      * Fetch all required directory entries altogether on a background IO
24380c4356baSChristian Schoenebeck      * thread from fs driver. We don't want to do that for each entry
24390c4356baSChristian Schoenebeck      * individually, because hopping between threads (this main IO thread
24400c4356baSChristian Schoenebeck      * and background IO driver thread) would sum up to huge latencies.
24410c4356baSChristian Schoenebeck      */
24420c4356baSChristian Schoenebeck     count = v9fs_co_readdir_many(pdu, fidp, &entries, offset, max_count,
24430c4356baSChristian Schoenebeck                                  dostat);
24440c4356baSChristian Schoenebeck     if (count < 0) {
24450c4356baSChristian Schoenebeck         err = count;
24460c4356baSChristian Schoenebeck         count = 0;
24470c4356baSChristian Schoenebeck         goto out;
244860ce86c7SWei Liu     }
24490c4356baSChristian Schoenebeck     count = 0;
245060ce86c7SWei Liu 
24510c4356baSChristian Schoenebeck     for (struct V9fsDirEnt *e = entries; e; e = e->next) {
24520c4356baSChristian Schoenebeck         dent = e->dent;
24531a6ed33cSAntonios Motakis 
24541a6ed33cSAntonios Motakis         if (pdu->s->ctx.export_flags & V9FS_REMAP_INODES) {
24550c4356baSChristian Schoenebeck             st = e->st;
24560c4356baSChristian Schoenebeck             /* e->st should never be NULL, but just to be sure */
24570c4356baSChristian Schoenebeck             if (!st) {
24580c4356baSChristian Schoenebeck                 err = -1;
24590c4356baSChristian Schoenebeck                 break;
24600c4356baSChristian Schoenebeck             }
24610c4356baSChristian Schoenebeck 
24620c4356baSChristian Schoenebeck             /* remap inode */
24630c4356baSChristian Schoenebeck             err = stat_to_qid(pdu, st, &qid);
24641a6ed33cSAntonios Motakis             if (err < 0) {
24650c4356baSChristian Schoenebeck                 break;
24661a6ed33cSAntonios Motakis             }
24671a6ed33cSAntonios Motakis         } else {
246860ce86c7SWei Liu             /*
246960ce86c7SWei Liu              * Fill up just the path field of qid because the client uses
247060ce86c7SWei Liu              * only that. To fill the entire qid structure we will have
24711a6ed33cSAntonios Motakis              * to stat each dirent found, which is expensive. For the
24720c4356baSChristian Schoenebeck              * latter reason we don't call stat_to_qid() here. Only drawback
24731a6ed33cSAntonios Motakis              * is that no multi-device export detection of stat_to_qid()
24741a6ed33cSAntonios Motakis              * would be done and provided as error to the user here. But
24751a6ed33cSAntonios Motakis              * user would get that error anyway when accessing those
24761a6ed33cSAntonios Motakis              * files/dirs through other ways.
247760ce86c7SWei Liu              */
247860ce86c7SWei Liu             size = MIN(sizeof(dent->d_ino), sizeof(qid.path));
247960ce86c7SWei Liu             memcpy(&qid.path, &dent->d_ino, size);
248060ce86c7SWei Liu             /* Fill the other fields with dummy values */
248160ce86c7SWei Liu             qid.type = 0;
248260ce86c7SWei Liu             qid.version = 0;
24831a6ed33cSAntonios Motakis         }
248460ce86c7SWei Liu 
2485*6b3b279bSKeno Fischer         off = qemu_dirent_off(dent);
24860c4356baSChristian Schoenebeck         v9fs_string_init(&name);
24870c4356baSChristian Schoenebeck         v9fs_string_sprintf(&name, "%s", dent->d_name);
24880c4356baSChristian Schoenebeck 
248960ce86c7SWei Liu         /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
249060ce86c7SWei Liu         len = pdu_marshal(pdu, 11 + count, "Qqbs",
2491*6b3b279bSKeno Fischer                           &qid, off,
249260ce86c7SWei Liu                           dent->d_type, &name);
24937cde47d4SGreg Kurz 
24940c4356baSChristian Schoenebeck         v9fs_string_free(&name);
24957cde47d4SGreg Kurz 
249660ce86c7SWei Liu         if (len < 0) {
24970c4356baSChristian Schoenebeck             err = len;
24980c4356baSChristian Schoenebeck             break;
249960ce86c7SWei Liu         }
25000c4356baSChristian Schoenebeck 
250160ce86c7SWei Liu         count += len;
250260ce86c7SWei Liu     }
25037cde47d4SGreg Kurz 
25040c4356baSChristian Schoenebeck out:
25050c4356baSChristian Schoenebeck     v9fs_free_dirents(entries);
250660ce86c7SWei Liu     if (err < 0) {
250760ce86c7SWei Liu         return err;
250860ce86c7SWei Liu     }
250960ce86c7SWei Liu     return count;
251060ce86c7SWei Liu }
251160ce86c7SWei Liu 
25128440e22eSGreg Kurz static void coroutine_fn v9fs_readdir(void *opaque)
251360ce86c7SWei Liu {
251460ce86c7SWei Liu     int32_t fid;
251560ce86c7SWei Liu     V9fsFidState *fidp;
251660ce86c7SWei Liu     ssize_t retval = 0;
251760ce86c7SWei Liu     size_t offset = 7;
251860ce86c7SWei Liu     uint64_t initial_offset;
251960ce86c7SWei Liu     int32_t count;
252060ce86c7SWei Liu     uint32_t max_count;
252160ce86c7SWei Liu     V9fsPDU *pdu = opaque;
2522d36a5c22SChristian Schoenebeck     V9fsState *s = pdu->s;
252360ce86c7SWei Liu 
252460ce86c7SWei Liu     retval = pdu_unmarshal(pdu, offset, "dqd", &fid,
252560ce86c7SWei Liu                            &initial_offset, &max_count);
252660ce86c7SWei Liu     if (retval < 0) {
252760ce86c7SWei Liu         goto out_nofid;
252860ce86c7SWei Liu     }
252960ce86c7SWei Liu     trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count);
253060ce86c7SWei Liu 
2531d36a5c22SChristian Schoenebeck     /* Enough space for a R_readdir header: size[4] Rreaddir tag[2] count[4] */
2532d36a5c22SChristian Schoenebeck     if (max_count > s->msize - 11) {
2533d36a5c22SChristian Schoenebeck         max_count = s->msize - 11;
2534d36a5c22SChristian Schoenebeck         warn_report_once(
2535d36a5c22SChristian Schoenebeck             "9p: bad client: T_readdir with count > msize - 11"
2536d36a5c22SChristian Schoenebeck         );
2537d36a5c22SChristian Schoenebeck     }
2538d36a5c22SChristian Schoenebeck 
253960ce86c7SWei Liu     fidp = get_fid(pdu, fid);
254060ce86c7SWei Liu     if (fidp == NULL) {
254160ce86c7SWei Liu         retval = -EINVAL;
254260ce86c7SWei Liu         goto out_nofid;
254360ce86c7SWei Liu     }
2544f314ea4eSGreg Kurz     if (!fidp->fs.dir.stream) {
254560ce86c7SWei Liu         retval = -EINVAL;
254660ce86c7SWei Liu         goto out;
254760ce86c7SWei Liu     }
2548d2c5cf7cSChristian Schoenebeck     if (s->proto_version != V9FS_PROTO_2000L) {
2549d2c5cf7cSChristian Schoenebeck         warn_report_once(
2550d2c5cf7cSChristian Schoenebeck             "9p: bad client: T_readdir request only expected with 9P2000.L "
2551d2c5cf7cSChristian Schoenebeck             "protocol version"
2552d2c5cf7cSChristian Schoenebeck         );
2553d2c5cf7cSChristian Schoenebeck         retval = -EOPNOTSUPP;
2554d2c5cf7cSChristian Schoenebeck         goto out;
2555d2c5cf7cSChristian Schoenebeck     }
25560c4356baSChristian Schoenebeck     count = v9fs_do_readdir(pdu, fidp, (off_t) initial_offset, max_count);
255760ce86c7SWei Liu     if (count < 0) {
255860ce86c7SWei Liu         retval = count;
255960ce86c7SWei Liu         goto out;
256060ce86c7SWei Liu     }
256160ce86c7SWei Liu     retval = pdu_marshal(pdu, offset, "d", count);
256260ce86c7SWei Liu     if (retval < 0) {
256360ce86c7SWei Liu         goto out;
256460ce86c7SWei Liu     }
256560ce86c7SWei Liu     retval += count + offset;
256660ce86c7SWei Liu     trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval);
256760ce86c7SWei Liu out:
256860ce86c7SWei Liu     put_fid(pdu, fidp);
256960ce86c7SWei Liu out_nofid:
257060ce86c7SWei Liu     pdu_complete(pdu, retval);
257160ce86c7SWei Liu }
257260ce86c7SWei Liu 
257360ce86c7SWei Liu static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
257460ce86c7SWei Liu                             uint64_t off, uint32_t count,
257560ce86c7SWei Liu                             struct iovec *sg, int cnt)
257660ce86c7SWei Liu {
257760ce86c7SWei Liu     int i, to_copy;
257860ce86c7SWei Liu     ssize_t err = 0;
25797e55d65cSLi Qiang     uint64_t write_count;
258060ce86c7SWei Liu     size_t offset = 7;
258160ce86c7SWei Liu 
258260ce86c7SWei Liu 
25837e55d65cSLi Qiang     if (fidp->fs.xattr.len < off) {
2584b858e80aSDaniel Henrique Barboza         return -ENOSPC;
258560ce86c7SWei Liu     }
25867e55d65cSLi Qiang     write_count = fidp->fs.xattr.len - off;
25877e55d65cSLi Qiang     if (write_count > count) {
25887e55d65cSLi Qiang         write_count = count;
25897e55d65cSLi Qiang     }
259060ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "d", write_count);
259160ce86c7SWei Liu     if (err < 0) {
259260ce86c7SWei Liu         return err;
259360ce86c7SWei Liu     }
259460ce86c7SWei Liu     err += offset;
259560ce86c7SWei Liu     fidp->fs.xattr.copied_len += write_count;
259660ce86c7SWei Liu     /*
259760ce86c7SWei Liu      * Now copy the content from sg list
259860ce86c7SWei Liu      */
259960ce86c7SWei Liu     for (i = 0; i < cnt; i++) {
260060ce86c7SWei Liu         if (write_count > sg[i].iov_len) {
260160ce86c7SWei Liu             to_copy = sg[i].iov_len;
260260ce86c7SWei Liu         } else {
260360ce86c7SWei Liu             to_copy = write_count;
260460ce86c7SWei Liu         }
260560ce86c7SWei Liu         memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy);
260660ce86c7SWei Liu         /* updating vs->off since we are not using below */
260760ce86c7SWei Liu         off += to_copy;
260860ce86c7SWei Liu         write_count -= to_copy;
260960ce86c7SWei Liu     }
2610b858e80aSDaniel Henrique Barboza 
261160ce86c7SWei Liu     return err;
261260ce86c7SWei Liu }
261360ce86c7SWei Liu 
26148440e22eSGreg Kurz static void coroutine_fn v9fs_write(void *opaque)
261560ce86c7SWei Liu {
261660ce86c7SWei Liu     ssize_t err;
261760ce86c7SWei Liu     int32_t fid;
261860ce86c7SWei Liu     uint64_t off;
261960ce86c7SWei Liu     uint32_t count;
262060ce86c7SWei Liu     int32_t len = 0;
262160ce86c7SWei Liu     int32_t total = 0;
262260ce86c7SWei Liu     size_t offset = 7;
262360ce86c7SWei Liu     V9fsFidState *fidp;
262460ce86c7SWei Liu     V9fsPDU *pdu = opaque;
262560ce86c7SWei Liu     V9fsState *s = pdu->s;
262660ce86c7SWei Liu     QEMUIOVector qiov_full;
262760ce86c7SWei Liu     QEMUIOVector qiov;
262860ce86c7SWei Liu 
262960ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count);
263060ce86c7SWei Liu     if (err < 0) {
263160ce86c7SWei Liu         pdu_complete(pdu, err);
263260ce86c7SWei Liu         return;
263360ce86c7SWei Liu     }
263460ce86c7SWei Liu     offset += err;
2635cf45183bSStefano Stabellini     v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, count, true);
263660ce86c7SWei Liu     trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov);
263760ce86c7SWei Liu 
263860ce86c7SWei Liu     fidp = get_fid(pdu, fid);
263960ce86c7SWei Liu     if (fidp == NULL) {
264060ce86c7SWei Liu         err = -EINVAL;
264160ce86c7SWei Liu         goto out_nofid;
264260ce86c7SWei Liu     }
264360ce86c7SWei Liu     if (fidp->fid_type == P9_FID_FILE) {
264460ce86c7SWei Liu         if (fidp->fs.fd == -1) {
264560ce86c7SWei Liu             err = -EINVAL;
264660ce86c7SWei Liu             goto out;
264760ce86c7SWei Liu         }
264860ce86c7SWei Liu     } else if (fidp->fid_type == P9_FID_XATTR) {
264960ce86c7SWei Liu         /*
265060ce86c7SWei Liu          * setxattr operation
265160ce86c7SWei Liu          */
265260ce86c7SWei Liu         err = v9fs_xattr_write(s, pdu, fidp, off, count,
265360ce86c7SWei Liu                                qiov_full.iov, qiov_full.niov);
265460ce86c7SWei Liu         goto out;
265560ce86c7SWei Liu     } else {
265660ce86c7SWei Liu         err = -EINVAL;
265760ce86c7SWei Liu         goto out;
265860ce86c7SWei Liu     }
265960ce86c7SWei Liu     qemu_iovec_init(&qiov, qiov_full.niov);
266060ce86c7SWei Liu     do {
266160ce86c7SWei Liu         qemu_iovec_reset(&qiov);
266260ce86c7SWei Liu         qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total);
266360ce86c7SWei Liu         if (0) {
266460ce86c7SWei Liu             print_sg(qiov.iov, qiov.niov);
266560ce86c7SWei Liu         }
266660ce86c7SWei Liu         /* Loop in case of EINTR */
266760ce86c7SWei Liu         do {
266860ce86c7SWei Liu             len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off);
266960ce86c7SWei Liu             if (len >= 0) {
267060ce86c7SWei Liu                 off   += len;
267160ce86c7SWei Liu                 total += len;
267260ce86c7SWei Liu             }
267360ce86c7SWei Liu         } while (len == -EINTR && !pdu->cancelled);
267460ce86c7SWei Liu         if (len < 0) {
267560ce86c7SWei Liu             /* IO error return the error */
267660ce86c7SWei Liu             err = len;
267760ce86c7SWei Liu             goto out_qiov;
267860ce86c7SWei Liu         }
267960ce86c7SWei Liu     } while (total < count && len > 0);
268060ce86c7SWei Liu 
268160ce86c7SWei Liu     offset = 7;
268260ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "d", total);
268360ce86c7SWei Liu     if (err < 0) {
2684fdfcc9aeSLi Qiang         goto out_qiov;
268560ce86c7SWei Liu     }
268660ce86c7SWei Liu     err += offset;
268760ce86c7SWei Liu     trace_v9fs_write_return(pdu->tag, pdu->id, total, err);
268860ce86c7SWei Liu out_qiov:
268960ce86c7SWei Liu     qemu_iovec_destroy(&qiov);
269060ce86c7SWei Liu out:
269160ce86c7SWei Liu     put_fid(pdu, fidp);
269260ce86c7SWei Liu out_nofid:
269360ce86c7SWei Liu     qemu_iovec_destroy(&qiov_full);
269460ce86c7SWei Liu     pdu_complete(pdu, err);
269560ce86c7SWei Liu }
269660ce86c7SWei Liu 
26978440e22eSGreg Kurz static void coroutine_fn v9fs_create(void *opaque)
269860ce86c7SWei Liu {
269960ce86c7SWei Liu     int32_t fid;
270060ce86c7SWei Liu     int err = 0;
270160ce86c7SWei Liu     size_t offset = 7;
270260ce86c7SWei Liu     V9fsFidState *fidp;
270360ce86c7SWei Liu     V9fsQID qid;
270460ce86c7SWei Liu     int32_t perm;
270560ce86c7SWei Liu     int8_t mode;
270660ce86c7SWei Liu     V9fsPath path;
270760ce86c7SWei Liu     struct stat stbuf;
270860ce86c7SWei Liu     V9fsString name;
270960ce86c7SWei Liu     V9fsString extension;
271060ce86c7SWei Liu     int iounit;
271160ce86c7SWei Liu     V9fsPDU *pdu = opaque;
27125b3c77aaSGreg Kurz     V9fsState *s = pdu->s;
271360ce86c7SWei Liu 
271460ce86c7SWei Liu     v9fs_path_init(&path);
271560ce86c7SWei Liu     v9fs_string_init(&name);
271660ce86c7SWei Liu     v9fs_string_init(&extension);
271760ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name,
271860ce86c7SWei Liu                         &perm, &mode, &extension);
271960ce86c7SWei Liu     if (err < 0) {
272060ce86c7SWei Liu         goto out_nofid;
272160ce86c7SWei Liu     }
272260ce86c7SWei Liu     trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode);
272360ce86c7SWei Liu 
2724fff39a7aSGreg Kurz     if (name_is_illegal(name.data)) {
2725fff39a7aSGreg Kurz         err = -ENOENT;
2726fff39a7aSGreg Kurz         goto out_nofid;
2727fff39a7aSGreg Kurz     }
2728fff39a7aSGreg Kurz 
2729805b5d98SGreg Kurz     if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2730805b5d98SGreg Kurz         err = -EEXIST;
2731805b5d98SGreg Kurz         goto out_nofid;
2732805b5d98SGreg Kurz     }
2733805b5d98SGreg Kurz 
273460ce86c7SWei Liu     fidp = get_fid(pdu, fid);
273560ce86c7SWei Liu     if (fidp == NULL) {
273660ce86c7SWei Liu         err = -EINVAL;
273760ce86c7SWei Liu         goto out_nofid;
273860ce86c7SWei Liu     }
2739d63fb193SLi Qiang     if (fidp->fid_type != P9_FID_NONE) {
2740d63fb193SLi Qiang         err = -EINVAL;
2741d63fb193SLi Qiang         goto out;
2742d63fb193SLi Qiang     }
274360ce86c7SWei Liu     if (perm & P9_STAT_MODE_DIR) {
274460ce86c7SWei Liu         err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777,
274560ce86c7SWei Liu                             fidp->uid, -1, &stbuf);
274660ce86c7SWei Liu         if (err < 0) {
274760ce86c7SWei Liu             goto out;
274860ce86c7SWei Liu         }
274960ce86c7SWei Liu         err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
275060ce86c7SWei Liu         if (err < 0) {
275160ce86c7SWei Liu             goto out;
275260ce86c7SWei Liu         }
27535b3c77aaSGreg Kurz         v9fs_path_write_lock(s);
275460ce86c7SWei Liu         v9fs_path_copy(&fidp->path, &path);
27555b3c77aaSGreg Kurz         v9fs_path_unlock(s);
275660ce86c7SWei Liu         err = v9fs_co_opendir(pdu, fidp);
275760ce86c7SWei Liu         if (err < 0) {
275860ce86c7SWei Liu             goto out;
275960ce86c7SWei Liu         }
276060ce86c7SWei Liu         fidp->fid_type = P9_FID_DIR;
276160ce86c7SWei Liu     } else if (perm & P9_STAT_MODE_SYMLINK) {
276260ce86c7SWei Liu         err = v9fs_co_symlink(pdu, fidp, &name,
276360ce86c7SWei Liu                               extension.data, -1 , &stbuf);
276460ce86c7SWei Liu         if (err < 0) {
276560ce86c7SWei Liu             goto out;
276660ce86c7SWei Liu         }
276760ce86c7SWei Liu         err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
276860ce86c7SWei Liu         if (err < 0) {
276960ce86c7SWei Liu             goto out;
277060ce86c7SWei Liu         }
27715b3c77aaSGreg Kurz         v9fs_path_write_lock(s);
277260ce86c7SWei Liu         v9fs_path_copy(&fidp->path, &path);
27735b3c77aaSGreg Kurz         v9fs_path_unlock(s);
277460ce86c7SWei Liu     } else if (perm & P9_STAT_MODE_LINK) {
277560ce86c7SWei Liu         int32_t ofid = atoi(extension.data);
277660ce86c7SWei Liu         V9fsFidState *ofidp = get_fid(pdu, ofid);
277760ce86c7SWei Liu         if (ofidp == NULL) {
277860ce86c7SWei Liu             err = -EINVAL;
277960ce86c7SWei Liu             goto out;
278060ce86c7SWei Liu         }
278160ce86c7SWei Liu         err = v9fs_co_link(pdu, ofidp, fidp, &name);
278260ce86c7SWei Liu         put_fid(pdu, ofidp);
278360ce86c7SWei Liu         if (err < 0) {
278460ce86c7SWei Liu             goto out;
278560ce86c7SWei Liu         }
278660ce86c7SWei Liu         err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
278760ce86c7SWei Liu         if (err < 0) {
278860ce86c7SWei Liu             fidp->fid_type = P9_FID_NONE;
278960ce86c7SWei Liu             goto out;
279060ce86c7SWei Liu         }
27915b3c77aaSGreg Kurz         v9fs_path_write_lock(s);
279260ce86c7SWei Liu         v9fs_path_copy(&fidp->path, &path);
27935b3c77aaSGreg Kurz         v9fs_path_unlock(s);
279460ce86c7SWei Liu         err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
279560ce86c7SWei Liu         if (err < 0) {
279660ce86c7SWei Liu             fidp->fid_type = P9_FID_NONE;
279760ce86c7SWei Liu             goto out;
279860ce86c7SWei Liu         }
279960ce86c7SWei Liu     } else if (perm & P9_STAT_MODE_DEVICE) {
280060ce86c7SWei Liu         char ctype;
280160ce86c7SWei Liu         uint32_t major, minor;
280260ce86c7SWei Liu         mode_t nmode = 0;
280360ce86c7SWei Liu 
280460ce86c7SWei Liu         if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) {
280560ce86c7SWei Liu             err = -errno;
280660ce86c7SWei Liu             goto out;
280760ce86c7SWei Liu         }
280860ce86c7SWei Liu 
280960ce86c7SWei Liu         switch (ctype) {
281060ce86c7SWei Liu         case 'c':
281160ce86c7SWei Liu             nmode = S_IFCHR;
281260ce86c7SWei Liu             break;
281360ce86c7SWei Liu         case 'b':
281460ce86c7SWei Liu             nmode = S_IFBLK;
281560ce86c7SWei Liu             break;
281660ce86c7SWei Liu         default:
281760ce86c7SWei Liu             err = -EIO;
281860ce86c7SWei Liu             goto out;
281960ce86c7SWei Liu         }
282060ce86c7SWei Liu 
282160ce86c7SWei Liu         nmode |= perm & 0777;
282260ce86c7SWei Liu         err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
282360ce86c7SWei Liu                             makedev(major, minor), nmode, &stbuf);
282460ce86c7SWei Liu         if (err < 0) {
282560ce86c7SWei Liu             goto out;
282660ce86c7SWei Liu         }
282760ce86c7SWei Liu         err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
282860ce86c7SWei Liu         if (err < 0) {
282960ce86c7SWei Liu             goto out;
283060ce86c7SWei Liu         }
28315b3c77aaSGreg Kurz         v9fs_path_write_lock(s);
283260ce86c7SWei Liu         v9fs_path_copy(&fidp->path, &path);
28335b3c77aaSGreg Kurz         v9fs_path_unlock(s);
283460ce86c7SWei Liu     } else if (perm & P9_STAT_MODE_NAMED_PIPE) {
283560ce86c7SWei Liu         err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
283660ce86c7SWei Liu                             0, S_IFIFO | (perm & 0777), &stbuf);
283760ce86c7SWei Liu         if (err < 0) {
283860ce86c7SWei Liu             goto out;
283960ce86c7SWei Liu         }
284060ce86c7SWei Liu         err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
284160ce86c7SWei Liu         if (err < 0) {
284260ce86c7SWei Liu             goto out;
284360ce86c7SWei Liu         }
28445b3c77aaSGreg Kurz         v9fs_path_write_lock(s);
284560ce86c7SWei Liu         v9fs_path_copy(&fidp->path, &path);
28465b3c77aaSGreg Kurz         v9fs_path_unlock(s);
284760ce86c7SWei Liu     } else if (perm & P9_STAT_MODE_SOCKET) {
284860ce86c7SWei Liu         err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
284960ce86c7SWei Liu                             0, S_IFSOCK | (perm & 0777), &stbuf);
285060ce86c7SWei Liu         if (err < 0) {
285160ce86c7SWei Liu             goto out;
285260ce86c7SWei Liu         }
285360ce86c7SWei Liu         err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
285460ce86c7SWei Liu         if (err < 0) {
285560ce86c7SWei Liu             goto out;
285660ce86c7SWei Liu         }
28575b3c77aaSGreg Kurz         v9fs_path_write_lock(s);
285860ce86c7SWei Liu         v9fs_path_copy(&fidp->path, &path);
28595b3c77aaSGreg Kurz         v9fs_path_unlock(s);
286060ce86c7SWei Liu     } else {
286160ce86c7SWei Liu         err = v9fs_co_open2(pdu, fidp, &name, -1,
286260ce86c7SWei Liu                             omode_to_uflags(mode) | O_CREAT, perm, &stbuf);
286360ce86c7SWei Liu         if (err < 0) {
286460ce86c7SWei Liu             goto out;
286560ce86c7SWei Liu         }
286660ce86c7SWei Liu         fidp->fid_type = P9_FID_FILE;
286760ce86c7SWei Liu         fidp->open_flags = omode_to_uflags(mode);
286860ce86c7SWei Liu         if (fidp->open_flags & O_EXCL) {
286960ce86c7SWei Liu             /*
287060ce86c7SWei Liu              * We let the host file system do O_EXCL check
287160ce86c7SWei Liu              * We should not reclaim such fd
287260ce86c7SWei Liu              */
287360ce86c7SWei Liu             fidp->flags |= FID_NON_RECLAIMABLE;
287460ce86c7SWei Liu         }
287560ce86c7SWei Liu     }
287660ce86c7SWei Liu     iounit = get_iounit(pdu, &fidp->path);
28773b5ee9e8SAntonios Motakis     err = stat_to_qid(pdu, &stbuf, &qid);
28783b5ee9e8SAntonios Motakis     if (err < 0) {
28793b5ee9e8SAntonios Motakis         goto out;
28803b5ee9e8SAntonios Motakis     }
288160ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
288260ce86c7SWei Liu     if (err < 0) {
288360ce86c7SWei Liu         goto out;
288460ce86c7SWei Liu     }
288560ce86c7SWei Liu     err += offset;
288660ce86c7SWei Liu     trace_v9fs_create_return(pdu->tag, pdu->id,
288760ce86c7SWei Liu                              qid.type, qid.version, qid.path, iounit);
288860ce86c7SWei Liu out:
288960ce86c7SWei Liu     put_fid(pdu, fidp);
289060ce86c7SWei Liu out_nofid:
289160ce86c7SWei Liu    pdu_complete(pdu, err);
289260ce86c7SWei Liu    v9fs_string_free(&name);
289360ce86c7SWei Liu    v9fs_string_free(&extension);
289460ce86c7SWei Liu    v9fs_path_free(&path);
289560ce86c7SWei Liu }
289660ce86c7SWei Liu 
28978440e22eSGreg Kurz static void coroutine_fn v9fs_symlink(void *opaque)
289860ce86c7SWei Liu {
289960ce86c7SWei Liu     V9fsPDU *pdu = opaque;
290060ce86c7SWei Liu     V9fsString name;
290160ce86c7SWei Liu     V9fsString symname;
290260ce86c7SWei Liu     V9fsFidState *dfidp;
290360ce86c7SWei Liu     V9fsQID qid;
290460ce86c7SWei Liu     struct stat stbuf;
290560ce86c7SWei Liu     int32_t dfid;
290660ce86c7SWei Liu     int err = 0;
290760ce86c7SWei Liu     gid_t gid;
290860ce86c7SWei Liu     size_t offset = 7;
290960ce86c7SWei Liu 
291060ce86c7SWei Liu     v9fs_string_init(&name);
291160ce86c7SWei Liu     v9fs_string_init(&symname);
291260ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid);
291360ce86c7SWei Liu     if (err < 0) {
291460ce86c7SWei Liu         goto out_nofid;
291560ce86c7SWei Liu     }
291660ce86c7SWei Liu     trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid);
291760ce86c7SWei Liu 
2918fff39a7aSGreg Kurz     if (name_is_illegal(name.data)) {
2919fff39a7aSGreg Kurz         err = -ENOENT;
2920fff39a7aSGreg Kurz         goto out_nofid;
2921fff39a7aSGreg Kurz     }
2922fff39a7aSGreg Kurz 
2923805b5d98SGreg Kurz     if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2924805b5d98SGreg Kurz         err = -EEXIST;
2925805b5d98SGreg Kurz         goto out_nofid;
2926805b5d98SGreg Kurz     }
2927805b5d98SGreg Kurz 
292860ce86c7SWei Liu     dfidp = get_fid(pdu, dfid);
292960ce86c7SWei Liu     if (dfidp == NULL) {
293060ce86c7SWei Liu         err = -EINVAL;
293160ce86c7SWei Liu         goto out_nofid;
293260ce86c7SWei Liu     }
293360ce86c7SWei Liu     err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf);
293460ce86c7SWei Liu     if (err < 0) {
293560ce86c7SWei Liu         goto out;
293660ce86c7SWei Liu     }
29373b5ee9e8SAntonios Motakis     err = stat_to_qid(pdu, &stbuf, &qid);
29383b5ee9e8SAntonios Motakis     if (err < 0) {
29393b5ee9e8SAntonios Motakis         goto out;
29403b5ee9e8SAntonios Motakis     }
294160ce86c7SWei Liu     err =  pdu_marshal(pdu, offset, "Q", &qid);
294260ce86c7SWei Liu     if (err < 0) {
294360ce86c7SWei Liu         goto out;
294460ce86c7SWei Liu     }
294560ce86c7SWei Liu     err += offset;
294660ce86c7SWei Liu     trace_v9fs_symlink_return(pdu->tag, pdu->id,
294760ce86c7SWei Liu                               qid.type, qid.version, qid.path);
294860ce86c7SWei Liu out:
294960ce86c7SWei Liu     put_fid(pdu, dfidp);
295060ce86c7SWei Liu out_nofid:
295160ce86c7SWei Liu     pdu_complete(pdu, err);
295260ce86c7SWei Liu     v9fs_string_free(&name);
295360ce86c7SWei Liu     v9fs_string_free(&symname);
295460ce86c7SWei Liu }
295560ce86c7SWei Liu 
2956a1bf8b74SGreg Kurz static void coroutine_fn v9fs_flush(void *opaque)
295760ce86c7SWei Liu {
295860ce86c7SWei Liu     ssize_t err;
295960ce86c7SWei Liu     int16_t tag;
296060ce86c7SWei Liu     size_t offset = 7;
2961d5f2af7bSGreg Kurz     V9fsPDU *cancel_pdu = NULL;
296260ce86c7SWei Liu     V9fsPDU *pdu = opaque;
296360ce86c7SWei Liu     V9fsState *s = pdu->s;
296460ce86c7SWei Liu 
296560ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "w", &tag);
296660ce86c7SWei Liu     if (err < 0) {
296760ce86c7SWei Liu         pdu_complete(pdu, err);
296860ce86c7SWei Liu         return;
296960ce86c7SWei Liu     }
297060ce86c7SWei Liu     trace_v9fs_flush(pdu->tag, pdu->id, tag);
297160ce86c7SWei Liu 
2972d5f2af7bSGreg Kurz     if (pdu->tag == tag) {
29733dc6f869SAlistair Francis         warn_report("the guest sent a self-referencing 9P flush request");
2974d5f2af7bSGreg Kurz     } else {
297560ce86c7SWei Liu         QLIST_FOREACH(cancel_pdu, &s->active_list, next) {
297660ce86c7SWei Liu             if (cancel_pdu->tag == tag) {
297760ce86c7SWei Liu                 break;
297860ce86c7SWei Liu             }
297960ce86c7SWei Liu         }
2980d5f2af7bSGreg Kurz     }
298160ce86c7SWei Liu     if (cancel_pdu) {
298260ce86c7SWei Liu         cancel_pdu->cancelled = 1;
298360ce86c7SWei Liu         /*
298460ce86c7SWei Liu          * Wait for pdu to complete.
298560ce86c7SWei Liu          */
29861ace7ceaSPaolo Bonzini         qemu_co_queue_wait(&cancel_pdu->complete, NULL);
298718adde86SGreg Kurz         if (!qemu_co_queue_next(&cancel_pdu->complete)) {
298860ce86c7SWei Liu             cancel_pdu->cancelled = 0;
298960ce86c7SWei Liu             pdu_free(cancel_pdu);
299060ce86c7SWei Liu         }
299118adde86SGreg Kurz     }
299260ce86c7SWei Liu     pdu_complete(pdu, 7);
299360ce86c7SWei Liu }
299460ce86c7SWei Liu 
29958440e22eSGreg Kurz static void coroutine_fn v9fs_link(void *opaque)
299660ce86c7SWei Liu {
299760ce86c7SWei Liu     V9fsPDU *pdu = opaque;
299860ce86c7SWei Liu     int32_t dfid, oldfid;
299960ce86c7SWei Liu     V9fsFidState *dfidp, *oldfidp;
300060ce86c7SWei Liu     V9fsString name;
300160ce86c7SWei Liu     size_t offset = 7;
300260ce86c7SWei Liu     int err = 0;
300360ce86c7SWei Liu 
300460ce86c7SWei Liu     v9fs_string_init(&name);
300560ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
300660ce86c7SWei Liu     if (err < 0) {
300760ce86c7SWei Liu         goto out_nofid;
300860ce86c7SWei Liu     }
300960ce86c7SWei Liu     trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data);
301060ce86c7SWei Liu 
3011fff39a7aSGreg Kurz     if (name_is_illegal(name.data)) {
3012fff39a7aSGreg Kurz         err = -ENOENT;
3013fff39a7aSGreg Kurz         goto out_nofid;
3014fff39a7aSGreg Kurz     }
3015fff39a7aSGreg Kurz 
3016805b5d98SGreg Kurz     if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3017805b5d98SGreg Kurz         err = -EEXIST;
3018805b5d98SGreg Kurz         goto out_nofid;
3019805b5d98SGreg Kurz     }
3020805b5d98SGreg Kurz 
302160ce86c7SWei Liu     dfidp = get_fid(pdu, dfid);
302260ce86c7SWei Liu     if (dfidp == NULL) {
302360ce86c7SWei Liu         err = -ENOENT;
302460ce86c7SWei Liu         goto out_nofid;
302560ce86c7SWei Liu     }
302660ce86c7SWei Liu 
302760ce86c7SWei Liu     oldfidp = get_fid(pdu, oldfid);
302860ce86c7SWei Liu     if (oldfidp == NULL) {
302960ce86c7SWei Liu         err = -ENOENT;
303060ce86c7SWei Liu         goto out;
303160ce86c7SWei Liu     }
303260ce86c7SWei Liu     err = v9fs_co_link(pdu, oldfidp, dfidp, &name);
303360ce86c7SWei Liu     if (!err) {
303460ce86c7SWei Liu         err = offset;
303560ce86c7SWei Liu     }
30364c158678SLi Qiang     put_fid(pdu, oldfidp);
303760ce86c7SWei Liu out:
303860ce86c7SWei Liu     put_fid(pdu, dfidp);
303960ce86c7SWei Liu out_nofid:
304060ce86c7SWei Liu     v9fs_string_free(&name);
304160ce86c7SWei Liu     pdu_complete(pdu, err);
304260ce86c7SWei Liu }
304360ce86c7SWei Liu 
304460ce86c7SWei Liu /* Only works with path name based fid */
30458440e22eSGreg Kurz static void coroutine_fn v9fs_remove(void *opaque)
304660ce86c7SWei Liu {
304760ce86c7SWei Liu     int32_t fid;
304860ce86c7SWei Liu     int err = 0;
304960ce86c7SWei Liu     size_t offset = 7;
305060ce86c7SWei Liu     V9fsFidState *fidp;
305160ce86c7SWei Liu     V9fsPDU *pdu = opaque;
305260ce86c7SWei Liu 
305360ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "d", &fid);
305460ce86c7SWei Liu     if (err < 0) {
305560ce86c7SWei Liu         goto out_nofid;
305660ce86c7SWei Liu     }
305760ce86c7SWei Liu     trace_v9fs_remove(pdu->tag, pdu->id, fid);
305860ce86c7SWei Liu 
305960ce86c7SWei Liu     fidp = get_fid(pdu, fid);
306060ce86c7SWei Liu     if (fidp == NULL) {
306160ce86c7SWei Liu         err = -EINVAL;
306260ce86c7SWei Liu         goto out_nofid;
306360ce86c7SWei Liu     }
306460ce86c7SWei Liu     /* if fs driver is not path based, return EOPNOTSUPP */
306560ce86c7SWei Liu     if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
306660ce86c7SWei Liu         err = -EOPNOTSUPP;
306760ce86c7SWei Liu         goto out_err;
306860ce86c7SWei Liu     }
306960ce86c7SWei Liu     /*
307060ce86c7SWei Liu      * IF the file is unlinked, we cannot reopen
307160ce86c7SWei Liu      * the file later. So don't reclaim fd
307260ce86c7SWei Liu      */
307360ce86c7SWei Liu     err = v9fs_mark_fids_unreclaim(pdu, &fidp->path);
307460ce86c7SWei Liu     if (err < 0) {
307560ce86c7SWei Liu         goto out_err;
307660ce86c7SWei Liu     }
307760ce86c7SWei Liu     err = v9fs_co_remove(pdu, &fidp->path);
307860ce86c7SWei Liu     if (!err) {
307960ce86c7SWei Liu         err = offset;
308060ce86c7SWei Liu     }
308160ce86c7SWei Liu out_err:
308260ce86c7SWei Liu     /* For TREMOVE we need to clunk the fid even on failed remove */
308360ce86c7SWei Liu     clunk_fid(pdu->s, fidp->fid);
308460ce86c7SWei Liu     put_fid(pdu, fidp);
308560ce86c7SWei Liu out_nofid:
308660ce86c7SWei Liu     pdu_complete(pdu, err);
308760ce86c7SWei Liu }
308860ce86c7SWei Liu 
30898440e22eSGreg Kurz static void coroutine_fn v9fs_unlinkat(void *opaque)
309060ce86c7SWei Liu {
309160ce86c7SWei Liu     int err = 0;
309260ce86c7SWei Liu     V9fsString name;
309367e87345SKeno Fischer     int32_t dfid, flags, rflags = 0;
309460ce86c7SWei Liu     size_t offset = 7;
309560ce86c7SWei Liu     V9fsPath path;
309660ce86c7SWei Liu     V9fsFidState *dfidp;
309760ce86c7SWei Liu     V9fsPDU *pdu = opaque;
309860ce86c7SWei Liu 
309960ce86c7SWei Liu     v9fs_string_init(&name);
310060ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags);
310160ce86c7SWei Liu     if (err < 0) {
310260ce86c7SWei Liu         goto out_nofid;
310360ce86c7SWei Liu     }
3104fff39a7aSGreg Kurz 
3105fff39a7aSGreg Kurz     if (name_is_illegal(name.data)) {
3106fff39a7aSGreg Kurz         err = -ENOENT;
3107fff39a7aSGreg Kurz         goto out_nofid;
3108fff39a7aSGreg Kurz     }
3109fff39a7aSGreg Kurz 
3110805b5d98SGreg Kurz     if (!strcmp(".", name.data)) {
3111805b5d98SGreg Kurz         err = -EINVAL;
3112805b5d98SGreg Kurz         goto out_nofid;
3113805b5d98SGreg Kurz     }
3114805b5d98SGreg Kurz 
3115805b5d98SGreg Kurz     if (!strcmp("..", name.data)) {
3116805b5d98SGreg Kurz         err = -ENOTEMPTY;
3117805b5d98SGreg Kurz         goto out_nofid;
3118805b5d98SGreg Kurz     }
3119805b5d98SGreg Kurz 
312067e87345SKeno Fischer     if (flags & ~P9_DOTL_AT_REMOVEDIR) {
312167e87345SKeno Fischer         err = -EINVAL;
312267e87345SKeno Fischer         goto out_nofid;
312367e87345SKeno Fischer     }
312467e87345SKeno Fischer 
312567e87345SKeno Fischer     if (flags & P9_DOTL_AT_REMOVEDIR) {
312667e87345SKeno Fischer         rflags |= AT_REMOVEDIR;
312767e87345SKeno Fischer     }
312867e87345SKeno Fischer 
312960ce86c7SWei Liu     dfidp = get_fid(pdu, dfid);
313060ce86c7SWei Liu     if (dfidp == NULL) {
313160ce86c7SWei Liu         err = -EINVAL;
313260ce86c7SWei Liu         goto out_nofid;
313360ce86c7SWei Liu     }
313460ce86c7SWei Liu     /*
313560ce86c7SWei Liu      * IF the file is unlinked, we cannot reopen
313660ce86c7SWei Liu      * the file later. So don't reclaim fd
313760ce86c7SWei Liu      */
313860ce86c7SWei Liu     v9fs_path_init(&path);
313960ce86c7SWei Liu     err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path);
314060ce86c7SWei Liu     if (err < 0) {
314160ce86c7SWei Liu         goto out_err;
314260ce86c7SWei Liu     }
314360ce86c7SWei Liu     err = v9fs_mark_fids_unreclaim(pdu, &path);
314460ce86c7SWei Liu     if (err < 0) {
314560ce86c7SWei Liu         goto out_err;
314660ce86c7SWei Liu     }
314767e87345SKeno Fischer     err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, rflags);
314860ce86c7SWei Liu     if (!err) {
314960ce86c7SWei Liu         err = offset;
315060ce86c7SWei Liu     }
315160ce86c7SWei Liu out_err:
315260ce86c7SWei Liu     put_fid(pdu, dfidp);
315360ce86c7SWei Liu     v9fs_path_free(&path);
315460ce86c7SWei Liu out_nofid:
315560ce86c7SWei Liu     pdu_complete(pdu, err);
315660ce86c7SWei Liu     v9fs_string_free(&name);
315760ce86c7SWei Liu }
315860ce86c7SWei Liu 
315960ce86c7SWei Liu 
316060ce86c7SWei Liu /* Only works with path name based fid */
31618440e22eSGreg Kurz static int coroutine_fn v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp,
31628440e22eSGreg Kurz                                              int32_t newdirfid,
31638440e22eSGreg Kurz                                              V9fsString *name)
316460ce86c7SWei Liu {
316560ce86c7SWei Liu     int err = 0;
316660ce86c7SWei Liu     V9fsPath new_path;
316760ce86c7SWei Liu     V9fsFidState *tfidp;
316860ce86c7SWei Liu     V9fsState *s = pdu->s;
316960ce86c7SWei Liu     V9fsFidState *dirfidp = NULL;
317060ce86c7SWei Liu 
317160ce86c7SWei Liu     v9fs_path_init(&new_path);
317260ce86c7SWei Liu     if (newdirfid != -1) {
317360ce86c7SWei Liu         dirfidp = get_fid(pdu, newdirfid);
317460ce86c7SWei Liu         if (dirfidp == NULL) {
3175b858e80aSDaniel Henrique Barboza             return -ENOENT;
317660ce86c7SWei Liu         }
317749dd946bSGreg Kurz         if (fidp->fid_type != P9_FID_NONE) {
317849dd946bSGreg Kurz             err = -EINVAL;
317949dd946bSGreg Kurz             goto out;
318049dd946bSGreg Kurz         }
31814fa62005SGreg Kurz         err = v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path);
31824fa62005SGreg Kurz         if (err < 0) {
31834fa62005SGreg Kurz             goto out;
31844fa62005SGreg Kurz         }
318560ce86c7SWei Liu     } else {
31864d8bc733SJan Dakinevich         char *dir_name = g_path_get_dirname(fidp->path.data);
31874d8bc733SJan Dakinevich         V9fsPath dir_path;
31884d8bc733SJan Dakinevich 
31894d8bc733SJan Dakinevich         v9fs_path_init(&dir_path);
31904d8bc733SJan Dakinevich         v9fs_path_sprintf(&dir_path, "%s", dir_name);
31914d8bc733SJan Dakinevich         g_free(dir_name);
31924d8bc733SJan Dakinevich 
31934d8bc733SJan Dakinevich         err = v9fs_co_name_to_path(pdu, &dir_path, name->data, &new_path);
31944d8bc733SJan Dakinevich         v9fs_path_free(&dir_path);
31954fa62005SGreg Kurz         if (err < 0) {
31964fa62005SGreg Kurz             goto out;
31974fa62005SGreg Kurz         }
319860ce86c7SWei Liu     }
319960ce86c7SWei Liu     err = v9fs_co_rename(pdu, &fidp->path, &new_path);
320060ce86c7SWei Liu     if (err < 0) {
320160ce86c7SWei Liu         goto out;
320260ce86c7SWei Liu     }
320360ce86c7SWei Liu     /*
320460ce86c7SWei Liu      * Fixup fid's pointing to the old name to
320560ce86c7SWei Liu      * start pointing to the new name
320660ce86c7SWei Liu      */
3207feabd6cfSGreg Kurz     QSIMPLEQ_FOREACH(tfidp, &s->fid_list, next) {
320860ce86c7SWei Liu         if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) {
320960ce86c7SWei Liu             /* replace the name */
321060ce86c7SWei Liu             v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data));
321160ce86c7SWei Liu         }
321260ce86c7SWei Liu     }
321360ce86c7SWei Liu out:
321460ce86c7SWei Liu     if (dirfidp) {
321560ce86c7SWei Liu         put_fid(pdu, dirfidp);
321660ce86c7SWei Liu     }
321760ce86c7SWei Liu     v9fs_path_free(&new_path);
321860ce86c7SWei Liu     return err;
321960ce86c7SWei Liu }
322060ce86c7SWei Liu 
322160ce86c7SWei Liu /* Only works with path name based fid */
32228440e22eSGreg Kurz static void coroutine_fn v9fs_rename(void *opaque)
322360ce86c7SWei Liu {
322460ce86c7SWei Liu     int32_t fid;
322560ce86c7SWei Liu     ssize_t err = 0;
322660ce86c7SWei Liu     size_t offset = 7;
322760ce86c7SWei Liu     V9fsString name;
322860ce86c7SWei Liu     int32_t newdirfid;
322960ce86c7SWei Liu     V9fsFidState *fidp;
323060ce86c7SWei Liu     V9fsPDU *pdu = opaque;
323160ce86c7SWei Liu     V9fsState *s = pdu->s;
323260ce86c7SWei Liu 
323360ce86c7SWei Liu     v9fs_string_init(&name);
323460ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name);
323560ce86c7SWei Liu     if (err < 0) {
323660ce86c7SWei Liu         goto out_nofid;
323760ce86c7SWei Liu     }
3238fff39a7aSGreg Kurz 
3239fff39a7aSGreg Kurz     if (name_is_illegal(name.data)) {
3240fff39a7aSGreg Kurz         err = -ENOENT;
3241fff39a7aSGreg Kurz         goto out_nofid;
3242fff39a7aSGreg Kurz     }
3243fff39a7aSGreg Kurz 
3244805b5d98SGreg Kurz     if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3245805b5d98SGreg Kurz         err = -EISDIR;
3246805b5d98SGreg Kurz         goto out_nofid;
3247805b5d98SGreg Kurz     }
3248805b5d98SGreg Kurz 
324960ce86c7SWei Liu     fidp = get_fid(pdu, fid);
325060ce86c7SWei Liu     if (fidp == NULL) {
325160ce86c7SWei Liu         err = -ENOENT;
325260ce86c7SWei Liu         goto out_nofid;
325360ce86c7SWei Liu     }
325449dd946bSGreg Kurz     if (fidp->fid_type != P9_FID_NONE) {
325549dd946bSGreg Kurz         err = -EINVAL;
325649dd946bSGreg Kurz         goto out;
325749dd946bSGreg Kurz     }
325860ce86c7SWei Liu     /* if fs driver is not path based, return EOPNOTSUPP */
325960ce86c7SWei Liu     if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
326060ce86c7SWei Liu         err = -EOPNOTSUPP;
326160ce86c7SWei Liu         goto out;
326260ce86c7SWei Liu     }
326360ce86c7SWei Liu     v9fs_path_write_lock(s);
326460ce86c7SWei Liu     err = v9fs_complete_rename(pdu, fidp, newdirfid, &name);
326560ce86c7SWei Liu     v9fs_path_unlock(s);
326660ce86c7SWei Liu     if (!err) {
326760ce86c7SWei Liu         err = offset;
326860ce86c7SWei Liu     }
326960ce86c7SWei Liu out:
327060ce86c7SWei Liu     put_fid(pdu, fidp);
327160ce86c7SWei Liu out_nofid:
327260ce86c7SWei Liu     pdu_complete(pdu, err);
327360ce86c7SWei Liu     v9fs_string_free(&name);
327460ce86c7SWei Liu }
327560ce86c7SWei Liu 
32764fa62005SGreg Kurz static int coroutine_fn v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir,
32778440e22eSGreg Kurz                                            V9fsString *old_name,
32788440e22eSGreg Kurz                                            V9fsPath *newdir,
327960ce86c7SWei Liu                                            V9fsString *new_name)
328060ce86c7SWei Liu {
328160ce86c7SWei Liu     V9fsFidState *tfidp;
328260ce86c7SWei Liu     V9fsPath oldpath, newpath;
328360ce86c7SWei Liu     V9fsState *s = pdu->s;
32844fa62005SGreg Kurz     int err;
328560ce86c7SWei Liu 
328660ce86c7SWei Liu     v9fs_path_init(&oldpath);
328760ce86c7SWei Liu     v9fs_path_init(&newpath);
32884fa62005SGreg Kurz     err = v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath);
32894fa62005SGreg Kurz     if (err < 0) {
32904fa62005SGreg Kurz         goto out;
32914fa62005SGreg Kurz     }
32924fa62005SGreg Kurz     err = v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath);
32934fa62005SGreg Kurz     if (err < 0) {
32944fa62005SGreg Kurz         goto out;
32954fa62005SGreg Kurz     }
329660ce86c7SWei Liu 
329760ce86c7SWei Liu     /*
329860ce86c7SWei Liu      * Fixup fid's pointing to the old name to
329960ce86c7SWei Liu      * start pointing to the new name
330060ce86c7SWei Liu      */
3301feabd6cfSGreg Kurz     QSIMPLEQ_FOREACH(tfidp, &s->fid_list, next) {
330260ce86c7SWei Liu         if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) {
330360ce86c7SWei Liu             /* replace the name */
330460ce86c7SWei Liu             v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data));
330560ce86c7SWei Liu         }
330660ce86c7SWei Liu     }
33074fa62005SGreg Kurz out:
330860ce86c7SWei Liu     v9fs_path_free(&oldpath);
330960ce86c7SWei Liu     v9fs_path_free(&newpath);
33104fa62005SGreg Kurz     return err;
331160ce86c7SWei Liu }
331260ce86c7SWei Liu 
33138440e22eSGreg Kurz static int coroutine_fn v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid,
33148440e22eSGreg Kurz                                                V9fsString *old_name,
33158440e22eSGreg Kurz                                                int32_t newdirfid,
331660ce86c7SWei Liu                                                V9fsString *new_name)
331760ce86c7SWei Liu {
331860ce86c7SWei Liu     int err = 0;
331960ce86c7SWei Liu     V9fsState *s = pdu->s;
332060ce86c7SWei Liu     V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL;
332160ce86c7SWei Liu 
332260ce86c7SWei Liu     olddirfidp = get_fid(pdu, olddirfid);
332360ce86c7SWei Liu     if (olddirfidp == NULL) {
332460ce86c7SWei Liu         err = -ENOENT;
332560ce86c7SWei Liu         goto out;
332660ce86c7SWei Liu     }
332760ce86c7SWei Liu     if (newdirfid != -1) {
332860ce86c7SWei Liu         newdirfidp = get_fid(pdu, newdirfid);
332960ce86c7SWei Liu         if (newdirfidp == NULL) {
333060ce86c7SWei Liu             err = -ENOENT;
333160ce86c7SWei Liu             goto out;
333260ce86c7SWei Liu         }
333360ce86c7SWei Liu     } else {
333460ce86c7SWei Liu         newdirfidp = get_fid(pdu, olddirfid);
333560ce86c7SWei Liu     }
333660ce86c7SWei Liu 
333760ce86c7SWei Liu     err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name,
333860ce86c7SWei Liu                            &newdirfidp->path, new_name);
333960ce86c7SWei Liu     if (err < 0) {
334060ce86c7SWei Liu         goto out;
334160ce86c7SWei Liu     }
334260ce86c7SWei Liu     if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
334360ce86c7SWei Liu         /* Only for path based fid  we need to do the below fixup */
33444fa62005SGreg Kurz         err = v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name,
334560ce86c7SWei Liu                                  &newdirfidp->path, new_name);
334660ce86c7SWei Liu     }
334760ce86c7SWei Liu out:
334860ce86c7SWei Liu     if (olddirfidp) {
334960ce86c7SWei Liu         put_fid(pdu, olddirfidp);
335060ce86c7SWei Liu     }
335160ce86c7SWei Liu     if (newdirfidp) {
335260ce86c7SWei Liu         put_fid(pdu, newdirfidp);
335360ce86c7SWei Liu     }
335460ce86c7SWei Liu     return err;
335560ce86c7SWei Liu }
335660ce86c7SWei Liu 
33578440e22eSGreg Kurz static void coroutine_fn v9fs_renameat(void *opaque)
335860ce86c7SWei Liu {
335960ce86c7SWei Liu     ssize_t err = 0;
336060ce86c7SWei Liu     size_t offset = 7;
336160ce86c7SWei Liu     V9fsPDU *pdu = opaque;
336260ce86c7SWei Liu     V9fsState *s = pdu->s;
336360ce86c7SWei Liu     int32_t olddirfid, newdirfid;
336460ce86c7SWei Liu     V9fsString old_name, new_name;
336560ce86c7SWei Liu 
336660ce86c7SWei Liu     v9fs_string_init(&old_name);
336760ce86c7SWei Liu     v9fs_string_init(&new_name);
336860ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid,
336960ce86c7SWei Liu                         &old_name, &newdirfid, &new_name);
337060ce86c7SWei Liu     if (err < 0) {
337160ce86c7SWei Liu         goto out_err;
337260ce86c7SWei Liu     }
337360ce86c7SWei Liu 
3374fff39a7aSGreg Kurz     if (name_is_illegal(old_name.data) || name_is_illegal(new_name.data)) {
3375fff39a7aSGreg Kurz         err = -ENOENT;
3376fff39a7aSGreg Kurz         goto out_err;
3377fff39a7aSGreg Kurz     }
3378fff39a7aSGreg Kurz 
3379805b5d98SGreg Kurz     if (!strcmp(".", old_name.data) || !strcmp("..", old_name.data) ||
3380805b5d98SGreg Kurz         !strcmp(".", new_name.data) || !strcmp("..", new_name.data)) {
3381805b5d98SGreg Kurz         err = -EISDIR;
3382805b5d98SGreg Kurz         goto out_err;
3383805b5d98SGreg Kurz     }
3384805b5d98SGreg Kurz 
338560ce86c7SWei Liu     v9fs_path_write_lock(s);
338660ce86c7SWei Liu     err = v9fs_complete_renameat(pdu, olddirfid,
338760ce86c7SWei Liu                                  &old_name, newdirfid, &new_name);
338860ce86c7SWei Liu     v9fs_path_unlock(s);
338960ce86c7SWei Liu     if (!err) {
339060ce86c7SWei Liu         err = offset;
339160ce86c7SWei Liu     }
339260ce86c7SWei Liu 
339360ce86c7SWei Liu out_err:
339460ce86c7SWei Liu     pdu_complete(pdu, err);
339560ce86c7SWei Liu     v9fs_string_free(&old_name);
339660ce86c7SWei Liu     v9fs_string_free(&new_name);
339760ce86c7SWei Liu }
339860ce86c7SWei Liu 
33998440e22eSGreg Kurz static void coroutine_fn v9fs_wstat(void *opaque)
340060ce86c7SWei Liu {
340160ce86c7SWei Liu     int32_t fid;
340260ce86c7SWei Liu     int err = 0;
340360ce86c7SWei Liu     int16_t unused;
340460ce86c7SWei Liu     V9fsStat v9stat;
340560ce86c7SWei Liu     size_t offset = 7;
340660ce86c7SWei Liu     struct stat stbuf;
340760ce86c7SWei Liu     V9fsFidState *fidp;
340860ce86c7SWei Liu     V9fsPDU *pdu = opaque;
34091d203986SGreg Kurz     V9fsState *s = pdu->s;
341060ce86c7SWei Liu 
341160ce86c7SWei Liu     v9fs_stat_init(&v9stat);
341260ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat);
341360ce86c7SWei Liu     if (err < 0) {
341460ce86c7SWei Liu         goto out_nofid;
341560ce86c7SWei Liu     }
341660ce86c7SWei Liu     trace_v9fs_wstat(pdu->tag, pdu->id, fid,
341760ce86c7SWei Liu                      v9stat.mode, v9stat.atime, v9stat.mtime);
341860ce86c7SWei Liu 
341960ce86c7SWei Liu     fidp = get_fid(pdu, fid);
342060ce86c7SWei Liu     if (fidp == NULL) {
342160ce86c7SWei Liu         err = -EINVAL;
342260ce86c7SWei Liu         goto out_nofid;
342360ce86c7SWei Liu     }
342460ce86c7SWei Liu     /* do we need to sync the file? */
342560ce86c7SWei Liu     if (donttouch_stat(&v9stat)) {
342660ce86c7SWei Liu         err = v9fs_co_fsync(pdu, fidp, 0);
342760ce86c7SWei Liu         goto out;
342860ce86c7SWei Liu     }
342960ce86c7SWei Liu     if (v9stat.mode != -1) {
343060ce86c7SWei Liu         uint32_t v9_mode;
343160ce86c7SWei Liu         err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
343260ce86c7SWei Liu         if (err < 0) {
343360ce86c7SWei Liu             goto out;
343460ce86c7SWei Liu         }
343560ce86c7SWei Liu         v9_mode = stat_to_v9mode(&stbuf);
343660ce86c7SWei Liu         if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
343760ce86c7SWei Liu             (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
343860ce86c7SWei Liu             /* Attempting to change the type */
343960ce86c7SWei Liu             err = -EIO;
344060ce86c7SWei Liu             goto out;
344160ce86c7SWei Liu         }
344260ce86c7SWei Liu         err = v9fs_co_chmod(pdu, &fidp->path,
344360ce86c7SWei Liu                             v9mode_to_mode(v9stat.mode,
344460ce86c7SWei Liu                                            &v9stat.extension));
344560ce86c7SWei Liu         if (err < 0) {
344660ce86c7SWei Liu             goto out;
344760ce86c7SWei Liu         }
344860ce86c7SWei Liu     }
344960ce86c7SWei Liu     if (v9stat.mtime != -1 || v9stat.atime != -1) {
345060ce86c7SWei Liu         struct timespec times[2];
345160ce86c7SWei Liu         if (v9stat.atime != -1) {
345260ce86c7SWei Liu             times[0].tv_sec = v9stat.atime;
345360ce86c7SWei Liu             times[0].tv_nsec = 0;
345460ce86c7SWei Liu         } else {
345560ce86c7SWei Liu             times[0].tv_nsec = UTIME_OMIT;
345660ce86c7SWei Liu         }
345760ce86c7SWei Liu         if (v9stat.mtime != -1) {
345860ce86c7SWei Liu             times[1].tv_sec = v9stat.mtime;
345960ce86c7SWei Liu             times[1].tv_nsec = 0;
346060ce86c7SWei Liu         } else {
346160ce86c7SWei Liu             times[1].tv_nsec = UTIME_OMIT;
346260ce86c7SWei Liu         }
346360ce86c7SWei Liu         err = v9fs_co_utimensat(pdu, &fidp->path, times);
346460ce86c7SWei Liu         if (err < 0) {
346560ce86c7SWei Liu             goto out;
346660ce86c7SWei Liu         }
346760ce86c7SWei Liu     }
346860ce86c7SWei Liu     if (v9stat.n_gid != -1 || v9stat.n_uid != -1) {
346960ce86c7SWei Liu         err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid);
347060ce86c7SWei Liu         if (err < 0) {
347160ce86c7SWei Liu             goto out;
347260ce86c7SWei Liu         }
347360ce86c7SWei Liu     }
347460ce86c7SWei Liu     if (v9stat.name.size != 0) {
34751d203986SGreg Kurz         v9fs_path_write_lock(s);
347660ce86c7SWei Liu         err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name);
34771d203986SGreg Kurz         v9fs_path_unlock(s);
347860ce86c7SWei Liu         if (err < 0) {
347960ce86c7SWei Liu             goto out;
348060ce86c7SWei Liu         }
348160ce86c7SWei Liu     }
348260ce86c7SWei Liu     if (v9stat.length != -1) {
348360ce86c7SWei Liu         err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length);
348460ce86c7SWei Liu         if (err < 0) {
348560ce86c7SWei Liu             goto out;
348660ce86c7SWei Liu         }
348760ce86c7SWei Liu     }
348860ce86c7SWei Liu     err = offset;
348960ce86c7SWei Liu out:
349060ce86c7SWei Liu     put_fid(pdu, fidp);
349160ce86c7SWei Liu out_nofid:
349260ce86c7SWei Liu     v9fs_stat_free(&v9stat);
349360ce86c7SWei Liu     pdu_complete(pdu, err);
349460ce86c7SWei Liu }
349560ce86c7SWei Liu 
349660ce86c7SWei Liu static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf)
349760ce86c7SWei Liu {
349860ce86c7SWei Liu     uint32_t f_type;
349960ce86c7SWei Liu     uint32_t f_bsize;
350060ce86c7SWei Liu     uint64_t f_blocks;
350160ce86c7SWei Liu     uint64_t f_bfree;
350260ce86c7SWei Liu     uint64_t f_bavail;
350360ce86c7SWei Liu     uint64_t f_files;
350460ce86c7SWei Liu     uint64_t f_ffree;
350560ce86c7SWei Liu     uint64_t fsid_val;
350660ce86c7SWei Liu     uint32_t f_namelen;
350760ce86c7SWei Liu     size_t offset = 7;
350860ce86c7SWei Liu     int32_t bsize_factor;
350960ce86c7SWei Liu 
351060ce86c7SWei Liu     /*
351160ce86c7SWei Liu      * compute bsize factor based on host file system block size
351260ce86c7SWei Liu      * and client msize
351360ce86c7SWei Liu      */
351460ce86c7SWei Liu     bsize_factor = (s->msize - P9_IOHDRSZ) / stbuf->f_bsize;
351560ce86c7SWei Liu     if (!bsize_factor) {
351660ce86c7SWei Liu         bsize_factor = 1;
351760ce86c7SWei Liu     }
351860ce86c7SWei Liu     f_type  = stbuf->f_type;
351960ce86c7SWei Liu     f_bsize = stbuf->f_bsize;
352060ce86c7SWei Liu     f_bsize *= bsize_factor;
352160ce86c7SWei Liu     /*
352260ce86c7SWei Liu      * f_bsize is adjusted(multiplied) by bsize factor, so we need to
352360ce86c7SWei Liu      * adjust(divide) the number of blocks, free blocks and available
352460ce86c7SWei Liu      * blocks by bsize factor
352560ce86c7SWei Liu      */
352660ce86c7SWei Liu     f_blocks = stbuf->f_blocks / bsize_factor;
352760ce86c7SWei Liu     f_bfree  = stbuf->f_bfree / bsize_factor;
352860ce86c7SWei Liu     f_bavail = stbuf->f_bavail / bsize_factor;
352960ce86c7SWei Liu     f_files  = stbuf->f_files;
353060ce86c7SWei Liu     f_ffree  = stbuf->f_ffree;
3531f41db099SKeno Fischer #ifdef CONFIG_DARWIN
3532f41db099SKeno Fischer     fsid_val = (unsigned int)stbuf->f_fsid.val[0] |
3533f41db099SKeno Fischer                (unsigned long long)stbuf->f_fsid.val[1] << 32;
3534f41db099SKeno Fischer     f_namelen = NAME_MAX;
3535f41db099SKeno Fischer #else
353660ce86c7SWei Liu     fsid_val = (unsigned int) stbuf->f_fsid.__val[0] |
353760ce86c7SWei Liu                (unsigned long long)stbuf->f_fsid.__val[1] << 32;
353860ce86c7SWei Liu     f_namelen = stbuf->f_namelen;
3539f41db099SKeno Fischer #endif
354060ce86c7SWei Liu 
354160ce86c7SWei Liu     return pdu_marshal(pdu, offset, "ddqqqqqqd",
354260ce86c7SWei Liu                        f_type, f_bsize, f_blocks, f_bfree,
354360ce86c7SWei Liu                        f_bavail, f_files, f_ffree,
354460ce86c7SWei Liu                        fsid_val, f_namelen);
354560ce86c7SWei Liu }
354660ce86c7SWei Liu 
35478440e22eSGreg Kurz static void coroutine_fn v9fs_statfs(void *opaque)
354860ce86c7SWei Liu {
354960ce86c7SWei Liu     int32_t fid;
355060ce86c7SWei Liu     ssize_t retval = 0;
355160ce86c7SWei Liu     size_t offset = 7;
355260ce86c7SWei Liu     V9fsFidState *fidp;
355360ce86c7SWei Liu     struct statfs stbuf;
355460ce86c7SWei Liu     V9fsPDU *pdu = opaque;
355560ce86c7SWei Liu     V9fsState *s = pdu->s;
355660ce86c7SWei Liu 
355760ce86c7SWei Liu     retval = pdu_unmarshal(pdu, offset, "d", &fid);
355860ce86c7SWei Liu     if (retval < 0) {
355960ce86c7SWei Liu         goto out_nofid;
356060ce86c7SWei Liu     }
356160ce86c7SWei Liu     fidp = get_fid(pdu, fid);
356260ce86c7SWei Liu     if (fidp == NULL) {
356360ce86c7SWei Liu         retval = -ENOENT;
356460ce86c7SWei Liu         goto out_nofid;
356560ce86c7SWei Liu     }
356660ce86c7SWei Liu     retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf);
356760ce86c7SWei Liu     if (retval < 0) {
356860ce86c7SWei Liu         goto out;
356960ce86c7SWei Liu     }
357060ce86c7SWei Liu     retval = v9fs_fill_statfs(s, pdu, &stbuf);
357160ce86c7SWei Liu     if (retval < 0) {
357260ce86c7SWei Liu         goto out;
357360ce86c7SWei Liu     }
357460ce86c7SWei Liu     retval += offset;
357560ce86c7SWei Liu out:
357660ce86c7SWei Liu     put_fid(pdu, fidp);
357760ce86c7SWei Liu out_nofid:
357860ce86c7SWei Liu     pdu_complete(pdu, retval);
357960ce86c7SWei Liu }
358060ce86c7SWei Liu 
35818440e22eSGreg Kurz static void coroutine_fn v9fs_mknod(void *opaque)
358260ce86c7SWei Liu {
358360ce86c7SWei Liu 
358460ce86c7SWei Liu     int mode;
358560ce86c7SWei Liu     gid_t gid;
358660ce86c7SWei Liu     int32_t fid;
358760ce86c7SWei Liu     V9fsQID qid;
358860ce86c7SWei Liu     int err = 0;
358960ce86c7SWei Liu     int major, minor;
359060ce86c7SWei Liu     size_t offset = 7;
359160ce86c7SWei Liu     V9fsString name;
359260ce86c7SWei Liu     struct stat stbuf;
359360ce86c7SWei Liu     V9fsFidState *fidp;
359460ce86c7SWei Liu     V9fsPDU *pdu = opaque;
359560ce86c7SWei Liu 
359660ce86c7SWei Liu     v9fs_string_init(&name);
359760ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode,
359860ce86c7SWei Liu                         &major, &minor, &gid);
359960ce86c7SWei Liu     if (err < 0) {
360060ce86c7SWei Liu         goto out_nofid;
360160ce86c7SWei Liu     }
360260ce86c7SWei Liu     trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor);
360360ce86c7SWei Liu 
3604fff39a7aSGreg Kurz     if (name_is_illegal(name.data)) {
3605fff39a7aSGreg Kurz         err = -ENOENT;
3606fff39a7aSGreg Kurz         goto out_nofid;
3607fff39a7aSGreg Kurz     }
3608fff39a7aSGreg Kurz 
3609805b5d98SGreg Kurz     if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3610805b5d98SGreg Kurz         err = -EEXIST;
3611805b5d98SGreg Kurz         goto out_nofid;
3612805b5d98SGreg Kurz     }
3613805b5d98SGreg Kurz 
361460ce86c7SWei Liu     fidp = get_fid(pdu, fid);
361560ce86c7SWei Liu     if (fidp == NULL) {
361660ce86c7SWei Liu         err = -ENOENT;
361760ce86c7SWei Liu         goto out_nofid;
361860ce86c7SWei Liu     }
361960ce86c7SWei Liu     err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid,
362060ce86c7SWei Liu                         makedev(major, minor), mode, &stbuf);
362160ce86c7SWei Liu     if (err < 0) {
362260ce86c7SWei Liu         goto out;
362360ce86c7SWei Liu     }
36243b5ee9e8SAntonios Motakis     err = stat_to_qid(pdu, &stbuf, &qid);
36253b5ee9e8SAntonios Motakis     if (err < 0) {
36263b5ee9e8SAntonios Motakis         goto out;
36273b5ee9e8SAntonios Motakis     }
362860ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "Q", &qid);
362960ce86c7SWei Liu     if (err < 0) {
363060ce86c7SWei Liu         goto out;
363160ce86c7SWei Liu     }
363260ce86c7SWei Liu     err += offset;
363360ce86c7SWei Liu     trace_v9fs_mknod_return(pdu->tag, pdu->id,
363460ce86c7SWei Liu                             qid.type, qid.version, qid.path);
363560ce86c7SWei Liu out:
363660ce86c7SWei Liu     put_fid(pdu, fidp);
363760ce86c7SWei Liu out_nofid:
363860ce86c7SWei Liu     pdu_complete(pdu, err);
363960ce86c7SWei Liu     v9fs_string_free(&name);
364060ce86c7SWei Liu }
364160ce86c7SWei Liu 
364260ce86c7SWei Liu /*
364360ce86c7SWei Liu  * Implement posix byte range locking code
364460ce86c7SWei Liu  * Server side handling of locking code is very simple, because 9p server in
364560ce86c7SWei Liu  * QEMU can handle only one client. And most of the lock handling
364660ce86c7SWei Liu  * (like conflict, merging) etc is done by the VFS layer itself, so no need to
364760ce86c7SWei Liu  * do any thing in * qemu 9p server side lock code path.
364860ce86c7SWei Liu  * So when a TLOCK request comes, always return success
364960ce86c7SWei Liu  */
36508440e22eSGreg Kurz static void coroutine_fn v9fs_lock(void *opaque)
365160ce86c7SWei Liu {
365260ce86c7SWei Liu     V9fsFlock flock;
365360ce86c7SWei Liu     size_t offset = 7;
365460ce86c7SWei Liu     struct stat stbuf;
365560ce86c7SWei Liu     V9fsFidState *fidp;
365660ce86c7SWei Liu     int32_t fid, err = 0;
365760ce86c7SWei Liu     V9fsPDU *pdu = opaque;
365860ce86c7SWei Liu 
365960ce86c7SWei Liu     v9fs_string_init(&flock.client_id);
366060ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type,
366160ce86c7SWei Liu                         &flock.flags, &flock.start, &flock.length,
366260ce86c7SWei Liu                         &flock.proc_id, &flock.client_id);
366360ce86c7SWei Liu     if (err < 0) {
366460ce86c7SWei Liu         goto out_nofid;
366560ce86c7SWei Liu     }
366660ce86c7SWei Liu     trace_v9fs_lock(pdu->tag, pdu->id, fid,
366760ce86c7SWei Liu                     flock.type, flock.start, flock.length);
366860ce86c7SWei Liu 
366960ce86c7SWei Liu 
367060ce86c7SWei Liu     /* We support only block flag now (that too ignored currently) */
367160ce86c7SWei Liu     if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) {
367260ce86c7SWei Liu         err = -EINVAL;
367360ce86c7SWei Liu         goto out_nofid;
367460ce86c7SWei Liu     }
367560ce86c7SWei Liu     fidp = get_fid(pdu, fid);
367660ce86c7SWei Liu     if (fidp == NULL) {
367760ce86c7SWei Liu         err = -ENOENT;
367860ce86c7SWei Liu         goto out_nofid;
367960ce86c7SWei Liu     }
368060ce86c7SWei Liu     err = v9fs_co_fstat(pdu, fidp, &stbuf);
368160ce86c7SWei Liu     if (err < 0) {
368260ce86c7SWei Liu         goto out;
368360ce86c7SWei Liu     }
36844bae2b39SPaolo Bonzini     err = pdu_marshal(pdu, offset, "b", P9_LOCK_SUCCESS);
36854bae2b39SPaolo Bonzini     if (err < 0) {
36864bae2b39SPaolo Bonzini         goto out;
36874bae2b39SPaolo Bonzini     }
36884bae2b39SPaolo Bonzini     err += offset;
36894bae2b39SPaolo Bonzini     trace_v9fs_lock_return(pdu->tag, pdu->id, P9_LOCK_SUCCESS);
369060ce86c7SWei Liu out:
369160ce86c7SWei Liu     put_fid(pdu, fidp);
369260ce86c7SWei Liu out_nofid:
369360ce86c7SWei Liu     pdu_complete(pdu, err);
369460ce86c7SWei Liu     v9fs_string_free(&flock.client_id);
369560ce86c7SWei Liu }
369660ce86c7SWei Liu 
369760ce86c7SWei Liu /*
369860ce86c7SWei Liu  * When a TGETLOCK request comes, always return success because all lock
369960ce86c7SWei Liu  * handling is done by client's VFS layer.
370060ce86c7SWei Liu  */
37018440e22eSGreg Kurz static void coroutine_fn v9fs_getlock(void *opaque)
370260ce86c7SWei Liu {
370360ce86c7SWei Liu     size_t offset = 7;
370460ce86c7SWei Liu     struct stat stbuf;
370560ce86c7SWei Liu     V9fsFidState *fidp;
370660ce86c7SWei Liu     V9fsGetlock glock;
370760ce86c7SWei Liu     int32_t fid, err = 0;
370860ce86c7SWei Liu     V9fsPDU *pdu = opaque;
370960ce86c7SWei Liu 
371060ce86c7SWei Liu     v9fs_string_init(&glock.client_id);
371160ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type,
371260ce86c7SWei Liu                         &glock.start, &glock.length, &glock.proc_id,
371360ce86c7SWei Liu                         &glock.client_id);
371460ce86c7SWei Liu     if (err < 0) {
371560ce86c7SWei Liu         goto out_nofid;
371660ce86c7SWei Liu     }
371760ce86c7SWei Liu     trace_v9fs_getlock(pdu->tag, pdu->id, fid,
371860ce86c7SWei Liu                        glock.type, glock.start, glock.length);
371960ce86c7SWei Liu 
372060ce86c7SWei Liu     fidp = get_fid(pdu, fid);
372160ce86c7SWei Liu     if (fidp == NULL) {
372260ce86c7SWei Liu         err = -ENOENT;
372360ce86c7SWei Liu         goto out_nofid;
372460ce86c7SWei Liu     }
372560ce86c7SWei Liu     err = v9fs_co_fstat(pdu, fidp, &stbuf);
372660ce86c7SWei Liu     if (err < 0) {
372760ce86c7SWei Liu         goto out;
372860ce86c7SWei Liu     }
372960ce86c7SWei Liu     glock.type = P9_LOCK_TYPE_UNLCK;
373060ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "bqqds", glock.type,
373160ce86c7SWei Liu                           glock.start, glock.length, glock.proc_id,
373260ce86c7SWei Liu                           &glock.client_id);
373360ce86c7SWei Liu     if (err < 0) {
373460ce86c7SWei Liu         goto out;
373560ce86c7SWei Liu     }
373660ce86c7SWei Liu     err += offset;
373760ce86c7SWei Liu     trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start,
373860ce86c7SWei Liu                               glock.length, glock.proc_id);
373960ce86c7SWei Liu out:
374060ce86c7SWei Liu     put_fid(pdu, fidp);
374160ce86c7SWei Liu out_nofid:
374260ce86c7SWei Liu     pdu_complete(pdu, err);
374360ce86c7SWei Liu     v9fs_string_free(&glock.client_id);
374460ce86c7SWei Liu }
374560ce86c7SWei Liu 
37468440e22eSGreg Kurz static void coroutine_fn v9fs_mkdir(void *opaque)
374760ce86c7SWei Liu {
374860ce86c7SWei Liu     V9fsPDU *pdu = opaque;
374960ce86c7SWei Liu     size_t offset = 7;
375060ce86c7SWei Liu     int32_t fid;
375160ce86c7SWei Liu     struct stat stbuf;
375260ce86c7SWei Liu     V9fsQID qid;
375360ce86c7SWei Liu     V9fsString name;
375460ce86c7SWei Liu     V9fsFidState *fidp;
375560ce86c7SWei Liu     gid_t gid;
375660ce86c7SWei Liu     int mode;
375760ce86c7SWei Liu     int err = 0;
375860ce86c7SWei Liu 
375960ce86c7SWei Liu     v9fs_string_init(&name);
376060ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid);
376160ce86c7SWei Liu     if (err < 0) {
376260ce86c7SWei Liu         goto out_nofid;
376360ce86c7SWei Liu     }
376460ce86c7SWei Liu     trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid);
376560ce86c7SWei Liu 
3766fff39a7aSGreg Kurz     if (name_is_illegal(name.data)) {
3767fff39a7aSGreg Kurz         err = -ENOENT;
3768fff39a7aSGreg Kurz         goto out_nofid;
3769fff39a7aSGreg Kurz     }
3770fff39a7aSGreg Kurz 
3771805b5d98SGreg Kurz     if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3772805b5d98SGreg Kurz         err = -EEXIST;
3773805b5d98SGreg Kurz         goto out_nofid;
3774805b5d98SGreg Kurz     }
3775805b5d98SGreg Kurz 
377660ce86c7SWei Liu     fidp = get_fid(pdu, fid);
377760ce86c7SWei Liu     if (fidp == NULL) {
377860ce86c7SWei Liu         err = -ENOENT;
377960ce86c7SWei Liu         goto out_nofid;
378060ce86c7SWei Liu     }
378160ce86c7SWei Liu     err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf);
378260ce86c7SWei Liu     if (err < 0) {
378360ce86c7SWei Liu         goto out;
378460ce86c7SWei Liu     }
37853b5ee9e8SAntonios Motakis     err = stat_to_qid(pdu, &stbuf, &qid);
37863b5ee9e8SAntonios Motakis     if (err < 0) {
37873b5ee9e8SAntonios Motakis         goto out;
37883b5ee9e8SAntonios Motakis     }
378960ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "Q", &qid);
379060ce86c7SWei Liu     if (err < 0) {
379160ce86c7SWei Liu         goto out;
379260ce86c7SWei Liu     }
379360ce86c7SWei Liu     err += offset;
379460ce86c7SWei Liu     trace_v9fs_mkdir_return(pdu->tag, pdu->id,
379560ce86c7SWei Liu                             qid.type, qid.version, qid.path, err);
379660ce86c7SWei Liu out:
379760ce86c7SWei Liu     put_fid(pdu, fidp);
379860ce86c7SWei Liu out_nofid:
379960ce86c7SWei Liu     pdu_complete(pdu, err);
380060ce86c7SWei Liu     v9fs_string_free(&name);
380160ce86c7SWei Liu }
380260ce86c7SWei Liu 
38038440e22eSGreg Kurz static void coroutine_fn v9fs_xattrwalk(void *opaque)
380460ce86c7SWei Liu {
380560ce86c7SWei Liu     int64_t size;
380660ce86c7SWei Liu     V9fsString name;
380760ce86c7SWei Liu     ssize_t err = 0;
380860ce86c7SWei Liu     size_t offset = 7;
380960ce86c7SWei Liu     int32_t fid, newfid;
381060ce86c7SWei Liu     V9fsFidState *file_fidp;
381160ce86c7SWei Liu     V9fsFidState *xattr_fidp = NULL;
381260ce86c7SWei Liu     V9fsPDU *pdu = opaque;
381360ce86c7SWei Liu     V9fsState *s = pdu->s;
381460ce86c7SWei Liu 
381560ce86c7SWei Liu     v9fs_string_init(&name);
381660ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name);
381760ce86c7SWei Liu     if (err < 0) {
381860ce86c7SWei Liu         goto out_nofid;
381960ce86c7SWei Liu     }
382060ce86c7SWei Liu     trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data);
382160ce86c7SWei Liu 
382260ce86c7SWei Liu     file_fidp = get_fid(pdu, fid);
382360ce86c7SWei Liu     if (file_fidp == NULL) {
382460ce86c7SWei Liu         err = -ENOENT;
382560ce86c7SWei Liu         goto out_nofid;
382660ce86c7SWei Liu     }
382760ce86c7SWei Liu     xattr_fidp = alloc_fid(s, newfid);
382860ce86c7SWei Liu     if (xattr_fidp == NULL) {
382960ce86c7SWei Liu         err = -EINVAL;
383060ce86c7SWei Liu         goto out;
383160ce86c7SWei Liu     }
383260ce86c7SWei Liu     v9fs_path_copy(&xattr_fidp->path, &file_fidp->path);
3833ba42ebb8SLi Qiang     if (!v9fs_string_size(&name)) {
383460ce86c7SWei Liu         /*
383560ce86c7SWei Liu          * listxattr request. Get the size first
383660ce86c7SWei Liu          */
383760ce86c7SWei Liu         size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0);
383860ce86c7SWei Liu         if (size < 0) {
383960ce86c7SWei Liu             err = size;
384060ce86c7SWei Liu             clunk_fid(s, xattr_fidp->fid);
384160ce86c7SWei Liu             goto out;
384260ce86c7SWei Liu         }
384360ce86c7SWei Liu         /*
384460ce86c7SWei Liu          * Read the xattr value
384560ce86c7SWei Liu          */
384660ce86c7SWei Liu         xattr_fidp->fs.xattr.len = size;
384760ce86c7SWei Liu         xattr_fidp->fid_type = P9_FID_XATTR;
3848dd28fbbcSLi Qiang         xattr_fidp->fs.xattr.xattrwalk_fid = true;
38497bd92756SPrasad J Pandit         xattr_fidp->fs.xattr.value = g_malloc0(size);
3850a647502cSKeno Fischer         if (size) {
385160ce86c7SWei Liu             err = v9fs_co_llistxattr(pdu, &xattr_fidp->path,
385260ce86c7SWei Liu                                      xattr_fidp->fs.xattr.value,
385360ce86c7SWei Liu                                      xattr_fidp->fs.xattr.len);
385460ce86c7SWei Liu             if (err < 0) {
385560ce86c7SWei Liu                 clunk_fid(s, xattr_fidp->fid);
385660ce86c7SWei Liu                 goto out;
385760ce86c7SWei Liu             }
385860ce86c7SWei Liu         }
385960ce86c7SWei Liu         err = pdu_marshal(pdu, offset, "q", size);
386060ce86c7SWei Liu         if (err < 0) {
386160ce86c7SWei Liu             goto out;
386260ce86c7SWei Liu         }
386360ce86c7SWei Liu         err += offset;
386460ce86c7SWei Liu     } else {
386560ce86c7SWei Liu         /*
386660ce86c7SWei Liu          * specific xattr fid. We check for xattr
386760ce86c7SWei Liu          * presence also collect the xattr size
386860ce86c7SWei Liu          */
386960ce86c7SWei Liu         size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
387060ce86c7SWei Liu                                  &name, NULL, 0);
387160ce86c7SWei Liu         if (size < 0) {
387260ce86c7SWei Liu             err = size;
387360ce86c7SWei Liu             clunk_fid(s, xattr_fidp->fid);
387460ce86c7SWei Liu             goto out;
387560ce86c7SWei Liu         }
387660ce86c7SWei Liu         /*
387760ce86c7SWei Liu          * Read the xattr value
387860ce86c7SWei Liu          */
387960ce86c7SWei Liu         xattr_fidp->fs.xattr.len = size;
388060ce86c7SWei Liu         xattr_fidp->fid_type = P9_FID_XATTR;
3881dd28fbbcSLi Qiang         xattr_fidp->fs.xattr.xattrwalk_fid = true;
38827bd92756SPrasad J Pandit         xattr_fidp->fs.xattr.value = g_malloc0(size);
3883a647502cSKeno Fischer         if (size) {
388460ce86c7SWei Liu             err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
388560ce86c7SWei Liu                                     &name, xattr_fidp->fs.xattr.value,
388660ce86c7SWei Liu                                     xattr_fidp->fs.xattr.len);
388760ce86c7SWei Liu             if (err < 0) {
388860ce86c7SWei Liu                 clunk_fid(s, xattr_fidp->fid);
388960ce86c7SWei Liu                 goto out;
389060ce86c7SWei Liu             }
389160ce86c7SWei Liu         }
389260ce86c7SWei Liu         err = pdu_marshal(pdu, offset, "q", size);
389360ce86c7SWei Liu         if (err < 0) {
389460ce86c7SWei Liu             goto out;
389560ce86c7SWei Liu         }
389660ce86c7SWei Liu         err += offset;
389760ce86c7SWei Liu     }
389860ce86c7SWei Liu     trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size);
389960ce86c7SWei Liu out:
390060ce86c7SWei Liu     put_fid(pdu, file_fidp);
390160ce86c7SWei Liu     if (xattr_fidp) {
390260ce86c7SWei Liu         put_fid(pdu, xattr_fidp);
390360ce86c7SWei Liu     }
390460ce86c7SWei Liu out_nofid:
390560ce86c7SWei Liu     pdu_complete(pdu, err);
390660ce86c7SWei Liu     v9fs_string_free(&name);
390760ce86c7SWei Liu }
390860ce86c7SWei Liu 
39098440e22eSGreg Kurz static void coroutine_fn v9fs_xattrcreate(void *opaque)
391060ce86c7SWei Liu {
3911aca6897fSKeno Fischer     int flags, rflags = 0;
391260ce86c7SWei Liu     int32_t fid;
39133b79ef2cSGreg Kurz     uint64_t size;
391460ce86c7SWei Liu     ssize_t err = 0;
391560ce86c7SWei Liu     V9fsString name;
391660ce86c7SWei Liu     size_t offset = 7;
391760ce86c7SWei Liu     V9fsFidState *file_fidp;
391860ce86c7SWei Liu     V9fsFidState *xattr_fidp;
391960ce86c7SWei Liu     V9fsPDU *pdu = opaque;
392060ce86c7SWei Liu 
392160ce86c7SWei Liu     v9fs_string_init(&name);
392260ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags);
392360ce86c7SWei Liu     if (err < 0) {
392460ce86c7SWei Liu         goto out_nofid;
392560ce86c7SWei Liu     }
392660ce86c7SWei Liu     trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags);
392760ce86c7SWei Liu 
3928aca6897fSKeno Fischer     if (flags & ~(P9_XATTR_CREATE | P9_XATTR_REPLACE)) {
3929aca6897fSKeno Fischer         err = -EINVAL;
3930aca6897fSKeno Fischer         goto out_nofid;
3931aca6897fSKeno Fischer     }
3932aca6897fSKeno Fischer 
3933aca6897fSKeno Fischer     if (flags & P9_XATTR_CREATE) {
3934aca6897fSKeno Fischer         rflags |= XATTR_CREATE;
3935aca6897fSKeno Fischer     }
3936aca6897fSKeno Fischer 
3937aca6897fSKeno Fischer     if (flags & P9_XATTR_REPLACE) {
3938aca6897fSKeno Fischer         rflags |= XATTR_REPLACE;
3939aca6897fSKeno Fischer     }
3940aca6897fSKeno Fischer 
39413b79ef2cSGreg Kurz     if (size > XATTR_SIZE_MAX) {
39423b79ef2cSGreg Kurz         err = -E2BIG;
39433b79ef2cSGreg Kurz         goto out_nofid;
39443b79ef2cSGreg Kurz     }
39453b79ef2cSGreg Kurz 
394660ce86c7SWei Liu     file_fidp = get_fid(pdu, fid);
394760ce86c7SWei Liu     if (file_fidp == NULL) {
394860ce86c7SWei Liu         err = -EINVAL;
394960ce86c7SWei Liu         goto out_nofid;
395060ce86c7SWei Liu     }
3951dd654e03SGreg Kurz     if (file_fidp->fid_type != P9_FID_NONE) {
3952dd654e03SGreg Kurz         err = -EINVAL;
3953dd654e03SGreg Kurz         goto out_put_fid;
3954dd654e03SGreg Kurz     }
3955dd654e03SGreg Kurz 
395660ce86c7SWei Liu     /* Make the file fid point to xattr */
395760ce86c7SWei Liu     xattr_fidp = file_fidp;
395860ce86c7SWei Liu     xattr_fidp->fid_type = P9_FID_XATTR;
395960ce86c7SWei Liu     xattr_fidp->fs.xattr.copied_len = 0;
3960dd28fbbcSLi Qiang     xattr_fidp->fs.xattr.xattrwalk_fid = false;
396160ce86c7SWei Liu     xattr_fidp->fs.xattr.len = size;
3962aca6897fSKeno Fischer     xattr_fidp->fs.xattr.flags = rflags;
396360ce86c7SWei Liu     v9fs_string_init(&xattr_fidp->fs.xattr.name);
396460ce86c7SWei Liu     v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
3965eb687602SLi Qiang     xattr_fidp->fs.xattr.value = g_malloc0(size);
396660ce86c7SWei Liu     err = offset;
3967dd654e03SGreg Kurz out_put_fid:
396860ce86c7SWei Liu     put_fid(pdu, file_fidp);
396960ce86c7SWei Liu out_nofid:
397060ce86c7SWei Liu     pdu_complete(pdu, err);
397160ce86c7SWei Liu     v9fs_string_free(&name);
397260ce86c7SWei Liu }
397360ce86c7SWei Liu 
39748440e22eSGreg Kurz static void coroutine_fn v9fs_readlink(void *opaque)
397560ce86c7SWei Liu {
397660ce86c7SWei Liu     V9fsPDU *pdu = opaque;
397760ce86c7SWei Liu     size_t offset = 7;
397860ce86c7SWei Liu     V9fsString target;
397960ce86c7SWei Liu     int32_t fid;
398060ce86c7SWei Liu     int err = 0;
398160ce86c7SWei Liu     V9fsFidState *fidp;
398260ce86c7SWei Liu 
398360ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "d", &fid);
398460ce86c7SWei Liu     if (err < 0) {
398560ce86c7SWei Liu         goto out_nofid;
398660ce86c7SWei Liu     }
398760ce86c7SWei Liu     trace_v9fs_readlink(pdu->tag, pdu->id, fid);
398860ce86c7SWei Liu     fidp = get_fid(pdu, fid);
398960ce86c7SWei Liu     if (fidp == NULL) {
399060ce86c7SWei Liu         err = -ENOENT;
399160ce86c7SWei Liu         goto out_nofid;
399260ce86c7SWei Liu     }
399360ce86c7SWei Liu 
399460ce86c7SWei Liu     v9fs_string_init(&target);
399560ce86c7SWei Liu     err = v9fs_co_readlink(pdu, &fidp->path, &target);
399660ce86c7SWei Liu     if (err < 0) {
399760ce86c7SWei Liu         goto out;
399860ce86c7SWei Liu     }
399960ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "s", &target);
400060ce86c7SWei Liu     if (err < 0) {
400160ce86c7SWei Liu         v9fs_string_free(&target);
400260ce86c7SWei Liu         goto out;
400360ce86c7SWei Liu     }
400460ce86c7SWei Liu     err += offset;
400560ce86c7SWei Liu     trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data);
400660ce86c7SWei Liu     v9fs_string_free(&target);
400760ce86c7SWei Liu out:
400860ce86c7SWei Liu     put_fid(pdu, fidp);
400960ce86c7SWei Liu out_nofid:
401060ce86c7SWei Liu     pdu_complete(pdu, err);
401160ce86c7SWei Liu }
401260ce86c7SWei Liu 
401360ce86c7SWei Liu static CoroutineEntry *pdu_co_handlers[] = {
401460ce86c7SWei Liu     [P9_TREADDIR] = v9fs_readdir,
401560ce86c7SWei Liu     [P9_TSTATFS] = v9fs_statfs,
401660ce86c7SWei Liu     [P9_TGETATTR] = v9fs_getattr,
401760ce86c7SWei Liu     [P9_TSETATTR] = v9fs_setattr,
401860ce86c7SWei Liu     [P9_TXATTRWALK] = v9fs_xattrwalk,
401960ce86c7SWei Liu     [P9_TXATTRCREATE] = v9fs_xattrcreate,
402060ce86c7SWei Liu     [P9_TMKNOD] = v9fs_mknod,
402160ce86c7SWei Liu     [P9_TRENAME] = v9fs_rename,
402260ce86c7SWei Liu     [P9_TLOCK] = v9fs_lock,
402360ce86c7SWei Liu     [P9_TGETLOCK] = v9fs_getlock,
402460ce86c7SWei Liu     [P9_TRENAMEAT] = v9fs_renameat,
402560ce86c7SWei Liu     [P9_TREADLINK] = v9fs_readlink,
402660ce86c7SWei Liu     [P9_TUNLINKAT] = v9fs_unlinkat,
402760ce86c7SWei Liu     [P9_TMKDIR] = v9fs_mkdir,
402860ce86c7SWei Liu     [P9_TVERSION] = v9fs_version,
402960ce86c7SWei Liu     [P9_TLOPEN] = v9fs_open,
403060ce86c7SWei Liu     [P9_TATTACH] = v9fs_attach,
403160ce86c7SWei Liu     [P9_TSTAT] = v9fs_stat,
403260ce86c7SWei Liu     [P9_TWALK] = v9fs_walk,
403360ce86c7SWei Liu     [P9_TCLUNK] = v9fs_clunk,
403460ce86c7SWei Liu     [P9_TFSYNC] = v9fs_fsync,
403560ce86c7SWei Liu     [P9_TOPEN] = v9fs_open,
403660ce86c7SWei Liu     [P9_TREAD] = v9fs_read,
403760ce86c7SWei Liu #if 0
403860ce86c7SWei Liu     [P9_TAUTH] = v9fs_auth,
403960ce86c7SWei Liu #endif
404060ce86c7SWei Liu     [P9_TFLUSH] = v9fs_flush,
404160ce86c7SWei Liu     [P9_TLINK] = v9fs_link,
404260ce86c7SWei Liu     [P9_TSYMLINK] = v9fs_symlink,
404360ce86c7SWei Liu     [P9_TCREATE] = v9fs_create,
404460ce86c7SWei Liu     [P9_TLCREATE] = v9fs_lcreate,
404560ce86c7SWei Liu     [P9_TWRITE] = v9fs_write,
404660ce86c7SWei Liu     [P9_TWSTAT] = v9fs_wstat,
404760ce86c7SWei Liu     [P9_TREMOVE] = v9fs_remove,
404860ce86c7SWei Liu };
404960ce86c7SWei Liu 
40508440e22eSGreg Kurz static void coroutine_fn v9fs_op_not_supp(void *opaque)
405160ce86c7SWei Liu {
405260ce86c7SWei Liu     V9fsPDU *pdu = opaque;
405360ce86c7SWei Liu     pdu_complete(pdu, -EOPNOTSUPP);
405460ce86c7SWei Liu }
405560ce86c7SWei Liu 
40568440e22eSGreg Kurz static void coroutine_fn v9fs_fs_ro(void *opaque)
405760ce86c7SWei Liu {
405860ce86c7SWei Liu     V9fsPDU *pdu = opaque;
405960ce86c7SWei Liu     pdu_complete(pdu, -EROFS);
406060ce86c7SWei Liu }
406160ce86c7SWei Liu 
406260ce86c7SWei Liu static inline bool is_read_only_op(V9fsPDU *pdu)
406360ce86c7SWei Liu {
406460ce86c7SWei Liu     switch (pdu->id) {
406560ce86c7SWei Liu     case P9_TREADDIR:
406660ce86c7SWei Liu     case P9_TSTATFS:
406760ce86c7SWei Liu     case P9_TGETATTR:
406860ce86c7SWei Liu     case P9_TXATTRWALK:
406960ce86c7SWei Liu     case P9_TLOCK:
407060ce86c7SWei Liu     case P9_TGETLOCK:
407160ce86c7SWei Liu     case P9_TREADLINK:
407260ce86c7SWei Liu     case P9_TVERSION:
407360ce86c7SWei Liu     case P9_TLOPEN:
407460ce86c7SWei Liu     case P9_TATTACH:
407560ce86c7SWei Liu     case P9_TSTAT:
407660ce86c7SWei Liu     case P9_TWALK:
407760ce86c7SWei Liu     case P9_TCLUNK:
407860ce86c7SWei Liu     case P9_TFSYNC:
407960ce86c7SWei Liu     case P9_TOPEN:
408060ce86c7SWei Liu     case P9_TREAD:
408160ce86c7SWei Liu     case P9_TAUTH:
408260ce86c7SWei Liu     case P9_TFLUSH:
408360ce86c7SWei Liu         return 1;
408460ce86c7SWei Liu     default:
408560ce86c7SWei Liu         return 0;
408660ce86c7SWei Liu     }
408760ce86c7SWei Liu }
408860ce86c7SWei Liu 
4089506f3275SGreg Kurz void pdu_submit(V9fsPDU *pdu, P9MsgHeader *hdr)
409060ce86c7SWei Liu {
409160ce86c7SWei Liu     Coroutine *co;
409260ce86c7SWei Liu     CoroutineEntry *handler;
409360ce86c7SWei Liu     V9fsState *s = pdu->s;
409460ce86c7SWei Liu 
4095506f3275SGreg Kurz     pdu->size = le32_to_cpu(hdr->size_le);
4096506f3275SGreg Kurz     pdu->id = hdr->id;
4097506f3275SGreg Kurz     pdu->tag = le16_to_cpu(hdr->tag_le);
4098506f3275SGreg Kurz 
409960ce86c7SWei Liu     if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) ||
410060ce86c7SWei Liu         (pdu_co_handlers[pdu->id] == NULL)) {
410160ce86c7SWei Liu         handler = v9fs_op_not_supp;
4102d1471233SGreg Kurz     } else if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) {
4103d1471233SGreg Kurz         handler = v9fs_fs_ro;
410460ce86c7SWei Liu     } else {
410560ce86c7SWei Liu         handler = pdu_co_handlers[pdu->id];
410660ce86c7SWei Liu     }
410760ce86c7SWei Liu 
4108506f3275SGreg Kurz     qemu_co_queue_init(&pdu->complete);
41090b8b8753SPaolo Bonzini     co = qemu_coroutine_create(handler, pdu);
41100b8b8753SPaolo Bonzini     qemu_coroutine_enter(co);
411160ce86c7SWei Liu }
411260ce86c7SWei Liu 
41132a0c56aaSWei Liu /* Returns 0 on success, 1 on failure. */
4114066eb006SGreg Kurz int v9fs_device_realize_common(V9fsState *s, const V9fsTransport *t,
4115066eb006SGreg Kurz                                Error **errp)
41162a0c56aaSWei Liu {
411792c45122SVladimir Sementsov-Ogievskiy     ERRP_GUARD();
41182a0c56aaSWei Liu     int i, len;
41192a0c56aaSWei Liu     struct stat stat;
41202a0c56aaSWei Liu     FsDriverEntry *fse;
41212a0c56aaSWei Liu     V9fsPath path;
41222a0c56aaSWei Liu     int rc = 1;
41232a0c56aaSWei Liu 
4124066eb006SGreg Kurz     assert(!s->transport);
4125066eb006SGreg Kurz     s->transport = t;
4126066eb006SGreg Kurz 
41272a0c56aaSWei Liu     /* initialize pdu allocator */
41282a0c56aaSWei Liu     QLIST_INIT(&s->free_list);
41292a0c56aaSWei Liu     QLIST_INIT(&s->active_list);
41300d78289cSGreg Kurz     for (i = 0; i < MAX_REQ; i++) {
4131583f21f8SStefano Stabellini         QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
4132583f21f8SStefano Stabellini         s->pdus[i].s = s;
4133583f21f8SStefano Stabellini         s->pdus[i].idx = i;
41342a0c56aaSWei Liu     }
41352a0c56aaSWei Liu 
41362a0c56aaSWei Liu     v9fs_path_init(&path);
41372a0c56aaSWei Liu 
41382a0c56aaSWei Liu     fse = get_fsdev_fsentry(s->fsconf.fsdev_id);
41392a0c56aaSWei Liu 
41402a0c56aaSWei Liu     if (!fse) {
41412a0c56aaSWei Liu         /* We don't have a fsdev identified by fsdev_id */
41422a0c56aaSWei Liu         error_setg(errp, "9pfs device couldn't find fsdev with the "
41432a0c56aaSWei Liu                    "id = %s",
41442a0c56aaSWei Liu                    s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL");
41452a0c56aaSWei Liu         goto out;
41462a0c56aaSWei Liu     }
41472a0c56aaSWei Liu 
41482a0c56aaSWei Liu     if (!s->fsconf.tag) {
41492a0c56aaSWei Liu         /* we haven't specified a mount_tag */
41502a0c56aaSWei Liu         error_setg(errp, "fsdev with id %s needs mount_tag arguments",
41512a0c56aaSWei Liu                    s->fsconf.fsdev_id);
41522a0c56aaSWei Liu         goto out;
41532a0c56aaSWei Liu     }
41542a0c56aaSWei Liu 
41552a0c56aaSWei Liu     s->ctx.export_flags = fse->export_flags;
41562a0c56aaSWei Liu     s->ctx.fs_root = g_strdup(fse->path);
41572a0c56aaSWei Liu     s->ctx.exops.get_st_gen = NULL;
41582a0c56aaSWei Liu     len = strlen(s->fsconf.tag);
41592a0c56aaSWei Liu     if (len > MAX_TAG_LEN - 1) {
41602a0c56aaSWei Liu         error_setg(errp, "mount tag '%s' (%d bytes) is longer than "
41612a0c56aaSWei Liu                    "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1);
41622a0c56aaSWei Liu         goto out;
41632a0c56aaSWei Liu     }
41642a0c56aaSWei Liu 
41652a0c56aaSWei Liu     s->tag = g_strdup(s->fsconf.tag);
41662a0c56aaSWei Liu     s->ctx.uid = -1;
41672a0c56aaSWei Liu 
41682a0c56aaSWei Liu     s->ops = fse->ops;
41692a0c56aaSWei Liu 
4170b96feb2cSTobias Schramm     s->ctx.fmode = fse->fmode;
4171b96feb2cSTobias Schramm     s->ctx.dmode = fse->dmode;
4172b96feb2cSTobias Schramm 
4173feabd6cfSGreg Kurz     QSIMPLEQ_INIT(&s->fid_list);
41742a0c56aaSWei Liu     qemu_co_rwlock_init(&s->rename_lock);
41752a0c56aaSWei Liu 
417665603a80SGreg Kurz     if (s->ops->init(&s->ctx, errp) < 0) {
417765603a80SGreg Kurz         error_prepend(errp, "cannot initialize fsdev '%s': ",
417865603a80SGreg Kurz                       s->fsconf.fsdev_id);
41792a0c56aaSWei Liu         goto out;
41802a0c56aaSWei Liu     }
41812a0c56aaSWei Liu 
41822a0c56aaSWei Liu     /*
41832a0c56aaSWei Liu      * Check details of export path, We need to use fs driver
41842a0c56aaSWei Liu      * call back to do that. Since we are in the init path, we don't
41852a0c56aaSWei Liu      * use co-routines here.
41862a0c56aaSWei Liu      */
41872a0c56aaSWei Liu     if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) {
41882a0c56aaSWei Liu         error_setg(errp,
41892a0c56aaSWei Liu                    "error in converting name to path %s", strerror(errno));
41902a0c56aaSWei Liu         goto out;
41912a0c56aaSWei Liu     }
41922a0c56aaSWei Liu     if (s->ops->lstat(&s->ctx, &path, &stat)) {
41932a0c56aaSWei Liu         error_setg(errp, "share path %s does not exist", fse->path);
41942a0c56aaSWei Liu         goto out;
41952a0c56aaSWei Liu     } else if (!S_ISDIR(stat.st_mode)) {
41962a0c56aaSWei Liu         error_setg(errp, "share path %s is not a directory", fse->path);
41972a0c56aaSWei Liu         goto out;
41982a0c56aaSWei Liu     }
4199b8bbdb88SPradeep Jagadeesh 
42003b5ee9e8SAntonios Motakis     s->dev_id = stat.st_dev;
42013b5ee9e8SAntonios Motakis 
42026b6aa828SChristian Schoenebeck     /* init inode remapping : */
42036b6aa828SChristian Schoenebeck     /* hash table for variable length inode suffixes */
42046b6aa828SChristian Schoenebeck     qpd_table_init(&s->qpd_table);
42056b6aa828SChristian Schoenebeck     /* hash table for slow/full inode remapping (most users won't need it) */
42066b6aa828SChristian Schoenebeck     qpf_table_init(&s->qpf_table);
42076b6aa828SChristian Schoenebeck     /* hash table for quick inode remapping */
42081a6ed33cSAntonios Motakis     qpp_table_init(&s->qpp_table);
42096b6aa828SChristian Schoenebeck     s->qp_ndevices = 0;
42106b6aa828SChristian Schoenebeck     s->qp_affix_next = 1; /* reserve 0 to detect overflow */
4211f3fe4a2dSAntonios Motakis     s->qp_fullpath_next = 1;
42121a6ed33cSAntonios Motakis 
4213b8bbdb88SPradeep Jagadeesh     s->ctx.fst = &fse->fst;
4214b8bbdb88SPradeep Jagadeesh     fsdev_throttle_init(s->ctx.fst);
4215b8bbdb88SPradeep Jagadeesh 
42162a0c56aaSWei Liu     rc = 0;
42172a0c56aaSWei Liu out:
42182a0c56aaSWei Liu     if (rc) {
4219b69c3c21SMarkus Armbruster         v9fs_device_unrealize_common(s);
4220702dbcc2SLi Qiang     }
42212a0c56aaSWei Liu     v9fs_path_free(&path);
42222a0c56aaSWei Liu     return rc;
42232a0c56aaSWei Liu }
42242a0c56aaSWei Liu 
4225b69c3c21SMarkus Armbruster void v9fs_device_unrealize_common(V9fsState *s)
42262a0c56aaSWei Liu {
4227c0da0cb7SGreg Kurz     if (s->ops && s->ops->cleanup) {
4228702dbcc2SLi Qiang         s->ops->cleanup(&s->ctx);
4229702dbcc2SLi Qiang     }
4230c0da0cb7SGreg Kurz     if (s->ctx.fst) {
4231b8bbdb88SPradeep Jagadeesh         fsdev_throttle_cleanup(s->ctx.fst);
4232c0da0cb7SGreg Kurz     }
42332a0c56aaSWei Liu     g_free(s->tag);
42346b6aa828SChristian Schoenebeck     qp_table_destroy(&s->qpd_table);
4235f3fe4a2dSAntonios Motakis     qp_table_destroy(&s->qpp_table);
4236f3fe4a2dSAntonios Motakis     qp_table_destroy(&s->qpf_table);
42374774718eSLi Qiang     g_free(s->ctx.fs_root);
42382a0c56aaSWei Liu }
42392a0c56aaSWei Liu 
42400e44a0fdSGreg Kurz typedef struct VirtfsCoResetData {
42410e44a0fdSGreg Kurz     V9fsPDU pdu;
42420e44a0fdSGreg Kurz     bool done;
42430e44a0fdSGreg Kurz } VirtfsCoResetData;
42440e44a0fdSGreg Kurz 
42450e44a0fdSGreg Kurz static void coroutine_fn virtfs_co_reset(void *opaque)
42460e44a0fdSGreg Kurz {
42470e44a0fdSGreg Kurz     VirtfsCoResetData *data = opaque;
42480e44a0fdSGreg Kurz 
42490e44a0fdSGreg Kurz     virtfs_reset(&data->pdu);
42500e44a0fdSGreg Kurz     data->done = true;
42510e44a0fdSGreg Kurz }
42520e44a0fdSGreg Kurz 
42530e44a0fdSGreg Kurz void v9fs_reset(V9fsState *s)
42540e44a0fdSGreg Kurz {
42550e44a0fdSGreg Kurz     VirtfsCoResetData data = { .pdu = { .s = s }, .done = false };
42560e44a0fdSGreg Kurz     Coroutine *co;
42570e44a0fdSGreg Kurz 
42580e44a0fdSGreg Kurz     while (!QLIST_EMPTY(&s->active_list)) {
42590e44a0fdSGreg Kurz         aio_poll(qemu_get_aio_context(), true);
42600e44a0fdSGreg Kurz     }
42610e44a0fdSGreg Kurz 
42620e44a0fdSGreg Kurz     co = qemu_coroutine_create(virtfs_co_reset, &data);
42630e44a0fdSGreg Kurz     qemu_coroutine_enter(co);
42640e44a0fdSGreg Kurz 
42650e44a0fdSGreg Kurz     while (!data.done) {
42660e44a0fdSGreg Kurz         aio_poll(qemu_get_aio_context(), true);
42670e44a0fdSGreg Kurz     }
42680e44a0fdSGreg Kurz }
42690e44a0fdSGreg Kurz 
427060ce86c7SWei Liu static void __attribute__((__constructor__)) v9fs_set_fd_limit(void)
427160ce86c7SWei Liu {
427260ce86c7SWei Liu     struct rlimit rlim;
427360ce86c7SWei Liu     if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
427463325b18SGreg Kurz         error_report("Failed to get the resource limit");
427560ce86c7SWei Liu         exit(1);
427660ce86c7SWei Liu     }
427760ce86c7SWei Liu     open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur / 3);
427860ce86c7SWei Liu     open_fd_rc = rlim.rlim_cur / 2;
427960ce86c7SWei Liu }
4280