xenbus.c 19 KB

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