/*
 * Virtio 9p backend
 *
 * Copyright IBM, Corp. 2010
 *
 * Authors:
 *  Harsh Prateek Bora <harsh@linux.vnet.ibm.com>
 *  Venkateswararao Jujjuri(JV) <jvrao@linux.vnet.ibm.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 * the COPYING file in the top-level directory.
 *
 */

#ifndef _QEMU_VIRTIO_9P_COTH_H
#define _QEMU_VIRTIO_9P_COTH_H

#include "qemu/thread.h"
#include "block/coroutine.h"
#include "virtio-9p.h"
#include <glib.h>

typedef struct V9fsThPool {
    int rfd;
    int wfd;
    GThreadPool *pool;
    GAsyncQueue *completed;
} V9fsThPool;

/*
 * we want to use bottom half because we want to make sure the below
 * sequence of events.
 *
 *   1. Yield the coroutine in the QEMU thread.
 *   2. Submit the coroutine to a worker thread.
 *   3. Enter the coroutine in the worker thread.
 * we cannot swap step 1 and 2, because that would imply worker thread
 * can enter coroutine while step1 is still running
 */
#define v9fs_co_run_in_worker(code_block)                               \
    do {                                                                \
        QEMUBH *co_bh;                                                  \
        co_bh = qemu_bh_new(co_run_in_worker_bh,                        \
                            qemu_coroutine_self());                     \
        qemu_bh_schedule(co_bh);                                        \
        /*                                                              \
         * yield in qemu thread and re-enter back                       \
         * in glib worker thread                                        \
         */                                                             \
        qemu_coroutine_yield();                                         \
        qemu_bh_delete(co_bh);                                          \
        code_block;                                                     \
        /* re-enter back to qemu thread */                              \
        qemu_coroutine_yield();                                         \
    } while (0)

extern void co_run_in_worker_bh(void *);
extern int v9fs_init_worker_threads(void);
extern int v9fs_co_readlink(V9fsPDU *, V9fsPath *, V9fsString *);
extern int v9fs_co_readdir_r(V9fsPDU *, V9fsFidState *,
                           struct dirent *, struct dirent **result);
extern off_t v9fs_co_telldir(V9fsPDU *, V9fsFidState *);
extern void v9fs_co_seekdir(V9fsPDU *, V9fsFidState *, off_t);
extern void v9fs_co_rewinddir(V9fsPDU *, V9fsFidState *);
extern int v9fs_co_statfs(V9fsPDU *, V9fsPath *, struct statfs *);
extern int v9fs_co_lstat(V9fsPDU *, V9fsPath *, struct stat *);
extern int v9fs_co_chmod(V9fsPDU *, V9fsPath *, mode_t);
extern int v9fs_co_utimensat(V9fsPDU *, V9fsPath *, struct timespec [2]);
extern int v9fs_co_chown(V9fsPDU *, V9fsPath *, uid_t, gid_t);
extern int v9fs_co_truncate(V9fsPDU *, V9fsPath *, off_t);
extern int v9fs_co_llistxattr(V9fsPDU *, V9fsPath *, void *, size_t);
extern int v9fs_co_lgetxattr(V9fsPDU *, V9fsPath *,
                             V9fsString *, void *, size_t);
extern int v9fs_co_mknod(V9fsPDU *, V9fsFidState *, V9fsString *, uid_t,
                         gid_t, dev_t, mode_t, struct stat *);
extern int v9fs_co_mkdir(V9fsPDU *, V9fsFidState *, V9fsString *,
                         mode_t, uid_t, gid_t, struct stat *);
extern int v9fs_co_remove(V9fsPDU *, V9fsPath *);
extern int v9fs_co_rename(V9fsPDU *, V9fsPath *, V9fsPath *);
extern int v9fs_co_unlinkat(V9fsPDU *, V9fsPath *, V9fsString *, int flags);
extern int v9fs_co_renameat(V9fsPDU *, V9fsPath *, V9fsString *,
                            V9fsPath *, V9fsString *);
extern int v9fs_co_fstat(V9fsPDU *, V9fsFidState *, struct stat *);
extern int v9fs_co_opendir(V9fsPDU *, V9fsFidState *);
extern int v9fs_co_open(V9fsPDU *, V9fsFidState *, int);
extern int v9fs_co_open2(V9fsPDU *, V9fsFidState *, V9fsString *,
                         gid_t, int, int, struct stat *);
extern int v9fs_co_lsetxattr(V9fsPDU *, V9fsPath *, V9fsString *,
                             void *, size_t, int);
extern int v9fs_co_lremovexattr(V9fsPDU *, V9fsPath *, V9fsString *);
extern int v9fs_co_closedir(V9fsPDU *, V9fsFidOpenState *);
extern int v9fs_co_close(V9fsPDU *, V9fsFidOpenState *);
extern int v9fs_co_fsync(V9fsPDU *, V9fsFidState *, int);
extern int v9fs_co_symlink(V9fsPDU *, V9fsFidState *, V9fsString *,
                           const char *, gid_t, struct stat *);
extern int v9fs_co_link(V9fsPDU *, V9fsFidState *,
                        V9fsFidState *, V9fsString *);
extern int v9fs_co_pwritev(V9fsPDU *, V9fsFidState *,
                           struct iovec *, int, int64_t);
extern int v9fs_co_preadv(V9fsPDU *, V9fsFidState *,
                          struct iovec *, int, int64_t);
extern int v9fs_co_name_to_path(V9fsPDU *, V9fsPath *,
                                const char *, V9fsPath *);
extern int v9fs_co_st_gen(V9fsPDU *pdu, V9fsPath *path, mode_t,
                          V9fsStatDotl *v9stat);

#endif
