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 <stdlib.h>
29
30 #include "tapdisk-driver.h"
31 #include "tapdisk-server.h"
32 #include "tapdisk-disktype.h"
33
34 td_driver_t *
tapdisk_driver_allocate(int type,char * name,td_flag_t flags,int storage)35 tapdisk_driver_allocate(int type, char *name, td_flag_t flags, int storage)
36 {
37 int err;
38 td_driver_t *driver;
39 const struct tap_disk *ops;
40
41 ops = tapdisk_disk_drivers[type];
42 if (!ops)
43 return NULL;
44
45 driver = calloc(1, sizeof(td_driver_t));
46 if (!driver)
47 return NULL;
48
49 err = tapdisk_namedup(&driver->name, name);
50 if (err)
51 goto fail;
52
53 driver->ops = ops;
54 driver->type = type;
55 driver->storage = storage;
56 driver->data = calloc(1, ops->private_data_size);
57 if (!driver->data)
58 goto fail;
59
60 if (td_flag_test(flags, TD_OPEN_RDONLY))
61 td_flag_set(driver->state, TD_DRIVER_RDONLY);
62
63 return driver;
64
65 fail:
66 free(driver->name);
67 free(driver->data);
68 free(driver);
69 return NULL;
70 }
71
72 void
tapdisk_driver_free(td_driver_t * driver)73 tapdisk_driver_free(td_driver_t *driver)
74 {
75 if (!driver)
76 return;
77
78 if (driver->refcnt)
79 return;
80
81 if (td_flag_test(driver->state, TD_DRIVER_OPEN))
82 EPRINTF("freeing open driver %s (state 0x%08x)\n",
83 driver->name, driver->state);
84
85 free(driver->name);
86 free(driver->data);
87 free(driver);
88 }
89
90 void
tapdisk_driver_queue_tiocb(td_driver_t * driver,struct tiocb * tiocb)91 tapdisk_driver_queue_tiocb(td_driver_t *driver, struct tiocb *tiocb)
92 {
93 tapdisk_server_queue_tiocb(tiocb);
94 }
95
96 void
tapdisk_driver_debug(td_driver_t * driver)97 tapdisk_driver_debug(td_driver_t *driver)
98 {
99 if (driver->ops->td_debug)
100 driver->ops->td_debug(driver);
101 }
102