xref: /openbmc/qemu/hw/9pfs/9p.c (revision 29c9d2ca)
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 
14fbc04127SPeter Maydell #include "qemu/osdep.h"
15e3e83f2eSGreg Kurz #include <glib/gprintf.h>
1660ce86c7SWei Liu #include "hw/virtio/virtio.h"
17da34e65cSMarkus Armbruster #include "qapi/error.h"
1860ce86c7SWei Liu #include "qemu/error-report.h"
1960ce86c7SWei Liu #include "qemu/iov.h"
20db725815SMarkus Armbruster #include "qemu/main-loop.h"
2160ce86c7SWei Liu #include "qemu/sockets.h"
2260ce86c7SWei Liu #include "virtio-9p.h"
2360ce86c7SWei Liu #include "fsdev/qemu-fsdev.h"
2460ce86c7SWei Liu #include "9p-xattr.h"
2560ce86c7SWei Liu #include "coth.h"
2660ce86c7SWei Liu #include "trace.h"
27795c40b8SJuan Quintela #include "migration/blocker.h"
28357e2f7fSGreg Kurz #include "sysemu/qtest.h"
291a6ed33cSAntonios Motakis #include "qemu/xxhash.h"
306b6aa828SChristian Schoenebeck #include <math.h>
3103556ea9SDan Robertson #include <linux/limits.h>
3260ce86c7SWei Liu 
3360ce86c7SWei Liu int open_fd_hw;
3460ce86c7SWei Liu int total_open_fd;
3560ce86c7SWei Liu static int open_fd_rc;
3660ce86c7SWei Liu 
3760ce86c7SWei Liu enum {
3860ce86c7SWei Liu     Oread   = 0x00,
3960ce86c7SWei Liu     Owrite  = 0x01,
4060ce86c7SWei Liu     Ordwr   = 0x02,
4160ce86c7SWei Liu     Oexec   = 0x03,
4260ce86c7SWei Liu     Oexcl   = 0x04,
4360ce86c7SWei Liu     Otrunc  = 0x10,
4460ce86c7SWei Liu     Orexec  = 0x20,
4560ce86c7SWei Liu     Orclose = 0x40,
4660ce86c7SWei Liu     Oappend = 0x80,
4760ce86c7SWei Liu };
4860ce86c7SWei Liu 
4975673590SGreg Kurz static ssize_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
5060ce86c7SWei Liu {
5160ce86c7SWei Liu     ssize_t ret;
5260ce86c7SWei Liu     va_list ap;
5360ce86c7SWei Liu 
5460ce86c7SWei Liu     va_start(ap, fmt);
55ea83441cSStefano Stabellini     ret = pdu->s->transport->pdu_vmarshal(pdu, offset, fmt, ap);
5660ce86c7SWei Liu     va_end(ap);
5760ce86c7SWei Liu 
5860ce86c7SWei Liu     return ret;
5960ce86c7SWei Liu }
6060ce86c7SWei Liu 
6175673590SGreg Kurz static ssize_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
6260ce86c7SWei Liu {
6360ce86c7SWei Liu     ssize_t ret;
6460ce86c7SWei Liu     va_list ap;
6560ce86c7SWei Liu 
6660ce86c7SWei Liu     va_start(ap, fmt);
67ea83441cSStefano Stabellini     ret = pdu->s->transport->pdu_vunmarshal(pdu, offset, fmt, ap);
6860ce86c7SWei Liu     va_end(ap);
6960ce86c7SWei Liu 
7060ce86c7SWei Liu     return ret;
7160ce86c7SWei Liu }
7260ce86c7SWei Liu 
7360ce86c7SWei Liu static int omode_to_uflags(int8_t mode)
7460ce86c7SWei Liu {
7560ce86c7SWei Liu     int ret = 0;
7660ce86c7SWei Liu 
7760ce86c7SWei Liu     switch (mode & 3) {
7860ce86c7SWei Liu     case Oread:
7960ce86c7SWei Liu         ret = O_RDONLY;
8060ce86c7SWei Liu         break;
8160ce86c7SWei Liu     case Ordwr:
8260ce86c7SWei Liu         ret = O_RDWR;
8360ce86c7SWei Liu         break;
8460ce86c7SWei Liu     case Owrite:
8560ce86c7SWei Liu         ret = O_WRONLY;
8660ce86c7SWei Liu         break;
8760ce86c7SWei Liu     case Oexec:
8860ce86c7SWei Liu         ret = O_RDONLY;
8960ce86c7SWei Liu         break;
9060ce86c7SWei Liu     }
9160ce86c7SWei Liu 
9260ce86c7SWei Liu     if (mode & Otrunc) {
9360ce86c7SWei Liu         ret |= O_TRUNC;
9460ce86c7SWei Liu     }
9560ce86c7SWei Liu 
9660ce86c7SWei Liu     if (mode & Oappend) {
9760ce86c7SWei Liu         ret |= O_APPEND;
9860ce86c7SWei Liu     }
9960ce86c7SWei Liu 
10060ce86c7SWei Liu     if (mode & Oexcl) {
10160ce86c7SWei Liu         ret |= O_EXCL;
10260ce86c7SWei Liu     }
10360ce86c7SWei Liu 
10460ce86c7SWei Liu     return ret;
10560ce86c7SWei Liu }
10660ce86c7SWei Liu 
1078e71b96cSGreg Kurz typedef struct DotlOpenflagMap {
10860ce86c7SWei Liu     int dotl_flag;
10960ce86c7SWei Liu     int open_flag;
1108e71b96cSGreg Kurz } DotlOpenflagMap;
11160ce86c7SWei Liu 
11260ce86c7SWei Liu static int dotl_to_open_flags(int flags)
11360ce86c7SWei Liu {
11460ce86c7SWei Liu     int i;
11560ce86c7SWei Liu     /*
11660ce86c7SWei Liu      * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
11760ce86c7SWei Liu      * and P9_DOTL_NOACCESS
11860ce86c7SWei Liu      */
11960ce86c7SWei Liu     int oflags = flags & O_ACCMODE;
12060ce86c7SWei Liu 
1218e71b96cSGreg Kurz     DotlOpenflagMap dotl_oflag_map[] = {
12260ce86c7SWei Liu         { P9_DOTL_CREATE, O_CREAT },
12360ce86c7SWei Liu         { P9_DOTL_EXCL, O_EXCL },
12460ce86c7SWei Liu         { P9_DOTL_NOCTTY , O_NOCTTY },
12560ce86c7SWei Liu         { P9_DOTL_TRUNC, O_TRUNC },
12660ce86c7SWei Liu         { P9_DOTL_APPEND, O_APPEND },
12760ce86c7SWei Liu         { P9_DOTL_NONBLOCK, O_NONBLOCK } ,
12860ce86c7SWei Liu         { P9_DOTL_DSYNC, O_DSYNC },
12960ce86c7SWei Liu         { P9_DOTL_FASYNC, FASYNC },
13060ce86c7SWei Liu         { P9_DOTL_DIRECT, O_DIRECT },
13160ce86c7SWei Liu         { P9_DOTL_LARGEFILE, O_LARGEFILE },
13260ce86c7SWei Liu         { P9_DOTL_DIRECTORY, O_DIRECTORY },
13360ce86c7SWei Liu         { P9_DOTL_NOFOLLOW, O_NOFOLLOW },
13460ce86c7SWei Liu         { P9_DOTL_NOATIME, O_NOATIME },
13560ce86c7SWei Liu         { P9_DOTL_SYNC, O_SYNC },
13660ce86c7SWei Liu     };
13760ce86c7SWei Liu 
13860ce86c7SWei Liu     for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) {
13960ce86c7SWei Liu         if (flags & dotl_oflag_map[i].dotl_flag) {
14060ce86c7SWei Liu             oflags |= dotl_oflag_map[i].open_flag;
14160ce86c7SWei Liu         }
14260ce86c7SWei Liu     }
14360ce86c7SWei Liu 
14460ce86c7SWei Liu     return oflags;
14560ce86c7SWei Liu }
14660ce86c7SWei Liu 
14760ce86c7SWei Liu void cred_init(FsCred *credp)
14860ce86c7SWei Liu {
14960ce86c7SWei Liu     credp->fc_uid = -1;
15060ce86c7SWei Liu     credp->fc_gid = -1;
15160ce86c7SWei Liu     credp->fc_mode = -1;
15260ce86c7SWei Liu     credp->fc_rdev = -1;
15360ce86c7SWei Liu }
15460ce86c7SWei Liu 
15560ce86c7SWei Liu static int get_dotl_openflags(V9fsState *s, int oflags)
15660ce86c7SWei Liu {
15760ce86c7SWei Liu     int flags;
15860ce86c7SWei Liu     /*
15960ce86c7SWei Liu      * Filter the client open flags
16060ce86c7SWei Liu      */
16160ce86c7SWei Liu     flags = dotl_to_open_flags(oflags);
16260ce86c7SWei Liu     flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT);
16360ce86c7SWei Liu     /*
16460ce86c7SWei Liu      * Ignore direct disk access hint until the server supports it.
16560ce86c7SWei Liu      */
16660ce86c7SWei Liu     flags &= ~O_DIRECT;
16760ce86c7SWei Liu     return flags;
16860ce86c7SWei Liu }
16960ce86c7SWei Liu 
17060ce86c7SWei Liu void v9fs_path_init(V9fsPath *path)
17160ce86c7SWei Liu {
17260ce86c7SWei Liu     path->data = NULL;
17360ce86c7SWei Liu     path->size = 0;
17460ce86c7SWei Liu }
17560ce86c7SWei Liu 
17660ce86c7SWei Liu void v9fs_path_free(V9fsPath *path)
17760ce86c7SWei Liu {
17860ce86c7SWei Liu     g_free(path->data);
17960ce86c7SWei Liu     path->data = NULL;
18060ce86c7SWei Liu     path->size = 0;
18160ce86c7SWei Liu }
18260ce86c7SWei Liu 
183e3e83f2eSGreg Kurz 
184e3e83f2eSGreg Kurz void GCC_FMT_ATTR(2, 3)
185e3e83f2eSGreg Kurz v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...)
186e3e83f2eSGreg Kurz {
187e3e83f2eSGreg Kurz     va_list ap;
188e3e83f2eSGreg Kurz 
189e3e83f2eSGreg Kurz     v9fs_path_free(path);
190e3e83f2eSGreg Kurz 
191e3e83f2eSGreg Kurz     va_start(ap, fmt);
192e3e83f2eSGreg Kurz     /* Bump the size for including terminating NULL */
193e3e83f2eSGreg Kurz     path->size = g_vasprintf(&path->data, fmt, ap) + 1;
194e3e83f2eSGreg Kurz     va_end(ap);
195e3e83f2eSGreg Kurz }
196e3e83f2eSGreg Kurz 
197e446a1ebSMarc-André Lureau void v9fs_path_copy(V9fsPath *dst, const V9fsPath *src)
19860ce86c7SWei Liu {
199e446a1ebSMarc-André Lureau     v9fs_path_free(dst);
200e446a1ebSMarc-André Lureau     dst->size = src->size;
201e446a1ebSMarc-André Lureau     dst->data = g_memdup(src->data, src->size);
20260ce86c7SWei Liu }
20360ce86c7SWei Liu 
20460ce86c7SWei Liu int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
20560ce86c7SWei Liu                       const char *name, V9fsPath *path)
20660ce86c7SWei Liu {
20760ce86c7SWei Liu     int err;
20860ce86c7SWei Liu     err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
20960ce86c7SWei Liu     if (err < 0) {
21060ce86c7SWei Liu         err = -errno;
21160ce86c7SWei Liu     }
21260ce86c7SWei Liu     return err;
21360ce86c7SWei Liu }
21460ce86c7SWei Liu 
21560ce86c7SWei Liu /*
21660ce86c7SWei Liu  * Return TRUE if s1 is an ancestor of s2.
21760ce86c7SWei Liu  *
21860ce86c7SWei Liu  * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
21960ce86c7SWei Liu  * As a special case, We treat s1 as ancestor of s2 if they are same!
22060ce86c7SWei Liu  */
22160ce86c7SWei Liu static int v9fs_path_is_ancestor(V9fsPath *s1, V9fsPath *s2)
22260ce86c7SWei Liu {
22360ce86c7SWei Liu     if (!strncmp(s1->data, s2->data, s1->size - 1)) {
22460ce86c7SWei Liu         if (s2->data[s1->size - 1] == '\0' || s2->data[s1->size - 1] == '/') {
22560ce86c7SWei Liu             return 1;
22660ce86c7SWei Liu         }
22760ce86c7SWei Liu     }
22860ce86c7SWei Liu     return 0;
22960ce86c7SWei Liu }
23060ce86c7SWei Liu 
23160ce86c7SWei Liu static size_t v9fs_string_size(V9fsString *str)
23260ce86c7SWei Liu {
23360ce86c7SWei Liu     return str->size;
23460ce86c7SWei Liu }
23560ce86c7SWei Liu 
23660ce86c7SWei Liu /*
23760ce86c7SWei Liu  * returns 0 if fid got re-opened, 1 if not, < 0 on error */
2388440e22eSGreg Kurz static int coroutine_fn v9fs_reopen_fid(V9fsPDU *pdu, V9fsFidState *f)
23960ce86c7SWei Liu {
24060ce86c7SWei Liu     int err = 1;
24160ce86c7SWei Liu     if (f->fid_type == P9_FID_FILE) {
24260ce86c7SWei Liu         if (f->fs.fd == -1) {
24360ce86c7SWei Liu             do {
24460ce86c7SWei Liu                 err = v9fs_co_open(pdu, f, f->open_flags);
24560ce86c7SWei Liu             } while (err == -EINTR && !pdu->cancelled);
24660ce86c7SWei Liu         }
24760ce86c7SWei Liu     } else if (f->fid_type == P9_FID_DIR) {
248f314ea4eSGreg Kurz         if (f->fs.dir.stream == NULL) {
24960ce86c7SWei Liu             do {
25060ce86c7SWei Liu                 err = v9fs_co_opendir(pdu, f);
25160ce86c7SWei Liu             } while (err == -EINTR && !pdu->cancelled);
25260ce86c7SWei Liu         }
25360ce86c7SWei Liu     }
25460ce86c7SWei Liu     return err;
25560ce86c7SWei Liu }
25660ce86c7SWei Liu 
2578440e22eSGreg Kurz static V9fsFidState *coroutine_fn get_fid(V9fsPDU *pdu, int32_t fid)
25860ce86c7SWei Liu {
25960ce86c7SWei Liu     int err;
26060ce86c7SWei Liu     V9fsFidState *f;
26160ce86c7SWei Liu     V9fsState *s = pdu->s;
26260ce86c7SWei Liu 
26360ce86c7SWei Liu     for (f = s->fid_list; f; f = f->next) {
26460ce86c7SWei Liu         BUG_ON(f->clunked);
26560ce86c7SWei Liu         if (f->fid == fid) {
26660ce86c7SWei Liu             /*
26760ce86c7SWei Liu              * Update the fid ref upfront so that
26860ce86c7SWei Liu              * we don't get reclaimed when we yield
26960ce86c7SWei Liu              * in open later.
27060ce86c7SWei Liu              */
27160ce86c7SWei Liu             f->ref++;
27260ce86c7SWei Liu             /*
27360ce86c7SWei Liu              * check whether we need to reopen the
27460ce86c7SWei Liu              * file. We might have closed the fd
27560ce86c7SWei Liu              * while trying to free up some file
27660ce86c7SWei Liu              * descriptors.
27760ce86c7SWei Liu              */
27860ce86c7SWei Liu             err = v9fs_reopen_fid(pdu, f);
27960ce86c7SWei Liu             if (err < 0) {
28060ce86c7SWei Liu                 f->ref--;
28160ce86c7SWei Liu                 return NULL;
28260ce86c7SWei Liu             }
28360ce86c7SWei Liu             /*
28460ce86c7SWei Liu              * Mark the fid as referenced so that the LRU
28560ce86c7SWei Liu              * reclaim won't close the file descriptor
28660ce86c7SWei Liu              */
28760ce86c7SWei Liu             f->flags |= FID_REFERENCED;
28860ce86c7SWei Liu             return f;
28960ce86c7SWei Liu         }
29060ce86c7SWei Liu     }
29160ce86c7SWei Liu     return NULL;
29260ce86c7SWei Liu }
29360ce86c7SWei Liu 
29460ce86c7SWei Liu static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
29560ce86c7SWei Liu {
29660ce86c7SWei Liu     V9fsFidState *f;
29760ce86c7SWei Liu 
29860ce86c7SWei Liu     for (f = s->fid_list; f; f = f->next) {
29960ce86c7SWei Liu         /* If fid is already there return NULL */
30060ce86c7SWei Liu         BUG_ON(f->clunked);
30160ce86c7SWei Liu         if (f->fid == fid) {
30260ce86c7SWei Liu             return NULL;
30360ce86c7SWei Liu         }
30460ce86c7SWei Liu     }
30560ce86c7SWei Liu     f = g_malloc0(sizeof(V9fsFidState));
30660ce86c7SWei Liu     f->fid = fid;
30760ce86c7SWei Liu     f->fid_type = P9_FID_NONE;
30860ce86c7SWei Liu     f->ref = 1;
30960ce86c7SWei Liu     /*
31060ce86c7SWei Liu      * Mark the fid as referenced so that the LRU
31160ce86c7SWei Liu      * reclaim won't close the file descriptor
31260ce86c7SWei Liu      */
31360ce86c7SWei Liu     f->flags |= FID_REFERENCED;
31460ce86c7SWei Liu     f->next = s->fid_list;
31560ce86c7SWei Liu     s->fid_list = f;
31660ce86c7SWei Liu 
3177cde47d4SGreg Kurz     v9fs_readdir_init(&f->fs.dir);
3187cde47d4SGreg Kurz     v9fs_readdir_init(&f->fs_reclaim.dir);
3197cde47d4SGreg Kurz 
32060ce86c7SWei Liu     return f;
32160ce86c7SWei Liu }
32260ce86c7SWei Liu 
3238440e22eSGreg Kurz static int coroutine_fn v9fs_xattr_fid_clunk(V9fsPDU *pdu, V9fsFidState *fidp)
32460ce86c7SWei Liu {
32560ce86c7SWei Liu     int retval = 0;
32660ce86c7SWei Liu 
327dd28fbbcSLi Qiang     if (fidp->fs.xattr.xattrwalk_fid) {
32860ce86c7SWei Liu         /* getxattr/listxattr fid */
32960ce86c7SWei Liu         goto free_value;
33060ce86c7SWei Liu     }
33160ce86c7SWei Liu     /*
33260ce86c7SWei Liu      * if this is fid for setxattr. clunk should
33360ce86c7SWei Liu      * result in setxattr localcall
33460ce86c7SWei Liu      */
33560ce86c7SWei Liu     if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) {
33660ce86c7SWei Liu         /* clunk after partial write */
33760ce86c7SWei Liu         retval = -EINVAL;
33860ce86c7SWei Liu         goto free_out;
33960ce86c7SWei Liu     }
34060ce86c7SWei Liu     if (fidp->fs.xattr.len) {
34160ce86c7SWei Liu         retval = v9fs_co_lsetxattr(pdu, &fidp->path, &fidp->fs.xattr.name,
34260ce86c7SWei Liu                                    fidp->fs.xattr.value,
34360ce86c7SWei Liu                                    fidp->fs.xattr.len,
34460ce86c7SWei Liu                                    fidp->fs.xattr.flags);
34560ce86c7SWei Liu     } else {
34660ce86c7SWei Liu         retval = v9fs_co_lremovexattr(pdu, &fidp->path, &fidp->fs.xattr.name);
34760ce86c7SWei Liu     }
34860ce86c7SWei Liu free_out:
34960ce86c7SWei Liu     v9fs_string_free(&fidp->fs.xattr.name);
35060ce86c7SWei Liu free_value:
35160ce86c7SWei Liu     g_free(fidp->fs.xattr.value);
35260ce86c7SWei Liu     return retval;
35360ce86c7SWei Liu }
35460ce86c7SWei Liu 
3558440e22eSGreg Kurz static int coroutine_fn free_fid(V9fsPDU *pdu, V9fsFidState *fidp)
35660ce86c7SWei Liu {
35760ce86c7SWei Liu     int retval = 0;
35860ce86c7SWei Liu 
35960ce86c7SWei Liu     if (fidp->fid_type == P9_FID_FILE) {
36060ce86c7SWei Liu         /* If we reclaimed the fd no need to close */
36160ce86c7SWei Liu         if (fidp->fs.fd != -1) {
36260ce86c7SWei Liu             retval = v9fs_co_close(pdu, &fidp->fs);
36360ce86c7SWei Liu         }
36460ce86c7SWei Liu     } else if (fidp->fid_type == P9_FID_DIR) {
365f314ea4eSGreg Kurz         if (fidp->fs.dir.stream != NULL) {
36660ce86c7SWei Liu             retval = v9fs_co_closedir(pdu, &fidp->fs);
36760ce86c7SWei Liu         }
36860ce86c7SWei Liu     } else if (fidp->fid_type == P9_FID_XATTR) {
36960ce86c7SWei Liu         retval = v9fs_xattr_fid_clunk(pdu, fidp);
37060ce86c7SWei Liu     }
37160ce86c7SWei Liu     v9fs_path_free(&fidp->path);
37260ce86c7SWei Liu     g_free(fidp);
37360ce86c7SWei Liu     return retval;
37460ce86c7SWei Liu }
37560ce86c7SWei Liu 
3768440e22eSGreg Kurz static int coroutine_fn put_fid(V9fsPDU *pdu, V9fsFidState *fidp)
37760ce86c7SWei Liu {
37860ce86c7SWei Liu     BUG_ON(!fidp->ref);
37960ce86c7SWei Liu     fidp->ref--;
38060ce86c7SWei Liu     /*
38160ce86c7SWei Liu      * Don't free the fid if it is in reclaim list
38260ce86c7SWei Liu      */
38360ce86c7SWei Liu     if (!fidp->ref && fidp->clunked) {
38460ce86c7SWei Liu         if (fidp->fid == pdu->s->root_fid) {
38560ce86c7SWei Liu             /*
38660ce86c7SWei Liu              * if the clunked fid is root fid then we
38760ce86c7SWei Liu              * have unmounted the fs on the client side.
38860ce86c7SWei Liu              * delete the migration blocker. Ideally, this
38960ce86c7SWei Liu              * should be hooked to transport close notification
39060ce86c7SWei Liu              */
39160ce86c7SWei Liu             if (pdu->s->migration_blocker) {
39260ce86c7SWei Liu                 migrate_del_blocker(pdu->s->migration_blocker);
39360ce86c7SWei Liu                 error_free(pdu->s->migration_blocker);
39460ce86c7SWei Liu                 pdu->s->migration_blocker = NULL;
39560ce86c7SWei Liu             }
39660ce86c7SWei Liu         }
39760ce86c7SWei Liu         return free_fid(pdu, fidp);
39860ce86c7SWei Liu     }
39960ce86c7SWei Liu     return 0;
40060ce86c7SWei Liu }
40160ce86c7SWei Liu 
40260ce86c7SWei Liu static V9fsFidState *clunk_fid(V9fsState *s, int32_t fid)
40360ce86c7SWei Liu {
40460ce86c7SWei Liu     V9fsFidState **fidpp, *fidp;
40560ce86c7SWei Liu 
40660ce86c7SWei Liu     for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
40760ce86c7SWei Liu         if ((*fidpp)->fid == fid) {
40860ce86c7SWei Liu             break;
40960ce86c7SWei Liu         }
41060ce86c7SWei Liu     }
41160ce86c7SWei Liu     if (*fidpp == NULL) {
41260ce86c7SWei Liu         return NULL;
41360ce86c7SWei Liu     }
41460ce86c7SWei Liu     fidp = *fidpp;
41560ce86c7SWei Liu     *fidpp = fidp->next;
41660ce86c7SWei Liu     fidp->clunked = 1;
41760ce86c7SWei Liu     return fidp;
41860ce86c7SWei Liu }
41960ce86c7SWei Liu 
4208440e22eSGreg Kurz void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu)
42160ce86c7SWei Liu {
42260ce86c7SWei Liu     int reclaim_count = 0;
42360ce86c7SWei Liu     V9fsState *s = pdu->s;
42460ce86c7SWei Liu     V9fsFidState *f, *reclaim_list = NULL;
42560ce86c7SWei Liu 
42660ce86c7SWei Liu     for (f = s->fid_list; f; f = f->next) {
42760ce86c7SWei Liu         /*
42860ce86c7SWei Liu          * Unlink fids cannot be reclaimed. Check
42960ce86c7SWei Liu          * for them and skip them. Also skip fids
43060ce86c7SWei Liu          * currently being operated on.
43160ce86c7SWei Liu          */
43260ce86c7SWei Liu         if (f->ref || f->flags & FID_NON_RECLAIMABLE) {
43360ce86c7SWei Liu             continue;
43460ce86c7SWei Liu         }
43560ce86c7SWei Liu         /*
43660ce86c7SWei Liu          * if it is a recently referenced fid
43760ce86c7SWei Liu          * we leave the fid untouched and clear the
43860ce86c7SWei Liu          * reference bit. We come back to it later
43960ce86c7SWei Liu          * in the next iteration. (a simple LRU without
44060ce86c7SWei Liu          * moving list elements around)
44160ce86c7SWei Liu          */
44260ce86c7SWei Liu         if (f->flags & FID_REFERENCED) {
44360ce86c7SWei Liu             f->flags &= ~FID_REFERENCED;
44460ce86c7SWei Liu             continue;
44560ce86c7SWei Liu         }
44660ce86c7SWei Liu         /*
44760ce86c7SWei Liu          * Add fids to reclaim list.
44860ce86c7SWei Liu          */
44960ce86c7SWei Liu         if (f->fid_type == P9_FID_FILE) {
45060ce86c7SWei Liu             if (f->fs.fd != -1) {
45160ce86c7SWei Liu                 /*
45260ce86c7SWei Liu                  * Up the reference count so that
45360ce86c7SWei Liu                  * a clunk request won't free this fid
45460ce86c7SWei Liu                  */
45560ce86c7SWei Liu                 f->ref++;
45660ce86c7SWei Liu                 f->rclm_lst = reclaim_list;
45760ce86c7SWei Liu                 reclaim_list = f;
45860ce86c7SWei Liu                 f->fs_reclaim.fd = f->fs.fd;
45960ce86c7SWei Liu                 f->fs.fd = -1;
46060ce86c7SWei Liu                 reclaim_count++;
46160ce86c7SWei Liu             }
46260ce86c7SWei Liu         } else if (f->fid_type == P9_FID_DIR) {
463f314ea4eSGreg Kurz             if (f->fs.dir.stream != NULL) {
46460ce86c7SWei Liu                 /*
46560ce86c7SWei Liu                  * Up the reference count so that
46660ce86c7SWei Liu                  * a clunk request won't free this fid
46760ce86c7SWei Liu                  */
46860ce86c7SWei Liu                 f->ref++;
46960ce86c7SWei Liu                 f->rclm_lst = reclaim_list;
47060ce86c7SWei Liu                 reclaim_list = f;
471f314ea4eSGreg Kurz                 f->fs_reclaim.dir.stream = f->fs.dir.stream;
472f314ea4eSGreg Kurz                 f->fs.dir.stream = NULL;
47360ce86c7SWei Liu                 reclaim_count++;
47460ce86c7SWei Liu             }
47560ce86c7SWei Liu         }
47660ce86c7SWei Liu         if (reclaim_count >= open_fd_rc) {
47760ce86c7SWei Liu             break;
47860ce86c7SWei Liu         }
47960ce86c7SWei Liu     }
48060ce86c7SWei Liu     /*
48160ce86c7SWei Liu      * Now close the fid in reclaim list. Free them if they
48260ce86c7SWei Liu      * are already clunked.
48360ce86c7SWei Liu      */
48460ce86c7SWei Liu     while (reclaim_list) {
48560ce86c7SWei Liu         f = reclaim_list;
48660ce86c7SWei Liu         reclaim_list = f->rclm_lst;
48760ce86c7SWei Liu         if (f->fid_type == P9_FID_FILE) {
48860ce86c7SWei Liu             v9fs_co_close(pdu, &f->fs_reclaim);
48960ce86c7SWei Liu         } else if (f->fid_type == P9_FID_DIR) {
49060ce86c7SWei Liu             v9fs_co_closedir(pdu, &f->fs_reclaim);
49160ce86c7SWei Liu         }
49260ce86c7SWei Liu         f->rclm_lst = NULL;
49360ce86c7SWei Liu         /*
49460ce86c7SWei Liu          * Now drop the fid reference, free it
49560ce86c7SWei Liu          * if clunked.
49660ce86c7SWei Liu          */
49760ce86c7SWei Liu         put_fid(pdu, f);
49860ce86c7SWei Liu     }
49960ce86c7SWei Liu }
50060ce86c7SWei Liu 
5018440e22eSGreg Kurz static int coroutine_fn v9fs_mark_fids_unreclaim(V9fsPDU *pdu, V9fsPath *path)
50260ce86c7SWei Liu {
50360ce86c7SWei Liu     int err;
50460ce86c7SWei Liu     V9fsState *s = pdu->s;
50560ce86c7SWei Liu     V9fsFidState *fidp, head_fid;
50660ce86c7SWei Liu 
50760ce86c7SWei Liu     head_fid.next = s->fid_list;
50860ce86c7SWei Liu     for (fidp = s->fid_list; fidp; fidp = fidp->next) {
50960ce86c7SWei Liu         if (fidp->path.size != path->size) {
51060ce86c7SWei Liu             continue;
51160ce86c7SWei Liu         }
51260ce86c7SWei Liu         if (!memcmp(fidp->path.data, path->data, path->size)) {
51360ce86c7SWei Liu             /* Mark the fid non reclaimable. */
51460ce86c7SWei Liu             fidp->flags |= FID_NON_RECLAIMABLE;
51560ce86c7SWei Liu 
51660ce86c7SWei Liu             /* reopen the file/dir if already closed */
51760ce86c7SWei Liu             err = v9fs_reopen_fid(pdu, fidp);
51860ce86c7SWei Liu             if (err < 0) {
519267fcadfSGreg Kurz                 return err;
52060ce86c7SWei Liu             }
52160ce86c7SWei Liu             /*
52260ce86c7SWei Liu              * Go back to head of fid list because
52360ce86c7SWei Liu              * the list could have got updated when
52460ce86c7SWei Liu              * switched to the worker thread
52560ce86c7SWei Liu              */
52660ce86c7SWei Liu             if (err == 0) {
52760ce86c7SWei Liu                 fidp = &head_fid;
52860ce86c7SWei Liu             }
52960ce86c7SWei Liu         }
53060ce86c7SWei Liu     }
53160ce86c7SWei Liu     return 0;
53260ce86c7SWei Liu }
53360ce86c7SWei Liu 
5348440e22eSGreg Kurz static void coroutine_fn virtfs_reset(V9fsPDU *pdu)
53560ce86c7SWei Liu {
53660ce86c7SWei Liu     V9fsState *s = pdu->s;
53779decce3SGreg Kurz     V9fsFidState *fidp;
53860ce86c7SWei Liu 
53960ce86c7SWei Liu     /* Free all fids */
54060ce86c7SWei Liu     while (s->fid_list) {
5416d54af0eSGreg Kurz         /* Get fid */
54260ce86c7SWei Liu         fidp = s->fid_list;
5436d54af0eSGreg Kurz         fidp->ref++;
54460ce86c7SWei Liu 
5456d54af0eSGreg Kurz         /* Clunk fid */
5466d54af0eSGreg Kurz         s->fid_list = fidp->next;
54760ce86c7SWei Liu         fidp->clunked = 1;
5486d54af0eSGreg Kurz 
5496d54af0eSGreg Kurz         put_fid(pdu, fidp);
55060ce86c7SWei Liu     }
55160ce86c7SWei Liu }
55260ce86c7SWei Liu 
55360ce86c7SWei Liu #define P9_QID_TYPE_DIR         0x80
55460ce86c7SWei Liu #define P9_QID_TYPE_SYMLINK     0x02
55560ce86c7SWei Liu 
55660ce86c7SWei Liu #define P9_STAT_MODE_DIR        0x80000000
55760ce86c7SWei Liu #define P9_STAT_MODE_APPEND     0x40000000
55860ce86c7SWei Liu #define P9_STAT_MODE_EXCL       0x20000000
55960ce86c7SWei Liu #define P9_STAT_MODE_MOUNT      0x10000000
56060ce86c7SWei Liu #define P9_STAT_MODE_AUTH       0x08000000
56160ce86c7SWei Liu #define P9_STAT_MODE_TMP        0x04000000
56260ce86c7SWei Liu #define P9_STAT_MODE_SYMLINK    0x02000000
56360ce86c7SWei Liu #define P9_STAT_MODE_LINK       0x01000000
56460ce86c7SWei Liu #define P9_STAT_MODE_DEVICE     0x00800000
56560ce86c7SWei Liu #define P9_STAT_MODE_NAMED_PIPE 0x00200000
56660ce86c7SWei Liu #define P9_STAT_MODE_SOCKET     0x00100000
56760ce86c7SWei Liu #define P9_STAT_MODE_SETUID     0x00080000
56860ce86c7SWei Liu #define P9_STAT_MODE_SETGID     0x00040000
56960ce86c7SWei Liu #define P9_STAT_MODE_SETVTX     0x00010000
57060ce86c7SWei Liu 
57160ce86c7SWei Liu #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR |          \
57260ce86c7SWei Liu                                 P9_STAT_MODE_SYMLINK |      \
57360ce86c7SWei Liu                                 P9_STAT_MODE_LINK |         \
57460ce86c7SWei Liu                                 P9_STAT_MODE_DEVICE |       \
57560ce86c7SWei Liu                                 P9_STAT_MODE_NAMED_PIPE |   \
57660ce86c7SWei Liu                                 P9_STAT_MODE_SOCKET)
57760ce86c7SWei Liu 
5786b6aa828SChristian Schoenebeck /* Mirrors all bits of a byte. So e.g. binary 10100000 would become 00000101. */
5796b6aa828SChristian Schoenebeck static inline uint8_t mirror8bit(uint8_t byte)
5806b6aa828SChristian Schoenebeck {
5816b6aa828SChristian Schoenebeck     return (byte * 0x0202020202ULL & 0x010884422010ULL) % 1023;
5826b6aa828SChristian Schoenebeck }
5836b6aa828SChristian Schoenebeck 
5846b6aa828SChristian Schoenebeck /* Same as mirror8bit() just for a 64 bit data type instead for a byte. */
5856b6aa828SChristian Schoenebeck static inline uint64_t mirror64bit(uint64_t value)
5866b6aa828SChristian Schoenebeck {
5876b6aa828SChristian Schoenebeck     return ((uint64_t)mirror8bit(value         & 0xff) << 56) |
5886b6aa828SChristian Schoenebeck            ((uint64_t)mirror8bit((value >> 8)  & 0xff) << 48) |
5896b6aa828SChristian Schoenebeck            ((uint64_t)mirror8bit((value >> 16) & 0xff) << 40) |
5906b6aa828SChristian Schoenebeck            ((uint64_t)mirror8bit((value >> 24) & 0xff) << 32) |
5916b6aa828SChristian Schoenebeck            ((uint64_t)mirror8bit((value >> 32) & 0xff) << 24) |
5926b6aa828SChristian Schoenebeck            ((uint64_t)mirror8bit((value >> 40) & 0xff) << 16) |
5936b6aa828SChristian Schoenebeck            ((uint64_t)mirror8bit((value >> 48) & 0xff) << 8)  |
5946b6aa828SChristian Schoenebeck            ((uint64_t)mirror8bit((value >> 56) & 0xff));
5956b6aa828SChristian Schoenebeck }
5966b6aa828SChristian Schoenebeck 
5976b6aa828SChristian Schoenebeck /**
5986b6aa828SChristian Schoenebeck  * @brief Parameter k for the Exponential Golomb algorihm to be used.
5996b6aa828SChristian Schoenebeck  *
6006b6aa828SChristian Schoenebeck  * The smaller this value, the smaller the minimum bit count for the Exp.
6016b6aa828SChristian Schoenebeck  * Golomb generated affixes will be (at lowest index) however for the
6026b6aa828SChristian Schoenebeck  * price of having higher maximum bit count of generated affixes (at highest
6036b6aa828SChristian Schoenebeck  * index). Likewise increasing this parameter yields in smaller maximum bit
6046b6aa828SChristian Schoenebeck  * count for the price of having higher minimum bit count.
6056b6aa828SChristian Schoenebeck  *
6066b6aa828SChristian Schoenebeck  * In practice that means: a good value for k depends on the expected amount
6076b6aa828SChristian Schoenebeck  * of devices to be exposed by one export. For a small amount of devices k
6086b6aa828SChristian Schoenebeck  * should be small, for a large amount of devices k might be increased
6096b6aa828SChristian Schoenebeck  * instead. The default of k=0 should be fine for most users though.
6106b6aa828SChristian Schoenebeck  *
6116b6aa828SChristian Schoenebeck  * @b IMPORTANT: In case this ever becomes a runtime parameter; the value of
6126b6aa828SChristian Schoenebeck  * k should not change as long as guest is still running! Because that would
6136b6aa828SChristian Schoenebeck  * cause completely different inode numbers to be generated on guest.
6146b6aa828SChristian Schoenebeck  */
6156b6aa828SChristian Schoenebeck #define EXP_GOLOMB_K    0
6166b6aa828SChristian Schoenebeck 
6176b6aa828SChristian Schoenebeck /**
6186b6aa828SChristian Schoenebeck  * @brief Exponential Golomb algorithm for arbitrary k (including k=0).
6196b6aa828SChristian Schoenebeck  *
6206b6aa828SChristian Schoenebeck  * The Exponential Golomb algorithm generates @b prefixes (@b not suffixes!)
6216b6aa828SChristian Schoenebeck  * with growing length and with the mathematical property of being
6226b6aa828SChristian Schoenebeck  * "prefix-free". The latter means the generated prefixes can be prepended
6236b6aa828SChristian Schoenebeck  * in front of arbitrary numbers and the resulting concatenated numbers are
6246b6aa828SChristian Schoenebeck  * guaranteed to be always unique.
6256b6aa828SChristian Schoenebeck  *
6266b6aa828SChristian Schoenebeck  * This is a minor adjustment to the original Exp. Golomb algorithm in the
6276b6aa828SChristian Schoenebeck  * sense that lowest allowed index (@param n) starts with 1, not with zero.
6286b6aa828SChristian Schoenebeck  *
6296b6aa828SChristian Schoenebeck  * @param n - natural number (or index) of the prefix to be generated
6306b6aa828SChristian Schoenebeck  *            (1, 2, 3, ...)
6316b6aa828SChristian Schoenebeck  * @param k - parameter k of Exp. Golomb algorithm to be used
6326b6aa828SChristian Schoenebeck  *            (see comment on EXP_GOLOMB_K macro for details about k)
6336b6aa828SChristian Schoenebeck  */
6346b6aa828SChristian Schoenebeck static VariLenAffix expGolombEncode(uint64_t n, int k)
6356b6aa828SChristian Schoenebeck {
6366b6aa828SChristian Schoenebeck     const uint64_t value = n + (1 << k) - 1;
6376b6aa828SChristian Schoenebeck     const int bits = (int) log2(value) + 1;
6386b6aa828SChristian Schoenebeck     return (VariLenAffix) {
6396b6aa828SChristian Schoenebeck         .type = AffixType_Prefix,
6406b6aa828SChristian Schoenebeck         .value = value,
6416b6aa828SChristian Schoenebeck         .bits = bits + MAX((bits - 1 - k), 0)
6426b6aa828SChristian Schoenebeck     };
6436b6aa828SChristian Schoenebeck }
6446b6aa828SChristian Schoenebeck 
6456b6aa828SChristian Schoenebeck /**
6466b6aa828SChristian Schoenebeck  * @brief Converts a suffix into a prefix, or a prefix into a suffix.
6476b6aa828SChristian Schoenebeck  *
6486b6aa828SChristian Schoenebeck  * Simply mirror all bits of the affix value, for the purpose to preserve
6496b6aa828SChristian Schoenebeck  * respectively the mathematical "prefix-free" or "suffix-free" property
6506b6aa828SChristian Schoenebeck  * after the conversion.
6516b6aa828SChristian Schoenebeck  *
6526b6aa828SChristian Schoenebeck  * If a passed prefix is suitable to create unique numbers, then the
6536b6aa828SChristian Schoenebeck  * returned suffix is suitable to create unique numbers as well (and vice
6546b6aa828SChristian Schoenebeck  * versa).
6556b6aa828SChristian Schoenebeck  */
6566b6aa828SChristian Schoenebeck static VariLenAffix invertAffix(const VariLenAffix *affix)
6576b6aa828SChristian Schoenebeck {
6586b6aa828SChristian Schoenebeck     return (VariLenAffix) {
6596b6aa828SChristian Schoenebeck         .type =
6606b6aa828SChristian Schoenebeck             (affix->type == AffixType_Suffix) ?
6616b6aa828SChristian Schoenebeck                 AffixType_Prefix : AffixType_Suffix,
6626b6aa828SChristian Schoenebeck         .value =
6636b6aa828SChristian Schoenebeck             mirror64bit(affix->value) >>
6646b6aa828SChristian Schoenebeck             ((sizeof(affix->value) * 8) - affix->bits),
6656b6aa828SChristian Schoenebeck         .bits = affix->bits
6666b6aa828SChristian Schoenebeck     };
6676b6aa828SChristian Schoenebeck }
6686b6aa828SChristian Schoenebeck 
6696b6aa828SChristian Schoenebeck /**
6706b6aa828SChristian Schoenebeck  * @brief Generates suffix numbers with "suffix-free" property.
6716b6aa828SChristian Schoenebeck  *
6726b6aa828SChristian Schoenebeck  * This is just a wrapper function on top of the Exp. Golomb algorithm.
6736b6aa828SChristian Schoenebeck  *
6746b6aa828SChristian Schoenebeck  * Since the Exp. Golomb algorithm generates prefixes, but we need suffixes,
6756b6aa828SChristian Schoenebeck  * this function converts the Exp. Golomb prefixes into appropriate suffixes
6766b6aa828SChristian Schoenebeck  * which are still suitable for generating unique numbers.
6776b6aa828SChristian Schoenebeck  *
6786b6aa828SChristian Schoenebeck  * @param n - natural number (or index) of the suffix to be generated
6796b6aa828SChristian Schoenebeck  *            (1, 2, 3, ...)
6806b6aa828SChristian Schoenebeck  */
6816b6aa828SChristian Schoenebeck static VariLenAffix affixForIndex(uint64_t index)
6826b6aa828SChristian Schoenebeck {
6836b6aa828SChristian Schoenebeck     VariLenAffix prefix;
6846b6aa828SChristian Schoenebeck     prefix = expGolombEncode(index, EXP_GOLOMB_K);
6856b6aa828SChristian Schoenebeck     return invertAffix(&prefix); /* convert prefix to suffix */
6866b6aa828SChristian Schoenebeck }
6876b6aa828SChristian Schoenebeck 
6881a6ed33cSAntonios Motakis /* creative abuse of tb_hash_func7, which is based on xxhash */
6891a6ed33cSAntonios Motakis static uint32_t qpp_hash(QppEntry e)
6901a6ed33cSAntonios Motakis {
6911a6ed33cSAntonios Motakis     return qemu_xxhash7(e.ino_prefix, e.dev, 0, 0, 0);
6921a6ed33cSAntonios Motakis }
6931a6ed33cSAntonios Motakis 
694f3fe4a2dSAntonios Motakis static uint32_t qpf_hash(QpfEntry e)
695f3fe4a2dSAntonios Motakis {
696f3fe4a2dSAntonios Motakis     return qemu_xxhash7(e.ino, e.dev, 0, 0, 0);
697f3fe4a2dSAntonios Motakis }
698f3fe4a2dSAntonios Motakis 
6996b6aa828SChristian Schoenebeck static bool qpd_cmp_func(const void *obj, const void *userp)
7006b6aa828SChristian Schoenebeck {
7016b6aa828SChristian Schoenebeck     const QpdEntry *e1 = obj, *e2 = userp;
7026b6aa828SChristian Schoenebeck     return e1->dev == e2->dev;
7036b6aa828SChristian Schoenebeck }
7046b6aa828SChristian Schoenebeck 
7056b6aa828SChristian Schoenebeck static bool qpp_cmp_func(const void *obj, const void *userp)
7061a6ed33cSAntonios Motakis {
7071a6ed33cSAntonios Motakis     const QppEntry *e1 = obj, *e2 = userp;
7081a6ed33cSAntonios Motakis     return e1->dev == e2->dev && e1->ino_prefix == e2->ino_prefix;
7091a6ed33cSAntonios Motakis }
7101a6ed33cSAntonios Motakis 
7116b6aa828SChristian Schoenebeck static bool qpf_cmp_func(const void *obj, const void *userp)
712f3fe4a2dSAntonios Motakis {
713f3fe4a2dSAntonios Motakis     const QpfEntry *e1 = obj, *e2 = userp;
714f3fe4a2dSAntonios Motakis     return e1->dev == e2->dev && e1->ino == e2->ino;
715f3fe4a2dSAntonios Motakis }
716f3fe4a2dSAntonios Motakis 
717f3fe4a2dSAntonios Motakis static void qp_table_remove(void *p, uint32_t h, void *up)
7181a6ed33cSAntonios Motakis {
7191a6ed33cSAntonios Motakis     g_free(p);
7201a6ed33cSAntonios Motakis }
7211a6ed33cSAntonios Motakis 
722f3fe4a2dSAntonios Motakis static void qp_table_destroy(struct qht *ht)
7231a6ed33cSAntonios Motakis {
7241a6ed33cSAntonios Motakis     if (!ht || !ht->map) {
7251a6ed33cSAntonios Motakis         return;
7261a6ed33cSAntonios Motakis     }
727f3fe4a2dSAntonios Motakis     qht_iter(ht, qp_table_remove, NULL);
7281a6ed33cSAntonios Motakis     qht_destroy(ht);
7291a6ed33cSAntonios Motakis }
7301a6ed33cSAntonios Motakis 
7316b6aa828SChristian Schoenebeck static void qpd_table_init(struct qht *ht)
7326b6aa828SChristian Schoenebeck {
7336b6aa828SChristian Schoenebeck     qht_init(ht, qpd_cmp_func, 1, QHT_MODE_AUTO_RESIZE);
7346b6aa828SChristian Schoenebeck }
7356b6aa828SChristian Schoenebeck 
7361a6ed33cSAntonios Motakis static void qpp_table_init(struct qht *ht)
7371a6ed33cSAntonios Motakis {
7386b6aa828SChristian Schoenebeck     qht_init(ht, qpp_cmp_func, 1, QHT_MODE_AUTO_RESIZE);
7391a6ed33cSAntonios Motakis }
7401a6ed33cSAntonios Motakis 
741f3fe4a2dSAntonios Motakis static void qpf_table_init(struct qht *ht)
742f3fe4a2dSAntonios Motakis {
7436b6aa828SChristian Schoenebeck     qht_init(ht, qpf_cmp_func, 1 << 16, QHT_MODE_AUTO_RESIZE);
744f3fe4a2dSAntonios Motakis }
745f3fe4a2dSAntonios Motakis 
7466b6aa828SChristian Schoenebeck /*
7476b6aa828SChristian Schoenebeck  * Returns how many (high end) bits of inode numbers of the passed fs
7486b6aa828SChristian Schoenebeck  * device shall be used (in combination with the device number) to
7496b6aa828SChristian Schoenebeck  * generate hash values for qpp_table entries.
7506b6aa828SChristian Schoenebeck  *
7516b6aa828SChristian Schoenebeck  * This function is required if variable length suffixes are used for inode
7526b6aa828SChristian Schoenebeck  * number mapping on guest level. Since a device may end up having multiple
7536b6aa828SChristian Schoenebeck  * entries in qpp_table, each entry most probably with a different suffix
7546b6aa828SChristian Schoenebeck  * length, we thus need this function in conjunction with qpd_table to
7556b6aa828SChristian Schoenebeck  * "agree" about a fix amount of bits (per device) to be always used for
7566b6aa828SChristian Schoenebeck  * generating hash values for the purpose of accessing qpp_table in order
7576b6aa828SChristian Schoenebeck  * get consistent behaviour when accessing qpp_table.
7586b6aa828SChristian Schoenebeck  */
7596b6aa828SChristian Schoenebeck static int qid_inode_prefix_hash_bits(V9fsPDU *pdu, dev_t dev)
7606b6aa828SChristian Schoenebeck {
7616b6aa828SChristian Schoenebeck     QpdEntry lookup = {
7626b6aa828SChristian Schoenebeck         .dev = dev
7636b6aa828SChristian Schoenebeck     }, *val;
7646b6aa828SChristian Schoenebeck     uint32_t hash = dev;
7656b6aa828SChristian Schoenebeck     VariLenAffix affix;
7666b6aa828SChristian Schoenebeck 
7676b6aa828SChristian Schoenebeck     val = qht_lookup(&pdu->s->qpd_table, &lookup, hash);
7686b6aa828SChristian Schoenebeck     if (!val) {
7696b6aa828SChristian Schoenebeck         val = g_malloc0(sizeof(QpdEntry));
7706b6aa828SChristian Schoenebeck         *val = lookup;
7716b6aa828SChristian Schoenebeck         affix = affixForIndex(pdu->s->qp_affix_next);
7726b6aa828SChristian Schoenebeck         val->prefix_bits = affix.bits;
7736b6aa828SChristian Schoenebeck         qht_insert(&pdu->s->qpd_table, val, hash, NULL);
7746b6aa828SChristian Schoenebeck         pdu->s->qp_ndevices++;
7756b6aa828SChristian Schoenebeck     }
7766b6aa828SChristian Schoenebeck     return val->prefix_bits;
7776b6aa828SChristian Schoenebeck }
7786b6aa828SChristian Schoenebeck 
7796b6aa828SChristian Schoenebeck /**
7806b6aa828SChristian Schoenebeck  * @brief Slow / full mapping host inode nr -> guest inode nr.
7816b6aa828SChristian Schoenebeck  *
7826b6aa828SChristian Schoenebeck  * This function performs a slower and much more costly remapping of an
7836b6aa828SChristian Schoenebeck  * original file inode number on host to an appropriate different inode
7846b6aa828SChristian Schoenebeck  * number on guest. For every (dev, inode) combination on host a new
7856b6aa828SChristian Schoenebeck  * sequential number is generated, cached and exposed as inode number on
7866b6aa828SChristian Schoenebeck  * guest.
7876b6aa828SChristian Schoenebeck  *
7886b6aa828SChristian Schoenebeck  * This is just a "last resort" fallback solution if the much faster/cheaper
7896b6aa828SChristian Schoenebeck  * qid_path_suffixmap() failed. In practice this slow / full mapping is not
7906b6aa828SChristian Schoenebeck  * expected ever to be used at all though.
7916b6aa828SChristian Schoenebeck  *
7926b6aa828SChristian Schoenebeck  * @see qid_path_suffixmap() for details
7936b6aa828SChristian Schoenebeck  *
7946b6aa828SChristian Schoenebeck  */
795f3fe4a2dSAntonios Motakis static int qid_path_fullmap(V9fsPDU *pdu, const struct stat *stbuf,
796f3fe4a2dSAntonios Motakis                             uint64_t *path)
797f3fe4a2dSAntonios Motakis {
798f3fe4a2dSAntonios Motakis     QpfEntry lookup = {
799f3fe4a2dSAntonios Motakis         .dev = stbuf->st_dev,
800f3fe4a2dSAntonios Motakis         .ino = stbuf->st_ino
801f3fe4a2dSAntonios Motakis     }, *val;
802f3fe4a2dSAntonios Motakis     uint32_t hash = qpf_hash(lookup);
8036b6aa828SChristian Schoenebeck     VariLenAffix affix;
804f3fe4a2dSAntonios Motakis 
805f3fe4a2dSAntonios Motakis     val = qht_lookup(&pdu->s->qpf_table, &lookup, hash);
806f3fe4a2dSAntonios Motakis 
807f3fe4a2dSAntonios Motakis     if (!val) {
808f3fe4a2dSAntonios Motakis         if (pdu->s->qp_fullpath_next == 0) {
809f3fe4a2dSAntonios Motakis             /* no more files can be mapped :'( */
810f3fe4a2dSAntonios Motakis             error_report_once(
811f3fe4a2dSAntonios Motakis                 "9p: No more prefixes available for remapping inodes from "
812f3fe4a2dSAntonios Motakis                 "host to guest."
813f3fe4a2dSAntonios Motakis             );
814f3fe4a2dSAntonios Motakis             return -ENFILE;
815f3fe4a2dSAntonios Motakis         }
816f3fe4a2dSAntonios Motakis 
817f3fe4a2dSAntonios Motakis         val = g_malloc0(sizeof(QppEntry));
818f3fe4a2dSAntonios Motakis         *val = lookup;
819f3fe4a2dSAntonios Motakis 
820f3fe4a2dSAntonios Motakis         /* new unique inode and device combo */
8216b6aa828SChristian Schoenebeck         affix = affixForIndex(
8226b6aa828SChristian Schoenebeck             1ULL << (sizeof(pdu->s->qp_affix_next) * 8)
8236b6aa828SChristian Schoenebeck         );
8246b6aa828SChristian Schoenebeck         val->path = (pdu->s->qp_fullpath_next++ << affix.bits) | affix.value;
8256b6aa828SChristian Schoenebeck         pdu->s->qp_fullpath_next &= ((1ULL << (64 - affix.bits)) - 1);
826f3fe4a2dSAntonios Motakis         qht_insert(&pdu->s->qpf_table, val, hash, NULL);
827f3fe4a2dSAntonios Motakis     }
828f3fe4a2dSAntonios Motakis 
829f3fe4a2dSAntonios Motakis     *path = val->path;
830f3fe4a2dSAntonios Motakis     return 0;
831f3fe4a2dSAntonios Motakis }
832f3fe4a2dSAntonios Motakis 
8336b6aa828SChristian Schoenebeck /**
8346b6aa828SChristian Schoenebeck  * @brief Quick mapping host inode nr -> guest inode nr.
8351a6ed33cSAntonios Motakis  *
8366b6aa828SChristian Schoenebeck  * This function performs quick remapping of an original file inode number
8376b6aa828SChristian Schoenebeck  * on host to an appropriate different inode number on guest. This remapping
8386b6aa828SChristian Schoenebeck  * of inodes is required to avoid inode nr collisions on guest which would
8396b6aa828SChristian Schoenebeck  * happen if the 9p export contains more than 1 exported file system (or
8406b6aa828SChristian Schoenebeck  * more than 1 file system data set), because unlike on host level where the
8416b6aa828SChristian Schoenebeck  * files would have different device nrs, all files exported by 9p would
8426b6aa828SChristian Schoenebeck  * share the same device nr on guest (the device nr of the virtual 9p device
8436b6aa828SChristian Schoenebeck  * that is).
8446b6aa828SChristian Schoenebeck  *
8456b6aa828SChristian Schoenebeck  * Inode remapping is performed by chopping off high end bits of the original
8466b6aa828SChristian Schoenebeck  * inode number from host, shifting the result upwards and then assigning a
8476b6aa828SChristian Schoenebeck  * generated suffix number for the low end bits, where the same suffix number
8486b6aa828SChristian Schoenebeck  * will be shared by all inodes with the same device id AND the same high end
8496b6aa828SChristian Schoenebeck  * bits that have been chopped off. That approach utilizes the fact that inode
8506b6aa828SChristian Schoenebeck  * numbers very likely share the same high end bits (i.e. due to their common
8516b6aa828SChristian Schoenebeck  * sequential generation by file systems) and hence we only have to generate
8526b6aa828SChristian Schoenebeck  * and track a very limited amount of suffixes in practice due to that.
8536b6aa828SChristian Schoenebeck  *
8546b6aa828SChristian Schoenebeck  * We generate variable size suffixes for that purpose. The 1st generated
8556b6aa828SChristian Schoenebeck  * suffix will only have 1 bit and hence we only need to chop off 1 bit from
8566b6aa828SChristian Schoenebeck  * the original inode number. The subsequent suffixes being generated will
8576b6aa828SChristian Schoenebeck  * grow in (bit) size subsequently, i.e. the 2nd and 3rd suffix being
8586b6aa828SChristian Schoenebeck  * generated will have 3 bits and hence we have to chop off 3 bits from their
8596b6aa828SChristian Schoenebeck  * original inodes, and so on. That approach of using variable length suffixes
8606b6aa828SChristian Schoenebeck  * (i.e. over fixed size ones) utilizes the fact that in practice only a very
8616b6aa828SChristian Schoenebeck  * limited amount of devices are shared by the same export (e.g. typically
8626b6aa828SChristian Schoenebeck  * less than 2 dozen devices per 9p export), so in practice we need to chop
8636b6aa828SChristian Schoenebeck  * off less bits than with fixed size prefixes and yet are flexible to add
8646b6aa828SChristian Schoenebeck  * new devices at runtime below host's export directory at any time without
8656b6aa828SChristian Schoenebeck  * having to reboot guest nor requiring to reconfigure guest for that. And due
8666b6aa828SChristian Schoenebeck  * to the very limited amount of original high end bits that we chop off that
8676b6aa828SChristian Schoenebeck  * way, the total amount of suffixes we need to generate is less than by using
8686b6aa828SChristian Schoenebeck  * fixed size prefixes and hence it also improves performance of the inode
8696b6aa828SChristian Schoenebeck  * remapping algorithm, and finally has the nice side effect that the inode
8706b6aa828SChristian Schoenebeck  * numbers on guest will be much smaller & human friendly. ;-)
8711a6ed33cSAntonios Motakis  */
8726b6aa828SChristian Schoenebeck static int qid_path_suffixmap(V9fsPDU *pdu, const struct stat *stbuf,
8731a6ed33cSAntonios Motakis                               uint64_t *path)
8741a6ed33cSAntonios Motakis {
8756b6aa828SChristian Schoenebeck     const int ino_hash_bits = qid_inode_prefix_hash_bits(pdu, stbuf->st_dev);
8761a6ed33cSAntonios Motakis     QppEntry lookup = {
8771a6ed33cSAntonios Motakis         .dev = stbuf->st_dev,
8786b6aa828SChristian Schoenebeck         .ino_prefix = (uint16_t) (stbuf->st_ino >> (64 - ino_hash_bits))
8791a6ed33cSAntonios Motakis     }, *val;
8801a6ed33cSAntonios Motakis     uint32_t hash = qpp_hash(lookup);
8811a6ed33cSAntonios Motakis 
8821a6ed33cSAntonios Motakis     val = qht_lookup(&pdu->s->qpp_table, &lookup, hash);
8831a6ed33cSAntonios Motakis 
8841a6ed33cSAntonios Motakis     if (!val) {
8856b6aa828SChristian Schoenebeck         if (pdu->s->qp_affix_next == 0) {
8866b6aa828SChristian Schoenebeck             /* we ran out of affixes */
887f3fe4a2dSAntonios Motakis             warn_report_once(
888f3fe4a2dSAntonios Motakis                 "9p: Potential degraded performance of inode remapping"
8891a6ed33cSAntonios Motakis             );
8901a6ed33cSAntonios Motakis             return -ENFILE;
8911a6ed33cSAntonios Motakis         }
8921a6ed33cSAntonios Motakis 
8931a6ed33cSAntonios Motakis         val = g_malloc0(sizeof(QppEntry));
8941a6ed33cSAntonios Motakis         *val = lookup;
8951a6ed33cSAntonios Motakis 
8966b6aa828SChristian Schoenebeck         /* new unique inode affix and device combo */
8976b6aa828SChristian Schoenebeck         val->qp_affix_index = pdu->s->qp_affix_next++;
8986b6aa828SChristian Schoenebeck         val->qp_affix = affixForIndex(val->qp_affix_index);
8991a6ed33cSAntonios Motakis         qht_insert(&pdu->s->qpp_table, val, hash, NULL);
9001a6ed33cSAntonios Motakis     }
9016b6aa828SChristian Schoenebeck     /* assuming generated affix to be suffix type, not prefix */
9026b6aa828SChristian Schoenebeck     *path = (stbuf->st_ino << val->qp_affix.bits) | val->qp_affix.value;
9031a6ed33cSAntonios Motakis     return 0;
9041a6ed33cSAntonios Motakis }
9051a6ed33cSAntonios Motakis 
9063b5ee9e8SAntonios Motakis static int stat_to_qid(V9fsPDU *pdu, const struct stat *stbuf, V9fsQID *qidp)
90760ce86c7SWei Liu {
9081a6ed33cSAntonios Motakis     int err;
90960ce86c7SWei Liu     size_t size;
91060ce86c7SWei Liu 
9111a6ed33cSAntonios Motakis     if (pdu->s->ctx.export_flags & V9FS_REMAP_INODES) {
9121a6ed33cSAntonios Motakis         /* map inode+device to qid path (fast path) */
9136b6aa828SChristian Schoenebeck         err = qid_path_suffixmap(pdu, stbuf, &qidp->path);
914f3fe4a2dSAntonios Motakis         if (err == -ENFILE) {
915f3fe4a2dSAntonios Motakis             /* fast path didn't work, fall back to full map */
916f3fe4a2dSAntonios Motakis             err = qid_path_fullmap(pdu, stbuf, &qidp->path);
917f3fe4a2dSAntonios Motakis         }
9181a6ed33cSAntonios Motakis         if (err) {
9191a6ed33cSAntonios Motakis             return err;
9201a6ed33cSAntonios Motakis         }
9211a6ed33cSAntonios Motakis     } else {
9223b5ee9e8SAntonios Motakis         if (pdu->s->dev_id != stbuf->st_dev) {
9231a6ed33cSAntonios Motakis             if (pdu->s->ctx.export_flags & V9FS_FORBID_MULTIDEVS) {
9241a6ed33cSAntonios Motakis                 error_report_once(
9251a6ed33cSAntonios Motakis                     "9p: Multiple devices detected in same VirtFS export. "
9261a6ed33cSAntonios Motakis                     "Access of guest to additional devices is (partly) "
9271a6ed33cSAntonios Motakis                     "denied due to virtfs option 'multidevs=forbid' being "
9281a6ed33cSAntonios Motakis                     "effective."
9291a6ed33cSAntonios Motakis                 );
9301a6ed33cSAntonios Motakis                 return -ENODEV;
9311a6ed33cSAntonios Motakis             } else {
9323b5ee9e8SAntonios Motakis                 warn_report_once(
9333b5ee9e8SAntonios Motakis                     "9p: Multiple devices detected in same VirtFS export, "
9343b5ee9e8SAntonios Motakis                     "which might lead to file ID collisions and severe "
9351a6ed33cSAntonios Motakis                     "misbehaviours on guest! You should either use a "
9361a6ed33cSAntonios Motakis                     "separate export for each device shared from host or "
9371a6ed33cSAntonios Motakis                     "use virtfs option 'multidevs=remap'!"
9383b5ee9e8SAntonios Motakis                 );
9393b5ee9e8SAntonios Motakis             }
9401a6ed33cSAntonios Motakis         }
94160ce86c7SWei Liu         memset(&qidp->path, 0, sizeof(qidp->path));
94260ce86c7SWei Liu         size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
94360ce86c7SWei Liu         memcpy(&qidp->path, &stbuf->st_ino, size);
9441a6ed33cSAntonios Motakis     }
9451a6ed33cSAntonios Motakis 
94660ce86c7SWei Liu     qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
94760ce86c7SWei Liu     qidp->type = 0;
94860ce86c7SWei Liu     if (S_ISDIR(stbuf->st_mode)) {
94960ce86c7SWei Liu         qidp->type |= P9_QID_TYPE_DIR;
95060ce86c7SWei Liu     }
95160ce86c7SWei Liu     if (S_ISLNK(stbuf->st_mode)) {
95260ce86c7SWei Liu         qidp->type |= P9_QID_TYPE_SYMLINK;
95360ce86c7SWei Liu     }
9543b5ee9e8SAntonios Motakis 
9553b5ee9e8SAntonios Motakis     return 0;
95660ce86c7SWei Liu }
95760ce86c7SWei Liu 
9588440e22eSGreg Kurz static int coroutine_fn fid_to_qid(V9fsPDU *pdu, V9fsFidState *fidp,
9598440e22eSGreg Kurz                                    V9fsQID *qidp)
96060ce86c7SWei Liu {
96160ce86c7SWei Liu     struct stat stbuf;
96260ce86c7SWei Liu     int err;
96360ce86c7SWei Liu 
96460ce86c7SWei Liu     err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
96560ce86c7SWei Liu     if (err < 0) {
96660ce86c7SWei Liu         return err;
96760ce86c7SWei Liu     }
9683b5ee9e8SAntonios Motakis     err = stat_to_qid(pdu, &stbuf, qidp);
9693b5ee9e8SAntonios Motakis     if (err < 0) {
9703b5ee9e8SAntonios Motakis         return err;
9713b5ee9e8SAntonios Motakis     }
97260ce86c7SWei Liu     return 0;
97360ce86c7SWei Liu }
97460ce86c7SWei Liu 
9751a6ed33cSAntonios Motakis static int coroutine_fn dirent_to_qid(V9fsPDU *pdu, V9fsFidState *fidp,
9761a6ed33cSAntonios Motakis                                       struct dirent *dent, V9fsQID *qidp)
9771a6ed33cSAntonios Motakis {
9781a6ed33cSAntonios Motakis     struct stat stbuf;
9791a6ed33cSAntonios Motakis     V9fsPath path;
9801a6ed33cSAntonios Motakis     int err;
9811a6ed33cSAntonios Motakis 
9821a6ed33cSAntonios Motakis     v9fs_path_init(&path);
9831a6ed33cSAntonios Motakis 
9841a6ed33cSAntonios Motakis     err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path);
9851a6ed33cSAntonios Motakis     if (err < 0) {
9861a6ed33cSAntonios Motakis         goto out;
9871a6ed33cSAntonios Motakis     }
9881a6ed33cSAntonios Motakis     err = v9fs_co_lstat(pdu, &path, &stbuf);
9891a6ed33cSAntonios Motakis     if (err < 0) {
9901a6ed33cSAntonios Motakis         goto out;
9911a6ed33cSAntonios Motakis     }
9921a6ed33cSAntonios Motakis     err = stat_to_qid(pdu, &stbuf, qidp);
9931a6ed33cSAntonios Motakis 
9941a6ed33cSAntonios Motakis out:
9951a6ed33cSAntonios Motakis     v9fs_path_free(&path);
9961a6ed33cSAntonios Motakis     return err;
9971a6ed33cSAntonios Motakis }
9981a6ed33cSAntonios Motakis 
99960ce86c7SWei Liu V9fsPDU *pdu_alloc(V9fsState *s)
100060ce86c7SWei Liu {
100160ce86c7SWei Liu     V9fsPDU *pdu = NULL;
100260ce86c7SWei Liu 
100360ce86c7SWei Liu     if (!QLIST_EMPTY(&s->free_list)) {
100460ce86c7SWei Liu         pdu = QLIST_FIRST(&s->free_list);
100560ce86c7SWei Liu         QLIST_REMOVE(pdu, next);
100660ce86c7SWei Liu         QLIST_INSERT_HEAD(&s->active_list, pdu, next);
100760ce86c7SWei Liu     }
100860ce86c7SWei Liu     return pdu;
100960ce86c7SWei Liu }
101060ce86c7SWei Liu 
101160ce86c7SWei Liu void pdu_free(V9fsPDU *pdu)
101260ce86c7SWei Liu {
101360ce86c7SWei Liu     V9fsState *s = pdu->s;
1014f74e27bfSGreg Kurz 
1015f74e27bfSGreg Kurz     g_assert(!pdu->cancelled);
101660ce86c7SWei Liu     QLIST_REMOVE(pdu, next);
101760ce86c7SWei Liu     QLIST_INSERT_HEAD(&s->free_list, pdu, next);
101860ce86c7SWei Liu }
101960ce86c7SWei Liu 
10208440e22eSGreg Kurz static void coroutine_fn pdu_complete(V9fsPDU *pdu, ssize_t len)
102160ce86c7SWei Liu {
102260ce86c7SWei Liu     int8_t id = pdu->id + 1; /* Response */
102360ce86c7SWei Liu     V9fsState *s = pdu->s;
102406a37db7SGreg Kurz     int ret;
102560ce86c7SWei Liu 
1026fc78d5eeSKeno Fischer     /*
1027fc78d5eeSKeno Fischer      * The 9p spec requires that successfully cancelled pdus receive no reply.
1028fc78d5eeSKeno Fischer      * Sending a reply would confuse clients because they would
1029fc78d5eeSKeno Fischer      * assume that any EINTR is the actual result of the operation,
1030fc78d5eeSKeno Fischer      * rather than a consequence of the cancellation. However, if
1031fc78d5eeSKeno Fischer      * the operation completed (succesfully or with an error other
1032fc78d5eeSKeno Fischer      * than caused be cancellation), we do send out that reply, both
1033fc78d5eeSKeno Fischer      * for efficiency and to avoid confusing the rest of the state machine
1034fc78d5eeSKeno Fischer      * that assumes passing a non-error here will mean a successful
1035fc78d5eeSKeno Fischer      * transmission of the reply.
1036fc78d5eeSKeno Fischer      */
1037fc78d5eeSKeno Fischer     bool discard = pdu->cancelled && len == -EINTR;
1038fc78d5eeSKeno Fischer     if (discard) {
1039fc78d5eeSKeno Fischer         trace_v9fs_rcancel(pdu->tag, pdu->id);
1040fc78d5eeSKeno Fischer         pdu->size = 0;
1041fc78d5eeSKeno Fischer         goto out_notify;
1042fc78d5eeSKeno Fischer     }
1043fc78d5eeSKeno Fischer 
104460ce86c7SWei Liu     if (len < 0) {
104560ce86c7SWei Liu         int err = -len;
104660ce86c7SWei Liu         len = 7;
104760ce86c7SWei Liu 
104860ce86c7SWei Liu         if (s->proto_version != V9FS_PROTO_2000L) {
104960ce86c7SWei Liu             V9fsString str;
105060ce86c7SWei Liu 
105160ce86c7SWei Liu             str.data = strerror(err);
105260ce86c7SWei Liu             str.size = strlen(str.data);
105360ce86c7SWei Liu 
105406a37db7SGreg Kurz             ret = pdu_marshal(pdu, len, "s", &str);
105506a37db7SGreg Kurz             if (ret < 0) {
105606a37db7SGreg Kurz                 goto out_notify;
105706a37db7SGreg Kurz             }
105806a37db7SGreg Kurz             len += ret;
105960ce86c7SWei Liu             id = P9_RERROR;
106060ce86c7SWei Liu         }
106160ce86c7SWei Liu 
106206a37db7SGreg Kurz         ret = pdu_marshal(pdu, len, "d", err);
106306a37db7SGreg Kurz         if (ret < 0) {
106406a37db7SGreg Kurz             goto out_notify;
106506a37db7SGreg Kurz         }
106606a37db7SGreg Kurz         len += ret;
106760ce86c7SWei Liu 
106860ce86c7SWei Liu         if (s->proto_version == V9FS_PROTO_2000L) {
106960ce86c7SWei Liu             id = P9_RLERROR;
107060ce86c7SWei Liu         }
107160ce86c7SWei Liu         trace_v9fs_rerror(pdu->tag, pdu->id, err); /* Trace ERROR */
107260ce86c7SWei Liu     }
107360ce86c7SWei Liu 
107460ce86c7SWei Liu     /* fill out the header */
107506a37db7SGreg Kurz     if (pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag) < 0) {
107606a37db7SGreg Kurz         goto out_notify;
107706a37db7SGreg Kurz     }
107860ce86c7SWei Liu 
107960ce86c7SWei Liu     /* keep these in sync */
108060ce86c7SWei Liu     pdu->size = len;
108160ce86c7SWei Liu     pdu->id = id;
108260ce86c7SWei Liu 
108306a37db7SGreg Kurz out_notify:
1084a17d8659SGreg Kurz     pdu->s->transport->push_and_notify(pdu);
108560ce86c7SWei Liu 
108660ce86c7SWei Liu     /* Now wakeup anybody waiting in flush for this request */
1087f74e27bfSGreg Kurz     if (!qemu_co_queue_next(&pdu->complete)) {
108860ce86c7SWei Liu         pdu_free(pdu);
108960ce86c7SWei Liu     }
1090f74e27bfSGreg Kurz }
109160ce86c7SWei Liu 
109260ce86c7SWei Liu static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
109360ce86c7SWei Liu {
109460ce86c7SWei Liu     mode_t ret;
109560ce86c7SWei Liu 
109660ce86c7SWei Liu     ret = mode & 0777;
109760ce86c7SWei Liu     if (mode & P9_STAT_MODE_DIR) {
109860ce86c7SWei Liu         ret |= S_IFDIR;
109960ce86c7SWei Liu     }
110060ce86c7SWei Liu 
110160ce86c7SWei Liu     if (mode & P9_STAT_MODE_SYMLINK) {
110260ce86c7SWei Liu         ret |= S_IFLNK;
110360ce86c7SWei Liu     }
110460ce86c7SWei Liu     if (mode & P9_STAT_MODE_SOCKET) {
110560ce86c7SWei Liu         ret |= S_IFSOCK;
110660ce86c7SWei Liu     }
110760ce86c7SWei Liu     if (mode & P9_STAT_MODE_NAMED_PIPE) {
110860ce86c7SWei Liu         ret |= S_IFIFO;
110960ce86c7SWei Liu     }
111060ce86c7SWei Liu     if (mode & P9_STAT_MODE_DEVICE) {
111160ce86c7SWei Liu         if (extension->size && extension->data[0] == 'c') {
111260ce86c7SWei Liu             ret |= S_IFCHR;
111360ce86c7SWei Liu         } else {
111460ce86c7SWei Liu             ret |= S_IFBLK;
111560ce86c7SWei Liu         }
111660ce86c7SWei Liu     }
111760ce86c7SWei Liu 
111860ce86c7SWei Liu     if (!(ret&~0777)) {
111960ce86c7SWei Liu         ret |= S_IFREG;
112060ce86c7SWei Liu     }
112160ce86c7SWei Liu 
112260ce86c7SWei Liu     if (mode & P9_STAT_MODE_SETUID) {
112360ce86c7SWei Liu         ret |= S_ISUID;
112460ce86c7SWei Liu     }
112560ce86c7SWei Liu     if (mode & P9_STAT_MODE_SETGID) {
112660ce86c7SWei Liu         ret |= S_ISGID;
112760ce86c7SWei Liu     }
112860ce86c7SWei Liu     if (mode & P9_STAT_MODE_SETVTX) {
112960ce86c7SWei Liu         ret |= S_ISVTX;
113060ce86c7SWei Liu     }
113160ce86c7SWei Liu 
113260ce86c7SWei Liu     return ret;
113360ce86c7SWei Liu }
113460ce86c7SWei Liu 
113560ce86c7SWei Liu static int donttouch_stat(V9fsStat *stat)
113660ce86c7SWei Liu {
113760ce86c7SWei Liu     if (stat->type == -1 &&
113860ce86c7SWei Liu         stat->dev == -1 &&
113987032833SAntonios Motakis         stat->qid.type == 0xff &&
114087032833SAntonios Motakis         stat->qid.version == (uint32_t) -1 &&
114187032833SAntonios Motakis         stat->qid.path == (uint64_t) -1 &&
114260ce86c7SWei Liu         stat->mode == -1 &&
114360ce86c7SWei Liu         stat->atime == -1 &&
114460ce86c7SWei Liu         stat->mtime == -1 &&
114560ce86c7SWei Liu         stat->length == -1 &&
114660ce86c7SWei Liu         !stat->name.size &&
114760ce86c7SWei Liu         !stat->uid.size &&
114860ce86c7SWei Liu         !stat->gid.size &&
114960ce86c7SWei Liu         !stat->muid.size &&
115060ce86c7SWei Liu         stat->n_uid == -1 &&
115160ce86c7SWei Liu         stat->n_gid == -1 &&
115260ce86c7SWei Liu         stat->n_muid == -1) {
115360ce86c7SWei Liu         return 1;
115460ce86c7SWei Liu     }
115560ce86c7SWei Liu 
115660ce86c7SWei Liu     return 0;
115760ce86c7SWei Liu }
115860ce86c7SWei Liu 
115960ce86c7SWei Liu static void v9fs_stat_init(V9fsStat *stat)
116060ce86c7SWei Liu {
116160ce86c7SWei Liu     v9fs_string_init(&stat->name);
116260ce86c7SWei Liu     v9fs_string_init(&stat->uid);
116360ce86c7SWei Liu     v9fs_string_init(&stat->gid);
116460ce86c7SWei Liu     v9fs_string_init(&stat->muid);
116560ce86c7SWei Liu     v9fs_string_init(&stat->extension);
116660ce86c7SWei Liu }
116760ce86c7SWei Liu 
116860ce86c7SWei Liu static void v9fs_stat_free(V9fsStat *stat)
116960ce86c7SWei Liu {
117060ce86c7SWei Liu     v9fs_string_free(&stat->name);
117160ce86c7SWei Liu     v9fs_string_free(&stat->uid);
117260ce86c7SWei Liu     v9fs_string_free(&stat->gid);
117360ce86c7SWei Liu     v9fs_string_free(&stat->muid);
117460ce86c7SWei Liu     v9fs_string_free(&stat->extension);
117560ce86c7SWei Liu }
117660ce86c7SWei Liu 
117760ce86c7SWei Liu static uint32_t stat_to_v9mode(const struct stat *stbuf)
117860ce86c7SWei Liu {
117960ce86c7SWei Liu     uint32_t mode;
118060ce86c7SWei Liu 
118160ce86c7SWei Liu     mode = stbuf->st_mode & 0777;
118260ce86c7SWei Liu     if (S_ISDIR(stbuf->st_mode)) {
118360ce86c7SWei Liu         mode |= P9_STAT_MODE_DIR;
118460ce86c7SWei Liu     }
118560ce86c7SWei Liu 
118660ce86c7SWei Liu     if (S_ISLNK(stbuf->st_mode)) {
118760ce86c7SWei Liu         mode |= P9_STAT_MODE_SYMLINK;
118860ce86c7SWei Liu     }
118960ce86c7SWei Liu 
119060ce86c7SWei Liu     if (S_ISSOCK(stbuf->st_mode)) {
119160ce86c7SWei Liu         mode |= P9_STAT_MODE_SOCKET;
119260ce86c7SWei Liu     }
119360ce86c7SWei Liu 
119460ce86c7SWei Liu     if (S_ISFIFO(stbuf->st_mode)) {
119560ce86c7SWei Liu         mode |= P9_STAT_MODE_NAMED_PIPE;
119660ce86c7SWei Liu     }
119760ce86c7SWei Liu 
119860ce86c7SWei Liu     if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
119960ce86c7SWei Liu         mode |= P9_STAT_MODE_DEVICE;
120060ce86c7SWei Liu     }
120160ce86c7SWei Liu 
120260ce86c7SWei Liu     if (stbuf->st_mode & S_ISUID) {
120360ce86c7SWei Liu         mode |= P9_STAT_MODE_SETUID;
120460ce86c7SWei Liu     }
120560ce86c7SWei Liu 
120660ce86c7SWei Liu     if (stbuf->st_mode & S_ISGID) {
120760ce86c7SWei Liu         mode |= P9_STAT_MODE_SETGID;
120860ce86c7SWei Liu     }
120960ce86c7SWei Liu 
121060ce86c7SWei Liu     if (stbuf->st_mode & S_ISVTX) {
121160ce86c7SWei Liu         mode |= P9_STAT_MODE_SETVTX;
121260ce86c7SWei Liu     }
121360ce86c7SWei Liu 
121460ce86c7SWei Liu     return mode;
121560ce86c7SWei Liu }
121660ce86c7SWei Liu 
12176069537fSJan Dakinevich static int coroutine_fn stat_to_v9stat(V9fsPDU *pdu, V9fsPath *path,
12186069537fSJan Dakinevich                                        const char *basename,
121960ce86c7SWei Liu                                        const struct stat *stbuf,
122060ce86c7SWei Liu                                        V9fsStat *v9stat)
122160ce86c7SWei Liu {
122260ce86c7SWei Liu     int err;
122360ce86c7SWei Liu 
122460ce86c7SWei Liu     memset(v9stat, 0, sizeof(*v9stat));
122560ce86c7SWei Liu 
12263b5ee9e8SAntonios Motakis     err = stat_to_qid(pdu, stbuf, &v9stat->qid);
12273b5ee9e8SAntonios Motakis     if (err < 0) {
12283b5ee9e8SAntonios Motakis         return err;
12293b5ee9e8SAntonios Motakis     }
123060ce86c7SWei Liu     v9stat->mode = stat_to_v9mode(stbuf);
123160ce86c7SWei Liu     v9stat->atime = stbuf->st_atime;
123260ce86c7SWei Liu     v9stat->mtime = stbuf->st_mtime;
123360ce86c7SWei Liu     v9stat->length = stbuf->st_size;
123460ce86c7SWei Liu 
1235abdf0086SGreg Kurz     v9fs_string_free(&v9stat->uid);
1236abdf0086SGreg Kurz     v9fs_string_free(&v9stat->gid);
1237abdf0086SGreg Kurz     v9fs_string_free(&v9stat->muid);
123860ce86c7SWei Liu 
123960ce86c7SWei Liu     v9stat->n_uid = stbuf->st_uid;
124060ce86c7SWei Liu     v9stat->n_gid = stbuf->st_gid;
124160ce86c7SWei Liu     v9stat->n_muid = 0;
124260ce86c7SWei Liu 
1243abdf0086SGreg Kurz     v9fs_string_free(&v9stat->extension);
124460ce86c7SWei Liu 
124560ce86c7SWei Liu     if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
12466069537fSJan Dakinevich         err = v9fs_co_readlink(pdu, path, &v9stat->extension);
124760ce86c7SWei Liu         if (err < 0) {
124860ce86c7SWei Liu             return err;
124960ce86c7SWei Liu         }
125060ce86c7SWei Liu     } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
125160ce86c7SWei Liu         v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
125260ce86c7SWei Liu                 S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
125360ce86c7SWei Liu                 major(stbuf->st_rdev), minor(stbuf->st_rdev));
125460ce86c7SWei Liu     } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
125560ce86c7SWei Liu         v9fs_string_sprintf(&v9stat->extension, "%s %lu",
125660ce86c7SWei Liu                 "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink);
125760ce86c7SWei Liu     }
125860ce86c7SWei Liu 
12596069537fSJan Dakinevich     v9fs_string_sprintf(&v9stat->name, "%s", basename);
126060ce86c7SWei Liu 
126160ce86c7SWei Liu     v9stat->size = 61 +
126260ce86c7SWei Liu         v9fs_string_size(&v9stat->name) +
126360ce86c7SWei Liu         v9fs_string_size(&v9stat->uid) +
126460ce86c7SWei Liu         v9fs_string_size(&v9stat->gid) +
126560ce86c7SWei Liu         v9fs_string_size(&v9stat->muid) +
126660ce86c7SWei Liu         v9fs_string_size(&v9stat->extension);
126760ce86c7SWei Liu     return 0;
126860ce86c7SWei Liu }
126960ce86c7SWei Liu 
127060ce86c7SWei Liu #define P9_STATS_MODE          0x00000001ULL
127160ce86c7SWei Liu #define P9_STATS_NLINK         0x00000002ULL
127260ce86c7SWei Liu #define P9_STATS_UID           0x00000004ULL
127360ce86c7SWei Liu #define P9_STATS_GID           0x00000008ULL
127460ce86c7SWei Liu #define P9_STATS_RDEV          0x00000010ULL
127560ce86c7SWei Liu #define P9_STATS_ATIME         0x00000020ULL
127660ce86c7SWei Liu #define P9_STATS_MTIME         0x00000040ULL
127760ce86c7SWei Liu #define P9_STATS_CTIME         0x00000080ULL
127860ce86c7SWei Liu #define P9_STATS_INO           0x00000100ULL
127960ce86c7SWei Liu #define P9_STATS_SIZE          0x00000200ULL
128060ce86c7SWei Liu #define P9_STATS_BLOCKS        0x00000400ULL
128160ce86c7SWei Liu 
128260ce86c7SWei Liu #define P9_STATS_BTIME         0x00000800ULL
128360ce86c7SWei Liu #define P9_STATS_GEN           0x00001000ULL
128460ce86c7SWei Liu #define P9_STATS_DATA_VERSION  0x00002000ULL
128560ce86c7SWei Liu 
128660ce86c7SWei Liu #define P9_STATS_BASIC         0x000007ffULL /* Mask for fields up to BLOCKS */
128760ce86c7SWei Liu #define P9_STATS_ALL           0x00003fffULL /* Mask for All fields above */
128860ce86c7SWei Liu 
128960ce86c7SWei Liu 
12903b5ee9e8SAntonios Motakis static int stat_to_v9stat_dotl(V9fsPDU *pdu, const struct stat *stbuf,
129160ce86c7SWei Liu                                 V9fsStatDotl *v9lstat)
129260ce86c7SWei Liu {
129360ce86c7SWei Liu     memset(v9lstat, 0, sizeof(*v9lstat));
129460ce86c7SWei Liu 
129560ce86c7SWei Liu     v9lstat->st_mode = stbuf->st_mode;
129660ce86c7SWei Liu     v9lstat->st_nlink = stbuf->st_nlink;
129760ce86c7SWei Liu     v9lstat->st_uid = stbuf->st_uid;
129860ce86c7SWei Liu     v9lstat->st_gid = stbuf->st_gid;
129960ce86c7SWei Liu     v9lstat->st_rdev = stbuf->st_rdev;
130060ce86c7SWei Liu     v9lstat->st_size = stbuf->st_size;
130160ce86c7SWei Liu     v9lstat->st_blksize = stbuf->st_blksize;
130260ce86c7SWei Liu     v9lstat->st_blocks = stbuf->st_blocks;
130360ce86c7SWei Liu     v9lstat->st_atime_sec = stbuf->st_atime;
130460ce86c7SWei Liu     v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
130560ce86c7SWei Liu     v9lstat->st_mtime_sec = stbuf->st_mtime;
130660ce86c7SWei Liu     v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
130760ce86c7SWei Liu     v9lstat->st_ctime_sec = stbuf->st_ctime;
130860ce86c7SWei Liu     v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
130960ce86c7SWei Liu     /* Currently we only support BASIC fields in stat */
131060ce86c7SWei Liu     v9lstat->st_result_mask = P9_STATS_BASIC;
131160ce86c7SWei Liu 
13123b5ee9e8SAntonios Motakis     return stat_to_qid(pdu, stbuf, &v9lstat->qid);
131360ce86c7SWei Liu }
131460ce86c7SWei Liu 
131560ce86c7SWei Liu static void print_sg(struct iovec *sg, int cnt)
131660ce86c7SWei Liu {
131760ce86c7SWei Liu     int i;
131860ce86c7SWei Liu 
131960ce86c7SWei Liu     printf("sg[%d]: {", cnt);
132060ce86c7SWei Liu     for (i = 0; i < cnt; i++) {
132160ce86c7SWei Liu         if (i) {
132260ce86c7SWei Liu             printf(", ");
132360ce86c7SWei Liu         }
132460ce86c7SWei Liu         printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
132560ce86c7SWei Liu     }
132660ce86c7SWei Liu     printf("}\n");
132760ce86c7SWei Liu }
132860ce86c7SWei Liu 
132960ce86c7SWei Liu /* Will call this only for path name based fid */
133060ce86c7SWei Liu static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len)
133160ce86c7SWei Liu {
133260ce86c7SWei Liu     V9fsPath str;
133360ce86c7SWei Liu     v9fs_path_init(&str);
133460ce86c7SWei Liu     v9fs_path_copy(&str, dst);
1335e3e83f2eSGreg Kurz     v9fs_path_sprintf(dst, "%s%s", src->data, str.data + len);
133660ce86c7SWei Liu     v9fs_path_free(&str);
133760ce86c7SWei Liu }
133860ce86c7SWei Liu 
133960ce86c7SWei Liu static inline bool is_ro_export(FsContext *ctx)
134060ce86c7SWei Liu {
134160ce86c7SWei Liu     return ctx->export_flags & V9FS_RDONLY;
134260ce86c7SWei Liu }
134360ce86c7SWei Liu 
13448440e22eSGreg Kurz static void coroutine_fn v9fs_version(void *opaque)
134560ce86c7SWei Liu {
134660ce86c7SWei Liu     ssize_t err;
134760ce86c7SWei Liu     V9fsPDU *pdu = opaque;
134860ce86c7SWei Liu     V9fsState *s = pdu->s;
134960ce86c7SWei Liu     V9fsString version;
135060ce86c7SWei Liu     size_t offset = 7;
135160ce86c7SWei Liu 
135260ce86c7SWei Liu     v9fs_string_init(&version);
135360ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
135460ce86c7SWei Liu     if (err < 0) {
135560ce86c7SWei Liu         goto out;
135660ce86c7SWei Liu     }
135760ce86c7SWei Liu     trace_v9fs_version(pdu->tag, pdu->id, s->msize, version.data);
135860ce86c7SWei Liu 
135960ce86c7SWei Liu     virtfs_reset(pdu);
136060ce86c7SWei Liu 
136160ce86c7SWei Liu     if (!strcmp(version.data, "9P2000.u")) {
136260ce86c7SWei Liu         s->proto_version = V9FS_PROTO_2000U;
136360ce86c7SWei Liu     } else if (!strcmp(version.data, "9P2000.L")) {
136460ce86c7SWei Liu         s->proto_version = V9FS_PROTO_2000L;
136560ce86c7SWei Liu     } else {
136660ce86c7SWei Liu         v9fs_string_sprintf(&version, "unknown");
1367e16453a3SChristian Schoenebeck         /* skip min. msize check, reporting invalid version has priority */
1368e16453a3SChristian Schoenebeck         goto marshal;
136960ce86c7SWei Liu     }
137060ce86c7SWei Liu 
1371e16453a3SChristian Schoenebeck     if (s->msize < P9_MIN_MSIZE) {
1372e16453a3SChristian Schoenebeck         err = -EMSGSIZE;
1373e16453a3SChristian Schoenebeck         error_report(
1374e16453a3SChristian Schoenebeck             "9pfs: Client requested msize < minimum msize ("
1375e16453a3SChristian Schoenebeck             stringify(P9_MIN_MSIZE) ") supported by this server."
1376e16453a3SChristian Schoenebeck         );
1377e16453a3SChristian Schoenebeck         goto out;
1378e16453a3SChristian Schoenebeck     }
1379e16453a3SChristian Schoenebeck 
1380e16453a3SChristian Schoenebeck marshal:
138160ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "ds", s->msize, &version);
138260ce86c7SWei Liu     if (err < 0) {
138360ce86c7SWei Liu         goto out;
138460ce86c7SWei Liu     }
1385403a905bSPhilippe Mathieu-Daudé     err += offset;
138660ce86c7SWei Liu     trace_v9fs_version_return(pdu->tag, pdu->id, s->msize, version.data);
138760ce86c7SWei Liu out:
1388403a905bSPhilippe Mathieu-Daudé     pdu_complete(pdu, err);
138960ce86c7SWei Liu     v9fs_string_free(&version);
139060ce86c7SWei Liu }
139160ce86c7SWei Liu 
13928440e22eSGreg Kurz static void coroutine_fn v9fs_attach(void *opaque)
139360ce86c7SWei Liu {
139460ce86c7SWei Liu     V9fsPDU *pdu = opaque;
139560ce86c7SWei Liu     V9fsState *s = pdu->s;
139660ce86c7SWei Liu     int32_t fid, afid, n_uname;
139760ce86c7SWei Liu     V9fsString uname, aname;
139860ce86c7SWei Liu     V9fsFidState *fidp;
139960ce86c7SWei Liu     size_t offset = 7;
140060ce86c7SWei Liu     V9fsQID qid;
140160ce86c7SWei Liu     ssize_t err;
140260ce86c7SWei Liu 
140360ce86c7SWei Liu     v9fs_string_init(&uname);
140460ce86c7SWei Liu     v9fs_string_init(&aname);
140560ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "ddssd", &fid,
140660ce86c7SWei Liu                         &afid, &uname, &aname, &n_uname);
140760ce86c7SWei Liu     if (err < 0) {
140860ce86c7SWei Liu         goto out_nofid;
140960ce86c7SWei Liu     }
141060ce86c7SWei Liu     trace_v9fs_attach(pdu->tag, pdu->id, fid, afid, uname.data, aname.data);
141160ce86c7SWei Liu 
141260ce86c7SWei Liu     fidp = alloc_fid(s, fid);
141360ce86c7SWei Liu     if (fidp == NULL) {
141460ce86c7SWei Liu         err = -EINVAL;
141560ce86c7SWei Liu         goto out_nofid;
141660ce86c7SWei Liu     }
141760ce86c7SWei Liu     fidp->uid = n_uname;
141860ce86c7SWei Liu     err = v9fs_co_name_to_path(pdu, NULL, "/", &fidp->path);
141960ce86c7SWei Liu     if (err < 0) {
142060ce86c7SWei Liu         err = -EINVAL;
142160ce86c7SWei Liu         clunk_fid(s, fid);
142260ce86c7SWei Liu         goto out;
142360ce86c7SWei Liu     }
142460ce86c7SWei Liu     err = fid_to_qid(pdu, fidp, &qid);
142560ce86c7SWei Liu     if (err < 0) {
142660ce86c7SWei Liu         err = -EINVAL;
142760ce86c7SWei Liu         clunk_fid(s, fid);
142860ce86c7SWei Liu         goto out;
142960ce86c7SWei Liu     }
1430fe44dc91SAshijeet Acharya 
1431fe44dc91SAshijeet Acharya     /*
1432fe44dc91SAshijeet Acharya      * disable migration if we haven't done already.
1433fe44dc91SAshijeet Acharya      * attach could get called multiple times for the same export.
1434fe44dc91SAshijeet Acharya      */
1435fe44dc91SAshijeet Acharya     if (!s->migration_blocker) {
1436fe44dc91SAshijeet Acharya         error_setg(&s->migration_blocker,
1437fe44dc91SAshijeet Acharya                    "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1438fe44dc91SAshijeet Acharya                    s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag);
14399261ef5eSMarkus Armbruster         err = migrate_add_blocker(s->migration_blocker, NULL);
14409261ef5eSMarkus Armbruster         if (err < 0) {
1441fe44dc91SAshijeet Acharya             error_free(s->migration_blocker);
1442fe44dc91SAshijeet Acharya             s->migration_blocker = NULL;
1443fe44dc91SAshijeet Acharya             clunk_fid(s, fid);
1444fe44dc91SAshijeet Acharya             goto out;
1445fe44dc91SAshijeet Acharya         }
1446fe44dc91SAshijeet Acharya         s->root_fid = fid;
1447fe44dc91SAshijeet Acharya     }
1448fe44dc91SAshijeet Acharya 
144960ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "Q", &qid);
145060ce86c7SWei Liu     if (err < 0) {
145160ce86c7SWei Liu         clunk_fid(s, fid);
145260ce86c7SWei Liu         goto out;
145360ce86c7SWei Liu     }
145460ce86c7SWei Liu     err += offset;
1455fe44dc91SAshijeet Acharya 
145656f101ecSGreg Kurz     memcpy(&s->root_qid, &qid, sizeof(qid));
145760ce86c7SWei Liu     trace_v9fs_attach_return(pdu->tag, pdu->id,
145860ce86c7SWei Liu                              qid.type, qid.version, qid.path);
145960ce86c7SWei Liu out:
146060ce86c7SWei Liu     put_fid(pdu, fidp);
146160ce86c7SWei Liu out_nofid:
146260ce86c7SWei Liu     pdu_complete(pdu, err);
146360ce86c7SWei Liu     v9fs_string_free(&uname);
146460ce86c7SWei Liu     v9fs_string_free(&aname);
146560ce86c7SWei Liu }
146660ce86c7SWei Liu 
14678440e22eSGreg Kurz static void coroutine_fn v9fs_stat(void *opaque)
146860ce86c7SWei Liu {
146960ce86c7SWei Liu     int32_t fid;
147060ce86c7SWei Liu     V9fsStat v9stat;
147160ce86c7SWei Liu     ssize_t err = 0;
147260ce86c7SWei Liu     size_t offset = 7;
147360ce86c7SWei Liu     struct stat stbuf;
147460ce86c7SWei Liu     V9fsFidState *fidp;
147560ce86c7SWei Liu     V9fsPDU *pdu = opaque;
14766069537fSJan Dakinevich     char *basename;
147760ce86c7SWei Liu 
147860ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "d", &fid);
147960ce86c7SWei Liu     if (err < 0) {
148060ce86c7SWei Liu         goto out_nofid;
148160ce86c7SWei Liu     }
148260ce86c7SWei Liu     trace_v9fs_stat(pdu->tag, pdu->id, fid);
148360ce86c7SWei Liu 
148460ce86c7SWei Liu     fidp = get_fid(pdu, fid);
148560ce86c7SWei Liu     if (fidp == NULL) {
148660ce86c7SWei Liu         err = -ENOENT;
148760ce86c7SWei Liu         goto out_nofid;
148860ce86c7SWei Liu     }
148960ce86c7SWei Liu     err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
149060ce86c7SWei Liu     if (err < 0) {
149160ce86c7SWei Liu         goto out;
149260ce86c7SWei Liu     }
14936069537fSJan Dakinevich     basename = g_path_get_basename(fidp->path.data);
14946069537fSJan Dakinevich     err = stat_to_v9stat(pdu, &fidp->path, basename, &stbuf, &v9stat);
14956069537fSJan Dakinevich     g_free(basename);
149660ce86c7SWei Liu     if (err < 0) {
149760ce86c7SWei Liu         goto out;
149860ce86c7SWei Liu     }
149960ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "wS", 0, &v9stat);
150060ce86c7SWei Liu     if (err < 0) {
150160ce86c7SWei Liu         v9fs_stat_free(&v9stat);
150260ce86c7SWei Liu         goto out;
150360ce86c7SWei Liu     }
150460ce86c7SWei Liu     trace_v9fs_stat_return(pdu->tag, pdu->id, v9stat.mode,
150560ce86c7SWei Liu                            v9stat.atime, v9stat.mtime, v9stat.length);
150660ce86c7SWei Liu     err += offset;
150760ce86c7SWei Liu     v9fs_stat_free(&v9stat);
150860ce86c7SWei Liu out:
150960ce86c7SWei Liu     put_fid(pdu, fidp);
151060ce86c7SWei Liu out_nofid:
151160ce86c7SWei Liu     pdu_complete(pdu, err);
151260ce86c7SWei Liu }
151360ce86c7SWei Liu 
15148440e22eSGreg Kurz static void coroutine_fn v9fs_getattr(void *opaque)
151560ce86c7SWei Liu {
151660ce86c7SWei Liu     int32_t fid;
151760ce86c7SWei Liu     size_t offset = 7;
151860ce86c7SWei Liu     ssize_t retval = 0;
151960ce86c7SWei Liu     struct stat stbuf;
152060ce86c7SWei Liu     V9fsFidState *fidp;
152160ce86c7SWei Liu     uint64_t request_mask;
152260ce86c7SWei Liu     V9fsStatDotl v9stat_dotl;
152360ce86c7SWei Liu     V9fsPDU *pdu = opaque;
152460ce86c7SWei Liu 
152560ce86c7SWei Liu     retval = pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask);
152660ce86c7SWei Liu     if (retval < 0) {
152760ce86c7SWei Liu         goto out_nofid;
152860ce86c7SWei Liu     }
152960ce86c7SWei Liu     trace_v9fs_getattr(pdu->tag, pdu->id, fid, request_mask);
153060ce86c7SWei Liu 
153160ce86c7SWei Liu     fidp = get_fid(pdu, fid);
153260ce86c7SWei Liu     if (fidp == NULL) {
153360ce86c7SWei Liu         retval = -ENOENT;
153460ce86c7SWei Liu         goto out_nofid;
153560ce86c7SWei Liu     }
153660ce86c7SWei Liu     /*
153760ce86c7SWei Liu      * Currently we only support BASIC fields in stat, so there is no
153860ce86c7SWei Liu      * need to look at request_mask.
153960ce86c7SWei Liu      */
154060ce86c7SWei Liu     retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
154160ce86c7SWei Liu     if (retval < 0) {
154260ce86c7SWei Liu         goto out;
154360ce86c7SWei Liu     }
15443b5ee9e8SAntonios Motakis     retval = stat_to_v9stat_dotl(pdu, &stbuf, &v9stat_dotl);
15453b5ee9e8SAntonios Motakis     if (retval < 0) {
15463b5ee9e8SAntonios Motakis         goto out;
15473b5ee9e8SAntonios Motakis     }
154860ce86c7SWei Liu 
154960ce86c7SWei Liu     /*  fill st_gen if requested and supported by underlying fs */
155060ce86c7SWei Liu     if (request_mask & P9_STATS_GEN) {
155160ce86c7SWei Liu         retval = v9fs_co_st_gen(pdu, &fidp->path, stbuf.st_mode, &v9stat_dotl);
155260ce86c7SWei Liu         switch (retval) {
155360ce86c7SWei Liu         case 0:
155460ce86c7SWei Liu             /* we have valid st_gen: update result mask */
155560ce86c7SWei Liu             v9stat_dotl.st_result_mask |= P9_STATS_GEN;
155660ce86c7SWei Liu             break;
155760ce86c7SWei Liu         case -EINTR:
155860ce86c7SWei Liu             /* request cancelled, e.g. by Tflush */
155960ce86c7SWei Liu             goto out;
156060ce86c7SWei Liu         default:
156160ce86c7SWei Liu             /* failed to get st_gen: not fatal, ignore */
156260ce86c7SWei Liu             break;
156360ce86c7SWei Liu         }
156460ce86c7SWei Liu     }
156560ce86c7SWei Liu     retval = pdu_marshal(pdu, offset, "A", &v9stat_dotl);
156660ce86c7SWei Liu     if (retval < 0) {
156760ce86c7SWei Liu         goto out;
156860ce86c7SWei Liu     }
156960ce86c7SWei Liu     retval += offset;
157060ce86c7SWei Liu     trace_v9fs_getattr_return(pdu->tag, pdu->id, v9stat_dotl.st_result_mask,
157160ce86c7SWei Liu                               v9stat_dotl.st_mode, v9stat_dotl.st_uid,
157260ce86c7SWei Liu                               v9stat_dotl.st_gid);
157360ce86c7SWei Liu out:
157460ce86c7SWei Liu     put_fid(pdu, fidp);
157560ce86c7SWei Liu out_nofid:
157660ce86c7SWei Liu     pdu_complete(pdu, retval);
157760ce86c7SWei Liu }
157860ce86c7SWei Liu 
157960ce86c7SWei Liu /* Attribute flags */
158060ce86c7SWei Liu #define P9_ATTR_MODE       (1 << 0)
158160ce86c7SWei Liu #define P9_ATTR_UID        (1 << 1)
158260ce86c7SWei Liu #define P9_ATTR_GID        (1 << 2)
158360ce86c7SWei Liu #define P9_ATTR_SIZE       (1 << 3)
158460ce86c7SWei Liu #define P9_ATTR_ATIME      (1 << 4)
158560ce86c7SWei Liu #define P9_ATTR_MTIME      (1 << 5)
158660ce86c7SWei Liu #define P9_ATTR_CTIME      (1 << 6)
158760ce86c7SWei Liu #define P9_ATTR_ATIME_SET  (1 << 7)
158860ce86c7SWei Liu #define P9_ATTR_MTIME_SET  (1 << 8)
158960ce86c7SWei Liu 
159060ce86c7SWei Liu #define P9_ATTR_MASK    127
159160ce86c7SWei Liu 
15928440e22eSGreg Kurz static void coroutine_fn v9fs_setattr(void *opaque)
159360ce86c7SWei Liu {
159460ce86c7SWei Liu     int err = 0;
159560ce86c7SWei Liu     int32_t fid;
159660ce86c7SWei Liu     V9fsFidState *fidp;
159760ce86c7SWei Liu     size_t offset = 7;
159860ce86c7SWei Liu     V9fsIattr v9iattr;
159960ce86c7SWei Liu     V9fsPDU *pdu = opaque;
160060ce86c7SWei Liu 
160160ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr);
160260ce86c7SWei Liu     if (err < 0) {
160360ce86c7SWei Liu         goto out_nofid;
160460ce86c7SWei Liu     }
160560ce86c7SWei Liu 
16068f9c64bfSGreg Kurz     trace_v9fs_setattr(pdu->tag, pdu->id, fid,
16078f9c64bfSGreg Kurz                        v9iattr.valid, v9iattr.mode, v9iattr.uid, v9iattr.gid,
16088f9c64bfSGreg Kurz                        v9iattr.size, v9iattr.atime_sec, v9iattr.mtime_sec);
16098f9c64bfSGreg Kurz 
161060ce86c7SWei Liu     fidp = get_fid(pdu, fid);
161160ce86c7SWei Liu     if (fidp == NULL) {
161260ce86c7SWei Liu         err = -EINVAL;
161360ce86c7SWei Liu         goto out_nofid;
161460ce86c7SWei Liu     }
161560ce86c7SWei Liu     if (v9iattr.valid & P9_ATTR_MODE) {
161660ce86c7SWei Liu         err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode);
161760ce86c7SWei Liu         if (err < 0) {
161860ce86c7SWei Liu             goto out;
161960ce86c7SWei Liu         }
162060ce86c7SWei Liu     }
162160ce86c7SWei Liu     if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) {
162260ce86c7SWei Liu         struct timespec times[2];
162360ce86c7SWei Liu         if (v9iattr.valid & P9_ATTR_ATIME) {
162460ce86c7SWei Liu             if (v9iattr.valid & P9_ATTR_ATIME_SET) {
162560ce86c7SWei Liu                 times[0].tv_sec = v9iattr.atime_sec;
162660ce86c7SWei Liu                 times[0].tv_nsec = v9iattr.atime_nsec;
162760ce86c7SWei Liu             } else {
162860ce86c7SWei Liu                 times[0].tv_nsec = UTIME_NOW;
162960ce86c7SWei Liu             }
163060ce86c7SWei Liu         } else {
163160ce86c7SWei Liu             times[0].tv_nsec = UTIME_OMIT;
163260ce86c7SWei Liu         }
163360ce86c7SWei Liu         if (v9iattr.valid & P9_ATTR_MTIME) {
163460ce86c7SWei Liu             if (v9iattr.valid & P9_ATTR_MTIME_SET) {
163560ce86c7SWei Liu                 times[1].tv_sec = v9iattr.mtime_sec;
163660ce86c7SWei Liu                 times[1].tv_nsec = v9iattr.mtime_nsec;
163760ce86c7SWei Liu             } else {
163860ce86c7SWei Liu                 times[1].tv_nsec = UTIME_NOW;
163960ce86c7SWei Liu             }
164060ce86c7SWei Liu         } else {
164160ce86c7SWei Liu             times[1].tv_nsec = UTIME_OMIT;
164260ce86c7SWei Liu         }
164360ce86c7SWei Liu         err = v9fs_co_utimensat(pdu, &fidp->path, times);
164460ce86c7SWei Liu         if (err < 0) {
164560ce86c7SWei Liu             goto out;
164660ce86c7SWei Liu         }
164760ce86c7SWei Liu     }
164860ce86c7SWei Liu     /*
164960ce86c7SWei Liu      * If the only valid entry in iattr is ctime we can call
165060ce86c7SWei Liu      * chown(-1,-1) to update the ctime of the file
165160ce86c7SWei Liu      */
165260ce86c7SWei Liu     if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) ||
165360ce86c7SWei Liu         ((v9iattr.valid & P9_ATTR_CTIME)
165460ce86c7SWei Liu          && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) {
165560ce86c7SWei Liu         if (!(v9iattr.valid & P9_ATTR_UID)) {
165660ce86c7SWei Liu             v9iattr.uid = -1;
165760ce86c7SWei Liu         }
165860ce86c7SWei Liu         if (!(v9iattr.valid & P9_ATTR_GID)) {
165960ce86c7SWei Liu             v9iattr.gid = -1;
166060ce86c7SWei Liu         }
166160ce86c7SWei Liu         err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid,
166260ce86c7SWei Liu                             v9iattr.gid);
166360ce86c7SWei Liu         if (err < 0) {
166460ce86c7SWei Liu             goto out;
166560ce86c7SWei Liu         }
166660ce86c7SWei Liu     }
166760ce86c7SWei Liu     if (v9iattr.valid & (P9_ATTR_SIZE)) {
166860ce86c7SWei Liu         err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size);
166960ce86c7SWei Liu         if (err < 0) {
167060ce86c7SWei Liu             goto out;
167160ce86c7SWei Liu         }
167260ce86c7SWei Liu     }
167360ce86c7SWei Liu     err = offset;
16748f9c64bfSGreg Kurz     trace_v9fs_setattr_return(pdu->tag, pdu->id);
167560ce86c7SWei Liu out:
167660ce86c7SWei Liu     put_fid(pdu, fidp);
167760ce86c7SWei Liu out_nofid:
167860ce86c7SWei Liu     pdu_complete(pdu, err);
167960ce86c7SWei Liu }
168060ce86c7SWei Liu 
168160ce86c7SWei Liu static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids)
168260ce86c7SWei Liu {
168360ce86c7SWei Liu     int i;
168460ce86c7SWei Liu     ssize_t err;
168560ce86c7SWei Liu     size_t offset = 7;
168660ce86c7SWei Liu 
168760ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "w", nwnames);
168860ce86c7SWei Liu     if (err < 0) {
168960ce86c7SWei Liu         return err;
169060ce86c7SWei Liu     }
169160ce86c7SWei Liu     offset += err;
169260ce86c7SWei Liu     for (i = 0; i < nwnames; i++) {
169360ce86c7SWei Liu         err = pdu_marshal(pdu, offset, "Q", &qids[i]);
169460ce86c7SWei Liu         if (err < 0) {
169560ce86c7SWei Liu             return err;
169660ce86c7SWei Liu         }
169760ce86c7SWei Liu         offset += err;
169860ce86c7SWei Liu     }
169960ce86c7SWei Liu     return offset;
170060ce86c7SWei Liu }
170160ce86c7SWei Liu 
1702fff39a7aSGreg Kurz static bool name_is_illegal(const char *name)
1703fff39a7aSGreg Kurz {
1704fff39a7aSGreg Kurz     return !*name || strchr(name, '/') != NULL;
1705fff39a7aSGreg Kurz }
1706fff39a7aSGreg Kurz 
170756f101ecSGreg Kurz static bool not_same_qid(const V9fsQID *qid1, const V9fsQID *qid2)
170856f101ecSGreg Kurz {
170956f101ecSGreg Kurz     return
171056f101ecSGreg Kurz         qid1->type != qid2->type ||
171156f101ecSGreg Kurz         qid1->version != qid2->version ||
171256f101ecSGreg Kurz         qid1->path != qid2->path;
171356f101ecSGreg Kurz }
171456f101ecSGreg Kurz 
17158440e22eSGreg Kurz static void coroutine_fn v9fs_walk(void *opaque)
171660ce86c7SWei Liu {
171760ce86c7SWei Liu     int name_idx;
171860ce86c7SWei Liu     V9fsQID *qids = NULL;
171960ce86c7SWei Liu     int i, err = 0;
172060ce86c7SWei Liu     V9fsPath dpath, path;
172160ce86c7SWei Liu     uint16_t nwnames;
172260ce86c7SWei Liu     struct stat stbuf;
172360ce86c7SWei Liu     size_t offset = 7;
172460ce86c7SWei Liu     int32_t fid, newfid;
172560ce86c7SWei Liu     V9fsString *wnames = NULL;
172660ce86c7SWei Liu     V9fsFidState *fidp;
172760ce86c7SWei Liu     V9fsFidState *newfidp = NULL;
172860ce86c7SWei Liu     V9fsPDU *pdu = opaque;
172960ce86c7SWei Liu     V9fsState *s = pdu->s;
173056f101ecSGreg Kurz     V9fsQID qid;
173160ce86c7SWei Liu 
173260ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames);
173360ce86c7SWei Liu     if (err < 0) {
173460ce86c7SWei Liu         pdu_complete(pdu, err);
173560ce86c7SWei Liu         return ;
173660ce86c7SWei Liu     }
173760ce86c7SWei Liu     offset += err;
173860ce86c7SWei Liu 
173960ce86c7SWei Liu     trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames);
174060ce86c7SWei Liu 
174160ce86c7SWei Liu     if (nwnames && nwnames <= P9_MAXWELEM) {
17421923923bSGreg Kurz         wnames = g_new0(V9fsString, nwnames);
17431923923bSGreg Kurz         qids   = g_new0(V9fsQID, nwnames);
174460ce86c7SWei Liu         for (i = 0; i < nwnames; i++) {
174560ce86c7SWei Liu             err = pdu_unmarshal(pdu, offset, "s", &wnames[i]);
174660ce86c7SWei Liu             if (err < 0) {
174760ce86c7SWei Liu                 goto out_nofid;
174860ce86c7SWei Liu             }
1749fff39a7aSGreg Kurz             if (name_is_illegal(wnames[i].data)) {
1750fff39a7aSGreg Kurz                 err = -ENOENT;
1751fff39a7aSGreg Kurz                 goto out_nofid;
1752fff39a7aSGreg Kurz             }
175360ce86c7SWei Liu             offset += err;
175460ce86c7SWei Liu         }
175560ce86c7SWei Liu     } else if (nwnames > P9_MAXWELEM) {
175660ce86c7SWei Liu         err = -EINVAL;
175760ce86c7SWei Liu         goto out_nofid;
175860ce86c7SWei Liu     }
175960ce86c7SWei Liu     fidp = get_fid(pdu, fid);
176060ce86c7SWei Liu     if (fidp == NULL) {
176160ce86c7SWei Liu         err = -ENOENT;
176260ce86c7SWei Liu         goto out_nofid;
176360ce86c7SWei Liu     }
176456f101ecSGreg Kurz 
176513fd08e6SGreg Kurz     v9fs_path_init(&dpath);
176613fd08e6SGreg Kurz     v9fs_path_init(&path);
176713fd08e6SGreg Kurz 
176856f101ecSGreg Kurz     err = fid_to_qid(pdu, fidp, &qid);
176956f101ecSGreg Kurz     if (err < 0) {
177056f101ecSGreg Kurz         goto out;
177156f101ecSGreg Kurz     }
177256f101ecSGreg Kurz 
177360ce86c7SWei Liu     /*
177460ce86c7SWei Liu      * Both dpath and path initially poin to fidp.
177560ce86c7SWei Liu      * Needed to handle request with nwnames == 0
177660ce86c7SWei Liu      */
177760ce86c7SWei Liu     v9fs_path_copy(&dpath, &fidp->path);
177860ce86c7SWei Liu     v9fs_path_copy(&path, &fidp->path);
177960ce86c7SWei Liu     for (name_idx = 0; name_idx < nwnames; name_idx++) {
178056f101ecSGreg Kurz         if (not_same_qid(&pdu->s->root_qid, &qid) ||
178156f101ecSGreg Kurz             strcmp("..", wnames[name_idx].data)) {
178256f101ecSGreg Kurz             err = v9fs_co_name_to_path(pdu, &dpath, wnames[name_idx].data,
178356f101ecSGreg Kurz                                        &path);
178460ce86c7SWei Liu             if (err < 0) {
178560ce86c7SWei Liu                 goto out;
178660ce86c7SWei Liu             }
178756f101ecSGreg Kurz 
178860ce86c7SWei Liu             err = v9fs_co_lstat(pdu, &path, &stbuf);
178960ce86c7SWei Liu             if (err < 0) {
179060ce86c7SWei Liu                 goto out;
179160ce86c7SWei Liu             }
17923b5ee9e8SAntonios Motakis             err = stat_to_qid(pdu, &stbuf, &qid);
17933b5ee9e8SAntonios Motakis             if (err < 0) {
17943b5ee9e8SAntonios Motakis                 goto out;
17953b5ee9e8SAntonios Motakis             }
179660ce86c7SWei Liu             v9fs_path_copy(&dpath, &path);
179760ce86c7SWei Liu         }
179856f101ecSGreg Kurz         memcpy(&qids[name_idx], &qid, sizeof(qid));
179956f101ecSGreg Kurz     }
180060ce86c7SWei Liu     if (fid == newfid) {
180149dd946bSGreg Kurz         if (fidp->fid_type != P9_FID_NONE) {
180249dd946bSGreg Kurz             err = -EINVAL;
180349dd946bSGreg Kurz             goto out;
180449dd946bSGreg Kurz         }
18055b3c77aaSGreg Kurz         v9fs_path_write_lock(s);
180660ce86c7SWei Liu         v9fs_path_copy(&fidp->path, &path);
18075b3c77aaSGreg Kurz         v9fs_path_unlock(s);
180860ce86c7SWei Liu     } else {
180960ce86c7SWei Liu         newfidp = alloc_fid(s, newfid);
181060ce86c7SWei Liu         if (newfidp == NULL) {
181160ce86c7SWei Liu             err = -EINVAL;
181260ce86c7SWei Liu             goto out;
181360ce86c7SWei Liu         }
181460ce86c7SWei Liu         newfidp->uid = fidp->uid;
181560ce86c7SWei Liu         v9fs_path_copy(&newfidp->path, &path);
181660ce86c7SWei Liu     }
181760ce86c7SWei Liu     err = v9fs_walk_marshal(pdu, nwnames, qids);
181860ce86c7SWei Liu     trace_v9fs_walk_return(pdu->tag, pdu->id, nwnames, qids);
181960ce86c7SWei Liu out:
182060ce86c7SWei Liu     put_fid(pdu, fidp);
182160ce86c7SWei Liu     if (newfidp) {
182260ce86c7SWei Liu         put_fid(pdu, newfidp);
182360ce86c7SWei Liu     }
182460ce86c7SWei Liu     v9fs_path_free(&dpath);
182560ce86c7SWei Liu     v9fs_path_free(&path);
182660ce86c7SWei Liu out_nofid:
182760ce86c7SWei Liu     pdu_complete(pdu, err);
182860ce86c7SWei Liu     if (nwnames && nwnames <= P9_MAXWELEM) {
182960ce86c7SWei Liu         for (name_idx = 0; name_idx < nwnames; name_idx++) {
183060ce86c7SWei Liu             v9fs_string_free(&wnames[name_idx]);
183160ce86c7SWei Liu         }
183260ce86c7SWei Liu         g_free(wnames);
183360ce86c7SWei Liu         g_free(qids);
183460ce86c7SWei Liu     }
183560ce86c7SWei Liu }
183660ce86c7SWei Liu 
18378440e22eSGreg Kurz static int32_t coroutine_fn get_iounit(V9fsPDU *pdu, V9fsPath *path)
183860ce86c7SWei Liu {
183960ce86c7SWei Liu     struct statfs stbuf;
184060ce86c7SWei Liu     int32_t iounit = 0;
184160ce86c7SWei Liu     V9fsState *s = pdu->s;
184260ce86c7SWei Liu 
184360ce86c7SWei Liu     /*
184460ce86c7SWei Liu      * iounit should be multiples of f_bsize (host filesystem block size
184560ce86c7SWei Liu      * and as well as less than (client msize - P9_IOHDRSZ))
184660ce86c7SWei Liu      */
184760ce86c7SWei Liu     if (!v9fs_co_statfs(pdu, path, &stbuf)) {
184868d654daSDan Schatzberg         if (stbuf.f_bsize) {
184960ce86c7SWei Liu             iounit = stbuf.f_bsize;
185060ce86c7SWei Liu             iounit *= (s->msize - P9_IOHDRSZ) / stbuf.f_bsize;
185160ce86c7SWei Liu         }
185268d654daSDan Schatzberg     }
185360ce86c7SWei Liu     if (!iounit) {
185460ce86c7SWei Liu         iounit = s->msize - P9_IOHDRSZ;
185560ce86c7SWei Liu     }
185660ce86c7SWei Liu     return iounit;
185760ce86c7SWei Liu }
185860ce86c7SWei Liu 
18598440e22eSGreg Kurz static void coroutine_fn v9fs_open(void *opaque)
186060ce86c7SWei Liu {
186160ce86c7SWei Liu     int flags;
186260ce86c7SWei Liu     int32_t fid;
186360ce86c7SWei Liu     int32_t mode;
186460ce86c7SWei Liu     V9fsQID qid;
186560ce86c7SWei Liu     int iounit = 0;
186660ce86c7SWei Liu     ssize_t err = 0;
186760ce86c7SWei Liu     size_t offset = 7;
186860ce86c7SWei Liu     struct stat stbuf;
186960ce86c7SWei Liu     V9fsFidState *fidp;
187060ce86c7SWei Liu     V9fsPDU *pdu = opaque;
187160ce86c7SWei Liu     V9fsState *s = pdu->s;
187260ce86c7SWei Liu 
187360ce86c7SWei Liu     if (s->proto_version == V9FS_PROTO_2000L) {
187460ce86c7SWei Liu         err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode);
187560ce86c7SWei Liu     } else {
187660ce86c7SWei Liu         uint8_t modebyte;
187760ce86c7SWei Liu         err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte);
187860ce86c7SWei Liu         mode = modebyte;
187960ce86c7SWei Liu     }
188060ce86c7SWei Liu     if (err < 0) {
188160ce86c7SWei Liu         goto out_nofid;
188260ce86c7SWei Liu     }
188360ce86c7SWei Liu     trace_v9fs_open(pdu->tag, pdu->id, fid, mode);
188460ce86c7SWei Liu 
188560ce86c7SWei Liu     fidp = get_fid(pdu, fid);
188660ce86c7SWei Liu     if (fidp == NULL) {
188760ce86c7SWei Liu         err = -ENOENT;
188860ce86c7SWei Liu         goto out_nofid;
188960ce86c7SWei Liu     }
189049dd946bSGreg Kurz     if (fidp->fid_type != P9_FID_NONE) {
189149dd946bSGreg Kurz         err = -EINVAL;
189249dd946bSGreg Kurz         goto out;
189349dd946bSGreg Kurz     }
189460ce86c7SWei Liu 
189560ce86c7SWei Liu     err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
189660ce86c7SWei Liu     if (err < 0) {
189760ce86c7SWei Liu         goto out;
189860ce86c7SWei Liu     }
18993b5ee9e8SAntonios Motakis     err = stat_to_qid(pdu, &stbuf, &qid);
19003b5ee9e8SAntonios Motakis     if (err < 0) {
19013b5ee9e8SAntonios Motakis         goto out;
19023b5ee9e8SAntonios Motakis     }
190360ce86c7SWei Liu     if (S_ISDIR(stbuf.st_mode)) {
190460ce86c7SWei Liu         err = v9fs_co_opendir(pdu, fidp);
190560ce86c7SWei Liu         if (err < 0) {
190660ce86c7SWei Liu             goto out;
190760ce86c7SWei Liu         }
190860ce86c7SWei Liu         fidp->fid_type = P9_FID_DIR;
190960ce86c7SWei Liu         err = pdu_marshal(pdu, offset, "Qd", &qid, 0);
191060ce86c7SWei Liu         if (err < 0) {
191160ce86c7SWei Liu             goto out;
191260ce86c7SWei Liu         }
191360ce86c7SWei Liu         err += offset;
191460ce86c7SWei Liu     } else {
191560ce86c7SWei Liu         if (s->proto_version == V9FS_PROTO_2000L) {
191660ce86c7SWei Liu             flags = get_dotl_openflags(s, mode);
191760ce86c7SWei Liu         } else {
191860ce86c7SWei Liu             flags = omode_to_uflags(mode);
191960ce86c7SWei Liu         }
192060ce86c7SWei Liu         if (is_ro_export(&s->ctx)) {
192160ce86c7SWei Liu             if (mode & O_WRONLY || mode & O_RDWR ||
192260ce86c7SWei Liu                 mode & O_APPEND || mode & O_TRUNC) {
192360ce86c7SWei Liu                 err = -EROFS;
192460ce86c7SWei Liu                 goto out;
192560ce86c7SWei Liu             }
192660ce86c7SWei Liu         }
192760ce86c7SWei Liu         err = v9fs_co_open(pdu, fidp, flags);
192860ce86c7SWei Liu         if (err < 0) {
192960ce86c7SWei Liu             goto out;
193060ce86c7SWei Liu         }
193160ce86c7SWei Liu         fidp->fid_type = P9_FID_FILE;
193260ce86c7SWei Liu         fidp->open_flags = flags;
193360ce86c7SWei Liu         if (flags & O_EXCL) {
193460ce86c7SWei Liu             /*
193560ce86c7SWei Liu              * We let the host file system do O_EXCL check
193660ce86c7SWei Liu              * We should not reclaim such fd
193760ce86c7SWei Liu              */
193860ce86c7SWei Liu             fidp->flags |= FID_NON_RECLAIMABLE;
193960ce86c7SWei Liu         }
194060ce86c7SWei Liu         iounit = get_iounit(pdu, &fidp->path);
194160ce86c7SWei Liu         err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
194260ce86c7SWei Liu         if (err < 0) {
194360ce86c7SWei Liu             goto out;
194460ce86c7SWei Liu         }
194560ce86c7SWei Liu         err += offset;
194660ce86c7SWei Liu     }
194760ce86c7SWei Liu     trace_v9fs_open_return(pdu->tag, pdu->id,
194860ce86c7SWei Liu                            qid.type, qid.version, qid.path, iounit);
194960ce86c7SWei Liu out:
195060ce86c7SWei Liu     put_fid(pdu, fidp);
195160ce86c7SWei Liu out_nofid:
195260ce86c7SWei Liu     pdu_complete(pdu, err);
195360ce86c7SWei Liu }
195460ce86c7SWei Liu 
19558440e22eSGreg Kurz static void coroutine_fn v9fs_lcreate(void *opaque)
195660ce86c7SWei Liu {
195760ce86c7SWei Liu     int32_t dfid, flags, mode;
195860ce86c7SWei Liu     gid_t gid;
195960ce86c7SWei Liu     ssize_t err = 0;
196060ce86c7SWei Liu     ssize_t offset = 7;
196160ce86c7SWei Liu     V9fsString name;
196260ce86c7SWei Liu     V9fsFidState *fidp;
196360ce86c7SWei Liu     struct stat stbuf;
196460ce86c7SWei Liu     V9fsQID qid;
196560ce86c7SWei Liu     int32_t iounit;
196660ce86c7SWei Liu     V9fsPDU *pdu = opaque;
196760ce86c7SWei Liu 
196860ce86c7SWei Liu     v9fs_string_init(&name);
196960ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dsddd", &dfid,
197060ce86c7SWei Liu                         &name, &flags, &mode, &gid);
197160ce86c7SWei Liu     if (err < 0) {
197260ce86c7SWei Liu         goto out_nofid;
197360ce86c7SWei Liu     }
197460ce86c7SWei Liu     trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid);
197560ce86c7SWei Liu 
1976fff39a7aSGreg Kurz     if (name_is_illegal(name.data)) {
1977fff39a7aSGreg Kurz         err = -ENOENT;
1978fff39a7aSGreg Kurz         goto out_nofid;
1979fff39a7aSGreg Kurz     }
1980fff39a7aSGreg Kurz 
1981805b5d98SGreg Kurz     if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
1982805b5d98SGreg Kurz         err = -EEXIST;
1983805b5d98SGreg Kurz         goto out_nofid;
1984805b5d98SGreg Kurz     }
1985805b5d98SGreg Kurz 
198660ce86c7SWei Liu     fidp = get_fid(pdu, dfid);
198760ce86c7SWei Liu     if (fidp == NULL) {
198860ce86c7SWei Liu         err = -ENOENT;
198960ce86c7SWei Liu         goto out_nofid;
199060ce86c7SWei Liu     }
1991d63fb193SLi Qiang     if (fidp->fid_type != P9_FID_NONE) {
1992d63fb193SLi Qiang         err = -EINVAL;
1993d63fb193SLi Qiang         goto out;
1994d63fb193SLi Qiang     }
199560ce86c7SWei Liu 
199660ce86c7SWei Liu     flags = get_dotl_openflags(pdu->s, flags);
199760ce86c7SWei Liu     err = v9fs_co_open2(pdu, fidp, &name, gid,
199860ce86c7SWei Liu                         flags | O_CREAT, mode, &stbuf);
199960ce86c7SWei Liu     if (err < 0) {
200060ce86c7SWei Liu         goto out;
200160ce86c7SWei Liu     }
200260ce86c7SWei Liu     fidp->fid_type = P9_FID_FILE;
200360ce86c7SWei Liu     fidp->open_flags = flags;
200460ce86c7SWei Liu     if (flags & O_EXCL) {
200560ce86c7SWei Liu         /*
200660ce86c7SWei Liu          * We let the host file system do O_EXCL check
200760ce86c7SWei Liu          * We should not reclaim such fd
200860ce86c7SWei Liu          */
200960ce86c7SWei Liu         fidp->flags |= FID_NON_RECLAIMABLE;
201060ce86c7SWei Liu     }
201160ce86c7SWei Liu     iounit =  get_iounit(pdu, &fidp->path);
20123b5ee9e8SAntonios Motakis     err = stat_to_qid(pdu, &stbuf, &qid);
20133b5ee9e8SAntonios Motakis     if (err < 0) {
20143b5ee9e8SAntonios Motakis         goto out;
20153b5ee9e8SAntonios Motakis     }
201660ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
201760ce86c7SWei Liu     if (err < 0) {
201860ce86c7SWei Liu         goto out;
201960ce86c7SWei Liu     }
202060ce86c7SWei Liu     err += offset;
202160ce86c7SWei Liu     trace_v9fs_lcreate_return(pdu->tag, pdu->id,
202260ce86c7SWei Liu                               qid.type, qid.version, qid.path, iounit);
202360ce86c7SWei Liu out:
202460ce86c7SWei Liu     put_fid(pdu, fidp);
202560ce86c7SWei Liu out_nofid:
202660ce86c7SWei Liu     pdu_complete(pdu, err);
202760ce86c7SWei Liu     v9fs_string_free(&name);
202860ce86c7SWei Liu }
202960ce86c7SWei Liu 
2030a1bf8b74SGreg Kurz static void coroutine_fn v9fs_fsync(void *opaque)
203160ce86c7SWei Liu {
203260ce86c7SWei Liu     int err;
203360ce86c7SWei Liu     int32_t fid;
203460ce86c7SWei Liu     int datasync;
203560ce86c7SWei Liu     size_t offset = 7;
203660ce86c7SWei Liu     V9fsFidState *fidp;
203760ce86c7SWei Liu     V9fsPDU *pdu = opaque;
203860ce86c7SWei Liu 
203960ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
204060ce86c7SWei Liu     if (err < 0) {
204160ce86c7SWei Liu         goto out_nofid;
204260ce86c7SWei Liu     }
204360ce86c7SWei Liu     trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync);
204460ce86c7SWei Liu 
204560ce86c7SWei Liu     fidp = get_fid(pdu, fid);
204660ce86c7SWei Liu     if (fidp == NULL) {
204760ce86c7SWei Liu         err = -ENOENT;
204860ce86c7SWei Liu         goto out_nofid;
204960ce86c7SWei Liu     }
205060ce86c7SWei Liu     err = v9fs_co_fsync(pdu, fidp, datasync);
205160ce86c7SWei Liu     if (!err) {
205260ce86c7SWei Liu         err = offset;
205360ce86c7SWei Liu     }
205460ce86c7SWei Liu     put_fid(pdu, fidp);
205560ce86c7SWei Liu out_nofid:
205660ce86c7SWei Liu     pdu_complete(pdu, err);
205760ce86c7SWei Liu }
205860ce86c7SWei Liu 
20598440e22eSGreg Kurz static void coroutine_fn v9fs_clunk(void *opaque)
206060ce86c7SWei Liu {
206160ce86c7SWei Liu     int err;
206260ce86c7SWei Liu     int32_t fid;
206360ce86c7SWei Liu     size_t offset = 7;
206460ce86c7SWei Liu     V9fsFidState *fidp;
206560ce86c7SWei Liu     V9fsPDU *pdu = opaque;
206660ce86c7SWei Liu     V9fsState *s = pdu->s;
206760ce86c7SWei Liu 
206860ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "d", &fid);
206960ce86c7SWei Liu     if (err < 0) {
207060ce86c7SWei Liu         goto out_nofid;
207160ce86c7SWei Liu     }
207260ce86c7SWei Liu     trace_v9fs_clunk(pdu->tag, pdu->id, fid);
207360ce86c7SWei Liu 
207460ce86c7SWei Liu     fidp = clunk_fid(s, fid);
207560ce86c7SWei Liu     if (fidp == NULL) {
207660ce86c7SWei Liu         err = -ENOENT;
207760ce86c7SWei Liu         goto out_nofid;
207860ce86c7SWei Liu     }
207960ce86c7SWei Liu     /*
208060ce86c7SWei Liu      * Bump the ref so that put_fid will
208160ce86c7SWei Liu      * free the fid.
208260ce86c7SWei Liu      */
208360ce86c7SWei Liu     fidp->ref++;
208460ce86c7SWei Liu     err = put_fid(pdu, fidp);
208560ce86c7SWei Liu     if (!err) {
208660ce86c7SWei Liu         err = offset;
208760ce86c7SWei Liu     }
208860ce86c7SWei Liu out_nofid:
208960ce86c7SWei Liu     pdu_complete(pdu, err);
209060ce86c7SWei Liu }
209160ce86c7SWei Liu 
2092bcb8998fSStefano Stabellini /*
2093bcb8998fSStefano Stabellini  * Create a QEMUIOVector for a sub-region of PDU iovecs
2094bcb8998fSStefano Stabellini  *
2095bcb8998fSStefano Stabellini  * @qiov:       uninitialized QEMUIOVector
2096bcb8998fSStefano Stabellini  * @skip:       number of bytes to skip from beginning of PDU
2097bcb8998fSStefano Stabellini  * @size:       number of bytes to include
2098bcb8998fSStefano Stabellini  * @is_write:   true - write, false - read
2099bcb8998fSStefano Stabellini  *
2100bcb8998fSStefano Stabellini  * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
2101bcb8998fSStefano Stabellini  * with qemu_iovec_destroy().
2102bcb8998fSStefano Stabellini  */
2103bcb8998fSStefano Stabellini static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu,
2104cf45183bSStefano Stabellini                                     size_t skip, size_t size,
2105bcb8998fSStefano Stabellini                                     bool is_write)
2106bcb8998fSStefano Stabellini {
2107bcb8998fSStefano Stabellini     QEMUIOVector elem;
2108bcb8998fSStefano Stabellini     struct iovec *iov;
2109bcb8998fSStefano Stabellini     unsigned int niov;
2110bcb8998fSStefano Stabellini 
211188da0b03SStefano Stabellini     if (is_write) {
2112cf45183bSStefano Stabellini         pdu->s->transport->init_out_iov_from_pdu(pdu, &iov, &niov, size + skip);
211388da0b03SStefano Stabellini     } else {
2114cf45183bSStefano Stabellini         pdu->s->transport->init_in_iov_from_pdu(pdu, &iov, &niov, size + skip);
211588da0b03SStefano Stabellini     }
2116bcb8998fSStefano Stabellini 
2117bcb8998fSStefano Stabellini     qemu_iovec_init_external(&elem, iov, niov);
2118bcb8998fSStefano Stabellini     qemu_iovec_init(qiov, niov);
2119cf45183bSStefano Stabellini     qemu_iovec_concat(qiov, &elem, skip, size);
2120bcb8998fSStefano Stabellini }
2121bcb8998fSStefano Stabellini 
212260ce86c7SWei Liu static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
212360ce86c7SWei Liu                            uint64_t off, uint32_t max_count)
212460ce86c7SWei Liu {
212560ce86c7SWei Liu     ssize_t err;
212660ce86c7SWei Liu     size_t offset = 7;
2127cf45183bSStefano Stabellini     uint64_t read_count;
2128bcb8998fSStefano Stabellini     QEMUIOVector qiov_full;
212960ce86c7SWei Liu 
21307e55d65cSLi Qiang     if (fidp->fs.xattr.len < off) {
21317e55d65cSLi Qiang         read_count = 0;
213216724a17SGreg Kurz     } else {
2133cf45183bSStefano Stabellini         read_count = fidp->fs.xattr.len - off;
2134cf45183bSStefano Stabellini     }
2135cf45183bSStefano Stabellini     if (read_count > max_count) {
213660ce86c7SWei Liu         read_count = max_count;
213760ce86c7SWei Liu     }
213860ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "d", read_count);
213960ce86c7SWei Liu     if (err < 0) {
214060ce86c7SWei Liu         return err;
214160ce86c7SWei Liu     }
214260ce86c7SWei Liu     offset += err;
214300588a0aSWei Liu 
2144cf45183bSStefano Stabellini     v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, read_count, false);
2145fa0eb5c5SGreg Kurz     err = v9fs_pack(qiov_full.iov, qiov_full.niov, 0,
214660ce86c7SWei Liu                     ((char *)fidp->fs.xattr.value) + off,
214760ce86c7SWei Liu                     read_count);
2148bcb8998fSStefano Stabellini     qemu_iovec_destroy(&qiov_full);
214960ce86c7SWei Liu     if (err < 0) {
215060ce86c7SWei Liu         return err;
215160ce86c7SWei Liu     }
215260ce86c7SWei Liu     offset += err;
215360ce86c7SWei Liu     return offset;
215460ce86c7SWei Liu }
215560ce86c7SWei Liu 
21568440e22eSGreg Kurz static int coroutine_fn v9fs_do_readdir_with_stat(V9fsPDU *pdu,
21578440e22eSGreg Kurz                                                   V9fsFidState *fidp,
21588440e22eSGreg Kurz                                                   uint32_t max_count)
215960ce86c7SWei Liu {
216060ce86c7SWei Liu     V9fsPath path;
216160ce86c7SWei Liu     V9fsStat v9stat;
216260ce86c7SWei Liu     int len, err = 0;
216360ce86c7SWei Liu     int32_t count = 0;
216460ce86c7SWei Liu     struct stat stbuf;
216560ce86c7SWei Liu     off_t saved_dir_pos;
2166635324e8SGreg Kurz     struct dirent *dent;
216760ce86c7SWei Liu 
216860ce86c7SWei Liu     /* save the directory position */
216960ce86c7SWei Liu     saved_dir_pos = v9fs_co_telldir(pdu, fidp);
217060ce86c7SWei Liu     if (saved_dir_pos < 0) {
217160ce86c7SWei Liu         return saved_dir_pos;
217260ce86c7SWei Liu     }
217360ce86c7SWei Liu 
217460ce86c7SWei Liu     while (1) {
217560ce86c7SWei Liu         v9fs_path_init(&path);
21767cde47d4SGreg Kurz 
21777cde47d4SGreg Kurz         v9fs_readdir_lock(&fidp->fs.dir);
21787cde47d4SGreg Kurz 
2179635324e8SGreg Kurz         err = v9fs_co_readdir(pdu, fidp, &dent);
2180635324e8SGreg Kurz         if (err || !dent) {
218160ce86c7SWei Liu             break;
218260ce86c7SWei Liu         }
218360ce86c7SWei Liu         err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path);
218460ce86c7SWei Liu         if (err < 0) {
21858762a46dSGreg Kurz             break;
218660ce86c7SWei Liu         }
218760ce86c7SWei Liu         err = v9fs_co_lstat(pdu, &path, &stbuf);
218860ce86c7SWei Liu         if (err < 0) {
21898762a46dSGreg Kurz             break;
219060ce86c7SWei Liu         }
21916069537fSJan Dakinevich         err = stat_to_v9stat(pdu, &path, dent->d_name, &stbuf, &v9stat);
219260ce86c7SWei Liu         if (err < 0) {
21938762a46dSGreg Kurz             break;
219460ce86c7SWei Liu         }
2195772a7369SJan Dakinevich         if ((count + v9stat.size + 2) > max_count) {
21967cde47d4SGreg Kurz             v9fs_readdir_unlock(&fidp->fs.dir);
21977cde47d4SGreg Kurz 
219860ce86c7SWei Liu             /* Ran out of buffer. Set dir back to old position and return */
219960ce86c7SWei Liu             v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
220060ce86c7SWei Liu             v9fs_stat_free(&v9stat);
220160ce86c7SWei Liu             v9fs_path_free(&path);
220260ce86c7SWei Liu             return count;
220360ce86c7SWei Liu         }
2204772a7369SJan Dakinevich 
2205772a7369SJan Dakinevich         /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
2206772a7369SJan Dakinevich         len = pdu_marshal(pdu, 11 + count, "S", &v9stat);
2207772a7369SJan Dakinevich 
2208772a7369SJan Dakinevich         v9fs_readdir_unlock(&fidp->fs.dir);
2209772a7369SJan Dakinevich 
2210772a7369SJan Dakinevich         if (len < 0) {
2211772a7369SJan Dakinevich             v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
2212772a7369SJan Dakinevich             v9fs_stat_free(&v9stat);
2213772a7369SJan Dakinevich             v9fs_path_free(&path);
2214772a7369SJan Dakinevich             return len;
2215772a7369SJan Dakinevich         }
221660ce86c7SWei Liu         count += len;
221760ce86c7SWei Liu         v9fs_stat_free(&v9stat);
221860ce86c7SWei Liu         v9fs_path_free(&path);
221960ce86c7SWei Liu         saved_dir_pos = dent->d_off;
222060ce86c7SWei Liu     }
22218762a46dSGreg Kurz 
22227cde47d4SGreg Kurz     v9fs_readdir_unlock(&fidp->fs.dir);
22237cde47d4SGreg Kurz 
222460ce86c7SWei Liu     v9fs_path_free(&path);
222560ce86c7SWei Liu     if (err < 0) {
222660ce86c7SWei Liu         return err;
222760ce86c7SWei Liu     }
222860ce86c7SWei Liu     return count;
222960ce86c7SWei Liu }
223060ce86c7SWei Liu 
22318440e22eSGreg Kurz static void coroutine_fn v9fs_read(void *opaque)
223260ce86c7SWei Liu {
223360ce86c7SWei Liu     int32_t fid;
223460ce86c7SWei Liu     uint64_t off;
223560ce86c7SWei Liu     ssize_t err = 0;
223660ce86c7SWei Liu     int32_t count = 0;
223760ce86c7SWei Liu     size_t offset = 7;
223860ce86c7SWei Liu     uint32_t max_count;
223960ce86c7SWei Liu     V9fsFidState *fidp;
224060ce86c7SWei Liu     V9fsPDU *pdu = opaque;
224160ce86c7SWei Liu     V9fsState *s = pdu->s;
224260ce86c7SWei Liu 
224360ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count);
224460ce86c7SWei Liu     if (err < 0) {
224560ce86c7SWei Liu         goto out_nofid;
224660ce86c7SWei Liu     }
224760ce86c7SWei Liu     trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count);
224860ce86c7SWei Liu 
224960ce86c7SWei Liu     fidp = get_fid(pdu, fid);
225060ce86c7SWei Liu     if (fidp == NULL) {
225160ce86c7SWei Liu         err = -EINVAL;
225260ce86c7SWei Liu         goto out_nofid;
225360ce86c7SWei Liu     }
225460ce86c7SWei Liu     if (fidp->fid_type == P9_FID_DIR) {
225560ce86c7SWei Liu 
225660ce86c7SWei Liu         if (off == 0) {
225760ce86c7SWei Liu             v9fs_co_rewinddir(pdu, fidp);
225860ce86c7SWei Liu         }
225960ce86c7SWei Liu         count = v9fs_do_readdir_with_stat(pdu, fidp, max_count);
226060ce86c7SWei Liu         if (count < 0) {
226160ce86c7SWei Liu             err = count;
226260ce86c7SWei Liu             goto out;
226360ce86c7SWei Liu         }
226460ce86c7SWei Liu         err = pdu_marshal(pdu, offset, "d", count);
226560ce86c7SWei Liu         if (err < 0) {
226660ce86c7SWei Liu             goto out;
226760ce86c7SWei Liu         }
226860ce86c7SWei Liu         err += offset + count;
226960ce86c7SWei Liu     } else if (fidp->fid_type == P9_FID_FILE) {
227060ce86c7SWei Liu         QEMUIOVector qiov_full;
227160ce86c7SWei Liu         QEMUIOVector qiov;
227260ce86c7SWei Liu         int32_t len;
227360ce86c7SWei Liu 
2274cf45183bSStefano Stabellini         v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, max_count, false);
227560ce86c7SWei Liu         qemu_iovec_init(&qiov, qiov_full.niov);
227660ce86c7SWei Liu         do {
227760ce86c7SWei Liu             qemu_iovec_reset(&qiov);
227860ce86c7SWei Liu             qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count);
227960ce86c7SWei Liu             if (0) {
228060ce86c7SWei Liu                 print_sg(qiov.iov, qiov.niov);
228160ce86c7SWei Liu             }
228260ce86c7SWei Liu             /* Loop in case of EINTR */
228360ce86c7SWei Liu             do {
228460ce86c7SWei Liu                 len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off);
228560ce86c7SWei Liu                 if (len >= 0) {
228660ce86c7SWei Liu                     off   += len;
228760ce86c7SWei Liu                     count += len;
228860ce86c7SWei Liu                 }
228960ce86c7SWei Liu             } while (len == -EINTR && !pdu->cancelled);
229060ce86c7SWei Liu             if (len < 0) {
229160ce86c7SWei Liu                 /* IO error return the error */
229260ce86c7SWei Liu                 err = len;
2293e95c9a49SLi Qiang                 goto out_free_iovec;
229460ce86c7SWei Liu             }
229560ce86c7SWei Liu         } while (count < max_count && len > 0);
229660ce86c7SWei Liu         err = pdu_marshal(pdu, offset, "d", count);
229760ce86c7SWei Liu         if (err < 0) {
2298e95c9a49SLi Qiang             goto out_free_iovec;
229960ce86c7SWei Liu         }
230060ce86c7SWei Liu         err += offset + count;
2301e95c9a49SLi Qiang out_free_iovec:
230260ce86c7SWei Liu         qemu_iovec_destroy(&qiov);
230360ce86c7SWei Liu         qemu_iovec_destroy(&qiov_full);
230460ce86c7SWei Liu     } else if (fidp->fid_type == P9_FID_XATTR) {
230560ce86c7SWei Liu         err = v9fs_xattr_read(s, pdu, fidp, off, max_count);
230660ce86c7SWei Liu     } else {
230760ce86c7SWei Liu         err = -EINVAL;
230860ce86c7SWei Liu     }
230960ce86c7SWei Liu     trace_v9fs_read_return(pdu->tag, pdu->id, count, err);
231060ce86c7SWei Liu out:
231160ce86c7SWei Liu     put_fid(pdu, fidp);
231260ce86c7SWei Liu out_nofid:
231360ce86c7SWei Liu     pdu_complete(pdu, err);
231460ce86c7SWei Liu }
231560ce86c7SWei Liu 
2316*29c9d2caSChristian Schoenebeck /**
2317*29c9d2caSChristian Schoenebeck  * Returns size required in Rreaddir response for the passed dirent @p name.
2318*29c9d2caSChristian Schoenebeck  *
2319*29c9d2caSChristian Schoenebeck  * @param name - directory entry's name (i.e. file name, directory name)
2320*29c9d2caSChristian Schoenebeck  * @returns required size in bytes
2321*29c9d2caSChristian Schoenebeck  */
2322*29c9d2caSChristian Schoenebeck size_t v9fs_readdir_response_size(V9fsString *name)
232360ce86c7SWei Liu {
232460ce86c7SWei Liu     /*
232560ce86c7SWei Liu      * Size of each dirent on the wire: size of qid (13) + size of offset (8)
232660ce86c7SWei Liu      * size of type (1) + size of name.size (2) + strlen(name.data)
232760ce86c7SWei Liu      */
232860ce86c7SWei Liu     return 24 + v9fs_string_size(name);
232960ce86c7SWei Liu }
233060ce86c7SWei Liu 
23318440e22eSGreg Kurz static int coroutine_fn v9fs_do_readdir(V9fsPDU *pdu, V9fsFidState *fidp,
23328440e22eSGreg Kurz                                         int32_t max_count)
233360ce86c7SWei Liu {
233460ce86c7SWei Liu     size_t size;
233560ce86c7SWei Liu     V9fsQID qid;
233660ce86c7SWei Liu     V9fsString name;
233760ce86c7SWei Liu     int len, err = 0;
233860ce86c7SWei Liu     int32_t count = 0;
233960ce86c7SWei Liu     off_t saved_dir_pos;
2340635324e8SGreg Kurz     struct dirent *dent;
234160ce86c7SWei Liu 
234260ce86c7SWei Liu     /* save the directory position */
234360ce86c7SWei Liu     saved_dir_pos = v9fs_co_telldir(pdu, fidp);
234460ce86c7SWei Liu     if (saved_dir_pos < 0) {
234560ce86c7SWei Liu         return saved_dir_pos;
234660ce86c7SWei Liu     }
234760ce86c7SWei Liu 
234860ce86c7SWei Liu     while (1) {
23497cde47d4SGreg Kurz         v9fs_readdir_lock(&fidp->fs.dir);
23507cde47d4SGreg Kurz 
2351635324e8SGreg Kurz         err = v9fs_co_readdir(pdu, fidp, &dent);
2352635324e8SGreg Kurz         if (err || !dent) {
235360ce86c7SWei Liu             break;
235460ce86c7SWei Liu         }
235560ce86c7SWei Liu         v9fs_string_init(&name);
235660ce86c7SWei Liu         v9fs_string_sprintf(&name, "%s", dent->d_name);
2357*29c9d2caSChristian Schoenebeck         if ((count + v9fs_readdir_response_size(&name)) > max_count) {
23587cde47d4SGreg Kurz             v9fs_readdir_unlock(&fidp->fs.dir);
23597cde47d4SGreg Kurz 
236060ce86c7SWei Liu             /* Ran out of buffer. Set dir back to old position and return */
236160ce86c7SWei Liu             v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
236260ce86c7SWei Liu             v9fs_string_free(&name);
236360ce86c7SWei Liu             return count;
236460ce86c7SWei Liu         }
23651a6ed33cSAntonios Motakis 
23661a6ed33cSAntonios Motakis         if (pdu->s->ctx.export_flags & V9FS_REMAP_INODES) {
23671a6ed33cSAntonios Motakis             /*
23681a6ed33cSAntonios Motakis              * dirent_to_qid() implies expensive stat call for each entry,
23691a6ed33cSAntonios Motakis              * we must do that here though since inode remapping requires
23701a6ed33cSAntonios Motakis              * the device id, which in turn might be different for
23711a6ed33cSAntonios Motakis              * different entries; we cannot make any assumption to avoid
23721a6ed33cSAntonios Motakis              * that here.
23731a6ed33cSAntonios Motakis              */
23741a6ed33cSAntonios Motakis             err = dirent_to_qid(pdu, fidp, dent, &qid);
23751a6ed33cSAntonios Motakis             if (err < 0) {
23761a6ed33cSAntonios Motakis                 v9fs_readdir_unlock(&fidp->fs.dir);
23771a6ed33cSAntonios Motakis                 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
23781a6ed33cSAntonios Motakis                 v9fs_string_free(&name);
23791a6ed33cSAntonios Motakis                 return err;
23801a6ed33cSAntonios Motakis             }
23811a6ed33cSAntonios Motakis         } else {
238260ce86c7SWei Liu             /*
238360ce86c7SWei Liu              * Fill up just the path field of qid because the client uses
238460ce86c7SWei Liu              * only that. To fill the entire qid structure we will have
23851a6ed33cSAntonios Motakis              * to stat each dirent found, which is expensive. For the
23861a6ed33cSAntonios Motakis              * latter reason we don't call dirent_to_qid() here. Only drawback
23871a6ed33cSAntonios Motakis              * is that no multi-device export detection of stat_to_qid()
23881a6ed33cSAntonios Motakis              * would be done and provided as error to the user here. But
23891a6ed33cSAntonios Motakis              * user would get that error anyway when accessing those
23901a6ed33cSAntonios Motakis              * files/dirs through other ways.
239160ce86c7SWei Liu              */
239260ce86c7SWei Liu             size = MIN(sizeof(dent->d_ino), sizeof(qid.path));
239360ce86c7SWei Liu             memcpy(&qid.path, &dent->d_ino, size);
239460ce86c7SWei Liu             /* Fill the other fields with dummy values */
239560ce86c7SWei Liu             qid.type = 0;
239660ce86c7SWei Liu             qid.version = 0;
23971a6ed33cSAntonios Motakis         }
239860ce86c7SWei Liu 
239960ce86c7SWei Liu         /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
240060ce86c7SWei Liu         len = pdu_marshal(pdu, 11 + count, "Qqbs",
240160ce86c7SWei Liu                           &qid, dent->d_off,
240260ce86c7SWei Liu                           dent->d_type, &name);
24037cde47d4SGreg Kurz 
24047cde47d4SGreg Kurz         v9fs_readdir_unlock(&fidp->fs.dir);
24057cde47d4SGreg Kurz 
240660ce86c7SWei Liu         if (len < 0) {
240760ce86c7SWei Liu             v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
240860ce86c7SWei Liu             v9fs_string_free(&name);
240960ce86c7SWei Liu             return len;
241060ce86c7SWei Liu         }
241160ce86c7SWei Liu         count += len;
241260ce86c7SWei Liu         v9fs_string_free(&name);
241360ce86c7SWei Liu         saved_dir_pos = dent->d_off;
241460ce86c7SWei Liu     }
24157cde47d4SGreg Kurz 
24167cde47d4SGreg Kurz     v9fs_readdir_unlock(&fidp->fs.dir);
24177cde47d4SGreg Kurz 
241860ce86c7SWei Liu     if (err < 0) {
241960ce86c7SWei Liu         return err;
242060ce86c7SWei Liu     }
242160ce86c7SWei Liu     return count;
242260ce86c7SWei Liu }
242360ce86c7SWei Liu 
24248440e22eSGreg Kurz static void coroutine_fn v9fs_readdir(void *opaque)
242560ce86c7SWei Liu {
242660ce86c7SWei Liu     int32_t fid;
242760ce86c7SWei Liu     V9fsFidState *fidp;
242860ce86c7SWei Liu     ssize_t retval = 0;
242960ce86c7SWei Liu     size_t offset = 7;
243060ce86c7SWei Liu     uint64_t initial_offset;
243160ce86c7SWei Liu     int32_t count;
243260ce86c7SWei Liu     uint32_t max_count;
243360ce86c7SWei Liu     V9fsPDU *pdu = opaque;
2434d36a5c22SChristian Schoenebeck     V9fsState *s = pdu->s;
243560ce86c7SWei Liu 
243660ce86c7SWei Liu     retval = pdu_unmarshal(pdu, offset, "dqd", &fid,
243760ce86c7SWei Liu                            &initial_offset, &max_count);
243860ce86c7SWei Liu     if (retval < 0) {
243960ce86c7SWei Liu         goto out_nofid;
244060ce86c7SWei Liu     }
244160ce86c7SWei Liu     trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count);
244260ce86c7SWei Liu 
2443d36a5c22SChristian Schoenebeck     /* Enough space for a R_readdir header: size[4] Rreaddir tag[2] count[4] */
2444d36a5c22SChristian Schoenebeck     if (max_count > s->msize - 11) {
2445d36a5c22SChristian Schoenebeck         max_count = s->msize - 11;
2446d36a5c22SChristian Schoenebeck         warn_report_once(
2447d36a5c22SChristian Schoenebeck             "9p: bad client: T_readdir with count > msize - 11"
2448d36a5c22SChristian Schoenebeck         );
2449d36a5c22SChristian Schoenebeck     }
2450d36a5c22SChristian Schoenebeck 
245160ce86c7SWei Liu     fidp = get_fid(pdu, fid);
245260ce86c7SWei Liu     if (fidp == NULL) {
245360ce86c7SWei Liu         retval = -EINVAL;
245460ce86c7SWei Liu         goto out_nofid;
245560ce86c7SWei Liu     }
2456f314ea4eSGreg Kurz     if (!fidp->fs.dir.stream) {
245760ce86c7SWei Liu         retval = -EINVAL;
245860ce86c7SWei Liu         goto out;
245960ce86c7SWei Liu     }
246060ce86c7SWei Liu     if (initial_offset == 0) {
246160ce86c7SWei Liu         v9fs_co_rewinddir(pdu, fidp);
246260ce86c7SWei Liu     } else {
246360ce86c7SWei Liu         v9fs_co_seekdir(pdu, fidp, initial_offset);
246460ce86c7SWei Liu     }
246560ce86c7SWei Liu     count = v9fs_do_readdir(pdu, fidp, max_count);
246660ce86c7SWei Liu     if (count < 0) {
246760ce86c7SWei Liu         retval = count;
246860ce86c7SWei Liu         goto out;
246960ce86c7SWei Liu     }
247060ce86c7SWei Liu     retval = pdu_marshal(pdu, offset, "d", count);
247160ce86c7SWei Liu     if (retval < 0) {
247260ce86c7SWei Liu         goto out;
247360ce86c7SWei Liu     }
247460ce86c7SWei Liu     retval += count + offset;
247560ce86c7SWei Liu     trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval);
247660ce86c7SWei Liu out:
247760ce86c7SWei Liu     put_fid(pdu, fidp);
247860ce86c7SWei Liu out_nofid:
247960ce86c7SWei Liu     pdu_complete(pdu, retval);
248060ce86c7SWei Liu }
248160ce86c7SWei Liu 
248260ce86c7SWei Liu static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
248360ce86c7SWei Liu                             uint64_t off, uint32_t count,
248460ce86c7SWei Liu                             struct iovec *sg, int cnt)
248560ce86c7SWei Liu {
248660ce86c7SWei Liu     int i, to_copy;
248760ce86c7SWei Liu     ssize_t err = 0;
24887e55d65cSLi Qiang     uint64_t write_count;
248960ce86c7SWei Liu     size_t offset = 7;
249060ce86c7SWei Liu 
249160ce86c7SWei Liu 
24927e55d65cSLi Qiang     if (fidp->fs.xattr.len < off) {
2493b858e80aSDaniel Henrique Barboza         return -ENOSPC;
249460ce86c7SWei Liu     }
24957e55d65cSLi Qiang     write_count = fidp->fs.xattr.len - off;
24967e55d65cSLi Qiang     if (write_count > count) {
24977e55d65cSLi Qiang         write_count = count;
24987e55d65cSLi Qiang     }
249960ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "d", write_count);
250060ce86c7SWei Liu     if (err < 0) {
250160ce86c7SWei Liu         return err;
250260ce86c7SWei Liu     }
250360ce86c7SWei Liu     err += offset;
250460ce86c7SWei Liu     fidp->fs.xattr.copied_len += write_count;
250560ce86c7SWei Liu     /*
250660ce86c7SWei Liu      * Now copy the content from sg list
250760ce86c7SWei Liu      */
250860ce86c7SWei Liu     for (i = 0; i < cnt; i++) {
250960ce86c7SWei Liu         if (write_count > sg[i].iov_len) {
251060ce86c7SWei Liu             to_copy = sg[i].iov_len;
251160ce86c7SWei Liu         } else {
251260ce86c7SWei Liu             to_copy = write_count;
251360ce86c7SWei Liu         }
251460ce86c7SWei Liu         memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy);
251560ce86c7SWei Liu         /* updating vs->off since we are not using below */
251660ce86c7SWei Liu         off += to_copy;
251760ce86c7SWei Liu         write_count -= to_copy;
251860ce86c7SWei Liu     }
2519b858e80aSDaniel Henrique Barboza 
252060ce86c7SWei Liu     return err;
252160ce86c7SWei Liu }
252260ce86c7SWei Liu 
25238440e22eSGreg Kurz static void coroutine_fn v9fs_write(void *opaque)
252460ce86c7SWei Liu {
252560ce86c7SWei Liu     ssize_t err;
252660ce86c7SWei Liu     int32_t fid;
252760ce86c7SWei Liu     uint64_t off;
252860ce86c7SWei Liu     uint32_t count;
252960ce86c7SWei Liu     int32_t len = 0;
253060ce86c7SWei Liu     int32_t total = 0;
253160ce86c7SWei Liu     size_t offset = 7;
253260ce86c7SWei Liu     V9fsFidState *fidp;
253360ce86c7SWei Liu     V9fsPDU *pdu = opaque;
253460ce86c7SWei Liu     V9fsState *s = pdu->s;
253560ce86c7SWei Liu     QEMUIOVector qiov_full;
253660ce86c7SWei Liu     QEMUIOVector qiov;
253760ce86c7SWei Liu 
253860ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count);
253960ce86c7SWei Liu     if (err < 0) {
254060ce86c7SWei Liu         pdu_complete(pdu, err);
254160ce86c7SWei Liu         return;
254260ce86c7SWei Liu     }
254360ce86c7SWei Liu     offset += err;
2544cf45183bSStefano Stabellini     v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, count, true);
254560ce86c7SWei Liu     trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov);
254660ce86c7SWei Liu 
254760ce86c7SWei Liu     fidp = get_fid(pdu, fid);
254860ce86c7SWei Liu     if (fidp == NULL) {
254960ce86c7SWei Liu         err = -EINVAL;
255060ce86c7SWei Liu         goto out_nofid;
255160ce86c7SWei Liu     }
255260ce86c7SWei Liu     if (fidp->fid_type == P9_FID_FILE) {
255360ce86c7SWei Liu         if (fidp->fs.fd == -1) {
255460ce86c7SWei Liu             err = -EINVAL;
255560ce86c7SWei Liu             goto out;
255660ce86c7SWei Liu         }
255760ce86c7SWei Liu     } else if (fidp->fid_type == P9_FID_XATTR) {
255860ce86c7SWei Liu         /*
255960ce86c7SWei Liu          * setxattr operation
256060ce86c7SWei Liu          */
256160ce86c7SWei Liu         err = v9fs_xattr_write(s, pdu, fidp, off, count,
256260ce86c7SWei Liu                                qiov_full.iov, qiov_full.niov);
256360ce86c7SWei Liu         goto out;
256460ce86c7SWei Liu     } else {
256560ce86c7SWei Liu         err = -EINVAL;
256660ce86c7SWei Liu         goto out;
256760ce86c7SWei Liu     }
256860ce86c7SWei Liu     qemu_iovec_init(&qiov, qiov_full.niov);
256960ce86c7SWei Liu     do {
257060ce86c7SWei Liu         qemu_iovec_reset(&qiov);
257160ce86c7SWei Liu         qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total);
257260ce86c7SWei Liu         if (0) {
257360ce86c7SWei Liu             print_sg(qiov.iov, qiov.niov);
257460ce86c7SWei Liu         }
257560ce86c7SWei Liu         /* Loop in case of EINTR */
257660ce86c7SWei Liu         do {
257760ce86c7SWei Liu             len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off);
257860ce86c7SWei Liu             if (len >= 0) {
257960ce86c7SWei Liu                 off   += len;
258060ce86c7SWei Liu                 total += len;
258160ce86c7SWei Liu             }
258260ce86c7SWei Liu         } while (len == -EINTR && !pdu->cancelled);
258360ce86c7SWei Liu         if (len < 0) {
258460ce86c7SWei Liu             /* IO error return the error */
258560ce86c7SWei Liu             err = len;
258660ce86c7SWei Liu             goto out_qiov;
258760ce86c7SWei Liu         }
258860ce86c7SWei Liu     } while (total < count && len > 0);
258960ce86c7SWei Liu 
259060ce86c7SWei Liu     offset = 7;
259160ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "d", total);
259260ce86c7SWei Liu     if (err < 0) {
2593fdfcc9aeSLi Qiang         goto out_qiov;
259460ce86c7SWei Liu     }
259560ce86c7SWei Liu     err += offset;
259660ce86c7SWei Liu     trace_v9fs_write_return(pdu->tag, pdu->id, total, err);
259760ce86c7SWei Liu out_qiov:
259860ce86c7SWei Liu     qemu_iovec_destroy(&qiov);
259960ce86c7SWei Liu out:
260060ce86c7SWei Liu     put_fid(pdu, fidp);
260160ce86c7SWei Liu out_nofid:
260260ce86c7SWei Liu     qemu_iovec_destroy(&qiov_full);
260360ce86c7SWei Liu     pdu_complete(pdu, err);
260460ce86c7SWei Liu }
260560ce86c7SWei Liu 
26068440e22eSGreg Kurz static void coroutine_fn v9fs_create(void *opaque)
260760ce86c7SWei Liu {
260860ce86c7SWei Liu     int32_t fid;
260960ce86c7SWei Liu     int err = 0;
261060ce86c7SWei Liu     size_t offset = 7;
261160ce86c7SWei Liu     V9fsFidState *fidp;
261260ce86c7SWei Liu     V9fsQID qid;
261360ce86c7SWei Liu     int32_t perm;
261460ce86c7SWei Liu     int8_t mode;
261560ce86c7SWei Liu     V9fsPath path;
261660ce86c7SWei Liu     struct stat stbuf;
261760ce86c7SWei Liu     V9fsString name;
261860ce86c7SWei Liu     V9fsString extension;
261960ce86c7SWei Liu     int iounit;
262060ce86c7SWei Liu     V9fsPDU *pdu = opaque;
26215b3c77aaSGreg Kurz     V9fsState *s = pdu->s;
262260ce86c7SWei Liu 
262360ce86c7SWei Liu     v9fs_path_init(&path);
262460ce86c7SWei Liu     v9fs_string_init(&name);
262560ce86c7SWei Liu     v9fs_string_init(&extension);
262660ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name,
262760ce86c7SWei Liu                         &perm, &mode, &extension);
262860ce86c7SWei Liu     if (err < 0) {
262960ce86c7SWei Liu         goto out_nofid;
263060ce86c7SWei Liu     }
263160ce86c7SWei Liu     trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode);
263260ce86c7SWei Liu 
2633fff39a7aSGreg Kurz     if (name_is_illegal(name.data)) {
2634fff39a7aSGreg Kurz         err = -ENOENT;
2635fff39a7aSGreg Kurz         goto out_nofid;
2636fff39a7aSGreg Kurz     }
2637fff39a7aSGreg Kurz 
2638805b5d98SGreg Kurz     if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2639805b5d98SGreg Kurz         err = -EEXIST;
2640805b5d98SGreg Kurz         goto out_nofid;
2641805b5d98SGreg Kurz     }
2642805b5d98SGreg Kurz 
264360ce86c7SWei Liu     fidp = get_fid(pdu, fid);
264460ce86c7SWei Liu     if (fidp == NULL) {
264560ce86c7SWei Liu         err = -EINVAL;
264660ce86c7SWei Liu         goto out_nofid;
264760ce86c7SWei Liu     }
2648d63fb193SLi Qiang     if (fidp->fid_type != P9_FID_NONE) {
2649d63fb193SLi Qiang         err = -EINVAL;
2650d63fb193SLi Qiang         goto out;
2651d63fb193SLi Qiang     }
265260ce86c7SWei Liu     if (perm & P9_STAT_MODE_DIR) {
265360ce86c7SWei Liu         err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777,
265460ce86c7SWei Liu                             fidp->uid, -1, &stbuf);
265560ce86c7SWei Liu         if (err < 0) {
265660ce86c7SWei Liu             goto out;
265760ce86c7SWei Liu         }
265860ce86c7SWei Liu         err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
265960ce86c7SWei Liu         if (err < 0) {
266060ce86c7SWei Liu             goto out;
266160ce86c7SWei Liu         }
26625b3c77aaSGreg Kurz         v9fs_path_write_lock(s);
266360ce86c7SWei Liu         v9fs_path_copy(&fidp->path, &path);
26645b3c77aaSGreg Kurz         v9fs_path_unlock(s);
266560ce86c7SWei Liu         err = v9fs_co_opendir(pdu, fidp);
266660ce86c7SWei Liu         if (err < 0) {
266760ce86c7SWei Liu             goto out;
266860ce86c7SWei Liu         }
266960ce86c7SWei Liu         fidp->fid_type = P9_FID_DIR;
267060ce86c7SWei Liu     } else if (perm & P9_STAT_MODE_SYMLINK) {
267160ce86c7SWei Liu         err = v9fs_co_symlink(pdu, fidp, &name,
267260ce86c7SWei Liu                               extension.data, -1 , &stbuf);
267360ce86c7SWei Liu         if (err < 0) {
267460ce86c7SWei Liu             goto out;
267560ce86c7SWei Liu         }
267660ce86c7SWei Liu         err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
267760ce86c7SWei Liu         if (err < 0) {
267860ce86c7SWei Liu             goto out;
267960ce86c7SWei Liu         }
26805b3c77aaSGreg Kurz         v9fs_path_write_lock(s);
268160ce86c7SWei Liu         v9fs_path_copy(&fidp->path, &path);
26825b3c77aaSGreg Kurz         v9fs_path_unlock(s);
268360ce86c7SWei Liu     } else if (perm & P9_STAT_MODE_LINK) {
268460ce86c7SWei Liu         int32_t ofid = atoi(extension.data);
268560ce86c7SWei Liu         V9fsFidState *ofidp = get_fid(pdu, ofid);
268660ce86c7SWei Liu         if (ofidp == NULL) {
268760ce86c7SWei Liu             err = -EINVAL;
268860ce86c7SWei Liu             goto out;
268960ce86c7SWei Liu         }
269060ce86c7SWei Liu         err = v9fs_co_link(pdu, ofidp, fidp, &name);
269160ce86c7SWei Liu         put_fid(pdu, ofidp);
269260ce86c7SWei Liu         if (err < 0) {
269360ce86c7SWei Liu             goto out;
269460ce86c7SWei Liu         }
269560ce86c7SWei Liu         err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
269660ce86c7SWei Liu         if (err < 0) {
269760ce86c7SWei Liu             fidp->fid_type = P9_FID_NONE;
269860ce86c7SWei Liu             goto out;
269960ce86c7SWei Liu         }
27005b3c77aaSGreg Kurz         v9fs_path_write_lock(s);
270160ce86c7SWei Liu         v9fs_path_copy(&fidp->path, &path);
27025b3c77aaSGreg Kurz         v9fs_path_unlock(s);
270360ce86c7SWei Liu         err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
270460ce86c7SWei Liu         if (err < 0) {
270560ce86c7SWei Liu             fidp->fid_type = P9_FID_NONE;
270660ce86c7SWei Liu             goto out;
270760ce86c7SWei Liu         }
270860ce86c7SWei Liu     } else if (perm & P9_STAT_MODE_DEVICE) {
270960ce86c7SWei Liu         char ctype;
271060ce86c7SWei Liu         uint32_t major, minor;
271160ce86c7SWei Liu         mode_t nmode = 0;
271260ce86c7SWei Liu 
271360ce86c7SWei Liu         if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) {
271460ce86c7SWei Liu             err = -errno;
271560ce86c7SWei Liu             goto out;
271660ce86c7SWei Liu         }
271760ce86c7SWei Liu 
271860ce86c7SWei Liu         switch (ctype) {
271960ce86c7SWei Liu         case 'c':
272060ce86c7SWei Liu             nmode = S_IFCHR;
272160ce86c7SWei Liu             break;
272260ce86c7SWei Liu         case 'b':
272360ce86c7SWei Liu             nmode = S_IFBLK;
272460ce86c7SWei Liu             break;
272560ce86c7SWei Liu         default:
272660ce86c7SWei Liu             err = -EIO;
272760ce86c7SWei Liu             goto out;
272860ce86c7SWei Liu         }
272960ce86c7SWei Liu 
273060ce86c7SWei Liu         nmode |= perm & 0777;
273160ce86c7SWei Liu         err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
273260ce86c7SWei Liu                             makedev(major, minor), nmode, &stbuf);
273360ce86c7SWei Liu         if (err < 0) {
273460ce86c7SWei Liu             goto out;
273560ce86c7SWei Liu         }
273660ce86c7SWei Liu         err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
273760ce86c7SWei Liu         if (err < 0) {
273860ce86c7SWei Liu             goto out;
273960ce86c7SWei Liu         }
27405b3c77aaSGreg Kurz         v9fs_path_write_lock(s);
274160ce86c7SWei Liu         v9fs_path_copy(&fidp->path, &path);
27425b3c77aaSGreg Kurz         v9fs_path_unlock(s);
274360ce86c7SWei Liu     } else if (perm & P9_STAT_MODE_NAMED_PIPE) {
274460ce86c7SWei Liu         err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
274560ce86c7SWei Liu                             0, S_IFIFO | (perm & 0777), &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     } else if (perm & P9_STAT_MODE_SOCKET) {
275760ce86c7SWei Liu         err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
275860ce86c7SWei Liu                             0, S_IFSOCK | (perm & 0777), &stbuf);
275960ce86c7SWei Liu         if (err < 0) {
276060ce86c7SWei Liu             goto out;
276160ce86c7SWei Liu         }
276260ce86c7SWei Liu         err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
276360ce86c7SWei Liu         if (err < 0) {
276460ce86c7SWei Liu             goto out;
276560ce86c7SWei Liu         }
27665b3c77aaSGreg Kurz         v9fs_path_write_lock(s);
276760ce86c7SWei Liu         v9fs_path_copy(&fidp->path, &path);
27685b3c77aaSGreg Kurz         v9fs_path_unlock(s);
276960ce86c7SWei Liu     } else {
277060ce86c7SWei Liu         err = v9fs_co_open2(pdu, fidp, &name, -1,
277160ce86c7SWei Liu                             omode_to_uflags(mode)|O_CREAT, perm, &stbuf);
277260ce86c7SWei Liu         if (err < 0) {
277360ce86c7SWei Liu             goto out;
277460ce86c7SWei Liu         }
277560ce86c7SWei Liu         fidp->fid_type = P9_FID_FILE;
277660ce86c7SWei Liu         fidp->open_flags = omode_to_uflags(mode);
277760ce86c7SWei Liu         if (fidp->open_flags & O_EXCL) {
277860ce86c7SWei Liu             /*
277960ce86c7SWei Liu              * We let the host file system do O_EXCL check
278060ce86c7SWei Liu              * We should not reclaim such fd
278160ce86c7SWei Liu              */
278260ce86c7SWei Liu             fidp->flags |= FID_NON_RECLAIMABLE;
278360ce86c7SWei Liu         }
278460ce86c7SWei Liu     }
278560ce86c7SWei Liu     iounit = get_iounit(pdu, &fidp->path);
27863b5ee9e8SAntonios Motakis     err = stat_to_qid(pdu, &stbuf, &qid);
27873b5ee9e8SAntonios Motakis     if (err < 0) {
27883b5ee9e8SAntonios Motakis         goto out;
27893b5ee9e8SAntonios Motakis     }
279060ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
279160ce86c7SWei Liu     if (err < 0) {
279260ce86c7SWei Liu         goto out;
279360ce86c7SWei Liu     }
279460ce86c7SWei Liu     err += offset;
279560ce86c7SWei Liu     trace_v9fs_create_return(pdu->tag, pdu->id,
279660ce86c7SWei Liu                              qid.type, qid.version, qid.path, iounit);
279760ce86c7SWei Liu out:
279860ce86c7SWei Liu     put_fid(pdu, fidp);
279960ce86c7SWei Liu out_nofid:
280060ce86c7SWei Liu    pdu_complete(pdu, err);
280160ce86c7SWei Liu    v9fs_string_free(&name);
280260ce86c7SWei Liu    v9fs_string_free(&extension);
280360ce86c7SWei Liu    v9fs_path_free(&path);
280460ce86c7SWei Liu }
280560ce86c7SWei Liu 
28068440e22eSGreg Kurz static void coroutine_fn v9fs_symlink(void *opaque)
280760ce86c7SWei Liu {
280860ce86c7SWei Liu     V9fsPDU *pdu = opaque;
280960ce86c7SWei Liu     V9fsString name;
281060ce86c7SWei Liu     V9fsString symname;
281160ce86c7SWei Liu     V9fsFidState *dfidp;
281260ce86c7SWei Liu     V9fsQID qid;
281360ce86c7SWei Liu     struct stat stbuf;
281460ce86c7SWei Liu     int32_t dfid;
281560ce86c7SWei Liu     int err = 0;
281660ce86c7SWei Liu     gid_t gid;
281760ce86c7SWei Liu     size_t offset = 7;
281860ce86c7SWei Liu 
281960ce86c7SWei Liu     v9fs_string_init(&name);
282060ce86c7SWei Liu     v9fs_string_init(&symname);
282160ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid);
282260ce86c7SWei Liu     if (err < 0) {
282360ce86c7SWei Liu         goto out_nofid;
282460ce86c7SWei Liu     }
282560ce86c7SWei Liu     trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid);
282660ce86c7SWei Liu 
2827fff39a7aSGreg Kurz     if (name_is_illegal(name.data)) {
2828fff39a7aSGreg Kurz         err = -ENOENT;
2829fff39a7aSGreg Kurz         goto out_nofid;
2830fff39a7aSGreg Kurz     }
2831fff39a7aSGreg Kurz 
2832805b5d98SGreg Kurz     if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2833805b5d98SGreg Kurz         err = -EEXIST;
2834805b5d98SGreg Kurz         goto out_nofid;
2835805b5d98SGreg Kurz     }
2836805b5d98SGreg Kurz 
283760ce86c7SWei Liu     dfidp = get_fid(pdu, dfid);
283860ce86c7SWei Liu     if (dfidp == NULL) {
283960ce86c7SWei Liu         err = -EINVAL;
284060ce86c7SWei Liu         goto out_nofid;
284160ce86c7SWei Liu     }
284260ce86c7SWei Liu     err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf);
284360ce86c7SWei Liu     if (err < 0) {
284460ce86c7SWei Liu         goto out;
284560ce86c7SWei Liu     }
28463b5ee9e8SAntonios Motakis     err = stat_to_qid(pdu, &stbuf, &qid);
28473b5ee9e8SAntonios Motakis     if (err < 0) {
28483b5ee9e8SAntonios Motakis         goto out;
28493b5ee9e8SAntonios Motakis     }
285060ce86c7SWei Liu     err =  pdu_marshal(pdu, offset, "Q", &qid);
285160ce86c7SWei Liu     if (err < 0) {
285260ce86c7SWei Liu         goto out;
285360ce86c7SWei Liu     }
285460ce86c7SWei Liu     err += offset;
285560ce86c7SWei Liu     trace_v9fs_symlink_return(pdu->tag, pdu->id,
285660ce86c7SWei Liu                               qid.type, qid.version, qid.path);
285760ce86c7SWei Liu out:
285860ce86c7SWei Liu     put_fid(pdu, dfidp);
285960ce86c7SWei Liu out_nofid:
286060ce86c7SWei Liu     pdu_complete(pdu, err);
286160ce86c7SWei Liu     v9fs_string_free(&name);
286260ce86c7SWei Liu     v9fs_string_free(&symname);
286360ce86c7SWei Liu }
286460ce86c7SWei Liu 
2865a1bf8b74SGreg Kurz static void coroutine_fn v9fs_flush(void *opaque)
286660ce86c7SWei Liu {
286760ce86c7SWei Liu     ssize_t err;
286860ce86c7SWei Liu     int16_t tag;
286960ce86c7SWei Liu     size_t offset = 7;
2870d5f2af7bSGreg Kurz     V9fsPDU *cancel_pdu = NULL;
287160ce86c7SWei Liu     V9fsPDU *pdu = opaque;
287260ce86c7SWei Liu     V9fsState *s = pdu->s;
287360ce86c7SWei Liu 
287460ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "w", &tag);
287560ce86c7SWei Liu     if (err < 0) {
287660ce86c7SWei Liu         pdu_complete(pdu, err);
287760ce86c7SWei Liu         return;
287860ce86c7SWei Liu     }
287960ce86c7SWei Liu     trace_v9fs_flush(pdu->tag, pdu->id, tag);
288060ce86c7SWei Liu 
2881d5f2af7bSGreg Kurz     if (pdu->tag == tag) {
28823dc6f869SAlistair Francis         warn_report("the guest sent a self-referencing 9P flush request");
2883d5f2af7bSGreg Kurz     } else {
288460ce86c7SWei Liu         QLIST_FOREACH(cancel_pdu, &s->active_list, next) {
288560ce86c7SWei Liu             if (cancel_pdu->tag == tag) {
288660ce86c7SWei Liu                 break;
288760ce86c7SWei Liu             }
288860ce86c7SWei Liu         }
2889d5f2af7bSGreg Kurz     }
289060ce86c7SWei Liu     if (cancel_pdu) {
289160ce86c7SWei Liu         cancel_pdu->cancelled = 1;
289260ce86c7SWei Liu         /*
289360ce86c7SWei Liu          * Wait for pdu to complete.
289460ce86c7SWei Liu          */
28951ace7ceaSPaolo Bonzini         qemu_co_queue_wait(&cancel_pdu->complete, NULL);
289618adde86SGreg Kurz         if (!qemu_co_queue_next(&cancel_pdu->complete)) {
289760ce86c7SWei Liu             cancel_pdu->cancelled = 0;
289860ce86c7SWei Liu             pdu_free(cancel_pdu);
289960ce86c7SWei Liu         }
290018adde86SGreg Kurz     }
290160ce86c7SWei Liu     pdu_complete(pdu, 7);
290260ce86c7SWei Liu }
290360ce86c7SWei Liu 
29048440e22eSGreg Kurz static void coroutine_fn v9fs_link(void *opaque)
290560ce86c7SWei Liu {
290660ce86c7SWei Liu     V9fsPDU *pdu = opaque;
290760ce86c7SWei Liu     int32_t dfid, oldfid;
290860ce86c7SWei Liu     V9fsFidState *dfidp, *oldfidp;
290960ce86c7SWei Liu     V9fsString name;
291060ce86c7SWei Liu     size_t offset = 7;
291160ce86c7SWei Liu     int err = 0;
291260ce86c7SWei Liu 
291360ce86c7SWei Liu     v9fs_string_init(&name);
291460ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
291560ce86c7SWei Liu     if (err < 0) {
291660ce86c7SWei Liu         goto out_nofid;
291760ce86c7SWei Liu     }
291860ce86c7SWei Liu     trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data);
291960ce86c7SWei Liu 
2920fff39a7aSGreg Kurz     if (name_is_illegal(name.data)) {
2921fff39a7aSGreg Kurz         err = -ENOENT;
2922fff39a7aSGreg Kurz         goto out_nofid;
2923fff39a7aSGreg Kurz     }
2924fff39a7aSGreg Kurz 
2925805b5d98SGreg Kurz     if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2926805b5d98SGreg Kurz         err = -EEXIST;
2927805b5d98SGreg Kurz         goto out_nofid;
2928805b5d98SGreg Kurz     }
2929805b5d98SGreg Kurz 
293060ce86c7SWei Liu     dfidp = get_fid(pdu, dfid);
293160ce86c7SWei Liu     if (dfidp == NULL) {
293260ce86c7SWei Liu         err = -ENOENT;
293360ce86c7SWei Liu         goto out_nofid;
293460ce86c7SWei Liu     }
293560ce86c7SWei Liu 
293660ce86c7SWei Liu     oldfidp = get_fid(pdu, oldfid);
293760ce86c7SWei Liu     if (oldfidp == NULL) {
293860ce86c7SWei Liu         err = -ENOENT;
293960ce86c7SWei Liu         goto out;
294060ce86c7SWei Liu     }
294160ce86c7SWei Liu     err = v9fs_co_link(pdu, oldfidp, dfidp, &name);
294260ce86c7SWei Liu     if (!err) {
294360ce86c7SWei Liu         err = offset;
294460ce86c7SWei Liu     }
29454c158678SLi Qiang     put_fid(pdu, oldfidp);
294660ce86c7SWei Liu out:
294760ce86c7SWei Liu     put_fid(pdu, dfidp);
294860ce86c7SWei Liu out_nofid:
294960ce86c7SWei Liu     v9fs_string_free(&name);
295060ce86c7SWei Liu     pdu_complete(pdu, err);
295160ce86c7SWei Liu }
295260ce86c7SWei Liu 
295360ce86c7SWei Liu /* Only works with path name based fid */
29548440e22eSGreg Kurz static void coroutine_fn v9fs_remove(void *opaque)
295560ce86c7SWei Liu {
295660ce86c7SWei Liu     int32_t fid;
295760ce86c7SWei Liu     int err = 0;
295860ce86c7SWei Liu     size_t offset = 7;
295960ce86c7SWei Liu     V9fsFidState *fidp;
296060ce86c7SWei Liu     V9fsPDU *pdu = opaque;
296160ce86c7SWei Liu 
296260ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "d", &fid);
296360ce86c7SWei Liu     if (err < 0) {
296460ce86c7SWei Liu         goto out_nofid;
296560ce86c7SWei Liu     }
296660ce86c7SWei Liu     trace_v9fs_remove(pdu->tag, pdu->id, fid);
296760ce86c7SWei Liu 
296860ce86c7SWei Liu     fidp = get_fid(pdu, fid);
296960ce86c7SWei Liu     if (fidp == NULL) {
297060ce86c7SWei Liu         err = -EINVAL;
297160ce86c7SWei Liu         goto out_nofid;
297260ce86c7SWei Liu     }
297360ce86c7SWei Liu     /* if fs driver is not path based, return EOPNOTSUPP */
297460ce86c7SWei Liu     if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
297560ce86c7SWei Liu         err = -EOPNOTSUPP;
297660ce86c7SWei Liu         goto out_err;
297760ce86c7SWei Liu     }
297860ce86c7SWei Liu     /*
297960ce86c7SWei Liu      * IF the file is unlinked, we cannot reopen
298060ce86c7SWei Liu      * the file later. So don't reclaim fd
298160ce86c7SWei Liu      */
298260ce86c7SWei Liu     err = v9fs_mark_fids_unreclaim(pdu, &fidp->path);
298360ce86c7SWei Liu     if (err < 0) {
298460ce86c7SWei Liu         goto out_err;
298560ce86c7SWei Liu     }
298660ce86c7SWei Liu     err = v9fs_co_remove(pdu, &fidp->path);
298760ce86c7SWei Liu     if (!err) {
298860ce86c7SWei Liu         err = offset;
298960ce86c7SWei Liu     }
299060ce86c7SWei Liu out_err:
299160ce86c7SWei Liu     /* For TREMOVE we need to clunk the fid even on failed remove */
299260ce86c7SWei Liu     clunk_fid(pdu->s, fidp->fid);
299360ce86c7SWei Liu     put_fid(pdu, fidp);
299460ce86c7SWei Liu out_nofid:
299560ce86c7SWei Liu     pdu_complete(pdu, err);
299660ce86c7SWei Liu }
299760ce86c7SWei Liu 
29988440e22eSGreg Kurz static void coroutine_fn v9fs_unlinkat(void *opaque)
299960ce86c7SWei Liu {
300060ce86c7SWei Liu     int err = 0;
300160ce86c7SWei Liu     V9fsString name;
300267e87345SKeno Fischer     int32_t dfid, flags, rflags = 0;
300360ce86c7SWei Liu     size_t offset = 7;
300460ce86c7SWei Liu     V9fsPath path;
300560ce86c7SWei Liu     V9fsFidState *dfidp;
300660ce86c7SWei Liu     V9fsPDU *pdu = opaque;
300760ce86c7SWei Liu 
300860ce86c7SWei Liu     v9fs_string_init(&name);
300960ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags);
301060ce86c7SWei Liu     if (err < 0) {
301160ce86c7SWei Liu         goto out_nofid;
301260ce86c7SWei Liu     }
3013fff39a7aSGreg Kurz 
3014fff39a7aSGreg Kurz     if (name_is_illegal(name.data)) {
3015fff39a7aSGreg Kurz         err = -ENOENT;
3016fff39a7aSGreg Kurz         goto out_nofid;
3017fff39a7aSGreg Kurz     }
3018fff39a7aSGreg Kurz 
3019805b5d98SGreg Kurz     if (!strcmp(".", name.data)) {
3020805b5d98SGreg Kurz         err = -EINVAL;
3021805b5d98SGreg Kurz         goto out_nofid;
3022805b5d98SGreg Kurz     }
3023805b5d98SGreg Kurz 
3024805b5d98SGreg Kurz     if (!strcmp("..", name.data)) {
3025805b5d98SGreg Kurz         err = -ENOTEMPTY;
3026805b5d98SGreg Kurz         goto out_nofid;
3027805b5d98SGreg Kurz     }
3028805b5d98SGreg Kurz 
302967e87345SKeno Fischer     if (flags & ~P9_DOTL_AT_REMOVEDIR) {
303067e87345SKeno Fischer         err = -EINVAL;
303167e87345SKeno Fischer         goto out_nofid;
303267e87345SKeno Fischer     }
303367e87345SKeno Fischer 
303467e87345SKeno Fischer     if (flags & P9_DOTL_AT_REMOVEDIR) {
303567e87345SKeno Fischer         rflags |= AT_REMOVEDIR;
303667e87345SKeno Fischer     }
303767e87345SKeno Fischer 
303860ce86c7SWei Liu     dfidp = get_fid(pdu, dfid);
303960ce86c7SWei Liu     if (dfidp == NULL) {
304060ce86c7SWei Liu         err = -EINVAL;
304160ce86c7SWei Liu         goto out_nofid;
304260ce86c7SWei Liu     }
304360ce86c7SWei Liu     /*
304460ce86c7SWei Liu      * IF the file is unlinked, we cannot reopen
304560ce86c7SWei Liu      * the file later. So don't reclaim fd
304660ce86c7SWei Liu      */
304760ce86c7SWei Liu     v9fs_path_init(&path);
304860ce86c7SWei Liu     err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path);
304960ce86c7SWei Liu     if (err < 0) {
305060ce86c7SWei Liu         goto out_err;
305160ce86c7SWei Liu     }
305260ce86c7SWei Liu     err = v9fs_mark_fids_unreclaim(pdu, &path);
305360ce86c7SWei Liu     if (err < 0) {
305460ce86c7SWei Liu         goto out_err;
305560ce86c7SWei Liu     }
305667e87345SKeno Fischer     err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, rflags);
305760ce86c7SWei Liu     if (!err) {
305860ce86c7SWei Liu         err = offset;
305960ce86c7SWei Liu     }
306060ce86c7SWei Liu out_err:
306160ce86c7SWei Liu     put_fid(pdu, dfidp);
306260ce86c7SWei Liu     v9fs_path_free(&path);
306360ce86c7SWei Liu out_nofid:
306460ce86c7SWei Liu     pdu_complete(pdu, err);
306560ce86c7SWei Liu     v9fs_string_free(&name);
306660ce86c7SWei Liu }
306760ce86c7SWei Liu 
306860ce86c7SWei Liu 
306960ce86c7SWei Liu /* Only works with path name based fid */
30708440e22eSGreg Kurz static int coroutine_fn v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp,
30718440e22eSGreg Kurz                                              int32_t newdirfid,
30728440e22eSGreg Kurz                                              V9fsString *name)
307360ce86c7SWei Liu {
307460ce86c7SWei Liu     int err = 0;
307560ce86c7SWei Liu     V9fsPath new_path;
307660ce86c7SWei Liu     V9fsFidState *tfidp;
307760ce86c7SWei Liu     V9fsState *s = pdu->s;
307860ce86c7SWei Liu     V9fsFidState *dirfidp = NULL;
307960ce86c7SWei Liu 
308060ce86c7SWei Liu     v9fs_path_init(&new_path);
308160ce86c7SWei Liu     if (newdirfid != -1) {
308260ce86c7SWei Liu         dirfidp = get_fid(pdu, newdirfid);
308360ce86c7SWei Liu         if (dirfidp == NULL) {
3084b858e80aSDaniel Henrique Barboza             return -ENOENT;
308560ce86c7SWei Liu         }
308649dd946bSGreg Kurz         if (fidp->fid_type != P9_FID_NONE) {
308749dd946bSGreg Kurz             err = -EINVAL;
308849dd946bSGreg Kurz             goto out;
308949dd946bSGreg Kurz         }
30904fa62005SGreg Kurz         err = v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path);
30914fa62005SGreg Kurz         if (err < 0) {
30924fa62005SGreg Kurz             goto out;
30934fa62005SGreg Kurz         }
309460ce86c7SWei Liu     } else {
30954d8bc733SJan Dakinevich         char *dir_name = g_path_get_dirname(fidp->path.data);
30964d8bc733SJan Dakinevich         V9fsPath dir_path;
30974d8bc733SJan Dakinevich 
30984d8bc733SJan Dakinevich         v9fs_path_init(&dir_path);
30994d8bc733SJan Dakinevich         v9fs_path_sprintf(&dir_path, "%s", dir_name);
31004d8bc733SJan Dakinevich         g_free(dir_name);
31014d8bc733SJan Dakinevich 
31024d8bc733SJan Dakinevich         err = v9fs_co_name_to_path(pdu, &dir_path, name->data, &new_path);
31034d8bc733SJan Dakinevich         v9fs_path_free(&dir_path);
31044fa62005SGreg Kurz         if (err < 0) {
31054fa62005SGreg Kurz             goto out;
31064fa62005SGreg Kurz         }
310760ce86c7SWei Liu     }
310860ce86c7SWei Liu     err = v9fs_co_rename(pdu, &fidp->path, &new_path);
310960ce86c7SWei Liu     if (err < 0) {
311060ce86c7SWei Liu         goto out;
311160ce86c7SWei Liu     }
311260ce86c7SWei Liu     /*
311360ce86c7SWei Liu      * Fixup fid's pointing to the old name to
311460ce86c7SWei Liu      * start pointing to the new name
311560ce86c7SWei Liu      */
311660ce86c7SWei Liu     for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
311760ce86c7SWei Liu         if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) {
311860ce86c7SWei Liu             /* replace the name */
311960ce86c7SWei Liu             v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data));
312060ce86c7SWei Liu         }
312160ce86c7SWei Liu     }
312260ce86c7SWei Liu out:
312360ce86c7SWei Liu     if (dirfidp) {
312460ce86c7SWei Liu         put_fid(pdu, dirfidp);
312560ce86c7SWei Liu     }
312660ce86c7SWei Liu     v9fs_path_free(&new_path);
312760ce86c7SWei Liu     return err;
312860ce86c7SWei Liu }
312960ce86c7SWei Liu 
313060ce86c7SWei Liu /* Only works with path name based fid */
31318440e22eSGreg Kurz static void coroutine_fn v9fs_rename(void *opaque)
313260ce86c7SWei Liu {
313360ce86c7SWei Liu     int32_t fid;
313460ce86c7SWei Liu     ssize_t err = 0;
313560ce86c7SWei Liu     size_t offset = 7;
313660ce86c7SWei Liu     V9fsString name;
313760ce86c7SWei Liu     int32_t newdirfid;
313860ce86c7SWei Liu     V9fsFidState *fidp;
313960ce86c7SWei Liu     V9fsPDU *pdu = opaque;
314060ce86c7SWei Liu     V9fsState *s = pdu->s;
314160ce86c7SWei Liu 
314260ce86c7SWei Liu     v9fs_string_init(&name);
314360ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name);
314460ce86c7SWei Liu     if (err < 0) {
314560ce86c7SWei Liu         goto out_nofid;
314660ce86c7SWei Liu     }
3147fff39a7aSGreg Kurz 
3148fff39a7aSGreg Kurz     if (name_is_illegal(name.data)) {
3149fff39a7aSGreg Kurz         err = -ENOENT;
3150fff39a7aSGreg Kurz         goto out_nofid;
3151fff39a7aSGreg Kurz     }
3152fff39a7aSGreg Kurz 
3153805b5d98SGreg Kurz     if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3154805b5d98SGreg Kurz         err = -EISDIR;
3155805b5d98SGreg Kurz         goto out_nofid;
3156805b5d98SGreg Kurz     }
3157805b5d98SGreg Kurz 
315860ce86c7SWei Liu     fidp = get_fid(pdu, fid);
315960ce86c7SWei Liu     if (fidp == NULL) {
316060ce86c7SWei Liu         err = -ENOENT;
316160ce86c7SWei Liu         goto out_nofid;
316260ce86c7SWei Liu     }
316349dd946bSGreg Kurz     if (fidp->fid_type != P9_FID_NONE) {
316449dd946bSGreg Kurz         err = -EINVAL;
316549dd946bSGreg Kurz         goto out;
316649dd946bSGreg Kurz     }
316760ce86c7SWei Liu     /* if fs driver is not path based, return EOPNOTSUPP */
316860ce86c7SWei Liu     if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
316960ce86c7SWei Liu         err = -EOPNOTSUPP;
317060ce86c7SWei Liu         goto out;
317160ce86c7SWei Liu     }
317260ce86c7SWei Liu     v9fs_path_write_lock(s);
317360ce86c7SWei Liu     err = v9fs_complete_rename(pdu, fidp, newdirfid, &name);
317460ce86c7SWei Liu     v9fs_path_unlock(s);
317560ce86c7SWei Liu     if (!err) {
317660ce86c7SWei Liu         err = offset;
317760ce86c7SWei Liu     }
317860ce86c7SWei Liu out:
317960ce86c7SWei Liu     put_fid(pdu, fidp);
318060ce86c7SWei Liu out_nofid:
318160ce86c7SWei Liu     pdu_complete(pdu, err);
318260ce86c7SWei Liu     v9fs_string_free(&name);
318360ce86c7SWei Liu }
318460ce86c7SWei Liu 
31854fa62005SGreg Kurz static int coroutine_fn v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir,
31868440e22eSGreg Kurz                                            V9fsString *old_name,
31878440e22eSGreg Kurz                                            V9fsPath *newdir,
318860ce86c7SWei Liu                                            V9fsString *new_name)
318960ce86c7SWei Liu {
319060ce86c7SWei Liu     V9fsFidState *tfidp;
319160ce86c7SWei Liu     V9fsPath oldpath, newpath;
319260ce86c7SWei Liu     V9fsState *s = pdu->s;
31934fa62005SGreg Kurz     int err;
319460ce86c7SWei Liu 
319560ce86c7SWei Liu     v9fs_path_init(&oldpath);
319660ce86c7SWei Liu     v9fs_path_init(&newpath);
31974fa62005SGreg Kurz     err = v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath);
31984fa62005SGreg Kurz     if (err < 0) {
31994fa62005SGreg Kurz         goto out;
32004fa62005SGreg Kurz     }
32014fa62005SGreg Kurz     err = v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath);
32024fa62005SGreg Kurz     if (err < 0) {
32034fa62005SGreg Kurz         goto out;
32044fa62005SGreg Kurz     }
320560ce86c7SWei Liu 
320660ce86c7SWei Liu     /*
320760ce86c7SWei Liu      * Fixup fid's pointing to the old name to
320860ce86c7SWei Liu      * start pointing to the new name
320960ce86c7SWei Liu      */
321060ce86c7SWei Liu     for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
321160ce86c7SWei Liu         if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) {
321260ce86c7SWei Liu             /* replace the name */
321360ce86c7SWei Liu             v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data));
321460ce86c7SWei Liu         }
321560ce86c7SWei Liu     }
32164fa62005SGreg Kurz out:
321760ce86c7SWei Liu     v9fs_path_free(&oldpath);
321860ce86c7SWei Liu     v9fs_path_free(&newpath);
32194fa62005SGreg Kurz     return err;
322060ce86c7SWei Liu }
322160ce86c7SWei Liu 
32228440e22eSGreg Kurz static int coroutine_fn v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid,
32238440e22eSGreg Kurz                                                V9fsString *old_name,
32248440e22eSGreg Kurz                                                int32_t newdirfid,
322560ce86c7SWei Liu                                                V9fsString *new_name)
322660ce86c7SWei Liu {
322760ce86c7SWei Liu     int err = 0;
322860ce86c7SWei Liu     V9fsState *s = pdu->s;
322960ce86c7SWei Liu     V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL;
323060ce86c7SWei Liu 
323160ce86c7SWei Liu     olddirfidp = get_fid(pdu, olddirfid);
323260ce86c7SWei Liu     if (olddirfidp == NULL) {
323360ce86c7SWei Liu         err = -ENOENT;
323460ce86c7SWei Liu         goto out;
323560ce86c7SWei Liu     }
323660ce86c7SWei Liu     if (newdirfid != -1) {
323760ce86c7SWei Liu         newdirfidp = get_fid(pdu, newdirfid);
323860ce86c7SWei Liu         if (newdirfidp == NULL) {
323960ce86c7SWei Liu             err = -ENOENT;
324060ce86c7SWei Liu             goto out;
324160ce86c7SWei Liu         }
324260ce86c7SWei Liu     } else {
324360ce86c7SWei Liu         newdirfidp = get_fid(pdu, olddirfid);
324460ce86c7SWei Liu     }
324560ce86c7SWei Liu 
324660ce86c7SWei Liu     err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name,
324760ce86c7SWei Liu                            &newdirfidp->path, new_name);
324860ce86c7SWei Liu     if (err < 0) {
324960ce86c7SWei Liu         goto out;
325060ce86c7SWei Liu     }
325160ce86c7SWei Liu     if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
325260ce86c7SWei Liu         /* Only for path based fid  we need to do the below fixup */
32534fa62005SGreg Kurz         err = v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name,
325460ce86c7SWei Liu                                  &newdirfidp->path, new_name);
325560ce86c7SWei Liu     }
325660ce86c7SWei Liu out:
325760ce86c7SWei Liu     if (olddirfidp) {
325860ce86c7SWei Liu         put_fid(pdu, olddirfidp);
325960ce86c7SWei Liu     }
326060ce86c7SWei Liu     if (newdirfidp) {
326160ce86c7SWei Liu         put_fid(pdu, newdirfidp);
326260ce86c7SWei Liu     }
326360ce86c7SWei Liu     return err;
326460ce86c7SWei Liu }
326560ce86c7SWei Liu 
32668440e22eSGreg Kurz static void coroutine_fn v9fs_renameat(void *opaque)
326760ce86c7SWei Liu {
326860ce86c7SWei Liu     ssize_t err = 0;
326960ce86c7SWei Liu     size_t offset = 7;
327060ce86c7SWei Liu     V9fsPDU *pdu = opaque;
327160ce86c7SWei Liu     V9fsState *s = pdu->s;
327260ce86c7SWei Liu     int32_t olddirfid, newdirfid;
327360ce86c7SWei Liu     V9fsString old_name, new_name;
327460ce86c7SWei Liu 
327560ce86c7SWei Liu     v9fs_string_init(&old_name);
327660ce86c7SWei Liu     v9fs_string_init(&new_name);
327760ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid,
327860ce86c7SWei Liu                         &old_name, &newdirfid, &new_name);
327960ce86c7SWei Liu     if (err < 0) {
328060ce86c7SWei Liu         goto out_err;
328160ce86c7SWei Liu     }
328260ce86c7SWei Liu 
3283fff39a7aSGreg Kurz     if (name_is_illegal(old_name.data) || name_is_illegal(new_name.data)) {
3284fff39a7aSGreg Kurz         err = -ENOENT;
3285fff39a7aSGreg Kurz         goto out_err;
3286fff39a7aSGreg Kurz     }
3287fff39a7aSGreg Kurz 
3288805b5d98SGreg Kurz     if (!strcmp(".", old_name.data) || !strcmp("..", old_name.data) ||
3289805b5d98SGreg Kurz         !strcmp(".", new_name.data) || !strcmp("..", new_name.data)) {
3290805b5d98SGreg Kurz         err = -EISDIR;
3291805b5d98SGreg Kurz         goto out_err;
3292805b5d98SGreg Kurz     }
3293805b5d98SGreg Kurz 
329460ce86c7SWei Liu     v9fs_path_write_lock(s);
329560ce86c7SWei Liu     err = v9fs_complete_renameat(pdu, olddirfid,
329660ce86c7SWei Liu                                  &old_name, newdirfid, &new_name);
329760ce86c7SWei Liu     v9fs_path_unlock(s);
329860ce86c7SWei Liu     if (!err) {
329960ce86c7SWei Liu         err = offset;
330060ce86c7SWei Liu     }
330160ce86c7SWei Liu 
330260ce86c7SWei Liu out_err:
330360ce86c7SWei Liu     pdu_complete(pdu, err);
330460ce86c7SWei Liu     v9fs_string_free(&old_name);
330560ce86c7SWei Liu     v9fs_string_free(&new_name);
330660ce86c7SWei Liu }
330760ce86c7SWei Liu 
33088440e22eSGreg Kurz static void coroutine_fn v9fs_wstat(void *opaque)
330960ce86c7SWei Liu {
331060ce86c7SWei Liu     int32_t fid;
331160ce86c7SWei Liu     int err = 0;
331260ce86c7SWei Liu     int16_t unused;
331360ce86c7SWei Liu     V9fsStat v9stat;
331460ce86c7SWei Liu     size_t offset = 7;
331560ce86c7SWei Liu     struct stat stbuf;
331660ce86c7SWei Liu     V9fsFidState *fidp;
331760ce86c7SWei Liu     V9fsPDU *pdu = opaque;
33181d203986SGreg Kurz     V9fsState *s = pdu->s;
331960ce86c7SWei Liu 
332060ce86c7SWei Liu     v9fs_stat_init(&v9stat);
332160ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat);
332260ce86c7SWei Liu     if (err < 0) {
332360ce86c7SWei Liu         goto out_nofid;
332460ce86c7SWei Liu     }
332560ce86c7SWei Liu     trace_v9fs_wstat(pdu->tag, pdu->id, fid,
332660ce86c7SWei Liu                      v9stat.mode, v9stat.atime, v9stat.mtime);
332760ce86c7SWei Liu 
332860ce86c7SWei Liu     fidp = get_fid(pdu, fid);
332960ce86c7SWei Liu     if (fidp == NULL) {
333060ce86c7SWei Liu         err = -EINVAL;
333160ce86c7SWei Liu         goto out_nofid;
333260ce86c7SWei Liu     }
333360ce86c7SWei Liu     /* do we need to sync the file? */
333460ce86c7SWei Liu     if (donttouch_stat(&v9stat)) {
333560ce86c7SWei Liu         err = v9fs_co_fsync(pdu, fidp, 0);
333660ce86c7SWei Liu         goto out;
333760ce86c7SWei Liu     }
333860ce86c7SWei Liu     if (v9stat.mode != -1) {
333960ce86c7SWei Liu         uint32_t v9_mode;
334060ce86c7SWei Liu         err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
334160ce86c7SWei Liu         if (err < 0) {
334260ce86c7SWei Liu             goto out;
334360ce86c7SWei Liu         }
334460ce86c7SWei Liu         v9_mode = stat_to_v9mode(&stbuf);
334560ce86c7SWei Liu         if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
334660ce86c7SWei Liu             (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
334760ce86c7SWei Liu             /* Attempting to change the type */
334860ce86c7SWei Liu             err = -EIO;
334960ce86c7SWei Liu             goto out;
335060ce86c7SWei Liu         }
335160ce86c7SWei Liu         err = v9fs_co_chmod(pdu, &fidp->path,
335260ce86c7SWei Liu                             v9mode_to_mode(v9stat.mode,
335360ce86c7SWei Liu                                            &v9stat.extension));
335460ce86c7SWei Liu         if (err < 0) {
335560ce86c7SWei Liu             goto out;
335660ce86c7SWei Liu         }
335760ce86c7SWei Liu     }
335860ce86c7SWei Liu     if (v9stat.mtime != -1 || v9stat.atime != -1) {
335960ce86c7SWei Liu         struct timespec times[2];
336060ce86c7SWei Liu         if (v9stat.atime != -1) {
336160ce86c7SWei Liu             times[0].tv_sec = v9stat.atime;
336260ce86c7SWei Liu             times[0].tv_nsec = 0;
336360ce86c7SWei Liu         } else {
336460ce86c7SWei Liu             times[0].tv_nsec = UTIME_OMIT;
336560ce86c7SWei Liu         }
336660ce86c7SWei Liu         if (v9stat.mtime != -1) {
336760ce86c7SWei Liu             times[1].tv_sec = v9stat.mtime;
336860ce86c7SWei Liu             times[1].tv_nsec = 0;
336960ce86c7SWei Liu         } else {
337060ce86c7SWei Liu             times[1].tv_nsec = UTIME_OMIT;
337160ce86c7SWei Liu         }
337260ce86c7SWei Liu         err = v9fs_co_utimensat(pdu, &fidp->path, times);
337360ce86c7SWei Liu         if (err < 0) {
337460ce86c7SWei Liu             goto out;
337560ce86c7SWei Liu         }
337660ce86c7SWei Liu     }
337760ce86c7SWei Liu     if (v9stat.n_gid != -1 || v9stat.n_uid != -1) {
337860ce86c7SWei Liu         err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid);
337960ce86c7SWei Liu         if (err < 0) {
338060ce86c7SWei Liu             goto out;
338160ce86c7SWei Liu         }
338260ce86c7SWei Liu     }
338360ce86c7SWei Liu     if (v9stat.name.size != 0) {
33841d203986SGreg Kurz         v9fs_path_write_lock(s);
338560ce86c7SWei Liu         err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name);
33861d203986SGreg Kurz         v9fs_path_unlock(s);
338760ce86c7SWei Liu         if (err < 0) {
338860ce86c7SWei Liu             goto out;
338960ce86c7SWei Liu         }
339060ce86c7SWei Liu     }
339160ce86c7SWei Liu     if (v9stat.length != -1) {
339260ce86c7SWei Liu         err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length);
339360ce86c7SWei Liu         if (err < 0) {
339460ce86c7SWei Liu             goto out;
339560ce86c7SWei Liu         }
339660ce86c7SWei Liu     }
339760ce86c7SWei Liu     err = offset;
339860ce86c7SWei Liu out:
339960ce86c7SWei Liu     put_fid(pdu, fidp);
340060ce86c7SWei Liu out_nofid:
340160ce86c7SWei Liu     v9fs_stat_free(&v9stat);
340260ce86c7SWei Liu     pdu_complete(pdu, err);
340360ce86c7SWei Liu }
340460ce86c7SWei Liu 
340560ce86c7SWei Liu static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf)
340660ce86c7SWei Liu {
340760ce86c7SWei Liu     uint32_t f_type;
340860ce86c7SWei Liu     uint32_t f_bsize;
340960ce86c7SWei Liu     uint64_t f_blocks;
341060ce86c7SWei Liu     uint64_t f_bfree;
341160ce86c7SWei Liu     uint64_t f_bavail;
341260ce86c7SWei Liu     uint64_t f_files;
341360ce86c7SWei Liu     uint64_t f_ffree;
341460ce86c7SWei Liu     uint64_t fsid_val;
341560ce86c7SWei Liu     uint32_t f_namelen;
341660ce86c7SWei Liu     size_t offset = 7;
341760ce86c7SWei Liu     int32_t bsize_factor;
341860ce86c7SWei Liu 
341960ce86c7SWei Liu     /*
342060ce86c7SWei Liu      * compute bsize factor based on host file system block size
342160ce86c7SWei Liu      * and client msize
342260ce86c7SWei Liu      */
342360ce86c7SWei Liu     bsize_factor = (s->msize - P9_IOHDRSZ)/stbuf->f_bsize;
342460ce86c7SWei Liu     if (!bsize_factor) {
342560ce86c7SWei Liu         bsize_factor = 1;
342660ce86c7SWei Liu     }
342760ce86c7SWei Liu     f_type  = stbuf->f_type;
342860ce86c7SWei Liu     f_bsize = stbuf->f_bsize;
342960ce86c7SWei Liu     f_bsize *= bsize_factor;
343060ce86c7SWei Liu     /*
343160ce86c7SWei Liu      * f_bsize is adjusted(multiplied) by bsize factor, so we need to
343260ce86c7SWei Liu      * adjust(divide) the number of blocks, free blocks and available
343360ce86c7SWei Liu      * blocks by bsize factor
343460ce86c7SWei Liu      */
343560ce86c7SWei Liu     f_blocks = stbuf->f_blocks/bsize_factor;
343660ce86c7SWei Liu     f_bfree  = stbuf->f_bfree/bsize_factor;
343760ce86c7SWei Liu     f_bavail = stbuf->f_bavail/bsize_factor;
343860ce86c7SWei Liu     f_files  = stbuf->f_files;
343960ce86c7SWei Liu     f_ffree  = stbuf->f_ffree;
344060ce86c7SWei Liu     fsid_val = (unsigned int) stbuf->f_fsid.__val[0] |
344160ce86c7SWei Liu                (unsigned long long)stbuf->f_fsid.__val[1] << 32;
344260ce86c7SWei Liu     f_namelen = stbuf->f_namelen;
344360ce86c7SWei Liu 
344460ce86c7SWei Liu     return pdu_marshal(pdu, offset, "ddqqqqqqd",
344560ce86c7SWei Liu                        f_type, f_bsize, f_blocks, f_bfree,
344660ce86c7SWei Liu                        f_bavail, f_files, f_ffree,
344760ce86c7SWei Liu                        fsid_val, f_namelen);
344860ce86c7SWei Liu }
344960ce86c7SWei Liu 
34508440e22eSGreg Kurz static void coroutine_fn v9fs_statfs(void *opaque)
345160ce86c7SWei Liu {
345260ce86c7SWei Liu     int32_t fid;
345360ce86c7SWei Liu     ssize_t retval = 0;
345460ce86c7SWei Liu     size_t offset = 7;
345560ce86c7SWei Liu     V9fsFidState *fidp;
345660ce86c7SWei Liu     struct statfs stbuf;
345760ce86c7SWei Liu     V9fsPDU *pdu = opaque;
345860ce86c7SWei Liu     V9fsState *s = pdu->s;
345960ce86c7SWei Liu 
346060ce86c7SWei Liu     retval = pdu_unmarshal(pdu, offset, "d", &fid);
346160ce86c7SWei Liu     if (retval < 0) {
346260ce86c7SWei Liu         goto out_nofid;
346360ce86c7SWei Liu     }
346460ce86c7SWei Liu     fidp = get_fid(pdu, fid);
346560ce86c7SWei Liu     if (fidp == NULL) {
346660ce86c7SWei Liu         retval = -ENOENT;
346760ce86c7SWei Liu         goto out_nofid;
346860ce86c7SWei Liu     }
346960ce86c7SWei Liu     retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf);
347060ce86c7SWei Liu     if (retval < 0) {
347160ce86c7SWei Liu         goto out;
347260ce86c7SWei Liu     }
347360ce86c7SWei Liu     retval = v9fs_fill_statfs(s, pdu, &stbuf);
347460ce86c7SWei Liu     if (retval < 0) {
347560ce86c7SWei Liu         goto out;
347660ce86c7SWei Liu     }
347760ce86c7SWei Liu     retval += offset;
347860ce86c7SWei Liu out:
347960ce86c7SWei Liu     put_fid(pdu, fidp);
348060ce86c7SWei Liu out_nofid:
348160ce86c7SWei Liu     pdu_complete(pdu, retval);
348260ce86c7SWei Liu }
348360ce86c7SWei Liu 
34848440e22eSGreg Kurz static void coroutine_fn v9fs_mknod(void *opaque)
348560ce86c7SWei Liu {
348660ce86c7SWei Liu 
348760ce86c7SWei Liu     int mode;
348860ce86c7SWei Liu     gid_t gid;
348960ce86c7SWei Liu     int32_t fid;
349060ce86c7SWei Liu     V9fsQID qid;
349160ce86c7SWei Liu     int err = 0;
349260ce86c7SWei Liu     int major, minor;
349360ce86c7SWei Liu     size_t offset = 7;
349460ce86c7SWei Liu     V9fsString name;
349560ce86c7SWei Liu     struct stat stbuf;
349660ce86c7SWei Liu     V9fsFidState *fidp;
349760ce86c7SWei Liu     V9fsPDU *pdu = opaque;
349860ce86c7SWei Liu 
349960ce86c7SWei Liu     v9fs_string_init(&name);
350060ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode,
350160ce86c7SWei Liu                         &major, &minor, &gid);
350260ce86c7SWei Liu     if (err < 0) {
350360ce86c7SWei Liu         goto out_nofid;
350460ce86c7SWei Liu     }
350560ce86c7SWei Liu     trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor);
350660ce86c7SWei Liu 
3507fff39a7aSGreg Kurz     if (name_is_illegal(name.data)) {
3508fff39a7aSGreg Kurz         err = -ENOENT;
3509fff39a7aSGreg Kurz         goto out_nofid;
3510fff39a7aSGreg Kurz     }
3511fff39a7aSGreg Kurz 
3512805b5d98SGreg Kurz     if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3513805b5d98SGreg Kurz         err = -EEXIST;
3514805b5d98SGreg Kurz         goto out_nofid;
3515805b5d98SGreg Kurz     }
3516805b5d98SGreg Kurz 
351760ce86c7SWei Liu     fidp = get_fid(pdu, fid);
351860ce86c7SWei Liu     if (fidp == NULL) {
351960ce86c7SWei Liu         err = -ENOENT;
352060ce86c7SWei Liu         goto out_nofid;
352160ce86c7SWei Liu     }
352260ce86c7SWei Liu     err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid,
352360ce86c7SWei Liu                         makedev(major, minor), mode, &stbuf);
352460ce86c7SWei Liu     if (err < 0) {
352560ce86c7SWei Liu         goto out;
352660ce86c7SWei Liu     }
35273b5ee9e8SAntonios Motakis     err = stat_to_qid(pdu, &stbuf, &qid);
35283b5ee9e8SAntonios Motakis     if (err < 0) {
35293b5ee9e8SAntonios Motakis         goto out;
35303b5ee9e8SAntonios Motakis     }
353160ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "Q", &qid);
353260ce86c7SWei Liu     if (err < 0) {
353360ce86c7SWei Liu         goto out;
353460ce86c7SWei Liu     }
353560ce86c7SWei Liu     err += offset;
353660ce86c7SWei Liu     trace_v9fs_mknod_return(pdu->tag, pdu->id,
353760ce86c7SWei Liu                             qid.type, qid.version, qid.path);
353860ce86c7SWei Liu out:
353960ce86c7SWei Liu     put_fid(pdu, fidp);
354060ce86c7SWei Liu out_nofid:
354160ce86c7SWei Liu     pdu_complete(pdu, err);
354260ce86c7SWei Liu     v9fs_string_free(&name);
354360ce86c7SWei Liu }
354460ce86c7SWei Liu 
354560ce86c7SWei Liu /*
354660ce86c7SWei Liu  * Implement posix byte range locking code
354760ce86c7SWei Liu  * Server side handling of locking code is very simple, because 9p server in
354860ce86c7SWei Liu  * QEMU can handle only one client. And most of the lock handling
354960ce86c7SWei Liu  * (like conflict, merging) etc is done by the VFS layer itself, so no need to
355060ce86c7SWei Liu  * do any thing in * qemu 9p server side lock code path.
355160ce86c7SWei Liu  * So when a TLOCK request comes, always return success
355260ce86c7SWei Liu  */
35538440e22eSGreg Kurz static void coroutine_fn v9fs_lock(void *opaque)
355460ce86c7SWei Liu {
355560ce86c7SWei Liu     V9fsFlock flock;
355660ce86c7SWei Liu     size_t offset = 7;
355760ce86c7SWei Liu     struct stat stbuf;
355860ce86c7SWei Liu     V9fsFidState *fidp;
355960ce86c7SWei Liu     int32_t fid, err = 0;
356060ce86c7SWei Liu     V9fsPDU *pdu = opaque;
356160ce86c7SWei Liu 
356260ce86c7SWei Liu     v9fs_string_init(&flock.client_id);
356360ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type,
356460ce86c7SWei Liu                         &flock.flags, &flock.start, &flock.length,
356560ce86c7SWei Liu                         &flock.proc_id, &flock.client_id);
356660ce86c7SWei Liu     if (err < 0) {
356760ce86c7SWei Liu         goto out_nofid;
356860ce86c7SWei Liu     }
356960ce86c7SWei Liu     trace_v9fs_lock(pdu->tag, pdu->id, fid,
357060ce86c7SWei Liu                     flock.type, flock.start, flock.length);
357160ce86c7SWei Liu 
357260ce86c7SWei Liu 
357360ce86c7SWei Liu     /* We support only block flag now (that too ignored currently) */
357460ce86c7SWei Liu     if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) {
357560ce86c7SWei Liu         err = -EINVAL;
357660ce86c7SWei Liu         goto out_nofid;
357760ce86c7SWei Liu     }
357860ce86c7SWei Liu     fidp = get_fid(pdu, fid);
357960ce86c7SWei Liu     if (fidp == NULL) {
358060ce86c7SWei Liu         err = -ENOENT;
358160ce86c7SWei Liu         goto out_nofid;
358260ce86c7SWei Liu     }
358360ce86c7SWei Liu     err = v9fs_co_fstat(pdu, fidp, &stbuf);
358460ce86c7SWei Liu     if (err < 0) {
358560ce86c7SWei Liu         goto out;
358660ce86c7SWei Liu     }
35874bae2b39SPaolo Bonzini     err = pdu_marshal(pdu, offset, "b", P9_LOCK_SUCCESS);
35884bae2b39SPaolo Bonzini     if (err < 0) {
35894bae2b39SPaolo Bonzini         goto out;
35904bae2b39SPaolo Bonzini     }
35914bae2b39SPaolo Bonzini     err += offset;
35924bae2b39SPaolo Bonzini     trace_v9fs_lock_return(pdu->tag, pdu->id, P9_LOCK_SUCCESS);
359360ce86c7SWei Liu out:
359460ce86c7SWei Liu     put_fid(pdu, fidp);
359560ce86c7SWei Liu out_nofid:
359660ce86c7SWei Liu     pdu_complete(pdu, err);
359760ce86c7SWei Liu     v9fs_string_free(&flock.client_id);
359860ce86c7SWei Liu }
359960ce86c7SWei Liu 
360060ce86c7SWei Liu /*
360160ce86c7SWei Liu  * When a TGETLOCK request comes, always return success because all lock
360260ce86c7SWei Liu  * handling is done by client's VFS layer.
360360ce86c7SWei Liu  */
36048440e22eSGreg Kurz static void coroutine_fn v9fs_getlock(void *opaque)
360560ce86c7SWei Liu {
360660ce86c7SWei Liu     size_t offset = 7;
360760ce86c7SWei Liu     struct stat stbuf;
360860ce86c7SWei Liu     V9fsFidState *fidp;
360960ce86c7SWei Liu     V9fsGetlock glock;
361060ce86c7SWei Liu     int32_t fid, err = 0;
361160ce86c7SWei Liu     V9fsPDU *pdu = opaque;
361260ce86c7SWei Liu 
361360ce86c7SWei Liu     v9fs_string_init(&glock.client_id);
361460ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type,
361560ce86c7SWei Liu                         &glock.start, &glock.length, &glock.proc_id,
361660ce86c7SWei Liu                         &glock.client_id);
361760ce86c7SWei Liu     if (err < 0) {
361860ce86c7SWei Liu         goto out_nofid;
361960ce86c7SWei Liu     }
362060ce86c7SWei Liu     trace_v9fs_getlock(pdu->tag, pdu->id, fid,
362160ce86c7SWei Liu                        glock.type, glock.start, glock.length);
362260ce86c7SWei Liu 
362360ce86c7SWei Liu     fidp = get_fid(pdu, fid);
362460ce86c7SWei Liu     if (fidp == NULL) {
362560ce86c7SWei Liu         err = -ENOENT;
362660ce86c7SWei Liu         goto out_nofid;
362760ce86c7SWei Liu     }
362860ce86c7SWei Liu     err = v9fs_co_fstat(pdu, fidp, &stbuf);
362960ce86c7SWei Liu     if (err < 0) {
363060ce86c7SWei Liu         goto out;
363160ce86c7SWei Liu     }
363260ce86c7SWei Liu     glock.type = P9_LOCK_TYPE_UNLCK;
363360ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "bqqds", glock.type,
363460ce86c7SWei Liu                           glock.start, glock.length, glock.proc_id,
363560ce86c7SWei Liu                           &glock.client_id);
363660ce86c7SWei Liu     if (err < 0) {
363760ce86c7SWei Liu         goto out;
363860ce86c7SWei Liu     }
363960ce86c7SWei Liu     err += offset;
364060ce86c7SWei Liu     trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start,
364160ce86c7SWei Liu                               glock.length, glock.proc_id);
364260ce86c7SWei Liu out:
364360ce86c7SWei Liu     put_fid(pdu, fidp);
364460ce86c7SWei Liu out_nofid:
364560ce86c7SWei Liu     pdu_complete(pdu, err);
364660ce86c7SWei Liu     v9fs_string_free(&glock.client_id);
364760ce86c7SWei Liu }
364860ce86c7SWei Liu 
36498440e22eSGreg Kurz static void coroutine_fn v9fs_mkdir(void *opaque)
365060ce86c7SWei Liu {
365160ce86c7SWei Liu     V9fsPDU *pdu = opaque;
365260ce86c7SWei Liu     size_t offset = 7;
365360ce86c7SWei Liu     int32_t fid;
365460ce86c7SWei Liu     struct stat stbuf;
365560ce86c7SWei Liu     V9fsQID qid;
365660ce86c7SWei Liu     V9fsString name;
365760ce86c7SWei Liu     V9fsFidState *fidp;
365860ce86c7SWei Liu     gid_t gid;
365960ce86c7SWei Liu     int mode;
366060ce86c7SWei Liu     int err = 0;
366160ce86c7SWei Liu 
366260ce86c7SWei Liu     v9fs_string_init(&name);
366360ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid);
366460ce86c7SWei Liu     if (err < 0) {
366560ce86c7SWei Liu         goto out_nofid;
366660ce86c7SWei Liu     }
366760ce86c7SWei Liu     trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid);
366860ce86c7SWei Liu 
3669fff39a7aSGreg Kurz     if (name_is_illegal(name.data)) {
3670fff39a7aSGreg Kurz         err = -ENOENT;
3671fff39a7aSGreg Kurz         goto out_nofid;
3672fff39a7aSGreg Kurz     }
3673fff39a7aSGreg Kurz 
3674805b5d98SGreg Kurz     if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3675805b5d98SGreg Kurz         err = -EEXIST;
3676805b5d98SGreg Kurz         goto out_nofid;
3677805b5d98SGreg Kurz     }
3678805b5d98SGreg Kurz 
367960ce86c7SWei Liu     fidp = get_fid(pdu, fid);
368060ce86c7SWei Liu     if (fidp == NULL) {
368160ce86c7SWei Liu         err = -ENOENT;
368260ce86c7SWei Liu         goto out_nofid;
368360ce86c7SWei Liu     }
368460ce86c7SWei Liu     err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf);
368560ce86c7SWei Liu     if (err < 0) {
368660ce86c7SWei Liu         goto out;
368760ce86c7SWei Liu     }
36883b5ee9e8SAntonios Motakis     err = stat_to_qid(pdu, &stbuf, &qid);
36893b5ee9e8SAntonios Motakis     if (err < 0) {
36903b5ee9e8SAntonios Motakis         goto out;
36913b5ee9e8SAntonios Motakis     }
369260ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "Q", &qid);
369360ce86c7SWei Liu     if (err < 0) {
369460ce86c7SWei Liu         goto out;
369560ce86c7SWei Liu     }
369660ce86c7SWei Liu     err += offset;
369760ce86c7SWei Liu     trace_v9fs_mkdir_return(pdu->tag, pdu->id,
369860ce86c7SWei Liu                             qid.type, qid.version, qid.path, err);
369960ce86c7SWei Liu out:
370060ce86c7SWei Liu     put_fid(pdu, fidp);
370160ce86c7SWei Liu out_nofid:
370260ce86c7SWei Liu     pdu_complete(pdu, err);
370360ce86c7SWei Liu     v9fs_string_free(&name);
370460ce86c7SWei Liu }
370560ce86c7SWei Liu 
37068440e22eSGreg Kurz static void coroutine_fn v9fs_xattrwalk(void *opaque)
370760ce86c7SWei Liu {
370860ce86c7SWei Liu     int64_t size;
370960ce86c7SWei Liu     V9fsString name;
371060ce86c7SWei Liu     ssize_t err = 0;
371160ce86c7SWei Liu     size_t offset = 7;
371260ce86c7SWei Liu     int32_t fid, newfid;
371360ce86c7SWei Liu     V9fsFidState *file_fidp;
371460ce86c7SWei Liu     V9fsFidState *xattr_fidp = NULL;
371560ce86c7SWei Liu     V9fsPDU *pdu = opaque;
371660ce86c7SWei Liu     V9fsState *s = pdu->s;
371760ce86c7SWei Liu 
371860ce86c7SWei Liu     v9fs_string_init(&name);
371960ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name);
372060ce86c7SWei Liu     if (err < 0) {
372160ce86c7SWei Liu         goto out_nofid;
372260ce86c7SWei Liu     }
372360ce86c7SWei Liu     trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data);
372460ce86c7SWei Liu 
372560ce86c7SWei Liu     file_fidp = get_fid(pdu, fid);
372660ce86c7SWei Liu     if (file_fidp == NULL) {
372760ce86c7SWei Liu         err = -ENOENT;
372860ce86c7SWei Liu         goto out_nofid;
372960ce86c7SWei Liu     }
373060ce86c7SWei Liu     xattr_fidp = alloc_fid(s, newfid);
373160ce86c7SWei Liu     if (xattr_fidp == NULL) {
373260ce86c7SWei Liu         err = -EINVAL;
373360ce86c7SWei Liu         goto out;
373460ce86c7SWei Liu     }
373560ce86c7SWei Liu     v9fs_path_copy(&xattr_fidp->path, &file_fidp->path);
3736ba42ebb8SLi Qiang     if (!v9fs_string_size(&name)) {
373760ce86c7SWei Liu         /*
373860ce86c7SWei Liu          * listxattr request. Get the size first
373960ce86c7SWei Liu          */
374060ce86c7SWei Liu         size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0);
374160ce86c7SWei Liu         if (size < 0) {
374260ce86c7SWei Liu             err = size;
374360ce86c7SWei Liu             clunk_fid(s, xattr_fidp->fid);
374460ce86c7SWei Liu             goto out;
374560ce86c7SWei Liu         }
374660ce86c7SWei Liu         /*
374760ce86c7SWei Liu          * Read the xattr value
374860ce86c7SWei Liu          */
374960ce86c7SWei Liu         xattr_fidp->fs.xattr.len = size;
375060ce86c7SWei Liu         xattr_fidp->fid_type = P9_FID_XATTR;
3751dd28fbbcSLi Qiang         xattr_fidp->fs.xattr.xattrwalk_fid = true;
37527bd92756SPrasad J Pandit         xattr_fidp->fs.xattr.value = g_malloc0(size);
3753a647502cSKeno Fischer         if (size) {
375460ce86c7SWei Liu             err = v9fs_co_llistxattr(pdu, &xattr_fidp->path,
375560ce86c7SWei Liu                                      xattr_fidp->fs.xattr.value,
375660ce86c7SWei Liu                                      xattr_fidp->fs.xattr.len);
375760ce86c7SWei Liu             if (err < 0) {
375860ce86c7SWei Liu                 clunk_fid(s, xattr_fidp->fid);
375960ce86c7SWei Liu                 goto out;
376060ce86c7SWei Liu             }
376160ce86c7SWei Liu         }
376260ce86c7SWei Liu         err = pdu_marshal(pdu, offset, "q", size);
376360ce86c7SWei Liu         if (err < 0) {
376460ce86c7SWei Liu             goto out;
376560ce86c7SWei Liu         }
376660ce86c7SWei Liu         err += offset;
376760ce86c7SWei Liu     } else {
376860ce86c7SWei Liu         /*
376960ce86c7SWei Liu          * specific xattr fid. We check for xattr
377060ce86c7SWei Liu          * presence also collect the xattr size
377160ce86c7SWei Liu          */
377260ce86c7SWei Liu         size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
377360ce86c7SWei Liu                                  &name, NULL, 0);
377460ce86c7SWei Liu         if (size < 0) {
377560ce86c7SWei Liu             err = size;
377660ce86c7SWei Liu             clunk_fid(s, xattr_fidp->fid);
377760ce86c7SWei Liu             goto out;
377860ce86c7SWei Liu         }
377960ce86c7SWei Liu         /*
378060ce86c7SWei Liu          * Read the xattr value
378160ce86c7SWei Liu          */
378260ce86c7SWei Liu         xattr_fidp->fs.xattr.len = size;
378360ce86c7SWei Liu         xattr_fidp->fid_type = P9_FID_XATTR;
3784dd28fbbcSLi Qiang         xattr_fidp->fs.xattr.xattrwalk_fid = true;
37857bd92756SPrasad J Pandit         xattr_fidp->fs.xattr.value = g_malloc0(size);
3786a647502cSKeno Fischer         if (size) {
378760ce86c7SWei Liu             err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
378860ce86c7SWei Liu                                     &name, xattr_fidp->fs.xattr.value,
378960ce86c7SWei Liu                                     xattr_fidp->fs.xattr.len);
379060ce86c7SWei Liu             if (err < 0) {
379160ce86c7SWei Liu                 clunk_fid(s, xattr_fidp->fid);
379260ce86c7SWei Liu                 goto out;
379360ce86c7SWei Liu             }
379460ce86c7SWei Liu         }
379560ce86c7SWei Liu         err = pdu_marshal(pdu, offset, "q", size);
379660ce86c7SWei Liu         if (err < 0) {
379760ce86c7SWei Liu             goto out;
379860ce86c7SWei Liu         }
379960ce86c7SWei Liu         err += offset;
380060ce86c7SWei Liu     }
380160ce86c7SWei Liu     trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size);
380260ce86c7SWei Liu out:
380360ce86c7SWei Liu     put_fid(pdu, file_fidp);
380460ce86c7SWei Liu     if (xattr_fidp) {
380560ce86c7SWei Liu         put_fid(pdu, xattr_fidp);
380660ce86c7SWei Liu     }
380760ce86c7SWei Liu out_nofid:
380860ce86c7SWei Liu     pdu_complete(pdu, err);
380960ce86c7SWei Liu     v9fs_string_free(&name);
381060ce86c7SWei Liu }
381160ce86c7SWei Liu 
38128440e22eSGreg Kurz static void coroutine_fn v9fs_xattrcreate(void *opaque)
381360ce86c7SWei Liu {
3814aca6897fSKeno Fischer     int flags, rflags = 0;
381560ce86c7SWei Liu     int32_t fid;
38163b79ef2cSGreg Kurz     uint64_t size;
381760ce86c7SWei Liu     ssize_t err = 0;
381860ce86c7SWei Liu     V9fsString name;
381960ce86c7SWei Liu     size_t offset = 7;
382060ce86c7SWei Liu     V9fsFidState *file_fidp;
382160ce86c7SWei Liu     V9fsFidState *xattr_fidp;
382260ce86c7SWei Liu     V9fsPDU *pdu = opaque;
382360ce86c7SWei Liu 
382460ce86c7SWei Liu     v9fs_string_init(&name);
382560ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags);
382660ce86c7SWei Liu     if (err < 0) {
382760ce86c7SWei Liu         goto out_nofid;
382860ce86c7SWei Liu     }
382960ce86c7SWei Liu     trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags);
383060ce86c7SWei Liu 
3831aca6897fSKeno Fischer     if (flags & ~(P9_XATTR_CREATE | P9_XATTR_REPLACE)) {
3832aca6897fSKeno Fischer         err = -EINVAL;
3833aca6897fSKeno Fischer         goto out_nofid;
3834aca6897fSKeno Fischer     }
3835aca6897fSKeno Fischer 
3836aca6897fSKeno Fischer     if (flags & P9_XATTR_CREATE) {
3837aca6897fSKeno Fischer         rflags |= XATTR_CREATE;
3838aca6897fSKeno Fischer     }
3839aca6897fSKeno Fischer 
3840aca6897fSKeno Fischer     if (flags & P9_XATTR_REPLACE) {
3841aca6897fSKeno Fischer         rflags |= XATTR_REPLACE;
3842aca6897fSKeno Fischer     }
3843aca6897fSKeno Fischer 
38443b79ef2cSGreg Kurz     if (size > XATTR_SIZE_MAX) {
38453b79ef2cSGreg Kurz         err = -E2BIG;
38463b79ef2cSGreg Kurz         goto out_nofid;
38473b79ef2cSGreg Kurz     }
38483b79ef2cSGreg Kurz 
384960ce86c7SWei Liu     file_fidp = get_fid(pdu, fid);
385060ce86c7SWei Liu     if (file_fidp == NULL) {
385160ce86c7SWei Liu         err = -EINVAL;
385260ce86c7SWei Liu         goto out_nofid;
385360ce86c7SWei Liu     }
3854dd654e03SGreg Kurz     if (file_fidp->fid_type != P9_FID_NONE) {
3855dd654e03SGreg Kurz         err = -EINVAL;
3856dd654e03SGreg Kurz         goto out_put_fid;
3857dd654e03SGreg Kurz     }
3858dd654e03SGreg Kurz 
385960ce86c7SWei Liu     /* Make the file fid point to xattr */
386060ce86c7SWei Liu     xattr_fidp = file_fidp;
386160ce86c7SWei Liu     xattr_fidp->fid_type = P9_FID_XATTR;
386260ce86c7SWei Liu     xattr_fidp->fs.xattr.copied_len = 0;
3863dd28fbbcSLi Qiang     xattr_fidp->fs.xattr.xattrwalk_fid = false;
386460ce86c7SWei Liu     xattr_fidp->fs.xattr.len = size;
3865aca6897fSKeno Fischer     xattr_fidp->fs.xattr.flags = rflags;
386660ce86c7SWei Liu     v9fs_string_init(&xattr_fidp->fs.xattr.name);
386760ce86c7SWei Liu     v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
3868eb687602SLi Qiang     xattr_fidp->fs.xattr.value = g_malloc0(size);
386960ce86c7SWei Liu     err = offset;
3870dd654e03SGreg Kurz out_put_fid:
387160ce86c7SWei Liu     put_fid(pdu, file_fidp);
387260ce86c7SWei Liu out_nofid:
387360ce86c7SWei Liu     pdu_complete(pdu, err);
387460ce86c7SWei Liu     v9fs_string_free(&name);
387560ce86c7SWei Liu }
387660ce86c7SWei Liu 
38778440e22eSGreg Kurz static void coroutine_fn v9fs_readlink(void *opaque)
387860ce86c7SWei Liu {
387960ce86c7SWei Liu     V9fsPDU *pdu = opaque;
388060ce86c7SWei Liu     size_t offset = 7;
388160ce86c7SWei Liu     V9fsString target;
388260ce86c7SWei Liu     int32_t fid;
388360ce86c7SWei Liu     int err = 0;
388460ce86c7SWei Liu     V9fsFidState *fidp;
388560ce86c7SWei Liu 
388660ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "d", &fid);
388760ce86c7SWei Liu     if (err < 0) {
388860ce86c7SWei Liu         goto out_nofid;
388960ce86c7SWei Liu     }
389060ce86c7SWei Liu     trace_v9fs_readlink(pdu->tag, pdu->id, fid);
389160ce86c7SWei Liu     fidp = get_fid(pdu, fid);
389260ce86c7SWei Liu     if (fidp == NULL) {
389360ce86c7SWei Liu         err = -ENOENT;
389460ce86c7SWei Liu         goto out_nofid;
389560ce86c7SWei Liu     }
389660ce86c7SWei Liu 
389760ce86c7SWei Liu     v9fs_string_init(&target);
389860ce86c7SWei Liu     err = v9fs_co_readlink(pdu, &fidp->path, &target);
389960ce86c7SWei Liu     if (err < 0) {
390060ce86c7SWei Liu         goto out;
390160ce86c7SWei Liu     }
390260ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "s", &target);
390360ce86c7SWei Liu     if (err < 0) {
390460ce86c7SWei Liu         v9fs_string_free(&target);
390560ce86c7SWei Liu         goto out;
390660ce86c7SWei Liu     }
390760ce86c7SWei Liu     err += offset;
390860ce86c7SWei Liu     trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data);
390960ce86c7SWei Liu     v9fs_string_free(&target);
391060ce86c7SWei Liu out:
391160ce86c7SWei Liu     put_fid(pdu, fidp);
391260ce86c7SWei Liu out_nofid:
391360ce86c7SWei Liu     pdu_complete(pdu, err);
391460ce86c7SWei Liu }
391560ce86c7SWei Liu 
391660ce86c7SWei Liu static CoroutineEntry *pdu_co_handlers[] = {
391760ce86c7SWei Liu     [P9_TREADDIR] = v9fs_readdir,
391860ce86c7SWei Liu     [P9_TSTATFS] = v9fs_statfs,
391960ce86c7SWei Liu     [P9_TGETATTR] = v9fs_getattr,
392060ce86c7SWei Liu     [P9_TSETATTR] = v9fs_setattr,
392160ce86c7SWei Liu     [P9_TXATTRWALK] = v9fs_xattrwalk,
392260ce86c7SWei Liu     [P9_TXATTRCREATE] = v9fs_xattrcreate,
392360ce86c7SWei Liu     [P9_TMKNOD] = v9fs_mknod,
392460ce86c7SWei Liu     [P9_TRENAME] = v9fs_rename,
392560ce86c7SWei Liu     [P9_TLOCK] = v9fs_lock,
392660ce86c7SWei Liu     [P9_TGETLOCK] = v9fs_getlock,
392760ce86c7SWei Liu     [P9_TRENAMEAT] = v9fs_renameat,
392860ce86c7SWei Liu     [P9_TREADLINK] = v9fs_readlink,
392960ce86c7SWei Liu     [P9_TUNLINKAT] = v9fs_unlinkat,
393060ce86c7SWei Liu     [P9_TMKDIR] = v9fs_mkdir,
393160ce86c7SWei Liu     [P9_TVERSION] = v9fs_version,
393260ce86c7SWei Liu     [P9_TLOPEN] = v9fs_open,
393360ce86c7SWei Liu     [P9_TATTACH] = v9fs_attach,
393460ce86c7SWei Liu     [P9_TSTAT] = v9fs_stat,
393560ce86c7SWei Liu     [P9_TWALK] = v9fs_walk,
393660ce86c7SWei Liu     [P9_TCLUNK] = v9fs_clunk,
393760ce86c7SWei Liu     [P9_TFSYNC] = v9fs_fsync,
393860ce86c7SWei Liu     [P9_TOPEN] = v9fs_open,
393960ce86c7SWei Liu     [P9_TREAD] = v9fs_read,
394060ce86c7SWei Liu #if 0
394160ce86c7SWei Liu     [P9_TAUTH] = v9fs_auth,
394260ce86c7SWei Liu #endif
394360ce86c7SWei Liu     [P9_TFLUSH] = v9fs_flush,
394460ce86c7SWei Liu     [P9_TLINK] = v9fs_link,
394560ce86c7SWei Liu     [P9_TSYMLINK] = v9fs_symlink,
394660ce86c7SWei Liu     [P9_TCREATE] = v9fs_create,
394760ce86c7SWei Liu     [P9_TLCREATE] = v9fs_lcreate,
394860ce86c7SWei Liu     [P9_TWRITE] = v9fs_write,
394960ce86c7SWei Liu     [P9_TWSTAT] = v9fs_wstat,
395060ce86c7SWei Liu     [P9_TREMOVE] = v9fs_remove,
395160ce86c7SWei Liu };
395260ce86c7SWei Liu 
39538440e22eSGreg Kurz static void coroutine_fn v9fs_op_not_supp(void *opaque)
395460ce86c7SWei Liu {
395560ce86c7SWei Liu     V9fsPDU *pdu = opaque;
395660ce86c7SWei Liu     pdu_complete(pdu, -EOPNOTSUPP);
395760ce86c7SWei Liu }
395860ce86c7SWei Liu 
39598440e22eSGreg Kurz static void coroutine_fn v9fs_fs_ro(void *opaque)
396060ce86c7SWei Liu {
396160ce86c7SWei Liu     V9fsPDU *pdu = opaque;
396260ce86c7SWei Liu     pdu_complete(pdu, -EROFS);
396360ce86c7SWei Liu }
396460ce86c7SWei Liu 
396560ce86c7SWei Liu static inline bool is_read_only_op(V9fsPDU *pdu)
396660ce86c7SWei Liu {
396760ce86c7SWei Liu     switch (pdu->id) {
396860ce86c7SWei Liu     case P9_TREADDIR:
396960ce86c7SWei Liu     case P9_TSTATFS:
397060ce86c7SWei Liu     case P9_TGETATTR:
397160ce86c7SWei Liu     case P9_TXATTRWALK:
397260ce86c7SWei Liu     case P9_TLOCK:
397360ce86c7SWei Liu     case P9_TGETLOCK:
397460ce86c7SWei Liu     case P9_TREADLINK:
397560ce86c7SWei Liu     case P9_TVERSION:
397660ce86c7SWei Liu     case P9_TLOPEN:
397760ce86c7SWei Liu     case P9_TATTACH:
397860ce86c7SWei Liu     case P9_TSTAT:
397960ce86c7SWei Liu     case P9_TWALK:
398060ce86c7SWei Liu     case P9_TCLUNK:
398160ce86c7SWei Liu     case P9_TFSYNC:
398260ce86c7SWei Liu     case P9_TOPEN:
398360ce86c7SWei Liu     case P9_TREAD:
398460ce86c7SWei Liu     case P9_TAUTH:
398560ce86c7SWei Liu     case P9_TFLUSH:
398660ce86c7SWei Liu         return 1;
398760ce86c7SWei Liu     default:
398860ce86c7SWei Liu         return 0;
398960ce86c7SWei Liu     }
399060ce86c7SWei Liu }
399160ce86c7SWei Liu 
3992506f3275SGreg Kurz void pdu_submit(V9fsPDU *pdu, P9MsgHeader *hdr)
399360ce86c7SWei Liu {
399460ce86c7SWei Liu     Coroutine *co;
399560ce86c7SWei Liu     CoroutineEntry *handler;
399660ce86c7SWei Liu     V9fsState *s = pdu->s;
399760ce86c7SWei Liu 
3998506f3275SGreg Kurz     pdu->size = le32_to_cpu(hdr->size_le);
3999506f3275SGreg Kurz     pdu->id = hdr->id;
4000506f3275SGreg Kurz     pdu->tag = le16_to_cpu(hdr->tag_le);
4001506f3275SGreg Kurz 
400260ce86c7SWei Liu     if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) ||
400360ce86c7SWei Liu         (pdu_co_handlers[pdu->id] == NULL)) {
400460ce86c7SWei Liu         handler = v9fs_op_not_supp;
4005d1471233SGreg Kurz     } else if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) {
4006d1471233SGreg Kurz         handler = v9fs_fs_ro;
400760ce86c7SWei Liu     } else {
400860ce86c7SWei Liu         handler = pdu_co_handlers[pdu->id];
400960ce86c7SWei Liu     }
401060ce86c7SWei Liu 
4011506f3275SGreg Kurz     qemu_co_queue_init(&pdu->complete);
40120b8b8753SPaolo Bonzini     co = qemu_coroutine_create(handler, pdu);
40130b8b8753SPaolo Bonzini     qemu_coroutine_enter(co);
401460ce86c7SWei Liu }
401560ce86c7SWei Liu 
40162a0c56aaSWei Liu /* Returns 0 on success, 1 on failure. */
4017066eb006SGreg Kurz int v9fs_device_realize_common(V9fsState *s, const V9fsTransport *t,
4018066eb006SGreg Kurz                                Error **errp)
40192a0c56aaSWei Liu {
402092c45122SVladimir Sementsov-Ogievskiy     ERRP_GUARD();
40212a0c56aaSWei Liu     int i, len;
40222a0c56aaSWei Liu     struct stat stat;
40232a0c56aaSWei Liu     FsDriverEntry *fse;
40242a0c56aaSWei Liu     V9fsPath path;
40252a0c56aaSWei Liu     int rc = 1;
40262a0c56aaSWei Liu 
4027066eb006SGreg Kurz     assert(!s->transport);
4028066eb006SGreg Kurz     s->transport = t;
4029066eb006SGreg Kurz 
40302a0c56aaSWei Liu     /* initialize pdu allocator */
40312a0c56aaSWei Liu     QLIST_INIT(&s->free_list);
40322a0c56aaSWei Liu     QLIST_INIT(&s->active_list);
40330d78289cSGreg Kurz     for (i = 0; i < MAX_REQ; i++) {
4034583f21f8SStefano Stabellini         QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
4035583f21f8SStefano Stabellini         s->pdus[i].s = s;
4036583f21f8SStefano Stabellini         s->pdus[i].idx = i;
40372a0c56aaSWei Liu     }
40382a0c56aaSWei Liu 
40392a0c56aaSWei Liu     v9fs_path_init(&path);
40402a0c56aaSWei Liu 
40412a0c56aaSWei Liu     fse = get_fsdev_fsentry(s->fsconf.fsdev_id);
40422a0c56aaSWei Liu 
40432a0c56aaSWei Liu     if (!fse) {
40442a0c56aaSWei Liu         /* We don't have a fsdev identified by fsdev_id */
40452a0c56aaSWei Liu         error_setg(errp, "9pfs device couldn't find fsdev with the "
40462a0c56aaSWei Liu                    "id = %s",
40472a0c56aaSWei Liu                    s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL");
40482a0c56aaSWei Liu         goto out;
40492a0c56aaSWei Liu     }
40502a0c56aaSWei Liu 
40512a0c56aaSWei Liu     if (!s->fsconf.tag) {
40522a0c56aaSWei Liu         /* we haven't specified a mount_tag */
40532a0c56aaSWei Liu         error_setg(errp, "fsdev with id %s needs mount_tag arguments",
40542a0c56aaSWei Liu                    s->fsconf.fsdev_id);
40552a0c56aaSWei Liu         goto out;
40562a0c56aaSWei Liu     }
40572a0c56aaSWei Liu 
40582a0c56aaSWei Liu     s->ctx.export_flags = fse->export_flags;
40592a0c56aaSWei Liu     s->ctx.fs_root = g_strdup(fse->path);
40602a0c56aaSWei Liu     s->ctx.exops.get_st_gen = NULL;
40612a0c56aaSWei Liu     len = strlen(s->fsconf.tag);
40622a0c56aaSWei Liu     if (len > MAX_TAG_LEN - 1) {
40632a0c56aaSWei Liu         error_setg(errp, "mount tag '%s' (%d bytes) is longer than "
40642a0c56aaSWei Liu                    "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1);
40652a0c56aaSWei Liu         goto out;
40662a0c56aaSWei Liu     }
40672a0c56aaSWei Liu 
40682a0c56aaSWei Liu     s->tag = g_strdup(s->fsconf.tag);
40692a0c56aaSWei Liu     s->ctx.uid = -1;
40702a0c56aaSWei Liu 
40712a0c56aaSWei Liu     s->ops = fse->ops;
40722a0c56aaSWei Liu 
4073b96feb2cSTobias Schramm     s->ctx.fmode = fse->fmode;
4074b96feb2cSTobias Schramm     s->ctx.dmode = fse->dmode;
4075b96feb2cSTobias Schramm 
40762a0c56aaSWei Liu     s->fid_list = NULL;
40772a0c56aaSWei Liu     qemu_co_rwlock_init(&s->rename_lock);
40782a0c56aaSWei Liu 
407965603a80SGreg Kurz     if (s->ops->init(&s->ctx, errp) < 0) {
408065603a80SGreg Kurz         error_prepend(errp, "cannot initialize fsdev '%s': ",
408165603a80SGreg Kurz                       s->fsconf.fsdev_id);
40822a0c56aaSWei Liu         goto out;
40832a0c56aaSWei Liu     }
40842a0c56aaSWei Liu 
40852a0c56aaSWei Liu     /*
40862a0c56aaSWei Liu      * Check details of export path, We need to use fs driver
40872a0c56aaSWei Liu      * call back to do that. Since we are in the init path, we don't
40882a0c56aaSWei Liu      * use co-routines here.
40892a0c56aaSWei Liu      */
40902a0c56aaSWei Liu     if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) {
40912a0c56aaSWei Liu         error_setg(errp,
40922a0c56aaSWei Liu                    "error in converting name to path %s", strerror(errno));
40932a0c56aaSWei Liu         goto out;
40942a0c56aaSWei Liu     }
40952a0c56aaSWei Liu     if (s->ops->lstat(&s->ctx, &path, &stat)) {
40962a0c56aaSWei Liu         error_setg(errp, "share path %s does not exist", fse->path);
40972a0c56aaSWei Liu         goto out;
40982a0c56aaSWei Liu     } else if (!S_ISDIR(stat.st_mode)) {
40992a0c56aaSWei Liu         error_setg(errp, "share path %s is not a directory", fse->path);
41002a0c56aaSWei Liu         goto out;
41012a0c56aaSWei Liu     }
4102b8bbdb88SPradeep Jagadeesh 
41033b5ee9e8SAntonios Motakis     s->dev_id = stat.st_dev;
41043b5ee9e8SAntonios Motakis 
41056b6aa828SChristian Schoenebeck     /* init inode remapping : */
41066b6aa828SChristian Schoenebeck     /* hash table for variable length inode suffixes */
41076b6aa828SChristian Schoenebeck     qpd_table_init(&s->qpd_table);
41086b6aa828SChristian Schoenebeck     /* hash table for slow/full inode remapping (most users won't need it) */
41096b6aa828SChristian Schoenebeck     qpf_table_init(&s->qpf_table);
41106b6aa828SChristian Schoenebeck     /* hash table for quick inode remapping */
41111a6ed33cSAntonios Motakis     qpp_table_init(&s->qpp_table);
41126b6aa828SChristian Schoenebeck     s->qp_ndevices = 0;
41136b6aa828SChristian Schoenebeck     s->qp_affix_next = 1; /* reserve 0 to detect overflow */
4114f3fe4a2dSAntonios Motakis     s->qp_fullpath_next = 1;
41151a6ed33cSAntonios Motakis 
4116b8bbdb88SPradeep Jagadeesh     s->ctx.fst = &fse->fst;
4117b8bbdb88SPradeep Jagadeesh     fsdev_throttle_init(s->ctx.fst);
4118b8bbdb88SPradeep Jagadeesh 
41192a0c56aaSWei Liu     rc = 0;
41202a0c56aaSWei Liu out:
41212a0c56aaSWei Liu     if (rc) {
4122b69c3c21SMarkus Armbruster         v9fs_device_unrealize_common(s);
4123702dbcc2SLi Qiang     }
41242a0c56aaSWei Liu     v9fs_path_free(&path);
41252a0c56aaSWei Liu     return rc;
41262a0c56aaSWei Liu }
41272a0c56aaSWei Liu 
4128b69c3c21SMarkus Armbruster void v9fs_device_unrealize_common(V9fsState *s)
41292a0c56aaSWei Liu {
4130c0da0cb7SGreg Kurz     if (s->ops && s->ops->cleanup) {
4131702dbcc2SLi Qiang         s->ops->cleanup(&s->ctx);
4132702dbcc2SLi Qiang     }
4133c0da0cb7SGreg Kurz     if (s->ctx.fst) {
4134b8bbdb88SPradeep Jagadeesh         fsdev_throttle_cleanup(s->ctx.fst);
4135c0da0cb7SGreg Kurz     }
41362a0c56aaSWei Liu     g_free(s->tag);
41376b6aa828SChristian Schoenebeck     qp_table_destroy(&s->qpd_table);
4138f3fe4a2dSAntonios Motakis     qp_table_destroy(&s->qpp_table);
4139f3fe4a2dSAntonios Motakis     qp_table_destroy(&s->qpf_table);
41404774718eSLi Qiang     g_free(s->ctx.fs_root);
41412a0c56aaSWei Liu }
41422a0c56aaSWei Liu 
41430e44a0fdSGreg Kurz typedef struct VirtfsCoResetData {
41440e44a0fdSGreg Kurz     V9fsPDU pdu;
41450e44a0fdSGreg Kurz     bool done;
41460e44a0fdSGreg Kurz } VirtfsCoResetData;
41470e44a0fdSGreg Kurz 
41480e44a0fdSGreg Kurz static void coroutine_fn virtfs_co_reset(void *opaque)
41490e44a0fdSGreg Kurz {
41500e44a0fdSGreg Kurz     VirtfsCoResetData *data = opaque;
41510e44a0fdSGreg Kurz 
41520e44a0fdSGreg Kurz     virtfs_reset(&data->pdu);
41530e44a0fdSGreg Kurz     data->done = true;
41540e44a0fdSGreg Kurz }
41550e44a0fdSGreg Kurz 
41560e44a0fdSGreg Kurz void v9fs_reset(V9fsState *s)
41570e44a0fdSGreg Kurz {
41580e44a0fdSGreg Kurz     VirtfsCoResetData data = { .pdu = { .s = s }, .done = false };
41590e44a0fdSGreg Kurz     Coroutine *co;
41600e44a0fdSGreg Kurz 
41610e44a0fdSGreg Kurz     while (!QLIST_EMPTY(&s->active_list)) {
41620e44a0fdSGreg Kurz         aio_poll(qemu_get_aio_context(), true);
41630e44a0fdSGreg Kurz     }
41640e44a0fdSGreg Kurz 
41650e44a0fdSGreg Kurz     co = qemu_coroutine_create(virtfs_co_reset, &data);
41660e44a0fdSGreg Kurz     qemu_coroutine_enter(co);
41670e44a0fdSGreg Kurz 
41680e44a0fdSGreg Kurz     while (!data.done) {
41690e44a0fdSGreg Kurz         aio_poll(qemu_get_aio_context(), true);
41700e44a0fdSGreg Kurz     }
41710e44a0fdSGreg Kurz }
41720e44a0fdSGreg Kurz 
417360ce86c7SWei Liu static void __attribute__((__constructor__)) v9fs_set_fd_limit(void)
417460ce86c7SWei Liu {
417560ce86c7SWei Liu     struct rlimit rlim;
417660ce86c7SWei Liu     if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
417763325b18SGreg Kurz         error_report("Failed to get the resource limit");
417860ce86c7SWei Liu         exit(1);
417960ce86c7SWei Liu     }
418060ce86c7SWei Liu     open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur/3);
418160ce86c7SWei Liu     open_fd_rc = rlim.rlim_cur/2;
418260ce86c7SWei Liu }
4183