block.c 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039
  1. /*
  2. * QEMU System Emulator block driver
  3. *
  4. * Copyright (c) 2003 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "config-host.h"
  25. #include "qemu-common.h"
  26. #include "trace.h"
  27. #include "monitor.h"
  28. #include "block_int.h"
  29. #include "module.h"
  30. #include "qemu-objects.h"
  31. #ifdef CONFIG_BSD
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #include <sys/ioctl.h>
  35. #include <sys/queue.h>
  36. #ifndef __DragonFly__
  37. #include <sys/disk.h>
  38. #endif
  39. #endif
  40. #ifdef _WIN32
  41. #include <windows.h>
  42. #endif
  43. static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
  44. int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
  45. BlockDriverCompletionFunc *cb, void *opaque);
  46. static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
  47. int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
  48. BlockDriverCompletionFunc *cb, void *opaque);
  49. static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
  50. BlockDriverCompletionFunc *cb, void *opaque);
  51. static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,
  52. BlockDriverCompletionFunc *cb, void *opaque);
  53. static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
  54. uint8_t *buf, int nb_sectors);
  55. static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
  56. const uint8_t *buf, int nb_sectors);
  57. static QTAILQ_HEAD(, BlockDriverState) bdrv_states =
  58. QTAILQ_HEAD_INITIALIZER(bdrv_states);
  59. static QLIST_HEAD(, BlockDriver) bdrv_drivers =
  60. QLIST_HEAD_INITIALIZER(bdrv_drivers);
  61. /* The device to use for VM snapshots */
  62. static BlockDriverState *bs_snapshots;
  63. /* If non-zero, use only whitelisted block drivers */
  64. static int use_bdrv_whitelist;
  65. #ifdef _WIN32
  66. static int is_windows_drive_prefix(const char *filename)
  67. {
  68. return (((filename[0] >= 'a' && filename[0] <= 'z') ||
  69. (filename[0] >= 'A' && filename[0] <= 'Z')) &&
  70. filename[1] == ':');
  71. }
  72. int is_windows_drive(const char *filename)
  73. {
  74. if (is_windows_drive_prefix(filename) &&
  75. filename[2] == '\0')
  76. return 1;
  77. if (strstart(filename, "\\\\.\\", NULL) ||
  78. strstart(filename, "//./", NULL))
  79. return 1;
  80. return 0;
  81. }
  82. #endif
  83. /* check if the path starts with "<protocol>:" */
  84. static int path_has_protocol(const char *path)
  85. {
  86. #ifdef _WIN32
  87. if (is_windows_drive(path) ||
  88. is_windows_drive_prefix(path)) {
  89. return 0;
  90. }
  91. #endif
  92. return strchr(path, ':') != NULL;
  93. }
  94. int path_is_absolute(const char *path)
  95. {
  96. const char *p;
  97. #ifdef _WIN32
  98. /* specific case for names like: "\\.\d:" */
  99. if (*path == '/' || *path == '\\')
  100. return 1;
  101. #endif
  102. p = strchr(path, ':');
  103. if (p)
  104. p++;
  105. else
  106. p = path;
  107. #ifdef _WIN32
  108. return (*p == '/' || *p == '\\');
  109. #else
  110. return (*p == '/');
  111. #endif
  112. }
  113. /* if filename is absolute, just copy it to dest. Otherwise, build a
  114. path to it by considering it is relative to base_path. URL are
  115. supported. */
  116. void path_combine(char *dest, int dest_size,
  117. const char *base_path,
  118. const char *filename)
  119. {
  120. const char *p, *p1;
  121. int len;
  122. if (dest_size <= 0)
  123. return;
  124. if (path_is_absolute(filename)) {
  125. pstrcpy(dest, dest_size, filename);
  126. } else {
  127. p = strchr(base_path, ':');
  128. if (p)
  129. p++;
  130. else
  131. p = base_path;
  132. p1 = strrchr(base_path, '/');
  133. #ifdef _WIN32
  134. {
  135. const char *p2;
  136. p2 = strrchr(base_path, '\\');
  137. if (!p1 || p2 > p1)
  138. p1 = p2;
  139. }
  140. #endif
  141. if (p1)
  142. p1++;
  143. else
  144. p1 = base_path;
  145. if (p1 > p)
  146. p = p1;
  147. len = p - base_path;
  148. if (len > dest_size - 1)
  149. len = dest_size - 1;
  150. memcpy(dest, base_path, len);
  151. dest[len] = '\0';
  152. pstrcat(dest, dest_size, filename);
  153. }
  154. }
  155. void bdrv_register(BlockDriver *bdrv)
  156. {
  157. if (!bdrv->bdrv_aio_readv) {
  158. /* add AIO emulation layer */
  159. bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
  160. bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
  161. } else if (!bdrv->bdrv_read) {
  162. /* add synchronous IO emulation layer */
  163. bdrv->bdrv_read = bdrv_read_em;
  164. bdrv->bdrv_write = bdrv_write_em;
  165. }
  166. if (!bdrv->bdrv_aio_flush)
  167. bdrv->bdrv_aio_flush = bdrv_aio_flush_em;
  168. QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
  169. }
  170. /* create a new block device (by default it is empty) */
  171. BlockDriverState *bdrv_new(const char *device_name)
  172. {
  173. BlockDriverState *bs;
  174. bs = qemu_mallocz(sizeof(BlockDriverState));
  175. pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
  176. if (device_name[0] != '\0') {
  177. QTAILQ_INSERT_TAIL(&bdrv_states, bs, list);
  178. }
  179. return bs;
  180. }
  181. BlockDriver *bdrv_find_format(const char *format_name)
  182. {
  183. BlockDriver *drv1;
  184. QLIST_FOREACH(drv1, &bdrv_drivers, list) {
  185. if (!strcmp(drv1->format_name, format_name)) {
  186. return drv1;
  187. }
  188. }
  189. return NULL;
  190. }
  191. static int bdrv_is_whitelisted(BlockDriver *drv)
  192. {
  193. static const char *whitelist[] = {
  194. CONFIG_BDRV_WHITELIST
  195. };
  196. const char **p;
  197. if (!whitelist[0])
  198. return 1; /* no whitelist, anything goes */
  199. for (p = whitelist; *p; p++) {
  200. if (!strcmp(drv->format_name, *p)) {
  201. return 1;
  202. }
  203. }
  204. return 0;
  205. }
  206. BlockDriver *bdrv_find_whitelisted_format(const char *format_name)
  207. {
  208. BlockDriver *drv = bdrv_find_format(format_name);
  209. return drv && bdrv_is_whitelisted(drv) ? drv : NULL;
  210. }
  211. int bdrv_create(BlockDriver *drv, const char* filename,
  212. QEMUOptionParameter *options)
  213. {
  214. if (!drv->bdrv_create)
  215. return -ENOTSUP;
  216. return drv->bdrv_create(filename, options);
  217. }
  218. int bdrv_create_file(const char* filename, QEMUOptionParameter *options)
  219. {
  220. BlockDriver *drv;
  221. drv = bdrv_find_protocol(filename);
  222. if (drv == NULL) {
  223. return -ENOENT;
  224. }
  225. return bdrv_create(drv, filename, options);
  226. }
  227. #ifdef _WIN32
  228. void get_tmp_filename(char *filename, int size)
  229. {
  230. char temp_dir[MAX_PATH];
  231. GetTempPath(MAX_PATH, temp_dir);
  232. GetTempFileName(temp_dir, "qem", 0, filename);
  233. }
  234. #else
  235. void get_tmp_filename(char *filename, int size)
  236. {
  237. int fd;
  238. const char *tmpdir;
  239. /* XXX: race condition possible */
  240. tmpdir = getenv("TMPDIR");
  241. if (!tmpdir)
  242. tmpdir = "/tmp";
  243. snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
  244. fd = mkstemp(filename);
  245. close(fd);
  246. }
  247. #endif
  248. /*
  249. * Detect host devices. By convention, /dev/cdrom[N] is always
  250. * recognized as a host CDROM.
  251. */
  252. static BlockDriver *find_hdev_driver(const char *filename)
  253. {
  254. int score_max = 0, score;
  255. BlockDriver *drv = NULL, *d;
  256. QLIST_FOREACH(d, &bdrv_drivers, list) {
  257. if (d->bdrv_probe_device) {
  258. score = d->bdrv_probe_device(filename);
  259. if (score > score_max) {
  260. score_max = score;
  261. drv = d;
  262. }
  263. }
  264. }
  265. return drv;
  266. }
  267. BlockDriver *bdrv_find_protocol(const char *filename)
  268. {
  269. BlockDriver *drv1;
  270. char protocol[128];
  271. int len;
  272. const char *p;
  273. /* TODO Drivers without bdrv_file_open must be specified explicitly */
  274. /*
  275. * XXX(hch): we really should not let host device detection
  276. * override an explicit protocol specification, but moving this
  277. * later breaks access to device names with colons in them.
  278. * Thanks to the brain-dead persistent naming schemes on udev-
  279. * based Linux systems those actually are quite common.
  280. */
  281. drv1 = find_hdev_driver(filename);
  282. if (drv1) {
  283. return drv1;
  284. }
  285. if (!path_has_protocol(filename)) {
  286. return bdrv_find_format("file");
  287. }
  288. p = strchr(filename, ':');
  289. assert(p != NULL);
  290. len = p - filename;
  291. if (len > sizeof(protocol) - 1)
  292. len = sizeof(protocol) - 1;
  293. memcpy(protocol, filename, len);
  294. protocol[len] = '\0';
  295. QLIST_FOREACH(drv1, &bdrv_drivers, list) {
  296. if (drv1->protocol_name &&
  297. !strcmp(drv1->protocol_name, protocol)) {
  298. return drv1;
  299. }
  300. }
  301. return NULL;
  302. }
  303. static int find_image_format(const char *filename, BlockDriver **pdrv)
  304. {
  305. int ret, score, score_max;
  306. BlockDriver *drv1, *drv;
  307. uint8_t buf[2048];
  308. BlockDriverState *bs;
  309. ret = bdrv_file_open(&bs, filename, 0);
  310. if (ret < 0) {
  311. *pdrv = NULL;
  312. return ret;
  313. }
  314. /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
  315. if (bs->sg || !bdrv_is_inserted(bs)) {
  316. bdrv_delete(bs);
  317. drv = bdrv_find_format("raw");
  318. if (!drv) {
  319. ret = -ENOENT;
  320. }
  321. *pdrv = drv;
  322. return ret;
  323. }
  324. ret = bdrv_pread(bs, 0, buf, sizeof(buf));
  325. bdrv_delete(bs);
  326. if (ret < 0) {
  327. *pdrv = NULL;
  328. return ret;
  329. }
  330. score_max = 0;
  331. drv = NULL;
  332. QLIST_FOREACH(drv1, &bdrv_drivers, list) {
  333. if (drv1->bdrv_probe) {
  334. score = drv1->bdrv_probe(buf, ret, filename);
  335. if (score > score_max) {
  336. score_max = score;
  337. drv = drv1;
  338. }
  339. }
  340. }
  341. if (!drv) {
  342. ret = -ENOENT;
  343. }
  344. *pdrv = drv;
  345. return ret;
  346. }
  347. /**
  348. * Set the current 'total_sectors' value
  349. */
  350. static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
  351. {
  352. BlockDriver *drv = bs->drv;
  353. /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
  354. if (bs->sg)
  355. return 0;
  356. /* query actual device if possible, otherwise just trust the hint */
  357. if (drv->bdrv_getlength) {
  358. int64_t length = drv->bdrv_getlength(bs);
  359. if (length < 0) {
  360. return length;
  361. }
  362. hint = length >> BDRV_SECTOR_BITS;
  363. }
  364. bs->total_sectors = hint;
  365. return 0;
  366. }
  367. /*
  368. * Common part for opening disk images and files
  369. */
  370. static int bdrv_open_common(BlockDriverState *bs, const char *filename,
  371. int flags, BlockDriver *drv)
  372. {
  373. int ret, open_flags;
  374. assert(drv != NULL);
  375. bs->file = NULL;
  376. bs->total_sectors = 0;
  377. bs->encrypted = 0;
  378. bs->valid_key = 0;
  379. bs->open_flags = flags;
  380. /* buffer_alignment defaulted to 512, drivers can change this value */
  381. bs->buffer_alignment = 512;
  382. pstrcpy(bs->filename, sizeof(bs->filename), filename);
  383. if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {
  384. return -ENOTSUP;
  385. }
  386. bs->drv = drv;
  387. bs->opaque = qemu_mallocz(drv->instance_size);
  388. if (flags & BDRV_O_CACHE_WB)
  389. bs->enable_write_cache = 1;
  390. /*
  391. * Clear flags that are internal to the block layer before opening the
  392. * image.
  393. */
  394. open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
  395. /*
  396. * Snapshots should be writable.
  397. */
  398. if (bs->is_temporary) {
  399. open_flags |= BDRV_O_RDWR;
  400. }
  401. /* Open the image, either directly or using a protocol */
  402. if (drv->bdrv_file_open) {
  403. ret = drv->bdrv_file_open(bs, filename, open_flags);
  404. } else {
  405. ret = bdrv_file_open(&bs->file, filename, open_flags);
  406. if (ret >= 0) {
  407. ret = drv->bdrv_open(bs, open_flags);
  408. }
  409. }
  410. if (ret < 0) {
  411. goto free_and_fail;
  412. }
  413. bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR);
  414. ret = refresh_total_sectors(bs, bs->total_sectors);
  415. if (ret < 0) {
  416. goto free_and_fail;
  417. }
  418. #ifndef _WIN32
  419. if (bs->is_temporary) {
  420. unlink(filename);
  421. }
  422. #endif
  423. return 0;
  424. free_and_fail:
  425. if (bs->file) {
  426. bdrv_delete(bs->file);
  427. bs->file = NULL;
  428. }
  429. qemu_free(bs->opaque);
  430. bs->opaque = NULL;
  431. bs->drv = NULL;
  432. return ret;
  433. }
  434. /*
  435. * Opens a file using a protocol (file, host_device, nbd, ...)
  436. */
  437. int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
  438. {
  439. BlockDriverState *bs;
  440. BlockDriver *drv;
  441. int ret;
  442. drv = bdrv_find_protocol(filename);
  443. if (!drv) {
  444. return -ENOENT;
  445. }
  446. bs = bdrv_new("");
  447. ret = bdrv_open_common(bs, filename, flags, drv);
  448. if (ret < 0) {
  449. bdrv_delete(bs);
  450. return ret;
  451. }
  452. bs->growable = 1;
  453. *pbs = bs;
  454. return 0;
  455. }
  456. /*
  457. * Opens a disk image (raw, qcow2, vmdk, ...)
  458. */
  459. int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
  460. BlockDriver *drv)
  461. {
  462. int ret;
  463. if (flags & BDRV_O_SNAPSHOT) {
  464. BlockDriverState *bs1;
  465. int64_t total_size;
  466. int is_protocol = 0;
  467. BlockDriver *bdrv_qcow2;
  468. QEMUOptionParameter *options;
  469. char tmp_filename[PATH_MAX];
  470. char backing_filename[PATH_MAX];
  471. /* if snapshot, we create a temporary backing file and open it
  472. instead of opening 'filename' directly */
  473. /* if there is a backing file, use it */
  474. bs1 = bdrv_new("");
  475. ret = bdrv_open(bs1, filename, 0, drv);
  476. if (ret < 0) {
  477. bdrv_delete(bs1);
  478. return ret;
  479. }
  480. total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK;
  481. if (bs1->drv && bs1->drv->protocol_name)
  482. is_protocol = 1;
  483. bdrv_delete(bs1);
  484. get_tmp_filename(tmp_filename, sizeof(tmp_filename));
  485. /* Real path is meaningless for protocols */
  486. if (is_protocol)
  487. snprintf(backing_filename, sizeof(backing_filename),
  488. "%s", filename);
  489. else if (!realpath(filename, backing_filename))
  490. return -errno;
  491. bdrv_qcow2 = bdrv_find_format("qcow2");
  492. options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
  493. set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size);
  494. set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
  495. if (drv) {
  496. set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
  497. drv->format_name);
  498. }
  499. ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
  500. free_option_parameters(options);
  501. if (ret < 0) {
  502. return ret;
  503. }
  504. filename = tmp_filename;
  505. drv = bdrv_qcow2;
  506. bs->is_temporary = 1;
  507. }
  508. /* Find the right image format driver */
  509. if (!drv) {
  510. ret = find_image_format(filename, &drv);
  511. }
  512. if (!drv) {
  513. goto unlink_and_fail;
  514. }
  515. /* Open the image */
  516. ret = bdrv_open_common(bs, filename, flags, drv);
  517. if (ret < 0) {
  518. goto unlink_and_fail;
  519. }
  520. /* If there is a backing file, use it */
  521. if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') {
  522. char backing_filename[PATH_MAX];
  523. int back_flags;
  524. BlockDriver *back_drv = NULL;
  525. bs->backing_hd = bdrv_new("");
  526. if (path_has_protocol(bs->backing_file)) {
  527. pstrcpy(backing_filename, sizeof(backing_filename),
  528. bs->backing_file);
  529. } else {
  530. path_combine(backing_filename, sizeof(backing_filename),
  531. filename, bs->backing_file);
  532. }
  533. if (bs->backing_format[0] != '\0') {
  534. back_drv = bdrv_find_format(bs->backing_format);
  535. }
  536. /* backing files always opened read-only */
  537. back_flags =
  538. flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
  539. ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv);
  540. if (ret < 0) {
  541. bdrv_close(bs);
  542. return ret;
  543. }
  544. if (bs->is_temporary) {
  545. bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR);
  546. } else {
  547. /* base image inherits from "parent" */
  548. bs->backing_hd->keep_read_only = bs->keep_read_only;
  549. }
  550. }
  551. if (!bdrv_key_required(bs)) {
  552. /* call the change callback */
  553. bs->media_changed = 1;
  554. if (bs->change_cb)
  555. bs->change_cb(bs->change_opaque, CHANGE_MEDIA);
  556. }
  557. return 0;
  558. unlink_and_fail:
  559. if (bs->is_temporary) {
  560. unlink(filename);
  561. }
  562. return ret;
  563. }
  564. void bdrv_close(BlockDriverState *bs)
  565. {
  566. if (bs->drv) {
  567. if (bs == bs_snapshots) {
  568. bs_snapshots = NULL;
  569. }
  570. if (bs->backing_hd) {
  571. bdrv_delete(bs->backing_hd);
  572. bs->backing_hd = NULL;
  573. }
  574. bs->drv->bdrv_close(bs);
  575. qemu_free(bs->opaque);
  576. #ifdef _WIN32
  577. if (bs->is_temporary) {
  578. unlink(bs->filename);
  579. }
  580. #endif
  581. bs->opaque = NULL;
  582. bs->drv = NULL;
  583. if (bs->file != NULL) {
  584. bdrv_close(bs->file);
  585. }
  586. /* call the change callback */
  587. bs->media_changed = 1;
  588. if (bs->change_cb)
  589. bs->change_cb(bs->change_opaque, CHANGE_MEDIA);
  590. }
  591. }
  592. void bdrv_close_all(void)
  593. {
  594. BlockDriverState *bs;
  595. QTAILQ_FOREACH(bs, &bdrv_states, list) {
  596. bdrv_close(bs);
  597. }
  598. }
  599. /* make a BlockDriverState anonymous by removing from bdrv_state list.
  600. Also, NULL terminate the device_name to prevent double remove */
  601. void bdrv_make_anon(BlockDriverState *bs)
  602. {
  603. if (bs->device_name[0] != '\0') {
  604. QTAILQ_REMOVE(&bdrv_states, bs, list);
  605. }
  606. bs->device_name[0] = '\0';
  607. }
  608. void bdrv_delete(BlockDriverState *bs)
  609. {
  610. assert(!bs->peer);
  611. /* remove from list, if necessary */
  612. bdrv_make_anon(bs);
  613. bdrv_close(bs);
  614. if (bs->file != NULL) {
  615. bdrv_delete(bs->file);
  616. }
  617. assert(bs != bs_snapshots);
  618. qemu_free(bs);
  619. }
  620. int bdrv_attach(BlockDriverState *bs, DeviceState *qdev)
  621. {
  622. if (bs->peer) {
  623. return -EBUSY;
  624. }
  625. bs->peer = qdev;
  626. return 0;
  627. }
  628. void bdrv_detach(BlockDriverState *bs, DeviceState *qdev)
  629. {
  630. assert(bs->peer == qdev);
  631. bs->peer = NULL;
  632. }
  633. DeviceState *bdrv_get_attached(BlockDriverState *bs)
  634. {
  635. return bs->peer;
  636. }
  637. /*
  638. * Run consistency checks on an image
  639. *
  640. * Returns 0 if the check could be completed (it doesn't mean that the image is
  641. * free of errors) or -errno when an internal error occurred. The results of the
  642. * check are stored in res.
  643. */
  644. int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res)
  645. {
  646. if (bs->drv->bdrv_check == NULL) {
  647. return -ENOTSUP;
  648. }
  649. memset(res, 0, sizeof(*res));
  650. return bs->drv->bdrv_check(bs, res);
  651. }
  652. #define COMMIT_BUF_SECTORS 2048
  653. /* commit COW file into the raw image */
  654. int bdrv_commit(BlockDriverState *bs)
  655. {
  656. BlockDriver *drv = bs->drv;
  657. BlockDriver *backing_drv;
  658. int64_t sector, total_sectors;
  659. int n, ro, open_flags;
  660. int ret = 0, rw_ret = 0;
  661. uint8_t *buf;
  662. char filename[1024];
  663. BlockDriverState *bs_rw, *bs_ro;
  664. if (!drv)
  665. return -ENOMEDIUM;
  666. if (!bs->backing_hd) {
  667. return -ENOTSUP;
  668. }
  669. if (bs->backing_hd->keep_read_only) {
  670. return -EACCES;
  671. }
  672. backing_drv = bs->backing_hd->drv;
  673. ro = bs->backing_hd->read_only;
  674. strncpy(filename, bs->backing_hd->filename, sizeof(filename));
  675. open_flags = bs->backing_hd->open_flags;
  676. if (ro) {
  677. /* re-open as RW */
  678. bdrv_delete(bs->backing_hd);
  679. bs->backing_hd = NULL;
  680. bs_rw = bdrv_new("");
  681. rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR,
  682. backing_drv);
  683. if (rw_ret < 0) {
  684. bdrv_delete(bs_rw);
  685. /* try to re-open read-only */
  686. bs_ro = bdrv_new("");
  687. ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR,
  688. backing_drv);
  689. if (ret < 0) {
  690. bdrv_delete(bs_ro);
  691. /* drive not functional anymore */
  692. bs->drv = NULL;
  693. return ret;
  694. }
  695. bs->backing_hd = bs_ro;
  696. return rw_ret;
  697. }
  698. bs->backing_hd = bs_rw;
  699. }
  700. total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
  701. buf = qemu_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);
  702. for (sector = 0; sector < total_sectors; sector += n) {
  703. if (drv->bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) {
  704. if (bdrv_read(bs, sector, buf, n) != 0) {
  705. ret = -EIO;
  706. goto ro_cleanup;
  707. }
  708. if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) {
  709. ret = -EIO;
  710. goto ro_cleanup;
  711. }
  712. }
  713. }
  714. if (drv->bdrv_make_empty) {
  715. ret = drv->bdrv_make_empty(bs);
  716. bdrv_flush(bs);
  717. }
  718. /*
  719. * Make sure all data we wrote to the backing device is actually
  720. * stable on disk.
  721. */
  722. if (bs->backing_hd)
  723. bdrv_flush(bs->backing_hd);
  724. ro_cleanup:
  725. qemu_free(buf);
  726. if (ro) {
  727. /* re-open as RO */
  728. bdrv_delete(bs->backing_hd);
  729. bs->backing_hd = NULL;
  730. bs_ro = bdrv_new("");
  731. ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR,
  732. backing_drv);
  733. if (ret < 0) {
  734. bdrv_delete(bs_ro);
  735. /* drive not functional anymore */
  736. bs->drv = NULL;
  737. return ret;
  738. }
  739. bs->backing_hd = bs_ro;
  740. bs->backing_hd->keep_read_only = 0;
  741. }
  742. return ret;
  743. }
  744. void bdrv_commit_all(void)
  745. {
  746. BlockDriverState *bs;
  747. QTAILQ_FOREACH(bs, &bdrv_states, list) {
  748. bdrv_commit(bs);
  749. }
  750. }
  751. /*
  752. * Return values:
  753. * 0 - success
  754. * -EINVAL - backing format specified, but no file
  755. * -ENOSPC - can't update the backing file because no space is left in the
  756. * image file header
  757. * -ENOTSUP - format driver doesn't support changing the backing file
  758. */
  759. int bdrv_change_backing_file(BlockDriverState *bs,
  760. const char *backing_file, const char *backing_fmt)
  761. {
  762. BlockDriver *drv = bs->drv;
  763. if (drv->bdrv_change_backing_file != NULL) {
  764. return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
  765. } else {
  766. return -ENOTSUP;
  767. }
  768. }
  769. static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
  770. size_t size)
  771. {
  772. int64_t len;
  773. if (!bdrv_is_inserted(bs))
  774. return -ENOMEDIUM;
  775. if (bs->growable)
  776. return 0;
  777. len = bdrv_getlength(bs);
  778. if (offset < 0)
  779. return -EIO;
  780. if ((offset > len) || (len - offset < size))
  781. return -EIO;
  782. return 0;
  783. }
  784. static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
  785. int nb_sectors)
  786. {
  787. return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
  788. nb_sectors * BDRV_SECTOR_SIZE);
  789. }
  790. /* return < 0 if error. See bdrv_write() for the return codes */
  791. int bdrv_read(BlockDriverState *bs, int64_t sector_num,
  792. uint8_t *buf, int nb_sectors)
  793. {
  794. BlockDriver *drv = bs->drv;
  795. if (!drv)
  796. return -ENOMEDIUM;
  797. if (bdrv_check_request(bs, sector_num, nb_sectors))
  798. return -EIO;
  799. return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
  800. }
  801. static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num,
  802. int nb_sectors, int dirty)
  803. {
  804. int64_t start, end;
  805. unsigned long val, idx, bit;
  806. start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
  807. end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
  808. for (; start <= end; start++) {
  809. idx = start / (sizeof(unsigned long) * 8);
  810. bit = start % (sizeof(unsigned long) * 8);
  811. val = bs->dirty_bitmap[idx];
  812. if (dirty) {
  813. if (!(val & (1UL << bit))) {
  814. bs->dirty_count++;
  815. val |= 1UL << bit;
  816. }
  817. } else {
  818. if (val & (1UL << bit)) {
  819. bs->dirty_count--;
  820. val &= ~(1UL << bit);
  821. }
  822. }
  823. bs->dirty_bitmap[idx] = val;
  824. }
  825. }
  826. /* Return < 0 if error. Important errors are:
  827. -EIO generic I/O error (may happen for all errors)
  828. -ENOMEDIUM No media inserted.
  829. -EINVAL Invalid sector number or nb_sectors
  830. -EACCES Trying to write a read-only device
  831. */
  832. int bdrv_write(BlockDriverState *bs, int64_t sector_num,
  833. const uint8_t *buf, int nb_sectors)
  834. {
  835. BlockDriver *drv = bs->drv;
  836. if (!bs->drv)
  837. return -ENOMEDIUM;
  838. if (bs->read_only)
  839. return -EACCES;
  840. if (bdrv_check_request(bs, sector_num, nb_sectors))
  841. return -EIO;
  842. if (bs->dirty_bitmap) {
  843. set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
  844. }
  845. if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
  846. bs->wr_highest_sector = sector_num + nb_sectors - 1;
  847. }
  848. return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
  849. }
  850. int bdrv_pread(BlockDriverState *bs, int64_t offset,
  851. void *buf, int count1)
  852. {
  853. uint8_t tmp_buf[BDRV_SECTOR_SIZE];
  854. int len, nb_sectors, count;
  855. int64_t sector_num;
  856. int ret;
  857. count = count1;
  858. /* first read to align to sector start */
  859. len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
  860. if (len > count)
  861. len = count;
  862. sector_num = offset >> BDRV_SECTOR_BITS;
  863. if (len > 0) {
  864. if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
  865. return ret;
  866. memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
  867. count -= len;
  868. if (count == 0)
  869. return count1;
  870. sector_num++;
  871. buf += len;
  872. }
  873. /* read the sectors "in place" */
  874. nb_sectors = count >> BDRV_SECTOR_BITS;
  875. if (nb_sectors > 0) {
  876. if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0)
  877. return ret;
  878. sector_num += nb_sectors;
  879. len = nb_sectors << BDRV_SECTOR_BITS;
  880. buf += len;
  881. count -= len;
  882. }
  883. /* add data from the last sector */
  884. if (count > 0) {
  885. if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
  886. return ret;
  887. memcpy(buf, tmp_buf, count);
  888. }
  889. return count1;
  890. }
  891. int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
  892. const void *buf, int count1)
  893. {
  894. uint8_t tmp_buf[BDRV_SECTOR_SIZE];
  895. int len, nb_sectors, count;
  896. int64_t sector_num;
  897. int ret;
  898. count = count1;
  899. /* first write to align to sector start */
  900. len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
  901. if (len > count)
  902. len = count;
  903. sector_num = offset >> BDRV_SECTOR_BITS;
  904. if (len > 0) {
  905. if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
  906. return ret;
  907. memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len);
  908. if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
  909. return ret;
  910. count -= len;
  911. if (count == 0)
  912. return count1;
  913. sector_num++;
  914. buf += len;
  915. }
  916. /* write the sectors "in place" */
  917. nb_sectors = count >> BDRV_SECTOR_BITS;
  918. if (nb_sectors > 0) {
  919. if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0)
  920. return ret;
  921. sector_num += nb_sectors;
  922. len = nb_sectors << BDRV_SECTOR_BITS;
  923. buf += len;
  924. count -= len;
  925. }
  926. /* add data from the last sector */
  927. if (count > 0) {
  928. if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
  929. return ret;
  930. memcpy(tmp_buf, buf, count);
  931. if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
  932. return ret;
  933. }
  934. return count1;
  935. }
  936. /*
  937. * Writes to the file and ensures that no writes are reordered across this
  938. * request (acts as a barrier)
  939. *
  940. * Returns 0 on success, -errno in error cases.
  941. */
  942. int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
  943. const void *buf, int count)
  944. {
  945. int ret;
  946. ret = bdrv_pwrite(bs, offset, buf, count);
  947. if (ret < 0) {
  948. return ret;
  949. }
  950. /* No flush needed for cache=writethrough, it uses O_DSYNC */
  951. if ((bs->open_flags & BDRV_O_CACHE_MASK) != 0) {
  952. bdrv_flush(bs);
  953. }
  954. return 0;
  955. }
  956. /*
  957. * Writes to the file and ensures that no writes are reordered across this
  958. * request (acts as a barrier)
  959. *
  960. * Returns 0 on success, -errno in error cases.
  961. */
  962. int bdrv_write_sync(BlockDriverState *bs, int64_t sector_num,
  963. const uint8_t *buf, int nb_sectors)
  964. {
  965. return bdrv_pwrite_sync(bs, BDRV_SECTOR_SIZE * sector_num,
  966. buf, BDRV_SECTOR_SIZE * nb_sectors);
  967. }
  968. /**
  969. * Truncate file to 'offset' bytes (needed only for file protocols)
  970. */
  971. int bdrv_truncate(BlockDriverState *bs, int64_t offset)
  972. {
  973. BlockDriver *drv = bs->drv;
  974. int ret;
  975. if (!drv)
  976. return -ENOMEDIUM;
  977. if (!drv->bdrv_truncate)
  978. return -ENOTSUP;
  979. if (bs->read_only)
  980. return -EACCES;
  981. if (bdrv_in_use(bs))
  982. return -EBUSY;
  983. ret = drv->bdrv_truncate(bs, offset);
  984. if (ret == 0) {
  985. ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
  986. if (bs->change_cb) {
  987. bs->change_cb(bs->change_opaque, CHANGE_SIZE);
  988. }
  989. }
  990. return ret;
  991. }
  992. /**
  993. * Length of a allocated file in bytes. Sparse files are counted by actual
  994. * allocated space. Return < 0 if error or unknown.
  995. */
  996. int64_t bdrv_get_allocated_file_size(BlockDriverState *bs)
  997. {
  998. BlockDriver *drv = bs->drv;
  999. if (!drv) {
  1000. return -ENOMEDIUM;
  1001. }
  1002. if (drv->bdrv_get_allocated_file_size) {
  1003. return drv->bdrv_get_allocated_file_size(bs);
  1004. }
  1005. if (bs->file) {
  1006. return bdrv_get_allocated_file_size(bs->file);
  1007. }
  1008. return -ENOTSUP;
  1009. }
  1010. /**
  1011. * Length of a file in bytes. Return < 0 if error or unknown.
  1012. */
  1013. int64_t bdrv_getlength(BlockDriverState *bs)
  1014. {
  1015. BlockDriver *drv = bs->drv;
  1016. if (!drv)
  1017. return -ENOMEDIUM;
  1018. if (bs->growable || bs->removable) {
  1019. if (drv->bdrv_getlength) {
  1020. return drv->bdrv_getlength(bs);
  1021. }
  1022. }
  1023. return bs->total_sectors * BDRV_SECTOR_SIZE;
  1024. }
  1025. /* return 0 as number of sectors if no device present or error */
  1026. void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
  1027. {
  1028. int64_t length;
  1029. length = bdrv_getlength(bs);
  1030. if (length < 0)
  1031. length = 0;
  1032. else
  1033. length = length >> BDRV_SECTOR_BITS;
  1034. *nb_sectors_ptr = length;
  1035. }
  1036. struct partition {
  1037. uint8_t boot_ind; /* 0x80 - active */
  1038. uint8_t head; /* starting head */
  1039. uint8_t sector; /* starting sector */
  1040. uint8_t cyl; /* starting cylinder */
  1041. uint8_t sys_ind; /* What partition type */
  1042. uint8_t end_head; /* end head */
  1043. uint8_t end_sector; /* end sector */
  1044. uint8_t end_cyl; /* end cylinder */
  1045. uint32_t start_sect; /* starting sector counting from 0 */
  1046. uint32_t nr_sects; /* nr of sectors in partition */
  1047. } __attribute__((packed));
  1048. /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
  1049. static int guess_disk_lchs(BlockDriverState *bs,
  1050. int *pcylinders, int *pheads, int *psectors)
  1051. {
  1052. uint8_t buf[BDRV_SECTOR_SIZE];
  1053. int ret, i, heads, sectors, cylinders;
  1054. struct partition *p;
  1055. uint32_t nr_sects;
  1056. uint64_t nb_sectors;
  1057. bdrv_get_geometry(bs, &nb_sectors);
  1058. ret = bdrv_read(bs, 0, buf, 1);
  1059. if (ret < 0)
  1060. return -1;
  1061. /* test msdos magic */
  1062. if (buf[510] != 0x55 || buf[511] != 0xaa)
  1063. return -1;
  1064. for(i = 0; i < 4; i++) {
  1065. p = ((struct partition *)(buf + 0x1be)) + i;
  1066. nr_sects = le32_to_cpu(p->nr_sects);
  1067. if (nr_sects && p->end_head) {
  1068. /* We make the assumption that the partition terminates on
  1069. a cylinder boundary */
  1070. heads = p->end_head + 1;
  1071. sectors = p->end_sector & 63;
  1072. if (sectors == 0)
  1073. continue;
  1074. cylinders = nb_sectors / (heads * sectors);
  1075. if (cylinders < 1 || cylinders > 16383)
  1076. continue;
  1077. *pheads = heads;
  1078. *psectors = sectors;
  1079. *pcylinders = cylinders;
  1080. #if 0
  1081. printf("guessed geometry: LCHS=%d %d %d\n",
  1082. cylinders, heads, sectors);
  1083. #endif
  1084. return 0;
  1085. }
  1086. }
  1087. return -1;
  1088. }
  1089. void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
  1090. {
  1091. int translation, lba_detected = 0;
  1092. int cylinders, heads, secs;
  1093. uint64_t nb_sectors;
  1094. /* if a geometry hint is available, use it */
  1095. bdrv_get_geometry(bs, &nb_sectors);
  1096. bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
  1097. translation = bdrv_get_translation_hint(bs);
  1098. if (cylinders != 0) {
  1099. *pcyls = cylinders;
  1100. *pheads = heads;
  1101. *psecs = secs;
  1102. } else {
  1103. if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
  1104. if (heads > 16) {
  1105. /* if heads > 16, it means that a BIOS LBA
  1106. translation was active, so the default
  1107. hardware geometry is OK */
  1108. lba_detected = 1;
  1109. goto default_geometry;
  1110. } else {
  1111. *pcyls = cylinders;
  1112. *pheads = heads;
  1113. *psecs = secs;
  1114. /* disable any translation to be in sync with
  1115. the logical geometry */
  1116. if (translation == BIOS_ATA_TRANSLATION_AUTO) {
  1117. bdrv_set_translation_hint(bs,
  1118. BIOS_ATA_TRANSLATION_NONE);
  1119. }
  1120. }
  1121. } else {
  1122. default_geometry:
  1123. /* if no geometry, use a standard physical disk geometry */
  1124. cylinders = nb_sectors / (16 * 63);
  1125. if (cylinders > 16383)
  1126. cylinders = 16383;
  1127. else if (cylinders < 2)
  1128. cylinders = 2;
  1129. *pcyls = cylinders;
  1130. *pheads = 16;
  1131. *psecs = 63;
  1132. if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
  1133. if ((*pcyls * *pheads) <= 131072) {
  1134. bdrv_set_translation_hint(bs,
  1135. BIOS_ATA_TRANSLATION_LARGE);
  1136. } else {
  1137. bdrv_set_translation_hint(bs,
  1138. BIOS_ATA_TRANSLATION_LBA);
  1139. }
  1140. }
  1141. }
  1142. bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
  1143. }
  1144. }
  1145. void bdrv_set_geometry_hint(BlockDriverState *bs,
  1146. int cyls, int heads, int secs)
  1147. {
  1148. bs->cyls = cyls;
  1149. bs->heads = heads;
  1150. bs->secs = secs;
  1151. }
  1152. void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
  1153. {
  1154. bs->translation = translation;
  1155. }
  1156. void bdrv_get_geometry_hint(BlockDriverState *bs,
  1157. int *pcyls, int *pheads, int *psecs)
  1158. {
  1159. *pcyls = bs->cyls;
  1160. *pheads = bs->heads;
  1161. *psecs = bs->secs;
  1162. }
  1163. /* Recognize floppy formats */
  1164. typedef struct FDFormat {
  1165. FDriveType drive;
  1166. uint8_t last_sect;
  1167. uint8_t max_track;
  1168. uint8_t max_head;
  1169. } FDFormat;
  1170. static const FDFormat fd_formats[] = {
  1171. /* First entry is default format */
  1172. /* 1.44 MB 3"1/2 floppy disks */
  1173. { FDRIVE_DRV_144, 18, 80, 1, },
  1174. { FDRIVE_DRV_144, 20, 80, 1, },
  1175. { FDRIVE_DRV_144, 21, 80, 1, },
  1176. { FDRIVE_DRV_144, 21, 82, 1, },
  1177. { FDRIVE_DRV_144, 21, 83, 1, },
  1178. { FDRIVE_DRV_144, 22, 80, 1, },
  1179. { FDRIVE_DRV_144, 23, 80, 1, },
  1180. { FDRIVE_DRV_144, 24, 80, 1, },
  1181. /* 2.88 MB 3"1/2 floppy disks */
  1182. { FDRIVE_DRV_288, 36, 80, 1, },
  1183. { FDRIVE_DRV_288, 39, 80, 1, },
  1184. { FDRIVE_DRV_288, 40, 80, 1, },
  1185. { FDRIVE_DRV_288, 44, 80, 1, },
  1186. { FDRIVE_DRV_288, 48, 80, 1, },
  1187. /* 720 kB 3"1/2 floppy disks */
  1188. { FDRIVE_DRV_144, 9, 80, 1, },
  1189. { FDRIVE_DRV_144, 10, 80, 1, },
  1190. { FDRIVE_DRV_144, 10, 82, 1, },
  1191. { FDRIVE_DRV_144, 10, 83, 1, },
  1192. { FDRIVE_DRV_144, 13, 80, 1, },
  1193. { FDRIVE_DRV_144, 14, 80, 1, },
  1194. /* 1.2 MB 5"1/4 floppy disks */
  1195. { FDRIVE_DRV_120, 15, 80, 1, },
  1196. { FDRIVE_DRV_120, 18, 80, 1, },
  1197. { FDRIVE_DRV_120, 18, 82, 1, },
  1198. { FDRIVE_DRV_120, 18, 83, 1, },
  1199. { FDRIVE_DRV_120, 20, 80, 1, },
  1200. /* 720 kB 5"1/4 floppy disks */
  1201. { FDRIVE_DRV_120, 9, 80, 1, },
  1202. { FDRIVE_DRV_120, 11, 80, 1, },
  1203. /* 360 kB 5"1/4 floppy disks */
  1204. { FDRIVE_DRV_120, 9, 40, 1, },
  1205. { FDRIVE_DRV_120, 9, 40, 0, },
  1206. { FDRIVE_DRV_120, 10, 41, 1, },
  1207. { FDRIVE_DRV_120, 10, 42, 1, },
  1208. /* 320 kB 5"1/4 floppy disks */
  1209. { FDRIVE_DRV_120, 8, 40, 1, },
  1210. { FDRIVE_DRV_120, 8, 40, 0, },
  1211. /* 360 kB must match 5"1/4 better than 3"1/2... */
  1212. { FDRIVE_DRV_144, 9, 80, 0, },
  1213. /* end */
  1214. { FDRIVE_DRV_NONE, -1, -1, 0, },
  1215. };
  1216. void bdrv_get_floppy_geometry_hint(BlockDriverState *bs, int *nb_heads,
  1217. int *max_track, int *last_sect,
  1218. FDriveType drive_in, FDriveType *drive)
  1219. {
  1220. const FDFormat *parse;
  1221. uint64_t nb_sectors, size;
  1222. int i, first_match, match;
  1223. bdrv_get_geometry_hint(bs, nb_heads, max_track, last_sect);
  1224. if (*nb_heads != 0 && *max_track != 0 && *last_sect != 0) {
  1225. /* User defined disk */
  1226. } else {
  1227. bdrv_get_geometry(bs, &nb_sectors);
  1228. match = -1;
  1229. first_match = -1;
  1230. for (i = 0; ; i++) {
  1231. parse = &fd_formats[i];
  1232. if (parse->drive == FDRIVE_DRV_NONE) {
  1233. break;
  1234. }
  1235. if (drive_in == parse->drive ||
  1236. drive_in == FDRIVE_DRV_NONE) {
  1237. size = (parse->max_head + 1) * parse->max_track *
  1238. parse->last_sect;
  1239. if (nb_sectors == size) {
  1240. match = i;
  1241. break;
  1242. }
  1243. if (first_match == -1) {
  1244. first_match = i;
  1245. }
  1246. }
  1247. }
  1248. if (match == -1) {
  1249. if (first_match == -1) {
  1250. match = 1;
  1251. } else {
  1252. match = first_match;
  1253. }
  1254. parse = &fd_formats[match];
  1255. }
  1256. *nb_heads = parse->max_head + 1;
  1257. *max_track = parse->max_track;
  1258. *last_sect = parse->last_sect;
  1259. *drive = parse->drive;
  1260. }
  1261. }
  1262. int bdrv_get_translation_hint(BlockDriverState *bs)
  1263. {
  1264. return bs->translation;
  1265. }
  1266. void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error,
  1267. BlockErrorAction on_write_error)
  1268. {
  1269. bs->on_read_error = on_read_error;
  1270. bs->on_write_error = on_write_error;
  1271. }
  1272. BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read)
  1273. {
  1274. return is_read ? bs->on_read_error : bs->on_write_error;
  1275. }
  1276. void bdrv_set_removable(BlockDriverState *bs, int removable)
  1277. {
  1278. bs->removable = removable;
  1279. if (removable && bs == bs_snapshots) {
  1280. bs_snapshots = NULL;
  1281. }
  1282. }
  1283. int bdrv_is_removable(BlockDriverState *bs)
  1284. {
  1285. return bs->removable;
  1286. }
  1287. int bdrv_is_read_only(BlockDriverState *bs)
  1288. {
  1289. return bs->read_only;
  1290. }
  1291. int bdrv_is_sg(BlockDriverState *bs)
  1292. {
  1293. return bs->sg;
  1294. }
  1295. int bdrv_enable_write_cache(BlockDriverState *bs)
  1296. {
  1297. return bs->enable_write_cache;
  1298. }
  1299. /* XXX: no longer used */
  1300. void bdrv_set_change_cb(BlockDriverState *bs,
  1301. void (*change_cb)(void *opaque, int reason),
  1302. void *opaque)
  1303. {
  1304. bs->change_cb = change_cb;
  1305. bs->change_opaque = opaque;
  1306. }
  1307. int bdrv_is_encrypted(BlockDriverState *bs)
  1308. {
  1309. if (bs->backing_hd && bs->backing_hd->encrypted)
  1310. return 1;
  1311. return bs->encrypted;
  1312. }
  1313. int bdrv_key_required(BlockDriverState *bs)
  1314. {
  1315. BlockDriverState *backing_hd = bs->backing_hd;
  1316. if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
  1317. return 1;
  1318. return (bs->encrypted && !bs->valid_key);
  1319. }
  1320. int bdrv_set_key(BlockDriverState *bs, const char *key)
  1321. {
  1322. int ret;
  1323. if (bs->backing_hd && bs->backing_hd->encrypted) {
  1324. ret = bdrv_set_key(bs->backing_hd, key);
  1325. if (ret < 0)
  1326. return ret;
  1327. if (!bs->encrypted)
  1328. return 0;
  1329. }
  1330. if (!bs->encrypted) {
  1331. return -EINVAL;
  1332. } else if (!bs->drv || !bs->drv->bdrv_set_key) {
  1333. return -ENOMEDIUM;
  1334. }
  1335. ret = bs->drv->bdrv_set_key(bs, key);
  1336. if (ret < 0) {
  1337. bs->valid_key = 0;
  1338. } else if (!bs->valid_key) {
  1339. bs->valid_key = 1;
  1340. /* call the change callback now, we skipped it on open */
  1341. bs->media_changed = 1;
  1342. if (bs->change_cb)
  1343. bs->change_cb(bs->change_opaque, CHANGE_MEDIA);
  1344. }
  1345. return ret;
  1346. }
  1347. void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
  1348. {
  1349. if (!bs->drv) {
  1350. buf[0] = '\0';
  1351. } else {
  1352. pstrcpy(buf, buf_size, bs->drv->format_name);
  1353. }
  1354. }
  1355. void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
  1356. void *opaque)
  1357. {
  1358. BlockDriver *drv;
  1359. QLIST_FOREACH(drv, &bdrv_drivers, list) {
  1360. it(opaque, drv->format_name);
  1361. }
  1362. }
  1363. BlockDriverState *bdrv_find(const char *name)
  1364. {
  1365. BlockDriverState *bs;
  1366. QTAILQ_FOREACH(bs, &bdrv_states, list) {
  1367. if (!strcmp(name, bs->device_name)) {
  1368. return bs;
  1369. }
  1370. }
  1371. return NULL;
  1372. }
  1373. BlockDriverState *bdrv_next(BlockDriverState *bs)
  1374. {
  1375. if (!bs) {
  1376. return QTAILQ_FIRST(&bdrv_states);
  1377. }
  1378. return QTAILQ_NEXT(bs, list);
  1379. }
  1380. void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
  1381. {
  1382. BlockDriverState *bs;
  1383. QTAILQ_FOREACH(bs, &bdrv_states, list) {
  1384. it(opaque, bs);
  1385. }
  1386. }
  1387. const char *bdrv_get_device_name(BlockDriverState *bs)
  1388. {
  1389. return bs->device_name;
  1390. }
  1391. int bdrv_flush(BlockDriverState *bs)
  1392. {
  1393. if (bs->open_flags & BDRV_O_NO_FLUSH) {
  1394. return 0;
  1395. }
  1396. if (bs->drv && bs->drv->bdrv_flush) {
  1397. return bs->drv->bdrv_flush(bs);
  1398. }
  1399. /*
  1400. * Some block drivers always operate in either writethrough or unsafe mode
  1401. * and don't support bdrv_flush therefore. Usually qemu doesn't know how
  1402. * the server works (because the behaviour is hardcoded or depends on
  1403. * server-side configuration), so we can't ensure that everything is safe
  1404. * on disk. Returning an error doesn't work because that would break guests
  1405. * even if the server operates in writethrough mode.
  1406. *
  1407. * Let's hope the user knows what he's doing.
  1408. */
  1409. return 0;
  1410. }
  1411. void bdrv_flush_all(void)
  1412. {
  1413. BlockDriverState *bs;
  1414. QTAILQ_FOREACH(bs, &bdrv_states, list) {
  1415. if (bs->drv && !bdrv_is_read_only(bs) &&
  1416. (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) {
  1417. bdrv_flush(bs);
  1418. }
  1419. }
  1420. }
  1421. int bdrv_has_zero_init(BlockDriverState *bs)
  1422. {
  1423. assert(bs->drv);
  1424. if (bs->drv->bdrv_has_zero_init) {
  1425. return bs->drv->bdrv_has_zero_init(bs);
  1426. }
  1427. return 1;
  1428. }
  1429. int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors)
  1430. {
  1431. if (!bs->drv) {
  1432. return -ENOMEDIUM;
  1433. }
  1434. if (!bs->drv->bdrv_discard) {
  1435. return 0;
  1436. }
  1437. return bs->drv->bdrv_discard(bs, sector_num, nb_sectors);
  1438. }
  1439. /*
  1440. * Returns true iff the specified sector is present in the disk image. Drivers
  1441. * not implementing the functionality are assumed to not support backing files,
  1442. * hence all their sectors are reported as allocated.
  1443. *
  1444. * 'pnum' is set to the number of sectors (including and immediately following
  1445. * the specified sector) that are known to be in the same
  1446. * allocated/unallocated state.
  1447. *
  1448. * 'nb_sectors' is the max value 'pnum' should be set to.
  1449. */
  1450. int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
  1451. int *pnum)
  1452. {
  1453. int64_t n;
  1454. if (!bs->drv->bdrv_is_allocated) {
  1455. if (sector_num >= bs->total_sectors) {
  1456. *pnum = 0;
  1457. return 0;
  1458. }
  1459. n = bs->total_sectors - sector_num;
  1460. *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
  1461. return 1;
  1462. }
  1463. return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
  1464. }
  1465. void bdrv_mon_event(const BlockDriverState *bdrv,
  1466. BlockMonEventAction action, int is_read)
  1467. {
  1468. QObject *data;
  1469. const char *action_str;
  1470. switch (action) {
  1471. case BDRV_ACTION_REPORT:
  1472. action_str = "report";
  1473. break;
  1474. case BDRV_ACTION_IGNORE:
  1475. action_str = "ignore";
  1476. break;
  1477. case BDRV_ACTION_STOP:
  1478. action_str = "stop";
  1479. break;
  1480. default:
  1481. abort();
  1482. }
  1483. data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
  1484. bdrv->device_name,
  1485. action_str,
  1486. is_read ? "read" : "write");
  1487. monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data);
  1488. qobject_decref(data);
  1489. }
  1490. static void bdrv_print_dict(QObject *obj, void *opaque)
  1491. {
  1492. QDict *bs_dict;
  1493. Monitor *mon = opaque;
  1494. bs_dict = qobject_to_qdict(obj);
  1495. monitor_printf(mon, "%s: removable=%d",
  1496. qdict_get_str(bs_dict, "device"),
  1497. qdict_get_bool(bs_dict, "removable"));
  1498. if (qdict_get_bool(bs_dict, "removable")) {
  1499. monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked"));
  1500. }
  1501. if (qdict_haskey(bs_dict, "inserted")) {
  1502. QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted"));
  1503. monitor_printf(mon, " file=");
  1504. monitor_print_filename(mon, qdict_get_str(qdict, "file"));
  1505. if (qdict_haskey(qdict, "backing_file")) {
  1506. monitor_printf(mon, " backing_file=");
  1507. monitor_print_filename(mon, qdict_get_str(qdict, "backing_file"));
  1508. }
  1509. monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
  1510. qdict_get_bool(qdict, "ro"),
  1511. qdict_get_str(qdict, "drv"),
  1512. qdict_get_bool(qdict, "encrypted"));
  1513. } else {
  1514. monitor_printf(mon, " [not inserted]");
  1515. }
  1516. monitor_printf(mon, "\n");
  1517. }
  1518. void bdrv_info_print(Monitor *mon, const QObject *data)
  1519. {
  1520. qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon);
  1521. }
  1522. void bdrv_info(Monitor *mon, QObject **ret_data)
  1523. {
  1524. QList *bs_list;
  1525. BlockDriverState *bs;
  1526. bs_list = qlist_new();
  1527. QTAILQ_FOREACH(bs, &bdrv_states, list) {
  1528. QObject *bs_obj;
  1529. bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': 'unknown', "
  1530. "'removable': %i, 'locked': %i }",
  1531. bs->device_name, bs->removable,
  1532. bs->locked);
  1533. if (bs->drv) {
  1534. QObject *obj;
  1535. QDict *bs_dict = qobject_to_qdict(bs_obj);
  1536. obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, "
  1537. "'encrypted': %i }",
  1538. bs->filename, bs->read_only,
  1539. bs->drv->format_name,
  1540. bdrv_is_encrypted(bs));
  1541. if (bs->backing_file[0] != '\0') {
  1542. QDict *qdict = qobject_to_qdict(obj);
  1543. qdict_put(qdict, "backing_file",
  1544. qstring_from_str(bs->backing_file));
  1545. }
  1546. qdict_put_obj(bs_dict, "inserted", obj);
  1547. }
  1548. qlist_append_obj(bs_list, bs_obj);
  1549. }
  1550. *ret_data = QOBJECT(bs_list);
  1551. }
  1552. static void bdrv_stats_iter(QObject *data, void *opaque)
  1553. {
  1554. QDict *qdict;
  1555. Monitor *mon = opaque;
  1556. qdict = qobject_to_qdict(data);
  1557. monitor_printf(mon, "%s:", qdict_get_str(qdict, "device"));
  1558. qdict = qobject_to_qdict(qdict_get(qdict, "stats"));
  1559. monitor_printf(mon, " rd_bytes=%" PRId64
  1560. " wr_bytes=%" PRId64
  1561. " rd_operations=%" PRId64
  1562. " wr_operations=%" PRId64
  1563. "\n",
  1564. qdict_get_int(qdict, "rd_bytes"),
  1565. qdict_get_int(qdict, "wr_bytes"),
  1566. qdict_get_int(qdict, "rd_operations"),
  1567. qdict_get_int(qdict, "wr_operations"));
  1568. }
  1569. void bdrv_stats_print(Monitor *mon, const QObject *data)
  1570. {
  1571. qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon);
  1572. }
  1573. static QObject* bdrv_info_stats_bs(BlockDriverState *bs)
  1574. {
  1575. QObject *res;
  1576. QDict *dict;
  1577. res = qobject_from_jsonf("{ 'stats': {"
  1578. "'rd_bytes': %" PRId64 ","
  1579. "'wr_bytes': %" PRId64 ","
  1580. "'rd_operations': %" PRId64 ","
  1581. "'wr_operations': %" PRId64 ","
  1582. "'wr_highest_offset': %" PRId64
  1583. "} }",
  1584. bs->rd_bytes, bs->wr_bytes,
  1585. bs->rd_ops, bs->wr_ops,
  1586. bs->wr_highest_sector *
  1587. (uint64_t)BDRV_SECTOR_SIZE);
  1588. dict = qobject_to_qdict(res);
  1589. if (*bs->device_name) {
  1590. qdict_put(dict, "device", qstring_from_str(bs->device_name));
  1591. }
  1592. if (bs->file) {
  1593. QObject *parent = bdrv_info_stats_bs(bs->file);
  1594. qdict_put_obj(dict, "parent", parent);
  1595. }
  1596. return res;
  1597. }
  1598. void bdrv_info_stats(Monitor *mon, QObject **ret_data)
  1599. {
  1600. QObject *obj;
  1601. QList *devices;
  1602. BlockDriverState *bs;
  1603. devices = qlist_new();
  1604. QTAILQ_FOREACH(bs, &bdrv_states, list) {
  1605. obj = bdrv_info_stats_bs(bs);
  1606. qlist_append_obj(devices, obj);
  1607. }
  1608. *ret_data = QOBJECT(devices);
  1609. }
  1610. const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
  1611. {
  1612. if (bs->backing_hd && bs->backing_hd->encrypted)
  1613. return bs->backing_file;
  1614. else if (bs->encrypted)
  1615. return bs->filename;
  1616. else
  1617. return NULL;
  1618. }
  1619. void bdrv_get_backing_filename(BlockDriverState *bs,
  1620. char *filename, int filename_size)
  1621. {
  1622. if (!bs->backing_file) {
  1623. pstrcpy(filename, filename_size, "");
  1624. } else {
  1625. pstrcpy(filename, filename_size, bs->backing_file);
  1626. }
  1627. }
  1628. int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
  1629. const uint8_t *buf, int nb_sectors)
  1630. {
  1631. BlockDriver *drv = bs->drv;
  1632. if (!drv)
  1633. return -ENOMEDIUM;
  1634. if (!drv->bdrv_write_compressed)
  1635. return -ENOTSUP;
  1636. if (bdrv_check_request(bs, sector_num, nb_sectors))
  1637. return -EIO;
  1638. if (bs->dirty_bitmap) {
  1639. set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
  1640. }
  1641. return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
  1642. }
  1643. int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
  1644. {
  1645. BlockDriver *drv = bs->drv;
  1646. if (!drv)
  1647. return -ENOMEDIUM;
  1648. if (!drv->bdrv_get_info)
  1649. return -ENOTSUP;
  1650. memset(bdi, 0, sizeof(*bdi));
  1651. return drv->bdrv_get_info(bs, bdi);
  1652. }
  1653. int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
  1654. int64_t pos, int size)
  1655. {
  1656. BlockDriver *drv = bs->drv;
  1657. if (!drv)
  1658. return -ENOMEDIUM;
  1659. if (drv->bdrv_save_vmstate)
  1660. return drv->bdrv_save_vmstate(bs, buf, pos, size);
  1661. if (bs->file)
  1662. return bdrv_save_vmstate(bs->file, buf, pos, size);
  1663. return -ENOTSUP;
  1664. }
  1665. int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
  1666. int64_t pos, int size)
  1667. {
  1668. BlockDriver *drv = bs->drv;
  1669. if (!drv)
  1670. return -ENOMEDIUM;
  1671. if (drv->bdrv_load_vmstate)
  1672. return drv->bdrv_load_vmstate(bs, buf, pos, size);
  1673. if (bs->file)
  1674. return bdrv_load_vmstate(bs->file, buf, pos, size);
  1675. return -ENOTSUP;
  1676. }
  1677. void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
  1678. {
  1679. BlockDriver *drv = bs->drv;
  1680. if (!drv || !drv->bdrv_debug_event) {
  1681. return;
  1682. }
  1683. return drv->bdrv_debug_event(bs, event);
  1684. }
  1685. /**************************************************************/
  1686. /* handling of snapshots */
  1687. int bdrv_can_snapshot(BlockDriverState *bs)
  1688. {
  1689. BlockDriver *drv = bs->drv;
  1690. if (!drv || bdrv_is_removable(bs) || bdrv_is_read_only(bs)) {
  1691. return 0;
  1692. }
  1693. if (!drv->bdrv_snapshot_create) {
  1694. if (bs->file != NULL) {
  1695. return bdrv_can_snapshot(bs->file);
  1696. }
  1697. return 0;
  1698. }
  1699. return 1;
  1700. }
  1701. int bdrv_is_snapshot(BlockDriverState *bs)
  1702. {
  1703. return !!(bs->open_flags & BDRV_O_SNAPSHOT);
  1704. }
  1705. BlockDriverState *bdrv_snapshots(void)
  1706. {
  1707. BlockDriverState *bs;
  1708. if (bs_snapshots) {
  1709. return bs_snapshots;
  1710. }
  1711. bs = NULL;
  1712. while ((bs = bdrv_next(bs))) {
  1713. if (bdrv_can_snapshot(bs)) {
  1714. bs_snapshots = bs;
  1715. return bs;
  1716. }
  1717. }
  1718. return NULL;
  1719. }
  1720. int bdrv_snapshot_create(BlockDriverState *bs,
  1721. QEMUSnapshotInfo *sn_info)
  1722. {
  1723. BlockDriver *drv = bs->drv;
  1724. if (!drv)
  1725. return -ENOMEDIUM;
  1726. if (drv->bdrv_snapshot_create)
  1727. return drv->bdrv_snapshot_create(bs, sn_info);
  1728. if (bs->file)
  1729. return bdrv_snapshot_create(bs->file, sn_info);
  1730. return -ENOTSUP;
  1731. }
  1732. int bdrv_snapshot_goto(BlockDriverState *bs,
  1733. const char *snapshot_id)
  1734. {
  1735. BlockDriver *drv = bs->drv;
  1736. int ret, open_ret;
  1737. if (!drv)
  1738. return -ENOMEDIUM;
  1739. if (drv->bdrv_snapshot_goto)
  1740. return drv->bdrv_snapshot_goto(bs, snapshot_id);
  1741. if (bs->file) {
  1742. drv->bdrv_close(bs);
  1743. ret = bdrv_snapshot_goto(bs->file, snapshot_id);
  1744. open_ret = drv->bdrv_open(bs, bs->open_flags);
  1745. if (open_ret < 0) {
  1746. bdrv_delete(bs->file);
  1747. bs->drv = NULL;
  1748. return open_ret;
  1749. }
  1750. return ret;
  1751. }
  1752. return -ENOTSUP;
  1753. }
  1754. int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
  1755. {
  1756. BlockDriver *drv = bs->drv;
  1757. if (!drv)
  1758. return -ENOMEDIUM;
  1759. if (drv->bdrv_snapshot_delete)
  1760. return drv->bdrv_snapshot_delete(bs, snapshot_id);
  1761. if (bs->file)
  1762. return bdrv_snapshot_delete(bs->file, snapshot_id);
  1763. return -ENOTSUP;
  1764. }
  1765. int bdrv_snapshot_list(BlockDriverState *bs,
  1766. QEMUSnapshotInfo **psn_info)
  1767. {
  1768. BlockDriver *drv = bs->drv;
  1769. if (!drv)
  1770. return -ENOMEDIUM;
  1771. if (drv->bdrv_snapshot_list)
  1772. return drv->bdrv_snapshot_list(bs, psn_info);
  1773. if (bs->file)
  1774. return bdrv_snapshot_list(bs->file, psn_info);
  1775. return -ENOTSUP;
  1776. }
  1777. int bdrv_snapshot_load_tmp(BlockDriverState *bs,
  1778. const char *snapshot_name)
  1779. {
  1780. BlockDriver *drv = bs->drv;
  1781. if (!drv) {
  1782. return -ENOMEDIUM;
  1783. }
  1784. if (!bs->read_only) {
  1785. return -EINVAL;
  1786. }
  1787. if (drv->bdrv_snapshot_load_tmp) {
  1788. return drv->bdrv_snapshot_load_tmp(bs, snapshot_name);
  1789. }
  1790. return -ENOTSUP;
  1791. }
  1792. #define NB_SUFFIXES 4
  1793. char *get_human_readable_size(char *buf, int buf_size, int64_t size)
  1794. {
  1795. static const char suffixes[NB_SUFFIXES] = "KMGT";
  1796. int64_t base;
  1797. int i;
  1798. if (size <= 999) {
  1799. snprintf(buf, buf_size, "%" PRId64, size);
  1800. } else {
  1801. base = 1024;
  1802. for(i = 0; i < NB_SUFFIXES; i++) {
  1803. if (size < (10 * base)) {
  1804. snprintf(buf, buf_size, "%0.1f%c",
  1805. (double)size / base,
  1806. suffixes[i]);
  1807. break;
  1808. } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
  1809. snprintf(buf, buf_size, "%" PRId64 "%c",
  1810. ((size + (base >> 1)) / base),
  1811. suffixes[i]);
  1812. break;
  1813. }
  1814. base = base * 1024;
  1815. }
  1816. }
  1817. return buf;
  1818. }
  1819. char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
  1820. {
  1821. char buf1[128], date_buf[128], clock_buf[128];
  1822. #ifdef _WIN32
  1823. struct tm *ptm;
  1824. #else
  1825. struct tm tm;
  1826. #endif
  1827. time_t ti;
  1828. int64_t secs;
  1829. if (!sn) {
  1830. snprintf(buf, buf_size,
  1831. "%-10s%-20s%7s%20s%15s",
  1832. "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
  1833. } else {
  1834. ti = sn->date_sec;
  1835. #ifdef _WIN32
  1836. ptm = localtime(&ti);
  1837. strftime(date_buf, sizeof(date_buf),
  1838. "%Y-%m-%d %H:%M:%S", ptm);
  1839. #else
  1840. localtime_r(&ti, &tm);
  1841. strftime(date_buf, sizeof(date_buf),
  1842. "%Y-%m-%d %H:%M:%S", &tm);
  1843. #endif
  1844. secs = sn->vm_clock_nsec / 1000000000;
  1845. snprintf(clock_buf, sizeof(clock_buf),
  1846. "%02d:%02d:%02d.%03d",
  1847. (int)(secs / 3600),
  1848. (int)((secs / 60) % 60),
  1849. (int)(secs % 60),
  1850. (int)((sn->vm_clock_nsec / 1000000) % 1000));
  1851. snprintf(buf, buf_size,
  1852. "%-10s%-20s%7s%20s%15s",
  1853. sn->id_str, sn->name,
  1854. get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
  1855. date_buf,
  1856. clock_buf);
  1857. }
  1858. return buf;
  1859. }
  1860. /**************************************************************/
  1861. /* async I/Os */
  1862. BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
  1863. QEMUIOVector *qiov, int nb_sectors,
  1864. BlockDriverCompletionFunc *cb, void *opaque)
  1865. {
  1866. BlockDriver *drv = bs->drv;
  1867. BlockDriverAIOCB *ret;
  1868. trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque);
  1869. if (!drv)
  1870. return NULL;
  1871. if (bdrv_check_request(bs, sector_num, nb_sectors))
  1872. return NULL;
  1873. ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
  1874. cb, opaque);
  1875. if (ret) {
  1876. /* Update stats even though technically transfer has not happened. */
  1877. bs->rd_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
  1878. bs->rd_ops ++;
  1879. }
  1880. return ret;
  1881. }
  1882. typedef struct BlockCompleteData {
  1883. BlockDriverCompletionFunc *cb;
  1884. void *opaque;
  1885. BlockDriverState *bs;
  1886. int64_t sector_num;
  1887. int nb_sectors;
  1888. } BlockCompleteData;
  1889. static void block_complete_cb(void *opaque, int ret)
  1890. {
  1891. BlockCompleteData *b = opaque;
  1892. if (b->bs->dirty_bitmap) {
  1893. set_dirty_bitmap(b->bs, b->sector_num, b->nb_sectors, 1);
  1894. }
  1895. b->cb(b->opaque, ret);
  1896. qemu_free(b);
  1897. }
  1898. static BlockCompleteData *blk_dirty_cb_alloc(BlockDriverState *bs,
  1899. int64_t sector_num,
  1900. int nb_sectors,
  1901. BlockDriverCompletionFunc *cb,
  1902. void *opaque)
  1903. {
  1904. BlockCompleteData *blkdata = qemu_mallocz(sizeof(BlockCompleteData));
  1905. blkdata->bs = bs;
  1906. blkdata->cb = cb;
  1907. blkdata->opaque = opaque;
  1908. blkdata->sector_num = sector_num;
  1909. blkdata->nb_sectors = nb_sectors;
  1910. return blkdata;
  1911. }
  1912. BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
  1913. QEMUIOVector *qiov, int nb_sectors,
  1914. BlockDriverCompletionFunc *cb, void *opaque)
  1915. {
  1916. BlockDriver *drv = bs->drv;
  1917. BlockDriverAIOCB *ret;
  1918. BlockCompleteData *blk_cb_data;
  1919. trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque);
  1920. if (!drv)
  1921. return NULL;
  1922. if (bs->read_only)
  1923. return NULL;
  1924. if (bdrv_check_request(bs, sector_num, nb_sectors))
  1925. return NULL;
  1926. if (bs->dirty_bitmap) {
  1927. blk_cb_data = blk_dirty_cb_alloc(bs, sector_num, nb_sectors, cb,
  1928. opaque);
  1929. cb = &block_complete_cb;
  1930. opaque = blk_cb_data;
  1931. }
  1932. ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
  1933. cb, opaque);
  1934. if (ret) {
  1935. /* Update stats even though technically transfer has not happened. */
  1936. bs->wr_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
  1937. bs->wr_ops ++;
  1938. if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
  1939. bs->wr_highest_sector = sector_num + nb_sectors - 1;
  1940. }
  1941. }
  1942. return ret;
  1943. }
  1944. typedef struct MultiwriteCB {
  1945. int error;
  1946. int num_requests;
  1947. int num_callbacks;
  1948. struct {
  1949. BlockDriverCompletionFunc *cb;
  1950. void *opaque;
  1951. QEMUIOVector *free_qiov;
  1952. void *free_buf;
  1953. } callbacks[];
  1954. } MultiwriteCB;
  1955. static void multiwrite_user_cb(MultiwriteCB *mcb)
  1956. {
  1957. int i;
  1958. for (i = 0; i < mcb->num_callbacks; i++) {
  1959. mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
  1960. if (mcb->callbacks[i].free_qiov) {
  1961. qemu_iovec_destroy(mcb->callbacks[i].free_qiov);
  1962. }
  1963. qemu_free(mcb->callbacks[i].free_qiov);
  1964. qemu_vfree(mcb->callbacks[i].free_buf);
  1965. }
  1966. }
  1967. static void multiwrite_cb(void *opaque, int ret)
  1968. {
  1969. MultiwriteCB *mcb = opaque;
  1970. trace_multiwrite_cb(mcb, ret);
  1971. if (ret < 0 && !mcb->error) {
  1972. mcb->error = ret;
  1973. }
  1974. mcb->num_requests--;
  1975. if (mcb->num_requests == 0) {
  1976. multiwrite_user_cb(mcb);
  1977. qemu_free(mcb);
  1978. }
  1979. }
  1980. static int multiwrite_req_compare(const void *a, const void *b)
  1981. {
  1982. const BlockRequest *req1 = a, *req2 = b;
  1983. /*
  1984. * Note that we can't simply subtract req2->sector from req1->sector
  1985. * here as that could overflow the return value.
  1986. */
  1987. if (req1->sector > req2->sector) {
  1988. return 1;
  1989. } else if (req1->sector < req2->sector) {
  1990. return -1;
  1991. } else {
  1992. return 0;
  1993. }
  1994. }
  1995. /*
  1996. * Takes a bunch of requests and tries to merge them. Returns the number of
  1997. * requests that remain after merging.
  1998. */
  1999. static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
  2000. int num_reqs, MultiwriteCB *mcb)
  2001. {
  2002. int i, outidx;
  2003. // Sort requests by start sector
  2004. qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
  2005. // Check if adjacent requests touch the same clusters. If so, combine them,
  2006. // filling up gaps with zero sectors.
  2007. outidx = 0;
  2008. for (i = 1; i < num_reqs; i++) {
  2009. int merge = 0;
  2010. int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
  2011. // This handles the cases that are valid for all block drivers, namely
  2012. // exactly sequential writes and overlapping writes.
  2013. if (reqs[i].sector <= oldreq_last) {
  2014. merge = 1;
  2015. }
  2016. // The block driver may decide that it makes sense to combine requests
  2017. // even if there is a gap of some sectors between them. In this case,
  2018. // the gap is filled with zeros (therefore only applicable for yet
  2019. // unused space in format like qcow2).
  2020. if (!merge && bs->drv->bdrv_merge_requests) {
  2021. merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]);
  2022. }
  2023. if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) {
  2024. merge = 0;
  2025. }
  2026. if (merge) {
  2027. size_t size;
  2028. QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov));
  2029. qemu_iovec_init(qiov,
  2030. reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
  2031. // Add the first request to the merged one. If the requests are
  2032. // overlapping, drop the last sectors of the first request.
  2033. size = (reqs[i].sector - reqs[outidx].sector) << 9;
  2034. qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
  2035. // We might need to add some zeros between the two requests
  2036. if (reqs[i].sector > oldreq_last) {
  2037. size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
  2038. uint8_t *buf = qemu_blockalign(bs, zero_bytes);
  2039. memset(buf, 0, zero_bytes);
  2040. qemu_iovec_add(qiov, buf, zero_bytes);
  2041. mcb->callbacks[i].free_buf = buf;
  2042. }
  2043. // Add the second request
  2044. qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
  2045. reqs[outidx].nb_sectors = qiov->size >> 9;
  2046. reqs[outidx].qiov = qiov;
  2047. mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
  2048. } else {
  2049. outidx++;
  2050. reqs[outidx].sector = reqs[i].sector;
  2051. reqs[outidx].nb_sectors = reqs[i].nb_sectors;
  2052. reqs[outidx].qiov = reqs[i].qiov;
  2053. }
  2054. }
  2055. return outidx + 1;
  2056. }
  2057. /*
  2058. * Submit multiple AIO write requests at once.
  2059. *
  2060. * On success, the function returns 0 and all requests in the reqs array have
  2061. * been submitted. In error case this function returns -1, and any of the
  2062. * requests may or may not be submitted yet. In particular, this means that the
  2063. * callback will be called for some of the requests, for others it won't. The
  2064. * caller must check the error field of the BlockRequest to wait for the right
  2065. * callbacks (if error != 0, no callback will be called).
  2066. *
  2067. * The implementation may modify the contents of the reqs array, e.g. to merge
  2068. * requests. However, the fields opaque and error are left unmodified as they
  2069. * are used to signal failure for a single request to the caller.
  2070. */
  2071. int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
  2072. {
  2073. BlockDriverAIOCB *acb;
  2074. MultiwriteCB *mcb;
  2075. int i;
  2076. /* don't submit writes if we don't have a medium */
  2077. if (bs->drv == NULL) {
  2078. for (i = 0; i < num_reqs; i++) {
  2079. reqs[i].error = -ENOMEDIUM;
  2080. }
  2081. return -1;
  2082. }
  2083. if (num_reqs == 0) {
  2084. return 0;
  2085. }
  2086. // Create MultiwriteCB structure
  2087. mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
  2088. mcb->num_requests = 0;
  2089. mcb->num_callbacks = num_reqs;
  2090. for (i = 0; i < num_reqs; i++) {
  2091. mcb->callbacks[i].cb = reqs[i].cb;
  2092. mcb->callbacks[i].opaque = reqs[i].opaque;
  2093. }
  2094. // Check for mergable requests
  2095. num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
  2096. trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs);
  2097. /*
  2098. * Run the aio requests. As soon as one request can't be submitted
  2099. * successfully, fail all requests that are not yet submitted (we must
  2100. * return failure for all requests anyway)
  2101. *
  2102. * num_requests cannot be set to the right value immediately: If
  2103. * bdrv_aio_writev fails for some request, num_requests would be too high
  2104. * and therefore multiwrite_cb() would never recognize the multiwrite
  2105. * request as completed. We also cannot use the loop variable i to set it
  2106. * when the first request fails because the callback may already have been
  2107. * called for previously submitted requests. Thus, num_requests must be
  2108. * incremented for each request that is submitted.
  2109. *
  2110. * The problem that callbacks may be called early also means that we need
  2111. * to take care that num_requests doesn't become 0 before all requests are
  2112. * submitted - multiwrite_cb() would consider the multiwrite request
  2113. * completed. A dummy request that is "completed" by a manual call to
  2114. * multiwrite_cb() takes care of this.
  2115. */
  2116. mcb->num_requests = 1;
  2117. // Run the aio requests
  2118. for (i = 0; i < num_reqs; i++) {
  2119. mcb->num_requests++;
  2120. acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
  2121. reqs[i].nb_sectors, multiwrite_cb, mcb);
  2122. if (acb == NULL) {
  2123. // We can only fail the whole thing if no request has been
  2124. // submitted yet. Otherwise we'll wait for the submitted AIOs to
  2125. // complete and report the error in the callback.
  2126. if (i == 0) {
  2127. trace_bdrv_aio_multiwrite_earlyfail(mcb);
  2128. goto fail;
  2129. } else {
  2130. trace_bdrv_aio_multiwrite_latefail(mcb, i);
  2131. multiwrite_cb(mcb, -EIO);
  2132. break;
  2133. }
  2134. }
  2135. }
  2136. /* Complete the dummy request */
  2137. multiwrite_cb(mcb, 0);
  2138. return 0;
  2139. fail:
  2140. for (i = 0; i < mcb->num_callbacks; i++) {
  2141. reqs[i].error = -EIO;
  2142. }
  2143. qemu_free(mcb);
  2144. return -1;
  2145. }
  2146. BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
  2147. BlockDriverCompletionFunc *cb, void *opaque)
  2148. {
  2149. BlockDriver *drv = bs->drv;
  2150. trace_bdrv_aio_flush(bs, opaque);
  2151. if (bs->open_flags & BDRV_O_NO_FLUSH) {
  2152. return bdrv_aio_noop_em(bs, cb, opaque);
  2153. }
  2154. if (!drv)
  2155. return NULL;
  2156. return drv->bdrv_aio_flush(bs, cb, opaque);
  2157. }
  2158. void bdrv_aio_cancel(BlockDriverAIOCB *acb)
  2159. {
  2160. acb->pool->cancel(acb);
  2161. }
  2162. /**************************************************************/
  2163. /* async block device emulation */
  2164. typedef struct BlockDriverAIOCBSync {
  2165. BlockDriverAIOCB common;
  2166. QEMUBH *bh;
  2167. int ret;
  2168. /* vector translation state */
  2169. QEMUIOVector *qiov;
  2170. uint8_t *bounce;
  2171. int is_write;
  2172. } BlockDriverAIOCBSync;
  2173. static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
  2174. {
  2175. BlockDriverAIOCBSync *acb =
  2176. container_of(blockacb, BlockDriverAIOCBSync, common);
  2177. qemu_bh_delete(acb->bh);
  2178. acb->bh = NULL;
  2179. qemu_aio_release(acb);
  2180. }
  2181. static AIOPool bdrv_em_aio_pool = {
  2182. .aiocb_size = sizeof(BlockDriverAIOCBSync),
  2183. .cancel = bdrv_aio_cancel_em,
  2184. };
  2185. static void bdrv_aio_bh_cb(void *opaque)
  2186. {
  2187. BlockDriverAIOCBSync *acb = opaque;
  2188. if (!acb->is_write)
  2189. qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
  2190. qemu_vfree(acb->bounce);
  2191. acb->common.cb(acb->common.opaque, acb->ret);
  2192. qemu_bh_delete(acb->bh);
  2193. acb->bh = NULL;
  2194. qemu_aio_release(acb);
  2195. }
  2196. static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
  2197. int64_t sector_num,
  2198. QEMUIOVector *qiov,
  2199. int nb_sectors,
  2200. BlockDriverCompletionFunc *cb,
  2201. void *opaque,
  2202. int is_write)
  2203. {
  2204. BlockDriverAIOCBSync *acb;
  2205. acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
  2206. acb->is_write = is_write;
  2207. acb->qiov = qiov;
  2208. acb->bounce = qemu_blockalign(bs, qiov->size);
  2209. if (!acb->bh)
  2210. acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
  2211. if (is_write) {
  2212. qemu_iovec_to_buffer(acb->qiov, acb->bounce);
  2213. acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
  2214. } else {
  2215. acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
  2216. }
  2217. qemu_bh_schedule(acb->bh);
  2218. return &acb->common;
  2219. }
  2220. static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
  2221. int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
  2222. BlockDriverCompletionFunc *cb, void *opaque)
  2223. {
  2224. return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
  2225. }
  2226. static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
  2227. int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
  2228. BlockDriverCompletionFunc *cb, void *opaque)
  2229. {
  2230. return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
  2231. }
  2232. static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
  2233. BlockDriverCompletionFunc *cb, void *opaque)
  2234. {
  2235. BlockDriverAIOCBSync *acb;
  2236. acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
  2237. acb->is_write = 1; /* don't bounce in the completion hadler */
  2238. acb->qiov = NULL;
  2239. acb->bounce = NULL;
  2240. acb->ret = 0;
  2241. if (!acb->bh)
  2242. acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
  2243. bdrv_flush(bs);
  2244. qemu_bh_schedule(acb->bh);
  2245. return &acb->common;
  2246. }
  2247. static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,
  2248. BlockDriverCompletionFunc *cb, void *opaque)
  2249. {
  2250. BlockDriverAIOCBSync *acb;
  2251. acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
  2252. acb->is_write = 1; /* don't bounce in the completion handler */
  2253. acb->qiov = NULL;
  2254. acb->bounce = NULL;
  2255. acb->ret = 0;
  2256. if (!acb->bh) {
  2257. acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
  2258. }
  2259. qemu_bh_schedule(acb->bh);
  2260. return &acb->common;
  2261. }
  2262. /**************************************************************/
  2263. /* sync block device emulation */
  2264. static void bdrv_rw_em_cb(void *opaque, int ret)
  2265. {
  2266. *(int *)opaque = ret;
  2267. }
  2268. #define NOT_DONE 0x7fffffff
  2269. static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
  2270. uint8_t *buf, int nb_sectors)
  2271. {
  2272. int async_ret;
  2273. BlockDriverAIOCB *acb;
  2274. struct iovec iov;
  2275. QEMUIOVector qiov;
  2276. async_context_push();
  2277. async_ret = NOT_DONE;
  2278. iov.iov_base = (void *)buf;
  2279. iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
  2280. qemu_iovec_init_external(&qiov, &iov, 1);
  2281. acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
  2282. bdrv_rw_em_cb, &async_ret);
  2283. if (acb == NULL) {
  2284. async_ret = -1;
  2285. goto fail;
  2286. }
  2287. while (async_ret == NOT_DONE) {
  2288. qemu_aio_wait();
  2289. }
  2290. fail:
  2291. async_context_pop();
  2292. return async_ret;
  2293. }
  2294. static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
  2295. const uint8_t *buf, int nb_sectors)
  2296. {
  2297. int async_ret;
  2298. BlockDriverAIOCB *acb;
  2299. struct iovec iov;
  2300. QEMUIOVector qiov;
  2301. async_context_push();
  2302. async_ret = NOT_DONE;
  2303. iov.iov_base = (void *)buf;
  2304. iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
  2305. qemu_iovec_init_external(&qiov, &iov, 1);
  2306. acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
  2307. bdrv_rw_em_cb, &async_ret);
  2308. if (acb == NULL) {
  2309. async_ret = -1;
  2310. goto fail;
  2311. }
  2312. while (async_ret == NOT_DONE) {
  2313. qemu_aio_wait();
  2314. }
  2315. fail:
  2316. async_context_pop();
  2317. return async_ret;
  2318. }
  2319. void bdrv_init(void)
  2320. {
  2321. module_call_init(MODULE_INIT_BLOCK);
  2322. }
  2323. void bdrv_init_with_whitelist(void)
  2324. {
  2325. use_bdrv_whitelist = 1;
  2326. bdrv_init();
  2327. }
  2328. void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
  2329. BlockDriverCompletionFunc *cb, void *opaque)
  2330. {
  2331. BlockDriverAIOCB *acb;
  2332. if (pool->free_aiocb) {
  2333. acb = pool->free_aiocb;
  2334. pool->free_aiocb = acb->next;
  2335. } else {
  2336. acb = qemu_mallocz(pool->aiocb_size);
  2337. acb->pool = pool;
  2338. }
  2339. acb->bs = bs;
  2340. acb->cb = cb;
  2341. acb->opaque = opaque;
  2342. return acb;
  2343. }
  2344. void qemu_aio_release(void *p)
  2345. {
  2346. BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
  2347. AIOPool *pool = acb->pool;
  2348. acb->next = pool->free_aiocb;
  2349. pool->free_aiocb = acb;
  2350. }
  2351. /**************************************************************/
  2352. /* removable device support */
  2353. /**
  2354. * Return TRUE if the media is present
  2355. */
  2356. int bdrv_is_inserted(BlockDriverState *bs)
  2357. {
  2358. BlockDriver *drv = bs->drv;
  2359. int ret;
  2360. if (!drv)
  2361. return 0;
  2362. if (!drv->bdrv_is_inserted)
  2363. return !bs->tray_open;
  2364. ret = drv->bdrv_is_inserted(bs);
  2365. return ret;
  2366. }
  2367. /**
  2368. * Return TRUE if the media changed since the last call to this
  2369. * function. It is currently only used for floppy disks
  2370. */
  2371. int bdrv_media_changed(BlockDriverState *bs)
  2372. {
  2373. BlockDriver *drv = bs->drv;
  2374. int ret;
  2375. if (!drv || !drv->bdrv_media_changed)
  2376. ret = -ENOTSUP;
  2377. else
  2378. ret = drv->bdrv_media_changed(bs);
  2379. if (ret == -ENOTSUP)
  2380. ret = bs->media_changed;
  2381. bs->media_changed = 0;
  2382. return ret;
  2383. }
  2384. /**
  2385. * If eject_flag is TRUE, eject the media. Otherwise, close the tray
  2386. */
  2387. int bdrv_eject(BlockDriverState *bs, int eject_flag)
  2388. {
  2389. BlockDriver *drv = bs->drv;
  2390. int ret;
  2391. if (bs->locked) {
  2392. return -EBUSY;
  2393. }
  2394. if (!drv || !drv->bdrv_eject) {
  2395. ret = -ENOTSUP;
  2396. } else {
  2397. ret = drv->bdrv_eject(bs, eject_flag);
  2398. }
  2399. if (ret == -ENOTSUP) {
  2400. ret = 0;
  2401. }
  2402. if (ret >= 0) {
  2403. bs->tray_open = eject_flag;
  2404. }
  2405. return ret;
  2406. }
  2407. int bdrv_is_locked(BlockDriverState *bs)
  2408. {
  2409. return bs->locked;
  2410. }
  2411. /**
  2412. * Lock or unlock the media (if it is locked, the user won't be able
  2413. * to eject it manually).
  2414. */
  2415. void bdrv_set_locked(BlockDriverState *bs, int locked)
  2416. {
  2417. BlockDriver *drv = bs->drv;
  2418. trace_bdrv_set_locked(bs, locked);
  2419. bs->locked = locked;
  2420. if (drv && drv->bdrv_set_locked) {
  2421. drv->bdrv_set_locked(bs, locked);
  2422. }
  2423. }
  2424. /* needed for generic scsi interface */
  2425. int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
  2426. {
  2427. BlockDriver *drv = bs->drv;
  2428. if (drv && drv->bdrv_ioctl)
  2429. return drv->bdrv_ioctl(bs, req, buf);
  2430. return -ENOTSUP;
  2431. }
  2432. BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
  2433. unsigned long int req, void *buf,
  2434. BlockDriverCompletionFunc *cb, void *opaque)
  2435. {
  2436. BlockDriver *drv = bs->drv;
  2437. if (drv && drv->bdrv_aio_ioctl)
  2438. return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
  2439. return NULL;
  2440. }
  2441. void *qemu_blockalign(BlockDriverState *bs, size_t size)
  2442. {
  2443. return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
  2444. }
  2445. void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable)
  2446. {
  2447. int64_t bitmap_size;
  2448. bs->dirty_count = 0;
  2449. if (enable) {
  2450. if (!bs->dirty_bitmap) {
  2451. bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
  2452. BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
  2453. bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
  2454. bs->dirty_bitmap = qemu_mallocz(bitmap_size);
  2455. }
  2456. } else {
  2457. if (bs->dirty_bitmap) {
  2458. qemu_free(bs->dirty_bitmap);
  2459. bs->dirty_bitmap = NULL;
  2460. }
  2461. }
  2462. }
  2463. int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
  2464. {
  2465. int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
  2466. if (bs->dirty_bitmap &&
  2467. (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) {
  2468. return !!(bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] &
  2469. (1UL << (chunk % (sizeof(unsigned long) * 8))));
  2470. } else {
  2471. return 0;
  2472. }
  2473. }
  2474. void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
  2475. int nr_sectors)
  2476. {
  2477. set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
  2478. }
  2479. int64_t bdrv_get_dirty_count(BlockDriverState *bs)
  2480. {
  2481. return bs->dirty_count;
  2482. }
  2483. void bdrv_set_in_use(BlockDriverState *bs, int in_use)
  2484. {
  2485. assert(bs->in_use != in_use);
  2486. bs->in_use = in_use;
  2487. }
  2488. int bdrv_in_use(BlockDriverState *bs)
  2489. {
  2490. return bs->in_use;
  2491. }
  2492. int bdrv_img_create(const char *filename, const char *fmt,
  2493. const char *base_filename, const char *base_fmt,
  2494. char *options, uint64_t img_size, int flags)
  2495. {
  2496. QEMUOptionParameter *param = NULL, *create_options = NULL;
  2497. QEMUOptionParameter *backing_fmt, *backing_file, *size;
  2498. BlockDriverState *bs = NULL;
  2499. BlockDriver *drv, *proto_drv;
  2500. BlockDriver *backing_drv = NULL;
  2501. int ret = 0;
  2502. /* Find driver and parse its options */
  2503. drv = bdrv_find_format(fmt);
  2504. if (!drv) {
  2505. error_report("Unknown file format '%s'", fmt);
  2506. ret = -EINVAL;
  2507. goto out;
  2508. }
  2509. proto_drv = bdrv_find_protocol(filename);
  2510. if (!proto_drv) {
  2511. error_report("Unknown protocol '%s'", filename);
  2512. ret = -EINVAL;
  2513. goto out;
  2514. }
  2515. create_options = append_option_parameters(create_options,
  2516. drv->create_options);
  2517. create_options = append_option_parameters(create_options,
  2518. proto_drv->create_options);
  2519. /* Create parameter list with default values */
  2520. param = parse_option_parameters("", create_options, param);
  2521. set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size);
  2522. /* Parse -o options */
  2523. if (options) {
  2524. param = parse_option_parameters(options, create_options, param);
  2525. if (param == NULL) {
  2526. error_report("Invalid options for file format '%s'.", fmt);
  2527. ret = -EINVAL;
  2528. goto out;
  2529. }
  2530. }
  2531. if (base_filename) {
  2532. if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE,
  2533. base_filename)) {
  2534. error_report("Backing file not supported for file format '%s'",
  2535. fmt);
  2536. ret = -EINVAL;
  2537. goto out;
  2538. }
  2539. }
  2540. if (base_fmt) {
  2541. if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) {
  2542. error_report("Backing file format not supported for file "
  2543. "format '%s'", fmt);
  2544. ret = -EINVAL;
  2545. goto out;
  2546. }
  2547. }
  2548. backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
  2549. if (backing_file && backing_file->value.s) {
  2550. if (!strcmp(filename, backing_file->value.s)) {
  2551. error_report("Error: Trying to create an image with the "
  2552. "same filename as the backing file");
  2553. ret = -EINVAL;
  2554. goto out;
  2555. }
  2556. }
  2557. backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT);
  2558. if (backing_fmt && backing_fmt->value.s) {
  2559. backing_drv = bdrv_find_format(backing_fmt->value.s);
  2560. if (!backing_drv) {
  2561. error_report("Unknown backing file format '%s'",
  2562. backing_fmt->value.s);
  2563. ret = -EINVAL;
  2564. goto out;
  2565. }
  2566. }
  2567. // The size for the image must always be specified, with one exception:
  2568. // If we are using a backing file, we can obtain the size from there
  2569. size = get_option_parameter(param, BLOCK_OPT_SIZE);
  2570. if (size && size->value.n == -1) {
  2571. if (backing_file && backing_file->value.s) {
  2572. uint64_t size;
  2573. char buf[32];
  2574. bs = bdrv_new("");
  2575. ret = bdrv_open(bs, backing_file->value.s, flags, backing_drv);
  2576. if (ret < 0) {
  2577. error_report("Could not open '%s'", backing_file->value.s);
  2578. goto out;
  2579. }
  2580. bdrv_get_geometry(bs, &size);
  2581. size *= 512;
  2582. snprintf(buf, sizeof(buf), "%" PRId64, size);
  2583. set_option_parameter(param, BLOCK_OPT_SIZE, buf);
  2584. } else {
  2585. error_report("Image creation needs a size parameter");
  2586. ret = -EINVAL;
  2587. goto out;
  2588. }
  2589. }
  2590. printf("Formatting '%s', fmt=%s ", filename, fmt);
  2591. print_option_parameters(param);
  2592. puts("");
  2593. ret = bdrv_create(drv, filename, param);
  2594. if (ret < 0) {
  2595. if (ret == -ENOTSUP) {
  2596. error_report("Formatting or formatting option not supported for "
  2597. "file format '%s'", fmt);
  2598. } else if (ret == -EFBIG) {
  2599. error_report("The image size is too large for file format '%s'",
  2600. fmt);
  2601. } else {
  2602. error_report("%s: error while creating %s: %s", filename, fmt,
  2603. strerror(-ret));
  2604. }
  2605. }
  2606. out:
  2607. free_option_parameters(create_options);
  2608. free_option_parameters(param);
  2609. if (bs) {
  2610. bdrv_delete(bs);
  2611. }
  2612. return ret;
  2613. }