debuggers.hg

view tools/blktap2/drivers/tapdisk-interface.c @ 19709:ae810b258394

blktap2: Fix build with gcc3. Cannot handle defining a function which
is passed a struct-by-value which is not yet fully defined. Thus
defining a request struct which contains a pointer to a function which
is passed-by-value an instance of that request structure is
impossible. We work around it by defining the function poiinter as
void* and then casting in one place.

Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Thu May 28 11:01:00 2009 +0100 (2009-05-28)
parents 1c627434605e
children 3ffdb094c2c0
line source
1 /*
2 * Copyright (c) 2008, XenSource Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of XenSource Inc. nor the names of its contributors
13 * may be used to endorse or promote products derived from this software
14 * without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
20 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 #include <errno.h>
30 #include "tapdisk.h"
31 #include "tapdisk-vbd.h"
32 #include "tapdisk-image.h"
33 #include "tapdisk-driver.h"
34 #include "tapdisk-server.h"
35 #include "tapdisk-interface.h"
37 int
38 td_load(td_image_t *image)
39 {
40 int err;
41 td_image_t *shared;
42 td_driver_t *driver;
44 shared = tapdisk_server_get_shared_image(image);
45 if (!shared)
46 return -ENODEV;
48 driver = shared->driver;
49 if (!driver)
50 return -EBADF;
52 driver->refcnt++;
53 image->driver = driver;
54 image->info = driver->info;
56 DPRINTF("loaded shared image %s (%d users, state: 0x%08x, type: %d)\n",
57 driver->name, driver->refcnt, driver->state, driver->type);
58 return 0;
59 }
61 int
62 td_open(td_image_t *image)
63 {
64 int err;
65 td_driver_t *driver;
67 driver = image->driver;
68 if (!driver) {
69 driver = tapdisk_driver_allocate(image->type,
70 image->name,
71 image->flags,
72 image->storage);
73 if (!driver)
74 return -ENOMEM;
75 }
77 if (!td_flag_test(driver->state, TD_DRIVER_OPEN)) {
78 err = driver->ops->td_open(driver, image->name, image->flags);
79 if (err) {
80 if (!image->driver)
81 tapdisk_driver_free(driver);
82 return err;
83 }
85 td_flag_set(driver->state, TD_DRIVER_OPEN);
86 DPRINTF("opened image %s (%d users, state: 0x%08x, type: %d)\n",
87 driver->name, driver->refcnt + 1,
88 driver->state, driver->type);
89 }
91 image->driver = driver;
92 image->info = driver->info;
93 driver->refcnt++;
94 return 0;
95 }
97 int
98 td_close(td_image_t *image)
99 {
100 td_driver_t *driver;
102 driver = image->driver;
103 if (!driver)
104 return -ENODEV;
106 driver->refcnt--;
107 if (!driver->refcnt && td_flag_test(driver->state, TD_DRIVER_OPEN)) {
108 driver->ops->td_close(driver);
109 td_flag_clear(driver->state, TD_DRIVER_OPEN);
110 }
112 DPRINTF("closed image %s (%d users, state: 0x%08x, type: %d)\n",
113 driver->name, driver->refcnt, driver->state, driver->type);
115 return 0;
116 }
118 int
119 td_get_parent_id(td_image_t *image, td_disk_id_t *id)
120 {
121 td_driver_t *driver;
123 driver = image->driver;
124 if (!driver)
125 return -ENODEV;
127 if (!td_flag_test(driver->state, TD_DRIVER_OPEN))
128 return -EBADF;
130 return driver->ops->td_get_parent_id(driver, id);
131 }
133 int
134 td_validate_parent(td_image_t *image, td_image_t *parent)
135 {
136 td_driver_t *driver, *pdriver;
138 driver = image->driver;
139 pdriver = parent->driver;
140 if (!driver || !pdriver)
141 return -ENODEV;
143 if (!td_flag_test(driver->state, TD_DRIVER_OPEN) ||
144 !td_flag_test(pdriver->state, TD_DRIVER_OPEN))
145 return -EBADF;
147 return 0;
148 return driver->ops->td_validate_parent(driver, pdriver, 0);
149 }
151 void
152 td_queue_write(td_image_t *image, td_request_t treq)
153 {
154 int err;
155 td_driver_t *driver;
157 driver = image->driver;
158 if (!driver) {
159 err = -ENODEV;
160 goto fail;
161 }
163 if (!td_flag_test(driver->state, TD_DRIVER_OPEN)) {
164 err = -EBADF;
165 goto fail;
166 }
168 err = tapdisk_image_check_td_request(image, treq);
169 if (err)
170 goto fail;
172 driver->ops->td_queue_write(driver, treq);
173 return;
175 fail:
176 td_complete_request(treq, err);
177 }
179 void
180 td_queue_read(td_image_t *image, td_request_t treq)
181 {
182 int err;
183 td_driver_t *driver;
185 driver = image->driver;
186 if (!driver) {
187 err = -ENODEV;
188 goto fail;
189 }
191 if (!td_flag_test(driver->state, TD_DRIVER_OPEN)) {
192 err = -EBADF;
193 goto fail;
194 }
196 err = tapdisk_image_check_td_request(image, treq);
197 if (err)
198 goto fail;
200 driver->ops->td_queue_read(driver, treq);
201 return;
203 fail:
204 td_complete_request(treq, err);
205 }
207 void
208 td_forward_request(td_request_t treq)
209 {
210 tapdisk_vbd_forward_request(treq);
211 }
213 void
214 td_complete_request(td_request_t treq, int res)
215 {
216 ((td_callback_t)treq.cb)(treq, res);
217 }
219 void
220 td_queue_tiocb(td_driver_t *driver, struct tiocb *tiocb)
221 {
222 tapdisk_driver_queue_tiocb(driver, tiocb);
223 }
225 void
226 td_prep_read(struct tiocb *tiocb, int fd, char *buf, size_t bytes,
227 long long offset, td_queue_callback_t cb, void *arg)
228 {
229 tapdisk_prep_tiocb(tiocb, fd, 0, buf, bytes, offset, cb, arg);
230 }
232 void
233 td_prep_write(struct tiocb *tiocb, int fd, char *buf, size_t bytes,
234 long long offset, td_queue_callback_t cb, void *arg)
235 {
236 tapdisk_prep_tiocb(tiocb, fd, 1, buf, bytes, offset, cb, arg);
237 }
239 void
240 td_debug(td_image_t *image)
241 {
242 td_driver_t *driver;
244 driver = image->driver;
245 if (!driver || !td_flag_test(driver->state, TD_DRIVER_OPEN))
247 return;
249 tapdisk_driver_debug(driver);
250 }