You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

qemu-io-cmds.c 62KB

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