1 /* drivers/xen/blktap/xenbus.c
3 * Xenbus code for blktap
5 * Copyright (c) 2004-2005, Andrew Warfield and Julian Chesterfield
7 * Based on the blkback xenbus code:
9 * Copyright (C) 2005 Rusty Russell <rusty@rustcorp.com.au>
10 * Copyright (C) 2005 XenSource Ltd
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License version 2
14 * as published by the Free Software Foundation; or, when distributed
15 * separately from the Linux kernel or incorporated into other
16 * software packages, subject to the following license:
18 * Permission is hereby granted, free of charge, to any person obtaining a copy
19 * of this source file (the "Software"), to deal in the Software without
20 * restriction, including without limitation the rights to use, copy, modify,
21 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
22 * and to permit persons to whom the Software is furnished to do so, subject to
23 * the following conditions:
25 * The above copyright notice and this permission notice shall be included in
26 * all copies or substantial portions of the Software.
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
33 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
38 #include <linux/module.h>
39 #include <linux/kthread.h>
40 #include <xen/xenbus.h>
46 struct xenbus_device *dev;
48 struct xenbus_watch backend_watch;
53 static void connect(struct backend_info *);
54 static int connect_ring(struct backend_info *);
55 static int blktap_remove(struct xenbus_device *dev);
56 static int blktap_probe(struct xenbus_device *dev,
57 const struct xenbus_device_id *id);
58 static void tap_backend_changed(struct xenbus_watch *, const char **,
60 static void tap_frontend_changed(struct xenbus_device *dev,
61 enum xenbus_state frontend_state);
63 static int strsep_len(const char *str, char c, unsigned int len)
67 for (i = 0; str[i]; i++)
73 return (len == 0) ? i : -ERANGE;
76 static long get_id(const char *str)
82 len = strsep_len(str, '/', 2);
84 if ( (len < 0) || (end < 0) ) return -1;
87 strncpy(num,ptr,end - len);
88 tptr = num + (end - (len + 1));
90 DPRINTK("Get_id called for %s (%s)\n",str,num);
92 return simple_strtol(num, NULL, 10);
95 static void tap_update_blkif_status(blkif_t *blkif)
99 /* Not ready to connect? */
100 if(!blkif->irq || !blkif->sectors) {
104 /* Already connected? */
105 if (blkif->be->dev->state == XenbusStateConnected)
108 /* Attempt to connect: exit if we fail to. */
110 if (blkif->be->dev->state != XenbusStateConnected)
113 blkif->xenblkd = kthread_run(tap_blkif_schedule, blkif,
117 if (IS_ERR(blkif->xenblkd)) {
118 err = PTR_ERR(blkif->xenblkd);
119 blkif->xenblkd = NULL;
120 xenbus_dev_fatal(blkif->be->dev, err, "start xenblkd");
121 WPRINTK("Error starting thread\n");
125 static int blktap_remove(struct xenbus_device *dev)
127 struct backend_info *be = dev->dev.driver_data;
129 if (be->backend_watch.node) {
130 unregister_xenbus_watch(&be->backend_watch);
131 kfree(be->backend_watch.node);
132 be->backend_watch.node = NULL;
135 if (be->blkif->xenblkd)
136 kthread_stop(be->blkif->xenblkd);
137 signal_tapdisk(be->blkif->dev_num);
138 tap_blkif_free(be->blkif);
142 dev->dev.driver_data = NULL;
147 * Entry point to this code when a new device is created. Allocate
148 * the basic structures, and watch the store waiting for the
149 * user-space program to tell us the physical device info. Switch to
152 static int blktap_probe(struct xenbus_device *dev,
153 const struct xenbus_device_id *id)
156 struct backend_info *be = kzalloc(sizeof(struct backend_info),
159 xenbus_dev_fatal(dev, -ENOMEM,
160 "allocating backend structure");
165 dev->dev.driver_data = be;
166 be->xenbus_id = get_id(dev->nodename);
168 be->blkif = tap_alloc_blkif(dev->otherend_id);
169 if (IS_ERR(be->blkif)) {
170 err = PTR_ERR(be->blkif);
172 xenbus_dev_fatal(dev, err, "creating block interface");
176 /* setup back pointer */
178 be->blkif->sectors = 0;
180 /* set a watch on disk info, waiting for userspace to update details*/
181 err = xenbus_watch_path2(dev, dev->nodename, "info",
182 &be->backend_watch, tap_backend_changed);
186 err = xenbus_switch_state(dev, XenbusStateInitWait);
192 DPRINTK("blktap probe failed");
199 * Callback received when the user space code has placed the device
200 * information in xenstore.
202 static void tap_backend_changed(struct xenbus_watch *watch,
203 const char **vec, unsigned int len)
207 struct backend_info *be
208 = container_of(watch, struct backend_info, backend_watch);
209 struct xenbus_device *dev = be->dev;
212 * Check to see whether userspace code has opened the image
214 * and disk info to xenstore
216 err = xenbus_gather(XBT_NIL, dev->nodename, "info", "%lu", &info,
219 xenbus_dev_error(dev, err, "getting info");
223 DPRINTK("Userspace update on disk info, %lu\n",info);
225 err = xenbus_gather(XBT_NIL, dev->nodename, "sectors", "%llu",
226 &be->blkif->sectors, NULL);
228 /* Associate tap dev with domid*/
229 be->blkif->dev_num = dom_to_devid(be->blkif->domid, be->xenbus_id,
231 DPRINTK("Thread started for domid [%d], connecting disk\n",
234 tap_update_blkif_status(be->blkif);
238 * Callback received when the frontend's state changes.
240 static void tap_frontend_changed(struct xenbus_device *dev,
241 enum xenbus_state frontend_state)
243 struct backend_info *be = dev->dev.driver_data;
248 switch (frontend_state) {
249 case XenbusStateInitialising:
250 if (dev->state == XenbusStateClosed) {
251 printk("%s: %s: prepare for reconnect\n",
252 __FUNCTION__, dev->nodename);
253 xenbus_switch_state(dev, XenbusStateInitWait);
257 case XenbusStateInitialised:
258 case XenbusStateConnected:
259 /* Ensure we connect even when two watches fire in
260 close successsion and we miss the intermediate value
261 of frontend_state. */
262 if (dev->state == XenbusStateConnected)
265 err = connect_ring(be);
268 tap_update_blkif_status(be->blkif);
271 case XenbusStateClosing:
272 if (be->blkif->xenblkd) {
273 kthread_stop(be->blkif->xenblkd);
274 be->blkif->xenblkd = NULL;
276 xenbus_switch_state(dev, XenbusStateClosing);
279 case XenbusStateClosed:
280 xenbus_switch_state(dev, XenbusStateClosed);
281 if (xenbus_dev_is_online(dev))
283 /* fall through if not online */
284 case XenbusStateUnknown:
285 device_unregister(&dev->dev);
289 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
297 * Switch to Connected state.
299 static void connect(struct backend_info *be)
303 struct xenbus_device *dev = be->dev;
305 err = xenbus_switch_state(dev, XenbusStateConnected);
307 xenbus_dev_fatal(dev, err, "switching to Connected state",
314 static int connect_ring(struct backend_info *be)
316 struct xenbus_device *dev = be->dev;
317 unsigned long ring_ref;
321 DPRINTK("%s", dev->otherend);
323 err = xenbus_gather(XBT_NIL, dev->otherend, "ring-ref", "%lu",
324 &ring_ref, "event-channel", "%u", &evtchn, NULL);
326 xenbus_dev_fatal(dev, err,
327 "reading %s/ring-ref and event-channel",
332 /* Map the shared frame, irq etc. */
333 err = tap_blkif_map(be->blkif, ring_ref, evtchn);
335 xenbus_dev_fatal(dev, err, "mapping ring-ref %lu port %u",
344 /* ** Driver Registration ** */
347 static struct xenbus_device_id blktap_ids[] = {
353 static struct xenbus_driver blktap = {
355 .owner = THIS_MODULE,
357 .probe = blktap_probe,
358 .remove = blktap_remove,
359 .otherend_changed = tap_frontend_changed
363 void tap_blkif_xenbus_init(void)
365 xenbus_register_backend(&blktap);