xenbus.c 21 KB

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