xen_disk.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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 "xen_backend.h"
  38. #include "xen_blkif.h"
  39. #include "sysemu/blockdev.h"
  40. /* ------------------------------------------------------------- */
  41. static int batch_maps = 0;
  42. static int max_requests = 32;
  43. /* ------------------------------------------------------------- */
  44. #define BLOCK_SIZE 512
  45. #define IOCB_COUNT (BLKIF_MAX_SEGMENTS_PER_REQUEST + 2)
  46. struct PersistentGrant {
  47. void *page;
  48. struct XenBlkDev *blkdev;
  49. };
  50. typedef struct PersistentGrant PersistentGrant;
  51. struct ioreq {
  52. blkif_request_t req;
  53. int16_t status;
  54. /* parsed request */
  55. off_t start;
  56. QEMUIOVector v;
  57. int presync;
  58. int postsync;
  59. uint8_t mapped;
  60. /* grant mapping */
  61. uint32_t domids[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  62. uint32_t refs[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  63. int prot;
  64. void *page[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  65. void *pages;
  66. int num_unmap;
  67. /* aio status */
  68. int aio_inflight;
  69. int aio_errors;
  70. struct XenBlkDev *blkdev;
  71. QLIST_ENTRY(ioreq) list;
  72. BlockAcctCookie acct;
  73. };
  74. struct XenBlkDev {
  75. struct XenDevice xendev; /* must be first */
  76. char *params;
  77. char *mode;
  78. char *type;
  79. char *dev;
  80. char *devtype;
  81. const char *fileproto;
  82. const char *filename;
  83. int ring_ref;
  84. void *sring;
  85. int64_t file_blk;
  86. int64_t file_size;
  87. int protocol;
  88. blkif_back_rings_t rings;
  89. int more_work;
  90. int cnt_map;
  91. /* request lists */
  92. QLIST_HEAD(inflight_head, ioreq) inflight;
  93. QLIST_HEAD(finished_head, ioreq) finished;
  94. QLIST_HEAD(freelist_head, ioreq) freelist;
  95. int requests_total;
  96. int requests_inflight;
  97. int requests_finished;
  98. /* Persistent grants extension */
  99. gboolean feature_persistent;
  100. GTree *persistent_gnts;
  101. unsigned int persistent_gnt_count;
  102. unsigned int max_grants;
  103. /* qemu block driver */
  104. DriveInfo *dinfo;
  105. BlockDriverState *bs;
  106. QEMUBH *bh;
  107. };
  108. /* ------------------------------------------------------------- */
  109. static void ioreq_reset(struct ioreq *ioreq)
  110. {
  111. memset(&ioreq->req, 0, sizeof(ioreq->req));
  112. ioreq->status = 0;
  113. ioreq->start = 0;
  114. ioreq->presync = 0;
  115. ioreq->postsync = 0;
  116. ioreq->mapped = 0;
  117. memset(ioreq->domids, 0, sizeof(ioreq->domids));
  118. memset(ioreq->refs, 0, sizeof(ioreq->refs));
  119. ioreq->prot = 0;
  120. memset(ioreq->page, 0, sizeof(ioreq->page));
  121. ioreq->pages = NULL;
  122. ioreq->aio_inflight = 0;
  123. ioreq->aio_errors = 0;
  124. ioreq->blkdev = NULL;
  125. memset(&ioreq->list, 0, sizeof(ioreq->list));
  126. memset(&ioreq->acct, 0, sizeof(ioreq->acct));
  127. qemu_iovec_reset(&ioreq->v);
  128. }
  129. static gint int_cmp(gconstpointer a, gconstpointer b, gpointer user_data)
  130. {
  131. uint ua = GPOINTER_TO_UINT(a);
  132. uint ub = GPOINTER_TO_UINT(b);
  133. return (ua > ub) - (ua < ub);
  134. }
  135. static void destroy_grant(gpointer pgnt)
  136. {
  137. PersistentGrant *grant = pgnt;
  138. XenGnttab gnt = grant->blkdev->xendev.gnttabdev;
  139. if (xc_gnttab_munmap(gnt, grant->page, 1) != 0) {
  140. xen_be_printf(&grant->blkdev->xendev, 0,
  141. "xc_gnttab_munmap failed: %s\n",
  142. strerror(errno));
  143. }
  144. grant->blkdev->persistent_gnt_count--;
  145. xen_be_printf(&grant->blkdev->xendev, 3,
  146. "unmapped grant %p\n", grant->page);
  147. g_free(grant);
  148. }
  149. static struct ioreq *ioreq_start(struct XenBlkDev *blkdev)
  150. {
  151. struct ioreq *ioreq = NULL;
  152. if (QLIST_EMPTY(&blkdev->freelist)) {
  153. if (blkdev->requests_total >= max_requests) {
  154. goto out;
  155. }
  156. /* allocate new struct */
  157. ioreq = g_malloc0(sizeof(*ioreq));
  158. ioreq->blkdev = blkdev;
  159. blkdev->requests_total++;
  160. qemu_iovec_init(&ioreq->v, BLKIF_MAX_SEGMENTS_PER_REQUEST);
  161. } else {
  162. /* get one from freelist */
  163. ioreq = QLIST_FIRST(&blkdev->freelist);
  164. QLIST_REMOVE(ioreq, list);
  165. }
  166. QLIST_INSERT_HEAD(&blkdev->inflight, ioreq, list);
  167. blkdev->requests_inflight++;
  168. out:
  169. return ioreq;
  170. }
  171. static void ioreq_finish(struct ioreq *ioreq)
  172. {
  173. struct XenBlkDev *blkdev = ioreq->blkdev;
  174. QLIST_REMOVE(ioreq, list);
  175. QLIST_INSERT_HEAD(&blkdev->finished, ioreq, list);
  176. blkdev->requests_inflight--;
  177. blkdev->requests_finished++;
  178. }
  179. static void ioreq_release(struct ioreq *ioreq, bool finish)
  180. {
  181. struct XenBlkDev *blkdev = ioreq->blkdev;
  182. QLIST_REMOVE(ioreq, list);
  183. ioreq_reset(ioreq);
  184. ioreq->blkdev = blkdev;
  185. QLIST_INSERT_HEAD(&blkdev->freelist, ioreq, list);
  186. if (finish) {
  187. blkdev->requests_finished--;
  188. } else {
  189. blkdev->requests_inflight--;
  190. }
  191. }
  192. /*
  193. * translate request into iovec + start offset
  194. * do sanity checks along the way
  195. */
  196. static int ioreq_parse(struct ioreq *ioreq)
  197. {
  198. struct XenBlkDev *blkdev = ioreq->blkdev;
  199. uintptr_t mem;
  200. size_t len;
  201. int i;
  202. xen_be_printf(&blkdev->xendev, 3,
  203. "op %d, nr %d, handle %d, id %" PRId64 ", sector %" PRId64 "\n",
  204. ioreq->req.operation, ioreq->req.nr_segments,
  205. ioreq->req.handle, ioreq->req.id, ioreq->req.sector_number);
  206. switch (ioreq->req.operation) {
  207. case BLKIF_OP_READ:
  208. ioreq->prot = PROT_WRITE; /* to memory */
  209. break;
  210. case BLKIF_OP_FLUSH_DISKCACHE:
  211. ioreq->presync = 1;
  212. if (!ioreq->req.nr_segments) {
  213. return 0;
  214. }
  215. /* fall through */
  216. case BLKIF_OP_WRITE:
  217. ioreq->prot = PROT_READ; /* from memory */
  218. break;
  219. default:
  220. xen_be_printf(&blkdev->xendev, 0, "error: unknown operation (%d)\n",
  221. ioreq->req.operation);
  222. goto err;
  223. };
  224. if (ioreq->req.operation != BLKIF_OP_READ && blkdev->mode[0] != 'w') {
  225. xen_be_printf(&blkdev->xendev, 0, "error: write req for ro device\n");
  226. goto err;
  227. }
  228. ioreq->start = ioreq->req.sector_number * blkdev->file_blk;
  229. for (i = 0; i < ioreq->req.nr_segments; i++) {
  230. if (i == BLKIF_MAX_SEGMENTS_PER_REQUEST) {
  231. xen_be_printf(&blkdev->xendev, 0, "error: nr_segments too big\n");
  232. goto err;
  233. }
  234. if (ioreq->req.seg[i].first_sect > ioreq->req.seg[i].last_sect) {
  235. xen_be_printf(&blkdev->xendev, 0, "error: first > last sector\n");
  236. goto err;
  237. }
  238. if (ioreq->req.seg[i].last_sect * BLOCK_SIZE >= XC_PAGE_SIZE) {
  239. xen_be_printf(&blkdev->xendev, 0, "error: page crossing\n");
  240. goto err;
  241. }
  242. ioreq->domids[i] = blkdev->xendev.dom;
  243. ioreq->refs[i] = ioreq->req.seg[i].gref;
  244. mem = ioreq->req.seg[i].first_sect * blkdev->file_blk;
  245. len = (ioreq->req.seg[i].last_sect - ioreq->req.seg[i].first_sect + 1) * blkdev->file_blk;
  246. qemu_iovec_add(&ioreq->v, (void*)mem, len);
  247. }
  248. if (ioreq->start + ioreq->v.size > blkdev->file_size) {
  249. xen_be_printf(&blkdev->xendev, 0, "error: access beyond end of file\n");
  250. goto err;
  251. }
  252. return 0;
  253. err:
  254. ioreq->status = BLKIF_RSP_ERROR;
  255. return -1;
  256. }
  257. static void ioreq_unmap(struct ioreq *ioreq)
  258. {
  259. XenGnttab gnt = ioreq->blkdev->xendev.gnttabdev;
  260. int i;
  261. if (ioreq->num_unmap == 0 || ioreq->mapped == 0) {
  262. return;
  263. }
  264. if (batch_maps) {
  265. if (!ioreq->pages) {
  266. return;
  267. }
  268. if (xc_gnttab_munmap(gnt, ioreq->pages, ioreq->num_unmap) != 0) {
  269. xen_be_printf(&ioreq->blkdev->xendev, 0, "xc_gnttab_munmap failed: %s\n",
  270. strerror(errno));
  271. }
  272. ioreq->blkdev->cnt_map -= ioreq->num_unmap;
  273. ioreq->pages = NULL;
  274. } else {
  275. for (i = 0; i < ioreq->num_unmap; i++) {
  276. if (!ioreq->page[i]) {
  277. continue;
  278. }
  279. if (xc_gnttab_munmap(gnt, ioreq->page[i], 1) != 0) {
  280. xen_be_printf(&ioreq->blkdev->xendev, 0, "xc_gnttab_munmap failed: %s\n",
  281. strerror(errno));
  282. }
  283. ioreq->blkdev->cnt_map--;
  284. ioreq->page[i] = NULL;
  285. }
  286. }
  287. ioreq->mapped = 0;
  288. }
  289. static int ioreq_map(struct ioreq *ioreq)
  290. {
  291. XenGnttab gnt = ioreq->blkdev->xendev.gnttabdev;
  292. uint32_t domids[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  293. uint32_t refs[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  294. void *page[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  295. int i, j, new_maps = 0;
  296. PersistentGrant *grant;
  297. /* domids and refs variables will contain the information necessary
  298. * to map the grants that are needed to fulfill this request.
  299. *
  300. * After mapping the needed grants, the page array will contain the
  301. * memory address of each granted page in the order specified in ioreq
  302. * (disregarding if it's a persistent grant or not).
  303. */
  304. if (ioreq->v.niov == 0 || ioreq->mapped == 1) {
  305. return 0;
  306. }
  307. if (ioreq->blkdev->feature_persistent) {
  308. for (i = 0; i < ioreq->v.niov; i++) {
  309. grant = g_tree_lookup(ioreq->blkdev->persistent_gnts,
  310. GUINT_TO_POINTER(ioreq->refs[i]));
  311. if (grant != NULL) {
  312. page[i] = grant->page;
  313. xen_be_printf(&ioreq->blkdev->xendev, 3,
  314. "using persistent-grant %" PRIu32 "\n",
  315. ioreq->refs[i]);
  316. } else {
  317. /* Add the grant to the list of grants that
  318. * should be mapped
  319. */
  320. domids[new_maps] = ioreq->domids[i];
  321. refs[new_maps] = ioreq->refs[i];
  322. page[i] = NULL;
  323. new_maps++;
  324. }
  325. }
  326. /* Set the protection to RW, since grants may be reused later
  327. * with a different protection than the one needed for this request
  328. */
  329. ioreq->prot = PROT_WRITE | PROT_READ;
  330. } else {
  331. /* All grants in the request should be mapped */
  332. memcpy(refs, ioreq->refs, sizeof(refs));
  333. memcpy(domids, ioreq->domids, sizeof(domids));
  334. memset(page, 0, sizeof(page));
  335. new_maps = ioreq->v.niov;
  336. }
  337. if (batch_maps && new_maps) {
  338. ioreq->pages = xc_gnttab_map_grant_refs
  339. (gnt, new_maps, domids, refs, ioreq->prot);
  340. if (ioreq->pages == NULL) {
  341. xen_be_printf(&ioreq->blkdev->xendev, 0,
  342. "can't map %d grant refs (%s, %d maps)\n",
  343. new_maps, strerror(errno), ioreq->blkdev->cnt_map);
  344. return -1;
  345. }
  346. for (i = 0, j = 0; i < ioreq->v.niov; i++) {
  347. if (page[i] == NULL) {
  348. page[i] = ioreq->pages + (j++) * XC_PAGE_SIZE;
  349. }
  350. }
  351. ioreq->blkdev->cnt_map += new_maps;
  352. } else if (new_maps) {
  353. for (i = 0; i < new_maps; i++) {
  354. ioreq->page[i] = xc_gnttab_map_grant_ref
  355. (gnt, domids[i], refs[i], ioreq->prot);
  356. if (ioreq->page[i] == NULL) {
  357. xen_be_printf(&ioreq->blkdev->xendev, 0,
  358. "can't map grant ref %d (%s, %d maps)\n",
  359. refs[i], strerror(errno), ioreq->blkdev->cnt_map);
  360. ioreq_unmap(ioreq);
  361. return -1;
  362. }
  363. ioreq->blkdev->cnt_map++;
  364. }
  365. for (i = 0, j = 0; i < ioreq->v.niov; i++) {
  366. if (page[i] == NULL) {
  367. page[i] = ioreq->page[j++];
  368. }
  369. }
  370. }
  371. if (ioreq->blkdev->feature_persistent) {
  372. while ((ioreq->blkdev->persistent_gnt_count < ioreq->blkdev->max_grants)
  373. && new_maps) {
  374. /* Go through the list of newly mapped grants and add as many
  375. * as possible to the list of persistently mapped grants.
  376. *
  377. * Since we start at the end of ioreq->page(s), we only need
  378. * to decrease new_maps to prevent this granted pages from
  379. * being unmapped in ioreq_unmap.
  380. */
  381. grant = g_malloc0(sizeof(*grant));
  382. new_maps--;
  383. if (batch_maps) {
  384. grant->page = ioreq->pages + (new_maps) * XC_PAGE_SIZE;
  385. } else {
  386. grant->page = ioreq->page[new_maps];
  387. }
  388. grant->blkdev = ioreq->blkdev;
  389. xen_be_printf(&ioreq->blkdev->xendev, 3,
  390. "adding grant %" PRIu32 " page: %p\n",
  391. refs[new_maps], grant->page);
  392. g_tree_insert(ioreq->blkdev->persistent_gnts,
  393. GUINT_TO_POINTER(refs[new_maps]),
  394. grant);
  395. ioreq->blkdev->persistent_gnt_count++;
  396. }
  397. }
  398. for (i = 0; i < ioreq->v.niov; i++) {
  399. ioreq->v.iov[i].iov_base += (uintptr_t)page[i];
  400. }
  401. ioreq->mapped = 1;
  402. ioreq->num_unmap = new_maps;
  403. return 0;
  404. }
  405. static int ioreq_runio_qemu_aio(struct ioreq *ioreq);
  406. static void qemu_aio_complete(void *opaque, int ret)
  407. {
  408. struct ioreq *ioreq = opaque;
  409. if (ret != 0) {
  410. xen_be_printf(&ioreq->blkdev->xendev, 0, "%s I/O error\n",
  411. ioreq->req.operation == BLKIF_OP_READ ? "read" : "write");
  412. ioreq->aio_errors++;
  413. }
  414. ioreq->aio_inflight--;
  415. if (ioreq->presync) {
  416. ioreq->presync = 0;
  417. ioreq_runio_qemu_aio(ioreq);
  418. return;
  419. }
  420. if (ioreq->aio_inflight > 0) {
  421. return;
  422. }
  423. if (ioreq->postsync) {
  424. ioreq->postsync = 0;
  425. ioreq->aio_inflight++;
  426. bdrv_aio_flush(ioreq->blkdev->bs, qemu_aio_complete, ioreq);
  427. return;
  428. }
  429. ioreq->status = ioreq->aio_errors ? BLKIF_RSP_ERROR : BLKIF_RSP_OKAY;
  430. ioreq_unmap(ioreq);
  431. ioreq_finish(ioreq);
  432. bdrv_acct_done(ioreq->blkdev->bs, &ioreq->acct);
  433. qemu_bh_schedule(ioreq->blkdev->bh);
  434. }
  435. static int ioreq_runio_qemu_aio(struct ioreq *ioreq)
  436. {
  437. struct XenBlkDev *blkdev = ioreq->blkdev;
  438. if (ioreq->req.nr_segments && ioreq_map(ioreq) == -1) {
  439. goto err_no_map;
  440. }
  441. ioreq->aio_inflight++;
  442. if (ioreq->presync) {
  443. bdrv_aio_flush(ioreq->blkdev->bs, qemu_aio_complete, ioreq);
  444. return 0;
  445. }
  446. switch (ioreq->req.operation) {
  447. case BLKIF_OP_READ:
  448. bdrv_acct_start(blkdev->bs, &ioreq->acct, ioreq->v.size, BDRV_ACCT_READ);
  449. ioreq->aio_inflight++;
  450. bdrv_aio_readv(blkdev->bs, ioreq->start / BLOCK_SIZE,
  451. &ioreq->v, ioreq->v.size / BLOCK_SIZE,
  452. qemu_aio_complete, ioreq);
  453. break;
  454. case BLKIF_OP_WRITE:
  455. case BLKIF_OP_FLUSH_DISKCACHE:
  456. if (!ioreq->req.nr_segments) {
  457. break;
  458. }
  459. bdrv_acct_start(blkdev->bs, &ioreq->acct, ioreq->v.size, BDRV_ACCT_WRITE);
  460. ioreq->aio_inflight++;
  461. bdrv_aio_writev(blkdev->bs, ioreq->start / BLOCK_SIZE,
  462. &ioreq->v, ioreq->v.size / BLOCK_SIZE,
  463. qemu_aio_complete, ioreq);
  464. break;
  465. default:
  466. /* unknown operation (shouldn't happen -- parse catches this) */
  467. goto err;
  468. }
  469. qemu_aio_complete(ioreq, 0);
  470. return 0;
  471. err:
  472. ioreq_unmap(ioreq);
  473. err_no_map:
  474. ioreq_finish(ioreq);
  475. ioreq->status = BLKIF_RSP_ERROR;
  476. return -1;
  477. }
  478. static int blk_send_response_one(struct ioreq *ioreq)
  479. {
  480. struct XenBlkDev *blkdev = ioreq->blkdev;
  481. int send_notify = 0;
  482. int have_requests = 0;
  483. blkif_response_t resp;
  484. void *dst;
  485. resp.id = ioreq->req.id;
  486. resp.operation = ioreq->req.operation;
  487. resp.status = ioreq->status;
  488. /* Place on the response ring for the relevant domain. */
  489. switch (blkdev->protocol) {
  490. case BLKIF_PROTOCOL_NATIVE:
  491. dst = RING_GET_RESPONSE(&blkdev->rings.native, blkdev->rings.native.rsp_prod_pvt);
  492. break;
  493. case BLKIF_PROTOCOL_X86_32:
  494. dst = RING_GET_RESPONSE(&blkdev->rings.x86_32_part,
  495. blkdev->rings.x86_32_part.rsp_prod_pvt);
  496. break;
  497. case BLKIF_PROTOCOL_X86_64:
  498. dst = RING_GET_RESPONSE(&blkdev->rings.x86_64_part,
  499. blkdev->rings.x86_64_part.rsp_prod_pvt);
  500. break;
  501. default:
  502. dst = NULL;
  503. }
  504. memcpy(dst, &resp, sizeof(resp));
  505. blkdev->rings.common.rsp_prod_pvt++;
  506. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blkdev->rings.common, send_notify);
  507. if (blkdev->rings.common.rsp_prod_pvt == blkdev->rings.common.req_cons) {
  508. /*
  509. * Tail check for pending requests. Allows frontend to avoid
  510. * notifications if requests are already in flight (lower
  511. * overheads and promotes batching).
  512. */
  513. RING_FINAL_CHECK_FOR_REQUESTS(&blkdev->rings.common, have_requests);
  514. } else if (RING_HAS_UNCONSUMED_REQUESTS(&blkdev->rings.common)) {
  515. have_requests = 1;
  516. }
  517. if (have_requests) {
  518. blkdev->more_work++;
  519. }
  520. return send_notify;
  521. }
  522. /* walk finished list, send outstanding responses, free requests */
  523. static void blk_send_response_all(struct XenBlkDev *blkdev)
  524. {
  525. struct ioreq *ioreq;
  526. int send_notify = 0;
  527. while (!QLIST_EMPTY(&blkdev->finished)) {
  528. ioreq = QLIST_FIRST(&blkdev->finished);
  529. send_notify += blk_send_response_one(ioreq);
  530. ioreq_release(ioreq, true);
  531. }
  532. if (send_notify) {
  533. xen_be_send_notify(&blkdev->xendev);
  534. }
  535. }
  536. static int blk_get_request(struct XenBlkDev *blkdev, struct ioreq *ioreq, RING_IDX rc)
  537. {
  538. switch (blkdev->protocol) {
  539. case BLKIF_PROTOCOL_NATIVE:
  540. memcpy(&ioreq->req, RING_GET_REQUEST(&blkdev->rings.native, rc),
  541. sizeof(ioreq->req));
  542. break;
  543. case BLKIF_PROTOCOL_X86_32:
  544. blkif_get_x86_32_req(&ioreq->req,
  545. RING_GET_REQUEST(&blkdev->rings.x86_32_part, rc));
  546. break;
  547. case BLKIF_PROTOCOL_X86_64:
  548. blkif_get_x86_64_req(&ioreq->req,
  549. RING_GET_REQUEST(&blkdev->rings.x86_64_part, rc));
  550. break;
  551. }
  552. return 0;
  553. }
  554. static void blk_handle_requests(struct XenBlkDev *blkdev)
  555. {
  556. RING_IDX rc, rp;
  557. struct ioreq *ioreq;
  558. blkdev->more_work = 0;
  559. rc = blkdev->rings.common.req_cons;
  560. rp = blkdev->rings.common.sring->req_prod;
  561. xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
  562. blk_send_response_all(blkdev);
  563. while (rc != rp) {
  564. /* pull request from ring */
  565. if (RING_REQUEST_CONS_OVERFLOW(&blkdev->rings.common, rc)) {
  566. break;
  567. }
  568. ioreq = ioreq_start(blkdev);
  569. if (ioreq == NULL) {
  570. blkdev->more_work++;
  571. break;
  572. }
  573. blk_get_request(blkdev, ioreq, rc);
  574. blkdev->rings.common.req_cons = ++rc;
  575. /* parse them */
  576. if (ioreq_parse(ioreq) != 0) {
  577. if (blk_send_response_one(ioreq)) {
  578. xen_be_send_notify(&blkdev->xendev);
  579. }
  580. ioreq_release(ioreq, false);
  581. continue;
  582. }
  583. ioreq_runio_qemu_aio(ioreq);
  584. }
  585. if (blkdev->more_work && blkdev->requests_inflight < max_requests) {
  586. qemu_bh_schedule(blkdev->bh);
  587. }
  588. }
  589. /* ------------------------------------------------------------- */
  590. static void blk_bh(void *opaque)
  591. {
  592. struct XenBlkDev *blkdev = opaque;
  593. blk_handle_requests(blkdev);
  594. }
  595. /*
  596. * We need to account for the grant allocations requiring contiguous
  597. * chunks; the worst case number would be
  598. * max_req * max_seg + (max_req - 1) * (max_seg - 1) + 1,
  599. * but in order to keep things simple just use
  600. * 2 * max_req * max_seg.
  601. */
  602. #define MAX_GRANTS(max_req, max_seg) (2 * (max_req) * (max_seg))
  603. static void blk_alloc(struct XenDevice *xendev)
  604. {
  605. struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
  606. QLIST_INIT(&blkdev->inflight);
  607. QLIST_INIT(&blkdev->finished);
  608. QLIST_INIT(&blkdev->freelist);
  609. blkdev->bh = qemu_bh_new(blk_bh, blkdev);
  610. if (xen_mode != XEN_EMULATE) {
  611. batch_maps = 1;
  612. }
  613. if (xc_gnttab_set_max_grants(xendev->gnttabdev,
  614. MAX_GRANTS(max_requests, BLKIF_MAX_SEGMENTS_PER_REQUEST)) < 0) {
  615. xen_be_printf(xendev, 0, "xc_gnttab_set_max_grants failed: %s\n",
  616. strerror(errno));
  617. }
  618. }
  619. static int blk_init(struct XenDevice *xendev)
  620. {
  621. struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
  622. int index, qflags, info = 0;
  623. /* read xenstore entries */
  624. if (blkdev->params == NULL) {
  625. char *h = NULL;
  626. blkdev->params = xenstore_read_be_str(&blkdev->xendev, "params");
  627. if (blkdev->params != NULL) {
  628. h = strchr(blkdev->params, ':');
  629. }
  630. if (h != NULL) {
  631. blkdev->fileproto = blkdev->params;
  632. blkdev->filename = h+1;
  633. *h = 0;
  634. } else {
  635. blkdev->fileproto = "<unset>";
  636. blkdev->filename = blkdev->params;
  637. }
  638. }
  639. if (!strcmp("aio", blkdev->fileproto)) {
  640. blkdev->fileproto = "raw";
  641. }
  642. if (blkdev->mode == NULL) {
  643. blkdev->mode = xenstore_read_be_str(&blkdev->xendev, "mode");
  644. }
  645. if (blkdev->type == NULL) {
  646. blkdev->type = xenstore_read_be_str(&blkdev->xendev, "type");
  647. }
  648. if (blkdev->dev == NULL) {
  649. blkdev->dev = xenstore_read_be_str(&blkdev->xendev, "dev");
  650. }
  651. if (blkdev->devtype == NULL) {
  652. blkdev->devtype = xenstore_read_be_str(&blkdev->xendev, "device-type");
  653. }
  654. /* do we have all we need? */
  655. if (blkdev->params == NULL ||
  656. blkdev->mode == NULL ||
  657. blkdev->type == NULL ||
  658. blkdev->dev == NULL) {
  659. goto out_error;
  660. }
  661. /* read-only ? */
  662. qflags = BDRV_O_NOCACHE | BDRV_O_CACHE_WB | BDRV_O_NATIVE_AIO;
  663. if (strcmp(blkdev->mode, "w") == 0) {
  664. qflags |= BDRV_O_RDWR;
  665. } else {
  666. info |= VDISK_READONLY;
  667. }
  668. /* cdrom ? */
  669. if (blkdev->devtype && !strcmp(blkdev->devtype, "cdrom")) {
  670. info |= VDISK_CDROM;
  671. }
  672. /* init qemu block driver */
  673. index = (blkdev->xendev.dev - 202 * 256) / 16;
  674. blkdev->dinfo = drive_get(IF_XEN, 0, index);
  675. if (!blkdev->dinfo) {
  676. /* setup via xenbus -> create new block driver instance */
  677. xen_be_printf(&blkdev->xendev, 2, "create new bdrv (xenbus setup)\n");
  678. blkdev->bs = bdrv_new(blkdev->dev);
  679. if (blkdev->bs) {
  680. if (bdrv_open(blkdev->bs, blkdev->filename, qflags,
  681. bdrv_find_whitelisted_format(blkdev->fileproto)) != 0) {
  682. bdrv_delete(blkdev->bs);
  683. blkdev->bs = NULL;
  684. }
  685. }
  686. if (!blkdev->bs) {
  687. goto out_error;
  688. }
  689. } else {
  690. /* setup via qemu cmdline -> already setup for us */
  691. xen_be_printf(&blkdev->xendev, 2, "get configured bdrv (cmdline setup)\n");
  692. blkdev->bs = blkdev->dinfo->bdrv;
  693. }
  694. bdrv_attach_dev_nofail(blkdev->bs, blkdev);
  695. blkdev->file_blk = BLOCK_SIZE;
  696. blkdev->file_size = bdrv_getlength(blkdev->bs);
  697. if (blkdev->file_size < 0) {
  698. xen_be_printf(&blkdev->xendev, 1, "bdrv_getlength: %d (%s) | drv %s\n",
  699. (int)blkdev->file_size, strerror(-blkdev->file_size),
  700. bdrv_get_format_name(blkdev->bs) ?: "-");
  701. blkdev->file_size = 0;
  702. }
  703. xen_be_printf(xendev, 1, "type \"%s\", fileproto \"%s\", filename \"%s\","
  704. " size %" PRId64 " (%" PRId64 " MB)\n",
  705. blkdev->type, blkdev->fileproto, blkdev->filename,
  706. blkdev->file_size, blkdev->file_size >> 20);
  707. /* fill info */
  708. xenstore_write_be_int(&blkdev->xendev, "feature-flush-cache", 1);
  709. xenstore_write_be_int(&blkdev->xendev, "feature-persistent", 1);
  710. xenstore_write_be_int(&blkdev->xendev, "info", info);
  711. xenstore_write_be_int(&blkdev->xendev, "sector-size", blkdev->file_blk);
  712. xenstore_write_be_int(&blkdev->xendev, "sectors",
  713. blkdev->file_size / blkdev->file_blk);
  714. return 0;
  715. out_error:
  716. g_free(blkdev->params);
  717. blkdev->params = NULL;
  718. g_free(blkdev->mode);
  719. blkdev->mode = NULL;
  720. g_free(blkdev->type);
  721. blkdev->type = NULL;
  722. g_free(blkdev->dev);
  723. blkdev->dev = NULL;
  724. g_free(blkdev->devtype);
  725. blkdev->devtype = NULL;
  726. return -1;
  727. }
  728. static int blk_connect(struct XenDevice *xendev)
  729. {
  730. struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
  731. int pers;
  732. if (xenstore_read_fe_int(&blkdev->xendev, "ring-ref", &blkdev->ring_ref) == -1) {
  733. return -1;
  734. }
  735. if (xenstore_read_fe_int(&blkdev->xendev, "event-channel",
  736. &blkdev->xendev.remote_port) == -1) {
  737. return -1;
  738. }
  739. if (xenstore_read_fe_int(&blkdev->xendev, "feature-persistent", &pers)) {
  740. blkdev->feature_persistent = FALSE;
  741. } else {
  742. blkdev->feature_persistent = !!pers;
  743. }
  744. blkdev->protocol = BLKIF_PROTOCOL_NATIVE;
  745. if (blkdev->xendev.protocol) {
  746. if (strcmp(blkdev->xendev.protocol, XEN_IO_PROTO_ABI_X86_32) == 0) {
  747. blkdev->protocol = BLKIF_PROTOCOL_X86_32;
  748. }
  749. if (strcmp(blkdev->xendev.protocol, XEN_IO_PROTO_ABI_X86_64) == 0) {
  750. blkdev->protocol = BLKIF_PROTOCOL_X86_64;
  751. }
  752. }
  753. blkdev->sring = xc_gnttab_map_grant_ref(blkdev->xendev.gnttabdev,
  754. blkdev->xendev.dom,
  755. blkdev->ring_ref,
  756. PROT_READ | PROT_WRITE);
  757. if (!blkdev->sring) {
  758. return -1;
  759. }
  760. blkdev->cnt_map++;
  761. switch (blkdev->protocol) {
  762. case BLKIF_PROTOCOL_NATIVE:
  763. {
  764. blkif_sring_t *sring_native = blkdev->sring;
  765. BACK_RING_INIT(&blkdev->rings.native, sring_native, XC_PAGE_SIZE);
  766. break;
  767. }
  768. case BLKIF_PROTOCOL_X86_32:
  769. {
  770. blkif_x86_32_sring_t *sring_x86_32 = blkdev->sring;
  771. BACK_RING_INIT(&blkdev->rings.x86_32_part, sring_x86_32, XC_PAGE_SIZE);
  772. break;
  773. }
  774. case BLKIF_PROTOCOL_X86_64:
  775. {
  776. blkif_x86_64_sring_t *sring_x86_64 = blkdev->sring;
  777. BACK_RING_INIT(&blkdev->rings.x86_64_part, sring_x86_64, XC_PAGE_SIZE);
  778. break;
  779. }
  780. }
  781. if (blkdev->feature_persistent) {
  782. /* Init persistent grants */
  783. blkdev->max_grants = max_requests * BLKIF_MAX_SEGMENTS_PER_REQUEST;
  784. blkdev->persistent_gnts = g_tree_new_full((GCompareDataFunc)int_cmp,
  785. NULL, NULL,
  786. (GDestroyNotify)destroy_grant);
  787. blkdev->persistent_gnt_count = 0;
  788. }
  789. xen_be_bind_evtchn(&blkdev->xendev);
  790. xen_be_printf(&blkdev->xendev, 1, "ok: proto %s, ring-ref %d, "
  791. "remote port %d, local port %d\n",
  792. blkdev->xendev.protocol, blkdev->ring_ref,
  793. blkdev->xendev.remote_port, blkdev->xendev.local_port);
  794. return 0;
  795. }
  796. static void blk_disconnect(struct XenDevice *xendev)
  797. {
  798. struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
  799. if (blkdev->bs) {
  800. if (!blkdev->dinfo) {
  801. /* close/delete only if we created it ourself */
  802. bdrv_close(blkdev->bs);
  803. bdrv_detach_dev(blkdev->bs, blkdev);
  804. bdrv_delete(blkdev->bs);
  805. }
  806. blkdev->bs = NULL;
  807. }
  808. xen_be_unbind_evtchn(&blkdev->xendev);
  809. if (blkdev->sring) {
  810. xc_gnttab_munmap(blkdev->xendev.gnttabdev, blkdev->sring, 1);
  811. blkdev->cnt_map--;
  812. blkdev->sring = NULL;
  813. }
  814. }
  815. static int blk_free(struct XenDevice *xendev)
  816. {
  817. struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
  818. struct ioreq *ioreq;
  819. if (blkdev->bs || blkdev->sring) {
  820. blk_disconnect(xendev);
  821. }
  822. /* Free persistent grants */
  823. if (blkdev->feature_persistent) {
  824. g_tree_destroy(blkdev->persistent_gnts);
  825. }
  826. while (!QLIST_EMPTY(&blkdev->freelist)) {
  827. ioreq = QLIST_FIRST(&blkdev->freelist);
  828. QLIST_REMOVE(ioreq, list);
  829. qemu_iovec_destroy(&ioreq->v);
  830. g_free(ioreq);
  831. }
  832. g_free(blkdev->params);
  833. g_free(blkdev->mode);
  834. g_free(blkdev->type);
  835. g_free(blkdev->dev);
  836. g_free(blkdev->devtype);
  837. qemu_bh_delete(blkdev->bh);
  838. return 0;
  839. }
  840. static void blk_event(struct XenDevice *xendev)
  841. {
  842. struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
  843. qemu_bh_schedule(blkdev->bh);
  844. }
  845. struct XenDevOps xen_blkdev_ops = {
  846. .size = sizeof(struct XenBlkDev),
  847. .flags = DEVOPS_FLAG_NEED_GNTDEV,
  848. .alloc = blk_alloc,
  849. .init = blk_init,
  850. .initialise = blk_connect,
  851. .disconnect = blk_disconnect,
  852. .event = blk_event,
  853. .free = blk_free,
  854. };