dm-ebs-target.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. * Copyright (C) 2020 Red Hat GmbH
  3. *
  4. * This file is released under the GPL.
  5. *
  6. * Device-mapper target to emulate smaller logical block
  7. * size on backing devices exposing (natively) larger ones.
  8. *
  9. * E.g. 512 byte sector emulation on 4K native disks.
  10. */
  11. #include "dm.h"
  12. #include <linux/module.h>
  13. #include <linux/workqueue.h>
  14. #include <linux/dm-bufio.h>
  15. #define DM_MSG_PREFIX "ebs"
  16. static void ebs_dtr(struct dm_target *ti);
  17. /* Emulated block size context. */
  18. struct ebs_c {
  19. struct dm_dev *dev; /* Underlying device to emulate block size on. */
  20. struct dm_bufio_client *bufio; /* Use dm-bufio for read and read-modify-write processing. */
  21. struct workqueue_struct *wq; /* Workqueue for ^ processing of bios. */
  22. struct work_struct ws; /* Work item used for ^. */
  23. struct bio_list bios_in; /* Worker bios input list. */
  24. spinlock_t lock; /* Guard bios input list above. */
  25. sector_t start; /* <start> table line argument, see ebs_ctr below. */
  26. unsigned int e_bs; /* Emulated block size in sectors exposed to upper layer. */
  27. unsigned int u_bs; /* Underlying block size in sectors retrieved from/set on lower layer device. */
  28. unsigned char block_shift; /* bitshift sectors -> blocks used in dm-bufio API. */
  29. bool u_bs_set:1; /* Flag to indicate underlying block size is set on table line. */
  30. };
  31. static inline sector_t __sector_to_block(struct ebs_c *ec, sector_t sector)
  32. {
  33. return sector >> ec->block_shift;
  34. }
  35. static inline sector_t __block_mod(sector_t sector, unsigned int bs)
  36. {
  37. return sector & (bs - 1);
  38. }
  39. /* Return number of blocks for a bio, accounting for misalignment of start and end sectors. */
  40. static inline unsigned int __nr_blocks(struct ebs_c *ec, struct bio *bio)
  41. {
  42. sector_t end_sector = __block_mod(bio->bi_iter.bi_sector, ec->u_bs) + bio_sectors(bio);
  43. return __sector_to_block(ec, end_sector) + (__block_mod(end_sector, ec->u_bs) ? 1 : 0);
  44. }
  45. static inline bool __ebs_check_bs(unsigned int bs)
  46. {
  47. return bs && is_power_of_2(bs);
  48. }
  49. /*
  50. * READ/WRITE:
  51. *
  52. * copy blocks between bufio blocks and bio vector's (partial/overlapping) pages.
  53. */
  54. static int __ebs_rw_bvec(struct ebs_c *ec, enum req_op op, struct bio_vec *bv,
  55. struct bvec_iter *iter)
  56. {
  57. int r = 0;
  58. unsigned char *ba, *pa;
  59. unsigned int cur_len;
  60. unsigned int bv_len = bv->bv_len;
  61. unsigned int buf_off = to_bytes(__block_mod(iter->bi_sector, ec->u_bs));
  62. sector_t block = __sector_to_block(ec, iter->bi_sector);
  63. struct dm_buffer *b;
  64. if (unlikely(!bv->bv_page || !bv_len))
  65. return -EIO;
  66. pa = bvec_virt(bv);
  67. /* Handle overlapping page <-> blocks */
  68. while (bv_len) {
  69. cur_len = min(dm_bufio_get_block_size(ec->bufio) - buf_off, bv_len);
  70. /* Avoid reading for writes in case bio vector's page overwrites block completely. */
  71. if (op == REQ_OP_READ || buf_off || bv_len < dm_bufio_get_block_size(ec->bufio))
  72. ba = dm_bufio_read(ec->bufio, block, &b);
  73. else
  74. ba = dm_bufio_new(ec->bufio, block, &b);
  75. if (IS_ERR(ba)) {
  76. /*
  77. * Carry on with next buffer, if any, to issue all possible
  78. * data but return error.
  79. */
  80. r = PTR_ERR(ba);
  81. } else {
  82. /* Copy data to/from bio to buffer if read/new was successful above. */
  83. ba += buf_off;
  84. if (op == REQ_OP_READ) {
  85. memcpy(pa, ba, cur_len);
  86. flush_dcache_page(bv->bv_page);
  87. } else {
  88. flush_dcache_page(bv->bv_page);
  89. memcpy(ba, pa, cur_len);
  90. dm_bufio_mark_partial_buffer_dirty(b, buf_off, buf_off + cur_len);
  91. }
  92. dm_bufio_release(b);
  93. }
  94. pa += cur_len;
  95. bv_len -= cur_len;
  96. buf_off = 0;
  97. block++;
  98. }
  99. return r;
  100. }
  101. /* READ/WRITE: iterate bio vector's copying between (partial) pages and bufio blocks. */
  102. static int __ebs_rw_bio(struct ebs_c *ec, enum req_op op, struct bio *bio)
  103. {
  104. int r = 0, rr;
  105. struct bio_vec bv;
  106. struct bvec_iter iter;
  107. bio_for_each_bvec(bv, bio, iter) {
  108. rr = __ebs_rw_bvec(ec, op, &bv, &iter);
  109. if (rr)
  110. r = rr;
  111. }
  112. return r;
  113. }
  114. /*
  115. * Discard bio's blocks, i.e. pass discards down.
  116. *
  117. * Avoid discarding partial blocks at beginning and end;
  118. * return 0 in case no blocks can be discarded as a result.
  119. */
  120. static int __ebs_discard_bio(struct ebs_c *ec, struct bio *bio)
  121. {
  122. sector_t block, blocks, sector = bio->bi_iter.bi_sector;
  123. block = __sector_to_block(ec, sector);
  124. blocks = __nr_blocks(ec, bio);
  125. /*
  126. * Partial first underlying block (__nr_blocks() may have
  127. * resulted in one block).
  128. */
  129. if (__block_mod(sector, ec->u_bs)) {
  130. block++;
  131. blocks--;
  132. }
  133. /* Partial last underlying block if any. */
  134. if (blocks && __block_mod(bio_end_sector(bio), ec->u_bs))
  135. blocks--;
  136. return blocks ? dm_bufio_issue_discard(ec->bufio, block, blocks) : 0;
  137. }
  138. /* Release blocks them from the bufio cache. */
  139. static void __ebs_forget_bio(struct ebs_c *ec, struct bio *bio)
  140. {
  141. sector_t blocks, sector = bio->bi_iter.bi_sector;
  142. blocks = __nr_blocks(ec, bio);
  143. dm_bufio_forget_buffers(ec->bufio, __sector_to_block(ec, sector), blocks);
  144. }
  145. /* Worker function to process incoming bios. */
  146. static void __ebs_process_bios(struct work_struct *ws)
  147. {
  148. int r;
  149. bool write = false;
  150. sector_t block1, block2;
  151. struct ebs_c *ec = container_of(ws, struct ebs_c, ws);
  152. struct bio *bio;
  153. struct bio_list bios;
  154. bio_list_init(&bios);
  155. spin_lock_irq(&ec->lock);
  156. bios = ec->bios_in;
  157. bio_list_init(&ec->bios_in);
  158. spin_unlock_irq(&ec->lock);
  159. /* Prefetch all read and any mis-aligned write buffers */
  160. bio_list_for_each(bio, &bios) {
  161. block1 = __sector_to_block(ec, bio->bi_iter.bi_sector);
  162. if (bio_op(bio) == REQ_OP_READ)
  163. dm_bufio_prefetch(ec->bufio, block1, __nr_blocks(ec, bio));
  164. else if (bio_op(bio) == REQ_OP_WRITE && !(bio->bi_opf & REQ_PREFLUSH)) {
  165. block2 = __sector_to_block(ec, bio_end_sector(bio));
  166. if (__block_mod(bio->bi_iter.bi_sector, ec->u_bs))
  167. dm_bufio_prefetch(ec->bufio, block1, 1);
  168. if (__block_mod(bio_end_sector(bio), ec->u_bs) && block2 != block1)
  169. dm_bufio_prefetch(ec->bufio, block2, 1);
  170. }
  171. }
  172. bio_list_for_each(bio, &bios) {
  173. r = -EIO;
  174. if (bio_op(bio) == REQ_OP_READ)
  175. r = __ebs_rw_bio(ec, REQ_OP_READ, bio);
  176. else if (bio_op(bio) == REQ_OP_WRITE) {
  177. write = true;
  178. r = __ebs_rw_bio(ec, REQ_OP_WRITE, bio);
  179. } else if (bio_op(bio) == REQ_OP_DISCARD) {
  180. __ebs_forget_bio(ec, bio);
  181. r = __ebs_discard_bio(ec, bio);
  182. }
  183. if (r < 0)
  184. bio->bi_status = errno_to_blk_status(r);
  185. }
  186. /*
  187. * We write dirty buffers after processing I/O on them
  188. * but before we endio thus addressing REQ_FUA/REQ_SYNC.
  189. */
  190. r = write ? dm_bufio_write_dirty_buffers(ec->bufio) : 0;
  191. while ((bio = bio_list_pop(&bios))) {
  192. /* Any other request is endioed. */
  193. if (unlikely(r && bio_op(bio) == REQ_OP_WRITE))
  194. bio_io_error(bio);
  195. else
  196. bio_endio(bio);
  197. }
  198. }
  199. /*
  200. * Construct an emulated block size mapping: <dev_path> <offset> <ebs> [<ubs>]
  201. *
  202. * <dev_path>: path of the underlying device
  203. * <offset>: offset in 512 bytes sectors into <dev_path>
  204. * <ebs>: emulated block size in units of 512 bytes exposed to the upper layer
  205. * [<ubs>]: underlying block size in units of 512 bytes imposed on the lower layer;
  206. * optional, if not supplied, retrieve logical block size from underlying device
  207. */
  208. static int ebs_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  209. {
  210. int r;
  211. unsigned short tmp1;
  212. unsigned long long tmp;
  213. char dummy;
  214. struct ebs_c *ec;
  215. if (argc < 3 || argc > 4) {
  216. ti->error = "Invalid argument count";
  217. return -EINVAL;
  218. }
  219. ec = ti->private = kzalloc(sizeof(*ec), GFP_KERNEL);
  220. if (!ec) {
  221. ti->error = "Cannot allocate ebs context";
  222. return -ENOMEM;
  223. }
  224. r = -EINVAL;
  225. if (sscanf(argv[1], "%llu%c", &tmp, &dummy) != 1 ||
  226. tmp != (sector_t)tmp ||
  227. (sector_t)tmp >= ti->len) {
  228. ti->error = "Invalid device offset sector";
  229. goto bad;
  230. }
  231. ec->start = tmp;
  232. if (sscanf(argv[2], "%hu%c", &tmp1, &dummy) != 1 ||
  233. !__ebs_check_bs(tmp1) ||
  234. to_bytes(tmp1) > PAGE_SIZE) {
  235. ti->error = "Invalid emulated block size";
  236. goto bad;
  237. }
  238. ec->e_bs = tmp1;
  239. if (argc > 3) {
  240. if (sscanf(argv[3], "%hu%c", &tmp1, &dummy) != 1 || !__ebs_check_bs(tmp1)) {
  241. ti->error = "Invalid underlying block size";
  242. goto bad;
  243. }
  244. ec->u_bs = tmp1;
  245. ec->u_bs_set = true;
  246. } else
  247. ec->u_bs_set = false;
  248. r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &ec->dev);
  249. if (r) {
  250. ti->error = "Device lookup failed";
  251. ec->dev = NULL;
  252. goto bad;
  253. }
  254. r = -EINVAL;
  255. if (!ec->u_bs_set) {
  256. ec->u_bs = to_sector(bdev_logical_block_size(ec->dev->bdev));
  257. if (!__ebs_check_bs(ec->u_bs)) {
  258. ti->error = "Invalid retrieved underlying block size";
  259. goto bad;
  260. }
  261. }
  262. if (!ec->u_bs_set && ec->e_bs == ec->u_bs)
  263. DMINFO("Emulation superfluous: emulated equal to underlying block size");
  264. if (__block_mod(ec->start, ec->u_bs)) {
  265. ti->error = "Device offset must be multiple of underlying block size";
  266. goto bad;
  267. }
  268. ec->bufio = dm_bufio_client_create(ec->dev->bdev, to_bytes(ec->u_bs), 1,
  269. 0, NULL, NULL, 0);
  270. if (IS_ERR(ec->bufio)) {
  271. ti->error = "Cannot create dm bufio client";
  272. r = PTR_ERR(ec->bufio);
  273. ec->bufio = NULL;
  274. goto bad;
  275. }
  276. ec->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
  277. if (!ec->wq) {
  278. ti->error = "Cannot create dm-" DM_MSG_PREFIX " workqueue";
  279. r = -ENOMEM;
  280. goto bad;
  281. }
  282. ec->block_shift = __ffs(ec->u_bs);
  283. INIT_WORK(&ec->ws, &__ebs_process_bios);
  284. bio_list_init(&ec->bios_in);
  285. spin_lock_init(&ec->lock);
  286. ti->num_flush_bios = 1;
  287. ti->num_discard_bios = 1;
  288. ti->num_secure_erase_bios = 0;
  289. ti->num_write_zeroes_bios = 0;
  290. return 0;
  291. bad:
  292. ebs_dtr(ti);
  293. return r;
  294. }
  295. static void ebs_dtr(struct dm_target *ti)
  296. {
  297. struct ebs_c *ec = ti->private;
  298. if (ec->wq)
  299. destroy_workqueue(ec->wq);
  300. if (ec->bufio)
  301. dm_bufio_client_destroy(ec->bufio);
  302. if (ec->dev)
  303. dm_put_device(ti, ec->dev);
  304. kfree(ec);
  305. }
  306. static int ebs_map(struct dm_target *ti, struct bio *bio)
  307. {
  308. struct ebs_c *ec = ti->private;
  309. bio_set_dev(bio, ec->dev->bdev);
  310. bio->bi_iter.bi_sector = ec->start + dm_target_offset(ti, bio->bi_iter.bi_sector);
  311. if (unlikely(bio_op(bio) == REQ_OP_FLUSH))
  312. return DM_MAPIO_REMAPPED;
  313. /*
  314. * Only queue for bufio processing in case of partial or overlapping buffers
  315. * -or-
  316. * emulation with ebs == ubs aiming for tests of dm-bufio overhead.
  317. */
  318. if (likely(__block_mod(bio->bi_iter.bi_sector, ec->u_bs) ||
  319. __block_mod(bio_end_sector(bio), ec->u_bs) ||
  320. ec->e_bs == ec->u_bs)) {
  321. spin_lock_irq(&ec->lock);
  322. bio_list_add(&ec->bios_in, bio);
  323. spin_unlock_irq(&ec->lock);
  324. queue_work(ec->wq, &ec->ws);
  325. return DM_MAPIO_SUBMITTED;
  326. }
  327. /* Forget any buffer content relative to this direct backing device I/O. */
  328. __ebs_forget_bio(ec, bio);
  329. return DM_MAPIO_REMAPPED;
  330. }
  331. static void ebs_status(struct dm_target *ti, status_type_t type,
  332. unsigned int status_flags, char *result, unsigned int maxlen)
  333. {
  334. struct ebs_c *ec = ti->private;
  335. switch (type) {
  336. case STATUSTYPE_INFO:
  337. *result = '\0';
  338. break;
  339. case STATUSTYPE_TABLE:
  340. snprintf(result, maxlen, ec->u_bs_set ? "%s %llu %u %u" : "%s %llu %u",
  341. ec->dev->name, (unsigned long long) ec->start, ec->e_bs, ec->u_bs);
  342. break;
  343. case STATUSTYPE_IMA:
  344. *result = '\0';
  345. break;
  346. }
  347. }
  348. static int ebs_prepare_ioctl(struct dm_target *ti, struct block_device **bdev)
  349. {
  350. struct ebs_c *ec = ti->private;
  351. struct dm_dev *dev = ec->dev;
  352. /*
  353. * Only pass ioctls through if the device sizes match exactly.
  354. */
  355. *bdev = dev->bdev;
  356. return !!(ec->start || ti->len != bdev_nr_sectors(dev->bdev));
  357. }
  358. static void ebs_io_hints(struct dm_target *ti, struct queue_limits *limits)
  359. {
  360. struct ebs_c *ec = ti->private;
  361. limits->logical_block_size = to_bytes(ec->e_bs);
  362. limits->physical_block_size = to_bytes(ec->u_bs);
  363. limits->alignment_offset = limits->physical_block_size;
  364. blk_limits_io_min(limits, limits->logical_block_size);
  365. }
  366. static int ebs_iterate_devices(struct dm_target *ti,
  367. iterate_devices_callout_fn fn, void *data)
  368. {
  369. struct ebs_c *ec = ti->private;
  370. return fn(ti, ec->dev, ec->start, ti->len, data);
  371. }
  372. static struct target_type ebs_target = {
  373. .name = "ebs",
  374. .version = {1, 0, 1},
  375. .features = DM_TARGET_PASSES_INTEGRITY,
  376. .module = THIS_MODULE,
  377. .ctr = ebs_ctr,
  378. .dtr = ebs_dtr,
  379. .map = ebs_map,
  380. .status = ebs_status,
  381. .io_hints = ebs_io_hints,
  382. .prepare_ioctl = ebs_prepare_ioctl,
  383. .iterate_devices = ebs_iterate_devices,
  384. };
  385. static int __init dm_ebs_init(void)
  386. {
  387. int r = dm_register_target(&ebs_target);
  388. if (r < 0)
  389. DMERR("register failed %d", r);
  390. return r;
  391. }
  392. static void dm_ebs_exit(void)
  393. {
  394. dm_unregister_target(&ebs_target);
  395. }
  396. module_init(dm_ebs_init);
  397. module_exit(dm_ebs_exit);
  398. MODULE_AUTHOR("Heinz Mauelshagen <[email protected]>");
  399. MODULE_DESCRIPTION(DM_NAME " emulated block size target");
  400. MODULE_LICENSE("GPL");