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 V9fsSynthNode *node, *tmp; 76364031f1SWei Liu 77b05528b5SGreg Kurz if (!synth_fs) { 78ab04d2c5SGreg Kurz return -EAGAIN; 79364031f1SWei Liu } 80364031f1SWei Liu if (!name || (strlen(name) >= NAME_MAX)) { 81ab04d2c5SGreg Kurz return -EINVAL; 82364031f1SWei Liu } 83364031f1SWei Liu if (!parent) { 84b05528b5SGreg Kurz parent = &synth_root; 85364031f1SWei Liu } 86e4fd889fSMahmoud Mandour QEMU_LOCK_GUARD(&synth_mutex); 87364031f1SWei Liu QLIST_FOREACH(tmp, &parent->child, sibling) { 88364031f1SWei Liu if (!strcmp(tmp->name, name)) { 89ab04d2c5SGreg Kurz return -EEXIST; 90364031f1SWei Liu } 91364031f1SWei Liu } 92364031f1SWei Liu /* Add the name */ 930009df31SChristian Schoenebeck node = v9fs_add_dir_node(parent, mode, name, NULL, ++synth_node_count); 94364031f1SWei Liu v9fs_add_dir_node(node, parent->attr->mode, "..", 95364031f1SWei Liu parent->attr, parent->attr->inode); 96364031f1SWei Liu v9fs_add_dir_node(node, node->attr->mode, ".", 97364031f1SWei Liu node->attr, node->attr->inode); 98364031f1SWei Liu *result = node; 9966997c42SMarkus Armbruster return 0; 100364031f1SWei Liu } 101364031f1SWei Liu 102364031f1SWei Liu int qemu_v9fs_synth_add_file(V9fsSynthNode *parent, int mode, 103364031f1SWei Liu const char *name, v9fs_synth_read read, 104364031f1SWei Liu v9fs_synth_write write, void *arg) 105364031f1SWei Liu { 106364031f1SWei Liu V9fsSynthNode *node, *tmp; 107364031f1SWei Liu 108b05528b5SGreg Kurz if (!synth_fs) { 109ab04d2c5SGreg Kurz return -EAGAIN; 110364031f1SWei Liu } 111364031f1SWei Liu if (!name || (strlen(name) >= NAME_MAX)) { 112ab04d2c5SGreg Kurz return -EINVAL; 113364031f1SWei Liu } 114364031f1SWei Liu if (!parent) { 115b05528b5SGreg Kurz parent = &synth_root; 116364031f1SWei Liu } 117364031f1SWei Liu 118e4fd889fSMahmoud Mandour QEMU_LOCK_GUARD(&synth_mutex); 119364031f1SWei Liu QLIST_FOREACH(tmp, &parent->child, sibling) { 120364031f1SWei Liu if (!strcmp(tmp->name, name)) { 121ab04d2c5SGreg Kurz return -EEXIST; 122364031f1SWei Liu } 123364031f1SWei Liu } 124364031f1SWei Liu /* Add file type and remove write bits */ 125364031f1SWei Liu mode = ((mode & 0777) | S_IFREG); 1261366244aSMarkus Armbruster node = g_new0(V9fsSynthNode, 1); 127364031f1SWei Liu node->attr = &node->actual_attr; 1280009df31SChristian Schoenebeck node->attr->inode = ++synth_node_count; 129364031f1SWei Liu node->attr->nlink = 1; 130364031f1SWei Liu node->attr->read = read; 131364031f1SWei Liu node->attr->write = write; 132364031f1SWei Liu node->attr->mode = mode; 133364031f1SWei Liu node->private = arg; 134364031f1SWei Liu pstrcpy(node->name, sizeof(node->name), name); 135364031f1SWei Liu QLIST_INSERT_HEAD_RCU(&parent->child, node, sibling); 13666997c42SMarkus Armbruster return 0; 137364031f1SWei Liu } 138364031f1SWei Liu 139b05528b5SGreg Kurz static void synth_fill_statbuf(V9fsSynthNode *node, struct stat *stbuf) 140364031f1SWei Liu { 141364031f1SWei Liu stbuf->st_dev = 0; 142364031f1SWei Liu stbuf->st_ino = node->attr->inode; 143364031f1SWei Liu stbuf->st_mode = node->attr->mode; 144364031f1SWei Liu stbuf->st_nlink = node->attr->nlink; 145364031f1SWei Liu stbuf->st_uid = 0; 146364031f1SWei Liu stbuf->st_gid = 0; 147364031f1SWei Liu stbuf->st_rdev = 0; 148364031f1SWei Liu stbuf->st_size = 0; 149364031f1SWei Liu stbuf->st_blksize = 0; 150364031f1SWei Liu stbuf->st_blocks = 0; 151364031f1SWei Liu stbuf->st_atime = 0; 152364031f1SWei Liu stbuf->st_mtime = 0; 153364031f1SWei Liu stbuf->st_ctime = 0; 154364031f1SWei Liu } 155364031f1SWei Liu 156b05528b5SGreg Kurz static int synth_lstat(FsContext *fs_ctx, 157364031f1SWei Liu V9fsPath *fs_path, struct stat *stbuf) 158364031f1SWei Liu { 159364031f1SWei Liu V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data; 160364031f1SWei Liu 161b05528b5SGreg Kurz synth_fill_statbuf(node, stbuf); 162364031f1SWei Liu return 0; 163364031f1SWei Liu } 164364031f1SWei Liu 165b05528b5SGreg Kurz static int synth_fstat(FsContext *fs_ctx, int fid_type, 166364031f1SWei Liu V9fsFidOpenState *fs, struct stat *stbuf) 167364031f1SWei Liu { 168364031f1SWei Liu V9fsSynthOpenState *synth_open = fs->private; 169b05528b5SGreg Kurz synth_fill_statbuf(synth_open->node, stbuf); 170364031f1SWei Liu return 0; 171364031f1SWei Liu } 172364031f1SWei Liu 173b05528b5SGreg Kurz static int synth_opendir(FsContext *ctx, 174364031f1SWei Liu V9fsPath *fs_path, V9fsFidOpenState *fs) 175364031f1SWei Liu { 176364031f1SWei Liu V9fsSynthOpenState *synth_open; 177364031f1SWei Liu V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data; 178364031f1SWei Liu 179e64e27d5SVitaly Chikunov /* 180e64e27d5SVitaly Chikunov * V9fsSynthOpenState contains 'struct dirent' which have OS-specific 181e64e27d5SVitaly Chikunov * properties, thus it's zero cleared on allocation here and below 182e64e27d5SVitaly Chikunov * in synth_open. 183e64e27d5SVitaly Chikunov */ 184e64e27d5SVitaly Chikunov synth_open = g_new0(V9fsSynthOpenState, 1); 185364031f1SWei Liu synth_open->node = node; 186364031f1SWei Liu node->open_count++; 187364031f1SWei Liu fs->private = synth_open; 188364031f1SWei Liu return 0; 189364031f1SWei Liu } 190364031f1SWei Liu 191b05528b5SGreg Kurz static int synth_closedir(FsContext *ctx, V9fsFidOpenState *fs) 192364031f1SWei Liu { 193364031f1SWei Liu V9fsSynthOpenState *synth_open = fs->private; 194364031f1SWei Liu V9fsSynthNode *node = synth_open->node; 195364031f1SWei Liu 196364031f1SWei Liu node->open_count--; 197364031f1SWei Liu g_free(synth_open); 198364031f1SWei Liu fs->private = NULL; 199364031f1SWei Liu return 0; 200364031f1SWei Liu } 201364031f1SWei Liu 202b05528b5SGreg Kurz static off_t synth_telldir(FsContext *ctx, V9fsFidOpenState *fs) 203364031f1SWei Liu { 204364031f1SWei Liu V9fsSynthOpenState *synth_open = fs->private; 205364031f1SWei Liu return synth_open->offset; 206364031f1SWei Liu } 207364031f1SWei Liu 208b05528b5SGreg Kurz static void synth_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off) 209364031f1SWei Liu { 210364031f1SWei Liu V9fsSynthOpenState *synth_open = fs->private; 211364031f1SWei Liu synth_open->offset = off; 212364031f1SWei Liu } 213364031f1SWei Liu 214b05528b5SGreg Kurz static void synth_rewinddir(FsContext *ctx, V9fsFidOpenState *fs) 215364031f1SWei Liu { 216b05528b5SGreg Kurz synth_seekdir(ctx, fs, 0); 217364031f1SWei Liu } 218364031f1SWei Liu 219b05528b5SGreg Kurz static void synth_direntry(V9fsSynthNode *node, 220364031f1SWei Liu struct dirent *entry, off_t off) 221364031f1SWei Liu { 222e64e27d5SVitaly Chikunov size_t sz = strlen(node->name) + 1; 223e64e27d5SVitaly Chikunov /* 224e64e27d5SVitaly Chikunov * 'entry' is always inside of V9fsSynthOpenState which have NAME_MAX 225e64e27d5SVitaly Chikunov * back padding. Ensure we do not overflow it. 226e64e27d5SVitaly Chikunov */ 227e64e27d5SVitaly Chikunov g_assert(sizeof(struct dirent) + NAME_MAX >= 228e64e27d5SVitaly Chikunov offsetof(struct dirent, d_name) + sz); 229e64e27d5SVitaly Chikunov memcpy(entry->d_name, node->name, sz); 230364031f1SWei Liu entry->d_ino = node->attr->inode; 2316b3b279bSKeno Fischer #ifdef CONFIG_DARWIN 2326b3b279bSKeno Fischer entry->d_seekoff = off + 1; 2336b3b279bSKeno Fischer #else 234364031f1SWei Liu entry->d_off = off + 1; 2356b3b279bSKeno Fischer #endif 236364031f1SWei Liu } 237364031f1SWei Liu 238b05528b5SGreg Kurz static struct dirent *synth_get_dentry(V9fsSynthNode *dir, 239635324e8SGreg Kurz struct dirent *entry, off_t off) 240364031f1SWei Liu { 241364031f1SWei Liu int i = 0; 242364031f1SWei Liu V9fsSynthNode *node; 243364031f1SWei Liu 244364031f1SWei Liu rcu_read_lock(); 245364031f1SWei Liu QLIST_FOREACH(node, &dir->child, sibling) { 246364031f1SWei Liu /* This is the off child of the directory */ 247364031f1SWei Liu if (i == off) { 248364031f1SWei Liu break; 249364031f1SWei Liu } 250364031f1SWei Liu i++; 251364031f1SWei Liu } 252364031f1SWei Liu rcu_read_unlock(); 253364031f1SWei Liu if (!node) { 254364031f1SWei Liu /* end of directory */ 255635324e8SGreg Kurz return NULL; 256364031f1SWei Liu } 257b05528b5SGreg Kurz synth_direntry(node, entry, off); 258635324e8SGreg Kurz return entry; 259364031f1SWei Liu } 260364031f1SWei Liu 261b05528b5SGreg Kurz static struct dirent *synth_readdir(FsContext *ctx, V9fsFidOpenState *fs) 262364031f1SWei Liu { 263635324e8SGreg Kurz struct dirent *entry; 264364031f1SWei Liu V9fsSynthOpenState *synth_open = fs->private; 265364031f1SWei Liu V9fsSynthNode *node = synth_open->node; 266b05528b5SGreg Kurz entry = synth_get_dentry(node, &synth_open->dent, synth_open->offset); 267635324e8SGreg Kurz if (entry) { 268364031f1SWei Liu synth_open->offset++; 269364031f1SWei Liu } 270635324e8SGreg Kurz return entry; 271364031f1SWei Liu } 272364031f1SWei Liu 273b05528b5SGreg Kurz static int synth_open(FsContext *ctx, V9fsPath *fs_path, 274364031f1SWei Liu int flags, V9fsFidOpenState *fs) 275364031f1SWei Liu { 276364031f1SWei Liu V9fsSynthOpenState *synth_open; 277364031f1SWei Liu V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data; 278364031f1SWei Liu 279e64e27d5SVitaly Chikunov synth_open = g_new0(V9fsSynthOpenState, 1); 280364031f1SWei Liu synth_open->node = node; 281364031f1SWei Liu node->open_count++; 282364031f1SWei Liu fs->private = synth_open; 283364031f1SWei Liu return 0; 284364031f1SWei Liu } 285364031f1SWei Liu 286b05528b5SGreg Kurz static int synth_open2(FsContext *fs_ctx, V9fsPath *dir_path, 287364031f1SWei Liu const char *name, int flags, 288364031f1SWei Liu FsCred *credp, V9fsFidOpenState *fs) 289364031f1SWei Liu { 290364031f1SWei Liu errno = ENOSYS; 291364031f1SWei Liu return -1; 292364031f1SWei Liu } 293364031f1SWei Liu 294b05528b5SGreg Kurz static int synth_close(FsContext *ctx, V9fsFidOpenState *fs) 295364031f1SWei Liu { 296364031f1SWei Liu V9fsSynthOpenState *synth_open = fs->private; 297364031f1SWei Liu V9fsSynthNode *node = synth_open->node; 298364031f1SWei Liu 299364031f1SWei Liu node->open_count--; 300364031f1SWei Liu g_free(synth_open); 301364031f1SWei Liu fs->private = NULL; 302364031f1SWei Liu return 0; 303364031f1SWei Liu } 304364031f1SWei Liu 305b05528b5SGreg Kurz static ssize_t synth_pwritev(FsContext *ctx, V9fsFidOpenState *fs, 306364031f1SWei Liu const struct iovec *iov, 307364031f1SWei Liu int iovcnt, off_t offset) 308364031f1SWei Liu { 309364031f1SWei Liu int i, count = 0, wcount; 310364031f1SWei Liu V9fsSynthOpenState *synth_open = fs->private; 311364031f1SWei Liu V9fsSynthNode *node = synth_open->node; 312364031f1SWei Liu if (!node->attr->write) { 313364031f1SWei Liu errno = EPERM; 314364031f1SWei Liu return -1; 315364031f1SWei Liu } 316364031f1SWei Liu for (i = 0; i < iovcnt; i++) { 317364031f1SWei Liu wcount = node->attr->write(iov[i].iov_base, iov[i].iov_len, 318364031f1SWei Liu offset, node->private); 319364031f1SWei Liu offset += wcount; 320364031f1SWei Liu count += wcount; 321364031f1SWei Liu /* If we wrote less than requested. we are done */ 322364031f1SWei Liu if (wcount < iov[i].iov_len) { 323364031f1SWei Liu break; 324364031f1SWei Liu } 325364031f1SWei Liu } 326364031f1SWei Liu return count; 327364031f1SWei Liu } 328364031f1SWei Liu 329b05528b5SGreg Kurz static ssize_t synth_preadv(FsContext *ctx, V9fsFidOpenState *fs, 330364031f1SWei Liu const struct iovec *iov, 331364031f1SWei Liu int iovcnt, off_t offset) 332364031f1SWei Liu { 333364031f1SWei Liu int i, count = 0, rcount; 334364031f1SWei Liu V9fsSynthOpenState *synth_open = fs->private; 335364031f1SWei Liu V9fsSynthNode *node = synth_open->node; 336364031f1SWei Liu if (!node->attr->read) { 337364031f1SWei Liu errno = EPERM; 338364031f1SWei Liu return -1; 339364031f1SWei Liu } 340364031f1SWei Liu for (i = 0; i < iovcnt; i++) { 341364031f1SWei Liu rcount = node->attr->read(iov[i].iov_base, iov[i].iov_len, 342364031f1SWei Liu offset, node->private); 343364031f1SWei Liu offset += rcount; 344364031f1SWei Liu count += rcount; 345364031f1SWei Liu /* If we read less than requested. we are done */ 346364031f1SWei Liu if (rcount < iov[i].iov_len) { 347364031f1SWei Liu break; 348364031f1SWei Liu } 349364031f1SWei Liu } 350364031f1SWei Liu return count; 351364031f1SWei Liu } 352364031f1SWei Liu 353b05528b5SGreg Kurz static int synth_truncate(FsContext *ctx, V9fsPath *path, off_t offset) 354364031f1SWei Liu { 355364031f1SWei Liu errno = ENOSYS; 356364031f1SWei Liu return -1; 357364031f1SWei Liu } 358364031f1SWei Liu 359b05528b5SGreg Kurz static int synth_chmod(FsContext *fs_ctx, V9fsPath *path, FsCred *credp) 360364031f1SWei Liu { 361364031f1SWei Liu errno = EPERM; 362364031f1SWei Liu return -1; 363364031f1SWei Liu } 364364031f1SWei Liu 365b05528b5SGreg Kurz static int synth_mknod(FsContext *fs_ctx, V9fsPath *path, 366364031f1SWei Liu const char *buf, FsCred *credp) 367364031f1SWei Liu { 368364031f1SWei Liu errno = EPERM; 369364031f1SWei Liu return -1; 370364031f1SWei Liu } 371364031f1SWei Liu 372b05528b5SGreg Kurz static int synth_mkdir(FsContext *fs_ctx, V9fsPath *path, 373364031f1SWei Liu const char *buf, FsCred *credp) 374364031f1SWei Liu { 375364031f1SWei Liu errno = EPERM; 376364031f1SWei Liu return -1; 377364031f1SWei Liu } 378364031f1SWei Liu 379b05528b5SGreg Kurz static ssize_t synth_readlink(FsContext *fs_ctx, V9fsPath *path, 380364031f1SWei Liu char *buf, size_t bufsz) 381364031f1SWei Liu { 382364031f1SWei Liu errno = ENOSYS; 383364031f1SWei Liu return -1; 384364031f1SWei Liu } 385364031f1SWei Liu 386b05528b5SGreg Kurz static int synth_symlink(FsContext *fs_ctx, const char *oldpath, 387364031f1SWei Liu V9fsPath *newpath, const char *buf, FsCred *credp) 388364031f1SWei Liu { 389364031f1SWei Liu errno = EPERM; 390364031f1SWei Liu return -1; 391364031f1SWei Liu } 392364031f1SWei Liu 393b05528b5SGreg Kurz static int synth_link(FsContext *fs_ctx, V9fsPath *oldpath, 394364031f1SWei Liu V9fsPath *newpath, const char *buf) 395364031f1SWei Liu { 396364031f1SWei Liu errno = EPERM; 397364031f1SWei Liu return -1; 398364031f1SWei Liu } 399364031f1SWei Liu 400b05528b5SGreg Kurz static int synth_rename(FsContext *ctx, const char *oldpath, 401364031f1SWei Liu const char *newpath) 402364031f1SWei Liu { 403364031f1SWei Liu errno = EPERM; 404364031f1SWei Liu return -1; 405364031f1SWei Liu } 406364031f1SWei Liu 407b05528b5SGreg Kurz static int synth_chown(FsContext *fs_ctx, V9fsPath *path, FsCred *credp) 408364031f1SWei Liu { 409364031f1SWei Liu errno = EPERM; 410364031f1SWei Liu return -1; 411364031f1SWei Liu } 412364031f1SWei Liu 413b05528b5SGreg Kurz static int synth_utimensat(FsContext *fs_ctx, V9fsPath *path, 414364031f1SWei Liu const struct timespec *buf) 415364031f1SWei Liu { 416364031f1SWei Liu errno = EPERM; 417364031f1SWei Liu return 0; 418364031f1SWei Liu } 419364031f1SWei Liu 420b05528b5SGreg Kurz static int synth_remove(FsContext *ctx, const char *path) 421364031f1SWei Liu { 422364031f1SWei Liu errno = EPERM; 423364031f1SWei Liu return -1; 424364031f1SWei Liu } 425364031f1SWei Liu 426b05528b5SGreg Kurz static int synth_fsync(FsContext *ctx, int fid_type, 427364031f1SWei Liu V9fsFidOpenState *fs, int datasync) 428364031f1SWei Liu { 429364031f1SWei Liu errno = ENOSYS; 430364031f1SWei Liu return 0; 431364031f1SWei Liu } 432364031f1SWei Liu 433b05528b5SGreg Kurz static int synth_statfs(FsContext *s, V9fsPath *fs_path, 434364031f1SWei Liu struct statfs *stbuf) 435364031f1SWei Liu { 436364031f1SWei Liu stbuf->f_type = 0xABCD; 437364031f1SWei Liu stbuf->f_bsize = 512; 438364031f1SWei Liu stbuf->f_blocks = 0; 439b05528b5SGreg Kurz stbuf->f_files = synth_node_count; 440f41db099SKeno Fischer #ifndef CONFIG_DARWIN 441364031f1SWei Liu stbuf->f_namelen = NAME_MAX; 442f41db099SKeno Fischer #endif 443364031f1SWei Liu return 0; 444364031f1SWei Liu } 445364031f1SWei Liu 446b05528b5SGreg Kurz static ssize_t synth_lgetxattr(FsContext *ctx, V9fsPath *path, 447364031f1SWei Liu const char *name, void *value, size_t size) 448364031f1SWei Liu { 449364031f1SWei Liu errno = ENOTSUP; 450364031f1SWei Liu return -1; 451364031f1SWei Liu } 452364031f1SWei Liu 453b05528b5SGreg Kurz static ssize_t synth_llistxattr(FsContext *ctx, V9fsPath *path, 454364031f1SWei Liu void *value, size_t size) 455364031f1SWei Liu { 456364031f1SWei Liu errno = ENOTSUP; 457364031f1SWei Liu return -1; 458364031f1SWei Liu } 459364031f1SWei Liu 460b05528b5SGreg Kurz static int synth_lsetxattr(FsContext *ctx, V9fsPath *path, 461364031f1SWei Liu const char *name, void *value, 462364031f1SWei Liu size_t size, int flags) 463364031f1SWei Liu { 464364031f1SWei Liu errno = ENOTSUP; 465364031f1SWei Liu return -1; 466364031f1SWei Liu } 467364031f1SWei Liu 468b05528b5SGreg Kurz static int synth_lremovexattr(FsContext *ctx, 469364031f1SWei Liu V9fsPath *path, const char *name) 470364031f1SWei Liu { 471364031f1SWei Liu errno = ENOTSUP; 472364031f1SWei Liu return -1; 473364031f1SWei Liu } 474364031f1SWei Liu 475b05528b5SGreg Kurz static int synth_name_to_path(FsContext *ctx, V9fsPath *dir_path, 476364031f1SWei Liu const char *name, V9fsPath *target) 477364031f1SWei Liu { 478364031f1SWei Liu V9fsSynthNode *node; 479364031f1SWei Liu V9fsSynthNode *dir_node; 480364031f1SWei Liu 481364031f1SWei Liu /* "." and ".." are not allowed */ 482364031f1SWei Liu if (!strcmp(name, ".") || !strcmp(name, "..")) { 483364031f1SWei Liu errno = EINVAL; 484364031f1SWei Liu return -1; 485364031f1SWei Liu 486364031f1SWei Liu } 487364031f1SWei Liu if (!dir_path) { 488b05528b5SGreg Kurz dir_node = &synth_root; 489364031f1SWei Liu } else { 490364031f1SWei Liu dir_node = *(V9fsSynthNode **)dir_path->data; 491364031f1SWei Liu } 492364031f1SWei Liu if (!strcmp(name, "/")) { 493364031f1SWei Liu node = dir_node; 494364031f1SWei Liu goto out; 495364031f1SWei Liu } 496*28cbbdd2SMichael Tokarev /* search for the name in the children */ 497364031f1SWei Liu rcu_read_lock(); 498364031f1SWei Liu QLIST_FOREACH(node, &dir_node->child, sibling) { 499364031f1SWei Liu if (!strcmp(node->name, name)) { 500364031f1SWei Liu break; 501364031f1SWei Liu } 502364031f1SWei Liu } 503364031f1SWei Liu rcu_read_unlock(); 504364031f1SWei Liu 505364031f1SWei Liu if (!node) { 506364031f1SWei Liu errno = ENOENT; 507364031f1SWei Liu return -1; 508364031f1SWei Liu } 509364031f1SWei Liu out: 510364031f1SWei Liu /* Copy the node pointer to fid */ 5116ce7177aSMarc-André Lureau g_free(target->data); 512453a1b23SMarc-André Lureau target->data = g_memdup(&node, sizeof(void *)); 513364031f1SWei Liu target->size = sizeof(void *); 514364031f1SWei Liu return 0; 515364031f1SWei Liu } 516364031f1SWei Liu 517b05528b5SGreg Kurz static int synth_renameat(FsContext *ctx, V9fsPath *olddir, 518364031f1SWei Liu const char *old_name, V9fsPath *newdir, 519364031f1SWei Liu const char *new_name) 520364031f1SWei Liu { 521364031f1SWei Liu errno = EPERM; 522364031f1SWei Liu return -1; 523364031f1SWei Liu } 524364031f1SWei Liu 525b05528b5SGreg Kurz static int synth_unlinkat(FsContext *ctx, V9fsPath *dir, 526364031f1SWei Liu const char *name, int flags) 527364031f1SWei Liu { 528364031f1SWei Liu errno = EPERM; 529364031f1SWei Liu return -1; 530364031f1SWei Liu } 531364031f1SWei Liu 532354b86f8SGreg Kurz static ssize_t v9fs_synth_qtest_write(void *buf, int len, off_t offset, 533354b86f8SGreg Kurz void *arg) 534354b86f8SGreg Kurz { 535354b86f8SGreg Kurz return 1; 536354b86f8SGreg Kurz } 537354b86f8SGreg Kurz 538357e2f7fSGreg Kurz static ssize_t v9fs_synth_qtest_flush_write(void *buf, int len, off_t offset, 539357e2f7fSGreg Kurz void *arg) 540357e2f7fSGreg Kurz { 541357e2f7fSGreg Kurz bool should_block = !!*(uint8_t *)buf; 542357e2f7fSGreg Kurz 543357e2f7fSGreg Kurz if (should_block) { 544357e2f7fSGreg Kurz /* This will cause the server to call us again until we're cancelled */ 545357e2f7fSGreg Kurz errno = EINTR; 546357e2f7fSGreg Kurz return -1; 547357e2f7fSGreg Kurz } 548357e2f7fSGreg Kurz 549357e2f7fSGreg Kurz return 1; 550357e2f7fSGreg Kurz } 551357e2f7fSGreg Kurz 55265603a80SGreg Kurz static int synth_init(FsContext *ctx, Error **errp) 553364031f1SWei Liu { 554b05528b5SGreg Kurz QLIST_INIT(&synth_root.child); 555b05528b5SGreg Kurz qemu_mutex_init(&synth_mutex); 556364031f1SWei Liu 557364031f1SWei Liu /* Add "." and ".." entries for root */ 558b05528b5SGreg Kurz v9fs_add_dir_node(&synth_root, synth_root.attr->mode, 559b05528b5SGreg Kurz "..", synth_root.attr, synth_root.attr->inode); 560b05528b5SGreg Kurz v9fs_add_dir_node(&synth_root, synth_root.attr->mode, 561b05528b5SGreg Kurz ".", synth_root.attr, synth_root.attr->inode); 562364031f1SWei Liu 563364031f1SWei Liu /* Mark the subsystem is ready for use */ 564b05528b5SGreg Kurz synth_fs = 1; 5652893ddd5SGreg Kurz 5662893ddd5SGreg Kurz if (qtest_enabled()) { 5672893ddd5SGreg Kurz V9fsSynthNode *node = NULL; 5682893ddd5SGreg Kurz int i, ret; 5692893ddd5SGreg Kurz 5702893ddd5SGreg Kurz /* Directory hierarchy for WALK test */ 5712893ddd5SGreg Kurz for (i = 0; i < P9_MAXWELEM; i++) { 5722893ddd5SGreg Kurz char *name = g_strdup_printf(QTEST_V9FS_SYNTH_WALK_FILE, i); 5732893ddd5SGreg Kurz 5742893ddd5SGreg Kurz ret = qemu_v9fs_synth_mkdir(node, 0700, name, &node); 5752893ddd5SGreg Kurz assert(!ret); 5762893ddd5SGreg Kurz g_free(name); 5772893ddd5SGreg Kurz } 57882469aaeSGreg Kurz 57982469aaeSGreg Kurz /* File for LOPEN test */ 58082469aaeSGreg Kurz ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_LOPEN_FILE, 58182469aaeSGreg Kurz NULL, NULL, ctx); 58282469aaeSGreg Kurz assert(!ret); 583354b86f8SGreg Kurz 584354b86f8SGreg Kurz /* File for WRITE test */ 585354b86f8SGreg Kurz ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_WRITE_FILE, 586354b86f8SGreg Kurz NULL, v9fs_synth_qtest_write, ctx); 587354b86f8SGreg Kurz assert(!ret); 588357e2f7fSGreg Kurz 589357e2f7fSGreg Kurz /* File for FLUSH test */ 590357e2f7fSGreg Kurz ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_FLUSH_FILE, 591357e2f7fSGreg Kurz NULL, v9fs_synth_qtest_flush_write, 592357e2f7fSGreg Kurz ctx); 593357e2f7fSGreg Kurz assert(!ret); 594af46a3b2SChristian Schoenebeck 595af46a3b2SChristian Schoenebeck /* Directory for READDIR test */ 596af46a3b2SChristian Schoenebeck { 597af46a3b2SChristian Schoenebeck V9fsSynthNode *dir = NULL; 598af46a3b2SChristian Schoenebeck ret = qemu_v9fs_synth_mkdir( 599af46a3b2SChristian Schoenebeck NULL, 0700, QTEST_V9FS_SYNTH_READDIR_DIR, &dir 600af46a3b2SChristian Schoenebeck ); 601af46a3b2SChristian Schoenebeck assert(!ret); 602af46a3b2SChristian Schoenebeck for (i = 0; i < QTEST_V9FS_SYNTH_READDIR_NFILES; ++i) { 603af46a3b2SChristian Schoenebeck char *name = g_strdup_printf( 604af46a3b2SChristian Schoenebeck QTEST_V9FS_SYNTH_READDIR_FILE, i 605af46a3b2SChristian Schoenebeck ); 606af46a3b2SChristian Schoenebeck ret = qemu_v9fs_synth_add_file( 607af46a3b2SChristian Schoenebeck dir, 0, name, NULL, NULL, ctx 608af46a3b2SChristian Schoenebeck ); 609af46a3b2SChristian Schoenebeck assert(!ret); 610af46a3b2SChristian Schoenebeck g_free(name); 611af46a3b2SChristian Schoenebeck } 612af46a3b2SChristian Schoenebeck } 6132893ddd5SGreg Kurz } 6142893ddd5SGreg Kurz 615364031f1SWei Liu return 0; 616364031f1SWei Liu } 617364031f1SWei Liu 618364031f1SWei Liu FileOperations synth_ops = { 619b05528b5SGreg Kurz .init = synth_init, 620b05528b5SGreg Kurz .lstat = synth_lstat, 621b05528b5SGreg Kurz .readlink = synth_readlink, 622b05528b5SGreg Kurz .close = synth_close, 623b05528b5SGreg Kurz .closedir = synth_closedir, 624b05528b5SGreg Kurz .open = synth_open, 625b05528b5SGreg Kurz .opendir = synth_opendir, 626b05528b5SGreg Kurz .rewinddir = synth_rewinddir, 627b05528b5SGreg Kurz .telldir = synth_telldir, 628b05528b5SGreg Kurz .readdir = synth_readdir, 629b05528b5SGreg Kurz .seekdir = synth_seekdir, 630b05528b5SGreg Kurz .preadv = synth_preadv, 631b05528b5SGreg Kurz .pwritev = synth_pwritev, 632b05528b5SGreg Kurz .chmod = synth_chmod, 633b05528b5SGreg Kurz .mknod = synth_mknod, 634b05528b5SGreg Kurz .mkdir = synth_mkdir, 635b05528b5SGreg Kurz .fstat = synth_fstat, 636b05528b5SGreg Kurz .open2 = synth_open2, 637b05528b5SGreg Kurz .symlink = synth_symlink, 638b05528b5SGreg Kurz .link = synth_link, 639b05528b5SGreg Kurz .truncate = synth_truncate, 640b05528b5SGreg Kurz .rename = synth_rename, 641b05528b5SGreg Kurz .chown = synth_chown, 642b05528b5SGreg Kurz .utimensat = synth_utimensat, 643b05528b5SGreg Kurz .remove = synth_remove, 644b05528b5SGreg Kurz .fsync = synth_fsync, 645b05528b5SGreg Kurz .statfs = synth_statfs, 646b05528b5SGreg Kurz .lgetxattr = synth_lgetxattr, 647b05528b5SGreg Kurz .llistxattr = synth_llistxattr, 648b05528b5SGreg Kurz .lsetxattr = synth_lsetxattr, 649b05528b5SGreg Kurz .lremovexattr = synth_lremovexattr, 650b05528b5SGreg Kurz .name_to_path = synth_name_to_path, 651b05528b5SGreg Kurz .renameat = synth_renameat, 652b05528b5SGreg Kurz .unlinkat = synth_unlinkat, 653364031f1SWei Liu }; 654