xref: /openbmc/qemu/hw/9pfs/9p-synth.c (revision f348b6d1a53e5271cf1c9f9acc4646b4b98c1771)
1364031f1SWei Liu /*
2364031f1SWei Liu  * Virtio 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"
16364031f1SWei Liu #include "hw/virtio/virtio.h"
17ebe74f8bSWei Liu #include "9p.h"
18267ae092SWei Liu #include "9p-xattr.h"
19364031f1SWei Liu #include "fsdev/qemu-fsdev.h"
20364031f1SWei Liu #include "9p-synth.h"
21364031f1SWei Liu #include "qemu/rcu.h"
22364031f1SWei Liu #include "qemu/rcu_queue.h"
23*f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
24364031f1SWei Liu 
25364031f1SWei Liu /* Root node for synth file system */
26364031f1SWei Liu static V9fsSynthNode v9fs_synth_root = {
27364031f1SWei Liu     .name = "/",
28364031f1SWei Liu     .actual_attr = {
29364031f1SWei Liu         .mode = 0555 | S_IFDIR,
30364031f1SWei Liu         .nlink = 1,
31364031f1SWei Liu     },
32364031f1SWei Liu     .attr = &v9fs_synth_root.actual_attr,
33364031f1SWei Liu };
34364031f1SWei Liu 
35364031f1SWei Liu static QemuMutex  v9fs_synth_mutex;
36364031f1SWei Liu static int v9fs_synth_node_count;
37364031f1SWei Liu /* set to 1 when the synth fs is ready */
38364031f1SWei Liu static int v9fs_synth_fs;
39364031f1SWei Liu 
40364031f1SWei Liu static V9fsSynthNode *v9fs_add_dir_node(V9fsSynthNode *parent, int mode,
41364031f1SWei Liu                                         const char *name,
42364031f1SWei Liu                                         V9fsSynthNodeAttr *attr, int inode)
43364031f1SWei Liu {
44364031f1SWei Liu     V9fsSynthNode *node;
45364031f1SWei Liu 
46364031f1SWei Liu     /* Add directory type and remove write bits */
47364031f1SWei Liu     mode = ((mode & 0777) | S_IFDIR) & ~(S_IWUSR | S_IWGRP | S_IWOTH);
48364031f1SWei Liu     node = g_malloc0(sizeof(V9fsSynthNode));
49364031f1SWei Liu     if (attr) {
50364031f1SWei Liu         /* We are adding .. or . entries */
51364031f1SWei Liu         node->attr = attr;
52364031f1SWei Liu         node->attr->nlink++;
53364031f1SWei Liu     } else {
54364031f1SWei Liu         node->attr = &node->actual_attr;
55364031f1SWei Liu         node->attr->inode = inode;
56364031f1SWei Liu         node->attr->nlink = 1;
57364031f1SWei Liu         /* We don't allow write to directories */
58364031f1SWei Liu         node->attr->mode   = mode;
59364031f1SWei Liu         node->attr->write = NULL;
60364031f1SWei Liu         node->attr->read  = NULL;
61364031f1SWei Liu     }
62364031f1SWei Liu     node->private = node;
63364031f1SWei Liu     pstrcpy(node->name, sizeof(node->name), name);
64364031f1SWei Liu     QLIST_INSERT_HEAD_RCU(&parent->child, node, sibling);
65364031f1SWei Liu     return node;
66364031f1SWei Liu }
67364031f1SWei Liu 
68364031f1SWei Liu int qemu_v9fs_synth_mkdir(V9fsSynthNode *parent, int mode,
69364031f1SWei Liu                           const char *name, V9fsSynthNode **result)
70364031f1SWei Liu {
71364031f1SWei Liu     int ret;
72364031f1SWei Liu     V9fsSynthNode *node, *tmp;
73364031f1SWei Liu 
74364031f1SWei Liu     if (!v9fs_synth_fs) {
75364031f1SWei Liu         return EAGAIN;
76364031f1SWei Liu     }
77364031f1SWei Liu     if (!name || (strlen(name) >= NAME_MAX)) {
78364031f1SWei Liu         return EINVAL;
79364031f1SWei Liu     }
80364031f1SWei Liu     if (!parent) {
81364031f1SWei Liu         parent = &v9fs_synth_root;
82364031f1SWei Liu     }
83364031f1SWei Liu     qemu_mutex_lock(&v9fs_synth_mutex);
84364031f1SWei Liu     QLIST_FOREACH(tmp, &parent->child, sibling) {
85364031f1SWei Liu         if (!strcmp(tmp->name, name)) {
86364031f1SWei Liu             ret = EEXIST;
87364031f1SWei Liu             goto err_out;
88364031f1SWei Liu         }
89364031f1SWei Liu     }
90364031f1SWei Liu     /* Add the name */
91364031f1SWei Liu     node = v9fs_add_dir_node(parent, mode, name, NULL, v9fs_synth_node_count++);
92364031f1SWei Liu     v9fs_add_dir_node(node, parent->attr->mode, "..",
93364031f1SWei Liu                       parent->attr, parent->attr->inode);
94364031f1SWei Liu     v9fs_add_dir_node(node, node->attr->mode, ".",
95364031f1SWei Liu                       node->attr, node->attr->inode);
96364031f1SWei Liu     *result = node;
97364031f1SWei Liu     ret = 0;
98364031f1SWei Liu err_out:
99364031f1SWei Liu     qemu_mutex_unlock(&v9fs_synth_mutex);
100364031f1SWei Liu     return ret;
101364031f1SWei Liu }
102364031f1SWei Liu 
103364031f1SWei Liu int qemu_v9fs_synth_add_file(V9fsSynthNode *parent, int mode,
104364031f1SWei Liu                              const char *name, v9fs_synth_read read,
105364031f1SWei Liu                              v9fs_synth_write write, void *arg)
106364031f1SWei Liu {
107364031f1SWei Liu     int ret;
108364031f1SWei Liu     V9fsSynthNode *node, *tmp;
109364031f1SWei Liu 
110364031f1SWei Liu     if (!v9fs_synth_fs) {
111364031f1SWei Liu         return EAGAIN;
112364031f1SWei Liu     }
113364031f1SWei Liu     if (!name || (strlen(name) >= NAME_MAX)) {
114364031f1SWei Liu         return EINVAL;
115364031f1SWei Liu     }
116364031f1SWei Liu     if (!parent) {
117364031f1SWei Liu         parent = &v9fs_synth_root;
118364031f1SWei Liu     }
119364031f1SWei Liu 
120364031f1SWei Liu     qemu_mutex_lock(&v9fs_synth_mutex);
121364031f1SWei Liu     QLIST_FOREACH(tmp, &parent->child, sibling) {
122364031f1SWei Liu         if (!strcmp(tmp->name, name)) {
123364031f1SWei Liu             ret = EEXIST;
124364031f1SWei Liu             goto err_out;
125364031f1SWei Liu         }
126364031f1SWei Liu     }
127364031f1SWei Liu     /* Add file type and remove write bits */
128364031f1SWei Liu     mode = ((mode & 0777) | S_IFREG);
129364031f1SWei Liu     node = g_malloc0(sizeof(V9fsSynthNode));
130364031f1SWei Liu     node->attr         = &node->actual_attr;
131364031f1SWei Liu     node->attr->inode  = v9fs_synth_node_count++;
132364031f1SWei Liu     node->attr->nlink  = 1;
133364031f1SWei Liu     node->attr->read   = read;
134364031f1SWei Liu     node->attr->write  = write;
135364031f1SWei Liu     node->attr->mode   = mode;
136364031f1SWei Liu     node->private      = arg;
137364031f1SWei Liu     pstrcpy(node->name, sizeof(node->name), name);
138364031f1SWei Liu     QLIST_INSERT_HEAD_RCU(&parent->child, node, sibling);
139364031f1SWei Liu     ret = 0;
140364031f1SWei Liu err_out:
141364031f1SWei Liu     qemu_mutex_unlock(&v9fs_synth_mutex);
142364031f1SWei Liu     return ret;
143364031f1SWei Liu }
144364031f1SWei Liu 
145364031f1SWei Liu static void v9fs_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 
162364031f1SWei Liu static int v9fs_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 
167364031f1SWei Liu     v9fs_synth_fill_statbuf(node, stbuf);
168364031f1SWei Liu     return 0;
169364031f1SWei Liu }
170364031f1SWei Liu 
171364031f1SWei Liu static int v9fs_synth_fstat(FsContext *fs_ctx, int fid_type,
172364031f1SWei Liu                             V9fsFidOpenState *fs, struct stat *stbuf)
173364031f1SWei Liu {
174364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
175364031f1SWei Liu     v9fs_synth_fill_statbuf(synth_open->node, stbuf);
176364031f1SWei Liu     return 0;
177364031f1SWei Liu }
178364031f1SWei Liu 
179364031f1SWei Liu static int v9fs_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 
192364031f1SWei Liu static int v9fs_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 
203364031f1SWei Liu static off_t v9fs_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 
209364031f1SWei Liu static void v9fs_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 
215364031f1SWei Liu static void v9fs_synth_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
216364031f1SWei Liu {
217364031f1SWei Liu     v9fs_synth_seekdir(ctx, fs, 0);
218364031f1SWei Liu }
219364031f1SWei Liu 
220364031f1SWei Liu static void v9fs_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 
228364031f1SWei Liu static int v9fs_synth_get_dentry(V9fsSynthNode *dir, struct dirent *entry,
229364031f1SWei Liu                                  struct dirent **result, 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 */
245364031f1SWei Liu         *result = NULL;
246364031f1SWei Liu         return 0;
247364031f1SWei Liu     }
248364031f1SWei Liu     v9fs_synth_direntry(node, entry, off);
249364031f1SWei Liu     *result = entry;
250364031f1SWei Liu     return 0;
251364031f1SWei Liu }
252364031f1SWei Liu 
253364031f1SWei Liu static int v9fs_synth_readdir_r(FsContext *ctx, V9fsFidOpenState *fs,
254364031f1SWei Liu                                 struct dirent *entry, struct dirent **result)
255364031f1SWei Liu {
256364031f1SWei Liu     int ret;
257364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
258364031f1SWei Liu     V9fsSynthNode *node = synth_open->node;
259364031f1SWei Liu     ret = v9fs_synth_get_dentry(node, entry, result, synth_open->offset);
260364031f1SWei Liu     if (!ret && *result != NULL) {
261364031f1SWei Liu         synth_open->offset++;
262364031f1SWei Liu     }
263364031f1SWei Liu     return ret;
264364031f1SWei Liu }
265364031f1SWei Liu 
266364031f1SWei Liu static int v9fs_synth_open(FsContext *ctx, V9fsPath *fs_path,
267364031f1SWei Liu                            int flags, V9fsFidOpenState *fs)
268364031f1SWei Liu {
269364031f1SWei Liu     V9fsSynthOpenState *synth_open;
270364031f1SWei Liu     V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;
271364031f1SWei Liu 
272364031f1SWei Liu     synth_open = g_malloc(sizeof(*synth_open));
273364031f1SWei Liu     synth_open->node = node;
274364031f1SWei Liu     node->open_count++;
275364031f1SWei Liu     fs->private = synth_open;
276364031f1SWei Liu     return 0;
277364031f1SWei Liu }
278364031f1SWei Liu 
279364031f1SWei Liu static int v9fs_synth_open2(FsContext *fs_ctx, V9fsPath *dir_path,
280364031f1SWei Liu                             const char *name, int flags,
281364031f1SWei Liu                             FsCred *credp, V9fsFidOpenState *fs)
282364031f1SWei Liu {
283364031f1SWei Liu     errno = ENOSYS;
284364031f1SWei Liu     return -1;
285364031f1SWei Liu }
286364031f1SWei Liu 
287364031f1SWei Liu static int v9fs_synth_close(FsContext *ctx, V9fsFidOpenState *fs)
288364031f1SWei Liu {
289364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
290364031f1SWei Liu     V9fsSynthNode *node = synth_open->node;
291364031f1SWei Liu 
292364031f1SWei Liu     node->open_count--;
293364031f1SWei Liu     g_free(synth_open);
294364031f1SWei Liu     fs->private = NULL;
295364031f1SWei Liu     return 0;
296364031f1SWei Liu }
297364031f1SWei Liu 
298364031f1SWei Liu static ssize_t v9fs_synth_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
299364031f1SWei Liu                                   const struct iovec *iov,
300364031f1SWei Liu                                   int iovcnt, off_t offset)
301364031f1SWei Liu {
302364031f1SWei Liu     int i, count = 0, wcount;
303364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
304364031f1SWei Liu     V9fsSynthNode *node = synth_open->node;
305364031f1SWei Liu     if (!node->attr->write) {
306364031f1SWei Liu         errno = EPERM;
307364031f1SWei Liu         return -1;
308364031f1SWei Liu     }
309364031f1SWei Liu     for (i = 0; i < iovcnt; i++) {
310364031f1SWei Liu         wcount = node->attr->write(iov[i].iov_base, iov[i].iov_len,
311364031f1SWei Liu                                    offset, node->private);
312364031f1SWei Liu         offset += wcount;
313364031f1SWei Liu         count  += wcount;
314364031f1SWei Liu         /* If we wrote less than requested. we are done */
315364031f1SWei Liu         if (wcount < iov[i].iov_len) {
316364031f1SWei Liu             break;
317364031f1SWei Liu         }
318364031f1SWei Liu     }
319364031f1SWei Liu     return count;
320364031f1SWei Liu }
321364031f1SWei Liu 
322364031f1SWei Liu static ssize_t v9fs_synth_preadv(FsContext *ctx, V9fsFidOpenState *fs,
323364031f1SWei Liu                                  const struct iovec *iov,
324364031f1SWei Liu                                  int iovcnt, off_t offset)
325364031f1SWei Liu {
326364031f1SWei Liu     int i, count = 0, rcount;
327364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
328364031f1SWei Liu     V9fsSynthNode *node = synth_open->node;
329364031f1SWei Liu     if (!node->attr->read) {
330364031f1SWei Liu         errno = EPERM;
331364031f1SWei Liu         return -1;
332364031f1SWei Liu     }
333364031f1SWei Liu     for (i = 0; i < iovcnt; i++) {
334364031f1SWei Liu         rcount = node->attr->read(iov[i].iov_base, iov[i].iov_len,
335364031f1SWei Liu                                   offset, node->private);
336364031f1SWei Liu         offset += rcount;
337364031f1SWei Liu         count  += rcount;
338364031f1SWei Liu         /* If we read less than requested. we are done */
339364031f1SWei Liu         if (rcount < iov[i].iov_len) {
340364031f1SWei Liu             break;
341364031f1SWei Liu         }
342364031f1SWei Liu     }
343364031f1SWei Liu     return count;
344364031f1SWei Liu }
345364031f1SWei Liu 
346364031f1SWei Liu static int v9fs_synth_truncate(FsContext *ctx, V9fsPath *path, off_t offset)
347364031f1SWei Liu {
348364031f1SWei Liu     errno = ENOSYS;
349364031f1SWei Liu     return -1;
350364031f1SWei Liu }
351364031f1SWei Liu 
352364031f1SWei Liu static int v9fs_synth_chmod(FsContext *fs_ctx, V9fsPath *path, FsCred *credp)
353364031f1SWei Liu {
354364031f1SWei Liu     errno = EPERM;
355364031f1SWei Liu     return -1;
356364031f1SWei Liu }
357364031f1SWei Liu 
358364031f1SWei Liu static int v9fs_synth_mknod(FsContext *fs_ctx, V9fsPath *path,
359364031f1SWei Liu                        const char *buf, FsCred *credp)
360364031f1SWei Liu {
361364031f1SWei Liu     errno = EPERM;
362364031f1SWei Liu     return -1;
363364031f1SWei Liu }
364364031f1SWei Liu 
365364031f1SWei Liu static int v9fs_synth_mkdir(FsContext *fs_ctx, V9fsPath *path,
366364031f1SWei Liu                        const char *buf, FsCred *credp)
367364031f1SWei Liu {
368364031f1SWei Liu     errno = EPERM;
369364031f1SWei Liu     return -1;
370364031f1SWei Liu }
371364031f1SWei Liu 
372364031f1SWei Liu static ssize_t v9fs_synth_readlink(FsContext *fs_ctx, V9fsPath *path,
373364031f1SWei Liu                                    char *buf, size_t bufsz)
374364031f1SWei Liu {
375364031f1SWei Liu     errno = ENOSYS;
376364031f1SWei Liu     return -1;
377364031f1SWei Liu }
378364031f1SWei Liu 
379364031f1SWei Liu static int v9fs_synth_symlink(FsContext *fs_ctx, const char *oldpath,
380364031f1SWei Liu                               V9fsPath *newpath, const char *buf, FsCred *credp)
381364031f1SWei Liu {
382364031f1SWei Liu     errno = EPERM;
383364031f1SWei Liu     return -1;
384364031f1SWei Liu }
385364031f1SWei Liu 
386364031f1SWei Liu static int v9fs_synth_link(FsContext *fs_ctx, V9fsPath *oldpath,
387364031f1SWei Liu                            V9fsPath *newpath, const char *buf)
388364031f1SWei Liu {
389364031f1SWei Liu     errno = EPERM;
390364031f1SWei Liu     return -1;
391364031f1SWei Liu }
392364031f1SWei Liu 
393364031f1SWei Liu static int v9fs_synth_rename(FsContext *ctx, const char *oldpath,
394364031f1SWei Liu                              const char *newpath)
395364031f1SWei Liu {
396364031f1SWei Liu     errno = EPERM;
397364031f1SWei Liu     return -1;
398364031f1SWei Liu }
399364031f1SWei Liu 
400364031f1SWei Liu static int v9fs_synth_chown(FsContext *fs_ctx, V9fsPath *path, FsCred *credp)
401364031f1SWei Liu {
402364031f1SWei Liu     errno = EPERM;
403364031f1SWei Liu     return -1;
404364031f1SWei Liu }
405364031f1SWei Liu 
406364031f1SWei Liu static int v9fs_synth_utimensat(FsContext *fs_ctx, V9fsPath *path,
407364031f1SWei Liu                                 const struct timespec *buf)
408364031f1SWei Liu {
409364031f1SWei Liu     errno = EPERM;
410364031f1SWei Liu     return 0;
411364031f1SWei Liu }
412364031f1SWei Liu 
413364031f1SWei Liu static int v9fs_synth_remove(FsContext *ctx, const char *path)
414364031f1SWei Liu {
415364031f1SWei Liu     errno = EPERM;
416364031f1SWei Liu     return -1;
417364031f1SWei Liu }
418364031f1SWei Liu 
419364031f1SWei Liu static int v9fs_synth_fsync(FsContext *ctx, int fid_type,
420364031f1SWei Liu                             V9fsFidOpenState *fs, int datasync)
421364031f1SWei Liu {
422364031f1SWei Liu     errno = ENOSYS;
423364031f1SWei Liu     return 0;
424364031f1SWei Liu }
425364031f1SWei Liu 
426364031f1SWei Liu static int v9fs_synth_statfs(FsContext *s, V9fsPath *fs_path,
427364031f1SWei Liu                              struct statfs *stbuf)
428364031f1SWei Liu {
429364031f1SWei Liu     stbuf->f_type = 0xABCD;
430364031f1SWei Liu     stbuf->f_bsize = 512;
431364031f1SWei Liu     stbuf->f_blocks = 0;
432364031f1SWei Liu     stbuf->f_files = v9fs_synth_node_count;
433364031f1SWei Liu     stbuf->f_namelen = NAME_MAX;
434364031f1SWei Liu     return 0;
435364031f1SWei Liu }
436364031f1SWei Liu 
437364031f1SWei Liu static ssize_t v9fs_synth_lgetxattr(FsContext *ctx, V9fsPath *path,
438364031f1SWei Liu                                     const char *name, void *value, size_t size)
439364031f1SWei Liu {
440364031f1SWei Liu     errno = ENOTSUP;
441364031f1SWei Liu     return -1;
442364031f1SWei Liu }
443364031f1SWei Liu 
444364031f1SWei Liu static ssize_t v9fs_synth_llistxattr(FsContext *ctx, V9fsPath *path,
445364031f1SWei Liu                                      void *value, size_t size)
446364031f1SWei Liu {
447364031f1SWei Liu     errno = ENOTSUP;
448364031f1SWei Liu     return -1;
449364031f1SWei Liu }
450364031f1SWei Liu 
451364031f1SWei Liu static int v9fs_synth_lsetxattr(FsContext *ctx, V9fsPath *path,
452364031f1SWei Liu                                 const char *name, void *value,
453364031f1SWei Liu                                 size_t size, int flags)
454364031f1SWei Liu {
455364031f1SWei Liu     errno = ENOTSUP;
456364031f1SWei Liu     return -1;
457364031f1SWei Liu }
458364031f1SWei Liu 
459364031f1SWei Liu static int v9fs_synth_lremovexattr(FsContext *ctx,
460364031f1SWei Liu                                    V9fsPath *path, const char *name)
461364031f1SWei Liu {
462364031f1SWei Liu     errno = ENOTSUP;
463364031f1SWei Liu     return -1;
464364031f1SWei Liu }
465364031f1SWei Liu 
466364031f1SWei Liu static int v9fs_synth_name_to_path(FsContext *ctx, V9fsPath *dir_path,
467364031f1SWei Liu                                    const char *name, V9fsPath *target)
468364031f1SWei Liu {
469364031f1SWei Liu     V9fsSynthNode *node;
470364031f1SWei Liu     V9fsSynthNode *dir_node;
471364031f1SWei Liu 
472364031f1SWei Liu     /* "." and ".." are not allowed */
473364031f1SWei Liu     if (!strcmp(name, ".") || !strcmp(name, "..")) {
474364031f1SWei Liu         errno = EINVAL;
475364031f1SWei Liu         return -1;
476364031f1SWei Liu 
477364031f1SWei Liu     }
478364031f1SWei Liu     if (!dir_path) {
479364031f1SWei Liu         dir_node = &v9fs_synth_root;
480364031f1SWei Liu     } else {
481364031f1SWei Liu         dir_node = *(V9fsSynthNode **)dir_path->data;
482364031f1SWei Liu     }
483364031f1SWei Liu     if (!strcmp(name, "/")) {
484364031f1SWei Liu         node = dir_node;
485364031f1SWei Liu         goto out;
486364031f1SWei Liu     }
487364031f1SWei Liu     /* search for the name in the childern */
488364031f1SWei Liu     rcu_read_lock();
489364031f1SWei Liu     QLIST_FOREACH(node, &dir_node->child, sibling) {
490364031f1SWei Liu         if (!strcmp(node->name, name)) {
491364031f1SWei Liu             break;
492364031f1SWei Liu         }
493364031f1SWei Liu     }
494364031f1SWei Liu     rcu_read_unlock();
495364031f1SWei Liu 
496364031f1SWei Liu     if (!node) {
497364031f1SWei Liu         errno = ENOENT;
498364031f1SWei Liu         return -1;
499364031f1SWei Liu     }
500364031f1SWei Liu out:
501364031f1SWei Liu     /* Copy the node pointer to fid */
502364031f1SWei Liu     target->data = g_malloc(sizeof(void *));
503364031f1SWei Liu     memcpy(target->data, &node, sizeof(void *));
504364031f1SWei Liu     target->size = sizeof(void *);
505364031f1SWei Liu     return 0;
506364031f1SWei Liu }
507364031f1SWei Liu 
508364031f1SWei Liu static int v9fs_synth_renameat(FsContext *ctx, V9fsPath *olddir,
509364031f1SWei Liu                                const char *old_name, V9fsPath *newdir,
510364031f1SWei Liu                                const char *new_name)
511364031f1SWei Liu {
512364031f1SWei Liu     errno = EPERM;
513364031f1SWei Liu     return -1;
514364031f1SWei Liu }
515364031f1SWei Liu 
516364031f1SWei Liu static int v9fs_synth_unlinkat(FsContext *ctx, V9fsPath *dir,
517364031f1SWei Liu                                const char *name, int flags)
518364031f1SWei Liu {
519364031f1SWei Liu     errno = EPERM;
520364031f1SWei Liu     return -1;
521364031f1SWei Liu }
522364031f1SWei Liu 
523364031f1SWei Liu static int v9fs_synth_init(FsContext *ctx)
524364031f1SWei Liu {
525364031f1SWei Liu     QLIST_INIT(&v9fs_synth_root.child);
526364031f1SWei Liu     qemu_mutex_init(&v9fs_synth_mutex);
527364031f1SWei Liu 
528364031f1SWei Liu     /* Add "." and ".." entries for root */
529364031f1SWei Liu     v9fs_add_dir_node(&v9fs_synth_root, v9fs_synth_root.attr->mode,
530364031f1SWei Liu                       "..", v9fs_synth_root.attr, v9fs_synth_root.attr->inode);
531364031f1SWei Liu     v9fs_add_dir_node(&v9fs_synth_root, v9fs_synth_root.attr->mode,
532364031f1SWei Liu                       ".", v9fs_synth_root.attr, v9fs_synth_root.attr->inode);
533364031f1SWei Liu 
534364031f1SWei Liu     /* Mark the subsystem is ready for use */
535364031f1SWei Liu     v9fs_synth_fs = 1;
536364031f1SWei Liu     return 0;
537364031f1SWei Liu }
538364031f1SWei Liu 
539364031f1SWei Liu FileOperations synth_ops = {
540364031f1SWei Liu     .init         = v9fs_synth_init,
541364031f1SWei Liu     .lstat        = v9fs_synth_lstat,
542364031f1SWei Liu     .readlink     = v9fs_synth_readlink,
543364031f1SWei Liu     .close        = v9fs_synth_close,
544364031f1SWei Liu     .closedir     = v9fs_synth_closedir,
545364031f1SWei Liu     .open         = v9fs_synth_open,
546364031f1SWei Liu     .opendir      = v9fs_synth_opendir,
547364031f1SWei Liu     .rewinddir    = v9fs_synth_rewinddir,
548364031f1SWei Liu     .telldir      = v9fs_synth_telldir,
549364031f1SWei Liu     .readdir_r    = v9fs_synth_readdir_r,
550364031f1SWei Liu     .seekdir      = v9fs_synth_seekdir,
551364031f1SWei Liu     .preadv       = v9fs_synth_preadv,
552364031f1SWei Liu     .pwritev      = v9fs_synth_pwritev,
553364031f1SWei Liu     .chmod        = v9fs_synth_chmod,
554364031f1SWei Liu     .mknod        = v9fs_synth_mknod,
555364031f1SWei Liu     .mkdir        = v9fs_synth_mkdir,
556364031f1SWei Liu     .fstat        = v9fs_synth_fstat,
557364031f1SWei Liu     .open2        = v9fs_synth_open2,
558364031f1SWei Liu     .symlink      = v9fs_synth_symlink,
559364031f1SWei Liu     .link         = v9fs_synth_link,
560364031f1SWei Liu     .truncate     = v9fs_synth_truncate,
561364031f1SWei Liu     .rename       = v9fs_synth_rename,
562364031f1SWei Liu     .chown        = v9fs_synth_chown,
563364031f1SWei Liu     .utimensat    = v9fs_synth_utimensat,
564364031f1SWei Liu     .remove       = v9fs_synth_remove,
565364031f1SWei Liu     .fsync        = v9fs_synth_fsync,
566364031f1SWei Liu     .statfs       = v9fs_synth_statfs,
567364031f1SWei Liu     .lgetxattr    = v9fs_synth_lgetxattr,
568364031f1SWei Liu     .llistxattr   = v9fs_synth_llistxattr,
569364031f1SWei Liu     .lsetxattr    = v9fs_synth_lsetxattr,
570364031f1SWei Liu     .lremovexattr = v9fs_synth_lremovexattr,
571364031f1SWei Liu     .name_to_path = v9fs_synth_name_to_path,
572364031f1SWei Liu     .renameat     = v9fs_synth_renameat,
573364031f1SWei Liu     .unlinkat     = v9fs_synth_unlinkat,
574364031f1SWei Liu };
575