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.
16 * Not so fast! You might want to read the 9p developer docs first:
17 * https://wiki.qemu.org/Documentation/9p
20 #include "qemu/osdep.h"
22 #include "fsdev/qemu-fsdev.h"
25 #include "qemu/rcu_queue.h"
26 #include "qemu/cutils.h"
27 #include "sysemu/qtest.h"
29 /* Root node for synth file system */
30 static V9fsSynthNode synth_root
= {
33 .mode
= 0555 | S_IFDIR
,
36 .attr
= &synth_root
.actual_attr
,
39 static QemuMutex synth_mutex
;
40 static int synth_node_count
;
41 /* set to 1 when the synth fs is ready */
44 static V9fsSynthNode
*v9fs_add_dir_node(V9fsSynthNode
*parent
, int mode
,
46 V9fsSynthNodeAttr
*attr
, int inode
)
50 /* Add directory type and remove write bits */
51 mode
= ((mode
& 0777) | S_IFDIR
) & ~(S_IWUSR
| S_IWGRP
| S_IWOTH
);
52 node
= g_new0(V9fsSynthNode
, 1);
54 /* We are adding .. or . entries */
58 node
->attr
= &node
->actual_attr
;
59 node
->attr
->inode
= inode
;
60 node
->attr
->nlink
= 1;
61 /* We don't allow write to directories */
62 node
->attr
->mode
= mode
;
63 node
->attr
->write
= NULL
;
64 node
->attr
->read
= NULL
;
67 pstrcpy(node
->name
, sizeof(node
->name
), name
);
68 QLIST_INSERT_HEAD_RCU(&parent
->child
, node
, sibling
);
72 int qemu_v9fs_synth_mkdir(V9fsSynthNode
*parent
, int mode
,
73 const char *name
, V9fsSynthNode
**result
)
75 V9fsSynthNode
*node
, *tmp
;
80 if (!name
|| (strlen(name
) >= NAME_MAX
)) {
86 QEMU_LOCK_GUARD(&synth_mutex
);
87 QLIST_FOREACH(tmp
, &parent
->child
, sibling
) {
88 if (!strcmp(tmp
->name
, name
)) {
93 node
= v9fs_add_dir_node(parent
, mode
, name
, NULL
, ++synth_node_count
);
94 v9fs_add_dir_node(node
, parent
->attr
->mode
, "..",
95 parent
->attr
, parent
->attr
->inode
);
96 v9fs_add_dir_node(node
, node
->attr
->mode
, ".",
97 node
->attr
, node
->attr
->inode
);
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 V9fsSynthNode
*node
, *tmp
;
111 if (!name
|| (strlen(name
) >= NAME_MAX
)) {
115 parent
= &synth_root
;
118 QEMU_LOCK_GUARD(&synth_mutex
);
119 QLIST_FOREACH(tmp
, &parent
->child
, sibling
) {
120 if (!strcmp(tmp
->name
, name
)) {
124 /* Add file type and remove write bits */
125 mode
= ((mode
& 0777) | S_IFREG
);
126 node
= g_new0(V9fsSynthNode
, 1);
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
);
139 static void synth_fill_statbuf(V9fsSynthNode
*node
, struct stat
*stbuf
)
142 stbuf
->st_ino
= node
->attr
->inode
;
143 stbuf
->st_mode
= node
->attr
->mode
;
144 stbuf
->st_nlink
= node
->attr
->nlink
;
149 stbuf
->st_blksize
= 0;
150 stbuf
->st_blocks
= 0;
156 static int synth_lstat(FsContext
*fs_ctx
,
157 V9fsPath
*fs_path
, struct stat
*stbuf
)
159 V9fsSynthNode
*node
= *(V9fsSynthNode
**)fs_path
->data
;
161 synth_fill_statbuf(node
, stbuf
);
165 static int synth_fstat(FsContext
*fs_ctx
, int fid_type
,
166 V9fsFidOpenState
*fs
, struct stat
*stbuf
)
168 V9fsSynthOpenState
*synth_open
= fs
->private;
169 synth_fill_statbuf(synth_open
->node
, stbuf
);
173 static int synth_opendir(FsContext
*ctx
,
174 V9fsPath
*fs_path
, V9fsFidOpenState
*fs
)
176 V9fsSynthOpenState
*synth_open
;
177 V9fsSynthNode
*node
= *(V9fsSynthNode
**)fs_path
->data
;
180 * V9fsSynthOpenState contains 'struct dirent' which have OS-specific
181 * properties, thus it's zero cleared on allocation here and below
184 synth_open
= g_new0(V9fsSynthOpenState
, 1);
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 size_t sz
= strlen(node
->name
) + 1;
224 * 'entry' is always inside of V9fsSynthOpenState which have NAME_MAX
225 * back padding. Ensure we do not overflow it.
227 g_assert(sizeof(struct dirent
) + NAME_MAX
>=
228 offsetof(struct dirent
, d_name
) + sz
);
229 memcpy(entry
->d_name
, node
->name
, sz
);
230 entry
->d_ino
= node
->attr
->inode
;
232 entry
->d_seekoff
= off
+ 1;
234 entry
->d_off
= off
+ 1;
238 static struct dirent
*synth_get_dentry(V9fsSynthNode
*dir
,
239 struct dirent
*entry
, off_t off
)
245 QLIST_FOREACH(node
, &dir
->child
, sibling
) {
246 /* This is the off child of the directory */
254 /* end of directory */
257 synth_direntry(node
, entry
, off
);
261 static struct dirent
*synth_readdir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
263 struct dirent
*entry
;
264 V9fsSynthOpenState
*synth_open
= fs
->private;
265 V9fsSynthNode
*node
= synth_open
->node
;
266 entry
= synth_get_dentry(node
, &synth_open
->dent
, synth_open
->offset
);
268 synth_open
->offset
++;
273 static int synth_open(FsContext
*ctx
, V9fsPath
*fs_path
,
274 int flags
, V9fsFidOpenState
*fs
)
276 V9fsSynthOpenState
*synth_open
;
277 V9fsSynthNode
*node
= *(V9fsSynthNode
**)fs_path
->data
;
279 synth_open
= g_new0(V9fsSynthOpenState
, 1);
280 synth_open
->node
= node
;
282 fs
->private = synth_open
;
286 static int synth_open2(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
287 const char *name
, int flags
,
288 FsCred
*credp
, V9fsFidOpenState
*fs
)
294 static int synth_close(FsContext
*ctx
, V9fsFidOpenState
*fs
)
296 V9fsSynthOpenState
*synth_open
= fs
->private;
297 V9fsSynthNode
*node
= synth_open
->node
;
305 static ssize_t
synth_pwritev(FsContext
*ctx
, V9fsFidOpenState
*fs
,
306 const struct iovec
*iov
,
307 int iovcnt
, off_t offset
)
309 int i
, count
= 0, wcount
;
310 V9fsSynthOpenState
*synth_open
= fs
->private;
311 V9fsSynthNode
*node
= synth_open
->node
;
312 if (!node
->attr
->write
) {
316 for (i
= 0; i
< iovcnt
; i
++) {
317 wcount
= node
->attr
->write(iov
[i
].iov_base
, iov
[i
].iov_len
,
318 offset
, node
->private);
321 /* If we wrote less than requested. we are done */
322 if (wcount
< iov
[i
].iov_len
) {
329 static ssize_t
synth_preadv(FsContext
*ctx
, V9fsFidOpenState
*fs
,
330 const struct iovec
*iov
,
331 int iovcnt
, off_t offset
)
333 int i
, count
= 0, rcount
;
334 V9fsSynthOpenState
*synth_open
= fs
->private;
335 V9fsSynthNode
*node
= synth_open
->node
;
336 if (!node
->attr
->read
) {
340 for (i
= 0; i
< iovcnt
; i
++) {
341 rcount
= node
->attr
->read(iov
[i
].iov_base
, iov
[i
].iov_len
,
342 offset
, node
->private);
345 /* If we read less than requested. we are done */
346 if (rcount
< iov
[i
].iov_len
) {
353 static int synth_truncate(FsContext
*ctx
, V9fsPath
*path
, off_t offset
)
359 static int synth_chmod(FsContext
*fs_ctx
, V9fsPath
*path
, FsCred
*credp
)
365 static int synth_mknod(FsContext
*fs_ctx
, V9fsPath
*path
,
366 const char *buf
, FsCred
*credp
)
372 static int synth_mkdir(FsContext
*fs_ctx
, V9fsPath
*path
,
373 const char *buf
, FsCred
*credp
)
379 static ssize_t
synth_readlink(FsContext
*fs_ctx
, V9fsPath
*path
,
380 char *buf
, size_t bufsz
)
386 static int synth_symlink(FsContext
*fs_ctx
, const char *oldpath
,
387 V9fsPath
*newpath
, const char *buf
, FsCred
*credp
)
393 static int synth_link(FsContext
*fs_ctx
, V9fsPath
*oldpath
,
394 V9fsPath
*newpath
, const char *buf
)
400 static int synth_rename(FsContext
*ctx
, const char *oldpath
,
407 static int synth_chown(FsContext
*fs_ctx
, V9fsPath
*path
, FsCred
*credp
)
413 static int synth_utimensat(FsContext
*fs_ctx
, V9fsPath
*path
,
414 const struct timespec
*buf
)
420 static int synth_remove(FsContext
*ctx
, const char *path
)
426 static int synth_fsync(FsContext
*ctx
, int fid_type
,
427 V9fsFidOpenState
*fs
, int datasync
)
433 static int synth_statfs(FsContext
*s
, V9fsPath
*fs_path
,
434 struct statfs
*stbuf
)
436 stbuf
->f_type
= 0xABCD;
437 stbuf
->f_bsize
= 512;
439 stbuf
->f_files
= synth_node_count
;
440 #ifndef CONFIG_DARWIN
441 stbuf
->f_namelen
= NAME_MAX
;
446 static ssize_t
synth_lgetxattr(FsContext
*ctx
, V9fsPath
*path
,
447 const char *name
, void *value
, size_t size
)
453 static ssize_t
synth_llistxattr(FsContext
*ctx
, V9fsPath
*path
,
454 void *value
, size_t size
)
460 static int synth_lsetxattr(FsContext
*ctx
, V9fsPath
*path
,
461 const char *name
, void *value
,
462 size_t size
, int flags
)
468 static int synth_lremovexattr(FsContext
*ctx
,
469 V9fsPath
*path
, const char *name
)
475 static int synth_name_to_path(FsContext
*ctx
, V9fsPath
*dir_path
,
476 const char *name
, V9fsPath
*target
)
479 V9fsSynthNode
*dir_node
;
481 /* "." and ".." are not allowed */
482 if (!strcmp(name
, ".") || !strcmp(name
, "..")) {
488 dir_node
= &synth_root
;
490 dir_node
= *(V9fsSynthNode
**)dir_path
->data
;
492 if (!strcmp(name
, "/")) {
496 /* search for the name in the childern */
498 QLIST_FOREACH(node
, &dir_node
->child
, sibling
) {
499 if (!strcmp(node
->name
, name
)) {
510 /* Copy the node pointer to fid */
511 g_free(target
->data
);
512 target
->data
= g_memdup(&node
, sizeof(void *));
513 target
->size
= sizeof(void *);
517 static int synth_renameat(FsContext
*ctx
, V9fsPath
*olddir
,
518 const char *old_name
, V9fsPath
*newdir
,
519 const char *new_name
)
525 static int synth_unlinkat(FsContext
*ctx
, V9fsPath
*dir
,
526 const char *name
, int flags
)
532 static ssize_t
v9fs_synth_qtest_write(void *buf
, int len
, off_t offset
,
538 static ssize_t
v9fs_synth_qtest_flush_write(void *buf
, int len
, off_t offset
,
541 bool should_block
= !!*(uint8_t *)buf
;
544 /* This will cause the server to call us again until we're cancelled */
552 static int synth_init(FsContext
*ctx
, Error
**errp
)
554 QLIST_INIT(&synth_root
.child
);
555 qemu_mutex_init(&synth_mutex
);
557 /* Add "." and ".." entries for root */
558 v9fs_add_dir_node(&synth_root
, synth_root
.attr
->mode
,
559 "..", synth_root
.attr
, synth_root
.attr
->inode
);
560 v9fs_add_dir_node(&synth_root
, synth_root
.attr
->mode
,
561 ".", synth_root
.attr
, synth_root
.attr
->inode
);
563 /* Mark the subsystem is ready for use */
566 if (qtest_enabled()) {
567 V9fsSynthNode
*node
= NULL
;
570 /* Directory hierarchy for WALK test */
571 for (i
= 0; i
< P9_MAXWELEM
; i
++) {
572 char *name
= g_strdup_printf(QTEST_V9FS_SYNTH_WALK_FILE
, i
);
574 ret
= qemu_v9fs_synth_mkdir(node
, 0700, name
, &node
);
579 /* File for LOPEN test */
580 ret
= qemu_v9fs_synth_add_file(NULL
, 0, QTEST_V9FS_SYNTH_LOPEN_FILE
,
584 /* File for WRITE test */
585 ret
= qemu_v9fs_synth_add_file(NULL
, 0, QTEST_V9FS_SYNTH_WRITE_FILE
,
586 NULL
, v9fs_synth_qtest_write
, ctx
);
589 /* File for FLUSH test */
590 ret
= qemu_v9fs_synth_add_file(NULL
, 0, QTEST_V9FS_SYNTH_FLUSH_FILE
,
591 NULL
, v9fs_synth_qtest_flush_write
,
595 /* Directory for READDIR test */
597 V9fsSynthNode
*dir
= NULL
;
598 ret
= qemu_v9fs_synth_mkdir(
599 NULL
, 0700, QTEST_V9FS_SYNTH_READDIR_DIR
, &dir
602 for (i
= 0; i
< QTEST_V9FS_SYNTH_READDIR_NFILES
; ++i
) {
603 char *name
= g_strdup_printf(
604 QTEST_V9FS_SYNTH_READDIR_FILE
, i
606 ret
= qemu_v9fs_synth_add_file(
607 dir
, 0, name
, NULL
, NULL
, ctx
618 FileOperations synth_ops
= {
620 .lstat
= synth_lstat
,
621 .readlink
= synth_readlink
,
622 .close
= synth_close
,
623 .closedir
= synth_closedir
,
625 .opendir
= synth_opendir
,
626 .rewinddir
= synth_rewinddir
,
627 .telldir
= synth_telldir
,
628 .readdir
= synth_readdir
,
629 .seekdir
= synth_seekdir
,
630 .preadv
= synth_preadv
,
631 .pwritev
= synth_pwritev
,
632 .chmod
= synth_chmod
,
633 .mknod
= synth_mknod
,
634 .mkdir
= synth_mkdir
,
635 .fstat
= synth_fstat
,
636 .open2
= synth_open2
,
637 .symlink
= synth_symlink
,
639 .truncate
= synth_truncate
,
640 .rename
= synth_rename
,
641 .chown
= synth_chown
,
642 .utimensat
= synth_utimensat
,
643 .remove
= synth_remove
,
644 .fsync
= synth_fsync
,
645 .statfs
= synth_statfs
,
646 .lgetxattr
= synth_lgetxattr
,
647 .llistxattr
= synth_llistxattr
,
648 .lsetxattr
= synth_lsetxattr
,
649 .lremovexattr
= synth_lremovexattr
,
650 .name_to_path
= synth_name_to_path
,
651 .renameat
= synth_renameat
,
652 .unlinkat
= synth_unlinkat
,