160ce86c7SWei Liu /* 260ce86c7SWei Liu * Virtio 9p backend 360ce86c7SWei Liu * 460ce86c7SWei Liu * Copyright IBM, Corp. 2010 560ce86c7SWei Liu * 660ce86c7SWei Liu * Authors: 760ce86c7SWei Liu * Anthony Liguori <aliguori@us.ibm.com> 860ce86c7SWei Liu * 960ce86c7SWei Liu * This work is licensed under the terms of the GNU GPL, version 2. See 1060ce86c7SWei Liu * the COPYING file in the top-level directory. 1160ce86c7SWei Liu * 1260ce86c7SWei Liu */ 1360ce86c7SWei Liu 146f569084SChristian Schoenebeck /* 156f569084SChristian Schoenebeck * Not so fast! You might want to read the 9p developer docs first: 166f569084SChristian Schoenebeck * https://wiki.qemu.org/Documentation/9p 176f569084SChristian Schoenebeck */ 186f569084SChristian Schoenebeck 19fbc04127SPeter Maydell #include "qemu/osdep.h" 20e3e83f2eSGreg Kurz #include <glib/gprintf.h> 2160ce86c7SWei Liu #include "hw/virtio/virtio.h" 22da34e65cSMarkus Armbruster #include "qapi/error.h" 2360ce86c7SWei Liu #include "qemu/error-report.h" 2460ce86c7SWei Liu #include "qemu/iov.h" 25db725815SMarkus Armbruster #include "qemu/main-loop.h" 2660ce86c7SWei Liu #include "qemu/sockets.h" 2760ce86c7SWei Liu #include "virtio-9p.h" 2860ce86c7SWei Liu #include "fsdev/qemu-fsdev.h" 2960ce86c7SWei Liu #include "9p-xattr.h" 3060ce86c7SWei Liu #include "coth.h" 3160ce86c7SWei Liu #include "trace.h" 32795c40b8SJuan Quintela #include "migration/blocker.h" 331a6ed33cSAntonios Motakis #include "qemu/xxhash.h" 346b6aa828SChristian Schoenebeck #include <math.h> 3503556ea9SDan Robertson #include <linux/limits.h> 3660ce86c7SWei Liu 3760ce86c7SWei Liu int open_fd_hw; 3860ce86c7SWei Liu int total_open_fd; 3960ce86c7SWei Liu static int open_fd_rc; 4060ce86c7SWei Liu 4160ce86c7SWei Liu enum { 4260ce86c7SWei Liu Oread = 0x00, 4360ce86c7SWei Liu Owrite = 0x01, 4460ce86c7SWei Liu Ordwr = 0x02, 4560ce86c7SWei Liu Oexec = 0x03, 4660ce86c7SWei Liu Oexcl = 0x04, 4760ce86c7SWei Liu Otrunc = 0x10, 4860ce86c7SWei Liu Orexec = 0x20, 4960ce86c7SWei Liu Orclose = 0x40, 5060ce86c7SWei Liu Oappend = 0x80, 5160ce86c7SWei Liu }; 5260ce86c7SWei Liu 5375673590SGreg Kurz static ssize_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...) 5460ce86c7SWei Liu { 5560ce86c7SWei Liu ssize_t ret; 5660ce86c7SWei Liu va_list ap; 5760ce86c7SWei Liu 5860ce86c7SWei Liu va_start(ap, fmt); 59ea83441cSStefano Stabellini ret = pdu->s->transport->pdu_vmarshal(pdu, offset, fmt, ap); 6060ce86c7SWei Liu va_end(ap); 6160ce86c7SWei Liu 6260ce86c7SWei Liu return ret; 6360ce86c7SWei Liu } 6460ce86c7SWei Liu 6575673590SGreg Kurz static ssize_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...) 6660ce86c7SWei Liu { 6760ce86c7SWei Liu ssize_t ret; 6860ce86c7SWei Liu va_list ap; 6960ce86c7SWei Liu 7060ce86c7SWei Liu va_start(ap, fmt); 71ea83441cSStefano Stabellini ret = pdu->s->transport->pdu_vunmarshal(pdu, offset, fmt, ap); 7260ce86c7SWei Liu va_end(ap); 7360ce86c7SWei Liu 7460ce86c7SWei Liu return ret; 7560ce86c7SWei Liu } 7660ce86c7SWei Liu 7760ce86c7SWei Liu static int omode_to_uflags(int8_t mode) 7860ce86c7SWei Liu { 7960ce86c7SWei Liu int ret = 0; 8060ce86c7SWei Liu 8160ce86c7SWei Liu switch (mode & 3) { 8260ce86c7SWei Liu case Oread: 8360ce86c7SWei Liu ret = O_RDONLY; 8460ce86c7SWei Liu break; 8560ce86c7SWei Liu case Ordwr: 8660ce86c7SWei Liu ret = O_RDWR; 8760ce86c7SWei Liu break; 8860ce86c7SWei Liu case Owrite: 8960ce86c7SWei Liu ret = O_WRONLY; 9060ce86c7SWei Liu break; 9160ce86c7SWei Liu case Oexec: 9260ce86c7SWei Liu ret = O_RDONLY; 9360ce86c7SWei Liu break; 9460ce86c7SWei Liu } 9560ce86c7SWei Liu 9660ce86c7SWei Liu if (mode & Otrunc) { 9760ce86c7SWei Liu ret |= O_TRUNC; 9860ce86c7SWei Liu } 9960ce86c7SWei Liu 10060ce86c7SWei Liu if (mode & Oappend) { 10160ce86c7SWei Liu ret |= O_APPEND; 10260ce86c7SWei Liu } 10360ce86c7SWei Liu 10460ce86c7SWei Liu if (mode & Oexcl) { 10560ce86c7SWei Liu ret |= O_EXCL; 10660ce86c7SWei Liu } 10760ce86c7SWei Liu 10860ce86c7SWei Liu return ret; 10960ce86c7SWei Liu } 11060ce86c7SWei Liu 1118e71b96cSGreg Kurz typedef struct DotlOpenflagMap { 11260ce86c7SWei Liu int dotl_flag; 11360ce86c7SWei Liu int open_flag; 1148e71b96cSGreg Kurz } DotlOpenflagMap; 11560ce86c7SWei Liu 11660ce86c7SWei Liu static int dotl_to_open_flags(int flags) 11760ce86c7SWei Liu { 11860ce86c7SWei Liu int i; 11960ce86c7SWei Liu /* 12060ce86c7SWei Liu * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY 12160ce86c7SWei Liu * and P9_DOTL_NOACCESS 12260ce86c7SWei Liu */ 12360ce86c7SWei Liu int oflags = flags & O_ACCMODE; 12460ce86c7SWei Liu 1258e71b96cSGreg Kurz DotlOpenflagMap dotl_oflag_map[] = { 12660ce86c7SWei Liu { P9_DOTL_CREATE, O_CREAT }, 12760ce86c7SWei Liu { P9_DOTL_EXCL, O_EXCL }, 12860ce86c7SWei Liu { P9_DOTL_NOCTTY , O_NOCTTY }, 12960ce86c7SWei Liu { P9_DOTL_TRUNC, O_TRUNC }, 13060ce86c7SWei Liu { P9_DOTL_APPEND, O_APPEND }, 13160ce86c7SWei Liu { P9_DOTL_NONBLOCK, O_NONBLOCK } , 13260ce86c7SWei Liu { P9_DOTL_DSYNC, O_DSYNC }, 13360ce86c7SWei Liu { P9_DOTL_FASYNC, FASYNC }, 13460ce86c7SWei Liu { P9_DOTL_DIRECT, O_DIRECT }, 13560ce86c7SWei Liu { P9_DOTL_LARGEFILE, O_LARGEFILE }, 13660ce86c7SWei Liu { P9_DOTL_DIRECTORY, O_DIRECTORY }, 13760ce86c7SWei Liu { P9_DOTL_NOFOLLOW, O_NOFOLLOW }, 13860ce86c7SWei Liu { P9_DOTL_NOATIME, O_NOATIME }, 13960ce86c7SWei Liu { P9_DOTL_SYNC, O_SYNC }, 14060ce86c7SWei Liu }; 14160ce86c7SWei Liu 14260ce86c7SWei Liu for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) { 14360ce86c7SWei Liu if (flags & dotl_oflag_map[i].dotl_flag) { 14460ce86c7SWei Liu oflags |= dotl_oflag_map[i].open_flag; 14560ce86c7SWei Liu } 14660ce86c7SWei Liu } 14760ce86c7SWei Liu 14860ce86c7SWei Liu return oflags; 14960ce86c7SWei Liu } 15060ce86c7SWei Liu 15160ce86c7SWei Liu void cred_init(FsCred *credp) 15260ce86c7SWei Liu { 15360ce86c7SWei Liu credp->fc_uid = -1; 15460ce86c7SWei Liu credp->fc_gid = -1; 15560ce86c7SWei Liu credp->fc_mode = -1; 15660ce86c7SWei Liu credp->fc_rdev = -1; 15760ce86c7SWei Liu } 15860ce86c7SWei Liu 15960ce86c7SWei Liu static int get_dotl_openflags(V9fsState *s, int oflags) 16060ce86c7SWei Liu { 16160ce86c7SWei Liu int flags; 16260ce86c7SWei Liu /* 16360ce86c7SWei Liu * Filter the client open flags 16460ce86c7SWei Liu */ 16560ce86c7SWei Liu flags = dotl_to_open_flags(oflags); 16660ce86c7SWei Liu flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT); 16760ce86c7SWei Liu /* 16860ce86c7SWei Liu * Ignore direct disk access hint until the server supports it. 16960ce86c7SWei Liu */ 17060ce86c7SWei Liu flags &= ~O_DIRECT; 17160ce86c7SWei Liu return flags; 17260ce86c7SWei Liu } 17360ce86c7SWei Liu 17460ce86c7SWei Liu void v9fs_path_init(V9fsPath *path) 17560ce86c7SWei Liu { 17660ce86c7SWei Liu path->data = NULL; 17760ce86c7SWei Liu path->size = 0; 17860ce86c7SWei Liu } 17960ce86c7SWei Liu 18060ce86c7SWei Liu void v9fs_path_free(V9fsPath *path) 18160ce86c7SWei Liu { 18260ce86c7SWei Liu g_free(path->data); 18360ce86c7SWei Liu path->data = NULL; 18460ce86c7SWei Liu path->size = 0; 18560ce86c7SWei Liu } 18660ce86c7SWei Liu 187e3e83f2eSGreg Kurz 188e3e83f2eSGreg Kurz void GCC_FMT_ATTR(2, 3) 189e3e83f2eSGreg Kurz v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...) 190e3e83f2eSGreg Kurz { 191e3e83f2eSGreg Kurz va_list ap; 192e3e83f2eSGreg Kurz 193e3e83f2eSGreg Kurz v9fs_path_free(path); 194e3e83f2eSGreg Kurz 195e3e83f2eSGreg Kurz va_start(ap, fmt); 196e3e83f2eSGreg Kurz /* Bump the size for including terminating NULL */ 197e3e83f2eSGreg Kurz path->size = g_vasprintf(&path->data, fmt, ap) + 1; 198e3e83f2eSGreg Kurz va_end(ap); 199e3e83f2eSGreg Kurz } 200e3e83f2eSGreg Kurz 201e446a1ebSMarc-André Lureau void v9fs_path_copy(V9fsPath *dst, const V9fsPath *src) 20260ce86c7SWei Liu { 203e446a1ebSMarc-André Lureau v9fs_path_free(dst); 204e446a1ebSMarc-André Lureau dst->size = src->size; 205e446a1ebSMarc-André Lureau dst->data = g_memdup(src->data, src->size); 20660ce86c7SWei Liu } 20760ce86c7SWei Liu 20860ce86c7SWei Liu int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath, 20960ce86c7SWei Liu const char *name, V9fsPath *path) 21060ce86c7SWei Liu { 21160ce86c7SWei Liu int err; 21260ce86c7SWei Liu err = s->ops->name_to_path(&s->ctx, dirpath, name, path); 21360ce86c7SWei Liu if (err < 0) { 21460ce86c7SWei Liu err = -errno; 21560ce86c7SWei Liu } 21660ce86c7SWei Liu return err; 21760ce86c7SWei Liu } 21860ce86c7SWei Liu 21960ce86c7SWei Liu /* 22060ce86c7SWei Liu * Return TRUE if s1 is an ancestor of s2. 22160ce86c7SWei Liu * 22260ce86c7SWei Liu * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d". 22360ce86c7SWei Liu * As a special case, We treat s1 as ancestor of s2 if they are same! 22460ce86c7SWei Liu */ 22560ce86c7SWei Liu static int v9fs_path_is_ancestor(V9fsPath *s1, V9fsPath *s2) 22660ce86c7SWei Liu { 22760ce86c7SWei Liu if (!strncmp(s1->data, s2->data, s1->size - 1)) { 22860ce86c7SWei Liu if (s2->data[s1->size - 1] == '\0' || s2->data[s1->size - 1] == '/') { 22960ce86c7SWei Liu return 1; 23060ce86c7SWei Liu } 23160ce86c7SWei Liu } 23260ce86c7SWei Liu return 0; 23360ce86c7SWei Liu } 23460ce86c7SWei Liu 23560ce86c7SWei Liu static size_t v9fs_string_size(V9fsString *str) 23660ce86c7SWei Liu { 23760ce86c7SWei Liu return str->size; 23860ce86c7SWei Liu } 23960ce86c7SWei Liu 24060ce86c7SWei Liu /* 24160ce86c7SWei Liu * returns 0 if fid got re-opened, 1 if not, < 0 on error */ 2428440e22eSGreg Kurz static int coroutine_fn v9fs_reopen_fid(V9fsPDU *pdu, V9fsFidState *f) 24360ce86c7SWei Liu { 24460ce86c7SWei Liu int err = 1; 24560ce86c7SWei Liu if (f->fid_type == P9_FID_FILE) { 24660ce86c7SWei Liu if (f->fs.fd == -1) { 24760ce86c7SWei Liu do { 24860ce86c7SWei Liu err = v9fs_co_open(pdu, f, f->open_flags); 24960ce86c7SWei Liu } while (err == -EINTR && !pdu->cancelled); 25060ce86c7SWei Liu } 25160ce86c7SWei Liu } else if (f->fid_type == P9_FID_DIR) { 252f314ea4eSGreg Kurz if (f->fs.dir.stream == NULL) { 25360ce86c7SWei Liu do { 25460ce86c7SWei Liu err = v9fs_co_opendir(pdu, f); 25560ce86c7SWei Liu } while (err == -EINTR && !pdu->cancelled); 25660ce86c7SWei Liu } 25760ce86c7SWei Liu } 25860ce86c7SWei Liu return err; 25960ce86c7SWei Liu } 26060ce86c7SWei Liu 2618440e22eSGreg Kurz static V9fsFidState *coroutine_fn get_fid(V9fsPDU *pdu, int32_t fid) 26260ce86c7SWei Liu { 26360ce86c7SWei Liu int err; 26460ce86c7SWei Liu V9fsFidState *f; 26560ce86c7SWei Liu V9fsState *s = pdu->s; 26660ce86c7SWei Liu 267feabd6cfSGreg Kurz QSIMPLEQ_FOREACH(f, &s->fid_list, next) { 26860ce86c7SWei Liu BUG_ON(f->clunked); 26960ce86c7SWei Liu if (f->fid == fid) { 27060ce86c7SWei Liu /* 27160ce86c7SWei Liu * Update the fid ref upfront so that 27260ce86c7SWei Liu * we don't get reclaimed when we yield 27360ce86c7SWei Liu * in open later. 27460ce86c7SWei Liu */ 27560ce86c7SWei Liu f->ref++; 27660ce86c7SWei Liu /* 27760ce86c7SWei Liu * check whether we need to reopen the 27860ce86c7SWei Liu * file. We might have closed the fd 27960ce86c7SWei Liu * while trying to free up some file 28060ce86c7SWei Liu * descriptors. 28160ce86c7SWei Liu */ 28260ce86c7SWei Liu err = v9fs_reopen_fid(pdu, f); 28360ce86c7SWei Liu if (err < 0) { 28460ce86c7SWei Liu f->ref--; 28560ce86c7SWei Liu return NULL; 28660ce86c7SWei Liu } 28760ce86c7SWei Liu /* 28860ce86c7SWei Liu * Mark the fid as referenced so that the LRU 28960ce86c7SWei Liu * reclaim won't close the file descriptor 29060ce86c7SWei Liu */ 29160ce86c7SWei Liu f->flags |= FID_REFERENCED; 29260ce86c7SWei Liu return f; 29360ce86c7SWei Liu } 29460ce86c7SWei Liu } 29560ce86c7SWei Liu return NULL; 29660ce86c7SWei Liu } 29760ce86c7SWei Liu 29860ce86c7SWei Liu static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid) 29960ce86c7SWei Liu { 30060ce86c7SWei Liu V9fsFidState *f; 30160ce86c7SWei Liu 302feabd6cfSGreg Kurz QSIMPLEQ_FOREACH(f, &s->fid_list, next) { 30360ce86c7SWei Liu /* If fid is already there return NULL */ 30460ce86c7SWei Liu BUG_ON(f->clunked); 30560ce86c7SWei Liu if (f->fid == fid) { 30660ce86c7SWei Liu return NULL; 30760ce86c7SWei Liu } 30860ce86c7SWei Liu } 30960ce86c7SWei Liu f = g_malloc0(sizeof(V9fsFidState)); 31060ce86c7SWei Liu f->fid = fid; 31160ce86c7SWei Liu f->fid_type = P9_FID_NONE; 31260ce86c7SWei Liu f->ref = 1; 31360ce86c7SWei Liu /* 31460ce86c7SWei Liu * Mark the fid as referenced so that the LRU 31560ce86c7SWei Liu * reclaim won't close the file descriptor 31660ce86c7SWei Liu */ 31760ce86c7SWei Liu f->flags |= FID_REFERENCED; 31820b7f45bSGreg Kurz QSIMPLEQ_INSERT_TAIL(&s->fid_list, f, next); 31960ce86c7SWei Liu 320d2c5cf7cSChristian Schoenebeck v9fs_readdir_init(s->proto_version, &f->fs.dir); 321d2c5cf7cSChristian Schoenebeck v9fs_readdir_init(s->proto_version, &f->fs_reclaim.dir); 3227cde47d4SGreg Kurz 32360ce86c7SWei Liu return f; 32460ce86c7SWei Liu } 32560ce86c7SWei Liu 3268440e22eSGreg Kurz static int coroutine_fn v9fs_xattr_fid_clunk(V9fsPDU *pdu, V9fsFidState *fidp) 32760ce86c7SWei Liu { 32860ce86c7SWei Liu int retval = 0; 32960ce86c7SWei Liu 330dd28fbbcSLi Qiang if (fidp->fs.xattr.xattrwalk_fid) { 33160ce86c7SWei Liu /* getxattr/listxattr fid */ 33260ce86c7SWei Liu goto free_value; 33360ce86c7SWei Liu } 33460ce86c7SWei Liu /* 33560ce86c7SWei Liu * if this is fid for setxattr. clunk should 33660ce86c7SWei Liu * result in setxattr localcall 33760ce86c7SWei Liu */ 33860ce86c7SWei Liu if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) { 33960ce86c7SWei Liu /* clunk after partial write */ 34060ce86c7SWei Liu retval = -EINVAL; 34160ce86c7SWei Liu goto free_out; 34260ce86c7SWei Liu } 34360ce86c7SWei Liu if (fidp->fs.xattr.len) { 34460ce86c7SWei Liu retval = v9fs_co_lsetxattr(pdu, &fidp->path, &fidp->fs.xattr.name, 34560ce86c7SWei Liu fidp->fs.xattr.value, 34660ce86c7SWei Liu fidp->fs.xattr.len, 34760ce86c7SWei Liu fidp->fs.xattr.flags); 34860ce86c7SWei Liu } else { 34960ce86c7SWei Liu retval = v9fs_co_lremovexattr(pdu, &fidp->path, &fidp->fs.xattr.name); 35060ce86c7SWei Liu } 35160ce86c7SWei Liu free_out: 35260ce86c7SWei Liu v9fs_string_free(&fidp->fs.xattr.name); 35360ce86c7SWei Liu free_value: 35460ce86c7SWei Liu g_free(fidp->fs.xattr.value); 35560ce86c7SWei Liu return retval; 35660ce86c7SWei Liu } 35760ce86c7SWei Liu 3588440e22eSGreg Kurz static int coroutine_fn free_fid(V9fsPDU *pdu, V9fsFidState *fidp) 35960ce86c7SWei Liu { 36060ce86c7SWei Liu int retval = 0; 36160ce86c7SWei Liu 36260ce86c7SWei Liu if (fidp->fid_type == P9_FID_FILE) { 36360ce86c7SWei Liu /* If we reclaimed the fd no need to close */ 36460ce86c7SWei Liu if (fidp->fs.fd != -1) { 36560ce86c7SWei Liu retval = v9fs_co_close(pdu, &fidp->fs); 36660ce86c7SWei Liu } 36760ce86c7SWei Liu } else if (fidp->fid_type == P9_FID_DIR) { 368f314ea4eSGreg Kurz if (fidp->fs.dir.stream != NULL) { 36960ce86c7SWei Liu retval = v9fs_co_closedir(pdu, &fidp->fs); 37060ce86c7SWei Liu } 37160ce86c7SWei Liu } else if (fidp->fid_type == P9_FID_XATTR) { 37260ce86c7SWei Liu retval = v9fs_xattr_fid_clunk(pdu, fidp); 37360ce86c7SWei Liu } 37460ce86c7SWei Liu v9fs_path_free(&fidp->path); 37560ce86c7SWei Liu g_free(fidp); 37660ce86c7SWei Liu return retval; 37760ce86c7SWei Liu } 37860ce86c7SWei Liu 3798440e22eSGreg Kurz static int coroutine_fn put_fid(V9fsPDU *pdu, V9fsFidState *fidp) 38060ce86c7SWei Liu { 38160ce86c7SWei Liu BUG_ON(!fidp->ref); 38260ce86c7SWei Liu fidp->ref--; 38360ce86c7SWei Liu /* 38460ce86c7SWei Liu * Don't free the fid if it is in reclaim list 38560ce86c7SWei Liu */ 38660ce86c7SWei Liu if (!fidp->ref && fidp->clunked) { 38760ce86c7SWei Liu if (fidp->fid == pdu->s->root_fid) { 38860ce86c7SWei Liu /* 38960ce86c7SWei Liu * if the clunked fid is root fid then we 39060ce86c7SWei Liu * have unmounted the fs on the client side. 39160ce86c7SWei Liu * delete the migration blocker. Ideally, this 39260ce86c7SWei Liu * should be hooked to transport close notification 39360ce86c7SWei Liu */ 39460ce86c7SWei Liu if (pdu->s->migration_blocker) { 39560ce86c7SWei Liu migrate_del_blocker(pdu->s->migration_blocker); 39660ce86c7SWei Liu error_free(pdu->s->migration_blocker); 39760ce86c7SWei Liu pdu->s->migration_blocker = NULL; 39860ce86c7SWei Liu } 39960ce86c7SWei Liu } 40060ce86c7SWei Liu return free_fid(pdu, fidp); 40160ce86c7SWei Liu } 40260ce86c7SWei Liu return 0; 40360ce86c7SWei Liu } 40460ce86c7SWei Liu 40560ce86c7SWei Liu static V9fsFidState *clunk_fid(V9fsState *s, int32_t fid) 40660ce86c7SWei Liu { 407feabd6cfSGreg Kurz V9fsFidState *fidp; 40860ce86c7SWei Liu 409feabd6cfSGreg Kurz QSIMPLEQ_FOREACH(fidp, &s->fid_list, next) { 410feabd6cfSGreg Kurz if (fidp->fid == fid) { 411feabd6cfSGreg Kurz QSIMPLEQ_REMOVE(&s->fid_list, fidp, V9fsFidState, next); 4122e53160fSGreg Kurz fidp->clunked = true; 41360ce86c7SWei Liu return fidp; 41460ce86c7SWei Liu } 415feabd6cfSGreg Kurz } 416feabd6cfSGreg Kurz return NULL; 417feabd6cfSGreg Kurz } 41860ce86c7SWei Liu 4198440e22eSGreg Kurz void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu) 42060ce86c7SWei Liu { 42160ce86c7SWei Liu int reclaim_count = 0; 42260ce86c7SWei Liu V9fsState *s = pdu->s; 42381f9766bSGreg Kurz V9fsFidState *f; 42481f9766bSGreg Kurz QSLIST_HEAD(, V9fsFidState) reclaim_list = 42581f9766bSGreg Kurz QSLIST_HEAD_INITIALIZER(reclaim_list); 42660ce86c7SWei Liu 427feabd6cfSGreg Kurz QSIMPLEQ_FOREACH(f, &s->fid_list, next) { 42860ce86c7SWei Liu /* 42960ce86c7SWei Liu * Unlink fids cannot be reclaimed. Check 43060ce86c7SWei Liu * for them and skip them. Also skip fids 43160ce86c7SWei Liu * currently being operated on. 43260ce86c7SWei Liu */ 43360ce86c7SWei Liu if (f->ref || f->flags & FID_NON_RECLAIMABLE) { 43460ce86c7SWei Liu continue; 43560ce86c7SWei Liu } 43660ce86c7SWei Liu /* 43760ce86c7SWei Liu * if it is a recently referenced fid 43860ce86c7SWei Liu * we leave the fid untouched and clear the 43960ce86c7SWei Liu * reference bit. We come back to it later 44060ce86c7SWei Liu * in the next iteration. (a simple LRU without 44160ce86c7SWei Liu * moving list elements around) 44260ce86c7SWei Liu */ 44360ce86c7SWei Liu if (f->flags & FID_REFERENCED) { 44460ce86c7SWei Liu f->flags &= ~FID_REFERENCED; 44560ce86c7SWei Liu continue; 44660ce86c7SWei Liu } 44760ce86c7SWei Liu /* 44860ce86c7SWei Liu * Add fids to reclaim list. 44960ce86c7SWei Liu */ 45060ce86c7SWei Liu if (f->fid_type == P9_FID_FILE) { 45160ce86c7SWei Liu if (f->fs.fd != -1) { 45260ce86c7SWei Liu /* 45360ce86c7SWei Liu * Up the reference count so that 45460ce86c7SWei Liu * a clunk request won't free this fid 45560ce86c7SWei Liu */ 45660ce86c7SWei Liu f->ref++; 45781f9766bSGreg Kurz QSLIST_INSERT_HEAD(&reclaim_list, f, reclaim_next); 45860ce86c7SWei Liu f->fs_reclaim.fd = f->fs.fd; 45960ce86c7SWei Liu f->fs.fd = -1; 46060ce86c7SWei Liu reclaim_count++; 46160ce86c7SWei Liu } 46260ce86c7SWei Liu } else if (f->fid_type == P9_FID_DIR) { 463f314ea4eSGreg Kurz if (f->fs.dir.stream != NULL) { 46460ce86c7SWei Liu /* 46560ce86c7SWei Liu * Up the reference count so that 46660ce86c7SWei Liu * a clunk request won't free this fid 46760ce86c7SWei Liu */ 46860ce86c7SWei Liu f->ref++; 46981f9766bSGreg Kurz QSLIST_INSERT_HEAD(&reclaim_list, f, reclaim_next); 470f314ea4eSGreg Kurz f->fs_reclaim.dir.stream = f->fs.dir.stream; 471f314ea4eSGreg Kurz f->fs.dir.stream = NULL; 47260ce86c7SWei Liu reclaim_count++; 47360ce86c7SWei Liu } 47460ce86c7SWei Liu } 47560ce86c7SWei Liu if (reclaim_count >= open_fd_rc) { 47660ce86c7SWei Liu break; 47760ce86c7SWei Liu } 47860ce86c7SWei Liu } 47960ce86c7SWei Liu /* 48060ce86c7SWei Liu * Now close the fid in reclaim list. Free them if they 48160ce86c7SWei Liu * are already clunked. 48260ce86c7SWei Liu */ 48381f9766bSGreg Kurz while (!QSLIST_EMPTY(&reclaim_list)) { 48481f9766bSGreg Kurz f = QSLIST_FIRST(&reclaim_list); 48581f9766bSGreg Kurz QSLIST_REMOVE(&reclaim_list, f, V9fsFidState, reclaim_next); 48660ce86c7SWei Liu if (f->fid_type == P9_FID_FILE) { 48760ce86c7SWei Liu v9fs_co_close(pdu, &f->fs_reclaim); 48860ce86c7SWei Liu } else if (f->fid_type == P9_FID_DIR) { 48960ce86c7SWei Liu v9fs_co_closedir(pdu, &f->fs_reclaim); 49060ce86c7SWei Liu } 49160ce86c7SWei Liu /* 49260ce86c7SWei Liu * Now drop the fid reference, free it 49360ce86c7SWei Liu * if clunked. 49460ce86c7SWei Liu */ 49560ce86c7SWei Liu put_fid(pdu, f); 49660ce86c7SWei Liu } 49760ce86c7SWei Liu } 49860ce86c7SWei Liu 4998440e22eSGreg Kurz static int coroutine_fn v9fs_mark_fids_unreclaim(V9fsPDU *pdu, V9fsPath *path) 50060ce86c7SWei Liu { 50160ce86c7SWei Liu int err; 50260ce86c7SWei Liu V9fsState *s = pdu->s; 50320b7f45bSGreg Kurz V9fsFidState *fidp, *fidp_next; 50460ce86c7SWei Liu 50520b7f45bSGreg Kurz fidp = QSIMPLEQ_FIRST(&s->fid_list); 50620b7f45bSGreg Kurz if (!fidp) { 50720b7f45bSGreg Kurz return 0; 50860ce86c7SWei Liu } 50920b7f45bSGreg Kurz 51020b7f45bSGreg Kurz /* 51120b7f45bSGreg Kurz * v9fs_reopen_fid() can yield : a reference on the fid must be held 51220b7f45bSGreg Kurz * to ensure its pointer remains valid and we can safely pass it to 51320b7f45bSGreg Kurz * QSIMPLEQ_NEXT(). The corresponding put_fid() can also yield so 51420b7f45bSGreg Kurz * we must keep a reference on the next fid as well. So the logic here 51520b7f45bSGreg Kurz * is to get a reference on a fid and only put it back during the next 51620b7f45bSGreg Kurz * iteration after we could get a reference on the next fid. Start with 51720b7f45bSGreg Kurz * the first one. 51820b7f45bSGreg Kurz */ 51920b7f45bSGreg Kurz for (fidp->ref++; fidp; fidp = fidp_next) { 52020b7f45bSGreg Kurz if (fidp->path.size == path->size && 52120b7f45bSGreg Kurz !memcmp(fidp->path.data, path->data, path->size)) { 52260ce86c7SWei Liu /* Mark the fid non reclaimable. */ 52360ce86c7SWei Liu fidp->flags |= FID_NON_RECLAIMABLE; 52460ce86c7SWei Liu 52560ce86c7SWei Liu /* reopen the file/dir if already closed */ 52660ce86c7SWei Liu err = v9fs_reopen_fid(pdu, fidp); 52760ce86c7SWei Liu if (err < 0) { 52820b7f45bSGreg Kurz put_fid(pdu, fidp); 529267fcadfSGreg Kurz return err; 53060ce86c7SWei Liu } 53120b7f45bSGreg Kurz } 53220b7f45bSGreg Kurz 53320b7f45bSGreg Kurz fidp_next = QSIMPLEQ_NEXT(fidp, next); 53420b7f45bSGreg Kurz 53520b7f45bSGreg Kurz if (fidp_next) { 53660ce86c7SWei Liu /* 53720b7f45bSGreg Kurz * Ensure the next fid survives a potential clunk request during 53820b7f45bSGreg Kurz * put_fid() below and v9fs_reopen_fid() in the next iteration. 53960ce86c7SWei Liu */ 54020b7f45bSGreg Kurz fidp_next->ref++; 54160ce86c7SWei Liu } 54220b7f45bSGreg Kurz 54320b7f45bSGreg Kurz /* We're done with this fid */ 54420b7f45bSGreg Kurz put_fid(pdu, fidp); 54560ce86c7SWei Liu } 54620b7f45bSGreg Kurz 54760ce86c7SWei Liu return 0; 54860ce86c7SWei Liu } 54960ce86c7SWei Liu 5508440e22eSGreg Kurz static void coroutine_fn virtfs_reset(V9fsPDU *pdu) 55160ce86c7SWei Liu { 55260ce86c7SWei Liu V9fsState *s = pdu->s; 55379decce3SGreg Kurz V9fsFidState *fidp; 55460ce86c7SWei Liu 55560ce86c7SWei Liu /* Free all fids */ 556feabd6cfSGreg Kurz while (!QSIMPLEQ_EMPTY(&s->fid_list)) { 5576d54af0eSGreg Kurz /* Get fid */ 558feabd6cfSGreg Kurz fidp = QSIMPLEQ_FIRST(&s->fid_list); 5596d54af0eSGreg Kurz fidp->ref++; 56060ce86c7SWei Liu 5616d54af0eSGreg Kurz /* Clunk fid */ 562feabd6cfSGreg Kurz QSIMPLEQ_REMOVE(&s->fid_list, fidp, V9fsFidState, next); 5632e53160fSGreg Kurz fidp->clunked = true; 5646d54af0eSGreg Kurz 5656d54af0eSGreg Kurz put_fid(pdu, fidp); 56660ce86c7SWei Liu } 56760ce86c7SWei Liu } 56860ce86c7SWei Liu 56960ce86c7SWei Liu #define P9_QID_TYPE_DIR 0x80 57060ce86c7SWei Liu #define P9_QID_TYPE_SYMLINK 0x02 57160ce86c7SWei Liu 57260ce86c7SWei Liu #define P9_STAT_MODE_DIR 0x80000000 57360ce86c7SWei Liu #define P9_STAT_MODE_APPEND 0x40000000 57460ce86c7SWei Liu #define P9_STAT_MODE_EXCL 0x20000000 57560ce86c7SWei Liu #define P9_STAT_MODE_MOUNT 0x10000000 57660ce86c7SWei Liu #define P9_STAT_MODE_AUTH 0x08000000 57760ce86c7SWei Liu #define P9_STAT_MODE_TMP 0x04000000 57860ce86c7SWei Liu #define P9_STAT_MODE_SYMLINK 0x02000000 57960ce86c7SWei Liu #define P9_STAT_MODE_LINK 0x01000000 58060ce86c7SWei Liu #define P9_STAT_MODE_DEVICE 0x00800000 58160ce86c7SWei Liu #define P9_STAT_MODE_NAMED_PIPE 0x00200000 58260ce86c7SWei Liu #define P9_STAT_MODE_SOCKET 0x00100000 58360ce86c7SWei Liu #define P9_STAT_MODE_SETUID 0x00080000 58460ce86c7SWei Liu #define P9_STAT_MODE_SETGID 0x00040000 58560ce86c7SWei Liu #define P9_STAT_MODE_SETVTX 0x00010000 58660ce86c7SWei Liu 58760ce86c7SWei Liu #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \ 58860ce86c7SWei Liu P9_STAT_MODE_SYMLINK | \ 58960ce86c7SWei Liu P9_STAT_MODE_LINK | \ 59060ce86c7SWei Liu P9_STAT_MODE_DEVICE | \ 59160ce86c7SWei Liu P9_STAT_MODE_NAMED_PIPE | \ 59260ce86c7SWei Liu P9_STAT_MODE_SOCKET) 59360ce86c7SWei Liu 5946b6aa828SChristian Schoenebeck /* Mirrors all bits of a byte. So e.g. binary 10100000 would become 00000101. */ 5956b6aa828SChristian Schoenebeck static inline uint8_t mirror8bit(uint8_t byte) 5966b6aa828SChristian Schoenebeck { 5976b6aa828SChristian Schoenebeck return (byte * 0x0202020202ULL & 0x010884422010ULL) % 1023; 5986b6aa828SChristian Schoenebeck } 5996b6aa828SChristian Schoenebeck 6006b6aa828SChristian Schoenebeck /* Same as mirror8bit() just for a 64 bit data type instead for a byte. */ 6016b6aa828SChristian Schoenebeck static inline uint64_t mirror64bit(uint64_t value) 6026b6aa828SChristian Schoenebeck { 6036b6aa828SChristian Schoenebeck return ((uint64_t)mirror8bit(value & 0xff) << 56) | 6046b6aa828SChristian Schoenebeck ((uint64_t)mirror8bit((value >> 8) & 0xff) << 48) | 6056b6aa828SChristian Schoenebeck ((uint64_t)mirror8bit((value >> 16) & 0xff) << 40) | 6066b6aa828SChristian Schoenebeck ((uint64_t)mirror8bit((value >> 24) & 0xff) << 32) | 6076b6aa828SChristian Schoenebeck ((uint64_t)mirror8bit((value >> 32) & 0xff) << 24) | 6086b6aa828SChristian Schoenebeck ((uint64_t)mirror8bit((value >> 40) & 0xff) << 16) | 6096b6aa828SChristian Schoenebeck ((uint64_t)mirror8bit((value >> 48) & 0xff) << 8) | 6106b6aa828SChristian Schoenebeck ((uint64_t)mirror8bit((value >> 56) & 0xff)); 6116b6aa828SChristian Schoenebeck } 6126b6aa828SChristian Schoenebeck 6136b6aa828SChristian Schoenebeck /** 6146b6aa828SChristian Schoenebeck * @brief Parameter k for the Exponential Golomb algorihm to be used. 6156b6aa828SChristian Schoenebeck * 6166b6aa828SChristian Schoenebeck * The smaller this value, the smaller the minimum bit count for the Exp. 6176b6aa828SChristian Schoenebeck * Golomb generated affixes will be (at lowest index) however for the 6186b6aa828SChristian Schoenebeck * price of having higher maximum bit count of generated affixes (at highest 6196b6aa828SChristian Schoenebeck * index). Likewise increasing this parameter yields in smaller maximum bit 6206b6aa828SChristian Schoenebeck * count for the price of having higher minimum bit count. 6216b6aa828SChristian Schoenebeck * 6226b6aa828SChristian Schoenebeck * In practice that means: a good value for k depends on the expected amount 6236b6aa828SChristian Schoenebeck * of devices to be exposed by one export. For a small amount of devices k 6246b6aa828SChristian Schoenebeck * should be small, for a large amount of devices k might be increased 6256b6aa828SChristian Schoenebeck * instead. The default of k=0 should be fine for most users though. 6266b6aa828SChristian Schoenebeck * 6276b6aa828SChristian Schoenebeck * @b IMPORTANT: In case this ever becomes a runtime parameter; the value of 6286b6aa828SChristian Schoenebeck * k should not change as long as guest is still running! Because that would 6296b6aa828SChristian Schoenebeck * cause completely different inode numbers to be generated on guest. 6306b6aa828SChristian Schoenebeck */ 6316b6aa828SChristian Schoenebeck #define EXP_GOLOMB_K 0 6326b6aa828SChristian Schoenebeck 6336b6aa828SChristian Schoenebeck /** 6346b6aa828SChristian Schoenebeck * @brief Exponential Golomb algorithm for arbitrary k (including k=0). 6356b6aa828SChristian Schoenebeck * 6366b6aa828SChristian Schoenebeck * The Exponential Golomb algorithm generates @b prefixes (@b not suffixes!) 6376b6aa828SChristian Schoenebeck * with growing length and with the mathematical property of being 6386b6aa828SChristian Schoenebeck * "prefix-free". The latter means the generated prefixes can be prepended 6396b6aa828SChristian Schoenebeck * in front of arbitrary numbers and the resulting concatenated numbers are 6406b6aa828SChristian Schoenebeck * guaranteed to be always unique. 6416b6aa828SChristian Schoenebeck * 6426b6aa828SChristian Schoenebeck * This is a minor adjustment to the original Exp. Golomb algorithm in the 6436b6aa828SChristian Schoenebeck * sense that lowest allowed index (@param n) starts with 1, not with zero. 6446b6aa828SChristian Schoenebeck * 6456b6aa828SChristian Schoenebeck * @param n - natural number (or index) of the prefix to be generated 6466b6aa828SChristian Schoenebeck * (1, 2, 3, ...) 6476b6aa828SChristian Schoenebeck * @param k - parameter k of Exp. Golomb algorithm to be used 6486b6aa828SChristian Schoenebeck * (see comment on EXP_GOLOMB_K macro for details about k) 6496b6aa828SChristian Schoenebeck */ 6506b6aa828SChristian Schoenebeck static VariLenAffix expGolombEncode(uint64_t n, int k) 6516b6aa828SChristian Schoenebeck { 6526b6aa828SChristian Schoenebeck const uint64_t value = n + (1 << k) - 1; 6536b6aa828SChristian Schoenebeck const int bits = (int) log2(value) + 1; 6546b6aa828SChristian Schoenebeck return (VariLenAffix) { 6556b6aa828SChristian Schoenebeck .type = AffixType_Prefix, 6566b6aa828SChristian Schoenebeck .value = value, 6576b6aa828SChristian Schoenebeck .bits = bits + MAX((bits - 1 - k), 0) 6586b6aa828SChristian Schoenebeck }; 6596b6aa828SChristian Schoenebeck } 6606b6aa828SChristian Schoenebeck 6616b6aa828SChristian Schoenebeck /** 6626b6aa828SChristian Schoenebeck * @brief Converts a suffix into a prefix, or a prefix into a suffix. 6636b6aa828SChristian Schoenebeck * 6646b6aa828SChristian Schoenebeck * Simply mirror all bits of the affix value, for the purpose to preserve 6656b6aa828SChristian Schoenebeck * respectively the mathematical "prefix-free" or "suffix-free" property 6666b6aa828SChristian Schoenebeck * after the conversion. 6676b6aa828SChristian Schoenebeck * 6686b6aa828SChristian Schoenebeck * If a passed prefix is suitable to create unique numbers, then the 6696b6aa828SChristian Schoenebeck * returned suffix is suitable to create unique numbers as well (and vice 6706b6aa828SChristian Schoenebeck * versa). 6716b6aa828SChristian Schoenebeck */ 6726b6aa828SChristian Schoenebeck static VariLenAffix invertAffix(const VariLenAffix *affix) 6736b6aa828SChristian Schoenebeck { 6746b6aa828SChristian Schoenebeck return (VariLenAffix) { 6756b6aa828SChristian Schoenebeck .type = 6766b6aa828SChristian Schoenebeck (affix->type == AffixType_Suffix) ? 6776b6aa828SChristian Schoenebeck AffixType_Prefix : AffixType_Suffix, 6786b6aa828SChristian Schoenebeck .value = 6796b6aa828SChristian Schoenebeck mirror64bit(affix->value) >> 6806b6aa828SChristian Schoenebeck ((sizeof(affix->value) * 8) - affix->bits), 6816b6aa828SChristian Schoenebeck .bits = affix->bits 6826b6aa828SChristian Schoenebeck }; 6836b6aa828SChristian Schoenebeck } 6846b6aa828SChristian Schoenebeck 6856b6aa828SChristian Schoenebeck /** 6866b6aa828SChristian Schoenebeck * @brief Generates suffix numbers with "suffix-free" property. 6876b6aa828SChristian Schoenebeck * 6886b6aa828SChristian Schoenebeck * This is just a wrapper function on top of the Exp. Golomb algorithm. 6896b6aa828SChristian Schoenebeck * 6906b6aa828SChristian Schoenebeck * Since the Exp. Golomb algorithm generates prefixes, but we need suffixes, 6916b6aa828SChristian Schoenebeck * this function converts the Exp. Golomb prefixes into appropriate suffixes 6926b6aa828SChristian Schoenebeck * which are still suitable for generating unique numbers. 6936b6aa828SChristian Schoenebeck * 6946b6aa828SChristian Schoenebeck * @param n - natural number (or index) of the suffix to be generated 6956b6aa828SChristian Schoenebeck * (1, 2, 3, ...) 6966b6aa828SChristian Schoenebeck */ 6976b6aa828SChristian Schoenebeck static VariLenAffix affixForIndex(uint64_t index) 6986b6aa828SChristian Schoenebeck { 6996b6aa828SChristian Schoenebeck VariLenAffix prefix; 7006b6aa828SChristian Schoenebeck prefix = expGolombEncode(index, EXP_GOLOMB_K); 7016b6aa828SChristian Schoenebeck return invertAffix(&prefix); /* convert prefix to suffix */ 7026b6aa828SChristian Schoenebeck } 7036b6aa828SChristian Schoenebeck 7041a6ed33cSAntonios Motakis /* creative abuse of tb_hash_func7, which is based on xxhash */ 7051a6ed33cSAntonios Motakis static uint32_t qpp_hash(QppEntry e) 7061a6ed33cSAntonios Motakis { 7071a6ed33cSAntonios Motakis return qemu_xxhash7(e.ino_prefix, e.dev, 0, 0, 0); 7081a6ed33cSAntonios Motakis } 7091a6ed33cSAntonios Motakis 710f3fe4a2dSAntonios Motakis static uint32_t qpf_hash(QpfEntry e) 711f3fe4a2dSAntonios Motakis { 712f3fe4a2dSAntonios Motakis return qemu_xxhash7(e.ino, e.dev, 0, 0, 0); 713f3fe4a2dSAntonios Motakis } 714f3fe4a2dSAntonios Motakis 7156b6aa828SChristian Schoenebeck static bool qpd_cmp_func(const void *obj, const void *userp) 7166b6aa828SChristian Schoenebeck { 7176b6aa828SChristian Schoenebeck const QpdEntry *e1 = obj, *e2 = userp; 7186b6aa828SChristian Schoenebeck return e1->dev == e2->dev; 7196b6aa828SChristian Schoenebeck } 7206b6aa828SChristian Schoenebeck 7216b6aa828SChristian Schoenebeck static bool qpp_cmp_func(const void *obj, const void *userp) 7221a6ed33cSAntonios Motakis { 7231a6ed33cSAntonios Motakis const QppEntry *e1 = obj, *e2 = userp; 7241a6ed33cSAntonios Motakis return e1->dev == e2->dev && e1->ino_prefix == e2->ino_prefix; 7251a6ed33cSAntonios Motakis } 7261a6ed33cSAntonios Motakis 7276b6aa828SChristian Schoenebeck static bool qpf_cmp_func(const void *obj, const void *userp) 728f3fe4a2dSAntonios Motakis { 729f3fe4a2dSAntonios Motakis const QpfEntry *e1 = obj, *e2 = userp; 730f3fe4a2dSAntonios Motakis return e1->dev == e2->dev && e1->ino == e2->ino; 731f3fe4a2dSAntonios Motakis } 732f3fe4a2dSAntonios Motakis 733f3fe4a2dSAntonios Motakis static void qp_table_remove(void *p, uint32_t h, void *up) 7341a6ed33cSAntonios Motakis { 7351a6ed33cSAntonios Motakis g_free(p); 7361a6ed33cSAntonios Motakis } 7371a6ed33cSAntonios Motakis 738f3fe4a2dSAntonios Motakis static void qp_table_destroy(struct qht *ht) 7391a6ed33cSAntonios Motakis { 7401a6ed33cSAntonios Motakis if (!ht || !ht->map) { 7411a6ed33cSAntonios Motakis return; 7421a6ed33cSAntonios Motakis } 743f3fe4a2dSAntonios Motakis qht_iter(ht, qp_table_remove, NULL); 7441a6ed33cSAntonios Motakis qht_destroy(ht); 7451a6ed33cSAntonios Motakis } 7461a6ed33cSAntonios Motakis 7476b6aa828SChristian Schoenebeck static void qpd_table_init(struct qht *ht) 7486b6aa828SChristian Schoenebeck { 7496b6aa828SChristian Schoenebeck qht_init(ht, qpd_cmp_func, 1, QHT_MODE_AUTO_RESIZE); 7506b6aa828SChristian Schoenebeck } 7516b6aa828SChristian Schoenebeck 7521a6ed33cSAntonios Motakis static void qpp_table_init(struct qht *ht) 7531a6ed33cSAntonios Motakis { 7546b6aa828SChristian Schoenebeck qht_init(ht, qpp_cmp_func, 1, QHT_MODE_AUTO_RESIZE); 7551a6ed33cSAntonios Motakis } 7561a6ed33cSAntonios Motakis 757f3fe4a2dSAntonios Motakis static void qpf_table_init(struct qht *ht) 758f3fe4a2dSAntonios Motakis { 7596b6aa828SChristian Schoenebeck qht_init(ht, qpf_cmp_func, 1 << 16, QHT_MODE_AUTO_RESIZE); 760f3fe4a2dSAntonios Motakis } 761f3fe4a2dSAntonios Motakis 7626b6aa828SChristian Schoenebeck /* 7636b6aa828SChristian Schoenebeck * Returns how many (high end) bits of inode numbers of the passed fs 7646b6aa828SChristian Schoenebeck * device shall be used (in combination with the device number) to 7656b6aa828SChristian Schoenebeck * generate hash values for qpp_table entries. 7666b6aa828SChristian Schoenebeck * 7676b6aa828SChristian Schoenebeck * This function is required if variable length suffixes are used for inode 7686b6aa828SChristian Schoenebeck * number mapping on guest level. Since a device may end up having multiple 7696b6aa828SChristian Schoenebeck * entries in qpp_table, each entry most probably with a different suffix 7706b6aa828SChristian Schoenebeck * length, we thus need this function in conjunction with qpd_table to 7716b6aa828SChristian Schoenebeck * "agree" about a fix amount of bits (per device) to be always used for 7726b6aa828SChristian Schoenebeck * generating hash values for the purpose of accessing qpp_table in order 7736b6aa828SChristian Schoenebeck * get consistent behaviour when accessing qpp_table. 7746b6aa828SChristian Schoenebeck */ 7756b6aa828SChristian Schoenebeck static int qid_inode_prefix_hash_bits(V9fsPDU *pdu, dev_t dev) 7766b6aa828SChristian Schoenebeck { 7776b6aa828SChristian Schoenebeck QpdEntry lookup = { 7786b6aa828SChristian Schoenebeck .dev = dev 7796b6aa828SChristian Schoenebeck }, *val; 7806b6aa828SChristian Schoenebeck uint32_t hash = dev; 7816b6aa828SChristian Schoenebeck VariLenAffix affix; 7826b6aa828SChristian Schoenebeck 7836b6aa828SChristian Schoenebeck val = qht_lookup(&pdu->s->qpd_table, &lookup, hash); 7846b6aa828SChristian Schoenebeck if (!val) { 7856b6aa828SChristian Schoenebeck val = g_malloc0(sizeof(QpdEntry)); 7866b6aa828SChristian Schoenebeck *val = lookup; 7876b6aa828SChristian Schoenebeck affix = affixForIndex(pdu->s->qp_affix_next); 7886b6aa828SChristian Schoenebeck val->prefix_bits = affix.bits; 7896b6aa828SChristian Schoenebeck qht_insert(&pdu->s->qpd_table, val, hash, NULL); 7906b6aa828SChristian Schoenebeck pdu->s->qp_ndevices++; 7916b6aa828SChristian Schoenebeck } 7926b6aa828SChristian Schoenebeck return val->prefix_bits; 7936b6aa828SChristian Schoenebeck } 7946b6aa828SChristian Schoenebeck 7956b6aa828SChristian Schoenebeck /** 7966b6aa828SChristian Schoenebeck * @brief Slow / full mapping host inode nr -> guest inode nr. 7976b6aa828SChristian Schoenebeck * 7986b6aa828SChristian Schoenebeck * This function performs a slower and much more costly remapping of an 7996b6aa828SChristian Schoenebeck * original file inode number on host to an appropriate different inode 8006b6aa828SChristian Schoenebeck * number on guest. For every (dev, inode) combination on host a new 8016b6aa828SChristian Schoenebeck * sequential number is generated, cached and exposed as inode number on 8026b6aa828SChristian Schoenebeck * guest. 8036b6aa828SChristian Schoenebeck * 8046b6aa828SChristian Schoenebeck * This is just a "last resort" fallback solution if the much faster/cheaper 8056b6aa828SChristian Schoenebeck * qid_path_suffixmap() failed. In practice this slow / full mapping is not 8066b6aa828SChristian Schoenebeck * expected ever to be used at all though. 8076b6aa828SChristian Schoenebeck * 8086b6aa828SChristian Schoenebeck * @see qid_path_suffixmap() for details 8096b6aa828SChristian Schoenebeck * 8106b6aa828SChristian Schoenebeck */ 811f3fe4a2dSAntonios Motakis static int qid_path_fullmap(V9fsPDU *pdu, const struct stat *stbuf, 812f3fe4a2dSAntonios Motakis uint64_t *path) 813f3fe4a2dSAntonios Motakis { 814f3fe4a2dSAntonios Motakis QpfEntry lookup = { 815f3fe4a2dSAntonios Motakis .dev = stbuf->st_dev, 816f3fe4a2dSAntonios Motakis .ino = stbuf->st_ino 817f3fe4a2dSAntonios Motakis }, *val; 818f3fe4a2dSAntonios Motakis uint32_t hash = qpf_hash(lookup); 8196b6aa828SChristian Schoenebeck VariLenAffix affix; 820f3fe4a2dSAntonios Motakis 821f3fe4a2dSAntonios Motakis val = qht_lookup(&pdu->s->qpf_table, &lookup, hash); 822f3fe4a2dSAntonios Motakis 823f3fe4a2dSAntonios Motakis if (!val) { 824f3fe4a2dSAntonios Motakis if (pdu->s->qp_fullpath_next == 0) { 825f3fe4a2dSAntonios Motakis /* no more files can be mapped :'( */ 826f3fe4a2dSAntonios Motakis error_report_once( 827f3fe4a2dSAntonios Motakis "9p: No more prefixes available for remapping inodes from " 828f3fe4a2dSAntonios Motakis "host to guest." 829f3fe4a2dSAntonios Motakis ); 830f3fe4a2dSAntonios Motakis return -ENFILE; 831f3fe4a2dSAntonios Motakis } 832f3fe4a2dSAntonios Motakis 833f3fe4a2dSAntonios Motakis val = g_malloc0(sizeof(QppEntry)); 834f3fe4a2dSAntonios Motakis *val = lookup; 835f3fe4a2dSAntonios Motakis 836f3fe4a2dSAntonios Motakis /* new unique inode and device combo */ 8376b6aa828SChristian Schoenebeck affix = affixForIndex( 8386b6aa828SChristian Schoenebeck 1ULL << (sizeof(pdu->s->qp_affix_next) * 8) 8396b6aa828SChristian Schoenebeck ); 8406b6aa828SChristian Schoenebeck val->path = (pdu->s->qp_fullpath_next++ << affix.bits) | affix.value; 8416b6aa828SChristian Schoenebeck pdu->s->qp_fullpath_next &= ((1ULL << (64 - affix.bits)) - 1); 842f3fe4a2dSAntonios Motakis qht_insert(&pdu->s->qpf_table, val, hash, NULL); 843f3fe4a2dSAntonios Motakis } 844f3fe4a2dSAntonios Motakis 845f3fe4a2dSAntonios Motakis *path = val->path; 846f3fe4a2dSAntonios Motakis return 0; 847f3fe4a2dSAntonios Motakis } 848f3fe4a2dSAntonios Motakis 8496b6aa828SChristian Schoenebeck /** 8506b6aa828SChristian Schoenebeck * @brief Quick mapping host inode nr -> guest inode nr. 8511a6ed33cSAntonios Motakis * 8526b6aa828SChristian Schoenebeck * This function performs quick remapping of an original file inode number 8536b6aa828SChristian Schoenebeck * on host to an appropriate different inode number on guest. This remapping 8546b6aa828SChristian Schoenebeck * of inodes is required to avoid inode nr collisions on guest which would 8556b6aa828SChristian Schoenebeck * happen if the 9p export contains more than 1 exported file system (or 8566b6aa828SChristian Schoenebeck * more than 1 file system data set), because unlike on host level where the 8576b6aa828SChristian Schoenebeck * files would have different device nrs, all files exported by 9p would 8586b6aa828SChristian Schoenebeck * share the same device nr on guest (the device nr of the virtual 9p device 8596b6aa828SChristian Schoenebeck * that is). 8606b6aa828SChristian Schoenebeck * 8616b6aa828SChristian Schoenebeck * Inode remapping is performed by chopping off high end bits of the original 8626b6aa828SChristian Schoenebeck * inode number from host, shifting the result upwards and then assigning a 8636b6aa828SChristian Schoenebeck * generated suffix number for the low end bits, where the same suffix number 8646b6aa828SChristian Schoenebeck * will be shared by all inodes with the same device id AND the same high end 8656b6aa828SChristian Schoenebeck * bits that have been chopped off. That approach utilizes the fact that inode 8666b6aa828SChristian Schoenebeck * numbers very likely share the same high end bits (i.e. due to their common 8676b6aa828SChristian Schoenebeck * sequential generation by file systems) and hence we only have to generate 8686b6aa828SChristian Schoenebeck * and track a very limited amount of suffixes in practice due to that. 8696b6aa828SChristian Schoenebeck * 8706b6aa828SChristian Schoenebeck * We generate variable size suffixes for that purpose. The 1st generated 8716b6aa828SChristian Schoenebeck * suffix will only have 1 bit and hence we only need to chop off 1 bit from 8726b6aa828SChristian Schoenebeck * the original inode number. The subsequent suffixes being generated will 8736b6aa828SChristian Schoenebeck * grow in (bit) size subsequently, i.e. the 2nd and 3rd suffix being 8746b6aa828SChristian Schoenebeck * generated will have 3 bits and hence we have to chop off 3 bits from their 8756b6aa828SChristian Schoenebeck * original inodes, and so on. That approach of using variable length suffixes 8766b6aa828SChristian Schoenebeck * (i.e. over fixed size ones) utilizes the fact that in practice only a very 8776b6aa828SChristian Schoenebeck * limited amount of devices are shared by the same export (e.g. typically 8786b6aa828SChristian Schoenebeck * less than 2 dozen devices per 9p export), so in practice we need to chop 8796b6aa828SChristian Schoenebeck * off less bits than with fixed size prefixes and yet are flexible to add 8806b6aa828SChristian Schoenebeck * new devices at runtime below host's export directory at any time without 8816b6aa828SChristian Schoenebeck * having to reboot guest nor requiring to reconfigure guest for that. And due 8826b6aa828SChristian Schoenebeck * to the very limited amount of original high end bits that we chop off that 8836b6aa828SChristian Schoenebeck * way, the total amount of suffixes we need to generate is less than by using 8846b6aa828SChristian Schoenebeck * fixed size prefixes and hence it also improves performance of the inode 8856b6aa828SChristian Schoenebeck * remapping algorithm, and finally has the nice side effect that the inode 8866b6aa828SChristian Schoenebeck * numbers on guest will be much smaller & human friendly. ;-) 8871a6ed33cSAntonios Motakis */ 8886b6aa828SChristian Schoenebeck static int qid_path_suffixmap(V9fsPDU *pdu, const struct stat *stbuf, 8891a6ed33cSAntonios Motakis uint64_t *path) 8901a6ed33cSAntonios Motakis { 8916b6aa828SChristian Schoenebeck const int ino_hash_bits = qid_inode_prefix_hash_bits(pdu, stbuf->st_dev); 8921a6ed33cSAntonios Motakis QppEntry lookup = { 8931a6ed33cSAntonios Motakis .dev = stbuf->st_dev, 8946b6aa828SChristian Schoenebeck .ino_prefix = (uint16_t) (stbuf->st_ino >> (64 - ino_hash_bits)) 8951a6ed33cSAntonios Motakis }, *val; 8961a6ed33cSAntonios Motakis uint32_t hash = qpp_hash(lookup); 8971a6ed33cSAntonios Motakis 8981a6ed33cSAntonios Motakis val = qht_lookup(&pdu->s->qpp_table, &lookup, hash); 8991a6ed33cSAntonios Motakis 9001a6ed33cSAntonios Motakis if (!val) { 9016b6aa828SChristian Schoenebeck if (pdu->s->qp_affix_next == 0) { 9026b6aa828SChristian Schoenebeck /* we ran out of affixes */ 903f3fe4a2dSAntonios Motakis warn_report_once( 904f3fe4a2dSAntonios Motakis "9p: Potential degraded performance of inode remapping" 9051a6ed33cSAntonios Motakis ); 9061a6ed33cSAntonios Motakis return -ENFILE; 9071a6ed33cSAntonios Motakis } 9081a6ed33cSAntonios Motakis 9091a6ed33cSAntonios Motakis val = g_malloc0(sizeof(QppEntry)); 9101a6ed33cSAntonios Motakis *val = lookup; 9111a6ed33cSAntonios Motakis 9126b6aa828SChristian Schoenebeck /* new unique inode affix and device combo */ 9136b6aa828SChristian Schoenebeck val->qp_affix_index = pdu->s->qp_affix_next++; 9146b6aa828SChristian Schoenebeck val->qp_affix = affixForIndex(val->qp_affix_index); 9151a6ed33cSAntonios Motakis qht_insert(&pdu->s->qpp_table, val, hash, NULL); 9161a6ed33cSAntonios Motakis } 9176b6aa828SChristian Schoenebeck /* assuming generated affix to be suffix type, not prefix */ 9186b6aa828SChristian Schoenebeck *path = (stbuf->st_ino << val->qp_affix.bits) | val->qp_affix.value; 9191a6ed33cSAntonios Motakis return 0; 9201a6ed33cSAntonios Motakis } 9211a6ed33cSAntonios Motakis 9223b5ee9e8SAntonios Motakis static int stat_to_qid(V9fsPDU *pdu, const struct stat *stbuf, V9fsQID *qidp) 92360ce86c7SWei Liu { 9241a6ed33cSAntonios Motakis int err; 92560ce86c7SWei Liu size_t size; 92660ce86c7SWei Liu 9271a6ed33cSAntonios Motakis if (pdu->s->ctx.export_flags & V9FS_REMAP_INODES) { 9281a6ed33cSAntonios Motakis /* map inode+device to qid path (fast path) */ 9296b6aa828SChristian Schoenebeck err = qid_path_suffixmap(pdu, stbuf, &qidp->path); 930f3fe4a2dSAntonios Motakis if (err == -ENFILE) { 931f3fe4a2dSAntonios Motakis /* fast path didn't work, fall back to full map */ 932f3fe4a2dSAntonios Motakis err = qid_path_fullmap(pdu, stbuf, &qidp->path); 933f3fe4a2dSAntonios Motakis } 9341a6ed33cSAntonios Motakis if (err) { 9351a6ed33cSAntonios Motakis return err; 9361a6ed33cSAntonios Motakis } 9371a6ed33cSAntonios Motakis } else { 9383b5ee9e8SAntonios Motakis if (pdu->s->dev_id != stbuf->st_dev) { 9391a6ed33cSAntonios Motakis if (pdu->s->ctx.export_flags & V9FS_FORBID_MULTIDEVS) { 9401a6ed33cSAntonios Motakis error_report_once( 9411a6ed33cSAntonios Motakis "9p: Multiple devices detected in same VirtFS export. " 9421a6ed33cSAntonios Motakis "Access of guest to additional devices is (partly) " 9431a6ed33cSAntonios Motakis "denied due to virtfs option 'multidevs=forbid' being " 9441a6ed33cSAntonios Motakis "effective." 9451a6ed33cSAntonios Motakis ); 9461a6ed33cSAntonios Motakis return -ENODEV; 9471a6ed33cSAntonios Motakis } else { 9483b5ee9e8SAntonios Motakis warn_report_once( 9493b5ee9e8SAntonios Motakis "9p: Multiple devices detected in same VirtFS export, " 9503b5ee9e8SAntonios Motakis "which might lead to file ID collisions and severe " 9511a6ed33cSAntonios Motakis "misbehaviours on guest! You should either use a " 9521a6ed33cSAntonios Motakis "separate export for each device shared from host or " 9531a6ed33cSAntonios Motakis "use virtfs option 'multidevs=remap'!" 9543b5ee9e8SAntonios Motakis ); 9553b5ee9e8SAntonios Motakis } 9561a6ed33cSAntonios Motakis } 95760ce86c7SWei Liu memset(&qidp->path, 0, sizeof(qidp->path)); 95860ce86c7SWei Liu size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path)); 95960ce86c7SWei Liu memcpy(&qidp->path, &stbuf->st_ino, size); 9601a6ed33cSAntonios Motakis } 9611a6ed33cSAntonios Motakis 96260ce86c7SWei Liu qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8); 96360ce86c7SWei Liu qidp->type = 0; 96460ce86c7SWei Liu if (S_ISDIR(stbuf->st_mode)) { 96560ce86c7SWei Liu qidp->type |= P9_QID_TYPE_DIR; 96660ce86c7SWei Liu } 96760ce86c7SWei Liu if (S_ISLNK(stbuf->st_mode)) { 96860ce86c7SWei Liu qidp->type |= P9_QID_TYPE_SYMLINK; 96960ce86c7SWei Liu } 9703b5ee9e8SAntonios Motakis 9713b5ee9e8SAntonios Motakis return 0; 97260ce86c7SWei Liu } 97360ce86c7SWei Liu 97460ce86c7SWei Liu V9fsPDU *pdu_alloc(V9fsState *s) 97560ce86c7SWei Liu { 97660ce86c7SWei Liu V9fsPDU *pdu = NULL; 97760ce86c7SWei Liu 97860ce86c7SWei Liu if (!QLIST_EMPTY(&s->free_list)) { 97960ce86c7SWei Liu pdu = QLIST_FIRST(&s->free_list); 98060ce86c7SWei Liu QLIST_REMOVE(pdu, next); 98160ce86c7SWei Liu QLIST_INSERT_HEAD(&s->active_list, pdu, next); 98260ce86c7SWei Liu } 98360ce86c7SWei Liu return pdu; 98460ce86c7SWei Liu } 98560ce86c7SWei Liu 98660ce86c7SWei Liu void pdu_free(V9fsPDU *pdu) 98760ce86c7SWei Liu { 98860ce86c7SWei Liu V9fsState *s = pdu->s; 989f74e27bfSGreg Kurz 990f74e27bfSGreg Kurz g_assert(!pdu->cancelled); 99160ce86c7SWei Liu QLIST_REMOVE(pdu, next); 99260ce86c7SWei Liu QLIST_INSERT_HEAD(&s->free_list, pdu, next); 99360ce86c7SWei Liu } 99460ce86c7SWei Liu 9958440e22eSGreg Kurz static void coroutine_fn pdu_complete(V9fsPDU *pdu, ssize_t len) 99660ce86c7SWei Liu { 99760ce86c7SWei Liu int8_t id = pdu->id + 1; /* Response */ 99860ce86c7SWei Liu V9fsState *s = pdu->s; 99906a37db7SGreg Kurz int ret; 100060ce86c7SWei Liu 1001fc78d5eeSKeno Fischer /* 1002fc78d5eeSKeno Fischer * The 9p spec requires that successfully cancelled pdus receive no reply. 1003fc78d5eeSKeno Fischer * Sending a reply would confuse clients because they would 1004fc78d5eeSKeno Fischer * assume that any EINTR is the actual result of the operation, 1005fc78d5eeSKeno Fischer * rather than a consequence of the cancellation. However, if 1006fc78d5eeSKeno Fischer * the operation completed (succesfully or with an error other 1007fc78d5eeSKeno Fischer * than caused be cancellation), we do send out that reply, both 1008fc78d5eeSKeno Fischer * for efficiency and to avoid confusing the rest of the state machine 1009fc78d5eeSKeno Fischer * that assumes passing a non-error here will mean a successful 1010fc78d5eeSKeno Fischer * transmission of the reply. 1011fc78d5eeSKeno Fischer */ 1012fc78d5eeSKeno Fischer bool discard = pdu->cancelled && len == -EINTR; 1013fc78d5eeSKeno Fischer if (discard) { 1014fc78d5eeSKeno Fischer trace_v9fs_rcancel(pdu->tag, pdu->id); 1015fc78d5eeSKeno Fischer pdu->size = 0; 1016fc78d5eeSKeno Fischer goto out_notify; 1017fc78d5eeSKeno Fischer } 1018fc78d5eeSKeno Fischer 101960ce86c7SWei Liu if (len < 0) { 102060ce86c7SWei Liu int err = -len; 102160ce86c7SWei Liu len = 7; 102260ce86c7SWei Liu 102360ce86c7SWei Liu if (s->proto_version != V9FS_PROTO_2000L) { 102460ce86c7SWei Liu V9fsString str; 102560ce86c7SWei Liu 102660ce86c7SWei Liu str.data = strerror(err); 102760ce86c7SWei Liu str.size = strlen(str.data); 102860ce86c7SWei Liu 102906a37db7SGreg Kurz ret = pdu_marshal(pdu, len, "s", &str); 103006a37db7SGreg Kurz if (ret < 0) { 103106a37db7SGreg Kurz goto out_notify; 103206a37db7SGreg Kurz } 103306a37db7SGreg Kurz len += ret; 103460ce86c7SWei Liu id = P9_RERROR; 103560ce86c7SWei Liu } 103660ce86c7SWei Liu 103706a37db7SGreg Kurz ret = pdu_marshal(pdu, len, "d", err); 103806a37db7SGreg Kurz if (ret < 0) { 103906a37db7SGreg Kurz goto out_notify; 104006a37db7SGreg Kurz } 104106a37db7SGreg Kurz len += ret; 104260ce86c7SWei Liu 104360ce86c7SWei Liu if (s->proto_version == V9FS_PROTO_2000L) { 104460ce86c7SWei Liu id = P9_RLERROR; 104560ce86c7SWei Liu } 104660ce86c7SWei Liu trace_v9fs_rerror(pdu->tag, pdu->id, err); /* Trace ERROR */ 104760ce86c7SWei Liu } 104860ce86c7SWei Liu 104960ce86c7SWei Liu /* fill out the header */ 105006a37db7SGreg Kurz if (pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag) < 0) { 105106a37db7SGreg Kurz goto out_notify; 105206a37db7SGreg Kurz } 105360ce86c7SWei Liu 105460ce86c7SWei Liu /* keep these in sync */ 105560ce86c7SWei Liu pdu->size = len; 105660ce86c7SWei Liu pdu->id = id; 105760ce86c7SWei Liu 105806a37db7SGreg Kurz out_notify: 1059a17d8659SGreg Kurz pdu->s->transport->push_and_notify(pdu); 106060ce86c7SWei Liu 106160ce86c7SWei Liu /* Now wakeup anybody waiting in flush for this request */ 1062f74e27bfSGreg Kurz if (!qemu_co_queue_next(&pdu->complete)) { 106360ce86c7SWei Liu pdu_free(pdu); 106460ce86c7SWei Liu } 1065f74e27bfSGreg Kurz } 106660ce86c7SWei Liu 106760ce86c7SWei Liu static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension) 106860ce86c7SWei Liu { 106960ce86c7SWei Liu mode_t ret; 107060ce86c7SWei Liu 107160ce86c7SWei Liu ret = mode & 0777; 107260ce86c7SWei Liu if (mode & P9_STAT_MODE_DIR) { 107360ce86c7SWei Liu ret |= S_IFDIR; 107460ce86c7SWei Liu } 107560ce86c7SWei Liu 107660ce86c7SWei Liu if (mode & P9_STAT_MODE_SYMLINK) { 107760ce86c7SWei Liu ret |= S_IFLNK; 107860ce86c7SWei Liu } 107960ce86c7SWei Liu if (mode & P9_STAT_MODE_SOCKET) { 108060ce86c7SWei Liu ret |= S_IFSOCK; 108160ce86c7SWei Liu } 108260ce86c7SWei Liu if (mode & P9_STAT_MODE_NAMED_PIPE) { 108360ce86c7SWei Liu ret |= S_IFIFO; 108460ce86c7SWei Liu } 108560ce86c7SWei Liu if (mode & P9_STAT_MODE_DEVICE) { 108660ce86c7SWei Liu if (extension->size && extension->data[0] == 'c') { 108760ce86c7SWei Liu ret |= S_IFCHR; 108860ce86c7SWei Liu } else { 108960ce86c7SWei Liu ret |= S_IFBLK; 109060ce86c7SWei Liu } 109160ce86c7SWei Liu } 109260ce86c7SWei Liu 109360ce86c7SWei Liu if (!(ret & ~0777)) { 109460ce86c7SWei Liu ret |= S_IFREG; 109560ce86c7SWei Liu } 109660ce86c7SWei Liu 109760ce86c7SWei Liu if (mode & P9_STAT_MODE_SETUID) { 109860ce86c7SWei Liu ret |= S_ISUID; 109960ce86c7SWei Liu } 110060ce86c7SWei Liu if (mode & P9_STAT_MODE_SETGID) { 110160ce86c7SWei Liu ret |= S_ISGID; 110260ce86c7SWei Liu } 110360ce86c7SWei Liu if (mode & P9_STAT_MODE_SETVTX) { 110460ce86c7SWei Liu ret |= S_ISVTX; 110560ce86c7SWei Liu } 110660ce86c7SWei Liu 110760ce86c7SWei Liu return ret; 110860ce86c7SWei Liu } 110960ce86c7SWei Liu 111060ce86c7SWei Liu static int donttouch_stat(V9fsStat *stat) 111160ce86c7SWei Liu { 111260ce86c7SWei Liu if (stat->type == -1 && 111360ce86c7SWei Liu stat->dev == -1 && 111487032833SAntonios Motakis stat->qid.type == 0xff && 111587032833SAntonios Motakis stat->qid.version == (uint32_t) -1 && 111687032833SAntonios Motakis stat->qid.path == (uint64_t) -1 && 111760ce86c7SWei Liu stat->mode == -1 && 111860ce86c7SWei Liu stat->atime == -1 && 111960ce86c7SWei Liu stat->mtime == -1 && 112060ce86c7SWei Liu stat->length == -1 && 112160ce86c7SWei Liu !stat->name.size && 112260ce86c7SWei Liu !stat->uid.size && 112360ce86c7SWei Liu !stat->gid.size && 112460ce86c7SWei Liu !stat->muid.size && 112560ce86c7SWei Liu stat->n_uid == -1 && 112660ce86c7SWei Liu stat->n_gid == -1 && 112760ce86c7SWei Liu stat->n_muid == -1) { 112860ce86c7SWei Liu return 1; 112960ce86c7SWei Liu } 113060ce86c7SWei Liu 113160ce86c7SWei Liu return 0; 113260ce86c7SWei Liu } 113360ce86c7SWei Liu 113460ce86c7SWei Liu static void v9fs_stat_init(V9fsStat *stat) 113560ce86c7SWei Liu { 113660ce86c7SWei Liu v9fs_string_init(&stat->name); 113760ce86c7SWei Liu v9fs_string_init(&stat->uid); 113860ce86c7SWei Liu v9fs_string_init(&stat->gid); 113960ce86c7SWei Liu v9fs_string_init(&stat->muid); 114060ce86c7SWei Liu v9fs_string_init(&stat->extension); 114160ce86c7SWei Liu } 114260ce86c7SWei Liu 114360ce86c7SWei Liu static void v9fs_stat_free(V9fsStat *stat) 114460ce86c7SWei Liu { 114560ce86c7SWei Liu v9fs_string_free(&stat->name); 114660ce86c7SWei Liu v9fs_string_free(&stat->uid); 114760ce86c7SWei Liu v9fs_string_free(&stat->gid); 114860ce86c7SWei Liu v9fs_string_free(&stat->muid); 114960ce86c7SWei Liu v9fs_string_free(&stat->extension); 115060ce86c7SWei Liu } 115160ce86c7SWei Liu 115260ce86c7SWei Liu static uint32_t stat_to_v9mode(const struct stat *stbuf) 115360ce86c7SWei Liu { 115460ce86c7SWei Liu uint32_t mode; 115560ce86c7SWei Liu 115660ce86c7SWei Liu mode = stbuf->st_mode & 0777; 115760ce86c7SWei Liu if (S_ISDIR(stbuf->st_mode)) { 115860ce86c7SWei Liu mode |= P9_STAT_MODE_DIR; 115960ce86c7SWei Liu } 116060ce86c7SWei Liu 116160ce86c7SWei Liu if (S_ISLNK(stbuf->st_mode)) { 116260ce86c7SWei Liu mode |= P9_STAT_MODE_SYMLINK; 116360ce86c7SWei Liu } 116460ce86c7SWei Liu 116560ce86c7SWei Liu if (S_ISSOCK(stbuf->st_mode)) { 116660ce86c7SWei Liu mode |= P9_STAT_MODE_SOCKET; 116760ce86c7SWei Liu } 116860ce86c7SWei Liu 116960ce86c7SWei Liu if (S_ISFIFO(stbuf->st_mode)) { 117060ce86c7SWei Liu mode |= P9_STAT_MODE_NAMED_PIPE; 117160ce86c7SWei Liu } 117260ce86c7SWei Liu 117360ce86c7SWei Liu if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) { 117460ce86c7SWei Liu mode |= P9_STAT_MODE_DEVICE; 117560ce86c7SWei Liu } 117660ce86c7SWei Liu 117760ce86c7SWei Liu if (stbuf->st_mode & S_ISUID) { 117860ce86c7SWei Liu mode |= P9_STAT_MODE_SETUID; 117960ce86c7SWei Liu } 118060ce86c7SWei Liu 118160ce86c7SWei Liu if (stbuf->st_mode & S_ISGID) { 118260ce86c7SWei Liu mode |= P9_STAT_MODE_SETGID; 118360ce86c7SWei Liu } 118460ce86c7SWei Liu 118560ce86c7SWei Liu if (stbuf->st_mode & S_ISVTX) { 118660ce86c7SWei Liu mode |= P9_STAT_MODE_SETVTX; 118760ce86c7SWei Liu } 118860ce86c7SWei Liu 118960ce86c7SWei Liu return mode; 119060ce86c7SWei Liu } 119160ce86c7SWei Liu 11926069537fSJan Dakinevich static int coroutine_fn stat_to_v9stat(V9fsPDU *pdu, V9fsPath *path, 11936069537fSJan Dakinevich const char *basename, 119460ce86c7SWei Liu const struct stat *stbuf, 119560ce86c7SWei Liu V9fsStat *v9stat) 119660ce86c7SWei Liu { 119760ce86c7SWei Liu int err; 119860ce86c7SWei Liu 119960ce86c7SWei Liu memset(v9stat, 0, sizeof(*v9stat)); 120060ce86c7SWei Liu 12013b5ee9e8SAntonios Motakis err = stat_to_qid(pdu, stbuf, &v9stat->qid); 12023b5ee9e8SAntonios Motakis if (err < 0) { 12033b5ee9e8SAntonios Motakis return err; 12043b5ee9e8SAntonios Motakis } 120560ce86c7SWei Liu v9stat->mode = stat_to_v9mode(stbuf); 120660ce86c7SWei Liu v9stat->atime = stbuf->st_atime; 120760ce86c7SWei Liu v9stat->mtime = stbuf->st_mtime; 120860ce86c7SWei Liu v9stat->length = stbuf->st_size; 120960ce86c7SWei Liu 1210abdf0086SGreg Kurz v9fs_string_free(&v9stat->uid); 1211abdf0086SGreg Kurz v9fs_string_free(&v9stat->gid); 1212abdf0086SGreg Kurz v9fs_string_free(&v9stat->muid); 121360ce86c7SWei Liu 121460ce86c7SWei Liu v9stat->n_uid = stbuf->st_uid; 121560ce86c7SWei Liu v9stat->n_gid = stbuf->st_gid; 121660ce86c7SWei Liu v9stat->n_muid = 0; 121760ce86c7SWei Liu 1218abdf0086SGreg Kurz v9fs_string_free(&v9stat->extension); 121960ce86c7SWei Liu 122060ce86c7SWei Liu if (v9stat->mode & P9_STAT_MODE_SYMLINK) { 12216069537fSJan Dakinevich err = v9fs_co_readlink(pdu, path, &v9stat->extension); 122260ce86c7SWei Liu if (err < 0) { 122360ce86c7SWei Liu return err; 122460ce86c7SWei Liu } 122560ce86c7SWei Liu } else if (v9stat->mode & P9_STAT_MODE_DEVICE) { 122660ce86c7SWei Liu v9fs_string_sprintf(&v9stat->extension, "%c %u %u", 122760ce86c7SWei Liu S_ISCHR(stbuf->st_mode) ? 'c' : 'b', 122860ce86c7SWei Liu major(stbuf->st_rdev), minor(stbuf->st_rdev)); 122960ce86c7SWei Liu } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) { 123060ce86c7SWei Liu v9fs_string_sprintf(&v9stat->extension, "%s %lu", 123160ce86c7SWei Liu "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink); 123260ce86c7SWei Liu } 123360ce86c7SWei Liu 12346069537fSJan Dakinevich v9fs_string_sprintf(&v9stat->name, "%s", basename); 123560ce86c7SWei Liu 123660ce86c7SWei Liu v9stat->size = 61 + 123760ce86c7SWei Liu v9fs_string_size(&v9stat->name) + 123860ce86c7SWei Liu v9fs_string_size(&v9stat->uid) + 123960ce86c7SWei Liu v9fs_string_size(&v9stat->gid) + 124060ce86c7SWei Liu v9fs_string_size(&v9stat->muid) + 124160ce86c7SWei Liu v9fs_string_size(&v9stat->extension); 124260ce86c7SWei Liu return 0; 124360ce86c7SWei Liu } 124460ce86c7SWei Liu 124560ce86c7SWei Liu #define P9_STATS_MODE 0x00000001ULL 124660ce86c7SWei Liu #define P9_STATS_NLINK 0x00000002ULL 124760ce86c7SWei Liu #define P9_STATS_UID 0x00000004ULL 124860ce86c7SWei Liu #define P9_STATS_GID 0x00000008ULL 124960ce86c7SWei Liu #define P9_STATS_RDEV 0x00000010ULL 125060ce86c7SWei Liu #define P9_STATS_ATIME 0x00000020ULL 125160ce86c7SWei Liu #define P9_STATS_MTIME 0x00000040ULL 125260ce86c7SWei Liu #define P9_STATS_CTIME 0x00000080ULL 125360ce86c7SWei Liu #define P9_STATS_INO 0x00000100ULL 125460ce86c7SWei Liu #define P9_STATS_SIZE 0x00000200ULL 125560ce86c7SWei Liu #define P9_STATS_BLOCKS 0x00000400ULL 125660ce86c7SWei Liu 125760ce86c7SWei Liu #define P9_STATS_BTIME 0x00000800ULL 125860ce86c7SWei Liu #define P9_STATS_GEN 0x00001000ULL 125960ce86c7SWei Liu #define P9_STATS_DATA_VERSION 0x00002000ULL 126060ce86c7SWei Liu 126160ce86c7SWei Liu #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */ 126260ce86c7SWei Liu #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */ 126360ce86c7SWei Liu 126460ce86c7SWei Liu 12653b5ee9e8SAntonios Motakis static int stat_to_v9stat_dotl(V9fsPDU *pdu, const struct stat *stbuf, 126660ce86c7SWei Liu V9fsStatDotl *v9lstat) 126760ce86c7SWei Liu { 126860ce86c7SWei Liu memset(v9lstat, 0, sizeof(*v9lstat)); 126960ce86c7SWei Liu 127060ce86c7SWei Liu v9lstat->st_mode = stbuf->st_mode; 127160ce86c7SWei Liu v9lstat->st_nlink = stbuf->st_nlink; 127260ce86c7SWei Liu v9lstat->st_uid = stbuf->st_uid; 127360ce86c7SWei Liu v9lstat->st_gid = stbuf->st_gid; 127460ce86c7SWei Liu v9lstat->st_rdev = stbuf->st_rdev; 127560ce86c7SWei Liu v9lstat->st_size = stbuf->st_size; 127660ce86c7SWei Liu v9lstat->st_blksize = stbuf->st_blksize; 127760ce86c7SWei Liu v9lstat->st_blocks = stbuf->st_blocks; 127860ce86c7SWei Liu v9lstat->st_atime_sec = stbuf->st_atime; 127960ce86c7SWei Liu v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec; 128060ce86c7SWei Liu v9lstat->st_mtime_sec = stbuf->st_mtime; 128160ce86c7SWei Liu v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec; 128260ce86c7SWei Liu v9lstat->st_ctime_sec = stbuf->st_ctime; 128360ce86c7SWei Liu v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec; 128460ce86c7SWei Liu /* Currently we only support BASIC fields in stat */ 128560ce86c7SWei Liu v9lstat->st_result_mask = P9_STATS_BASIC; 128660ce86c7SWei Liu 12873b5ee9e8SAntonios Motakis return stat_to_qid(pdu, stbuf, &v9lstat->qid); 128860ce86c7SWei Liu } 128960ce86c7SWei Liu 129060ce86c7SWei Liu static void print_sg(struct iovec *sg, int cnt) 129160ce86c7SWei Liu { 129260ce86c7SWei Liu int i; 129360ce86c7SWei Liu 129460ce86c7SWei Liu printf("sg[%d]: {", cnt); 129560ce86c7SWei Liu for (i = 0; i < cnt; i++) { 129660ce86c7SWei Liu if (i) { 129760ce86c7SWei Liu printf(", "); 129860ce86c7SWei Liu } 129960ce86c7SWei Liu printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len); 130060ce86c7SWei Liu } 130160ce86c7SWei Liu printf("}\n"); 130260ce86c7SWei Liu } 130360ce86c7SWei Liu 130460ce86c7SWei Liu /* Will call this only for path name based fid */ 130560ce86c7SWei Liu static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len) 130660ce86c7SWei Liu { 130760ce86c7SWei Liu V9fsPath str; 130860ce86c7SWei Liu v9fs_path_init(&str); 130960ce86c7SWei Liu v9fs_path_copy(&str, dst); 1310e3e83f2eSGreg Kurz v9fs_path_sprintf(dst, "%s%s", src->data, str.data + len); 131160ce86c7SWei Liu v9fs_path_free(&str); 131260ce86c7SWei Liu } 131360ce86c7SWei Liu 131460ce86c7SWei Liu static inline bool is_ro_export(FsContext *ctx) 131560ce86c7SWei Liu { 131660ce86c7SWei Liu return ctx->export_flags & V9FS_RDONLY; 131760ce86c7SWei Liu } 131860ce86c7SWei Liu 13198440e22eSGreg Kurz static void coroutine_fn v9fs_version(void *opaque) 132060ce86c7SWei Liu { 132160ce86c7SWei Liu ssize_t err; 132260ce86c7SWei Liu V9fsPDU *pdu = opaque; 132360ce86c7SWei Liu V9fsState *s = pdu->s; 132460ce86c7SWei Liu V9fsString version; 132560ce86c7SWei Liu size_t offset = 7; 132660ce86c7SWei Liu 132760ce86c7SWei Liu v9fs_string_init(&version); 132860ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "ds", &s->msize, &version); 132960ce86c7SWei Liu if (err < 0) { 133060ce86c7SWei Liu goto out; 133160ce86c7SWei Liu } 133260ce86c7SWei Liu trace_v9fs_version(pdu->tag, pdu->id, s->msize, version.data); 133360ce86c7SWei Liu 133460ce86c7SWei Liu virtfs_reset(pdu); 133560ce86c7SWei Liu 133660ce86c7SWei Liu if (!strcmp(version.data, "9P2000.u")) { 133760ce86c7SWei Liu s->proto_version = V9FS_PROTO_2000U; 133860ce86c7SWei Liu } else if (!strcmp(version.data, "9P2000.L")) { 133960ce86c7SWei Liu s->proto_version = V9FS_PROTO_2000L; 134060ce86c7SWei Liu } else { 134160ce86c7SWei Liu v9fs_string_sprintf(&version, "unknown"); 1342e16453a3SChristian Schoenebeck /* skip min. msize check, reporting invalid version has priority */ 1343e16453a3SChristian Schoenebeck goto marshal; 134460ce86c7SWei Liu } 134560ce86c7SWei Liu 1346e16453a3SChristian Schoenebeck if (s->msize < P9_MIN_MSIZE) { 1347e16453a3SChristian Schoenebeck err = -EMSGSIZE; 1348e16453a3SChristian Schoenebeck error_report( 1349e16453a3SChristian Schoenebeck "9pfs: Client requested msize < minimum msize (" 1350e16453a3SChristian Schoenebeck stringify(P9_MIN_MSIZE) ") supported by this server." 1351e16453a3SChristian Schoenebeck ); 1352e16453a3SChristian Schoenebeck goto out; 1353e16453a3SChristian Schoenebeck } 1354e16453a3SChristian Schoenebeck 135562777d82SChristian Schoenebeck /* 8192 is the default msize of Linux clients */ 1356c418f935SChristian Schoenebeck if (s->msize <= 8192 && !(s->ctx.export_flags & V9FS_NO_PERF_WARN)) { 135762777d82SChristian Schoenebeck warn_report_once( 135862777d82SChristian Schoenebeck "9p: degraded performance: a reasonable high msize should be " 135962777d82SChristian Schoenebeck "chosen on client/guest side (chosen msize is <= 8192). See " 136062777d82SChristian Schoenebeck "https://wiki.qemu.org/Documentation/9psetup#msize for details." 136162777d82SChristian Schoenebeck ); 136262777d82SChristian Schoenebeck } 136362777d82SChristian Schoenebeck 1364e16453a3SChristian Schoenebeck marshal: 136560ce86c7SWei Liu err = pdu_marshal(pdu, offset, "ds", s->msize, &version); 136660ce86c7SWei Liu if (err < 0) { 136760ce86c7SWei Liu goto out; 136860ce86c7SWei Liu } 1369403a905bSPhilippe Mathieu-Daudé err += offset; 137060ce86c7SWei Liu trace_v9fs_version_return(pdu->tag, pdu->id, s->msize, version.data); 137160ce86c7SWei Liu out: 1372403a905bSPhilippe Mathieu-Daudé pdu_complete(pdu, err); 137360ce86c7SWei Liu v9fs_string_free(&version); 137460ce86c7SWei Liu } 137560ce86c7SWei Liu 13768440e22eSGreg Kurz static void coroutine_fn v9fs_attach(void *opaque) 137760ce86c7SWei Liu { 137860ce86c7SWei Liu V9fsPDU *pdu = opaque; 137960ce86c7SWei Liu V9fsState *s = pdu->s; 138060ce86c7SWei Liu int32_t fid, afid, n_uname; 138160ce86c7SWei Liu V9fsString uname, aname; 138260ce86c7SWei Liu V9fsFidState *fidp; 138360ce86c7SWei Liu size_t offset = 7; 138460ce86c7SWei Liu V9fsQID qid; 138560ce86c7SWei Liu ssize_t err; 138611024375SChristian Schoenebeck struct stat stbuf; 138760ce86c7SWei Liu 138860ce86c7SWei Liu v9fs_string_init(&uname); 138960ce86c7SWei Liu v9fs_string_init(&aname); 139060ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "ddssd", &fid, 139160ce86c7SWei Liu &afid, &uname, &aname, &n_uname); 139260ce86c7SWei Liu if (err < 0) { 139360ce86c7SWei Liu goto out_nofid; 139460ce86c7SWei Liu } 139560ce86c7SWei Liu trace_v9fs_attach(pdu->tag, pdu->id, fid, afid, uname.data, aname.data); 139660ce86c7SWei Liu 139760ce86c7SWei Liu fidp = alloc_fid(s, fid); 139860ce86c7SWei Liu if (fidp == NULL) { 139960ce86c7SWei Liu err = -EINVAL; 140060ce86c7SWei Liu goto out_nofid; 140160ce86c7SWei Liu } 140260ce86c7SWei Liu fidp->uid = n_uname; 140360ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, NULL, "/", &fidp->path); 140460ce86c7SWei Liu if (err < 0) { 140560ce86c7SWei Liu err = -EINVAL; 140660ce86c7SWei Liu clunk_fid(s, fid); 140760ce86c7SWei Liu goto out; 140860ce86c7SWei Liu } 140911024375SChristian Schoenebeck err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); 141011024375SChristian Schoenebeck if (err < 0) { 141111024375SChristian Schoenebeck err = -EINVAL; 141211024375SChristian Schoenebeck clunk_fid(s, fid); 141311024375SChristian Schoenebeck goto out; 141411024375SChristian Schoenebeck } 141511024375SChristian Schoenebeck err = stat_to_qid(pdu, &stbuf, &qid); 141660ce86c7SWei Liu if (err < 0) { 141760ce86c7SWei Liu err = -EINVAL; 141860ce86c7SWei Liu clunk_fid(s, fid); 141960ce86c7SWei Liu goto out; 142060ce86c7SWei Liu } 1421fe44dc91SAshijeet Acharya 1422fe44dc91SAshijeet Acharya /* 1423fe44dc91SAshijeet Acharya * disable migration if we haven't done already. 1424fe44dc91SAshijeet Acharya * attach could get called multiple times for the same export. 1425fe44dc91SAshijeet Acharya */ 1426fe44dc91SAshijeet Acharya if (!s->migration_blocker) { 1427fe44dc91SAshijeet Acharya error_setg(&s->migration_blocker, 1428fe44dc91SAshijeet Acharya "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'", 1429fe44dc91SAshijeet Acharya s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag); 14309261ef5eSMarkus Armbruster err = migrate_add_blocker(s->migration_blocker, NULL); 14319261ef5eSMarkus Armbruster if (err < 0) { 1432fe44dc91SAshijeet Acharya error_free(s->migration_blocker); 1433fe44dc91SAshijeet Acharya s->migration_blocker = NULL; 1434fe44dc91SAshijeet Acharya clunk_fid(s, fid); 1435fe44dc91SAshijeet Acharya goto out; 1436fe44dc91SAshijeet Acharya } 1437fe44dc91SAshijeet Acharya s->root_fid = fid; 1438fe44dc91SAshijeet Acharya } 1439fe44dc91SAshijeet Acharya 144060ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Q", &qid); 144160ce86c7SWei Liu if (err < 0) { 144260ce86c7SWei Liu clunk_fid(s, fid); 144360ce86c7SWei Liu goto out; 144460ce86c7SWei Liu } 144560ce86c7SWei Liu err += offset; 1446fe44dc91SAshijeet Acharya 144756f101ecSGreg Kurz memcpy(&s->root_qid, &qid, sizeof(qid)); 144811024375SChristian Schoenebeck memcpy(&s->root_st, &stbuf, sizeof(stbuf)); 144960ce86c7SWei Liu trace_v9fs_attach_return(pdu->tag, pdu->id, 145060ce86c7SWei Liu qid.type, qid.version, qid.path); 145160ce86c7SWei Liu out: 145260ce86c7SWei Liu put_fid(pdu, fidp); 145360ce86c7SWei Liu out_nofid: 145460ce86c7SWei Liu pdu_complete(pdu, err); 145560ce86c7SWei Liu v9fs_string_free(&uname); 145660ce86c7SWei Liu v9fs_string_free(&aname); 145760ce86c7SWei Liu } 145860ce86c7SWei Liu 14598440e22eSGreg Kurz static void coroutine_fn v9fs_stat(void *opaque) 146060ce86c7SWei Liu { 146160ce86c7SWei Liu int32_t fid; 146260ce86c7SWei Liu V9fsStat v9stat; 146360ce86c7SWei Liu ssize_t err = 0; 146460ce86c7SWei Liu size_t offset = 7; 146560ce86c7SWei Liu struct stat stbuf; 146660ce86c7SWei Liu V9fsFidState *fidp; 146760ce86c7SWei Liu V9fsPDU *pdu = opaque; 14686069537fSJan Dakinevich char *basename; 146960ce86c7SWei Liu 147060ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "d", &fid); 147160ce86c7SWei Liu if (err < 0) { 147260ce86c7SWei Liu goto out_nofid; 147360ce86c7SWei Liu } 147460ce86c7SWei Liu trace_v9fs_stat(pdu->tag, pdu->id, fid); 147560ce86c7SWei Liu 147660ce86c7SWei Liu fidp = get_fid(pdu, fid); 147760ce86c7SWei Liu if (fidp == NULL) { 147860ce86c7SWei Liu err = -ENOENT; 147960ce86c7SWei Liu goto out_nofid; 148060ce86c7SWei Liu } 148160ce86c7SWei Liu err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); 148260ce86c7SWei Liu if (err < 0) { 148360ce86c7SWei Liu goto out; 148460ce86c7SWei Liu } 14856069537fSJan Dakinevich basename = g_path_get_basename(fidp->path.data); 14866069537fSJan Dakinevich err = stat_to_v9stat(pdu, &fidp->path, basename, &stbuf, &v9stat); 14876069537fSJan Dakinevich g_free(basename); 148860ce86c7SWei Liu if (err < 0) { 148960ce86c7SWei Liu goto out; 149060ce86c7SWei Liu } 149160ce86c7SWei Liu err = pdu_marshal(pdu, offset, "wS", 0, &v9stat); 149260ce86c7SWei Liu if (err < 0) { 149360ce86c7SWei Liu v9fs_stat_free(&v9stat); 149460ce86c7SWei Liu goto out; 149560ce86c7SWei Liu } 149660ce86c7SWei Liu trace_v9fs_stat_return(pdu->tag, pdu->id, v9stat.mode, 149760ce86c7SWei Liu v9stat.atime, v9stat.mtime, v9stat.length); 149860ce86c7SWei Liu err += offset; 149960ce86c7SWei Liu v9fs_stat_free(&v9stat); 150060ce86c7SWei Liu out: 150160ce86c7SWei Liu put_fid(pdu, fidp); 150260ce86c7SWei Liu out_nofid: 150360ce86c7SWei Liu pdu_complete(pdu, err); 150460ce86c7SWei Liu } 150560ce86c7SWei Liu 15068440e22eSGreg Kurz static void coroutine_fn v9fs_getattr(void *opaque) 150760ce86c7SWei Liu { 150860ce86c7SWei Liu int32_t fid; 150960ce86c7SWei Liu size_t offset = 7; 151060ce86c7SWei Liu ssize_t retval = 0; 151160ce86c7SWei Liu struct stat stbuf; 151260ce86c7SWei Liu V9fsFidState *fidp; 151360ce86c7SWei Liu uint64_t request_mask; 151460ce86c7SWei Liu V9fsStatDotl v9stat_dotl; 151560ce86c7SWei Liu V9fsPDU *pdu = opaque; 151660ce86c7SWei Liu 151760ce86c7SWei Liu retval = pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask); 151860ce86c7SWei Liu if (retval < 0) { 151960ce86c7SWei Liu goto out_nofid; 152060ce86c7SWei Liu } 152160ce86c7SWei Liu trace_v9fs_getattr(pdu->tag, pdu->id, fid, request_mask); 152260ce86c7SWei Liu 152360ce86c7SWei Liu fidp = get_fid(pdu, fid); 152460ce86c7SWei Liu if (fidp == NULL) { 152560ce86c7SWei Liu retval = -ENOENT; 152660ce86c7SWei Liu goto out_nofid; 152760ce86c7SWei Liu } 152860ce86c7SWei Liu /* 152960ce86c7SWei Liu * Currently we only support BASIC fields in stat, so there is no 153060ce86c7SWei Liu * need to look at request_mask. 153160ce86c7SWei Liu */ 153260ce86c7SWei Liu retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf); 153360ce86c7SWei Liu if (retval < 0) { 153460ce86c7SWei Liu goto out; 153560ce86c7SWei Liu } 15363b5ee9e8SAntonios Motakis retval = stat_to_v9stat_dotl(pdu, &stbuf, &v9stat_dotl); 15373b5ee9e8SAntonios Motakis if (retval < 0) { 15383b5ee9e8SAntonios Motakis goto out; 15393b5ee9e8SAntonios Motakis } 154060ce86c7SWei Liu 154160ce86c7SWei Liu /* fill st_gen if requested and supported by underlying fs */ 154260ce86c7SWei Liu if (request_mask & P9_STATS_GEN) { 154360ce86c7SWei Liu retval = v9fs_co_st_gen(pdu, &fidp->path, stbuf.st_mode, &v9stat_dotl); 154460ce86c7SWei Liu switch (retval) { 154560ce86c7SWei Liu case 0: 154660ce86c7SWei Liu /* we have valid st_gen: update result mask */ 154760ce86c7SWei Liu v9stat_dotl.st_result_mask |= P9_STATS_GEN; 154860ce86c7SWei Liu break; 154960ce86c7SWei Liu case -EINTR: 155060ce86c7SWei Liu /* request cancelled, e.g. by Tflush */ 155160ce86c7SWei Liu goto out; 155260ce86c7SWei Liu default: 155360ce86c7SWei Liu /* failed to get st_gen: not fatal, ignore */ 155460ce86c7SWei Liu break; 155560ce86c7SWei Liu } 155660ce86c7SWei Liu } 155760ce86c7SWei Liu retval = pdu_marshal(pdu, offset, "A", &v9stat_dotl); 155860ce86c7SWei Liu if (retval < 0) { 155960ce86c7SWei Liu goto out; 156060ce86c7SWei Liu } 156160ce86c7SWei Liu retval += offset; 156260ce86c7SWei Liu trace_v9fs_getattr_return(pdu->tag, pdu->id, v9stat_dotl.st_result_mask, 156360ce86c7SWei Liu v9stat_dotl.st_mode, v9stat_dotl.st_uid, 156460ce86c7SWei Liu v9stat_dotl.st_gid); 156560ce86c7SWei Liu out: 156660ce86c7SWei Liu put_fid(pdu, fidp); 156760ce86c7SWei Liu out_nofid: 156860ce86c7SWei Liu pdu_complete(pdu, retval); 156960ce86c7SWei Liu } 157060ce86c7SWei Liu 157160ce86c7SWei Liu /* Attribute flags */ 157260ce86c7SWei Liu #define P9_ATTR_MODE (1 << 0) 157360ce86c7SWei Liu #define P9_ATTR_UID (1 << 1) 157460ce86c7SWei Liu #define P9_ATTR_GID (1 << 2) 157560ce86c7SWei Liu #define P9_ATTR_SIZE (1 << 3) 157660ce86c7SWei Liu #define P9_ATTR_ATIME (1 << 4) 157760ce86c7SWei Liu #define P9_ATTR_MTIME (1 << 5) 157860ce86c7SWei Liu #define P9_ATTR_CTIME (1 << 6) 157960ce86c7SWei Liu #define P9_ATTR_ATIME_SET (1 << 7) 158060ce86c7SWei Liu #define P9_ATTR_MTIME_SET (1 << 8) 158160ce86c7SWei Liu 158260ce86c7SWei Liu #define P9_ATTR_MASK 127 158360ce86c7SWei Liu 15848440e22eSGreg Kurz static void coroutine_fn v9fs_setattr(void *opaque) 158560ce86c7SWei Liu { 158660ce86c7SWei Liu int err = 0; 158760ce86c7SWei Liu int32_t fid; 158860ce86c7SWei Liu V9fsFidState *fidp; 158960ce86c7SWei Liu size_t offset = 7; 159060ce86c7SWei Liu V9fsIattr v9iattr; 159160ce86c7SWei Liu V9fsPDU *pdu = opaque; 159260ce86c7SWei Liu 159360ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr); 159460ce86c7SWei Liu if (err < 0) { 159560ce86c7SWei Liu goto out_nofid; 159660ce86c7SWei Liu } 159760ce86c7SWei Liu 15988f9c64bfSGreg Kurz trace_v9fs_setattr(pdu->tag, pdu->id, fid, 15998f9c64bfSGreg Kurz v9iattr.valid, v9iattr.mode, v9iattr.uid, v9iattr.gid, 16008f9c64bfSGreg Kurz v9iattr.size, v9iattr.atime_sec, v9iattr.mtime_sec); 16018f9c64bfSGreg Kurz 160260ce86c7SWei Liu fidp = get_fid(pdu, fid); 160360ce86c7SWei Liu if (fidp == NULL) { 160460ce86c7SWei Liu err = -EINVAL; 160560ce86c7SWei Liu goto out_nofid; 160660ce86c7SWei Liu } 160760ce86c7SWei Liu if (v9iattr.valid & P9_ATTR_MODE) { 160860ce86c7SWei Liu err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode); 160960ce86c7SWei Liu if (err < 0) { 161060ce86c7SWei Liu goto out; 161160ce86c7SWei Liu } 161260ce86c7SWei Liu } 161360ce86c7SWei Liu if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) { 161460ce86c7SWei Liu struct timespec times[2]; 161560ce86c7SWei Liu if (v9iattr.valid & P9_ATTR_ATIME) { 161660ce86c7SWei Liu if (v9iattr.valid & P9_ATTR_ATIME_SET) { 161760ce86c7SWei Liu times[0].tv_sec = v9iattr.atime_sec; 161860ce86c7SWei Liu times[0].tv_nsec = v9iattr.atime_nsec; 161960ce86c7SWei Liu } else { 162060ce86c7SWei Liu times[0].tv_nsec = UTIME_NOW; 162160ce86c7SWei Liu } 162260ce86c7SWei Liu } else { 162360ce86c7SWei Liu times[0].tv_nsec = UTIME_OMIT; 162460ce86c7SWei Liu } 162560ce86c7SWei Liu if (v9iattr.valid & P9_ATTR_MTIME) { 162660ce86c7SWei Liu if (v9iattr.valid & P9_ATTR_MTIME_SET) { 162760ce86c7SWei Liu times[1].tv_sec = v9iattr.mtime_sec; 162860ce86c7SWei Liu times[1].tv_nsec = v9iattr.mtime_nsec; 162960ce86c7SWei Liu } else { 163060ce86c7SWei Liu times[1].tv_nsec = UTIME_NOW; 163160ce86c7SWei Liu } 163260ce86c7SWei Liu } else { 163360ce86c7SWei Liu times[1].tv_nsec = UTIME_OMIT; 163460ce86c7SWei Liu } 163560ce86c7SWei Liu err = v9fs_co_utimensat(pdu, &fidp->path, times); 163660ce86c7SWei Liu if (err < 0) { 163760ce86c7SWei Liu goto out; 163860ce86c7SWei Liu } 163960ce86c7SWei Liu } 164060ce86c7SWei Liu /* 164160ce86c7SWei Liu * If the only valid entry in iattr is ctime we can call 164260ce86c7SWei Liu * chown(-1,-1) to update the ctime of the file 164360ce86c7SWei Liu */ 164460ce86c7SWei Liu if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) || 164560ce86c7SWei Liu ((v9iattr.valid & P9_ATTR_CTIME) 164660ce86c7SWei Liu && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) { 164760ce86c7SWei Liu if (!(v9iattr.valid & P9_ATTR_UID)) { 164860ce86c7SWei Liu v9iattr.uid = -1; 164960ce86c7SWei Liu } 165060ce86c7SWei Liu if (!(v9iattr.valid & P9_ATTR_GID)) { 165160ce86c7SWei Liu v9iattr.gid = -1; 165260ce86c7SWei Liu } 165360ce86c7SWei Liu err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid, 165460ce86c7SWei Liu v9iattr.gid); 165560ce86c7SWei Liu if (err < 0) { 165660ce86c7SWei Liu goto out; 165760ce86c7SWei Liu } 165860ce86c7SWei Liu } 165960ce86c7SWei Liu if (v9iattr.valid & (P9_ATTR_SIZE)) { 166060ce86c7SWei Liu err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size); 166160ce86c7SWei Liu if (err < 0) { 166260ce86c7SWei Liu goto out; 166360ce86c7SWei Liu } 166460ce86c7SWei Liu } 166560ce86c7SWei Liu err = offset; 16668f9c64bfSGreg Kurz trace_v9fs_setattr_return(pdu->tag, pdu->id); 166760ce86c7SWei Liu out: 166860ce86c7SWei Liu put_fid(pdu, fidp); 166960ce86c7SWei Liu out_nofid: 167060ce86c7SWei Liu pdu_complete(pdu, err); 167160ce86c7SWei Liu } 167260ce86c7SWei Liu 167360ce86c7SWei Liu static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids) 167460ce86c7SWei Liu { 167560ce86c7SWei Liu int i; 167660ce86c7SWei Liu ssize_t err; 167760ce86c7SWei Liu size_t offset = 7; 167860ce86c7SWei Liu 167960ce86c7SWei Liu err = pdu_marshal(pdu, offset, "w", nwnames); 168060ce86c7SWei Liu if (err < 0) { 168160ce86c7SWei Liu return err; 168260ce86c7SWei Liu } 168360ce86c7SWei Liu offset += err; 168460ce86c7SWei Liu for (i = 0; i < nwnames; i++) { 168560ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Q", &qids[i]); 168660ce86c7SWei Liu if (err < 0) { 168760ce86c7SWei Liu return err; 168860ce86c7SWei Liu } 168960ce86c7SWei Liu offset += err; 169060ce86c7SWei Liu } 169160ce86c7SWei Liu return offset; 169260ce86c7SWei Liu } 169360ce86c7SWei Liu 1694fff39a7aSGreg Kurz static bool name_is_illegal(const char *name) 1695fff39a7aSGreg Kurz { 1696fff39a7aSGreg Kurz return !*name || strchr(name, '/') != NULL; 1697fff39a7aSGreg Kurz } 1698fff39a7aSGreg Kurz 1699*f22cad42SChristian Schoenebeck static bool same_stat_id(const struct stat *a, const struct stat *b) 170056f101ecSGreg Kurz { 1701*f22cad42SChristian Schoenebeck return a->st_dev == b->st_dev && a->st_ino == b->st_ino; 170256f101ecSGreg Kurz } 170356f101ecSGreg Kurz 17048440e22eSGreg Kurz static void coroutine_fn v9fs_walk(void *opaque) 170560ce86c7SWei Liu { 170660ce86c7SWei Liu int name_idx; 170760ce86c7SWei Liu V9fsQID *qids = NULL; 170860ce86c7SWei Liu int i, err = 0; 170960ce86c7SWei Liu V9fsPath dpath, path; 171060ce86c7SWei Liu uint16_t nwnames; 171160ce86c7SWei Liu struct stat stbuf; 171260ce86c7SWei Liu size_t offset = 7; 171360ce86c7SWei Liu int32_t fid, newfid; 171460ce86c7SWei Liu V9fsString *wnames = NULL; 171560ce86c7SWei Liu V9fsFidState *fidp; 171660ce86c7SWei Liu V9fsFidState *newfidp = NULL; 171760ce86c7SWei Liu V9fsPDU *pdu = opaque; 171860ce86c7SWei Liu V9fsState *s = pdu->s; 171956f101ecSGreg Kurz V9fsQID qid; 172060ce86c7SWei Liu 172160ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames); 172260ce86c7SWei Liu if (err < 0) { 172360ce86c7SWei Liu pdu_complete(pdu, err); 172460ce86c7SWei Liu return ; 172560ce86c7SWei Liu } 172660ce86c7SWei Liu offset += err; 172760ce86c7SWei Liu 172860ce86c7SWei Liu trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames); 172960ce86c7SWei Liu 1730232a4d2cSChristian Schoenebeck if (nwnames > P9_MAXWELEM) { 1731232a4d2cSChristian Schoenebeck err = -EINVAL; 1732232a4d2cSChristian Schoenebeck goto out_nofid; 1733232a4d2cSChristian Schoenebeck } 1734232a4d2cSChristian Schoenebeck if (nwnames) { 17351923923bSGreg Kurz wnames = g_new0(V9fsString, nwnames); 17361923923bSGreg Kurz qids = g_new0(V9fsQID, nwnames); 173760ce86c7SWei Liu for (i = 0; i < nwnames; i++) { 173860ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "s", &wnames[i]); 173960ce86c7SWei Liu if (err < 0) { 174060ce86c7SWei Liu goto out_nofid; 174160ce86c7SWei Liu } 1742fff39a7aSGreg Kurz if (name_is_illegal(wnames[i].data)) { 1743fff39a7aSGreg Kurz err = -ENOENT; 1744fff39a7aSGreg Kurz goto out_nofid; 1745fff39a7aSGreg Kurz } 174660ce86c7SWei Liu offset += err; 174760ce86c7SWei Liu } 174860ce86c7SWei Liu } 174960ce86c7SWei Liu fidp = get_fid(pdu, fid); 175060ce86c7SWei Liu if (fidp == NULL) { 175160ce86c7SWei Liu err = -ENOENT; 175260ce86c7SWei Liu goto out_nofid; 175360ce86c7SWei Liu } 175456f101ecSGreg Kurz 175513fd08e6SGreg Kurz v9fs_path_init(&dpath); 175613fd08e6SGreg Kurz v9fs_path_init(&path); 175713fd08e6SGreg Kurz 17581d0fc0d0SChristian Schoenebeck err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); 17591d0fc0d0SChristian Schoenebeck if (err < 0) { 17601d0fc0d0SChristian Schoenebeck goto out; 17611d0fc0d0SChristian Schoenebeck } 17621d0fc0d0SChristian Schoenebeck err = stat_to_qid(pdu, &stbuf, &qid); 176356f101ecSGreg Kurz if (err < 0) { 176456f101ecSGreg Kurz goto out; 176556f101ecSGreg Kurz } 176656f101ecSGreg Kurz 176760ce86c7SWei Liu /* 176860ce86c7SWei Liu * Both dpath and path initially poin to fidp. 176960ce86c7SWei Liu * Needed to handle request with nwnames == 0 177060ce86c7SWei Liu */ 177160ce86c7SWei Liu v9fs_path_copy(&dpath, &fidp->path); 177260ce86c7SWei Liu v9fs_path_copy(&path, &fidp->path); 177360ce86c7SWei Liu for (name_idx = 0; name_idx < nwnames; name_idx++) { 1774*f22cad42SChristian Schoenebeck if (!same_stat_id(&pdu->s->root_st, &stbuf) || 177556f101ecSGreg Kurz strcmp("..", wnames[name_idx].data)) { 177656f101ecSGreg Kurz err = v9fs_co_name_to_path(pdu, &dpath, wnames[name_idx].data, 177756f101ecSGreg Kurz &path); 177860ce86c7SWei Liu if (err < 0) { 177960ce86c7SWei Liu goto out; 178060ce86c7SWei Liu } 178156f101ecSGreg Kurz 178260ce86c7SWei Liu err = v9fs_co_lstat(pdu, &path, &stbuf); 178360ce86c7SWei Liu if (err < 0) { 178460ce86c7SWei Liu goto out; 178560ce86c7SWei Liu } 17863b5ee9e8SAntonios Motakis err = stat_to_qid(pdu, &stbuf, &qid); 17873b5ee9e8SAntonios Motakis if (err < 0) { 17883b5ee9e8SAntonios Motakis goto out; 17893b5ee9e8SAntonios Motakis } 179060ce86c7SWei Liu v9fs_path_copy(&dpath, &path); 179160ce86c7SWei Liu } 179256f101ecSGreg Kurz memcpy(&qids[name_idx], &qid, sizeof(qid)); 179356f101ecSGreg Kurz } 179460ce86c7SWei Liu if (fid == newfid) { 179549dd946bSGreg Kurz if (fidp->fid_type != P9_FID_NONE) { 179649dd946bSGreg Kurz err = -EINVAL; 179749dd946bSGreg Kurz goto out; 179849dd946bSGreg Kurz } 17995b3c77aaSGreg Kurz v9fs_path_write_lock(s); 180060ce86c7SWei Liu v9fs_path_copy(&fidp->path, &path); 18015b3c77aaSGreg Kurz v9fs_path_unlock(s); 180260ce86c7SWei Liu } else { 180360ce86c7SWei Liu newfidp = alloc_fid(s, newfid); 180460ce86c7SWei Liu if (newfidp == NULL) { 180560ce86c7SWei Liu err = -EINVAL; 180660ce86c7SWei Liu goto out; 180760ce86c7SWei Liu } 180860ce86c7SWei Liu newfidp->uid = fidp->uid; 180960ce86c7SWei Liu v9fs_path_copy(&newfidp->path, &path); 181060ce86c7SWei Liu } 181160ce86c7SWei Liu err = v9fs_walk_marshal(pdu, nwnames, qids); 181260ce86c7SWei Liu trace_v9fs_walk_return(pdu->tag, pdu->id, nwnames, qids); 181360ce86c7SWei Liu out: 181460ce86c7SWei Liu put_fid(pdu, fidp); 181560ce86c7SWei Liu if (newfidp) { 181660ce86c7SWei Liu put_fid(pdu, newfidp); 181760ce86c7SWei Liu } 181860ce86c7SWei Liu v9fs_path_free(&dpath); 181960ce86c7SWei Liu v9fs_path_free(&path); 182060ce86c7SWei Liu out_nofid: 182160ce86c7SWei Liu pdu_complete(pdu, err); 182260ce86c7SWei Liu if (nwnames && nwnames <= P9_MAXWELEM) { 182360ce86c7SWei Liu for (name_idx = 0; name_idx < nwnames; name_idx++) { 182460ce86c7SWei Liu v9fs_string_free(&wnames[name_idx]); 182560ce86c7SWei Liu } 182660ce86c7SWei Liu g_free(wnames); 182760ce86c7SWei Liu g_free(qids); 182860ce86c7SWei Liu } 182960ce86c7SWei Liu } 183060ce86c7SWei Liu 18318440e22eSGreg Kurz static int32_t coroutine_fn get_iounit(V9fsPDU *pdu, V9fsPath *path) 183260ce86c7SWei Liu { 183360ce86c7SWei Liu struct statfs stbuf; 183460ce86c7SWei Liu int32_t iounit = 0; 183560ce86c7SWei Liu V9fsState *s = pdu->s; 183660ce86c7SWei Liu 183760ce86c7SWei Liu /* 183860ce86c7SWei Liu * iounit should be multiples of f_bsize (host filesystem block size 183960ce86c7SWei Liu * and as well as less than (client msize - P9_IOHDRSZ)) 184060ce86c7SWei Liu */ 184160ce86c7SWei Liu if (!v9fs_co_statfs(pdu, path, &stbuf)) { 184268d654daSDan Schatzberg if (stbuf.f_bsize) { 184360ce86c7SWei Liu iounit = stbuf.f_bsize; 184460ce86c7SWei Liu iounit *= (s->msize - P9_IOHDRSZ) / stbuf.f_bsize; 184560ce86c7SWei Liu } 184668d654daSDan Schatzberg } 184760ce86c7SWei Liu if (!iounit) { 184860ce86c7SWei Liu iounit = s->msize - P9_IOHDRSZ; 184960ce86c7SWei Liu } 185060ce86c7SWei Liu return iounit; 185160ce86c7SWei Liu } 185260ce86c7SWei Liu 18538440e22eSGreg Kurz static void coroutine_fn v9fs_open(void *opaque) 185460ce86c7SWei Liu { 185560ce86c7SWei Liu int flags; 185660ce86c7SWei Liu int32_t fid; 185760ce86c7SWei Liu int32_t mode; 185860ce86c7SWei Liu V9fsQID qid; 185960ce86c7SWei Liu int iounit = 0; 186060ce86c7SWei Liu ssize_t err = 0; 186160ce86c7SWei Liu size_t offset = 7; 186260ce86c7SWei Liu struct stat stbuf; 186360ce86c7SWei Liu V9fsFidState *fidp; 186460ce86c7SWei Liu V9fsPDU *pdu = opaque; 186560ce86c7SWei Liu V9fsState *s = pdu->s; 186660ce86c7SWei Liu 186760ce86c7SWei Liu if (s->proto_version == V9FS_PROTO_2000L) { 186860ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode); 186960ce86c7SWei Liu } else { 187060ce86c7SWei Liu uint8_t modebyte; 187160ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte); 187260ce86c7SWei Liu mode = modebyte; 187360ce86c7SWei Liu } 187460ce86c7SWei Liu if (err < 0) { 187560ce86c7SWei Liu goto out_nofid; 187660ce86c7SWei Liu } 187760ce86c7SWei Liu trace_v9fs_open(pdu->tag, pdu->id, fid, mode); 187860ce86c7SWei Liu 187960ce86c7SWei Liu fidp = get_fid(pdu, fid); 188060ce86c7SWei Liu if (fidp == NULL) { 188160ce86c7SWei Liu err = -ENOENT; 188260ce86c7SWei Liu goto out_nofid; 188360ce86c7SWei Liu } 188449dd946bSGreg Kurz if (fidp->fid_type != P9_FID_NONE) { 188549dd946bSGreg Kurz err = -EINVAL; 188649dd946bSGreg Kurz goto out; 188749dd946bSGreg Kurz } 188860ce86c7SWei Liu 188960ce86c7SWei Liu err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); 189060ce86c7SWei Liu if (err < 0) { 189160ce86c7SWei Liu goto out; 189260ce86c7SWei Liu } 18933b5ee9e8SAntonios Motakis err = stat_to_qid(pdu, &stbuf, &qid); 18943b5ee9e8SAntonios Motakis if (err < 0) { 18953b5ee9e8SAntonios Motakis goto out; 18963b5ee9e8SAntonios Motakis } 189760ce86c7SWei Liu if (S_ISDIR(stbuf.st_mode)) { 189860ce86c7SWei Liu err = v9fs_co_opendir(pdu, fidp); 189960ce86c7SWei Liu if (err < 0) { 190060ce86c7SWei Liu goto out; 190160ce86c7SWei Liu } 190260ce86c7SWei Liu fidp->fid_type = P9_FID_DIR; 190360ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Qd", &qid, 0); 190460ce86c7SWei Liu if (err < 0) { 190560ce86c7SWei Liu goto out; 190660ce86c7SWei Liu } 190760ce86c7SWei Liu err += offset; 190860ce86c7SWei Liu } else { 190960ce86c7SWei Liu if (s->proto_version == V9FS_PROTO_2000L) { 191060ce86c7SWei Liu flags = get_dotl_openflags(s, mode); 191160ce86c7SWei Liu } else { 191260ce86c7SWei Liu flags = omode_to_uflags(mode); 191360ce86c7SWei Liu } 191460ce86c7SWei Liu if (is_ro_export(&s->ctx)) { 191560ce86c7SWei Liu if (mode & O_WRONLY || mode & O_RDWR || 191660ce86c7SWei Liu mode & O_APPEND || mode & O_TRUNC) { 191760ce86c7SWei Liu err = -EROFS; 191860ce86c7SWei Liu goto out; 191960ce86c7SWei Liu } 192060ce86c7SWei Liu } 192160ce86c7SWei Liu err = v9fs_co_open(pdu, fidp, flags); 192260ce86c7SWei Liu if (err < 0) { 192360ce86c7SWei Liu goto out; 192460ce86c7SWei Liu } 192560ce86c7SWei Liu fidp->fid_type = P9_FID_FILE; 192660ce86c7SWei Liu fidp->open_flags = flags; 192760ce86c7SWei Liu if (flags & O_EXCL) { 192860ce86c7SWei Liu /* 192960ce86c7SWei Liu * We let the host file system do O_EXCL check 193060ce86c7SWei Liu * We should not reclaim such fd 193160ce86c7SWei Liu */ 193260ce86c7SWei Liu fidp->flags |= FID_NON_RECLAIMABLE; 193360ce86c7SWei Liu } 193460ce86c7SWei Liu iounit = get_iounit(pdu, &fidp->path); 193560ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Qd", &qid, iounit); 193660ce86c7SWei Liu if (err < 0) { 193760ce86c7SWei Liu goto out; 193860ce86c7SWei Liu } 193960ce86c7SWei Liu err += offset; 194060ce86c7SWei Liu } 194160ce86c7SWei Liu trace_v9fs_open_return(pdu->tag, pdu->id, 194260ce86c7SWei Liu qid.type, qid.version, qid.path, iounit); 194360ce86c7SWei Liu out: 194460ce86c7SWei Liu put_fid(pdu, fidp); 194560ce86c7SWei Liu out_nofid: 194660ce86c7SWei Liu pdu_complete(pdu, err); 194760ce86c7SWei Liu } 194860ce86c7SWei Liu 19498440e22eSGreg Kurz static void coroutine_fn v9fs_lcreate(void *opaque) 195060ce86c7SWei Liu { 195160ce86c7SWei Liu int32_t dfid, flags, mode; 195260ce86c7SWei Liu gid_t gid; 195360ce86c7SWei Liu ssize_t err = 0; 195460ce86c7SWei Liu ssize_t offset = 7; 195560ce86c7SWei Liu V9fsString name; 195660ce86c7SWei Liu V9fsFidState *fidp; 195760ce86c7SWei Liu struct stat stbuf; 195860ce86c7SWei Liu V9fsQID qid; 195960ce86c7SWei Liu int32_t iounit; 196060ce86c7SWei Liu V9fsPDU *pdu = opaque; 196160ce86c7SWei Liu 196260ce86c7SWei Liu v9fs_string_init(&name); 196360ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dsddd", &dfid, 196460ce86c7SWei Liu &name, &flags, &mode, &gid); 196560ce86c7SWei Liu if (err < 0) { 196660ce86c7SWei Liu goto out_nofid; 196760ce86c7SWei Liu } 196860ce86c7SWei Liu trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid); 196960ce86c7SWei Liu 1970fff39a7aSGreg Kurz if (name_is_illegal(name.data)) { 1971fff39a7aSGreg Kurz err = -ENOENT; 1972fff39a7aSGreg Kurz goto out_nofid; 1973fff39a7aSGreg Kurz } 1974fff39a7aSGreg Kurz 1975805b5d98SGreg Kurz if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 1976805b5d98SGreg Kurz err = -EEXIST; 1977805b5d98SGreg Kurz goto out_nofid; 1978805b5d98SGreg Kurz } 1979805b5d98SGreg Kurz 198060ce86c7SWei Liu fidp = get_fid(pdu, dfid); 198160ce86c7SWei Liu if (fidp == NULL) { 198260ce86c7SWei Liu err = -ENOENT; 198360ce86c7SWei Liu goto out_nofid; 198460ce86c7SWei Liu } 1985d63fb193SLi Qiang if (fidp->fid_type != P9_FID_NONE) { 1986d63fb193SLi Qiang err = -EINVAL; 1987d63fb193SLi Qiang goto out; 1988d63fb193SLi Qiang } 198960ce86c7SWei Liu 199060ce86c7SWei Liu flags = get_dotl_openflags(pdu->s, flags); 199160ce86c7SWei Liu err = v9fs_co_open2(pdu, fidp, &name, gid, 199260ce86c7SWei Liu flags | O_CREAT, mode, &stbuf); 199360ce86c7SWei Liu if (err < 0) { 199460ce86c7SWei Liu goto out; 199560ce86c7SWei Liu } 199660ce86c7SWei Liu fidp->fid_type = P9_FID_FILE; 199760ce86c7SWei Liu fidp->open_flags = flags; 199860ce86c7SWei Liu if (flags & O_EXCL) { 199960ce86c7SWei Liu /* 200060ce86c7SWei Liu * We let the host file system do O_EXCL check 200160ce86c7SWei Liu * We should not reclaim such fd 200260ce86c7SWei Liu */ 200360ce86c7SWei Liu fidp->flags |= FID_NON_RECLAIMABLE; 200460ce86c7SWei Liu } 200560ce86c7SWei Liu iounit = get_iounit(pdu, &fidp->path); 20063b5ee9e8SAntonios Motakis err = stat_to_qid(pdu, &stbuf, &qid); 20073b5ee9e8SAntonios Motakis if (err < 0) { 20083b5ee9e8SAntonios Motakis goto out; 20093b5ee9e8SAntonios Motakis } 201060ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Qd", &qid, iounit); 201160ce86c7SWei Liu if (err < 0) { 201260ce86c7SWei Liu goto out; 201360ce86c7SWei Liu } 201460ce86c7SWei Liu err += offset; 201560ce86c7SWei Liu trace_v9fs_lcreate_return(pdu->tag, pdu->id, 201660ce86c7SWei Liu qid.type, qid.version, qid.path, iounit); 201760ce86c7SWei Liu out: 201860ce86c7SWei Liu put_fid(pdu, fidp); 201960ce86c7SWei Liu out_nofid: 202060ce86c7SWei Liu pdu_complete(pdu, err); 202160ce86c7SWei Liu v9fs_string_free(&name); 202260ce86c7SWei Liu } 202360ce86c7SWei Liu 2024a1bf8b74SGreg Kurz static void coroutine_fn v9fs_fsync(void *opaque) 202560ce86c7SWei Liu { 202660ce86c7SWei Liu int err; 202760ce86c7SWei Liu int32_t fid; 202860ce86c7SWei Liu int datasync; 202960ce86c7SWei Liu size_t offset = 7; 203060ce86c7SWei Liu V9fsFidState *fidp; 203160ce86c7SWei Liu V9fsPDU *pdu = opaque; 203260ce86c7SWei Liu 203360ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync); 203460ce86c7SWei Liu if (err < 0) { 203560ce86c7SWei Liu goto out_nofid; 203660ce86c7SWei Liu } 203760ce86c7SWei Liu trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync); 203860ce86c7SWei Liu 203960ce86c7SWei Liu fidp = get_fid(pdu, fid); 204060ce86c7SWei Liu if (fidp == NULL) { 204160ce86c7SWei Liu err = -ENOENT; 204260ce86c7SWei Liu goto out_nofid; 204360ce86c7SWei Liu } 204460ce86c7SWei Liu err = v9fs_co_fsync(pdu, fidp, datasync); 204560ce86c7SWei Liu if (!err) { 204660ce86c7SWei Liu err = offset; 204760ce86c7SWei Liu } 204860ce86c7SWei Liu put_fid(pdu, fidp); 204960ce86c7SWei Liu out_nofid: 205060ce86c7SWei Liu pdu_complete(pdu, err); 205160ce86c7SWei Liu } 205260ce86c7SWei Liu 20538440e22eSGreg Kurz static void coroutine_fn v9fs_clunk(void *opaque) 205460ce86c7SWei Liu { 205560ce86c7SWei Liu int err; 205660ce86c7SWei Liu int32_t fid; 205760ce86c7SWei Liu size_t offset = 7; 205860ce86c7SWei Liu V9fsFidState *fidp; 205960ce86c7SWei Liu V9fsPDU *pdu = opaque; 206060ce86c7SWei Liu V9fsState *s = pdu->s; 206160ce86c7SWei Liu 206260ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "d", &fid); 206360ce86c7SWei Liu if (err < 0) { 206460ce86c7SWei Liu goto out_nofid; 206560ce86c7SWei Liu } 206660ce86c7SWei Liu trace_v9fs_clunk(pdu->tag, pdu->id, fid); 206760ce86c7SWei Liu 206860ce86c7SWei Liu fidp = clunk_fid(s, fid); 206960ce86c7SWei Liu if (fidp == NULL) { 207060ce86c7SWei Liu err = -ENOENT; 207160ce86c7SWei Liu goto out_nofid; 207260ce86c7SWei Liu } 207360ce86c7SWei Liu /* 207460ce86c7SWei Liu * Bump the ref so that put_fid will 207560ce86c7SWei Liu * free the fid. 207660ce86c7SWei Liu */ 207760ce86c7SWei Liu fidp->ref++; 207860ce86c7SWei Liu err = put_fid(pdu, fidp); 207960ce86c7SWei Liu if (!err) { 208060ce86c7SWei Liu err = offset; 208160ce86c7SWei Liu } 208260ce86c7SWei Liu out_nofid: 208360ce86c7SWei Liu pdu_complete(pdu, err); 208460ce86c7SWei Liu } 208560ce86c7SWei Liu 2086bcb8998fSStefano Stabellini /* 2087bcb8998fSStefano Stabellini * Create a QEMUIOVector for a sub-region of PDU iovecs 2088bcb8998fSStefano Stabellini * 2089bcb8998fSStefano Stabellini * @qiov: uninitialized QEMUIOVector 2090bcb8998fSStefano Stabellini * @skip: number of bytes to skip from beginning of PDU 2091bcb8998fSStefano Stabellini * @size: number of bytes to include 2092bcb8998fSStefano Stabellini * @is_write: true - write, false - read 2093bcb8998fSStefano Stabellini * 2094bcb8998fSStefano Stabellini * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up 2095bcb8998fSStefano Stabellini * with qemu_iovec_destroy(). 2096bcb8998fSStefano Stabellini */ 2097bcb8998fSStefano Stabellini static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu, 2098cf45183bSStefano Stabellini size_t skip, size_t size, 2099bcb8998fSStefano Stabellini bool is_write) 2100bcb8998fSStefano Stabellini { 2101bcb8998fSStefano Stabellini QEMUIOVector elem; 2102bcb8998fSStefano Stabellini struct iovec *iov; 2103bcb8998fSStefano Stabellini unsigned int niov; 2104bcb8998fSStefano Stabellini 210588da0b03SStefano Stabellini if (is_write) { 2106cf45183bSStefano Stabellini pdu->s->transport->init_out_iov_from_pdu(pdu, &iov, &niov, size + skip); 210788da0b03SStefano Stabellini } else { 2108cf45183bSStefano Stabellini pdu->s->transport->init_in_iov_from_pdu(pdu, &iov, &niov, size + skip); 210988da0b03SStefano Stabellini } 2110bcb8998fSStefano Stabellini 2111bcb8998fSStefano Stabellini qemu_iovec_init_external(&elem, iov, niov); 2112bcb8998fSStefano Stabellini qemu_iovec_init(qiov, niov); 2113cf45183bSStefano Stabellini qemu_iovec_concat(qiov, &elem, skip, size); 2114bcb8998fSStefano Stabellini } 2115bcb8998fSStefano Stabellini 211660ce86c7SWei Liu static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp, 211760ce86c7SWei Liu uint64_t off, uint32_t max_count) 211860ce86c7SWei Liu { 211960ce86c7SWei Liu ssize_t err; 212060ce86c7SWei Liu size_t offset = 7; 2121cf45183bSStefano Stabellini uint64_t read_count; 2122bcb8998fSStefano Stabellini QEMUIOVector qiov_full; 212360ce86c7SWei Liu 21247e55d65cSLi Qiang if (fidp->fs.xattr.len < off) { 21257e55d65cSLi Qiang read_count = 0; 212616724a17SGreg Kurz } else { 2127cf45183bSStefano Stabellini read_count = fidp->fs.xattr.len - off; 2128cf45183bSStefano Stabellini } 2129cf45183bSStefano Stabellini if (read_count > max_count) { 213060ce86c7SWei Liu read_count = max_count; 213160ce86c7SWei Liu } 213260ce86c7SWei Liu err = pdu_marshal(pdu, offset, "d", read_count); 213360ce86c7SWei Liu if (err < 0) { 213460ce86c7SWei Liu return err; 213560ce86c7SWei Liu } 213660ce86c7SWei Liu offset += err; 213700588a0aSWei Liu 2138cf45183bSStefano Stabellini v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, read_count, false); 2139fa0eb5c5SGreg Kurz err = v9fs_pack(qiov_full.iov, qiov_full.niov, 0, 214060ce86c7SWei Liu ((char *)fidp->fs.xattr.value) + off, 214160ce86c7SWei Liu read_count); 2142bcb8998fSStefano Stabellini qemu_iovec_destroy(&qiov_full); 214360ce86c7SWei Liu if (err < 0) { 214460ce86c7SWei Liu return err; 214560ce86c7SWei Liu } 214660ce86c7SWei Liu offset += err; 214760ce86c7SWei Liu return offset; 214860ce86c7SWei Liu } 214960ce86c7SWei Liu 21508440e22eSGreg Kurz static int coroutine_fn v9fs_do_readdir_with_stat(V9fsPDU *pdu, 21518440e22eSGreg Kurz V9fsFidState *fidp, 21528440e22eSGreg Kurz uint32_t max_count) 215360ce86c7SWei Liu { 215460ce86c7SWei Liu V9fsPath path; 215560ce86c7SWei Liu V9fsStat v9stat; 215660ce86c7SWei Liu int len, err = 0; 215760ce86c7SWei Liu int32_t count = 0; 215860ce86c7SWei Liu struct stat stbuf; 215960ce86c7SWei Liu off_t saved_dir_pos; 2160635324e8SGreg Kurz struct dirent *dent; 216160ce86c7SWei Liu 216260ce86c7SWei Liu /* save the directory position */ 216360ce86c7SWei Liu saved_dir_pos = v9fs_co_telldir(pdu, fidp); 216460ce86c7SWei Liu if (saved_dir_pos < 0) { 216560ce86c7SWei Liu return saved_dir_pos; 216660ce86c7SWei Liu } 216760ce86c7SWei Liu 216860ce86c7SWei Liu while (1) { 216960ce86c7SWei Liu v9fs_path_init(&path); 21707cde47d4SGreg Kurz 21717cde47d4SGreg Kurz v9fs_readdir_lock(&fidp->fs.dir); 21727cde47d4SGreg Kurz 2173635324e8SGreg Kurz err = v9fs_co_readdir(pdu, fidp, &dent); 2174635324e8SGreg Kurz if (err || !dent) { 217560ce86c7SWei Liu break; 217660ce86c7SWei Liu } 217760ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path); 217860ce86c7SWei Liu if (err < 0) { 21798762a46dSGreg Kurz break; 218060ce86c7SWei Liu } 218160ce86c7SWei Liu err = v9fs_co_lstat(pdu, &path, &stbuf); 218260ce86c7SWei Liu if (err < 0) { 21838762a46dSGreg Kurz break; 218460ce86c7SWei Liu } 21856069537fSJan Dakinevich err = stat_to_v9stat(pdu, &path, dent->d_name, &stbuf, &v9stat); 218660ce86c7SWei Liu if (err < 0) { 21878762a46dSGreg Kurz break; 218860ce86c7SWei Liu } 2189772a7369SJan Dakinevich if ((count + v9stat.size + 2) > max_count) { 21907cde47d4SGreg Kurz v9fs_readdir_unlock(&fidp->fs.dir); 21917cde47d4SGreg Kurz 219260ce86c7SWei Liu /* Ran out of buffer. Set dir back to old position and return */ 219360ce86c7SWei Liu v9fs_co_seekdir(pdu, fidp, saved_dir_pos); 219460ce86c7SWei Liu v9fs_stat_free(&v9stat); 219560ce86c7SWei Liu v9fs_path_free(&path); 219660ce86c7SWei Liu return count; 219760ce86c7SWei Liu } 2198772a7369SJan Dakinevich 2199772a7369SJan Dakinevich /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */ 2200772a7369SJan Dakinevich len = pdu_marshal(pdu, 11 + count, "S", &v9stat); 2201772a7369SJan Dakinevich 2202772a7369SJan Dakinevich v9fs_readdir_unlock(&fidp->fs.dir); 2203772a7369SJan Dakinevich 2204772a7369SJan Dakinevich if (len < 0) { 2205772a7369SJan Dakinevich v9fs_co_seekdir(pdu, fidp, saved_dir_pos); 2206772a7369SJan Dakinevich v9fs_stat_free(&v9stat); 2207772a7369SJan Dakinevich v9fs_path_free(&path); 2208772a7369SJan Dakinevich return len; 2209772a7369SJan Dakinevich } 221060ce86c7SWei Liu count += len; 221160ce86c7SWei Liu v9fs_stat_free(&v9stat); 221260ce86c7SWei Liu v9fs_path_free(&path); 221360ce86c7SWei Liu saved_dir_pos = dent->d_off; 221460ce86c7SWei Liu } 22158762a46dSGreg Kurz 22167cde47d4SGreg Kurz v9fs_readdir_unlock(&fidp->fs.dir); 22177cde47d4SGreg Kurz 221860ce86c7SWei Liu v9fs_path_free(&path); 221960ce86c7SWei Liu if (err < 0) { 222060ce86c7SWei Liu return err; 222160ce86c7SWei Liu } 222260ce86c7SWei Liu return count; 222360ce86c7SWei Liu } 222460ce86c7SWei Liu 22258440e22eSGreg Kurz static void coroutine_fn v9fs_read(void *opaque) 222660ce86c7SWei Liu { 222760ce86c7SWei Liu int32_t fid; 222860ce86c7SWei Liu uint64_t off; 222960ce86c7SWei Liu ssize_t err = 0; 223060ce86c7SWei Liu int32_t count = 0; 223160ce86c7SWei Liu size_t offset = 7; 223260ce86c7SWei Liu uint32_t max_count; 223360ce86c7SWei Liu V9fsFidState *fidp; 223460ce86c7SWei Liu V9fsPDU *pdu = opaque; 223560ce86c7SWei Liu V9fsState *s = pdu->s; 223660ce86c7SWei Liu 223760ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count); 223860ce86c7SWei Liu if (err < 0) { 223960ce86c7SWei Liu goto out_nofid; 224060ce86c7SWei Liu } 224160ce86c7SWei Liu trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count); 224260ce86c7SWei Liu 224360ce86c7SWei Liu fidp = get_fid(pdu, fid); 224460ce86c7SWei Liu if (fidp == NULL) { 224560ce86c7SWei Liu err = -EINVAL; 224660ce86c7SWei Liu goto out_nofid; 224760ce86c7SWei Liu } 224860ce86c7SWei Liu if (fidp->fid_type == P9_FID_DIR) { 2249d2c5cf7cSChristian Schoenebeck if (s->proto_version != V9FS_PROTO_2000U) { 2250d2c5cf7cSChristian Schoenebeck warn_report_once( 2251d2c5cf7cSChristian Schoenebeck "9p: bad client: T_read request on directory only expected " 2252d2c5cf7cSChristian Schoenebeck "with 9P2000.u protocol version" 2253d2c5cf7cSChristian Schoenebeck ); 2254d2c5cf7cSChristian Schoenebeck err = -EOPNOTSUPP; 2255d2c5cf7cSChristian Schoenebeck goto out; 2256d2c5cf7cSChristian Schoenebeck } 225760ce86c7SWei Liu if (off == 0) { 225860ce86c7SWei Liu v9fs_co_rewinddir(pdu, fidp); 225960ce86c7SWei Liu } 226060ce86c7SWei Liu count = v9fs_do_readdir_with_stat(pdu, fidp, max_count); 226160ce86c7SWei Liu if (count < 0) { 226260ce86c7SWei Liu err = count; 226360ce86c7SWei Liu goto out; 226460ce86c7SWei Liu } 226560ce86c7SWei Liu err = pdu_marshal(pdu, offset, "d", count); 226660ce86c7SWei Liu if (err < 0) { 226760ce86c7SWei Liu goto out; 226860ce86c7SWei Liu } 226960ce86c7SWei Liu err += offset + count; 227060ce86c7SWei Liu } else if (fidp->fid_type == P9_FID_FILE) { 227160ce86c7SWei Liu QEMUIOVector qiov_full; 227260ce86c7SWei Liu QEMUIOVector qiov; 227360ce86c7SWei Liu int32_t len; 227460ce86c7SWei Liu 2275cf45183bSStefano Stabellini v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, max_count, false); 227660ce86c7SWei Liu qemu_iovec_init(&qiov, qiov_full.niov); 227760ce86c7SWei Liu do { 227860ce86c7SWei Liu qemu_iovec_reset(&qiov); 227960ce86c7SWei Liu qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count); 228060ce86c7SWei Liu if (0) { 228160ce86c7SWei Liu print_sg(qiov.iov, qiov.niov); 228260ce86c7SWei Liu } 228360ce86c7SWei Liu /* Loop in case of EINTR */ 228460ce86c7SWei Liu do { 228560ce86c7SWei Liu len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off); 228660ce86c7SWei Liu if (len >= 0) { 228760ce86c7SWei Liu off += len; 228860ce86c7SWei Liu count += len; 228960ce86c7SWei Liu } 229060ce86c7SWei Liu } while (len == -EINTR && !pdu->cancelled); 229160ce86c7SWei Liu if (len < 0) { 229260ce86c7SWei Liu /* IO error return the error */ 229360ce86c7SWei Liu err = len; 2294e95c9a49SLi Qiang goto out_free_iovec; 229560ce86c7SWei Liu } 229660ce86c7SWei Liu } while (count < max_count && len > 0); 229760ce86c7SWei Liu err = pdu_marshal(pdu, offset, "d", count); 229860ce86c7SWei Liu if (err < 0) { 2299e95c9a49SLi Qiang goto out_free_iovec; 230060ce86c7SWei Liu } 230160ce86c7SWei Liu err += offset + count; 2302e95c9a49SLi Qiang out_free_iovec: 230360ce86c7SWei Liu qemu_iovec_destroy(&qiov); 230460ce86c7SWei Liu qemu_iovec_destroy(&qiov_full); 230560ce86c7SWei Liu } else if (fidp->fid_type == P9_FID_XATTR) { 230660ce86c7SWei Liu err = v9fs_xattr_read(s, pdu, fidp, off, max_count); 230760ce86c7SWei Liu } else { 230860ce86c7SWei Liu err = -EINVAL; 230960ce86c7SWei Liu } 231060ce86c7SWei Liu trace_v9fs_read_return(pdu->tag, pdu->id, count, err); 231160ce86c7SWei Liu out: 231260ce86c7SWei Liu put_fid(pdu, fidp); 231360ce86c7SWei Liu out_nofid: 231460ce86c7SWei Liu pdu_complete(pdu, err); 231560ce86c7SWei Liu } 231660ce86c7SWei Liu 231729c9d2caSChristian Schoenebeck /** 231829c9d2caSChristian Schoenebeck * Returns size required in Rreaddir response for the passed dirent @p name. 231929c9d2caSChristian Schoenebeck * 232029c9d2caSChristian Schoenebeck * @param name - directory entry's name (i.e. file name, directory name) 232129c9d2caSChristian Schoenebeck * @returns required size in bytes 232229c9d2caSChristian Schoenebeck */ 232329c9d2caSChristian Schoenebeck size_t v9fs_readdir_response_size(V9fsString *name) 232460ce86c7SWei Liu { 232560ce86c7SWei Liu /* 232660ce86c7SWei Liu * Size of each dirent on the wire: size of qid (13) + size of offset (8) 232760ce86c7SWei Liu * size of type (1) + size of name.size (2) + strlen(name.data) 232860ce86c7SWei Liu */ 232960ce86c7SWei Liu return 24 + v9fs_string_size(name); 233060ce86c7SWei Liu } 233160ce86c7SWei Liu 23320c4356baSChristian Schoenebeck static void v9fs_free_dirents(struct V9fsDirEnt *e) 23330c4356baSChristian Schoenebeck { 23340c4356baSChristian Schoenebeck struct V9fsDirEnt *next = NULL; 23350c4356baSChristian Schoenebeck 23360c4356baSChristian Schoenebeck for (; e; e = next) { 23370c4356baSChristian Schoenebeck next = e->next; 23380c4356baSChristian Schoenebeck g_free(e->dent); 23390c4356baSChristian Schoenebeck g_free(e->st); 23400c4356baSChristian Schoenebeck g_free(e); 23410c4356baSChristian Schoenebeck } 23420c4356baSChristian Schoenebeck } 23430c4356baSChristian Schoenebeck 23448440e22eSGreg Kurz static int coroutine_fn v9fs_do_readdir(V9fsPDU *pdu, V9fsFidState *fidp, 23450c4356baSChristian Schoenebeck off_t offset, int32_t max_count) 234660ce86c7SWei Liu { 234760ce86c7SWei Liu size_t size; 234860ce86c7SWei Liu V9fsQID qid; 234960ce86c7SWei Liu V9fsString name; 235060ce86c7SWei Liu int len, err = 0; 235160ce86c7SWei Liu int32_t count = 0; 2352635324e8SGreg Kurz struct dirent *dent; 23530c4356baSChristian Schoenebeck struct stat *st; 23540c4356baSChristian Schoenebeck struct V9fsDirEnt *entries = NULL; 235560ce86c7SWei Liu 23560c4356baSChristian Schoenebeck /* 23570c4356baSChristian Schoenebeck * inode remapping requires the device id, which in turn might be 23580c4356baSChristian Schoenebeck * different for different directory entries, so if inode remapping is 23590c4356baSChristian Schoenebeck * enabled we have to make a full stat for each directory entry 23600c4356baSChristian Schoenebeck */ 23610c4356baSChristian Schoenebeck const bool dostat = pdu->s->ctx.export_flags & V9FS_REMAP_INODES; 23620c4356baSChristian Schoenebeck 23630c4356baSChristian Schoenebeck /* 23640c4356baSChristian Schoenebeck * Fetch all required directory entries altogether on a background IO 23650c4356baSChristian Schoenebeck * thread from fs driver. We don't want to do that for each entry 23660c4356baSChristian Schoenebeck * individually, because hopping between threads (this main IO thread 23670c4356baSChristian Schoenebeck * and background IO driver thread) would sum up to huge latencies. 23680c4356baSChristian Schoenebeck */ 23690c4356baSChristian Schoenebeck count = v9fs_co_readdir_many(pdu, fidp, &entries, offset, max_count, 23700c4356baSChristian Schoenebeck dostat); 23710c4356baSChristian Schoenebeck if (count < 0) { 23720c4356baSChristian Schoenebeck err = count; 23730c4356baSChristian Schoenebeck count = 0; 23740c4356baSChristian Schoenebeck goto out; 237560ce86c7SWei Liu } 23760c4356baSChristian Schoenebeck count = 0; 237760ce86c7SWei Liu 23780c4356baSChristian Schoenebeck for (struct V9fsDirEnt *e = entries; e; e = e->next) { 23790c4356baSChristian Schoenebeck dent = e->dent; 23801a6ed33cSAntonios Motakis 23811a6ed33cSAntonios Motakis if (pdu->s->ctx.export_flags & V9FS_REMAP_INODES) { 23820c4356baSChristian Schoenebeck st = e->st; 23830c4356baSChristian Schoenebeck /* e->st should never be NULL, but just to be sure */ 23840c4356baSChristian Schoenebeck if (!st) { 23850c4356baSChristian Schoenebeck err = -1; 23860c4356baSChristian Schoenebeck break; 23870c4356baSChristian Schoenebeck } 23880c4356baSChristian Schoenebeck 23890c4356baSChristian Schoenebeck /* remap inode */ 23900c4356baSChristian Schoenebeck err = stat_to_qid(pdu, st, &qid); 23911a6ed33cSAntonios Motakis if (err < 0) { 23920c4356baSChristian Schoenebeck break; 23931a6ed33cSAntonios Motakis } 23941a6ed33cSAntonios Motakis } else { 239560ce86c7SWei Liu /* 239660ce86c7SWei Liu * Fill up just the path field of qid because the client uses 239760ce86c7SWei Liu * only that. To fill the entire qid structure we will have 23981a6ed33cSAntonios Motakis * to stat each dirent found, which is expensive. For the 23990c4356baSChristian Schoenebeck * latter reason we don't call stat_to_qid() here. Only drawback 24001a6ed33cSAntonios Motakis * is that no multi-device export detection of stat_to_qid() 24011a6ed33cSAntonios Motakis * would be done and provided as error to the user here. But 24021a6ed33cSAntonios Motakis * user would get that error anyway when accessing those 24031a6ed33cSAntonios Motakis * files/dirs through other ways. 240460ce86c7SWei Liu */ 240560ce86c7SWei Liu size = MIN(sizeof(dent->d_ino), sizeof(qid.path)); 240660ce86c7SWei Liu memcpy(&qid.path, &dent->d_ino, size); 240760ce86c7SWei Liu /* Fill the other fields with dummy values */ 240860ce86c7SWei Liu qid.type = 0; 240960ce86c7SWei Liu qid.version = 0; 24101a6ed33cSAntonios Motakis } 241160ce86c7SWei Liu 24120c4356baSChristian Schoenebeck v9fs_string_init(&name); 24130c4356baSChristian Schoenebeck v9fs_string_sprintf(&name, "%s", dent->d_name); 24140c4356baSChristian Schoenebeck 241560ce86c7SWei Liu /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */ 241660ce86c7SWei Liu len = pdu_marshal(pdu, 11 + count, "Qqbs", 241760ce86c7SWei Liu &qid, dent->d_off, 241860ce86c7SWei Liu dent->d_type, &name); 24197cde47d4SGreg Kurz 24200c4356baSChristian Schoenebeck v9fs_string_free(&name); 24217cde47d4SGreg Kurz 242260ce86c7SWei Liu if (len < 0) { 24230c4356baSChristian Schoenebeck err = len; 24240c4356baSChristian Schoenebeck break; 242560ce86c7SWei Liu } 24260c4356baSChristian Schoenebeck 242760ce86c7SWei Liu count += len; 242860ce86c7SWei Liu } 24297cde47d4SGreg Kurz 24300c4356baSChristian Schoenebeck out: 24310c4356baSChristian Schoenebeck v9fs_free_dirents(entries); 243260ce86c7SWei Liu if (err < 0) { 243360ce86c7SWei Liu return err; 243460ce86c7SWei Liu } 243560ce86c7SWei Liu return count; 243660ce86c7SWei Liu } 243760ce86c7SWei Liu 24388440e22eSGreg Kurz static void coroutine_fn v9fs_readdir(void *opaque) 243960ce86c7SWei Liu { 244060ce86c7SWei Liu int32_t fid; 244160ce86c7SWei Liu V9fsFidState *fidp; 244260ce86c7SWei Liu ssize_t retval = 0; 244360ce86c7SWei Liu size_t offset = 7; 244460ce86c7SWei Liu uint64_t initial_offset; 244560ce86c7SWei Liu int32_t count; 244660ce86c7SWei Liu uint32_t max_count; 244760ce86c7SWei Liu V9fsPDU *pdu = opaque; 2448d36a5c22SChristian Schoenebeck V9fsState *s = pdu->s; 244960ce86c7SWei Liu 245060ce86c7SWei Liu retval = pdu_unmarshal(pdu, offset, "dqd", &fid, 245160ce86c7SWei Liu &initial_offset, &max_count); 245260ce86c7SWei Liu if (retval < 0) { 245360ce86c7SWei Liu goto out_nofid; 245460ce86c7SWei Liu } 245560ce86c7SWei Liu trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count); 245660ce86c7SWei Liu 2457d36a5c22SChristian Schoenebeck /* Enough space for a R_readdir header: size[4] Rreaddir tag[2] count[4] */ 2458d36a5c22SChristian Schoenebeck if (max_count > s->msize - 11) { 2459d36a5c22SChristian Schoenebeck max_count = s->msize - 11; 2460d36a5c22SChristian Schoenebeck warn_report_once( 2461d36a5c22SChristian Schoenebeck "9p: bad client: T_readdir with count > msize - 11" 2462d36a5c22SChristian Schoenebeck ); 2463d36a5c22SChristian Schoenebeck } 2464d36a5c22SChristian Schoenebeck 246560ce86c7SWei Liu fidp = get_fid(pdu, fid); 246660ce86c7SWei Liu if (fidp == NULL) { 246760ce86c7SWei Liu retval = -EINVAL; 246860ce86c7SWei Liu goto out_nofid; 246960ce86c7SWei Liu } 2470f314ea4eSGreg Kurz if (!fidp->fs.dir.stream) { 247160ce86c7SWei Liu retval = -EINVAL; 247260ce86c7SWei Liu goto out; 247360ce86c7SWei Liu } 2474d2c5cf7cSChristian Schoenebeck if (s->proto_version != V9FS_PROTO_2000L) { 2475d2c5cf7cSChristian Schoenebeck warn_report_once( 2476d2c5cf7cSChristian Schoenebeck "9p: bad client: T_readdir request only expected with 9P2000.L " 2477d2c5cf7cSChristian Schoenebeck "protocol version" 2478d2c5cf7cSChristian Schoenebeck ); 2479d2c5cf7cSChristian Schoenebeck retval = -EOPNOTSUPP; 2480d2c5cf7cSChristian Schoenebeck goto out; 2481d2c5cf7cSChristian Schoenebeck } 24820c4356baSChristian Schoenebeck count = v9fs_do_readdir(pdu, fidp, (off_t) initial_offset, max_count); 248360ce86c7SWei Liu if (count < 0) { 248460ce86c7SWei Liu retval = count; 248560ce86c7SWei Liu goto out; 248660ce86c7SWei Liu } 248760ce86c7SWei Liu retval = pdu_marshal(pdu, offset, "d", count); 248860ce86c7SWei Liu if (retval < 0) { 248960ce86c7SWei Liu goto out; 249060ce86c7SWei Liu } 249160ce86c7SWei Liu retval += count + offset; 249260ce86c7SWei Liu trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval); 249360ce86c7SWei Liu out: 249460ce86c7SWei Liu put_fid(pdu, fidp); 249560ce86c7SWei Liu out_nofid: 249660ce86c7SWei Liu pdu_complete(pdu, retval); 249760ce86c7SWei Liu } 249860ce86c7SWei Liu 249960ce86c7SWei Liu static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp, 250060ce86c7SWei Liu uint64_t off, uint32_t count, 250160ce86c7SWei Liu struct iovec *sg, int cnt) 250260ce86c7SWei Liu { 250360ce86c7SWei Liu int i, to_copy; 250460ce86c7SWei Liu ssize_t err = 0; 25057e55d65cSLi Qiang uint64_t write_count; 250660ce86c7SWei Liu size_t offset = 7; 250760ce86c7SWei Liu 250860ce86c7SWei Liu 25097e55d65cSLi Qiang if (fidp->fs.xattr.len < off) { 2510b858e80aSDaniel Henrique Barboza return -ENOSPC; 251160ce86c7SWei Liu } 25127e55d65cSLi Qiang write_count = fidp->fs.xattr.len - off; 25137e55d65cSLi Qiang if (write_count > count) { 25147e55d65cSLi Qiang write_count = count; 25157e55d65cSLi Qiang } 251660ce86c7SWei Liu err = pdu_marshal(pdu, offset, "d", write_count); 251760ce86c7SWei Liu if (err < 0) { 251860ce86c7SWei Liu return err; 251960ce86c7SWei Liu } 252060ce86c7SWei Liu err += offset; 252160ce86c7SWei Liu fidp->fs.xattr.copied_len += write_count; 252260ce86c7SWei Liu /* 252360ce86c7SWei Liu * Now copy the content from sg list 252460ce86c7SWei Liu */ 252560ce86c7SWei Liu for (i = 0; i < cnt; i++) { 252660ce86c7SWei Liu if (write_count > sg[i].iov_len) { 252760ce86c7SWei Liu to_copy = sg[i].iov_len; 252860ce86c7SWei Liu } else { 252960ce86c7SWei Liu to_copy = write_count; 253060ce86c7SWei Liu } 253160ce86c7SWei Liu memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy); 253260ce86c7SWei Liu /* updating vs->off since we are not using below */ 253360ce86c7SWei Liu off += to_copy; 253460ce86c7SWei Liu write_count -= to_copy; 253560ce86c7SWei Liu } 2536b858e80aSDaniel Henrique Barboza 253760ce86c7SWei Liu return err; 253860ce86c7SWei Liu } 253960ce86c7SWei Liu 25408440e22eSGreg Kurz static void coroutine_fn v9fs_write(void *opaque) 254160ce86c7SWei Liu { 254260ce86c7SWei Liu ssize_t err; 254360ce86c7SWei Liu int32_t fid; 254460ce86c7SWei Liu uint64_t off; 254560ce86c7SWei Liu uint32_t count; 254660ce86c7SWei Liu int32_t len = 0; 254760ce86c7SWei Liu int32_t total = 0; 254860ce86c7SWei Liu size_t offset = 7; 254960ce86c7SWei Liu V9fsFidState *fidp; 255060ce86c7SWei Liu V9fsPDU *pdu = opaque; 255160ce86c7SWei Liu V9fsState *s = pdu->s; 255260ce86c7SWei Liu QEMUIOVector qiov_full; 255360ce86c7SWei Liu QEMUIOVector qiov; 255460ce86c7SWei Liu 255560ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count); 255660ce86c7SWei Liu if (err < 0) { 255760ce86c7SWei Liu pdu_complete(pdu, err); 255860ce86c7SWei Liu return; 255960ce86c7SWei Liu } 256060ce86c7SWei Liu offset += err; 2561cf45183bSStefano Stabellini v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, count, true); 256260ce86c7SWei Liu trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov); 256360ce86c7SWei Liu 256460ce86c7SWei Liu fidp = get_fid(pdu, fid); 256560ce86c7SWei Liu if (fidp == NULL) { 256660ce86c7SWei Liu err = -EINVAL; 256760ce86c7SWei Liu goto out_nofid; 256860ce86c7SWei Liu } 256960ce86c7SWei Liu if (fidp->fid_type == P9_FID_FILE) { 257060ce86c7SWei Liu if (fidp->fs.fd == -1) { 257160ce86c7SWei Liu err = -EINVAL; 257260ce86c7SWei Liu goto out; 257360ce86c7SWei Liu } 257460ce86c7SWei Liu } else if (fidp->fid_type == P9_FID_XATTR) { 257560ce86c7SWei Liu /* 257660ce86c7SWei Liu * setxattr operation 257760ce86c7SWei Liu */ 257860ce86c7SWei Liu err = v9fs_xattr_write(s, pdu, fidp, off, count, 257960ce86c7SWei Liu qiov_full.iov, qiov_full.niov); 258060ce86c7SWei Liu goto out; 258160ce86c7SWei Liu } else { 258260ce86c7SWei Liu err = -EINVAL; 258360ce86c7SWei Liu goto out; 258460ce86c7SWei Liu } 258560ce86c7SWei Liu qemu_iovec_init(&qiov, qiov_full.niov); 258660ce86c7SWei Liu do { 258760ce86c7SWei Liu qemu_iovec_reset(&qiov); 258860ce86c7SWei Liu qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total); 258960ce86c7SWei Liu if (0) { 259060ce86c7SWei Liu print_sg(qiov.iov, qiov.niov); 259160ce86c7SWei Liu } 259260ce86c7SWei Liu /* Loop in case of EINTR */ 259360ce86c7SWei Liu do { 259460ce86c7SWei Liu len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off); 259560ce86c7SWei Liu if (len >= 0) { 259660ce86c7SWei Liu off += len; 259760ce86c7SWei Liu total += len; 259860ce86c7SWei Liu } 259960ce86c7SWei Liu } while (len == -EINTR && !pdu->cancelled); 260060ce86c7SWei Liu if (len < 0) { 260160ce86c7SWei Liu /* IO error return the error */ 260260ce86c7SWei Liu err = len; 260360ce86c7SWei Liu goto out_qiov; 260460ce86c7SWei Liu } 260560ce86c7SWei Liu } while (total < count && len > 0); 260660ce86c7SWei Liu 260760ce86c7SWei Liu offset = 7; 260860ce86c7SWei Liu err = pdu_marshal(pdu, offset, "d", total); 260960ce86c7SWei Liu if (err < 0) { 2610fdfcc9aeSLi Qiang goto out_qiov; 261160ce86c7SWei Liu } 261260ce86c7SWei Liu err += offset; 261360ce86c7SWei Liu trace_v9fs_write_return(pdu->tag, pdu->id, total, err); 261460ce86c7SWei Liu out_qiov: 261560ce86c7SWei Liu qemu_iovec_destroy(&qiov); 261660ce86c7SWei Liu out: 261760ce86c7SWei Liu put_fid(pdu, fidp); 261860ce86c7SWei Liu out_nofid: 261960ce86c7SWei Liu qemu_iovec_destroy(&qiov_full); 262060ce86c7SWei Liu pdu_complete(pdu, err); 262160ce86c7SWei Liu } 262260ce86c7SWei Liu 26238440e22eSGreg Kurz static void coroutine_fn v9fs_create(void *opaque) 262460ce86c7SWei Liu { 262560ce86c7SWei Liu int32_t fid; 262660ce86c7SWei Liu int err = 0; 262760ce86c7SWei Liu size_t offset = 7; 262860ce86c7SWei Liu V9fsFidState *fidp; 262960ce86c7SWei Liu V9fsQID qid; 263060ce86c7SWei Liu int32_t perm; 263160ce86c7SWei Liu int8_t mode; 263260ce86c7SWei Liu V9fsPath path; 263360ce86c7SWei Liu struct stat stbuf; 263460ce86c7SWei Liu V9fsString name; 263560ce86c7SWei Liu V9fsString extension; 263660ce86c7SWei Liu int iounit; 263760ce86c7SWei Liu V9fsPDU *pdu = opaque; 26385b3c77aaSGreg Kurz V9fsState *s = pdu->s; 263960ce86c7SWei Liu 264060ce86c7SWei Liu v9fs_path_init(&path); 264160ce86c7SWei Liu v9fs_string_init(&name); 264260ce86c7SWei Liu v9fs_string_init(&extension); 264360ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name, 264460ce86c7SWei Liu &perm, &mode, &extension); 264560ce86c7SWei Liu if (err < 0) { 264660ce86c7SWei Liu goto out_nofid; 264760ce86c7SWei Liu } 264860ce86c7SWei Liu trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode); 264960ce86c7SWei Liu 2650fff39a7aSGreg Kurz if (name_is_illegal(name.data)) { 2651fff39a7aSGreg Kurz err = -ENOENT; 2652fff39a7aSGreg Kurz goto out_nofid; 2653fff39a7aSGreg Kurz } 2654fff39a7aSGreg Kurz 2655805b5d98SGreg Kurz if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 2656805b5d98SGreg Kurz err = -EEXIST; 2657805b5d98SGreg Kurz goto out_nofid; 2658805b5d98SGreg Kurz } 2659805b5d98SGreg Kurz 266060ce86c7SWei Liu fidp = get_fid(pdu, fid); 266160ce86c7SWei Liu if (fidp == NULL) { 266260ce86c7SWei Liu err = -EINVAL; 266360ce86c7SWei Liu goto out_nofid; 266460ce86c7SWei Liu } 2665d63fb193SLi Qiang if (fidp->fid_type != P9_FID_NONE) { 2666d63fb193SLi Qiang err = -EINVAL; 2667d63fb193SLi Qiang goto out; 2668d63fb193SLi Qiang } 266960ce86c7SWei Liu if (perm & P9_STAT_MODE_DIR) { 267060ce86c7SWei Liu err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777, 267160ce86c7SWei Liu fidp->uid, -1, &stbuf); 267260ce86c7SWei Liu if (err < 0) { 267360ce86c7SWei Liu goto out; 267460ce86c7SWei Liu } 267560ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); 267660ce86c7SWei Liu if (err < 0) { 267760ce86c7SWei Liu goto out; 267860ce86c7SWei Liu } 26795b3c77aaSGreg Kurz v9fs_path_write_lock(s); 268060ce86c7SWei Liu v9fs_path_copy(&fidp->path, &path); 26815b3c77aaSGreg Kurz v9fs_path_unlock(s); 268260ce86c7SWei Liu err = v9fs_co_opendir(pdu, fidp); 268360ce86c7SWei Liu if (err < 0) { 268460ce86c7SWei Liu goto out; 268560ce86c7SWei Liu } 268660ce86c7SWei Liu fidp->fid_type = P9_FID_DIR; 268760ce86c7SWei Liu } else if (perm & P9_STAT_MODE_SYMLINK) { 268860ce86c7SWei Liu err = v9fs_co_symlink(pdu, fidp, &name, 268960ce86c7SWei Liu extension.data, -1 , &stbuf); 269060ce86c7SWei Liu if (err < 0) { 269160ce86c7SWei Liu goto out; 269260ce86c7SWei Liu } 269360ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); 269460ce86c7SWei Liu if (err < 0) { 269560ce86c7SWei Liu goto out; 269660ce86c7SWei Liu } 26975b3c77aaSGreg Kurz v9fs_path_write_lock(s); 269860ce86c7SWei Liu v9fs_path_copy(&fidp->path, &path); 26995b3c77aaSGreg Kurz v9fs_path_unlock(s); 270060ce86c7SWei Liu } else if (perm & P9_STAT_MODE_LINK) { 270160ce86c7SWei Liu int32_t ofid = atoi(extension.data); 270260ce86c7SWei Liu V9fsFidState *ofidp = get_fid(pdu, ofid); 270360ce86c7SWei Liu if (ofidp == NULL) { 270460ce86c7SWei Liu err = -EINVAL; 270560ce86c7SWei Liu goto out; 270660ce86c7SWei Liu } 270760ce86c7SWei Liu err = v9fs_co_link(pdu, ofidp, fidp, &name); 270860ce86c7SWei Liu put_fid(pdu, ofidp); 270960ce86c7SWei Liu if (err < 0) { 271060ce86c7SWei Liu goto out; 271160ce86c7SWei Liu } 271260ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); 271360ce86c7SWei Liu if (err < 0) { 271460ce86c7SWei Liu fidp->fid_type = P9_FID_NONE; 271560ce86c7SWei Liu goto out; 271660ce86c7SWei Liu } 27175b3c77aaSGreg Kurz v9fs_path_write_lock(s); 271860ce86c7SWei Liu v9fs_path_copy(&fidp->path, &path); 27195b3c77aaSGreg Kurz v9fs_path_unlock(s); 272060ce86c7SWei Liu err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); 272160ce86c7SWei Liu if (err < 0) { 272260ce86c7SWei Liu fidp->fid_type = P9_FID_NONE; 272360ce86c7SWei Liu goto out; 272460ce86c7SWei Liu } 272560ce86c7SWei Liu } else if (perm & P9_STAT_MODE_DEVICE) { 272660ce86c7SWei Liu char ctype; 272760ce86c7SWei Liu uint32_t major, minor; 272860ce86c7SWei Liu mode_t nmode = 0; 272960ce86c7SWei Liu 273060ce86c7SWei Liu if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) { 273160ce86c7SWei Liu err = -errno; 273260ce86c7SWei Liu goto out; 273360ce86c7SWei Liu } 273460ce86c7SWei Liu 273560ce86c7SWei Liu switch (ctype) { 273660ce86c7SWei Liu case 'c': 273760ce86c7SWei Liu nmode = S_IFCHR; 273860ce86c7SWei Liu break; 273960ce86c7SWei Liu case 'b': 274060ce86c7SWei Liu nmode = S_IFBLK; 274160ce86c7SWei Liu break; 274260ce86c7SWei Liu default: 274360ce86c7SWei Liu err = -EIO; 274460ce86c7SWei Liu goto out; 274560ce86c7SWei Liu } 274660ce86c7SWei Liu 274760ce86c7SWei Liu nmode |= perm & 0777; 274860ce86c7SWei Liu err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1, 274960ce86c7SWei Liu makedev(major, minor), nmode, &stbuf); 275060ce86c7SWei Liu if (err < 0) { 275160ce86c7SWei Liu goto out; 275260ce86c7SWei Liu } 275360ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); 275460ce86c7SWei Liu if (err < 0) { 275560ce86c7SWei Liu goto out; 275660ce86c7SWei Liu } 27575b3c77aaSGreg Kurz v9fs_path_write_lock(s); 275860ce86c7SWei Liu v9fs_path_copy(&fidp->path, &path); 27595b3c77aaSGreg Kurz v9fs_path_unlock(s); 276060ce86c7SWei Liu } else if (perm & P9_STAT_MODE_NAMED_PIPE) { 276160ce86c7SWei Liu err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1, 276260ce86c7SWei Liu 0, S_IFIFO | (perm & 0777), &stbuf); 276360ce86c7SWei Liu if (err < 0) { 276460ce86c7SWei Liu goto out; 276560ce86c7SWei Liu } 276660ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); 276760ce86c7SWei Liu if (err < 0) { 276860ce86c7SWei Liu goto out; 276960ce86c7SWei Liu } 27705b3c77aaSGreg Kurz v9fs_path_write_lock(s); 277160ce86c7SWei Liu v9fs_path_copy(&fidp->path, &path); 27725b3c77aaSGreg Kurz v9fs_path_unlock(s); 277360ce86c7SWei Liu } else if (perm & P9_STAT_MODE_SOCKET) { 277460ce86c7SWei Liu err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1, 277560ce86c7SWei Liu 0, S_IFSOCK | (perm & 0777), &stbuf); 277660ce86c7SWei Liu if (err < 0) { 277760ce86c7SWei Liu goto out; 277860ce86c7SWei Liu } 277960ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); 278060ce86c7SWei Liu if (err < 0) { 278160ce86c7SWei Liu goto out; 278260ce86c7SWei Liu } 27835b3c77aaSGreg Kurz v9fs_path_write_lock(s); 278460ce86c7SWei Liu v9fs_path_copy(&fidp->path, &path); 27855b3c77aaSGreg Kurz v9fs_path_unlock(s); 278660ce86c7SWei Liu } else { 278760ce86c7SWei Liu err = v9fs_co_open2(pdu, fidp, &name, -1, 278860ce86c7SWei Liu omode_to_uflags(mode) | O_CREAT, perm, &stbuf); 278960ce86c7SWei Liu if (err < 0) { 279060ce86c7SWei Liu goto out; 279160ce86c7SWei Liu } 279260ce86c7SWei Liu fidp->fid_type = P9_FID_FILE; 279360ce86c7SWei Liu fidp->open_flags = omode_to_uflags(mode); 279460ce86c7SWei Liu if (fidp->open_flags & O_EXCL) { 279560ce86c7SWei Liu /* 279660ce86c7SWei Liu * We let the host file system do O_EXCL check 279760ce86c7SWei Liu * We should not reclaim such fd 279860ce86c7SWei Liu */ 279960ce86c7SWei Liu fidp->flags |= FID_NON_RECLAIMABLE; 280060ce86c7SWei Liu } 280160ce86c7SWei Liu } 280260ce86c7SWei Liu iounit = get_iounit(pdu, &fidp->path); 28033b5ee9e8SAntonios Motakis err = stat_to_qid(pdu, &stbuf, &qid); 28043b5ee9e8SAntonios Motakis if (err < 0) { 28053b5ee9e8SAntonios Motakis goto out; 28063b5ee9e8SAntonios Motakis } 280760ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Qd", &qid, iounit); 280860ce86c7SWei Liu if (err < 0) { 280960ce86c7SWei Liu goto out; 281060ce86c7SWei Liu } 281160ce86c7SWei Liu err += offset; 281260ce86c7SWei Liu trace_v9fs_create_return(pdu->tag, pdu->id, 281360ce86c7SWei Liu qid.type, qid.version, qid.path, iounit); 281460ce86c7SWei Liu out: 281560ce86c7SWei Liu put_fid(pdu, fidp); 281660ce86c7SWei Liu out_nofid: 281760ce86c7SWei Liu pdu_complete(pdu, err); 281860ce86c7SWei Liu v9fs_string_free(&name); 281960ce86c7SWei Liu v9fs_string_free(&extension); 282060ce86c7SWei Liu v9fs_path_free(&path); 282160ce86c7SWei Liu } 282260ce86c7SWei Liu 28238440e22eSGreg Kurz static void coroutine_fn v9fs_symlink(void *opaque) 282460ce86c7SWei Liu { 282560ce86c7SWei Liu V9fsPDU *pdu = opaque; 282660ce86c7SWei Liu V9fsString name; 282760ce86c7SWei Liu V9fsString symname; 282860ce86c7SWei Liu V9fsFidState *dfidp; 282960ce86c7SWei Liu V9fsQID qid; 283060ce86c7SWei Liu struct stat stbuf; 283160ce86c7SWei Liu int32_t dfid; 283260ce86c7SWei Liu int err = 0; 283360ce86c7SWei Liu gid_t gid; 283460ce86c7SWei Liu size_t offset = 7; 283560ce86c7SWei Liu 283660ce86c7SWei Liu v9fs_string_init(&name); 283760ce86c7SWei Liu v9fs_string_init(&symname); 283860ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid); 283960ce86c7SWei Liu if (err < 0) { 284060ce86c7SWei Liu goto out_nofid; 284160ce86c7SWei Liu } 284260ce86c7SWei Liu trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid); 284360ce86c7SWei Liu 2844fff39a7aSGreg Kurz if (name_is_illegal(name.data)) { 2845fff39a7aSGreg Kurz err = -ENOENT; 2846fff39a7aSGreg Kurz goto out_nofid; 2847fff39a7aSGreg Kurz } 2848fff39a7aSGreg Kurz 2849805b5d98SGreg Kurz if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 2850805b5d98SGreg Kurz err = -EEXIST; 2851805b5d98SGreg Kurz goto out_nofid; 2852805b5d98SGreg Kurz } 2853805b5d98SGreg Kurz 285460ce86c7SWei Liu dfidp = get_fid(pdu, dfid); 285560ce86c7SWei Liu if (dfidp == NULL) { 285660ce86c7SWei Liu err = -EINVAL; 285760ce86c7SWei Liu goto out_nofid; 285860ce86c7SWei Liu } 285960ce86c7SWei Liu err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf); 286060ce86c7SWei Liu if (err < 0) { 286160ce86c7SWei Liu goto out; 286260ce86c7SWei Liu } 28633b5ee9e8SAntonios Motakis err = stat_to_qid(pdu, &stbuf, &qid); 28643b5ee9e8SAntonios Motakis if (err < 0) { 28653b5ee9e8SAntonios Motakis goto out; 28663b5ee9e8SAntonios Motakis } 286760ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Q", &qid); 286860ce86c7SWei Liu if (err < 0) { 286960ce86c7SWei Liu goto out; 287060ce86c7SWei Liu } 287160ce86c7SWei Liu err += offset; 287260ce86c7SWei Liu trace_v9fs_symlink_return(pdu->tag, pdu->id, 287360ce86c7SWei Liu qid.type, qid.version, qid.path); 287460ce86c7SWei Liu out: 287560ce86c7SWei Liu put_fid(pdu, dfidp); 287660ce86c7SWei Liu out_nofid: 287760ce86c7SWei Liu pdu_complete(pdu, err); 287860ce86c7SWei Liu v9fs_string_free(&name); 287960ce86c7SWei Liu v9fs_string_free(&symname); 288060ce86c7SWei Liu } 288160ce86c7SWei Liu 2882a1bf8b74SGreg Kurz static void coroutine_fn v9fs_flush(void *opaque) 288360ce86c7SWei Liu { 288460ce86c7SWei Liu ssize_t err; 288560ce86c7SWei Liu int16_t tag; 288660ce86c7SWei Liu size_t offset = 7; 2887d5f2af7bSGreg Kurz V9fsPDU *cancel_pdu = NULL; 288860ce86c7SWei Liu V9fsPDU *pdu = opaque; 288960ce86c7SWei Liu V9fsState *s = pdu->s; 289060ce86c7SWei Liu 289160ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "w", &tag); 289260ce86c7SWei Liu if (err < 0) { 289360ce86c7SWei Liu pdu_complete(pdu, err); 289460ce86c7SWei Liu return; 289560ce86c7SWei Liu } 289660ce86c7SWei Liu trace_v9fs_flush(pdu->tag, pdu->id, tag); 289760ce86c7SWei Liu 2898d5f2af7bSGreg Kurz if (pdu->tag == tag) { 28993dc6f869SAlistair Francis warn_report("the guest sent a self-referencing 9P flush request"); 2900d5f2af7bSGreg Kurz } else { 290160ce86c7SWei Liu QLIST_FOREACH(cancel_pdu, &s->active_list, next) { 290260ce86c7SWei Liu if (cancel_pdu->tag == tag) { 290360ce86c7SWei Liu break; 290460ce86c7SWei Liu } 290560ce86c7SWei Liu } 2906d5f2af7bSGreg Kurz } 290760ce86c7SWei Liu if (cancel_pdu) { 290860ce86c7SWei Liu cancel_pdu->cancelled = 1; 290960ce86c7SWei Liu /* 291060ce86c7SWei Liu * Wait for pdu to complete. 291160ce86c7SWei Liu */ 29121ace7ceaSPaolo Bonzini qemu_co_queue_wait(&cancel_pdu->complete, NULL); 291318adde86SGreg Kurz if (!qemu_co_queue_next(&cancel_pdu->complete)) { 291460ce86c7SWei Liu cancel_pdu->cancelled = 0; 291560ce86c7SWei Liu pdu_free(cancel_pdu); 291660ce86c7SWei Liu } 291718adde86SGreg Kurz } 291860ce86c7SWei Liu pdu_complete(pdu, 7); 291960ce86c7SWei Liu } 292060ce86c7SWei Liu 29218440e22eSGreg Kurz static void coroutine_fn v9fs_link(void *opaque) 292260ce86c7SWei Liu { 292360ce86c7SWei Liu V9fsPDU *pdu = opaque; 292460ce86c7SWei Liu int32_t dfid, oldfid; 292560ce86c7SWei Liu V9fsFidState *dfidp, *oldfidp; 292660ce86c7SWei Liu V9fsString name; 292760ce86c7SWei Liu size_t offset = 7; 292860ce86c7SWei Liu int err = 0; 292960ce86c7SWei Liu 293060ce86c7SWei Liu v9fs_string_init(&name); 293160ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name); 293260ce86c7SWei Liu if (err < 0) { 293360ce86c7SWei Liu goto out_nofid; 293460ce86c7SWei Liu } 293560ce86c7SWei Liu trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data); 293660ce86c7SWei Liu 2937fff39a7aSGreg Kurz if (name_is_illegal(name.data)) { 2938fff39a7aSGreg Kurz err = -ENOENT; 2939fff39a7aSGreg Kurz goto out_nofid; 2940fff39a7aSGreg Kurz } 2941fff39a7aSGreg Kurz 2942805b5d98SGreg Kurz if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 2943805b5d98SGreg Kurz err = -EEXIST; 2944805b5d98SGreg Kurz goto out_nofid; 2945805b5d98SGreg Kurz } 2946805b5d98SGreg Kurz 294760ce86c7SWei Liu dfidp = get_fid(pdu, dfid); 294860ce86c7SWei Liu if (dfidp == NULL) { 294960ce86c7SWei Liu err = -ENOENT; 295060ce86c7SWei Liu goto out_nofid; 295160ce86c7SWei Liu } 295260ce86c7SWei Liu 295360ce86c7SWei Liu oldfidp = get_fid(pdu, oldfid); 295460ce86c7SWei Liu if (oldfidp == NULL) { 295560ce86c7SWei Liu err = -ENOENT; 295660ce86c7SWei Liu goto out; 295760ce86c7SWei Liu } 295860ce86c7SWei Liu err = v9fs_co_link(pdu, oldfidp, dfidp, &name); 295960ce86c7SWei Liu if (!err) { 296060ce86c7SWei Liu err = offset; 296160ce86c7SWei Liu } 29624c158678SLi Qiang put_fid(pdu, oldfidp); 296360ce86c7SWei Liu out: 296460ce86c7SWei Liu put_fid(pdu, dfidp); 296560ce86c7SWei Liu out_nofid: 296660ce86c7SWei Liu v9fs_string_free(&name); 296760ce86c7SWei Liu pdu_complete(pdu, err); 296860ce86c7SWei Liu } 296960ce86c7SWei Liu 297060ce86c7SWei Liu /* Only works with path name based fid */ 29718440e22eSGreg Kurz static void coroutine_fn v9fs_remove(void *opaque) 297260ce86c7SWei Liu { 297360ce86c7SWei Liu int32_t fid; 297460ce86c7SWei Liu int err = 0; 297560ce86c7SWei Liu size_t offset = 7; 297660ce86c7SWei Liu V9fsFidState *fidp; 297760ce86c7SWei Liu V9fsPDU *pdu = opaque; 297860ce86c7SWei Liu 297960ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "d", &fid); 298060ce86c7SWei Liu if (err < 0) { 298160ce86c7SWei Liu goto out_nofid; 298260ce86c7SWei Liu } 298360ce86c7SWei Liu trace_v9fs_remove(pdu->tag, pdu->id, fid); 298460ce86c7SWei Liu 298560ce86c7SWei Liu fidp = get_fid(pdu, fid); 298660ce86c7SWei Liu if (fidp == NULL) { 298760ce86c7SWei Liu err = -EINVAL; 298860ce86c7SWei Liu goto out_nofid; 298960ce86c7SWei Liu } 299060ce86c7SWei Liu /* if fs driver is not path based, return EOPNOTSUPP */ 299160ce86c7SWei Liu if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) { 299260ce86c7SWei Liu err = -EOPNOTSUPP; 299360ce86c7SWei Liu goto out_err; 299460ce86c7SWei Liu } 299560ce86c7SWei Liu /* 299660ce86c7SWei Liu * IF the file is unlinked, we cannot reopen 299760ce86c7SWei Liu * the file later. So don't reclaim fd 299860ce86c7SWei Liu */ 299960ce86c7SWei Liu err = v9fs_mark_fids_unreclaim(pdu, &fidp->path); 300060ce86c7SWei Liu if (err < 0) { 300160ce86c7SWei Liu goto out_err; 300260ce86c7SWei Liu } 300360ce86c7SWei Liu err = v9fs_co_remove(pdu, &fidp->path); 300460ce86c7SWei Liu if (!err) { 300560ce86c7SWei Liu err = offset; 300660ce86c7SWei Liu } 300760ce86c7SWei Liu out_err: 300860ce86c7SWei Liu /* For TREMOVE we need to clunk the fid even on failed remove */ 300960ce86c7SWei Liu clunk_fid(pdu->s, fidp->fid); 301060ce86c7SWei Liu put_fid(pdu, fidp); 301160ce86c7SWei Liu out_nofid: 301260ce86c7SWei Liu pdu_complete(pdu, err); 301360ce86c7SWei Liu } 301460ce86c7SWei Liu 30158440e22eSGreg Kurz static void coroutine_fn v9fs_unlinkat(void *opaque) 301660ce86c7SWei Liu { 301760ce86c7SWei Liu int err = 0; 301860ce86c7SWei Liu V9fsString name; 301967e87345SKeno Fischer int32_t dfid, flags, rflags = 0; 302060ce86c7SWei Liu size_t offset = 7; 302160ce86c7SWei Liu V9fsPath path; 302260ce86c7SWei Liu V9fsFidState *dfidp; 302360ce86c7SWei Liu V9fsPDU *pdu = opaque; 302460ce86c7SWei Liu 302560ce86c7SWei Liu v9fs_string_init(&name); 302660ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags); 302760ce86c7SWei Liu if (err < 0) { 302860ce86c7SWei Liu goto out_nofid; 302960ce86c7SWei Liu } 3030fff39a7aSGreg Kurz 3031fff39a7aSGreg Kurz if (name_is_illegal(name.data)) { 3032fff39a7aSGreg Kurz err = -ENOENT; 3033fff39a7aSGreg Kurz goto out_nofid; 3034fff39a7aSGreg Kurz } 3035fff39a7aSGreg Kurz 3036805b5d98SGreg Kurz if (!strcmp(".", name.data)) { 3037805b5d98SGreg Kurz err = -EINVAL; 3038805b5d98SGreg Kurz goto out_nofid; 3039805b5d98SGreg Kurz } 3040805b5d98SGreg Kurz 3041805b5d98SGreg Kurz if (!strcmp("..", name.data)) { 3042805b5d98SGreg Kurz err = -ENOTEMPTY; 3043805b5d98SGreg Kurz goto out_nofid; 3044805b5d98SGreg Kurz } 3045805b5d98SGreg Kurz 304667e87345SKeno Fischer if (flags & ~P9_DOTL_AT_REMOVEDIR) { 304767e87345SKeno Fischer err = -EINVAL; 304867e87345SKeno Fischer goto out_nofid; 304967e87345SKeno Fischer } 305067e87345SKeno Fischer 305167e87345SKeno Fischer if (flags & P9_DOTL_AT_REMOVEDIR) { 305267e87345SKeno Fischer rflags |= AT_REMOVEDIR; 305367e87345SKeno Fischer } 305467e87345SKeno Fischer 305560ce86c7SWei Liu dfidp = get_fid(pdu, dfid); 305660ce86c7SWei Liu if (dfidp == NULL) { 305760ce86c7SWei Liu err = -EINVAL; 305860ce86c7SWei Liu goto out_nofid; 305960ce86c7SWei Liu } 306060ce86c7SWei Liu /* 306160ce86c7SWei Liu * IF the file is unlinked, we cannot reopen 306260ce86c7SWei Liu * the file later. So don't reclaim fd 306360ce86c7SWei Liu */ 306460ce86c7SWei Liu v9fs_path_init(&path); 306560ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path); 306660ce86c7SWei Liu if (err < 0) { 306760ce86c7SWei Liu goto out_err; 306860ce86c7SWei Liu } 306960ce86c7SWei Liu err = v9fs_mark_fids_unreclaim(pdu, &path); 307060ce86c7SWei Liu if (err < 0) { 307160ce86c7SWei Liu goto out_err; 307260ce86c7SWei Liu } 307367e87345SKeno Fischer err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, rflags); 307460ce86c7SWei Liu if (!err) { 307560ce86c7SWei Liu err = offset; 307660ce86c7SWei Liu } 307760ce86c7SWei Liu out_err: 307860ce86c7SWei Liu put_fid(pdu, dfidp); 307960ce86c7SWei Liu v9fs_path_free(&path); 308060ce86c7SWei Liu out_nofid: 308160ce86c7SWei Liu pdu_complete(pdu, err); 308260ce86c7SWei Liu v9fs_string_free(&name); 308360ce86c7SWei Liu } 308460ce86c7SWei Liu 308560ce86c7SWei Liu 308660ce86c7SWei Liu /* Only works with path name based fid */ 30878440e22eSGreg Kurz static int coroutine_fn v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp, 30888440e22eSGreg Kurz int32_t newdirfid, 30898440e22eSGreg Kurz V9fsString *name) 309060ce86c7SWei Liu { 309160ce86c7SWei Liu int err = 0; 309260ce86c7SWei Liu V9fsPath new_path; 309360ce86c7SWei Liu V9fsFidState *tfidp; 309460ce86c7SWei Liu V9fsState *s = pdu->s; 309560ce86c7SWei Liu V9fsFidState *dirfidp = NULL; 309660ce86c7SWei Liu 309760ce86c7SWei Liu v9fs_path_init(&new_path); 309860ce86c7SWei Liu if (newdirfid != -1) { 309960ce86c7SWei Liu dirfidp = get_fid(pdu, newdirfid); 310060ce86c7SWei Liu if (dirfidp == NULL) { 3101b858e80aSDaniel Henrique Barboza return -ENOENT; 310260ce86c7SWei Liu } 310349dd946bSGreg Kurz if (fidp->fid_type != P9_FID_NONE) { 310449dd946bSGreg Kurz err = -EINVAL; 310549dd946bSGreg Kurz goto out; 310649dd946bSGreg Kurz } 31074fa62005SGreg Kurz err = v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path); 31084fa62005SGreg Kurz if (err < 0) { 31094fa62005SGreg Kurz goto out; 31104fa62005SGreg Kurz } 311160ce86c7SWei Liu } else { 31124d8bc733SJan Dakinevich char *dir_name = g_path_get_dirname(fidp->path.data); 31134d8bc733SJan Dakinevich V9fsPath dir_path; 31144d8bc733SJan Dakinevich 31154d8bc733SJan Dakinevich v9fs_path_init(&dir_path); 31164d8bc733SJan Dakinevich v9fs_path_sprintf(&dir_path, "%s", dir_name); 31174d8bc733SJan Dakinevich g_free(dir_name); 31184d8bc733SJan Dakinevich 31194d8bc733SJan Dakinevich err = v9fs_co_name_to_path(pdu, &dir_path, name->data, &new_path); 31204d8bc733SJan Dakinevich v9fs_path_free(&dir_path); 31214fa62005SGreg Kurz if (err < 0) { 31224fa62005SGreg Kurz goto out; 31234fa62005SGreg Kurz } 312460ce86c7SWei Liu } 312560ce86c7SWei Liu err = v9fs_co_rename(pdu, &fidp->path, &new_path); 312660ce86c7SWei Liu if (err < 0) { 312760ce86c7SWei Liu goto out; 312860ce86c7SWei Liu } 312960ce86c7SWei Liu /* 313060ce86c7SWei Liu * Fixup fid's pointing to the old name to 313160ce86c7SWei Liu * start pointing to the new name 313260ce86c7SWei Liu */ 3133feabd6cfSGreg Kurz QSIMPLEQ_FOREACH(tfidp, &s->fid_list, next) { 313460ce86c7SWei Liu if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) { 313560ce86c7SWei Liu /* replace the name */ 313660ce86c7SWei Liu v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data)); 313760ce86c7SWei Liu } 313860ce86c7SWei Liu } 313960ce86c7SWei Liu out: 314060ce86c7SWei Liu if (dirfidp) { 314160ce86c7SWei Liu put_fid(pdu, dirfidp); 314260ce86c7SWei Liu } 314360ce86c7SWei Liu v9fs_path_free(&new_path); 314460ce86c7SWei Liu return err; 314560ce86c7SWei Liu } 314660ce86c7SWei Liu 314760ce86c7SWei Liu /* Only works with path name based fid */ 31488440e22eSGreg Kurz static void coroutine_fn v9fs_rename(void *opaque) 314960ce86c7SWei Liu { 315060ce86c7SWei Liu int32_t fid; 315160ce86c7SWei Liu ssize_t err = 0; 315260ce86c7SWei Liu size_t offset = 7; 315360ce86c7SWei Liu V9fsString name; 315460ce86c7SWei Liu int32_t newdirfid; 315560ce86c7SWei Liu V9fsFidState *fidp; 315660ce86c7SWei Liu V9fsPDU *pdu = opaque; 315760ce86c7SWei Liu V9fsState *s = pdu->s; 315860ce86c7SWei Liu 315960ce86c7SWei Liu v9fs_string_init(&name); 316060ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name); 316160ce86c7SWei Liu if (err < 0) { 316260ce86c7SWei Liu goto out_nofid; 316360ce86c7SWei Liu } 3164fff39a7aSGreg Kurz 3165fff39a7aSGreg Kurz if (name_is_illegal(name.data)) { 3166fff39a7aSGreg Kurz err = -ENOENT; 3167fff39a7aSGreg Kurz goto out_nofid; 3168fff39a7aSGreg Kurz } 3169fff39a7aSGreg Kurz 3170805b5d98SGreg Kurz if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 3171805b5d98SGreg Kurz err = -EISDIR; 3172805b5d98SGreg Kurz goto out_nofid; 3173805b5d98SGreg Kurz } 3174805b5d98SGreg Kurz 317560ce86c7SWei Liu fidp = get_fid(pdu, fid); 317660ce86c7SWei Liu if (fidp == NULL) { 317760ce86c7SWei Liu err = -ENOENT; 317860ce86c7SWei Liu goto out_nofid; 317960ce86c7SWei Liu } 318049dd946bSGreg Kurz if (fidp->fid_type != P9_FID_NONE) { 318149dd946bSGreg Kurz err = -EINVAL; 318249dd946bSGreg Kurz goto out; 318349dd946bSGreg Kurz } 318460ce86c7SWei Liu /* if fs driver is not path based, return EOPNOTSUPP */ 318560ce86c7SWei Liu if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) { 318660ce86c7SWei Liu err = -EOPNOTSUPP; 318760ce86c7SWei Liu goto out; 318860ce86c7SWei Liu } 318960ce86c7SWei Liu v9fs_path_write_lock(s); 319060ce86c7SWei Liu err = v9fs_complete_rename(pdu, fidp, newdirfid, &name); 319160ce86c7SWei Liu v9fs_path_unlock(s); 319260ce86c7SWei Liu if (!err) { 319360ce86c7SWei Liu err = offset; 319460ce86c7SWei Liu } 319560ce86c7SWei Liu out: 319660ce86c7SWei Liu put_fid(pdu, fidp); 319760ce86c7SWei Liu out_nofid: 319860ce86c7SWei Liu pdu_complete(pdu, err); 319960ce86c7SWei Liu v9fs_string_free(&name); 320060ce86c7SWei Liu } 320160ce86c7SWei Liu 32024fa62005SGreg Kurz static int coroutine_fn v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir, 32038440e22eSGreg Kurz V9fsString *old_name, 32048440e22eSGreg Kurz V9fsPath *newdir, 320560ce86c7SWei Liu V9fsString *new_name) 320660ce86c7SWei Liu { 320760ce86c7SWei Liu V9fsFidState *tfidp; 320860ce86c7SWei Liu V9fsPath oldpath, newpath; 320960ce86c7SWei Liu V9fsState *s = pdu->s; 32104fa62005SGreg Kurz int err; 321160ce86c7SWei Liu 321260ce86c7SWei Liu v9fs_path_init(&oldpath); 321360ce86c7SWei Liu v9fs_path_init(&newpath); 32144fa62005SGreg Kurz err = v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath); 32154fa62005SGreg Kurz if (err < 0) { 32164fa62005SGreg Kurz goto out; 32174fa62005SGreg Kurz } 32184fa62005SGreg Kurz err = v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath); 32194fa62005SGreg Kurz if (err < 0) { 32204fa62005SGreg Kurz goto out; 32214fa62005SGreg Kurz } 322260ce86c7SWei Liu 322360ce86c7SWei Liu /* 322460ce86c7SWei Liu * Fixup fid's pointing to the old name to 322560ce86c7SWei Liu * start pointing to the new name 322660ce86c7SWei Liu */ 3227feabd6cfSGreg Kurz QSIMPLEQ_FOREACH(tfidp, &s->fid_list, next) { 322860ce86c7SWei Liu if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) { 322960ce86c7SWei Liu /* replace the name */ 323060ce86c7SWei Liu v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data)); 323160ce86c7SWei Liu } 323260ce86c7SWei Liu } 32334fa62005SGreg Kurz out: 323460ce86c7SWei Liu v9fs_path_free(&oldpath); 323560ce86c7SWei Liu v9fs_path_free(&newpath); 32364fa62005SGreg Kurz return err; 323760ce86c7SWei Liu } 323860ce86c7SWei Liu 32398440e22eSGreg Kurz static int coroutine_fn v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid, 32408440e22eSGreg Kurz V9fsString *old_name, 32418440e22eSGreg Kurz int32_t newdirfid, 324260ce86c7SWei Liu V9fsString *new_name) 324360ce86c7SWei Liu { 324460ce86c7SWei Liu int err = 0; 324560ce86c7SWei Liu V9fsState *s = pdu->s; 324660ce86c7SWei Liu V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL; 324760ce86c7SWei Liu 324860ce86c7SWei Liu olddirfidp = get_fid(pdu, olddirfid); 324960ce86c7SWei Liu if (olddirfidp == NULL) { 325060ce86c7SWei Liu err = -ENOENT; 325160ce86c7SWei Liu goto out; 325260ce86c7SWei Liu } 325360ce86c7SWei Liu if (newdirfid != -1) { 325460ce86c7SWei Liu newdirfidp = get_fid(pdu, newdirfid); 325560ce86c7SWei Liu if (newdirfidp == NULL) { 325660ce86c7SWei Liu err = -ENOENT; 325760ce86c7SWei Liu goto out; 325860ce86c7SWei Liu } 325960ce86c7SWei Liu } else { 326060ce86c7SWei Liu newdirfidp = get_fid(pdu, olddirfid); 326160ce86c7SWei Liu } 326260ce86c7SWei Liu 326360ce86c7SWei Liu err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name, 326460ce86c7SWei Liu &newdirfidp->path, new_name); 326560ce86c7SWei Liu if (err < 0) { 326660ce86c7SWei Liu goto out; 326760ce86c7SWei Liu } 326860ce86c7SWei Liu if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) { 326960ce86c7SWei Liu /* Only for path based fid we need to do the below fixup */ 32704fa62005SGreg Kurz err = v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name, 327160ce86c7SWei Liu &newdirfidp->path, new_name); 327260ce86c7SWei Liu } 327360ce86c7SWei Liu out: 327460ce86c7SWei Liu if (olddirfidp) { 327560ce86c7SWei Liu put_fid(pdu, olddirfidp); 327660ce86c7SWei Liu } 327760ce86c7SWei Liu if (newdirfidp) { 327860ce86c7SWei Liu put_fid(pdu, newdirfidp); 327960ce86c7SWei Liu } 328060ce86c7SWei Liu return err; 328160ce86c7SWei Liu } 328260ce86c7SWei Liu 32838440e22eSGreg Kurz static void coroutine_fn v9fs_renameat(void *opaque) 328460ce86c7SWei Liu { 328560ce86c7SWei Liu ssize_t err = 0; 328660ce86c7SWei Liu size_t offset = 7; 328760ce86c7SWei Liu V9fsPDU *pdu = opaque; 328860ce86c7SWei Liu V9fsState *s = pdu->s; 328960ce86c7SWei Liu int32_t olddirfid, newdirfid; 329060ce86c7SWei Liu V9fsString old_name, new_name; 329160ce86c7SWei Liu 329260ce86c7SWei Liu v9fs_string_init(&old_name); 329360ce86c7SWei Liu v9fs_string_init(&new_name); 329460ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid, 329560ce86c7SWei Liu &old_name, &newdirfid, &new_name); 329660ce86c7SWei Liu if (err < 0) { 329760ce86c7SWei Liu goto out_err; 329860ce86c7SWei Liu } 329960ce86c7SWei Liu 3300fff39a7aSGreg Kurz if (name_is_illegal(old_name.data) || name_is_illegal(new_name.data)) { 3301fff39a7aSGreg Kurz err = -ENOENT; 3302fff39a7aSGreg Kurz goto out_err; 3303fff39a7aSGreg Kurz } 3304fff39a7aSGreg Kurz 3305805b5d98SGreg Kurz if (!strcmp(".", old_name.data) || !strcmp("..", old_name.data) || 3306805b5d98SGreg Kurz !strcmp(".", new_name.data) || !strcmp("..", new_name.data)) { 3307805b5d98SGreg Kurz err = -EISDIR; 3308805b5d98SGreg Kurz goto out_err; 3309805b5d98SGreg Kurz } 3310805b5d98SGreg Kurz 331160ce86c7SWei Liu v9fs_path_write_lock(s); 331260ce86c7SWei Liu err = v9fs_complete_renameat(pdu, olddirfid, 331360ce86c7SWei Liu &old_name, newdirfid, &new_name); 331460ce86c7SWei Liu v9fs_path_unlock(s); 331560ce86c7SWei Liu if (!err) { 331660ce86c7SWei Liu err = offset; 331760ce86c7SWei Liu } 331860ce86c7SWei Liu 331960ce86c7SWei Liu out_err: 332060ce86c7SWei Liu pdu_complete(pdu, err); 332160ce86c7SWei Liu v9fs_string_free(&old_name); 332260ce86c7SWei Liu v9fs_string_free(&new_name); 332360ce86c7SWei Liu } 332460ce86c7SWei Liu 33258440e22eSGreg Kurz static void coroutine_fn v9fs_wstat(void *opaque) 332660ce86c7SWei Liu { 332760ce86c7SWei Liu int32_t fid; 332860ce86c7SWei Liu int err = 0; 332960ce86c7SWei Liu int16_t unused; 333060ce86c7SWei Liu V9fsStat v9stat; 333160ce86c7SWei Liu size_t offset = 7; 333260ce86c7SWei Liu struct stat stbuf; 333360ce86c7SWei Liu V9fsFidState *fidp; 333460ce86c7SWei Liu V9fsPDU *pdu = opaque; 33351d203986SGreg Kurz V9fsState *s = pdu->s; 333660ce86c7SWei Liu 333760ce86c7SWei Liu v9fs_stat_init(&v9stat); 333860ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat); 333960ce86c7SWei Liu if (err < 0) { 334060ce86c7SWei Liu goto out_nofid; 334160ce86c7SWei Liu } 334260ce86c7SWei Liu trace_v9fs_wstat(pdu->tag, pdu->id, fid, 334360ce86c7SWei Liu v9stat.mode, v9stat.atime, v9stat.mtime); 334460ce86c7SWei Liu 334560ce86c7SWei Liu fidp = get_fid(pdu, fid); 334660ce86c7SWei Liu if (fidp == NULL) { 334760ce86c7SWei Liu err = -EINVAL; 334860ce86c7SWei Liu goto out_nofid; 334960ce86c7SWei Liu } 335060ce86c7SWei Liu /* do we need to sync the file? */ 335160ce86c7SWei Liu if (donttouch_stat(&v9stat)) { 335260ce86c7SWei Liu err = v9fs_co_fsync(pdu, fidp, 0); 335360ce86c7SWei Liu goto out; 335460ce86c7SWei Liu } 335560ce86c7SWei Liu if (v9stat.mode != -1) { 335660ce86c7SWei Liu uint32_t v9_mode; 335760ce86c7SWei Liu err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); 335860ce86c7SWei Liu if (err < 0) { 335960ce86c7SWei Liu goto out; 336060ce86c7SWei Liu } 336160ce86c7SWei Liu v9_mode = stat_to_v9mode(&stbuf); 336260ce86c7SWei Liu if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) != 336360ce86c7SWei Liu (v9_mode & P9_STAT_MODE_TYPE_BITS)) { 336460ce86c7SWei Liu /* Attempting to change the type */ 336560ce86c7SWei Liu err = -EIO; 336660ce86c7SWei Liu goto out; 336760ce86c7SWei Liu } 336860ce86c7SWei Liu err = v9fs_co_chmod(pdu, &fidp->path, 336960ce86c7SWei Liu v9mode_to_mode(v9stat.mode, 337060ce86c7SWei Liu &v9stat.extension)); 337160ce86c7SWei Liu if (err < 0) { 337260ce86c7SWei Liu goto out; 337360ce86c7SWei Liu } 337460ce86c7SWei Liu } 337560ce86c7SWei Liu if (v9stat.mtime != -1 || v9stat.atime != -1) { 337660ce86c7SWei Liu struct timespec times[2]; 337760ce86c7SWei Liu if (v9stat.atime != -1) { 337860ce86c7SWei Liu times[0].tv_sec = v9stat.atime; 337960ce86c7SWei Liu times[0].tv_nsec = 0; 338060ce86c7SWei Liu } else { 338160ce86c7SWei Liu times[0].tv_nsec = UTIME_OMIT; 338260ce86c7SWei Liu } 338360ce86c7SWei Liu if (v9stat.mtime != -1) { 338460ce86c7SWei Liu times[1].tv_sec = v9stat.mtime; 338560ce86c7SWei Liu times[1].tv_nsec = 0; 338660ce86c7SWei Liu } else { 338760ce86c7SWei Liu times[1].tv_nsec = UTIME_OMIT; 338860ce86c7SWei Liu } 338960ce86c7SWei Liu err = v9fs_co_utimensat(pdu, &fidp->path, times); 339060ce86c7SWei Liu if (err < 0) { 339160ce86c7SWei Liu goto out; 339260ce86c7SWei Liu } 339360ce86c7SWei Liu } 339460ce86c7SWei Liu if (v9stat.n_gid != -1 || v9stat.n_uid != -1) { 339560ce86c7SWei Liu err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid); 339660ce86c7SWei Liu if (err < 0) { 339760ce86c7SWei Liu goto out; 339860ce86c7SWei Liu } 339960ce86c7SWei Liu } 340060ce86c7SWei Liu if (v9stat.name.size != 0) { 34011d203986SGreg Kurz v9fs_path_write_lock(s); 340260ce86c7SWei Liu err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name); 34031d203986SGreg Kurz v9fs_path_unlock(s); 340460ce86c7SWei Liu if (err < 0) { 340560ce86c7SWei Liu goto out; 340660ce86c7SWei Liu } 340760ce86c7SWei Liu } 340860ce86c7SWei Liu if (v9stat.length != -1) { 340960ce86c7SWei Liu err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length); 341060ce86c7SWei Liu if (err < 0) { 341160ce86c7SWei Liu goto out; 341260ce86c7SWei Liu } 341360ce86c7SWei Liu } 341460ce86c7SWei Liu err = offset; 341560ce86c7SWei Liu out: 341660ce86c7SWei Liu put_fid(pdu, fidp); 341760ce86c7SWei Liu out_nofid: 341860ce86c7SWei Liu v9fs_stat_free(&v9stat); 341960ce86c7SWei Liu pdu_complete(pdu, err); 342060ce86c7SWei Liu } 342160ce86c7SWei Liu 342260ce86c7SWei Liu static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf) 342360ce86c7SWei Liu { 342460ce86c7SWei Liu uint32_t f_type; 342560ce86c7SWei Liu uint32_t f_bsize; 342660ce86c7SWei Liu uint64_t f_blocks; 342760ce86c7SWei Liu uint64_t f_bfree; 342860ce86c7SWei Liu uint64_t f_bavail; 342960ce86c7SWei Liu uint64_t f_files; 343060ce86c7SWei Liu uint64_t f_ffree; 343160ce86c7SWei Liu uint64_t fsid_val; 343260ce86c7SWei Liu uint32_t f_namelen; 343360ce86c7SWei Liu size_t offset = 7; 343460ce86c7SWei Liu int32_t bsize_factor; 343560ce86c7SWei Liu 343660ce86c7SWei Liu /* 343760ce86c7SWei Liu * compute bsize factor based on host file system block size 343860ce86c7SWei Liu * and client msize 343960ce86c7SWei Liu */ 344060ce86c7SWei Liu bsize_factor = (s->msize - P9_IOHDRSZ) / stbuf->f_bsize; 344160ce86c7SWei Liu if (!bsize_factor) { 344260ce86c7SWei Liu bsize_factor = 1; 344360ce86c7SWei Liu } 344460ce86c7SWei Liu f_type = stbuf->f_type; 344560ce86c7SWei Liu f_bsize = stbuf->f_bsize; 344660ce86c7SWei Liu f_bsize *= bsize_factor; 344760ce86c7SWei Liu /* 344860ce86c7SWei Liu * f_bsize is adjusted(multiplied) by bsize factor, so we need to 344960ce86c7SWei Liu * adjust(divide) the number of blocks, free blocks and available 345060ce86c7SWei Liu * blocks by bsize factor 345160ce86c7SWei Liu */ 345260ce86c7SWei Liu f_blocks = stbuf->f_blocks / bsize_factor; 345360ce86c7SWei Liu f_bfree = stbuf->f_bfree / bsize_factor; 345460ce86c7SWei Liu f_bavail = stbuf->f_bavail / bsize_factor; 345560ce86c7SWei Liu f_files = stbuf->f_files; 345660ce86c7SWei Liu f_ffree = stbuf->f_ffree; 345760ce86c7SWei Liu fsid_val = (unsigned int) stbuf->f_fsid.__val[0] | 345860ce86c7SWei Liu (unsigned long long)stbuf->f_fsid.__val[1] << 32; 345960ce86c7SWei Liu f_namelen = stbuf->f_namelen; 346060ce86c7SWei Liu 346160ce86c7SWei Liu return pdu_marshal(pdu, offset, "ddqqqqqqd", 346260ce86c7SWei Liu f_type, f_bsize, f_blocks, f_bfree, 346360ce86c7SWei Liu f_bavail, f_files, f_ffree, 346460ce86c7SWei Liu fsid_val, f_namelen); 346560ce86c7SWei Liu } 346660ce86c7SWei Liu 34678440e22eSGreg Kurz static void coroutine_fn v9fs_statfs(void *opaque) 346860ce86c7SWei Liu { 346960ce86c7SWei Liu int32_t fid; 347060ce86c7SWei Liu ssize_t retval = 0; 347160ce86c7SWei Liu size_t offset = 7; 347260ce86c7SWei Liu V9fsFidState *fidp; 347360ce86c7SWei Liu struct statfs stbuf; 347460ce86c7SWei Liu V9fsPDU *pdu = opaque; 347560ce86c7SWei Liu V9fsState *s = pdu->s; 347660ce86c7SWei Liu 347760ce86c7SWei Liu retval = pdu_unmarshal(pdu, offset, "d", &fid); 347860ce86c7SWei Liu if (retval < 0) { 347960ce86c7SWei Liu goto out_nofid; 348060ce86c7SWei Liu } 348160ce86c7SWei Liu fidp = get_fid(pdu, fid); 348260ce86c7SWei Liu if (fidp == NULL) { 348360ce86c7SWei Liu retval = -ENOENT; 348460ce86c7SWei Liu goto out_nofid; 348560ce86c7SWei Liu } 348660ce86c7SWei Liu retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf); 348760ce86c7SWei Liu if (retval < 0) { 348860ce86c7SWei Liu goto out; 348960ce86c7SWei Liu } 349060ce86c7SWei Liu retval = v9fs_fill_statfs(s, pdu, &stbuf); 349160ce86c7SWei Liu if (retval < 0) { 349260ce86c7SWei Liu goto out; 349360ce86c7SWei Liu } 349460ce86c7SWei Liu retval += offset; 349560ce86c7SWei Liu out: 349660ce86c7SWei Liu put_fid(pdu, fidp); 349760ce86c7SWei Liu out_nofid: 349860ce86c7SWei Liu pdu_complete(pdu, retval); 349960ce86c7SWei Liu } 350060ce86c7SWei Liu 35018440e22eSGreg Kurz static void coroutine_fn v9fs_mknod(void *opaque) 350260ce86c7SWei Liu { 350360ce86c7SWei Liu 350460ce86c7SWei Liu int mode; 350560ce86c7SWei Liu gid_t gid; 350660ce86c7SWei Liu int32_t fid; 350760ce86c7SWei Liu V9fsQID qid; 350860ce86c7SWei Liu int err = 0; 350960ce86c7SWei Liu int major, minor; 351060ce86c7SWei Liu size_t offset = 7; 351160ce86c7SWei Liu V9fsString name; 351260ce86c7SWei Liu struct stat stbuf; 351360ce86c7SWei Liu V9fsFidState *fidp; 351460ce86c7SWei Liu V9fsPDU *pdu = opaque; 351560ce86c7SWei Liu 351660ce86c7SWei Liu v9fs_string_init(&name); 351760ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode, 351860ce86c7SWei Liu &major, &minor, &gid); 351960ce86c7SWei Liu if (err < 0) { 352060ce86c7SWei Liu goto out_nofid; 352160ce86c7SWei Liu } 352260ce86c7SWei Liu trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor); 352360ce86c7SWei Liu 3524fff39a7aSGreg Kurz if (name_is_illegal(name.data)) { 3525fff39a7aSGreg Kurz err = -ENOENT; 3526fff39a7aSGreg Kurz goto out_nofid; 3527fff39a7aSGreg Kurz } 3528fff39a7aSGreg Kurz 3529805b5d98SGreg Kurz if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 3530805b5d98SGreg Kurz err = -EEXIST; 3531805b5d98SGreg Kurz goto out_nofid; 3532805b5d98SGreg Kurz } 3533805b5d98SGreg Kurz 353460ce86c7SWei Liu fidp = get_fid(pdu, fid); 353560ce86c7SWei Liu if (fidp == NULL) { 353660ce86c7SWei Liu err = -ENOENT; 353760ce86c7SWei Liu goto out_nofid; 353860ce86c7SWei Liu } 353960ce86c7SWei Liu err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid, 354060ce86c7SWei Liu makedev(major, minor), mode, &stbuf); 354160ce86c7SWei Liu if (err < 0) { 354260ce86c7SWei Liu goto out; 354360ce86c7SWei Liu } 35443b5ee9e8SAntonios Motakis err = stat_to_qid(pdu, &stbuf, &qid); 35453b5ee9e8SAntonios Motakis if (err < 0) { 35463b5ee9e8SAntonios Motakis goto out; 35473b5ee9e8SAntonios Motakis } 354860ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Q", &qid); 354960ce86c7SWei Liu if (err < 0) { 355060ce86c7SWei Liu goto out; 355160ce86c7SWei Liu } 355260ce86c7SWei Liu err += offset; 355360ce86c7SWei Liu trace_v9fs_mknod_return(pdu->tag, pdu->id, 355460ce86c7SWei Liu qid.type, qid.version, qid.path); 355560ce86c7SWei Liu out: 355660ce86c7SWei Liu put_fid(pdu, fidp); 355760ce86c7SWei Liu out_nofid: 355860ce86c7SWei Liu pdu_complete(pdu, err); 355960ce86c7SWei Liu v9fs_string_free(&name); 356060ce86c7SWei Liu } 356160ce86c7SWei Liu 356260ce86c7SWei Liu /* 356360ce86c7SWei Liu * Implement posix byte range locking code 356460ce86c7SWei Liu * Server side handling of locking code is very simple, because 9p server in 356560ce86c7SWei Liu * QEMU can handle only one client. And most of the lock handling 356660ce86c7SWei Liu * (like conflict, merging) etc is done by the VFS layer itself, so no need to 356760ce86c7SWei Liu * do any thing in * qemu 9p server side lock code path. 356860ce86c7SWei Liu * So when a TLOCK request comes, always return success 356960ce86c7SWei Liu */ 35708440e22eSGreg Kurz static void coroutine_fn v9fs_lock(void *opaque) 357160ce86c7SWei Liu { 357260ce86c7SWei Liu V9fsFlock flock; 357360ce86c7SWei Liu size_t offset = 7; 357460ce86c7SWei Liu struct stat stbuf; 357560ce86c7SWei Liu V9fsFidState *fidp; 357660ce86c7SWei Liu int32_t fid, err = 0; 357760ce86c7SWei Liu V9fsPDU *pdu = opaque; 357860ce86c7SWei Liu 357960ce86c7SWei Liu v9fs_string_init(&flock.client_id); 358060ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type, 358160ce86c7SWei Liu &flock.flags, &flock.start, &flock.length, 358260ce86c7SWei Liu &flock.proc_id, &flock.client_id); 358360ce86c7SWei Liu if (err < 0) { 358460ce86c7SWei Liu goto out_nofid; 358560ce86c7SWei Liu } 358660ce86c7SWei Liu trace_v9fs_lock(pdu->tag, pdu->id, fid, 358760ce86c7SWei Liu flock.type, flock.start, flock.length); 358860ce86c7SWei Liu 358960ce86c7SWei Liu 359060ce86c7SWei Liu /* We support only block flag now (that too ignored currently) */ 359160ce86c7SWei Liu if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) { 359260ce86c7SWei Liu err = -EINVAL; 359360ce86c7SWei Liu goto out_nofid; 359460ce86c7SWei Liu } 359560ce86c7SWei Liu fidp = get_fid(pdu, fid); 359660ce86c7SWei Liu if (fidp == NULL) { 359760ce86c7SWei Liu err = -ENOENT; 359860ce86c7SWei Liu goto out_nofid; 359960ce86c7SWei Liu } 360060ce86c7SWei Liu err = v9fs_co_fstat(pdu, fidp, &stbuf); 360160ce86c7SWei Liu if (err < 0) { 360260ce86c7SWei Liu goto out; 360360ce86c7SWei Liu } 36044bae2b39SPaolo Bonzini err = pdu_marshal(pdu, offset, "b", P9_LOCK_SUCCESS); 36054bae2b39SPaolo Bonzini if (err < 0) { 36064bae2b39SPaolo Bonzini goto out; 36074bae2b39SPaolo Bonzini } 36084bae2b39SPaolo Bonzini err += offset; 36094bae2b39SPaolo Bonzini trace_v9fs_lock_return(pdu->tag, pdu->id, P9_LOCK_SUCCESS); 361060ce86c7SWei Liu out: 361160ce86c7SWei Liu put_fid(pdu, fidp); 361260ce86c7SWei Liu out_nofid: 361360ce86c7SWei Liu pdu_complete(pdu, err); 361460ce86c7SWei Liu v9fs_string_free(&flock.client_id); 361560ce86c7SWei Liu } 361660ce86c7SWei Liu 361760ce86c7SWei Liu /* 361860ce86c7SWei Liu * When a TGETLOCK request comes, always return success because all lock 361960ce86c7SWei Liu * handling is done by client's VFS layer. 362060ce86c7SWei Liu */ 36218440e22eSGreg Kurz static void coroutine_fn v9fs_getlock(void *opaque) 362260ce86c7SWei Liu { 362360ce86c7SWei Liu size_t offset = 7; 362460ce86c7SWei Liu struct stat stbuf; 362560ce86c7SWei Liu V9fsFidState *fidp; 362660ce86c7SWei Liu V9fsGetlock glock; 362760ce86c7SWei Liu int32_t fid, err = 0; 362860ce86c7SWei Liu V9fsPDU *pdu = opaque; 362960ce86c7SWei Liu 363060ce86c7SWei Liu v9fs_string_init(&glock.client_id); 363160ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type, 363260ce86c7SWei Liu &glock.start, &glock.length, &glock.proc_id, 363360ce86c7SWei Liu &glock.client_id); 363460ce86c7SWei Liu if (err < 0) { 363560ce86c7SWei Liu goto out_nofid; 363660ce86c7SWei Liu } 363760ce86c7SWei Liu trace_v9fs_getlock(pdu->tag, pdu->id, fid, 363860ce86c7SWei Liu glock.type, glock.start, glock.length); 363960ce86c7SWei Liu 364060ce86c7SWei Liu fidp = get_fid(pdu, fid); 364160ce86c7SWei Liu if (fidp == NULL) { 364260ce86c7SWei Liu err = -ENOENT; 364360ce86c7SWei Liu goto out_nofid; 364460ce86c7SWei Liu } 364560ce86c7SWei Liu err = v9fs_co_fstat(pdu, fidp, &stbuf); 364660ce86c7SWei Liu if (err < 0) { 364760ce86c7SWei Liu goto out; 364860ce86c7SWei Liu } 364960ce86c7SWei Liu glock.type = P9_LOCK_TYPE_UNLCK; 365060ce86c7SWei Liu err = pdu_marshal(pdu, offset, "bqqds", glock.type, 365160ce86c7SWei Liu glock.start, glock.length, glock.proc_id, 365260ce86c7SWei Liu &glock.client_id); 365360ce86c7SWei Liu if (err < 0) { 365460ce86c7SWei Liu goto out; 365560ce86c7SWei Liu } 365660ce86c7SWei Liu err += offset; 365760ce86c7SWei Liu trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start, 365860ce86c7SWei Liu glock.length, glock.proc_id); 365960ce86c7SWei Liu out: 366060ce86c7SWei Liu put_fid(pdu, fidp); 366160ce86c7SWei Liu out_nofid: 366260ce86c7SWei Liu pdu_complete(pdu, err); 366360ce86c7SWei Liu v9fs_string_free(&glock.client_id); 366460ce86c7SWei Liu } 366560ce86c7SWei Liu 36668440e22eSGreg Kurz static void coroutine_fn v9fs_mkdir(void *opaque) 366760ce86c7SWei Liu { 366860ce86c7SWei Liu V9fsPDU *pdu = opaque; 366960ce86c7SWei Liu size_t offset = 7; 367060ce86c7SWei Liu int32_t fid; 367160ce86c7SWei Liu struct stat stbuf; 367260ce86c7SWei Liu V9fsQID qid; 367360ce86c7SWei Liu V9fsString name; 367460ce86c7SWei Liu V9fsFidState *fidp; 367560ce86c7SWei Liu gid_t gid; 367660ce86c7SWei Liu int mode; 367760ce86c7SWei Liu int err = 0; 367860ce86c7SWei Liu 367960ce86c7SWei Liu v9fs_string_init(&name); 368060ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid); 368160ce86c7SWei Liu if (err < 0) { 368260ce86c7SWei Liu goto out_nofid; 368360ce86c7SWei Liu } 368460ce86c7SWei Liu trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid); 368560ce86c7SWei Liu 3686fff39a7aSGreg Kurz if (name_is_illegal(name.data)) { 3687fff39a7aSGreg Kurz err = -ENOENT; 3688fff39a7aSGreg Kurz goto out_nofid; 3689fff39a7aSGreg Kurz } 3690fff39a7aSGreg Kurz 3691805b5d98SGreg Kurz if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 3692805b5d98SGreg Kurz err = -EEXIST; 3693805b5d98SGreg Kurz goto out_nofid; 3694805b5d98SGreg Kurz } 3695805b5d98SGreg Kurz 369660ce86c7SWei Liu fidp = get_fid(pdu, fid); 369760ce86c7SWei Liu if (fidp == NULL) { 369860ce86c7SWei Liu err = -ENOENT; 369960ce86c7SWei Liu goto out_nofid; 370060ce86c7SWei Liu } 370160ce86c7SWei Liu err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf); 370260ce86c7SWei Liu if (err < 0) { 370360ce86c7SWei Liu goto out; 370460ce86c7SWei Liu } 37053b5ee9e8SAntonios Motakis err = stat_to_qid(pdu, &stbuf, &qid); 37063b5ee9e8SAntonios Motakis if (err < 0) { 37073b5ee9e8SAntonios Motakis goto out; 37083b5ee9e8SAntonios Motakis } 370960ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Q", &qid); 371060ce86c7SWei Liu if (err < 0) { 371160ce86c7SWei Liu goto out; 371260ce86c7SWei Liu } 371360ce86c7SWei Liu err += offset; 371460ce86c7SWei Liu trace_v9fs_mkdir_return(pdu->tag, pdu->id, 371560ce86c7SWei Liu qid.type, qid.version, qid.path, err); 371660ce86c7SWei Liu out: 371760ce86c7SWei Liu put_fid(pdu, fidp); 371860ce86c7SWei Liu out_nofid: 371960ce86c7SWei Liu pdu_complete(pdu, err); 372060ce86c7SWei Liu v9fs_string_free(&name); 372160ce86c7SWei Liu } 372260ce86c7SWei Liu 37238440e22eSGreg Kurz static void coroutine_fn v9fs_xattrwalk(void *opaque) 372460ce86c7SWei Liu { 372560ce86c7SWei Liu int64_t size; 372660ce86c7SWei Liu V9fsString name; 372760ce86c7SWei Liu ssize_t err = 0; 372860ce86c7SWei Liu size_t offset = 7; 372960ce86c7SWei Liu int32_t fid, newfid; 373060ce86c7SWei Liu V9fsFidState *file_fidp; 373160ce86c7SWei Liu V9fsFidState *xattr_fidp = NULL; 373260ce86c7SWei Liu V9fsPDU *pdu = opaque; 373360ce86c7SWei Liu V9fsState *s = pdu->s; 373460ce86c7SWei Liu 373560ce86c7SWei Liu v9fs_string_init(&name); 373660ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name); 373760ce86c7SWei Liu if (err < 0) { 373860ce86c7SWei Liu goto out_nofid; 373960ce86c7SWei Liu } 374060ce86c7SWei Liu trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data); 374160ce86c7SWei Liu 374260ce86c7SWei Liu file_fidp = get_fid(pdu, fid); 374360ce86c7SWei Liu if (file_fidp == NULL) { 374460ce86c7SWei Liu err = -ENOENT; 374560ce86c7SWei Liu goto out_nofid; 374660ce86c7SWei Liu } 374760ce86c7SWei Liu xattr_fidp = alloc_fid(s, newfid); 374860ce86c7SWei Liu if (xattr_fidp == NULL) { 374960ce86c7SWei Liu err = -EINVAL; 375060ce86c7SWei Liu goto out; 375160ce86c7SWei Liu } 375260ce86c7SWei Liu v9fs_path_copy(&xattr_fidp->path, &file_fidp->path); 3753ba42ebb8SLi Qiang if (!v9fs_string_size(&name)) { 375460ce86c7SWei Liu /* 375560ce86c7SWei Liu * listxattr request. Get the size first 375660ce86c7SWei Liu */ 375760ce86c7SWei Liu size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0); 375860ce86c7SWei Liu if (size < 0) { 375960ce86c7SWei Liu err = size; 376060ce86c7SWei Liu clunk_fid(s, xattr_fidp->fid); 376160ce86c7SWei Liu goto out; 376260ce86c7SWei Liu } 376360ce86c7SWei Liu /* 376460ce86c7SWei Liu * Read the xattr value 376560ce86c7SWei Liu */ 376660ce86c7SWei Liu xattr_fidp->fs.xattr.len = size; 376760ce86c7SWei Liu xattr_fidp->fid_type = P9_FID_XATTR; 3768dd28fbbcSLi Qiang xattr_fidp->fs.xattr.xattrwalk_fid = true; 37697bd92756SPrasad J Pandit xattr_fidp->fs.xattr.value = g_malloc0(size); 3770a647502cSKeno Fischer if (size) { 377160ce86c7SWei Liu err = v9fs_co_llistxattr(pdu, &xattr_fidp->path, 377260ce86c7SWei Liu xattr_fidp->fs.xattr.value, 377360ce86c7SWei Liu xattr_fidp->fs.xattr.len); 377460ce86c7SWei Liu if (err < 0) { 377560ce86c7SWei Liu clunk_fid(s, xattr_fidp->fid); 377660ce86c7SWei Liu goto out; 377760ce86c7SWei Liu } 377860ce86c7SWei Liu } 377960ce86c7SWei Liu err = pdu_marshal(pdu, offset, "q", size); 378060ce86c7SWei Liu if (err < 0) { 378160ce86c7SWei Liu goto out; 378260ce86c7SWei Liu } 378360ce86c7SWei Liu err += offset; 378460ce86c7SWei Liu } else { 378560ce86c7SWei Liu /* 378660ce86c7SWei Liu * specific xattr fid. We check for xattr 378760ce86c7SWei Liu * presence also collect the xattr size 378860ce86c7SWei Liu */ 378960ce86c7SWei Liu size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path, 379060ce86c7SWei Liu &name, NULL, 0); 379160ce86c7SWei Liu if (size < 0) { 379260ce86c7SWei Liu err = size; 379360ce86c7SWei Liu clunk_fid(s, xattr_fidp->fid); 379460ce86c7SWei Liu goto out; 379560ce86c7SWei Liu } 379660ce86c7SWei Liu /* 379760ce86c7SWei Liu * Read the xattr value 379860ce86c7SWei Liu */ 379960ce86c7SWei Liu xattr_fidp->fs.xattr.len = size; 380060ce86c7SWei Liu xattr_fidp->fid_type = P9_FID_XATTR; 3801dd28fbbcSLi Qiang xattr_fidp->fs.xattr.xattrwalk_fid = true; 38027bd92756SPrasad J Pandit xattr_fidp->fs.xattr.value = g_malloc0(size); 3803a647502cSKeno Fischer if (size) { 380460ce86c7SWei Liu err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path, 380560ce86c7SWei Liu &name, xattr_fidp->fs.xattr.value, 380660ce86c7SWei Liu xattr_fidp->fs.xattr.len); 380760ce86c7SWei Liu if (err < 0) { 380860ce86c7SWei Liu clunk_fid(s, xattr_fidp->fid); 380960ce86c7SWei Liu goto out; 381060ce86c7SWei Liu } 381160ce86c7SWei Liu } 381260ce86c7SWei Liu err = pdu_marshal(pdu, offset, "q", size); 381360ce86c7SWei Liu if (err < 0) { 381460ce86c7SWei Liu goto out; 381560ce86c7SWei Liu } 381660ce86c7SWei Liu err += offset; 381760ce86c7SWei Liu } 381860ce86c7SWei Liu trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size); 381960ce86c7SWei Liu out: 382060ce86c7SWei Liu put_fid(pdu, file_fidp); 382160ce86c7SWei Liu if (xattr_fidp) { 382260ce86c7SWei Liu put_fid(pdu, xattr_fidp); 382360ce86c7SWei Liu } 382460ce86c7SWei Liu out_nofid: 382560ce86c7SWei Liu pdu_complete(pdu, err); 382660ce86c7SWei Liu v9fs_string_free(&name); 382760ce86c7SWei Liu } 382860ce86c7SWei Liu 38298440e22eSGreg Kurz static void coroutine_fn v9fs_xattrcreate(void *opaque) 383060ce86c7SWei Liu { 3831aca6897fSKeno Fischer int flags, rflags = 0; 383260ce86c7SWei Liu int32_t fid; 38333b79ef2cSGreg Kurz uint64_t size; 383460ce86c7SWei Liu ssize_t err = 0; 383560ce86c7SWei Liu V9fsString name; 383660ce86c7SWei Liu size_t offset = 7; 383760ce86c7SWei Liu V9fsFidState *file_fidp; 383860ce86c7SWei Liu V9fsFidState *xattr_fidp; 383960ce86c7SWei Liu V9fsPDU *pdu = opaque; 384060ce86c7SWei Liu 384160ce86c7SWei Liu v9fs_string_init(&name); 384260ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags); 384360ce86c7SWei Liu if (err < 0) { 384460ce86c7SWei Liu goto out_nofid; 384560ce86c7SWei Liu } 384660ce86c7SWei Liu trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags); 384760ce86c7SWei Liu 3848aca6897fSKeno Fischer if (flags & ~(P9_XATTR_CREATE | P9_XATTR_REPLACE)) { 3849aca6897fSKeno Fischer err = -EINVAL; 3850aca6897fSKeno Fischer goto out_nofid; 3851aca6897fSKeno Fischer } 3852aca6897fSKeno Fischer 3853aca6897fSKeno Fischer if (flags & P9_XATTR_CREATE) { 3854aca6897fSKeno Fischer rflags |= XATTR_CREATE; 3855aca6897fSKeno Fischer } 3856aca6897fSKeno Fischer 3857aca6897fSKeno Fischer if (flags & P9_XATTR_REPLACE) { 3858aca6897fSKeno Fischer rflags |= XATTR_REPLACE; 3859aca6897fSKeno Fischer } 3860aca6897fSKeno Fischer 38613b79ef2cSGreg Kurz if (size > XATTR_SIZE_MAX) { 38623b79ef2cSGreg Kurz err = -E2BIG; 38633b79ef2cSGreg Kurz goto out_nofid; 38643b79ef2cSGreg Kurz } 38653b79ef2cSGreg Kurz 386660ce86c7SWei Liu file_fidp = get_fid(pdu, fid); 386760ce86c7SWei Liu if (file_fidp == NULL) { 386860ce86c7SWei Liu err = -EINVAL; 386960ce86c7SWei Liu goto out_nofid; 387060ce86c7SWei Liu } 3871dd654e03SGreg Kurz if (file_fidp->fid_type != P9_FID_NONE) { 3872dd654e03SGreg Kurz err = -EINVAL; 3873dd654e03SGreg Kurz goto out_put_fid; 3874dd654e03SGreg Kurz } 3875dd654e03SGreg Kurz 387660ce86c7SWei Liu /* Make the file fid point to xattr */ 387760ce86c7SWei Liu xattr_fidp = file_fidp; 387860ce86c7SWei Liu xattr_fidp->fid_type = P9_FID_XATTR; 387960ce86c7SWei Liu xattr_fidp->fs.xattr.copied_len = 0; 3880dd28fbbcSLi Qiang xattr_fidp->fs.xattr.xattrwalk_fid = false; 388160ce86c7SWei Liu xattr_fidp->fs.xattr.len = size; 3882aca6897fSKeno Fischer xattr_fidp->fs.xattr.flags = rflags; 388360ce86c7SWei Liu v9fs_string_init(&xattr_fidp->fs.xattr.name); 388460ce86c7SWei Liu v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name); 3885eb687602SLi Qiang xattr_fidp->fs.xattr.value = g_malloc0(size); 388660ce86c7SWei Liu err = offset; 3887dd654e03SGreg Kurz out_put_fid: 388860ce86c7SWei Liu put_fid(pdu, file_fidp); 388960ce86c7SWei Liu out_nofid: 389060ce86c7SWei Liu pdu_complete(pdu, err); 389160ce86c7SWei Liu v9fs_string_free(&name); 389260ce86c7SWei Liu } 389360ce86c7SWei Liu 38948440e22eSGreg Kurz static void coroutine_fn v9fs_readlink(void *opaque) 389560ce86c7SWei Liu { 389660ce86c7SWei Liu V9fsPDU *pdu = opaque; 389760ce86c7SWei Liu size_t offset = 7; 389860ce86c7SWei Liu V9fsString target; 389960ce86c7SWei Liu int32_t fid; 390060ce86c7SWei Liu int err = 0; 390160ce86c7SWei Liu V9fsFidState *fidp; 390260ce86c7SWei Liu 390360ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "d", &fid); 390460ce86c7SWei Liu if (err < 0) { 390560ce86c7SWei Liu goto out_nofid; 390660ce86c7SWei Liu } 390760ce86c7SWei Liu trace_v9fs_readlink(pdu->tag, pdu->id, fid); 390860ce86c7SWei Liu fidp = get_fid(pdu, fid); 390960ce86c7SWei Liu if (fidp == NULL) { 391060ce86c7SWei Liu err = -ENOENT; 391160ce86c7SWei Liu goto out_nofid; 391260ce86c7SWei Liu } 391360ce86c7SWei Liu 391460ce86c7SWei Liu v9fs_string_init(&target); 391560ce86c7SWei Liu err = v9fs_co_readlink(pdu, &fidp->path, &target); 391660ce86c7SWei Liu if (err < 0) { 391760ce86c7SWei Liu goto out; 391860ce86c7SWei Liu } 391960ce86c7SWei Liu err = pdu_marshal(pdu, offset, "s", &target); 392060ce86c7SWei Liu if (err < 0) { 392160ce86c7SWei Liu v9fs_string_free(&target); 392260ce86c7SWei Liu goto out; 392360ce86c7SWei Liu } 392460ce86c7SWei Liu err += offset; 392560ce86c7SWei Liu trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data); 392660ce86c7SWei Liu v9fs_string_free(&target); 392760ce86c7SWei Liu out: 392860ce86c7SWei Liu put_fid(pdu, fidp); 392960ce86c7SWei Liu out_nofid: 393060ce86c7SWei Liu pdu_complete(pdu, err); 393160ce86c7SWei Liu } 393260ce86c7SWei Liu 393360ce86c7SWei Liu static CoroutineEntry *pdu_co_handlers[] = { 393460ce86c7SWei Liu [P9_TREADDIR] = v9fs_readdir, 393560ce86c7SWei Liu [P9_TSTATFS] = v9fs_statfs, 393660ce86c7SWei Liu [P9_TGETATTR] = v9fs_getattr, 393760ce86c7SWei Liu [P9_TSETATTR] = v9fs_setattr, 393860ce86c7SWei Liu [P9_TXATTRWALK] = v9fs_xattrwalk, 393960ce86c7SWei Liu [P9_TXATTRCREATE] = v9fs_xattrcreate, 394060ce86c7SWei Liu [P9_TMKNOD] = v9fs_mknod, 394160ce86c7SWei Liu [P9_TRENAME] = v9fs_rename, 394260ce86c7SWei Liu [P9_TLOCK] = v9fs_lock, 394360ce86c7SWei Liu [P9_TGETLOCK] = v9fs_getlock, 394460ce86c7SWei Liu [P9_TRENAMEAT] = v9fs_renameat, 394560ce86c7SWei Liu [P9_TREADLINK] = v9fs_readlink, 394660ce86c7SWei Liu [P9_TUNLINKAT] = v9fs_unlinkat, 394760ce86c7SWei Liu [P9_TMKDIR] = v9fs_mkdir, 394860ce86c7SWei Liu [P9_TVERSION] = v9fs_version, 394960ce86c7SWei Liu [P9_TLOPEN] = v9fs_open, 395060ce86c7SWei Liu [P9_TATTACH] = v9fs_attach, 395160ce86c7SWei Liu [P9_TSTAT] = v9fs_stat, 395260ce86c7SWei Liu [P9_TWALK] = v9fs_walk, 395360ce86c7SWei Liu [P9_TCLUNK] = v9fs_clunk, 395460ce86c7SWei Liu [P9_TFSYNC] = v9fs_fsync, 395560ce86c7SWei Liu [P9_TOPEN] = v9fs_open, 395660ce86c7SWei Liu [P9_TREAD] = v9fs_read, 395760ce86c7SWei Liu #if 0 395860ce86c7SWei Liu [P9_TAUTH] = v9fs_auth, 395960ce86c7SWei Liu #endif 396060ce86c7SWei Liu [P9_TFLUSH] = v9fs_flush, 396160ce86c7SWei Liu [P9_TLINK] = v9fs_link, 396260ce86c7SWei Liu [P9_TSYMLINK] = v9fs_symlink, 396360ce86c7SWei Liu [P9_TCREATE] = v9fs_create, 396460ce86c7SWei Liu [P9_TLCREATE] = v9fs_lcreate, 396560ce86c7SWei Liu [P9_TWRITE] = v9fs_write, 396660ce86c7SWei Liu [P9_TWSTAT] = v9fs_wstat, 396760ce86c7SWei Liu [P9_TREMOVE] = v9fs_remove, 396860ce86c7SWei Liu }; 396960ce86c7SWei Liu 39708440e22eSGreg Kurz static void coroutine_fn v9fs_op_not_supp(void *opaque) 397160ce86c7SWei Liu { 397260ce86c7SWei Liu V9fsPDU *pdu = opaque; 397360ce86c7SWei Liu pdu_complete(pdu, -EOPNOTSUPP); 397460ce86c7SWei Liu } 397560ce86c7SWei Liu 39768440e22eSGreg Kurz static void coroutine_fn v9fs_fs_ro(void *opaque) 397760ce86c7SWei Liu { 397860ce86c7SWei Liu V9fsPDU *pdu = opaque; 397960ce86c7SWei Liu pdu_complete(pdu, -EROFS); 398060ce86c7SWei Liu } 398160ce86c7SWei Liu 398260ce86c7SWei Liu static inline bool is_read_only_op(V9fsPDU *pdu) 398360ce86c7SWei Liu { 398460ce86c7SWei Liu switch (pdu->id) { 398560ce86c7SWei Liu case P9_TREADDIR: 398660ce86c7SWei Liu case P9_TSTATFS: 398760ce86c7SWei Liu case P9_TGETATTR: 398860ce86c7SWei Liu case P9_TXATTRWALK: 398960ce86c7SWei Liu case P9_TLOCK: 399060ce86c7SWei Liu case P9_TGETLOCK: 399160ce86c7SWei Liu case P9_TREADLINK: 399260ce86c7SWei Liu case P9_TVERSION: 399360ce86c7SWei Liu case P9_TLOPEN: 399460ce86c7SWei Liu case P9_TATTACH: 399560ce86c7SWei Liu case P9_TSTAT: 399660ce86c7SWei Liu case P9_TWALK: 399760ce86c7SWei Liu case P9_TCLUNK: 399860ce86c7SWei Liu case P9_TFSYNC: 399960ce86c7SWei Liu case P9_TOPEN: 400060ce86c7SWei Liu case P9_TREAD: 400160ce86c7SWei Liu case P9_TAUTH: 400260ce86c7SWei Liu case P9_TFLUSH: 400360ce86c7SWei Liu return 1; 400460ce86c7SWei Liu default: 400560ce86c7SWei Liu return 0; 400660ce86c7SWei Liu } 400760ce86c7SWei Liu } 400860ce86c7SWei Liu 4009506f3275SGreg Kurz void pdu_submit(V9fsPDU *pdu, P9MsgHeader *hdr) 401060ce86c7SWei Liu { 401160ce86c7SWei Liu Coroutine *co; 401260ce86c7SWei Liu CoroutineEntry *handler; 401360ce86c7SWei Liu V9fsState *s = pdu->s; 401460ce86c7SWei Liu 4015506f3275SGreg Kurz pdu->size = le32_to_cpu(hdr->size_le); 4016506f3275SGreg Kurz pdu->id = hdr->id; 4017506f3275SGreg Kurz pdu->tag = le16_to_cpu(hdr->tag_le); 4018506f3275SGreg Kurz 401960ce86c7SWei Liu if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) || 402060ce86c7SWei Liu (pdu_co_handlers[pdu->id] == NULL)) { 402160ce86c7SWei Liu handler = v9fs_op_not_supp; 4022d1471233SGreg Kurz } else if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) { 4023d1471233SGreg Kurz handler = v9fs_fs_ro; 402460ce86c7SWei Liu } else { 402560ce86c7SWei Liu handler = pdu_co_handlers[pdu->id]; 402660ce86c7SWei Liu } 402760ce86c7SWei Liu 4028506f3275SGreg Kurz qemu_co_queue_init(&pdu->complete); 40290b8b8753SPaolo Bonzini co = qemu_coroutine_create(handler, pdu); 40300b8b8753SPaolo Bonzini qemu_coroutine_enter(co); 403160ce86c7SWei Liu } 403260ce86c7SWei Liu 40332a0c56aaSWei Liu /* Returns 0 on success, 1 on failure. */ 4034066eb006SGreg Kurz int v9fs_device_realize_common(V9fsState *s, const V9fsTransport *t, 4035066eb006SGreg Kurz Error **errp) 40362a0c56aaSWei Liu { 403792c45122SVladimir Sementsov-Ogievskiy ERRP_GUARD(); 40382a0c56aaSWei Liu int i, len; 40392a0c56aaSWei Liu struct stat stat; 40402a0c56aaSWei Liu FsDriverEntry *fse; 40412a0c56aaSWei Liu V9fsPath path; 40422a0c56aaSWei Liu int rc = 1; 40432a0c56aaSWei Liu 4044066eb006SGreg Kurz assert(!s->transport); 4045066eb006SGreg Kurz s->transport = t; 4046066eb006SGreg Kurz 40472a0c56aaSWei Liu /* initialize pdu allocator */ 40482a0c56aaSWei Liu QLIST_INIT(&s->free_list); 40492a0c56aaSWei Liu QLIST_INIT(&s->active_list); 40500d78289cSGreg Kurz for (i = 0; i < MAX_REQ; i++) { 4051583f21f8SStefano Stabellini QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next); 4052583f21f8SStefano Stabellini s->pdus[i].s = s; 4053583f21f8SStefano Stabellini s->pdus[i].idx = i; 40542a0c56aaSWei Liu } 40552a0c56aaSWei Liu 40562a0c56aaSWei Liu v9fs_path_init(&path); 40572a0c56aaSWei Liu 40582a0c56aaSWei Liu fse = get_fsdev_fsentry(s->fsconf.fsdev_id); 40592a0c56aaSWei Liu 40602a0c56aaSWei Liu if (!fse) { 40612a0c56aaSWei Liu /* We don't have a fsdev identified by fsdev_id */ 40622a0c56aaSWei Liu error_setg(errp, "9pfs device couldn't find fsdev with the " 40632a0c56aaSWei Liu "id = %s", 40642a0c56aaSWei Liu s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL"); 40652a0c56aaSWei Liu goto out; 40662a0c56aaSWei Liu } 40672a0c56aaSWei Liu 40682a0c56aaSWei Liu if (!s->fsconf.tag) { 40692a0c56aaSWei Liu /* we haven't specified a mount_tag */ 40702a0c56aaSWei Liu error_setg(errp, "fsdev with id %s needs mount_tag arguments", 40712a0c56aaSWei Liu s->fsconf.fsdev_id); 40722a0c56aaSWei Liu goto out; 40732a0c56aaSWei Liu } 40742a0c56aaSWei Liu 40752a0c56aaSWei Liu s->ctx.export_flags = fse->export_flags; 40762a0c56aaSWei Liu s->ctx.fs_root = g_strdup(fse->path); 40772a0c56aaSWei Liu s->ctx.exops.get_st_gen = NULL; 40782a0c56aaSWei Liu len = strlen(s->fsconf.tag); 40792a0c56aaSWei Liu if (len > MAX_TAG_LEN - 1) { 40802a0c56aaSWei Liu error_setg(errp, "mount tag '%s' (%d bytes) is longer than " 40812a0c56aaSWei Liu "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1); 40822a0c56aaSWei Liu goto out; 40832a0c56aaSWei Liu } 40842a0c56aaSWei Liu 40852a0c56aaSWei Liu s->tag = g_strdup(s->fsconf.tag); 40862a0c56aaSWei Liu s->ctx.uid = -1; 40872a0c56aaSWei Liu 40882a0c56aaSWei Liu s->ops = fse->ops; 40892a0c56aaSWei Liu 4090b96feb2cSTobias Schramm s->ctx.fmode = fse->fmode; 4091b96feb2cSTobias Schramm s->ctx.dmode = fse->dmode; 4092b96feb2cSTobias Schramm 4093feabd6cfSGreg Kurz QSIMPLEQ_INIT(&s->fid_list); 40942a0c56aaSWei Liu qemu_co_rwlock_init(&s->rename_lock); 40952a0c56aaSWei Liu 409665603a80SGreg Kurz if (s->ops->init(&s->ctx, errp) < 0) { 409765603a80SGreg Kurz error_prepend(errp, "cannot initialize fsdev '%s': ", 409865603a80SGreg Kurz s->fsconf.fsdev_id); 40992a0c56aaSWei Liu goto out; 41002a0c56aaSWei Liu } 41012a0c56aaSWei Liu 41022a0c56aaSWei Liu /* 41032a0c56aaSWei Liu * Check details of export path, We need to use fs driver 41042a0c56aaSWei Liu * call back to do that. Since we are in the init path, we don't 41052a0c56aaSWei Liu * use co-routines here. 41062a0c56aaSWei Liu */ 41072a0c56aaSWei Liu if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) { 41082a0c56aaSWei Liu error_setg(errp, 41092a0c56aaSWei Liu "error in converting name to path %s", strerror(errno)); 41102a0c56aaSWei Liu goto out; 41112a0c56aaSWei Liu } 41122a0c56aaSWei Liu if (s->ops->lstat(&s->ctx, &path, &stat)) { 41132a0c56aaSWei Liu error_setg(errp, "share path %s does not exist", fse->path); 41142a0c56aaSWei Liu goto out; 41152a0c56aaSWei Liu } else if (!S_ISDIR(stat.st_mode)) { 41162a0c56aaSWei Liu error_setg(errp, "share path %s is not a directory", fse->path); 41172a0c56aaSWei Liu goto out; 41182a0c56aaSWei Liu } 4119b8bbdb88SPradeep Jagadeesh 41203b5ee9e8SAntonios Motakis s->dev_id = stat.st_dev; 41213b5ee9e8SAntonios Motakis 41226b6aa828SChristian Schoenebeck /* init inode remapping : */ 41236b6aa828SChristian Schoenebeck /* hash table for variable length inode suffixes */ 41246b6aa828SChristian Schoenebeck qpd_table_init(&s->qpd_table); 41256b6aa828SChristian Schoenebeck /* hash table for slow/full inode remapping (most users won't need it) */ 41266b6aa828SChristian Schoenebeck qpf_table_init(&s->qpf_table); 41276b6aa828SChristian Schoenebeck /* hash table for quick inode remapping */ 41281a6ed33cSAntonios Motakis qpp_table_init(&s->qpp_table); 41296b6aa828SChristian Schoenebeck s->qp_ndevices = 0; 41306b6aa828SChristian Schoenebeck s->qp_affix_next = 1; /* reserve 0 to detect overflow */ 4131f3fe4a2dSAntonios Motakis s->qp_fullpath_next = 1; 41321a6ed33cSAntonios Motakis 4133b8bbdb88SPradeep Jagadeesh s->ctx.fst = &fse->fst; 4134b8bbdb88SPradeep Jagadeesh fsdev_throttle_init(s->ctx.fst); 4135b8bbdb88SPradeep Jagadeesh 41362a0c56aaSWei Liu rc = 0; 41372a0c56aaSWei Liu out: 41382a0c56aaSWei Liu if (rc) { 4139b69c3c21SMarkus Armbruster v9fs_device_unrealize_common(s); 4140702dbcc2SLi Qiang } 41412a0c56aaSWei Liu v9fs_path_free(&path); 41422a0c56aaSWei Liu return rc; 41432a0c56aaSWei Liu } 41442a0c56aaSWei Liu 4145b69c3c21SMarkus Armbruster void v9fs_device_unrealize_common(V9fsState *s) 41462a0c56aaSWei Liu { 4147c0da0cb7SGreg Kurz if (s->ops && s->ops->cleanup) { 4148702dbcc2SLi Qiang s->ops->cleanup(&s->ctx); 4149702dbcc2SLi Qiang } 4150c0da0cb7SGreg Kurz if (s->ctx.fst) { 4151b8bbdb88SPradeep Jagadeesh fsdev_throttle_cleanup(s->ctx.fst); 4152c0da0cb7SGreg Kurz } 41532a0c56aaSWei Liu g_free(s->tag); 41546b6aa828SChristian Schoenebeck qp_table_destroy(&s->qpd_table); 4155f3fe4a2dSAntonios Motakis qp_table_destroy(&s->qpp_table); 4156f3fe4a2dSAntonios Motakis qp_table_destroy(&s->qpf_table); 41574774718eSLi Qiang g_free(s->ctx.fs_root); 41582a0c56aaSWei Liu } 41592a0c56aaSWei Liu 41600e44a0fdSGreg Kurz typedef struct VirtfsCoResetData { 41610e44a0fdSGreg Kurz V9fsPDU pdu; 41620e44a0fdSGreg Kurz bool done; 41630e44a0fdSGreg Kurz } VirtfsCoResetData; 41640e44a0fdSGreg Kurz 41650e44a0fdSGreg Kurz static void coroutine_fn virtfs_co_reset(void *opaque) 41660e44a0fdSGreg Kurz { 41670e44a0fdSGreg Kurz VirtfsCoResetData *data = opaque; 41680e44a0fdSGreg Kurz 41690e44a0fdSGreg Kurz virtfs_reset(&data->pdu); 41700e44a0fdSGreg Kurz data->done = true; 41710e44a0fdSGreg Kurz } 41720e44a0fdSGreg Kurz 41730e44a0fdSGreg Kurz void v9fs_reset(V9fsState *s) 41740e44a0fdSGreg Kurz { 41750e44a0fdSGreg Kurz VirtfsCoResetData data = { .pdu = { .s = s }, .done = false }; 41760e44a0fdSGreg Kurz Coroutine *co; 41770e44a0fdSGreg Kurz 41780e44a0fdSGreg Kurz while (!QLIST_EMPTY(&s->active_list)) { 41790e44a0fdSGreg Kurz aio_poll(qemu_get_aio_context(), true); 41800e44a0fdSGreg Kurz } 41810e44a0fdSGreg Kurz 41820e44a0fdSGreg Kurz co = qemu_coroutine_create(virtfs_co_reset, &data); 41830e44a0fdSGreg Kurz qemu_coroutine_enter(co); 41840e44a0fdSGreg Kurz 41850e44a0fdSGreg Kurz while (!data.done) { 41860e44a0fdSGreg Kurz aio_poll(qemu_get_aio_context(), true); 41870e44a0fdSGreg Kurz } 41880e44a0fdSGreg Kurz } 41890e44a0fdSGreg Kurz 419060ce86c7SWei Liu static void __attribute__((__constructor__)) v9fs_set_fd_limit(void) 419160ce86c7SWei Liu { 419260ce86c7SWei Liu struct rlimit rlim; 419360ce86c7SWei Liu if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) { 419463325b18SGreg Kurz error_report("Failed to get the resource limit"); 419560ce86c7SWei Liu exit(1); 419660ce86c7SWei Liu } 419760ce86c7SWei Liu open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur / 3); 419860ce86c7SWei Liu open_fd_rc = rlim.rlim_cur / 2; 419960ce86c7SWei Liu } 4200