2
0

qemu-io-cmds.c 59 KB

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