qemu-io-cmds.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767
  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. #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. unsigned int nr_zones;
  1527. ++optind;
  1528. offset = cvtnum(argv[optind]);
  1529. ++optind;
  1530. nr_zones = cvtnum(argv[optind]);
  1531. g_autofree BlockZoneDescriptor *zones = NULL;
  1532. zones = g_new(BlockZoneDescriptor, nr_zones);
  1533. ret = blk_zone_report(blk, offset, &nr_zones, zones);
  1534. if (ret < 0) {
  1535. printf("zone report failed: %s\n", strerror(-ret));
  1536. } else {
  1537. for (int i = 0; i < nr_zones; ++i) {
  1538. printf("start: 0x%" PRIx64 ", len 0x%" PRIx64 ", "
  1539. "cap"" 0x%" PRIx64 ", wptr 0x%" PRIx64 ", "
  1540. "zcond:%u, [type: %u]\n",
  1541. tosector(zones[i].start), tosector(zones[i].length),
  1542. tosector(zones[i].cap), tosector(zones[i].wp),
  1543. zones[i].state, zones[i].type);
  1544. }
  1545. }
  1546. return ret;
  1547. }
  1548. static const cmdinfo_t zone_report_cmd = {
  1549. .name = "zone_report",
  1550. .altname = "zrp",
  1551. .cfunc = zone_report_f,
  1552. .argmin = 2,
  1553. .argmax = 2,
  1554. .args = "offset number",
  1555. .oneline = "report zone information",
  1556. };
  1557. static int zone_open_f(BlockBackend *blk, int argc, char **argv)
  1558. {
  1559. int ret;
  1560. int64_t offset, len;
  1561. ++optind;
  1562. offset = cvtnum(argv[optind]);
  1563. ++optind;
  1564. len = cvtnum(argv[optind]);
  1565. ret = blk_zone_mgmt(blk, BLK_ZO_OPEN, offset, len);
  1566. if (ret < 0) {
  1567. printf("zone open failed: %s\n", strerror(-ret));
  1568. }
  1569. return ret;
  1570. }
  1571. static const cmdinfo_t zone_open_cmd = {
  1572. .name = "zone_open",
  1573. .altname = "zo",
  1574. .cfunc = zone_open_f,
  1575. .argmin = 2,
  1576. .argmax = 2,
  1577. .args = "offset len",
  1578. .oneline = "explicit open a range of zones in zone block device",
  1579. };
  1580. static int zone_close_f(BlockBackend *blk, int argc, char **argv)
  1581. {
  1582. int ret;
  1583. int64_t offset, len;
  1584. ++optind;
  1585. offset = cvtnum(argv[optind]);
  1586. ++optind;
  1587. len = cvtnum(argv[optind]);
  1588. ret = blk_zone_mgmt(blk, BLK_ZO_CLOSE, offset, len);
  1589. if (ret < 0) {
  1590. printf("zone close failed: %s\n", strerror(-ret));
  1591. }
  1592. return ret;
  1593. }
  1594. static const cmdinfo_t zone_close_cmd = {
  1595. .name = "zone_close",
  1596. .altname = "zc",
  1597. .cfunc = zone_close_f,
  1598. .argmin = 2,
  1599. .argmax = 2,
  1600. .args = "offset len",
  1601. .oneline = "close a range of zones in zone block device",
  1602. };
  1603. static int zone_finish_f(BlockBackend *blk, int argc, char **argv)
  1604. {
  1605. int ret;
  1606. int64_t offset, len;
  1607. ++optind;
  1608. offset = cvtnum(argv[optind]);
  1609. ++optind;
  1610. len = cvtnum(argv[optind]);
  1611. ret = blk_zone_mgmt(blk, BLK_ZO_FINISH, offset, len);
  1612. if (ret < 0) {
  1613. printf("zone finish failed: %s\n", strerror(-ret));
  1614. }
  1615. return ret;
  1616. }
  1617. static const cmdinfo_t zone_finish_cmd = {
  1618. .name = "zone_finish",
  1619. .altname = "zf",
  1620. .cfunc = zone_finish_f,
  1621. .argmin = 2,
  1622. .argmax = 2,
  1623. .args = "offset len",
  1624. .oneline = "finish a range of zones in zone block device",
  1625. };
  1626. static int zone_reset_f(BlockBackend *blk, int argc, char **argv)
  1627. {
  1628. int ret;
  1629. int64_t offset, len;
  1630. ++optind;
  1631. offset = cvtnum(argv[optind]);
  1632. ++optind;
  1633. len = cvtnum(argv[optind]);
  1634. ret = blk_zone_mgmt(blk, BLK_ZO_RESET, offset, len);
  1635. if (ret < 0) {
  1636. printf("zone reset failed: %s\n", strerror(-ret));
  1637. }
  1638. return ret;
  1639. }
  1640. static const cmdinfo_t zone_reset_cmd = {
  1641. .name = "zone_reset",
  1642. .altname = "zrs",
  1643. .cfunc = zone_reset_f,
  1644. .argmin = 2,
  1645. .argmax = 2,
  1646. .args = "offset len",
  1647. .oneline = "reset a zone write pointer in zone block device",
  1648. };
  1649. static int do_aio_zone_append(BlockBackend *blk, QEMUIOVector *qiov,
  1650. int64_t *offset, int flags, int *total)
  1651. {
  1652. int async_ret = NOT_DONE;
  1653. blk_aio_zone_append(blk, offset, qiov, flags, aio_rw_done, &async_ret);
  1654. while (async_ret == NOT_DONE) {
  1655. main_loop_wait(false);
  1656. }
  1657. *total = qiov->size;
  1658. return async_ret < 0 ? async_ret : 1;
  1659. }
  1660. static int zone_append_f(BlockBackend *blk, int argc, char **argv)
  1661. {
  1662. int ret;
  1663. bool pflag = false;
  1664. int flags = 0;
  1665. int total = 0;
  1666. int64_t offset;
  1667. char *buf;
  1668. int c, nr_iov;
  1669. int pattern = 0xcd;
  1670. QEMUIOVector qiov;
  1671. if (optind > argc - 3) {
  1672. return -EINVAL;
  1673. }
  1674. if ((c = getopt(argc, argv, "p")) != -1) {
  1675. pflag = true;
  1676. }
  1677. offset = cvtnum(argv[optind]);
  1678. if (offset < 0) {
  1679. print_cvtnum_err(offset, argv[optind]);
  1680. return offset;
  1681. }
  1682. optind++;
  1683. nr_iov = argc - optind;
  1684. buf = create_iovec(blk, &qiov, &argv[optind], nr_iov, pattern,
  1685. flags & BDRV_REQ_REGISTERED_BUF);
  1686. if (buf == NULL) {
  1687. return -EINVAL;
  1688. }
  1689. ret = do_aio_zone_append(blk, &qiov, &offset, flags, &total);
  1690. if (ret < 0) {
  1691. printf("zone append failed: %s\n", strerror(-ret));
  1692. goto out;
  1693. }
  1694. if (pflag) {
  1695. printf("After zap done, the append sector is 0x%" PRIx64 "\n",
  1696. tosector(offset));
  1697. }
  1698. out:
  1699. qemu_io_free(blk, buf, qiov.size,
  1700. flags & BDRV_REQ_REGISTERED_BUF);
  1701. qemu_iovec_destroy(&qiov);
  1702. return ret;
  1703. }
  1704. static const cmdinfo_t zone_append_cmd = {
  1705. .name = "zone_append",
  1706. .altname = "zap",
  1707. .cfunc = zone_append_f,
  1708. .argmin = 3,
  1709. .argmax = 4,
  1710. .args = "offset len [len..]",
  1711. .oneline = "append write a number of bytes at a specified offset",
  1712. };
  1713. static int truncate_f(BlockBackend *blk, int argc, char **argv);
  1714. static const cmdinfo_t truncate_cmd = {
  1715. .name = "truncate",
  1716. .altname = "t",
  1717. .cfunc = truncate_f,
  1718. .perm = BLK_PERM_WRITE | BLK_PERM_RESIZE,
  1719. .argmin = 1,
  1720. .argmax = 3,
  1721. .args = "[-m prealloc_mode] off",
  1722. .oneline = "truncates the current file at the given offset",
  1723. };
  1724. static int truncate_f(BlockBackend *blk, int argc, char **argv)
  1725. {
  1726. Error *local_err = NULL;
  1727. int64_t offset;
  1728. int c, ret;
  1729. PreallocMode prealloc = PREALLOC_MODE_OFF;
  1730. while ((c = getopt(argc, argv, "m:")) != -1) {
  1731. switch (c) {
  1732. case 'm':
  1733. prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg,
  1734. PREALLOC_MODE__MAX, NULL);
  1735. if (prealloc == PREALLOC_MODE__MAX) {
  1736. error_report("Invalid preallocation mode '%s'", optarg);
  1737. return -EINVAL;
  1738. }
  1739. break;
  1740. default:
  1741. qemuio_command_usage(&truncate_cmd);
  1742. return -EINVAL;
  1743. }
  1744. }
  1745. offset = cvtnum(argv[optind]);
  1746. if (offset < 0) {
  1747. print_cvtnum_err(offset, argv[1]);
  1748. return offset;
  1749. }
  1750. /*
  1751. * qemu-io is a debugging tool, so let us be strict here and pass
  1752. * exact=true. It is better to err on the "emit more errors" side
  1753. * than to be overly permissive.
  1754. */
  1755. ret = blk_truncate(blk, offset, false, prealloc, 0, &local_err);
  1756. if (ret < 0) {
  1757. error_report_err(local_err);
  1758. return ret;
  1759. }
  1760. return 0;
  1761. }
  1762. static int length_f(BlockBackend *blk, int argc, char **argv)
  1763. {
  1764. int64_t size;
  1765. char s1[64];
  1766. size = blk_getlength(blk);
  1767. if (size < 0) {
  1768. printf("getlength: %s\n", strerror(-size));
  1769. return size;
  1770. }
  1771. cvtstr(size, s1, sizeof(s1));
  1772. printf("%s\n", s1);
  1773. return 0;
  1774. }
  1775. static const cmdinfo_t length_cmd = {
  1776. .name = "length",
  1777. .altname = "l",
  1778. .cfunc = length_f,
  1779. .oneline = "gets the length of the current file",
  1780. };
  1781. static int info_f(BlockBackend *blk, int argc, char **argv)
  1782. {
  1783. BlockDriverState *bs = blk_bs(blk);
  1784. BlockDriverInfo bdi;
  1785. ImageInfoSpecific *spec_info;
  1786. Error *local_err = NULL;
  1787. char s1[64], s2[64];
  1788. int ret;
  1789. GLOBAL_STATE_CODE();
  1790. GRAPH_RDLOCK_GUARD_MAINLOOP();
  1791. if (bs->drv && bs->drv->format_name) {
  1792. printf("format name: %s\n", bs->drv->format_name);
  1793. }
  1794. if (bs->drv && bs->drv->protocol_name) {
  1795. printf("format name: %s\n", bs->drv->protocol_name);
  1796. }
  1797. ret = bdrv_get_info(bs, &bdi);
  1798. if (ret) {
  1799. return ret;
  1800. }
  1801. cvtstr(bdi.cluster_size, s1, sizeof(s1));
  1802. cvtstr(bdi.vm_state_offset, s2, sizeof(s2));
  1803. printf("cluster size: %s\n", s1);
  1804. printf("vm state offset: %s\n", s2);
  1805. spec_info = bdrv_get_specific_info(bs, &local_err);
  1806. if (local_err) {
  1807. error_report_err(local_err);
  1808. return -EIO;
  1809. }
  1810. if (spec_info) {
  1811. bdrv_image_info_specific_dump(spec_info,
  1812. "Format specific information:\n",
  1813. 0);
  1814. qapi_free_ImageInfoSpecific(spec_info);
  1815. }
  1816. return 0;
  1817. }
  1818. static const cmdinfo_t info_cmd = {
  1819. .name = "info",
  1820. .altname = "i",
  1821. .cfunc = info_f,
  1822. .oneline = "prints information about the current file",
  1823. };
  1824. static void discard_help(void)
  1825. {
  1826. printf(
  1827. "\n"
  1828. " discards a range of bytes from the given offset\n"
  1829. "\n"
  1830. " Example:\n"
  1831. " 'discard 512 1k' - discards 1 kilobyte from 512 bytes into the file\n"
  1832. "\n"
  1833. " Discards a segment of the currently open file.\n"
  1834. " -C, -- report statistics in a machine parsable format\n"
  1835. " -q, -- quiet mode, do not show I/O statistics\n"
  1836. "\n");
  1837. }
  1838. static int discard_f(BlockBackend *blk, int argc, char **argv);
  1839. static const cmdinfo_t discard_cmd = {
  1840. .name = "discard",
  1841. .altname = "d",
  1842. .cfunc = discard_f,
  1843. .perm = BLK_PERM_WRITE,
  1844. .argmin = 2,
  1845. .argmax = -1,
  1846. .args = "[-Cq] off len",
  1847. .oneline = "discards a number of bytes at a specified offset",
  1848. .help = discard_help,
  1849. };
  1850. static int discard_f(BlockBackend *blk, int argc, char **argv)
  1851. {
  1852. struct timespec t1, t2;
  1853. bool Cflag = false, qflag = false;
  1854. int c, ret;
  1855. int64_t offset, bytes;
  1856. while ((c = getopt(argc, argv, "Cq")) != -1) {
  1857. switch (c) {
  1858. case 'C':
  1859. Cflag = true;
  1860. break;
  1861. case 'q':
  1862. qflag = true;
  1863. break;
  1864. default:
  1865. qemuio_command_usage(&discard_cmd);
  1866. return -EINVAL;
  1867. }
  1868. }
  1869. if (optind != argc - 2) {
  1870. qemuio_command_usage(&discard_cmd);
  1871. return -EINVAL;
  1872. }
  1873. offset = cvtnum(argv[optind]);
  1874. if (offset < 0) {
  1875. print_cvtnum_err(offset, argv[optind]);
  1876. return offset;
  1877. }
  1878. optind++;
  1879. bytes = cvtnum(argv[optind]);
  1880. if (bytes < 0) {
  1881. print_cvtnum_err(bytes, argv[optind]);
  1882. return bytes;
  1883. } else if (bytes > BDRV_REQUEST_MAX_BYTES) {
  1884. printf("length cannot exceed %"PRIu64", given %s\n",
  1885. (uint64_t)BDRV_REQUEST_MAX_BYTES, argv[optind]);
  1886. return -EINVAL;
  1887. }
  1888. clock_gettime(CLOCK_MONOTONIC, &t1);
  1889. ret = blk_pdiscard(blk, offset, bytes);
  1890. clock_gettime(CLOCK_MONOTONIC, &t2);
  1891. if (ret < 0) {
  1892. printf("discard failed: %s\n", strerror(-ret));
  1893. return ret;
  1894. }
  1895. /* Finally, report back -- -C gives a parsable format */
  1896. if (!qflag) {
  1897. t2 = tsub(t2, t1);
  1898. print_report("discard", &t2, offset, bytes, bytes, 1, Cflag);
  1899. }
  1900. return 0;
  1901. }
  1902. static int alloc_f(BlockBackend *blk, int argc, char **argv)
  1903. {
  1904. BlockDriverState *bs = blk_bs(blk);
  1905. int64_t offset, start, remaining, count;
  1906. char s1[64];
  1907. int ret;
  1908. int64_t num, sum_alloc;
  1909. start = offset = cvtnum(argv[1]);
  1910. if (offset < 0) {
  1911. print_cvtnum_err(offset, argv[1]);
  1912. return offset;
  1913. }
  1914. if (argc == 3) {
  1915. count = cvtnum(argv[2]);
  1916. if (count < 0) {
  1917. print_cvtnum_err(count, argv[2]);
  1918. return count;
  1919. }
  1920. } else {
  1921. count = BDRV_SECTOR_SIZE;
  1922. }
  1923. remaining = count;
  1924. sum_alloc = 0;
  1925. while (remaining) {
  1926. ret = bdrv_is_allocated(bs, offset, remaining, &num);
  1927. if (ret < 0) {
  1928. printf("is_allocated failed: %s\n", strerror(-ret));
  1929. return ret;
  1930. }
  1931. offset += num;
  1932. remaining -= num;
  1933. if (ret) {
  1934. sum_alloc += num;
  1935. }
  1936. if (num == 0) {
  1937. count -= remaining;
  1938. remaining = 0;
  1939. }
  1940. }
  1941. cvtstr(start, s1, sizeof(s1));
  1942. printf("%"PRId64"/%"PRId64" bytes allocated at offset %s\n",
  1943. sum_alloc, count, s1);
  1944. return 0;
  1945. }
  1946. static const cmdinfo_t alloc_cmd = {
  1947. .name = "alloc",
  1948. .altname = "a",
  1949. .argmin = 1,
  1950. .argmax = 2,
  1951. .cfunc = alloc_f,
  1952. .args = "offset [count]",
  1953. .oneline = "checks if offset is allocated in the file",
  1954. };
  1955. static int map_is_allocated(BlockDriverState *bs, int64_t offset,
  1956. int64_t bytes, int64_t *pnum)
  1957. {
  1958. int64_t num;
  1959. int ret, firstret;
  1960. ret = bdrv_is_allocated(bs, offset, bytes, &num);
  1961. if (ret < 0) {
  1962. return ret;
  1963. }
  1964. firstret = ret;
  1965. *pnum = num;
  1966. while (bytes > 0 && ret == firstret) {
  1967. offset += num;
  1968. bytes -= num;
  1969. ret = bdrv_is_allocated(bs, offset, bytes, &num);
  1970. if (ret == firstret && num) {
  1971. *pnum += num;
  1972. } else {
  1973. break;
  1974. }
  1975. }
  1976. return firstret;
  1977. }
  1978. static int map_f(BlockBackend *blk, int argc, char **argv)
  1979. {
  1980. int64_t offset, bytes;
  1981. char s1[64], s2[64];
  1982. int64_t num;
  1983. int ret;
  1984. const char *retstr;
  1985. offset = 0;
  1986. bytes = blk_getlength(blk);
  1987. if (bytes < 0) {
  1988. error_report("Failed to query image length: %s", strerror(-bytes));
  1989. return bytes;
  1990. }
  1991. while (bytes) {
  1992. ret = map_is_allocated(blk_bs(blk), offset, bytes, &num);
  1993. if (ret < 0) {
  1994. error_report("Failed to get allocation status: %s", strerror(-ret));
  1995. return ret;
  1996. } else if (!num) {
  1997. error_report("Unexpected end of image");
  1998. return -EIO;
  1999. }
  2000. retstr = ret ? " allocated" : "not allocated";
  2001. cvtstr(num, s1, sizeof(s1));
  2002. cvtstr(offset, s2, sizeof(s2));
  2003. printf("%s (0x%" PRIx64 ") bytes %s at offset %s (0x%" PRIx64 ")\n",
  2004. s1, num, retstr, s2, offset);
  2005. offset += num;
  2006. bytes -= num;
  2007. }
  2008. return 0;
  2009. }
  2010. static const cmdinfo_t map_cmd = {
  2011. .name = "map",
  2012. .argmin = 0,
  2013. .argmax = 0,
  2014. .cfunc = map_f,
  2015. .args = "",
  2016. .oneline = "prints the allocated areas of a file",
  2017. };
  2018. static void reopen_help(void)
  2019. {
  2020. printf(
  2021. "\n"
  2022. " Changes the open options of an already opened image\n"
  2023. "\n"
  2024. " Example:\n"
  2025. " 'reopen -o lazy-refcounts=on' - activates lazy refcount writeback on a qcow2 image\n"
  2026. "\n"
  2027. " -r, -- Reopen the image read-only\n"
  2028. " -w, -- Reopen the image read-write\n"
  2029. " -c, -- Change the cache mode to the given value\n"
  2030. " -o, -- Changes block driver options (cf. 'open' command)\n"
  2031. "\n");
  2032. }
  2033. static int reopen_f(BlockBackend *blk, int argc, char **argv);
  2034. static QemuOptsList reopen_opts = {
  2035. .name = "reopen",
  2036. .merge_lists = true,
  2037. .head = QTAILQ_HEAD_INITIALIZER(reopen_opts.head),
  2038. .desc = {
  2039. /* no elements => accept any params */
  2040. { /* end of list */ }
  2041. },
  2042. };
  2043. static const cmdinfo_t reopen_cmd = {
  2044. .name = "reopen",
  2045. .argmin = 0,
  2046. .argmax = -1,
  2047. .cfunc = reopen_f,
  2048. .args = "[(-r|-w)] [-c cache] [-o options]",
  2049. .oneline = "reopens an image with new options",
  2050. .help = reopen_help,
  2051. };
  2052. static int reopen_f(BlockBackend *blk, int argc, char **argv)
  2053. {
  2054. BlockDriverState *bs = blk_bs(blk);
  2055. QemuOpts *qopts;
  2056. QDict *opts;
  2057. int c;
  2058. int flags = bs->open_flags;
  2059. bool writethrough = !blk_enable_write_cache(blk);
  2060. bool has_rw_option = false;
  2061. bool has_cache_option = false;
  2062. Error *local_err = NULL;
  2063. while ((c = getopt(argc, argv, "c:o:rw")) != -1) {
  2064. switch (c) {
  2065. case 'c':
  2066. if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
  2067. error_report("Invalid cache option: %s", optarg);
  2068. return -EINVAL;
  2069. }
  2070. has_cache_option = true;
  2071. break;
  2072. case 'o':
  2073. if (!qemu_opts_parse_noisily(&reopen_opts, optarg, 0)) {
  2074. qemu_opts_reset(&reopen_opts);
  2075. return -EINVAL;
  2076. }
  2077. break;
  2078. case 'r':
  2079. if (has_rw_option) {
  2080. error_report("Only one -r/-w option may be given");
  2081. return -EINVAL;
  2082. }
  2083. flags &= ~BDRV_O_RDWR;
  2084. has_rw_option = true;
  2085. break;
  2086. case 'w':
  2087. if (has_rw_option) {
  2088. error_report("Only one -r/-w option may be given");
  2089. return -EINVAL;
  2090. }
  2091. flags |= BDRV_O_RDWR;
  2092. has_rw_option = true;
  2093. break;
  2094. default:
  2095. qemu_opts_reset(&reopen_opts);
  2096. qemuio_command_usage(&reopen_cmd);
  2097. return -EINVAL;
  2098. }
  2099. }
  2100. if (optind != argc) {
  2101. qemu_opts_reset(&reopen_opts);
  2102. qemuio_command_usage(&reopen_cmd);
  2103. return -EINVAL;
  2104. }
  2105. if (!writethrough != blk_enable_write_cache(blk) &&
  2106. blk_get_attached_dev(blk))
  2107. {
  2108. error_report("Cannot change cache.writeback: Device attached");
  2109. qemu_opts_reset(&reopen_opts);
  2110. return -EBUSY;
  2111. }
  2112. if (!(flags & BDRV_O_RDWR)) {
  2113. uint64_t orig_perm, orig_shared_perm;
  2114. bdrv_drain(bs);
  2115. blk_get_perm(blk, &orig_perm, &orig_shared_perm);
  2116. blk_set_perm(blk,
  2117. orig_perm & ~(BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED),
  2118. orig_shared_perm,
  2119. &error_abort);
  2120. }
  2121. qopts = qemu_opts_find(&reopen_opts, NULL);
  2122. opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : qdict_new();
  2123. qemu_opts_reset(&reopen_opts);
  2124. if (qdict_haskey(opts, BDRV_OPT_READ_ONLY)) {
  2125. if (has_rw_option) {
  2126. error_report("Cannot set both -r/-w and '" BDRV_OPT_READ_ONLY "'");
  2127. qobject_unref(opts);
  2128. return -EINVAL;
  2129. }
  2130. } else {
  2131. qdict_put_bool(opts, BDRV_OPT_READ_ONLY, !(flags & BDRV_O_RDWR));
  2132. }
  2133. if (qdict_haskey(opts, BDRV_OPT_CACHE_DIRECT) ||
  2134. qdict_haskey(opts, BDRV_OPT_CACHE_NO_FLUSH)) {
  2135. if (has_cache_option) {
  2136. error_report("Cannot set both -c and the cache options");
  2137. qobject_unref(opts);
  2138. return -EINVAL;
  2139. }
  2140. } else {
  2141. qdict_put_bool(opts, BDRV_OPT_CACHE_DIRECT, flags & BDRV_O_NOCACHE);
  2142. qdict_put_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, flags & BDRV_O_NO_FLUSH);
  2143. }
  2144. bdrv_reopen(bs, opts, true, &local_err);
  2145. if (local_err) {
  2146. error_report_err(local_err);
  2147. return -EINVAL;
  2148. }
  2149. blk_set_enable_write_cache(blk, !writethrough);
  2150. return 0;
  2151. }
  2152. static int break_f(BlockBackend *blk, int argc, char **argv)
  2153. {
  2154. int ret;
  2155. ret = bdrv_debug_breakpoint(blk_bs(blk), argv[1], argv[2]);
  2156. if (ret < 0) {
  2157. printf("Could not set breakpoint: %s\n", strerror(-ret));
  2158. return ret;
  2159. }
  2160. return 0;
  2161. }
  2162. static int remove_break_f(BlockBackend *blk, int argc, char **argv)
  2163. {
  2164. int ret;
  2165. ret = bdrv_debug_remove_breakpoint(blk_bs(blk), argv[1]);
  2166. if (ret < 0) {
  2167. printf("Could not remove breakpoint %s: %s\n", argv[1], strerror(-ret));
  2168. return ret;
  2169. }
  2170. return 0;
  2171. }
  2172. static const cmdinfo_t break_cmd = {
  2173. .name = "break",
  2174. .argmin = 2,
  2175. .argmax = 2,
  2176. .cfunc = break_f,
  2177. .args = "event tag",
  2178. .oneline = "sets a breakpoint on event and tags the stopped "
  2179. "request as tag",
  2180. };
  2181. static const cmdinfo_t remove_break_cmd = {
  2182. .name = "remove_break",
  2183. .argmin = 1,
  2184. .argmax = 1,
  2185. .cfunc = remove_break_f,
  2186. .args = "tag",
  2187. .oneline = "remove a breakpoint by tag",
  2188. };
  2189. static int resume_f(BlockBackend *blk, int argc, char **argv)
  2190. {
  2191. int ret;
  2192. ret = bdrv_debug_resume(blk_bs(blk), argv[1]);
  2193. if (ret < 0) {
  2194. printf("Could not resume request: %s\n", strerror(-ret));
  2195. return ret;
  2196. }
  2197. return 0;
  2198. }
  2199. static const cmdinfo_t resume_cmd = {
  2200. .name = "resume",
  2201. .argmin = 1,
  2202. .argmax = 1,
  2203. .cfunc = resume_f,
  2204. .args = "tag",
  2205. .oneline = "resumes the request tagged as tag",
  2206. };
  2207. static int wait_break_f(BlockBackend *blk, int argc, char **argv)
  2208. {
  2209. while (!bdrv_debug_is_suspended(blk_bs(blk), argv[1])) {
  2210. aio_poll(blk_get_aio_context(blk), true);
  2211. }
  2212. return 0;
  2213. }
  2214. static const cmdinfo_t wait_break_cmd = {
  2215. .name = "wait_break",
  2216. .argmin = 1,
  2217. .argmax = 1,
  2218. .cfunc = wait_break_f,
  2219. .args = "tag",
  2220. .oneline = "waits for the suspension of a request",
  2221. };
  2222. static int abort_f(BlockBackend *blk, int argc, char **argv)
  2223. {
  2224. abort();
  2225. }
  2226. static const cmdinfo_t abort_cmd = {
  2227. .name = "abort",
  2228. .cfunc = abort_f,
  2229. .flags = CMD_NOFILE_OK,
  2230. .oneline = "simulate a program crash using abort(3)",
  2231. };
  2232. static void sigraise_help(void)
  2233. {
  2234. printf(
  2235. "\n"
  2236. " raises the given signal\n"
  2237. "\n"
  2238. " Example:\n"
  2239. " 'sigraise %i' - raises SIGTERM\n"
  2240. "\n"
  2241. " Invokes raise(signal), where \"signal\" is the mandatory integer argument\n"
  2242. " given to sigraise.\n"
  2243. "\n", SIGTERM);
  2244. }
  2245. static int sigraise_f(BlockBackend *blk, int argc, char **argv);
  2246. static const cmdinfo_t sigraise_cmd = {
  2247. .name = "sigraise",
  2248. .cfunc = sigraise_f,
  2249. .argmin = 1,
  2250. .argmax = 1,
  2251. .flags = CMD_NOFILE_OK,
  2252. .args = "signal",
  2253. .oneline = "raises a signal",
  2254. .help = sigraise_help,
  2255. };
  2256. static int sigraise_f(BlockBackend *blk, int argc, char **argv)
  2257. {
  2258. int64_t sig = cvtnum(argv[1]);
  2259. if (sig < 0) {
  2260. print_cvtnum_err(sig, argv[1]);
  2261. return sig;
  2262. } else if (sig > NSIG) {
  2263. printf("signal argument '%s' is too large to be a valid signal\n",
  2264. argv[1]);
  2265. return -EINVAL;
  2266. }
  2267. /* Using raise() to kill this process does not necessarily flush all open
  2268. * streams. At least stdout and stderr (although the latter should be
  2269. * non-buffered anyway) should be flushed, though. */
  2270. fflush(stdout);
  2271. fflush(stderr);
  2272. raise(sig);
  2273. return 0;
  2274. }
  2275. static void sleep_cb(void *opaque)
  2276. {
  2277. bool *expired = opaque;
  2278. *expired = true;
  2279. }
  2280. static int sleep_f(BlockBackend *blk, int argc, char **argv)
  2281. {
  2282. char *endptr;
  2283. long ms;
  2284. struct QEMUTimer *timer;
  2285. bool expired = false;
  2286. ms = strtol(argv[1], &endptr, 0);
  2287. if (ms < 0 || *endptr != '\0') {
  2288. printf("%s is not a valid number\n", argv[1]);
  2289. return -EINVAL;
  2290. }
  2291. timer = timer_new_ns(QEMU_CLOCK_HOST, sleep_cb, &expired);
  2292. timer_mod(timer, qemu_clock_get_ns(QEMU_CLOCK_HOST) + SCALE_MS * ms);
  2293. while (!expired) {
  2294. main_loop_wait(false);
  2295. }
  2296. timer_free(timer);
  2297. return 0;
  2298. }
  2299. static const cmdinfo_t sleep_cmd = {
  2300. .name = "sleep",
  2301. .argmin = 1,
  2302. .argmax = 1,
  2303. .cfunc = sleep_f,
  2304. .flags = CMD_NOFILE_OK,
  2305. .oneline = "waits for the given value in milliseconds",
  2306. };
  2307. static void help_oneline(const char *cmd, const cmdinfo_t *ct)
  2308. {
  2309. printf("%s ", cmd);
  2310. if (ct->args) {
  2311. printf("%s ", ct->args);
  2312. }
  2313. printf("-- %s\n", ct->oneline);
  2314. }
  2315. static void help_onecmd(const char *cmd, const cmdinfo_t *ct)
  2316. {
  2317. help_oneline(cmd, ct);
  2318. if (ct->help) {
  2319. ct->help();
  2320. }
  2321. }
  2322. static void help_all(void)
  2323. {
  2324. const cmdinfo_t *ct;
  2325. for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
  2326. help_oneline(ct->name, ct);
  2327. }
  2328. printf("\nUse 'help commandname' for extended help.\n");
  2329. }
  2330. static int help_f(BlockBackend *blk, int argc, char **argv)
  2331. {
  2332. const cmdinfo_t *ct;
  2333. if (argc < 2) {
  2334. help_all();
  2335. return 0;
  2336. }
  2337. ct = find_command(argv[1]);
  2338. if (ct == NULL) {
  2339. printf("command %s not found\n", argv[1]);
  2340. return -EINVAL;
  2341. }
  2342. help_onecmd(argv[1], ct);
  2343. return 0;
  2344. }
  2345. static const cmdinfo_t help_cmd = {
  2346. .name = "help",
  2347. .altname = "?",
  2348. .cfunc = help_f,
  2349. .argmin = 0,
  2350. .argmax = 1,
  2351. .flags = CMD_FLAG_GLOBAL,
  2352. .args = "[command]",
  2353. .oneline = "help for one or all commands",
  2354. };
  2355. /*
  2356. * Called with aio context of blk acquired. Or with qemu_get_aio_context()
  2357. * context acquired if blk is NULL.
  2358. */
  2359. int qemuio_command(BlockBackend *blk, const char *cmd)
  2360. {
  2361. char *input;
  2362. const cmdinfo_t *ct;
  2363. char **v;
  2364. int c;
  2365. int ret = 0;
  2366. input = g_strdup(cmd);
  2367. v = breakline(input, &c);
  2368. if (c) {
  2369. ct = find_command(v[0]);
  2370. if (ct) {
  2371. ret = command(blk, ct, c, v);
  2372. } else {
  2373. fprintf(stderr, "command \"%s\" not found\n", v[0]);
  2374. ret = -EINVAL;
  2375. }
  2376. }
  2377. g_free(input);
  2378. g_free(v);
  2379. return ret;
  2380. }
  2381. static void __attribute((constructor)) init_qemuio_commands(void)
  2382. {
  2383. /* initialize commands */
  2384. qemuio_add_command(&help_cmd);
  2385. qemuio_add_command(&read_cmd);
  2386. qemuio_add_command(&readv_cmd);
  2387. qemuio_add_command(&write_cmd);
  2388. qemuio_add_command(&writev_cmd);
  2389. qemuio_add_command(&aio_read_cmd);
  2390. qemuio_add_command(&aio_write_cmd);
  2391. qemuio_add_command(&aio_flush_cmd);
  2392. qemuio_add_command(&flush_cmd);
  2393. qemuio_add_command(&zone_report_cmd);
  2394. qemuio_add_command(&zone_open_cmd);
  2395. qemuio_add_command(&zone_close_cmd);
  2396. qemuio_add_command(&zone_finish_cmd);
  2397. qemuio_add_command(&zone_reset_cmd);
  2398. qemuio_add_command(&zone_append_cmd);
  2399. qemuio_add_command(&truncate_cmd);
  2400. qemuio_add_command(&length_cmd);
  2401. qemuio_add_command(&info_cmd);
  2402. qemuio_add_command(&discard_cmd);
  2403. qemuio_add_command(&alloc_cmd);
  2404. qemuio_add_command(&map_cmd);
  2405. qemuio_add_command(&reopen_cmd);
  2406. qemuio_add_command(&break_cmd);
  2407. qemuio_add_command(&remove_break_cmd);
  2408. qemuio_add_command(&resume_cmd);
  2409. qemuio_add_command(&wait_break_cmd);
  2410. qemuio_add_command(&abort_cmd);
  2411. qemuio_add_command(&sleep_cmd);
  2412. qemuio_add_command(&sigraise_cmd);
  2413. }