2
0

xen_disk.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. /*
  2. * xen paravirt block device backend
  3. *
  4. * (c) Gerd Hoffmann <kraxel@redhat.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; under version 2 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * Contributions after 2012-01-13 are licensed under the terms of the
  19. * GNU GPL, version 2 or (at your option) any later version.
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <stdarg.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26. #include <signal.h>
  27. #include <inttypes.h>
  28. #include <time.h>
  29. #include <fcntl.h>
  30. #include <errno.h>
  31. #include <sys/ioctl.h>
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #include <sys/mman.h>
  35. #include <sys/uio.h>
  36. #include "hw.h"
  37. #include "qemu-char.h"
  38. #include "xen_backend.h"
  39. #include "xen_blkif.h"
  40. #include "blockdev.h"
  41. /* ------------------------------------------------------------- */
  42. static int batch_maps = 0;
  43. static int max_requests = 32;
  44. /* ------------------------------------------------------------- */
  45. #define BLOCK_SIZE 512
  46. #define IOCB_COUNT (BLKIF_MAX_SEGMENTS_PER_REQUEST + 2)
  47. struct ioreq {
  48. blkif_request_t req;
  49. int16_t status;
  50. /* parsed request */
  51. off_t start;
  52. QEMUIOVector v;
  53. int presync;
  54. int postsync;
  55. uint8_t mapped;
  56. /* grant mapping */
  57. uint32_t domids[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  58. uint32_t refs[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  59. int prot;
  60. void *page[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  61. void *pages;
  62. /* aio status */
  63. int aio_inflight;
  64. int aio_errors;
  65. struct XenBlkDev *blkdev;
  66. QLIST_ENTRY(ioreq) list;
  67. BlockAcctCookie acct;
  68. };
  69. struct XenBlkDev {
  70. struct XenDevice xendev; /* must be first */
  71. char *params;
  72. char *mode;
  73. char *type;
  74. char *dev;
  75. char *devtype;
  76. const char *fileproto;
  77. const char *filename;
  78. int ring_ref;
  79. void *sring;
  80. int64_t file_blk;
  81. int64_t file_size;
  82. int protocol;
  83. blkif_back_rings_t rings;
  84. int more_work;
  85. int cnt_map;
  86. /* request lists */
  87. QLIST_HEAD(inflight_head, ioreq) inflight;
  88. QLIST_HEAD(finished_head, ioreq) finished;
  89. QLIST_HEAD(freelist_head, ioreq) freelist;
  90. int requests_total;
  91. int requests_inflight;
  92. int requests_finished;
  93. /* qemu block driver */
  94. DriveInfo *dinfo;
  95. BlockDriverState *bs;
  96. QEMUBH *bh;
  97. };
  98. /* ------------------------------------------------------------- */
  99. static struct ioreq *ioreq_start(struct XenBlkDev *blkdev)
  100. {
  101. struct ioreq *ioreq = NULL;
  102. if (QLIST_EMPTY(&blkdev->freelist)) {
  103. if (blkdev->requests_total >= max_requests) {
  104. goto out;
  105. }
  106. /* allocate new struct */
  107. ioreq = g_malloc0(sizeof(*ioreq));
  108. ioreq->blkdev = blkdev;
  109. blkdev->requests_total++;
  110. qemu_iovec_init(&ioreq->v, BLKIF_MAX_SEGMENTS_PER_REQUEST);
  111. } else {
  112. /* get one from freelist */
  113. ioreq = QLIST_FIRST(&blkdev->freelist);
  114. QLIST_REMOVE(ioreq, list);
  115. qemu_iovec_reset(&ioreq->v);
  116. }
  117. QLIST_INSERT_HEAD(&blkdev->inflight, ioreq, list);
  118. blkdev->requests_inflight++;
  119. out:
  120. return ioreq;
  121. }
  122. static void ioreq_finish(struct ioreq *ioreq)
  123. {
  124. struct XenBlkDev *blkdev = ioreq->blkdev;
  125. QLIST_REMOVE(ioreq, list);
  126. QLIST_INSERT_HEAD(&blkdev->finished, ioreq, list);
  127. blkdev->requests_inflight--;
  128. blkdev->requests_finished++;
  129. }
  130. static void ioreq_release(struct ioreq *ioreq, bool finish)
  131. {
  132. struct XenBlkDev *blkdev = ioreq->blkdev;
  133. QLIST_REMOVE(ioreq, list);
  134. memset(ioreq, 0, sizeof(*ioreq));
  135. ioreq->blkdev = blkdev;
  136. QLIST_INSERT_HEAD(&blkdev->freelist, ioreq, list);
  137. if (finish) {
  138. blkdev->requests_finished--;
  139. } else {
  140. blkdev->requests_inflight--;
  141. }
  142. }
  143. /*
  144. * translate request into iovec + start offset
  145. * do sanity checks along the way
  146. */
  147. static int ioreq_parse(struct ioreq *ioreq)
  148. {
  149. struct XenBlkDev *blkdev = ioreq->blkdev;
  150. uintptr_t mem;
  151. size_t len;
  152. int i;
  153. xen_be_printf(&blkdev->xendev, 3,
  154. "op %d, nr %d, handle %d, id %" PRId64 ", sector %" PRId64 "\n",
  155. ioreq->req.operation, ioreq->req.nr_segments,
  156. ioreq->req.handle, ioreq->req.id, ioreq->req.sector_number);
  157. switch (ioreq->req.operation) {
  158. case BLKIF_OP_READ:
  159. ioreq->prot = PROT_WRITE; /* to memory */
  160. break;
  161. case BLKIF_OP_WRITE_BARRIER:
  162. if (!ioreq->req.nr_segments) {
  163. ioreq->presync = 1;
  164. return 0;
  165. }
  166. ioreq->presync = ioreq->postsync = 1;
  167. /* fall through */
  168. case BLKIF_OP_WRITE:
  169. ioreq->prot = PROT_READ; /* from memory */
  170. break;
  171. default:
  172. xen_be_printf(&blkdev->xendev, 0, "error: unknown operation (%d)\n",
  173. ioreq->req.operation);
  174. goto err;
  175. };
  176. if (ioreq->req.operation != BLKIF_OP_READ && blkdev->mode[0] != 'w') {
  177. xen_be_printf(&blkdev->xendev, 0, "error: write req for ro device\n");
  178. goto err;
  179. }
  180. ioreq->start = ioreq->req.sector_number * blkdev->file_blk;
  181. for (i = 0; i < ioreq->req.nr_segments; i++) {
  182. if (i == BLKIF_MAX_SEGMENTS_PER_REQUEST) {
  183. xen_be_printf(&blkdev->xendev, 0, "error: nr_segments too big\n");
  184. goto err;
  185. }
  186. if (ioreq->req.seg[i].first_sect > ioreq->req.seg[i].last_sect) {
  187. xen_be_printf(&blkdev->xendev, 0, "error: first > last sector\n");
  188. goto err;
  189. }
  190. if (ioreq->req.seg[i].last_sect * BLOCK_SIZE >= XC_PAGE_SIZE) {
  191. xen_be_printf(&blkdev->xendev, 0, "error: page crossing\n");
  192. goto err;
  193. }
  194. ioreq->domids[i] = blkdev->xendev.dom;
  195. ioreq->refs[i] = ioreq->req.seg[i].gref;
  196. mem = ioreq->req.seg[i].first_sect * blkdev->file_blk;
  197. len = (ioreq->req.seg[i].last_sect - ioreq->req.seg[i].first_sect + 1) * blkdev->file_blk;
  198. qemu_iovec_add(&ioreq->v, (void*)mem, len);
  199. }
  200. if (ioreq->start + ioreq->v.size > blkdev->file_size) {
  201. xen_be_printf(&blkdev->xendev, 0, "error: access beyond end of file\n");
  202. goto err;
  203. }
  204. return 0;
  205. err:
  206. ioreq->status = BLKIF_RSP_ERROR;
  207. return -1;
  208. }
  209. static void ioreq_unmap(struct ioreq *ioreq)
  210. {
  211. XenGnttab gnt = ioreq->blkdev->xendev.gnttabdev;
  212. int i;
  213. if (ioreq->v.niov == 0 || ioreq->mapped == 0) {
  214. return;
  215. }
  216. if (batch_maps) {
  217. if (!ioreq->pages) {
  218. return;
  219. }
  220. if (xc_gnttab_munmap(gnt, ioreq->pages, ioreq->v.niov) != 0) {
  221. xen_be_printf(&ioreq->blkdev->xendev, 0, "xc_gnttab_munmap failed: %s\n",
  222. strerror(errno));
  223. }
  224. ioreq->blkdev->cnt_map -= ioreq->v.niov;
  225. ioreq->pages = NULL;
  226. } else {
  227. for (i = 0; i < ioreq->v.niov; i++) {
  228. if (!ioreq->page[i]) {
  229. continue;
  230. }
  231. if (xc_gnttab_munmap(gnt, ioreq->page[i], 1) != 0) {
  232. xen_be_printf(&ioreq->blkdev->xendev, 0, "xc_gnttab_munmap failed: %s\n",
  233. strerror(errno));
  234. }
  235. ioreq->blkdev->cnt_map--;
  236. ioreq->page[i] = NULL;
  237. }
  238. }
  239. ioreq->mapped = 0;
  240. }
  241. static int ioreq_map(struct ioreq *ioreq)
  242. {
  243. XenGnttab gnt = ioreq->blkdev->xendev.gnttabdev;
  244. int i;
  245. if (ioreq->v.niov == 0 || ioreq->mapped == 1) {
  246. return 0;
  247. }
  248. if (batch_maps) {
  249. ioreq->pages = xc_gnttab_map_grant_refs
  250. (gnt, ioreq->v.niov, ioreq->domids, ioreq->refs, ioreq->prot);
  251. if (ioreq->pages == NULL) {
  252. xen_be_printf(&ioreq->blkdev->xendev, 0,
  253. "can't map %d grant refs (%s, %d maps)\n",
  254. ioreq->v.niov, strerror(errno), ioreq->blkdev->cnt_map);
  255. return -1;
  256. }
  257. for (i = 0; i < ioreq->v.niov; i++) {
  258. ioreq->v.iov[i].iov_base = ioreq->pages + i * XC_PAGE_SIZE +
  259. (uintptr_t)ioreq->v.iov[i].iov_base;
  260. }
  261. ioreq->blkdev->cnt_map += ioreq->v.niov;
  262. } else {
  263. for (i = 0; i < ioreq->v.niov; i++) {
  264. ioreq->page[i] = xc_gnttab_map_grant_ref
  265. (gnt, ioreq->domids[i], ioreq->refs[i], ioreq->prot);
  266. if (ioreq->page[i] == NULL) {
  267. xen_be_printf(&ioreq->blkdev->xendev, 0,
  268. "can't map grant ref %d (%s, %d maps)\n",
  269. ioreq->refs[i], strerror(errno), ioreq->blkdev->cnt_map);
  270. ioreq_unmap(ioreq);
  271. return -1;
  272. }
  273. ioreq->v.iov[i].iov_base = ioreq->page[i] + (uintptr_t)ioreq->v.iov[i].iov_base;
  274. ioreq->blkdev->cnt_map++;
  275. }
  276. }
  277. ioreq->mapped = 1;
  278. return 0;
  279. }
  280. static int ioreq_runio_qemu_aio(struct ioreq *ioreq);
  281. static void qemu_aio_complete(void *opaque, int ret)
  282. {
  283. struct ioreq *ioreq = opaque;
  284. if (ret != 0) {
  285. xen_be_printf(&ioreq->blkdev->xendev, 0, "%s I/O error\n",
  286. ioreq->req.operation == BLKIF_OP_READ ? "read" : "write");
  287. ioreq->aio_errors++;
  288. }
  289. ioreq->aio_inflight--;
  290. if (ioreq->presync) {
  291. ioreq->presync = 0;
  292. ioreq_runio_qemu_aio(ioreq);
  293. return;
  294. }
  295. if (ioreq->aio_inflight > 0) {
  296. return;
  297. }
  298. if (ioreq->postsync) {
  299. ioreq->postsync = 0;
  300. ioreq->aio_inflight++;
  301. bdrv_aio_flush(ioreq->blkdev->bs, qemu_aio_complete, ioreq);
  302. return;
  303. }
  304. ioreq->status = ioreq->aio_errors ? BLKIF_RSP_ERROR : BLKIF_RSP_OKAY;
  305. ioreq_unmap(ioreq);
  306. ioreq_finish(ioreq);
  307. bdrv_acct_done(ioreq->blkdev->bs, &ioreq->acct);
  308. qemu_bh_schedule(ioreq->blkdev->bh);
  309. }
  310. static int ioreq_runio_qemu_aio(struct ioreq *ioreq)
  311. {
  312. struct XenBlkDev *blkdev = ioreq->blkdev;
  313. if (ioreq->req.nr_segments && ioreq_map(ioreq) == -1) {
  314. goto err_no_map;
  315. }
  316. ioreq->aio_inflight++;
  317. if (ioreq->presync) {
  318. bdrv_aio_flush(ioreq->blkdev->bs, qemu_aio_complete, ioreq);
  319. return 0;
  320. }
  321. switch (ioreq->req.operation) {
  322. case BLKIF_OP_READ:
  323. bdrv_acct_start(blkdev->bs, &ioreq->acct, ioreq->v.size, BDRV_ACCT_READ);
  324. ioreq->aio_inflight++;
  325. bdrv_aio_readv(blkdev->bs, ioreq->start / BLOCK_SIZE,
  326. &ioreq->v, ioreq->v.size / BLOCK_SIZE,
  327. qemu_aio_complete, ioreq);
  328. break;
  329. case BLKIF_OP_WRITE:
  330. case BLKIF_OP_WRITE_BARRIER:
  331. if (!ioreq->req.nr_segments) {
  332. break;
  333. }
  334. bdrv_acct_start(blkdev->bs, &ioreq->acct, ioreq->v.size, BDRV_ACCT_WRITE);
  335. ioreq->aio_inflight++;
  336. bdrv_aio_writev(blkdev->bs, ioreq->start / BLOCK_SIZE,
  337. &ioreq->v, ioreq->v.size / BLOCK_SIZE,
  338. qemu_aio_complete, ioreq);
  339. break;
  340. default:
  341. /* unknown operation (shouldn't happen -- parse catches this) */
  342. goto err;
  343. }
  344. qemu_aio_complete(ioreq, 0);
  345. return 0;
  346. err:
  347. ioreq_unmap(ioreq);
  348. err_no_map:
  349. ioreq_finish(ioreq);
  350. ioreq->status = BLKIF_RSP_ERROR;
  351. return -1;
  352. }
  353. static int blk_send_response_one(struct ioreq *ioreq)
  354. {
  355. struct XenBlkDev *blkdev = ioreq->blkdev;
  356. int send_notify = 0;
  357. int have_requests = 0;
  358. blkif_response_t resp;
  359. void *dst;
  360. resp.id = ioreq->req.id;
  361. resp.operation = ioreq->req.operation;
  362. resp.status = ioreq->status;
  363. /* Place on the response ring for the relevant domain. */
  364. switch (blkdev->protocol) {
  365. case BLKIF_PROTOCOL_NATIVE:
  366. dst = RING_GET_RESPONSE(&blkdev->rings.native, blkdev->rings.native.rsp_prod_pvt);
  367. break;
  368. case BLKIF_PROTOCOL_X86_32:
  369. dst = RING_GET_RESPONSE(&blkdev->rings.x86_32_part,
  370. blkdev->rings.x86_32_part.rsp_prod_pvt);
  371. break;
  372. case BLKIF_PROTOCOL_X86_64:
  373. dst = RING_GET_RESPONSE(&blkdev->rings.x86_64_part,
  374. blkdev->rings.x86_64_part.rsp_prod_pvt);
  375. break;
  376. default:
  377. dst = NULL;
  378. }
  379. memcpy(dst, &resp, sizeof(resp));
  380. blkdev->rings.common.rsp_prod_pvt++;
  381. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blkdev->rings.common, send_notify);
  382. if (blkdev->rings.common.rsp_prod_pvt == blkdev->rings.common.req_cons) {
  383. /*
  384. * Tail check for pending requests. Allows frontend to avoid
  385. * notifications if requests are already in flight (lower
  386. * overheads and promotes batching).
  387. */
  388. RING_FINAL_CHECK_FOR_REQUESTS(&blkdev->rings.common, have_requests);
  389. } else if (RING_HAS_UNCONSUMED_REQUESTS(&blkdev->rings.common)) {
  390. have_requests = 1;
  391. }
  392. if (have_requests) {
  393. blkdev->more_work++;
  394. }
  395. return send_notify;
  396. }
  397. /* walk finished list, send outstanding responses, free requests */
  398. static void blk_send_response_all(struct XenBlkDev *blkdev)
  399. {
  400. struct ioreq *ioreq;
  401. int send_notify = 0;
  402. while (!QLIST_EMPTY(&blkdev->finished)) {
  403. ioreq = QLIST_FIRST(&blkdev->finished);
  404. send_notify += blk_send_response_one(ioreq);
  405. ioreq_release(ioreq, true);
  406. }
  407. if (send_notify) {
  408. xen_be_send_notify(&blkdev->xendev);
  409. }
  410. }
  411. static int blk_get_request(struct XenBlkDev *blkdev, struct ioreq *ioreq, RING_IDX rc)
  412. {
  413. switch (blkdev->protocol) {
  414. case BLKIF_PROTOCOL_NATIVE:
  415. memcpy(&ioreq->req, RING_GET_REQUEST(&blkdev->rings.native, rc),
  416. sizeof(ioreq->req));
  417. break;
  418. case BLKIF_PROTOCOL_X86_32:
  419. blkif_get_x86_32_req(&ioreq->req,
  420. RING_GET_REQUEST(&blkdev->rings.x86_32_part, rc));
  421. break;
  422. case BLKIF_PROTOCOL_X86_64:
  423. blkif_get_x86_64_req(&ioreq->req,
  424. RING_GET_REQUEST(&blkdev->rings.x86_64_part, rc));
  425. break;
  426. }
  427. return 0;
  428. }
  429. static void blk_handle_requests(struct XenBlkDev *blkdev)
  430. {
  431. RING_IDX rc, rp;
  432. struct ioreq *ioreq;
  433. blkdev->more_work = 0;
  434. rc = blkdev->rings.common.req_cons;
  435. rp = blkdev->rings.common.sring->req_prod;
  436. xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
  437. blk_send_response_all(blkdev);
  438. while (rc != rp) {
  439. /* pull request from ring */
  440. if (RING_REQUEST_CONS_OVERFLOW(&blkdev->rings.common, rc)) {
  441. break;
  442. }
  443. ioreq = ioreq_start(blkdev);
  444. if (ioreq == NULL) {
  445. blkdev->more_work++;
  446. break;
  447. }
  448. blk_get_request(blkdev, ioreq, rc);
  449. blkdev->rings.common.req_cons = ++rc;
  450. /* parse them */
  451. if (ioreq_parse(ioreq) != 0) {
  452. if (blk_send_response_one(ioreq)) {
  453. xen_be_send_notify(&blkdev->xendev);
  454. }
  455. ioreq_release(ioreq, false);
  456. continue;
  457. }
  458. ioreq_runio_qemu_aio(ioreq);
  459. }
  460. if (blkdev->more_work && blkdev->requests_inflight < max_requests) {
  461. qemu_bh_schedule(blkdev->bh);
  462. }
  463. }
  464. /* ------------------------------------------------------------- */
  465. static void blk_bh(void *opaque)
  466. {
  467. struct XenBlkDev *blkdev = opaque;
  468. blk_handle_requests(blkdev);
  469. }
  470. /*
  471. * We need to account for the grant allocations requiring contiguous
  472. * chunks; the worst case number would be
  473. * max_req * max_seg + (max_req - 1) * (max_seg - 1) + 1,
  474. * but in order to keep things simple just use
  475. * 2 * max_req * max_seg.
  476. */
  477. #define MAX_GRANTS(max_req, max_seg) (2 * (max_req) * (max_seg))
  478. static void blk_alloc(struct XenDevice *xendev)
  479. {
  480. struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
  481. QLIST_INIT(&blkdev->inflight);
  482. QLIST_INIT(&blkdev->finished);
  483. QLIST_INIT(&blkdev->freelist);
  484. blkdev->bh = qemu_bh_new(blk_bh, blkdev);
  485. if (xen_mode != XEN_EMULATE) {
  486. batch_maps = 1;
  487. }
  488. if (xc_gnttab_set_max_grants(xendev->gnttabdev,
  489. MAX_GRANTS(max_requests, BLKIF_MAX_SEGMENTS_PER_REQUEST)) < 0) {
  490. xen_be_printf(xendev, 0, "xc_gnttab_set_max_grants failed: %s\n",
  491. strerror(errno));
  492. }
  493. }
  494. static int blk_init(struct XenDevice *xendev)
  495. {
  496. struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
  497. int index, qflags, info = 0;
  498. /* read xenstore entries */
  499. if (blkdev->params == NULL) {
  500. char *h = NULL;
  501. blkdev->params = xenstore_read_be_str(&blkdev->xendev, "params");
  502. if (blkdev->params != NULL) {
  503. h = strchr(blkdev->params, ':');
  504. }
  505. if (h != NULL) {
  506. blkdev->fileproto = blkdev->params;
  507. blkdev->filename = h+1;
  508. *h = 0;
  509. } else {
  510. blkdev->fileproto = "<unset>";
  511. blkdev->filename = blkdev->params;
  512. }
  513. }
  514. if (!strcmp("aio", blkdev->fileproto)) {
  515. blkdev->fileproto = "raw";
  516. }
  517. if (blkdev->mode == NULL) {
  518. blkdev->mode = xenstore_read_be_str(&blkdev->xendev, "mode");
  519. }
  520. if (blkdev->type == NULL) {
  521. blkdev->type = xenstore_read_be_str(&blkdev->xendev, "type");
  522. }
  523. if (blkdev->dev == NULL) {
  524. blkdev->dev = xenstore_read_be_str(&blkdev->xendev, "dev");
  525. }
  526. if (blkdev->devtype == NULL) {
  527. blkdev->devtype = xenstore_read_be_str(&blkdev->xendev, "device-type");
  528. }
  529. /* do we have all we need? */
  530. if (blkdev->params == NULL ||
  531. blkdev->mode == NULL ||
  532. blkdev->type == NULL ||
  533. blkdev->dev == NULL) {
  534. goto out_error;
  535. }
  536. /* read-only ? */
  537. qflags = BDRV_O_NOCACHE | BDRV_O_CACHE_WB | BDRV_O_NATIVE_AIO;
  538. if (strcmp(blkdev->mode, "w") == 0) {
  539. qflags |= BDRV_O_RDWR;
  540. } else {
  541. info |= VDISK_READONLY;
  542. }
  543. /* cdrom ? */
  544. if (blkdev->devtype && !strcmp(blkdev->devtype, "cdrom")) {
  545. info |= VDISK_CDROM;
  546. }
  547. /* init qemu block driver */
  548. index = (blkdev->xendev.dev - 202 * 256) / 16;
  549. blkdev->dinfo = drive_get(IF_XEN, 0, index);
  550. if (!blkdev->dinfo) {
  551. /* setup via xenbus -> create new block driver instance */
  552. xen_be_printf(&blkdev->xendev, 2, "create new bdrv (xenbus setup)\n");
  553. blkdev->bs = bdrv_new(blkdev->dev);
  554. if (blkdev->bs) {
  555. if (bdrv_open(blkdev->bs, blkdev->filename, qflags,
  556. bdrv_find_whitelisted_format(blkdev->fileproto)) != 0) {
  557. bdrv_delete(blkdev->bs);
  558. blkdev->bs = NULL;
  559. }
  560. }
  561. if (!blkdev->bs) {
  562. goto out_error;
  563. }
  564. } else {
  565. /* setup via qemu cmdline -> already setup for us */
  566. xen_be_printf(&blkdev->xendev, 2, "get configured bdrv (cmdline setup)\n");
  567. blkdev->bs = blkdev->dinfo->bdrv;
  568. }
  569. bdrv_attach_dev_nofail(blkdev->bs, blkdev);
  570. blkdev->file_blk = BLOCK_SIZE;
  571. blkdev->file_size = bdrv_getlength(blkdev->bs);
  572. if (blkdev->file_size < 0) {
  573. xen_be_printf(&blkdev->xendev, 1, "bdrv_getlength: %d (%s) | drv %s\n",
  574. (int)blkdev->file_size, strerror(-blkdev->file_size),
  575. bdrv_get_format_name(blkdev->bs) ?: "-");
  576. blkdev->file_size = 0;
  577. }
  578. xen_be_printf(xendev, 1, "type \"%s\", fileproto \"%s\", filename \"%s\","
  579. " size %" PRId64 " (%" PRId64 " MB)\n",
  580. blkdev->type, blkdev->fileproto, blkdev->filename,
  581. blkdev->file_size, blkdev->file_size >> 20);
  582. /* fill info */
  583. xenstore_write_be_int(&blkdev->xendev, "feature-barrier", 1);
  584. xenstore_write_be_int(&blkdev->xendev, "info", info);
  585. xenstore_write_be_int(&blkdev->xendev, "sector-size", blkdev->file_blk);
  586. xenstore_write_be_int(&blkdev->xendev, "sectors",
  587. blkdev->file_size / blkdev->file_blk);
  588. return 0;
  589. out_error:
  590. g_free(blkdev->params);
  591. blkdev->params = NULL;
  592. g_free(blkdev->mode);
  593. blkdev->mode = NULL;
  594. g_free(blkdev->type);
  595. blkdev->type = NULL;
  596. g_free(blkdev->dev);
  597. blkdev->dev = NULL;
  598. g_free(blkdev->devtype);
  599. blkdev->devtype = NULL;
  600. return -1;
  601. }
  602. static int blk_connect(struct XenDevice *xendev)
  603. {
  604. struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
  605. if (xenstore_read_fe_int(&blkdev->xendev, "ring-ref", &blkdev->ring_ref) == -1) {
  606. return -1;
  607. }
  608. if (xenstore_read_fe_int(&blkdev->xendev, "event-channel",
  609. &blkdev->xendev.remote_port) == -1) {
  610. return -1;
  611. }
  612. blkdev->protocol = BLKIF_PROTOCOL_NATIVE;
  613. if (blkdev->xendev.protocol) {
  614. if (strcmp(blkdev->xendev.protocol, XEN_IO_PROTO_ABI_X86_32) == 0) {
  615. blkdev->protocol = BLKIF_PROTOCOL_X86_32;
  616. }
  617. if (strcmp(blkdev->xendev.protocol, XEN_IO_PROTO_ABI_X86_64) == 0) {
  618. blkdev->protocol = BLKIF_PROTOCOL_X86_64;
  619. }
  620. }
  621. blkdev->sring = xc_gnttab_map_grant_ref(blkdev->xendev.gnttabdev,
  622. blkdev->xendev.dom,
  623. blkdev->ring_ref,
  624. PROT_READ | PROT_WRITE);
  625. if (!blkdev->sring) {
  626. return -1;
  627. }
  628. blkdev->cnt_map++;
  629. switch (blkdev->protocol) {
  630. case BLKIF_PROTOCOL_NATIVE:
  631. {
  632. blkif_sring_t *sring_native = blkdev->sring;
  633. BACK_RING_INIT(&blkdev->rings.native, sring_native, XC_PAGE_SIZE);
  634. break;
  635. }
  636. case BLKIF_PROTOCOL_X86_32:
  637. {
  638. blkif_x86_32_sring_t *sring_x86_32 = blkdev->sring;
  639. BACK_RING_INIT(&blkdev->rings.x86_32_part, sring_x86_32, XC_PAGE_SIZE);
  640. break;
  641. }
  642. case BLKIF_PROTOCOL_X86_64:
  643. {
  644. blkif_x86_64_sring_t *sring_x86_64 = blkdev->sring;
  645. BACK_RING_INIT(&blkdev->rings.x86_64_part, sring_x86_64, XC_PAGE_SIZE);
  646. break;
  647. }
  648. }
  649. xen_be_bind_evtchn(&blkdev->xendev);
  650. xen_be_printf(&blkdev->xendev, 1, "ok: proto %s, ring-ref %d, "
  651. "remote port %d, local port %d\n",
  652. blkdev->xendev.protocol, blkdev->ring_ref,
  653. blkdev->xendev.remote_port, blkdev->xendev.local_port);
  654. return 0;
  655. }
  656. static void blk_disconnect(struct XenDevice *xendev)
  657. {
  658. struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
  659. if (blkdev->bs) {
  660. if (!blkdev->dinfo) {
  661. /* close/delete only if we created it ourself */
  662. bdrv_close(blkdev->bs);
  663. bdrv_detach_dev(blkdev->bs, blkdev);
  664. bdrv_delete(blkdev->bs);
  665. }
  666. blkdev->bs = NULL;
  667. }
  668. xen_be_unbind_evtchn(&blkdev->xendev);
  669. if (blkdev->sring) {
  670. xc_gnttab_munmap(blkdev->xendev.gnttabdev, blkdev->sring, 1);
  671. blkdev->cnt_map--;
  672. blkdev->sring = NULL;
  673. }
  674. }
  675. static int blk_free(struct XenDevice *xendev)
  676. {
  677. struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
  678. struct ioreq *ioreq;
  679. if (blkdev->bs || blkdev->sring) {
  680. blk_disconnect(xendev);
  681. }
  682. while (!QLIST_EMPTY(&blkdev->freelist)) {
  683. ioreq = QLIST_FIRST(&blkdev->freelist);
  684. QLIST_REMOVE(ioreq, list);
  685. qemu_iovec_destroy(&ioreq->v);
  686. g_free(ioreq);
  687. }
  688. g_free(blkdev->params);
  689. g_free(blkdev->mode);
  690. g_free(blkdev->type);
  691. g_free(blkdev->dev);
  692. g_free(blkdev->devtype);
  693. qemu_bh_delete(blkdev->bh);
  694. return 0;
  695. }
  696. static void blk_event(struct XenDevice *xendev)
  697. {
  698. struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
  699. qemu_bh_schedule(blkdev->bh);
  700. }
  701. struct XenDevOps xen_blkdev_ops = {
  702. .size = sizeof(struct XenBlkDev),
  703. .flags = DEVOPS_FLAG_NEED_GNTDEV,
  704. .alloc = blk_alloc,
  705. .init = blk_init,
  706. .initialise = blk_connect,
  707. .disconnect = blk_disconnect,
  708. .event = blk_event,
  709. .free = blk_free,
  710. };