]> git.proxmox.com Git - mirror_qemu.git/blame - hw/9pfs/9p-synth.c
9pfs: disable msize warning for synth driver
[mirror_qemu.git] / hw / 9pfs / 9p-synth.c
CommitLineData
9db221ae 1/*
aae91ad9 2 * 9p synthetic file system support
9db221ae
AK
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
fbc04127 15#include "qemu/osdep.h"
ebe74f8b 16#include "9p.h"
9db221ae 17#include "fsdev/qemu-fsdev.h"
364031f1 18#include "9p-synth.h"
7911747b 19#include "qemu/rcu.h"
341774fe 20#include "qemu/rcu_queue.h"
f348b6d1 21#include "qemu/cutils.h"
2893ddd5 22#include "sysemu/qtest.h"
9db221ae
AK
23
24/* Root node for synth file system */
b05528b5 25static V9fsSynthNode synth_root = {
9db221ae
AK
26 .name = "/",
27 .actual_attr = {
28 .mode = 0555 | S_IFDIR,
29 .nlink = 1,
30 },
b05528b5 31 .attr = &synth_root.actual_attr,
9db221ae
AK
32};
33
b05528b5
GK
34static QemuMutex synth_mutex;
35static int synth_node_count;
9db221ae 36/* set to 1 when the synth fs is ready */
b05528b5 37static int synth_fs;
9db221ae
AK
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;
a79b5f8b 62 pstrcpy(node->name, sizeof(node->name), name);
2583e443 63 QLIST_INSERT_HEAD_RCU(&parent->child, node, sibling);
9db221ae
AK
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
b05528b5 73 if (!synth_fs) {
9db221ae
AK
74 return EAGAIN;
75 }
76 if (!name || (strlen(name) >= NAME_MAX)) {
77 return EINVAL;
78 }
79 if (!parent) {
b05528b5 80 parent = &synth_root;
9db221ae 81 }
b05528b5 82 qemu_mutex_lock(&synth_mutex);
9db221ae
AK
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 */
b05528b5 90 node = v9fs_add_dir_node(parent, mode, name, NULL, synth_node_count++);
9db221ae
AK
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:
b05528b5 98 qemu_mutex_unlock(&synth_mutex);
9db221ae
AK
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
b05528b5 109 if (!synth_fs) {
9db221ae
AK
110 return EAGAIN;
111 }
112 if (!name || (strlen(name) >= NAME_MAX)) {
113 return EINVAL;
114 }
115 if (!parent) {
b05528b5 116 parent = &synth_root;
9db221ae
AK
117 }
118
b05528b5 119 qemu_mutex_lock(&synth_mutex);
9db221ae
AK
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;
b05528b5 130 node->attr->inode = synth_node_count++;
9db221ae
AK
131 node->attr->nlink = 1;
132 node->attr->read = read;
133 node->attr->write = write;
134 node->attr->mode = mode;
135 node->private = arg;
a79b5f8b 136 pstrcpy(node->name, sizeof(node->name), name);
2583e443 137 QLIST_INSERT_HEAD_RCU(&parent->child, node, sibling);
9db221ae
AK
138 ret = 0;
139err_out:
b05528b5 140 qemu_mutex_unlock(&synth_mutex);
9db221ae
AK
141 return ret;
142}
143
b05528b5 144static void synth_fill_statbuf(V9fsSynthNode *node, struct stat *stbuf)
9db221ae
AK
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
b05528b5 161static int synth_lstat(FsContext *fs_ctx,
9db221ae
AK
162 V9fsPath *fs_path, struct stat *stbuf)
163{
164 V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;
165
b05528b5 166 synth_fill_statbuf(node, stbuf);
9db221ae
AK
167 return 0;
168}
169
b05528b5 170static int synth_fstat(FsContext *fs_ctx, int fid_type,
9db221ae
AK
171 V9fsFidOpenState *fs, struct stat *stbuf)
172{
173 V9fsSynthOpenState *synth_open = fs->private;
b05528b5 174 synth_fill_statbuf(synth_open->node, stbuf);
9db221ae
AK
175 return 0;
176}
177
b05528b5 178static int synth_opendir(FsContext *ctx,
9db221ae
AK
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
b05528b5 191static int synth_closedir(FsContext *ctx, V9fsFidOpenState *fs)
9db221ae
AK
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
b05528b5 202static off_t synth_telldir(FsContext *ctx, V9fsFidOpenState *fs)
9db221ae
AK
203{
204 V9fsSynthOpenState *synth_open = fs->private;
205 return synth_open->offset;
206}
207
b05528b5 208static void synth_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
9db221ae
AK
209{
210 V9fsSynthOpenState *synth_open = fs->private;
211 synth_open->offset = off;
212}
213
b05528b5 214static void synth_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
9db221ae 215{
b05528b5 216 synth_seekdir(ctx, fs, 0);
9db221ae
AK
217}
218
b05528b5 219static void synth_direntry(V9fsSynthNode *node,
9db221ae
AK
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
b05528b5 227static struct dirent *synth_get_dentry(V9fsSynthNode *dir,
635324e8 228 struct dirent *entry, off_t off)
9db221ae
AK
229{
230 int i = 0;
231 V9fsSynthNode *node;
232
2583e443 233 rcu_read_lock();
9db221ae
AK
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 }
2583e443 241 rcu_read_unlock();
9db221ae
AK
242 if (!node) {
243 /* end of directory */
635324e8 244 return NULL;
9db221ae 245 }
b05528b5 246 synth_direntry(node, entry, off);
635324e8 247 return entry;
9db221ae
AK
248}
249
b05528b5 250static struct dirent *synth_readdir(FsContext *ctx, V9fsFidOpenState *fs)
9db221ae 251{
635324e8 252 struct dirent *entry;
9db221ae
AK
253 V9fsSynthOpenState *synth_open = fs->private;
254 V9fsSynthNode *node = synth_open->node;
b05528b5 255 entry = synth_get_dentry(node, &synth_open->dent, synth_open->offset);
635324e8 256 if (entry) {
9db221ae
AK
257 synth_open->offset++;
258 }
635324e8 259 return entry;
9db221ae
AK
260}
261
b05528b5 262static int synth_open(FsContext *ctx, V9fsPath *fs_path,
9db221ae
AK
263 int flags, V9fsFidOpenState *fs)
264{
265 V9fsSynthOpenState *synth_open;
266 V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;
267
268 synth_open = g_malloc(sizeof(*synth_open));
269 synth_open->node = node;
270 node->open_count++;
271 fs->private = synth_open;
272 return 0;
273}
274
b05528b5 275static int synth_open2(FsContext *fs_ctx, V9fsPath *dir_path,
9db221ae
AK
276 const char *name, int flags,
277 FsCred *credp, V9fsFidOpenState *fs)
278{
279 errno = ENOSYS;
280 return -1;
281}
282
b05528b5 283static int synth_close(FsContext *ctx, V9fsFidOpenState *fs)
9db221ae
AK
284{
285 V9fsSynthOpenState *synth_open = fs->private;
286 V9fsSynthNode *node = synth_open->node;
287
288 node->open_count--;
289 g_free(synth_open);
290 fs->private = NULL;
291 return 0;
292}
293
b05528b5 294static ssize_t synth_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
9db221ae
AK
295 const struct iovec *iov,
296 int iovcnt, off_t offset)
297{
298 int i, count = 0, wcount;
299 V9fsSynthOpenState *synth_open = fs->private;
300 V9fsSynthNode *node = synth_open->node;
301 if (!node->attr->write) {
302 errno = EPERM;
303 return -1;
304 }
305 for (i = 0; i < iovcnt; i++) {
306 wcount = node->attr->write(iov[i].iov_base, iov[i].iov_len,
307 offset, node->private);
308 offset += wcount;
309 count += wcount;
310 /* If we wrote less than requested. we are done */
311 if (wcount < iov[i].iov_len) {
312 break;
313 }
314 }
315 return count;
316}
317
b05528b5 318static ssize_t synth_preadv(FsContext *ctx, V9fsFidOpenState *fs,
9db221ae
AK
319 const struct iovec *iov,
320 int iovcnt, off_t offset)
321{
322 int i, count = 0, rcount;
323 V9fsSynthOpenState *synth_open = fs->private;
324 V9fsSynthNode *node = synth_open->node;
325 if (!node->attr->read) {
326 errno = EPERM;
327 return -1;
328 }
329 for (i = 0; i < iovcnt; i++) {
330 rcount = node->attr->read(iov[i].iov_base, iov[i].iov_len,
331 offset, node->private);
332 offset += rcount;
333 count += rcount;
334 /* If we read less than requested. we are done */
335 if (rcount < iov[i].iov_len) {
336 break;
337 }
338 }
339 return count;
340}
341
b05528b5 342static int synth_truncate(FsContext *ctx, V9fsPath *path, off_t offset)
9db221ae
AK
343{
344 errno = ENOSYS;
345 return -1;
346}
347
b05528b5 348static int synth_chmod(FsContext *fs_ctx, V9fsPath *path, FsCred *credp)
9db221ae
AK
349{
350 errno = EPERM;
351 return -1;
352}
353
b05528b5 354static int synth_mknod(FsContext *fs_ctx, V9fsPath *path,
9db221ae
AK
355 const char *buf, FsCred *credp)
356{
357 errno = EPERM;
358 return -1;
359}
360
b05528b5 361static int synth_mkdir(FsContext *fs_ctx, V9fsPath *path,
9db221ae
AK
362 const char *buf, FsCred *credp)
363{
364 errno = EPERM;
365 return -1;
366}
367
b05528b5 368static ssize_t synth_readlink(FsContext *fs_ctx, V9fsPath *path,
9db221ae
AK
369 char *buf, size_t bufsz)
370{
371 errno = ENOSYS;
372 return -1;
373}
374
b05528b5 375static int synth_symlink(FsContext *fs_ctx, const char *oldpath,
9db221ae
AK
376 V9fsPath *newpath, const char *buf, FsCred *credp)
377{
378 errno = EPERM;
379 return -1;
380}
381
b05528b5 382static int synth_link(FsContext *fs_ctx, V9fsPath *oldpath,
9db221ae
AK
383 V9fsPath *newpath, const char *buf)
384{
385 errno = EPERM;
386 return -1;
387}
388
b05528b5 389static int synth_rename(FsContext *ctx, const char *oldpath,
9db221ae
AK
390 const char *newpath)
391{
392 errno = EPERM;
393 return -1;
394}
395
b05528b5 396static int synth_chown(FsContext *fs_ctx, V9fsPath *path, FsCred *credp)
9db221ae
AK
397{
398 errno = EPERM;
399 return -1;
400}
401
b05528b5 402static int synth_utimensat(FsContext *fs_ctx, V9fsPath *path,
9db221ae
AK
403 const struct timespec *buf)
404{
405 errno = EPERM;
406 return 0;
407}
408
b05528b5 409static int synth_remove(FsContext *ctx, const char *path)
9db221ae
AK
410{
411 errno = EPERM;
412 return -1;
413}
414
b05528b5 415static int synth_fsync(FsContext *ctx, int fid_type,
8b888272 416 V9fsFidOpenState *fs, int datasync)
9db221ae
AK
417{
418 errno = ENOSYS;
419 return 0;
420}
421
b05528b5 422static int synth_statfs(FsContext *s, V9fsPath *fs_path,
9db221ae
AK
423 struct statfs *stbuf)
424{
425 stbuf->f_type = 0xABCD;
426 stbuf->f_bsize = 512;
427 stbuf->f_blocks = 0;
b05528b5 428 stbuf->f_files = synth_node_count;
9db221ae
AK
429 stbuf->f_namelen = NAME_MAX;
430 return 0;
431}
432
b05528b5 433static ssize_t synth_lgetxattr(FsContext *ctx, V9fsPath *path,
9db221ae
AK
434 const char *name, void *value, size_t size)
435{
436 errno = ENOTSUP;
437 return -1;
438}
439
b05528b5 440static ssize_t synth_llistxattr(FsContext *ctx, V9fsPath *path,
9db221ae
AK
441 void *value, size_t size)
442{
443 errno = ENOTSUP;
444 return -1;
445}
446
b05528b5 447static int synth_lsetxattr(FsContext *ctx, V9fsPath *path,
9db221ae
AK
448 const char *name, void *value,
449 size_t size, int flags)
450{
451 errno = ENOTSUP;
452 return -1;
453}
454
b05528b5 455static int synth_lremovexattr(FsContext *ctx,
9db221ae
AK
456 V9fsPath *path, const char *name)
457{
458 errno = ENOTSUP;
459 return -1;
460}
461
b05528b5 462static int synth_name_to_path(FsContext *ctx, V9fsPath *dir_path,
9db221ae
AK
463 const char *name, V9fsPath *target)
464{
465 V9fsSynthNode *node;
466 V9fsSynthNode *dir_node;
467
468 /* "." and ".." are not allowed */
469 if (!strcmp(name, ".") || !strcmp(name, "..")) {
470 errno = EINVAL;
471 return -1;
472
473 }
474 if (!dir_path) {
b05528b5 475 dir_node = &synth_root;
9db221ae
AK
476 } else {
477 dir_node = *(V9fsSynthNode **)dir_path->data;
478 }
479 if (!strcmp(name, "/")) {
480 node = dir_node;
481 goto out;
482 }
483 /* search for the name in the childern */
2583e443 484 rcu_read_lock();
9db221ae
AK
485 QLIST_FOREACH(node, &dir_node->child, sibling) {
486 if (!strcmp(node->name, name)) {
487 break;
488 }
489 }
2583e443
HPB
490 rcu_read_unlock();
491
9db221ae
AK
492 if (!node) {
493 errno = ENOENT;
494 return -1;
495 }
496out:
497 /* Copy the node pointer to fid */
6ce7177a 498 g_free(target->data);
453a1b23 499 target->data = g_memdup(&node, sizeof(void *));
9db221ae
AK
500 target->size = sizeof(void *);
501 return 0;
502}
503
b05528b5 504static int synth_renameat(FsContext *ctx, V9fsPath *olddir,
9db221ae
AK
505 const char *old_name, V9fsPath *newdir,
506 const char *new_name)
507{
508 errno = EPERM;
509 return -1;
510}
511
b05528b5 512static int synth_unlinkat(FsContext *ctx, V9fsPath *dir,
9db221ae
AK
513 const char *name, int flags)
514{
515 errno = EPERM;
516 return -1;
517}
518
354b86f8
GK
519static ssize_t v9fs_synth_qtest_write(void *buf, int len, off_t offset,
520 void *arg)
521{
522 return 1;
523}
524
357e2f7f
GK
525static ssize_t v9fs_synth_qtest_flush_write(void *buf, int len, off_t offset,
526 void *arg)
527{
528 bool should_block = !!*(uint8_t *)buf;
529
530 if (should_block) {
531 /* This will cause the server to call us again until we're cancelled */
532 errno = EINTR;
533 return -1;
534 }
535
536 return 1;
537}
538
65603a80 539static int synth_init(FsContext *ctx, Error **errp)
9db221ae 540{
b05528b5
GK
541 QLIST_INIT(&synth_root.child);
542 qemu_mutex_init(&synth_mutex);
9db221ae 543
c418f935
CS
544 ctx->export_flags |= V9FS_NO_PERF_WARN;
545
9db221ae 546 /* Add "." and ".." entries for root */
b05528b5
GK
547 v9fs_add_dir_node(&synth_root, synth_root.attr->mode,
548 "..", synth_root.attr, synth_root.attr->inode);
549 v9fs_add_dir_node(&synth_root, synth_root.attr->mode,
550 ".", synth_root.attr, synth_root.attr->inode);
9db221ae
AK
551
552 /* Mark the subsystem is ready for use */
b05528b5 553 synth_fs = 1;
2893ddd5
GK
554
555 if (qtest_enabled()) {
556 V9fsSynthNode *node = NULL;
557 int i, ret;
558
559 /* Directory hierarchy for WALK test */
560 for (i = 0; i < P9_MAXWELEM; i++) {
561 char *name = g_strdup_printf(QTEST_V9FS_SYNTH_WALK_FILE, i);
562
563 ret = qemu_v9fs_synth_mkdir(node, 0700, name, &node);
564 assert(!ret);
565 g_free(name);
566 }
82469aae
GK
567
568 /* File for LOPEN test */
569 ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_LOPEN_FILE,
570 NULL, NULL, ctx);
571 assert(!ret);
354b86f8
GK
572
573 /* File for WRITE test */
574 ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_WRITE_FILE,
575 NULL, v9fs_synth_qtest_write, ctx);
576 assert(!ret);
357e2f7f
GK
577
578 /* File for FLUSH test */
579 ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_FLUSH_FILE,
580 NULL, v9fs_synth_qtest_flush_write,
581 ctx);
582 assert(!ret);
af46a3b2
CS
583
584 /* Directory for READDIR test */
585 {
586 V9fsSynthNode *dir = NULL;
587 ret = qemu_v9fs_synth_mkdir(
588 NULL, 0700, QTEST_V9FS_SYNTH_READDIR_DIR, &dir
589 );
590 assert(!ret);
591 for (i = 0; i < QTEST_V9FS_SYNTH_READDIR_NFILES; ++i) {
592 char *name = g_strdup_printf(
593 QTEST_V9FS_SYNTH_READDIR_FILE, i
594 );
595 ret = qemu_v9fs_synth_add_file(
596 dir, 0, name, NULL, NULL, ctx
597 );
598 assert(!ret);
599 g_free(name);
600 }
601 }
2893ddd5
GK
602 }
603
9db221ae
AK
604 return 0;
605}
606
607FileOperations synth_ops = {
b05528b5
GK
608 .init = synth_init,
609 .lstat = synth_lstat,
610 .readlink = synth_readlink,
611 .close = synth_close,
612 .closedir = synth_closedir,
613 .open = synth_open,
614 .opendir = synth_opendir,
615 .rewinddir = synth_rewinddir,
616 .telldir = synth_telldir,
617 .readdir = synth_readdir,
618 .seekdir = synth_seekdir,
619 .preadv = synth_preadv,
620 .pwritev = synth_pwritev,
621 .chmod = synth_chmod,
622 .mknod = synth_mknod,
623 .mkdir = synth_mkdir,
624 .fstat = synth_fstat,
625 .open2 = synth_open2,
626 .symlink = synth_symlink,
627 .link = synth_link,
628 .truncate = synth_truncate,
629 .rename = synth_rename,
630 .chown = synth_chown,
631 .utimensat = synth_utimensat,
632 .remove = synth_remove,
633 .fsync = synth_fsync,
634 .statfs = synth_statfs,
635 .lgetxattr = synth_lgetxattr,
636 .llistxattr = synth_llistxattr,
637 .lsetxattr = synth_lsetxattr,
638 .lremovexattr = synth_lremovexattr,
639 .name_to_path = synth_name_to_path,
640 .renameat = synth_renameat,
641 .unlinkat = synth_unlinkat,
9db221ae 642};