qemu-io-cmds.c 59 KB

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