xenbus.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. /* Xenbus code for blkif backend
  2. Copyright (C) 2005 Rusty Russell <rusty@rustcorp.com.au>
  3. Copyright (C) 2005 XenSource Ltd
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. */
  13. #include <stdarg.h>
  14. #include <linux/module.h>
  15. #include <linux/kthread.h>
  16. #include <xen/events.h>
  17. #include <xen/grant_table.h>
  18. #include "common.h"
  19. #undef DPRINTK
  20. #define DPRINTK(fmt, args...) \
  21. pr_debug("blkback/xenbus (%s:%d) " fmt ".\n", \
  22. __func__, __LINE__, ##args)
  23. struct backend_info {
  24. struct xenbus_device *dev;
  25. struct blkif_st *blkif;
  26. struct xenbus_watch backend_watch;
  27. unsigned major;
  28. unsigned minor;
  29. char *mode;
  30. };
  31. static struct kmem_cache *blkif_cachep;
  32. static void connect(struct backend_info *);
  33. static int connect_ring(struct backend_info *);
  34. static void backend_changed(struct xenbus_watch *, const char **,
  35. unsigned int);
  36. struct xenbus_device *blkback_xenbus(struct backend_info *be)
  37. {
  38. return be->dev;
  39. }
  40. static int blkback_name(struct blkif_st *blkif, char *buf)
  41. {
  42. char *devpath, *devname;
  43. struct xenbus_device *dev = blkif->be->dev;
  44. devpath = xenbus_read(XBT_NIL, dev->nodename, "dev", NULL);
  45. if (IS_ERR(devpath))
  46. return PTR_ERR(devpath);
  47. devname = strstr(devpath, "/dev/");
  48. if (devname != NULL)
  49. devname += strlen("/dev/");
  50. else
  51. devname = devpath;
  52. snprintf(buf, TASK_COMM_LEN, "blkback.%d.%s", blkif->domid, devname);
  53. kfree(devpath);
  54. return 0;
  55. }
  56. static void update_blkif_status(struct blkif_st *blkif)
  57. {
  58. int err;
  59. char name[TASK_COMM_LEN];
  60. /* Not ready to connect? */
  61. if (!blkif->irq || !blkif->vbd.bdev)
  62. return;
  63. /* Already connected? */
  64. if (blkif->be->dev->state == XenbusStateConnected)
  65. return;
  66. /* Attempt to connect: exit if we fail to. */
  67. connect(blkif->be);
  68. if (blkif->be->dev->state != XenbusStateConnected)
  69. return;
  70. err = blkback_name(blkif, name);
  71. if (err) {
  72. xenbus_dev_error(blkif->be->dev, err, "get blkback dev name");
  73. return;
  74. }
  75. err = filemap_write_and_wait(blkif->vbd.bdev->bd_inode->i_mapping);
  76. if (err) {
  77. xenbus_dev_error(blkif->be->dev, err, "block flush");
  78. return;
  79. }
  80. invalidate_inode_pages2(blkif->vbd.bdev->bd_inode->i_mapping);
  81. blkif->xenblkd = kthread_run(blkif_schedule, blkif, name);
  82. if (IS_ERR(blkif->xenblkd)) {
  83. err = PTR_ERR(blkif->xenblkd);
  84. blkif->xenblkd = NULL;
  85. xenbus_dev_error(blkif->be->dev, err, "start xenblkd");
  86. }
  87. }
  88. struct blkif_st *blkif_alloc(domid_t domid)
  89. {
  90. struct blkif_st *blkif;
  91. blkif = kmem_cache_alloc(blkif_cachep, GFP_KERNEL);
  92. if (!blkif)
  93. return ERR_PTR(-ENOMEM);
  94. memset(blkif, 0, sizeof(*blkif));
  95. blkif->domid = domid;
  96. spin_lock_init(&blkif->blk_ring_lock);
  97. atomic_set(&blkif->refcnt, 1);
  98. init_waitqueue_head(&blkif->wq);
  99. blkif->st_print = jiffies;
  100. init_waitqueue_head(&blkif->waiting_to_free);
  101. return blkif;
  102. }
  103. static int map_frontend_page(struct blkif_st *blkif, unsigned long shared_page)
  104. {
  105. struct gnttab_map_grant_ref op;
  106. gnttab_set_map_op(&op, (unsigned long)blkif->blk_ring_area->addr,
  107. GNTMAP_host_map, shared_page, blkif->domid);
  108. if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1))
  109. BUG();
  110. if (op.status) {
  111. DPRINTK(" Grant table operation failure !\n");
  112. return op.status;
  113. }
  114. blkif->shmem_ref = shared_page;
  115. blkif->shmem_handle = op.handle;
  116. return 0;
  117. }
  118. static void unmap_frontend_page(struct blkif_st *blkif)
  119. {
  120. struct gnttab_unmap_grant_ref op;
  121. gnttab_set_unmap_op(&op, (unsigned long)blkif->blk_ring_area->addr,
  122. GNTMAP_host_map, blkif->shmem_handle);
  123. if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1))
  124. BUG();
  125. }
  126. int blkif_map(struct blkif_st *blkif, unsigned long shared_page,
  127. unsigned int evtchn)
  128. {
  129. int err;
  130. /* Already connected through? */
  131. if (blkif->irq)
  132. return 0;
  133. blkif->blk_ring_area = alloc_vm_area(PAGE_SIZE);
  134. if (!blkif->blk_ring_area)
  135. return -ENOMEM;
  136. err = map_frontend_page(blkif, shared_page);
  137. if (err) {
  138. free_vm_area(blkif->blk_ring_area);
  139. return err;
  140. }
  141. switch (blkif->blk_protocol) {
  142. case BLKIF_PROTOCOL_NATIVE:
  143. {
  144. struct blkif_sring *sring;
  145. sring = (struct blkif_sring *)blkif->blk_ring_area->addr;
  146. BACK_RING_INIT(&blkif->blk_rings.native, sring, PAGE_SIZE);
  147. break;
  148. }
  149. case BLKIF_PROTOCOL_X86_32:
  150. {
  151. struct blkif_x86_32_sring *sring_x86_32;
  152. sring_x86_32 = (struct blkif_x86_32_sring *)blkif->blk_ring_area->addr;
  153. BACK_RING_INIT(&blkif->blk_rings.x86_32, sring_x86_32, PAGE_SIZE);
  154. break;
  155. }
  156. case BLKIF_PROTOCOL_X86_64:
  157. {
  158. struct blkif_x86_64_sring *sring_x86_64;
  159. sring_x86_64 = (struct blkif_x86_64_sring *)blkif->blk_ring_area->addr;
  160. BACK_RING_INIT(&blkif->blk_rings.x86_64, sring_x86_64, PAGE_SIZE);
  161. break;
  162. }
  163. default:
  164. BUG();
  165. }
  166. err = bind_interdomain_evtchn_to_irqhandler(
  167. blkif->domid, evtchn, blkif_be_int, 0, "blkif-backend", blkif);
  168. if (err < 0) {
  169. unmap_frontend_page(blkif);
  170. free_vm_area(blkif->blk_ring_area);
  171. blkif->blk_rings.common.sring = NULL;
  172. return err;
  173. }
  174. blkif->irq = err;
  175. return 0;
  176. }
  177. void blkif_disconnect(struct blkif_st *blkif)
  178. {
  179. if (blkif->xenblkd) {
  180. kthread_stop(blkif->xenblkd);
  181. blkif->xenblkd = NULL;
  182. }
  183. atomic_dec(&blkif->refcnt);
  184. wait_event(blkif->waiting_to_free, atomic_read(&blkif->refcnt) == 0);
  185. atomic_inc(&blkif->refcnt);
  186. if (blkif->irq) {
  187. unbind_from_irqhandler(blkif->irq, blkif);
  188. blkif->irq = 0;
  189. }
  190. if (blkif->blk_rings.common.sring) {
  191. unmap_frontend_page(blkif);
  192. free_vm_area(blkif->blk_ring_area);
  193. blkif->blk_rings.common.sring = NULL;
  194. }
  195. }
  196. void blkif_free(struct blkif_st *blkif)
  197. {
  198. if (!atomic_dec_and_test(&blkif->refcnt))
  199. BUG();
  200. kmem_cache_free(blkif_cachep, blkif);
  201. }
  202. int __init blkif_interface_init(void)
  203. {
  204. blkif_cachep = kmem_cache_create("blkif_cache", sizeof(struct blkif_st),
  205. 0, 0, NULL);
  206. if (!blkif_cachep)
  207. return -ENOMEM;
  208. return 0;
  209. }
  210. /*
  211. * sysfs interface for VBD I/O requests
  212. */
  213. #define VBD_SHOW(name, format, args...) \
  214. static ssize_t show_##name(struct device *_dev, \
  215. struct device_attribute *attr, \
  216. char *buf) \
  217. { \
  218. struct xenbus_device *dev = to_xenbus_device(_dev); \
  219. struct backend_info *be = dev_get_drvdata(&dev->dev); \
  220. \
  221. return sprintf(buf, format, ##args); \
  222. } \
  223. static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
  224. VBD_SHOW(oo_req, "%d\n", be->blkif->st_oo_req);
  225. VBD_SHOW(rd_req, "%d\n", be->blkif->st_rd_req);
  226. VBD_SHOW(wr_req, "%d\n", be->blkif->st_wr_req);
  227. VBD_SHOW(br_req, "%d\n", be->blkif->st_br_req);
  228. VBD_SHOW(rd_sect, "%d\n", be->blkif->st_rd_sect);
  229. VBD_SHOW(wr_sect, "%d\n", be->blkif->st_wr_sect);
  230. static struct attribute *vbdstat_attrs[] = {
  231. &dev_attr_oo_req.attr,
  232. &dev_attr_rd_req.attr,
  233. &dev_attr_wr_req.attr,
  234. &dev_attr_br_req.attr,
  235. &dev_attr_rd_sect.attr,
  236. &dev_attr_wr_sect.attr,
  237. NULL
  238. };
  239. static struct attribute_group vbdstat_group = {
  240. .name = "statistics",
  241. .attrs = vbdstat_attrs,
  242. };
  243. VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
  244. VBD_SHOW(mode, "%s\n", be->mode);
  245. int xenvbd_sysfs_addif(struct xenbus_device *dev)
  246. {
  247. int error;
  248. error = device_create_file(&dev->dev, &dev_attr_physical_device);
  249. if (error)
  250. goto fail1;
  251. error = device_create_file(&dev->dev, &dev_attr_mode);
  252. if (error)
  253. goto fail2;
  254. error = sysfs_create_group(&dev->dev.kobj, &vbdstat_group);
  255. if (error)
  256. goto fail3;
  257. return 0;
  258. fail3: sysfs_remove_group(&dev->dev.kobj, &vbdstat_group);
  259. fail2: device_remove_file(&dev->dev, &dev_attr_mode);
  260. fail1: device_remove_file(&dev->dev, &dev_attr_physical_device);
  261. return error;
  262. }
  263. void xenvbd_sysfs_delif(struct xenbus_device *dev)
  264. {
  265. sysfs_remove_group(&dev->dev.kobj, &vbdstat_group);
  266. device_remove_file(&dev->dev, &dev_attr_mode);
  267. device_remove_file(&dev->dev, &dev_attr_physical_device);
  268. }
  269. static void vbd_free(struct vbd *vbd)
  270. {
  271. if (vbd->bdev)
  272. blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE);
  273. vbd->bdev = NULL;
  274. }
  275. static int vbd_create(struct blkif_st *blkif, blkif_vdev_t handle,
  276. unsigned major, unsigned minor, int readonly,
  277. int cdrom)
  278. {
  279. struct vbd *vbd;
  280. struct block_device *bdev;
  281. vbd = &blkif->vbd;
  282. vbd->handle = handle;
  283. vbd->readonly = readonly;
  284. vbd->type = 0;
  285. vbd->pdevice = MKDEV(major, minor);
  286. bdev = blkdev_get_by_dev(vbd->pdevice, vbd->readonly ?
  287. FMODE_READ : FMODE_WRITE, NULL);
  288. if (IS_ERR(bdev)) {
  289. DPRINTK("vbd_creat: device %08x could not be opened.\n",
  290. vbd->pdevice);
  291. return -ENOENT;
  292. }
  293. vbd->bdev = bdev;
  294. vbd->size = vbd_sz(vbd);
  295. if (vbd->bdev->bd_disk == NULL) {
  296. DPRINTK("vbd_creat: device %08x doesn't exist.\n",
  297. vbd->pdevice);
  298. vbd_free(vbd);
  299. return -ENOENT;
  300. }
  301. if (vbd->bdev->bd_disk->flags & GENHD_FL_CD || cdrom)
  302. vbd->type |= VDISK_CDROM;
  303. if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE)
  304. vbd->type |= VDISK_REMOVABLE;
  305. DPRINTK("Successful creation of handle=%04x (dom=%u)\n",
  306. handle, blkif->domid);
  307. return 0;
  308. }
  309. static int blkback_remove(struct xenbus_device *dev)
  310. {
  311. struct backend_info *be = dev_get_drvdata(&dev->dev);
  312. DPRINTK("");
  313. if (be->major || be->minor)
  314. xenvbd_sysfs_delif(dev);
  315. if (be->backend_watch.node) {
  316. unregister_xenbus_watch(&be->backend_watch);
  317. kfree(be->backend_watch.node);
  318. be->backend_watch.node = NULL;
  319. }
  320. if (be->blkif) {
  321. blkif_disconnect(be->blkif);
  322. vbd_free(&be->blkif->vbd);
  323. blkif_free(be->blkif);
  324. be->blkif = NULL;
  325. }
  326. kfree(be);
  327. dev_set_drvdata(&dev->dev, NULL);
  328. return 0;
  329. }
  330. int blkback_barrier(struct xenbus_transaction xbt,
  331. struct backend_info *be, int state)
  332. {
  333. struct xenbus_device *dev = be->dev;
  334. int err;
  335. err = xenbus_printf(xbt, dev->nodename, "feature-barrier",
  336. "%d", state);
  337. if (err)
  338. xenbus_dev_fatal(dev, err, "writing feature-barrier");
  339. return err;
  340. }
  341. /**
  342. * Entry point to this code when a new device is created. Allocate the basic
  343. * structures, and watch the store waiting for the hotplug scripts to tell us
  344. * the device's physical major and minor numbers. Switch to InitWait.
  345. */
  346. static int blkback_probe(struct xenbus_device *dev,
  347. const struct xenbus_device_id *id)
  348. {
  349. int err;
  350. struct backend_info *be = kzalloc(sizeof(struct backend_info),
  351. GFP_KERNEL);
  352. if (!be) {
  353. xenbus_dev_fatal(dev, -ENOMEM,
  354. "allocating backend structure");
  355. return -ENOMEM;
  356. }
  357. be->dev = dev;
  358. dev_set_drvdata(&dev->dev, be);
  359. be->blkif = blkif_alloc(dev->otherend_id);
  360. if (IS_ERR(be->blkif)) {
  361. err = PTR_ERR(be->blkif);
  362. be->blkif = NULL;
  363. xenbus_dev_fatal(dev, err, "creating block interface");
  364. goto fail;
  365. }
  366. /* setup back pointer */
  367. be->blkif->be = be;
  368. err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed,
  369. "%s/%s", dev->nodename, "physical-device");
  370. if (err)
  371. goto fail;
  372. err = xenbus_switch_state(dev, XenbusStateInitWait);
  373. if (err)
  374. goto fail;
  375. return 0;
  376. fail:
  377. DPRINTK("failed");
  378. blkback_remove(dev);
  379. return err;
  380. }
  381. /**
  382. * Callback received when the hotplug scripts have placed the physical-device
  383. * node. Read it and the mode node, and create a vbd. If the frontend is
  384. * ready, connect.
  385. */
  386. static void backend_changed(struct xenbus_watch *watch,
  387. const char **vec, unsigned int len)
  388. {
  389. int err;
  390. unsigned major;
  391. unsigned minor;
  392. struct backend_info *be
  393. = container_of(watch, struct backend_info, backend_watch);
  394. struct xenbus_device *dev = be->dev;
  395. int cdrom = 0;
  396. char *device_type;
  397. DPRINTK("");
  398. err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
  399. &major, &minor);
  400. if (XENBUS_EXIST_ERR(err)) {
  401. /* Since this watch will fire once immediately after it is
  402. registered, we expect this. Ignore it, and wait for the
  403. hotplug scripts. */
  404. return;
  405. }
  406. if (err != 2) {
  407. xenbus_dev_fatal(dev, err, "reading physical-device");
  408. return;
  409. }
  410. if ((be->major || be->minor) &&
  411. ((be->major != major) || (be->minor != minor))) {
  412. printk(KERN_WARNING
  413. "blkback: changing physical device (from %x:%x to "
  414. "%x:%x) not supported.\n", be->major, be->minor,
  415. major, minor);
  416. return;
  417. }
  418. be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
  419. if (IS_ERR(be->mode)) {
  420. err = PTR_ERR(be->mode);
  421. be->mode = NULL;
  422. xenbus_dev_fatal(dev, err, "reading mode");
  423. return;
  424. }
  425. device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL);
  426. if (!IS_ERR(device_type)) {
  427. cdrom = strcmp(device_type, "cdrom") == 0;
  428. kfree(device_type);
  429. }
  430. if (be->major == 0 && be->minor == 0) {
  431. /* Front end dir is a number, which is used as the handle. */
  432. char *p = strrchr(dev->otherend, '/') + 1;
  433. long handle;
  434. err = strict_strtoul(p, 0, &handle);
  435. if (err)
  436. return;
  437. be->major = major;
  438. be->minor = minor;
  439. err = vbd_create(be->blkif, handle, major, minor,
  440. (NULL == strchr(be->mode, 'w')), cdrom);
  441. if (err) {
  442. be->major = be->minor = 0;
  443. xenbus_dev_fatal(dev, err, "creating vbd structure");
  444. return;
  445. }
  446. err = xenvbd_sysfs_addif(dev);
  447. if (err) {
  448. vbd_free(&be->blkif->vbd);
  449. be->major = be->minor = 0;
  450. xenbus_dev_fatal(dev, err, "creating sysfs entries");
  451. return;
  452. }
  453. /* We're potentially connected now */
  454. update_blkif_status(be->blkif);
  455. }
  456. }
  457. /**
  458. * Callback received when the frontend's state changes.
  459. */
  460. static void frontend_changed(struct xenbus_device *dev,
  461. enum xenbus_state frontend_state)
  462. {
  463. struct backend_info *be = dev_get_drvdata(&dev->dev);
  464. int err;
  465. DPRINTK("%s", xenbus_strstate(frontend_state));
  466. switch (frontend_state) {
  467. case XenbusStateInitialising:
  468. if (dev->state == XenbusStateClosed) {
  469. printk(KERN_INFO "%s: %s: prepare for reconnect\n",
  470. __func__, dev->nodename);
  471. xenbus_switch_state(dev, XenbusStateInitWait);
  472. }
  473. break;
  474. case XenbusStateInitialised:
  475. case XenbusStateConnected:
  476. /* Ensure we connect even when two watches fire in
  477. close successsion and we miss the intermediate value
  478. of frontend_state. */
  479. if (dev->state == XenbusStateConnected)
  480. break;
  481. /* Enforce precondition before potential leak point.
  482. * blkif_disconnect() is idempotent.
  483. */
  484. blkif_disconnect(be->blkif);
  485. err = connect_ring(be);
  486. if (err)
  487. break;
  488. update_blkif_status(be->blkif);
  489. break;
  490. case XenbusStateClosing:
  491. blkif_disconnect(be->blkif);
  492. xenbus_switch_state(dev, XenbusStateClosing);
  493. break;
  494. case XenbusStateClosed:
  495. xenbus_switch_state(dev, XenbusStateClosed);
  496. if (xenbus_dev_is_online(dev))
  497. break;
  498. /* fall through if not online */
  499. case XenbusStateUnknown:
  500. /* implies blkif_disconnect() via blkback_remove() */
  501. device_unregister(&dev->dev);
  502. break;
  503. default:
  504. xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
  505. frontend_state);
  506. break;
  507. }
  508. }
  509. /* ** Connection ** */
  510. /**
  511. * Write the physical details regarding the block device to the store, and
  512. * switch to Connected state.
  513. */
  514. static void connect(struct backend_info *be)
  515. {
  516. struct xenbus_transaction xbt;
  517. int err;
  518. struct xenbus_device *dev = be->dev;
  519. DPRINTK("%s", dev->otherend);
  520. /* Supply the information about the device the frontend needs */
  521. again:
  522. err = xenbus_transaction_start(&xbt);
  523. if (err) {
  524. xenbus_dev_fatal(dev, err, "starting transaction");
  525. return;
  526. }
  527. err = blkback_barrier(xbt, be, 1);
  528. if (err)
  529. goto abort;
  530. err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
  531. (unsigned long long)vbd_sz(&be->blkif->vbd));
  532. if (err) {
  533. xenbus_dev_fatal(dev, err, "writing %s/sectors",
  534. dev->nodename);
  535. goto abort;
  536. }
  537. /* FIXME: use a typename instead */
  538. err = xenbus_printf(xbt, dev->nodename, "info", "%u",
  539. be->blkif->vbd.type |
  540. (be->blkif->vbd.readonly ? VDISK_READONLY : 0));
  541. if (err) {
  542. xenbus_dev_fatal(dev, err, "writing %s/info",
  543. dev->nodename);
  544. goto abort;
  545. }
  546. err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
  547. (unsigned long)
  548. bdev_logical_block_size(be->blkif->vbd.bdev));
  549. if (err) {
  550. xenbus_dev_fatal(dev, err, "writing %s/sector-size",
  551. dev->nodename);
  552. goto abort;
  553. }
  554. err = xenbus_transaction_end(xbt, 0);
  555. if (err == -EAGAIN)
  556. goto again;
  557. if (err)
  558. xenbus_dev_fatal(dev, err, "ending transaction");
  559. err = xenbus_switch_state(dev, XenbusStateConnected);
  560. if (err)
  561. xenbus_dev_fatal(dev, err, "switching to Connected state",
  562. dev->nodename);
  563. return;
  564. abort:
  565. xenbus_transaction_end(xbt, 1);
  566. }
  567. static int connect_ring(struct backend_info *be)
  568. {
  569. struct xenbus_device *dev = be->dev;
  570. unsigned long ring_ref;
  571. unsigned int evtchn;
  572. char protocol[64] = "";
  573. int err;
  574. DPRINTK("%s", dev->otherend);
  575. err = xenbus_gather(XBT_NIL, dev->otherend, "ring-ref", "%lu",
  576. &ring_ref, "event-channel", "%u", &evtchn, NULL);
  577. if (err) {
  578. xenbus_dev_fatal(dev, err,
  579. "reading %s/ring-ref and event-channel",
  580. dev->otherend);
  581. return err;
  582. }
  583. be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
  584. err = xenbus_gather(XBT_NIL, dev->otherend, "protocol",
  585. "%63s", protocol, NULL);
  586. if (err)
  587. strcpy(protocol, "unspecified, assuming native");
  588. else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE))
  589. be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
  590. else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32))
  591. be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
  592. else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64))
  593. be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64;
  594. else {
  595. xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
  596. return -1;
  597. }
  598. printk(KERN_INFO
  599. "blkback: ring-ref %ld, event-channel %d, protocol %d (%s)\n",
  600. ring_ref, evtchn, be->blkif->blk_protocol, protocol);
  601. /* Map the shared frame, irq etc. */
  602. err = blkif_map(be->blkif, ring_ref, evtchn);
  603. if (err) {
  604. xenbus_dev_fatal(dev, err, "mapping ring-ref %lu port %u",
  605. ring_ref, evtchn);
  606. return err;
  607. }
  608. return 0;
  609. }
  610. /* ** Driver Registration ** */
  611. static const struct xenbus_device_id blkback_ids[] = {
  612. { "vbd" },
  613. { "" }
  614. };
  615. static struct xenbus_driver blkback = {
  616. .name = "vbd",
  617. .owner = THIS_MODULE,
  618. .ids = blkback_ids,
  619. .probe = blkback_probe,
  620. .remove = blkback_remove,
  621. .otherend_changed = frontend_changed
  622. };
  623. int blkif_xenbus_init(void)
  624. {
  625. return xenbus_register_backend(&blkback);
  626. }