1364031f1SWei Liu /* 2aae91ad9SGreg Kurz * 9p synthetic file system support 3364031f1SWei Liu * 4364031f1SWei Liu * Copyright IBM, Corp. 2011 5364031f1SWei Liu * 6364031f1SWei Liu * Authors: 7364031f1SWei Liu * Malahal Naineni <malahal@us.ibm.com> 8364031f1SWei Liu * Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com> 9364031f1SWei Liu * 10364031f1SWei Liu * This work is licensed under the terms of the GNU GPL, version 2. See 11364031f1SWei Liu * the COPYING file in the top-level directory. 12364031f1SWei Liu * 13364031f1SWei Liu */ 14364031f1SWei Liu 156f569084SChristian Schoenebeck /* 166f569084SChristian Schoenebeck * Not so fast! You might want to read the 9p developer docs first: 176f569084SChristian Schoenebeck * https://wiki.qemu.org/Documentation/9p 186f569084SChristian Schoenebeck */ 196f569084SChristian Schoenebeck 20fbc04127SPeter Maydell #include "qemu/osdep.h" 21ebe74f8bSWei Liu #include "9p.h" 22364031f1SWei Liu #include "fsdev/qemu-fsdev.h" 23364031f1SWei Liu #include "9p-synth.h" 24364031f1SWei Liu #include "qemu/rcu.h" 25364031f1SWei Liu #include "qemu/rcu_queue.h" 26f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 272893ddd5SGreg Kurz #include "sysemu/qtest.h" 28364031f1SWei Liu 29364031f1SWei Liu /* Root node for synth file system */ 30b05528b5SGreg Kurz static V9fsSynthNode synth_root = { 31364031f1SWei Liu .name = "/", 32364031f1SWei Liu .actual_attr = { 33364031f1SWei Liu .mode = 0555 | S_IFDIR, 34364031f1SWei Liu .nlink = 1, 35364031f1SWei Liu }, 36b05528b5SGreg Kurz .attr = &synth_root.actual_attr, 37364031f1SWei Liu }; 38364031f1SWei Liu 39b05528b5SGreg Kurz static QemuMutex synth_mutex; 40b05528b5SGreg Kurz static int synth_node_count; 41364031f1SWei Liu /* set to 1 when the synth fs is ready */ 42b05528b5SGreg Kurz static int synth_fs; 43364031f1SWei Liu 44364031f1SWei Liu static V9fsSynthNode *v9fs_add_dir_node(V9fsSynthNode *parent, int mode, 45364031f1SWei Liu const char *name, 46364031f1SWei Liu V9fsSynthNodeAttr *attr, int inode) 47364031f1SWei Liu { 48364031f1SWei Liu V9fsSynthNode *node; 49364031f1SWei Liu 50364031f1SWei Liu /* Add directory type and remove write bits */ 51364031f1SWei Liu mode = ((mode & 0777) | S_IFDIR) & ~(S_IWUSR | S_IWGRP | S_IWOTH); 521366244aSMarkus Armbruster node = g_new0(V9fsSynthNode, 1); 53364031f1SWei Liu if (attr) { 54364031f1SWei Liu /* We are adding .. or . entries */ 55364031f1SWei Liu node->attr = attr; 56364031f1SWei Liu node->attr->nlink++; 57364031f1SWei Liu } else { 58364031f1SWei Liu node->attr = &node->actual_attr; 59364031f1SWei Liu node->attr->inode = inode; 60364031f1SWei Liu node->attr->nlink = 1; 61364031f1SWei Liu /* We don't allow write to directories */ 62364031f1SWei Liu node->attr->mode = mode; 63364031f1SWei Liu node->attr->write = NULL; 64364031f1SWei Liu node->attr->read = NULL; 65364031f1SWei Liu } 66364031f1SWei Liu node->private = node; 67364031f1SWei Liu pstrcpy(node->name, sizeof(node->name), name); 68364031f1SWei Liu QLIST_INSERT_HEAD_RCU(&parent->child, node, sibling); 69364031f1SWei Liu return node; 70364031f1SWei Liu } 71364031f1SWei Liu 72364031f1SWei Liu int qemu_v9fs_synth_mkdir(V9fsSynthNode *parent, int mode, 73364031f1SWei Liu const char *name, V9fsSynthNode **result) 74364031f1SWei Liu { 75364031f1SWei Liu int ret; 76364031f1SWei Liu V9fsSynthNode *node, *tmp; 77364031f1SWei Liu 78b05528b5SGreg Kurz if (!synth_fs) { 79364031f1SWei Liu return EAGAIN; 80364031f1SWei Liu } 81364031f1SWei Liu if (!name || (strlen(name) >= NAME_MAX)) { 82364031f1SWei Liu return EINVAL; 83364031f1SWei Liu } 84364031f1SWei Liu if (!parent) { 85b05528b5SGreg Kurz parent = &synth_root; 86364031f1SWei Liu } 87e4fd889fSMahmoud Mandour QEMU_LOCK_GUARD(&synth_mutex); 88364031f1SWei Liu QLIST_FOREACH(tmp, &parent->child, sibling) { 89364031f1SWei Liu if (!strcmp(tmp->name, name)) { 90364031f1SWei Liu ret = EEXIST; 91e4fd889fSMahmoud Mandour return ret; 92364031f1SWei Liu } 93364031f1SWei Liu } 94364031f1SWei Liu /* Add the name */ 95*0009df31SChristian Schoenebeck node = v9fs_add_dir_node(parent, mode, name, NULL, ++synth_node_count); 96364031f1SWei Liu v9fs_add_dir_node(node, parent->attr->mode, "..", 97364031f1SWei Liu parent->attr, parent->attr->inode); 98364031f1SWei Liu v9fs_add_dir_node(node, node->attr->mode, ".", 99364031f1SWei Liu node->attr, node->attr->inode); 100364031f1SWei Liu *result = node; 101364031f1SWei Liu ret = 0; 102364031f1SWei Liu return ret; 103364031f1SWei Liu } 104364031f1SWei Liu 105364031f1SWei Liu int qemu_v9fs_synth_add_file(V9fsSynthNode *parent, int mode, 106364031f1SWei Liu const char *name, v9fs_synth_read read, 107364031f1SWei Liu v9fs_synth_write write, void *arg) 108364031f1SWei Liu { 109364031f1SWei Liu int ret; 110364031f1SWei Liu V9fsSynthNode *node, *tmp; 111364031f1SWei Liu 112b05528b5SGreg Kurz if (!synth_fs) { 113364031f1SWei Liu return EAGAIN; 114364031f1SWei Liu } 115364031f1SWei Liu if (!name || (strlen(name) >= NAME_MAX)) { 116364031f1SWei Liu return EINVAL; 117364031f1SWei Liu } 118364031f1SWei Liu if (!parent) { 119b05528b5SGreg Kurz parent = &synth_root; 120364031f1SWei Liu } 121364031f1SWei Liu 122e4fd889fSMahmoud Mandour QEMU_LOCK_GUARD(&synth_mutex); 123364031f1SWei Liu QLIST_FOREACH(tmp, &parent->child, sibling) { 124364031f1SWei Liu if (!strcmp(tmp->name, name)) { 125364031f1SWei Liu ret = EEXIST; 126e4fd889fSMahmoud Mandour return ret; 127364031f1SWei Liu } 128364031f1SWei Liu } 129364031f1SWei Liu /* Add file type and remove write bits */ 130364031f1SWei Liu mode = ((mode & 0777) | S_IFREG); 1311366244aSMarkus Armbruster node = g_new0(V9fsSynthNode, 1); 132364031f1SWei Liu node->attr = &node->actual_attr; 133*0009df31SChristian Schoenebeck node->attr->inode = ++synth_node_count; 134364031f1SWei Liu node->attr->nlink = 1; 135364031f1SWei Liu node->attr->read = read; 136364031f1SWei Liu node->attr->write = write; 137364031f1SWei Liu node->attr->mode = mode; 138364031f1SWei Liu node->private = arg; 139364031f1SWei Liu pstrcpy(node->name, sizeof(node->name), name); 140364031f1SWei Liu QLIST_INSERT_HEAD_RCU(&parent->child, node, sibling); 141364031f1SWei Liu ret = 0; 142364031f1SWei Liu return ret; 143364031f1SWei Liu } 144364031f1SWei Liu 145b05528b5SGreg Kurz static void synth_fill_statbuf(V9fsSynthNode *node, struct stat *stbuf) 146364031f1SWei Liu { 147364031f1SWei Liu stbuf->st_dev = 0; 148364031f1SWei Liu stbuf->st_ino = node->attr->inode; 149364031f1SWei Liu stbuf->st_mode = node->attr->mode; 150364031f1SWei Liu stbuf->st_nlink = node->attr->nlink; 151364031f1SWei Liu stbuf->st_uid = 0; 152364031f1SWei Liu stbuf->st_gid = 0; 153364031f1SWei Liu stbuf->st_rdev = 0; 154364031f1SWei Liu stbuf->st_size = 0; 155364031f1SWei Liu stbuf->st_blksize = 0; 156364031f1SWei Liu stbuf->st_blocks = 0; 157364031f1SWei Liu stbuf->st_atime = 0; 158364031f1SWei Liu stbuf->st_mtime = 0; 159364031f1SWei Liu stbuf->st_ctime = 0; 160364031f1SWei Liu } 161364031f1SWei Liu 162b05528b5SGreg Kurz static int synth_lstat(FsContext *fs_ctx, 163364031f1SWei Liu V9fsPath *fs_path, struct stat *stbuf) 164364031f1SWei Liu { 165364031f1SWei Liu V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data; 166364031f1SWei Liu 167b05528b5SGreg Kurz synth_fill_statbuf(node, stbuf); 168364031f1SWei Liu return 0; 169364031f1SWei Liu } 170364031f1SWei Liu 171b05528b5SGreg Kurz static int synth_fstat(FsContext *fs_ctx, int fid_type, 172364031f1SWei Liu V9fsFidOpenState *fs, struct stat *stbuf) 173364031f1SWei Liu { 174364031f1SWei Liu V9fsSynthOpenState *synth_open = fs->private; 175b05528b5SGreg Kurz synth_fill_statbuf(synth_open->node, stbuf); 176364031f1SWei Liu return 0; 177364031f1SWei Liu } 178364031f1SWei Liu 179b05528b5SGreg Kurz static int synth_opendir(FsContext *ctx, 180364031f1SWei Liu V9fsPath *fs_path, V9fsFidOpenState *fs) 181364031f1SWei Liu { 182364031f1SWei Liu V9fsSynthOpenState *synth_open; 183364031f1SWei Liu V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data; 184364031f1SWei Liu 185e64e27d5SVitaly Chikunov /* 186e64e27d5SVitaly Chikunov * V9fsSynthOpenState contains 'struct dirent' which have OS-specific 187e64e27d5SVitaly Chikunov * properties, thus it's zero cleared on allocation here and below 188e64e27d5SVitaly Chikunov * in synth_open. 189e64e27d5SVitaly Chikunov */ 190e64e27d5SVitaly Chikunov synth_open = g_new0(V9fsSynthOpenState, 1); 191364031f1SWei Liu synth_open->node = node; 192364031f1SWei Liu node->open_count++; 193364031f1SWei Liu fs->private = synth_open; 194364031f1SWei Liu return 0; 195364031f1SWei Liu } 196364031f1SWei Liu 197b05528b5SGreg Kurz static int synth_closedir(FsContext *ctx, V9fsFidOpenState *fs) 198364031f1SWei Liu { 199364031f1SWei Liu V9fsSynthOpenState *synth_open = fs->private; 200364031f1SWei Liu V9fsSynthNode *node = synth_open->node; 201364031f1SWei Liu 202364031f1SWei Liu node->open_count--; 203364031f1SWei Liu g_free(synth_open); 204364031f1SWei Liu fs->private = NULL; 205364031f1SWei Liu return 0; 206364031f1SWei Liu } 207364031f1SWei Liu 208b05528b5SGreg Kurz static off_t synth_telldir(FsContext *ctx, V9fsFidOpenState *fs) 209364031f1SWei Liu { 210364031f1SWei Liu V9fsSynthOpenState *synth_open = fs->private; 211364031f1SWei Liu return synth_open->offset; 212364031f1SWei Liu } 213364031f1SWei Liu 214b05528b5SGreg Kurz static void synth_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off) 215364031f1SWei Liu { 216364031f1SWei Liu V9fsSynthOpenState *synth_open = fs->private; 217364031f1SWei Liu synth_open->offset = off; 218364031f1SWei Liu } 219364031f1SWei Liu 220b05528b5SGreg Kurz static void synth_rewinddir(FsContext *ctx, V9fsFidOpenState *fs) 221364031f1SWei Liu { 222b05528b5SGreg Kurz synth_seekdir(ctx, fs, 0); 223364031f1SWei Liu } 224364031f1SWei Liu 225b05528b5SGreg Kurz static void synth_direntry(V9fsSynthNode *node, 226364031f1SWei Liu struct dirent *entry, off_t off) 227364031f1SWei Liu { 228e64e27d5SVitaly Chikunov size_t sz = strlen(node->name) + 1; 229e64e27d5SVitaly Chikunov /* 230e64e27d5SVitaly Chikunov * 'entry' is always inside of V9fsSynthOpenState which have NAME_MAX 231e64e27d5SVitaly Chikunov * back padding. Ensure we do not overflow it. 232e64e27d5SVitaly Chikunov */ 233e64e27d5SVitaly Chikunov g_assert(sizeof(struct dirent) + NAME_MAX >= 234e64e27d5SVitaly Chikunov offsetof(struct dirent, d_name) + sz); 235e64e27d5SVitaly Chikunov memcpy(entry->d_name, node->name, sz); 236364031f1SWei Liu entry->d_ino = node->attr->inode; 2376b3b279bSKeno Fischer #ifdef CONFIG_DARWIN 2386b3b279bSKeno Fischer entry->d_seekoff = off + 1; 2396b3b279bSKeno Fischer #else 240364031f1SWei Liu entry->d_off = off + 1; 2416b3b279bSKeno Fischer #endif 242364031f1SWei Liu } 243364031f1SWei Liu 244b05528b5SGreg Kurz static struct dirent *synth_get_dentry(V9fsSynthNode *dir, 245635324e8SGreg Kurz struct dirent *entry, off_t off) 246364031f1SWei Liu { 247364031f1SWei Liu int i = 0; 248364031f1SWei Liu V9fsSynthNode *node; 249364031f1SWei Liu 250364031f1SWei Liu rcu_read_lock(); 251364031f1SWei Liu QLIST_FOREACH(node, &dir->child, sibling) { 252364031f1SWei Liu /* This is the off child of the directory */ 253364031f1SWei Liu if (i == off) { 254364031f1SWei Liu break; 255364031f1SWei Liu } 256364031f1SWei Liu i++; 257364031f1SWei Liu } 258364031f1SWei Liu rcu_read_unlock(); 259364031f1SWei Liu if (!node) { 260364031f1SWei Liu /* end of directory */ 261635324e8SGreg Kurz return NULL; 262364031f1SWei Liu } 263b05528b5SGreg Kurz synth_direntry(node, entry, off); 264635324e8SGreg Kurz return entry; 265364031f1SWei Liu } 266364031f1SWei Liu 267b05528b5SGreg Kurz static struct dirent *synth_readdir(FsContext *ctx, V9fsFidOpenState *fs) 268364031f1SWei Liu { 269635324e8SGreg Kurz struct dirent *entry; 270364031f1SWei Liu V9fsSynthOpenState *synth_open = fs->private; 271364031f1SWei Liu V9fsSynthNode *node = synth_open->node; 272b05528b5SGreg Kurz entry = synth_get_dentry(node, &synth_open->dent, synth_open->offset); 273635324e8SGreg Kurz if (entry) { 274364031f1SWei Liu synth_open->offset++; 275364031f1SWei Liu } 276635324e8SGreg Kurz return entry; 277364031f1SWei Liu } 278364031f1SWei Liu 279b05528b5SGreg Kurz static int synth_open(FsContext *ctx, V9fsPath *fs_path, 280364031f1SWei Liu int flags, V9fsFidOpenState *fs) 281364031f1SWei Liu { 282364031f1SWei Liu V9fsSynthOpenState *synth_open; 283364031f1SWei Liu V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data; 284364031f1SWei Liu 285e64e27d5SVitaly Chikunov synth_open = g_new0(V9fsSynthOpenState, 1); 286364031f1SWei Liu synth_open->node = node; 287364031f1SWei Liu node->open_count++; 288364031f1SWei Liu fs->private = synth_open; 289364031f1SWei Liu return 0; 290364031f1SWei Liu } 291364031f1SWei Liu 292b05528b5SGreg Kurz static int synth_open2(FsContext *fs_ctx, V9fsPath *dir_path, 293364031f1SWei Liu const char *name, int flags, 294364031f1SWei Liu FsCred *credp, V9fsFidOpenState *fs) 295364031f1SWei Liu { 296364031f1SWei Liu errno = ENOSYS; 297364031f1SWei Liu return -1; 298364031f1SWei Liu } 299364031f1SWei Liu 300b05528b5SGreg Kurz static int synth_close(FsContext *ctx, V9fsFidOpenState *fs) 301364031f1SWei Liu { 302364031f1SWei Liu V9fsSynthOpenState *synth_open = fs->private; 303364031f1SWei Liu V9fsSynthNode *node = synth_open->node; 304364031f1SWei Liu 305364031f1SWei Liu node->open_count--; 306364031f1SWei Liu g_free(synth_open); 307364031f1SWei Liu fs->private = NULL; 308364031f1SWei Liu return 0; 309364031f1SWei Liu } 310364031f1SWei Liu 311b05528b5SGreg Kurz static ssize_t synth_pwritev(FsContext *ctx, V9fsFidOpenState *fs, 312364031f1SWei Liu const struct iovec *iov, 313364031f1SWei Liu int iovcnt, off_t offset) 314364031f1SWei Liu { 315364031f1SWei Liu int i, count = 0, wcount; 316364031f1SWei Liu V9fsSynthOpenState *synth_open = fs->private; 317364031f1SWei Liu V9fsSynthNode *node = synth_open->node; 318364031f1SWei Liu if (!node->attr->write) { 319364031f1SWei Liu errno = EPERM; 320364031f1SWei Liu return -1; 321364031f1SWei Liu } 322364031f1SWei Liu for (i = 0; i < iovcnt; i++) { 323364031f1SWei Liu wcount = node->attr->write(iov[i].iov_base, iov[i].iov_len, 324364031f1SWei Liu offset, node->private); 325364031f1SWei Liu offset += wcount; 326364031f1SWei Liu count += wcount; 327364031f1SWei Liu /* If we wrote less than requested. we are done */ 328364031f1SWei Liu if (wcount < iov[i].iov_len) { 329364031f1SWei Liu break; 330364031f1SWei Liu } 331364031f1SWei Liu } 332364031f1SWei Liu return count; 333364031f1SWei Liu } 334364031f1SWei Liu 335b05528b5SGreg Kurz static ssize_t synth_preadv(FsContext *ctx, V9fsFidOpenState *fs, 336364031f1SWei Liu const struct iovec *iov, 337364031f1SWei Liu int iovcnt, off_t offset) 338364031f1SWei Liu { 339364031f1SWei Liu int i, count = 0, rcount; 340364031f1SWei Liu V9fsSynthOpenState *synth_open = fs->private; 341364031f1SWei Liu V9fsSynthNode *node = synth_open->node; 342364031f1SWei Liu if (!node->attr->read) { 343364031f1SWei Liu errno = EPERM; 344364031f1SWei Liu return -1; 345364031f1SWei Liu } 346364031f1SWei Liu for (i = 0; i < iovcnt; i++) { 347364031f1SWei Liu rcount = node->attr->read(iov[i].iov_base, iov[i].iov_len, 348364031f1SWei Liu offset, node->private); 349364031f1SWei Liu offset += rcount; 350364031f1SWei Liu count += rcount; 351364031f1SWei Liu /* If we read less than requested. we are done */ 352364031f1SWei Liu if (rcount < iov[i].iov_len) { 353364031f1SWei Liu break; 354364031f1SWei Liu } 355364031f1SWei Liu } 356364031f1SWei Liu return count; 357364031f1SWei Liu } 358364031f1SWei Liu 359b05528b5SGreg Kurz static int synth_truncate(FsContext *ctx, V9fsPath *path, off_t offset) 360364031f1SWei Liu { 361364031f1SWei Liu errno = ENOSYS; 362364031f1SWei Liu return -1; 363364031f1SWei Liu } 364364031f1SWei Liu 365b05528b5SGreg Kurz static int synth_chmod(FsContext *fs_ctx, V9fsPath *path, FsCred *credp) 366364031f1SWei Liu { 367364031f1SWei Liu errno = EPERM; 368364031f1SWei Liu return -1; 369364031f1SWei Liu } 370364031f1SWei Liu 371b05528b5SGreg Kurz static int synth_mknod(FsContext *fs_ctx, V9fsPath *path, 372364031f1SWei Liu const char *buf, FsCred *credp) 373364031f1SWei Liu { 374364031f1SWei Liu errno = EPERM; 375364031f1SWei Liu return -1; 376364031f1SWei Liu } 377364031f1SWei Liu 378b05528b5SGreg Kurz static int synth_mkdir(FsContext *fs_ctx, V9fsPath *path, 379364031f1SWei Liu const char *buf, FsCred *credp) 380364031f1SWei Liu { 381364031f1SWei Liu errno = EPERM; 382364031f1SWei Liu return -1; 383364031f1SWei Liu } 384364031f1SWei Liu 385b05528b5SGreg Kurz static ssize_t synth_readlink(FsContext *fs_ctx, V9fsPath *path, 386364031f1SWei Liu char *buf, size_t bufsz) 387364031f1SWei Liu { 388364031f1SWei Liu errno = ENOSYS; 389364031f1SWei Liu return -1; 390364031f1SWei Liu } 391364031f1SWei Liu 392b05528b5SGreg Kurz static int synth_symlink(FsContext *fs_ctx, const char *oldpath, 393364031f1SWei Liu V9fsPath *newpath, const char *buf, FsCred *credp) 394364031f1SWei Liu { 395364031f1SWei Liu errno = EPERM; 396364031f1SWei Liu return -1; 397364031f1SWei Liu } 398364031f1SWei Liu 399b05528b5SGreg Kurz static int synth_link(FsContext *fs_ctx, V9fsPath *oldpath, 400364031f1SWei Liu V9fsPath *newpath, const char *buf) 401364031f1SWei Liu { 402364031f1SWei Liu errno = EPERM; 403364031f1SWei Liu return -1; 404364031f1SWei Liu } 405364031f1SWei Liu 406b05528b5SGreg Kurz static int synth_rename(FsContext *ctx, const char *oldpath, 407364031f1SWei Liu const char *newpath) 408364031f1SWei Liu { 409364031f1SWei Liu errno = EPERM; 410364031f1SWei Liu return -1; 411364031f1SWei Liu } 412364031f1SWei Liu 413b05528b5SGreg Kurz static int synth_chown(FsContext *fs_ctx, V9fsPath *path, FsCred *credp) 414364031f1SWei Liu { 415364031f1SWei Liu errno = EPERM; 416364031f1SWei Liu return -1; 417364031f1SWei Liu } 418364031f1SWei Liu 419b05528b5SGreg Kurz static int synth_utimensat(FsContext *fs_ctx, V9fsPath *path, 420364031f1SWei Liu const struct timespec *buf) 421364031f1SWei Liu { 422364031f1SWei Liu errno = EPERM; 423364031f1SWei Liu return 0; 424364031f1SWei Liu } 425364031f1SWei Liu 426b05528b5SGreg Kurz static int synth_remove(FsContext *ctx, const char *path) 427364031f1SWei Liu { 428364031f1SWei Liu errno = EPERM; 429364031f1SWei Liu return -1; 430364031f1SWei Liu } 431364031f1SWei Liu 432b05528b5SGreg Kurz static int synth_fsync(FsContext *ctx, int fid_type, 433364031f1SWei Liu V9fsFidOpenState *fs, int datasync) 434364031f1SWei Liu { 435364031f1SWei Liu errno = ENOSYS; 436364031f1SWei Liu return 0; 437364031f1SWei Liu } 438364031f1SWei Liu 439b05528b5SGreg Kurz static int synth_statfs(FsContext *s, V9fsPath *fs_path, 440364031f1SWei Liu struct statfs *stbuf) 441364031f1SWei Liu { 442364031f1SWei Liu stbuf->f_type = 0xABCD; 443364031f1SWei Liu stbuf->f_bsize = 512; 444364031f1SWei Liu stbuf->f_blocks = 0; 445b05528b5SGreg Kurz stbuf->f_files = synth_node_count; 446f41db099SKeno Fischer #ifndef CONFIG_DARWIN 447364031f1SWei Liu stbuf->f_namelen = NAME_MAX; 448f41db099SKeno Fischer #endif 449364031f1SWei Liu return 0; 450364031f1SWei Liu } 451364031f1SWei Liu 452b05528b5SGreg Kurz static ssize_t synth_lgetxattr(FsContext *ctx, V9fsPath *path, 453364031f1SWei Liu const char *name, void *value, size_t size) 454364031f1SWei Liu { 455364031f1SWei Liu errno = ENOTSUP; 456364031f1SWei Liu return -1; 457364031f1SWei Liu } 458364031f1SWei Liu 459b05528b5SGreg Kurz static ssize_t synth_llistxattr(FsContext *ctx, V9fsPath *path, 460364031f1SWei Liu void *value, size_t size) 461364031f1SWei Liu { 462364031f1SWei Liu errno = ENOTSUP; 463364031f1SWei Liu return -1; 464364031f1SWei Liu } 465364031f1SWei Liu 466b05528b5SGreg Kurz static int synth_lsetxattr(FsContext *ctx, V9fsPath *path, 467364031f1SWei Liu const char *name, void *value, 468364031f1SWei Liu size_t size, int flags) 469364031f1SWei Liu { 470364031f1SWei Liu errno = ENOTSUP; 471364031f1SWei Liu return -1; 472364031f1SWei Liu } 473364031f1SWei Liu 474b05528b5SGreg Kurz static int synth_lremovexattr(FsContext *ctx, 475364031f1SWei Liu V9fsPath *path, const char *name) 476364031f1SWei Liu { 477364031f1SWei Liu errno = ENOTSUP; 478364031f1SWei Liu return -1; 479364031f1SWei Liu } 480364031f1SWei Liu 481b05528b5SGreg Kurz static int synth_name_to_path(FsContext *ctx, V9fsPath *dir_path, 482364031f1SWei Liu const char *name, V9fsPath *target) 483364031f1SWei Liu { 484364031f1SWei Liu V9fsSynthNode *node; 485364031f1SWei Liu V9fsSynthNode *dir_node; 486364031f1SWei Liu 487364031f1SWei Liu /* "." and ".." are not allowed */ 488364031f1SWei Liu if (!strcmp(name, ".") || !strcmp(name, "..")) { 489364031f1SWei Liu errno = EINVAL; 490364031f1SWei Liu return -1; 491364031f1SWei Liu 492364031f1SWei Liu } 493364031f1SWei Liu if (!dir_path) { 494b05528b5SGreg Kurz dir_node = &synth_root; 495364031f1SWei Liu } else { 496364031f1SWei Liu dir_node = *(V9fsSynthNode **)dir_path->data; 497364031f1SWei Liu } 498364031f1SWei Liu if (!strcmp(name, "/")) { 499364031f1SWei Liu node = dir_node; 500364031f1SWei Liu goto out; 501364031f1SWei Liu } 502364031f1SWei Liu /* search for the name in the childern */ 503364031f1SWei Liu rcu_read_lock(); 504364031f1SWei Liu QLIST_FOREACH(node, &dir_node->child, sibling) { 505364031f1SWei Liu if (!strcmp(node->name, name)) { 506364031f1SWei Liu break; 507364031f1SWei Liu } 508364031f1SWei Liu } 509364031f1SWei Liu rcu_read_unlock(); 510364031f1SWei Liu 511364031f1SWei Liu if (!node) { 512364031f1SWei Liu errno = ENOENT; 513364031f1SWei Liu return -1; 514364031f1SWei Liu } 515364031f1SWei Liu out: 516364031f1SWei Liu /* Copy the node pointer to fid */ 5176ce7177aSMarc-André Lureau g_free(target->data); 518453a1b23SMarc-André Lureau target->data = g_memdup(&node, sizeof(void *)); 519364031f1SWei Liu target->size = sizeof(void *); 520364031f1SWei Liu return 0; 521364031f1SWei Liu } 522364031f1SWei Liu 523b05528b5SGreg Kurz static int synth_renameat(FsContext *ctx, V9fsPath *olddir, 524364031f1SWei Liu const char *old_name, V9fsPath *newdir, 525364031f1SWei Liu const char *new_name) 526364031f1SWei Liu { 527364031f1SWei Liu errno = EPERM; 528364031f1SWei Liu return -1; 529364031f1SWei Liu } 530364031f1SWei Liu 531b05528b5SGreg Kurz static int synth_unlinkat(FsContext *ctx, V9fsPath *dir, 532364031f1SWei Liu const char *name, int flags) 533364031f1SWei Liu { 534364031f1SWei Liu errno = EPERM; 535364031f1SWei Liu return -1; 536364031f1SWei Liu } 537364031f1SWei Liu 538354b86f8SGreg Kurz static ssize_t v9fs_synth_qtest_write(void *buf, int len, off_t offset, 539354b86f8SGreg Kurz void *arg) 540354b86f8SGreg Kurz { 541354b86f8SGreg Kurz return 1; 542354b86f8SGreg Kurz } 543354b86f8SGreg Kurz 544357e2f7fSGreg Kurz static ssize_t v9fs_synth_qtest_flush_write(void *buf, int len, off_t offset, 545357e2f7fSGreg Kurz void *arg) 546357e2f7fSGreg Kurz { 547357e2f7fSGreg Kurz bool should_block = !!*(uint8_t *)buf; 548357e2f7fSGreg Kurz 549357e2f7fSGreg Kurz if (should_block) { 550357e2f7fSGreg Kurz /* This will cause the server to call us again until we're cancelled */ 551357e2f7fSGreg Kurz errno = EINTR; 552357e2f7fSGreg Kurz return -1; 553357e2f7fSGreg Kurz } 554357e2f7fSGreg Kurz 555357e2f7fSGreg Kurz return 1; 556357e2f7fSGreg Kurz } 557357e2f7fSGreg Kurz 55865603a80SGreg Kurz static int synth_init(FsContext *ctx, Error **errp) 559364031f1SWei Liu { 560b05528b5SGreg Kurz QLIST_INIT(&synth_root.child); 561b05528b5SGreg Kurz qemu_mutex_init(&synth_mutex); 562364031f1SWei Liu 563364031f1SWei Liu /* Add "." and ".." entries for root */ 564b05528b5SGreg Kurz v9fs_add_dir_node(&synth_root, synth_root.attr->mode, 565b05528b5SGreg Kurz "..", synth_root.attr, synth_root.attr->inode); 566b05528b5SGreg Kurz v9fs_add_dir_node(&synth_root, synth_root.attr->mode, 567b05528b5SGreg Kurz ".", synth_root.attr, synth_root.attr->inode); 568364031f1SWei Liu 569364031f1SWei Liu /* Mark the subsystem is ready for use */ 570b05528b5SGreg Kurz synth_fs = 1; 5712893ddd5SGreg Kurz 5722893ddd5SGreg Kurz if (qtest_enabled()) { 5732893ddd5SGreg Kurz V9fsSynthNode *node = NULL; 5742893ddd5SGreg Kurz int i, ret; 5752893ddd5SGreg Kurz 5762893ddd5SGreg Kurz /* Directory hierarchy for WALK test */ 5772893ddd5SGreg Kurz for (i = 0; i < P9_MAXWELEM; i++) { 5782893ddd5SGreg Kurz char *name = g_strdup_printf(QTEST_V9FS_SYNTH_WALK_FILE, i); 5792893ddd5SGreg Kurz 5802893ddd5SGreg Kurz ret = qemu_v9fs_synth_mkdir(node, 0700, name, &node); 5812893ddd5SGreg Kurz assert(!ret); 5822893ddd5SGreg Kurz g_free(name); 5832893ddd5SGreg Kurz } 58482469aaeSGreg Kurz 58582469aaeSGreg Kurz /* File for LOPEN test */ 58682469aaeSGreg Kurz ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_LOPEN_FILE, 58782469aaeSGreg Kurz NULL, NULL, ctx); 58882469aaeSGreg Kurz assert(!ret); 589354b86f8SGreg Kurz 590354b86f8SGreg Kurz /* File for WRITE test */ 591354b86f8SGreg Kurz ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_WRITE_FILE, 592354b86f8SGreg Kurz NULL, v9fs_synth_qtest_write, ctx); 593354b86f8SGreg Kurz assert(!ret); 594357e2f7fSGreg Kurz 595357e2f7fSGreg Kurz /* File for FLUSH test */ 596357e2f7fSGreg Kurz ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_FLUSH_FILE, 597357e2f7fSGreg Kurz NULL, v9fs_synth_qtest_flush_write, 598357e2f7fSGreg Kurz ctx); 599357e2f7fSGreg Kurz assert(!ret); 600af46a3b2SChristian Schoenebeck 601af46a3b2SChristian Schoenebeck /* Directory for READDIR test */ 602af46a3b2SChristian Schoenebeck { 603af46a3b2SChristian Schoenebeck V9fsSynthNode *dir = NULL; 604af46a3b2SChristian Schoenebeck ret = qemu_v9fs_synth_mkdir( 605af46a3b2SChristian Schoenebeck NULL, 0700, QTEST_V9FS_SYNTH_READDIR_DIR, &dir 606af46a3b2SChristian Schoenebeck ); 607af46a3b2SChristian Schoenebeck assert(!ret); 608af46a3b2SChristian Schoenebeck for (i = 0; i < QTEST_V9FS_SYNTH_READDIR_NFILES; ++i) { 609af46a3b2SChristian Schoenebeck char *name = g_strdup_printf( 610af46a3b2SChristian Schoenebeck QTEST_V9FS_SYNTH_READDIR_FILE, i 611af46a3b2SChristian Schoenebeck ); 612af46a3b2SChristian Schoenebeck ret = qemu_v9fs_synth_add_file( 613af46a3b2SChristian Schoenebeck dir, 0, name, NULL, NULL, ctx 614af46a3b2SChristian Schoenebeck ); 615af46a3b2SChristian Schoenebeck assert(!ret); 616af46a3b2SChristian Schoenebeck g_free(name); 617af46a3b2SChristian Schoenebeck } 618af46a3b2SChristian Schoenebeck } 6192893ddd5SGreg Kurz } 6202893ddd5SGreg Kurz 621364031f1SWei Liu return 0; 622364031f1SWei Liu } 623364031f1SWei Liu 624364031f1SWei Liu FileOperations synth_ops = { 625b05528b5SGreg Kurz .init = synth_init, 626b05528b5SGreg Kurz .lstat = synth_lstat, 627b05528b5SGreg Kurz .readlink = synth_readlink, 628b05528b5SGreg Kurz .close = synth_close, 629b05528b5SGreg Kurz .closedir = synth_closedir, 630b05528b5SGreg Kurz .open = synth_open, 631b05528b5SGreg Kurz .opendir = synth_opendir, 632b05528b5SGreg Kurz .rewinddir = synth_rewinddir, 633b05528b5SGreg Kurz .telldir = synth_telldir, 634b05528b5SGreg Kurz .readdir = synth_readdir, 635b05528b5SGreg Kurz .seekdir = synth_seekdir, 636b05528b5SGreg Kurz .preadv = synth_preadv, 637b05528b5SGreg Kurz .pwritev = synth_pwritev, 638b05528b5SGreg Kurz .chmod = synth_chmod, 639b05528b5SGreg Kurz .mknod = synth_mknod, 640b05528b5SGreg Kurz .mkdir = synth_mkdir, 641b05528b5SGreg Kurz .fstat = synth_fstat, 642b05528b5SGreg Kurz .open2 = synth_open2, 643b05528b5SGreg Kurz .symlink = synth_symlink, 644b05528b5SGreg Kurz .link = synth_link, 645b05528b5SGreg Kurz .truncate = synth_truncate, 646b05528b5SGreg Kurz .rename = synth_rename, 647b05528b5SGreg Kurz .chown = synth_chown, 648b05528b5SGreg Kurz .utimensat = synth_utimensat, 649b05528b5SGreg Kurz .remove = synth_remove, 650b05528b5SGreg Kurz .fsync = synth_fsync, 651b05528b5SGreg Kurz .statfs = synth_statfs, 652b05528b5SGreg Kurz .lgetxattr = synth_lgetxattr, 653b05528b5SGreg Kurz .llistxattr = synth_llistxattr, 654b05528b5SGreg Kurz .lsetxattr = synth_lsetxattr, 655b05528b5SGreg Kurz .lremovexattr = synth_lremovexattr, 656b05528b5SGreg Kurz .name_to_path = synth_name_to_path, 657b05528b5SGreg Kurz .renameat = synth_renameat, 658b05528b5SGreg Kurz .unlinkat = synth_unlinkat, 659364031f1SWei Liu }; 660