qemu-io-cmds.c 54 KB

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