qemu-io-cmds.c 59 KB

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