xen-block.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  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_frontend_printf(xendev, "virtual-device", "%lu",
  344. vdev->number);
  345. xen_device_frontend_printf(xendev, "device-type", "%s",
  346. blockdev->device_type);
  347. xen_device_backend_printf(xendev, "sector-size", "%u",
  348. conf->logical_block_size);
  349. xen_block_set_size(blockdev);
  350. blockdev->dataplane =
  351. xen_block_dataplane_create(xendev, blk, conf->logical_block_size,
  352. blockdev->props.iothread);
  353. blk_set_dev_ops(blk, &xen_block_dev_ops, blockdev);
  354. }
  355. static void xen_block_frontend_changed(XenDevice *xendev,
  356. enum xenbus_state frontend_state,
  357. Error **errp)
  358. {
  359. ERRP_GUARD();
  360. enum xenbus_state backend_state = xen_device_backend_get_state(xendev);
  361. switch (frontend_state) {
  362. case XenbusStateInitialised:
  363. case XenbusStateConnected:
  364. if (backend_state == XenbusStateConnected) {
  365. break;
  366. }
  367. xen_block_disconnect(xendev, errp);
  368. if (*errp) {
  369. break;
  370. }
  371. xen_block_connect(xendev, errp);
  372. if (*errp) {
  373. break;
  374. }
  375. xen_device_backend_set_state(xendev, XenbusStateConnected);
  376. break;
  377. case XenbusStateClosing:
  378. xen_device_backend_set_state(xendev, XenbusStateClosing);
  379. break;
  380. case XenbusStateClosed:
  381. case XenbusStateUnknown:
  382. xen_block_disconnect(xendev, errp);
  383. if (*errp) {
  384. break;
  385. }
  386. xen_device_backend_set_state(xendev, XenbusStateClosed);
  387. break;
  388. default:
  389. break;
  390. }
  391. }
  392. static char *disk_to_vbd_name(unsigned int disk)
  393. {
  394. char *name, *prefix = (disk >= 26) ?
  395. disk_to_vbd_name((disk / 26) - 1) : g_strdup("");
  396. name = g_strdup_printf("%s%c", prefix, 'a' + disk % 26);
  397. g_free(prefix);
  398. return name;
  399. }
  400. static void xen_block_get_vdev(Object *obj, Visitor *v, const char *name,
  401. void *opaque, Error **errp)
  402. {
  403. const Property *prop = opaque;
  404. XenBlockVdev *vdev = object_field_prop_ptr(obj, prop);
  405. char *str;
  406. switch (vdev->type) {
  407. case XEN_BLOCK_VDEV_TYPE_DP:
  408. str = g_strdup_printf("d%lup%lu", vdev->disk, vdev->partition);
  409. break;
  410. case XEN_BLOCK_VDEV_TYPE_XVD:
  411. case XEN_BLOCK_VDEV_TYPE_HD:
  412. case XEN_BLOCK_VDEV_TYPE_SD: {
  413. char *vbd_name = disk_to_vbd_name(vdev->disk);
  414. str = g_strdup_printf("%s%s%lu",
  415. (vdev->type == XEN_BLOCK_VDEV_TYPE_XVD) ?
  416. "xvd" :
  417. (vdev->type == XEN_BLOCK_VDEV_TYPE_HD) ?
  418. "hd" :
  419. "sd",
  420. vbd_name, vdev->partition);
  421. g_free(vbd_name);
  422. break;
  423. }
  424. default:
  425. error_setg(errp, "invalid vdev type");
  426. return;
  427. }
  428. visit_type_str(v, name, &str, errp);
  429. g_free(str);
  430. }
  431. static int vbd_name_to_disk(const char *name, const char **endp,
  432. unsigned long *disk)
  433. {
  434. unsigned int n = 0;
  435. while (*name != '\0') {
  436. if (!g_ascii_isalpha(*name) || !g_ascii_islower(*name)) {
  437. break;
  438. }
  439. n *= 26;
  440. n += *name++ - 'a' + 1;
  441. }
  442. *endp = name;
  443. if (!n) {
  444. return -1;
  445. }
  446. *disk = n - 1;
  447. return 0;
  448. }
  449. static void xen_block_set_vdev(Object *obj, Visitor *v, const char *name,
  450. void *opaque, Error **errp)
  451. {
  452. const Property *prop = opaque;
  453. XenBlockVdev *vdev = object_field_prop_ptr(obj, prop);
  454. char *str, *p;
  455. const char *end;
  456. if (!visit_type_str(v, name, &str, errp)) {
  457. return;
  458. }
  459. p = strchr(str, 'd');
  460. if (!p) {
  461. goto invalid;
  462. }
  463. *p++ = '\0';
  464. if (*str == '\0') {
  465. vdev->type = XEN_BLOCK_VDEV_TYPE_DP;
  466. } else if (strcmp(str, "xv") == 0) {
  467. vdev->type = XEN_BLOCK_VDEV_TYPE_XVD;
  468. } else if (strcmp(str, "h") == 0) {
  469. vdev->type = XEN_BLOCK_VDEV_TYPE_HD;
  470. } else if (strcmp(str, "s") == 0) {
  471. vdev->type = XEN_BLOCK_VDEV_TYPE_SD;
  472. } else {
  473. goto invalid;
  474. }
  475. if (vdev->type == XEN_BLOCK_VDEV_TYPE_DP) {
  476. if (qemu_strtoul(p, &end, 10, &vdev->disk)) {
  477. goto invalid;
  478. }
  479. if (*end == 'p') {
  480. if (*(++end) == '\0') {
  481. goto invalid;
  482. }
  483. }
  484. } else {
  485. if (vbd_name_to_disk(p, &end, &vdev->disk)) {
  486. goto invalid;
  487. }
  488. }
  489. if (*end != '\0') {
  490. p = (char *)end;
  491. if (qemu_strtoul(p, &end, 10, &vdev->partition)) {
  492. goto invalid;
  493. }
  494. if (*end != '\0') {
  495. goto invalid;
  496. }
  497. } else {
  498. vdev->partition = 0;
  499. }
  500. switch (vdev->type) {
  501. case XEN_BLOCK_VDEV_TYPE_DP:
  502. case XEN_BLOCK_VDEV_TYPE_XVD:
  503. if (vdev->disk < (1 << 4) && vdev->partition < (1 << 4)) {
  504. vdev->number = (XVDA_MAJOR << 8) | (vdev->disk << 4) |
  505. vdev->partition;
  506. } else if (vdev->disk < (1 << 20) && vdev->partition < (1 << 8)) {
  507. vdev->number = (XVDQ_MAJOR << 8) | (vdev->disk << 8) |
  508. vdev->partition;
  509. } else {
  510. goto invalid;
  511. }
  512. break;
  513. case XEN_BLOCK_VDEV_TYPE_HD:
  514. if ((vdev->disk == 0 || vdev->disk == 1) &&
  515. vdev->partition < (1 << 6)) {
  516. vdev->number = (HDA_MAJOR << 8) | (vdev->disk << 6) |
  517. vdev->partition;
  518. } else if ((vdev->disk == 2 || vdev->disk == 3) &&
  519. vdev->partition < (1 << 6)) {
  520. vdev->number = (HDC_MAJOR << 8) | ((vdev->disk - 2) << 6) |
  521. vdev->partition;
  522. } else {
  523. goto invalid;
  524. }
  525. break;
  526. case XEN_BLOCK_VDEV_TYPE_SD:
  527. if (vdev->disk < (1 << 4) && vdev->partition < (1 << 4)) {
  528. vdev->number = (SDA_MAJOR << 8) | (vdev->disk << 4) |
  529. vdev->partition;
  530. } else {
  531. goto invalid;
  532. }
  533. break;
  534. default:
  535. goto invalid;
  536. }
  537. g_free(str);
  538. return;
  539. invalid:
  540. error_setg(errp, "invalid virtual disk specifier");
  541. vdev->type = XEN_BLOCK_VDEV_TYPE_INVALID;
  542. g_free(str);
  543. }
  544. /*
  545. * This property deals with 'vdev' names adhering to the Xen VBD naming
  546. * scheme described in:
  547. *
  548. * https://xenbits.xen.org/docs/unstable/man/xen-vbd-interface.7.html
  549. */
  550. static const PropertyInfo xen_block_prop_vdev = {
  551. .name = "str",
  552. .description = "Virtual Disk specifier: d*p*/xvd*/hd*/sd*",
  553. .get = xen_block_get_vdev,
  554. .set = xen_block_set_vdev,
  555. };
  556. static const Property xen_block_props[] = {
  557. DEFINE_PROP("vdev", XenBlockDevice, props.vdev,
  558. xen_block_prop_vdev, XenBlockVdev),
  559. DEFINE_BLOCK_PROPERTIES(XenBlockDevice, props.conf),
  560. DEFINE_PROP_UINT32("max-ring-page-order", XenBlockDevice,
  561. props.max_ring_page_order, 4),
  562. DEFINE_PROP_LINK("iothread", XenBlockDevice, props.iothread,
  563. TYPE_IOTHREAD, IOThread *),
  564. };
  565. static void xen_block_class_init(ObjectClass *class, void *data)
  566. {
  567. DeviceClass *dev_class = DEVICE_CLASS(class);
  568. XenDeviceClass *xendev_class = XEN_DEVICE_CLASS(class);
  569. xendev_class->backend = "qdisk";
  570. xendev_class->device = "vbd";
  571. xendev_class->get_name = xen_block_get_name;
  572. xendev_class->realize = xen_block_realize;
  573. xendev_class->frontend_changed = xen_block_frontend_changed;
  574. xendev_class->unrealize = xen_block_unrealize;
  575. device_class_set_props(dev_class, xen_block_props);
  576. }
  577. static const TypeInfo xen_block_type_info = {
  578. .name = TYPE_XEN_BLOCK_DEVICE,
  579. .parent = TYPE_XEN_DEVICE,
  580. .instance_size = sizeof(XenBlockDevice),
  581. .abstract = true,
  582. .class_size = sizeof(XenBlockDeviceClass),
  583. .class_init = xen_block_class_init,
  584. };
  585. static void xen_disk_unrealize(XenBlockDevice *blockdev)
  586. {
  587. trace_xen_disk_unrealize();
  588. }
  589. static void xen_disk_realize(XenBlockDevice *blockdev, Error **errp)
  590. {
  591. BlockConf *conf = &blockdev->props.conf;
  592. trace_xen_disk_realize();
  593. blockdev->device_type = "disk";
  594. if (!conf->blk) {
  595. error_setg(errp, "drive property not set");
  596. return;
  597. }
  598. blockdev->info = blk_supports_write_perm(conf->blk) ? 0 : VDISK_READONLY;
  599. }
  600. static void xen_disk_class_init(ObjectClass *class, void *data)
  601. {
  602. DeviceClass *dev_class = DEVICE_CLASS(class);
  603. XenBlockDeviceClass *blockdev_class = XEN_BLOCK_DEVICE_CLASS(class);
  604. blockdev_class->realize = xen_disk_realize;
  605. blockdev_class->unrealize = xen_disk_unrealize;
  606. dev_class->desc = "Xen Disk Device";
  607. }
  608. static const TypeInfo xen_disk_type_info = {
  609. .name = TYPE_XEN_DISK_DEVICE,
  610. .parent = TYPE_XEN_BLOCK_DEVICE,
  611. .instance_size = sizeof(XenDiskDevice),
  612. .class_init = xen_disk_class_init,
  613. };
  614. static void xen_cdrom_unrealize(XenBlockDevice *blockdev)
  615. {
  616. trace_xen_cdrom_unrealize();
  617. }
  618. static void xen_cdrom_realize(XenBlockDevice *blockdev, Error **errp)
  619. {
  620. BlockConf *conf = &blockdev->props.conf;
  621. trace_xen_cdrom_realize();
  622. blockdev->device_type = "cdrom";
  623. if (!conf->blk) {
  624. int rc;
  625. /* Set up an empty drive */
  626. conf->blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
  627. rc = blk_attach_dev(conf->blk, DEVICE(blockdev));
  628. if (!rc) {
  629. error_setg_errno(errp, -rc, "failed to create drive");
  630. return;
  631. }
  632. }
  633. blockdev->info = VDISK_READONLY | VDISK_CDROM;
  634. }
  635. static void xen_cdrom_class_init(ObjectClass *class, void *data)
  636. {
  637. DeviceClass *dev_class = DEVICE_CLASS(class);
  638. XenBlockDeviceClass *blockdev_class = XEN_BLOCK_DEVICE_CLASS(class);
  639. blockdev_class->realize = xen_cdrom_realize;
  640. blockdev_class->unrealize = xen_cdrom_unrealize;
  641. dev_class->desc = "Xen CD-ROM Device";
  642. }
  643. static const TypeInfo xen_cdrom_type_info = {
  644. .name = TYPE_XEN_CDROM_DEVICE,
  645. .parent = TYPE_XEN_BLOCK_DEVICE,
  646. .instance_size = sizeof(XenCDRomDevice),
  647. .class_init = xen_cdrom_class_init,
  648. };
  649. static void xen_block_register_types(void)
  650. {
  651. type_register_static(&xen_block_type_info);
  652. type_register_static(&xen_disk_type_info);
  653. type_register_static(&xen_cdrom_type_info);
  654. }
  655. type_init(xen_block_register_types)
  656. static void xen_block_blockdev_del(const char *node_name, Error **errp)
  657. {
  658. trace_xen_block_blockdev_del(node_name);
  659. qmp_blockdev_del(node_name, errp);
  660. }
  661. static char *xen_block_blockdev_add(const char *id, QDict *qdict,
  662. Error **errp)
  663. {
  664. ERRP_GUARD();
  665. const char *driver = qdict_get_try_str(qdict, "driver");
  666. BlockdevOptions *options = NULL;
  667. char *node_name;
  668. Visitor *v;
  669. if (!driver) {
  670. error_setg(errp, "no 'driver' parameter");
  671. return NULL;
  672. }
  673. node_name = g_strdup_printf("%s-%s", id, driver);
  674. qdict_put_str(qdict, "node-name", node_name);
  675. trace_xen_block_blockdev_add(node_name);
  676. v = qobject_input_visitor_new(QOBJECT(qdict));
  677. visit_type_BlockdevOptions(v, NULL, &options, errp);
  678. visit_free(v);
  679. if (!options) {
  680. goto fail;
  681. }
  682. qmp_blockdev_add(options, errp);
  683. if (*errp) {
  684. goto fail;
  685. }
  686. qapi_free_BlockdevOptions(options);
  687. return node_name;
  688. fail:
  689. if (options) {
  690. qapi_free_BlockdevOptions(options);
  691. }
  692. g_free(node_name);
  693. return NULL;
  694. }
  695. static void xen_block_drive_destroy(XenBlockDrive *drive, Error **errp)
  696. {
  697. ERRP_GUARD();
  698. char *node_name = drive->node_name;
  699. if (node_name) {
  700. xen_block_blockdev_del(node_name, errp);
  701. if (*errp) {
  702. return;
  703. }
  704. g_free(node_name);
  705. drive->node_name = NULL;
  706. }
  707. g_free(drive->id);
  708. g_free(drive);
  709. }
  710. static XenBlockDrive *xen_block_drive_create(const char *id,
  711. const char *device_type,
  712. QDict *opts, Error **errp)
  713. {
  714. ERRP_GUARD();
  715. const char *params = qdict_get_try_str(opts, "params");
  716. const char *mode = qdict_get_try_str(opts, "mode");
  717. const char *direct_io_safe = qdict_get_try_str(opts, "direct-io-safe");
  718. const char *discard_enable = qdict_get_try_str(opts, "discard-enable");
  719. char *driver = NULL;
  720. char *filename = NULL;
  721. XenBlockDrive *drive = NULL;
  722. QDict *file_layer;
  723. QDict *driver_layer;
  724. struct stat st;
  725. int rc;
  726. if (params) {
  727. char **v = g_strsplit(params, ":", 2);
  728. if (v[1] == NULL) {
  729. filename = g_strdup(v[0]);
  730. driver = g_strdup("raw");
  731. } else {
  732. if (strcmp(v[0], "aio") == 0) {
  733. driver = g_strdup("raw");
  734. } else if (strcmp(v[0], "vhd") == 0) {
  735. driver = g_strdup("vpc");
  736. } else {
  737. driver = g_strdup(v[0]);
  738. }
  739. filename = g_strdup(v[1]);
  740. }
  741. g_strfreev(v);
  742. } else {
  743. error_setg(errp, "no params");
  744. goto done;
  745. }
  746. assert(filename);
  747. assert(driver);
  748. drive = g_new0(XenBlockDrive, 1);
  749. drive->id = g_strdup(id);
  750. rc = stat(filename, &st);
  751. if (rc) {
  752. error_setg_errno(errp, errno, "Could not stat file '%s'", filename);
  753. goto done;
  754. }
  755. file_layer = qdict_new();
  756. driver_layer = qdict_new();
  757. if (S_ISBLK(st.st_mode)) {
  758. qdict_put_str(file_layer, "driver", "host_device");
  759. } else {
  760. qdict_put_str(file_layer, "driver", "file");
  761. }
  762. qdict_put_str(file_layer, "filename", filename);
  763. if (mode && *mode != 'w') {
  764. qdict_put_bool(file_layer, "read-only", true);
  765. }
  766. if (direct_io_safe) {
  767. unsigned long value;
  768. if (!qemu_strtoul(direct_io_safe, NULL, 2, &value) && !!value) {
  769. QDict *cache_qdict = qdict_new();
  770. qdict_put_bool(cache_qdict, "direct", true);
  771. qdict_put(file_layer, "cache", cache_qdict);
  772. qdict_put_str(file_layer, "aio", "native");
  773. }
  774. }
  775. if (discard_enable) {
  776. unsigned long value;
  777. if (!qemu_strtoul(discard_enable, NULL, 2, &value) && !!value) {
  778. qdict_put_str(file_layer, "discard", "unmap");
  779. qdict_put_str(driver_layer, "discard", "unmap");
  780. }
  781. }
  782. /*
  783. * It is necessary to turn file locking off as an emulated device
  784. * may have already opened the same image file.
  785. */
  786. qdict_put_str(file_layer, "locking", "off");
  787. qdict_put_str(driver_layer, "driver", driver);
  788. qdict_put(driver_layer, "file", file_layer);
  789. g_assert(!drive->node_name);
  790. drive->node_name = xen_block_blockdev_add(drive->id, driver_layer,
  791. errp);
  792. qobject_unref(driver_layer);
  793. done:
  794. g_free(filename);
  795. g_free(driver);
  796. if (*errp) {
  797. xen_block_drive_destroy(drive, NULL);
  798. return NULL;
  799. }
  800. return drive;
  801. }
  802. static const char *xen_block_drive_get_node_name(XenBlockDrive *drive)
  803. {
  804. return drive->node_name ? drive->node_name : "";
  805. }
  806. static void xen_block_iothread_destroy(XenBlockIOThread *iothread,
  807. Error **errp)
  808. {
  809. qmp_object_del(iothread->id, errp);
  810. g_free(iothread->id);
  811. g_free(iothread);
  812. }
  813. static XenBlockIOThread *xen_block_iothread_create(const char *id,
  814. Error **errp)
  815. {
  816. ERRP_GUARD();
  817. XenBlockIOThread *iothread = g_new(XenBlockIOThread, 1);
  818. ObjectOptions *opts;
  819. iothread->id = g_strdup(id);
  820. opts = g_new(ObjectOptions, 1);
  821. *opts = (ObjectOptions) {
  822. .qom_type = OBJECT_TYPE_IOTHREAD,
  823. .id = g_strdup(id),
  824. };
  825. qmp_object_add(opts, errp);
  826. qapi_free_ObjectOptions(opts);
  827. if (*errp) {
  828. g_free(iothread->id);
  829. g_free(iothread);
  830. return NULL;
  831. }
  832. return iothread;
  833. }
  834. static void xen_block_device_create(XenBackendInstance *backend,
  835. QDict *opts, Error **errp)
  836. {
  837. ERRP_GUARD();
  838. XenBus *xenbus = xen_backend_get_bus(backend);
  839. const char *name = xen_backend_get_name(backend);
  840. unsigned long number;
  841. const char *vdev, *device_type;
  842. XenBlockDrive *drive = NULL;
  843. XenBlockIOThread *iothread = NULL;
  844. XenDevice *xendev = NULL;
  845. const char *type;
  846. XenBlockDevice *blockdev;
  847. if (qemu_strtoul(name, NULL, 10, &number)) {
  848. error_setg(errp, "failed to parse name '%s'", name);
  849. goto fail;
  850. }
  851. trace_xen_block_device_create(number);
  852. vdev = qdict_get_try_str(opts, "dev");
  853. if (!vdev) {
  854. error_setg(errp, "no dev parameter");
  855. goto fail;
  856. }
  857. device_type = qdict_get_try_str(opts, "device-type");
  858. if (!device_type) {
  859. error_setg(errp, "no device-type parameter");
  860. goto fail;
  861. }
  862. if (!strcmp(device_type, "disk")) {
  863. type = TYPE_XEN_DISK_DEVICE;
  864. } else if (!strcmp(device_type, "cdrom")) {
  865. type = TYPE_XEN_CDROM_DEVICE;
  866. } else {
  867. error_setg(errp, "invalid device-type parameter '%s'", device_type);
  868. goto fail;
  869. }
  870. drive = xen_block_drive_create(vdev, device_type, opts, errp);
  871. if (!drive) {
  872. error_prepend(errp, "failed to create drive: ");
  873. goto fail;
  874. }
  875. iothread = xen_block_iothread_create(vdev, errp);
  876. if (*errp) {
  877. error_prepend(errp, "failed to create iothread: ");
  878. goto fail;
  879. }
  880. xendev = XEN_DEVICE(qdev_new(type));
  881. blockdev = XEN_BLOCK_DEVICE(xendev);
  882. if (!object_property_set_str(OBJECT(xendev), "vdev", vdev,
  883. errp)) {
  884. error_prepend(errp, "failed to set 'vdev': ");
  885. goto fail;
  886. }
  887. if (!object_property_set_str(OBJECT(xendev), "drive",
  888. xen_block_drive_get_node_name(drive),
  889. errp)) {
  890. error_prepend(errp, "failed to set 'drive': ");
  891. goto fail;
  892. }
  893. if (!object_property_set_str(OBJECT(xendev), "iothread", iothread->id,
  894. errp)) {
  895. error_prepend(errp, "failed to set 'iothread': ");
  896. goto fail;
  897. }
  898. blockdev->iothread = iothread;
  899. blockdev->drive = drive;
  900. if (!qdev_realize_and_unref(DEVICE(xendev), BUS(xenbus), errp)) {
  901. error_prepend(errp, "realization of device %s failed: ", type);
  902. goto fail;
  903. }
  904. xen_backend_set_device(backend, xendev);
  905. return;
  906. fail:
  907. if (xendev) {
  908. object_unparent(OBJECT(xendev));
  909. }
  910. if (iothread) {
  911. xen_block_iothread_destroy(iothread, NULL);
  912. }
  913. if (drive) {
  914. xen_block_drive_destroy(drive, NULL);
  915. }
  916. }
  917. static void xen_block_device_destroy(XenBackendInstance *backend,
  918. Error **errp)
  919. {
  920. ERRP_GUARD();
  921. XenDevice *xendev = xen_backend_get_device(backend);
  922. XenBlockDevice *blockdev = XEN_BLOCK_DEVICE(xendev);
  923. XenBlockVdev *vdev = &blockdev->props.vdev;
  924. XenBlockDrive *drive = blockdev->drive;
  925. XenBlockIOThread *iothread = blockdev->iothread;
  926. trace_xen_block_device_destroy(vdev->number);
  927. object_unparent(OBJECT(xendev));
  928. /*
  929. * Drain all pending RCU callbacks as object_unparent() frees `xendev'
  930. * in a RCU callback.
  931. * And due to the property "drive" still existing in `xendev', we
  932. * can't destroy the XenBlockDrive associated with `xendev' with
  933. * xen_block_drive_destroy() below.
  934. */
  935. drain_call_rcu();
  936. if (iothread) {
  937. xen_block_iothread_destroy(iothread, errp);
  938. if (*errp) {
  939. error_prepend(errp, "failed to destroy iothread: ");
  940. return;
  941. }
  942. }
  943. if (drive) {
  944. xen_block_drive_destroy(drive, errp);
  945. if (*errp) {
  946. error_prepend(errp, "failed to destroy drive: ");
  947. return;
  948. }
  949. }
  950. }
  951. static const XenBackendInfo xen_block_backend_info = {
  952. .type = "qdisk",
  953. .create = xen_block_device_create,
  954. .destroy = xen_block_device_destroy,
  955. };
  956. static void xen_block_register_backend(void)
  957. {
  958. xen_backend_register(&xen_block_backend_info);
  959. }
  960. xen_backend_init(xen_block_register_backend);