]> git.proxmox.com Git - mirror_qemu.git/blame - hw/9pfs/virtio-9p-synth.c
hw/9pfs: Add synthetic file system support using 9p
[mirror_qemu.git] / hw / 9pfs / virtio-9p-synth.c
CommitLineData
9db221ae
AK
1/*
2 * Virtio 9p synthetic file system support
3 *
4 * Copyright IBM, Corp. 2011
5 *
6 * Authors:
7 * Malahal Naineni <malahal@us.ibm.com>
8 * Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
9 *
10 * This work is licensed under the terms of the GNU GPL, version 2. See
11 * the COPYING file in the top-level directory.
12 *
13 */
14
15#include "hw/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"
20
21#include <sys/stat.h>
22
23/* Root node for synth file system */
24V9fsSynthNode v9fs_synth_root = {
25 .name = "/",
26 .actual_attr = {
27 .mode = 0555 | S_IFDIR,
28 .nlink = 1,
29 },
30 .attr = &v9fs_synth_root.actual_attr,
31};
32
33/*FIXME!! should be converted to qemu_rwlock_t */
34static pthread_rwlock_t v9fs_synth_mutex;
35static int v9fs_synth_node_count;
36/* set to 1 when the synth fs is ready */
37static int v9fs_synth_fs;
38
39static V9fsSynthNode *v9fs_add_dir_node(V9fsSynthNode *parent, int mode,
40 const char *name,
41 V9fsSynthNodeAttr *attr, int inode)
42{
43 V9fsSynthNode *node;
44
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;
60 }
61 node->private = node;
62 strncpy(node->name, name, sizeof(node->name));
63 QLIST_INSERT_HEAD(&parent->child, node, sibling);
64 return node;
65}
66
67int qemu_v9fs_synth_mkdir(V9fsSynthNode *parent, int mode,
68 const char *name, V9fsSynthNode **result)
69{
70 int ret;
71 V9fsSynthNode *node, *tmp;
72
73 if (!v9fs_synth_fs) {
74 return EAGAIN;
75 }
76 if (!name || (strlen(name) >= NAME_MAX)) {
77 return EINVAL;
78 }
79 if (!parent) {
80 parent = &v9fs_synth_root;
81 }
82 pthread_rwlock_wrlock(&v9fs_synth_mutex);
83 QLIST_FOREACH(tmp, &parent->child, sibling) {
84 if (!strcmp(tmp->name, name)) {
85 ret = EEXIST;
86 goto err_out;
87 }
88 }
89 /* Add the 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);
95 *result = node;
96 ret = 0;
97err_out:
98 pthread_rwlock_unlock(&v9fs_synth_mutex);
99 return ret;
100}
101
102int qemu_v9fs_synth_add_file(V9fsSynthNode *parent, int mode,
103 const char *name, v9fs_synth_read read,
104 v9fs_synth_write write, void *arg)
105{
106 int ret;
107 V9fsSynthNode *node, *tmp;
108
109 if (!v9fs_synth_fs) {
110 return EAGAIN;
111 }
112 if (!name || (strlen(name) >= NAME_MAX)) {
113 return EINVAL;
114 }
115 if (!parent) {
116 parent = &v9fs_synth_root;
117 }
118
119 pthread_rwlock_wrlock(&v9fs_synth_mutex);
120 QLIST_FOREACH(tmp, &parent->child, sibling) {
121 if (!strcmp(tmp->name, name)) {
122 ret = EEXIST;
123 goto err_out;
124 }
125 }
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;
135 node->private = arg;
136 strncpy(node->name, name, sizeof(node->name));
137 QLIST_INSERT_HEAD(&parent->child, node, sibling);
138 ret = 0;
139err_out:
140 pthread_rwlock_unlock(&v9fs_synth_mutex);
141 return ret;
142}
143
144static void v9fs_synth_fill_statbuf(V9fsSynthNode *node, struct stat *stbuf)
145{
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;
159}
160
161static int v9fs_synth_lstat(FsContext *fs_ctx,
162 V9fsPath *fs_path, struct stat *stbuf)
163{
164 V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;
165
166 v9fs_synth_fill_statbuf(node, stbuf);
167 return 0;
168}
169
170static int v9fs_synth_fstat(FsContext *fs_ctx,
171 V9fsFidOpenState *fs, struct stat *stbuf)
172{
173 V9fsSynthOpenState *synth_open = fs->private;
174 v9fs_synth_fill_statbuf(synth_open->node, stbuf);
175 return 0;
176}
177
178static int v9fs_synth_opendir(FsContext *ctx,
179 V9fsPath *fs_path, V9fsFidOpenState *fs)
180{
181 V9fsSynthOpenState *synth_open;
182 V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;
183
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;
189}
190
191static int v9fs_synth_closedir(FsContext *ctx, V9fsFidOpenState *fs)
192{
193 V9fsSynthOpenState *synth_open = fs->private;
194 V9fsSynthNode *node = synth_open->node;
195
196 node->open_count--;
197 g_free(synth_open);
198 fs->private = NULL;
199 return 0;
200}
201
202static off_t v9fs_synth_telldir(FsContext *ctx, V9fsFidOpenState *fs)
203{
204 V9fsSynthOpenState *synth_open = fs->private;
205 return synth_open->offset;
206}
207
208static void v9fs_synth_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
209{
210 V9fsSynthOpenState *synth_open = fs->private;
211 synth_open->offset = off;
212}
213
214static void v9fs_synth_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
215{
216 v9fs_synth_seekdir(ctx, fs, 0);
217}
218
219static void v9fs_synth_direntry(V9fsSynthNode *node,
220 struct dirent *entry, off_t off)
221{
222 strcpy(entry->d_name, node->name);
223 entry->d_ino = node->attr->inode;
224 entry->d_off = off + 1;
225}
226
227static int v9fs_synth_get_dentry(V9fsSynthNode *dir, struct dirent *entry,
228 struct dirent **result, off_t off)
229{
230 int i = 0;
231 V9fsSynthNode *node;
232
233 pthread_rwlock_rdlock(&v9fs_synth_mutex);
234 QLIST_FOREACH(node, &dir->child, sibling) {
235 /* This is the off child of the directory */
236 if (i == off) {
237 break;
238 }
239 i++;
240 }
241 pthread_rwlock_unlock(&v9fs_synth_mutex);
242 if (!node) {
243 /* end of directory */
244 *result = NULL;
245 return 0;
246 }
247 v9fs_synth_direntry(node, entry, off);
248 *result = entry;
249 return 0;
250}
251
252static int v9fs_synth_readdir_r(FsContext *ctx, V9fsFidOpenState *fs,
253 struct dirent *entry, struct dirent **result)
254{
255 int ret;
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++;
261 }
262 return ret;
263}
264
265static int v9fs_synth_open(FsContext *ctx, V9fsPath *fs_path,
266 int flags, V9fsFidOpenState *fs)
267{
268 V9fsSynthOpenState *synth_open;
269 V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;
270
271 synth_open = g_malloc(sizeof(*synth_open));
272 synth_open->node = node;
273 node->open_count++;
274 fs->private = synth_open;
275 return 0;
276}
277
278static int v9fs_synth_open2(FsContext *fs_ctx, V9fsPath *dir_path,
279 const char *name, int flags,
280 FsCred *credp, V9fsFidOpenState *fs)
281{
282 errno = ENOSYS;
283 return -1;
284}
285
286static int v9fs_synth_close(FsContext *ctx, V9fsFidOpenState *fs)
287{
288 V9fsSynthOpenState *synth_open = fs->private;
289 V9fsSynthNode *node = synth_open->node;
290
291 node->open_count--;
292 g_free(synth_open);
293 fs->private = NULL;
294 return 0;
295}
296
297static ssize_t v9fs_synth_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
298 const struct iovec *iov,
299 int iovcnt, off_t offset)
300{
301 int i, count = 0, wcount;
302 V9fsSynthOpenState *synth_open = fs->private;
303 V9fsSynthNode *node = synth_open->node;
304 if (!node->attr->write) {
305 errno = EPERM;
306 return -1;
307 }
308 for (i = 0; i < iovcnt; i++) {
309 wcount = node->attr->write(iov[i].iov_base, iov[i].iov_len,
310 offset, node->private);
311 offset += wcount;
312 count += wcount;
313 /* If we wrote less than requested. we are done */
314 if (wcount < iov[i].iov_len) {
315 break;
316 }
317 }
318 return count;
319}
320
321static ssize_t v9fs_synth_preadv(FsContext *ctx, V9fsFidOpenState *fs,
322 const struct iovec *iov,
323 int iovcnt, off_t offset)
324{
325 int i, count = 0, rcount;
326 V9fsSynthOpenState *synth_open = fs->private;
327 V9fsSynthNode *node = synth_open->node;
328 if (!node->attr->read) {
329 errno = EPERM;
330 return -1;
331 }
332 for (i = 0; i < iovcnt; i++) {
333 rcount = node->attr->read(iov[i].iov_base, iov[i].iov_len,
334 offset, node->private);
335 offset += rcount;
336 count += rcount;
337 /* If we read less than requested. we are done */
338 if (rcount < iov[i].iov_len) {
339 break;
340 }
341 }
342 return count;
343}
344
345static int v9fs_synth_truncate(FsContext *ctx, V9fsPath *path, off_t offset)
346{
347 errno = ENOSYS;
348 return -1;
349}
350
351static int v9fs_synth_chmod(FsContext *fs_ctx, V9fsPath *path, FsCred *credp)
352{
353 errno = EPERM;
354 return -1;
355}
356
357static int v9fs_synth_mknod(FsContext *fs_ctx, V9fsPath *path,
358 const char *buf, FsCred *credp)
359{
360 errno = EPERM;
361 return -1;
362}
363
364static int v9fs_synth_mkdir(FsContext *fs_ctx, V9fsPath *path,
365 const char *buf, FsCred *credp)
366{
367 errno = EPERM;
368 return -1;
369}
370
371static ssize_t v9fs_synth_readlink(FsContext *fs_ctx, V9fsPath *path,
372 char *buf, size_t bufsz)
373{
374 errno = ENOSYS;
375 return -1;
376}
377
378static int v9fs_synth_symlink(FsContext *fs_ctx, const char *oldpath,
379 V9fsPath *newpath, const char *buf, FsCred *credp)
380{
381 errno = EPERM;
382 return -1;
383}
384
385static int v9fs_synth_link(FsContext *fs_ctx, V9fsPath *oldpath,
386 V9fsPath *newpath, const char *buf)
387{
388 errno = EPERM;
389 return -1;
390}
391
392static int v9fs_synth_rename(FsContext *ctx, const char *oldpath,
393 const char *newpath)
394{
395 errno = EPERM;
396 return -1;
397}
398
399static int v9fs_synth_chown(FsContext *fs_ctx, V9fsPath *path, FsCred *credp)
400{
401 errno = EPERM;
402 return -1;
403}
404
405static int v9fs_synth_utimensat(FsContext *fs_ctx, V9fsPath *path,
406 const struct timespec *buf)
407{
408 errno = EPERM;
409 return 0;
410}
411
412static int v9fs_synth_remove(FsContext *ctx, const char *path)
413{
414 errno = EPERM;
415 return -1;
416}
417
418static int v9fs_synth_fsync(FsContext *ctx, V9fsFidOpenState *fs, int datasync)
419{
420 errno = ENOSYS;
421 return 0;
422}
423
424static int v9fs_synth_statfs(FsContext *s, V9fsPath *fs_path,
425 struct statfs *stbuf)
426{
427 stbuf->f_type = 0xABCD;
428 stbuf->f_bsize = 512;
429 stbuf->f_blocks = 0;
430 stbuf->f_files = v9fs_synth_node_count;
431 stbuf->f_namelen = NAME_MAX;
432 return 0;
433}
434
435static ssize_t v9fs_synth_lgetxattr(FsContext *ctx, V9fsPath *path,
436 const char *name, void *value, size_t size)
437{
438 errno = ENOTSUP;
439 return -1;
440}
441
442static ssize_t v9fs_synth_llistxattr(FsContext *ctx, V9fsPath *path,
443 void *value, size_t size)
444{
445 errno = ENOTSUP;
446 return -1;
447}
448
449static int v9fs_synth_lsetxattr(FsContext *ctx, V9fsPath *path,
450 const char *name, void *value,
451 size_t size, int flags)
452{
453 errno = ENOTSUP;
454 return -1;
455}
456
457static int v9fs_synth_lremovexattr(FsContext *ctx,
458 V9fsPath *path, const char *name)
459{
460 errno = ENOTSUP;
461 return -1;
462}
463
464static int v9fs_synth_name_to_path(FsContext *ctx, V9fsPath *dir_path,
465 const char *name, V9fsPath *target)
466{
467 V9fsSynthNode *node;
468 V9fsSynthNode *dir_node;
469
470 /* "." and ".." are not allowed */
471 if (!strcmp(name, ".") || !strcmp(name, "..")) {
472 errno = EINVAL;
473 return -1;
474
475 }
476 if (!dir_path) {
477 dir_node = &v9fs_synth_root;
478 } else {
479 dir_node = *(V9fsSynthNode **)dir_path->data;
480 }
481 if (!strcmp(name, "/")) {
482 node = dir_node;
483 goto out;
484 }
485 /* search for the name in the childern */
486 pthread_rwlock_rdlock(&v9fs_synth_mutex);
487 QLIST_FOREACH(node, &dir_node->child, sibling) {
488 if (!strcmp(node->name, name)) {
489 break;
490 }
491 }
492 pthread_rwlock_unlock(&v9fs_synth_mutex);
493 if (!node) {
494 errno = ENOENT;
495 return -1;
496 }
497out:
498 /* Copy the node pointer to fid */
499 target->data = g_malloc(sizeof(void *));
500 memcpy(target->data, &node, sizeof(void *));
501 target->size = sizeof(void *);
502 return 0;
503}
504
505static int v9fs_synth_renameat(FsContext *ctx, V9fsPath *olddir,
506 const char *old_name, V9fsPath *newdir,
507 const char *new_name)
508{
509 errno = EPERM;
510 return -1;
511}
512
513static int v9fs_synth_unlinkat(FsContext *ctx, V9fsPath *dir,
514 const char *name, int flags)
515{
516 errno = EPERM;
517 return -1;
518}
519
520static int v9fs_synth_init(FsContext *ctx)
521{
522 pthread_rwlockattr_t rwlockattr;
523
524 QLIST_INIT(&v9fs_synth_root.child);
525 pthread_rwlockattr_init(&rwlockattr);
526 pthread_rwlock_init(&v9fs_synth_mutex, &rwlockattr);
527
528 /* Add "." and ".." entries for root */
529 v9fs_add_dir_node(&v9fs_synth_root, v9fs_synth_root.attr->mode,
530 "..", v9fs_synth_root.attr, v9fs_synth_root.attr->inode);
531 v9fs_add_dir_node(&v9fs_synth_root, v9fs_synth_root.attr->mode,
532 ".", v9fs_synth_root.attr, v9fs_synth_root.attr->inode);
533
534 /* Mark the subsystem is ready for use */
535 v9fs_synth_fs = 1;
536 return 0;
537}
538
539FileOperations synth_ops = {
540 .init = v9fs_synth_init,
541 .lstat = v9fs_synth_lstat,
542 .readlink = v9fs_synth_readlink,
543 .close = v9fs_synth_close,
544 .closedir = v9fs_synth_closedir,
545 .open = v9fs_synth_open,
546 .opendir = v9fs_synth_opendir,
547 .rewinddir = v9fs_synth_rewinddir,
548 .telldir = v9fs_synth_telldir,
549 .readdir_r = v9fs_synth_readdir_r,
550 .seekdir = v9fs_synth_seekdir,
551 .preadv = v9fs_synth_preadv,
552 .pwritev = v9fs_synth_pwritev,
553 .chmod = v9fs_synth_chmod,
554 .mknod = v9fs_synth_mknod,
555 .mkdir = v9fs_synth_mkdir,
556 .fstat = v9fs_synth_fstat,
557 .open2 = v9fs_synth_open2,
558 .symlink = v9fs_synth_symlink,
559 .link = v9fs_synth_link,
560 .truncate = v9fs_synth_truncate,
561 .rename = v9fs_synth_rename,
562 .chown = v9fs_synth_chown,
563 .utimensat = v9fs_synth_utimensat,
564 .remove = v9fs_synth_remove,
565 .fsync = v9fs_synth_fsync,
566 .statfs = v9fs_synth_statfs,
567 .lgetxattr = v9fs_synth_lgetxattr,
568 .llistxattr = v9fs_synth_llistxattr,
569 .lsetxattr = v9fs_synth_lsetxattr,
570 .lremovexattr = v9fs_synth_lremovexattr,
571 .name_to_path = v9fs_synth_name_to_path,
572 .renameat = v9fs_synth_renameat,
573 .unlinkat = v9fs_synth_unlinkat,
574};