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" 3060ce86c7SWei Liu 3160ce86c7SWei Liu int open_fd_hw; 3260ce86c7SWei Liu int total_open_fd; 3360ce86c7SWei Liu static int open_fd_rc; 3460ce86c7SWei Liu 3560ce86c7SWei Liu enum { 3660ce86c7SWei Liu Oread = 0x00, 3760ce86c7SWei Liu Owrite = 0x01, 3860ce86c7SWei Liu Ordwr = 0x02, 3960ce86c7SWei Liu Oexec = 0x03, 4060ce86c7SWei Liu Oexcl = 0x04, 4160ce86c7SWei Liu Otrunc = 0x10, 4260ce86c7SWei Liu Orexec = 0x20, 4360ce86c7SWei Liu Orclose = 0x40, 4460ce86c7SWei Liu Oappend = 0x80, 4560ce86c7SWei Liu }; 4660ce86c7SWei Liu 4775673590SGreg Kurz static ssize_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...) 4860ce86c7SWei Liu { 4960ce86c7SWei Liu ssize_t ret; 5060ce86c7SWei Liu va_list ap; 5160ce86c7SWei Liu 5260ce86c7SWei Liu va_start(ap, fmt); 53ea83441cSStefano Stabellini ret = pdu->s->transport->pdu_vmarshal(pdu, offset, fmt, ap); 5460ce86c7SWei Liu va_end(ap); 5560ce86c7SWei Liu 5660ce86c7SWei Liu return ret; 5760ce86c7SWei Liu } 5860ce86c7SWei Liu 5975673590SGreg Kurz static ssize_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...) 6060ce86c7SWei Liu { 6160ce86c7SWei Liu ssize_t ret; 6260ce86c7SWei Liu va_list ap; 6360ce86c7SWei Liu 6460ce86c7SWei Liu va_start(ap, fmt); 65ea83441cSStefano Stabellini ret = pdu->s->transport->pdu_vunmarshal(pdu, offset, fmt, ap); 6660ce86c7SWei Liu va_end(ap); 6760ce86c7SWei Liu 6860ce86c7SWei Liu return ret; 6960ce86c7SWei Liu } 7060ce86c7SWei Liu 7160ce86c7SWei Liu static int omode_to_uflags(int8_t mode) 7260ce86c7SWei Liu { 7360ce86c7SWei Liu int ret = 0; 7460ce86c7SWei Liu 7560ce86c7SWei Liu switch (mode & 3) { 7660ce86c7SWei Liu case Oread: 7760ce86c7SWei Liu ret = O_RDONLY; 7860ce86c7SWei Liu break; 7960ce86c7SWei Liu case Ordwr: 8060ce86c7SWei Liu ret = O_RDWR; 8160ce86c7SWei Liu break; 8260ce86c7SWei Liu case Owrite: 8360ce86c7SWei Liu ret = O_WRONLY; 8460ce86c7SWei Liu break; 8560ce86c7SWei Liu case Oexec: 8660ce86c7SWei Liu ret = O_RDONLY; 8760ce86c7SWei Liu break; 8860ce86c7SWei Liu } 8960ce86c7SWei Liu 9060ce86c7SWei Liu if (mode & Otrunc) { 9160ce86c7SWei Liu ret |= O_TRUNC; 9260ce86c7SWei Liu } 9360ce86c7SWei Liu 9460ce86c7SWei Liu if (mode & Oappend) { 9560ce86c7SWei Liu ret |= O_APPEND; 9660ce86c7SWei Liu } 9760ce86c7SWei Liu 9860ce86c7SWei Liu if (mode & Oexcl) { 9960ce86c7SWei Liu ret |= O_EXCL; 10060ce86c7SWei Liu } 10160ce86c7SWei Liu 10260ce86c7SWei Liu return ret; 10360ce86c7SWei Liu } 10460ce86c7SWei Liu 1058e71b96cSGreg Kurz typedef struct DotlOpenflagMap { 10660ce86c7SWei Liu int dotl_flag; 10760ce86c7SWei Liu int open_flag; 1088e71b96cSGreg Kurz } DotlOpenflagMap; 10960ce86c7SWei Liu 11060ce86c7SWei Liu static int dotl_to_open_flags(int flags) 11160ce86c7SWei Liu { 11260ce86c7SWei Liu int i; 11360ce86c7SWei Liu /* 11460ce86c7SWei Liu * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY 11560ce86c7SWei Liu * and P9_DOTL_NOACCESS 11660ce86c7SWei Liu */ 11760ce86c7SWei Liu int oflags = flags & O_ACCMODE; 11860ce86c7SWei Liu 1198e71b96cSGreg Kurz DotlOpenflagMap dotl_oflag_map[] = { 12060ce86c7SWei Liu { P9_DOTL_CREATE, O_CREAT }, 12160ce86c7SWei Liu { P9_DOTL_EXCL, O_EXCL }, 12260ce86c7SWei Liu { P9_DOTL_NOCTTY , O_NOCTTY }, 12360ce86c7SWei Liu { P9_DOTL_TRUNC, O_TRUNC }, 12460ce86c7SWei Liu { P9_DOTL_APPEND, O_APPEND }, 12560ce86c7SWei Liu { P9_DOTL_NONBLOCK, O_NONBLOCK } , 12660ce86c7SWei Liu { P9_DOTL_DSYNC, O_DSYNC }, 12760ce86c7SWei Liu { P9_DOTL_FASYNC, FASYNC }, 12860ce86c7SWei Liu { P9_DOTL_DIRECT, O_DIRECT }, 12960ce86c7SWei Liu { P9_DOTL_LARGEFILE, O_LARGEFILE }, 13060ce86c7SWei Liu { P9_DOTL_DIRECTORY, O_DIRECTORY }, 13160ce86c7SWei Liu { P9_DOTL_NOFOLLOW, O_NOFOLLOW }, 13260ce86c7SWei Liu { P9_DOTL_NOATIME, O_NOATIME }, 13360ce86c7SWei Liu { P9_DOTL_SYNC, O_SYNC }, 13460ce86c7SWei Liu }; 13560ce86c7SWei Liu 13660ce86c7SWei Liu for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) { 13760ce86c7SWei Liu if (flags & dotl_oflag_map[i].dotl_flag) { 13860ce86c7SWei Liu oflags |= dotl_oflag_map[i].open_flag; 13960ce86c7SWei Liu } 14060ce86c7SWei Liu } 14160ce86c7SWei Liu 14260ce86c7SWei Liu return oflags; 14360ce86c7SWei Liu } 14460ce86c7SWei Liu 14560ce86c7SWei Liu void cred_init(FsCred *credp) 14660ce86c7SWei Liu { 14760ce86c7SWei Liu credp->fc_uid = -1; 14860ce86c7SWei Liu credp->fc_gid = -1; 14960ce86c7SWei Liu credp->fc_mode = -1; 15060ce86c7SWei Liu credp->fc_rdev = -1; 15160ce86c7SWei Liu } 15260ce86c7SWei Liu 15360ce86c7SWei Liu static int get_dotl_openflags(V9fsState *s, int oflags) 15460ce86c7SWei Liu { 15560ce86c7SWei Liu int flags; 15660ce86c7SWei Liu /* 15760ce86c7SWei Liu * Filter the client open flags 15860ce86c7SWei Liu */ 15960ce86c7SWei Liu flags = dotl_to_open_flags(oflags); 16060ce86c7SWei Liu flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT); 16160ce86c7SWei Liu /* 16260ce86c7SWei Liu * Ignore direct disk access hint until the server supports it. 16360ce86c7SWei Liu */ 16460ce86c7SWei Liu flags &= ~O_DIRECT; 16560ce86c7SWei Liu return flags; 16660ce86c7SWei Liu } 16760ce86c7SWei Liu 16860ce86c7SWei Liu void v9fs_path_init(V9fsPath *path) 16960ce86c7SWei Liu { 17060ce86c7SWei Liu path->data = NULL; 17160ce86c7SWei Liu path->size = 0; 17260ce86c7SWei Liu } 17360ce86c7SWei Liu 17460ce86c7SWei Liu void v9fs_path_free(V9fsPath *path) 17560ce86c7SWei Liu { 17660ce86c7SWei Liu g_free(path->data); 17760ce86c7SWei Liu path->data = NULL; 17860ce86c7SWei Liu path->size = 0; 17960ce86c7SWei Liu } 18060ce86c7SWei Liu 181e3e83f2eSGreg Kurz 182e3e83f2eSGreg Kurz void GCC_FMT_ATTR(2, 3) 183e3e83f2eSGreg Kurz v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...) 184e3e83f2eSGreg Kurz { 185e3e83f2eSGreg Kurz va_list ap; 186e3e83f2eSGreg Kurz 187e3e83f2eSGreg Kurz v9fs_path_free(path); 188e3e83f2eSGreg Kurz 189e3e83f2eSGreg Kurz va_start(ap, fmt); 190e3e83f2eSGreg Kurz /* Bump the size for including terminating NULL */ 191e3e83f2eSGreg Kurz path->size = g_vasprintf(&path->data, fmt, ap) + 1; 192e3e83f2eSGreg Kurz va_end(ap); 193e3e83f2eSGreg Kurz } 194e3e83f2eSGreg Kurz 195e446a1ebSMarc-André Lureau void v9fs_path_copy(V9fsPath *dst, const V9fsPath *src) 19660ce86c7SWei Liu { 197e446a1ebSMarc-André Lureau v9fs_path_free(dst); 198e446a1ebSMarc-André Lureau dst->size = src->size; 199e446a1ebSMarc-André Lureau dst->data = g_memdup(src->data, src->size); 20060ce86c7SWei Liu } 20160ce86c7SWei Liu 20260ce86c7SWei Liu int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath, 20360ce86c7SWei Liu const char *name, V9fsPath *path) 20460ce86c7SWei Liu { 20560ce86c7SWei Liu int err; 20660ce86c7SWei Liu err = s->ops->name_to_path(&s->ctx, dirpath, name, path); 20760ce86c7SWei Liu if (err < 0) { 20860ce86c7SWei Liu err = -errno; 20960ce86c7SWei Liu } 21060ce86c7SWei Liu return err; 21160ce86c7SWei Liu } 21260ce86c7SWei Liu 21360ce86c7SWei Liu /* 21460ce86c7SWei Liu * Return TRUE if s1 is an ancestor of s2. 21560ce86c7SWei Liu * 21660ce86c7SWei Liu * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d". 21760ce86c7SWei Liu * As a special case, We treat s1 as ancestor of s2 if they are same! 21860ce86c7SWei Liu */ 21960ce86c7SWei Liu static int v9fs_path_is_ancestor(V9fsPath *s1, V9fsPath *s2) 22060ce86c7SWei Liu { 22160ce86c7SWei Liu if (!strncmp(s1->data, s2->data, s1->size - 1)) { 22260ce86c7SWei Liu if (s2->data[s1->size - 1] == '\0' || s2->data[s1->size - 1] == '/') { 22360ce86c7SWei Liu return 1; 22460ce86c7SWei Liu } 22560ce86c7SWei Liu } 22660ce86c7SWei Liu return 0; 22760ce86c7SWei Liu } 22860ce86c7SWei Liu 22960ce86c7SWei Liu static size_t v9fs_string_size(V9fsString *str) 23060ce86c7SWei Liu { 23160ce86c7SWei Liu return str->size; 23260ce86c7SWei Liu } 23360ce86c7SWei Liu 23460ce86c7SWei Liu /* 23560ce86c7SWei Liu * returns 0 if fid got re-opened, 1 if not, < 0 on error */ 2368440e22eSGreg Kurz static int coroutine_fn v9fs_reopen_fid(V9fsPDU *pdu, V9fsFidState *f) 23760ce86c7SWei Liu { 23860ce86c7SWei Liu int err = 1; 23960ce86c7SWei Liu if (f->fid_type == P9_FID_FILE) { 24060ce86c7SWei Liu if (f->fs.fd == -1) { 24160ce86c7SWei Liu do { 24260ce86c7SWei Liu err = v9fs_co_open(pdu, f, f->open_flags); 24360ce86c7SWei Liu } while (err == -EINTR && !pdu->cancelled); 24460ce86c7SWei Liu } 24560ce86c7SWei Liu } else if (f->fid_type == P9_FID_DIR) { 246f314ea4eSGreg Kurz if (f->fs.dir.stream == NULL) { 24760ce86c7SWei Liu do { 24860ce86c7SWei Liu err = v9fs_co_opendir(pdu, f); 24960ce86c7SWei Liu } while (err == -EINTR && !pdu->cancelled); 25060ce86c7SWei Liu } 25160ce86c7SWei Liu } 25260ce86c7SWei Liu return err; 25360ce86c7SWei Liu } 25460ce86c7SWei Liu 2558440e22eSGreg Kurz static V9fsFidState *coroutine_fn get_fid(V9fsPDU *pdu, int32_t fid) 25660ce86c7SWei Liu { 25760ce86c7SWei Liu int err; 25860ce86c7SWei Liu V9fsFidState *f; 25960ce86c7SWei Liu V9fsState *s = pdu->s; 26060ce86c7SWei Liu 26160ce86c7SWei Liu for (f = s->fid_list; f; f = f->next) { 26260ce86c7SWei Liu BUG_ON(f->clunked); 26360ce86c7SWei Liu if (f->fid == fid) { 26460ce86c7SWei Liu /* 26560ce86c7SWei Liu * Update the fid ref upfront so that 26660ce86c7SWei Liu * we don't get reclaimed when we yield 26760ce86c7SWei Liu * in open later. 26860ce86c7SWei Liu */ 26960ce86c7SWei Liu f->ref++; 27060ce86c7SWei Liu /* 27160ce86c7SWei Liu * check whether we need to reopen the 27260ce86c7SWei Liu * file. We might have closed the fd 27360ce86c7SWei Liu * while trying to free up some file 27460ce86c7SWei Liu * descriptors. 27560ce86c7SWei Liu */ 27660ce86c7SWei Liu err = v9fs_reopen_fid(pdu, f); 27760ce86c7SWei Liu if (err < 0) { 27860ce86c7SWei Liu f->ref--; 27960ce86c7SWei Liu return NULL; 28060ce86c7SWei Liu } 28160ce86c7SWei Liu /* 28260ce86c7SWei Liu * Mark the fid as referenced so that the LRU 28360ce86c7SWei Liu * reclaim won't close the file descriptor 28460ce86c7SWei Liu */ 28560ce86c7SWei Liu f->flags |= FID_REFERENCED; 28660ce86c7SWei Liu return f; 28760ce86c7SWei Liu } 28860ce86c7SWei Liu } 28960ce86c7SWei Liu return NULL; 29060ce86c7SWei Liu } 29160ce86c7SWei Liu 29260ce86c7SWei Liu static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid) 29360ce86c7SWei Liu { 29460ce86c7SWei Liu V9fsFidState *f; 29560ce86c7SWei Liu 29660ce86c7SWei Liu for (f = s->fid_list; f; f = f->next) { 29760ce86c7SWei Liu /* If fid is already there return NULL */ 29860ce86c7SWei Liu BUG_ON(f->clunked); 29960ce86c7SWei Liu if (f->fid == fid) { 30060ce86c7SWei Liu return NULL; 30160ce86c7SWei Liu } 30260ce86c7SWei Liu } 30360ce86c7SWei Liu f = g_malloc0(sizeof(V9fsFidState)); 30460ce86c7SWei Liu f->fid = fid; 30560ce86c7SWei Liu f->fid_type = P9_FID_NONE; 30660ce86c7SWei Liu f->ref = 1; 30760ce86c7SWei Liu /* 30860ce86c7SWei Liu * Mark the fid as referenced so that the LRU 30960ce86c7SWei Liu * reclaim won't close the file descriptor 31060ce86c7SWei Liu */ 31160ce86c7SWei Liu f->flags |= FID_REFERENCED; 31260ce86c7SWei Liu f->next = s->fid_list; 31360ce86c7SWei Liu s->fid_list = f; 31460ce86c7SWei Liu 3157cde47d4SGreg Kurz v9fs_readdir_init(&f->fs.dir); 3167cde47d4SGreg Kurz v9fs_readdir_init(&f->fs_reclaim.dir); 3177cde47d4SGreg Kurz 31860ce86c7SWei Liu return f; 31960ce86c7SWei Liu } 32060ce86c7SWei Liu 3218440e22eSGreg Kurz static int coroutine_fn v9fs_xattr_fid_clunk(V9fsPDU *pdu, V9fsFidState *fidp) 32260ce86c7SWei Liu { 32360ce86c7SWei Liu int retval = 0; 32460ce86c7SWei Liu 325dd28fbbcSLi Qiang if (fidp->fs.xattr.xattrwalk_fid) { 32660ce86c7SWei Liu /* getxattr/listxattr fid */ 32760ce86c7SWei Liu goto free_value; 32860ce86c7SWei Liu } 32960ce86c7SWei Liu /* 33060ce86c7SWei Liu * if this is fid for setxattr. clunk should 33160ce86c7SWei Liu * result in setxattr localcall 33260ce86c7SWei Liu */ 33360ce86c7SWei Liu if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) { 33460ce86c7SWei Liu /* clunk after partial write */ 33560ce86c7SWei Liu retval = -EINVAL; 33660ce86c7SWei Liu goto free_out; 33760ce86c7SWei Liu } 33860ce86c7SWei Liu if (fidp->fs.xattr.len) { 33960ce86c7SWei Liu retval = v9fs_co_lsetxattr(pdu, &fidp->path, &fidp->fs.xattr.name, 34060ce86c7SWei Liu fidp->fs.xattr.value, 34160ce86c7SWei Liu fidp->fs.xattr.len, 34260ce86c7SWei Liu fidp->fs.xattr.flags); 34360ce86c7SWei Liu } else { 34460ce86c7SWei Liu retval = v9fs_co_lremovexattr(pdu, &fidp->path, &fidp->fs.xattr.name); 34560ce86c7SWei Liu } 34660ce86c7SWei Liu free_out: 34760ce86c7SWei Liu v9fs_string_free(&fidp->fs.xattr.name); 34860ce86c7SWei Liu free_value: 34960ce86c7SWei Liu g_free(fidp->fs.xattr.value); 35060ce86c7SWei Liu return retval; 35160ce86c7SWei Liu } 35260ce86c7SWei Liu 3538440e22eSGreg Kurz static int coroutine_fn free_fid(V9fsPDU *pdu, V9fsFidState *fidp) 35460ce86c7SWei Liu { 35560ce86c7SWei Liu int retval = 0; 35660ce86c7SWei Liu 35760ce86c7SWei Liu if (fidp->fid_type == P9_FID_FILE) { 35860ce86c7SWei Liu /* If we reclaimed the fd no need to close */ 35960ce86c7SWei Liu if (fidp->fs.fd != -1) { 36060ce86c7SWei Liu retval = v9fs_co_close(pdu, &fidp->fs); 36160ce86c7SWei Liu } 36260ce86c7SWei Liu } else if (fidp->fid_type == P9_FID_DIR) { 363f314ea4eSGreg Kurz if (fidp->fs.dir.stream != NULL) { 36460ce86c7SWei Liu retval = v9fs_co_closedir(pdu, &fidp->fs); 36560ce86c7SWei Liu } 36660ce86c7SWei Liu } else if (fidp->fid_type == P9_FID_XATTR) { 36760ce86c7SWei Liu retval = v9fs_xattr_fid_clunk(pdu, fidp); 36860ce86c7SWei Liu } 36960ce86c7SWei Liu v9fs_path_free(&fidp->path); 37060ce86c7SWei Liu g_free(fidp); 37160ce86c7SWei Liu return retval; 37260ce86c7SWei Liu } 37360ce86c7SWei Liu 3748440e22eSGreg Kurz static int coroutine_fn put_fid(V9fsPDU *pdu, V9fsFidState *fidp) 37560ce86c7SWei Liu { 37660ce86c7SWei Liu BUG_ON(!fidp->ref); 37760ce86c7SWei Liu fidp->ref--; 37860ce86c7SWei Liu /* 37960ce86c7SWei Liu * Don't free the fid if it is in reclaim list 38060ce86c7SWei Liu */ 38160ce86c7SWei Liu if (!fidp->ref && fidp->clunked) { 38260ce86c7SWei Liu if (fidp->fid == pdu->s->root_fid) { 38360ce86c7SWei Liu /* 38460ce86c7SWei Liu * if the clunked fid is root fid then we 38560ce86c7SWei Liu * have unmounted the fs on the client side. 38660ce86c7SWei Liu * delete the migration blocker. Ideally, this 38760ce86c7SWei Liu * should be hooked to transport close notification 38860ce86c7SWei Liu */ 38960ce86c7SWei Liu if (pdu->s->migration_blocker) { 39060ce86c7SWei Liu migrate_del_blocker(pdu->s->migration_blocker); 39160ce86c7SWei Liu error_free(pdu->s->migration_blocker); 39260ce86c7SWei Liu pdu->s->migration_blocker = NULL; 39360ce86c7SWei Liu } 39460ce86c7SWei Liu } 39560ce86c7SWei Liu return free_fid(pdu, fidp); 39660ce86c7SWei Liu } 39760ce86c7SWei Liu return 0; 39860ce86c7SWei Liu } 39960ce86c7SWei Liu 40060ce86c7SWei Liu static V9fsFidState *clunk_fid(V9fsState *s, int32_t fid) 40160ce86c7SWei Liu { 40260ce86c7SWei Liu V9fsFidState **fidpp, *fidp; 40360ce86c7SWei Liu 40460ce86c7SWei Liu for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) { 40560ce86c7SWei Liu if ((*fidpp)->fid == fid) { 40660ce86c7SWei Liu break; 40760ce86c7SWei Liu } 40860ce86c7SWei Liu } 40960ce86c7SWei Liu if (*fidpp == NULL) { 41060ce86c7SWei Liu return NULL; 41160ce86c7SWei Liu } 41260ce86c7SWei Liu fidp = *fidpp; 41360ce86c7SWei Liu *fidpp = fidp->next; 41460ce86c7SWei Liu fidp->clunked = 1; 41560ce86c7SWei Liu return fidp; 41660ce86c7SWei Liu } 41760ce86c7SWei Liu 4188440e22eSGreg Kurz void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu) 41960ce86c7SWei Liu { 42060ce86c7SWei Liu int reclaim_count = 0; 42160ce86c7SWei Liu V9fsState *s = pdu->s; 42260ce86c7SWei Liu V9fsFidState *f, *reclaim_list = NULL; 42360ce86c7SWei Liu 42460ce86c7SWei Liu for (f = s->fid_list; f; f = f->next) { 42560ce86c7SWei Liu /* 42660ce86c7SWei Liu * Unlink fids cannot be reclaimed. Check 42760ce86c7SWei Liu * for them and skip them. Also skip fids 42860ce86c7SWei Liu * currently being operated on. 42960ce86c7SWei Liu */ 43060ce86c7SWei Liu if (f->ref || f->flags & FID_NON_RECLAIMABLE) { 43160ce86c7SWei Liu continue; 43260ce86c7SWei Liu } 43360ce86c7SWei Liu /* 43460ce86c7SWei Liu * if it is a recently referenced fid 43560ce86c7SWei Liu * we leave the fid untouched and clear the 43660ce86c7SWei Liu * reference bit. We come back to it later 43760ce86c7SWei Liu * in the next iteration. (a simple LRU without 43860ce86c7SWei Liu * moving list elements around) 43960ce86c7SWei Liu */ 44060ce86c7SWei Liu if (f->flags & FID_REFERENCED) { 44160ce86c7SWei Liu f->flags &= ~FID_REFERENCED; 44260ce86c7SWei Liu continue; 44360ce86c7SWei Liu } 44460ce86c7SWei Liu /* 44560ce86c7SWei Liu * Add fids to reclaim list. 44660ce86c7SWei Liu */ 44760ce86c7SWei Liu if (f->fid_type == P9_FID_FILE) { 44860ce86c7SWei Liu if (f->fs.fd != -1) { 44960ce86c7SWei Liu /* 45060ce86c7SWei Liu * Up the reference count so that 45160ce86c7SWei Liu * a clunk request won't free this fid 45260ce86c7SWei Liu */ 45360ce86c7SWei Liu f->ref++; 45460ce86c7SWei Liu f->rclm_lst = reclaim_list; 45560ce86c7SWei Liu reclaim_list = f; 45660ce86c7SWei Liu f->fs_reclaim.fd = f->fs.fd; 45760ce86c7SWei Liu f->fs.fd = -1; 45860ce86c7SWei Liu reclaim_count++; 45960ce86c7SWei Liu } 46060ce86c7SWei Liu } else if (f->fid_type == P9_FID_DIR) { 461f314ea4eSGreg Kurz if (f->fs.dir.stream != NULL) { 46260ce86c7SWei Liu /* 46360ce86c7SWei Liu * Up the reference count so that 46460ce86c7SWei Liu * a clunk request won't free this fid 46560ce86c7SWei Liu */ 46660ce86c7SWei Liu f->ref++; 46760ce86c7SWei Liu f->rclm_lst = reclaim_list; 46860ce86c7SWei Liu reclaim_list = f; 469f314ea4eSGreg Kurz f->fs_reclaim.dir.stream = f->fs.dir.stream; 470f314ea4eSGreg Kurz f->fs.dir.stream = NULL; 47160ce86c7SWei Liu reclaim_count++; 47260ce86c7SWei Liu } 47360ce86c7SWei Liu } 47460ce86c7SWei Liu if (reclaim_count >= open_fd_rc) { 47560ce86c7SWei Liu break; 47660ce86c7SWei Liu } 47760ce86c7SWei Liu } 47860ce86c7SWei Liu /* 47960ce86c7SWei Liu * Now close the fid in reclaim list. Free them if they 48060ce86c7SWei Liu * are already clunked. 48160ce86c7SWei Liu */ 48260ce86c7SWei Liu while (reclaim_list) { 48360ce86c7SWei Liu f = reclaim_list; 48460ce86c7SWei Liu reclaim_list = f->rclm_lst; 48560ce86c7SWei Liu if (f->fid_type == P9_FID_FILE) { 48660ce86c7SWei Liu v9fs_co_close(pdu, &f->fs_reclaim); 48760ce86c7SWei Liu } else if (f->fid_type == P9_FID_DIR) { 48860ce86c7SWei Liu v9fs_co_closedir(pdu, &f->fs_reclaim); 48960ce86c7SWei Liu } 49060ce86c7SWei Liu f->rclm_lst = NULL; 49160ce86c7SWei Liu /* 49260ce86c7SWei Liu * Now drop the fid reference, free it 49360ce86c7SWei Liu * if clunked. 49460ce86c7SWei Liu */ 49560ce86c7SWei Liu put_fid(pdu, f); 49660ce86c7SWei Liu } 49760ce86c7SWei Liu } 49860ce86c7SWei Liu 4998440e22eSGreg Kurz static int coroutine_fn v9fs_mark_fids_unreclaim(V9fsPDU *pdu, V9fsPath *path) 50060ce86c7SWei Liu { 50160ce86c7SWei Liu int err; 50260ce86c7SWei Liu V9fsState *s = pdu->s; 50360ce86c7SWei Liu V9fsFidState *fidp, head_fid; 50460ce86c7SWei Liu 50560ce86c7SWei Liu head_fid.next = s->fid_list; 50660ce86c7SWei Liu for (fidp = s->fid_list; fidp; fidp = fidp->next) { 50760ce86c7SWei Liu if (fidp->path.size != path->size) { 50860ce86c7SWei Liu continue; 50960ce86c7SWei Liu } 51060ce86c7SWei Liu if (!memcmp(fidp->path.data, path->data, path->size)) { 51160ce86c7SWei Liu /* Mark the fid non reclaimable. */ 51260ce86c7SWei Liu fidp->flags |= FID_NON_RECLAIMABLE; 51360ce86c7SWei Liu 51460ce86c7SWei Liu /* reopen the file/dir if already closed */ 51560ce86c7SWei Liu err = v9fs_reopen_fid(pdu, fidp); 51660ce86c7SWei Liu if (err < 0) { 517267fcadfSGreg Kurz return err; 51860ce86c7SWei Liu } 51960ce86c7SWei Liu /* 52060ce86c7SWei Liu * Go back to head of fid list because 52160ce86c7SWei Liu * the list could have got updated when 52260ce86c7SWei Liu * switched to the worker thread 52360ce86c7SWei Liu */ 52460ce86c7SWei Liu if (err == 0) { 52560ce86c7SWei Liu fidp = &head_fid; 52660ce86c7SWei Liu } 52760ce86c7SWei Liu } 52860ce86c7SWei Liu } 52960ce86c7SWei Liu return 0; 53060ce86c7SWei Liu } 53160ce86c7SWei Liu 5328440e22eSGreg Kurz static void coroutine_fn virtfs_reset(V9fsPDU *pdu) 53360ce86c7SWei Liu { 53460ce86c7SWei Liu V9fsState *s = pdu->s; 53579decce3SGreg Kurz V9fsFidState *fidp; 53660ce86c7SWei Liu 53760ce86c7SWei Liu /* Free all fids */ 53860ce86c7SWei Liu while (s->fid_list) { 5396d54af0eSGreg Kurz /* Get fid */ 54060ce86c7SWei Liu fidp = s->fid_list; 5416d54af0eSGreg Kurz fidp->ref++; 54260ce86c7SWei Liu 5436d54af0eSGreg Kurz /* Clunk fid */ 5446d54af0eSGreg Kurz s->fid_list = fidp->next; 54560ce86c7SWei Liu fidp->clunked = 1; 5466d54af0eSGreg Kurz 5476d54af0eSGreg Kurz put_fid(pdu, fidp); 54860ce86c7SWei Liu } 54960ce86c7SWei Liu } 55060ce86c7SWei Liu 55160ce86c7SWei Liu #define P9_QID_TYPE_DIR 0x80 55260ce86c7SWei Liu #define P9_QID_TYPE_SYMLINK 0x02 55360ce86c7SWei Liu 55460ce86c7SWei Liu #define P9_STAT_MODE_DIR 0x80000000 55560ce86c7SWei Liu #define P9_STAT_MODE_APPEND 0x40000000 55660ce86c7SWei Liu #define P9_STAT_MODE_EXCL 0x20000000 55760ce86c7SWei Liu #define P9_STAT_MODE_MOUNT 0x10000000 55860ce86c7SWei Liu #define P9_STAT_MODE_AUTH 0x08000000 55960ce86c7SWei Liu #define P9_STAT_MODE_TMP 0x04000000 56060ce86c7SWei Liu #define P9_STAT_MODE_SYMLINK 0x02000000 56160ce86c7SWei Liu #define P9_STAT_MODE_LINK 0x01000000 56260ce86c7SWei Liu #define P9_STAT_MODE_DEVICE 0x00800000 56360ce86c7SWei Liu #define P9_STAT_MODE_NAMED_PIPE 0x00200000 56460ce86c7SWei Liu #define P9_STAT_MODE_SOCKET 0x00100000 56560ce86c7SWei Liu #define P9_STAT_MODE_SETUID 0x00080000 56660ce86c7SWei Liu #define P9_STAT_MODE_SETGID 0x00040000 56760ce86c7SWei Liu #define P9_STAT_MODE_SETVTX 0x00010000 56860ce86c7SWei Liu 56960ce86c7SWei Liu #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \ 57060ce86c7SWei Liu P9_STAT_MODE_SYMLINK | \ 57160ce86c7SWei Liu P9_STAT_MODE_LINK | \ 57260ce86c7SWei Liu P9_STAT_MODE_DEVICE | \ 57360ce86c7SWei Liu P9_STAT_MODE_NAMED_PIPE | \ 57460ce86c7SWei Liu P9_STAT_MODE_SOCKET) 57560ce86c7SWei Liu 5761a6ed33cSAntonios Motakis /* creative abuse of tb_hash_func7, which is based on xxhash */ 5771a6ed33cSAntonios Motakis static uint32_t qpp_hash(QppEntry e) 5781a6ed33cSAntonios Motakis { 5791a6ed33cSAntonios Motakis return qemu_xxhash7(e.ino_prefix, e.dev, 0, 0, 0); 5801a6ed33cSAntonios Motakis } 5811a6ed33cSAntonios Motakis 582*f3fe4a2dSAntonios Motakis static uint32_t qpf_hash(QpfEntry e) 583*f3fe4a2dSAntonios Motakis { 584*f3fe4a2dSAntonios Motakis return qemu_xxhash7(e.ino, e.dev, 0, 0, 0); 585*f3fe4a2dSAntonios Motakis } 586*f3fe4a2dSAntonios Motakis 5871a6ed33cSAntonios Motakis static bool qpp_lookup_func(const void *obj, const void *userp) 5881a6ed33cSAntonios Motakis { 5891a6ed33cSAntonios Motakis const QppEntry *e1 = obj, *e2 = userp; 5901a6ed33cSAntonios Motakis return e1->dev == e2->dev && e1->ino_prefix == e2->ino_prefix; 5911a6ed33cSAntonios Motakis } 5921a6ed33cSAntonios Motakis 593*f3fe4a2dSAntonios Motakis static bool qpf_lookup_func(const void *obj, const void *userp) 594*f3fe4a2dSAntonios Motakis { 595*f3fe4a2dSAntonios Motakis const QpfEntry *e1 = obj, *e2 = userp; 596*f3fe4a2dSAntonios Motakis return e1->dev == e2->dev && e1->ino == e2->ino; 597*f3fe4a2dSAntonios Motakis } 598*f3fe4a2dSAntonios Motakis 599*f3fe4a2dSAntonios Motakis static void qp_table_remove(void *p, uint32_t h, void *up) 6001a6ed33cSAntonios Motakis { 6011a6ed33cSAntonios Motakis g_free(p); 6021a6ed33cSAntonios Motakis } 6031a6ed33cSAntonios Motakis 604*f3fe4a2dSAntonios Motakis static void qp_table_destroy(struct qht *ht) 6051a6ed33cSAntonios Motakis { 6061a6ed33cSAntonios Motakis if (!ht || !ht->map) { 6071a6ed33cSAntonios Motakis return; 6081a6ed33cSAntonios Motakis } 609*f3fe4a2dSAntonios Motakis qht_iter(ht, qp_table_remove, NULL); 6101a6ed33cSAntonios Motakis qht_destroy(ht); 6111a6ed33cSAntonios Motakis } 6121a6ed33cSAntonios Motakis 6131a6ed33cSAntonios Motakis static void qpp_table_init(struct qht *ht) 6141a6ed33cSAntonios Motakis { 6151a6ed33cSAntonios Motakis qht_init(ht, qpp_lookup_func, 1, QHT_MODE_AUTO_RESIZE); 6161a6ed33cSAntonios Motakis } 6171a6ed33cSAntonios Motakis 618*f3fe4a2dSAntonios Motakis static void qpf_table_init(struct qht *ht) 619*f3fe4a2dSAntonios Motakis { 620*f3fe4a2dSAntonios Motakis qht_init(ht, qpf_lookup_func, 1 << 16, QHT_MODE_AUTO_RESIZE); 621*f3fe4a2dSAntonios Motakis } 622*f3fe4a2dSAntonios Motakis 623*f3fe4a2dSAntonios Motakis static int qid_path_fullmap(V9fsPDU *pdu, const struct stat *stbuf, 624*f3fe4a2dSAntonios Motakis uint64_t *path) 625*f3fe4a2dSAntonios Motakis { 626*f3fe4a2dSAntonios Motakis QpfEntry lookup = { 627*f3fe4a2dSAntonios Motakis .dev = stbuf->st_dev, 628*f3fe4a2dSAntonios Motakis .ino = stbuf->st_ino 629*f3fe4a2dSAntonios Motakis }, *val; 630*f3fe4a2dSAntonios Motakis uint32_t hash = qpf_hash(lookup); 631*f3fe4a2dSAntonios Motakis 632*f3fe4a2dSAntonios Motakis /* most users won't need the fullmap, so init the table lazily */ 633*f3fe4a2dSAntonios Motakis if (!pdu->s->qpf_table.map) { 634*f3fe4a2dSAntonios Motakis qpf_table_init(&pdu->s->qpf_table); 635*f3fe4a2dSAntonios Motakis } 636*f3fe4a2dSAntonios Motakis 637*f3fe4a2dSAntonios Motakis val = qht_lookup(&pdu->s->qpf_table, &lookup, hash); 638*f3fe4a2dSAntonios Motakis 639*f3fe4a2dSAntonios Motakis if (!val) { 640*f3fe4a2dSAntonios Motakis if (pdu->s->qp_fullpath_next == 0) { 641*f3fe4a2dSAntonios Motakis /* no more files can be mapped :'( */ 642*f3fe4a2dSAntonios Motakis error_report_once( 643*f3fe4a2dSAntonios Motakis "9p: No more prefixes available for remapping inodes from " 644*f3fe4a2dSAntonios Motakis "host to guest." 645*f3fe4a2dSAntonios Motakis ); 646*f3fe4a2dSAntonios Motakis return -ENFILE; 647*f3fe4a2dSAntonios Motakis } 648*f3fe4a2dSAntonios Motakis 649*f3fe4a2dSAntonios Motakis val = g_malloc0(sizeof(QppEntry)); 650*f3fe4a2dSAntonios Motakis *val = lookup; 651*f3fe4a2dSAntonios Motakis 652*f3fe4a2dSAntonios Motakis /* new unique inode and device combo */ 653*f3fe4a2dSAntonios Motakis val->path = pdu->s->qp_fullpath_next++; 654*f3fe4a2dSAntonios Motakis pdu->s->qp_fullpath_next &= QPATH_INO_MASK; 655*f3fe4a2dSAntonios Motakis qht_insert(&pdu->s->qpf_table, val, hash, NULL); 656*f3fe4a2dSAntonios Motakis } 657*f3fe4a2dSAntonios Motakis 658*f3fe4a2dSAntonios Motakis *path = val->path; 659*f3fe4a2dSAntonios Motakis return 0; 660*f3fe4a2dSAntonios Motakis } 661*f3fe4a2dSAntonios Motakis 6621a6ed33cSAntonios Motakis /* 6631a6ed33cSAntonios Motakis * stat_to_qid needs to map inode number (64 bits) and device id (32 bits) 6641a6ed33cSAntonios Motakis * to a unique QID path (64 bits). To avoid having to map and keep track 6651a6ed33cSAntonios Motakis * of up to 2^64 objects, we map only the 16 highest bits of the inode plus 6661a6ed33cSAntonios Motakis * the device id to the 16 highest bits of the QID path. The 48 lowest bits 6671a6ed33cSAntonios Motakis * of the QID path equal to the lowest bits of the inode number. 6681a6ed33cSAntonios Motakis * 6691a6ed33cSAntonios Motakis * This takes advantage of the fact that inode number are usually not 6701a6ed33cSAntonios Motakis * random but allocated sequentially, so we have fewer items to keep 6711a6ed33cSAntonios Motakis * track of. 6721a6ed33cSAntonios Motakis */ 6731a6ed33cSAntonios Motakis static int qid_path_prefixmap(V9fsPDU *pdu, const struct stat *stbuf, 6741a6ed33cSAntonios Motakis uint64_t *path) 6751a6ed33cSAntonios Motakis { 6761a6ed33cSAntonios Motakis QppEntry lookup = { 6771a6ed33cSAntonios Motakis .dev = stbuf->st_dev, 6781a6ed33cSAntonios Motakis .ino_prefix = (uint16_t) (stbuf->st_ino >> 48) 6791a6ed33cSAntonios Motakis }, *val; 6801a6ed33cSAntonios Motakis uint32_t hash = qpp_hash(lookup); 6811a6ed33cSAntonios Motakis 6821a6ed33cSAntonios Motakis val = qht_lookup(&pdu->s->qpp_table, &lookup, hash); 6831a6ed33cSAntonios Motakis 6841a6ed33cSAntonios Motakis if (!val) { 6851a6ed33cSAntonios Motakis if (pdu->s->qp_prefix_next == 0) { 6861a6ed33cSAntonios Motakis /* we ran out of prefixes */ 687*f3fe4a2dSAntonios Motakis warn_report_once( 688*f3fe4a2dSAntonios Motakis "9p: Potential degraded performance of inode remapping" 6891a6ed33cSAntonios Motakis ); 6901a6ed33cSAntonios Motakis return -ENFILE; 6911a6ed33cSAntonios Motakis } 6921a6ed33cSAntonios Motakis 6931a6ed33cSAntonios Motakis val = g_malloc0(sizeof(QppEntry)); 6941a6ed33cSAntonios Motakis *val = lookup; 6951a6ed33cSAntonios Motakis 6961a6ed33cSAntonios Motakis /* new unique inode prefix and device combo */ 6971a6ed33cSAntonios Motakis val->qp_prefix = pdu->s->qp_prefix_next++; 6981a6ed33cSAntonios Motakis qht_insert(&pdu->s->qpp_table, val, hash, NULL); 6991a6ed33cSAntonios Motakis } 7001a6ed33cSAntonios Motakis 7011a6ed33cSAntonios Motakis *path = ((uint64_t)val->qp_prefix << 48) | (stbuf->st_ino & QPATH_INO_MASK); 7021a6ed33cSAntonios Motakis return 0; 7031a6ed33cSAntonios Motakis } 7041a6ed33cSAntonios Motakis 7053b5ee9e8SAntonios Motakis static int stat_to_qid(V9fsPDU *pdu, const struct stat *stbuf, V9fsQID *qidp) 70660ce86c7SWei Liu { 7071a6ed33cSAntonios Motakis int err; 70860ce86c7SWei Liu size_t size; 70960ce86c7SWei Liu 7101a6ed33cSAntonios Motakis if (pdu->s->ctx.export_flags & V9FS_REMAP_INODES) { 7111a6ed33cSAntonios Motakis /* map inode+device to qid path (fast path) */ 7121a6ed33cSAntonios Motakis err = qid_path_prefixmap(pdu, stbuf, &qidp->path); 713*f3fe4a2dSAntonios Motakis if (err == -ENFILE) { 714*f3fe4a2dSAntonios Motakis /* fast path didn't work, fall back to full map */ 715*f3fe4a2dSAntonios Motakis err = qid_path_fullmap(pdu, stbuf, &qidp->path); 716*f3fe4a2dSAntonios Motakis } 7171a6ed33cSAntonios Motakis if (err) { 7181a6ed33cSAntonios Motakis return err; 7191a6ed33cSAntonios Motakis } 7201a6ed33cSAntonios Motakis } else { 7213b5ee9e8SAntonios Motakis if (pdu->s->dev_id != stbuf->st_dev) { 7221a6ed33cSAntonios Motakis if (pdu->s->ctx.export_flags & V9FS_FORBID_MULTIDEVS) { 7231a6ed33cSAntonios Motakis error_report_once( 7241a6ed33cSAntonios Motakis "9p: Multiple devices detected in same VirtFS export. " 7251a6ed33cSAntonios Motakis "Access of guest to additional devices is (partly) " 7261a6ed33cSAntonios Motakis "denied due to virtfs option 'multidevs=forbid' being " 7271a6ed33cSAntonios Motakis "effective." 7281a6ed33cSAntonios Motakis ); 7291a6ed33cSAntonios Motakis return -ENODEV; 7301a6ed33cSAntonios Motakis } else { 7313b5ee9e8SAntonios Motakis warn_report_once( 7323b5ee9e8SAntonios Motakis "9p: Multiple devices detected in same VirtFS export, " 7333b5ee9e8SAntonios Motakis "which might lead to file ID collisions and severe " 7341a6ed33cSAntonios Motakis "misbehaviours on guest! You should either use a " 7351a6ed33cSAntonios Motakis "separate export for each device shared from host or " 7361a6ed33cSAntonios Motakis "use virtfs option 'multidevs=remap'!" 7373b5ee9e8SAntonios Motakis ); 7383b5ee9e8SAntonios Motakis } 7391a6ed33cSAntonios Motakis } 74060ce86c7SWei Liu memset(&qidp->path, 0, sizeof(qidp->path)); 74160ce86c7SWei Liu size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path)); 74260ce86c7SWei Liu memcpy(&qidp->path, &stbuf->st_ino, size); 7431a6ed33cSAntonios Motakis } 7441a6ed33cSAntonios Motakis 74560ce86c7SWei Liu qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8); 74660ce86c7SWei Liu qidp->type = 0; 74760ce86c7SWei Liu if (S_ISDIR(stbuf->st_mode)) { 74860ce86c7SWei Liu qidp->type |= P9_QID_TYPE_DIR; 74960ce86c7SWei Liu } 75060ce86c7SWei Liu if (S_ISLNK(stbuf->st_mode)) { 75160ce86c7SWei Liu qidp->type |= P9_QID_TYPE_SYMLINK; 75260ce86c7SWei Liu } 7533b5ee9e8SAntonios Motakis 7543b5ee9e8SAntonios Motakis return 0; 75560ce86c7SWei Liu } 75660ce86c7SWei Liu 7578440e22eSGreg Kurz static int coroutine_fn fid_to_qid(V9fsPDU *pdu, V9fsFidState *fidp, 7588440e22eSGreg Kurz V9fsQID *qidp) 75960ce86c7SWei Liu { 76060ce86c7SWei Liu struct stat stbuf; 76160ce86c7SWei Liu int err; 76260ce86c7SWei Liu 76360ce86c7SWei Liu err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); 76460ce86c7SWei Liu if (err < 0) { 76560ce86c7SWei Liu return err; 76660ce86c7SWei Liu } 7673b5ee9e8SAntonios Motakis err = stat_to_qid(pdu, &stbuf, qidp); 7683b5ee9e8SAntonios Motakis if (err < 0) { 7693b5ee9e8SAntonios Motakis return err; 7703b5ee9e8SAntonios Motakis } 77160ce86c7SWei Liu return 0; 77260ce86c7SWei Liu } 77360ce86c7SWei Liu 7741a6ed33cSAntonios Motakis static int coroutine_fn dirent_to_qid(V9fsPDU *pdu, V9fsFidState *fidp, 7751a6ed33cSAntonios Motakis struct dirent *dent, V9fsQID *qidp) 7761a6ed33cSAntonios Motakis { 7771a6ed33cSAntonios Motakis struct stat stbuf; 7781a6ed33cSAntonios Motakis V9fsPath path; 7791a6ed33cSAntonios Motakis int err; 7801a6ed33cSAntonios Motakis 7811a6ed33cSAntonios Motakis v9fs_path_init(&path); 7821a6ed33cSAntonios Motakis 7831a6ed33cSAntonios Motakis err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path); 7841a6ed33cSAntonios Motakis if (err < 0) { 7851a6ed33cSAntonios Motakis goto out; 7861a6ed33cSAntonios Motakis } 7871a6ed33cSAntonios Motakis err = v9fs_co_lstat(pdu, &path, &stbuf); 7881a6ed33cSAntonios Motakis if (err < 0) { 7891a6ed33cSAntonios Motakis goto out; 7901a6ed33cSAntonios Motakis } 7911a6ed33cSAntonios Motakis err = stat_to_qid(pdu, &stbuf, qidp); 7921a6ed33cSAntonios Motakis 7931a6ed33cSAntonios Motakis out: 7941a6ed33cSAntonios Motakis v9fs_path_free(&path); 7951a6ed33cSAntonios Motakis return err; 7961a6ed33cSAntonios Motakis } 7971a6ed33cSAntonios Motakis 79860ce86c7SWei Liu V9fsPDU *pdu_alloc(V9fsState *s) 79960ce86c7SWei Liu { 80060ce86c7SWei Liu V9fsPDU *pdu = NULL; 80160ce86c7SWei Liu 80260ce86c7SWei Liu if (!QLIST_EMPTY(&s->free_list)) { 80360ce86c7SWei Liu pdu = QLIST_FIRST(&s->free_list); 80460ce86c7SWei Liu QLIST_REMOVE(pdu, next); 80560ce86c7SWei Liu QLIST_INSERT_HEAD(&s->active_list, pdu, next); 80660ce86c7SWei Liu } 80760ce86c7SWei Liu return pdu; 80860ce86c7SWei Liu } 80960ce86c7SWei Liu 81060ce86c7SWei Liu void pdu_free(V9fsPDU *pdu) 81160ce86c7SWei Liu { 81260ce86c7SWei Liu V9fsState *s = pdu->s; 813f74e27bfSGreg Kurz 814f74e27bfSGreg Kurz g_assert(!pdu->cancelled); 81560ce86c7SWei Liu QLIST_REMOVE(pdu, next); 81660ce86c7SWei Liu QLIST_INSERT_HEAD(&s->free_list, pdu, next); 81760ce86c7SWei Liu } 81860ce86c7SWei Liu 8198440e22eSGreg Kurz static void coroutine_fn pdu_complete(V9fsPDU *pdu, ssize_t len) 82060ce86c7SWei Liu { 82160ce86c7SWei Liu int8_t id = pdu->id + 1; /* Response */ 82260ce86c7SWei Liu V9fsState *s = pdu->s; 82306a37db7SGreg Kurz int ret; 82460ce86c7SWei Liu 825fc78d5eeSKeno Fischer /* 826fc78d5eeSKeno Fischer * The 9p spec requires that successfully cancelled pdus receive no reply. 827fc78d5eeSKeno Fischer * Sending a reply would confuse clients because they would 828fc78d5eeSKeno Fischer * assume that any EINTR is the actual result of the operation, 829fc78d5eeSKeno Fischer * rather than a consequence of the cancellation. However, if 830fc78d5eeSKeno Fischer * the operation completed (succesfully or with an error other 831fc78d5eeSKeno Fischer * than caused be cancellation), we do send out that reply, both 832fc78d5eeSKeno Fischer * for efficiency and to avoid confusing the rest of the state machine 833fc78d5eeSKeno Fischer * that assumes passing a non-error here will mean a successful 834fc78d5eeSKeno Fischer * transmission of the reply. 835fc78d5eeSKeno Fischer */ 836fc78d5eeSKeno Fischer bool discard = pdu->cancelled && len == -EINTR; 837fc78d5eeSKeno Fischer if (discard) { 838fc78d5eeSKeno Fischer trace_v9fs_rcancel(pdu->tag, pdu->id); 839fc78d5eeSKeno Fischer pdu->size = 0; 840fc78d5eeSKeno Fischer goto out_notify; 841fc78d5eeSKeno Fischer } 842fc78d5eeSKeno Fischer 84360ce86c7SWei Liu if (len < 0) { 84460ce86c7SWei Liu int err = -len; 84560ce86c7SWei Liu len = 7; 84660ce86c7SWei Liu 84760ce86c7SWei Liu if (s->proto_version != V9FS_PROTO_2000L) { 84860ce86c7SWei Liu V9fsString str; 84960ce86c7SWei Liu 85060ce86c7SWei Liu str.data = strerror(err); 85160ce86c7SWei Liu str.size = strlen(str.data); 85260ce86c7SWei Liu 85306a37db7SGreg Kurz ret = pdu_marshal(pdu, len, "s", &str); 85406a37db7SGreg Kurz if (ret < 0) { 85506a37db7SGreg Kurz goto out_notify; 85606a37db7SGreg Kurz } 85706a37db7SGreg Kurz len += ret; 85860ce86c7SWei Liu id = P9_RERROR; 85960ce86c7SWei Liu } 86060ce86c7SWei Liu 86106a37db7SGreg Kurz ret = pdu_marshal(pdu, len, "d", err); 86206a37db7SGreg Kurz if (ret < 0) { 86306a37db7SGreg Kurz goto out_notify; 86406a37db7SGreg Kurz } 86506a37db7SGreg Kurz len += ret; 86660ce86c7SWei Liu 86760ce86c7SWei Liu if (s->proto_version == V9FS_PROTO_2000L) { 86860ce86c7SWei Liu id = P9_RLERROR; 86960ce86c7SWei Liu } 87060ce86c7SWei Liu trace_v9fs_rerror(pdu->tag, pdu->id, err); /* Trace ERROR */ 87160ce86c7SWei Liu } 87260ce86c7SWei Liu 87360ce86c7SWei Liu /* fill out the header */ 87406a37db7SGreg Kurz if (pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag) < 0) { 87506a37db7SGreg Kurz goto out_notify; 87606a37db7SGreg Kurz } 87760ce86c7SWei Liu 87860ce86c7SWei Liu /* keep these in sync */ 87960ce86c7SWei Liu pdu->size = len; 88060ce86c7SWei Liu pdu->id = id; 88160ce86c7SWei Liu 88206a37db7SGreg Kurz out_notify: 883a17d8659SGreg Kurz pdu->s->transport->push_and_notify(pdu); 88460ce86c7SWei Liu 88560ce86c7SWei Liu /* Now wakeup anybody waiting in flush for this request */ 886f74e27bfSGreg Kurz if (!qemu_co_queue_next(&pdu->complete)) { 88760ce86c7SWei Liu pdu_free(pdu); 88860ce86c7SWei Liu } 889f74e27bfSGreg Kurz } 89060ce86c7SWei Liu 89160ce86c7SWei Liu static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension) 89260ce86c7SWei Liu { 89360ce86c7SWei Liu mode_t ret; 89460ce86c7SWei Liu 89560ce86c7SWei Liu ret = mode & 0777; 89660ce86c7SWei Liu if (mode & P9_STAT_MODE_DIR) { 89760ce86c7SWei Liu ret |= S_IFDIR; 89860ce86c7SWei Liu } 89960ce86c7SWei Liu 90060ce86c7SWei Liu if (mode & P9_STAT_MODE_SYMLINK) { 90160ce86c7SWei Liu ret |= S_IFLNK; 90260ce86c7SWei Liu } 90360ce86c7SWei Liu if (mode & P9_STAT_MODE_SOCKET) { 90460ce86c7SWei Liu ret |= S_IFSOCK; 90560ce86c7SWei Liu } 90660ce86c7SWei Liu if (mode & P9_STAT_MODE_NAMED_PIPE) { 90760ce86c7SWei Liu ret |= S_IFIFO; 90860ce86c7SWei Liu } 90960ce86c7SWei Liu if (mode & P9_STAT_MODE_DEVICE) { 91060ce86c7SWei Liu if (extension->size && extension->data[0] == 'c') { 91160ce86c7SWei Liu ret |= S_IFCHR; 91260ce86c7SWei Liu } else { 91360ce86c7SWei Liu ret |= S_IFBLK; 91460ce86c7SWei Liu } 91560ce86c7SWei Liu } 91660ce86c7SWei Liu 91760ce86c7SWei Liu if (!(ret&~0777)) { 91860ce86c7SWei Liu ret |= S_IFREG; 91960ce86c7SWei Liu } 92060ce86c7SWei Liu 92160ce86c7SWei Liu if (mode & P9_STAT_MODE_SETUID) { 92260ce86c7SWei Liu ret |= S_ISUID; 92360ce86c7SWei Liu } 92460ce86c7SWei Liu if (mode & P9_STAT_MODE_SETGID) { 92560ce86c7SWei Liu ret |= S_ISGID; 92660ce86c7SWei Liu } 92760ce86c7SWei Liu if (mode & P9_STAT_MODE_SETVTX) { 92860ce86c7SWei Liu ret |= S_ISVTX; 92960ce86c7SWei Liu } 93060ce86c7SWei Liu 93160ce86c7SWei Liu return ret; 93260ce86c7SWei Liu } 93360ce86c7SWei Liu 93460ce86c7SWei Liu static int donttouch_stat(V9fsStat *stat) 93560ce86c7SWei Liu { 93660ce86c7SWei Liu if (stat->type == -1 && 93760ce86c7SWei Liu stat->dev == -1 && 93887032833SAntonios Motakis stat->qid.type == 0xff && 93987032833SAntonios Motakis stat->qid.version == (uint32_t) -1 && 94087032833SAntonios Motakis stat->qid.path == (uint64_t) -1 && 94160ce86c7SWei Liu stat->mode == -1 && 94260ce86c7SWei Liu stat->atime == -1 && 94360ce86c7SWei Liu stat->mtime == -1 && 94460ce86c7SWei Liu stat->length == -1 && 94560ce86c7SWei Liu !stat->name.size && 94660ce86c7SWei Liu !stat->uid.size && 94760ce86c7SWei Liu !stat->gid.size && 94860ce86c7SWei Liu !stat->muid.size && 94960ce86c7SWei Liu stat->n_uid == -1 && 95060ce86c7SWei Liu stat->n_gid == -1 && 95160ce86c7SWei Liu stat->n_muid == -1) { 95260ce86c7SWei Liu return 1; 95360ce86c7SWei Liu } 95460ce86c7SWei Liu 95560ce86c7SWei Liu return 0; 95660ce86c7SWei Liu } 95760ce86c7SWei Liu 95860ce86c7SWei Liu static void v9fs_stat_init(V9fsStat *stat) 95960ce86c7SWei Liu { 96060ce86c7SWei Liu v9fs_string_init(&stat->name); 96160ce86c7SWei Liu v9fs_string_init(&stat->uid); 96260ce86c7SWei Liu v9fs_string_init(&stat->gid); 96360ce86c7SWei Liu v9fs_string_init(&stat->muid); 96460ce86c7SWei Liu v9fs_string_init(&stat->extension); 96560ce86c7SWei Liu } 96660ce86c7SWei Liu 96760ce86c7SWei Liu static void v9fs_stat_free(V9fsStat *stat) 96860ce86c7SWei Liu { 96960ce86c7SWei Liu v9fs_string_free(&stat->name); 97060ce86c7SWei Liu v9fs_string_free(&stat->uid); 97160ce86c7SWei Liu v9fs_string_free(&stat->gid); 97260ce86c7SWei Liu v9fs_string_free(&stat->muid); 97360ce86c7SWei Liu v9fs_string_free(&stat->extension); 97460ce86c7SWei Liu } 97560ce86c7SWei Liu 97660ce86c7SWei Liu static uint32_t stat_to_v9mode(const struct stat *stbuf) 97760ce86c7SWei Liu { 97860ce86c7SWei Liu uint32_t mode; 97960ce86c7SWei Liu 98060ce86c7SWei Liu mode = stbuf->st_mode & 0777; 98160ce86c7SWei Liu if (S_ISDIR(stbuf->st_mode)) { 98260ce86c7SWei Liu mode |= P9_STAT_MODE_DIR; 98360ce86c7SWei Liu } 98460ce86c7SWei Liu 98560ce86c7SWei Liu if (S_ISLNK(stbuf->st_mode)) { 98660ce86c7SWei Liu mode |= P9_STAT_MODE_SYMLINK; 98760ce86c7SWei Liu } 98860ce86c7SWei Liu 98960ce86c7SWei Liu if (S_ISSOCK(stbuf->st_mode)) { 99060ce86c7SWei Liu mode |= P9_STAT_MODE_SOCKET; 99160ce86c7SWei Liu } 99260ce86c7SWei Liu 99360ce86c7SWei Liu if (S_ISFIFO(stbuf->st_mode)) { 99460ce86c7SWei Liu mode |= P9_STAT_MODE_NAMED_PIPE; 99560ce86c7SWei Liu } 99660ce86c7SWei Liu 99760ce86c7SWei Liu if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) { 99860ce86c7SWei Liu mode |= P9_STAT_MODE_DEVICE; 99960ce86c7SWei Liu } 100060ce86c7SWei Liu 100160ce86c7SWei Liu if (stbuf->st_mode & S_ISUID) { 100260ce86c7SWei Liu mode |= P9_STAT_MODE_SETUID; 100360ce86c7SWei Liu } 100460ce86c7SWei Liu 100560ce86c7SWei Liu if (stbuf->st_mode & S_ISGID) { 100660ce86c7SWei Liu mode |= P9_STAT_MODE_SETGID; 100760ce86c7SWei Liu } 100860ce86c7SWei Liu 100960ce86c7SWei Liu if (stbuf->st_mode & S_ISVTX) { 101060ce86c7SWei Liu mode |= P9_STAT_MODE_SETVTX; 101160ce86c7SWei Liu } 101260ce86c7SWei Liu 101360ce86c7SWei Liu return mode; 101460ce86c7SWei Liu } 101560ce86c7SWei Liu 10166069537fSJan Dakinevich static int coroutine_fn stat_to_v9stat(V9fsPDU *pdu, V9fsPath *path, 10176069537fSJan Dakinevich const char *basename, 101860ce86c7SWei Liu const struct stat *stbuf, 101960ce86c7SWei Liu V9fsStat *v9stat) 102060ce86c7SWei Liu { 102160ce86c7SWei Liu int err; 102260ce86c7SWei Liu 102360ce86c7SWei Liu memset(v9stat, 0, sizeof(*v9stat)); 102460ce86c7SWei Liu 10253b5ee9e8SAntonios Motakis err = stat_to_qid(pdu, stbuf, &v9stat->qid); 10263b5ee9e8SAntonios Motakis if (err < 0) { 10273b5ee9e8SAntonios Motakis return err; 10283b5ee9e8SAntonios Motakis } 102960ce86c7SWei Liu v9stat->mode = stat_to_v9mode(stbuf); 103060ce86c7SWei Liu v9stat->atime = stbuf->st_atime; 103160ce86c7SWei Liu v9stat->mtime = stbuf->st_mtime; 103260ce86c7SWei Liu v9stat->length = stbuf->st_size; 103360ce86c7SWei Liu 1034abdf0086SGreg Kurz v9fs_string_free(&v9stat->uid); 1035abdf0086SGreg Kurz v9fs_string_free(&v9stat->gid); 1036abdf0086SGreg Kurz v9fs_string_free(&v9stat->muid); 103760ce86c7SWei Liu 103860ce86c7SWei Liu v9stat->n_uid = stbuf->st_uid; 103960ce86c7SWei Liu v9stat->n_gid = stbuf->st_gid; 104060ce86c7SWei Liu v9stat->n_muid = 0; 104160ce86c7SWei Liu 1042abdf0086SGreg Kurz v9fs_string_free(&v9stat->extension); 104360ce86c7SWei Liu 104460ce86c7SWei Liu if (v9stat->mode & P9_STAT_MODE_SYMLINK) { 10456069537fSJan Dakinevich err = v9fs_co_readlink(pdu, path, &v9stat->extension); 104660ce86c7SWei Liu if (err < 0) { 104760ce86c7SWei Liu return err; 104860ce86c7SWei Liu } 104960ce86c7SWei Liu } else if (v9stat->mode & P9_STAT_MODE_DEVICE) { 105060ce86c7SWei Liu v9fs_string_sprintf(&v9stat->extension, "%c %u %u", 105160ce86c7SWei Liu S_ISCHR(stbuf->st_mode) ? 'c' : 'b', 105260ce86c7SWei Liu major(stbuf->st_rdev), minor(stbuf->st_rdev)); 105360ce86c7SWei Liu } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) { 105460ce86c7SWei Liu v9fs_string_sprintf(&v9stat->extension, "%s %lu", 105560ce86c7SWei Liu "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink); 105660ce86c7SWei Liu } 105760ce86c7SWei Liu 10586069537fSJan Dakinevich v9fs_string_sprintf(&v9stat->name, "%s", basename); 105960ce86c7SWei Liu 106060ce86c7SWei Liu v9stat->size = 61 + 106160ce86c7SWei Liu v9fs_string_size(&v9stat->name) + 106260ce86c7SWei Liu v9fs_string_size(&v9stat->uid) + 106360ce86c7SWei Liu v9fs_string_size(&v9stat->gid) + 106460ce86c7SWei Liu v9fs_string_size(&v9stat->muid) + 106560ce86c7SWei Liu v9fs_string_size(&v9stat->extension); 106660ce86c7SWei Liu return 0; 106760ce86c7SWei Liu } 106860ce86c7SWei Liu 106960ce86c7SWei Liu #define P9_STATS_MODE 0x00000001ULL 107060ce86c7SWei Liu #define P9_STATS_NLINK 0x00000002ULL 107160ce86c7SWei Liu #define P9_STATS_UID 0x00000004ULL 107260ce86c7SWei Liu #define P9_STATS_GID 0x00000008ULL 107360ce86c7SWei Liu #define P9_STATS_RDEV 0x00000010ULL 107460ce86c7SWei Liu #define P9_STATS_ATIME 0x00000020ULL 107560ce86c7SWei Liu #define P9_STATS_MTIME 0x00000040ULL 107660ce86c7SWei Liu #define P9_STATS_CTIME 0x00000080ULL 107760ce86c7SWei Liu #define P9_STATS_INO 0x00000100ULL 107860ce86c7SWei Liu #define P9_STATS_SIZE 0x00000200ULL 107960ce86c7SWei Liu #define P9_STATS_BLOCKS 0x00000400ULL 108060ce86c7SWei Liu 108160ce86c7SWei Liu #define P9_STATS_BTIME 0x00000800ULL 108260ce86c7SWei Liu #define P9_STATS_GEN 0x00001000ULL 108360ce86c7SWei Liu #define P9_STATS_DATA_VERSION 0x00002000ULL 108460ce86c7SWei Liu 108560ce86c7SWei Liu #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */ 108660ce86c7SWei Liu #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */ 108760ce86c7SWei Liu 108860ce86c7SWei Liu 10893b5ee9e8SAntonios Motakis static int stat_to_v9stat_dotl(V9fsPDU *pdu, const struct stat *stbuf, 109060ce86c7SWei Liu V9fsStatDotl *v9lstat) 109160ce86c7SWei Liu { 109260ce86c7SWei Liu memset(v9lstat, 0, sizeof(*v9lstat)); 109360ce86c7SWei Liu 109460ce86c7SWei Liu v9lstat->st_mode = stbuf->st_mode; 109560ce86c7SWei Liu v9lstat->st_nlink = stbuf->st_nlink; 109660ce86c7SWei Liu v9lstat->st_uid = stbuf->st_uid; 109760ce86c7SWei Liu v9lstat->st_gid = stbuf->st_gid; 109860ce86c7SWei Liu v9lstat->st_rdev = stbuf->st_rdev; 109960ce86c7SWei Liu v9lstat->st_size = stbuf->st_size; 110060ce86c7SWei Liu v9lstat->st_blksize = stbuf->st_blksize; 110160ce86c7SWei Liu v9lstat->st_blocks = stbuf->st_blocks; 110260ce86c7SWei Liu v9lstat->st_atime_sec = stbuf->st_atime; 110360ce86c7SWei Liu v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec; 110460ce86c7SWei Liu v9lstat->st_mtime_sec = stbuf->st_mtime; 110560ce86c7SWei Liu v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec; 110660ce86c7SWei Liu v9lstat->st_ctime_sec = stbuf->st_ctime; 110760ce86c7SWei Liu v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec; 110860ce86c7SWei Liu /* Currently we only support BASIC fields in stat */ 110960ce86c7SWei Liu v9lstat->st_result_mask = P9_STATS_BASIC; 111060ce86c7SWei Liu 11113b5ee9e8SAntonios Motakis return stat_to_qid(pdu, stbuf, &v9lstat->qid); 111260ce86c7SWei Liu } 111360ce86c7SWei Liu 111460ce86c7SWei Liu static void print_sg(struct iovec *sg, int cnt) 111560ce86c7SWei Liu { 111660ce86c7SWei Liu int i; 111760ce86c7SWei Liu 111860ce86c7SWei Liu printf("sg[%d]: {", cnt); 111960ce86c7SWei Liu for (i = 0; i < cnt; i++) { 112060ce86c7SWei Liu if (i) { 112160ce86c7SWei Liu printf(", "); 112260ce86c7SWei Liu } 112360ce86c7SWei Liu printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len); 112460ce86c7SWei Liu } 112560ce86c7SWei Liu printf("}\n"); 112660ce86c7SWei Liu } 112760ce86c7SWei Liu 112860ce86c7SWei Liu /* Will call this only for path name based fid */ 112960ce86c7SWei Liu static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len) 113060ce86c7SWei Liu { 113160ce86c7SWei Liu V9fsPath str; 113260ce86c7SWei Liu v9fs_path_init(&str); 113360ce86c7SWei Liu v9fs_path_copy(&str, dst); 1134e3e83f2eSGreg Kurz v9fs_path_sprintf(dst, "%s%s", src->data, str.data + len); 113560ce86c7SWei Liu v9fs_path_free(&str); 113660ce86c7SWei Liu } 113760ce86c7SWei Liu 113860ce86c7SWei Liu static inline bool is_ro_export(FsContext *ctx) 113960ce86c7SWei Liu { 114060ce86c7SWei Liu return ctx->export_flags & V9FS_RDONLY; 114160ce86c7SWei Liu } 114260ce86c7SWei Liu 11438440e22eSGreg Kurz static void coroutine_fn v9fs_version(void *opaque) 114460ce86c7SWei Liu { 114560ce86c7SWei Liu ssize_t err; 114660ce86c7SWei Liu V9fsPDU *pdu = opaque; 114760ce86c7SWei Liu V9fsState *s = pdu->s; 114860ce86c7SWei Liu V9fsString version; 114960ce86c7SWei Liu size_t offset = 7; 115060ce86c7SWei Liu 115160ce86c7SWei Liu v9fs_string_init(&version); 115260ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "ds", &s->msize, &version); 115360ce86c7SWei Liu if (err < 0) { 115460ce86c7SWei Liu goto out; 115560ce86c7SWei Liu } 115660ce86c7SWei Liu trace_v9fs_version(pdu->tag, pdu->id, s->msize, version.data); 115760ce86c7SWei Liu 115860ce86c7SWei Liu virtfs_reset(pdu); 115960ce86c7SWei Liu 116060ce86c7SWei Liu if (!strcmp(version.data, "9P2000.u")) { 116160ce86c7SWei Liu s->proto_version = V9FS_PROTO_2000U; 116260ce86c7SWei Liu } else if (!strcmp(version.data, "9P2000.L")) { 116360ce86c7SWei Liu s->proto_version = V9FS_PROTO_2000L; 116460ce86c7SWei Liu } else { 116560ce86c7SWei Liu v9fs_string_sprintf(&version, "unknown"); 116660ce86c7SWei Liu } 116760ce86c7SWei Liu 116860ce86c7SWei Liu err = pdu_marshal(pdu, offset, "ds", s->msize, &version); 116960ce86c7SWei Liu if (err < 0) { 117060ce86c7SWei Liu goto out; 117160ce86c7SWei Liu } 1172403a905bSPhilippe Mathieu-Daudé err += offset; 117360ce86c7SWei Liu trace_v9fs_version_return(pdu->tag, pdu->id, s->msize, version.data); 117460ce86c7SWei Liu out: 1175403a905bSPhilippe Mathieu-Daudé pdu_complete(pdu, err); 117660ce86c7SWei Liu v9fs_string_free(&version); 117760ce86c7SWei Liu } 117860ce86c7SWei Liu 11798440e22eSGreg Kurz static void coroutine_fn v9fs_attach(void *opaque) 118060ce86c7SWei Liu { 118160ce86c7SWei Liu V9fsPDU *pdu = opaque; 118260ce86c7SWei Liu V9fsState *s = pdu->s; 118360ce86c7SWei Liu int32_t fid, afid, n_uname; 118460ce86c7SWei Liu V9fsString uname, aname; 118560ce86c7SWei Liu V9fsFidState *fidp; 118660ce86c7SWei Liu size_t offset = 7; 118760ce86c7SWei Liu V9fsQID qid; 118860ce86c7SWei Liu ssize_t err; 1189fe44dc91SAshijeet Acharya Error *local_err = NULL; 119060ce86c7SWei Liu 119160ce86c7SWei Liu v9fs_string_init(&uname); 119260ce86c7SWei Liu v9fs_string_init(&aname); 119360ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "ddssd", &fid, 119460ce86c7SWei Liu &afid, &uname, &aname, &n_uname); 119560ce86c7SWei Liu if (err < 0) { 119660ce86c7SWei Liu goto out_nofid; 119760ce86c7SWei Liu } 119860ce86c7SWei Liu trace_v9fs_attach(pdu->tag, pdu->id, fid, afid, uname.data, aname.data); 119960ce86c7SWei Liu 120060ce86c7SWei Liu fidp = alloc_fid(s, fid); 120160ce86c7SWei Liu if (fidp == NULL) { 120260ce86c7SWei Liu err = -EINVAL; 120360ce86c7SWei Liu goto out_nofid; 120460ce86c7SWei Liu } 120560ce86c7SWei Liu fidp->uid = n_uname; 120660ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, NULL, "/", &fidp->path); 120760ce86c7SWei Liu if (err < 0) { 120860ce86c7SWei Liu err = -EINVAL; 120960ce86c7SWei Liu clunk_fid(s, fid); 121060ce86c7SWei Liu goto out; 121160ce86c7SWei Liu } 121260ce86c7SWei Liu err = fid_to_qid(pdu, fidp, &qid); 121360ce86c7SWei Liu if (err < 0) { 121460ce86c7SWei Liu err = -EINVAL; 121560ce86c7SWei Liu clunk_fid(s, fid); 121660ce86c7SWei Liu goto out; 121760ce86c7SWei Liu } 1218fe44dc91SAshijeet Acharya 1219fe44dc91SAshijeet Acharya /* 1220fe44dc91SAshijeet Acharya * disable migration if we haven't done already. 1221fe44dc91SAshijeet Acharya * attach could get called multiple times for the same export. 1222fe44dc91SAshijeet Acharya */ 1223fe44dc91SAshijeet Acharya if (!s->migration_blocker) { 1224fe44dc91SAshijeet Acharya error_setg(&s->migration_blocker, 1225fe44dc91SAshijeet Acharya "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'", 1226fe44dc91SAshijeet Acharya s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag); 1227fe44dc91SAshijeet Acharya err = migrate_add_blocker(s->migration_blocker, &local_err); 1228fe44dc91SAshijeet Acharya if (local_err) { 1229fe44dc91SAshijeet Acharya error_free(local_err); 1230fe44dc91SAshijeet Acharya error_free(s->migration_blocker); 1231fe44dc91SAshijeet Acharya s->migration_blocker = NULL; 1232fe44dc91SAshijeet Acharya clunk_fid(s, fid); 1233fe44dc91SAshijeet Acharya goto out; 1234fe44dc91SAshijeet Acharya } 1235fe44dc91SAshijeet Acharya s->root_fid = fid; 1236fe44dc91SAshijeet Acharya } 1237fe44dc91SAshijeet Acharya 123860ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Q", &qid); 123960ce86c7SWei Liu if (err < 0) { 124060ce86c7SWei Liu clunk_fid(s, fid); 124160ce86c7SWei Liu goto out; 124260ce86c7SWei Liu } 124360ce86c7SWei Liu err += offset; 1244fe44dc91SAshijeet Acharya 124556f101ecSGreg Kurz memcpy(&s->root_qid, &qid, sizeof(qid)); 124660ce86c7SWei Liu trace_v9fs_attach_return(pdu->tag, pdu->id, 124760ce86c7SWei Liu qid.type, qid.version, qid.path); 124860ce86c7SWei Liu out: 124960ce86c7SWei Liu put_fid(pdu, fidp); 125060ce86c7SWei Liu out_nofid: 125160ce86c7SWei Liu pdu_complete(pdu, err); 125260ce86c7SWei Liu v9fs_string_free(&uname); 125360ce86c7SWei Liu v9fs_string_free(&aname); 125460ce86c7SWei Liu } 125560ce86c7SWei Liu 12568440e22eSGreg Kurz static void coroutine_fn v9fs_stat(void *opaque) 125760ce86c7SWei Liu { 125860ce86c7SWei Liu int32_t fid; 125960ce86c7SWei Liu V9fsStat v9stat; 126060ce86c7SWei Liu ssize_t err = 0; 126160ce86c7SWei Liu size_t offset = 7; 126260ce86c7SWei Liu struct stat stbuf; 126360ce86c7SWei Liu V9fsFidState *fidp; 126460ce86c7SWei Liu V9fsPDU *pdu = opaque; 12656069537fSJan Dakinevich char *basename; 126660ce86c7SWei Liu 126760ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "d", &fid); 126860ce86c7SWei Liu if (err < 0) { 126960ce86c7SWei Liu goto out_nofid; 127060ce86c7SWei Liu } 127160ce86c7SWei Liu trace_v9fs_stat(pdu->tag, pdu->id, fid); 127260ce86c7SWei Liu 127360ce86c7SWei Liu fidp = get_fid(pdu, fid); 127460ce86c7SWei Liu if (fidp == NULL) { 127560ce86c7SWei Liu err = -ENOENT; 127660ce86c7SWei Liu goto out_nofid; 127760ce86c7SWei Liu } 127860ce86c7SWei Liu err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); 127960ce86c7SWei Liu if (err < 0) { 128060ce86c7SWei Liu goto out; 128160ce86c7SWei Liu } 12826069537fSJan Dakinevich basename = g_path_get_basename(fidp->path.data); 12836069537fSJan Dakinevich err = stat_to_v9stat(pdu, &fidp->path, basename, &stbuf, &v9stat); 12846069537fSJan Dakinevich g_free(basename); 128560ce86c7SWei Liu if (err < 0) { 128660ce86c7SWei Liu goto out; 128760ce86c7SWei Liu } 128860ce86c7SWei Liu err = pdu_marshal(pdu, offset, "wS", 0, &v9stat); 128960ce86c7SWei Liu if (err < 0) { 129060ce86c7SWei Liu v9fs_stat_free(&v9stat); 129160ce86c7SWei Liu goto out; 129260ce86c7SWei Liu } 129360ce86c7SWei Liu trace_v9fs_stat_return(pdu->tag, pdu->id, v9stat.mode, 129460ce86c7SWei Liu v9stat.atime, v9stat.mtime, v9stat.length); 129560ce86c7SWei Liu err += offset; 129660ce86c7SWei Liu v9fs_stat_free(&v9stat); 129760ce86c7SWei Liu out: 129860ce86c7SWei Liu put_fid(pdu, fidp); 129960ce86c7SWei Liu out_nofid: 130060ce86c7SWei Liu pdu_complete(pdu, err); 130160ce86c7SWei Liu } 130260ce86c7SWei Liu 13038440e22eSGreg Kurz static void coroutine_fn v9fs_getattr(void *opaque) 130460ce86c7SWei Liu { 130560ce86c7SWei Liu int32_t fid; 130660ce86c7SWei Liu size_t offset = 7; 130760ce86c7SWei Liu ssize_t retval = 0; 130860ce86c7SWei Liu struct stat stbuf; 130960ce86c7SWei Liu V9fsFidState *fidp; 131060ce86c7SWei Liu uint64_t request_mask; 131160ce86c7SWei Liu V9fsStatDotl v9stat_dotl; 131260ce86c7SWei Liu V9fsPDU *pdu = opaque; 131360ce86c7SWei Liu 131460ce86c7SWei Liu retval = pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask); 131560ce86c7SWei Liu if (retval < 0) { 131660ce86c7SWei Liu goto out_nofid; 131760ce86c7SWei Liu } 131860ce86c7SWei Liu trace_v9fs_getattr(pdu->tag, pdu->id, fid, request_mask); 131960ce86c7SWei Liu 132060ce86c7SWei Liu fidp = get_fid(pdu, fid); 132160ce86c7SWei Liu if (fidp == NULL) { 132260ce86c7SWei Liu retval = -ENOENT; 132360ce86c7SWei Liu goto out_nofid; 132460ce86c7SWei Liu } 132560ce86c7SWei Liu /* 132660ce86c7SWei Liu * Currently we only support BASIC fields in stat, so there is no 132760ce86c7SWei Liu * need to look at request_mask. 132860ce86c7SWei Liu */ 132960ce86c7SWei Liu retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf); 133060ce86c7SWei Liu if (retval < 0) { 133160ce86c7SWei Liu goto out; 133260ce86c7SWei Liu } 13333b5ee9e8SAntonios Motakis retval = stat_to_v9stat_dotl(pdu, &stbuf, &v9stat_dotl); 13343b5ee9e8SAntonios Motakis if (retval < 0) { 13353b5ee9e8SAntonios Motakis goto out; 13363b5ee9e8SAntonios Motakis } 133760ce86c7SWei Liu 133860ce86c7SWei Liu /* fill st_gen if requested and supported by underlying fs */ 133960ce86c7SWei Liu if (request_mask & P9_STATS_GEN) { 134060ce86c7SWei Liu retval = v9fs_co_st_gen(pdu, &fidp->path, stbuf.st_mode, &v9stat_dotl); 134160ce86c7SWei Liu switch (retval) { 134260ce86c7SWei Liu case 0: 134360ce86c7SWei Liu /* we have valid st_gen: update result mask */ 134460ce86c7SWei Liu v9stat_dotl.st_result_mask |= P9_STATS_GEN; 134560ce86c7SWei Liu break; 134660ce86c7SWei Liu case -EINTR: 134760ce86c7SWei Liu /* request cancelled, e.g. by Tflush */ 134860ce86c7SWei Liu goto out; 134960ce86c7SWei Liu default: 135060ce86c7SWei Liu /* failed to get st_gen: not fatal, ignore */ 135160ce86c7SWei Liu break; 135260ce86c7SWei Liu } 135360ce86c7SWei Liu } 135460ce86c7SWei Liu retval = pdu_marshal(pdu, offset, "A", &v9stat_dotl); 135560ce86c7SWei Liu if (retval < 0) { 135660ce86c7SWei Liu goto out; 135760ce86c7SWei Liu } 135860ce86c7SWei Liu retval += offset; 135960ce86c7SWei Liu trace_v9fs_getattr_return(pdu->tag, pdu->id, v9stat_dotl.st_result_mask, 136060ce86c7SWei Liu v9stat_dotl.st_mode, v9stat_dotl.st_uid, 136160ce86c7SWei Liu v9stat_dotl.st_gid); 136260ce86c7SWei Liu out: 136360ce86c7SWei Liu put_fid(pdu, fidp); 136460ce86c7SWei Liu out_nofid: 136560ce86c7SWei Liu pdu_complete(pdu, retval); 136660ce86c7SWei Liu } 136760ce86c7SWei Liu 136860ce86c7SWei Liu /* Attribute flags */ 136960ce86c7SWei Liu #define P9_ATTR_MODE (1 << 0) 137060ce86c7SWei Liu #define P9_ATTR_UID (1 << 1) 137160ce86c7SWei Liu #define P9_ATTR_GID (1 << 2) 137260ce86c7SWei Liu #define P9_ATTR_SIZE (1 << 3) 137360ce86c7SWei Liu #define P9_ATTR_ATIME (1 << 4) 137460ce86c7SWei Liu #define P9_ATTR_MTIME (1 << 5) 137560ce86c7SWei Liu #define P9_ATTR_CTIME (1 << 6) 137660ce86c7SWei Liu #define P9_ATTR_ATIME_SET (1 << 7) 137760ce86c7SWei Liu #define P9_ATTR_MTIME_SET (1 << 8) 137860ce86c7SWei Liu 137960ce86c7SWei Liu #define P9_ATTR_MASK 127 138060ce86c7SWei Liu 13818440e22eSGreg Kurz static void coroutine_fn v9fs_setattr(void *opaque) 138260ce86c7SWei Liu { 138360ce86c7SWei Liu int err = 0; 138460ce86c7SWei Liu int32_t fid; 138560ce86c7SWei Liu V9fsFidState *fidp; 138660ce86c7SWei Liu size_t offset = 7; 138760ce86c7SWei Liu V9fsIattr v9iattr; 138860ce86c7SWei Liu V9fsPDU *pdu = opaque; 138960ce86c7SWei Liu 139060ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr); 139160ce86c7SWei Liu if (err < 0) { 139260ce86c7SWei Liu goto out_nofid; 139360ce86c7SWei Liu } 139460ce86c7SWei Liu 13958f9c64bfSGreg Kurz trace_v9fs_setattr(pdu->tag, pdu->id, fid, 13968f9c64bfSGreg Kurz v9iattr.valid, v9iattr.mode, v9iattr.uid, v9iattr.gid, 13978f9c64bfSGreg Kurz v9iattr.size, v9iattr.atime_sec, v9iattr.mtime_sec); 13988f9c64bfSGreg Kurz 139960ce86c7SWei Liu fidp = get_fid(pdu, fid); 140060ce86c7SWei Liu if (fidp == NULL) { 140160ce86c7SWei Liu err = -EINVAL; 140260ce86c7SWei Liu goto out_nofid; 140360ce86c7SWei Liu } 140460ce86c7SWei Liu if (v9iattr.valid & P9_ATTR_MODE) { 140560ce86c7SWei Liu err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode); 140660ce86c7SWei Liu if (err < 0) { 140760ce86c7SWei Liu goto out; 140860ce86c7SWei Liu } 140960ce86c7SWei Liu } 141060ce86c7SWei Liu if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) { 141160ce86c7SWei Liu struct timespec times[2]; 141260ce86c7SWei Liu if (v9iattr.valid & P9_ATTR_ATIME) { 141360ce86c7SWei Liu if (v9iattr.valid & P9_ATTR_ATIME_SET) { 141460ce86c7SWei Liu times[0].tv_sec = v9iattr.atime_sec; 141560ce86c7SWei Liu times[0].tv_nsec = v9iattr.atime_nsec; 141660ce86c7SWei Liu } else { 141760ce86c7SWei Liu times[0].tv_nsec = UTIME_NOW; 141860ce86c7SWei Liu } 141960ce86c7SWei Liu } else { 142060ce86c7SWei Liu times[0].tv_nsec = UTIME_OMIT; 142160ce86c7SWei Liu } 142260ce86c7SWei Liu if (v9iattr.valid & P9_ATTR_MTIME) { 142360ce86c7SWei Liu if (v9iattr.valid & P9_ATTR_MTIME_SET) { 142460ce86c7SWei Liu times[1].tv_sec = v9iattr.mtime_sec; 142560ce86c7SWei Liu times[1].tv_nsec = v9iattr.mtime_nsec; 142660ce86c7SWei Liu } else { 142760ce86c7SWei Liu times[1].tv_nsec = UTIME_NOW; 142860ce86c7SWei Liu } 142960ce86c7SWei Liu } else { 143060ce86c7SWei Liu times[1].tv_nsec = UTIME_OMIT; 143160ce86c7SWei Liu } 143260ce86c7SWei Liu err = v9fs_co_utimensat(pdu, &fidp->path, times); 143360ce86c7SWei Liu if (err < 0) { 143460ce86c7SWei Liu goto out; 143560ce86c7SWei Liu } 143660ce86c7SWei Liu } 143760ce86c7SWei Liu /* 143860ce86c7SWei Liu * If the only valid entry in iattr is ctime we can call 143960ce86c7SWei Liu * chown(-1,-1) to update the ctime of the file 144060ce86c7SWei Liu */ 144160ce86c7SWei Liu if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) || 144260ce86c7SWei Liu ((v9iattr.valid & P9_ATTR_CTIME) 144360ce86c7SWei Liu && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) { 144460ce86c7SWei Liu if (!(v9iattr.valid & P9_ATTR_UID)) { 144560ce86c7SWei Liu v9iattr.uid = -1; 144660ce86c7SWei Liu } 144760ce86c7SWei Liu if (!(v9iattr.valid & P9_ATTR_GID)) { 144860ce86c7SWei Liu v9iattr.gid = -1; 144960ce86c7SWei Liu } 145060ce86c7SWei Liu err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid, 145160ce86c7SWei Liu v9iattr.gid); 145260ce86c7SWei Liu if (err < 0) { 145360ce86c7SWei Liu goto out; 145460ce86c7SWei Liu } 145560ce86c7SWei Liu } 145660ce86c7SWei Liu if (v9iattr.valid & (P9_ATTR_SIZE)) { 145760ce86c7SWei Liu err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size); 145860ce86c7SWei Liu if (err < 0) { 145960ce86c7SWei Liu goto out; 146060ce86c7SWei Liu } 146160ce86c7SWei Liu } 146260ce86c7SWei Liu err = offset; 14638f9c64bfSGreg Kurz trace_v9fs_setattr_return(pdu->tag, pdu->id); 146460ce86c7SWei Liu out: 146560ce86c7SWei Liu put_fid(pdu, fidp); 146660ce86c7SWei Liu out_nofid: 146760ce86c7SWei Liu pdu_complete(pdu, err); 146860ce86c7SWei Liu } 146960ce86c7SWei Liu 147060ce86c7SWei Liu static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids) 147160ce86c7SWei Liu { 147260ce86c7SWei Liu int i; 147360ce86c7SWei Liu ssize_t err; 147460ce86c7SWei Liu size_t offset = 7; 147560ce86c7SWei Liu 147660ce86c7SWei Liu err = pdu_marshal(pdu, offset, "w", nwnames); 147760ce86c7SWei Liu if (err < 0) { 147860ce86c7SWei Liu return err; 147960ce86c7SWei Liu } 148060ce86c7SWei Liu offset += err; 148160ce86c7SWei Liu for (i = 0; i < nwnames; i++) { 148260ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Q", &qids[i]); 148360ce86c7SWei Liu if (err < 0) { 148460ce86c7SWei Liu return err; 148560ce86c7SWei Liu } 148660ce86c7SWei Liu offset += err; 148760ce86c7SWei Liu } 148860ce86c7SWei Liu return offset; 148960ce86c7SWei Liu } 149060ce86c7SWei Liu 1491fff39a7aSGreg Kurz static bool name_is_illegal(const char *name) 1492fff39a7aSGreg Kurz { 1493fff39a7aSGreg Kurz return !*name || strchr(name, '/') != NULL; 1494fff39a7aSGreg Kurz } 1495fff39a7aSGreg Kurz 149656f101ecSGreg Kurz static bool not_same_qid(const V9fsQID *qid1, const V9fsQID *qid2) 149756f101ecSGreg Kurz { 149856f101ecSGreg Kurz return 149956f101ecSGreg Kurz qid1->type != qid2->type || 150056f101ecSGreg Kurz qid1->version != qid2->version || 150156f101ecSGreg Kurz qid1->path != qid2->path; 150256f101ecSGreg Kurz } 150356f101ecSGreg Kurz 15048440e22eSGreg Kurz static void coroutine_fn v9fs_walk(void *opaque) 150560ce86c7SWei Liu { 150660ce86c7SWei Liu int name_idx; 150760ce86c7SWei Liu V9fsQID *qids = NULL; 150860ce86c7SWei Liu int i, err = 0; 150960ce86c7SWei Liu V9fsPath dpath, path; 151060ce86c7SWei Liu uint16_t nwnames; 151160ce86c7SWei Liu struct stat stbuf; 151260ce86c7SWei Liu size_t offset = 7; 151360ce86c7SWei Liu int32_t fid, newfid; 151460ce86c7SWei Liu V9fsString *wnames = NULL; 151560ce86c7SWei Liu V9fsFidState *fidp; 151660ce86c7SWei Liu V9fsFidState *newfidp = NULL; 151760ce86c7SWei Liu V9fsPDU *pdu = opaque; 151860ce86c7SWei Liu V9fsState *s = pdu->s; 151956f101ecSGreg Kurz V9fsQID qid; 152060ce86c7SWei Liu 152160ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames); 152260ce86c7SWei Liu if (err < 0) { 152360ce86c7SWei Liu pdu_complete(pdu, err); 152460ce86c7SWei Liu return ; 152560ce86c7SWei Liu } 152660ce86c7SWei Liu offset += err; 152760ce86c7SWei Liu 152860ce86c7SWei Liu trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames); 152960ce86c7SWei Liu 153060ce86c7SWei Liu if (nwnames && nwnames <= P9_MAXWELEM) { 15311923923bSGreg Kurz wnames = g_new0(V9fsString, nwnames); 15321923923bSGreg Kurz qids = g_new0(V9fsQID, nwnames); 153360ce86c7SWei Liu for (i = 0; i < nwnames; i++) { 153460ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "s", &wnames[i]); 153560ce86c7SWei Liu if (err < 0) { 153660ce86c7SWei Liu goto out_nofid; 153760ce86c7SWei Liu } 1538fff39a7aSGreg Kurz if (name_is_illegal(wnames[i].data)) { 1539fff39a7aSGreg Kurz err = -ENOENT; 1540fff39a7aSGreg Kurz goto out_nofid; 1541fff39a7aSGreg Kurz } 154260ce86c7SWei Liu offset += err; 154360ce86c7SWei Liu } 154460ce86c7SWei Liu } else if (nwnames > P9_MAXWELEM) { 154560ce86c7SWei Liu err = -EINVAL; 154660ce86c7SWei Liu goto out_nofid; 154760ce86c7SWei Liu } 154860ce86c7SWei Liu fidp = get_fid(pdu, fid); 154960ce86c7SWei Liu if (fidp == NULL) { 155060ce86c7SWei Liu err = -ENOENT; 155160ce86c7SWei Liu goto out_nofid; 155260ce86c7SWei Liu } 155356f101ecSGreg Kurz 155413fd08e6SGreg Kurz v9fs_path_init(&dpath); 155513fd08e6SGreg Kurz v9fs_path_init(&path); 155613fd08e6SGreg Kurz 155756f101ecSGreg Kurz err = fid_to_qid(pdu, fidp, &qid); 155856f101ecSGreg Kurz if (err < 0) { 155956f101ecSGreg Kurz goto out; 156056f101ecSGreg Kurz } 156156f101ecSGreg Kurz 156260ce86c7SWei Liu /* 156360ce86c7SWei Liu * Both dpath and path initially poin to fidp. 156460ce86c7SWei Liu * Needed to handle request with nwnames == 0 156560ce86c7SWei Liu */ 156660ce86c7SWei Liu v9fs_path_copy(&dpath, &fidp->path); 156760ce86c7SWei Liu v9fs_path_copy(&path, &fidp->path); 156860ce86c7SWei Liu for (name_idx = 0; name_idx < nwnames; name_idx++) { 156956f101ecSGreg Kurz if (not_same_qid(&pdu->s->root_qid, &qid) || 157056f101ecSGreg Kurz strcmp("..", wnames[name_idx].data)) { 157156f101ecSGreg Kurz err = v9fs_co_name_to_path(pdu, &dpath, wnames[name_idx].data, 157256f101ecSGreg Kurz &path); 157360ce86c7SWei Liu if (err < 0) { 157460ce86c7SWei Liu goto out; 157560ce86c7SWei Liu } 157656f101ecSGreg Kurz 157760ce86c7SWei Liu err = v9fs_co_lstat(pdu, &path, &stbuf); 157860ce86c7SWei Liu if (err < 0) { 157960ce86c7SWei Liu goto out; 158060ce86c7SWei Liu } 15813b5ee9e8SAntonios Motakis err = stat_to_qid(pdu, &stbuf, &qid); 15823b5ee9e8SAntonios Motakis if (err < 0) { 15833b5ee9e8SAntonios Motakis goto out; 15843b5ee9e8SAntonios Motakis } 158560ce86c7SWei Liu v9fs_path_copy(&dpath, &path); 158660ce86c7SWei Liu } 158756f101ecSGreg Kurz memcpy(&qids[name_idx], &qid, sizeof(qid)); 158856f101ecSGreg Kurz } 158960ce86c7SWei Liu if (fid == newfid) { 159049dd946bSGreg Kurz if (fidp->fid_type != P9_FID_NONE) { 159149dd946bSGreg Kurz err = -EINVAL; 159249dd946bSGreg Kurz goto out; 159349dd946bSGreg Kurz } 15945b3c77aaSGreg Kurz v9fs_path_write_lock(s); 159560ce86c7SWei Liu v9fs_path_copy(&fidp->path, &path); 15965b3c77aaSGreg Kurz v9fs_path_unlock(s); 159760ce86c7SWei Liu } else { 159860ce86c7SWei Liu newfidp = alloc_fid(s, newfid); 159960ce86c7SWei Liu if (newfidp == NULL) { 160060ce86c7SWei Liu err = -EINVAL; 160160ce86c7SWei Liu goto out; 160260ce86c7SWei Liu } 160360ce86c7SWei Liu newfidp->uid = fidp->uid; 160460ce86c7SWei Liu v9fs_path_copy(&newfidp->path, &path); 160560ce86c7SWei Liu } 160660ce86c7SWei Liu err = v9fs_walk_marshal(pdu, nwnames, qids); 160760ce86c7SWei Liu trace_v9fs_walk_return(pdu->tag, pdu->id, nwnames, qids); 160860ce86c7SWei Liu out: 160960ce86c7SWei Liu put_fid(pdu, fidp); 161060ce86c7SWei Liu if (newfidp) { 161160ce86c7SWei Liu put_fid(pdu, newfidp); 161260ce86c7SWei Liu } 161360ce86c7SWei Liu v9fs_path_free(&dpath); 161460ce86c7SWei Liu v9fs_path_free(&path); 161560ce86c7SWei Liu out_nofid: 161660ce86c7SWei Liu pdu_complete(pdu, err); 161760ce86c7SWei Liu if (nwnames && nwnames <= P9_MAXWELEM) { 161860ce86c7SWei Liu for (name_idx = 0; name_idx < nwnames; name_idx++) { 161960ce86c7SWei Liu v9fs_string_free(&wnames[name_idx]); 162060ce86c7SWei Liu } 162160ce86c7SWei Liu g_free(wnames); 162260ce86c7SWei Liu g_free(qids); 162360ce86c7SWei Liu } 162460ce86c7SWei Liu } 162560ce86c7SWei Liu 16268440e22eSGreg Kurz static int32_t coroutine_fn get_iounit(V9fsPDU *pdu, V9fsPath *path) 162760ce86c7SWei Liu { 162860ce86c7SWei Liu struct statfs stbuf; 162960ce86c7SWei Liu int32_t iounit = 0; 163060ce86c7SWei Liu V9fsState *s = pdu->s; 163160ce86c7SWei Liu 163260ce86c7SWei Liu /* 163360ce86c7SWei Liu * iounit should be multiples of f_bsize (host filesystem block size 163460ce86c7SWei Liu * and as well as less than (client msize - P9_IOHDRSZ)) 163560ce86c7SWei Liu */ 163660ce86c7SWei Liu if (!v9fs_co_statfs(pdu, path, &stbuf)) { 163760ce86c7SWei Liu iounit = stbuf.f_bsize; 163860ce86c7SWei Liu iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize; 163960ce86c7SWei Liu } 164060ce86c7SWei Liu if (!iounit) { 164160ce86c7SWei Liu iounit = s->msize - P9_IOHDRSZ; 164260ce86c7SWei Liu } 164360ce86c7SWei Liu return iounit; 164460ce86c7SWei Liu } 164560ce86c7SWei Liu 16468440e22eSGreg Kurz static void coroutine_fn v9fs_open(void *opaque) 164760ce86c7SWei Liu { 164860ce86c7SWei Liu int flags; 164960ce86c7SWei Liu int32_t fid; 165060ce86c7SWei Liu int32_t mode; 165160ce86c7SWei Liu V9fsQID qid; 165260ce86c7SWei Liu int iounit = 0; 165360ce86c7SWei Liu ssize_t err = 0; 165460ce86c7SWei Liu size_t offset = 7; 165560ce86c7SWei Liu struct stat stbuf; 165660ce86c7SWei Liu V9fsFidState *fidp; 165760ce86c7SWei Liu V9fsPDU *pdu = opaque; 165860ce86c7SWei Liu V9fsState *s = pdu->s; 165960ce86c7SWei Liu 166060ce86c7SWei Liu if (s->proto_version == V9FS_PROTO_2000L) { 166160ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode); 166260ce86c7SWei Liu } else { 166360ce86c7SWei Liu uint8_t modebyte; 166460ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte); 166560ce86c7SWei Liu mode = modebyte; 166660ce86c7SWei Liu } 166760ce86c7SWei Liu if (err < 0) { 166860ce86c7SWei Liu goto out_nofid; 166960ce86c7SWei Liu } 167060ce86c7SWei Liu trace_v9fs_open(pdu->tag, pdu->id, fid, mode); 167160ce86c7SWei Liu 167260ce86c7SWei Liu fidp = get_fid(pdu, fid); 167360ce86c7SWei Liu if (fidp == NULL) { 167460ce86c7SWei Liu err = -ENOENT; 167560ce86c7SWei Liu goto out_nofid; 167660ce86c7SWei Liu } 167749dd946bSGreg Kurz if (fidp->fid_type != P9_FID_NONE) { 167849dd946bSGreg Kurz err = -EINVAL; 167949dd946bSGreg Kurz goto out; 168049dd946bSGreg Kurz } 168160ce86c7SWei Liu 168260ce86c7SWei Liu err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); 168360ce86c7SWei Liu if (err < 0) { 168460ce86c7SWei Liu goto out; 168560ce86c7SWei Liu } 16863b5ee9e8SAntonios Motakis err = stat_to_qid(pdu, &stbuf, &qid); 16873b5ee9e8SAntonios Motakis if (err < 0) { 16883b5ee9e8SAntonios Motakis goto out; 16893b5ee9e8SAntonios Motakis } 169060ce86c7SWei Liu if (S_ISDIR(stbuf.st_mode)) { 169160ce86c7SWei Liu err = v9fs_co_opendir(pdu, fidp); 169260ce86c7SWei Liu if (err < 0) { 169360ce86c7SWei Liu goto out; 169460ce86c7SWei Liu } 169560ce86c7SWei Liu fidp->fid_type = P9_FID_DIR; 169660ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Qd", &qid, 0); 169760ce86c7SWei Liu if (err < 0) { 169860ce86c7SWei Liu goto out; 169960ce86c7SWei Liu } 170060ce86c7SWei Liu err += offset; 170160ce86c7SWei Liu } else { 170260ce86c7SWei Liu if (s->proto_version == V9FS_PROTO_2000L) { 170360ce86c7SWei Liu flags = get_dotl_openflags(s, mode); 170460ce86c7SWei Liu } else { 170560ce86c7SWei Liu flags = omode_to_uflags(mode); 170660ce86c7SWei Liu } 170760ce86c7SWei Liu if (is_ro_export(&s->ctx)) { 170860ce86c7SWei Liu if (mode & O_WRONLY || mode & O_RDWR || 170960ce86c7SWei Liu mode & O_APPEND || mode & O_TRUNC) { 171060ce86c7SWei Liu err = -EROFS; 171160ce86c7SWei Liu goto out; 171260ce86c7SWei Liu } 171360ce86c7SWei Liu } 171460ce86c7SWei Liu err = v9fs_co_open(pdu, fidp, flags); 171560ce86c7SWei Liu if (err < 0) { 171660ce86c7SWei Liu goto out; 171760ce86c7SWei Liu } 171860ce86c7SWei Liu fidp->fid_type = P9_FID_FILE; 171960ce86c7SWei Liu fidp->open_flags = flags; 172060ce86c7SWei Liu if (flags & O_EXCL) { 172160ce86c7SWei Liu /* 172260ce86c7SWei Liu * We let the host file system do O_EXCL check 172360ce86c7SWei Liu * We should not reclaim such fd 172460ce86c7SWei Liu */ 172560ce86c7SWei Liu fidp->flags |= FID_NON_RECLAIMABLE; 172660ce86c7SWei Liu } 172760ce86c7SWei Liu iounit = get_iounit(pdu, &fidp->path); 172860ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Qd", &qid, iounit); 172960ce86c7SWei Liu if (err < 0) { 173060ce86c7SWei Liu goto out; 173160ce86c7SWei Liu } 173260ce86c7SWei Liu err += offset; 173360ce86c7SWei Liu } 173460ce86c7SWei Liu trace_v9fs_open_return(pdu->tag, pdu->id, 173560ce86c7SWei Liu qid.type, qid.version, qid.path, iounit); 173660ce86c7SWei Liu out: 173760ce86c7SWei Liu put_fid(pdu, fidp); 173860ce86c7SWei Liu out_nofid: 173960ce86c7SWei Liu pdu_complete(pdu, err); 174060ce86c7SWei Liu } 174160ce86c7SWei Liu 17428440e22eSGreg Kurz static void coroutine_fn v9fs_lcreate(void *opaque) 174360ce86c7SWei Liu { 174460ce86c7SWei Liu int32_t dfid, flags, mode; 174560ce86c7SWei Liu gid_t gid; 174660ce86c7SWei Liu ssize_t err = 0; 174760ce86c7SWei Liu ssize_t offset = 7; 174860ce86c7SWei Liu V9fsString name; 174960ce86c7SWei Liu V9fsFidState *fidp; 175060ce86c7SWei Liu struct stat stbuf; 175160ce86c7SWei Liu V9fsQID qid; 175260ce86c7SWei Liu int32_t iounit; 175360ce86c7SWei Liu V9fsPDU *pdu = opaque; 175460ce86c7SWei Liu 175560ce86c7SWei Liu v9fs_string_init(&name); 175660ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dsddd", &dfid, 175760ce86c7SWei Liu &name, &flags, &mode, &gid); 175860ce86c7SWei Liu if (err < 0) { 175960ce86c7SWei Liu goto out_nofid; 176060ce86c7SWei Liu } 176160ce86c7SWei Liu trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid); 176260ce86c7SWei Liu 1763fff39a7aSGreg Kurz if (name_is_illegal(name.data)) { 1764fff39a7aSGreg Kurz err = -ENOENT; 1765fff39a7aSGreg Kurz goto out_nofid; 1766fff39a7aSGreg Kurz } 1767fff39a7aSGreg Kurz 1768805b5d98SGreg Kurz if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 1769805b5d98SGreg Kurz err = -EEXIST; 1770805b5d98SGreg Kurz goto out_nofid; 1771805b5d98SGreg Kurz } 1772805b5d98SGreg Kurz 177360ce86c7SWei Liu fidp = get_fid(pdu, dfid); 177460ce86c7SWei Liu if (fidp == NULL) { 177560ce86c7SWei Liu err = -ENOENT; 177660ce86c7SWei Liu goto out_nofid; 177760ce86c7SWei Liu } 1778d63fb193SLi Qiang if (fidp->fid_type != P9_FID_NONE) { 1779d63fb193SLi Qiang err = -EINVAL; 1780d63fb193SLi Qiang goto out; 1781d63fb193SLi Qiang } 178260ce86c7SWei Liu 178360ce86c7SWei Liu flags = get_dotl_openflags(pdu->s, flags); 178460ce86c7SWei Liu err = v9fs_co_open2(pdu, fidp, &name, gid, 178560ce86c7SWei Liu flags | O_CREAT, mode, &stbuf); 178660ce86c7SWei Liu if (err < 0) { 178760ce86c7SWei Liu goto out; 178860ce86c7SWei Liu } 178960ce86c7SWei Liu fidp->fid_type = P9_FID_FILE; 179060ce86c7SWei Liu fidp->open_flags = flags; 179160ce86c7SWei Liu if (flags & O_EXCL) { 179260ce86c7SWei Liu /* 179360ce86c7SWei Liu * We let the host file system do O_EXCL check 179460ce86c7SWei Liu * We should not reclaim such fd 179560ce86c7SWei Liu */ 179660ce86c7SWei Liu fidp->flags |= FID_NON_RECLAIMABLE; 179760ce86c7SWei Liu } 179860ce86c7SWei Liu iounit = get_iounit(pdu, &fidp->path); 17993b5ee9e8SAntonios Motakis err = stat_to_qid(pdu, &stbuf, &qid); 18003b5ee9e8SAntonios Motakis if (err < 0) { 18013b5ee9e8SAntonios Motakis goto out; 18023b5ee9e8SAntonios Motakis } 180360ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Qd", &qid, iounit); 180460ce86c7SWei Liu if (err < 0) { 180560ce86c7SWei Liu goto out; 180660ce86c7SWei Liu } 180760ce86c7SWei Liu err += offset; 180860ce86c7SWei Liu trace_v9fs_lcreate_return(pdu->tag, pdu->id, 180960ce86c7SWei Liu qid.type, qid.version, qid.path, iounit); 181060ce86c7SWei Liu out: 181160ce86c7SWei Liu put_fid(pdu, fidp); 181260ce86c7SWei Liu out_nofid: 181360ce86c7SWei Liu pdu_complete(pdu, err); 181460ce86c7SWei Liu v9fs_string_free(&name); 181560ce86c7SWei Liu } 181660ce86c7SWei Liu 1817a1bf8b74SGreg Kurz static void coroutine_fn v9fs_fsync(void *opaque) 181860ce86c7SWei Liu { 181960ce86c7SWei Liu int err; 182060ce86c7SWei Liu int32_t fid; 182160ce86c7SWei Liu int datasync; 182260ce86c7SWei Liu size_t offset = 7; 182360ce86c7SWei Liu V9fsFidState *fidp; 182460ce86c7SWei Liu V9fsPDU *pdu = opaque; 182560ce86c7SWei Liu 182660ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync); 182760ce86c7SWei Liu if (err < 0) { 182860ce86c7SWei Liu goto out_nofid; 182960ce86c7SWei Liu } 183060ce86c7SWei Liu trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync); 183160ce86c7SWei Liu 183260ce86c7SWei Liu fidp = get_fid(pdu, fid); 183360ce86c7SWei Liu if (fidp == NULL) { 183460ce86c7SWei Liu err = -ENOENT; 183560ce86c7SWei Liu goto out_nofid; 183660ce86c7SWei Liu } 183760ce86c7SWei Liu err = v9fs_co_fsync(pdu, fidp, datasync); 183860ce86c7SWei Liu if (!err) { 183960ce86c7SWei Liu err = offset; 184060ce86c7SWei Liu } 184160ce86c7SWei Liu put_fid(pdu, fidp); 184260ce86c7SWei Liu out_nofid: 184360ce86c7SWei Liu pdu_complete(pdu, err); 184460ce86c7SWei Liu } 184560ce86c7SWei Liu 18468440e22eSGreg Kurz static void coroutine_fn v9fs_clunk(void *opaque) 184760ce86c7SWei Liu { 184860ce86c7SWei Liu int err; 184960ce86c7SWei Liu int32_t fid; 185060ce86c7SWei Liu size_t offset = 7; 185160ce86c7SWei Liu V9fsFidState *fidp; 185260ce86c7SWei Liu V9fsPDU *pdu = opaque; 185360ce86c7SWei Liu V9fsState *s = pdu->s; 185460ce86c7SWei Liu 185560ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "d", &fid); 185660ce86c7SWei Liu if (err < 0) { 185760ce86c7SWei Liu goto out_nofid; 185860ce86c7SWei Liu } 185960ce86c7SWei Liu trace_v9fs_clunk(pdu->tag, pdu->id, fid); 186060ce86c7SWei Liu 186160ce86c7SWei Liu fidp = clunk_fid(s, fid); 186260ce86c7SWei Liu if (fidp == NULL) { 186360ce86c7SWei Liu err = -ENOENT; 186460ce86c7SWei Liu goto out_nofid; 186560ce86c7SWei Liu } 186660ce86c7SWei Liu /* 186760ce86c7SWei Liu * Bump the ref so that put_fid will 186860ce86c7SWei Liu * free the fid. 186960ce86c7SWei Liu */ 187060ce86c7SWei Liu fidp->ref++; 187160ce86c7SWei Liu err = put_fid(pdu, fidp); 187260ce86c7SWei Liu if (!err) { 187360ce86c7SWei Liu err = offset; 187460ce86c7SWei Liu } 187560ce86c7SWei Liu out_nofid: 187660ce86c7SWei Liu pdu_complete(pdu, err); 187760ce86c7SWei Liu } 187860ce86c7SWei Liu 1879bcb8998fSStefano Stabellini /* 1880bcb8998fSStefano Stabellini * Create a QEMUIOVector for a sub-region of PDU iovecs 1881bcb8998fSStefano Stabellini * 1882bcb8998fSStefano Stabellini * @qiov: uninitialized QEMUIOVector 1883bcb8998fSStefano Stabellini * @skip: number of bytes to skip from beginning of PDU 1884bcb8998fSStefano Stabellini * @size: number of bytes to include 1885bcb8998fSStefano Stabellini * @is_write: true - write, false - read 1886bcb8998fSStefano Stabellini * 1887bcb8998fSStefano Stabellini * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up 1888bcb8998fSStefano Stabellini * with qemu_iovec_destroy(). 1889bcb8998fSStefano Stabellini */ 1890bcb8998fSStefano Stabellini static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu, 1891bcb8998fSStefano Stabellini size_t skip, size_t size, 1892bcb8998fSStefano Stabellini bool is_write) 1893bcb8998fSStefano Stabellini { 1894bcb8998fSStefano Stabellini QEMUIOVector elem; 1895bcb8998fSStefano Stabellini struct iovec *iov; 1896bcb8998fSStefano Stabellini unsigned int niov; 1897bcb8998fSStefano Stabellini 189888da0b03SStefano Stabellini if (is_write) { 18998d37de41SGreg Kurz pdu->s->transport->init_out_iov_from_pdu(pdu, &iov, &niov, size + skip); 190088da0b03SStefano Stabellini } else { 1901fa0eb5c5SGreg Kurz pdu->s->transport->init_in_iov_from_pdu(pdu, &iov, &niov, size + skip); 190288da0b03SStefano Stabellini } 1903bcb8998fSStefano Stabellini 1904bcb8998fSStefano Stabellini qemu_iovec_init_external(&elem, iov, niov); 1905bcb8998fSStefano Stabellini qemu_iovec_init(qiov, niov); 1906bcb8998fSStefano Stabellini qemu_iovec_concat(qiov, &elem, skip, size); 1907bcb8998fSStefano Stabellini } 1908bcb8998fSStefano Stabellini 190960ce86c7SWei Liu static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp, 191060ce86c7SWei Liu uint64_t off, uint32_t max_count) 191160ce86c7SWei Liu { 191260ce86c7SWei Liu ssize_t err; 191360ce86c7SWei Liu size_t offset = 7; 19147e55d65cSLi Qiang uint64_t read_count; 1915bcb8998fSStefano Stabellini QEMUIOVector qiov_full; 191660ce86c7SWei Liu 19177e55d65cSLi Qiang if (fidp->fs.xattr.len < off) { 19187e55d65cSLi Qiang read_count = 0; 19197e55d65cSLi Qiang } else { 19207e55d65cSLi Qiang read_count = fidp->fs.xattr.len - off; 19217e55d65cSLi Qiang } 192260ce86c7SWei Liu if (read_count > max_count) { 192360ce86c7SWei Liu read_count = max_count; 192460ce86c7SWei Liu } 192560ce86c7SWei Liu err = pdu_marshal(pdu, offset, "d", read_count); 192660ce86c7SWei Liu if (err < 0) { 192760ce86c7SWei Liu return err; 192860ce86c7SWei Liu } 192960ce86c7SWei Liu offset += err; 193000588a0aSWei Liu 1931fa0eb5c5SGreg Kurz v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, read_count, false); 1932fa0eb5c5SGreg Kurz err = v9fs_pack(qiov_full.iov, qiov_full.niov, 0, 193360ce86c7SWei Liu ((char *)fidp->fs.xattr.value) + off, 193460ce86c7SWei Liu read_count); 1935bcb8998fSStefano Stabellini qemu_iovec_destroy(&qiov_full); 193660ce86c7SWei Liu if (err < 0) { 193760ce86c7SWei Liu return err; 193860ce86c7SWei Liu } 193960ce86c7SWei Liu offset += err; 194060ce86c7SWei Liu return offset; 194160ce86c7SWei Liu } 194260ce86c7SWei Liu 19438440e22eSGreg Kurz static int coroutine_fn v9fs_do_readdir_with_stat(V9fsPDU *pdu, 19448440e22eSGreg Kurz V9fsFidState *fidp, 19458440e22eSGreg Kurz uint32_t max_count) 194660ce86c7SWei Liu { 194760ce86c7SWei Liu V9fsPath path; 194860ce86c7SWei Liu V9fsStat v9stat; 194960ce86c7SWei Liu int len, err = 0; 195060ce86c7SWei Liu int32_t count = 0; 195160ce86c7SWei Liu struct stat stbuf; 195260ce86c7SWei Liu off_t saved_dir_pos; 1953635324e8SGreg Kurz struct dirent *dent; 195460ce86c7SWei Liu 195560ce86c7SWei Liu /* save the directory position */ 195660ce86c7SWei Liu saved_dir_pos = v9fs_co_telldir(pdu, fidp); 195760ce86c7SWei Liu if (saved_dir_pos < 0) { 195860ce86c7SWei Liu return saved_dir_pos; 195960ce86c7SWei Liu } 196060ce86c7SWei Liu 196160ce86c7SWei Liu while (1) { 196260ce86c7SWei Liu v9fs_path_init(&path); 19637cde47d4SGreg Kurz 19647cde47d4SGreg Kurz v9fs_readdir_lock(&fidp->fs.dir); 19657cde47d4SGreg Kurz 1966635324e8SGreg Kurz err = v9fs_co_readdir(pdu, fidp, &dent); 1967635324e8SGreg Kurz if (err || !dent) { 196860ce86c7SWei Liu break; 196960ce86c7SWei Liu } 197060ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path); 197160ce86c7SWei Liu if (err < 0) { 19728762a46dSGreg Kurz break; 197360ce86c7SWei Liu } 197460ce86c7SWei Liu err = v9fs_co_lstat(pdu, &path, &stbuf); 197560ce86c7SWei Liu if (err < 0) { 19768762a46dSGreg Kurz break; 197760ce86c7SWei Liu } 19786069537fSJan Dakinevich err = stat_to_v9stat(pdu, &path, dent->d_name, &stbuf, &v9stat); 197960ce86c7SWei Liu if (err < 0) { 19808762a46dSGreg Kurz break; 198160ce86c7SWei Liu } 1982772a7369SJan Dakinevich if ((count + v9stat.size + 2) > max_count) { 19837cde47d4SGreg Kurz v9fs_readdir_unlock(&fidp->fs.dir); 19847cde47d4SGreg Kurz 198560ce86c7SWei Liu /* Ran out of buffer. Set dir back to old position and return */ 198660ce86c7SWei Liu v9fs_co_seekdir(pdu, fidp, saved_dir_pos); 198760ce86c7SWei Liu v9fs_stat_free(&v9stat); 198860ce86c7SWei Liu v9fs_path_free(&path); 198960ce86c7SWei Liu return count; 199060ce86c7SWei Liu } 1991772a7369SJan Dakinevich 1992772a7369SJan Dakinevich /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */ 1993772a7369SJan Dakinevich len = pdu_marshal(pdu, 11 + count, "S", &v9stat); 1994772a7369SJan Dakinevich 1995772a7369SJan Dakinevich v9fs_readdir_unlock(&fidp->fs.dir); 1996772a7369SJan Dakinevich 1997772a7369SJan Dakinevich if (len < 0) { 1998772a7369SJan Dakinevich v9fs_co_seekdir(pdu, fidp, saved_dir_pos); 1999772a7369SJan Dakinevich v9fs_stat_free(&v9stat); 2000772a7369SJan Dakinevich v9fs_path_free(&path); 2001772a7369SJan Dakinevich return len; 2002772a7369SJan Dakinevich } 200360ce86c7SWei Liu count += len; 200460ce86c7SWei Liu v9fs_stat_free(&v9stat); 200560ce86c7SWei Liu v9fs_path_free(&path); 200660ce86c7SWei Liu saved_dir_pos = dent->d_off; 200760ce86c7SWei Liu } 20088762a46dSGreg Kurz 20097cde47d4SGreg Kurz v9fs_readdir_unlock(&fidp->fs.dir); 20107cde47d4SGreg Kurz 201160ce86c7SWei Liu v9fs_path_free(&path); 201260ce86c7SWei Liu if (err < 0) { 201360ce86c7SWei Liu return err; 201460ce86c7SWei Liu } 201560ce86c7SWei Liu return count; 201660ce86c7SWei Liu } 201760ce86c7SWei Liu 20188440e22eSGreg Kurz static void coroutine_fn v9fs_read(void *opaque) 201960ce86c7SWei Liu { 202060ce86c7SWei Liu int32_t fid; 202160ce86c7SWei Liu uint64_t off; 202260ce86c7SWei Liu ssize_t err = 0; 202360ce86c7SWei Liu int32_t count = 0; 202460ce86c7SWei Liu size_t offset = 7; 202560ce86c7SWei Liu uint32_t max_count; 202660ce86c7SWei Liu V9fsFidState *fidp; 202760ce86c7SWei Liu V9fsPDU *pdu = opaque; 202860ce86c7SWei Liu V9fsState *s = pdu->s; 202960ce86c7SWei Liu 203060ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count); 203160ce86c7SWei Liu if (err < 0) { 203260ce86c7SWei Liu goto out_nofid; 203360ce86c7SWei Liu } 203460ce86c7SWei Liu trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count); 203560ce86c7SWei Liu 203660ce86c7SWei Liu fidp = get_fid(pdu, fid); 203760ce86c7SWei Liu if (fidp == NULL) { 203860ce86c7SWei Liu err = -EINVAL; 203960ce86c7SWei Liu goto out_nofid; 204060ce86c7SWei Liu } 204160ce86c7SWei Liu if (fidp->fid_type == P9_FID_DIR) { 204260ce86c7SWei Liu 204360ce86c7SWei Liu if (off == 0) { 204460ce86c7SWei Liu v9fs_co_rewinddir(pdu, fidp); 204560ce86c7SWei Liu } 204660ce86c7SWei Liu count = v9fs_do_readdir_with_stat(pdu, fidp, max_count); 204760ce86c7SWei Liu if (count < 0) { 204860ce86c7SWei Liu err = count; 204960ce86c7SWei Liu goto out; 205060ce86c7SWei Liu } 205160ce86c7SWei Liu err = pdu_marshal(pdu, offset, "d", count); 205260ce86c7SWei Liu if (err < 0) { 205360ce86c7SWei Liu goto out; 205460ce86c7SWei Liu } 205560ce86c7SWei Liu err += offset + count; 205660ce86c7SWei Liu } else if (fidp->fid_type == P9_FID_FILE) { 205760ce86c7SWei Liu QEMUIOVector qiov_full; 205860ce86c7SWei Liu QEMUIOVector qiov; 205960ce86c7SWei Liu int32_t len; 206060ce86c7SWei Liu 206160ce86c7SWei Liu v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, max_count, false); 206260ce86c7SWei Liu qemu_iovec_init(&qiov, qiov_full.niov); 206360ce86c7SWei Liu do { 206460ce86c7SWei Liu qemu_iovec_reset(&qiov); 206560ce86c7SWei Liu qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count); 206660ce86c7SWei Liu if (0) { 206760ce86c7SWei Liu print_sg(qiov.iov, qiov.niov); 206860ce86c7SWei Liu } 206960ce86c7SWei Liu /* Loop in case of EINTR */ 207060ce86c7SWei Liu do { 207160ce86c7SWei Liu len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off); 207260ce86c7SWei Liu if (len >= 0) { 207360ce86c7SWei Liu off += len; 207460ce86c7SWei Liu count += len; 207560ce86c7SWei Liu } 207660ce86c7SWei Liu } while (len == -EINTR && !pdu->cancelled); 207760ce86c7SWei Liu if (len < 0) { 207860ce86c7SWei Liu /* IO error return the error */ 207960ce86c7SWei Liu err = len; 2080e95c9a49SLi Qiang goto out_free_iovec; 208160ce86c7SWei Liu } 208260ce86c7SWei Liu } while (count < max_count && len > 0); 208360ce86c7SWei Liu err = pdu_marshal(pdu, offset, "d", count); 208460ce86c7SWei Liu if (err < 0) { 2085e95c9a49SLi Qiang goto out_free_iovec; 208660ce86c7SWei Liu } 208760ce86c7SWei Liu err += offset + count; 2088e95c9a49SLi Qiang out_free_iovec: 208960ce86c7SWei Liu qemu_iovec_destroy(&qiov); 209060ce86c7SWei Liu qemu_iovec_destroy(&qiov_full); 209160ce86c7SWei Liu } else if (fidp->fid_type == P9_FID_XATTR) { 209260ce86c7SWei Liu err = v9fs_xattr_read(s, pdu, fidp, off, max_count); 209360ce86c7SWei Liu } else { 209460ce86c7SWei Liu err = -EINVAL; 209560ce86c7SWei Liu } 209660ce86c7SWei Liu trace_v9fs_read_return(pdu->tag, pdu->id, count, err); 209760ce86c7SWei Liu out: 209860ce86c7SWei Liu put_fid(pdu, fidp); 209960ce86c7SWei Liu out_nofid: 210060ce86c7SWei Liu pdu_complete(pdu, err); 210160ce86c7SWei Liu } 210260ce86c7SWei Liu 210360ce86c7SWei Liu static size_t v9fs_readdir_data_size(V9fsString *name) 210460ce86c7SWei Liu { 210560ce86c7SWei Liu /* 210660ce86c7SWei Liu * Size of each dirent on the wire: size of qid (13) + size of offset (8) 210760ce86c7SWei Liu * size of type (1) + size of name.size (2) + strlen(name.data) 210860ce86c7SWei Liu */ 210960ce86c7SWei Liu return 24 + v9fs_string_size(name); 211060ce86c7SWei Liu } 211160ce86c7SWei Liu 21128440e22eSGreg Kurz static int coroutine_fn v9fs_do_readdir(V9fsPDU *pdu, V9fsFidState *fidp, 21138440e22eSGreg Kurz int32_t max_count) 211460ce86c7SWei Liu { 211560ce86c7SWei Liu size_t size; 211660ce86c7SWei Liu V9fsQID qid; 211760ce86c7SWei Liu V9fsString name; 211860ce86c7SWei Liu int len, err = 0; 211960ce86c7SWei Liu int32_t count = 0; 212060ce86c7SWei Liu off_t saved_dir_pos; 2121635324e8SGreg Kurz struct dirent *dent; 212260ce86c7SWei Liu 212360ce86c7SWei Liu /* save the directory position */ 212460ce86c7SWei Liu saved_dir_pos = v9fs_co_telldir(pdu, fidp); 212560ce86c7SWei Liu if (saved_dir_pos < 0) { 212660ce86c7SWei Liu return saved_dir_pos; 212760ce86c7SWei Liu } 212860ce86c7SWei Liu 212960ce86c7SWei Liu while (1) { 21307cde47d4SGreg Kurz v9fs_readdir_lock(&fidp->fs.dir); 21317cde47d4SGreg Kurz 2132635324e8SGreg Kurz err = v9fs_co_readdir(pdu, fidp, &dent); 2133635324e8SGreg Kurz if (err || !dent) { 213460ce86c7SWei Liu break; 213560ce86c7SWei Liu } 213660ce86c7SWei Liu v9fs_string_init(&name); 213760ce86c7SWei Liu v9fs_string_sprintf(&name, "%s", dent->d_name); 213860ce86c7SWei Liu if ((count + v9fs_readdir_data_size(&name)) > max_count) { 21397cde47d4SGreg Kurz v9fs_readdir_unlock(&fidp->fs.dir); 21407cde47d4SGreg Kurz 214160ce86c7SWei Liu /* Ran out of buffer. Set dir back to old position and return */ 214260ce86c7SWei Liu v9fs_co_seekdir(pdu, fidp, saved_dir_pos); 214360ce86c7SWei Liu v9fs_string_free(&name); 214460ce86c7SWei Liu return count; 214560ce86c7SWei Liu } 21461a6ed33cSAntonios Motakis 21471a6ed33cSAntonios Motakis if (pdu->s->ctx.export_flags & V9FS_REMAP_INODES) { 21481a6ed33cSAntonios Motakis /* 21491a6ed33cSAntonios Motakis * dirent_to_qid() implies expensive stat call for each entry, 21501a6ed33cSAntonios Motakis * we must do that here though since inode remapping requires 21511a6ed33cSAntonios Motakis * the device id, which in turn might be different for 21521a6ed33cSAntonios Motakis * different entries; we cannot make any assumption to avoid 21531a6ed33cSAntonios Motakis * that here. 21541a6ed33cSAntonios Motakis */ 21551a6ed33cSAntonios Motakis err = dirent_to_qid(pdu, fidp, dent, &qid); 21561a6ed33cSAntonios Motakis if (err < 0) { 21571a6ed33cSAntonios Motakis v9fs_readdir_unlock(&fidp->fs.dir); 21581a6ed33cSAntonios Motakis v9fs_co_seekdir(pdu, fidp, saved_dir_pos); 21591a6ed33cSAntonios Motakis v9fs_string_free(&name); 21601a6ed33cSAntonios Motakis return err; 21611a6ed33cSAntonios Motakis } 21621a6ed33cSAntonios Motakis } else { 216360ce86c7SWei Liu /* 216460ce86c7SWei Liu * Fill up just the path field of qid because the client uses 216560ce86c7SWei Liu * only that. To fill the entire qid structure we will have 21661a6ed33cSAntonios Motakis * to stat each dirent found, which is expensive. For the 21671a6ed33cSAntonios Motakis * latter reason we don't call dirent_to_qid() here. Only drawback 21681a6ed33cSAntonios Motakis * is that no multi-device export detection of stat_to_qid() 21691a6ed33cSAntonios Motakis * would be done and provided as error to the user here. But 21701a6ed33cSAntonios Motakis * user would get that error anyway when accessing those 21711a6ed33cSAntonios Motakis * files/dirs through other ways. 217260ce86c7SWei Liu */ 217360ce86c7SWei Liu size = MIN(sizeof(dent->d_ino), sizeof(qid.path)); 217460ce86c7SWei Liu memcpy(&qid.path, &dent->d_ino, size); 217560ce86c7SWei Liu /* Fill the other fields with dummy values */ 217660ce86c7SWei Liu qid.type = 0; 217760ce86c7SWei Liu qid.version = 0; 21781a6ed33cSAntonios Motakis } 217960ce86c7SWei Liu 218060ce86c7SWei Liu /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */ 218160ce86c7SWei Liu len = pdu_marshal(pdu, 11 + count, "Qqbs", 218260ce86c7SWei Liu &qid, dent->d_off, 218360ce86c7SWei Liu dent->d_type, &name); 21847cde47d4SGreg Kurz 21857cde47d4SGreg Kurz v9fs_readdir_unlock(&fidp->fs.dir); 21867cde47d4SGreg Kurz 218760ce86c7SWei Liu if (len < 0) { 218860ce86c7SWei Liu v9fs_co_seekdir(pdu, fidp, saved_dir_pos); 218960ce86c7SWei Liu v9fs_string_free(&name); 219060ce86c7SWei Liu return len; 219160ce86c7SWei Liu } 219260ce86c7SWei Liu count += len; 219360ce86c7SWei Liu v9fs_string_free(&name); 219460ce86c7SWei Liu saved_dir_pos = dent->d_off; 219560ce86c7SWei Liu } 21967cde47d4SGreg Kurz 21977cde47d4SGreg Kurz v9fs_readdir_unlock(&fidp->fs.dir); 21987cde47d4SGreg Kurz 219960ce86c7SWei Liu if (err < 0) { 220060ce86c7SWei Liu return err; 220160ce86c7SWei Liu } 220260ce86c7SWei Liu return count; 220360ce86c7SWei Liu } 220460ce86c7SWei Liu 22058440e22eSGreg Kurz static void coroutine_fn v9fs_readdir(void *opaque) 220660ce86c7SWei Liu { 220760ce86c7SWei Liu int32_t fid; 220860ce86c7SWei Liu V9fsFidState *fidp; 220960ce86c7SWei Liu ssize_t retval = 0; 221060ce86c7SWei Liu size_t offset = 7; 221160ce86c7SWei Liu uint64_t initial_offset; 221260ce86c7SWei Liu int32_t count; 221360ce86c7SWei Liu uint32_t max_count; 221460ce86c7SWei Liu V9fsPDU *pdu = opaque; 221560ce86c7SWei Liu 221660ce86c7SWei Liu retval = pdu_unmarshal(pdu, offset, "dqd", &fid, 221760ce86c7SWei Liu &initial_offset, &max_count); 221860ce86c7SWei Liu if (retval < 0) { 221960ce86c7SWei Liu goto out_nofid; 222060ce86c7SWei Liu } 222160ce86c7SWei Liu trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count); 222260ce86c7SWei Liu 222360ce86c7SWei Liu fidp = get_fid(pdu, fid); 222460ce86c7SWei Liu if (fidp == NULL) { 222560ce86c7SWei Liu retval = -EINVAL; 222660ce86c7SWei Liu goto out_nofid; 222760ce86c7SWei Liu } 2228f314ea4eSGreg Kurz if (!fidp->fs.dir.stream) { 222960ce86c7SWei Liu retval = -EINVAL; 223060ce86c7SWei Liu goto out; 223160ce86c7SWei Liu } 223260ce86c7SWei Liu if (initial_offset == 0) { 223360ce86c7SWei Liu v9fs_co_rewinddir(pdu, fidp); 223460ce86c7SWei Liu } else { 223560ce86c7SWei Liu v9fs_co_seekdir(pdu, fidp, initial_offset); 223660ce86c7SWei Liu } 223760ce86c7SWei Liu count = v9fs_do_readdir(pdu, fidp, max_count); 223860ce86c7SWei Liu if (count < 0) { 223960ce86c7SWei Liu retval = count; 224060ce86c7SWei Liu goto out; 224160ce86c7SWei Liu } 224260ce86c7SWei Liu retval = pdu_marshal(pdu, offset, "d", count); 224360ce86c7SWei Liu if (retval < 0) { 224460ce86c7SWei Liu goto out; 224560ce86c7SWei Liu } 224660ce86c7SWei Liu retval += count + offset; 224760ce86c7SWei Liu trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval); 224860ce86c7SWei Liu out: 224960ce86c7SWei Liu put_fid(pdu, fidp); 225060ce86c7SWei Liu out_nofid: 225160ce86c7SWei Liu pdu_complete(pdu, retval); 225260ce86c7SWei Liu } 225360ce86c7SWei Liu 225460ce86c7SWei Liu static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp, 225560ce86c7SWei Liu uint64_t off, uint32_t count, 225660ce86c7SWei Liu struct iovec *sg, int cnt) 225760ce86c7SWei Liu { 225860ce86c7SWei Liu int i, to_copy; 225960ce86c7SWei Liu ssize_t err = 0; 22607e55d65cSLi Qiang uint64_t write_count; 226160ce86c7SWei Liu size_t offset = 7; 226260ce86c7SWei Liu 226360ce86c7SWei Liu 22647e55d65cSLi Qiang if (fidp->fs.xattr.len < off) { 226560ce86c7SWei Liu err = -ENOSPC; 226660ce86c7SWei Liu goto out; 226760ce86c7SWei Liu } 22687e55d65cSLi Qiang write_count = fidp->fs.xattr.len - off; 22697e55d65cSLi Qiang if (write_count > count) { 22707e55d65cSLi Qiang write_count = count; 22717e55d65cSLi Qiang } 227260ce86c7SWei Liu err = pdu_marshal(pdu, offset, "d", write_count); 227360ce86c7SWei Liu if (err < 0) { 227460ce86c7SWei Liu return err; 227560ce86c7SWei Liu } 227660ce86c7SWei Liu err += offset; 227760ce86c7SWei Liu fidp->fs.xattr.copied_len += write_count; 227860ce86c7SWei Liu /* 227960ce86c7SWei Liu * Now copy the content from sg list 228060ce86c7SWei Liu */ 228160ce86c7SWei Liu for (i = 0; i < cnt; i++) { 228260ce86c7SWei Liu if (write_count > sg[i].iov_len) { 228360ce86c7SWei Liu to_copy = sg[i].iov_len; 228460ce86c7SWei Liu } else { 228560ce86c7SWei Liu to_copy = write_count; 228660ce86c7SWei Liu } 228760ce86c7SWei Liu memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy); 228860ce86c7SWei Liu /* updating vs->off since we are not using below */ 228960ce86c7SWei Liu off += to_copy; 229060ce86c7SWei Liu write_count -= to_copy; 229160ce86c7SWei Liu } 229260ce86c7SWei Liu out: 229360ce86c7SWei Liu return err; 229460ce86c7SWei Liu } 229560ce86c7SWei Liu 22968440e22eSGreg Kurz static void coroutine_fn v9fs_write(void *opaque) 229760ce86c7SWei Liu { 229860ce86c7SWei Liu ssize_t err; 229960ce86c7SWei Liu int32_t fid; 230060ce86c7SWei Liu uint64_t off; 230160ce86c7SWei Liu uint32_t count; 230260ce86c7SWei Liu int32_t len = 0; 230360ce86c7SWei Liu int32_t total = 0; 230460ce86c7SWei Liu size_t offset = 7; 230560ce86c7SWei Liu V9fsFidState *fidp; 230660ce86c7SWei Liu V9fsPDU *pdu = opaque; 230760ce86c7SWei Liu V9fsState *s = pdu->s; 230860ce86c7SWei Liu QEMUIOVector qiov_full; 230960ce86c7SWei Liu QEMUIOVector qiov; 231060ce86c7SWei Liu 231160ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count); 231260ce86c7SWei Liu if (err < 0) { 231360ce86c7SWei Liu pdu_complete(pdu, err); 231460ce86c7SWei Liu return; 231560ce86c7SWei Liu } 231660ce86c7SWei Liu offset += err; 231760ce86c7SWei Liu v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, count, true); 231860ce86c7SWei Liu trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov); 231960ce86c7SWei Liu 232060ce86c7SWei Liu fidp = get_fid(pdu, fid); 232160ce86c7SWei Liu if (fidp == NULL) { 232260ce86c7SWei Liu err = -EINVAL; 232360ce86c7SWei Liu goto out_nofid; 232460ce86c7SWei Liu } 232560ce86c7SWei Liu if (fidp->fid_type == P9_FID_FILE) { 232660ce86c7SWei Liu if (fidp->fs.fd == -1) { 232760ce86c7SWei Liu err = -EINVAL; 232860ce86c7SWei Liu goto out; 232960ce86c7SWei Liu } 233060ce86c7SWei Liu } else if (fidp->fid_type == P9_FID_XATTR) { 233160ce86c7SWei Liu /* 233260ce86c7SWei Liu * setxattr operation 233360ce86c7SWei Liu */ 233460ce86c7SWei Liu err = v9fs_xattr_write(s, pdu, fidp, off, count, 233560ce86c7SWei Liu qiov_full.iov, qiov_full.niov); 233660ce86c7SWei Liu goto out; 233760ce86c7SWei Liu } else { 233860ce86c7SWei Liu err = -EINVAL; 233960ce86c7SWei Liu goto out; 234060ce86c7SWei Liu } 234160ce86c7SWei Liu qemu_iovec_init(&qiov, qiov_full.niov); 234260ce86c7SWei Liu do { 234360ce86c7SWei Liu qemu_iovec_reset(&qiov); 234460ce86c7SWei Liu qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total); 234560ce86c7SWei Liu if (0) { 234660ce86c7SWei Liu print_sg(qiov.iov, qiov.niov); 234760ce86c7SWei Liu } 234860ce86c7SWei Liu /* Loop in case of EINTR */ 234960ce86c7SWei Liu do { 235060ce86c7SWei Liu len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off); 235160ce86c7SWei Liu if (len >= 0) { 235260ce86c7SWei Liu off += len; 235360ce86c7SWei Liu total += len; 235460ce86c7SWei Liu } 235560ce86c7SWei Liu } while (len == -EINTR && !pdu->cancelled); 235660ce86c7SWei Liu if (len < 0) { 235760ce86c7SWei Liu /* IO error return the error */ 235860ce86c7SWei Liu err = len; 235960ce86c7SWei Liu goto out_qiov; 236060ce86c7SWei Liu } 236160ce86c7SWei Liu } while (total < count && len > 0); 236260ce86c7SWei Liu 236360ce86c7SWei Liu offset = 7; 236460ce86c7SWei Liu err = pdu_marshal(pdu, offset, "d", total); 236560ce86c7SWei Liu if (err < 0) { 2366fdfcc9aeSLi Qiang goto out_qiov; 236760ce86c7SWei Liu } 236860ce86c7SWei Liu err += offset; 236960ce86c7SWei Liu trace_v9fs_write_return(pdu->tag, pdu->id, total, err); 237060ce86c7SWei Liu out_qiov: 237160ce86c7SWei Liu qemu_iovec_destroy(&qiov); 237260ce86c7SWei Liu out: 237360ce86c7SWei Liu put_fid(pdu, fidp); 237460ce86c7SWei Liu out_nofid: 237560ce86c7SWei Liu qemu_iovec_destroy(&qiov_full); 237660ce86c7SWei Liu pdu_complete(pdu, err); 237760ce86c7SWei Liu } 237860ce86c7SWei Liu 23798440e22eSGreg Kurz static void coroutine_fn v9fs_create(void *opaque) 238060ce86c7SWei Liu { 238160ce86c7SWei Liu int32_t fid; 238260ce86c7SWei Liu int err = 0; 238360ce86c7SWei Liu size_t offset = 7; 238460ce86c7SWei Liu V9fsFidState *fidp; 238560ce86c7SWei Liu V9fsQID qid; 238660ce86c7SWei Liu int32_t perm; 238760ce86c7SWei Liu int8_t mode; 238860ce86c7SWei Liu V9fsPath path; 238960ce86c7SWei Liu struct stat stbuf; 239060ce86c7SWei Liu V9fsString name; 239160ce86c7SWei Liu V9fsString extension; 239260ce86c7SWei Liu int iounit; 239360ce86c7SWei Liu V9fsPDU *pdu = opaque; 23945b3c77aaSGreg Kurz V9fsState *s = pdu->s; 239560ce86c7SWei Liu 239660ce86c7SWei Liu v9fs_path_init(&path); 239760ce86c7SWei Liu v9fs_string_init(&name); 239860ce86c7SWei Liu v9fs_string_init(&extension); 239960ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name, 240060ce86c7SWei Liu &perm, &mode, &extension); 240160ce86c7SWei Liu if (err < 0) { 240260ce86c7SWei Liu goto out_nofid; 240360ce86c7SWei Liu } 240460ce86c7SWei Liu trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode); 240560ce86c7SWei Liu 2406fff39a7aSGreg Kurz if (name_is_illegal(name.data)) { 2407fff39a7aSGreg Kurz err = -ENOENT; 2408fff39a7aSGreg Kurz goto out_nofid; 2409fff39a7aSGreg Kurz } 2410fff39a7aSGreg Kurz 2411805b5d98SGreg Kurz if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 2412805b5d98SGreg Kurz err = -EEXIST; 2413805b5d98SGreg Kurz goto out_nofid; 2414805b5d98SGreg Kurz } 2415805b5d98SGreg Kurz 241660ce86c7SWei Liu fidp = get_fid(pdu, fid); 241760ce86c7SWei Liu if (fidp == NULL) { 241860ce86c7SWei Liu err = -EINVAL; 241960ce86c7SWei Liu goto out_nofid; 242060ce86c7SWei Liu } 2421d63fb193SLi Qiang if (fidp->fid_type != P9_FID_NONE) { 2422d63fb193SLi Qiang err = -EINVAL; 2423d63fb193SLi Qiang goto out; 2424d63fb193SLi Qiang } 242560ce86c7SWei Liu if (perm & P9_STAT_MODE_DIR) { 242660ce86c7SWei Liu err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777, 242760ce86c7SWei Liu fidp->uid, -1, &stbuf); 242860ce86c7SWei Liu if (err < 0) { 242960ce86c7SWei Liu goto out; 243060ce86c7SWei Liu } 243160ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); 243260ce86c7SWei Liu if (err < 0) { 243360ce86c7SWei Liu goto out; 243460ce86c7SWei Liu } 24355b3c77aaSGreg Kurz v9fs_path_write_lock(s); 243660ce86c7SWei Liu v9fs_path_copy(&fidp->path, &path); 24375b3c77aaSGreg Kurz v9fs_path_unlock(s); 243860ce86c7SWei Liu err = v9fs_co_opendir(pdu, fidp); 243960ce86c7SWei Liu if (err < 0) { 244060ce86c7SWei Liu goto out; 244160ce86c7SWei Liu } 244260ce86c7SWei Liu fidp->fid_type = P9_FID_DIR; 244360ce86c7SWei Liu } else if (perm & P9_STAT_MODE_SYMLINK) { 244460ce86c7SWei Liu err = v9fs_co_symlink(pdu, fidp, &name, 244560ce86c7SWei Liu extension.data, -1 , &stbuf); 244660ce86c7SWei Liu if (err < 0) { 244760ce86c7SWei Liu goto out; 244860ce86c7SWei Liu } 244960ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); 245060ce86c7SWei Liu if (err < 0) { 245160ce86c7SWei Liu goto out; 245260ce86c7SWei Liu } 24535b3c77aaSGreg Kurz v9fs_path_write_lock(s); 245460ce86c7SWei Liu v9fs_path_copy(&fidp->path, &path); 24555b3c77aaSGreg Kurz v9fs_path_unlock(s); 245660ce86c7SWei Liu } else if (perm & P9_STAT_MODE_LINK) { 245760ce86c7SWei Liu int32_t ofid = atoi(extension.data); 245860ce86c7SWei Liu V9fsFidState *ofidp = get_fid(pdu, ofid); 245960ce86c7SWei Liu if (ofidp == NULL) { 246060ce86c7SWei Liu err = -EINVAL; 246160ce86c7SWei Liu goto out; 246260ce86c7SWei Liu } 246360ce86c7SWei Liu err = v9fs_co_link(pdu, ofidp, fidp, &name); 246460ce86c7SWei Liu put_fid(pdu, ofidp); 246560ce86c7SWei Liu if (err < 0) { 246660ce86c7SWei Liu goto out; 246760ce86c7SWei Liu } 246860ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); 246960ce86c7SWei Liu if (err < 0) { 247060ce86c7SWei Liu fidp->fid_type = P9_FID_NONE; 247160ce86c7SWei Liu goto out; 247260ce86c7SWei Liu } 24735b3c77aaSGreg Kurz v9fs_path_write_lock(s); 247460ce86c7SWei Liu v9fs_path_copy(&fidp->path, &path); 24755b3c77aaSGreg Kurz v9fs_path_unlock(s); 247660ce86c7SWei Liu err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); 247760ce86c7SWei Liu if (err < 0) { 247860ce86c7SWei Liu fidp->fid_type = P9_FID_NONE; 247960ce86c7SWei Liu goto out; 248060ce86c7SWei Liu } 248160ce86c7SWei Liu } else if (perm & P9_STAT_MODE_DEVICE) { 248260ce86c7SWei Liu char ctype; 248360ce86c7SWei Liu uint32_t major, minor; 248460ce86c7SWei Liu mode_t nmode = 0; 248560ce86c7SWei Liu 248660ce86c7SWei Liu if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) { 248760ce86c7SWei Liu err = -errno; 248860ce86c7SWei Liu goto out; 248960ce86c7SWei Liu } 249060ce86c7SWei Liu 249160ce86c7SWei Liu switch (ctype) { 249260ce86c7SWei Liu case 'c': 249360ce86c7SWei Liu nmode = S_IFCHR; 249460ce86c7SWei Liu break; 249560ce86c7SWei Liu case 'b': 249660ce86c7SWei Liu nmode = S_IFBLK; 249760ce86c7SWei Liu break; 249860ce86c7SWei Liu default: 249960ce86c7SWei Liu err = -EIO; 250060ce86c7SWei Liu goto out; 250160ce86c7SWei Liu } 250260ce86c7SWei Liu 250360ce86c7SWei Liu nmode |= perm & 0777; 250460ce86c7SWei Liu err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1, 250560ce86c7SWei Liu makedev(major, minor), nmode, &stbuf); 250660ce86c7SWei Liu if (err < 0) { 250760ce86c7SWei Liu goto out; 250860ce86c7SWei Liu } 250960ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); 251060ce86c7SWei Liu if (err < 0) { 251160ce86c7SWei Liu goto out; 251260ce86c7SWei Liu } 25135b3c77aaSGreg Kurz v9fs_path_write_lock(s); 251460ce86c7SWei Liu v9fs_path_copy(&fidp->path, &path); 25155b3c77aaSGreg Kurz v9fs_path_unlock(s); 251660ce86c7SWei Liu } else if (perm & P9_STAT_MODE_NAMED_PIPE) { 251760ce86c7SWei Liu err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1, 251860ce86c7SWei Liu 0, S_IFIFO | (perm & 0777), &stbuf); 251960ce86c7SWei Liu if (err < 0) { 252060ce86c7SWei Liu goto out; 252160ce86c7SWei Liu } 252260ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); 252360ce86c7SWei Liu if (err < 0) { 252460ce86c7SWei Liu goto out; 252560ce86c7SWei Liu } 25265b3c77aaSGreg Kurz v9fs_path_write_lock(s); 252760ce86c7SWei Liu v9fs_path_copy(&fidp->path, &path); 25285b3c77aaSGreg Kurz v9fs_path_unlock(s); 252960ce86c7SWei Liu } else if (perm & P9_STAT_MODE_SOCKET) { 253060ce86c7SWei Liu err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1, 253160ce86c7SWei Liu 0, S_IFSOCK | (perm & 0777), &stbuf); 253260ce86c7SWei Liu if (err < 0) { 253360ce86c7SWei Liu goto out; 253460ce86c7SWei Liu } 253560ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); 253660ce86c7SWei Liu if (err < 0) { 253760ce86c7SWei Liu goto out; 253860ce86c7SWei Liu } 25395b3c77aaSGreg Kurz v9fs_path_write_lock(s); 254060ce86c7SWei Liu v9fs_path_copy(&fidp->path, &path); 25415b3c77aaSGreg Kurz v9fs_path_unlock(s); 254260ce86c7SWei Liu } else { 254360ce86c7SWei Liu err = v9fs_co_open2(pdu, fidp, &name, -1, 254460ce86c7SWei Liu omode_to_uflags(mode)|O_CREAT, perm, &stbuf); 254560ce86c7SWei Liu if (err < 0) { 254660ce86c7SWei Liu goto out; 254760ce86c7SWei Liu } 254860ce86c7SWei Liu fidp->fid_type = P9_FID_FILE; 254960ce86c7SWei Liu fidp->open_flags = omode_to_uflags(mode); 255060ce86c7SWei Liu if (fidp->open_flags & O_EXCL) { 255160ce86c7SWei Liu /* 255260ce86c7SWei Liu * We let the host file system do O_EXCL check 255360ce86c7SWei Liu * We should not reclaim such fd 255460ce86c7SWei Liu */ 255560ce86c7SWei Liu fidp->flags |= FID_NON_RECLAIMABLE; 255660ce86c7SWei Liu } 255760ce86c7SWei Liu } 255860ce86c7SWei Liu iounit = get_iounit(pdu, &fidp->path); 25593b5ee9e8SAntonios Motakis err = stat_to_qid(pdu, &stbuf, &qid); 25603b5ee9e8SAntonios Motakis if (err < 0) { 25613b5ee9e8SAntonios Motakis goto out; 25623b5ee9e8SAntonios Motakis } 256360ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Qd", &qid, iounit); 256460ce86c7SWei Liu if (err < 0) { 256560ce86c7SWei Liu goto out; 256660ce86c7SWei Liu } 256760ce86c7SWei Liu err += offset; 256860ce86c7SWei Liu trace_v9fs_create_return(pdu->tag, pdu->id, 256960ce86c7SWei Liu qid.type, qid.version, qid.path, iounit); 257060ce86c7SWei Liu out: 257160ce86c7SWei Liu put_fid(pdu, fidp); 257260ce86c7SWei Liu out_nofid: 257360ce86c7SWei Liu pdu_complete(pdu, err); 257460ce86c7SWei Liu v9fs_string_free(&name); 257560ce86c7SWei Liu v9fs_string_free(&extension); 257660ce86c7SWei Liu v9fs_path_free(&path); 257760ce86c7SWei Liu } 257860ce86c7SWei Liu 25798440e22eSGreg Kurz static void coroutine_fn v9fs_symlink(void *opaque) 258060ce86c7SWei Liu { 258160ce86c7SWei Liu V9fsPDU *pdu = opaque; 258260ce86c7SWei Liu V9fsString name; 258360ce86c7SWei Liu V9fsString symname; 258460ce86c7SWei Liu V9fsFidState *dfidp; 258560ce86c7SWei Liu V9fsQID qid; 258660ce86c7SWei Liu struct stat stbuf; 258760ce86c7SWei Liu int32_t dfid; 258860ce86c7SWei Liu int err = 0; 258960ce86c7SWei Liu gid_t gid; 259060ce86c7SWei Liu size_t offset = 7; 259160ce86c7SWei Liu 259260ce86c7SWei Liu v9fs_string_init(&name); 259360ce86c7SWei Liu v9fs_string_init(&symname); 259460ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid); 259560ce86c7SWei Liu if (err < 0) { 259660ce86c7SWei Liu goto out_nofid; 259760ce86c7SWei Liu } 259860ce86c7SWei Liu trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid); 259960ce86c7SWei Liu 2600fff39a7aSGreg Kurz if (name_is_illegal(name.data)) { 2601fff39a7aSGreg Kurz err = -ENOENT; 2602fff39a7aSGreg Kurz goto out_nofid; 2603fff39a7aSGreg Kurz } 2604fff39a7aSGreg Kurz 2605805b5d98SGreg Kurz if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 2606805b5d98SGreg Kurz err = -EEXIST; 2607805b5d98SGreg Kurz goto out_nofid; 2608805b5d98SGreg Kurz } 2609805b5d98SGreg Kurz 261060ce86c7SWei Liu dfidp = get_fid(pdu, dfid); 261160ce86c7SWei Liu if (dfidp == NULL) { 261260ce86c7SWei Liu err = -EINVAL; 261360ce86c7SWei Liu goto out_nofid; 261460ce86c7SWei Liu } 261560ce86c7SWei Liu err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf); 261660ce86c7SWei Liu if (err < 0) { 261760ce86c7SWei Liu goto out; 261860ce86c7SWei Liu } 26193b5ee9e8SAntonios Motakis err = stat_to_qid(pdu, &stbuf, &qid); 26203b5ee9e8SAntonios Motakis if (err < 0) { 26213b5ee9e8SAntonios Motakis goto out; 26223b5ee9e8SAntonios Motakis } 262360ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Q", &qid); 262460ce86c7SWei Liu if (err < 0) { 262560ce86c7SWei Liu goto out; 262660ce86c7SWei Liu } 262760ce86c7SWei Liu err += offset; 262860ce86c7SWei Liu trace_v9fs_symlink_return(pdu->tag, pdu->id, 262960ce86c7SWei Liu qid.type, qid.version, qid.path); 263060ce86c7SWei Liu out: 263160ce86c7SWei Liu put_fid(pdu, dfidp); 263260ce86c7SWei Liu out_nofid: 263360ce86c7SWei Liu pdu_complete(pdu, err); 263460ce86c7SWei Liu v9fs_string_free(&name); 263560ce86c7SWei Liu v9fs_string_free(&symname); 263660ce86c7SWei Liu } 263760ce86c7SWei Liu 2638a1bf8b74SGreg Kurz static void coroutine_fn v9fs_flush(void *opaque) 263960ce86c7SWei Liu { 264060ce86c7SWei Liu ssize_t err; 264160ce86c7SWei Liu int16_t tag; 264260ce86c7SWei Liu size_t offset = 7; 2643d5f2af7bSGreg Kurz V9fsPDU *cancel_pdu = NULL; 264460ce86c7SWei Liu V9fsPDU *pdu = opaque; 264560ce86c7SWei Liu V9fsState *s = pdu->s; 264660ce86c7SWei Liu 264760ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "w", &tag); 264860ce86c7SWei Liu if (err < 0) { 264960ce86c7SWei Liu pdu_complete(pdu, err); 265060ce86c7SWei Liu return; 265160ce86c7SWei Liu } 265260ce86c7SWei Liu trace_v9fs_flush(pdu->tag, pdu->id, tag); 265360ce86c7SWei Liu 2654d5f2af7bSGreg Kurz if (pdu->tag == tag) { 26553dc6f869SAlistair Francis warn_report("the guest sent a self-referencing 9P flush request"); 2656d5f2af7bSGreg Kurz } else { 265760ce86c7SWei Liu QLIST_FOREACH(cancel_pdu, &s->active_list, next) { 265860ce86c7SWei Liu if (cancel_pdu->tag == tag) { 265960ce86c7SWei Liu break; 266060ce86c7SWei Liu } 266160ce86c7SWei Liu } 2662d5f2af7bSGreg Kurz } 266360ce86c7SWei Liu if (cancel_pdu) { 266460ce86c7SWei Liu cancel_pdu->cancelled = 1; 266560ce86c7SWei Liu /* 266660ce86c7SWei Liu * Wait for pdu to complete. 266760ce86c7SWei Liu */ 26681ace7ceaSPaolo Bonzini qemu_co_queue_wait(&cancel_pdu->complete, NULL); 266918adde86SGreg Kurz if (!qemu_co_queue_next(&cancel_pdu->complete)) { 267060ce86c7SWei Liu cancel_pdu->cancelled = 0; 267160ce86c7SWei Liu pdu_free(cancel_pdu); 267260ce86c7SWei Liu } 267318adde86SGreg Kurz } 267460ce86c7SWei Liu pdu_complete(pdu, 7); 267560ce86c7SWei Liu } 267660ce86c7SWei Liu 26778440e22eSGreg Kurz static void coroutine_fn v9fs_link(void *opaque) 267860ce86c7SWei Liu { 267960ce86c7SWei Liu V9fsPDU *pdu = opaque; 268060ce86c7SWei Liu int32_t dfid, oldfid; 268160ce86c7SWei Liu V9fsFidState *dfidp, *oldfidp; 268260ce86c7SWei Liu V9fsString name; 268360ce86c7SWei Liu size_t offset = 7; 268460ce86c7SWei Liu int err = 0; 268560ce86c7SWei Liu 268660ce86c7SWei Liu v9fs_string_init(&name); 268760ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name); 268860ce86c7SWei Liu if (err < 0) { 268960ce86c7SWei Liu goto out_nofid; 269060ce86c7SWei Liu } 269160ce86c7SWei Liu trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data); 269260ce86c7SWei Liu 2693fff39a7aSGreg Kurz if (name_is_illegal(name.data)) { 2694fff39a7aSGreg Kurz err = -ENOENT; 2695fff39a7aSGreg Kurz goto out_nofid; 2696fff39a7aSGreg Kurz } 2697fff39a7aSGreg Kurz 2698805b5d98SGreg Kurz if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 2699805b5d98SGreg Kurz err = -EEXIST; 2700805b5d98SGreg Kurz goto out_nofid; 2701805b5d98SGreg Kurz } 2702805b5d98SGreg Kurz 270360ce86c7SWei Liu dfidp = get_fid(pdu, dfid); 270460ce86c7SWei Liu if (dfidp == NULL) { 270560ce86c7SWei Liu err = -ENOENT; 270660ce86c7SWei Liu goto out_nofid; 270760ce86c7SWei Liu } 270860ce86c7SWei Liu 270960ce86c7SWei Liu oldfidp = get_fid(pdu, oldfid); 271060ce86c7SWei Liu if (oldfidp == NULL) { 271160ce86c7SWei Liu err = -ENOENT; 271260ce86c7SWei Liu goto out; 271360ce86c7SWei Liu } 271460ce86c7SWei Liu err = v9fs_co_link(pdu, oldfidp, dfidp, &name); 271560ce86c7SWei Liu if (!err) { 271660ce86c7SWei Liu err = offset; 271760ce86c7SWei Liu } 27184c158678SLi Qiang put_fid(pdu, oldfidp); 271960ce86c7SWei Liu out: 272060ce86c7SWei Liu put_fid(pdu, dfidp); 272160ce86c7SWei Liu out_nofid: 272260ce86c7SWei Liu v9fs_string_free(&name); 272360ce86c7SWei Liu pdu_complete(pdu, err); 272460ce86c7SWei Liu } 272560ce86c7SWei Liu 272660ce86c7SWei Liu /* Only works with path name based fid */ 27278440e22eSGreg Kurz static void coroutine_fn v9fs_remove(void *opaque) 272860ce86c7SWei Liu { 272960ce86c7SWei Liu int32_t fid; 273060ce86c7SWei Liu int err = 0; 273160ce86c7SWei Liu size_t offset = 7; 273260ce86c7SWei Liu V9fsFidState *fidp; 273360ce86c7SWei Liu V9fsPDU *pdu = opaque; 273460ce86c7SWei Liu 273560ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "d", &fid); 273660ce86c7SWei Liu if (err < 0) { 273760ce86c7SWei Liu goto out_nofid; 273860ce86c7SWei Liu } 273960ce86c7SWei Liu trace_v9fs_remove(pdu->tag, pdu->id, fid); 274060ce86c7SWei Liu 274160ce86c7SWei Liu fidp = get_fid(pdu, fid); 274260ce86c7SWei Liu if (fidp == NULL) { 274360ce86c7SWei Liu err = -EINVAL; 274460ce86c7SWei Liu goto out_nofid; 274560ce86c7SWei Liu } 274660ce86c7SWei Liu /* if fs driver is not path based, return EOPNOTSUPP */ 274760ce86c7SWei Liu if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) { 274860ce86c7SWei Liu err = -EOPNOTSUPP; 274960ce86c7SWei Liu goto out_err; 275060ce86c7SWei Liu } 275160ce86c7SWei Liu /* 275260ce86c7SWei Liu * IF the file is unlinked, we cannot reopen 275360ce86c7SWei Liu * the file later. So don't reclaim fd 275460ce86c7SWei Liu */ 275560ce86c7SWei Liu err = v9fs_mark_fids_unreclaim(pdu, &fidp->path); 275660ce86c7SWei Liu if (err < 0) { 275760ce86c7SWei Liu goto out_err; 275860ce86c7SWei Liu } 275960ce86c7SWei Liu err = v9fs_co_remove(pdu, &fidp->path); 276060ce86c7SWei Liu if (!err) { 276160ce86c7SWei Liu err = offset; 276260ce86c7SWei Liu } 276360ce86c7SWei Liu out_err: 276460ce86c7SWei Liu /* For TREMOVE we need to clunk the fid even on failed remove */ 276560ce86c7SWei Liu clunk_fid(pdu->s, fidp->fid); 276660ce86c7SWei Liu put_fid(pdu, fidp); 276760ce86c7SWei Liu out_nofid: 276860ce86c7SWei Liu pdu_complete(pdu, err); 276960ce86c7SWei Liu } 277060ce86c7SWei Liu 27718440e22eSGreg Kurz static void coroutine_fn v9fs_unlinkat(void *opaque) 277260ce86c7SWei Liu { 277360ce86c7SWei Liu int err = 0; 277460ce86c7SWei Liu V9fsString name; 277567e87345SKeno Fischer int32_t dfid, flags, rflags = 0; 277660ce86c7SWei Liu size_t offset = 7; 277760ce86c7SWei Liu V9fsPath path; 277860ce86c7SWei Liu V9fsFidState *dfidp; 277960ce86c7SWei Liu V9fsPDU *pdu = opaque; 278060ce86c7SWei Liu 278160ce86c7SWei Liu v9fs_string_init(&name); 278260ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags); 278360ce86c7SWei Liu if (err < 0) { 278460ce86c7SWei Liu goto out_nofid; 278560ce86c7SWei Liu } 2786fff39a7aSGreg Kurz 2787fff39a7aSGreg Kurz if (name_is_illegal(name.data)) { 2788fff39a7aSGreg Kurz err = -ENOENT; 2789fff39a7aSGreg Kurz goto out_nofid; 2790fff39a7aSGreg Kurz } 2791fff39a7aSGreg Kurz 2792805b5d98SGreg Kurz if (!strcmp(".", name.data)) { 2793805b5d98SGreg Kurz err = -EINVAL; 2794805b5d98SGreg Kurz goto out_nofid; 2795805b5d98SGreg Kurz } 2796805b5d98SGreg Kurz 2797805b5d98SGreg Kurz if (!strcmp("..", name.data)) { 2798805b5d98SGreg Kurz err = -ENOTEMPTY; 2799805b5d98SGreg Kurz goto out_nofid; 2800805b5d98SGreg Kurz } 2801805b5d98SGreg Kurz 280267e87345SKeno Fischer if (flags & ~P9_DOTL_AT_REMOVEDIR) { 280367e87345SKeno Fischer err = -EINVAL; 280467e87345SKeno Fischer goto out_nofid; 280567e87345SKeno Fischer } 280667e87345SKeno Fischer 280767e87345SKeno Fischer if (flags & P9_DOTL_AT_REMOVEDIR) { 280867e87345SKeno Fischer rflags |= AT_REMOVEDIR; 280967e87345SKeno Fischer } 281067e87345SKeno Fischer 281160ce86c7SWei Liu dfidp = get_fid(pdu, dfid); 281260ce86c7SWei Liu if (dfidp == NULL) { 281360ce86c7SWei Liu err = -EINVAL; 281460ce86c7SWei Liu goto out_nofid; 281560ce86c7SWei Liu } 281660ce86c7SWei Liu /* 281760ce86c7SWei Liu * IF the file is unlinked, we cannot reopen 281860ce86c7SWei Liu * the file later. So don't reclaim fd 281960ce86c7SWei Liu */ 282060ce86c7SWei Liu v9fs_path_init(&path); 282160ce86c7SWei Liu err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path); 282260ce86c7SWei Liu if (err < 0) { 282360ce86c7SWei Liu goto out_err; 282460ce86c7SWei Liu } 282560ce86c7SWei Liu err = v9fs_mark_fids_unreclaim(pdu, &path); 282660ce86c7SWei Liu if (err < 0) { 282760ce86c7SWei Liu goto out_err; 282860ce86c7SWei Liu } 282967e87345SKeno Fischer err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, rflags); 283060ce86c7SWei Liu if (!err) { 283160ce86c7SWei Liu err = offset; 283260ce86c7SWei Liu } 283360ce86c7SWei Liu out_err: 283460ce86c7SWei Liu put_fid(pdu, dfidp); 283560ce86c7SWei Liu v9fs_path_free(&path); 283660ce86c7SWei Liu out_nofid: 283760ce86c7SWei Liu pdu_complete(pdu, err); 283860ce86c7SWei Liu v9fs_string_free(&name); 283960ce86c7SWei Liu } 284060ce86c7SWei Liu 284160ce86c7SWei Liu 284260ce86c7SWei Liu /* Only works with path name based fid */ 28438440e22eSGreg Kurz static int coroutine_fn v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp, 28448440e22eSGreg Kurz int32_t newdirfid, 28458440e22eSGreg Kurz V9fsString *name) 284660ce86c7SWei Liu { 284760ce86c7SWei Liu int err = 0; 284860ce86c7SWei Liu V9fsPath new_path; 284960ce86c7SWei Liu V9fsFidState *tfidp; 285060ce86c7SWei Liu V9fsState *s = pdu->s; 285160ce86c7SWei Liu V9fsFidState *dirfidp = NULL; 285260ce86c7SWei Liu 285360ce86c7SWei Liu v9fs_path_init(&new_path); 285460ce86c7SWei Liu if (newdirfid != -1) { 285560ce86c7SWei Liu dirfidp = get_fid(pdu, newdirfid); 285660ce86c7SWei Liu if (dirfidp == NULL) { 285760ce86c7SWei Liu err = -ENOENT; 285860ce86c7SWei Liu goto out_nofid; 285960ce86c7SWei Liu } 286049dd946bSGreg Kurz if (fidp->fid_type != P9_FID_NONE) { 286149dd946bSGreg Kurz err = -EINVAL; 286249dd946bSGreg Kurz goto out; 286349dd946bSGreg Kurz } 28644fa62005SGreg Kurz err = v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path); 28654fa62005SGreg Kurz if (err < 0) { 28664fa62005SGreg Kurz goto out; 28674fa62005SGreg Kurz } 286860ce86c7SWei Liu } else { 28694d8bc733SJan Dakinevich char *dir_name = g_path_get_dirname(fidp->path.data); 28704d8bc733SJan Dakinevich V9fsPath dir_path; 28714d8bc733SJan Dakinevich 28724d8bc733SJan Dakinevich v9fs_path_init(&dir_path); 28734d8bc733SJan Dakinevich v9fs_path_sprintf(&dir_path, "%s", dir_name); 28744d8bc733SJan Dakinevich g_free(dir_name); 28754d8bc733SJan Dakinevich 28764d8bc733SJan Dakinevich err = v9fs_co_name_to_path(pdu, &dir_path, name->data, &new_path); 28774d8bc733SJan Dakinevich v9fs_path_free(&dir_path); 28784fa62005SGreg Kurz if (err < 0) { 28794fa62005SGreg Kurz goto out; 28804fa62005SGreg Kurz } 288160ce86c7SWei Liu } 288260ce86c7SWei Liu err = v9fs_co_rename(pdu, &fidp->path, &new_path); 288360ce86c7SWei Liu if (err < 0) { 288460ce86c7SWei Liu goto out; 288560ce86c7SWei Liu } 288660ce86c7SWei Liu /* 288760ce86c7SWei Liu * Fixup fid's pointing to the old name to 288860ce86c7SWei Liu * start pointing to the new name 288960ce86c7SWei Liu */ 289060ce86c7SWei Liu for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) { 289160ce86c7SWei Liu if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) { 289260ce86c7SWei Liu /* replace the name */ 289360ce86c7SWei Liu v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data)); 289460ce86c7SWei Liu } 289560ce86c7SWei Liu } 289660ce86c7SWei Liu out: 289760ce86c7SWei Liu if (dirfidp) { 289860ce86c7SWei Liu put_fid(pdu, dirfidp); 289960ce86c7SWei Liu } 290060ce86c7SWei Liu v9fs_path_free(&new_path); 290160ce86c7SWei Liu out_nofid: 290260ce86c7SWei Liu return err; 290360ce86c7SWei Liu } 290460ce86c7SWei Liu 290560ce86c7SWei Liu /* Only works with path name based fid */ 29068440e22eSGreg Kurz static void coroutine_fn v9fs_rename(void *opaque) 290760ce86c7SWei Liu { 290860ce86c7SWei Liu int32_t fid; 290960ce86c7SWei Liu ssize_t err = 0; 291060ce86c7SWei Liu size_t offset = 7; 291160ce86c7SWei Liu V9fsString name; 291260ce86c7SWei Liu int32_t newdirfid; 291360ce86c7SWei Liu V9fsFidState *fidp; 291460ce86c7SWei Liu V9fsPDU *pdu = opaque; 291560ce86c7SWei Liu V9fsState *s = pdu->s; 291660ce86c7SWei Liu 291760ce86c7SWei Liu v9fs_string_init(&name); 291860ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name); 291960ce86c7SWei Liu if (err < 0) { 292060ce86c7SWei Liu goto out_nofid; 292160ce86c7SWei Liu } 2922fff39a7aSGreg Kurz 2923fff39a7aSGreg Kurz if (name_is_illegal(name.data)) { 2924fff39a7aSGreg Kurz err = -ENOENT; 2925fff39a7aSGreg Kurz goto out_nofid; 2926fff39a7aSGreg Kurz } 2927fff39a7aSGreg Kurz 2928805b5d98SGreg Kurz if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 2929805b5d98SGreg Kurz err = -EISDIR; 2930805b5d98SGreg Kurz goto out_nofid; 2931805b5d98SGreg Kurz } 2932805b5d98SGreg Kurz 293360ce86c7SWei Liu fidp = get_fid(pdu, fid); 293460ce86c7SWei Liu if (fidp == NULL) { 293560ce86c7SWei Liu err = -ENOENT; 293660ce86c7SWei Liu goto out_nofid; 293760ce86c7SWei Liu } 293849dd946bSGreg Kurz if (fidp->fid_type != P9_FID_NONE) { 293949dd946bSGreg Kurz err = -EINVAL; 294049dd946bSGreg Kurz goto out; 294149dd946bSGreg Kurz } 294260ce86c7SWei Liu /* if fs driver is not path based, return EOPNOTSUPP */ 294360ce86c7SWei Liu if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) { 294460ce86c7SWei Liu err = -EOPNOTSUPP; 294560ce86c7SWei Liu goto out; 294660ce86c7SWei Liu } 294760ce86c7SWei Liu v9fs_path_write_lock(s); 294860ce86c7SWei Liu err = v9fs_complete_rename(pdu, fidp, newdirfid, &name); 294960ce86c7SWei Liu v9fs_path_unlock(s); 295060ce86c7SWei Liu if (!err) { 295160ce86c7SWei Liu err = offset; 295260ce86c7SWei Liu } 295360ce86c7SWei Liu out: 295460ce86c7SWei Liu put_fid(pdu, fidp); 295560ce86c7SWei Liu out_nofid: 295660ce86c7SWei Liu pdu_complete(pdu, err); 295760ce86c7SWei Liu v9fs_string_free(&name); 295860ce86c7SWei Liu } 295960ce86c7SWei Liu 29604fa62005SGreg Kurz static int coroutine_fn v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir, 29618440e22eSGreg Kurz V9fsString *old_name, 29628440e22eSGreg Kurz V9fsPath *newdir, 296360ce86c7SWei Liu V9fsString *new_name) 296460ce86c7SWei Liu { 296560ce86c7SWei Liu V9fsFidState *tfidp; 296660ce86c7SWei Liu V9fsPath oldpath, newpath; 296760ce86c7SWei Liu V9fsState *s = pdu->s; 29684fa62005SGreg Kurz int err; 296960ce86c7SWei Liu 297060ce86c7SWei Liu v9fs_path_init(&oldpath); 297160ce86c7SWei Liu v9fs_path_init(&newpath); 29724fa62005SGreg Kurz err = v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath); 29734fa62005SGreg Kurz if (err < 0) { 29744fa62005SGreg Kurz goto out; 29754fa62005SGreg Kurz } 29764fa62005SGreg Kurz err = v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath); 29774fa62005SGreg Kurz if (err < 0) { 29784fa62005SGreg Kurz goto out; 29794fa62005SGreg Kurz } 298060ce86c7SWei Liu 298160ce86c7SWei Liu /* 298260ce86c7SWei Liu * Fixup fid's pointing to the old name to 298360ce86c7SWei Liu * start pointing to the new name 298460ce86c7SWei Liu */ 298560ce86c7SWei Liu for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) { 298660ce86c7SWei Liu if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) { 298760ce86c7SWei Liu /* replace the name */ 298860ce86c7SWei Liu v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data)); 298960ce86c7SWei Liu } 299060ce86c7SWei Liu } 29914fa62005SGreg Kurz out: 299260ce86c7SWei Liu v9fs_path_free(&oldpath); 299360ce86c7SWei Liu v9fs_path_free(&newpath); 29944fa62005SGreg Kurz return err; 299560ce86c7SWei Liu } 299660ce86c7SWei Liu 29978440e22eSGreg Kurz static int coroutine_fn v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid, 29988440e22eSGreg Kurz V9fsString *old_name, 29998440e22eSGreg Kurz int32_t newdirfid, 300060ce86c7SWei Liu V9fsString *new_name) 300160ce86c7SWei Liu { 300260ce86c7SWei Liu int err = 0; 300360ce86c7SWei Liu V9fsState *s = pdu->s; 300460ce86c7SWei Liu V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL; 300560ce86c7SWei Liu 300660ce86c7SWei Liu olddirfidp = get_fid(pdu, olddirfid); 300760ce86c7SWei Liu if (olddirfidp == NULL) { 300860ce86c7SWei Liu err = -ENOENT; 300960ce86c7SWei Liu goto out; 301060ce86c7SWei Liu } 301160ce86c7SWei Liu if (newdirfid != -1) { 301260ce86c7SWei Liu newdirfidp = get_fid(pdu, newdirfid); 301360ce86c7SWei Liu if (newdirfidp == NULL) { 301460ce86c7SWei Liu err = -ENOENT; 301560ce86c7SWei Liu goto out; 301660ce86c7SWei Liu } 301760ce86c7SWei Liu } else { 301860ce86c7SWei Liu newdirfidp = get_fid(pdu, olddirfid); 301960ce86c7SWei Liu } 302060ce86c7SWei Liu 302160ce86c7SWei Liu err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name, 302260ce86c7SWei Liu &newdirfidp->path, new_name); 302360ce86c7SWei Liu if (err < 0) { 302460ce86c7SWei Liu goto out; 302560ce86c7SWei Liu } 302660ce86c7SWei Liu if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) { 302760ce86c7SWei Liu /* Only for path based fid we need to do the below fixup */ 30284fa62005SGreg Kurz err = v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name, 302960ce86c7SWei Liu &newdirfidp->path, new_name); 303060ce86c7SWei Liu } 303160ce86c7SWei Liu out: 303260ce86c7SWei Liu if (olddirfidp) { 303360ce86c7SWei Liu put_fid(pdu, olddirfidp); 303460ce86c7SWei Liu } 303560ce86c7SWei Liu if (newdirfidp) { 303660ce86c7SWei Liu put_fid(pdu, newdirfidp); 303760ce86c7SWei Liu } 303860ce86c7SWei Liu return err; 303960ce86c7SWei Liu } 304060ce86c7SWei Liu 30418440e22eSGreg Kurz static void coroutine_fn v9fs_renameat(void *opaque) 304260ce86c7SWei Liu { 304360ce86c7SWei Liu ssize_t err = 0; 304460ce86c7SWei Liu size_t offset = 7; 304560ce86c7SWei Liu V9fsPDU *pdu = opaque; 304660ce86c7SWei Liu V9fsState *s = pdu->s; 304760ce86c7SWei Liu int32_t olddirfid, newdirfid; 304860ce86c7SWei Liu V9fsString old_name, new_name; 304960ce86c7SWei Liu 305060ce86c7SWei Liu v9fs_string_init(&old_name); 305160ce86c7SWei Liu v9fs_string_init(&new_name); 305260ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid, 305360ce86c7SWei Liu &old_name, &newdirfid, &new_name); 305460ce86c7SWei Liu if (err < 0) { 305560ce86c7SWei Liu goto out_err; 305660ce86c7SWei Liu } 305760ce86c7SWei Liu 3058fff39a7aSGreg Kurz if (name_is_illegal(old_name.data) || name_is_illegal(new_name.data)) { 3059fff39a7aSGreg Kurz err = -ENOENT; 3060fff39a7aSGreg Kurz goto out_err; 3061fff39a7aSGreg Kurz } 3062fff39a7aSGreg Kurz 3063805b5d98SGreg Kurz if (!strcmp(".", old_name.data) || !strcmp("..", old_name.data) || 3064805b5d98SGreg Kurz !strcmp(".", new_name.data) || !strcmp("..", new_name.data)) { 3065805b5d98SGreg Kurz err = -EISDIR; 3066805b5d98SGreg Kurz goto out_err; 3067805b5d98SGreg Kurz } 3068805b5d98SGreg Kurz 306960ce86c7SWei Liu v9fs_path_write_lock(s); 307060ce86c7SWei Liu err = v9fs_complete_renameat(pdu, olddirfid, 307160ce86c7SWei Liu &old_name, newdirfid, &new_name); 307260ce86c7SWei Liu v9fs_path_unlock(s); 307360ce86c7SWei Liu if (!err) { 307460ce86c7SWei Liu err = offset; 307560ce86c7SWei Liu } 307660ce86c7SWei Liu 307760ce86c7SWei Liu out_err: 307860ce86c7SWei Liu pdu_complete(pdu, err); 307960ce86c7SWei Liu v9fs_string_free(&old_name); 308060ce86c7SWei Liu v9fs_string_free(&new_name); 308160ce86c7SWei Liu } 308260ce86c7SWei Liu 30838440e22eSGreg Kurz static void coroutine_fn v9fs_wstat(void *opaque) 308460ce86c7SWei Liu { 308560ce86c7SWei Liu int32_t fid; 308660ce86c7SWei Liu int err = 0; 308760ce86c7SWei Liu int16_t unused; 308860ce86c7SWei Liu V9fsStat v9stat; 308960ce86c7SWei Liu size_t offset = 7; 309060ce86c7SWei Liu struct stat stbuf; 309160ce86c7SWei Liu V9fsFidState *fidp; 309260ce86c7SWei Liu V9fsPDU *pdu = opaque; 30931d203986SGreg Kurz V9fsState *s = pdu->s; 309460ce86c7SWei Liu 309560ce86c7SWei Liu v9fs_stat_init(&v9stat); 309660ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat); 309760ce86c7SWei Liu if (err < 0) { 309860ce86c7SWei Liu goto out_nofid; 309960ce86c7SWei Liu } 310060ce86c7SWei Liu trace_v9fs_wstat(pdu->tag, pdu->id, fid, 310160ce86c7SWei Liu v9stat.mode, v9stat.atime, v9stat.mtime); 310260ce86c7SWei Liu 310360ce86c7SWei Liu fidp = get_fid(pdu, fid); 310460ce86c7SWei Liu if (fidp == NULL) { 310560ce86c7SWei Liu err = -EINVAL; 310660ce86c7SWei Liu goto out_nofid; 310760ce86c7SWei Liu } 310860ce86c7SWei Liu /* do we need to sync the file? */ 310960ce86c7SWei Liu if (donttouch_stat(&v9stat)) { 311060ce86c7SWei Liu err = v9fs_co_fsync(pdu, fidp, 0); 311160ce86c7SWei Liu goto out; 311260ce86c7SWei Liu } 311360ce86c7SWei Liu if (v9stat.mode != -1) { 311460ce86c7SWei Liu uint32_t v9_mode; 311560ce86c7SWei Liu err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); 311660ce86c7SWei Liu if (err < 0) { 311760ce86c7SWei Liu goto out; 311860ce86c7SWei Liu } 311960ce86c7SWei Liu v9_mode = stat_to_v9mode(&stbuf); 312060ce86c7SWei Liu if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) != 312160ce86c7SWei Liu (v9_mode & P9_STAT_MODE_TYPE_BITS)) { 312260ce86c7SWei Liu /* Attempting to change the type */ 312360ce86c7SWei Liu err = -EIO; 312460ce86c7SWei Liu goto out; 312560ce86c7SWei Liu } 312660ce86c7SWei Liu err = v9fs_co_chmod(pdu, &fidp->path, 312760ce86c7SWei Liu v9mode_to_mode(v9stat.mode, 312860ce86c7SWei Liu &v9stat.extension)); 312960ce86c7SWei Liu if (err < 0) { 313060ce86c7SWei Liu goto out; 313160ce86c7SWei Liu } 313260ce86c7SWei Liu } 313360ce86c7SWei Liu if (v9stat.mtime != -1 || v9stat.atime != -1) { 313460ce86c7SWei Liu struct timespec times[2]; 313560ce86c7SWei Liu if (v9stat.atime != -1) { 313660ce86c7SWei Liu times[0].tv_sec = v9stat.atime; 313760ce86c7SWei Liu times[0].tv_nsec = 0; 313860ce86c7SWei Liu } else { 313960ce86c7SWei Liu times[0].tv_nsec = UTIME_OMIT; 314060ce86c7SWei Liu } 314160ce86c7SWei Liu if (v9stat.mtime != -1) { 314260ce86c7SWei Liu times[1].tv_sec = v9stat.mtime; 314360ce86c7SWei Liu times[1].tv_nsec = 0; 314460ce86c7SWei Liu } else { 314560ce86c7SWei Liu times[1].tv_nsec = UTIME_OMIT; 314660ce86c7SWei Liu } 314760ce86c7SWei Liu err = v9fs_co_utimensat(pdu, &fidp->path, times); 314860ce86c7SWei Liu if (err < 0) { 314960ce86c7SWei Liu goto out; 315060ce86c7SWei Liu } 315160ce86c7SWei Liu } 315260ce86c7SWei Liu if (v9stat.n_gid != -1 || v9stat.n_uid != -1) { 315360ce86c7SWei Liu err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid); 315460ce86c7SWei Liu if (err < 0) { 315560ce86c7SWei Liu goto out; 315660ce86c7SWei Liu } 315760ce86c7SWei Liu } 315860ce86c7SWei Liu if (v9stat.name.size != 0) { 31591d203986SGreg Kurz v9fs_path_write_lock(s); 316060ce86c7SWei Liu err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name); 31611d203986SGreg Kurz v9fs_path_unlock(s); 316260ce86c7SWei Liu if (err < 0) { 316360ce86c7SWei Liu goto out; 316460ce86c7SWei Liu } 316560ce86c7SWei Liu } 316660ce86c7SWei Liu if (v9stat.length != -1) { 316760ce86c7SWei Liu err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length); 316860ce86c7SWei Liu if (err < 0) { 316960ce86c7SWei Liu goto out; 317060ce86c7SWei Liu } 317160ce86c7SWei Liu } 317260ce86c7SWei Liu err = offset; 317360ce86c7SWei Liu out: 317460ce86c7SWei Liu put_fid(pdu, fidp); 317560ce86c7SWei Liu out_nofid: 317660ce86c7SWei Liu v9fs_stat_free(&v9stat); 317760ce86c7SWei Liu pdu_complete(pdu, err); 317860ce86c7SWei Liu } 317960ce86c7SWei Liu 318060ce86c7SWei Liu static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf) 318160ce86c7SWei Liu { 318260ce86c7SWei Liu uint32_t f_type; 318360ce86c7SWei Liu uint32_t f_bsize; 318460ce86c7SWei Liu uint64_t f_blocks; 318560ce86c7SWei Liu uint64_t f_bfree; 318660ce86c7SWei Liu uint64_t f_bavail; 318760ce86c7SWei Liu uint64_t f_files; 318860ce86c7SWei Liu uint64_t f_ffree; 318960ce86c7SWei Liu uint64_t fsid_val; 319060ce86c7SWei Liu uint32_t f_namelen; 319160ce86c7SWei Liu size_t offset = 7; 319260ce86c7SWei Liu int32_t bsize_factor; 319360ce86c7SWei Liu 319460ce86c7SWei Liu /* 319560ce86c7SWei Liu * compute bsize factor based on host file system block size 319660ce86c7SWei Liu * and client msize 319760ce86c7SWei Liu */ 319860ce86c7SWei Liu bsize_factor = (s->msize - P9_IOHDRSZ)/stbuf->f_bsize; 319960ce86c7SWei Liu if (!bsize_factor) { 320060ce86c7SWei Liu bsize_factor = 1; 320160ce86c7SWei Liu } 320260ce86c7SWei Liu f_type = stbuf->f_type; 320360ce86c7SWei Liu f_bsize = stbuf->f_bsize; 320460ce86c7SWei Liu f_bsize *= bsize_factor; 320560ce86c7SWei Liu /* 320660ce86c7SWei Liu * f_bsize is adjusted(multiplied) by bsize factor, so we need to 320760ce86c7SWei Liu * adjust(divide) the number of blocks, free blocks and available 320860ce86c7SWei Liu * blocks by bsize factor 320960ce86c7SWei Liu */ 321060ce86c7SWei Liu f_blocks = stbuf->f_blocks/bsize_factor; 321160ce86c7SWei Liu f_bfree = stbuf->f_bfree/bsize_factor; 321260ce86c7SWei Liu f_bavail = stbuf->f_bavail/bsize_factor; 321360ce86c7SWei Liu f_files = stbuf->f_files; 321460ce86c7SWei Liu f_ffree = stbuf->f_ffree; 321560ce86c7SWei Liu fsid_val = (unsigned int) stbuf->f_fsid.__val[0] | 321660ce86c7SWei Liu (unsigned long long)stbuf->f_fsid.__val[1] << 32; 321760ce86c7SWei Liu f_namelen = stbuf->f_namelen; 321860ce86c7SWei Liu 321960ce86c7SWei Liu return pdu_marshal(pdu, offset, "ddqqqqqqd", 322060ce86c7SWei Liu f_type, f_bsize, f_blocks, f_bfree, 322160ce86c7SWei Liu f_bavail, f_files, f_ffree, 322260ce86c7SWei Liu fsid_val, f_namelen); 322360ce86c7SWei Liu } 322460ce86c7SWei Liu 32258440e22eSGreg Kurz static void coroutine_fn v9fs_statfs(void *opaque) 322660ce86c7SWei Liu { 322760ce86c7SWei Liu int32_t fid; 322860ce86c7SWei Liu ssize_t retval = 0; 322960ce86c7SWei Liu size_t offset = 7; 323060ce86c7SWei Liu V9fsFidState *fidp; 323160ce86c7SWei Liu struct statfs stbuf; 323260ce86c7SWei Liu V9fsPDU *pdu = opaque; 323360ce86c7SWei Liu V9fsState *s = pdu->s; 323460ce86c7SWei Liu 323560ce86c7SWei Liu retval = pdu_unmarshal(pdu, offset, "d", &fid); 323660ce86c7SWei Liu if (retval < 0) { 323760ce86c7SWei Liu goto out_nofid; 323860ce86c7SWei Liu } 323960ce86c7SWei Liu fidp = get_fid(pdu, fid); 324060ce86c7SWei Liu if (fidp == NULL) { 324160ce86c7SWei Liu retval = -ENOENT; 324260ce86c7SWei Liu goto out_nofid; 324360ce86c7SWei Liu } 324460ce86c7SWei Liu retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf); 324560ce86c7SWei Liu if (retval < 0) { 324660ce86c7SWei Liu goto out; 324760ce86c7SWei Liu } 324860ce86c7SWei Liu retval = v9fs_fill_statfs(s, pdu, &stbuf); 324960ce86c7SWei Liu if (retval < 0) { 325060ce86c7SWei Liu goto out; 325160ce86c7SWei Liu } 325260ce86c7SWei Liu retval += offset; 325360ce86c7SWei Liu out: 325460ce86c7SWei Liu put_fid(pdu, fidp); 325560ce86c7SWei Liu out_nofid: 325660ce86c7SWei Liu pdu_complete(pdu, retval); 325760ce86c7SWei Liu } 325860ce86c7SWei Liu 32598440e22eSGreg Kurz static void coroutine_fn v9fs_mknod(void *opaque) 326060ce86c7SWei Liu { 326160ce86c7SWei Liu 326260ce86c7SWei Liu int mode; 326360ce86c7SWei Liu gid_t gid; 326460ce86c7SWei Liu int32_t fid; 326560ce86c7SWei Liu V9fsQID qid; 326660ce86c7SWei Liu int err = 0; 326760ce86c7SWei Liu int major, minor; 326860ce86c7SWei Liu size_t offset = 7; 326960ce86c7SWei Liu V9fsString name; 327060ce86c7SWei Liu struct stat stbuf; 327160ce86c7SWei Liu V9fsFidState *fidp; 327260ce86c7SWei Liu V9fsPDU *pdu = opaque; 327360ce86c7SWei Liu 327460ce86c7SWei Liu v9fs_string_init(&name); 327560ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode, 327660ce86c7SWei Liu &major, &minor, &gid); 327760ce86c7SWei Liu if (err < 0) { 327860ce86c7SWei Liu goto out_nofid; 327960ce86c7SWei Liu } 328060ce86c7SWei Liu trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor); 328160ce86c7SWei Liu 3282fff39a7aSGreg Kurz if (name_is_illegal(name.data)) { 3283fff39a7aSGreg Kurz err = -ENOENT; 3284fff39a7aSGreg Kurz goto out_nofid; 3285fff39a7aSGreg Kurz } 3286fff39a7aSGreg Kurz 3287805b5d98SGreg Kurz if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 3288805b5d98SGreg Kurz err = -EEXIST; 3289805b5d98SGreg Kurz goto out_nofid; 3290805b5d98SGreg Kurz } 3291805b5d98SGreg Kurz 329260ce86c7SWei Liu fidp = get_fid(pdu, fid); 329360ce86c7SWei Liu if (fidp == NULL) { 329460ce86c7SWei Liu err = -ENOENT; 329560ce86c7SWei Liu goto out_nofid; 329660ce86c7SWei Liu } 329760ce86c7SWei Liu err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid, 329860ce86c7SWei Liu makedev(major, minor), mode, &stbuf); 329960ce86c7SWei Liu if (err < 0) { 330060ce86c7SWei Liu goto out; 330160ce86c7SWei Liu } 33023b5ee9e8SAntonios Motakis err = stat_to_qid(pdu, &stbuf, &qid); 33033b5ee9e8SAntonios Motakis if (err < 0) { 33043b5ee9e8SAntonios Motakis goto out; 33053b5ee9e8SAntonios Motakis } 330660ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Q", &qid); 330760ce86c7SWei Liu if (err < 0) { 330860ce86c7SWei Liu goto out; 330960ce86c7SWei Liu } 331060ce86c7SWei Liu err += offset; 331160ce86c7SWei Liu trace_v9fs_mknod_return(pdu->tag, pdu->id, 331260ce86c7SWei Liu qid.type, qid.version, qid.path); 331360ce86c7SWei Liu out: 331460ce86c7SWei Liu put_fid(pdu, fidp); 331560ce86c7SWei Liu out_nofid: 331660ce86c7SWei Liu pdu_complete(pdu, err); 331760ce86c7SWei Liu v9fs_string_free(&name); 331860ce86c7SWei Liu } 331960ce86c7SWei Liu 332060ce86c7SWei Liu /* 332160ce86c7SWei Liu * Implement posix byte range locking code 332260ce86c7SWei Liu * Server side handling of locking code is very simple, because 9p server in 332360ce86c7SWei Liu * QEMU can handle only one client. And most of the lock handling 332460ce86c7SWei Liu * (like conflict, merging) etc is done by the VFS layer itself, so no need to 332560ce86c7SWei Liu * do any thing in * qemu 9p server side lock code path. 332660ce86c7SWei Liu * So when a TLOCK request comes, always return success 332760ce86c7SWei Liu */ 33288440e22eSGreg Kurz static void coroutine_fn v9fs_lock(void *opaque) 332960ce86c7SWei Liu { 333060ce86c7SWei Liu V9fsFlock flock; 333160ce86c7SWei Liu size_t offset = 7; 333260ce86c7SWei Liu struct stat stbuf; 333360ce86c7SWei Liu V9fsFidState *fidp; 333460ce86c7SWei Liu int32_t fid, err = 0; 333560ce86c7SWei Liu V9fsPDU *pdu = opaque; 333660ce86c7SWei Liu 333760ce86c7SWei Liu v9fs_string_init(&flock.client_id); 333860ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type, 333960ce86c7SWei Liu &flock.flags, &flock.start, &flock.length, 334060ce86c7SWei Liu &flock.proc_id, &flock.client_id); 334160ce86c7SWei Liu if (err < 0) { 334260ce86c7SWei Liu goto out_nofid; 334360ce86c7SWei Liu } 334460ce86c7SWei Liu trace_v9fs_lock(pdu->tag, pdu->id, fid, 334560ce86c7SWei Liu flock.type, flock.start, flock.length); 334660ce86c7SWei Liu 334760ce86c7SWei Liu 334860ce86c7SWei Liu /* We support only block flag now (that too ignored currently) */ 334960ce86c7SWei Liu if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) { 335060ce86c7SWei Liu err = -EINVAL; 335160ce86c7SWei Liu goto out_nofid; 335260ce86c7SWei Liu } 335360ce86c7SWei Liu fidp = get_fid(pdu, fid); 335460ce86c7SWei Liu if (fidp == NULL) { 335560ce86c7SWei Liu err = -ENOENT; 335660ce86c7SWei Liu goto out_nofid; 335760ce86c7SWei Liu } 335860ce86c7SWei Liu err = v9fs_co_fstat(pdu, fidp, &stbuf); 335960ce86c7SWei Liu if (err < 0) { 336060ce86c7SWei Liu goto out; 336160ce86c7SWei Liu } 33624bae2b39SPaolo Bonzini err = pdu_marshal(pdu, offset, "b", P9_LOCK_SUCCESS); 33634bae2b39SPaolo Bonzini if (err < 0) { 33644bae2b39SPaolo Bonzini goto out; 33654bae2b39SPaolo Bonzini } 33664bae2b39SPaolo Bonzini err += offset; 33674bae2b39SPaolo Bonzini trace_v9fs_lock_return(pdu->tag, pdu->id, P9_LOCK_SUCCESS); 336860ce86c7SWei Liu out: 336960ce86c7SWei Liu put_fid(pdu, fidp); 337060ce86c7SWei Liu out_nofid: 337160ce86c7SWei Liu pdu_complete(pdu, err); 337260ce86c7SWei Liu v9fs_string_free(&flock.client_id); 337360ce86c7SWei Liu } 337460ce86c7SWei Liu 337560ce86c7SWei Liu /* 337660ce86c7SWei Liu * When a TGETLOCK request comes, always return success because all lock 337760ce86c7SWei Liu * handling is done by client's VFS layer. 337860ce86c7SWei Liu */ 33798440e22eSGreg Kurz static void coroutine_fn v9fs_getlock(void *opaque) 338060ce86c7SWei Liu { 338160ce86c7SWei Liu size_t offset = 7; 338260ce86c7SWei Liu struct stat stbuf; 338360ce86c7SWei Liu V9fsFidState *fidp; 338460ce86c7SWei Liu V9fsGetlock glock; 338560ce86c7SWei Liu int32_t fid, err = 0; 338660ce86c7SWei Liu V9fsPDU *pdu = opaque; 338760ce86c7SWei Liu 338860ce86c7SWei Liu v9fs_string_init(&glock.client_id); 338960ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type, 339060ce86c7SWei Liu &glock.start, &glock.length, &glock.proc_id, 339160ce86c7SWei Liu &glock.client_id); 339260ce86c7SWei Liu if (err < 0) { 339360ce86c7SWei Liu goto out_nofid; 339460ce86c7SWei Liu } 339560ce86c7SWei Liu trace_v9fs_getlock(pdu->tag, pdu->id, fid, 339660ce86c7SWei Liu glock.type, glock.start, glock.length); 339760ce86c7SWei Liu 339860ce86c7SWei Liu fidp = get_fid(pdu, fid); 339960ce86c7SWei Liu if (fidp == NULL) { 340060ce86c7SWei Liu err = -ENOENT; 340160ce86c7SWei Liu goto out_nofid; 340260ce86c7SWei Liu } 340360ce86c7SWei Liu err = v9fs_co_fstat(pdu, fidp, &stbuf); 340460ce86c7SWei Liu if (err < 0) { 340560ce86c7SWei Liu goto out; 340660ce86c7SWei Liu } 340760ce86c7SWei Liu glock.type = P9_LOCK_TYPE_UNLCK; 340860ce86c7SWei Liu err = pdu_marshal(pdu, offset, "bqqds", glock.type, 340960ce86c7SWei Liu glock.start, glock.length, glock.proc_id, 341060ce86c7SWei Liu &glock.client_id); 341160ce86c7SWei Liu if (err < 0) { 341260ce86c7SWei Liu goto out; 341360ce86c7SWei Liu } 341460ce86c7SWei Liu err += offset; 341560ce86c7SWei Liu trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start, 341660ce86c7SWei Liu glock.length, glock.proc_id); 341760ce86c7SWei Liu out: 341860ce86c7SWei Liu put_fid(pdu, fidp); 341960ce86c7SWei Liu out_nofid: 342060ce86c7SWei Liu pdu_complete(pdu, err); 342160ce86c7SWei Liu v9fs_string_free(&glock.client_id); 342260ce86c7SWei Liu } 342360ce86c7SWei Liu 34248440e22eSGreg Kurz static void coroutine_fn v9fs_mkdir(void *opaque) 342560ce86c7SWei Liu { 342660ce86c7SWei Liu V9fsPDU *pdu = opaque; 342760ce86c7SWei Liu size_t offset = 7; 342860ce86c7SWei Liu int32_t fid; 342960ce86c7SWei Liu struct stat stbuf; 343060ce86c7SWei Liu V9fsQID qid; 343160ce86c7SWei Liu V9fsString name; 343260ce86c7SWei Liu V9fsFidState *fidp; 343360ce86c7SWei Liu gid_t gid; 343460ce86c7SWei Liu int mode; 343560ce86c7SWei Liu int err = 0; 343660ce86c7SWei Liu 343760ce86c7SWei Liu v9fs_string_init(&name); 343860ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid); 343960ce86c7SWei Liu if (err < 0) { 344060ce86c7SWei Liu goto out_nofid; 344160ce86c7SWei Liu } 344260ce86c7SWei Liu trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid); 344360ce86c7SWei Liu 3444fff39a7aSGreg Kurz if (name_is_illegal(name.data)) { 3445fff39a7aSGreg Kurz err = -ENOENT; 3446fff39a7aSGreg Kurz goto out_nofid; 3447fff39a7aSGreg Kurz } 3448fff39a7aSGreg Kurz 3449805b5d98SGreg Kurz if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 3450805b5d98SGreg Kurz err = -EEXIST; 3451805b5d98SGreg Kurz goto out_nofid; 3452805b5d98SGreg Kurz } 3453805b5d98SGreg Kurz 345460ce86c7SWei Liu fidp = get_fid(pdu, fid); 345560ce86c7SWei Liu if (fidp == NULL) { 345660ce86c7SWei Liu err = -ENOENT; 345760ce86c7SWei Liu goto out_nofid; 345860ce86c7SWei Liu } 345960ce86c7SWei Liu err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf); 346060ce86c7SWei Liu if (err < 0) { 346160ce86c7SWei Liu goto out; 346260ce86c7SWei Liu } 34633b5ee9e8SAntonios Motakis err = stat_to_qid(pdu, &stbuf, &qid); 34643b5ee9e8SAntonios Motakis if (err < 0) { 34653b5ee9e8SAntonios Motakis goto out; 34663b5ee9e8SAntonios Motakis } 346760ce86c7SWei Liu err = pdu_marshal(pdu, offset, "Q", &qid); 346860ce86c7SWei Liu if (err < 0) { 346960ce86c7SWei Liu goto out; 347060ce86c7SWei Liu } 347160ce86c7SWei Liu err += offset; 347260ce86c7SWei Liu trace_v9fs_mkdir_return(pdu->tag, pdu->id, 347360ce86c7SWei Liu qid.type, qid.version, qid.path, err); 347460ce86c7SWei Liu out: 347560ce86c7SWei Liu put_fid(pdu, fidp); 347660ce86c7SWei Liu out_nofid: 347760ce86c7SWei Liu pdu_complete(pdu, err); 347860ce86c7SWei Liu v9fs_string_free(&name); 347960ce86c7SWei Liu } 348060ce86c7SWei Liu 34818440e22eSGreg Kurz static void coroutine_fn v9fs_xattrwalk(void *opaque) 348260ce86c7SWei Liu { 348360ce86c7SWei Liu int64_t size; 348460ce86c7SWei Liu V9fsString name; 348560ce86c7SWei Liu ssize_t err = 0; 348660ce86c7SWei Liu size_t offset = 7; 348760ce86c7SWei Liu int32_t fid, newfid; 348860ce86c7SWei Liu V9fsFidState *file_fidp; 348960ce86c7SWei Liu V9fsFidState *xattr_fidp = NULL; 349060ce86c7SWei Liu V9fsPDU *pdu = opaque; 349160ce86c7SWei Liu V9fsState *s = pdu->s; 349260ce86c7SWei Liu 349360ce86c7SWei Liu v9fs_string_init(&name); 349460ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name); 349560ce86c7SWei Liu if (err < 0) { 349660ce86c7SWei Liu goto out_nofid; 349760ce86c7SWei Liu } 349860ce86c7SWei Liu trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data); 349960ce86c7SWei Liu 350060ce86c7SWei Liu file_fidp = get_fid(pdu, fid); 350160ce86c7SWei Liu if (file_fidp == NULL) { 350260ce86c7SWei Liu err = -ENOENT; 350360ce86c7SWei Liu goto out_nofid; 350460ce86c7SWei Liu } 350560ce86c7SWei Liu xattr_fidp = alloc_fid(s, newfid); 350660ce86c7SWei Liu if (xattr_fidp == NULL) { 350760ce86c7SWei Liu err = -EINVAL; 350860ce86c7SWei Liu goto out; 350960ce86c7SWei Liu } 351060ce86c7SWei Liu v9fs_path_copy(&xattr_fidp->path, &file_fidp->path); 3511ba42ebb8SLi Qiang if (!v9fs_string_size(&name)) { 351260ce86c7SWei Liu /* 351360ce86c7SWei Liu * listxattr request. Get the size first 351460ce86c7SWei Liu */ 351560ce86c7SWei Liu size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0); 351660ce86c7SWei Liu if (size < 0) { 351760ce86c7SWei Liu err = size; 351860ce86c7SWei Liu clunk_fid(s, xattr_fidp->fid); 351960ce86c7SWei Liu goto out; 352060ce86c7SWei Liu } 352160ce86c7SWei Liu /* 352260ce86c7SWei Liu * Read the xattr value 352360ce86c7SWei Liu */ 352460ce86c7SWei Liu xattr_fidp->fs.xattr.len = size; 352560ce86c7SWei Liu xattr_fidp->fid_type = P9_FID_XATTR; 3526dd28fbbcSLi Qiang xattr_fidp->fs.xattr.xattrwalk_fid = true; 35277bd92756SPrasad J Pandit xattr_fidp->fs.xattr.value = g_malloc0(size); 3528a647502cSKeno Fischer if (size) { 352960ce86c7SWei Liu err = v9fs_co_llistxattr(pdu, &xattr_fidp->path, 353060ce86c7SWei Liu xattr_fidp->fs.xattr.value, 353160ce86c7SWei Liu xattr_fidp->fs.xattr.len); 353260ce86c7SWei Liu if (err < 0) { 353360ce86c7SWei Liu clunk_fid(s, xattr_fidp->fid); 353460ce86c7SWei Liu goto out; 353560ce86c7SWei Liu } 353660ce86c7SWei Liu } 353760ce86c7SWei Liu err = pdu_marshal(pdu, offset, "q", size); 353860ce86c7SWei Liu if (err < 0) { 353960ce86c7SWei Liu goto out; 354060ce86c7SWei Liu } 354160ce86c7SWei Liu err += offset; 354260ce86c7SWei Liu } else { 354360ce86c7SWei Liu /* 354460ce86c7SWei Liu * specific xattr fid. We check for xattr 354560ce86c7SWei Liu * presence also collect the xattr size 354660ce86c7SWei Liu */ 354760ce86c7SWei Liu size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path, 354860ce86c7SWei Liu &name, NULL, 0); 354960ce86c7SWei Liu if (size < 0) { 355060ce86c7SWei Liu err = size; 355160ce86c7SWei Liu clunk_fid(s, xattr_fidp->fid); 355260ce86c7SWei Liu goto out; 355360ce86c7SWei Liu } 355460ce86c7SWei Liu /* 355560ce86c7SWei Liu * Read the xattr value 355660ce86c7SWei Liu */ 355760ce86c7SWei Liu xattr_fidp->fs.xattr.len = size; 355860ce86c7SWei Liu xattr_fidp->fid_type = P9_FID_XATTR; 3559dd28fbbcSLi Qiang xattr_fidp->fs.xattr.xattrwalk_fid = true; 35607bd92756SPrasad J Pandit xattr_fidp->fs.xattr.value = g_malloc0(size); 3561a647502cSKeno Fischer if (size) { 356260ce86c7SWei Liu err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path, 356360ce86c7SWei Liu &name, xattr_fidp->fs.xattr.value, 356460ce86c7SWei Liu xattr_fidp->fs.xattr.len); 356560ce86c7SWei Liu if (err < 0) { 356660ce86c7SWei Liu clunk_fid(s, xattr_fidp->fid); 356760ce86c7SWei Liu goto out; 356860ce86c7SWei Liu } 356960ce86c7SWei Liu } 357060ce86c7SWei Liu err = pdu_marshal(pdu, offset, "q", size); 357160ce86c7SWei Liu if (err < 0) { 357260ce86c7SWei Liu goto out; 357360ce86c7SWei Liu } 357460ce86c7SWei Liu err += offset; 357560ce86c7SWei Liu } 357660ce86c7SWei Liu trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size); 357760ce86c7SWei Liu out: 357860ce86c7SWei Liu put_fid(pdu, file_fidp); 357960ce86c7SWei Liu if (xattr_fidp) { 358060ce86c7SWei Liu put_fid(pdu, xattr_fidp); 358160ce86c7SWei Liu } 358260ce86c7SWei Liu out_nofid: 358360ce86c7SWei Liu pdu_complete(pdu, err); 358460ce86c7SWei Liu v9fs_string_free(&name); 358560ce86c7SWei Liu } 358660ce86c7SWei Liu 35878440e22eSGreg Kurz static void coroutine_fn v9fs_xattrcreate(void *opaque) 358860ce86c7SWei Liu { 3589aca6897fSKeno Fischer int flags, rflags = 0; 359060ce86c7SWei Liu int32_t fid; 35913b79ef2cSGreg Kurz uint64_t size; 359260ce86c7SWei Liu ssize_t err = 0; 359360ce86c7SWei Liu V9fsString name; 359460ce86c7SWei Liu size_t offset = 7; 359560ce86c7SWei Liu V9fsFidState *file_fidp; 359660ce86c7SWei Liu V9fsFidState *xattr_fidp; 359760ce86c7SWei Liu V9fsPDU *pdu = opaque; 359860ce86c7SWei Liu 359960ce86c7SWei Liu v9fs_string_init(&name); 360060ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags); 360160ce86c7SWei Liu if (err < 0) { 360260ce86c7SWei Liu goto out_nofid; 360360ce86c7SWei Liu } 360460ce86c7SWei Liu trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags); 360560ce86c7SWei Liu 3606aca6897fSKeno Fischer if (flags & ~(P9_XATTR_CREATE | P9_XATTR_REPLACE)) { 3607aca6897fSKeno Fischer err = -EINVAL; 3608aca6897fSKeno Fischer goto out_nofid; 3609aca6897fSKeno Fischer } 3610aca6897fSKeno Fischer 3611aca6897fSKeno Fischer if (flags & P9_XATTR_CREATE) { 3612aca6897fSKeno Fischer rflags |= XATTR_CREATE; 3613aca6897fSKeno Fischer } 3614aca6897fSKeno Fischer 3615aca6897fSKeno Fischer if (flags & P9_XATTR_REPLACE) { 3616aca6897fSKeno Fischer rflags |= XATTR_REPLACE; 3617aca6897fSKeno Fischer } 3618aca6897fSKeno Fischer 36193b79ef2cSGreg Kurz if (size > XATTR_SIZE_MAX) { 36203b79ef2cSGreg Kurz err = -E2BIG; 36213b79ef2cSGreg Kurz goto out_nofid; 36223b79ef2cSGreg Kurz } 36233b79ef2cSGreg Kurz 362460ce86c7SWei Liu file_fidp = get_fid(pdu, fid); 362560ce86c7SWei Liu if (file_fidp == NULL) { 362660ce86c7SWei Liu err = -EINVAL; 362760ce86c7SWei Liu goto out_nofid; 362860ce86c7SWei Liu } 3629dd654e03SGreg Kurz if (file_fidp->fid_type != P9_FID_NONE) { 3630dd654e03SGreg Kurz err = -EINVAL; 3631dd654e03SGreg Kurz goto out_put_fid; 3632dd654e03SGreg Kurz } 3633dd654e03SGreg Kurz 363460ce86c7SWei Liu /* Make the file fid point to xattr */ 363560ce86c7SWei Liu xattr_fidp = file_fidp; 363660ce86c7SWei Liu xattr_fidp->fid_type = P9_FID_XATTR; 363760ce86c7SWei Liu xattr_fidp->fs.xattr.copied_len = 0; 3638dd28fbbcSLi Qiang xattr_fidp->fs.xattr.xattrwalk_fid = false; 363960ce86c7SWei Liu xattr_fidp->fs.xattr.len = size; 3640aca6897fSKeno Fischer xattr_fidp->fs.xattr.flags = rflags; 364160ce86c7SWei Liu v9fs_string_init(&xattr_fidp->fs.xattr.name); 364260ce86c7SWei Liu v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name); 3643eb687602SLi Qiang xattr_fidp->fs.xattr.value = g_malloc0(size); 364460ce86c7SWei Liu err = offset; 3645dd654e03SGreg Kurz out_put_fid: 364660ce86c7SWei Liu put_fid(pdu, file_fidp); 364760ce86c7SWei Liu out_nofid: 364860ce86c7SWei Liu pdu_complete(pdu, err); 364960ce86c7SWei Liu v9fs_string_free(&name); 365060ce86c7SWei Liu } 365160ce86c7SWei Liu 36528440e22eSGreg Kurz static void coroutine_fn v9fs_readlink(void *opaque) 365360ce86c7SWei Liu { 365460ce86c7SWei Liu V9fsPDU *pdu = opaque; 365560ce86c7SWei Liu size_t offset = 7; 365660ce86c7SWei Liu V9fsString target; 365760ce86c7SWei Liu int32_t fid; 365860ce86c7SWei Liu int err = 0; 365960ce86c7SWei Liu V9fsFidState *fidp; 366060ce86c7SWei Liu 366160ce86c7SWei Liu err = pdu_unmarshal(pdu, offset, "d", &fid); 366260ce86c7SWei Liu if (err < 0) { 366360ce86c7SWei Liu goto out_nofid; 366460ce86c7SWei Liu } 366560ce86c7SWei Liu trace_v9fs_readlink(pdu->tag, pdu->id, fid); 366660ce86c7SWei Liu fidp = get_fid(pdu, fid); 366760ce86c7SWei Liu if (fidp == NULL) { 366860ce86c7SWei Liu err = -ENOENT; 366960ce86c7SWei Liu goto out_nofid; 367060ce86c7SWei Liu } 367160ce86c7SWei Liu 367260ce86c7SWei Liu v9fs_string_init(&target); 367360ce86c7SWei Liu err = v9fs_co_readlink(pdu, &fidp->path, &target); 367460ce86c7SWei Liu if (err < 0) { 367560ce86c7SWei Liu goto out; 367660ce86c7SWei Liu } 367760ce86c7SWei Liu err = pdu_marshal(pdu, offset, "s", &target); 367860ce86c7SWei Liu if (err < 0) { 367960ce86c7SWei Liu v9fs_string_free(&target); 368060ce86c7SWei Liu goto out; 368160ce86c7SWei Liu } 368260ce86c7SWei Liu err += offset; 368360ce86c7SWei Liu trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data); 368460ce86c7SWei Liu v9fs_string_free(&target); 368560ce86c7SWei Liu out: 368660ce86c7SWei Liu put_fid(pdu, fidp); 368760ce86c7SWei Liu out_nofid: 368860ce86c7SWei Liu pdu_complete(pdu, err); 368960ce86c7SWei Liu } 369060ce86c7SWei Liu 369160ce86c7SWei Liu static CoroutineEntry *pdu_co_handlers[] = { 369260ce86c7SWei Liu [P9_TREADDIR] = v9fs_readdir, 369360ce86c7SWei Liu [P9_TSTATFS] = v9fs_statfs, 369460ce86c7SWei Liu [P9_TGETATTR] = v9fs_getattr, 369560ce86c7SWei Liu [P9_TSETATTR] = v9fs_setattr, 369660ce86c7SWei Liu [P9_TXATTRWALK] = v9fs_xattrwalk, 369760ce86c7SWei Liu [P9_TXATTRCREATE] = v9fs_xattrcreate, 369860ce86c7SWei Liu [P9_TMKNOD] = v9fs_mknod, 369960ce86c7SWei Liu [P9_TRENAME] = v9fs_rename, 370060ce86c7SWei Liu [P9_TLOCK] = v9fs_lock, 370160ce86c7SWei Liu [P9_TGETLOCK] = v9fs_getlock, 370260ce86c7SWei Liu [P9_TRENAMEAT] = v9fs_renameat, 370360ce86c7SWei Liu [P9_TREADLINK] = v9fs_readlink, 370460ce86c7SWei Liu [P9_TUNLINKAT] = v9fs_unlinkat, 370560ce86c7SWei Liu [P9_TMKDIR] = v9fs_mkdir, 370660ce86c7SWei Liu [P9_TVERSION] = v9fs_version, 370760ce86c7SWei Liu [P9_TLOPEN] = v9fs_open, 370860ce86c7SWei Liu [P9_TATTACH] = v9fs_attach, 370960ce86c7SWei Liu [P9_TSTAT] = v9fs_stat, 371060ce86c7SWei Liu [P9_TWALK] = v9fs_walk, 371160ce86c7SWei Liu [P9_TCLUNK] = v9fs_clunk, 371260ce86c7SWei Liu [P9_TFSYNC] = v9fs_fsync, 371360ce86c7SWei Liu [P9_TOPEN] = v9fs_open, 371460ce86c7SWei Liu [P9_TREAD] = v9fs_read, 371560ce86c7SWei Liu #if 0 371660ce86c7SWei Liu [P9_TAUTH] = v9fs_auth, 371760ce86c7SWei Liu #endif 371860ce86c7SWei Liu [P9_TFLUSH] = v9fs_flush, 371960ce86c7SWei Liu [P9_TLINK] = v9fs_link, 372060ce86c7SWei Liu [P9_TSYMLINK] = v9fs_symlink, 372160ce86c7SWei Liu [P9_TCREATE] = v9fs_create, 372260ce86c7SWei Liu [P9_TLCREATE] = v9fs_lcreate, 372360ce86c7SWei Liu [P9_TWRITE] = v9fs_write, 372460ce86c7SWei Liu [P9_TWSTAT] = v9fs_wstat, 372560ce86c7SWei Liu [P9_TREMOVE] = v9fs_remove, 372660ce86c7SWei Liu }; 372760ce86c7SWei Liu 37288440e22eSGreg Kurz static void coroutine_fn v9fs_op_not_supp(void *opaque) 372960ce86c7SWei Liu { 373060ce86c7SWei Liu V9fsPDU *pdu = opaque; 373160ce86c7SWei Liu pdu_complete(pdu, -EOPNOTSUPP); 373260ce86c7SWei Liu } 373360ce86c7SWei Liu 37348440e22eSGreg Kurz static void coroutine_fn v9fs_fs_ro(void *opaque) 373560ce86c7SWei Liu { 373660ce86c7SWei Liu V9fsPDU *pdu = opaque; 373760ce86c7SWei Liu pdu_complete(pdu, -EROFS); 373860ce86c7SWei Liu } 373960ce86c7SWei Liu 374060ce86c7SWei Liu static inline bool is_read_only_op(V9fsPDU *pdu) 374160ce86c7SWei Liu { 374260ce86c7SWei Liu switch (pdu->id) { 374360ce86c7SWei Liu case P9_TREADDIR: 374460ce86c7SWei Liu case P9_TSTATFS: 374560ce86c7SWei Liu case P9_TGETATTR: 374660ce86c7SWei Liu case P9_TXATTRWALK: 374760ce86c7SWei Liu case P9_TLOCK: 374860ce86c7SWei Liu case P9_TGETLOCK: 374960ce86c7SWei Liu case P9_TREADLINK: 375060ce86c7SWei Liu case P9_TVERSION: 375160ce86c7SWei Liu case P9_TLOPEN: 375260ce86c7SWei Liu case P9_TATTACH: 375360ce86c7SWei Liu case P9_TSTAT: 375460ce86c7SWei Liu case P9_TWALK: 375560ce86c7SWei Liu case P9_TCLUNK: 375660ce86c7SWei Liu case P9_TFSYNC: 375760ce86c7SWei Liu case P9_TOPEN: 375860ce86c7SWei Liu case P9_TREAD: 375960ce86c7SWei Liu case P9_TAUTH: 376060ce86c7SWei Liu case P9_TFLUSH: 376160ce86c7SWei Liu return 1; 376260ce86c7SWei Liu default: 376360ce86c7SWei Liu return 0; 376460ce86c7SWei Liu } 376560ce86c7SWei Liu } 376660ce86c7SWei Liu 3767506f3275SGreg Kurz void pdu_submit(V9fsPDU *pdu, P9MsgHeader *hdr) 376860ce86c7SWei Liu { 376960ce86c7SWei Liu Coroutine *co; 377060ce86c7SWei Liu CoroutineEntry *handler; 377160ce86c7SWei Liu V9fsState *s = pdu->s; 377260ce86c7SWei Liu 3773506f3275SGreg Kurz pdu->size = le32_to_cpu(hdr->size_le); 3774506f3275SGreg Kurz pdu->id = hdr->id; 3775506f3275SGreg Kurz pdu->tag = le16_to_cpu(hdr->tag_le); 3776506f3275SGreg Kurz 377760ce86c7SWei Liu if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) || 377860ce86c7SWei Liu (pdu_co_handlers[pdu->id] == NULL)) { 377960ce86c7SWei Liu handler = v9fs_op_not_supp; 3780d1471233SGreg Kurz } else if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) { 3781d1471233SGreg Kurz handler = v9fs_fs_ro; 378260ce86c7SWei Liu } else { 378360ce86c7SWei Liu handler = pdu_co_handlers[pdu->id]; 378460ce86c7SWei Liu } 378560ce86c7SWei Liu 3786506f3275SGreg Kurz qemu_co_queue_init(&pdu->complete); 37870b8b8753SPaolo Bonzini co = qemu_coroutine_create(handler, pdu); 37880b8b8753SPaolo Bonzini qemu_coroutine_enter(co); 378960ce86c7SWei Liu } 379060ce86c7SWei Liu 37912a0c56aaSWei Liu /* Returns 0 on success, 1 on failure. */ 3792066eb006SGreg Kurz int v9fs_device_realize_common(V9fsState *s, const V9fsTransport *t, 3793066eb006SGreg Kurz Error **errp) 37942a0c56aaSWei Liu { 37952a0c56aaSWei Liu int i, len; 37962a0c56aaSWei Liu struct stat stat; 37972a0c56aaSWei Liu FsDriverEntry *fse; 37982a0c56aaSWei Liu V9fsPath path; 37992a0c56aaSWei Liu int rc = 1; 38002a0c56aaSWei Liu 3801066eb006SGreg Kurz assert(!s->transport); 3802066eb006SGreg Kurz s->transport = t; 3803066eb006SGreg Kurz 38042a0c56aaSWei Liu /* initialize pdu allocator */ 38052a0c56aaSWei Liu QLIST_INIT(&s->free_list); 38062a0c56aaSWei Liu QLIST_INIT(&s->active_list); 38070d78289cSGreg Kurz for (i = 0; i < MAX_REQ; i++) { 3808583f21f8SStefano Stabellini QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next); 3809583f21f8SStefano Stabellini s->pdus[i].s = s; 3810583f21f8SStefano Stabellini s->pdus[i].idx = i; 38112a0c56aaSWei Liu } 38122a0c56aaSWei Liu 38132a0c56aaSWei Liu v9fs_path_init(&path); 38142a0c56aaSWei Liu 38152a0c56aaSWei Liu fse = get_fsdev_fsentry(s->fsconf.fsdev_id); 38162a0c56aaSWei Liu 38172a0c56aaSWei Liu if (!fse) { 38182a0c56aaSWei Liu /* We don't have a fsdev identified by fsdev_id */ 38192a0c56aaSWei Liu error_setg(errp, "9pfs device couldn't find fsdev with the " 38202a0c56aaSWei Liu "id = %s", 38212a0c56aaSWei Liu s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL"); 38222a0c56aaSWei Liu goto out; 38232a0c56aaSWei Liu } 38242a0c56aaSWei Liu 38252a0c56aaSWei Liu if (!s->fsconf.tag) { 38262a0c56aaSWei Liu /* we haven't specified a mount_tag */ 38272a0c56aaSWei Liu error_setg(errp, "fsdev with id %s needs mount_tag arguments", 38282a0c56aaSWei Liu s->fsconf.fsdev_id); 38292a0c56aaSWei Liu goto out; 38302a0c56aaSWei Liu } 38312a0c56aaSWei Liu 38322a0c56aaSWei Liu s->ctx.export_flags = fse->export_flags; 38332a0c56aaSWei Liu s->ctx.fs_root = g_strdup(fse->path); 38342a0c56aaSWei Liu s->ctx.exops.get_st_gen = NULL; 38352a0c56aaSWei Liu len = strlen(s->fsconf.tag); 38362a0c56aaSWei Liu if (len > MAX_TAG_LEN - 1) { 38372a0c56aaSWei Liu error_setg(errp, "mount tag '%s' (%d bytes) is longer than " 38382a0c56aaSWei Liu "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1); 38392a0c56aaSWei Liu goto out; 38402a0c56aaSWei Liu } 38412a0c56aaSWei Liu 38422a0c56aaSWei Liu s->tag = g_strdup(s->fsconf.tag); 38432a0c56aaSWei Liu s->ctx.uid = -1; 38442a0c56aaSWei Liu 38452a0c56aaSWei Liu s->ops = fse->ops; 38462a0c56aaSWei Liu 3847b96feb2cSTobias Schramm s->ctx.fmode = fse->fmode; 3848b96feb2cSTobias Schramm s->ctx.dmode = fse->dmode; 3849b96feb2cSTobias Schramm 38502a0c56aaSWei Liu s->fid_list = NULL; 38512a0c56aaSWei Liu qemu_co_rwlock_init(&s->rename_lock); 38522a0c56aaSWei Liu 385365603a80SGreg Kurz if (s->ops->init(&s->ctx, errp) < 0) { 385465603a80SGreg Kurz error_prepend(errp, "cannot initialize fsdev '%s': ", 385565603a80SGreg Kurz s->fsconf.fsdev_id); 38562a0c56aaSWei Liu goto out; 38572a0c56aaSWei Liu } 38582a0c56aaSWei Liu 38592a0c56aaSWei Liu /* 38602a0c56aaSWei Liu * Check details of export path, We need to use fs driver 38612a0c56aaSWei Liu * call back to do that. Since we are in the init path, we don't 38622a0c56aaSWei Liu * use co-routines here. 38632a0c56aaSWei Liu */ 38642a0c56aaSWei Liu if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) { 38652a0c56aaSWei Liu error_setg(errp, 38662a0c56aaSWei Liu "error in converting name to path %s", strerror(errno)); 38672a0c56aaSWei Liu goto out; 38682a0c56aaSWei Liu } 38692a0c56aaSWei Liu if (s->ops->lstat(&s->ctx, &path, &stat)) { 38702a0c56aaSWei Liu error_setg(errp, "share path %s does not exist", fse->path); 38712a0c56aaSWei Liu goto out; 38722a0c56aaSWei Liu } else if (!S_ISDIR(stat.st_mode)) { 38732a0c56aaSWei Liu error_setg(errp, "share path %s is not a directory", fse->path); 38742a0c56aaSWei Liu goto out; 38752a0c56aaSWei Liu } 3876b8bbdb88SPradeep Jagadeesh 38773b5ee9e8SAntonios Motakis s->dev_id = stat.st_dev; 38783b5ee9e8SAntonios Motakis 38791a6ed33cSAntonios Motakis qpp_table_init(&s->qpp_table); 38801a6ed33cSAntonios Motakis s->qp_prefix_next = 1; /* reserve 0 to detect overflow */ 3881*f3fe4a2dSAntonios Motakis s->qp_fullpath_next = 1; 38821a6ed33cSAntonios Motakis 3883b8bbdb88SPradeep Jagadeesh s->ctx.fst = &fse->fst; 3884b8bbdb88SPradeep Jagadeesh fsdev_throttle_init(s->ctx.fst); 3885b8bbdb88SPradeep Jagadeesh 38862a0c56aaSWei Liu rc = 0; 38872a0c56aaSWei Liu out: 38882a0c56aaSWei Liu if (rc) { 3889c0da0cb7SGreg Kurz v9fs_device_unrealize_common(s, NULL); 3890702dbcc2SLi Qiang } 38912a0c56aaSWei Liu v9fs_path_free(&path); 38922a0c56aaSWei Liu return rc; 38932a0c56aaSWei Liu } 38942a0c56aaSWei Liu 38952a0c56aaSWei Liu void v9fs_device_unrealize_common(V9fsState *s, Error **errp) 38962a0c56aaSWei Liu { 3897c0da0cb7SGreg Kurz if (s->ops && s->ops->cleanup) { 3898702dbcc2SLi Qiang s->ops->cleanup(&s->ctx); 3899702dbcc2SLi Qiang } 3900c0da0cb7SGreg Kurz if (s->ctx.fst) { 3901b8bbdb88SPradeep Jagadeesh fsdev_throttle_cleanup(s->ctx.fst); 3902c0da0cb7SGreg Kurz } 39032a0c56aaSWei Liu g_free(s->tag); 3904*f3fe4a2dSAntonios Motakis qp_table_destroy(&s->qpp_table); 3905*f3fe4a2dSAntonios Motakis qp_table_destroy(&s->qpf_table); 39064774718eSLi Qiang g_free(s->ctx.fs_root); 39072a0c56aaSWei Liu } 39082a0c56aaSWei Liu 39090e44a0fdSGreg Kurz typedef struct VirtfsCoResetData { 39100e44a0fdSGreg Kurz V9fsPDU pdu; 39110e44a0fdSGreg Kurz bool done; 39120e44a0fdSGreg Kurz } VirtfsCoResetData; 39130e44a0fdSGreg Kurz 39140e44a0fdSGreg Kurz static void coroutine_fn virtfs_co_reset(void *opaque) 39150e44a0fdSGreg Kurz { 39160e44a0fdSGreg Kurz VirtfsCoResetData *data = opaque; 39170e44a0fdSGreg Kurz 39180e44a0fdSGreg Kurz virtfs_reset(&data->pdu); 39190e44a0fdSGreg Kurz data->done = true; 39200e44a0fdSGreg Kurz } 39210e44a0fdSGreg Kurz 39220e44a0fdSGreg Kurz void v9fs_reset(V9fsState *s) 39230e44a0fdSGreg Kurz { 39240e44a0fdSGreg Kurz VirtfsCoResetData data = { .pdu = { .s = s }, .done = false }; 39250e44a0fdSGreg Kurz Coroutine *co; 39260e44a0fdSGreg Kurz 39270e44a0fdSGreg Kurz while (!QLIST_EMPTY(&s->active_list)) { 39280e44a0fdSGreg Kurz aio_poll(qemu_get_aio_context(), true); 39290e44a0fdSGreg Kurz } 39300e44a0fdSGreg Kurz 39310e44a0fdSGreg Kurz co = qemu_coroutine_create(virtfs_co_reset, &data); 39320e44a0fdSGreg Kurz qemu_coroutine_enter(co); 39330e44a0fdSGreg Kurz 39340e44a0fdSGreg Kurz while (!data.done) { 39350e44a0fdSGreg Kurz aio_poll(qemu_get_aio_context(), true); 39360e44a0fdSGreg Kurz } 39370e44a0fdSGreg Kurz } 39380e44a0fdSGreg Kurz 393960ce86c7SWei Liu static void __attribute__((__constructor__)) v9fs_set_fd_limit(void) 394060ce86c7SWei Liu { 394160ce86c7SWei Liu struct rlimit rlim; 394260ce86c7SWei Liu if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) { 394363325b18SGreg Kurz error_report("Failed to get the resource limit"); 394460ce86c7SWei Liu exit(1); 394560ce86c7SWei Liu } 394660ce86c7SWei Liu open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur/3); 394760ce86c7SWei Liu open_fd_rc = rlim.rlim_cur/2; 394860ce86c7SWei Liu } 3949