blk.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. if (!bvec_iter_advance(bip->bip_vec, &bip->bip_iter, cur_len))
  95. return -EIO;
  96. }
  97. return err;
  98. }
  99. #else /* CONFIG_BLK_DEV_INTEGRITY */
  100. static int nd_blk_rw_integrity(struct nd_namespace_blk *nsblk,
  101. struct bio_integrity_payload *bip, u64 lba, int rw)
  102. {
  103. return 0;
  104. }
  105. #endif
  106. static int nsblk_do_bvec(struct nd_namespace_blk *nsblk,
  107. struct bio_integrity_payload *bip, struct page *page,
  108. unsigned int len, unsigned int off, int rw, sector_t sector)
  109. {
  110. struct nd_blk_region *ndbr = to_ndbr(nsblk);
  111. resource_size_t dev_offset, ns_offset;
  112. u32 internal_lbasize, sector_size;
  113. int err = 0;
  114. void *iobuf;
  115. u64 lba;
  116. internal_lbasize = nsblk_internal_lbasize(nsblk);
  117. sector_size = nsblk_sector_size(nsblk);
  118. while (len) {
  119. unsigned int cur_len;
  120. /*
  121. * If we don't have an integrity payload, we don't have to
  122. * split the bvec into sectors, as this would cause unnecessary
  123. * Block Window setup/move steps. the do_io routine is capable
  124. * of handling len <= PAGE_SIZE.
  125. */
  126. cur_len = bip ? min(len, sector_size) : len;
  127. lba = div_u64(sector << SECTOR_SHIFT, sector_size);
  128. ns_offset = lba * internal_lbasize;
  129. dev_offset = to_dev_offset(nsblk, ns_offset, cur_len);
  130. if (dev_offset == SIZE_MAX)
  131. return -EIO;
  132. iobuf = kmap_atomic(page);
  133. err = ndbr->do_io(ndbr, dev_offset, iobuf + off, cur_len, rw);
  134. kunmap_atomic(iobuf);
  135. if (err)
  136. return err;
  137. if (bip) {
  138. err = nd_blk_rw_integrity(nsblk, bip, lba, rw);
  139. if (err)
  140. return err;
  141. }
  142. len -= cur_len;
  143. off += cur_len;
  144. sector += sector_size >> SECTOR_SHIFT;
  145. }
  146. return err;
  147. }
  148. static blk_qc_t nd_blk_make_request(struct request_queue *q, struct bio *bio)
  149. {
  150. struct bio_integrity_payload *bip;
  151. struct nd_namespace_blk *nsblk;
  152. struct bvec_iter iter;
  153. unsigned long start;
  154. struct bio_vec bvec;
  155. int err = 0, rw;
  156. bool do_acct;
  157. if (!bio_integrity_prep(bio))
  158. return BLK_QC_T_NONE;
  159. bip = bio_integrity(bio);
  160. nsblk = q->queuedata;
  161. rw = bio_data_dir(bio);
  162. do_acct = nd_iostat_start(bio, &start);
  163. bio_for_each_segment(bvec, bio, iter) {
  164. unsigned int len = bvec.bv_len;
  165. BUG_ON(len > PAGE_SIZE);
  166. err = nsblk_do_bvec(nsblk, bip, bvec.bv_page, len,
  167. bvec.bv_offset, rw, iter.bi_sector);
  168. if (err) {
  169. dev_dbg(&nsblk->common.dev,
  170. "io error in %s sector %lld, len %d,\n",
  171. (rw == READ) ? "READ" : "WRITE",
  172. (unsigned long long) iter.bi_sector, len);
  173. bio->bi_status = errno_to_blk_status(err);
  174. break;
  175. }
  176. }
  177. if (do_acct)
  178. nd_iostat_end(bio, start);
  179. bio_endio(bio);
  180. return BLK_QC_T_NONE;
  181. }
  182. static int nsblk_rw_bytes(struct nd_namespace_common *ndns,
  183. resource_size_t offset, void *iobuf, size_t n, int rw,
  184. unsigned long flags)
  185. {
  186. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(&ndns->dev);
  187. struct nd_blk_region *ndbr = to_ndbr(nsblk);
  188. resource_size_t dev_offset;
  189. dev_offset = to_dev_offset(nsblk, offset, n);
  190. if (unlikely(offset + n > nsblk->size)) {
  191. dev_WARN_ONCE(&ndns->dev, 1, "request out of range\n");
  192. return -EFAULT;
  193. }
  194. if (dev_offset == SIZE_MAX)
  195. return -EIO;
  196. return ndbr->do_io(ndbr, dev_offset, iobuf, n, rw);
  197. }
  198. static const struct block_device_operations nd_blk_fops = {
  199. .owner = THIS_MODULE,
  200. .revalidate_disk = nvdimm_revalidate_disk,
  201. };
  202. static void nd_blk_release_queue(void *q)
  203. {
  204. blk_cleanup_queue(q);
  205. }
  206. static void nd_blk_release_disk(void *disk)
  207. {
  208. del_gendisk(disk);
  209. put_disk(disk);
  210. }
  211. static int nsblk_attach_disk(struct nd_namespace_blk *nsblk)
  212. {
  213. struct device *dev = &nsblk->common.dev;
  214. resource_size_t available_disk_size;
  215. struct request_queue *q;
  216. struct gendisk *disk;
  217. u64 internal_nlba;
  218. internal_nlba = div_u64(nsblk->size, nsblk_internal_lbasize(nsblk));
  219. available_disk_size = internal_nlba * nsblk_sector_size(nsblk);
  220. q = blk_alloc_queue(GFP_KERNEL);
  221. if (!q)
  222. return -ENOMEM;
  223. if (devm_add_action_or_reset(dev, nd_blk_release_queue, q))
  224. return -ENOMEM;
  225. blk_queue_make_request(q, nd_blk_make_request);
  226. blk_queue_max_hw_sectors(q, UINT_MAX);
  227. blk_queue_logical_block_size(q, nsblk_sector_size(nsblk));
  228. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
  229. q->queuedata = nsblk;
  230. disk = alloc_disk(0);
  231. if (!disk)
  232. return -ENOMEM;
  233. disk->first_minor = 0;
  234. disk->fops = &nd_blk_fops;
  235. disk->queue = q;
  236. disk->flags = GENHD_FL_EXT_DEVT;
  237. nvdimm_namespace_disk_name(&nsblk->common, disk->disk_name);
  238. if (devm_add_action_or_reset(dev, nd_blk_release_disk, disk))
  239. return -ENOMEM;
  240. if (nsblk_meta_size(nsblk)) {
  241. int rc = nd_integrity_init(disk, nsblk_meta_size(nsblk));
  242. if (rc)
  243. return rc;
  244. }
  245. set_capacity(disk, available_disk_size >> SECTOR_SHIFT);
  246. device_add_disk(dev, disk);
  247. revalidate_disk(disk);
  248. return 0;
  249. }
  250. static int nd_blk_probe(struct device *dev)
  251. {
  252. struct nd_namespace_common *ndns;
  253. struct nd_namespace_blk *nsblk;
  254. ndns = nvdimm_namespace_common_probe(dev);
  255. if (IS_ERR(ndns))
  256. return PTR_ERR(ndns);
  257. nsblk = to_nd_namespace_blk(&ndns->dev);
  258. nsblk->size = nvdimm_namespace_capacity(ndns);
  259. dev_set_drvdata(dev, nsblk);
  260. ndns->rw_bytes = nsblk_rw_bytes;
  261. if (is_nd_btt(dev))
  262. return nvdimm_namespace_attach_btt(ndns);
  263. else if (nd_btt_probe(dev, ndns) == 0) {
  264. /* we'll come back as btt-blk */
  265. return -ENXIO;
  266. } else
  267. return nsblk_attach_disk(nsblk);
  268. }
  269. static int nd_blk_remove(struct device *dev)
  270. {
  271. if (is_nd_btt(dev))
  272. nvdimm_namespace_detach_btt(to_nd_btt(dev));
  273. return 0;
  274. }
  275. static struct nd_device_driver nd_blk_driver = {
  276. .probe = nd_blk_probe,
  277. .remove = nd_blk_remove,
  278. .drv = {
  279. .name = "nd_blk",
  280. },
  281. .type = ND_DRIVER_NAMESPACE_BLK,
  282. };
  283. static int __init nd_blk_init(void)
  284. {
  285. return nd_driver_register(&nd_blk_driver);
  286. }
  287. static void __exit nd_blk_exit(void)
  288. {
  289. driver_unregister(&nd_blk_driver.drv);
  290. }
  291. MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>");
  292. MODULE_LICENSE("GPL v2");
  293. MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_BLK);
  294. module_init(nd_blk_init);
  295. module_exit(nd_blk_exit);