xenbus.c 13 KB

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