2
0

qemu-io-cmds.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504
  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. /*
  307. * qemu_io_alloc_from_file()
  308. *
  309. * Allocates the buffer and populates it with the content of the given file
  310. * up to @len bytes. If the file length is less than @len, then the buffer
  311. * is populated with the file content cyclically.
  312. *
  313. * @blk - the block backend where the buffer content is going to be written to
  314. * @len - the buffer length
  315. * @file_name - the file to read the content from
  316. *
  317. * Returns: the buffer pointer on success
  318. * NULL on error
  319. */
  320. static void *qemu_io_alloc_from_file(BlockBackend *blk, size_t len,
  321. const char *file_name)
  322. {
  323. char *buf, *buf_origin;
  324. FILE *f = fopen(file_name, "r");
  325. int pattern_len;
  326. if (!f) {
  327. perror(file_name);
  328. return NULL;
  329. }
  330. if (qemuio_misalign) {
  331. len += MISALIGN_OFFSET;
  332. }
  333. buf_origin = buf = blk_blockalign(blk, len);
  334. if (qemuio_misalign) {
  335. buf_origin += MISALIGN_OFFSET;
  336. buf += MISALIGN_OFFSET;
  337. len -= MISALIGN_OFFSET;
  338. }
  339. pattern_len = fread(buf_origin, 1, len, f);
  340. if (ferror(f)) {
  341. perror(file_name);
  342. goto error;
  343. }
  344. if (pattern_len == 0) {
  345. fprintf(stderr, "%s: file is empty\n", file_name);
  346. goto error;
  347. }
  348. fclose(f);
  349. f = NULL;
  350. if (len > pattern_len) {
  351. len -= pattern_len;
  352. buf += pattern_len;
  353. while (len > 0) {
  354. size_t len_to_copy = MIN(pattern_len, len);
  355. memcpy(buf, buf_origin, len_to_copy);
  356. len -= len_to_copy;
  357. buf += len_to_copy;
  358. }
  359. }
  360. return buf_origin;
  361. error:
  362. qemu_io_free(buf_origin);
  363. if (f) {
  364. fclose(f);
  365. }
  366. return NULL;
  367. }
  368. static void dump_buffer(const void *buffer, int64_t offset, int64_t len)
  369. {
  370. uint64_t i;
  371. int j;
  372. const uint8_t *p;
  373. for (i = 0, p = buffer; i < len; i += 16) {
  374. const uint8_t *s = p;
  375. printf("%08" PRIx64 ": ", offset + i);
  376. for (j = 0; j < 16 && i + j < len; j++, p++) {
  377. printf("%02x ", *p);
  378. }
  379. printf(" ");
  380. for (j = 0; j < 16 && i + j < len; j++, s++) {
  381. if (isalnum(*s)) {
  382. printf("%c", *s);
  383. } else {
  384. printf(".");
  385. }
  386. }
  387. printf("\n");
  388. }
  389. }
  390. static void print_report(const char *op, struct timespec *t, int64_t offset,
  391. int64_t count, int64_t total, int cnt, bool Cflag)
  392. {
  393. char s1[64], s2[64], ts[64];
  394. timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
  395. if (!Cflag) {
  396. cvtstr((double)total, s1, sizeof(s1));
  397. cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
  398. printf("%s %"PRId64"/%"PRId64" bytes at offset %" PRId64 "\n",
  399. op, total, count, offset);
  400. printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
  401. s1, cnt, ts, s2, tdiv((double)cnt, *t));
  402. } else {/* bytes,ops,time,bytes/sec,ops/sec */
  403. printf("%"PRId64",%d,%s,%.3f,%.3f\n",
  404. total, cnt, ts,
  405. tdiv((double)total, *t),
  406. tdiv((double)cnt, *t));
  407. }
  408. }
  409. /*
  410. * Parse multiple length statements for vectored I/O, and construct an I/O
  411. * vector matching it.
  412. */
  413. static void *
  414. create_iovec(BlockBackend *blk, QEMUIOVector *qiov, char **argv, int nr_iov,
  415. int pattern)
  416. {
  417. size_t *sizes = g_new0(size_t, nr_iov);
  418. size_t count = 0;
  419. void *buf = NULL;
  420. void *p;
  421. int i;
  422. for (i = 0; i < nr_iov; i++) {
  423. char *arg = argv[i];
  424. int64_t len;
  425. len = cvtnum(arg);
  426. if (len < 0) {
  427. print_cvtnum_err(len, arg);
  428. goto fail;
  429. }
  430. if (len > BDRV_REQUEST_MAX_BYTES) {
  431. printf("Argument '%s' exceeds maximum size %" PRIu64 "\n", arg,
  432. (uint64_t)BDRV_REQUEST_MAX_BYTES);
  433. goto fail;
  434. }
  435. if (count > BDRV_REQUEST_MAX_BYTES - len) {
  436. printf("The total number of bytes exceed the maximum size %" PRIu64
  437. "\n", (uint64_t)BDRV_REQUEST_MAX_BYTES);
  438. goto fail;
  439. }
  440. sizes[i] = len;
  441. count += len;
  442. }
  443. qemu_iovec_init(qiov, nr_iov);
  444. buf = p = qemu_io_alloc(blk, count, pattern);
  445. for (i = 0; i < nr_iov; i++) {
  446. qemu_iovec_add(qiov, p, sizes[i]);
  447. p += sizes[i];
  448. }
  449. fail:
  450. g_free(sizes);
  451. return buf;
  452. }
  453. static int do_pread(BlockBackend *blk, char *buf, int64_t offset,
  454. int64_t bytes, int64_t *total)
  455. {
  456. if (bytes > INT_MAX) {
  457. return -ERANGE;
  458. }
  459. *total = blk_pread(blk, offset, (uint8_t *)buf, bytes);
  460. if (*total < 0) {
  461. return *total;
  462. }
  463. return 1;
  464. }
  465. static int do_pwrite(BlockBackend *blk, char *buf, int64_t offset,
  466. int64_t bytes, int flags, int64_t *total)
  467. {
  468. if (bytes > INT_MAX) {
  469. return -ERANGE;
  470. }
  471. *total = blk_pwrite(blk, offset, (uint8_t *)buf, bytes, flags);
  472. if (*total < 0) {
  473. return *total;
  474. }
  475. return 1;
  476. }
  477. typedef struct {
  478. BlockBackend *blk;
  479. int64_t offset;
  480. int64_t bytes;
  481. int64_t *total;
  482. int flags;
  483. int ret;
  484. bool done;
  485. } CoWriteZeroes;
  486. static void coroutine_fn co_pwrite_zeroes_entry(void *opaque)
  487. {
  488. CoWriteZeroes *data = opaque;
  489. data->ret = blk_co_pwrite_zeroes(data->blk, data->offset, data->bytes,
  490. data->flags);
  491. data->done = true;
  492. if (data->ret < 0) {
  493. *data->total = data->ret;
  494. return;
  495. }
  496. *data->total = data->bytes;
  497. }
  498. static int do_co_pwrite_zeroes(BlockBackend *blk, int64_t offset,
  499. int64_t bytes, int flags, int64_t *total)
  500. {
  501. Coroutine *co;
  502. CoWriteZeroes data = {
  503. .blk = blk,
  504. .offset = offset,
  505. .bytes = bytes,
  506. .total = total,
  507. .flags = flags,
  508. .done = false,
  509. };
  510. if (bytes > INT_MAX) {
  511. return -ERANGE;
  512. }
  513. co = qemu_coroutine_create(co_pwrite_zeroes_entry, &data);
  514. bdrv_coroutine_enter(blk_bs(blk), co);
  515. while (!data.done) {
  516. aio_poll(blk_get_aio_context(blk), true);
  517. }
  518. if (data.ret < 0) {
  519. return data.ret;
  520. } else {
  521. return 1;
  522. }
  523. }
  524. static int do_write_compressed(BlockBackend *blk, char *buf, int64_t offset,
  525. int64_t bytes, int64_t *total)
  526. {
  527. int ret;
  528. if (bytes > BDRV_REQUEST_MAX_BYTES) {
  529. return -ERANGE;
  530. }
  531. ret = blk_pwrite_compressed(blk, offset, buf, bytes);
  532. if (ret < 0) {
  533. return ret;
  534. }
  535. *total = bytes;
  536. return 1;
  537. }
  538. static int do_load_vmstate(BlockBackend *blk, char *buf, int64_t offset,
  539. int64_t count, int64_t *total)
  540. {
  541. if (count > INT_MAX) {
  542. return -ERANGE;
  543. }
  544. *total = blk_load_vmstate(blk, (uint8_t *)buf, offset, count);
  545. if (*total < 0) {
  546. return *total;
  547. }
  548. return 1;
  549. }
  550. static int do_save_vmstate(BlockBackend *blk, char *buf, int64_t offset,
  551. int64_t count, int64_t *total)
  552. {
  553. if (count > INT_MAX) {
  554. return -ERANGE;
  555. }
  556. *total = blk_save_vmstate(blk, (uint8_t *)buf, offset, count);
  557. if (*total < 0) {
  558. return *total;
  559. }
  560. return 1;
  561. }
  562. #define NOT_DONE 0x7fffffff
  563. static void aio_rw_done(void *opaque, int ret)
  564. {
  565. *(int *)opaque = ret;
  566. }
  567. static int do_aio_readv(BlockBackend *blk, QEMUIOVector *qiov,
  568. int64_t offset, int *total)
  569. {
  570. int async_ret = NOT_DONE;
  571. blk_aio_preadv(blk, offset, qiov, 0, aio_rw_done, &async_ret);
  572. while (async_ret == NOT_DONE) {
  573. main_loop_wait(false);
  574. }
  575. *total = qiov->size;
  576. return async_ret < 0 ? async_ret : 1;
  577. }
  578. static int do_aio_writev(BlockBackend *blk, QEMUIOVector *qiov,
  579. int64_t offset, int flags, int *total)
  580. {
  581. int async_ret = NOT_DONE;
  582. blk_aio_pwritev(blk, offset, qiov, flags, aio_rw_done, &async_ret);
  583. while (async_ret == NOT_DONE) {
  584. main_loop_wait(false);
  585. }
  586. *total = qiov->size;
  587. return async_ret < 0 ? async_ret : 1;
  588. }
  589. static void read_help(void)
  590. {
  591. printf(
  592. "\n"
  593. " reads a range of bytes from the given offset\n"
  594. "\n"
  595. " Example:\n"
  596. " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
  597. "\n"
  598. " Reads a segment of the currently open file, optionally dumping it to the\n"
  599. " standard output stream (with -v option) for subsequent inspection.\n"
  600. " -b, -- read from the VM state rather than the virtual disk\n"
  601. " -C, -- report statistics in a machine parsable format\n"
  602. " -l, -- length for pattern verification (only with -P)\n"
  603. " -p, -- ignored for backwards compatibility\n"
  604. " -P, -- use a pattern to verify read data\n"
  605. " -q, -- quiet mode, do not show I/O statistics\n"
  606. " -s, -- start offset for pattern verification (only with -P)\n"
  607. " -v, -- dump buffer to standard output\n"
  608. "\n");
  609. }
  610. static int read_f(BlockBackend *blk, int argc, char **argv);
  611. static const cmdinfo_t read_cmd = {
  612. .name = "read",
  613. .altname = "r",
  614. .cfunc = read_f,
  615. .argmin = 2,
  616. .argmax = -1,
  617. .args = "[-abCqv] [-P pattern [-s off] [-l len]] off len",
  618. .oneline = "reads a number of bytes at a specified offset",
  619. .help = read_help,
  620. };
  621. static int read_f(BlockBackend *blk, int argc, char **argv)
  622. {
  623. struct timespec t1, t2;
  624. bool Cflag = false, qflag = false, vflag = false;
  625. bool Pflag = false, sflag = false, lflag = false, bflag = false;
  626. int c, cnt, ret;
  627. char *buf;
  628. int64_t offset;
  629. int64_t count;
  630. /* Some compilers get confused and warn if this is not initialized. */
  631. int64_t total = 0;
  632. int pattern = 0;
  633. int64_t pattern_offset = 0, pattern_count = 0;
  634. while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != -1) {
  635. switch (c) {
  636. case 'b':
  637. bflag = true;
  638. break;
  639. case 'C':
  640. Cflag = true;
  641. break;
  642. case 'l':
  643. lflag = true;
  644. pattern_count = cvtnum(optarg);
  645. if (pattern_count < 0) {
  646. print_cvtnum_err(pattern_count, optarg);
  647. return pattern_count;
  648. }
  649. break;
  650. case 'p':
  651. /* Ignored for backwards compatibility */
  652. break;
  653. case 'P':
  654. Pflag = true;
  655. pattern = parse_pattern(optarg);
  656. if (pattern < 0) {
  657. return -EINVAL;
  658. }
  659. break;
  660. case 'q':
  661. qflag = true;
  662. break;
  663. case 's':
  664. sflag = true;
  665. pattern_offset = cvtnum(optarg);
  666. if (pattern_offset < 0) {
  667. print_cvtnum_err(pattern_offset, optarg);
  668. return pattern_offset;
  669. }
  670. break;
  671. case 'v':
  672. vflag = true;
  673. break;
  674. default:
  675. qemuio_command_usage(&read_cmd);
  676. return -EINVAL;
  677. }
  678. }
  679. if (optind != argc - 2) {
  680. qemuio_command_usage(&read_cmd);
  681. return -EINVAL;
  682. }
  683. offset = cvtnum(argv[optind]);
  684. if (offset < 0) {
  685. print_cvtnum_err(offset, argv[optind]);
  686. return offset;
  687. }
  688. optind++;
  689. count = cvtnum(argv[optind]);
  690. if (count < 0) {
  691. print_cvtnum_err(count, argv[optind]);
  692. return count;
  693. } else if (count > BDRV_REQUEST_MAX_BYTES) {
  694. printf("length cannot exceed %" PRIu64 ", given %s\n",
  695. (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
  696. return -EINVAL;
  697. }
  698. if (!Pflag && (lflag || sflag)) {
  699. qemuio_command_usage(&read_cmd);
  700. return -EINVAL;
  701. }
  702. if (!lflag) {
  703. pattern_count = count - pattern_offset;
  704. }
  705. if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
  706. printf("pattern verification range exceeds end of read data\n");
  707. return -EINVAL;
  708. }
  709. if (bflag) {
  710. if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) {
  711. printf("%" PRId64 " is not a sector-aligned value for 'offset'\n",
  712. offset);
  713. return -EINVAL;
  714. }
  715. if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) {
  716. printf("%"PRId64" is not a sector-aligned value for 'count'\n",
  717. count);
  718. return -EINVAL;
  719. }
  720. }
  721. buf = qemu_io_alloc(blk, count, 0xab);
  722. clock_gettime(CLOCK_MONOTONIC, &t1);
  723. if (bflag) {
  724. ret = do_load_vmstate(blk, buf, offset, count, &total);
  725. } else {
  726. ret = do_pread(blk, buf, offset, count, &total);
  727. }
  728. clock_gettime(CLOCK_MONOTONIC, &t2);
  729. if (ret < 0) {
  730. printf("read failed: %s\n", strerror(-ret));
  731. goto out;
  732. }
  733. cnt = ret;
  734. ret = 0;
  735. if (Pflag) {
  736. void *cmp_buf = g_malloc(pattern_count);
  737. memset(cmp_buf, pattern, pattern_count);
  738. if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
  739. printf("Pattern verification failed at offset %"
  740. PRId64 ", %"PRId64" bytes\n",
  741. offset + pattern_offset, pattern_count);
  742. ret = -EINVAL;
  743. }
  744. g_free(cmp_buf);
  745. }
  746. if (qflag) {
  747. goto out;
  748. }
  749. if (vflag) {
  750. dump_buffer(buf, offset, count);
  751. }
  752. /* Finally, report back -- -C gives a parsable format */
  753. t2 = tsub(t2, t1);
  754. print_report("read", &t2, offset, count, total, cnt, Cflag);
  755. out:
  756. qemu_io_free(buf);
  757. return ret;
  758. }
  759. static void readv_help(void)
  760. {
  761. printf(
  762. "\n"
  763. " reads a range of bytes from the given offset into multiple buffers\n"
  764. "\n"
  765. " Example:\n"
  766. " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
  767. "\n"
  768. " Reads a segment of the currently open file, optionally dumping it to the\n"
  769. " standard output stream (with -v option) for subsequent inspection.\n"
  770. " Uses multiple iovec buffers if more than one byte range is specified.\n"
  771. " -C, -- report statistics in a machine parsable format\n"
  772. " -P, -- use a pattern to verify read data\n"
  773. " -v, -- dump buffer to standard output\n"
  774. " -q, -- quiet mode, do not show I/O statistics\n"
  775. "\n");
  776. }
  777. static int readv_f(BlockBackend *blk, int argc, char **argv);
  778. static const cmdinfo_t readv_cmd = {
  779. .name = "readv",
  780. .cfunc = readv_f,
  781. .argmin = 2,
  782. .argmax = -1,
  783. .args = "[-Cqv] [-P pattern] off len [len..]",
  784. .oneline = "reads a number of bytes at a specified offset",
  785. .help = readv_help,
  786. };
  787. static int readv_f(BlockBackend *blk, int argc, char **argv)
  788. {
  789. struct timespec t1, t2;
  790. bool Cflag = false, qflag = false, vflag = false;
  791. int c, cnt, ret;
  792. char *buf;
  793. int64_t offset;
  794. /* Some compilers get confused and warn if this is not initialized. */
  795. int total = 0;
  796. int nr_iov;
  797. QEMUIOVector qiov;
  798. int pattern = 0;
  799. bool Pflag = false;
  800. while ((c = getopt(argc, argv, "CP:qv")) != -1) {
  801. switch (c) {
  802. case 'C':
  803. Cflag = true;
  804. break;
  805. case 'P':
  806. Pflag = true;
  807. pattern = parse_pattern(optarg);
  808. if (pattern < 0) {
  809. return -EINVAL;
  810. }
  811. break;
  812. case 'q':
  813. qflag = true;
  814. break;
  815. case 'v':
  816. vflag = true;
  817. break;
  818. default:
  819. qemuio_command_usage(&readv_cmd);
  820. return -EINVAL;
  821. }
  822. }
  823. if (optind > argc - 2) {
  824. qemuio_command_usage(&readv_cmd);
  825. return -EINVAL;
  826. }
  827. offset = cvtnum(argv[optind]);
  828. if (offset < 0) {
  829. print_cvtnum_err(offset, argv[optind]);
  830. return offset;
  831. }
  832. optind++;
  833. nr_iov = argc - optind;
  834. buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, 0xab);
  835. if (buf == NULL) {
  836. return -EINVAL;
  837. }
  838. clock_gettime(CLOCK_MONOTONIC, &t1);
  839. ret = do_aio_readv(blk, &qiov, offset, &total);
  840. clock_gettime(CLOCK_MONOTONIC, &t2);
  841. if (ret < 0) {
  842. printf("readv failed: %s\n", strerror(-ret));
  843. goto out;
  844. }
  845. cnt = ret;
  846. ret = 0;
  847. if (Pflag) {
  848. void *cmp_buf = g_malloc(qiov.size);
  849. memset(cmp_buf, pattern, qiov.size);
  850. if (memcmp(buf, cmp_buf, qiov.size)) {
  851. printf("Pattern verification failed at offset %"
  852. PRId64 ", %zu bytes\n", offset, qiov.size);
  853. ret = -EINVAL;
  854. }
  855. g_free(cmp_buf);
  856. }
  857. if (qflag) {
  858. goto out;
  859. }
  860. if (vflag) {
  861. dump_buffer(buf, offset, qiov.size);
  862. }
  863. /* Finally, report back -- -C gives a parsable format */
  864. t2 = tsub(t2, t1);
  865. print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
  866. out:
  867. qemu_iovec_destroy(&qiov);
  868. qemu_io_free(buf);
  869. return ret;
  870. }
  871. static void write_help(void)
  872. {
  873. printf(
  874. "\n"
  875. " writes a range of bytes from the given offset\n"
  876. "\n"
  877. " Example:\n"
  878. " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
  879. "\n"
  880. " Writes into a segment of the currently open file, using a buffer\n"
  881. " filled with a set pattern (0xcdcdcdcd).\n"
  882. " -b, -- write to the VM state rather than the virtual disk\n"
  883. " -c, -- write compressed data with blk_write_compressed\n"
  884. " -f, -- use Force Unit Access semantics\n"
  885. " -n, -- with -z, don't allow slow fallback\n"
  886. " -p, -- ignored for backwards compatibility\n"
  887. " -P, -- use different pattern to fill file\n"
  888. " -s, -- use a pattern file to fill the write buffer\n"
  889. " -C, -- report statistics in a machine parsable format\n"
  890. " -q, -- quiet mode, do not show I/O statistics\n"
  891. " -u, -- with -z, allow unmapping\n"
  892. " -z, -- write zeroes using blk_co_pwrite_zeroes\n"
  893. "\n");
  894. }
  895. static int write_f(BlockBackend *blk, int argc, char **argv);
  896. static const cmdinfo_t write_cmd = {
  897. .name = "write",
  898. .altname = "w",
  899. .cfunc = write_f,
  900. .perm = BLK_PERM_WRITE,
  901. .argmin = 2,
  902. .argmax = -1,
  903. .args = "[-bcCfnquz] [-P pattern | -s source_file] off len",
  904. .oneline = "writes a number of bytes at a specified offset",
  905. .help = write_help,
  906. };
  907. static int write_f(BlockBackend *blk, int argc, char **argv)
  908. {
  909. struct timespec t1, t2;
  910. bool Cflag = false, qflag = false, bflag = false;
  911. bool Pflag = false, zflag = false, cflag = false, sflag = false;
  912. int flags = 0;
  913. int c, cnt, ret;
  914. char *buf = NULL;
  915. int64_t offset;
  916. int64_t count;
  917. /* Some compilers get confused and warn if this is not initialized. */
  918. int64_t total = 0;
  919. int pattern = 0xcd;
  920. const char *file_name = NULL;
  921. while ((c = getopt(argc, argv, "bcCfnpP:qs:uz")) != -1) {
  922. switch (c) {
  923. case 'b':
  924. bflag = true;
  925. break;
  926. case 'c':
  927. cflag = true;
  928. break;
  929. case 'C':
  930. Cflag = true;
  931. break;
  932. case 'f':
  933. flags |= BDRV_REQ_FUA;
  934. break;
  935. case 'n':
  936. flags |= BDRV_REQ_NO_FALLBACK;
  937. break;
  938. case 'p':
  939. /* Ignored for backwards compatibility */
  940. break;
  941. case 'P':
  942. Pflag = true;
  943. pattern = parse_pattern(optarg);
  944. if (pattern < 0) {
  945. return -EINVAL;
  946. }
  947. break;
  948. case 'q':
  949. qflag = true;
  950. break;
  951. case 's':
  952. sflag = true;
  953. file_name = optarg;
  954. break;
  955. case 'u':
  956. flags |= BDRV_REQ_MAY_UNMAP;
  957. break;
  958. case 'z':
  959. zflag = true;
  960. break;
  961. default:
  962. qemuio_command_usage(&write_cmd);
  963. return -EINVAL;
  964. }
  965. }
  966. if (optind != argc - 2) {
  967. qemuio_command_usage(&write_cmd);
  968. return -EINVAL;
  969. }
  970. if (bflag && zflag) {
  971. printf("-b and -z cannot be specified at the same time\n");
  972. return -EINVAL;
  973. }
  974. if ((flags & BDRV_REQ_FUA) && (bflag || cflag)) {
  975. printf("-f and -b or -c cannot be specified at the same time\n");
  976. return -EINVAL;
  977. }
  978. if ((flags & BDRV_REQ_NO_FALLBACK) && !zflag) {
  979. printf("-n requires -z to be specified\n");
  980. return -EINVAL;
  981. }
  982. if ((flags & BDRV_REQ_MAY_UNMAP) && !zflag) {
  983. printf("-u requires -z to be specified\n");
  984. return -EINVAL;
  985. }
  986. if (zflag + Pflag + sflag > 1) {
  987. printf("Only one of -z, -P, and -s "
  988. "can be specified at the same time\n");
  989. return -EINVAL;
  990. }
  991. offset = cvtnum(argv[optind]);
  992. if (offset < 0) {
  993. print_cvtnum_err(offset, argv[optind]);
  994. return offset;
  995. }
  996. optind++;
  997. count = cvtnum(argv[optind]);
  998. if (count < 0) {
  999. print_cvtnum_err(count, argv[optind]);
  1000. return count;
  1001. } else if (count > BDRV_REQUEST_MAX_BYTES) {
  1002. printf("length cannot exceed %" PRIu64 ", given %s\n",
  1003. (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
  1004. return -EINVAL;
  1005. }
  1006. if (bflag || cflag) {
  1007. if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) {
  1008. printf("%" PRId64 " is not a sector-aligned value for 'offset'\n",
  1009. offset);
  1010. return -EINVAL;
  1011. }
  1012. if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) {
  1013. printf("%"PRId64" is not a sector-aligned value for 'count'\n",
  1014. count);
  1015. return -EINVAL;
  1016. }
  1017. }
  1018. if (!zflag) {
  1019. if (sflag) {
  1020. buf = qemu_io_alloc_from_file(blk, count, file_name);
  1021. if (!buf) {
  1022. return -EINVAL;
  1023. }
  1024. } else {
  1025. buf = qemu_io_alloc(blk, count, pattern);
  1026. }
  1027. }
  1028. clock_gettime(CLOCK_MONOTONIC, &t1);
  1029. if (bflag) {
  1030. ret = do_save_vmstate(blk, buf, offset, count, &total);
  1031. } else if (zflag) {
  1032. ret = do_co_pwrite_zeroes(blk, offset, count, flags, &total);
  1033. } else if (cflag) {
  1034. ret = do_write_compressed(blk, buf, offset, count, &total);
  1035. } else {
  1036. ret = do_pwrite(blk, buf, offset, count, flags, &total);
  1037. }
  1038. clock_gettime(CLOCK_MONOTONIC, &t2);
  1039. if (ret < 0) {
  1040. printf("write failed: %s\n", strerror(-ret));
  1041. goto out;
  1042. }
  1043. cnt = ret;
  1044. ret = 0;
  1045. if (qflag) {
  1046. goto out;
  1047. }
  1048. /* Finally, report back -- -C gives a parsable format */
  1049. t2 = tsub(t2, t1);
  1050. print_report("wrote", &t2, offset, count, total, cnt, Cflag);
  1051. out:
  1052. if (!zflag) {
  1053. qemu_io_free(buf);
  1054. }
  1055. return ret;
  1056. }
  1057. static void
  1058. writev_help(void)
  1059. {
  1060. printf(
  1061. "\n"
  1062. " writes a range of bytes from the given offset source from multiple buffers\n"
  1063. "\n"
  1064. " Example:\n"
  1065. " 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
  1066. "\n"
  1067. " Writes into a segment of the currently open file, using a buffer\n"
  1068. " filled with a set pattern (0xcdcdcdcd).\n"
  1069. " -P, -- use different pattern to fill file\n"
  1070. " -C, -- report statistics in a machine parsable format\n"
  1071. " -f, -- use Force Unit Access semantics\n"
  1072. " -q, -- quiet mode, do not show I/O statistics\n"
  1073. "\n");
  1074. }
  1075. static int writev_f(BlockBackend *blk, int argc, char **argv);
  1076. static const cmdinfo_t writev_cmd = {
  1077. .name = "writev",
  1078. .cfunc = writev_f,
  1079. .perm = BLK_PERM_WRITE,
  1080. .argmin = 2,
  1081. .argmax = -1,
  1082. .args = "[-Cfq] [-P pattern] off len [len..]",
  1083. .oneline = "writes a number of bytes at a specified offset",
  1084. .help = writev_help,
  1085. };
  1086. static int writev_f(BlockBackend *blk, int argc, char **argv)
  1087. {
  1088. struct timespec t1, t2;
  1089. bool Cflag = false, qflag = false;
  1090. int flags = 0;
  1091. int c, cnt, ret;
  1092. char *buf;
  1093. int64_t offset;
  1094. /* Some compilers get confused and warn if this is not initialized. */
  1095. int total = 0;
  1096. int nr_iov;
  1097. int pattern = 0xcd;
  1098. QEMUIOVector qiov;
  1099. while ((c = getopt(argc, argv, "CfqP:")) != -1) {
  1100. switch (c) {
  1101. case 'C':
  1102. Cflag = true;
  1103. break;
  1104. case 'f':
  1105. flags |= BDRV_REQ_FUA;
  1106. break;
  1107. case 'q':
  1108. qflag = true;
  1109. break;
  1110. case 'P':
  1111. pattern = parse_pattern(optarg);
  1112. if (pattern < 0) {
  1113. return -EINVAL;
  1114. }
  1115. break;
  1116. default:
  1117. qemuio_command_usage(&writev_cmd);
  1118. return -EINVAL;
  1119. }
  1120. }
  1121. if (optind > argc - 2) {
  1122. qemuio_command_usage(&writev_cmd);
  1123. return -EINVAL;
  1124. }
  1125. offset = cvtnum(argv[optind]);
  1126. if (offset < 0) {
  1127. print_cvtnum_err(offset, argv[optind]);
  1128. return offset;
  1129. }
  1130. optind++;
  1131. nr_iov = argc - optind;
  1132. buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, pattern);
  1133. if (buf == NULL) {
  1134. return -EINVAL;
  1135. }
  1136. clock_gettime(CLOCK_MONOTONIC, &t1);
  1137. ret = do_aio_writev(blk, &qiov, offset, flags, &total);
  1138. clock_gettime(CLOCK_MONOTONIC, &t2);
  1139. if (ret < 0) {
  1140. printf("writev failed: %s\n", strerror(-ret));
  1141. goto out;
  1142. }
  1143. cnt = ret;
  1144. ret = 0;
  1145. if (qflag) {
  1146. goto out;
  1147. }
  1148. /* Finally, report back -- -C gives a parsable format */
  1149. t2 = tsub(t2, t1);
  1150. print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
  1151. out:
  1152. qemu_iovec_destroy(&qiov);
  1153. qemu_io_free(buf);
  1154. return ret;
  1155. }
  1156. struct aio_ctx {
  1157. BlockBackend *blk;
  1158. QEMUIOVector qiov;
  1159. int64_t offset;
  1160. char *buf;
  1161. bool qflag;
  1162. bool vflag;
  1163. bool Cflag;
  1164. bool Pflag;
  1165. bool zflag;
  1166. BlockAcctCookie acct;
  1167. int pattern;
  1168. struct timespec t1;
  1169. };
  1170. static void aio_write_done(void *opaque, int ret)
  1171. {
  1172. struct aio_ctx *ctx = opaque;
  1173. struct timespec t2;
  1174. clock_gettime(CLOCK_MONOTONIC, &t2);
  1175. if (ret < 0) {
  1176. printf("aio_write failed: %s\n", strerror(-ret));
  1177. block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
  1178. goto out;
  1179. }
  1180. block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
  1181. if (ctx->qflag) {
  1182. goto out;
  1183. }
  1184. /* Finally, report back -- -C gives a parsable format */
  1185. t2 = tsub(t2, ctx->t1);
  1186. print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
  1187. ctx->qiov.size, 1, ctx->Cflag);
  1188. out:
  1189. if (!ctx->zflag) {
  1190. qemu_io_free(ctx->buf);
  1191. qemu_iovec_destroy(&ctx->qiov);
  1192. }
  1193. g_free(ctx);
  1194. }
  1195. static void aio_read_done(void *opaque, int ret)
  1196. {
  1197. struct aio_ctx *ctx = opaque;
  1198. struct timespec t2;
  1199. clock_gettime(CLOCK_MONOTONIC, &t2);
  1200. if (ret < 0) {
  1201. printf("readv failed: %s\n", strerror(-ret));
  1202. block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
  1203. goto out;
  1204. }
  1205. if (ctx->Pflag) {
  1206. void *cmp_buf = g_malloc(ctx->qiov.size);
  1207. memset(cmp_buf, ctx->pattern, ctx->qiov.size);
  1208. if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
  1209. printf("Pattern verification failed at offset %"
  1210. PRId64 ", %zu bytes\n", ctx->offset, ctx->qiov.size);
  1211. }
  1212. g_free(cmp_buf);
  1213. }
  1214. block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
  1215. if (ctx->qflag) {
  1216. goto out;
  1217. }
  1218. if (ctx->vflag) {
  1219. dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
  1220. }
  1221. /* Finally, report back -- -C gives a parsable format */
  1222. t2 = tsub(t2, ctx->t1);
  1223. print_report("read", &t2, ctx->offset, ctx->qiov.size,
  1224. ctx->qiov.size, 1, ctx->Cflag);
  1225. out:
  1226. qemu_io_free(ctx->buf);
  1227. qemu_iovec_destroy(&ctx->qiov);
  1228. g_free(ctx);
  1229. }
  1230. static void aio_read_help(void)
  1231. {
  1232. printf(
  1233. "\n"
  1234. " asynchronously reads a range of bytes from the given offset\n"
  1235. "\n"
  1236. " Example:\n"
  1237. " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
  1238. "\n"
  1239. " Reads a segment of the currently open file, optionally dumping it to the\n"
  1240. " standard output stream (with -v option) for subsequent inspection.\n"
  1241. " The read is performed asynchronously and the aio_flush command must be\n"
  1242. " used to ensure all outstanding aio requests have been completed.\n"
  1243. " Note that due to its asynchronous nature, this command will be\n"
  1244. " considered successful once the request is submitted, independently\n"
  1245. " of potential I/O errors or pattern mismatches.\n"
  1246. " -C, -- report statistics in a machine parsable format\n"
  1247. " -P, -- use a pattern to verify read data\n"
  1248. " -i, -- treat request as invalid, for exercising stats\n"
  1249. " -v, -- dump buffer to standard output\n"
  1250. " -q, -- quiet mode, do not show I/O statistics\n"
  1251. "\n");
  1252. }
  1253. static int aio_read_f(BlockBackend *blk, int argc, char **argv);
  1254. static const cmdinfo_t aio_read_cmd = {
  1255. .name = "aio_read",
  1256. .cfunc = aio_read_f,
  1257. .argmin = 2,
  1258. .argmax = -1,
  1259. .args = "[-Ciqv] [-P pattern] off len [len..]",
  1260. .oneline = "asynchronously reads a number of bytes",
  1261. .help = aio_read_help,
  1262. };
  1263. static int aio_read_f(BlockBackend *blk, int argc, char **argv)
  1264. {
  1265. int nr_iov, c;
  1266. struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
  1267. ctx->blk = blk;
  1268. while ((c = getopt(argc, argv, "CP:iqv")) != -1) {
  1269. switch (c) {
  1270. case 'C':
  1271. ctx->Cflag = true;
  1272. break;
  1273. case 'P':
  1274. ctx->Pflag = true;
  1275. ctx->pattern = parse_pattern(optarg);
  1276. if (ctx->pattern < 0) {
  1277. g_free(ctx);
  1278. return -EINVAL;
  1279. }
  1280. break;
  1281. case 'i':
  1282. printf("injecting invalid read request\n");
  1283. block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
  1284. g_free(ctx);
  1285. return 0;
  1286. case 'q':
  1287. ctx->qflag = true;
  1288. break;
  1289. case 'v':
  1290. ctx->vflag = true;
  1291. break;
  1292. default:
  1293. g_free(ctx);
  1294. qemuio_command_usage(&aio_read_cmd);
  1295. return -EINVAL;
  1296. }
  1297. }
  1298. if (optind > argc - 2) {
  1299. g_free(ctx);
  1300. qemuio_command_usage(&aio_read_cmd);
  1301. return -EINVAL;
  1302. }
  1303. ctx->offset = cvtnum(argv[optind]);
  1304. if (ctx->offset < 0) {
  1305. int ret = ctx->offset;
  1306. print_cvtnum_err(ret, argv[optind]);
  1307. g_free(ctx);
  1308. return ret;
  1309. }
  1310. optind++;
  1311. nr_iov = argc - optind;
  1312. ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, 0xab);
  1313. if (ctx->buf == NULL) {
  1314. block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
  1315. g_free(ctx);
  1316. return -EINVAL;
  1317. }
  1318. clock_gettime(CLOCK_MONOTONIC, &ctx->t1);
  1319. block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
  1320. BLOCK_ACCT_READ);
  1321. blk_aio_preadv(blk, ctx->offset, &ctx->qiov, 0, aio_read_done, ctx);
  1322. return 0;
  1323. }
  1324. static void aio_write_help(void)
  1325. {
  1326. printf(
  1327. "\n"
  1328. " asynchronously writes a range of bytes from the given offset source\n"
  1329. " from multiple buffers\n"
  1330. "\n"
  1331. " Example:\n"
  1332. " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
  1333. "\n"
  1334. " Writes into a segment of the currently open file, using a buffer\n"
  1335. " filled with a set pattern (0xcdcdcdcd).\n"
  1336. " The write is performed asynchronously and the aio_flush command must be\n"
  1337. " used to ensure all outstanding aio requests have been completed.\n"
  1338. " Note that due to its asynchronous nature, this command will be\n"
  1339. " considered successful once the request is submitted, independently\n"
  1340. " of potential I/O errors or pattern mismatches.\n"
  1341. " -P, -- use different pattern to fill file\n"
  1342. " -C, -- report statistics in a machine parsable format\n"
  1343. " -f, -- use Force Unit Access semantics\n"
  1344. " -i, -- treat request as invalid, for exercising stats\n"
  1345. " -q, -- quiet mode, do not show I/O statistics\n"
  1346. " -u, -- with -z, allow unmapping\n"
  1347. " -z, -- write zeroes using blk_aio_pwrite_zeroes\n"
  1348. "\n");
  1349. }
  1350. static int aio_write_f(BlockBackend *blk, int argc, char **argv);
  1351. static const cmdinfo_t aio_write_cmd = {
  1352. .name = "aio_write",
  1353. .cfunc = aio_write_f,
  1354. .perm = BLK_PERM_WRITE,
  1355. .argmin = 2,
  1356. .argmax = -1,
  1357. .args = "[-Cfiquz] [-P pattern] off len [len..]",
  1358. .oneline = "asynchronously writes a number of bytes",
  1359. .help = aio_write_help,
  1360. };
  1361. static int aio_write_f(BlockBackend *blk, int argc, char **argv)
  1362. {
  1363. int nr_iov, c;
  1364. int pattern = 0xcd;
  1365. struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
  1366. int flags = 0;
  1367. ctx->blk = blk;
  1368. while ((c = getopt(argc, argv, "CfiqP:uz")) != -1) {
  1369. switch (c) {
  1370. case 'C':
  1371. ctx->Cflag = true;
  1372. break;
  1373. case 'f':
  1374. flags |= BDRV_REQ_FUA;
  1375. break;
  1376. case 'q':
  1377. ctx->qflag = true;
  1378. break;
  1379. case 'u':
  1380. flags |= BDRV_REQ_MAY_UNMAP;
  1381. break;
  1382. case 'P':
  1383. pattern = parse_pattern(optarg);
  1384. if (pattern < 0) {
  1385. g_free(ctx);
  1386. return -EINVAL;
  1387. }
  1388. break;
  1389. case 'i':
  1390. printf("injecting invalid write request\n");
  1391. block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
  1392. g_free(ctx);
  1393. return 0;
  1394. case 'z':
  1395. ctx->zflag = true;
  1396. break;
  1397. default:
  1398. g_free(ctx);
  1399. qemuio_command_usage(&aio_write_cmd);
  1400. return -EINVAL;
  1401. }
  1402. }
  1403. if (optind > argc - 2) {
  1404. g_free(ctx);
  1405. qemuio_command_usage(&aio_write_cmd);
  1406. return -EINVAL;
  1407. }
  1408. if (ctx->zflag && optind != argc - 2) {
  1409. printf("-z supports only a single length parameter\n");
  1410. g_free(ctx);
  1411. return -EINVAL;
  1412. }
  1413. if ((flags & BDRV_REQ_MAY_UNMAP) && !ctx->zflag) {
  1414. printf("-u requires -z to be specified\n");
  1415. g_free(ctx);
  1416. return -EINVAL;
  1417. }
  1418. if (ctx->zflag && ctx->Pflag) {
  1419. printf("-z and -P cannot be specified at the same time\n");
  1420. g_free(ctx);
  1421. return -EINVAL;
  1422. }
  1423. ctx->offset = cvtnum(argv[optind]);
  1424. if (ctx->offset < 0) {
  1425. int ret = ctx->offset;
  1426. print_cvtnum_err(ret, argv[optind]);
  1427. g_free(ctx);
  1428. return ret;
  1429. }
  1430. optind++;
  1431. if (ctx->zflag) {
  1432. int64_t count = cvtnum(argv[optind]);
  1433. if (count < 0) {
  1434. print_cvtnum_err(count, argv[optind]);
  1435. g_free(ctx);
  1436. return count;
  1437. }
  1438. ctx->qiov.size = count;
  1439. blk_aio_pwrite_zeroes(blk, ctx->offset, count, flags, aio_write_done,
  1440. ctx);
  1441. } else {
  1442. nr_iov = argc - optind;
  1443. ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov,
  1444. pattern);
  1445. if (ctx->buf == NULL) {
  1446. block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
  1447. g_free(ctx);
  1448. return -EINVAL;
  1449. }
  1450. clock_gettime(CLOCK_MONOTONIC, &ctx->t1);
  1451. block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
  1452. BLOCK_ACCT_WRITE);
  1453. blk_aio_pwritev(blk, ctx->offset, &ctx->qiov, flags, aio_write_done,
  1454. ctx);
  1455. }
  1456. return 0;
  1457. }
  1458. static int aio_flush_f(BlockBackend *blk, int argc, char **argv)
  1459. {
  1460. BlockAcctCookie cookie;
  1461. block_acct_start(blk_get_stats(blk), &cookie, 0, BLOCK_ACCT_FLUSH);
  1462. blk_drain_all();
  1463. block_acct_done(blk_get_stats(blk), &cookie);
  1464. return 0;
  1465. }
  1466. static const cmdinfo_t aio_flush_cmd = {
  1467. .name = "aio_flush",
  1468. .cfunc = aio_flush_f,
  1469. .oneline = "completes all outstanding aio requests"
  1470. };
  1471. static int flush_f(BlockBackend *blk, int argc, char **argv)
  1472. {
  1473. return blk_flush(blk);
  1474. }
  1475. static const cmdinfo_t flush_cmd = {
  1476. .name = "flush",
  1477. .altname = "f",
  1478. .cfunc = flush_f,
  1479. .oneline = "flush all in-core file state to disk",
  1480. };
  1481. static int truncate_f(BlockBackend *blk, int argc, char **argv)
  1482. {
  1483. Error *local_err = NULL;
  1484. int64_t offset;
  1485. int ret;
  1486. offset = cvtnum(argv[1]);
  1487. if (offset < 0) {
  1488. print_cvtnum_err(offset, argv[1]);
  1489. return offset;
  1490. }
  1491. /*
  1492. * qemu-io is a debugging tool, so let us be strict here and pass
  1493. * exact=true. It is better to err on the "emit more errors" side
  1494. * than to be overly permissive.
  1495. */
  1496. ret = blk_truncate(blk, offset, true, PREALLOC_MODE_OFF, &local_err);
  1497. if (ret < 0) {
  1498. error_report_err(local_err);
  1499. return ret;
  1500. }
  1501. return 0;
  1502. }
  1503. static const cmdinfo_t truncate_cmd = {
  1504. .name = "truncate",
  1505. .altname = "t",
  1506. .cfunc = truncate_f,
  1507. .perm = BLK_PERM_WRITE | BLK_PERM_RESIZE,
  1508. .argmin = 1,
  1509. .argmax = 1,
  1510. .args = "off",
  1511. .oneline = "truncates the current file at the given offset",
  1512. };
  1513. static int length_f(BlockBackend *blk, int argc, char **argv)
  1514. {
  1515. int64_t size;
  1516. char s1[64];
  1517. size = blk_getlength(blk);
  1518. if (size < 0) {
  1519. printf("getlength: %s\n", strerror(-size));
  1520. return size;
  1521. }
  1522. cvtstr(size, s1, sizeof(s1));
  1523. printf("%s\n", s1);
  1524. return 0;
  1525. }
  1526. static const cmdinfo_t length_cmd = {
  1527. .name = "length",
  1528. .altname = "l",
  1529. .cfunc = length_f,
  1530. .oneline = "gets the length of the current file",
  1531. };
  1532. static int info_f(BlockBackend *blk, int argc, char **argv)
  1533. {
  1534. BlockDriverState *bs = blk_bs(blk);
  1535. BlockDriverInfo bdi;
  1536. ImageInfoSpecific *spec_info;
  1537. Error *local_err = NULL;
  1538. char s1[64], s2[64];
  1539. int ret;
  1540. if (bs->drv && bs->drv->format_name) {
  1541. printf("format name: %s\n", bs->drv->format_name);
  1542. }
  1543. if (bs->drv && bs->drv->protocol_name) {
  1544. printf("format name: %s\n", bs->drv->protocol_name);
  1545. }
  1546. ret = bdrv_get_info(bs, &bdi);
  1547. if (ret) {
  1548. return ret;
  1549. }
  1550. cvtstr(bdi.cluster_size, s1, sizeof(s1));
  1551. cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
  1552. printf("cluster size: %s\n", s1);
  1553. printf("vm state offset: %s\n", s2);
  1554. spec_info = bdrv_get_specific_info(bs, &local_err);
  1555. if (local_err) {
  1556. error_report_err(local_err);
  1557. return -EIO;
  1558. }
  1559. if (spec_info) {
  1560. printf("Format specific information:\n");
  1561. bdrv_image_info_specific_dump(spec_info);
  1562. qapi_free_ImageInfoSpecific(spec_info);
  1563. }
  1564. return 0;
  1565. }
  1566. static const cmdinfo_t info_cmd = {
  1567. .name = "info",
  1568. .altname = "i",
  1569. .cfunc = info_f,
  1570. .oneline = "prints information about the current file",
  1571. };
  1572. static void discard_help(void)
  1573. {
  1574. printf(
  1575. "\n"
  1576. " discards a range of bytes from the given offset\n"
  1577. "\n"
  1578. " Example:\n"
  1579. " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
  1580. "\n"
  1581. " Discards a segment of the currently open file.\n"
  1582. " -C, -- report statistics in a machine parsable format\n"
  1583. " -q, -- quiet mode, do not show I/O statistics\n"
  1584. "\n");
  1585. }
  1586. static int discard_f(BlockBackend *blk, int argc, char **argv);
  1587. static const cmdinfo_t discard_cmd = {
  1588. .name = "discard",
  1589. .altname = "d",
  1590. .cfunc = discard_f,
  1591. .perm = BLK_PERM_WRITE,
  1592. .argmin = 2,
  1593. .argmax = -1,
  1594. .args = "[-Cq] off len",
  1595. .oneline = "discards a number of bytes at a specified offset",
  1596. .help = discard_help,
  1597. };
  1598. static int discard_f(BlockBackend *blk, int argc, char **argv)
  1599. {
  1600. struct timespec t1, t2;
  1601. bool Cflag = false, qflag = false;
  1602. int c, ret;
  1603. int64_t offset, bytes;
  1604. while ((c = getopt(argc, argv, "Cq")) != -1) {
  1605. switch (c) {
  1606. case 'C':
  1607. Cflag = true;
  1608. break;
  1609. case 'q':
  1610. qflag = true;
  1611. break;
  1612. default:
  1613. qemuio_command_usage(&discard_cmd);
  1614. return -EINVAL;
  1615. }
  1616. }
  1617. if (optind != argc - 2) {
  1618. qemuio_command_usage(&discard_cmd);
  1619. return -EINVAL;
  1620. }
  1621. offset = cvtnum(argv[optind]);
  1622. if (offset < 0) {
  1623. print_cvtnum_err(offset, argv[optind]);
  1624. return offset;
  1625. }
  1626. optind++;
  1627. bytes = cvtnum(argv[optind]);
  1628. if (bytes < 0) {
  1629. print_cvtnum_err(bytes, argv[optind]);
  1630. return bytes;
  1631. } else if (bytes > BDRV_REQUEST_MAX_BYTES) {
  1632. printf("length cannot exceed %"PRIu64", given %s\n",
  1633. (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
  1634. return -EINVAL;
  1635. }
  1636. clock_gettime(CLOCK_MONOTONIC, &t1);
  1637. ret = blk_pdiscard(blk, offset, bytes);
  1638. clock_gettime(CLOCK_MONOTONIC, &t2);
  1639. if (ret < 0) {
  1640. printf("discard failed: %s\n", strerror(-ret));
  1641. return ret;
  1642. }
  1643. /* Finally, report back -- -C gives a parsable format */
  1644. if (!qflag) {
  1645. t2 = tsub(t2, t1);
  1646. print_report("discard", &t2, offset, bytes, bytes, 1, Cflag);
  1647. }
  1648. return 0;
  1649. }
  1650. static int alloc_f(BlockBackend *blk, int argc, char **argv)
  1651. {
  1652. BlockDriverState *bs = blk_bs(blk);
  1653. int64_t offset, start, remaining, count;
  1654. char s1[64];
  1655. int ret;
  1656. int64_t num, sum_alloc;
  1657. start = offset = cvtnum(argv[1]);
  1658. if (offset < 0) {
  1659. print_cvtnum_err(offset, argv[1]);
  1660. return offset;
  1661. }
  1662. if (argc == 3) {
  1663. count = cvtnum(argv[2]);
  1664. if (count < 0) {
  1665. print_cvtnum_err(count, argv[2]);
  1666. return count;
  1667. }
  1668. } else {
  1669. count = BDRV_SECTOR_SIZE;
  1670. }
  1671. remaining = count;
  1672. sum_alloc = 0;
  1673. while (remaining) {
  1674. ret = bdrv_is_allocated(bs, offset, remaining, &num);
  1675. if (ret < 0) {
  1676. printf("is_allocated failed: %s\n", strerror(-ret));
  1677. return ret;
  1678. }
  1679. offset += num;
  1680. remaining -= num;
  1681. if (ret) {
  1682. sum_alloc += num;
  1683. }
  1684. if (num == 0) {
  1685. count -= remaining;
  1686. remaining = 0;
  1687. }
  1688. }
  1689. cvtstr(start, s1, sizeof(s1));
  1690. printf("%"PRId64"/%"PRId64" bytes allocated at offset %s\n",
  1691. sum_alloc, count, s1);
  1692. return 0;
  1693. }
  1694. static const cmdinfo_t alloc_cmd = {
  1695. .name = "alloc",
  1696. .altname = "a",
  1697. .argmin = 1,
  1698. .argmax = 2,
  1699. .cfunc = alloc_f,
  1700. .args = "offset [count]",
  1701. .oneline = "checks if offset is allocated in the file",
  1702. };
  1703. static int map_is_allocated(BlockDriverState *bs, int64_t offset,
  1704. int64_t bytes, int64_t *pnum)
  1705. {
  1706. int64_t num;
  1707. int num_checked;
  1708. int ret, firstret;
  1709. num_checked = MIN(bytes, BDRV_REQUEST_MAX_BYTES);
  1710. ret = bdrv_is_allocated(bs, offset, num_checked, &num);
  1711. if (ret < 0) {
  1712. return ret;
  1713. }
  1714. firstret = ret;
  1715. *pnum = num;
  1716. while (bytes > 0 && ret == firstret) {
  1717. offset += num;
  1718. bytes -= num;
  1719. num_checked = MIN(bytes, BDRV_REQUEST_MAX_BYTES);
  1720. ret = bdrv_is_allocated(bs, offset, num_checked, &num);
  1721. if (ret == firstret && num) {
  1722. *pnum += num;
  1723. } else {
  1724. break;
  1725. }
  1726. }
  1727. return firstret;
  1728. }
  1729. static int map_f(BlockBackend *blk, int argc, char **argv)
  1730. {
  1731. int64_t offset, bytes;
  1732. char s1[64], s2[64];
  1733. int64_t num;
  1734. int ret;
  1735. const char *retstr;
  1736. offset = 0;
  1737. bytes = blk_getlength(blk);
  1738. if (bytes < 0) {
  1739. error_report("Failed to query image length: %s", strerror(-bytes));
  1740. return bytes;
  1741. }
  1742. while (bytes) {
  1743. ret = map_is_allocated(blk_bs(blk), offset, bytes, &num);
  1744. if (ret < 0) {
  1745. error_report("Failed to get allocation status: %s", strerror(-ret));
  1746. return ret;
  1747. } else if (!num) {
  1748. error_report("Unexpected end of image");
  1749. return -EIO;
  1750. }
  1751. retstr = ret ? " allocated" : "not allocated";
  1752. cvtstr(num, s1, sizeof(s1));
  1753. cvtstr(offset, s2, sizeof(s2));
  1754. printf("%s (0x%" PRIx64 ") bytes %s at offset %s (0x%" PRIx64 ")\n",
  1755. s1, num, retstr, s2, offset);
  1756. offset += num;
  1757. bytes -= num;
  1758. }
  1759. return 0;
  1760. }
  1761. static const cmdinfo_t map_cmd = {
  1762. .name = "map",
  1763. .argmin = 0,
  1764. .argmax = 0,
  1765. .cfunc = map_f,
  1766. .args = "",
  1767. .oneline = "prints the allocated areas of a file",
  1768. };
  1769. static void reopen_help(void)
  1770. {
  1771. printf(
  1772. "\n"
  1773. " Changes the open options of an already opened image\n"
  1774. "\n"
  1775. " Example:\n"
  1776. " 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
  1777. "\n"
  1778. " -r, -- Reopen the image read-only\n"
  1779. " -w, -- Reopen the image read-write\n"
  1780. " -c, -- Change the cache mode to the given value\n"
  1781. " -o, -- Changes block driver options (cf. 'open' command)\n"
  1782. "\n");
  1783. }
  1784. static int reopen_f(BlockBackend *blk, int argc, char **argv);
  1785. static QemuOptsList reopen_opts = {
  1786. .name = "reopen",
  1787. .merge_lists = true,
  1788. .head = QTAILQ_HEAD_INITIALIZER(reopen_opts.head),
  1789. .desc = {
  1790. /* no elements => accept any params */
  1791. { /* end of list */ }
  1792. },
  1793. };
  1794. static const cmdinfo_t reopen_cmd = {
  1795. .name = "reopen",
  1796. .argmin = 0,
  1797. .argmax = -1,
  1798. .cfunc = reopen_f,
  1799. .args = "[(-r|-w)] [-c cache] [-o options]",
  1800. .oneline = "reopens an image with new options",
  1801. .help = reopen_help,
  1802. };
  1803. static int reopen_f(BlockBackend *blk, int argc, char **argv)
  1804. {
  1805. BlockDriverState *bs = blk_bs(blk);
  1806. QemuOpts *qopts;
  1807. QDict *opts;
  1808. int c;
  1809. int flags = bs->open_flags;
  1810. bool writethrough = !blk_enable_write_cache(blk);
  1811. bool has_rw_option = false;
  1812. bool has_cache_option = false;
  1813. BlockReopenQueue *brq;
  1814. Error *local_err = NULL;
  1815. while ((c = getopt(argc, argv, "c:o:rw")) != -1) {
  1816. switch (c) {
  1817. case 'c':
  1818. if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
  1819. error_report("Invalid cache option: %s", optarg);
  1820. return -EINVAL;
  1821. }
  1822. has_cache_option = true;
  1823. break;
  1824. case 'o':
  1825. if (!qemu_opts_parse_noisily(&reopen_opts, optarg, 0)) {
  1826. qemu_opts_reset(&reopen_opts);
  1827. return -EINVAL;
  1828. }
  1829. break;
  1830. case 'r':
  1831. if (has_rw_option) {
  1832. error_report("Only one -r/-w option may be given");
  1833. return -EINVAL;
  1834. }
  1835. flags &= ~BDRV_O_RDWR;
  1836. has_rw_option = true;
  1837. break;
  1838. case 'w':
  1839. if (has_rw_option) {
  1840. error_report("Only one -r/-w option may be given");
  1841. return -EINVAL;
  1842. }
  1843. flags |= BDRV_O_RDWR;
  1844. has_rw_option = true;
  1845. break;
  1846. default:
  1847. qemu_opts_reset(&reopen_opts);
  1848. qemuio_command_usage(&reopen_cmd);
  1849. return -EINVAL;
  1850. }
  1851. }
  1852. if (optind != argc) {
  1853. qemu_opts_reset(&reopen_opts);
  1854. qemuio_command_usage(&reopen_cmd);
  1855. return -EINVAL;
  1856. }
  1857. if (!writethrough != blk_enable_write_cache(blk) &&
  1858. blk_get_attached_dev(blk))
  1859. {
  1860. error_report("Cannot change cache.writeback: Device attached");
  1861. qemu_opts_reset(&reopen_opts);
  1862. return -EBUSY;
  1863. }
  1864. if (!(flags & BDRV_O_RDWR)) {
  1865. uint64_t orig_perm, orig_shared_perm;
  1866. bdrv_drain(bs);
  1867. blk_get_perm(blk, &orig_perm, &orig_shared_perm);
  1868. blk_set_perm(blk,
  1869. orig_perm & ~(BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED),
  1870. orig_shared_perm,
  1871. &error_abort);
  1872. }
  1873. qopts = qemu_opts_find(&reopen_opts, NULL);
  1874. opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : qdict_new();
  1875. qemu_opts_reset(&reopen_opts);
  1876. if (qdict_haskey(opts, BDRV_OPT_READ_ONLY)) {
  1877. if (has_rw_option) {
  1878. error_report("Cannot set both -r/-w and '" BDRV_OPT_READ_ONLY "'");
  1879. qobject_unref(opts);
  1880. return -EINVAL;
  1881. }
  1882. } else {
  1883. qdict_put_bool(opts, BDRV_OPT_READ_ONLY, !(flags & BDRV_O_RDWR));
  1884. }
  1885. if (qdict_haskey(opts, BDRV_OPT_CACHE_DIRECT) ||
  1886. qdict_haskey(opts, BDRV_OPT_CACHE_NO_FLUSH)) {
  1887. if (has_cache_option) {
  1888. error_report("Cannot set both -c and the cache options");
  1889. qobject_unref(opts);
  1890. return -EINVAL;
  1891. }
  1892. } else {
  1893. qdict_put_bool(opts, BDRV_OPT_CACHE_DIRECT, flags & BDRV_O_NOCACHE);
  1894. qdict_put_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, flags & BDRV_O_NO_FLUSH);
  1895. }
  1896. bdrv_subtree_drained_begin(bs);
  1897. brq = bdrv_reopen_queue(NULL, bs, opts, true);
  1898. bdrv_reopen_multiple(brq, &local_err);
  1899. bdrv_subtree_drained_end(bs);
  1900. if (local_err) {
  1901. error_report_err(local_err);
  1902. return -EINVAL;
  1903. }
  1904. blk_set_enable_write_cache(blk, !writethrough);
  1905. return 0;
  1906. }
  1907. static int break_f(BlockBackend *blk, int argc, char **argv)
  1908. {
  1909. int ret;
  1910. ret = bdrv_debug_breakpoint(blk_bs(blk), argv[1], argv[2]);
  1911. if (ret < 0) {
  1912. printf("Could not set breakpoint: %s\n", strerror(-ret));
  1913. return ret;
  1914. }
  1915. return 0;
  1916. }
  1917. static int remove_break_f(BlockBackend *blk, int argc, char **argv)
  1918. {
  1919. int ret;
  1920. ret = bdrv_debug_remove_breakpoint(blk_bs(blk), argv[1]);
  1921. if (ret < 0) {
  1922. printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret));
  1923. return ret;
  1924. }
  1925. return 0;
  1926. }
  1927. static const cmdinfo_t break_cmd = {
  1928. .name = "break",
  1929. .argmin = 2,
  1930. .argmax = 2,
  1931. .cfunc = break_f,
  1932. .args = "event tag",
  1933. .oneline = "sets a breakpoint on event and tags the stopped "
  1934. "request as tag",
  1935. };
  1936. static const cmdinfo_t remove_break_cmd = {
  1937. .name = "remove_break",
  1938. .argmin = 1,
  1939. .argmax = 1,
  1940. .cfunc = remove_break_f,
  1941. .args = "tag",
  1942. .oneline = "remove a breakpoint by tag",
  1943. };
  1944. static int resume_f(BlockBackend *blk, int argc, char **argv)
  1945. {
  1946. int ret;
  1947. ret = bdrv_debug_resume(blk_bs(blk), argv[1]);
  1948. if (ret < 0) {
  1949. printf("Could not resume request: %s\n", strerror(-ret));
  1950. return ret;
  1951. }
  1952. return 0;
  1953. }
  1954. static const cmdinfo_t resume_cmd = {
  1955. .name = "resume",
  1956. .argmin = 1,
  1957. .argmax = 1,
  1958. .cfunc = resume_f,
  1959. .args = "tag",
  1960. .oneline = "resumes the request tagged as tag",
  1961. };
  1962. static int wait_break_f(BlockBackend *blk, int argc, char **argv)
  1963. {
  1964. while (!bdrv_debug_is_suspended(blk_bs(blk), argv[1])) {
  1965. aio_poll(blk_get_aio_context(blk), true);
  1966. }
  1967. return 0;
  1968. }
  1969. static const cmdinfo_t wait_break_cmd = {
  1970. .name = "wait_break",
  1971. .argmin = 1,
  1972. .argmax = 1,
  1973. .cfunc = wait_break_f,
  1974. .args = "tag",
  1975. .oneline = "waits for the suspension of a request",
  1976. };
  1977. static int abort_f(BlockBackend *blk, int argc, char **argv)
  1978. {
  1979. abort();
  1980. }
  1981. static const cmdinfo_t abort_cmd = {
  1982. .name = "abort",
  1983. .cfunc = abort_f,
  1984. .flags = CMD_NOFILE_OK,
  1985. .oneline = "simulate a program crash using abort(3)",
  1986. };
  1987. static void sigraise_help(void)
  1988. {
  1989. printf(
  1990. "\n"
  1991. " raises the given signal\n"
  1992. "\n"
  1993. " Example:\n"
  1994. " 'sigraise %i' - raises SIGTERM\n"
  1995. "\n"
  1996. " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
  1997. " given to sigraise.\n"
  1998. "\n", SIGTERM);
  1999. }
  2000. static int sigraise_f(BlockBackend *blk, int argc, char **argv);
  2001. static const cmdinfo_t sigraise_cmd = {
  2002. .name = "sigraise",
  2003. .cfunc = sigraise_f,
  2004. .argmin = 1,
  2005. .argmax = 1,
  2006. .flags = CMD_NOFILE_OK,
  2007. .args = "signal",
  2008. .oneline = "raises a signal",
  2009. .help = sigraise_help,
  2010. };
  2011. static int sigraise_f(BlockBackend *blk, int argc, char **argv)
  2012. {
  2013. int64_t sig = cvtnum(argv[1]);
  2014. if (sig < 0) {
  2015. print_cvtnum_err(sig, argv[1]);
  2016. return sig;
  2017. } else if (sig > NSIG) {
  2018. printf("signal argument '%s' is too large to be a valid signal\n",
  2019. argv[1]);
  2020. return -EINVAL;
  2021. }
  2022. /* Using raise() to kill this process does not necessarily flush all open
  2023. * streams. At least stdout and stderr (although the latter should be
  2024. * non-buffered anyway) should be flushed, though. */
  2025. fflush(stdout);
  2026. fflush(stderr);
  2027. raise(sig);
  2028. return 0;
  2029. }
  2030. static void sleep_cb(void *opaque)
  2031. {
  2032. bool *expired = opaque;
  2033. *expired = true;
  2034. }
  2035. static int sleep_f(BlockBackend *blk, int argc, char **argv)
  2036. {
  2037. char *endptr;
  2038. long ms;
  2039. struct QEMUTimer *timer;
  2040. bool expired = false;
  2041. ms = strtol(argv[1], &endptr, 0);
  2042. if (ms < 0 || *endptr != '\0') {
  2043. printf("%s is not a valid number\n", argv[1]);
  2044. return -EINVAL;
  2045. }
  2046. timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired);
  2047. timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms);
  2048. while (!expired) {
  2049. main_loop_wait(false);
  2050. }
  2051. timer_free(timer);
  2052. return 0;
  2053. }
  2054. static const cmdinfo_t sleep_cmd = {
  2055. .name = "sleep",
  2056. .argmin = 1,
  2057. .argmax = 1,
  2058. .cfunc = sleep_f,
  2059. .flags = CMD_NOFILE_OK,
  2060. .oneline = "waits for the given value in milliseconds",
  2061. };
  2062. static void help_oneline(const char *cmd, const cmdinfo_t *ct)
  2063. {
  2064. if (cmd) {
  2065. printf("%s ", cmd);
  2066. } else {
  2067. printf("%s ", ct->name);
  2068. if (ct->altname) {
  2069. printf("(or %s) ", ct->altname);
  2070. }
  2071. }
  2072. if (ct->args) {
  2073. printf("%s ", ct->args);
  2074. }
  2075. printf("-- %s\n", ct->oneline);
  2076. }
  2077. static void help_onecmd(const char *cmd, const cmdinfo_t *ct)
  2078. {
  2079. help_oneline(cmd, ct);
  2080. if (ct->help) {
  2081. ct->help();
  2082. }
  2083. }
  2084. static void help_all(void)
  2085. {
  2086. const cmdinfo_t *ct;
  2087. for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
  2088. help_oneline(ct->name, ct);
  2089. }
  2090. printf("\nUse 'help commandname' for extended help.\n");
  2091. }
  2092. static int help_f(BlockBackend *blk, int argc, char **argv)
  2093. {
  2094. const cmdinfo_t *ct;
  2095. if (argc == 1) {
  2096. help_all();
  2097. return 0;
  2098. }
  2099. ct = find_command(argv[1]);
  2100. if (ct == NULL) {
  2101. printf("command %s not found\n", argv[1]);
  2102. return -EINVAL;
  2103. }
  2104. help_onecmd(argv[1], ct);
  2105. return 0;
  2106. }
  2107. static const cmdinfo_t help_cmd = {
  2108. .name = "help",
  2109. .altname = "?",
  2110. .cfunc = help_f,
  2111. .argmin = 0,
  2112. .argmax = 1,
  2113. .flags = CMD_FLAG_GLOBAL,
  2114. .args = "[command]",
  2115. .oneline = "help for one or all commands",
  2116. };
  2117. int qemuio_command(BlockBackend *blk, const char *cmd)
  2118. {
  2119. AioContext *ctx;
  2120. char *input;
  2121. const cmdinfo_t *ct;
  2122. char **v;
  2123. int c;
  2124. int ret = 0;
  2125. input = g_strdup(cmd);
  2126. v = breakline(input, &c);
  2127. if (c) {
  2128. ct = find_command(v[0]);
  2129. if (ct) {
  2130. ctx = blk ? blk_get_aio_context(blk) : qemu_get_aio_context();
  2131. aio_context_acquire(ctx);
  2132. ret = command(blk, ct, c, v);
  2133. aio_context_release(ctx);
  2134. } else {
  2135. fprintf(stderr, "command \"%s\" not found\n", v[0]);
  2136. ret = -EINVAL;
  2137. }
  2138. }
  2139. g_free(input);
  2140. g_free(v);
  2141. return ret;
  2142. }
  2143. static void __attribute((constructor)) init_qemuio_commands(void)
  2144. {
  2145. /* initialize commands */
  2146. qemuio_add_command(&help_cmd);
  2147. qemuio_add_command(&read_cmd);
  2148. qemuio_add_command(&readv_cmd);
  2149. qemuio_add_command(&write_cmd);
  2150. qemuio_add_command(&writev_cmd);
  2151. qemuio_add_command(&aio_read_cmd);
  2152. qemuio_add_command(&aio_write_cmd);
  2153. qemuio_add_command(&aio_flush_cmd);
  2154. qemuio_add_command(&flush_cmd);
  2155. qemuio_add_command(&truncate_cmd);
  2156. qemuio_add_command(&length_cmd);
  2157. qemuio_add_command(&info_cmd);
  2158. qemuio_add_command(&discard_cmd);
  2159. qemuio_add_command(&alloc_cmd);
  2160. qemuio_add_command(&map_cmd);
  2161. qemuio_add_command(&reopen_cmd);
  2162. qemuio_add_command(&break_cmd);
  2163. qemuio_add_command(&remove_break_cmd);
  2164. qemuio_add_command(&resume_cmd);
  2165. qemuio_add_command(&wait_break_cmd);
  2166. qemuio_add_command(&abort_cmd);
  2167. qemuio_add_command(&sleep_cmd);
  2168. qemuio_add_command(&sigraise_cmd);
  2169. }