xref: /openbmc/qemu/hw/9pfs/9p-synth.c (revision 6f569084277dc7ae351141cfa6ad3649e92a1390)
1364031f1SWei Liu /*
2aae91ad9SGreg Kurz  * 9p synthetic file system support
3364031f1SWei Liu  *
4364031f1SWei Liu  * Copyright IBM, Corp. 2011
5364031f1SWei Liu  *
6364031f1SWei Liu  * Authors:
7364031f1SWei Liu  *  Malahal Naineni <malahal@us.ibm.com>
8364031f1SWei Liu  *  Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
9364031f1SWei Liu  *
10364031f1SWei Liu  * This work is licensed under the terms of the GNU GPL, version 2.  See
11364031f1SWei Liu  * the COPYING file in the top-level directory.
12364031f1SWei Liu  *
13364031f1SWei Liu  */
14364031f1SWei Liu 
15*6f569084SChristian Schoenebeck /*
16*6f569084SChristian Schoenebeck  * Not so fast! You might want to read the 9p developer docs first:
17*6f569084SChristian Schoenebeck  * https://wiki.qemu.org/Documentation/9p
18*6f569084SChristian Schoenebeck  */
19*6f569084SChristian Schoenebeck 
20fbc04127SPeter Maydell #include "qemu/osdep.h"
21ebe74f8bSWei Liu #include "9p.h"
22364031f1SWei Liu #include "fsdev/qemu-fsdev.h"
23364031f1SWei Liu #include "9p-synth.h"
24364031f1SWei Liu #include "qemu/rcu.h"
25364031f1SWei Liu #include "qemu/rcu_queue.h"
26f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
272893ddd5SGreg Kurz #include "sysemu/qtest.h"
28364031f1SWei Liu 
29364031f1SWei Liu /* Root node for synth file system */
30b05528b5SGreg Kurz static V9fsSynthNode synth_root = {
31364031f1SWei Liu     .name = "/",
32364031f1SWei Liu     .actual_attr = {
33364031f1SWei Liu         .mode = 0555 | S_IFDIR,
34364031f1SWei Liu         .nlink = 1,
35364031f1SWei Liu     },
36b05528b5SGreg Kurz     .attr = &synth_root.actual_attr,
37364031f1SWei Liu };
38364031f1SWei Liu 
39b05528b5SGreg Kurz static QemuMutex  synth_mutex;
40b05528b5SGreg Kurz static int synth_node_count;
41364031f1SWei Liu /* set to 1 when the synth fs is ready */
42b05528b5SGreg Kurz static int synth_fs;
43364031f1SWei Liu 
44364031f1SWei Liu static V9fsSynthNode *v9fs_add_dir_node(V9fsSynthNode *parent, int mode,
45364031f1SWei Liu                                         const char *name,
46364031f1SWei Liu                                         V9fsSynthNodeAttr *attr, int inode)
47364031f1SWei Liu {
48364031f1SWei Liu     V9fsSynthNode *node;
49364031f1SWei Liu 
50364031f1SWei Liu     /* Add directory type and remove write bits */
51364031f1SWei Liu     mode = ((mode & 0777) | S_IFDIR) & ~(S_IWUSR | S_IWGRP | S_IWOTH);
52364031f1SWei Liu     node = g_malloc0(sizeof(V9fsSynthNode));
53364031f1SWei Liu     if (attr) {
54364031f1SWei Liu         /* We are adding .. or . entries */
55364031f1SWei Liu         node->attr = attr;
56364031f1SWei Liu         node->attr->nlink++;
57364031f1SWei Liu     } else {
58364031f1SWei Liu         node->attr = &node->actual_attr;
59364031f1SWei Liu         node->attr->inode = inode;
60364031f1SWei Liu         node->attr->nlink = 1;
61364031f1SWei Liu         /* We don't allow write to directories */
62364031f1SWei Liu         node->attr->mode   = mode;
63364031f1SWei Liu         node->attr->write = NULL;
64364031f1SWei Liu         node->attr->read  = NULL;
65364031f1SWei Liu     }
66364031f1SWei Liu     node->private = node;
67364031f1SWei Liu     pstrcpy(node->name, sizeof(node->name), name);
68364031f1SWei Liu     QLIST_INSERT_HEAD_RCU(&parent->child, node, sibling);
69364031f1SWei Liu     return node;
70364031f1SWei Liu }
71364031f1SWei Liu 
72364031f1SWei Liu int qemu_v9fs_synth_mkdir(V9fsSynthNode *parent, int mode,
73364031f1SWei Liu                           const char *name, V9fsSynthNode **result)
74364031f1SWei Liu {
75364031f1SWei Liu     int ret;
76364031f1SWei Liu     V9fsSynthNode *node, *tmp;
77364031f1SWei Liu 
78b05528b5SGreg Kurz     if (!synth_fs) {
79364031f1SWei Liu         return EAGAIN;
80364031f1SWei Liu     }
81364031f1SWei Liu     if (!name || (strlen(name) >= NAME_MAX)) {
82364031f1SWei Liu         return EINVAL;
83364031f1SWei Liu     }
84364031f1SWei Liu     if (!parent) {
85b05528b5SGreg Kurz         parent = &synth_root;
86364031f1SWei Liu     }
87e4fd889fSMahmoud Mandour     QEMU_LOCK_GUARD(&synth_mutex);
88364031f1SWei Liu     QLIST_FOREACH(tmp, &parent->child, sibling) {
89364031f1SWei Liu         if (!strcmp(tmp->name, name)) {
90364031f1SWei Liu             ret = EEXIST;
91e4fd889fSMahmoud Mandour             return ret;
92364031f1SWei Liu         }
93364031f1SWei Liu     }
94364031f1SWei Liu     /* Add the name */
95b05528b5SGreg Kurz     node = v9fs_add_dir_node(parent, mode, name, NULL, synth_node_count++);
96364031f1SWei Liu     v9fs_add_dir_node(node, parent->attr->mode, "..",
97364031f1SWei Liu                       parent->attr, parent->attr->inode);
98364031f1SWei Liu     v9fs_add_dir_node(node, node->attr->mode, ".",
99364031f1SWei Liu                       node->attr, node->attr->inode);
100364031f1SWei Liu     *result = node;
101364031f1SWei Liu     ret = 0;
102364031f1SWei Liu     return ret;
103364031f1SWei Liu }
104364031f1SWei Liu 
105364031f1SWei Liu int qemu_v9fs_synth_add_file(V9fsSynthNode *parent, int mode,
106364031f1SWei Liu                              const char *name, v9fs_synth_read read,
107364031f1SWei Liu                              v9fs_synth_write write, void *arg)
108364031f1SWei Liu {
109364031f1SWei Liu     int ret;
110364031f1SWei Liu     V9fsSynthNode *node, *tmp;
111364031f1SWei Liu 
112b05528b5SGreg Kurz     if (!synth_fs) {
113364031f1SWei Liu         return EAGAIN;
114364031f1SWei Liu     }
115364031f1SWei Liu     if (!name || (strlen(name) >= NAME_MAX)) {
116364031f1SWei Liu         return EINVAL;
117364031f1SWei Liu     }
118364031f1SWei Liu     if (!parent) {
119b05528b5SGreg Kurz         parent = &synth_root;
120364031f1SWei Liu     }
121364031f1SWei Liu 
122e4fd889fSMahmoud Mandour     QEMU_LOCK_GUARD(&synth_mutex);
123364031f1SWei Liu     QLIST_FOREACH(tmp, &parent->child, sibling) {
124364031f1SWei Liu         if (!strcmp(tmp->name, name)) {
125364031f1SWei Liu             ret = EEXIST;
126e4fd889fSMahmoud Mandour             return ret;
127364031f1SWei Liu         }
128364031f1SWei Liu     }
129364031f1SWei Liu     /* Add file type and remove write bits */
130364031f1SWei Liu     mode = ((mode & 0777) | S_IFREG);
131364031f1SWei Liu     node = g_malloc0(sizeof(V9fsSynthNode));
132364031f1SWei Liu     node->attr         = &node->actual_attr;
133b05528b5SGreg Kurz     node->attr->inode  = synth_node_count++;
134364031f1SWei Liu     node->attr->nlink  = 1;
135364031f1SWei Liu     node->attr->read   = read;
136364031f1SWei Liu     node->attr->write  = write;
137364031f1SWei Liu     node->attr->mode   = mode;
138364031f1SWei Liu     node->private      = arg;
139364031f1SWei Liu     pstrcpy(node->name, sizeof(node->name), name);
140364031f1SWei Liu     QLIST_INSERT_HEAD_RCU(&parent->child, node, sibling);
141364031f1SWei Liu     ret = 0;
142364031f1SWei Liu     return ret;
143364031f1SWei Liu }
144364031f1SWei Liu 
145b05528b5SGreg Kurz static void synth_fill_statbuf(V9fsSynthNode *node, struct stat *stbuf)
146364031f1SWei Liu {
147364031f1SWei Liu     stbuf->st_dev = 0;
148364031f1SWei Liu     stbuf->st_ino = node->attr->inode;
149364031f1SWei Liu     stbuf->st_mode = node->attr->mode;
150364031f1SWei Liu     stbuf->st_nlink = node->attr->nlink;
151364031f1SWei Liu     stbuf->st_uid = 0;
152364031f1SWei Liu     stbuf->st_gid = 0;
153364031f1SWei Liu     stbuf->st_rdev = 0;
154364031f1SWei Liu     stbuf->st_size = 0;
155364031f1SWei Liu     stbuf->st_blksize = 0;
156364031f1SWei Liu     stbuf->st_blocks = 0;
157364031f1SWei Liu     stbuf->st_atime = 0;
158364031f1SWei Liu     stbuf->st_mtime = 0;
159364031f1SWei Liu     stbuf->st_ctime = 0;
160364031f1SWei Liu }
161364031f1SWei Liu 
162b05528b5SGreg Kurz static int synth_lstat(FsContext *fs_ctx,
163364031f1SWei Liu                             V9fsPath *fs_path, struct stat *stbuf)
164364031f1SWei Liu {
165364031f1SWei Liu     V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;
166364031f1SWei Liu 
167b05528b5SGreg Kurz     synth_fill_statbuf(node, stbuf);
168364031f1SWei Liu     return 0;
169364031f1SWei Liu }
170364031f1SWei Liu 
171b05528b5SGreg Kurz static int synth_fstat(FsContext *fs_ctx, int fid_type,
172364031f1SWei Liu                             V9fsFidOpenState *fs, struct stat *stbuf)
173364031f1SWei Liu {
174364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
175b05528b5SGreg Kurz     synth_fill_statbuf(synth_open->node, stbuf);
176364031f1SWei Liu     return 0;
177364031f1SWei Liu }
178364031f1SWei Liu 
179b05528b5SGreg Kurz static int synth_opendir(FsContext *ctx,
180364031f1SWei Liu                              V9fsPath *fs_path, V9fsFidOpenState *fs)
181364031f1SWei Liu {
182364031f1SWei Liu     V9fsSynthOpenState *synth_open;
183364031f1SWei Liu     V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;
184364031f1SWei Liu 
185364031f1SWei Liu     synth_open = g_malloc(sizeof(*synth_open));
186364031f1SWei Liu     synth_open->node = node;
187364031f1SWei Liu     node->open_count++;
188364031f1SWei Liu     fs->private = synth_open;
189364031f1SWei Liu     return 0;
190364031f1SWei Liu }
191364031f1SWei Liu 
192b05528b5SGreg Kurz static int synth_closedir(FsContext *ctx, V9fsFidOpenState *fs)
193364031f1SWei Liu {
194364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
195364031f1SWei Liu     V9fsSynthNode *node = synth_open->node;
196364031f1SWei Liu 
197364031f1SWei Liu     node->open_count--;
198364031f1SWei Liu     g_free(synth_open);
199364031f1SWei Liu     fs->private = NULL;
200364031f1SWei Liu     return 0;
201364031f1SWei Liu }
202364031f1SWei Liu 
203b05528b5SGreg Kurz static off_t synth_telldir(FsContext *ctx, V9fsFidOpenState *fs)
204364031f1SWei Liu {
205364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
206364031f1SWei Liu     return synth_open->offset;
207364031f1SWei Liu }
208364031f1SWei Liu 
209b05528b5SGreg Kurz static void synth_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
210364031f1SWei Liu {
211364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
212364031f1SWei Liu     synth_open->offset = off;
213364031f1SWei Liu }
214364031f1SWei Liu 
215b05528b5SGreg Kurz static void synth_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
216364031f1SWei Liu {
217b05528b5SGreg Kurz     synth_seekdir(ctx, fs, 0);
218364031f1SWei Liu }
219364031f1SWei Liu 
220b05528b5SGreg Kurz static void synth_direntry(V9fsSynthNode *node,
221364031f1SWei Liu                                 struct dirent *entry, off_t off)
222364031f1SWei Liu {
223364031f1SWei Liu     strcpy(entry->d_name, node->name);
224364031f1SWei Liu     entry->d_ino = node->attr->inode;
225364031f1SWei Liu     entry->d_off = off + 1;
226364031f1SWei Liu }
227364031f1SWei Liu 
228b05528b5SGreg Kurz static struct dirent *synth_get_dentry(V9fsSynthNode *dir,
229635324e8SGreg Kurz                                             struct dirent *entry, off_t off)
230364031f1SWei Liu {
231364031f1SWei Liu     int i = 0;
232364031f1SWei Liu     V9fsSynthNode *node;
233364031f1SWei Liu 
234364031f1SWei Liu     rcu_read_lock();
235364031f1SWei Liu     QLIST_FOREACH(node, &dir->child, sibling) {
236364031f1SWei Liu         /* This is the off child of the directory */
237364031f1SWei Liu         if (i == off) {
238364031f1SWei Liu             break;
239364031f1SWei Liu         }
240364031f1SWei Liu         i++;
241364031f1SWei Liu     }
242364031f1SWei Liu     rcu_read_unlock();
243364031f1SWei Liu     if (!node) {
244364031f1SWei Liu         /* end of directory */
245635324e8SGreg Kurz         return NULL;
246364031f1SWei Liu     }
247b05528b5SGreg Kurz     synth_direntry(node, entry, off);
248635324e8SGreg Kurz     return entry;
249364031f1SWei Liu }
250364031f1SWei Liu 
251b05528b5SGreg Kurz static struct dirent *synth_readdir(FsContext *ctx, V9fsFidOpenState *fs)
252364031f1SWei Liu {
253635324e8SGreg Kurz     struct dirent *entry;
254364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
255364031f1SWei Liu     V9fsSynthNode *node = synth_open->node;
256b05528b5SGreg Kurz     entry = synth_get_dentry(node, &synth_open->dent, synth_open->offset);
257635324e8SGreg Kurz     if (entry) {
258364031f1SWei Liu         synth_open->offset++;
259364031f1SWei Liu     }
260635324e8SGreg Kurz     return entry;
261364031f1SWei Liu }
262364031f1SWei Liu 
263b05528b5SGreg Kurz static int synth_open(FsContext *ctx, V9fsPath *fs_path,
264364031f1SWei Liu                            int flags, V9fsFidOpenState *fs)
265364031f1SWei Liu {
266364031f1SWei Liu     V9fsSynthOpenState *synth_open;
267364031f1SWei Liu     V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;
268364031f1SWei Liu 
269364031f1SWei Liu     synth_open = g_malloc(sizeof(*synth_open));
270364031f1SWei Liu     synth_open->node = node;
271364031f1SWei Liu     node->open_count++;
272364031f1SWei Liu     fs->private = synth_open;
273364031f1SWei Liu     return 0;
274364031f1SWei Liu }
275364031f1SWei Liu 
276b05528b5SGreg Kurz static int synth_open2(FsContext *fs_ctx, V9fsPath *dir_path,
277364031f1SWei Liu                             const char *name, int flags,
278364031f1SWei Liu                             FsCred *credp, V9fsFidOpenState *fs)
279364031f1SWei Liu {
280364031f1SWei Liu     errno = ENOSYS;
281364031f1SWei Liu     return -1;
282364031f1SWei Liu }
283364031f1SWei Liu 
284b05528b5SGreg Kurz static int synth_close(FsContext *ctx, V9fsFidOpenState *fs)
285364031f1SWei Liu {
286364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
287364031f1SWei Liu     V9fsSynthNode *node = synth_open->node;
288364031f1SWei Liu 
289364031f1SWei Liu     node->open_count--;
290364031f1SWei Liu     g_free(synth_open);
291364031f1SWei Liu     fs->private = NULL;
292364031f1SWei Liu     return 0;
293364031f1SWei Liu }
294364031f1SWei Liu 
295b05528b5SGreg Kurz static ssize_t synth_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
296364031f1SWei Liu                                   const struct iovec *iov,
297364031f1SWei Liu                                   int iovcnt, off_t offset)
298364031f1SWei Liu {
299364031f1SWei Liu     int i, count = 0, wcount;
300364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
301364031f1SWei Liu     V9fsSynthNode *node = synth_open->node;
302364031f1SWei Liu     if (!node->attr->write) {
303364031f1SWei Liu         errno = EPERM;
304364031f1SWei Liu         return -1;
305364031f1SWei Liu     }
306364031f1SWei Liu     for (i = 0; i < iovcnt; i++) {
307364031f1SWei Liu         wcount = node->attr->write(iov[i].iov_base, iov[i].iov_len,
308364031f1SWei Liu                                    offset, node->private);
309364031f1SWei Liu         offset += wcount;
310364031f1SWei Liu         count  += wcount;
311364031f1SWei Liu         /* If we wrote less than requested. we are done */
312364031f1SWei Liu         if (wcount < iov[i].iov_len) {
313364031f1SWei Liu             break;
314364031f1SWei Liu         }
315364031f1SWei Liu     }
316364031f1SWei Liu     return count;
317364031f1SWei Liu }
318364031f1SWei Liu 
319b05528b5SGreg Kurz static ssize_t synth_preadv(FsContext *ctx, V9fsFidOpenState *fs,
320364031f1SWei Liu                                  const struct iovec *iov,
321364031f1SWei Liu                                  int iovcnt, off_t offset)
322364031f1SWei Liu {
323364031f1SWei Liu     int i, count = 0, rcount;
324364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
325364031f1SWei Liu     V9fsSynthNode *node = synth_open->node;
326364031f1SWei Liu     if (!node->attr->read) {
327364031f1SWei Liu         errno = EPERM;
328364031f1SWei Liu         return -1;
329364031f1SWei Liu     }
330364031f1SWei Liu     for (i = 0; i < iovcnt; i++) {
331364031f1SWei Liu         rcount = node->attr->read(iov[i].iov_base, iov[i].iov_len,
332364031f1SWei Liu                                   offset, node->private);
333364031f1SWei Liu         offset += rcount;
334364031f1SWei Liu         count  += rcount;
335364031f1SWei Liu         /* If we read less than requested. we are done */
336364031f1SWei Liu         if (rcount < iov[i].iov_len) {
337364031f1SWei Liu             break;
338364031f1SWei Liu         }
339364031f1SWei Liu     }
340364031f1SWei Liu     return count;
341364031f1SWei Liu }
342364031f1SWei Liu 
343b05528b5SGreg Kurz static int synth_truncate(FsContext *ctx, V9fsPath *path, off_t offset)
344364031f1SWei Liu {
345364031f1SWei Liu     errno = ENOSYS;
346364031f1SWei Liu     return -1;
347364031f1SWei Liu }
348364031f1SWei Liu 
349b05528b5SGreg Kurz static int synth_chmod(FsContext *fs_ctx, V9fsPath *path, FsCred *credp)
350364031f1SWei Liu {
351364031f1SWei Liu     errno = EPERM;
352364031f1SWei Liu     return -1;
353364031f1SWei Liu }
354364031f1SWei Liu 
355b05528b5SGreg Kurz static int synth_mknod(FsContext *fs_ctx, V9fsPath *path,
356364031f1SWei Liu                        const char *buf, FsCred *credp)
357364031f1SWei Liu {
358364031f1SWei Liu     errno = EPERM;
359364031f1SWei Liu     return -1;
360364031f1SWei Liu }
361364031f1SWei Liu 
362b05528b5SGreg Kurz static int synth_mkdir(FsContext *fs_ctx, V9fsPath *path,
363364031f1SWei Liu                        const char *buf, FsCred *credp)
364364031f1SWei Liu {
365364031f1SWei Liu     errno = EPERM;
366364031f1SWei Liu     return -1;
367364031f1SWei Liu }
368364031f1SWei Liu 
369b05528b5SGreg Kurz static ssize_t synth_readlink(FsContext *fs_ctx, V9fsPath *path,
370364031f1SWei Liu                                    char *buf, size_t bufsz)
371364031f1SWei Liu {
372364031f1SWei Liu     errno = ENOSYS;
373364031f1SWei Liu     return -1;
374364031f1SWei Liu }
375364031f1SWei Liu 
376b05528b5SGreg Kurz static int synth_symlink(FsContext *fs_ctx, const char *oldpath,
377364031f1SWei Liu                               V9fsPath *newpath, const char *buf, FsCred *credp)
378364031f1SWei Liu {
379364031f1SWei Liu     errno = EPERM;
380364031f1SWei Liu     return -1;
381364031f1SWei Liu }
382364031f1SWei Liu 
383b05528b5SGreg Kurz static int synth_link(FsContext *fs_ctx, V9fsPath *oldpath,
384364031f1SWei Liu                            V9fsPath *newpath, const char *buf)
385364031f1SWei Liu {
386364031f1SWei Liu     errno = EPERM;
387364031f1SWei Liu     return -1;
388364031f1SWei Liu }
389364031f1SWei Liu 
390b05528b5SGreg Kurz static int synth_rename(FsContext *ctx, const char *oldpath,
391364031f1SWei Liu                              const char *newpath)
392364031f1SWei Liu {
393364031f1SWei Liu     errno = EPERM;
394364031f1SWei Liu     return -1;
395364031f1SWei Liu }
396364031f1SWei Liu 
397b05528b5SGreg Kurz static int synth_chown(FsContext *fs_ctx, V9fsPath *path, FsCred *credp)
398364031f1SWei Liu {
399364031f1SWei Liu     errno = EPERM;
400364031f1SWei Liu     return -1;
401364031f1SWei Liu }
402364031f1SWei Liu 
403b05528b5SGreg Kurz static int synth_utimensat(FsContext *fs_ctx, V9fsPath *path,
404364031f1SWei Liu                                 const struct timespec *buf)
405364031f1SWei Liu {
406364031f1SWei Liu     errno = EPERM;
407364031f1SWei Liu     return 0;
408364031f1SWei Liu }
409364031f1SWei Liu 
410b05528b5SGreg Kurz static int synth_remove(FsContext *ctx, const char *path)
411364031f1SWei Liu {
412364031f1SWei Liu     errno = EPERM;
413364031f1SWei Liu     return -1;
414364031f1SWei Liu }
415364031f1SWei Liu 
416b05528b5SGreg Kurz static int synth_fsync(FsContext *ctx, int fid_type,
417364031f1SWei Liu                             V9fsFidOpenState *fs, int datasync)
418364031f1SWei Liu {
419364031f1SWei Liu     errno = ENOSYS;
420364031f1SWei Liu     return 0;
421364031f1SWei Liu }
422364031f1SWei Liu 
423b05528b5SGreg Kurz static int synth_statfs(FsContext *s, V9fsPath *fs_path,
424364031f1SWei Liu                              struct statfs *stbuf)
425364031f1SWei Liu {
426364031f1SWei Liu     stbuf->f_type = 0xABCD;
427364031f1SWei Liu     stbuf->f_bsize = 512;
428364031f1SWei Liu     stbuf->f_blocks = 0;
429b05528b5SGreg Kurz     stbuf->f_files = synth_node_count;
430364031f1SWei Liu     stbuf->f_namelen = NAME_MAX;
431364031f1SWei Liu     return 0;
432364031f1SWei Liu }
433364031f1SWei Liu 
434b05528b5SGreg Kurz static ssize_t synth_lgetxattr(FsContext *ctx, V9fsPath *path,
435364031f1SWei Liu                                     const char *name, void *value, size_t size)
436364031f1SWei Liu {
437364031f1SWei Liu     errno = ENOTSUP;
438364031f1SWei Liu     return -1;
439364031f1SWei Liu }
440364031f1SWei Liu 
441b05528b5SGreg Kurz static ssize_t synth_llistxattr(FsContext *ctx, V9fsPath *path,
442364031f1SWei Liu                                      void *value, size_t size)
443364031f1SWei Liu {
444364031f1SWei Liu     errno = ENOTSUP;
445364031f1SWei Liu     return -1;
446364031f1SWei Liu }
447364031f1SWei Liu 
448b05528b5SGreg Kurz static int synth_lsetxattr(FsContext *ctx, V9fsPath *path,
449364031f1SWei Liu                                 const char *name, void *value,
450364031f1SWei Liu                                 size_t size, int flags)
451364031f1SWei Liu {
452364031f1SWei Liu     errno = ENOTSUP;
453364031f1SWei Liu     return -1;
454364031f1SWei Liu }
455364031f1SWei Liu 
456b05528b5SGreg Kurz static int synth_lremovexattr(FsContext *ctx,
457364031f1SWei Liu                                    V9fsPath *path, const char *name)
458364031f1SWei Liu {
459364031f1SWei Liu     errno = ENOTSUP;
460364031f1SWei Liu     return -1;
461364031f1SWei Liu }
462364031f1SWei Liu 
463b05528b5SGreg Kurz static int synth_name_to_path(FsContext *ctx, V9fsPath *dir_path,
464364031f1SWei Liu                                    const char *name, V9fsPath *target)
465364031f1SWei Liu {
466364031f1SWei Liu     V9fsSynthNode *node;
467364031f1SWei Liu     V9fsSynthNode *dir_node;
468364031f1SWei Liu 
469364031f1SWei Liu     /* "." and ".." are not allowed */
470364031f1SWei Liu     if (!strcmp(name, ".") || !strcmp(name, "..")) {
471364031f1SWei Liu         errno = EINVAL;
472364031f1SWei Liu         return -1;
473364031f1SWei Liu 
474364031f1SWei Liu     }
475364031f1SWei Liu     if (!dir_path) {
476b05528b5SGreg Kurz         dir_node = &synth_root;
477364031f1SWei Liu     } else {
478364031f1SWei Liu         dir_node = *(V9fsSynthNode **)dir_path->data;
479364031f1SWei Liu     }
480364031f1SWei Liu     if (!strcmp(name, "/")) {
481364031f1SWei Liu         node = dir_node;
482364031f1SWei Liu         goto out;
483364031f1SWei Liu     }
484364031f1SWei Liu     /* search for the name in the childern */
485364031f1SWei Liu     rcu_read_lock();
486364031f1SWei Liu     QLIST_FOREACH(node, &dir_node->child, sibling) {
487364031f1SWei Liu         if (!strcmp(node->name, name)) {
488364031f1SWei Liu             break;
489364031f1SWei Liu         }
490364031f1SWei Liu     }
491364031f1SWei Liu     rcu_read_unlock();
492364031f1SWei Liu 
493364031f1SWei Liu     if (!node) {
494364031f1SWei Liu         errno = ENOENT;
495364031f1SWei Liu         return -1;
496364031f1SWei Liu     }
497364031f1SWei Liu out:
498364031f1SWei Liu     /* Copy the node pointer to fid */
4996ce7177aSMarc-André Lureau     g_free(target->data);
500453a1b23SMarc-André Lureau     target->data = g_memdup(&node, sizeof(void *));
501364031f1SWei Liu     target->size = sizeof(void *);
502364031f1SWei Liu     return 0;
503364031f1SWei Liu }
504364031f1SWei Liu 
505b05528b5SGreg Kurz static int synth_renameat(FsContext *ctx, V9fsPath *olddir,
506364031f1SWei Liu                                const char *old_name, V9fsPath *newdir,
507364031f1SWei Liu                                const char *new_name)
508364031f1SWei Liu {
509364031f1SWei Liu     errno = EPERM;
510364031f1SWei Liu     return -1;
511364031f1SWei Liu }
512364031f1SWei Liu 
513b05528b5SGreg Kurz static int synth_unlinkat(FsContext *ctx, V9fsPath *dir,
514364031f1SWei Liu                                const char *name, int flags)
515364031f1SWei Liu {
516364031f1SWei Liu     errno = EPERM;
517364031f1SWei Liu     return -1;
518364031f1SWei Liu }
519364031f1SWei Liu 
520354b86f8SGreg Kurz static ssize_t v9fs_synth_qtest_write(void *buf, int len, off_t offset,
521354b86f8SGreg Kurz                                       void *arg)
522354b86f8SGreg Kurz {
523354b86f8SGreg Kurz     return 1;
524354b86f8SGreg Kurz }
525354b86f8SGreg Kurz 
526357e2f7fSGreg Kurz static ssize_t v9fs_synth_qtest_flush_write(void *buf, int len, off_t offset,
527357e2f7fSGreg Kurz                                             void *arg)
528357e2f7fSGreg Kurz {
529357e2f7fSGreg Kurz     bool should_block = !!*(uint8_t *)buf;
530357e2f7fSGreg Kurz 
531357e2f7fSGreg Kurz     if (should_block) {
532357e2f7fSGreg Kurz         /* This will cause the server to call us again until we're cancelled */
533357e2f7fSGreg Kurz         errno = EINTR;
534357e2f7fSGreg Kurz         return -1;
535357e2f7fSGreg Kurz     }
536357e2f7fSGreg Kurz 
537357e2f7fSGreg Kurz     return 1;
538357e2f7fSGreg Kurz }
539357e2f7fSGreg Kurz 
54065603a80SGreg Kurz static int synth_init(FsContext *ctx, Error **errp)
541364031f1SWei Liu {
542b05528b5SGreg Kurz     QLIST_INIT(&synth_root.child);
543b05528b5SGreg Kurz     qemu_mutex_init(&synth_mutex);
544364031f1SWei Liu 
545364031f1SWei Liu     /* Add "." and ".." entries for root */
546b05528b5SGreg Kurz     v9fs_add_dir_node(&synth_root, synth_root.attr->mode,
547b05528b5SGreg Kurz                       "..", synth_root.attr, synth_root.attr->inode);
548b05528b5SGreg Kurz     v9fs_add_dir_node(&synth_root, synth_root.attr->mode,
549b05528b5SGreg Kurz                       ".", synth_root.attr, synth_root.attr->inode);
550364031f1SWei Liu 
551364031f1SWei Liu     /* Mark the subsystem is ready for use */
552b05528b5SGreg Kurz     synth_fs = 1;
5532893ddd5SGreg Kurz 
5542893ddd5SGreg Kurz     if (qtest_enabled()) {
5552893ddd5SGreg Kurz         V9fsSynthNode *node = NULL;
5562893ddd5SGreg Kurz         int i, ret;
5572893ddd5SGreg Kurz 
5582893ddd5SGreg Kurz         /* Directory hierarchy for WALK test */
5592893ddd5SGreg Kurz         for (i = 0; i < P9_MAXWELEM; i++) {
5602893ddd5SGreg Kurz             char *name = g_strdup_printf(QTEST_V9FS_SYNTH_WALK_FILE, i);
5612893ddd5SGreg Kurz 
5622893ddd5SGreg Kurz             ret = qemu_v9fs_synth_mkdir(node, 0700, name, &node);
5632893ddd5SGreg Kurz             assert(!ret);
5642893ddd5SGreg Kurz             g_free(name);
5652893ddd5SGreg Kurz         }
56682469aaeSGreg Kurz 
56782469aaeSGreg Kurz         /* File for LOPEN test */
56882469aaeSGreg Kurz         ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_LOPEN_FILE,
56982469aaeSGreg Kurz                                        NULL, NULL, ctx);
57082469aaeSGreg Kurz         assert(!ret);
571354b86f8SGreg Kurz 
572354b86f8SGreg Kurz         /* File for WRITE test */
573354b86f8SGreg Kurz         ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_WRITE_FILE,
574354b86f8SGreg Kurz                                        NULL, v9fs_synth_qtest_write, ctx);
575354b86f8SGreg Kurz         assert(!ret);
576357e2f7fSGreg Kurz 
577357e2f7fSGreg Kurz         /* File for FLUSH test */
578357e2f7fSGreg Kurz         ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_FLUSH_FILE,
579357e2f7fSGreg Kurz                                        NULL, v9fs_synth_qtest_flush_write,
580357e2f7fSGreg Kurz                                        ctx);
581357e2f7fSGreg Kurz         assert(!ret);
582af46a3b2SChristian Schoenebeck 
583af46a3b2SChristian Schoenebeck         /* Directory for READDIR test */
584af46a3b2SChristian Schoenebeck         {
585af46a3b2SChristian Schoenebeck             V9fsSynthNode *dir = NULL;
586af46a3b2SChristian Schoenebeck             ret = qemu_v9fs_synth_mkdir(
587af46a3b2SChristian Schoenebeck                 NULL, 0700, QTEST_V9FS_SYNTH_READDIR_DIR, &dir
588af46a3b2SChristian Schoenebeck             );
589af46a3b2SChristian Schoenebeck             assert(!ret);
590af46a3b2SChristian Schoenebeck             for (i = 0; i < QTEST_V9FS_SYNTH_READDIR_NFILES; ++i) {
591af46a3b2SChristian Schoenebeck                 char *name = g_strdup_printf(
592af46a3b2SChristian Schoenebeck                     QTEST_V9FS_SYNTH_READDIR_FILE, i
593af46a3b2SChristian Schoenebeck                 );
594af46a3b2SChristian Schoenebeck                 ret = qemu_v9fs_synth_add_file(
595af46a3b2SChristian Schoenebeck                     dir, 0, name, NULL, NULL, ctx
596af46a3b2SChristian Schoenebeck                 );
597af46a3b2SChristian Schoenebeck                 assert(!ret);
598af46a3b2SChristian Schoenebeck                 g_free(name);
599af46a3b2SChristian Schoenebeck             }
600af46a3b2SChristian Schoenebeck         }
6012893ddd5SGreg Kurz     }
6022893ddd5SGreg Kurz 
603364031f1SWei Liu     return 0;
604364031f1SWei Liu }
605364031f1SWei Liu 
606364031f1SWei Liu FileOperations synth_ops = {
607b05528b5SGreg Kurz     .init         = synth_init,
608b05528b5SGreg Kurz     .lstat        = synth_lstat,
609b05528b5SGreg Kurz     .readlink     = synth_readlink,
610b05528b5SGreg Kurz     .close        = synth_close,
611b05528b5SGreg Kurz     .closedir     = synth_closedir,
612b05528b5SGreg Kurz     .open         = synth_open,
613b05528b5SGreg Kurz     .opendir      = synth_opendir,
614b05528b5SGreg Kurz     .rewinddir    = synth_rewinddir,
615b05528b5SGreg Kurz     .telldir      = synth_telldir,
616b05528b5SGreg Kurz     .readdir      = synth_readdir,
617b05528b5SGreg Kurz     .seekdir      = synth_seekdir,
618b05528b5SGreg Kurz     .preadv       = synth_preadv,
619b05528b5SGreg Kurz     .pwritev      = synth_pwritev,
620b05528b5SGreg Kurz     .chmod        = synth_chmod,
621b05528b5SGreg Kurz     .mknod        = synth_mknod,
622b05528b5SGreg Kurz     .mkdir        = synth_mkdir,
623b05528b5SGreg Kurz     .fstat        = synth_fstat,
624b05528b5SGreg Kurz     .open2        = synth_open2,
625b05528b5SGreg Kurz     .symlink      = synth_symlink,
626b05528b5SGreg Kurz     .link         = synth_link,
627b05528b5SGreg Kurz     .truncate     = synth_truncate,
628b05528b5SGreg Kurz     .rename       = synth_rename,
629b05528b5SGreg Kurz     .chown        = synth_chown,
630b05528b5SGreg Kurz     .utimensat    = synth_utimensat,
631b05528b5SGreg Kurz     .remove       = synth_remove,
632b05528b5SGreg Kurz     .fsync        = synth_fsync,
633b05528b5SGreg Kurz     .statfs       = synth_statfs,
634b05528b5SGreg Kurz     .lgetxattr    = synth_lgetxattr,
635b05528b5SGreg Kurz     .llistxattr   = synth_llistxattr,
636b05528b5SGreg Kurz     .lsetxattr    = synth_lsetxattr,
637b05528b5SGreg Kurz     .lremovexattr = synth_lremovexattr,
638b05528b5SGreg Kurz     .name_to_path = synth_name_to_path,
639b05528b5SGreg Kurz     .renameat     = synth_renameat,
640b05528b5SGreg Kurz     .unlinkat     = synth_unlinkat,
641364031f1SWei Liu };
642