You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

qemu-io-cmds.c 62KB

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