xref: /openbmc/qemu/hw/9pfs/9p-synth.c (revision aae91ad9ae0677395366ff1f7a3e8763fd8470f2)
1364031f1SWei Liu /*
2*aae91ad9SGreg 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 */
24364031f1SWei Liu static V9fsSynthNode v9fs_synth_root = {
25364031f1SWei Liu     .name = "/",
26364031f1SWei Liu     .actual_attr = {
27364031f1SWei Liu         .mode = 0555 | S_IFDIR,
28364031f1SWei Liu         .nlink = 1,
29364031f1SWei Liu     },
30364031f1SWei Liu     .attr = &v9fs_synth_root.actual_attr,
31364031f1SWei Liu };
32364031f1SWei Liu 
33364031f1SWei Liu static QemuMutex  v9fs_synth_mutex;
34364031f1SWei Liu static int v9fs_synth_node_count;
35364031f1SWei Liu /* set to 1 when the synth fs is ready */
36364031f1SWei Liu static int v9fs_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 
72364031f1SWei Liu     if (!v9fs_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) {
79364031f1SWei Liu         parent = &v9fs_synth_root;
80364031f1SWei Liu     }
81364031f1SWei Liu     qemu_mutex_lock(&v9fs_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 */
89364031f1SWei Liu     node = v9fs_add_dir_node(parent, mode, name, NULL, v9fs_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:
97364031f1SWei Liu     qemu_mutex_unlock(&v9fs_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 
108364031f1SWei Liu     if (!v9fs_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) {
115364031f1SWei Liu         parent = &v9fs_synth_root;
116364031f1SWei Liu     }
117364031f1SWei Liu 
118364031f1SWei Liu     qemu_mutex_lock(&v9fs_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;
129364031f1SWei Liu     node->attr->inode  = v9fs_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:
139364031f1SWei Liu     qemu_mutex_unlock(&v9fs_synth_mutex);
140364031f1SWei Liu     return ret;
141364031f1SWei Liu }
142364031f1SWei Liu 
143364031f1SWei Liu static void v9fs_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 
160364031f1SWei Liu static int v9fs_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 
165364031f1SWei Liu     v9fs_synth_fill_statbuf(node, stbuf);
166364031f1SWei Liu     return 0;
167364031f1SWei Liu }
168364031f1SWei Liu 
169364031f1SWei Liu static int v9fs_synth_fstat(FsContext *fs_ctx, int fid_type,
170364031f1SWei Liu                             V9fsFidOpenState *fs, struct stat *stbuf)
171364031f1SWei Liu {
172364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
173364031f1SWei Liu     v9fs_synth_fill_statbuf(synth_open->node, stbuf);
174364031f1SWei Liu     return 0;
175364031f1SWei Liu }
176364031f1SWei Liu 
177364031f1SWei Liu static int v9fs_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 
190364031f1SWei Liu static int v9fs_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 
201364031f1SWei Liu static off_t v9fs_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 
207364031f1SWei Liu static void v9fs_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 
213364031f1SWei Liu static void v9fs_synth_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
214364031f1SWei Liu {
215364031f1SWei Liu     v9fs_synth_seekdir(ctx, fs, 0);
216364031f1SWei Liu }
217364031f1SWei Liu 
218364031f1SWei Liu static void v9fs_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 
226364031f1SWei Liu static int v9fs_synth_get_dentry(V9fsSynthNode *dir, struct dirent *entry,
227364031f1SWei Liu                                  struct dirent **result, 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 */
243364031f1SWei Liu         *result = NULL;
244364031f1SWei Liu         return 0;
245364031f1SWei Liu     }
246364031f1SWei Liu     v9fs_synth_direntry(node, entry, off);
247364031f1SWei Liu     *result = entry;
248364031f1SWei Liu     return 0;
249364031f1SWei Liu }
250364031f1SWei Liu 
251364031f1SWei Liu static int v9fs_synth_readdir_r(FsContext *ctx, V9fsFidOpenState *fs,
252364031f1SWei Liu                                 struct dirent *entry, struct dirent **result)
253364031f1SWei Liu {
254364031f1SWei Liu     int ret;
255364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
256364031f1SWei Liu     V9fsSynthNode *node = synth_open->node;
257364031f1SWei Liu     ret = v9fs_synth_get_dentry(node, entry, result, synth_open->offset);
258364031f1SWei Liu     if (!ret && *result != NULL) {
259364031f1SWei Liu         synth_open->offset++;
260364031f1SWei Liu     }
261364031f1SWei Liu     return ret;
262364031f1SWei Liu }
263364031f1SWei Liu 
264364031f1SWei Liu static int v9fs_synth_open(FsContext *ctx, V9fsPath *fs_path,
265364031f1SWei Liu                            int flags, V9fsFidOpenState *fs)
266364031f1SWei Liu {
267364031f1SWei Liu     V9fsSynthOpenState *synth_open;
268364031f1SWei Liu     V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;
269364031f1SWei Liu 
270364031f1SWei Liu     synth_open = g_malloc(sizeof(*synth_open));
271364031f1SWei Liu     synth_open->node = node;
272364031f1SWei Liu     node->open_count++;
273364031f1SWei Liu     fs->private = synth_open;
274364031f1SWei Liu     return 0;
275364031f1SWei Liu }
276364031f1SWei Liu 
277364031f1SWei Liu static int v9fs_synth_open2(FsContext *fs_ctx, V9fsPath *dir_path,
278364031f1SWei Liu                             const char *name, int flags,
279364031f1SWei Liu                             FsCred *credp, V9fsFidOpenState *fs)
280364031f1SWei Liu {
281364031f1SWei Liu     errno = ENOSYS;
282364031f1SWei Liu     return -1;
283364031f1SWei Liu }
284364031f1SWei Liu 
285364031f1SWei Liu static int v9fs_synth_close(FsContext *ctx, V9fsFidOpenState *fs)
286364031f1SWei Liu {
287364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
288364031f1SWei Liu     V9fsSynthNode *node = synth_open->node;
289364031f1SWei Liu 
290364031f1SWei Liu     node->open_count--;
291364031f1SWei Liu     g_free(synth_open);
292364031f1SWei Liu     fs->private = NULL;
293364031f1SWei Liu     return 0;
294364031f1SWei Liu }
295364031f1SWei Liu 
296364031f1SWei Liu static ssize_t v9fs_synth_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
297364031f1SWei Liu                                   const struct iovec *iov,
298364031f1SWei Liu                                   int iovcnt, off_t offset)
299364031f1SWei Liu {
300364031f1SWei Liu     int i, count = 0, wcount;
301364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
302364031f1SWei Liu     V9fsSynthNode *node = synth_open->node;
303364031f1SWei Liu     if (!node->attr->write) {
304364031f1SWei Liu         errno = EPERM;
305364031f1SWei Liu         return -1;
306364031f1SWei Liu     }
307364031f1SWei Liu     for (i = 0; i < iovcnt; i++) {
308364031f1SWei Liu         wcount = node->attr->write(iov[i].iov_base, iov[i].iov_len,
309364031f1SWei Liu                                    offset, node->private);
310364031f1SWei Liu         offset += wcount;
311364031f1SWei Liu         count  += wcount;
312364031f1SWei Liu         /* If we wrote less than requested. we are done */
313364031f1SWei Liu         if (wcount < iov[i].iov_len) {
314364031f1SWei Liu             break;
315364031f1SWei Liu         }
316364031f1SWei Liu     }
317364031f1SWei Liu     return count;
318364031f1SWei Liu }
319364031f1SWei Liu 
320364031f1SWei Liu static ssize_t v9fs_synth_preadv(FsContext *ctx, V9fsFidOpenState *fs,
321364031f1SWei Liu                                  const struct iovec *iov,
322364031f1SWei Liu                                  int iovcnt, off_t offset)
323364031f1SWei Liu {
324364031f1SWei Liu     int i, count = 0, rcount;
325364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
326364031f1SWei Liu     V9fsSynthNode *node = synth_open->node;
327364031f1SWei Liu     if (!node->attr->read) {
328364031f1SWei Liu         errno = EPERM;
329364031f1SWei Liu         return -1;
330364031f1SWei Liu     }
331364031f1SWei Liu     for (i = 0; i < iovcnt; i++) {
332364031f1SWei Liu         rcount = node->attr->read(iov[i].iov_base, iov[i].iov_len,
333364031f1SWei Liu                                   offset, node->private);
334364031f1SWei Liu         offset += rcount;
335364031f1SWei Liu         count  += rcount;
336364031f1SWei Liu         /* If we read less than requested. we are done */
337364031f1SWei Liu         if (rcount < iov[i].iov_len) {
338364031f1SWei Liu             break;
339364031f1SWei Liu         }
340364031f1SWei Liu     }
341364031f1SWei Liu     return count;
342364031f1SWei Liu }
343364031f1SWei Liu 
344364031f1SWei Liu static int v9fs_synth_truncate(FsContext *ctx, V9fsPath *path, off_t offset)
345364031f1SWei Liu {
346364031f1SWei Liu     errno = ENOSYS;
347364031f1SWei Liu     return -1;
348364031f1SWei Liu }
349364031f1SWei Liu 
350364031f1SWei Liu static int v9fs_synth_chmod(FsContext *fs_ctx, V9fsPath *path, FsCred *credp)
351364031f1SWei Liu {
352364031f1SWei Liu     errno = EPERM;
353364031f1SWei Liu     return -1;
354364031f1SWei Liu }
355364031f1SWei Liu 
356364031f1SWei Liu static int v9fs_synth_mknod(FsContext *fs_ctx, V9fsPath *path,
357364031f1SWei Liu                        const char *buf, FsCred *credp)
358364031f1SWei Liu {
359364031f1SWei Liu     errno = EPERM;
360364031f1SWei Liu     return -1;
361364031f1SWei Liu }
362364031f1SWei Liu 
363364031f1SWei Liu static int v9fs_synth_mkdir(FsContext *fs_ctx, V9fsPath *path,
364364031f1SWei Liu                        const char *buf, FsCred *credp)
365364031f1SWei Liu {
366364031f1SWei Liu     errno = EPERM;
367364031f1SWei Liu     return -1;
368364031f1SWei Liu }
369364031f1SWei Liu 
370364031f1SWei Liu static ssize_t v9fs_synth_readlink(FsContext *fs_ctx, V9fsPath *path,
371364031f1SWei Liu                                    char *buf, size_t bufsz)
372364031f1SWei Liu {
373364031f1SWei Liu     errno = ENOSYS;
374364031f1SWei Liu     return -1;
375364031f1SWei Liu }
376364031f1SWei Liu 
377364031f1SWei Liu static int v9fs_synth_symlink(FsContext *fs_ctx, const char *oldpath,
378364031f1SWei Liu                               V9fsPath *newpath, const char *buf, FsCred *credp)
379364031f1SWei Liu {
380364031f1SWei Liu     errno = EPERM;
381364031f1SWei Liu     return -1;
382364031f1SWei Liu }
383364031f1SWei Liu 
384364031f1SWei Liu static int v9fs_synth_link(FsContext *fs_ctx, V9fsPath *oldpath,
385364031f1SWei Liu                            V9fsPath *newpath, const char *buf)
386364031f1SWei Liu {
387364031f1SWei Liu     errno = EPERM;
388364031f1SWei Liu     return -1;
389364031f1SWei Liu }
390364031f1SWei Liu 
391364031f1SWei Liu static int v9fs_synth_rename(FsContext *ctx, const char *oldpath,
392364031f1SWei Liu                              const char *newpath)
393364031f1SWei Liu {
394364031f1SWei Liu     errno = EPERM;
395364031f1SWei Liu     return -1;
396364031f1SWei Liu }
397364031f1SWei Liu 
398364031f1SWei Liu static int v9fs_synth_chown(FsContext *fs_ctx, V9fsPath *path, FsCred *credp)
399364031f1SWei Liu {
400364031f1SWei Liu     errno = EPERM;
401364031f1SWei Liu     return -1;
402364031f1SWei Liu }
403364031f1SWei Liu 
404364031f1SWei Liu static int v9fs_synth_utimensat(FsContext *fs_ctx, V9fsPath *path,
405364031f1SWei Liu                                 const struct timespec *buf)
406364031f1SWei Liu {
407364031f1SWei Liu     errno = EPERM;
408364031f1SWei Liu     return 0;
409364031f1SWei Liu }
410364031f1SWei Liu 
411364031f1SWei Liu static int v9fs_synth_remove(FsContext *ctx, const char *path)
412364031f1SWei Liu {
413364031f1SWei Liu     errno = EPERM;
414364031f1SWei Liu     return -1;
415364031f1SWei Liu }
416364031f1SWei Liu 
417364031f1SWei Liu static int v9fs_synth_fsync(FsContext *ctx, int fid_type,
418364031f1SWei Liu                             V9fsFidOpenState *fs, int datasync)
419364031f1SWei Liu {
420364031f1SWei Liu     errno = ENOSYS;
421364031f1SWei Liu     return 0;
422364031f1SWei Liu }
423364031f1SWei Liu 
424364031f1SWei Liu static int v9fs_synth_statfs(FsContext *s, V9fsPath *fs_path,
425364031f1SWei Liu                              struct statfs *stbuf)
426364031f1SWei Liu {
427364031f1SWei Liu     stbuf->f_type = 0xABCD;
428364031f1SWei Liu     stbuf->f_bsize = 512;
429364031f1SWei Liu     stbuf->f_blocks = 0;
430364031f1SWei Liu     stbuf->f_files = v9fs_synth_node_count;
431364031f1SWei Liu     stbuf->f_namelen = NAME_MAX;
432364031f1SWei Liu     return 0;
433364031f1SWei Liu }
434364031f1SWei Liu 
435364031f1SWei Liu static ssize_t v9fs_synth_lgetxattr(FsContext *ctx, V9fsPath *path,
436364031f1SWei Liu                                     const char *name, void *value, size_t size)
437364031f1SWei Liu {
438364031f1SWei Liu     errno = ENOTSUP;
439364031f1SWei Liu     return -1;
440364031f1SWei Liu }
441364031f1SWei Liu 
442364031f1SWei Liu static ssize_t v9fs_synth_llistxattr(FsContext *ctx, V9fsPath *path,
443364031f1SWei Liu                                      void *value, size_t size)
444364031f1SWei Liu {
445364031f1SWei Liu     errno = ENOTSUP;
446364031f1SWei Liu     return -1;
447364031f1SWei Liu }
448364031f1SWei Liu 
449364031f1SWei Liu static int v9fs_synth_lsetxattr(FsContext *ctx, V9fsPath *path,
450364031f1SWei Liu                                 const char *name, void *value,
451364031f1SWei Liu                                 size_t size, int flags)
452364031f1SWei Liu {
453364031f1SWei Liu     errno = ENOTSUP;
454364031f1SWei Liu     return -1;
455364031f1SWei Liu }
456364031f1SWei Liu 
457364031f1SWei Liu static int v9fs_synth_lremovexattr(FsContext *ctx,
458364031f1SWei Liu                                    V9fsPath *path, const char *name)
459364031f1SWei Liu {
460364031f1SWei Liu     errno = ENOTSUP;
461364031f1SWei Liu     return -1;
462364031f1SWei Liu }
463364031f1SWei Liu 
464364031f1SWei Liu static int v9fs_synth_name_to_path(FsContext *ctx, V9fsPath *dir_path,
465364031f1SWei Liu                                    const char *name, V9fsPath *target)
466364031f1SWei Liu {
467364031f1SWei Liu     V9fsSynthNode *node;
468364031f1SWei Liu     V9fsSynthNode *dir_node;
469364031f1SWei Liu 
470364031f1SWei Liu     /* "." and ".." are not allowed */
471364031f1SWei Liu     if (!strcmp(name, ".") || !strcmp(name, "..")) {
472364031f1SWei Liu         errno = EINVAL;
473364031f1SWei Liu         return -1;
474364031f1SWei Liu 
475364031f1SWei Liu     }
476364031f1SWei Liu     if (!dir_path) {
477364031f1SWei Liu         dir_node = &v9fs_synth_root;
478364031f1SWei Liu     } else {
479364031f1SWei Liu         dir_node = *(V9fsSynthNode **)dir_path->data;
480364031f1SWei Liu     }
481364031f1SWei Liu     if (!strcmp(name, "/")) {
482364031f1SWei Liu         node = dir_node;
483364031f1SWei Liu         goto out;
484364031f1SWei Liu     }
485364031f1SWei Liu     /* search for the name in the childern */
486364031f1SWei Liu     rcu_read_lock();
487364031f1SWei Liu     QLIST_FOREACH(node, &dir_node->child, sibling) {
488364031f1SWei Liu         if (!strcmp(node->name, name)) {
489364031f1SWei Liu             break;
490364031f1SWei Liu         }
491364031f1SWei Liu     }
492364031f1SWei Liu     rcu_read_unlock();
493364031f1SWei Liu 
494364031f1SWei Liu     if (!node) {
495364031f1SWei Liu         errno = ENOENT;
496364031f1SWei Liu         return -1;
497364031f1SWei Liu     }
498364031f1SWei Liu out:
499364031f1SWei Liu     /* Copy the node pointer to fid */
500364031f1SWei Liu     target->data = g_malloc(sizeof(void *));
501364031f1SWei Liu     memcpy(target->data, &node, sizeof(void *));
502364031f1SWei Liu     target->size = sizeof(void *);
503364031f1SWei Liu     return 0;
504364031f1SWei Liu }
505364031f1SWei Liu 
506364031f1SWei Liu static int v9fs_synth_renameat(FsContext *ctx, V9fsPath *olddir,
507364031f1SWei Liu                                const char *old_name, V9fsPath *newdir,
508364031f1SWei Liu                                const char *new_name)
509364031f1SWei Liu {
510364031f1SWei Liu     errno = EPERM;
511364031f1SWei Liu     return -1;
512364031f1SWei Liu }
513364031f1SWei Liu 
514364031f1SWei Liu static int v9fs_synth_unlinkat(FsContext *ctx, V9fsPath *dir,
515364031f1SWei Liu                                const char *name, int flags)
516364031f1SWei Liu {
517364031f1SWei Liu     errno = EPERM;
518364031f1SWei Liu     return -1;
519364031f1SWei Liu }
520364031f1SWei Liu 
521364031f1SWei Liu static int v9fs_synth_init(FsContext *ctx)
522364031f1SWei Liu {
523364031f1SWei Liu     QLIST_INIT(&v9fs_synth_root.child);
524364031f1SWei Liu     qemu_mutex_init(&v9fs_synth_mutex);
525364031f1SWei Liu 
526364031f1SWei Liu     /* Add "." and ".." entries for root */
527364031f1SWei Liu     v9fs_add_dir_node(&v9fs_synth_root, v9fs_synth_root.attr->mode,
528364031f1SWei Liu                       "..", v9fs_synth_root.attr, v9fs_synth_root.attr->inode);
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 
532364031f1SWei Liu     /* Mark the subsystem is ready for use */
533364031f1SWei Liu     v9fs_synth_fs = 1;
534364031f1SWei Liu     return 0;
535364031f1SWei Liu }
536364031f1SWei Liu 
537364031f1SWei Liu FileOperations synth_ops = {
538364031f1SWei Liu     .init         = v9fs_synth_init,
539364031f1SWei Liu     .lstat        = v9fs_synth_lstat,
540364031f1SWei Liu     .readlink     = v9fs_synth_readlink,
541364031f1SWei Liu     .close        = v9fs_synth_close,
542364031f1SWei Liu     .closedir     = v9fs_synth_closedir,
543364031f1SWei Liu     .open         = v9fs_synth_open,
544364031f1SWei Liu     .opendir      = v9fs_synth_opendir,
545364031f1SWei Liu     .rewinddir    = v9fs_synth_rewinddir,
546364031f1SWei Liu     .telldir      = v9fs_synth_telldir,
547364031f1SWei Liu     .readdir_r    = v9fs_synth_readdir_r,
548364031f1SWei Liu     .seekdir      = v9fs_synth_seekdir,
549364031f1SWei Liu     .preadv       = v9fs_synth_preadv,
550364031f1SWei Liu     .pwritev      = v9fs_synth_pwritev,
551364031f1SWei Liu     .chmod        = v9fs_synth_chmod,
552364031f1SWei Liu     .mknod        = v9fs_synth_mknod,
553364031f1SWei Liu     .mkdir        = v9fs_synth_mkdir,
554364031f1SWei Liu     .fstat        = v9fs_synth_fstat,
555364031f1SWei Liu     .open2        = v9fs_synth_open2,
556364031f1SWei Liu     .symlink      = v9fs_synth_symlink,
557364031f1SWei Liu     .link         = v9fs_synth_link,
558364031f1SWei Liu     .truncate     = v9fs_synth_truncate,
559364031f1SWei Liu     .rename       = v9fs_synth_rename,
560364031f1SWei Liu     .chown        = v9fs_synth_chown,
561364031f1SWei Liu     .utimensat    = v9fs_synth_utimensat,
562364031f1SWei Liu     .remove       = v9fs_synth_remove,
563364031f1SWei Liu     .fsync        = v9fs_synth_fsync,
564364031f1SWei Liu     .statfs       = v9fs_synth_statfs,
565364031f1SWei Liu     .lgetxattr    = v9fs_synth_lgetxattr,
566364031f1SWei Liu     .llistxattr   = v9fs_synth_llistxattr,
567364031f1SWei Liu     .lsetxattr    = v9fs_synth_lsetxattr,
568364031f1SWei Liu     .lremovexattr = v9fs_synth_lremovexattr,
569364031f1SWei Liu     .name_to_path = v9fs_synth_name_to_path,
570364031f1SWei Liu     .renameat     = v9fs_synth_renameat,
571364031f1SWei Liu     .unlinkat     = v9fs_synth_unlinkat,
572364031f1SWei Liu };
573