direct-io.hg

view linux-2.6-xen-sparse/drivers/xen/blkback/xenbus.c @ 12496:0c0ef61de06b

[BLK] Handle block devices with more than 2^32 sectors.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kfraser@localhost.localdomain
date Tue Nov 21 10:16:58 2006 +0000 (2006-11-21)
parents 4eaadb2ae198
children e66f047bc97e
line source
1 /* Xenbus code for blkif backend
2 Copyright (C) 2005 Rusty Russell <rusty@rustcorp.com.au>
3 Copyright (C) 2005 XenSource Ltd
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
20 #include <stdarg.h>
21 #include <linux/module.h>
22 #include <linux/kthread.h>
23 #include "common.h"
25 #undef DPRINTK
26 #define DPRINTK(fmt, args...) \
27 pr_debug("blkback/xenbus (%s:%d) " fmt ".\n", \
28 __FUNCTION__, __LINE__, ##args)
30 struct backend_info
31 {
32 struct xenbus_device *dev;
33 blkif_t *blkif;
34 struct xenbus_watch backend_watch;
35 unsigned major;
36 unsigned minor;
37 char *mode;
38 };
40 static void connect(struct backend_info *);
41 static int connect_ring(struct backend_info *);
42 static void backend_changed(struct xenbus_watch *, const char **,
43 unsigned int);
45 static void update_blkif_status(blkif_t *blkif)
46 {
47 int err;
49 /* Not ready to connect? */
50 if (!blkif->irq || !blkif->vbd.bdev)
51 return;
53 /* Already connected? */
54 if (blkif->be->dev->state == XenbusStateConnected)
55 return;
57 /* Attempt to connect: exit if we fail to. */
58 connect(blkif->be);
59 if (blkif->be->dev->state != XenbusStateConnected)
60 return;
62 blkif->xenblkd = kthread_run(blkif_schedule, blkif,
63 "xvd %d %02x:%02x",
64 blkif->domid,
65 blkif->be->major, blkif->be->minor);
66 if (IS_ERR(blkif->xenblkd)) {
67 err = PTR_ERR(blkif->xenblkd);
68 blkif->xenblkd = NULL;
69 xenbus_dev_error(blkif->be->dev, err, "start xenblkd");
70 }
71 }
74 /****************************************************************
75 * sysfs interface for VBD I/O requests
76 */
78 #define VBD_SHOW(name, format, args...) \
79 static ssize_t show_##name(struct device *_dev, \
80 struct device_attribute *attr, \
81 char *buf) \
82 { \
83 struct xenbus_device *dev = to_xenbus_device(_dev); \
84 struct backend_info *be = dev->dev.driver_data; \
85 \
86 return sprintf(buf, format, ##args); \
87 } \
88 DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
90 VBD_SHOW(oo_req, "%d\n", be->blkif->st_oo_req);
91 VBD_SHOW(rd_req, "%d\n", be->blkif->st_rd_req);
92 VBD_SHOW(wr_req, "%d\n", be->blkif->st_wr_req);
93 VBD_SHOW(br_req, "%d\n", be->blkif->st_br_req);
95 static struct attribute *vbdstat_attrs[] = {
96 &dev_attr_oo_req.attr,
97 &dev_attr_rd_req.attr,
98 &dev_attr_wr_req.attr,
99 &dev_attr_br_req.attr,
100 NULL
101 };
103 static struct attribute_group vbdstat_group = {
104 .name = "statistics",
105 .attrs = vbdstat_attrs,
106 };
108 VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
109 VBD_SHOW(mode, "%s\n", be->mode);
111 int xenvbd_sysfs_addif(struct xenbus_device *dev)
112 {
113 int error;
115 error = device_create_file(&dev->dev, &dev_attr_physical_device);
116 if (error)
117 goto fail1;
119 error = device_create_file(&dev->dev, &dev_attr_mode);
120 if (error)
121 goto fail2;
123 error = sysfs_create_group(&dev->dev.kobj, &vbdstat_group);
124 if (error)
125 goto fail3;
127 return 0;
129 fail3: sysfs_remove_group(&dev->dev.kobj, &vbdstat_group);
130 fail2: device_remove_file(&dev->dev, &dev_attr_mode);
131 fail1: device_remove_file(&dev->dev, &dev_attr_physical_device);
132 return error;
133 }
135 void xenvbd_sysfs_delif(struct xenbus_device *dev)
136 {
137 sysfs_remove_group(&dev->dev.kobj, &vbdstat_group);
138 device_remove_file(&dev->dev, &dev_attr_mode);
139 device_remove_file(&dev->dev, &dev_attr_physical_device);
140 }
142 static int blkback_remove(struct xenbus_device *dev)
143 {
144 struct backend_info *be = dev->dev.driver_data;
146 DPRINTK("");
148 if (be->backend_watch.node) {
149 unregister_xenbus_watch(&be->backend_watch);
150 kfree(be->backend_watch.node);
151 be->backend_watch.node = NULL;
152 }
154 if (be->blkif) {
155 blkif_disconnect(be->blkif);
156 vbd_free(&be->blkif->vbd);
157 blkif_free(be->blkif);
158 be->blkif = NULL;
159 }
161 if (be->major || be->minor)
162 xenvbd_sysfs_delif(dev);
164 kfree(be);
165 dev->dev.driver_data = NULL;
166 return 0;
167 }
169 int blkback_barrier(struct xenbus_transaction xbt,
170 struct backend_info *be, int state)
171 {
172 struct xenbus_device *dev = be->dev;
173 int err;
175 err = xenbus_printf(xbt, dev->nodename, "feature-barrier",
176 "%d", state);
177 if (err)
178 xenbus_dev_fatal(dev, err, "writing feature-barrier");
180 return err;
181 }
183 /**
184 * Entry point to this code when a new device is created. Allocate the basic
185 * structures, and watch the store waiting for the hotplug scripts to tell us
186 * the device's physical major and minor numbers. Switch to InitWait.
187 */
188 static int blkback_probe(struct xenbus_device *dev,
189 const struct xenbus_device_id *id)
190 {
191 int err;
192 struct backend_info *be = kzalloc(sizeof(struct backend_info),
193 GFP_KERNEL);
194 if (!be) {
195 xenbus_dev_fatal(dev, -ENOMEM,
196 "allocating backend structure");
197 return -ENOMEM;
198 }
199 be->dev = dev;
200 dev->dev.driver_data = be;
202 be->blkif = blkif_alloc(dev->otherend_id);
203 if (IS_ERR(be->blkif)) {
204 err = PTR_ERR(be->blkif);
205 be->blkif = NULL;
206 xenbus_dev_fatal(dev, err, "creating block interface");
207 goto fail;
208 }
210 /* setup back pointer */
211 be->blkif->be = be;
213 err = xenbus_watch_path2(dev, dev->nodename, "physical-device",
214 &be->backend_watch, backend_changed);
215 if (err)
216 goto fail;
218 err = xenbus_switch_state(dev, XenbusStateInitWait);
219 if (err)
220 goto fail;
222 return 0;
224 fail:
225 DPRINTK("failed");
226 blkback_remove(dev);
227 return err;
228 }
231 /**
232 * Callback received when the hotplug scripts have placed the physical-device
233 * node. Read it and the mode node, and create a vbd. If the frontend is
234 * ready, connect.
235 */
236 static void backend_changed(struct xenbus_watch *watch,
237 const char **vec, unsigned int len)
238 {
239 int err;
240 unsigned major;
241 unsigned minor;
242 struct backend_info *be
243 = container_of(watch, struct backend_info, backend_watch);
244 struct xenbus_device *dev = be->dev;
246 DPRINTK("");
248 err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
249 &major, &minor);
250 if (XENBUS_EXIST_ERR(err)) {
251 /* Since this watch will fire once immediately after it is
252 registered, we expect this. Ignore it, and wait for the
253 hotplug scripts. */
254 return;
255 }
256 if (err != 2) {
257 xenbus_dev_fatal(dev, err, "reading physical-device");
258 return;
259 }
261 if ((be->major || be->minor) &&
262 ((be->major != major) || (be->minor != minor))) {
263 printk(KERN_WARNING
264 "blkback: changing physical device (from %x:%x to "
265 "%x:%x) not supported.\n", be->major, be->minor,
266 major, minor);
267 return;
268 }
270 be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
271 if (IS_ERR(be->mode)) {
272 err = PTR_ERR(be->mode);
273 be->mode = NULL;
274 xenbus_dev_fatal(dev, err, "reading mode");
275 return;
276 }
278 if (be->major == 0 && be->minor == 0) {
279 /* Front end dir is a number, which is used as the handle. */
281 char *p = strrchr(dev->otherend, '/') + 1;
282 long handle = simple_strtoul(p, NULL, 0);
284 be->major = major;
285 be->minor = minor;
287 err = vbd_create(be->blkif, handle, major, minor,
288 (NULL == strchr(be->mode, 'w')));
289 if (err) {
290 be->major = be->minor = 0;
291 xenbus_dev_fatal(dev, err, "creating vbd structure");
292 return;
293 }
295 err = xenvbd_sysfs_addif(dev);
296 if (err) {
297 vbd_free(&be->blkif->vbd);
298 be->major = be->minor = 0;
299 xenbus_dev_fatal(dev, err, "creating sysfs entries");
300 return;
301 }
303 /* We're potentially connected now */
304 update_blkif_status(be->blkif);
305 }
306 }
309 /**
310 * Callback received when the frontend's state changes.
311 */
312 static void frontend_changed(struct xenbus_device *dev,
313 enum xenbus_state frontend_state)
314 {
315 struct backend_info *be = dev->dev.driver_data;
316 int err;
318 DPRINTK("%s", xenbus_strstate(frontend_state));
320 switch (frontend_state) {
321 case XenbusStateInitialising:
322 if (dev->state == XenbusStateClosed) {
323 printk("%s: %s: prepare for reconnect\n",
324 __FUNCTION__, dev->nodename);
325 xenbus_switch_state(dev, XenbusStateInitWait);
326 }
327 break;
329 case XenbusStateInitialised:
330 case XenbusStateConnected:
331 /* Ensure we connect even when two watches fire in
332 close successsion and we miss the intermediate value
333 of frontend_state. */
334 if (dev->state == XenbusStateConnected)
335 break;
337 err = connect_ring(be);
338 if (err)
339 break;
340 update_blkif_status(be->blkif);
341 break;
343 case XenbusStateClosing:
344 blkif_disconnect(be->blkif);
345 xenbus_switch_state(dev, XenbusStateClosing);
346 break;
348 case XenbusStateClosed:
349 xenbus_switch_state(dev, XenbusStateClosed);
350 if (xenbus_dev_is_online(dev))
351 break;
352 /* fall through if not online */
353 case XenbusStateUnknown:
354 device_unregister(&dev->dev);
355 break;
357 default:
358 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
359 frontend_state);
360 break;
361 }
362 }
365 /* ** Connection ** */
368 /**
369 * Write the physical details regarding the block device to the store, and
370 * switch to Connected state.
371 */
372 static void connect(struct backend_info *be)
373 {
374 struct xenbus_transaction xbt;
375 int err;
376 struct xenbus_device *dev = be->dev;
378 DPRINTK("%s", dev->otherend);
380 /* Supply the information about the device the frontend needs */
381 again:
382 err = xenbus_transaction_start(&xbt);
383 if (err) {
384 xenbus_dev_fatal(dev, err, "starting transaction");
385 return;
386 }
388 err = blkback_barrier(xbt, be, 1);
389 if (err)
390 goto abort;
392 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
393 vbd_size(&be->blkif->vbd));
394 if (err) {
395 xenbus_dev_fatal(dev, err, "writing %s/sectors",
396 dev->nodename);
397 goto abort;
398 }
400 /* FIXME: use a typename instead */
401 err = xenbus_printf(xbt, dev->nodename, "info", "%u",
402 vbd_info(&be->blkif->vbd));
403 if (err) {
404 xenbus_dev_fatal(dev, err, "writing %s/info",
405 dev->nodename);
406 goto abort;
407 }
408 err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
409 vbd_secsize(&be->blkif->vbd));
410 if (err) {
411 xenbus_dev_fatal(dev, err, "writing %s/sector-size",
412 dev->nodename);
413 goto abort;
414 }
416 err = xenbus_transaction_end(xbt, 0);
417 if (err == -EAGAIN)
418 goto again;
419 if (err)
420 xenbus_dev_fatal(dev, err, "ending transaction");
422 err = xenbus_switch_state(dev, XenbusStateConnected);
423 if (err)
424 xenbus_dev_fatal(dev, err, "switching to Connected state",
425 dev->nodename);
427 return;
428 abort:
429 xenbus_transaction_end(xbt, 1);
430 }
433 static int connect_ring(struct backend_info *be)
434 {
435 struct xenbus_device *dev = be->dev;
436 unsigned long ring_ref;
437 unsigned int evtchn;
438 int err;
440 DPRINTK("%s", dev->otherend);
442 err = xenbus_gather(XBT_NIL, dev->otherend, "ring-ref", "%lu", &ring_ref,
443 "event-channel", "%u", &evtchn, NULL);
444 if (err) {
445 xenbus_dev_fatal(dev, err,
446 "reading %s/ring-ref and event-channel",
447 dev->otherend);
448 return err;
449 }
451 /* Map the shared frame, irq etc. */
452 err = blkif_map(be->blkif, ring_ref, evtchn);
453 if (err) {
454 xenbus_dev_fatal(dev, err, "mapping ring-ref %lu port %u",
455 ring_ref, evtchn);
456 return err;
457 }
459 return 0;
460 }
463 /* ** Driver Registration ** */
466 static struct xenbus_device_id blkback_ids[] = {
467 { "vbd" },
468 { "" }
469 };
472 static struct xenbus_driver blkback = {
473 .name = "vbd",
474 .owner = THIS_MODULE,
475 .ids = blkback_ids,
476 .probe = blkback_probe,
477 .remove = blkback_remove,
478 .otherend_changed = frontend_changed
479 };
482 void blkif_xenbus_init(void)
483 {
484 xenbus_register_backend(&blkback);
485 }