xenbus.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  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("xen-blkback: (%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 *xen_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 *xen_blkbk_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 xen_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(xen_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. static struct blkif_st *xen_blkif_alloc(domid_t domid)
  89. {
  90. struct blkif_st *blkif;
  91. blkif = kmem_cache_alloc(xen_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. static int xen_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(blkif->domid, evtchn,
  167. xen_blkif_be_int, 0,
  168. "blkif-backend", blkif);
  169. if (err < 0) {
  170. unmap_frontend_page(blkif);
  171. free_vm_area(blkif->blk_ring_area);
  172. blkif->blk_rings.common.sring = NULL;
  173. return err;
  174. }
  175. blkif->irq = err;
  176. return 0;
  177. }
  178. static void xen_blkif_disconnect(struct blkif_st *blkif)
  179. {
  180. if (blkif->xenblkd) {
  181. kthread_stop(blkif->xenblkd);
  182. blkif->xenblkd = NULL;
  183. }
  184. atomic_dec(&blkif->refcnt);
  185. wait_event(blkif->waiting_to_free, atomic_read(&blkif->refcnt) == 0);
  186. atomic_inc(&blkif->refcnt);
  187. if (blkif->irq) {
  188. unbind_from_irqhandler(blkif->irq, blkif);
  189. blkif->irq = 0;
  190. }
  191. if (blkif->blk_rings.common.sring) {
  192. unmap_frontend_page(blkif);
  193. free_vm_area(blkif->blk_ring_area);
  194. blkif->blk_rings.common.sring = NULL;
  195. }
  196. }
  197. void xen_blkif_free(struct blkif_st *blkif)
  198. {
  199. if (!atomic_dec_and_test(&blkif->refcnt))
  200. BUG();
  201. kmem_cache_free(xen_blkif_cachep, blkif);
  202. }
  203. int __init xen_blkif_interface_init(void)
  204. {
  205. xen_blkif_cachep = kmem_cache_create("blkif_cache",
  206. sizeof(struct blkif_st),
  207. 0, 0, NULL);
  208. if (!xen_blkif_cachep)
  209. return -ENOMEM;
  210. return 0;
  211. }
  212. /*
  213. * sysfs interface for VBD I/O requests
  214. */
  215. #define VBD_SHOW(name, format, args...) \
  216. static ssize_t show_##name(struct device *_dev, \
  217. struct device_attribute *attr, \
  218. char *buf) \
  219. { \
  220. struct xenbus_device *dev = to_xenbus_device(_dev); \
  221. struct backend_info *be = dev_get_drvdata(&dev->dev); \
  222. \
  223. return sprintf(buf, format, ##args); \
  224. } \
  225. static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
  226. VBD_SHOW(oo_req, "%d\n", be->blkif->st_oo_req);
  227. VBD_SHOW(rd_req, "%d\n", be->blkif->st_rd_req);
  228. VBD_SHOW(wr_req, "%d\n", be->blkif->st_wr_req);
  229. VBD_SHOW(f_req, "%d\n", be->blkif->st_f_req);
  230. VBD_SHOW(rd_sect, "%d\n", be->blkif->st_rd_sect);
  231. VBD_SHOW(wr_sect, "%d\n", be->blkif->st_wr_sect);
  232. static struct attribute *vbdstat_attrs[] = {
  233. &dev_attr_oo_req.attr,
  234. &dev_attr_rd_req.attr,
  235. &dev_attr_wr_req.attr,
  236. &dev_attr_f_req.attr,
  237. &dev_attr_rd_sect.attr,
  238. &dev_attr_wr_sect.attr,
  239. NULL
  240. };
  241. static struct attribute_group vbdstat_group = {
  242. .name = "statistics",
  243. .attrs = vbdstat_attrs,
  244. };
  245. VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
  246. VBD_SHOW(mode, "%s\n", be->mode);
  247. int xenvbd_sysfs_addif(struct xenbus_device *dev)
  248. {
  249. int error;
  250. error = device_create_file(&dev->dev, &dev_attr_physical_device);
  251. if (error)
  252. goto fail1;
  253. error = device_create_file(&dev->dev, &dev_attr_mode);
  254. if (error)
  255. goto fail2;
  256. error = sysfs_create_group(&dev->dev.kobj, &vbdstat_group);
  257. if (error)
  258. goto fail3;
  259. return 0;
  260. fail3: sysfs_remove_group(&dev->dev.kobj, &vbdstat_group);
  261. fail2: device_remove_file(&dev->dev, &dev_attr_mode);
  262. fail1: device_remove_file(&dev->dev, &dev_attr_physical_device);
  263. return error;
  264. }
  265. void xenvbd_sysfs_delif(struct xenbus_device *dev)
  266. {
  267. sysfs_remove_group(&dev->dev.kobj, &vbdstat_group);
  268. device_remove_file(&dev->dev, &dev_attr_mode);
  269. device_remove_file(&dev->dev, &dev_attr_physical_device);
  270. }
  271. static void vbd_free(struct vbd *vbd)
  272. {
  273. if (vbd->bdev)
  274. blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE);
  275. vbd->bdev = NULL;
  276. }
  277. static int vbd_create(struct blkif_st *blkif, blkif_vdev_t handle,
  278. unsigned major, unsigned minor, int readonly,
  279. int cdrom)
  280. {
  281. struct vbd *vbd;
  282. struct block_device *bdev;
  283. struct request_queue *q;
  284. vbd = &blkif->vbd;
  285. vbd->handle = handle;
  286. vbd->readonly = readonly;
  287. vbd->type = 0;
  288. vbd->pdevice = MKDEV(major, minor);
  289. bdev = blkdev_get_by_dev(vbd->pdevice, vbd->readonly ?
  290. FMODE_READ : FMODE_WRITE, NULL);
  291. if (IS_ERR(bdev)) {
  292. DPRINTK("vbd_creat: device %08x could not be opened.\n",
  293. vbd->pdevice);
  294. return -ENOENT;
  295. }
  296. vbd->bdev = bdev;
  297. vbd->size = vbd_sz(vbd);
  298. if (vbd->bdev->bd_disk == NULL) {
  299. DPRINTK("vbd_creat: device %08x doesn't exist.\n",
  300. vbd->pdevice);
  301. vbd_free(vbd);
  302. return -ENOENT;
  303. }
  304. if (vbd->bdev->bd_disk->flags & GENHD_FL_CD || cdrom)
  305. vbd->type |= VDISK_CDROM;
  306. if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE)
  307. vbd->type |= VDISK_REMOVABLE;
  308. q = bdev_get_queue(bdev);
  309. if (q && q->flush_flags)
  310. vbd->flush_support = true;
  311. DPRINTK("Successful creation of handle=%04x (dom=%u)\n",
  312. handle, blkif->domid);
  313. return 0;
  314. }
  315. static int xen_blkbk_remove(struct xenbus_device *dev)
  316. {
  317. struct backend_info *be = dev_get_drvdata(&dev->dev);
  318. DPRINTK("");
  319. if (be->major || be->minor)
  320. xenvbd_sysfs_delif(dev);
  321. if (be->backend_watch.node) {
  322. unregister_xenbus_watch(&be->backend_watch);
  323. kfree(be->backend_watch.node);
  324. be->backend_watch.node = NULL;
  325. }
  326. if (be->blkif) {
  327. xen_blkif_disconnect(be->blkif);
  328. vbd_free(&be->blkif->vbd);
  329. xen_blkif_free(be->blkif);
  330. be->blkif = NULL;
  331. }
  332. kfree(be);
  333. dev_set_drvdata(&dev->dev, NULL);
  334. return 0;
  335. }
  336. int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
  337. struct backend_info *be, int state)
  338. {
  339. struct xenbus_device *dev = be->dev;
  340. int err;
  341. err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache",
  342. "%d", state);
  343. if (err)
  344. xenbus_dev_fatal(dev, err, "writing feature-flush-cache");
  345. return err;
  346. }
  347. /*
  348. * Entry point to this code when a new device is created. Allocate the basic
  349. * structures, and watch the store waiting for the hotplug scripts to tell us
  350. * the device's physical major and minor numbers. Switch to InitWait.
  351. */
  352. static int xen_blkbk_probe(struct xenbus_device *dev,
  353. const struct xenbus_device_id *id)
  354. {
  355. int err;
  356. struct backend_info *be = kzalloc(sizeof(struct backend_info),
  357. GFP_KERNEL);
  358. if (!be) {
  359. xenbus_dev_fatal(dev, -ENOMEM,
  360. "allocating backend structure");
  361. return -ENOMEM;
  362. }
  363. be->dev = dev;
  364. dev_set_drvdata(&dev->dev, be);
  365. be->blkif = xen_blkif_alloc(dev->otherend_id);
  366. if (IS_ERR(be->blkif)) {
  367. err = PTR_ERR(be->blkif);
  368. be->blkif = NULL;
  369. xenbus_dev_fatal(dev, err, "creating block interface");
  370. goto fail;
  371. }
  372. /* setup back pointer */
  373. be->blkif->be = be;
  374. err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed,
  375. "%s/%s", dev->nodename, "physical-device");
  376. if (err)
  377. goto fail;
  378. err = xenbus_switch_state(dev, XenbusStateInitWait);
  379. if (err)
  380. goto fail;
  381. return 0;
  382. fail:
  383. DPRINTK("failed");
  384. xen_blkbk_remove(dev);
  385. return err;
  386. }
  387. /*
  388. * Callback received when the hotplug scripts have placed the physical-device
  389. * node. Read it and the mode node, and create a vbd. If the frontend is
  390. * ready, connect.
  391. */
  392. static void backend_changed(struct xenbus_watch *watch,
  393. const char **vec, unsigned int len)
  394. {
  395. int err;
  396. unsigned major;
  397. unsigned minor;
  398. struct backend_info *be
  399. = container_of(watch, struct backend_info, backend_watch);
  400. struct xenbus_device *dev = be->dev;
  401. int cdrom = 0;
  402. char *device_type;
  403. DPRINTK("");
  404. err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
  405. &major, &minor);
  406. if (XENBUS_EXIST_ERR(err)) {
  407. /*
  408. * Since this watch will fire once immediately after it is
  409. * registered, we expect this. Ignore it, and wait for the
  410. * hotplug scripts.
  411. */
  412. return;
  413. }
  414. if (err != 2) {
  415. xenbus_dev_fatal(dev, err, "reading physical-device");
  416. return;
  417. }
  418. if ((be->major || be->minor) &&
  419. ((be->major != major) || (be->minor != minor))) {
  420. pr_warn("xen-blkback: changing physical device (from %x:%x to %x:%x) not supported.\n",
  421. be->major, be->minor, major, minor);
  422. return;
  423. }
  424. be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
  425. if (IS_ERR(be->mode)) {
  426. err = PTR_ERR(be->mode);
  427. be->mode = NULL;
  428. xenbus_dev_fatal(dev, err, "reading mode");
  429. return;
  430. }
  431. device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL);
  432. if (!IS_ERR(device_type)) {
  433. cdrom = strcmp(device_type, "cdrom") == 0;
  434. kfree(device_type);
  435. }
  436. if (be->major == 0 && be->minor == 0) {
  437. /* Front end dir is a number, which is used as the handle. */
  438. char *p = strrchr(dev->otherend, '/') + 1;
  439. long handle;
  440. err = strict_strtoul(p, 0, &handle);
  441. if (err)
  442. return;
  443. be->major = major;
  444. be->minor = minor;
  445. err = vbd_create(be->blkif, handle, major, minor,
  446. (NULL == strchr(be->mode, 'w')), cdrom);
  447. if (err) {
  448. be->major = be->minor = 0;
  449. xenbus_dev_fatal(dev, err, "creating vbd structure");
  450. return;
  451. }
  452. err = xenvbd_sysfs_addif(dev);
  453. if (err) {
  454. vbd_free(&be->blkif->vbd);
  455. be->major = be->minor = 0;
  456. xenbus_dev_fatal(dev, err, "creating sysfs entries");
  457. return;
  458. }
  459. /* We're potentially connected now */
  460. xen_update_blkif_status(be->blkif);
  461. }
  462. }
  463. /*
  464. * Callback received when the frontend's state changes.
  465. */
  466. static void frontend_changed(struct xenbus_device *dev,
  467. enum xenbus_state frontend_state)
  468. {
  469. struct backend_info *be = dev_get_drvdata(&dev->dev);
  470. int err;
  471. DPRINTK("%s", xenbus_strstate(frontend_state));
  472. switch (frontend_state) {
  473. case XenbusStateInitialising:
  474. if (dev->state == XenbusStateClosed) {
  475. pr_info("xen-blkback: %s: prepare for reconnect\n",
  476. dev->nodename);
  477. xenbus_switch_state(dev, XenbusStateInitWait);
  478. }
  479. break;
  480. case XenbusStateInitialised:
  481. case XenbusStateConnected:
  482. /*
  483. * Ensure we connect even when two watches fire in
  484. * close successsion and we miss the intermediate value
  485. * of frontend_state.
  486. */
  487. if (dev->state == XenbusStateConnected)
  488. break;
  489. /*
  490. * Enforce precondition before potential leak point.
  491. * blkif_disconnect() is idempotent.
  492. */
  493. xen_blkif_disconnect(be->blkif);
  494. err = connect_ring(be);
  495. if (err)
  496. break;
  497. xen_update_blkif_status(be->blkif);
  498. break;
  499. case XenbusStateClosing:
  500. xen_blkif_disconnect(be->blkif);
  501. xenbus_switch_state(dev, XenbusStateClosing);
  502. break;
  503. case XenbusStateClosed:
  504. xenbus_switch_state(dev, XenbusStateClosed);
  505. if (xenbus_dev_is_online(dev))
  506. break;
  507. /* fall through if not online */
  508. case XenbusStateUnknown:
  509. /* implies blkif_disconnect() via blkback_remove() */
  510. device_unregister(&dev->dev);
  511. break;
  512. default:
  513. xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
  514. frontend_state);
  515. break;
  516. }
  517. }
  518. /* ** Connection ** */
  519. /*
  520. * Write the physical details regarding the block device to the store, and
  521. * switch to Connected state.
  522. */
  523. static void connect(struct backend_info *be)
  524. {
  525. struct xenbus_transaction xbt;
  526. int err;
  527. struct xenbus_device *dev = be->dev;
  528. DPRINTK("%s", dev->otherend);
  529. /* Supply the information about the device the frontend needs */
  530. again:
  531. err = xenbus_transaction_start(&xbt);
  532. if (err) {
  533. xenbus_dev_fatal(dev, err, "starting transaction");
  534. return;
  535. }
  536. err = xen_blkbk_flush_diskcache(xbt, be, be->blkif->vbd.flush_support);
  537. if (err)
  538. goto abort;
  539. err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
  540. (unsigned long long)vbd_sz(&be->blkif->vbd));
  541. if (err) {
  542. xenbus_dev_fatal(dev, err, "writing %s/sectors",
  543. dev->nodename);
  544. goto abort;
  545. }
  546. /* FIXME: use a typename instead */
  547. err = xenbus_printf(xbt, dev->nodename, "info", "%u",
  548. be->blkif->vbd.type |
  549. (be->blkif->vbd.readonly ? VDISK_READONLY : 0));
  550. if (err) {
  551. xenbus_dev_fatal(dev, err, "writing %s/info",
  552. dev->nodename);
  553. goto abort;
  554. }
  555. err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
  556. (unsigned long)
  557. bdev_logical_block_size(be->blkif->vbd.bdev));
  558. if (err) {
  559. xenbus_dev_fatal(dev, err, "writing %s/sector-size",
  560. dev->nodename);
  561. goto abort;
  562. }
  563. err = xenbus_transaction_end(xbt, 0);
  564. if (err == -EAGAIN)
  565. goto again;
  566. if (err)
  567. xenbus_dev_fatal(dev, err, "ending transaction");
  568. err = xenbus_switch_state(dev, XenbusStateConnected);
  569. if (err)
  570. xenbus_dev_fatal(dev, err, "switching to Connected state",
  571. dev->nodename);
  572. return;
  573. abort:
  574. xenbus_transaction_end(xbt, 1);
  575. }
  576. static int connect_ring(struct backend_info *be)
  577. {
  578. struct xenbus_device *dev = be->dev;
  579. unsigned long ring_ref;
  580. unsigned int evtchn;
  581. char protocol[64] = "";
  582. int err;
  583. DPRINTK("%s", dev->otherend);
  584. err = xenbus_gather(XBT_NIL, dev->otherend, "ring-ref", "%lu",
  585. &ring_ref, "event-channel", "%u", &evtchn, NULL);
  586. if (err) {
  587. xenbus_dev_fatal(dev, err,
  588. "reading %s/ring-ref and event-channel",
  589. dev->otherend);
  590. return err;
  591. }
  592. be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
  593. err = xenbus_gather(XBT_NIL, dev->otherend, "protocol",
  594. "%63s", protocol, NULL);
  595. if (err)
  596. strcpy(protocol, "unspecified, assuming native");
  597. else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE))
  598. be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
  599. else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32))
  600. be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
  601. else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64))
  602. be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64;
  603. else {
  604. xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
  605. return -1;
  606. }
  607. pr_info("xen-blkback: ring-ref %ld, event-channel %d, protocol %d (%s)\n",
  608. ring_ref, evtchn, be->blkif->blk_protocol, protocol);
  609. /* Map the shared frame, irq etc. */
  610. err = xen_blkif_map(be->blkif, ring_ref, evtchn);
  611. if (err) {
  612. xenbus_dev_fatal(dev, err, "mapping ring-ref %lu port %u",
  613. ring_ref, evtchn);
  614. return err;
  615. }
  616. return 0;
  617. }
  618. /* ** Driver Registration ** */
  619. static const struct xenbus_device_id xen_blkbk_ids[] = {
  620. { "vbd" },
  621. { "" }
  622. };
  623. static struct xenbus_driver xen_blkbk = {
  624. .name = "vbd",
  625. .owner = THIS_MODULE,
  626. .ids = xen_blkbk_ids,
  627. .probe = xen_blkbk_probe,
  628. .remove = xen_blkbk_remove,
  629. .otherend_changed = frontend_changed
  630. };
  631. int xen_blkif_xenbus_init(void)
  632. {
  633. return xenbus_register_backend(&xen_blkbk);
  634. }