debuggers.hg

view tools/blktap/drivers/tapdisk.h @ 0:7d21f7218375

Exact replica of unstable on 051908 + README-this
author Mukesh Rathor
date Mon May 19 15:34:57 2008 -0700 (2008-05-19)
parents
children dec845b4d9c3
line source
1 /* tapdisk.h
2 *
3 * Generic disk interface for blktap-based image adapters.
4 *
5 * (c) 2006 Andrew Warfield and Julian Chesterfield
6 *
7 * Some notes on the tap_disk interface:
8 *
9 * tap_disk aims to provide a generic interface to easily implement new
10 * types of image accessors. The structure-of-function-calls is similar
11 * to disk interfaces used in qemu/denali/etc, with the significant
12 * difference being the expectation of asynchronous rather than synchronous
13 * I/O. The asynchronous interface is intended to allow lots of requests to
14 * be pipelined through a disk, without the disk requiring any of its own
15 * threads of control. As such, a batch of requests is delivered to the disk
16 * using:
17 *
18 * td_queue_[read,write]()
19 *
20 * and passing in a completion callback, which the disk is responsible for
21 * tracking. The end of a back is marked with a call to:
22 *
23 * td_submit()
24 *
25 * The disk implementation must provide a file handle, which is used to
26 * indicate that it needs to do work. tapdisk will add this file handle
27 * (returned from td_get_fd()) to it's poll set, and will call into the disk
28 * using td_do_callbacks() whenever there is data pending.
29 *
30 * Two disk implementations demonstrate how this interface may be used to
31 * implement disks with both asynchronous and synchronous calls. block-aio.c
32 * maps this interface down onto the linux libaio calls, while block-sync uses
33 * normal posix read/write.
34 *
35 * A few things to realize about the sync case, which doesn't need to defer
36 * io completions:
37 *
38 * - td_queue_[read,write]() call read/write directly, and then call the
39 * callback immediately. The MUST then return a value greater than 0
40 * in order to tell tapdisk that requests have finished early, and to
41 * force responses to be kicked to the clents.
42 *
43 * - The fd used for poll is an otherwise unused pipe, which allows poll to
44 * be safely called without ever returning anything.
45 *
46 * NOTE: tapdisk uses the number of sectors submitted per request as a
47 * ref count. Plugins must use the callback function to communicate the
48 * completion--or error--of every sector submitted to them.
49 *
50 * td_get_parent_id returns:
51 * 0 if parent id successfully retrieved
52 * TD_NO_PARENT if no parent exists
53 * -errno on error
54 */
56 #ifndef TAPDISK_H_
57 #define TAPDISK_H_
59 #include <stdint.h>
60 #include <syslog.h>
61 #include <stdio.h>
62 #include "blktaplib.h"
64 /*If enabled, log all debug messages to syslog*/
65 #if 1
66 #define DPRINTF(_f, _a...) syslog( LOG_DEBUG, __FILE__ ":%d: " _f , __LINE__, ## _a )
67 #else
68 #define DPRINTF(_f, _a...) ((void)0)
69 #endif
71 /* Things disks need to know about, these should probably be in a higher-level
72 * header. */
73 #define MAX_SEGMENTS_PER_REQ 11
74 #define SECTOR_SHIFT 9
75 #define DEFAULT_SECTOR_SIZE 512
77 #define MAX_IOFD 2
79 #define BLK_NOT_ALLOCATED 99
80 #define TD_NO_PARENT 1
82 typedef uint32_t td_flag_t;
84 #define TD_RDONLY 1
86 struct td_state;
87 struct tap_disk;
89 struct disk_id {
90 char *name;
91 int drivertype;
92 };
94 struct disk_driver {
95 int early;
96 char *name;
97 void *private;
98 td_flag_t flags;
99 int io_fd[MAX_IOFD];
100 struct tap_disk *drv;
101 struct td_state *td_state;
102 struct disk_driver *next;
103 };
105 /* This structure represents the state of an active virtual disk. */
106 struct td_state {
107 struct disk_driver *disks;
108 void *blkif;
109 void *image;
110 void *ring_info;
111 void *fd_entry;
112 uint64_t sector_size;
113 uint64_t size;
114 unsigned int info;
115 };
117 /* Prototype of the callback to activate as requests complete. */
118 typedef int (*td_callback_t)(struct disk_driver *dd, int res, uint64_t sector,
119 int nb_sectors, int id, void *private);
121 /* Structure describing the interface to a virtual disk implementation. */
122 /* See note at the top of this file describing this interface. */
123 struct tap_disk {
124 const char *disk_type;
125 int private_data_size;
126 int (*td_open) (struct disk_driver *dd,
127 const char *name, td_flag_t flags);
128 int (*td_queue_read) (struct disk_driver *dd, uint64_t sector,
129 int nb_sectors, char *buf, td_callback_t cb,
130 int id, void *prv);
131 int (*td_queue_write) (struct disk_driver *dd, uint64_t sector,
132 int nb_sectors, char *buf, td_callback_t cb,
133 int id, void *prv);
134 int (*td_submit) (struct disk_driver *dd);
135 int (*td_close) (struct disk_driver *dd);
136 int (*td_do_callbacks) (struct disk_driver *dd, int sid);
137 int (*td_get_parent_id) (struct disk_driver *dd, struct disk_id *id);
138 int (*td_validate_parent)(struct disk_driver *dd,
139 struct disk_driver *p, td_flag_t flags);
140 };
142 typedef struct disk_info {
143 int idnum;
144 char name[50]; /* e.g. "RAMDISK" */
145 char handle[10]; /* xend handle, e.g. 'ram' */
146 int single_handler; /* is there a single controller for all */
147 /* instances of disk type? */
148 #ifdef TAPDISK
149 struct tap_disk *drv;
150 #endif
151 } disk_info_t;
153 void debug_fe_ring(struct td_state *s);
155 extern struct tap_disk tapdisk_aio;
156 extern struct tap_disk tapdisk_sync;
157 extern struct tap_disk tapdisk_vmdk;
158 extern struct tap_disk tapdisk_ram;
159 extern struct tap_disk tapdisk_qcow;
160 extern struct tap_disk tapdisk_qcow2;
162 #define MAX_DISK_TYPES 20
164 #define DISK_TYPE_AIO 0
165 #define DISK_TYPE_SYNC 1
166 #define DISK_TYPE_VMDK 2
167 #define DISK_TYPE_RAM 3
168 #define DISK_TYPE_QCOW 4
169 #define DISK_TYPE_QCOW2 5
170 #define DISK_TYPE_IOEMU 6
173 /*Define Individual Disk Parameters here */
174 static disk_info_t aio_disk = {
175 DISK_TYPE_AIO,
176 "raw image (aio)",
177 "aio",
178 0,
179 #ifdef TAPDISK
180 &tapdisk_aio,
181 #endif
182 };
184 static disk_info_t sync_disk = {
185 DISK_TYPE_SYNC,
186 "raw image (sync)",
187 "sync",
188 0,
189 #ifdef TAPDISK
190 &tapdisk_sync,
191 #endif
192 };
194 static disk_info_t vmdk_disk = {
195 DISK_TYPE_VMDK,
196 "vmware image (vmdk)",
197 "vmdk",
198 1,
199 #ifdef TAPDISK
200 &tapdisk_vmdk,
201 #endif
202 };
204 static disk_info_t ram_disk = {
205 DISK_TYPE_RAM,
206 "ramdisk image (ram)",
207 "ram",
208 1,
209 #ifdef TAPDISK
210 &tapdisk_ram,
211 #endif
212 };
214 static disk_info_t qcow_disk = {
215 DISK_TYPE_QCOW,
216 "qcow disk (qcow)",
217 "qcow",
218 0,
219 #ifdef TAPDISK
220 &tapdisk_qcow,
221 #endif
222 };
224 static disk_info_t qcow2_disk = {
225 DISK_TYPE_QCOW2,
226 "qcow2 disk (qcow2)",
227 "qcow2",
228 0,
229 #ifdef TAPDISK
230 &tapdisk_qcow2,
231 #endif
232 };
234 static disk_info_t ioemu_disk = {
235 DISK_TYPE_IOEMU,
236 "ioemu disk",
237 "ioemu",
238 1,
239 #ifdef TAPDISK
240 NULL
241 #endif
242 };
244 /*Main disk info array */
245 static disk_info_t *dtypes[] = {
246 &aio_disk,
247 &sync_disk,
248 &vmdk_disk,
249 &ram_disk,
250 &qcow_disk,
251 &qcow2_disk,
252 &ioemu_disk,
253 };
255 typedef struct driver_list_entry {
256 struct blkif *blkif;
257 struct driver_list_entry **pprev, *next;
258 } driver_list_entry_t;
260 typedef struct fd_list_entry {
261 int cookie;
262 int tap_fd;
263 struct td_state *s;
264 struct fd_list_entry **pprev, *next;
265 } fd_list_entry_t;
267 int qcow_create(const char *filename, uint64_t total_size,
268 const char *backing_file, int flags);
269 #endif /*TAPDISK_H_*/