xref: /openbmc/qemu/hw/9pfs/9p-synth.c (revision 520e210c)
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 #include "qemu/osdep.h"
16 #include "9p.h"
17 #include "fsdev/qemu-fsdev.h"
18 #include "9p-synth.h"
19 #include "qemu/rcu.h"
20 #include "qemu/rcu_queue.h"
21 #include "qemu/cutils.h"
22 #include "sysemu/qtest.h"
23 
24 /* Root node for synth file system */
25 static V9fsSynthNode synth_root = {
26     .name = "/",
27     .actual_attr = {
28         .mode = 0555 | S_IFDIR,
29         .nlink = 1,
30     },
31     .attr = &synth_root.actual_attr,
32 };
33 
34 static QemuMutex  synth_mutex;
35 static int synth_node_count;
36 /* set to 1 when the synth fs is ready */
37 static int synth_fs;
38 
39 static 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     pstrcpy(node->name, sizeof(node->name), name);
63     QLIST_INSERT_HEAD_RCU(&parent->child, node, sibling);
64     return node;
65 }
66 
67 int 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 (!synth_fs) {
74         return EAGAIN;
75     }
76     if (!name || (strlen(name) >= NAME_MAX)) {
77         return EINVAL;
78     }
79     if (!parent) {
80         parent = &synth_root;
81     }
82     qemu_mutex_lock(&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, 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;
97 err_out:
98     qemu_mutex_unlock(&synth_mutex);
99     return ret;
100 }
101 
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)
105 {
106     int ret;
107     V9fsSynthNode *node, *tmp;
108 
109     if (!synth_fs) {
110         return EAGAIN;
111     }
112     if (!name || (strlen(name) >= NAME_MAX)) {
113         return EINVAL;
114     }
115     if (!parent) {
116         parent = &synth_root;
117     }
118 
119     qemu_mutex_lock(&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  = 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     ret = 0;
139 err_out:
140     qemu_mutex_unlock(&synth_mutex);
141     return ret;
142 }
143 
144 static void 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 
161 static int synth_lstat(FsContext *fs_ctx,
162                             V9fsPath *fs_path, struct stat *stbuf)
163 {
164     V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;
165 
166     synth_fill_statbuf(node, stbuf);
167     return 0;
168 }
169 
170 static int synth_fstat(FsContext *fs_ctx, int fid_type,
171                             V9fsFidOpenState *fs, struct stat *stbuf)
172 {
173     V9fsSynthOpenState *synth_open = fs->private;
174     synth_fill_statbuf(synth_open->node, stbuf);
175     return 0;
176 }
177 
178 static int 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 
191 static int 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 
202 static off_t synth_telldir(FsContext *ctx, V9fsFidOpenState *fs)
203 {
204     V9fsSynthOpenState *synth_open = fs->private;
205     return synth_open->offset;
206 }
207 
208 static void synth_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
209 {
210     V9fsSynthOpenState *synth_open = fs->private;
211     synth_open->offset = off;
212 }
213 
214 static void synth_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
215 {
216     synth_seekdir(ctx, fs, 0);
217 }
218 
219 static void 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 
227 static struct dirent *synth_get_dentry(V9fsSynthNode *dir,
228                                             struct dirent *entry, off_t off)
229 {
230     int i = 0;
231     V9fsSynthNode *node;
232 
233     rcu_read_lock();
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     rcu_read_unlock();
242     if (!node) {
243         /* end of directory */
244         return NULL;
245     }
246     synth_direntry(node, entry, off);
247     return entry;
248 }
249 
250 static struct dirent *synth_readdir(FsContext *ctx, V9fsFidOpenState *fs)
251 {
252     struct dirent *entry;
253     V9fsSynthOpenState *synth_open = fs->private;
254     V9fsSynthNode *node = synth_open->node;
255     entry = synth_get_dentry(node, &synth_open->dent, synth_open->offset);
256     if (entry) {
257         synth_open->offset++;
258     }
259     return entry;
260 }
261 
262 static int synth_open(FsContext *ctx, V9fsPath *fs_path,
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 
275 static int synth_open2(FsContext *fs_ctx, V9fsPath *dir_path,
276                             const char *name, int flags,
277                             FsCred *credp, V9fsFidOpenState *fs)
278 {
279     errno = ENOSYS;
280     return -1;
281 }
282 
283 static int synth_close(FsContext *ctx, V9fsFidOpenState *fs)
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 
294 static ssize_t synth_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
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 
318 static ssize_t synth_preadv(FsContext *ctx, V9fsFidOpenState *fs,
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 
342 static int synth_truncate(FsContext *ctx, V9fsPath *path, off_t offset)
343 {
344     errno = ENOSYS;
345     return -1;
346 }
347 
348 static int synth_chmod(FsContext *fs_ctx, V9fsPath *path, FsCred *credp)
349 {
350     errno = EPERM;
351     return -1;
352 }
353 
354 static int synth_mknod(FsContext *fs_ctx, V9fsPath *path,
355                        const char *buf, FsCred *credp)
356 {
357     errno = EPERM;
358     return -1;
359 }
360 
361 static int synth_mkdir(FsContext *fs_ctx, V9fsPath *path,
362                        const char *buf, FsCred *credp)
363 {
364     errno = EPERM;
365     return -1;
366 }
367 
368 static ssize_t synth_readlink(FsContext *fs_ctx, V9fsPath *path,
369                                    char *buf, size_t bufsz)
370 {
371     errno = ENOSYS;
372     return -1;
373 }
374 
375 static int synth_symlink(FsContext *fs_ctx, const char *oldpath,
376                               V9fsPath *newpath, const char *buf, FsCred *credp)
377 {
378     errno = EPERM;
379     return -1;
380 }
381 
382 static int synth_link(FsContext *fs_ctx, V9fsPath *oldpath,
383                            V9fsPath *newpath, const char *buf)
384 {
385     errno = EPERM;
386     return -1;
387 }
388 
389 static int synth_rename(FsContext *ctx, const char *oldpath,
390                              const char *newpath)
391 {
392     errno = EPERM;
393     return -1;
394 }
395 
396 static int synth_chown(FsContext *fs_ctx, V9fsPath *path, FsCred *credp)
397 {
398     errno = EPERM;
399     return -1;
400 }
401 
402 static int synth_utimensat(FsContext *fs_ctx, V9fsPath *path,
403                                 const struct timespec *buf)
404 {
405     errno = EPERM;
406     return 0;
407 }
408 
409 static int synth_remove(FsContext *ctx, const char *path)
410 {
411     errno = EPERM;
412     return -1;
413 }
414 
415 static int synth_fsync(FsContext *ctx, int fid_type,
416                             V9fsFidOpenState *fs, int datasync)
417 {
418     errno = ENOSYS;
419     return 0;
420 }
421 
422 static int synth_statfs(FsContext *s, V9fsPath *fs_path,
423                              struct statfs *stbuf)
424 {
425     stbuf->f_type = 0xABCD;
426     stbuf->f_bsize = 512;
427     stbuf->f_blocks = 0;
428     stbuf->f_files = synth_node_count;
429     stbuf->f_namelen = NAME_MAX;
430     return 0;
431 }
432 
433 static ssize_t synth_lgetxattr(FsContext *ctx, V9fsPath *path,
434                                     const char *name, void *value, size_t size)
435 {
436     errno = ENOTSUP;
437     return -1;
438 }
439 
440 static ssize_t synth_llistxattr(FsContext *ctx, V9fsPath *path,
441                                      void *value, size_t size)
442 {
443     errno = ENOTSUP;
444     return -1;
445 }
446 
447 static int synth_lsetxattr(FsContext *ctx, V9fsPath *path,
448                                 const char *name, void *value,
449                                 size_t size, int flags)
450 {
451     errno = ENOTSUP;
452     return -1;
453 }
454 
455 static int synth_lremovexattr(FsContext *ctx,
456                                    V9fsPath *path, const char *name)
457 {
458     errno = ENOTSUP;
459     return -1;
460 }
461 
462 static int synth_name_to_path(FsContext *ctx, V9fsPath *dir_path,
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) {
475         dir_node = &synth_root;
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 */
484     rcu_read_lock();
485     QLIST_FOREACH(node, &dir_node->child, sibling) {
486         if (!strcmp(node->name, name)) {
487             break;
488         }
489     }
490     rcu_read_unlock();
491 
492     if (!node) {
493         errno = ENOENT;
494         return -1;
495     }
496 out:
497     /* Copy the node pointer to fid */
498     g_free(target->data);
499     target->data = g_memdup(&node, sizeof(void *));
500     target->size = sizeof(void *);
501     return 0;
502 }
503 
504 static int synth_renameat(FsContext *ctx, V9fsPath *olddir,
505                                const char *old_name, V9fsPath *newdir,
506                                const char *new_name)
507 {
508     errno = EPERM;
509     return -1;
510 }
511 
512 static int synth_unlinkat(FsContext *ctx, V9fsPath *dir,
513                                const char *name, int flags)
514 {
515     errno = EPERM;
516     return -1;
517 }
518 
519 static ssize_t v9fs_synth_qtest_write(void *buf, int len, off_t offset,
520                                       void *arg)
521 {
522     return 1;
523 }
524 
525 static 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 
539 static int synth_init(FsContext *ctx, Error **errp)
540 {
541     QLIST_INIT(&synth_root.child);
542     qemu_mutex_init(&synth_mutex);
543 
544     /* Add "." and ".." entries for root */
545     v9fs_add_dir_node(&synth_root, synth_root.attr->mode,
546                       "..", synth_root.attr, synth_root.attr->inode);
547     v9fs_add_dir_node(&synth_root, synth_root.attr->mode,
548                       ".", synth_root.attr, synth_root.attr->inode);
549 
550     /* Mark the subsystem is ready for use */
551     synth_fs = 1;
552 
553     if (qtest_enabled()) {
554         V9fsSynthNode *node = NULL;
555         int i, ret;
556 
557         /* Directory hierarchy for WALK test */
558         for (i = 0; i < P9_MAXWELEM; i++) {
559             char *name = g_strdup_printf(QTEST_V9FS_SYNTH_WALK_FILE, i);
560 
561             ret = qemu_v9fs_synth_mkdir(node, 0700, name, &node);
562             assert(!ret);
563             g_free(name);
564         }
565 
566         /* File for LOPEN test */
567         ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_LOPEN_FILE,
568                                        NULL, NULL, ctx);
569         assert(!ret);
570 
571         /* File for WRITE test */
572         ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_WRITE_FILE,
573                                        NULL, v9fs_synth_qtest_write, ctx);
574         assert(!ret);
575 
576         /* File for FLUSH test */
577         ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_FLUSH_FILE,
578                                        NULL, v9fs_synth_qtest_flush_write,
579                                        ctx);
580         assert(!ret);
581     }
582 
583     return 0;
584 }
585 
586 FileOperations synth_ops = {
587     .init         = synth_init,
588     .lstat        = synth_lstat,
589     .readlink     = synth_readlink,
590     .close        = synth_close,
591     .closedir     = synth_closedir,
592     .open         = synth_open,
593     .opendir      = synth_opendir,
594     .rewinddir    = synth_rewinddir,
595     .telldir      = synth_telldir,
596     .readdir      = synth_readdir,
597     .seekdir      = synth_seekdir,
598     .preadv       = synth_preadv,
599     .pwritev      = synth_pwritev,
600     .chmod        = synth_chmod,
601     .mknod        = synth_mknod,
602     .mkdir        = synth_mkdir,
603     .fstat        = synth_fstat,
604     .open2        = synth_open2,
605     .symlink      = synth_symlink,
606     .link         = synth_link,
607     .truncate     = synth_truncate,
608     .rename       = synth_rename,
609     .chown        = synth_chown,
610     .utimensat    = synth_utimensat,
611     .remove       = synth_remove,
612     .fsync        = synth_fsync,
613     .statfs       = synth_statfs,
614     .lgetxattr    = synth_lgetxattr,
615     .llistxattr   = synth_llistxattr,
616     .lsetxattr    = synth_lsetxattr,
617     .lremovexattr = synth_lremovexattr,
618     .name_to_path = synth_name_to_path,
619     .renameat     = synth_renameat,
620     .unlinkat     = synth_unlinkat,
621 };
622