xenbus.c 20 KB

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