block/nvme: Make nvme_identify() return boolean indicating error
[qemu/ar7.git] / hw / 9pfs / 9p-synth.c
blob7eb210ffa89b273751bc12915eb41a73d4fdcda8
1 /*
2 * 9p synthetic file system support
4 * Copyright IBM, Corp. 2011
6 * Authors:
7 * Malahal Naineni <malahal@us.ibm.com>
8 * Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
10 * This work is licensed under the terms of the GNU GPL, version 2. See
11 * the COPYING file in the top-level directory.
15 #include "qemu/osdep.h"
16 #include "9p.h"
17 #include "fsdev/qemu-fsdev.h"
18 #include "9p-synth.h"
19 #include "qemu/rcu.h"
20 #include "qemu/rcu_queue.h"
21 #include "qemu/cutils.h"
22 #include "sysemu/qtest.h"
24 /* Root node for synth file system */
25 static V9fsSynthNode synth_root = {
26 .name = "/",
27 .actual_attr = {
28 .mode = 0555 | S_IFDIR,
29 .nlink = 1,
31 .attr = &synth_root.actual_attr,
34 static QemuMutex synth_mutex;
35 static int synth_node_count;
36 /* set to 1 when the synth fs is ready */
37 static int synth_fs;
39 static V9fsSynthNode *v9fs_add_dir_node(V9fsSynthNode *parent, int mode,
40 const char *name,
41 V9fsSynthNodeAttr *attr, int inode)
43 V9fsSynthNode *node;
45 /* Add directory type and remove write bits */
46 mode = ((mode & 0777) | S_IFDIR) & ~(S_IWUSR | S_IWGRP | S_IWOTH);
47 node = g_malloc0(sizeof(V9fsSynthNode));
48 if (attr) {
49 /* We are adding .. or . entries */
50 node->attr = attr;
51 node->attr->nlink++;
52 } else {
53 node->attr = &node->actual_attr;
54 node->attr->inode = inode;
55 node->attr->nlink = 1;
56 /* We don't allow write to directories */
57 node->attr->mode = mode;
58 node->attr->write = NULL;
59 node->attr->read = NULL;
61 node->private = node;
62 pstrcpy(node->name, sizeof(node->name), name);
63 QLIST_INSERT_HEAD_RCU(&parent->child, node, sibling);
64 return node;
67 int qemu_v9fs_synth_mkdir(V9fsSynthNode *parent, int mode,
68 const char *name, V9fsSynthNode **result)
70 int ret;
71 V9fsSynthNode *node, *tmp;
73 if (!synth_fs) {
74 return EAGAIN;
76 if (!name || (strlen(name) >= NAME_MAX)) {
77 return EINVAL;
79 if (!parent) {
80 parent = &synth_root;
82 qemu_mutex_lock(&synth_mutex);
83 QLIST_FOREACH(tmp, &parent->child, sibling) {
84 if (!strcmp(tmp->name, name)) {
85 ret = EEXIST;
86 goto err_out;
89 /* Add the name */
90 node = v9fs_add_dir_node(parent, mode, name, NULL, synth_node_count++);
91 v9fs_add_dir_node(node, parent->attr->mode, "..",
92 parent->attr, parent->attr->inode);
93 v9fs_add_dir_node(node, node->attr->mode, ".",
94 node->attr, node->attr->inode);
95 *result = node;
96 ret = 0;
97 err_out:
98 qemu_mutex_unlock(&synth_mutex);
99 return ret;
102 int qemu_v9fs_synth_add_file(V9fsSynthNode *parent, int mode,
103 const char *name, v9fs_synth_read read,
104 v9fs_synth_write write, void *arg)
106 int ret;
107 V9fsSynthNode *node, *tmp;
109 if (!synth_fs) {
110 return EAGAIN;
112 if (!name || (strlen(name) >= NAME_MAX)) {
113 return EINVAL;
115 if (!parent) {
116 parent = &synth_root;
119 qemu_mutex_lock(&synth_mutex);
120 QLIST_FOREACH(tmp, &parent->child, sibling) {
121 if (!strcmp(tmp->name, name)) {
122 ret = EEXIST;
123 goto err_out;
126 /* Add file type and remove write bits */
127 mode = ((mode & 0777) | S_IFREG);
128 node = g_malloc0(sizeof(V9fsSynthNode));
129 node->attr = &node->actual_attr;
130 node->attr->inode = synth_node_count++;
131 node->attr->nlink = 1;
132 node->attr->read = read;
133 node->attr->write = write;
134 node->attr->mode = mode;
135 node->private = arg;
136 pstrcpy(node->name, sizeof(node->name), name);
137 QLIST_INSERT_HEAD_RCU(&parent->child, node, sibling);
138 ret = 0;
139 err_out:
140 qemu_mutex_unlock(&synth_mutex);
141 return ret;
144 static void synth_fill_statbuf(V9fsSynthNode *node, struct stat *stbuf)
146 stbuf->st_dev = 0;
147 stbuf->st_ino = node->attr->inode;
148 stbuf->st_mode = node->attr->mode;
149 stbuf->st_nlink = node->attr->nlink;
150 stbuf->st_uid = 0;
151 stbuf->st_gid = 0;
152 stbuf->st_rdev = 0;
153 stbuf->st_size = 0;
154 stbuf->st_blksize = 0;
155 stbuf->st_blocks = 0;
156 stbuf->st_atime = 0;
157 stbuf->st_mtime = 0;
158 stbuf->st_ctime = 0;
161 static int synth_lstat(FsContext *fs_ctx,
162 V9fsPath *fs_path, struct stat *stbuf)
164 V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;
166 synth_fill_statbuf(node, stbuf);
167 return 0;
170 static int synth_fstat(FsContext *fs_ctx, int fid_type,
171 V9fsFidOpenState *fs, struct stat *stbuf)
173 V9fsSynthOpenState *synth_open = fs->private;
174 synth_fill_statbuf(synth_open->node, stbuf);
175 return 0;
178 static int synth_opendir(FsContext *ctx,
179 V9fsPath *fs_path, V9fsFidOpenState *fs)
181 V9fsSynthOpenState *synth_open;
182 V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;
184 synth_open = g_malloc(sizeof(*synth_open));
185 synth_open->node = node;
186 node->open_count++;
187 fs->private = synth_open;
188 return 0;
191 static int synth_closedir(FsContext *ctx, V9fsFidOpenState *fs)
193 V9fsSynthOpenState *synth_open = fs->private;
194 V9fsSynthNode *node = synth_open->node;
196 node->open_count--;
197 g_free(synth_open);
198 fs->private = NULL;
199 return 0;
202 static off_t synth_telldir(FsContext *ctx, V9fsFidOpenState *fs)
204 V9fsSynthOpenState *synth_open = fs->private;
205 return synth_open->offset;
208 static void synth_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
210 V9fsSynthOpenState *synth_open = fs->private;
211 synth_open->offset = off;
214 static void synth_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
216 synth_seekdir(ctx, fs, 0);
219 static void synth_direntry(V9fsSynthNode *node,
220 struct dirent *entry, off_t off)
222 strcpy(entry->d_name, node->name);
223 entry->d_ino = node->attr->inode;
224 entry->d_off = off + 1;
227 static struct dirent *synth_get_dentry(V9fsSynthNode *dir,
228 struct dirent *entry, off_t off)
230 int i = 0;
231 V9fsSynthNode *node;
233 rcu_read_lock();
234 QLIST_FOREACH(node, &dir->child, sibling) {
235 /* This is the off child of the directory */
236 if (i == off) {
237 break;
239 i++;
241 rcu_read_unlock();
242 if (!node) {
243 /* end of directory */
244 return NULL;
246 synth_direntry(node, entry, off);
247 return entry;
250 static struct dirent *synth_readdir(FsContext *ctx, V9fsFidOpenState *fs)
252 struct dirent *entry;
253 V9fsSynthOpenState *synth_open = fs->private;
254 V9fsSynthNode *node = synth_open->node;
255 entry = synth_get_dentry(node, &synth_open->dent, synth_open->offset);
256 if (entry) {
257 synth_open->offset++;
259 return entry;
262 static int synth_open(FsContext *ctx, V9fsPath *fs_path,
263 int flags, V9fsFidOpenState *fs)
265 V9fsSynthOpenState *synth_open;
266 V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;
268 synth_open = g_malloc(sizeof(*synth_open));
269 synth_open->node = node;
270 node->open_count++;
271 fs->private = synth_open;
272 return 0;
275 static int synth_open2(FsContext *fs_ctx, V9fsPath *dir_path,
276 const char *name, int flags,
277 FsCred *credp, V9fsFidOpenState *fs)
279 errno = ENOSYS;
280 return -1;
283 static int synth_close(FsContext *ctx, V9fsFidOpenState *fs)
285 V9fsSynthOpenState *synth_open = fs->private;
286 V9fsSynthNode *node = synth_open->node;
288 node->open_count--;
289 g_free(synth_open);
290 fs->private = NULL;
291 return 0;
294 static ssize_t synth_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
295 const struct iovec *iov,
296 int iovcnt, off_t offset)
298 int i, count = 0, wcount;
299 V9fsSynthOpenState *synth_open = fs->private;
300 V9fsSynthNode *node = synth_open->node;
301 if (!node->attr->write) {
302 errno = EPERM;
303 return -1;
305 for (i = 0; i < iovcnt; i++) {
306 wcount = node->attr->write(iov[i].iov_base, iov[i].iov_len,
307 offset, node->private);
308 offset += wcount;
309 count += wcount;
310 /* If we wrote less than requested. we are done */
311 if (wcount < iov[i].iov_len) {
312 break;
315 return count;
318 static ssize_t synth_preadv(FsContext *ctx, V9fsFidOpenState *fs,
319 const struct iovec *iov,
320 int iovcnt, off_t offset)
322 int i, count = 0, rcount;
323 V9fsSynthOpenState *synth_open = fs->private;
324 V9fsSynthNode *node = synth_open->node;
325 if (!node->attr->read) {
326 errno = EPERM;
327 return -1;
329 for (i = 0; i < iovcnt; i++) {
330 rcount = node->attr->read(iov[i].iov_base, iov[i].iov_len,
331 offset, node->private);
332 offset += rcount;
333 count += rcount;
334 /* If we read less than requested. we are done */
335 if (rcount < iov[i].iov_len) {
336 break;
339 return count;
342 static int synth_truncate(FsContext *ctx, V9fsPath *path, off_t offset)
344 errno = ENOSYS;
345 return -1;
348 static int synth_chmod(FsContext *fs_ctx, V9fsPath *path, FsCred *credp)
350 errno = EPERM;
351 return -1;
354 static int synth_mknod(FsContext *fs_ctx, V9fsPath *path,
355 const char *buf, FsCred *credp)
357 errno = EPERM;
358 return -1;
361 static int synth_mkdir(FsContext *fs_ctx, V9fsPath *path,
362 const char *buf, FsCred *credp)
364 errno = EPERM;
365 return -1;
368 static ssize_t synth_readlink(FsContext *fs_ctx, V9fsPath *path,
369 char *buf, size_t bufsz)
371 errno = ENOSYS;
372 return -1;
375 static int synth_symlink(FsContext *fs_ctx, const char *oldpath,
376 V9fsPath *newpath, const char *buf, FsCred *credp)
378 errno = EPERM;
379 return -1;
382 static int synth_link(FsContext *fs_ctx, V9fsPath *oldpath,
383 V9fsPath *newpath, const char *buf)
385 errno = EPERM;
386 return -1;
389 static int synth_rename(FsContext *ctx, const char *oldpath,
390 const char *newpath)
392 errno = EPERM;
393 return -1;
396 static int synth_chown(FsContext *fs_ctx, V9fsPath *path, FsCred *credp)
398 errno = EPERM;
399 return -1;
402 static int synth_utimensat(FsContext *fs_ctx, V9fsPath *path,
403 const struct timespec *buf)
405 errno = EPERM;
406 return 0;
409 static int synth_remove(FsContext *ctx, const char *path)
411 errno = EPERM;
412 return -1;
415 static int synth_fsync(FsContext *ctx, int fid_type,
416 V9fsFidOpenState *fs, int datasync)
418 errno = ENOSYS;
419 return 0;
422 static int synth_statfs(FsContext *s, V9fsPath *fs_path,
423 struct statfs *stbuf)
425 stbuf->f_type = 0xABCD;
426 stbuf->f_bsize = 512;
427 stbuf->f_blocks = 0;
428 stbuf->f_files = synth_node_count;
429 stbuf->f_namelen = NAME_MAX;
430 return 0;
433 static ssize_t synth_lgetxattr(FsContext *ctx, V9fsPath *path,
434 const char *name, void *value, size_t size)
436 errno = ENOTSUP;
437 return -1;
440 static ssize_t synth_llistxattr(FsContext *ctx, V9fsPath *path,
441 void *value, size_t size)
443 errno = ENOTSUP;
444 return -1;
447 static int synth_lsetxattr(FsContext *ctx, V9fsPath *path,
448 const char *name, void *value,
449 size_t size, int flags)
451 errno = ENOTSUP;
452 return -1;
455 static int synth_lremovexattr(FsContext *ctx,
456 V9fsPath *path, const char *name)
458 errno = ENOTSUP;
459 return -1;
462 static int synth_name_to_path(FsContext *ctx, V9fsPath *dir_path,
463 const char *name, V9fsPath *target)
465 V9fsSynthNode *node;
466 V9fsSynthNode *dir_node;
468 /* "." and ".." are not allowed */
469 if (!strcmp(name, ".") || !strcmp(name, "..")) {
470 errno = EINVAL;
471 return -1;
474 if (!dir_path) {
475 dir_node = &synth_root;
476 } else {
477 dir_node = *(V9fsSynthNode **)dir_path->data;
479 if (!strcmp(name, "/")) {
480 node = dir_node;
481 goto out;
483 /* search for the name in the childern */
484 rcu_read_lock();
485 QLIST_FOREACH(node, &dir_node->child, sibling) {
486 if (!strcmp(node->name, name)) {
487 break;
490 rcu_read_unlock();
492 if (!node) {
493 errno = ENOENT;
494 return -1;
496 out:
497 /* Copy the node pointer to fid */
498 g_free(target->data);
499 target->data = g_memdup(&node, sizeof(void *));
500 target->size = sizeof(void *);
501 return 0;
504 static int synth_renameat(FsContext *ctx, V9fsPath *olddir,
505 const char *old_name, V9fsPath *newdir,
506 const char *new_name)
508 errno = EPERM;
509 return -1;
512 static int synth_unlinkat(FsContext *ctx, V9fsPath *dir,
513 const char *name, int flags)
515 errno = EPERM;
516 return -1;
519 static ssize_t v9fs_synth_qtest_write(void *buf, int len, off_t offset,
520 void *arg)
522 return 1;
525 static ssize_t v9fs_synth_qtest_flush_write(void *buf, int len, off_t offset,
526 void *arg)
528 bool should_block = !!*(uint8_t *)buf;
530 if (should_block) {
531 /* This will cause the server to call us again until we're cancelled */
532 errno = EINTR;
533 return -1;
536 return 1;
539 static int synth_init(FsContext *ctx, Error **errp)
541 QLIST_INIT(&synth_root.child);
542 qemu_mutex_init(&synth_mutex);
544 /* Add "." and ".." entries for root */
545 v9fs_add_dir_node(&synth_root, synth_root.attr->mode,
546 "..", synth_root.attr, synth_root.attr->inode);
547 v9fs_add_dir_node(&synth_root, synth_root.attr->mode,
548 ".", synth_root.attr, synth_root.attr->inode);
550 /* Mark the subsystem is ready for use */
551 synth_fs = 1;
553 if (qtest_enabled()) {
554 V9fsSynthNode *node = NULL;
555 int i, ret;
557 /* Directory hierarchy for WALK test */
558 for (i = 0; i < P9_MAXWELEM; i++) {
559 char *name = g_strdup_printf(QTEST_V9FS_SYNTH_WALK_FILE, i);
561 ret = qemu_v9fs_synth_mkdir(node, 0700, name, &node);
562 assert(!ret);
563 g_free(name);
566 /* File for LOPEN test */
567 ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_LOPEN_FILE,
568 NULL, NULL, ctx);
569 assert(!ret);
571 /* File for WRITE test */
572 ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_WRITE_FILE,
573 NULL, v9fs_synth_qtest_write, ctx);
574 assert(!ret);
576 /* File for FLUSH test */
577 ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_FLUSH_FILE,
578 NULL, v9fs_synth_qtest_flush_write,
579 ctx);
580 assert(!ret);
582 /* Directory for READDIR test */
584 V9fsSynthNode *dir = NULL;
585 ret = qemu_v9fs_synth_mkdir(
586 NULL, 0700, QTEST_V9FS_SYNTH_READDIR_DIR, &dir
588 assert(!ret);
589 for (i = 0; i < QTEST_V9FS_SYNTH_READDIR_NFILES; ++i) {
590 char *name = g_strdup_printf(
591 QTEST_V9FS_SYNTH_READDIR_FILE, i
593 ret = qemu_v9fs_synth_add_file(
594 dir, 0, name, NULL, NULL, ctx
596 assert(!ret);
597 g_free(name);
602 return 0;
605 FileOperations synth_ops = {
606 .init = synth_init,
607 .lstat = synth_lstat,
608 .readlink = synth_readlink,
609 .close = synth_close,
610 .closedir = synth_closedir,
611 .open = synth_open,
612 .opendir = synth_opendir,
613 .rewinddir = synth_rewinddir,
614 .telldir = synth_telldir,
615 .readdir = synth_readdir,
616 .seekdir = synth_seekdir,
617 .preadv = synth_preadv,
618 .pwritev = synth_pwritev,
619 .chmod = synth_chmod,
620 .mknod = synth_mknod,
621 .mkdir = synth_mkdir,
622 .fstat = synth_fstat,
623 .open2 = synth_open2,
624 .symlink = synth_symlink,
625 .link = synth_link,
626 .truncate = synth_truncate,
627 .rename = synth_rename,
628 .chown = synth_chown,
629 .utimensat = synth_utimensat,
630 .remove = synth_remove,
631 .fsync = synth_fsync,
632 .statfs = synth_statfs,
633 .lgetxattr = synth_lgetxattr,
634 .llistxattr = synth_llistxattr,
635 .lsetxattr = synth_lsetxattr,
636 .lremovexattr = synth_lremovexattr,
637 .name_to_path = synth_name_to_path,
638 .renameat = synth_renameat,
639 .unlinkat = synth_unlinkat,