xenbus.c 30 KB

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