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_LOCK_GUARD(&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
);
100 int qemu_v9fs_synth_add_file(V9fsSynthNode
*parent
, int mode
,
101 const char *name
, v9fs_synth_read read
,
102 v9fs_synth_write write
, void *arg
)
105 V9fsSynthNode
*node
, *tmp
;
110 if (!name
|| (strlen(name
) >= NAME_MAX
)) {
114 parent
= &synth_root
;
117 QEMU_LOCK_GUARD(&synth_mutex
);
118 QLIST_FOREACH(tmp
, &parent
->child
, sibling
) {
119 if (!strcmp(tmp
->name
, name
)) {
124 /* Add file type and remove write bits */
125 mode
= ((mode
& 0777) | S_IFREG
);
126 node
= g_malloc0(sizeof(V9fsSynthNode
));
127 node
->attr
= &node
->actual_attr
;
128 node
->attr
->inode
= synth_node_count
++;
129 node
->attr
->nlink
= 1;
130 node
->attr
->read
= read
;
131 node
->attr
->write
= write
;
132 node
->attr
->mode
= mode
;
134 pstrcpy(node
->name
, sizeof(node
->name
), name
);
135 QLIST_INSERT_HEAD_RCU(&parent
->child
, node
, sibling
);
140 static void synth_fill_statbuf(V9fsSynthNode
*node
, struct stat
*stbuf
)
143 stbuf
->st_ino
= node
->attr
->inode
;
144 stbuf
->st_mode
= node
->attr
->mode
;
145 stbuf
->st_nlink
= node
->attr
->nlink
;
150 stbuf
->st_blksize
= 0;
151 stbuf
->st_blocks
= 0;
157 static int synth_lstat(FsContext
*fs_ctx
,
158 V9fsPath
*fs_path
, struct stat
*stbuf
)
160 V9fsSynthNode
*node
= *(V9fsSynthNode
**)fs_path
->data
;
162 synth_fill_statbuf(node
, stbuf
);
166 static int synth_fstat(FsContext
*fs_ctx
, int fid_type
,
167 V9fsFidOpenState
*fs
, struct stat
*stbuf
)
169 V9fsSynthOpenState
*synth_open
= fs
->private;
170 synth_fill_statbuf(synth_open
->node
, stbuf
);
174 static int synth_opendir(FsContext
*ctx
,
175 V9fsPath
*fs_path
, V9fsFidOpenState
*fs
)
177 V9fsSynthOpenState
*synth_open
;
178 V9fsSynthNode
*node
= *(V9fsSynthNode
**)fs_path
->data
;
180 synth_open
= g_malloc(sizeof(*synth_open
));
181 synth_open
->node
= node
;
183 fs
->private = synth_open
;
187 static int synth_closedir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
189 V9fsSynthOpenState
*synth_open
= fs
->private;
190 V9fsSynthNode
*node
= synth_open
->node
;
198 static off_t
synth_telldir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
200 V9fsSynthOpenState
*synth_open
= fs
->private;
201 return synth_open
->offset
;
204 static void synth_seekdir(FsContext
*ctx
, V9fsFidOpenState
*fs
, off_t off
)
206 V9fsSynthOpenState
*synth_open
= fs
->private;
207 synth_open
->offset
= off
;
210 static void synth_rewinddir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
212 synth_seekdir(ctx
, fs
, 0);
215 static void synth_direntry(V9fsSynthNode
*node
,
216 struct dirent
*entry
, off_t off
)
218 strcpy(entry
->d_name
, node
->name
);
219 entry
->d_ino
= node
->attr
->inode
;
220 entry
->d_off
= off
+ 1;
223 static struct dirent
*synth_get_dentry(V9fsSynthNode
*dir
,
224 struct dirent
*entry
, off_t off
)
230 QLIST_FOREACH(node
, &dir
->child
, sibling
) {
231 /* This is the off child of the directory */
239 /* end of directory */
242 synth_direntry(node
, entry
, off
);
246 static struct dirent
*synth_readdir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
248 struct dirent
*entry
;
249 V9fsSynthOpenState
*synth_open
= fs
->private;
250 V9fsSynthNode
*node
= synth_open
->node
;
251 entry
= synth_get_dentry(node
, &synth_open
->dent
, synth_open
->offset
);
253 synth_open
->offset
++;
258 static int synth_open(FsContext
*ctx
, V9fsPath
*fs_path
,
259 int flags
, V9fsFidOpenState
*fs
)
261 V9fsSynthOpenState
*synth_open
;
262 V9fsSynthNode
*node
= *(V9fsSynthNode
**)fs_path
->data
;
264 synth_open
= g_malloc(sizeof(*synth_open
));
265 synth_open
->node
= node
;
267 fs
->private = synth_open
;
271 static int synth_open2(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
272 const char *name
, int flags
,
273 FsCred
*credp
, V9fsFidOpenState
*fs
)
279 static int synth_close(FsContext
*ctx
, V9fsFidOpenState
*fs
)
281 V9fsSynthOpenState
*synth_open
= fs
->private;
282 V9fsSynthNode
*node
= synth_open
->node
;
290 static ssize_t
synth_pwritev(FsContext
*ctx
, V9fsFidOpenState
*fs
,
291 const struct iovec
*iov
,
292 int iovcnt
, off_t offset
)
294 int i
, count
= 0, wcount
;
295 V9fsSynthOpenState
*synth_open
= fs
->private;
296 V9fsSynthNode
*node
= synth_open
->node
;
297 if (!node
->attr
->write
) {
301 for (i
= 0; i
< iovcnt
; i
++) {
302 wcount
= node
->attr
->write(iov
[i
].iov_base
, iov
[i
].iov_len
,
303 offset
, node
->private);
306 /* If we wrote less than requested. we are done */
307 if (wcount
< iov
[i
].iov_len
) {
314 static ssize_t
synth_preadv(FsContext
*ctx
, V9fsFidOpenState
*fs
,
315 const struct iovec
*iov
,
316 int iovcnt
, off_t offset
)
318 int i
, count
= 0, rcount
;
319 V9fsSynthOpenState
*synth_open
= fs
->private;
320 V9fsSynthNode
*node
= synth_open
->node
;
321 if (!node
->attr
->read
) {
325 for (i
= 0; i
< iovcnt
; i
++) {
326 rcount
= node
->attr
->read(iov
[i
].iov_base
, iov
[i
].iov_len
,
327 offset
, node
->private);
330 /* If we read less than requested. we are done */
331 if (rcount
< iov
[i
].iov_len
) {
338 static int synth_truncate(FsContext
*ctx
, V9fsPath
*path
, off_t offset
)
344 static int synth_chmod(FsContext
*fs_ctx
, V9fsPath
*path
, FsCred
*credp
)
350 static int synth_mknod(FsContext
*fs_ctx
, V9fsPath
*path
,
351 const char *buf
, FsCred
*credp
)
357 static int synth_mkdir(FsContext
*fs_ctx
, V9fsPath
*path
,
358 const char *buf
, FsCred
*credp
)
364 static ssize_t
synth_readlink(FsContext
*fs_ctx
, V9fsPath
*path
,
365 char *buf
, size_t bufsz
)
371 static int synth_symlink(FsContext
*fs_ctx
, const char *oldpath
,
372 V9fsPath
*newpath
, const char *buf
, FsCred
*credp
)
378 static int synth_link(FsContext
*fs_ctx
, V9fsPath
*oldpath
,
379 V9fsPath
*newpath
, const char *buf
)
385 static int synth_rename(FsContext
*ctx
, const char *oldpath
,
392 static int synth_chown(FsContext
*fs_ctx
, V9fsPath
*path
, FsCred
*credp
)
398 static int synth_utimensat(FsContext
*fs_ctx
, V9fsPath
*path
,
399 const struct timespec
*buf
)
405 static int synth_remove(FsContext
*ctx
, const char *path
)
411 static int synth_fsync(FsContext
*ctx
, int fid_type
,
412 V9fsFidOpenState
*fs
, int datasync
)
418 static int synth_statfs(FsContext
*s
, V9fsPath
*fs_path
,
419 struct statfs
*stbuf
)
421 stbuf
->f_type
= 0xABCD;
422 stbuf
->f_bsize
= 512;
424 stbuf
->f_files
= synth_node_count
;
425 stbuf
->f_namelen
= NAME_MAX
;
429 static ssize_t
synth_lgetxattr(FsContext
*ctx
, V9fsPath
*path
,
430 const char *name
, void *value
, size_t size
)
436 static ssize_t
synth_llistxattr(FsContext
*ctx
, V9fsPath
*path
,
437 void *value
, size_t size
)
443 static int synth_lsetxattr(FsContext
*ctx
, V9fsPath
*path
,
444 const char *name
, void *value
,
445 size_t size
, int flags
)
451 static int synth_lremovexattr(FsContext
*ctx
,
452 V9fsPath
*path
, const char *name
)
458 static int synth_name_to_path(FsContext
*ctx
, V9fsPath
*dir_path
,
459 const char *name
, V9fsPath
*target
)
462 V9fsSynthNode
*dir_node
;
464 /* "." and ".." are not allowed */
465 if (!strcmp(name
, ".") || !strcmp(name
, "..")) {
471 dir_node
= &synth_root
;
473 dir_node
= *(V9fsSynthNode
**)dir_path
->data
;
475 if (!strcmp(name
, "/")) {
479 /* search for the name in the childern */
481 QLIST_FOREACH(node
, &dir_node
->child
, sibling
) {
482 if (!strcmp(node
->name
, name
)) {
493 /* Copy the node pointer to fid */
494 g_free(target
->data
);
495 target
->data
= g_memdup(&node
, sizeof(void *));
496 target
->size
= sizeof(void *);
500 static int synth_renameat(FsContext
*ctx
, V9fsPath
*olddir
,
501 const char *old_name
, V9fsPath
*newdir
,
502 const char *new_name
)
508 static int synth_unlinkat(FsContext
*ctx
, V9fsPath
*dir
,
509 const char *name
, int flags
)
515 static ssize_t
v9fs_synth_qtest_write(void *buf
, int len
, off_t offset
,
521 static ssize_t
v9fs_synth_qtest_flush_write(void *buf
, int len
, off_t offset
,
524 bool should_block
= !!*(uint8_t *)buf
;
527 /* This will cause the server to call us again until we're cancelled */
535 static int synth_init(FsContext
*ctx
, Error
**errp
)
537 QLIST_INIT(&synth_root
.child
);
538 qemu_mutex_init(&synth_mutex
);
540 /* Add "." and ".." entries for root */
541 v9fs_add_dir_node(&synth_root
, synth_root
.attr
->mode
,
542 "..", synth_root
.attr
, synth_root
.attr
->inode
);
543 v9fs_add_dir_node(&synth_root
, synth_root
.attr
->mode
,
544 ".", synth_root
.attr
, synth_root
.attr
->inode
);
546 /* Mark the subsystem is ready for use */
549 if (qtest_enabled()) {
550 V9fsSynthNode
*node
= NULL
;
553 /* Directory hierarchy for WALK test */
554 for (i
= 0; i
< P9_MAXWELEM
; i
++) {
555 char *name
= g_strdup_printf(QTEST_V9FS_SYNTH_WALK_FILE
, i
);
557 ret
= qemu_v9fs_synth_mkdir(node
, 0700, name
, &node
);
562 /* File for LOPEN test */
563 ret
= qemu_v9fs_synth_add_file(NULL
, 0, QTEST_V9FS_SYNTH_LOPEN_FILE
,
567 /* File for WRITE test */
568 ret
= qemu_v9fs_synth_add_file(NULL
, 0, QTEST_V9FS_SYNTH_WRITE_FILE
,
569 NULL
, v9fs_synth_qtest_write
, ctx
);
572 /* File for FLUSH test */
573 ret
= qemu_v9fs_synth_add_file(NULL
, 0, QTEST_V9FS_SYNTH_FLUSH_FILE
,
574 NULL
, v9fs_synth_qtest_flush_write
,
578 /* Directory for READDIR test */
580 V9fsSynthNode
*dir
= NULL
;
581 ret
= qemu_v9fs_synth_mkdir(
582 NULL
, 0700, QTEST_V9FS_SYNTH_READDIR_DIR
, &dir
585 for (i
= 0; i
< QTEST_V9FS_SYNTH_READDIR_NFILES
; ++i
) {
586 char *name
= g_strdup_printf(
587 QTEST_V9FS_SYNTH_READDIR_FILE
, i
589 ret
= qemu_v9fs_synth_add_file(
590 dir
, 0, name
, NULL
, NULL
, ctx
601 FileOperations synth_ops
= {
603 .lstat
= synth_lstat
,
604 .readlink
= synth_readlink
,
605 .close
= synth_close
,
606 .closedir
= synth_closedir
,
608 .opendir
= synth_opendir
,
609 .rewinddir
= synth_rewinddir
,
610 .telldir
= synth_telldir
,
611 .readdir
= synth_readdir
,
612 .seekdir
= synth_seekdir
,
613 .preadv
= synth_preadv
,
614 .pwritev
= synth_pwritev
,
615 .chmod
= synth_chmod
,
616 .mknod
= synth_mknod
,
617 .mkdir
= synth_mkdir
,
618 .fstat
= synth_fstat
,
619 .open2
= synth_open2
,
620 .symlink
= synth_symlink
,
622 .truncate
= synth_truncate
,
623 .rename
= synth_rename
,
624 .chown
= synth_chown
,
625 .utimensat
= synth_utimensat
,
626 .remove
= synth_remove
,
627 .fsync
= synth_fsync
,
628 .statfs
= synth_statfs
,
629 .lgetxattr
= synth_lgetxattr
,
630 .llistxattr
= synth_llistxattr
,
631 .lsetxattr
= synth_lsetxattr
,
632 .lremovexattr
= synth_lremovexattr
,
633 .name_to_path
= synth_name_to_path
,
634 .renameat
= synth_renameat
,
635 .unlinkat
= synth_unlinkat
,