xenbus.c 20 KB

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