common.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License version 2
  4. * as published by the Free Software Foundation; or, when distributed
  5. * separately from the Linux kernel or incorporated into other
  6. * software packages, subject to the following license:
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this source file (the "Software"), to deal in the Software without
  10. * restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  12. * and to permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  24. * IN THE SOFTWARE.
  25. */
  26. #ifndef __XEN_BLKIF__BACKEND__COMMON_H__
  27. #define __XEN_BLKIF__BACKEND__COMMON_H__
  28. #include <linux/module.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/slab.h>
  31. #include <linux/blkdev.h>
  32. #include <linux/vmalloc.h>
  33. #include <linux/wait.h>
  34. #include <linux/io.h>
  35. #include <linux/rbtree.h>
  36. #include <asm/setup.h>
  37. #include <asm/pgalloc.h>
  38. #include <asm/hypervisor.h>
  39. #include <xen/grant_table.h>
  40. #include <xen/xenbus.h>
  41. #include <xen/interface/io/ring.h>
  42. #include <xen/interface/io/blkif.h>
  43. #include <xen/interface/io/protocols.h>
  44. #define DRV_PFX "xen-blkback:"
  45. #define DPRINTK(fmt, args...) \
  46. pr_debug(DRV_PFX "(%s:%d) " fmt ".\n", \
  47. __func__, __LINE__, ##args)
  48. /*
  49. * This is the maximum number of segments that would be allowed in indirect
  50. * requests. This value will also be passed to the frontend.
  51. */
  52. #define MAX_INDIRECT_SEGMENTS 256
  53. #define SEGS_PER_INDIRECT_FRAME \
  54. (PAGE_SIZE/sizeof(struct blkif_request_segment))
  55. #define MAX_INDIRECT_PAGES \
  56. ((MAX_INDIRECT_SEGMENTS + SEGS_PER_INDIRECT_FRAME - 1)/SEGS_PER_INDIRECT_FRAME)
  57. #define INDIRECT_PAGES(_segs) \
  58. ((_segs + SEGS_PER_INDIRECT_FRAME - 1)/SEGS_PER_INDIRECT_FRAME)
  59. /* Not a real protocol. Used to generate ring structs which contain
  60. * the elements common to all protocols only. This way we get a
  61. * compiler-checkable way to use common struct elements, so we can
  62. * avoid using switch(protocol) in a number of places. */
  63. struct blkif_common_request {
  64. char dummy;
  65. };
  66. struct blkif_common_response {
  67. char dummy;
  68. };
  69. struct blkif_x86_32_request_rw {
  70. uint8_t nr_segments; /* number of segments */
  71. blkif_vdev_t handle; /* only for read/write requests */
  72. uint64_t id; /* private guest value, echoed in resp */
  73. blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
  74. struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  75. } __attribute__((__packed__));
  76. struct blkif_x86_32_request_discard {
  77. uint8_t flag; /* BLKIF_DISCARD_SECURE or zero */
  78. blkif_vdev_t _pad1; /* was "handle" for read/write requests */
  79. uint64_t id; /* private guest value, echoed in resp */
  80. blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
  81. uint64_t nr_sectors;
  82. } __attribute__((__packed__));
  83. struct blkif_x86_32_request_other {
  84. uint8_t _pad1;
  85. blkif_vdev_t _pad2;
  86. uint64_t id; /* private guest value, echoed in resp */
  87. } __attribute__((__packed__));
  88. struct blkif_x86_32_request_indirect {
  89. uint8_t indirect_op;
  90. uint16_t nr_segments;
  91. uint64_t id;
  92. blkif_sector_t sector_number;
  93. blkif_vdev_t handle;
  94. uint16_t _pad1;
  95. grant_ref_t indirect_grefs[BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST];
  96. /*
  97. * The maximum number of indirect segments (and pages) that will
  98. * be used is determined by MAX_INDIRECT_SEGMENTS, this value
  99. * is also exported to the guest (via xenstore
  100. * feature-max-indirect-segments entry), so the frontend knows how
  101. * many indirect segments the backend supports.
  102. */
  103. uint64_t _pad2; /* make it 64 byte aligned */
  104. } __attribute__((__packed__));
  105. struct blkif_x86_32_request {
  106. uint8_t operation; /* BLKIF_OP_??? */
  107. union {
  108. struct blkif_x86_32_request_rw rw;
  109. struct blkif_x86_32_request_discard discard;
  110. struct blkif_x86_32_request_other other;
  111. struct blkif_x86_32_request_indirect indirect;
  112. } u;
  113. } __attribute__((__packed__));
  114. /* i386 protocol version */
  115. #pragma pack(push, 4)
  116. struct blkif_x86_32_response {
  117. uint64_t id; /* copied from request */
  118. uint8_t operation; /* copied from request */
  119. int16_t status; /* BLKIF_RSP_??? */
  120. };
  121. #pragma pack(pop)
  122. /* x86_64 protocol version */
  123. struct blkif_x86_64_request_rw {
  124. uint8_t nr_segments; /* number of segments */
  125. blkif_vdev_t handle; /* only for read/write requests */
  126. uint32_t _pad1; /* offsetof(blkif_reqest..,u.rw.id)==8 */
  127. uint64_t id;
  128. blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
  129. struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  130. } __attribute__((__packed__));
  131. struct blkif_x86_64_request_discard {
  132. uint8_t flag; /* BLKIF_DISCARD_SECURE or zero */
  133. blkif_vdev_t _pad1; /* was "handle" for read/write requests */
  134. uint32_t _pad2; /* offsetof(blkif_..,u.discard.id)==8 */
  135. uint64_t id;
  136. blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
  137. uint64_t nr_sectors;
  138. } __attribute__((__packed__));
  139. struct blkif_x86_64_request_other {
  140. uint8_t _pad1;
  141. blkif_vdev_t _pad2;
  142. uint32_t _pad3; /* offsetof(blkif_..,u.discard.id)==8 */
  143. uint64_t id; /* private guest value, echoed in resp */
  144. } __attribute__((__packed__));
  145. struct blkif_x86_64_request_indirect {
  146. uint8_t indirect_op;
  147. uint16_t nr_segments;
  148. uint32_t _pad1; /* offsetof(blkif_..,u.indirect.id)==8 */
  149. uint64_t id;
  150. blkif_sector_t sector_number;
  151. blkif_vdev_t handle;
  152. uint16_t _pad2;
  153. grant_ref_t indirect_grefs[BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST];
  154. /*
  155. * The maximum number of indirect segments (and pages) that will
  156. * be used is determined by MAX_INDIRECT_SEGMENTS, this value
  157. * is also exported to the guest (via xenstore
  158. * feature-max-indirect-segments entry), so the frontend knows how
  159. * many indirect segments the backend supports.
  160. */
  161. uint32_t _pad3; /* make it 64 byte aligned */
  162. } __attribute__((__packed__));
  163. struct blkif_x86_64_request {
  164. uint8_t operation; /* BLKIF_OP_??? */
  165. union {
  166. struct blkif_x86_64_request_rw rw;
  167. struct blkif_x86_64_request_discard discard;
  168. struct blkif_x86_64_request_other other;
  169. struct blkif_x86_64_request_indirect indirect;
  170. } u;
  171. } __attribute__((__packed__));
  172. struct blkif_x86_64_response {
  173. uint64_t __attribute__((__aligned__(8))) id;
  174. uint8_t operation; /* copied from request */
  175. int16_t status; /* BLKIF_RSP_??? */
  176. };
  177. DEFINE_RING_TYPES(blkif_common, struct blkif_common_request,
  178. struct blkif_common_response);
  179. DEFINE_RING_TYPES(blkif_x86_32, struct blkif_x86_32_request,
  180. struct blkif_x86_32_response);
  181. DEFINE_RING_TYPES(blkif_x86_64, struct blkif_x86_64_request,
  182. struct blkif_x86_64_response);
  183. union blkif_back_rings {
  184. struct blkif_back_ring native;
  185. struct blkif_common_back_ring common;
  186. struct blkif_x86_32_back_ring x86_32;
  187. struct blkif_x86_64_back_ring x86_64;
  188. };
  189. enum blkif_protocol {
  190. BLKIF_PROTOCOL_NATIVE = 1,
  191. BLKIF_PROTOCOL_X86_32 = 2,
  192. BLKIF_PROTOCOL_X86_64 = 3,
  193. };
  194. struct xen_vbd {
  195. /* What the domain refers to this vbd as. */
  196. blkif_vdev_t handle;
  197. /* Non-zero -> read-only */
  198. unsigned char readonly;
  199. /* VDISK_xxx */
  200. unsigned char type;
  201. /* phys device that this vbd maps to. */
  202. u32 pdevice;
  203. struct block_device *bdev;
  204. /* Cached size parameter. */
  205. sector_t size;
  206. unsigned int flush_support:1;
  207. unsigned int discard_secure:1;
  208. unsigned int feature_gnt_persistent:1;
  209. unsigned int overflow_max_grants:1;
  210. };
  211. struct backend_info;
  212. /* Number of available flags */
  213. #define PERSISTENT_GNT_FLAGS_SIZE 2
  214. /* This persistent grant is currently in use */
  215. #define PERSISTENT_GNT_ACTIVE 0
  216. /*
  217. * This persistent grant has been used, this flag is set when we remove the
  218. * PERSISTENT_GNT_ACTIVE, to know that this grant has been used recently.
  219. */
  220. #define PERSISTENT_GNT_WAS_ACTIVE 1
  221. /* Number of requests that we can fit in a ring */
  222. #define XEN_BLKIF_REQS 32
  223. struct persistent_gnt {
  224. struct page *page;
  225. grant_ref_t gnt;
  226. grant_handle_t handle;
  227. DECLARE_BITMAP(flags, PERSISTENT_GNT_FLAGS_SIZE);
  228. struct rb_node node;
  229. struct list_head remove_node;
  230. };
  231. struct xen_blkif {
  232. /* Unique identifier for this interface. */
  233. domid_t domid;
  234. unsigned int handle;
  235. /* Physical parameters of the comms window. */
  236. unsigned int irq;
  237. /* Comms information. */
  238. enum blkif_protocol blk_protocol;
  239. union blkif_back_rings blk_rings;
  240. void *blk_ring;
  241. /* The VBD attached to this interface. */
  242. struct xen_vbd vbd;
  243. /* Back pointer to the backend_info. */
  244. struct backend_info *be;
  245. /* Private fields. */
  246. spinlock_t blk_ring_lock;
  247. atomic_t refcnt;
  248. wait_queue_head_t wq;
  249. /* for barrier (drain) requests */
  250. struct completion drain_complete;
  251. atomic_t drain;
  252. atomic_t inflight;
  253. /* One thread per one blkif. */
  254. struct task_struct *xenblkd;
  255. unsigned int waiting_reqs;
  256. /* tree to store persistent grants */
  257. struct rb_root persistent_gnts;
  258. unsigned int persistent_gnt_c;
  259. atomic_t persistent_gnt_in_use;
  260. unsigned long next_lru;
  261. /* used by the kworker that offload work from the persistent purge */
  262. struct list_head persistent_purge_list;
  263. struct work_struct persistent_purge_work;
  264. /* buffer of free pages to map grant refs */
  265. spinlock_t free_pages_lock;
  266. int free_pages_num;
  267. struct list_head free_pages;
  268. /* List of all 'pending_req' available */
  269. struct list_head pending_free;
  270. /* And its spinlock. */
  271. spinlock_t pending_free_lock;
  272. wait_queue_head_t pending_free_wq;
  273. /* statistics */
  274. unsigned long st_print;
  275. unsigned long long st_rd_req;
  276. unsigned long long st_wr_req;
  277. unsigned long long st_oo_req;
  278. unsigned long long st_f_req;
  279. unsigned long long st_ds_req;
  280. unsigned long long st_rd_sect;
  281. unsigned long long st_wr_sect;
  282. struct work_struct free_work;
  283. /* Thread shutdown wait queue. */
  284. wait_queue_head_t shutdown_wq;
  285. };
  286. struct seg_buf {
  287. unsigned long offset;
  288. unsigned int nsec;
  289. };
  290. struct grant_page {
  291. struct page *page;
  292. struct persistent_gnt *persistent_gnt;
  293. grant_handle_t handle;
  294. grant_ref_t gref;
  295. };
  296. /*
  297. * Each outstanding request that we've passed to the lower device layers has a
  298. * 'pending_req' allocated to it. Each buffer_head that completes decrements
  299. * the pendcnt towards zero. When it hits zero, the specified domain has a
  300. * response queued for it, with the saved 'id' passed back.
  301. */
  302. struct pending_req {
  303. struct xen_blkif *blkif;
  304. u64 id;
  305. int nr_pages;
  306. atomic_t pendcnt;
  307. unsigned short operation;
  308. int status;
  309. struct list_head free_list;
  310. struct grant_page *segments[MAX_INDIRECT_SEGMENTS];
  311. /* Indirect descriptors */
  312. struct grant_page *indirect_pages[MAX_INDIRECT_PAGES];
  313. struct seg_buf seg[MAX_INDIRECT_SEGMENTS];
  314. struct bio *biolist[MAX_INDIRECT_SEGMENTS];
  315. };
  316. #define vbd_sz(_v) ((_v)->bdev->bd_part ? \
  317. (_v)->bdev->bd_part->nr_sects : \
  318. get_capacity((_v)->bdev->bd_disk))
  319. #define xen_blkif_get(_b) (atomic_inc(&(_b)->refcnt))
  320. #define xen_blkif_put(_b) \
  321. do { \
  322. if (atomic_dec_and_test(&(_b)->refcnt)) \
  323. schedule_work(&(_b)->free_work);\
  324. } while (0)
  325. struct phys_req {
  326. unsigned short dev;
  327. blkif_sector_t nr_sects;
  328. struct block_device *bdev;
  329. blkif_sector_t sector_number;
  330. };
  331. int xen_blkif_interface_init(void);
  332. int xen_blkif_xenbus_init(void);
  333. irqreturn_t xen_blkif_be_int(int irq, void *dev_id);
  334. int xen_blkif_schedule(void *arg);
  335. int xen_blkif_purge_persistent(void *arg);
  336. void xen_blkbk_free_caches(struct xen_blkif *blkif);
  337. int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
  338. struct backend_info *be, int state);
  339. int xen_blkbk_barrier(struct xenbus_transaction xbt,
  340. struct backend_info *be, int state);
  341. struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be);
  342. void xen_blkbk_unmap_purged_grants(struct work_struct *work);
  343. static inline void blkif_get_x86_32_req(struct blkif_request *dst,
  344. struct blkif_x86_32_request *src)
  345. {
  346. int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST, j;
  347. dst->operation = src->operation;
  348. switch (src->operation) {
  349. case BLKIF_OP_READ:
  350. case BLKIF_OP_WRITE:
  351. case BLKIF_OP_WRITE_BARRIER:
  352. case BLKIF_OP_FLUSH_DISKCACHE:
  353. dst->u.rw.nr_segments = src->u.rw.nr_segments;
  354. dst->u.rw.handle = src->u.rw.handle;
  355. dst->u.rw.id = src->u.rw.id;
  356. dst->u.rw.sector_number = src->u.rw.sector_number;
  357. barrier();
  358. if (n > dst->u.rw.nr_segments)
  359. n = dst->u.rw.nr_segments;
  360. for (i = 0; i < n; i++)
  361. dst->u.rw.seg[i] = src->u.rw.seg[i];
  362. break;
  363. case BLKIF_OP_DISCARD:
  364. dst->u.discard.flag = src->u.discard.flag;
  365. dst->u.discard.id = src->u.discard.id;
  366. dst->u.discard.sector_number = src->u.discard.sector_number;
  367. dst->u.discard.nr_sectors = src->u.discard.nr_sectors;
  368. break;
  369. case BLKIF_OP_INDIRECT:
  370. dst->u.indirect.indirect_op = src->u.indirect.indirect_op;
  371. dst->u.indirect.nr_segments = src->u.indirect.nr_segments;
  372. dst->u.indirect.handle = src->u.indirect.handle;
  373. dst->u.indirect.id = src->u.indirect.id;
  374. dst->u.indirect.sector_number = src->u.indirect.sector_number;
  375. barrier();
  376. j = min(MAX_INDIRECT_PAGES, INDIRECT_PAGES(dst->u.indirect.nr_segments));
  377. for (i = 0; i < j; i++)
  378. dst->u.indirect.indirect_grefs[i] =
  379. src->u.indirect.indirect_grefs[i];
  380. break;
  381. default:
  382. /*
  383. * Don't know how to translate this op. Only get the
  384. * ID so failure can be reported to the frontend.
  385. */
  386. dst->u.other.id = src->u.other.id;
  387. break;
  388. }
  389. }
  390. static inline void blkif_get_x86_64_req(struct blkif_request *dst,
  391. struct blkif_x86_64_request *src)
  392. {
  393. int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST, j;
  394. dst->operation = src->operation;
  395. switch (src->operation) {
  396. case BLKIF_OP_READ:
  397. case BLKIF_OP_WRITE:
  398. case BLKIF_OP_WRITE_BARRIER:
  399. case BLKIF_OP_FLUSH_DISKCACHE:
  400. dst->u.rw.nr_segments = src->u.rw.nr_segments;
  401. dst->u.rw.handle = src->u.rw.handle;
  402. dst->u.rw.id = src->u.rw.id;
  403. dst->u.rw.sector_number = src->u.rw.sector_number;
  404. barrier();
  405. if (n > dst->u.rw.nr_segments)
  406. n = dst->u.rw.nr_segments;
  407. for (i = 0; i < n; i++)
  408. dst->u.rw.seg[i] = src->u.rw.seg[i];
  409. break;
  410. case BLKIF_OP_DISCARD:
  411. dst->u.discard.flag = src->u.discard.flag;
  412. dst->u.discard.id = src->u.discard.id;
  413. dst->u.discard.sector_number = src->u.discard.sector_number;
  414. dst->u.discard.nr_sectors = src->u.discard.nr_sectors;
  415. break;
  416. case BLKIF_OP_INDIRECT:
  417. dst->u.indirect.indirect_op = src->u.indirect.indirect_op;
  418. dst->u.indirect.nr_segments = src->u.indirect.nr_segments;
  419. dst->u.indirect.handle = src->u.indirect.handle;
  420. dst->u.indirect.id = src->u.indirect.id;
  421. dst->u.indirect.sector_number = src->u.indirect.sector_number;
  422. barrier();
  423. j = min(MAX_INDIRECT_PAGES, INDIRECT_PAGES(dst->u.indirect.nr_segments));
  424. for (i = 0; i < j; i++)
  425. dst->u.indirect.indirect_grefs[i] =
  426. src->u.indirect.indirect_grefs[i];
  427. break;
  428. default:
  429. /*
  430. * Don't know how to translate this op. Only get the
  431. * ID so failure can be reported to the frontend.
  432. */
  433. dst->u.other.id = src->u.other.id;
  434. break;
  435. }
  436. }
  437. #endif /* __XEN_BLKIF__BACKEND__COMMON_H__ */