master
c 718 lines 19.3 KB
Raw
1 /*
2 * 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 /*
16 * Not so fast! You might want to read the 9p developer docs first:
17 * https://wiki.qemu.org/Documentation/9p
18 */
19
20 #include "qemu/osdep.h"
21 #include "9p.h"
22 #include "fsdev/qemu-fsdev.h"
23 #include "9p-synth.h"
24 #include "qemu/rcu.h"
25 #include "qemu/rcu_queue.h"
26 #include "qemu/cutils.h"
27 #include "system/qtest.h"
28 #include "qapi/error.h"
29 #include "qemu/option.h"
30
31 /* Root node for synth file system */
32 static V9fsSynthNode synth_root = {
33 .name = "/",
34 .actual_attr = {
35 .mode = 0555 | S_IFDIR,
36 .nlink = 1,
37 },
38 .attr = &synth_root.actual_attr,
39 };
40
41 static QemuMutex synth_mutex;
42 static int synth_node_count;
43 /* set to 1 when the synth fs is ready */
44 static int synth_fs;
45
46 static V9fsSynthNode *v9fs_add_dir_node(V9fsSynthNode *parent, int mode,
47 const char *name,
48 V9fsSynthNodeAttr *attr, int inode)
49 {
50 V9fsSynthNode *node;
51
52 /* Add directory type and remove write bits */
53 mode = ((mode & 0777) | S_IFDIR) & ~(S_IWUSR | S_IWGRP | S_IWOTH);
54 node = g_new0(V9fsSynthNode, 1);
55 if (attr) {
56 /* We are adding .. or . entries */
57 node->attr = attr;
58 node->attr->nlink++;
59 } else {
60 node->attr = &node->actual_attr;
61 node->attr->inode = inode;
62 node->attr->nlink = 1;
63 /* We don't allow write to directories */
64 node->attr->mode = mode;
65 node->attr->write = NULL;
66 node->attr->read = NULL;
67 }
68 node->private = node;
69 pstrcpy(node->name, sizeof(node->name), name);
70 QLIST_INSERT_HEAD_RCU(&parent->child, node, sibling);
71 return node;
72 }
73
74 int qemu_v9fs_synth_mkdir(V9fsSynthNode *parent, int mode,
75 const char *name, V9fsSynthNode **result)
76 {
77 V9fsSynthNode *node, *tmp;
78
79 if (!synth_fs) {
80 return -EAGAIN;
81 }
82 if (!name || (strlen(name) >= NAME_MAX)) {
83 return -EINVAL;
84 }
85 if (!parent) {
86 parent = &synth_root;
87 }
88 QEMU_LOCK_GUARD(&synth_mutex);
89 QLIST_FOREACH(tmp, &parent->child, sibling) {
90 if (!strcmp(tmp->name, name)) {
91 return -EEXIST;
92 }
93 }
94 /* Add the name */
95 node = v9fs_add_dir_node(parent, mode, name, NULL, ++synth_node_count);
96 v9fs_add_dir_node(node, parent->attr->mode, "..",
97 parent->attr, parent->attr->inode);
98 v9fs_add_dir_node(node, node->attr->mode, ".",
99 node->attr, node->attr->inode);
100 *result = node;
101 return 0;
102 }
103
104 int qemu_v9fs_synth_add_file(V9fsSynthNode *parent, int mode,
105 const char *name, v9fs_synth_read read,
106 v9fs_synth_write write, void *arg)
107 {
108 V9fsSynthNode *node, *tmp;
109
110 if (!synth_fs) {
111 return -EAGAIN;
112 }
113 if (!name || (strlen(name) >= NAME_MAX)) {
114 return -EINVAL;
115 }
116 if (!parent) {
117 parent = &synth_root;
118 }
119
120 QEMU_LOCK_GUARD(&synth_mutex);
121 QLIST_FOREACH(tmp, &parent->child, sibling) {
122 if (!strcmp(tmp->name, name)) {
123 return -EEXIST;
124 }
125 }
126 /* Add file type and remove write bits */
127 mode = ((mode & 0777) | S_IFREG);
128 node = g_new0(V9fsSynthNode, 1);
129 node->attr = &node->actual_attr;
130 node->attr->inode = ++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 pstrcpy(node->name, sizeof(node->name), name);
137 QLIST_INSERT_HEAD_RCU(&parent->child, node, sibling);
138 return 0;
139 }
140
141 static void synth_fill_statbuf(V9fsSynthNode *node, struct stat *stbuf)
142 {
143 stbuf->st_dev = 0;
144 stbuf->st_ino = node->attr->inode;
145 stbuf->st_mode = node->attr->mode;
146 stbuf->st_nlink = node->attr->nlink;
147 stbuf->st_uid = 0;
148 stbuf->st_gid = 0;
149 stbuf->st_rdev = 0;
150 stbuf->st_size = 0;
151 stbuf->st_blksize = 0;
152 stbuf->st_blocks = 0;
153 stbuf->st_atime = 0;
154 stbuf->st_mtime = 0;
155 stbuf->st_ctime = 0;
156 }
157
158 static int synth_lstat(FsContext *fs_ctx,
159 V9fsPath *fs_path, struct stat *stbuf)
160 {
161 V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;
162
163 synth_fill_statbuf(node, stbuf);
164 return 0;
165 }
166
167 static int synth_fstat(FsContext *fs_ctx, int fid_type,
168 V9fsFidOpenState *fs, struct stat *stbuf)
169 {
170 V9fsSynthOpenState *synth_open = fs->private;
171 synth_fill_statbuf(synth_open->node, stbuf);
172 return 0;
173 }
174
175 static int synth_opendir(FsContext *ctx,
176 V9fsPath *fs_path, V9fsFidOpenState *fs)
177 {
178 V9fsSynthOpenState *synth_open;
179 V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;
180
181 /*
182 * V9fsSynthOpenState contains 'struct dirent' which have OS-specific
183 * properties, thus it's zero cleared on allocation here and below
184 * in synth_open.
185 */
186 synth_open = g_new0(V9fsSynthOpenState, 1);
187 synth_open->node = node;
188 node->open_count++;
189 fs->private = synth_open;
190 return 0;
191 }
192
193 static int synth_closedir(FsContext *ctx, V9fsFidOpenState *fs)
194 {
195 V9fsSynthOpenState *synth_open = fs->private;
196 V9fsSynthNode *node = synth_open->node;
197
198 node->open_count--;
199 g_free(synth_open);
200 fs->private = NULL;
201 return 0;
202 }
203
204 static off_t synth_telldir(FsContext *ctx, V9fsFidOpenState *fs)
205 {
206 V9fsSynthOpenState *synth_open = fs->private;
207 return synth_open->offset;
208 }
209
210 static void synth_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
211 {
212 V9fsSynthOpenState *synth_open = fs->private;
213 synth_open->offset = off;
214 }
215
216 static void synth_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
217 {
218 synth_seekdir(ctx, fs, 0);
219 }
220
221 static void synth_direntry(V9fsSynthNode *node,
222 struct dirent *entry, off_t off)
223 {
224 size_t sz = strlen(node->name) + 1;
225 /*
226 * 'entry' is always inside of V9fsSynthOpenState which have NAME_MAX
227 * back padding. Ensure we do not overflow it.
228 */
229 g_assert(sizeof(struct dirent) + NAME_MAX >=
230 offsetof(struct dirent, d_name) + sz);
231 memcpy(entry->d_name, node->name, sz);
232 entry->d_ino = node->attr->inode;
233 #ifdef CONFIG_DARWIN
234 entry->d_seekoff = off + 1;
235 #else
236 entry->d_off = off + 1;
237 #endif
238 }
239
240 static struct dirent *synth_get_dentry(V9fsSynthNode *dir,
241 struct dirent *entry, off_t off)
242 {
243 int i = 0;
244 V9fsSynthNode *node;
245
246 rcu_read_lock();
247 QLIST_FOREACH(node, &dir->child, sibling) {
248 /* This is the off child of the directory */
249 if (i == off) {
250 break;
251 }
252 i++;
253 }
254 rcu_read_unlock();
255 if (!node) {
256 /* end of directory */
257 return NULL;
258 }
259 synth_direntry(node, entry, off);
260 return entry;
261 }
262
263 static struct dirent *synth_readdir(FsContext *ctx, V9fsFidOpenState *fs)
264 {
265 struct dirent *entry;
266 V9fsSynthOpenState *synth_open = fs->private;
267 V9fsSynthNode *node = synth_open->node;
268 entry = synth_get_dentry(node, &synth_open->dent, synth_open->offset);
269 if (entry) {
270 synth_open->offset++;
271 }
272 return entry;
273 }
274
275 static int synth_open(FsContext *ctx, V9fsPath *fs_path,
276 int flags, V9fsFidOpenState *fs)
277 {
278 V9fsSynthOpenState *synth_open;
279 V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;
280
281 synth_open = g_new0(V9fsSynthOpenState, 1);
282 synth_open->node = node;
283 node->open_count++;
284 fs->private = synth_open;
285 return 0;
286 }
287
288 static int synth_open2(FsContext *fs_ctx, V9fsPath *dir_path,
289 const char *name, int flags,
290 FsCred *credp, V9fsFidOpenState *fs)
291 {
292 errno = ENOSYS;
293 return -1;
294 }
295
296 static int synth_close(FsContext *ctx, V9fsFidOpenState *fs)
297 {
298 V9fsSynthOpenState *synth_open = fs->private;
299 V9fsSynthNode *node = synth_open->node;
300
301 node->open_count--;
302 g_free(synth_open);
303 fs->private = NULL;
304 return 0;
305 }
306
307 static ssize_t synth_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
308 const struct iovec *iov,
309 int iovcnt, off_t offset)
310 {
311 int i, count = 0, wcount;
312 V9fsSynthOpenState *synth_open = fs->private;
313 V9fsSynthNode *node = synth_open->node;
314 if (!node->attr->write) {
315 errno = EPERM;
316 return -1;
317 }
318 for (i = 0; i < iovcnt; i++) {
319 wcount = node->attr->write(iov[i].iov_base, iov[i].iov_len,
320 offset, node->private);
321 offset += wcount;
322 count += wcount;
323 /* If we wrote less than requested. we are done */
324 if (wcount < iov[i].iov_len) {
325 break;
326 }
327 }
328 return count;
329 }
330
331 static ssize_t synth_preadv(FsContext *ctx, V9fsFidOpenState *fs,
332 const struct iovec *iov,
333 int iovcnt, off_t offset)
334 {
335 int i, count = 0, rcount;
336 V9fsSynthOpenState *synth_open = fs->private;
337 V9fsSynthNode *node = synth_open->node;
338 if (!node->attr->read) {
339 errno = EPERM;
340 return -1;
341 }
342 for (i = 0; i < iovcnt; i++) {
343 rcount = node->attr->read(iov[i].iov_base, iov[i].iov_len,
344 offset, node->private);
345 offset += rcount;
346 count += rcount;
347 /* If we read less than requested. we are done */
348 if (rcount < iov[i].iov_len) {
349 break;
350 }
351 }
352 return count;
353 }
354
355 static int synth_truncate(FsContext *ctx, V9fsPath *path, off_t offset)
356 {
357 errno = ENOSYS;
358 return -1;
359 }
360
361 static int synth_ftruncate(FsContext *ctx, int fid_type, V9fsFidOpenState *fs,
362 off_t size)
363 {
364 errno = ENOSYS;
365 return -1;
366 }
367
368 static int synth_chmod(FsContext *fs_ctx, V9fsPath *path, FsCred *credp)
369 {
370 errno = EPERM;
371 return -1;
372 }
373
374 static int synth_mknod(FsContext *fs_ctx, V9fsPath *path,
375 const char *buf, FsCred *credp)
376 {
377 errno = EPERM;
378 return -1;
379 }
380
381 static int synth_mkdir(FsContext *fs_ctx, V9fsPath *path,
382 const char *buf, FsCred *credp)
383 {
384 errno = EPERM;
385 return -1;
386 }
387
388 static ssize_t synth_readlink(FsContext *fs_ctx, V9fsPath *path,
389 char *buf, size_t bufsz)
390 {
391 errno = ENOSYS;
392 return -1;
393 }
394
395 static int synth_symlink(FsContext *fs_ctx, const char *oldpath,
396 V9fsPath *newpath, const char *buf, FsCred *credp)
397 {
398 errno = EPERM;
399 return -1;
400 }
401
402 static int synth_link(FsContext *fs_ctx, V9fsPath *oldpath,
403 V9fsPath *newpath, const char *buf)
404 {
405 errno = EPERM;
406 return -1;
407 }
408
409 static int synth_rename(FsContext *ctx, const char *oldpath,
410 const char *newpath)
411 {
412 errno = EPERM;
413 return -1;
414 }
415
416 static int synth_chown(FsContext *fs_ctx, V9fsPath *path, FsCred *credp)
417 {
418 errno = EPERM;
419 return -1;
420 }
421
422 static int synth_utimensat(FsContext *fs_ctx, V9fsPath *path,
423 const struct timespec *buf)
424 {
425 errno = EPERM;
426 return 0;
427 }
428
429 static int synth_futimens(FsContext *fs_ctx, int fid_type, V9fsFidOpenState *fs,
430 const struct timespec *buf)
431 {
432 errno = ENOSYS;
433 return -1;
434 }
435
436 static int synth_remove(FsContext *ctx, const char *path)
437 {
438 errno = EPERM;
439 return -1;
440 }
441
442 static int synth_fsync(FsContext *ctx, int fid_type,
443 V9fsFidOpenState *fs, int datasync)
444 {
445 errno = ENOSYS;
446 return 0;
447 }
448
449 static int synth_statfs(FsContext *s, V9fsPath *fs_path,
450 struct statfs *stbuf)
451 {
452 stbuf->f_type = 0xABCD;
453 stbuf->f_bsize = 512;
454 stbuf->f_blocks = 0;
455 stbuf->f_files = synth_node_count;
456 #if !defined(CONFIG_DARWIN) && !defined(CONFIG_FREEBSD)
457 stbuf->f_namelen = NAME_MAX;
458 #endif
459 return 0;
460 }
461
462 static ssize_t synth_lgetxattr(FsContext *ctx, V9fsPath *path,
463 const char *name, void *value, size_t size)
464 {
465 errno = ENOTSUP;
466 return -1;
467 }
468
469 static ssize_t synth_llistxattr(FsContext *ctx, V9fsPath *path,
470 void *value, size_t size)
471 {
472 errno = ENOTSUP;
473 return -1;
474 }
475
476 static int synth_lsetxattr(FsContext *ctx, V9fsPath *path,
477 const char *name, void *value,
478 size_t size, int flags)
479 {
480 /* pretend it worked */
481 return 0;
482 }
483
484 static int synth_lremovexattr(FsContext *ctx,
485 V9fsPath *path, const char *name)
486 {
487 /* pretend it worked */
488 return 0;
489 }
490
491 static int synth_name_to_path(FsContext *ctx, V9fsPath *dir_path,
492 const char *name, V9fsPath *target)
493 {
494 V9fsSynthNode *node;
495 V9fsSynthNode *dir_node;
496
497 /* "." and ".." are not allowed */
498 if (!strcmp(name, ".") || !strcmp(name, "..")) {
499 errno = EINVAL;
500 return -1;
501
502 }
503 if (!dir_path) {
504 dir_node = &synth_root;
505 } else {
506 dir_node = *(V9fsSynthNode **)dir_path->data;
507 }
508 if (!strcmp(name, "/")) {
509 node = dir_node;
510 goto out;
511 }
512 /* search for the name in the children */
513 rcu_read_lock();
514 QLIST_FOREACH(node, &dir_node->child, sibling) {
515 if (!strcmp(node->name, name)) {
516 break;
517 }
518 }
519 rcu_read_unlock();
520
521 if (!node) {
522 errno = ENOENT;
523 return -1;
524 }
525 out:
526 /* Copy the node pointer to fid */
527 g_free(target->data);
528 target->data = g_memdup(&node, sizeof(void *));
529 target->size = sizeof(void *);
530 return 0;
531 }
532
533 static int synth_renameat(FsContext *ctx, V9fsPath *olddir,
534 const char *old_name, V9fsPath *newdir,
535 const char *new_name)
536 {
537 errno = EPERM;
538 return -1;
539 }
540
541 static int synth_unlinkat(FsContext *ctx, V9fsPath *dir,
542 const char *name, int flags)
543 {
544 errno = EPERM;
545 return -1;
546 }
547
548 static ssize_t v9fs_synth_qtest_write(void *buf, int len, off_t offset,
549 void *arg)
550 {
551 return 1;
552 }
553
554 static ssize_t v9fs_synth_qtest_flush_write(void *buf, int len, off_t offset,
555 void *arg)
556 {
557 bool should_block = !!*(uint8_t *)buf;
558
559 if (should_block) {
560 /* This will cause the server to call us again until we're cancelled */
561 errno = EINTR;
562 return -1;
563 }
564
565 return 1;
566 }
567
568 /* transmits internal xattr counter to client */
569 static ssize_t v9fs_synth_read_xattr_count(void *buf, int len, off_t offset,
570 void *arg)
571 {
572 FsContext *ctx = arg;
573 size_t local_count = ctx->xattr_fid_count;
574 if (len < (int)sizeof(size_t)) {
575 return -ENOSPC;
576 }
577 memcpy(buf, &local_count, sizeof(size_t));
578 return sizeof(size_t);
579 }
580
581 static int synth_init(FsContext *ctx, Error **errp)
582 {
583 QLIST_INIT(&synth_root.child);
584 qemu_mutex_init(&synth_mutex);
585
586 /* Add "." and ".." entries for root */
587 v9fs_add_dir_node(&synth_root, synth_root.attr->mode,
588 "..", synth_root.attr, synth_root.attr->inode);
589 v9fs_add_dir_node(&synth_root, synth_root.attr->mode,
590 ".", synth_root.attr, synth_root.attr->inode);
591
592 /* Mark the subsystem is ready for use */
593 synth_fs = 1;
594
595 if (qtest_enabled()) {
596 V9fsSynthNode *node = NULL;
597 int i, ret;
598
599 /* Directory hierarchy for WALK test */
600 for (i = 0; i < P9_MAXWELEM; i++) {
601 char *name = g_strdup_printf(QTEST_V9FS_SYNTH_WALK_FILE, i);
602
603 ret = qemu_v9fs_synth_mkdir(node, 0700, name, &node);
604 assert(!ret);
605 g_free(name);
606 }
607
608 /* File for LOPEN test */
609 ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_LOPEN_FILE,
610 NULL, NULL, ctx);
611 assert(!ret);
612
613 /* File for WRITE test */
614 ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_WRITE_FILE,
615 NULL, v9fs_synth_qtest_write, ctx);
616 assert(!ret);
617
618 /* File for FLUSH test */
619 ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_FLUSH_FILE,
620 NULL, v9fs_synth_qtest_flush_write,
621 ctx);
622 assert(!ret);
623
624 /* Directory for READDIR test */
625 {
626 V9fsSynthNode *dir = NULL;
627 ret = qemu_v9fs_synth_mkdir(
628 NULL, 0700, QTEST_V9FS_SYNTH_READDIR_DIR, &dir
629 );
630 assert(!ret);
631 for (i = 0; i < QTEST_V9FS_SYNTH_READDIR_NFILES; ++i) {
632 char *name = g_strdup_printf(
633 QTEST_V9FS_SYNTH_READDIR_FILE, i
634 );
635 ret = qemu_v9fs_synth_add_file(
636 dir, 0, name, NULL, NULL, ctx
637 );
638 assert(!ret);
639 g_free(name);
640 }
641 }
642
643 /* Directory for internal statistic queries */
644 {
645 V9fsSynthNode *stat_dir = NULL;
646 ret = qemu_v9fs_synth_mkdir(NULL, 0755, "stat", &stat_dir);
647 assert(!ret);
648
649 /* File for internal xattr count query */
650 ret = qemu_v9fs_synth_add_file(stat_dir, 0444, "xattr_count",
651 v9fs_synth_read_xattr_count,
652 NULL, ctx);
653 assert(!ret);
654 }
655 }
656
657 return 0;
658 }
659
660 static int synth_parse_opts(QemuOpts *opts, FsDriverEntry *fse, Error **errp)
661 {
662 uint64_t val = qemu_opt_get_number(opts, "max_xattr",
663 V9FS_MAX_XATTR_DEFAULT);
664 if (val > UINT32_MAX) {
665 error_setg(errp, "max_xattr value '%s' too large",
666 qemu_opt_get(opts, "max_xattr"));
667 return -1;
668 }
669 fse->max_xattr = val;
670
671 return 0;
672 }
673
674 static bool synth_has_valid_file_handle(int fid_type, V9fsFidOpenState *fs)
675 {
676 return false;
677 }
678
679 FileOperations synth_ops = {
680 .parse_opts = synth_parse_opts,
681 .init = synth_init,
682 .lstat = synth_lstat,
683 .readlink = synth_readlink,
684 .close = synth_close,
685 .closedir = synth_closedir,
686 .open = synth_open,
687 .opendir = synth_opendir,
688 .rewinddir = synth_rewinddir,
689 .telldir = synth_telldir,
690 .readdir = synth_readdir,
691 .seekdir = synth_seekdir,
692 .preadv = synth_preadv,
693 .pwritev = synth_pwritev,
694 .chmod = synth_chmod,
695 .mknod = synth_mknod,
696 .mkdir = synth_mkdir,
697 .fstat = synth_fstat,
698 .open2 = synth_open2,
699 .symlink = synth_symlink,
700 .link = synth_link,
701 .truncate = synth_truncate,
702 .rename = synth_rename,
703 .chown = synth_chown,
704 .utimensat = synth_utimensat,
705 .remove = synth_remove,
706 .fsync = synth_fsync,
707 .statfs = synth_statfs,
708 .lgetxattr = synth_lgetxattr,
709 .llistxattr = synth_llistxattr,
710 .lsetxattr = synth_lsetxattr,
711 .lremovexattr = synth_lremovexattr,
712 .name_to_path = synth_name_to_path,
713 .renameat = synth_renameat,
714 .unlinkat = synth_unlinkat,
715 .has_valid_file_handle = synth_has_valid_file_handle,
716 .ftruncate = synth_ftruncate,
717 .futimens = synth_futimens,
718 };