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