io-cmd-bdev.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. }
  47. void nvmet_bdev_ns_disable(struct nvmet_ns *ns)
  48. {
  49. if (ns->bdev_file) {
  50. fput(ns->bdev_file);
  51. ns->bdev = NULL;
  52. ns->bdev_file = NULL;
  53. }
  54. }
  55. static void nvmet_bdev_ns_enable_integrity(struct nvmet_ns *ns)
  56. {
  57. struct blk_integrity *bi = bdev_get_integrity(ns->bdev);
  58. if (!bi)
  59. return;
  60. if (bi->csum_type == BLK_INTEGRITY_CSUM_CRC) {
  61. ns->metadata_size = bi->tuple_size;
  62. if (bi->flags & BLK_INTEGRITY_REF_TAG)
  63. ns->pi_type = NVME_NS_DPS_PI_TYPE1;
  64. else
  65. ns->pi_type = NVME_NS_DPS_PI_TYPE3;
  66. } else {
  67. ns->metadata_size = 0;
  68. }
  69. }
  70. int nvmet_bdev_ns_enable(struct nvmet_ns *ns)
  71. {
  72. int ret;
  73. /*
  74. * When buffered_io namespace attribute is enabled that means user want
  75. * this block device to be used as a file, so block device can take
  76. * an advantage of cache.
  77. */
  78. if (ns->buffered_io)
  79. return -ENOTBLK;
  80. ns->bdev_file = bdev_file_open_by_path(ns->device_path,
  81. BLK_OPEN_READ | BLK_OPEN_WRITE, NULL, NULL);
  82. if (IS_ERR(ns->bdev_file)) {
  83. ret = PTR_ERR(ns->bdev_file);
  84. if (ret != -ENOTBLK) {
  85. pr_err("failed to open block device %s: (%d)\n",
  86. ns->device_path, ret);
  87. }
  88. ns->bdev_file = NULL;
  89. return ret;
  90. }
  91. ns->bdev = file_bdev(ns->bdev_file);
  92. ns->size = bdev_nr_bytes(ns->bdev);
  93. ns->blksize_shift = blksize_bits(bdev_logical_block_size(ns->bdev));
  94. ns->pi_type = 0;
  95. ns->metadata_size = 0;
  96. if (IS_ENABLED(CONFIG_BLK_DEV_INTEGRITY))
  97. nvmet_bdev_ns_enable_integrity(ns);
  98. if (bdev_is_zoned(ns->bdev)) {
  99. if (!nvmet_bdev_zns_enable(ns)) {
  100. nvmet_bdev_ns_disable(ns);
  101. return -EINVAL;
  102. }
  103. ns->csi = NVME_CSI_ZNS;
  104. }
  105. return 0;
  106. }
  107. void nvmet_bdev_ns_revalidate(struct nvmet_ns *ns)
  108. {
  109. ns->size = bdev_nr_bytes(ns->bdev);
  110. }
  111. u16 blk_to_nvme_status(struct nvmet_req *req, blk_status_t blk_sts)
  112. {
  113. u16 status = NVME_SC_SUCCESS;
  114. if (likely(blk_sts == BLK_STS_OK))
  115. return status;
  116. /*
  117. * Right now there exists M : 1 mapping between block layer error
  118. * to the NVMe status code (see nvme_error_status()). For consistency,
  119. * when we reverse map we use most appropriate NVMe Status code from
  120. * the group of the NVMe staus codes used in the nvme_error_status().
  121. */
  122. switch (blk_sts) {
  123. case BLK_STS_NOSPC:
  124. status = NVME_SC_CAP_EXCEEDED | NVME_STATUS_DNR;
  125. req->error_loc = offsetof(struct nvme_rw_command, length);
  126. break;
  127. case BLK_STS_TARGET:
  128. status = NVME_SC_LBA_RANGE | NVME_STATUS_DNR;
  129. req->error_loc = offsetof(struct nvme_rw_command, slba);
  130. break;
  131. case BLK_STS_NOTSUPP:
  132. status = NVME_SC_INVALID_OPCODE | NVME_STATUS_DNR;
  133. req->error_loc = offsetof(struct nvme_common_command, opcode);
  134. break;
  135. case BLK_STS_MEDIUM:
  136. status = NVME_SC_ACCESS_DENIED;
  137. req->error_loc = offsetof(struct nvme_rw_command, nsid);
  138. break;
  139. case BLK_STS_IOERR:
  140. default:
  141. status = NVME_SC_INTERNAL | NVME_STATUS_DNR;
  142. req->error_loc = offsetof(struct nvme_common_command, opcode);
  143. }
  144. switch (req->cmd->common.opcode) {
  145. case nvme_cmd_read:
  146. case nvme_cmd_write:
  147. req->error_slba = le64_to_cpu(req->cmd->rw.slba);
  148. break;
  149. case nvme_cmd_write_zeroes:
  150. req->error_slba =
  151. le64_to_cpu(req->cmd->write_zeroes.slba);
  152. break;
  153. default:
  154. req->error_slba = 0;
  155. }
  156. return status;
  157. }
  158. static void nvmet_bio_done(struct bio *bio)
  159. {
  160. struct nvmet_req *req = bio->bi_private;
  161. nvmet_req_complete(req, blk_to_nvme_status(req, bio->bi_status));
  162. nvmet_req_bio_put(req, bio);
  163. }
  164. #ifdef CONFIG_BLK_DEV_INTEGRITY
  165. static int nvmet_bdev_alloc_bip(struct nvmet_req *req, struct bio *bio,
  166. struct sg_mapping_iter *miter)
  167. {
  168. struct blk_integrity *bi;
  169. struct bio_integrity_payload *bip;
  170. int rc;
  171. size_t resid, len;
  172. bi = bdev_get_integrity(req->ns->bdev);
  173. if (unlikely(!bi)) {
  174. pr_err("Unable to locate bio_integrity\n");
  175. return -ENODEV;
  176. }
  177. bip = bio_integrity_alloc(bio, GFP_NOIO,
  178. bio_max_segs(req->metadata_sg_cnt));
  179. if (IS_ERR(bip)) {
  180. pr_err("Unable to allocate bio_integrity_payload\n");
  181. return PTR_ERR(bip);
  182. }
  183. /* virtual start sector must be in integrity interval units */
  184. bip_set_seed(bip, bio->bi_iter.bi_sector >>
  185. (bi->interval_exp - SECTOR_SHIFT));
  186. resid = bio_integrity_bytes(bi, bio_sectors(bio));
  187. while (resid > 0 && sg_miter_next(miter)) {
  188. len = min_t(size_t, miter->length, resid);
  189. rc = bio_integrity_add_page(bio, miter->page, len,
  190. offset_in_page(miter->addr));
  191. if (unlikely(rc != len)) {
  192. pr_err("bio_integrity_add_page() failed; %d\n", rc);
  193. sg_miter_stop(miter);
  194. return -ENOMEM;
  195. }
  196. resid -= len;
  197. if (len < miter->length)
  198. miter->consumed -= miter->length - len;
  199. }
  200. sg_miter_stop(miter);
  201. return 0;
  202. }
  203. #else
  204. static int nvmet_bdev_alloc_bip(struct nvmet_req *req, struct bio *bio,
  205. struct sg_mapping_iter *miter)
  206. {
  207. return -EINVAL;
  208. }
  209. #endif /* CONFIG_BLK_DEV_INTEGRITY */
  210. static void nvmet_bdev_execute_rw(struct nvmet_req *req)
  211. {
  212. unsigned int sg_cnt = req->sg_cnt;
  213. struct bio *bio;
  214. struct scatterlist *sg;
  215. struct blk_plug plug;
  216. sector_t sector;
  217. blk_opf_t opf;
  218. int i, rc;
  219. struct sg_mapping_iter prot_miter;
  220. unsigned int iter_flags;
  221. unsigned int total_len = nvmet_rw_data_len(req) + req->metadata_len;
  222. if (!nvmet_check_transfer_len(req, total_len))
  223. return;
  224. if (!req->sg_cnt) {
  225. nvmet_req_complete(req, 0);
  226. return;
  227. }
  228. if (req->cmd->rw.opcode == nvme_cmd_write) {
  229. opf = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
  230. if (req->cmd->rw.control & cpu_to_le16(NVME_RW_FUA))
  231. opf |= REQ_FUA;
  232. iter_flags = SG_MITER_TO_SG;
  233. } else {
  234. opf = REQ_OP_READ;
  235. iter_flags = SG_MITER_FROM_SG;
  236. }
  237. if (is_pci_p2pdma_page(sg_page(req->sg)))
  238. opf |= REQ_NOMERGE;
  239. sector = nvmet_lba_to_sect(req->ns, req->cmd->rw.slba);
  240. if (nvmet_use_inline_bvec(req)) {
  241. bio = &req->b.inline_bio;
  242. bio_init(bio, req->ns->bdev, req->inline_bvec,
  243. ARRAY_SIZE(req->inline_bvec), opf);
  244. } else {
  245. bio = bio_alloc(req->ns->bdev, bio_max_segs(sg_cnt), opf,
  246. GFP_KERNEL);
  247. }
  248. bio->bi_iter.bi_sector = sector;
  249. bio->bi_private = req;
  250. bio->bi_end_io = nvmet_bio_done;
  251. blk_start_plug(&plug);
  252. if (req->metadata_len)
  253. sg_miter_start(&prot_miter, req->metadata_sg,
  254. req->metadata_sg_cnt, iter_flags);
  255. for_each_sg(req->sg, sg, req->sg_cnt, i) {
  256. while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
  257. != sg->length) {
  258. struct bio *prev = bio;
  259. if (req->metadata_len) {
  260. rc = nvmet_bdev_alloc_bip(req, bio,
  261. &prot_miter);
  262. if (unlikely(rc)) {
  263. bio_io_error(bio);
  264. return;
  265. }
  266. }
  267. bio = bio_alloc(req->ns->bdev, bio_max_segs(sg_cnt),
  268. opf, GFP_KERNEL);
  269. bio->bi_iter.bi_sector = sector;
  270. bio_chain(bio, prev);
  271. submit_bio(prev);
  272. }
  273. sector += sg->length >> 9;
  274. sg_cnt--;
  275. }
  276. if (req->metadata_len) {
  277. rc = nvmet_bdev_alloc_bip(req, bio, &prot_miter);
  278. if (unlikely(rc)) {
  279. bio_io_error(bio);
  280. return;
  281. }
  282. }
  283. submit_bio(bio);
  284. blk_finish_plug(&plug);
  285. }
  286. static void nvmet_bdev_execute_flush(struct nvmet_req *req)
  287. {
  288. struct bio *bio = &req->b.inline_bio;
  289. if (!bdev_write_cache(req->ns->bdev)) {
  290. nvmet_req_complete(req, NVME_SC_SUCCESS);
  291. return;
  292. }
  293. if (!nvmet_check_transfer_len(req, 0))
  294. return;
  295. bio_init(bio, req->ns->bdev, req->inline_bvec,
  296. ARRAY_SIZE(req->inline_bvec), REQ_OP_WRITE | REQ_PREFLUSH);
  297. bio->bi_private = req;
  298. bio->bi_end_io = nvmet_bio_done;
  299. submit_bio(bio);
  300. }
  301. u16 nvmet_bdev_flush(struct nvmet_req *req)
  302. {
  303. if (!bdev_write_cache(req->ns->bdev))
  304. return 0;
  305. if (blkdev_issue_flush(req->ns->bdev))
  306. return NVME_SC_INTERNAL | NVME_STATUS_DNR;
  307. return 0;
  308. }
  309. static u16 nvmet_bdev_discard_range(struct nvmet_req *req,
  310. struct nvme_dsm_range *range, struct bio **bio)
  311. {
  312. struct nvmet_ns *ns = req->ns;
  313. int ret;
  314. ret = __blkdev_issue_discard(ns->bdev,
  315. nvmet_lba_to_sect(ns, range->slba),
  316. le32_to_cpu(range->nlb) << (ns->blksize_shift - 9),
  317. GFP_KERNEL, bio);
  318. if (ret && ret != -EOPNOTSUPP) {
  319. req->error_slba = le64_to_cpu(range->slba);
  320. return errno_to_nvme_status(req, ret);
  321. }
  322. return NVME_SC_SUCCESS;
  323. }
  324. static void nvmet_bdev_execute_discard(struct nvmet_req *req)
  325. {
  326. struct nvme_dsm_range range;
  327. struct bio *bio = NULL;
  328. int i;
  329. u16 status;
  330. for (i = 0; i <= le32_to_cpu(req->cmd->dsm.nr); i++) {
  331. status = nvmet_copy_from_sgl(req, i * sizeof(range), &range,
  332. sizeof(range));
  333. if (status)
  334. break;
  335. status = nvmet_bdev_discard_range(req, &range, &bio);
  336. if (status)
  337. break;
  338. }
  339. if (bio) {
  340. bio->bi_private = req;
  341. bio->bi_end_io = nvmet_bio_done;
  342. if (status)
  343. bio_io_error(bio);
  344. else
  345. submit_bio(bio);
  346. } else {
  347. nvmet_req_complete(req, status);
  348. }
  349. }
  350. static void nvmet_bdev_execute_dsm(struct nvmet_req *req)
  351. {
  352. if (!nvmet_check_data_len_lte(req, nvmet_dsm_len(req)))
  353. return;
  354. switch (le32_to_cpu(req->cmd->dsm.attributes)) {
  355. case NVME_DSMGMT_AD:
  356. nvmet_bdev_execute_discard(req);
  357. return;
  358. case NVME_DSMGMT_IDR:
  359. case NVME_DSMGMT_IDW:
  360. default:
  361. /* Not supported yet */
  362. nvmet_req_complete(req, 0);
  363. return;
  364. }
  365. }
  366. static void nvmet_bdev_execute_write_zeroes(struct nvmet_req *req)
  367. {
  368. struct nvme_write_zeroes_cmd *write_zeroes = &req->cmd->write_zeroes;
  369. struct bio *bio = NULL;
  370. sector_t sector;
  371. sector_t nr_sector;
  372. int ret;
  373. if (!nvmet_check_transfer_len(req, 0))
  374. return;
  375. sector = nvmet_lba_to_sect(req->ns, write_zeroes->slba);
  376. nr_sector = (((sector_t)le16_to_cpu(write_zeroes->length) + 1) <<
  377. (req->ns->blksize_shift - 9));
  378. ret = __blkdev_issue_zeroout(req->ns->bdev, sector, nr_sector,
  379. GFP_KERNEL, &bio, 0);
  380. if (bio) {
  381. bio->bi_private = req;
  382. bio->bi_end_io = nvmet_bio_done;
  383. submit_bio(bio);
  384. } else {
  385. nvmet_req_complete(req, errno_to_nvme_status(req, ret));
  386. }
  387. }
  388. u16 nvmet_bdev_parse_io_cmd(struct nvmet_req *req)
  389. {
  390. switch (req->cmd->common.opcode) {
  391. case nvme_cmd_read:
  392. case nvme_cmd_write:
  393. req->execute = nvmet_bdev_execute_rw;
  394. if (req->sq->ctrl->pi_support && nvmet_ns_has_pi(req->ns))
  395. req->metadata_len = nvmet_rw_metadata_len(req);
  396. return 0;
  397. case nvme_cmd_flush:
  398. req->execute = nvmet_bdev_execute_flush;
  399. return 0;
  400. case nvme_cmd_dsm:
  401. req->execute = nvmet_bdev_execute_dsm;
  402. return 0;
  403. case nvme_cmd_write_zeroes:
  404. req->execute = nvmet_bdev_execute_write_zeroes;
  405. return 0;
  406. default:
  407. return nvmet_report_invalid_opcode(req);
  408. }
  409. }