xenbus.c 22 KB

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