xenbus.c 19 KB

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