2
0

qemu-io-cmds.c 54 KB

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