160ce86c7SWei Liu /* 260ce86c7SWei Liu * Virtio 9p backend 360ce86c7SWei Liu * 460ce86c7SWei Liu * Copyright IBM, Corp. 2010 560ce86c7SWei Liu * 660ce86c7SWei Liu * Authors: 760ce86c7SWei Liu * Anthony Liguori <aliguori@us.ibm.com> 860ce86c7SWei Liu * 960ce86c7SWei Liu * This work is licensed under the terms of the GNU GPL, version 2. See 1060ce86c7SWei Liu * the COPYING file in the top-level directory. 1160ce86c7SWei Liu * 1260ce86c7SWei Liu */ 1360ce86c7SWei Liu 14fbc04127SPeter Maydell #include "qemu/osdep.h" 15e3e83f2eSGreg Kurz #include <glib/gprintf.h> 1660ce86c7SWei Liu #include "hw/virtio/virtio.h" 17da34e65cSMarkus Armbruster #include "qapi/error.h" 1860ce86c7SWei Liu #include "qemu/error-report.h" 1960ce86c7SWei Liu #include "qemu/iov.h" 20db725815SMarkus Armbruster #include "qemu/main-loop.h" 2160ce86c7SWei Liu #include "qemu/sockets.h" 2260ce86c7SWei Liu #include "virtio-9p.h" 2360ce86c7SWei Liu #include "fsdev/qemu-fsdev.h" 2460ce86c7SWei Liu #include "9p-xattr.h" 2560ce86c7SWei Liu #include "coth.h" 2660ce86c7SWei Liu #include "trace.h" 27795c40b8SJuan Quintela #include "migration/blocker.h" 28357e2f7fSGreg Kurz #include "sysemu/qtest.h" 291a6ed33cSAntonios Motakis #include "qemu/xxhash.h" 30*6b6aa828SChristian Schoenebeck #include <math.h> 3160ce86c7SWei Liu 3260ce86c7SWei Liu int open_fd_hw; 3360ce86c7SWei Liu int total_open_fd; 3460ce86c7SWei Liu static int open_fd_rc; 3560ce86c7SWei Liu 3660ce86c7SWei Liu enum { 3760ce86c7SWei Liu Oread = 0x00, 3860ce86c7SWei Liu Owrite = 0x01, 3960ce86c7SWei Liu Ordwr = 0x02, 4060ce86c7SWei Liu Oexec = 0x03, 4160ce86c7SWei Liu Oexcl = 0x04, 4260ce86c7SWei Liu Otrunc = 0x10, 4360ce86c7SWei Liu Orexec = 0x20, 4460ce86c7SWei Liu Orclose = 0x40, 4560ce86c7SWei Liu Oappend = 0x80, 4660ce86c7SWei Liu }; 4760ce86c7SWei Liu 4875673590SGreg Kurz static ssize_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...) 4960ce86c7SWei Liu { 5060ce86c7SWei Liu ssize_t ret; 5160ce86c7SWei Liu va_list ap; 5260ce86c7SWei Liu 5360ce86c7SWei Liu va_start(ap, fmt); 54ea83441cSStefano Stabellini ret = pdu->s->transport->pdu_vmarshal(pdu, offset, fmt, ap); 5560ce86c7SWei Liu va_end(ap); 5660ce86c7SWei Liu 5760ce86c7SWei Liu return ret; 5860ce86c7SWei Liu } 5960ce86c7SWei Liu 6075673590SGreg Kurz static ssize_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...) 6160ce86c7SWei Liu { 6260ce86c7SWei Liu ssize_t ret; 6360ce86c7SWei Liu va_list ap; 6460ce86c7SWei Liu 6560ce86c7SWei Liu va_start(ap, fmt); 66ea83441cSStefano Stabellini ret = pdu->s->transport->pdu_vunmarshal(pdu, offset, fmt, ap); 6760ce86c7SWei Liu va_end(ap); 6860ce86c7SWei Liu 6960ce86c7SWei Liu return ret; 7060ce86c7SWei Liu } 7160ce86c7SWei Liu 7260ce86c7SWei Liu static int omode_to_uflags(int8_t mode) 7360ce86c7SWei Liu { 7460ce86c7SWei Liu int ret = 0; 7560ce86c7SWei Liu 7660ce86c7SWei Liu switch (mode & 3) { 7760ce86c7SWei Liu case Oread: 7860ce86c7SWei Liu ret = O_RDONLY; 7960ce86c7SWei Liu break; 8060ce86c7SWei Liu case Ordwr: 8160ce86c7SWei Liu ret = O_RDWR; 8260ce86c7SWei Liu break; 8360ce86c7SWei Liu case Owrite: 8460ce86c7SWei Liu ret = O_WRONLY; 8560ce86c7SWei Liu break; 8660ce86c7SWei Liu case Oexec: 8760ce86c7SWei Liu ret = O_RDONLY; 8860ce86c7SWei Liu break; 8960ce86c7SWei Liu } 9060ce86c7SWei Liu 9160ce86c7SWei Liu if (mode & Otrunc) { 9260ce86c7SWei Liu ret |= O_TRUNC; 9360ce86c7SWei Liu } 9460ce86c7SWei Liu 9560ce86c7SWei Liu if (mode & Oappend) { 9660ce86c7SWei Liu ret |= O_APPEND; 9760ce86c7SWei Liu } 9860ce86c7SWei Liu 9960ce86c7SWei Liu if (mode & Oexcl) { 10060ce86c7SWei Liu ret |= O_EXCL; 10160ce86c7SWei Liu } 10260ce86c7SWei Liu 10360ce86c7SWei Liu return ret; 10460ce86c7SWei Liu } 10560ce86c7SWei Liu 1068e71b96cSGreg Kurz typedef struct DotlOpenflagMap { 10760ce86c7SWei Liu int dotl_flag; 10860ce86c7SWei Liu int open_flag; 1098e71b96cSGreg Kurz } DotlOpenflagMap; 11060ce86c7SWei Liu 11160ce86c7SWei Liu static int dotl_to_open_flags(int flags) 11260ce86c7SWei Liu { 11360ce86c7SWei Liu int i; 11460ce86c7SWei Liu /* 11560ce86c7SWei Liu * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY 11660ce86c7SWei Liu * and P9_DOTL_NOACCESS 11760ce86c7SWei Liu */ 11860ce86c7SWei Liu int oflags = flags & O_ACCMODE; 11960ce86c7SWei Liu 1208e71b96cSGreg Kurz DotlOpenflagMap dotl_oflag_map[] = { 12160ce86c7SWei Liu { P9_DOTL_CREATE, O_CREAT }, 12260ce86c7SWei Liu { P9_DOTL_EXCL, O_EXCL }, 12360ce86c7SWei Liu { P9_DOTL_NOCTTY , O_NOCTTY }, 12460ce86c7SWei Liu { P9_DOTL_TRUNC, O_TRUNC }, 12560ce86c7SWei Liu { P9_DOTL_APPEND, O_APPEND }, 12660ce86c7SWei Liu { P9_DOTL_NONBLOCK, O_NONBLOCK } , 12760ce86c7SWei Liu { P9_DOTL_DSYNC, O_DSYNC }, 12860ce86c7SWei Liu { P9_DOTL_FASYNC, FASYNC }, 12960ce86c7SWei Liu { P9_DOTL_DIRECT, O_DIRECT }, 13060ce86c7SWei Liu { P9_DOTL_LARGEFILE, O_LARGEFILE }, 13160ce86c7SWei Liu { P9_DOTL_DIRECTORY, O_DIRECTORY }, 13260ce86c7SWei Liu { P9_DOTL_NOFOLLOW, O_NOFOLLOW }, 13360ce86c7SWei Liu { P9_DOTL_NOATIME, O_NOATIME }, 13460ce86c7SWei Liu { P9_DOTL_SYNC, O_SYNC }, 13560ce86c7SWei Liu }; 13660ce86c7SWei Liu 13760ce86c7SWei Liu for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) { 13860ce86c7SWei Liu if (flags & dotl_oflag_map[i].dotl_flag) { 13960ce86c7SWei Liu oflags |= dotl_oflag_map[i].open_flag; 14060ce86c7SWei Liu } 14160ce86c7SWei Liu } 14260ce86c7SWei Liu 14360ce86c7SWei Liu return oflags; 14460ce86c7SWei Liu } 14560ce86c7SWei Liu 14660ce86c7SWei Liu void cred_init(FsCred *credp) 14760ce86c7SWei Liu { 14860ce86c7SWei Liu credp->fc_uid = -1; 14960ce86c7SWei Liu credp->fc_gid = -1; 15060ce86c7SWei Liu credp->fc_mode = -1; 15160ce86c7SWei Liu credp->fc_rdev = -1; 15260ce86c7SWei Liu } 15360ce86c7SWei Liu 15460ce86c7SWei Liu static int get_dotl_openflags(V9fsState *s, int oflags) 15560ce86c7SWei Liu { 15660ce86c7SWei Liu int flags; 15760ce86c7SWei Liu /* 15860ce86c7SWei Liu * Filter the client open flags 15960ce86c7SWei Liu */ 16060ce86c7SWei Liu flags = dotl_to_open_flags(oflags); 16160ce86c7SWei Liu flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT); 16260ce86c7SWei Liu /* 16360ce86c7SWei Liu * Ignore direct disk access hint until the server supports it. 16460ce86c7SWei Liu */ 16560ce86c7SWei Liu flags &= ~O_DIRECT; 16660ce86c7SWei Liu return flags; 16760ce86c7SWei Liu } 16860ce86c7SWei Liu 16960ce86c7SWei Liu void v9fs_path_init(V9fsPath *path) 17060ce86c7SWei Liu { 17160ce86c7SWei Liu path->data = NULL; 17260ce86c7SWei Liu path->size = 0; 17360ce86c7SWei Liu } 17460ce86c7SWei Liu 17560ce86c7SWei Liu void v9fs_path_free(V9fsPath *path) 17660ce86c7SWei Liu { 17760ce86c7SWei Liu g_free(path->data); 17860ce86c7SWei Liu path->data = NULL; 17960ce86c7SWei Liu path->size = 0; 18060ce86c7SWei Liu } 18160ce86c7SWei Liu 182e3e83f2eSGreg Kurz 183e3e83f2eSGreg Kurz void GCC_FMT_ATTR(2, 3) 184e3e83f2eSGreg Kurz v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...) 185e3e83f2eSGreg Kurz { 186e3e83f2eSGreg Kurz va_list ap; 187e3e83f2eSGreg Kurz 188e3e83f2eSGreg Kurz v9fs_path_free(path); 189e3e83f2eSGreg Kurz 190e3e83f2eSGreg Kurz va_start(ap, fmt); 191e3e83f2eSGreg Kurz /* Bump the size for including terminating NULL */ 192e3e83f2eSGreg Kurz path->size = g_vasprintf(&path->data, fmt, ap) + 1; 193e3e83f2eSGreg Kurz va_end(ap); 194e3e83f2eSGreg Kurz } 195e3e83f2eSGreg Kurz 196e446a1ebSMarc-André Lureau void v9fs_path_copy(V9fsPath *dst, const V9fsPath *src) 19760ce86c7SWei Liu { 198e446a1ebSMarc-André Lureau v9fs_path_free(dst); 199e446a1ebSMarc-André Lureau dst->size = src->size; 200e446a1ebSMarc-André Lureau dst->data = g_memdup(src->data, src->size); 20160ce86c7SWei Liu } 20260ce86c7SWei Liu 20360ce86c7SWei Liu int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath, 20460ce86c7SWei Liu const char *name, V9fsPath *path) 20560ce86c7SWei Liu { 20660ce86c7SWei Liu int err; 20760ce86c7SWei Liu err = s->ops->name_to_path(&s->ctx, dirpath, name, path); 20860ce86c7SWei Liu if (err < 0) { 20960ce86c7SWei Liu err = -errno; 21060ce86c7SWei Liu } 21160ce86c7SWei Liu return err; 21260ce86c7SWei Liu } 21360ce86c7SWei Liu 21460ce86c7SWei Liu /* 21560ce86c7SWei Liu * Return TRUE if s1 is an ancestor of s2. 21660ce86c7SWei Liu * 21760ce86c7SWei Liu * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d". 21860ce86c7SWei Liu * As a special case, We treat s1 as ancestor of s2 if they are same! 21960ce86c7SWei Liu */ 22060ce86c7SWei Liu static int v9fs_path_is_ancestor(V9fsPath *s1, V9fsPath *s2) 22160ce86c7SWei Liu { 22260ce86c7SWei Liu if (!strncmp(s1->data, s2->data, s1->size - 1)) { 22360ce86c7SWei Liu if (s2->data[s1->size - 1] == '\0' || s2->data[s1->size - 1] == '/') { 22460ce86c7SWei Liu return 1; 22560ce86c7SWei Liu } 22660ce86c7SWei Liu } 22760ce86c7SWei Liu return 0; 22860ce86c7SWei Liu } 22960ce86c7SWei Liu 23060ce86c7SWei Liu static size_t v9fs_string_size(V9fsString *str) 23160ce86c7SWei Liu { 23260ce86c7SWei Liu return str->size; 23360ce86c7SWei Liu } 23460ce86c7SWei Liu 23560ce86c7SWei Liu /* 23660ce86c7SWei Liu * returns 0 if fid got re-opened, 1 if not, < 0 on error */ 2378440e22eSGreg Kurz static int coroutine_fn v9fs_reopen_fid(V9fsPDU *pdu, V9fsFidState *f) 23860ce86c7SWei Liu { 23960ce86c7SWei Liu int err = 1; 24060ce86c7SWei Liu if (f->fid_type == P9_FID_FILE) { 24160ce86c7SWei Liu if (f->fs.fd == -1) { 24260ce86c7SWei Liu do { 24360ce86c7SWei Liu err = v9fs_co_open(pdu, f, f->open_flags); 24460ce86c7SWei Liu } while (err == -EINTR && !pdu->cancelled); 24560ce86c7SWei Liu } 24660ce86c7SWei Liu } else if (f->fid_type == P9_FID_DIR) { 247f314ea4eSGreg Kurz if (f->fs.dir.stream == NULL) { 24860ce86c7SWei Liu do { 24960ce86c7SWei Liu err = v9fs_co_opendir(pdu, f); 25060ce86c7SWei Liu } while (err == -EINTR && !pdu->cancelled); 25160ce86c7SWei Liu } 25260ce86c7SWei Liu } 25360ce86c7SWei Liu return err; 25460ce86c7SWei Liu } 25560ce86c7SWei Liu 2568440e22eSGreg Kurz static V9fsFidState *coroutine_fn get_fid(V9fsPDU *pdu, int32_t fid) 25760ce86c7SWei Liu { 25860ce86c7SWei Liu int err; 25960ce86c7SWei Liu V9fsFidState *f; 26060ce86c7SWei Liu V9fsState *s = pdu->s; 26160ce86c7SWei Liu 26260ce86c7SWei Liu for (f = s->fid_list; f; f = f->next) { 26360ce86c7SWei Liu BUG_ON(f->clunked); 26460ce86c7SWei Liu if (f->fid == fid) { 26560ce86c7SWei Liu /* 26660ce86c7SWei Liu * Update the fid ref upfront so that 26760ce86c7SWei Liu * we don't get reclaimed when we yield 26860ce86c7SWei Liu * in open later. 26960ce86c7SWei Liu */ 27060ce86c7SWei Liu f->ref++; 27160ce86c7SWei Liu /* 27260ce86c7SWei Liu * check whether we need to reopen the 27360ce86c7SWei Liu * file. We might have closed the fd 27460ce86c7SWei Liu * while trying to free up some file 27560ce86c7SWei Liu * descriptors. 27660ce86c7SWei Liu */ 27760ce86c7SWei Liu err = v9fs_reopen_fid(pdu, f); 27860ce86c7SWei Liu if (err < 0) { 27960ce86c7SWei Liu f->ref--; 28060ce86c7SWei Liu return NULL; 28160ce86c7SWei Liu } 28260ce86c7SWei Liu /* 28360ce86c7SWei Liu * Mark the fid as referenced so that the LRU 28460ce86c7SWei Liu * reclaim won't close the file descriptor 28560ce86c7SWei Liu */ 28660ce86c7SWei Liu f->flags |= FID_REFERENCED; 28760ce86c7SWei Liu return f; 28860ce86c7SWei Liu } 28960ce86c7SWei Liu } 29060ce86c7SWei Liu return NULL; 29160ce86c7SWei Liu } 29260ce86c7SWei Liu 29360ce86c7SWei Liu static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid) 29460ce86c7SWei Liu { 29560ce86c7SWei Liu V9fsFidState *f; 29660ce86c7SWei Liu 29760ce86c7SWei Liu for (f = s->fid_list; f; f = f->next) { 29860ce86c7SWei Liu /* If fid is already there return NULL */ 29960ce86c7SWei Liu BUG_ON(f->clunked); 30060ce86c7SWei Liu if (f->fid == fid) { 30160ce86c7SWei Liu return NULL; 30260ce86c7SWei Liu } 30360ce86c7SWei Liu } 30460ce86c7SWei Liu f = g_malloc0(sizeof(V9fsFidState)); 30560ce86c7SWei Liu f->fid = fid; 30660ce86c7SWei Liu f->fid_type = P9_FID_NONE; 30760ce86c7SWei Liu f->ref = 1; 30860ce86c7SWei Liu /* 30960ce86c7SWei Liu * Mark the fid as referenced so that the LRU 31060ce86c7SWei Liu * reclaim won't close the file descriptor 31160ce86c7SWei Liu */ 31260ce86c7SWei Liu f->flags |= FID_REFERENCED; 31360ce86c7SWei Liu f->next = s->fid_list; 31460ce86c7SWei Liu s->fid_list = f; 31560ce86c7SWei Liu 3167cde47d4SGreg Kurz v9fs_readdir_init(&f->fs.dir); 3177cde47d4SGreg Kurz v9fs_readdir_init(&f->fs_reclaim.dir); 3187cde47d4SGreg Kurz 31960ce86c7SWei Liu return f; 32060ce86c7SWei Liu } 32160ce86c7SWei Liu 3228440e22eSGreg Kurz static int coroutine_fn v9fs_xattr_fid_clunk(V9fsPDU *pdu, V9fsFidState *fidp) 32360ce86c7SWei Liu { 32460ce86c7SWei Liu int retval = 0; 32560ce86c7SWei Liu 326dd28fbbcSLi Qiang if (fidp->fs.xattr.xattrwalk_fid) { 32760ce86c7SWei Liu /* getxattr/listxattr fid */ 32860ce86c7SWei Liu goto free_value; 32960ce86c7SWei Liu } 33060ce86c7SWei Liu /* 33160ce86c7SWei Liu * if this is fid for setxattr. clunk should 33260ce86c7SWei Liu * result in setxattr localcall 33360ce86c7SWei Liu */ 33460ce86c7SWei Liu if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) { 33560ce86c7SWei Liu /* clunk after partial write */ 33660ce86c7SWei Liu retval = -EINVAL; 33760ce86c7SWei Liu goto free_out; 33860ce86c7SWei Liu } 33960ce86c7SWei Liu if (fidp->fs.xattr.len) { 34060ce86c7SWei Liu retval = v9fs_co_lsetxattr(pdu, &fidp->path, &fidp->fs.xattr.name, 34160ce86c7SWei Liu fidp->fs.xattr.value, 34260ce86c7SWei Liu fidp->fs.xattr.len, 34360ce86c7SWei Liu fidp->fs.xattr.flags); 34460ce86c7SWei Liu } else { 34560ce86c7SWei Liu retval = v9fs_co_lremovexattr(pdu, &fidp->path, &fidp->fs.xattr.name); 34660ce86c7SWei Liu } 34760ce86c7SWei Liu free_out: 34860ce86c7SWei Liu v9fs_string_free(&fidp->fs.xattr.name); 34960ce86c7SWei Liu free_value: 35060ce86c7SWei Liu g_free(fidp->fs.xattr.value); 35160ce86c7SWei Liu return retval; 35260ce86c7SWei Liu } 35360ce86c7SWei Liu 3548440e22eSGreg Kurz static int coroutine_fn free_fid(V9fsPDU *pdu, V9fsFidState *fidp) 35560ce86c7SWei Liu { 35660ce86c7SWei Liu int retval = 0; 35760ce86c7SWei Liu 35860ce86c7SWei Liu if (fidp->fid_type == P9_FID_FILE) { 35960ce86c7SWei Liu /* If we reclaimed the fd no need to close */ 36060ce86c7SWei Liu if (fidp->fs.fd != -1) { 36160ce86c7SWei Liu retval = v9fs_co_close(pdu, &fidp->fs); 36260ce86c7SWei Liu } 36360ce86c7SWei Liu } else if (fidp->fid_type == P9_FID_DIR) { 364f314ea4eSGreg Kurz if (fidp->fs.dir.stream != NULL) { 36560ce86c7SWei Liu retval = v9fs_co_closedir(pdu, &fidp->fs); 36660ce86c7SWei Liu } 36760ce86c7SWei Liu } else if (fidp->fid_type == P9_FID_XATTR) { 36860ce86c7SWei Liu retval = v9fs_xattr_fid_clunk(pdu, fidp); 36960ce86c7SWei Liu } 37060ce86c7SWei Liu v9fs_path_free(&fidp->path); 37160ce86c7SWei Liu g_free(fidp); 37260ce86c7SWei Liu return retval; 37360ce86c7SWei Liu } 37460ce86c7SWei Liu 3758440e22eSGreg Kurz static int coroutine_fn put_fid(V9fsPDU *pdu, V9fsFidState *fidp) 37660ce86c7SWei Liu { 37760ce86c7SWei Liu BUG_ON(!fidp->ref); 37860ce86c7SWei Liu fidp->ref--; 37960ce86c7SWei Liu /* 38060ce86c7SWei Liu * Don't free the fid if it is in reclaim list 38160ce86c7SWei Liu */ 38260ce86c7SWei Liu if (!fidp->ref && fidp->clunked) { 38360ce86c7SWei Liu if (fidp->fid == pdu->s->root_fid) { 38460ce86c7SWei Liu /* 38560ce86c7SWei Liu * if the clunked fid is root fid then we 38660ce86c7SWei Liu * have unmounted the fs on the client side. 38760ce86c7SWei Liu * delete the migration blocker. Ideally, this 38860ce86c7SWei Liu * should be hooked to transport close notification 38960ce86c7SWei Liu */ 39060ce86c7SWei Liu if (pdu->s->migration_blocker) { 39160ce86c7SWei Liu migrate_del_blocker(pdu->s->migration_blocker); 39260ce86c7SWei Liu error_free(pdu->s->migration_blocker); 39360ce86c7SWei Liu pdu->s->migration_blocker = NULL; 39460ce86c7SWei Liu } 39560ce86c7SWei Liu } 39660ce86c7SWei Liu return free_fid(pdu, fidp); 39760ce86c7SWei Liu } 39860ce86c7SWei Liu return 0; 39960ce86c7SWei Liu } 40060ce86c7SWei Liu 40160ce86c7SWei Liu static V9fsFidState *clunk_fid(V9fsState *s, int32_t fid) 40260ce86c7SWei Liu { 40360ce86c7SWei Liu V9fsFidState **fidpp, *fidp; 40460ce86c7SWei Liu 40560ce86c7SWei Liu for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) { 40660ce86c7SWei Liu if ((*fidpp)->fid == fid) { 40760ce86c7SWei Liu break; 40860ce86c7SWei Liu } 40960ce86c7SWei Liu } 41060ce86c7SWei Liu if (*fidpp == NULL) { 41160ce86c7SWei Liu return NULL; 41260ce86c7SWei Liu } 41360ce86c7SWei Liu fidp = *fidpp; 41460ce86c7SWei Liu *fidpp = fidp->next; 41560ce86c7SWei Liu fidp->clunked = 1; 41660ce86c7SWei Liu return fidp; 41760ce86c7SWei Liu } 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; 42360ce86c7SWei Liu V9fsFidState *f, *reclaim_list = NULL; 42460ce86c7SWei Liu 42560ce86c7SWei Liu for (f = s->fid_list; f; f = f->next) { 42660ce86c7SWei Liu /* 42760ce86c7SWei Liu * Unlink fids cannot be reclaimed. Check 42860ce86c7SWei Liu * for them and skip them. Also skip fids 42960ce86c7SWei Liu * currently being operated on. 43060ce86c7SWei Liu */ 43160ce86c7SWei Liu if (f->ref || f->flags & FID_NON_RECLAIMABLE) { 43260ce86c7SWei Liu continue; 43360ce86c7SWei Liu } 43460ce86c7SWei Liu /* 43560ce86c7SWei Liu * if it is a recently referenced fid 43660ce86c7SWei Liu * we leave the fid untouched and clear the 43760ce86c7SWei Liu * reference bit. We come back to it later 43860ce86c7SWei Liu * in the next iteration. (a simple LRU without 43960ce86c7SWei Liu * moving list elements around) 44060ce86c7SWei Liu */ 44160ce86c7SWei Liu if (f->flags & FID_REFERENCED) { 44260ce86c7SWei Liu f->flags &= ~FID_REFERENCED; 44360ce86c7SWei Liu continue; 44460ce86c7SWei Liu } 44560ce86c7SWei Liu /* 44660ce86c7SWei Liu * Add fids to reclaim list. 44760ce86c7SWei Liu */ 44860ce86c7SWei Liu if (f->fid_type == P9_FID_FILE) { 44960ce86c7SWei Liu if (f->fs.fd != -1) { 45060ce86c7SWei Liu /* 45160ce86c7SWei Liu * Up the reference count so that 45260ce86c7SWei Liu * a clunk request won't free this fid 45360ce86c7SWei Liu */ 45460ce86c7SWei Liu f->ref++; 45560ce86c7SWei Liu f->rclm_lst = reclaim_list; 45660ce86c7SWei Liu reclaim_list = f; 45760ce86c7SWei Liu f->fs_reclaim.fd = f->fs.fd; 45860ce86c7SWei Liu f->fs.fd = -1; 45960ce86c7SWei Liu reclaim_count++; 46060ce86c7SWei Liu } 46160ce86c7SWei Liu } else if (f->fid_type == P9_FID_DIR) { 462f314ea4eSGreg Kurz if (f->fs.dir.stream != NULL) { 46360ce86c7SWei Liu /* 46460ce86c7SWei Liu * Up the reference count so that 46560ce86c7SWei Liu * a clunk request won't free this fid 46660ce86c7SWei Liu */ 46760ce86c7SWei Liu f->ref++; 46860ce86c7SWei Liu f->rclm_lst = reclaim_list; 46960ce86c7SWei Liu reclaim_list = f; 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 */ 48360ce86c7SWei Liu while (reclaim_list) { 48460ce86c7SWei Liu f = reclaim_list; 48560ce86c7SWei Liu reclaim_list = f->rclm_lst; 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 f->rclm_lst = NULL; 49260ce86c7SWei Liu /* 49360ce86c7SWei Liu * Now drop the fid reference, free it 49460ce86c7SWei Liu * if clunked. 49560ce86c7SWei Liu */ 49660ce86c7SWei Liu put_fid(pdu, f); 49760ce86c7SWei Liu } 49860ce86c7SWei Liu } 49960ce86c7SWei Liu 5008440e22eSGreg Kurz static int coroutine_fn v9fs_mark_fids_unreclaim(V9fsPDU *pdu, V9fsPath *path) 50160ce86c7SWei Liu { 50260ce86c7SWei Liu int err; 50360ce86c7SWei Liu V9fsState *s = pdu->s; 50460ce86c7SWei Liu V9fsFidState *fidp, head_fid; 50560ce86c7SWei Liu 50660ce86c7SWei Liu head_fid.next = s->fid_list; 50760ce86c7SWei Liu for (fidp = s->fid_list; fidp; fidp = fidp->next) { 50860ce86c7SWei Liu if (fidp->path.size != path->size) { 50960ce86c7SWei Liu continue; 51060ce86c7SWei Liu } 51160ce86c7SWei Liu if (!memcmp(fidp->path.data, path->data, path->size)) { 51260ce86c7SWei Liu /* Mark the fid non reclaimable. */ 51360ce86c7SWei Liu fidp->flags |= FID_NON_RECLAIMABLE; 51460ce86c7SWei Liu 51560ce86c7SWei Liu /* reopen the file/dir if already closed */ 51660ce86c7SWei Liu err = v9fs_reopen_fid(pdu, fidp); 51760ce86c7SWei Liu if (err < 0) { 518267fcadfSGreg Kurz return err; 51960ce86c7SWei Liu } 52060ce86c7SWei Liu /* 52160ce86c7SWei Liu * Go back to head of fid list because 52260ce86c7SWei Liu * the list could have got updated when 52360ce86c7SWei Liu * switched to the worker thread 52460ce86c7SWei Liu */ 52560ce86c7SWei Liu if (err == 0) { 52660ce86c7SWei Liu fidp = &head_fid; 52760ce86c7SWei Liu } 52860ce86c7SWei Liu } 52960ce86c7SWei Liu } 53060ce86c7SWei Liu return 0; 53160ce86c7SWei Liu } 53260ce86c7SWei Liu 5338440e22eSGreg Kurz static void coroutine_fn virtfs_reset(V9fsPDU *pdu) 53460ce86c7SWei Liu { 53560ce86c7SWei Liu V9fsState *s = pdu->s; 53679decce3SGreg Kurz V9fsFidState *fidp; 53760ce86c7SWei Liu 53860ce86c7SWei Liu /* Free all fids */ 53960ce86c7SWei Liu while (s->fid_list) { 5406d54af0eSGreg Kurz /* Get fid */ 54160ce86c7SWei Liu fidp = s->fid_list; 5426d54af0eSGreg Kurz fidp->ref++; 54360ce86c7SWei Liu 5446d54af0eSGreg Kurz /* Clunk fid */ 5456d54af0eSGreg Kurz s->fid_list = fidp->next; 54660ce86c7SWei Liu fidp->clunked = 1; 5476d54af0eSGreg Kurz 5486d54af0eSGreg Kurz put_fid(pdu, fidp); 54960ce86c7SWei Liu } 55060ce86c7SWei Liu } 55160ce86c7SWei Liu 55260ce86c7SWei Liu #define P9_QID_TYPE_DIR 0x80 55360ce86c7SWei Liu #define P9_QID_TYPE_SYMLINK 0x02 55460ce86c7SWei Liu 55560ce86c7SWei Liu #define P9_STAT_MODE_DIR 0x80000000 55660ce86c7SWei Liu #define P9_STAT_MODE_APPEND 0x40000000 55760ce86c7SWei Liu #define P9_STAT_MODE_EXCL 0x20000000 55860ce86c7SWei Liu #define P9_STAT_MODE_MOUNT 0x10000000 55960ce86c7SWei Liu #define P9_STAT_MODE_AUTH 0x08000000 56060ce86c7SWei Liu #define P9_STAT_MODE_TMP 0x04000000 56160ce86c7SWei Liu #define P9_STAT_MODE_SYMLINK 0x02000000 56260ce86c7SWei Liu #define P9_STAT_MODE_LINK 0x01000000 56360ce86c7SWei Liu #define P9_STAT_MODE_DEVICE 0x00800000 56460ce86c7SWei Liu #define P9_STAT_MODE_NAMED_PIPE 0x00200000 56560ce86c7SWei Liu #define P9_STAT_MODE_SOCKET 0x00100000 56660ce86c7SWei Liu #define P9_STAT_MODE_SETUID 0x00080000 56760ce86c7SWei Liu #define P9_STAT_MODE_SETGID 0x00040000 56860ce86c7SWei Liu #define P9_STAT_MODE_SETVTX 0x00010000 56960ce86c7SWei Liu 57060ce86c7SWei Liu #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \ 57160ce86c7SWei Liu P9_STAT_MODE_SYMLINK | \ 57260ce86c7SWei Liu P9_STAT_MODE_LINK | \ 57360ce86c7SWei Liu P9_STAT_MODE_DEVICE | \ 57460ce86c7SWei Liu P9_STAT_MODE_NAMED_PIPE | \ 57560ce86c7SWei Liu P9_STAT_MODE_SOCKET) 57660ce86c7SWei Liu 577*6b6aa828SChristian Schoenebeck /* Mirrors all bits of a byte. So e.g. binary 10100000 would become 00000101. */ 578*6b6aa828SChristian Schoenebeck static inline uint8_t mirror8bit(uint8_t byte) 579*6b6aa828SChristian Schoenebeck { 580*6b6aa828SChristian Schoenebeck return (byte * 0x0202020202ULL & 0x010884422010ULL) % 1023; 581*6b6aa828SChristian Schoenebeck } 582*6b6aa828SChristian Schoenebeck 583*6b6aa828SChristian Schoenebeck /* Same as mirror8bit() just for a 64 bit data type instead for a byte. */ 584*6b6aa828SChristian Schoenebeck static inline uint64_t mirror64bit(uint64_t value) 585*6b6aa828SChristian Schoenebeck { 586*6b6aa828SChristian Schoenebeck return ((uint64_t)mirror8bit(value & 0xff) << 56) | 587*6b6aa828SChristian Schoenebeck ((uint64_t)mirror8bit((value >> 8) & 0xff) << 48) | 588*6b6aa828SChristian Schoenebeck ((uint64_t)mirror8bit((value >> 16) & 0xff) << 40) | 589*6b6aa828SChristian Schoenebeck ((uint64_t)mirror8bit((value >> 24) & 0xff) << 32) | 590*6b6aa828SChristian Schoenebeck ((uint64_t)mirror8bit((value >> 32) & 0xff) << 24) | 591*6b6aa828SChristian Schoenebeck ((uint64_t)mirror8bit((value >> 40) & 0xff) << 16) | 592*6b6aa828SChristian Schoenebeck ((uint64_t)mirror8bit((value >> 48) & 0xff) << 8) | 593*6b6aa828SChristian Schoenebeck ((uint64_t)mirror8bit((value >> 56) & 0xff)); 594*6b6aa828SChristian Schoenebeck } 595*6b6aa828SChristian Schoenebeck 596*6b6aa828SChristian Schoenebeck /** 597*6b6aa828SChristian Schoenebeck * @brief Parameter k for the Exponential Golomb algorihm to be used. 598*6b6aa828SChristian Schoenebeck * 599*6b6aa828SChristian Schoenebeck * The smaller this value, the smaller the minimum bit count for the Exp. 600*6b6aa828SChristian Schoenebeck * Golomb generated affixes will be (at lowest index) however for the 601*6b6aa828SChristian Schoenebeck * price of having higher maximum bit count of generated affixes (at highest 602*6b6aa828SChristian Schoenebeck * index). Likewise increasing this parameter yields in smaller maximum bit 603*6b6aa828SChristian Schoenebeck * count for the price of having higher minimum bit count. 604*6b6aa828SChristian Schoenebeck * 605*6b6aa828SChristian Schoenebeck * In practice that means: a good value for k depends on the expected amount 606*6b6aa828SChristian Schoenebeck * of devices to be exposed by one export. For a small amount of devices k 607*6b6aa828SChristian Schoenebeck * should be small, for a large amount of devices k might be increased 608*6b6aa828SChristian Schoenebeck * instead. The default of k=0 should be fine for most users though. 609*6b6aa828SChristian Schoenebeck * 610*6b6aa828SChristian Schoenebeck * @b IMPORTANT: In case this ever becomes a runtime parameter; the value of 611*6b6aa828SChristian Schoenebeck * k should not change as long as guest is still running! Because that would 612*6b6aa828SChristian Schoenebeck * cause completely different inode numbers to be generated on guest. 613*6b6aa828SChristian Schoenebeck */ 614*6b6aa828SChristian Schoenebeck #define EXP_GOLOMB_K 0 615*6b6aa828SChristian Schoenebeck 616*6b6aa828SChristian Schoenebeck /** 617*6b6aa828SChristian Schoenebeck * @brief Exponential Golomb algorithm for arbitrary k (including k=0). 618*6b6aa828SChristian Schoenebeck * 619*6b6aa828SChristian Schoenebeck * The Exponential Golomb algorithm generates @b prefixes (@b not suffixes!) 620*6b6aa828SChristian Schoenebeck * with growing length and with the mathematical property of being 621*6b6aa828SChristian Schoenebeck * "prefix-free". The latter means the generated prefixes can be prepended 622*6b6aa828SChristian Schoenebeck * in front of arbitrary numbers and the resulting concatenated numbers are 623*6b6aa828SChristian Schoenebeck * guaranteed to be always unique. 624*6b6aa828SChristian Schoenebeck * 625*6b6aa828SChristian Schoenebeck * This is a minor adjustment to the original Exp. Golomb algorithm in the 626*6b6aa828SChristian Schoenebeck * sense that lowest allowed index (@param n) starts with 1, not with zero. 627*6b6aa828SChristian Schoenebeck * 628*6b6aa828SChristian Schoenebeck * @param n - natural number (or index) of the prefix to be generated 629*6b6aa828SChristian Schoenebeck * (1, 2, 3, ...) 630*6b6aa828SChristian Schoenebeck * @param k - parameter k of Exp. Golomb algorithm to be used 631*6b6aa828SChristian Schoenebeck * (see comment on EXP_GOLOMB_K macro for details about k) 632*6b6aa828SChristian Schoenebeck */ 633*6b6aa828SChristian Schoenebeck static VariLenAffix expGolombEncode(uint64_t n, int k) 634*6b6aa828SChristian Schoenebeck { 635*6b6aa828SChristian Schoenebeck const uint64_t value = n + (1 << k) - 1; 636*6b6aa828SChristian Schoenebeck const int bits = (int) log2(value) + 1; 637*6b6aa828SChristian Schoenebeck return (VariLenAffix) { 638*6b6aa828SChristian Schoenebeck .type = AffixType_Prefix, 639*6b6aa828SChristian Schoenebeck .value = value, 640*6b6aa828SChristian Schoenebeck .bits = bits + MAX((bits - 1 - k), 0) 641*6b6aa828SChristian Schoenebeck }; 642*6b6aa828SChristian Schoenebeck } 643*6b6aa828SChristian Schoenebeck 644*6b6aa828SChristian Schoenebeck /** 645*6b6aa828SChristian Schoenebeck * @brief Converts a suffix into a prefix, or a prefix into a suffix. 646*6b6aa828SChristian Schoenebeck * 647*6b6aa828SChristian Schoenebeck * Simply mirror all bits of the affix value, for the purpose to preserve 648*6b6aa828SChristian Schoenebeck * respectively the mathematical "prefix-free" or "suffix-free" property 649*6b6aa828SChristian Schoenebeck * after the conversion. 650*6b6aa828SChristian Schoenebeck * 651*6b6aa828SChristian Schoenebeck * If a passed prefix is suitable to create unique numbers, then the 652*6b6aa828SChristian Schoenebeck * returned suffix is suitable to create unique numbers as well (and vice 653*6b6aa828SChristian Schoenebeck * versa). 654*6b6aa828SChristian Schoenebeck */ 655*6b6aa828SChristian Schoenebeck static VariLenAffix invertAffix(const VariLenAffix *affix) 656*6b6aa828SChristian Schoenebeck { 657*6b6aa828SChristian Schoenebeck return (VariLenAffix) { 658*6b6aa828SChristian Schoenebeck .type = 659*6b6aa828SChristian Schoenebeck (affix->type == AffixType_Suffix) ? 660*6b6aa828SChristian Schoenebeck AffixType_Prefix : AffixType_Suffix, 661*6b6aa828SChristian Schoenebeck .value = 662*6b6aa828SChristian Schoenebeck mirror64bit(affix->value) >> 663*6b6aa828SChristian Schoenebeck ((sizeof(affix->value) * 8) - affix->bits), 664*6b6aa828SChristian Schoenebeck .bits = affix->bits 665*6b6aa828SChristian Schoenebeck }; 666*6b6aa828SChristian Schoenebeck } 667*6b6aa828SChristian Schoenebeck 668*6b6aa828SChristian Schoenebeck /** 669*6b6aa828SChristian Schoenebeck * @brief Generates suffix numbers with "suffix-free" property. 670*6b6aa828SChristian Schoenebeck * 671*6b6aa828SChristian Schoenebeck * This is just a wrapper function on top of the Exp. Golomb algorithm. 672*6b6aa828SChristian Schoenebeck * 673*6b6aa828SChristian Schoenebeck * Since the Exp. Golomb algorithm generates prefixes, but we need suffixes, 674*6b6aa828SChristian Schoenebeck * this function converts the Exp. Golomb prefixes into appropriate suffixes 675*6b6aa828SChristian Schoenebeck * which are still suitable for generating unique numbers. 676*6b6aa828SChristian Schoenebeck * 677*6b6aa828SChristian Schoenebeck * @param n - natural number (or index) of the suffix to be generated 678*6b6aa828SChristian Schoenebeck * (1, 2, 3, ...) 679*6b6aa828SChristian Schoenebeck */ 680*6b6aa828SChristian Schoenebeck static VariLenAffix affixForIndex(uint64_t index) 681*6b6aa828SChristian Schoenebeck { 682*6b6aa828SChristian Schoenebeck VariLenAffix prefix; 683*6b6aa828SChristian Schoenebeck prefix = expGolombEncode(index, EXP_GOLOMB_K); 684*6b6aa828SChristian Schoenebeck return invertAffix(&prefix); /* convert prefix to suffix */ 685*6b6aa828SChristian Schoenebeck } 686*6b6aa828SChristian Schoenebeck 6871a6ed33cSAntonios Motakis /* creative abuse of tb_hash_func7, which is based on xxhash */ 6881a6ed33cSAntonios Motakis static uint32_t qpp_hash(QppEntry e) 6891a6ed33cSAntonios Motakis { 6901a6ed33cSAntonios Motakis return qemu_xxhash7(e.ino_prefix, e.dev, 0, 0, 0); 6911a6ed33cSAntonios Motakis } 6921a6ed33cSAntonios Motakis 693f3fe4a2dSAntonios Motakis static uint32_t qpf_hash(QpfEntry e) 694f3fe4a2dSAntonios Motakis { 695f3fe4a2dSAntonios Motakis return qemu_xxhash7(e.ino, e.dev, 0, 0, 0); 696f3fe4a2dSAntonios Motakis } 697f3fe4a2dSAntonios Motakis 698*6b6aa828SChristian Schoenebeck static bool qpd_cmp_func(const void *obj, const void *userp) 699*6b6aa828SChristian Schoenebeck { 700*6b6aa828SChristian Schoenebeck const QpdEntry *e1 = obj, *e2 = userp; 701*6b6aa828SChristian Schoenebeck return e1->dev == e2->dev; 702*6b6aa828SChristian Schoenebeck } 703*6b6aa828SChristian Schoenebeck 704*6b6aa828SChristian Schoenebeck static bool qpp_cmp_func(const void *obj, const void *userp) 7051a6ed33cSAntonios Motakis { 7061a6ed33cSAntonios Motakis const QppEntry *e1 = obj, *e2 = userp; 7071a6ed33cSAntonios Motakis return e1->dev == e2->dev && e1->ino_prefix == e2->ino_prefix; 7081a6ed33cSAntonios Motakis } 7091a6ed33cSAntonios Motakis 710*6b6aa828SChristian Schoenebeck static bool qpf_cmp_func(const void *obj, const void *userp) 711f3fe4a2dSAntonios Motakis { 712f3fe4a2dSAntonios Motakis const QpfEntry *e1 = obj, *e2 = userp; 713f3fe4a2dSAntonios Motakis return e1->dev == e2->dev && e1->ino == e2->ino; 714f3fe4a2dSAntonios Motakis } 715f3fe4a2dSAntonios Motakis 716f3fe4a2dSAntonios Motakis static void qp_table_remove(void *p, uint32_t h, void *up) 7171a6ed33cSAntonios Motakis { 7181a6ed33cSAntonios Motakis g_free(p); 7191a6ed33cSAntonios Motakis } 7201a6ed33cSAntonios Motakis 721f3fe4a2dSAntonios Motakis static void qp_table_destroy(struct qht *ht) 7221a6ed33cSAntonios Motakis { 7231a6ed33cSAntonios Motakis if (!ht || !ht->map) { 7241a6ed33cSAntonios Motakis return; 7251a6ed33cSAntonios Motakis } 726f3fe4a2dSAntonios Motakis qht_iter(ht, qp_table_remove, NULL); 7271a6ed33cSAntonios Motakis qht_destroy(ht); 7281a6ed33cSAntonios Motakis } 7291a6ed33cSAntonios Motakis 730*6b6aa828SChristian Schoenebeck static void qpd_table_init(struct qht *ht) 731*6b6aa828SChristian Schoenebeck { 732*6b6aa828SChristian Schoenebeck qht_init(ht, qpd_cmp_func, 1, QHT_MODE_AUTO_RESIZE); 733*6b6aa828SChristian Schoenebeck } 734*6b6aa828SChristian Schoenebeck 7351a6ed33cSAntonios Motakis static void qpp_table_init(struct qht *ht) 7361a6ed33cSAntonios Motakis { 737*6b6aa828SChristian Schoenebeck qht_init(ht, qpp_cmp_func, 1, QHT_MODE_AUTO_RESIZE); 7381a6ed33cSAntonios Motakis } 7391a6ed33cSAntonios Motakis 740f3fe4a2dSAntonios Motakis static void qpf_table_init(struct qht *ht) 741f3fe4a2dSAntonios Motakis { 742*6b6aa828SChristian Schoenebeck qht_init(ht, qpf_cmp_func, 1 << 16, QHT_MODE_AUTO_RESIZE); 743f3fe4a2dSAntonios Motakis } 744f3fe4a2dSAntonios Motakis 745*6b6aa828SChristian Schoenebeck /* 746*6b6aa828SChristian Schoenebeck * Returns how many (high end) bits of inode numbers of the passed fs 747*6b6aa828SChristian Schoenebeck * device shall be used (in combination with the device number) to 748*6b6aa828SChristian Schoenebeck * generate hash values for qpp_table entries. 749*6b6aa828SChristian Schoenebeck * 750*6b6aa828SChristian Schoenebeck * This function is required if variable length suffixes are used for inode 751*6b6aa828SChristian Schoenebeck * number mapping on guest level. Since a device may end up having multiple 752*6b6aa828SChristian Schoenebeck * entries in qpp_table, each entry most probably with a different suffix 753*6b6aa828SChristian Schoenebeck * length, we thus need this function in conjunction with qpd_table to 754*6b6aa828SChristian Schoenebeck * "agree" about a fix amount of bits (per device) to be always used for 755*6b6aa828SChristian Schoenebeck * generating hash values for the purpose of accessing qpp_table in order 756*6b6aa828SChristian Schoenebeck * get consistent behaviour when accessing qpp_table. 757*6b6aa828SChristian Schoenebeck */ 758*6b6aa828SChristian Schoenebeck static int qid_inode_prefix_hash_bits(V9fsPDU *pdu, dev_t dev) 759*6b6aa828SChristian Schoenebeck { 760*6b6aa828SChristian Schoenebeck QpdEntry lookup = { 761*6b6aa828SChristian Schoenebeck .dev = dev 762*6b6aa828SChristian Schoenebeck }, *val; 763*6b6aa828SChristian Schoenebeck uint32_t hash = dev; 764*6b6aa828SChristian Schoenebeck VariLenAffix affix; 765*6b6aa828SChristian Schoenebeck 766*6b6aa828SChristian Schoenebeck val = qht_lookup(&pdu->s->qpd_table, &lookup, hash); 767*6b6aa828SChristian Schoenebeck if (!val) { 768*6b6aa828SChristian Schoenebeck val = g_malloc0(sizeof(QpdEntry)); 769*6b6aa828SChristian Schoenebeck *val = lookup; 770*6b6aa828SChristian Schoenebeck affix = affixForIndex(pdu->s->qp_affix_next); 771*6b6aa828SChristian Schoenebeck val->prefix_bits = affix.bits; 772*6b6aa828SChristian Schoenebeck qht_insert(&pdu->s->qpd_table, val, hash, NULL); 773*6b6aa828SChristian Schoenebeck pdu->s->qp_ndevices++; 774*6b6aa828SChristian Schoenebeck } 775*6b6aa828SChristian Schoenebeck return val->prefix_bits; 776*6b6aa828SChristian Schoenebeck } 777*6b6aa828SChristian Schoenebeck 778*6b6aa828SChristian Schoenebeck /** 779*6b6aa828SChristian Schoenebeck * @brief Slow / full mapping host inode nr -> guest inode nr. 780*6b6aa828SChristian Schoenebeck * 781*6b6aa828SChristian Schoenebeck * This function performs a slower and much more costly remapping of an 782*6b6aa828SChristian Schoenebeck * original file inode number on host to an appropriate different inode 783*6b6aa828SChristian Schoenebeck * number on guest. For every (dev, inode) combination on host a new 784*6b6aa828SChristian Schoenebeck * sequential number is generated, cached and exposed as inode number on 785*6b6aa828SChristian Schoenebeck * guest. 786*6b6aa828SChristian Schoenebeck * 787*6b6aa828SChristian Schoenebeck * This is just a "last resort" fallback solution if the much faster/cheaper 788*6b6aa828SChristian Schoenebeck * qid_path_suffixmap() failed. In practice this slow / full mapping is not 789*6b6aa828SChristian Schoenebeck * expected ever to be used at all though. 790*6b6aa828SChristian Schoenebeck * 791*6b6aa828SChristian Schoenebeck * @see qid_path_suffixmap() for details 792*6b6aa828SChristian Schoenebeck * 793*6b6aa828SChristian Schoenebeck */ 794f3fe4a2dSAntonios Motakis static int qid_path_fullmap(V9fsPDU *pdu, const struct stat *stbuf, 795f3fe4a2dSAntonios Motakis uint64_t *path) 796f3fe4a2dSAntonios Motakis { 797f3fe4a2dSAntonios Motakis QpfEntry lookup = { 798f3fe4a2dSAntonios Motakis .dev = stbuf->st_dev, 799f3fe4a2dSAntonios Motakis .ino = stbuf->st_ino 800f3fe4a2dSAntonios Motakis }, *val; 801f3fe4a2dSAntonios Motakis uint32_t hash = qpf_hash(lookup); 802*6b6aa828SChristian Schoenebeck VariLenAffix affix; 803f3fe4a2dSAntonios Motakis 804f3fe4a2dSAntonios Motakis val = qht_lookup(&pdu->s->qpf_table, &lookup, hash); 805f3fe4a2dSAntonios Motakis 806f3fe4a2dSAntonios Motakis if (!val) { 807f3fe4a2dSAntonios Motakis if (pdu->s->qp_fullpath_next == 0) { 808f3fe4a2dSAntonios Motakis /* no more files can be mapped :'( */ 809f3fe4a2dSAntonios Motakis error_report_once( 810f3fe4a2dSAntonios Motakis "9p: No more prefixes available for remapping inodes from " 811f3fe4a2dSAntonios Motakis "host to guest." 812f3fe4a2dSAntonios Motakis ); 813f3fe4a2dSAntonios Motakis return -ENFILE; 814f3fe4a2dSAntonios Motakis } 815f3fe4a2dSAntonios Motakis 816f3fe4a2dSAntonios Motakis val = g_malloc0(sizeof(QppEntry)); 817f3fe4a2dSAntonios Motakis *val = lookup; 818f3fe4a2dSAntonios Motakis 819f3fe4a2dSAntonios Motakis /* new unique inode and device combo */ 820*6b6aa828SChristian Schoenebeck affix = affixForIndex( 821*6b6aa828SChristian Schoenebeck 1ULL << (sizeof(pdu->s->qp_affix_next) * 8) 822*6b6aa828SChristian Schoenebeck ); 823*6b6aa828SChristian Schoenebeck val->path = (pdu->s->qp_fullpath_next++ << affix.bits) | affix.value; 824*6b6aa828SChristian Schoenebeck pdu->s->qp_fullpath_next &= ((1ULL << (64 - affix.bits)) - 1); 825f3fe4a2dSAntonios Motakis qht_insert(&pdu->s->qpf_table, val, hash, NULL); 826f3fe4a2dSAntonios Motakis } 827f3fe4a2dSAntonios Motakis 828f3fe4a2dSAntonios Motakis *path = val->path; 829f3fe4a2dSAntonios Motakis return 0; 830f3fe4a2dSAntonios Motakis } 831f3fe4a2dSAntonios Motakis 832*6b6aa828SChristian Schoenebeck /** 833*6b6aa828SChristian Schoenebeck * @brief Quick mapping host inode nr -> guest inode nr. 8341a6ed33cSAntonios Motakis * 835*6b6aa828SChristian Schoenebeck * This function performs quick remapping of an original file inode number 836*6b6aa828SChristian Schoenebeck * on host to an appropriate different inode number on guest. This remapping 837*6b6aa828SChristian Schoenebeck * of inodes is required to avoid inode nr collisions on guest which would 838*6b6aa828SChristian Schoenebeck * happen if the 9p export contains more than 1 exported file system (or 839*6b6aa828SChristian Schoenebeck * more than 1 file system data set), because unlike on host level where the 840*6b6aa828SChristian Schoenebeck * files would have different device nrs, all files exported by 9p would 841*6b6aa828SChristian Schoenebeck * share the same device nr on guest (the device nr of the virtual 9p device 842*6b6aa828SChristian Schoenebeck * that is). 843*6b6aa828SChristian Schoenebeck * 844*6b6aa828SChristian Schoenebeck * Inode remapping is performed by chopping off high end bits of the original 845*6b6aa828SChristian Schoenebeck * inode number from host, shifting the result upwards and then assigning a 846*6b6aa828SChristian Schoenebeck * generated suffix number for the low end bits, where the same suffix number 847*6b6aa828SChristian Schoenebeck * will be shared by all inodes with the same device id AND the same high end 848*6b6aa828SChristian Schoenebeck * bits that have been chopped off. That approach utilizes the fact that inode 849*6b6aa828SChristian Schoenebeck * numbers very likely share the same high end bits (i.e. due to their common 850*6b6aa828SChristian Schoenebeck * sequential generation by file systems) and hence we only have to generate 851*6b6aa828SChristian Schoenebeck * and track a very limited amount of suffixes in practice due to that. 852*6b6aa828SChristian Schoenebeck * 853*6b6aa828SChristian Schoenebeck * We generate variable size suffixes for that purpose. The 1st generated 854*6b6aa828SChristian Schoenebeck * suffix will only have 1 bit and hence we only need to chop off 1 bit from 855*6b6aa828SChristian Schoenebeck * the original inode number. The subsequent suffixes being generated will 856*6b6aa828SChristian Schoenebeck * grow in (bit) size subsequently, i.e. the 2nd and 3rd suffix being 857*6b6aa828SChristian Schoenebeck * generated will have 3 bits and hence we have to chop off 3 bits from their 858*6b6aa828SChristian Schoenebeck * original inodes, and so on. That approach of using variable length suffixes 859*6b6aa828SChristian Schoenebeck * (i.e. over fixed size ones) utilizes the fact that in practice only a very 860*6b6aa828SChristian Schoenebeck * limited amount of devices are shared by the same export (e.g. typically 861*6b6aa828SChristian Schoenebeck * less than 2 dozen devices per 9p export), so in practice we need to chop 862*6b6aa828SChristian Schoenebeck * off less bits than with fixed size prefixes and yet are flexible to add 863*6b6aa828SChristian Schoenebeck * new devices at runtime below host's export directory at any time without 864*6b6aa828SChristian Schoenebeck * having to reboot guest nor requiring to reconfigure guest for that. And due 865*6b6aa828SChristian Schoenebeck * to the very limited amount of original high end bits that we chop off that 866*6b6aa828SChristian Schoenebeck * way, the total amount of suffixes we need to generate is less than by using 867*6b6aa828SChristian Schoenebeck * fixed size prefixes and hence it also improves performance of the inode 868*6b6aa828SChristian Schoenebeck * remapping algorithm, and finally has the nice side effect that the inode 869*6b6aa828SChristian Schoenebeck * numbers on guest will be much smaller & human friendly. ;-) 8701a6ed33cSAntonios Motakis */ 871*6b6aa828SChristian Schoenebeck static int qid_path_suffixmap(V9fsPDU *pdu, const struct stat *stbuf, 8721a6ed33cSAntonios Motakis uint64_t *path) 8731a6ed33cSAntonios Motakis { 874*6b6aa828SChristian Schoenebeck const int ino_hash_bits = qid_inode_prefix_hash_bits(pdu, stbuf->st_dev); 8751a6ed33cSAntonios Motakis QppEntry lookup = { 8761a6ed33cSAntonios Motakis .dev = stbuf->st_dev, 877*6b6aa828SChristian Schoenebeck .ino_prefix = (uint16_t) (stbuf->st_ino >> (64 - ino_hash_bits)) 8781a6ed33cSAntonios Motakis }, *val; 8791a6ed33cSAntonios Motakis uint32_t hash = qpp_hash(lookup); 8801a6ed33cSAntonios Motakis 8811a6ed33cSAntonios Motakis val = qht_lookup(&pdu->s->qpp_table, &lookup, hash); 8821a6ed33cSAntonios Motakis 8831a6ed33cSAntonios Motakis if (!val) { 884*6b6aa828SChristian Schoenebeck if (pdu->s->qp_affix_next == 0) { 885*6b6aa828SChristian Schoenebeck /* we ran out of affixes */ 886f3fe4a2dSAntonios Motakis warn_report_once( 887f3fe4a2dSAntonios Motakis "9p: Potential degraded performance of inode remapping" 8881a6ed33cSAntonios Motakis ); 8891a6ed33cSAntonios Motakis return -ENFILE; 8901a6ed33cSAntonios Motakis } 8911a6ed33cSAntonios Motakis 8921a6ed33cSAntonios Motakis val = g_malloc0(sizeof(QppEntry)); 8931a6ed33cSAntonios Motakis *val = lookup; 8941a6ed33cSAntonios Motakis 895*6b6aa828SChristian Schoenebeck /* new unique inode affix and device combo */ 896*6b6aa828SChristian Schoenebeck val->qp_affix_index = pdu->s->qp_affix_next++; 897*6b6aa828SChristian Schoenebeck val->qp_affix = affixForIndex(val->qp_affix_index); 8981a6ed33cSAntonios Motakis qht_insert(&pdu->s->qpp_table, val, hash, NULL); 8991a6ed33cSAntonios Motakis } 900*6b6aa828SChristian Schoenebeck /* assuming generated affix to be suffix type, not prefix */ 901*6b6aa828SChristian Schoenebeck *path = (stbuf->st_ino << val->qp_affix.bits) | val->qp_affix.value; 9021a6ed33cSAntonios Motakis return 0; 9031a6ed33cSAntonios Motakis } 9041a6ed33cSAntonios Motakis 9053b5ee9e8SAntonios Motakis static int stat_to_qid(V9fsPDU *pdu, const struct stat *stbuf, V9fsQID *qidp) 90660ce86c7SWei Liu { 9071a6ed33cSAntonios Motakis int err; 90860ce86c7SWei Liu size_t size; 90960ce86c7SWei Liu 9101a6ed33cSAntonios Motakis if (pdu->s->ctx.export_flags & V9FS_REMAP_INODES) { 9111a6ed33cSAntonios Motakis /* map inode+device to qid path (fast path) */ 912*6b6aa828SChristian Schoenebeck err = qid_path_suffixmap(pdu, stbuf, &qidp->path); 913f3fe4a2dSAntonios Motakis if (err == -ENFILE) { 914f3fe4a2dSAntonios Motakis /* fast path didn't work, fall back to full map */ 915f3fe4a2dSAntonios Motakis err = qid_path_fullmap(pdu, stbuf, &qidp->path); 916f3fe4a2dSAntonios Motakis } 9171a6ed33cSAntonios Motakis if (err) { 9181a6ed33cSAntonios Motakis return err; 9191a6ed33cSAntonios Motakis } 9201a6ed33cSAntonios Motakis } else { 9213b5ee9e8SAntonios Motakis if (pdu->s->dev_id != stbuf->st_dev) { 9221a6ed33cSAntonios Motakis if (pdu->s->ctx.export_flags & V9FS_FORBID_MULTIDEVS) { 9231a6ed33cSAntonios Motakis error_report_once( 9241a6ed33cSAntonios Motakis "9p: Multiple devices detected in same VirtFS export. " 9251a6ed33cSAntonios Motakis "Access of guest to additional devices is (partly) " 9261a6ed33cSAntonios Motakis "denied due to virtfs option 'multidevs=forbid' being " 9271a6ed33cSAntonios Motakis "effective." 9281a6ed33cSAntonios Motakis ); 9291a6ed33cSAntonios Motakis return -ENODEV; 9301a6ed33cSAntonios Motakis } else { 9313b5ee9e8SAntonios Motakis warn_report_once( 9323b5ee9e8SAntonios Motakis "9p: Multiple devices detected in same VirtFS export, " 9333b5ee9e8SAntonios Motakis "which might lead to file ID collisions and severe " 9341a6ed33cSAntonios Motakis "misbehaviours on guest! You should either use a " 9351a6ed33cSAntonios Motakis "separate export for each device shared from host or " 9361a6ed33cSAntonios Motakis "use virtfs option 'multidevs=remap'!" 9373b5ee9e8SAntonios Motakis ); 9383b5ee9e8SAntonios Motakis } 9391a6ed33cSAntonios Motakis } 94060ce86c7SWei Liu memset(&qidp->path, 0, sizeof(qidp->path)); 94160ce86c7SWei Liu size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path)); 94260ce86c7SWei Liu memcpy(&qidp->path, &stbuf->st_ino, size); 9431a6ed33cSAntonios Motakis } 9441a6ed33cSAntonios Motakis 94560ce86c7SWei Liu qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8); 94660ce86c7SWei Liu qidp->type = 0; 94760ce86c7SWei Liu if (S_ISDIR(stbuf->st_mode)) { 94860ce86c7SWei Liu qidp->type |= P9_QID_TYPE_DIR; 94960ce86c7SWei Liu } 95060ce86c7SWei Liu if (S_ISLNK(stbuf->st_mode)) { 95160ce86c7SWei Liu qidp->type |= P9_QID_TYPE_SYMLINK; 95260ce86c7SWei Liu } 9533b5ee9e8SAntonios Motakis 9543b5ee9e8SAntonios Motakis return 0; 95560ce86c7SWei Liu } 95660ce86c7SWei Liu 9578440e22eSGreg Kurz static int coroutine_fn fid_to_qid(V9fsPDU *pdu, V9fsFidState *fidp, 9588440e22eSGreg Kurz V9fsQID *qidp) 95960ce86c7SWei Liu { 96060ce86c7SWei Liu struct stat stbuf; 96160ce86c7SWei Liu int err; 96260ce86c7SWei Liu 96360ce86c7SWei Liu err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); 96460ce86c7SWei Liu if (err < 0) { 96560ce86c7SWei Liu return err; 96660ce86c7SWei Liu } 9673b5ee9e8SAntonios Motakis err = stat_to_qid(pdu, &stbuf, qidp); 9683b5ee9e8SAntonios Motakis if (err < 0) { 9693b5ee9e8SAntonios Motakis return err; 9703b5ee9e8SAntonios Motakis } 97160ce86c7SWei Liu return 0; 97260ce86c7SWei Liu } 97360ce86c7SWei Liu 9741a6ed33cSAntonios Motakis static int coroutine_fn dirent_to_qid(V9fsPDU *pdu, V9fsFidState *fidp, 9751a6ed33cSAntonios Motakis struct dirent *dent, V9fsQID *qidp) 9761a6ed33cSAntonios Motakis { 9771a6ed33cSAntonios Motakis struct stat stbuf; 9781a6ed33cSAntonios Motakis V9fsPath path; 9791a6ed33cSAntonios Motakis int err; 9801a6ed33cSAntonios Motakis 9811a6ed33cSAntonios Motakis v9fs_path_init(&path); 9821a6ed33cSAntonios Motakis 9831a6ed33cSAntonios Motakis err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path); 9841a6ed33cSAntonios Motakis if (err < 0) { 9851a6ed33cSAntonios Motakis goto out; 9861a6ed33cSAntonios Motakis } 9871a6ed33cSAntonios Motakis err = v9fs_co_lstat(pdu, &path, &stbuf); 9881a6ed33cSAntonios Motakis if (err < 0) { 9891a6ed33cSAntonios Motakis goto out; 9901a6ed33cSAntonios Motakis } 9911a6ed33cSAntonios Motakis err = stat_to_qid(pdu, &stbuf, qidp); 9921a6ed33cSAntonios Motakis 9931a6ed33cSAntonios Motakis out: 9941a6ed33cSAntonios Motakis v9fs_path_free(&path); 9951a6ed33cSAntonios Motakis return err; 9961a6ed33cSAntonios Motakis } 9971a6ed33cSAntonios Motakis 99860ce86c7SWei Liu V9fsPDU *pdu_alloc(V9fsState *s) 99960ce86c7SWei Liu { 100060ce86c7SWei Liu V9fsPDU *pdu = NULL; 100160ce86c7SWei Liu 100260ce86c7SWei Liu if (!QLIST_EMPTY(&s->free_list)) { 100360ce86c7SWei Liu pdu = QLIST_FIRST(&s->free_list); 100460ce86c7SWei Liu QLIST_REMOVE(pdu, next); 100560ce86c7SWei Liu QLIST_INSERT_HEAD(&s->active_list, pdu, next); 100660ce86c7SWei Liu } 100760ce86c7SWei Liu return pdu; 100860ce86c7SWei Liu } 100960ce86c7SWei Liu 101060ce86c7SWei Liu void pdu_free(V9fsPDU *pdu) 101160ce86c7SWei Liu { 101260ce86c7SWei Liu V9fsState *s = pdu->s; 1013f74e27bfSGreg Kurz 1014f74e27bfSGreg Kurz g_assert(!pdu->cancelled); 101560ce86c7SWei Liu QLIST_REMOVE(pdu, next); 101660ce86c7SWei Liu QLIST_INSERT_HEAD(&s->free_list, pdu, next); 101760ce86c7SWei Liu } 101860ce86c7SWei Liu 10198440e22eSGreg Kurz static void coroutine_fn pdu_complete(V9fsPDU *pdu, ssize_t len) 102060ce86c7SWei Liu { 102160ce86c7SWei Liu int8_t id = pdu->id + 1; /* Response */ 102260ce86c7SWei Liu V9fsState *s = pdu->s; 102306a37db7SGreg Kurz int ret; 102460ce86c7SWei Liu 1025fc78d5eeSKeno Fischer /* 1026fc78d5eeSKeno Fischer * The 9p spec requires that successfully cancelled pdus receive no reply. 1027fc78d5eeSKeno Fischer * Sending a reply would confuse clients because they would 1028fc78d5eeSKeno Fischer * assume that any EINTR is the actual result of the operation, 1029fc78d5eeSKeno Fischer * rather than a consequence of the cancellation. However, if 1030fc78d5eeSKeno Fischer * the operation completed (succesfully or with an error other 1031fc78d5eeSKeno Fischer * than caused be cancellation), we do send out that reply, both 1032fc78d5eeSKeno Fischer * for efficiency and to avoid confusing the rest of the state machine 1033fc78d5eeSKeno Fischer * that assumes passing a non-error here will mean a successful 1034fc78d5eeSKeno Fischer * transmission of the reply. 1035fc78d5eeSKeno Fischer */ 1036fc78d5eeSKeno Fischer bool discard = pdu->cancelled && len == -EINTR; 1037fc78d5eeSKeno Fischer if (discard) { 1038fc78d5eeSKeno Fischer trace_v9fs_rcancel(pdu->tag, pdu->id); 1039fc78d5eeSKeno Fischer pdu->size = 0; 1040fc78d5eeSKeno Fischer goto out_notify; 1041fc78d5eeSKeno Fischer } 1042fc78d5eeSKeno Fischer 104360ce86c7SWei Liu if (len < 0) { 104460ce86c7SWei Liu int err = -len; 104560ce86c7SWei Liu len = 7; 104660ce86c7SWei Liu 104760ce86c7SWei Liu if (s->proto_version != V9FS_PROTO_2000L) { 104860ce86c7SWei Liu V9fsString str; 104960ce86c7SWei Liu 105060ce86c7SWei Liu str.data = strerror(err); 105160ce86c7SWei Liu str.size = strlen(str.data); 105260ce86c7SWei Liu 105306a37db7SGreg Kurz ret = pdu_marshal(pdu, len, "s", &str); 105406a37db7SGreg Kurz if (ret < 0) { 105506a37db7SGreg Kurz goto out_notify; 105606a37db7SGreg Kurz } 105706a37db7SGreg Kurz len += ret; 105860ce86c7SWei Liu id = P9_RERROR; 105960ce86c7SWei Liu } 106060ce86c7SWei Liu 106106a37db7SGreg Kurz ret = pdu_marshal(pdu, len, "d", err); 106206a37db7SGreg Kurz if (ret < 0) { 106306a37db7SGreg Kurz goto out_notify; 106406a37db7SGreg Kurz } 106506a37db7SGreg Kurz len += ret; 106660ce86c7SWei Liu 106760ce86c7SWei Liu if (s->proto_version == V9FS_PROTO_2000L) { 106860ce86c7SWei Liu id = P9_RLERROR; 106960ce86c7SWei Liu } 107060ce86c7SWei Liu trace_v9fs_rerror(pdu->tag, pdu->id, err); /* Trace ERROR */ 107160ce86c7SWei Liu } 107260ce86c7SWei Liu 107360ce86c7SWei Liu /* fill out the header */ 107406a37db7SGreg Kurz if (pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag) < 0) { 107506a37db7SGreg Kurz goto out_notify; 107606a37db7SGreg Kurz } 107760ce86c7SWei Liu 107860ce86c7SWei Liu /* keep these in sync */ 107960ce86c7SWei Liu pdu->size = len; 108060ce86c7SWei Liu pdu->id = id; 108160ce86c7SWei Liu 108206a37db7SGreg Kurz out_notify: 1083a17d8659SGreg Kurz pdu->s->transport->push_and_notify(pdu); 108460ce86c7SWei Liu 108560ce86c7SWei Liu /* Now wakeup anybody waiting in flush for this request */ 1086f74e27bfSGreg Kurz if (!qemu_co_queue_next(&pdu->complete)) { 108760ce86c7SWei Liu pdu_free(pdu); 108860ce86c7SWei Liu } 1089f74e27bfSGreg Kurz } 109060ce86c7SWei Liu 109160ce86c7SWei Liu static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension) 109260ce86c7SWei Liu { 109360ce86c7SWei Liu mode_t ret; 109460ce86c7SWei Liu 109560ce86c7SWei Liu ret = mode & 0777; 109660ce86c7SWei Liu if (mode & P9_STAT_MODE_DIR) { 109760ce86c7SWei Liu ret |= S_IFDIR; 109860ce86c7SWei Liu } 109960ce86c7SWei Liu 110060ce86c7SWei Liu if (mode & P9_STAT_MODE_SYMLINK) { 110160ce86c7SWei Liu ret |= S_IFLNK; 110260ce86c7SWei Liu } 110360ce86c7SWei Liu if (mode & P9_STAT_MODE_SOCKET) { 110460ce86c7SWei Liu ret |= S_IFSOCK; 110560ce86c7SWei Liu } 110660ce86c7SWei Liu if (mode & P9_STAT_MODE_NAMED_PIPE) { 110760ce86c7SWei Liu ret |= S_IFIFO; 110860ce86c7SWei Liu } 110960ce86c7SWei Liu if (mode & P9_STAT_MODE_DEVICE) { 111060ce86c7SWei Liu if (extension->size && extension->data[0] == 'c') { 111160ce86c7SWei Liu ret |= S_IFCHR; 111260ce86c7SWei Liu } else { 111360ce86c7SWei Liu ret |= S_IFBLK; 111460ce86c7SWei Liu } 111560ce86c7SWei Liu } 111660ce86c7SWei Liu 111760ce86c7SWei Liu if (!(ret&~0777)) { 111860ce86c7SWei Liu ret |= S_IFREG; 111960ce86c7SWei Liu } 112060ce86c7SWei Liu 112160ce86c7SWei Liu if (mode & P9_STAT_MODE_SETUID) { 112260ce86c7SWei Liu ret |= S_ISUID; 112360ce86c7SWei Liu } 112460ce86c7SWei Liu if (mode & P9_STAT_MODE_SETGID) { 112560ce86c7SWei Liu ret |= S_ISGID; 112660ce86c7SWei Liu } 112760ce86c7SWei Liu if (mode & P9_STAT_MODE_SETVTX) { 112860ce86c7SWei Liu ret |= S_ISVTX; 112960ce86c7SWei Liu } 113060ce86c7SWei Liu 113160ce86c7SWei Liu return ret; 113260ce86c7SWei Liu } 113360ce86c7SWei Liu 113460ce86c7SWei Liu static int donttouch_stat(V9fsStat *stat) 113560ce86c7SWei Liu { 113660ce86c7SWei Liu if (stat->type == -1 && 113760ce86c7SWei Liu stat->dev == -1 && 113887032833SAntonios Motakis stat->qid.type == 0xff && 113987032833SAntonios Motakis stat->qid.version == (uint32_t) -1 && 114087032833SAntonios Motakis stat->qid.path == (uint64_t) -1 && 114160ce86c7SWei Liu stat->mode == -1 && 114260ce86c7SWei Liu stat->atime == -1 && 114360ce86c7SWei Liu stat->mtime == -1 && 114460ce86c7SWei Liu stat->length == -1 && 114560ce86c7SWei Liu !stat->name.size && 114660ce86c7SWei Liu !stat->uid.size && 114760ce86c7SWei Liu !stat->gid.size && 114860ce86c7SWei Liu !stat->muid.size && 114960ce86c7SWei Liu stat->n_uid == -1 && 115060ce86c7SWei Liu stat->n_gid == -1 && 115160ce86c7SWei Liu stat->n_muid == -1) { 115260ce86c7SWei Liu return 1; 115360ce86c7SWei Liu } 115460ce86c7SWei Liu 115560ce86c7SWei Liu return 0; 115660ce86c7SWei Liu } 115760ce86c7SWei Liu 115860ce86c7SWei Liu static void v9fs_stat_init(V9fsStat *stat) 115960ce86c7SWei Liu { 116060ce86c7SWei Liu v9fs_string_init(&stat->name); 116160ce86c7SWei Liu v9fs_string_init(&stat->uid); 116260ce86c7SWei Liu v9fs_string_init(&stat->gid); 116360ce86c7SWei Liu v9fs_string_init(&stat->muid); 116460ce86c7SWei Liu v9fs_string_init(&stat->extension); 116560ce86c7SWei Liu } 116660ce86c7SWei Liu 116760ce86c7SWei Liu static void v9fs_stat_free(V9fsStat *stat) 116860ce86c7SWei Liu { 116960ce86c7SWei Liu v9fs_string_free(&stat->name); 117060ce86c7SWei Liu v9fs_string_free(&stat->uid); 117160ce86c7SWei Liu v9fs_string_free(&stat->gid); 117260ce86c7SWei Liu v9fs_string_free(&stat->muid); 117360ce86c7SWei Liu v9fs_string_free(&stat->extension); 117460ce86c7SWei Liu } 117560ce86c7SWei Liu 117660ce86c7SWei Liu static uint32_t stat_to_v9mode(const struct stat *stbuf) 117760ce86c7SWei Liu { 117860ce86c7SWei Liu uint32_t mode; 117960ce86c7SWei Liu 118060ce86c7SWei Liu mode = stbuf->st_mode & 0777; 118160ce86c7SWei Liu if (S_ISDIR(stbuf->st_mode)) { 118260ce86c7SWei Liu mode |= P9_STAT_MODE_DIR; 118360ce86c7SWei Liu } 118460ce86c7SWei Liu 118560ce86c7SWei Liu if (S_ISLNK(stbuf->st_mode)) { 118660ce86c7SWei Liu mode |= P9_STAT_MODE_SYMLINK; 118760ce86c7SWei Liu } 118860ce86c7SWei Liu 118960ce86c7SWei Liu if (S_ISSOCK(stbuf->st_mode)) { 119060ce86c7SWei Liu mode |= P9_STAT_MODE_SOCKET; 119160ce86c7SWei Liu } 119260ce86c7SWei Liu 119360ce86c7SWei Liu if (S_ISFIFO(stbuf->st_mode)) { 119460ce86c7SWei Liu mode |= P9_STAT_MODE_NAMED_PIPE; 119560ce86c7SWei Liu } 119660ce86c7SWei Liu 119760ce86c7SWei Liu if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) { 119860ce86c7SWei Liu mode |= P9_STAT_MODE_DEVICE; 119960ce86c7SWei Liu } 120060ce86c7SWei Liu 120160ce86c7SWei Liu if (stbuf->st_mode & S_ISUID) { 120260ce86c7SWei Liu mode |= P9_STAT_MODE_SETUID; 120360ce86c7SWei Liu } 120460ce86c7SWei Liu 120560ce86c7SWei Liu if (stbuf->st_mode & S_ISGID) { 120660ce86c7SWei Liu mode |= P9_STAT_MODE_SETGID; 120760ce86c7SWei Liu } 120860ce86c7SWei Liu 120960ce86c7SWei Liu if (stbuf->st_mode & S_ISVTX) { 121060ce86c7SWei Liu mode |= P9_STAT_MODE_SETVTX; 121160ce86c7SWei Liu } 121260ce86c7SWei Liu 121360ce86c7SWei Liu return mode; 121460ce86c7SWei Liu } 121560ce86c7SWei Liu 12166069537fSJan Dakinevich static int coroutine_fn stat_to_v9stat(V9fsPDU *pdu, V9fsPath *path, 12176069537fSJan Dakinevich const char *basename, 121860ce86c7SWei Liu const struct stat *stbuf, 121960ce86c7SWei Liu V9fsStat *v9stat) 122060ce86c7SWei Liu { 122160ce86c7SWei Liu int err; 122260ce86c7SWei Liu 122360ce86c7SWei Liu memset(v9stat, 0, sizeof(*v9stat)); 122460ce86c7SWei Liu 12253b5ee9e8SAntonios Motakis err = stat_to_qid(pdu, stbuf, &v9stat->qid); 12263b5ee9e8SAntonios Motakis if (err < 0) { 12273b5ee9e8SAntonios Motakis return err; 12283b5ee9e8SAntonios Motakis } 122960ce86c7SWei Liu v9stat->mode = stat_to_v9mode(stbuf); 123060ce86c7SWei Liu v9stat->atime = stbuf->st_atime; 123160ce86c7SWei Liu v9stat->mtime = stbuf->st_mtime; 123260ce86c7SWei Liu v9stat->length = stbuf->st_size; 123360ce86c7SWei Liu 1234abdf0086SGreg Kurz v9fs_string_free(&v9stat->uid); 1235abdf0086SGreg Kurz v9fs_string_free(&v9stat->gid); 1236abdf0086SGreg Kurz v9fs_string_free(&v9stat->muid); 123760ce86c7SWei Liu 123860ce86c7SWei Liu v9stat->n_uid = stbuf->st_uid; 123960ce86c7SWei Liu v9stat->n_gid = stbuf->st_gid; 124060ce86c7SWei Liu v9stat->n_muid = 0; 124160ce86c7SWei Liu 1242abdf0086SGreg Kurz v9fs_string_free(&v9stat->extension); 124360ce86c7SWei Liu 124460ce86c7SWei Liu if (v9stat->mode & P9_STAT_MODE_SYMLINK) { 12456069537fSJan Dakinevich err = v9fs_co_readlink(pdu, path, &v9stat->extension); 124660ce86c7SWei Liu if (err < 0) { 124760ce86c7SWei Liu return err; 124860ce86c7SWei Liu } 124960ce86c7SWei Liu } else if (v9stat->mode & P9_STAT_MODE_DEVICE) { 125060ce86c7SWei Liu v9fs_string_sprintf(&v9stat->extension, "%c %u %u", 125160ce86c7SWei Liu S_ISCHR(stbuf->st_mode) ? 'c' : 'b', 125260ce86c7SWei Liu major(stbuf->st_rdev), minor(stbuf->st_rdev)); 125360ce86c7SWei Liu } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) { 125460ce86c7SWei Liu v9fs_string_sprintf(&v9stat->extension, "%s %lu", 125560ce86c7SWei Liu "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink); 125660ce86c7SWei Liu } 125760ce86c7SWei Liu 12586069537fSJan Dakinevich v9fs_string_sprintf(&v9stat->name, "%s", basename); 125960ce86c7SWei Liu 126060ce86c7SWei Liu v9stat->size = 61 + 126160ce86c7SWei Liu v9fs_string_size(&v9stat->name) + 126260ce86c7SWei Liu v9fs_string_size(&v9stat->uid) + 126360ce86c7SWei Liu v9fs_string_size(&v9stat->gid) + 126460ce86c7SWei Liu v9fs_string_size(&v9stat->muid) + 126560ce86c7SWei Liu v9fs_string_size(&v9stat->extension); 126660ce86c7SWei Liu return 0; 126760ce86c7SWei Liu } 126860ce86c7SWei Liu 126960ce86c7SWei Liu #define P9_STATS_MODE 0x00000001ULL 127060ce86c7SWei Liu #define P9_STATS_NLINK 0x00000002ULL 127160ce86c7SWei Liu #define P9_STATS_UID 0x00000004ULL 127260ce86c7SWei Liu #define P9_STATS_GID 0x00000008ULL 127360ce86c7SWei Liu #define P9_STATS_RDEV 0x00000010ULL 127460ce86c7SWei Liu #define P9_STATS_ATIME 0x00000020ULL 127560ce86c7SWei Liu #define P9_STATS_MTIME 0x00000040ULL 127660ce86c7SWei Liu #define P9_STATS_CTIME 0x00000080ULL 127760ce86c7SWei Liu #define P9_STATS_INO 0x00000100ULL 127860ce86c7SWei Liu #define P9_STATS_SIZE 0x00000200ULL 127960ce86c7SWei Liu #define P9_STATS_BLOCKS 0x00000400ULL 128060ce86c7SWei Liu 128160ce86c7SWei Liu #define P9_STATS_BTIME 0x00000800ULL 128260ce86c7SWei Liu #define P9_STATS_GEN 0x00001000ULL 128360ce86c7SWei Liu #define P9_STATS_DATA_VERSION 0x00002000ULL 128460ce86c7SWei Liu 128560ce86c7SWei Liu #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */ 128660ce86c7SWei Liu #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */ 128760ce86c7SWei Liu 128860ce86c7SWei Liu 12893b5ee9e8SAntonios Motakis static int stat_to_v9stat_dotl(V9fsPDU *pdu, const struct stat *stbuf, 129060ce86c7SWei Liu V9fsStatDotl *v9lstat) 129160ce86c7SWei Liu { 129260ce86c7SWei Liu memset(v9lstat, 0, sizeof(*v9lstat)); 129360ce86c7SWei Liu 129460ce86c7SWei Liu v9lstat->st_mode = stbuf->st_mode; 129560ce86c7SWei Liu v9lstat->st_nlink = stbuf->st_nlink; 129660ce86c7SWei Liu v9lstat->st_uid = stbuf->st_uid; 129760ce86c7SWei Liu v9lstat->st_gid = stbuf->st_gid; 129860ce86c7SWei Liu v9lstat->st_rdev = stbuf->st_rdev; 129960ce86c7SWei Liu v9lstat->st_size = stbuf->st_size; 130060ce86c7SWei Liu v9lstat->st_blksize = stbuf->st_blksize; 130160ce86c7SWei Liu v9lstat->st_blocks = stbuf->st_blocks; 130260ce86c7SWei Liu v9lstat->st_atime_sec = stbuf->st_atime; 130360ce86c7SWei Liu v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec; 130460ce86c7SWei Liu v9lstat->st_mtime_sec = stbuf->st_mtime; 130560ce86c7SWei Liu v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec; 130660ce86c7SWei Liu v9lstat->st_ctime_sec = stbuf->st_ctime; 130760ce86c7SWei Liu v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec; 130860ce86c7SWei Liu /* Currently we only support BASIC fields in stat */ 130960ce86c7SWei Liu v9lstat->st_result_mask = P9_STATS_BASIC; 131060ce86c7SWei Liu 13113b5ee9e8SAntonios Motakis return stat_to_qid(pdu, stbuf, &v9lstat->qid); 131260ce86c7SWei Liu } 131360ce86c7SWei Liu 131460ce86c7SWei Liu static void print_sg(struct iovec *sg, int cnt) 131560ce86c7SWei Liu { 131660ce86c7SWei Liu int i; 131760ce86c7SWei Liu 131860ce86c7SWei Liu printf("sg[%d]: {", cnt); 131960ce86c7SWei Liu for (i = 0; i < cnt; i++) { 132060ce86c7SWei Liu if (i) { 132160ce86c7SWei Liu printf(", "); 132260ce86c7SWei Liu } 132360ce86c7SWei Liu printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len); 132460ce86c7SWei Liu } 132560ce86c7SWei Liu printf("}\n"); 132660ce86c7SWei Liu } 132760ce86c7SWei Liu 132860ce86c7SWei Liu /* Will call this only for path name based fid */ 132960ce86c7SWei Liu static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len) 133060ce86c7SWei Liu { 133160ce86c7SWei Liu V9fsPath str; 133260ce86c7SWei Liu v9fs_path_init(&str); 133360ce86c7SWei Liu v9fs_path_copy(&str, dst); 1334e3e83f2eSGreg Kurz v9fs_path_sprintf(dst, "%s%s", src->data, str.data + len); 133560ce86c7SWei Liu v9fs_path_free(&str); 133660ce86c7SWei Liu } 133760ce86c7SWei Liu 133860ce86c7SWei Liu static inline bool is_ro_export(FsContext *ctx) 133960ce86c7SWei Liu { 134060ce86c7SWei Liu return ctx->export_flags & V9FS_RDONLY; 134160ce86c7SWei Liu } 134260ce86c7SWei Liu 13438440e22eSGreg Kurz static void coroutine_fn v9fs_version(void *opaque) 134460ce86c7SWei Liu { 134560ce86c7SWei Liu ssize_t err; 134660ce86c7SWei Liu V9fsPDU *pdu = opaque; 134760ce86c7SWei Liu V9fsState *s = pdu->s; 134860ce86c7SWei Liu V9fsString version; 134960ce86c7SWei Liu size_t offset = 7; 135060ce86c7SWei Liu 135160ce86c7SWei Liu v9fs_string_init(&version); 135260ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "ds", &s->msize, &version); 135360ce86c7SWei Liu if (err < 0) { 135460ce86c7SWei Liu goto out; 135560ce86c7SWei Liu } 135660ce86c7SWei Liu trace_v9fs_version(pdu->tag, pdu->id, s->msize, version.data); 135760ce86c7SWei Liu 135860ce86c7SWei Liu virtfs_reset(pdu); 135960ce86c7SWei Liu 136060ce86c7SWei Liu if (!strcmp(version.data, "9P2000.u")) { 136160ce86c7SWei Liu s->proto_version = V9FS_PROTO_2000U; 136260ce86c7SWei Liu } else if (!strcmp(version.data, "9P2000.L")) { 136360ce86c7SWei Liu s->proto_version = V9FS_PROTO_2000L; 136460ce86c7SWei Liu } else { 136560ce86c7SWei Liu v9fs_string_sprintf(&version, "unknown"); 136660ce86c7SWei Liu } 136760ce86c7SWei Liu 136860ce86c7SWei Liu err = pdu_marshal(pdu, offset, "ds", s->msize, &version); 136960ce86c7SWei Liu if (err < 0) { 137060ce86c7SWei Liu goto out; 137160ce86c7SWei Liu } 1372403a905bSPhilippe Mathieu-Daudé err += offset; 137360ce86c7SWei Liu trace_v9fs_version_return(pdu->tag, pdu->id, s->msize, version.data); 137460ce86c7SWei Liu out: 1375403a905bSPhilippe Mathieu-Daudé pdu_complete(pdu, err); 137660ce86c7SWei Liu v9fs_string_free(&version); 137760ce86c7SWei Liu } 137860ce86c7SWei Liu 13798440e22eSGreg Kurz static void coroutine_fn v9fs_attach(void *opaque) 138060ce86c7SWei Liu { 138160ce86c7SWei Liu V9fsPDU *pdu = opaque; 138260ce86c7SWei Liu V9fsState *s = pdu->s; 138360ce86c7SWei Liu int32_t fid, afid, n_uname; 138460ce86c7SWei Liu V9fsString uname, aname; 138560ce86c7SWei Liu V9fsFidState *fidp; 138660ce86c7SWei Liu size_t offset = 7; 138760ce86c7SWei Liu V9fsQID qid; 138860ce86c7SWei Liu ssize_t err; 1389fe44dc91SAshijeet Acharya Error *local_err = NULL; 139060ce86c7SWei Liu 139160ce86c7SWei Liu v9fs_string_init(&uname); 139260ce86c7SWei Liu v9fs_string_init(&aname); 139360ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "ddssd", &fid, 139460ce86c7SWei Liu &afid, &uname, &aname, &n_uname); 139560ce86c7SWei Liu if (err < 0) { 139660ce86c7SWei Liu goto out_nofid; 139760ce86c7SWei Liu } 139860ce86c7SWei Liu trace_v9fs_attach(pdu->tag, pdu->id, fid, afid, uname.data, aname.data); 139960ce86c7SWei Liu 140060ce86c7SWei Liu fidp = alloc_fid(s, fid); 140160ce86c7SWei Liu if (fidp == NULL) { 140260ce86c7SWei Liu err = -EINVAL; 140360ce86c7SWei Liu goto out_nofid; 140460ce86c7SWei Liu } 140560ce86c7SWei Liu fidp->uid = n_uname; 140660ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, NULL, "/", &fidp->path); 140760ce86c7SWei Liu if (err < 0) { 140860ce86c7SWei Liu err = -EINVAL; 140960ce86c7SWei Liu clunk_fid(s, fid); 141060ce86c7SWei Liu goto out; 141160ce86c7SWei Liu } 141260ce86c7SWei Liu err = fid_to_qid(pdu, fidp, &qid); 141360ce86c7SWei Liu if (err < 0) { 141460ce86c7SWei Liu err = -EINVAL; 141560ce86c7SWei Liu clunk_fid(s, fid); 141660ce86c7SWei Liu goto out; 141760ce86c7SWei Liu } 1418fe44dc91SAshijeet Acharya 1419fe44dc91SAshijeet Acharya /* 1420fe44dc91SAshijeet Acharya * disable migration if we haven't done already. 1421fe44dc91SAshijeet Acharya * attach could get called multiple times for the same export. 1422fe44dc91SAshijeet Acharya */ 1423fe44dc91SAshijeet Acharya if (!s->migration_blocker) { 1424fe44dc91SAshijeet Acharya error_setg(&s->migration_blocker, 1425fe44dc91SAshijeet Acharya "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'", 1426fe44dc91SAshijeet Acharya s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag); 1427fe44dc91SAshijeet Acharya err = migrate_add_blocker(s->migration_blocker, &local_err); 1428fe44dc91SAshijeet Acharya if (local_err) { 1429fe44dc91SAshijeet Acharya error_free(local_err); 1430fe44dc91SAshijeet Acharya error_free(s->migration_blocker); 1431fe44dc91SAshijeet Acharya s->migration_blocker = NULL; 1432fe44dc91SAshijeet Acharya clunk_fid(s, fid); 1433fe44dc91SAshijeet Acharya goto out; 1434fe44dc91SAshijeet Acharya } 1435fe44dc91SAshijeet Acharya s->root_fid = fid; 1436fe44dc91SAshijeet Acharya } 1437fe44dc91SAshijeet Acharya 143860ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Q", &qid); 143960ce86c7SWei Liu if (err < 0) { 144060ce86c7SWei Liu clunk_fid(s, fid); 144160ce86c7SWei Liu goto out; 144260ce86c7SWei Liu } 144360ce86c7SWei Liu err += offset; 1444fe44dc91SAshijeet Acharya 144556f101ecSGreg Kurz memcpy(&s->root_qid, &qid, sizeof(qid)); 144660ce86c7SWei Liu trace_v9fs_attach_return(pdu->tag, pdu->id, 144760ce86c7SWei Liu qid.type, qid.version, qid.path); 144860ce86c7SWei Liu out: 144960ce86c7SWei Liu put_fid(pdu, fidp); 145060ce86c7SWei Liu out_nofid: 145160ce86c7SWei Liu pdu_complete(pdu, err); 145260ce86c7SWei Liu v9fs_string_free(&uname); 145360ce86c7SWei Liu v9fs_string_free(&aname); 145460ce86c7SWei Liu } 145560ce86c7SWei Liu 14568440e22eSGreg Kurz static void coroutine_fn v9fs_stat(void *opaque) 145760ce86c7SWei Liu { 145860ce86c7SWei Liu int32_t fid; 145960ce86c7SWei Liu V9fsStat v9stat; 146060ce86c7SWei Liu ssize_t err = 0; 146160ce86c7SWei Liu size_t offset = 7; 146260ce86c7SWei Liu struct stat stbuf; 146360ce86c7SWei Liu V9fsFidState *fidp; 146460ce86c7SWei Liu V9fsPDU *pdu = opaque; 14656069537fSJan Dakinevich char *basename; 146660ce86c7SWei Liu 146760ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "d", &fid); 146860ce86c7SWei Liu if (err < 0) { 146960ce86c7SWei Liu goto out_nofid; 147060ce86c7SWei Liu } 147160ce86c7SWei Liu trace_v9fs_stat(pdu->tag, pdu->id, fid); 147260ce86c7SWei Liu 147360ce86c7SWei Liu fidp = get_fid(pdu, fid); 147460ce86c7SWei Liu if (fidp == NULL) { 147560ce86c7SWei Liu err = -ENOENT; 147660ce86c7SWei Liu goto out_nofid; 147760ce86c7SWei Liu } 147860ce86c7SWei Liu err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); 147960ce86c7SWei Liu if (err < 0) { 148060ce86c7SWei Liu goto out; 148160ce86c7SWei Liu } 14826069537fSJan Dakinevich basename = g_path_get_basename(fidp->path.data); 14836069537fSJan Dakinevich err = stat_to_v9stat(pdu, &fidp->path, basename, &stbuf, &v9stat); 14846069537fSJan Dakinevich g_free(basename); 148560ce86c7SWei Liu if (err < 0) { 148660ce86c7SWei Liu goto out; 148760ce86c7SWei Liu } 148860ce86c7SWei Liu err = pdu_marshal(pdu, offset, "wS", 0, &v9stat); 148960ce86c7SWei Liu if (err < 0) { 149060ce86c7SWei Liu v9fs_stat_free(&v9stat); 149160ce86c7SWei Liu goto out; 149260ce86c7SWei Liu } 149360ce86c7SWei Liu trace_v9fs_stat_return(pdu->tag, pdu->id, v9stat.mode, 149460ce86c7SWei Liu v9stat.atime, v9stat.mtime, v9stat.length); 149560ce86c7SWei Liu err += offset; 149660ce86c7SWei Liu v9fs_stat_free(&v9stat); 149760ce86c7SWei Liu out: 149860ce86c7SWei Liu put_fid(pdu, fidp); 149960ce86c7SWei Liu out_nofid: 150060ce86c7SWei Liu pdu_complete(pdu, err); 150160ce86c7SWei Liu } 150260ce86c7SWei Liu 15038440e22eSGreg Kurz static void coroutine_fn v9fs_getattr(void *opaque) 150460ce86c7SWei Liu { 150560ce86c7SWei Liu int32_t fid; 150660ce86c7SWei Liu size_t offset = 7; 150760ce86c7SWei Liu ssize_t retval = 0; 150860ce86c7SWei Liu struct stat stbuf; 150960ce86c7SWei Liu V9fsFidState *fidp; 151060ce86c7SWei Liu uint64_t request_mask; 151160ce86c7SWei Liu V9fsStatDotl v9stat_dotl; 151260ce86c7SWei Liu V9fsPDU *pdu = opaque; 151360ce86c7SWei Liu 151460ce86c7SWei Liu retval = pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask); 151560ce86c7SWei Liu if (retval < 0) { 151660ce86c7SWei Liu goto out_nofid; 151760ce86c7SWei Liu } 151860ce86c7SWei Liu trace_v9fs_getattr(pdu->tag, pdu->id, fid, request_mask); 151960ce86c7SWei Liu 152060ce86c7SWei Liu fidp = get_fid(pdu, fid); 152160ce86c7SWei Liu if (fidp == NULL) { 152260ce86c7SWei Liu retval = -ENOENT; 152360ce86c7SWei Liu goto out_nofid; 152460ce86c7SWei Liu } 152560ce86c7SWei Liu /* 152660ce86c7SWei Liu * Currently we only support BASIC fields in stat, so there is no 152760ce86c7SWei Liu * need to look at request_mask. 152860ce86c7SWei Liu */ 152960ce86c7SWei Liu retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf); 153060ce86c7SWei Liu if (retval < 0) { 153160ce86c7SWei Liu goto out; 153260ce86c7SWei Liu } 15333b5ee9e8SAntonios Motakis retval = stat_to_v9stat_dotl(pdu, &stbuf, &v9stat_dotl); 15343b5ee9e8SAntonios Motakis if (retval < 0) { 15353b5ee9e8SAntonios Motakis goto out; 15363b5ee9e8SAntonios Motakis } 153760ce86c7SWei Liu 153860ce86c7SWei Liu /* fill st_gen if requested and supported by underlying fs */ 153960ce86c7SWei Liu if (request_mask & P9_STATS_GEN) { 154060ce86c7SWei Liu retval = v9fs_co_st_gen(pdu, &fidp->path, stbuf.st_mode, &v9stat_dotl); 154160ce86c7SWei Liu switch (retval) { 154260ce86c7SWei Liu case 0: 154360ce86c7SWei Liu /* we have valid st_gen: update result mask */ 154460ce86c7SWei Liu v9stat_dotl.st_result_mask |= P9_STATS_GEN; 154560ce86c7SWei Liu break; 154660ce86c7SWei Liu case -EINTR: 154760ce86c7SWei Liu /* request cancelled, e.g. by Tflush */ 154860ce86c7SWei Liu goto out; 154960ce86c7SWei Liu default: 155060ce86c7SWei Liu /* failed to get st_gen: not fatal, ignore */ 155160ce86c7SWei Liu break; 155260ce86c7SWei Liu } 155360ce86c7SWei Liu } 155460ce86c7SWei Liu retval = pdu_marshal(pdu, offset, "A", &v9stat_dotl); 155560ce86c7SWei Liu if (retval < 0) { 155660ce86c7SWei Liu goto out; 155760ce86c7SWei Liu } 155860ce86c7SWei Liu retval += offset; 155960ce86c7SWei Liu trace_v9fs_getattr_return(pdu->tag, pdu->id, v9stat_dotl.st_result_mask, 156060ce86c7SWei Liu v9stat_dotl.st_mode, v9stat_dotl.st_uid, 156160ce86c7SWei Liu v9stat_dotl.st_gid); 156260ce86c7SWei Liu out: 156360ce86c7SWei Liu put_fid(pdu, fidp); 156460ce86c7SWei Liu out_nofid: 156560ce86c7SWei Liu pdu_complete(pdu, retval); 156660ce86c7SWei Liu } 156760ce86c7SWei Liu 156860ce86c7SWei Liu /* Attribute flags */ 156960ce86c7SWei Liu #define P9_ATTR_MODE (1 << 0) 157060ce86c7SWei Liu #define P9_ATTR_UID (1 << 1) 157160ce86c7SWei Liu #define P9_ATTR_GID (1 << 2) 157260ce86c7SWei Liu #define P9_ATTR_SIZE (1 << 3) 157360ce86c7SWei Liu #define P9_ATTR_ATIME (1 << 4) 157460ce86c7SWei Liu #define P9_ATTR_MTIME (1 << 5) 157560ce86c7SWei Liu #define P9_ATTR_CTIME (1 << 6) 157660ce86c7SWei Liu #define P9_ATTR_ATIME_SET (1 << 7) 157760ce86c7SWei Liu #define P9_ATTR_MTIME_SET (1 << 8) 157860ce86c7SWei Liu 157960ce86c7SWei Liu #define P9_ATTR_MASK 127 158060ce86c7SWei Liu 15818440e22eSGreg Kurz static void coroutine_fn v9fs_setattr(void *opaque) 158260ce86c7SWei Liu { 158360ce86c7SWei Liu int err = 0; 158460ce86c7SWei Liu int32_t fid; 158560ce86c7SWei Liu V9fsFidState *fidp; 158660ce86c7SWei Liu size_t offset = 7; 158760ce86c7SWei Liu V9fsIattr v9iattr; 158860ce86c7SWei Liu V9fsPDU *pdu = opaque; 158960ce86c7SWei Liu 159060ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr); 159160ce86c7SWei Liu if (err < 0) { 159260ce86c7SWei Liu goto out_nofid; 159360ce86c7SWei Liu } 159460ce86c7SWei Liu 15958f9c64bfSGreg Kurz trace_v9fs_setattr(pdu->tag, pdu->id, fid, 15968f9c64bfSGreg Kurz v9iattr.valid, v9iattr.mode, v9iattr.uid, v9iattr.gid, 15978f9c64bfSGreg Kurz v9iattr.size, v9iattr.atime_sec, v9iattr.mtime_sec); 15988f9c64bfSGreg Kurz 159960ce86c7SWei Liu fidp = get_fid(pdu, fid); 160060ce86c7SWei Liu if (fidp == NULL) { 160160ce86c7SWei Liu err = -EINVAL; 160260ce86c7SWei Liu goto out_nofid; 160360ce86c7SWei Liu } 160460ce86c7SWei Liu if (v9iattr.valid & P9_ATTR_MODE) { 160560ce86c7SWei Liu err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode); 160660ce86c7SWei Liu if (err < 0) { 160760ce86c7SWei Liu goto out; 160860ce86c7SWei Liu } 160960ce86c7SWei Liu } 161060ce86c7SWei Liu if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) { 161160ce86c7SWei Liu struct timespec times[2]; 161260ce86c7SWei Liu if (v9iattr.valid & P9_ATTR_ATIME) { 161360ce86c7SWei Liu if (v9iattr.valid & P9_ATTR_ATIME_SET) { 161460ce86c7SWei Liu times[0].tv_sec = v9iattr.atime_sec; 161560ce86c7SWei Liu times[0].tv_nsec = v9iattr.atime_nsec; 161660ce86c7SWei Liu } else { 161760ce86c7SWei Liu times[0].tv_nsec = UTIME_NOW; 161860ce86c7SWei Liu } 161960ce86c7SWei Liu } else { 162060ce86c7SWei Liu times[0].tv_nsec = UTIME_OMIT; 162160ce86c7SWei Liu } 162260ce86c7SWei Liu if (v9iattr.valid & P9_ATTR_MTIME) { 162360ce86c7SWei Liu if (v9iattr.valid & P9_ATTR_MTIME_SET) { 162460ce86c7SWei Liu times[1].tv_sec = v9iattr.mtime_sec; 162560ce86c7SWei Liu times[1].tv_nsec = v9iattr.mtime_nsec; 162660ce86c7SWei Liu } else { 162760ce86c7SWei Liu times[1].tv_nsec = UTIME_NOW; 162860ce86c7SWei Liu } 162960ce86c7SWei Liu } else { 163060ce86c7SWei Liu times[1].tv_nsec = UTIME_OMIT; 163160ce86c7SWei Liu } 163260ce86c7SWei Liu err = v9fs_co_utimensat(pdu, &fidp->path, times); 163360ce86c7SWei Liu if (err < 0) { 163460ce86c7SWei Liu goto out; 163560ce86c7SWei Liu } 163660ce86c7SWei Liu } 163760ce86c7SWei Liu /* 163860ce86c7SWei Liu * If the only valid entry in iattr is ctime we can call 163960ce86c7SWei Liu * chown(-1,-1) to update the ctime of the file 164060ce86c7SWei Liu */ 164160ce86c7SWei Liu if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) || 164260ce86c7SWei Liu ((v9iattr.valid & P9_ATTR_CTIME) 164360ce86c7SWei Liu && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) { 164460ce86c7SWei Liu if (!(v9iattr.valid & P9_ATTR_UID)) { 164560ce86c7SWei Liu v9iattr.uid = -1; 164660ce86c7SWei Liu } 164760ce86c7SWei Liu if (!(v9iattr.valid & P9_ATTR_GID)) { 164860ce86c7SWei Liu v9iattr.gid = -1; 164960ce86c7SWei Liu } 165060ce86c7SWei Liu err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid, 165160ce86c7SWei Liu v9iattr.gid); 165260ce86c7SWei Liu if (err < 0) { 165360ce86c7SWei Liu goto out; 165460ce86c7SWei Liu } 165560ce86c7SWei Liu } 165660ce86c7SWei Liu if (v9iattr.valid & (P9_ATTR_SIZE)) { 165760ce86c7SWei Liu err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size); 165860ce86c7SWei Liu if (err < 0) { 165960ce86c7SWei Liu goto out; 166060ce86c7SWei Liu } 166160ce86c7SWei Liu } 166260ce86c7SWei Liu err = offset; 16638f9c64bfSGreg Kurz trace_v9fs_setattr_return(pdu->tag, pdu->id); 166460ce86c7SWei Liu out: 166560ce86c7SWei Liu put_fid(pdu, fidp); 166660ce86c7SWei Liu out_nofid: 166760ce86c7SWei Liu pdu_complete(pdu, err); 166860ce86c7SWei Liu } 166960ce86c7SWei Liu 167060ce86c7SWei Liu static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids) 167160ce86c7SWei Liu { 167260ce86c7SWei Liu int i; 167360ce86c7SWei Liu ssize_t err; 167460ce86c7SWei Liu size_t offset = 7; 167560ce86c7SWei Liu 167660ce86c7SWei Liu err = pdu_marshal(pdu, offset, "w", nwnames); 167760ce86c7SWei Liu if (err < 0) { 167860ce86c7SWei Liu return err; 167960ce86c7SWei Liu } 168060ce86c7SWei Liu offset += err; 168160ce86c7SWei Liu for (i = 0; i < nwnames; i++) { 168260ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Q", &qids[i]); 168360ce86c7SWei Liu if (err < 0) { 168460ce86c7SWei Liu return err; 168560ce86c7SWei Liu } 168660ce86c7SWei Liu offset += err; 168760ce86c7SWei Liu } 168860ce86c7SWei Liu return offset; 168960ce86c7SWei Liu } 169060ce86c7SWei Liu 1691fff39a7aSGreg Kurz static bool name_is_illegal(const char *name) 1692fff39a7aSGreg Kurz { 1693fff39a7aSGreg Kurz return !*name || strchr(name, '/') != NULL; 1694fff39a7aSGreg Kurz } 1695fff39a7aSGreg Kurz 169656f101ecSGreg Kurz static bool not_same_qid(const V9fsQID *qid1, const V9fsQID *qid2) 169756f101ecSGreg Kurz { 169856f101ecSGreg Kurz return 169956f101ecSGreg Kurz qid1->type != qid2->type || 170056f101ecSGreg Kurz qid1->version != qid2->version || 170156f101ecSGreg Kurz qid1->path != qid2->path; 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 173060ce86c7SWei Liu if (nwnames && nwnames <= P9_MAXWELEM) { 17311923923bSGreg Kurz wnames = g_new0(V9fsString, nwnames); 17321923923bSGreg Kurz qids = g_new0(V9fsQID, nwnames); 173360ce86c7SWei Liu for (i = 0; i < nwnames; i++) { 173460ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "s", &wnames[i]); 173560ce86c7SWei Liu if (err < 0) { 173660ce86c7SWei Liu goto out_nofid; 173760ce86c7SWei Liu } 1738fff39a7aSGreg Kurz if (name_is_illegal(wnames[i].data)) { 1739fff39a7aSGreg Kurz err = -ENOENT; 1740fff39a7aSGreg Kurz goto out_nofid; 1741fff39a7aSGreg Kurz } 174260ce86c7SWei Liu offset += err; 174360ce86c7SWei Liu } 174460ce86c7SWei Liu } else if (nwnames > P9_MAXWELEM) { 174560ce86c7SWei Liu err = -EINVAL; 174660ce86c7SWei Liu goto out_nofid; 174760ce86c7SWei Liu } 174860ce86c7SWei Liu fidp = get_fid(pdu, fid); 174960ce86c7SWei Liu if (fidp == NULL) { 175060ce86c7SWei Liu err = -ENOENT; 175160ce86c7SWei Liu goto out_nofid; 175260ce86c7SWei Liu } 175356f101ecSGreg Kurz 175413fd08e6SGreg Kurz v9fs_path_init(&dpath); 175513fd08e6SGreg Kurz v9fs_path_init(&path); 175613fd08e6SGreg Kurz 175756f101ecSGreg Kurz err = fid_to_qid(pdu, fidp, &qid); 175856f101ecSGreg Kurz if (err < 0) { 175956f101ecSGreg Kurz goto out; 176056f101ecSGreg Kurz } 176156f101ecSGreg Kurz 176260ce86c7SWei Liu /* 176360ce86c7SWei Liu * Both dpath and path initially poin to fidp. 176460ce86c7SWei Liu * Needed to handle request with nwnames == 0 176560ce86c7SWei Liu */ 176660ce86c7SWei Liu v9fs_path_copy(&dpath, &fidp->path); 176760ce86c7SWei Liu v9fs_path_copy(&path, &fidp->path); 176860ce86c7SWei Liu for (name_idx = 0; name_idx < nwnames; name_idx++) { 176956f101ecSGreg Kurz if (not_same_qid(&pdu->s->root_qid, &qid) || 177056f101ecSGreg Kurz strcmp("..", wnames[name_idx].data)) { 177156f101ecSGreg Kurz err = v9fs_co_name_to_path(pdu, &dpath, wnames[name_idx].data, 177256f101ecSGreg Kurz &path); 177360ce86c7SWei Liu if (err < 0) { 177460ce86c7SWei Liu goto out; 177560ce86c7SWei Liu } 177656f101ecSGreg Kurz 177760ce86c7SWei Liu err = v9fs_co_lstat(pdu, &path, &stbuf); 177860ce86c7SWei Liu if (err < 0) { 177960ce86c7SWei Liu goto out; 178060ce86c7SWei Liu } 17813b5ee9e8SAntonios Motakis err = stat_to_qid(pdu, &stbuf, &qid); 17823b5ee9e8SAntonios Motakis if (err < 0) { 17833b5ee9e8SAntonios Motakis goto out; 17843b5ee9e8SAntonios Motakis } 178560ce86c7SWei Liu v9fs_path_copy(&dpath, &path); 178660ce86c7SWei Liu } 178756f101ecSGreg Kurz memcpy(&qids[name_idx], &qid, sizeof(qid)); 178856f101ecSGreg Kurz } 178960ce86c7SWei Liu if (fid == newfid) { 179049dd946bSGreg Kurz if (fidp->fid_type != P9_FID_NONE) { 179149dd946bSGreg Kurz err = -EINVAL; 179249dd946bSGreg Kurz goto out; 179349dd946bSGreg Kurz } 17945b3c77aaSGreg Kurz v9fs_path_write_lock(s); 179560ce86c7SWei Liu v9fs_path_copy(&fidp->path, &path); 17965b3c77aaSGreg Kurz v9fs_path_unlock(s); 179760ce86c7SWei Liu } else { 179860ce86c7SWei Liu newfidp = alloc_fid(s, newfid); 179960ce86c7SWei Liu if (newfidp == NULL) { 180060ce86c7SWei Liu err = -EINVAL; 180160ce86c7SWei Liu goto out; 180260ce86c7SWei Liu } 180360ce86c7SWei Liu newfidp->uid = fidp->uid; 180460ce86c7SWei Liu v9fs_path_copy(&newfidp->path, &path); 180560ce86c7SWei Liu } 180660ce86c7SWei Liu err = v9fs_walk_marshal(pdu, nwnames, qids); 180760ce86c7SWei Liu trace_v9fs_walk_return(pdu->tag, pdu->id, nwnames, qids); 180860ce86c7SWei Liu out: 180960ce86c7SWei Liu put_fid(pdu, fidp); 181060ce86c7SWei Liu if (newfidp) { 181160ce86c7SWei Liu put_fid(pdu, newfidp); 181260ce86c7SWei Liu } 181360ce86c7SWei Liu v9fs_path_free(&dpath); 181460ce86c7SWei Liu v9fs_path_free(&path); 181560ce86c7SWei Liu out_nofid: 181660ce86c7SWei Liu pdu_complete(pdu, err); 181760ce86c7SWei Liu if (nwnames && nwnames <= P9_MAXWELEM) { 181860ce86c7SWei Liu for (name_idx = 0; name_idx < nwnames; name_idx++) { 181960ce86c7SWei Liu v9fs_string_free(&wnames[name_idx]); 182060ce86c7SWei Liu } 182160ce86c7SWei Liu g_free(wnames); 182260ce86c7SWei Liu g_free(qids); 182360ce86c7SWei Liu } 182460ce86c7SWei Liu } 182560ce86c7SWei Liu 18268440e22eSGreg Kurz static int32_t coroutine_fn get_iounit(V9fsPDU *pdu, V9fsPath *path) 182760ce86c7SWei Liu { 182860ce86c7SWei Liu struct statfs stbuf; 182960ce86c7SWei Liu int32_t iounit = 0; 183060ce86c7SWei Liu V9fsState *s = pdu->s; 183160ce86c7SWei Liu 183260ce86c7SWei Liu /* 183360ce86c7SWei Liu * iounit should be multiples of f_bsize (host filesystem block size 183460ce86c7SWei Liu * and as well as less than (client msize - P9_IOHDRSZ)) 183560ce86c7SWei Liu */ 183660ce86c7SWei Liu if (!v9fs_co_statfs(pdu, path, &stbuf)) { 183760ce86c7SWei Liu iounit = stbuf.f_bsize; 183860ce86c7SWei Liu iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize; 183960ce86c7SWei Liu } 184060ce86c7SWei Liu if (!iounit) { 184160ce86c7SWei Liu iounit = s->msize - P9_IOHDRSZ; 184260ce86c7SWei Liu } 184360ce86c7SWei Liu return iounit; 184460ce86c7SWei Liu } 184560ce86c7SWei Liu 18468440e22eSGreg Kurz static void coroutine_fn v9fs_open(void *opaque) 184760ce86c7SWei Liu { 184860ce86c7SWei Liu int flags; 184960ce86c7SWei Liu int32_t fid; 185060ce86c7SWei Liu int32_t mode; 185160ce86c7SWei Liu V9fsQID qid; 185260ce86c7SWei Liu int iounit = 0; 185360ce86c7SWei Liu ssize_t err = 0; 185460ce86c7SWei Liu size_t offset = 7; 185560ce86c7SWei Liu struct stat stbuf; 185660ce86c7SWei Liu V9fsFidState *fidp; 185760ce86c7SWei Liu V9fsPDU *pdu = opaque; 185860ce86c7SWei Liu V9fsState *s = pdu->s; 185960ce86c7SWei Liu 186060ce86c7SWei Liu if (s->proto_version == V9FS_PROTO_2000L) { 186160ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode); 186260ce86c7SWei Liu } else { 186360ce86c7SWei Liu uint8_t modebyte; 186460ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte); 186560ce86c7SWei Liu mode = modebyte; 186660ce86c7SWei Liu } 186760ce86c7SWei Liu if (err < 0) { 186860ce86c7SWei Liu goto out_nofid; 186960ce86c7SWei Liu } 187060ce86c7SWei Liu trace_v9fs_open(pdu->tag, pdu->id, fid, mode); 187160ce86c7SWei Liu 187260ce86c7SWei Liu fidp = get_fid(pdu, fid); 187360ce86c7SWei Liu if (fidp == NULL) { 187460ce86c7SWei Liu err = -ENOENT; 187560ce86c7SWei Liu goto out_nofid; 187660ce86c7SWei Liu } 187749dd946bSGreg Kurz if (fidp->fid_type != P9_FID_NONE) { 187849dd946bSGreg Kurz err = -EINVAL; 187949dd946bSGreg Kurz goto out; 188049dd946bSGreg Kurz } 188160ce86c7SWei Liu 188260ce86c7SWei Liu err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); 188360ce86c7SWei Liu if (err < 0) { 188460ce86c7SWei Liu goto out; 188560ce86c7SWei Liu } 18863b5ee9e8SAntonios Motakis err = stat_to_qid(pdu, &stbuf, &qid); 18873b5ee9e8SAntonios Motakis if (err < 0) { 18883b5ee9e8SAntonios Motakis goto out; 18893b5ee9e8SAntonios Motakis } 189060ce86c7SWei Liu if (S_ISDIR(stbuf.st_mode)) { 189160ce86c7SWei Liu err = v9fs_co_opendir(pdu, fidp); 189260ce86c7SWei Liu if (err < 0) { 189360ce86c7SWei Liu goto out; 189460ce86c7SWei Liu } 189560ce86c7SWei Liu fidp->fid_type = P9_FID_DIR; 189660ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Qd", &qid, 0); 189760ce86c7SWei Liu if (err < 0) { 189860ce86c7SWei Liu goto out; 189960ce86c7SWei Liu } 190060ce86c7SWei Liu err += offset; 190160ce86c7SWei Liu } else { 190260ce86c7SWei Liu if (s->proto_version == V9FS_PROTO_2000L) { 190360ce86c7SWei Liu flags = get_dotl_openflags(s, mode); 190460ce86c7SWei Liu } else { 190560ce86c7SWei Liu flags = omode_to_uflags(mode); 190660ce86c7SWei Liu } 190760ce86c7SWei Liu if (is_ro_export(&s->ctx)) { 190860ce86c7SWei Liu if (mode & O_WRONLY || mode & O_RDWR || 190960ce86c7SWei Liu mode & O_APPEND || mode & O_TRUNC) { 191060ce86c7SWei Liu err = -EROFS; 191160ce86c7SWei Liu goto out; 191260ce86c7SWei Liu } 191360ce86c7SWei Liu } 191460ce86c7SWei Liu err = v9fs_co_open(pdu, fidp, flags); 191560ce86c7SWei Liu if (err < 0) { 191660ce86c7SWei Liu goto out; 191760ce86c7SWei Liu } 191860ce86c7SWei Liu fidp->fid_type = P9_FID_FILE; 191960ce86c7SWei Liu fidp->open_flags = flags; 192060ce86c7SWei Liu if (flags & O_EXCL) { 192160ce86c7SWei Liu /* 192260ce86c7SWei Liu * We let the host file system do O_EXCL check 192360ce86c7SWei Liu * We should not reclaim such fd 192460ce86c7SWei Liu */ 192560ce86c7SWei Liu fidp->flags |= FID_NON_RECLAIMABLE; 192660ce86c7SWei Liu } 192760ce86c7SWei Liu iounit = get_iounit(pdu, &fidp->path); 192860ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Qd", &qid, iounit); 192960ce86c7SWei Liu if (err < 0) { 193060ce86c7SWei Liu goto out; 193160ce86c7SWei Liu } 193260ce86c7SWei Liu err += offset; 193360ce86c7SWei Liu } 193460ce86c7SWei Liu trace_v9fs_open_return(pdu->tag, pdu->id, 193560ce86c7SWei Liu qid.type, qid.version, qid.path, iounit); 193660ce86c7SWei Liu out: 193760ce86c7SWei Liu put_fid(pdu, fidp); 193860ce86c7SWei Liu out_nofid: 193960ce86c7SWei Liu pdu_complete(pdu, err); 194060ce86c7SWei Liu } 194160ce86c7SWei Liu 19428440e22eSGreg Kurz static void coroutine_fn v9fs_lcreate(void *opaque) 194360ce86c7SWei Liu { 194460ce86c7SWei Liu int32_t dfid, flags, mode; 194560ce86c7SWei Liu gid_t gid; 194660ce86c7SWei Liu ssize_t err = 0; 194760ce86c7SWei Liu ssize_t offset = 7; 194860ce86c7SWei Liu V9fsString name; 194960ce86c7SWei Liu V9fsFidState *fidp; 195060ce86c7SWei Liu struct stat stbuf; 195160ce86c7SWei Liu V9fsQID qid; 195260ce86c7SWei Liu int32_t iounit; 195360ce86c7SWei Liu V9fsPDU *pdu = opaque; 195460ce86c7SWei Liu 195560ce86c7SWei Liu v9fs_string_init(&name); 195660ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dsddd", &dfid, 195760ce86c7SWei Liu &name, &flags, &mode, &gid); 195860ce86c7SWei Liu if (err < 0) { 195960ce86c7SWei Liu goto out_nofid; 196060ce86c7SWei Liu } 196160ce86c7SWei Liu trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid); 196260ce86c7SWei Liu 1963fff39a7aSGreg Kurz if (name_is_illegal(name.data)) { 1964fff39a7aSGreg Kurz err = -ENOENT; 1965fff39a7aSGreg Kurz goto out_nofid; 1966fff39a7aSGreg Kurz } 1967fff39a7aSGreg Kurz 1968805b5d98SGreg Kurz if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 1969805b5d98SGreg Kurz err = -EEXIST; 1970805b5d98SGreg Kurz goto out_nofid; 1971805b5d98SGreg Kurz } 1972805b5d98SGreg Kurz 197360ce86c7SWei Liu fidp = get_fid(pdu, dfid); 197460ce86c7SWei Liu if (fidp == NULL) { 197560ce86c7SWei Liu err = -ENOENT; 197660ce86c7SWei Liu goto out_nofid; 197760ce86c7SWei Liu } 1978d63fb193SLi Qiang if (fidp->fid_type != P9_FID_NONE) { 1979d63fb193SLi Qiang err = -EINVAL; 1980d63fb193SLi Qiang goto out; 1981d63fb193SLi Qiang } 198260ce86c7SWei Liu 198360ce86c7SWei Liu flags = get_dotl_openflags(pdu->s, flags); 198460ce86c7SWei Liu err = v9fs_co_open2(pdu, fidp, &name, gid, 198560ce86c7SWei Liu flags | O_CREAT, mode, &stbuf); 198660ce86c7SWei Liu if (err < 0) { 198760ce86c7SWei Liu goto out; 198860ce86c7SWei Liu } 198960ce86c7SWei Liu fidp->fid_type = P9_FID_FILE; 199060ce86c7SWei Liu fidp->open_flags = flags; 199160ce86c7SWei Liu if (flags & O_EXCL) { 199260ce86c7SWei Liu /* 199360ce86c7SWei Liu * We let the host file system do O_EXCL check 199460ce86c7SWei Liu * We should not reclaim such fd 199560ce86c7SWei Liu */ 199660ce86c7SWei Liu fidp->flags |= FID_NON_RECLAIMABLE; 199760ce86c7SWei Liu } 199860ce86c7SWei Liu iounit = get_iounit(pdu, &fidp->path); 19993b5ee9e8SAntonios Motakis err = stat_to_qid(pdu, &stbuf, &qid); 20003b5ee9e8SAntonios Motakis if (err < 0) { 20013b5ee9e8SAntonios Motakis goto out; 20023b5ee9e8SAntonios Motakis } 200360ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Qd", &qid, iounit); 200460ce86c7SWei Liu if (err < 0) { 200560ce86c7SWei Liu goto out; 200660ce86c7SWei Liu } 200760ce86c7SWei Liu err += offset; 200860ce86c7SWei Liu trace_v9fs_lcreate_return(pdu->tag, pdu->id, 200960ce86c7SWei Liu qid.type, qid.version, qid.path, iounit); 201060ce86c7SWei Liu out: 201160ce86c7SWei Liu put_fid(pdu, fidp); 201260ce86c7SWei Liu out_nofid: 201360ce86c7SWei Liu pdu_complete(pdu, err); 201460ce86c7SWei Liu v9fs_string_free(&name); 201560ce86c7SWei Liu } 201660ce86c7SWei Liu 2017a1bf8b74SGreg Kurz static void coroutine_fn v9fs_fsync(void *opaque) 201860ce86c7SWei Liu { 201960ce86c7SWei Liu int err; 202060ce86c7SWei Liu int32_t fid; 202160ce86c7SWei Liu int datasync; 202260ce86c7SWei Liu size_t offset = 7; 202360ce86c7SWei Liu V9fsFidState *fidp; 202460ce86c7SWei Liu V9fsPDU *pdu = opaque; 202560ce86c7SWei Liu 202660ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync); 202760ce86c7SWei Liu if (err < 0) { 202860ce86c7SWei Liu goto out_nofid; 202960ce86c7SWei Liu } 203060ce86c7SWei Liu trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync); 203160ce86c7SWei Liu 203260ce86c7SWei Liu fidp = get_fid(pdu, fid); 203360ce86c7SWei Liu if (fidp == NULL) { 203460ce86c7SWei Liu err = -ENOENT; 203560ce86c7SWei Liu goto out_nofid; 203660ce86c7SWei Liu } 203760ce86c7SWei Liu err = v9fs_co_fsync(pdu, fidp, datasync); 203860ce86c7SWei Liu if (!err) { 203960ce86c7SWei Liu err = offset; 204060ce86c7SWei Liu } 204160ce86c7SWei Liu put_fid(pdu, fidp); 204260ce86c7SWei Liu out_nofid: 204360ce86c7SWei Liu pdu_complete(pdu, err); 204460ce86c7SWei Liu } 204560ce86c7SWei Liu 20468440e22eSGreg Kurz static void coroutine_fn v9fs_clunk(void *opaque) 204760ce86c7SWei Liu { 204860ce86c7SWei Liu int err; 204960ce86c7SWei Liu int32_t fid; 205060ce86c7SWei Liu size_t offset = 7; 205160ce86c7SWei Liu V9fsFidState *fidp; 205260ce86c7SWei Liu V9fsPDU *pdu = opaque; 205360ce86c7SWei Liu V9fsState *s = pdu->s; 205460ce86c7SWei Liu 205560ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "d", &fid); 205660ce86c7SWei Liu if (err < 0) { 205760ce86c7SWei Liu goto out_nofid; 205860ce86c7SWei Liu } 205960ce86c7SWei Liu trace_v9fs_clunk(pdu->tag, pdu->id, fid); 206060ce86c7SWei Liu 206160ce86c7SWei Liu fidp = clunk_fid(s, fid); 206260ce86c7SWei Liu if (fidp == NULL) { 206360ce86c7SWei Liu err = -ENOENT; 206460ce86c7SWei Liu goto out_nofid; 206560ce86c7SWei Liu } 206660ce86c7SWei Liu /* 206760ce86c7SWei Liu * Bump the ref so that put_fid will 206860ce86c7SWei Liu * free the fid. 206960ce86c7SWei Liu */ 207060ce86c7SWei Liu fidp->ref++; 207160ce86c7SWei Liu err = put_fid(pdu, fidp); 207260ce86c7SWei Liu if (!err) { 207360ce86c7SWei Liu err = offset; 207460ce86c7SWei Liu } 207560ce86c7SWei Liu out_nofid: 207660ce86c7SWei Liu pdu_complete(pdu, err); 207760ce86c7SWei Liu } 207860ce86c7SWei Liu 2079bcb8998fSStefano Stabellini /* 2080bcb8998fSStefano Stabellini * Create a QEMUIOVector for a sub-region of PDU iovecs 2081bcb8998fSStefano Stabellini * 2082bcb8998fSStefano Stabellini * @qiov: uninitialized QEMUIOVector 2083bcb8998fSStefano Stabellini * @skip: number of bytes to skip from beginning of PDU 2084bcb8998fSStefano Stabellini * @size: number of bytes to include 2085bcb8998fSStefano Stabellini * @is_write: true - write, false - read 2086bcb8998fSStefano Stabellini * 2087bcb8998fSStefano Stabellini * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up 2088bcb8998fSStefano Stabellini * with qemu_iovec_destroy(). 2089bcb8998fSStefano Stabellini */ 2090bcb8998fSStefano Stabellini static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu, 2091bcb8998fSStefano Stabellini size_t skip, size_t size, 2092bcb8998fSStefano Stabellini bool is_write) 2093bcb8998fSStefano Stabellini { 2094bcb8998fSStefano Stabellini QEMUIOVector elem; 2095bcb8998fSStefano Stabellini struct iovec *iov; 2096bcb8998fSStefano Stabellini unsigned int niov; 2097bcb8998fSStefano Stabellini 209888da0b03SStefano Stabellini if (is_write) { 20998d37de41SGreg Kurz pdu->s->transport->init_out_iov_from_pdu(pdu, &iov, &niov, size + skip); 210088da0b03SStefano Stabellini } else { 2101fa0eb5c5SGreg Kurz pdu->s->transport->init_in_iov_from_pdu(pdu, &iov, &niov, size + skip); 210288da0b03SStefano Stabellini } 2103bcb8998fSStefano Stabellini 2104bcb8998fSStefano Stabellini qemu_iovec_init_external(&elem, iov, niov); 2105bcb8998fSStefano Stabellini qemu_iovec_init(qiov, niov); 2106bcb8998fSStefano Stabellini qemu_iovec_concat(qiov, &elem, skip, size); 2107bcb8998fSStefano Stabellini } 2108bcb8998fSStefano Stabellini 210960ce86c7SWei Liu static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp, 211060ce86c7SWei Liu uint64_t off, uint32_t max_count) 211160ce86c7SWei Liu { 211260ce86c7SWei Liu ssize_t err; 211360ce86c7SWei Liu size_t offset = 7; 21147e55d65cSLi Qiang uint64_t read_count; 2115bcb8998fSStefano Stabellini QEMUIOVector qiov_full; 211660ce86c7SWei Liu 21177e55d65cSLi Qiang if (fidp->fs.xattr.len < off) { 21187e55d65cSLi Qiang read_count = 0; 21197e55d65cSLi Qiang } else { 21207e55d65cSLi Qiang read_count = fidp->fs.xattr.len - off; 21217e55d65cSLi Qiang } 212260ce86c7SWei Liu if (read_count > max_count) { 212360ce86c7SWei Liu read_count = max_count; 212460ce86c7SWei Liu } 212560ce86c7SWei Liu err = pdu_marshal(pdu, offset, "d", read_count); 212660ce86c7SWei Liu if (err < 0) { 212760ce86c7SWei Liu return err; 212860ce86c7SWei Liu } 212960ce86c7SWei Liu offset += err; 213000588a0aSWei Liu 2131fa0eb5c5SGreg Kurz v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, read_count, false); 2132fa0eb5c5SGreg Kurz err = v9fs_pack(qiov_full.iov, qiov_full.niov, 0, 213360ce86c7SWei Liu ((char *)fidp->fs.xattr.value) + off, 213460ce86c7SWei Liu read_count); 2135bcb8998fSStefano Stabellini qemu_iovec_destroy(&qiov_full); 213660ce86c7SWei Liu if (err < 0) { 213760ce86c7SWei Liu return err; 213860ce86c7SWei Liu } 213960ce86c7SWei Liu offset += err; 214060ce86c7SWei Liu return offset; 214160ce86c7SWei Liu } 214260ce86c7SWei Liu 21438440e22eSGreg Kurz static int coroutine_fn v9fs_do_readdir_with_stat(V9fsPDU *pdu, 21448440e22eSGreg Kurz V9fsFidState *fidp, 21458440e22eSGreg Kurz uint32_t max_count) 214660ce86c7SWei Liu { 214760ce86c7SWei Liu V9fsPath path; 214860ce86c7SWei Liu V9fsStat v9stat; 214960ce86c7SWei Liu int len, err = 0; 215060ce86c7SWei Liu int32_t count = 0; 215160ce86c7SWei Liu struct stat stbuf; 215260ce86c7SWei Liu off_t saved_dir_pos; 2153635324e8SGreg Kurz struct dirent *dent; 215460ce86c7SWei Liu 215560ce86c7SWei Liu /* save the directory position */ 215660ce86c7SWei Liu saved_dir_pos = v9fs_co_telldir(pdu, fidp); 215760ce86c7SWei Liu if (saved_dir_pos < 0) { 215860ce86c7SWei Liu return saved_dir_pos; 215960ce86c7SWei Liu } 216060ce86c7SWei Liu 216160ce86c7SWei Liu while (1) { 216260ce86c7SWei Liu v9fs_path_init(&path); 21637cde47d4SGreg Kurz 21647cde47d4SGreg Kurz v9fs_readdir_lock(&fidp->fs.dir); 21657cde47d4SGreg Kurz 2166635324e8SGreg Kurz err = v9fs_co_readdir(pdu, fidp, &dent); 2167635324e8SGreg Kurz if (err || !dent) { 216860ce86c7SWei Liu break; 216960ce86c7SWei Liu } 217060ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path); 217160ce86c7SWei Liu if (err < 0) { 21728762a46dSGreg Kurz break; 217360ce86c7SWei Liu } 217460ce86c7SWei Liu err = v9fs_co_lstat(pdu, &path, &stbuf); 217560ce86c7SWei Liu if (err < 0) { 21768762a46dSGreg Kurz break; 217760ce86c7SWei Liu } 21786069537fSJan Dakinevich err = stat_to_v9stat(pdu, &path, dent->d_name, &stbuf, &v9stat); 217960ce86c7SWei Liu if (err < 0) { 21808762a46dSGreg Kurz break; 218160ce86c7SWei Liu } 2182772a7369SJan Dakinevich if ((count + v9stat.size + 2) > max_count) { 21837cde47d4SGreg Kurz v9fs_readdir_unlock(&fidp->fs.dir); 21847cde47d4SGreg Kurz 218560ce86c7SWei Liu /* Ran out of buffer. Set dir back to old position and return */ 218660ce86c7SWei Liu v9fs_co_seekdir(pdu, fidp, saved_dir_pos); 218760ce86c7SWei Liu v9fs_stat_free(&v9stat); 218860ce86c7SWei Liu v9fs_path_free(&path); 218960ce86c7SWei Liu return count; 219060ce86c7SWei Liu } 2191772a7369SJan Dakinevich 2192772a7369SJan Dakinevich /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */ 2193772a7369SJan Dakinevich len = pdu_marshal(pdu, 11 + count, "S", &v9stat); 2194772a7369SJan Dakinevich 2195772a7369SJan Dakinevich v9fs_readdir_unlock(&fidp->fs.dir); 2196772a7369SJan Dakinevich 2197772a7369SJan Dakinevich if (len < 0) { 2198772a7369SJan Dakinevich v9fs_co_seekdir(pdu, fidp, saved_dir_pos); 2199772a7369SJan Dakinevich v9fs_stat_free(&v9stat); 2200772a7369SJan Dakinevich v9fs_path_free(&path); 2201772a7369SJan Dakinevich return len; 2202772a7369SJan Dakinevich } 220360ce86c7SWei Liu count += len; 220460ce86c7SWei Liu v9fs_stat_free(&v9stat); 220560ce86c7SWei Liu v9fs_path_free(&path); 220660ce86c7SWei Liu saved_dir_pos = dent->d_off; 220760ce86c7SWei Liu } 22088762a46dSGreg Kurz 22097cde47d4SGreg Kurz v9fs_readdir_unlock(&fidp->fs.dir); 22107cde47d4SGreg Kurz 221160ce86c7SWei Liu v9fs_path_free(&path); 221260ce86c7SWei Liu if (err < 0) { 221360ce86c7SWei Liu return err; 221460ce86c7SWei Liu } 221560ce86c7SWei Liu return count; 221660ce86c7SWei Liu } 221760ce86c7SWei Liu 22188440e22eSGreg Kurz static void coroutine_fn v9fs_read(void *opaque) 221960ce86c7SWei Liu { 222060ce86c7SWei Liu int32_t fid; 222160ce86c7SWei Liu uint64_t off; 222260ce86c7SWei Liu ssize_t err = 0; 222360ce86c7SWei Liu int32_t count = 0; 222460ce86c7SWei Liu size_t offset = 7; 222560ce86c7SWei Liu uint32_t max_count; 222660ce86c7SWei Liu V9fsFidState *fidp; 222760ce86c7SWei Liu V9fsPDU *pdu = opaque; 222860ce86c7SWei Liu V9fsState *s = pdu->s; 222960ce86c7SWei Liu 223060ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count); 223160ce86c7SWei Liu if (err < 0) { 223260ce86c7SWei Liu goto out_nofid; 223360ce86c7SWei Liu } 223460ce86c7SWei Liu trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count); 223560ce86c7SWei Liu 223660ce86c7SWei Liu fidp = get_fid(pdu, fid); 223760ce86c7SWei Liu if (fidp == NULL) { 223860ce86c7SWei Liu err = -EINVAL; 223960ce86c7SWei Liu goto out_nofid; 224060ce86c7SWei Liu } 224160ce86c7SWei Liu if (fidp->fid_type == P9_FID_DIR) { 224260ce86c7SWei Liu 224360ce86c7SWei Liu if (off == 0) { 224460ce86c7SWei Liu v9fs_co_rewinddir(pdu, fidp); 224560ce86c7SWei Liu } 224660ce86c7SWei Liu count = v9fs_do_readdir_with_stat(pdu, fidp, max_count); 224760ce86c7SWei Liu if (count < 0) { 224860ce86c7SWei Liu err = count; 224960ce86c7SWei Liu goto out; 225060ce86c7SWei Liu } 225160ce86c7SWei Liu err = pdu_marshal(pdu, offset, "d", count); 225260ce86c7SWei Liu if (err < 0) { 225360ce86c7SWei Liu goto out; 225460ce86c7SWei Liu } 225560ce86c7SWei Liu err += offset + count; 225660ce86c7SWei Liu } else if (fidp->fid_type == P9_FID_FILE) { 225760ce86c7SWei Liu QEMUIOVector qiov_full; 225860ce86c7SWei Liu QEMUIOVector qiov; 225960ce86c7SWei Liu int32_t len; 226060ce86c7SWei Liu 226160ce86c7SWei Liu v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, max_count, false); 226260ce86c7SWei Liu qemu_iovec_init(&qiov, qiov_full.niov); 226360ce86c7SWei Liu do { 226460ce86c7SWei Liu qemu_iovec_reset(&qiov); 226560ce86c7SWei Liu qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count); 226660ce86c7SWei Liu if (0) { 226760ce86c7SWei Liu print_sg(qiov.iov, qiov.niov); 226860ce86c7SWei Liu } 226960ce86c7SWei Liu /* Loop in case of EINTR */ 227060ce86c7SWei Liu do { 227160ce86c7SWei Liu len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off); 227260ce86c7SWei Liu if (len >= 0) { 227360ce86c7SWei Liu off += len; 227460ce86c7SWei Liu count += len; 227560ce86c7SWei Liu } 227660ce86c7SWei Liu } while (len == -EINTR && !pdu->cancelled); 227760ce86c7SWei Liu if (len < 0) { 227860ce86c7SWei Liu /* IO error return the error */ 227960ce86c7SWei Liu err = len; 2280e95c9a49SLi Qiang goto out_free_iovec; 228160ce86c7SWei Liu } 228260ce86c7SWei Liu } while (count < max_count && len > 0); 228360ce86c7SWei Liu err = pdu_marshal(pdu, offset, "d", count); 228460ce86c7SWei Liu if (err < 0) { 2285e95c9a49SLi Qiang goto out_free_iovec; 228660ce86c7SWei Liu } 228760ce86c7SWei Liu err += offset + count; 2288e95c9a49SLi Qiang out_free_iovec: 228960ce86c7SWei Liu qemu_iovec_destroy(&qiov); 229060ce86c7SWei Liu qemu_iovec_destroy(&qiov_full); 229160ce86c7SWei Liu } else if (fidp->fid_type == P9_FID_XATTR) { 229260ce86c7SWei Liu err = v9fs_xattr_read(s, pdu, fidp, off, max_count); 229360ce86c7SWei Liu } else { 229460ce86c7SWei Liu err = -EINVAL; 229560ce86c7SWei Liu } 229660ce86c7SWei Liu trace_v9fs_read_return(pdu->tag, pdu->id, count, err); 229760ce86c7SWei Liu out: 229860ce86c7SWei Liu put_fid(pdu, fidp); 229960ce86c7SWei Liu out_nofid: 230060ce86c7SWei Liu pdu_complete(pdu, err); 230160ce86c7SWei Liu } 230260ce86c7SWei Liu 230360ce86c7SWei Liu static size_t v9fs_readdir_data_size(V9fsString *name) 230460ce86c7SWei Liu { 230560ce86c7SWei Liu /* 230660ce86c7SWei Liu * Size of each dirent on the wire: size of qid (13) + size of offset (8) 230760ce86c7SWei Liu * size of type (1) + size of name.size (2) + strlen(name.data) 230860ce86c7SWei Liu */ 230960ce86c7SWei Liu return 24 + v9fs_string_size(name); 231060ce86c7SWei Liu } 231160ce86c7SWei Liu 23128440e22eSGreg Kurz static int coroutine_fn v9fs_do_readdir(V9fsPDU *pdu, V9fsFidState *fidp, 23138440e22eSGreg Kurz int32_t max_count) 231460ce86c7SWei Liu { 231560ce86c7SWei Liu size_t size; 231660ce86c7SWei Liu V9fsQID qid; 231760ce86c7SWei Liu V9fsString name; 231860ce86c7SWei Liu int len, err = 0; 231960ce86c7SWei Liu int32_t count = 0; 232060ce86c7SWei Liu off_t saved_dir_pos; 2321635324e8SGreg Kurz struct dirent *dent; 232260ce86c7SWei Liu 232360ce86c7SWei Liu /* save the directory position */ 232460ce86c7SWei Liu saved_dir_pos = v9fs_co_telldir(pdu, fidp); 232560ce86c7SWei Liu if (saved_dir_pos < 0) { 232660ce86c7SWei Liu return saved_dir_pos; 232760ce86c7SWei Liu } 232860ce86c7SWei Liu 232960ce86c7SWei Liu while (1) { 23307cde47d4SGreg Kurz v9fs_readdir_lock(&fidp->fs.dir); 23317cde47d4SGreg Kurz 2332635324e8SGreg Kurz err = v9fs_co_readdir(pdu, fidp, &dent); 2333635324e8SGreg Kurz if (err || !dent) { 233460ce86c7SWei Liu break; 233560ce86c7SWei Liu } 233660ce86c7SWei Liu v9fs_string_init(&name); 233760ce86c7SWei Liu v9fs_string_sprintf(&name, "%s", dent->d_name); 233860ce86c7SWei Liu if ((count + v9fs_readdir_data_size(&name)) > max_count) { 23397cde47d4SGreg Kurz v9fs_readdir_unlock(&fidp->fs.dir); 23407cde47d4SGreg Kurz 234160ce86c7SWei Liu /* Ran out of buffer. Set dir back to old position and return */ 234260ce86c7SWei Liu v9fs_co_seekdir(pdu, fidp, saved_dir_pos); 234360ce86c7SWei Liu v9fs_string_free(&name); 234460ce86c7SWei Liu return count; 234560ce86c7SWei Liu } 23461a6ed33cSAntonios Motakis 23471a6ed33cSAntonios Motakis if (pdu->s->ctx.export_flags & V9FS_REMAP_INODES) { 23481a6ed33cSAntonios Motakis /* 23491a6ed33cSAntonios Motakis * dirent_to_qid() implies expensive stat call for each entry, 23501a6ed33cSAntonios Motakis * we must do that here though since inode remapping requires 23511a6ed33cSAntonios Motakis * the device id, which in turn might be different for 23521a6ed33cSAntonios Motakis * different entries; we cannot make any assumption to avoid 23531a6ed33cSAntonios Motakis * that here. 23541a6ed33cSAntonios Motakis */ 23551a6ed33cSAntonios Motakis err = dirent_to_qid(pdu, fidp, dent, &qid); 23561a6ed33cSAntonios Motakis if (err < 0) { 23571a6ed33cSAntonios Motakis v9fs_readdir_unlock(&fidp->fs.dir); 23581a6ed33cSAntonios Motakis v9fs_co_seekdir(pdu, fidp, saved_dir_pos); 23591a6ed33cSAntonios Motakis v9fs_string_free(&name); 23601a6ed33cSAntonios Motakis return err; 23611a6ed33cSAntonios Motakis } 23621a6ed33cSAntonios Motakis } else { 236360ce86c7SWei Liu /* 236460ce86c7SWei Liu * Fill up just the path field of qid because the client uses 236560ce86c7SWei Liu * only that. To fill the entire qid structure we will have 23661a6ed33cSAntonios Motakis * to stat each dirent found, which is expensive. For the 23671a6ed33cSAntonios Motakis * latter reason we don't call dirent_to_qid() here. Only drawback 23681a6ed33cSAntonios Motakis * is that no multi-device export detection of stat_to_qid() 23691a6ed33cSAntonios Motakis * would be done and provided as error to the user here. But 23701a6ed33cSAntonios Motakis * user would get that error anyway when accessing those 23711a6ed33cSAntonios Motakis * files/dirs through other ways. 237260ce86c7SWei Liu */ 237360ce86c7SWei Liu size = MIN(sizeof(dent->d_ino), sizeof(qid.path)); 237460ce86c7SWei Liu memcpy(&qid.path, &dent->d_ino, size); 237560ce86c7SWei Liu /* Fill the other fields with dummy values */ 237660ce86c7SWei Liu qid.type = 0; 237760ce86c7SWei Liu qid.version = 0; 23781a6ed33cSAntonios Motakis } 237960ce86c7SWei Liu 238060ce86c7SWei Liu /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */ 238160ce86c7SWei Liu len = pdu_marshal(pdu, 11 + count, "Qqbs", 238260ce86c7SWei Liu &qid, dent->d_off, 238360ce86c7SWei Liu dent->d_type, &name); 23847cde47d4SGreg Kurz 23857cde47d4SGreg Kurz v9fs_readdir_unlock(&fidp->fs.dir); 23867cde47d4SGreg Kurz 238760ce86c7SWei Liu if (len < 0) { 238860ce86c7SWei Liu v9fs_co_seekdir(pdu, fidp, saved_dir_pos); 238960ce86c7SWei Liu v9fs_string_free(&name); 239060ce86c7SWei Liu return len; 239160ce86c7SWei Liu } 239260ce86c7SWei Liu count += len; 239360ce86c7SWei Liu v9fs_string_free(&name); 239460ce86c7SWei Liu saved_dir_pos = dent->d_off; 239560ce86c7SWei Liu } 23967cde47d4SGreg Kurz 23977cde47d4SGreg Kurz v9fs_readdir_unlock(&fidp->fs.dir); 23987cde47d4SGreg Kurz 239960ce86c7SWei Liu if (err < 0) { 240060ce86c7SWei Liu return err; 240160ce86c7SWei Liu } 240260ce86c7SWei Liu return count; 240360ce86c7SWei Liu } 240460ce86c7SWei Liu 24058440e22eSGreg Kurz static void coroutine_fn v9fs_readdir(void *opaque) 240660ce86c7SWei Liu { 240760ce86c7SWei Liu int32_t fid; 240860ce86c7SWei Liu V9fsFidState *fidp; 240960ce86c7SWei Liu ssize_t retval = 0; 241060ce86c7SWei Liu size_t offset = 7; 241160ce86c7SWei Liu uint64_t initial_offset; 241260ce86c7SWei Liu int32_t count; 241360ce86c7SWei Liu uint32_t max_count; 241460ce86c7SWei Liu V9fsPDU *pdu = opaque; 241560ce86c7SWei Liu 241660ce86c7SWei Liu retval = pdu_unmarshal(pdu, offset, "dqd", &fid, 241760ce86c7SWei Liu &initial_offset, &max_count); 241860ce86c7SWei Liu if (retval < 0) { 241960ce86c7SWei Liu goto out_nofid; 242060ce86c7SWei Liu } 242160ce86c7SWei Liu trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count); 242260ce86c7SWei Liu 242360ce86c7SWei Liu fidp = get_fid(pdu, fid); 242460ce86c7SWei Liu if (fidp == NULL) { 242560ce86c7SWei Liu retval = -EINVAL; 242660ce86c7SWei Liu goto out_nofid; 242760ce86c7SWei Liu } 2428f314ea4eSGreg Kurz if (!fidp->fs.dir.stream) { 242960ce86c7SWei Liu retval = -EINVAL; 243060ce86c7SWei Liu goto out; 243160ce86c7SWei Liu } 243260ce86c7SWei Liu if (initial_offset == 0) { 243360ce86c7SWei Liu v9fs_co_rewinddir(pdu, fidp); 243460ce86c7SWei Liu } else { 243560ce86c7SWei Liu v9fs_co_seekdir(pdu, fidp, initial_offset); 243660ce86c7SWei Liu } 243760ce86c7SWei Liu count = v9fs_do_readdir(pdu, fidp, max_count); 243860ce86c7SWei Liu if (count < 0) { 243960ce86c7SWei Liu retval = count; 244060ce86c7SWei Liu goto out; 244160ce86c7SWei Liu } 244260ce86c7SWei Liu retval = pdu_marshal(pdu, offset, "d", count); 244360ce86c7SWei Liu if (retval < 0) { 244460ce86c7SWei Liu goto out; 244560ce86c7SWei Liu } 244660ce86c7SWei Liu retval += count + offset; 244760ce86c7SWei Liu trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval); 244860ce86c7SWei Liu out: 244960ce86c7SWei Liu put_fid(pdu, fidp); 245060ce86c7SWei Liu out_nofid: 245160ce86c7SWei Liu pdu_complete(pdu, retval); 245260ce86c7SWei Liu } 245360ce86c7SWei Liu 245460ce86c7SWei Liu static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp, 245560ce86c7SWei Liu uint64_t off, uint32_t count, 245660ce86c7SWei Liu struct iovec *sg, int cnt) 245760ce86c7SWei Liu { 245860ce86c7SWei Liu int i, to_copy; 245960ce86c7SWei Liu ssize_t err = 0; 24607e55d65cSLi Qiang uint64_t write_count; 246160ce86c7SWei Liu size_t offset = 7; 246260ce86c7SWei Liu 246360ce86c7SWei Liu 24647e55d65cSLi Qiang if (fidp->fs.xattr.len < off) { 246560ce86c7SWei Liu err = -ENOSPC; 246660ce86c7SWei Liu goto out; 246760ce86c7SWei Liu } 24687e55d65cSLi Qiang write_count = fidp->fs.xattr.len - off; 24697e55d65cSLi Qiang if (write_count > count) { 24707e55d65cSLi Qiang write_count = count; 24717e55d65cSLi Qiang } 247260ce86c7SWei Liu err = pdu_marshal(pdu, offset, "d", write_count); 247360ce86c7SWei Liu if (err < 0) { 247460ce86c7SWei Liu return err; 247560ce86c7SWei Liu } 247660ce86c7SWei Liu err += offset; 247760ce86c7SWei Liu fidp->fs.xattr.copied_len += write_count; 247860ce86c7SWei Liu /* 247960ce86c7SWei Liu * Now copy the content from sg list 248060ce86c7SWei Liu */ 248160ce86c7SWei Liu for (i = 0; i < cnt; i++) { 248260ce86c7SWei Liu if (write_count > sg[i].iov_len) { 248360ce86c7SWei Liu to_copy = sg[i].iov_len; 248460ce86c7SWei Liu } else { 248560ce86c7SWei Liu to_copy = write_count; 248660ce86c7SWei Liu } 248760ce86c7SWei Liu memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy); 248860ce86c7SWei Liu /* updating vs->off since we are not using below */ 248960ce86c7SWei Liu off += to_copy; 249060ce86c7SWei Liu write_count -= to_copy; 249160ce86c7SWei Liu } 249260ce86c7SWei Liu out: 249360ce86c7SWei Liu return err; 249460ce86c7SWei Liu } 249560ce86c7SWei Liu 24968440e22eSGreg Kurz static void coroutine_fn v9fs_write(void *opaque) 249760ce86c7SWei Liu { 249860ce86c7SWei Liu ssize_t err; 249960ce86c7SWei Liu int32_t fid; 250060ce86c7SWei Liu uint64_t off; 250160ce86c7SWei Liu uint32_t count; 250260ce86c7SWei Liu int32_t len = 0; 250360ce86c7SWei Liu int32_t total = 0; 250460ce86c7SWei Liu size_t offset = 7; 250560ce86c7SWei Liu V9fsFidState *fidp; 250660ce86c7SWei Liu V9fsPDU *pdu = opaque; 250760ce86c7SWei Liu V9fsState *s = pdu->s; 250860ce86c7SWei Liu QEMUIOVector qiov_full; 250960ce86c7SWei Liu QEMUIOVector qiov; 251060ce86c7SWei Liu 251160ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count); 251260ce86c7SWei Liu if (err < 0) { 251360ce86c7SWei Liu pdu_complete(pdu, err); 251460ce86c7SWei Liu return; 251560ce86c7SWei Liu } 251660ce86c7SWei Liu offset += err; 251760ce86c7SWei Liu v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, count, true); 251860ce86c7SWei Liu trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov); 251960ce86c7SWei Liu 252060ce86c7SWei Liu fidp = get_fid(pdu, fid); 252160ce86c7SWei Liu if (fidp == NULL) { 252260ce86c7SWei Liu err = -EINVAL; 252360ce86c7SWei Liu goto out_nofid; 252460ce86c7SWei Liu } 252560ce86c7SWei Liu if (fidp->fid_type == P9_FID_FILE) { 252660ce86c7SWei Liu if (fidp->fs.fd == -1) { 252760ce86c7SWei Liu err = -EINVAL; 252860ce86c7SWei Liu goto out; 252960ce86c7SWei Liu } 253060ce86c7SWei Liu } else if (fidp->fid_type == P9_FID_XATTR) { 253160ce86c7SWei Liu /* 253260ce86c7SWei Liu * setxattr operation 253360ce86c7SWei Liu */ 253460ce86c7SWei Liu err = v9fs_xattr_write(s, pdu, fidp, off, count, 253560ce86c7SWei Liu qiov_full.iov, qiov_full.niov); 253660ce86c7SWei Liu goto out; 253760ce86c7SWei Liu } else { 253860ce86c7SWei Liu err = -EINVAL; 253960ce86c7SWei Liu goto out; 254060ce86c7SWei Liu } 254160ce86c7SWei Liu qemu_iovec_init(&qiov, qiov_full.niov); 254260ce86c7SWei Liu do { 254360ce86c7SWei Liu qemu_iovec_reset(&qiov); 254460ce86c7SWei Liu qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total); 254560ce86c7SWei Liu if (0) { 254660ce86c7SWei Liu print_sg(qiov.iov, qiov.niov); 254760ce86c7SWei Liu } 254860ce86c7SWei Liu /* Loop in case of EINTR */ 254960ce86c7SWei Liu do { 255060ce86c7SWei Liu len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off); 255160ce86c7SWei Liu if (len >= 0) { 255260ce86c7SWei Liu off += len; 255360ce86c7SWei Liu total += len; 255460ce86c7SWei Liu } 255560ce86c7SWei Liu } while (len == -EINTR && !pdu->cancelled); 255660ce86c7SWei Liu if (len < 0) { 255760ce86c7SWei Liu /* IO error return the error */ 255860ce86c7SWei Liu err = len; 255960ce86c7SWei Liu goto out_qiov; 256060ce86c7SWei Liu } 256160ce86c7SWei Liu } while (total < count && len > 0); 256260ce86c7SWei Liu 256360ce86c7SWei Liu offset = 7; 256460ce86c7SWei Liu err = pdu_marshal(pdu, offset, "d", total); 256560ce86c7SWei Liu if (err < 0) { 2566fdfcc9aeSLi Qiang goto out_qiov; 256760ce86c7SWei Liu } 256860ce86c7SWei Liu err += offset; 256960ce86c7SWei Liu trace_v9fs_write_return(pdu->tag, pdu->id, total, err); 257060ce86c7SWei Liu out_qiov: 257160ce86c7SWei Liu qemu_iovec_destroy(&qiov); 257260ce86c7SWei Liu out: 257360ce86c7SWei Liu put_fid(pdu, fidp); 257460ce86c7SWei Liu out_nofid: 257560ce86c7SWei Liu qemu_iovec_destroy(&qiov_full); 257660ce86c7SWei Liu pdu_complete(pdu, err); 257760ce86c7SWei Liu } 257860ce86c7SWei Liu 25798440e22eSGreg Kurz static void coroutine_fn v9fs_create(void *opaque) 258060ce86c7SWei Liu { 258160ce86c7SWei Liu int32_t fid; 258260ce86c7SWei Liu int err = 0; 258360ce86c7SWei Liu size_t offset = 7; 258460ce86c7SWei Liu V9fsFidState *fidp; 258560ce86c7SWei Liu V9fsQID qid; 258660ce86c7SWei Liu int32_t perm; 258760ce86c7SWei Liu int8_t mode; 258860ce86c7SWei Liu V9fsPath path; 258960ce86c7SWei Liu struct stat stbuf; 259060ce86c7SWei Liu V9fsString name; 259160ce86c7SWei Liu V9fsString extension; 259260ce86c7SWei Liu int iounit; 259360ce86c7SWei Liu V9fsPDU *pdu = opaque; 25945b3c77aaSGreg Kurz V9fsState *s = pdu->s; 259560ce86c7SWei Liu 259660ce86c7SWei Liu v9fs_path_init(&path); 259760ce86c7SWei Liu v9fs_string_init(&name); 259860ce86c7SWei Liu v9fs_string_init(&extension); 259960ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name, 260060ce86c7SWei Liu &perm, &mode, &extension); 260160ce86c7SWei Liu if (err < 0) { 260260ce86c7SWei Liu goto out_nofid; 260360ce86c7SWei Liu } 260460ce86c7SWei Liu trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode); 260560ce86c7SWei Liu 2606fff39a7aSGreg Kurz if (name_is_illegal(name.data)) { 2607fff39a7aSGreg Kurz err = -ENOENT; 2608fff39a7aSGreg Kurz goto out_nofid; 2609fff39a7aSGreg Kurz } 2610fff39a7aSGreg Kurz 2611805b5d98SGreg Kurz if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 2612805b5d98SGreg Kurz err = -EEXIST; 2613805b5d98SGreg Kurz goto out_nofid; 2614805b5d98SGreg Kurz } 2615805b5d98SGreg Kurz 261660ce86c7SWei Liu fidp = get_fid(pdu, fid); 261760ce86c7SWei Liu if (fidp == NULL) { 261860ce86c7SWei Liu err = -EINVAL; 261960ce86c7SWei Liu goto out_nofid; 262060ce86c7SWei Liu } 2621d63fb193SLi Qiang if (fidp->fid_type != P9_FID_NONE) { 2622d63fb193SLi Qiang err = -EINVAL; 2623d63fb193SLi Qiang goto out; 2624d63fb193SLi Qiang } 262560ce86c7SWei Liu if (perm & P9_STAT_MODE_DIR) { 262660ce86c7SWei Liu err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777, 262760ce86c7SWei Liu fidp->uid, -1, &stbuf); 262860ce86c7SWei Liu if (err < 0) { 262960ce86c7SWei Liu goto out; 263060ce86c7SWei Liu } 263160ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); 263260ce86c7SWei Liu if (err < 0) { 263360ce86c7SWei Liu goto out; 263460ce86c7SWei Liu } 26355b3c77aaSGreg Kurz v9fs_path_write_lock(s); 263660ce86c7SWei Liu v9fs_path_copy(&fidp->path, &path); 26375b3c77aaSGreg Kurz v9fs_path_unlock(s); 263860ce86c7SWei Liu err = v9fs_co_opendir(pdu, fidp); 263960ce86c7SWei Liu if (err < 0) { 264060ce86c7SWei Liu goto out; 264160ce86c7SWei Liu } 264260ce86c7SWei Liu fidp->fid_type = P9_FID_DIR; 264360ce86c7SWei Liu } else if (perm & P9_STAT_MODE_SYMLINK) { 264460ce86c7SWei Liu err = v9fs_co_symlink(pdu, fidp, &name, 264560ce86c7SWei Liu extension.data, -1 , &stbuf); 264660ce86c7SWei Liu if (err < 0) { 264760ce86c7SWei Liu goto out; 264860ce86c7SWei Liu } 264960ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); 265060ce86c7SWei Liu if (err < 0) { 265160ce86c7SWei Liu goto out; 265260ce86c7SWei Liu } 26535b3c77aaSGreg Kurz v9fs_path_write_lock(s); 265460ce86c7SWei Liu v9fs_path_copy(&fidp->path, &path); 26555b3c77aaSGreg Kurz v9fs_path_unlock(s); 265660ce86c7SWei Liu } else if (perm & P9_STAT_MODE_LINK) { 265760ce86c7SWei Liu int32_t ofid = atoi(extension.data); 265860ce86c7SWei Liu V9fsFidState *ofidp = get_fid(pdu, ofid); 265960ce86c7SWei Liu if (ofidp == NULL) { 266060ce86c7SWei Liu err = -EINVAL; 266160ce86c7SWei Liu goto out; 266260ce86c7SWei Liu } 266360ce86c7SWei Liu err = v9fs_co_link(pdu, ofidp, fidp, &name); 266460ce86c7SWei Liu put_fid(pdu, ofidp); 266560ce86c7SWei Liu if (err < 0) { 266660ce86c7SWei Liu goto out; 266760ce86c7SWei Liu } 266860ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); 266960ce86c7SWei Liu if (err < 0) { 267060ce86c7SWei Liu fidp->fid_type = P9_FID_NONE; 267160ce86c7SWei Liu goto out; 267260ce86c7SWei Liu } 26735b3c77aaSGreg Kurz v9fs_path_write_lock(s); 267460ce86c7SWei Liu v9fs_path_copy(&fidp->path, &path); 26755b3c77aaSGreg Kurz v9fs_path_unlock(s); 267660ce86c7SWei Liu err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); 267760ce86c7SWei Liu if (err < 0) { 267860ce86c7SWei Liu fidp->fid_type = P9_FID_NONE; 267960ce86c7SWei Liu goto out; 268060ce86c7SWei Liu } 268160ce86c7SWei Liu } else if (perm & P9_STAT_MODE_DEVICE) { 268260ce86c7SWei Liu char ctype; 268360ce86c7SWei Liu uint32_t major, minor; 268460ce86c7SWei Liu mode_t nmode = 0; 268560ce86c7SWei Liu 268660ce86c7SWei Liu if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) { 268760ce86c7SWei Liu err = -errno; 268860ce86c7SWei Liu goto out; 268960ce86c7SWei Liu } 269060ce86c7SWei Liu 269160ce86c7SWei Liu switch (ctype) { 269260ce86c7SWei Liu case 'c': 269360ce86c7SWei Liu nmode = S_IFCHR; 269460ce86c7SWei Liu break; 269560ce86c7SWei Liu case 'b': 269660ce86c7SWei Liu nmode = S_IFBLK; 269760ce86c7SWei Liu break; 269860ce86c7SWei Liu default: 269960ce86c7SWei Liu err = -EIO; 270060ce86c7SWei Liu goto out; 270160ce86c7SWei Liu } 270260ce86c7SWei Liu 270360ce86c7SWei Liu nmode |= perm & 0777; 270460ce86c7SWei Liu err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1, 270560ce86c7SWei Liu makedev(major, minor), nmode, &stbuf); 270660ce86c7SWei Liu if (err < 0) { 270760ce86c7SWei Liu goto out; 270860ce86c7SWei Liu } 270960ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); 271060ce86c7SWei Liu if (err < 0) { 271160ce86c7SWei Liu goto out; 271260ce86c7SWei Liu } 27135b3c77aaSGreg Kurz v9fs_path_write_lock(s); 271460ce86c7SWei Liu v9fs_path_copy(&fidp->path, &path); 27155b3c77aaSGreg Kurz v9fs_path_unlock(s); 271660ce86c7SWei Liu } else if (perm & P9_STAT_MODE_NAMED_PIPE) { 271760ce86c7SWei Liu err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1, 271860ce86c7SWei Liu 0, S_IFIFO | (perm & 0777), &stbuf); 271960ce86c7SWei Liu if (err < 0) { 272060ce86c7SWei Liu goto out; 272160ce86c7SWei Liu } 272260ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); 272360ce86c7SWei Liu if (err < 0) { 272460ce86c7SWei Liu goto out; 272560ce86c7SWei Liu } 27265b3c77aaSGreg Kurz v9fs_path_write_lock(s); 272760ce86c7SWei Liu v9fs_path_copy(&fidp->path, &path); 27285b3c77aaSGreg Kurz v9fs_path_unlock(s); 272960ce86c7SWei Liu } else if (perm & P9_STAT_MODE_SOCKET) { 273060ce86c7SWei Liu err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1, 273160ce86c7SWei Liu 0, S_IFSOCK | (perm & 0777), &stbuf); 273260ce86c7SWei Liu if (err < 0) { 273360ce86c7SWei Liu goto out; 273460ce86c7SWei Liu } 273560ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); 273660ce86c7SWei Liu if (err < 0) { 273760ce86c7SWei Liu goto out; 273860ce86c7SWei Liu } 27395b3c77aaSGreg Kurz v9fs_path_write_lock(s); 274060ce86c7SWei Liu v9fs_path_copy(&fidp->path, &path); 27415b3c77aaSGreg Kurz v9fs_path_unlock(s); 274260ce86c7SWei Liu } else { 274360ce86c7SWei Liu err = v9fs_co_open2(pdu, fidp, &name, -1, 274460ce86c7SWei Liu omode_to_uflags(mode)|O_CREAT, perm, &stbuf); 274560ce86c7SWei Liu if (err < 0) { 274660ce86c7SWei Liu goto out; 274760ce86c7SWei Liu } 274860ce86c7SWei Liu fidp->fid_type = P9_FID_FILE; 274960ce86c7SWei Liu fidp->open_flags = omode_to_uflags(mode); 275060ce86c7SWei Liu if (fidp->open_flags & O_EXCL) { 275160ce86c7SWei Liu /* 275260ce86c7SWei Liu * We let the host file system do O_EXCL check 275360ce86c7SWei Liu * We should not reclaim such fd 275460ce86c7SWei Liu */ 275560ce86c7SWei Liu fidp->flags |= FID_NON_RECLAIMABLE; 275660ce86c7SWei Liu } 275760ce86c7SWei Liu } 275860ce86c7SWei Liu iounit = get_iounit(pdu, &fidp->path); 27593b5ee9e8SAntonios Motakis err = stat_to_qid(pdu, &stbuf, &qid); 27603b5ee9e8SAntonios Motakis if (err < 0) { 27613b5ee9e8SAntonios Motakis goto out; 27623b5ee9e8SAntonios Motakis } 276360ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Qd", &qid, iounit); 276460ce86c7SWei Liu if (err < 0) { 276560ce86c7SWei Liu goto out; 276660ce86c7SWei Liu } 276760ce86c7SWei Liu err += offset; 276860ce86c7SWei Liu trace_v9fs_create_return(pdu->tag, pdu->id, 276960ce86c7SWei Liu qid.type, qid.version, qid.path, iounit); 277060ce86c7SWei Liu out: 277160ce86c7SWei Liu put_fid(pdu, fidp); 277260ce86c7SWei Liu out_nofid: 277360ce86c7SWei Liu pdu_complete(pdu, err); 277460ce86c7SWei Liu v9fs_string_free(&name); 277560ce86c7SWei Liu v9fs_string_free(&extension); 277660ce86c7SWei Liu v9fs_path_free(&path); 277760ce86c7SWei Liu } 277860ce86c7SWei Liu 27798440e22eSGreg Kurz static void coroutine_fn v9fs_symlink(void *opaque) 278060ce86c7SWei Liu { 278160ce86c7SWei Liu V9fsPDU *pdu = opaque; 278260ce86c7SWei Liu V9fsString name; 278360ce86c7SWei Liu V9fsString symname; 278460ce86c7SWei Liu V9fsFidState *dfidp; 278560ce86c7SWei Liu V9fsQID qid; 278660ce86c7SWei Liu struct stat stbuf; 278760ce86c7SWei Liu int32_t dfid; 278860ce86c7SWei Liu int err = 0; 278960ce86c7SWei Liu gid_t gid; 279060ce86c7SWei Liu size_t offset = 7; 279160ce86c7SWei Liu 279260ce86c7SWei Liu v9fs_string_init(&name); 279360ce86c7SWei Liu v9fs_string_init(&symname); 279460ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid); 279560ce86c7SWei Liu if (err < 0) { 279660ce86c7SWei Liu goto out_nofid; 279760ce86c7SWei Liu } 279860ce86c7SWei Liu trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid); 279960ce86c7SWei Liu 2800fff39a7aSGreg Kurz if (name_is_illegal(name.data)) { 2801fff39a7aSGreg Kurz err = -ENOENT; 2802fff39a7aSGreg Kurz goto out_nofid; 2803fff39a7aSGreg Kurz } 2804fff39a7aSGreg Kurz 2805805b5d98SGreg Kurz if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 2806805b5d98SGreg Kurz err = -EEXIST; 2807805b5d98SGreg Kurz goto out_nofid; 2808805b5d98SGreg Kurz } 2809805b5d98SGreg Kurz 281060ce86c7SWei Liu dfidp = get_fid(pdu, dfid); 281160ce86c7SWei Liu if (dfidp == NULL) { 281260ce86c7SWei Liu err = -EINVAL; 281360ce86c7SWei Liu goto out_nofid; 281460ce86c7SWei Liu } 281560ce86c7SWei Liu err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf); 281660ce86c7SWei Liu if (err < 0) { 281760ce86c7SWei Liu goto out; 281860ce86c7SWei Liu } 28193b5ee9e8SAntonios Motakis err = stat_to_qid(pdu, &stbuf, &qid); 28203b5ee9e8SAntonios Motakis if (err < 0) { 28213b5ee9e8SAntonios Motakis goto out; 28223b5ee9e8SAntonios Motakis } 282360ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Q", &qid); 282460ce86c7SWei Liu if (err < 0) { 282560ce86c7SWei Liu goto out; 282660ce86c7SWei Liu } 282760ce86c7SWei Liu err += offset; 282860ce86c7SWei Liu trace_v9fs_symlink_return(pdu->tag, pdu->id, 282960ce86c7SWei Liu qid.type, qid.version, qid.path); 283060ce86c7SWei Liu out: 283160ce86c7SWei Liu put_fid(pdu, dfidp); 283260ce86c7SWei Liu out_nofid: 283360ce86c7SWei Liu pdu_complete(pdu, err); 283460ce86c7SWei Liu v9fs_string_free(&name); 283560ce86c7SWei Liu v9fs_string_free(&symname); 283660ce86c7SWei Liu } 283760ce86c7SWei Liu 2838a1bf8b74SGreg Kurz static void coroutine_fn v9fs_flush(void *opaque) 283960ce86c7SWei Liu { 284060ce86c7SWei Liu ssize_t err; 284160ce86c7SWei Liu int16_t tag; 284260ce86c7SWei Liu size_t offset = 7; 2843d5f2af7bSGreg Kurz V9fsPDU *cancel_pdu = NULL; 284460ce86c7SWei Liu V9fsPDU *pdu = opaque; 284560ce86c7SWei Liu V9fsState *s = pdu->s; 284660ce86c7SWei Liu 284760ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "w", &tag); 284860ce86c7SWei Liu if (err < 0) { 284960ce86c7SWei Liu pdu_complete(pdu, err); 285060ce86c7SWei Liu return; 285160ce86c7SWei Liu } 285260ce86c7SWei Liu trace_v9fs_flush(pdu->tag, pdu->id, tag); 285360ce86c7SWei Liu 2854d5f2af7bSGreg Kurz if (pdu->tag == tag) { 28553dc6f869SAlistair Francis warn_report("the guest sent a self-referencing 9P flush request"); 2856d5f2af7bSGreg Kurz } else { 285760ce86c7SWei Liu QLIST_FOREACH(cancel_pdu, &s->active_list, next) { 285860ce86c7SWei Liu if (cancel_pdu->tag == tag) { 285960ce86c7SWei Liu break; 286060ce86c7SWei Liu } 286160ce86c7SWei Liu } 2862d5f2af7bSGreg Kurz } 286360ce86c7SWei Liu if (cancel_pdu) { 286460ce86c7SWei Liu cancel_pdu->cancelled = 1; 286560ce86c7SWei Liu /* 286660ce86c7SWei Liu * Wait for pdu to complete. 286760ce86c7SWei Liu */ 28681ace7ceaSPaolo Bonzini qemu_co_queue_wait(&cancel_pdu->complete, NULL); 286918adde86SGreg Kurz if (!qemu_co_queue_next(&cancel_pdu->complete)) { 287060ce86c7SWei Liu cancel_pdu->cancelled = 0; 287160ce86c7SWei Liu pdu_free(cancel_pdu); 287260ce86c7SWei Liu } 287318adde86SGreg Kurz } 287460ce86c7SWei Liu pdu_complete(pdu, 7); 287560ce86c7SWei Liu } 287660ce86c7SWei Liu 28778440e22eSGreg Kurz static void coroutine_fn v9fs_link(void *opaque) 287860ce86c7SWei Liu { 287960ce86c7SWei Liu V9fsPDU *pdu = opaque; 288060ce86c7SWei Liu int32_t dfid, oldfid; 288160ce86c7SWei Liu V9fsFidState *dfidp, *oldfidp; 288260ce86c7SWei Liu V9fsString name; 288360ce86c7SWei Liu size_t offset = 7; 288460ce86c7SWei Liu int err = 0; 288560ce86c7SWei Liu 288660ce86c7SWei Liu v9fs_string_init(&name); 288760ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name); 288860ce86c7SWei Liu if (err < 0) { 288960ce86c7SWei Liu goto out_nofid; 289060ce86c7SWei Liu } 289160ce86c7SWei Liu trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data); 289260ce86c7SWei Liu 2893fff39a7aSGreg Kurz if (name_is_illegal(name.data)) { 2894fff39a7aSGreg Kurz err = -ENOENT; 2895fff39a7aSGreg Kurz goto out_nofid; 2896fff39a7aSGreg Kurz } 2897fff39a7aSGreg Kurz 2898805b5d98SGreg Kurz if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 2899805b5d98SGreg Kurz err = -EEXIST; 2900805b5d98SGreg Kurz goto out_nofid; 2901805b5d98SGreg Kurz } 2902805b5d98SGreg Kurz 290360ce86c7SWei Liu dfidp = get_fid(pdu, dfid); 290460ce86c7SWei Liu if (dfidp == NULL) { 290560ce86c7SWei Liu err = -ENOENT; 290660ce86c7SWei Liu goto out_nofid; 290760ce86c7SWei Liu } 290860ce86c7SWei Liu 290960ce86c7SWei Liu oldfidp = get_fid(pdu, oldfid); 291060ce86c7SWei Liu if (oldfidp == NULL) { 291160ce86c7SWei Liu err = -ENOENT; 291260ce86c7SWei Liu goto out; 291360ce86c7SWei Liu } 291460ce86c7SWei Liu err = v9fs_co_link(pdu, oldfidp, dfidp, &name); 291560ce86c7SWei Liu if (!err) { 291660ce86c7SWei Liu err = offset; 291760ce86c7SWei Liu } 29184c158678SLi Qiang put_fid(pdu, oldfidp); 291960ce86c7SWei Liu out: 292060ce86c7SWei Liu put_fid(pdu, dfidp); 292160ce86c7SWei Liu out_nofid: 292260ce86c7SWei Liu v9fs_string_free(&name); 292360ce86c7SWei Liu pdu_complete(pdu, err); 292460ce86c7SWei Liu } 292560ce86c7SWei Liu 292660ce86c7SWei Liu /* Only works with path name based fid */ 29278440e22eSGreg Kurz static void coroutine_fn v9fs_remove(void *opaque) 292860ce86c7SWei Liu { 292960ce86c7SWei Liu int32_t fid; 293060ce86c7SWei Liu int err = 0; 293160ce86c7SWei Liu size_t offset = 7; 293260ce86c7SWei Liu V9fsFidState *fidp; 293360ce86c7SWei Liu V9fsPDU *pdu = opaque; 293460ce86c7SWei Liu 293560ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "d", &fid); 293660ce86c7SWei Liu if (err < 0) { 293760ce86c7SWei Liu goto out_nofid; 293860ce86c7SWei Liu } 293960ce86c7SWei Liu trace_v9fs_remove(pdu->tag, pdu->id, fid); 294060ce86c7SWei Liu 294160ce86c7SWei Liu fidp = get_fid(pdu, fid); 294260ce86c7SWei Liu if (fidp == NULL) { 294360ce86c7SWei Liu err = -EINVAL; 294460ce86c7SWei Liu goto out_nofid; 294560ce86c7SWei Liu } 294660ce86c7SWei Liu /* if fs driver is not path based, return EOPNOTSUPP */ 294760ce86c7SWei Liu if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) { 294860ce86c7SWei Liu err = -EOPNOTSUPP; 294960ce86c7SWei Liu goto out_err; 295060ce86c7SWei Liu } 295160ce86c7SWei Liu /* 295260ce86c7SWei Liu * IF the file is unlinked, we cannot reopen 295360ce86c7SWei Liu * the file later. So don't reclaim fd 295460ce86c7SWei Liu */ 295560ce86c7SWei Liu err = v9fs_mark_fids_unreclaim(pdu, &fidp->path); 295660ce86c7SWei Liu if (err < 0) { 295760ce86c7SWei Liu goto out_err; 295860ce86c7SWei Liu } 295960ce86c7SWei Liu err = v9fs_co_remove(pdu, &fidp->path); 296060ce86c7SWei Liu if (!err) { 296160ce86c7SWei Liu err = offset; 296260ce86c7SWei Liu } 296360ce86c7SWei Liu out_err: 296460ce86c7SWei Liu /* For TREMOVE we need to clunk the fid even on failed remove */ 296560ce86c7SWei Liu clunk_fid(pdu->s, fidp->fid); 296660ce86c7SWei Liu put_fid(pdu, fidp); 296760ce86c7SWei Liu out_nofid: 296860ce86c7SWei Liu pdu_complete(pdu, err); 296960ce86c7SWei Liu } 297060ce86c7SWei Liu 29718440e22eSGreg Kurz static void coroutine_fn v9fs_unlinkat(void *opaque) 297260ce86c7SWei Liu { 297360ce86c7SWei Liu int err = 0; 297460ce86c7SWei Liu V9fsString name; 297567e87345SKeno Fischer int32_t dfid, flags, rflags = 0; 297660ce86c7SWei Liu size_t offset = 7; 297760ce86c7SWei Liu V9fsPath path; 297860ce86c7SWei Liu V9fsFidState *dfidp; 297960ce86c7SWei Liu V9fsPDU *pdu = opaque; 298060ce86c7SWei Liu 298160ce86c7SWei Liu v9fs_string_init(&name); 298260ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags); 298360ce86c7SWei Liu if (err < 0) { 298460ce86c7SWei Liu goto out_nofid; 298560ce86c7SWei Liu } 2986fff39a7aSGreg Kurz 2987fff39a7aSGreg Kurz if (name_is_illegal(name.data)) { 2988fff39a7aSGreg Kurz err = -ENOENT; 2989fff39a7aSGreg Kurz goto out_nofid; 2990fff39a7aSGreg Kurz } 2991fff39a7aSGreg Kurz 2992805b5d98SGreg Kurz if (!strcmp(".", name.data)) { 2993805b5d98SGreg Kurz err = -EINVAL; 2994805b5d98SGreg Kurz goto out_nofid; 2995805b5d98SGreg Kurz } 2996805b5d98SGreg Kurz 2997805b5d98SGreg Kurz if (!strcmp("..", name.data)) { 2998805b5d98SGreg Kurz err = -ENOTEMPTY; 2999805b5d98SGreg Kurz goto out_nofid; 3000805b5d98SGreg Kurz } 3001805b5d98SGreg Kurz 300267e87345SKeno Fischer if (flags & ~P9_DOTL_AT_REMOVEDIR) { 300367e87345SKeno Fischer err = -EINVAL; 300467e87345SKeno Fischer goto out_nofid; 300567e87345SKeno Fischer } 300667e87345SKeno Fischer 300767e87345SKeno Fischer if (flags & P9_DOTL_AT_REMOVEDIR) { 300867e87345SKeno Fischer rflags |= AT_REMOVEDIR; 300967e87345SKeno Fischer } 301067e87345SKeno Fischer 301160ce86c7SWei Liu dfidp = get_fid(pdu, dfid); 301260ce86c7SWei Liu if (dfidp == NULL) { 301360ce86c7SWei Liu err = -EINVAL; 301460ce86c7SWei Liu goto out_nofid; 301560ce86c7SWei Liu } 301660ce86c7SWei Liu /* 301760ce86c7SWei Liu * IF the file is unlinked, we cannot reopen 301860ce86c7SWei Liu * the file later. So don't reclaim fd 301960ce86c7SWei Liu */ 302060ce86c7SWei Liu v9fs_path_init(&path); 302160ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path); 302260ce86c7SWei Liu if (err < 0) { 302360ce86c7SWei Liu goto out_err; 302460ce86c7SWei Liu } 302560ce86c7SWei Liu err = v9fs_mark_fids_unreclaim(pdu, &path); 302660ce86c7SWei Liu if (err < 0) { 302760ce86c7SWei Liu goto out_err; 302860ce86c7SWei Liu } 302967e87345SKeno Fischer err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, rflags); 303060ce86c7SWei Liu if (!err) { 303160ce86c7SWei Liu err = offset; 303260ce86c7SWei Liu } 303360ce86c7SWei Liu out_err: 303460ce86c7SWei Liu put_fid(pdu, dfidp); 303560ce86c7SWei Liu v9fs_path_free(&path); 303660ce86c7SWei Liu out_nofid: 303760ce86c7SWei Liu pdu_complete(pdu, err); 303860ce86c7SWei Liu v9fs_string_free(&name); 303960ce86c7SWei Liu } 304060ce86c7SWei Liu 304160ce86c7SWei Liu 304260ce86c7SWei Liu /* Only works with path name based fid */ 30438440e22eSGreg Kurz static int coroutine_fn v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp, 30448440e22eSGreg Kurz int32_t newdirfid, 30458440e22eSGreg Kurz V9fsString *name) 304660ce86c7SWei Liu { 304760ce86c7SWei Liu int err = 0; 304860ce86c7SWei Liu V9fsPath new_path; 304960ce86c7SWei Liu V9fsFidState *tfidp; 305060ce86c7SWei Liu V9fsState *s = pdu->s; 305160ce86c7SWei Liu V9fsFidState *dirfidp = NULL; 305260ce86c7SWei Liu 305360ce86c7SWei Liu v9fs_path_init(&new_path); 305460ce86c7SWei Liu if (newdirfid != -1) { 305560ce86c7SWei Liu dirfidp = get_fid(pdu, newdirfid); 305660ce86c7SWei Liu if (dirfidp == NULL) { 305760ce86c7SWei Liu err = -ENOENT; 305860ce86c7SWei Liu goto out_nofid; 305960ce86c7SWei Liu } 306049dd946bSGreg Kurz if (fidp->fid_type != P9_FID_NONE) { 306149dd946bSGreg Kurz err = -EINVAL; 306249dd946bSGreg Kurz goto out; 306349dd946bSGreg Kurz } 30644fa62005SGreg Kurz err = v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path); 30654fa62005SGreg Kurz if (err < 0) { 30664fa62005SGreg Kurz goto out; 30674fa62005SGreg Kurz } 306860ce86c7SWei Liu } else { 30694d8bc733SJan Dakinevich char *dir_name = g_path_get_dirname(fidp->path.data); 30704d8bc733SJan Dakinevich V9fsPath dir_path; 30714d8bc733SJan Dakinevich 30724d8bc733SJan Dakinevich v9fs_path_init(&dir_path); 30734d8bc733SJan Dakinevich v9fs_path_sprintf(&dir_path, "%s", dir_name); 30744d8bc733SJan Dakinevich g_free(dir_name); 30754d8bc733SJan Dakinevich 30764d8bc733SJan Dakinevich err = v9fs_co_name_to_path(pdu, &dir_path, name->data, &new_path); 30774d8bc733SJan Dakinevich v9fs_path_free(&dir_path); 30784fa62005SGreg Kurz if (err < 0) { 30794fa62005SGreg Kurz goto out; 30804fa62005SGreg Kurz } 308160ce86c7SWei Liu } 308260ce86c7SWei Liu err = v9fs_co_rename(pdu, &fidp->path, &new_path); 308360ce86c7SWei Liu if (err < 0) { 308460ce86c7SWei Liu goto out; 308560ce86c7SWei Liu } 308660ce86c7SWei Liu /* 308760ce86c7SWei Liu * Fixup fid's pointing to the old name to 308860ce86c7SWei Liu * start pointing to the new name 308960ce86c7SWei Liu */ 309060ce86c7SWei Liu for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) { 309160ce86c7SWei Liu if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) { 309260ce86c7SWei Liu /* replace the name */ 309360ce86c7SWei Liu v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data)); 309460ce86c7SWei Liu } 309560ce86c7SWei Liu } 309660ce86c7SWei Liu out: 309760ce86c7SWei Liu if (dirfidp) { 309860ce86c7SWei Liu put_fid(pdu, dirfidp); 309960ce86c7SWei Liu } 310060ce86c7SWei Liu v9fs_path_free(&new_path); 310160ce86c7SWei Liu out_nofid: 310260ce86c7SWei Liu return err; 310360ce86c7SWei Liu } 310460ce86c7SWei Liu 310560ce86c7SWei Liu /* Only works with path name based fid */ 31068440e22eSGreg Kurz static void coroutine_fn v9fs_rename(void *opaque) 310760ce86c7SWei Liu { 310860ce86c7SWei Liu int32_t fid; 310960ce86c7SWei Liu ssize_t err = 0; 311060ce86c7SWei Liu size_t offset = 7; 311160ce86c7SWei Liu V9fsString name; 311260ce86c7SWei Liu int32_t newdirfid; 311360ce86c7SWei Liu V9fsFidState *fidp; 311460ce86c7SWei Liu V9fsPDU *pdu = opaque; 311560ce86c7SWei Liu V9fsState *s = pdu->s; 311660ce86c7SWei Liu 311760ce86c7SWei Liu v9fs_string_init(&name); 311860ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name); 311960ce86c7SWei Liu if (err < 0) { 312060ce86c7SWei Liu goto out_nofid; 312160ce86c7SWei Liu } 3122fff39a7aSGreg Kurz 3123fff39a7aSGreg Kurz if (name_is_illegal(name.data)) { 3124fff39a7aSGreg Kurz err = -ENOENT; 3125fff39a7aSGreg Kurz goto out_nofid; 3126fff39a7aSGreg Kurz } 3127fff39a7aSGreg Kurz 3128805b5d98SGreg Kurz if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 3129805b5d98SGreg Kurz err = -EISDIR; 3130805b5d98SGreg Kurz goto out_nofid; 3131805b5d98SGreg Kurz } 3132805b5d98SGreg Kurz 313360ce86c7SWei Liu fidp = get_fid(pdu, fid); 313460ce86c7SWei Liu if (fidp == NULL) { 313560ce86c7SWei Liu err = -ENOENT; 313660ce86c7SWei Liu goto out_nofid; 313760ce86c7SWei Liu } 313849dd946bSGreg Kurz if (fidp->fid_type != P9_FID_NONE) { 313949dd946bSGreg Kurz err = -EINVAL; 314049dd946bSGreg Kurz goto out; 314149dd946bSGreg Kurz } 314260ce86c7SWei Liu /* if fs driver is not path based, return EOPNOTSUPP */ 314360ce86c7SWei Liu if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) { 314460ce86c7SWei Liu err = -EOPNOTSUPP; 314560ce86c7SWei Liu goto out; 314660ce86c7SWei Liu } 314760ce86c7SWei Liu v9fs_path_write_lock(s); 314860ce86c7SWei Liu err = v9fs_complete_rename(pdu, fidp, newdirfid, &name); 314960ce86c7SWei Liu v9fs_path_unlock(s); 315060ce86c7SWei Liu if (!err) { 315160ce86c7SWei Liu err = offset; 315260ce86c7SWei Liu } 315360ce86c7SWei Liu out: 315460ce86c7SWei Liu put_fid(pdu, fidp); 315560ce86c7SWei Liu out_nofid: 315660ce86c7SWei Liu pdu_complete(pdu, err); 315760ce86c7SWei Liu v9fs_string_free(&name); 315860ce86c7SWei Liu } 315960ce86c7SWei Liu 31604fa62005SGreg Kurz static int coroutine_fn v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir, 31618440e22eSGreg Kurz V9fsString *old_name, 31628440e22eSGreg Kurz V9fsPath *newdir, 316360ce86c7SWei Liu V9fsString *new_name) 316460ce86c7SWei Liu { 316560ce86c7SWei Liu V9fsFidState *tfidp; 316660ce86c7SWei Liu V9fsPath oldpath, newpath; 316760ce86c7SWei Liu V9fsState *s = pdu->s; 31684fa62005SGreg Kurz int err; 316960ce86c7SWei Liu 317060ce86c7SWei Liu v9fs_path_init(&oldpath); 317160ce86c7SWei Liu v9fs_path_init(&newpath); 31724fa62005SGreg Kurz err = v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath); 31734fa62005SGreg Kurz if (err < 0) { 31744fa62005SGreg Kurz goto out; 31754fa62005SGreg Kurz } 31764fa62005SGreg Kurz err = v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath); 31774fa62005SGreg Kurz if (err < 0) { 31784fa62005SGreg Kurz goto out; 31794fa62005SGreg Kurz } 318060ce86c7SWei Liu 318160ce86c7SWei Liu /* 318260ce86c7SWei Liu * Fixup fid's pointing to the old name to 318360ce86c7SWei Liu * start pointing to the new name 318460ce86c7SWei Liu */ 318560ce86c7SWei Liu for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) { 318660ce86c7SWei Liu if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) { 318760ce86c7SWei Liu /* replace the name */ 318860ce86c7SWei Liu v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data)); 318960ce86c7SWei Liu } 319060ce86c7SWei Liu } 31914fa62005SGreg Kurz out: 319260ce86c7SWei Liu v9fs_path_free(&oldpath); 319360ce86c7SWei Liu v9fs_path_free(&newpath); 31944fa62005SGreg Kurz return err; 319560ce86c7SWei Liu } 319660ce86c7SWei Liu 31978440e22eSGreg Kurz static int coroutine_fn v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid, 31988440e22eSGreg Kurz V9fsString *old_name, 31998440e22eSGreg Kurz int32_t newdirfid, 320060ce86c7SWei Liu V9fsString *new_name) 320160ce86c7SWei Liu { 320260ce86c7SWei Liu int err = 0; 320360ce86c7SWei Liu V9fsState *s = pdu->s; 320460ce86c7SWei Liu V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL; 320560ce86c7SWei Liu 320660ce86c7SWei Liu olddirfidp = get_fid(pdu, olddirfid); 320760ce86c7SWei Liu if (olddirfidp == NULL) { 320860ce86c7SWei Liu err = -ENOENT; 320960ce86c7SWei Liu goto out; 321060ce86c7SWei Liu } 321160ce86c7SWei Liu if (newdirfid != -1) { 321260ce86c7SWei Liu newdirfidp = get_fid(pdu, newdirfid); 321360ce86c7SWei Liu if (newdirfidp == NULL) { 321460ce86c7SWei Liu err = -ENOENT; 321560ce86c7SWei Liu goto out; 321660ce86c7SWei Liu } 321760ce86c7SWei Liu } else { 321860ce86c7SWei Liu newdirfidp = get_fid(pdu, olddirfid); 321960ce86c7SWei Liu } 322060ce86c7SWei Liu 322160ce86c7SWei Liu err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name, 322260ce86c7SWei Liu &newdirfidp->path, new_name); 322360ce86c7SWei Liu if (err < 0) { 322460ce86c7SWei Liu goto out; 322560ce86c7SWei Liu } 322660ce86c7SWei Liu if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) { 322760ce86c7SWei Liu /* Only for path based fid we need to do the below fixup */ 32284fa62005SGreg Kurz err = v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name, 322960ce86c7SWei Liu &newdirfidp->path, new_name); 323060ce86c7SWei Liu } 323160ce86c7SWei Liu out: 323260ce86c7SWei Liu if (olddirfidp) { 323360ce86c7SWei Liu put_fid(pdu, olddirfidp); 323460ce86c7SWei Liu } 323560ce86c7SWei Liu if (newdirfidp) { 323660ce86c7SWei Liu put_fid(pdu, newdirfidp); 323760ce86c7SWei Liu } 323860ce86c7SWei Liu return err; 323960ce86c7SWei Liu } 324060ce86c7SWei Liu 32418440e22eSGreg Kurz static void coroutine_fn v9fs_renameat(void *opaque) 324260ce86c7SWei Liu { 324360ce86c7SWei Liu ssize_t err = 0; 324460ce86c7SWei Liu size_t offset = 7; 324560ce86c7SWei Liu V9fsPDU *pdu = opaque; 324660ce86c7SWei Liu V9fsState *s = pdu->s; 324760ce86c7SWei Liu int32_t olddirfid, newdirfid; 324860ce86c7SWei Liu V9fsString old_name, new_name; 324960ce86c7SWei Liu 325060ce86c7SWei Liu v9fs_string_init(&old_name); 325160ce86c7SWei Liu v9fs_string_init(&new_name); 325260ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid, 325360ce86c7SWei Liu &old_name, &newdirfid, &new_name); 325460ce86c7SWei Liu if (err < 0) { 325560ce86c7SWei Liu goto out_err; 325660ce86c7SWei Liu } 325760ce86c7SWei Liu 3258fff39a7aSGreg Kurz if (name_is_illegal(old_name.data) || name_is_illegal(new_name.data)) { 3259fff39a7aSGreg Kurz err = -ENOENT; 3260fff39a7aSGreg Kurz goto out_err; 3261fff39a7aSGreg Kurz } 3262fff39a7aSGreg Kurz 3263805b5d98SGreg Kurz if (!strcmp(".", old_name.data) || !strcmp("..", old_name.data) || 3264805b5d98SGreg Kurz !strcmp(".", new_name.data) || !strcmp("..", new_name.data)) { 3265805b5d98SGreg Kurz err = -EISDIR; 3266805b5d98SGreg Kurz goto out_err; 3267805b5d98SGreg Kurz } 3268805b5d98SGreg Kurz 326960ce86c7SWei Liu v9fs_path_write_lock(s); 327060ce86c7SWei Liu err = v9fs_complete_renameat(pdu, olddirfid, 327160ce86c7SWei Liu &old_name, newdirfid, &new_name); 327260ce86c7SWei Liu v9fs_path_unlock(s); 327360ce86c7SWei Liu if (!err) { 327460ce86c7SWei Liu err = offset; 327560ce86c7SWei Liu } 327660ce86c7SWei Liu 327760ce86c7SWei Liu out_err: 327860ce86c7SWei Liu pdu_complete(pdu, err); 327960ce86c7SWei Liu v9fs_string_free(&old_name); 328060ce86c7SWei Liu v9fs_string_free(&new_name); 328160ce86c7SWei Liu } 328260ce86c7SWei Liu 32838440e22eSGreg Kurz static void coroutine_fn v9fs_wstat(void *opaque) 328460ce86c7SWei Liu { 328560ce86c7SWei Liu int32_t fid; 328660ce86c7SWei Liu int err = 0; 328760ce86c7SWei Liu int16_t unused; 328860ce86c7SWei Liu V9fsStat v9stat; 328960ce86c7SWei Liu size_t offset = 7; 329060ce86c7SWei Liu struct stat stbuf; 329160ce86c7SWei Liu V9fsFidState *fidp; 329260ce86c7SWei Liu V9fsPDU *pdu = opaque; 32931d203986SGreg Kurz V9fsState *s = pdu->s; 329460ce86c7SWei Liu 329560ce86c7SWei Liu v9fs_stat_init(&v9stat); 329660ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat); 329760ce86c7SWei Liu if (err < 0) { 329860ce86c7SWei Liu goto out_nofid; 329960ce86c7SWei Liu } 330060ce86c7SWei Liu trace_v9fs_wstat(pdu->tag, pdu->id, fid, 330160ce86c7SWei Liu v9stat.mode, v9stat.atime, v9stat.mtime); 330260ce86c7SWei Liu 330360ce86c7SWei Liu fidp = get_fid(pdu, fid); 330460ce86c7SWei Liu if (fidp == NULL) { 330560ce86c7SWei Liu err = -EINVAL; 330660ce86c7SWei Liu goto out_nofid; 330760ce86c7SWei Liu } 330860ce86c7SWei Liu /* do we need to sync the file? */ 330960ce86c7SWei Liu if (donttouch_stat(&v9stat)) { 331060ce86c7SWei Liu err = v9fs_co_fsync(pdu, fidp, 0); 331160ce86c7SWei Liu goto out; 331260ce86c7SWei Liu } 331360ce86c7SWei Liu if (v9stat.mode != -1) { 331460ce86c7SWei Liu uint32_t v9_mode; 331560ce86c7SWei Liu err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); 331660ce86c7SWei Liu if (err < 0) { 331760ce86c7SWei Liu goto out; 331860ce86c7SWei Liu } 331960ce86c7SWei Liu v9_mode = stat_to_v9mode(&stbuf); 332060ce86c7SWei Liu if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) != 332160ce86c7SWei Liu (v9_mode & P9_STAT_MODE_TYPE_BITS)) { 332260ce86c7SWei Liu /* Attempting to change the type */ 332360ce86c7SWei Liu err = -EIO; 332460ce86c7SWei Liu goto out; 332560ce86c7SWei Liu } 332660ce86c7SWei Liu err = v9fs_co_chmod(pdu, &fidp->path, 332760ce86c7SWei Liu v9mode_to_mode(v9stat.mode, 332860ce86c7SWei Liu &v9stat.extension)); 332960ce86c7SWei Liu if (err < 0) { 333060ce86c7SWei Liu goto out; 333160ce86c7SWei Liu } 333260ce86c7SWei Liu } 333360ce86c7SWei Liu if (v9stat.mtime != -1 || v9stat.atime != -1) { 333460ce86c7SWei Liu struct timespec times[2]; 333560ce86c7SWei Liu if (v9stat.atime != -1) { 333660ce86c7SWei Liu times[0].tv_sec = v9stat.atime; 333760ce86c7SWei Liu times[0].tv_nsec = 0; 333860ce86c7SWei Liu } else { 333960ce86c7SWei Liu times[0].tv_nsec = UTIME_OMIT; 334060ce86c7SWei Liu } 334160ce86c7SWei Liu if (v9stat.mtime != -1) { 334260ce86c7SWei Liu times[1].tv_sec = v9stat.mtime; 334360ce86c7SWei Liu times[1].tv_nsec = 0; 334460ce86c7SWei Liu } else { 334560ce86c7SWei Liu times[1].tv_nsec = UTIME_OMIT; 334660ce86c7SWei Liu } 334760ce86c7SWei Liu err = v9fs_co_utimensat(pdu, &fidp->path, times); 334860ce86c7SWei Liu if (err < 0) { 334960ce86c7SWei Liu goto out; 335060ce86c7SWei Liu } 335160ce86c7SWei Liu } 335260ce86c7SWei Liu if (v9stat.n_gid != -1 || v9stat.n_uid != -1) { 335360ce86c7SWei Liu err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid); 335460ce86c7SWei Liu if (err < 0) { 335560ce86c7SWei Liu goto out; 335660ce86c7SWei Liu } 335760ce86c7SWei Liu } 335860ce86c7SWei Liu if (v9stat.name.size != 0) { 33591d203986SGreg Kurz v9fs_path_write_lock(s); 336060ce86c7SWei Liu err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name); 33611d203986SGreg Kurz v9fs_path_unlock(s); 336260ce86c7SWei Liu if (err < 0) { 336360ce86c7SWei Liu goto out; 336460ce86c7SWei Liu } 336560ce86c7SWei Liu } 336660ce86c7SWei Liu if (v9stat.length != -1) { 336760ce86c7SWei Liu err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length); 336860ce86c7SWei Liu if (err < 0) { 336960ce86c7SWei Liu goto out; 337060ce86c7SWei Liu } 337160ce86c7SWei Liu } 337260ce86c7SWei Liu err = offset; 337360ce86c7SWei Liu out: 337460ce86c7SWei Liu put_fid(pdu, fidp); 337560ce86c7SWei Liu out_nofid: 337660ce86c7SWei Liu v9fs_stat_free(&v9stat); 337760ce86c7SWei Liu pdu_complete(pdu, err); 337860ce86c7SWei Liu } 337960ce86c7SWei Liu 338060ce86c7SWei Liu static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf) 338160ce86c7SWei Liu { 338260ce86c7SWei Liu uint32_t f_type; 338360ce86c7SWei Liu uint32_t f_bsize; 338460ce86c7SWei Liu uint64_t f_blocks; 338560ce86c7SWei Liu uint64_t f_bfree; 338660ce86c7SWei Liu uint64_t f_bavail; 338760ce86c7SWei Liu uint64_t f_files; 338860ce86c7SWei Liu uint64_t f_ffree; 338960ce86c7SWei Liu uint64_t fsid_val; 339060ce86c7SWei Liu uint32_t f_namelen; 339160ce86c7SWei Liu size_t offset = 7; 339260ce86c7SWei Liu int32_t bsize_factor; 339360ce86c7SWei Liu 339460ce86c7SWei Liu /* 339560ce86c7SWei Liu * compute bsize factor based on host file system block size 339660ce86c7SWei Liu * and client msize 339760ce86c7SWei Liu */ 339860ce86c7SWei Liu bsize_factor = (s->msize - P9_IOHDRSZ)/stbuf->f_bsize; 339960ce86c7SWei Liu if (!bsize_factor) { 340060ce86c7SWei Liu bsize_factor = 1; 340160ce86c7SWei Liu } 340260ce86c7SWei Liu f_type = stbuf->f_type; 340360ce86c7SWei Liu f_bsize = stbuf->f_bsize; 340460ce86c7SWei Liu f_bsize *= bsize_factor; 340560ce86c7SWei Liu /* 340660ce86c7SWei Liu * f_bsize is adjusted(multiplied) by bsize factor, so we need to 340760ce86c7SWei Liu * adjust(divide) the number of blocks, free blocks and available 340860ce86c7SWei Liu * blocks by bsize factor 340960ce86c7SWei Liu */ 341060ce86c7SWei Liu f_blocks = stbuf->f_blocks/bsize_factor; 341160ce86c7SWei Liu f_bfree = stbuf->f_bfree/bsize_factor; 341260ce86c7SWei Liu f_bavail = stbuf->f_bavail/bsize_factor; 341360ce86c7SWei Liu f_files = stbuf->f_files; 341460ce86c7SWei Liu f_ffree = stbuf->f_ffree; 341560ce86c7SWei Liu fsid_val = (unsigned int) stbuf->f_fsid.__val[0] | 341660ce86c7SWei Liu (unsigned long long)stbuf->f_fsid.__val[1] << 32; 341760ce86c7SWei Liu f_namelen = stbuf->f_namelen; 341860ce86c7SWei Liu 341960ce86c7SWei Liu return pdu_marshal(pdu, offset, "ddqqqqqqd", 342060ce86c7SWei Liu f_type, f_bsize, f_blocks, f_bfree, 342160ce86c7SWei Liu f_bavail, f_files, f_ffree, 342260ce86c7SWei Liu fsid_val, f_namelen); 342360ce86c7SWei Liu } 342460ce86c7SWei Liu 34258440e22eSGreg Kurz static void coroutine_fn v9fs_statfs(void *opaque) 342660ce86c7SWei Liu { 342760ce86c7SWei Liu int32_t fid; 342860ce86c7SWei Liu ssize_t retval = 0; 342960ce86c7SWei Liu size_t offset = 7; 343060ce86c7SWei Liu V9fsFidState *fidp; 343160ce86c7SWei Liu struct statfs stbuf; 343260ce86c7SWei Liu V9fsPDU *pdu = opaque; 343360ce86c7SWei Liu V9fsState *s = pdu->s; 343460ce86c7SWei Liu 343560ce86c7SWei Liu retval = pdu_unmarshal(pdu, offset, "d", &fid); 343660ce86c7SWei Liu if (retval < 0) { 343760ce86c7SWei Liu goto out_nofid; 343860ce86c7SWei Liu } 343960ce86c7SWei Liu fidp = get_fid(pdu, fid); 344060ce86c7SWei Liu if (fidp == NULL) { 344160ce86c7SWei Liu retval = -ENOENT; 344260ce86c7SWei Liu goto out_nofid; 344360ce86c7SWei Liu } 344460ce86c7SWei Liu retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf); 344560ce86c7SWei Liu if (retval < 0) { 344660ce86c7SWei Liu goto out; 344760ce86c7SWei Liu } 344860ce86c7SWei Liu retval = v9fs_fill_statfs(s, pdu, &stbuf); 344960ce86c7SWei Liu if (retval < 0) { 345060ce86c7SWei Liu goto out; 345160ce86c7SWei Liu } 345260ce86c7SWei Liu retval += offset; 345360ce86c7SWei Liu out: 345460ce86c7SWei Liu put_fid(pdu, fidp); 345560ce86c7SWei Liu out_nofid: 345660ce86c7SWei Liu pdu_complete(pdu, retval); 345760ce86c7SWei Liu } 345860ce86c7SWei Liu 34598440e22eSGreg Kurz static void coroutine_fn v9fs_mknod(void *opaque) 346060ce86c7SWei Liu { 346160ce86c7SWei Liu 346260ce86c7SWei Liu int mode; 346360ce86c7SWei Liu gid_t gid; 346460ce86c7SWei Liu int32_t fid; 346560ce86c7SWei Liu V9fsQID qid; 346660ce86c7SWei Liu int err = 0; 346760ce86c7SWei Liu int major, minor; 346860ce86c7SWei Liu size_t offset = 7; 346960ce86c7SWei Liu V9fsString name; 347060ce86c7SWei Liu struct stat stbuf; 347160ce86c7SWei Liu V9fsFidState *fidp; 347260ce86c7SWei Liu V9fsPDU *pdu = opaque; 347360ce86c7SWei Liu 347460ce86c7SWei Liu v9fs_string_init(&name); 347560ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode, 347660ce86c7SWei Liu &major, &minor, &gid); 347760ce86c7SWei Liu if (err < 0) { 347860ce86c7SWei Liu goto out_nofid; 347960ce86c7SWei Liu } 348060ce86c7SWei Liu trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor); 348160ce86c7SWei Liu 3482fff39a7aSGreg Kurz if (name_is_illegal(name.data)) { 3483fff39a7aSGreg Kurz err = -ENOENT; 3484fff39a7aSGreg Kurz goto out_nofid; 3485fff39a7aSGreg Kurz } 3486fff39a7aSGreg Kurz 3487805b5d98SGreg Kurz if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 3488805b5d98SGreg Kurz err = -EEXIST; 3489805b5d98SGreg Kurz goto out_nofid; 3490805b5d98SGreg Kurz } 3491805b5d98SGreg Kurz 349260ce86c7SWei Liu fidp = get_fid(pdu, fid); 349360ce86c7SWei Liu if (fidp == NULL) { 349460ce86c7SWei Liu err = -ENOENT; 349560ce86c7SWei Liu goto out_nofid; 349660ce86c7SWei Liu } 349760ce86c7SWei Liu err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid, 349860ce86c7SWei Liu makedev(major, minor), mode, &stbuf); 349960ce86c7SWei Liu if (err < 0) { 350060ce86c7SWei Liu goto out; 350160ce86c7SWei Liu } 35023b5ee9e8SAntonios Motakis err = stat_to_qid(pdu, &stbuf, &qid); 35033b5ee9e8SAntonios Motakis if (err < 0) { 35043b5ee9e8SAntonios Motakis goto out; 35053b5ee9e8SAntonios Motakis } 350660ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Q", &qid); 350760ce86c7SWei Liu if (err < 0) { 350860ce86c7SWei Liu goto out; 350960ce86c7SWei Liu } 351060ce86c7SWei Liu err += offset; 351160ce86c7SWei Liu trace_v9fs_mknod_return(pdu->tag, pdu->id, 351260ce86c7SWei Liu qid.type, qid.version, qid.path); 351360ce86c7SWei Liu out: 351460ce86c7SWei Liu put_fid(pdu, fidp); 351560ce86c7SWei Liu out_nofid: 351660ce86c7SWei Liu pdu_complete(pdu, err); 351760ce86c7SWei Liu v9fs_string_free(&name); 351860ce86c7SWei Liu } 351960ce86c7SWei Liu 352060ce86c7SWei Liu /* 352160ce86c7SWei Liu * Implement posix byte range locking code 352260ce86c7SWei Liu * Server side handling of locking code is very simple, because 9p server in 352360ce86c7SWei Liu * QEMU can handle only one client. And most of the lock handling 352460ce86c7SWei Liu * (like conflict, merging) etc is done by the VFS layer itself, so no need to 352560ce86c7SWei Liu * do any thing in * qemu 9p server side lock code path. 352660ce86c7SWei Liu * So when a TLOCK request comes, always return success 352760ce86c7SWei Liu */ 35288440e22eSGreg Kurz static void coroutine_fn v9fs_lock(void *opaque) 352960ce86c7SWei Liu { 353060ce86c7SWei Liu V9fsFlock flock; 353160ce86c7SWei Liu size_t offset = 7; 353260ce86c7SWei Liu struct stat stbuf; 353360ce86c7SWei Liu V9fsFidState *fidp; 353460ce86c7SWei Liu int32_t fid, err = 0; 353560ce86c7SWei Liu V9fsPDU *pdu = opaque; 353660ce86c7SWei Liu 353760ce86c7SWei Liu v9fs_string_init(&flock.client_id); 353860ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type, 353960ce86c7SWei Liu &flock.flags, &flock.start, &flock.length, 354060ce86c7SWei Liu &flock.proc_id, &flock.client_id); 354160ce86c7SWei Liu if (err < 0) { 354260ce86c7SWei Liu goto out_nofid; 354360ce86c7SWei Liu } 354460ce86c7SWei Liu trace_v9fs_lock(pdu->tag, pdu->id, fid, 354560ce86c7SWei Liu flock.type, flock.start, flock.length); 354660ce86c7SWei Liu 354760ce86c7SWei Liu 354860ce86c7SWei Liu /* We support only block flag now (that too ignored currently) */ 354960ce86c7SWei Liu if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) { 355060ce86c7SWei Liu err = -EINVAL; 355160ce86c7SWei Liu goto out_nofid; 355260ce86c7SWei Liu } 355360ce86c7SWei Liu fidp = get_fid(pdu, fid); 355460ce86c7SWei Liu if (fidp == NULL) { 355560ce86c7SWei Liu err = -ENOENT; 355660ce86c7SWei Liu goto out_nofid; 355760ce86c7SWei Liu } 355860ce86c7SWei Liu err = v9fs_co_fstat(pdu, fidp, &stbuf); 355960ce86c7SWei Liu if (err < 0) { 356060ce86c7SWei Liu goto out; 356160ce86c7SWei Liu } 35624bae2b39SPaolo Bonzini err = pdu_marshal(pdu, offset, "b", P9_LOCK_SUCCESS); 35634bae2b39SPaolo Bonzini if (err < 0) { 35644bae2b39SPaolo Bonzini goto out; 35654bae2b39SPaolo Bonzini } 35664bae2b39SPaolo Bonzini err += offset; 35674bae2b39SPaolo Bonzini trace_v9fs_lock_return(pdu->tag, pdu->id, P9_LOCK_SUCCESS); 356860ce86c7SWei Liu out: 356960ce86c7SWei Liu put_fid(pdu, fidp); 357060ce86c7SWei Liu out_nofid: 357160ce86c7SWei Liu pdu_complete(pdu, err); 357260ce86c7SWei Liu v9fs_string_free(&flock.client_id); 357360ce86c7SWei Liu } 357460ce86c7SWei Liu 357560ce86c7SWei Liu /* 357660ce86c7SWei Liu * When a TGETLOCK request comes, always return success because all lock 357760ce86c7SWei Liu * handling is done by client's VFS layer. 357860ce86c7SWei Liu */ 35798440e22eSGreg Kurz static void coroutine_fn v9fs_getlock(void *opaque) 358060ce86c7SWei Liu { 358160ce86c7SWei Liu size_t offset = 7; 358260ce86c7SWei Liu struct stat stbuf; 358360ce86c7SWei Liu V9fsFidState *fidp; 358460ce86c7SWei Liu V9fsGetlock glock; 358560ce86c7SWei Liu int32_t fid, err = 0; 358660ce86c7SWei Liu V9fsPDU *pdu = opaque; 358760ce86c7SWei Liu 358860ce86c7SWei Liu v9fs_string_init(&glock.client_id); 358960ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type, 359060ce86c7SWei Liu &glock.start, &glock.length, &glock.proc_id, 359160ce86c7SWei Liu &glock.client_id); 359260ce86c7SWei Liu if (err < 0) { 359360ce86c7SWei Liu goto out_nofid; 359460ce86c7SWei Liu } 359560ce86c7SWei Liu trace_v9fs_getlock(pdu->tag, pdu->id, fid, 359660ce86c7SWei Liu glock.type, glock.start, glock.length); 359760ce86c7SWei Liu 359860ce86c7SWei Liu fidp = get_fid(pdu, fid); 359960ce86c7SWei Liu if (fidp == NULL) { 360060ce86c7SWei Liu err = -ENOENT; 360160ce86c7SWei Liu goto out_nofid; 360260ce86c7SWei Liu } 360360ce86c7SWei Liu err = v9fs_co_fstat(pdu, fidp, &stbuf); 360460ce86c7SWei Liu if (err < 0) { 360560ce86c7SWei Liu goto out; 360660ce86c7SWei Liu } 360760ce86c7SWei Liu glock.type = P9_LOCK_TYPE_UNLCK; 360860ce86c7SWei Liu err = pdu_marshal(pdu, offset, "bqqds", glock.type, 360960ce86c7SWei Liu glock.start, glock.length, glock.proc_id, 361060ce86c7SWei Liu &glock.client_id); 361160ce86c7SWei Liu if (err < 0) { 361260ce86c7SWei Liu goto out; 361360ce86c7SWei Liu } 361460ce86c7SWei Liu err += offset; 361560ce86c7SWei Liu trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start, 361660ce86c7SWei Liu glock.length, glock.proc_id); 361760ce86c7SWei Liu out: 361860ce86c7SWei Liu put_fid(pdu, fidp); 361960ce86c7SWei Liu out_nofid: 362060ce86c7SWei Liu pdu_complete(pdu, err); 362160ce86c7SWei Liu v9fs_string_free(&glock.client_id); 362260ce86c7SWei Liu } 362360ce86c7SWei Liu 36248440e22eSGreg Kurz static void coroutine_fn v9fs_mkdir(void *opaque) 362560ce86c7SWei Liu { 362660ce86c7SWei Liu V9fsPDU *pdu = opaque; 362760ce86c7SWei Liu size_t offset = 7; 362860ce86c7SWei Liu int32_t fid; 362960ce86c7SWei Liu struct stat stbuf; 363060ce86c7SWei Liu V9fsQID qid; 363160ce86c7SWei Liu V9fsString name; 363260ce86c7SWei Liu V9fsFidState *fidp; 363360ce86c7SWei Liu gid_t gid; 363460ce86c7SWei Liu int mode; 363560ce86c7SWei Liu int err = 0; 363660ce86c7SWei Liu 363760ce86c7SWei Liu v9fs_string_init(&name); 363860ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid); 363960ce86c7SWei Liu if (err < 0) { 364060ce86c7SWei Liu goto out_nofid; 364160ce86c7SWei Liu } 364260ce86c7SWei Liu trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid); 364360ce86c7SWei Liu 3644fff39a7aSGreg Kurz if (name_is_illegal(name.data)) { 3645fff39a7aSGreg Kurz err = -ENOENT; 3646fff39a7aSGreg Kurz goto out_nofid; 3647fff39a7aSGreg Kurz } 3648fff39a7aSGreg Kurz 3649805b5d98SGreg Kurz if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 3650805b5d98SGreg Kurz err = -EEXIST; 3651805b5d98SGreg Kurz goto out_nofid; 3652805b5d98SGreg Kurz } 3653805b5d98SGreg Kurz 365460ce86c7SWei Liu fidp = get_fid(pdu, fid); 365560ce86c7SWei Liu if (fidp == NULL) { 365660ce86c7SWei Liu err = -ENOENT; 365760ce86c7SWei Liu goto out_nofid; 365860ce86c7SWei Liu } 365960ce86c7SWei Liu err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf); 366060ce86c7SWei Liu if (err < 0) { 366160ce86c7SWei Liu goto out; 366260ce86c7SWei Liu } 36633b5ee9e8SAntonios Motakis err = stat_to_qid(pdu, &stbuf, &qid); 36643b5ee9e8SAntonios Motakis if (err < 0) { 36653b5ee9e8SAntonios Motakis goto out; 36663b5ee9e8SAntonios Motakis } 366760ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Q", &qid); 366860ce86c7SWei Liu if (err < 0) { 366960ce86c7SWei Liu goto out; 367060ce86c7SWei Liu } 367160ce86c7SWei Liu err += offset; 367260ce86c7SWei Liu trace_v9fs_mkdir_return(pdu->tag, pdu->id, 367360ce86c7SWei Liu qid.type, qid.version, qid.path, err); 367460ce86c7SWei Liu out: 367560ce86c7SWei Liu put_fid(pdu, fidp); 367660ce86c7SWei Liu out_nofid: 367760ce86c7SWei Liu pdu_complete(pdu, err); 367860ce86c7SWei Liu v9fs_string_free(&name); 367960ce86c7SWei Liu } 368060ce86c7SWei Liu 36818440e22eSGreg Kurz static void coroutine_fn v9fs_xattrwalk(void *opaque) 368260ce86c7SWei Liu { 368360ce86c7SWei Liu int64_t size; 368460ce86c7SWei Liu V9fsString name; 368560ce86c7SWei Liu ssize_t err = 0; 368660ce86c7SWei Liu size_t offset = 7; 368760ce86c7SWei Liu int32_t fid, newfid; 368860ce86c7SWei Liu V9fsFidState *file_fidp; 368960ce86c7SWei Liu V9fsFidState *xattr_fidp = NULL; 369060ce86c7SWei Liu V9fsPDU *pdu = opaque; 369160ce86c7SWei Liu V9fsState *s = pdu->s; 369260ce86c7SWei Liu 369360ce86c7SWei Liu v9fs_string_init(&name); 369460ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name); 369560ce86c7SWei Liu if (err < 0) { 369660ce86c7SWei Liu goto out_nofid; 369760ce86c7SWei Liu } 369860ce86c7SWei Liu trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data); 369960ce86c7SWei Liu 370060ce86c7SWei Liu file_fidp = get_fid(pdu, fid); 370160ce86c7SWei Liu if (file_fidp == NULL) { 370260ce86c7SWei Liu err = -ENOENT; 370360ce86c7SWei Liu goto out_nofid; 370460ce86c7SWei Liu } 370560ce86c7SWei Liu xattr_fidp = alloc_fid(s, newfid); 370660ce86c7SWei Liu if (xattr_fidp == NULL) { 370760ce86c7SWei Liu err = -EINVAL; 370860ce86c7SWei Liu goto out; 370960ce86c7SWei Liu } 371060ce86c7SWei Liu v9fs_path_copy(&xattr_fidp->path, &file_fidp->path); 3711ba42ebb8SLi Qiang if (!v9fs_string_size(&name)) { 371260ce86c7SWei Liu /* 371360ce86c7SWei Liu * listxattr request. Get the size first 371460ce86c7SWei Liu */ 371560ce86c7SWei Liu size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0); 371660ce86c7SWei Liu if (size < 0) { 371760ce86c7SWei Liu err = size; 371860ce86c7SWei Liu clunk_fid(s, xattr_fidp->fid); 371960ce86c7SWei Liu goto out; 372060ce86c7SWei Liu } 372160ce86c7SWei Liu /* 372260ce86c7SWei Liu * Read the xattr value 372360ce86c7SWei Liu */ 372460ce86c7SWei Liu xattr_fidp->fs.xattr.len = size; 372560ce86c7SWei Liu xattr_fidp->fid_type = P9_FID_XATTR; 3726dd28fbbcSLi Qiang xattr_fidp->fs.xattr.xattrwalk_fid = true; 37277bd92756SPrasad J Pandit xattr_fidp->fs.xattr.value = g_malloc0(size); 3728a647502cSKeno Fischer if (size) { 372960ce86c7SWei Liu err = v9fs_co_llistxattr(pdu, &xattr_fidp->path, 373060ce86c7SWei Liu xattr_fidp->fs.xattr.value, 373160ce86c7SWei Liu xattr_fidp->fs.xattr.len); 373260ce86c7SWei Liu if (err < 0) { 373360ce86c7SWei Liu clunk_fid(s, xattr_fidp->fid); 373460ce86c7SWei Liu goto out; 373560ce86c7SWei Liu } 373660ce86c7SWei Liu } 373760ce86c7SWei Liu err = pdu_marshal(pdu, offset, "q", size); 373860ce86c7SWei Liu if (err < 0) { 373960ce86c7SWei Liu goto out; 374060ce86c7SWei Liu } 374160ce86c7SWei Liu err += offset; 374260ce86c7SWei Liu } else { 374360ce86c7SWei Liu /* 374460ce86c7SWei Liu * specific xattr fid. We check for xattr 374560ce86c7SWei Liu * presence also collect the xattr size 374660ce86c7SWei Liu */ 374760ce86c7SWei Liu size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path, 374860ce86c7SWei Liu &name, NULL, 0); 374960ce86c7SWei Liu if (size < 0) { 375060ce86c7SWei Liu err = size; 375160ce86c7SWei Liu clunk_fid(s, xattr_fidp->fid); 375260ce86c7SWei Liu goto out; 375360ce86c7SWei Liu } 375460ce86c7SWei Liu /* 375560ce86c7SWei Liu * Read the xattr value 375660ce86c7SWei Liu */ 375760ce86c7SWei Liu xattr_fidp->fs.xattr.len = size; 375860ce86c7SWei Liu xattr_fidp->fid_type = P9_FID_XATTR; 3759dd28fbbcSLi Qiang xattr_fidp->fs.xattr.xattrwalk_fid = true; 37607bd92756SPrasad J Pandit xattr_fidp->fs.xattr.value = g_malloc0(size); 3761a647502cSKeno Fischer if (size) { 376260ce86c7SWei Liu err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path, 376360ce86c7SWei Liu &name, xattr_fidp->fs.xattr.value, 376460ce86c7SWei Liu xattr_fidp->fs.xattr.len); 376560ce86c7SWei Liu if (err < 0) { 376660ce86c7SWei Liu clunk_fid(s, xattr_fidp->fid); 376760ce86c7SWei Liu goto out; 376860ce86c7SWei Liu } 376960ce86c7SWei Liu } 377060ce86c7SWei Liu err = pdu_marshal(pdu, offset, "q", size); 377160ce86c7SWei Liu if (err < 0) { 377260ce86c7SWei Liu goto out; 377360ce86c7SWei Liu } 377460ce86c7SWei Liu err += offset; 377560ce86c7SWei Liu } 377660ce86c7SWei Liu trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size); 377760ce86c7SWei Liu out: 377860ce86c7SWei Liu put_fid(pdu, file_fidp); 377960ce86c7SWei Liu if (xattr_fidp) { 378060ce86c7SWei Liu put_fid(pdu, xattr_fidp); 378160ce86c7SWei Liu } 378260ce86c7SWei Liu out_nofid: 378360ce86c7SWei Liu pdu_complete(pdu, err); 378460ce86c7SWei Liu v9fs_string_free(&name); 378560ce86c7SWei Liu } 378660ce86c7SWei Liu 37878440e22eSGreg Kurz static void coroutine_fn v9fs_xattrcreate(void *opaque) 378860ce86c7SWei Liu { 3789aca6897fSKeno Fischer int flags, rflags = 0; 379060ce86c7SWei Liu int32_t fid; 37913b79ef2cSGreg Kurz uint64_t size; 379260ce86c7SWei Liu ssize_t err = 0; 379360ce86c7SWei Liu V9fsString name; 379460ce86c7SWei Liu size_t offset = 7; 379560ce86c7SWei Liu V9fsFidState *file_fidp; 379660ce86c7SWei Liu V9fsFidState *xattr_fidp; 379760ce86c7SWei Liu V9fsPDU *pdu = opaque; 379860ce86c7SWei Liu 379960ce86c7SWei Liu v9fs_string_init(&name); 380060ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags); 380160ce86c7SWei Liu if (err < 0) { 380260ce86c7SWei Liu goto out_nofid; 380360ce86c7SWei Liu } 380460ce86c7SWei Liu trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags); 380560ce86c7SWei Liu 3806aca6897fSKeno Fischer if (flags & ~(P9_XATTR_CREATE | P9_XATTR_REPLACE)) { 3807aca6897fSKeno Fischer err = -EINVAL; 3808aca6897fSKeno Fischer goto out_nofid; 3809aca6897fSKeno Fischer } 3810aca6897fSKeno Fischer 3811aca6897fSKeno Fischer if (flags & P9_XATTR_CREATE) { 3812aca6897fSKeno Fischer rflags |= XATTR_CREATE; 3813aca6897fSKeno Fischer } 3814aca6897fSKeno Fischer 3815aca6897fSKeno Fischer if (flags & P9_XATTR_REPLACE) { 3816aca6897fSKeno Fischer rflags |= XATTR_REPLACE; 3817aca6897fSKeno Fischer } 3818aca6897fSKeno Fischer 38193b79ef2cSGreg Kurz if (size > XATTR_SIZE_MAX) { 38203b79ef2cSGreg Kurz err = -E2BIG; 38213b79ef2cSGreg Kurz goto out_nofid; 38223b79ef2cSGreg Kurz } 38233b79ef2cSGreg Kurz 382460ce86c7SWei Liu file_fidp = get_fid(pdu, fid); 382560ce86c7SWei Liu if (file_fidp == NULL) { 382660ce86c7SWei Liu err = -EINVAL; 382760ce86c7SWei Liu goto out_nofid; 382860ce86c7SWei Liu } 3829dd654e03SGreg Kurz if (file_fidp->fid_type != P9_FID_NONE) { 3830dd654e03SGreg Kurz err = -EINVAL; 3831dd654e03SGreg Kurz goto out_put_fid; 3832dd654e03SGreg Kurz } 3833dd654e03SGreg Kurz 383460ce86c7SWei Liu /* Make the file fid point to xattr */ 383560ce86c7SWei Liu xattr_fidp = file_fidp; 383660ce86c7SWei Liu xattr_fidp->fid_type = P9_FID_XATTR; 383760ce86c7SWei Liu xattr_fidp->fs.xattr.copied_len = 0; 3838dd28fbbcSLi Qiang xattr_fidp->fs.xattr.xattrwalk_fid = false; 383960ce86c7SWei Liu xattr_fidp->fs.xattr.len = size; 3840aca6897fSKeno Fischer xattr_fidp->fs.xattr.flags = rflags; 384160ce86c7SWei Liu v9fs_string_init(&xattr_fidp->fs.xattr.name); 384260ce86c7SWei Liu v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name); 3843eb687602SLi Qiang xattr_fidp->fs.xattr.value = g_malloc0(size); 384460ce86c7SWei Liu err = offset; 3845dd654e03SGreg Kurz out_put_fid: 384660ce86c7SWei Liu put_fid(pdu, file_fidp); 384760ce86c7SWei Liu out_nofid: 384860ce86c7SWei Liu pdu_complete(pdu, err); 384960ce86c7SWei Liu v9fs_string_free(&name); 385060ce86c7SWei Liu } 385160ce86c7SWei Liu 38528440e22eSGreg Kurz static void coroutine_fn v9fs_readlink(void *opaque) 385360ce86c7SWei Liu { 385460ce86c7SWei Liu V9fsPDU *pdu = opaque; 385560ce86c7SWei Liu size_t offset = 7; 385660ce86c7SWei Liu V9fsString target; 385760ce86c7SWei Liu int32_t fid; 385860ce86c7SWei Liu int err = 0; 385960ce86c7SWei Liu V9fsFidState *fidp; 386060ce86c7SWei Liu 386160ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "d", &fid); 386260ce86c7SWei Liu if (err < 0) { 386360ce86c7SWei Liu goto out_nofid; 386460ce86c7SWei Liu } 386560ce86c7SWei Liu trace_v9fs_readlink(pdu->tag, pdu->id, fid); 386660ce86c7SWei Liu fidp = get_fid(pdu, fid); 386760ce86c7SWei Liu if (fidp == NULL) { 386860ce86c7SWei Liu err = -ENOENT; 386960ce86c7SWei Liu goto out_nofid; 387060ce86c7SWei Liu } 387160ce86c7SWei Liu 387260ce86c7SWei Liu v9fs_string_init(&target); 387360ce86c7SWei Liu err = v9fs_co_readlink(pdu, &fidp->path, &target); 387460ce86c7SWei Liu if (err < 0) { 387560ce86c7SWei Liu goto out; 387660ce86c7SWei Liu } 387760ce86c7SWei Liu err = pdu_marshal(pdu, offset, "s", &target); 387860ce86c7SWei Liu if (err < 0) { 387960ce86c7SWei Liu v9fs_string_free(&target); 388060ce86c7SWei Liu goto out; 388160ce86c7SWei Liu } 388260ce86c7SWei Liu err += offset; 388360ce86c7SWei Liu trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data); 388460ce86c7SWei Liu v9fs_string_free(&target); 388560ce86c7SWei Liu out: 388660ce86c7SWei Liu put_fid(pdu, fidp); 388760ce86c7SWei Liu out_nofid: 388860ce86c7SWei Liu pdu_complete(pdu, err); 388960ce86c7SWei Liu } 389060ce86c7SWei Liu 389160ce86c7SWei Liu static CoroutineEntry *pdu_co_handlers[] = { 389260ce86c7SWei Liu [P9_TREADDIR] = v9fs_readdir, 389360ce86c7SWei Liu [P9_TSTATFS] = v9fs_statfs, 389460ce86c7SWei Liu [P9_TGETATTR] = v9fs_getattr, 389560ce86c7SWei Liu [P9_TSETATTR] = v9fs_setattr, 389660ce86c7SWei Liu [P9_TXATTRWALK] = v9fs_xattrwalk, 389760ce86c7SWei Liu [P9_TXATTRCREATE] = v9fs_xattrcreate, 389860ce86c7SWei Liu [P9_TMKNOD] = v9fs_mknod, 389960ce86c7SWei Liu [P9_TRENAME] = v9fs_rename, 390060ce86c7SWei Liu [P9_TLOCK] = v9fs_lock, 390160ce86c7SWei Liu [P9_TGETLOCK] = v9fs_getlock, 390260ce86c7SWei Liu [P9_TRENAMEAT] = v9fs_renameat, 390360ce86c7SWei Liu [P9_TREADLINK] = v9fs_readlink, 390460ce86c7SWei Liu [P9_TUNLINKAT] = v9fs_unlinkat, 390560ce86c7SWei Liu [P9_TMKDIR] = v9fs_mkdir, 390660ce86c7SWei Liu [P9_TVERSION] = v9fs_version, 390760ce86c7SWei Liu [P9_TLOPEN] = v9fs_open, 390860ce86c7SWei Liu [P9_TATTACH] = v9fs_attach, 390960ce86c7SWei Liu [P9_TSTAT] = v9fs_stat, 391060ce86c7SWei Liu [P9_TWALK] = v9fs_walk, 391160ce86c7SWei Liu [P9_TCLUNK] = v9fs_clunk, 391260ce86c7SWei Liu [P9_TFSYNC] = v9fs_fsync, 391360ce86c7SWei Liu [P9_TOPEN] = v9fs_open, 391460ce86c7SWei Liu [P9_TREAD] = v9fs_read, 391560ce86c7SWei Liu #if 0 391660ce86c7SWei Liu [P9_TAUTH] = v9fs_auth, 391760ce86c7SWei Liu #endif 391860ce86c7SWei Liu [P9_TFLUSH] = v9fs_flush, 391960ce86c7SWei Liu [P9_TLINK] = v9fs_link, 392060ce86c7SWei Liu [P9_TSYMLINK] = v9fs_symlink, 392160ce86c7SWei Liu [P9_TCREATE] = v9fs_create, 392260ce86c7SWei Liu [P9_TLCREATE] = v9fs_lcreate, 392360ce86c7SWei Liu [P9_TWRITE] = v9fs_write, 392460ce86c7SWei Liu [P9_TWSTAT] = v9fs_wstat, 392560ce86c7SWei Liu [P9_TREMOVE] = v9fs_remove, 392660ce86c7SWei Liu }; 392760ce86c7SWei Liu 39288440e22eSGreg Kurz static void coroutine_fn v9fs_op_not_supp(void *opaque) 392960ce86c7SWei Liu { 393060ce86c7SWei Liu V9fsPDU *pdu = opaque; 393160ce86c7SWei Liu pdu_complete(pdu, -EOPNOTSUPP); 393260ce86c7SWei Liu } 393360ce86c7SWei Liu 39348440e22eSGreg Kurz static void coroutine_fn v9fs_fs_ro(void *opaque) 393560ce86c7SWei Liu { 393660ce86c7SWei Liu V9fsPDU *pdu = opaque; 393760ce86c7SWei Liu pdu_complete(pdu, -EROFS); 393860ce86c7SWei Liu } 393960ce86c7SWei Liu 394060ce86c7SWei Liu static inline bool is_read_only_op(V9fsPDU *pdu) 394160ce86c7SWei Liu { 394260ce86c7SWei Liu switch (pdu->id) { 394360ce86c7SWei Liu case P9_TREADDIR: 394460ce86c7SWei Liu case P9_TSTATFS: 394560ce86c7SWei Liu case P9_TGETATTR: 394660ce86c7SWei Liu case P9_TXATTRWALK: 394760ce86c7SWei Liu case P9_TLOCK: 394860ce86c7SWei Liu case P9_TGETLOCK: 394960ce86c7SWei Liu case P9_TREADLINK: 395060ce86c7SWei Liu case P9_TVERSION: 395160ce86c7SWei Liu case P9_TLOPEN: 395260ce86c7SWei Liu case P9_TATTACH: 395360ce86c7SWei Liu case P9_TSTAT: 395460ce86c7SWei Liu case P9_TWALK: 395560ce86c7SWei Liu case P9_TCLUNK: 395660ce86c7SWei Liu case P9_TFSYNC: 395760ce86c7SWei Liu case P9_TOPEN: 395860ce86c7SWei Liu case P9_TREAD: 395960ce86c7SWei Liu case P9_TAUTH: 396060ce86c7SWei Liu case P9_TFLUSH: 396160ce86c7SWei Liu return 1; 396260ce86c7SWei Liu default: 396360ce86c7SWei Liu return 0; 396460ce86c7SWei Liu } 396560ce86c7SWei Liu } 396660ce86c7SWei Liu 3967506f3275SGreg Kurz void pdu_submit(V9fsPDU *pdu, P9MsgHeader *hdr) 396860ce86c7SWei Liu { 396960ce86c7SWei Liu Coroutine *co; 397060ce86c7SWei Liu CoroutineEntry *handler; 397160ce86c7SWei Liu V9fsState *s = pdu->s; 397260ce86c7SWei Liu 3973506f3275SGreg Kurz pdu->size = le32_to_cpu(hdr->size_le); 3974506f3275SGreg Kurz pdu->id = hdr->id; 3975506f3275SGreg Kurz pdu->tag = le16_to_cpu(hdr->tag_le); 3976506f3275SGreg Kurz 397760ce86c7SWei Liu if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) || 397860ce86c7SWei Liu (pdu_co_handlers[pdu->id] == NULL)) { 397960ce86c7SWei Liu handler = v9fs_op_not_supp; 3980d1471233SGreg Kurz } else if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) { 3981d1471233SGreg Kurz handler = v9fs_fs_ro; 398260ce86c7SWei Liu } else { 398360ce86c7SWei Liu handler = pdu_co_handlers[pdu->id]; 398460ce86c7SWei Liu } 398560ce86c7SWei Liu 3986506f3275SGreg Kurz qemu_co_queue_init(&pdu->complete); 39870b8b8753SPaolo Bonzini co = qemu_coroutine_create(handler, pdu); 39880b8b8753SPaolo Bonzini qemu_coroutine_enter(co); 398960ce86c7SWei Liu } 399060ce86c7SWei Liu 39912a0c56aaSWei Liu /* Returns 0 on success, 1 on failure. */ 3992066eb006SGreg Kurz int v9fs_device_realize_common(V9fsState *s, const V9fsTransport *t, 3993066eb006SGreg Kurz Error **errp) 39942a0c56aaSWei Liu { 39952a0c56aaSWei Liu int i, len; 39962a0c56aaSWei Liu struct stat stat; 39972a0c56aaSWei Liu FsDriverEntry *fse; 39982a0c56aaSWei Liu V9fsPath path; 39992a0c56aaSWei Liu int rc = 1; 40002a0c56aaSWei Liu 4001066eb006SGreg Kurz assert(!s->transport); 4002066eb006SGreg Kurz s->transport = t; 4003066eb006SGreg Kurz 40042a0c56aaSWei Liu /* initialize pdu allocator */ 40052a0c56aaSWei Liu QLIST_INIT(&s->free_list); 40062a0c56aaSWei Liu QLIST_INIT(&s->active_list); 40070d78289cSGreg Kurz for (i = 0; i < MAX_REQ; i++) { 4008583f21f8SStefano Stabellini QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next); 4009583f21f8SStefano Stabellini s->pdus[i].s = s; 4010583f21f8SStefano Stabellini s->pdus[i].idx = i; 40112a0c56aaSWei Liu } 40122a0c56aaSWei Liu 40132a0c56aaSWei Liu v9fs_path_init(&path); 40142a0c56aaSWei Liu 40152a0c56aaSWei Liu fse = get_fsdev_fsentry(s->fsconf.fsdev_id); 40162a0c56aaSWei Liu 40172a0c56aaSWei Liu if (!fse) { 40182a0c56aaSWei Liu /* We don't have a fsdev identified by fsdev_id */ 40192a0c56aaSWei Liu error_setg(errp, "9pfs device couldn't find fsdev with the " 40202a0c56aaSWei Liu "id = %s", 40212a0c56aaSWei Liu s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL"); 40222a0c56aaSWei Liu goto out; 40232a0c56aaSWei Liu } 40242a0c56aaSWei Liu 40252a0c56aaSWei Liu if (!s->fsconf.tag) { 40262a0c56aaSWei Liu /* we haven't specified a mount_tag */ 40272a0c56aaSWei Liu error_setg(errp, "fsdev with id %s needs mount_tag arguments", 40282a0c56aaSWei Liu s->fsconf.fsdev_id); 40292a0c56aaSWei Liu goto out; 40302a0c56aaSWei Liu } 40312a0c56aaSWei Liu 40322a0c56aaSWei Liu s->ctx.export_flags = fse->export_flags; 40332a0c56aaSWei Liu s->ctx.fs_root = g_strdup(fse->path); 40342a0c56aaSWei Liu s->ctx.exops.get_st_gen = NULL; 40352a0c56aaSWei Liu len = strlen(s->fsconf.tag); 40362a0c56aaSWei Liu if (len > MAX_TAG_LEN - 1) { 40372a0c56aaSWei Liu error_setg(errp, "mount tag '%s' (%d bytes) is longer than " 40382a0c56aaSWei Liu "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1); 40392a0c56aaSWei Liu goto out; 40402a0c56aaSWei Liu } 40412a0c56aaSWei Liu 40422a0c56aaSWei Liu s->tag = g_strdup(s->fsconf.tag); 40432a0c56aaSWei Liu s->ctx.uid = -1; 40442a0c56aaSWei Liu 40452a0c56aaSWei Liu s->ops = fse->ops; 40462a0c56aaSWei Liu 4047b96feb2cSTobias Schramm s->ctx.fmode = fse->fmode; 4048b96feb2cSTobias Schramm s->ctx.dmode = fse->dmode; 4049b96feb2cSTobias Schramm 40502a0c56aaSWei Liu s->fid_list = NULL; 40512a0c56aaSWei Liu qemu_co_rwlock_init(&s->rename_lock); 40522a0c56aaSWei Liu 405365603a80SGreg Kurz if (s->ops->init(&s->ctx, errp) < 0) { 405465603a80SGreg Kurz error_prepend(errp, "cannot initialize fsdev '%s': ", 405565603a80SGreg Kurz s->fsconf.fsdev_id); 40562a0c56aaSWei Liu goto out; 40572a0c56aaSWei Liu } 40582a0c56aaSWei Liu 40592a0c56aaSWei Liu /* 40602a0c56aaSWei Liu * Check details of export path, We need to use fs driver 40612a0c56aaSWei Liu * call back to do that. Since we are in the init path, we don't 40622a0c56aaSWei Liu * use co-routines here. 40632a0c56aaSWei Liu */ 40642a0c56aaSWei Liu if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) { 40652a0c56aaSWei Liu error_setg(errp, 40662a0c56aaSWei Liu "error in converting name to path %s", strerror(errno)); 40672a0c56aaSWei Liu goto out; 40682a0c56aaSWei Liu } 40692a0c56aaSWei Liu if (s->ops->lstat(&s->ctx, &path, &stat)) { 40702a0c56aaSWei Liu error_setg(errp, "share path %s does not exist", fse->path); 40712a0c56aaSWei Liu goto out; 40722a0c56aaSWei Liu } else if (!S_ISDIR(stat.st_mode)) { 40732a0c56aaSWei Liu error_setg(errp, "share path %s is not a directory", fse->path); 40742a0c56aaSWei Liu goto out; 40752a0c56aaSWei Liu } 4076b8bbdb88SPradeep Jagadeesh 40773b5ee9e8SAntonios Motakis s->dev_id = stat.st_dev; 40783b5ee9e8SAntonios Motakis 4079*6b6aa828SChristian Schoenebeck /* init inode remapping : */ 4080*6b6aa828SChristian Schoenebeck /* hash table for variable length inode suffixes */ 4081*6b6aa828SChristian Schoenebeck qpd_table_init(&s->qpd_table); 4082*6b6aa828SChristian Schoenebeck /* hash table for slow/full inode remapping (most users won't need it) */ 4083*6b6aa828SChristian Schoenebeck qpf_table_init(&s->qpf_table); 4084*6b6aa828SChristian Schoenebeck /* hash table for quick inode remapping */ 40851a6ed33cSAntonios Motakis qpp_table_init(&s->qpp_table); 4086*6b6aa828SChristian Schoenebeck s->qp_ndevices = 0; 4087*6b6aa828SChristian Schoenebeck s->qp_affix_next = 1; /* reserve 0 to detect overflow */ 4088f3fe4a2dSAntonios Motakis s->qp_fullpath_next = 1; 40891a6ed33cSAntonios Motakis 4090b8bbdb88SPradeep Jagadeesh s->ctx.fst = &fse->fst; 4091b8bbdb88SPradeep Jagadeesh fsdev_throttle_init(s->ctx.fst); 4092b8bbdb88SPradeep Jagadeesh 40932a0c56aaSWei Liu rc = 0; 40942a0c56aaSWei Liu out: 40952a0c56aaSWei Liu if (rc) { 4096c0da0cb7SGreg Kurz v9fs_device_unrealize_common(s, NULL); 4097702dbcc2SLi Qiang } 40982a0c56aaSWei Liu v9fs_path_free(&path); 40992a0c56aaSWei Liu return rc; 41002a0c56aaSWei Liu } 41012a0c56aaSWei Liu 41022a0c56aaSWei Liu void v9fs_device_unrealize_common(V9fsState *s, Error **errp) 41032a0c56aaSWei Liu { 4104c0da0cb7SGreg Kurz if (s->ops && s->ops->cleanup) { 4105702dbcc2SLi Qiang s->ops->cleanup(&s->ctx); 4106702dbcc2SLi Qiang } 4107c0da0cb7SGreg Kurz if (s->ctx.fst) { 4108b8bbdb88SPradeep Jagadeesh fsdev_throttle_cleanup(s->ctx.fst); 4109c0da0cb7SGreg Kurz } 41102a0c56aaSWei Liu g_free(s->tag); 4111*6b6aa828SChristian Schoenebeck qp_table_destroy(&s->qpd_table); 4112f3fe4a2dSAntonios Motakis qp_table_destroy(&s->qpp_table); 4113f3fe4a2dSAntonios Motakis qp_table_destroy(&s->qpf_table); 41144774718eSLi Qiang g_free(s->ctx.fs_root); 41152a0c56aaSWei Liu } 41162a0c56aaSWei Liu 41170e44a0fdSGreg Kurz typedef struct VirtfsCoResetData { 41180e44a0fdSGreg Kurz V9fsPDU pdu; 41190e44a0fdSGreg Kurz bool done; 41200e44a0fdSGreg Kurz } VirtfsCoResetData; 41210e44a0fdSGreg Kurz 41220e44a0fdSGreg Kurz static void coroutine_fn virtfs_co_reset(void *opaque) 41230e44a0fdSGreg Kurz { 41240e44a0fdSGreg Kurz VirtfsCoResetData *data = opaque; 41250e44a0fdSGreg Kurz 41260e44a0fdSGreg Kurz virtfs_reset(&data->pdu); 41270e44a0fdSGreg Kurz data->done = true; 41280e44a0fdSGreg Kurz } 41290e44a0fdSGreg Kurz 41300e44a0fdSGreg Kurz void v9fs_reset(V9fsState *s) 41310e44a0fdSGreg Kurz { 41320e44a0fdSGreg Kurz VirtfsCoResetData data = { .pdu = { .s = s }, .done = false }; 41330e44a0fdSGreg Kurz Coroutine *co; 41340e44a0fdSGreg Kurz 41350e44a0fdSGreg Kurz while (!QLIST_EMPTY(&s->active_list)) { 41360e44a0fdSGreg Kurz aio_poll(qemu_get_aio_context(), true); 41370e44a0fdSGreg Kurz } 41380e44a0fdSGreg Kurz 41390e44a0fdSGreg Kurz co = qemu_coroutine_create(virtfs_co_reset, &data); 41400e44a0fdSGreg Kurz qemu_coroutine_enter(co); 41410e44a0fdSGreg Kurz 41420e44a0fdSGreg Kurz while (!data.done) { 41430e44a0fdSGreg Kurz aio_poll(qemu_get_aio_context(), true); 41440e44a0fdSGreg Kurz } 41450e44a0fdSGreg Kurz } 41460e44a0fdSGreg Kurz 414760ce86c7SWei Liu static void __attribute__((__constructor__)) v9fs_set_fd_limit(void) 414860ce86c7SWei Liu { 414960ce86c7SWei Liu struct rlimit rlim; 415060ce86c7SWei Liu if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) { 415163325b18SGreg Kurz error_report("Failed to get the resource limit"); 415260ce86c7SWei Liu exit(1); 415360ce86c7SWei Liu } 415460ce86c7SWei Liu open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur/3); 415560ce86c7SWei Liu open_fd_rc = rlim.rlim_cur/2; 415660ce86c7SWei Liu } 4157