xenbus.c 19 KB

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