xref: /openbmc/qemu/hw/9pfs/9p-synth.c (revision b8012ecf)
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 "sysemu/qtest.h"
28 
29 /* Root node for synth file system */
30 static V9fsSynthNode synth_root = {
31     .name = "/",
32     .actual_attr = {
33         .mode = 0555 | S_IFDIR,
34         .nlink = 1,
35     },
36     .attr = &synth_root.actual_attr,
37 };
38 
39 static QemuMutex  synth_mutex;
40 static int synth_node_count;
41 /* set to 1 when the synth fs is ready */
42 static int synth_fs;
43 
44 static V9fsSynthNode *v9fs_add_dir_node(V9fsSynthNode *parent, int mode,
45                                         const char *name,
46                                         V9fsSynthNodeAttr *attr, int inode)
47 {
48     V9fsSynthNode *node;
49 
50     /* Add directory type and remove write bits */
51     mode = ((mode & 0777) | S_IFDIR) & ~(S_IWUSR | S_IWGRP | S_IWOTH);
52     node = g_malloc0(sizeof(V9fsSynthNode));
53     if (attr) {
54         /* We are adding .. or . entries */
55         node->attr = attr;
56         node->attr->nlink++;
57     } else {
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;
65     }
66     node->private = node;
67     pstrcpy(node->name, sizeof(node->name), name);
68     QLIST_INSERT_HEAD_RCU(&parent->child, node, sibling);
69     return node;
70 }
71 
72 int qemu_v9fs_synth_mkdir(V9fsSynthNode *parent, int mode,
73                           const char *name, V9fsSynthNode **result)
74 {
75     int ret;
76     V9fsSynthNode *node, *tmp;
77 
78     if (!synth_fs) {
79         return EAGAIN;
80     }
81     if (!name || (strlen(name) >= NAME_MAX)) {
82         return EINVAL;
83     }
84     if (!parent) {
85         parent = &synth_root;
86     }
87     QEMU_LOCK_GUARD(&synth_mutex);
88     QLIST_FOREACH(tmp, &parent->child, sibling) {
89         if (!strcmp(tmp->name, name)) {
90             ret = EEXIST;
91             return ret;
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     ret = 0;
102     return ret;
103 }
104 
105 int qemu_v9fs_synth_add_file(V9fsSynthNode *parent, int mode,
106                              const char *name, v9fs_synth_read read,
107                              v9fs_synth_write write, void *arg)
108 {
109     int ret;
110     V9fsSynthNode *node, *tmp;
111 
112     if (!synth_fs) {
113         return EAGAIN;
114     }
115     if (!name || (strlen(name) >= NAME_MAX)) {
116         return EINVAL;
117     }
118     if (!parent) {
119         parent = &synth_root;
120     }
121 
122     QEMU_LOCK_GUARD(&synth_mutex);
123     QLIST_FOREACH(tmp, &parent->child, sibling) {
124         if (!strcmp(tmp->name, name)) {
125             ret = EEXIST;
126             return ret;
127         }
128     }
129     /* Add file type and remove write bits */
130     mode = ((mode & 0777) | S_IFREG);
131     node = g_malloc0(sizeof(V9fsSynthNode));
132     node->attr         = &node->actual_attr;
133     node->attr->inode  = synth_node_count++;
134     node->attr->nlink  = 1;
135     node->attr->read   = read;
136     node->attr->write  = write;
137     node->attr->mode   = mode;
138     node->private      = arg;
139     pstrcpy(node->name, sizeof(node->name), name);
140     QLIST_INSERT_HEAD_RCU(&parent->child, node, sibling);
141     ret = 0;
142     return ret;
143 }
144 
145 static void synth_fill_statbuf(V9fsSynthNode *node, struct stat *stbuf)
146 {
147     stbuf->st_dev = 0;
148     stbuf->st_ino = node->attr->inode;
149     stbuf->st_mode = node->attr->mode;
150     stbuf->st_nlink = node->attr->nlink;
151     stbuf->st_uid = 0;
152     stbuf->st_gid = 0;
153     stbuf->st_rdev = 0;
154     stbuf->st_size = 0;
155     stbuf->st_blksize = 0;
156     stbuf->st_blocks = 0;
157     stbuf->st_atime = 0;
158     stbuf->st_mtime = 0;
159     stbuf->st_ctime = 0;
160 }
161 
162 static int synth_lstat(FsContext *fs_ctx,
163                             V9fsPath *fs_path, struct stat *stbuf)
164 {
165     V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;
166 
167     synth_fill_statbuf(node, stbuf);
168     return 0;
169 }
170 
171 static int synth_fstat(FsContext *fs_ctx, int fid_type,
172                             V9fsFidOpenState *fs, struct stat *stbuf)
173 {
174     V9fsSynthOpenState *synth_open = fs->private;
175     synth_fill_statbuf(synth_open->node, stbuf);
176     return 0;
177 }
178 
179 static int synth_opendir(FsContext *ctx,
180                              V9fsPath *fs_path, V9fsFidOpenState *fs)
181 {
182     V9fsSynthOpenState *synth_open;
183     V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;
184 
185     /*
186      * V9fsSynthOpenState contains 'struct dirent' which have OS-specific
187      * properties, thus it's zero cleared on allocation here and below
188      * in synth_open.
189      */
190     synth_open = g_new0(V9fsSynthOpenState, 1);
191     synth_open->node = node;
192     node->open_count++;
193     fs->private = synth_open;
194     return 0;
195 }
196 
197 static int synth_closedir(FsContext *ctx, V9fsFidOpenState *fs)
198 {
199     V9fsSynthOpenState *synth_open = fs->private;
200     V9fsSynthNode *node = synth_open->node;
201 
202     node->open_count--;
203     g_free(synth_open);
204     fs->private = NULL;
205     return 0;
206 }
207 
208 static off_t synth_telldir(FsContext *ctx, V9fsFidOpenState *fs)
209 {
210     V9fsSynthOpenState *synth_open = fs->private;
211     return synth_open->offset;
212 }
213 
214 static void synth_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
215 {
216     V9fsSynthOpenState *synth_open = fs->private;
217     synth_open->offset = off;
218 }
219 
220 static void synth_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
221 {
222     synth_seekdir(ctx, fs, 0);
223 }
224 
225 static void synth_direntry(V9fsSynthNode *node,
226                                 struct dirent *entry, off_t off)
227 {
228     size_t sz = strlen(node->name) + 1;
229     /*
230      * 'entry' is always inside of V9fsSynthOpenState which have NAME_MAX
231      * back padding. Ensure we do not overflow it.
232      */
233     g_assert(sizeof(struct dirent) + NAME_MAX >=
234              offsetof(struct dirent, d_name) + sz);
235     memcpy(entry->d_name, node->name, sz);
236     entry->d_ino = node->attr->inode;
237     entry->d_off = off + 1;
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_chmod(FsContext *fs_ctx, V9fsPath *path, FsCred *credp)
362 {
363     errno = EPERM;
364     return -1;
365 }
366 
367 static int synth_mknod(FsContext *fs_ctx, V9fsPath *path,
368                        const char *buf, FsCred *credp)
369 {
370     errno = EPERM;
371     return -1;
372 }
373 
374 static int synth_mkdir(FsContext *fs_ctx, V9fsPath *path,
375                        const char *buf, FsCred *credp)
376 {
377     errno = EPERM;
378     return -1;
379 }
380 
381 static ssize_t synth_readlink(FsContext *fs_ctx, V9fsPath *path,
382                                    char *buf, size_t bufsz)
383 {
384     errno = ENOSYS;
385     return -1;
386 }
387 
388 static int synth_symlink(FsContext *fs_ctx, const char *oldpath,
389                               V9fsPath *newpath, const char *buf, FsCred *credp)
390 {
391     errno = EPERM;
392     return -1;
393 }
394 
395 static int synth_link(FsContext *fs_ctx, V9fsPath *oldpath,
396                            V9fsPath *newpath, const char *buf)
397 {
398     errno = EPERM;
399     return -1;
400 }
401 
402 static int synth_rename(FsContext *ctx, const char *oldpath,
403                              const char *newpath)
404 {
405     errno = EPERM;
406     return -1;
407 }
408 
409 static int synth_chown(FsContext *fs_ctx, V9fsPath *path, FsCred *credp)
410 {
411     errno = EPERM;
412     return -1;
413 }
414 
415 static int synth_utimensat(FsContext *fs_ctx, V9fsPath *path,
416                                 const struct timespec *buf)
417 {
418     errno = EPERM;
419     return 0;
420 }
421 
422 static int synth_remove(FsContext *ctx, const char *path)
423 {
424     errno = EPERM;
425     return -1;
426 }
427 
428 static int synth_fsync(FsContext *ctx, int fid_type,
429                             V9fsFidOpenState *fs, int datasync)
430 {
431     errno = ENOSYS;
432     return 0;
433 }
434 
435 static int synth_statfs(FsContext *s, V9fsPath *fs_path,
436                              struct statfs *stbuf)
437 {
438     stbuf->f_type = 0xABCD;
439     stbuf->f_bsize = 512;
440     stbuf->f_blocks = 0;
441     stbuf->f_files = synth_node_count;
442     stbuf->f_namelen = NAME_MAX;
443     return 0;
444 }
445 
446 static ssize_t synth_lgetxattr(FsContext *ctx, V9fsPath *path,
447                                     const char *name, void *value, size_t size)
448 {
449     errno = ENOTSUP;
450     return -1;
451 }
452 
453 static ssize_t synth_llistxattr(FsContext *ctx, V9fsPath *path,
454                                      void *value, size_t size)
455 {
456     errno = ENOTSUP;
457     return -1;
458 }
459 
460 static int synth_lsetxattr(FsContext *ctx, V9fsPath *path,
461                                 const char *name, void *value,
462                                 size_t size, int flags)
463 {
464     errno = ENOTSUP;
465     return -1;
466 }
467 
468 static int synth_lremovexattr(FsContext *ctx,
469                                    V9fsPath *path, const char *name)
470 {
471     errno = ENOTSUP;
472     return -1;
473 }
474 
475 static int synth_name_to_path(FsContext *ctx, V9fsPath *dir_path,
476                                    const char *name, V9fsPath *target)
477 {
478     V9fsSynthNode *node;
479     V9fsSynthNode *dir_node;
480 
481     /* "." and ".." are not allowed */
482     if (!strcmp(name, ".") || !strcmp(name, "..")) {
483         errno = EINVAL;
484         return -1;
485 
486     }
487     if (!dir_path) {
488         dir_node = &synth_root;
489     } else {
490         dir_node = *(V9fsSynthNode **)dir_path->data;
491     }
492     if (!strcmp(name, "/")) {
493         node = dir_node;
494         goto out;
495     }
496     /* search for the name in the childern */
497     rcu_read_lock();
498     QLIST_FOREACH(node, &dir_node->child, sibling) {
499         if (!strcmp(node->name, name)) {
500             break;
501         }
502     }
503     rcu_read_unlock();
504 
505     if (!node) {
506         errno = ENOENT;
507         return -1;
508     }
509 out:
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 *);
514     return 0;
515 }
516 
517 static int synth_renameat(FsContext *ctx, V9fsPath *olddir,
518                                const char *old_name, V9fsPath *newdir,
519                                const char *new_name)
520 {
521     errno = EPERM;
522     return -1;
523 }
524 
525 static int synth_unlinkat(FsContext *ctx, V9fsPath *dir,
526                                const char *name, int flags)
527 {
528     errno = EPERM;
529     return -1;
530 }
531 
532 static ssize_t v9fs_synth_qtest_write(void *buf, int len, off_t offset,
533                                       void *arg)
534 {
535     return 1;
536 }
537 
538 static ssize_t v9fs_synth_qtest_flush_write(void *buf, int len, off_t offset,
539                                             void *arg)
540 {
541     bool should_block = !!*(uint8_t *)buf;
542 
543     if (should_block) {
544         /* This will cause the server to call us again until we're cancelled */
545         errno = EINTR;
546         return -1;
547     }
548 
549     return 1;
550 }
551 
552 static int synth_init(FsContext *ctx, Error **errp)
553 {
554     QLIST_INIT(&synth_root.child);
555     qemu_mutex_init(&synth_mutex);
556 
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);
562 
563     /* Mark the subsystem is ready for use */
564     synth_fs = 1;
565 
566     if (qtest_enabled()) {
567         V9fsSynthNode *node = NULL;
568         int i, ret;
569 
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);
573 
574             ret = qemu_v9fs_synth_mkdir(node, 0700, name, &node);
575             assert(!ret);
576             g_free(name);
577         }
578 
579         /* File for LOPEN test */
580         ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_LOPEN_FILE,
581                                        NULL, NULL, ctx);
582         assert(!ret);
583 
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);
587         assert(!ret);
588 
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,
592                                        ctx);
593         assert(!ret);
594 
595         /* Directory for READDIR test */
596         {
597             V9fsSynthNode *dir = NULL;
598             ret = qemu_v9fs_synth_mkdir(
599                 NULL, 0700, QTEST_V9FS_SYNTH_READDIR_DIR, &dir
600             );
601             assert(!ret);
602             for (i = 0; i < QTEST_V9FS_SYNTH_READDIR_NFILES; ++i) {
603                 char *name = g_strdup_printf(
604                     QTEST_V9FS_SYNTH_READDIR_FILE, i
605                 );
606                 ret = qemu_v9fs_synth_add_file(
607                     dir, 0, name, NULL, NULL, ctx
608                 );
609                 assert(!ret);
610                 g_free(name);
611             }
612         }
613     }
614 
615     return 0;
616 }
617 
618 FileOperations synth_ops = {
619     .init         = synth_init,
620     .lstat        = synth_lstat,
621     .readlink     = synth_readlink,
622     .close        = synth_close,
623     .closedir     = synth_closedir,
624     .open         = synth_open,
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,
638     .link         = synth_link,
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,
653 };
654