qcow.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  1. /*
  2. * Block driver for the QCOW format
  3. *
  4. * Copyright (c) 2004-2006 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qapi/error.h"
  26. #include "qemu/error-report.h"
  27. #include "block/block_int.h"
  28. #include "block/qdict.h"
  29. #include "system/block-backend.h"
  30. #include "qemu/module.h"
  31. #include "qemu/option.h"
  32. #include "qemu/bswap.h"
  33. #include "qemu/cutils.h"
  34. #include "qemu/memalign.h"
  35. #include <zlib.h>
  36. #include "qobject/qdict.h"
  37. #include "qobject/qstring.h"
  38. #include "qapi/qobject-input-visitor.h"
  39. #include "qapi/qapi-visit-block-core.h"
  40. #include "crypto/block.h"
  41. #include "migration/blocker.h"
  42. #include "crypto.h"
  43. /**************************************************************/
  44. /* QEMU COW block driver with compression and encryption support */
  45. #define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
  46. #define QCOW_VERSION 1
  47. #define QCOW_CRYPT_NONE 0
  48. #define QCOW_CRYPT_AES 1
  49. #define QCOW_OFLAG_COMPRESSED (1LL << 63)
  50. typedef struct QCowHeader {
  51. uint32_t magic;
  52. uint32_t version;
  53. uint64_t backing_file_offset;
  54. uint32_t backing_file_size;
  55. uint32_t mtime;
  56. uint64_t size; /* in bytes */
  57. uint8_t cluster_bits;
  58. uint8_t l2_bits;
  59. uint16_t padding;
  60. uint32_t crypt_method;
  61. uint64_t l1_table_offset;
  62. } QEMU_PACKED QCowHeader;
  63. #define L2_CACHE_SIZE 16
  64. typedef struct BDRVQcowState {
  65. int cluster_bits;
  66. int cluster_size;
  67. int l2_bits;
  68. int l2_size;
  69. unsigned int l1_size;
  70. uint64_t cluster_offset_mask;
  71. uint64_t l1_table_offset;
  72. uint64_t *l1_table;
  73. uint64_t *l2_cache;
  74. uint64_t l2_cache_offsets[L2_CACHE_SIZE];
  75. uint32_t l2_cache_counts[L2_CACHE_SIZE];
  76. uint8_t *cluster_cache;
  77. uint8_t *cluster_data;
  78. uint64_t cluster_cache_offset;
  79. QCryptoBlock *crypto; /* Disk encryption format driver */
  80. uint32_t crypt_method_header;
  81. CoMutex lock;
  82. Error *migration_blocker;
  83. } BDRVQcowState;
  84. static QemuOptsList qcow_create_opts;
  85. static int coroutine_fn GRAPH_RDLOCK
  86. decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset);
  87. static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename)
  88. {
  89. const QCowHeader *cow_header = (const void *)buf;
  90. if (buf_size >= sizeof(QCowHeader) &&
  91. be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
  92. be32_to_cpu(cow_header->version) == QCOW_VERSION)
  93. return 100;
  94. else
  95. return 0;
  96. }
  97. static int qcow_open(BlockDriverState *bs, QDict *options, int flags,
  98. Error **errp)
  99. {
  100. BDRVQcowState *s = bs->opaque;
  101. unsigned int len, i, shift;
  102. int ret;
  103. QCowHeader header;
  104. QCryptoBlockOpenOptions *crypto_opts = NULL;
  105. unsigned int cflags = 0;
  106. QDict *encryptopts = NULL;
  107. const char *encryptfmt;
  108. qdict_extract_subqdict(options, &encryptopts, "encrypt.");
  109. encryptfmt = qdict_get_try_str(encryptopts, "format");
  110. ret = bdrv_open_file_child(NULL, options, "file", bs, errp);
  111. if (ret < 0) {
  112. goto fail_unlocked;
  113. }
  114. bdrv_graph_rdlock_main_loop();
  115. ret = bdrv_pread(bs->file, 0, sizeof(header), &header, 0);
  116. if (ret < 0) {
  117. goto fail;
  118. }
  119. header.magic = be32_to_cpu(header.magic);
  120. header.version = be32_to_cpu(header.version);
  121. header.backing_file_offset = be64_to_cpu(header.backing_file_offset);
  122. header.backing_file_size = be32_to_cpu(header.backing_file_size);
  123. header.mtime = be32_to_cpu(header.mtime);
  124. header.size = be64_to_cpu(header.size);
  125. header.crypt_method = be32_to_cpu(header.crypt_method);
  126. header.l1_table_offset = be64_to_cpu(header.l1_table_offset);
  127. if (header.magic != QCOW_MAGIC) {
  128. error_setg(errp, "Image not in qcow format");
  129. ret = -EINVAL;
  130. goto fail;
  131. }
  132. if (header.version != QCOW_VERSION) {
  133. error_setg(errp, "qcow (v%d) does not support qcow version %" PRIu32,
  134. QCOW_VERSION, header.version);
  135. if (header.version == 2 || header.version == 3) {
  136. error_append_hint(errp, "Try the 'qcow2' driver instead.\n");
  137. }
  138. ret = -ENOTSUP;
  139. goto fail;
  140. }
  141. if (header.size <= 1) {
  142. error_setg(errp, "Image size is too small (must be at least 2 bytes)");
  143. ret = -EINVAL;
  144. goto fail;
  145. }
  146. if (header.cluster_bits < 9 || header.cluster_bits > 16) {
  147. error_setg(errp, "Cluster size must be between 512 and 64k");
  148. ret = -EINVAL;
  149. goto fail;
  150. }
  151. /* l2_bits specifies number of entries; storing a uint64_t in each entry,
  152. * so bytes = num_entries << 3. */
  153. if (header.l2_bits < 9 - 3 || header.l2_bits > 16 - 3) {
  154. error_setg(errp, "L2 table size must be between 512 and 64k");
  155. ret = -EINVAL;
  156. goto fail;
  157. }
  158. s->crypt_method_header = header.crypt_method;
  159. if (s->crypt_method_header) {
  160. if (bdrv_uses_whitelist() &&
  161. s->crypt_method_header == QCOW_CRYPT_AES) {
  162. error_setg(errp,
  163. "Use of AES-CBC encrypted qcow images is no longer "
  164. "supported in system emulators");
  165. error_append_hint(errp,
  166. "You can use 'qemu-img convert' to convert your "
  167. "image to an alternative supported format, such "
  168. "as unencrypted qcow, or raw with the LUKS "
  169. "format instead.\n");
  170. ret = -ENOSYS;
  171. goto fail;
  172. }
  173. if (s->crypt_method_header == QCOW_CRYPT_AES) {
  174. if (encryptfmt && !g_str_equal(encryptfmt, "aes")) {
  175. error_setg(errp,
  176. "Header reported 'aes' encryption format but "
  177. "options specify '%s'", encryptfmt);
  178. ret = -EINVAL;
  179. goto fail;
  180. }
  181. qdict_put_str(encryptopts, "format", "qcow");
  182. crypto_opts = block_crypto_open_opts_init(encryptopts, errp);
  183. if (!crypto_opts) {
  184. ret = -EINVAL;
  185. goto fail;
  186. }
  187. if (flags & BDRV_O_NO_IO) {
  188. cflags |= QCRYPTO_BLOCK_OPEN_NO_IO;
  189. }
  190. s->crypto = qcrypto_block_open(crypto_opts, "encrypt.",
  191. NULL, NULL, cflags, errp);
  192. if (!s->crypto) {
  193. ret = -EINVAL;
  194. goto fail;
  195. }
  196. } else {
  197. error_setg(errp, "invalid encryption method in qcow header");
  198. ret = -EINVAL;
  199. goto fail;
  200. }
  201. bs->encrypted = true;
  202. } else {
  203. if (encryptfmt) {
  204. error_setg(errp, "No encryption in image header, but options "
  205. "specified format '%s'", encryptfmt);
  206. ret = -EINVAL;
  207. goto fail;
  208. }
  209. }
  210. s->cluster_bits = header.cluster_bits;
  211. s->cluster_size = 1 << s->cluster_bits;
  212. s->l2_bits = header.l2_bits;
  213. s->l2_size = 1 << s->l2_bits;
  214. bs->total_sectors = header.size / 512;
  215. s->cluster_offset_mask = (1LL << (63 - s->cluster_bits)) - 1;
  216. /* read the level 1 table */
  217. shift = s->cluster_bits + s->l2_bits;
  218. if (header.size > UINT64_MAX - (1LL << shift)) {
  219. error_setg(errp, "Image too large");
  220. ret = -EINVAL;
  221. goto fail;
  222. } else {
  223. uint64_t l1_size = (header.size + (1LL << shift) - 1) >> shift;
  224. if (l1_size > INT_MAX / sizeof(uint64_t)) {
  225. error_setg(errp, "Image too large");
  226. ret = -EINVAL;
  227. goto fail;
  228. }
  229. s->l1_size = l1_size;
  230. }
  231. s->l1_table_offset = header.l1_table_offset;
  232. s->l1_table = g_try_new(uint64_t, s->l1_size);
  233. if (s->l1_table == NULL) {
  234. error_setg(errp, "Could not allocate memory for L1 table");
  235. ret = -ENOMEM;
  236. goto fail;
  237. }
  238. ret = bdrv_pread(bs->file, s->l1_table_offset,
  239. s->l1_size * sizeof(uint64_t), s->l1_table, 0);
  240. if (ret < 0) {
  241. goto fail;
  242. }
  243. for(i = 0;i < s->l1_size; i++) {
  244. s->l1_table[i] = be64_to_cpu(s->l1_table[i]);
  245. }
  246. /* alloc L2 cache (max. 64k * 16 * 8 = 8 MB) */
  247. s->l2_cache =
  248. qemu_try_blockalign(bs->file->bs,
  249. s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
  250. if (s->l2_cache == NULL) {
  251. error_setg(errp, "Could not allocate L2 table cache");
  252. ret = -ENOMEM;
  253. goto fail;
  254. }
  255. s->cluster_cache = g_malloc(s->cluster_size);
  256. s->cluster_data = g_malloc(s->cluster_size);
  257. s->cluster_cache_offset = -1;
  258. /* read the backing file name */
  259. if (header.backing_file_offset != 0) {
  260. len = header.backing_file_size;
  261. if (len > 1023 || len >= sizeof(bs->backing_file)) {
  262. error_setg(errp, "Backing file name too long");
  263. ret = -EINVAL;
  264. goto fail;
  265. }
  266. ret = bdrv_pread(bs->file, header.backing_file_offset, len,
  267. bs->auto_backing_file, 0);
  268. if (ret < 0) {
  269. goto fail;
  270. }
  271. bs->auto_backing_file[len] = '\0';
  272. pstrcpy(bs->backing_file, sizeof(bs->backing_file),
  273. bs->auto_backing_file);
  274. }
  275. /* Disable migration when qcow images are used */
  276. error_setg(&s->migration_blocker, "The qcow format used by node '%s' "
  277. "does not support live migration",
  278. bdrv_get_device_or_node_name(bs));
  279. ret = migrate_add_blocker_normal(&s->migration_blocker, errp);
  280. if (ret < 0) {
  281. goto fail;
  282. }
  283. qobject_unref(encryptopts);
  284. qapi_free_QCryptoBlockOpenOptions(crypto_opts);
  285. qemu_co_mutex_init(&s->lock);
  286. bdrv_graph_rdunlock_main_loop();
  287. return 0;
  288. fail:
  289. bdrv_graph_rdunlock_main_loop();
  290. fail_unlocked:
  291. g_free(s->l1_table);
  292. qemu_vfree(s->l2_cache);
  293. g_free(s->cluster_cache);
  294. g_free(s->cluster_data);
  295. qcrypto_block_free(s->crypto);
  296. qobject_unref(encryptopts);
  297. qapi_free_QCryptoBlockOpenOptions(crypto_opts);
  298. return ret;
  299. }
  300. /* We have nothing to do for QCOW reopen, stubs just return
  301. * success */
  302. static int qcow_reopen_prepare(BDRVReopenState *state,
  303. BlockReopenQueue *queue, Error **errp)
  304. {
  305. return 0;
  306. }
  307. /* 'allocate' is:
  308. *
  309. * 0 to not allocate.
  310. *
  311. * 1 to allocate a normal cluster (for sector-aligned byte offsets 'n_start'
  312. * to 'n_end' within the cluster)
  313. *
  314. * 2 to allocate a compressed cluster of size
  315. * 'compressed_size'. 'compressed_size' must be > 0 and <
  316. * cluster_size
  317. *
  318. * return 0 if not allocated, 1 if *result is assigned, and negative
  319. * errno on failure.
  320. */
  321. static int coroutine_fn GRAPH_RDLOCK
  322. get_cluster_offset(BlockDriverState *bs, uint64_t offset, int allocate,
  323. int compressed_size, int n_start, int n_end,
  324. uint64_t *result)
  325. {
  326. BDRVQcowState *s = bs->opaque;
  327. int min_index, i, j, l1_index, l2_index, ret;
  328. int64_t l2_offset;
  329. uint64_t *l2_table, cluster_offset, tmp;
  330. uint32_t min_count;
  331. int new_l2_table;
  332. *result = 0;
  333. l1_index = offset >> (s->l2_bits + s->cluster_bits);
  334. l2_offset = s->l1_table[l1_index];
  335. new_l2_table = 0;
  336. if (!l2_offset) {
  337. if (!allocate)
  338. return 0;
  339. /* allocate a new l2 entry */
  340. l2_offset = bdrv_co_getlength(bs->file->bs);
  341. if (l2_offset < 0) {
  342. return l2_offset;
  343. }
  344. /* round to cluster size */
  345. l2_offset = QEMU_ALIGN_UP(l2_offset, s->cluster_size);
  346. /* update the L1 entry */
  347. s->l1_table[l1_index] = l2_offset;
  348. tmp = cpu_to_be64(l2_offset);
  349. BLKDBG_CO_EVENT(bs->file, BLKDBG_L1_UPDATE);
  350. ret = bdrv_co_pwrite_sync(bs->file,
  351. s->l1_table_offset + l1_index * sizeof(tmp),
  352. sizeof(tmp), &tmp, 0);
  353. if (ret < 0) {
  354. return ret;
  355. }
  356. new_l2_table = 1;
  357. }
  358. for(i = 0; i < L2_CACHE_SIZE; i++) {
  359. if (l2_offset == s->l2_cache_offsets[i]) {
  360. /* increment the hit count */
  361. if (++s->l2_cache_counts[i] == 0xffffffff) {
  362. for(j = 0; j < L2_CACHE_SIZE; j++) {
  363. s->l2_cache_counts[j] >>= 1;
  364. }
  365. }
  366. l2_table = s->l2_cache + (i << s->l2_bits);
  367. goto found;
  368. }
  369. }
  370. /* not found: load a new entry in the least used one */
  371. min_index = 0;
  372. min_count = 0xffffffff;
  373. for(i = 0; i < L2_CACHE_SIZE; i++) {
  374. if (s->l2_cache_counts[i] < min_count) {
  375. min_count = s->l2_cache_counts[i];
  376. min_index = i;
  377. }
  378. }
  379. l2_table = s->l2_cache + (min_index << s->l2_bits);
  380. BLKDBG_CO_EVENT(bs->file, BLKDBG_L2_LOAD);
  381. if (new_l2_table) {
  382. memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
  383. ret = bdrv_co_pwrite_sync(bs->file, l2_offset,
  384. s->l2_size * sizeof(uint64_t), l2_table, 0);
  385. if (ret < 0) {
  386. return ret;
  387. }
  388. } else {
  389. ret = bdrv_co_pread(bs->file, l2_offset,
  390. s->l2_size * sizeof(uint64_t), l2_table, 0);
  391. if (ret < 0) {
  392. return ret;
  393. }
  394. }
  395. s->l2_cache_offsets[min_index] = l2_offset;
  396. s->l2_cache_counts[min_index] = 1;
  397. found:
  398. l2_index = (offset >> s->cluster_bits) & (s->l2_size - 1);
  399. cluster_offset = be64_to_cpu(l2_table[l2_index]);
  400. if (!cluster_offset ||
  401. ((cluster_offset & QCOW_OFLAG_COMPRESSED) && allocate == 1)) {
  402. if (!allocate)
  403. return 0;
  404. BLKDBG_CO_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC);
  405. assert(QEMU_IS_ALIGNED(n_start | n_end, BDRV_SECTOR_SIZE));
  406. /* allocate a new cluster */
  407. if ((cluster_offset & QCOW_OFLAG_COMPRESSED) &&
  408. (n_end - n_start) < s->cluster_size) {
  409. /* if the cluster is already compressed, we must
  410. decompress it in the case it is not completely
  411. overwritten */
  412. if (decompress_cluster(bs, cluster_offset) < 0) {
  413. return -EIO;
  414. }
  415. cluster_offset = bdrv_co_getlength(bs->file->bs);
  416. if ((int64_t) cluster_offset < 0) {
  417. return cluster_offset;
  418. }
  419. cluster_offset = QEMU_ALIGN_UP(cluster_offset, s->cluster_size);
  420. /* write the cluster content */
  421. BLKDBG_CO_EVENT(bs->file, BLKDBG_WRITE_AIO);
  422. ret = bdrv_co_pwrite(bs->file, cluster_offset, s->cluster_size,
  423. s->cluster_cache, 0);
  424. if (ret < 0) {
  425. return ret;
  426. }
  427. } else {
  428. cluster_offset = bdrv_co_getlength(bs->file->bs);
  429. if ((int64_t) cluster_offset < 0) {
  430. return cluster_offset;
  431. }
  432. if (allocate == 1) {
  433. /* round to cluster size */
  434. cluster_offset = QEMU_ALIGN_UP(cluster_offset, s->cluster_size);
  435. if (cluster_offset + s->cluster_size > INT64_MAX) {
  436. return -E2BIG;
  437. }
  438. ret = bdrv_co_truncate(bs->file,
  439. cluster_offset + s->cluster_size,
  440. false, PREALLOC_MODE_OFF, 0, NULL);
  441. if (ret < 0) {
  442. return ret;
  443. }
  444. /* if encrypted, we must initialize the cluster
  445. content which won't be written */
  446. if (bs->encrypted &&
  447. (n_end - n_start) < s->cluster_size) {
  448. uint64_t start_offset;
  449. assert(s->crypto);
  450. start_offset = offset & ~(s->cluster_size - 1);
  451. for (i = 0; i < s->cluster_size; i += BDRV_SECTOR_SIZE) {
  452. if (i < n_start || i >= n_end) {
  453. memset(s->cluster_data, 0x00, BDRV_SECTOR_SIZE);
  454. if (qcrypto_block_encrypt(s->crypto,
  455. start_offset + i,
  456. s->cluster_data,
  457. BDRV_SECTOR_SIZE,
  458. NULL) < 0) {
  459. return -EIO;
  460. }
  461. BLKDBG_CO_EVENT(bs->file, BLKDBG_WRITE_AIO);
  462. ret = bdrv_co_pwrite(bs->file, cluster_offset + i,
  463. BDRV_SECTOR_SIZE,
  464. s->cluster_data, 0);
  465. if (ret < 0) {
  466. return ret;
  467. }
  468. }
  469. }
  470. }
  471. } else if (allocate == 2) {
  472. cluster_offset |= QCOW_OFLAG_COMPRESSED |
  473. (uint64_t)compressed_size << (63 - s->cluster_bits);
  474. }
  475. }
  476. /* update L2 table */
  477. tmp = cpu_to_be64(cluster_offset);
  478. l2_table[l2_index] = tmp;
  479. if (allocate == 2) {
  480. BLKDBG_CO_EVENT(bs->file, BLKDBG_L2_UPDATE_COMPRESSED);
  481. } else {
  482. BLKDBG_CO_EVENT(bs->file, BLKDBG_L2_UPDATE);
  483. }
  484. ret = bdrv_co_pwrite_sync(bs->file, l2_offset + l2_index * sizeof(tmp),
  485. sizeof(tmp), &tmp, 0);
  486. if (ret < 0) {
  487. return ret;
  488. }
  489. }
  490. *result = cluster_offset;
  491. return 1;
  492. }
  493. static int coroutine_fn GRAPH_RDLOCK
  494. qcow_co_block_status(BlockDriverState *bs, bool want_zero,
  495. int64_t offset, int64_t bytes, int64_t *pnum,
  496. int64_t *map, BlockDriverState **file)
  497. {
  498. BDRVQcowState *s = bs->opaque;
  499. int index_in_cluster, ret;
  500. int64_t n;
  501. uint64_t cluster_offset;
  502. qemu_co_mutex_lock(&s->lock);
  503. ret = get_cluster_offset(bs, offset, 0, 0, 0, 0, &cluster_offset);
  504. qemu_co_mutex_unlock(&s->lock);
  505. if (ret < 0) {
  506. return ret;
  507. }
  508. index_in_cluster = offset & (s->cluster_size - 1);
  509. n = s->cluster_size - index_in_cluster;
  510. if (n > bytes) {
  511. n = bytes;
  512. }
  513. *pnum = n;
  514. if (!cluster_offset) {
  515. return 0;
  516. }
  517. if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
  518. return BDRV_BLOCK_DATA | BDRV_BLOCK_COMPRESSED;
  519. }
  520. if (s->crypto) {
  521. return BDRV_BLOCK_DATA;
  522. }
  523. *map = cluster_offset | index_in_cluster;
  524. *file = bs->file->bs;
  525. return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
  526. }
  527. static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
  528. const uint8_t *buf, int buf_size)
  529. {
  530. z_stream strm1, *strm = &strm1;
  531. int ret, out_len;
  532. memset(strm, 0, sizeof(*strm));
  533. strm->next_in = (uint8_t *)buf;
  534. strm->avail_in = buf_size;
  535. strm->next_out = out_buf;
  536. strm->avail_out = out_buf_size;
  537. ret = inflateInit2(strm, -12);
  538. if (ret != Z_OK)
  539. return -1;
  540. ret = inflate(strm, Z_FINISH);
  541. out_len = strm->next_out - out_buf;
  542. if ((ret != Z_STREAM_END && ret != Z_BUF_ERROR) ||
  543. out_len != out_buf_size) {
  544. inflateEnd(strm);
  545. return -1;
  546. }
  547. inflateEnd(strm);
  548. return 0;
  549. }
  550. static int coroutine_fn GRAPH_RDLOCK
  551. decompress_cluster(BlockDriverState *bs, uint64_t cluster_offset)
  552. {
  553. BDRVQcowState *s = bs->opaque;
  554. int ret, csize;
  555. uint64_t coffset;
  556. coffset = cluster_offset & s->cluster_offset_mask;
  557. if (s->cluster_cache_offset != coffset) {
  558. csize = cluster_offset >> (63 - s->cluster_bits);
  559. csize &= (s->cluster_size - 1);
  560. BLKDBG_CO_EVENT(bs->file, BLKDBG_READ_COMPRESSED);
  561. ret = bdrv_co_pread(bs->file, coffset, csize, s->cluster_data, 0);
  562. if (ret < 0)
  563. return -1;
  564. if (decompress_buffer(s->cluster_cache, s->cluster_size,
  565. s->cluster_data, csize) < 0) {
  566. return -1;
  567. }
  568. s->cluster_cache_offset = coffset;
  569. }
  570. return 0;
  571. }
  572. static void qcow_refresh_limits(BlockDriverState *bs, Error **errp)
  573. {
  574. /* At least encrypted images require 512-byte alignment. Apply the
  575. * limit universally, rather than just on encrypted images, as
  576. * it's easier to let the block layer handle rounding than to
  577. * audit this code further. */
  578. bs->bl.request_alignment = BDRV_SECTOR_SIZE;
  579. }
  580. static int coroutine_fn GRAPH_RDLOCK
  581. qcow_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
  582. QEMUIOVector *qiov, BdrvRequestFlags flags)
  583. {
  584. BDRVQcowState *s = bs->opaque;
  585. int offset_in_cluster;
  586. int ret = 0, n;
  587. uint64_t cluster_offset;
  588. uint8_t *buf;
  589. void *orig_buf;
  590. if (qiov->niov > 1) {
  591. buf = orig_buf = qemu_try_blockalign(bs, qiov->size);
  592. if (buf == NULL) {
  593. return -ENOMEM;
  594. }
  595. } else {
  596. orig_buf = NULL;
  597. buf = (uint8_t *)qiov->iov->iov_base;
  598. }
  599. qemu_co_mutex_lock(&s->lock);
  600. while (bytes != 0) {
  601. /* prepare next request */
  602. ret = get_cluster_offset(bs, offset, 0, 0, 0, 0, &cluster_offset);
  603. if (ret < 0) {
  604. break;
  605. }
  606. offset_in_cluster = offset & (s->cluster_size - 1);
  607. n = s->cluster_size - offset_in_cluster;
  608. if (n > bytes) {
  609. n = bytes;
  610. }
  611. if (!cluster_offset) {
  612. if (bs->backing) {
  613. /* read from the base image */
  614. qemu_co_mutex_unlock(&s->lock);
  615. /* qcow2 emits this on bs->file instead of bs->backing */
  616. BLKDBG_CO_EVENT(bs->file, BLKDBG_READ_BACKING_AIO);
  617. ret = bdrv_co_pread(bs->backing, offset, n, buf, 0);
  618. qemu_co_mutex_lock(&s->lock);
  619. if (ret < 0) {
  620. break;
  621. }
  622. } else {
  623. /* Note: in this case, no need to wait */
  624. memset(buf, 0, n);
  625. }
  626. } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
  627. /* add AIO support for compressed blocks ? */
  628. if (decompress_cluster(bs, cluster_offset) < 0) {
  629. ret = -EIO;
  630. break;
  631. }
  632. memcpy(buf, s->cluster_cache + offset_in_cluster, n);
  633. } else {
  634. if ((cluster_offset & 511) != 0) {
  635. ret = -EIO;
  636. break;
  637. }
  638. qemu_co_mutex_unlock(&s->lock);
  639. BLKDBG_CO_EVENT(bs->file, BLKDBG_READ_AIO);
  640. ret = bdrv_co_pread(bs->file, cluster_offset + offset_in_cluster,
  641. n, buf, 0);
  642. qemu_co_mutex_lock(&s->lock);
  643. if (ret < 0) {
  644. break;
  645. }
  646. if (bs->encrypted) {
  647. assert(s->crypto);
  648. if (qcrypto_block_decrypt(s->crypto,
  649. offset, buf, n, NULL) < 0) {
  650. ret = -EIO;
  651. break;
  652. }
  653. }
  654. }
  655. ret = 0;
  656. bytes -= n;
  657. offset += n;
  658. buf += n;
  659. }
  660. qemu_co_mutex_unlock(&s->lock);
  661. if (qiov->niov > 1) {
  662. qemu_iovec_from_buf(qiov, 0, orig_buf, qiov->size);
  663. qemu_vfree(orig_buf);
  664. }
  665. return ret;
  666. }
  667. static int coroutine_fn GRAPH_RDLOCK
  668. qcow_co_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes,
  669. QEMUIOVector *qiov, BdrvRequestFlags flags)
  670. {
  671. BDRVQcowState *s = bs->opaque;
  672. int offset_in_cluster;
  673. uint64_t cluster_offset;
  674. int ret = 0, n;
  675. uint8_t *buf;
  676. void *orig_buf;
  677. s->cluster_cache_offset = -1; /* disable compressed cache */
  678. /* We must always copy the iov when encrypting, so we
  679. * don't modify the original data buffer during encryption */
  680. if (bs->encrypted || qiov->niov > 1) {
  681. buf = orig_buf = qemu_try_blockalign(bs, qiov->size);
  682. if (buf == NULL) {
  683. return -ENOMEM;
  684. }
  685. qemu_iovec_to_buf(qiov, 0, buf, qiov->size);
  686. } else {
  687. orig_buf = NULL;
  688. buf = (uint8_t *)qiov->iov->iov_base;
  689. }
  690. qemu_co_mutex_lock(&s->lock);
  691. while (bytes != 0) {
  692. offset_in_cluster = offset & (s->cluster_size - 1);
  693. n = s->cluster_size - offset_in_cluster;
  694. if (n > bytes) {
  695. n = bytes;
  696. }
  697. ret = get_cluster_offset(bs, offset, 1, 0, offset_in_cluster,
  698. offset_in_cluster + n, &cluster_offset);
  699. if (ret < 0) {
  700. break;
  701. }
  702. if (!cluster_offset || (cluster_offset & 511) != 0) {
  703. ret = -EIO;
  704. break;
  705. }
  706. if (bs->encrypted) {
  707. assert(s->crypto);
  708. if (qcrypto_block_encrypt(s->crypto, offset, buf, n, NULL) < 0) {
  709. ret = -EIO;
  710. break;
  711. }
  712. }
  713. qemu_co_mutex_unlock(&s->lock);
  714. BLKDBG_CO_EVENT(bs->file, BLKDBG_WRITE_AIO);
  715. ret = bdrv_co_pwrite(bs->file, cluster_offset + offset_in_cluster,
  716. n, buf, 0);
  717. qemu_co_mutex_lock(&s->lock);
  718. if (ret < 0) {
  719. break;
  720. }
  721. ret = 0;
  722. bytes -= n;
  723. offset += n;
  724. buf += n;
  725. }
  726. qemu_co_mutex_unlock(&s->lock);
  727. qemu_vfree(orig_buf);
  728. return ret;
  729. }
  730. static void qcow_close(BlockDriverState *bs)
  731. {
  732. BDRVQcowState *s = bs->opaque;
  733. qcrypto_block_free(s->crypto);
  734. s->crypto = NULL;
  735. g_free(s->l1_table);
  736. qemu_vfree(s->l2_cache);
  737. g_free(s->cluster_cache);
  738. g_free(s->cluster_data);
  739. migrate_del_blocker(&s->migration_blocker);
  740. }
  741. static int coroutine_fn GRAPH_UNLOCKED
  742. qcow_co_create(BlockdevCreateOptions *opts, Error **errp)
  743. {
  744. BlockdevCreateOptionsQcow *qcow_opts;
  745. int header_size, backing_filename_len, l1_size, shift, i;
  746. QCowHeader header;
  747. uint8_t *tmp;
  748. int64_t total_size = 0;
  749. int ret;
  750. BlockDriverState *bs;
  751. BlockBackend *qcow_blk;
  752. QCryptoBlock *crypto = NULL;
  753. assert(opts->driver == BLOCKDEV_DRIVER_QCOW);
  754. qcow_opts = &opts->u.qcow;
  755. /* Sanity checks */
  756. total_size = qcow_opts->size;
  757. if (total_size == 0) {
  758. error_setg(errp, "Image size is too small, cannot be zero length");
  759. return -EINVAL;
  760. }
  761. if (qcow_opts->encrypt &&
  762. qcow_opts->encrypt->format != QCRYPTO_BLOCK_FORMAT_QCOW)
  763. {
  764. error_setg(errp, "Unsupported encryption format");
  765. return -EINVAL;
  766. }
  767. /* Create BlockBackend to write to the image */
  768. bs = bdrv_co_open_blockdev_ref(qcow_opts->file, errp);
  769. if (bs == NULL) {
  770. return -EIO;
  771. }
  772. qcow_blk = blk_co_new_with_bs(bs, BLK_PERM_WRITE | BLK_PERM_RESIZE,
  773. BLK_PERM_ALL, errp);
  774. if (!qcow_blk) {
  775. ret = -EPERM;
  776. goto exit;
  777. }
  778. blk_set_allow_write_beyond_eof(qcow_blk, true);
  779. /* Create image format */
  780. memset(&header, 0, sizeof(header));
  781. header.magic = cpu_to_be32(QCOW_MAGIC);
  782. header.version = cpu_to_be32(QCOW_VERSION);
  783. header.size = cpu_to_be64(total_size);
  784. header_size = sizeof(header);
  785. backing_filename_len = 0;
  786. if (qcow_opts->backing_file) {
  787. if (strcmp(qcow_opts->backing_file, "fat:")) {
  788. header.backing_file_offset = cpu_to_be64(header_size);
  789. backing_filename_len = strlen(qcow_opts->backing_file);
  790. header.backing_file_size = cpu_to_be32(backing_filename_len);
  791. header_size += backing_filename_len;
  792. } else {
  793. /* special backing file for vvfat */
  794. qcow_opts->backing_file = NULL;
  795. }
  796. header.cluster_bits = 9; /* 512 byte cluster to avoid copying
  797. unmodified sectors */
  798. header.l2_bits = 12; /* 32 KB L2 tables */
  799. } else {
  800. header.cluster_bits = 12; /* 4 KB clusters */
  801. header.l2_bits = 9; /* 4 KB L2 tables */
  802. }
  803. header_size = (header_size + 7) & ~7;
  804. shift = header.cluster_bits + header.l2_bits;
  805. l1_size = (total_size + (1LL << shift) - 1) >> shift;
  806. header.l1_table_offset = cpu_to_be64(header_size);
  807. if (qcow_opts->encrypt) {
  808. header.crypt_method = cpu_to_be32(QCOW_CRYPT_AES);
  809. crypto = qcrypto_block_create(qcow_opts->encrypt, "encrypt.",
  810. NULL, NULL, NULL, 0, errp);
  811. if (!crypto) {
  812. ret = -EINVAL;
  813. goto exit;
  814. }
  815. } else {
  816. header.crypt_method = cpu_to_be32(QCOW_CRYPT_NONE);
  817. }
  818. /* write all the data */
  819. ret = blk_co_pwrite(qcow_blk, 0, sizeof(header), &header, 0);
  820. if (ret < 0) {
  821. goto exit;
  822. }
  823. if (qcow_opts->backing_file) {
  824. ret = blk_co_pwrite(qcow_blk, sizeof(header), backing_filename_len,
  825. qcow_opts->backing_file, 0);
  826. if (ret < 0) {
  827. goto exit;
  828. }
  829. }
  830. tmp = g_malloc0(BDRV_SECTOR_SIZE);
  831. for (i = 0; i < DIV_ROUND_UP(sizeof(uint64_t) * l1_size, BDRV_SECTOR_SIZE);
  832. i++) {
  833. ret = blk_co_pwrite(qcow_blk, header_size + BDRV_SECTOR_SIZE * i,
  834. BDRV_SECTOR_SIZE, tmp, 0);
  835. if (ret < 0) {
  836. g_free(tmp);
  837. goto exit;
  838. }
  839. }
  840. g_free(tmp);
  841. ret = 0;
  842. exit:
  843. blk_co_unref(qcow_blk);
  844. bdrv_co_unref(bs);
  845. qcrypto_block_free(crypto);
  846. return ret;
  847. }
  848. static int coroutine_fn GRAPH_UNLOCKED
  849. qcow_co_create_opts(BlockDriver *drv, const char *filename,
  850. QemuOpts *opts, Error **errp)
  851. {
  852. BlockdevCreateOptions *create_options = NULL;
  853. BlockDriverState *bs = NULL;
  854. QDict *qdict = NULL;
  855. Visitor *v;
  856. const char *val;
  857. int ret;
  858. char *backing_fmt;
  859. static const QDictRenames opt_renames[] = {
  860. { BLOCK_OPT_BACKING_FILE, "backing-file" },
  861. { BLOCK_OPT_ENCRYPT, BLOCK_OPT_ENCRYPT_FORMAT },
  862. { NULL, NULL },
  863. };
  864. /*
  865. * We can't actually store a backing format, but can check that
  866. * the user's request made sense.
  867. */
  868. backing_fmt = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FMT);
  869. if (backing_fmt && !bdrv_find_format(backing_fmt)) {
  870. error_setg(errp, "unrecognized backing format '%s'", backing_fmt);
  871. ret = -EINVAL;
  872. goto fail;
  873. }
  874. /* Parse options and convert legacy syntax */
  875. qdict = qemu_opts_to_qdict_filtered(opts, NULL, &qcow_create_opts, true);
  876. val = qdict_get_try_str(qdict, BLOCK_OPT_ENCRYPT);
  877. if (val && !strcmp(val, "on")) {
  878. qdict_put_str(qdict, BLOCK_OPT_ENCRYPT, "qcow");
  879. } else if (val && !strcmp(val, "off")) {
  880. qdict_del(qdict, BLOCK_OPT_ENCRYPT);
  881. }
  882. val = qdict_get_try_str(qdict, BLOCK_OPT_ENCRYPT_FORMAT);
  883. if (val && !strcmp(val, "aes")) {
  884. qdict_put_str(qdict, BLOCK_OPT_ENCRYPT_FORMAT, "qcow");
  885. }
  886. if (!qdict_rename_keys(qdict, opt_renames, errp)) {
  887. ret = -EINVAL;
  888. goto fail;
  889. }
  890. /* Create and open the file (protocol layer) */
  891. ret = bdrv_co_create_file(filename, opts, errp);
  892. if (ret < 0) {
  893. goto fail;
  894. }
  895. bs = bdrv_co_open(filename, NULL, NULL,
  896. BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp);
  897. if (bs == NULL) {
  898. ret = -EIO;
  899. goto fail;
  900. }
  901. /* Now get the QAPI type BlockdevCreateOptions */
  902. qdict_put_str(qdict, "driver", "qcow");
  903. qdict_put_str(qdict, "file", bs->node_name);
  904. v = qobject_input_visitor_new_flat_confused(qdict, errp);
  905. if (!v) {
  906. ret = -EINVAL;
  907. goto fail;
  908. }
  909. visit_type_BlockdevCreateOptions(v, NULL, &create_options, errp);
  910. visit_free(v);
  911. if (!create_options) {
  912. ret = -EINVAL;
  913. goto fail;
  914. }
  915. /* Silently round up size */
  916. assert(create_options->driver == BLOCKDEV_DRIVER_QCOW);
  917. create_options->u.qcow.size =
  918. ROUND_UP(create_options->u.qcow.size, BDRV_SECTOR_SIZE);
  919. /* Create the qcow image (format layer) */
  920. ret = qcow_co_create(create_options, errp);
  921. if (ret < 0) {
  922. goto fail;
  923. }
  924. ret = 0;
  925. fail:
  926. g_free(backing_fmt);
  927. qobject_unref(qdict);
  928. bdrv_co_unref(bs);
  929. qapi_free_BlockdevCreateOptions(create_options);
  930. return ret;
  931. }
  932. static int GRAPH_RDLOCK qcow_make_empty(BlockDriverState *bs)
  933. {
  934. BDRVQcowState *s = bs->opaque;
  935. uint32_t l1_length = s->l1_size * sizeof(uint64_t);
  936. int ret;
  937. memset(s->l1_table, 0, l1_length);
  938. if (bdrv_pwrite_sync(bs->file, s->l1_table_offset, l1_length, s->l1_table,
  939. 0) < 0)
  940. return -1;
  941. ret = bdrv_truncate(bs->file, s->l1_table_offset + l1_length, false,
  942. PREALLOC_MODE_OFF, 0, NULL);
  943. if (ret < 0)
  944. return ret;
  945. memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
  946. memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t));
  947. memset(s->l2_cache_counts, 0, L2_CACHE_SIZE * sizeof(uint32_t));
  948. return 0;
  949. }
  950. /* XXX: put compressed sectors first, then all the cluster aligned
  951. tables to avoid losing bytes in alignment */
  952. static int coroutine_fn GRAPH_RDLOCK
  953. qcow_co_pwritev_compressed(BlockDriverState *bs, int64_t offset, int64_t bytes,
  954. QEMUIOVector *qiov)
  955. {
  956. BDRVQcowState *s = bs->opaque;
  957. z_stream strm;
  958. int ret, out_len;
  959. uint8_t *buf, *out_buf;
  960. uint64_t cluster_offset;
  961. buf = qemu_blockalign(bs, s->cluster_size);
  962. if (bytes != s->cluster_size) {
  963. if (bytes > s->cluster_size ||
  964. offset + bytes != bs->total_sectors << BDRV_SECTOR_BITS)
  965. {
  966. qemu_vfree(buf);
  967. return -EINVAL;
  968. }
  969. /* Zero-pad last write if image size is not cluster aligned */
  970. memset(buf + bytes, 0, s->cluster_size - bytes);
  971. }
  972. qemu_iovec_to_buf(qiov, 0, buf, qiov->size);
  973. out_buf = g_malloc(s->cluster_size);
  974. /* best compression, small window, no zlib header */
  975. memset(&strm, 0, sizeof(strm));
  976. ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION,
  977. Z_DEFLATED, -12,
  978. 9, Z_DEFAULT_STRATEGY);
  979. if (ret != 0) {
  980. ret = -EINVAL;
  981. goto fail;
  982. }
  983. strm.avail_in = s->cluster_size;
  984. strm.next_in = (uint8_t *)buf;
  985. strm.avail_out = s->cluster_size;
  986. strm.next_out = out_buf;
  987. ret = deflate(&strm, Z_FINISH);
  988. if (ret != Z_STREAM_END && ret != Z_OK) {
  989. deflateEnd(&strm);
  990. ret = -EINVAL;
  991. goto fail;
  992. }
  993. out_len = strm.next_out - out_buf;
  994. deflateEnd(&strm);
  995. if (ret != Z_STREAM_END || out_len >= s->cluster_size) {
  996. /* could not compress: write normal cluster */
  997. ret = qcow_co_pwritev(bs, offset, bytes, qiov, 0);
  998. if (ret < 0) {
  999. goto fail;
  1000. }
  1001. goto success;
  1002. }
  1003. qemu_co_mutex_lock(&s->lock);
  1004. ret = get_cluster_offset(bs, offset, 2, out_len, 0, 0, &cluster_offset);
  1005. qemu_co_mutex_unlock(&s->lock);
  1006. if (ret < 0) {
  1007. goto fail;
  1008. }
  1009. if (cluster_offset == 0) {
  1010. ret = -EIO;
  1011. goto fail;
  1012. }
  1013. cluster_offset &= s->cluster_offset_mask;
  1014. BLKDBG_CO_EVENT(bs->file, BLKDBG_WRITE_COMPRESSED);
  1015. ret = bdrv_co_pwrite(bs->file, cluster_offset, out_len, out_buf, 0);
  1016. if (ret < 0) {
  1017. goto fail;
  1018. }
  1019. success:
  1020. ret = 0;
  1021. fail:
  1022. qemu_vfree(buf);
  1023. g_free(out_buf);
  1024. return ret;
  1025. }
  1026. static int coroutine_fn
  1027. qcow_co_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
  1028. {
  1029. BDRVQcowState *s = bs->opaque;
  1030. bdi->cluster_size = s->cluster_size;
  1031. return 0;
  1032. }
  1033. static QemuOptsList qcow_create_opts = {
  1034. .name = "qcow-create-opts",
  1035. .head = QTAILQ_HEAD_INITIALIZER(qcow_create_opts.head),
  1036. .desc = {
  1037. {
  1038. .name = BLOCK_OPT_SIZE,
  1039. .type = QEMU_OPT_SIZE,
  1040. .help = "Virtual disk size"
  1041. },
  1042. {
  1043. .name = BLOCK_OPT_BACKING_FILE,
  1044. .type = QEMU_OPT_STRING,
  1045. .help = "File name of a base image"
  1046. },
  1047. {
  1048. .name = BLOCK_OPT_BACKING_FMT,
  1049. .type = QEMU_OPT_STRING,
  1050. .help = "Format of the backing image",
  1051. },
  1052. {
  1053. .name = BLOCK_OPT_ENCRYPT,
  1054. .type = QEMU_OPT_BOOL,
  1055. .help = "Encrypt the image with format 'aes'. (Deprecated "
  1056. "in favor of " BLOCK_OPT_ENCRYPT_FORMAT "=aes)",
  1057. },
  1058. {
  1059. .name = BLOCK_OPT_ENCRYPT_FORMAT,
  1060. .type = QEMU_OPT_STRING,
  1061. .help = "Encrypt the image, format choices: 'aes'",
  1062. },
  1063. BLOCK_CRYPTO_OPT_DEF_QCOW_KEY_SECRET("encrypt."),
  1064. { /* end of list */ }
  1065. }
  1066. };
  1067. static const char *const qcow_strong_runtime_opts[] = {
  1068. "encrypt." BLOCK_CRYPTO_OPT_QCOW_KEY_SECRET,
  1069. NULL
  1070. };
  1071. static BlockDriver bdrv_qcow = {
  1072. .format_name = "qcow",
  1073. .instance_size = sizeof(BDRVQcowState),
  1074. .bdrv_probe = qcow_probe,
  1075. .bdrv_open = qcow_open,
  1076. .bdrv_close = qcow_close,
  1077. .bdrv_child_perm = bdrv_default_perms,
  1078. .bdrv_reopen_prepare = qcow_reopen_prepare,
  1079. .bdrv_co_create = qcow_co_create,
  1080. .bdrv_co_create_opts = qcow_co_create_opts,
  1081. .bdrv_has_zero_init = bdrv_has_zero_init_1,
  1082. .is_format = true,
  1083. .supports_backing = true,
  1084. .bdrv_refresh_limits = qcow_refresh_limits,
  1085. .bdrv_co_preadv = qcow_co_preadv,
  1086. .bdrv_co_pwritev = qcow_co_pwritev,
  1087. .bdrv_co_block_status = qcow_co_block_status,
  1088. .bdrv_make_empty = qcow_make_empty,
  1089. .bdrv_co_pwritev_compressed = qcow_co_pwritev_compressed,
  1090. .bdrv_co_get_info = qcow_co_get_info,
  1091. .create_opts = &qcow_create_opts,
  1092. .strong_runtime_opts = qcow_strong_runtime_opts,
  1093. };
  1094. static void bdrv_qcow_init(void)
  1095. {
  1096. bdrv_register(&bdrv_qcow);
  1097. }
  1098. block_init(bdrv_qcow_init);