xref: /openbmc/qemu/hw/9pfs/9p-synth.c (revision e4fd889f51094a8e76274ca1e9e0ed70375166f0)
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"
222893ddd5SGreg Kurz #include "sysemu/qtest.h"
23364031f1SWei Liu 
24364031f1SWei Liu /* Root node for synth file system */
25b05528b5SGreg Kurz static V9fsSynthNode synth_root = {
26364031f1SWei Liu     .name = "/",
27364031f1SWei Liu     .actual_attr = {
28364031f1SWei Liu         .mode = 0555 | S_IFDIR,
29364031f1SWei Liu         .nlink = 1,
30364031f1SWei Liu     },
31b05528b5SGreg Kurz     .attr = &synth_root.actual_attr,
32364031f1SWei Liu };
33364031f1SWei Liu 
34b05528b5SGreg Kurz static QemuMutex  synth_mutex;
35b05528b5SGreg Kurz static int synth_node_count;
36364031f1SWei Liu /* set to 1 when the synth fs is ready */
37b05528b5SGreg Kurz static int 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 
73b05528b5SGreg Kurz     if (!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) {
80b05528b5SGreg Kurz         parent = &synth_root;
81364031f1SWei Liu     }
82*e4fd889fSMahmoud Mandour     QEMU_LOCK_GUARD(&synth_mutex);
83364031f1SWei Liu     QLIST_FOREACH(tmp, &parent->child, sibling) {
84364031f1SWei Liu         if (!strcmp(tmp->name, name)) {
85364031f1SWei Liu             ret = EEXIST;
86*e4fd889fSMahmoud Mandour             return ret;
87364031f1SWei Liu         }
88364031f1SWei Liu     }
89364031f1SWei Liu     /* Add the name */
90b05528b5SGreg Kurz     node = v9fs_add_dir_node(parent, mode, name, NULL, 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     return ret;
98364031f1SWei Liu }
99364031f1SWei Liu 
100364031f1SWei Liu int qemu_v9fs_synth_add_file(V9fsSynthNode *parent, int mode,
101364031f1SWei Liu                              const char *name, v9fs_synth_read read,
102364031f1SWei Liu                              v9fs_synth_write write, void *arg)
103364031f1SWei Liu {
104364031f1SWei Liu     int ret;
105364031f1SWei Liu     V9fsSynthNode *node, *tmp;
106364031f1SWei Liu 
107b05528b5SGreg Kurz     if (!synth_fs) {
108364031f1SWei Liu         return EAGAIN;
109364031f1SWei Liu     }
110364031f1SWei Liu     if (!name || (strlen(name) >= NAME_MAX)) {
111364031f1SWei Liu         return EINVAL;
112364031f1SWei Liu     }
113364031f1SWei Liu     if (!parent) {
114b05528b5SGreg Kurz         parent = &synth_root;
115364031f1SWei Liu     }
116364031f1SWei Liu 
117*e4fd889fSMahmoud Mandour     QEMU_LOCK_GUARD(&synth_mutex);
118364031f1SWei Liu     QLIST_FOREACH(tmp, &parent->child, sibling) {
119364031f1SWei Liu         if (!strcmp(tmp->name, name)) {
120364031f1SWei Liu             ret = EEXIST;
121*e4fd889fSMahmoud Mandour             return ret;
122364031f1SWei Liu         }
123364031f1SWei Liu     }
124364031f1SWei Liu     /* Add file type and remove write bits */
125364031f1SWei Liu     mode = ((mode & 0777) | S_IFREG);
126364031f1SWei Liu     node = g_malloc0(sizeof(V9fsSynthNode));
127364031f1SWei Liu     node->attr         = &node->actual_attr;
128b05528b5SGreg Kurz     node->attr->inode  = synth_node_count++;
129364031f1SWei Liu     node->attr->nlink  = 1;
130364031f1SWei Liu     node->attr->read   = read;
131364031f1SWei Liu     node->attr->write  = write;
132364031f1SWei Liu     node->attr->mode   = mode;
133364031f1SWei Liu     node->private      = arg;
134364031f1SWei Liu     pstrcpy(node->name, sizeof(node->name), name);
135364031f1SWei Liu     QLIST_INSERT_HEAD_RCU(&parent->child, node, sibling);
136364031f1SWei Liu     ret = 0;
137364031f1SWei Liu     return ret;
138364031f1SWei Liu }
139364031f1SWei Liu 
140b05528b5SGreg Kurz static void synth_fill_statbuf(V9fsSynthNode *node, struct stat *stbuf)
141364031f1SWei Liu {
142364031f1SWei Liu     stbuf->st_dev = 0;
143364031f1SWei Liu     stbuf->st_ino = node->attr->inode;
144364031f1SWei Liu     stbuf->st_mode = node->attr->mode;
145364031f1SWei Liu     stbuf->st_nlink = node->attr->nlink;
146364031f1SWei Liu     stbuf->st_uid = 0;
147364031f1SWei Liu     stbuf->st_gid = 0;
148364031f1SWei Liu     stbuf->st_rdev = 0;
149364031f1SWei Liu     stbuf->st_size = 0;
150364031f1SWei Liu     stbuf->st_blksize = 0;
151364031f1SWei Liu     stbuf->st_blocks = 0;
152364031f1SWei Liu     stbuf->st_atime = 0;
153364031f1SWei Liu     stbuf->st_mtime = 0;
154364031f1SWei Liu     stbuf->st_ctime = 0;
155364031f1SWei Liu }
156364031f1SWei Liu 
157b05528b5SGreg Kurz static int synth_lstat(FsContext *fs_ctx,
158364031f1SWei Liu                             V9fsPath *fs_path, struct stat *stbuf)
159364031f1SWei Liu {
160364031f1SWei Liu     V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;
161364031f1SWei Liu 
162b05528b5SGreg Kurz     synth_fill_statbuf(node, stbuf);
163364031f1SWei Liu     return 0;
164364031f1SWei Liu }
165364031f1SWei Liu 
166b05528b5SGreg Kurz static int synth_fstat(FsContext *fs_ctx, int fid_type,
167364031f1SWei Liu                             V9fsFidOpenState *fs, struct stat *stbuf)
168364031f1SWei Liu {
169364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
170b05528b5SGreg Kurz     synth_fill_statbuf(synth_open->node, stbuf);
171364031f1SWei Liu     return 0;
172364031f1SWei Liu }
173364031f1SWei Liu 
174b05528b5SGreg Kurz static int synth_opendir(FsContext *ctx,
175364031f1SWei Liu                              V9fsPath *fs_path, V9fsFidOpenState *fs)
176364031f1SWei Liu {
177364031f1SWei Liu     V9fsSynthOpenState *synth_open;
178364031f1SWei Liu     V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;
179364031f1SWei Liu 
180364031f1SWei Liu     synth_open = g_malloc(sizeof(*synth_open));
181364031f1SWei Liu     synth_open->node = node;
182364031f1SWei Liu     node->open_count++;
183364031f1SWei Liu     fs->private = synth_open;
184364031f1SWei Liu     return 0;
185364031f1SWei Liu }
186364031f1SWei Liu 
187b05528b5SGreg Kurz static int synth_closedir(FsContext *ctx, V9fsFidOpenState *fs)
188364031f1SWei Liu {
189364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
190364031f1SWei Liu     V9fsSynthNode *node = synth_open->node;
191364031f1SWei Liu 
192364031f1SWei Liu     node->open_count--;
193364031f1SWei Liu     g_free(synth_open);
194364031f1SWei Liu     fs->private = NULL;
195364031f1SWei Liu     return 0;
196364031f1SWei Liu }
197364031f1SWei Liu 
198b05528b5SGreg Kurz static off_t synth_telldir(FsContext *ctx, V9fsFidOpenState *fs)
199364031f1SWei Liu {
200364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
201364031f1SWei Liu     return synth_open->offset;
202364031f1SWei Liu }
203364031f1SWei Liu 
204b05528b5SGreg Kurz static void synth_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
205364031f1SWei Liu {
206364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
207364031f1SWei Liu     synth_open->offset = off;
208364031f1SWei Liu }
209364031f1SWei Liu 
210b05528b5SGreg Kurz static void synth_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
211364031f1SWei Liu {
212b05528b5SGreg Kurz     synth_seekdir(ctx, fs, 0);
213364031f1SWei Liu }
214364031f1SWei Liu 
215b05528b5SGreg Kurz static void synth_direntry(V9fsSynthNode *node,
216364031f1SWei Liu                                 struct dirent *entry, off_t off)
217364031f1SWei Liu {
218364031f1SWei Liu     strcpy(entry->d_name, node->name);
219364031f1SWei Liu     entry->d_ino = node->attr->inode;
220364031f1SWei Liu     entry->d_off = off + 1;
221364031f1SWei Liu }
222364031f1SWei Liu 
223b05528b5SGreg Kurz static struct dirent *synth_get_dentry(V9fsSynthNode *dir,
224635324e8SGreg Kurz                                             struct dirent *entry, off_t off)
225364031f1SWei Liu {
226364031f1SWei Liu     int i = 0;
227364031f1SWei Liu     V9fsSynthNode *node;
228364031f1SWei Liu 
229364031f1SWei Liu     rcu_read_lock();
230364031f1SWei Liu     QLIST_FOREACH(node, &dir->child, sibling) {
231364031f1SWei Liu         /* This is the off child of the directory */
232364031f1SWei Liu         if (i == off) {
233364031f1SWei Liu             break;
234364031f1SWei Liu         }
235364031f1SWei Liu         i++;
236364031f1SWei Liu     }
237364031f1SWei Liu     rcu_read_unlock();
238364031f1SWei Liu     if (!node) {
239364031f1SWei Liu         /* end of directory */
240635324e8SGreg Kurz         return NULL;
241364031f1SWei Liu     }
242b05528b5SGreg Kurz     synth_direntry(node, entry, off);
243635324e8SGreg Kurz     return entry;
244364031f1SWei Liu }
245364031f1SWei Liu 
246b05528b5SGreg Kurz static struct dirent *synth_readdir(FsContext *ctx, V9fsFidOpenState *fs)
247364031f1SWei Liu {
248635324e8SGreg Kurz     struct dirent *entry;
249364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
250364031f1SWei Liu     V9fsSynthNode *node = synth_open->node;
251b05528b5SGreg Kurz     entry = synth_get_dentry(node, &synth_open->dent, synth_open->offset);
252635324e8SGreg Kurz     if (entry) {
253364031f1SWei Liu         synth_open->offset++;
254364031f1SWei Liu     }
255635324e8SGreg Kurz     return entry;
256364031f1SWei Liu }
257364031f1SWei Liu 
258b05528b5SGreg Kurz static int synth_open(FsContext *ctx, V9fsPath *fs_path,
259364031f1SWei Liu                            int flags, V9fsFidOpenState *fs)
260364031f1SWei Liu {
261364031f1SWei Liu     V9fsSynthOpenState *synth_open;
262364031f1SWei Liu     V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;
263364031f1SWei Liu 
264364031f1SWei Liu     synth_open = g_malloc(sizeof(*synth_open));
265364031f1SWei Liu     synth_open->node = node;
266364031f1SWei Liu     node->open_count++;
267364031f1SWei Liu     fs->private = synth_open;
268364031f1SWei Liu     return 0;
269364031f1SWei Liu }
270364031f1SWei Liu 
271b05528b5SGreg Kurz static int synth_open2(FsContext *fs_ctx, V9fsPath *dir_path,
272364031f1SWei Liu                             const char *name, int flags,
273364031f1SWei Liu                             FsCred *credp, V9fsFidOpenState *fs)
274364031f1SWei Liu {
275364031f1SWei Liu     errno = ENOSYS;
276364031f1SWei Liu     return -1;
277364031f1SWei Liu }
278364031f1SWei Liu 
279b05528b5SGreg Kurz static int synth_close(FsContext *ctx, V9fsFidOpenState *fs)
280364031f1SWei Liu {
281364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
282364031f1SWei Liu     V9fsSynthNode *node = synth_open->node;
283364031f1SWei Liu 
284364031f1SWei Liu     node->open_count--;
285364031f1SWei Liu     g_free(synth_open);
286364031f1SWei Liu     fs->private = NULL;
287364031f1SWei Liu     return 0;
288364031f1SWei Liu }
289364031f1SWei Liu 
290b05528b5SGreg Kurz static ssize_t synth_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
291364031f1SWei Liu                                   const struct iovec *iov,
292364031f1SWei Liu                                   int iovcnt, off_t offset)
293364031f1SWei Liu {
294364031f1SWei Liu     int i, count = 0, wcount;
295364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
296364031f1SWei Liu     V9fsSynthNode *node = synth_open->node;
297364031f1SWei Liu     if (!node->attr->write) {
298364031f1SWei Liu         errno = EPERM;
299364031f1SWei Liu         return -1;
300364031f1SWei Liu     }
301364031f1SWei Liu     for (i = 0; i < iovcnt; i++) {
302364031f1SWei Liu         wcount = node->attr->write(iov[i].iov_base, iov[i].iov_len,
303364031f1SWei Liu                                    offset, node->private);
304364031f1SWei Liu         offset += wcount;
305364031f1SWei Liu         count  += wcount;
306364031f1SWei Liu         /* If we wrote less than requested. we are done */
307364031f1SWei Liu         if (wcount < iov[i].iov_len) {
308364031f1SWei Liu             break;
309364031f1SWei Liu         }
310364031f1SWei Liu     }
311364031f1SWei Liu     return count;
312364031f1SWei Liu }
313364031f1SWei Liu 
314b05528b5SGreg Kurz static ssize_t synth_preadv(FsContext *ctx, V9fsFidOpenState *fs,
315364031f1SWei Liu                                  const struct iovec *iov,
316364031f1SWei Liu                                  int iovcnt, off_t offset)
317364031f1SWei Liu {
318364031f1SWei Liu     int i, count = 0, rcount;
319364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
320364031f1SWei Liu     V9fsSynthNode *node = synth_open->node;
321364031f1SWei Liu     if (!node->attr->read) {
322364031f1SWei Liu         errno = EPERM;
323364031f1SWei Liu         return -1;
324364031f1SWei Liu     }
325364031f1SWei Liu     for (i = 0; i < iovcnt; i++) {
326364031f1SWei Liu         rcount = node->attr->read(iov[i].iov_base, iov[i].iov_len,
327364031f1SWei Liu                                   offset, node->private);
328364031f1SWei Liu         offset += rcount;
329364031f1SWei Liu         count  += rcount;
330364031f1SWei Liu         /* If we read less than requested. we are done */
331364031f1SWei Liu         if (rcount < iov[i].iov_len) {
332364031f1SWei Liu             break;
333364031f1SWei Liu         }
334364031f1SWei Liu     }
335364031f1SWei Liu     return count;
336364031f1SWei Liu }
337364031f1SWei Liu 
338b05528b5SGreg Kurz static int synth_truncate(FsContext *ctx, V9fsPath *path, off_t offset)
339364031f1SWei Liu {
340364031f1SWei Liu     errno = ENOSYS;
341364031f1SWei Liu     return -1;
342364031f1SWei Liu }
343364031f1SWei Liu 
344b05528b5SGreg Kurz static int synth_chmod(FsContext *fs_ctx, V9fsPath *path, FsCred *credp)
345364031f1SWei Liu {
346364031f1SWei Liu     errno = EPERM;
347364031f1SWei Liu     return -1;
348364031f1SWei Liu }
349364031f1SWei Liu 
350b05528b5SGreg Kurz static int synth_mknod(FsContext *fs_ctx, V9fsPath *path,
351364031f1SWei Liu                        const char *buf, FsCred *credp)
352364031f1SWei Liu {
353364031f1SWei Liu     errno = EPERM;
354364031f1SWei Liu     return -1;
355364031f1SWei Liu }
356364031f1SWei Liu 
357b05528b5SGreg Kurz static int synth_mkdir(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 
364b05528b5SGreg Kurz static ssize_t synth_readlink(FsContext *fs_ctx, V9fsPath *path,
365364031f1SWei Liu                                    char *buf, size_t bufsz)
366364031f1SWei Liu {
367364031f1SWei Liu     errno = ENOSYS;
368364031f1SWei Liu     return -1;
369364031f1SWei Liu }
370364031f1SWei Liu 
371b05528b5SGreg Kurz static int synth_symlink(FsContext *fs_ctx, const char *oldpath,
372364031f1SWei Liu                               V9fsPath *newpath, const char *buf, FsCred *credp)
373364031f1SWei Liu {
374364031f1SWei Liu     errno = EPERM;
375364031f1SWei Liu     return -1;
376364031f1SWei Liu }
377364031f1SWei Liu 
378b05528b5SGreg Kurz static int synth_link(FsContext *fs_ctx, V9fsPath *oldpath,
379364031f1SWei Liu                            V9fsPath *newpath, const char *buf)
380364031f1SWei Liu {
381364031f1SWei Liu     errno = EPERM;
382364031f1SWei Liu     return -1;
383364031f1SWei Liu }
384364031f1SWei Liu 
385b05528b5SGreg Kurz static int synth_rename(FsContext *ctx, const char *oldpath,
386364031f1SWei Liu                              const char *newpath)
387364031f1SWei Liu {
388364031f1SWei Liu     errno = EPERM;
389364031f1SWei Liu     return -1;
390364031f1SWei Liu }
391364031f1SWei Liu 
392b05528b5SGreg Kurz static int synth_chown(FsContext *fs_ctx, V9fsPath *path, FsCred *credp)
393364031f1SWei Liu {
394364031f1SWei Liu     errno = EPERM;
395364031f1SWei Liu     return -1;
396364031f1SWei Liu }
397364031f1SWei Liu 
398b05528b5SGreg Kurz static int synth_utimensat(FsContext *fs_ctx, V9fsPath *path,
399364031f1SWei Liu                                 const struct timespec *buf)
400364031f1SWei Liu {
401364031f1SWei Liu     errno = EPERM;
402364031f1SWei Liu     return 0;
403364031f1SWei Liu }
404364031f1SWei Liu 
405b05528b5SGreg Kurz static int synth_remove(FsContext *ctx, const char *path)
406364031f1SWei Liu {
407364031f1SWei Liu     errno = EPERM;
408364031f1SWei Liu     return -1;
409364031f1SWei Liu }
410364031f1SWei Liu 
411b05528b5SGreg Kurz static int synth_fsync(FsContext *ctx, int fid_type,
412364031f1SWei Liu                             V9fsFidOpenState *fs, int datasync)
413364031f1SWei Liu {
414364031f1SWei Liu     errno = ENOSYS;
415364031f1SWei Liu     return 0;
416364031f1SWei Liu }
417364031f1SWei Liu 
418b05528b5SGreg Kurz static int synth_statfs(FsContext *s, V9fsPath *fs_path,
419364031f1SWei Liu                              struct statfs *stbuf)
420364031f1SWei Liu {
421364031f1SWei Liu     stbuf->f_type = 0xABCD;
422364031f1SWei Liu     stbuf->f_bsize = 512;
423364031f1SWei Liu     stbuf->f_blocks = 0;
424b05528b5SGreg Kurz     stbuf->f_files = synth_node_count;
425364031f1SWei Liu     stbuf->f_namelen = NAME_MAX;
426364031f1SWei Liu     return 0;
427364031f1SWei Liu }
428364031f1SWei Liu 
429b05528b5SGreg Kurz static ssize_t synth_lgetxattr(FsContext *ctx, V9fsPath *path,
430364031f1SWei Liu                                     const char *name, void *value, size_t size)
431364031f1SWei Liu {
432364031f1SWei Liu     errno = ENOTSUP;
433364031f1SWei Liu     return -1;
434364031f1SWei Liu }
435364031f1SWei Liu 
436b05528b5SGreg Kurz static ssize_t synth_llistxattr(FsContext *ctx, V9fsPath *path,
437364031f1SWei Liu                                      void *value, size_t size)
438364031f1SWei Liu {
439364031f1SWei Liu     errno = ENOTSUP;
440364031f1SWei Liu     return -1;
441364031f1SWei Liu }
442364031f1SWei Liu 
443b05528b5SGreg Kurz static int synth_lsetxattr(FsContext *ctx, V9fsPath *path,
444364031f1SWei Liu                                 const char *name, void *value,
445364031f1SWei Liu                                 size_t size, int flags)
446364031f1SWei Liu {
447364031f1SWei Liu     errno = ENOTSUP;
448364031f1SWei Liu     return -1;
449364031f1SWei Liu }
450364031f1SWei Liu 
451b05528b5SGreg Kurz static int synth_lremovexattr(FsContext *ctx,
452364031f1SWei Liu                                    V9fsPath *path, const char *name)
453364031f1SWei Liu {
454364031f1SWei Liu     errno = ENOTSUP;
455364031f1SWei Liu     return -1;
456364031f1SWei Liu }
457364031f1SWei Liu 
458b05528b5SGreg Kurz static int synth_name_to_path(FsContext *ctx, V9fsPath *dir_path,
459364031f1SWei Liu                                    const char *name, V9fsPath *target)
460364031f1SWei Liu {
461364031f1SWei Liu     V9fsSynthNode *node;
462364031f1SWei Liu     V9fsSynthNode *dir_node;
463364031f1SWei Liu 
464364031f1SWei Liu     /* "." and ".." are not allowed */
465364031f1SWei Liu     if (!strcmp(name, ".") || !strcmp(name, "..")) {
466364031f1SWei Liu         errno = EINVAL;
467364031f1SWei Liu         return -1;
468364031f1SWei Liu 
469364031f1SWei Liu     }
470364031f1SWei Liu     if (!dir_path) {
471b05528b5SGreg Kurz         dir_node = &synth_root;
472364031f1SWei Liu     } else {
473364031f1SWei Liu         dir_node = *(V9fsSynthNode **)dir_path->data;
474364031f1SWei Liu     }
475364031f1SWei Liu     if (!strcmp(name, "/")) {
476364031f1SWei Liu         node = dir_node;
477364031f1SWei Liu         goto out;
478364031f1SWei Liu     }
479364031f1SWei Liu     /* search for the name in the childern */
480364031f1SWei Liu     rcu_read_lock();
481364031f1SWei Liu     QLIST_FOREACH(node, &dir_node->child, sibling) {
482364031f1SWei Liu         if (!strcmp(node->name, name)) {
483364031f1SWei Liu             break;
484364031f1SWei Liu         }
485364031f1SWei Liu     }
486364031f1SWei Liu     rcu_read_unlock();
487364031f1SWei Liu 
488364031f1SWei Liu     if (!node) {
489364031f1SWei Liu         errno = ENOENT;
490364031f1SWei Liu         return -1;
491364031f1SWei Liu     }
492364031f1SWei Liu out:
493364031f1SWei Liu     /* Copy the node pointer to fid */
4946ce7177aSMarc-André Lureau     g_free(target->data);
495453a1b23SMarc-André Lureau     target->data = g_memdup(&node, sizeof(void *));
496364031f1SWei Liu     target->size = sizeof(void *);
497364031f1SWei Liu     return 0;
498364031f1SWei Liu }
499364031f1SWei Liu 
500b05528b5SGreg Kurz static int synth_renameat(FsContext *ctx, V9fsPath *olddir,
501364031f1SWei Liu                                const char *old_name, V9fsPath *newdir,
502364031f1SWei Liu                                const char *new_name)
503364031f1SWei Liu {
504364031f1SWei Liu     errno = EPERM;
505364031f1SWei Liu     return -1;
506364031f1SWei Liu }
507364031f1SWei Liu 
508b05528b5SGreg Kurz static int synth_unlinkat(FsContext *ctx, V9fsPath *dir,
509364031f1SWei Liu                                const char *name, int flags)
510364031f1SWei Liu {
511364031f1SWei Liu     errno = EPERM;
512364031f1SWei Liu     return -1;
513364031f1SWei Liu }
514364031f1SWei Liu 
515354b86f8SGreg Kurz static ssize_t v9fs_synth_qtest_write(void *buf, int len, off_t offset,
516354b86f8SGreg Kurz                                       void *arg)
517354b86f8SGreg Kurz {
518354b86f8SGreg Kurz     return 1;
519354b86f8SGreg Kurz }
520354b86f8SGreg Kurz 
521357e2f7fSGreg Kurz static ssize_t v9fs_synth_qtest_flush_write(void *buf, int len, off_t offset,
522357e2f7fSGreg Kurz                                             void *arg)
523357e2f7fSGreg Kurz {
524357e2f7fSGreg Kurz     bool should_block = !!*(uint8_t *)buf;
525357e2f7fSGreg Kurz 
526357e2f7fSGreg Kurz     if (should_block) {
527357e2f7fSGreg Kurz         /* This will cause the server to call us again until we're cancelled */
528357e2f7fSGreg Kurz         errno = EINTR;
529357e2f7fSGreg Kurz         return -1;
530357e2f7fSGreg Kurz     }
531357e2f7fSGreg Kurz 
532357e2f7fSGreg Kurz     return 1;
533357e2f7fSGreg Kurz }
534357e2f7fSGreg Kurz 
53565603a80SGreg Kurz static int synth_init(FsContext *ctx, Error **errp)
536364031f1SWei Liu {
537b05528b5SGreg Kurz     QLIST_INIT(&synth_root.child);
538b05528b5SGreg Kurz     qemu_mutex_init(&synth_mutex);
539364031f1SWei Liu 
540364031f1SWei Liu     /* Add "." and ".." entries for root */
541b05528b5SGreg Kurz     v9fs_add_dir_node(&synth_root, synth_root.attr->mode,
542b05528b5SGreg Kurz                       "..", synth_root.attr, synth_root.attr->inode);
543b05528b5SGreg Kurz     v9fs_add_dir_node(&synth_root, synth_root.attr->mode,
544b05528b5SGreg Kurz                       ".", synth_root.attr, synth_root.attr->inode);
545364031f1SWei Liu 
546364031f1SWei Liu     /* Mark the subsystem is ready for use */
547b05528b5SGreg Kurz     synth_fs = 1;
5482893ddd5SGreg Kurz 
5492893ddd5SGreg Kurz     if (qtest_enabled()) {
5502893ddd5SGreg Kurz         V9fsSynthNode *node = NULL;
5512893ddd5SGreg Kurz         int i, ret;
5522893ddd5SGreg Kurz 
5532893ddd5SGreg Kurz         /* Directory hierarchy for WALK test */
5542893ddd5SGreg Kurz         for (i = 0; i < P9_MAXWELEM; i++) {
5552893ddd5SGreg Kurz             char *name = g_strdup_printf(QTEST_V9FS_SYNTH_WALK_FILE, i);
5562893ddd5SGreg Kurz 
5572893ddd5SGreg Kurz             ret = qemu_v9fs_synth_mkdir(node, 0700, name, &node);
5582893ddd5SGreg Kurz             assert(!ret);
5592893ddd5SGreg Kurz             g_free(name);
5602893ddd5SGreg Kurz         }
56182469aaeSGreg Kurz 
56282469aaeSGreg Kurz         /* File for LOPEN test */
56382469aaeSGreg Kurz         ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_LOPEN_FILE,
56482469aaeSGreg Kurz                                        NULL, NULL, ctx);
56582469aaeSGreg Kurz         assert(!ret);
566354b86f8SGreg Kurz 
567354b86f8SGreg Kurz         /* File for WRITE test */
568354b86f8SGreg Kurz         ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_WRITE_FILE,
569354b86f8SGreg Kurz                                        NULL, v9fs_synth_qtest_write, ctx);
570354b86f8SGreg Kurz         assert(!ret);
571357e2f7fSGreg Kurz 
572357e2f7fSGreg Kurz         /* File for FLUSH test */
573357e2f7fSGreg Kurz         ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_FLUSH_FILE,
574357e2f7fSGreg Kurz                                        NULL, v9fs_synth_qtest_flush_write,
575357e2f7fSGreg Kurz                                        ctx);
576357e2f7fSGreg Kurz         assert(!ret);
577af46a3b2SChristian Schoenebeck 
578af46a3b2SChristian Schoenebeck         /* Directory for READDIR test */
579af46a3b2SChristian Schoenebeck         {
580af46a3b2SChristian Schoenebeck             V9fsSynthNode *dir = NULL;
581af46a3b2SChristian Schoenebeck             ret = qemu_v9fs_synth_mkdir(
582af46a3b2SChristian Schoenebeck                 NULL, 0700, QTEST_V9FS_SYNTH_READDIR_DIR, &dir
583af46a3b2SChristian Schoenebeck             );
584af46a3b2SChristian Schoenebeck             assert(!ret);
585af46a3b2SChristian Schoenebeck             for (i = 0; i < QTEST_V9FS_SYNTH_READDIR_NFILES; ++i) {
586af46a3b2SChristian Schoenebeck                 char *name = g_strdup_printf(
587af46a3b2SChristian Schoenebeck                     QTEST_V9FS_SYNTH_READDIR_FILE, i
588af46a3b2SChristian Schoenebeck                 );
589af46a3b2SChristian Schoenebeck                 ret = qemu_v9fs_synth_add_file(
590af46a3b2SChristian Schoenebeck                     dir, 0, name, NULL, NULL, ctx
591af46a3b2SChristian Schoenebeck                 );
592af46a3b2SChristian Schoenebeck                 assert(!ret);
593af46a3b2SChristian Schoenebeck                 g_free(name);
594af46a3b2SChristian Schoenebeck             }
595af46a3b2SChristian Schoenebeck         }
5962893ddd5SGreg Kurz     }
5972893ddd5SGreg Kurz 
598364031f1SWei Liu     return 0;
599364031f1SWei Liu }
600364031f1SWei Liu 
601364031f1SWei Liu FileOperations synth_ops = {
602b05528b5SGreg Kurz     .init         = synth_init,
603b05528b5SGreg Kurz     .lstat        = synth_lstat,
604b05528b5SGreg Kurz     .readlink     = synth_readlink,
605b05528b5SGreg Kurz     .close        = synth_close,
606b05528b5SGreg Kurz     .closedir     = synth_closedir,
607b05528b5SGreg Kurz     .open         = synth_open,
608b05528b5SGreg Kurz     .opendir      = synth_opendir,
609b05528b5SGreg Kurz     .rewinddir    = synth_rewinddir,
610b05528b5SGreg Kurz     .telldir      = synth_telldir,
611b05528b5SGreg Kurz     .readdir      = synth_readdir,
612b05528b5SGreg Kurz     .seekdir      = synth_seekdir,
613b05528b5SGreg Kurz     .preadv       = synth_preadv,
614b05528b5SGreg Kurz     .pwritev      = synth_pwritev,
615b05528b5SGreg Kurz     .chmod        = synth_chmod,
616b05528b5SGreg Kurz     .mknod        = synth_mknod,
617b05528b5SGreg Kurz     .mkdir        = synth_mkdir,
618b05528b5SGreg Kurz     .fstat        = synth_fstat,
619b05528b5SGreg Kurz     .open2        = synth_open2,
620b05528b5SGreg Kurz     .symlink      = synth_symlink,
621b05528b5SGreg Kurz     .link         = synth_link,
622b05528b5SGreg Kurz     .truncate     = synth_truncate,
623b05528b5SGreg Kurz     .rename       = synth_rename,
624b05528b5SGreg Kurz     .chown        = synth_chown,
625b05528b5SGreg Kurz     .utimensat    = synth_utimensat,
626b05528b5SGreg Kurz     .remove       = synth_remove,
627b05528b5SGreg Kurz     .fsync        = synth_fsync,
628b05528b5SGreg Kurz     .statfs       = synth_statfs,
629b05528b5SGreg Kurz     .lgetxattr    = synth_lgetxattr,
630b05528b5SGreg Kurz     .llistxattr   = synth_llistxattr,
631b05528b5SGreg Kurz     .lsetxattr    = synth_lsetxattr,
632b05528b5SGreg Kurz     .lremovexattr = synth_lremovexattr,
633b05528b5SGreg Kurz     .name_to_path = synth_name_to_path,
634b05528b5SGreg Kurz     .renameat     = synth_renameat,
635b05528b5SGreg Kurz     .unlinkat     = synth_unlinkat,
636364031f1SWei Liu };
637