2
0

qemu-io-cmds.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813
  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 "system/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. #include "qemu/memalign.h"
  24. #define CMD_NOFILE_OK 0x01
  25. bool qemuio_misalign;
  26. static cmdinfo_t *cmdtab;
  27. static int ncmds;
  28. static int compare_cmdname(const void *a, const void *b)
  29. {
  30. return strcmp(((const cmdinfo_t *)a)->name,
  31. ((const cmdinfo_t *)b)->name);
  32. }
  33. void qemuio_add_command(const cmdinfo_t *ci)
  34. {
  35. /* ci->perm assumes a file is open, but the GLOBAL and NOFILE_OK
  36. * flags allow it not to be, so that combination is invalid.
  37. * Catch it now rather than letting it manifest as a crash if a
  38. * particular set of command line options are used.
  39. */
  40. assert(ci->perm == 0 ||
  41. (ci->flags & (CMD_FLAG_GLOBAL | CMD_NOFILE_OK)) == 0);
  42. cmdtab = g_renew(cmdinfo_t, cmdtab, ++ncmds);
  43. cmdtab[ncmds - 1] = *ci;
  44. qsort(cmdtab, ncmds, sizeof(*cmdtab), compare_cmdname);
  45. }
  46. void qemuio_command_usage(const cmdinfo_t *ci)
  47. {
  48. printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
  49. }
  50. static int init_check_command(BlockBackend *blk, const cmdinfo_t *ct)
  51. {
  52. if (ct->flags & CMD_FLAG_GLOBAL) {
  53. return 1;
  54. }
  55. if (!(ct->flags & CMD_NOFILE_OK) && !blk) {
  56. fprintf(stderr, "no file open, try 'help open'\n");
  57. return 0;
  58. }
  59. return 1;
  60. }
  61. static int command(BlockBackend *blk, const cmdinfo_t *ct, int argc,
  62. char **argv)
  63. {
  64. char *cmd = argv[0];
  65. if (!init_check_command(blk, ct)) {
  66. return -EINVAL;
  67. }
  68. if (argc - 1 < ct->argmin || (ct->argmax != -1 && argc - 1 > ct->argmax)) {
  69. if (ct->argmax == -1) {
  70. fprintf(stderr,
  71. "bad argument count %d to %s, expected at least %d arguments\n",
  72. argc-1, cmd, ct->argmin);
  73. } else if (ct->argmin == ct->argmax) {
  74. fprintf(stderr,
  75. "bad argument count %d to %s, expected %d arguments\n",
  76. argc-1, cmd, ct->argmin);
  77. } else {
  78. fprintf(stderr,
  79. "bad argument count %d to %s, expected between %d and %d arguments\n",
  80. argc-1, cmd, ct->argmin, ct->argmax);
  81. }
  82. return -EINVAL;
  83. }
  84. /*
  85. * Request additional permissions if necessary for this command. The caller
  86. * is responsible for restoring the original permissions afterwards if this
  87. * is what it wants.
  88. *
  89. * Coverity thinks that blk may be NULL in the following if condition. It's
  90. * not so: in init_check_command() we fail if blk is NULL for command with
  91. * both CMD_FLAG_GLOBAL and CMD_NOFILE_OK flags unset. And in
  92. * qemuio_add_command() we assert that command with non-zero .perm field
  93. * doesn't set this flags. So, the following assertion is to silence
  94. * Coverity:
  95. */
  96. assert(blk || !ct->perm);
  97. if (ct->perm && blk_is_available(blk)) {
  98. uint64_t orig_perm, orig_shared_perm;
  99. blk_get_perm(blk, &orig_perm, &orig_shared_perm);
  100. if (ct->perm & ~orig_perm) {
  101. uint64_t new_perm;
  102. Error *local_err = NULL;
  103. int ret;
  104. new_perm = orig_perm | ct->perm;
  105. ret = blk_set_perm(blk, new_perm, orig_shared_perm, &local_err);
  106. if (ret < 0) {
  107. error_report_err(local_err);
  108. return ret;
  109. }
  110. }
  111. }
  112. qemu_reset_optind();
  113. return ct->cfunc(blk, argc, argv);
  114. }
  115. static const cmdinfo_t *find_command(const char *cmd)
  116. {
  117. cmdinfo_t *ct;
  118. for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
  119. if (strcmp(ct->name, cmd) == 0 ||
  120. (ct->altname && strcmp(ct->altname, cmd) == 0))
  121. {
  122. return (const cmdinfo_t *)ct;
  123. }
  124. }
  125. return NULL;
  126. }
  127. /* Invoke fn() for commands with a matching prefix */
  128. void qemuio_complete_command(const char *input,
  129. void (*fn)(const char *cmd, void *opaque),
  130. void *opaque)
  131. {
  132. cmdinfo_t *ct;
  133. size_t input_len = strlen(input);
  134. for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
  135. if (strncmp(input, ct->name, input_len) == 0) {
  136. fn(ct->name, opaque);
  137. }
  138. }
  139. }
  140. static char **breakline(char *input, int *count)
  141. {
  142. int c = 0;
  143. char *p;
  144. char **rval = g_new0(char *, 1);
  145. while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
  146. if (!*p) {
  147. continue;
  148. }
  149. c++;
  150. rval = g_renew(char *, rval, (c + 1));
  151. rval[c - 1] = p;
  152. rval[c] = NULL;
  153. }
  154. *count = c;
  155. return rval;
  156. }
  157. static int64_t cvtnum(const char *s)
  158. {
  159. int err;
  160. uint64_t value;
  161. err = qemu_strtosz(s, NULL, &value);
  162. if (err < 0) {
  163. return err;
  164. }
  165. if (value > INT64_MAX) {
  166. return -ERANGE;
  167. }
  168. return value;
  169. }
  170. static void print_cvtnum_err(int64_t rc, const char *arg)
  171. {
  172. switch (rc) {
  173. case -EINVAL:
  174. printf("Parsing error: non-numeric argument,"
  175. " or extraneous/unrecognized suffix -- %s\n", arg);
  176. break;
  177. case -ERANGE:
  178. printf("Parsing error: argument too large -- %s\n", arg);
  179. break;
  180. default:
  181. printf("Parsing error: %s\n", arg);
  182. }
  183. }
  184. #define EXABYTES(x) ((long long)(x) << 60)
  185. #define PETABYTES(x) ((long long)(x) << 50)
  186. #define TERABYTES(x) ((long long)(x) << 40)
  187. #define GIGABYTES(x) ((long long)(x) << 30)
  188. #define MEGABYTES(x) ((long long)(x) << 20)
  189. #define KILOBYTES(x) ((long long)(x) << 10)
  190. #define TO_EXABYTES(x) ((x) / EXABYTES(1))
  191. #define TO_PETABYTES(x) ((x) / PETABYTES(1))
  192. #define TO_TERABYTES(x) ((x) / TERABYTES(1))
  193. #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
  194. #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
  195. #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
  196. static void cvtstr(double value, char *str, size_t size)
  197. {
  198. char *trim;
  199. const char *suffix;
  200. if (value >= EXABYTES(1)) {
  201. suffix = " EiB";
  202. snprintf(str, size - 4, "%.3f", TO_EXABYTES(value));
  203. } else if (value >= PETABYTES(1)) {
  204. suffix = " PiB";
  205. snprintf(str, size - 4, "%.3f", TO_PETABYTES(value));
  206. } else if (value >= TERABYTES(1)) {
  207. suffix = " TiB";
  208. snprintf(str, size - 4, "%.3f", TO_TERABYTES(value));
  209. } else if (value >= GIGABYTES(1)) {
  210. suffix = " GiB";
  211. snprintf(str, size - 4, "%.3f", TO_GIGABYTES(value));
  212. } else if (value >= MEGABYTES(1)) {
  213. suffix = " MiB";
  214. snprintf(str, size - 4, "%.3f", TO_MEGABYTES(value));
  215. } else if (value >= KILOBYTES(1)) {
  216. suffix = " KiB";
  217. snprintf(str, size - 4, "%.3f", TO_KILOBYTES(value));
  218. } else {
  219. suffix = " bytes";
  220. snprintf(str, size - 6, "%f", value);
  221. }
  222. trim = strstr(str, ".000");
  223. if (trim) {
  224. strcpy(trim, suffix);
  225. } else {
  226. strcat(str, suffix);
  227. }
  228. }
  229. static struct timespec tsub(struct timespec t1, struct timespec t2)
  230. {
  231. t1.tv_nsec -= t2.tv_nsec;
  232. if (t1.tv_nsec < 0) {
  233. t1.tv_nsec += NANOSECONDS_PER_SECOND;
  234. t1.tv_sec--;
  235. }
  236. t1.tv_sec -= t2.tv_sec;
  237. return t1;
  238. }
  239. static double tdiv(double value, struct timespec tv)
  240. {
  241. double seconds = tv.tv_sec + (tv.tv_nsec / 1e9);
  242. return value / seconds;
  243. }
  244. #define HOURS(sec) ((sec) / (60 * 60))
  245. #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
  246. #define SECONDS(sec) ((sec) % 60)
  247. enum {
  248. DEFAULT_TIME = 0x0,
  249. TERSE_FIXED_TIME = 0x1,
  250. VERBOSE_FIXED_TIME = 0x2,
  251. };
  252. static void timestr(struct timespec *tv, char *ts, size_t size, int format)
  253. {
  254. double frac_sec = tv->tv_nsec / 1e9;
  255. if (format & TERSE_FIXED_TIME) {
  256. if (!HOURS(tv->tv_sec)) {
  257. snprintf(ts, size, "%u:%05.2f",
  258. (unsigned int) MINUTES(tv->tv_sec),
  259. SECONDS(tv->tv_sec) + frac_sec);
  260. return;
  261. }
  262. format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
  263. }
  264. if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) {
  265. snprintf(ts, size, "%u:%02u:%05.2f",
  266. (unsigned int) HOURS(tv->tv_sec),
  267. (unsigned int) MINUTES(tv->tv_sec),
  268. SECONDS(tv->tv_sec) + frac_sec);
  269. } else {
  270. snprintf(ts, size, "%05.2f sec", frac_sec);
  271. }
  272. }
  273. /*
  274. * Parse the pattern argument to various sub-commands.
  275. *
  276. * Because the pattern is used as an argument to memset it must evaluate
  277. * to an unsigned integer that fits into a single byte.
  278. */
  279. static int parse_pattern(const char *arg)
  280. {
  281. char *endptr = NULL;
  282. long pattern;
  283. pattern = strtol(arg, &endptr, 0);
  284. if (pattern < 0 || pattern > UCHAR_MAX || *endptr != '\0') {
  285. printf("%s is not a valid pattern byte\n", arg);
  286. return -1;
  287. }
  288. return pattern;
  289. }
  290. /*
  291. * Memory allocation helpers.
  292. *
  293. * Make sure memory is aligned by default, or purposefully misaligned if
  294. * that is specified on the command line.
  295. */
  296. #define MISALIGN_OFFSET 16
  297. static void *qemu_io_alloc(BlockBackend *blk, size_t len, int pattern,
  298. bool register_buf)
  299. {
  300. void *buf;
  301. if (qemuio_misalign) {
  302. len += MISALIGN_OFFSET;
  303. }
  304. buf = blk_blockalign(blk, len);
  305. memset(buf, pattern, len);
  306. if (register_buf) {
  307. blk_register_buf(blk, buf, len, &error_abort);
  308. }
  309. if (qemuio_misalign) {
  310. buf += MISALIGN_OFFSET;
  311. }
  312. return buf;
  313. }
  314. static void qemu_io_free(BlockBackend *blk, void *p, size_t len,
  315. bool unregister_buf)
  316. {
  317. if (qemuio_misalign) {
  318. p -= MISALIGN_OFFSET;
  319. len += MISALIGN_OFFSET;
  320. }
  321. if (unregister_buf) {
  322. blk_unregister_buf(blk, p, len);
  323. }
  324. qemu_vfree(p);
  325. }
  326. /*
  327. * qemu_io_alloc_from_file()
  328. *
  329. * Allocates the buffer and populates it with the content of the given file
  330. * up to @len bytes. If the file length is less than @len, then the buffer
  331. * is populated with the file content cyclically.
  332. *
  333. * @blk - the block backend where the buffer content is going to be written to
  334. * @len - the buffer length
  335. * @file_name - the file to read the content from
  336. * @register_buf - call blk_register_buf()
  337. *
  338. * Returns: the buffer pointer on success
  339. * NULL on error
  340. */
  341. static void *qemu_io_alloc_from_file(BlockBackend *blk, size_t len,
  342. const char *file_name, bool register_buf)
  343. {
  344. size_t alloc_len = len + (qemuio_misalign ? MISALIGN_OFFSET : 0);
  345. char *alloc_buf, *buf, *end;
  346. FILE *f = fopen(file_name, "r");
  347. int pattern_len;
  348. if (!f) {
  349. perror(file_name);
  350. return NULL;
  351. }
  352. alloc_buf = buf = blk_blockalign(blk, alloc_len);
  353. if (qemuio_misalign) {
  354. buf += MISALIGN_OFFSET;
  355. }
  356. pattern_len = fread(buf, 1, len, f);
  357. if (ferror(f)) {
  358. perror(file_name);
  359. goto error;
  360. }
  361. if (pattern_len == 0) {
  362. fprintf(stderr, "%s: file is empty\n", file_name);
  363. goto error;
  364. }
  365. fclose(f);
  366. f = NULL;
  367. if (register_buf) {
  368. blk_register_buf(blk, alloc_buf, alloc_len, &error_abort);
  369. }
  370. end = buf + len;
  371. for (char *p = buf + pattern_len; p < end; p += pattern_len) {
  372. memcpy(p, buf, MIN(pattern_len, end - p));
  373. }
  374. return buf;
  375. error:
  376. /*
  377. * This code path is only taken before blk_register_buf() is called, so
  378. * hardcode the qemu_io_free() unregister_buf argument to false.
  379. */
  380. qemu_io_free(blk, alloc_buf, alloc_len, false);
  381. if (f) {
  382. fclose(f);
  383. }
  384. return NULL;
  385. }
  386. static void dump_buffer(const void *buffer, int64_t offset, int64_t len)
  387. {
  388. uint64_t i;
  389. int j;
  390. const uint8_t *p;
  391. for (i = 0, p = buffer; i < len; i += 16) {
  392. const uint8_t *s = p;
  393. printf("%08" PRIx64 ": ", offset + i);
  394. for (j = 0; j < 16 && i + j < len; j++, p++) {
  395. printf("%02x ", *p);
  396. }
  397. printf(" ");
  398. for (j = 0; j < 16 && i + j < len; j++, s++) {
  399. if (isalnum(*s)) {
  400. printf("%c", *s);
  401. } else {
  402. printf(".");
  403. }
  404. }
  405. printf("\n");
  406. }
  407. }
  408. static void print_report(const char *op, struct timespec *t, int64_t offset,
  409. int64_t count, int64_t total, int cnt, bool Cflag)
  410. {
  411. char s1[64], s2[64], ts[64];
  412. timestr(t, ts, sizeof(ts), Cflag ? VERBOSE_FIXED_TIME : 0);
  413. if (!Cflag) {
  414. cvtstr((double)total, s1, sizeof(s1));
  415. cvtstr(tdiv((double)total, *t), s2, sizeof(s2));
  416. printf("%s %"PRId64"/%"PRId64" bytes at offset %" PRId64 "\n",
  417. op, total, count, offset);
  418. printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
  419. s1, cnt, ts, s2, tdiv((double)cnt, *t));
  420. } else {/* bytes,ops,time,bytes/sec,ops/sec */
  421. printf("%"PRId64",%d,%s,%.3f,%.3f\n",
  422. total, cnt, ts,
  423. tdiv((double)total, *t),
  424. tdiv((double)cnt, *t));
  425. }
  426. }
  427. /*
  428. * Parse multiple length statements for vectored I/O, and construct an I/O
  429. * vector matching it.
  430. */
  431. static void *
  432. create_iovec(BlockBackend *blk, QEMUIOVector *qiov, char **argv, int nr_iov,
  433. int pattern, bool register_buf)
  434. {
  435. size_t *sizes = g_new0(size_t, nr_iov);
  436. size_t count = 0;
  437. void *buf = NULL;
  438. void *p;
  439. int i;
  440. for (i = 0; i < nr_iov; i++) {
  441. char *arg = argv[i];
  442. int64_t len;
  443. len = cvtnum(arg);
  444. if (len < 0) {
  445. print_cvtnum_err(len, arg);
  446. goto fail;
  447. }
  448. if (len > BDRV_REQUEST_MAX_BYTES) {
  449. printf("Argument '%s' exceeds maximum size %" PRIu64 "\n", arg,
  450. (uint64_t)BDRV_REQUEST_MAX_BYTES);
  451. goto fail;
  452. }
  453. if (count > BDRV_REQUEST_MAX_BYTES - len) {
  454. printf("The total number of bytes exceed the maximum size %" PRIu64
  455. "\n", (uint64_t)BDRV_REQUEST_MAX_BYTES);
  456. goto fail;
  457. }
  458. sizes[i] = len;
  459. count += len;
  460. }
  461. qemu_iovec_init(qiov, nr_iov);
  462. buf = p = qemu_io_alloc(blk, count, pattern, register_buf);
  463. for (i = 0; i < nr_iov; i++) {
  464. qemu_iovec_add(qiov, p, sizes[i]);
  465. p += sizes[i];
  466. }
  467. fail:
  468. g_free(sizes);
  469. return buf;
  470. }
  471. static int do_pread(BlockBackend *blk, char *buf, int64_t offset,
  472. int64_t bytes, BdrvRequestFlags flags, int64_t *total)
  473. {
  474. int ret;
  475. if (bytes > INT_MAX) {
  476. return -ERANGE;
  477. }
  478. ret = blk_pread(blk, offset, bytes, (uint8_t *)buf, flags);
  479. if (ret < 0) {
  480. return ret;
  481. }
  482. *total = bytes;
  483. return 1;
  484. }
  485. static int do_pwrite(BlockBackend *blk, char *buf, int64_t offset,
  486. int64_t bytes, BdrvRequestFlags flags, int64_t *total)
  487. {
  488. int ret;
  489. if (bytes > INT_MAX) {
  490. return -ERANGE;
  491. }
  492. ret = blk_pwrite(blk, offset, bytes, (uint8_t *)buf, flags);
  493. if (ret < 0) {
  494. return ret;
  495. }
  496. *total = bytes;
  497. return 1;
  498. }
  499. static int do_pwrite_zeroes(BlockBackend *blk, int64_t offset,
  500. int64_t bytes, BdrvRequestFlags flags,
  501. int64_t *total)
  502. {
  503. int ret = blk_pwrite_zeroes(blk, offset, bytes,
  504. flags | BDRV_REQ_ZERO_WRITE);
  505. if (ret < 0) {
  506. return ret;
  507. }
  508. *total = bytes;
  509. return 1;
  510. }
  511. static int do_write_compressed(BlockBackend *blk, char *buf, int64_t offset,
  512. int64_t bytes, int64_t *total)
  513. {
  514. int ret;
  515. if (bytes > BDRV_REQUEST_MAX_BYTES) {
  516. return -ERANGE;
  517. }
  518. ret = blk_pwrite_compressed(blk, offset, bytes, buf);
  519. if (ret < 0) {
  520. return ret;
  521. }
  522. *total = bytes;
  523. return 1;
  524. }
  525. static int do_load_vmstate(BlockBackend *blk, char *buf, int64_t offset,
  526. int64_t count, int64_t *total)
  527. {
  528. if (count > INT_MAX) {
  529. return -ERANGE;
  530. }
  531. *total = blk_load_vmstate(blk, (uint8_t *)buf, offset, count);
  532. if (*total < 0) {
  533. return *total;
  534. }
  535. return 1;
  536. }
  537. static int do_save_vmstate(BlockBackend *blk, char *buf, int64_t offset,
  538. int64_t count, int64_t *total)
  539. {
  540. if (count > INT_MAX) {
  541. return -ERANGE;
  542. }
  543. *total = blk_save_vmstate(blk, (uint8_t *)buf, offset, count);
  544. if (*total < 0) {
  545. return *total;
  546. }
  547. return 1;
  548. }
  549. #define NOT_DONE 0x7fffffff
  550. static void aio_rw_done(void *opaque, int ret)
  551. {
  552. *(int *)opaque = ret;
  553. }
  554. static int do_aio_readv(BlockBackend *blk, QEMUIOVector *qiov,
  555. int64_t offset, BdrvRequestFlags flags, int *total)
  556. {
  557. int async_ret = NOT_DONE;
  558. blk_aio_preadv(blk, offset, qiov, flags, aio_rw_done, &async_ret);
  559. while (async_ret == NOT_DONE) {
  560. main_loop_wait(false);
  561. }
  562. *total = qiov->size;
  563. return async_ret < 0 ? async_ret : 1;
  564. }
  565. static int do_aio_writev(BlockBackend *blk, QEMUIOVector *qiov,
  566. int64_t offset, BdrvRequestFlags flags, int *total)
  567. {
  568. int async_ret = NOT_DONE;
  569. blk_aio_pwritev(blk, offset, qiov, flags, aio_rw_done, &async_ret);
  570. while (async_ret == NOT_DONE) {
  571. main_loop_wait(false);
  572. }
  573. *total = qiov->size;
  574. return async_ret < 0 ? async_ret : 1;
  575. }
  576. static void read_help(void)
  577. {
  578. printf(
  579. "\n"
  580. " reads a range of bytes from the given offset\n"
  581. "\n"
  582. " Example:\n"
  583. " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
  584. "\n"
  585. " Reads a segment of the currently open file, optionally dumping it to the\n"
  586. " standard output stream (with -v option) for subsequent inspection.\n"
  587. " -b, -- read from the VM state rather than the virtual disk\n"
  588. " -C, -- report statistics in a machine parsable format\n"
  589. " -l, -- length for pattern verification (only with -P)\n"
  590. " -p, -- ignored for backwards compatibility\n"
  591. " -P, -- use a pattern to verify read data\n"
  592. " -q, -- quiet mode, do not show I/O statistics\n"
  593. " -r, -- register I/O buffer\n"
  594. " -s, -- start offset for pattern verification (only with -P)\n"
  595. " -v, -- dump buffer to standard output\n"
  596. "\n");
  597. }
  598. static int read_f(BlockBackend *blk, int argc, char **argv);
  599. static const cmdinfo_t read_cmd = {
  600. .name = "read",
  601. .altname = "r",
  602. .cfunc = read_f,
  603. .argmin = 2,
  604. .argmax = -1,
  605. .args = "[-abCqrv] [-P pattern [-s off] [-l len]] off len",
  606. .oneline = "reads a number of bytes at a specified offset",
  607. .help = read_help,
  608. };
  609. static int read_f(BlockBackend *blk, int argc, char **argv)
  610. {
  611. struct timespec t1, t2;
  612. bool Cflag = false, qflag = false, vflag = false;
  613. bool Pflag = false, sflag = false, lflag = false, bflag = false;
  614. int c, cnt, ret;
  615. char *buf;
  616. int64_t offset;
  617. int64_t count;
  618. /* Some compilers get confused and warn if this is not initialized. */
  619. int64_t total = 0;
  620. int pattern = 0;
  621. int64_t pattern_offset = 0, pattern_count = 0;
  622. BdrvRequestFlags flags = 0;
  623. while ((c = getopt(argc, argv, "bCl:pP:qrs:v")) != -1) {
  624. switch (c) {
  625. case 'b':
  626. bflag = true;
  627. break;
  628. case 'C':
  629. Cflag = true;
  630. break;
  631. case 'l':
  632. lflag = true;
  633. pattern_count = cvtnum(optarg);
  634. if (pattern_count < 0) {
  635. print_cvtnum_err(pattern_count, optarg);
  636. return pattern_count;
  637. }
  638. break;
  639. case 'p':
  640. /* Ignored for backwards compatibility */
  641. break;
  642. case 'P':
  643. Pflag = true;
  644. pattern = parse_pattern(optarg);
  645. if (pattern < 0) {
  646. return -EINVAL;
  647. }
  648. break;
  649. case 'q':
  650. qflag = true;
  651. break;
  652. case 'r':
  653. flags |= BDRV_REQ_REGISTERED_BUF;
  654. break;
  655. case 's':
  656. sflag = true;
  657. pattern_offset = cvtnum(optarg);
  658. if (pattern_offset < 0) {
  659. print_cvtnum_err(pattern_offset, optarg);
  660. return pattern_offset;
  661. }
  662. break;
  663. case 'v':
  664. vflag = true;
  665. break;
  666. default:
  667. qemuio_command_usage(&read_cmd);
  668. return -EINVAL;
  669. }
  670. }
  671. if (optind != argc - 2) {
  672. qemuio_command_usage(&read_cmd);
  673. return -EINVAL;
  674. }
  675. offset = cvtnum(argv[optind]);
  676. if (offset < 0) {
  677. print_cvtnum_err(offset, argv[optind]);
  678. return offset;
  679. }
  680. optind++;
  681. count = cvtnum(argv[optind]);
  682. if (count < 0) {
  683. print_cvtnum_err(count, argv[optind]);
  684. return count;
  685. } else if (count > BDRV_REQUEST_MAX_BYTES) {
  686. printf("length cannot exceed %" PRIu64 ", given %s\n",
  687. (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
  688. return -EINVAL;
  689. }
  690. if (!Pflag && (lflag || sflag)) {
  691. qemuio_command_usage(&read_cmd);
  692. return -EINVAL;
  693. }
  694. if (!lflag) {
  695. pattern_count = count - pattern_offset;
  696. }
  697. if ((pattern_count < 0) || (pattern_count + pattern_offset > count)) {
  698. printf("pattern verification range exceeds end of read data\n");
  699. return -EINVAL;
  700. }
  701. if (bflag) {
  702. if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) {
  703. printf("%" PRId64 " is not a sector-aligned value for 'offset'\n",
  704. offset);
  705. return -EINVAL;
  706. }
  707. if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) {
  708. printf("%"PRId64" is not a sector-aligned value for 'count'\n",
  709. count);
  710. return -EINVAL;
  711. }
  712. if (flags & BDRV_REQ_REGISTERED_BUF) {
  713. printf("I/O buffer registration is not supported when reading "
  714. "from vmstate\n");
  715. return -EINVAL;
  716. }
  717. }
  718. buf = qemu_io_alloc(blk, count, 0xab, flags & BDRV_REQ_REGISTERED_BUF);
  719. clock_gettime(CLOCK_MONOTONIC, &t1);
  720. if (bflag) {
  721. ret = do_load_vmstate(blk, buf, offset, count, &total);
  722. } else {
  723. ret = do_pread(blk, buf, offset, count, flags, &total);
  724. }
  725. clock_gettime(CLOCK_MONOTONIC, &t2);
  726. if (ret < 0) {
  727. printf("read failed: %s\n", strerror(-ret));
  728. goto out;
  729. }
  730. cnt = ret;
  731. ret = 0;
  732. if (Pflag) {
  733. void *cmp_buf = g_malloc(pattern_count);
  734. memset(cmp_buf, pattern, pattern_count);
  735. if (memcmp(buf + pattern_offset, cmp_buf, pattern_count)) {
  736. printf("Pattern verification failed at offset %"
  737. PRId64 ", %"PRId64" bytes\n",
  738. offset + pattern_offset, pattern_count);
  739. ret = -EINVAL;
  740. }
  741. g_free(cmp_buf);
  742. }
  743. if (qflag) {
  744. goto out;
  745. }
  746. if (vflag) {
  747. dump_buffer(buf, offset, count);
  748. }
  749. /* Finally, report back -- -C gives a parsable format */
  750. t2 = tsub(t2, t1);
  751. print_report("read", &t2, offset, count, total, cnt, Cflag);
  752. out:
  753. qemu_io_free(blk, buf, count, flags & BDRV_REQ_REGISTERED_BUF);
  754. return ret;
  755. }
  756. static void readv_help(void)
  757. {
  758. printf(
  759. "\n"
  760. " reads a range of bytes from the given offset into multiple buffers\n"
  761. "\n"
  762. " Example:\n"
  763. " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
  764. "\n"
  765. " Reads a segment of the currently open file, optionally dumping it to the\n"
  766. " standard output stream (with -v option) for subsequent inspection.\n"
  767. " Uses multiple iovec buffers if more than one byte range is specified.\n"
  768. " -C, -- report statistics in a machine parsable format\n"
  769. " -P, -- use a pattern to verify read data\n"
  770. " -q, -- quiet mode, do not show I/O statistics\n"
  771. " -r, -- register I/O buffer\n"
  772. " -v, -- dump buffer to standard output\n"
  773. "\n");
  774. }
  775. static int readv_f(BlockBackend *blk, int argc, char **argv);
  776. static const cmdinfo_t readv_cmd = {
  777. .name = "readv",
  778. .cfunc = readv_f,
  779. .argmin = 2,
  780. .argmax = -1,
  781. .args = "[-Cqrv] [-P pattern] off len [len..]",
  782. .oneline = "reads a number of bytes at a specified offset",
  783. .help = readv_help,
  784. };
  785. static int readv_f(BlockBackend *blk, int argc, char **argv)
  786. {
  787. struct timespec t1, t2;
  788. bool Cflag = false, qflag = false, vflag = false;
  789. int c, cnt, ret;
  790. char *buf;
  791. int64_t offset;
  792. /* Some compilers get confused and warn if this is not initialized. */
  793. int total = 0;
  794. int nr_iov;
  795. QEMUIOVector qiov;
  796. int pattern = 0;
  797. bool Pflag = false;
  798. BdrvRequestFlags flags = 0;
  799. while ((c = getopt(argc, argv, "CP:qrv")) != -1) {
  800. switch (c) {
  801. case 'C':
  802. Cflag = true;
  803. break;
  804. case 'P':
  805. Pflag = true;
  806. pattern = parse_pattern(optarg);
  807. if (pattern < 0) {
  808. return -EINVAL;
  809. }
  810. break;
  811. case 'q':
  812. qflag = true;
  813. break;
  814. case 'r':
  815. flags |= BDRV_REQ_REGISTERED_BUF;
  816. break;
  817. case 'v':
  818. vflag = true;
  819. break;
  820. default:
  821. qemuio_command_usage(&readv_cmd);
  822. return -EINVAL;
  823. }
  824. }
  825. if (optind > argc - 2) {
  826. qemuio_command_usage(&readv_cmd);
  827. return -EINVAL;
  828. }
  829. offset = cvtnum(argv[optind]);
  830. if (offset < 0) {
  831. print_cvtnum_err(offset, argv[optind]);
  832. return offset;
  833. }
  834. optind++;
  835. nr_iov = argc - optind;
  836. buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, 0xab,
  837. flags & BDRV_REQ_REGISTERED_BUF);
  838. if (buf == NULL) {
  839. return -EINVAL;
  840. }
  841. clock_gettime(CLOCK_MONOTONIC, &t1);
  842. ret = do_aio_readv(blk, &qiov, offset, flags, &total);
  843. clock_gettime(CLOCK_MONOTONIC, &t2);
  844. if (ret < 0) {
  845. printf("readv failed: %s\n", strerror(-ret));
  846. goto out;
  847. }
  848. cnt = ret;
  849. ret = 0;
  850. if (Pflag) {
  851. void *cmp_buf = g_malloc(qiov.size);
  852. memset(cmp_buf, pattern, qiov.size);
  853. if (memcmp(buf, cmp_buf, qiov.size)) {
  854. printf("Pattern verification failed at offset %"
  855. PRId64 ", %zu bytes\n", offset, qiov.size);
  856. ret = -EINVAL;
  857. }
  858. g_free(cmp_buf);
  859. }
  860. if (qflag) {
  861. goto out;
  862. }
  863. if (vflag) {
  864. dump_buffer(buf, offset, qiov.size);
  865. }
  866. /* Finally, report back -- -C gives a parsable format */
  867. t2 = tsub(t2, t1);
  868. print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
  869. out:
  870. qemu_io_free(blk, buf, qiov.size, flags & BDRV_REQ_REGISTERED_BUF);
  871. qemu_iovec_destroy(&qiov);
  872. return ret;
  873. }
  874. static void write_help(void)
  875. {
  876. printf(
  877. "\n"
  878. " writes a range of bytes from the given offset\n"
  879. "\n"
  880. " Example:\n"
  881. " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
  882. "\n"
  883. " Writes into a segment of the currently open file, using a buffer\n"
  884. " filled with a set pattern (0xcdcdcdcd).\n"
  885. " -b, -- write to the VM state rather than the virtual disk\n"
  886. " -c, -- write compressed data with blk_write_compressed\n"
  887. " -C, -- report statistics in a machine parsable format\n"
  888. " -f, -- use Force Unit Access semantics\n"
  889. " -n, -- with -z, don't allow slow fallback\n"
  890. " -p, -- ignored for backwards compatibility\n"
  891. " -P, -- use different pattern to fill file\n"
  892. " -q, -- quiet mode, do not show I/O statistics\n"
  893. " -r, -- register I/O buffer\n"
  894. " -s, -- use a pattern file to fill the write buffer\n"
  895. " -u, -- with -z, allow unmapping\n"
  896. " -z, -- write zeroes using blk_pwrite_zeroes\n"
  897. "\n");
  898. }
  899. static int write_f(BlockBackend *blk, int argc, char **argv);
  900. static const cmdinfo_t write_cmd = {
  901. .name = "write",
  902. .altname = "w",
  903. .cfunc = write_f,
  904. .perm = BLK_PERM_WRITE,
  905. .argmin = 2,
  906. .argmax = -1,
  907. .args = "[-bcCfnqruz] [-P pattern | -s source_file] off len",
  908. .oneline = "writes a number of bytes at a specified offset",
  909. .help = write_help,
  910. };
  911. static int write_f(BlockBackend *blk, int argc, char **argv)
  912. {
  913. struct timespec t1, t2;
  914. bool Cflag = false, qflag = false, bflag = false;
  915. bool Pflag = false, zflag = false, cflag = false, sflag = false;
  916. BdrvRequestFlags flags = 0;
  917. int c, cnt, ret;
  918. char *buf = NULL;
  919. int64_t offset;
  920. int64_t count;
  921. /* Some compilers get confused and warn if this is not initialized. */
  922. int64_t total = 0;
  923. int pattern = 0xcd;
  924. const char *file_name = NULL;
  925. while ((c = getopt(argc, argv, "bcCfnpP:qrs:uz")) != -1) {
  926. switch (c) {
  927. case 'b':
  928. bflag = true;
  929. break;
  930. case 'c':
  931. cflag = true;
  932. break;
  933. case 'C':
  934. Cflag = true;
  935. break;
  936. case 'f':
  937. flags |= BDRV_REQ_FUA;
  938. break;
  939. case 'n':
  940. flags |= BDRV_REQ_NO_FALLBACK;
  941. break;
  942. case 'p':
  943. /* Ignored for backwards compatibility */
  944. break;
  945. case 'P':
  946. Pflag = true;
  947. pattern = parse_pattern(optarg);
  948. if (pattern < 0) {
  949. return -EINVAL;
  950. }
  951. break;
  952. case 'q':
  953. qflag = true;
  954. break;
  955. case 'r':
  956. flags |= BDRV_REQ_REGISTERED_BUF;
  957. break;
  958. case 's':
  959. sflag = true;
  960. file_name = optarg;
  961. break;
  962. case 'u':
  963. flags |= BDRV_REQ_MAY_UNMAP;
  964. break;
  965. case 'z':
  966. zflag = true;
  967. break;
  968. default:
  969. qemuio_command_usage(&write_cmd);
  970. return -EINVAL;
  971. }
  972. }
  973. if (optind != argc - 2) {
  974. qemuio_command_usage(&write_cmd);
  975. return -EINVAL;
  976. }
  977. if (bflag && zflag) {
  978. printf("-b and -z cannot be specified at the same time\n");
  979. return -EINVAL;
  980. }
  981. if ((flags & BDRV_REQ_FUA) && (bflag || cflag)) {
  982. printf("-f and -b or -c cannot be specified at the same time\n");
  983. return -EINVAL;
  984. }
  985. if ((flags & BDRV_REQ_NO_FALLBACK) && !zflag) {
  986. printf("-n requires -z to be specified\n");
  987. return -EINVAL;
  988. }
  989. if ((flags & BDRV_REQ_MAY_UNMAP) && !zflag) {
  990. printf("-u requires -z to be specified\n");
  991. return -EINVAL;
  992. }
  993. if (zflag + Pflag + sflag > 1) {
  994. printf("Only one of -z, -P, and -s "
  995. "can be specified at the same time\n");
  996. return -EINVAL;
  997. }
  998. offset = cvtnum(argv[optind]);
  999. if (offset < 0) {
  1000. print_cvtnum_err(offset, argv[optind]);
  1001. return offset;
  1002. }
  1003. optind++;
  1004. count = cvtnum(argv[optind]);
  1005. if (count < 0) {
  1006. print_cvtnum_err(count, argv[optind]);
  1007. return count;
  1008. } else if (count > BDRV_REQUEST_MAX_BYTES &&
  1009. !(flags & BDRV_REQ_NO_FALLBACK)) {
  1010. printf("length cannot exceed %" PRIu64 " without -n, given %s\n",
  1011. (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
  1012. return -EINVAL;
  1013. }
  1014. if (bflag || cflag) {
  1015. if (!QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE)) {
  1016. printf("%" PRId64 " is not a sector-aligned value for 'offset'\n",
  1017. offset);
  1018. return -EINVAL;
  1019. }
  1020. if (!QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)) {
  1021. printf("%"PRId64" is not a sector-aligned value for 'count'\n",
  1022. count);
  1023. return -EINVAL;
  1024. }
  1025. }
  1026. if (zflag) {
  1027. if (flags & BDRV_REQ_REGISTERED_BUF) {
  1028. printf("cannot combine zero write with registered I/O buffer\n");
  1029. return -EINVAL;
  1030. }
  1031. } else {
  1032. if (sflag) {
  1033. buf = qemu_io_alloc_from_file(blk, count, file_name,
  1034. flags & BDRV_REQ_REGISTERED_BUF);
  1035. if (!buf) {
  1036. return -EINVAL;
  1037. }
  1038. } else {
  1039. buf = qemu_io_alloc(blk, count, pattern,
  1040. flags & BDRV_REQ_REGISTERED_BUF);
  1041. }
  1042. }
  1043. clock_gettime(CLOCK_MONOTONIC, &t1);
  1044. if (bflag) {
  1045. ret = do_save_vmstate(blk, buf, offset, count, &total);
  1046. } else if (zflag) {
  1047. ret = do_pwrite_zeroes(blk, offset, count, flags, &total);
  1048. } else if (cflag) {
  1049. ret = do_write_compressed(blk, buf, offset, count, &total);
  1050. } else {
  1051. ret = do_pwrite(blk, buf, offset, count, flags, &total);
  1052. }
  1053. clock_gettime(CLOCK_MONOTONIC, &t2);
  1054. if (ret < 0) {
  1055. printf("write failed: %s\n", strerror(-ret));
  1056. goto out;
  1057. }
  1058. cnt = ret;
  1059. ret = 0;
  1060. if (qflag) {
  1061. goto out;
  1062. }
  1063. /* Finally, report back -- -C gives a parsable format */
  1064. t2 = tsub(t2, t1);
  1065. print_report("wrote", &t2, offset, count, total, cnt, Cflag);
  1066. out:
  1067. if (!zflag) {
  1068. qemu_io_free(blk, buf, count, flags & BDRV_REQ_REGISTERED_BUF);
  1069. }
  1070. return ret;
  1071. }
  1072. static void
  1073. writev_help(void)
  1074. {
  1075. printf(
  1076. "\n"
  1077. " writes a range of bytes from the given offset source from multiple buffers\n"
  1078. "\n"
  1079. " Example:\n"
  1080. " 'writev 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
  1081. "\n"
  1082. " Writes into a segment of the currently open file, using a buffer\n"
  1083. " filled with a set pattern (0xcdcdcdcd).\n"
  1084. " -C, -- report statistics in a machine parsable format\n"
  1085. " -f, -- use Force Unit Access semantics\n"
  1086. " -P, -- use different pattern to fill file\n"
  1087. " -q, -- quiet mode, do not show I/O statistics\n"
  1088. " -r, -- register I/O buffer\n"
  1089. "\n");
  1090. }
  1091. static int writev_f(BlockBackend *blk, int argc, char **argv);
  1092. static const cmdinfo_t writev_cmd = {
  1093. .name = "writev",
  1094. .cfunc = writev_f,
  1095. .perm = BLK_PERM_WRITE,
  1096. .argmin = 2,
  1097. .argmax = -1,
  1098. .args = "[-Cfqr] [-P pattern] off len [len..]",
  1099. .oneline = "writes a number of bytes at a specified offset",
  1100. .help = writev_help,
  1101. };
  1102. static int writev_f(BlockBackend *blk, int argc, char **argv)
  1103. {
  1104. struct timespec t1, t2;
  1105. bool Cflag = false, qflag = false;
  1106. BdrvRequestFlags flags = 0;
  1107. int c, cnt, ret;
  1108. char *buf;
  1109. int64_t offset;
  1110. /* Some compilers get confused and warn if this is not initialized. */
  1111. int total = 0;
  1112. int nr_iov;
  1113. int pattern = 0xcd;
  1114. QEMUIOVector qiov;
  1115. while ((c = getopt(argc, argv, "CfP:qr")) != -1) {
  1116. switch (c) {
  1117. case 'C':
  1118. Cflag = true;
  1119. break;
  1120. case 'f':
  1121. flags |= BDRV_REQ_FUA;
  1122. break;
  1123. case 'q':
  1124. qflag = true;
  1125. break;
  1126. case 'r':
  1127. flags |= BDRV_REQ_REGISTERED_BUF;
  1128. break;
  1129. case 'P':
  1130. pattern = parse_pattern(optarg);
  1131. if (pattern < 0) {
  1132. return -EINVAL;
  1133. }
  1134. break;
  1135. default:
  1136. qemuio_command_usage(&writev_cmd);
  1137. return -EINVAL;
  1138. }
  1139. }
  1140. if (optind > argc - 2) {
  1141. qemuio_command_usage(&writev_cmd);
  1142. return -EINVAL;
  1143. }
  1144. offset = cvtnum(argv[optind]);
  1145. if (offset < 0) {
  1146. print_cvtnum_err(offset, argv[optind]);
  1147. return offset;
  1148. }
  1149. optind++;
  1150. nr_iov = argc - optind;
  1151. buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, pattern,
  1152. flags & BDRV_REQ_REGISTERED_BUF);
  1153. if (buf == NULL) {
  1154. return -EINVAL;
  1155. }
  1156. clock_gettime(CLOCK_MONOTONIC, &t1);
  1157. ret = do_aio_writev(blk, &qiov, offset, flags, &total);
  1158. clock_gettime(CLOCK_MONOTONIC, &t2);
  1159. if (ret < 0) {
  1160. printf("writev failed: %s\n", strerror(-ret));
  1161. goto out;
  1162. }
  1163. cnt = ret;
  1164. ret = 0;
  1165. if (qflag) {
  1166. goto out;
  1167. }
  1168. /* Finally, report back -- -C gives a parsable format */
  1169. t2 = tsub(t2, t1);
  1170. print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
  1171. out:
  1172. qemu_io_free(blk, buf, qiov.size, flags & BDRV_REQ_REGISTERED_BUF);
  1173. qemu_iovec_destroy(&qiov);
  1174. return ret;
  1175. }
  1176. struct aio_ctx {
  1177. BlockBackend *blk;
  1178. QEMUIOVector qiov;
  1179. int64_t offset;
  1180. char *buf;
  1181. bool qflag;
  1182. bool vflag;
  1183. bool Cflag;
  1184. bool Pflag;
  1185. bool zflag;
  1186. BlockAcctCookie acct;
  1187. int pattern;
  1188. BdrvRequestFlags flags;
  1189. struct timespec t1;
  1190. };
  1191. static void aio_write_done(void *opaque, int ret)
  1192. {
  1193. struct aio_ctx *ctx = opaque;
  1194. struct timespec t2;
  1195. clock_gettime(CLOCK_MONOTONIC, &t2);
  1196. if (ret < 0) {
  1197. printf("aio_write failed: %s\n", strerror(-ret));
  1198. block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
  1199. goto out;
  1200. }
  1201. block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
  1202. if (ctx->qflag) {
  1203. goto out;
  1204. }
  1205. /* Finally, report back -- -C gives a parsable format */
  1206. t2 = tsub(t2, ctx->t1);
  1207. print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
  1208. ctx->qiov.size, 1, ctx->Cflag);
  1209. out:
  1210. if (!ctx->zflag) {
  1211. qemu_io_free(ctx->blk, ctx->buf, ctx->qiov.size,
  1212. ctx->flags & BDRV_REQ_REGISTERED_BUF);
  1213. qemu_iovec_destroy(&ctx->qiov);
  1214. }
  1215. g_free(ctx);
  1216. }
  1217. static void aio_read_done(void *opaque, int ret)
  1218. {
  1219. struct aio_ctx *ctx = opaque;
  1220. struct timespec t2;
  1221. clock_gettime(CLOCK_MONOTONIC, &t2);
  1222. if (ret < 0) {
  1223. printf("readv failed: %s\n", strerror(-ret));
  1224. block_acct_failed(blk_get_stats(ctx->blk), &ctx->acct);
  1225. goto out;
  1226. }
  1227. if (ctx->Pflag) {
  1228. void *cmp_buf = g_malloc(ctx->qiov.size);
  1229. memset(cmp_buf, ctx->pattern, ctx->qiov.size);
  1230. if (memcmp(ctx->buf, cmp_buf, ctx->qiov.size)) {
  1231. printf("Pattern verification failed at offset %"
  1232. PRId64 ", %zu bytes\n", ctx->offset, ctx->qiov.size);
  1233. }
  1234. g_free(cmp_buf);
  1235. }
  1236. block_acct_done(blk_get_stats(ctx->blk), &ctx->acct);
  1237. if (ctx->qflag) {
  1238. goto out;
  1239. }
  1240. if (ctx->vflag) {
  1241. dump_buffer(ctx->buf, ctx->offset, ctx->qiov.size);
  1242. }
  1243. /* Finally, report back -- -C gives a parsable format */
  1244. t2 = tsub(t2, ctx->t1);
  1245. print_report("read", &t2, ctx->offset, ctx->qiov.size,
  1246. ctx->qiov.size, 1, ctx->Cflag);
  1247. out:
  1248. qemu_io_free(ctx->blk, ctx->buf, ctx->qiov.size,
  1249. ctx->flags & BDRV_REQ_REGISTERED_BUF);
  1250. qemu_iovec_destroy(&ctx->qiov);
  1251. g_free(ctx);
  1252. }
  1253. static void aio_read_help(void)
  1254. {
  1255. printf(
  1256. "\n"
  1257. " asynchronously reads a range of bytes from the given offset\n"
  1258. "\n"
  1259. " Example:\n"
  1260. " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
  1261. "\n"
  1262. " Reads a segment of the currently open file, optionally dumping it to the\n"
  1263. " standard output stream (with -v option) for subsequent inspection.\n"
  1264. " The read is performed asynchronously and the aio_flush command must be\n"
  1265. " used to ensure all outstanding aio requests have been completed.\n"
  1266. " Note that due to its asynchronous nature, this command will be\n"
  1267. " considered successful once the request is submitted, independently\n"
  1268. " of potential I/O errors or pattern mismatches.\n"
  1269. " -C, -- report statistics in a machine parsable format\n"
  1270. " -i, -- treat request as invalid, for exercising stats\n"
  1271. " -P, -- use a pattern to verify read data\n"
  1272. " -q, -- quiet mode, do not show I/O statistics\n"
  1273. " -r, -- register I/O buffer\n"
  1274. " -v, -- dump buffer to standard output\n"
  1275. "\n");
  1276. }
  1277. static int aio_read_f(BlockBackend *blk, int argc, char **argv);
  1278. static const cmdinfo_t aio_read_cmd = {
  1279. .name = "aio_read",
  1280. .cfunc = aio_read_f,
  1281. .argmin = 2,
  1282. .argmax = -1,
  1283. .args = "[-Ciqrv] [-P pattern] off len [len..]",
  1284. .oneline = "asynchronously reads a number of bytes",
  1285. .help = aio_read_help,
  1286. };
  1287. static int aio_read_f(BlockBackend *blk, int argc, char **argv)
  1288. {
  1289. int nr_iov, c;
  1290. struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
  1291. ctx->blk = blk;
  1292. while ((c = getopt(argc, argv, "CiP:qrv")) != -1) {
  1293. switch (c) {
  1294. case 'C':
  1295. ctx->Cflag = true;
  1296. break;
  1297. case 'P':
  1298. ctx->Pflag = true;
  1299. ctx->pattern = parse_pattern(optarg);
  1300. if (ctx->pattern < 0) {
  1301. g_free(ctx);
  1302. return -EINVAL;
  1303. }
  1304. break;
  1305. case 'i':
  1306. printf("injecting invalid read request\n");
  1307. block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
  1308. g_free(ctx);
  1309. return 0;
  1310. case 'q':
  1311. ctx->qflag = true;
  1312. break;
  1313. case 'r':
  1314. ctx->flags |= BDRV_REQ_REGISTERED_BUF;
  1315. break;
  1316. case 'v':
  1317. ctx->vflag = true;
  1318. break;
  1319. default:
  1320. g_free(ctx);
  1321. qemuio_command_usage(&aio_read_cmd);
  1322. return -EINVAL;
  1323. }
  1324. }
  1325. if (optind > argc - 2) {
  1326. g_free(ctx);
  1327. qemuio_command_usage(&aio_read_cmd);
  1328. return -EINVAL;
  1329. }
  1330. ctx->offset = cvtnum(argv[optind]);
  1331. if (ctx->offset < 0) {
  1332. int ret = ctx->offset;
  1333. print_cvtnum_err(ret, argv[optind]);
  1334. g_free(ctx);
  1335. return ret;
  1336. }
  1337. optind++;
  1338. nr_iov = argc - optind;
  1339. ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov, 0xab,
  1340. ctx->flags & BDRV_REQ_REGISTERED_BUF);
  1341. if (ctx->buf == NULL) {
  1342. block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_READ);
  1343. g_free(ctx);
  1344. return -EINVAL;
  1345. }
  1346. clock_gettime(CLOCK_MONOTONIC, &ctx->t1);
  1347. block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
  1348. BLOCK_ACCT_READ);
  1349. blk_aio_preadv(blk, ctx->offset, &ctx->qiov, ctx->flags, aio_read_done,
  1350. ctx);
  1351. return 0;
  1352. }
  1353. static void aio_write_help(void)
  1354. {
  1355. printf(
  1356. "\n"
  1357. " asynchronously writes a range of bytes from the given offset source\n"
  1358. " from multiple buffers\n"
  1359. "\n"
  1360. " Example:\n"
  1361. " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
  1362. "\n"
  1363. " Writes into a segment of the currently open file, using a buffer\n"
  1364. " filled with a set pattern (0xcdcdcdcd).\n"
  1365. " The write is performed asynchronously and the aio_flush command must be\n"
  1366. " used to ensure all outstanding aio requests have been completed.\n"
  1367. " Note that due to its asynchronous nature, this command will be\n"
  1368. " considered successful once the request is submitted, independently\n"
  1369. " of potential I/O errors or pattern mismatches.\n"
  1370. " -C, -- report statistics in a machine parsable format\n"
  1371. " -f, -- use Force Unit Access semantics\n"
  1372. " -i, -- treat request as invalid, for exercising stats\n"
  1373. " -P, -- use different pattern to fill file\n"
  1374. " -q, -- quiet mode, do not show I/O statistics\n"
  1375. " -r, -- register I/O buffer\n"
  1376. " -u, -- with -z, allow unmapping\n"
  1377. " -z, -- write zeroes using blk_aio_pwrite_zeroes\n"
  1378. "\n");
  1379. }
  1380. static int aio_write_f(BlockBackend *blk, int argc, char **argv);
  1381. static const cmdinfo_t aio_write_cmd = {
  1382. .name = "aio_write",
  1383. .cfunc = aio_write_f,
  1384. .perm = BLK_PERM_WRITE,
  1385. .argmin = 2,
  1386. .argmax = -1,
  1387. .args = "[-Cfiqruz] [-P pattern] off len [len..]",
  1388. .oneline = "asynchronously writes a number of bytes",
  1389. .help = aio_write_help,
  1390. };
  1391. static int aio_write_f(BlockBackend *blk, int argc, char **argv)
  1392. {
  1393. int nr_iov, c;
  1394. int pattern = 0xcd;
  1395. struct aio_ctx *ctx = g_new0(struct aio_ctx, 1);
  1396. ctx->blk = blk;
  1397. while ((c = getopt(argc, argv, "CfiP:qruz")) != -1) {
  1398. switch (c) {
  1399. case 'C':
  1400. ctx->Cflag = true;
  1401. break;
  1402. case 'f':
  1403. ctx->flags |= BDRV_REQ_FUA;
  1404. break;
  1405. case 'q':
  1406. ctx->qflag = true;
  1407. break;
  1408. case 'r':
  1409. ctx->flags |= BDRV_REQ_REGISTERED_BUF;
  1410. break;
  1411. case 'u':
  1412. ctx->flags |= BDRV_REQ_MAY_UNMAP;
  1413. break;
  1414. case 'P':
  1415. pattern = parse_pattern(optarg);
  1416. if (pattern < 0) {
  1417. g_free(ctx);
  1418. return -EINVAL;
  1419. }
  1420. break;
  1421. case 'i':
  1422. printf("injecting invalid write request\n");
  1423. block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
  1424. g_free(ctx);
  1425. return 0;
  1426. case 'z':
  1427. ctx->zflag = true;
  1428. break;
  1429. default:
  1430. g_free(ctx);
  1431. qemuio_command_usage(&aio_write_cmd);
  1432. return -EINVAL;
  1433. }
  1434. }
  1435. if (optind > argc - 2) {
  1436. g_free(ctx);
  1437. qemuio_command_usage(&aio_write_cmd);
  1438. return -EINVAL;
  1439. }
  1440. if (ctx->zflag && optind != argc - 2) {
  1441. printf("-z supports only a single length parameter\n");
  1442. g_free(ctx);
  1443. return -EINVAL;
  1444. }
  1445. if ((ctx->flags & BDRV_REQ_MAY_UNMAP) && !ctx->zflag) {
  1446. printf("-u requires -z to be specified\n");
  1447. g_free(ctx);
  1448. return -EINVAL;
  1449. }
  1450. if (ctx->zflag && ctx->Pflag) {
  1451. printf("-z and -P cannot be specified at the same time\n");
  1452. g_free(ctx);
  1453. return -EINVAL;
  1454. }
  1455. if (ctx->zflag && (ctx->flags & BDRV_REQ_REGISTERED_BUF)) {
  1456. printf("cannot combine zero write with registered I/O buffer\n");
  1457. g_free(ctx);
  1458. return -EINVAL;
  1459. }
  1460. ctx->offset = cvtnum(argv[optind]);
  1461. if (ctx->offset < 0) {
  1462. int ret = ctx->offset;
  1463. print_cvtnum_err(ret, argv[optind]);
  1464. g_free(ctx);
  1465. return ret;
  1466. }
  1467. optind++;
  1468. if (ctx->zflag) {
  1469. int64_t count = cvtnum(argv[optind]);
  1470. if (count < 0) {
  1471. print_cvtnum_err(count, argv[optind]);
  1472. g_free(ctx);
  1473. return count;
  1474. }
  1475. ctx->qiov.size = count;
  1476. blk_aio_pwrite_zeroes(blk, ctx->offset, count, ctx->flags,
  1477. aio_write_done, ctx);
  1478. } else {
  1479. nr_iov = argc - optind;
  1480. ctx->buf = create_iovec(blk, &ctx->qiov, &argv[optind], nr_iov,
  1481. pattern, ctx->flags & BDRV_REQ_REGISTERED_BUF);
  1482. if (ctx->buf == NULL) {
  1483. block_acct_invalid(blk_get_stats(blk), BLOCK_ACCT_WRITE);
  1484. g_free(ctx);
  1485. return -EINVAL;
  1486. }
  1487. clock_gettime(CLOCK_MONOTONIC, &ctx->t1);
  1488. block_acct_start(blk_get_stats(blk), &ctx->acct, ctx->qiov.size,
  1489. BLOCK_ACCT_WRITE);
  1490. blk_aio_pwritev(blk, ctx->offset, &ctx->qiov, ctx->flags,
  1491. aio_write_done, ctx);
  1492. }
  1493. return 0;
  1494. }
  1495. static int aio_flush_f(BlockBackend *blk, int argc, char **argv)
  1496. {
  1497. BlockAcctCookie cookie;
  1498. block_acct_start(blk_get_stats(blk), &cookie, 0, BLOCK_ACCT_FLUSH);
  1499. blk_drain_all();
  1500. block_acct_done(blk_get_stats(blk), &cookie);
  1501. return 0;
  1502. }
  1503. static const cmdinfo_t aio_flush_cmd = {
  1504. .name = "aio_flush",
  1505. .cfunc = aio_flush_f,
  1506. .oneline = "completes all outstanding aio requests"
  1507. };
  1508. static int flush_f(BlockBackend *blk, int argc, char **argv)
  1509. {
  1510. return blk_flush(blk);
  1511. }
  1512. static const cmdinfo_t flush_cmd = {
  1513. .name = "flush",
  1514. .altname = "f",
  1515. .cfunc = flush_f,
  1516. .oneline = "flush all in-core file state to disk",
  1517. };
  1518. static inline int64_t tosector(int64_t bytes)
  1519. {
  1520. return bytes >> BDRV_SECTOR_BITS;
  1521. }
  1522. static int zone_report_f(BlockBackend *blk, int argc, char **argv)
  1523. {
  1524. int ret;
  1525. int64_t offset;
  1526. int64_t val;
  1527. unsigned int nr_zones;
  1528. ++optind;
  1529. offset = cvtnum(argv[optind]);
  1530. if (offset < 0) {
  1531. print_cvtnum_err(offset, argv[optind]);
  1532. return offset;
  1533. }
  1534. ++optind;
  1535. val = cvtnum(argv[optind]);
  1536. if (val < 0) {
  1537. print_cvtnum_err(val, argv[optind]);
  1538. return val;
  1539. }
  1540. if (val > UINT_MAX) {
  1541. printf("Number of zones must be less than 2^32\n");
  1542. return -ERANGE;
  1543. }
  1544. nr_zones = val;
  1545. g_autofree BlockZoneDescriptor *zones = NULL;
  1546. zones = g_new(BlockZoneDescriptor, nr_zones);
  1547. ret = blk_zone_report(blk, offset, &nr_zones, zones);
  1548. if (ret < 0) {
  1549. printf("zone report failed: %s\n", strerror(-ret));
  1550. } else {
  1551. for (int i = 0; i < nr_zones; ++i) {
  1552. printf("start: 0x%" PRIx64 ", len 0x%" PRIx64 ", "
  1553. "cap"" 0x%" PRIx64 ", wptr 0x%" PRIx64 ", "
  1554. "zcond:%u, [type: %u]\n",
  1555. tosector(zones[i].start), tosector(zones[i].length),
  1556. tosector(zones[i].cap), tosector(zones[i].wp),
  1557. zones[i].state, zones[i].type);
  1558. }
  1559. }
  1560. return ret;
  1561. }
  1562. static const cmdinfo_t zone_report_cmd = {
  1563. .name = "zone_report",
  1564. .altname = "zrp",
  1565. .cfunc = zone_report_f,
  1566. .argmin = 2,
  1567. .argmax = 2,
  1568. .args = "offset number",
  1569. .oneline = "report zone information",
  1570. };
  1571. static int zone_open_f(BlockBackend *blk, int argc, char **argv)
  1572. {
  1573. int ret;
  1574. int64_t offset, len;
  1575. ++optind;
  1576. offset = cvtnum(argv[optind]);
  1577. if (offset < 0) {
  1578. print_cvtnum_err(offset, argv[optind]);
  1579. return offset;
  1580. }
  1581. ++optind;
  1582. len = cvtnum(argv[optind]);
  1583. if (len < 0) {
  1584. print_cvtnum_err(len, argv[optind]);
  1585. return len;
  1586. }
  1587. ret = blk_zone_mgmt(blk, BLK_ZO_OPEN, offset, len);
  1588. if (ret < 0) {
  1589. printf("zone open failed: %s\n", strerror(-ret));
  1590. }
  1591. return ret;
  1592. }
  1593. static const cmdinfo_t zone_open_cmd = {
  1594. .name = "zone_open",
  1595. .altname = "zo",
  1596. .cfunc = zone_open_f,
  1597. .argmin = 2,
  1598. .argmax = 2,
  1599. .args = "offset len",
  1600. .oneline = "explicit open a range of zones in zone block device",
  1601. };
  1602. static int zone_close_f(BlockBackend *blk, int argc, char **argv)
  1603. {
  1604. int ret;
  1605. int64_t offset, len;
  1606. ++optind;
  1607. offset = cvtnum(argv[optind]);
  1608. if (offset < 0) {
  1609. print_cvtnum_err(offset, argv[optind]);
  1610. return offset;
  1611. }
  1612. ++optind;
  1613. len = cvtnum(argv[optind]);
  1614. if (len < 0) {
  1615. print_cvtnum_err(len, argv[optind]);
  1616. return len;
  1617. }
  1618. ret = blk_zone_mgmt(blk, BLK_ZO_CLOSE, offset, len);
  1619. if (ret < 0) {
  1620. printf("zone close failed: %s\n", strerror(-ret));
  1621. }
  1622. return ret;
  1623. }
  1624. static const cmdinfo_t zone_close_cmd = {
  1625. .name = "zone_close",
  1626. .altname = "zc",
  1627. .cfunc = zone_close_f,
  1628. .argmin = 2,
  1629. .argmax = 2,
  1630. .args = "offset len",
  1631. .oneline = "close a range of zones in zone block device",
  1632. };
  1633. static int zone_finish_f(BlockBackend *blk, int argc, char **argv)
  1634. {
  1635. int ret;
  1636. int64_t offset, len;
  1637. ++optind;
  1638. offset = cvtnum(argv[optind]);
  1639. if (offset < 0) {
  1640. print_cvtnum_err(offset, argv[optind]);
  1641. return offset;
  1642. }
  1643. ++optind;
  1644. len = cvtnum(argv[optind]);
  1645. if (len < 0) {
  1646. print_cvtnum_err(len, argv[optind]);
  1647. return len;
  1648. }
  1649. ret = blk_zone_mgmt(blk, BLK_ZO_FINISH, offset, len);
  1650. if (ret < 0) {
  1651. printf("zone finish failed: %s\n", strerror(-ret));
  1652. }
  1653. return ret;
  1654. }
  1655. static const cmdinfo_t zone_finish_cmd = {
  1656. .name = "zone_finish",
  1657. .altname = "zf",
  1658. .cfunc = zone_finish_f,
  1659. .argmin = 2,
  1660. .argmax = 2,
  1661. .args = "offset len",
  1662. .oneline = "finish a range of zones in zone block device",
  1663. };
  1664. static int zone_reset_f(BlockBackend *blk, int argc, char **argv)
  1665. {
  1666. int ret;
  1667. int64_t offset, len;
  1668. ++optind;
  1669. offset = cvtnum(argv[optind]);
  1670. if (offset < 0) {
  1671. print_cvtnum_err(offset, argv[optind]);
  1672. return offset;
  1673. }
  1674. ++optind;
  1675. len = cvtnum(argv[optind]);
  1676. if (len < 0) {
  1677. print_cvtnum_err(len, argv[optind]);
  1678. return len;
  1679. }
  1680. ret = blk_zone_mgmt(blk, BLK_ZO_RESET, offset, len);
  1681. if (ret < 0) {
  1682. printf("zone reset failed: %s\n", strerror(-ret));
  1683. }
  1684. return ret;
  1685. }
  1686. static const cmdinfo_t zone_reset_cmd = {
  1687. .name = "zone_reset",
  1688. .altname = "zrs",
  1689. .cfunc = zone_reset_f,
  1690. .argmin = 2,
  1691. .argmax = 2,
  1692. .args = "offset len",
  1693. .oneline = "reset a zone write pointer in zone block device",
  1694. };
  1695. static int do_aio_zone_append(BlockBackend *blk, QEMUIOVector *qiov,
  1696. int64_t *offset, int flags, int *total)
  1697. {
  1698. int async_ret = NOT_DONE;
  1699. blk_aio_zone_append(blk, offset, qiov, flags, aio_rw_done, &async_ret);
  1700. while (async_ret == NOT_DONE) {
  1701. main_loop_wait(false);
  1702. }
  1703. *total = qiov->size;
  1704. return async_ret < 0 ? async_ret : 1;
  1705. }
  1706. static int zone_append_f(BlockBackend *blk, int argc, char **argv)
  1707. {
  1708. int ret;
  1709. bool pflag = false;
  1710. int flags = 0;
  1711. int total = 0;
  1712. int64_t offset;
  1713. char *buf;
  1714. int c, nr_iov;
  1715. int pattern = 0xcd;
  1716. QEMUIOVector qiov;
  1717. if (optind > argc - 3) {
  1718. return -EINVAL;
  1719. }
  1720. if ((c = getopt(argc, argv, "p")) != -1) {
  1721. pflag = true;
  1722. }
  1723. offset = cvtnum(argv[optind]);
  1724. if (offset < 0) {
  1725. print_cvtnum_err(offset, argv[optind]);
  1726. return offset;
  1727. }
  1728. optind++;
  1729. nr_iov = argc - optind;
  1730. buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, pattern,
  1731. flags & BDRV_REQ_REGISTERED_BUF);
  1732. if (buf == NULL) {
  1733. return -EINVAL;
  1734. }
  1735. ret = do_aio_zone_append(blk, &qiov, &offset, flags, &total);
  1736. if (ret < 0) {
  1737. printf("zone append failed: %s\n", strerror(-ret));
  1738. goto out;
  1739. }
  1740. if (pflag) {
  1741. printf("After zap done, the append sector is 0x%" PRIx64 "\n",
  1742. tosector(offset));
  1743. }
  1744. out:
  1745. qemu_io_free(blk, buf, qiov.size,
  1746. flags & BDRV_REQ_REGISTERED_BUF);
  1747. qemu_iovec_destroy(&qiov);
  1748. return ret;
  1749. }
  1750. static const cmdinfo_t zone_append_cmd = {
  1751. .name = "zone_append",
  1752. .altname = "zap",
  1753. .cfunc = zone_append_f,
  1754. .argmin = 3,
  1755. .argmax = 4,
  1756. .args = "offset len [len..]",
  1757. .oneline = "append write a number of bytes at a specified offset",
  1758. };
  1759. static int truncate_f(BlockBackend *blk, int argc, char **argv);
  1760. static const cmdinfo_t truncate_cmd = {
  1761. .name = "truncate",
  1762. .altname = "t",
  1763. .cfunc = truncate_f,
  1764. .perm = BLK_PERM_WRITE | BLK_PERM_RESIZE,
  1765. .argmin = 1,
  1766. .argmax = 3,
  1767. .args = "[-m prealloc_mode] off",
  1768. .oneline = "truncates the current file at the given offset",
  1769. };
  1770. static int truncate_f(BlockBackend *blk, int argc, char **argv)
  1771. {
  1772. Error *local_err = NULL;
  1773. int64_t offset;
  1774. int c, ret;
  1775. PreallocMode prealloc = PREALLOC_MODE_OFF;
  1776. while ((c = getopt(argc, argv, "m:")) != -1) {
  1777. switch (c) {
  1778. case 'm':
  1779. prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg,
  1780. PREALLOC_MODE__MAX, NULL);
  1781. if (prealloc == PREALLOC_MODE__MAX) {
  1782. error_report("Invalid preallocation mode '%s'", optarg);
  1783. return -EINVAL;
  1784. }
  1785. break;
  1786. default:
  1787. qemuio_command_usage(&truncate_cmd);
  1788. return -EINVAL;
  1789. }
  1790. }
  1791. offset = cvtnum(argv[optind]);
  1792. if (offset < 0) {
  1793. print_cvtnum_err(offset, argv[1]);
  1794. return offset;
  1795. }
  1796. /*
  1797. * qemu-io is a debugging tool, so let us be strict here and pass
  1798. * exact=true. It is better to err on the "emit more errors" side
  1799. * than to be overly permissive.
  1800. */
  1801. ret = blk_truncate(blk, offset, false, prealloc, 0, &local_err);
  1802. if (ret < 0) {
  1803. error_report_err(local_err);
  1804. return ret;
  1805. }
  1806. return 0;
  1807. }
  1808. static int length_f(BlockBackend *blk, int argc, char **argv)
  1809. {
  1810. int64_t size;
  1811. char s1[64];
  1812. size = blk_getlength(blk);
  1813. if (size < 0) {
  1814. printf("getlength: %s\n", strerror(-size));
  1815. return size;
  1816. }
  1817. cvtstr(size, s1, sizeof(s1));
  1818. printf("%s\n", s1);
  1819. return 0;
  1820. }
  1821. static const cmdinfo_t length_cmd = {
  1822. .name = "length",
  1823. .altname = "l",
  1824. .cfunc = length_f,
  1825. .oneline = "gets the length of the current file",
  1826. };
  1827. static int info_f(BlockBackend *blk, int argc, char **argv)
  1828. {
  1829. BlockDriverState *bs = blk_bs(blk);
  1830. BlockDriverInfo bdi;
  1831. ImageInfoSpecific *spec_info;
  1832. Error *local_err = NULL;
  1833. char s1[64], s2[64];
  1834. int ret;
  1835. GLOBAL_STATE_CODE();
  1836. GRAPH_RDLOCK_GUARD_MAINLOOP();
  1837. if (bs->drv && bs->drv->format_name) {
  1838. printf("format name: %s\n", bs->drv->format_name);
  1839. }
  1840. if (bs->drv && bs->drv->protocol_name) {
  1841. printf("format name: %s\n", bs->drv->protocol_name);
  1842. }
  1843. ret = bdrv_get_info(bs, &bdi);
  1844. if (ret) {
  1845. return ret;
  1846. }
  1847. cvtstr(bdi.cluster_size, s1, sizeof(s1));
  1848. cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
  1849. printf("cluster size: %s\n", s1);
  1850. printf("vm state offset: %s\n", s2);
  1851. spec_info = bdrv_get_specific_info(bs, &local_err);
  1852. if (local_err) {
  1853. error_report_err(local_err);
  1854. return -EIO;
  1855. }
  1856. if (spec_info) {
  1857. bdrv_image_info_specific_dump(spec_info,
  1858. "Format specific information:\n",
  1859. 0);
  1860. qapi_free_ImageInfoSpecific(spec_info);
  1861. }
  1862. return 0;
  1863. }
  1864. static const cmdinfo_t info_cmd = {
  1865. .name = "info",
  1866. .altname = "i",
  1867. .cfunc = info_f,
  1868. .oneline = "prints information about the current file",
  1869. };
  1870. static void discard_help(void)
  1871. {
  1872. printf(
  1873. "\n"
  1874. " discards a range of bytes from the given offset\n"
  1875. "\n"
  1876. " Example:\n"
  1877. " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
  1878. "\n"
  1879. " Discards a segment of the currently open file.\n"
  1880. " -C, -- report statistics in a machine parsable format\n"
  1881. " -q, -- quiet mode, do not show I/O statistics\n"
  1882. "\n");
  1883. }
  1884. static int discard_f(BlockBackend *blk, int argc, char **argv);
  1885. static const cmdinfo_t discard_cmd = {
  1886. .name = "discard",
  1887. .altname = "d",
  1888. .cfunc = discard_f,
  1889. .perm = BLK_PERM_WRITE,
  1890. .argmin = 2,
  1891. .argmax = -1,
  1892. .args = "[-Cq] off len",
  1893. .oneline = "discards a number of bytes at a specified offset",
  1894. .help = discard_help,
  1895. };
  1896. static int discard_f(BlockBackend *blk, int argc, char **argv)
  1897. {
  1898. struct timespec t1, t2;
  1899. bool Cflag = false, qflag = false;
  1900. int c, ret;
  1901. int64_t offset, bytes;
  1902. while ((c = getopt(argc, argv, "Cq")) != -1) {
  1903. switch (c) {
  1904. case 'C':
  1905. Cflag = true;
  1906. break;
  1907. case 'q':
  1908. qflag = true;
  1909. break;
  1910. default:
  1911. qemuio_command_usage(&discard_cmd);
  1912. return -EINVAL;
  1913. }
  1914. }
  1915. if (optind != argc - 2) {
  1916. qemuio_command_usage(&discard_cmd);
  1917. return -EINVAL;
  1918. }
  1919. offset = cvtnum(argv[optind]);
  1920. if (offset < 0) {
  1921. print_cvtnum_err(offset, argv[optind]);
  1922. return offset;
  1923. }
  1924. optind++;
  1925. bytes = cvtnum(argv[optind]);
  1926. if (bytes < 0) {
  1927. print_cvtnum_err(bytes, argv[optind]);
  1928. return bytes;
  1929. } else if (bytes > BDRV_REQUEST_MAX_BYTES) {
  1930. printf("length cannot exceed %"PRIu64", given %s\n",
  1931. (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
  1932. return -EINVAL;
  1933. }
  1934. clock_gettime(CLOCK_MONOTONIC, &t1);
  1935. ret = blk_pdiscard(blk, offset, bytes);
  1936. clock_gettime(CLOCK_MONOTONIC, &t2);
  1937. if (ret < 0) {
  1938. printf("discard failed: %s\n", strerror(-ret));
  1939. return ret;
  1940. }
  1941. /* Finally, report back -- -C gives a parsable format */
  1942. if (!qflag) {
  1943. t2 = tsub(t2, t1);
  1944. print_report("discard", &t2, offset, bytes, bytes, 1, Cflag);
  1945. }
  1946. return 0;
  1947. }
  1948. static int alloc_f(BlockBackend *blk, int argc, char **argv)
  1949. {
  1950. BlockDriverState *bs = blk_bs(blk);
  1951. int64_t offset, start, remaining, count;
  1952. char s1[64];
  1953. int ret;
  1954. int64_t num, sum_alloc;
  1955. start = offset = cvtnum(argv[1]);
  1956. if (offset < 0) {
  1957. print_cvtnum_err(offset, argv[1]);
  1958. return offset;
  1959. }
  1960. if (argc == 3) {
  1961. count = cvtnum(argv[2]);
  1962. if (count < 0) {
  1963. print_cvtnum_err(count, argv[2]);
  1964. return count;
  1965. }
  1966. } else {
  1967. count = BDRV_SECTOR_SIZE;
  1968. }
  1969. remaining = count;
  1970. sum_alloc = 0;
  1971. while (remaining) {
  1972. ret = bdrv_is_allocated(bs, offset, remaining, &num);
  1973. if (ret < 0) {
  1974. printf("is_allocated failed: %s\n", strerror(-ret));
  1975. return ret;
  1976. }
  1977. offset += num;
  1978. remaining -= num;
  1979. if (ret) {
  1980. sum_alloc += num;
  1981. }
  1982. if (num == 0) {
  1983. count -= remaining;
  1984. remaining = 0;
  1985. }
  1986. }
  1987. cvtstr(start, s1, sizeof(s1));
  1988. printf("%"PRId64"/%"PRId64" bytes allocated at offset %s\n",
  1989. sum_alloc, count, s1);
  1990. return 0;
  1991. }
  1992. static const cmdinfo_t alloc_cmd = {
  1993. .name = "alloc",
  1994. .altname = "a",
  1995. .argmin = 1,
  1996. .argmax = 2,
  1997. .cfunc = alloc_f,
  1998. .args = "offset [count]",
  1999. .oneline = "checks if offset is allocated in the file",
  2000. };
  2001. static int map_is_allocated(BlockDriverState *bs, int64_t offset,
  2002. int64_t bytes, int64_t *pnum)
  2003. {
  2004. int64_t num;
  2005. int ret, firstret;
  2006. ret = bdrv_is_allocated(bs, offset, bytes, &num);
  2007. if (ret < 0) {
  2008. return ret;
  2009. }
  2010. firstret = ret;
  2011. *pnum = num;
  2012. while (bytes > 0 && ret == firstret) {
  2013. offset += num;
  2014. bytes -= num;
  2015. ret = bdrv_is_allocated(bs, offset, bytes, &num);
  2016. if (ret == firstret && num) {
  2017. *pnum += num;
  2018. } else {
  2019. break;
  2020. }
  2021. }
  2022. return firstret;
  2023. }
  2024. static int map_f(BlockBackend *blk, int argc, char **argv)
  2025. {
  2026. int64_t offset, bytes;
  2027. char s1[64], s2[64];
  2028. int64_t num;
  2029. int ret;
  2030. const char *retstr;
  2031. offset = 0;
  2032. bytes = blk_getlength(blk);
  2033. if (bytes < 0) {
  2034. error_report("Failed to query image length: %s", strerror(-bytes));
  2035. return bytes;
  2036. }
  2037. while (bytes) {
  2038. ret = map_is_allocated(blk_bs(blk), offset, bytes, &num);
  2039. if (ret < 0) {
  2040. error_report("Failed to get allocation status: %s", strerror(-ret));
  2041. return ret;
  2042. } else if (!num) {
  2043. error_report("Unexpected end of image");
  2044. return -EIO;
  2045. }
  2046. retstr = ret ? " allocated" : "not allocated";
  2047. cvtstr(num, s1, sizeof(s1));
  2048. cvtstr(offset, s2, sizeof(s2));
  2049. printf("%s (0x%" PRIx64 ") bytes %s at offset %s (0x%" PRIx64 ")\n",
  2050. s1, num, retstr, s2, offset);
  2051. offset += num;
  2052. bytes -= num;
  2053. }
  2054. return 0;
  2055. }
  2056. static const cmdinfo_t map_cmd = {
  2057. .name = "map",
  2058. .argmin = 0,
  2059. .argmax = 0,
  2060. .cfunc = map_f,
  2061. .args = "",
  2062. .oneline = "prints the allocated areas of a file",
  2063. };
  2064. static void reopen_help(void)
  2065. {
  2066. printf(
  2067. "\n"
  2068. " Changes the open options of an already opened image\n"
  2069. "\n"
  2070. " Example:\n"
  2071. " 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
  2072. "\n"
  2073. " -r, -- Reopen the image read-only\n"
  2074. " -w, -- Reopen the image read-write\n"
  2075. " -c, -- Change the cache mode to the given value\n"
  2076. " -o, -- Changes block driver options (cf. 'open' command)\n"
  2077. "\n");
  2078. }
  2079. static int reopen_f(BlockBackend *blk, int argc, char **argv);
  2080. static QemuOptsList reopen_opts = {
  2081. .name = "reopen",
  2082. .merge_lists = true,
  2083. .head = QTAILQ_HEAD_INITIALIZER(reopen_opts.head),
  2084. .desc = {
  2085. /* no elements => accept any params */
  2086. { /* end of list */ }
  2087. },
  2088. };
  2089. static const cmdinfo_t reopen_cmd = {
  2090. .name = "reopen",
  2091. .argmin = 0,
  2092. .argmax = -1,
  2093. .cfunc = reopen_f,
  2094. .args = "[(-r|-w)] [-c cache] [-o options]",
  2095. .oneline = "reopens an image with new options",
  2096. .help = reopen_help,
  2097. };
  2098. static int reopen_f(BlockBackend *blk, int argc, char **argv)
  2099. {
  2100. BlockDriverState *bs = blk_bs(blk);
  2101. QemuOpts *qopts;
  2102. QDict *opts;
  2103. int c;
  2104. int flags = bs->open_flags;
  2105. bool writethrough = !blk_enable_write_cache(blk);
  2106. bool has_rw_option = false;
  2107. bool has_cache_option = false;
  2108. Error *local_err = NULL;
  2109. while ((c = getopt(argc, argv, "c:o:rw")) != -1) {
  2110. switch (c) {
  2111. case 'c':
  2112. if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
  2113. error_report("Invalid cache option: %s", optarg);
  2114. return -EINVAL;
  2115. }
  2116. has_cache_option = true;
  2117. break;
  2118. case 'o':
  2119. if (!qemu_opts_parse_noisily(&reopen_opts, optarg, 0)) {
  2120. qemu_opts_reset(&reopen_opts);
  2121. return -EINVAL;
  2122. }
  2123. break;
  2124. case 'r':
  2125. if (has_rw_option) {
  2126. error_report("Only one -r/-w option may be given");
  2127. return -EINVAL;
  2128. }
  2129. flags &= ~BDRV_O_RDWR;
  2130. has_rw_option = true;
  2131. break;
  2132. case 'w':
  2133. if (has_rw_option) {
  2134. error_report("Only one -r/-w option may be given");
  2135. return -EINVAL;
  2136. }
  2137. flags |= BDRV_O_RDWR;
  2138. has_rw_option = true;
  2139. break;
  2140. default:
  2141. qemu_opts_reset(&reopen_opts);
  2142. qemuio_command_usage(&reopen_cmd);
  2143. return -EINVAL;
  2144. }
  2145. }
  2146. if (optind != argc) {
  2147. qemu_opts_reset(&reopen_opts);
  2148. qemuio_command_usage(&reopen_cmd);
  2149. return -EINVAL;
  2150. }
  2151. if (!writethrough != blk_enable_write_cache(blk) &&
  2152. blk_get_attached_dev(blk))
  2153. {
  2154. error_report("Cannot change cache.writeback: Device attached");
  2155. qemu_opts_reset(&reopen_opts);
  2156. return -EBUSY;
  2157. }
  2158. if (!(flags & BDRV_O_RDWR)) {
  2159. uint64_t orig_perm, orig_shared_perm;
  2160. bdrv_drain(bs);
  2161. blk_get_perm(blk, &orig_perm, &orig_shared_perm);
  2162. blk_set_perm(blk,
  2163. orig_perm & ~(BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED),
  2164. orig_shared_perm,
  2165. &error_abort);
  2166. }
  2167. qopts = qemu_opts_find(&reopen_opts, NULL);
  2168. opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : qdict_new();
  2169. qemu_opts_reset(&reopen_opts);
  2170. if (qdict_haskey(opts, BDRV_OPT_READ_ONLY)) {
  2171. if (has_rw_option) {
  2172. error_report("Cannot set both -r/-w and '" BDRV_OPT_READ_ONLY "'");
  2173. qobject_unref(opts);
  2174. return -EINVAL;
  2175. }
  2176. } else {
  2177. qdict_put_bool(opts, BDRV_OPT_READ_ONLY, !(flags & BDRV_O_RDWR));
  2178. }
  2179. if (qdict_haskey(opts, BDRV_OPT_CACHE_DIRECT) ||
  2180. qdict_haskey(opts, BDRV_OPT_CACHE_NO_FLUSH)) {
  2181. if (has_cache_option) {
  2182. error_report("Cannot set both -c and the cache options");
  2183. qobject_unref(opts);
  2184. return -EINVAL;
  2185. }
  2186. } else {
  2187. qdict_put_bool(opts, BDRV_OPT_CACHE_DIRECT, flags & BDRV_O_NOCACHE);
  2188. qdict_put_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, flags & BDRV_O_NO_FLUSH);
  2189. }
  2190. bdrv_reopen(bs, opts, true, &local_err);
  2191. if (local_err) {
  2192. error_report_err(local_err);
  2193. return -EINVAL;
  2194. }
  2195. blk_set_enable_write_cache(blk, !writethrough);
  2196. return 0;
  2197. }
  2198. static int break_f(BlockBackend *blk, int argc, char **argv)
  2199. {
  2200. int ret;
  2201. ret = bdrv_debug_breakpoint(blk_bs(blk), argv[1], argv[2]);
  2202. if (ret < 0) {
  2203. printf("Could not set breakpoint: %s\n", strerror(-ret));
  2204. return ret;
  2205. }
  2206. return 0;
  2207. }
  2208. static int remove_break_f(BlockBackend *blk, int argc, char **argv)
  2209. {
  2210. int ret;
  2211. ret = bdrv_debug_remove_breakpoint(blk_bs(blk), argv[1]);
  2212. if (ret < 0) {
  2213. printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret));
  2214. return ret;
  2215. }
  2216. return 0;
  2217. }
  2218. static const cmdinfo_t break_cmd = {
  2219. .name = "break",
  2220. .argmin = 2,
  2221. .argmax = 2,
  2222. .cfunc = break_f,
  2223. .args = "event tag",
  2224. .oneline = "sets a breakpoint on event and tags the stopped "
  2225. "request as tag",
  2226. };
  2227. static const cmdinfo_t remove_break_cmd = {
  2228. .name = "remove_break",
  2229. .argmin = 1,
  2230. .argmax = 1,
  2231. .cfunc = remove_break_f,
  2232. .args = "tag",
  2233. .oneline = "remove a breakpoint by tag",
  2234. };
  2235. static int resume_f(BlockBackend *blk, int argc, char **argv)
  2236. {
  2237. int ret;
  2238. ret = bdrv_debug_resume(blk_bs(blk), argv[1]);
  2239. if (ret < 0) {
  2240. printf("Could not resume request: %s\n", strerror(-ret));
  2241. return ret;
  2242. }
  2243. return 0;
  2244. }
  2245. static const cmdinfo_t resume_cmd = {
  2246. .name = "resume",
  2247. .argmin = 1,
  2248. .argmax = 1,
  2249. .cfunc = resume_f,
  2250. .args = "tag",
  2251. .oneline = "resumes the request tagged as tag",
  2252. };
  2253. static int wait_break_f(BlockBackend *blk, int argc, char **argv)
  2254. {
  2255. while (!bdrv_debug_is_suspended(blk_bs(blk), argv[1])) {
  2256. aio_poll(blk_get_aio_context(blk), true);
  2257. }
  2258. return 0;
  2259. }
  2260. static const cmdinfo_t wait_break_cmd = {
  2261. .name = "wait_break",
  2262. .argmin = 1,
  2263. .argmax = 1,
  2264. .cfunc = wait_break_f,
  2265. .args = "tag",
  2266. .oneline = "waits for the suspension of a request",
  2267. };
  2268. static int abort_f(BlockBackend *blk, int argc, char **argv)
  2269. {
  2270. abort();
  2271. }
  2272. static const cmdinfo_t abort_cmd = {
  2273. .name = "abort",
  2274. .cfunc = abort_f,
  2275. .flags = CMD_NOFILE_OK,
  2276. .oneline = "simulate a program crash using abort(3)",
  2277. };
  2278. static void sigraise_help(void)
  2279. {
  2280. printf(
  2281. "\n"
  2282. " raises the given signal\n"
  2283. "\n"
  2284. " Example:\n"
  2285. " 'sigraise %i' - raises SIGTERM\n"
  2286. "\n"
  2287. " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
  2288. " given to sigraise.\n"
  2289. "\n", SIGTERM);
  2290. }
  2291. static int sigraise_f(BlockBackend *blk, int argc, char **argv);
  2292. static const cmdinfo_t sigraise_cmd = {
  2293. .name = "sigraise",
  2294. .cfunc = sigraise_f,
  2295. .argmin = 1,
  2296. .argmax = 1,
  2297. .flags = CMD_NOFILE_OK,
  2298. .args = "signal",
  2299. .oneline = "raises a signal",
  2300. .help = sigraise_help,
  2301. };
  2302. static int sigraise_f(BlockBackend *blk, int argc, char **argv)
  2303. {
  2304. int64_t sig = cvtnum(argv[1]);
  2305. if (sig < 0) {
  2306. print_cvtnum_err(sig, argv[1]);
  2307. return sig;
  2308. } else if (sig > NSIG) {
  2309. printf("signal argument '%s' is too large to be a valid signal\n",
  2310. argv[1]);
  2311. return -EINVAL;
  2312. }
  2313. /* Using raise() to kill this process does not necessarily flush all open
  2314. * streams. At least stdout and stderr (although the latter should be
  2315. * non-buffered anyway) should be flushed, though. */
  2316. fflush(stdout);
  2317. fflush(stderr);
  2318. raise(sig);
  2319. return 0;
  2320. }
  2321. static void sleep_cb(void *opaque)
  2322. {
  2323. bool *expired = opaque;
  2324. *expired = true;
  2325. }
  2326. static int sleep_f(BlockBackend *blk, int argc, char **argv)
  2327. {
  2328. char *endptr;
  2329. long ms;
  2330. struct QEMUTimer *timer;
  2331. bool expired = false;
  2332. ms = strtol(argv[1], &endptr, 0);
  2333. if (ms < 0 || *endptr != '\0') {
  2334. printf("%s is not a valid number\n", argv[1]);
  2335. return -EINVAL;
  2336. }
  2337. timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired);
  2338. timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms);
  2339. while (!expired) {
  2340. main_loop_wait(false);
  2341. }
  2342. timer_free(timer);
  2343. return 0;
  2344. }
  2345. static const cmdinfo_t sleep_cmd = {
  2346. .name = "sleep",
  2347. .argmin = 1,
  2348. .argmax = 1,
  2349. .cfunc = sleep_f,
  2350. .flags = CMD_NOFILE_OK,
  2351. .oneline = "waits for the given value in milliseconds",
  2352. };
  2353. static void help_oneline(const char *cmd, const cmdinfo_t *ct)
  2354. {
  2355. printf("%s ", cmd);
  2356. if (ct->args) {
  2357. printf("%s ", ct->args);
  2358. }
  2359. printf("-- %s\n", ct->oneline);
  2360. }
  2361. static void help_onecmd(const char *cmd, const cmdinfo_t *ct)
  2362. {
  2363. help_oneline(cmd, ct);
  2364. if (ct->help) {
  2365. ct->help();
  2366. }
  2367. }
  2368. static void help_all(void)
  2369. {
  2370. const cmdinfo_t *ct;
  2371. for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
  2372. help_oneline(ct->name, ct);
  2373. }
  2374. printf("\nUse 'help commandname' for extended help.\n");
  2375. }
  2376. static int help_f(BlockBackend *blk, int argc, char **argv)
  2377. {
  2378. const cmdinfo_t *ct;
  2379. if (argc < 2) {
  2380. help_all();
  2381. return 0;
  2382. }
  2383. ct = find_command(argv[1]);
  2384. if (ct == NULL) {
  2385. printf("command %s not found\n", argv[1]);
  2386. return -EINVAL;
  2387. }
  2388. help_onecmd(argv[1], ct);
  2389. return 0;
  2390. }
  2391. static const cmdinfo_t help_cmd = {
  2392. .name = "help",
  2393. .altname = "?",
  2394. .cfunc = help_f,
  2395. .argmin = 0,
  2396. .argmax = 1,
  2397. .flags = CMD_FLAG_GLOBAL,
  2398. .args = "[command]",
  2399. .oneline = "help for one or all commands",
  2400. };
  2401. /*
  2402. * Called with aio context of blk acquired. Or with qemu_get_aio_context()
  2403. * context acquired if blk is NULL.
  2404. */
  2405. int qemuio_command(BlockBackend *blk, const char *cmd)
  2406. {
  2407. char *input;
  2408. const cmdinfo_t *ct;
  2409. char **v;
  2410. int c;
  2411. int ret = 0;
  2412. input = g_strdup(cmd);
  2413. v = breakline(input, &c);
  2414. if (c) {
  2415. ct = find_command(v[0]);
  2416. if (ct) {
  2417. ret = command(blk, ct, c, v);
  2418. } else {
  2419. fprintf(stderr, "command \"%s\" not found\n", v[0]);
  2420. ret = -EINVAL;
  2421. }
  2422. }
  2423. g_free(input);
  2424. g_free(v);
  2425. return ret;
  2426. }
  2427. static void __attribute((constructor)) init_qemuio_commands(void)
  2428. {
  2429. /* initialize commands */
  2430. qemuio_add_command(&help_cmd);
  2431. qemuio_add_command(&read_cmd);
  2432. qemuio_add_command(&readv_cmd);
  2433. qemuio_add_command(&write_cmd);
  2434. qemuio_add_command(&writev_cmd);
  2435. qemuio_add_command(&aio_read_cmd);
  2436. qemuio_add_command(&aio_write_cmd);
  2437. qemuio_add_command(&aio_flush_cmd);
  2438. qemuio_add_command(&flush_cmd);
  2439. qemuio_add_command(&zone_report_cmd);
  2440. qemuio_add_command(&zone_open_cmd);
  2441. qemuio_add_command(&zone_close_cmd);
  2442. qemuio_add_command(&zone_finish_cmd);
  2443. qemuio_add_command(&zone_reset_cmd);
  2444. qemuio_add_command(&zone_append_cmd);
  2445. qemuio_add_command(&truncate_cmd);
  2446. qemuio_add_command(&length_cmd);
  2447. qemuio_add_command(&info_cmd);
  2448. qemuio_add_command(&discard_cmd);
  2449. qemuio_add_command(&alloc_cmd);
  2450. qemuio_add_command(&map_cmd);
  2451. qemuio_add_command(&reopen_cmd);
  2452. qemuio_add_command(&break_cmd);
  2453. qemuio_add_command(&remove_break_cmd);
  2454. qemuio_add_command(&resume_cmd);
  2455. qemuio_add_command(&wait_break_cmd);
  2456. qemuio_add_command(&abort_cmd);
  2457. qemuio_add_command(&sleep_cmd);
  2458. qemuio_add_command(&sigraise_cmd);
  2459. }