2 * Virtio 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 "hw/virtio/virtio.h"
16 #include "virtio-9p.h"
17 #include "virtio-9p-xattr.h"
18 #include "fsdev/qemu-fsdev.h"
19 #include "virtio-9p-synth.h"
21 #include "qemu/rcu_queue.h"
24 /* Root node for synth file system */
25 static V9fsSynthNode v9fs_synth_root
= {
28 .mode
= 0555 | S_IFDIR
,
31 .attr
= &v9fs_synth_root
.actual_attr
,
34 static QemuMutex v9fs_synth_mutex
;
35 static int v9fs_synth_node_count
;
36 /* set to 1 when the synth fs is ready */
37 static int v9fs_synth_fs
;
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
)) {
80 parent
= &v9fs_synth_root
;
82 qemu_mutex_lock(&v9fs_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
, v9fs_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(&v9fs_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
;
109 if (!v9fs_synth_fs
) {
112 if (!name
|| (strlen(name
) >= NAME_MAX
)) {
116 parent
= &v9fs_synth_root
;
119 qemu_mutex_lock(&v9fs_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
= v9fs_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(&v9fs_synth_mutex
);
144 static void v9fs_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 v9fs_synth_lstat(FsContext
*fs_ctx
,
162 V9fsPath
*fs_path
, struct stat
*stbuf
)
164 V9fsSynthNode
*node
= *(V9fsSynthNode
**)fs_path
->data
;
166 v9fs_synth_fill_statbuf(node
, stbuf
);
170 static int v9fs_synth_fstat(FsContext
*fs_ctx
, int fid_type
,
171 V9fsFidOpenState
*fs
, struct stat
*stbuf
)
173 V9fsSynthOpenState
*synth_open
= fs
->private;
174 v9fs_synth_fill_statbuf(synth_open
->node
, stbuf
);
178 static int v9fs_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 v9fs_synth_closedir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
193 V9fsSynthOpenState
*synth_open
= fs
->private;
194 V9fsSynthNode
*node
= synth_open
->node
;
202 static off_t
v9fs_synth_telldir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
204 V9fsSynthOpenState
*synth_open
= fs
->private;
205 return synth_open
->offset
;
208 static void v9fs_synth_seekdir(FsContext
*ctx
, V9fsFidOpenState
*fs
, off_t off
)
210 V9fsSynthOpenState
*synth_open
= fs
->private;
211 synth_open
->offset
= off
;
214 static void v9fs_synth_rewinddir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
216 v9fs_synth_seekdir(ctx
, fs
, 0);
219 static void v9fs_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 int v9fs_synth_get_dentry(V9fsSynthNode
*dir
, struct dirent
*entry
,
228 struct dirent
**result
, off_t off
)
234 QLIST_FOREACH(node
, &dir
->child
, sibling
) {
235 /* This is the off child of the directory */
243 /* end of directory */
247 v9fs_synth_direntry(node
, entry
, off
);
252 static int v9fs_synth_readdir_r(FsContext
*ctx
, V9fsFidOpenState
*fs
,
253 struct dirent
*entry
, struct dirent
**result
)
256 V9fsSynthOpenState
*synth_open
= fs
->private;
257 V9fsSynthNode
*node
= synth_open
->node
;
258 ret
= v9fs_synth_get_dentry(node
, entry
, result
, synth_open
->offset
);
259 if (!ret
&& *result
!= NULL
) {
260 synth_open
->offset
++;
265 static int v9fs_synth_open(FsContext
*ctx
, V9fsPath
*fs_path
,
266 int flags
, V9fsFidOpenState
*fs
)
268 V9fsSynthOpenState
*synth_open
;
269 V9fsSynthNode
*node
= *(V9fsSynthNode
**)fs_path
->data
;
271 synth_open
= g_malloc(sizeof(*synth_open
));
272 synth_open
->node
= node
;
274 fs
->private = synth_open
;
278 static int v9fs_synth_open2(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
279 const char *name
, int flags
,
280 FsCred
*credp
, V9fsFidOpenState
*fs
)
286 static int v9fs_synth_close(FsContext
*ctx
, V9fsFidOpenState
*fs
)
288 V9fsSynthOpenState
*synth_open
= fs
->private;
289 V9fsSynthNode
*node
= synth_open
->node
;
297 static ssize_t
v9fs_synth_pwritev(FsContext
*ctx
, V9fsFidOpenState
*fs
,
298 const struct iovec
*iov
,
299 int iovcnt
, off_t offset
)
301 int i
, count
= 0, wcount
;
302 V9fsSynthOpenState
*synth_open
= fs
->private;
303 V9fsSynthNode
*node
= synth_open
->node
;
304 if (!node
->attr
->write
) {
308 for (i
= 0; i
< iovcnt
; i
++) {
309 wcount
= node
->attr
->write(iov
[i
].iov_base
, iov
[i
].iov_len
,
310 offset
, node
->private);
313 /* If we wrote less than requested. we are done */
314 if (wcount
< iov
[i
].iov_len
) {
321 static ssize_t
v9fs_synth_preadv(FsContext
*ctx
, V9fsFidOpenState
*fs
,
322 const struct iovec
*iov
,
323 int iovcnt
, off_t offset
)
325 int i
, count
= 0, rcount
;
326 V9fsSynthOpenState
*synth_open
= fs
->private;
327 V9fsSynthNode
*node
= synth_open
->node
;
328 if (!node
->attr
->read
) {
332 for (i
= 0; i
< iovcnt
; i
++) {
333 rcount
= node
->attr
->read(iov
[i
].iov_base
, iov
[i
].iov_len
,
334 offset
, node
->private);
337 /* If we read less than requested. we are done */
338 if (rcount
< iov
[i
].iov_len
) {
345 static int v9fs_synth_truncate(FsContext
*ctx
, V9fsPath
*path
, off_t offset
)
351 static int v9fs_synth_chmod(FsContext
*fs_ctx
, V9fsPath
*path
, FsCred
*credp
)
357 static int v9fs_synth_mknod(FsContext
*fs_ctx
, V9fsPath
*path
,
358 const char *buf
, FsCred
*credp
)
364 static int v9fs_synth_mkdir(FsContext
*fs_ctx
, V9fsPath
*path
,
365 const char *buf
, FsCred
*credp
)
371 static ssize_t
v9fs_synth_readlink(FsContext
*fs_ctx
, V9fsPath
*path
,
372 char *buf
, size_t bufsz
)
378 static int v9fs_synth_symlink(FsContext
*fs_ctx
, const char *oldpath
,
379 V9fsPath
*newpath
, const char *buf
, FsCred
*credp
)
385 static int v9fs_synth_link(FsContext
*fs_ctx
, V9fsPath
*oldpath
,
386 V9fsPath
*newpath
, const char *buf
)
392 static int v9fs_synth_rename(FsContext
*ctx
, const char *oldpath
,
399 static int v9fs_synth_chown(FsContext
*fs_ctx
, V9fsPath
*path
, FsCred
*credp
)
405 static int v9fs_synth_utimensat(FsContext
*fs_ctx
, V9fsPath
*path
,
406 const struct timespec
*buf
)
412 static int v9fs_synth_remove(FsContext
*ctx
, const char *path
)
418 static int v9fs_synth_fsync(FsContext
*ctx
, int fid_type
,
419 V9fsFidOpenState
*fs
, int datasync
)
425 static int v9fs_synth_statfs(FsContext
*s
, V9fsPath
*fs_path
,
426 struct statfs
*stbuf
)
428 stbuf
->f_type
= 0xABCD;
429 stbuf
->f_bsize
= 512;
431 stbuf
->f_files
= v9fs_synth_node_count
;
432 stbuf
->f_namelen
= NAME_MAX
;
436 static ssize_t
v9fs_synth_lgetxattr(FsContext
*ctx
, V9fsPath
*path
,
437 const char *name
, void *value
, size_t size
)
443 static ssize_t
v9fs_synth_llistxattr(FsContext
*ctx
, V9fsPath
*path
,
444 void *value
, size_t size
)
450 static int v9fs_synth_lsetxattr(FsContext
*ctx
, V9fsPath
*path
,
451 const char *name
, void *value
,
452 size_t size
, int flags
)
458 static int v9fs_synth_lremovexattr(FsContext
*ctx
,
459 V9fsPath
*path
, const char *name
)
465 static int v9fs_synth_name_to_path(FsContext
*ctx
, V9fsPath
*dir_path
,
466 const char *name
, V9fsPath
*target
)
469 V9fsSynthNode
*dir_node
;
471 /* "." and ".." are not allowed */
472 if (!strcmp(name
, ".") || !strcmp(name
, "..")) {
478 dir_node
= &v9fs_synth_root
;
480 dir_node
= *(V9fsSynthNode
**)dir_path
->data
;
482 if (!strcmp(name
, "/")) {
486 /* search for the name in the childern */
488 QLIST_FOREACH(node
, &dir_node
->child
, sibling
) {
489 if (!strcmp(node
->name
, name
)) {
500 /* Copy the node pointer to fid */
501 target
->data
= g_malloc(sizeof(void *));
502 memcpy(target
->data
, &node
, sizeof(void *));
503 target
->size
= sizeof(void *);
507 static int v9fs_synth_renameat(FsContext
*ctx
, V9fsPath
*olddir
,
508 const char *old_name
, V9fsPath
*newdir
,
509 const char *new_name
)
515 static int v9fs_synth_unlinkat(FsContext
*ctx
, V9fsPath
*dir
,
516 const char *name
, int flags
)
522 static int v9fs_synth_init(FsContext
*ctx
)
524 QLIST_INIT(&v9fs_synth_root
.child
);
525 qemu_mutex_init(&v9fs_synth_mutex
);
527 /* Add "." and ".." entries for root */
528 v9fs_add_dir_node(&v9fs_synth_root
, v9fs_synth_root
.attr
->mode
,
529 "..", v9fs_synth_root
.attr
, v9fs_synth_root
.attr
->inode
);
530 v9fs_add_dir_node(&v9fs_synth_root
, v9fs_synth_root
.attr
->mode
,
531 ".", v9fs_synth_root
.attr
, v9fs_synth_root
.attr
->inode
);
533 /* Mark the subsystem is ready for use */
538 FileOperations synth_ops
= {
539 .init
= v9fs_synth_init
,
540 .lstat
= v9fs_synth_lstat
,
541 .readlink
= v9fs_synth_readlink
,
542 .close
= v9fs_synth_close
,
543 .closedir
= v9fs_synth_closedir
,
544 .open
= v9fs_synth_open
,
545 .opendir
= v9fs_synth_opendir
,
546 .rewinddir
= v9fs_synth_rewinddir
,
547 .telldir
= v9fs_synth_telldir
,
548 .readdir_r
= v9fs_synth_readdir_r
,
549 .seekdir
= v9fs_synth_seekdir
,
550 .preadv
= v9fs_synth_preadv
,
551 .pwritev
= v9fs_synth_pwritev
,
552 .chmod
= v9fs_synth_chmod
,
553 .mknod
= v9fs_synth_mknod
,
554 .mkdir
= v9fs_synth_mkdir
,
555 .fstat
= v9fs_synth_fstat
,
556 .open2
= v9fs_synth_open2
,
557 .symlink
= v9fs_synth_symlink
,
558 .link
= v9fs_synth_link
,
559 .truncate
= v9fs_synth_truncate
,
560 .rename
= v9fs_synth_rename
,
561 .chown
= v9fs_synth_chown
,
562 .utimensat
= v9fs_synth_utimensat
,
563 .remove
= v9fs_synth_remove
,
564 .fsync
= v9fs_synth_fsync
,
565 .statfs
= v9fs_synth_statfs
,
566 .lgetxattr
= v9fs_synth_lgetxattr
,
567 .llistxattr
= v9fs_synth_llistxattr
,
568 .lsetxattr
= v9fs_synth_lsetxattr
,
569 .lremovexattr
= v9fs_synth_lremovexattr
,
570 .name_to_path
= v9fs_synth_name_to_path
,
571 .renameat
= v9fs_synth_renameat
,
572 .unlinkat
= v9fs_synth_unlinkat
,