io-cmd-bdev.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * NVMe I/O command implementation.
  4. * Copyright (c) 2015-2016 HGST, a Western Digital Company.
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/blkdev.h>
  8. #include <linux/blk-integrity.h>
  9. #include <linux/memremap.h>
  10. #include <linux/module.h>
  11. #include "nvmet.h"
  12. void nvmet_bdev_set_limits(struct block_device *bdev, struct nvme_id_ns *id)
  13. {
  14. /* Logical blocks per physical block, 0's based. */
  15. const __le16 lpp0b = to0based(bdev_physical_block_size(bdev) /
  16. bdev_logical_block_size(bdev));
  17. /*
  18. * For NVMe 1.2 and later, bit 1 indicates that the fields NAWUN,
  19. * NAWUPF, and NACWU are defined for this namespace and should be
  20. * used by the host for this namespace instead of the AWUN, AWUPF,
  21. * and ACWU fields in the Identify Controller data structure. If
  22. * any of these fields are zero that means that the corresponding
  23. * field from the identify controller data structure should be used.
  24. */
  25. id->nsfeat |= 1 << 1;
  26. id->nawun = lpp0b;
  27. id->nawupf = lpp0b;
  28. id->nacwu = lpp0b;
  29. /*
  30. * Bit 4 indicates that the fields NPWG, NPWA, NPDG, NPDA, and
  31. * NOWS are defined for this namespace and should be used by
  32. * the host for I/O optimization.
  33. */
  34. id->nsfeat |= 1 << 4;
  35. /* NPWG = Namespace Preferred Write Granularity. 0's based */
  36. id->npwg = to0based(bdev_io_min(bdev) / bdev_logical_block_size(bdev));
  37. /* NPWA = Namespace Preferred Write Alignment. 0's based */
  38. id->npwa = id->npwg;
  39. /* NPDG = Namespace Preferred Deallocate Granularity. 0's based */
  40. id->npdg = to0based(bdev_discard_granularity(bdev) /
  41. bdev_logical_block_size(bdev));
  42. /* NPDG = Namespace Preferred Deallocate Alignment */
  43. id->npda = id->npdg;
  44. /* NOWS = Namespace Optimal Write Size */
  45. id->nows = to0based(bdev_io_opt(bdev) / bdev_logical_block_size(bdev));
  46. /* Set WZDS and DRB if device supports unmapped write zeroes */
  47. if (bdev_write_zeroes_unmap_sectors(bdev))
  48. id->dlfeat = (1 << 3) | 0x1;
  49. }
  50. void nvmet_bdev_ns_disable(struct nvmet_ns *ns)
  51. {
  52. if (ns->bdev_file) {
  53. fput(ns->bdev_file);
  54. ns->bdev = NULL;
  55. ns->bdev_file = NULL;
  56. }
  57. }
  58. static void nvmet_bdev_ns_enable_integrity(struct nvmet_ns *ns)
  59. {
  60. struct blk_integrity *bi = bdev_get_integrity(ns->bdev);
  61. if (!bi)
  62. return;
  63. if (bi->csum_type == BLK_INTEGRITY_CSUM_CRC) {
  64. ns->metadata_size = bi->metadata_size;
  65. if (bi->flags & BLK_INTEGRITY_REF_TAG)
  66. ns->pi_type = NVME_NS_DPS_PI_TYPE1;
  67. else
  68. ns->pi_type = NVME_NS_DPS_PI_TYPE3;
  69. } else {
  70. ns->metadata_size = 0;
  71. }
  72. }
  73. int nvmet_bdev_ns_enable(struct nvmet_ns *ns)
  74. {
  75. int ret;
  76. /*
  77. * When buffered_io namespace attribute is enabled that means user want
  78. * this block device to be used as a file, so block device can take
  79. * an advantage of cache.
  80. */
  81. if (ns->buffered_io)
  82. return -ENOTBLK;
  83. ns->bdev_file = bdev_file_open_by_path(ns->device_path,
  84. BLK_OPEN_READ | BLK_OPEN_WRITE, NULL, NULL);
  85. if (IS_ERR(ns->bdev_file)) {
  86. ret = PTR_ERR(ns->bdev_file);
  87. if (ret != -ENOTBLK) {
  88. pr_err("failed to open block device %s: (%d)\n",
  89. ns->device_path, ret);
  90. }
  91. ns->bdev_file = NULL;
  92. return ret;
  93. }
  94. ns->bdev = file_bdev(ns->bdev_file);
  95. ns->size = bdev_nr_bytes(ns->bdev);
  96. ns->blksize_shift = blksize_bits(bdev_logical_block_size(ns->bdev));
  97. ns->pi_type = 0;
  98. ns->metadata_size = 0;
  99. if (IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY))
  100. nvmet_bdev_ns_enable_integrity(ns);
  101. if (bdev_is_zoned(ns->bdev)) {
  102. if (!nvmet_bdev_zns_enable(ns)) {
  103. nvmet_bdev_ns_disable(ns);
  104. return -EINVAL;
  105. }
  106. ns->csi = NVME_CSI_ZNS;
  107. }
  108. return 0;
  109. }
  110. void nvmet_bdev_ns_revalidate(struct nvmet_ns *ns)
  111. {
  112. ns->size = bdev_nr_bytes(ns->bdev);
  113. }
  114. u16 blk_to_nvme_status(struct nvmet_req *req, blk_status_t blk_sts)
  115. {
  116. u16 status = NVME_SC_SUCCESS;
  117. if (likely(blk_sts == BLK_STS_OK))
  118. return status;
  119. /*
  120. * Right now there exists M : 1 mapping between block layer error
  121. * to the NVMe status code (see nvme_error_status()). For consistency,
  122. * when we reverse map we use most appropriate NVMe Status code from
  123. * the group of the NVMe status codes used in the nvme_error_status().
  124. */
  125. switch (blk_sts) {
  126. case BLK_STS_NOSPC:
  127. status = NVME_SC_CAP_EXCEEDED | NVME_STATUS_DNR;
  128. req->error_loc = offsetof(struct nvme_rw_command, length);
  129. break;
  130. case BLK_STS_TARGET:
  131. status = NVME_SC_LBA_RANGE | NVME_STATUS_DNR;
  132. req->error_loc = offsetof(struct nvme_rw_command, slba);
  133. break;
  134. case BLK_STS_NOTSUPP:
  135. status = NVME_SC_INVALID_OPCODE | NVME_STATUS_DNR;
  136. req->error_loc = offsetof(struct nvme_common_command, opcode);
  137. break;
  138. case BLK_STS_MEDIUM:
  139. status = NVME_SC_ACCESS_DENIED;
  140. req->error_loc = offsetof(struct nvme_rw_command, nsid);
  141. break;
  142. case BLK_STS_IOERR:
  143. default:
  144. status = NVME_SC_INTERNAL | NVME_STATUS_DNR;
  145. req->error_loc = offsetof(struct nvme_common_command, opcode);
  146. }
  147. switch (req->cmd->common.opcode) {
  148. case nvme_cmd_read:
  149. case nvme_cmd_write:
  150. req->error_slba = le64_to_cpu(req->cmd->rw.slba);
  151. break;
  152. case nvme_cmd_write_zeroes:
  153. req->error_slba =
  154. le64_to_cpu(req->cmd->write_zeroes.slba);
  155. break;
  156. default:
  157. req->error_slba = 0;
  158. }
  159. return status;
  160. }
  161. static void nvmet_bio_done(struct bio *bio)
  162. {
  163. struct nvmet_req *req = bio->bi_private;
  164. blk_status_t blk_status = bio->bi_status;
  165. nvmet_req_bio_put(req, bio);
  166. nvmet_req_complete(req, blk_to_nvme_status(req, blk_status));
  167. }
  168. #ifdef CONFIG_BLK_DEV_INTEGRITY
  169. static int nvmet_bdev_alloc_bip(struct nvmet_req *req, struct bio *bio,
  170. struct sg_mapping_iter *miter)
  171. {
  172. struct blk_integrity *bi;
  173. struct bio_integrity_payload *bip;
  174. int rc;
  175. size_t resid, len;
  176. bi = bdev_get_integrity(req->ns->bdev);
  177. if (unlikely(!bi)) {
  178. pr_err("Unable to locate bio_integrity\n");
  179. return -ENODEV;
  180. }
  181. bip = bio_integrity_alloc(bio, GFP_NOIO,
  182. bio_max_segs(req->metadata_sg_cnt));
  183. if (IS_ERR(bip)) {
  184. pr_err("Unable to allocate bio_integrity_payload\n");
  185. return PTR_ERR(bip);
  186. }
  187. /* virtual start sector must be in integrity interval units */
  188. bip_set_seed(bip, bio->bi_iter.bi_sector >>
  189. (bi->interval_exp - SECTOR_SHIFT));
  190. resid = bio_integrity_bytes(bi, bio_sectors(bio));
  191. while (resid > 0 && sg_miter_next(miter)) {
  192. len = min_t(size_t, miter->length, resid);
  193. rc = bio_integrity_add_page(bio, miter->page, len,
  194. offset_in_page(miter->addr));
  195. if (unlikely(rc != len)) {
  196. pr_err("bio_integrity_add_page() failed; %d\n", rc);
  197. sg_miter_stop(miter);
  198. return -ENOMEM;
  199. }
  200. resid -= len;
  201. if (len < miter->length)
  202. miter->consumed -= miter->length - len;
  203. }
  204. sg_miter_stop(miter);
  205. return 0;
  206. }
  207. #else
  208. static int nvmet_bdev_alloc_bip(struct nvmet_req *req, struct bio *bio,
  209. struct sg_mapping_iter *miter)
  210. {
  211. return -EINVAL;
  212. }
  213. #endif /* CONFIG_BLK_DEV_INTEGRITY */
  214. static void nvmet_bdev_execute_rw(struct nvmet_req *req)
  215. {
  216. unsigned int sg_cnt = req->sg_cnt;
  217. struct bio *bio;
  218. struct scatterlist *sg;
  219. struct blk_plug plug;
  220. sector_t sector;
  221. blk_opf_t opf;
  222. int i, rc;
  223. struct sg_mapping_iter prot_miter;
  224. unsigned int iter_flags;
  225. unsigned int total_len = nvmet_rw_data_len(req) + req->metadata_len;
  226. if (!nvmet_check_transfer_len(req, total_len))
  227. return;
  228. if (!req->sg_cnt) {
  229. nvmet_req_complete(req, 0);
  230. return;
  231. }
  232. if (req->cmd->rw.opcode == nvme_cmd_write) {
  233. opf = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
  234. if (req->cmd->rw.control & cpu_to_le16(NVME_RW_FUA))
  235. opf |= REQ_FUA;
  236. iter_flags = SG_MITER_TO_SG;
  237. } else {
  238. opf = REQ_OP_READ;
  239. iter_flags = SG_MITER_FROM_SG;
  240. }
  241. if (req->cmd->rw.control & cpu_to_le16(NVME_RW_LR))
  242. opf |= REQ_FAILFAST_DEV;
  243. if (is_pci_p2pdma_page(sg_page(req->sg)))
  244. opf |= REQ_NOMERGE;
  245. sector = nvmet_lba_to_sect(req->ns, req->cmd->rw.slba);
  246. if (nvmet_use_inline_bvec(req)) {
  247. bio = &req->b.inline_bio;
  248. bio_init(bio, req->ns->bdev, req->inline_bvec,
  249. ARRAY_SIZE(req->inline_bvec), opf);
  250. } else {
  251. bio = bio_alloc(req->ns->bdev, bio_max_segs(sg_cnt), opf,
  252. GFP_KERNEL);
  253. }
  254. bio->bi_iter.bi_sector = sector;
  255. bio->bi_private = req;
  256. bio->bi_end_io = nvmet_bio_done;
  257. blk_start_plug(&plug);
  258. if (req->metadata_len)
  259. sg_miter_start(&prot_miter, req->metadata_sg,
  260. req->metadata_sg_cnt, iter_flags);
  261. for_each_sg(req->sg, sg, req->sg_cnt, i) {
  262. while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
  263. != sg->length) {
  264. struct bio *prev = bio;
  265. if (req->metadata_len) {
  266. rc = nvmet_bdev_alloc_bip(req, bio,
  267. &prot_miter);
  268. if (unlikely(rc)) {
  269. bio_io_error(bio);
  270. return;
  271. }
  272. }
  273. bio = bio_alloc(req->ns->bdev, bio_max_segs(sg_cnt),
  274. opf, GFP_KERNEL);
  275. bio->bi_iter.bi_sector = sector;
  276. bio_chain(bio, prev);
  277. submit_bio(prev);
  278. }
  279. sector += sg->length >> 9;
  280. sg_cnt--;
  281. }
  282. if (req->metadata_len) {
  283. rc = nvmet_bdev_alloc_bip(req, bio, &prot_miter);
  284. if (unlikely(rc)) {
  285. bio_io_error(bio);
  286. return;
  287. }
  288. }
  289. submit_bio(bio);
  290. blk_finish_plug(&plug);
  291. }
  292. static void nvmet_bdev_execute_flush(struct nvmet_req *req)
  293. {
  294. struct bio *bio = &req->b.inline_bio;
  295. if (!bdev_write_cache(req->ns->bdev)) {
  296. nvmet_req_complete(req, NVME_SC_SUCCESS);
  297. return;
  298. }
  299. if (!nvmet_check_transfer_len(req, 0))
  300. return;
  301. bio_init(bio, req->ns->bdev, req->inline_bvec,
  302. ARRAY_SIZE(req->inline_bvec), REQ_OP_WRITE | REQ_PREFLUSH);
  303. bio->bi_private = req;
  304. bio->bi_end_io = nvmet_bio_done;
  305. submit_bio(bio);
  306. }
  307. u16 nvmet_bdev_flush(struct nvmet_req *req)
  308. {
  309. if (!bdev_write_cache(req->ns->bdev))
  310. return 0;
  311. if (blkdev_issue_flush(req->ns->bdev))
  312. return NVME_SC_INTERNAL | NVME_STATUS_DNR;
  313. return 0;
  314. }
  315. static void nvmet_bdev_execute_discard(struct nvmet_req *req)
  316. {
  317. struct nvmet_ns *ns = req->ns;
  318. struct nvme_dsm_range range;
  319. struct bio *bio = NULL;
  320. sector_t nr_sects;
  321. int i;
  322. u16 status = NVME_SC_SUCCESS;
  323. for (i = 0; i <= le32_to_cpu(req->cmd->dsm.nr); i++) {
  324. status = nvmet_copy_from_sgl(req, i * sizeof(range), &range,
  325. sizeof(range));
  326. if (status)
  327. break;
  328. nr_sects = le32_to_cpu(range.nlb) << (ns->blksize_shift - 9);
  329. __blkdev_issue_discard(ns->bdev,
  330. nvmet_lba_to_sect(ns, range.slba), nr_sects,
  331. GFP_KERNEL, &bio);
  332. }
  333. if (bio) {
  334. bio->bi_private = req;
  335. bio->bi_end_io = nvmet_bio_done;
  336. if (status)
  337. bio_io_error(bio);
  338. else
  339. submit_bio(bio);
  340. } else {
  341. nvmet_req_complete(req, status);
  342. }
  343. }
  344. static void nvmet_bdev_execute_dsm(struct nvmet_req *req)
  345. {
  346. if (!nvmet_check_data_len_lte(req, nvmet_dsm_len(req)))
  347. return;
  348. switch (le32_to_cpu(req->cmd->dsm.attributes)) {
  349. case NVME_DSMGMT_AD:
  350. nvmet_bdev_execute_discard(req);
  351. return;
  352. case NVME_DSMGMT_IDR:
  353. case NVME_DSMGMT_IDW:
  354. default:
  355. /* Not supported yet */
  356. nvmet_req_complete(req, 0);
  357. return;
  358. }
  359. }
  360. static void nvmet_bdev_execute_write_zeroes(struct nvmet_req *req)
  361. {
  362. struct nvme_write_zeroes_cmd *write_zeroes = &req->cmd->write_zeroes;
  363. struct bio *bio = NULL;
  364. sector_t sector;
  365. sector_t nr_sector;
  366. int ret;
  367. if (!nvmet_check_transfer_len(req, 0))
  368. return;
  369. sector = nvmet_lba_to_sect(req->ns, write_zeroes->slba);
  370. nr_sector = (((sector_t)le16_to_cpu(write_zeroes->length) + 1) <<
  371. (req->ns->blksize_shift - 9));
  372. ret = __blkdev_issue_zeroout(req->ns->bdev, sector, nr_sector,
  373. GFP_KERNEL, &bio, 0);
  374. if (bio) {
  375. bio->bi_private = req;
  376. bio->bi_end_io = nvmet_bio_done;
  377. submit_bio(bio);
  378. } else {
  379. nvmet_req_complete(req, errno_to_nvme_status(req, ret));
  380. }
  381. }
  382. u16 nvmet_bdev_parse_io_cmd(struct nvmet_req *req)
  383. {
  384. switch (req->cmd->common.opcode) {
  385. case nvme_cmd_read:
  386. case nvme_cmd_write:
  387. req->execute = nvmet_bdev_execute_rw;
  388. if (req->sq->ctrl->pi_support && nvmet_ns_has_pi(req->ns))
  389. req->metadata_len = nvmet_rw_metadata_len(req);
  390. return 0;
  391. case nvme_cmd_flush:
  392. req->execute = nvmet_bdev_execute_flush;
  393. return 0;
  394. case nvme_cmd_dsm:
  395. req->execute = nvmet_bdev_execute_dsm;
  396. return 0;
  397. case nvme_cmd_write_zeroes:
  398. req->execute = nvmet_bdev_execute_write_zeroes;
  399. return 0;
  400. default:
  401. return nvmet_report_invalid_opcode(req);
  402. }
  403. }