xenbus.c 21 KB

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