common.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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_aligned))
  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. /* One thread per one blkif. */
  253. struct task_struct *xenblkd;
  254. unsigned int waiting_reqs;
  255. /* tree to store persistent grants */
  256. struct rb_root persistent_gnts;
  257. unsigned int persistent_gnt_c;
  258. atomic_t persistent_gnt_in_use;
  259. unsigned long next_lru;
  260. /* used by the kworker that offload work from the persistent purge */
  261. struct list_head persistent_purge_list;
  262. struct work_struct persistent_purge_work;
  263. /* buffer of free pages to map grant refs */
  264. spinlock_t free_pages_lock;
  265. int free_pages_num;
  266. struct list_head free_pages;
  267. /* Allocation of pending_reqs */
  268. struct pending_req *pending_reqs;
  269. /* List of all 'pending_req' available */
  270. struct list_head pending_free;
  271. /* And its spinlock. */
  272. spinlock_t pending_free_lock;
  273. wait_queue_head_t pending_free_wq;
  274. /* statistics */
  275. unsigned long st_print;
  276. unsigned long long st_rd_req;
  277. unsigned long long st_wr_req;
  278. unsigned long long st_oo_req;
  279. unsigned long long st_f_req;
  280. unsigned long long st_ds_req;
  281. unsigned long long st_rd_sect;
  282. unsigned long long st_wr_sect;
  283. wait_queue_head_t waiting_to_free;
  284. };
  285. struct seg_buf {
  286. unsigned long offset;
  287. unsigned int nsec;
  288. };
  289. /*
  290. * Each outstanding request that we've passed to the lower device layers has a
  291. * 'pending_req' allocated to it. Each buffer_head that completes decrements
  292. * the pendcnt towards zero. When it hits zero, the specified domain has a
  293. * response queued for it, with the saved 'id' passed back.
  294. */
  295. struct pending_req {
  296. struct xen_blkif *blkif;
  297. u64 id;
  298. int nr_pages;
  299. atomic_t pendcnt;
  300. unsigned short operation;
  301. int status;
  302. struct list_head free_list;
  303. struct page *pages[MAX_INDIRECT_SEGMENTS];
  304. struct persistent_gnt *persistent_gnts[MAX_INDIRECT_SEGMENTS];
  305. grant_handle_t grant_handles[MAX_INDIRECT_SEGMENTS];
  306. grant_ref_t grefs[MAX_INDIRECT_SEGMENTS];
  307. /* Indirect descriptors */
  308. struct persistent_gnt *indirect_persistent_gnts[MAX_INDIRECT_PAGES];
  309. struct page *indirect_pages[MAX_INDIRECT_PAGES];
  310. grant_handle_t indirect_handles[MAX_INDIRECT_PAGES];
  311. struct seg_buf seg[MAX_INDIRECT_SEGMENTS];
  312. struct bio *biolist[MAX_INDIRECT_SEGMENTS];
  313. };
  314. #define vbd_sz(_v) ((_v)->bdev->bd_part ? \
  315. (_v)->bdev->bd_part->nr_sects : \
  316. get_capacity((_v)->bdev->bd_disk))
  317. #define xen_blkif_get(_b) (atomic_inc(&(_b)->refcnt))
  318. #define xen_blkif_put(_b) \
  319. do { \
  320. if (atomic_dec_and_test(&(_b)->refcnt)) \
  321. wake_up(&(_b)->waiting_to_free);\
  322. } while (0)
  323. struct phys_req {
  324. unsigned short dev;
  325. blkif_sector_t nr_sects;
  326. struct block_device *bdev;
  327. blkif_sector_t sector_number;
  328. };
  329. int xen_blkif_interface_init(void);
  330. int xen_blkif_xenbus_init(void);
  331. irqreturn_t xen_blkif_be_int(int irq, void *dev_id);
  332. int xen_blkif_schedule(void *arg);
  333. int xen_blkif_purge_persistent(void *arg);
  334. int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
  335. struct backend_info *be, int state);
  336. int xen_blkbk_barrier(struct xenbus_transaction xbt,
  337. struct backend_info *be, int state);
  338. struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be);
  339. static inline void blkif_get_x86_32_req(struct blkif_request *dst,
  340. struct blkif_x86_32_request *src)
  341. {
  342. int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST, j;
  343. dst->operation = src->operation;
  344. switch (src->operation) {
  345. case BLKIF_OP_READ:
  346. case BLKIF_OP_WRITE:
  347. case BLKIF_OP_WRITE_BARRIER:
  348. case BLKIF_OP_FLUSH_DISKCACHE:
  349. dst->u.rw.nr_segments = src->u.rw.nr_segments;
  350. dst->u.rw.handle = src->u.rw.handle;
  351. dst->u.rw.id = src->u.rw.id;
  352. dst->u.rw.sector_number = src->u.rw.sector_number;
  353. barrier();
  354. if (n > dst->u.rw.nr_segments)
  355. n = dst->u.rw.nr_segments;
  356. for (i = 0; i < n; i++)
  357. dst->u.rw.seg[i] = src->u.rw.seg[i];
  358. break;
  359. case BLKIF_OP_DISCARD:
  360. dst->u.discard.flag = src->u.discard.flag;
  361. dst->u.discard.id = src->u.discard.id;
  362. dst->u.discard.sector_number = src->u.discard.sector_number;
  363. dst->u.discard.nr_sectors = src->u.discard.nr_sectors;
  364. break;
  365. case BLKIF_OP_INDIRECT:
  366. dst->u.indirect.indirect_op = src->u.indirect.indirect_op;
  367. dst->u.indirect.nr_segments = src->u.indirect.nr_segments;
  368. dst->u.indirect.handle = src->u.indirect.handle;
  369. dst->u.indirect.id = src->u.indirect.id;
  370. dst->u.indirect.sector_number = src->u.indirect.sector_number;
  371. barrier();
  372. j = min(MAX_INDIRECT_PAGES, INDIRECT_PAGES(dst->u.indirect.nr_segments));
  373. for (i = 0; i < j; i++)
  374. dst->u.indirect.indirect_grefs[i] =
  375. src->u.indirect.indirect_grefs[i];
  376. break;
  377. default:
  378. /*
  379. * Don't know how to translate this op. Only get the
  380. * ID so failure can be reported to the frontend.
  381. */
  382. dst->u.other.id = src->u.other.id;
  383. break;
  384. }
  385. }
  386. static inline void blkif_get_x86_64_req(struct blkif_request *dst,
  387. struct blkif_x86_64_request *src)
  388. {
  389. int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST, j;
  390. dst->operation = src->operation;
  391. switch (src->operation) {
  392. case BLKIF_OP_READ:
  393. case BLKIF_OP_WRITE:
  394. case BLKIF_OP_WRITE_BARRIER:
  395. case BLKIF_OP_FLUSH_DISKCACHE:
  396. dst->u.rw.nr_segments = src->u.rw.nr_segments;
  397. dst->u.rw.handle = src->u.rw.handle;
  398. dst->u.rw.id = src->u.rw.id;
  399. dst->u.rw.sector_number = src->u.rw.sector_number;
  400. barrier();
  401. if (n > dst->u.rw.nr_segments)
  402. n = dst->u.rw.nr_segments;
  403. for (i = 0; i < n; i++)
  404. dst->u.rw.seg[i] = src->u.rw.seg[i];
  405. break;
  406. case BLKIF_OP_DISCARD:
  407. dst->u.discard.flag = src->u.discard.flag;
  408. dst->u.discard.id = src->u.discard.id;
  409. dst->u.discard.sector_number = src->u.discard.sector_number;
  410. dst->u.discard.nr_sectors = src->u.discard.nr_sectors;
  411. break;
  412. case BLKIF_OP_INDIRECT:
  413. dst->u.indirect.indirect_op = src->u.indirect.indirect_op;
  414. dst->u.indirect.nr_segments = src->u.indirect.nr_segments;
  415. dst->u.indirect.handle = src->u.indirect.handle;
  416. dst->u.indirect.id = src->u.indirect.id;
  417. dst->u.indirect.sector_number = src->u.indirect.sector_number;
  418. barrier();
  419. j = min(MAX_INDIRECT_PAGES, INDIRECT_PAGES(dst->u.indirect.nr_segments));
  420. for (i = 0; i < j; i++)
  421. dst->u.indirect.indirect_grefs[i] =
  422. src->u.indirect.indirect_grefs[i];
  423. break;
  424. default:
  425. /*
  426. * Don't know how to translate this op. Only get the
  427. * ID so failure can be reported to the frontend.
  428. */
  429. dst->u.other.id = src->u.other.id;
  430. break;
  431. }
  432. }
  433. #endif /* __XEN_BLKIF__BACKEND__COMMON_H__ */