2 * 9p synthetic file system support
4 * Copyright IBM, Corp. 2011
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"
17 #include "fsdev/qemu-fsdev.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
= {
28 .mode
= 0555 | S_IFDIR
,
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 */
39 static V9fsSynthNode
*v9fs_add_dir_node(V9fsSynthNode
*parent
, int mode
,
41 V9fsSynthNodeAttr
*attr
, int inode
)
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
));
49 /* We are adding .. or . entries */
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
;
62 pstrcpy(node
->name
, sizeof(node
->name
), name
);
63 QLIST_INSERT_HEAD_RCU(&parent
->child
, node
, sibling
);
67 int qemu_v9fs_synth_mkdir(V9fsSynthNode
*parent
, int mode
,
68 const char *name
, V9fsSynthNode
**result
)
71 V9fsSynthNode
*node
, *tmp
;
76 if (!name
|| (strlen(name
) >= NAME_MAX
)) {
82 qemu_mutex_lock(&synth_mutex
);
83 QLIST_FOREACH(tmp
, &parent
->child
, sibling
) {
84 if (!strcmp(tmp
->name
, 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
);
98 qemu_mutex_unlock(&synth_mutex
);
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
)
107 V9fsSynthNode
*node
, *tmp
;
112 if (!name
|| (strlen(name
) >= NAME_MAX
)) {
116 parent
= &synth_root
;
119 qemu_mutex_lock(&synth_mutex
);
120 QLIST_FOREACH(tmp
, &parent
->child
, sibling
) {
121 if (!strcmp(tmp
->name
, name
)) {
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
;
136 pstrcpy(node
->name
, sizeof(node
->name
), name
);
137 QLIST_INSERT_HEAD_RCU(&parent
->child
, node
, sibling
);
140 qemu_mutex_unlock(&synth_mutex
);
144 static void synth_fill_statbuf(V9fsSynthNode
*node
, struct stat
*stbuf
)
147 stbuf
->st_ino
= node
->attr
->inode
;
148 stbuf
->st_mode
= node
->attr
->mode
;
149 stbuf
->st_nlink
= node
->attr
->nlink
;
154 stbuf
->st_blksize
= 0;
155 stbuf
->st_blocks
= 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
);
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
);
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
;
187 fs
->private = synth_open
;
191 static int synth_closedir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
193 V9fsSynthOpenState
*synth_open
= fs
->private;
194 V9fsSynthNode
*node
= synth_open
->node
;
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
)
234 QLIST_FOREACH(node
, &dir
->child
, sibling
) {
235 /* This is the off child of the directory */
243 /* end of directory */
246 synth_direntry(node
, entry
, off
);
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
);
257 synth_open
->offset
++;
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
;
271 fs
->private = synth_open
;
275 static int synth_open2(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
276 const char *name
, int flags
,
277 FsCred
*credp
, V9fsFidOpenState
*fs
)
283 static int synth_close(FsContext
*ctx
, V9fsFidOpenState
*fs
)
285 V9fsSynthOpenState
*synth_open
= fs
->private;
286 V9fsSynthNode
*node
= synth_open
->node
;
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
) {
305 for (i
= 0; i
< iovcnt
; i
++) {
306 wcount
= node
->attr
->write(iov
[i
].iov_base
, iov
[i
].iov_len
,
307 offset
, node
->private);
310 /* If we wrote less than requested. we are done */
311 if (wcount
< iov
[i
].iov_len
) {
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
) {
329 for (i
= 0; i
< iovcnt
; i
++) {
330 rcount
= node
->attr
->read(iov
[i
].iov_base
, iov
[i
].iov_len
,
331 offset
, node
->private);
334 /* If we read less than requested. we are done */
335 if (rcount
< iov
[i
].iov_len
) {
342 static int synth_truncate(FsContext
*ctx
, V9fsPath
*path
, off_t offset
)
348 static int synth_chmod(FsContext
*fs_ctx
, V9fsPath
*path
, FsCred
*credp
)
354 static int synth_mknod(FsContext
*fs_ctx
, V9fsPath
*path
,
355 const char *buf
, FsCred
*credp
)
361 static int synth_mkdir(FsContext
*fs_ctx
, V9fsPath
*path
,
362 const char *buf
, FsCred
*credp
)
368 static ssize_t
synth_readlink(FsContext
*fs_ctx
, V9fsPath
*path
,
369 char *buf
, size_t bufsz
)
375 static int synth_symlink(FsContext
*fs_ctx
, const char *oldpath
,
376 V9fsPath
*newpath
, const char *buf
, FsCred
*credp
)
382 static int synth_link(FsContext
*fs_ctx
, V9fsPath
*oldpath
,
383 V9fsPath
*newpath
, const char *buf
)
389 static int synth_rename(FsContext
*ctx
, const char *oldpath
,
396 static int synth_chown(FsContext
*fs_ctx
, V9fsPath
*path
, FsCred
*credp
)
402 static int synth_utimensat(FsContext
*fs_ctx
, V9fsPath
*path
,
403 const struct timespec
*buf
)
409 static int synth_remove(FsContext
*ctx
, const char *path
)
415 static int synth_fsync(FsContext
*ctx
, int fid_type
,
416 V9fsFidOpenState
*fs
, int datasync
)
422 static int synth_statfs(FsContext
*s
, V9fsPath
*fs_path
,
423 struct statfs
*stbuf
)
425 stbuf
->f_type
= 0xABCD;
426 stbuf
->f_bsize
= 512;
428 stbuf
->f_files
= synth_node_count
;
429 stbuf
->f_namelen
= NAME_MAX
;
433 static ssize_t
synth_lgetxattr(FsContext
*ctx
, V9fsPath
*path
,
434 const char *name
, void *value
, size_t size
)
440 static ssize_t
synth_llistxattr(FsContext
*ctx
, V9fsPath
*path
,
441 void *value
, size_t size
)
447 static int synth_lsetxattr(FsContext
*ctx
, V9fsPath
*path
,
448 const char *name
, void *value
,
449 size_t size
, int flags
)
455 static int synth_lremovexattr(FsContext
*ctx
,
456 V9fsPath
*path
, const char *name
)
462 static int synth_name_to_path(FsContext
*ctx
, V9fsPath
*dir_path
,
463 const char *name
, V9fsPath
*target
)
466 V9fsSynthNode
*dir_node
;
468 /* "." and ".." are not allowed */
469 if (!strcmp(name
, ".") || !strcmp(name
, "..")) {
475 dir_node
= &synth_root
;
477 dir_node
= *(V9fsSynthNode
**)dir_path
->data
;
479 if (!strcmp(name
, "/")) {
483 /* search for the name in the childern */
485 QLIST_FOREACH(node
, &dir_node
->child
, sibling
) {
486 if (!strcmp(node
->name
, name
)) {
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 *);
504 static int synth_renameat(FsContext
*ctx
, V9fsPath
*olddir
,
505 const char *old_name
, V9fsPath
*newdir
,
506 const char *new_name
)
512 static int synth_unlinkat(FsContext
*ctx
, V9fsPath
*dir
,
513 const char *name
, int flags
)
519 static ssize_t
v9fs_synth_qtest_write(void *buf
, int len
, off_t offset
,
525 static ssize_t
v9fs_synth_qtest_flush_write(void *buf
, int len
, off_t offset
,
528 bool should_block
= !!*(uint8_t *)buf
;
531 /* This will cause the server to call us again until we're cancelled */
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 */
553 if (qtest_enabled()) {
554 V9fsSynthNode
*node
= NULL
;
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
);
566 /* File for LOPEN test */
567 ret
= qemu_v9fs_synth_add_file(NULL
, 0, QTEST_V9FS_SYNTH_LOPEN_FILE
,
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
);
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
,
586 FileOperations synth_ops
= {
588 .lstat
= synth_lstat
,
589 .readlink
= synth_readlink
,
590 .close
= synth_close
,
591 .closedir
= synth_closedir
,
593 .opendir
= synth_opendir
,
594 .rewinddir
= synth_rewinddir
,
595 .telldir
= synth_telldir
,
596 .readdir
= synth_readdir
,
597 .seekdir
= synth_seekdir
,
598 .preadv
= synth_preadv
,
599 .pwritev
= synth_pwritev
,
600 .chmod
= synth_chmod
,
601 .mknod
= synth_mknod
,
602 .mkdir
= synth_mkdir
,
603 .fstat
= synth_fstat
,
604 .open2
= synth_open2
,
605 .symlink
= synth_symlink
,
607 .truncate
= synth_truncate
,
608 .rename
= synth_rename
,
609 .chown
= synth_chown
,
610 .utimensat
= synth_utimensat
,
611 .remove
= synth_remove
,
612 .fsync
= synth_fsync
,
613 .statfs
= synth_statfs
,
614 .lgetxattr
= synth_lgetxattr
,
615 .llistxattr
= synth_llistxattr
,
616 .lsetxattr
= synth_lsetxattr
,
617 .lremovexattr
= synth_lremovexattr
,
618 .name_to_path
= synth_name_to_path
,
619 .renameat
= synth_renameat
,
620 .unlinkat
= synth_unlinkat
,