xref: /openbmc/qemu/hw/9pfs/9p-synth.c (revision 357e2f7f4e4dc68f01d5b81f5cd669874314e14a)
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     }
82b05528b5SGreg Kurz     qemu_mutex_lock(&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 */
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 err_out:
98b05528b5SGreg Kurz     qemu_mutex_unlock(&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 
109b05528b5SGreg Kurz     if (!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) {
116b05528b5SGreg Kurz         parent = &synth_root;
117364031f1SWei Liu     }
118364031f1SWei Liu 
119b05528b5SGreg Kurz     qemu_mutex_lock(&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;
130b05528b5SGreg Kurz     node->attr->inode  = 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:
140b05528b5SGreg Kurz     qemu_mutex_unlock(&synth_mutex);
141364031f1SWei Liu     return ret;
142364031f1SWei Liu }
143364031f1SWei Liu 
144b05528b5SGreg Kurz static void 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 
161b05528b5SGreg Kurz static int 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 
166b05528b5SGreg Kurz     synth_fill_statbuf(node, stbuf);
167364031f1SWei Liu     return 0;
168364031f1SWei Liu }
169364031f1SWei Liu 
170b05528b5SGreg Kurz static int synth_fstat(FsContext *fs_ctx, int fid_type,
171364031f1SWei Liu                             V9fsFidOpenState *fs, struct stat *stbuf)
172364031f1SWei Liu {
173364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
174b05528b5SGreg Kurz     synth_fill_statbuf(synth_open->node, stbuf);
175364031f1SWei Liu     return 0;
176364031f1SWei Liu }
177364031f1SWei Liu 
178b05528b5SGreg Kurz static int 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 
191b05528b5SGreg Kurz static int 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 
202b05528b5SGreg Kurz static off_t 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 
208b05528b5SGreg Kurz static void 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 
214b05528b5SGreg Kurz static void synth_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
215364031f1SWei Liu {
216b05528b5SGreg Kurz     synth_seekdir(ctx, fs, 0);
217364031f1SWei Liu }
218364031f1SWei Liu 
219b05528b5SGreg Kurz static void 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 
227b05528b5SGreg Kurz static struct dirent *synth_get_dentry(V9fsSynthNode *dir,
228635324e8SGreg Kurz                                             struct dirent *entry, 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 */
244635324e8SGreg Kurz         return NULL;
245364031f1SWei Liu     }
246b05528b5SGreg Kurz     synth_direntry(node, entry, off);
247635324e8SGreg Kurz     return entry;
248364031f1SWei Liu }
249364031f1SWei Liu 
250b05528b5SGreg Kurz static struct dirent *synth_readdir(FsContext *ctx, V9fsFidOpenState *fs)
251364031f1SWei Liu {
252635324e8SGreg Kurz     struct dirent *entry;
253364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
254364031f1SWei Liu     V9fsSynthNode *node = synth_open->node;
255b05528b5SGreg Kurz     entry = synth_get_dentry(node, &synth_open->dent, synth_open->offset);
256635324e8SGreg Kurz     if (entry) {
257364031f1SWei Liu         synth_open->offset++;
258364031f1SWei Liu     }
259635324e8SGreg Kurz     return entry;
260364031f1SWei Liu }
261364031f1SWei Liu 
262b05528b5SGreg Kurz static int synth_open(FsContext *ctx, V9fsPath *fs_path,
263364031f1SWei Liu                            int flags, V9fsFidOpenState *fs)
264364031f1SWei Liu {
265364031f1SWei Liu     V9fsSynthOpenState *synth_open;
266364031f1SWei Liu     V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;
267364031f1SWei Liu 
268364031f1SWei Liu     synth_open = g_malloc(sizeof(*synth_open));
269364031f1SWei Liu     synth_open->node = node;
270364031f1SWei Liu     node->open_count++;
271364031f1SWei Liu     fs->private = synth_open;
272364031f1SWei Liu     return 0;
273364031f1SWei Liu }
274364031f1SWei Liu 
275b05528b5SGreg Kurz static int synth_open2(FsContext *fs_ctx, V9fsPath *dir_path,
276364031f1SWei Liu                             const char *name, int flags,
277364031f1SWei Liu                             FsCred *credp, V9fsFidOpenState *fs)
278364031f1SWei Liu {
279364031f1SWei Liu     errno = ENOSYS;
280364031f1SWei Liu     return -1;
281364031f1SWei Liu }
282364031f1SWei Liu 
283b05528b5SGreg Kurz static int synth_close(FsContext *ctx, V9fsFidOpenState *fs)
284364031f1SWei Liu {
285364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
286364031f1SWei Liu     V9fsSynthNode *node = synth_open->node;
287364031f1SWei Liu 
288364031f1SWei Liu     node->open_count--;
289364031f1SWei Liu     g_free(synth_open);
290364031f1SWei Liu     fs->private = NULL;
291364031f1SWei Liu     return 0;
292364031f1SWei Liu }
293364031f1SWei Liu 
294b05528b5SGreg Kurz static ssize_t synth_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
295364031f1SWei Liu                                   const struct iovec *iov,
296364031f1SWei Liu                                   int iovcnt, off_t offset)
297364031f1SWei Liu {
298364031f1SWei Liu     int i, count = 0, wcount;
299364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
300364031f1SWei Liu     V9fsSynthNode *node = synth_open->node;
301364031f1SWei Liu     if (!node->attr->write) {
302364031f1SWei Liu         errno = EPERM;
303364031f1SWei Liu         return -1;
304364031f1SWei Liu     }
305364031f1SWei Liu     for (i = 0; i < iovcnt; i++) {
306364031f1SWei Liu         wcount = node->attr->write(iov[i].iov_base, iov[i].iov_len,
307364031f1SWei Liu                                    offset, node->private);
308364031f1SWei Liu         offset += wcount;
309364031f1SWei Liu         count  += wcount;
310364031f1SWei Liu         /* If we wrote less than requested. we are done */
311364031f1SWei Liu         if (wcount < iov[i].iov_len) {
312364031f1SWei Liu             break;
313364031f1SWei Liu         }
314364031f1SWei Liu     }
315364031f1SWei Liu     return count;
316364031f1SWei Liu }
317364031f1SWei Liu 
318b05528b5SGreg Kurz static ssize_t synth_preadv(FsContext *ctx, V9fsFidOpenState *fs,
319364031f1SWei Liu                                  const struct iovec *iov,
320364031f1SWei Liu                                  int iovcnt, off_t offset)
321364031f1SWei Liu {
322364031f1SWei Liu     int i, count = 0, rcount;
323364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
324364031f1SWei Liu     V9fsSynthNode *node = synth_open->node;
325364031f1SWei Liu     if (!node->attr->read) {
326364031f1SWei Liu         errno = EPERM;
327364031f1SWei Liu         return -1;
328364031f1SWei Liu     }
329364031f1SWei Liu     for (i = 0; i < iovcnt; i++) {
330364031f1SWei Liu         rcount = node->attr->read(iov[i].iov_base, iov[i].iov_len,
331364031f1SWei Liu                                   offset, node->private);
332364031f1SWei Liu         offset += rcount;
333364031f1SWei Liu         count  += rcount;
334364031f1SWei Liu         /* If we read less than requested. we are done */
335364031f1SWei Liu         if (rcount < iov[i].iov_len) {
336364031f1SWei Liu             break;
337364031f1SWei Liu         }
338364031f1SWei Liu     }
339364031f1SWei Liu     return count;
340364031f1SWei Liu }
341364031f1SWei Liu 
342b05528b5SGreg Kurz static int synth_truncate(FsContext *ctx, V9fsPath *path, off_t offset)
343364031f1SWei Liu {
344364031f1SWei Liu     errno = ENOSYS;
345364031f1SWei Liu     return -1;
346364031f1SWei Liu }
347364031f1SWei Liu 
348b05528b5SGreg Kurz static int synth_chmod(FsContext *fs_ctx, V9fsPath *path, FsCred *credp)
349364031f1SWei Liu {
350364031f1SWei Liu     errno = EPERM;
351364031f1SWei Liu     return -1;
352364031f1SWei Liu }
353364031f1SWei Liu 
354b05528b5SGreg Kurz static int synth_mknod(FsContext *fs_ctx, V9fsPath *path,
355364031f1SWei Liu                        const char *buf, FsCred *credp)
356364031f1SWei Liu {
357364031f1SWei Liu     errno = EPERM;
358364031f1SWei Liu     return -1;
359364031f1SWei Liu }
360364031f1SWei Liu 
361b05528b5SGreg Kurz static int synth_mkdir(FsContext *fs_ctx, V9fsPath *path,
362364031f1SWei Liu                        const char *buf, FsCred *credp)
363364031f1SWei Liu {
364364031f1SWei Liu     errno = EPERM;
365364031f1SWei Liu     return -1;
366364031f1SWei Liu }
367364031f1SWei Liu 
368b05528b5SGreg Kurz static ssize_t synth_readlink(FsContext *fs_ctx, V9fsPath *path,
369364031f1SWei Liu                                    char *buf, size_t bufsz)
370364031f1SWei Liu {
371364031f1SWei Liu     errno = ENOSYS;
372364031f1SWei Liu     return -1;
373364031f1SWei Liu }
374364031f1SWei Liu 
375b05528b5SGreg Kurz static int synth_symlink(FsContext *fs_ctx, const char *oldpath,
376364031f1SWei Liu                               V9fsPath *newpath, const char *buf, FsCred *credp)
377364031f1SWei Liu {
378364031f1SWei Liu     errno = EPERM;
379364031f1SWei Liu     return -1;
380364031f1SWei Liu }
381364031f1SWei Liu 
382b05528b5SGreg Kurz static int synth_link(FsContext *fs_ctx, V9fsPath *oldpath,
383364031f1SWei Liu                            V9fsPath *newpath, const char *buf)
384364031f1SWei Liu {
385364031f1SWei Liu     errno = EPERM;
386364031f1SWei Liu     return -1;
387364031f1SWei Liu }
388364031f1SWei Liu 
389b05528b5SGreg Kurz static int synth_rename(FsContext *ctx, const char *oldpath,
390364031f1SWei Liu                              const char *newpath)
391364031f1SWei Liu {
392364031f1SWei Liu     errno = EPERM;
393364031f1SWei Liu     return -1;
394364031f1SWei Liu }
395364031f1SWei Liu 
396b05528b5SGreg Kurz static int synth_chown(FsContext *fs_ctx, V9fsPath *path, FsCred *credp)
397364031f1SWei Liu {
398364031f1SWei Liu     errno = EPERM;
399364031f1SWei Liu     return -1;
400364031f1SWei Liu }
401364031f1SWei Liu 
402b05528b5SGreg Kurz static int synth_utimensat(FsContext *fs_ctx, V9fsPath *path,
403364031f1SWei Liu                                 const struct timespec *buf)
404364031f1SWei Liu {
405364031f1SWei Liu     errno = EPERM;
406364031f1SWei Liu     return 0;
407364031f1SWei Liu }
408364031f1SWei Liu 
409b05528b5SGreg Kurz static int synth_remove(FsContext *ctx, const char *path)
410364031f1SWei Liu {
411364031f1SWei Liu     errno = EPERM;
412364031f1SWei Liu     return -1;
413364031f1SWei Liu }
414364031f1SWei Liu 
415b05528b5SGreg Kurz static int synth_fsync(FsContext *ctx, int fid_type,
416364031f1SWei Liu                             V9fsFidOpenState *fs, int datasync)
417364031f1SWei Liu {
418364031f1SWei Liu     errno = ENOSYS;
419364031f1SWei Liu     return 0;
420364031f1SWei Liu }
421364031f1SWei Liu 
422b05528b5SGreg Kurz static int synth_statfs(FsContext *s, V9fsPath *fs_path,
423364031f1SWei Liu                              struct statfs *stbuf)
424364031f1SWei Liu {
425364031f1SWei Liu     stbuf->f_type = 0xABCD;
426364031f1SWei Liu     stbuf->f_bsize = 512;
427364031f1SWei Liu     stbuf->f_blocks = 0;
428b05528b5SGreg Kurz     stbuf->f_files = synth_node_count;
429364031f1SWei Liu     stbuf->f_namelen = NAME_MAX;
430364031f1SWei Liu     return 0;
431364031f1SWei Liu }
432364031f1SWei Liu 
433b05528b5SGreg Kurz static ssize_t synth_lgetxattr(FsContext *ctx, V9fsPath *path,
434364031f1SWei Liu                                     const char *name, void *value, size_t size)
435364031f1SWei Liu {
436364031f1SWei Liu     errno = ENOTSUP;
437364031f1SWei Liu     return -1;
438364031f1SWei Liu }
439364031f1SWei Liu 
440b05528b5SGreg Kurz static ssize_t synth_llistxattr(FsContext *ctx, V9fsPath *path,
441364031f1SWei Liu                                      void *value, size_t size)
442364031f1SWei Liu {
443364031f1SWei Liu     errno = ENOTSUP;
444364031f1SWei Liu     return -1;
445364031f1SWei Liu }
446364031f1SWei Liu 
447b05528b5SGreg Kurz static int synth_lsetxattr(FsContext *ctx, V9fsPath *path,
448364031f1SWei Liu                                 const char *name, void *value,
449364031f1SWei Liu                                 size_t size, int flags)
450364031f1SWei Liu {
451364031f1SWei Liu     errno = ENOTSUP;
452364031f1SWei Liu     return -1;
453364031f1SWei Liu }
454364031f1SWei Liu 
455b05528b5SGreg Kurz static int synth_lremovexattr(FsContext *ctx,
456364031f1SWei Liu                                    V9fsPath *path, const char *name)
457364031f1SWei Liu {
458364031f1SWei Liu     errno = ENOTSUP;
459364031f1SWei Liu     return -1;
460364031f1SWei Liu }
461364031f1SWei Liu 
462b05528b5SGreg Kurz static int synth_name_to_path(FsContext *ctx, V9fsPath *dir_path,
463364031f1SWei Liu                                    const char *name, V9fsPath *target)
464364031f1SWei Liu {
465364031f1SWei Liu     V9fsSynthNode *node;
466364031f1SWei Liu     V9fsSynthNode *dir_node;
467364031f1SWei Liu 
468364031f1SWei Liu     /* "." and ".." are not allowed */
469364031f1SWei Liu     if (!strcmp(name, ".") || !strcmp(name, "..")) {
470364031f1SWei Liu         errno = EINVAL;
471364031f1SWei Liu         return -1;
472364031f1SWei Liu 
473364031f1SWei Liu     }
474364031f1SWei Liu     if (!dir_path) {
475b05528b5SGreg Kurz         dir_node = &synth_root;
476364031f1SWei Liu     } else {
477364031f1SWei Liu         dir_node = *(V9fsSynthNode **)dir_path->data;
478364031f1SWei Liu     }
479364031f1SWei Liu     if (!strcmp(name, "/")) {
480364031f1SWei Liu         node = dir_node;
481364031f1SWei Liu         goto out;
482364031f1SWei Liu     }
483364031f1SWei Liu     /* search for the name in the childern */
484364031f1SWei Liu     rcu_read_lock();
485364031f1SWei Liu     QLIST_FOREACH(node, &dir_node->child, sibling) {
486364031f1SWei Liu         if (!strcmp(node->name, name)) {
487364031f1SWei Liu             break;
488364031f1SWei Liu         }
489364031f1SWei Liu     }
490364031f1SWei Liu     rcu_read_unlock();
491364031f1SWei Liu 
492364031f1SWei Liu     if (!node) {
493364031f1SWei Liu         errno = ENOENT;
494364031f1SWei Liu         return -1;
495364031f1SWei Liu     }
496364031f1SWei Liu out:
497364031f1SWei Liu     /* Copy the node pointer to fid */
498453a1b23SMarc-André Lureau     target->data = g_memdup(&node, sizeof(void *));
499364031f1SWei Liu     target->size = sizeof(void *);
500364031f1SWei Liu     return 0;
501364031f1SWei Liu }
502364031f1SWei Liu 
503b05528b5SGreg Kurz static int synth_renameat(FsContext *ctx, V9fsPath *olddir,
504364031f1SWei Liu                                const char *old_name, V9fsPath *newdir,
505364031f1SWei Liu                                const char *new_name)
506364031f1SWei Liu {
507364031f1SWei Liu     errno = EPERM;
508364031f1SWei Liu     return -1;
509364031f1SWei Liu }
510364031f1SWei Liu 
511b05528b5SGreg Kurz static int synth_unlinkat(FsContext *ctx, V9fsPath *dir,
512364031f1SWei Liu                                const char *name, int flags)
513364031f1SWei Liu {
514364031f1SWei Liu     errno = EPERM;
515364031f1SWei Liu     return -1;
516364031f1SWei Liu }
517364031f1SWei Liu 
518354b86f8SGreg Kurz static ssize_t v9fs_synth_qtest_write(void *buf, int len, off_t offset,
519354b86f8SGreg Kurz                                       void *arg)
520354b86f8SGreg Kurz {
521354b86f8SGreg Kurz     return 1;
522354b86f8SGreg Kurz }
523354b86f8SGreg Kurz 
524*357e2f7fSGreg Kurz static ssize_t v9fs_synth_qtest_flush_write(void *buf, int len, off_t offset,
525*357e2f7fSGreg Kurz                                             void *arg)
526*357e2f7fSGreg Kurz {
527*357e2f7fSGreg Kurz     bool should_block = !!*(uint8_t *)buf;
528*357e2f7fSGreg Kurz 
529*357e2f7fSGreg Kurz     if (should_block) {
530*357e2f7fSGreg Kurz         /* This will cause the server to call us again until we're cancelled */
531*357e2f7fSGreg Kurz         errno = EINTR;
532*357e2f7fSGreg Kurz         return -1;
533*357e2f7fSGreg Kurz     }
534*357e2f7fSGreg Kurz 
535*357e2f7fSGreg Kurz     return 1;
536*357e2f7fSGreg Kurz }
537*357e2f7fSGreg Kurz 
53865603a80SGreg Kurz static int synth_init(FsContext *ctx, Error **errp)
539364031f1SWei Liu {
540b05528b5SGreg Kurz     QLIST_INIT(&synth_root.child);
541b05528b5SGreg Kurz     qemu_mutex_init(&synth_mutex);
542364031f1SWei Liu 
543364031f1SWei Liu     /* Add "." and ".." entries for root */
544b05528b5SGreg Kurz     v9fs_add_dir_node(&synth_root, synth_root.attr->mode,
545b05528b5SGreg Kurz                       "..", synth_root.attr, synth_root.attr->inode);
546b05528b5SGreg Kurz     v9fs_add_dir_node(&synth_root, synth_root.attr->mode,
547b05528b5SGreg Kurz                       ".", synth_root.attr, synth_root.attr->inode);
548364031f1SWei Liu 
549364031f1SWei Liu     /* Mark the subsystem is ready for use */
550b05528b5SGreg Kurz     synth_fs = 1;
5512893ddd5SGreg Kurz 
5522893ddd5SGreg Kurz     if (qtest_enabled()) {
5532893ddd5SGreg Kurz         V9fsSynthNode *node = NULL;
5542893ddd5SGreg Kurz         int i, ret;
5552893ddd5SGreg Kurz 
5562893ddd5SGreg Kurz         /* Directory hierarchy for WALK test */
5572893ddd5SGreg Kurz         for (i = 0; i < P9_MAXWELEM; i++) {
5582893ddd5SGreg Kurz             char *name = g_strdup_printf(QTEST_V9FS_SYNTH_WALK_FILE, i);
5592893ddd5SGreg Kurz 
5602893ddd5SGreg Kurz             ret = qemu_v9fs_synth_mkdir(node, 0700, name, &node);
5612893ddd5SGreg Kurz             assert(!ret);
5622893ddd5SGreg Kurz             g_free(name);
5632893ddd5SGreg Kurz         }
56482469aaeSGreg Kurz 
56582469aaeSGreg Kurz         /* File for LOPEN test */
56682469aaeSGreg Kurz         ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_LOPEN_FILE,
56782469aaeSGreg Kurz                                        NULL, NULL, ctx);
56882469aaeSGreg Kurz         assert(!ret);
569354b86f8SGreg Kurz 
570354b86f8SGreg Kurz         /* File for WRITE test */
571354b86f8SGreg Kurz         ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_WRITE_FILE,
572354b86f8SGreg Kurz                                        NULL, v9fs_synth_qtest_write, ctx);
573354b86f8SGreg Kurz         assert(!ret);
574*357e2f7fSGreg Kurz 
575*357e2f7fSGreg Kurz         /* File for FLUSH test */
576*357e2f7fSGreg Kurz         ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_FLUSH_FILE,
577*357e2f7fSGreg Kurz                                        NULL, v9fs_synth_qtest_flush_write,
578*357e2f7fSGreg Kurz                                        ctx);
579*357e2f7fSGreg Kurz         assert(!ret);
5802893ddd5SGreg Kurz     }
5812893ddd5SGreg Kurz 
582364031f1SWei Liu     return 0;
583364031f1SWei Liu }
584364031f1SWei Liu 
585364031f1SWei Liu FileOperations synth_ops = {
586b05528b5SGreg Kurz     .init         = synth_init,
587b05528b5SGreg Kurz     .lstat        = synth_lstat,
588b05528b5SGreg Kurz     .readlink     = synth_readlink,
589b05528b5SGreg Kurz     .close        = synth_close,
590b05528b5SGreg Kurz     .closedir     = synth_closedir,
591b05528b5SGreg Kurz     .open         = synth_open,
592b05528b5SGreg Kurz     .opendir      = synth_opendir,
593b05528b5SGreg Kurz     .rewinddir    = synth_rewinddir,
594b05528b5SGreg Kurz     .telldir      = synth_telldir,
595b05528b5SGreg Kurz     .readdir      = synth_readdir,
596b05528b5SGreg Kurz     .seekdir      = synth_seekdir,
597b05528b5SGreg Kurz     .preadv       = synth_preadv,
598b05528b5SGreg Kurz     .pwritev      = synth_pwritev,
599b05528b5SGreg Kurz     .chmod        = synth_chmod,
600b05528b5SGreg Kurz     .mknod        = synth_mknod,
601b05528b5SGreg Kurz     .mkdir        = synth_mkdir,
602b05528b5SGreg Kurz     .fstat        = synth_fstat,
603b05528b5SGreg Kurz     .open2        = synth_open2,
604b05528b5SGreg Kurz     .symlink      = synth_symlink,
605b05528b5SGreg Kurz     .link         = synth_link,
606b05528b5SGreg Kurz     .truncate     = synth_truncate,
607b05528b5SGreg Kurz     .rename       = synth_rename,
608b05528b5SGreg Kurz     .chown        = synth_chown,
609b05528b5SGreg Kurz     .utimensat    = synth_utimensat,
610b05528b5SGreg Kurz     .remove       = synth_remove,
611b05528b5SGreg Kurz     .fsync        = synth_fsync,
612b05528b5SGreg Kurz     .statfs       = synth_statfs,
613b05528b5SGreg Kurz     .lgetxattr    = synth_lgetxattr,
614b05528b5SGreg Kurz     .llistxattr   = synth_llistxattr,
615b05528b5SGreg Kurz     .lsetxattr    = synth_lsetxattr,
616b05528b5SGreg Kurz     .lremovexattr = synth_lremovexattr,
617b05528b5SGreg Kurz     .name_to_path = synth_name_to_path,
618b05528b5SGreg Kurz     .renameat     = synth_renameat,
619b05528b5SGreg Kurz     .unlinkat     = synth_unlinkat,
620364031f1SWei Liu };
621