xenbus.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Xenbus code for blkif backend
  3. Copyright (C) 2005 Rusty Russell <[email protected]>
  4. Copyright (C) 2005 XenSource Ltd
  5. */
  6. #define pr_fmt(fmt) "xen-blkback: " fmt
  7. #include <linux/module.h>
  8. #include <linux/kthread.h>
  9. #include <linux/pagemap.h>
  10. #include <xen/events.h>
  11. #include <xen/grant_table.h>
  12. #include "common.h"
  13. /* On the XenBus the max length of 'ring-ref%u'. */
  14. #define RINGREF_NAME_LEN (20)
  15. struct backend_info {
  16. struct xenbus_device *dev;
  17. struct xen_blkif *blkif;
  18. struct xenbus_watch backend_watch;
  19. unsigned major;
  20. unsigned minor;
  21. char *mode;
  22. };
  23. static struct kmem_cache *xen_blkif_cachep;
  24. static void connect(struct backend_info *);
  25. static int connect_ring(struct backend_info *);
  26. static void backend_changed(struct xenbus_watch *, const char *,
  27. const char *);
  28. static void xen_blkif_free(struct xen_blkif *blkif);
  29. static void xen_vbd_free(struct xen_vbd *vbd);
  30. struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be)
  31. {
  32. return be->dev;
  33. }
  34. /*
  35. * The last request could free the device from softirq context and
  36. * xen_blkif_free() can sleep.
  37. */
  38. static void xen_blkif_deferred_free(struct work_struct *work)
  39. {
  40. struct xen_blkif *blkif;
  41. blkif = container_of(work, struct xen_blkif, free_work);
  42. xen_blkif_free(blkif);
  43. }
  44. static int blkback_name(struct xen_blkif *blkif, char *buf)
  45. {
  46. char *devpath, *devname;
  47. struct xenbus_device *dev = blkif->be->dev;
  48. devpath = xenbus_read(XBT_NIL, dev->nodename, "dev", NULL);
  49. if (IS_ERR(devpath))
  50. return PTR_ERR(devpath);
  51. devname = strstr(devpath, "/dev/");
  52. if (devname != NULL)
  53. devname += strlen("/dev/");
  54. else
  55. devname = devpath;
  56. snprintf(buf, TASK_COMM_LEN, "%d.%s", blkif->domid, devname);
  57. kfree(devpath);
  58. return 0;
  59. }
  60. static void xen_update_blkif_status(struct xen_blkif *blkif)
  61. {
  62. int err;
  63. char name[TASK_COMM_LEN];
  64. struct xen_blkif_ring *ring;
  65. int i;
  66. /* Not ready to connect? */
  67. if (!blkif->rings || !blkif->rings[0].irq || !blkif->vbd.bdev)
  68. return;
  69. /* Already connected? */
  70. if (blkif->be->dev->state == XenbusStateConnected)
  71. return;
  72. /* Attempt to connect: exit if we fail to. */
  73. connect(blkif->be);
  74. if (blkif->be->dev->state != XenbusStateConnected)
  75. return;
  76. err = blkback_name(blkif, name);
  77. if (err) {
  78. xenbus_dev_error(blkif->be->dev, err, "get blkback dev name");
  79. return;
  80. }
  81. err = sync_blockdev(blkif->vbd.bdev);
  82. if (err) {
  83. xenbus_dev_error(blkif->be->dev, err, "block flush");
  84. return;
  85. }
  86. invalidate_inode_pages2(blkif->vbd.bdev->bd_inode->i_mapping);
  87. for (i = 0; i < blkif->nr_rings; i++) {
  88. ring = &blkif->rings[i];
  89. ring->xenblkd = kthread_run(xen_blkif_schedule, ring, "%s-%d", name, i);
  90. if (IS_ERR(ring->xenblkd)) {
  91. err = PTR_ERR(ring->xenblkd);
  92. ring->xenblkd = NULL;
  93. xenbus_dev_fatal(blkif->be->dev, err,
  94. "start %s-%d xenblkd", name, i);
  95. goto out;
  96. }
  97. }
  98. return;
  99. out:
  100. while (--i >= 0) {
  101. ring = &blkif->rings[i];
  102. kthread_stop(ring->xenblkd);
  103. }
  104. return;
  105. }
  106. static int xen_blkif_alloc_rings(struct xen_blkif *blkif)
  107. {
  108. unsigned int r;
  109. blkif->rings = kcalloc(blkif->nr_rings, sizeof(struct xen_blkif_ring),
  110. GFP_KERNEL);
  111. if (!blkif->rings)
  112. return -ENOMEM;
  113. for (r = 0; r < blkif->nr_rings; r++) {
  114. struct xen_blkif_ring *ring = &blkif->rings[r];
  115. spin_lock_init(&ring->blk_ring_lock);
  116. init_waitqueue_head(&ring->wq);
  117. INIT_LIST_HEAD(&ring->pending_free);
  118. INIT_LIST_HEAD(&ring->persistent_purge_list);
  119. INIT_WORK(&ring->persistent_purge_work, xen_blkbk_unmap_purged_grants);
  120. gnttab_page_cache_init(&ring->free_pages);
  121. spin_lock_init(&ring->pending_free_lock);
  122. init_waitqueue_head(&ring->pending_free_wq);
  123. init_waitqueue_head(&ring->shutdown_wq);
  124. ring->blkif = blkif;
  125. ring->st_print = jiffies;
  126. ring->active = true;
  127. }
  128. return 0;
  129. }
  130. /* Enable the persistent grants feature. */
  131. static bool feature_persistent = true;
  132. module_param(feature_persistent, bool, 0644);
  133. MODULE_PARM_DESC(feature_persistent, "Enables the persistent grants feature");
  134. static struct xen_blkif *xen_blkif_alloc(domid_t domid)
  135. {
  136. struct xen_blkif *blkif;
  137. BUILD_BUG_ON(MAX_INDIRECT_PAGES > BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST);
  138. blkif = kmem_cache_zalloc(xen_blkif_cachep, GFP_KERNEL);
  139. if (!blkif)
  140. return ERR_PTR(-ENOMEM);
  141. blkif->domid = domid;
  142. atomic_set(&blkif->refcnt, 1);
  143. init_completion(&blkif->drain_complete);
  144. /*
  145. * Because freeing back to the cache may be deferred, it is not
  146. * safe to unload the module (and hence destroy the cache) until
  147. * this has completed. To prevent premature unloading, take an
  148. * extra module reference here and release only when the object
  149. * has been freed back to the cache.
  150. */
  151. __module_get(THIS_MODULE);
  152. INIT_WORK(&blkif->free_work, xen_blkif_deferred_free);
  153. return blkif;
  154. }
  155. static int xen_blkif_map(struct xen_blkif_ring *ring, grant_ref_t *gref,
  156. unsigned int nr_grefs, unsigned int evtchn)
  157. {
  158. int err;
  159. struct xen_blkif *blkif = ring->blkif;
  160. const struct blkif_common_sring *sring_common;
  161. RING_IDX rsp_prod, req_prod;
  162. unsigned int size;
  163. /* Already connected through? */
  164. if (ring->irq)
  165. return 0;
  166. err = xenbus_map_ring_valloc(blkif->be->dev, gref, nr_grefs,
  167. &ring->blk_ring);
  168. if (err < 0)
  169. return err;
  170. sring_common = (struct blkif_common_sring *)ring->blk_ring;
  171. rsp_prod = READ_ONCE(sring_common->rsp_prod);
  172. req_prod = READ_ONCE(sring_common->req_prod);
  173. switch (blkif->blk_protocol) {
  174. case BLKIF_PROTOCOL_NATIVE:
  175. {
  176. struct blkif_sring *sring_native =
  177. (struct blkif_sring *)ring->blk_ring;
  178. BACK_RING_ATTACH(&ring->blk_rings.native, sring_native,
  179. rsp_prod, XEN_PAGE_SIZE * nr_grefs);
  180. size = __RING_SIZE(sring_native, XEN_PAGE_SIZE * nr_grefs);
  181. break;
  182. }
  183. case BLKIF_PROTOCOL_X86_32:
  184. {
  185. struct blkif_x86_32_sring *sring_x86_32 =
  186. (struct blkif_x86_32_sring *)ring->blk_ring;
  187. BACK_RING_ATTACH(&ring->blk_rings.x86_32, sring_x86_32,
  188. rsp_prod, XEN_PAGE_SIZE * nr_grefs);
  189. size = __RING_SIZE(sring_x86_32, XEN_PAGE_SIZE * nr_grefs);
  190. break;
  191. }
  192. case BLKIF_PROTOCOL_X86_64:
  193. {
  194. struct blkif_x86_64_sring *sring_x86_64 =
  195. (struct blkif_x86_64_sring *)ring->blk_ring;
  196. BACK_RING_ATTACH(&ring->blk_rings.x86_64, sring_x86_64,
  197. rsp_prod, XEN_PAGE_SIZE * nr_grefs);
  198. size = __RING_SIZE(sring_x86_64, XEN_PAGE_SIZE * nr_grefs);
  199. break;
  200. }
  201. default:
  202. BUG();
  203. }
  204. err = -EIO;
  205. if (req_prod - rsp_prod > size)
  206. goto fail;
  207. err = bind_interdomain_evtchn_to_irqhandler_lateeoi(blkif->be->dev,
  208. evtchn, xen_blkif_be_int, 0, "blkif-backend", ring);
  209. if (err < 0)
  210. goto fail;
  211. ring->irq = err;
  212. return 0;
  213. fail:
  214. xenbus_unmap_ring_vfree(blkif->be->dev, ring->blk_ring);
  215. ring->blk_rings.common.sring = NULL;
  216. return err;
  217. }
  218. static int xen_blkif_disconnect(struct xen_blkif *blkif)
  219. {
  220. struct pending_req *req, *n;
  221. unsigned int j, r;
  222. bool busy = false;
  223. for (r = 0; r < blkif->nr_rings; r++) {
  224. struct xen_blkif_ring *ring = &blkif->rings[r];
  225. unsigned int i = 0;
  226. if (!ring->active)
  227. continue;
  228. if (ring->xenblkd) {
  229. kthread_stop(ring->xenblkd);
  230. ring->xenblkd = NULL;
  231. wake_up(&ring->shutdown_wq);
  232. }
  233. /* The above kthread_stop() guarantees that at this point we
  234. * don't have any discard_io or other_io requests. So, checking
  235. * for inflight IO is enough.
  236. */
  237. if (atomic_read(&ring->inflight) > 0) {
  238. busy = true;
  239. continue;
  240. }
  241. if (ring->irq) {
  242. unbind_from_irqhandler(ring->irq, ring);
  243. ring->irq = 0;
  244. }
  245. if (ring->blk_rings.common.sring) {
  246. xenbus_unmap_ring_vfree(blkif->be->dev, ring->blk_ring);
  247. ring->blk_rings.common.sring = NULL;
  248. }
  249. /* Remove all persistent grants and the cache of ballooned pages. */
  250. xen_blkbk_free_caches(ring);
  251. /* Check that there is no request in use */
  252. list_for_each_entry_safe(req, n, &ring->pending_free, free_list) {
  253. list_del(&req->free_list);
  254. for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++)
  255. kfree(req->segments[j]);
  256. for (j = 0; j < MAX_INDIRECT_PAGES; j++)
  257. kfree(req->indirect_pages[j]);
  258. kfree(req);
  259. i++;
  260. }
  261. BUG_ON(atomic_read(&ring->persistent_gnt_in_use) != 0);
  262. BUG_ON(!list_empty(&ring->persistent_purge_list));
  263. BUG_ON(!RB_EMPTY_ROOT(&ring->persistent_gnts));
  264. BUG_ON(ring->free_pages.num_pages != 0);
  265. BUG_ON(ring->persistent_gnt_c != 0);
  266. WARN_ON(i != (XEN_BLKIF_REQS_PER_PAGE * blkif->nr_ring_pages));
  267. ring->active = false;
  268. }
  269. if (busy)
  270. return -EBUSY;
  271. blkif->nr_ring_pages = 0;
  272. /*
  273. * blkif->rings was allocated in connect_ring, so we should free it in
  274. * here.
  275. */
  276. kfree(blkif->rings);
  277. blkif->rings = NULL;
  278. blkif->nr_rings = 0;
  279. return 0;
  280. }
  281. static void xen_blkif_free(struct xen_blkif *blkif)
  282. {
  283. WARN_ON(xen_blkif_disconnect(blkif));
  284. xen_vbd_free(&blkif->vbd);
  285. kfree(blkif->be->mode);
  286. kfree(blkif->be);
  287. /* Make sure everything is drained before shutting down */
  288. kmem_cache_free(xen_blkif_cachep, blkif);
  289. module_put(THIS_MODULE);
  290. }
  291. int __init xen_blkif_interface_init(void)
  292. {
  293. xen_blkif_cachep = kmem_cache_create("blkif_cache",
  294. sizeof(struct xen_blkif),
  295. 0, 0, NULL);
  296. if (!xen_blkif_cachep)
  297. return -ENOMEM;
  298. return 0;
  299. }
  300. void xen_blkif_interface_fini(void)
  301. {
  302. kmem_cache_destroy(xen_blkif_cachep);
  303. xen_blkif_cachep = NULL;
  304. }
  305. /*
  306. * sysfs interface for VBD I/O requests
  307. */
  308. #define VBD_SHOW_ALLRING(name, format) \
  309. static ssize_t show_##name(struct device *_dev, \
  310. struct device_attribute *attr, \
  311. char *buf) \
  312. { \
  313. struct xenbus_device *dev = to_xenbus_device(_dev); \
  314. struct backend_info *be = dev_get_drvdata(&dev->dev); \
  315. struct xen_blkif *blkif = be->blkif; \
  316. unsigned int i; \
  317. unsigned long long result = 0; \
  318. \
  319. if (!blkif->rings) \
  320. goto out; \
  321. \
  322. for (i = 0; i < blkif->nr_rings; i++) { \
  323. struct xen_blkif_ring *ring = &blkif->rings[i]; \
  324. \
  325. result += ring->st_##name; \
  326. } \
  327. \
  328. out: \
  329. return sprintf(buf, format, result); \
  330. } \
  331. static DEVICE_ATTR(name, 0444, show_##name, NULL)
  332. VBD_SHOW_ALLRING(oo_req, "%llu\n");
  333. VBD_SHOW_ALLRING(rd_req, "%llu\n");
  334. VBD_SHOW_ALLRING(wr_req, "%llu\n");
  335. VBD_SHOW_ALLRING(f_req, "%llu\n");
  336. VBD_SHOW_ALLRING(ds_req, "%llu\n");
  337. VBD_SHOW_ALLRING(rd_sect, "%llu\n");
  338. VBD_SHOW_ALLRING(wr_sect, "%llu\n");
  339. static struct attribute *xen_vbdstat_attrs[] = {
  340. &dev_attr_oo_req.attr,
  341. &dev_attr_rd_req.attr,
  342. &dev_attr_wr_req.attr,
  343. &dev_attr_f_req.attr,
  344. &dev_attr_ds_req.attr,
  345. &dev_attr_rd_sect.attr,
  346. &dev_attr_wr_sect.attr,
  347. NULL
  348. };
  349. static const struct attribute_group xen_vbdstat_group = {
  350. .name = "statistics",
  351. .attrs = xen_vbdstat_attrs,
  352. };
  353. #define VBD_SHOW(name, format, args...) \
  354. static ssize_t show_##name(struct device *_dev, \
  355. struct device_attribute *attr, \
  356. char *buf) \
  357. { \
  358. struct xenbus_device *dev = to_xenbus_device(_dev); \
  359. struct backend_info *be = dev_get_drvdata(&dev->dev); \
  360. \
  361. return sprintf(buf, format, ##args); \
  362. } \
  363. static DEVICE_ATTR(name, 0444, show_##name, NULL)
  364. VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
  365. VBD_SHOW(mode, "%s\n", be->mode);
  366. static int xenvbd_sysfs_addif(struct xenbus_device *dev)
  367. {
  368. int error;
  369. error = device_create_file(&dev->dev, &dev_attr_physical_device);
  370. if (error)
  371. goto fail1;
  372. error = device_create_file(&dev->dev, &dev_attr_mode);
  373. if (error)
  374. goto fail2;
  375. error = sysfs_create_group(&dev->dev.kobj, &xen_vbdstat_group);
  376. if (error)
  377. goto fail3;
  378. return 0;
  379. fail3: sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
  380. fail2: device_remove_file(&dev->dev, &dev_attr_mode);
  381. fail1: device_remove_file(&dev->dev, &dev_attr_physical_device);
  382. return error;
  383. }
  384. static void xenvbd_sysfs_delif(struct xenbus_device *dev)
  385. {
  386. sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
  387. device_remove_file(&dev->dev, &dev_attr_mode);
  388. device_remove_file(&dev->dev, &dev_attr_physical_device);
  389. }
  390. static void xen_vbd_free(struct xen_vbd *vbd)
  391. {
  392. if (vbd->bdev)
  393. blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE);
  394. vbd->bdev = NULL;
  395. }
  396. static int xen_vbd_create(struct xen_blkif *blkif, blkif_vdev_t handle,
  397. unsigned major, unsigned minor, int readonly,
  398. int cdrom)
  399. {
  400. struct xen_vbd *vbd;
  401. struct block_device *bdev;
  402. vbd = &blkif->vbd;
  403. vbd->handle = handle;
  404. vbd->readonly = readonly;
  405. vbd->type = 0;
  406. vbd->pdevice = MKDEV(major, minor);
  407. bdev = blkdev_get_by_dev(vbd->pdevice, vbd->readonly ?
  408. FMODE_READ : FMODE_WRITE, NULL);
  409. if (IS_ERR(bdev)) {
  410. pr_warn("xen_vbd_create: device %08x could not be opened\n",
  411. vbd->pdevice);
  412. return -ENOENT;
  413. }
  414. vbd->bdev = bdev;
  415. if (vbd->bdev->bd_disk == NULL) {
  416. pr_warn("xen_vbd_create: device %08x doesn't exist\n",
  417. vbd->pdevice);
  418. xen_vbd_free(vbd);
  419. return -ENOENT;
  420. }
  421. vbd->size = vbd_sz(vbd);
  422. if (cdrom || disk_to_cdi(vbd->bdev->bd_disk))
  423. vbd->type |= VDISK_CDROM;
  424. if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE)
  425. vbd->type |= VDISK_REMOVABLE;
  426. if (bdev_write_cache(bdev))
  427. vbd->flush_support = true;
  428. if (bdev_max_secure_erase_sectors(bdev))
  429. vbd->discard_secure = true;
  430. pr_debug("Successful creation of handle=%04x (dom=%u)\n",
  431. handle, blkif->domid);
  432. return 0;
  433. }
  434. static int xen_blkbk_remove(struct xenbus_device *dev)
  435. {
  436. struct backend_info *be = dev_get_drvdata(&dev->dev);
  437. pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
  438. if (be->major || be->minor)
  439. xenvbd_sysfs_delif(dev);
  440. if (be->backend_watch.node) {
  441. unregister_xenbus_watch(&be->backend_watch);
  442. kfree(be->backend_watch.node);
  443. be->backend_watch.node = NULL;
  444. }
  445. dev_set_drvdata(&dev->dev, NULL);
  446. if (be->blkif) {
  447. xen_blkif_disconnect(be->blkif);
  448. /* Put the reference we set in xen_blkif_alloc(). */
  449. xen_blkif_put(be->blkif);
  450. }
  451. return 0;
  452. }
  453. int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
  454. struct backend_info *be, int state)
  455. {
  456. struct xenbus_device *dev = be->dev;
  457. int err;
  458. err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache",
  459. "%d", state);
  460. if (err)
  461. dev_warn(&dev->dev, "writing feature-flush-cache (%d)", err);
  462. return err;
  463. }
  464. static void xen_blkbk_discard(struct xenbus_transaction xbt, struct backend_info *be)
  465. {
  466. struct xenbus_device *dev = be->dev;
  467. struct xen_blkif *blkif = be->blkif;
  468. int err;
  469. int state = 0;
  470. struct block_device *bdev = be->blkif->vbd.bdev;
  471. if (!xenbus_read_unsigned(dev->nodename, "discard-enable", 1))
  472. return;
  473. if (bdev_max_discard_sectors(bdev)) {
  474. err = xenbus_printf(xbt, dev->nodename,
  475. "discard-granularity", "%u",
  476. bdev_discard_granularity(bdev));
  477. if (err) {
  478. dev_warn(&dev->dev, "writing discard-granularity (%d)", err);
  479. return;
  480. }
  481. err = xenbus_printf(xbt, dev->nodename,
  482. "discard-alignment", "%u",
  483. bdev_discard_alignment(bdev));
  484. if (err) {
  485. dev_warn(&dev->dev, "writing discard-alignment (%d)", err);
  486. return;
  487. }
  488. state = 1;
  489. /* Optional. */
  490. err = xenbus_printf(xbt, dev->nodename,
  491. "discard-secure", "%d",
  492. blkif->vbd.discard_secure);
  493. if (err) {
  494. dev_warn(&dev->dev, "writing discard-secure (%d)", err);
  495. return;
  496. }
  497. }
  498. err = xenbus_printf(xbt, dev->nodename, "feature-discard",
  499. "%d", state);
  500. if (err)
  501. dev_warn(&dev->dev, "writing feature-discard (%d)", err);
  502. }
  503. int xen_blkbk_barrier(struct xenbus_transaction xbt,
  504. struct backend_info *be, int state)
  505. {
  506. struct xenbus_device *dev = be->dev;
  507. int err;
  508. err = xenbus_printf(xbt, dev->nodename, "feature-barrier",
  509. "%d", state);
  510. if (err)
  511. dev_warn(&dev->dev, "writing feature-barrier (%d)", err);
  512. return err;
  513. }
  514. /*
  515. * Entry point to this code when a new device is created. Allocate the basic
  516. * structures, and watch the store waiting for the hotplug scripts to tell us
  517. * the device's physical major and minor numbers. Switch to InitWait.
  518. */
  519. static int xen_blkbk_probe(struct xenbus_device *dev,
  520. const struct xenbus_device_id *id)
  521. {
  522. int err;
  523. struct backend_info *be = kzalloc(sizeof(struct backend_info),
  524. GFP_KERNEL);
  525. /* match the pr_debug in xen_blkbk_remove */
  526. pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
  527. if (!be) {
  528. xenbus_dev_fatal(dev, -ENOMEM,
  529. "allocating backend structure");
  530. return -ENOMEM;
  531. }
  532. be->dev = dev;
  533. dev_set_drvdata(&dev->dev, be);
  534. be->blkif = xen_blkif_alloc(dev->otherend_id);
  535. if (IS_ERR(be->blkif)) {
  536. err = PTR_ERR(be->blkif);
  537. be->blkif = NULL;
  538. xenbus_dev_fatal(dev, err, "creating block interface");
  539. goto fail;
  540. }
  541. err = xenbus_printf(XBT_NIL, dev->nodename,
  542. "feature-max-indirect-segments", "%u",
  543. MAX_INDIRECT_SEGMENTS);
  544. if (err)
  545. dev_warn(&dev->dev,
  546. "writing %s/feature-max-indirect-segments (%d)",
  547. dev->nodename, err);
  548. /* Multi-queue: advertise how many queues are supported by us.*/
  549. err = xenbus_printf(XBT_NIL, dev->nodename,
  550. "multi-queue-max-queues", "%u", xenblk_max_queues);
  551. if (err)
  552. pr_warn("Error writing multi-queue-max-queues\n");
  553. /* setup back pointer */
  554. be->blkif->be = be;
  555. err = xenbus_watch_pathfmt(dev, &be->backend_watch, NULL,
  556. backend_changed,
  557. "%s/%s", dev->nodename, "physical-device");
  558. if (err)
  559. goto fail;
  560. err = xenbus_printf(XBT_NIL, dev->nodename, "max-ring-page-order", "%u",
  561. xen_blkif_max_ring_order);
  562. if (err)
  563. pr_warn("%s write out 'max-ring-page-order' failed\n", __func__);
  564. err = xenbus_switch_state(dev, XenbusStateInitWait);
  565. if (err)
  566. goto fail;
  567. return 0;
  568. fail:
  569. pr_warn("%s failed\n", __func__);
  570. xen_blkbk_remove(dev);
  571. return err;
  572. }
  573. /*
  574. * Callback received when the hotplug scripts have placed the physical-device
  575. * node. Read it and the mode node, and create a vbd. If the frontend is
  576. * ready, connect.
  577. */
  578. static void backend_changed(struct xenbus_watch *watch,
  579. const char *path, const char *token)
  580. {
  581. int err;
  582. unsigned major;
  583. unsigned minor;
  584. struct backend_info *be
  585. = container_of(watch, struct backend_info, backend_watch);
  586. struct xenbus_device *dev = be->dev;
  587. int cdrom = 0;
  588. unsigned long handle;
  589. char *device_type;
  590. pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
  591. err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
  592. &major, &minor);
  593. if (XENBUS_EXIST_ERR(err)) {
  594. /*
  595. * Since this watch will fire once immediately after it is
  596. * registered, we expect this. Ignore it, and wait for the
  597. * hotplug scripts.
  598. */
  599. return;
  600. }
  601. if (err != 2) {
  602. xenbus_dev_fatal(dev, err, "reading physical-device");
  603. return;
  604. }
  605. if (be->major | be->minor) {
  606. if (be->major != major || be->minor != minor)
  607. pr_warn("changing physical device (from %x:%x to %x:%x) not supported.\n",
  608. be->major, be->minor, major, minor);
  609. return;
  610. }
  611. be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
  612. if (IS_ERR(be->mode)) {
  613. err = PTR_ERR(be->mode);
  614. be->mode = NULL;
  615. xenbus_dev_fatal(dev, err, "reading mode");
  616. return;
  617. }
  618. device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL);
  619. if (!IS_ERR(device_type)) {
  620. cdrom = strcmp(device_type, "cdrom") == 0;
  621. kfree(device_type);
  622. }
  623. /* Front end dir is a number, which is used as the handle. */
  624. err = kstrtoul(strrchr(dev->otherend, '/') + 1, 0, &handle);
  625. if (err) {
  626. kfree(be->mode);
  627. be->mode = NULL;
  628. return;
  629. }
  630. be->major = major;
  631. be->minor = minor;
  632. err = xen_vbd_create(be->blkif, handle, major, minor,
  633. !strchr(be->mode, 'w'), cdrom);
  634. if (err)
  635. xenbus_dev_fatal(dev, err, "creating vbd structure");
  636. else {
  637. err = xenvbd_sysfs_addif(dev);
  638. if (err) {
  639. xen_vbd_free(&be->blkif->vbd);
  640. xenbus_dev_fatal(dev, err, "creating sysfs entries");
  641. }
  642. }
  643. if (err) {
  644. kfree(be->mode);
  645. be->mode = NULL;
  646. be->major = 0;
  647. be->minor = 0;
  648. } else {
  649. /* We're potentially connected now */
  650. xen_update_blkif_status(be->blkif);
  651. }
  652. }
  653. /*
  654. * Callback received when the frontend's state changes.
  655. */
  656. static void frontend_changed(struct xenbus_device *dev,
  657. enum xenbus_state frontend_state)
  658. {
  659. struct backend_info *be = dev_get_drvdata(&dev->dev);
  660. int err;
  661. pr_debug("%s %p %s\n", __func__, dev, xenbus_strstate(frontend_state));
  662. switch (frontend_state) {
  663. case XenbusStateInitialising:
  664. if (dev->state == XenbusStateClosed) {
  665. pr_info("%s: prepare for reconnect\n", dev->nodename);
  666. xenbus_switch_state(dev, XenbusStateInitWait);
  667. }
  668. break;
  669. case XenbusStateInitialised:
  670. case XenbusStateConnected:
  671. /*
  672. * Ensure we connect even when two watches fire in
  673. * close succession and we miss the intermediate value
  674. * of frontend_state.
  675. */
  676. if (dev->state == XenbusStateConnected)
  677. break;
  678. /*
  679. * Enforce precondition before potential leak point.
  680. * xen_blkif_disconnect() is idempotent.
  681. */
  682. err = xen_blkif_disconnect(be->blkif);
  683. if (err) {
  684. xenbus_dev_fatal(dev, err, "pending I/O");
  685. break;
  686. }
  687. err = connect_ring(be);
  688. if (err) {
  689. /*
  690. * Clean up so that memory resources can be used by
  691. * other devices. connect_ring reported already error.
  692. */
  693. xen_blkif_disconnect(be->blkif);
  694. break;
  695. }
  696. xen_update_blkif_status(be->blkif);
  697. break;
  698. case XenbusStateClosing:
  699. xenbus_switch_state(dev, XenbusStateClosing);
  700. break;
  701. case XenbusStateClosed:
  702. xen_blkif_disconnect(be->blkif);
  703. xenbus_switch_state(dev, XenbusStateClosed);
  704. if (xenbus_dev_is_online(dev))
  705. break;
  706. fallthrough;
  707. /* if not online */
  708. case XenbusStateUnknown:
  709. /* implies xen_blkif_disconnect() via xen_blkbk_remove() */
  710. device_unregister(&dev->dev);
  711. break;
  712. default:
  713. xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
  714. frontend_state);
  715. break;
  716. }
  717. }
  718. /* Once a memory pressure is detected, squeeze free page pools for a while. */
  719. static unsigned int buffer_squeeze_duration_ms = 10;
  720. module_param_named(buffer_squeeze_duration_ms,
  721. buffer_squeeze_duration_ms, int, 0644);
  722. MODULE_PARM_DESC(buffer_squeeze_duration_ms,
  723. "Duration in ms to squeeze pages buffer when a memory pressure is detected");
  724. /*
  725. * Callback received when the memory pressure is detected.
  726. */
  727. static void reclaim_memory(struct xenbus_device *dev)
  728. {
  729. struct backend_info *be = dev_get_drvdata(&dev->dev);
  730. if (!be)
  731. return;
  732. be->blkif->buffer_squeeze_end = jiffies +
  733. msecs_to_jiffies(buffer_squeeze_duration_ms);
  734. }
  735. /* ** Connection ** */
  736. /*
  737. * Write the physical details regarding the block device to the store, and
  738. * switch to Connected state.
  739. */
  740. static void connect(struct backend_info *be)
  741. {
  742. struct xenbus_transaction xbt;
  743. int err;
  744. struct xenbus_device *dev = be->dev;
  745. pr_debug("%s %s\n", __func__, dev->otherend);
  746. /* Supply the information about the device the frontend needs */
  747. again:
  748. err = xenbus_transaction_start(&xbt);
  749. if (err) {
  750. xenbus_dev_fatal(dev, err, "starting transaction");
  751. return;
  752. }
  753. /* If we can't advertise it is OK. */
  754. xen_blkbk_flush_diskcache(xbt, be, be->blkif->vbd.flush_support);
  755. xen_blkbk_discard(xbt, be);
  756. xen_blkbk_barrier(xbt, be, be->blkif->vbd.flush_support);
  757. err = xenbus_printf(xbt, dev->nodename, "feature-persistent", "%u",
  758. be->blkif->vbd.feature_gnt_persistent_parm);
  759. if (err) {
  760. xenbus_dev_fatal(dev, err, "writing %s/feature-persistent",
  761. dev->nodename);
  762. goto abort;
  763. }
  764. err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
  765. (unsigned long long)vbd_sz(&be->blkif->vbd));
  766. if (err) {
  767. xenbus_dev_fatal(dev, err, "writing %s/sectors",
  768. dev->nodename);
  769. goto abort;
  770. }
  771. /* FIXME: use a typename instead */
  772. err = xenbus_printf(xbt, dev->nodename, "info", "%u",
  773. be->blkif->vbd.type |
  774. (be->blkif->vbd.readonly ? VDISK_READONLY : 0));
  775. if (err) {
  776. xenbus_dev_fatal(dev, err, "writing %s/info",
  777. dev->nodename);
  778. goto abort;
  779. }
  780. err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
  781. (unsigned long)
  782. bdev_logical_block_size(be->blkif->vbd.bdev));
  783. if (err) {
  784. xenbus_dev_fatal(dev, err, "writing %s/sector-size",
  785. dev->nodename);
  786. goto abort;
  787. }
  788. err = xenbus_printf(xbt, dev->nodename, "physical-sector-size", "%u",
  789. bdev_physical_block_size(be->blkif->vbd.bdev));
  790. if (err)
  791. xenbus_dev_error(dev, err, "writing %s/physical-sector-size",
  792. dev->nodename);
  793. err = xenbus_transaction_end(xbt, 0);
  794. if (err == -EAGAIN)
  795. goto again;
  796. if (err)
  797. xenbus_dev_fatal(dev, err, "ending transaction");
  798. err = xenbus_switch_state(dev, XenbusStateConnected);
  799. if (err)
  800. xenbus_dev_fatal(dev, err, "%s: switching to Connected state",
  801. dev->nodename);
  802. return;
  803. abort:
  804. xenbus_transaction_end(xbt, 1);
  805. }
  806. /*
  807. * Each ring may have multi pages, depends on "ring-page-order".
  808. */
  809. static int read_per_ring_refs(struct xen_blkif_ring *ring, const char *dir)
  810. {
  811. unsigned int ring_ref[XENBUS_MAX_RING_GRANTS];
  812. struct pending_req *req, *n;
  813. int err, i, j;
  814. struct xen_blkif *blkif = ring->blkif;
  815. struct xenbus_device *dev = blkif->be->dev;
  816. unsigned int nr_grefs, evtchn;
  817. err = xenbus_scanf(XBT_NIL, dir, "event-channel", "%u",
  818. &evtchn);
  819. if (err != 1) {
  820. err = -EINVAL;
  821. xenbus_dev_fatal(dev, err, "reading %s/event-channel", dir);
  822. return err;
  823. }
  824. nr_grefs = blkif->nr_ring_pages;
  825. if (unlikely(!nr_grefs)) {
  826. WARN_ON(true);
  827. return -EINVAL;
  828. }
  829. for (i = 0; i < nr_grefs; i++) {
  830. char ring_ref_name[RINGREF_NAME_LEN];
  831. if (blkif->multi_ref)
  832. snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i);
  833. else {
  834. WARN_ON(i != 0);
  835. snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref");
  836. }
  837. err = xenbus_scanf(XBT_NIL, dir, ring_ref_name,
  838. "%u", &ring_ref[i]);
  839. if (err != 1) {
  840. err = -EINVAL;
  841. xenbus_dev_fatal(dev, err, "reading %s/%s",
  842. dir, ring_ref_name);
  843. return err;
  844. }
  845. }
  846. err = -ENOMEM;
  847. for (i = 0; i < nr_grefs * XEN_BLKIF_REQS_PER_PAGE; i++) {
  848. req = kzalloc(sizeof(*req), GFP_KERNEL);
  849. if (!req)
  850. goto fail;
  851. list_add_tail(&req->free_list, &ring->pending_free);
  852. for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) {
  853. req->segments[j] = kzalloc(sizeof(*req->segments[0]), GFP_KERNEL);
  854. if (!req->segments[j])
  855. goto fail;
  856. }
  857. for (j = 0; j < MAX_INDIRECT_PAGES; j++) {
  858. req->indirect_pages[j] = kzalloc(sizeof(*req->indirect_pages[0]),
  859. GFP_KERNEL);
  860. if (!req->indirect_pages[j])
  861. goto fail;
  862. }
  863. }
  864. /* Map the shared frame, irq etc. */
  865. err = xen_blkif_map(ring, ring_ref, nr_grefs, evtchn);
  866. if (err) {
  867. xenbus_dev_fatal(dev, err, "mapping ring-ref port %u", evtchn);
  868. goto fail;
  869. }
  870. return 0;
  871. fail:
  872. list_for_each_entry_safe(req, n, &ring->pending_free, free_list) {
  873. list_del(&req->free_list);
  874. for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) {
  875. if (!req->segments[j])
  876. break;
  877. kfree(req->segments[j]);
  878. }
  879. for (j = 0; j < MAX_INDIRECT_PAGES; j++) {
  880. if (!req->indirect_pages[j])
  881. break;
  882. kfree(req->indirect_pages[j]);
  883. }
  884. kfree(req);
  885. }
  886. return err;
  887. }
  888. static int connect_ring(struct backend_info *be)
  889. {
  890. struct xenbus_device *dev = be->dev;
  891. struct xen_blkif *blkif = be->blkif;
  892. char protocol[64] = "";
  893. int err, i;
  894. char *xspath;
  895. size_t xspathsize;
  896. const size_t xenstore_path_ext_size = 11; /* sufficient for "/queue-NNN" */
  897. unsigned int requested_num_queues = 0;
  898. unsigned int ring_page_order;
  899. pr_debug("%s %s\n", __func__, dev->otherend);
  900. blkif->blk_protocol = BLKIF_PROTOCOL_DEFAULT;
  901. err = xenbus_scanf(XBT_NIL, dev->otherend, "protocol",
  902. "%63s", protocol);
  903. if (err <= 0)
  904. strcpy(protocol, "unspecified, assuming default");
  905. else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE))
  906. blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
  907. else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32))
  908. blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
  909. else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64))
  910. blkif->blk_protocol = BLKIF_PROTOCOL_X86_64;
  911. else {
  912. xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
  913. return -ENOSYS;
  914. }
  915. blkif->vbd.feature_gnt_persistent_parm = feature_persistent;
  916. blkif->vbd.feature_gnt_persistent =
  917. blkif->vbd.feature_gnt_persistent_parm &&
  918. xenbus_read_unsigned(dev->otherend, "feature-persistent", 0);
  919. blkif->vbd.overflow_max_grants = 0;
  920. /*
  921. * Read the number of hardware queues from frontend.
  922. */
  923. requested_num_queues = xenbus_read_unsigned(dev->otherend,
  924. "multi-queue-num-queues",
  925. 1);
  926. if (requested_num_queues > xenblk_max_queues
  927. || requested_num_queues == 0) {
  928. /* Buggy or malicious guest. */
  929. xenbus_dev_fatal(dev, err,
  930. "guest requested %u queues, exceeding the maximum of %u.",
  931. requested_num_queues, xenblk_max_queues);
  932. return -ENOSYS;
  933. }
  934. blkif->nr_rings = requested_num_queues;
  935. if (xen_blkif_alloc_rings(blkif))
  936. return -ENOMEM;
  937. pr_info("%s: using %d queues, protocol %d (%s) %s\n", dev->nodename,
  938. blkif->nr_rings, blkif->blk_protocol, protocol,
  939. blkif->vbd.feature_gnt_persistent ? "persistent grants" : "");
  940. err = xenbus_scanf(XBT_NIL, dev->otherend, "ring-page-order", "%u",
  941. &ring_page_order);
  942. if (err != 1) {
  943. blkif->nr_ring_pages = 1;
  944. blkif->multi_ref = false;
  945. } else if (ring_page_order <= xen_blkif_max_ring_order) {
  946. blkif->nr_ring_pages = 1 << ring_page_order;
  947. blkif->multi_ref = true;
  948. } else {
  949. err = -EINVAL;
  950. xenbus_dev_fatal(dev, err,
  951. "requested ring page order %d exceed max:%d",
  952. ring_page_order,
  953. xen_blkif_max_ring_order);
  954. return err;
  955. }
  956. if (blkif->nr_rings == 1)
  957. return read_per_ring_refs(&blkif->rings[0], dev->otherend);
  958. else {
  959. xspathsize = strlen(dev->otherend) + xenstore_path_ext_size;
  960. xspath = kmalloc(xspathsize, GFP_KERNEL);
  961. if (!xspath) {
  962. xenbus_dev_fatal(dev, -ENOMEM, "reading ring references");
  963. return -ENOMEM;
  964. }
  965. for (i = 0; i < blkif->nr_rings; i++) {
  966. memset(xspath, 0, xspathsize);
  967. snprintf(xspath, xspathsize, "%s/queue-%u", dev->otherend, i);
  968. err = read_per_ring_refs(&blkif->rings[i], xspath);
  969. if (err) {
  970. kfree(xspath);
  971. return err;
  972. }
  973. }
  974. kfree(xspath);
  975. }
  976. return 0;
  977. }
  978. static const struct xenbus_device_id xen_blkbk_ids[] = {
  979. { "vbd" },
  980. { "" }
  981. };
  982. static struct xenbus_driver xen_blkbk_driver = {
  983. .ids = xen_blkbk_ids,
  984. .probe = xen_blkbk_probe,
  985. .remove = xen_blkbk_remove,
  986. .otherend_changed = frontend_changed,
  987. .allow_rebind = true,
  988. .reclaim_memory = reclaim_memory,
  989. };
  990. int xen_blkif_xenbus_init(void)
  991. {
  992. return xenbus_register_backend(&xen_blkbk_driver);
  993. }
  994. void xen_blkif_xenbus_fini(void)
  995. {
  996. xenbus_unregister_driver(&xen_blkbk_driver);
  997. }