xref: /openbmc/qemu/hw/9pfs/9p.c (revision c8a7fc51)
160ce86c7SWei Liu /*
260ce86c7SWei Liu  * Virtio 9p backend
360ce86c7SWei Liu  *
460ce86c7SWei Liu  * Copyright IBM, Corp. 2010
560ce86c7SWei Liu  *
660ce86c7SWei Liu  * Authors:
760ce86c7SWei Liu  *  Anthony Liguori   <aliguori@us.ibm.com>
860ce86c7SWei Liu  *
960ce86c7SWei Liu  * This work is licensed under the terms of the GNU GPL, version 2.  See
1060ce86c7SWei Liu  * the COPYING file in the top-level directory.
1160ce86c7SWei Liu  *
1260ce86c7SWei Liu  */
1360ce86c7SWei Liu 
146f569084SChristian Schoenebeck /*
156f569084SChristian Schoenebeck  * Not so fast! You might want to read the 9p developer docs first:
166f569084SChristian Schoenebeck  * https://wiki.qemu.org/Documentation/9p
176f569084SChristian Schoenebeck  */
186f569084SChristian Schoenebeck 
19fbc04127SPeter Maydell #include "qemu/osdep.h"
20a136d175SWill Cohen #ifdef CONFIG_LINUX
21a136d175SWill Cohen #include <linux/limits.h>
22a136d175SWill Cohen #endif
23e3e83f2eSGreg Kurz #include <glib/gprintf.h>
2460ce86c7SWei Liu #include "hw/virtio/virtio.h"
25da34e65cSMarkus Armbruster #include "qapi/error.h"
2660ce86c7SWei Liu #include "qemu/error-report.h"
2760ce86c7SWei Liu #include "qemu/iov.h"
28db725815SMarkus Armbruster #include "qemu/main-loop.h"
2960ce86c7SWei Liu #include "qemu/sockets.h"
3060ce86c7SWei Liu #include "virtio-9p.h"
3160ce86c7SWei Liu #include "fsdev/qemu-fsdev.h"
3260ce86c7SWei Liu #include "9p-xattr.h"
336b3b279bSKeno Fischer #include "9p-util.h"
3460ce86c7SWei Liu #include "coth.h"
3560ce86c7SWei Liu #include "trace.h"
36795c40b8SJuan Quintela #include "migration/blocker.h"
371a6ed33cSAntonios Motakis #include "qemu/xxhash.h"
386b6aa828SChristian Schoenebeck #include <math.h>
3960ce86c7SWei Liu 
4060ce86c7SWei Liu int open_fd_hw;
4160ce86c7SWei Liu int total_open_fd;
4260ce86c7SWei Liu static int open_fd_rc;
4360ce86c7SWei Liu 
4460ce86c7SWei Liu enum {
4560ce86c7SWei Liu     Oread   = 0x00,
4660ce86c7SWei Liu     Owrite  = 0x01,
4760ce86c7SWei Liu     Ordwr   = 0x02,
4860ce86c7SWei Liu     Oexec   = 0x03,
4960ce86c7SWei Liu     Oexcl   = 0x04,
5060ce86c7SWei Liu     Otrunc  = 0x10,
5160ce86c7SWei Liu     Orexec  = 0x20,
5260ce86c7SWei Liu     Orclose = 0x40,
5360ce86c7SWei Liu     Oappend = 0x80,
5460ce86c7SWei Liu };
5560ce86c7SWei Liu 
56cc82fde9SChristian Schoenebeck P9ARRAY_DEFINE_TYPE(V9fsPath, v9fs_path_free);
57cc82fde9SChristian Schoenebeck 
pdu_marshal(V9fsPDU * pdu,size_t offset,const char * fmt,...)5875673590SGreg Kurz static ssize_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
5960ce86c7SWei Liu {
6060ce86c7SWei Liu     ssize_t ret;
6160ce86c7SWei Liu     va_list ap;
6260ce86c7SWei Liu 
6360ce86c7SWei Liu     va_start(ap, fmt);
64ea83441cSStefano Stabellini     ret = pdu->s->transport->pdu_vmarshal(pdu, offset, fmt, ap);
6560ce86c7SWei Liu     va_end(ap);
6660ce86c7SWei Liu 
6760ce86c7SWei Liu     return ret;
6860ce86c7SWei Liu }
6960ce86c7SWei Liu 
pdu_unmarshal(V9fsPDU * pdu,size_t offset,const char * fmt,...)7075673590SGreg Kurz static ssize_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
7160ce86c7SWei Liu {
7260ce86c7SWei Liu     ssize_t ret;
7360ce86c7SWei Liu     va_list ap;
7460ce86c7SWei Liu 
7560ce86c7SWei Liu     va_start(ap, fmt);
76ea83441cSStefano Stabellini     ret = pdu->s->transport->pdu_vunmarshal(pdu, offset, fmt, ap);
7760ce86c7SWei Liu     va_end(ap);
7860ce86c7SWei Liu 
7960ce86c7SWei Liu     return ret;
8060ce86c7SWei Liu }
8160ce86c7SWei Liu 
omode_to_uflags(int8_t mode)8260ce86c7SWei Liu static int omode_to_uflags(int8_t mode)
8360ce86c7SWei Liu {
8460ce86c7SWei Liu     int ret = 0;
8560ce86c7SWei Liu 
8660ce86c7SWei Liu     switch (mode & 3) {
8760ce86c7SWei Liu     case Oread:
8860ce86c7SWei Liu         ret = O_RDONLY;
8960ce86c7SWei Liu         break;
9060ce86c7SWei Liu     case Ordwr:
9160ce86c7SWei Liu         ret = O_RDWR;
9260ce86c7SWei Liu         break;
9360ce86c7SWei Liu     case Owrite:
9460ce86c7SWei Liu         ret = O_WRONLY;
9560ce86c7SWei Liu         break;
9660ce86c7SWei Liu     case Oexec:
9760ce86c7SWei Liu         ret = O_RDONLY;
9860ce86c7SWei Liu         break;
9960ce86c7SWei Liu     }
10060ce86c7SWei Liu 
10160ce86c7SWei Liu     if (mode & Otrunc) {
10260ce86c7SWei Liu         ret |= O_TRUNC;
10360ce86c7SWei Liu     }
10460ce86c7SWei Liu 
10560ce86c7SWei Liu     if (mode & Oappend) {
10660ce86c7SWei Liu         ret |= O_APPEND;
10760ce86c7SWei Liu     }
10860ce86c7SWei Liu 
10960ce86c7SWei Liu     if (mode & Oexcl) {
11060ce86c7SWei Liu         ret |= O_EXCL;
11160ce86c7SWei Liu     }
11260ce86c7SWei Liu 
11360ce86c7SWei Liu     return ret;
11460ce86c7SWei Liu }
11560ce86c7SWei Liu 
1168e71b96cSGreg Kurz typedef struct DotlOpenflagMap {
11760ce86c7SWei Liu     int dotl_flag;
11860ce86c7SWei Liu     int open_flag;
1198e71b96cSGreg Kurz } DotlOpenflagMap;
12060ce86c7SWei Liu 
dotl_to_open_flags(int flags)12160ce86c7SWei Liu static int dotl_to_open_flags(int flags)
12260ce86c7SWei Liu {
12360ce86c7SWei Liu     int i;
12460ce86c7SWei Liu     /*
12560ce86c7SWei Liu      * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
12660ce86c7SWei Liu      * and P9_DOTL_NOACCESS
12760ce86c7SWei Liu      */
12860ce86c7SWei Liu     int oflags = flags & O_ACCMODE;
12960ce86c7SWei Liu 
1308e71b96cSGreg Kurz     DotlOpenflagMap dotl_oflag_map[] = {
13160ce86c7SWei Liu         { P9_DOTL_CREATE, O_CREAT },
13260ce86c7SWei Liu         { P9_DOTL_EXCL, O_EXCL },
13360ce86c7SWei Liu         { P9_DOTL_NOCTTY , O_NOCTTY },
13460ce86c7SWei Liu         { P9_DOTL_TRUNC, O_TRUNC },
13560ce86c7SWei Liu         { P9_DOTL_APPEND, O_APPEND },
13660ce86c7SWei Liu         { P9_DOTL_NONBLOCK, O_NONBLOCK } ,
13760ce86c7SWei Liu         { P9_DOTL_DSYNC, O_DSYNC },
13860ce86c7SWei Liu         { P9_DOTL_FASYNC, FASYNC },
13967a71e3bSKeno Fischer #ifndef CONFIG_DARWIN
14067a71e3bSKeno Fischer         { P9_DOTL_NOATIME, O_NOATIME },
14167a71e3bSKeno Fischer         /*
14267a71e3bSKeno Fischer          *  On Darwin, we could map to F_NOCACHE, which is
14367a71e3bSKeno Fischer          *  similar, but doesn't quite have the same
14467a71e3bSKeno Fischer          *  semantics. However, we don't support O_DIRECT
14567a71e3bSKeno Fischer          *  even on linux at the moment, so we just ignore
14667a71e3bSKeno Fischer          *  it here.
14767a71e3bSKeno Fischer          */
14860ce86c7SWei Liu         { P9_DOTL_DIRECT, O_DIRECT },
14967a71e3bSKeno Fischer #endif
15060ce86c7SWei Liu         { P9_DOTL_LARGEFILE, O_LARGEFILE },
15160ce86c7SWei Liu         { P9_DOTL_DIRECTORY, O_DIRECTORY },
15260ce86c7SWei Liu         { P9_DOTL_NOFOLLOW, O_NOFOLLOW },
15360ce86c7SWei Liu         { P9_DOTL_SYNC, O_SYNC },
15460ce86c7SWei Liu     };
15560ce86c7SWei Liu 
15660ce86c7SWei Liu     for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) {
15760ce86c7SWei Liu         if (flags & dotl_oflag_map[i].dotl_flag) {
15860ce86c7SWei Liu             oflags |= dotl_oflag_map[i].open_flag;
15960ce86c7SWei Liu         }
16060ce86c7SWei Liu     }
16160ce86c7SWei Liu 
16260ce86c7SWei Liu     return oflags;
16360ce86c7SWei Liu }
16460ce86c7SWei Liu 
cred_init(FsCred * credp)16560ce86c7SWei Liu void cred_init(FsCred *credp)
16660ce86c7SWei Liu {
16760ce86c7SWei Liu     credp->fc_uid = -1;
16860ce86c7SWei Liu     credp->fc_gid = -1;
16960ce86c7SWei Liu     credp->fc_mode = -1;
17060ce86c7SWei Liu     credp->fc_rdev = -1;
17160ce86c7SWei Liu }
17260ce86c7SWei Liu 
get_dotl_openflags(V9fsState * s,int oflags)17360ce86c7SWei Liu static int get_dotl_openflags(V9fsState *s, int oflags)
17460ce86c7SWei Liu {
17560ce86c7SWei Liu     int flags;
17660ce86c7SWei Liu     /*
17760ce86c7SWei Liu      * Filter the client open flags
17860ce86c7SWei Liu      */
17960ce86c7SWei Liu     flags = dotl_to_open_flags(oflags);
18060ce86c7SWei Liu     flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT);
18167a71e3bSKeno Fischer #ifndef CONFIG_DARWIN
18260ce86c7SWei Liu     /*
18360ce86c7SWei Liu      * Ignore direct disk access hint until the server supports it.
18460ce86c7SWei Liu      */
18560ce86c7SWei Liu     flags &= ~O_DIRECT;
18667a71e3bSKeno Fischer #endif
18760ce86c7SWei Liu     return flags;
18860ce86c7SWei Liu }
18960ce86c7SWei Liu 
v9fs_path_init(V9fsPath * path)19060ce86c7SWei Liu void v9fs_path_init(V9fsPath *path)
19160ce86c7SWei Liu {
19260ce86c7SWei Liu     path->data = NULL;
19360ce86c7SWei Liu     path->size = 0;
19460ce86c7SWei Liu }
19560ce86c7SWei Liu 
v9fs_path_free(V9fsPath * path)19660ce86c7SWei Liu void v9fs_path_free(V9fsPath *path)
19760ce86c7SWei Liu {
19860ce86c7SWei Liu     g_free(path->data);
19960ce86c7SWei Liu     path->data = NULL;
20060ce86c7SWei Liu     path->size = 0;
20160ce86c7SWei Liu }
20260ce86c7SWei Liu 
203e3e83f2eSGreg Kurz 
2049edc6313SMarc-André Lureau void G_GNUC_PRINTF(2, 3)
v9fs_path_sprintf(V9fsPath * path,const char * fmt,...)205e3e83f2eSGreg Kurz v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...)
206e3e83f2eSGreg Kurz {
207e3e83f2eSGreg Kurz     va_list ap;
208e3e83f2eSGreg Kurz 
209e3e83f2eSGreg Kurz     v9fs_path_free(path);
210e3e83f2eSGreg Kurz 
211e3e83f2eSGreg Kurz     va_start(ap, fmt);
212e3e83f2eSGreg Kurz     /* Bump the size for including terminating NULL */
213e3e83f2eSGreg Kurz     path->size = g_vasprintf(&path->data, fmt, ap) + 1;
214e3e83f2eSGreg Kurz     va_end(ap);
215e3e83f2eSGreg Kurz }
216e3e83f2eSGreg Kurz 
v9fs_path_copy(V9fsPath * dst,const V9fsPath * src)217e446a1ebSMarc-André Lureau void v9fs_path_copy(V9fsPath *dst, const V9fsPath *src)
21860ce86c7SWei Liu {
219e446a1ebSMarc-André Lureau     v9fs_path_free(dst);
220e446a1ebSMarc-André Lureau     dst->size = src->size;
221e446a1ebSMarc-André Lureau     dst->data = g_memdup(src->data, src->size);
22260ce86c7SWei Liu }
22360ce86c7SWei Liu 
v9fs_name_to_path(V9fsState * s,V9fsPath * dirpath,const char * name,V9fsPath * path)22460ce86c7SWei Liu int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
22560ce86c7SWei Liu                       const char *name, V9fsPath *path)
22660ce86c7SWei Liu {
22760ce86c7SWei Liu     int err;
22860ce86c7SWei Liu     err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
22960ce86c7SWei Liu     if (err < 0) {
23060ce86c7SWei Liu         err = -errno;
23160ce86c7SWei Liu     }
23260ce86c7SWei Liu     return err;
23360ce86c7SWei Liu }
23460ce86c7SWei Liu 
23560ce86c7SWei Liu /*
23660ce86c7SWei Liu  * Return TRUE if s1 is an ancestor of s2.
23760ce86c7SWei Liu  *
23860ce86c7SWei Liu  * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
23960ce86c7SWei Liu  * As a special case, We treat s1 as ancestor of s2 if they are same!
24060ce86c7SWei Liu  */
v9fs_path_is_ancestor(V9fsPath * s1,V9fsPath * s2)24160ce86c7SWei Liu static int v9fs_path_is_ancestor(V9fsPath *s1, V9fsPath *s2)
24260ce86c7SWei Liu {
24360ce86c7SWei Liu     if (!strncmp(s1->data, s2->data, s1->size - 1)) {
24460ce86c7SWei Liu         if (s2->data[s1->size - 1] == '\0' || s2->data[s1->size - 1] == '/') {
24560ce86c7SWei Liu             return 1;
24660ce86c7SWei Liu         }
24760ce86c7SWei Liu     }
24860ce86c7SWei Liu     return 0;
24960ce86c7SWei Liu }
25060ce86c7SWei Liu 
v9fs_string_size(V9fsString * str)25160ce86c7SWei Liu static size_t v9fs_string_size(V9fsString *str)
25260ce86c7SWei Liu {
25360ce86c7SWei Liu     return str->size;
25460ce86c7SWei Liu }
25560ce86c7SWei Liu 
25660ce86c7SWei Liu /*
257f5265c8fSLinus Heckemann  * returns 0 if fid got re-opened, 1 if not, < 0 on error
258f5265c8fSLinus Heckemann  */
v9fs_reopen_fid(V9fsPDU * pdu,V9fsFidState * f)2598440e22eSGreg Kurz static int coroutine_fn v9fs_reopen_fid(V9fsPDU *pdu, V9fsFidState *f)
26060ce86c7SWei Liu {
26160ce86c7SWei Liu     int err = 1;
26260ce86c7SWei Liu     if (f->fid_type == P9_FID_FILE) {
26360ce86c7SWei Liu         if (f->fs.fd == -1) {
26460ce86c7SWei Liu             do {
26560ce86c7SWei Liu                 err = v9fs_co_open(pdu, f, f->open_flags);
26660ce86c7SWei Liu             } while (err == -EINTR && !pdu->cancelled);
26760ce86c7SWei Liu         }
26860ce86c7SWei Liu     } else if (f->fid_type == P9_FID_DIR) {
269f314ea4eSGreg Kurz         if (f->fs.dir.stream == NULL) {
27060ce86c7SWei Liu             do {
27160ce86c7SWei Liu                 err = v9fs_co_opendir(pdu, f);
27260ce86c7SWei Liu             } while (err == -EINTR && !pdu->cancelled);
27360ce86c7SWei Liu         }
27460ce86c7SWei Liu     }
27560ce86c7SWei Liu     return err;
27660ce86c7SWei Liu }
27760ce86c7SWei Liu 
get_fid(V9fsPDU * pdu,int32_t fid)2788440e22eSGreg Kurz static V9fsFidState *coroutine_fn get_fid(V9fsPDU *pdu, int32_t fid)
27960ce86c7SWei Liu {
28060ce86c7SWei Liu     int err;
28160ce86c7SWei Liu     V9fsFidState *f;
28260ce86c7SWei Liu     V9fsState *s = pdu->s;
28360ce86c7SWei Liu 
284f5265c8fSLinus Heckemann     f = g_hash_table_lookup(s->fids, GINT_TO_POINTER(fid));
285f5265c8fSLinus Heckemann     if (f) {
28660ce86c7SWei Liu         BUG_ON(f->clunked);
28760ce86c7SWei Liu         /*
28860ce86c7SWei Liu          * Update the fid ref upfront so that
28960ce86c7SWei Liu          * we don't get reclaimed when we yield
29060ce86c7SWei Liu          * in open later.
29160ce86c7SWei Liu          */
29260ce86c7SWei Liu         f->ref++;
29360ce86c7SWei Liu         /*
29460ce86c7SWei Liu          * check whether we need to reopen the
29560ce86c7SWei Liu          * file. We might have closed the fd
29660ce86c7SWei Liu          * while trying to free up some file
29760ce86c7SWei Liu          * descriptors.
29860ce86c7SWei Liu          */
29960ce86c7SWei Liu         err = v9fs_reopen_fid(pdu, f);
30060ce86c7SWei Liu         if (err < 0) {
30160ce86c7SWei Liu             f->ref--;
30260ce86c7SWei Liu             return NULL;
30360ce86c7SWei Liu         }
30460ce86c7SWei Liu         /*
30560ce86c7SWei Liu          * Mark the fid as referenced so that the LRU
30660ce86c7SWei Liu          * reclaim won't close the file descriptor
30760ce86c7SWei Liu          */
30860ce86c7SWei Liu         f->flags |= FID_REFERENCED;
30960ce86c7SWei Liu         return f;
31060ce86c7SWei Liu     }
31160ce86c7SWei Liu     return NULL;
31260ce86c7SWei Liu }
31360ce86c7SWei Liu 
alloc_fid(V9fsState * s,int32_t fid)31460ce86c7SWei Liu static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
31560ce86c7SWei Liu {
31660ce86c7SWei Liu     V9fsFidState *f;
31760ce86c7SWei Liu 
318f5265c8fSLinus Heckemann     f = g_hash_table_lookup(s->fids, GINT_TO_POINTER(fid));
319f5265c8fSLinus Heckemann     if (f) {
32060ce86c7SWei Liu         /* If fid is already there return NULL */
32160ce86c7SWei Liu         BUG_ON(f->clunked);
32260ce86c7SWei Liu         return NULL;
32360ce86c7SWei Liu     }
3241366244aSMarkus Armbruster     f = g_new0(V9fsFidState, 1);
32560ce86c7SWei Liu     f->fid = fid;
32660ce86c7SWei Liu     f->fid_type = P9_FID_NONE;
32760ce86c7SWei Liu     f->ref = 1;
32860ce86c7SWei Liu     /*
32960ce86c7SWei Liu      * Mark the fid as referenced so that the LRU
33060ce86c7SWei Liu      * reclaim won't close the file descriptor
33160ce86c7SWei Liu      */
33260ce86c7SWei Liu     f->flags |= FID_REFERENCED;
333f5265c8fSLinus Heckemann     g_hash_table_insert(s->fids, GINT_TO_POINTER(fid), f);
33460ce86c7SWei Liu 
335d2c5cf7cSChristian Schoenebeck     v9fs_readdir_init(s->proto_version, &f->fs.dir);
336d2c5cf7cSChristian Schoenebeck     v9fs_readdir_init(s->proto_version, &f->fs_reclaim.dir);
3377cde47d4SGreg Kurz 
33860ce86c7SWei Liu     return f;
33960ce86c7SWei Liu }
34060ce86c7SWei Liu 
v9fs_xattr_fid_clunk(V9fsPDU * pdu,V9fsFidState * fidp)3418440e22eSGreg Kurz static int coroutine_fn v9fs_xattr_fid_clunk(V9fsPDU *pdu, V9fsFidState *fidp)
34260ce86c7SWei Liu {
34360ce86c7SWei Liu     int retval = 0;
34460ce86c7SWei Liu 
345dd28fbbcSLi Qiang     if (fidp->fs.xattr.xattrwalk_fid) {
34660ce86c7SWei Liu         /* getxattr/listxattr fid */
34760ce86c7SWei Liu         goto free_value;
34860ce86c7SWei Liu     }
34960ce86c7SWei Liu     /*
35060ce86c7SWei Liu      * if this is fid for setxattr. clunk should
35160ce86c7SWei Liu      * result in setxattr localcall
35260ce86c7SWei Liu      */
35360ce86c7SWei Liu     if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) {
35460ce86c7SWei Liu         /* clunk after partial write */
35560ce86c7SWei Liu         retval = -EINVAL;
35660ce86c7SWei Liu         goto free_out;
35760ce86c7SWei Liu     }
35860ce86c7SWei Liu     if (fidp->fs.xattr.len) {
35960ce86c7SWei Liu         retval = v9fs_co_lsetxattr(pdu, &fidp->path, &fidp->fs.xattr.name,
36060ce86c7SWei Liu                                    fidp->fs.xattr.value,
36160ce86c7SWei Liu                                    fidp->fs.xattr.len,
36260ce86c7SWei Liu                                    fidp->fs.xattr.flags);
36360ce86c7SWei Liu     } else {
36460ce86c7SWei Liu         retval = v9fs_co_lremovexattr(pdu, &fidp->path, &fidp->fs.xattr.name);
36560ce86c7SWei Liu     }
36660ce86c7SWei Liu free_out:
36760ce86c7SWei Liu     v9fs_string_free(&fidp->fs.xattr.name);
36860ce86c7SWei Liu free_value:
36960ce86c7SWei Liu     g_free(fidp->fs.xattr.value);
37060ce86c7SWei Liu     return retval;
37160ce86c7SWei Liu }
37260ce86c7SWei Liu 
free_fid(V9fsPDU * pdu,V9fsFidState * fidp)3738440e22eSGreg Kurz static int coroutine_fn free_fid(V9fsPDU *pdu, V9fsFidState *fidp)
37460ce86c7SWei Liu {
37560ce86c7SWei Liu     int retval = 0;
37660ce86c7SWei Liu 
37760ce86c7SWei Liu     if (fidp->fid_type == P9_FID_FILE) {
37860ce86c7SWei Liu         /* If we reclaimed the fd no need to close */
37960ce86c7SWei Liu         if (fidp->fs.fd != -1) {
38060ce86c7SWei Liu             retval = v9fs_co_close(pdu, &fidp->fs);
38160ce86c7SWei Liu         }
38260ce86c7SWei Liu     } else if (fidp->fid_type == P9_FID_DIR) {
383f314ea4eSGreg Kurz         if (fidp->fs.dir.stream != NULL) {
38460ce86c7SWei Liu             retval = v9fs_co_closedir(pdu, &fidp->fs);
38560ce86c7SWei Liu         }
38660ce86c7SWei Liu     } else if (fidp->fid_type == P9_FID_XATTR) {
38760ce86c7SWei Liu         retval = v9fs_xattr_fid_clunk(pdu, fidp);
38860ce86c7SWei Liu     }
38960ce86c7SWei Liu     v9fs_path_free(&fidp->path);
39060ce86c7SWei Liu     g_free(fidp);
39160ce86c7SWei Liu     return retval;
39260ce86c7SWei Liu }
39360ce86c7SWei Liu 
put_fid(V9fsPDU * pdu,V9fsFidState * fidp)3948440e22eSGreg Kurz static int coroutine_fn put_fid(V9fsPDU *pdu, V9fsFidState *fidp)
39560ce86c7SWei Liu {
39660ce86c7SWei Liu     BUG_ON(!fidp->ref);
39760ce86c7SWei Liu     fidp->ref--;
39860ce86c7SWei Liu     /*
39960ce86c7SWei Liu      * Don't free the fid if it is in reclaim list
40060ce86c7SWei Liu      */
40160ce86c7SWei Liu     if (!fidp->ref && fidp->clunked) {
40260ce86c7SWei Liu         if (fidp->fid == pdu->s->root_fid) {
40360ce86c7SWei Liu             /*
40460ce86c7SWei Liu              * if the clunked fid is root fid then we
40560ce86c7SWei Liu              * have unmounted the fs on the client side.
40660ce86c7SWei Liu              * delete the migration blocker. Ideally, this
40760ce86c7SWei Liu              * should be hooked to transport close notification
40860ce86c7SWei Liu              */
409*c8a7fc51SSteve Sistare             migrate_del_blocker(&pdu->s->migration_blocker);
41060ce86c7SWei Liu         }
41160ce86c7SWei Liu         return free_fid(pdu, fidp);
41260ce86c7SWei Liu     }
41360ce86c7SWei Liu     return 0;
41460ce86c7SWei Liu }
41560ce86c7SWei Liu 
clunk_fid(V9fsState * s,int32_t fid)41660ce86c7SWei Liu static V9fsFidState *clunk_fid(V9fsState *s, int32_t fid)
41760ce86c7SWei Liu {
418feabd6cfSGreg Kurz     V9fsFidState *fidp;
41960ce86c7SWei Liu 
420f5265c8fSLinus Heckemann     /* TODO: Use g_hash_table_steal_extended() instead? */
421f5265c8fSLinus Heckemann     fidp = g_hash_table_lookup(s->fids, GINT_TO_POINTER(fid));
422f5265c8fSLinus Heckemann     if (fidp) {
423f5265c8fSLinus Heckemann         g_hash_table_remove(s->fids, GINT_TO_POINTER(fid));
4242e53160fSGreg Kurz         fidp->clunked = true;
42560ce86c7SWei Liu         return fidp;
42660ce86c7SWei Liu     }
427feabd6cfSGreg Kurz     return NULL;
428feabd6cfSGreg Kurz }
42960ce86c7SWei Liu 
v9fs_reclaim_fd(V9fsPDU * pdu)4308440e22eSGreg Kurz void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu)
43160ce86c7SWei Liu {
43260ce86c7SWei Liu     int reclaim_count = 0;
43360ce86c7SWei Liu     V9fsState *s = pdu->s;
43481f9766bSGreg Kurz     V9fsFidState *f;
435f5265c8fSLinus Heckemann     GHashTableIter iter;
436f5265c8fSLinus Heckemann     gpointer fid;
437f5265c8fSLinus Heckemann 
438f5265c8fSLinus Heckemann     g_hash_table_iter_init(&iter, s->fids);
439f5265c8fSLinus Heckemann 
44081f9766bSGreg Kurz     QSLIST_HEAD(, V9fsFidState) reclaim_list =
44181f9766bSGreg Kurz         QSLIST_HEAD_INITIALIZER(reclaim_list);
44260ce86c7SWei Liu 
443f5265c8fSLinus Heckemann     while (g_hash_table_iter_next(&iter, &fid, (gpointer *) &f)) {
44460ce86c7SWei Liu         /*
44560ce86c7SWei Liu          * Unlink fids cannot be reclaimed. Check
44660ce86c7SWei Liu          * for them and skip them. Also skip fids
44760ce86c7SWei Liu          * currently being operated on.
44860ce86c7SWei Liu          */
44960ce86c7SWei Liu         if (f->ref || f->flags & FID_NON_RECLAIMABLE) {
45060ce86c7SWei Liu             continue;
45160ce86c7SWei Liu         }
45260ce86c7SWei Liu         /*
45360ce86c7SWei Liu          * if it is a recently referenced fid
45460ce86c7SWei Liu          * we leave the fid untouched and clear the
45560ce86c7SWei Liu          * reference bit. We come back to it later
45660ce86c7SWei Liu          * in the next iteration. (a simple LRU without
45760ce86c7SWei Liu          * moving list elements around)
45860ce86c7SWei Liu          */
45960ce86c7SWei Liu         if (f->flags & FID_REFERENCED) {
46060ce86c7SWei Liu             f->flags &= ~FID_REFERENCED;
46160ce86c7SWei Liu             continue;
46260ce86c7SWei Liu         }
46360ce86c7SWei Liu         /*
46460ce86c7SWei Liu          * Add fids to reclaim list.
46560ce86c7SWei Liu          */
46660ce86c7SWei Liu         if (f->fid_type == P9_FID_FILE) {
46760ce86c7SWei Liu             if (f->fs.fd != -1) {
46860ce86c7SWei Liu                 /*
46960ce86c7SWei Liu                  * Up the reference count so that
47060ce86c7SWei Liu                  * a clunk request won't free this fid
47160ce86c7SWei Liu                  */
47260ce86c7SWei Liu                 f->ref++;
47381f9766bSGreg Kurz                 QSLIST_INSERT_HEAD(&reclaim_list, f, reclaim_next);
47460ce86c7SWei Liu                 f->fs_reclaim.fd = f->fs.fd;
47560ce86c7SWei Liu                 f->fs.fd = -1;
47660ce86c7SWei Liu                 reclaim_count++;
47760ce86c7SWei Liu             }
47860ce86c7SWei Liu         } else if (f->fid_type == P9_FID_DIR) {
479f314ea4eSGreg Kurz             if (f->fs.dir.stream != NULL) {
48060ce86c7SWei Liu                 /*
48160ce86c7SWei Liu                  * Up the reference count so that
48260ce86c7SWei Liu                  * a clunk request won't free this fid
48360ce86c7SWei Liu                  */
48460ce86c7SWei Liu                 f->ref++;
48581f9766bSGreg Kurz                 QSLIST_INSERT_HEAD(&reclaim_list, f, reclaim_next);
486f314ea4eSGreg Kurz                 f->fs_reclaim.dir.stream = f->fs.dir.stream;
487f314ea4eSGreg Kurz                 f->fs.dir.stream = NULL;
48860ce86c7SWei Liu                 reclaim_count++;
48960ce86c7SWei Liu             }
49060ce86c7SWei Liu         }
49160ce86c7SWei Liu         if (reclaim_count >= open_fd_rc) {
49260ce86c7SWei Liu             break;
49360ce86c7SWei Liu         }
49460ce86c7SWei Liu     }
49560ce86c7SWei Liu     /*
49660ce86c7SWei Liu      * Now close the fid in reclaim list. Free them if they
49760ce86c7SWei Liu      * are already clunked.
49860ce86c7SWei Liu      */
49981f9766bSGreg Kurz     while (!QSLIST_EMPTY(&reclaim_list)) {
50081f9766bSGreg Kurz         f = QSLIST_FIRST(&reclaim_list);
50181f9766bSGreg Kurz         QSLIST_REMOVE(&reclaim_list, f, V9fsFidState, reclaim_next);
50260ce86c7SWei Liu         if (f->fid_type == P9_FID_FILE) {
50360ce86c7SWei Liu             v9fs_co_close(pdu, &f->fs_reclaim);
50460ce86c7SWei Liu         } else if (f->fid_type == P9_FID_DIR) {
50560ce86c7SWei Liu             v9fs_co_closedir(pdu, &f->fs_reclaim);
50660ce86c7SWei Liu         }
50760ce86c7SWei Liu         /*
50860ce86c7SWei Liu          * Now drop the fid reference, free it
50960ce86c7SWei Liu          * if clunked.
51060ce86c7SWei Liu          */
51160ce86c7SWei Liu         put_fid(pdu, f);
51260ce86c7SWei Liu     }
51360ce86c7SWei Liu }
51460ce86c7SWei Liu 
515f5265c8fSLinus Heckemann /*
516f5265c8fSLinus Heckemann  * This is used when a path is removed from the directory tree. Any
517f5265c8fSLinus Heckemann  * fids that still reference it must not be closed from then on, since
518f5265c8fSLinus Heckemann  * they cannot be reopened.
519f5265c8fSLinus Heckemann  */
v9fs_mark_fids_unreclaim(V9fsPDU * pdu,V9fsPath * path)5208440e22eSGreg Kurz static int coroutine_fn v9fs_mark_fids_unreclaim(V9fsPDU *pdu, V9fsPath *path)
52160ce86c7SWei Liu {
522f5265c8fSLinus Heckemann     int err = 0;
52360ce86c7SWei Liu     V9fsState *s = pdu->s;
524f5265c8fSLinus Heckemann     V9fsFidState *fidp;
525f5265c8fSLinus Heckemann     gpointer fid;
526f5265c8fSLinus Heckemann     GHashTableIter iter;
527f5265c8fSLinus Heckemann     /*
528f5265c8fSLinus Heckemann      * The most common case is probably that we have exactly one
529f5265c8fSLinus Heckemann      * fid for the given path, so preallocate exactly one.
530f5265c8fSLinus Heckemann      */
531f5265c8fSLinus Heckemann     g_autoptr(GArray) to_reopen = g_array_sized_new(FALSE, FALSE,
532f5265c8fSLinus Heckemann             sizeof(V9fsFidState *), 1);
533f5265c8fSLinus Heckemann     gint i;
53460ce86c7SWei Liu 
535f5265c8fSLinus Heckemann     g_hash_table_iter_init(&iter, s->fids);
53620b7f45bSGreg Kurz 
53720b7f45bSGreg Kurz     /*
538f5265c8fSLinus Heckemann      * We iterate over the fid table looking for the entries we need
539f5265c8fSLinus Heckemann      * to reopen, and store them in to_reopen. This is because
540f5265c8fSLinus Heckemann      * v9fs_reopen_fid() and put_fid() yield. This allows the fid table
541f5265c8fSLinus Heckemann      * to be modified in the meantime, invalidating our iterator.
54220b7f45bSGreg Kurz      */
543f5265c8fSLinus Heckemann     while (g_hash_table_iter_next(&iter, &fid, (gpointer *) &fidp)) {
54420b7f45bSGreg Kurz         if (fidp->path.size == path->size &&
54520b7f45bSGreg Kurz             !memcmp(fidp->path.data, path->data, path->size)) {
546f5265c8fSLinus Heckemann             /*
547f5265c8fSLinus Heckemann              * Ensure the fid survives a potential clunk request during
548f5265c8fSLinus Heckemann              * v9fs_reopen_fid or put_fid.
549f5265c8fSLinus Heckemann              */
550f5265c8fSLinus Heckemann             fidp->ref++;
55160ce86c7SWei Liu             fidp->flags |= FID_NON_RECLAIMABLE;
552f5265c8fSLinus Heckemann             g_array_append_val(to_reopen, fidp);
553f5265c8fSLinus Heckemann         }
554f5265c8fSLinus Heckemann     }
55560ce86c7SWei Liu 
556f5265c8fSLinus Heckemann     for (i = 0; i < to_reopen->len; i++) {
557f5265c8fSLinus Heckemann         fidp = g_array_index(to_reopen, V9fsFidState*, i);
55860ce86c7SWei Liu         /* reopen the file/dir if already closed */
55960ce86c7SWei Liu         err = v9fs_reopen_fid(pdu, fidp);
56060ce86c7SWei Liu         if (err < 0) {
561f5265c8fSLinus Heckemann             break;
562f5265c8fSLinus Heckemann         }
563f5265c8fSLinus Heckemann     }
564f5265c8fSLinus Heckemann 
565f5265c8fSLinus Heckemann     for (i = 0; i < to_reopen->len; i++) {
566f5265c8fSLinus Heckemann         put_fid(pdu, g_array_index(to_reopen, V9fsFidState*, i));
567f5265c8fSLinus Heckemann     }
568267fcadfSGreg Kurz     return err;
56960ce86c7SWei Liu }
57060ce86c7SWei Liu 
virtfs_reset(V9fsPDU * pdu)5718440e22eSGreg Kurz static void coroutine_fn virtfs_reset(V9fsPDU *pdu)
57260ce86c7SWei Liu {
57360ce86c7SWei Liu     V9fsState *s = pdu->s;
57479decce3SGreg Kurz     V9fsFidState *fidp;
575f5265c8fSLinus Heckemann     GList *freeing;
576f5265c8fSLinus Heckemann     /*
577f5265c8fSLinus Heckemann      * Get a list of all the values (fid states) in the table, which
578f5265c8fSLinus Heckemann      * we then...
579f5265c8fSLinus Heckemann      */
580f5265c8fSLinus Heckemann     g_autoptr(GList) fids = g_hash_table_get_values(s->fids);
58160ce86c7SWei Liu 
582f5265c8fSLinus Heckemann     /* ... remove from the table, taking over ownership. */
583f5265c8fSLinus Heckemann     g_hash_table_steal_all(s->fids);
584f5265c8fSLinus Heckemann 
585f5265c8fSLinus Heckemann     /*
586f5265c8fSLinus Heckemann      * This allows us to release our references to them asynchronously without
587f5265c8fSLinus Heckemann      * iterating over the hash table and risking iterator invalidation
588f5265c8fSLinus Heckemann      * through concurrent modifications.
589f5265c8fSLinus Heckemann      */
590f5265c8fSLinus Heckemann     for (freeing = fids; freeing; freeing = freeing->next) {
591f5265c8fSLinus Heckemann         fidp = freeing->data;
5926d54af0eSGreg Kurz         fidp->ref++;
5932e53160fSGreg Kurz         fidp->clunked = true;
5946d54af0eSGreg Kurz         put_fid(pdu, fidp);
59560ce86c7SWei Liu     }
59660ce86c7SWei Liu }
59760ce86c7SWei Liu 
59860ce86c7SWei Liu #define P9_QID_TYPE_DIR         0x80
59960ce86c7SWei Liu #define P9_QID_TYPE_SYMLINK     0x02
60060ce86c7SWei Liu 
60160ce86c7SWei Liu #define P9_STAT_MODE_DIR        0x80000000
60260ce86c7SWei Liu #define P9_STAT_MODE_APPEND     0x40000000
60360ce86c7SWei Liu #define P9_STAT_MODE_EXCL       0x20000000
60460ce86c7SWei Liu #define P9_STAT_MODE_MOUNT      0x10000000
60560ce86c7SWei Liu #define P9_STAT_MODE_AUTH       0x08000000
60660ce86c7SWei Liu #define P9_STAT_MODE_TMP        0x04000000
60760ce86c7SWei Liu #define P9_STAT_MODE_SYMLINK    0x02000000
60860ce86c7SWei Liu #define P9_STAT_MODE_LINK       0x01000000
60960ce86c7SWei Liu #define P9_STAT_MODE_DEVICE     0x00800000
61060ce86c7SWei Liu #define P9_STAT_MODE_NAMED_PIPE 0x00200000
61160ce86c7SWei Liu #define P9_STAT_MODE_SOCKET     0x00100000
61260ce86c7SWei Liu #define P9_STAT_MODE_SETUID     0x00080000
61360ce86c7SWei Liu #define P9_STAT_MODE_SETGID     0x00040000
61460ce86c7SWei Liu #define P9_STAT_MODE_SETVTX     0x00010000
61560ce86c7SWei Liu 
61660ce86c7SWei Liu #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR |          \
61760ce86c7SWei Liu                                 P9_STAT_MODE_SYMLINK |      \
61860ce86c7SWei Liu                                 P9_STAT_MODE_LINK |         \
61960ce86c7SWei Liu                                 P9_STAT_MODE_DEVICE |       \
62060ce86c7SWei Liu                                 P9_STAT_MODE_NAMED_PIPE |   \
62160ce86c7SWei Liu                                 P9_STAT_MODE_SOCKET)
62260ce86c7SWei Liu 
6236b6aa828SChristian Schoenebeck /* Mirrors all bits of a byte. So e.g. binary 10100000 would become 00000101. */
mirror8bit(uint8_t byte)6246b6aa828SChristian Schoenebeck static inline uint8_t mirror8bit(uint8_t byte)
6256b6aa828SChristian Schoenebeck {
6266b6aa828SChristian Schoenebeck     return (byte * 0x0202020202ULL & 0x010884422010ULL) % 1023;
6276b6aa828SChristian Schoenebeck }
6286b6aa828SChristian Schoenebeck 
6296b6aa828SChristian Schoenebeck /* Same as mirror8bit() just for a 64 bit data type instead for a byte. */
mirror64bit(uint64_t value)6306b6aa828SChristian Schoenebeck static inline uint64_t mirror64bit(uint64_t value)
6316b6aa828SChristian Schoenebeck {
6326b6aa828SChristian Schoenebeck     return ((uint64_t)mirror8bit(value         & 0xff) << 56) |
6336b6aa828SChristian Schoenebeck            ((uint64_t)mirror8bit((value >> 8)  & 0xff) << 48) |
6346b6aa828SChristian Schoenebeck            ((uint64_t)mirror8bit((value >> 16) & 0xff) << 40) |
6356b6aa828SChristian Schoenebeck            ((uint64_t)mirror8bit((value >> 24) & 0xff) << 32) |
6366b6aa828SChristian Schoenebeck            ((uint64_t)mirror8bit((value >> 32) & 0xff) << 24) |
6376b6aa828SChristian Schoenebeck            ((uint64_t)mirror8bit((value >> 40) & 0xff) << 16) |
6386b6aa828SChristian Schoenebeck            ((uint64_t)mirror8bit((value >> 48) & 0xff) << 8)  |
6396b6aa828SChristian Schoenebeck            ((uint64_t)mirror8bit((value >> 56) & 0xff));
6406b6aa828SChristian Schoenebeck }
6416b6aa828SChristian Schoenebeck 
642e16fea41SChristian Schoenebeck /*
64328cbbdd2SMichael Tokarev  * Parameter k for the Exponential Golomb algorithm to be used.
6446b6aa828SChristian Schoenebeck  *
6456b6aa828SChristian Schoenebeck  * The smaller this value, the smaller the minimum bit count for the Exp.
6466b6aa828SChristian Schoenebeck  * Golomb generated affixes will be (at lowest index) however for the
6476b6aa828SChristian Schoenebeck  * price of having higher maximum bit count of generated affixes (at highest
6486b6aa828SChristian Schoenebeck  * index). Likewise increasing this parameter yields in smaller maximum bit
6496b6aa828SChristian Schoenebeck  * count for the price of having higher minimum bit count.
6506b6aa828SChristian Schoenebeck  *
6516b6aa828SChristian Schoenebeck  * In practice that means: a good value for k depends on the expected amount
6526b6aa828SChristian Schoenebeck  * of devices to be exposed by one export. For a small amount of devices k
6536b6aa828SChristian Schoenebeck  * should be small, for a large amount of devices k might be increased
6546b6aa828SChristian Schoenebeck  * instead. The default of k=0 should be fine for most users though.
6556b6aa828SChristian Schoenebeck  *
656e16fea41SChristian Schoenebeck  * IMPORTANT: In case this ever becomes a runtime parameter; the value of
6576b6aa828SChristian Schoenebeck  * k should not change as long as guest is still running! Because that would
6586b6aa828SChristian Schoenebeck  * cause completely different inode numbers to be generated on guest.
6596b6aa828SChristian Schoenebeck  */
6606b6aa828SChristian Schoenebeck #define EXP_GOLOMB_K    0
6616b6aa828SChristian Schoenebeck 
6626b6aa828SChristian Schoenebeck /**
663e16fea41SChristian Schoenebeck  * expGolombEncode() - Exponential Golomb algorithm for arbitrary k
664e16fea41SChristian Schoenebeck  *                     (including k=0).
6656b6aa828SChristian Schoenebeck  *
666e16fea41SChristian Schoenebeck  * @n: natural number (or index) of the prefix to be generated
667e16fea41SChristian Schoenebeck  *     (1, 2, 3, ...)
668e16fea41SChristian Schoenebeck  * @k: parameter k of Exp. Golomb algorithm to be used
669e16fea41SChristian Schoenebeck  *     (see comment on EXP_GOLOMB_K macro for details about k)
670e16fea41SChristian Schoenebeck  * Return: prefix for given @n and @k
671e16fea41SChristian Schoenebeck  *
672e16fea41SChristian Schoenebeck  * The Exponential Golomb algorithm generates prefixes (NOT suffixes!)
6736b6aa828SChristian Schoenebeck  * with growing length and with the mathematical property of being
6746b6aa828SChristian Schoenebeck  * "prefix-free". The latter means the generated prefixes can be prepended
6756b6aa828SChristian Schoenebeck  * in front of arbitrary numbers and the resulting concatenated numbers are
6766b6aa828SChristian Schoenebeck  * guaranteed to be always unique.
6776b6aa828SChristian Schoenebeck  *
6786b6aa828SChristian Schoenebeck  * This is a minor adjustment to the original Exp. Golomb algorithm in the
679e16fea41SChristian Schoenebeck  * sense that lowest allowed index (@n) starts with 1, not with zero.
6806b6aa828SChristian Schoenebeck  */
expGolombEncode(uint64_t n,int k)6816b6aa828SChristian Schoenebeck static VariLenAffix expGolombEncode(uint64_t n, int k)
6826b6aa828SChristian Schoenebeck {
6836b6aa828SChristian Schoenebeck     const uint64_t value = n + (1 << k) - 1;
6846b6aa828SChristian Schoenebeck     const int bits = (int) log2(value) + 1;
6856b6aa828SChristian Schoenebeck     return (VariLenAffix) {
6866b6aa828SChristian Schoenebeck         .type = AffixType_Prefix,
6876b6aa828SChristian Schoenebeck         .value = value,
6886b6aa828SChristian Schoenebeck         .bits = bits + MAX((bits - 1 - k), 0)
6896b6aa828SChristian Schoenebeck     };
6906b6aa828SChristian Schoenebeck }
6916b6aa828SChristian Schoenebeck 
6926b6aa828SChristian Schoenebeck /**
693e16fea41SChristian Schoenebeck  * invertAffix() - Converts a suffix into a prefix, or a prefix into a suffix.
694e16fea41SChristian Schoenebeck  * @affix: either suffix or prefix to be inverted
695e16fea41SChristian Schoenebeck  * Return: inversion of passed @affix
6966b6aa828SChristian Schoenebeck  *
6976b6aa828SChristian Schoenebeck  * Simply mirror all bits of the affix value, for the purpose to preserve
6986b6aa828SChristian Schoenebeck  * respectively the mathematical "prefix-free" or "suffix-free" property
6996b6aa828SChristian Schoenebeck  * after the conversion.
7006b6aa828SChristian Schoenebeck  *
7016b6aa828SChristian Schoenebeck  * If a passed prefix is suitable to create unique numbers, then the
7026b6aa828SChristian Schoenebeck  * returned suffix is suitable to create unique numbers as well (and vice
7036b6aa828SChristian Schoenebeck  * versa).
7046b6aa828SChristian Schoenebeck  */
invertAffix(const VariLenAffix * affix)7056b6aa828SChristian Schoenebeck static VariLenAffix invertAffix(const VariLenAffix *affix)
7066b6aa828SChristian Schoenebeck {
7076b6aa828SChristian Schoenebeck     return (VariLenAffix) {
7086b6aa828SChristian Schoenebeck         .type =
7096b6aa828SChristian Schoenebeck             (affix->type == AffixType_Suffix) ?
7106b6aa828SChristian Schoenebeck                 AffixType_Prefix : AffixType_Suffix,
7116b6aa828SChristian Schoenebeck         .value =
7126b6aa828SChristian Schoenebeck             mirror64bit(affix->value) >>
7136b6aa828SChristian Schoenebeck             ((sizeof(affix->value) * 8) - affix->bits),
7146b6aa828SChristian Schoenebeck         .bits = affix->bits
7156b6aa828SChristian Schoenebeck     };
7166b6aa828SChristian Schoenebeck }
7176b6aa828SChristian Schoenebeck 
7186b6aa828SChristian Schoenebeck /**
719e16fea41SChristian Schoenebeck  * affixForIndex() - Generates suffix numbers with "suffix-free" property.
720e16fea41SChristian Schoenebeck  * @index: natural number (or index) of the suffix to be generated
721e16fea41SChristian Schoenebeck  *         (1, 2, 3, ...)
722e16fea41SChristian Schoenebeck  * Return: Suffix suitable to assemble unique number.
7236b6aa828SChristian Schoenebeck  *
7246b6aa828SChristian Schoenebeck  * This is just a wrapper function on top of the Exp. Golomb algorithm.
7256b6aa828SChristian Schoenebeck  *
7266b6aa828SChristian Schoenebeck  * Since the Exp. Golomb algorithm generates prefixes, but we need suffixes,
7276b6aa828SChristian Schoenebeck  * this function converts the Exp. Golomb prefixes into appropriate suffixes
7286b6aa828SChristian Schoenebeck  * which are still suitable for generating unique numbers.
7296b6aa828SChristian Schoenebeck  */
affixForIndex(uint64_t index)7306b6aa828SChristian Schoenebeck static VariLenAffix affixForIndex(uint64_t index)
7316b6aa828SChristian Schoenebeck {
7326b6aa828SChristian Schoenebeck     VariLenAffix prefix;
7336b6aa828SChristian Schoenebeck     prefix = expGolombEncode(index, EXP_GOLOMB_K);
7346b6aa828SChristian Schoenebeck     return invertAffix(&prefix); /* convert prefix to suffix */
7356b6aa828SChristian Schoenebeck }
7366b6aa828SChristian Schoenebeck 
qpp_hash(QppEntry e)7371a6ed33cSAntonios Motakis static uint32_t qpp_hash(QppEntry e)
7381a6ed33cSAntonios Motakis {
73980106bc5SAlex Bennée     return qemu_xxhash4(e.ino_prefix, e.dev);
7401a6ed33cSAntonios Motakis }
7411a6ed33cSAntonios Motakis 
qpf_hash(QpfEntry e)742f3fe4a2dSAntonios Motakis static uint32_t qpf_hash(QpfEntry e)
743f3fe4a2dSAntonios Motakis {
74480106bc5SAlex Bennée     return qemu_xxhash4(e.ino, e.dev);
745f3fe4a2dSAntonios Motakis }
746f3fe4a2dSAntonios Motakis 
qpd_cmp_func(const void * obj,const void * userp)7476b6aa828SChristian Schoenebeck static bool qpd_cmp_func(const void *obj, const void *userp)
7486b6aa828SChristian Schoenebeck {
7496b6aa828SChristian Schoenebeck     const QpdEntry *e1 = obj, *e2 = userp;
7506b6aa828SChristian Schoenebeck     return e1->dev == e2->dev;
7516b6aa828SChristian Schoenebeck }
7526b6aa828SChristian Schoenebeck 
qpp_cmp_func(const void * obj,const void * userp)7536b6aa828SChristian Schoenebeck static bool qpp_cmp_func(const void *obj, const void *userp)
7541a6ed33cSAntonios Motakis {
7551a6ed33cSAntonios Motakis     const QppEntry *e1 = obj, *e2 = userp;
7561a6ed33cSAntonios Motakis     return e1->dev == e2->dev && e1->ino_prefix == e2->ino_prefix;
7571a6ed33cSAntonios Motakis }
7581a6ed33cSAntonios Motakis 
qpf_cmp_func(const void * obj,const void * userp)7596b6aa828SChristian Schoenebeck static bool qpf_cmp_func(const void *obj, const void *userp)
760f3fe4a2dSAntonios Motakis {
761f3fe4a2dSAntonios Motakis     const QpfEntry *e1 = obj, *e2 = userp;
762f3fe4a2dSAntonios Motakis     return e1->dev == e2->dev && e1->ino == e2->ino;
763f3fe4a2dSAntonios Motakis }
764f3fe4a2dSAntonios Motakis 
qp_table_remove(void * p,uint32_t h,void * up)765f3fe4a2dSAntonios Motakis static void qp_table_remove(void *p, uint32_t h, void *up)
7661a6ed33cSAntonios Motakis {
7671a6ed33cSAntonios Motakis     g_free(p);
7681a6ed33cSAntonios Motakis }
7691a6ed33cSAntonios Motakis 
qp_table_destroy(struct qht * ht)770f3fe4a2dSAntonios Motakis static void qp_table_destroy(struct qht *ht)
7711a6ed33cSAntonios Motakis {
7721a6ed33cSAntonios Motakis     if (!ht || !ht->map) {
7731a6ed33cSAntonios Motakis         return;
7741a6ed33cSAntonios Motakis     }
775f3fe4a2dSAntonios Motakis     qht_iter(ht, qp_table_remove, NULL);
7761a6ed33cSAntonios Motakis     qht_destroy(ht);
7771a6ed33cSAntonios Motakis }
7781a6ed33cSAntonios Motakis 
qpd_table_init(struct qht * ht)7796b6aa828SChristian Schoenebeck static void qpd_table_init(struct qht *ht)
7806b6aa828SChristian Schoenebeck {
7816b6aa828SChristian Schoenebeck     qht_init(ht, qpd_cmp_func, 1, QHT_MODE_AUTO_RESIZE);
7826b6aa828SChristian Schoenebeck }
7836b6aa828SChristian Schoenebeck 
qpp_table_init(struct qht * ht)7841a6ed33cSAntonios Motakis static void qpp_table_init(struct qht *ht)
7851a6ed33cSAntonios Motakis {
7866b6aa828SChristian Schoenebeck     qht_init(ht, qpp_cmp_func, 1, QHT_MODE_AUTO_RESIZE);
7871a6ed33cSAntonios Motakis }
7881a6ed33cSAntonios Motakis 
qpf_table_init(struct qht * ht)789f3fe4a2dSAntonios Motakis static void qpf_table_init(struct qht *ht)
790f3fe4a2dSAntonios Motakis {
7916b6aa828SChristian Schoenebeck     qht_init(ht, qpf_cmp_func, 1 << 16, QHT_MODE_AUTO_RESIZE);
792f3fe4a2dSAntonios Motakis }
793f3fe4a2dSAntonios Motakis 
7946b6aa828SChristian Schoenebeck /*
7956b6aa828SChristian Schoenebeck  * Returns how many (high end) bits of inode numbers of the passed fs
7966b6aa828SChristian Schoenebeck  * device shall be used (in combination with the device number) to
7976b6aa828SChristian Schoenebeck  * generate hash values for qpp_table entries.
7986b6aa828SChristian Schoenebeck  *
7996b6aa828SChristian Schoenebeck  * This function is required if variable length suffixes are used for inode
8006b6aa828SChristian Schoenebeck  * number mapping on guest level. Since a device may end up having multiple
8016b6aa828SChristian Schoenebeck  * entries in qpp_table, each entry most probably with a different suffix
8026b6aa828SChristian Schoenebeck  * length, we thus need this function in conjunction with qpd_table to
8036b6aa828SChristian Schoenebeck  * "agree" about a fix amount of bits (per device) to be always used for
8046b6aa828SChristian Schoenebeck  * generating hash values for the purpose of accessing qpp_table in order
8056b6aa828SChristian Schoenebeck  * get consistent behaviour when accessing qpp_table.
8066b6aa828SChristian Schoenebeck  */
qid_inode_prefix_hash_bits(V9fsPDU * pdu,dev_t dev)8076b6aa828SChristian Schoenebeck static int qid_inode_prefix_hash_bits(V9fsPDU *pdu, dev_t dev)
8086b6aa828SChristian Schoenebeck {
8096b6aa828SChristian Schoenebeck     QpdEntry lookup = {
8106b6aa828SChristian Schoenebeck         .dev = dev
8116b6aa828SChristian Schoenebeck     }, *val;
8126b6aa828SChristian Schoenebeck     uint32_t hash = dev;
8136b6aa828SChristian Schoenebeck     VariLenAffix affix;
8146b6aa828SChristian Schoenebeck 
8156b6aa828SChristian Schoenebeck     val = qht_lookup(&pdu->s->qpd_table, &lookup, hash);
8166b6aa828SChristian Schoenebeck     if (!val) {
8171366244aSMarkus Armbruster         val = g_new0(QpdEntry, 1);
8186b6aa828SChristian Schoenebeck         *val = lookup;
8196b6aa828SChristian Schoenebeck         affix = affixForIndex(pdu->s->qp_affix_next);
8206b6aa828SChristian Schoenebeck         val->prefix_bits = affix.bits;
8216b6aa828SChristian Schoenebeck         qht_insert(&pdu->s->qpd_table, val, hash, NULL);
8226b6aa828SChristian Schoenebeck         pdu->s->qp_ndevices++;
8236b6aa828SChristian Schoenebeck     }
8246b6aa828SChristian Schoenebeck     return val->prefix_bits;
8256b6aa828SChristian Schoenebeck }
8266b6aa828SChristian Schoenebeck 
827e16fea41SChristian Schoenebeck /*
828e16fea41SChristian Schoenebeck  * Slow / full mapping host inode nr -> guest inode nr.
8296b6aa828SChristian Schoenebeck  *
8306b6aa828SChristian Schoenebeck  * This function performs a slower and much more costly remapping of an
8316b6aa828SChristian Schoenebeck  * original file inode number on host to an appropriate different inode
8326b6aa828SChristian Schoenebeck  * number on guest. For every (dev, inode) combination on host a new
8336b6aa828SChristian Schoenebeck  * sequential number is generated, cached and exposed as inode number on
8346b6aa828SChristian Schoenebeck  * guest.
8356b6aa828SChristian Schoenebeck  *
8366b6aa828SChristian Schoenebeck  * This is just a "last resort" fallback solution if the much faster/cheaper
8376b6aa828SChristian Schoenebeck  * qid_path_suffixmap() failed. In practice this slow / full mapping is not
8386b6aa828SChristian Schoenebeck  * expected ever to be used at all though.
8396b6aa828SChristian Schoenebeck  *
840e16fea41SChristian Schoenebeck  * See qid_path_suffixmap() for details
8416b6aa828SChristian Schoenebeck  *
8426b6aa828SChristian Schoenebeck  */
qid_path_fullmap(V9fsPDU * pdu,const struct stat * stbuf,uint64_t * path)843f3fe4a2dSAntonios Motakis static int qid_path_fullmap(V9fsPDU *pdu, const struct stat *stbuf,
844f3fe4a2dSAntonios Motakis                             uint64_t *path)
845f3fe4a2dSAntonios Motakis {
846f3fe4a2dSAntonios Motakis     QpfEntry lookup = {
847f3fe4a2dSAntonios Motakis         .dev = stbuf->st_dev,
848f3fe4a2dSAntonios Motakis         .ino = stbuf->st_ino
849f3fe4a2dSAntonios Motakis     }, *val;
850f3fe4a2dSAntonios Motakis     uint32_t hash = qpf_hash(lookup);
8516b6aa828SChristian Schoenebeck     VariLenAffix affix;
852f3fe4a2dSAntonios Motakis 
853f3fe4a2dSAntonios Motakis     val = qht_lookup(&pdu->s->qpf_table, &lookup, hash);
854f3fe4a2dSAntonios Motakis 
855f3fe4a2dSAntonios Motakis     if (!val) {
856f3fe4a2dSAntonios Motakis         if (pdu->s->qp_fullpath_next == 0) {
857f3fe4a2dSAntonios Motakis             /* no more files can be mapped :'( */
858f3fe4a2dSAntonios Motakis             error_report_once(
859f3fe4a2dSAntonios Motakis                 "9p: No more prefixes available for remapping inodes from "
860f3fe4a2dSAntonios Motakis                 "host to guest."
861f3fe4a2dSAntonios Motakis             );
862f3fe4a2dSAntonios Motakis             return -ENFILE;
863f3fe4a2dSAntonios Motakis         }
864f3fe4a2dSAntonios Motakis 
8651366244aSMarkus Armbruster         val = g_new0(QpfEntry, 1);
866f3fe4a2dSAntonios Motakis         *val = lookup;
867f3fe4a2dSAntonios Motakis 
868f3fe4a2dSAntonios Motakis         /* new unique inode and device combo */
8696b6aa828SChristian Schoenebeck         affix = affixForIndex(
8706b6aa828SChristian Schoenebeck             1ULL << (sizeof(pdu->s->qp_affix_next) * 8)
8716b6aa828SChristian Schoenebeck         );
8726b6aa828SChristian Schoenebeck         val->path = (pdu->s->qp_fullpath_next++ << affix.bits) | affix.value;
8736b6aa828SChristian Schoenebeck         pdu->s->qp_fullpath_next &= ((1ULL << (64 - affix.bits)) - 1);
874f3fe4a2dSAntonios Motakis         qht_insert(&pdu->s->qpf_table, val, hash, NULL);
875f3fe4a2dSAntonios Motakis     }
876f3fe4a2dSAntonios Motakis 
877f3fe4a2dSAntonios Motakis     *path = val->path;
878f3fe4a2dSAntonios Motakis     return 0;
879f3fe4a2dSAntonios Motakis }
880f3fe4a2dSAntonios Motakis 
881e16fea41SChristian Schoenebeck /*
882e16fea41SChristian Schoenebeck  * Quick mapping host inode nr -> guest inode nr.
8831a6ed33cSAntonios Motakis  *
8846b6aa828SChristian Schoenebeck  * This function performs quick remapping of an original file inode number
8856b6aa828SChristian Schoenebeck  * on host to an appropriate different inode number on guest. This remapping
8866b6aa828SChristian Schoenebeck  * of inodes is required to avoid inode nr collisions on guest which would
8876b6aa828SChristian Schoenebeck  * happen if the 9p export contains more than 1 exported file system (or
8886b6aa828SChristian Schoenebeck  * more than 1 file system data set), because unlike on host level where the
8896b6aa828SChristian Schoenebeck  * files would have different device nrs, all files exported by 9p would
8906b6aa828SChristian Schoenebeck  * share the same device nr on guest (the device nr of the virtual 9p device
8916b6aa828SChristian Schoenebeck  * that is).
8926b6aa828SChristian Schoenebeck  *
8936b6aa828SChristian Schoenebeck  * Inode remapping is performed by chopping off high end bits of the original
8946b6aa828SChristian Schoenebeck  * inode number from host, shifting the result upwards and then assigning a
8956b6aa828SChristian Schoenebeck  * generated suffix number for the low end bits, where the same suffix number
8966b6aa828SChristian Schoenebeck  * will be shared by all inodes with the same device id AND the same high end
8976b6aa828SChristian Schoenebeck  * bits that have been chopped off. That approach utilizes the fact that inode
8986b6aa828SChristian Schoenebeck  * numbers very likely share the same high end bits (i.e. due to their common
8996b6aa828SChristian Schoenebeck  * sequential generation by file systems) and hence we only have to generate
9006b6aa828SChristian Schoenebeck  * and track a very limited amount of suffixes in practice due to that.
9016b6aa828SChristian Schoenebeck  *
9026b6aa828SChristian Schoenebeck  * We generate variable size suffixes for that purpose. The 1st generated
9036b6aa828SChristian Schoenebeck  * suffix will only have 1 bit and hence we only need to chop off 1 bit from
9046b6aa828SChristian Schoenebeck  * the original inode number. The subsequent suffixes being generated will
9056b6aa828SChristian Schoenebeck  * grow in (bit) size subsequently, i.e. the 2nd and 3rd suffix being
9066b6aa828SChristian Schoenebeck  * generated will have 3 bits and hence we have to chop off 3 bits from their
9076b6aa828SChristian Schoenebeck  * original inodes, and so on. That approach of using variable length suffixes
9086b6aa828SChristian Schoenebeck  * (i.e. over fixed size ones) utilizes the fact that in practice only a very
9096b6aa828SChristian Schoenebeck  * limited amount of devices are shared by the same export (e.g. typically
9106b6aa828SChristian Schoenebeck  * less than 2 dozen devices per 9p export), so in practice we need to chop
9116b6aa828SChristian Schoenebeck  * off less bits than with fixed size prefixes and yet are flexible to add
9126b6aa828SChristian Schoenebeck  * new devices at runtime below host's export directory at any time without
9136b6aa828SChristian Schoenebeck  * having to reboot guest nor requiring to reconfigure guest for that. And due
9146b6aa828SChristian Schoenebeck  * to the very limited amount of original high end bits that we chop off that
9156b6aa828SChristian Schoenebeck  * way, the total amount of suffixes we need to generate is less than by using
9166b6aa828SChristian Schoenebeck  * fixed size prefixes and hence it also improves performance of the inode
9176b6aa828SChristian Schoenebeck  * remapping algorithm, and finally has the nice side effect that the inode
9186b6aa828SChristian Schoenebeck  * numbers on guest will be much smaller & human friendly. ;-)
9191a6ed33cSAntonios Motakis  */
qid_path_suffixmap(V9fsPDU * pdu,const struct stat * stbuf,uint64_t * path)9206b6aa828SChristian Schoenebeck static int qid_path_suffixmap(V9fsPDU *pdu, const struct stat *stbuf,
9211a6ed33cSAntonios Motakis                               uint64_t *path)
9221a6ed33cSAntonios Motakis {
9236b6aa828SChristian Schoenebeck     const int ino_hash_bits = qid_inode_prefix_hash_bits(pdu, stbuf->st_dev);
9241a6ed33cSAntonios Motakis     QppEntry lookup = {
9251a6ed33cSAntonios Motakis         .dev = stbuf->st_dev,
9266b6aa828SChristian Schoenebeck         .ino_prefix = (uint16_t) (stbuf->st_ino >> (64 - ino_hash_bits))
9271a6ed33cSAntonios Motakis     }, *val;
9281a6ed33cSAntonios Motakis     uint32_t hash = qpp_hash(lookup);
9291a6ed33cSAntonios Motakis 
9301a6ed33cSAntonios Motakis     val = qht_lookup(&pdu->s->qpp_table, &lookup, hash);
9311a6ed33cSAntonios Motakis 
9321a6ed33cSAntonios Motakis     if (!val) {
9336b6aa828SChristian Schoenebeck         if (pdu->s->qp_affix_next == 0) {
9346b6aa828SChristian Schoenebeck             /* we ran out of affixes */
935f3fe4a2dSAntonios Motakis             warn_report_once(
936f3fe4a2dSAntonios Motakis                 "9p: Potential degraded performance of inode remapping"
9371a6ed33cSAntonios Motakis             );
9381a6ed33cSAntonios Motakis             return -ENFILE;
9391a6ed33cSAntonios Motakis         }
9401a6ed33cSAntonios Motakis 
9411366244aSMarkus Armbruster         val = g_new0(QppEntry, 1);
9421a6ed33cSAntonios Motakis         *val = lookup;
9431a6ed33cSAntonios Motakis 
9446b6aa828SChristian Schoenebeck         /* new unique inode affix and device combo */
9456b6aa828SChristian Schoenebeck         val->qp_affix_index = pdu->s->qp_affix_next++;
9466b6aa828SChristian Schoenebeck         val->qp_affix = affixForIndex(val->qp_affix_index);
9471a6ed33cSAntonios Motakis         qht_insert(&pdu->s->qpp_table, val, hash, NULL);
9481a6ed33cSAntonios Motakis     }
9496b6aa828SChristian Schoenebeck     /* assuming generated affix to be suffix type, not prefix */
9506b6aa828SChristian Schoenebeck     *path = (stbuf->st_ino << val->qp_affix.bits) | val->qp_affix.value;
9511a6ed33cSAntonios Motakis     return 0;
9521a6ed33cSAntonios Motakis }
9531a6ed33cSAntonios Motakis 
stat_to_qid(V9fsPDU * pdu,const struct stat * stbuf,V9fsQID * qidp)9543b5ee9e8SAntonios Motakis static int stat_to_qid(V9fsPDU *pdu, const struct stat *stbuf, V9fsQID *qidp)
95560ce86c7SWei Liu {
9561a6ed33cSAntonios Motakis     int err;
95760ce86c7SWei Liu     size_t size;
95860ce86c7SWei Liu 
9591a6ed33cSAntonios Motakis     if (pdu->s->ctx.export_flags & V9FS_REMAP_INODES) {
9601a6ed33cSAntonios Motakis         /* map inode+device to qid path (fast path) */
9616b6aa828SChristian Schoenebeck         err = qid_path_suffixmap(pdu, stbuf, &qidp->path);
962f3fe4a2dSAntonios Motakis         if (err == -ENFILE) {
963f3fe4a2dSAntonios Motakis             /* fast path didn't work, fall back to full map */
964f3fe4a2dSAntonios Motakis             err = qid_path_fullmap(pdu, stbuf, &qidp->path);
965f3fe4a2dSAntonios Motakis         }
9661a6ed33cSAntonios Motakis         if (err) {
9671a6ed33cSAntonios Motakis             return err;
9681a6ed33cSAntonios Motakis         }
9691a6ed33cSAntonios Motakis     } else {
9703b5ee9e8SAntonios Motakis         if (pdu->s->dev_id != stbuf->st_dev) {
9711a6ed33cSAntonios Motakis             if (pdu->s->ctx.export_flags & V9FS_FORBID_MULTIDEVS) {
9721a6ed33cSAntonios Motakis                 error_report_once(
9731a6ed33cSAntonios Motakis                     "9p: Multiple devices detected in same VirtFS export. "
9741a6ed33cSAntonios Motakis                     "Access of guest to additional devices is (partly) "
9751a6ed33cSAntonios Motakis                     "denied due to virtfs option 'multidevs=forbid' being "
9761a6ed33cSAntonios Motakis                     "effective."
9771a6ed33cSAntonios Motakis                 );
9781a6ed33cSAntonios Motakis                 return -ENODEV;
9791a6ed33cSAntonios Motakis             } else {
9803b5ee9e8SAntonios Motakis                 warn_report_once(
9813b5ee9e8SAntonios Motakis                     "9p: Multiple devices detected in same VirtFS export, "
9823b5ee9e8SAntonios Motakis                     "which might lead to file ID collisions and severe "
9831a6ed33cSAntonios Motakis                     "misbehaviours on guest! You should either use a "
9841a6ed33cSAntonios Motakis                     "separate export for each device shared from host or "
9851a6ed33cSAntonios Motakis                     "use virtfs option 'multidevs=remap'!"
9863b5ee9e8SAntonios Motakis                 );
9873b5ee9e8SAntonios Motakis             }
9881a6ed33cSAntonios Motakis         }
98960ce86c7SWei Liu         memset(&qidp->path, 0, sizeof(qidp->path));
99060ce86c7SWei Liu         size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
99160ce86c7SWei Liu         memcpy(&qidp->path, &stbuf->st_ino, size);
9921a6ed33cSAntonios Motakis     }
9931a6ed33cSAntonios Motakis 
99460ce86c7SWei Liu     qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
99560ce86c7SWei Liu     qidp->type = 0;
99660ce86c7SWei Liu     if (S_ISDIR(stbuf->st_mode)) {
99760ce86c7SWei Liu         qidp->type |= P9_QID_TYPE_DIR;
99860ce86c7SWei Liu     }
99960ce86c7SWei Liu     if (S_ISLNK(stbuf->st_mode)) {
100060ce86c7SWei Liu         qidp->type |= P9_QID_TYPE_SYMLINK;
100160ce86c7SWei Liu     }
10023b5ee9e8SAntonios Motakis 
10033b5ee9e8SAntonios Motakis     return 0;
100460ce86c7SWei Liu }
100560ce86c7SWei Liu 
pdu_alloc(V9fsState * s)100660ce86c7SWei Liu V9fsPDU *pdu_alloc(V9fsState *s)
100760ce86c7SWei Liu {
100860ce86c7SWei Liu     V9fsPDU *pdu = NULL;
100960ce86c7SWei Liu 
101060ce86c7SWei Liu     if (!QLIST_EMPTY(&s->free_list)) {
101160ce86c7SWei Liu         pdu = QLIST_FIRST(&s->free_list);
101260ce86c7SWei Liu         QLIST_REMOVE(pdu, next);
101360ce86c7SWei Liu         QLIST_INSERT_HEAD(&s->active_list, pdu, next);
101460ce86c7SWei Liu     }
101560ce86c7SWei Liu     return pdu;
101660ce86c7SWei Liu }
101760ce86c7SWei Liu 
pdu_free(V9fsPDU * pdu)101860ce86c7SWei Liu void pdu_free(V9fsPDU *pdu)
101960ce86c7SWei Liu {
102060ce86c7SWei Liu     V9fsState *s = pdu->s;
1021f74e27bfSGreg Kurz 
1022f74e27bfSGreg Kurz     g_assert(!pdu->cancelled);
102360ce86c7SWei Liu     QLIST_REMOVE(pdu, next);
102460ce86c7SWei Liu     QLIST_INSERT_HEAD(&s->free_list, pdu, next);
102560ce86c7SWei Liu }
102660ce86c7SWei Liu 
pdu_complete(V9fsPDU * pdu,ssize_t len)10278440e22eSGreg Kurz static void coroutine_fn pdu_complete(V9fsPDU *pdu, ssize_t len)
102860ce86c7SWei Liu {
102960ce86c7SWei Liu     int8_t id = pdu->id + 1; /* Response */
103060ce86c7SWei Liu     V9fsState *s = pdu->s;
103106a37db7SGreg Kurz     int ret;
103260ce86c7SWei Liu 
1033fc78d5eeSKeno Fischer     /*
1034fc78d5eeSKeno Fischer      * The 9p spec requires that successfully cancelled pdus receive no reply.
1035fc78d5eeSKeno Fischer      * Sending a reply would confuse clients because they would
1036fc78d5eeSKeno Fischer      * assume that any EINTR is the actual result of the operation,
1037fc78d5eeSKeno Fischer      * rather than a consequence of the cancellation. However, if
103828cbbdd2SMichael Tokarev      * the operation completed (successfully or with an error other
1039fc78d5eeSKeno Fischer      * than caused be cancellation), we do send out that reply, both
1040fc78d5eeSKeno Fischer      * for efficiency and to avoid confusing the rest of the state machine
1041fc78d5eeSKeno Fischer      * that assumes passing a non-error here will mean a successful
1042fc78d5eeSKeno Fischer      * transmission of the reply.
1043fc78d5eeSKeno Fischer      */
1044fc78d5eeSKeno Fischer     bool discard = pdu->cancelled && len == -EINTR;
1045fc78d5eeSKeno Fischer     if (discard) {
1046fc78d5eeSKeno Fischer         trace_v9fs_rcancel(pdu->tag, pdu->id);
1047fc78d5eeSKeno Fischer         pdu->size = 0;
1048fc78d5eeSKeno Fischer         goto out_notify;
1049fc78d5eeSKeno Fischer     }
1050fc78d5eeSKeno Fischer 
105160ce86c7SWei Liu     if (len < 0) {
105260ce86c7SWei Liu         int err = -len;
105360ce86c7SWei Liu         len = 7;
105460ce86c7SWei Liu 
105560ce86c7SWei Liu         if (s->proto_version != V9FS_PROTO_2000L) {
105660ce86c7SWei Liu             V9fsString str;
105760ce86c7SWei Liu 
105860ce86c7SWei Liu             str.data = strerror(err);
105960ce86c7SWei Liu             str.size = strlen(str.data);
106060ce86c7SWei Liu 
106106a37db7SGreg Kurz             ret = pdu_marshal(pdu, len, "s", &str);
106206a37db7SGreg Kurz             if (ret < 0) {
106306a37db7SGreg Kurz                 goto out_notify;
106406a37db7SGreg Kurz             }
106506a37db7SGreg Kurz             len += ret;
106660ce86c7SWei Liu             id = P9_RERROR;
1067951fe2f8SChristian Schoenebeck         } else {
1068951fe2f8SChristian Schoenebeck             err = errno_to_dotl(err);
106960ce86c7SWei Liu         }
107060ce86c7SWei Liu 
107106a37db7SGreg Kurz         ret = pdu_marshal(pdu, len, "d", err);
107206a37db7SGreg Kurz         if (ret < 0) {
107306a37db7SGreg Kurz             goto out_notify;
107406a37db7SGreg Kurz         }
107506a37db7SGreg Kurz         len += ret;
107660ce86c7SWei Liu 
107760ce86c7SWei Liu         if (s->proto_version == V9FS_PROTO_2000L) {
107860ce86c7SWei Liu             id = P9_RLERROR;
107960ce86c7SWei Liu         }
108060ce86c7SWei Liu         trace_v9fs_rerror(pdu->tag, pdu->id, err); /* Trace ERROR */
108160ce86c7SWei Liu     }
108260ce86c7SWei Liu 
108360ce86c7SWei Liu     /* fill out the header */
108406a37db7SGreg Kurz     if (pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag) < 0) {
108506a37db7SGreg Kurz         goto out_notify;
108606a37db7SGreg Kurz     }
108760ce86c7SWei Liu 
108860ce86c7SWei Liu     /* keep these in sync */
108960ce86c7SWei Liu     pdu->size = len;
109060ce86c7SWei Liu     pdu->id = id;
109160ce86c7SWei Liu 
109206a37db7SGreg Kurz out_notify:
1093a17d8659SGreg Kurz     pdu->s->transport->push_and_notify(pdu);
109460ce86c7SWei Liu 
109560ce86c7SWei Liu     /* Now wakeup anybody waiting in flush for this request */
1096f74e27bfSGreg Kurz     if (!qemu_co_queue_next(&pdu->complete)) {
109760ce86c7SWei Liu         pdu_free(pdu);
109860ce86c7SWei Liu     }
1099f74e27bfSGreg Kurz }
110060ce86c7SWei Liu 
v9mode_to_mode(uint32_t mode,V9fsString * extension)110160ce86c7SWei Liu static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
110260ce86c7SWei Liu {
110360ce86c7SWei Liu     mode_t ret;
110460ce86c7SWei Liu 
110560ce86c7SWei Liu     ret = mode & 0777;
110660ce86c7SWei Liu     if (mode & P9_STAT_MODE_DIR) {
110760ce86c7SWei Liu         ret |= S_IFDIR;
110860ce86c7SWei Liu     }
110960ce86c7SWei Liu 
111060ce86c7SWei Liu     if (mode & P9_STAT_MODE_SYMLINK) {
111160ce86c7SWei Liu         ret |= S_IFLNK;
111260ce86c7SWei Liu     }
111360ce86c7SWei Liu     if (mode & P9_STAT_MODE_SOCKET) {
111460ce86c7SWei Liu         ret |= S_IFSOCK;
111560ce86c7SWei Liu     }
111660ce86c7SWei Liu     if (mode & P9_STAT_MODE_NAMED_PIPE) {
111760ce86c7SWei Liu         ret |= S_IFIFO;
111860ce86c7SWei Liu     }
111960ce86c7SWei Liu     if (mode & P9_STAT_MODE_DEVICE) {
112060ce86c7SWei Liu         if (extension->size && extension->data[0] == 'c') {
112160ce86c7SWei Liu             ret |= S_IFCHR;
112260ce86c7SWei Liu         } else {
112360ce86c7SWei Liu             ret |= S_IFBLK;
112460ce86c7SWei Liu         }
112560ce86c7SWei Liu     }
112660ce86c7SWei Liu 
112760ce86c7SWei Liu     if (!(ret & ~0777)) {
112860ce86c7SWei Liu         ret |= S_IFREG;
112960ce86c7SWei Liu     }
113060ce86c7SWei Liu 
113160ce86c7SWei Liu     if (mode & P9_STAT_MODE_SETUID) {
113260ce86c7SWei Liu         ret |= S_ISUID;
113360ce86c7SWei Liu     }
113460ce86c7SWei Liu     if (mode & P9_STAT_MODE_SETGID) {
113560ce86c7SWei Liu         ret |= S_ISGID;
113660ce86c7SWei Liu     }
113760ce86c7SWei Liu     if (mode & P9_STAT_MODE_SETVTX) {
113860ce86c7SWei Liu         ret |= S_ISVTX;
113960ce86c7SWei Liu     }
114060ce86c7SWei Liu 
114160ce86c7SWei Liu     return ret;
114260ce86c7SWei Liu }
114360ce86c7SWei Liu 
donttouch_stat(V9fsStat * stat)114460ce86c7SWei Liu static int donttouch_stat(V9fsStat *stat)
114560ce86c7SWei Liu {
114660ce86c7SWei Liu     if (stat->type == -1 &&
114760ce86c7SWei Liu         stat->dev == -1 &&
114887032833SAntonios Motakis         stat->qid.type == 0xff &&
114987032833SAntonios Motakis         stat->qid.version == (uint32_t) -1 &&
115087032833SAntonios Motakis         stat->qid.path == (uint64_t) -1 &&
115160ce86c7SWei Liu         stat->mode == -1 &&
115260ce86c7SWei Liu         stat->atime == -1 &&
115360ce86c7SWei Liu         stat->mtime == -1 &&
115460ce86c7SWei Liu         stat->length == -1 &&
115560ce86c7SWei Liu         !stat->name.size &&
115660ce86c7SWei Liu         !stat->uid.size &&
115760ce86c7SWei Liu         !stat->gid.size &&
115860ce86c7SWei Liu         !stat->muid.size &&
115960ce86c7SWei Liu         stat->n_uid == -1 &&
116060ce86c7SWei Liu         stat->n_gid == -1 &&
116160ce86c7SWei Liu         stat->n_muid == -1) {
116260ce86c7SWei Liu         return 1;
116360ce86c7SWei Liu     }
116460ce86c7SWei Liu 
116560ce86c7SWei Liu     return 0;
116660ce86c7SWei Liu }
116760ce86c7SWei Liu 
v9fs_stat_init(V9fsStat * stat)116860ce86c7SWei Liu static void v9fs_stat_init(V9fsStat *stat)
116960ce86c7SWei Liu {
117060ce86c7SWei Liu     v9fs_string_init(&stat->name);
117160ce86c7SWei Liu     v9fs_string_init(&stat->uid);
117260ce86c7SWei Liu     v9fs_string_init(&stat->gid);
117360ce86c7SWei Liu     v9fs_string_init(&stat->muid);
117460ce86c7SWei Liu     v9fs_string_init(&stat->extension);
117560ce86c7SWei Liu }
117660ce86c7SWei Liu 
v9fs_stat_free(V9fsStat * stat)117760ce86c7SWei Liu static void v9fs_stat_free(V9fsStat *stat)
117860ce86c7SWei Liu {
117960ce86c7SWei Liu     v9fs_string_free(&stat->name);
118060ce86c7SWei Liu     v9fs_string_free(&stat->uid);
118160ce86c7SWei Liu     v9fs_string_free(&stat->gid);
118260ce86c7SWei Liu     v9fs_string_free(&stat->muid);
118360ce86c7SWei Liu     v9fs_string_free(&stat->extension);
118460ce86c7SWei Liu }
118560ce86c7SWei Liu 
stat_to_v9mode(const struct stat * stbuf)118660ce86c7SWei Liu static uint32_t stat_to_v9mode(const struct stat *stbuf)
118760ce86c7SWei Liu {
118860ce86c7SWei Liu     uint32_t mode;
118960ce86c7SWei Liu 
119060ce86c7SWei Liu     mode = stbuf->st_mode & 0777;
119160ce86c7SWei Liu     if (S_ISDIR(stbuf->st_mode)) {
119260ce86c7SWei Liu         mode |= P9_STAT_MODE_DIR;
119360ce86c7SWei Liu     }
119460ce86c7SWei Liu 
119560ce86c7SWei Liu     if (S_ISLNK(stbuf->st_mode)) {
119660ce86c7SWei Liu         mode |= P9_STAT_MODE_SYMLINK;
119760ce86c7SWei Liu     }
119860ce86c7SWei Liu 
119960ce86c7SWei Liu     if (S_ISSOCK(stbuf->st_mode)) {
120060ce86c7SWei Liu         mode |= P9_STAT_MODE_SOCKET;
120160ce86c7SWei Liu     }
120260ce86c7SWei Liu 
120360ce86c7SWei Liu     if (S_ISFIFO(stbuf->st_mode)) {
120460ce86c7SWei Liu         mode |= P9_STAT_MODE_NAMED_PIPE;
120560ce86c7SWei Liu     }
120660ce86c7SWei Liu 
120760ce86c7SWei Liu     if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
120860ce86c7SWei Liu         mode |= P9_STAT_MODE_DEVICE;
120960ce86c7SWei Liu     }
121060ce86c7SWei Liu 
121160ce86c7SWei Liu     if (stbuf->st_mode & S_ISUID) {
121260ce86c7SWei Liu         mode |= P9_STAT_MODE_SETUID;
121360ce86c7SWei Liu     }
121460ce86c7SWei Liu 
121560ce86c7SWei Liu     if (stbuf->st_mode & S_ISGID) {
121660ce86c7SWei Liu         mode |= P9_STAT_MODE_SETGID;
121760ce86c7SWei Liu     }
121860ce86c7SWei Liu 
121960ce86c7SWei Liu     if (stbuf->st_mode & S_ISVTX) {
122060ce86c7SWei Liu         mode |= P9_STAT_MODE_SETVTX;
122160ce86c7SWei Liu     }
122260ce86c7SWei Liu 
122360ce86c7SWei Liu     return mode;
122460ce86c7SWei Liu }
122560ce86c7SWei Liu 
stat_to_v9stat(V9fsPDU * pdu,V9fsPath * path,const char * basename,const struct stat * stbuf,V9fsStat * v9stat)12266069537fSJan Dakinevich static int coroutine_fn stat_to_v9stat(V9fsPDU *pdu, V9fsPath *path,
12276069537fSJan Dakinevich                                        const char *basename,
122860ce86c7SWei Liu                                        const struct stat *stbuf,
122960ce86c7SWei Liu                                        V9fsStat *v9stat)
123060ce86c7SWei Liu {
123160ce86c7SWei Liu     int err;
123260ce86c7SWei Liu 
123360ce86c7SWei Liu     memset(v9stat, 0, sizeof(*v9stat));
123460ce86c7SWei Liu 
12353b5ee9e8SAntonios Motakis     err = stat_to_qid(pdu, stbuf, &v9stat->qid);
12363b5ee9e8SAntonios Motakis     if (err < 0) {
12373b5ee9e8SAntonios Motakis         return err;
12383b5ee9e8SAntonios Motakis     }
123960ce86c7SWei Liu     v9stat->mode = stat_to_v9mode(stbuf);
124060ce86c7SWei Liu     v9stat->atime = stbuf->st_atime;
124160ce86c7SWei Liu     v9stat->mtime = stbuf->st_mtime;
124260ce86c7SWei Liu     v9stat->length = stbuf->st_size;
124360ce86c7SWei Liu 
1244abdf0086SGreg Kurz     v9fs_string_free(&v9stat->uid);
1245abdf0086SGreg Kurz     v9fs_string_free(&v9stat->gid);
1246abdf0086SGreg Kurz     v9fs_string_free(&v9stat->muid);
124760ce86c7SWei Liu 
124860ce86c7SWei Liu     v9stat->n_uid = stbuf->st_uid;
124960ce86c7SWei Liu     v9stat->n_gid = stbuf->st_gid;
125060ce86c7SWei Liu     v9stat->n_muid = 0;
125160ce86c7SWei Liu 
1252abdf0086SGreg Kurz     v9fs_string_free(&v9stat->extension);
125360ce86c7SWei Liu 
125460ce86c7SWei Liu     if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
12556069537fSJan Dakinevich         err = v9fs_co_readlink(pdu, path, &v9stat->extension);
125660ce86c7SWei Liu         if (err < 0) {
125760ce86c7SWei Liu             return err;
125860ce86c7SWei Liu         }
125960ce86c7SWei Liu     } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
126060ce86c7SWei Liu         v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
126160ce86c7SWei Liu                 S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
126260ce86c7SWei Liu                 major(stbuf->st_rdev), minor(stbuf->st_rdev));
126360ce86c7SWei Liu     } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
126460ce86c7SWei Liu         v9fs_string_sprintf(&v9stat->extension, "%s %lu",
126560ce86c7SWei Liu                 "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink);
126660ce86c7SWei Liu     }
126760ce86c7SWei Liu 
12686069537fSJan Dakinevich     v9fs_string_sprintf(&v9stat->name, "%s", basename);
126960ce86c7SWei Liu 
127060ce86c7SWei Liu     v9stat->size = 61 +
127160ce86c7SWei Liu         v9fs_string_size(&v9stat->name) +
127260ce86c7SWei Liu         v9fs_string_size(&v9stat->uid) +
127360ce86c7SWei Liu         v9fs_string_size(&v9stat->gid) +
127460ce86c7SWei Liu         v9fs_string_size(&v9stat->muid) +
127560ce86c7SWei Liu         v9fs_string_size(&v9stat->extension);
127660ce86c7SWei Liu     return 0;
127760ce86c7SWei Liu }
127860ce86c7SWei Liu 
127960ce86c7SWei Liu #define P9_STATS_MODE          0x00000001ULL
128060ce86c7SWei Liu #define P9_STATS_NLINK         0x00000002ULL
128160ce86c7SWei Liu #define P9_STATS_UID           0x00000004ULL
128260ce86c7SWei Liu #define P9_STATS_GID           0x00000008ULL
128360ce86c7SWei Liu #define P9_STATS_RDEV          0x00000010ULL
128460ce86c7SWei Liu #define P9_STATS_ATIME         0x00000020ULL
128560ce86c7SWei Liu #define P9_STATS_MTIME         0x00000040ULL
128660ce86c7SWei Liu #define P9_STATS_CTIME         0x00000080ULL
128760ce86c7SWei Liu #define P9_STATS_INO           0x00000100ULL
128860ce86c7SWei Liu #define P9_STATS_SIZE          0x00000200ULL
128960ce86c7SWei Liu #define P9_STATS_BLOCKS        0x00000400ULL
129060ce86c7SWei Liu 
129160ce86c7SWei Liu #define P9_STATS_BTIME         0x00000800ULL
129260ce86c7SWei Liu #define P9_STATS_GEN           0x00001000ULL
129360ce86c7SWei Liu #define P9_STATS_DATA_VERSION  0x00002000ULL
129460ce86c7SWei Liu 
129560ce86c7SWei Liu #define P9_STATS_BASIC         0x000007ffULL /* Mask for fields up to BLOCKS */
129660ce86c7SWei Liu #define P9_STATS_ALL           0x00003fffULL /* Mask for All fields above */
129760ce86c7SWei Liu 
129860ce86c7SWei Liu 
1299b565bccbSChristian Schoenebeck /**
1300e16fea41SChristian Schoenebeck  * blksize_to_iounit() - Block size exposed to 9p client.
1301e16fea41SChristian Schoenebeck  * Return: block size
1302b565bccbSChristian Schoenebeck  *
1303b565bccbSChristian Schoenebeck  * @pdu: 9p client request
1304b565bccbSChristian Schoenebeck  * @blksize: host filesystem's block size
1305e16fea41SChristian Schoenebeck  *
1306e16fea41SChristian Schoenebeck  * Convert host filesystem's block size into an appropriate block size for
1307e16fea41SChristian Schoenebeck  * 9p client (guest OS side). The value returned suggests an "optimum" block
1308e16fea41SChristian Schoenebeck  * size for 9p I/O, i.e. to maximize performance.
1309b565bccbSChristian Schoenebeck  */
blksize_to_iounit(const V9fsPDU * pdu,int32_t blksize)1310b565bccbSChristian Schoenebeck static int32_t blksize_to_iounit(const V9fsPDU *pdu, int32_t blksize)
1311669ced09SChristian Schoenebeck {
1312669ced09SChristian Schoenebeck     int32_t iounit = 0;
1313669ced09SChristian Schoenebeck     V9fsState *s = pdu->s;
1314669ced09SChristian Schoenebeck 
1315669ced09SChristian Schoenebeck     /*
1316b565bccbSChristian Schoenebeck      * iounit should be multiples of blksize (host filesystem block size)
1317669ced09SChristian Schoenebeck      * as well as less than (client msize - P9_IOHDRSZ)
1318669ced09SChristian Schoenebeck      */
1319b565bccbSChristian Schoenebeck     if (blksize) {
132004a7f9e5SChristian Schoenebeck         iounit = QEMU_ALIGN_DOWN(s->msize - P9_IOHDRSZ, blksize);
1321669ced09SChristian Schoenebeck     }
1322669ced09SChristian Schoenebeck     if (!iounit) {
1323669ced09SChristian Schoenebeck         iounit = s->msize - P9_IOHDRSZ;
1324669ced09SChristian Schoenebeck     }
1325669ced09SChristian Schoenebeck     return iounit;
1326669ced09SChristian Schoenebeck }
1327669ced09SChristian Schoenebeck 
stat_to_iounit(const V9fsPDU * pdu,const struct stat * stbuf)1328b565bccbSChristian Schoenebeck static int32_t stat_to_iounit(const V9fsPDU *pdu, const struct stat *stbuf)
1329b565bccbSChristian Schoenebeck {
1330b565bccbSChristian Schoenebeck     return blksize_to_iounit(pdu, stbuf->st_blksize);
1331b565bccbSChristian Schoenebeck }
1332b565bccbSChristian Schoenebeck 
stat_to_v9stat_dotl(V9fsPDU * pdu,const struct stat * stbuf,V9fsStatDotl * v9lstat)13333b5ee9e8SAntonios Motakis static int stat_to_v9stat_dotl(V9fsPDU *pdu, const struct stat *stbuf,
133460ce86c7SWei Liu                                 V9fsStatDotl *v9lstat)
133560ce86c7SWei Liu {
133660ce86c7SWei Liu     memset(v9lstat, 0, sizeof(*v9lstat));
133760ce86c7SWei Liu 
133860ce86c7SWei Liu     v9lstat->st_mode = stbuf->st_mode;
133960ce86c7SWei Liu     v9lstat->st_nlink = stbuf->st_nlink;
134060ce86c7SWei Liu     v9lstat->st_uid = stbuf->st_uid;
134160ce86c7SWei Liu     v9lstat->st_gid = stbuf->st_gid;
1342e5c88e22SChristian Schoenebeck     v9lstat->st_rdev = host_dev_to_dotl_dev(stbuf->st_rdev);
134360ce86c7SWei Liu     v9lstat->st_size = stbuf->st_size;
1344669ced09SChristian Schoenebeck     v9lstat->st_blksize = stat_to_iounit(pdu, stbuf);
134560ce86c7SWei Liu     v9lstat->st_blocks = stbuf->st_blocks;
134660ce86c7SWei Liu     v9lstat->st_atime_sec = stbuf->st_atime;
134760ce86c7SWei Liu     v9lstat->st_mtime_sec = stbuf->st_mtime;
134860ce86c7SWei Liu     v9lstat->st_ctime_sec = stbuf->st_ctime;
1349f41db099SKeno Fischer #ifdef CONFIG_DARWIN
1350f41db099SKeno Fischer     v9lstat->st_atime_nsec = stbuf->st_atimespec.tv_nsec;
1351f41db099SKeno Fischer     v9lstat->st_mtime_nsec = stbuf->st_mtimespec.tv_nsec;
1352f41db099SKeno Fischer     v9lstat->st_ctime_nsec = stbuf->st_ctimespec.tv_nsec;
1353f41db099SKeno Fischer #else
1354f41db099SKeno Fischer     v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
1355f41db099SKeno Fischer     v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
135660ce86c7SWei Liu     v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
1357f41db099SKeno Fischer #endif
135860ce86c7SWei Liu     /* Currently we only support BASIC fields in stat */
135960ce86c7SWei Liu     v9lstat->st_result_mask = P9_STATS_BASIC;
136060ce86c7SWei Liu 
13613b5ee9e8SAntonios Motakis     return stat_to_qid(pdu, stbuf, &v9lstat->qid);
136260ce86c7SWei Liu }
136360ce86c7SWei Liu 
print_sg(struct iovec * sg,int cnt)136460ce86c7SWei Liu static void print_sg(struct iovec *sg, int cnt)
136560ce86c7SWei Liu {
136660ce86c7SWei Liu     int i;
136760ce86c7SWei Liu 
136860ce86c7SWei Liu     printf("sg[%d]: {", cnt);
136960ce86c7SWei Liu     for (i = 0; i < cnt; i++) {
137060ce86c7SWei Liu         if (i) {
137160ce86c7SWei Liu             printf(", ");
137260ce86c7SWei Liu         }
137360ce86c7SWei Liu         printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
137460ce86c7SWei Liu     }
137560ce86c7SWei Liu     printf("}\n");
137660ce86c7SWei Liu }
137760ce86c7SWei Liu 
137860ce86c7SWei Liu /* Will call this only for path name based fid */
v9fs_fix_path(V9fsPath * dst,V9fsPath * src,int len)137960ce86c7SWei Liu static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len)
138060ce86c7SWei Liu {
138160ce86c7SWei Liu     V9fsPath str;
138260ce86c7SWei Liu     v9fs_path_init(&str);
138360ce86c7SWei Liu     v9fs_path_copy(&str, dst);
1384e3e83f2eSGreg Kurz     v9fs_path_sprintf(dst, "%s%s", src->data, str.data + len);
138560ce86c7SWei Liu     v9fs_path_free(&str);
138660ce86c7SWei Liu }
138760ce86c7SWei Liu 
is_ro_export(FsContext * ctx)138860ce86c7SWei Liu static inline bool is_ro_export(FsContext *ctx)
138960ce86c7SWei Liu {
139060ce86c7SWei Liu     return ctx->export_flags & V9FS_RDONLY;
139160ce86c7SWei Liu }
139260ce86c7SWei Liu 
v9fs_version(void * opaque)13938440e22eSGreg Kurz static void coroutine_fn v9fs_version(void *opaque)
139460ce86c7SWei Liu {
139560ce86c7SWei Liu     ssize_t err;
139660ce86c7SWei Liu     V9fsPDU *pdu = opaque;
139760ce86c7SWei Liu     V9fsState *s = pdu->s;
139860ce86c7SWei Liu     V9fsString version;
139960ce86c7SWei Liu     size_t offset = 7;
140060ce86c7SWei Liu 
140160ce86c7SWei Liu     v9fs_string_init(&version);
140260ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
140360ce86c7SWei Liu     if (err < 0) {
140460ce86c7SWei Liu         goto out;
140560ce86c7SWei Liu     }
140660ce86c7SWei Liu     trace_v9fs_version(pdu->tag, pdu->id, s->msize, version.data);
140760ce86c7SWei Liu 
140860ce86c7SWei Liu     virtfs_reset(pdu);
140960ce86c7SWei Liu 
141060ce86c7SWei Liu     if (!strcmp(version.data, "9P2000.u")) {
141160ce86c7SWei Liu         s->proto_version = V9FS_PROTO_2000U;
141260ce86c7SWei Liu     } else if (!strcmp(version.data, "9P2000.L")) {
141360ce86c7SWei Liu         s->proto_version = V9FS_PROTO_2000L;
141460ce86c7SWei Liu     } else {
141560ce86c7SWei Liu         v9fs_string_sprintf(&version, "unknown");
1416e16453a3SChristian Schoenebeck         /* skip min. msize check, reporting invalid version has priority */
1417e16453a3SChristian Schoenebeck         goto marshal;
141860ce86c7SWei Liu     }
141960ce86c7SWei Liu 
1420e16453a3SChristian Schoenebeck     if (s->msize < P9_MIN_MSIZE) {
1421e16453a3SChristian Schoenebeck         err = -EMSGSIZE;
1422e16453a3SChristian Schoenebeck         error_report(
1423e16453a3SChristian Schoenebeck             "9pfs: Client requested msize < minimum msize ("
1424e16453a3SChristian Schoenebeck             stringify(P9_MIN_MSIZE) ") supported by this server."
1425e16453a3SChristian Schoenebeck         );
1426e16453a3SChristian Schoenebeck         goto out;
1427e16453a3SChristian Schoenebeck     }
1428e16453a3SChristian Schoenebeck 
142962777d82SChristian Schoenebeck     /* 8192 is the default msize of Linux clients */
1430c418f935SChristian Schoenebeck     if (s->msize <= 8192 && !(s->ctx.export_flags & V9FS_NO_PERF_WARN)) {
143162777d82SChristian Schoenebeck         warn_report_once(
143262777d82SChristian Schoenebeck             "9p: degraded performance: a reasonable high msize should be "
143362777d82SChristian Schoenebeck             "chosen on client/guest side (chosen msize is <= 8192). See "
143462777d82SChristian Schoenebeck             "https://wiki.qemu.org/Documentation/9psetup#msize for details."
143562777d82SChristian Schoenebeck         );
143662777d82SChristian Schoenebeck     }
143762777d82SChristian Schoenebeck 
1438e16453a3SChristian Schoenebeck marshal:
143960ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "ds", s->msize, &version);
144060ce86c7SWei Liu     if (err < 0) {
144160ce86c7SWei Liu         goto out;
144260ce86c7SWei Liu     }
1443403a905bSPhilippe Mathieu-Daudé     err += offset;
144460ce86c7SWei Liu     trace_v9fs_version_return(pdu->tag, pdu->id, s->msize, version.data);
144560ce86c7SWei Liu out:
1446403a905bSPhilippe Mathieu-Daudé     pdu_complete(pdu, err);
144760ce86c7SWei Liu     v9fs_string_free(&version);
144860ce86c7SWei Liu }
144960ce86c7SWei Liu 
v9fs_attach(void * opaque)14508440e22eSGreg Kurz static void coroutine_fn v9fs_attach(void *opaque)
145160ce86c7SWei Liu {
145260ce86c7SWei Liu     V9fsPDU *pdu = opaque;
145360ce86c7SWei Liu     V9fsState *s = pdu->s;
145460ce86c7SWei Liu     int32_t fid, afid, n_uname;
145560ce86c7SWei Liu     V9fsString uname, aname;
145660ce86c7SWei Liu     V9fsFidState *fidp;
145760ce86c7SWei Liu     size_t offset = 7;
145860ce86c7SWei Liu     V9fsQID qid;
145960ce86c7SWei Liu     ssize_t err;
146011024375SChristian Schoenebeck     struct stat stbuf;
146160ce86c7SWei Liu 
146260ce86c7SWei Liu     v9fs_string_init(&uname);
146360ce86c7SWei Liu     v9fs_string_init(&aname);
146460ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "ddssd", &fid,
146560ce86c7SWei Liu                         &afid, &uname, &aname, &n_uname);
146660ce86c7SWei Liu     if (err < 0) {
146760ce86c7SWei Liu         goto out_nofid;
146860ce86c7SWei Liu     }
146960ce86c7SWei Liu     trace_v9fs_attach(pdu->tag, pdu->id, fid, afid, uname.data, aname.data);
147060ce86c7SWei Liu 
147160ce86c7SWei Liu     fidp = alloc_fid(s, fid);
147260ce86c7SWei Liu     if (fidp == NULL) {
147360ce86c7SWei Liu         err = -EINVAL;
147460ce86c7SWei Liu         goto out_nofid;
147560ce86c7SWei Liu     }
147660ce86c7SWei Liu     fidp->uid = n_uname;
147760ce86c7SWei Liu     err = v9fs_co_name_to_path(pdu, NULL, "/", &fidp->path);
147860ce86c7SWei Liu     if (err < 0) {
147960ce86c7SWei Liu         err = -EINVAL;
148060ce86c7SWei Liu         clunk_fid(s, fid);
148160ce86c7SWei Liu         goto out;
148260ce86c7SWei Liu     }
148311024375SChristian Schoenebeck     err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
148411024375SChristian Schoenebeck     if (err < 0) {
148511024375SChristian Schoenebeck         err = -EINVAL;
148611024375SChristian Schoenebeck         clunk_fid(s, fid);
148711024375SChristian Schoenebeck         goto out;
148811024375SChristian Schoenebeck     }
148911024375SChristian Schoenebeck     err = stat_to_qid(pdu, &stbuf, &qid);
149060ce86c7SWei Liu     if (err < 0) {
149160ce86c7SWei Liu         err = -EINVAL;
149260ce86c7SWei Liu         clunk_fid(s, fid);
149360ce86c7SWei Liu         goto out;
149460ce86c7SWei Liu     }
1495fe44dc91SAshijeet Acharya 
1496fe44dc91SAshijeet Acharya     /*
1497fe44dc91SAshijeet Acharya      * disable migration if we haven't done already.
1498fe44dc91SAshijeet Acharya      * attach could get called multiple times for the same export.
1499fe44dc91SAshijeet Acharya      */
1500fe44dc91SAshijeet Acharya     if (!s->migration_blocker) {
1501fe44dc91SAshijeet Acharya         error_setg(&s->migration_blocker,
1502fe44dc91SAshijeet Acharya                    "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1503fe44dc91SAshijeet Acharya                    s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag);
1504*c8a7fc51SSteve Sistare         err = migrate_add_blocker(&s->migration_blocker, NULL);
15059261ef5eSMarkus Armbruster         if (err < 0) {
1506fe44dc91SAshijeet Acharya             clunk_fid(s, fid);
1507fe44dc91SAshijeet Acharya             goto out;
1508fe44dc91SAshijeet Acharya         }
1509fe44dc91SAshijeet Acharya         s->root_fid = fid;
1510fe44dc91SAshijeet Acharya     }
1511fe44dc91SAshijeet Acharya 
151260ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "Q", &qid);
151360ce86c7SWei Liu     if (err < 0) {
151460ce86c7SWei Liu         clunk_fid(s, fid);
151560ce86c7SWei Liu         goto out;
151660ce86c7SWei Liu     }
151760ce86c7SWei Liu     err += offset;
1518fe44dc91SAshijeet Acharya 
151911024375SChristian Schoenebeck     memcpy(&s->root_st, &stbuf, sizeof(stbuf));
152060ce86c7SWei Liu     trace_v9fs_attach_return(pdu->tag, pdu->id,
152160ce86c7SWei Liu                              qid.type, qid.version, qid.path);
152260ce86c7SWei Liu out:
152360ce86c7SWei Liu     put_fid(pdu, fidp);
152460ce86c7SWei Liu out_nofid:
152560ce86c7SWei Liu     pdu_complete(pdu, err);
152660ce86c7SWei Liu     v9fs_string_free(&uname);
152760ce86c7SWei Liu     v9fs_string_free(&aname);
152860ce86c7SWei Liu }
152960ce86c7SWei Liu 
v9fs_stat(void * opaque)15308440e22eSGreg Kurz static void coroutine_fn v9fs_stat(void *opaque)
153160ce86c7SWei Liu {
153260ce86c7SWei Liu     int32_t fid;
153360ce86c7SWei Liu     V9fsStat v9stat;
153460ce86c7SWei Liu     ssize_t err = 0;
153560ce86c7SWei Liu     size_t offset = 7;
153660ce86c7SWei Liu     struct stat stbuf;
153760ce86c7SWei Liu     V9fsFidState *fidp;
153860ce86c7SWei Liu     V9fsPDU *pdu = opaque;
15396069537fSJan Dakinevich     char *basename;
154060ce86c7SWei Liu 
154160ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "d", &fid);
154260ce86c7SWei Liu     if (err < 0) {
154360ce86c7SWei Liu         goto out_nofid;
154460ce86c7SWei Liu     }
154560ce86c7SWei Liu     trace_v9fs_stat(pdu->tag, pdu->id, fid);
154660ce86c7SWei Liu 
154760ce86c7SWei Liu     fidp = get_fid(pdu, fid);
154860ce86c7SWei Liu     if (fidp == NULL) {
154960ce86c7SWei Liu         err = -ENOENT;
155060ce86c7SWei Liu         goto out_nofid;
155160ce86c7SWei Liu     }
155260ce86c7SWei Liu     err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
155360ce86c7SWei Liu     if (err < 0) {
155460ce86c7SWei Liu         goto out;
155560ce86c7SWei Liu     }
15566069537fSJan Dakinevich     basename = g_path_get_basename(fidp->path.data);
15576069537fSJan Dakinevich     err = stat_to_v9stat(pdu, &fidp->path, basename, &stbuf, &v9stat);
15586069537fSJan Dakinevich     g_free(basename);
155960ce86c7SWei Liu     if (err < 0) {
156060ce86c7SWei Liu         goto out;
156160ce86c7SWei Liu     }
156260ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "wS", 0, &v9stat);
156360ce86c7SWei Liu     if (err < 0) {
156460ce86c7SWei Liu         v9fs_stat_free(&v9stat);
156560ce86c7SWei Liu         goto out;
156660ce86c7SWei Liu     }
156760ce86c7SWei Liu     trace_v9fs_stat_return(pdu->tag, pdu->id, v9stat.mode,
156860ce86c7SWei Liu                            v9stat.atime, v9stat.mtime, v9stat.length);
156960ce86c7SWei Liu     err += offset;
157060ce86c7SWei Liu     v9fs_stat_free(&v9stat);
157160ce86c7SWei Liu out:
157260ce86c7SWei Liu     put_fid(pdu, fidp);
157360ce86c7SWei Liu out_nofid:
157460ce86c7SWei Liu     pdu_complete(pdu, err);
157560ce86c7SWei Liu }
157660ce86c7SWei Liu 
v9fs_getattr(void * opaque)15778440e22eSGreg Kurz static void coroutine_fn v9fs_getattr(void *opaque)
157860ce86c7SWei Liu {
157960ce86c7SWei Liu     int32_t fid;
158060ce86c7SWei Liu     size_t offset = 7;
158160ce86c7SWei Liu     ssize_t retval = 0;
158260ce86c7SWei Liu     struct stat stbuf;
158360ce86c7SWei Liu     V9fsFidState *fidp;
158460ce86c7SWei Liu     uint64_t request_mask;
158560ce86c7SWei Liu     V9fsStatDotl v9stat_dotl;
158660ce86c7SWei Liu     V9fsPDU *pdu = opaque;
158760ce86c7SWei Liu 
158860ce86c7SWei Liu     retval = pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask);
158960ce86c7SWei Liu     if (retval < 0) {
159060ce86c7SWei Liu         goto out_nofid;
159160ce86c7SWei Liu     }
159260ce86c7SWei Liu     trace_v9fs_getattr(pdu->tag, pdu->id, fid, request_mask);
159360ce86c7SWei Liu 
159460ce86c7SWei Liu     fidp = get_fid(pdu, fid);
159560ce86c7SWei Liu     if (fidp == NULL) {
159660ce86c7SWei Liu         retval = -ENOENT;
159760ce86c7SWei Liu         goto out_nofid;
159860ce86c7SWei Liu     }
159960ce86c7SWei Liu     /*
160060ce86c7SWei Liu      * Currently we only support BASIC fields in stat, so there is no
160160ce86c7SWei Liu      * need to look at request_mask.
160260ce86c7SWei Liu      */
160360ce86c7SWei Liu     retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
160460ce86c7SWei Liu     if (retval < 0) {
160560ce86c7SWei Liu         goto out;
160660ce86c7SWei Liu     }
16073b5ee9e8SAntonios Motakis     retval = stat_to_v9stat_dotl(pdu, &stbuf, &v9stat_dotl);
16083b5ee9e8SAntonios Motakis     if (retval < 0) {
16093b5ee9e8SAntonios Motakis         goto out;
16103b5ee9e8SAntonios Motakis     }
161160ce86c7SWei Liu 
161260ce86c7SWei Liu     /*  fill st_gen if requested and supported by underlying fs */
161360ce86c7SWei Liu     if (request_mask & P9_STATS_GEN) {
161460ce86c7SWei Liu         retval = v9fs_co_st_gen(pdu, &fidp->path, stbuf.st_mode, &v9stat_dotl);
161560ce86c7SWei Liu         switch (retval) {
161660ce86c7SWei Liu         case 0:
161760ce86c7SWei Liu             /* we have valid st_gen: update result mask */
161860ce86c7SWei Liu             v9stat_dotl.st_result_mask |= P9_STATS_GEN;
161960ce86c7SWei Liu             break;
162060ce86c7SWei Liu         case -EINTR:
162160ce86c7SWei Liu             /* request cancelled, e.g. by Tflush */
162260ce86c7SWei Liu             goto out;
162360ce86c7SWei Liu         default:
162460ce86c7SWei Liu             /* failed to get st_gen: not fatal, ignore */
162560ce86c7SWei Liu             break;
162660ce86c7SWei Liu         }
162760ce86c7SWei Liu     }
162860ce86c7SWei Liu     retval = pdu_marshal(pdu, offset, "A", &v9stat_dotl);
162960ce86c7SWei Liu     if (retval < 0) {
163060ce86c7SWei Liu         goto out;
163160ce86c7SWei Liu     }
163260ce86c7SWei Liu     retval += offset;
163360ce86c7SWei Liu     trace_v9fs_getattr_return(pdu->tag, pdu->id, v9stat_dotl.st_result_mask,
163460ce86c7SWei Liu                               v9stat_dotl.st_mode, v9stat_dotl.st_uid,
163560ce86c7SWei Liu                               v9stat_dotl.st_gid);
163660ce86c7SWei Liu out:
163760ce86c7SWei Liu     put_fid(pdu, fidp);
163860ce86c7SWei Liu out_nofid:
163960ce86c7SWei Liu     pdu_complete(pdu, retval);
164060ce86c7SWei Liu }
164160ce86c7SWei Liu 
164260ce86c7SWei Liu /* Attribute flags */
164360ce86c7SWei Liu #define P9_ATTR_MODE       (1 << 0)
164460ce86c7SWei Liu #define P9_ATTR_UID        (1 << 1)
164560ce86c7SWei Liu #define P9_ATTR_GID        (1 << 2)
164660ce86c7SWei Liu #define P9_ATTR_SIZE       (1 << 3)
164760ce86c7SWei Liu #define P9_ATTR_ATIME      (1 << 4)
164860ce86c7SWei Liu #define P9_ATTR_MTIME      (1 << 5)
164960ce86c7SWei Liu #define P9_ATTR_CTIME      (1 << 6)
165060ce86c7SWei Liu #define P9_ATTR_ATIME_SET  (1 << 7)
165160ce86c7SWei Liu #define P9_ATTR_MTIME_SET  (1 << 8)
165260ce86c7SWei Liu 
165360ce86c7SWei Liu #define P9_ATTR_MASK    127
165460ce86c7SWei Liu 
v9fs_setattr(void * opaque)16558440e22eSGreg Kurz static void coroutine_fn v9fs_setattr(void *opaque)
165660ce86c7SWei Liu {
165760ce86c7SWei Liu     int err = 0;
165860ce86c7SWei Liu     int32_t fid;
165960ce86c7SWei Liu     V9fsFidState *fidp;
166060ce86c7SWei Liu     size_t offset = 7;
166160ce86c7SWei Liu     V9fsIattr v9iattr;
166260ce86c7SWei Liu     V9fsPDU *pdu = opaque;
166360ce86c7SWei Liu 
166460ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr);
166560ce86c7SWei Liu     if (err < 0) {
166660ce86c7SWei Liu         goto out_nofid;
166760ce86c7SWei Liu     }
166860ce86c7SWei Liu 
16698f9c64bfSGreg Kurz     trace_v9fs_setattr(pdu->tag, pdu->id, fid,
16708f9c64bfSGreg Kurz                        v9iattr.valid, v9iattr.mode, v9iattr.uid, v9iattr.gid,
16718f9c64bfSGreg Kurz                        v9iattr.size, v9iattr.atime_sec, v9iattr.mtime_sec);
16728f9c64bfSGreg Kurz 
167360ce86c7SWei Liu     fidp = get_fid(pdu, fid);
167460ce86c7SWei Liu     if (fidp == NULL) {
167560ce86c7SWei Liu         err = -EINVAL;
167660ce86c7SWei Liu         goto out_nofid;
167760ce86c7SWei Liu     }
167860ce86c7SWei Liu     if (v9iattr.valid & P9_ATTR_MODE) {
167960ce86c7SWei Liu         err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode);
168060ce86c7SWei Liu         if (err < 0) {
168160ce86c7SWei Liu             goto out;
168260ce86c7SWei Liu         }
168360ce86c7SWei Liu     }
168460ce86c7SWei Liu     if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) {
168560ce86c7SWei Liu         struct timespec times[2];
168660ce86c7SWei Liu         if (v9iattr.valid & P9_ATTR_ATIME) {
168760ce86c7SWei Liu             if (v9iattr.valid & P9_ATTR_ATIME_SET) {
168860ce86c7SWei Liu                 times[0].tv_sec = v9iattr.atime_sec;
168960ce86c7SWei Liu                 times[0].tv_nsec = v9iattr.atime_nsec;
169060ce86c7SWei Liu             } else {
169160ce86c7SWei Liu                 times[0].tv_nsec = UTIME_NOW;
169260ce86c7SWei Liu             }
169360ce86c7SWei Liu         } else {
169460ce86c7SWei Liu             times[0].tv_nsec = UTIME_OMIT;
169560ce86c7SWei Liu         }
169660ce86c7SWei Liu         if (v9iattr.valid & P9_ATTR_MTIME) {
169760ce86c7SWei Liu             if (v9iattr.valid & P9_ATTR_MTIME_SET) {
169860ce86c7SWei Liu                 times[1].tv_sec = v9iattr.mtime_sec;
169960ce86c7SWei Liu                 times[1].tv_nsec = v9iattr.mtime_nsec;
170060ce86c7SWei Liu             } else {
170160ce86c7SWei Liu                 times[1].tv_nsec = UTIME_NOW;
170260ce86c7SWei Liu             }
170360ce86c7SWei Liu         } else {
170460ce86c7SWei Liu             times[1].tv_nsec = UTIME_OMIT;
170560ce86c7SWei Liu         }
170660ce86c7SWei Liu         err = v9fs_co_utimensat(pdu, &fidp->path, times);
170760ce86c7SWei Liu         if (err < 0) {
170860ce86c7SWei Liu             goto out;
170960ce86c7SWei Liu         }
171060ce86c7SWei Liu     }
171160ce86c7SWei Liu     /*
171260ce86c7SWei Liu      * If the only valid entry in iattr is ctime we can call
171360ce86c7SWei Liu      * chown(-1,-1) to update the ctime of the file
171460ce86c7SWei Liu      */
171560ce86c7SWei Liu     if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) ||
171660ce86c7SWei Liu         ((v9iattr.valid & P9_ATTR_CTIME)
171760ce86c7SWei Liu          && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) {
171860ce86c7SWei Liu         if (!(v9iattr.valid & P9_ATTR_UID)) {
171960ce86c7SWei Liu             v9iattr.uid = -1;
172060ce86c7SWei Liu         }
172160ce86c7SWei Liu         if (!(v9iattr.valid & P9_ATTR_GID)) {
172260ce86c7SWei Liu             v9iattr.gid = -1;
172360ce86c7SWei Liu         }
172460ce86c7SWei Liu         err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid,
172560ce86c7SWei Liu                             v9iattr.gid);
172660ce86c7SWei Liu         if (err < 0) {
172760ce86c7SWei Liu             goto out;
172860ce86c7SWei Liu         }
172960ce86c7SWei Liu     }
173060ce86c7SWei Liu     if (v9iattr.valid & (P9_ATTR_SIZE)) {
173160ce86c7SWei Liu         err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size);
173260ce86c7SWei Liu         if (err < 0) {
173360ce86c7SWei Liu             goto out;
173460ce86c7SWei Liu         }
173560ce86c7SWei Liu     }
173660ce86c7SWei Liu     err = offset;
17378f9c64bfSGreg Kurz     trace_v9fs_setattr_return(pdu->tag, pdu->id);
173860ce86c7SWei Liu out:
173960ce86c7SWei Liu     put_fid(pdu, fidp);
174060ce86c7SWei Liu out_nofid:
174160ce86c7SWei Liu     pdu_complete(pdu, err);
174260ce86c7SWei Liu }
174360ce86c7SWei Liu 
v9fs_walk_marshal(V9fsPDU * pdu,uint16_t nwnames,V9fsQID * qids)174460ce86c7SWei Liu static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids)
174560ce86c7SWei Liu {
174660ce86c7SWei Liu     int i;
174760ce86c7SWei Liu     ssize_t err;
174860ce86c7SWei Liu     size_t offset = 7;
174960ce86c7SWei Liu 
175060ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "w", nwnames);
175160ce86c7SWei Liu     if (err < 0) {
175260ce86c7SWei Liu         return err;
175360ce86c7SWei Liu     }
175460ce86c7SWei Liu     offset += err;
175560ce86c7SWei Liu     for (i = 0; i < nwnames; i++) {
175660ce86c7SWei Liu         err = pdu_marshal(pdu, offset, "Q", &qids[i]);
175760ce86c7SWei Liu         if (err < 0) {
175860ce86c7SWei Liu             return err;
175960ce86c7SWei Liu         }
176060ce86c7SWei Liu         offset += err;
176160ce86c7SWei Liu     }
176260ce86c7SWei Liu     return offset;
176360ce86c7SWei Liu }
176460ce86c7SWei Liu 
name_is_illegal(const char * name)1765fff39a7aSGreg Kurz static bool name_is_illegal(const char *name)
1766fff39a7aSGreg Kurz {
1767fff39a7aSGreg Kurz     return !*name || strchr(name, '/') != NULL;
1768fff39a7aSGreg Kurz }
1769fff39a7aSGreg Kurz 
same_stat_id(const struct stat * a,const struct stat * b)1770f22cad42SChristian Schoenebeck static bool same_stat_id(const struct stat *a, const struct stat *b)
177156f101ecSGreg Kurz {
1772f22cad42SChristian Schoenebeck     return a->st_dev == b->st_dev && a->st_ino == b->st_ino;
177356f101ecSGreg Kurz }
177456f101ecSGreg Kurz 
v9fs_walk(void * opaque)17758440e22eSGreg Kurz static void coroutine_fn v9fs_walk(void *opaque)
177660ce86c7SWei Liu {
1777fd6c979eSChristian Schoenebeck     int name_idx, nwalked;
1778869605b5SChristian Schoenebeck     g_autofree V9fsQID *qids = NULL;
1779a93d2e89SChristian Schoenebeck     int i, err = 0, any_err = 0;
17807e985780SChristian Schoenebeck     V9fsPath dpath, path;
17817e985780SChristian Schoenebeck     P9ARRAY_REF(V9fsPath) pathes = NULL;
178260ce86c7SWei Liu     uint16_t nwnames;
1783869605b5SChristian Schoenebeck     struct stat stbuf, fidst;
1784869605b5SChristian Schoenebeck     g_autofree struct stat *stbufs = NULL;
178560ce86c7SWei Liu     size_t offset = 7;
178660ce86c7SWei Liu     int32_t fid, newfid;
17877e985780SChristian Schoenebeck     P9ARRAY_REF(V9fsString) wnames = NULL;
178860ce86c7SWei Liu     V9fsFidState *fidp;
178960ce86c7SWei Liu     V9fsFidState *newfidp = NULL;
179060ce86c7SWei Liu     V9fsPDU *pdu = opaque;
179160ce86c7SWei Liu     V9fsState *s = pdu->s;
179256f101ecSGreg Kurz     V9fsQID qid;
179360ce86c7SWei Liu 
179460ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames);
179560ce86c7SWei Liu     if (err < 0) {
179660ce86c7SWei Liu         pdu_complete(pdu, err);
179760ce86c7SWei Liu         return;
179860ce86c7SWei Liu     }
179960ce86c7SWei Liu     offset += err;
180060ce86c7SWei Liu 
180160ce86c7SWei Liu     trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames);
180260ce86c7SWei Liu 
1803232a4d2cSChristian Schoenebeck     if (nwnames > P9_MAXWELEM) {
1804232a4d2cSChristian Schoenebeck         err = -EINVAL;
1805232a4d2cSChristian Schoenebeck         goto out_nofid;
1806232a4d2cSChristian Schoenebeck     }
1807232a4d2cSChristian Schoenebeck     if (nwnames) {
18087e985780SChristian Schoenebeck         P9ARRAY_NEW(V9fsString, wnames, nwnames);
18091923923bSGreg Kurz         qids   = g_new0(V9fsQID, nwnames);
18108d6cb100SChristian Schoenebeck         stbufs = g_new0(struct stat, nwnames);
18117e985780SChristian Schoenebeck         P9ARRAY_NEW(V9fsPath, pathes, nwnames);
181260ce86c7SWei Liu         for (i = 0; i < nwnames; i++) {
181360ce86c7SWei Liu             err = pdu_unmarshal(pdu, offset, "s", &wnames[i]);
181460ce86c7SWei Liu             if (err < 0) {
181560ce86c7SWei Liu                 goto out_nofid;
181660ce86c7SWei Liu             }
1817fff39a7aSGreg Kurz             if (name_is_illegal(wnames[i].data)) {
1818fff39a7aSGreg Kurz                 err = -ENOENT;
1819fff39a7aSGreg Kurz                 goto out_nofid;
1820fff39a7aSGreg Kurz             }
182160ce86c7SWei Liu             offset += err;
182260ce86c7SWei Liu         }
182360ce86c7SWei Liu     }
182460ce86c7SWei Liu     fidp = get_fid(pdu, fid);
182560ce86c7SWei Liu     if (fidp == NULL) {
182660ce86c7SWei Liu         err = -ENOENT;
182760ce86c7SWei Liu         goto out_nofid;
182860ce86c7SWei Liu     }
182956f101ecSGreg Kurz 
183013fd08e6SGreg Kurz     v9fs_path_init(&dpath);
183113fd08e6SGreg Kurz     v9fs_path_init(&path);
183260ce86c7SWei Liu     /*
18338d6cb100SChristian Schoenebeck      * Both dpath and path initially point to fidp.
183460ce86c7SWei Liu      * Needed to handle request with nwnames == 0
183560ce86c7SWei Liu      */
183660ce86c7SWei Liu     v9fs_path_copy(&dpath, &fidp->path);
183760ce86c7SWei Liu     v9fs_path_copy(&path, &fidp->path);
18388d6cb100SChristian Schoenebeck 
18398d6cb100SChristian Schoenebeck     /*
18408d6cb100SChristian Schoenebeck      * To keep latency (i.e. overall execution time for processing this
18418d6cb100SChristian Schoenebeck      * Twalk client request) as small as possible, run all the required fs
18428d6cb100SChristian Schoenebeck      * driver code altogether inside the following block.
18438d6cb100SChristian Schoenebeck      */
18448d6cb100SChristian Schoenebeck     v9fs_co_run_in_worker({
1845a93d2e89SChristian Schoenebeck         nwalked = 0;
18468d6cb100SChristian Schoenebeck         if (v9fs_request_cancelled(pdu)) {
1847a93d2e89SChristian Schoenebeck             any_err |= err = -EINTR;
18488d6cb100SChristian Schoenebeck             break;
18498d6cb100SChristian Schoenebeck         }
18508d6cb100SChristian Schoenebeck         err = s->ops->lstat(&s->ctx, &dpath, &fidst);
18518d6cb100SChristian Schoenebeck         if (err < 0) {
1852a93d2e89SChristian Schoenebeck             any_err |= err = -errno;
18538d6cb100SChristian Schoenebeck             break;
18548d6cb100SChristian Schoenebeck         }
18558d6cb100SChristian Schoenebeck         stbuf = fidst;
1856a93d2e89SChristian Schoenebeck         for (; nwalked < nwnames; nwalked++) {
18578d6cb100SChristian Schoenebeck             if (v9fs_request_cancelled(pdu)) {
1858a93d2e89SChristian Schoenebeck                 any_err |= err = -EINTR;
18598d6cb100SChristian Schoenebeck                 break;
18608d6cb100SChristian Schoenebeck             }
1861f22cad42SChristian Schoenebeck             if (!same_stat_id(&pdu->s->root_st, &stbuf) ||
1862fd6c979eSChristian Schoenebeck                 strcmp("..", wnames[nwalked].data))
18638d6cb100SChristian Schoenebeck             {
18648d6cb100SChristian Schoenebeck                 err = s->ops->name_to_path(&s->ctx, &dpath,
1865fd6c979eSChristian Schoenebeck                                            wnames[nwalked].data,
1866fd6c979eSChristian Schoenebeck                                            &pathes[nwalked]);
18678d6cb100SChristian Schoenebeck                 if (err < 0) {
1868a93d2e89SChristian Schoenebeck                     any_err |= err = -errno;
18698d6cb100SChristian Schoenebeck                     break;
18708d6cb100SChristian Schoenebeck                 }
18718d6cb100SChristian Schoenebeck                 if (v9fs_request_cancelled(pdu)) {
1872a93d2e89SChristian Schoenebeck                     any_err |= err = -EINTR;
18738d6cb100SChristian Schoenebeck                     break;
18748d6cb100SChristian Schoenebeck                 }
1875fd6c979eSChristian Schoenebeck                 err = s->ops->lstat(&s->ctx, &pathes[nwalked], &stbuf);
18768d6cb100SChristian Schoenebeck                 if (err < 0) {
1877a93d2e89SChristian Schoenebeck                     any_err |= err = -errno;
18788d6cb100SChristian Schoenebeck                     break;
18798d6cb100SChristian Schoenebeck                 }
1880fd6c979eSChristian Schoenebeck                 stbufs[nwalked] = stbuf;
1881fd6c979eSChristian Schoenebeck                 v9fs_path_copy(&dpath, &pathes[nwalked]);
18828d6cb100SChristian Schoenebeck             }
18838d6cb100SChristian Schoenebeck         }
18848d6cb100SChristian Schoenebeck     });
18858d6cb100SChristian Schoenebeck     /*
18868d6cb100SChristian Schoenebeck      * Handle all the rest of this Twalk request on main thread ...
1887a93d2e89SChristian Schoenebeck      *
1888a93d2e89SChristian Schoenebeck      * NOTE: -EINTR is an exception where we deviate from the protocol spec
1889a93d2e89SChristian Schoenebeck      * and simply send a (R)Lerror response instead of bothering to assemble
1890a93d2e89SChristian Schoenebeck      * a (deducted) Rwalk response; because -EINTR is always the result of a
1891a93d2e89SChristian Schoenebeck      * Tflush request, so client would no longer wait for a response in this
1892a93d2e89SChristian Schoenebeck      * case anyway.
18938d6cb100SChristian Schoenebeck      */
1894a93d2e89SChristian Schoenebeck     if ((err < 0 && !nwalked) || err == -EINTR) {
189560ce86c7SWei Liu         goto out;
189660ce86c7SWei Liu     }
189756f101ecSGreg Kurz 
1898a93d2e89SChristian Schoenebeck     any_err |= err = stat_to_qid(pdu, &fidst, &qid);
1899a93d2e89SChristian Schoenebeck     if (err < 0 && !nwalked) {
190060ce86c7SWei Liu         goto out;
190160ce86c7SWei Liu     }
19028d6cb100SChristian Schoenebeck     stbuf = fidst;
19038d6cb100SChristian Schoenebeck 
19048d6cb100SChristian Schoenebeck     /* reset dpath and path */
19058d6cb100SChristian Schoenebeck     v9fs_path_copy(&dpath, &fidp->path);
19068d6cb100SChristian Schoenebeck     v9fs_path_copy(&path, &fidp->path);
19078d6cb100SChristian Schoenebeck 
1908a93d2e89SChristian Schoenebeck     for (name_idx = 0; name_idx < nwalked; name_idx++) {
19098d6cb100SChristian Schoenebeck         if (!same_stat_id(&pdu->s->root_st, &stbuf) ||
19108d6cb100SChristian Schoenebeck             strcmp("..", wnames[name_idx].data))
19118d6cb100SChristian Schoenebeck         {
19128d6cb100SChristian Schoenebeck             stbuf = stbufs[name_idx];
1913a93d2e89SChristian Schoenebeck             any_err |= err = stat_to_qid(pdu, &stbuf, &qid);
19143b5ee9e8SAntonios Motakis             if (err < 0) {
1915a93d2e89SChristian Schoenebeck                 break;
19163b5ee9e8SAntonios Motakis             }
19178d6cb100SChristian Schoenebeck             v9fs_path_copy(&path, &pathes[name_idx]);
191860ce86c7SWei Liu             v9fs_path_copy(&dpath, &path);
191960ce86c7SWei Liu         }
192056f101ecSGreg Kurz         memcpy(&qids[name_idx], &qid, sizeof(qid));
192156f101ecSGreg Kurz     }
1922a93d2e89SChristian Schoenebeck     if (any_err < 0) {
1923a93d2e89SChristian Schoenebeck         if (!name_idx) {
1924a93d2e89SChristian Schoenebeck             /* don't send any QIDs, send Rlerror instead */
1925a93d2e89SChristian Schoenebeck             goto out;
1926a93d2e89SChristian Schoenebeck         } else {
1927a93d2e89SChristian Schoenebeck             /* send QIDs (not Rlerror), but fid MUST remain unaffected */
1928a93d2e89SChristian Schoenebeck             goto send_qids;
1929a93d2e89SChristian Schoenebeck         }
1930a93d2e89SChristian Schoenebeck     }
193160ce86c7SWei Liu     if (fid == newfid) {
193249dd946bSGreg Kurz         if (fidp->fid_type != P9_FID_NONE) {
193349dd946bSGreg Kurz             err = -EINVAL;
193449dd946bSGreg Kurz             goto out;
193549dd946bSGreg Kurz         }
19365b3c77aaSGreg Kurz         v9fs_path_write_lock(s);
193760ce86c7SWei Liu         v9fs_path_copy(&fidp->path, &path);
19385b3c77aaSGreg Kurz         v9fs_path_unlock(s);
193960ce86c7SWei Liu     } else {
194060ce86c7SWei Liu         newfidp = alloc_fid(s, newfid);
194160ce86c7SWei Liu         if (newfidp == NULL) {
194260ce86c7SWei Liu             err = -EINVAL;
194360ce86c7SWei Liu             goto out;
194460ce86c7SWei Liu         }
194560ce86c7SWei Liu         newfidp->uid = fidp->uid;
194660ce86c7SWei Liu         v9fs_path_copy(&newfidp->path, &path);
194760ce86c7SWei Liu     }
1948a93d2e89SChristian Schoenebeck send_qids:
1949a93d2e89SChristian Schoenebeck     err = v9fs_walk_marshal(pdu, name_idx, qids);
1950a93d2e89SChristian Schoenebeck     trace_v9fs_walk_return(pdu->tag, pdu->id, name_idx, qids);
195160ce86c7SWei Liu out:
195260ce86c7SWei Liu     put_fid(pdu, fidp);
195360ce86c7SWei Liu     if (newfidp) {
195460ce86c7SWei Liu         put_fid(pdu, newfidp);
195560ce86c7SWei Liu     }
195660ce86c7SWei Liu     v9fs_path_free(&dpath);
195760ce86c7SWei Liu     v9fs_path_free(&path);
195860ce86c7SWei Liu out_nofid:
195960ce86c7SWei Liu     pdu_complete(pdu, err);
196060ce86c7SWei Liu }
196160ce86c7SWei Liu 
get_iounit(V9fsPDU * pdu,V9fsPath * path)19628440e22eSGreg Kurz static int32_t coroutine_fn get_iounit(V9fsPDU *pdu, V9fsPath *path)
196360ce86c7SWei Liu {
196460ce86c7SWei Liu     struct statfs stbuf;
1965b565bccbSChristian Schoenebeck     int err = v9fs_co_statfs(pdu, path, &stbuf);
196660ce86c7SWei Liu 
1967b565bccbSChristian Schoenebeck     return blksize_to_iounit(pdu, (err >= 0) ? stbuf.f_bsize : 0);
196860ce86c7SWei Liu }
196960ce86c7SWei Liu 
v9fs_open(void * opaque)19708440e22eSGreg Kurz static void coroutine_fn v9fs_open(void *opaque)
197160ce86c7SWei Liu {
197260ce86c7SWei Liu     int flags;
197360ce86c7SWei Liu     int32_t fid;
197460ce86c7SWei Liu     int32_t mode;
197560ce86c7SWei Liu     V9fsQID qid;
197660ce86c7SWei Liu     int iounit = 0;
197760ce86c7SWei Liu     ssize_t err = 0;
197860ce86c7SWei Liu     size_t offset = 7;
197960ce86c7SWei Liu     struct stat stbuf;
198060ce86c7SWei Liu     V9fsFidState *fidp;
198160ce86c7SWei Liu     V9fsPDU *pdu = opaque;
198260ce86c7SWei Liu     V9fsState *s = pdu->s;
198360ce86c7SWei Liu 
198460ce86c7SWei Liu     if (s->proto_version == V9FS_PROTO_2000L) {
198560ce86c7SWei Liu         err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode);
198660ce86c7SWei Liu     } else {
198760ce86c7SWei Liu         uint8_t modebyte;
198860ce86c7SWei Liu         err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte);
198960ce86c7SWei Liu         mode = modebyte;
199060ce86c7SWei Liu     }
199160ce86c7SWei Liu     if (err < 0) {
199260ce86c7SWei Liu         goto out_nofid;
199360ce86c7SWei Liu     }
199460ce86c7SWei Liu     trace_v9fs_open(pdu->tag, pdu->id, fid, mode);
199560ce86c7SWei Liu 
199660ce86c7SWei Liu     fidp = get_fid(pdu, fid);
199760ce86c7SWei Liu     if (fidp == NULL) {
199860ce86c7SWei Liu         err = -ENOENT;
199960ce86c7SWei Liu         goto out_nofid;
200060ce86c7SWei Liu     }
200149dd946bSGreg Kurz     if (fidp->fid_type != P9_FID_NONE) {
200249dd946bSGreg Kurz         err = -EINVAL;
200349dd946bSGreg Kurz         goto out;
200449dd946bSGreg Kurz     }
200560ce86c7SWei Liu 
200660ce86c7SWei Liu     err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
200760ce86c7SWei Liu     if (err < 0) {
200860ce86c7SWei Liu         goto out;
200960ce86c7SWei Liu     }
20103b5ee9e8SAntonios Motakis     err = stat_to_qid(pdu, &stbuf, &qid);
20113b5ee9e8SAntonios Motakis     if (err < 0) {
20123b5ee9e8SAntonios Motakis         goto out;
20133b5ee9e8SAntonios Motakis     }
201460ce86c7SWei Liu     if (S_ISDIR(stbuf.st_mode)) {
201560ce86c7SWei Liu         err = v9fs_co_opendir(pdu, fidp);
201660ce86c7SWei Liu         if (err < 0) {
201760ce86c7SWei Liu             goto out;
201860ce86c7SWei Liu         }
201960ce86c7SWei Liu         fidp->fid_type = P9_FID_DIR;
202060ce86c7SWei Liu         err = pdu_marshal(pdu, offset, "Qd", &qid, 0);
202160ce86c7SWei Liu         if (err < 0) {
202260ce86c7SWei Liu             goto out;
202360ce86c7SWei Liu         }
202460ce86c7SWei Liu         err += offset;
202560ce86c7SWei Liu     } else {
202660ce86c7SWei Liu         if (s->proto_version == V9FS_PROTO_2000L) {
202760ce86c7SWei Liu             flags = get_dotl_openflags(s, mode);
202860ce86c7SWei Liu         } else {
202960ce86c7SWei Liu             flags = omode_to_uflags(mode);
203060ce86c7SWei Liu         }
203160ce86c7SWei Liu         if (is_ro_export(&s->ctx)) {
203260ce86c7SWei Liu             if (mode & O_WRONLY || mode & O_RDWR ||
203360ce86c7SWei Liu                 mode & O_APPEND || mode & O_TRUNC) {
203460ce86c7SWei Liu                 err = -EROFS;
203560ce86c7SWei Liu                 goto out;
203660ce86c7SWei Liu             }
203760ce86c7SWei Liu         }
203860ce86c7SWei Liu         err = v9fs_co_open(pdu, fidp, flags);
203960ce86c7SWei Liu         if (err < 0) {
204060ce86c7SWei Liu             goto out;
204160ce86c7SWei Liu         }
204260ce86c7SWei Liu         fidp->fid_type = P9_FID_FILE;
204360ce86c7SWei Liu         fidp->open_flags = flags;
204460ce86c7SWei Liu         if (flags & O_EXCL) {
204560ce86c7SWei Liu             /*
204660ce86c7SWei Liu              * We let the host file system do O_EXCL check
204760ce86c7SWei Liu              * We should not reclaim such fd
204860ce86c7SWei Liu              */
204960ce86c7SWei Liu             fidp->flags |= FID_NON_RECLAIMABLE;
205060ce86c7SWei Liu         }
205160ce86c7SWei Liu         iounit = get_iounit(pdu, &fidp->path);
205260ce86c7SWei Liu         err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
205360ce86c7SWei Liu         if (err < 0) {
205460ce86c7SWei Liu             goto out;
205560ce86c7SWei Liu         }
205660ce86c7SWei Liu         err += offset;
205760ce86c7SWei Liu     }
205860ce86c7SWei Liu     trace_v9fs_open_return(pdu->tag, pdu->id,
205960ce86c7SWei Liu                            qid.type, qid.version, qid.path, iounit);
206060ce86c7SWei Liu out:
206160ce86c7SWei Liu     put_fid(pdu, fidp);
206260ce86c7SWei Liu out_nofid:
206360ce86c7SWei Liu     pdu_complete(pdu, err);
206460ce86c7SWei Liu }
206560ce86c7SWei Liu 
v9fs_lcreate(void * opaque)20668440e22eSGreg Kurz static void coroutine_fn v9fs_lcreate(void *opaque)
206760ce86c7SWei Liu {
206860ce86c7SWei Liu     int32_t dfid, flags, mode;
206960ce86c7SWei Liu     gid_t gid;
207060ce86c7SWei Liu     ssize_t err = 0;
207160ce86c7SWei Liu     ssize_t offset = 7;
207260ce86c7SWei Liu     V9fsString name;
207360ce86c7SWei Liu     V9fsFidState *fidp;
207460ce86c7SWei Liu     struct stat stbuf;
207560ce86c7SWei Liu     V9fsQID qid;
207660ce86c7SWei Liu     int32_t iounit;
207760ce86c7SWei Liu     V9fsPDU *pdu = opaque;
207860ce86c7SWei Liu 
207960ce86c7SWei Liu     v9fs_string_init(&name);
208060ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dsddd", &dfid,
208160ce86c7SWei Liu                         &name, &flags, &mode, &gid);
208260ce86c7SWei Liu     if (err < 0) {
208360ce86c7SWei Liu         goto out_nofid;
208460ce86c7SWei Liu     }
208560ce86c7SWei Liu     trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid);
208660ce86c7SWei Liu 
2087fff39a7aSGreg Kurz     if (name_is_illegal(name.data)) {
2088fff39a7aSGreg Kurz         err = -ENOENT;
2089fff39a7aSGreg Kurz         goto out_nofid;
2090fff39a7aSGreg Kurz     }
2091fff39a7aSGreg Kurz 
2092805b5d98SGreg Kurz     if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2093805b5d98SGreg Kurz         err = -EEXIST;
2094805b5d98SGreg Kurz         goto out_nofid;
2095805b5d98SGreg Kurz     }
2096805b5d98SGreg Kurz 
209760ce86c7SWei Liu     fidp = get_fid(pdu, dfid);
209860ce86c7SWei Liu     if (fidp == NULL) {
209960ce86c7SWei Liu         err = -ENOENT;
210060ce86c7SWei Liu         goto out_nofid;
210160ce86c7SWei Liu     }
2102d63fb193SLi Qiang     if (fidp->fid_type != P9_FID_NONE) {
2103d63fb193SLi Qiang         err = -EINVAL;
2104d63fb193SLi Qiang         goto out;
2105d63fb193SLi Qiang     }
210660ce86c7SWei Liu 
210760ce86c7SWei Liu     flags = get_dotl_openflags(pdu->s, flags);
210860ce86c7SWei Liu     err = v9fs_co_open2(pdu, fidp, &name, gid,
210960ce86c7SWei Liu                         flags | O_CREAT, mode, &stbuf);
211060ce86c7SWei Liu     if (err < 0) {
211160ce86c7SWei Liu         goto out;
211260ce86c7SWei Liu     }
211360ce86c7SWei Liu     fidp->fid_type = P9_FID_FILE;
211460ce86c7SWei Liu     fidp->open_flags = flags;
211560ce86c7SWei Liu     if (flags & O_EXCL) {
211660ce86c7SWei Liu         /*
211760ce86c7SWei Liu          * We let the host file system do O_EXCL check
211860ce86c7SWei Liu          * We should not reclaim such fd
211960ce86c7SWei Liu          */
212060ce86c7SWei Liu         fidp->flags |= FID_NON_RECLAIMABLE;
212160ce86c7SWei Liu     }
212260ce86c7SWei Liu     iounit =  get_iounit(pdu, &fidp->path);
21233b5ee9e8SAntonios Motakis     err = stat_to_qid(pdu, &stbuf, &qid);
21243b5ee9e8SAntonios Motakis     if (err < 0) {
21253b5ee9e8SAntonios Motakis         goto out;
21263b5ee9e8SAntonios Motakis     }
212760ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
212860ce86c7SWei Liu     if (err < 0) {
212960ce86c7SWei Liu         goto out;
213060ce86c7SWei Liu     }
213160ce86c7SWei Liu     err += offset;
213260ce86c7SWei Liu     trace_v9fs_lcreate_return(pdu->tag, pdu->id,
213360ce86c7SWei Liu                               qid.type, qid.version, qid.path, iounit);
213460ce86c7SWei Liu out:
213560ce86c7SWei Liu     put_fid(pdu, fidp);
213660ce86c7SWei Liu out_nofid:
213760ce86c7SWei Liu     pdu_complete(pdu, err);
213860ce86c7SWei Liu     v9fs_string_free(&name);
213960ce86c7SWei Liu }
214060ce86c7SWei Liu 
v9fs_fsync(void * opaque)2141a1bf8b74SGreg Kurz static void coroutine_fn v9fs_fsync(void *opaque)
214260ce86c7SWei Liu {
214360ce86c7SWei Liu     int err;
214460ce86c7SWei Liu     int32_t fid;
214560ce86c7SWei Liu     int datasync;
214660ce86c7SWei Liu     size_t offset = 7;
214760ce86c7SWei Liu     V9fsFidState *fidp;
214860ce86c7SWei Liu     V9fsPDU *pdu = opaque;
214960ce86c7SWei Liu 
215060ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
215160ce86c7SWei Liu     if (err < 0) {
215260ce86c7SWei Liu         goto out_nofid;
215360ce86c7SWei Liu     }
215460ce86c7SWei Liu     trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync);
215560ce86c7SWei Liu 
215660ce86c7SWei Liu     fidp = get_fid(pdu, fid);
215760ce86c7SWei Liu     if (fidp == NULL) {
215860ce86c7SWei Liu         err = -ENOENT;
215960ce86c7SWei Liu         goto out_nofid;
216060ce86c7SWei Liu     }
216160ce86c7SWei Liu     err = v9fs_co_fsync(pdu, fidp, datasync);
216260ce86c7SWei Liu     if (!err) {
216360ce86c7SWei Liu         err = offset;
216460ce86c7SWei Liu     }
216560ce86c7SWei Liu     put_fid(pdu, fidp);
216660ce86c7SWei Liu out_nofid:
216760ce86c7SWei Liu     pdu_complete(pdu, err);
216860ce86c7SWei Liu }
216960ce86c7SWei Liu 
v9fs_clunk(void * opaque)21708440e22eSGreg Kurz static void coroutine_fn v9fs_clunk(void *opaque)
217160ce86c7SWei Liu {
217260ce86c7SWei Liu     int err;
217360ce86c7SWei Liu     int32_t fid;
217460ce86c7SWei Liu     size_t offset = 7;
217560ce86c7SWei Liu     V9fsFidState *fidp;
217660ce86c7SWei Liu     V9fsPDU *pdu = opaque;
217760ce86c7SWei Liu     V9fsState *s = pdu->s;
217860ce86c7SWei Liu 
217960ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "d", &fid);
218060ce86c7SWei Liu     if (err < 0) {
218160ce86c7SWei Liu         goto out_nofid;
218260ce86c7SWei Liu     }
218360ce86c7SWei Liu     trace_v9fs_clunk(pdu->tag, pdu->id, fid);
218460ce86c7SWei Liu 
218560ce86c7SWei Liu     fidp = clunk_fid(s, fid);
218660ce86c7SWei Liu     if (fidp == NULL) {
218760ce86c7SWei Liu         err = -ENOENT;
218860ce86c7SWei Liu         goto out_nofid;
218960ce86c7SWei Liu     }
219060ce86c7SWei Liu     /*
219160ce86c7SWei Liu      * Bump the ref so that put_fid will
219260ce86c7SWei Liu      * free the fid.
219360ce86c7SWei Liu      */
219460ce86c7SWei Liu     fidp->ref++;
219560ce86c7SWei Liu     err = put_fid(pdu, fidp);
219660ce86c7SWei Liu     if (!err) {
219760ce86c7SWei Liu         err = offset;
219860ce86c7SWei Liu     }
219960ce86c7SWei Liu out_nofid:
220060ce86c7SWei Liu     pdu_complete(pdu, err);
220160ce86c7SWei Liu }
220260ce86c7SWei Liu 
2203bcb8998fSStefano Stabellini /*
2204bcb8998fSStefano Stabellini  * Create a QEMUIOVector for a sub-region of PDU iovecs
2205bcb8998fSStefano Stabellini  *
2206bcb8998fSStefano Stabellini  * @qiov:       uninitialized QEMUIOVector
2207bcb8998fSStefano Stabellini  * @skip:       number of bytes to skip from beginning of PDU
2208bcb8998fSStefano Stabellini  * @size:       number of bytes to include
2209bcb8998fSStefano Stabellini  * @is_write:   true - write, false - read
2210bcb8998fSStefano Stabellini  *
2211bcb8998fSStefano Stabellini  * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
2212bcb8998fSStefano Stabellini  * with qemu_iovec_destroy().
2213bcb8998fSStefano Stabellini  */
v9fs_init_qiov_from_pdu(QEMUIOVector * qiov,V9fsPDU * pdu,size_t skip,size_t size,bool is_write)2214bcb8998fSStefano Stabellini static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu,
2215cf45183bSStefano Stabellini                                     size_t skip, size_t size,
2216bcb8998fSStefano Stabellini                                     bool is_write)
2217bcb8998fSStefano Stabellini {
2218bcb8998fSStefano Stabellini     QEMUIOVector elem;
2219bcb8998fSStefano Stabellini     struct iovec *iov;
2220bcb8998fSStefano Stabellini     unsigned int niov;
2221bcb8998fSStefano Stabellini 
222288da0b03SStefano Stabellini     if (is_write) {
2223cf45183bSStefano Stabellini         pdu->s->transport->init_out_iov_from_pdu(pdu, &iov, &niov, size + skip);
222488da0b03SStefano Stabellini     } else {
2225cf45183bSStefano Stabellini         pdu->s->transport->init_in_iov_from_pdu(pdu, &iov, &niov, size + skip);
222688da0b03SStefano Stabellini     }
2227bcb8998fSStefano Stabellini 
2228bcb8998fSStefano Stabellini     qemu_iovec_init_external(&elem, iov, niov);
2229bcb8998fSStefano Stabellini     qemu_iovec_init(qiov, niov);
2230cf45183bSStefano Stabellini     qemu_iovec_concat(qiov, &elem, skip, size);
2231bcb8998fSStefano Stabellini }
2232bcb8998fSStefano Stabellini 
v9fs_xattr_read(V9fsState * s,V9fsPDU * pdu,V9fsFidState * fidp,uint64_t off,uint32_t max_count)223360ce86c7SWei Liu static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
223460ce86c7SWei Liu                            uint64_t off, uint32_t max_count)
223560ce86c7SWei Liu {
223660ce86c7SWei Liu     ssize_t err;
223760ce86c7SWei Liu     size_t offset = 7;
2238cf45183bSStefano Stabellini     uint64_t read_count;
2239bcb8998fSStefano Stabellini     QEMUIOVector qiov_full;
224060ce86c7SWei Liu 
22417e55d65cSLi Qiang     if (fidp->fs.xattr.len < off) {
22427e55d65cSLi Qiang         read_count = 0;
224316724a17SGreg Kurz     } else {
2244cf45183bSStefano Stabellini         read_count = fidp->fs.xattr.len - off;
2245cf45183bSStefano Stabellini     }
2246cf45183bSStefano Stabellini     if (read_count > max_count) {
224760ce86c7SWei Liu         read_count = max_count;
224860ce86c7SWei Liu     }
224960ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "d", read_count);
225060ce86c7SWei Liu     if (err < 0) {
225160ce86c7SWei Liu         return err;
225260ce86c7SWei Liu     }
225360ce86c7SWei Liu     offset += err;
225400588a0aSWei Liu 
2255cf45183bSStefano Stabellini     v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, read_count, false);
2256fa0eb5c5SGreg Kurz     err = v9fs_pack(qiov_full.iov, qiov_full.niov, 0,
225760ce86c7SWei Liu                     ((char *)fidp->fs.xattr.value) + off,
225860ce86c7SWei Liu                     read_count);
2259bcb8998fSStefano Stabellini     qemu_iovec_destroy(&qiov_full);
226060ce86c7SWei Liu     if (err < 0) {
226160ce86c7SWei Liu         return err;
226260ce86c7SWei Liu     }
226360ce86c7SWei Liu     offset += err;
226460ce86c7SWei Liu     return offset;
226560ce86c7SWei Liu }
226660ce86c7SWei Liu 
v9fs_do_readdir_with_stat(V9fsPDU * pdu,V9fsFidState * fidp,uint32_t max_count)22678440e22eSGreg Kurz static int coroutine_fn v9fs_do_readdir_with_stat(V9fsPDU *pdu,
22688440e22eSGreg Kurz                                                   V9fsFidState *fidp,
22698440e22eSGreg Kurz                                                   uint32_t max_count)
227060ce86c7SWei Liu {
227160ce86c7SWei Liu     V9fsPath path;
227260ce86c7SWei Liu     V9fsStat v9stat;
227360ce86c7SWei Liu     int len, err = 0;
227460ce86c7SWei Liu     int32_t count = 0;
227560ce86c7SWei Liu     struct stat stbuf;
227660ce86c7SWei Liu     off_t saved_dir_pos;
2277635324e8SGreg Kurz     struct dirent *dent;
227860ce86c7SWei Liu 
227960ce86c7SWei Liu     /* save the directory position */
228060ce86c7SWei Liu     saved_dir_pos = v9fs_co_telldir(pdu, fidp);
228160ce86c7SWei Liu     if (saved_dir_pos < 0) {
228260ce86c7SWei Liu         return saved_dir_pos;
228360ce86c7SWei Liu     }
228460ce86c7SWei Liu 
228560ce86c7SWei Liu     while (1) {
228660ce86c7SWei Liu         v9fs_path_init(&path);
22877cde47d4SGreg Kurz 
22887cde47d4SGreg Kurz         v9fs_readdir_lock(&fidp->fs.dir);
22897cde47d4SGreg Kurz 
2290635324e8SGreg Kurz         err = v9fs_co_readdir(pdu, fidp, &dent);
2291635324e8SGreg Kurz         if (err || !dent) {
229260ce86c7SWei Liu             break;
229360ce86c7SWei Liu         }
229460ce86c7SWei Liu         err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path);
229560ce86c7SWei Liu         if (err < 0) {
22968762a46dSGreg Kurz             break;
229760ce86c7SWei Liu         }
229860ce86c7SWei Liu         err = v9fs_co_lstat(pdu, &path, &stbuf);
229960ce86c7SWei Liu         if (err < 0) {
23008762a46dSGreg Kurz             break;
230160ce86c7SWei Liu         }
23026069537fSJan Dakinevich         err = stat_to_v9stat(pdu, &path, dent->d_name, &stbuf, &v9stat);
230360ce86c7SWei Liu         if (err < 0) {
23048762a46dSGreg Kurz             break;
230560ce86c7SWei Liu         }
2306772a7369SJan Dakinevich         if ((count + v9stat.size + 2) > max_count) {
23077cde47d4SGreg Kurz             v9fs_readdir_unlock(&fidp->fs.dir);
23087cde47d4SGreg Kurz 
230960ce86c7SWei Liu             /* Ran out of buffer. Set dir back to old position and return */
231060ce86c7SWei Liu             v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
231160ce86c7SWei Liu             v9fs_stat_free(&v9stat);
231260ce86c7SWei Liu             v9fs_path_free(&path);
231360ce86c7SWei Liu             return count;
231460ce86c7SWei Liu         }
2315772a7369SJan Dakinevich 
2316772a7369SJan Dakinevich         /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
2317772a7369SJan Dakinevich         len = pdu_marshal(pdu, 11 + count, "S", &v9stat);
2318772a7369SJan Dakinevich 
2319772a7369SJan Dakinevich         v9fs_readdir_unlock(&fidp->fs.dir);
2320772a7369SJan Dakinevich 
2321772a7369SJan Dakinevich         if (len < 0) {
2322772a7369SJan Dakinevich             v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
2323772a7369SJan Dakinevich             v9fs_stat_free(&v9stat);
2324772a7369SJan Dakinevich             v9fs_path_free(&path);
2325772a7369SJan Dakinevich             return len;
2326772a7369SJan Dakinevich         }
232760ce86c7SWei Liu         count += len;
232860ce86c7SWei Liu         v9fs_stat_free(&v9stat);
232960ce86c7SWei Liu         v9fs_path_free(&path);
23306b3b279bSKeno Fischer         saved_dir_pos = qemu_dirent_off(dent);
233160ce86c7SWei Liu     }
23328762a46dSGreg Kurz 
23337cde47d4SGreg Kurz     v9fs_readdir_unlock(&fidp->fs.dir);
23347cde47d4SGreg Kurz 
233560ce86c7SWei Liu     v9fs_path_free(&path);
233660ce86c7SWei Liu     if (err < 0) {
233760ce86c7SWei Liu         return err;
233860ce86c7SWei Liu     }
233960ce86c7SWei Liu     return count;
234060ce86c7SWei Liu }
234160ce86c7SWei Liu 
v9fs_read(void * opaque)23428440e22eSGreg Kurz static void coroutine_fn v9fs_read(void *opaque)
234360ce86c7SWei Liu {
234460ce86c7SWei Liu     int32_t fid;
234560ce86c7SWei Liu     uint64_t off;
234660ce86c7SWei Liu     ssize_t err = 0;
234760ce86c7SWei Liu     int32_t count = 0;
234860ce86c7SWei Liu     size_t offset = 7;
234960ce86c7SWei Liu     uint32_t max_count;
235060ce86c7SWei Liu     V9fsFidState *fidp;
235160ce86c7SWei Liu     V9fsPDU *pdu = opaque;
235260ce86c7SWei Liu     V9fsState *s = pdu->s;
235360ce86c7SWei Liu 
235460ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count);
235560ce86c7SWei Liu     if (err < 0) {
235660ce86c7SWei Liu         goto out_nofid;
235760ce86c7SWei Liu     }
235860ce86c7SWei Liu     trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count);
235960ce86c7SWei Liu 
236060ce86c7SWei Liu     fidp = get_fid(pdu, fid);
236160ce86c7SWei Liu     if (fidp == NULL) {
236260ce86c7SWei Liu         err = -EINVAL;
236360ce86c7SWei Liu         goto out_nofid;
236460ce86c7SWei Liu     }
236560ce86c7SWei Liu     if (fidp->fid_type == P9_FID_DIR) {
2366d2c5cf7cSChristian Schoenebeck         if (s->proto_version != V9FS_PROTO_2000U) {
2367d2c5cf7cSChristian Schoenebeck             warn_report_once(
2368d2c5cf7cSChristian Schoenebeck                 "9p: bad client: T_read request on directory only expected "
2369d2c5cf7cSChristian Schoenebeck                 "with 9P2000.u protocol version"
2370d2c5cf7cSChristian Schoenebeck             );
2371d2c5cf7cSChristian Schoenebeck             err = -EOPNOTSUPP;
2372d2c5cf7cSChristian Schoenebeck             goto out;
2373d2c5cf7cSChristian Schoenebeck         }
237460ce86c7SWei Liu         if (off == 0) {
237560ce86c7SWei Liu             v9fs_co_rewinddir(pdu, fidp);
237660ce86c7SWei Liu         }
237760ce86c7SWei Liu         count = v9fs_do_readdir_with_stat(pdu, fidp, max_count);
237860ce86c7SWei Liu         if (count < 0) {
237960ce86c7SWei Liu             err = count;
238060ce86c7SWei Liu             goto out;
238160ce86c7SWei Liu         }
238260ce86c7SWei Liu         err = pdu_marshal(pdu, offset, "d", count);
238360ce86c7SWei Liu         if (err < 0) {
238460ce86c7SWei Liu             goto out;
238560ce86c7SWei Liu         }
238660ce86c7SWei Liu         err += offset + count;
238760ce86c7SWei Liu     } else if (fidp->fid_type == P9_FID_FILE) {
238860ce86c7SWei Liu         QEMUIOVector qiov_full;
238960ce86c7SWei Liu         QEMUIOVector qiov;
239060ce86c7SWei Liu         int32_t len;
239160ce86c7SWei Liu 
2392cf45183bSStefano Stabellini         v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, max_count, false);
239360ce86c7SWei Liu         qemu_iovec_init(&qiov, qiov_full.niov);
239460ce86c7SWei Liu         do {
239560ce86c7SWei Liu             qemu_iovec_reset(&qiov);
239660ce86c7SWei Liu             qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count);
239760ce86c7SWei Liu             if (0) {
239860ce86c7SWei Liu                 print_sg(qiov.iov, qiov.niov);
239960ce86c7SWei Liu             }
240060ce86c7SWei Liu             /* Loop in case of EINTR */
240160ce86c7SWei Liu             do {
240260ce86c7SWei Liu                 len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off);
240360ce86c7SWei Liu                 if (len >= 0) {
240460ce86c7SWei Liu                     off   += len;
240560ce86c7SWei Liu                     count += len;
240660ce86c7SWei Liu                 }
240760ce86c7SWei Liu             } while (len == -EINTR && !pdu->cancelled);
240860ce86c7SWei Liu             if (len < 0) {
240960ce86c7SWei Liu                 /* IO error return the error */
241060ce86c7SWei Liu                 err = len;
2411e95c9a49SLi Qiang                 goto out_free_iovec;
241260ce86c7SWei Liu             }
241360ce86c7SWei Liu         } while (count < max_count && len > 0);
241460ce86c7SWei Liu         err = pdu_marshal(pdu, offset, "d", count);
241560ce86c7SWei Liu         if (err < 0) {
2416e95c9a49SLi Qiang             goto out_free_iovec;
241760ce86c7SWei Liu         }
241860ce86c7SWei Liu         err += offset + count;
2419e95c9a49SLi Qiang out_free_iovec:
242060ce86c7SWei Liu         qemu_iovec_destroy(&qiov);
242160ce86c7SWei Liu         qemu_iovec_destroy(&qiov_full);
242260ce86c7SWei Liu     } else if (fidp->fid_type == P9_FID_XATTR) {
242360ce86c7SWei Liu         err = v9fs_xattr_read(s, pdu, fidp, off, max_count);
242460ce86c7SWei Liu     } else {
242560ce86c7SWei Liu         err = -EINVAL;
242660ce86c7SWei Liu     }
242760ce86c7SWei Liu     trace_v9fs_read_return(pdu->tag, pdu->id, count, err);
242860ce86c7SWei Liu out:
242960ce86c7SWei Liu     put_fid(pdu, fidp);
243060ce86c7SWei Liu out_nofid:
243160ce86c7SWei Liu     pdu_complete(pdu, err);
243260ce86c7SWei Liu }
243360ce86c7SWei Liu 
243429c9d2caSChristian Schoenebeck /**
2435e16fea41SChristian Schoenebeck  * v9fs_readdir_response_size() - Returns size required in Rreaddir response
2436e16fea41SChristian Schoenebeck  * for the passed dirent @name.
243729c9d2caSChristian Schoenebeck  *
2438e16fea41SChristian Schoenebeck  * @name: directory entry's name (i.e. file name, directory name)
2439e16fea41SChristian Schoenebeck  * Return: required size in bytes
244029c9d2caSChristian Schoenebeck  */
v9fs_readdir_response_size(V9fsString * name)244129c9d2caSChristian Schoenebeck size_t v9fs_readdir_response_size(V9fsString *name)
244260ce86c7SWei Liu {
244360ce86c7SWei Liu     /*
244460ce86c7SWei Liu      * Size of each dirent on the wire: size of qid (13) + size of offset (8)
244560ce86c7SWei Liu      * size of type (1) + size of name.size (2) + strlen(name.data)
244660ce86c7SWei Liu      */
244760ce86c7SWei Liu     return 24 + v9fs_string_size(name);
244860ce86c7SWei Liu }
244960ce86c7SWei Liu 
v9fs_free_dirents(struct V9fsDirEnt * e)24500c4356baSChristian Schoenebeck static void v9fs_free_dirents(struct V9fsDirEnt *e)
24510c4356baSChristian Schoenebeck {
24520c4356baSChristian Schoenebeck     struct V9fsDirEnt *next = NULL;
24530c4356baSChristian Schoenebeck 
24540c4356baSChristian Schoenebeck     for (; e; e = next) {
24550c4356baSChristian Schoenebeck         next = e->next;
24560c4356baSChristian Schoenebeck         g_free(e->dent);
24570c4356baSChristian Schoenebeck         g_free(e->st);
24580c4356baSChristian Schoenebeck         g_free(e);
24590c4356baSChristian Schoenebeck     }
24600c4356baSChristian Schoenebeck }
24610c4356baSChristian Schoenebeck 
v9fs_do_readdir(V9fsPDU * pdu,V9fsFidState * fidp,off_t offset,int32_t max_count)24628440e22eSGreg Kurz static int coroutine_fn v9fs_do_readdir(V9fsPDU *pdu, V9fsFidState *fidp,
24630c4356baSChristian Schoenebeck                                         off_t offset, int32_t max_count)
246460ce86c7SWei Liu {
246560ce86c7SWei Liu     size_t size;
246660ce86c7SWei Liu     V9fsQID qid;
246760ce86c7SWei Liu     V9fsString name;
246860ce86c7SWei Liu     int len, err = 0;
246960ce86c7SWei Liu     int32_t count = 0;
24706b3b279bSKeno Fischer     off_t off;
2471635324e8SGreg Kurz     struct dirent *dent;
24720c4356baSChristian Schoenebeck     struct stat *st;
24730c4356baSChristian Schoenebeck     struct V9fsDirEnt *entries = NULL;
247460ce86c7SWei Liu 
24750c4356baSChristian Schoenebeck     /*
24760c4356baSChristian Schoenebeck      * inode remapping requires the device id, which in turn might be
24770c4356baSChristian Schoenebeck      * different for different directory entries, so if inode remapping is
24780c4356baSChristian Schoenebeck      * enabled we have to make a full stat for each directory entry
24790c4356baSChristian Schoenebeck      */
24800c4356baSChristian Schoenebeck     const bool dostat = pdu->s->ctx.export_flags & V9FS_REMAP_INODES;
24810c4356baSChristian Schoenebeck 
24820c4356baSChristian Schoenebeck     /*
24830c4356baSChristian Schoenebeck      * Fetch all required directory entries altogether on a background IO
24840c4356baSChristian Schoenebeck      * thread from fs driver. We don't want to do that for each entry
24850c4356baSChristian Schoenebeck      * individually, because hopping between threads (this main IO thread
24860c4356baSChristian Schoenebeck      * and background IO driver thread) would sum up to huge latencies.
24870c4356baSChristian Schoenebeck      */
24880c4356baSChristian Schoenebeck     count = v9fs_co_readdir_many(pdu, fidp, &entries, offset, max_count,
24890c4356baSChristian Schoenebeck                                  dostat);
24900c4356baSChristian Schoenebeck     if (count < 0) {
24910c4356baSChristian Schoenebeck         err = count;
24920c4356baSChristian Schoenebeck         count = 0;
24930c4356baSChristian Schoenebeck         goto out;
249460ce86c7SWei Liu     }
24950c4356baSChristian Schoenebeck     count = 0;
249660ce86c7SWei Liu 
24970c4356baSChristian Schoenebeck     for (struct V9fsDirEnt *e = entries; e; e = e->next) {
24980c4356baSChristian Schoenebeck         dent = e->dent;
24991a6ed33cSAntonios Motakis 
25001a6ed33cSAntonios Motakis         if (pdu->s->ctx.export_flags & V9FS_REMAP_INODES) {
25010c4356baSChristian Schoenebeck             st = e->st;
25020c4356baSChristian Schoenebeck             /* e->st should never be NULL, but just to be sure */
25030c4356baSChristian Schoenebeck             if (!st) {
25040c4356baSChristian Schoenebeck                 err = -1;
25050c4356baSChristian Schoenebeck                 break;
25060c4356baSChristian Schoenebeck             }
25070c4356baSChristian Schoenebeck 
25080c4356baSChristian Schoenebeck             /* remap inode */
25090c4356baSChristian Schoenebeck             err = stat_to_qid(pdu, st, &qid);
25101a6ed33cSAntonios Motakis             if (err < 0) {
25110c4356baSChristian Schoenebeck                 break;
25121a6ed33cSAntonios Motakis             }
25131a6ed33cSAntonios Motakis         } else {
251460ce86c7SWei Liu             /*
251560ce86c7SWei Liu              * Fill up just the path field of qid because the client uses
251660ce86c7SWei Liu              * only that. To fill the entire qid structure we will have
25171a6ed33cSAntonios Motakis              * to stat each dirent found, which is expensive. For the
25180c4356baSChristian Schoenebeck              * latter reason we don't call stat_to_qid() here. Only drawback
25191a6ed33cSAntonios Motakis              * is that no multi-device export detection of stat_to_qid()
25201a6ed33cSAntonios Motakis              * would be done and provided as error to the user here. But
25211a6ed33cSAntonios Motakis              * user would get that error anyway when accessing those
25221a6ed33cSAntonios Motakis              * files/dirs through other ways.
252360ce86c7SWei Liu              */
252460ce86c7SWei Liu             size = MIN(sizeof(dent->d_ino), sizeof(qid.path));
252560ce86c7SWei Liu             memcpy(&qid.path, &dent->d_ino, size);
252660ce86c7SWei Liu             /* Fill the other fields with dummy values */
252760ce86c7SWei Liu             qid.type = 0;
252860ce86c7SWei Liu             qid.version = 0;
25291a6ed33cSAntonios Motakis         }
253060ce86c7SWei Liu 
25316b3b279bSKeno Fischer         off = qemu_dirent_off(dent);
25320c4356baSChristian Schoenebeck         v9fs_string_init(&name);
25330c4356baSChristian Schoenebeck         v9fs_string_sprintf(&name, "%s", dent->d_name);
25340c4356baSChristian Schoenebeck 
253560ce86c7SWei Liu         /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
253660ce86c7SWei Liu         len = pdu_marshal(pdu, 11 + count, "Qqbs",
25376b3b279bSKeno Fischer                           &qid, off,
253860ce86c7SWei Liu                           dent->d_type, &name);
25397cde47d4SGreg Kurz 
25400c4356baSChristian Schoenebeck         v9fs_string_free(&name);
25417cde47d4SGreg Kurz 
254260ce86c7SWei Liu         if (len < 0) {
25430c4356baSChristian Schoenebeck             err = len;
25440c4356baSChristian Schoenebeck             break;
254560ce86c7SWei Liu         }
25460c4356baSChristian Schoenebeck 
254760ce86c7SWei Liu         count += len;
254860ce86c7SWei Liu     }
25497cde47d4SGreg Kurz 
25500c4356baSChristian Schoenebeck out:
25510c4356baSChristian Schoenebeck     v9fs_free_dirents(entries);
255260ce86c7SWei Liu     if (err < 0) {
255360ce86c7SWei Liu         return err;
255460ce86c7SWei Liu     }
255560ce86c7SWei Liu     return count;
255660ce86c7SWei Liu }
255760ce86c7SWei Liu 
v9fs_readdir(void * opaque)25588440e22eSGreg Kurz static void coroutine_fn v9fs_readdir(void *opaque)
255960ce86c7SWei Liu {
256060ce86c7SWei Liu     int32_t fid;
256160ce86c7SWei Liu     V9fsFidState *fidp;
256260ce86c7SWei Liu     ssize_t retval = 0;
256360ce86c7SWei Liu     size_t offset = 7;
256460ce86c7SWei Liu     uint64_t initial_offset;
256560ce86c7SWei Liu     int32_t count;
256660ce86c7SWei Liu     uint32_t max_count;
256760ce86c7SWei Liu     V9fsPDU *pdu = opaque;
2568d36a5c22SChristian Schoenebeck     V9fsState *s = pdu->s;
256960ce86c7SWei Liu 
257060ce86c7SWei Liu     retval = pdu_unmarshal(pdu, offset, "dqd", &fid,
257160ce86c7SWei Liu                            &initial_offset, &max_count);
257260ce86c7SWei Liu     if (retval < 0) {
257360ce86c7SWei Liu         goto out_nofid;
257460ce86c7SWei Liu     }
257560ce86c7SWei Liu     trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count);
257660ce86c7SWei Liu 
2577d36a5c22SChristian Schoenebeck     /* Enough space for a R_readdir header: size[4] Rreaddir tag[2] count[4] */
2578d36a5c22SChristian Schoenebeck     if (max_count > s->msize - 11) {
2579d36a5c22SChristian Schoenebeck         max_count = s->msize - 11;
2580d36a5c22SChristian Schoenebeck         warn_report_once(
2581d36a5c22SChristian Schoenebeck             "9p: bad client: T_readdir with count > msize - 11"
2582d36a5c22SChristian Schoenebeck         );
2583d36a5c22SChristian Schoenebeck     }
2584d36a5c22SChristian Schoenebeck 
258560ce86c7SWei Liu     fidp = get_fid(pdu, fid);
258660ce86c7SWei Liu     if (fidp == NULL) {
258760ce86c7SWei Liu         retval = -EINVAL;
258860ce86c7SWei Liu         goto out_nofid;
258960ce86c7SWei Liu     }
2590f314ea4eSGreg Kurz     if (!fidp->fs.dir.stream) {
259160ce86c7SWei Liu         retval = -EINVAL;
259260ce86c7SWei Liu         goto out;
259360ce86c7SWei Liu     }
2594d2c5cf7cSChristian Schoenebeck     if (s->proto_version != V9FS_PROTO_2000L) {
2595d2c5cf7cSChristian Schoenebeck         warn_report_once(
2596d2c5cf7cSChristian Schoenebeck             "9p: bad client: T_readdir request only expected with 9P2000.L "
2597d2c5cf7cSChristian Schoenebeck             "protocol version"
2598d2c5cf7cSChristian Schoenebeck         );
2599d2c5cf7cSChristian Schoenebeck         retval = -EOPNOTSUPP;
2600d2c5cf7cSChristian Schoenebeck         goto out;
2601d2c5cf7cSChristian Schoenebeck     }
26020c4356baSChristian Schoenebeck     count = v9fs_do_readdir(pdu, fidp, (off_t) initial_offset, max_count);
260360ce86c7SWei Liu     if (count < 0) {
260460ce86c7SWei Liu         retval = count;
260560ce86c7SWei Liu         goto out;
260660ce86c7SWei Liu     }
260760ce86c7SWei Liu     retval = pdu_marshal(pdu, offset, "d", count);
260860ce86c7SWei Liu     if (retval < 0) {
260960ce86c7SWei Liu         goto out;
261060ce86c7SWei Liu     }
261160ce86c7SWei Liu     retval += count + offset;
261260ce86c7SWei Liu     trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval);
261360ce86c7SWei Liu out:
261460ce86c7SWei Liu     put_fid(pdu, fidp);
261560ce86c7SWei Liu out_nofid:
261660ce86c7SWei Liu     pdu_complete(pdu, retval);
261760ce86c7SWei Liu }
261860ce86c7SWei Liu 
v9fs_xattr_write(V9fsState * s,V9fsPDU * pdu,V9fsFidState * fidp,uint64_t off,uint32_t count,struct iovec * sg,int cnt)261960ce86c7SWei Liu static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
262060ce86c7SWei Liu                             uint64_t off, uint32_t count,
262160ce86c7SWei Liu                             struct iovec *sg, int cnt)
262260ce86c7SWei Liu {
262360ce86c7SWei Liu     int i, to_copy;
262460ce86c7SWei Liu     ssize_t err = 0;
26257e55d65cSLi Qiang     uint64_t write_count;
262660ce86c7SWei Liu     size_t offset = 7;
262760ce86c7SWei Liu 
262860ce86c7SWei Liu 
26297e55d65cSLi Qiang     if (fidp->fs.xattr.len < off) {
2630b858e80aSDaniel Henrique Barboza         return -ENOSPC;
263160ce86c7SWei Liu     }
26327e55d65cSLi Qiang     write_count = fidp->fs.xattr.len - off;
26337e55d65cSLi Qiang     if (write_count > count) {
26347e55d65cSLi Qiang         write_count = count;
26357e55d65cSLi Qiang     }
263660ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "d", write_count);
263760ce86c7SWei Liu     if (err < 0) {
263860ce86c7SWei Liu         return err;
263960ce86c7SWei Liu     }
264060ce86c7SWei Liu     err += offset;
264160ce86c7SWei Liu     fidp->fs.xattr.copied_len += write_count;
264260ce86c7SWei Liu     /*
264360ce86c7SWei Liu      * Now copy the content from sg list
264460ce86c7SWei Liu      */
264560ce86c7SWei Liu     for (i = 0; i < cnt; i++) {
264660ce86c7SWei Liu         if (write_count > sg[i].iov_len) {
264760ce86c7SWei Liu             to_copy = sg[i].iov_len;
264860ce86c7SWei Liu         } else {
264960ce86c7SWei Liu             to_copy = write_count;
265060ce86c7SWei Liu         }
265160ce86c7SWei Liu         memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy);
265260ce86c7SWei Liu         /* updating vs->off since we are not using below */
265360ce86c7SWei Liu         off += to_copy;
265460ce86c7SWei Liu         write_count -= to_copy;
265560ce86c7SWei Liu     }
2656b858e80aSDaniel Henrique Barboza 
265760ce86c7SWei Liu     return err;
265860ce86c7SWei Liu }
265960ce86c7SWei Liu 
v9fs_write(void * opaque)26608440e22eSGreg Kurz static void coroutine_fn v9fs_write(void *opaque)
266160ce86c7SWei Liu {
266260ce86c7SWei Liu     ssize_t err;
266360ce86c7SWei Liu     int32_t fid;
266460ce86c7SWei Liu     uint64_t off;
266560ce86c7SWei Liu     uint32_t count;
266660ce86c7SWei Liu     int32_t len = 0;
266760ce86c7SWei Liu     int32_t total = 0;
266860ce86c7SWei Liu     size_t offset = 7;
266960ce86c7SWei Liu     V9fsFidState *fidp;
267060ce86c7SWei Liu     V9fsPDU *pdu = opaque;
267160ce86c7SWei Liu     V9fsState *s = pdu->s;
267260ce86c7SWei Liu     QEMUIOVector qiov_full;
267360ce86c7SWei Liu     QEMUIOVector qiov;
267460ce86c7SWei Liu 
267560ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count);
267660ce86c7SWei Liu     if (err < 0) {
267760ce86c7SWei Liu         pdu_complete(pdu, err);
267860ce86c7SWei Liu         return;
267960ce86c7SWei Liu     }
268060ce86c7SWei Liu     offset += err;
2681cf45183bSStefano Stabellini     v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, count, true);
268260ce86c7SWei Liu     trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov);
268360ce86c7SWei Liu 
268460ce86c7SWei Liu     fidp = get_fid(pdu, fid);
268560ce86c7SWei Liu     if (fidp == NULL) {
268660ce86c7SWei Liu         err = -EINVAL;
268760ce86c7SWei Liu         goto out_nofid;
268860ce86c7SWei Liu     }
268960ce86c7SWei Liu     if (fidp->fid_type == P9_FID_FILE) {
269060ce86c7SWei Liu         if (fidp->fs.fd == -1) {
269160ce86c7SWei Liu             err = -EINVAL;
269260ce86c7SWei Liu             goto out;
269360ce86c7SWei Liu         }
269460ce86c7SWei Liu     } else if (fidp->fid_type == P9_FID_XATTR) {
269560ce86c7SWei Liu         /*
269660ce86c7SWei Liu          * setxattr operation
269760ce86c7SWei Liu          */
269860ce86c7SWei Liu         err = v9fs_xattr_write(s, pdu, fidp, off, count,
269960ce86c7SWei Liu                                qiov_full.iov, qiov_full.niov);
270060ce86c7SWei Liu         goto out;
270160ce86c7SWei Liu     } else {
270260ce86c7SWei Liu         err = -EINVAL;
270360ce86c7SWei Liu         goto out;
270460ce86c7SWei Liu     }
270560ce86c7SWei Liu     qemu_iovec_init(&qiov, qiov_full.niov);
270660ce86c7SWei Liu     do {
270760ce86c7SWei Liu         qemu_iovec_reset(&qiov);
270860ce86c7SWei Liu         qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total);
270960ce86c7SWei Liu         if (0) {
271060ce86c7SWei Liu             print_sg(qiov.iov, qiov.niov);
271160ce86c7SWei Liu         }
271260ce86c7SWei Liu         /* Loop in case of EINTR */
271360ce86c7SWei Liu         do {
271460ce86c7SWei Liu             len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off);
271560ce86c7SWei Liu             if (len >= 0) {
271660ce86c7SWei Liu                 off   += len;
271760ce86c7SWei Liu                 total += len;
271860ce86c7SWei Liu             }
271960ce86c7SWei Liu         } while (len == -EINTR && !pdu->cancelled);
272060ce86c7SWei Liu         if (len < 0) {
272160ce86c7SWei Liu             /* IO error return the error */
272260ce86c7SWei Liu             err = len;
272360ce86c7SWei Liu             goto out_qiov;
272460ce86c7SWei Liu         }
272560ce86c7SWei Liu     } while (total < count && len > 0);
272660ce86c7SWei Liu 
272760ce86c7SWei Liu     offset = 7;
272860ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "d", total);
272960ce86c7SWei Liu     if (err < 0) {
2730fdfcc9aeSLi Qiang         goto out_qiov;
273160ce86c7SWei Liu     }
273260ce86c7SWei Liu     err += offset;
273360ce86c7SWei Liu     trace_v9fs_write_return(pdu->tag, pdu->id, total, err);
273460ce86c7SWei Liu out_qiov:
273560ce86c7SWei Liu     qemu_iovec_destroy(&qiov);
273660ce86c7SWei Liu out:
273760ce86c7SWei Liu     put_fid(pdu, fidp);
273860ce86c7SWei Liu out_nofid:
273960ce86c7SWei Liu     qemu_iovec_destroy(&qiov_full);
274060ce86c7SWei Liu     pdu_complete(pdu, err);
274160ce86c7SWei Liu }
274260ce86c7SWei Liu 
v9fs_create(void * opaque)27438440e22eSGreg Kurz static void coroutine_fn v9fs_create(void *opaque)
274460ce86c7SWei Liu {
274560ce86c7SWei Liu     int32_t fid;
274660ce86c7SWei Liu     int err = 0;
274760ce86c7SWei Liu     size_t offset = 7;
274860ce86c7SWei Liu     V9fsFidState *fidp;
274960ce86c7SWei Liu     V9fsQID qid;
275060ce86c7SWei Liu     int32_t perm;
275160ce86c7SWei Liu     int8_t mode;
275260ce86c7SWei Liu     V9fsPath path;
275360ce86c7SWei Liu     struct stat stbuf;
275460ce86c7SWei Liu     V9fsString name;
275560ce86c7SWei Liu     V9fsString extension;
275660ce86c7SWei Liu     int iounit;
275760ce86c7SWei Liu     V9fsPDU *pdu = opaque;
27585b3c77aaSGreg Kurz     V9fsState *s = pdu->s;
275960ce86c7SWei Liu 
276060ce86c7SWei Liu     v9fs_path_init(&path);
276160ce86c7SWei Liu     v9fs_string_init(&name);
276260ce86c7SWei Liu     v9fs_string_init(&extension);
276360ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name,
276460ce86c7SWei Liu                         &perm, &mode, &extension);
276560ce86c7SWei Liu     if (err < 0) {
276660ce86c7SWei Liu         goto out_nofid;
276760ce86c7SWei Liu     }
276860ce86c7SWei Liu     trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode);
276960ce86c7SWei Liu 
2770fff39a7aSGreg Kurz     if (name_is_illegal(name.data)) {
2771fff39a7aSGreg Kurz         err = -ENOENT;
2772fff39a7aSGreg Kurz         goto out_nofid;
2773fff39a7aSGreg Kurz     }
2774fff39a7aSGreg Kurz 
2775805b5d98SGreg Kurz     if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2776805b5d98SGreg Kurz         err = -EEXIST;
2777805b5d98SGreg Kurz         goto out_nofid;
2778805b5d98SGreg Kurz     }
2779805b5d98SGreg Kurz 
278060ce86c7SWei Liu     fidp = get_fid(pdu, fid);
278160ce86c7SWei Liu     if (fidp == NULL) {
278260ce86c7SWei Liu         err = -EINVAL;
278360ce86c7SWei Liu         goto out_nofid;
278460ce86c7SWei Liu     }
2785d63fb193SLi Qiang     if (fidp->fid_type != P9_FID_NONE) {
2786d63fb193SLi Qiang         err = -EINVAL;
2787d63fb193SLi Qiang         goto out;
2788d63fb193SLi Qiang     }
278960ce86c7SWei Liu     if (perm & P9_STAT_MODE_DIR) {
279060ce86c7SWei Liu         err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777,
279160ce86c7SWei Liu                             fidp->uid, -1, &stbuf);
279260ce86c7SWei Liu         if (err < 0) {
279360ce86c7SWei Liu             goto out;
279460ce86c7SWei Liu         }
279560ce86c7SWei Liu         err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
279660ce86c7SWei Liu         if (err < 0) {
279760ce86c7SWei Liu             goto out;
279860ce86c7SWei Liu         }
27995b3c77aaSGreg Kurz         v9fs_path_write_lock(s);
280060ce86c7SWei Liu         v9fs_path_copy(&fidp->path, &path);
28015b3c77aaSGreg Kurz         v9fs_path_unlock(s);
280260ce86c7SWei Liu         err = v9fs_co_opendir(pdu, fidp);
280360ce86c7SWei Liu         if (err < 0) {
280460ce86c7SWei Liu             goto out;
280560ce86c7SWei Liu         }
280660ce86c7SWei Liu         fidp->fid_type = P9_FID_DIR;
280760ce86c7SWei Liu     } else if (perm & P9_STAT_MODE_SYMLINK) {
280860ce86c7SWei Liu         err = v9fs_co_symlink(pdu, fidp, &name,
280960ce86c7SWei Liu                               extension.data, -1 , &stbuf);
281060ce86c7SWei Liu         if (err < 0) {
281160ce86c7SWei Liu             goto out;
281260ce86c7SWei Liu         }
281360ce86c7SWei Liu         err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
281460ce86c7SWei Liu         if (err < 0) {
281560ce86c7SWei Liu             goto out;
281660ce86c7SWei Liu         }
28175b3c77aaSGreg Kurz         v9fs_path_write_lock(s);
281860ce86c7SWei Liu         v9fs_path_copy(&fidp->path, &path);
28195b3c77aaSGreg Kurz         v9fs_path_unlock(s);
282060ce86c7SWei Liu     } else if (perm & P9_STAT_MODE_LINK) {
282160ce86c7SWei Liu         int32_t ofid = atoi(extension.data);
282260ce86c7SWei Liu         V9fsFidState *ofidp = get_fid(pdu, ofid);
282360ce86c7SWei Liu         if (ofidp == NULL) {
282460ce86c7SWei Liu             err = -EINVAL;
282560ce86c7SWei Liu             goto out;
282660ce86c7SWei Liu         }
282760ce86c7SWei Liu         err = v9fs_co_link(pdu, ofidp, fidp, &name);
282860ce86c7SWei Liu         put_fid(pdu, ofidp);
282960ce86c7SWei Liu         if (err < 0) {
283060ce86c7SWei Liu             goto out;
283160ce86c7SWei Liu         }
283260ce86c7SWei Liu         err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
283360ce86c7SWei Liu         if (err < 0) {
283460ce86c7SWei Liu             fidp->fid_type = P9_FID_NONE;
283560ce86c7SWei Liu             goto out;
283660ce86c7SWei Liu         }
28375b3c77aaSGreg Kurz         v9fs_path_write_lock(s);
283860ce86c7SWei Liu         v9fs_path_copy(&fidp->path, &path);
28395b3c77aaSGreg Kurz         v9fs_path_unlock(s);
284060ce86c7SWei Liu         err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
284160ce86c7SWei Liu         if (err < 0) {
284260ce86c7SWei Liu             fidp->fid_type = P9_FID_NONE;
284360ce86c7SWei Liu             goto out;
284460ce86c7SWei Liu         }
284560ce86c7SWei Liu     } else if (perm & P9_STAT_MODE_DEVICE) {
284660ce86c7SWei Liu         char ctype;
284760ce86c7SWei Liu         uint32_t major, minor;
284860ce86c7SWei Liu         mode_t nmode = 0;
284960ce86c7SWei Liu 
285060ce86c7SWei Liu         if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) {
285160ce86c7SWei Liu             err = -errno;
285260ce86c7SWei Liu             goto out;
285360ce86c7SWei Liu         }
285460ce86c7SWei Liu 
285560ce86c7SWei Liu         switch (ctype) {
285660ce86c7SWei Liu         case 'c':
285760ce86c7SWei Liu             nmode = S_IFCHR;
285860ce86c7SWei Liu             break;
285960ce86c7SWei Liu         case 'b':
286060ce86c7SWei Liu             nmode = S_IFBLK;
286160ce86c7SWei Liu             break;
286260ce86c7SWei Liu         default:
286360ce86c7SWei Liu             err = -EIO;
286460ce86c7SWei Liu             goto out;
286560ce86c7SWei Liu         }
286660ce86c7SWei Liu 
286760ce86c7SWei Liu         nmode |= perm & 0777;
286860ce86c7SWei Liu         err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
286960ce86c7SWei Liu                             makedev(major, minor), nmode, &stbuf);
287060ce86c7SWei Liu         if (err < 0) {
287160ce86c7SWei Liu             goto out;
287260ce86c7SWei Liu         }
287360ce86c7SWei Liu         err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
287460ce86c7SWei Liu         if (err < 0) {
287560ce86c7SWei Liu             goto out;
287660ce86c7SWei Liu         }
28775b3c77aaSGreg Kurz         v9fs_path_write_lock(s);
287860ce86c7SWei Liu         v9fs_path_copy(&fidp->path, &path);
28795b3c77aaSGreg Kurz         v9fs_path_unlock(s);
288060ce86c7SWei Liu     } else if (perm & P9_STAT_MODE_NAMED_PIPE) {
288160ce86c7SWei Liu         err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
288260ce86c7SWei Liu                             0, S_IFIFO | (perm & 0777), &stbuf);
288360ce86c7SWei Liu         if (err < 0) {
288460ce86c7SWei Liu             goto out;
288560ce86c7SWei Liu         }
288660ce86c7SWei Liu         err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
288760ce86c7SWei Liu         if (err < 0) {
288860ce86c7SWei Liu             goto out;
288960ce86c7SWei Liu         }
28905b3c77aaSGreg Kurz         v9fs_path_write_lock(s);
289160ce86c7SWei Liu         v9fs_path_copy(&fidp->path, &path);
28925b3c77aaSGreg Kurz         v9fs_path_unlock(s);
289360ce86c7SWei Liu     } else if (perm & P9_STAT_MODE_SOCKET) {
289460ce86c7SWei Liu         err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
289560ce86c7SWei Liu                             0, S_IFSOCK | (perm & 0777), &stbuf);
289660ce86c7SWei Liu         if (err < 0) {
289760ce86c7SWei Liu             goto out;
289860ce86c7SWei Liu         }
289960ce86c7SWei Liu         err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
290060ce86c7SWei Liu         if (err < 0) {
290160ce86c7SWei Liu             goto out;
290260ce86c7SWei Liu         }
29035b3c77aaSGreg Kurz         v9fs_path_write_lock(s);
290460ce86c7SWei Liu         v9fs_path_copy(&fidp->path, &path);
29055b3c77aaSGreg Kurz         v9fs_path_unlock(s);
290660ce86c7SWei Liu     } else {
290760ce86c7SWei Liu         err = v9fs_co_open2(pdu, fidp, &name, -1,
290860ce86c7SWei Liu                             omode_to_uflags(mode) | O_CREAT, perm, &stbuf);
290960ce86c7SWei Liu         if (err < 0) {
291060ce86c7SWei Liu             goto out;
291160ce86c7SWei Liu         }
291260ce86c7SWei Liu         fidp->fid_type = P9_FID_FILE;
291360ce86c7SWei Liu         fidp->open_flags = omode_to_uflags(mode);
291460ce86c7SWei Liu         if (fidp->open_flags & O_EXCL) {
291560ce86c7SWei Liu             /*
291660ce86c7SWei Liu              * We let the host file system do O_EXCL check
291760ce86c7SWei Liu              * We should not reclaim such fd
291860ce86c7SWei Liu              */
291960ce86c7SWei Liu             fidp->flags |= FID_NON_RECLAIMABLE;
292060ce86c7SWei Liu         }
292160ce86c7SWei Liu     }
292260ce86c7SWei Liu     iounit = get_iounit(pdu, &fidp->path);
29233b5ee9e8SAntonios Motakis     err = stat_to_qid(pdu, &stbuf, &qid);
29243b5ee9e8SAntonios Motakis     if (err < 0) {
29253b5ee9e8SAntonios Motakis         goto out;
29263b5ee9e8SAntonios Motakis     }
292760ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
292860ce86c7SWei Liu     if (err < 0) {
292960ce86c7SWei Liu         goto out;
293060ce86c7SWei Liu     }
293160ce86c7SWei Liu     err += offset;
293260ce86c7SWei Liu     trace_v9fs_create_return(pdu->tag, pdu->id,
293360ce86c7SWei Liu                              qid.type, qid.version, qid.path, iounit);
293460ce86c7SWei Liu out:
293560ce86c7SWei Liu     put_fid(pdu, fidp);
293660ce86c7SWei Liu out_nofid:
293760ce86c7SWei Liu    pdu_complete(pdu, err);
293860ce86c7SWei Liu    v9fs_string_free(&name);
293960ce86c7SWei Liu    v9fs_string_free(&extension);
294060ce86c7SWei Liu    v9fs_path_free(&path);
294160ce86c7SWei Liu }
294260ce86c7SWei Liu 
v9fs_symlink(void * opaque)29438440e22eSGreg Kurz static void coroutine_fn v9fs_symlink(void *opaque)
294460ce86c7SWei Liu {
294560ce86c7SWei Liu     V9fsPDU *pdu = opaque;
294660ce86c7SWei Liu     V9fsString name;
294760ce86c7SWei Liu     V9fsString symname;
294860ce86c7SWei Liu     V9fsFidState *dfidp;
294960ce86c7SWei Liu     V9fsQID qid;
295060ce86c7SWei Liu     struct stat stbuf;
295160ce86c7SWei Liu     int32_t dfid;
295260ce86c7SWei Liu     int err = 0;
295360ce86c7SWei Liu     gid_t gid;
295460ce86c7SWei Liu     size_t offset = 7;
295560ce86c7SWei Liu 
295660ce86c7SWei Liu     v9fs_string_init(&name);
295760ce86c7SWei Liu     v9fs_string_init(&symname);
295860ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid);
295960ce86c7SWei Liu     if (err < 0) {
296060ce86c7SWei Liu         goto out_nofid;
296160ce86c7SWei Liu     }
296260ce86c7SWei Liu     trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid);
296360ce86c7SWei Liu 
2964fff39a7aSGreg Kurz     if (name_is_illegal(name.data)) {
2965fff39a7aSGreg Kurz         err = -ENOENT;
2966fff39a7aSGreg Kurz         goto out_nofid;
2967fff39a7aSGreg Kurz     }
2968fff39a7aSGreg Kurz 
2969805b5d98SGreg Kurz     if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2970805b5d98SGreg Kurz         err = -EEXIST;
2971805b5d98SGreg Kurz         goto out_nofid;
2972805b5d98SGreg Kurz     }
2973805b5d98SGreg Kurz 
297460ce86c7SWei Liu     dfidp = get_fid(pdu, dfid);
297560ce86c7SWei Liu     if (dfidp == NULL) {
297660ce86c7SWei Liu         err = -EINVAL;
297760ce86c7SWei Liu         goto out_nofid;
297860ce86c7SWei Liu     }
297960ce86c7SWei Liu     err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf);
298060ce86c7SWei Liu     if (err < 0) {
298160ce86c7SWei Liu         goto out;
298260ce86c7SWei Liu     }
29833b5ee9e8SAntonios Motakis     err = stat_to_qid(pdu, &stbuf, &qid);
29843b5ee9e8SAntonios Motakis     if (err < 0) {
29853b5ee9e8SAntonios Motakis         goto out;
29863b5ee9e8SAntonios Motakis     }
298760ce86c7SWei Liu     err =  pdu_marshal(pdu, offset, "Q", &qid);
298860ce86c7SWei Liu     if (err < 0) {
298960ce86c7SWei Liu         goto out;
299060ce86c7SWei Liu     }
299160ce86c7SWei Liu     err += offset;
299260ce86c7SWei Liu     trace_v9fs_symlink_return(pdu->tag, pdu->id,
299360ce86c7SWei Liu                               qid.type, qid.version, qid.path);
299460ce86c7SWei Liu out:
299560ce86c7SWei Liu     put_fid(pdu, dfidp);
299660ce86c7SWei Liu out_nofid:
299760ce86c7SWei Liu     pdu_complete(pdu, err);
299860ce86c7SWei Liu     v9fs_string_free(&name);
299960ce86c7SWei Liu     v9fs_string_free(&symname);
300060ce86c7SWei Liu }
300160ce86c7SWei Liu 
v9fs_flush(void * opaque)3002a1bf8b74SGreg Kurz static void coroutine_fn v9fs_flush(void *opaque)
300360ce86c7SWei Liu {
300460ce86c7SWei Liu     ssize_t err;
300560ce86c7SWei Liu     int16_t tag;
300660ce86c7SWei Liu     size_t offset = 7;
3007d5f2af7bSGreg Kurz     V9fsPDU *cancel_pdu = NULL;
300860ce86c7SWei Liu     V9fsPDU *pdu = opaque;
300960ce86c7SWei Liu     V9fsState *s = pdu->s;
301060ce86c7SWei Liu 
301160ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "w", &tag);
301260ce86c7SWei Liu     if (err < 0) {
301360ce86c7SWei Liu         pdu_complete(pdu, err);
301460ce86c7SWei Liu         return;
301560ce86c7SWei Liu     }
301660ce86c7SWei Liu     trace_v9fs_flush(pdu->tag, pdu->id, tag);
301760ce86c7SWei Liu 
3018d5f2af7bSGreg Kurz     if (pdu->tag == tag) {
30193dc6f869SAlistair Francis         warn_report("the guest sent a self-referencing 9P flush request");
3020d5f2af7bSGreg Kurz     } else {
302160ce86c7SWei Liu         QLIST_FOREACH(cancel_pdu, &s->active_list, next) {
302260ce86c7SWei Liu             if (cancel_pdu->tag == tag) {
302360ce86c7SWei Liu                 break;
302460ce86c7SWei Liu             }
302560ce86c7SWei Liu         }
3026d5f2af7bSGreg Kurz     }
302760ce86c7SWei Liu     if (cancel_pdu) {
302860ce86c7SWei Liu         cancel_pdu->cancelled = 1;
302960ce86c7SWei Liu         /*
303060ce86c7SWei Liu          * Wait for pdu to complete.
303160ce86c7SWei Liu          */
30321ace7ceaSPaolo Bonzini         qemu_co_queue_wait(&cancel_pdu->complete, NULL);
303318adde86SGreg Kurz         if (!qemu_co_queue_next(&cancel_pdu->complete)) {
303460ce86c7SWei Liu             cancel_pdu->cancelled = 0;
303560ce86c7SWei Liu             pdu_free(cancel_pdu);
303660ce86c7SWei Liu         }
303718adde86SGreg Kurz     }
303860ce86c7SWei Liu     pdu_complete(pdu, 7);
303960ce86c7SWei Liu }
304060ce86c7SWei Liu 
v9fs_link(void * opaque)30418440e22eSGreg Kurz static void coroutine_fn v9fs_link(void *opaque)
304260ce86c7SWei Liu {
304360ce86c7SWei Liu     V9fsPDU *pdu = opaque;
304460ce86c7SWei Liu     int32_t dfid, oldfid;
304560ce86c7SWei Liu     V9fsFidState *dfidp, *oldfidp;
304660ce86c7SWei Liu     V9fsString name;
304760ce86c7SWei Liu     size_t offset = 7;
304860ce86c7SWei Liu     int err = 0;
304960ce86c7SWei Liu 
305060ce86c7SWei Liu     v9fs_string_init(&name);
305160ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
305260ce86c7SWei Liu     if (err < 0) {
305360ce86c7SWei Liu         goto out_nofid;
305460ce86c7SWei Liu     }
305560ce86c7SWei Liu     trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data);
305660ce86c7SWei Liu 
3057fff39a7aSGreg Kurz     if (name_is_illegal(name.data)) {
3058fff39a7aSGreg Kurz         err = -ENOENT;
3059fff39a7aSGreg Kurz         goto out_nofid;
3060fff39a7aSGreg Kurz     }
3061fff39a7aSGreg Kurz 
3062805b5d98SGreg Kurz     if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3063805b5d98SGreg Kurz         err = -EEXIST;
3064805b5d98SGreg Kurz         goto out_nofid;
3065805b5d98SGreg Kurz     }
3066805b5d98SGreg Kurz 
306760ce86c7SWei Liu     dfidp = get_fid(pdu, dfid);
306860ce86c7SWei Liu     if (dfidp == NULL) {
306960ce86c7SWei Liu         err = -ENOENT;
307060ce86c7SWei Liu         goto out_nofid;
307160ce86c7SWei Liu     }
307260ce86c7SWei Liu 
307360ce86c7SWei Liu     oldfidp = get_fid(pdu, oldfid);
307460ce86c7SWei Liu     if (oldfidp == NULL) {
307560ce86c7SWei Liu         err = -ENOENT;
307660ce86c7SWei Liu         goto out;
307760ce86c7SWei Liu     }
307860ce86c7SWei Liu     err = v9fs_co_link(pdu, oldfidp, dfidp, &name);
307960ce86c7SWei Liu     if (!err) {
308060ce86c7SWei Liu         err = offset;
308160ce86c7SWei Liu     }
30824c158678SLi Qiang     put_fid(pdu, oldfidp);
308360ce86c7SWei Liu out:
308460ce86c7SWei Liu     put_fid(pdu, dfidp);
308560ce86c7SWei Liu out_nofid:
308660ce86c7SWei Liu     v9fs_string_free(&name);
308760ce86c7SWei Liu     pdu_complete(pdu, err);
308860ce86c7SWei Liu }
308960ce86c7SWei Liu 
309060ce86c7SWei Liu /* Only works with path name based fid */
v9fs_remove(void * opaque)30918440e22eSGreg Kurz static void coroutine_fn v9fs_remove(void *opaque)
309260ce86c7SWei Liu {
309360ce86c7SWei Liu     int32_t fid;
309460ce86c7SWei Liu     int err = 0;
309560ce86c7SWei Liu     size_t offset = 7;
309660ce86c7SWei Liu     V9fsFidState *fidp;
309760ce86c7SWei Liu     V9fsPDU *pdu = opaque;
309860ce86c7SWei Liu 
309960ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "d", &fid);
310060ce86c7SWei Liu     if (err < 0) {
310160ce86c7SWei Liu         goto out_nofid;
310260ce86c7SWei Liu     }
310360ce86c7SWei Liu     trace_v9fs_remove(pdu->tag, pdu->id, fid);
310460ce86c7SWei Liu 
310560ce86c7SWei Liu     fidp = get_fid(pdu, fid);
310660ce86c7SWei Liu     if (fidp == NULL) {
310760ce86c7SWei Liu         err = -EINVAL;
310860ce86c7SWei Liu         goto out_nofid;
310960ce86c7SWei Liu     }
311060ce86c7SWei Liu     /* if fs driver is not path based, return EOPNOTSUPP */
311160ce86c7SWei Liu     if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
311260ce86c7SWei Liu         err = -EOPNOTSUPP;
311360ce86c7SWei Liu         goto out_err;
311460ce86c7SWei Liu     }
311560ce86c7SWei Liu     /*
311660ce86c7SWei Liu      * IF the file is unlinked, we cannot reopen
311760ce86c7SWei Liu      * the file later. So don't reclaim fd
311860ce86c7SWei Liu      */
311960ce86c7SWei Liu     err = v9fs_mark_fids_unreclaim(pdu, &fidp->path);
312060ce86c7SWei Liu     if (err < 0) {
312160ce86c7SWei Liu         goto out_err;
312260ce86c7SWei Liu     }
312360ce86c7SWei Liu     err = v9fs_co_remove(pdu, &fidp->path);
312460ce86c7SWei Liu     if (!err) {
312560ce86c7SWei Liu         err = offset;
312660ce86c7SWei Liu     }
312760ce86c7SWei Liu out_err:
312860ce86c7SWei Liu     /* For TREMOVE we need to clunk the fid even on failed remove */
312960ce86c7SWei Liu     clunk_fid(pdu->s, fidp->fid);
313060ce86c7SWei Liu     put_fid(pdu, fidp);
313160ce86c7SWei Liu out_nofid:
313260ce86c7SWei Liu     pdu_complete(pdu, err);
313360ce86c7SWei Liu }
313460ce86c7SWei Liu 
v9fs_unlinkat(void * opaque)31358440e22eSGreg Kurz static void coroutine_fn v9fs_unlinkat(void *opaque)
313660ce86c7SWei Liu {
313760ce86c7SWei Liu     int err = 0;
313860ce86c7SWei Liu     V9fsString name;
313967e87345SKeno Fischer     int32_t dfid, flags, rflags = 0;
314060ce86c7SWei Liu     size_t offset = 7;
314160ce86c7SWei Liu     V9fsPath path;
314260ce86c7SWei Liu     V9fsFidState *dfidp;
314360ce86c7SWei Liu     V9fsPDU *pdu = opaque;
314460ce86c7SWei Liu 
314560ce86c7SWei Liu     v9fs_string_init(&name);
314660ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags);
314760ce86c7SWei Liu     if (err < 0) {
314860ce86c7SWei Liu         goto out_nofid;
314960ce86c7SWei Liu     }
3150fff39a7aSGreg Kurz 
3151fff39a7aSGreg Kurz     if (name_is_illegal(name.data)) {
3152fff39a7aSGreg Kurz         err = -ENOENT;
3153fff39a7aSGreg Kurz         goto out_nofid;
3154fff39a7aSGreg Kurz     }
3155fff39a7aSGreg Kurz 
3156805b5d98SGreg Kurz     if (!strcmp(".", name.data)) {
3157805b5d98SGreg Kurz         err = -EINVAL;
3158805b5d98SGreg Kurz         goto out_nofid;
3159805b5d98SGreg Kurz     }
3160805b5d98SGreg Kurz 
3161805b5d98SGreg Kurz     if (!strcmp("..", name.data)) {
3162805b5d98SGreg Kurz         err = -ENOTEMPTY;
3163805b5d98SGreg Kurz         goto out_nofid;
3164805b5d98SGreg Kurz     }
3165805b5d98SGreg Kurz 
316667e87345SKeno Fischer     if (flags & ~P9_DOTL_AT_REMOVEDIR) {
316767e87345SKeno Fischer         err = -EINVAL;
316867e87345SKeno Fischer         goto out_nofid;
316967e87345SKeno Fischer     }
317067e87345SKeno Fischer 
317167e87345SKeno Fischer     if (flags & P9_DOTL_AT_REMOVEDIR) {
317267e87345SKeno Fischer         rflags |= AT_REMOVEDIR;
317367e87345SKeno Fischer     }
317467e87345SKeno Fischer 
317560ce86c7SWei Liu     dfidp = get_fid(pdu, dfid);
317660ce86c7SWei Liu     if (dfidp == NULL) {
317760ce86c7SWei Liu         err = -EINVAL;
317860ce86c7SWei Liu         goto out_nofid;
317960ce86c7SWei Liu     }
318060ce86c7SWei Liu     /*
318160ce86c7SWei Liu      * IF the file is unlinked, we cannot reopen
318260ce86c7SWei Liu      * the file later. So don't reclaim fd
318360ce86c7SWei Liu      */
318460ce86c7SWei Liu     v9fs_path_init(&path);
318560ce86c7SWei Liu     err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path);
318660ce86c7SWei Liu     if (err < 0) {
318760ce86c7SWei Liu         goto out_err;
318860ce86c7SWei Liu     }
318960ce86c7SWei Liu     err = v9fs_mark_fids_unreclaim(pdu, &path);
319060ce86c7SWei Liu     if (err < 0) {
319160ce86c7SWei Liu         goto out_err;
319260ce86c7SWei Liu     }
319367e87345SKeno Fischer     err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, rflags);
319460ce86c7SWei Liu     if (!err) {
319560ce86c7SWei Liu         err = offset;
319660ce86c7SWei Liu     }
319760ce86c7SWei Liu out_err:
319860ce86c7SWei Liu     put_fid(pdu, dfidp);
319960ce86c7SWei Liu     v9fs_path_free(&path);
320060ce86c7SWei Liu out_nofid:
320160ce86c7SWei Liu     pdu_complete(pdu, err);
320260ce86c7SWei Liu     v9fs_string_free(&name);
320360ce86c7SWei Liu }
320460ce86c7SWei Liu 
320560ce86c7SWei Liu 
320660ce86c7SWei Liu /* Only works with path name based fid */
v9fs_complete_rename(V9fsPDU * pdu,V9fsFidState * fidp,int32_t newdirfid,V9fsString * name)32078440e22eSGreg Kurz static int coroutine_fn v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp,
32088440e22eSGreg Kurz                                              int32_t newdirfid,
32098440e22eSGreg Kurz                                              V9fsString *name)
321060ce86c7SWei Liu {
321160ce86c7SWei Liu     int err = 0;
321260ce86c7SWei Liu     V9fsPath new_path;
321360ce86c7SWei Liu     V9fsFidState *tfidp;
321460ce86c7SWei Liu     V9fsState *s = pdu->s;
321560ce86c7SWei Liu     V9fsFidState *dirfidp = NULL;
3216f5265c8fSLinus Heckemann     GHashTableIter iter;
3217f5265c8fSLinus Heckemann     gpointer fid;
321860ce86c7SWei Liu 
321960ce86c7SWei Liu     v9fs_path_init(&new_path);
322060ce86c7SWei Liu     if (newdirfid != -1) {
322160ce86c7SWei Liu         dirfidp = get_fid(pdu, newdirfid);
322260ce86c7SWei Liu         if (dirfidp == NULL) {
3223b858e80aSDaniel Henrique Barboza             return -ENOENT;
322460ce86c7SWei Liu         }
322549dd946bSGreg Kurz         if (fidp->fid_type != P9_FID_NONE) {
322649dd946bSGreg Kurz             err = -EINVAL;
322749dd946bSGreg Kurz             goto out;
322849dd946bSGreg Kurz         }
32294fa62005SGreg Kurz         err = v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path);
32304fa62005SGreg Kurz         if (err < 0) {
32314fa62005SGreg Kurz             goto out;
32324fa62005SGreg Kurz         }
323360ce86c7SWei Liu     } else {
32344d8bc733SJan Dakinevich         char *dir_name = g_path_get_dirname(fidp->path.data);
32354d8bc733SJan Dakinevich         V9fsPath dir_path;
32364d8bc733SJan Dakinevich 
32374d8bc733SJan Dakinevich         v9fs_path_init(&dir_path);
32384d8bc733SJan Dakinevich         v9fs_path_sprintf(&dir_path, "%s", dir_name);
32394d8bc733SJan Dakinevich         g_free(dir_name);
32404d8bc733SJan Dakinevich 
32414d8bc733SJan Dakinevich         err = v9fs_co_name_to_path(pdu, &dir_path, name->data, &new_path);
32424d8bc733SJan Dakinevich         v9fs_path_free(&dir_path);
32434fa62005SGreg Kurz         if (err < 0) {
32444fa62005SGreg Kurz             goto out;
32454fa62005SGreg Kurz         }
324660ce86c7SWei Liu     }
324760ce86c7SWei Liu     err = v9fs_co_rename(pdu, &fidp->path, &new_path);
324860ce86c7SWei Liu     if (err < 0) {
324960ce86c7SWei Liu         goto out;
325060ce86c7SWei Liu     }
3251f5265c8fSLinus Heckemann 
325260ce86c7SWei Liu     /*
325360ce86c7SWei Liu      * Fixup fid's pointing to the old name to
325460ce86c7SWei Liu      * start pointing to the new name
325560ce86c7SWei Liu      */
3256f5265c8fSLinus Heckemann     g_hash_table_iter_init(&iter, s->fids);
3257f5265c8fSLinus Heckemann     while (g_hash_table_iter_next(&iter, &fid, (gpointer *) &tfidp)) {
325860ce86c7SWei Liu         if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) {
325960ce86c7SWei Liu             /* replace the name */
326060ce86c7SWei Liu             v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data));
326160ce86c7SWei Liu         }
326260ce86c7SWei Liu     }
326360ce86c7SWei Liu out:
326460ce86c7SWei Liu     if (dirfidp) {
326560ce86c7SWei Liu         put_fid(pdu, dirfidp);
326660ce86c7SWei Liu     }
326760ce86c7SWei Liu     v9fs_path_free(&new_path);
326860ce86c7SWei Liu     return err;
326960ce86c7SWei Liu }
327060ce86c7SWei Liu 
327160ce86c7SWei Liu /* Only works with path name based fid */
v9fs_rename(void * opaque)32728440e22eSGreg Kurz static void coroutine_fn v9fs_rename(void *opaque)
327360ce86c7SWei Liu {
327460ce86c7SWei Liu     int32_t fid;
327560ce86c7SWei Liu     ssize_t err = 0;
327660ce86c7SWei Liu     size_t offset = 7;
327760ce86c7SWei Liu     V9fsString name;
327860ce86c7SWei Liu     int32_t newdirfid;
327960ce86c7SWei Liu     V9fsFidState *fidp;
328060ce86c7SWei Liu     V9fsPDU *pdu = opaque;
328160ce86c7SWei Liu     V9fsState *s = pdu->s;
328260ce86c7SWei Liu 
328360ce86c7SWei Liu     v9fs_string_init(&name);
328460ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name);
328560ce86c7SWei Liu     if (err < 0) {
328660ce86c7SWei Liu         goto out_nofid;
328760ce86c7SWei Liu     }
3288fff39a7aSGreg Kurz 
3289fff39a7aSGreg Kurz     if (name_is_illegal(name.data)) {
3290fff39a7aSGreg Kurz         err = -ENOENT;
3291fff39a7aSGreg Kurz         goto out_nofid;
3292fff39a7aSGreg Kurz     }
3293fff39a7aSGreg Kurz 
3294805b5d98SGreg Kurz     if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3295805b5d98SGreg Kurz         err = -EISDIR;
3296805b5d98SGreg Kurz         goto out_nofid;
3297805b5d98SGreg Kurz     }
3298805b5d98SGreg Kurz 
329960ce86c7SWei Liu     fidp = get_fid(pdu, fid);
330060ce86c7SWei Liu     if (fidp == NULL) {
330160ce86c7SWei Liu         err = -ENOENT;
330260ce86c7SWei Liu         goto out_nofid;
330360ce86c7SWei Liu     }
330449dd946bSGreg Kurz     if (fidp->fid_type != P9_FID_NONE) {
330549dd946bSGreg Kurz         err = -EINVAL;
330649dd946bSGreg Kurz         goto out;
330749dd946bSGreg Kurz     }
330860ce86c7SWei Liu     /* if fs driver is not path based, return EOPNOTSUPP */
330960ce86c7SWei Liu     if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
331060ce86c7SWei Liu         err = -EOPNOTSUPP;
331160ce86c7SWei Liu         goto out;
331260ce86c7SWei Liu     }
331360ce86c7SWei Liu     v9fs_path_write_lock(s);
331460ce86c7SWei Liu     err = v9fs_complete_rename(pdu, fidp, newdirfid, &name);
331560ce86c7SWei Liu     v9fs_path_unlock(s);
331660ce86c7SWei Liu     if (!err) {
331760ce86c7SWei Liu         err = offset;
331860ce86c7SWei Liu     }
331960ce86c7SWei Liu out:
332060ce86c7SWei Liu     put_fid(pdu, fidp);
332160ce86c7SWei Liu out_nofid:
332260ce86c7SWei Liu     pdu_complete(pdu, err);
332360ce86c7SWei Liu     v9fs_string_free(&name);
332460ce86c7SWei Liu }
332560ce86c7SWei Liu 
v9fs_fix_fid_paths(V9fsPDU * pdu,V9fsPath * olddir,V9fsString * old_name,V9fsPath * newdir,V9fsString * new_name)33264fa62005SGreg Kurz static int coroutine_fn v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir,
33278440e22eSGreg Kurz                                            V9fsString *old_name,
33288440e22eSGreg Kurz                                            V9fsPath *newdir,
332960ce86c7SWei Liu                                            V9fsString *new_name)
333060ce86c7SWei Liu {
333160ce86c7SWei Liu     V9fsFidState *tfidp;
333260ce86c7SWei Liu     V9fsPath oldpath, newpath;
333360ce86c7SWei Liu     V9fsState *s = pdu->s;
33344fa62005SGreg Kurz     int err;
3335f5265c8fSLinus Heckemann     GHashTableIter iter;
3336f5265c8fSLinus Heckemann     gpointer fid;
333760ce86c7SWei Liu 
333860ce86c7SWei Liu     v9fs_path_init(&oldpath);
333960ce86c7SWei Liu     v9fs_path_init(&newpath);
33404fa62005SGreg Kurz     err = v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath);
33414fa62005SGreg Kurz     if (err < 0) {
33424fa62005SGreg Kurz         goto out;
33434fa62005SGreg Kurz     }
33444fa62005SGreg Kurz     err = v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath);
33454fa62005SGreg Kurz     if (err < 0) {
33464fa62005SGreg Kurz         goto out;
33474fa62005SGreg Kurz     }
334860ce86c7SWei Liu 
334960ce86c7SWei Liu     /*
335060ce86c7SWei Liu      * Fixup fid's pointing to the old name to
335160ce86c7SWei Liu      * start pointing to the new name
335260ce86c7SWei Liu      */
3353f5265c8fSLinus Heckemann     g_hash_table_iter_init(&iter, s->fids);
3354f5265c8fSLinus Heckemann     while (g_hash_table_iter_next(&iter, &fid, (gpointer *) &tfidp)) {
335560ce86c7SWei Liu         if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) {
335660ce86c7SWei Liu             /* replace the name */
335760ce86c7SWei Liu             v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data));
335860ce86c7SWei Liu         }
335960ce86c7SWei Liu     }
33604fa62005SGreg Kurz out:
336160ce86c7SWei Liu     v9fs_path_free(&oldpath);
336260ce86c7SWei Liu     v9fs_path_free(&newpath);
33634fa62005SGreg Kurz     return err;
336460ce86c7SWei Liu }
336560ce86c7SWei Liu 
v9fs_complete_renameat(V9fsPDU * pdu,int32_t olddirfid,V9fsString * old_name,int32_t newdirfid,V9fsString * new_name)33668440e22eSGreg Kurz static int coroutine_fn v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid,
33678440e22eSGreg Kurz                                                V9fsString *old_name,
33688440e22eSGreg Kurz                                                int32_t newdirfid,
336960ce86c7SWei Liu                                                V9fsString *new_name)
337060ce86c7SWei Liu {
337160ce86c7SWei Liu     int err = 0;
337260ce86c7SWei Liu     V9fsState *s = pdu->s;
337360ce86c7SWei Liu     V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL;
337460ce86c7SWei Liu 
337560ce86c7SWei Liu     olddirfidp = get_fid(pdu, olddirfid);
337660ce86c7SWei Liu     if (olddirfidp == NULL) {
337760ce86c7SWei Liu         err = -ENOENT;
337860ce86c7SWei Liu         goto out;
337960ce86c7SWei Liu     }
338060ce86c7SWei Liu     if (newdirfid != -1) {
338160ce86c7SWei Liu         newdirfidp = get_fid(pdu, newdirfid);
338260ce86c7SWei Liu         if (newdirfidp == NULL) {
338360ce86c7SWei Liu             err = -ENOENT;
338460ce86c7SWei Liu             goto out;
338560ce86c7SWei Liu         }
338660ce86c7SWei Liu     } else {
338760ce86c7SWei Liu         newdirfidp = get_fid(pdu, olddirfid);
338860ce86c7SWei Liu     }
338960ce86c7SWei Liu 
339060ce86c7SWei Liu     err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name,
339160ce86c7SWei Liu                            &newdirfidp->path, new_name);
339260ce86c7SWei Liu     if (err < 0) {
339360ce86c7SWei Liu         goto out;
339460ce86c7SWei Liu     }
339560ce86c7SWei Liu     if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
339660ce86c7SWei Liu         /* Only for path based fid  we need to do the below fixup */
33974fa62005SGreg Kurz         err = v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name,
339860ce86c7SWei Liu                                  &newdirfidp->path, new_name);
339960ce86c7SWei Liu     }
340060ce86c7SWei Liu out:
340160ce86c7SWei Liu     if (olddirfidp) {
340260ce86c7SWei Liu         put_fid(pdu, olddirfidp);
340360ce86c7SWei Liu     }
340460ce86c7SWei Liu     if (newdirfidp) {
340560ce86c7SWei Liu         put_fid(pdu, newdirfidp);
340660ce86c7SWei Liu     }
340760ce86c7SWei Liu     return err;
340860ce86c7SWei Liu }
340960ce86c7SWei Liu 
v9fs_renameat(void * opaque)34108440e22eSGreg Kurz static void coroutine_fn v9fs_renameat(void *opaque)
341160ce86c7SWei Liu {
341260ce86c7SWei Liu     ssize_t err = 0;
341360ce86c7SWei Liu     size_t offset = 7;
341460ce86c7SWei Liu     V9fsPDU *pdu = opaque;
341560ce86c7SWei Liu     V9fsState *s = pdu->s;
341660ce86c7SWei Liu     int32_t olddirfid, newdirfid;
341760ce86c7SWei Liu     V9fsString old_name, new_name;
341860ce86c7SWei Liu 
341960ce86c7SWei Liu     v9fs_string_init(&old_name);
342060ce86c7SWei Liu     v9fs_string_init(&new_name);
342160ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid,
342260ce86c7SWei Liu                         &old_name, &newdirfid, &new_name);
342360ce86c7SWei Liu     if (err < 0) {
342460ce86c7SWei Liu         goto out_err;
342560ce86c7SWei Liu     }
342660ce86c7SWei Liu 
3427fff39a7aSGreg Kurz     if (name_is_illegal(old_name.data) || name_is_illegal(new_name.data)) {
3428fff39a7aSGreg Kurz         err = -ENOENT;
3429fff39a7aSGreg Kurz         goto out_err;
3430fff39a7aSGreg Kurz     }
3431fff39a7aSGreg Kurz 
3432805b5d98SGreg Kurz     if (!strcmp(".", old_name.data) || !strcmp("..", old_name.data) ||
3433805b5d98SGreg Kurz         !strcmp(".", new_name.data) || !strcmp("..", new_name.data)) {
3434805b5d98SGreg Kurz         err = -EISDIR;
3435805b5d98SGreg Kurz         goto out_err;
3436805b5d98SGreg Kurz     }
3437805b5d98SGreg Kurz 
343860ce86c7SWei Liu     v9fs_path_write_lock(s);
343960ce86c7SWei Liu     err = v9fs_complete_renameat(pdu, olddirfid,
344060ce86c7SWei Liu                                  &old_name, newdirfid, &new_name);
344160ce86c7SWei Liu     v9fs_path_unlock(s);
344260ce86c7SWei Liu     if (!err) {
344360ce86c7SWei Liu         err = offset;
344460ce86c7SWei Liu     }
344560ce86c7SWei Liu 
344660ce86c7SWei Liu out_err:
344760ce86c7SWei Liu     pdu_complete(pdu, err);
344860ce86c7SWei Liu     v9fs_string_free(&old_name);
344960ce86c7SWei Liu     v9fs_string_free(&new_name);
345060ce86c7SWei Liu }
345160ce86c7SWei Liu 
v9fs_wstat(void * opaque)34528440e22eSGreg Kurz static void coroutine_fn v9fs_wstat(void *opaque)
345360ce86c7SWei Liu {
345460ce86c7SWei Liu     int32_t fid;
345560ce86c7SWei Liu     int err = 0;
345660ce86c7SWei Liu     int16_t unused;
345760ce86c7SWei Liu     V9fsStat v9stat;
345860ce86c7SWei Liu     size_t offset = 7;
345960ce86c7SWei Liu     struct stat stbuf;
346060ce86c7SWei Liu     V9fsFidState *fidp;
346160ce86c7SWei Liu     V9fsPDU *pdu = opaque;
34621d203986SGreg Kurz     V9fsState *s = pdu->s;
346360ce86c7SWei Liu 
346460ce86c7SWei Liu     v9fs_stat_init(&v9stat);
346560ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat);
346660ce86c7SWei Liu     if (err < 0) {
346760ce86c7SWei Liu         goto out_nofid;
346860ce86c7SWei Liu     }
346960ce86c7SWei Liu     trace_v9fs_wstat(pdu->tag, pdu->id, fid,
347060ce86c7SWei Liu                      v9stat.mode, v9stat.atime, v9stat.mtime);
347160ce86c7SWei Liu 
347260ce86c7SWei Liu     fidp = get_fid(pdu, fid);
347360ce86c7SWei Liu     if (fidp == NULL) {
347460ce86c7SWei Liu         err = -EINVAL;
347560ce86c7SWei Liu         goto out_nofid;
347660ce86c7SWei Liu     }
347760ce86c7SWei Liu     /* do we need to sync the file? */
347860ce86c7SWei Liu     if (donttouch_stat(&v9stat)) {
347960ce86c7SWei Liu         err = v9fs_co_fsync(pdu, fidp, 0);
348060ce86c7SWei Liu         goto out;
348160ce86c7SWei Liu     }
348260ce86c7SWei Liu     if (v9stat.mode != -1) {
348360ce86c7SWei Liu         uint32_t v9_mode;
348460ce86c7SWei Liu         err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
348560ce86c7SWei Liu         if (err < 0) {
348660ce86c7SWei Liu             goto out;
348760ce86c7SWei Liu         }
348860ce86c7SWei Liu         v9_mode = stat_to_v9mode(&stbuf);
348960ce86c7SWei Liu         if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
349060ce86c7SWei Liu             (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
349160ce86c7SWei Liu             /* Attempting to change the type */
349260ce86c7SWei Liu             err = -EIO;
349360ce86c7SWei Liu             goto out;
349460ce86c7SWei Liu         }
349560ce86c7SWei Liu         err = v9fs_co_chmod(pdu, &fidp->path,
349660ce86c7SWei Liu                             v9mode_to_mode(v9stat.mode,
349760ce86c7SWei Liu                                            &v9stat.extension));
349860ce86c7SWei Liu         if (err < 0) {
349960ce86c7SWei Liu             goto out;
350060ce86c7SWei Liu         }
350160ce86c7SWei Liu     }
350260ce86c7SWei Liu     if (v9stat.mtime != -1 || v9stat.atime != -1) {
350360ce86c7SWei Liu         struct timespec times[2];
350460ce86c7SWei Liu         if (v9stat.atime != -1) {
350560ce86c7SWei Liu             times[0].tv_sec = v9stat.atime;
350660ce86c7SWei Liu             times[0].tv_nsec = 0;
350760ce86c7SWei Liu         } else {
350860ce86c7SWei Liu             times[0].tv_nsec = UTIME_OMIT;
350960ce86c7SWei Liu         }
351060ce86c7SWei Liu         if (v9stat.mtime != -1) {
351160ce86c7SWei Liu             times[1].tv_sec = v9stat.mtime;
351260ce86c7SWei Liu             times[1].tv_nsec = 0;
351360ce86c7SWei Liu         } else {
351460ce86c7SWei Liu             times[1].tv_nsec = UTIME_OMIT;
351560ce86c7SWei Liu         }
351660ce86c7SWei Liu         err = v9fs_co_utimensat(pdu, &fidp->path, times);
351760ce86c7SWei Liu         if (err < 0) {
351860ce86c7SWei Liu             goto out;
351960ce86c7SWei Liu         }
352060ce86c7SWei Liu     }
352160ce86c7SWei Liu     if (v9stat.n_gid != -1 || v9stat.n_uid != -1) {
352260ce86c7SWei Liu         err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid);
352360ce86c7SWei Liu         if (err < 0) {
352460ce86c7SWei Liu             goto out;
352560ce86c7SWei Liu         }
352660ce86c7SWei Liu     }
352760ce86c7SWei Liu     if (v9stat.name.size != 0) {
35281d203986SGreg Kurz         v9fs_path_write_lock(s);
352960ce86c7SWei Liu         err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name);
35301d203986SGreg Kurz         v9fs_path_unlock(s);
353160ce86c7SWei Liu         if (err < 0) {
353260ce86c7SWei Liu             goto out;
353360ce86c7SWei Liu         }
353460ce86c7SWei Liu     }
353560ce86c7SWei Liu     if (v9stat.length != -1) {
353660ce86c7SWei Liu         err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length);
353760ce86c7SWei Liu         if (err < 0) {
353860ce86c7SWei Liu             goto out;
353960ce86c7SWei Liu         }
354060ce86c7SWei Liu     }
354160ce86c7SWei Liu     err = offset;
354260ce86c7SWei Liu out:
354360ce86c7SWei Liu     put_fid(pdu, fidp);
354460ce86c7SWei Liu out_nofid:
354560ce86c7SWei Liu     v9fs_stat_free(&v9stat);
354660ce86c7SWei Liu     pdu_complete(pdu, err);
354760ce86c7SWei Liu }
354860ce86c7SWei Liu 
v9fs_fill_statfs(V9fsState * s,V9fsPDU * pdu,struct statfs * stbuf)354960ce86c7SWei Liu static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf)
355060ce86c7SWei Liu {
355160ce86c7SWei Liu     uint32_t f_type;
355260ce86c7SWei Liu     uint32_t f_bsize;
355360ce86c7SWei Liu     uint64_t f_blocks;
355460ce86c7SWei Liu     uint64_t f_bfree;
355560ce86c7SWei Liu     uint64_t f_bavail;
355660ce86c7SWei Liu     uint64_t f_files;
355760ce86c7SWei Liu     uint64_t f_ffree;
355860ce86c7SWei Liu     uint64_t fsid_val;
355960ce86c7SWei Liu     uint32_t f_namelen;
356060ce86c7SWei Liu     size_t offset = 7;
356160ce86c7SWei Liu     int32_t bsize_factor;
356260ce86c7SWei Liu 
356360ce86c7SWei Liu     /*
356460ce86c7SWei Liu      * compute bsize factor based on host file system block size
356560ce86c7SWei Liu      * and client msize
356660ce86c7SWei Liu      */
356760ce86c7SWei Liu     bsize_factor = (s->msize - P9_IOHDRSZ) / stbuf->f_bsize;
356860ce86c7SWei Liu     if (!bsize_factor) {
356960ce86c7SWei Liu         bsize_factor = 1;
357060ce86c7SWei Liu     }
357160ce86c7SWei Liu     f_type  = stbuf->f_type;
357260ce86c7SWei Liu     f_bsize = stbuf->f_bsize;
357360ce86c7SWei Liu     f_bsize *= bsize_factor;
357460ce86c7SWei Liu     /*
357560ce86c7SWei Liu      * f_bsize is adjusted(multiplied) by bsize factor, so we need to
357660ce86c7SWei Liu      * adjust(divide) the number of blocks, free blocks and available
357760ce86c7SWei Liu      * blocks by bsize factor
357860ce86c7SWei Liu      */
357960ce86c7SWei Liu     f_blocks = stbuf->f_blocks / bsize_factor;
358060ce86c7SWei Liu     f_bfree  = stbuf->f_bfree / bsize_factor;
358160ce86c7SWei Liu     f_bavail = stbuf->f_bavail / bsize_factor;
358260ce86c7SWei Liu     f_files  = stbuf->f_files;
358360ce86c7SWei Liu     f_ffree  = stbuf->f_ffree;
3584f41db099SKeno Fischer #ifdef CONFIG_DARWIN
3585f41db099SKeno Fischer     fsid_val = (unsigned int)stbuf->f_fsid.val[0] |
3586f41db099SKeno Fischer                (unsigned long long)stbuf->f_fsid.val[1] << 32;
3587f41db099SKeno Fischer     f_namelen = NAME_MAX;
3588f41db099SKeno Fischer #else
358960ce86c7SWei Liu     fsid_val = (unsigned int) stbuf->f_fsid.__val[0] |
359060ce86c7SWei Liu                (unsigned long long)stbuf->f_fsid.__val[1] << 32;
359160ce86c7SWei Liu     f_namelen = stbuf->f_namelen;
3592f41db099SKeno Fischer #endif
359360ce86c7SWei Liu 
359460ce86c7SWei Liu     return pdu_marshal(pdu, offset, "ddqqqqqqd",
359560ce86c7SWei Liu                        f_type, f_bsize, f_blocks, f_bfree,
359660ce86c7SWei Liu                        f_bavail, f_files, f_ffree,
359760ce86c7SWei Liu                        fsid_val, f_namelen);
359860ce86c7SWei Liu }
359960ce86c7SWei Liu 
v9fs_statfs(void * opaque)36008440e22eSGreg Kurz static void coroutine_fn v9fs_statfs(void *opaque)
360160ce86c7SWei Liu {
360260ce86c7SWei Liu     int32_t fid;
360360ce86c7SWei Liu     ssize_t retval = 0;
360460ce86c7SWei Liu     size_t offset = 7;
360560ce86c7SWei Liu     V9fsFidState *fidp;
360660ce86c7SWei Liu     struct statfs stbuf;
360760ce86c7SWei Liu     V9fsPDU *pdu = opaque;
360860ce86c7SWei Liu     V9fsState *s = pdu->s;
360960ce86c7SWei Liu 
361060ce86c7SWei Liu     retval = pdu_unmarshal(pdu, offset, "d", &fid);
361160ce86c7SWei Liu     if (retval < 0) {
361260ce86c7SWei Liu         goto out_nofid;
361360ce86c7SWei Liu     }
361460ce86c7SWei Liu     fidp = get_fid(pdu, fid);
361560ce86c7SWei Liu     if (fidp == NULL) {
361660ce86c7SWei Liu         retval = -ENOENT;
361760ce86c7SWei Liu         goto out_nofid;
361860ce86c7SWei Liu     }
361960ce86c7SWei Liu     retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf);
362060ce86c7SWei Liu     if (retval < 0) {
362160ce86c7SWei Liu         goto out;
362260ce86c7SWei Liu     }
362360ce86c7SWei Liu     retval = v9fs_fill_statfs(s, pdu, &stbuf);
362460ce86c7SWei Liu     if (retval < 0) {
362560ce86c7SWei Liu         goto out;
362660ce86c7SWei Liu     }
362760ce86c7SWei Liu     retval += offset;
362860ce86c7SWei Liu out:
362960ce86c7SWei Liu     put_fid(pdu, fidp);
363060ce86c7SWei Liu out_nofid:
363160ce86c7SWei Liu     pdu_complete(pdu, retval);
363260ce86c7SWei Liu }
363360ce86c7SWei Liu 
v9fs_mknod(void * opaque)36348440e22eSGreg Kurz static void coroutine_fn v9fs_mknod(void *opaque)
363560ce86c7SWei Liu {
363660ce86c7SWei Liu 
363760ce86c7SWei Liu     int mode;
363860ce86c7SWei Liu     gid_t gid;
363960ce86c7SWei Liu     int32_t fid;
364060ce86c7SWei Liu     V9fsQID qid;
364160ce86c7SWei Liu     int err = 0;
364260ce86c7SWei Liu     int major, minor;
364360ce86c7SWei Liu     size_t offset = 7;
364460ce86c7SWei Liu     V9fsString name;
364560ce86c7SWei Liu     struct stat stbuf;
364660ce86c7SWei Liu     V9fsFidState *fidp;
364760ce86c7SWei Liu     V9fsPDU *pdu = opaque;
364860ce86c7SWei Liu 
364960ce86c7SWei Liu     v9fs_string_init(&name);
365060ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode,
365160ce86c7SWei Liu                         &major, &minor, &gid);
365260ce86c7SWei Liu     if (err < 0) {
365360ce86c7SWei Liu         goto out_nofid;
365460ce86c7SWei Liu     }
365560ce86c7SWei Liu     trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor);
365660ce86c7SWei Liu 
3657fff39a7aSGreg Kurz     if (name_is_illegal(name.data)) {
3658fff39a7aSGreg Kurz         err = -ENOENT;
3659fff39a7aSGreg Kurz         goto out_nofid;
3660fff39a7aSGreg Kurz     }
3661fff39a7aSGreg Kurz 
3662805b5d98SGreg Kurz     if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3663805b5d98SGreg Kurz         err = -EEXIST;
3664805b5d98SGreg Kurz         goto out_nofid;
3665805b5d98SGreg Kurz     }
3666805b5d98SGreg Kurz 
366760ce86c7SWei Liu     fidp = get_fid(pdu, fid);
366860ce86c7SWei Liu     if (fidp == NULL) {
366960ce86c7SWei Liu         err = -ENOENT;
367060ce86c7SWei Liu         goto out_nofid;
367160ce86c7SWei Liu     }
367260ce86c7SWei Liu     err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid,
367360ce86c7SWei Liu                         makedev(major, minor), mode, &stbuf);
367460ce86c7SWei Liu     if (err < 0) {
367560ce86c7SWei Liu         goto out;
367660ce86c7SWei Liu     }
36773b5ee9e8SAntonios Motakis     err = stat_to_qid(pdu, &stbuf, &qid);
36783b5ee9e8SAntonios Motakis     if (err < 0) {
36793b5ee9e8SAntonios Motakis         goto out;
36803b5ee9e8SAntonios Motakis     }
368160ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "Q", &qid);
368260ce86c7SWei Liu     if (err < 0) {
368360ce86c7SWei Liu         goto out;
368460ce86c7SWei Liu     }
368560ce86c7SWei Liu     err += offset;
368660ce86c7SWei Liu     trace_v9fs_mknod_return(pdu->tag, pdu->id,
368760ce86c7SWei Liu                             qid.type, qid.version, qid.path);
368860ce86c7SWei Liu out:
368960ce86c7SWei Liu     put_fid(pdu, fidp);
369060ce86c7SWei Liu out_nofid:
369160ce86c7SWei Liu     pdu_complete(pdu, err);
369260ce86c7SWei Liu     v9fs_string_free(&name);
369360ce86c7SWei Liu }
369460ce86c7SWei Liu 
369560ce86c7SWei Liu /*
369660ce86c7SWei Liu  * Implement posix byte range locking code
369760ce86c7SWei Liu  * Server side handling of locking code is very simple, because 9p server in
369860ce86c7SWei Liu  * QEMU can handle only one client. And most of the lock handling
369960ce86c7SWei Liu  * (like conflict, merging) etc is done by the VFS layer itself, so no need to
370060ce86c7SWei Liu  * do any thing in * qemu 9p server side lock code path.
370160ce86c7SWei Liu  * So when a TLOCK request comes, always return success
370260ce86c7SWei Liu  */
v9fs_lock(void * opaque)37038440e22eSGreg Kurz static void coroutine_fn v9fs_lock(void *opaque)
370460ce86c7SWei Liu {
370560ce86c7SWei Liu     V9fsFlock flock;
370660ce86c7SWei Liu     size_t offset = 7;
370760ce86c7SWei Liu     struct stat stbuf;
370860ce86c7SWei Liu     V9fsFidState *fidp;
370960ce86c7SWei Liu     int32_t fid, err = 0;
371060ce86c7SWei Liu     V9fsPDU *pdu = opaque;
371160ce86c7SWei Liu 
371260ce86c7SWei Liu     v9fs_string_init(&flock.client_id);
371360ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type,
371460ce86c7SWei Liu                         &flock.flags, &flock.start, &flock.length,
371560ce86c7SWei Liu                         &flock.proc_id, &flock.client_id);
371660ce86c7SWei Liu     if (err < 0) {
371760ce86c7SWei Liu         goto out_nofid;
371860ce86c7SWei Liu     }
371960ce86c7SWei Liu     trace_v9fs_lock(pdu->tag, pdu->id, fid,
372060ce86c7SWei Liu                     flock.type, flock.start, flock.length);
372160ce86c7SWei Liu 
372260ce86c7SWei Liu 
372360ce86c7SWei Liu     /* We support only block flag now (that too ignored currently) */
372460ce86c7SWei Liu     if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) {
372560ce86c7SWei Liu         err = -EINVAL;
372660ce86c7SWei Liu         goto out_nofid;
372760ce86c7SWei Liu     }
372860ce86c7SWei Liu     fidp = get_fid(pdu, fid);
372960ce86c7SWei Liu     if (fidp == NULL) {
373060ce86c7SWei Liu         err = -ENOENT;
373160ce86c7SWei Liu         goto out_nofid;
373260ce86c7SWei Liu     }
373360ce86c7SWei Liu     err = v9fs_co_fstat(pdu, fidp, &stbuf);
373460ce86c7SWei Liu     if (err < 0) {
373560ce86c7SWei Liu         goto out;
373660ce86c7SWei Liu     }
37374bae2b39SPaolo Bonzini     err = pdu_marshal(pdu, offset, "b", P9_LOCK_SUCCESS);
37384bae2b39SPaolo Bonzini     if (err < 0) {
37394bae2b39SPaolo Bonzini         goto out;
37404bae2b39SPaolo Bonzini     }
37414bae2b39SPaolo Bonzini     err += offset;
37424bae2b39SPaolo Bonzini     trace_v9fs_lock_return(pdu->tag, pdu->id, P9_LOCK_SUCCESS);
374360ce86c7SWei Liu out:
374460ce86c7SWei Liu     put_fid(pdu, fidp);
374560ce86c7SWei Liu out_nofid:
374660ce86c7SWei Liu     pdu_complete(pdu, err);
374760ce86c7SWei Liu     v9fs_string_free(&flock.client_id);
374860ce86c7SWei Liu }
374960ce86c7SWei Liu 
375060ce86c7SWei Liu /*
375160ce86c7SWei Liu  * When a TGETLOCK request comes, always return success because all lock
375260ce86c7SWei Liu  * handling is done by client's VFS layer.
375360ce86c7SWei Liu  */
v9fs_getlock(void * opaque)37548440e22eSGreg Kurz static void coroutine_fn v9fs_getlock(void *opaque)
375560ce86c7SWei Liu {
375660ce86c7SWei Liu     size_t offset = 7;
375760ce86c7SWei Liu     struct stat stbuf;
375860ce86c7SWei Liu     V9fsFidState *fidp;
375960ce86c7SWei Liu     V9fsGetlock glock;
376060ce86c7SWei Liu     int32_t fid, err = 0;
376160ce86c7SWei Liu     V9fsPDU *pdu = opaque;
376260ce86c7SWei Liu 
376360ce86c7SWei Liu     v9fs_string_init(&glock.client_id);
376460ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type,
376560ce86c7SWei Liu                         &glock.start, &glock.length, &glock.proc_id,
376660ce86c7SWei Liu                         &glock.client_id);
376760ce86c7SWei Liu     if (err < 0) {
376860ce86c7SWei Liu         goto out_nofid;
376960ce86c7SWei Liu     }
377060ce86c7SWei Liu     trace_v9fs_getlock(pdu->tag, pdu->id, fid,
377160ce86c7SWei Liu                        glock.type, glock.start, glock.length);
377260ce86c7SWei Liu 
377360ce86c7SWei Liu     fidp = get_fid(pdu, fid);
377460ce86c7SWei Liu     if (fidp == NULL) {
377560ce86c7SWei Liu         err = -ENOENT;
377660ce86c7SWei Liu         goto out_nofid;
377760ce86c7SWei Liu     }
377860ce86c7SWei Liu     err = v9fs_co_fstat(pdu, fidp, &stbuf);
377960ce86c7SWei Liu     if (err < 0) {
378060ce86c7SWei Liu         goto out;
378160ce86c7SWei Liu     }
378260ce86c7SWei Liu     glock.type = P9_LOCK_TYPE_UNLCK;
378360ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "bqqds", glock.type,
378460ce86c7SWei Liu                           glock.start, glock.length, glock.proc_id,
378560ce86c7SWei Liu                           &glock.client_id);
378660ce86c7SWei Liu     if (err < 0) {
378760ce86c7SWei Liu         goto out;
378860ce86c7SWei Liu     }
378960ce86c7SWei Liu     err += offset;
379060ce86c7SWei Liu     trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start,
379160ce86c7SWei Liu                               glock.length, glock.proc_id);
379260ce86c7SWei Liu out:
379360ce86c7SWei Liu     put_fid(pdu, fidp);
379460ce86c7SWei Liu out_nofid:
379560ce86c7SWei Liu     pdu_complete(pdu, err);
379660ce86c7SWei Liu     v9fs_string_free(&glock.client_id);
379760ce86c7SWei Liu }
379860ce86c7SWei Liu 
v9fs_mkdir(void * opaque)37998440e22eSGreg Kurz static void coroutine_fn v9fs_mkdir(void *opaque)
380060ce86c7SWei Liu {
380160ce86c7SWei Liu     V9fsPDU *pdu = opaque;
380260ce86c7SWei Liu     size_t offset = 7;
380360ce86c7SWei Liu     int32_t fid;
380460ce86c7SWei Liu     struct stat stbuf;
380560ce86c7SWei Liu     V9fsQID qid;
380660ce86c7SWei Liu     V9fsString name;
380760ce86c7SWei Liu     V9fsFidState *fidp;
380860ce86c7SWei Liu     gid_t gid;
380960ce86c7SWei Liu     int mode;
381060ce86c7SWei Liu     int err = 0;
381160ce86c7SWei Liu 
381260ce86c7SWei Liu     v9fs_string_init(&name);
381360ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid);
381460ce86c7SWei Liu     if (err < 0) {
381560ce86c7SWei Liu         goto out_nofid;
381660ce86c7SWei Liu     }
381760ce86c7SWei Liu     trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid);
381860ce86c7SWei Liu 
3819fff39a7aSGreg Kurz     if (name_is_illegal(name.data)) {
3820fff39a7aSGreg Kurz         err = -ENOENT;
3821fff39a7aSGreg Kurz         goto out_nofid;
3822fff39a7aSGreg Kurz     }
3823fff39a7aSGreg Kurz 
3824805b5d98SGreg Kurz     if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3825805b5d98SGreg Kurz         err = -EEXIST;
3826805b5d98SGreg Kurz         goto out_nofid;
3827805b5d98SGreg Kurz     }
3828805b5d98SGreg Kurz 
382960ce86c7SWei Liu     fidp = get_fid(pdu, fid);
383060ce86c7SWei Liu     if (fidp == NULL) {
383160ce86c7SWei Liu         err = -ENOENT;
383260ce86c7SWei Liu         goto out_nofid;
383360ce86c7SWei Liu     }
383460ce86c7SWei Liu     err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf);
383560ce86c7SWei Liu     if (err < 0) {
383660ce86c7SWei Liu         goto out;
383760ce86c7SWei Liu     }
38383b5ee9e8SAntonios Motakis     err = stat_to_qid(pdu, &stbuf, &qid);
38393b5ee9e8SAntonios Motakis     if (err < 0) {
38403b5ee9e8SAntonios Motakis         goto out;
38413b5ee9e8SAntonios Motakis     }
384260ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "Q", &qid);
384360ce86c7SWei Liu     if (err < 0) {
384460ce86c7SWei Liu         goto out;
384560ce86c7SWei Liu     }
384660ce86c7SWei Liu     err += offset;
384760ce86c7SWei Liu     trace_v9fs_mkdir_return(pdu->tag, pdu->id,
384860ce86c7SWei Liu                             qid.type, qid.version, qid.path, err);
384960ce86c7SWei Liu out:
385060ce86c7SWei Liu     put_fid(pdu, fidp);
385160ce86c7SWei Liu out_nofid:
385260ce86c7SWei Liu     pdu_complete(pdu, err);
385360ce86c7SWei Liu     v9fs_string_free(&name);
385460ce86c7SWei Liu }
385560ce86c7SWei Liu 
v9fs_xattrwalk(void * opaque)38568440e22eSGreg Kurz static void coroutine_fn v9fs_xattrwalk(void *opaque)
385760ce86c7SWei Liu {
385860ce86c7SWei Liu     int64_t size;
385960ce86c7SWei Liu     V9fsString name;
386060ce86c7SWei Liu     ssize_t err = 0;
386160ce86c7SWei Liu     size_t offset = 7;
386260ce86c7SWei Liu     int32_t fid, newfid;
386360ce86c7SWei Liu     V9fsFidState *file_fidp;
386460ce86c7SWei Liu     V9fsFidState *xattr_fidp = NULL;
386560ce86c7SWei Liu     V9fsPDU *pdu = opaque;
386660ce86c7SWei Liu     V9fsState *s = pdu->s;
386760ce86c7SWei Liu 
386860ce86c7SWei Liu     v9fs_string_init(&name);
386960ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name);
387060ce86c7SWei Liu     if (err < 0) {
387160ce86c7SWei Liu         goto out_nofid;
387260ce86c7SWei Liu     }
387360ce86c7SWei Liu     trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data);
387460ce86c7SWei Liu 
387560ce86c7SWei Liu     file_fidp = get_fid(pdu, fid);
387660ce86c7SWei Liu     if (file_fidp == NULL) {
387760ce86c7SWei Liu         err = -ENOENT;
387860ce86c7SWei Liu         goto out_nofid;
387960ce86c7SWei Liu     }
388060ce86c7SWei Liu     xattr_fidp = alloc_fid(s, newfid);
388160ce86c7SWei Liu     if (xattr_fidp == NULL) {
388260ce86c7SWei Liu         err = -EINVAL;
388360ce86c7SWei Liu         goto out;
388460ce86c7SWei Liu     }
388560ce86c7SWei Liu     v9fs_path_copy(&xattr_fidp->path, &file_fidp->path);
3886ba42ebb8SLi Qiang     if (!v9fs_string_size(&name)) {
388760ce86c7SWei Liu         /*
388860ce86c7SWei Liu          * listxattr request. Get the size first
388960ce86c7SWei Liu          */
389060ce86c7SWei Liu         size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0);
389160ce86c7SWei Liu         if (size < 0) {
389260ce86c7SWei Liu             err = size;
389360ce86c7SWei Liu             clunk_fid(s, xattr_fidp->fid);
389460ce86c7SWei Liu             goto out;
389560ce86c7SWei Liu         }
389660ce86c7SWei Liu         /*
389760ce86c7SWei Liu          * Read the xattr value
389860ce86c7SWei Liu          */
389960ce86c7SWei Liu         xattr_fidp->fs.xattr.len = size;
390060ce86c7SWei Liu         xattr_fidp->fid_type = P9_FID_XATTR;
3901dd28fbbcSLi Qiang         xattr_fidp->fs.xattr.xattrwalk_fid = true;
39027bd92756SPrasad J Pandit         xattr_fidp->fs.xattr.value = g_malloc0(size);
3903a647502cSKeno Fischer         if (size) {
390460ce86c7SWei Liu             err = v9fs_co_llistxattr(pdu, &xattr_fidp->path,
390560ce86c7SWei Liu                                      xattr_fidp->fs.xattr.value,
390660ce86c7SWei Liu                                      xattr_fidp->fs.xattr.len);
390760ce86c7SWei Liu             if (err < 0) {
390860ce86c7SWei Liu                 clunk_fid(s, xattr_fidp->fid);
390960ce86c7SWei Liu                 goto out;
391060ce86c7SWei Liu             }
391160ce86c7SWei Liu         }
391260ce86c7SWei Liu         err = pdu_marshal(pdu, offset, "q", size);
391360ce86c7SWei Liu         if (err < 0) {
391460ce86c7SWei Liu             goto out;
391560ce86c7SWei Liu         }
391660ce86c7SWei Liu         err += offset;
391760ce86c7SWei Liu     } else {
391860ce86c7SWei Liu         /*
391960ce86c7SWei Liu          * specific xattr fid. We check for xattr
392060ce86c7SWei Liu          * presence also collect the xattr size
392160ce86c7SWei Liu          */
392260ce86c7SWei Liu         size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
392360ce86c7SWei Liu                                  &name, NULL, 0);
392460ce86c7SWei Liu         if (size < 0) {
392560ce86c7SWei Liu             err = size;
392660ce86c7SWei Liu             clunk_fid(s, xattr_fidp->fid);
392760ce86c7SWei Liu             goto out;
392860ce86c7SWei Liu         }
392960ce86c7SWei Liu         /*
393060ce86c7SWei Liu          * Read the xattr value
393160ce86c7SWei Liu          */
393260ce86c7SWei Liu         xattr_fidp->fs.xattr.len = size;
393360ce86c7SWei Liu         xattr_fidp->fid_type = P9_FID_XATTR;
3934dd28fbbcSLi Qiang         xattr_fidp->fs.xattr.xattrwalk_fid = true;
39357bd92756SPrasad J Pandit         xattr_fidp->fs.xattr.value = g_malloc0(size);
3936a647502cSKeno Fischer         if (size) {
393760ce86c7SWei Liu             err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
393860ce86c7SWei Liu                                     &name, xattr_fidp->fs.xattr.value,
393960ce86c7SWei Liu                                     xattr_fidp->fs.xattr.len);
394060ce86c7SWei Liu             if (err < 0) {
394160ce86c7SWei Liu                 clunk_fid(s, xattr_fidp->fid);
394260ce86c7SWei Liu                 goto out;
394360ce86c7SWei Liu             }
394460ce86c7SWei Liu         }
394560ce86c7SWei Liu         err = pdu_marshal(pdu, offset, "q", size);
394660ce86c7SWei Liu         if (err < 0) {
394760ce86c7SWei Liu             goto out;
394860ce86c7SWei Liu         }
394960ce86c7SWei Liu         err += offset;
395060ce86c7SWei Liu     }
395160ce86c7SWei Liu     trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size);
395260ce86c7SWei Liu out:
395360ce86c7SWei Liu     put_fid(pdu, file_fidp);
395460ce86c7SWei Liu     if (xattr_fidp) {
395560ce86c7SWei Liu         put_fid(pdu, xattr_fidp);
395660ce86c7SWei Liu     }
395760ce86c7SWei Liu out_nofid:
395860ce86c7SWei Liu     pdu_complete(pdu, err);
395960ce86c7SWei Liu     v9fs_string_free(&name);
396060ce86c7SWei Liu }
396160ce86c7SWei Liu 
3962a136d175SWill Cohen #if defined(CONFIG_LINUX)
3963a136d175SWill Cohen /* Currently, only Linux has XATTR_SIZE_MAX */
3964a136d175SWill Cohen #define P9_XATTR_SIZE_MAX XATTR_SIZE_MAX
3965a136d175SWill Cohen #elif defined(CONFIG_DARWIN)
3966a136d175SWill Cohen /*
3967a136d175SWill Cohen  * Darwin doesn't seem to define a maximum xattr size in its user
3968a136d175SWill Cohen  * space header, so manually configure it across platforms as 64k.
3969a136d175SWill Cohen  *
3970a136d175SWill Cohen  * Having no limit at all can lead to QEMU crashing during large g_malloc()
3971a136d175SWill Cohen  * calls. Because QEMU does not currently support macOS guests, the below
3972a136d175SWill Cohen  * preliminary solution only works due to its being a reflection of the limit of
3973a136d175SWill Cohen  * Linux guests.
3974a136d175SWill Cohen  */
3975a136d175SWill Cohen #define P9_XATTR_SIZE_MAX 65536
3976a136d175SWill Cohen #else
3977a136d175SWill Cohen #error Missing definition for P9_XATTR_SIZE_MAX for this host system
3978a136d175SWill Cohen #endif
3979a136d175SWill Cohen 
v9fs_xattrcreate(void * opaque)39808440e22eSGreg Kurz static void coroutine_fn v9fs_xattrcreate(void *opaque)
398160ce86c7SWei Liu {
3982aca6897fSKeno Fischer     int flags, rflags = 0;
398360ce86c7SWei Liu     int32_t fid;
39843b79ef2cSGreg Kurz     uint64_t size;
398560ce86c7SWei Liu     ssize_t err = 0;
398660ce86c7SWei Liu     V9fsString name;
398760ce86c7SWei Liu     size_t offset = 7;
398860ce86c7SWei Liu     V9fsFidState *file_fidp;
398960ce86c7SWei Liu     V9fsFidState *xattr_fidp;
399060ce86c7SWei Liu     V9fsPDU *pdu = opaque;
399160ce86c7SWei Liu 
399260ce86c7SWei Liu     v9fs_string_init(&name);
399360ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags);
399460ce86c7SWei Liu     if (err < 0) {
399560ce86c7SWei Liu         goto out_nofid;
399660ce86c7SWei Liu     }
399760ce86c7SWei Liu     trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags);
399860ce86c7SWei Liu 
3999aca6897fSKeno Fischer     if (flags & ~(P9_XATTR_CREATE | P9_XATTR_REPLACE)) {
4000aca6897fSKeno Fischer         err = -EINVAL;
4001aca6897fSKeno Fischer         goto out_nofid;
4002aca6897fSKeno Fischer     }
4003aca6897fSKeno Fischer 
4004aca6897fSKeno Fischer     if (flags & P9_XATTR_CREATE) {
4005aca6897fSKeno Fischer         rflags |= XATTR_CREATE;
4006aca6897fSKeno Fischer     }
4007aca6897fSKeno Fischer 
4008aca6897fSKeno Fischer     if (flags & P9_XATTR_REPLACE) {
4009aca6897fSKeno Fischer         rflags |= XATTR_REPLACE;
4010aca6897fSKeno Fischer     }
4011aca6897fSKeno Fischer 
401238d7fd68SKeno Fischer     if (size > P9_XATTR_SIZE_MAX) {
40133b79ef2cSGreg Kurz         err = -E2BIG;
40143b79ef2cSGreg Kurz         goto out_nofid;
40153b79ef2cSGreg Kurz     }
40163b79ef2cSGreg Kurz 
401760ce86c7SWei Liu     file_fidp = get_fid(pdu, fid);
401860ce86c7SWei Liu     if (file_fidp == NULL) {
401960ce86c7SWei Liu         err = -EINVAL;
402060ce86c7SWei Liu         goto out_nofid;
402160ce86c7SWei Liu     }
4022dd654e03SGreg Kurz     if (file_fidp->fid_type != P9_FID_NONE) {
4023dd654e03SGreg Kurz         err = -EINVAL;
4024dd654e03SGreg Kurz         goto out_put_fid;
4025dd654e03SGreg Kurz     }
4026dd654e03SGreg Kurz 
402760ce86c7SWei Liu     /* Make the file fid point to xattr */
402860ce86c7SWei Liu     xattr_fidp = file_fidp;
402960ce86c7SWei Liu     xattr_fidp->fid_type = P9_FID_XATTR;
403060ce86c7SWei Liu     xattr_fidp->fs.xattr.copied_len = 0;
4031dd28fbbcSLi Qiang     xattr_fidp->fs.xattr.xattrwalk_fid = false;
403260ce86c7SWei Liu     xattr_fidp->fs.xattr.len = size;
4033aca6897fSKeno Fischer     xattr_fidp->fs.xattr.flags = rflags;
403460ce86c7SWei Liu     v9fs_string_init(&xattr_fidp->fs.xattr.name);
403560ce86c7SWei Liu     v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
4036eb687602SLi Qiang     xattr_fidp->fs.xattr.value = g_malloc0(size);
403760ce86c7SWei Liu     err = offset;
4038dd654e03SGreg Kurz out_put_fid:
403960ce86c7SWei Liu     put_fid(pdu, file_fidp);
404060ce86c7SWei Liu out_nofid:
404160ce86c7SWei Liu     pdu_complete(pdu, err);
404260ce86c7SWei Liu     v9fs_string_free(&name);
404360ce86c7SWei Liu }
404460ce86c7SWei Liu 
v9fs_readlink(void * opaque)40458440e22eSGreg Kurz static void coroutine_fn v9fs_readlink(void *opaque)
404660ce86c7SWei Liu {
404760ce86c7SWei Liu     V9fsPDU *pdu = opaque;
404860ce86c7SWei Liu     size_t offset = 7;
404960ce86c7SWei Liu     V9fsString target;
405060ce86c7SWei Liu     int32_t fid;
405160ce86c7SWei Liu     int err = 0;
405260ce86c7SWei Liu     V9fsFidState *fidp;
405360ce86c7SWei Liu 
405460ce86c7SWei Liu     err = pdu_unmarshal(pdu, offset, "d", &fid);
405560ce86c7SWei Liu     if (err < 0) {
405660ce86c7SWei Liu         goto out_nofid;
405760ce86c7SWei Liu     }
405860ce86c7SWei Liu     trace_v9fs_readlink(pdu->tag, pdu->id, fid);
405960ce86c7SWei Liu     fidp = get_fid(pdu, fid);
406060ce86c7SWei Liu     if (fidp == NULL) {
406160ce86c7SWei Liu         err = -ENOENT;
406260ce86c7SWei Liu         goto out_nofid;
406360ce86c7SWei Liu     }
406460ce86c7SWei Liu 
406560ce86c7SWei Liu     v9fs_string_init(&target);
406660ce86c7SWei Liu     err = v9fs_co_readlink(pdu, &fidp->path, &target);
406760ce86c7SWei Liu     if (err < 0) {
406860ce86c7SWei Liu         goto out;
406960ce86c7SWei Liu     }
407060ce86c7SWei Liu     err = pdu_marshal(pdu, offset, "s", &target);
407160ce86c7SWei Liu     if (err < 0) {
407260ce86c7SWei Liu         v9fs_string_free(&target);
407360ce86c7SWei Liu         goto out;
407460ce86c7SWei Liu     }
407560ce86c7SWei Liu     err += offset;
407660ce86c7SWei Liu     trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data);
407760ce86c7SWei Liu     v9fs_string_free(&target);
407860ce86c7SWei Liu out:
407960ce86c7SWei Liu     put_fid(pdu, fidp);
408060ce86c7SWei Liu out_nofid:
408160ce86c7SWei Liu     pdu_complete(pdu, err);
408260ce86c7SWei Liu }
408360ce86c7SWei Liu 
408460ce86c7SWei Liu static CoroutineEntry *pdu_co_handlers[] = {
408560ce86c7SWei Liu     [P9_TREADDIR] = v9fs_readdir,
408660ce86c7SWei Liu     [P9_TSTATFS] = v9fs_statfs,
408760ce86c7SWei Liu     [P9_TGETATTR] = v9fs_getattr,
408860ce86c7SWei Liu     [P9_TSETATTR] = v9fs_setattr,
408960ce86c7SWei Liu     [P9_TXATTRWALK] = v9fs_xattrwalk,
409060ce86c7SWei Liu     [P9_TXATTRCREATE] = v9fs_xattrcreate,
409160ce86c7SWei Liu     [P9_TMKNOD] = v9fs_mknod,
409260ce86c7SWei Liu     [P9_TRENAME] = v9fs_rename,
409360ce86c7SWei Liu     [P9_TLOCK] = v9fs_lock,
409460ce86c7SWei Liu     [P9_TGETLOCK] = v9fs_getlock,
409560ce86c7SWei Liu     [P9_TRENAMEAT] = v9fs_renameat,
409660ce86c7SWei Liu     [P9_TREADLINK] = v9fs_readlink,
409760ce86c7SWei Liu     [P9_TUNLINKAT] = v9fs_unlinkat,
409860ce86c7SWei Liu     [P9_TMKDIR] = v9fs_mkdir,
409960ce86c7SWei Liu     [P9_TVERSION] = v9fs_version,
410060ce86c7SWei Liu     [P9_TLOPEN] = v9fs_open,
410160ce86c7SWei Liu     [P9_TATTACH] = v9fs_attach,
410260ce86c7SWei Liu     [P9_TSTAT] = v9fs_stat,
410360ce86c7SWei Liu     [P9_TWALK] = v9fs_walk,
410460ce86c7SWei Liu     [P9_TCLUNK] = v9fs_clunk,
410560ce86c7SWei Liu     [P9_TFSYNC] = v9fs_fsync,
410660ce86c7SWei Liu     [P9_TOPEN] = v9fs_open,
410760ce86c7SWei Liu     [P9_TREAD] = v9fs_read,
410860ce86c7SWei Liu #if 0
410960ce86c7SWei Liu     [P9_TAUTH] = v9fs_auth,
411060ce86c7SWei Liu #endif
411160ce86c7SWei Liu     [P9_TFLUSH] = v9fs_flush,
411260ce86c7SWei Liu     [P9_TLINK] = v9fs_link,
411360ce86c7SWei Liu     [P9_TSYMLINK] = v9fs_symlink,
411460ce86c7SWei Liu     [P9_TCREATE] = v9fs_create,
411560ce86c7SWei Liu     [P9_TLCREATE] = v9fs_lcreate,
411660ce86c7SWei Liu     [P9_TWRITE] = v9fs_write,
411760ce86c7SWei Liu     [P9_TWSTAT] = v9fs_wstat,
411860ce86c7SWei Liu     [P9_TREMOVE] = v9fs_remove,
411960ce86c7SWei Liu };
412060ce86c7SWei Liu 
v9fs_op_not_supp(void * opaque)41218440e22eSGreg Kurz static void coroutine_fn v9fs_op_not_supp(void *opaque)
412260ce86c7SWei Liu {
412360ce86c7SWei Liu     V9fsPDU *pdu = opaque;
412460ce86c7SWei Liu     pdu_complete(pdu, -EOPNOTSUPP);
412560ce86c7SWei Liu }
412660ce86c7SWei Liu 
v9fs_fs_ro(void * opaque)41278440e22eSGreg Kurz static void coroutine_fn v9fs_fs_ro(void *opaque)
412860ce86c7SWei Liu {
412960ce86c7SWei Liu     V9fsPDU *pdu = opaque;
413060ce86c7SWei Liu     pdu_complete(pdu, -EROFS);
413160ce86c7SWei Liu }
413260ce86c7SWei Liu 
is_read_only_op(V9fsPDU * pdu)413360ce86c7SWei Liu static inline bool is_read_only_op(V9fsPDU *pdu)
413460ce86c7SWei Liu {
413560ce86c7SWei Liu     switch (pdu->id) {
413660ce86c7SWei Liu     case P9_TREADDIR:
413760ce86c7SWei Liu     case P9_TSTATFS:
413860ce86c7SWei Liu     case P9_TGETATTR:
413960ce86c7SWei Liu     case P9_TXATTRWALK:
414060ce86c7SWei Liu     case P9_TLOCK:
414160ce86c7SWei Liu     case P9_TGETLOCK:
414260ce86c7SWei Liu     case P9_TREADLINK:
414360ce86c7SWei Liu     case P9_TVERSION:
414460ce86c7SWei Liu     case P9_TLOPEN:
414560ce86c7SWei Liu     case P9_TATTACH:
414660ce86c7SWei Liu     case P9_TSTAT:
414760ce86c7SWei Liu     case P9_TWALK:
414860ce86c7SWei Liu     case P9_TCLUNK:
414960ce86c7SWei Liu     case P9_TFSYNC:
415060ce86c7SWei Liu     case P9_TOPEN:
415160ce86c7SWei Liu     case P9_TREAD:
415260ce86c7SWei Liu     case P9_TAUTH:
415360ce86c7SWei Liu     case P9_TFLUSH:
415460ce86c7SWei Liu         return 1;
415560ce86c7SWei Liu     default:
415660ce86c7SWei Liu         return 0;
415760ce86c7SWei Liu     }
415860ce86c7SWei Liu }
415960ce86c7SWei Liu 
pdu_submit(V9fsPDU * pdu,P9MsgHeader * hdr)4160506f3275SGreg Kurz void pdu_submit(V9fsPDU *pdu, P9MsgHeader *hdr)
416160ce86c7SWei Liu {
416260ce86c7SWei Liu     Coroutine *co;
416360ce86c7SWei Liu     CoroutineEntry *handler;
416460ce86c7SWei Liu     V9fsState *s = pdu->s;
416560ce86c7SWei Liu 
4166506f3275SGreg Kurz     pdu->size = le32_to_cpu(hdr->size_le);
4167506f3275SGreg Kurz     pdu->id = hdr->id;
4168506f3275SGreg Kurz     pdu->tag = le16_to_cpu(hdr->tag_le);
4169506f3275SGreg Kurz 
417060ce86c7SWei Liu     if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) ||
417160ce86c7SWei Liu         (pdu_co_handlers[pdu->id] == NULL)) {
417260ce86c7SWei Liu         handler = v9fs_op_not_supp;
4173d1471233SGreg Kurz     } else if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) {
4174d1471233SGreg Kurz         handler = v9fs_fs_ro;
417560ce86c7SWei Liu     } else {
417660ce86c7SWei Liu         handler = pdu_co_handlers[pdu->id];
417760ce86c7SWei Liu     }
417860ce86c7SWei Liu 
4179506f3275SGreg Kurz     qemu_co_queue_init(&pdu->complete);
41800b8b8753SPaolo Bonzini     co = qemu_coroutine_create(handler, pdu);
41810b8b8753SPaolo Bonzini     qemu_coroutine_enter(co);
418260ce86c7SWei Liu }
418360ce86c7SWei Liu 
41842a0c56aaSWei Liu /* Returns 0 on success, 1 on failure. */
v9fs_device_realize_common(V9fsState * s,const V9fsTransport * t,Error ** errp)4185066eb006SGreg Kurz int v9fs_device_realize_common(V9fsState *s, const V9fsTransport *t,
4186066eb006SGreg Kurz                                Error **errp)
41872a0c56aaSWei Liu {
418892c45122SVladimir Sementsov-Ogievskiy     ERRP_GUARD();
41892a0c56aaSWei Liu     int i, len;
41902a0c56aaSWei Liu     struct stat stat;
41912a0c56aaSWei Liu     FsDriverEntry *fse;
41922a0c56aaSWei Liu     V9fsPath path;
41932a0c56aaSWei Liu     int rc = 1;
41942a0c56aaSWei Liu 
4195066eb006SGreg Kurz     assert(!s->transport);
4196066eb006SGreg Kurz     s->transport = t;
4197066eb006SGreg Kurz 
41982a0c56aaSWei Liu     /* initialize pdu allocator */
41992a0c56aaSWei Liu     QLIST_INIT(&s->free_list);
42002a0c56aaSWei Liu     QLIST_INIT(&s->active_list);
42010d78289cSGreg Kurz     for (i = 0; i < MAX_REQ; i++) {
4202583f21f8SStefano Stabellini         QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
4203583f21f8SStefano Stabellini         s->pdus[i].s = s;
4204583f21f8SStefano Stabellini         s->pdus[i].idx = i;
42052a0c56aaSWei Liu     }
42062a0c56aaSWei Liu 
42072a0c56aaSWei Liu     v9fs_path_init(&path);
42082a0c56aaSWei Liu 
42092a0c56aaSWei Liu     fse = get_fsdev_fsentry(s->fsconf.fsdev_id);
42102a0c56aaSWei Liu 
42112a0c56aaSWei Liu     if (!fse) {
42122a0c56aaSWei Liu         /* We don't have a fsdev identified by fsdev_id */
42132a0c56aaSWei Liu         error_setg(errp, "9pfs device couldn't find fsdev with the "
42142a0c56aaSWei Liu                    "id = %s",
42152a0c56aaSWei Liu                    s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL");
42162a0c56aaSWei Liu         goto out;
42172a0c56aaSWei Liu     }
42182a0c56aaSWei Liu 
42192a0c56aaSWei Liu     if (!s->fsconf.tag) {
42202a0c56aaSWei Liu         /* we haven't specified a mount_tag */
42212a0c56aaSWei Liu         error_setg(errp, "fsdev with id %s needs mount_tag arguments",
42222a0c56aaSWei Liu                    s->fsconf.fsdev_id);
42232a0c56aaSWei Liu         goto out;
42242a0c56aaSWei Liu     }
42252a0c56aaSWei Liu 
42262a0c56aaSWei Liu     s->ctx.export_flags = fse->export_flags;
42272a0c56aaSWei Liu     s->ctx.fs_root = g_strdup(fse->path);
42282a0c56aaSWei Liu     s->ctx.exops.get_st_gen = NULL;
42292a0c56aaSWei Liu     len = strlen(s->fsconf.tag);
42302a0c56aaSWei Liu     if (len > MAX_TAG_LEN - 1) {
42312a0c56aaSWei Liu         error_setg(errp, "mount tag '%s' (%d bytes) is longer than "
42322a0c56aaSWei Liu                    "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1);
42332a0c56aaSWei Liu         goto out;
42342a0c56aaSWei Liu     }
42352a0c56aaSWei Liu 
42362a0c56aaSWei Liu     s->tag = g_strdup(s->fsconf.tag);
42372a0c56aaSWei Liu     s->ctx.uid = -1;
42382a0c56aaSWei Liu 
42392a0c56aaSWei Liu     s->ops = fse->ops;
42402a0c56aaSWei Liu 
4241b96feb2cSTobias Schramm     s->ctx.fmode = fse->fmode;
4242b96feb2cSTobias Schramm     s->ctx.dmode = fse->dmode;
4243b96feb2cSTobias Schramm 
4244f5265c8fSLinus Heckemann     s->fids = g_hash_table_new(NULL, NULL);
42452a0c56aaSWei Liu     qemu_co_rwlock_init(&s->rename_lock);
42462a0c56aaSWei Liu 
424765603a80SGreg Kurz     if (s->ops->init(&s->ctx, errp) < 0) {
424865603a80SGreg Kurz         error_prepend(errp, "cannot initialize fsdev '%s': ",
424965603a80SGreg Kurz                       s->fsconf.fsdev_id);
42502a0c56aaSWei Liu         goto out;
42512a0c56aaSWei Liu     }
42522a0c56aaSWei Liu 
42532a0c56aaSWei Liu     /*
42542a0c56aaSWei Liu      * Check details of export path, We need to use fs driver
42552a0c56aaSWei Liu      * call back to do that. Since we are in the init path, we don't
42562a0c56aaSWei Liu      * use co-routines here.
42572a0c56aaSWei Liu      */
42582a0c56aaSWei Liu     if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) {
42592a0c56aaSWei Liu         error_setg(errp,
42602a0c56aaSWei Liu                    "error in converting name to path %s", strerror(errno));
42612a0c56aaSWei Liu         goto out;
42622a0c56aaSWei Liu     }
42632a0c56aaSWei Liu     if (s->ops->lstat(&s->ctx, &path, &stat)) {
42642a0c56aaSWei Liu         error_setg(errp, "share path %s does not exist", fse->path);
42652a0c56aaSWei Liu         goto out;
42662a0c56aaSWei Liu     } else if (!S_ISDIR(stat.st_mode)) {
42672a0c56aaSWei Liu         error_setg(errp, "share path %s is not a directory", fse->path);
42682a0c56aaSWei Liu         goto out;
42692a0c56aaSWei Liu     }
4270b8bbdb88SPradeep Jagadeesh 
42713b5ee9e8SAntonios Motakis     s->dev_id = stat.st_dev;
42723b5ee9e8SAntonios Motakis 
42736b6aa828SChristian Schoenebeck     /* init inode remapping : */
42746b6aa828SChristian Schoenebeck     /* hash table for variable length inode suffixes */
42756b6aa828SChristian Schoenebeck     qpd_table_init(&s->qpd_table);
42766b6aa828SChristian Schoenebeck     /* hash table for slow/full inode remapping (most users won't need it) */
42776b6aa828SChristian Schoenebeck     qpf_table_init(&s->qpf_table);
42786b6aa828SChristian Schoenebeck     /* hash table for quick inode remapping */
42791a6ed33cSAntonios Motakis     qpp_table_init(&s->qpp_table);
42806b6aa828SChristian Schoenebeck     s->qp_ndevices = 0;
42816b6aa828SChristian Schoenebeck     s->qp_affix_next = 1; /* reserve 0 to detect overflow */
4282f3fe4a2dSAntonios Motakis     s->qp_fullpath_next = 1;
42831a6ed33cSAntonios Motakis 
4284b8bbdb88SPradeep Jagadeesh     s->ctx.fst = &fse->fst;
4285b8bbdb88SPradeep Jagadeesh     fsdev_throttle_init(s->ctx.fst);
4286b8bbdb88SPradeep Jagadeesh 
42872a0c56aaSWei Liu     rc = 0;
42882a0c56aaSWei Liu out:
42892a0c56aaSWei Liu     if (rc) {
4290b69c3c21SMarkus Armbruster         v9fs_device_unrealize_common(s);
4291702dbcc2SLi Qiang     }
42922a0c56aaSWei Liu     v9fs_path_free(&path);
42932a0c56aaSWei Liu     return rc;
42942a0c56aaSWei Liu }
42952a0c56aaSWei Liu 
v9fs_device_unrealize_common(V9fsState * s)4296b69c3c21SMarkus Armbruster void v9fs_device_unrealize_common(V9fsState *s)
42972a0c56aaSWei Liu {
4298c0da0cb7SGreg Kurz     if (s->ops && s->ops->cleanup) {
4299702dbcc2SLi Qiang         s->ops->cleanup(&s->ctx);
4300702dbcc2SLi Qiang     }
4301c0da0cb7SGreg Kurz     if (s->ctx.fst) {
4302b8bbdb88SPradeep Jagadeesh         fsdev_throttle_cleanup(s->ctx.fst);
4303c0da0cb7SGreg Kurz     }
4304f5265c8fSLinus Heckemann     if (s->fids) {
4305f5265c8fSLinus Heckemann         g_hash_table_destroy(s->fids);
4306f5265c8fSLinus Heckemann         s->fids = NULL;
4307f5265c8fSLinus Heckemann     }
43082a0c56aaSWei Liu     g_free(s->tag);
43096b6aa828SChristian Schoenebeck     qp_table_destroy(&s->qpd_table);
4310f3fe4a2dSAntonios Motakis     qp_table_destroy(&s->qpp_table);
4311f3fe4a2dSAntonios Motakis     qp_table_destroy(&s->qpf_table);
43124774718eSLi Qiang     g_free(s->ctx.fs_root);
43132a0c56aaSWei Liu }
43142a0c56aaSWei Liu 
43150e44a0fdSGreg Kurz typedef struct VirtfsCoResetData {
43160e44a0fdSGreg Kurz     V9fsPDU pdu;
43170e44a0fdSGreg Kurz     bool done;
43180e44a0fdSGreg Kurz } VirtfsCoResetData;
43190e44a0fdSGreg Kurz 
virtfs_co_reset(void * opaque)43200e44a0fdSGreg Kurz static void coroutine_fn virtfs_co_reset(void *opaque)
43210e44a0fdSGreg Kurz {
43220e44a0fdSGreg Kurz     VirtfsCoResetData *data = opaque;
43230e44a0fdSGreg Kurz 
43240e44a0fdSGreg Kurz     virtfs_reset(&data->pdu);
43250e44a0fdSGreg Kurz     data->done = true;
43260e44a0fdSGreg Kurz }
43270e44a0fdSGreg Kurz 
v9fs_reset(V9fsState * s)43280e44a0fdSGreg Kurz void v9fs_reset(V9fsState *s)
43290e44a0fdSGreg Kurz {
43300e44a0fdSGreg Kurz     VirtfsCoResetData data = { .pdu = { .s = s }, .done = false };
43310e44a0fdSGreg Kurz     Coroutine *co;
43320e44a0fdSGreg Kurz 
43330e44a0fdSGreg Kurz     while (!QLIST_EMPTY(&s->active_list)) {
43340e44a0fdSGreg Kurz         aio_poll(qemu_get_aio_context(), true);
43350e44a0fdSGreg Kurz     }
43360e44a0fdSGreg Kurz 
43370e44a0fdSGreg Kurz     co = qemu_coroutine_create(virtfs_co_reset, &data);
43380e44a0fdSGreg Kurz     qemu_coroutine_enter(co);
43390e44a0fdSGreg Kurz 
43400e44a0fdSGreg Kurz     while (!data.done) {
43410e44a0fdSGreg Kurz         aio_poll(qemu_get_aio_context(), true);
43420e44a0fdSGreg Kurz     }
43430e44a0fdSGreg Kurz }
43440e44a0fdSGreg Kurz 
v9fs_set_fd_limit(void)434560ce86c7SWei Liu static void __attribute__((__constructor__)) v9fs_set_fd_limit(void)
434660ce86c7SWei Liu {
434760ce86c7SWei Liu     struct rlimit rlim;
434860ce86c7SWei Liu     if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
434963325b18SGreg Kurz         error_report("Failed to get the resource limit");
435060ce86c7SWei Liu         exit(1);
435160ce86c7SWei Liu     }
435260ce86c7SWei Liu     open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur / 3);
435360ce86c7SWei Liu     open_fd_rc = rlim.rlim_cur / 2;
435460ce86c7SWei Liu }
4355