blk.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * NVDIMM Block Window Driver
  3. * Copyright (c) 2014, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/blkdev.h>
  15. #include <linux/fs.h>
  16. #include <linux/genhd.h>
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/nd.h>
  20. #include <linux/sizes.h>
  21. #include "nd.h"
  22. static u32 nsblk_meta_size(struct nd_namespace_blk *nsblk)
  23. {
  24. return nsblk->lbasize - ((nsblk->lbasize >= 4096) ? 4096 : 512);
  25. }
  26. static u32 nsblk_internal_lbasize(struct nd_namespace_blk *nsblk)
  27. {
  28. return roundup(nsblk->lbasize, INT_LBASIZE_ALIGNMENT);
  29. }
  30. static u32 nsblk_sector_size(struct nd_namespace_blk *nsblk)
  31. {
  32. return nsblk->lbasize - nsblk_meta_size(nsblk);
  33. }
  34. static resource_size_t to_dev_offset(struct nd_namespace_blk *nsblk,
  35. resource_size_t ns_offset, unsigned int len)
  36. {
  37. int i;
  38. for (i = 0; i < nsblk->num_resources; i++) {
  39. if (ns_offset < resource_size(nsblk->res[i])) {
  40. if (ns_offset + len > resource_size(nsblk->res[i])) {
  41. dev_WARN_ONCE(&nsblk->common.dev, 1,
  42. "illegal request\n");
  43. return SIZE_MAX;
  44. }
  45. return nsblk->res[i]->start + ns_offset;
  46. }
  47. ns_offset -= resource_size(nsblk->res[i]);
  48. }
  49. dev_WARN_ONCE(&nsblk->common.dev, 1, "request out of range\n");
  50. return SIZE_MAX;
  51. }
  52. static struct nd_blk_region *to_ndbr(struct nd_namespace_blk *nsblk)
  53. {
  54. struct nd_region *nd_region;
  55. struct device *parent;
  56. parent = nsblk->common.dev.parent;
  57. nd_region = container_of(parent, struct nd_region, dev);
  58. return container_of(nd_region, struct nd_blk_region, nd_region);
  59. }
  60. #ifdef CONFIG_BLK_DEV_INTEGRITY
  61. static int nd_blk_rw_integrity(struct nd_namespace_blk *nsblk,
  62. struct bio_integrity_payload *bip, u64 lba, int rw)
  63. {
  64. struct nd_blk_region *ndbr = to_ndbr(nsblk);
  65. unsigned int len = nsblk_meta_size(nsblk);
  66. resource_size_t dev_offset, ns_offset;
  67. u32 internal_lbasize, sector_size;
  68. int err = 0;
  69. internal_lbasize = nsblk_internal_lbasize(nsblk);
  70. sector_size = nsblk_sector_size(nsblk);
  71. ns_offset = lba * internal_lbasize + sector_size;
  72. dev_offset = to_dev_offset(nsblk, ns_offset, len);
  73. if (dev_offset == SIZE_MAX)
  74. return -EIO;
  75. while (len) {
  76. unsigned int cur_len;
  77. struct bio_vec bv;
  78. void *iobuf;
  79. bv = bvec_iter_bvec(bip->bip_vec, bip->bip_iter);
  80. /*
  81. * The 'bv' obtained from bvec_iter_bvec has its .bv_len and
  82. * .bv_offset already adjusted for iter->bi_bvec_done, and we
  83. * can use those directly
  84. */
  85. cur_len = min(len, bv.bv_len);
  86. iobuf = kmap_atomic(bv.bv_page);
  87. err = ndbr->do_io(ndbr, dev_offset, iobuf + bv.bv_offset,
  88. cur_len, rw);
  89. kunmap_atomic(iobuf);
  90. if (err)
  91. return err;
  92. len -= cur_len;
  93. dev_offset += cur_len;
  94. bvec_iter_advance(bip->bip_vec, &bip->bip_iter, cur_len);
  95. }
  96. return err;
  97. }
  98. #else /* CONFIG_BLK_DEV_INTEGRITY */
  99. static int nd_blk_rw_integrity(struct nd_namespace_blk *nsblk,
  100. struct bio_integrity_payload *bip, u64 lba, int rw)
  101. {
  102. return 0;
  103. }
  104. #endif
  105. static int nsblk_do_bvec(struct nd_namespace_blk *nsblk,
  106. struct bio_integrity_payload *bip, struct page *page,
  107. unsigned int len, unsigned int off, int rw, sector_t sector)
  108. {
  109. struct nd_blk_region *ndbr = to_ndbr(nsblk);
  110. resource_size_t dev_offset, ns_offset;
  111. u32 internal_lbasize, sector_size;
  112. int err = 0;
  113. void *iobuf;
  114. u64 lba;
  115. internal_lbasize = nsblk_internal_lbasize(nsblk);
  116. sector_size = nsblk_sector_size(nsblk);
  117. while (len) {
  118. unsigned int cur_len;
  119. /*
  120. * If we don't have an integrity payload, we don't have to
  121. * split the bvec into sectors, as this would cause unnecessary
  122. * Block Window setup/move steps. the do_io routine is capable
  123. * of handling len <= PAGE_SIZE.
  124. */
  125. cur_len = bip ? min(len, sector_size) : len;
  126. lba = div_u64(sector << SECTOR_SHIFT, sector_size);
  127. ns_offset = lba * internal_lbasize;
  128. dev_offset = to_dev_offset(nsblk, ns_offset, cur_len);
  129. if (dev_offset == SIZE_MAX)
  130. return -EIO;
  131. iobuf = kmap_atomic(page);
  132. err = ndbr->do_io(ndbr, dev_offset, iobuf + off, cur_len, rw);
  133. kunmap_atomic(iobuf);
  134. if (err)
  135. return err;
  136. if (bip) {
  137. err = nd_blk_rw_integrity(nsblk, bip, lba, rw);
  138. if (err)
  139. return err;
  140. }
  141. len -= cur_len;
  142. off += cur_len;
  143. sector += sector_size >> SECTOR_SHIFT;
  144. }
  145. return err;
  146. }
  147. static blk_qc_t nd_blk_make_request(struct request_queue *q, struct bio *bio)
  148. {
  149. struct bio_integrity_payload *bip;
  150. struct nd_namespace_blk *nsblk;
  151. struct bvec_iter iter;
  152. unsigned long start;
  153. struct bio_vec bvec;
  154. int err = 0, rw;
  155. bool do_acct;
  156. /*
  157. * bio_integrity_enabled also checks if the bio already has an
  158. * integrity payload attached. If it does, we *don't* do a
  159. * bio_integrity_prep here - the payload has been generated by
  160. * another kernel subsystem, and we just pass it through.
  161. */
  162. if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
  163. bio->bi_error = -EIO;
  164. goto out;
  165. }
  166. bip = bio_integrity(bio);
  167. nsblk = q->queuedata;
  168. rw = bio_data_dir(bio);
  169. do_acct = nd_iostat_start(bio, &start);
  170. bio_for_each_segment(bvec, bio, iter) {
  171. unsigned int len = bvec.bv_len;
  172. BUG_ON(len > PAGE_SIZE);
  173. err = nsblk_do_bvec(nsblk, bip, bvec.bv_page, len,
  174. bvec.bv_offset, rw, iter.bi_sector);
  175. if (err) {
  176. dev_dbg(&nsblk->common.dev,
  177. "io error in %s sector %lld, len %d,\n",
  178. (rw == READ) ? "READ" : "WRITE",
  179. (unsigned long long) iter.bi_sector, len);
  180. bio->bi_error = err;
  181. break;
  182. }
  183. }
  184. if (do_acct)
  185. nd_iostat_end(bio, start);
  186. out:
  187. bio_endio(bio);
  188. return BLK_QC_T_NONE;
  189. }
  190. static int nsblk_rw_bytes(struct nd_namespace_common *ndns,
  191. resource_size_t offset, void *iobuf, size_t n, int rw)
  192. {
  193. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(&ndns->dev);
  194. struct nd_blk_region *ndbr = to_ndbr(nsblk);
  195. resource_size_t dev_offset;
  196. dev_offset = to_dev_offset(nsblk, offset, n);
  197. if (unlikely(offset + n > nsblk->size)) {
  198. dev_WARN_ONCE(&ndns->dev, 1, "request out of range\n");
  199. return -EFAULT;
  200. }
  201. if (dev_offset == SIZE_MAX)
  202. return -EIO;
  203. return ndbr->do_io(ndbr, dev_offset, iobuf, n, rw);
  204. }
  205. static const struct block_device_operations nd_blk_fops = {
  206. .owner = THIS_MODULE,
  207. .revalidate_disk = nvdimm_revalidate_disk,
  208. };
  209. static void nd_blk_release_queue(void *q)
  210. {
  211. blk_cleanup_queue(q);
  212. }
  213. static void nd_blk_release_disk(void *disk)
  214. {
  215. del_gendisk(disk);
  216. put_disk(disk);
  217. }
  218. static int nsblk_attach_disk(struct nd_namespace_blk *nsblk)
  219. {
  220. struct device *dev = &nsblk->common.dev;
  221. resource_size_t available_disk_size;
  222. struct request_queue *q;
  223. struct gendisk *disk;
  224. u64 internal_nlba;
  225. internal_nlba = div_u64(nsblk->size, nsblk_internal_lbasize(nsblk));
  226. available_disk_size = internal_nlba * nsblk_sector_size(nsblk);
  227. q = blk_alloc_queue(GFP_KERNEL);
  228. if (!q)
  229. return -ENOMEM;
  230. if (devm_add_action_or_reset(dev, nd_blk_release_queue, q))
  231. return -ENOMEM;
  232. blk_queue_make_request(q, nd_blk_make_request);
  233. blk_queue_max_hw_sectors(q, UINT_MAX);
  234. blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
  235. blk_queue_logical_block_size(q, nsblk_sector_size(nsblk));
  236. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
  237. q->queuedata = nsblk;
  238. disk = alloc_disk(0);
  239. if (!disk)
  240. return -ENOMEM;
  241. disk->first_minor = 0;
  242. disk->fops = &nd_blk_fops;
  243. disk->queue = q;
  244. disk->flags = GENHD_FL_EXT_DEVT;
  245. nvdimm_namespace_disk_name(&nsblk->common, disk->disk_name);
  246. if (devm_add_action_or_reset(dev, nd_blk_release_disk, disk))
  247. return -ENOMEM;
  248. if (nsblk_meta_size(nsblk)) {
  249. int rc = nd_integrity_init(disk, nsblk_meta_size(nsblk));
  250. if (rc)
  251. return rc;
  252. }
  253. set_capacity(disk, available_disk_size >> SECTOR_SHIFT);
  254. device_add_disk(dev, disk);
  255. revalidate_disk(disk);
  256. return 0;
  257. }
  258. static int nd_blk_probe(struct device *dev)
  259. {
  260. struct nd_namespace_common *ndns;
  261. struct nd_namespace_blk *nsblk;
  262. ndns = nvdimm_namespace_common_probe(dev);
  263. if (IS_ERR(ndns))
  264. return PTR_ERR(ndns);
  265. nsblk = to_nd_namespace_blk(&ndns->dev);
  266. nsblk->size = nvdimm_namespace_capacity(ndns);
  267. dev_set_drvdata(dev, nsblk);
  268. ndns->rw_bytes = nsblk_rw_bytes;
  269. if (is_nd_btt(dev))
  270. return nvdimm_namespace_attach_btt(ndns);
  271. else if (nd_btt_probe(dev, ndns) == 0) {
  272. /* we'll come back as btt-blk */
  273. return -ENXIO;
  274. } else
  275. return nsblk_attach_disk(nsblk);
  276. }
  277. static int nd_blk_remove(struct device *dev)
  278. {
  279. if (is_nd_btt(dev))
  280. nvdimm_namespace_detach_btt(to_nd_btt(dev));
  281. return 0;
  282. }
  283. static struct nd_device_driver nd_blk_driver = {
  284. .probe = nd_blk_probe,
  285. .remove = nd_blk_remove,
  286. .drv = {
  287. .name = "nd_blk",
  288. },
  289. .type = ND_DRIVER_NAMESPACE_BLK,
  290. };
  291. static int __init nd_blk_init(void)
  292. {
  293. return nd_driver_register(&nd_blk_driver);
  294. }
  295. static void __exit nd_blk_exit(void)
  296. {
  297. driver_unregister(&nd_blk_driver.drv);
  298. }
  299. MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>");
  300. MODULE_LICENSE("GPL v2");
  301. MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_BLK);
  302. module_init(nd_blk_init);
  303. module_exit(nd_blk_exit);