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 #else 23a136d175SWill Cohen #include <limits.h> 24a136d175SWill Cohen #endif 25e3e83f2eSGreg Kurz #include <glib/gprintf.h> 2660ce86c7SWei Liu #include "hw/virtio/virtio.h" 27da34e65cSMarkus Armbruster #include "qapi/error.h" 2860ce86c7SWei Liu #include "qemu/error-report.h" 2960ce86c7SWei Liu #include "qemu/iov.h" 30db725815SMarkus Armbruster #include "qemu/main-loop.h" 3160ce86c7SWei Liu #include "qemu/sockets.h" 3260ce86c7SWei Liu #include "virtio-9p.h" 3360ce86c7SWei Liu #include "fsdev/qemu-fsdev.h" 3460ce86c7SWei Liu #include "9p-xattr.h" 356b3b279bSKeno Fischer #include "9p-util.h" 3660ce86c7SWei Liu #include "coth.h" 3760ce86c7SWei Liu #include "trace.h" 38795c40b8SJuan Quintela #include "migration/blocker.h" 391a6ed33cSAntonios Motakis #include "qemu/xxhash.h" 406b6aa828SChristian Schoenebeck #include <math.h> 4160ce86c7SWei Liu 4260ce86c7SWei Liu int open_fd_hw; 4360ce86c7SWei Liu int total_open_fd; 4460ce86c7SWei Liu static int open_fd_rc; 4560ce86c7SWei Liu 4660ce86c7SWei Liu enum { 4760ce86c7SWei Liu Oread = 0x00, 4860ce86c7SWei Liu Owrite = 0x01, 4960ce86c7SWei Liu Ordwr = 0x02, 5060ce86c7SWei Liu Oexec = 0x03, 5160ce86c7SWei Liu Oexcl = 0x04, 5260ce86c7SWei Liu Otrunc = 0x10, 5360ce86c7SWei Liu Orexec = 0x20, 5460ce86c7SWei Liu Orclose = 0x40, 5560ce86c7SWei Liu Oappend = 0x80, 5660ce86c7SWei Liu }; 5760ce86c7SWei Liu 58cc82fde9SChristian Schoenebeck P9ARRAY_DEFINE_TYPE(V9fsPath, v9fs_path_free); 59cc82fde9SChristian Schoenebeck 6075673590SGreg Kurz static ssize_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...) 6160ce86c7SWei Liu { 6260ce86c7SWei Liu ssize_t ret; 6360ce86c7SWei Liu va_list ap; 6460ce86c7SWei Liu 6560ce86c7SWei Liu va_start(ap, fmt); 66ea83441cSStefano Stabellini ret = pdu->s->transport->pdu_vmarshal(pdu, offset, fmt, ap); 6760ce86c7SWei Liu va_end(ap); 6860ce86c7SWei Liu 6960ce86c7SWei Liu return ret; 7060ce86c7SWei Liu } 7160ce86c7SWei Liu 7275673590SGreg Kurz static ssize_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...) 7360ce86c7SWei Liu { 7460ce86c7SWei Liu ssize_t ret; 7560ce86c7SWei Liu va_list ap; 7660ce86c7SWei Liu 7760ce86c7SWei Liu va_start(ap, fmt); 78ea83441cSStefano Stabellini ret = pdu->s->transport->pdu_vunmarshal(pdu, offset, fmt, ap); 7960ce86c7SWei Liu va_end(ap); 8060ce86c7SWei Liu 8160ce86c7SWei Liu return ret; 8260ce86c7SWei Liu } 8360ce86c7SWei Liu 8460ce86c7SWei Liu static int omode_to_uflags(int8_t mode) 8560ce86c7SWei Liu { 8660ce86c7SWei Liu int ret = 0; 8760ce86c7SWei Liu 8860ce86c7SWei Liu switch (mode & 3) { 8960ce86c7SWei Liu case Oread: 9060ce86c7SWei Liu ret = O_RDONLY; 9160ce86c7SWei Liu break; 9260ce86c7SWei Liu case Ordwr: 9360ce86c7SWei Liu ret = O_RDWR; 9460ce86c7SWei Liu break; 9560ce86c7SWei Liu case Owrite: 9660ce86c7SWei Liu ret = O_WRONLY; 9760ce86c7SWei Liu break; 9860ce86c7SWei Liu case Oexec: 9960ce86c7SWei Liu ret = O_RDONLY; 10060ce86c7SWei Liu break; 10160ce86c7SWei Liu } 10260ce86c7SWei Liu 10360ce86c7SWei Liu if (mode & Otrunc) { 10460ce86c7SWei Liu ret |= O_TRUNC; 10560ce86c7SWei Liu } 10660ce86c7SWei Liu 10760ce86c7SWei Liu if (mode & Oappend) { 10860ce86c7SWei Liu ret |= O_APPEND; 10960ce86c7SWei Liu } 11060ce86c7SWei Liu 11160ce86c7SWei Liu if (mode & Oexcl) { 11260ce86c7SWei Liu ret |= O_EXCL; 11360ce86c7SWei Liu } 11460ce86c7SWei Liu 11560ce86c7SWei Liu return ret; 11660ce86c7SWei Liu } 11760ce86c7SWei Liu 1188e71b96cSGreg Kurz typedef struct DotlOpenflagMap { 11960ce86c7SWei Liu int dotl_flag; 12060ce86c7SWei Liu int open_flag; 1218e71b96cSGreg Kurz } DotlOpenflagMap; 12260ce86c7SWei Liu 12360ce86c7SWei Liu static int dotl_to_open_flags(int flags) 12460ce86c7SWei Liu { 12560ce86c7SWei Liu int i; 12660ce86c7SWei Liu /* 12760ce86c7SWei Liu * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY 12860ce86c7SWei Liu * and P9_DOTL_NOACCESS 12960ce86c7SWei Liu */ 13060ce86c7SWei Liu int oflags = flags & O_ACCMODE; 13160ce86c7SWei Liu 1328e71b96cSGreg Kurz DotlOpenflagMap dotl_oflag_map[] = { 13360ce86c7SWei Liu { P9_DOTL_CREATE, O_CREAT }, 13460ce86c7SWei Liu { P9_DOTL_EXCL, O_EXCL }, 13560ce86c7SWei Liu { P9_DOTL_NOCTTY , O_NOCTTY }, 13660ce86c7SWei Liu { P9_DOTL_TRUNC, O_TRUNC }, 13760ce86c7SWei Liu { P9_DOTL_APPEND, O_APPEND }, 13860ce86c7SWei Liu { P9_DOTL_NONBLOCK, O_NONBLOCK } , 13960ce86c7SWei Liu { P9_DOTL_DSYNC, O_DSYNC }, 14060ce86c7SWei Liu { P9_DOTL_FASYNC, FASYNC }, 14167a71e3bSKeno Fischer #ifndef CONFIG_DARWIN 14267a71e3bSKeno Fischer { P9_DOTL_NOATIME, O_NOATIME }, 14367a71e3bSKeno Fischer /* 14467a71e3bSKeno Fischer * On Darwin, we could map to F_NOCACHE, which is 14567a71e3bSKeno Fischer * similar, but doesn't quite have the same 14667a71e3bSKeno Fischer * semantics. However, we don't support O_DIRECT 14767a71e3bSKeno Fischer * even on linux at the moment, so we just ignore 14867a71e3bSKeno Fischer * it here. 14967a71e3bSKeno Fischer */ 15060ce86c7SWei Liu { P9_DOTL_DIRECT, O_DIRECT }, 15167a71e3bSKeno Fischer #endif 15260ce86c7SWei Liu { P9_DOTL_LARGEFILE, O_LARGEFILE }, 15360ce86c7SWei Liu { P9_DOTL_DIRECTORY, O_DIRECTORY }, 15460ce86c7SWei Liu { P9_DOTL_NOFOLLOW, O_NOFOLLOW }, 15560ce86c7SWei Liu { P9_DOTL_SYNC, O_SYNC }, 15660ce86c7SWei Liu }; 15760ce86c7SWei Liu 15860ce86c7SWei Liu for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) { 15960ce86c7SWei Liu if (flags & dotl_oflag_map[i].dotl_flag) { 16060ce86c7SWei Liu oflags |= dotl_oflag_map[i].open_flag; 16160ce86c7SWei Liu } 16260ce86c7SWei Liu } 16360ce86c7SWei Liu 16460ce86c7SWei Liu return oflags; 16560ce86c7SWei Liu } 16660ce86c7SWei Liu 16760ce86c7SWei Liu void cred_init(FsCred *credp) 16860ce86c7SWei Liu { 16960ce86c7SWei Liu credp->fc_uid = -1; 17060ce86c7SWei Liu credp->fc_gid = -1; 17160ce86c7SWei Liu credp->fc_mode = -1; 17260ce86c7SWei Liu credp->fc_rdev = -1; 17360ce86c7SWei Liu } 17460ce86c7SWei Liu 17560ce86c7SWei Liu static int get_dotl_openflags(V9fsState *s, int oflags) 17660ce86c7SWei Liu { 17760ce86c7SWei Liu int flags; 17860ce86c7SWei Liu /* 17960ce86c7SWei Liu * Filter the client open flags 18060ce86c7SWei Liu */ 18160ce86c7SWei Liu flags = dotl_to_open_flags(oflags); 18260ce86c7SWei Liu flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT); 18367a71e3bSKeno Fischer #ifndef CONFIG_DARWIN 18460ce86c7SWei Liu /* 18560ce86c7SWei Liu * Ignore direct disk access hint until the server supports it. 18660ce86c7SWei Liu */ 18760ce86c7SWei Liu flags &= ~O_DIRECT; 18867a71e3bSKeno Fischer #endif 18960ce86c7SWei Liu return flags; 19060ce86c7SWei Liu } 19160ce86c7SWei Liu 19260ce86c7SWei Liu void v9fs_path_init(V9fsPath *path) 19360ce86c7SWei Liu { 19460ce86c7SWei Liu path->data = NULL; 19560ce86c7SWei Liu path->size = 0; 19660ce86c7SWei Liu } 19760ce86c7SWei Liu 19860ce86c7SWei Liu void v9fs_path_free(V9fsPath *path) 19960ce86c7SWei Liu { 20060ce86c7SWei Liu g_free(path->data); 20160ce86c7SWei Liu path->data = NULL; 20260ce86c7SWei Liu path->size = 0; 20360ce86c7SWei Liu } 20460ce86c7SWei Liu 205e3e83f2eSGreg Kurz 2069edc6313SMarc-André Lureau void G_GNUC_PRINTF(2, 3) 207e3e83f2eSGreg Kurz v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...) 208e3e83f2eSGreg Kurz { 209e3e83f2eSGreg Kurz va_list ap; 210e3e83f2eSGreg Kurz 211e3e83f2eSGreg Kurz v9fs_path_free(path); 212e3e83f2eSGreg Kurz 213e3e83f2eSGreg Kurz va_start(ap, fmt); 214e3e83f2eSGreg Kurz /* Bump the size for including terminating NULL */ 215e3e83f2eSGreg Kurz path->size = g_vasprintf(&path->data, fmt, ap) + 1; 216e3e83f2eSGreg Kurz va_end(ap); 217e3e83f2eSGreg Kurz } 218e3e83f2eSGreg Kurz 219e446a1ebSMarc-André Lureau void v9fs_path_copy(V9fsPath *dst, const V9fsPath *src) 22060ce86c7SWei Liu { 221e446a1ebSMarc-André Lureau v9fs_path_free(dst); 222e446a1ebSMarc-André Lureau dst->size = src->size; 223e446a1ebSMarc-André Lureau dst->data = g_memdup(src->data, src->size); 22460ce86c7SWei Liu } 22560ce86c7SWei Liu 22660ce86c7SWei Liu int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath, 22760ce86c7SWei Liu const char *name, V9fsPath *path) 22860ce86c7SWei Liu { 22960ce86c7SWei Liu int err; 23060ce86c7SWei Liu err = s->ops->name_to_path(&s->ctx, dirpath, name, path); 23160ce86c7SWei Liu if (err < 0) { 23260ce86c7SWei Liu err = -errno; 23360ce86c7SWei Liu } 23460ce86c7SWei Liu return err; 23560ce86c7SWei Liu } 23660ce86c7SWei Liu 23760ce86c7SWei Liu /* 23860ce86c7SWei Liu * Return TRUE if s1 is an ancestor of s2. 23960ce86c7SWei Liu * 24060ce86c7SWei Liu * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d". 24160ce86c7SWei Liu * As a special case, We treat s1 as ancestor of s2 if they are same! 24260ce86c7SWei Liu */ 24360ce86c7SWei Liu static int v9fs_path_is_ancestor(V9fsPath *s1, V9fsPath *s2) 24460ce86c7SWei Liu { 24560ce86c7SWei Liu if (!strncmp(s1->data, s2->data, s1->size - 1)) { 24660ce86c7SWei Liu if (s2->data[s1->size - 1] == '\0' || s2->data[s1->size - 1] == '/') { 24760ce86c7SWei Liu return 1; 24860ce86c7SWei Liu } 24960ce86c7SWei Liu } 25060ce86c7SWei Liu return 0; 25160ce86c7SWei Liu } 25260ce86c7SWei Liu 25360ce86c7SWei Liu static size_t v9fs_string_size(V9fsString *str) 25460ce86c7SWei Liu { 25560ce86c7SWei Liu return str->size; 25660ce86c7SWei Liu } 25760ce86c7SWei Liu 25860ce86c7SWei Liu /* 25960ce86c7SWei Liu * returns 0 if fid got re-opened, 1 if not, < 0 on error */ 2608440e22eSGreg Kurz static int coroutine_fn v9fs_reopen_fid(V9fsPDU *pdu, V9fsFidState *f) 26160ce86c7SWei Liu { 26260ce86c7SWei Liu int err = 1; 26360ce86c7SWei Liu if (f->fid_type == P9_FID_FILE) { 26460ce86c7SWei Liu if (f->fs.fd == -1) { 26560ce86c7SWei Liu do { 26660ce86c7SWei Liu err = v9fs_co_open(pdu, f, f->open_flags); 26760ce86c7SWei Liu } while (err == -EINTR && !pdu->cancelled); 26860ce86c7SWei Liu } 26960ce86c7SWei Liu } else if (f->fid_type == P9_FID_DIR) { 270f314ea4eSGreg Kurz if (f->fs.dir.stream == NULL) { 27160ce86c7SWei Liu do { 27260ce86c7SWei Liu err = v9fs_co_opendir(pdu, f); 27360ce86c7SWei Liu } while (err == -EINTR && !pdu->cancelled); 27460ce86c7SWei Liu } 27560ce86c7SWei Liu } 27660ce86c7SWei Liu return err; 27760ce86c7SWei Liu } 27860ce86c7SWei Liu 2798440e22eSGreg Kurz static V9fsFidState *coroutine_fn get_fid(V9fsPDU *pdu, int32_t fid) 28060ce86c7SWei Liu { 28160ce86c7SWei Liu int err; 28260ce86c7SWei Liu V9fsFidState *f; 28360ce86c7SWei Liu V9fsState *s = pdu->s; 28460ce86c7SWei Liu 285feabd6cfSGreg Kurz QSIMPLEQ_FOREACH(f, &s->fid_list, next) { 28660ce86c7SWei Liu BUG_ON(f->clunked); 28760ce86c7SWei Liu if (f->fid == fid) { 28860ce86c7SWei Liu /* 28960ce86c7SWei Liu * Update the fid ref upfront so that 29060ce86c7SWei Liu * we don't get reclaimed when we yield 29160ce86c7SWei Liu * in open later. 29260ce86c7SWei Liu */ 29360ce86c7SWei Liu f->ref++; 29460ce86c7SWei Liu /* 29560ce86c7SWei Liu * check whether we need to reopen the 29660ce86c7SWei Liu * file. We might have closed the fd 29760ce86c7SWei Liu * while trying to free up some file 29860ce86c7SWei Liu * descriptors. 29960ce86c7SWei Liu */ 30060ce86c7SWei Liu err = v9fs_reopen_fid(pdu, f); 30160ce86c7SWei Liu if (err < 0) { 30260ce86c7SWei Liu f->ref--; 30360ce86c7SWei Liu return NULL; 30460ce86c7SWei Liu } 30560ce86c7SWei Liu /* 30660ce86c7SWei Liu * Mark the fid as referenced so that the LRU 30760ce86c7SWei Liu * reclaim won't close the file descriptor 30860ce86c7SWei Liu */ 30960ce86c7SWei Liu f->flags |= FID_REFERENCED; 31060ce86c7SWei Liu return f; 31160ce86c7SWei Liu } 31260ce86c7SWei Liu } 31360ce86c7SWei Liu return NULL; 31460ce86c7SWei Liu } 31560ce86c7SWei Liu 31660ce86c7SWei Liu static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid) 31760ce86c7SWei Liu { 31860ce86c7SWei Liu V9fsFidState *f; 31960ce86c7SWei Liu 320feabd6cfSGreg Kurz QSIMPLEQ_FOREACH(f, &s->fid_list, next) { 32160ce86c7SWei Liu /* If fid is already there return NULL */ 32260ce86c7SWei Liu BUG_ON(f->clunked); 32360ce86c7SWei Liu if (f->fid == fid) { 32460ce86c7SWei Liu return NULL; 32560ce86c7SWei Liu } 32660ce86c7SWei Liu } 3271366244aSMarkus Armbruster f = g_new0(V9fsFidState, 1); 32860ce86c7SWei Liu f->fid = fid; 32960ce86c7SWei Liu f->fid_type = P9_FID_NONE; 33060ce86c7SWei Liu f->ref = 1; 33160ce86c7SWei Liu /* 33260ce86c7SWei Liu * Mark the fid as referenced so that the LRU 33360ce86c7SWei Liu * reclaim won't close the file descriptor 33460ce86c7SWei Liu */ 33560ce86c7SWei Liu f->flags |= FID_REFERENCED; 33620b7f45bSGreg Kurz QSIMPLEQ_INSERT_TAIL(&s->fid_list, f, next); 33760ce86c7SWei Liu 338d2c5cf7cSChristian Schoenebeck v9fs_readdir_init(s->proto_version, &f->fs.dir); 339d2c5cf7cSChristian Schoenebeck v9fs_readdir_init(s->proto_version, &f->fs_reclaim.dir); 3407cde47d4SGreg Kurz 34160ce86c7SWei Liu return f; 34260ce86c7SWei Liu } 34360ce86c7SWei Liu 3448440e22eSGreg Kurz static int coroutine_fn v9fs_xattr_fid_clunk(V9fsPDU *pdu, V9fsFidState *fidp) 34560ce86c7SWei Liu { 34660ce86c7SWei Liu int retval = 0; 34760ce86c7SWei Liu 348dd28fbbcSLi Qiang if (fidp->fs.xattr.xattrwalk_fid) { 34960ce86c7SWei Liu /* getxattr/listxattr fid */ 35060ce86c7SWei Liu goto free_value; 35160ce86c7SWei Liu } 35260ce86c7SWei Liu /* 35360ce86c7SWei Liu * if this is fid for setxattr. clunk should 35460ce86c7SWei Liu * result in setxattr localcall 35560ce86c7SWei Liu */ 35660ce86c7SWei Liu if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) { 35760ce86c7SWei Liu /* clunk after partial write */ 35860ce86c7SWei Liu retval = -EINVAL; 35960ce86c7SWei Liu goto free_out; 36060ce86c7SWei Liu } 36160ce86c7SWei Liu if (fidp->fs.xattr.len) { 36260ce86c7SWei Liu retval = v9fs_co_lsetxattr(pdu, &fidp->path, &fidp->fs.xattr.name, 36360ce86c7SWei Liu fidp->fs.xattr.value, 36460ce86c7SWei Liu fidp->fs.xattr.len, 36560ce86c7SWei Liu fidp->fs.xattr.flags); 36660ce86c7SWei Liu } else { 36760ce86c7SWei Liu retval = v9fs_co_lremovexattr(pdu, &fidp->path, &fidp->fs.xattr.name); 36860ce86c7SWei Liu } 36960ce86c7SWei Liu free_out: 37060ce86c7SWei Liu v9fs_string_free(&fidp->fs.xattr.name); 37160ce86c7SWei Liu free_value: 37260ce86c7SWei Liu g_free(fidp->fs.xattr.value); 37360ce86c7SWei Liu return retval; 37460ce86c7SWei Liu } 37560ce86c7SWei Liu 3768440e22eSGreg Kurz static int coroutine_fn free_fid(V9fsPDU *pdu, V9fsFidState *fidp) 37760ce86c7SWei Liu { 37860ce86c7SWei Liu int retval = 0; 37960ce86c7SWei Liu 38060ce86c7SWei Liu if (fidp->fid_type == P9_FID_FILE) { 38160ce86c7SWei Liu /* If we reclaimed the fd no need to close */ 38260ce86c7SWei Liu if (fidp->fs.fd != -1) { 38360ce86c7SWei Liu retval = v9fs_co_close(pdu, &fidp->fs); 38460ce86c7SWei Liu } 38560ce86c7SWei Liu } else if (fidp->fid_type == P9_FID_DIR) { 386f314ea4eSGreg Kurz if (fidp->fs.dir.stream != NULL) { 38760ce86c7SWei Liu retval = v9fs_co_closedir(pdu, &fidp->fs); 38860ce86c7SWei Liu } 38960ce86c7SWei Liu } else if (fidp->fid_type == P9_FID_XATTR) { 39060ce86c7SWei Liu retval = v9fs_xattr_fid_clunk(pdu, fidp); 39160ce86c7SWei Liu } 39260ce86c7SWei Liu v9fs_path_free(&fidp->path); 39360ce86c7SWei Liu g_free(fidp); 39460ce86c7SWei Liu return retval; 39560ce86c7SWei Liu } 39660ce86c7SWei Liu 3978440e22eSGreg Kurz static int coroutine_fn put_fid(V9fsPDU *pdu, V9fsFidState *fidp) 39860ce86c7SWei Liu { 39960ce86c7SWei Liu BUG_ON(!fidp->ref); 40060ce86c7SWei Liu fidp->ref--; 40160ce86c7SWei Liu /* 40260ce86c7SWei Liu * Don't free the fid if it is in reclaim list 40360ce86c7SWei Liu */ 40460ce86c7SWei Liu if (!fidp->ref && fidp->clunked) { 40560ce86c7SWei Liu if (fidp->fid == pdu->s->root_fid) { 40660ce86c7SWei Liu /* 40760ce86c7SWei Liu * if the clunked fid is root fid then we 40860ce86c7SWei Liu * have unmounted the fs on the client side. 40960ce86c7SWei Liu * delete the migration blocker. Ideally, this 41060ce86c7SWei Liu * should be hooked to transport close notification 41160ce86c7SWei Liu */ 41260ce86c7SWei Liu if (pdu->s->migration_blocker) { 41360ce86c7SWei Liu migrate_del_blocker(pdu->s->migration_blocker); 41460ce86c7SWei Liu error_free(pdu->s->migration_blocker); 41560ce86c7SWei Liu pdu->s->migration_blocker = NULL; 41660ce86c7SWei Liu } 41760ce86c7SWei Liu } 41860ce86c7SWei Liu return free_fid(pdu, fidp); 41960ce86c7SWei Liu } 42060ce86c7SWei Liu return 0; 42160ce86c7SWei Liu } 42260ce86c7SWei Liu 42360ce86c7SWei Liu static V9fsFidState *clunk_fid(V9fsState *s, int32_t fid) 42460ce86c7SWei Liu { 425feabd6cfSGreg Kurz V9fsFidState *fidp; 42660ce86c7SWei Liu 427feabd6cfSGreg Kurz QSIMPLEQ_FOREACH(fidp, &s->fid_list, next) { 428feabd6cfSGreg Kurz if (fidp->fid == fid) { 429feabd6cfSGreg Kurz QSIMPLEQ_REMOVE(&s->fid_list, fidp, V9fsFidState, next); 4302e53160fSGreg Kurz fidp->clunked = true; 43160ce86c7SWei Liu return fidp; 43260ce86c7SWei Liu } 433feabd6cfSGreg Kurz } 434feabd6cfSGreg Kurz return NULL; 435feabd6cfSGreg Kurz } 43660ce86c7SWei Liu 4378440e22eSGreg Kurz void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu) 43860ce86c7SWei Liu { 43960ce86c7SWei Liu int reclaim_count = 0; 44060ce86c7SWei Liu V9fsState *s = pdu->s; 44181f9766bSGreg Kurz V9fsFidState *f; 44281f9766bSGreg Kurz QSLIST_HEAD(, V9fsFidState) reclaim_list = 44381f9766bSGreg Kurz QSLIST_HEAD_INITIALIZER(reclaim_list); 44460ce86c7SWei Liu 445feabd6cfSGreg Kurz QSIMPLEQ_FOREACH(f, &s->fid_list, next) { 44660ce86c7SWei Liu /* 44760ce86c7SWei Liu * Unlink fids cannot be reclaimed. Check 44860ce86c7SWei Liu * for them and skip them. Also skip fids 44960ce86c7SWei Liu * currently being operated on. 45060ce86c7SWei Liu */ 45160ce86c7SWei Liu if (f->ref || f->flags & FID_NON_RECLAIMABLE) { 45260ce86c7SWei Liu continue; 45360ce86c7SWei Liu } 45460ce86c7SWei Liu /* 45560ce86c7SWei Liu * if it is a recently referenced fid 45660ce86c7SWei Liu * we leave the fid untouched and clear the 45760ce86c7SWei Liu * reference bit. We come back to it later 45860ce86c7SWei Liu * in the next iteration. (a simple LRU without 45960ce86c7SWei Liu * moving list elements around) 46060ce86c7SWei Liu */ 46160ce86c7SWei Liu if (f->flags & FID_REFERENCED) { 46260ce86c7SWei Liu f->flags &= ~FID_REFERENCED; 46360ce86c7SWei Liu continue; 46460ce86c7SWei Liu } 46560ce86c7SWei Liu /* 46660ce86c7SWei Liu * Add fids to reclaim list. 46760ce86c7SWei Liu */ 46860ce86c7SWei Liu if (f->fid_type == P9_FID_FILE) { 46960ce86c7SWei Liu if (f->fs.fd != -1) { 47060ce86c7SWei Liu /* 47160ce86c7SWei Liu * Up the reference count so that 47260ce86c7SWei Liu * a clunk request won't free this fid 47360ce86c7SWei Liu */ 47460ce86c7SWei Liu f->ref++; 47581f9766bSGreg Kurz QSLIST_INSERT_HEAD(&reclaim_list, f, reclaim_next); 47660ce86c7SWei Liu f->fs_reclaim.fd = f->fs.fd; 47760ce86c7SWei Liu f->fs.fd = -1; 47860ce86c7SWei Liu reclaim_count++; 47960ce86c7SWei Liu } 48060ce86c7SWei Liu } else if (f->fid_type == P9_FID_DIR) { 481f314ea4eSGreg Kurz if (f->fs.dir.stream != NULL) { 48260ce86c7SWei Liu /* 48360ce86c7SWei Liu * Up the reference count so that 48460ce86c7SWei Liu * a clunk request won't free this fid 48560ce86c7SWei Liu */ 48660ce86c7SWei Liu f->ref++; 48781f9766bSGreg Kurz QSLIST_INSERT_HEAD(&reclaim_list, f, reclaim_next); 488f314ea4eSGreg Kurz f->fs_reclaim.dir.stream = f->fs.dir.stream; 489f314ea4eSGreg Kurz f->fs.dir.stream = NULL; 49060ce86c7SWei Liu reclaim_count++; 49160ce86c7SWei Liu } 49260ce86c7SWei Liu } 49360ce86c7SWei Liu if (reclaim_count >= open_fd_rc) { 49460ce86c7SWei Liu break; 49560ce86c7SWei Liu } 49660ce86c7SWei Liu } 49760ce86c7SWei Liu /* 49860ce86c7SWei Liu * Now close the fid in reclaim list. Free them if they 49960ce86c7SWei Liu * are already clunked. 50060ce86c7SWei Liu */ 50181f9766bSGreg Kurz while (!QSLIST_EMPTY(&reclaim_list)) { 50281f9766bSGreg Kurz f = QSLIST_FIRST(&reclaim_list); 50381f9766bSGreg Kurz QSLIST_REMOVE(&reclaim_list, f, V9fsFidState, reclaim_next); 50460ce86c7SWei Liu if (f->fid_type == P9_FID_FILE) { 50560ce86c7SWei Liu v9fs_co_close(pdu, &f->fs_reclaim); 50660ce86c7SWei Liu } else if (f->fid_type == P9_FID_DIR) { 50760ce86c7SWei Liu v9fs_co_closedir(pdu, &f->fs_reclaim); 50860ce86c7SWei Liu } 50960ce86c7SWei Liu /* 51060ce86c7SWei Liu * Now drop the fid reference, free it 51160ce86c7SWei Liu * if clunked. 51260ce86c7SWei Liu */ 51360ce86c7SWei Liu put_fid(pdu, f); 51460ce86c7SWei Liu } 51560ce86c7SWei Liu } 51660ce86c7SWei Liu 5178440e22eSGreg Kurz static int coroutine_fn v9fs_mark_fids_unreclaim(V9fsPDU *pdu, V9fsPath *path) 51860ce86c7SWei Liu { 51960ce86c7SWei Liu int err; 52060ce86c7SWei Liu V9fsState *s = pdu->s; 52120b7f45bSGreg Kurz V9fsFidState *fidp, *fidp_next; 52260ce86c7SWei Liu 52320b7f45bSGreg Kurz fidp = QSIMPLEQ_FIRST(&s->fid_list); 52420b7f45bSGreg Kurz if (!fidp) { 52520b7f45bSGreg Kurz return 0; 52660ce86c7SWei Liu } 52720b7f45bSGreg Kurz 52820b7f45bSGreg Kurz /* 52920b7f45bSGreg Kurz * v9fs_reopen_fid() can yield : a reference on the fid must be held 53020b7f45bSGreg Kurz * to ensure its pointer remains valid and we can safely pass it to 53120b7f45bSGreg Kurz * QSIMPLEQ_NEXT(). The corresponding put_fid() can also yield so 53220b7f45bSGreg Kurz * we must keep a reference on the next fid as well. So the logic here 53320b7f45bSGreg Kurz * is to get a reference on a fid and only put it back during the next 53420b7f45bSGreg Kurz * iteration after we could get a reference on the next fid. Start with 53520b7f45bSGreg Kurz * the first one. 53620b7f45bSGreg Kurz */ 53720b7f45bSGreg Kurz for (fidp->ref++; fidp; fidp = fidp_next) { 53820b7f45bSGreg Kurz if (fidp->path.size == path->size && 53920b7f45bSGreg Kurz !memcmp(fidp->path.data, path->data, path->size)) { 54060ce86c7SWei Liu /* Mark the fid non reclaimable. */ 54160ce86c7SWei Liu fidp->flags |= FID_NON_RECLAIMABLE; 54260ce86c7SWei Liu 54360ce86c7SWei Liu /* reopen the file/dir if already closed */ 54460ce86c7SWei Liu err = v9fs_reopen_fid(pdu, fidp); 54560ce86c7SWei Liu if (err < 0) { 54620b7f45bSGreg Kurz put_fid(pdu, fidp); 547267fcadfSGreg Kurz return err; 54860ce86c7SWei Liu } 54920b7f45bSGreg Kurz } 55020b7f45bSGreg Kurz 55120b7f45bSGreg Kurz fidp_next = QSIMPLEQ_NEXT(fidp, next); 55220b7f45bSGreg Kurz 55320b7f45bSGreg Kurz if (fidp_next) { 55460ce86c7SWei Liu /* 55520b7f45bSGreg Kurz * Ensure the next fid survives a potential clunk request during 55620b7f45bSGreg Kurz * put_fid() below and v9fs_reopen_fid() in the next iteration. 55760ce86c7SWei Liu */ 55820b7f45bSGreg Kurz fidp_next->ref++; 55960ce86c7SWei Liu } 56020b7f45bSGreg Kurz 56120b7f45bSGreg Kurz /* We're done with this fid */ 56220b7f45bSGreg Kurz put_fid(pdu, fidp); 56360ce86c7SWei Liu } 56420b7f45bSGreg Kurz 56560ce86c7SWei Liu return 0; 56660ce86c7SWei Liu } 56760ce86c7SWei Liu 5688440e22eSGreg Kurz static void coroutine_fn virtfs_reset(V9fsPDU *pdu) 56960ce86c7SWei Liu { 57060ce86c7SWei Liu V9fsState *s = pdu->s; 57179decce3SGreg Kurz V9fsFidState *fidp; 57260ce86c7SWei Liu 57360ce86c7SWei Liu /* Free all fids */ 574feabd6cfSGreg Kurz while (!QSIMPLEQ_EMPTY(&s->fid_list)) { 5756d54af0eSGreg Kurz /* Get fid */ 576feabd6cfSGreg Kurz fidp = QSIMPLEQ_FIRST(&s->fid_list); 5776d54af0eSGreg Kurz fidp->ref++; 57860ce86c7SWei Liu 5796d54af0eSGreg Kurz /* Clunk fid */ 580feabd6cfSGreg Kurz QSIMPLEQ_REMOVE(&s->fid_list, fidp, V9fsFidState, next); 5812e53160fSGreg Kurz fidp->clunked = true; 5826d54af0eSGreg Kurz 5836d54af0eSGreg Kurz put_fid(pdu, fidp); 58460ce86c7SWei Liu } 58560ce86c7SWei Liu } 58660ce86c7SWei Liu 58760ce86c7SWei Liu #define P9_QID_TYPE_DIR 0x80 58860ce86c7SWei Liu #define P9_QID_TYPE_SYMLINK 0x02 58960ce86c7SWei Liu 59060ce86c7SWei Liu #define P9_STAT_MODE_DIR 0x80000000 59160ce86c7SWei Liu #define P9_STAT_MODE_APPEND 0x40000000 59260ce86c7SWei Liu #define P9_STAT_MODE_EXCL 0x20000000 59360ce86c7SWei Liu #define P9_STAT_MODE_MOUNT 0x10000000 59460ce86c7SWei Liu #define P9_STAT_MODE_AUTH 0x08000000 59560ce86c7SWei Liu #define P9_STAT_MODE_TMP 0x04000000 59660ce86c7SWei Liu #define P9_STAT_MODE_SYMLINK 0x02000000 59760ce86c7SWei Liu #define P9_STAT_MODE_LINK 0x01000000 59860ce86c7SWei Liu #define P9_STAT_MODE_DEVICE 0x00800000 59960ce86c7SWei Liu #define P9_STAT_MODE_NAMED_PIPE 0x00200000 60060ce86c7SWei Liu #define P9_STAT_MODE_SOCKET 0x00100000 60160ce86c7SWei Liu #define P9_STAT_MODE_SETUID 0x00080000 60260ce86c7SWei Liu #define P9_STAT_MODE_SETGID 0x00040000 60360ce86c7SWei Liu #define P9_STAT_MODE_SETVTX 0x00010000 60460ce86c7SWei Liu 60560ce86c7SWei Liu #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \ 60660ce86c7SWei Liu P9_STAT_MODE_SYMLINK | \ 60760ce86c7SWei Liu P9_STAT_MODE_LINK | \ 60860ce86c7SWei Liu P9_STAT_MODE_DEVICE | \ 60960ce86c7SWei Liu P9_STAT_MODE_NAMED_PIPE | \ 61060ce86c7SWei Liu P9_STAT_MODE_SOCKET) 61160ce86c7SWei Liu 6126b6aa828SChristian Schoenebeck /* Mirrors all bits of a byte. So e.g. binary 10100000 would become 00000101. */ 6136b6aa828SChristian Schoenebeck static inline uint8_t mirror8bit(uint8_t byte) 6146b6aa828SChristian Schoenebeck { 6156b6aa828SChristian Schoenebeck return (byte * 0x0202020202ULL & 0x010884422010ULL) % 1023; 6166b6aa828SChristian Schoenebeck } 6176b6aa828SChristian Schoenebeck 6186b6aa828SChristian Schoenebeck /* Same as mirror8bit() just for a 64 bit data type instead for a byte. */ 6196b6aa828SChristian Schoenebeck static inline uint64_t mirror64bit(uint64_t value) 6206b6aa828SChristian Schoenebeck { 6216b6aa828SChristian Schoenebeck return ((uint64_t)mirror8bit(value & 0xff) << 56) | 6226b6aa828SChristian Schoenebeck ((uint64_t)mirror8bit((value >> 8) & 0xff) << 48) | 6236b6aa828SChristian Schoenebeck ((uint64_t)mirror8bit((value >> 16) & 0xff) << 40) | 6246b6aa828SChristian Schoenebeck ((uint64_t)mirror8bit((value >> 24) & 0xff) << 32) | 6256b6aa828SChristian Schoenebeck ((uint64_t)mirror8bit((value >> 32) & 0xff) << 24) | 6266b6aa828SChristian Schoenebeck ((uint64_t)mirror8bit((value >> 40) & 0xff) << 16) | 6276b6aa828SChristian Schoenebeck ((uint64_t)mirror8bit((value >> 48) & 0xff) << 8) | 6286b6aa828SChristian Schoenebeck ((uint64_t)mirror8bit((value >> 56) & 0xff)); 6296b6aa828SChristian Schoenebeck } 6306b6aa828SChristian Schoenebeck 631e16fea41SChristian Schoenebeck /* 632e16fea41SChristian Schoenebeck * Parameter k for the Exponential Golomb algorihm to be used. 6336b6aa828SChristian Schoenebeck * 6346b6aa828SChristian Schoenebeck * The smaller this value, the smaller the minimum bit count for the Exp. 6356b6aa828SChristian Schoenebeck * Golomb generated affixes will be (at lowest index) however for the 6366b6aa828SChristian Schoenebeck * price of having higher maximum bit count of generated affixes (at highest 6376b6aa828SChristian Schoenebeck * index). Likewise increasing this parameter yields in smaller maximum bit 6386b6aa828SChristian Schoenebeck * count for the price of having higher minimum bit count. 6396b6aa828SChristian Schoenebeck * 6406b6aa828SChristian Schoenebeck * In practice that means: a good value for k depends on the expected amount 6416b6aa828SChristian Schoenebeck * of devices to be exposed by one export. For a small amount of devices k 6426b6aa828SChristian Schoenebeck * should be small, for a large amount of devices k might be increased 6436b6aa828SChristian Schoenebeck * instead. The default of k=0 should be fine for most users though. 6446b6aa828SChristian Schoenebeck * 645e16fea41SChristian Schoenebeck * IMPORTANT: In case this ever becomes a runtime parameter; the value of 6466b6aa828SChristian Schoenebeck * k should not change as long as guest is still running! Because that would 6476b6aa828SChristian Schoenebeck * cause completely different inode numbers to be generated on guest. 6486b6aa828SChristian Schoenebeck */ 6496b6aa828SChristian Schoenebeck #define EXP_GOLOMB_K 0 6506b6aa828SChristian Schoenebeck 6516b6aa828SChristian Schoenebeck /** 652e16fea41SChristian Schoenebeck * expGolombEncode() - Exponential Golomb algorithm for arbitrary k 653e16fea41SChristian Schoenebeck * (including k=0). 6546b6aa828SChristian Schoenebeck * 655e16fea41SChristian Schoenebeck * @n: natural number (or index) of the prefix to be generated 656e16fea41SChristian Schoenebeck * (1, 2, 3, ...) 657e16fea41SChristian Schoenebeck * @k: parameter k of Exp. Golomb algorithm to be used 658e16fea41SChristian Schoenebeck * (see comment on EXP_GOLOMB_K macro for details about k) 659e16fea41SChristian Schoenebeck * Return: prefix for given @n and @k 660e16fea41SChristian Schoenebeck * 661e16fea41SChristian Schoenebeck * The Exponential Golomb algorithm generates prefixes (NOT suffixes!) 6626b6aa828SChristian Schoenebeck * with growing length and with the mathematical property of being 6636b6aa828SChristian Schoenebeck * "prefix-free". The latter means the generated prefixes can be prepended 6646b6aa828SChristian Schoenebeck * in front of arbitrary numbers and the resulting concatenated numbers are 6656b6aa828SChristian Schoenebeck * guaranteed to be always unique. 6666b6aa828SChristian Schoenebeck * 6676b6aa828SChristian Schoenebeck * This is a minor adjustment to the original Exp. Golomb algorithm in the 668e16fea41SChristian Schoenebeck * sense that lowest allowed index (@n) starts with 1, not with zero. 6696b6aa828SChristian Schoenebeck */ 6706b6aa828SChristian Schoenebeck static VariLenAffix expGolombEncode(uint64_t n, int k) 6716b6aa828SChristian Schoenebeck { 6726b6aa828SChristian Schoenebeck const uint64_t value = n + (1 << k) - 1; 6736b6aa828SChristian Schoenebeck const int bits = (int) log2(value) + 1; 6746b6aa828SChristian Schoenebeck return (VariLenAffix) { 6756b6aa828SChristian Schoenebeck .type = AffixType_Prefix, 6766b6aa828SChristian Schoenebeck .value = value, 6776b6aa828SChristian Schoenebeck .bits = bits + MAX((bits - 1 - k), 0) 6786b6aa828SChristian Schoenebeck }; 6796b6aa828SChristian Schoenebeck } 6806b6aa828SChristian Schoenebeck 6816b6aa828SChristian Schoenebeck /** 682e16fea41SChristian Schoenebeck * invertAffix() - Converts a suffix into a prefix, or a prefix into a suffix. 683e16fea41SChristian Schoenebeck * @affix: either suffix or prefix to be inverted 684e16fea41SChristian Schoenebeck * Return: inversion of passed @affix 6856b6aa828SChristian Schoenebeck * 6866b6aa828SChristian Schoenebeck * Simply mirror all bits of the affix value, for the purpose to preserve 6876b6aa828SChristian Schoenebeck * respectively the mathematical "prefix-free" or "suffix-free" property 6886b6aa828SChristian Schoenebeck * after the conversion. 6896b6aa828SChristian Schoenebeck * 6906b6aa828SChristian Schoenebeck * If a passed prefix is suitable to create unique numbers, then the 6916b6aa828SChristian Schoenebeck * returned suffix is suitable to create unique numbers as well (and vice 6926b6aa828SChristian Schoenebeck * versa). 6936b6aa828SChristian Schoenebeck */ 6946b6aa828SChristian Schoenebeck static VariLenAffix invertAffix(const VariLenAffix *affix) 6956b6aa828SChristian Schoenebeck { 6966b6aa828SChristian Schoenebeck return (VariLenAffix) { 6976b6aa828SChristian Schoenebeck .type = 6986b6aa828SChristian Schoenebeck (affix->type == AffixType_Suffix) ? 6996b6aa828SChristian Schoenebeck AffixType_Prefix : AffixType_Suffix, 7006b6aa828SChristian Schoenebeck .value = 7016b6aa828SChristian Schoenebeck mirror64bit(affix->value) >> 7026b6aa828SChristian Schoenebeck ((sizeof(affix->value) * 8) - affix->bits), 7036b6aa828SChristian Schoenebeck .bits = affix->bits 7046b6aa828SChristian Schoenebeck }; 7056b6aa828SChristian Schoenebeck } 7066b6aa828SChristian Schoenebeck 7076b6aa828SChristian Schoenebeck /** 708e16fea41SChristian Schoenebeck * affixForIndex() - Generates suffix numbers with "suffix-free" property. 709e16fea41SChristian Schoenebeck * @index: natural number (or index) of the suffix to be generated 710e16fea41SChristian Schoenebeck * (1, 2, 3, ...) 711e16fea41SChristian Schoenebeck * Return: Suffix suitable to assemble unique number. 7126b6aa828SChristian Schoenebeck * 7136b6aa828SChristian Schoenebeck * This is just a wrapper function on top of the Exp. Golomb algorithm. 7146b6aa828SChristian Schoenebeck * 7156b6aa828SChristian Schoenebeck * Since the Exp. Golomb algorithm generates prefixes, but we need suffixes, 7166b6aa828SChristian Schoenebeck * this function converts the Exp. Golomb prefixes into appropriate suffixes 7176b6aa828SChristian Schoenebeck * which are still suitable for generating unique numbers. 7186b6aa828SChristian Schoenebeck */ 7196b6aa828SChristian Schoenebeck static VariLenAffix affixForIndex(uint64_t index) 7206b6aa828SChristian Schoenebeck { 7216b6aa828SChristian Schoenebeck VariLenAffix prefix; 7226b6aa828SChristian Schoenebeck prefix = expGolombEncode(index, EXP_GOLOMB_K); 7236b6aa828SChristian Schoenebeck return invertAffix(&prefix); /* convert prefix to suffix */ 7246b6aa828SChristian Schoenebeck } 7256b6aa828SChristian Schoenebeck 7261a6ed33cSAntonios Motakis /* creative abuse of tb_hash_func7, which is based on xxhash */ 7271a6ed33cSAntonios Motakis static uint32_t qpp_hash(QppEntry e) 7281a6ed33cSAntonios Motakis { 7291a6ed33cSAntonios Motakis return qemu_xxhash7(e.ino_prefix, e.dev, 0, 0, 0); 7301a6ed33cSAntonios Motakis } 7311a6ed33cSAntonios Motakis 732f3fe4a2dSAntonios Motakis static uint32_t qpf_hash(QpfEntry e) 733f3fe4a2dSAntonios Motakis { 734f3fe4a2dSAntonios Motakis return qemu_xxhash7(e.ino, e.dev, 0, 0, 0); 735f3fe4a2dSAntonios Motakis } 736f3fe4a2dSAntonios Motakis 7376b6aa828SChristian Schoenebeck static bool qpd_cmp_func(const void *obj, const void *userp) 7386b6aa828SChristian Schoenebeck { 7396b6aa828SChristian Schoenebeck const QpdEntry *e1 = obj, *e2 = userp; 7406b6aa828SChristian Schoenebeck return e1->dev == e2->dev; 7416b6aa828SChristian Schoenebeck } 7426b6aa828SChristian Schoenebeck 7436b6aa828SChristian Schoenebeck static bool qpp_cmp_func(const void *obj, const void *userp) 7441a6ed33cSAntonios Motakis { 7451a6ed33cSAntonios Motakis const QppEntry *e1 = obj, *e2 = userp; 7461a6ed33cSAntonios Motakis return e1->dev == e2->dev && e1->ino_prefix == e2->ino_prefix; 7471a6ed33cSAntonios Motakis } 7481a6ed33cSAntonios Motakis 7496b6aa828SChristian Schoenebeck static bool qpf_cmp_func(const void *obj, const void *userp) 750f3fe4a2dSAntonios Motakis { 751f3fe4a2dSAntonios Motakis const QpfEntry *e1 = obj, *e2 = userp; 752f3fe4a2dSAntonios Motakis return e1->dev == e2->dev && e1->ino == e2->ino; 753f3fe4a2dSAntonios Motakis } 754f3fe4a2dSAntonios Motakis 755f3fe4a2dSAntonios Motakis static void qp_table_remove(void *p, uint32_t h, void *up) 7561a6ed33cSAntonios Motakis { 7571a6ed33cSAntonios Motakis g_free(p); 7581a6ed33cSAntonios Motakis } 7591a6ed33cSAntonios Motakis 760f3fe4a2dSAntonios Motakis static void qp_table_destroy(struct qht *ht) 7611a6ed33cSAntonios Motakis { 7621a6ed33cSAntonios Motakis if (!ht || !ht->map) { 7631a6ed33cSAntonios Motakis return; 7641a6ed33cSAntonios Motakis } 765f3fe4a2dSAntonios Motakis qht_iter(ht, qp_table_remove, NULL); 7661a6ed33cSAntonios Motakis qht_destroy(ht); 7671a6ed33cSAntonios Motakis } 7681a6ed33cSAntonios Motakis 7696b6aa828SChristian Schoenebeck static void qpd_table_init(struct qht *ht) 7706b6aa828SChristian Schoenebeck { 7716b6aa828SChristian Schoenebeck qht_init(ht, qpd_cmp_func, 1, QHT_MODE_AUTO_RESIZE); 7726b6aa828SChristian Schoenebeck } 7736b6aa828SChristian Schoenebeck 7741a6ed33cSAntonios Motakis static void qpp_table_init(struct qht *ht) 7751a6ed33cSAntonios Motakis { 7766b6aa828SChristian Schoenebeck qht_init(ht, qpp_cmp_func, 1, QHT_MODE_AUTO_RESIZE); 7771a6ed33cSAntonios Motakis } 7781a6ed33cSAntonios Motakis 779f3fe4a2dSAntonios Motakis static void qpf_table_init(struct qht *ht) 780f3fe4a2dSAntonios Motakis { 7816b6aa828SChristian Schoenebeck qht_init(ht, qpf_cmp_func, 1 << 16, QHT_MODE_AUTO_RESIZE); 782f3fe4a2dSAntonios Motakis } 783f3fe4a2dSAntonios Motakis 7846b6aa828SChristian Schoenebeck /* 7856b6aa828SChristian Schoenebeck * Returns how many (high end) bits of inode numbers of the passed fs 7866b6aa828SChristian Schoenebeck * device shall be used (in combination with the device number) to 7876b6aa828SChristian Schoenebeck * generate hash values for qpp_table entries. 7886b6aa828SChristian Schoenebeck * 7896b6aa828SChristian Schoenebeck * This function is required if variable length suffixes are used for inode 7906b6aa828SChristian Schoenebeck * number mapping on guest level. Since a device may end up having multiple 7916b6aa828SChristian Schoenebeck * entries in qpp_table, each entry most probably with a different suffix 7926b6aa828SChristian Schoenebeck * length, we thus need this function in conjunction with qpd_table to 7936b6aa828SChristian Schoenebeck * "agree" about a fix amount of bits (per device) to be always used for 7946b6aa828SChristian Schoenebeck * generating hash values for the purpose of accessing qpp_table in order 7956b6aa828SChristian Schoenebeck * get consistent behaviour when accessing qpp_table. 7966b6aa828SChristian Schoenebeck */ 7976b6aa828SChristian Schoenebeck static int qid_inode_prefix_hash_bits(V9fsPDU *pdu, dev_t dev) 7986b6aa828SChristian Schoenebeck { 7996b6aa828SChristian Schoenebeck QpdEntry lookup = { 8006b6aa828SChristian Schoenebeck .dev = dev 8016b6aa828SChristian Schoenebeck }, *val; 8026b6aa828SChristian Schoenebeck uint32_t hash = dev; 8036b6aa828SChristian Schoenebeck VariLenAffix affix; 8046b6aa828SChristian Schoenebeck 8056b6aa828SChristian Schoenebeck val = qht_lookup(&pdu->s->qpd_table, &lookup, hash); 8066b6aa828SChristian Schoenebeck if (!val) { 8071366244aSMarkus Armbruster val = g_new0(QpdEntry, 1); 8086b6aa828SChristian Schoenebeck *val = lookup; 8096b6aa828SChristian Schoenebeck affix = affixForIndex(pdu->s->qp_affix_next); 8106b6aa828SChristian Schoenebeck val->prefix_bits = affix.bits; 8116b6aa828SChristian Schoenebeck qht_insert(&pdu->s->qpd_table, val, hash, NULL); 8126b6aa828SChristian Schoenebeck pdu->s->qp_ndevices++; 8136b6aa828SChristian Schoenebeck } 8146b6aa828SChristian Schoenebeck return val->prefix_bits; 8156b6aa828SChristian Schoenebeck } 8166b6aa828SChristian Schoenebeck 817e16fea41SChristian Schoenebeck /* 818e16fea41SChristian Schoenebeck * Slow / full mapping host inode nr -> guest inode nr. 8196b6aa828SChristian Schoenebeck * 8206b6aa828SChristian Schoenebeck * This function performs a slower and much more costly remapping of an 8216b6aa828SChristian Schoenebeck * original file inode number on host to an appropriate different inode 8226b6aa828SChristian Schoenebeck * number on guest. For every (dev, inode) combination on host a new 8236b6aa828SChristian Schoenebeck * sequential number is generated, cached and exposed as inode number on 8246b6aa828SChristian Schoenebeck * guest. 8256b6aa828SChristian Schoenebeck * 8266b6aa828SChristian Schoenebeck * This is just a "last resort" fallback solution if the much faster/cheaper 8276b6aa828SChristian Schoenebeck * qid_path_suffixmap() failed. In practice this slow / full mapping is not 8286b6aa828SChristian Schoenebeck * expected ever to be used at all though. 8296b6aa828SChristian Schoenebeck * 830e16fea41SChristian Schoenebeck * See qid_path_suffixmap() for details 8316b6aa828SChristian Schoenebeck * 8326b6aa828SChristian Schoenebeck */ 833f3fe4a2dSAntonios Motakis static int qid_path_fullmap(V9fsPDU *pdu, const struct stat *stbuf, 834f3fe4a2dSAntonios Motakis uint64_t *path) 835f3fe4a2dSAntonios Motakis { 836f3fe4a2dSAntonios Motakis QpfEntry lookup = { 837f3fe4a2dSAntonios Motakis .dev = stbuf->st_dev, 838f3fe4a2dSAntonios Motakis .ino = stbuf->st_ino 839f3fe4a2dSAntonios Motakis }, *val; 840f3fe4a2dSAntonios Motakis uint32_t hash = qpf_hash(lookup); 8416b6aa828SChristian Schoenebeck VariLenAffix affix; 842f3fe4a2dSAntonios Motakis 843f3fe4a2dSAntonios Motakis val = qht_lookup(&pdu->s->qpf_table, &lookup, hash); 844f3fe4a2dSAntonios Motakis 845f3fe4a2dSAntonios Motakis if (!val) { 846f3fe4a2dSAntonios Motakis if (pdu->s->qp_fullpath_next == 0) { 847f3fe4a2dSAntonios Motakis /* no more files can be mapped :'( */ 848f3fe4a2dSAntonios Motakis error_report_once( 849f3fe4a2dSAntonios Motakis "9p: No more prefixes available for remapping inodes from " 850f3fe4a2dSAntonios Motakis "host to guest." 851f3fe4a2dSAntonios Motakis ); 852f3fe4a2dSAntonios Motakis return -ENFILE; 853f3fe4a2dSAntonios Motakis } 854f3fe4a2dSAntonios Motakis 8551366244aSMarkus Armbruster val = g_new0(QpfEntry, 1); 856f3fe4a2dSAntonios Motakis *val = lookup; 857f3fe4a2dSAntonios Motakis 858f3fe4a2dSAntonios Motakis /* new unique inode and device combo */ 8596b6aa828SChristian Schoenebeck affix = affixForIndex( 8606b6aa828SChristian Schoenebeck 1ULL << (sizeof(pdu->s->qp_affix_next) * 8) 8616b6aa828SChristian Schoenebeck ); 8626b6aa828SChristian Schoenebeck val->path = (pdu->s->qp_fullpath_next++ << affix.bits) | affix.value; 8636b6aa828SChristian Schoenebeck pdu->s->qp_fullpath_next &= ((1ULL << (64 - affix.bits)) - 1); 864f3fe4a2dSAntonios Motakis qht_insert(&pdu->s->qpf_table, val, hash, NULL); 865f3fe4a2dSAntonios Motakis } 866f3fe4a2dSAntonios Motakis 867f3fe4a2dSAntonios Motakis *path = val->path; 868f3fe4a2dSAntonios Motakis return 0; 869f3fe4a2dSAntonios Motakis } 870f3fe4a2dSAntonios Motakis 871e16fea41SChristian Schoenebeck /* 872e16fea41SChristian Schoenebeck * Quick mapping host inode nr -> guest inode nr. 8731a6ed33cSAntonios Motakis * 8746b6aa828SChristian Schoenebeck * This function performs quick remapping of an original file inode number 8756b6aa828SChristian Schoenebeck * on host to an appropriate different inode number on guest. This remapping 8766b6aa828SChristian Schoenebeck * of inodes is required to avoid inode nr collisions on guest which would 8776b6aa828SChristian Schoenebeck * happen if the 9p export contains more than 1 exported file system (or 8786b6aa828SChristian Schoenebeck * more than 1 file system data set), because unlike on host level where the 8796b6aa828SChristian Schoenebeck * files would have different device nrs, all files exported by 9p would 8806b6aa828SChristian Schoenebeck * share the same device nr on guest (the device nr of the virtual 9p device 8816b6aa828SChristian Schoenebeck * that is). 8826b6aa828SChristian Schoenebeck * 8836b6aa828SChristian Schoenebeck * Inode remapping is performed by chopping off high end bits of the original 8846b6aa828SChristian Schoenebeck * inode number from host, shifting the result upwards and then assigning a 8856b6aa828SChristian Schoenebeck * generated suffix number for the low end bits, where the same suffix number 8866b6aa828SChristian Schoenebeck * will be shared by all inodes with the same device id AND the same high end 8876b6aa828SChristian Schoenebeck * bits that have been chopped off. That approach utilizes the fact that inode 8886b6aa828SChristian Schoenebeck * numbers very likely share the same high end bits (i.e. due to their common 8896b6aa828SChristian Schoenebeck * sequential generation by file systems) and hence we only have to generate 8906b6aa828SChristian Schoenebeck * and track a very limited amount of suffixes in practice due to that. 8916b6aa828SChristian Schoenebeck * 8926b6aa828SChristian Schoenebeck * We generate variable size suffixes for that purpose. The 1st generated 8936b6aa828SChristian Schoenebeck * suffix will only have 1 bit and hence we only need to chop off 1 bit from 8946b6aa828SChristian Schoenebeck * the original inode number. The subsequent suffixes being generated will 8956b6aa828SChristian Schoenebeck * grow in (bit) size subsequently, i.e. the 2nd and 3rd suffix being 8966b6aa828SChristian Schoenebeck * generated will have 3 bits and hence we have to chop off 3 bits from their 8976b6aa828SChristian Schoenebeck * original inodes, and so on. That approach of using variable length suffixes 8986b6aa828SChristian Schoenebeck * (i.e. over fixed size ones) utilizes the fact that in practice only a very 8996b6aa828SChristian Schoenebeck * limited amount of devices are shared by the same export (e.g. typically 9006b6aa828SChristian Schoenebeck * less than 2 dozen devices per 9p export), so in practice we need to chop 9016b6aa828SChristian Schoenebeck * off less bits than with fixed size prefixes and yet are flexible to add 9026b6aa828SChristian Schoenebeck * new devices at runtime below host's export directory at any time without 9036b6aa828SChristian Schoenebeck * having to reboot guest nor requiring to reconfigure guest for that. And due 9046b6aa828SChristian Schoenebeck * to the very limited amount of original high end bits that we chop off that 9056b6aa828SChristian Schoenebeck * way, the total amount of suffixes we need to generate is less than by using 9066b6aa828SChristian Schoenebeck * fixed size prefixes and hence it also improves performance of the inode 9076b6aa828SChristian Schoenebeck * remapping algorithm, and finally has the nice side effect that the inode 9086b6aa828SChristian Schoenebeck * numbers on guest will be much smaller & human friendly. ;-) 9091a6ed33cSAntonios Motakis */ 9106b6aa828SChristian Schoenebeck static int qid_path_suffixmap(V9fsPDU *pdu, const struct stat *stbuf, 9111a6ed33cSAntonios Motakis uint64_t *path) 9121a6ed33cSAntonios Motakis { 9136b6aa828SChristian Schoenebeck const int ino_hash_bits = qid_inode_prefix_hash_bits(pdu, stbuf->st_dev); 9141a6ed33cSAntonios Motakis QppEntry lookup = { 9151a6ed33cSAntonios Motakis .dev = stbuf->st_dev, 9166b6aa828SChristian Schoenebeck .ino_prefix = (uint16_t) (stbuf->st_ino >> (64 - ino_hash_bits)) 9171a6ed33cSAntonios Motakis }, *val; 9181a6ed33cSAntonios Motakis uint32_t hash = qpp_hash(lookup); 9191a6ed33cSAntonios Motakis 9201a6ed33cSAntonios Motakis val = qht_lookup(&pdu->s->qpp_table, &lookup, hash); 9211a6ed33cSAntonios Motakis 9221a6ed33cSAntonios Motakis if (!val) { 9236b6aa828SChristian Schoenebeck if (pdu->s->qp_affix_next == 0) { 9246b6aa828SChristian Schoenebeck /* we ran out of affixes */ 925f3fe4a2dSAntonios Motakis warn_report_once( 926f3fe4a2dSAntonios Motakis "9p: Potential degraded performance of inode remapping" 9271a6ed33cSAntonios Motakis ); 9281a6ed33cSAntonios Motakis return -ENFILE; 9291a6ed33cSAntonios Motakis } 9301a6ed33cSAntonios Motakis 9311366244aSMarkus Armbruster val = g_new0(QppEntry, 1); 9321a6ed33cSAntonios Motakis *val = lookup; 9331a6ed33cSAntonios Motakis 9346b6aa828SChristian Schoenebeck /* new unique inode affix and device combo */ 9356b6aa828SChristian Schoenebeck val->qp_affix_index = pdu->s->qp_affix_next++; 9366b6aa828SChristian Schoenebeck val->qp_affix = affixForIndex(val->qp_affix_index); 9371a6ed33cSAntonios Motakis qht_insert(&pdu->s->qpp_table, val, hash, NULL); 9381a6ed33cSAntonios Motakis } 9396b6aa828SChristian Schoenebeck /* assuming generated affix to be suffix type, not prefix */ 9406b6aa828SChristian Schoenebeck *path = (stbuf->st_ino << val->qp_affix.bits) | val->qp_affix.value; 9411a6ed33cSAntonios Motakis return 0; 9421a6ed33cSAntonios Motakis } 9431a6ed33cSAntonios Motakis 9443b5ee9e8SAntonios Motakis static int stat_to_qid(V9fsPDU *pdu, const struct stat *stbuf, V9fsQID *qidp) 94560ce86c7SWei Liu { 9461a6ed33cSAntonios Motakis int err; 94760ce86c7SWei Liu size_t size; 94860ce86c7SWei Liu 9491a6ed33cSAntonios Motakis if (pdu->s->ctx.export_flags & V9FS_REMAP_INODES) { 9501a6ed33cSAntonios Motakis /* map inode+device to qid path (fast path) */ 9516b6aa828SChristian Schoenebeck err = qid_path_suffixmap(pdu, stbuf, &qidp->path); 952f3fe4a2dSAntonios Motakis if (err == -ENFILE) { 953f3fe4a2dSAntonios Motakis /* fast path didn't work, fall back to full map */ 954f3fe4a2dSAntonios Motakis err = qid_path_fullmap(pdu, stbuf, &qidp->path); 955f3fe4a2dSAntonios Motakis } 9561a6ed33cSAntonios Motakis if (err) { 9571a6ed33cSAntonios Motakis return err; 9581a6ed33cSAntonios Motakis } 9591a6ed33cSAntonios Motakis } else { 9603b5ee9e8SAntonios Motakis if (pdu->s->dev_id != stbuf->st_dev) { 9611a6ed33cSAntonios Motakis if (pdu->s->ctx.export_flags & V9FS_FORBID_MULTIDEVS) { 9621a6ed33cSAntonios Motakis error_report_once( 9631a6ed33cSAntonios Motakis "9p: Multiple devices detected in same VirtFS export. " 9641a6ed33cSAntonios Motakis "Access of guest to additional devices is (partly) " 9651a6ed33cSAntonios Motakis "denied due to virtfs option 'multidevs=forbid' being " 9661a6ed33cSAntonios Motakis "effective." 9671a6ed33cSAntonios Motakis ); 9681a6ed33cSAntonios Motakis return -ENODEV; 9691a6ed33cSAntonios Motakis } else { 9703b5ee9e8SAntonios Motakis warn_report_once( 9713b5ee9e8SAntonios Motakis "9p: Multiple devices detected in same VirtFS export, " 9723b5ee9e8SAntonios Motakis "which might lead to file ID collisions and severe " 9731a6ed33cSAntonios Motakis "misbehaviours on guest! You should either use a " 9741a6ed33cSAntonios Motakis "separate export for each device shared from host or " 9751a6ed33cSAntonios Motakis "use virtfs option 'multidevs=remap'!" 9763b5ee9e8SAntonios Motakis ); 9773b5ee9e8SAntonios Motakis } 9781a6ed33cSAntonios Motakis } 97960ce86c7SWei Liu memset(&qidp->path, 0, sizeof(qidp->path)); 98060ce86c7SWei Liu size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path)); 98160ce86c7SWei Liu memcpy(&qidp->path, &stbuf->st_ino, size); 9821a6ed33cSAntonios Motakis } 9831a6ed33cSAntonios Motakis 98460ce86c7SWei Liu qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8); 98560ce86c7SWei Liu qidp->type = 0; 98660ce86c7SWei Liu if (S_ISDIR(stbuf->st_mode)) { 98760ce86c7SWei Liu qidp->type |= P9_QID_TYPE_DIR; 98860ce86c7SWei Liu } 98960ce86c7SWei Liu if (S_ISLNK(stbuf->st_mode)) { 99060ce86c7SWei Liu qidp->type |= P9_QID_TYPE_SYMLINK; 99160ce86c7SWei Liu } 9923b5ee9e8SAntonios Motakis 9933b5ee9e8SAntonios Motakis return 0; 99460ce86c7SWei Liu } 99560ce86c7SWei Liu 99660ce86c7SWei Liu V9fsPDU *pdu_alloc(V9fsState *s) 99760ce86c7SWei Liu { 99860ce86c7SWei Liu V9fsPDU *pdu = NULL; 99960ce86c7SWei Liu 100060ce86c7SWei Liu if (!QLIST_EMPTY(&s->free_list)) { 100160ce86c7SWei Liu pdu = QLIST_FIRST(&s->free_list); 100260ce86c7SWei Liu QLIST_REMOVE(pdu, next); 100360ce86c7SWei Liu QLIST_INSERT_HEAD(&s->active_list, pdu, next); 100460ce86c7SWei Liu } 100560ce86c7SWei Liu return pdu; 100660ce86c7SWei Liu } 100760ce86c7SWei Liu 100860ce86c7SWei Liu void pdu_free(V9fsPDU *pdu) 100960ce86c7SWei Liu { 101060ce86c7SWei Liu V9fsState *s = pdu->s; 1011f74e27bfSGreg Kurz 1012f74e27bfSGreg Kurz g_assert(!pdu->cancelled); 101360ce86c7SWei Liu QLIST_REMOVE(pdu, next); 101460ce86c7SWei Liu QLIST_INSERT_HEAD(&s->free_list, pdu, next); 101560ce86c7SWei Liu } 101660ce86c7SWei Liu 10178440e22eSGreg Kurz static void coroutine_fn pdu_complete(V9fsPDU *pdu, ssize_t len) 101860ce86c7SWei Liu { 101960ce86c7SWei Liu int8_t id = pdu->id + 1; /* Response */ 102060ce86c7SWei Liu V9fsState *s = pdu->s; 102106a37db7SGreg Kurz int ret; 102260ce86c7SWei Liu 1023fc78d5eeSKeno Fischer /* 1024fc78d5eeSKeno Fischer * The 9p spec requires that successfully cancelled pdus receive no reply. 1025fc78d5eeSKeno Fischer * Sending a reply would confuse clients because they would 1026fc78d5eeSKeno Fischer * assume that any EINTR is the actual result of the operation, 1027fc78d5eeSKeno Fischer * rather than a consequence of the cancellation. However, if 1028fc78d5eeSKeno Fischer * the operation completed (succesfully or with an error other 1029fc78d5eeSKeno Fischer * than caused be cancellation), we do send out that reply, both 1030fc78d5eeSKeno Fischer * for efficiency and to avoid confusing the rest of the state machine 1031fc78d5eeSKeno Fischer * that assumes passing a non-error here will mean a successful 1032fc78d5eeSKeno Fischer * transmission of the reply. 1033fc78d5eeSKeno Fischer */ 1034fc78d5eeSKeno Fischer bool discard = pdu->cancelled && len == -EINTR; 1035fc78d5eeSKeno Fischer if (discard) { 1036fc78d5eeSKeno Fischer trace_v9fs_rcancel(pdu->tag, pdu->id); 1037fc78d5eeSKeno Fischer pdu->size = 0; 1038fc78d5eeSKeno Fischer goto out_notify; 1039fc78d5eeSKeno Fischer } 1040fc78d5eeSKeno Fischer 104160ce86c7SWei Liu if (len < 0) { 104260ce86c7SWei Liu int err = -len; 104360ce86c7SWei Liu len = 7; 104460ce86c7SWei Liu 104560ce86c7SWei Liu if (s->proto_version != V9FS_PROTO_2000L) { 104660ce86c7SWei Liu V9fsString str; 104760ce86c7SWei Liu 104860ce86c7SWei Liu str.data = strerror(err); 104960ce86c7SWei Liu str.size = strlen(str.data); 105060ce86c7SWei Liu 105106a37db7SGreg Kurz ret = pdu_marshal(pdu, len, "s", &str); 105206a37db7SGreg Kurz if (ret < 0) { 105306a37db7SGreg Kurz goto out_notify; 105406a37db7SGreg Kurz } 105506a37db7SGreg Kurz len += ret; 105660ce86c7SWei Liu id = P9_RERROR; 1057951fe2f8SChristian Schoenebeck } else { 1058951fe2f8SChristian Schoenebeck err = errno_to_dotl(err); 105960ce86c7SWei Liu } 106060ce86c7SWei Liu 106106a37db7SGreg Kurz ret = pdu_marshal(pdu, len, "d", err); 106206a37db7SGreg Kurz if (ret < 0) { 106306a37db7SGreg Kurz goto out_notify; 106406a37db7SGreg Kurz } 106506a37db7SGreg Kurz len += ret; 106660ce86c7SWei Liu 106760ce86c7SWei Liu if (s->proto_version == V9FS_PROTO_2000L) { 106860ce86c7SWei Liu id = P9_RLERROR; 106960ce86c7SWei Liu } 107060ce86c7SWei Liu trace_v9fs_rerror(pdu->tag, pdu->id, err); /* Trace ERROR */ 107160ce86c7SWei Liu } 107260ce86c7SWei Liu 107360ce86c7SWei Liu /* fill out the header */ 107406a37db7SGreg Kurz if (pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag) < 0) { 107506a37db7SGreg Kurz goto out_notify; 107606a37db7SGreg Kurz } 107760ce86c7SWei Liu 107860ce86c7SWei Liu /* keep these in sync */ 107960ce86c7SWei Liu pdu->size = len; 108060ce86c7SWei Liu pdu->id = id; 108160ce86c7SWei Liu 108206a37db7SGreg Kurz out_notify: 1083a17d8659SGreg Kurz pdu->s->transport->push_and_notify(pdu); 108460ce86c7SWei Liu 108560ce86c7SWei Liu /* Now wakeup anybody waiting in flush for this request */ 1086f74e27bfSGreg Kurz if (!qemu_co_queue_next(&pdu->complete)) { 108760ce86c7SWei Liu pdu_free(pdu); 108860ce86c7SWei Liu } 1089f74e27bfSGreg Kurz } 109060ce86c7SWei Liu 109160ce86c7SWei Liu static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension) 109260ce86c7SWei Liu { 109360ce86c7SWei Liu mode_t ret; 109460ce86c7SWei Liu 109560ce86c7SWei Liu ret = mode & 0777; 109660ce86c7SWei Liu if (mode & P9_STAT_MODE_DIR) { 109760ce86c7SWei Liu ret |= S_IFDIR; 109860ce86c7SWei Liu } 109960ce86c7SWei Liu 110060ce86c7SWei Liu if (mode & P9_STAT_MODE_SYMLINK) { 110160ce86c7SWei Liu ret |= S_IFLNK; 110260ce86c7SWei Liu } 110360ce86c7SWei Liu if (mode & P9_STAT_MODE_SOCKET) { 110460ce86c7SWei Liu ret |= S_IFSOCK; 110560ce86c7SWei Liu } 110660ce86c7SWei Liu if (mode & P9_STAT_MODE_NAMED_PIPE) { 110760ce86c7SWei Liu ret |= S_IFIFO; 110860ce86c7SWei Liu } 110960ce86c7SWei Liu if (mode & P9_STAT_MODE_DEVICE) { 111060ce86c7SWei Liu if (extension->size && extension->data[0] == 'c') { 111160ce86c7SWei Liu ret |= S_IFCHR; 111260ce86c7SWei Liu } else { 111360ce86c7SWei Liu ret |= S_IFBLK; 111460ce86c7SWei Liu } 111560ce86c7SWei Liu } 111660ce86c7SWei Liu 111760ce86c7SWei Liu if (!(ret & ~0777)) { 111860ce86c7SWei Liu ret |= S_IFREG; 111960ce86c7SWei Liu } 112060ce86c7SWei Liu 112160ce86c7SWei Liu if (mode & P9_STAT_MODE_SETUID) { 112260ce86c7SWei Liu ret |= S_ISUID; 112360ce86c7SWei Liu } 112460ce86c7SWei Liu if (mode & P9_STAT_MODE_SETGID) { 112560ce86c7SWei Liu ret |= S_ISGID; 112660ce86c7SWei Liu } 112760ce86c7SWei Liu if (mode & P9_STAT_MODE_SETVTX) { 112860ce86c7SWei Liu ret |= S_ISVTX; 112960ce86c7SWei Liu } 113060ce86c7SWei Liu 113160ce86c7SWei Liu return ret; 113260ce86c7SWei Liu } 113360ce86c7SWei Liu 113460ce86c7SWei Liu static int donttouch_stat(V9fsStat *stat) 113560ce86c7SWei Liu { 113660ce86c7SWei Liu if (stat->type == -1 && 113760ce86c7SWei Liu stat->dev == -1 && 113887032833SAntonios Motakis stat->qid.type == 0xff && 113987032833SAntonios Motakis stat->qid.version == (uint32_t) -1 && 114087032833SAntonios Motakis stat->qid.path == (uint64_t) -1 && 114160ce86c7SWei Liu stat->mode == -1 && 114260ce86c7SWei Liu stat->atime == -1 && 114360ce86c7SWei Liu stat->mtime == -1 && 114460ce86c7SWei Liu stat->length == -1 && 114560ce86c7SWei Liu !stat->name.size && 114660ce86c7SWei Liu !stat->uid.size && 114760ce86c7SWei Liu !stat->gid.size && 114860ce86c7SWei Liu !stat->muid.size && 114960ce86c7SWei Liu stat->n_uid == -1 && 115060ce86c7SWei Liu stat->n_gid == -1 && 115160ce86c7SWei Liu stat->n_muid == -1) { 115260ce86c7SWei Liu return 1; 115360ce86c7SWei Liu } 115460ce86c7SWei Liu 115560ce86c7SWei Liu return 0; 115660ce86c7SWei Liu } 115760ce86c7SWei Liu 115860ce86c7SWei Liu static void v9fs_stat_init(V9fsStat *stat) 115960ce86c7SWei Liu { 116060ce86c7SWei Liu v9fs_string_init(&stat->name); 116160ce86c7SWei Liu v9fs_string_init(&stat->uid); 116260ce86c7SWei Liu v9fs_string_init(&stat->gid); 116360ce86c7SWei Liu v9fs_string_init(&stat->muid); 116460ce86c7SWei Liu v9fs_string_init(&stat->extension); 116560ce86c7SWei Liu } 116660ce86c7SWei Liu 116760ce86c7SWei Liu static void v9fs_stat_free(V9fsStat *stat) 116860ce86c7SWei Liu { 116960ce86c7SWei Liu v9fs_string_free(&stat->name); 117060ce86c7SWei Liu v9fs_string_free(&stat->uid); 117160ce86c7SWei Liu v9fs_string_free(&stat->gid); 117260ce86c7SWei Liu v9fs_string_free(&stat->muid); 117360ce86c7SWei Liu v9fs_string_free(&stat->extension); 117460ce86c7SWei Liu } 117560ce86c7SWei Liu 117660ce86c7SWei Liu static uint32_t stat_to_v9mode(const struct stat *stbuf) 117760ce86c7SWei Liu { 117860ce86c7SWei Liu uint32_t mode; 117960ce86c7SWei Liu 118060ce86c7SWei Liu mode = stbuf->st_mode & 0777; 118160ce86c7SWei Liu if (S_ISDIR(stbuf->st_mode)) { 118260ce86c7SWei Liu mode |= P9_STAT_MODE_DIR; 118360ce86c7SWei Liu } 118460ce86c7SWei Liu 118560ce86c7SWei Liu if (S_ISLNK(stbuf->st_mode)) { 118660ce86c7SWei Liu mode |= P9_STAT_MODE_SYMLINK; 118760ce86c7SWei Liu } 118860ce86c7SWei Liu 118960ce86c7SWei Liu if (S_ISSOCK(stbuf->st_mode)) { 119060ce86c7SWei Liu mode |= P9_STAT_MODE_SOCKET; 119160ce86c7SWei Liu } 119260ce86c7SWei Liu 119360ce86c7SWei Liu if (S_ISFIFO(stbuf->st_mode)) { 119460ce86c7SWei Liu mode |= P9_STAT_MODE_NAMED_PIPE; 119560ce86c7SWei Liu } 119660ce86c7SWei Liu 119760ce86c7SWei Liu if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) { 119860ce86c7SWei Liu mode |= P9_STAT_MODE_DEVICE; 119960ce86c7SWei Liu } 120060ce86c7SWei Liu 120160ce86c7SWei Liu if (stbuf->st_mode & S_ISUID) { 120260ce86c7SWei Liu mode |= P9_STAT_MODE_SETUID; 120360ce86c7SWei Liu } 120460ce86c7SWei Liu 120560ce86c7SWei Liu if (stbuf->st_mode & S_ISGID) { 120660ce86c7SWei Liu mode |= P9_STAT_MODE_SETGID; 120760ce86c7SWei Liu } 120860ce86c7SWei Liu 120960ce86c7SWei Liu if (stbuf->st_mode & S_ISVTX) { 121060ce86c7SWei Liu mode |= P9_STAT_MODE_SETVTX; 121160ce86c7SWei Liu } 121260ce86c7SWei Liu 121360ce86c7SWei Liu return mode; 121460ce86c7SWei Liu } 121560ce86c7SWei Liu 12166069537fSJan Dakinevich static int coroutine_fn stat_to_v9stat(V9fsPDU *pdu, V9fsPath *path, 12176069537fSJan Dakinevich const char *basename, 121860ce86c7SWei Liu const struct stat *stbuf, 121960ce86c7SWei Liu V9fsStat *v9stat) 122060ce86c7SWei Liu { 122160ce86c7SWei Liu int err; 122260ce86c7SWei Liu 122360ce86c7SWei Liu memset(v9stat, 0, sizeof(*v9stat)); 122460ce86c7SWei Liu 12253b5ee9e8SAntonios Motakis err = stat_to_qid(pdu, stbuf, &v9stat->qid); 12263b5ee9e8SAntonios Motakis if (err < 0) { 12273b5ee9e8SAntonios Motakis return err; 12283b5ee9e8SAntonios Motakis } 122960ce86c7SWei Liu v9stat->mode = stat_to_v9mode(stbuf); 123060ce86c7SWei Liu v9stat->atime = stbuf->st_atime; 123160ce86c7SWei Liu v9stat->mtime = stbuf->st_mtime; 123260ce86c7SWei Liu v9stat->length = stbuf->st_size; 123360ce86c7SWei Liu 1234abdf0086SGreg Kurz v9fs_string_free(&v9stat->uid); 1235abdf0086SGreg Kurz v9fs_string_free(&v9stat->gid); 1236abdf0086SGreg Kurz v9fs_string_free(&v9stat->muid); 123760ce86c7SWei Liu 123860ce86c7SWei Liu v9stat->n_uid = stbuf->st_uid; 123960ce86c7SWei Liu v9stat->n_gid = stbuf->st_gid; 124060ce86c7SWei Liu v9stat->n_muid = 0; 124160ce86c7SWei Liu 1242abdf0086SGreg Kurz v9fs_string_free(&v9stat->extension); 124360ce86c7SWei Liu 124460ce86c7SWei Liu if (v9stat->mode & P9_STAT_MODE_SYMLINK) { 12456069537fSJan Dakinevich err = v9fs_co_readlink(pdu, path, &v9stat->extension); 124660ce86c7SWei Liu if (err < 0) { 124760ce86c7SWei Liu return err; 124860ce86c7SWei Liu } 124960ce86c7SWei Liu } else if (v9stat->mode & P9_STAT_MODE_DEVICE) { 125060ce86c7SWei Liu v9fs_string_sprintf(&v9stat->extension, "%c %u %u", 125160ce86c7SWei Liu S_ISCHR(stbuf->st_mode) ? 'c' : 'b', 125260ce86c7SWei Liu major(stbuf->st_rdev), minor(stbuf->st_rdev)); 125360ce86c7SWei Liu } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) { 125460ce86c7SWei Liu v9fs_string_sprintf(&v9stat->extension, "%s %lu", 125560ce86c7SWei Liu "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink); 125660ce86c7SWei Liu } 125760ce86c7SWei Liu 12586069537fSJan Dakinevich v9fs_string_sprintf(&v9stat->name, "%s", basename); 125960ce86c7SWei Liu 126060ce86c7SWei Liu v9stat->size = 61 + 126160ce86c7SWei Liu v9fs_string_size(&v9stat->name) + 126260ce86c7SWei Liu v9fs_string_size(&v9stat->uid) + 126360ce86c7SWei Liu v9fs_string_size(&v9stat->gid) + 126460ce86c7SWei Liu v9fs_string_size(&v9stat->muid) + 126560ce86c7SWei Liu v9fs_string_size(&v9stat->extension); 126660ce86c7SWei Liu return 0; 126760ce86c7SWei Liu } 126860ce86c7SWei Liu 126960ce86c7SWei Liu #define P9_STATS_MODE 0x00000001ULL 127060ce86c7SWei Liu #define P9_STATS_NLINK 0x00000002ULL 127160ce86c7SWei Liu #define P9_STATS_UID 0x00000004ULL 127260ce86c7SWei Liu #define P9_STATS_GID 0x00000008ULL 127360ce86c7SWei Liu #define P9_STATS_RDEV 0x00000010ULL 127460ce86c7SWei Liu #define P9_STATS_ATIME 0x00000020ULL 127560ce86c7SWei Liu #define P9_STATS_MTIME 0x00000040ULL 127660ce86c7SWei Liu #define P9_STATS_CTIME 0x00000080ULL 127760ce86c7SWei Liu #define P9_STATS_INO 0x00000100ULL 127860ce86c7SWei Liu #define P9_STATS_SIZE 0x00000200ULL 127960ce86c7SWei Liu #define P9_STATS_BLOCKS 0x00000400ULL 128060ce86c7SWei Liu 128160ce86c7SWei Liu #define P9_STATS_BTIME 0x00000800ULL 128260ce86c7SWei Liu #define P9_STATS_GEN 0x00001000ULL 128360ce86c7SWei Liu #define P9_STATS_DATA_VERSION 0x00002000ULL 128460ce86c7SWei Liu 128560ce86c7SWei Liu #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */ 128660ce86c7SWei Liu #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */ 128760ce86c7SWei Liu 128860ce86c7SWei Liu 1289b565bccbSChristian Schoenebeck /** 1290e16fea41SChristian Schoenebeck * blksize_to_iounit() - Block size exposed to 9p client. 1291e16fea41SChristian Schoenebeck * Return: block size 1292b565bccbSChristian Schoenebeck * 1293b565bccbSChristian Schoenebeck * @pdu: 9p client request 1294b565bccbSChristian Schoenebeck * @blksize: host filesystem's block size 1295e16fea41SChristian Schoenebeck * 1296e16fea41SChristian Schoenebeck * Convert host filesystem's block size into an appropriate block size for 1297e16fea41SChristian Schoenebeck * 9p client (guest OS side). The value returned suggests an "optimum" block 1298e16fea41SChristian Schoenebeck * size for 9p I/O, i.e. to maximize performance. 1299b565bccbSChristian Schoenebeck */ 1300b565bccbSChristian Schoenebeck static int32_t blksize_to_iounit(const V9fsPDU *pdu, int32_t blksize) 1301669ced09SChristian Schoenebeck { 1302669ced09SChristian Schoenebeck int32_t iounit = 0; 1303669ced09SChristian Schoenebeck V9fsState *s = pdu->s; 1304669ced09SChristian Schoenebeck 1305669ced09SChristian Schoenebeck /* 1306b565bccbSChristian Schoenebeck * iounit should be multiples of blksize (host filesystem block size) 1307669ced09SChristian Schoenebeck * as well as less than (client msize - P9_IOHDRSZ) 1308669ced09SChristian Schoenebeck */ 1309b565bccbSChristian Schoenebeck if (blksize) { 131004a7f9e5SChristian Schoenebeck iounit = QEMU_ALIGN_DOWN(s->msize - P9_IOHDRSZ, blksize); 1311669ced09SChristian Schoenebeck } 1312669ced09SChristian Schoenebeck if (!iounit) { 1313669ced09SChristian Schoenebeck iounit = s->msize - P9_IOHDRSZ; 1314669ced09SChristian Schoenebeck } 1315669ced09SChristian Schoenebeck return iounit; 1316669ced09SChristian Schoenebeck } 1317669ced09SChristian Schoenebeck 1318b565bccbSChristian Schoenebeck static int32_t stat_to_iounit(const V9fsPDU *pdu, const struct stat *stbuf) 1319b565bccbSChristian Schoenebeck { 1320b565bccbSChristian Schoenebeck return blksize_to_iounit(pdu, stbuf->st_blksize); 1321b565bccbSChristian Schoenebeck } 1322b565bccbSChristian Schoenebeck 13233b5ee9e8SAntonios Motakis static int stat_to_v9stat_dotl(V9fsPDU *pdu, const struct stat *stbuf, 132460ce86c7SWei Liu V9fsStatDotl *v9lstat) 132560ce86c7SWei Liu { 132660ce86c7SWei Liu memset(v9lstat, 0, sizeof(*v9lstat)); 132760ce86c7SWei Liu 132860ce86c7SWei Liu v9lstat->st_mode = stbuf->st_mode; 132960ce86c7SWei Liu v9lstat->st_nlink = stbuf->st_nlink; 133060ce86c7SWei Liu v9lstat->st_uid = stbuf->st_uid; 133160ce86c7SWei Liu v9lstat->st_gid = stbuf->st_gid; 1332e5c88e22SChristian Schoenebeck v9lstat->st_rdev = host_dev_to_dotl_dev(stbuf->st_rdev); 133360ce86c7SWei Liu v9lstat->st_size = stbuf->st_size; 1334669ced09SChristian Schoenebeck v9lstat->st_blksize = stat_to_iounit(pdu, stbuf); 133560ce86c7SWei Liu v9lstat->st_blocks = stbuf->st_blocks; 133660ce86c7SWei Liu v9lstat->st_atime_sec = stbuf->st_atime; 133760ce86c7SWei Liu v9lstat->st_mtime_sec = stbuf->st_mtime; 133860ce86c7SWei Liu v9lstat->st_ctime_sec = stbuf->st_ctime; 1339f41db099SKeno Fischer #ifdef CONFIG_DARWIN 1340f41db099SKeno Fischer v9lstat->st_atime_nsec = stbuf->st_atimespec.tv_nsec; 1341f41db099SKeno Fischer v9lstat->st_mtime_nsec = stbuf->st_mtimespec.tv_nsec; 1342f41db099SKeno Fischer v9lstat->st_ctime_nsec = stbuf->st_ctimespec.tv_nsec; 1343f41db099SKeno Fischer #else 1344f41db099SKeno Fischer v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec; 1345f41db099SKeno Fischer v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec; 134660ce86c7SWei Liu v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec; 1347f41db099SKeno Fischer #endif 134860ce86c7SWei Liu /* Currently we only support BASIC fields in stat */ 134960ce86c7SWei Liu v9lstat->st_result_mask = P9_STATS_BASIC; 135060ce86c7SWei Liu 13513b5ee9e8SAntonios Motakis return stat_to_qid(pdu, stbuf, &v9lstat->qid); 135260ce86c7SWei Liu } 135360ce86c7SWei Liu 135460ce86c7SWei Liu static void print_sg(struct iovec *sg, int cnt) 135560ce86c7SWei Liu { 135660ce86c7SWei Liu int i; 135760ce86c7SWei Liu 135860ce86c7SWei Liu printf("sg[%d]: {", cnt); 135960ce86c7SWei Liu for (i = 0; i < cnt; i++) { 136060ce86c7SWei Liu if (i) { 136160ce86c7SWei Liu printf(", "); 136260ce86c7SWei Liu } 136360ce86c7SWei Liu printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len); 136460ce86c7SWei Liu } 136560ce86c7SWei Liu printf("}\n"); 136660ce86c7SWei Liu } 136760ce86c7SWei Liu 136860ce86c7SWei Liu /* Will call this only for path name based fid */ 136960ce86c7SWei Liu static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len) 137060ce86c7SWei Liu { 137160ce86c7SWei Liu V9fsPath str; 137260ce86c7SWei Liu v9fs_path_init(&str); 137360ce86c7SWei Liu v9fs_path_copy(&str, dst); 1374e3e83f2eSGreg Kurz v9fs_path_sprintf(dst, "%s%s", src->data, str.data + len); 137560ce86c7SWei Liu v9fs_path_free(&str); 137660ce86c7SWei Liu } 137760ce86c7SWei Liu 137860ce86c7SWei Liu static inline bool is_ro_export(FsContext *ctx) 137960ce86c7SWei Liu { 138060ce86c7SWei Liu return ctx->export_flags & V9FS_RDONLY; 138160ce86c7SWei Liu } 138260ce86c7SWei Liu 13838440e22eSGreg Kurz static void coroutine_fn v9fs_version(void *opaque) 138460ce86c7SWei Liu { 138560ce86c7SWei Liu ssize_t err; 138660ce86c7SWei Liu V9fsPDU *pdu = opaque; 138760ce86c7SWei Liu V9fsState *s = pdu->s; 138860ce86c7SWei Liu V9fsString version; 138960ce86c7SWei Liu size_t offset = 7; 139060ce86c7SWei Liu 139160ce86c7SWei Liu v9fs_string_init(&version); 139260ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "ds", &s->msize, &version); 139360ce86c7SWei Liu if (err < 0) { 139460ce86c7SWei Liu goto out; 139560ce86c7SWei Liu } 139660ce86c7SWei Liu trace_v9fs_version(pdu->tag, pdu->id, s->msize, version.data); 139760ce86c7SWei Liu 139860ce86c7SWei Liu virtfs_reset(pdu); 139960ce86c7SWei Liu 140060ce86c7SWei Liu if (!strcmp(version.data, "9P2000.u")) { 140160ce86c7SWei Liu s->proto_version = V9FS_PROTO_2000U; 140260ce86c7SWei Liu } else if (!strcmp(version.data, "9P2000.L")) { 140360ce86c7SWei Liu s->proto_version = V9FS_PROTO_2000L; 140460ce86c7SWei Liu } else { 140560ce86c7SWei Liu v9fs_string_sprintf(&version, "unknown"); 1406e16453a3SChristian Schoenebeck /* skip min. msize check, reporting invalid version has priority */ 1407e16453a3SChristian Schoenebeck goto marshal; 140860ce86c7SWei Liu } 140960ce86c7SWei Liu 1410e16453a3SChristian Schoenebeck if (s->msize < P9_MIN_MSIZE) { 1411e16453a3SChristian Schoenebeck err = -EMSGSIZE; 1412e16453a3SChristian Schoenebeck error_report( 1413e16453a3SChristian Schoenebeck "9pfs: Client requested msize < minimum msize (" 1414e16453a3SChristian Schoenebeck stringify(P9_MIN_MSIZE) ") supported by this server." 1415e16453a3SChristian Schoenebeck ); 1416e16453a3SChristian Schoenebeck goto out; 1417e16453a3SChristian Schoenebeck } 1418e16453a3SChristian Schoenebeck 141962777d82SChristian Schoenebeck /* 8192 is the default msize of Linux clients */ 1420c418f935SChristian Schoenebeck if (s->msize <= 8192 && !(s->ctx.export_flags & V9FS_NO_PERF_WARN)) { 142162777d82SChristian Schoenebeck warn_report_once( 142262777d82SChristian Schoenebeck "9p: degraded performance: a reasonable high msize should be " 142362777d82SChristian Schoenebeck "chosen on client/guest side (chosen msize is <= 8192). See " 142462777d82SChristian Schoenebeck "https://wiki.qemu.org/Documentation/9psetup#msize for details." 142562777d82SChristian Schoenebeck ); 142662777d82SChristian Schoenebeck } 142762777d82SChristian Schoenebeck 1428e16453a3SChristian Schoenebeck marshal: 142960ce86c7SWei Liu err = pdu_marshal(pdu, offset, "ds", s->msize, &version); 143060ce86c7SWei Liu if (err < 0) { 143160ce86c7SWei Liu goto out; 143260ce86c7SWei Liu } 1433403a905bSPhilippe Mathieu-Daudé err += offset; 143460ce86c7SWei Liu trace_v9fs_version_return(pdu->tag, pdu->id, s->msize, version.data); 143560ce86c7SWei Liu out: 1436403a905bSPhilippe Mathieu-Daudé pdu_complete(pdu, err); 143760ce86c7SWei Liu v9fs_string_free(&version); 143860ce86c7SWei Liu } 143960ce86c7SWei Liu 14408440e22eSGreg Kurz static void coroutine_fn v9fs_attach(void *opaque) 144160ce86c7SWei Liu { 144260ce86c7SWei Liu V9fsPDU *pdu = opaque; 144360ce86c7SWei Liu V9fsState *s = pdu->s; 144460ce86c7SWei Liu int32_t fid, afid, n_uname; 144560ce86c7SWei Liu V9fsString uname, aname; 144660ce86c7SWei Liu V9fsFidState *fidp; 144760ce86c7SWei Liu size_t offset = 7; 144860ce86c7SWei Liu V9fsQID qid; 144960ce86c7SWei Liu ssize_t err; 145011024375SChristian Schoenebeck struct stat stbuf; 145160ce86c7SWei Liu 145260ce86c7SWei Liu v9fs_string_init(&uname); 145360ce86c7SWei Liu v9fs_string_init(&aname); 145460ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "ddssd", &fid, 145560ce86c7SWei Liu &afid, &uname, &aname, &n_uname); 145660ce86c7SWei Liu if (err < 0) { 145760ce86c7SWei Liu goto out_nofid; 145860ce86c7SWei Liu } 145960ce86c7SWei Liu trace_v9fs_attach(pdu->tag, pdu->id, fid, afid, uname.data, aname.data); 146060ce86c7SWei Liu 146160ce86c7SWei Liu fidp = alloc_fid(s, fid); 146260ce86c7SWei Liu if (fidp == NULL) { 146360ce86c7SWei Liu err = -EINVAL; 146460ce86c7SWei Liu goto out_nofid; 146560ce86c7SWei Liu } 146660ce86c7SWei Liu fidp->uid = n_uname; 146760ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, NULL, "/", &fidp->path); 146860ce86c7SWei Liu if (err < 0) { 146960ce86c7SWei Liu err = -EINVAL; 147060ce86c7SWei Liu clunk_fid(s, fid); 147160ce86c7SWei Liu goto out; 147260ce86c7SWei Liu } 147311024375SChristian Schoenebeck err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); 147411024375SChristian Schoenebeck if (err < 0) { 147511024375SChristian Schoenebeck err = -EINVAL; 147611024375SChristian Schoenebeck clunk_fid(s, fid); 147711024375SChristian Schoenebeck goto out; 147811024375SChristian Schoenebeck } 147911024375SChristian Schoenebeck err = stat_to_qid(pdu, &stbuf, &qid); 148060ce86c7SWei Liu if (err < 0) { 148160ce86c7SWei Liu err = -EINVAL; 148260ce86c7SWei Liu clunk_fid(s, fid); 148360ce86c7SWei Liu goto out; 148460ce86c7SWei Liu } 1485fe44dc91SAshijeet Acharya 1486fe44dc91SAshijeet Acharya /* 1487fe44dc91SAshijeet Acharya * disable migration if we haven't done already. 1488fe44dc91SAshijeet Acharya * attach could get called multiple times for the same export. 1489fe44dc91SAshijeet Acharya */ 1490fe44dc91SAshijeet Acharya if (!s->migration_blocker) { 1491fe44dc91SAshijeet Acharya error_setg(&s->migration_blocker, 1492fe44dc91SAshijeet Acharya "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'", 1493fe44dc91SAshijeet Acharya s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag); 14949261ef5eSMarkus Armbruster err = migrate_add_blocker(s->migration_blocker, NULL); 14959261ef5eSMarkus Armbruster if (err < 0) { 1496fe44dc91SAshijeet Acharya error_free(s->migration_blocker); 1497fe44dc91SAshijeet Acharya s->migration_blocker = NULL; 1498fe44dc91SAshijeet Acharya clunk_fid(s, fid); 1499fe44dc91SAshijeet Acharya goto out; 1500fe44dc91SAshijeet Acharya } 1501fe44dc91SAshijeet Acharya s->root_fid = fid; 1502fe44dc91SAshijeet Acharya } 1503fe44dc91SAshijeet Acharya 150460ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Q", &qid); 150560ce86c7SWei Liu if (err < 0) { 150660ce86c7SWei Liu clunk_fid(s, fid); 150760ce86c7SWei Liu goto out; 150860ce86c7SWei Liu } 150960ce86c7SWei Liu err += offset; 1510fe44dc91SAshijeet Acharya 151111024375SChristian Schoenebeck memcpy(&s->root_st, &stbuf, sizeof(stbuf)); 151260ce86c7SWei Liu trace_v9fs_attach_return(pdu->tag, pdu->id, 151360ce86c7SWei Liu qid.type, qid.version, qid.path); 151460ce86c7SWei Liu out: 151560ce86c7SWei Liu put_fid(pdu, fidp); 151660ce86c7SWei Liu out_nofid: 151760ce86c7SWei Liu pdu_complete(pdu, err); 151860ce86c7SWei Liu v9fs_string_free(&uname); 151960ce86c7SWei Liu v9fs_string_free(&aname); 152060ce86c7SWei Liu } 152160ce86c7SWei Liu 15228440e22eSGreg Kurz static void coroutine_fn v9fs_stat(void *opaque) 152360ce86c7SWei Liu { 152460ce86c7SWei Liu int32_t fid; 152560ce86c7SWei Liu V9fsStat v9stat; 152660ce86c7SWei Liu ssize_t err = 0; 152760ce86c7SWei Liu size_t offset = 7; 152860ce86c7SWei Liu struct stat stbuf; 152960ce86c7SWei Liu V9fsFidState *fidp; 153060ce86c7SWei Liu V9fsPDU *pdu = opaque; 15316069537fSJan Dakinevich char *basename; 153260ce86c7SWei Liu 153360ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "d", &fid); 153460ce86c7SWei Liu if (err < 0) { 153560ce86c7SWei Liu goto out_nofid; 153660ce86c7SWei Liu } 153760ce86c7SWei Liu trace_v9fs_stat(pdu->tag, pdu->id, fid); 153860ce86c7SWei Liu 153960ce86c7SWei Liu fidp = get_fid(pdu, fid); 154060ce86c7SWei Liu if (fidp == NULL) { 154160ce86c7SWei Liu err = -ENOENT; 154260ce86c7SWei Liu goto out_nofid; 154360ce86c7SWei Liu } 154460ce86c7SWei Liu err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); 154560ce86c7SWei Liu if (err < 0) { 154660ce86c7SWei Liu goto out; 154760ce86c7SWei Liu } 15486069537fSJan Dakinevich basename = g_path_get_basename(fidp->path.data); 15496069537fSJan Dakinevich err = stat_to_v9stat(pdu, &fidp->path, basename, &stbuf, &v9stat); 15506069537fSJan Dakinevich g_free(basename); 155160ce86c7SWei Liu if (err < 0) { 155260ce86c7SWei Liu goto out; 155360ce86c7SWei Liu } 155460ce86c7SWei Liu err = pdu_marshal(pdu, offset, "wS", 0, &v9stat); 155560ce86c7SWei Liu if (err < 0) { 155660ce86c7SWei Liu v9fs_stat_free(&v9stat); 155760ce86c7SWei Liu goto out; 155860ce86c7SWei Liu } 155960ce86c7SWei Liu trace_v9fs_stat_return(pdu->tag, pdu->id, v9stat.mode, 156060ce86c7SWei Liu v9stat.atime, v9stat.mtime, v9stat.length); 156160ce86c7SWei Liu err += offset; 156260ce86c7SWei Liu v9fs_stat_free(&v9stat); 156360ce86c7SWei Liu out: 156460ce86c7SWei Liu put_fid(pdu, fidp); 156560ce86c7SWei Liu out_nofid: 156660ce86c7SWei Liu pdu_complete(pdu, err); 156760ce86c7SWei Liu } 156860ce86c7SWei Liu 15698440e22eSGreg Kurz static void coroutine_fn v9fs_getattr(void *opaque) 157060ce86c7SWei Liu { 157160ce86c7SWei Liu int32_t fid; 157260ce86c7SWei Liu size_t offset = 7; 157360ce86c7SWei Liu ssize_t retval = 0; 157460ce86c7SWei Liu struct stat stbuf; 157560ce86c7SWei Liu V9fsFidState *fidp; 157660ce86c7SWei Liu uint64_t request_mask; 157760ce86c7SWei Liu V9fsStatDotl v9stat_dotl; 157860ce86c7SWei Liu V9fsPDU *pdu = opaque; 157960ce86c7SWei Liu 158060ce86c7SWei Liu retval = pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask); 158160ce86c7SWei Liu if (retval < 0) { 158260ce86c7SWei Liu goto out_nofid; 158360ce86c7SWei Liu } 158460ce86c7SWei Liu trace_v9fs_getattr(pdu->tag, pdu->id, fid, request_mask); 158560ce86c7SWei Liu 158660ce86c7SWei Liu fidp = get_fid(pdu, fid); 158760ce86c7SWei Liu if (fidp == NULL) { 158860ce86c7SWei Liu retval = -ENOENT; 158960ce86c7SWei Liu goto out_nofid; 159060ce86c7SWei Liu } 159160ce86c7SWei Liu /* 159260ce86c7SWei Liu * Currently we only support BASIC fields in stat, so there is no 159360ce86c7SWei Liu * need to look at request_mask. 159460ce86c7SWei Liu */ 159560ce86c7SWei Liu retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf); 159660ce86c7SWei Liu if (retval < 0) { 159760ce86c7SWei Liu goto out; 159860ce86c7SWei Liu } 15993b5ee9e8SAntonios Motakis retval = stat_to_v9stat_dotl(pdu, &stbuf, &v9stat_dotl); 16003b5ee9e8SAntonios Motakis if (retval < 0) { 16013b5ee9e8SAntonios Motakis goto out; 16023b5ee9e8SAntonios Motakis } 160360ce86c7SWei Liu 160460ce86c7SWei Liu /* fill st_gen if requested and supported by underlying fs */ 160560ce86c7SWei Liu if (request_mask & P9_STATS_GEN) { 160660ce86c7SWei Liu retval = v9fs_co_st_gen(pdu, &fidp->path, stbuf.st_mode, &v9stat_dotl); 160760ce86c7SWei Liu switch (retval) { 160860ce86c7SWei Liu case 0: 160960ce86c7SWei Liu /* we have valid st_gen: update result mask */ 161060ce86c7SWei Liu v9stat_dotl.st_result_mask |= P9_STATS_GEN; 161160ce86c7SWei Liu break; 161260ce86c7SWei Liu case -EINTR: 161360ce86c7SWei Liu /* request cancelled, e.g. by Tflush */ 161460ce86c7SWei Liu goto out; 161560ce86c7SWei Liu default: 161660ce86c7SWei Liu /* failed to get st_gen: not fatal, ignore */ 161760ce86c7SWei Liu break; 161860ce86c7SWei Liu } 161960ce86c7SWei Liu } 162060ce86c7SWei Liu retval = pdu_marshal(pdu, offset, "A", &v9stat_dotl); 162160ce86c7SWei Liu if (retval < 0) { 162260ce86c7SWei Liu goto out; 162360ce86c7SWei Liu } 162460ce86c7SWei Liu retval += offset; 162560ce86c7SWei Liu trace_v9fs_getattr_return(pdu->tag, pdu->id, v9stat_dotl.st_result_mask, 162660ce86c7SWei Liu v9stat_dotl.st_mode, v9stat_dotl.st_uid, 162760ce86c7SWei Liu v9stat_dotl.st_gid); 162860ce86c7SWei Liu out: 162960ce86c7SWei Liu put_fid(pdu, fidp); 163060ce86c7SWei Liu out_nofid: 163160ce86c7SWei Liu pdu_complete(pdu, retval); 163260ce86c7SWei Liu } 163360ce86c7SWei Liu 163460ce86c7SWei Liu /* Attribute flags */ 163560ce86c7SWei Liu #define P9_ATTR_MODE (1 << 0) 163660ce86c7SWei Liu #define P9_ATTR_UID (1 << 1) 163760ce86c7SWei Liu #define P9_ATTR_GID (1 << 2) 163860ce86c7SWei Liu #define P9_ATTR_SIZE (1 << 3) 163960ce86c7SWei Liu #define P9_ATTR_ATIME (1 << 4) 164060ce86c7SWei Liu #define P9_ATTR_MTIME (1 << 5) 164160ce86c7SWei Liu #define P9_ATTR_CTIME (1 << 6) 164260ce86c7SWei Liu #define P9_ATTR_ATIME_SET (1 << 7) 164360ce86c7SWei Liu #define P9_ATTR_MTIME_SET (1 << 8) 164460ce86c7SWei Liu 164560ce86c7SWei Liu #define P9_ATTR_MASK 127 164660ce86c7SWei Liu 16478440e22eSGreg Kurz static void coroutine_fn v9fs_setattr(void *opaque) 164860ce86c7SWei Liu { 164960ce86c7SWei Liu int err = 0; 165060ce86c7SWei Liu int32_t fid; 165160ce86c7SWei Liu V9fsFidState *fidp; 165260ce86c7SWei Liu size_t offset = 7; 165360ce86c7SWei Liu V9fsIattr v9iattr; 165460ce86c7SWei Liu V9fsPDU *pdu = opaque; 165560ce86c7SWei Liu 165660ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr); 165760ce86c7SWei Liu if (err < 0) { 165860ce86c7SWei Liu goto out_nofid; 165960ce86c7SWei Liu } 166060ce86c7SWei Liu 16618f9c64bfSGreg Kurz trace_v9fs_setattr(pdu->tag, pdu->id, fid, 16628f9c64bfSGreg Kurz v9iattr.valid, v9iattr.mode, v9iattr.uid, v9iattr.gid, 16638f9c64bfSGreg Kurz v9iattr.size, v9iattr.atime_sec, v9iattr.mtime_sec); 16648f9c64bfSGreg Kurz 166560ce86c7SWei Liu fidp = get_fid(pdu, fid); 166660ce86c7SWei Liu if (fidp == NULL) { 166760ce86c7SWei Liu err = -EINVAL; 166860ce86c7SWei Liu goto out_nofid; 166960ce86c7SWei Liu } 167060ce86c7SWei Liu if (v9iattr.valid & P9_ATTR_MODE) { 167160ce86c7SWei Liu err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode); 167260ce86c7SWei Liu if (err < 0) { 167360ce86c7SWei Liu goto out; 167460ce86c7SWei Liu } 167560ce86c7SWei Liu } 167660ce86c7SWei Liu if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) { 167760ce86c7SWei Liu struct timespec times[2]; 167860ce86c7SWei Liu if (v9iattr.valid & P9_ATTR_ATIME) { 167960ce86c7SWei Liu if (v9iattr.valid & P9_ATTR_ATIME_SET) { 168060ce86c7SWei Liu times[0].tv_sec = v9iattr.atime_sec; 168160ce86c7SWei Liu times[0].tv_nsec = v9iattr.atime_nsec; 168260ce86c7SWei Liu } else { 168360ce86c7SWei Liu times[0].tv_nsec = UTIME_NOW; 168460ce86c7SWei Liu } 168560ce86c7SWei Liu } else { 168660ce86c7SWei Liu times[0].tv_nsec = UTIME_OMIT; 168760ce86c7SWei Liu } 168860ce86c7SWei Liu if (v9iattr.valid & P9_ATTR_MTIME) { 168960ce86c7SWei Liu if (v9iattr.valid & P9_ATTR_MTIME_SET) { 169060ce86c7SWei Liu times[1].tv_sec = v9iattr.mtime_sec; 169160ce86c7SWei Liu times[1].tv_nsec = v9iattr.mtime_nsec; 169260ce86c7SWei Liu } else { 169360ce86c7SWei Liu times[1].tv_nsec = UTIME_NOW; 169460ce86c7SWei Liu } 169560ce86c7SWei Liu } else { 169660ce86c7SWei Liu times[1].tv_nsec = UTIME_OMIT; 169760ce86c7SWei Liu } 169860ce86c7SWei Liu err = v9fs_co_utimensat(pdu, &fidp->path, times); 169960ce86c7SWei Liu if (err < 0) { 170060ce86c7SWei Liu goto out; 170160ce86c7SWei Liu } 170260ce86c7SWei Liu } 170360ce86c7SWei Liu /* 170460ce86c7SWei Liu * If the only valid entry in iattr is ctime we can call 170560ce86c7SWei Liu * chown(-1,-1) to update the ctime of the file 170660ce86c7SWei Liu */ 170760ce86c7SWei Liu if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) || 170860ce86c7SWei Liu ((v9iattr.valid & P9_ATTR_CTIME) 170960ce86c7SWei Liu && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) { 171060ce86c7SWei Liu if (!(v9iattr.valid & P9_ATTR_UID)) { 171160ce86c7SWei Liu v9iattr.uid = -1; 171260ce86c7SWei Liu } 171360ce86c7SWei Liu if (!(v9iattr.valid & P9_ATTR_GID)) { 171460ce86c7SWei Liu v9iattr.gid = -1; 171560ce86c7SWei Liu } 171660ce86c7SWei Liu err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid, 171760ce86c7SWei Liu v9iattr.gid); 171860ce86c7SWei Liu if (err < 0) { 171960ce86c7SWei Liu goto out; 172060ce86c7SWei Liu } 172160ce86c7SWei Liu } 172260ce86c7SWei Liu if (v9iattr.valid & (P9_ATTR_SIZE)) { 172360ce86c7SWei Liu err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size); 172460ce86c7SWei Liu if (err < 0) { 172560ce86c7SWei Liu goto out; 172660ce86c7SWei Liu } 172760ce86c7SWei Liu } 172860ce86c7SWei Liu err = offset; 17298f9c64bfSGreg Kurz trace_v9fs_setattr_return(pdu->tag, pdu->id); 173060ce86c7SWei Liu out: 173160ce86c7SWei Liu put_fid(pdu, fidp); 173260ce86c7SWei Liu out_nofid: 173360ce86c7SWei Liu pdu_complete(pdu, err); 173460ce86c7SWei Liu } 173560ce86c7SWei Liu 173660ce86c7SWei Liu static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids) 173760ce86c7SWei Liu { 173860ce86c7SWei Liu int i; 173960ce86c7SWei Liu ssize_t err; 174060ce86c7SWei Liu size_t offset = 7; 174160ce86c7SWei Liu 174260ce86c7SWei Liu err = pdu_marshal(pdu, offset, "w", nwnames); 174360ce86c7SWei Liu if (err < 0) { 174460ce86c7SWei Liu return err; 174560ce86c7SWei Liu } 174660ce86c7SWei Liu offset += err; 174760ce86c7SWei Liu for (i = 0; i < nwnames; i++) { 174860ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Q", &qids[i]); 174960ce86c7SWei Liu if (err < 0) { 175060ce86c7SWei Liu return err; 175160ce86c7SWei Liu } 175260ce86c7SWei Liu offset += err; 175360ce86c7SWei Liu } 175460ce86c7SWei Liu return offset; 175560ce86c7SWei Liu } 175660ce86c7SWei Liu 1757fff39a7aSGreg Kurz static bool name_is_illegal(const char *name) 1758fff39a7aSGreg Kurz { 1759fff39a7aSGreg Kurz return !*name || strchr(name, '/') != NULL; 1760fff39a7aSGreg Kurz } 1761fff39a7aSGreg Kurz 1762f22cad42SChristian Schoenebeck static bool same_stat_id(const struct stat *a, const struct stat *b) 176356f101ecSGreg Kurz { 1764f22cad42SChristian Schoenebeck return a->st_dev == b->st_dev && a->st_ino == b->st_ino; 176556f101ecSGreg Kurz } 176656f101ecSGreg Kurz 17678440e22eSGreg Kurz static void coroutine_fn v9fs_walk(void *opaque) 176860ce86c7SWei Liu { 1769fd6c979eSChristian Schoenebeck int name_idx, nwalked; 1770869605b5SChristian Schoenebeck g_autofree V9fsQID *qids = NULL; 1771*a93d2e89SChristian Schoenebeck int i, err = 0, any_err = 0; 17727e985780SChristian Schoenebeck V9fsPath dpath, path; 17737e985780SChristian Schoenebeck P9ARRAY_REF(V9fsPath) pathes = NULL; 177460ce86c7SWei Liu uint16_t nwnames; 1775869605b5SChristian Schoenebeck struct stat stbuf, fidst; 1776869605b5SChristian Schoenebeck g_autofree struct stat *stbufs = NULL; 177760ce86c7SWei Liu size_t offset = 7; 177860ce86c7SWei Liu int32_t fid, newfid; 17797e985780SChristian Schoenebeck P9ARRAY_REF(V9fsString) wnames = NULL; 178060ce86c7SWei Liu V9fsFidState *fidp; 178160ce86c7SWei Liu V9fsFidState *newfidp = NULL; 178260ce86c7SWei Liu V9fsPDU *pdu = opaque; 178360ce86c7SWei Liu V9fsState *s = pdu->s; 178456f101ecSGreg Kurz V9fsQID qid; 178560ce86c7SWei Liu 178660ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames); 178760ce86c7SWei Liu if (err < 0) { 178860ce86c7SWei Liu pdu_complete(pdu, err); 178960ce86c7SWei Liu return ; 179060ce86c7SWei Liu } 179160ce86c7SWei Liu offset += err; 179260ce86c7SWei Liu 179360ce86c7SWei Liu trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames); 179460ce86c7SWei Liu 1795232a4d2cSChristian Schoenebeck if (nwnames > P9_MAXWELEM) { 1796232a4d2cSChristian Schoenebeck err = -EINVAL; 1797232a4d2cSChristian Schoenebeck goto out_nofid; 1798232a4d2cSChristian Schoenebeck } 1799232a4d2cSChristian Schoenebeck if (nwnames) { 18007e985780SChristian Schoenebeck P9ARRAY_NEW(V9fsString, wnames, nwnames); 18011923923bSGreg Kurz qids = g_new0(V9fsQID, nwnames); 18028d6cb100SChristian Schoenebeck stbufs = g_new0(struct stat, nwnames); 18037e985780SChristian Schoenebeck P9ARRAY_NEW(V9fsPath, pathes, nwnames); 180460ce86c7SWei Liu for (i = 0; i < nwnames; i++) { 180560ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "s", &wnames[i]); 180660ce86c7SWei Liu if (err < 0) { 180760ce86c7SWei Liu goto out_nofid; 180860ce86c7SWei Liu } 1809fff39a7aSGreg Kurz if (name_is_illegal(wnames[i].data)) { 1810fff39a7aSGreg Kurz err = -ENOENT; 1811fff39a7aSGreg Kurz goto out_nofid; 1812fff39a7aSGreg Kurz } 181360ce86c7SWei Liu offset += err; 181460ce86c7SWei Liu } 181560ce86c7SWei Liu } 181660ce86c7SWei Liu fidp = get_fid(pdu, fid); 181760ce86c7SWei Liu if (fidp == NULL) { 181860ce86c7SWei Liu err = -ENOENT; 181960ce86c7SWei Liu goto out_nofid; 182060ce86c7SWei Liu } 182156f101ecSGreg Kurz 182213fd08e6SGreg Kurz v9fs_path_init(&dpath); 182313fd08e6SGreg Kurz v9fs_path_init(&path); 182460ce86c7SWei Liu /* 18258d6cb100SChristian Schoenebeck * Both dpath and path initially point to fidp. 182660ce86c7SWei Liu * Needed to handle request with nwnames == 0 182760ce86c7SWei Liu */ 182860ce86c7SWei Liu v9fs_path_copy(&dpath, &fidp->path); 182960ce86c7SWei Liu v9fs_path_copy(&path, &fidp->path); 18308d6cb100SChristian Schoenebeck 18318d6cb100SChristian Schoenebeck /* 18328d6cb100SChristian Schoenebeck * To keep latency (i.e. overall execution time for processing this 18338d6cb100SChristian Schoenebeck * Twalk client request) as small as possible, run all the required fs 18348d6cb100SChristian Schoenebeck * driver code altogether inside the following block. 18358d6cb100SChristian Schoenebeck */ 18368d6cb100SChristian Schoenebeck v9fs_co_run_in_worker({ 1837*a93d2e89SChristian Schoenebeck nwalked = 0; 18388d6cb100SChristian Schoenebeck if (v9fs_request_cancelled(pdu)) { 1839*a93d2e89SChristian Schoenebeck any_err |= err = -EINTR; 18408d6cb100SChristian Schoenebeck break; 18418d6cb100SChristian Schoenebeck } 18428d6cb100SChristian Schoenebeck err = s->ops->lstat(&s->ctx, &dpath, &fidst); 18438d6cb100SChristian Schoenebeck if (err < 0) { 1844*a93d2e89SChristian Schoenebeck any_err |= err = -errno; 18458d6cb100SChristian Schoenebeck break; 18468d6cb100SChristian Schoenebeck } 18478d6cb100SChristian Schoenebeck stbuf = fidst; 1848*a93d2e89SChristian Schoenebeck for (; nwalked < nwnames; nwalked++) { 18498d6cb100SChristian Schoenebeck if (v9fs_request_cancelled(pdu)) { 1850*a93d2e89SChristian Schoenebeck any_err |= err = -EINTR; 18518d6cb100SChristian Schoenebeck break; 18528d6cb100SChristian Schoenebeck } 1853f22cad42SChristian Schoenebeck if (!same_stat_id(&pdu->s->root_st, &stbuf) || 1854fd6c979eSChristian Schoenebeck strcmp("..", wnames[nwalked].data)) 18558d6cb100SChristian Schoenebeck { 18568d6cb100SChristian Schoenebeck err = s->ops->name_to_path(&s->ctx, &dpath, 1857fd6c979eSChristian Schoenebeck wnames[nwalked].data, 1858fd6c979eSChristian Schoenebeck &pathes[nwalked]); 18598d6cb100SChristian Schoenebeck if (err < 0) { 1860*a93d2e89SChristian Schoenebeck any_err |= err = -errno; 18618d6cb100SChristian Schoenebeck break; 18628d6cb100SChristian Schoenebeck } 18638d6cb100SChristian Schoenebeck if (v9fs_request_cancelled(pdu)) { 1864*a93d2e89SChristian Schoenebeck any_err |= err = -EINTR; 18658d6cb100SChristian Schoenebeck break; 18668d6cb100SChristian Schoenebeck } 1867fd6c979eSChristian Schoenebeck err = s->ops->lstat(&s->ctx, &pathes[nwalked], &stbuf); 18688d6cb100SChristian Schoenebeck if (err < 0) { 1869*a93d2e89SChristian Schoenebeck any_err |= err = -errno; 18708d6cb100SChristian Schoenebeck break; 18718d6cb100SChristian Schoenebeck } 1872fd6c979eSChristian Schoenebeck stbufs[nwalked] = stbuf; 1873fd6c979eSChristian Schoenebeck v9fs_path_copy(&dpath, &pathes[nwalked]); 18748d6cb100SChristian Schoenebeck } 18758d6cb100SChristian Schoenebeck } 18768d6cb100SChristian Schoenebeck }); 18778d6cb100SChristian Schoenebeck /* 18788d6cb100SChristian Schoenebeck * Handle all the rest of this Twalk request on main thread ... 1879*a93d2e89SChristian Schoenebeck * 1880*a93d2e89SChristian Schoenebeck * NOTE: -EINTR is an exception where we deviate from the protocol spec 1881*a93d2e89SChristian Schoenebeck * and simply send a (R)Lerror response instead of bothering to assemble 1882*a93d2e89SChristian Schoenebeck * a (deducted) Rwalk response; because -EINTR is always the result of a 1883*a93d2e89SChristian Schoenebeck * Tflush request, so client would no longer wait for a response in this 1884*a93d2e89SChristian Schoenebeck * case anyway. 18858d6cb100SChristian Schoenebeck */ 1886*a93d2e89SChristian Schoenebeck if ((err < 0 && !nwalked) || err == -EINTR) { 188760ce86c7SWei Liu goto out; 188860ce86c7SWei Liu } 188956f101ecSGreg Kurz 1890*a93d2e89SChristian Schoenebeck any_err |= err = stat_to_qid(pdu, &fidst, &qid); 1891*a93d2e89SChristian Schoenebeck if (err < 0 && !nwalked) { 189260ce86c7SWei Liu goto out; 189360ce86c7SWei Liu } 18948d6cb100SChristian Schoenebeck stbuf = fidst; 18958d6cb100SChristian Schoenebeck 18968d6cb100SChristian Schoenebeck /* reset dpath and path */ 18978d6cb100SChristian Schoenebeck v9fs_path_copy(&dpath, &fidp->path); 18988d6cb100SChristian Schoenebeck v9fs_path_copy(&path, &fidp->path); 18998d6cb100SChristian Schoenebeck 1900*a93d2e89SChristian Schoenebeck for (name_idx = 0; name_idx < nwalked; name_idx++) { 19018d6cb100SChristian Schoenebeck if (!same_stat_id(&pdu->s->root_st, &stbuf) || 19028d6cb100SChristian Schoenebeck strcmp("..", wnames[name_idx].data)) 19038d6cb100SChristian Schoenebeck { 19048d6cb100SChristian Schoenebeck stbuf = stbufs[name_idx]; 1905*a93d2e89SChristian Schoenebeck any_err |= err = stat_to_qid(pdu, &stbuf, &qid); 19063b5ee9e8SAntonios Motakis if (err < 0) { 1907*a93d2e89SChristian Schoenebeck break; 19083b5ee9e8SAntonios Motakis } 19098d6cb100SChristian Schoenebeck v9fs_path_copy(&path, &pathes[name_idx]); 191060ce86c7SWei Liu v9fs_path_copy(&dpath, &path); 191160ce86c7SWei Liu } 191256f101ecSGreg Kurz memcpy(&qids[name_idx], &qid, sizeof(qid)); 191356f101ecSGreg Kurz } 1914*a93d2e89SChristian Schoenebeck if (any_err < 0) { 1915*a93d2e89SChristian Schoenebeck if (!name_idx) { 1916*a93d2e89SChristian Schoenebeck /* don't send any QIDs, send Rlerror instead */ 1917*a93d2e89SChristian Schoenebeck goto out; 1918*a93d2e89SChristian Schoenebeck } else { 1919*a93d2e89SChristian Schoenebeck /* send QIDs (not Rlerror), but fid MUST remain unaffected */ 1920*a93d2e89SChristian Schoenebeck goto send_qids; 1921*a93d2e89SChristian Schoenebeck } 1922*a93d2e89SChristian Schoenebeck } 192360ce86c7SWei Liu if (fid == newfid) { 192449dd946bSGreg Kurz if (fidp->fid_type != P9_FID_NONE) { 192549dd946bSGreg Kurz err = -EINVAL; 192649dd946bSGreg Kurz goto out; 192749dd946bSGreg Kurz } 19285b3c77aaSGreg Kurz v9fs_path_write_lock(s); 192960ce86c7SWei Liu v9fs_path_copy(&fidp->path, &path); 19305b3c77aaSGreg Kurz v9fs_path_unlock(s); 193160ce86c7SWei Liu } else { 193260ce86c7SWei Liu newfidp = alloc_fid(s, newfid); 193360ce86c7SWei Liu if (newfidp == NULL) { 193460ce86c7SWei Liu err = -EINVAL; 193560ce86c7SWei Liu goto out; 193660ce86c7SWei Liu } 193760ce86c7SWei Liu newfidp->uid = fidp->uid; 193860ce86c7SWei Liu v9fs_path_copy(&newfidp->path, &path); 193960ce86c7SWei Liu } 1940*a93d2e89SChristian Schoenebeck send_qids: 1941*a93d2e89SChristian Schoenebeck err = v9fs_walk_marshal(pdu, name_idx, qids); 1942*a93d2e89SChristian Schoenebeck trace_v9fs_walk_return(pdu->tag, pdu->id, name_idx, qids); 194360ce86c7SWei Liu out: 194460ce86c7SWei Liu put_fid(pdu, fidp); 194560ce86c7SWei Liu if (newfidp) { 194660ce86c7SWei Liu put_fid(pdu, newfidp); 194760ce86c7SWei Liu } 194860ce86c7SWei Liu v9fs_path_free(&dpath); 194960ce86c7SWei Liu v9fs_path_free(&path); 195060ce86c7SWei Liu out_nofid: 195160ce86c7SWei Liu pdu_complete(pdu, err); 195260ce86c7SWei Liu } 195360ce86c7SWei Liu 19548440e22eSGreg Kurz static int32_t coroutine_fn get_iounit(V9fsPDU *pdu, V9fsPath *path) 195560ce86c7SWei Liu { 195660ce86c7SWei Liu struct statfs stbuf; 1957b565bccbSChristian Schoenebeck int err = v9fs_co_statfs(pdu, path, &stbuf); 195860ce86c7SWei Liu 1959b565bccbSChristian Schoenebeck return blksize_to_iounit(pdu, (err >= 0) ? stbuf.f_bsize : 0); 196060ce86c7SWei Liu } 196160ce86c7SWei Liu 19628440e22eSGreg Kurz static void coroutine_fn v9fs_open(void *opaque) 196360ce86c7SWei Liu { 196460ce86c7SWei Liu int flags; 196560ce86c7SWei Liu int32_t fid; 196660ce86c7SWei Liu int32_t mode; 196760ce86c7SWei Liu V9fsQID qid; 196860ce86c7SWei Liu int iounit = 0; 196960ce86c7SWei Liu ssize_t err = 0; 197060ce86c7SWei Liu size_t offset = 7; 197160ce86c7SWei Liu struct stat stbuf; 197260ce86c7SWei Liu V9fsFidState *fidp; 197360ce86c7SWei Liu V9fsPDU *pdu = opaque; 197460ce86c7SWei Liu V9fsState *s = pdu->s; 197560ce86c7SWei Liu 197660ce86c7SWei Liu if (s->proto_version == V9FS_PROTO_2000L) { 197760ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode); 197860ce86c7SWei Liu } else { 197960ce86c7SWei Liu uint8_t modebyte; 198060ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte); 198160ce86c7SWei Liu mode = modebyte; 198260ce86c7SWei Liu } 198360ce86c7SWei Liu if (err < 0) { 198460ce86c7SWei Liu goto out_nofid; 198560ce86c7SWei Liu } 198660ce86c7SWei Liu trace_v9fs_open(pdu->tag, pdu->id, fid, mode); 198760ce86c7SWei Liu 198860ce86c7SWei Liu fidp = get_fid(pdu, fid); 198960ce86c7SWei Liu if (fidp == NULL) { 199060ce86c7SWei Liu err = -ENOENT; 199160ce86c7SWei Liu goto out_nofid; 199260ce86c7SWei Liu } 199349dd946bSGreg Kurz if (fidp->fid_type != P9_FID_NONE) { 199449dd946bSGreg Kurz err = -EINVAL; 199549dd946bSGreg Kurz goto out; 199649dd946bSGreg Kurz } 199760ce86c7SWei Liu 199860ce86c7SWei Liu err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); 199960ce86c7SWei Liu if (err < 0) { 200060ce86c7SWei Liu goto out; 200160ce86c7SWei Liu } 20023b5ee9e8SAntonios Motakis err = stat_to_qid(pdu, &stbuf, &qid); 20033b5ee9e8SAntonios Motakis if (err < 0) { 20043b5ee9e8SAntonios Motakis goto out; 20053b5ee9e8SAntonios Motakis } 200660ce86c7SWei Liu if (S_ISDIR(stbuf.st_mode)) { 200760ce86c7SWei Liu err = v9fs_co_opendir(pdu, fidp); 200860ce86c7SWei Liu if (err < 0) { 200960ce86c7SWei Liu goto out; 201060ce86c7SWei Liu } 201160ce86c7SWei Liu fidp->fid_type = P9_FID_DIR; 201260ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Qd", &qid, 0); 201360ce86c7SWei Liu if (err < 0) { 201460ce86c7SWei Liu goto out; 201560ce86c7SWei Liu } 201660ce86c7SWei Liu err += offset; 201760ce86c7SWei Liu } else { 201860ce86c7SWei Liu if (s->proto_version == V9FS_PROTO_2000L) { 201960ce86c7SWei Liu flags = get_dotl_openflags(s, mode); 202060ce86c7SWei Liu } else { 202160ce86c7SWei Liu flags = omode_to_uflags(mode); 202260ce86c7SWei Liu } 202360ce86c7SWei Liu if (is_ro_export(&s->ctx)) { 202460ce86c7SWei Liu if (mode & O_WRONLY || mode & O_RDWR || 202560ce86c7SWei Liu mode & O_APPEND || mode & O_TRUNC) { 202660ce86c7SWei Liu err = -EROFS; 202760ce86c7SWei Liu goto out; 202860ce86c7SWei Liu } 202960ce86c7SWei Liu } 203060ce86c7SWei Liu err = v9fs_co_open(pdu, fidp, flags); 203160ce86c7SWei Liu if (err < 0) { 203260ce86c7SWei Liu goto out; 203360ce86c7SWei Liu } 203460ce86c7SWei Liu fidp->fid_type = P9_FID_FILE; 203560ce86c7SWei Liu fidp->open_flags = flags; 203660ce86c7SWei Liu if (flags & O_EXCL) { 203760ce86c7SWei Liu /* 203860ce86c7SWei Liu * We let the host file system do O_EXCL check 203960ce86c7SWei Liu * We should not reclaim such fd 204060ce86c7SWei Liu */ 204160ce86c7SWei Liu fidp->flags |= FID_NON_RECLAIMABLE; 204260ce86c7SWei Liu } 204360ce86c7SWei Liu iounit = get_iounit(pdu, &fidp->path); 204460ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Qd", &qid, iounit); 204560ce86c7SWei Liu if (err < 0) { 204660ce86c7SWei Liu goto out; 204760ce86c7SWei Liu } 204860ce86c7SWei Liu err += offset; 204960ce86c7SWei Liu } 205060ce86c7SWei Liu trace_v9fs_open_return(pdu->tag, pdu->id, 205160ce86c7SWei Liu qid.type, qid.version, qid.path, iounit); 205260ce86c7SWei Liu out: 205360ce86c7SWei Liu put_fid(pdu, fidp); 205460ce86c7SWei Liu out_nofid: 205560ce86c7SWei Liu pdu_complete(pdu, err); 205660ce86c7SWei Liu } 205760ce86c7SWei Liu 20588440e22eSGreg Kurz static void coroutine_fn v9fs_lcreate(void *opaque) 205960ce86c7SWei Liu { 206060ce86c7SWei Liu int32_t dfid, flags, mode; 206160ce86c7SWei Liu gid_t gid; 206260ce86c7SWei Liu ssize_t err = 0; 206360ce86c7SWei Liu ssize_t offset = 7; 206460ce86c7SWei Liu V9fsString name; 206560ce86c7SWei Liu V9fsFidState *fidp; 206660ce86c7SWei Liu struct stat stbuf; 206760ce86c7SWei Liu V9fsQID qid; 206860ce86c7SWei Liu int32_t iounit; 206960ce86c7SWei Liu V9fsPDU *pdu = opaque; 207060ce86c7SWei Liu 207160ce86c7SWei Liu v9fs_string_init(&name); 207260ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dsddd", &dfid, 207360ce86c7SWei Liu &name, &flags, &mode, &gid); 207460ce86c7SWei Liu if (err < 0) { 207560ce86c7SWei Liu goto out_nofid; 207660ce86c7SWei Liu } 207760ce86c7SWei Liu trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid); 207860ce86c7SWei Liu 2079fff39a7aSGreg Kurz if (name_is_illegal(name.data)) { 2080fff39a7aSGreg Kurz err = -ENOENT; 2081fff39a7aSGreg Kurz goto out_nofid; 2082fff39a7aSGreg Kurz } 2083fff39a7aSGreg Kurz 2084805b5d98SGreg Kurz if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 2085805b5d98SGreg Kurz err = -EEXIST; 2086805b5d98SGreg Kurz goto out_nofid; 2087805b5d98SGreg Kurz } 2088805b5d98SGreg Kurz 208960ce86c7SWei Liu fidp = get_fid(pdu, dfid); 209060ce86c7SWei Liu if (fidp == NULL) { 209160ce86c7SWei Liu err = -ENOENT; 209260ce86c7SWei Liu goto out_nofid; 209360ce86c7SWei Liu } 2094d63fb193SLi Qiang if (fidp->fid_type != P9_FID_NONE) { 2095d63fb193SLi Qiang err = -EINVAL; 2096d63fb193SLi Qiang goto out; 2097d63fb193SLi Qiang } 209860ce86c7SWei Liu 209960ce86c7SWei Liu flags = get_dotl_openflags(pdu->s, flags); 210060ce86c7SWei Liu err = v9fs_co_open2(pdu, fidp, &name, gid, 210160ce86c7SWei Liu flags | O_CREAT, mode, &stbuf); 210260ce86c7SWei Liu if (err < 0) { 210360ce86c7SWei Liu goto out; 210460ce86c7SWei Liu } 210560ce86c7SWei Liu fidp->fid_type = P9_FID_FILE; 210660ce86c7SWei Liu fidp->open_flags = flags; 210760ce86c7SWei Liu if (flags & O_EXCL) { 210860ce86c7SWei Liu /* 210960ce86c7SWei Liu * We let the host file system do O_EXCL check 211060ce86c7SWei Liu * We should not reclaim such fd 211160ce86c7SWei Liu */ 211260ce86c7SWei Liu fidp->flags |= FID_NON_RECLAIMABLE; 211360ce86c7SWei Liu } 211460ce86c7SWei Liu iounit = get_iounit(pdu, &fidp->path); 21153b5ee9e8SAntonios Motakis err = stat_to_qid(pdu, &stbuf, &qid); 21163b5ee9e8SAntonios Motakis if (err < 0) { 21173b5ee9e8SAntonios Motakis goto out; 21183b5ee9e8SAntonios Motakis } 211960ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Qd", &qid, iounit); 212060ce86c7SWei Liu if (err < 0) { 212160ce86c7SWei Liu goto out; 212260ce86c7SWei Liu } 212360ce86c7SWei Liu err += offset; 212460ce86c7SWei Liu trace_v9fs_lcreate_return(pdu->tag, pdu->id, 212560ce86c7SWei Liu qid.type, qid.version, qid.path, iounit); 212660ce86c7SWei Liu out: 212760ce86c7SWei Liu put_fid(pdu, fidp); 212860ce86c7SWei Liu out_nofid: 212960ce86c7SWei Liu pdu_complete(pdu, err); 213060ce86c7SWei Liu v9fs_string_free(&name); 213160ce86c7SWei Liu } 213260ce86c7SWei Liu 2133a1bf8b74SGreg Kurz static void coroutine_fn v9fs_fsync(void *opaque) 213460ce86c7SWei Liu { 213560ce86c7SWei Liu int err; 213660ce86c7SWei Liu int32_t fid; 213760ce86c7SWei Liu int datasync; 213860ce86c7SWei Liu size_t offset = 7; 213960ce86c7SWei Liu V9fsFidState *fidp; 214060ce86c7SWei Liu V9fsPDU *pdu = opaque; 214160ce86c7SWei Liu 214260ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync); 214360ce86c7SWei Liu if (err < 0) { 214460ce86c7SWei Liu goto out_nofid; 214560ce86c7SWei Liu } 214660ce86c7SWei Liu trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync); 214760ce86c7SWei Liu 214860ce86c7SWei Liu fidp = get_fid(pdu, fid); 214960ce86c7SWei Liu if (fidp == NULL) { 215060ce86c7SWei Liu err = -ENOENT; 215160ce86c7SWei Liu goto out_nofid; 215260ce86c7SWei Liu } 215360ce86c7SWei Liu err = v9fs_co_fsync(pdu, fidp, datasync); 215460ce86c7SWei Liu if (!err) { 215560ce86c7SWei Liu err = offset; 215660ce86c7SWei Liu } 215760ce86c7SWei Liu put_fid(pdu, fidp); 215860ce86c7SWei Liu out_nofid: 215960ce86c7SWei Liu pdu_complete(pdu, err); 216060ce86c7SWei Liu } 216160ce86c7SWei Liu 21628440e22eSGreg Kurz static void coroutine_fn v9fs_clunk(void *opaque) 216360ce86c7SWei Liu { 216460ce86c7SWei Liu int err; 216560ce86c7SWei Liu int32_t fid; 216660ce86c7SWei Liu size_t offset = 7; 216760ce86c7SWei Liu V9fsFidState *fidp; 216860ce86c7SWei Liu V9fsPDU *pdu = opaque; 216960ce86c7SWei Liu V9fsState *s = pdu->s; 217060ce86c7SWei Liu 217160ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "d", &fid); 217260ce86c7SWei Liu if (err < 0) { 217360ce86c7SWei Liu goto out_nofid; 217460ce86c7SWei Liu } 217560ce86c7SWei Liu trace_v9fs_clunk(pdu->tag, pdu->id, fid); 217660ce86c7SWei Liu 217760ce86c7SWei Liu fidp = clunk_fid(s, fid); 217860ce86c7SWei Liu if (fidp == NULL) { 217960ce86c7SWei Liu err = -ENOENT; 218060ce86c7SWei Liu goto out_nofid; 218160ce86c7SWei Liu } 218260ce86c7SWei Liu /* 218360ce86c7SWei Liu * Bump the ref so that put_fid will 218460ce86c7SWei Liu * free the fid. 218560ce86c7SWei Liu */ 218660ce86c7SWei Liu fidp->ref++; 218760ce86c7SWei Liu err = put_fid(pdu, fidp); 218860ce86c7SWei Liu if (!err) { 218960ce86c7SWei Liu err = offset; 219060ce86c7SWei Liu } 219160ce86c7SWei Liu out_nofid: 219260ce86c7SWei Liu pdu_complete(pdu, err); 219360ce86c7SWei Liu } 219460ce86c7SWei Liu 2195bcb8998fSStefano Stabellini /* 2196bcb8998fSStefano Stabellini * Create a QEMUIOVector for a sub-region of PDU iovecs 2197bcb8998fSStefano Stabellini * 2198bcb8998fSStefano Stabellini * @qiov: uninitialized QEMUIOVector 2199bcb8998fSStefano Stabellini * @skip: number of bytes to skip from beginning of PDU 2200bcb8998fSStefano Stabellini * @size: number of bytes to include 2201bcb8998fSStefano Stabellini * @is_write: true - write, false - read 2202bcb8998fSStefano Stabellini * 2203bcb8998fSStefano Stabellini * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up 2204bcb8998fSStefano Stabellini * with qemu_iovec_destroy(). 2205bcb8998fSStefano Stabellini */ 2206bcb8998fSStefano Stabellini static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu, 2207cf45183bSStefano Stabellini size_t skip, size_t size, 2208bcb8998fSStefano Stabellini bool is_write) 2209bcb8998fSStefano Stabellini { 2210bcb8998fSStefano Stabellini QEMUIOVector elem; 2211bcb8998fSStefano Stabellini struct iovec *iov; 2212bcb8998fSStefano Stabellini unsigned int niov; 2213bcb8998fSStefano Stabellini 221488da0b03SStefano Stabellini if (is_write) { 2215cf45183bSStefano Stabellini pdu->s->transport->init_out_iov_from_pdu(pdu, &iov, &niov, size + skip); 221688da0b03SStefano Stabellini } else { 2217cf45183bSStefano Stabellini pdu->s->transport->init_in_iov_from_pdu(pdu, &iov, &niov, size + skip); 221888da0b03SStefano Stabellini } 2219bcb8998fSStefano Stabellini 2220bcb8998fSStefano Stabellini qemu_iovec_init_external(&elem, iov, niov); 2221bcb8998fSStefano Stabellini qemu_iovec_init(qiov, niov); 2222cf45183bSStefano Stabellini qemu_iovec_concat(qiov, &elem, skip, size); 2223bcb8998fSStefano Stabellini } 2224bcb8998fSStefano Stabellini 222560ce86c7SWei Liu static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp, 222660ce86c7SWei Liu uint64_t off, uint32_t max_count) 222760ce86c7SWei Liu { 222860ce86c7SWei Liu ssize_t err; 222960ce86c7SWei Liu size_t offset = 7; 2230cf45183bSStefano Stabellini uint64_t read_count; 2231bcb8998fSStefano Stabellini QEMUIOVector qiov_full; 223260ce86c7SWei Liu 22337e55d65cSLi Qiang if (fidp->fs.xattr.len < off) { 22347e55d65cSLi Qiang read_count = 0; 223516724a17SGreg Kurz } else { 2236cf45183bSStefano Stabellini read_count = fidp->fs.xattr.len - off; 2237cf45183bSStefano Stabellini } 2238cf45183bSStefano Stabellini if (read_count > max_count) { 223960ce86c7SWei Liu read_count = max_count; 224060ce86c7SWei Liu } 224160ce86c7SWei Liu err = pdu_marshal(pdu, offset, "d", read_count); 224260ce86c7SWei Liu if (err < 0) { 224360ce86c7SWei Liu return err; 224460ce86c7SWei Liu } 224560ce86c7SWei Liu offset += err; 224600588a0aSWei Liu 2247cf45183bSStefano Stabellini v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, read_count, false); 2248fa0eb5c5SGreg Kurz err = v9fs_pack(qiov_full.iov, qiov_full.niov, 0, 224960ce86c7SWei Liu ((char *)fidp->fs.xattr.value) + off, 225060ce86c7SWei Liu read_count); 2251bcb8998fSStefano Stabellini qemu_iovec_destroy(&qiov_full); 225260ce86c7SWei Liu if (err < 0) { 225360ce86c7SWei Liu return err; 225460ce86c7SWei Liu } 225560ce86c7SWei Liu offset += err; 225660ce86c7SWei Liu return offset; 225760ce86c7SWei Liu } 225860ce86c7SWei Liu 22598440e22eSGreg Kurz static int coroutine_fn v9fs_do_readdir_with_stat(V9fsPDU *pdu, 22608440e22eSGreg Kurz V9fsFidState *fidp, 22618440e22eSGreg Kurz uint32_t max_count) 226260ce86c7SWei Liu { 226360ce86c7SWei Liu V9fsPath path; 226460ce86c7SWei Liu V9fsStat v9stat; 226560ce86c7SWei Liu int len, err = 0; 226660ce86c7SWei Liu int32_t count = 0; 226760ce86c7SWei Liu struct stat stbuf; 226860ce86c7SWei Liu off_t saved_dir_pos; 2269635324e8SGreg Kurz struct dirent *dent; 227060ce86c7SWei Liu 227160ce86c7SWei Liu /* save the directory position */ 227260ce86c7SWei Liu saved_dir_pos = v9fs_co_telldir(pdu, fidp); 227360ce86c7SWei Liu if (saved_dir_pos < 0) { 227460ce86c7SWei Liu return saved_dir_pos; 227560ce86c7SWei Liu } 227660ce86c7SWei Liu 227760ce86c7SWei Liu while (1) { 227860ce86c7SWei Liu v9fs_path_init(&path); 22797cde47d4SGreg Kurz 22807cde47d4SGreg Kurz v9fs_readdir_lock(&fidp->fs.dir); 22817cde47d4SGreg Kurz 2282635324e8SGreg Kurz err = v9fs_co_readdir(pdu, fidp, &dent); 2283635324e8SGreg Kurz if (err || !dent) { 228460ce86c7SWei Liu break; 228560ce86c7SWei Liu } 228660ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path); 228760ce86c7SWei Liu if (err < 0) { 22888762a46dSGreg Kurz break; 228960ce86c7SWei Liu } 229060ce86c7SWei Liu err = v9fs_co_lstat(pdu, &path, &stbuf); 229160ce86c7SWei Liu if (err < 0) { 22928762a46dSGreg Kurz break; 229360ce86c7SWei Liu } 22946069537fSJan Dakinevich err = stat_to_v9stat(pdu, &path, dent->d_name, &stbuf, &v9stat); 229560ce86c7SWei Liu if (err < 0) { 22968762a46dSGreg Kurz break; 229760ce86c7SWei Liu } 2298772a7369SJan Dakinevich if ((count + v9stat.size + 2) > max_count) { 22997cde47d4SGreg Kurz v9fs_readdir_unlock(&fidp->fs.dir); 23007cde47d4SGreg Kurz 230160ce86c7SWei Liu /* Ran out of buffer. Set dir back to old position and return */ 230260ce86c7SWei Liu v9fs_co_seekdir(pdu, fidp, saved_dir_pos); 230360ce86c7SWei Liu v9fs_stat_free(&v9stat); 230460ce86c7SWei Liu v9fs_path_free(&path); 230560ce86c7SWei Liu return count; 230660ce86c7SWei Liu } 2307772a7369SJan Dakinevich 2308772a7369SJan Dakinevich /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */ 2309772a7369SJan Dakinevich len = pdu_marshal(pdu, 11 + count, "S", &v9stat); 2310772a7369SJan Dakinevich 2311772a7369SJan Dakinevich v9fs_readdir_unlock(&fidp->fs.dir); 2312772a7369SJan Dakinevich 2313772a7369SJan Dakinevich if (len < 0) { 2314772a7369SJan Dakinevich v9fs_co_seekdir(pdu, fidp, saved_dir_pos); 2315772a7369SJan Dakinevich v9fs_stat_free(&v9stat); 2316772a7369SJan Dakinevich v9fs_path_free(&path); 2317772a7369SJan Dakinevich return len; 2318772a7369SJan Dakinevich } 231960ce86c7SWei Liu count += len; 232060ce86c7SWei Liu v9fs_stat_free(&v9stat); 232160ce86c7SWei Liu v9fs_path_free(&path); 23226b3b279bSKeno Fischer saved_dir_pos = qemu_dirent_off(dent); 232360ce86c7SWei Liu } 23248762a46dSGreg Kurz 23257cde47d4SGreg Kurz v9fs_readdir_unlock(&fidp->fs.dir); 23267cde47d4SGreg Kurz 232760ce86c7SWei Liu v9fs_path_free(&path); 232860ce86c7SWei Liu if (err < 0) { 232960ce86c7SWei Liu return err; 233060ce86c7SWei Liu } 233160ce86c7SWei Liu return count; 233260ce86c7SWei Liu } 233360ce86c7SWei Liu 23348440e22eSGreg Kurz static void coroutine_fn v9fs_read(void *opaque) 233560ce86c7SWei Liu { 233660ce86c7SWei Liu int32_t fid; 233760ce86c7SWei Liu uint64_t off; 233860ce86c7SWei Liu ssize_t err = 0; 233960ce86c7SWei Liu int32_t count = 0; 234060ce86c7SWei Liu size_t offset = 7; 234160ce86c7SWei Liu uint32_t max_count; 234260ce86c7SWei Liu V9fsFidState *fidp; 234360ce86c7SWei Liu V9fsPDU *pdu = opaque; 234460ce86c7SWei Liu V9fsState *s = pdu->s; 234560ce86c7SWei Liu 234660ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count); 234760ce86c7SWei Liu if (err < 0) { 234860ce86c7SWei Liu goto out_nofid; 234960ce86c7SWei Liu } 235060ce86c7SWei Liu trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count); 235160ce86c7SWei Liu 235260ce86c7SWei Liu fidp = get_fid(pdu, fid); 235360ce86c7SWei Liu if (fidp == NULL) { 235460ce86c7SWei Liu err = -EINVAL; 235560ce86c7SWei Liu goto out_nofid; 235660ce86c7SWei Liu } 235760ce86c7SWei Liu if (fidp->fid_type == P9_FID_DIR) { 2358d2c5cf7cSChristian Schoenebeck if (s->proto_version != V9FS_PROTO_2000U) { 2359d2c5cf7cSChristian Schoenebeck warn_report_once( 2360d2c5cf7cSChristian Schoenebeck "9p: bad client: T_read request on directory only expected " 2361d2c5cf7cSChristian Schoenebeck "with 9P2000.u protocol version" 2362d2c5cf7cSChristian Schoenebeck ); 2363d2c5cf7cSChristian Schoenebeck err = -EOPNOTSUPP; 2364d2c5cf7cSChristian Schoenebeck goto out; 2365d2c5cf7cSChristian Schoenebeck } 236660ce86c7SWei Liu if (off == 0) { 236760ce86c7SWei Liu v9fs_co_rewinddir(pdu, fidp); 236860ce86c7SWei Liu } 236960ce86c7SWei Liu count = v9fs_do_readdir_with_stat(pdu, fidp, max_count); 237060ce86c7SWei Liu if (count < 0) { 237160ce86c7SWei Liu err = count; 237260ce86c7SWei Liu goto out; 237360ce86c7SWei Liu } 237460ce86c7SWei Liu err = pdu_marshal(pdu, offset, "d", count); 237560ce86c7SWei Liu if (err < 0) { 237660ce86c7SWei Liu goto out; 237760ce86c7SWei Liu } 237860ce86c7SWei Liu err += offset + count; 237960ce86c7SWei Liu } else if (fidp->fid_type == P9_FID_FILE) { 238060ce86c7SWei Liu QEMUIOVector qiov_full; 238160ce86c7SWei Liu QEMUIOVector qiov; 238260ce86c7SWei Liu int32_t len; 238360ce86c7SWei Liu 2384cf45183bSStefano Stabellini v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, max_count, false); 238560ce86c7SWei Liu qemu_iovec_init(&qiov, qiov_full.niov); 238660ce86c7SWei Liu do { 238760ce86c7SWei Liu qemu_iovec_reset(&qiov); 238860ce86c7SWei Liu qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count); 238960ce86c7SWei Liu if (0) { 239060ce86c7SWei Liu print_sg(qiov.iov, qiov.niov); 239160ce86c7SWei Liu } 239260ce86c7SWei Liu /* Loop in case of EINTR */ 239360ce86c7SWei Liu do { 239460ce86c7SWei Liu len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off); 239560ce86c7SWei Liu if (len >= 0) { 239660ce86c7SWei Liu off += len; 239760ce86c7SWei Liu count += len; 239860ce86c7SWei Liu } 239960ce86c7SWei Liu } while (len == -EINTR && !pdu->cancelled); 240060ce86c7SWei Liu if (len < 0) { 240160ce86c7SWei Liu /* IO error return the error */ 240260ce86c7SWei Liu err = len; 2403e95c9a49SLi Qiang goto out_free_iovec; 240460ce86c7SWei Liu } 240560ce86c7SWei Liu } while (count < max_count && len > 0); 240660ce86c7SWei Liu err = pdu_marshal(pdu, offset, "d", count); 240760ce86c7SWei Liu if (err < 0) { 2408e95c9a49SLi Qiang goto out_free_iovec; 240960ce86c7SWei Liu } 241060ce86c7SWei Liu err += offset + count; 2411e95c9a49SLi Qiang out_free_iovec: 241260ce86c7SWei Liu qemu_iovec_destroy(&qiov); 241360ce86c7SWei Liu qemu_iovec_destroy(&qiov_full); 241460ce86c7SWei Liu } else if (fidp->fid_type == P9_FID_XATTR) { 241560ce86c7SWei Liu err = v9fs_xattr_read(s, pdu, fidp, off, max_count); 241660ce86c7SWei Liu } else { 241760ce86c7SWei Liu err = -EINVAL; 241860ce86c7SWei Liu } 241960ce86c7SWei Liu trace_v9fs_read_return(pdu->tag, pdu->id, count, err); 242060ce86c7SWei Liu out: 242160ce86c7SWei Liu put_fid(pdu, fidp); 242260ce86c7SWei Liu out_nofid: 242360ce86c7SWei Liu pdu_complete(pdu, err); 242460ce86c7SWei Liu } 242560ce86c7SWei Liu 242629c9d2caSChristian Schoenebeck /** 2427e16fea41SChristian Schoenebeck * v9fs_readdir_response_size() - Returns size required in Rreaddir response 2428e16fea41SChristian Schoenebeck * for the passed dirent @name. 242929c9d2caSChristian Schoenebeck * 2430e16fea41SChristian Schoenebeck * @name: directory entry's name (i.e. file name, directory name) 2431e16fea41SChristian Schoenebeck * Return: required size in bytes 243229c9d2caSChristian Schoenebeck */ 243329c9d2caSChristian Schoenebeck size_t v9fs_readdir_response_size(V9fsString *name) 243460ce86c7SWei Liu { 243560ce86c7SWei Liu /* 243660ce86c7SWei Liu * Size of each dirent on the wire: size of qid (13) + size of offset (8) 243760ce86c7SWei Liu * size of type (1) + size of name.size (2) + strlen(name.data) 243860ce86c7SWei Liu */ 243960ce86c7SWei Liu return 24 + v9fs_string_size(name); 244060ce86c7SWei Liu } 244160ce86c7SWei Liu 24420c4356baSChristian Schoenebeck static void v9fs_free_dirents(struct V9fsDirEnt *e) 24430c4356baSChristian Schoenebeck { 24440c4356baSChristian Schoenebeck struct V9fsDirEnt *next = NULL; 24450c4356baSChristian Schoenebeck 24460c4356baSChristian Schoenebeck for (; e; e = next) { 24470c4356baSChristian Schoenebeck next = e->next; 24480c4356baSChristian Schoenebeck g_free(e->dent); 24490c4356baSChristian Schoenebeck g_free(e->st); 24500c4356baSChristian Schoenebeck g_free(e); 24510c4356baSChristian Schoenebeck } 24520c4356baSChristian Schoenebeck } 24530c4356baSChristian Schoenebeck 24548440e22eSGreg Kurz static int coroutine_fn v9fs_do_readdir(V9fsPDU *pdu, V9fsFidState *fidp, 24550c4356baSChristian Schoenebeck off_t offset, int32_t max_count) 245660ce86c7SWei Liu { 245760ce86c7SWei Liu size_t size; 245860ce86c7SWei Liu V9fsQID qid; 245960ce86c7SWei Liu V9fsString name; 246060ce86c7SWei Liu int len, err = 0; 246160ce86c7SWei Liu int32_t count = 0; 24626b3b279bSKeno Fischer off_t off; 2463635324e8SGreg Kurz struct dirent *dent; 24640c4356baSChristian Schoenebeck struct stat *st; 24650c4356baSChristian Schoenebeck struct V9fsDirEnt *entries = NULL; 246660ce86c7SWei Liu 24670c4356baSChristian Schoenebeck /* 24680c4356baSChristian Schoenebeck * inode remapping requires the device id, which in turn might be 24690c4356baSChristian Schoenebeck * different for different directory entries, so if inode remapping is 24700c4356baSChristian Schoenebeck * enabled we have to make a full stat for each directory entry 24710c4356baSChristian Schoenebeck */ 24720c4356baSChristian Schoenebeck const bool dostat = pdu->s->ctx.export_flags & V9FS_REMAP_INODES; 24730c4356baSChristian Schoenebeck 24740c4356baSChristian Schoenebeck /* 24750c4356baSChristian Schoenebeck * Fetch all required directory entries altogether on a background IO 24760c4356baSChristian Schoenebeck * thread from fs driver. We don't want to do that for each entry 24770c4356baSChristian Schoenebeck * individually, because hopping between threads (this main IO thread 24780c4356baSChristian Schoenebeck * and background IO driver thread) would sum up to huge latencies. 24790c4356baSChristian Schoenebeck */ 24800c4356baSChristian Schoenebeck count = v9fs_co_readdir_many(pdu, fidp, &entries, offset, max_count, 24810c4356baSChristian Schoenebeck dostat); 24820c4356baSChristian Schoenebeck if (count < 0) { 24830c4356baSChristian Schoenebeck err = count; 24840c4356baSChristian Schoenebeck count = 0; 24850c4356baSChristian Schoenebeck goto out; 248660ce86c7SWei Liu } 24870c4356baSChristian Schoenebeck count = 0; 248860ce86c7SWei Liu 24890c4356baSChristian Schoenebeck for (struct V9fsDirEnt *e = entries; e; e = e->next) { 24900c4356baSChristian Schoenebeck dent = e->dent; 24911a6ed33cSAntonios Motakis 24921a6ed33cSAntonios Motakis if (pdu->s->ctx.export_flags & V9FS_REMAP_INODES) { 24930c4356baSChristian Schoenebeck st = e->st; 24940c4356baSChristian Schoenebeck /* e->st should never be NULL, but just to be sure */ 24950c4356baSChristian Schoenebeck if (!st) { 24960c4356baSChristian Schoenebeck err = -1; 24970c4356baSChristian Schoenebeck break; 24980c4356baSChristian Schoenebeck } 24990c4356baSChristian Schoenebeck 25000c4356baSChristian Schoenebeck /* remap inode */ 25010c4356baSChristian Schoenebeck err = stat_to_qid(pdu, st, &qid); 25021a6ed33cSAntonios Motakis if (err < 0) { 25030c4356baSChristian Schoenebeck break; 25041a6ed33cSAntonios Motakis } 25051a6ed33cSAntonios Motakis } else { 250660ce86c7SWei Liu /* 250760ce86c7SWei Liu * Fill up just the path field of qid because the client uses 250860ce86c7SWei Liu * only that. To fill the entire qid structure we will have 25091a6ed33cSAntonios Motakis * to stat each dirent found, which is expensive. For the 25100c4356baSChristian Schoenebeck * latter reason we don't call stat_to_qid() here. Only drawback 25111a6ed33cSAntonios Motakis * is that no multi-device export detection of stat_to_qid() 25121a6ed33cSAntonios Motakis * would be done and provided as error to the user here. But 25131a6ed33cSAntonios Motakis * user would get that error anyway when accessing those 25141a6ed33cSAntonios Motakis * files/dirs through other ways. 251560ce86c7SWei Liu */ 251660ce86c7SWei Liu size = MIN(sizeof(dent->d_ino), sizeof(qid.path)); 251760ce86c7SWei Liu memcpy(&qid.path, &dent->d_ino, size); 251860ce86c7SWei Liu /* Fill the other fields with dummy values */ 251960ce86c7SWei Liu qid.type = 0; 252060ce86c7SWei Liu qid.version = 0; 25211a6ed33cSAntonios Motakis } 252260ce86c7SWei Liu 25236b3b279bSKeno Fischer off = qemu_dirent_off(dent); 25240c4356baSChristian Schoenebeck v9fs_string_init(&name); 25250c4356baSChristian Schoenebeck v9fs_string_sprintf(&name, "%s", dent->d_name); 25260c4356baSChristian Schoenebeck 252760ce86c7SWei Liu /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */ 252860ce86c7SWei Liu len = pdu_marshal(pdu, 11 + count, "Qqbs", 25296b3b279bSKeno Fischer &qid, off, 253060ce86c7SWei Liu dent->d_type, &name); 25317cde47d4SGreg Kurz 25320c4356baSChristian Schoenebeck v9fs_string_free(&name); 25337cde47d4SGreg Kurz 253460ce86c7SWei Liu if (len < 0) { 25350c4356baSChristian Schoenebeck err = len; 25360c4356baSChristian Schoenebeck break; 253760ce86c7SWei Liu } 25380c4356baSChristian Schoenebeck 253960ce86c7SWei Liu count += len; 254060ce86c7SWei Liu } 25417cde47d4SGreg Kurz 25420c4356baSChristian Schoenebeck out: 25430c4356baSChristian Schoenebeck v9fs_free_dirents(entries); 254460ce86c7SWei Liu if (err < 0) { 254560ce86c7SWei Liu return err; 254660ce86c7SWei Liu } 254760ce86c7SWei Liu return count; 254860ce86c7SWei Liu } 254960ce86c7SWei Liu 25508440e22eSGreg Kurz static void coroutine_fn v9fs_readdir(void *opaque) 255160ce86c7SWei Liu { 255260ce86c7SWei Liu int32_t fid; 255360ce86c7SWei Liu V9fsFidState *fidp; 255460ce86c7SWei Liu ssize_t retval = 0; 255560ce86c7SWei Liu size_t offset = 7; 255660ce86c7SWei Liu uint64_t initial_offset; 255760ce86c7SWei Liu int32_t count; 255860ce86c7SWei Liu uint32_t max_count; 255960ce86c7SWei Liu V9fsPDU *pdu = opaque; 2560d36a5c22SChristian Schoenebeck V9fsState *s = pdu->s; 256160ce86c7SWei Liu 256260ce86c7SWei Liu retval = pdu_unmarshal(pdu, offset, "dqd", &fid, 256360ce86c7SWei Liu &initial_offset, &max_count); 256460ce86c7SWei Liu if (retval < 0) { 256560ce86c7SWei Liu goto out_nofid; 256660ce86c7SWei Liu } 256760ce86c7SWei Liu trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count); 256860ce86c7SWei Liu 2569d36a5c22SChristian Schoenebeck /* Enough space for a R_readdir header: size[4] Rreaddir tag[2] count[4] */ 2570d36a5c22SChristian Schoenebeck if (max_count > s->msize - 11) { 2571d36a5c22SChristian Schoenebeck max_count = s->msize - 11; 2572d36a5c22SChristian Schoenebeck warn_report_once( 2573d36a5c22SChristian Schoenebeck "9p: bad client: T_readdir with count > msize - 11" 2574d36a5c22SChristian Schoenebeck ); 2575d36a5c22SChristian Schoenebeck } 2576d36a5c22SChristian Schoenebeck 257760ce86c7SWei Liu fidp = get_fid(pdu, fid); 257860ce86c7SWei Liu if (fidp == NULL) { 257960ce86c7SWei Liu retval = -EINVAL; 258060ce86c7SWei Liu goto out_nofid; 258160ce86c7SWei Liu } 2582f314ea4eSGreg Kurz if (!fidp->fs.dir.stream) { 258360ce86c7SWei Liu retval = -EINVAL; 258460ce86c7SWei Liu goto out; 258560ce86c7SWei Liu } 2586d2c5cf7cSChristian Schoenebeck if (s->proto_version != V9FS_PROTO_2000L) { 2587d2c5cf7cSChristian Schoenebeck warn_report_once( 2588d2c5cf7cSChristian Schoenebeck "9p: bad client: T_readdir request only expected with 9P2000.L " 2589d2c5cf7cSChristian Schoenebeck "protocol version" 2590d2c5cf7cSChristian Schoenebeck ); 2591d2c5cf7cSChristian Schoenebeck retval = -EOPNOTSUPP; 2592d2c5cf7cSChristian Schoenebeck goto out; 2593d2c5cf7cSChristian Schoenebeck } 25940c4356baSChristian Schoenebeck count = v9fs_do_readdir(pdu, fidp, (off_t) initial_offset, max_count); 259560ce86c7SWei Liu if (count < 0) { 259660ce86c7SWei Liu retval = count; 259760ce86c7SWei Liu goto out; 259860ce86c7SWei Liu } 259960ce86c7SWei Liu retval = pdu_marshal(pdu, offset, "d", count); 260060ce86c7SWei Liu if (retval < 0) { 260160ce86c7SWei Liu goto out; 260260ce86c7SWei Liu } 260360ce86c7SWei Liu retval += count + offset; 260460ce86c7SWei Liu trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval); 260560ce86c7SWei Liu out: 260660ce86c7SWei Liu put_fid(pdu, fidp); 260760ce86c7SWei Liu out_nofid: 260860ce86c7SWei Liu pdu_complete(pdu, retval); 260960ce86c7SWei Liu } 261060ce86c7SWei Liu 261160ce86c7SWei Liu static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp, 261260ce86c7SWei Liu uint64_t off, uint32_t count, 261360ce86c7SWei Liu struct iovec *sg, int cnt) 261460ce86c7SWei Liu { 261560ce86c7SWei Liu int i, to_copy; 261660ce86c7SWei Liu ssize_t err = 0; 26177e55d65cSLi Qiang uint64_t write_count; 261860ce86c7SWei Liu size_t offset = 7; 261960ce86c7SWei Liu 262060ce86c7SWei Liu 26217e55d65cSLi Qiang if (fidp->fs.xattr.len < off) { 2622b858e80aSDaniel Henrique Barboza return -ENOSPC; 262360ce86c7SWei Liu } 26247e55d65cSLi Qiang write_count = fidp->fs.xattr.len - off; 26257e55d65cSLi Qiang if (write_count > count) { 26267e55d65cSLi Qiang write_count = count; 26277e55d65cSLi Qiang } 262860ce86c7SWei Liu err = pdu_marshal(pdu, offset, "d", write_count); 262960ce86c7SWei Liu if (err < 0) { 263060ce86c7SWei Liu return err; 263160ce86c7SWei Liu } 263260ce86c7SWei Liu err += offset; 263360ce86c7SWei Liu fidp->fs.xattr.copied_len += write_count; 263460ce86c7SWei Liu /* 263560ce86c7SWei Liu * Now copy the content from sg list 263660ce86c7SWei Liu */ 263760ce86c7SWei Liu for (i = 0; i < cnt; i++) { 263860ce86c7SWei Liu if (write_count > sg[i].iov_len) { 263960ce86c7SWei Liu to_copy = sg[i].iov_len; 264060ce86c7SWei Liu } else { 264160ce86c7SWei Liu to_copy = write_count; 264260ce86c7SWei Liu } 264360ce86c7SWei Liu memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy); 264460ce86c7SWei Liu /* updating vs->off since we are not using below */ 264560ce86c7SWei Liu off += to_copy; 264660ce86c7SWei Liu write_count -= to_copy; 264760ce86c7SWei Liu } 2648b858e80aSDaniel Henrique Barboza 264960ce86c7SWei Liu return err; 265060ce86c7SWei Liu } 265160ce86c7SWei Liu 26528440e22eSGreg Kurz static void coroutine_fn v9fs_write(void *opaque) 265360ce86c7SWei Liu { 265460ce86c7SWei Liu ssize_t err; 265560ce86c7SWei Liu int32_t fid; 265660ce86c7SWei Liu uint64_t off; 265760ce86c7SWei Liu uint32_t count; 265860ce86c7SWei Liu int32_t len = 0; 265960ce86c7SWei Liu int32_t total = 0; 266060ce86c7SWei Liu size_t offset = 7; 266160ce86c7SWei Liu V9fsFidState *fidp; 266260ce86c7SWei Liu V9fsPDU *pdu = opaque; 266360ce86c7SWei Liu V9fsState *s = pdu->s; 266460ce86c7SWei Liu QEMUIOVector qiov_full; 266560ce86c7SWei Liu QEMUIOVector qiov; 266660ce86c7SWei Liu 266760ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count); 266860ce86c7SWei Liu if (err < 0) { 266960ce86c7SWei Liu pdu_complete(pdu, err); 267060ce86c7SWei Liu return; 267160ce86c7SWei Liu } 267260ce86c7SWei Liu offset += err; 2673cf45183bSStefano Stabellini v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, count, true); 267460ce86c7SWei Liu trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov); 267560ce86c7SWei Liu 267660ce86c7SWei Liu fidp = get_fid(pdu, fid); 267760ce86c7SWei Liu if (fidp == NULL) { 267860ce86c7SWei Liu err = -EINVAL; 267960ce86c7SWei Liu goto out_nofid; 268060ce86c7SWei Liu } 268160ce86c7SWei Liu if (fidp->fid_type == P9_FID_FILE) { 268260ce86c7SWei Liu if (fidp->fs.fd == -1) { 268360ce86c7SWei Liu err = -EINVAL; 268460ce86c7SWei Liu goto out; 268560ce86c7SWei Liu } 268660ce86c7SWei Liu } else if (fidp->fid_type == P9_FID_XATTR) { 268760ce86c7SWei Liu /* 268860ce86c7SWei Liu * setxattr operation 268960ce86c7SWei Liu */ 269060ce86c7SWei Liu err = v9fs_xattr_write(s, pdu, fidp, off, count, 269160ce86c7SWei Liu qiov_full.iov, qiov_full.niov); 269260ce86c7SWei Liu goto out; 269360ce86c7SWei Liu } else { 269460ce86c7SWei Liu err = -EINVAL; 269560ce86c7SWei Liu goto out; 269660ce86c7SWei Liu } 269760ce86c7SWei Liu qemu_iovec_init(&qiov, qiov_full.niov); 269860ce86c7SWei Liu do { 269960ce86c7SWei Liu qemu_iovec_reset(&qiov); 270060ce86c7SWei Liu qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total); 270160ce86c7SWei Liu if (0) { 270260ce86c7SWei Liu print_sg(qiov.iov, qiov.niov); 270360ce86c7SWei Liu } 270460ce86c7SWei Liu /* Loop in case of EINTR */ 270560ce86c7SWei Liu do { 270660ce86c7SWei Liu len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off); 270760ce86c7SWei Liu if (len >= 0) { 270860ce86c7SWei Liu off += len; 270960ce86c7SWei Liu total += len; 271060ce86c7SWei Liu } 271160ce86c7SWei Liu } while (len == -EINTR && !pdu->cancelled); 271260ce86c7SWei Liu if (len < 0) { 271360ce86c7SWei Liu /* IO error return the error */ 271460ce86c7SWei Liu err = len; 271560ce86c7SWei Liu goto out_qiov; 271660ce86c7SWei Liu } 271760ce86c7SWei Liu } while (total < count && len > 0); 271860ce86c7SWei Liu 271960ce86c7SWei Liu offset = 7; 272060ce86c7SWei Liu err = pdu_marshal(pdu, offset, "d", total); 272160ce86c7SWei Liu if (err < 0) { 2722fdfcc9aeSLi Qiang goto out_qiov; 272360ce86c7SWei Liu } 272460ce86c7SWei Liu err += offset; 272560ce86c7SWei Liu trace_v9fs_write_return(pdu->tag, pdu->id, total, err); 272660ce86c7SWei Liu out_qiov: 272760ce86c7SWei Liu qemu_iovec_destroy(&qiov); 272860ce86c7SWei Liu out: 272960ce86c7SWei Liu put_fid(pdu, fidp); 273060ce86c7SWei Liu out_nofid: 273160ce86c7SWei Liu qemu_iovec_destroy(&qiov_full); 273260ce86c7SWei Liu pdu_complete(pdu, err); 273360ce86c7SWei Liu } 273460ce86c7SWei Liu 27358440e22eSGreg Kurz static void coroutine_fn v9fs_create(void *opaque) 273660ce86c7SWei Liu { 273760ce86c7SWei Liu int32_t fid; 273860ce86c7SWei Liu int err = 0; 273960ce86c7SWei Liu size_t offset = 7; 274060ce86c7SWei Liu V9fsFidState *fidp; 274160ce86c7SWei Liu V9fsQID qid; 274260ce86c7SWei Liu int32_t perm; 274360ce86c7SWei Liu int8_t mode; 274460ce86c7SWei Liu V9fsPath path; 274560ce86c7SWei Liu struct stat stbuf; 274660ce86c7SWei Liu V9fsString name; 274760ce86c7SWei Liu V9fsString extension; 274860ce86c7SWei Liu int iounit; 274960ce86c7SWei Liu V9fsPDU *pdu = opaque; 27505b3c77aaSGreg Kurz V9fsState *s = pdu->s; 275160ce86c7SWei Liu 275260ce86c7SWei Liu v9fs_path_init(&path); 275360ce86c7SWei Liu v9fs_string_init(&name); 275460ce86c7SWei Liu v9fs_string_init(&extension); 275560ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name, 275660ce86c7SWei Liu &perm, &mode, &extension); 275760ce86c7SWei Liu if (err < 0) { 275860ce86c7SWei Liu goto out_nofid; 275960ce86c7SWei Liu } 276060ce86c7SWei Liu trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode); 276160ce86c7SWei Liu 2762fff39a7aSGreg Kurz if (name_is_illegal(name.data)) { 2763fff39a7aSGreg Kurz err = -ENOENT; 2764fff39a7aSGreg Kurz goto out_nofid; 2765fff39a7aSGreg Kurz } 2766fff39a7aSGreg Kurz 2767805b5d98SGreg Kurz if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 2768805b5d98SGreg Kurz err = -EEXIST; 2769805b5d98SGreg Kurz goto out_nofid; 2770805b5d98SGreg Kurz } 2771805b5d98SGreg Kurz 277260ce86c7SWei Liu fidp = get_fid(pdu, fid); 277360ce86c7SWei Liu if (fidp == NULL) { 277460ce86c7SWei Liu err = -EINVAL; 277560ce86c7SWei Liu goto out_nofid; 277660ce86c7SWei Liu } 2777d63fb193SLi Qiang if (fidp->fid_type != P9_FID_NONE) { 2778d63fb193SLi Qiang err = -EINVAL; 2779d63fb193SLi Qiang goto out; 2780d63fb193SLi Qiang } 278160ce86c7SWei Liu if (perm & P9_STAT_MODE_DIR) { 278260ce86c7SWei Liu err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777, 278360ce86c7SWei Liu fidp->uid, -1, &stbuf); 278460ce86c7SWei Liu if (err < 0) { 278560ce86c7SWei Liu goto out; 278660ce86c7SWei Liu } 278760ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); 278860ce86c7SWei Liu if (err < 0) { 278960ce86c7SWei Liu goto out; 279060ce86c7SWei Liu } 27915b3c77aaSGreg Kurz v9fs_path_write_lock(s); 279260ce86c7SWei Liu v9fs_path_copy(&fidp->path, &path); 27935b3c77aaSGreg Kurz v9fs_path_unlock(s); 279460ce86c7SWei Liu err = v9fs_co_opendir(pdu, fidp); 279560ce86c7SWei Liu if (err < 0) { 279660ce86c7SWei Liu goto out; 279760ce86c7SWei Liu } 279860ce86c7SWei Liu fidp->fid_type = P9_FID_DIR; 279960ce86c7SWei Liu } else if (perm & P9_STAT_MODE_SYMLINK) { 280060ce86c7SWei Liu err = v9fs_co_symlink(pdu, fidp, &name, 280160ce86c7SWei Liu extension.data, -1 , &stbuf); 280260ce86c7SWei Liu if (err < 0) { 280360ce86c7SWei Liu goto out; 280460ce86c7SWei Liu } 280560ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); 280660ce86c7SWei Liu if (err < 0) { 280760ce86c7SWei Liu goto out; 280860ce86c7SWei Liu } 28095b3c77aaSGreg Kurz v9fs_path_write_lock(s); 281060ce86c7SWei Liu v9fs_path_copy(&fidp->path, &path); 28115b3c77aaSGreg Kurz v9fs_path_unlock(s); 281260ce86c7SWei Liu } else if (perm & P9_STAT_MODE_LINK) { 281360ce86c7SWei Liu int32_t ofid = atoi(extension.data); 281460ce86c7SWei Liu V9fsFidState *ofidp = get_fid(pdu, ofid); 281560ce86c7SWei Liu if (ofidp == NULL) { 281660ce86c7SWei Liu err = -EINVAL; 281760ce86c7SWei Liu goto out; 281860ce86c7SWei Liu } 281960ce86c7SWei Liu err = v9fs_co_link(pdu, ofidp, fidp, &name); 282060ce86c7SWei Liu put_fid(pdu, ofidp); 282160ce86c7SWei Liu if (err < 0) { 282260ce86c7SWei Liu goto out; 282360ce86c7SWei Liu } 282460ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); 282560ce86c7SWei Liu if (err < 0) { 282660ce86c7SWei Liu fidp->fid_type = P9_FID_NONE; 282760ce86c7SWei Liu goto out; 282860ce86c7SWei Liu } 28295b3c77aaSGreg Kurz v9fs_path_write_lock(s); 283060ce86c7SWei Liu v9fs_path_copy(&fidp->path, &path); 28315b3c77aaSGreg Kurz v9fs_path_unlock(s); 283260ce86c7SWei Liu err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); 283360ce86c7SWei Liu if (err < 0) { 283460ce86c7SWei Liu fidp->fid_type = P9_FID_NONE; 283560ce86c7SWei Liu goto out; 283660ce86c7SWei Liu } 283760ce86c7SWei Liu } else if (perm & P9_STAT_MODE_DEVICE) { 283860ce86c7SWei Liu char ctype; 283960ce86c7SWei Liu uint32_t major, minor; 284060ce86c7SWei Liu mode_t nmode = 0; 284160ce86c7SWei Liu 284260ce86c7SWei Liu if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) { 284360ce86c7SWei Liu err = -errno; 284460ce86c7SWei Liu goto out; 284560ce86c7SWei Liu } 284660ce86c7SWei Liu 284760ce86c7SWei Liu switch (ctype) { 284860ce86c7SWei Liu case 'c': 284960ce86c7SWei Liu nmode = S_IFCHR; 285060ce86c7SWei Liu break; 285160ce86c7SWei Liu case 'b': 285260ce86c7SWei Liu nmode = S_IFBLK; 285360ce86c7SWei Liu break; 285460ce86c7SWei Liu default: 285560ce86c7SWei Liu err = -EIO; 285660ce86c7SWei Liu goto out; 285760ce86c7SWei Liu } 285860ce86c7SWei Liu 285960ce86c7SWei Liu nmode |= perm & 0777; 286060ce86c7SWei Liu err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1, 286160ce86c7SWei Liu makedev(major, minor), nmode, &stbuf); 286260ce86c7SWei Liu if (err < 0) { 286360ce86c7SWei Liu goto out; 286460ce86c7SWei Liu } 286560ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); 286660ce86c7SWei Liu if (err < 0) { 286760ce86c7SWei Liu goto out; 286860ce86c7SWei Liu } 28695b3c77aaSGreg Kurz v9fs_path_write_lock(s); 287060ce86c7SWei Liu v9fs_path_copy(&fidp->path, &path); 28715b3c77aaSGreg Kurz v9fs_path_unlock(s); 287260ce86c7SWei Liu } else if (perm & P9_STAT_MODE_NAMED_PIPE) { 287360ce86c7SWei Liu err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1, 287460ce86c7SWei Liu 0, S_IFIFO | (perm & 0777), &stbuf); 287560ce86c7SWei Liu if (err < 0) { 287660ce86c7SWei Liu goto out; 287760ce86c7SWei Liu } 287860ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); 287960ce86c7SWei Liu if (err < 0) { 288060ce86c7SWei Liu goto out; 288160ce86c7SWei Liu } 28825b3c77aaSGreg Kurz v9fs_path_write_lock(s); 288360ce86c7SWei Liu v9fs_path_copy(&fidp->path, &path); 28845b3c77aaSGreg Kurz v9fs_path_unlock(s); 288560ce86c7SWei Liu } else if (perm & P9_STAT_MODE_SOCKET) { 288660ce86c7SWei Liu err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1, 288760ce86c7SWei Liu 0, S_IFSOCK | (perm & 0777), &stbuf); 288860ce86c7SWei Liu if (err < 0) { 288960ce86c7SWei Liu goto out; 289060ce86c7SWei Liu } 289160ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); 289260ce86c7SWei Liu if (err < 0) { 289360ce86c7SWei Liu goto out; 289460ce86c7SWei Liu } 28955b3c77aaSGreg Kurz v9fs_path_write_lock(s); 289660ce86c7SWei Liu v9fs_path_copy(&fidp->path, &path); 28975b3c77aaSGreg Kurz v9fs_path_unlock(s); 289860ce86c7SWei Liu } else { 289960ce86c7SWei Liu err = v9fs_co_open2(pdu, fidp, &name, -1, 290060ce86c7SWei Liu omode_to_uflags(mode) | O_CREAT, perm, &stbuf); 290160ce86c7SWei Liu if (err < 0) { 290260ce86c7SWei Liu goto out; 290360ce86c7SWei Liu } 290460ce86c7SWei Liu fidp->fid_type = P9_FID_FILE; 290560ce86c7SWei Liu fidp->open_flags = omode_to_uflags(mode); 290660ce86c7SWei Liu if (fidp->open_flags & O_EXCL) { 290760ce86c7SWei Liu /* 290860ce86c7SWei Liu * We let the host file system do O_EXCL check 290960ce86c7SWei Liu * We should not reclaim such fd 291060ce86c7SWei Liu */ 291160ce86c7SWei Liu fidp->flags |= FID_NON_RECLAIMABLE; 291260ce86c7SWei Liu } 291360ce86c7SWei Liu } 291460ce86c7SWei Liu iounit = get_iounit(pdu, &fidp->path); 29153b5ee9e8SAntonios Motakis err = stat_to_qid(pdu, &stbuf, &qid); 29163b5ee9e8SAntonios Motakis if (err < 0) { 29173b5ee9e8SAntonios Motakis goto out; 29183b5ee9e8SAntonios Motakis } 291960ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Qd", &qid, iounit); 292060ce86c7SWei Liu if (err < 0) { 292160ce86c7SWei Liu goto out; 292260ce86c7SWei Liu } 292360ce86c7SWei Liu err += offset; 292460ce86c7SWei Liu trace_v9fs_create_return(pdu->tag, pdu->id, 292560ce86c7SWei Liu qid.type, qid.version, qid.path, iounit); 292660ce86c7SWei Liu out: 292760ce86c7SWei Liu put_fid(pdu, fidp); 292860ce86c7SWei Liu out_nofid: 292960ce86c7SWei Liu pdu_complete(pdu, err); 293060ce86c7SWei Liu v9fs_string_free(&name); 293160ce86c7SWei Liu v9fs_string_free(&extension); 293260ce86c7SWei Liu v9fs_path_free(&path); 293360ce86c7SWei Liu } 293460ce86c7SWei Liu 29358440e22eSGreg Kurz static void coroutine_fn v9fs_symlink(void *opaque) 293660ce86c7SWei Liu { 293760ce86c7SWei Liu V9fsPDU *pdu = opaque; 293860ce86c7SWei Liu V9fsString name; 293960ce86c7SWei Liu V9fsString symname; 294060ce86c7SWei Liu V9fsFidState *dfidp; 294160ce86c7SWei Liu V9fsQID qid; 294260ce86c7SWei Liu struct stat stbuf; 294360ce86c7SWei Liu int32_t dfid; 294460ce86c7SWei Liu int err = 0; 294560ce86c7SWei Liu gid_t gid; 294660ce86c7SWei Liu size_t offset = 7; 294760ce86c7SWei Liu 294860ce86c7SWei Liu v9fs_string_init(&name); 294960ce86c7SWei Liu v9fs_string_init(&symname); 295060ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid); 295160ce86c7SWei Liu if (err < 0) { 295260ce86c7SWei Liu goto out_nofid; 295360ce86c7SWei Liu } 295460ce86c7SWei Liu trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid); 295560ce86c7SWei Liu 2956fff39a7aSGreg Kurz if (name_is_illegal(name.data)) { 2957fff39a7aSGreg Kurz err = -ENOENT; 2958fff39a7aSGreg Kurz goto out_nofid; 2959fff39a7aSGreg Kurz } 2960fff39a7aSGreg Kurz 2961805b5d98SGreg Kurz if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 2962805b5d98SGreg Kurz err = -EEXIST; 2963805b5d98SGreg Kurz goto out_nofid; 2964805b5d98SGreg Kurz } 2965805b5d98SGreg Kurz 296660ce86c7SWei Liu dfidp = get_fid(pdu, dfid); 296760ce86c7SWei Liu if (dfidp == NULL) { 296860ce86c7SWei Liu err = -EINVAL; 296960ce86c7SWei Liu goto out_nofid; 297060ce86c7SWei Liu } 297160ce86c7SWei Liu err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf); 297260ce86c7SWei Liu if (err < 0) { 297360ce86c7SWei Liu goto out; 297460ce86c7SWei Liu } 29753b5ee9e8SAntonios Motakis err = stat_to_qid(pdu, &stbuf, &qid); 29763b5ee9e8SAntonios Motakis if (err < 0) { 29773b5ee9e8SAntonios Motakis goto out; 29783b5ee9e8SAntonios Motakis } 297960ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Q", &qid); 298060ce86c7SWei Liu if (err < 0) { 298160ce86c7SWei Liu goto out; 298260ce86c7SWei Liu } 298360ce86c7SWei Liu err += offset; 298460ce86c7SWei Liu trace_v9fs_symlink_return(pdu->tag, pdu->id, 298560ce86c7SWei Liu qid.type, qid.version, qid.path); 298660ce86c7SWei Liu out: 298760ce86c7SWei Liu put_fid(pdu, dfidp); 298860ce86c7SWei Liu out_nofid: 298960ce86c7SWei Liu pdu_complete(pdu, err); 299060ce86c7SWei Liu v9fs_string_free(&name); 299160ce86c7SWei Liu v9fs_string_free(&symname); 299260ce86c7SWei Liu } 299360ce86c7SWei Liu 2994a1bf8b74SGreg Kurz static void coroutine_fn v9fs_flush(void *opaque) 299560ce86c7SWei Liu { 299660ce86c7SWei Liu ssize_t err; 299760ce86c7SWei Liu int16_t tag; 299860ce86c7SWei Liu size_t offset = 7; 2999d5f2af7bSGreg Kurz V9fsPDU *cancel_pdu = NULL; 300060ce86c7SWei Liu V9fsPDU *pdu = opaque; 300160ce86c7SWei Liu V9fsState *s = pdu->s; 300260ce86c7SWei Liu 300360ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "w", &tag); 300460ce86c7SWei Liu if (err < 0) { 300560ce86c7SWei Liu pdu_complete(pdu, err); 300660ce86c7SWei Liu return; 300760ce86c7SWei Liu } 300860ce86c7SWei Liu trace_v9fs_flush(pdu->tag, pdu->id, tag); 300960ce86c7SWei Liu 3010d5f2af7bSGreg Kurz if (pdu->tag == tag) { 30113dc6f869SAlistair Francis warn_report("the guest sent a self-referencing 9P flush request"); 3012d5f2af7bSGreg Kurz } else { 301360ce86c7SWei Liu QLIST_FOREACH(cancel_pdu, &s->active_list, next) { 301460ce86c7SWei Liu if (cancel_pdu->tag == tag) { 301560ce86c7SWei Liu break; 301660ce86c7SWei Liu } 301760ce86c7SWei Liu } 3018d5f2af7bSGreg Kurz } 301960ce86c7SWei Liu if (cancel_pdu) { 302060ce86c7SWei Liu cancel_pdu->cancelled = 1; 302160ce86c7SWei Liu /* 302260ce86c7SWei Liu * Wait for pdu to complete. 302360ce86c7SWei Liu */ 30241ace7ceaSPaolo Bonzini qemu_co_queue_wait(&cancel_pdu->complete, NULL); 302518adde86SGreg Kurz if (!qemu_co_queue_next(&cancel_pdu->complete)) { 302660ce86c7SWei Liu cancel_pdu->cancelled = 0; 302760ce86c7SWei Liu pdu_free(cancel_pdu); 302860ce86c7SWei Liu } 302918adde86SGreg Kurz } 303060ce86c7SWei Liu pdu_complete(pdu, 7); 303160ce86c7SWei Liu } 303260ce86c7SWei Liu 30338440e22eSGreg Kurz static void coroutine_fn v9fs_link(void *opaque) 303460ce86c7SWei Liu { 303560ce86c7SWei Liu V9fsPDU *pdu = opaque; 303660ce86c7SWei Liu int32_t dfid, oldfid; 303760ce86c7SWei Liu V9fsFidState *dfidp, *oldfidp; 303860ce86c7SWei Liu V9fsString name; 303960ce86c7SWei Liu size_t offset = 7; 304060ce86c7SWei Liu int err = 0; 304160ce86c7SWei Liu 304260ce86c7SWei Liu v9fs_string_init(&name); 304360ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name); 304460ce86c7SWei Liu if (err < 0) { 304560ce86c7SWei Liu goto out_nofid; 304660ce86c7SWei Liu } 304760ce86c7SWei Liu trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data); 304860ce86c7SWei Liu 3049fff39a7aSGreg Kurz if (name_is_illegal(name.data)) { 3050fff39a7aSGreg Kurz err = -ENOENT; 3051fff39a7aSGreg Kurz goto out_nofid; 3052fff39a7aSGreg Kurz } 3053fff39a7aSGreg Kurz 3054805b5d98SGreg Kurz if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 3055805b5d98SGreg Kurz err = -EEXIST; 3056805b5d98SGreg Kurz goto out_nofid; 3057805b5d98SGreg Kurz } 3058805b5d98SGreg Kurz 305960ce86c7SWei Liu dfidp = get_fid(pdu, dfid); 306060ce86c7SWei Liu if (dfidp == NULL) { 306160ce86c7SWei Liu err = -ENOENT; 306260ce86c7SWei Liu goto out_nofid; 306360ce86c7SWei Liu } 306460ce86c7SWei Liu 306560ce86c7SWei Liu oldfidp = get_fid(pdu, oldfid); 306660ce86c7SWei Liu if (oldfidp == NULL) { 306760ce86c7SWei Liu err = -ENOENT; 306860ce86c7SWei Liu goto out; 306960ce86c7SWei Liu } 307060ce86c7SWei Liu err = v9fs_co_link(pdu, oldfidp, dfidp, &name); 307160ce86c7SWei Liu if (!err) { 307260ce86c7SWei Liu err = offset; 307360ce86c7SWei Liu } 30744c158678SLi Qiang put_fid(pdu, oldfidp); 307560ce86c7SWei Liu out: 307660ce86c7SWei Liu put_fid(pdu, dfidp); 307760ce86c7SWei Liu out_nofid: 307860ce86c7SWei Liu v9fs_string_free(&name); 307960ce86c7SWei Liu pdu_complete(pdu, err); 308060ce86c7SWei Liu } 308160ce86c7SWei Liu 308260ce86c7SWei Liu /* Only works with path name based fid */ 30838440e22eSGreg Kurz static void coroutine_fn v9fs_remove(void *opaque) 308460ce86c7SWei Liu { 308560ce86c7SWei Liu int32_t fid; 308660ce86c7SWei Liu int err = 0; 308760ce86c7SWei Liu size_t offset = 7; 308860ce86c7SWei Liu V9fsFidState *fidp; 308960ce86c7SWei Liu V9fsPDU *pdu = opaque; 309060ce86c7SWei Liu 309160ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "d", &fid); 309260ce86c7SWei Liu if (err < 0) { 309360ce86c7SWei Liu goto out_nofid; 309460ce86c7SWei Liu } 309560ce86c7SWei Liu trace_v9fs_remove(pdu->tag, pdu->id, fid); 309660ce86c7SWei Liu 309760ce86c7SWei Liu fidp = get_fid(pdu, fid); 309860ce86c7SWei Liu if (fidp == NULL) { 309960ce86c7SWei Liu err = -EINVAL; 310060ce86c7SWei Liu goto out_nofid; 310160ce86c7SWei Liu } 310260ce86c7SWei Liu /* if fs driver is not path based, return EOPNOTSUPP */ 310360ce86c7SWei Liu if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) { 310460ce86c7SWei Liu err = -EOPNOTSUPP; 310560ce86c7SWei Liu goto out_err; 310660ce86c7SWei Liu } 310760ce86c7SWei Liu /* 310860ce86c7SWei Liu * IF the file is unlinked, we cannot reopen 310960ce86c7SWei Liu * the file later. So don't reclaim fd 311060ce86c7SWei Liu */ 311160ce86c7SWei Liu err = v9fs_mark_fids_unreclaim(pdu, &fidp->path); 311260ce86c7SWei Liu if (err < 0) { 311360ce86c7SWei Liu goto out_err; 311460ce86c7SWei Liu } 311560ce86c7SWei Liu err = v9fs_co_remove(pdu, &fidp->path); 311660ce86c7SWei Liu if (!err) { 311760ce86c7SWei Liu err = offset; 311860ce86c7SWei Liu } 311960ce86c7SWei Liu out_err: 312060ce86c7SWei Liu /* For TREMOVE we need to clunk the fid even on failed remove */ 312160ce86c7SWei Liu clunk_fid(pdu->s, fidp->fid); 312260ce86c7SWei Liu put_fid(pdu, fidp); 312360ce86c7SWei Liu out_nofid: 312460ce86c7SWei Liu pdu_complete(pdu, err); 312560ce86c7SWei Liu } 312660ce86c7SWei Liu 31278440e22eSGreg Kurz static void coroutine_fn v9fs_unlinkat(void *opaque) 312860ce86c7SWei Liu { 312960ce86c7SWei Liu int err = 0; 313060ce86c7SWei Liu V9fsString name; 313167e87345SKeno Fischer int32_t dfid, flags, rflags = 0; 313260ce86c7SWei Liu size_t offset = 7; 313360ce86c7SWei Liu V9fsPath path; 313460ce86c7SWei Liu V9fsFidState *dfidp; 313560ce86c7SWei Liu V9fsPDU *pdu = opaque; 313660ce86c7SWei Liu 313760ce86c7SWei Liu v9fs_string_init(&name); 313860ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags); 313960ce86c7SWei Liu if (err < 0) { 314060ce86c7SWei Liu goto out_nofid; 314160ce86c7SWei Liu } 3142fff39a7aSGreg Kurz 3143fff39a7aSGreg Kurz if (name_is_illegal(name.data)) { 3144fff39a7aSGreg Kurz err = -ENOENT; 3145fff39a7aSGreg Kurz goto out_nofid; 3146fff39a7aSGreg Kurz } 3147fff39a7aSGreg Kurz 3148805b5d98SGreg Kurz if (!strcmp(".", name.data)) { 3149805b5d98SGreg Kurz err = -EINVAL; 3150805b5d98SGreg Kurz goto out_nofid; 3151805b5d98SGreg Kurz } 3152805b5d98SGreg Kurz 3153805b5d98SGreg Kurz if (!strcmp("..", name.data)) { 3154805b5d98SGreg Kurz err = -ENOTEMPTY; 3155805b5d98SGreg Kurz goto out_nofid; 3156805b5d98SGreg Kurz } 3157805b5d98SGreg Kurz 315867e87345SKeno Fischer if (flags & ~P9_DOTL_AT_REMOVEDIR) { 315967e87345SKeno Fischer err = -EINVAL; 316067e87345SKeno Fischer goto out_nofid; 316167e87345SKeno Fischer } 316267e87345SKeno Fischer 316367e87345SKeno Fischer if (flags & P9_DOTL_AT_REMOVEDIR) { 316467e87345SKeno Fischer rflags |= AT_REMOVEDIR; 316567e87345SKeno Fischer } 316667e87345SKeno Fischer 316760ce86c7SWei Liu dfidp = get_fid(pdu, dfid); 316860ce86c7SWei Liu if (dfidp == NULL) { 316960ce86c7SWei Liu err = -EINVAL; 317060ce86c7SWei Liu goto out_nofid; 317160ce86c7SWei Liu } 317260ce86c7SWei Liu /* 317360ce86c7SWei Liu * IF the file is unlinked, we cannot reopen 317460ce86c7SWei Liu * the file later. So don't reclaim fd 317560ce86c7SWei Liu */ 317660ce86c7SWei Liu v9fs_path_init(&path); 317760ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path); 317860ce86c7SWei Liu if (err < 0) { 317960ce86c7SWei Liu goto out_err; 318060ce86c7SWei Liu } 318160ce86c7SWei Liu err = v9fs_mark_fids_unreclaim(pdu, &path); 318260ce86c7SWei Liu if (err < 0) { 318360ce86c7SWei Liu goto out_err; 318460ce86c7SWei Liu } 318567e87345SKeno Fischer err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, rflags); 318660ce86c7SWei Liu if (!err) { 318760ce86c7SWei Liu err = offset; 318860ce86c7SWei Liu } 318960ce86c7SWei Liu out_err: 319060ce86c7SWei Liu put_fid(pdu, dfidp); 319160ce86c7SWei Liu v9fs_path_free(&path); 319260ce86c7SWei Liu out_nofid: 319360ce86c7SWei Liu pdu_complete(pdu, err); 319460ce86c7SWei Liu v9fs_string_free(&name); 319560ce86c7SWei Liu } 319660ce86c7SWei Liu 319760ce86c7SWei Liu 319860ce86c7SWei Liu /* Only works with path name based fid */ 31998440e22eSGreg Kurz static int coroutine_fn v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp, 32008440e22eSGreg Kurz int32_t newdirfid, 32018440e22eSGreg Kurz V9fsString *name) 320260ce86c7SWei Liu { 320360ce86c7SWei Liu int err = 0; 320460ce86c7SWei Liu V9fsPath new_path; 320560ce86c7SWei Liu V9fsFidState *tfidp; 320660ce86c7SWei Liu V9fsState *s = pdu->s; 320760ce86c7SWei Liu V9fsFidState *dirfidp = NULL; 320860ce86c7SWei Liu 320960ce86c7SWei Liu v9fs_path_init(&new_path); 321060ce86c7SWei Liu if (newdirfid != -1) { 321160ce86c7SWei Liu dirfidp = get_fid(pdu, newdirfid); 321260ce86c7SWei Liu if (dirfidp == NULL) { 3213b858e80aSDaniel Henrique Barboza return -ENOENT; 321460ce86c7SWei Liu } 321549dd946bSGreg Kurz if (fidp->fid_type != P9_FID_NONE) { 321649dd946bSGreg Kurz err = -EINVAL; 321749dd946bSGreg Kurz goto out; 321849dd946bSGreg Kurz } 32194fa62005SGreg Kurz err = v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path); 32204fa62005SGreg Kurz if (err < 0) { 32214fa62005SGreg Kurz goto out; 32224fa62005SGreg Kurz } 322360ce86c7SWei Liu } else { 32244d8bc733SJan Dakinevich char *dir_name = g_path_get_dirname(fidp->path.data); 32254d8bc733SJan Dakinevich V9fsPath dir_path; 32264d8bc733SJan Dakinevich 32274d8bc733SJan Dakinevich v9fs_path_init(&dir_path); 32284d8bc733SJan Dakinevich v9fs_path_sprintf(&dir_path, "%s", dir_name); 32294d8bc733SJan Dakinevich g_free(dir_name); 32304d8bc733SJan Dakinevich 32314d8bc733SJan Dakinevich err = v9fs_co_name_to_path(pdu, &dir_path, name->data, &new_path); 32324d8bc733SJan Dakinevich v9fs_path_free(&dir_path); 32334fa62005SGreg Kurz if (err < 0) { 32344fa62005SGreg Kurz goto out; 32354fa62005SGreg Kurz } 323660ce86c7SWei Liu } 323760ce86c7SWei Liu err = v9fs_co_rename(pdu, &fidp->path, &new_path); 323860ce86c7SWei Liu if (err < 0) { 323960ce86c7SWei Liu goto out; 324060ce86c7SWei Liu } 324160ce86c7SWei Liu /* 324260ce86c7SWei Liu * Fixup fid's pointing to the old name to 324360ce86c7SWei Liu * start pointing to the new name 324460ce86c7SWei Liu */ 3245feabd6cfSGreg Kurz QSIMPLEQ_FOREACH(tfidp, &s->fid_list, next) { 324660ce86c7SWei Liu if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) { 324760ce86c7SWei Liu /* replace the name */ 324860ce86c7SWei Liu v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data)); 324960ce86c7SWei Liu } 325060ce86c7SWei Liu } 325160ce86c7SWei Liu out: 325260ce86c7SWei Liu if (dirfidp) { 325360ce86c7SWei Liu put_fid(pdu, dirfidp); 325460ce86c7SWei Liu } 325560ce86c7SWei Liu v9fs_path_free(&new_path); 325660ce86c7SWei Liu return err; 325760ce86c7SWei Liu } 325860ce86c7SWei Liu 325960ce86c7SWei Liu /* Only works with path name based fid */ 32608440e22eSGreg Kurz static void coroutine_fn v9fs_rename(void *opaque) 326160ce86c7SWei Liu { 326260ce86c7SWei Liu int32_t fid; 326360ce86c7SWei Liu ssize_t err = 0; 326460ce86c7SWei Liu size_t offset = 7; 326560ce86c7SWei Liu V9fsString name; 326660ce86c7SWei Liu int32_t newdirfid; 326760ce86c7SWei Liu V9fsFidState *fidp; 326860ce86c7SWei Liu V9fsPDU *pdu = opaque; 326960ce86c7SWei Liu V9fsState *s = pdu->s; 327060ce86c7SWei Liu 327160ce86c7SWei Liu v9fs_string_init(&name); 327260ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name); 327360ce86c7SWei Liu if (err < 0) { 327460ce86c7SWei Liu goto out_nofid; 327560ce86c7SWei Liu } 3276fff39a7aSGreg Kurz 3277fff39a7aSGreg Kurz if (name_is_illegal(name.data)) { 3278fff39a7aSGreg Kurz err = -ENOENT; 3279fff39a7aSGreg Kurz goto out_nofid; 3280fff39a7aSGreg Kurz } 3281fff39a7aSGreg Kurz 3282805b5d98SGreg Kurz if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 3283805b5d98SGreg Kurz err = -EISDIR; 3284805b5d98SGreg Kurz goto out_nofid; 3285805b5d98SGreg Kurz } 3286805b5d98SGreg Kurz 328760ce86c7SWei Liu fidp = get_fid(pdu, fid); 328860ce86c7SWei Liu if (fidp == NULL) { 328960ce86c7SWei Liu err = -ENOENT; 329060ce86c7SWei Liu goto out_nofid; 329160ce86c7SWei Liu } 329249dd946bSGreg Kurz if (fidp->fid_type != P9_FID_NONE) { 329349dd946bSGreg Kurz err = -EINVAL; 329449dd946bSGreg Kurz goto out; 329549dd946bSGreg Kurz } 329660ce86c7SWei Liu /* if fs driver is not path based, return EOPNOTSUPP */ 329760ce86c7SWei Liu if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) { 329860ce86c7SWei Liu err = -EOPNOTSUPP; 329960ce86c7SWei Liu goto out; 330060ce86c7SWei Liu } 330160ce86c7SWei Liu v9fs_path_write_lock(s); 330260ce86c7SWei Liu err = v9fs_complete_rename(pdu, fidp, newdirfid, &name); 330360ce86c7SWei Liu v9fs_path_unlock(s); 330460ce86c7SWei Liu if (!err) { 330560ce86c7SWei Liu err = offset; 330660ce86c7SWei Liu } 330760ce86c7SWei Liu out: 330860ce86c7SWei Liu put_fid(pdu, fidp); 330960ce86c7SWei Liu out_nofid: 331060ce86c7SWei Liu pdu_complete(pdu, err); 331160ce86c7SWei Liu v9fs_string_free(&name); 331260ce86c7SWei Liu } 331360ce86c7SWei Liu 33144fa62005SGreg Kurz static int coroutine_fn v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir, 33158440e22eSGreg Kurz V9fsString *old_name, 33168440e22eSGreg Kurz V9fsPath *newdir, 331760ce86c7SWei Liu V9fsString *new_name) 331860ce86c7SWei Liu { 331960ce86c7SWei Liu V9fsFidState *tfidp; 332060ce86c7SWei Liu V9fsPath oldpath, newpath; 332160ce86c7SWei Liu V9fsState *s = pdu->s; 33224fa62005SGreg Kurz int err; 332360ce86c7SWei Liu 332460ce86c7SWei Liu v9fs_path_init(&oldpath); 332560ce86c7SWei Liu v9fs_path_init(&newpath); 33264fa62005SGreg Kurz err = v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath); 33274fa62005SGreg Kurz if (err < 0) { 33284fa62005SGreg Kurz goto out; 33294fa62005SGreg Kurz } 33304fa62005SGreg Kurz err = v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath); 33314fa62005SGreg Kurz if (err < 0) { 33324fa62005SGreg Kurz goto out; 33334fa62005SGreg Kurz } 333460ce86c7SWei Liu 333560ce86c7SWei Liu /* 333660ce86c7SWei Liu * Fixup fid's pointing to the old name to 333760ce86c7SWei Liu * start pointing to the new name 333860ce86c7SWei Liu */ 3339feabd6cfSGreg Kurz QSIMPLEQ_FOREACH(tfidp, &s->fid_list, next) { 334060ce86c7SWei Liu if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) { 334160ce86c7SWei Liu /* replace the name */ 334260ce86c7SWei Liu v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data)); 334360ce86c7SWei Liu } 334460ce86c7SWei Liu } 33454fa62005SGreg Kurz out: 334660ce86c7SWei Liu v9fs_path_free(&oldpath); 334760ce86c7SWei Liu v9fs_path_free(&newpath); 33484fa62005SGreg Kurz return err; 334960ce86c7SWei Liu } 335060ce86c7SWei Liu 33518440e22eSGreg Kurz static int coroutine_fn v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid, 33528440e22eSGreg Kurz V9fsString *old_name, 33538440e22eSGreg Kurz int32_t newdirfid, 335460ce86c7SWei Liu V9fsString *new_name) 335560ce86c7SWei Liu { 335660ce86c7SWei Liu int err = 0; 335760ce86c7SWei Liu V9fsState *s = pdu->s; 335860ce86c7SWei Liu V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL; 335960ce86c7SWei Liu 336060ce86c7SWei Liu olddirfidp = get_fid(pdu, olddirfid); 336160ce86c7SWei Liu if (olddirfidp == NULL) { 336260ce86c7SWei Liu err = -ENOENT; 336360ce86c7SWei Liu goto out; 336460ce86c7SWei Liu } 336560ce86c7SWei Liu if (newdirfid != -1) { 336660ce86c7SWei Liu newdirfidp = get_fid(pdu, newdirfid); 336760ce86c7SWei Liu if (newdirfidp == NULL) { 336860ce86c7SWei Liu err = -ENOENT; 336960ce86c7SWei Liu goto out; 337060ce86c7SWei Liu } 337160ce86c7SWei Liu } else { 337260ce86c7SWei Liu newdirfidp = get_fid(pdu, olddirfid); 337360ce86c7SWei Liu } 337460ce86c7SWei Liu 337560ce86c7SWei Liu err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name, 337660ce86c7SWei Liu &newdirfidp->path, new_name); 337760ce86c7SWei Liu if (err < 0) { 337860ce86c7SWei Liu goto out; 337960ce86c7SWei Liu } 338060ce86c7SWei Liu if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) { 338160ce86c7SWei Liu /* Only for path based fid we need to do the below fixup */ 33824fa62005SGreg Kurz err = v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name, 338360ce86c7SWei Liu &newdirfidp->path, new_name); 338460ce86c7SWei Liu } 338560ce86c7SWei Liu out: 338660ce86c7SWei Liu if (olddirfidp) { 338760ce86c7SWei Liu put_fid(pdu, olddirfidp); 338860ce86c7SWei Liu } 338960ce86c7SWei Liu if (newdirfidp) { 339060ce86c7SWei Liu put_fid(pdu, newdirfidp); 339160ce86c7SWei Liu } 339260ce86c7SWei Liu return err; 339360ce86c7SWei Liu } 339460ce86c7SWei Liu 33958440e22eSGreg Kurz static void coroutine_fn v9fs_renameat(void *opaque) 339660ce86c7SWei Liu { 339760ce86c7SWei Liu ssize_t err = 0; 339860ce86c7SWei Liu size_t offset = 7; 339960ce86c7SWei Liu V9fsPDU *pdu = opaque; 340060ce86c7SWei Liu V9fsState *s = pdu->s; 340160ce86c7SWei Liu int32_t olddirfid, newdirfid; 340260ce86c7SWei Liu V9fsString old_name, new_name; 340360ce86c7SWei Liu 340460ce86c7SWei Liu v9fs_string_init(&old_name); 340560ce86c7SWei Liu v9fs_string_init(&new_name); 340660ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid, 340760ce86c7SWei Liu &old_name, &newdirfid, &new_name); 340860ce86c7SWei Liu if (err < 0) { 340960ce86c7SWei Liu goto out_err; 341060ce86c7SWei Liu } 341160ce86c7SWei Liu 3412fff39a7aSGreg Kurz if (name_is_illegal(old_name.data) || name_is_illegal(new_name.data)) { 3413fff39a7aSGreg Kurz err = -ENOENT; 3414fff39a7aSGreg Kurz goto out_err; 3415fff39a7aSGreg Kurz } 3416fff39a7aSGreg Kurz 3417805b5d98SGreg Kurz if (!strcmp(".", old_name.data) || !strcmp("..", old_name.data) || 3418805b5d98SGreg Kurz !strcmp(".", new_name.data) || !strcmp("..", new_name.data)) { 3419805b5d98SGreg Kurz err = -EISDIR; 3420805b5d98SGreg Kurz goto out_err; 3421805b5d98SGreg Kurz } 3422805b5d98SGreg Kurz 342360ce86c7SWei Liu v9fs_path_write_lock(s); 342460ce86c7SWei Liu err = v9fs_complete_renameat(pdu, olddirfid, 342560ce86c7SWei Liu &old_name, newdirfid, &new_name); 342660ce86c7SWei Liu v9fs_path_unlock(s); 342760ce86c7SWei Liu if (!err) { 342860ce86c7SWei Liu err = offset; 342960ce86c7SWei Liu } 343060ce86c7SWei Liu 343160ce86c7SWei Liu out_err: 343260ce86c7SWei Liu pdu_complete(pdu, err); 343360ce86c7SWei Liu v9fs_string_free(&old_name); 343460ce86c7SWei Liu v9fs_string_free(&new_name); 343560ce86c7SWei Liu } 343660ce86c7SWei Liu 34378440e22eSGreg Kurz static void coroutine_fn v9fs_wstat(void *opaque) 343860ce86c7SWei Liu { 343960ce86c7SWei Liu int32_t fid; 344060ce86c7SWei Liu int err = 0; 344160ce86c7SWei Liu int16_t unused; 344260ce86c7SWei Liu V9fsStat v9stat; 344360ce86c7SWei Liu size_t offset = 7; 344460ce86c7SWei Liu struct stat stbuf; 344560ce86c7SWei Liu V9fsFidState *fidp; 344660ce86c7SWei Liu V9fsPDU *pdu = opaque; 34471d203986SGreg Kurz V9fsState *s = pdu->s; 344860ce86c7SWei Liu 344960ce86c7SWei Liu v9fs_stat_init(&v9stat); 345060ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat); 345160ce86c7SWei Liu if (err < 0) { 345260ce86c7SWei Liu goto out_nofid; 345360ce86c7SWei Liu } 345460ce86c7SWei Liu trace_v9fs_wstat(pdu->tag, pdu->id, fid, 345560ce86c7SWei Liu v9stat.mode, v9stat.atime, v9stat.mtime); 345660ce86c7SWei Liu 345760ce86c7SWei Liu fidp = get_fid(pdu, fid); 345860ce86c7SWei Liu if (fidp == NULL) { 345960ce86c7SWei Liu err = -EINVAL; 346060ce86c7SWei Liu goto out_nofid; 346160ce86c7SWei Liu } 346260ce86c7SWei Liu /* do we need to sync the file? */ 346360ce86c7SWei Liu if (donttouch_stat(&v9stat)) { 346460ce86c7SWei Liu err = v9fs_co_fsync(pdu, fidp, 0); 346560ce86c7SWei Liu goto out; 346660ce86c7SWei Liu } 346760ce86c7SWei Liu if (v9stat.mode != -1) { 346860ce86c7SWei Liu uint32_t v9_mode; 346960ce86c7SWei Liu err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); 347060ce86c7SWei Liu if (err < 0) { 347160ce86c7SWei Liu goto out; 347260ce86c7SWei Liu } 347360ce86c7SWei Liu v9_mode = stat_to_v9mode(&stbuf); 347460ce86c7SWei Liu if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) != 347560ce86c7SWei Liu (v9_mode & P9_STAT_MODE_TYPE_BITS)) { 347660ce86c7SWei Liu /* Attempting to change the type */ 347760ce86c7SWei Liu err = -EIO; 347860ce86c7SWei Liu goto out; 347960ce86c7SWei Liu } 348060ce86c7SWei Liu err = v9fs_co_chmod(pdu, &fidp->path, 348160ce86c7SWei Liu v9mode_to_mode(v9stat.mode, 348260ce86c7SWei Liu &v9stat.extension)); 348360ce86c7SWei Liu if (err < 0) { 348460ce86c7SWei Liu goto out; 348560ce86c7SWei Liu } 348660ce86c7SWei Liu } 348760ce86c7SWei Liu if (v9stat.mtime != -1 || v9stat.atime != -1) { 348860ce86c7SWei Liu struct timespec times[2]; 348960ce86c7SWei Liu if (v9stat.atime != -1) { 349060ce86c7SWei Liu times[0].tv_sec = v9stat.atime; 349160ce86c7SWei Liu times[0].tv_nsec = 0; 349260ce86c7SWei Liu } else { 349360ce86c7SWei Liu times[0].tv_nsec = UTIME_OMIT; 349460ce86c7SWei Liu } 349560ce86c7SWei Liu if (v9stat.mtime != -1) { 349660ce86c7SWei Liu times[1].tv_sec = v9stat.mtime; 349760ce86c7SWei Liu times[1].tv_nsec = 0; 349860ce86c7SWei Liu } else { 349960ce86c7SWei Liu times[1].tv_nsec = UTIME_OMIT; 350060ce86c7SWei Liu } 350160ce86c7SWei Liu err = v9fs_co_utimensat(pdu, &fidp->path, times); 350260ce86c7SWei Liu if (err < 0) { 350360ce86c7SWei Liu goto out; 350460ce86c7SWei Liu } 350560ce86c7SWei Liu } 350660ce86c7SWei Liu if (v9stat.n_gid != -1 || v9stat.n_uid != -1) { 350760ce86c7SWei Liu err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid); 350860ce86c7SWei Liu if (err < 0) { 350960ce86c7SWei Liu goto out; 351060ce86c7SWei Liu } 351160ce86c7SWei Liu } 351260ce86c7SWei Liu if (v9stat.name.size != 0) { 35131d203986SGreg Kurz v9fs_path_write_lock(s); 351460ce86c7SWei Liu err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name); 35151d203986SGreg Kurz v9fs_path_unlock(s); 351660ce86c7SWei Liu if (err < 0) { 351760ce86c7SWei Liu goto out; 351860ce86c7SWei Liu } 351960ce86c7SWei Liu } 352060ce86c7SWei Liu if (v9stat.length != -1) { 352160ce86c7SWei Liu err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length); 352260ce86c7SWei Liu if (err < 0) { 352360ce86c7SWei Liu goto out; 352460ce86c7SWei Liu } 352560ce86c7SWei Liu } 352660ce86c7SWei Liu err = offset; 352760ce86c7SWei Liu out: 352860ce86c7SWei Liu put_fid(pdu, fidp); 352960ce86c7SWei Liu out_nofid: 353060ce86c7SWei Liu v9fs_stat_free(&v9stat); 353160ce86c7SWei Liu pdu_complete(pdu, err); 353260ce86c7SWei Liu } 353360ce86c7SWei Liu 353460ce86c7SWei Liu static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf) 353560ce86c7SWei Liu { 353660ce86c7SWei Liu uint32_t f_type; 353760ce86c7SWei Liu uint32_t f_bsize; 353860ce86c7SWei Liu uint64_t f_blocks; 353960ce86c7SWei Liu uint64_t f_bfree; 354060ce86c7SWei Liu uint64_t f_bavail; 354160ce86c7SWei Liu uint64_t f_files; 354260ce86c7SWei Liu uint64_t f_ffree; 354360ce86c7SWei Liu uint64_t fsid_val; 354460ce86c7SWei Liu uint32_t f_namelen; 354560ce86c7SWei Liu size_t offset = 7; 354660ce86c7SWei Liu int32_t bsize_factor; 354760ce86c7SWei Liu 354860ce86c7SWei Liu /* 354960ce86c7SWei Liu * compute bsize factor based on host file system block size 355060ce86c7SWei Liu * and client msize 355160ce86c7SWei Liu */ 355260ce86c7SWei Liu bsize_factor = (s->msize - P9_IOHDRSZ) / stbuf->f_bsize; 355360ce86c7SWei Liu if (!bsize_factor) { 355460ce86c7SWei Liu bsize_factor = 1; 355560ce86c7SWei Liu } 355660ce86c7SWei Liu f_type = stbuf->f_type; 355760ce86c7SWei Liu f_bsize = stbuf->f_bsize; 355860ce86c7SWei Liu f_bsize *= bsize_factor; 355960ce86c7SWei Liu /* 356060ce86c7SWei Liu * f_bsize is adjusted(multiplied) by bsize factor, so we need to 356160ce86c7SWei Liu * adjust(divide) the number of blocks, free blocks and available 356260ce86c7SWei Liu * blocks by bsize factor 356360ce86c7SWei Liu */ 356460ce86c7SWei Liu f_blocks = stbuf->f_blocks / bsize_factor; 356560ce86c7SWei Liu f_bfree = stbuf->f_bfree / bsize_factor; 356660ce86c7SWei Liu f_bavail = stbuf->f_bavail / bsize_factor; 356760ce86c7SWei Liu f_files = stbuf->f_files; 356860ce86c7SWei Liu f_ffree = stbuf->f_ffree; 3569f41db099SKeno Fischer #ifdef CONFIG_DARWIN 3570f41db099SKeno Fischer fsid_val = (unsigned int)stbuf->f_fsid.val[0] | 3571f41db099SKeno Fischer (unsigned long long)stbuf->f_fsid.val[1] << 32; 3572f41db099SKeno Fischer f_namelen = NAME_MAX; 3573f41db099SKeno Fischer #else 357460ce86c7SWei Liu fsid_val = (unsigned int) stbuf->f_fsid.__val[0] | 357560ce86c7SWei Liu (unsigned long long)stbuf->f_fsid.__val[1] << 32; 357660ce86c7SWei Liu f_namelen = stbuf->f_namelen; 3577f41db099SKeno Fischer #endif 357860ce86c7SWei Liu 357960ce86c7SWei Liu return pdu_marshal(pdu, offset, "ddqqqqqqd", 358060ce86c7SWei Liu f_type, f_bsize, f_blocks, f_bfree, 358160ce86c7SWei Liu f_bavail, f_files, f_ffree, 358260ce86c7SWei Liu fsid_val, f_namelen); 358360ce86c7SWei Liu } 358460ce86c7SWei Liu 35858440e22eSGreg Kurz static void coroutine_fn v9fs_statfs(void *opaque) 358660ce86c7SWei Liu { 358760ce86c7SWei Liu int32_t fid; 358860ce86c7SWei Liu ssize_t retval = 0; 358960ce86c7SWei Liu size_t offset = 7; 359060ce86c7SWei Liu V9fsFidState *fidp; 359160ce86c7SWei Liu struct statfs stbuf; 359260ce86c7SWei Liu V9fsPDU *pdu = opaque; 359360ce86c7SWei Liu V9fsState *s = pdu->s; 359460ce86c7SWei Liu 359560ce86c7SWei Liu retval = pdu_unmarshal(pdu, offset, "d", &fid); 359660ce86c7SWei Liu if (retval < 0) { 359760ce86c7SWei Liu goto out_nofid; 359860ce86c7SWei Liu } 359960ce86c7SWei Liu fidp = get_fid(pdu, fid); 360060ce86c7SWei Liu if (fidp == NULL) { 360160ce86c7SWei Liu retval = -ENOENT; 360260ce86c7SWei Liu goto out_nofid; 360360ce86c7SWei Liu } 360460ce86c7SWei Liu retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf); 360560ce86c7SWei Liu if (retval < 0) { 360660ce86c7SWei Liu goto out; 360760ce86c7SWei Liu } 360860ce86c7SWei Liu retval = v9fs_fill_statfs(s, pdu, &stbuf); 360960ce86c7SWei Liu if (retval < 0) { 361060ce86c7SWei Liu goto out; 361160ce86c7SWei Liu } 361260ce86c7SWei Liu retval += offset; 361360ce86c7SWei Liu out: 361460ce86c7SWei Liu put_fid(pdu, fidp); 361560ce86c7SWei Liu out_nofid: 361660ce86c7SWei Liu pdu_complete(pdu, retval); 361760ce86c7SWei Liu } 361860ce86c7SWei Liu 36198440e22eSGreg Kurz static void coroutine_fn v9fs_mknod(void *opaque) 362060ce86c7SWei Liu { 362160ce86c7SWei Liu 362260ce86c7SWei Liu int mode; 362360ce86c7SWei Liu gid_t gid; 362460ce86c7SWei Liu int32_t fid; 362560ce86c7SWei Liu V9fsQID qid; 362660ce86c7SWei Liu int err = 0; 362760ce86c7SWei Liu int major, minor; 362860ce86c7SWei Liu size_t offset = 7; 362960ce86c7SWei Liu V9fsString name; 363060ce86c7SWei Liu struct stat stbuf; 363160ce86c7SWei Liu V9fsFidState *fidp; 363260ce86c7SWei Liu V9fsPDU *pdu = opaque; 363360ce86c7SWei Liu 363460ce86c7SWei Liu v9fs_string_init(&name); 363560ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode, 363660ce86c7SWei Liu &major, &minor, &gid); 363760ce86c7SWei Liu if (err < 0) { 363860ce86c7SWei Liu goto out_nofid; 363960ce86c7SWei Liu } 364060ce86c7SWei Liu trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor); 364160ce86c7SWei Liu 3642fff39a7aSGreg Kurz if (name_is_illegal(name.data)) { 3643fff39a7aSGreg Kurz err = -ENOENT; 3644fff39a7aSGreg Kurz goto out_nofid; 3645fff39a7aSGreg Kurz } 3646fff39a7aSGreg Kurz 3647805b5d98SGreg Kurz if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 3648805b5d98SGreg Kurz err = -EEXIST; 3649805b5d98SGreg Kurz goto out_nofid; 3650805b5d98SGreg Kurz } 3651805b5d98SGreg Kurz 365260ce86c7SWei Liu fidp = get_fid(pdu, fid); 365360ce86c7SWei Liu if (fidp == NULL) { 365460ce86c7SWei Liu err = -ENOENT; 365560ce86c7SWei Liu goto out_nofid; 365660ce86c7SWei Liu } 365760ce86c7SWei Liu err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid, 365860ce86c7SWei Liu makedev(major, minor), mode, &stbuf); 365960ce86c7SWei Liu if (err < 0) { 366060ce86c7SWei Liu goto out; 366160ce86c7SWei Liu } 36623b5ee9e8SAntonios Motakis err = stat_to_qid(pdu, &stbuf, &qid); 36633b5ee9e8SAntonios Motakis if (err < 0) { 36643b5ee9e8SAntonios Motakis goto out; 36653b5ee9e8SAntonios Motakis } 366660ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Q", &qid); 366760ce86c7SWei Liu if (err < 0) { 366860ce86c7SWei Liu goto out; 366960ce86c7SWei Liu } 367060ce86c7SWei Liu err += offset; 367160ce86c7SWei Liu trace_v9fs_mknod_return(pdu->tag, pdu->id, 367260ce86c7SWei Liu qid.type, qid.version, qid.path); 367360ce86c7SWei Liu out: 367460ce86c7SWei Liu put_fid(pdu, fidp); 367560ce86c7SWei Liu out_nofid: 367660ce86c7SWei Liu pdu_complete(pdu, err); 367760ce86c7SWei Liu v9fs_string_free(&name); 367860ce86c7SWei Liu } 367960ce86c7SWei Liu 368060ce86c7SWei Liu /* 368160ce86c7SWei Liu * Implement posix byte range locking code 368260ce86c7SWei Liu * Server side handling of locking code is very simple, because 9p server in 368360ce86c7SWei Liu * QEMU can handle only one client. And most of the lock handling 368460ce86c7SWei Liu * (like conflict, merging) etc is done by the VFS layer itself, so no need to 368560ce86c7SWei Liu * do any thing in * qemu 9p server side lock code path. 368660ce86c7SWei Liu * So when a TLOCK request comes, always return success 368760ce86c7SWei Liu */ 36888440e22eSGreg Kurz static void coroutine_fn v9fs_lock(void *opaque) 368960ce86c7SWei Liu { 369060ce86c7SWei Liu V9fsFlock flock; 369160ce86c7SWei Liu size_t offset = 7; 369260ce86c7SWei Liu struct stat stbuf; 369360ce86c7SWei Liu V9fsFidState *fidp; 369460ce86c7SWei Liu int32_t fid, err = 0; 369560ce86c7SWei Liu V9fsPDU *pdu = opaque; 369660ce86c7SWei Liu 369760ce86c7SWei Liu v9fs_string_init(&flock.client_id); 369860ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type, 369960ce86c7SWei Liu &flock.flags, &flock.start, &flock.length, 370060ce86c7SWei Liu &flock.proc_id, &flock.client_id); 370160ce86c7SWei Liu if (err < 0) { 370260ce86c7SWei Liu goto out_nofid; 370360ce86c7SWei Liu } 370460ce86c7SWei Liu trace_v9fs_lock(pdu->tag, pdu->id, fid, 370560ce86c7SWei Liu flock.type, flock.start, flock.length); 370660ce86c7SWei Liu 370760ce86c7SWei Liu 370860ce86c7SWei Liu /* We support only block flag now (that too ignored currently) */ 370960ce86c7SWei Liu if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) { 371060ce86c7SWei Liu err = -EINVAL; 371160ce86c7SWei Liu goto out_nofid; 371260ce86c7SWei Liu } 371360ce86c7SWei Liu fidp = get_fid(pdu, fid); 371460ce86c7SWei Liu if (fidp == NULL) { 371560ce86c7SWei Liu err = -ENOENT; 371660ce86c7SWei Liu goto out_nofid; 371760ce86c7SWei Liu } 371860ce86c7SWei Liu err = v9fs_co_fstat(pdu, fidp, &stbuf); 371960ce86c7SWei Liu if (err < 0) { 372060ce86c7SWei Liu goto out; 372160ce86c7SWei Liu } 37224bae2b39SPaolo Bonzini err = pdu_marshal(pdu, offset, "b", P9_LOCK_SUCCESS); 37234bae2b39SPaolo Bonzini if (err < 0) { 37244bae2b39SPaolo Bonzini goto out; 37254bae2b39SPaolo Bonzini } 37264bae2b39SPaolo Bonzini err += offset; 37274bae2b39SPaolo Bonzini trace_v9fs_lock_return(pdu->tag, pdu->id, P9_LOCK_SUCCESS); 372860ce86c7SWei Liu out: 372960ce86c7SWei Liu put_fid(pdu, fidp); 373060ce86c7SWei Liu out_nofid: 373160ce86c7SWei Liu pdu_complete(pdu, err); 373260ce86c7SWei Liu v9fs_string_free(&flock.client_id); 373360ce86c7SWei Liu } 373460ce86c7SWei Liu 373560ce86c7SWei Liu /* 373660ce86c7SWei Liu * When a TGETLOCK request comes, always return success because all lock 373760ce86c7SWei Liu * handling is done by client's VFS layer. 373860ce86c7SWei Liu */ 37398440e22eSGreg Kurz static void coroutine_fn v9fs_getlock(void *opaque) 374060ce86c7SWei Liu { 374160ce86c7SWei Liu size_t offset = 7; 374260ce86c7SWei Liu struct stat stbuf; 374360ce86c7SWei Liu V9fsFidState *fidp; 374460ce86c7SWei Liu V9fsGetlock glock; 374560ce86c7SWei Liu int32_t fid, err = 0; 374660ce86c7SWei Liu V9fsPDU *pdu = opaque; 374760ce86c7SWei Liu 374860ce86c7SWei Liu v9fs_string_init(&glock.client_id); 374960ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type, 375060ce86c7SWei Liu &glock.start, &glock.length, &glock.proc_id, 375160ce86c7SWei Liu &glock.client_id); 375260ce86c7SWei Liu if (err < 0) { 375360ce86c7SWei Liu goto out_nofid; 375460ce86c7SWei Liu } 375560ce86c7SWei Liu trace_v9fs_getlock(pdu->tag, pdu->id, fid, 375660ce86c7SWei Liu glock.type, glock.start, glock.length); 375760ce86c7SWei Liu 375860ce86c7SWei Liu fidp = get_fid(pdu, fid); 375960ce86c7SWei Liu if (fidp == NULL) { 376060ce86c7SWei Liu err = -ENOENT; 376160ce86c7SWei Liu goto out_nofid; 376260ce86c7SWei Liu } 376360ce86c7SWei Liu err = v9fs_co_fstat(pdu, fidp, &stbuf); 376460ce86c7SWei Liu if (err < 0) { 376560ce86c7SWei Liu goto out; 376660ce86c7SWei Liu } 376760ce86c7SWei Liu glock.type = P9_LOCK_TYPE_UNLCK; 376860ce86c7SWei Liu err = pdu_marshal(pdu, offset, "bqqds", glock.type, 376960ce86c7SWei Liu glock.start, glock.length, glock.proc_id, 377060ce86c7SWei Liu &glock.client_id); 377160ce86c7SWei Liu if (err < 0) { 377260ce86c7SWei Liu goto out; 377360ce86c7SWei Liu } 377460ce86c7SWei Liu err += offset; 377560ce86c7SWei Liu trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start, 377660ce86c7SWei Liu glock.length, glock.proc_id); 377760ce86c7SWei Liu out: 377860ce86c7SWei Liu put_fid(pdu, fidp); 377960ce86c7SWei Liu out_nofid: 378060ce86c7SWei Liu pdu_complete(pdu, err); 378160ce86c7SWei Liu v9fs_string_free(&glock.client_id); 378260ce86c7SWei Liu } 378360ce86c7SWei Liu 37848440e22eSGreg Kurz static void coroutine_fn v9fs_mkdir(void *opaque) 378560ce86c7SWei Liu { 378660ce86c7SWei Liu V9fsPDU *pdu = opaque; 378760ce86c7SWei Liu size_t offset = 7; 378860ce86c7SWei Liu int32_t fid; 378960ce86c7SWei Liu struct stat stbuf; 379060ce86c7SWei Liu V9fsQID qid; 379160ce86c7SWei Liu V9fsString name; 379260ce86c7SWei Liu V9fsFidState *fidp; 379360ce86c7SWei Liu gid_t gid; 379460ce86c7SWei Liu int mode; 379560ce86c7SWei Liu int err = 0; 379660ce86c7SWei Liu 379760ce86c7SWei Liu v9fs_string_init(&name); 379860ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid); 379960ce86c7SWei Liu if (err < 0) { 380060ce86c7SWei Liu goto out_nofid; 380160ce86c7SWei Liu } 380260ce86c7SWei Liu trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid); 380360ce86c7SWei Liu 3804fff39a7aSGreg Kurz if (name_is_illegal(name.data)) { 3805fff39a7aSGreg Kurz err = -ENOENT; 3806fff39a7aSGreg Kurz goto out_nofid; 3807fff39a7aSGreg Kurz } 3808fff39a7aSGreg Kurz 3809805b5d98SGreg Kurz if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 3810805b5d98SGreg Kurz err = -EEXIST; 3811805b5d98SGreg Kurz goto out_nofid; 3812805b5d98SGreg Kurz } 3813805b5d98SGreg Kurz 381460ce86c7SWei Liu fidp = get_fid(pdu, fid); 381560ce86c7SWei Liu if (fidp == NULL) { 381660ce86c7SWei Liu err = -ENOENT; 381760ce86c7SWei Liu goto out_nofid; 381860ce86c7SWei Liu } 381960ce86c7SWei Liu err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf); 382060ce86c7SWei Liu if (err < 0) { 382160ce86c7SWei Liu goto out; 382260ce86c7SWei Liu } 38233b5ee9e8SAntonios Motakis err = stat_to_qid(pdu, &stbuf, &qid); 38243b5ee9e8SAntonios Motakis if (err < 0) { 38253b5ee9e8SAntonios Motakis goto out; 38263b5ee9e8SAntonios Motakis } 382760ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Q", &qid); 382860ce86c7SWei Liu if (err < 0) { 382960ce86c7SWei Liu goto out; 383060ce86c7SWei Liu } 383160ce86c7SWei Liu err += offset; 383260ce86c7SWei Liu trace_v9fs_mkdir_return(pdu->tag, pdu->id, 383360ce86c7SWei Liu qid.type, qid.version, qid.path, err); 383460ce86c7SWei Liu out: 383560ce86c7SWei Liu put_fid(pdu, fidp); 383660ce86c7SWei Liu out_nofid: 383760ce86c7SWei Liu pdu_complete(pdu, err); 383860ce86c7SWei Liu v9fs_string_free(&name); 383960ce86c7SWei Liu } 384060ce86c7SWei Liu 38418440e22eSGreg Kurz static void coroutine_fn v9fs_xattrwalk(void *opaque) 384260ce86c7SWei Liu { 384360ce86c7SWei Liu int64_t size; 384460ce86c7SWei Liu V9fsString name; 384560ce86c7SWei Liu ssize_t err = 0; 384660ce86c7SWei Liu size_t offset = 7; 384760ce86c7SWei Liu int32_t fid, newfid; 384860ce86c7SWei Liu V9fsFidState *file_fidp; 384960ce86c7SWei Liu V9fsFidState *xattr_fidp = NULL; 385060ce86c7SWei Liu V9fsPDU *pdu = opaque; 385160ce86c7SWei Liu V9fsState *s = pdu->s; 385260ce86c7SWei Liu 385360ce86c7SWei Liu v9fs_string_init(&name); 385460ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name); 385560ce86c7SWei Liu if (err < 0) { 385660ce86c7SWei Liu goto out_nofid; 385760ce86c7SWei Liu } 385860ce86c7SWei Liu trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data); 385960ce86c7SWei Liu 386060ce86c7SWei Liu file_fidp = get_fid(pdu, fid); 386160ce86c7SWei Liu if (file_fidp == NULL) { 386260ce86c7SWei Liu err = -ENOENT; 386360ce86c7SWei Liu goto out_nofid; 386460ce86c7SWei Liu } 386560ce86c7SWei Liu xattr_fidp = alloc_fid(s, newfid); 386660ce86c7SWei Liu if (xattr_fidp == NULL) { 386760ce86c7SWei Liu err = -EINVAL; 386860ce86c7SWei Liu goto out; 386960ce86c7SWei Liu } 387060ce86c7SWei Liu v9fs_path_copy(&xattr_fidp->path, &file_fidp->path); 3871ba42ebb8SLi Qiang if (!v9fs_string_size(&name)) { 387260ce86c7SWei Liu /* 387360ce86c7SWei Liu * listxattr request. Get the size first 387460ce86c7SWei Liu */ 387560ce86c7SWei Liu size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0); 387660ce86c7SWei Liu if (size < 0) { 387760ce86c7SWei Liu err = size; 387860ce86c7SWei Liu clunk_fid(s, xattr_fidp->fid); 387960ce86c7SWei Liu goto out; 388060ce86c7SWei Liu } 388160ce86c7SWei Liu /* 388260ce86c7SWei Liu * Read the xattr value 388360ce86c7SWei Liu */ 388460ce86c7SWei Liu xattr_fidp->fs.xattr.len = size; 388560ce86c7SWei Liu xattr_fidp->fid_type = P9_FID_XATTR; 3886dd28fbbcSLi Qiang xattr_fidp->fs.xattr.xattrwalk_fid = true; 38877bd92756SPrasad J Pandit xattr_fidp->fs.xattr.value = g_malloc0(size); 3888a647502cSKeno Fischer if (size) { 388960ce86c7SWei Liu err = v9fs_co_llistxattr(pdu, &xattr_fidp->path, 389060ce86c7SWei Liu xattr_fidp->fs.xattr.value, 389160ce86c7SWei Liu xattr_fidp->fs.xattr.len); 389260ce86c7SWei Liu if (err < 0) { 389360ce86c7SWei Liu clunk_fid(s, xattr_fidp->fid); 389460ce86c7SWei Liu goto out; 389560ce86c7SWei Liu } 389660ce86c7SWei Liu } 389760ce86c7SWei Liu err = pdu_marshal(pdu, offset, "q", size); 389860ce86c7SWei Liu if (err < 0) { 389960ce86c7SWei Liu goto out; 390060ce86c7SWei Liu } 390160ce86c7SWei Liu err += offset; 390260ce86c7SWei Liu } else { 390360ce86c7SWei Liu /* 390460ce86c7SWei Liu * specific xattr fid. We check for xattr 390560ce86c7SWei Liu * presence also collect the xattr size 390660ce86c7SWei Liu */ 390760ce86c7SWei Liu size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path, 390860ce86c7SWei Liu &name, NULL, 0); 390960ce86c7SWei Liu if (size < 0) { 391060ce86c7SWei Liu err = size; 391160ce86c7SWei Liu clunk_fid(s, xattr_fidp->fid); 391260ce86c7SWei Liu goto out; 391360ce86c7SWei Liu } 391460ce86c7SWei Liu /* 391560ce86c7SWei Liu * Read the xattr value 391660ce86c7SWei Liu */ 391760ce86c7SWei Liu xattr_fidp->fs.xattr.len = size; 391860ce86c7SWei Liu xattr_fidp->fid_type = P9_FID_XATTR; 3919dd28fbbcSLi Qiang xattr_fidp->fs.xattr.xattrwalk_fid = true; 39207bd92756SPrasad J Pandit xattr_fidp->fs.xattr.value = g_malloc0(size); 3921a647502cSKeno Fischer if (size) { 392260ce86c7SWei Liu err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path, 392360ce86c7SWei Liu &name, xattr_fidp->fs.xattr.value, 392460ce86c7SWei Liu xattr_fidp->fs.xattr.len); 392560ce86c7SWei Liu if (err < 0) { 392660ce86c7SWei Liu clunk_fid(s, xattr_fidp->fid); 392760ce86c7SWei Liu goto out; 392860ce86c7SWei Liu } 392960ce86c7SWei Liu } 393060ce86c7SWei Liu err = pdu_marshal(pdu, offset, "q", size); 393160ce86c7SWei Liu if (err < 0) { 393260ce86c7SWei Liu goto out; 393360ce86c7SWei Liu } 393460ce86c7SWei Liu err += offset; 393560ce86c7SWei Liu } 393660ce86c7SWei Liu trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size); 393760ce86c7SWei Liu out: 393860ce86c7SWei Liu put_fid(pdu, file_fidp); 393960ce86c7SWei Liu if (xattr_fidp) { 394060ce86c7SWei Liu put_fid(pdu, xattr_fidp); 394160ce86c7SWei Liu } 394260ce86c7SWei Liu out_nofid: 394360ce86c7SWei Liu pdu_complete(pdu, err); 394460ce86c7SWei Liu v9fs_string_free(&name); 394560ce86c7SWei Liu } 394660ce86c7SWei Liu 3947a136d175SWill Cohen #if defined(CONFIG_LINUX) 3948a136d175SWill Cohen /* Currently, only Linux has XATTR_SIZE_MAX */ 3949a136d175SWill Cohen #define P9_XATTR_SIZE_MAX XATTR_SIZE_MAX 3950a136d175SWill Cohen #elif defined(CONFIG_DARWIN) 3951a136d175SWill Cohen /* 3952a136d175SWill Cohen * Darwin doesn't seem to define a maximum xattr size in its user 3953a136d175SWill Cohen * space header, so manually configure it across platforms as 64k. 3954a136d175SWill Cohen * 3955a136d175SWill Cohen * Having no limit at all can lead to QEMU crashing during large g_malloc() 3956a136d175SWill Cohen * calls. Because QEMU does not currently support macOS guests, the below 3957a136d175SWill Cohen * preliminary solution only works due to its being a reflection of the limit of 3958a136d175SWill Cohen * Linux guests. 3959a136d175SWill Cohen */ 3960a136d175SWill Cohen #define P9_XATTR_SIZE_MAX 65536 3961a136d175SWill Cohen #else 3962a136d175SWill Cohen #error Missing definition for P9_XATTR_SIZE_MAX for this host system 3963a136d175SWill Cohen #endif 3964a136d175SWill Cohen 39658440e22eSGreg Kurz static void coroutine_fn v9fs_xattrcreate(void *opaque) 396660ce86c7SWei Liu { 3967aca6897fSKeno Fischer int flags, rflags = 0; 396860ce86c7SWei Liu int32_t fid; 39693b79ef2cSGreg Kurz uint64_t size; 397060ce86c7SWei Liu ssize_t err = 0; 397160ce86c7SWei Liu V9fsString name; 397260ce86c7SWei Liu size_t offset = 7; 397360ce86c7SWei Liu V9fsFidState *file_fidp; 397460ce86c7SWei Liu V9fsFidState *xattr_fidp; 397560ce86c7SWei Liu V9fsPDU *pdu = opaque; 397660ce86c7SWei Liu 397760ce86c7SWei Liu v9fs_string_init(&name); 397860ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags); 397960ce86c7SWei Liu if (err < 0) { 398060ce86c7SWei Liu goto out_nofid; 398160ce86c7SWei Liu } 398260ce86c7SWei Liu trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags); 398360ce86c7SWei Liu 3984aca6897fSKeno Fischer if (flags & ~(P9_XATTR_CREATE | P9_XATTR_REPLACE)) { 3985aca6897fSKeno Fischer err = -EINVAL; 3986aca6897fSKeno Fischer goto out_nofid; 3987aca6897fSKeno Fischer } 3988aca6897fSKeno Fischer 3989aca6897fSKeno Fischer if (flags & P9_XATTR_CREATE) { 3990aca6897fSKeno Fischer rflags |= XATTR_CREATE; 3991aca6897fSKeno Fischer } 3992aca6897fSKeno Fischer 3993aca6897fSKeno Fischer if (flags & P9_XATTR_REPLACE) { 3994aca6897fSKeno Fischer rflags |= XATTR_REPLACE; 3995aca6897fSKeno Fischer } 3996aca6897fSKeno Fischer 399738d7fd68SKeno Fischer if (size > P9_XATTR_SIZE_MAX) { 39983b79ef2cSGreg Kurz err = -E2BIG; 39993b79ef2cSGreg Kurz goto out_nofid; 40003b79ef2cSGreg Kurz } 40013b79ef2cSGreg Kurz 400260ce86c7SWei Liu file_fidp = get_fid(pdu, fid); 400360ce86c7SWei Liu if (file_fidp == NULL) { 400460ce86c7SWei Liu err = -EINVAL; 400560ce86c7SWei Liu goto out_nofid; 400660ce86c7SWei Liu } 4007dd654e03SGreg Kurz if (file_fidp->fid_type != P9_FID_NONE) { 4008dd654e03SGreg Kurz err = -EINVAL; 4009dd654e03SGreg Kurz goto out_put_fid; 4010dd654e03SGreg Kurz } 4011dd654e03SGreg Kurz 401260ce86c7SWei Liu /* Make the file fid point to xattr */ 401360ce86c7SWei Liu xattr_fidp = file_fidp; 401460ce86c7SWei Liu xattr_fidp->fid_type = P9_FID_XATTR; 401560ce86c7SWei Liu xattr_fidp->fs.xattr.copied_len = 0; 4016dd28fbbcSLi Qiang xattr_fidp->fs.xattr.xattrwalk_fid = false; 401760ce86c7SWei Liu xattr_fidp->fs.xattr.len = size; 4018aca6897fSKeno Fischer xattr_fidp->fs.xattr.flags = rflags; 401960ce86c7SWei Liu v9fs_string_init(&xattr_fidp->fs.xattr.name); 402060ce86c7SWei Liu v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name); 4021eb687602SLi Qiang xattr_fidp->fs.xattr.value = g_malloc0(size); 402260ce86c7SWei Liu err = offset; 4023dd654e03SGreg Kurz out_put_fid: 402460ce86c7SWei Liu put_fid(pdu, file_fidp); 402560ce86c7SWei Liu out_nofid: 402660ce86c7SWei Liu pdu_complete(pdu, err); 402760ce86c7SWei Liu v9fs_string_free(&name); 402860ce86c7SWei Liu } 402960ce86c7SWei Liu 40308440e22eSGreg Kurz static void coroutine_fn v9fs_readlink(void *opaque) 403160ce86c7SWei Liu { 403260ce86c7SWei Liu V9fsPDU *pdu = opaque; 403360ce86c7SWei Liu size_t offset = 7; 403460ce86c7SWei Liu V9fsString target; 403560ce86c7SWei Liu int32_t fid; 403660ce86c7SWei Liu int err = 0; 403760ce86c7SWei Liu V9fsFidState *fidp; 403860ce86c7SWei Liu 403960ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "d", &fid); 404060ce86c7SWei Liu if (err < 0) { 404160ce86c7SWei Liu goto out_nofid; 404260ce86c7SWei Liu } 404360ce86c7SWei Liu trace_v9fs_readlink(pdu->tag, pdu->id, fid); 404460ce86c7SWei Liu fidp = get_fid(pdu, fid); 404560ce86c7SWei Liu if (fidp == NULL) { 404660ce86c7SWei Liu err = -ENOENT; 404760ce86c7SWei Liu goto out_nofid; 404860ce86c7SWei Liu } 404960ce86c7SWei Liu 405060ce86c7SWei Liu v9fs_string_init(&target); 405160ce86c7SWei Liu err = v9fs_co_readlink(pdu, &fidp->path, &target); 405260ce86c7SWei Liu if (err < 0) { 405360ce86c7SWei Liu goto out; 405460ce86c7SWei Liu } 405560ce86c7SWei Liu err = pdu_marshal(pdu, offset, "s", &target); 405660ce86c7SWei Liu if (err < 0) { 405760ce86c7SWei Liu v9fs_string_free(&target); 405860ce86c7SWei Liu goto out; 405960ce86c7SWei Liu } 406060ce86c7SWei Liu err += offset; 406160ce86c7SWei Liu trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data); 406260ce86c7SWei Liu v9fs_string_free(&target); 406360ce86c7SWei Liu out: 406460ce86c7SWei Liu put_fid(pdu, fidp); 406560ce86c7SWei Liu out_nofid: 406660ce86c7SWei Liu pdu_complete(pdu, err); 406760ce86c7SWei Liu } 406860ce86c7SWei Liu 406960ce86c7SWei Liu static CoroutineEntry *pdu_co_handlers[] = { 407060ce86c7SWei Liu [P9_TREADDIR] = v9fs_readdir, 407160ce86c7SWei Liu [P9_TSTATFS] = v9fs_statfs, 407260ce86c7SWei Liu [P9_TGETATTR] = v9fs_getattr, 407360ce86c7SWei Liu [P9_TSETATTR] = v9fs_setattr, 407460ce86c7SWei Liu [P9_TXATTRWALK] = v9fs_xattrwalk, 407560ce86c7SWei Liu [P9_TXATTRCREATE] = v9fs_xattrcreate, 407660ce86c7SWei Liu [P9_TMKNOD] = v9fs_mknod, 407760ce86c7SWei Liu [P9_TRENAME] = v9fs_rename, 407860ce86c7SWei Liu [P9_TLOCK] = v9fs_lock, 407960ce86c7SWei Liu [P9_TGETLOCK] = v9fs_getlock, 408060ce86c7SWei Liu [P9_TRENAMEAT] = v9fs_renameat, 408160ce86c7SWei Liu [P9_TREADLINK] = v9fs_readlink, 408260ce86c7SWei Liu [P9_TUNLINKAT] = v9fs_unlinkat, 408360ce86c7SWei Liu [P9_TMKDIR] = v9fs_mkdir, 408460ce86c7SWei Liu [P9_TVERSION] = v9fs_version, 408560ce86c7SWei Liu [P9_TLOPEN] = v9fs_open, 408660ce86c7SWei Liu [P9_TATTACH] = v9fs_attach, 408760ce86c7SWei Liu [P9_TSTAT] = v9fs_stat, 408860ce86c7SWei Liu [P9_TWALK] = v9fs_walk, 408960ce86c7SWei Liu [P9_TCLUNK] = v9fs_clunk, 409060ce86c7SWei Liu [P9_TFSYNC] = v9fs_fsync, 409160ce86c7SWei Liu [P9_TOPEN] = v9fs_open, 409260ce86c7SWei Liu [P9_TREAD] = v9fs_read, 409360ce86c7SWei Liu #if 0 409460ce86c7SWei Liu [P9_TAUTH] = v9fs_auth, 409560ce86c7SWei Liu #endif 409660ce86c7SWei Liu [P9_TFLUSH] = v9fs_flush, 409760ce86c7SWei Liu [P9_TLINK] = v9fs_link, 409860ce86c7SWei Liu [P9_TSYMLINK] = v9fs_symlink, 409960ce86c7SWei Liu [P9_TCREATE] = v9fs_create, 410060ce86c7SWei Liu [P9_TLCREATE] = v9fs_lcreate, 410160ce86c7SWei Liu [P9_TWRITE] = v9fs_write, 410260ce86c7SWei Liu [P9_TWSTAT] = v9fs_wstat, 410360ce86c7SWei Liu [P9_TREMOVE] = v9fs_remove, 410460ce86c7SWei Liu }; 410560ce86c7SWei Liu 41068440e22eSGreg Kurz static void coroutine_fn v9fs_op_not_supp(void *opaque) 410760ce86c7SWei Liu { 410860ce86c7SWei Liu V9fsPDU *pdu = opaque; 410960ce86c7SWei Liu pdu_complete(pdu, -EOPNOTSUPP); 411060ce86c7SWei Liu } 411160ce86c7SWei Liu 41128440e22eSGreg Kurz static void coroutine_fn v9fs_fs_ro(void *opaque) 411360ce86c7SWei Liu { 411460ce86c7SWei Liu V9fsPDU *pdu = opaque; 411560ce86c7SWei Liu pdu_complete(pdu, -EROFS); 411660ce86c7SWei Liu } 411760ce86c7SWei Liu 411860ce86c7SWei Liu static inline bool is_read_only_op(V9fsPDU *pdu) 411960ce86c7SWei Liu { 412060ce86c7SWei Liu switch (pdu->id) { 412160ce86c7SWei Liu case P9_TREADDIR: 412260ce86c7SWei Liu case P9_TSTATFS: 412360ce86c7SWei Liu case P9_TGETATTR: 412460ce86c7SWei Liu case P9_TXATTRWALK: 412560ce86c7SWei Liu case P9_TLOCK: 412660ce86c7SWei Liu case P9_TGETLOCK: 412760ce86c7SWei Liu case P9_TREADLINK: 412860ce86c7SWei Liu case P9_TVERSION: 412960ce86c7SWei Liu case P9_TLOPEN: 413060ce86c7SWei Liu case P9_TATTACH: 413160ce86c7SWei Liu case P9_TSTAT: 413260ce86c7SWei Liu case P9_TWALK: 413360ce86c7SWei Liu case P9_TCLUNK: 413460ce86c7SWei Liu case P9_TFSYNC: 413560ce86c7SWei Liu case P9_TOPEN: 413660ce86c7SWei Liu case P9_TREAD: 413760ce86c7SWei Liu case P9_TAUTH: 413860ce86c7SWei Liu case P9_TFLUSH: 413960ce86c7SWei Liu return 1; 414060ce86c7SWei Liu default: 414160ce86c7SWei Liu return 0; 414260ce86c7SWei Liu } 414360ce86c7SWei Liu } 414460ce86c7SWei Liu 4145506f3275SGreg Kurz void pdu_submit(V9fsPDU *pdu, P9MsgHeader *hdr) 414660ce86c7SWei Liu { 414760ce86c7SWei Liu Coroutine *co; 414860ce86c7SWei Liu CoroutineEntry *handler; 414960ce86c7SWei Liu V9fsState *s = pdu->s; 415060ce86c7SWei Liu 4151506f3275SGreg Kurz pdu->size = le32_to_cpu(hdr->size_le); 4152506f3275SGreg Kurz pdu->id = hdr->id; 4153506f3275SGreg Kurz pdu->tag = le16_to_cpu(hdr->tag_le); 4154506f3275SGreg Kurz 415560ce86c7SWei Liu if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) || 415660ce86c7SWei Liu (pdu_co_handlers[pdu->id] == NULL)) { 415760ce86c7SWei Liu handler = v9fs_op_not_supp; 4158d1471233SGreg Kurz } else if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) { 4159d1471233SGreg Kurz handler = v9fs_fs_ro; 416060ce86c7SWei Liu } else { 416160ce86c7SWei Liu handler = pdu_co_handlers[pdu->id]; 416260ce86c7SWei Liu } 416360ce86c7SWei Liu 4164506f3275SGreg Kurz qemu_co_queue_init(&pdu->complete); 41650b8b8753SPaolo Bonzini co = qemu_coroutine_create(handler, pdu); 41660b8b8753SPaolo Bonzini qemu_coroutine_enter(co); 416760ce86c7SWei Liu } 416860ce86c7SWei Liu 41692a0c56aaSWei Liu /* Returns 0 on success, 1 on failure. */ 4170066eb006SGreg Kurz int v9fs_device_realize_common(V9fsState *s, const V9fsTransport *t, 4171066eb006SGreg Kurz Error **errp) 41722a0c56aaSWei Liu { 417392c45122SVladimir Sementsov-Ogievskiy ERRP_GUARD(); 41742a0c56aaSWei Liu int i, len; 41752a0c56aaSWei Liu struct stat stat; 41762a0c56aaSWei Liu FsDriverEntry *fse; 41772a0c56aaSWei Liu V9fsPath path; 41782a0c56aaSWei Liu int rc = 1; 41792a0c56aaSWei Liu 4180066eb006SGreg Kurz assert(!s->transport); 4181066eb006SGreg Kurz s->transport = t; 4182066eb006SGreg Kurz 41832a0c56aaSWei Liu /* initialize pdu allocator */ 41842a0c56aaSWei Liu QLIST_INIT(&s->free_list); 41852a0c56aaSWei Liu QLIST_INIT(&s->active_list); 41860d78289cSGreg Kurz for (i = 0; i < MAX_REQ; i++) { 4187583f21f8SStefano Stabellini QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next); 4188583f21f8SStefano Stabellini s->pdus[i].s = s; 4189583f21f8SStefano Stabellini s->pdus[i].idx = i; 41902a0c56aaSWei Liu } 41912a0c56aaSWei Liu 41922a0c56aaSWei Liu v9fs_path_init(&path); 41932a0c56aaSWei Liu 41942a0c56aaSWei Liu fse = get_fsdev_fsentry(s->fsconf.fsdev_id); 41952a0c56aaSWei Liu 41962a0c56aaSWei Liu if (!fse) { 41972a0c56aaSWei Liu /* We don't have a fsdev identified by fsdev_id */ 41982a0c56aaSWei Liu error_setg(errp, "9pfs device couldn't find fsdev with the " 41992a0c56aaSWei Liu "id = %s", 42002a0c56aaSWei Liu s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL"); 42012a0c56aaSWei Liu goto out; 42022a0c56aaSWei Liu } 42032a0c56aaSWei Liu 42042a0c56aaSWei Liu if (!s->fsconf.tag) { 42052a0c56aaSWei Liu /* we haven't specified a mount_tag */ 42062a0c56aaSWei Liu error_setg(errp, "fsdev with id %s needs mount_tag arguments", 42072a0c56aaSWei Liu s->fsconf.fsdev_id); 42082a0c56aaSWei Liu goto out; 42092a0c56aaSWei Liu } 42102a0c56aaSWei Liu 42112a0c56aaSWei Liu s->ctx.export_flags = fse->export_flags; 42122a0c56aaSWei Liu s->ctx.fs_root = g_strdup(fse->path); 42132a0c56aaSWei Liu s->ctx.exops.get_st_gen = NULL; 42142a0c56aaSWei Liu len = strlen(s->fsconf.tag); 42152a0c56aaSWei Liu if (len > MAX_TAG_LEN - 1) { 42162a0c56aaSWei Liu error_setg(errp, "mount tag '%s' (%d bytes) is longer than " 42172a0c56aaSWei Liu "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1); 42182a0c56aaSWei Liu goto out; 42192a0c56aaSWei Liu } 42202a0c56aaSWei Liu 42212a0c56aaSWei Liu s->tag = g_strdup(s->fsconf.tag); 42222a0c56aaSWei Liu s->ctx.uid = -1; 42232a0c56aaSWei Liu 42242a0c56aaSWei Liu s->ops = fse->ops; 42252a0c56aaSWei Liu 4226b96feb2cSTobias Schramm s->ctx.fmode = fse->fmode; 4227b96feb2cSTobias Schramm s->ctx.dmode = fse->dmode; 4228b96feb2cSTobias Schramm 4229feabd6cfSGreg Kurz QSIMPLEQ_INIT(&s->fid_list); 42302a0c56aaSWei Liu qemu_co_rwlock_init(&s->rename_lock); 42312a0c56aaSWei Liu 423265603a80SGreg Kurz if (s->ops->init(&s->ctx, errp) < 0) { 423365603a80SGreg Kurz error_prepend(errp, "cannot initialize fsdev '%s': ", 423465603a80SGreg Kurz s->fsconf.fsdev_id); 42352a0c56aaSWei Liu goto out; 42362a0c56aaSWei Liu } 42372a0c56aaSWei Liu 42382a0c56aaSWei Liu /* 42392a0c56aaSWei Liu * Check details of export path, We need to use fs driver 42402a0c56aaSWei Liu * call back to do that. Since we are in the init path, we don't 42412a0c56aaSWei Liu * use co-routines here. 42422a0c56aaSWei Liu */ 42432a0c56aaSWei Liu if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) { 42442a0c56aaSWei Liu error_setg(errp, 42452a0c56aaSWei Liu "error in converting name to path %s", strerror(errno)); 42462a0c56aaSWei Liu goto out; 42472a0c56aaSWei Liu } 42482a0c56aaSWei Liu if (s->ops->lstat(&s->ctx, &path, &stat)) { 42492a0c56aaSWei Liu error_setg(errp, "share path %s does not exist", fse->path); 42502a0c56aaSWei Liu goto out; 42512a0c56aaSWei Liu } else if (!S_ISDIR(stat.st_mode)) { 42522a0c56aaSWei Liu error_setg(errp, "share path %s is not a directory", fse->path); 42532a0c56aaSWei Liu goto out; 42542a0c56aaSWei Liu } 4255b8bbdb88SPradeep Jagadeesh 42563b5ee9e8SAntonios Motakis s->dev_id = stat.st_dev; 42573b5ee9e8SAntonios Motakis 42586b6aa828SChristian Schoenebeck /* init inode remapping : */ 42596b6aa828SChristian Schoenebeck /* hash table for variable length inode suffixes */ 42606b6aa828SChristian Schoenebeck qpd_table_init(&s->qpd_table); 42616b6aa828SChristian Schoenebeck /* hash table for slow/full inode remapping (most users won't need it) */ 42626b6aa828SChristian Schoenebeck qpf_table_init(&s->qpf_table); 42636b6aa828SChristian Schoenebeck /* hash table for quick inode remapping */ 42641a6ed33cSAntonios Motakis qpp_table_init(&s->qpp_table); 42656b6aa828SChristian Schoenebeck s->qp_ndevices = 0; 42666b6aa828SChristian Schoenebeck s->qp_affix_next = 1; /* reserve 0 to detect overflow */ 4267f3fe4a2dSAntonios Motakis s->qp_fullpath_next = 1; 42681a6ed33cSAntonios Motakis 4269b8bbdb88SPradeep Jagadeesh s->ctx.fst = &fse->fst; 4270b8bbdb88SPradeep Jagadeesh fsdev_throttle_init(s->ctx.fst); 4271b8bbdb88SPradeep Jagadeesh 42722a0c56aaSWei Liu rc = 0; 42732a0c56aaSWei Liu out: 42742a0c56aaSWei Liu if (rc) { 4275b69c3c21SMarkus Armbruster v9fs_device_unrealize_common(s); 4276702dbcc2SLi Qiang } 42772a0c56aaSWei Liu v9fs_path_free(&path); 42782a0c56aaSWei Liu return rc; 42792a0c56aaSWei Liu } 42802a0c56aaSWei Liu 4281b69c3c21SMarkus Armbruster void v9fs_device_unrealize_common(V9fsState *s) 42822a0c56aaSWei Liu { 4283c0da0cb7SGreg Kurz if (s->ops && s->ops->cleanup) { 4284702dbcc2SLi Qiang s->ops->cleanup(&s->ctx); 4285702dbcc2SLi Qiang } 4286c0da0cb7SGreg Kurz if (s->ctx.fst) { 4287b8bbdb88SPradeep Jagadeesh fsdev_throttle_cleanup(s->ctx.fst); 4288c0da0cb7SGreg Kurz } 42892a0c56aaSWei Liu g_free(s->tag); 42906b6aa828SChristian Schoenebeck qp_table_destroy(&s->qpd_table); 4291f3fe4a2dSAntonios Motakis qp_table_destroy(&s->qpp_table); 4292f3fe4a2dSAntonios Motakis qp_table_destroy(&s->qpf_table); 42934774718eSLi Qiang g_free(s->ctx.fs_root); 42942a0c56aaSWei Liu } 42952a0c56aaSWei Liu 42960e44a0fdSGreg Kurz typedef struct VirtfsCoResetData { 42970e44a0fdSGreg Kurz V9fsPDU pdu; 42980e44a0fdSGreg Kurz bool done; 42990e44a0fdSGreg Kurz } VirtfsCoResetData; 43000e44a0fdSGreg Kurz 43010e44a0fdSGreg Kurz static void coroutine_fn virtfs_co_reset(void *opaque) 43020e44a0fdSGreg Kurz { 43030e44a0fdSGreg Kurz VirtfsCoResetData *data = opaque; 43040e44a0fdSGreg Kurz 43050e44a0fdSGreg Kurz virtfs_reset(&data->pdu); 43060e44a0fdSGreg Kurz data->done = true; 43070e44a0fdSGreg Kurz } 43080e44a0fdSGreg Kurz 43090e44a0fdSGreg Kurz void v9fs_reset(V9fsState *s) 43100e44a0fdSGreg Kurz { 43110e44a0fdSGreg Kurz VirtfsCoResetData data = { .pdu = { .s = s }, .done = false }; 43120e44a0fdSGreg Kurz Coroutine *co; 43130e44a0fdSGreg Kurz 43140e44a0fdSGreg Kurz while (!QLIST_EMPTY(&s->active_list)) { 43150e44a0fdSGreg Kurz aio_poll(qemu_get_aio_context(), true); 43160e44a0fdSGreg Kurz } 43170e44a0fdSGreg Kurz 43180e44a0fdSGreg Kurz co = qemu_coroutine_create(virtfs_co_reset, &data); 43190e44a0fdSGreg Kurz qemu_coroutine_enter(co); 43200e44a0fdSGreg Kurz 43210e44a0fdSGreg Kurz while (!data.done) { 43220e44a0fdSGreg Kurz aio_poll(qemu_get_aio_context(), true); 43230e44a0fdSGreg Kurz } 43240e44a0fdSGreg Kurz } 43250e44a0fdSGreg Kurz 432660ce86c7SWei Liu static void __attribute__((__constructor__)) v9fs_set_fd_limit(void) 432760ce86c7SWei Liu { 432860ce86c7SWei Liu struct rlimit rlim; 432960ce86c7SWei Liu if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) { 433063325b18SGreg Kurz error_report("Failed to get the resource limit"); 433160ce86c7SWei Liu exit(1); 433260ce86c7SWei Liu } 433360ce86c7SWei Liu open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur / 3); 433460ce86c7SWei Liu open_fd_rc = rlim.rlim_cur / 2; 433560ce86c7SWei Liu } 4336