xref: /openbmc/qemu/hw/9pfs/9p-synth.c (revision 65603a801e14a89701b359cd12d7c5b1764e6de1)
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 
15fbc04127SPeter Maydell #include "qemu/osdep.h"
16ebe74f8bSWei Liu #include "9p.h"
17364031f1SWei Liu #include "fsdev/qemu-fsdev.h"
18364031f1SWei Liu #include "9p-synth.h"
19364031f1SWei Liu #include "qemu/rcu.h"
20364031f1SWei Liu #include "qemu/rcu_queue.h"
21f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
22364031f1SWei Liu 
23364031f1SWei Liu /* Root node for synth file system */
24b05528b5SGreg Kurz static V9fsSynthNode synth_root = {
25364031f1SWei Liu     .name = "/",
26364031f1SWei Liu     .actual_attr = {
27364031f1SWei Liu         .mode = 0555 | S_IFDIR,
28364031f1SWei Liu         .nlink = 1,
29364031f1SWei Liu     },
30b05528b5SGreg Kurz     .attr = &synth_root.actual_attr,
31364031f1SWei Liu };
32364031f1SWei Liu 
33b05528b5SGreg Kurz static QemuMutex  synth_mutex;
34b05528b5SGreg Kurz static int synth_node_count;
35364031f1SWei Liu /* set to 1 when the synth fs is ready */
36b05528b5SGreg Kurz static int synth_fs;
37364031f1SWei Liu 
38364031f1SWei Liu static V9fsSynthNode *v9fs_add_dir_node(V9fsSynthNode *parent, int mode,
39364031f1SWei Liu                                         const char *name,
40364031f1SWei Liu                                         V9fsSynthNodeAttr *attr, int inode)
41364031f1SWei Liu {
42364031f1SWei Liu     V9fsSynthNode *node;
43364031f1SWei Liu 
44364031f1SWei Liu     /* Add directory type and remove write bits */
45364031f1SWei Liu     mode = ((mode & 0777) | S_IFDIR) & ~(S_IWUSR | S_IWGRP | S_IWOTH);
46364031f1SWei Liu     node = g_malloc0(sizeof(V9fsSynthNode));
47364031f1SWei Liu     if (attr) {
48364031f1SWei Liu         /* We are adding .. or . entries */
49364031f1SWei Liu         node->attr = attr;
50364031f1SWei Liu         node->attr->nlink++;
51364031f1SWei Liu     } else {
52364031f1SWei Liu         node->attr = &node->actual_attr;
53364031f1SWei Liu         node->attr->inode = inode;
54364031f1SWei Liu         node->attr->nlink = 1;
55364031f1SWei Liu         /* We don't allow write to directories */
56364031f1SWei Liu         node->attr->mode   = mode;
57364031f1SWei Liu         node->attr->write = NULL;
58364031f1SWei Liu         node->attr->read  = NULL;
59364031f1SWei Liu     }
60364031f1SWei Liu     node->private = node;
61364031f1SWei Liu     pstrcpy(node->name, sizeof(node->name), name);
62364031f1SWei Liu     QLIST_INSERT_HEAD_RCU(&parent->child, node, sibling);
63364031f1SWei Liu     return node;
64364031f1SWei Liu }
65364031f1SWei Liu 
66364031f1SWei Liu int qemu_v9fs_synth_mkdir(V9fsSynthNode *parent, int mode,
67364031f1SWei Liu                           const char *name, V9fsSynthNode **result)
68364031f1SWei Liu {
69364031f1SWei Liu     int ret;
70364031f1SWei Liu     V9fsSynthNode *node, *tmp;
71364031f1SWei Liu 
72b05528b5SGreg Kurz     if (!synth_fs) {
73364031f1SWei Liu         return EAGAIN;
74364031f1SWei Liu     }
75364031f1SWei Liu     if (!name || (strlen(name) >= NAME_MAX)) {
76364031f1SWei Liu         return EINVAL;
77364031f1SWei Liu     }
78364031f1SWei Liu     if (!parent) {
79b05528b5SGreg Kurz         parent = &synth_root;
80364031f1SWei Liu     }
81b05528b5SGreg Kurz     qemu_mutex_lock(&synth_mutex);
82364031f1SWei Liu     QLIST_FOREACH(tmp, &parent->child, sibling) {
83364031f1SWei Liu         if (!strcmp(tmp->name, name)) {
84364031f1SWei Liu             ret = EEXIST;
85364031f1SWei Liu             goto err_out;
86364031f1SWei Liu         }
87364031f1SWei Liu     }
88364031f1SWei Liu     /* Add the name */
89b05528b5SGreg Kurz     node = v9fs_add_dir_node(parent, mode, name, NULL, synth_node_count++);
90364031f1SWei Liu     v9fs_add_dir_node(node, parent->attr->mode, "..",
91364031f1SWei Liu                       parent->attr, parent->attr->inode);
92364031f1SWei Liu     v9fs_add_dir_node(node, node->attr->mode, ".",
93364031f1SWei Liu                       node->attr, node->attr->inode);
94364031f1SWei Liu     *result = node;
95364031f1SWei Liu     ret = 0;
96364031f1SWei Liu err_out:
97b05528b5SGreg Kurz     qemu_mutex_unlock(&synth_mutex);
98364031f1SWei Liu     return ret;
99364031f1SWei Liu }
100364031f1SWei Liu 
101364031f1SWei Liu int qemu_v9fs_synth_add_file(V9fsSynthNode *parent, int mode,
102364031f1SWei Liu                              const char *name, v9fs_synth_read read,
103364031f1SWei Liu                              v9fs_synth_write write, void *arg)
104364031f1SWei Liu {
105364031f1SWei Liu     int ret;
106364031f1SWei Liu     V9fsSynthNode *node, *tmp;
107364031f1SWei Liu 
108b05528b5SGreg Kurz     if (!synth_fs) {
109364031f1SWei Liu         return EAGAIN;
110364031f1SWei Liu     }
111364031f1SWei Liu     if (!name || (strlen(name) >= NAME_MAX)) {
112364031f1SWei Liu         return EINVAL;
113364031f1SWei Liu     }
114364031f1SWei Liu     if (!parent) {
115b05528b5SGreg Kurz         parent = &synth_root;
116364031f1SWei Liu     }
117364031f1SWei Liu 
118b05528b5SGreg Kurz     qemu_mutex_lock(&synth_mutex);
119364031f1SWei Liu     QLIST_FOREACH(tmp, &parent->child, sibling) {
120364031f1SWei Liu         if (!strcmp(tmp->name, name)) {
121364031f1SWei Liu             ret = EEXIST;
122364031f1SWei Liu             goto err_out;
123364031f1SWei Liu         }
124364031f1SWei Liu     }
125364031f1SWei Liu     /* Add file type and remove write bits */
126364031f1SWei Liu     mode = ((mode & 0777) | S_IFREG);
127364031f1SWei Liu     node = g_malloc0(sizeof(V9fsSynthNode));
128364031f1SWei Liu     node->attr         = &node->actual_attr;
129b05528b5SGreg Kurz     node->attr->inode  = synth_node_count++;
130364031f1SWei Liu     node->attr->nlink  = 1;
131364031f1SWei Liu     node->attr->read   = read;
132364031f1SWei Liu     node->attr->write  = write;
133364031f1SWei Liu     node->attr->mode   = mode;
134364031f1SWei Liu     node->private      = arg;
135364031f1SWei Liu     pstrcpy(node->name, sizeof(node->name), name);
136364031f1SWei Liu     QLIST_INSERT_HEAD_RCU(&parent->child, node, sibling);
137364031f1SWei Liu     ret = 0;
138364031f1SWei Liu err_out:
139b05528b5SGreg Kurz     qemu_mutex_unlock(&synth_mutex);
140364031f1SWei Liu     return ret;
141364031f1SWei Liu }
142364031f1SWei Liu 
143b05528b5SGreg Kurz static void synth_fill_statbuf(V9fsSynthNode *node, struct stat *stbuf)
144364031f1SWei Liu {
145364031f1SWei Liu     stbuf->st_dev = 0;
146364031f1SWei Liu     stbuf->st_ino = node->attr->inode;
147364031f1SWei Liu     stbuf->st_mode = node->attr->mode;
148364031f1SWei Liu     stbuf->st_nlink = node->attr->nlink;
149364031f1SWei Liu     stbuf->st_uid = 0;
150364031f1SWei Liu     stbuf->st_gid = 0;
151364031f1SWei Liu     stbuf->st_rdev = 0;
152364031f1SWei Liu     stbuf->st_size = 0;
153364031f1SWei Liu     stbuf->st_blksize = 0;
154364031f1SWei Liu     stbuf->st_blocks = 0;
155364031f1SWei Liu     stbuf->st_atime = 0;
156364031f1SWei Liu     stbuf->st_mtime = 0;
157364031f1SWei Liu     stbuf->st_ctime = 0;
158364031f1SWei Liu }
159364031f1SWei Liu 
160b05528b5SGreg Kurz static int synth_lstat(FsContext *fs_ctx,
161364031f1SWei Liu                             V9fsPath *fs_path, struct stat *stbuf)
162364031f1SWei Liu {
163364031f1SWei Liu     V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;
164364031f1SWei Liu 
165b05528b5SGreg Kurz     synth_fill_statbuf(node, stbuf);
166364031f1SWei Liu     return 0;
167364031f1SWei Liu }
168364031f1SWei Liu 
169b05528b5SGreg Kurz static int synth_fstat(FsContext *fs_ctx, int fid_type,
170364031f1SWei Liu                             V9fsFidOpenState *fs, struct stat *stbuf)
171364031f1SWei Liu {
172364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
173b05528b5SGreg Kurz     synth_fill_statbuf(synth_open->node, stbuf);
174364031f1SWei Liu     return 0;
175364031f1SWei Liu }
176364031f1SWei Liu 
177b05528b5SGreg Kurz static int synth_opendir(FsContext *ctx,
178364031f1SWei Liu                              V9fsPath *fs_path, V9fsFidOpenState *fs)
179364031f1SWei Liu {
180364031f1SWei Liu     V9fsSynthOpenState *synth_open;
181364031f1SWei Liu     V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;
182364031f1SWei Liu 
183364031f1SWei Liu     synth_open = g_malloc(sizeof(*synth_open));
184364031f1SWei Liu     synth_open->node = node;
185364031f1SWei Liu     node->open_count++;
186364031f1SWei Liu     fs->private = synth_open;
187364031f1SWei Liu     return 0;
188364031f1SWei Liu }
189364031f1SWei Liu 
190b05528b5SGreg Kurz static int synth_closedir(FsContext *ctx, V9fsFidOpenState *fs)
191364031f1SWei Liu {
192364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
193364031f1SWei Liu     V9fsSynthNode *node = synth_open->node;
194364031f1SWei Liu 
195364031f1SWei Liu     node->open_count--;
196364031f1SWei Liu     g_free(synth_open);
197364031f1SWei Liu     fs->private = NULL;
198364031f1SWei Liu     return 0;
199364031f1SWei Liu }
200364031f1SWei Liu 
201b05528b5SGreg Kurz static off_t synth_telldir(FsContext *ctx, V9fsFidOpenState *fs)
202364031f1SWei Liu {
203364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
204364031f1SWei Liu     return synth_open->offset;
205364031f1SWei Liu }
206364031f1SWei Liu 
207b05528b5SGreg Kurz static void synth_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
208364031f1SWei Liu {
209364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
210364031f1SWei Liu     synth_open->offset = off;
211364031f1SWei Liu }
212364031f1SWei Liu 
213b05528b5SGreg Kurz static void synth_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
214364031f1SWei Liu {
215b05528b5SGreg Kurz     synth_seekdir(ctx, fs, 0);
216364031f1SWei Liu }
217364031f1SWei Liu 
218b05528b5SGreg Kurz static void synth_direntry(V9fsSynthNode *node,
219364031f1SWei Liu                                 struct dirent *entry, off_t off)
220364031f1SWei Liu {
221364031f1SWei Liu     strcpy(entry->d_name, node->name);
222364031f1SWei Liu     entry->d_ino = node->attr->inode;
223364031f1SWei Liu     entry->d_off = off + 1;
224364031f1SWei Liu }
225364031f1SWei Liu 
226b05528b5SGreg Kurz static struct dirent *synth_get_dentry(V9fsSynthNode *dir,
227635324e8SGreg Kurz                                             struct dirent *entry, off_t off)
228364031f1SWei Liu {
229364031f1SWei Liu     int i = 0;
230364031f1SWei Liu     V9fsSynthNode *node;
231364031f1SWei Liu 
232364031f1SWei Liu     rcu_read_lock();
233364031f1SWei Liu     QLIST_FOREACH(node, &dir->child, sibling) {
234364031f1SWei Liu         /* This is the off child of the directory */
235364031f1SWei Liu         if (i == off) {
236364031f1SWei Liu             break;
237364031f1SWei Liu         }
238364031f1SWei Liu         i++;
239364031f1SWei Liu     }
240364031f1SWei Liu     rcu_read_unlock();
241364031f1SWei Liu     if (!node) {
242364031f1SWei Liu         /* end of directory */
243635324e8SGreg Kurz         return NULL;
244364031f1SWei Liu     }
245b05528b5SGreg Kurz     synth_direntry(node, entry, off);
246635324e8SGreg Kurz     return entry;
247364031f1SWei Liu }
248364031f1SWei Liu 
249b05528b5SGreg Kurz static struct dirent *synth_readdir(FsContext *ctx, V9fsFidOpenState *fs)
250364031f1SWei Liu {
251635324e8SGreg Kurz     struct dirent *entry;
252364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
253364031f1SWei Liu     V9fsSynthNode *node = synth_open->node;
254b05528b5SGreg Kurz     entry = synth_get_dentry(node, &synth_open->dent, synth_open->offset);
255635324e8SGreg Kurz     if (entry) {
256364031f1SWei Liu         synth_open->offset++;
257364031f1SWei Liu     }
258635324e8SGreg Kurz     return entry;
259364031f1SWei Liu }
260364031f1SWei Liu 
261b05528b5SGreg Kurz static int synth_open(FsContext *ctx, V9fsPath *fs_path,
262364031f1SWei Liu                            int flags, V9fsFidOpenState *fs)
263364031f1SWei Liu {
264364031f1SWei Liu     V9fsSynthOpenState *synth_open;
265364031f1SWei Liu     V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;
266364031f1SWei Liu 
267364031f1SWei Liu     synth_open = g_malloc(sizeof(*synth_open));
268364031f1SWei Liu     synth_open->node = node;
269364031f1SWei Liu     node->open_count++;
270364031f1SWei Liu     fs->private = synth_open;
271364031f1SWei Liu     return 0;
272364031f1SWei Liu }
273364031f1SWei Liu 
274b05528b5SGreg Kurz static int synth_open2(FsContext *fs_ctx, V9fsPath *dir_path,
275364031f1SWei Liu                             const char *name, int flags,
276364031f1SWei Liu                             FsCred *credp, V9fsFidOpenState *fs)
277364031f1SWei Liu {
278364031f1SWei Liu     errno = ENOSYS;
279364031f1SWei Liu     return -1;
280364031f1SWei Liu }
281364031f1SWei Liu 
282b05528b5SGreg Kurz static int synth_close(FsContext *ctx, V9fsFidOpenState *fs)
283364031f1SWei Liu {
284364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
285364031f1SWei Liu     V9fsSynthNode *node = synth_open->node;
286364031f1SWei Liu 
287364031f1SWei Liu     node->open_count--;
288364031f1SWei Liu     g_free(synth_open);
289364031f1SWei Liu     fs->private = NULL;
290364031f1SWei Liu     return 0;
291364031f1SWei Liu }
292364031f1SWei Liu 
293b05528b5SGreg Kurz static ssize_t synth_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
294364031f1SWei Liu                                   const struct iovec *iov,
295364031f1SWei Liu                                   int iovcnt, off_t offset)
296364031f1SWei Liu {
297364031f1SWei Liu     int i, count = 0, wcount;
298364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
299364031f1SWei Liu     V9fsSynthNode *node = synth_open->node;
300364031f1SWei Liu     if (!node->attr->write) {
301364031f1SWei Liu         errno = EPERM;
302364031f1SWei Liu         return -1;
303364031f1SWei Liu     }
304364031f1SWei Liu     for (i = 0; i < iovcnt; i++) {
305364031f1SWei Liu         wcount = node->attr->write(iov[i].iov_base, iov[i].iov_len,
306364031f1SWei Liu                                    offset, node->private);
307364031f1SWei Liu         offset += wcount;
308364031f1SWei Liu         count  += wcount;
309364031f1SWei Liu         /* If we wrote less than requested. we are done */
310364031f1SWei Liu         if (wcount < iov[i].iov_len) {
311364031f1SWei Liu             break;
312364031f1SWei Liu         }
313364031f1SWei Liu     }
314364031f1SWei Liu     return count;
315364031f1SWei Liu }
316364031f1SWei Liu 
317b05528b5SGreg Kurz static ssize_t synth_preadv(FsContext *ctx, V9fsFidOpenState *fs,
318364031f1SWei Liu                                  const struct iovec *iov,
319364031f1SWei Liu                                  int iovcnt, off_t offset)
320364031f1SWei Liu {
321364031f1SWei Liu     int i, count = 0, rcount;
322364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
323364031f1SWei Liu     V9fsSynthNode *node = synth_open->node;
324364031f1SWei Liu     if (!node->attr->read) {
325364031f1SWei Liu         errno = EPERM;
326364031f1SWei Liu         return -1;
327364031f1SWei Liu     }
328364031f1SWei Liu     for (i = 0; i < iovcnt; i++) {
329364031f1SWei Liu         rcount = node->attr->read(iov[i].iov_base, iov[i].iov_len,
330364031f1SWei Liu                                   offset, node->private);
331364031f1SWei Liu         offset += rcount;
332364031f1SWei Liu         count  += rcount;
333364031f1SWei Liu         /* If we read less than requested. we are done */
334364031f1SWei Liu         if (rcount < iov[i].iov_len) {
335364031f1SWei Liu             break;
336364031f1SWei Liu         }
337364031f1SWei Liu     }
338364031f1SWei Liu     return count;
339364031f1SWei Liu }
340364031f1SWei Liu 
341b05528b5SGreg Kurz static int synth_truncate(FsContext *ctx, V9fsPath *path, off_t offset)
342364031f1SWei Liu {
343364031f1SWei Liu     errno = ENOSYS;
344364031f1SWei Liu     return -1;
345364031f1SWei Liu }
346364031f1SWei Liu 
347b05528b5SGreg Kurz static int synth_chmod(FsContext *fs_ctx, V9fsPath *path, FsCred *credp)
348364031f1SWei Liu {
349364031f1SWei Liu     errno = EPERM;
350364031f1SWei Liu     return -1;
351364031f1SWei Liu }
352364031f1SWei Liu 
353b05528b5SGreg Kurz static int synth_mknod(FsContext *fs_ctx, V9fsPath *path,
354364031f1SWei Liu                        const char *buf, FsCred *credp)
355364031f1SWei Liu {
356364031f1SWei Liu     errno = EPERM;
357364031f1SWei Liu     return -1;
358364031f1SWei Liu }
359364031f1SWei Liu 
360b05528b5SGreg Kurz static int synth_mkdir(FsContext *fs_ctx, V9fsPath *path,
361364031f1SWei Liu                        const char *buf, FsCred *credp)
362364031f1SWei Liu {
363364031f1SWei Liu     errno = EPERM;
364364031f1SWei Liu     return -1;
365364031f1SWei Liu }
366364031f1SWei Liu 
367b05528b5SGreg Kurz static ssize_t synth_readlink(FsContext *fs_ctx, V9fsPath *path,
368364031f1SWei Liu                                    char *buf, size_t bufsz)
369364031f1SWei Liu {
370364031f1SWei Liu     errno = ENOSYS;
371364031f1SWei Liu     return -1;
372364031f1SWei Liu }
373364031f1SWei Liu 
374b05528b5SGreg Kurz static int synth_symlink(FsContext *fs_ctx, const char *oldpath,
375364031f1SWei Liu                               V9fsPath *newpath, const char *buf, FsCred *credp)
376364031f1SWei Liu {
377364031f1SWei Liu     errno = EPERM;
378364031f1SWei Liu     return -1;
379364031f1SWei Liu }
380364031f1SWei Liu 
381b05528b5SGreg Kurz static int synth_link(FsContext *fs_ctx, V9fsPath *oldpath,
382364031f1SWei Liu                            V9fsPath *newpath, const char *buf)
383364031f1SWei Liu {
384364031f1SWei Liu     errno = EPERM;
385364031f1SWei Liu     return -1;
386364031f1SWei Liu }
387364031f1SWei Liu 
388b05528b5SGreg Kurz static int synth_rename(FsContext *ctx, const char *oldpath,
389364031f1SWei Liu                              const char *newpath)
390364031f1SWei Liu {
391364031f1SWei Liu     errno = EPERM;
392364031f1SWei Liu     return -1;
393364031f1SWei Liu }
394364031f1SWei Liu 
395b05528b5SGreg Kurz static int synth_chown(FsContext *fs_ctx, V9fsPath *path, FsCred *credp)
396364031f1SWei Liu {
397364031f1SWei Liu     errno = EPERM;
398364031f1SWei Liu     return -1;
399364031f1SWei Liu }
400364031f1SWei Liu 
401b05528b5SGreg Kurz static int synth_utimensat(FsContext *fs_ctx, V9fsPath *path,
402364031f1SWei Liu                                 const struct timespec *buf)
403364031f1SWei Liu {
404364031f1SWei Liu     errno = EPERM;
405364031f1SWei Liu     return 0;
406364031f1SWei Liu }
407364031f1SWei Liu 
408b05528b5SGreg Kurz static int synth_remove(FsContext *ctx, const char *path)
409364031f1SWei Liu {
410364031f1SWei Liu     errno = EPERM;
411364031f1SWei Liu     return -1;
412364031f1SWei Liu }
413364031f1SWei Liu 
414b05528b5SGreg Kurz static int synth_fsync(FsContext *ctx, int fid_type,
415364031f1SWei Liu                             V9fsFidOpenState *fs, int datasync)
416364031f1SWei Liu {
417364031f1SWei Liu     errno = ENOSYS;
418364031f1SWei Liu     return 0;
419364031f1SWei Liu }
420364031f1SWei Liu 
421b05528b5SGreg Kurz static int synth_statfs(FsContext *s, V9fsPath *fs_path,
422364031f1SWei Liu                              struct statfs *stbuf)
423364031f1SWei Liu {
424364031f1SWei Liu     stbuf->f_type = 0xABCD;
425364031f1SWei Liu     stbuf->f_bsize = 512;
426364031f1SWei Liu     stbuf->f_blocks = 0;
427b05528b5SGreg Kurz     stbuf->f_files = synth_node_count;
428364031f1SWei Liu     stbuf->f_namelen = NAME_MAX;
429364031f1SWei Liu     return 0;
430364031f1SWei Liu }
431364031f1SWei Liu 
432b05528b5SGreg Kurz static ssize_t synth_lgetxattr(FsContext *ctx, V9fsPath *path,
433364031f1SWei Liu                                     const char *name, void *value, size_t size)
434364031f1SWei Liu {
435364031f1SWei Liu     errno = ENOTSUP;
436364031f1SWei Liu     return -1;
437364031f1SWei Liu }
438364031f1SWei Liu 
439b05528b5SGreg Kurz static ssize_t synth_llistxattr(FsContext *ctx, V9fsPath *path,
440364031f1SWei Liu                                      void *value, size_t size)
441364031f1SWei Liu {
442364031f1SWei Liu     errno = ENOTSUP;
443364031f1SWei Liu     return -1;
444364031f1SWei Liu }
445364031f1SWei Liu 
446b05528b5SGreg Kurz static int synth_lsetxattr(FsContext *ctx, V9fsPath *path,
447364031f1SWei Liu                                 const char *name, void *value,
448364031f1SWei Liu                                 size_t size, int flags)
449364031f1SWei Liu {
450364031f1SWei Liu     errno = ENOTSUP;
451364031f1SWei Liu     return -1;
452364031f1SWei Liu }
453364031f1SWei Liu 
454b05528b5SGreg Kurz static int synth_lremovexattr(FsContext *ctx,
455364031f1SWei Liu                                    V9fsPath *path, const char *name)
456364031f1SWei Liu {
457364031f1SWei Liu     errno = ENOTSUP;
458364031f1SWei Liu     return -1;
459364031f1SWei Liu }
460364031f1SWei Liu 
461b05528b5SGreg Kurz static int synth_name_to_path(FsContext *ctx, V9fsPath *dir_path,
462364031f1SWei Liu                                    const char *name, V9fsPath *target)
463364031f1SWei Liu {
464364031f1SWei Liu     V9fsSynthNode *node;
465364031f1SWei Liu     V9fsSynthNode *dir_node;
466364031f1SWei Liu 
467364031f1SWei Liu     /* "." and ".." are not allowed */
468364031f1SWei Liu     if (!strcmp(name, ".") || !strcmp(name, "..")) {
469364031f1SWei Liu         errno = EINVAL;
470364031f1SWei Liu         return -1;
471364031f1SWei Liu 
472364031f1SWei Liu     }
473364031f1SWei Liu     if (!dir_path) {
474b05528b5SGreg Kurz         dir_node = &synth_root;
475364031f1SWei Liu     } else {
476364031f1SWei Liu         dir_node = *(V9fsSynthNode **)dir_path->data;
477364031f1SWei Liu     }
478364031f1SWei Liu     if (!strcmp(name, "/")) {
479364031f1SWei Liu         node = dir_node;
480364031f1SWei Liu         goto out;
481364031f1SWei Liu     }
482364031f1SWei Liu     /* search for the name in the childern */
483364031f1SWei Liu     rcu_read_lock();
484364031f1SWei Liu     QLIST_FOREACH(node, &dir_node->child, sibling) {
485364031f1SWei Liu         if (!strcmp(node->name, name)) {
486364031f1SWei Liu             break;
487364031f1SWei Liu         }
488364031f1SWei Liu     }
489364031f1SWei Liu     rcu_read_unlock();
490364031f1SWei Liu 
491364031f1SWei Liu     if (!node) {
492364031f1SWei Liu         errno = ENOENT;
493364031f1SWei Liu         return -1;
494364031f1SWei Liu     }
495364031f1SWei Liu out:
496364031f1SWei Liu     /* Copy the node pointer to fid */
497453a1b23SMarc-André Lureau     target->data = g_memdup(&node, sizeof(void *));
498364031f1SWei Liu     target->size = sizeof(void *);
499364031f1SWei Liu     return 0;
500364031f1SWei Liu }
501364031f1SWei Liu 
502b05528b5SGreg Kurz static int synth_renameat(FsContext *ctx, V9fsPath *olddir,
503364031f1SWei Liu                                const char *old_name, V9fsPath *newdir,
504364031f1SWei Liu                                const char *new_name)
505364031f1SWei Liu {
506364031f1SWei Liu     errno = EPERM;
507364031f1SWei Liu     return -1;
508364031f1SWei Liu }
509364031f1SWei Liu 
510b05528b5SGreg Kurz static int synth_unlinkat(FsContext *ctx, V9fsPath *dir,
511364031f1SWei Liu                                const char *name, int flags)
512364031f1SWei Liu {
513364031f1SWei Liu     errno = EPERM;
514364031f1SWei Liu     return -1;
515364031f1SWei Liu }
516364031f1SWei Liu 
517*65603a80SGreg Kurz static int synth_init(FsContext *ctx, Error **errp)
518364031f1SWei Liu {
519b05528b5SGreg Kurz     QLIST_INIT(&synth_root.child);
520b05528b5SGreg Kurz     qemu_mutex_init(&synth_mutex);
521364031f1SWei Liu 
522364031f1SWei Liu     /* Add "." and ".." entries for root */
523b05528b5SGreg Kurz     v9fs_add_dir_node(&synth_root, synth_root.attr->mode,
524b05528b5SGreg Kurz                       "..", synth_root.attr, synth_root.attr->inode);
525b05528b5SGreg Kurz     v9fs_add_dir_node(&synth_root, synth_root.attr->mode,
526b05528b5SGreg Kurz                       ".", synth_root.attr, synth_root.attr->inode);
527364031f1SWei Liu 
528364031f1SWei Liu     /* Mark the subsystem is ready for use */
529b05528b5SGreg Kurz     synth_fs = 1;
530364031f1SWei Liu     return 0;
531364031f1SWei Liu }
532364031f1SWei Liu 
533364031f1SWei Liu FileOperations synth_ops = {
534b05528b5SGreg Kurz     .init         = synth_init,
535b05528b5SGreg Kurz     .lstat        = synth_lstat,
536b05528b5SGreg Kurz     .readlink     = synth_readlink,
537b05528b5SGreg Kurz     .close        = synth_close,
538b05528b5SGreg Kurz     .closedir     = synth_closedir,
539b05528b5SGreg Kurz     .open         = synth_open,
540b05528b5SGreg Kurz     .opendir      = synth_opendir,
541b05528b5SGreg Kurz     .rewinddir    = synth_rewinddir,
542b05528b5SGreg Kurz     .telldir      = synth_telldir,
543b05528b5SGreg Kurz     .readdir      = synth_readdir,
544b05528b5SGreg Kurz     .seekdir      = synth_seekdir,
545b05528b5SGreg Kurz     .preadv       = synth_preadv,
546b05528b5SGreg Kurz     .pwritev      = synth_pwritev,
547b05528b5SGreg Kurz     .chmod        = synth_chmod,
548b05528b5SGreg Kurz     .mknod        = synth_mknod,
549b05528b5SGreg Kurz     .mkdir        = synth_mkdir,
550b05528b5SGreg Kurz     .fstat        = synth_fstat,
551b05528b5SGreg Kurz     .open2        = synth_open2,
552b05528b5SGreg Kurz     .symlink      = synth_symlink,
553b05528b5SGreg Kurz     .link         = synth_link,
554b05528b5SGreg Kurz     .truncate     = synth_truncate,
555b05528b5SGreg Kurz     .rename       = synth_rename,
556b05528b5SGreg Kurz     .chown        = synth_chown,
557b05528b5SGreg Kurz     .utimensat    = synth_utimensat,
558b05528b5SGreg Kurz     .remove       = synth_remove,
559b05528b5SGreg Kurz     .fsync        = synth_fsync,
560b05528b5SGreg Kurz     .statfs       = synth_statfs,
561b05528b5SGreg Kurz     .lgetxattr    = synth_lgetxattr,
562b05528b5SGreg Kurz     .llistxattr   = synth_llistxattr,
563b05528b5SGreg Kurz     .lsetxattr    = synth_lsetxattr,
564b05528b5SGreg Kurz     .lremovexattr = synth_lremovexattr,
565b05528b5SGreg Kurz     .name_to_path = synth_name_to_path,
566b05528b5SGreg Kurz     .renameat     = synth_renameat,
567b05528b5SGreg Kurz     .unlinkat     = synth_unlinkat,
568364031f1SWei Liu };
569