xenbus.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #include <stdarg.h>
  17. #include <linux/module.h>
  18. #include <linux/kthread.h>
  19. #include "common.h"
  20. #undef DPRINTK
  21. #define DPRINTK(fmt, args...) \
  22. pr_debug("blkback/xenbus (%s:%d) " fmt ".\n", \
  23. __FUNCTION__, __LINE__, ##args)
  24. struct backend_info
  25. {
  26. struct xenbus_device *dev;
  27. blkif_t *blkif;
  28. struct xenbus_watch backend_watch;
  29. unsigned major;
  30. unsigned minor;
  31. char *mode;
  32. };
  33. static void connect(struct backend_info *);
  34. static int connect_ring(struct backend_info *);
  35. static void backend_changed(struct xenbus_watch *, const char **,
  36. unsigned int);
  37. struct xenbus_device *blkback_xenbus(struct backend_info *be)
  38. {
  39. return be->dev;
  40. }
  41. static int blkback_name(blkif_t *blkif, char *buf)
  42. {
  43. char *devpath, *devname;
  44. struct xenbus_device *dev = blkif->be->dev;
  45. devpath = xenbus_read(XBT_NIL, dev->nodename, "dev", NULL);
  46. if (IS_ERR(devpath))
  47. return PTR_ERR(devpath);
  48. if ((devname = strstr(devpath, "/dev/")) != NULL)
  49. devname += strlen("/dev/");
  50. else
  51. devname = devpath;
  52. snprintf(buf, TASK_COMM_LEN, "blkback.%d.%s", blkif->domid, devname);
  53. kfree(devpath);
  54. return 0;
  55. }
  56. static void update_blkif_status(blkif_t *blkif)
  57. {
  58. int err;
  59. char name[TASK_COMM_LEN];
  60. /* Not ready to connect? */
  61. if (!blkif->irq || !blkif->vbd.bdev)
  62. return;
  63. /* Already connected? */
  64. if (blkif->be->dev->state == XenbusStateConnected)
  65. return;
  66. /* Attempt to connect: exit if we fail to. */
  67. connect(blkif->be);
  68. if (blkif->be->dev->state != XenbusStateConnected)
  69. return;
  70. err = blkback_name(blkif, name);
  71. if (err) {
  72. xenbus_dev_error(blkif->be->dev, err, "get blkback dev name");
  73. return;
  74. }
  75. err = filemap_write_and_wait(blkif->vbd.bdev->bd_inode->i_mapping);
  76. if (err) {
  77. xenbus_dev_error(blkif->be->dev, err, "block flush");
  78. return;
  79. }
  80. invalidate_inode_pages2(blkif->vbd.bdev->bd_inode->i_mapping);
  81. blkif->xenblkd = kthread_run(blkif_schedule, blkif, name);
  82. if (IS_ERR(blkif->xenblkd)) {
  83. err = PTR_ERR(blkif->xenblkd);
  84. blkif->xenblkd = NULL;
  85. xenbus_dev_error(blkif->be->dev, err, "start xenblkd");
  86. }
  87. }
  88. /*
  89. * sysfs interface for VBD I/O requests
  90. */
  91. #define VBD_SHOW(name, format, args...) \
  92. static ssize_t show_##name(struct device *_dev, \
  93. struct device_attribute *attr, \
  94. char *buf) \
  95. { \
  96. struct xenbus_device *dev = to_xenbus_device(_dev); \
  97. struct backend_info *be = dev_get_drvdata(&dev->dev); \
  98. \
  99. return sprintf(buf, format, ##args); \
  100. } \
  101. static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
  102. VBD_SHOW(oo_req, "%d\n", be->blkif->st_oo_req);
  103. VBD_SHOW(rd_req, "%d\n", be->blkif->st_rd_req);
  104. VBD_SHOW(wr_req, "%d\n", be->blkif->st_wr_req);
  105. VBD_SHOW(br_req, "%d\n", be->blkif->st_br_req);
  106. VBD_SHOW(rd_sect, "%d\n", be->blkif->st_rd_sect);
  107. VBD_SHOW(wr_sect, "%d\n", be->blkif->st_wr_sect);
  108. static struct attribute *vbdstat_attrs[] = {
  109. &dev_attr_oo_req.attr,
  110. &dev_attr_rd_req.attr,
  111. &dev_attr_wr_req.attr,
  112. &dev_attr_br_req.attr,
  113. &dev_attr_rd_sect.attr,
  114. &dev_attr_wr_sect.attr,
  115. NULL
  116. };
  117. static struct attribute_group vbdstat_group = {
  118. .name = "statistics",
  119. .attrs = vbdstat_attrs,
  120. };
  121. VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
  122. VBD_SHOW(mode, "%s\n", be->mode);
  123. int xenvbd_sysfs_addif(struct xenbus_device *dev)
  124. {
  125. int error;
  126. error = device_create_file(&dev->dev, &dev_attr_physical_device);
  127. if (error)
  128. goto fail1;
  129. error = device_create_file(&dev->dev, &dev_attr_mode);
  130. if (error)
  131. goto fail2;
  132. error = sysfs_create_group(&dev->dev.kobj, &vbdstat_group);
  133. if (error)
  134. goto fail3;
  135. return 0;
  136. fail3: sysfs_remove_group(&dev->dev.kobj, &vbdstat_group);
  137. fail2: device_remove_file(&dev->dev, &dev_attr_mode);
  138. fail1: device_remove_file(&dev->dev, &dev_attr_physical_device);
  139. return error;
  140. }
  141. void xenvbd_sysfs_delif(struct xenbus_device *dev)
  142. {
  143. sysfs_remove_group(&dev->dev.kobj, &vbdstat_group);
  144. device_remove_file(&dev->dev, &dev_attr_mode);
  145. device_remove_file(&dev->dev, &dev_attr_physical_device);
  146. }
  147. static int blkback_remove(struct xenbus_device *dev)
  148. {
  149. struct backend_info *be = dev_get_drvdata(&dev->dev);
  150. DPRINTK("");
  151. if (be->major || be->minor)
  152. xenvbd_sysfs_delif(dev);
  153. if (be->backend_watch.node) {
  154. unregister_xenbus_watch(&be->backend_watch);
  155. kfree(be->backend_watch.node);
  156. be->backend_watch.node = NULL;
  157. }
  158. if (be->blkif) {
  159. blkif_disconnect(be->blkif);
  160. vbd_free(&be->blkif->vbd);
  161. blkif_free(be->blkif);
  162. be->blkif = NULL;
  163. }
  164. kfree(be);
  165. dev_set_drvdata(&dev->dev, NULL);
  166. return 0;
  167. }
  168. int blkback_barrier(struct xenbus_transaction xbt,
  169. struct backend_info *be, int state)
  170. {
  171. struct xenbus_device *dev = be->dev;
  172. int err;
  173. err = xenbus_printf(xbt, dev->nodename, "feature-barrier",
  174. "%d", state);
  175. if (err)
  176. xenbus_dev_fatal(dev, err, "writing feature-barrier");
  177. return err;
  178. }
  179. /**
  180. * Entry point to this code when a new device is created. Allocate the basic
  181. * structures, and watch the store waiting for the hotplug scripts to tell us
  182. * the device's physical major and minor numbers. Switch to InitWait.
  183. */
  184. static int blkback_probe(struct xenbus_device *dev,
  185. const struct xenbus_device_id *id)
  186. {
  187. int err;
  188. struct backend_info *be = kzalloc(sizeof(struct backend_info),
  189. GFP_KERNEL);
  190. if (!be) {
  191. xenbus_dev_fatal(dev, -ENOMEM,
  192. "allocating backend structure");
  193. return -ENOMEM;
  194. }
  195. be->dev = dev;
  196. dev_set_drvdata(&dev->dev, be);
  197. be->blkif = blkif_alloc(dev->otherend_id);
  198. if (IS_ERR(be->blkif)) {
  199. err = PTR_ERR(be->blkif);
  200. be->blkif = NULL;
  201. xenbus_dev_fatal(dev, err, "creating block interface");
  202. goto fail;
  203. }
  204. /* setup back pointer */
  205. be->blkif->be = be;
  206. err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed,
  207. "%s/%s", dev->nodename, "physical-device");
  208. if (err)
  209. goto fail;
  210. err = xenbus_switch_state(dev, XenbusStateInitWait);
  211. if (err)
  212. goto fail;
  213. return 0;
  214. fail:
  215. DPRINTK("failed");
  216. blkback_remove(dev);
  217. return err;
  218. }
  219. /**
  220. * Callback received when the hotplug scripts have placed the physical-device
  221. * node. Read it and the mode node, and create a vbd. If the frontend is
  222. * ready, connect.
  223. */
  224. static void backend_changed(struct xenbus_watch *watch,
  225. const char **vec, unsigned int len)
  226. {
  227. int err;
  228. unsigned major;
  229. unsigned minor;
  230. struct backend_info *be
  231. = container_of(watch, struct backend_info, backend_watch);
  232. struct xenbus_device *dev = be->dev;
  233. int cdrom = 0;
  234. char *device_type;
  235. DPRINTK("");
  236. err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
  237. &major, &minor);
  238. if (XENBUS_EXIST_ERR(err)) {
  239. /* Since this watch will fire once immediately after it is
  240. registered, we expect this. Ignore it, and wait for the
  241. hotplug scripts. */
  242. return;
  243. }
  244. if (err != 2) {
  245. xenbus_dev_fatal(dev, err, "reading physical-device");
  246. return;
  247. }
  248. if ((be->major || be->minor) &&
  249. ((be->major != major) || (be->minor != minor))) {
  250. printk(KERN_WARNING
  251. "blkback: changing physical device (from %x:%x to "
  252. "%x:%x) not supported.\n", be->major, be->minor,
  253. major, minor);
  254. return;
  255. }
  256. be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
  257. if (IS_ERR(be->mode)) {
  258. err = PTR_ERR(be->mode);
  259. be->mode = NULL;
  260. xenbus_dev_fatal(dev, err, "reading mode");
  261. return;
  262. }
  263. device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL);
  264. if (!IS_ERR(device_type)) {
  265. cdrom = strcmp(device_type, "cdrom") == 0;
  266. kfree(device_type);
  267. }
  268. if (be->major == 0 && be->minor == 0) {
  269. /* Front end dir is a number, which is used as the handle. */
  270. char *p = strrchr(dev->otherend, '/') + 1;
  271. long handle = simple_strtoul(p, NULL, 0);
  272. be->major = major;
  273. be->minor = minor;
  274. err = vbd_create(be->blkif, handle, major, minor,
  275. (NULL == strchr(be->mode, 'w')), cdrom);
  276. if (err) {
  277. be->major = be->minor = 0;
  278. xenbus_dev_fatal(dev, err, "creating vbd structure");
  279. return;
  280. }
  281. err = xenvbd_sysfs_addif(dev);
  282. if (err) {
  283. vbd_free(&be->blkif->vbd);
  284. be->major = be->minor = 0;
  285. xenbus_dev_fatal(dev, err, "creating sysfs entries");
  286. return;
  287. }
  288. /* We're potentially connected now */
  289. update_blkif_status(be->blkif);
  290. }
  291. }
  292. /**
  293. * Callback received when the frontend's state changes.
  294. */
  295. static void frontend_changed(struct xenbus_device *dev,
  296. enum xenbus_state frontend_state)
  297. {
  298. struct backend_info *be = dev_get_drvdata(&dev->dev);
  299. int err;
  300. DPRINTK("%s", xenbus_strstate(frontend_state));
  301. switch (frontend_state) {
  302. case XenbusStateInitialising:
  303. if (dev->state == XenbusStateClosed) {
  304. printk(KERN_INFO "%s: %s: prepare for reconnect\n",
  305. __FUNCTION__, dev->nodename);
  306. xenbus_switch_state(dev, XenbusStateInitWait);
  307. }
  308. break;
  309. case XenbusStateInitialised:
  310. case XenbusStateConnected:
  311. /* Ensure we connect even when two watches fire in
  312. close successsion and we miss the intermediate value
  313. of frontend_state. */
  314. if (dev->state == XenbusStateConnected)
  315. break;
  316. /* Enforce precondition before potential leak point.
  317. * blkif_disconnect() is idempotent.
  318. */
  319. blkif_disconnect(be->blkif);
  320. err = connect_ring(be);
  321. if (err)
  322. break;
  323. update_blkif_status(be->blkif);
  324. break;
  325. case XenbusStateClosing:
  326. blkif_disconnect(be->blkif);
  327. xenbus_switch_state(dev, XenbusStateClosing);
  328. break;
  329. case XenbusStateClosed:
  330. xenbus_switch_state(dev, XenbusStateClosed);
  331. if (xenbus_dev_is_online(dev))
  332. break;
  333. /* fall through if not online */
  334. case XenbusStateUnknown:
  335. /* implies blkif_disconnect() via blkback_remove() */
  336. device_unregister(&dev->dev);
  337. break;
  338. default:
  339. xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
  340. frontend_state);
  341. break;
  342. }
  343. }
  344. /* ** Connection ** */
  345. /**
  346. * Write the physical details regarding the block device to the store, and
  347. * switch to Connected state.
  348. */
  349. static void connect(struct backend_info *be)
  350. {
  351. struct xenbus_transaction xbt;
  352. int err;
  353. struct xenbus_device *dev = be->dev;
  354. DPRINTK("%s", dev->otherend);
  355. /* Supply the information about the device the frontend needs */
  356. again:
  357. err = xenbus_transaction_start(&xbt);
  358. if (err) {
  359. xenbus_dev_fatal(dev, err, "starting transaction");
  360. return;
  361. }
  362. err = blkback_barrier(xbt, be, 1);
  363. if (err)
  364. goto abort;
  365. err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
  366. vbd_size(&be->blkif->vbd));
  367. if (err) {
  368. xenbus_dev_fatal(dev, err, "writing %s/sectors",
  369. dev->nodename);
  370. goto abort;
  371. }
  372. /* FIXME: use a typename instead */
  373. err = xenbus_printf(xbt, dev->nodename, "info", "%u",
  374. vbd_info(&be->blkif->vbd));
  375. if (err) {
  376. xenbus_dev_fatal(dev, err, "writing %s/info",
  377. dev->nodename);
  378. goto abort;
  379. }
  380. err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
  381. vbd_secsize(&be->blkif->vbd));
  382. if (err) {
  383. xenbus_dev_fatal(dev, err, "writing %s/sector-size",
  384. dev->nodename);
  385. goto abort;
  386. }
  387. err = xenbus_transaction_end(xbt, 0);
  388. if (err == -EAGAIN)
  389. goto again;
  390. if (err)
  391. xenbus_dev_fatal(dev, err, "ending transaction");
  392. err = xenbus_switch_state(dev, XenbusStateConnected);
  393. if (err)
  394. xenbus_dev_fatal(dev, err, "switching to Connected state",
  395. dev->nodename);
  396. return;
  397. abort:
  398. xenbus_transaction_end(xbt, 1);
  399. }
  400. static int connect_ring(struct backend_info *be)
  401. {
  402. struct xenbus_device *dev = be->dev;
  403. unsigned long ring_ref;
  404. unsigned int evtchn;
  405. char protocol[64] = "";
  406. int err;
  407. DPRINTK("%s", dev->otherend);
  408. err = xenbus_gather(XBT_NIL, dev->otherend, "ring-ref", "%lu", &ring_ref,
  409. "event-channel", "%u", &evtchn, NULL);
  410. if (err) {
  411. xenbus_dev_fatal(dev, err,
  412. "reading %s/ring-ref and event-channel",
  413. dev->otherend);
  414. return err;
  415. }
  416. be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
  417. err = xenbus_gather(XBT_NIL, dev->otherend, "protocol",
  418. "%63s", protocol, NULL);
  419. if (err)
  420. strcpy(protocol, "unspecified, assuming native");
  421. else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE))
  422. be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
  423. else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32))
  424. be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
  425. else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64))
  426. be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64;
  427. else {
  428. xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
  429. return -1;
  430. }
  431. printk(KERN_INFO
  432. "blkback: ring-ref %ld, event-channel %d, protocol %d (%s)\n",
  433. ring_ref, evtchn, be->blkif->blk_protocol, protocol);
  434. /* Map the shared frame, irq etc. */
  435. err = blkif_map(be->blkif, ring_ref, evtchn);
  436. if (err) {
  437. xenbus_dev_fatal(dev, err, "mapping ring-ref %lu port %u",
  438. ring_ref, evtchn);
  439. return err;
  440. }
  441. return 0;
  442. }
  443. /* ** Driver Registration ** */
  444. static const struct xenbus_device_id blkback_ids[] = {
  445. { "vbd" },
  446. { "" }
  447. };
  448. static struct xenbus_driver blkback = {
  449. .name = "vbd",
  450. .owner = THIS_MODULE,
  451. .ids = blkback_ids,
  452. .probe = blkback_probe,
  453. .remove = blkback_remove,
  454. .otherend_changed = frontend_changed
  455. };
  456. int blkif_xenbus_init(void)
  457. {
  458. return xenbus_register_backend(&blkback);
  459. }