1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #ifndef XEN_9PFSD_H
4 #define XEN_9PFSD_H
5 
6 #include <pthread.h>
7 #include <stdbool.h>
8 #include <xenevtchn.h>
9 #include <xen_list.h>
10 #include <xen/xen.h>
11 #include <xen/io/xenbus.h>
12 #include <xen/io/9pfs.h>
13 
14 #define MAX_RINGS                4
15 #define MAX_RING_ORDER           9
16 #define MAX_OPEN_FILES_DEFAULT   5
17 
18 struct p9_header {
19     uint32_t size;
20     uint8_t cmd;
21     uint16_t tag;
22 } __attribute__((packed));
23 
24 struct p9_fid {
25     XEN_TAILQ_ENTRY(struct p9_fid) list;
26     unsigned int fid;
27     unsigned int ref;
28     int fd;
29     uint8_t mode;
30     bool opened;
31     bool isdir;
32     void *data;    /* File type specific. */
33     char path[];
34 };
35 
36 typedef struct device device;
37 
38 struct ring {
39     device *device;
40     pthread_t thread;
41     bool thread_active;
42     bool stop_thread;
43     pthread_cond_t cond;
44     pthread_mutex_t mutex;
45 
46     evtchn_port_t evtchn;
47     struct xen_9pfs_data_intf *intf;
48     unsigned int ring_order;
49     RING_IDX ring_size;
50 
51     /* Transport layer data. */
52     struct xen_9pfs_data data;
53     RING_IDX prod_pvt_in;
54     RING_IDX cons_pvt_out;
55 
56     /* Request and response handling. */
57     uint32_t max_size;
58     bool error;             /* Protocol error - stop processing. */
59     bool handle_response;   /* Main loop now handling response. */
60     void *buffer;           /* Request/response buffer. */
61     char *str;              /* String work space. */
62     unsigned int str_size;  /* Size of *str. */
63     unsigned int str_used;  /* Currently used size of *str. */
64 };
65 
66 struct device {
67     /* Admin data. */
68     XEN_TAILQ_ENTRY(device) list;
69     unsigned int last_seen;    /* Set in scan_backend(). */
70     unsigned int domid;
71     unsigned int devid;
72 
73     /* Tool side configuration data. */
74     char *host_path;
75     unsigned int max_space;
76     unsigned int max_files;
77     unsigned int max_open_files;
78     bool auto_delete;
79 
80     /* Connection data. */
81     enum xenbus_state backend_state;
82     enum xenbus_state frontend_state;
83     unsigned int num_rings;
84     struct ring *ring[MAX_RINGS];
85     int root_fd;
86 
87     /* File system handling. */
88     pthread_mutex_t fid_mutex;
89     XEN_TAILQ_HEAD(fidhead, struct p9_fid) fids;
90     struct p9_fid *root_fid;
91     unsigned int n_fids;
92 };
93 
94 extern xenevtchn_handle *xe;
95 
96 void *io_thread(void *arg);
97 void free_fids(device *device);
98 
99 #endif /* XEN_9PFSD_H */
100