qemu-io-cmds.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371
  1. /*
  2. * Command line utility to exercise the QEMU I/O path.
  3. *
  4. * Copyright (C) 2009-2016 Red Hat, Inc.
  5. * Copyright (c) 2003-2005 Silicon Graphics, Inc.
  6. *
  7. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  8. * See the COPYING file in the top-level directory.
  9. */
  10. #include "qemu/osdep.h"
  11. #include "qapi/error.h"
  12. #include "qemu-io.h"
  13. #include "sysemu/block-backend.h"
  14. #include "block/block.h"
  15. #include "block/block_int.h" /* for info_f() */
  16. #include "block/qapi.h"
  17. #include "qemu/error-report.h"
  18. #include "qemu/main-loop.h"
  19. #include "qemu/option.h"
  20. #include "qemu/timer.h"
  21. #include "qemu/cutils.h"
  22. #define CMD_NOFILE_OK 0x01
  23. bool qemuio_misalign;
  24. static cmdinfo_t *cmdtab;
  25. static int ncmds;
  26. static int compare_cmdname(const void *a, const void *b)
  27. {
  28. return strcmp(((const cmdinfo_t *)a)->name,
  29. ((const cmdinfo_t *)b)->name);
  30. }
  31. void qemuio_add_command(const cmdinfo_t *ci)
  32. {
  33. /* ci->perm assumes a file is open, but the GLOBAL and NOFILE_OK
  34. * flags allow it not to be, so that combination is invalid.
  35. * Catch it now rather than letting it manifest as a crash if a
  36. * particular set of command line options are used.
  37. */
  38. assert(ci->perm == 0 ||
  39. (ci->flags & (CMD_FLAG_GLOBAL | CMD_NOFILE_OK)) == 0);
  40. cmdtab = g_renew(cmdinfo_t, cmdtab, ++ncmds);
  41. cmdtab[ncmds - 1] = *ci;
  42. qsort(cmdtab, ncmds, sizeof(*cmdtab), compare_cmdname);
  43. }
  44. void qemuio_command_usage(const cmdinfo_t *ci)
  45. {
  46. printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
  47. }
  48. static int init_check_command(BlockBackend *blk, const cmdinfo_t *ct)
  49. {
  50. if (ct->flags & CMD_FLAG_GLOBAL) {
  51. return 1;
  52. }
  53. if (!(ct->flags & CMD_NOFILE_OK) && !blk) {
  54. fprintf(stderr, "no file open, try 'help open'\n");
  55. return 0;
  56. }
  57. return 1;
  58. }
  59. static int command(BlockBackend *blk, const cmdinfo_t *ct, int argc,
  60. char **argv)
  61. {
  62. char *cmd = argv[0];
  63. if (!init_check_command(blk, ct)) {
  64. return -EINVAL;
  65. }
  66. if (argc - 1 < ct->argmin || (ct->argmax != -1 && argc - 1 > ct->argmax)) {
  67. if (ct->argmax == -1) {
  68. fprintf(stderr,
  69. "bad argument count %d to %s, expected at least %d arguments\n",
  70. argc-1, cmd, ct->argmin);
  71. } else if (ct->argmin == ct->argmax) {
  72. fprintf(stderr,
  73. "bad argument count %d to %s, expected %d arguments\n",
  74. argc-1, cmd, ct->argmin);
  75. } else {
  76. fprintf(stderr,
  77. "bad argument count %d to %s, expected between %d and %d arguments\n",
  78. argc-1, cmd, ct->argmin, ct->argmax);
  79. }
  80. return -EINVAL;
  81. }
  82. /* Request additional permissions if necessary for this command. The caller
  83. * is responsible for restoring the original permissions afterwards if this
  84. * is what it wants. */
  85. if (ct->perm && blk_is_available(blk)) {
  86. uint64_t orig_perm, orig_shared_perm;
  87. blk_get_perm(blk, &orig_perm, &orig_shared_perm);
  88. if (ct->perm & ~orig_perm) {
  89. uint64_t new_perm;
  90. Error *local_err = NULL;
  91. int ret;
  92. new_perm = orig_perm | ct->perm;
  93. ret = blk_set_perm(blk, new_perm, orig_shared_perm, &local_err);
  94. if (ret < 0) {
  95. error_report_err(local_err);
  96. return ret;
  97. }
  98. }
  99. }
  100. optind = 0;
  101. return ct->cfunc(blk, argc, argv);
  102. }
  103. static const cmdinfo_t *find_command(const char *cmd)
  104. {
  105. cmdinfo_t *ct;
  106. for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
  107. if (strcmp(ct->name, cmd) == 0 ||
  108. (ct->altname && strcmp(ct->altname, cmd) == 0))
  109. {
  110. return (const cmdinfo_t *)ct;
  111. }
  112. }
  113. return NULL;
  114. }
  115. /* Invoke fn() for commands with a matching prefix */
  116. void qemuio_complete_command(const char *input,
  117. void (*fn)(const char *cmd, void *opaque),
  118. void *opaque)
  119. {
  120. cmdinfo_t *ct;
  121. size_t input_len = strlen(input);
  122. for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
  123. if (strncmp(input, ct->name, input_len) == 0) {
  124. fn(ct->name, opaque);
  125. }
  126. }
  127. }
  128. static char **breakline(char *input, int *count)
  129. {
  130. int c = 0;
  131. char *p;
  132. char **rval = g_new0(char *, 1);
  133. while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
  134. if (!*p) {
  135. continue;
  136. }
  137. c++;
  138. rval = g_renew(char *, rval, (c + 1));
  139. rval[c - 1] = p;
  140. rval[c] = NULL;
  141. }
  142. *count = c;
  143. return rval;
  144. }
  145. static int64_t cvtnum(const char *s)
  146. {
  147. int err;
  148. uint64_t value;
  149. err = qemu_strtosz(s, NULL, &value);
  150. if (err < 0) {
  151. return err;
  152. }
  153. if (value > INT64_MAX) {
  154. return -ERANGE;
  155. }
  156. return value;
  157. }
  158. static void print_cvtnum_err(int64_t rc, const char *arg)
  159. {
  160. switch (rc) {
  161. case -EINVAL:
  162. printf("Parsing error: non-numeric argument,"
  163. " or extraneous/unrecognized suffix -- %s\n", arg);
  164. break;
  165. case -ERANGE:
  166. printf("Parsing error: argument too large -- %s\n", arg);
  167. break;
  168. default:
  169. printf("Parsing error: %s\n", arg);
  170. }
  171. }
  172. #define EXABYTES(x) ((long long)(x) << 60)
  173. #define PETABYTES(x) ((long long)(x) << 50)
  174. #define TERABYTES(x) ((long long)(x) << 40)
  175. #define GIGABYTES(x) ((long long)(x) << 30)
  176. #define MEGABYTES(x) ((long long)(x) << 20)
  177. #define KILOBYTES(x) ((long long)(x) << 10)
  178. #define TO_EXABYTES(x) ((x) / EXABYTES(1))
  179. #define TO_PETABYTES(x) ((x) / PETABYTES(1))
  180. #define TO_TERABYTES(x) ((x) / TERABYTES(1))
  181. #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
  182. #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
  183. #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
  184. static void cvtstr(double value, char *str, size_t size)
  185. {
  186. char *trim;
  187. const char *suffix;
  188. if (value >= EXABYTES(1)) {
  189. suffix = " EiB";
  190. snprintf(str, size - 4, "%.3f", TO_EXABYTES(value));
  191. } else if (value >= PETABYTES(1)) {
  192. suffix = " PiB";
  193. snprintf(str, size - 4, "%.3f", TO_PETABYTES(value));
  194. } else if (value >= TERABYTES(1)) {
  195. suffix = " TiB";
  196. snprintf(str, size - 4, "%.3f", TO_TERABYTES(value));
  197. } else if (value >= GIGABYTES(1)) {
  198. suffix = " GiB";
  199. snprintf(str, size - 4, "%.3f", TO_GIGABYTES(value));
  200. } else if (value >= MEGABYTES(1)) {
  201. suffix = " MiB";
  202. snprintf(str, size - 4, "%.3f", TO_MEGABYTES(value));
  203. } else if (value >= KILOBYTES(1)) {
  204. suffix = " KiB";
  205. snprintf(str, size - 4, "%.3f", TO_KILOBYTES(value));
  206. } else {
  207. suffix = " bytes";
  208. snprintf(str, size - 6, "%f", value);
  209. }
  210. trim = strstr(str, ".000");
  211. if (trim) {
  212. strcpy(trim, suffix);
  213. } else {
  214. strcat(str, suffix);
  215. }
  216. }
  217. static struct timeval tsub(struct timeval t1, struct timeval t2)
  218. {
  219. t1.tv_usec -= t2.tv_usec;
  220. if (t1.tv_usec < 0) {
  221. t1.tv_usec += 1000000;
  222. t1.tv_sec--;
  223. }
  224. t1.tv_sec -= t2.tv_sec;
  225. return t1;
  226. }
  227. static double tdiv(double value, struct timeval tv)
  228. {
  229. return value / ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
  230. }
  231. #define HOURS(sec) ((sec) / (60 * 60))
  232. #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
  233. #define SECONDS(sec) ((sec) % 60)
  234. enum {
  235. DEFAULT_TIME = 0x0,
  236. TERSE_FIXED_TIME = 0x1,
  237. VERBOSE_FIXED_TIME = 0x2,
  238. };
  239. static void timestr(struct timeval *tv, char *ts, size_t size, int format)
  240. {
  241. double usec = (double)tv->tv_usec / 1000000.0;
  242. if (format & TERSE_FIXED_TIME) {
  243. if (!HOURS(tv->tv_sec)) {
  244. snprintf(ts, size, "%u:%02u.%02u",
  245. (unsigned int) MINUTES(tv->tv_sec),
  246. (unsigned int) SECONDS(tv->tv_sec),
  247. (unsigned int) (usec * 100));
  248. return;
  249. }
  250. format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
  251. }
  252. if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) {
  253. snprintf(ts, size, "%u:%02u:%02u.%02u",
  254. (unsigned int) HOURS(tv->tv_sec),
  255. (unsigned int) MINUTES(tv->tv_sec),
  256. (unsigned int) SECONDS(tv->tv_sec),
  257. (unsigned int) (usec * 100));
  258. } else {
  259. snprintf(ts, size, "0.%04u sec", (unsigned int) (usec * 10000));
  260. }
  261. }
  262. /*
  263. * Parse the pattern argument to various sub-commands.
  264. *
  265. * Because the pattern is used as an argument to memset it must evaluate
  266. * to an unsigned integer that fits into a single byte.
  267. */
  268. static int parse_pattern(const char *arg)
  269. {
  270. char *endptr = NULL;
  271. long pattern;
  272. pattern = strtol(arg, &endptr, 0);
  273. if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
  274. printf("%s is not a valid pattern byte\n", arg);
  275. return -1;
  276. }
  277. return pattern;
  278. }
  279. /*
  280. * Memory allocation helpers.
  281. *
  282. * Make sure memory is aligned by default, or purposefully misaligned if
  283. * that is specified on the command line.
  284. */
  285. #define MISALIGN_OFFSET 16
  286. static void *qemu_io_alloc(BlockBackend *blk, size_t len, int pattern)
  287. {
  288. void *buf;
  289. if (qemuio_misalign) {
  290. len += MISALIGN_OFFSET;
  291. }
  292. buf = blk_blockalign(blk, len);
  293. memset(buf, pattern, len);
  294. if (qemuio_misalign) {
  295. buf += MISALIGN_OFFSET;
  296. }
  297. return buf;
  298. }
  299. static void qemu_io_free(void *p)
  300. {
  301. if (qemuio_misalign) {
  302. p -= MISALIGN_OFFSET;
  303. }
  304. qemu_vfree(p);
  305. }
  306. static void dump_buffer(const void *buffer, int64_t offset, int64_t len)
  307. {
  308. uint64_t i;
  309. int j;
  310. const uint8_t *p;
  311. for (i = 0, p = buffer; i < len; i += 16) {
  312. const uint8_t *s = p;
  313. printf("%08" PRIx64 ": ", offset + i);
  314. for (j = 0; j < 16 && i + j < len; j++, p++) {
  315. printf("%02x ", *p);
  316. }
  317. printf(" ");
  318. for (j = 0; j < 16 && i + j < len; j++, s++) {
  319. if (isalnum(*s)) {
  320. printf("%c", *s);
  321. } else {
  322. printf(".");
  323. }
  324. }
  325. printf("\n");
  326. }
  327. }
  328. static void print_report(const char *op, struct timeval *t, int64_t offset,
  329. int64_t count, int64_t total, int cnt, bool Cflag)
  330. {
  331. char s1[64], s2[64], ts[64];
  332. timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
  333. if (!Cflag) {
  334. cvtstr((double)total, s1, sizeof(s1));
  335. cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
  336. printf("%s %"PRId64"/%"PRId64" bytes at offset %" PRId64 "\n",
  337. op, total, count, offset);
  338. printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
  339. s1, cnt, ts, s2, tdiv((double)cnt, *t));
  340. } else {/* bytes,ops,time,bytes/sec,ops/sec */
  341. printf("%"PRId64",%d,%s,%.3f,%.3f\n",
  342. total, cnt, ts,
  343. tdiv((double)total, *t),
  344. tdiv((double)cnt, *t));
  345. }
  346. }
  347. /*
  348. * Parse multiple length statements for vectored I/O, and construct an I/O
  349. * vector matching it.
  350. */
  351. static void *
  352. create_iovec(BlockBackend *blk, QEMUIOVector *qiov, char **argv, int nr_iov,
  353. int pattern)
  354. {
  355. size_t *sizes = g_new0(size_t, nr_iov);
  356. size_t count = 0;
  357. void *buf = NULL;
  358. void *p;
  359. int i;
  360. for (i = 0; i < nr_iov; i++) {
  361. char *arg = argv[i];
  362. int64_t len;
  363. len = cvtnum(arg);
  364. if (len < 0) {
  365. print_cvtnum_err(len, arg);
  366. goto fail;
  367. }
  368. if (len > BDRV_REQUEST_MAX_BYTES) {
  369. printf("Argument '%s' exceeds maximum size %" PRIu64 "\n", arg,
  370. (uint64_t)BDRV_REQUEST_MAX_BYTES);
  371. goto fail;
  372. }
  373. if (count > BDRV_REQUEST_MAX_BYTES - len) {
  374. printf("The total number of bytes exceed the maximum size %" PRIu64
  375. "\n", (uint64_t)BDRV_REQUEST_MAX_BYTES);
  376. goto fail;
  377. }
  378. sizes[i] = len;
  379. count += len;
  380. }
  381. qemu_iovec_init(qiov, nr_iov);
  382. buf = p = qemu_io_alloc(blk, count, pattern);
  383. for (i = 0; i < nr_iov; i++) {
  384. qemu_iovec_add(qiov, p, sizes[i]);
  385. p += sizes[i];
  386. }
  387. fail:
  388. g_free(sizes);
  389. return buf;
  390. }
  391. static int do_pread(BlockBackend *blk, char *buf, int64_t offset,
  392. int64_t bytes, int64_t *total)
  393. {
  394. if (bytes > INT_MAX) {
  395. return -ERANGE;
  396. }
  397. *total = blk_pread(blk, offset, (uint8_t *)buf, bytes);
  398. if (*total < 0) {
  399. return *total;
  400. }
  401. return 1;
  402. }
  403. static int do_pwrite(BlockBackend *blk, char *buf, int64_t offset,
  404. int64_t bytes, int flags, int64_t *total)
  405. {
  406. if (bytes > INT_MAX) {
  407. return -ERANGE;
  408. }
  409. *total = blk_pwrite(blk, offset, (uint8_t *)buf, bytes, flags);
  410. if (*total < 0) {
  411. return *total;
  412. }
  413. return 1;
  414. }
  415. typedef struct {
  416. BlockBackend *blk;
  417. int64_t offset;
  418. int64_t bytes;
  419. int64_t *total;
  420. int flags;
  421. int ret;
  422. bool done;
  423. } CoWriteZeroes;
  424. static void coroutine_fn co_pwrite_zeroes_entry(void *opaque)
  425. {
  426. CoWriteZeroes *data = opaque;
  427. data->ret = blk_co_pwrite_zeroes(data->blk, data->offset, data->bytes,
  428. data->flags);
  429. data->done = true;
  430. if (data->ret < 0) {
  431. *data->total = data->ret;
  432. return;
  433. }
  434. *data->total = data->bytes;
  435. }
  436. static int do_co_pwrite_zeroes(BlockBackend *blk, int64_t offset,
  437. int64_t bytes, int flags, int64_t *total)
  438. {
  439. Coroutine *co;
  440. CoWriteZeroes data = {
  441. .blk = blk,
  442. .offset = offset,
  443. .bytes = bytes,
  444. .total = total,
  445. .flags = flags,
  446. .done = false,
  447. };
  448. if (bytes > INT_MAX) {
  449. return -ERANGE;
  450. }
  451. co = qemu_coroutine_create(co_pwrite_zeroes_entry, &data);
  452. bdrv_coroutine_enter(blk_bs(blk), co);
  453. while (!data.done) {
  454. aio_poll(blk_get_aio_context(blk), true);
  455. }
  456. if (data.ret < 0) {
  457. return data.ret;
  458. } else {
  459. return 1;
  460. }
  461. }
  462. static int do_write_compressed(BlockBackend *blk, char *buf, int64_t offset,
  463. int64_t bytes, int64_t *total)
  464. {
  465. int ret;
  466. if (bytes >> 9 > BDRV_REQUEST_MAX_SECTORS) {
  467. return -ERANGE;
  468. }
  469. ret = blk_pwrite_compressed(blk, offset, buf, bytes);
  470. if (ret < 0) {
  471. return ret;
  472. }
  473. *total = bytes;
  474. return 1;
  475. }
  476. static int do_load_vmstate(BlockBackend *blk, char *buf, int64_t offset,
  477. int64_t count, int64_t *total)
  478. {
  479. if (count > INT_MAX) {
  480. return -ERANGE;
  481. }
  482. *total = blk_load_vmstate(blk, (uint8_t *)buf, offset, count);
  483. if (*total < 0) {
  484. return *total;
  485. }
  486. return 1;
  487. }
  488. static int do_save_vmstate(BlockBackend *blk, char *buf, int64_t offset,
  489. int64_t count, int64_t *total)
  490. {
  491. if (count > INT_MAX) {
  492. return -ERANGE;
  493. }
  494. *total = blk_save_vmstate(blk, (uint8_t *)buf, offset, count);
  495. if (*total < 0) {
  496. return *total;
  497. }
  498. return 1;
  499. }
  500. #define NOT_DONE 0x7fffffff
  501. static void aio_rw_done(void *opaque, int ret)
  502. {
  503. *(int *)opaque = ret;
  504. }
  505. static int do_aio_readv(BlockBackend *blk, QEMUIOVector *qiov,
  506. int64_t offset, int *total)
  507. {
  508. int async_ret = NOT_DONE;
  509. blk_aio_preadv(blk, offset, qiov, 0, aio_rw_done, &async_ret);
  510. while (async_ret == NOT_DONE) {
  511. main_loop_wait(false);
  512. }
  513. *total = qiov->size;
  514. return async_ret < 0 ? async_ret : 1;
  515. }
  516. static int do_aio_writev(BlockBackend *blk, QEMUIOVector *qiov,
  517. int64_t offset, int flags, int *total)
  518. {
  519. int async_ret = NOT_DONE;
  520. blk_aio_pwritev(blk, offset, qiov, flags, aio_rw_done, &async_ret);
  521. while (async_ret == NOT_DONE) {
  522. main_loop_wait(false);
  523. }
  524. *total = qiov->size;
  525. return async_ret < 0 ? async_ret : 1;
  526. }
  527. static void read_help(void)
  528. {
  529. printf(
  530. "\n"
  531. " reads a range of bytes from the given offset\n"
  532. "\n"
  533. " Example:\n"
  534. " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
  535. "\n"
  536. " Reads a segment of the currently open file, optionally dumping it to the\n"
  537. " standard output stream (with -v option) for subsequent inspection.\n"
  538. " -b, -- read from the VM state rather than the virtual disk\n"
  539. " -C, -- report statistics in a machine parsable format\n"
  540. " -l, -- length for pattern verification (only with -P)\n"
  541. " -p, -- ignored for backwards compatibility\n"
  542. " -P, -- use a pattern to verify read data\n"
  543. " -q, -- quiet mode, do not show I/O statistics\n"
  544. " -s, -- start offset for pattern verification (only with -P)\n"
  545. " -v, -- dump buffer to standard output\n"
  546. "\n");
  547. }
  548. static int read_f(BlockBackend *blk, int argc, char **argv);
  549. static const cmdinfo_t read_cmd = {
  550. .name = "read",
  551. .altname = "r",
  552. .cfunc = read_f,
  553. .argmin = 2,
  554. .argmax = -1,
  555. .args = "[-abCqv] [-P pattern [-s off] [-l len]] off len",
  556. .oneline = "reads a number of bytes at a specified offset",
  557. .help = read_help,
  558. };
  559. static int read_f(BlockBackend *blk, int argc, char **argv)
  560. {
  561. struct timeval t1, t2;
  562. bool Cflag = false, qflag = false, vflag = false;
  563. bool Pflag = false, sflag = false, lflag = false, bflag = false;
  564. int c, cnt, ret;
  565. char *buf;
  566. int64_t offset;
  567. int64_t count;
  568. /* Some compilers get confused and warn if this is not initialized. */
  569. int64_t total = 0;
  570. int pattern = 0;
  571. int64_t pattern_offset = 0, pattern_count = 0;
  572. while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != -1) {
  573. switch (c) {
  574. case 'b':
  575. bflag = true;
  576. break;
  577. case 'C':
  578. Cflag = true;
  579. break;
  580. case 'l':
  581. lflag = true;
  582. pattern_count = cvtnum(optarg);
  583. if (pattern_count < 0) {
  584. print_cvtnum_err(pattern_count, optarg);
  585. return pattern_count;
  586. }
  587. break;
  588. case 'p':
  589. /* Ignored for backwards compatibility */
  590. break;
  591. case 'P':
  592. Pflag = true;
  593. pattern = parse_pattern(optarg);
  594. if (pattern < 0) {
  595. return -EINVAL;
  596. }
  597. break;
  598. case 'q':
  599. qflag = true;
  600. break;
  601. case 's':
  602. sflag = true;
  603. pattern_offset = cvtnum(optarg);
  604. if (pattern_offset < 0) {
  605. print_cvtnum_err(pattern_offset, optarg);
  606. return pattern_offset;
  607. }
  608. break;
  609. case 'v':
  610. vflag = true;
  611. break;
  612. default:
  613. qemuio_command_usage(&read_cmd);
  614. return -EINVAL;
  615. }
  616. }
  617. if (optind != argc - 2) {
  618. qemuio_command_usage(&read_cmd);
  619. return -EINVAL;
  620. }
  621. offset = cvtnum(argv[optind]);
  622. if (offset < 0) {
  623. print_cvtnum_err(offset, argv[optind]);
  624. return offset;
  625. }
  626. optind++;
  627. count = cvtnum(argv[optind]);
  628. if (count < 0) {
  629. print_cvtnum_err(count, argv[optind]);
  630. return count;
  631. } else if (count > BDRV_REQUEST_MAX_BYTES) {
  632. printf("length cannot exceed %" PRIu64 ", given %s\n",
  633. (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
  634. return -EINVAL;
  635. }
  636. if (!Pflag && (lflag || sflag)) {
  637. qemuio_command_usage(&read_cmd);
  638. return -EINVAL;
  639. }
  640. if (!lflag) {
  641. pattern_count = count - pattern_offset;
  642. }
  643. if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
  644. printf("pattern verification range exceeds end of read data\n");
  645. return -EINVAL;
  646. }
  647. if (bflag) {
  648. if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) {
  649. printf("%" PRId64 " is not a sector-aligned value for 'offset'\n",
  650. offset);
  651. return -EINVAL;
  652. }
  653. if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) {
  654. printf("%"PRId64" is not a sector-aligned value for 'count'\n",
  655. count);
  656. return -EINVAL;
  657. }
  658. }
  659. buf = qemu_io_alloc(blk, count, 0xab);
  660. gettimeofday(&t1, NULL);
  661. if (bflag) {
  662. ret = do_load_vmstate(blk, buf, offset, count, &total);
  663. } else {
  664. ret = do_pread(blk, buf, offset, count, &total);
  665. }
  666. gettimeofday(&t2, NULL);
  667. if (ret < 0) {
  668. printf("read failed: %s\n", strerror(-ret));
  669. goto out;
  670. }
  671. cnt = ret;
  672. ret = 0;
  673. if (Pflag) {
  674. void *cmp_buf = g_malloc(pattern_count);
  675. memset(cmp_buf, pattern, pattern_count);
  676. if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
  677. printf("Pattern verification failed at offset %"
  678. PRId64 ", %"PRId64" bytes\n",
  679. offset + pattern_offset, pattern_count);
  680. ret = -EINVAL;
  681. }
  682. g_free(cmp_buf);
  683. }
  684. if (qflag) {
  685. goto out;
  686. }
  687. if (vflag) {
  688. dump_buffer(buf, offset, count);
  689. }
  690. /* Finally, report back -- -C gives a parsable format */
  691. t2 = tsub(t2, t1);
  692. print_report("read", &t2, offset, count, total, cnt, Cflag);
  693. out:
  694. qemu_io_free(buf);
  695. return ret;
  696. }
  697. static void readv_help(void)
  698. {
  699. printf(
  700. "\n"
  701. " reads a range of bytes from the given offset into multiple buffers\n"
  702. "\n"
  703. " Example:\n"
  704. " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
  705. "\n"
  706. " Reads a segment of the currently open file, optionally dumping it to the\n"
  707. " standard output stream (with -v option) for subsequent inspection.\n"
  708. " Uses multiple iovec buffers if more than one byte range is specified.\n"
  709. " -C, -- report statistics in a machine parsable format\n"
  710. " -P, -- use a pattern to verify read data\n"
  711. " -v, -- dump buffer to standard output\n"
  712. " -q, -- quiet mode, do not show I/O statistics\n"
  713. "\n");
  714. }
  715. static int readv_f(BlockBackend *blk, int argc, char **argv);
  716. static const cmdinfo_t readv_cmd = {
  717. .name = "readv",
  718. .cfunc = readv_f,
  719. .argmin = 2,
  720. .argmax = -1,
  721. .args = "[-Cqv] [-P pattern] off len [len..]",
  722. .oneline = "reads a number of bytes at a specified offset",
  723. .help = readv_help,
  724. };
  725. static int readv_f(BlockBackend *blk, int argc, char **argv)
  726. {
  727. struct timeval t1, t2;
  728. bool Cflag = false, qflag = false, vflag = false;
  729. int c, cnt, ret;
  730. char *buf;
  731. int64_t offset;
  732. /* Some compilers get confused and warn if this is not initialized. */
  733. int total = 0;
  734. int nr_iov;
  735. QEMUIOVector qiov;
  736. int pattern = 0;
  737. bool Pflag = false;
  738. while ((c = getopt(argc, argv, "CP:qv")) != -1) {
  739. switch (c) {
  740. case 'C':
  741. Cflag = true;
  742. break;
  743. case 'P':
  744. Pflag = true;
  745. pattern = parse_pattern(optarg);
  746. if (pattern < 0) {
  747. return -EINVAL;
  748. }
  749. break;
  750. case 'q':
  751. qflag = true;
  752. break;
  753. case 'v':
  754. vflag = true;
  755. break;
  756. default:
  757. qemuio_command_usage(&readv_cmd);
  758. return -EINVAL;
  759. }
  760. }
  761. if (optind > argc - 2) {
  762. qemuio_command_usage(&readv_cmd);
  763. return -EINVAL;
  764. }
  765. offset = cvtnum(argv[optind]);
  766. if (offset < 0) {
  767. print_cvtnum_err(offset, argv[optind]);
  768. return offset;
  769. }
  770. optind++;
  771. nr_iov = argc - optind;
  772. buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, 0xab);
  773. if (buf == NULL) {
  774. return -EINVAL;
  775. }
  776. gettimeofday(&t1, NULL);
  777. ret = do_aio_readv(blk, &qiov, offset, &total);
  778. gettimeofday(&t2, NULL);
  779. if (ret < 0) {
  780. printf("readv failed: %s\n", strerror(-ret));
  781. goto out;
  782. }
  783. cnt = ret;
  784. ret = 0;
  785. if (Pflag) {
  786. void *cmp_buf = g_malloc(qiov.size);
  787. memset(cmp_buf, pattern, qiov.size);
  788. if (memcmp(buf, cmp_buf, qiov.size)) {
  789. printf("Pattern verification failed at offset %"
  790. PRId64 ", %zd bytes\n", offset, qiov.size);
  791. ret = -EINVAL;
  792. }
  793. g_free(cmp_buf);
  794. }
  795. if (qflag) {
  796. goto out;
  797. }
  798. if (vflag) {
  799. dump_buffer(buf, offset, qiov.size);
  800. }
  801. /* Finally, report back -- -C gives a parsable format */
  802. t2 = tsub(t2, t1);
  803. print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
  804. out:
  805. qemu_iovec_destroy(&qiov);
  806. qemu_io_free(buf);
  807. return ret;
  808. }
  809. static void write_help(void)
  810. {
  811. printf(
  812. "\n"
  813. " writes a range of bytes from the given offset\n"
  814. "\n"
  815. " Example:\n"
  816. " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
  817. "\n"
  818. " Writes into a segment of the currently open file, using a buffer\n"
  819. " filled with a set pattern (0xcdcdcdcd).\n"
  820. " -b, -- write to the VM state rather than the virtual disk\n"
  821. " -c, -- write compressed data with blk_write_compressed\n"
  822. " -f, -- use Force Unit Access semantics\n"
  823. " -p, -- ignored for backwards compatibility\n"
  824. " -P, -- use different pattern to fill file\n"
  825. " -C, -- report statistics in a machine parsable format\n"
  826. " -q, -- quiet mode, do not show I/O statistics\n"
  827. " -u, -- with -z, allow unmapping\n"
  828. " -z, -- write zeroes using blk_co_pwrite_zeroes\n"
  829. "\n");
  830. }
  831. static int write_f(BlockBackend *blk, int argc, char **argv);
  832. static const cmdinfo_t write_cmd = {
  833. .name = "write",
  834. .altname = "w",
  835. .cfunc = write_f,
  836. .perm = BLK_PERM_WRITE,
  837. .argmin = 2,
  838. .argmax = -1,
  839. .args = "[-bcCfquz] [-P pattern] off len",
  840. .oneline = "writes a number of bytes at a specified offset",
  841. .help = write_help,
  842. };
  843. static int write_f(BlockBackend *blk, int argc, char **argv)
  844. {
  845. struct timeval t1, t2;
  846. bool Cflag = false, qflag = false, bflag = false;
  847. bool Pflag = false, zflag = false, cflag = false;
  848. int flags = 0;
  849. int c, cnt, ret;
  850. char *buf = NULL;
  851. int64_t offset;
  852. int64_t count;
  853. /* Some compilers get confused and warn if this is not initialized. */
  854. int64_t total = 0;
  855. int pattern = 0xcd;
  856. while ((c = getopt(argc, argv, "bcCfpP:quz")) != -1) {
  857. switch (c) {
  858. case 'b':
  859. bflag = true;
  860. break;
  861. case 'c':
  862. cflag = true;
  863. break;
  864. case 'C':
  865. Cflag = true;
  866. break;
  867. case 'f':
  868. flags |= BDRV_REQ_FUA;
  869. break;
  870. case 'p':
  871. /* Ignored for backwards compatibility */
  872. break;
  873. case 'P':
  874. Pflag = true;
  875. pattern = parse_pattern(optarg);
  876. if (pattern < 0) {
  877. return -EINVAL;
  878. }
  879. break;
  880. case 'q':
  881. qflag = true;
  882. break;
  883. case 'u':
  884. flags |= BDRV_REQ_MAY_UNMAP;
  885. break;
  886. case 'z':
  887. zflag = true;
  888. break;
  889. default:
  890. qemuio_command_usage(&write_cmd);
  891. return -EINVAL;
  892. }
  893. }
  894. if (optind != argc - 2) {
  895. qemuio_command_usage(&write_cmd);
  896. return -EINVAL;
  897. }
  898. if (bflag && zflag) {
  899. printf("-b and -z cannot be specified at the same time\n");
  900. return -EINVAL;
  901. }
  902. if ((flags & BDRV_REQ_FUA) && (bflag || cflag)) {
  903. printf("-f and -b or -c cannot be specified at the same time\n");
  904. return -EINVAL;
  905. }
  906. if ((flags & BDRV_REQ_MAY_UNMAP) && !zflag) {
  907. printf("-u requires -z to be specified\n");
  908. return -EINVAL;
  909. }
  910. if (zflag && Pflag) {
  911. printf("-z and -P cannot be specified at the same time\n");
  912. return -EINVAL;
  913. }
  914. offset = cvtnum(argv[optind]);
  915. if (offset < 0) {
  916. print_cvtnum_err(offset, argv[optind]);
  917. return offset;
  918. }
  919. optind++;
  920. count = cvtnum(argv[optind]);
  921. if (count < 0) {
  922. print_cvtnum_err(count, argv[optind]);
  923. return count;
  924. } else if (count > BDRV_REQUEST_MAX_BYTES) {
  925. printf("length cannot exceed %" PRIu64 ", given %s\n",
  926. (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
  927. return -EINVAL;
  928. }
  929. if (bflag || cflag) {
  930. if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) {
  931. printf("%" PRId64 " is not a sector-aligned value for 'offset'\n",
  932. offset);
  933. return -EINVAL;
  934. }
  935. if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) {
  936. printf("%"PRId64" is not a sector-aligned value for 'count'\n",
  937. count);
  938. return -EINVAL;
  939. }
  940. }
  941. if (!zflag) {
  942. buf = qemu_io_alloc(blk, count, pattern);
  943. }
  944. gettimeofday(&t1, NULL);
  945. if (bflag) {
  946. ret = do_save_vmstate(blk, buf, offset, count, &total);
  947. } else if (zflag) {
  948. ret = do_co_pwrite_zeroes(blk, offset, count, flags, &total);
  949. } else if (cflag) {
  950. ret = do_write_compressed(blk, buf, offset, count, &total);
  951. } else {
  952. ret = do_pwrite(blk, buf, offset, count, flags, &total);
  953. }
  954. gettimeofday(&t2, NULL);
  955. if (ret < 0) {
  956. printf("write failed: %s\n", strerror(-ret));
  957. goto out;
  958. }
  959. cnt = ret;
  960. ret = 0;
  961. if (qflag) {
  962. goto out;
  963. }
  964. /* Finally, report back -- -C gives a parsable format */
  965. t2 = tsub(t2, t1);
  966. print_report("wrote", &t2, offset, count, total, cnt, Cflag);
  967. out:
  968. if (!zflag) {
  969. qemu_io_free(buf);
  970. }
  971. return ret;
  972. }
  973. static void
  974. writev_help(void)
  975. {
  976. printf(
  977. "\n"
  978. " writes a range of bytes from the given offset source from multiple buffers\n"
  979. "\n"
  980. " Example:\n"
  981. " 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
  982. "\n"
  983. " Writes into a segment of the currently open file, using a buffer\n"
  984. " filled with a set pattern (0xcdcdcdcd).\n"
  985. " -P, -- use different pattern to fill file\n"
  986. " -C, -- report statistics in a machine parsable format\n"
  987. " -f, -- use Force Unit Access semantics\n"
  988. " -q, -- quiet mode, do not show I/O statistics\n"
  989. "\n");
  990. }
  991. static int writev_f(BlockBackend *blk, int argc, char **argv);
  992. static const cmdinfo_t writev_cmd = {
  993. .name = "writev",
  994. .cfunc = writev_f,
  995. .perm = BLK_PERM_WRITE,
  996. .argmin = 2,
  997. .argmax = -1,
  998. .args = "[-Cfq] [-P pattern] off len [len..]",
  999. .oneline = "writes a number of bytes at a specified offset",
  1000. .help = writev_help,
  1001. };
  1002. static int writev_f(BlockBackend *blk, int argc, char **argv)
  1003. {
  1004. struct timeval t1, t2;
  1005. bool Cflag = false, qflag = false;
  1006. int flags = 0;
  1007. int c, cnt, ret;
  1008. char *buf;
  1009. int64_t offset;
  1010. /* Some compilers get confused and warn if this is not initialized. */
  1011. int total = 0;
  1012. int nr_iov;
  1013. int pattern = 0xcd;
  1014. QEMUIOVector qiov;
  1015. while ((c = getopt(argc, argv, "CfqP:")) != -1) {
  1016. switch (c) {
  1017. case 'C':
  1018. Cflag = true;
  1019. break;
  1020. case 'f':
  1021. flags |= BDRV_REQ_FUA;
  1022. break;
  1023. case 'q':
  1024. qflag = true;
  1025. break;
  1026. case 'P':
  1027. pattern = parse_pattern(optarg);
  1028. if (pattern < 0) {
  1029. return -EINVAL;
  1030. }
  1031. break;
  1032. default:
  1033. qemuio_command_usage(&writev_cmd);
  1034. return -EINVAL;
  1035. }
  1036. }
  1037. if (optind > argc - 2) {
  1038. qemuio_command_usage(&writev_cmd);
  1039. return -EINVAL;
  1040. }
  1041. offset = cvtnum(argv[optind]);
  1042. if (offset < 0) {
  1043. print_cvtnum_err(offset, argv[optind]);
  1044. return offset;
  1045. }
  1046. optind++;
  1047. nr_iov = argc - optind;
  1048. buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, pattern);
  1049. if (buf == NULL) {
  1050. return -EINVAL;
  1051. }
  1052. gettimeofday(&t1, NULL);
  1053. ret = do_aio_writev(blk, &qiov, offset, flags, &total);
  1054. gettimeofday(&t2, NULL);
  1055. if (ret < 0) {
  1056. printf("writev failed: %s\n", strerror(-ret));
  1057. goto out;
  1058. }
  1059. cnt = ret;
  1060. ret = 0;
  1061. if (qflag) {
  1062. goto out;
  1063. }
  1064. /* Finally, report back -- -C gives a parsable format */
  1065. t2 = tsub(t2, t1);
  1066. print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
  1067. out:
  1068. qemu_iovec_destroy(&qiov);
  1069. qemu_io_free(buf);
  1070. return ret;
  1071. }
  1072. struct aio_ctx {
  1073. BlockBackend *blk;
  1074. QEMUIOVector qiov;
  1075. int64_t offset;
  1076. char *buf;
  1077. bool qflag;
  1078. bool vflag;
  1079. bool Cflag;
  1080. bool Pflag;
  1081. bool zflag;
  1082. BlockAcctCookie acct;
  1083. int pattern;
  1084. struct timeval t1;
  1085. };
  1086. static void aio_write_done(void *opaque, int ret)
  1087. {
  1088. struct aio_ctx *ctx = opaque;
  1089. struct timeval t2;
  1090. gettimeofday(&t2, NULL);
  1091. if (ret < 0) {
  1092. printf("aio_write failed: %s\n", strerror(-ret));
  1093. block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
  1094. goto out;
  1095. }
  1096. block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
  1097. if (ctx->qflag) {
  1098. goto out;
  1099. }
  1100. /* Finally, report back -- -C gives a parsable format */
  1101. t2 = tsub(t2, ctx->t1);
  1102. print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
  1103. ctx->qiov.size, 1, ctx->Cflag);
  1104. out:
  1105. if (!ctx->zflag) {
  1106. qemu_io_free(ctx->buf);
  1107. qemu_iovec_destroy(&ctx->qiov);
  1108. }
  1109. g_free(ctx);
  1110. }
  1111. static void aio_read_done(void *opaque, int ret)
  1112. {
  1113. struct aio_ctx *ctx = opaque;
  1114. struct timeval t2;
  1115. gettimeofday(&t2, NULL);
  1116. if (ret < 0) {
  1117. printf("readv failed: %s\n", strerror(-ret));
  1118. block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
  1119. goto out;
  1120. }
  1121. if (ctx->Pflag) {
  1122. void *cmp_buf = g_malloc(ctx->qiov.size);
  1123. memset(cmp_buf, ctx->pattern, ctx->qiov.size);
  1124. if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
  1125. printf("Pattern verification failed at offset %"
  1126. PRId64 ", %zd bytes\n", ctx->offset, ctx->qiov.size);
  1127. }
  1128. g_free(cmp_buf);
  1129. }
  1130. block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
  1131. if (ctx->qflag) {
  1132. goto out;
  1133. }
  1134. if (ctx->vflag) {
  1135. dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
  1136. }
  1137. /* Finally, report back -- -C gives a parsable format */
  1138. t2 = tsub(t2, ctx->t1);
  1139. print_report("read", &t2, ctx->offset, ctx->qiov.size,
  1140. ctx->qiov.size, 1, ctx->Cflag);
  1141. out:
  1142. qemu_io_free(ctx->buf);
  1143. qemu_iovec_destroy(&ctx->qiov);
  1144. g_free(ctx);
  1145. }
  1146. static void aio_read_help(void)
  1147. {
  1148. printf(
  1149. "\n"
  1150. " asynchronously reads a range of bytes from the given offset\n"
  1151. "\n"
  1152. " Example:\n"
  1153. " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
  1154. "\n"
  1155. " Reads a segment of the currently open file, optionally dumping it to the\n"
  1156. " standard output stream (with -v option) for subsequent inspection.\n"
  1157. " The read is performed asynchronously and the aio_flush command must be\n"
  1158. " used to ensure all outstanding aio requests have been completed.\n"
  1159. " Note that due to its asynchronous nature, this command will be\n"
  1160. " considered successful once the request is submitted, independently\n"
  1161. " of potential I/O errors or pattern mismatches.\n"
  1162. " -C, -- report statistics in a machine parsable format\n"
  1163. " -P, -- use a pattern to verify read data\n"
  1164. " -i, -- treat request as invalid, for exercising stats\n"
  1165. " -v, -- dump buffer to standard output\n"
  1166. " -q, -- quiet mode, do not show I/O statistics\n"
  1167. "\n");
  1168. }
  1169. static int aio_read_f(BlockBackend *blk, int argc, char **argv);
  1170. static const cmdinfo_t aio_read_cmd = {
  1171. .name = "aio_read",
  1172. .cfunc = aio_read_f,
  1173. .argmin = 2,
  1174. .argmax = -1,
  1175. .args = "[-Ciqv] [-P pattern] off len [len..]",
  1176. .oneline = "asynchronously reads a number of bytes",
  1177. .help = aio_read_help,
  1178. };
  1179. static int aio_read_f(BlockBackend *blk, int argc, char **argv)
  1180. {
  1181. int nr_iov, c;
  1182. struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
  1183. ctx->blk = blk;
  1184. while ((c = getopt(argc, argv, "CP:iqv")) != -1) {
  1185. switch (c) {
  1186. case 'C':
  1187. ctx->Cflag = true;
  1188. break;
  1189. case 'P':
  1190. ctx->Pflag = true;
  1191. ctx->pattern = parse_pattern(optarg);
  1192. if (ctx->pattern < 0) {
  1193. g_free(ctx);
  1194. return -EINVAL;
  1195. }
  1196. break;
  1197. case 'i':
  1198. printf("injecting invalid read request\n");
  1199. block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
  1200. g_free(ctx);
  1201. return 0;
  1202. case 'q':
  1203. ctx->qflag = true;
  1204. break;
  1205. case 'v':
  1206. ctx->vflag = true;
  1207. break;
  1208. default:
  1209. g_free(ctx);
  1210. qemuio_command_usage(&aio_read_cmd);
  1211. return -EINVAL;
  1212. }
  1213. }
  1214. if (optind > argc - 2) {
  1215. g_free(ctx);
  1216. qemuio_command_usage(&aio_read_cmd);
  1217. return -EINVAL;
  1218. }
  1219. ctx->offset = cvtnum(argv[optind]);
  1220. if (ctx->offset < 0) {
  1221. int ret = ctx->offset;
  1222. print_cvtnum_err(ret, argv[optind]);
  1223. g_free(ctx);
  1224. return ret;
  1225. }
  1226. optind++;
  1227. nr_iov = argc - optind;
  1228. ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, 0xab);
  1229. if (ctx->buf == NULL) {
  1230. block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
  1231. g_free(ctx);
  1232. return -EINVAL;
  1233. }
  1234. gettimeofday(&ctx->t1, NULL);
  1235. block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
  1236. BLOCK_ACCT_READ);
  1237. blk_aio_preadv(blk, ctx->offset, &ctx->qiov, 0, aio_read_done, ctx);
  1238. return 0;
  1239. }
  1240. static void aio_write_help(void)
  1241. {
  1242. printf(
  1243. "\n"
  1244. " asynchronously writes a range of bytes from the given offset source\n"
  1245. " from multiple buffers\n"
  1246. "\n"
  1247. " Example:\n"
  1248. " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
  1249. "\n"
  1250. " Writes into a segment of the currently open file, using a buffer\n"
  1251. " filled with a set pattern (0xcdcdcdcd).\n"
  1252. " The write is performed asynchronously and the aio_flush command must be\n"
  1253. " used to ensure all outstanding aio requests have been completed.\n"
  1254. " Note that due to its asynchronous nature, this command will be\n"
  1255. " considered successful once the request is submitted, independently\n"
  1256. " of potential I/O errors or pattern mismatches.\n"
  1257. " -P, -- use different pattern to fill file\n"
  1258. " -C, -- report statistics in a machine parsable format\n"
  1259. " -f, -- use Force Unit Access semantics\n"
  1260. " -i, -- treat request as invalid, for exercising stats\n"
  1261. " -q, -- quiet mode, do not show I/O statistics\n"
  1262. " -u, -- with -z, allow unmapping\n"
  1263. " -z, -- write zeroes using blk_aio_pwrite_zeroes\n"
  1264. "\n");
  1265. }
  1266. static int aio_write_f(BlockBackend *blk, int argc, char **argv);
  1267. static const cmdinfo_t aio_write_cmd = {
  1268. .name = "aio_write",
  1269. .cfunc = aio_write_f,
  1270. .perm = BLK_PERM_WRITE,
  1271. .argmin = 2,
  1272. .argmax = -1,
  1273. .args = "[-Cfiquz] [-P pattern] off len [len..]",
  1274. .oneline = "asynchronously writes a number of bytes",
  1275. .help = aio_write_help,
  1276. };
  1277. static int aio_write_f(BlockBackend *blk, int argc, char **argv)
  1278. {
  1279. int nr_iov, c;
  1280. int pattern = 0xcd;
  1281. struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
  1282. int flags = 0;
  1283. ctx->blk = blk;
  1284. while ((c = getopt(argc, argv, "CfiqP:uz")) != -1) {
  1285. switch (c) {
  1286. case 'C':
  1287. ctx->Cflag = true;
  1288. break;
  1289. case 'f':
  1290. flags |= BDRV_REQ_FUA;
  1291. break;
  1292. case 'q':
  1293. ctx->qflag = true;
  1294. break;
  1295. case 'u':
  1296. flags |= BDRV_REQ_MAY_UNMAP;
  1297. break;
  1298. case 'P':
  1299. pattern = parse_pattern(optarg);
  1300. if (pattern < 0) {
  1301. g_free(ctx);
  1302. return -EINVAL;
  1303. }
  1304. break;
  1305. case 'i':
  1306. printf("injecting invalid write request\n");
  1307. block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
  1308. g_free(ctx);
  1309. return 0;
  1310. case 'z':
  1311. ctx->zflag = true;
  1312. break;
  1313. default:
  1314. g_free(ctx);
  1315. qemuio_command_usage(&aio_write_cmd);
  1316. return -EINVAL;
  1317. }
  1318. }
  1319. if (optind > argc - 2) {
  1320. g_free(ctx);
  1321. qemuio_command_usage(&aio_write_cmd);
  1322. return -EINVAL;
  1323. }
  1324. if (ctx->zflag && optind != argc - 2) {
  1325. printf("-z supports only a single length parameter\n");
  1326. g_free(ctx);
  1327. return -EINVAL;
  1328. }
  1329. if ((flags & BDRV_REQ_MAY_UNMAP) && !ctx->zflag) {
  1330. printf("-u requires -z to be specified\n");
  1331. g_free(ctx);
  1332. return -EINVAL;
  1333. }
  1334. if (ctx->zflag && ctx->Pflag) {
  1335. printf("-z and -P cannot be specified at the same time\n");
  1336. g_free(ctx);
  1337. return -EINVAL;
  1338. }
  1339. ctx->offset = cvtnum(argv[optind]);
  1340. if (ctx->offset < 0) {
  1341. int ret = ctx->offset;
  1342. print_cvtnum_err(ret, argv[optind]);
  1343. g_free(ctx);
  1344. return ret;
  1345. }
  1346. optind++;
  1347. if (ctx->zflag) {
  1348. int64_t count = cvtnum(argv[optind]);
  1349. if (count < 0) {
  1350. print_cvtnum_err(count, argv[optind]);
  1351. g_free(ctx);
  1352. return count;
  1353. }
  1354. ctx->qiov.size = count;
  1355. blk_aio_pwrite_zeroes(blk, ctx->offset, count, flags, aio_write_done,
  1356. ctx);
  1357. } else {
  1358. nr_iov = argc - optind;
  1359. ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov,
  1360. pattern);
  1361. if (ctx->buf == NULL) {
  1362. block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
  1363. g_free(ctx);
  1364. return -EINVAL;
  1365. }
  1366. gettimeofday(&ctx->t1, NULL);
  1367. block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
  1368. BLOCK_ACCT_WRITE);
  1369. blk_aio_pwritev(blk, ctx->offset, &ctx->qiov, flags, aio_write_done,
  1370. ctx);
  1371. }
  1372. return 0;
  1373. }
  1374. static int aio_flush_f(BlockBackend *blk, int argc, char **argv)
  1375. {
  1376. BlockAcctCookie cookie;
  1377. block_acct_start(blk_get_stats(blk), &cookie, 0, BLOCK_ACCT_FLUSH);
  1378. blk_drain_all();
  1379. block_acct_done(blk_get_stats(blk), &cookie);
  1380. return 0;
  1381. }
  1382. static const cmdinfo_t aio_flush_cmd = {
  1383. .name = "aio_flush",
  1384. .cfunc = aio_flush_f,
  1385. .oneline = "completes all outstanding aio requests"
  1386. };
  1387. static int flush_f(BlockBackend *blk, int argc, char **argv)
  1388. {
  1389. return blk_flush(blk);
  1390. }
  1391. static const cmdinfo_t flush_cmd = {
  1392. .name = "flush",
  1393. .altname = "f",
  1394. .cfunc = flush_f,
  1395. .oneline = "flush all in-core file state to disk",
  1396. };
  1397. static int truncate_f(BlockBackend *blk, int argc, char **argv)
  1398. {
  1399. Error *local_err = NULL;
  1400. int64_t offset;
  1401. int ret;
  1402. offset = cvtnum(argv[1]);
  1403. if (offset < 0) {
  1404. print_cvtnum_err(offset, argv[1]);
  1405. return offset;
  1406. }
  1407. ret = blk_truncate(blk, offset, PREALLOC_MODE_OFF, &local_err);
  1408. if (ret < 0) {
  1409. error_report_err(local_err);
  1410. return ret;
  1411. }
  1412. return 0;
  1413. }
  1414. static const cmdinfo_t truncate_cmd = {
  1415. .name = "truncate",
  1416. .altname = "t",
  1417. .cfunc = truncate_f,
  1418. .perm = BLK_PERM_WRITE | BLK_PERM_RESIZE,
  1419. .argmin = 1,
  1420. .argmax = 1,
  1421. .args = "off",
  1422. .oneline = "truncates the current file at the given offset",
  1423. };
  1424. static int length_f(BlockBackend *blk, int argc, char **argv)
  1425. {
  1426. int64_t size;
  1427. char s1[64];
  1428. size = blk_getlength(blk);
  1429. if (size < 0) {
  1430. printf("getlength: %s\n", strerror(-size));
  1431. return size;
  1432. }
  1433. cvtstr(size, s1, sizeof(s1));
  1434. printf("%s\n", s1);
  1435. return 0;
  1436. }
  1437. static const cmdinfo_t length_cmd = {
  1438. .name = "length",
  1439. .altname = "l",
  1440. .cfunc = length_f,
  1441. .oneline = "gets the length of the current file",
  1442. };
  1443. static int info_f(BlockBackend *blk, int argc, char **argv)
  1444. {
  1445. BlockDriverState *bs = blk_bs(blk);
  1446. BlockDriverInfo bdi;
  1447. ImageInfoSpecific *spec_info;
  1448. char s1[64], s2[64];
  1449. int ret;
  1450. if (bs->drv && bs->drv->format_name) {
  1451. printf("format name: %s\n", bs->drv->format_name);
  1452. }
  1453. if (bs->drv && bs->drv->protocol_name) {
  1454. printf("format name: %s\n", bs->drv->protocol_name);
  1455. }
  1456. ret = bdrv_get_info(bs, &bdi);
  1457. if (ret) {
  1458. return ret;
  1459. }
  1460. cvtstr(bdi.cluster_size, s1, sizeof(s1));
  1461. cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
  1462. printf("cluster size: %s\n", s1);
  1463. printf("vm state offset: %s\n", s2);
  1464. spec_info = bdrv_get_specific_info(bs);
  1465. if (spec_info) {
  1466. printf("Format specific information:\n");
  1467. bdrv_image_info_specific_dump(fprintf, stdout, spec_info);
  1468. qapi_free_ImageInfoSpecific(spec_info);
  1469. }
  1470. return 0;
  1471. }
  1472. static const cmdinfo_t info_cmd = {
  1473. .name = "info",
  1474. .altname = "i",
  1475. .cfunc = info_f,
  1476. .oneline = "prints information about the current file",
  1477. };
  1478. static void discard_help(void)
  1479. {
  1480. printf(
  1481. "\n"
  1482. " discards a range of bytes from the given offset\n"
  1483. "\n"
  1484. " Example:\n"
  1485. " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
  1486. "\n"
  1487. " Discards a segment of the currently open file.\n"
  1488. " -C, -- report statistics in a machine parsable format\n"
  1489. " -q, -- quiet mode, do not show I/O statistics\n"
  1490. "\n");
  1491. }
  1492. static int discard_f(BlockBackend *blk, int argc, char **argv);
  1493. static const cmdinfo_t discard_cmd = {
  1494. .name = "discard",
  1495. .altname = "d",
  1496. .cfunc = discard_f,
  1497. .perm = BLK_PERM_WRITE,
  1498. .argmin = 2,
  1499. .argmax = -1,
  1500. .args = "[-Cq] off len",
  1501. .oneline = "discards a number of bytes at a specified offset",
  1502. .help = discard_help,
  1503. };
  1504. static int discard_f(BlockBackend *blk, int argc, char **argv)
  1505. {
  1506. struct timeval t1, t2;
  1507. bool Cflag = false, qflag = false;
  1508. int c, ret;
  1509. int64_t offset, bytes;
  1510. while ((c = getopt(argc, argv, "Cq")) != -1) {
  1511. switch (c) {
  1512. case 'C':
  1513. Cflag = true;
  1514. break;
  1515. case 'q':
  1516. qflag = true;
  1517. break;
  1518. default:
  1519. qemuio_command_usage(&discard_cmd);
  1520. return -EINVAL;
  1521. }
  1522. }
  1523. if (optind != argc - 2) {
  1524. qemuio_command_usage(&discard_cmd);
  1525. return -EINVAL;
  1526. }
  1527. offset = cvtnum(argv[optind]);
  1528. if (offset < 0) {
  1529. print_cvtnum_err(offset, argv[optind]);
  1530. return offset;
  1531. }
  1532. optind++;
  1533. bytes = cvtnum(argv[optind]);
  1534. if (bytes < 0) {
  1535. print_cvtnum_err(bytes, argv[optind]);
  1536. return bytes;
  1537. } else if (bytes >> BDRV_SECTOR_BITS > BDRV_REQUEST_MAX_SECTORS) {
  1538. printf("length cannot exceed %"PRIu64", given %s\n",
  1539. (uint64_t)BDRV_REQUEST_MAX_SECTORS << BDRV_SECTOR_BITS,
  1540. argv[optind]);
  1541. return -EINVAL;
  1542. }
  1543. gettimeofday(&t1, NULL);
  1544. ret = blk_pdiscard(blk, offset, bytes);
  1545. gettimeofday(&t2, NULL);
  1546. if (ret < 0) {
  1547. printf("discard failed: %s\n", strerror(-ret));
  1548. return ret;
  1549. }
  1550. /* Finally, report back -- -C gives a parsable format */
  1551. if (!qflag) {
  1552. t2 = tsub(t2, t1);
  1553. print_report("discard", &t2, offset, bytes, bytes, 1, Cflag);
  1554. }
  1555. return 0;
  1556. }
  1557. static int alloc_f(BlockBackend *blk, int argc, char **argv)
  1558. {
  1559. BlockDriverState *bs = blk_bs(blk);
  1560. int64_t offset, start, remaining, count;
  1561. char s1[64];
  1562. int ret;
  1563. int64_t num, sum_alloc;
  1564. start = offset = cvtnum(argv[1]);
  1565. if (offset < 0) {
  1566. print_cvtnum_err(offset, argv[1]);
  1567. return offset;
  1568. }
  1569. if (argc == 3) {
  1570. count = cvtnum(argv[2]);
  1571. if (count < 0) {
  1572. print_cvtnum_err(count, argv[2]);
  1573. return count;
  1574. }
  1575. } else {
  1576. count = BDRV_SECTOR_SIZE;
  1577. }
  1578. remaining = count;
  1579. sum_alloc = 0;
  1580. while (remaining) {
  1581. ret = bdrv_is_allocated(bs, offset, remaining, &num);
  1582. if (ret < 0) {
  1583. printf("is_allocated failed: %s\n", strerror(-ret));
  1584. return ret;
  1585. }
  1586. offset += num;
  1587. remaining -= num;
  1588. if (ret) {
  1589. sum_alloc += num;
  1590. }
  1591. if (num == 0) {
  1592. count -= remaining;
  1593. remaining = 0;
  1594. }
  1595. }
  1596. cvtstr(start, s1, sizeof(s1));
  1597. printf("%"PRId64"/%"PRId64" bytes allocated at offset %s\n",
  1598. sum_alloc, count, s1);
  1599. return 0;
  1600. }
  1601. static const cmdinfo_t alloc_cmd = {
  1602. .name = "alloc",
  1603. .altname = "a",
  1604. .argmin = 1,
  1605. .argmax = 2,
  1606. .cfunc = alloc_f,
  1607. .args = "offset [count]",
  1608. .oneline = "checks if offset is allocated in the file",
  1609. };
  1610. static int map_is_allocated(BlockDriverState *bs, int64_t offset,
  1611. int64_t bytes, int64_t *pnum)
  1612. {
  1613. int64_t num;
  1614. int num_checked;
  1615. int ret, firstret;
  1616. num_checked = MIN(bytes, BDRV_REQUEST_MAX_BYTES);
  1617. ret = bdrv_is_allocated(bs, offset, num_checked, &num);
  1618. if (ret < 0) {
  1619. return ret;
  1620. }
  1621. firstret = ret;
  1622. *pnum = num;
  1623. while (bytes > 0 && ret == firstret) {
  1624. offset += num;
  1625. bytes -= num;
  1626. num_checked = MIN(bytes, BDRV_REQUEST_MAX_BYTES);
  1627. ret = bdrv_is_allocated(bs, offset, num_checked, &num);
  1628. if (ret == firstret && num) {
  1629. *pnum += num;
  1630. } else {
  1631. break;
  1632. }
  1633. }
  1634. return firstret;
  1635. }
  1636. static int map_f(BlockBackend *blk, int argc, char **argv)
  1637. {
  1638. int64_t offset, bytes;
  1639. char s1[64], s2[64];
  1640. int64_t num;
  1641. int ret;
  1642. const char *retstr;
  1643. offset = 0;
  1644. bytes = blk_getlength(blk);
  1645. if (bytes < 0) {
  1646. error_report("Failed to query image length: %s", strerror(-bytes));
  1647. return bytes;
  1648. }
  1649. while (bytes) {
  1650. ret = map_is_allocated(blk_bs(blk), offset, bytes, &num);
  1651. if (ret < 0) {
  1652. error_report("Failed to get allocation status: %s", strerror(-ret));
  1653. return ret;
  1654. } else if (!num) {
  1655. error_report("Unexpected end of image");
  1656. return -EIO;
  1657. }
  1658. retstr = ret ? " allocated" : "not allocated";
  1659. cvtstr(num, s1, sizeof(s1));
  1660. cvtstr(offset, s2, sizeof(s2));
  1661. printf("%s (0x%" PRIx64 ") bytes %s at offset %s (0x%" PRIx64 ")\n",
  1662. s1, num, retstr, s2, offset);
  1663. offset += num;
  1664. bytes -= num;
  1665. }
  1666. return 0;
  1667. }
  1668. static const cmdinfo_t map_cmd = {
  1669. .name = "map",
  1670. .argmin = 0,
  1671. .argmax = 0,
  1672. .cfunc = map_f,
  1673. .args = "",
  1674. .oneline = "prints the allocated areas of a file",
  1675. };
  1676. static void reopen_help(void)
  1677. {
  1678. printf(
  1679. "\n"
  1680. " Changes the open options of an already opened image\n"
  1681. "\n"
  1682. " Example:\n"
  1683. " 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
  1684. "\n"
  1685. " -r, -- Reopen the image read-only\n"
  1686. " -w, -- Reopen the image read-write\n"
  1687. " -c, -- Change the cache mode to the given value\n"
  1688. " -o, -- Changes block driver options (cf. 'open' command)\n"
  1689. "\n");
  1690. }
  1691. static int reopen_f(BlockBackend *blk, int argc, char **argv);
  1692. static QemuOptsList reopen_opts = {
  1693. .name = "reopen",
  1694. .merge_lists = true,
  1695. .head = QTAILQ_HEAD_INITIALIZER(reopen_opts.head),
  1696. .desc = {
  1697. /* no elements => accept any params */
  1698. { /* end of list */ }
  1699. },
  1700. };
  1701. static const cmdinfo_t reopen_cmd = {
  1702. .name = "reopen",
  1703. .argmin = 0,
  1704. .argmax = -1,
  1705. .cfunc = reopen_f,
  1706. .args = "[(-r|-w)] [-c cache] [-o options]",
  1707. .oneline = "reopens an image with new options",
  1708. .help = reopen_help,
  1709. };
  1710. static int reopen_f(BlockBackend *blk, int argc, char **argv)
  1711. {
  1712. BlockDriverState *bs = blk_bs(blk);
  1713. QemuOpts *qopts;
  1714. QDict *opts;
  1715. int c;
  1716. int flags = bs->open_flags;
  1717. bool writethrough = !blk_enable_write_cache(blk);
  1718. bool has_rw_option = false;
  1719. BlockReopenQueue *brq;
  1720. Error *local_err = NULL;
  1721. while ((c = getopt(argc, argv, "c:o:rw")) != -1) {
  1722. switch (c) {
  1723. case 'c':
  1724. if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
  1725. error_report("Invalid cache option: %s", optarg);
  1726. return -EINVAL;
  1727. }
  1728. break;
  1729. case 'o':
  1730. if (!qemu_opts_parse_noisily(&reopen_opts, optarg, 0)) {
  1731. qemu_opts_reset(&reopen_opts);
  1732. return -EINVAL;
  1733. }
  1734. break;
  1735. case 'r':
  1736. if (has_rw_option) {
  1737. error_report("Only one -r/-w option may be given");
  1738. return -EINVAL;
  1739. }
  1740. flags &= ~BDRV_O_RDWR;
  1741. has_rw_option = true;
  1742. break;
  1743. case 'w':
  1744. if (has_rw_option) {
  1745. error_report("Only one -r/-w option may be given");
  1746. return -EINVAL;
  1747. }
  1748. flags |= BDRV_O_RDWR;
  1749. has_rw_option = true;
  1750. break;
  1751. default:
  1752. qemu_opts_reset(&reopen_opts);
  1753. qemuio_command_usage(&reopen_cmd);
  1754. return -EINVAL;
  1755. }
  1756. }
  1757. if (optind != argc) {
  1758. qemu_opts_reset(&reopen_opts);
  1759. qemuio_command_usage(&reopen_cmd);
  1760. return -EINVAL;
  1761. }
  1762. if (!writethrough != blk_enable_write_cache(blk) &&
  1763. blk_get_attached_dev(blk))
  1764. {
  1765. error_report("Cannot change cache.writeback: Device attached");
  1766. qemu_opts_reset(&reopen_opts);
  1767. return -EBUSY;
  1768. }
  1769. if (!(flags & BDRV_O_RDWR)) {
  1770. uint64_t orig_perm, orig_shared_perm;
  1771. bdrv_drain(bs);
  1772. blk_get_perm(blk, &orig_perm, &orig_shared_perm);
  1773. blk_set_perm(blk,
  1774. orig_perm & ~(BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED),
  1775. orig_shared_perm,
  1776. &error_abort);
  1777. }
  1778. qopts = qemu_opts_find(&reopen_opts, NULL);
  1779. opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
  1780. qemu_opts_reset(&reopen_opts);
  1781. bdrv_subtree_drained_begin(bs);
  1782. brq = bdrv_reopen_queue(NULL, bs, opts, flags);
  1783. bdrv_reopen_multiple(bdrv_get_aio_context(bs), brq, &local_err);
  1784. bdrv_subtree_drained_end(bs);
  1785. if (local_err) {
  1786. error_report_err(local_err);
  1787. return -EINVAL;
  1788. }
  1789. blk_set_enable_write_cache(blk, !writethrough);
  1790. return 0;
  1791. }
  1792. static int break_f(BlockBackend *blk, int argc, char **argv)
  1793. {
  1794. int ret;
  1795. ret = bdrv_debug_breakpoint(blk_bs(blk), argv[1], argv[2]);
  1796. if (ret < 0) {
  1797. printf("Could not set breakpoint: %s\n", strerror(-ret));
  1798. return ret;
  1799. }
  1800. return 0;
  1801. }
  1802. static int remove_break_f(BlockBackend *blk, int argc, char **argv)
  1803. {
  1804. int ret;
  1805. ret = bdrv_debug_remove_breakpoint(blk_bs(blk), argv[1]);
  1806. if (ret < 0) {
  1807. printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret));
  1808. return ret;
  1809. }
  1810. return 0;
  1811. }
  1812. static const cmdinfo_t break_cmd = {
  1813. .name = "break",
  1814. .argmin = 2,
  1815. .argmax = 2,
  1816. .cfunc = break_f,
  1817. .args = "event tag",
  1818. .oneline = "sets a breakpoint on event and tags the stopped "
  1819. "request as tag",
  1820. };
  1821. static const cmdinfo_t remove_break_cmd = {
  1822. .name = "remove_break",
  1823. .argmin = 1,
  1824. .argmax = 1,
  1825. .cfunc = remove_break_f,
  1826. .args = "tag",
  1827. .oneline = "remove a breakpoint by tag",
  1828. };
  1829. static int resume_f(BlockBackend *blk, int argc, char **argv)
  1830. {
  1831. int ret;
  1832. ret = bdrv_debug_resume(blk_bs(blk), argv[1]);
  1833. if (ret < 0) {
  1834. printf("Could not resume request: %s\n", strerror(-ret));
  1835. return ret;
  1836. }
  1837. return 0;
  1838. }
  1839. static const cmdinfo_t resume_cmd = {
  1840. .name = "resume",
  1841. .argmin = 1,
  1842. .argmax = 1,
  1843. .cfunc = resume_f,
  1844. .args = "tag",
  1845. .oneline = "resumes the request tagged as tag",
  1846. };
  1847. static int wait_break_f(BlockBackend *blk, int argc, char **argv)
  1848. {
  1849. while (!bdrv_debug_is_suspended(blk_bs(blk), argv[1])) {
  1850. aio_poll(blk_get_aio_context(blk), true);
  1851. }
  1852. return 0;
  1853. }
  1854. static const cmdinfo_t wait_break_cmd = {
  1855. .name = "wait_break",
  1856. .argmin = 1,
  1857. .argmax = 1,
  1858. .cfunc = wait_break_f,
  1859. .args = "tag",
  1860. .oneline = "waits for the suspension of a request",
  1861. };
  1862. static int abort_f(BlockBackend *blk, int argc, char **argv)
  1863. {
  1864. abort();
  1865. }
  1866. static const cmdinfo_t abort_cmd = {
  1867. .name = "abort",
  1868. .cfunc = abort_f,
  1869. .flags = CMD_NOFILE_OK,
  1870. .oneline = "simulate a program crash using abort(3)",
  1871. };
  1872. static void sigraise_help(void)
  1873. {
  1874. printf(
  1875. "\n"
  1876. " raises the given signal\n"
  1877. "\n"
  1878. " Example:\n"
  1879. " 'sigraise %i' - raises SIGTERM\n"
  1880. "\n"
  1881. " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
  1882. " given to sigraise.\n"
  1883. "\n", SIGTERM);
  1884. }
  1885. static int sigraise_f(BlockBackend *blk, int argc, char **argv);
  1886. static const cmdinfo_t sigraise_cmd = {
  1887. .name = "sigraise",
  1888. .cfunc = sigraise_f,
  1889. .argmin = 1,
  1890. .argmax = 1,
  1891. .flags = CMD_NOFILE_OK,
  1892. .args = "signal",
  1893. .oneline = "raises a signal",
  1894. .help = sigraise_help,
  1895. };
  1896. static int sigraise_f(BlockBackend *blk, int argc, char **argv)
  1897. {
  1898. int64_t sig = cvtnum(argv[1]);
  1899. if (sig < 0) {
  1900. print_cvtnum_err(sig, argv[1]);
  1901. return sig;
  1902. } else if (sig > NSIG) {
  1903. printf("signal argument '%s' is too large to be a valid signal\n",
  1904. argv[1]);
  1905. return -EINVAL;
  1906. }
  1907. /* Using raise() to kill this process does not necessarily flush all open
  1908. * streams. At least stdout and stderr (although the latter should be
  1909. * non-buffered anyway) should be flushed, though. */
  1910. fflush(stdout);
  1911. fflush(stderr);
  1912. raise(sig);
  1913. return 0;
  1914. }
  1915. static void sleep_cb(void *opaque)
  1916. {
  1917. bool *expired = opaque;
  1918. *expired = true;
  1919. }
  1920. static int sleep_f(BlockBackend *blk, int argc, char **argv)
  1921. {
  1922. char *endptr;
  1923. long ms;
  1924. struct QEMUTimer *timer;
  1925. bool expired = false;
  1926. ms = strtol(argv[1], &endptr, 0);
  1927. if (ms < 0 || *endptr != '\0') {
  1928. printf("%s is not a valid number\n", argv[1]);
  1929. return -EINVAL;
  1930. }
  1931. timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired);
  1932. timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms);
  1933. while (!expired) {
  1934. main_loop_wait(false);
  1935. }
  1936. timer_free(timer);
  1937. return 0;
  1938. }
  1939. static const cmdinfo_t sleep_cmd = {
  1940. .name = "sleep",
  1941. .argmin = 1,
  1942. .argmax = 1,
  1943. .cfunc = sleep_f,
  1944. .flags = CMD_NOFILE_OK,
  1945. .oneline = "waits for the given value in milliseconds",
  1946. };
  1947. static void help_oneline(const char *cmd, const cmdinfo_t *ct)
  1948. {
  1949. if (cmd) {
  1950. printf("%s ", cmd);
  1951. } else {
  1952. printf("%s ", ct->name);
  1953. if (ct->altname) {
  1954. printf("(or %s) ", ct->altname);
  1955. }
  1956. }
  1957. if (ct->args) {
  1958. printf("%s ", ct->args);
  1959. }
  1960. printf("-- %s\n", ct->oneline);
  1961. }
  1962. static void help_onecmd(const char *cmd, const cmdinfo_t *ct)
  1963. {
  1964. help_oneline(cmd, ct);
  1965. if (ct->help) {
  1966. ct->help();
  1967. }
  1968. }
  1969. static void help_all(void)
  1970. {
  1971. const cmdinfo_t *ct;
  1972. for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
  1973. help_oneline(ct->name, ct);
  1974. }
  1975. printf("\nUse 'help commandname' for extended help.\n");
  1976. }
  1977. static int help_f(BlockBackend *blk, int argc, char **argv)
  1978. {
  1979. const cmdinfo_t *ct;
  1980. if (argc == 1) {
  1981. help_all();
  1982. return 0;
  1983. }
  1984. ct = find_command(argv[1]);
  1985. if (ct == NULL) {
  1986. printf("command %s not found\n", argv[1]);
  1987. return -EINVAL;
  1988. }
  1989. help_onecmd(argv[1], ct);
  1990. return 0;
  1991. }
  1992. static const cmdinfo_t help_cmd = {
  1993. .name = "help",
  1994. .altname = "?",
  1995. .cfunc = help_f,
  1996. .argmin = 0,
  1997. .argmax = 1,
  1998. .flags = CMD_FLAG_GLOBAL,
  1999. .args = "[command]",
  2000. .oneline = "help for one or all commands",
  2001. };
  2002. int qemuio_command(BlockBackend *blk, const char *cmd)
  2003. {
  2004. AioContext *ctx;
  2005. char *input;
  2006. const cmdinfo_t *ct;
  2007. char **v;
  2008. int c;
  2009. int ret = 0;
  2010. input = g_strdup(cmd);
  2011. v = breakline(input, &c);
  2012. if (c) {
  2013. ct = find_command(v[0]);
  2014. if (ct) {
  2015. ctx = blk ? blk_get_aio_context(blk) : qemu_get_aio_context();
  2016. aio_context_acquire(ctx);
  2017. ret = command(blk, ct, c, v);
  2018. aio_context_release(ctx);
  2019. } else {
  2020. fprintf(stderr, "command \"%s\" not found\n", v[0]);
  2021. ret = -EINVAL;
  2022. }
  2023. }
  2024. g_free(input);
  2025. g_free(v);
  2026. return ret;
  2027. }
  2028. static void __attribute((constructor)) init_qemuio_commands(void)
  2029. {
  2030. /* initialize commands */
  2031. qemuio_add_command(&help_cmd);
  2032. qemuio_add_command(&read_cmd);
  2033. qemuio_add_command(&readv_cmd);
  2034. qemuio_add_command(&write_cmd);
  2035. qemuio_add_command(&writev_cmd);
  2036. qemuio_add_command(&aio_read_cmd);
  2037. qemuio_add_command(&aio_write_cmd);
  2038. qemuio_add_command(&aio_flush_cmd);
  2039. qemuio_add_command(&flush_cmd);
  2040. qemuio_add_command(&truncate_cmd);
  2041. qemuio_add_command(&length_cmd);
  2042. qemuio_add_command(&info_cmd);
  2043. qemuio_add_command(&discard_cmd);
  2044. qemuio_add_command(&alloc_cmd);
  2045. qemuio_add_command(&map_cmd);
  2046. qemuio_add_command(&reopen_cmd);
  2047. qemuio_add_command(&break_cmd);
  2048. qemuio_add_command(&remove_break_cmd);
  2049. qemuio_add_command(&resume_cmd);
  2050. qemuio_add_command(&wait_break_cmd);
  2051. qemuio_add_command(&abort_cmd);
  2052. qemuio_add_command(&sleep_cmd);
  2053. qemuio_add_command(&sigraise_cmd);
  2054. }