qemu-io-cmds.c 64 KB

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