xenbus.c 23 KB

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