xref: /openbmc/qemu/hw/9pfs/9p-synth.c (revision e64e27d5cb103b7764f1a05b6eda7e7fedd517c5)
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 
156f569084SChristian Schoenebeck /*
166f569084SChristian Schoenebeck  * Not so fast! You might want to read the 9p developer docs first:
176f569084SChristian Schoenebeck  * https://wiki.qemu.org/Documentation/9p
186f569084SChristian Schoenebeck  */
196f569084SChristian Schoenebeck 
20fbc04127SPeter Maydell #include "qemu/osdep.h"
21ebe74f8bSWei Liu #include "9p.h"
22364031f1SWei Liu #include "fsdev/qemu-fsdev.h"
23364031f1SWei Liu #include "9p-synth.h"
24364031f1SWei Liu #include "qemu/rcu.h"
25364031f1SWei Liu #include "qemu/rcu_queue.h"
26f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
272893ddd5SGreg Kurz #include "sysemu/qtest.h"
28364031f1SWei Liu 
29364031f1SWei Liu /* Root node for synth file system */
30b05528b5SGreg Kurz static V9fsSynthNode synth_root = {
31364031f1SWei Liu     .name = "/",
32364031f1SWei Liu     .actual_attr = {
33364031f1SWei Liu         .mode = 0555 | S_IFDIR,
34364031f1SWei Liu         .nlink = 1,
35364031f1SWei Liu     },
36b05528b5SGreg Kurz     .attr = &synth_root.actual_attr,
37364031f1SWei Liu };
38364031f1SWei Liu 
39b05528b5SGreg Kurz static QemuMutex  synth_mutex;
40b05528b5SGreg Kurz static int synth_node_count;
41364031f1SWei Liu /* set to 1 when the synth fs is ready */
42b05528b5SGreg Kurz static int synth_fs;
43364031f1SWei Liu 
44364031f1SWei Liu static V9fsSynthNode *v9fs_add_dir_node(V9fsSynthNode *parent, int mode,
45364031f1SWei Liu                                         const char *name,
46364031f1SWei Liu                                         V9fsSynthNodeAttr *attr, int inode)
47364031f1SWei Liu {
48364031f1SWei Liu     V9fsSynthNode *node;
49364031f1SWei Liu 
50364031f1SWei Liu     /* Add directory type and remove write bits */
51364031f1SWei Liu     mode = ((mode & 0777) | S_IFDIR) & ~(S_IWUSR | S_IWGRP | S_IWOTH);
52364031f1SWei Liu     node = g_malloc0(sizeof(V9fsSynthNode));
53364031f1SWei Liu     if (attr) {
54364031f1SWei Liu         /* We are adding .. or . entries */
55364031f1SWei Liu         node->attr = attr;
56364031f1SWei Liu         node->attr->nlink++;
57364031f1SWei Liu     } else {
58364031f1SWei Liu         node->attr = &node->actual_attr;
59364031f1SWei Liu         node->attr->inode = inode;
60364031f1SWei Liu         node->attr->nlink = 1;
61364031f1SWei Liu         /* We don't allow write to directories */
62364031f1SWei Liu         node->attr->mode   = mode;
63364031f1SWei Liu         node->attr->write = NULL;
64364031f1SWei Liu         node->attr->read  = NULL;
65364031f1SWei Liu     }
66364031f1SWei Liu     node->private = node;
67364031f1SWei Liu     pstrcpy(node->name, sizeof(node->name), name);
68364031f1SWei Liu     QLIST_INSERT_HEAD_RCU(&parent->child, node, sibling);
69364031f1SWei Liu     return node;
70364031f1SWei Liu }
71364031f1SWei Liu 
72364031f1SWei Liu int qemu_v9fs_synth_mkdir(V9fsSynthNode *parent, int mode,
73364031f1SWei Liu                           const char *name, V9fsSynthNode **result)
74364031f1SWei Liu {
75364031f1SWei Liu     int ret;
76364031f1SWei Liu     V9fsSynthNode *node, *tmp;
77364031f1SWei Liu 
78b05528b5SGreg Kurz     if (!synth_fs) {
79364031f1SWei Liu         return EAGAIN;
80364031f1SWei Liu     }
81364031f1SWei Liu     if (!name || (strlen(name) >= NAME_MAX)) {
82364031f1SWei Liu         return EINVAL;
83364031f1SWei Liu     }
84364031f1SWei Liu     if (!parent) {
85b05528b5SGreg Kurz         parent = &synth_root;
86364031f1SWei Liu     }
87e4fd889fSMahmoud Mandour     QEMU_LOCK_GUARD(&synth_mutex);
88364031f1SWei Liu     QLIST_FOREACH(tmp, &parent->child, sibling) {
89364031f1SWei Liu         if (!strcmp(tmp->name, name)) {
90364031f1SWei Liu             ret = EEXIST;
91e4fd889fSMahmoud Mandour             return ret;
92364031f1SWei Liu         }
93364031f1SWei Liu     }
94364031f1SWei Liu     /* Add the name */
95b05528b5SGreg Kurz     node = v9fs_add_dir_node(parent, mode, name, NULL, synth_node_count++);
96364031f1SWei Liu     v9fs_add_dir_node(node, parent->attr->mode, "..",
97364031f1SWei Liu                       parent->attr, parent->attr->inode);
98364031f1SWei Liu     v9fs_add_dir_node(node, node->attr->mode, ".",
99364031f1SWei Liu                       node->attr, node->attr->inode);
100364031f1SWei Liu     *result = node;
101364031f1SWei Liu     ret = 0;
102364031f1SWei Liu     return ret;
103364031f1SWei Liu }
104364031f1SWei Liu 
105364031f1SWei Liu int qemu_v9fs_synth_add_file(V9fsSynthNode *parent, int mode,
106364031f1SWei Liu                              const char *name, v9fs_synth_read read,
107364031f1SWei Liu                              v9fs_synth_write write, void *arg)
108364031f1SWei Liu {
109364031f1SWei Liu     int ret;
110364031f1SWei Liu     V9fsSynthNode *node, *tmp;
111364031f1SWei Liu 
112b05528b5SGreg Kurz     if (!synth_fs) {
113364031f1SWei Liu         return EAGAIN;
114364031f1SWei Liu     }
115364031f1SWei Liu     if (!name || (strlen(name) >= NAME_MAX)) {
116364031f1SWei Liu         return EINVAL;
117364031f1SWei Liu     }
118364031f1SWei Liu     if (!parent) {
119b05528b5SGreg Kurz         parent = &synth_root;
120364031f1SWei Liu     }
121364031f1SWei Liu 
122e4fd889fSMahmoud Mandour     QEMU_LOCK_GUARD(&synth_mutex);
123364031f1SWei Liu     QLIST_FOREACH(tmp, &parent->child, sibling) {
124364031f1SWei Liu         if (!strcmp(tmp->name, name)) {
125364031f1SWei Liu             ret = EEXIST;
126e4fd889fSMahmoud Mandour             return ret;
127364031f1SWei Liu         }
128364031f1SWei Liu     }
129364031f1SWei Liu     /* Add file type and remove write bits */
130364031f1SWei Liu     mode = ((mode & 0777) | S_IFREG);
131364031f1SWei Liu     node = g_malloc0(sizeof(V9fsSynthNode));
132364031f1SWei Liu     node->attr         = &node->actual_attr;
133b05528b5SGreg Kurz     node->attr->inode  = synth_node_count++;
134364031f1SWei Liu     node->attr->nlink  = 1;
135364031f1SWei Liu     node->attr->read   = read;
136364031f1SWei Liu     node->attr->write  = write;
137364031f1SWei Liu     node->attr->mode   = mode;
138364031f1SWei Liu     node->private      = arg;
139364031f1SWei Liu     pstrcpy(node->name, sizeof(node->name), name);
140364031f1SWei Liu     QLIST_INSERT_HEAD_RCU(&parent->child, node, sibling);
141364031f1SWei Liu     ret = 0;
142364031f1SWei Liu     return ret;
143364031f1SWei Liu }
144364031f1SWei Liu 
145b05528b5SGreg Kurz static void synth_fill_statbuf(V9fsSynthNode *node, struct stat *stbuf)
146364031f1SWei Liu {
147364031f1SWei Liu     stbuf->st_dev = 0;
148364031f1SWei Liu     stbuf->st_ino = node->attr->inode;
149364031f1SWei Liu     stbuf->st_mode = node->attr->mode;
150364031f1SWei Liu     stbuf->st_nlink = node->attr->nlink;
151364031f1SWei Liu     stbuf->st_uid = 0;
152364031f1SWei Liu     stbuf->st_gid = 0;
153364031f1SWei Liu     stbuf->st_rdev = 0;
154364031f1SWei Liu     stbuf->st_size = 0;
155364031f1SWei Liu     stbuf->st_blksize = 0;
156364031f1SWei Liu     stbuf->st_blocks = 0;
157364031f1SWei Liu     stbuf->st_atime = 0;
158364031f1SWei Liu     stbuf->st_mtime = 0;
159364031f1SWei Liu     stbuf->st_ctime = 0;
160364031f1SWei Liu }
161364031f1SWei Liu 
162b05528b5SGreg Kurz static int synth_lstat(FsContext *fs_ctx,
163364031f1SWei Liu                             V9fsPath *fs_path, struct stat *stbuf)
164364031f1SWei Liu {
165364031f1SWei Liu     V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;
166364031f1SWei Liu 
167b05528b5SGreg Kurz     synth_fill_statbuf(node, stbuf);
168364031f1SWei Liu     return 0;
169364031f1SWei Liu }
170364031f1SWei Liu 
171b05528b5SGreg Kurz static int synth_fstat(FsContext *fs_ctx, int fid_type,
172364031f1SWei Liu                             V9fsFidOpenState *fs, struct stat *stbuf)
173364031f1SWei Liu {
174364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
175b05528b5SGreg Kurz     synth_fill_statbuf(synth_open->node, stbuf);
176364031f1SWei Liu     return 0;
177364031f1SWei Liu }
178364031f1SWei Liu 
179b05528b5SGreg Kurz static int synth_opendir(FsContext *ctx,
180364031f1SWei Liu                              V9fsPath *fs_path, V9fsFidOpenState *fs)
181364031f1SWei Liu {
182364031f1SWei Liu     V9fsSynthOpenState *synth_open;
183364031f1SWei Liu     V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;
184364031f1SWei Liu 
185*e64e27d5SVitaly Chikunov     /*
186*e64e27d5SVitaly Chikunov      * V9fsSynthOpenState contains 'struct dirent' which have OS-specific
187*e64e27d5SVitaly Chikunov      * properties, thus it's zero cleared on allocation here and below
188*e64e27d5SVitaly Chikunov      * in synth_open.
189*e64e27d5SVitaly Chikunov      */
190*e64e27d5SVitaly Chikunov     synth_open = g_new0(V9fsSynthOpenState, 1);
191364031f1SWei Liu     synth_open->node = node;
192364031f1SWei Liu     node->open_count++;
193364031f1SWei Liu     fs->private = synth_open;
194364031f1SWei Liu     return 0;
195364031f1SWei Liu }
196364031f1SWei Liu 
197b05528b5SGreg Kurz static int synth_closedir(FsContext *ctx, V9fsFidOpenState *fs)
198364031f1SWei Liu {
199364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
200364031f1SWei Liu     V9fsSynthNode *node = synth_open->node;
201364031f1SWei Liu 
202364031f1SWei Liu     node->open_count--;
203364031f1SWei Liu     g_free(synth_open);
204364031f1SWei Liu     fs->private = NULL;
205364031f1SWei Liu     return 0;
206364031f1SWei Liu }
207364031f1SWei Liu 
208b05528b5SGreg Kurz static off_t synth_telldir(FsContext *ctx, V9fsFidOpenState *fs)
209364031f1SWei Liu {
210364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
211364031f1SWei Liu     return synth_open->offset;
212364031f1SWei Liu }
213364031f1SWei Liu 
214b05528b5SGreg Kurz static void synth_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
215364031f1SWei Liu {
216364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
217364031f1SWei Liu     synth_open->offset = off;
218364031f1SWei Liu }
219364031f1SWei Liu 
220b05528b5SGreg Kurz static void synth_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
221364031f1SWei Liu {
222b05528b5SGreg Kurz     synth_seekdir(ctx, fs, 0);
223364031f1SWei Liu }
224364031f1SWei Liu 
225b05528b5SGreg Kurz static void synth_direntry(V9fsSynthNode *node,
226364031f1SWei Liu                                 struct dirent *entry, off_t off)
227364031f1SWei Liu {
228*e64e27d5SVitaly Chikunov     size_t sz = strlen(node->name) + 1;
229*e64e27d5SVitaly Chikunov     /*
230*e64e27d5SVitaly Chikunov      * 'entry' is always inside of V9fsSynthOpenState which have NAME_MAX
231*e64e27d5SVitaly Chikunov      * back padding. Ensure we do not overflow it.
232*e64e27d5SVitaly Chikunov      */
233*e64e27d5SVitaly Chikunov     g_assert(sizeof(struct dirent) + NAME_MAX >=
234*e64e27d5SVitaly Chikunov              offsetof(struct dirent, d_name) + sz);
235*e64e27d5SVitaly Chikunov     memcpy(entry->d_name, node->name, sz);
236364031f1SWei Liu     entry->d_ino = node->attr->inode;
237364031f1SWei Liu     entry->d_off = off + 1;
238364031f1SWei Liu }
239364031f1SWei Liu 
240b05528b5SGreg Kurz static struct dirent *synth_get_dentry(V9fsSynthNode *dir,
241635324e8SGreg Kurz                                             struct dirent *entry, off_t off)
242364031f1SWei Liu {
243364031f1SWei Liu     int i = 0;
244364031f1SWei Liu     V9fsSynthNode *node;
245364031f1SWei Liu 
246364031f1SWei Liu     rcu_read_lock();
247364031f1SWei Liu     QLIST_FOREACH(node, &dir->child, sibling) {
248364031f1SWei Liu         /* This is the off child of the directory */
249364031f1SWei Liu         if (i == off) {
250364031f1SWei Liu             break;
251364031f1SWei Liu         }
252364031f1SWei Liu         i++;
253364031f1SWei Liu     }
254364031f1SWei Liu     rcu_read_unlock();
255364031f1SWei Liu     if (!node) {
256364031f1SWei Liu         /* end of directory */
257635324e8SGreg Kurz         return NULL;
258364031f1SWei Liu     }
259b05528b5SGreg Kurz     synth_direntry(node, entry, off);
260635324e8SGreg Kurz     return entry;
261364031f1SWei Liu }
262364031f1SWei Liu 
263b05528b5SGreg Kurz static struct dirent *synth_readdir(FsContext *ctx, V9fsFidOpenState *fs)
264364031f1SWei Liu {
265635324e8SGreg Kurz     struct dirent *entry;
266364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
267364031f1SWei Liu     V9fsSynthNode *node = synth_open->node;
268b05528b5SGreg Kurz     entry = synth_get_dentry(node, &synth_open->dent, synth_open->offset);
269635324e8SGreg Kurz     if (entry) {
270364031f1SWei Liu         synth_open->offset++;
271364031f1SWei Liu     }
272635324e8SGreg Kurz     return entry;
273364031f1SWei Liu }
274364031f1SWei Liu 
275b05528b5SGreg Kurz static int synth_open(FsContext *ctx, V9fsPath *fs_path,
276364031f1SWei Liu                            int flags, V9fsFidOpenState *fs)
277364031f1SWei Liu {
278364031f1SWei Liu     V9fsSynthOpenState *synth_open;
279364031f1SWei Liu     V9fsSynthNode *node = *(V9fsSynthNode **)fs_path->data;
280364031f1SWei Liu 
281*e64e27d5SVitaly Chikunov     synth_open = g_new0(V9fsSynthOpenState, 1);
282364031f1SWei Liu     synth_open->node = node;
283364031f1SWei Liu     node->open_count++;
284364031f1SWei Liu     fs->private = synth_open;
285364031f1SWei Liu     return 0;
286364031f1SWei Liu }
287364031f1SWei Liu 
288b05528b5SGreg Kurz static int synth_open2(FsContext *fs_ctx, V9fsPath *dir_path,
289364031f1SWei Liu                             const char *name, int flags,
290364031f1SWei Liu                             FsCred *credp, V9fsFidOpenState *fs)
291364031f1SWei Liu {
292364031f1SWei Liu     errno = ENOSYS;
293364031f1SWei Liu     return -1;
294364031f1SWei Liu }
295364031f1SWei Liu 
296b05528b5SGreg Kurz static int synth_close(FsContext *ctx, V9fsFidOpenState *fs)
297364031f1SWei Liu {
298364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
299364031f1SWei Liu     V9fsSynthNode *node = synth_open->node;
300364031f1SWei Liu 
301364031f1SWei Liu     node->open_count--;
302364031f1SWei Liu     g_free(synth_open);
303364031f1SWei Liu     fs->private = NULL;
304364031f1SWei Liu     return 0;
305364031f1SWei Liu }
306364031f1SWei Liu 
307b05528b5SGreg Kurz static ssize_t synth_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
308364031f1SWei Liu                                   const struct iovec *iov,
309364031f1SWei Liu                                   int iovcnt, off_t offset)
310364031f1SWei Liu {
311364031f1SWei Liu     int i, count = 0, wcount;
312364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
313364031f1SWei Liu     V9fsSynthNode *node = synth_open->node;
314364031f1SWei Liu     if (!node->attr->write) {
315364031f1SWei Liu         errno = EPERM;
316364031f1SWei Liu         return -1;
317364031f1SWei Liu     }
318364031f1SWei Liu     for (i = 0; i < iovcnt; i++) {
319364031f1SWei Liu         wcount = node->attr->write(iov[i].iov_base, iov[i].iov_len,
320364031f1SWei Liu                                    offset, node->private);
321364031f1SWei Liu         offset += wcount;
322364031f1SWei Liu         count  += wcount;
323364031f1SWei Liu         /* If we wrote less than requested. we are done */
324364031f1SWei Liu         if (wcount < iov[i].iov_len) {
325364031f1SWei Liu             break;
326364031f1SWei Liu         }
327364031f1SWei Liu     }
328364031f1SWei Liu     return count;
329364031f1SWei Liu }
330364031f1SWei Liu 
331b05528b5SGreg Kurz static ssize_t synth_preadv(FsContext *ctx, V9fsFidOpenState *fs,
332364031f1SWei Liu                                  const struct iovec *iov,
333364031f1SWei Liu                                  int iovcnt, off_t offset)
334364031f1SWei Liu {
335364031f1SWei Liu     int i, count = 0, rcount;
336364031f1SWei Liu     V9fsSynthOpenState *synth_open = fs->private;
337364031f1SWei Liu     V9fsSynthNode *node = synth_open->node;
338364031f1SWei Liu     if (!node->attr->read) {
339364031f1SWei Liu         errno = EPERM;
340364031f1SWei Liu         return -1;
341364031f1SWei Liu     }
342364031f1SWei Liu     for (i = 0; i < iovcnt; i++) {
343364031f1SWei Liu         rcount = node->attr->read(iov[i].iov_base, iov[i].iov_len,
344364031f1SWei Liu                                   offset, node->private);
345364031f1SWei Liu         offset += rcount;
346364031f1SWei Liu         count  += rcount;
347364031f1SWei Liu         /* If we read less than requested. we are done */
348364031f1SWei Liu         if (rcount < iov[i].iov_len) {
349364031f1SWei Liu             break;
350364031f1SWei Liu         }
351364031f1SWei Liu     }
352364031f1SWei Liu     return count;
353364031f1SWei Liu }
354364031f1SWei Liu 
355b05528b5SGreg Kurz static int synth_truncate(FsContext *ctx, V9fsPath *path, off_t offset)
356364031f1SWei Liu {
357364031f1SWei Liu     errno = ENOSYS;
358364031f1SWei Liu     return -1;
359364031f1SWei Liu }
360364031f1SWei Liu 
361b05528b5SGreg Kurz static int synth_chmod(FsContext *fs_ctx, V9fsPath *path, FsCred *credp)
362364031f1SWei Liu {
363364031f1SWei Liu     errno = EPERM;
364364031f1SWei Liu     return -1;
365364031f1SWei Liu }
366364031f1SWei Liu 
367b05528b5SGreg Kurz static int synth_mknod(FsContext *fs_ctx, V9fsPath *path,
368364031f1SWei Liu                        const char *buf, FsCred *credp)
369364031f1SWei Liu {
370364031f1SWei Liu     errno = EPERM;
371364031f1SWei Liu     return -1;
372364031f1SWei Liu }
373364031f1SWei Liu 
374b05528b5SGreg Kurz static int synth_mkdir(FsContext *fs_ctx, V9fsPath *path,
375364031f1SWei Liu                        const char *buf, FsCred *credp)
376364031f1SWei Liu {
377364031f1SWei Liu     errno = EPERM;
378364031f1SWei Liu     return -1;
379364031f1SWei Liu }
380364031f1SWei Liu 
381b05528b5SGreg Kurz static ssize_t synth_readlink(FsContext *fs_ctx, V9fsPath *path,
382364031f1SWei Liu                                    char *buf, size_t bufsz)
383364031f1SWei Liu {
384364031f1SWei Liu     errno = ENOSYS;
385364031f1SWei Liu     return -1;
386364031f1SWei Liu }
387364031f1SWei Liu 
388b05528b5SGreg Kurz static int synth_symlink(FsContext *fs_ctx, const char *oldpath,
389364031f1SWei Liu                               V9fsPath *newpath, const char *buf, FsCred *credp)
390364031f1SWei Liu {
391364031f1SWei Liu     errno = EPERM;
392364031f1SWei Liu     return -1;
393364031f1SWei Liu }
394364031f1SWei Liu 
395b05528b5SGreg Kurz static int synth_link(FsContext *fs_ctx, V9fsPath *oldpath,
396364031f1SWei Liu                            V9fsPath *newpath, const char *buf)
397364031f1SWei Liu {
398364031f1SWei Liu     errno = EPERM;
399364031f1SWei Liu     return -1;
400364031f1SWei Liu }
401364031f1SWei Liu 
402b05528b5SGreg Kurz static int synth_rename(FsContext *ctx, const char *oldpath,
403364031f1SWei Liu                              const char *newpath)
404364031f1SWei Liu {
405364031f1SWei Liu     errno = EPERM;
406364031f1SWei Liu     return -1;
407364031f1SWei Liu }
408364031f1SWei Liu 
409b05528b5SGreg Kurz static int synth_chown(FsContext *fs_ctx, V9fsPath *path, FsCred *credp)
410364031f1SWei Liu {
411364031f1SWei Liu     errno = EPERM;
412364031f1SWei Liu     return -1;
413364031f1SWei Liu }
414364031f1SWei Liu 
415b05528b5SGreg Kurz static int synth_utimensat(FsContext *fs_ctx, V9fsPath *path,
416364031f1SWei Liu                                 const struct timespec *buf)
417364031f1SWei Liu {
418364031f1SWei Liu     errno = EPERM;
419364031f1SWei Liu     return 0;
420364031f1SWei Liu }
421364031f1SWei Liu 
422b05528b5SGreg Kurz static int synth_remove(FsContext *ctx, const char *path)
423364031f1SWei Liu {
424364031f1SWei Liu     errno = EPERM;
425364031f1SWei Liu     return -1;
426364031f1SWei Liu }
427364031f1SWei Liu 
428b05528b5SGreg Kurz static int synth_fsync(FsContext *ctx, int fid_type,
429364031f1SWei Liu                             V9fsFidOpenState *fs, int datasync)
430364031f1SWei Liu {
431364031f1SWei Liu     errno = ENOSYS;
432364031f1SWei Liu     return 0;
433364031f1SWei Liu }
434364031f1SWei Liu 
435b05528b5SGreg Kurz static int synth_statfs(FsContext *s, V9fsPath *fs_path,
436364031f1SWei Liu                              struct statfs *stbuf)
437364031f1SWei Liu {
438364031f1SWei Liu     stbuf->f_type = 0xABCD;
439364031f1SWei Liu     stbuf->f_bsize = 512;
440364031f1SWei Liu     stbuf->f_blocks = 0;
441b05528b5SGreg Kurz     stbuf->f_files = synth_node_count;
442364031f1SWei Liu     stbuf->f_namelen = NAME_MAX;
443364031f1SWei Liu     return 0;
444364031f1SWei Liu }
445364031f1SWei Liu 
446b05528b5SGreg Kurz static ssize_t synth_lgetxattr(FsContext *ctx, V9fsPath *path,
447364031f1SWei Liu                                     const char *name, void *value, size_t size)
448364031f1SWei Liu {
449364031f1SWei Liu     errno = ENOTSUP;
450364031f1SWei Liu     return -1;
451364031f1SWei Liu }
452364031f1SWei Liu 
453b05528b5SGreg Kurz static ssize_t synth_llistxattr(FsContext *ctx, V9fsPath *path,
454364031f1SWei Liu                                      void *value, size_t size)
455364031f1SWei Liu {
456364031f1SWei Liu     errno = ENOTSUP;
457364031f1SWei Liu     return -1;
458364031f1SWei Liu }
459364031f1SWei Liu 
460b05528b5SGreg Kurz static int synth_lsetxattr(FsContext *ctx, V9fsPath *path,
461364031f1SWei Liu                                 const char *name, void *value,
462364031f1SWei Liu                                 size_t size, int flags)
463364031f1SWei Liu {
464364031f1SWei Liu     errno = ENOTSUP;
465364031f1SWei Liu     return -1;
466364031f1SWei Liu }
467364031f1SWei Liu 
468b05528b5SGreg Kurz static int synth_lremovexattr(FsContext *ctx,
469364031f1SWei Liu                                    V9fsPath *path, const char *name)
470364031f1SWei Liu {
471364031f1SWei Liu     errno = ENOTSUP;
472364031f1SWei Liu     return -1;
473364031f1SWei Liu }
474364031f1SWei Liu 
475b05528b5SGreg Kurz static int synth_name_to_path(FsContext *ctx, V9fsPath *dir_path,
476364031f1SWei Liu                                    const char *name, V9fsPath *target)
477364031f1SWei Liu {
478364031f1SWei Liu     V9fsSynthNode *node;
479364031f1SWei Liu     V9fsSynthNode *dir_node;
480364031f1SWei Liu 
481364031f1SWei Liu     /* "." and ".." are not allowed */
482364031f1SWei Liu     if (!strcmp(name, ".") || !strcmp(name, "..")) {
483364031f1SWei Liu         errno = EINVAL;
484364031f1SWei Liu         return -1;
485364031f1SWei Liu 
486364031f1SWei Liu     }
487364031f1SWei Liu     if (!dir_path) {
488b05528b5SGreg Kurz         dir_node = &synth_root;
489364031f1SWei Liu     } else {
490364031f1SWei Liu         dir_node = *(V9fsSynthNode **)dir_path->data;
491364031f1SWei Liu     }
492364031f1SWei Liu     if (!strcmp(name, "/")) {
493364031f1SWei Liu         node = dir_node;
494364031f1SWei Liu         goto out;
495364031f1SWei Liu     }
496364031f1SWei Liu     /* search for the name in the childern */
497364031f1SWei Liu     rcu_read_lock();
498364031f1SWei Liu     QLIST_FOREACH(node, &dir_node->child, sibling) {
499364031f1SWei Liu         if (!strcmp(node->name, name)) {
500364031f1SWei Liu             break;
501364031f1SWei Liu         }
502364031f1SWei Liu     }
503364031f1SWei Liu     rcu_read_unlock();
504364031f1SWei Liu 
505364031f1SWei Liu     if (!node) {
506364031f1SWei Liu         errno = ENOENT;
507364031f1SWei Liu         return -1;
508364031f1SWei Liu     }
509364031f1SWei Liu out:
510364031f1SWei Liu     /* Copy the node pointer to fid */
5116ce7177aSMarc-André Lureau     g_free(target->data);
512453a1b23SMarc-André Lureau     target->data = g_memdup(&node, sizeof(void *));
513364031f1SWei Liu     target->size = sizeof(void *);
514364031f1SWei Liu     return 0;
515364031f1SWei Liu }
516364031f1SWei Liu 
517b05528b5SGreg Kurz static int synth_renameat(FsContext *ctx, V9fsPath *olddir,
518364031f1SWei Liu                                const char *old_name, V9fsPath *newdir,
519364031f1SWei Liu                                const char *new_name)
520364031f1SWei Liu {
521364031f1SWei Liu     errno = EPERM;
522364031f1SWei Liu     return -1;
523364031f1SWei Liu }
524364031f1SWei Liu 
525b05528b5SGreg Kurz static int synth_unlinkat(FsContext *ctx, V9fsPath *dir,
526364031f1SWei Liu                                const char *name, int flags)
527364031f1SWei Liu {
528364031f1SWei Liu     errno = EPERM;
529364031f1SWei Liu     return -1;
530364031f1SWei Liu }
531364031f1SWei Liu 
532354b86f8SGreg Kurz static ssize_t v9fs_synth_qtest_write(void *buf, int len, off_t offset,
533354b86f8SGreg Kurz                                       void *arg)
534354b86f8SGreg Kurz {
535354b86f8SGreg Kurz     return 1;
536354b86f8SGreg Kurz }
537354b86f8SGreg Kurz 
538357e2f7fSGreg Kurz static ssize_t v9fs_synth_qtest_flush_write(void *buf, int len, off_t offset,
539357e2f7fSGreg Kurz                                             void *arg)
540357e2f7fSGreg Kurz {
541357e2f7fSGreg Kurz     bool should_block = !!*(uint8_t *)buf;
542357e2f7fSGreg Kurz 
543357e2f7fSGreg Kurz     if (should_block) {
544357e2f7fSGreg Kurz         /* This will cause the server to call us again until we're cancelled */
545357e2f7fSGreg Kurz         errno = EINTR;
546357e2f7fSGreg Kurz         return -1;
547357e2f7fSGreg Kurz     }
548357e2f7fSGreg Kurz 
549357e2f7fSGreg Kurz     return 1;
550357e2f7fSGreg Kurz }
551357e2f7fSGreg Kurz 
55265603a80SGreg Kurz static int synth_init(FsContext *ctx, Error **errp)
553364031f1SWei Liu {
554b05528b5SGreg Kurz     QLIST_INIT(&synth_root.child);
555b05528b5SGreg Kurz     qemu_mutex_init(&synth_mutex);
556364031f1SWei Liu 
557364031f1SWei Liu     /* Add "." and ".." entries for root */
558b05528b5SGreg Kurz     v9fs_add_dir_node(&synth_root, synth_root.attr->mode,
559b05528b5SGreg Kurz                       "..", synth_root.attr, synth_root.attr->inode);
560b05528b5SGreg Kurz     v9fs_add_dir_node(&synth_root, synth_root.attr->mode,
561b05528b5SGreg Kurz                       ".", synth_root.attr, synth_root.attr->inode);
562364031f1SWei Liu 
563364031f1SWei Liu     /* Mark the subsystem is ready for use */
564b05528b5SGreg Kurz     synth_fs = 1;
5652893ddd5SGreg Kurz 
5662893ddd5SGreg Kurz     if (qtest_enabled()) {
5672893ddd5SGreg Kurz         V9fsSynthNode *node = NULL;
5682893ddd5SGreg Kurz         int i, ret;
5692893ddd5SGreg Kurz 
5702893ddd5SGreg Kurz         /* Directory hierarchy for WALK test */
5712893ddd5SGreg Kurz         for (i = 0; i < P9_MAXWELEM; i++) {
5722893ddd5SGreg Kurz             char *name = g_strdup_printf(QTEST_V9FS_SYNTH_WALK_FILE, i);
5732893ddd5SGreg Kurz 
5742893ddd5SGreg Kurz             ret = qemu_v9fs_synth_mkdir(node, 0700, name, &node);
5752893ddd5SGreg Kurz             assert(!ret);
5762893ddd5SGreg Kurz             g_free(name);
5772893ddd5SGreg Kurz         }
57882469aaeSGreg Kurz 
57982469aaeSGreg Kurz         /* File for LOPEN test */
58082469aaeSGreg Kurz         ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_LOPEN_FILE,
58182469aaeSGreg Kurz                                        NULL, NULL, ctx);
58282469aaeSGreg Kurz         assert(!ret);
583354b86f8SGreg Kurz 
584354b86f8SGreg Kurz         /* File for WRITE test */
585354b86f8SGreg Kurz         ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_WRITE_FILE,
586354b86f8SGreg Kurz                                        NULL, v9fs_synth_qtest_write, ctx);
587354b86f8SGreg Kurz         assert(!ret);
588357e2f7fSGreg Kurz 
589357e2f7fSGreg Kurz         /* File for FLUSH test */
590357e2f7fSGreg Kurz         ret = qemu_v9fs_synth_add_file(NULL, 0, QTEST_V9FS_SYNTH_FLUSH_FILE,
591357e2f7fSGreg Kurz                                        NULL, v9fs_synth_qtest_flush_write,
592357e2f7fSGreg Kurz                                        ctx);
593357e2f7fSGreg Kurz         assert(!ret);
594af46a3b2SChristian Schoenebeck 
595af46a3b2SChristian Schoenebeck         /* Directory for READDIR test */
596af46a3b2SChristian Schoenebeck         {
597af46a3b2SChristian Schoenebeck             V9fsSynthNode *dir = NULL;
598af46a3b2SChristian Schoenebeck             ret = qemu_v9fs_synth_mkdir(
599af46a3b2SChristian Schoenebeck                 NULL, 0700, QTEST_V9FS_SYNTH_READDIR_DIR, &dir
600af46a3b2SChristian Schoenebeck             );
601af46a3b2SChristian Schoenebeck             assert(!ret);
602af46a3b2SChristian Schoenebeck             for (i = 0; i < QTEST_V9FS_SYNTH_READDIR_NFILES; ++i) {
603af46a3b2SChristian Schoenebeck                 char *name = g_strdup_printf(
604af46a3b2SChristian Schoenebeck                     QTEST_V9FS_SYNTH_READDIR_FILE, i
605af46a3b2SChristian Schoenebeck                 );
606af46a3b2SChristian Schoenebeck                 ret = qemu_v9fs_synth_add_file(
607af46a3b2SChristian Schoenebeck                     dir, 0, name, NULL, NULL, ctx
608af46a3b2SChristian Schoenebeck                 );
609af46a3b2SChristian Schoenebeck                 assert(!ret);
610af46a3b2SChristian Schoenebeck                 g_free(name);
611af46a3b2SChristian Schoenebeck             }
612af46a3b2SChristian Schoenebeck         }
6132893ddd5SGreg Kurz     }
6142893ddd5SGreg Kurz 
615364031f1SWei Liu     return 0;
616364031f1SWei Liu }
617364031f1SWei Liu 
618364031f1SWei Liu FileOperations synth_ops = {
619b05528b5SGreg Kurz     .init         = synth_init,
620b05528b5SGreg Kurz     .lstat        = synth_lstat,
621b05528b5SGreg Kurz     .readlink     = synth_readlink,
622b05528b5SGreg Kurz     .close        = synth_close,
623b05528b5SGreg Kurz     .closedir     = synth_closedir,
624b05528b5SGreg Kurz     .open         = synth_open,
625b05528b5SGreg Kurz     .opendir      = synth_opendir,
626b05528b5SGreg Kurz     .rewinddir    = synth_rewinddir,
627b05528b5SGreg Kurz     .telldir      = synth_telldir,
628b05528b5SGreg Kurz     .readdir      = synth_readdir,
629b05528b5SGreg Kurz     .seekdir      = synth_seekdir,
630b05528b5SGreg Kurz     .preadv       = synth_preadv,
631b05528b5SGreg Kurz     .pwritev      = synth_pwritev,
632b05528b5SGreg Kurz     .chmod        = synth_chmod,
633b05528b5SGreg Kurz     .mknod        = synth_mknod,
634b05528b5SGreg Kurz     .mkdir        = synth_mkdir,
635b05528b5SGreg Kurz     .fstat        = synth_fstat,
636b05528b5SGreg Kurz     .open2        = synth_open2,
637b05528b5SGreg Kurz     .symlink      = synth_symlink,
638b05528b5SGreg Kurz     .link         = synth_link,
639b05528b5SGreg Kurz     .truncate     = synth_truncate,
640b05528b5SGreg Kurz     .rename       = synth_rename,
641b05528b5SGreg Kurz     .chown        = synth_chown,
642b05528b5SGreg Kurz     .utimensat    = synth_utimensat,
643b05528b5SGreg Kurz     .remove       = synth_remove,
644b05528b5SGreg Kurz     .fsync        = synth_fsync,
645b05528b5SGreg Kurz     .statfs       = synth_statfs,
646b05528b5SGreg Kurz     .lgetxattr    = synth_lgetxattr,
647b05528b5SGreg Kurz     .llistxattr   = synth_llistxattr,
648b05528b5SGreg Kurz     .lsetxattr    = synth_lsetxattr,
649b05528b5SGreg Kurz     .lremovexattr = synth_lremovexattr,
650b05528b5SGreg Kurz     .name_to_path = synth_name_to_path,
651b05528b5SGreg Kurz     .renameat     = synth_renameat,
652b05528b5SGreg Kurz     .unlinkat     = synth_unlinkat,
653364031f1SWei Liu };
654