xen-block.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  1. /*
  2. * Copyright (c) 2018 Citrix Systems Inc.
  3. *
  4. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  5. * See the COPYING file in the top-level directory.
  6. */
  7. #include "qemu/osdep.h"
  8. #include "qemu/cutils.h"
  9. #include "qemu/main-loop.h"
  10. #include "qemu/module.h"
  11. #include "qemu/option.h"
  12. #include "qapi/error.h"
  13. #include "qapi/qapi-commands-block-core.h"
  14. #include "qapi/qapi-commands-qom.h"
  15. #include "qapi/qapi-visit-block-core.h"
  16. #include "qapi/qobject-input-visitor.h"
  17. #include "qapi/visitor.h"
  18. #include "qobject/qdict.h"
  19. #include "qobject/qstring.h"
  20. #include "qom/object_interfaces.h"
  21. #include "hw/block/xen_blkif.h"
  22. #include "hw/qdev-properties.h"
  23. #include "hw/xen/xen-block.h"
  24. #include "hw/xen/xen-backend.h"
  25. #include "system/blockdev.h"
  26. #include "system/block-backend.h"
  27. #include "system/iothread.h"
  28. #include "dataplane/xen-block.h"
  29. #include "hw/xen/interface/io/xs_wire.h"
  30. #include "trace.h"
  31. #define XVDA_MAJOR 202
  32. #define XVDQ_MAJOR (1 << 20)
  33. #define XVDBGQCV_MAJOR ((1 << 21) - 1)
  34. #define HDA_MAJOR 3
  35. #define HDC_MAJOR 22
  36. #define SDA_MAJOR 8
  37. static int vdev_to_diskno(unsigned int vdev_nr)
  38. {
  39. switch (vdev_nr >> 8) {
  40. case XVDA_MAJOR:
  41. case SDA_MAJOR:
  42. return (vdev_nr >> 4) & 0x15;
  43. case HDA_MAJOR:
  44. return (vdev_nr >> 6) & 1;
  45. case HDC_MAJOR:
  46. return ((vdev_nr >> 6) & 1) + 2;
  47. case XVDQ_MAJOR ... XVDBGQCV_MAJOR:
  48. return (vdev_nr >> 8) & 0xfffff;
  49. default:
  50. return -1;
  51. }
  52. }
  53. #define MAX_AUTO_VDEV 4096
  54. /*
  55. * Find a free device name in the xvda → xvdfan range and set it in
  56. * blockdev->props.vdev. Our definition of "free" is that there must
  57. * be no other disk or partition with the same disk number.
  58. *
  59. * You are technically permitted to have all of hda, hda1, sda, sda1,
  60. * xvda and xvda1 as *separate* PV block devices with separate backing
  61. * stores. That doesn't make it a good idea. This code will skip xvda
  62. * if *any* of those "conflicting" devices already exists.
  63. *
  64. * The limit of xvdfan (disk 4095) is fairly arbitrary just to avoid a
  65. * stupidly sized bitmap, but Linux as of v6.6 doesn't support anything
  66. * higher than that anyway.
  67. */
  68. static bool xen_block_find_free_vdev(XenBlockDevice *blockdev, Error **errp)
  69. {
  70. XenBus *xenbus = XEN_BUS(qdev_get_parent_bus(DEVICE(blockdev)));
  71. unsigned long used_devs[BITS_TO_LONGS(MAX_AUTO_VDEV)];
  72. XenBlockVdev *vdev = &blockdev->props.vdev;
  73. char fe_path[XENSTORE_ABS_PATH_MAX + 1];
  74. char **existing_frontends;
  75. unsigned int nr_existing = 0;
  76. unsigned int vdev_nr;
  77. int i, disk = 0;
  78. snprintf(fe_path, sizeof(fe_path), "/local/domain/%u/device/vbd",
  79. blockdev->xendev.frontend_id);
  80. existing_frontends = qemu_xen_xs_directory(xenbus->xsh, XBT_NULL, fe_path,
  81. &nr_existing);
  82. if (!existing_frontends) {
  83. if (errno == ENOENT) {
  84. /*
  85. * If the frontend directory doesn't exist because there are
  86. * no existing vbd devices, that's fine. Just ensure that we
  87. * don't dereference the NULL existing_frontends pointer, by
  88. * checking that nr_existing is zero so the loop below is not
  89. * entered.
  90. *
  91. * In fact this is redundant since nr_existing is initialized
  92. * to zero, but setting it again here makes it abundantly clear
  93. * to Coverity, and to the human reader who doesn't know the
  94. * semantics of qemu_xen_xs_directory() off the top of their
  95. * head.
  96. */
  97. nr_existing = 0;
  98. } else {
  99. /* All other errors accessing the frontend directory are fatal. */
  100. error_setg_errno(errp, errno, "cannot read %s", fe_path);
  101. return false;
  102. }
  103. }
  104. memset(used_devs, 0, sizeof(used_devs));
  105. for (i = 0; i < nr_existing; i++) {
  106. if (qemu_strtoui(existing_frontends[i], NULL, 10, &vdev_nr)) {
  107. free(existing_frontends[i]);
  108. continue;
  109. }
  110. free(existing_frontends[i]);
  111. disk = vdev_to_diskno(vdev_nr);
  112. if (disk < 0 || disk >= MAX_AUTO_VDEV) {
  113. continue;
  114. }
  115. set_bit(disk, used_devs);
  116. }
  117. free(existing_frontends);
  118. disk = find_first_zero_bit(used_devs, MAX_AUTO_VDEV);
  119. if (disk == MAX_AUTO_VDEV) {
  120. error_setg(errp, "cannot find device vdev for block device");
  121. return false;
  122. }
  123. vdev->type = XEN_BLOCK_VDEV_TYPE_XVD;
  124. vdev->partition = 0;
  125. vdev->disk = disk;
  126. if (disk < (1 << 4)) {
  127. vdev->number = (XVDA_MAJOR << 8) | (disk << 4);
  128. } else {
  129. vdev->number = (XVDQ_MAJOR << 8) | (disk << 8);
  130. }
  131. return true;
  132. }
  133. static char *xen_block_get_name(XenDevice *xendev, Error **errp)
  134. {
  135. XenBlockDevice *blockdev = XEN_BLOCK_DEVICE(xendev);
  136. XenBlockVdev *vdev = &blockdev->props.vdev;
  137. if (vdev->type == XEN_BLOCK_VDEV_TYPE_INVALID &&
  138. !xen_block_find_free_vdev(blockdev, errp)) {
  139. return NULL;
  140. }
  141. return g_strdup_printf("%lu", vdev->number);
  142. }
  143. static void xen_block_disconnect(XenDevice *xendev, Error **errp)
  144. {
  145. XenBlockDevice *blockdev = XEN_BLOCK_DEVICE(xendev);
  146. const char *type = object_get_typename(OBJECT(blockdev));
  147. XenBlockVdev *vdev = &blockdev->props.vdev;
  148. trace_xen_block_disconnect(type, vdev->disk, vdev->partition);
  149. xen_block_dataplane_stop(blockdev->dataplane);
  150. }
  151. static void xen_block_connect(XenDevice *xendev, Error **errp)
  152. {
  153. XenBlockDevice *blockdev = XEN_BLOCK_DEVICE(xendev);
  154. const char *type = object_get_typename(OBJECT(blockdev));
  155. XenBlockVdev *vdev = &blockdev->props.vdev;
  156. BlockConf *conf = &blockdev->props.conf;
  157. unsigned int feature_large_sector_size;
  158. unsigned int order, nr_ring_ref, *ring_ref, event_channel, protocol;
  159. char *str;
  160. trace_xen_block_connect(type, vdev->disk, vdev->partition);
  161. if (xen_device_frontend_scanf(xendev, "feature-large-sector-size", "%u",
  162. &feature_large_sector_size) != 1) {
  163. feature_large_sector_size = 0;
  164. }
  165. if (feature_large_sector_size != 1 &&
  166. conf->logical_block_size != XEN_BLKIF_SECTOR_SIZE) {
  167. error_setg(errp, "logical_block_size != %u not supported by frontend",
  168. XEN_BLKIF_SECTOR_SIZE);
  169. return;
  170. }
  171. if (xen_device_frontend_scanf(xendev, "ring-page-order", "%u",
  172. &order) != 1) {
  173. nr_ring_ref = 1;
  174. ring_ref = g_new(unsigned int, nr_ring_ref);
  175. if (xen_device_frontend_scanf(xendev, "ring-ref", "%u",
  176. &ring_ref[0]) != 1) {
  177. error_setg(errp, "failed to read ring-ref");
  178. g_free(ring_ref);
  179. return;
  180. }
  181. } else if (qemu_xen_gnttab_can_map_multi() &&
  182. order <= blockdev->props.max_ring_page_order) {
  183. unsigned int i;
  184. nr_ring_ref = 1 << order;
  185. ring_ref = g_new(unsigned int, nr_ring_ref);
  186. for (i = 0; i < nr_ring_ref; i++) {
  187. const char *key = g_strdup_printf("ring-ref%u", i);
  188. if (xen_device_frontend_scanf(xendev, key, "%u",
  189. &ring_ref[i]) != 1) {
  190. error_setg(errp, "failed to read %s", key);
  191. g_free((gpointer)key);
  192. g_free(ring_ref);
  193. return;
  194. }
  195. g_free((gpointer)key);
  196. }
  197. } else {
  198. error_setg(errp, "invalid ring-page-order (%d)", order);
  199. return;
  200. }
  201. if (xen_device_frontend_scanf(xendev, "event-channel", "%u",
  202. &event_channel) != 1) {
  203. error_setg(errp, "failed to read event-channel");
  204. g_free(ring_ref);
  205. return;
  206. }
  207. str = xen_device_frontend_read(xendev, "protocol");
  208. if (!str) {
  209. /* x86 defaults to the 32-bit protocol even for 64-bit guests. */
  210. if (object_dynamic_cast(OBJECT(qdev_get_machine()), "x86-machine")) {
  211. protocol = BLKIF_PROTOCOL_X86_32;
  212. } else {
  213. protocol = BLKIF_PROTOCOL_NATIVE;
  214. }
  215. } else {
  216. if (strcmp(str, XEN_IO_PROTO_ABI_X86_32) == 0) {
  217. protocol = BLKIF_PROTOCOL_X86_32;
  218. } else if (strcmp(str, XEN_IO_PROTO_ABI_X86_64) == 0) {
  219. protocol = BLKIF_PROTOCOL_X86_64;
  220. } else {
  221. protocol = BLKIF_PROTOCOL_NATIVE;
  222. }
  223. free(str);
  224. }
  225. xen_block_dataplane_start(blockdev->dataplane, ring_ref, nr_ring_ref,
  226. event_channel, protocol, errp);
  227. g_free(ring_ref);
  228. }
  229. static void xen_block_unrealize(XenDevice *xendev)
  230. {
  231. XenBlockDevice *blockdev = XEN_BLOCK_DEVICE(xendev);
  232. XenBlockDeviceClass *blockdev_class =
  233. XEN_BLOCK_DEVICE_GET_CLASS(xendev);
  234. const char *type = object_get_typename(OBJECT(blockdev));
  235. XenBlockVdev *vdev = &blockdev->props.vdev;
  236. if (vdev->type == XEN_BLOCK_VDEV_TYPE_INVALID) {
  237. return;
  238. }
  239. trace_xen_block_unrealize(type, vdev->disk, vdev->partition);
  240. /* Disconnect from the frontend in case this has not already happened */
  241. xen_block_disconnect(xendev, NULL);
  242. xen_block_dataplane_destroy(blockdev->dataplane);
  243. blockdev->dataplane = NULL;
  244. if (blockdev_class->unrealize) {
  245. blockdev_class->unrealize(blockdev);
  246. }
  247. }
  248. static void xen_block_set_size(XenBlockDevice *blockdev)
  249. {
  250. const char *type = object_get_typename(OBJECT(blockdev));
  251. XenBlockVdev *vdev = &blockdev->props.vdev;
  252. BlockConf *conf = &blockdev->props.conf;
  253. int64_t sectors = blk_getlength(conf->blk) / conf->logical_block_size;
  254. XenDevice *xendev = XEN_DEVICE(blockdev);
  255. trace_xen_block_size(type, vdev->disk, vdev->partition, sectors);
  256. xen_device_backend_printf(xendev, "sectors", "%"PRIi64, sectors);
  257. }
  258. static void xen_block_resize_cb(void *opaque)
  259. {
  260. XenBlockDevice *blockdev = opaque;
  261. XenDevice *xendev = XEN_DEVICE(blockdev);
  262. enum xenbus_state state = xen_device_backend_get_state(xendev);
  263. xen_block_set_size(blockdev);
  264. /*
  265. * Mimic the behaviour of Linux xen-blkback and re-write the state
  266. * to trigger the frontend watch.
  267. */
  268. xen_device_backend_printf(xendev, "state", "%u", state);
  269. }
  270. /* Suspend request handling */
  271. static void xen_block_drained_begin(void *opaque)
  272. {
  273. XenBlockDevice *blockdev = opaque;
  274. xen_block_dataplane_detach(blockdev->dataplane);
  275. }
  276. /* Resume request handling */
  277. static void xen_block_drained_end(void *opaque)
  278. {
  279. XenBlockDevice *blockdev = opaque;
  280. xen_block_dataplane_attach(blockdev->dataplane);
  281. }
  282. static const BlockDevOps xen_block_dev_ops = {
  283. .resize_cb = xen_block_resize_cb,
  284. .drained_begin = xen_block_drained_begin,
  285. .drained_end = xen_block_drained_end,
  286. };
  287. static void xen_block_realize(XenDevice *xendev, Error **errp)
  288. {
  289. ERRP_GUARD();
  290. XenBlockDevice *blockdev = XEN_BLOCK_DEVICE(xendev);
  291. XenBlockDeviceClass *blockdev_class =
  292. XEN_BLOCK_DEVICE_GET_CLASS(xendev);
  293. const char *type = object_get_typename(OBJECT(blockdev));
  294. XenBlockVdev *vdev = &blockdev->props.vdev;
  295. BlockConf *conf = &blockdev->props.conf;
  296. BlockBackend *blk = conf->blk;
  297. if (vdev->type == XEN_BLOCK_VDEV_TYPE_INVALID) {
  298. error_setg(errp, "vdev property not set");
  299. return;
  300. }
  301. trace_xen_block_realize(type, vdev->disk, vdev->partition);
  302. if (blockdev_class->realize) {
  303. blockdev_class->realize(blockdev, errp);
  304. if (*errp) {
  305. return;
  306. }
  307. }
  308. /*
  309. * The blkif protocol does not deal with removable media, so it must
  310. * always be present, even for CDRom devices.
  311. */
  312. assert(blk);
  313. if (!blk_is_inserted(blk)) {
  314. error_setg(errp, "device needs media, but drive is empty");
  315. return;
  316. }
  317. if (!blkconf_apply_backend_options(conf, blockdev->info & VDISK_READONLY,
  318. true, errp)) {
  319. return;
  320. }
  321. if (!(blockdev->info & VDISK_CDROM) &&
  322. !blkconf_geometry(conf, NULL, 65535, 255, 255, errp)) {
  323. return;
  324. }
  325. if (!blkconf_blocksizes(conf, errp)) {
  326. return;
  327. }
  328. if (conf->discard_granularity == -1) {
  329. conf->discard_granularity = conf->physical_block_size;
  330. }
  331. if (blk_get_flags(blk) & BDRV_O_UNMAP) {
  332. xen_device_backend_printf(xendev, "feature-discard", "%u", 1);
  333. xen_device_backend_printf(xendev, "discard-granularity", "%u",
  334. conf->discard_granularity);
  335. xen_device_backend_printf(xendev, "discard-alignment", "%u", 0);
  336. }
  337. xen_device_backend_printf(xendev, "feature-flush-cache", "%u", 1);
  338. if (qemu_xen_gnttab_can_map_multi()) {
  339. xen_device_backend_printf(xendev, "max-ring-page-order", "%u",
  340. blockdev->props.max_ring_page_order);
  341. }
  342. xen_device_backend_printf(xendev, "info", "%u", blockdev->info);
  343. xen_device_backend_printf(xendev, "mode",
  344. (blockdev->info & VDISK_READONLY) ? "r" : "w");
  345. xen_device_frontend_printf(xendev, "virtual-device", "%lu",
  346. vdev->number);
  347. xen_device_frontend_printf(xendev, "device-type", "%s",
  348. blockdev->device_type);
  349. xen_device_backend_printf(xendev, "sector-size", "%u",
  350. conf->logical_block_size);
  351. xen_block_set_size(blockdev);
  352. blockdev->dataplane =
  353. xen_block_dataplane_create(xendev, blk, conf->logical_block_size,
  354. blockdev->props.iothread);
  355. blk_set_dev_ops(blk, &xen_block_dev_ops, blockdev);
  356. }
  357. static void xen_block_frontend_changed(XenDevice *xendev,
  358. enum xenbus_state frontend_state,
  359. Error **errp)
  360. {
  361. ERRP_GUARD();
  362. enum xenbus_state backend_state = xen_device_backend_get_state(xendev);
  363. switch (frontend_state) {
  364. case XenbusStateInitialised:
  365. case XenbusStateConnected:
  366. if (backend_state == XenbusStateConnected) {
  367. break;
  368. }
  369. xen_block_disconnect(xendev, errp);
  370. if (*errp) {
  371. break;
  372. }
  373. xen_block_connect(xendev, errp);
  374. if (*errp) {
  375. break;
  376. }
  377. xen_device_backend_set_state(xendev, XenbusStateConnected);
  378. break;
  379. case XenbusStateClosing:
  380. xen_device_backend_set_state(xendev, XenbusStateClosing);
  381. break;
  382. case XenbusStateClosed:
  383. case XenbusStateUnknown:
  384. xen_block_disconnect(xendev, errp);
  385. if (*errp) {
  386. break;
  387. }
  388. xen_device_backend_set_state(xendev, XenbusStateClosed);
  389. break;
  390. default:
  391. break;
  392. }
  393. }
  394. static char *disk_to_vbd_name(unsigned int disk)
  395. {
  396. char *name, *prefix = (disk >= 26) ?
  397. disk_to_vbd_name((disk / 26) - 1) : g_strdup("");
  398. name = g_strdup_printf("%s%c", prefix, 'a' + disk % 26);
  399. g_free(prefix);
  400. return name;
  401. }
  402. static void xen_block_get_vdev(Object *obj, Visitor *v, const char *name,
  403. void *opaque, Error **errp)
  404. {
  405. const Property *prop = opaque;
  406. XenBlockVdev *vdev = object_field_prop_ptr(obj, prop);
  407. char *str;
  408. switch (vdev->type) {
  409. case XEN_BLOCK_VDEV_TYPE_DP:
  410. str = g_strdup_printf("d%lup%lu", vdev->disk, vdev->partition);
  411. break;
  412. case XEN_BLOCK_VDEV_TYPE_XVD:
  413. case XEN_BLOCK_VDEV_TYPE_HD:
  414. case XEN_BLOCK_VDEV_TYPE_SD: {
  415. char *vbd_name = disk_to_vbd_name(vdev->disk);
  416. str = g_strdup_printf("%s%s%lu",
  417. (vdev->type == XEN_BLOCK_VDEV_TYPE_XVD) ?
  418. "xvd" :
  419. (vdev->type == XEN_BLOCK_VDEV_TYPE_HD) ?
  420. "hd" :
  421. "sd",
  422. vbd_name, vdev->partition);
  423. g_free(vbd_name);
  424. break;
  425. }
  426. default:
  427. error_setg(errp, "invalid vdev type");
  428. return;
  429. }
  430. visit_type_str(v, name, &str, errp);
  431. g_free(str);
  432. }
  433. static int vbd_name_to_disk(const char *name, const char **endp,
  434. unsigned long *disk)
  435. {
  436. unsigned int n = 0;
  437. while (*name != '\0') {
  438. if (!g_ascii_isalpha(*name) || !g_ascii_islower(*name)) {
  439. break;
  440. }
  441. n *= 26;
  442. n += *name++ - 'a' + 1;
  443. }
  444. *endp = name;
  445. if (!n) {
  446. return -1;
  447. }
  448. *disk = n - 1;
  449. return 0;
  450. }
  451. static void xen_block_set_vdev(Object *obj, Visitor *v, const char *name,
  452. void *opaque, Error **errp)
  453. {
  454. const Property *prop = opaque;
  455. XenBlockVdev *vdev = object_field_prop_ptr(obj, prop);
  456. char *str, *p;
  457. const char *end;
  458. if (!visit_type_str(v, name, &str, errp)) {
  459. return;
  460. }
  461. p = strchr(str, 'd');
  462. if (!p) {
  463. goto invalid;
  464. }
  465. *p++ = '\0';
  466. if (*str == '\0') {
  467. vdev->type = XEN_BLOCK_VDEV_TYPE_DP;
  468. } else if (strcmp(str, "xv") == 0) {
  469. vdev->type = XEN_BLOCK_VDEV_TYPE_XVD;
  470. } else if (strcmp(str, "h") == 0) {
  471. vdev->type = XEN_BLOCK_VDEV_TYPE_HD;
  472. } else if (strcmp(str, "s") == 0) {
  473. vdev->type = XEN_BLOCK_VDEV_TYPE_SD;
  474. } else {
  475. goto invalid;
  476. }
  477. if (vdev->type == XEN_BLOCK_VDEV_TYPE_DP) {
  478. if (qemu_strtoul(p, &end, 10, &vdev->disk)) {
  479. goto invalid;
  480. }
  481. if (*end == 'p') {
  482. if (*(++end) == '\0') {
  483. goto invalid;
  484. }
  485. }
  486. } else {
  487. if (vbd_name_to_disk(p, &end, &vdev->disk)) {
  488. goto invalid;
  489. }
  490. }
  491. if (*end != '\0') {
  492. p = (char *)end;
  493. if (qemu_strtoul(p, &end, 10, &vdev->partition)) {
  494. goto invalid;
  495. }
  496. if (*end != '\0') {
  497. goto invalid;
  498. }
  499. } else {
  500. vdev->partition = 0;
  501. }
  502. switch (vdev->type) {
  503. case XEN_BLOCK_VDEV_TYPE_DP:
  504. case XEN_BLOCK_VDEV_TYPE_XVD:
  505. if (vdev->disk < (1 << 4) && vdev->partition < (1 << 4)) {
  506. vdev->number = (XVDA_MAJOR << 8) | (vdev->disk << 4) |
  507. vdev->partition;
  508. } else if (vdev->disk < (1 << 20) && vdev->partition < (1 << 8)) {
  509. vdev->number = (XVDQ_MAJOR << 8) | (vdev->disk << 8) |
  510. vdev->partition;
  511. } else {
  512. goto invalid;
  513. }
  514. break;
  515. case XEN_BLOCK_VDEV_TYPE_HD:
  516. if ((vdev->disk == 0 || vdev->disk == 1) &&
  517. vdev->partition < (1 << 6)) {
  518. vdev->number = (HDA_MAJOR << 8) | (vdev->disk << 6) |
  519. vdev->partition;
  520. } else if ((vdev->disk == 2 || vdev->disk == 3) &&
  521. vdev->partition < (1 << 6)) {
  522. vdev->number = (HDC_MAJOR << 8) | ((vdev->disk - 2) << 6) |
  523. vdev->partition;
  524. } else {
  525. goto invalid;
  526. }
  527. break;
  528. case XEN_BLOCK_VDEV_TYPE_SD:
  529. if (vdev->disk < (1 << 4) && vdev->partition < (1 << 4)) {
  530. vdev->number = (SDA_MAJOR << 8) | (vdev->disk << 4) |
  531. vdev->partition;
  532. } else {
  533. goto invalid;
  534. }
  535. break;
  536. default:
  537. goto invalid;
  538. }
  539. g_free(str);
  540. return;
  541. invalid:
  542. error_setg(errp, "invalid virtual disk specifier");
  543. vdev->type = XEN_BLOCK_VDEV_TYPE_INVALID;
  544. g_free(str);
  545. }
  546. /*
  547. * This property deals with 'vdev' names adhering to the Xen VBD naming
  548. * scheme described in:
  549. *
  550. * https://xenbits.xen.org/docs/unstable/man/xen-vbd-interface.7.html
  551. */
  552. static const PropertyInfo xen_block_prop_vdev = {
  553. .type = "str",
  554. .description = "Virtual Disk specifier (d*p*/xvd*/hd*/sd*)",
  555. .get = xen_block_get_vdev,
  556. .set = xen_block_set_vdev,
  557. };
  558. static const Property xen_block_props[] = {
  559. DEFINE_PROP("vdev", XenBlockDevice, props.vdev,
  560. xen_block_prop_vdev, XenBlockVdev),
  561. DEFINE_BLOCK_PROPERTIES(XenBlockDevice, props.conf),
  562. DEFINE_PROP_UINT32("max-ring-page-order", XenBlockDevice,
  563. props.max_ring_page_order, 4),
  564. DEFINE_PROP_LINK("iothread", XenBlockDevice, props.iothread,
  565. TYPE_IOTHREAD, IOThread *),
  566. };
  567. static void xen_block_class_init(ObjectClass *class, void *data)
  568. {
  569. DeviceClass *dev_class = DEVICE_CLASS(class);
  570. XenDeviceClass *xendev_class = XEN_DEVICE_CLASS(class);
  571. xendev_class->backend = "qdisk";
  572. xendev_class->device = "vbd";
  573. xendev_class->get_name = xen_block_get_name;
  574. xendev_class->realize = xen_block_realize;
  575. xendev_class->frontend_changed = xen_block_frontend_changed;
  576. xendev_class->unrealize = xen_block_unrealize;
  577. device_class_set_props(dev_class, xen_block_props);
  578. }
  579. static const TypeInfo xen_block_type_info = {
  580. .name = TYPE_XEN_BLOCK_DEVICE,
  581. .parent = TYPE_XEN_DEVICE,
  582. .instance_size = sizeof(XenBlockDevice),
  583. .abstract = true,
  584. .class_size = sizeof(XenBlockDeviceClass),
  585. .class_init = xen_block_class_init,
  586. };
  587. static void xen_disk_unrealize(XenBlockDevice *blockdev)
  588. {
  589. trace_xen_disk_unrealize();
  590. }
  591. static void xen_disk_realize(XenBlockDevice *blockdev, Error **errp)
  592. {
  593. BlockConf *conf = &blockdev->props.conf;
  594. trace_xen_disk_realize();
  595. blockdev->device_type = "disk";
  596. if (!conf->blk) {
  597. error_setg(errp, "drive property not set");
  598. return;
  599. }
  600. blockdev->info = blk_supports_write_perm(conf->blk) ? 0 : VDISK_READONLY;
  601. }
  602. static void xen_disk_class_init(ObjectClass *class, void *data)
  603. {
  604. DeviceClass *dev_class = DEVICE_CLASS(class);
  605. XenBlockDeviceClass *blockdev_class = XEN_BLOCK_DEVICE_CLASS(class);
  606. blockdev_class->realize = xen_disk_realize;
  607. blockdev_class->unrealize = xen_disk_unrealize;
  608. dev_class->desc = "Xen Disk Device";
  609. }
  610. static const TypeInfo xen_disk_type_info = {
  611. .name = TYPE_XEN_DISK_DEVICE,
  612. .parent = TYPE_XEN_BLOCK_DEVICE,
  613. .instance_size = sizeof(XenDiskDevice),
  614. .class_init = xen_disk_class_init,
  615. };
  616. static void xen_cdrom_unrealize(XenBlockDevice *blockdev)
  617. {
  618. trace_xen_cdrom_unrealize();
  619. }
  620. static void xen_cdrom_realize(XenBlockDevice *blockdev, Error **errp)
  621. {
  622. BlockConf *conf = &blockdev->props.conf;
  623. trace_xen_cdrom_realize();
  624. blockdev->device_type = "cdrom";
  625. if (!conf->blk) {
  626. int rc;
  627. /* Set up an empty drive */
  628. conf->blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
  629. rc = blk_attach_dev(conf->blk, DEVICE(blockdev));
  630. if (!rc) {
  631. error_setg_errno(errp, -rc, "failed to create drive");
  632. return;
  633. }
  634. }
  635. blockdev->info = VDISK_READONLY | VDISK_CDROM;
  636. }
  637. static void xen_cdrom_class_init(ObjectClass *class, void *data)
  638. {
  639. DeviceClass *dev_class = DEVICE_CLASS(class);
  640. XenBlockDeviceClass *blockdev_class = XEN_BLOCK_DEVICE_CLASS(class);
  641. blockdev_class->realize = xen_cdrom_realize;
  642. blockdev_class->unrealize = xen_cdrom_unrealize;
  643. dev_class->desc = "Xen CD-ROM Device";
  644. }
  645. static const TypeInfo xen_cdrom_type_info = {
  646. .name = TYPE_XEN_CDROM_DEVICE,
  647. .parent = TYPE_XEN_BLOCK_DEVICE,
  648. .instance_size = sizeof(XenCDRomDevice),
  649. .class_init = xen_cdrom_class_init,
  650. };
  651. static void xen_block_register_types(void)
  652. {
  653. type_register_static(&xen_block_type_info);
  654. type_register_static(&xen_disk_type_info);
  655. type_register_static(&xen_cdrom_type_info);
  656. }
  657. type_init(xen_block_register_types)
  658. static void xen_block_blockdev_del(const char *node_name, Error **errp)
  659. {
  660. trace_xen_block_blockdev_del(node_name);
  661. qmp_blockdev_del(node_name, errp);
  662. }
  663. static char *xen_block_blockdev_add(const char *id, QDict *qdict,
  664. Error **errp)
  665. {
  666. ERRP_GUARD();
  667. const char *driver = qdict_get_try_str(qdict, "driver");
  668. BlockdevOptions *options = NULL;
  669. char *node_name;
  670. Visitor *v;
  671. if (!driver) {
  672. error_setg(errp, "no 'driver' parameter");
  673. return NULL;
  674. }
  675. node_name = g_strdup_printf("%s-%s", id, driver);
  676. qdict_put_str(qdict, "node-name", node_name);
  677. trace_xen_block_blockdev_add(node_name);
  678. v = qobject_input_visitor_new(QOBJECT(qdict));
  679. visit_type_BlockdevOptions(v, NULL, &options, errp);
  680. visit_free(v);
  681. if (!options) {
  682. goto fail;
  683. }
  684. qmp_blockdev_add(options, errp);
  685. if (*errp) {
  686. goto fail;
  687. }
  688. qapi_free_BlockdevOptions(options);
  689. return node_name;
  690. fail:
  691. if (options) {
  692. qapi_free_BlockdevOptions(options);
  693. }
  694. g_free(node_name);
  695. return NULL;
  696. }
  697. static void xen_block_drive_destroy(XenBlockDrive *drive, Error **errp)
  698. {
  699. ERRP_GUARD();
  700. char *node_name = drive->node_name;
  701. if (node_name) {
  702. xen_block_blockdev_del(node_name, errp);
  703. if (*errp) {
  704. return;
  705. }
  706. g_free(node_name);
  707. drive->node_name = NULL;
  708. }
  709. g_free(drive->id);
  710. g_free(drive);
  711. }
  712. static XenBlockDrive *xen_block_drive_create(const char *id,
  713. const char *device_type,
  714. QDict *opts, Error **errp)
  715. {
  716. ERRP_GUARD();
  717. const char *params = qdict_get_try_str(opts, "params");
  718. const char *mode = qdict_get_try_str(opts, "mode");
  719. const char *direct_io_safe = qdict_get_try_str(opts, "direct-io-safe");
  720. const char *discard_enable = qdict_get_try_str(opts, "discard-enable");
  721. char *driver = NULL;
  722. char *filename = NULL;
  723. XenBlockDrive *drive = NULL;
  724. QDict *file_layer;
  725. QDict *driver_layer;
  726. struct stat st;
  727. int rc;
  728. if (params) {
  729. char **v = g_strsplit(params, ":", 2);
  730. if (v[1] == NULL) {
  731. filename = g_strdup(v[0]);
  732. driver = g_strdup("raw");
  733. } else {
  734. if (strcmp(v[0], "aio") == 0) {
  735. driver = g_strdup("raw");
  736. } else if (strcmp(v[0], "vhd") == 0) {
  737. driver = g_strdup("vpc");
  738. } else {
  739. driver = g_strdup(v[0]);
  740. }
  741. filename = g_strdup(v[1]);
  742. }
  743. g_strfreev(v);
  744. } else {
  745. error_setg(errp, "no params");
  746. goto done;
  747. }
  748. assert(filename);
  749. assert(driver);
  750. drive = g_new0(XenBlockDrive, 1);
  751. drive->id = g_strdup(id);
  752. rc = stat(filename, &st);
  753. if (rc) {
  754. error_setg_errno(errp, errno, "Could not stat file '%s'", filename);
  755. goto done;
  756. }
  757. file_layer = qdict_new();
  758. driver_layer = qdict_new();
  759. if (S_ISBLK(st.st_mode)) {
  760. qdict_put_str(file_layer, "driver", "host_device");
  761. } else {
  762. qdict_put_str(file_layer, "driver", "file");
  763. }
  764. qdict_put_str(file_layer, "filename", filename);
  765. if (mode && *mode != 'w') {
  766. qdict_put_bool(file_layer, "read-only", true);
  767. }
  768. if (direct_io_safe) {
  769. unsigned long value;
  770. if (!qemu_strtoul(direct_io_safe, NULL, 2, &value) && !!value) {
  771. QDict *cache_qdict = qdict_new();
  772. qdict_put_bool(cache_qdict, "direct", true);
  773. qdict_put(file_layer, "cache", cache_qdict);
  774. qdict_put_str(file_layer, "aio", "native");
  775. }
  776. }
  777. if (discard_enable) {
  778. unsigned long value;
  779. if (!qemu_strtoul(discard_enable, NULL, 2, &value) && !!value) {
  780. qdict_put_str(file_layer, "discard", "unmap");
  781. qdict_put_str(driver_layer, "discard", "unmap");
  782. }
  783. }
  784. /*
  785. * It is necessary to turn file locking off as an emulated device
  786. * may have already opened the same image file.
  787. */
  788. qdict_put_str(file_layer, "locking", "off");
  789. qdict_put_str(driver_layer, "driver", driver);
  790. qdict_put(driver_layer, "file", file_layer);
  791. g_assert(!drive->node_name);
  792. drive->node_name = xen_block_blockdev_add(drive->id, driver_layer,
  793. errp);
  794. qobject_unref(driver_layer);
  795. done:
  796. g_free(filename);
  797. g_free(driver);
  798. if (*errp) {
  799. xen_block_drive_destroy(drive, NULL);
  800. return NULL;
  801. }
  802. return drive;
  803. }
  804. static const char *xen_block_drive_get_node_name(XenBlockDrive *drive)
  805. {
  806. return drive->node_name ? drive->node_name : "";
  807. }
  808. static void xen_block_iothread_destroy(XenBlockIOThread *iothread,
  809. Error **errp)
  810. {
  811. qmp_object_del(iothread->id, errp);
  812. g_free(iothread->id);
  813. g_free(iothread);
  814. }
  815. static XenBlockIOThread *xen_block_iothread_create(const char *id,
  816. Error **errp)
  817. {
  818. ERRP_GUARD();
  819. XenBlockIOThread *iothread = g_new(XenBlockIOThread, 1);
  820. ObjectOptions *opts;
  821. iothread->id = g_strdup(id);
  822. opts = g_new(ObjectOptions, 1);
  823. *opts = (ObjectOptions) {
  824. .qom_type = OBJECT_TYPE_IOTHREAD,
  825. .id = g_strdup(id),
  826. };
  827. qmp_object_add(opts, errp);
  828. qapi_free_ObjectOptions(opts);
  829. if (*errp) {
  830. g_free(iothread->id);
  831. g_free(iothread);
  832. return NULL;
  833. }
  834. return iothread;
  835. }
  836. static void xen_block_device_create(XenBackendInstance *backend,
  837. QDict *opts, Error **errp)
  838. {
  839. ERRP_GUARD();
  840. XenBus *xenbus = xen_backend_get_bus(backend);
  841. const char *name = xen_backend_get_name(backend);
  842. unsigned long number;
  843. const char *vdev, *device_type;
  844. XenBlockDrive *drive = NULL;
  845. XenBlockIOThread *iothread = NULL;
  846. XenDevice *xendev = NULL;
  847. const char *type;
  848. XenBlockDevice *blockdev;
  849. if (qemu_strtoul(name, NULL, 10, &number)) {
  850. error_setg(errp, "failed to parse name '%s'", name);
  851. goto fail;
  852. }
  853. trace_xen_block_device_create(number);
  854. vdev = qdict_get_try_str(opts, "dev");
  855. if (!vdev) {
  856. error_setg(errp, "no dev parameter");
  857. goto fail;
  858. }
  859. device_type = qdict_get_try_str(opts, "device-type");
  860. if (!device_type) {
  861. error_setg(errp, "no device-type parameter");
  862. goto fail;
  863. }
  864. if (!strcmp(device_type, "disk")) {
  865. type = TYPE_XEN_DISK_DEVICE;
  866. } else if (!strcmp(device_type, "cdrom")) {
  867. type = TYPE_XEN_CDROM_DEVICE;
  868. } else {
  869. error_setg(errp, "invalid device-type parameter '%s'", device_type);
  870. goto fail;
  871. }
  872. drive = xen_block_drive_create(vdev, device_type, opts, errp);
  873. if (!drive) {
  874. error_prepend(errp, "failed to create drive: ");
  875. goto fail;
  876. }
  877. iothread = xen_block_iothread_create(vdev, errp);
  878. if (*errp) {
  879. error_prepend(errp, "failed to create iothread: ");
  880. goto fail;
  881. }
  882. xendev = XEN_DEVICE(qdev_new(type));
  883. blockdev = XEN_BLOCK_DEVICE(xendev);
  884. if (!object_property_set_str(OBJECT(xendev), "vdev", vdev,
  885. errp)) {
  886. error_prepend(errp, "failed to set 'vdev': ");
  887. goto fail;
  888. }
  889. if (!object_property_set_str(OBJECT(xendev), "drive",
  890. xen_block_drive_get_node_name(drive),
  891. errp)) {
  892. error_prepend(errp, "failed to set 'drive': ");
  893. goto fail;
  894. }
  895. if (!object_property_set_str(OBJECT(xendev), "iothread", iothread->id,
  896. errp)) {
  897. error_prepend(errp, "failed to set 'iothread': ");
  898. goto fail;
  899. }
  900. blockdev->iothread = iothread;
  901. blockdev->drive = drive;
  902. if (!qdev_realize_and_unref(DEVICE(xendev), BUS(xenbus), errp)) {
  903. error_prepend(errp, "realization of device %s failed: ", type);
  904. goto fail;
  905. }
  906. xen_backend_set_device(backend, xendev);
  907. return;
  908. fail:
  909. if (xendev) {
  910. object_unparent(OBJECT(xendev));
  911. }
  912. if (iothread) {
  913. xen_block_iothread_destroy(iothread, NULL);
  914. }
  915. if (drive) {
  916. xen_block_drive_destroy(drive, NULL);
  917. }
  918. }
  919. static void xen_block_device_destroy(XenBackendInstance *backend,
  920. Error **errp)
  921. {
  922. ERRP_GUARD();
  923. XenDevice *xendev = xen_backend_get_device(backend);
  924. XenBlockDevice *blockdev = XEN_BLOCK_DEVICE(xendev);
  925. XenBlockVdev *vdev = &blockdev->props.vdev;
  926. XenBlockDrive *drive = blockdev->drive;
  927. XenBlockIOThread *iothread = blockdev->iothread;
  928. trace_xen_block_device_destroy(vdev->number);
  929. object_unparent(OBJECT(xendev));
  930. /*
  931. * Drain all pending RCU callbacks as object_unparent() frees `xendev'
  932. * in a RCU callback.
  933. * And due to the property "drive" still existing in `xendev', we
  934. * can't destroy the XenBlockDrive associated with `xendev' with
  935. * xen_block_drive_destroy() below.
  936. */
  937. drain_call_rcu();
  938. if (iothread) {
  939. xen_block_iothread_destroy(iothread, errp);
  940. if (*errp) {
  941. error_prepend(errp, "failed to destroy iothread: ");
  942. return;
  943. }
  944. }
  945. if (drive) {
  946. xen_block_drive_destroy(drive, errp);
  947. if (*errp) {
  948. error_prepend(errp, "failed to destroy drive: ");
  949. return;
  950. }
  951. }
  952. }
  953. static const XenBackendInfo xen_block_backend_info = {
  954. .type = "qdisk",
  955. .create = xen_block_device_create,
  956. .destroy = xen_block_device_destroy,
  957. };
  958. static void xen_block_register_backend(void)
  959. {
  960. xen_backend_register(&xen_block_backend_info);
  961. }
  962. xen_backend_init(xen_block_register_backend);