xref: /linux/drivers/nvme/target/zns.c (revision 55ab7e14222e5f0b0fd9f7711ca391d2924b35e3)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * NVMe ZNS-ZBD command implementation.
4  * Copyright (C) 2021 Western Digital Corporation or its affiliates.
5  */
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/nvme.h>
8 #include <linux/blkdev.h>
9 #include "nvmet.h"
10 
11 /*
12  * We set the Memory Page Size Minimum (MPSMIN) for target controller to 0
13  * which gets added by 12 in the nvme_enable_ctrl() which results in 2^12 = 4k
14  * as page_shift value. When calculating the ZASL use shift by 12.
15  */
16 #define NVMET_MPSMIN_SHIFT	12
17 
nvmet_zasl(unsigned int zone_append_sects)18 static inline u8 nvmet_zasl(unsigned int zone_append_sects)
19 {
20 	/*
21 	 * Zone Append Size Limit (zasl) is expressed as a power of 2 value
22 	 * with the minimum memory page size (i.e. 12) as unit.
23 	 */
24 	return ilog2(zone_append_sects >> (NVMET_MPSMIN_SHIFT - 9));
25 }
26 
validate_conv_zones_cb(struct blk_zone * z,unsigned int i,void * data)27 static int validate_conv_zones_cb(struct blk_zone *z,
28 				  unsigned int i, void *data)
29 {
30 	if (z->type == BLK_ZONE_TYPE_CONVENTIONAL)
31 		return -EOPNOTSUPP;
32 	return 0;
33 }
34 
nvmet_bdev_zns_enable(struct nvmet_ns * ns)35 bool nvmet_bdev_zns_enable(struct nvmet_ns *ns)
36 {
37 	u8 zasl = nvmet_zasl(bdev_max_zone_append_sectors(ns->bdev));
38 	struct gendisk *bd_disk = ns->bdev->bd_disk;
39 	int ret;
40 
41 	if (ns->subsys->zasl) {
42 		if (ns->subsys->zasl > zasl)
43 			return false;
44 	}
45 	ns->subsys->zasl = zasl;
46 
47 	/*
48 	 * Generic zoned block devices may have a smaller last zone which is
49 	 * not supported by ZNS. Exclude zoned drives that have such smaller
50 	 * last zone.
51 	 */
52 	if (get_capacity(bd_disk) & (bdev_zone_sectors(ns->bdev) - 1))
53 		return false;
54 	/*
55 	 * ZNS does not define a conventional zone type. Use report zones
56 	 * to detect if the device has conventional zones and reject it if
57 	 * it does.
58 	 */
59 	ret = blkdev_report_zones(ns->bdev, 0, bdev_nr_zones(ns->bdev),
60 				  validate_conv_zones_cb, NULL);
61 	if (ret < 0)
62 		return false;
63 
64 	ns->blksize_shift = blksize_bits(bdev_logical_block_size(ns->bdev));
65 
66 	return true;
67 }
68 
nvmet_execute_identify_ctrl_zns(struct nvmet_req * req)69 void nvmet_execute_identify_ctrl_zns(struct nvmet_req *req)
70 {
71 	u8 zasl = req->sq->ctrl->subsys->zasl;
72 	struct nvme_id_ctrl_zns *id;
73 	u16 status;
74 
75 	id = kzalloc_obj(*id);
76 	if (!id) {
77 		status = NVME_SC_INTERNAL;
78 		goto out;
79 	}
80 
81 	id->zasl = min_not_zero(nvmet_ctrl_mdts(req), zasl);
82 
83 	status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
84 
85 	kfree(id);
86 out:
87 	nvmet_req_complete(req, status);
88 }
89 
nvmet_execute_identify_ns_zns(struct nvmet_req * req)90 void nvmet_execute_identify_ns_zns(struct nvmet_req *req)
91 {
92 	struct nvme_id_ns_zns *id_zns = NULL;
93 	u64 zsze;
94 	u16 status;
95 	u32 mar, mor;
96 
97 	if (le32_to_cpu(req->cmd->identify.nsid) == NVME_NSID_ALL) {
98 		req->error_loc = offsetof(struct nvme_identify, nsid);
99 		status = NVME_SC_INVALID_NS | NVME_STATUS_DNR;
100 		goto out;
101 	}
102 
103 	id_zns = kzalloc_obj(*id_zns);
104 	if (!id_zns) {
105 		status = NVME_SC_INTERNAL;
106 		goto out;
107 	}
108 
109 	status = nvmet_req_find_ns(req);
110 	if (status)
111 		goto done;
112 
113 	if (nvmet_ns_revalidate(req->ns)) {
114 		mutex_lock(&req->ns->subsys->lock);
115 		nvmet_ns_changed(req->ns->subsys, req->ns->nsid);
116 		mutex_unlock(&req->ns->subsys->lock);
117 	}
118 
119 	if (!req->ns->bdev || !bdev_is_zoned(req->ns->bdev)) {
120 		status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
121 		req->error_loc = offsetof(struct nvme_identify, nsid);
122 		goto out;
123 	}
124 
125 	zsze = (bdev_zone_sectors(req->ns->bdev) << 9) >>
126 					req->ns->blksize_shift;
127 	id_zns->lbafe[0].zsze = cpu_to_le64(zsze);
128 
129 	mor = bdev_max_open_zones(req->ns->bdev);
130 	if (!mor)
131 		mor = U32_MAX;
132 	else
133 		mor--;
134 	id_zns->mor = cpu_to_le32(mor);
135 
136 	mar = bdev_max_active_zones(req->ns->bdev);
137 	if (!mar)
138 		mar = U32_MAX;
139 	else
140 		mar--;
141 	id_zns->mar = cpu_to_le32(mar);
142 
143 done:
144 	status = nvmet_copy_to_sgl(req, 0, id_zns, sizeof(*id_zns));
145 out:
146 	kfree(id_zns);
147 	nvmet_req_complete(req, status);
148 }
149 
nvmet_bdev_validate_zone_mgmt_recv(struct nvmet_req * req)150 static u16 nvmet_bdev_validate_zone_mgmt_recv(struct nvmet_req *req)
151 {
152 	sector_t sect = nvmet_lba_to_sect(req->ns, req->cmd->zmr.slba);
153 	u32 out_bufsize = (le32_to_cpu(req->cmd->zmr.numd) + 1) << 2;
154 
155 	if (sect >= get_capacity(req->ns->bdev->bd_disk)) {
156 		req->error_loc = offsetof(struct nvme_zone_mgmt_recv_cmd, slba);
157 		return NVME_SC_LBA_RANGE | NVME_STATUS_DNR;
158 	}
159 
160 	if (out_bufsize < sizeof(struct nvme_zone_report)) {
161 		req->error_loc = offsetof(struct nvme_zone_mgmt_recv_cmd, numd);
162 		return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
163 	}
164 
165 	if (req->cmd->zmr.zra != NVME_ZRA_ZONE_REPORT) {
166 		req->error_loc = offsetof(struct nvme_zone_mgmt_recv_cmd, zra);
167 		return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
168 	}
169 
170 	switch (req->cmd->zmr.pr) {
171 	case 0:
172 	case 1:
173 		break;
174 	default:
175 		req->error_loc = offsetof(struct nvme_zone_mgmt_recv_cmd, pr);
176 		return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
177 	}
178 
179 	switch (req->cmd->zmr.zrasf) {
180 	case NVME_ZRASF_ZONE_REPORT_ALL:
181 	case NVME_ZRASF_ZONE_STATE_EMPTY:
182 	case NVME_ZRASF_ZONE_STATE_IMP_OPEN:
183 	case NVME_ZRASF_ZONE_STATE_EXP_OPEN:
184 	case NVME_ZRASF_ZONE_STATE_CLOSED:
185 	case NVME_ZRASF_ZONE_STATE_FULL:
186 	case NVME_ZRASF_ZONE_STATE_READONLY:
187 	case NVME_ZRASF_ZONE_STATE_OFFLINE:
188 		break;
189 	default:
190 		req->error_loc =
191 			offsetof(struct nvme_zone_mgmt_recv_cmd, zrasf);
192 		return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
193 	}
194 
195 	return NVME_SC_SUCCESS;
196 }
197 
198 struct nvmet_report_zone_data {
199 	struct nvmet_req *req;
200 	u64 out_buf_offset;
201 	u64 out_nr_zones;
202 	u64 nr_zones;
203 	u8 zrasf;
204 };
205 
nvmet_bdev_report_zone_cb(struct blk_zone * z,unsigned i,void * d)206 static int nvmet_bdev_report_zone_cb(struct blk_zone *z, unsigned i, void *d)
207 {
208 	static const unsigned int nvme_zrasf_to_blk_zcond[] = {
209 		[NVME_ZRASF_ZONE_STATE_EMPTY]	 = BLK_ZONE_COND_EMPTY,
210 		[NVME_ZRASF_ZONE_STATE_IMP_OPEN] = BLK_ZONE_COND_IMP_OPEN,
211 		[NVME_ZRASF_ZONE_STATE_EXP_OPEN] = BLK_ZONE_COND_EXP_OPEN,
212 		[NVME_ZRASF_ZONE_STATE_CLOSED]	 = BLK_ZONE_COND_CLOSED,
213 		[NVME_ZRASF_ZONE_STATE_READONLY] = BLK_ZONE_COND_READONLY,
214 		[NVME_ZRASF_ZONE_STATE_FULL]	 = BLK_ZONE_COND_FULL,
215 		[NVME_ZRASF_ZONE_STATE_OFFLINE]	 = BLK_ZONE_COND_OFFLINE,
216 	};
217 	struct nvmet_report_zone_data *rz = d;
218 
219 	if (rz->zrasf != NVME_ZRASF_ZONE_REPORT_ALL &&
220 	    z->cond != nvme_zrasf_to_blk_zcond[rz->zrasf])
221 		return 0;
222 
223 	if (rz->nr_zones < rz->out_nr_zones) {
224 		struct nvme_zone_descriptor zdesc = { };
225 		u16 status;
226 
227 		zdesc.zcap = nvmet_sect_to_lba(rz->req->ns, z->capacity);
228 		zdesc.zslba = nvmet_sect_to_lba(rz->req->ns, z->start);
229 		zdesc.wp = nvmet_sect_to_lba(rz->req->ns, z->wp);
230 		zdesc.za = z->reset ? 1 << 2 : 0;
231 		zdesc.zs = z->cond << 4;
232 		zdesc.zt = z->type;
233 
234 		status = nvmet_copy_to_sgl(rz->req, rz->out_buf_offset, &zdesc,
235 					   sizeof(zdesc));
236 		if (status)
237 			return -EINVAL;
238 
239 		rz->out_buf_offset += sizeof(zdesc);
240 	}
241 
242 	rz->nr_zones++;
243 
244 	return 0;
245 }
246 
nvmet_req_nr_zones_from_slba(struct nvmet_req * req)247 static unsigned long nvmet_req_nr_zones_from_slba(struct nvmet_req *req)
248 {
249 	unsigned int sect = nvmet_lba_to_sect(req->ns, req->cmd->zmr.slba);
250 
251 	return bdev_nr_zones(req->ns->bdev) - bdev_zone_no(req->ns->bdev, sect);
252 }
253 
get_nr_zones_from_buf(struct nvmet_req * req,u32 bufsize)254 static unsigned long get_nr_zones_from_buf(struct nvmet_req *req, u32 bufsize)
255 {
256 	if (bufsize <= sizeof(struct nvme_zone_report))
257 		return 0;
258 
259 	return (bufsize - sizeof(struct nvme_zone_report)) /
260 		sizeof(struct nvme_zone_descriptor);
261 }
262 
nvmet_bdev_zone_zmgmt_recv_work(struct work_struct * w)263 static void nvmet_bdev_zone_zmgmt_recv_work(struct work_struct *w)
264 {
265 	struct nvmet_req *req = container_of(w, struct nvmet_req, z.zmgmt_work);
266 	sector_t start_sect = nvmet_lba_to_sect(req->ns, req->cmd->zmr.slba);
267 	unsigned long req_slba_nr_zones = nvmet_req_nr_zones_from_slba(req);
268 	u32 out_bufsize = (le32_to_cpu(req->cmd->zmr.numd) + 1) << 2;
269 	__le64 nr_zones;
270 	u16 status;
271 	int ret;
272 	struct nvmet_report_zone_data rz_data = {
273 		.out_nr_zones = get_nr_zones_from_buf(req, out_bufsize),
274 		/* leave the place for report zone header */
275 		.out_buf_offset = sizeof(struct nvme_zone_report),
276 		.zrasf = req->cmd->zmr.zrasf,
277 		.nr_zones = 0,
278 		.req = req,
279 	};
280 
281 	status = nvmet_bdev_validate_zone_mgmt_recv(req);
282 	if (status)
283 		goto out;
284 
285 	if (!req_slba_nr_zones) {
286 		status = NVME_SC_SUCCESS;
287 		goto out;
288 	}
289 
290 	ret = blkdev_report_zones(req->ns->bdev, start_sect, req_slba_nr_zones,
291 				 nvmet_bdev_report_zone_cb, &rz_data);
292 	if (ret < 0) {
293 		status = NVME_SC_INTERNAL;
294 		goto out;
295 	}
296 
297 	/*
298 	 * Partial report (PR bit set): the host accepts an incomplete listing,
299 	 * so cap Number of Zones to the descriptors that fit in the buffer.
300 	 * Full report (PR bit clear): Number of Zones is the match count; fail
301 	 * if the buffer cannot hold every matching zone descriptor.
302 	 */
303 	if (req->cmd->zmr.pr) {
304 		rz_data.nr_zones = min(rz_data.nr_zones, rz_data.out_nr_zones);
305 	} else if (rz_data.nr_zones > rz_data.out_nr_zones) {
306 		req->error_loc = offsetof(struct nvme_zone_mgmt_recv_cmd, numd);
307 		status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
308 		goto out;
309 	}
310 
311 	nr_zones = cpu_to_le64(rz_data.nr_zones);
312 	status = nvmet_copy_to_sgl(req, 0, &nr_zones, sizeof(nr_zones));
313 
314 out:
315 	nvmet_req_complete(req, status);
316 }
317 
nvmet_bdev_execute_zone_mgmt_recv(struct nvmet_req * req)318 void nvmet_bdev_execute_zone_mgmt_recv(struct nvmet_req *req)
319 {
320 	INIT_WORK(&req->z.zmgmt_work, nvmet_bdev_zone_zmgmt_recv_work);
321 	queue_work(zbd_wq, &req->z.zmgmt_work);
322 }
323 
zsa_req_op(u8 zsa)324 static inline enum req_op zsa_req_op(u8 zsa)
325 {
326 	switch (zsa) {
327 	case NVME_ZONE_OPEN:
328 		return REQ_OP_ZONE_OPEN;
329 	case NVME_ZONE_CLOSE:
330 		return REQ_OP_ZONE_CLOSE;
331 	case NVME_ZONE_FINISH:
332 		return REQ_OP_ZONE_FINISH;
333 	case NVME_ZONE_RESET:
334 		return REQ_OP_ZONE_RESET;
335 	default:
336 		return REQ_OP_LAST;
337 	}
338 }
339 
blkdev_zone_mgmt_errno_to_nvme_status(int ret)340 static u16 blkdev_zone_mgmt_errno_to_nvme_status(int ret)
341 {
342 	switch (ret) {
343 	case 0:
344 		return NVME_SC_SUCCESS;
345 	case -EINVAL:
346 	case -EIO:
347 		return NVME_SC_ZONE_INVALID_TRANSITION | NVME_STATUS_DNR;
348 	default:
349 		return NVME_SC_INTERNAL;
350 	}
351 }
352 
353 struct nvmet_zone_mgmt_send_all_data {
354 	unsigned long *zbitmap;
355 	struct nvmet_req *req;
356 };
357 
zmgmt_send_scan_cb(struct blk_zone * z,unsigned i,void * d)358 static int zmgmt_send_scan_cb(struct blk_zone *z, unsigned i, void *d)
359 {
360 	struct nvmet_zone_mgmt_send_all_data *data = d;
361 
362 	switch (zsa_req_op(data->req->cmd->zms.zsa)) {
363 	case REQ_OP_ZONE_OPEN:
364 		switch (z->cond) {
365 		case BLK_ZONE_COND_CLOSED:
366 			break;
367 		default:
368 			return 0;
369 		}
370 		break;
371 	case REQ_OP_ZONE_CLOSE:
372 		switch (z->cond) {
373 		case BLK_ZONE_COND_IMP_OPEN:
374 		case BLK_ZONE_COND_EXP_OPEN:
375 			break;
376 		default:
377 			return 0;
378 		}
379 		break;
380 	case REQ_OP_ZONE_FINISH:
381 		switch (z->cond) {
382 		case BLK_ZONE_COND_IMP_OPEN:
383 		case BLK_ZONE_COND_EXP_OPEN:
384 		case BLK_ZONE_COND_CLOSED:
385 			break;
386 		default:
387 			return 0;
388 		}
389 		break;
390 	default:
391 		return -EINVAL;
392 	}
393 
394 	set_bit(i, data->zbitmap);
395 
396 	return 0;
397 }
398 
nvmet_bdev_zone_mgmt_emulate_all(struct nvmet_req * req)399 static u16 nvmet_bdev_zone_mgmt_emulate_all(struct nvmet_req *req)
400 {
401 	struct block_device *bdev = req->ns->bdev;
402 	unsigned int nr_zones = bdev_nr_zones(bdev);
403 	struct bio *bio = NULL;
404 	sector_t sector = 0;
405 	int ret;
406 	struct nvmet_zone_mgmt_send_all_data d = {
407 		.req = req,
408 	};
409 
410 	d.zbitmap = kcalloc_node(BITS_TO_LONGS(nr_zones), sizeof(*(d.zbitmap)),
411 				 GFP_NOIO, bdev->bd_disk->node_id);
412 	if (!d.zbitmap) {
413 		ret = -ENOMEM;
414 		goto out;
415 	}
416 
417 	/* Scan and build bitmap of the eligible zones */
418 	ret = blkdev_report_zones(bdev, 0, nr_zones, zmgmt_send_scan_cb, &d);
419 	if (ret != nr_zones) {
420 		if (ret > 0)
421 			ret = -EIO;
422 		goto out;
423 	} else {
424 		/* We scanned all the zones */
425 		ret = 0;
426 	}
427 
428 	while (sector < bdev_nr_sectors(bdev)) {
429 		if (test_bit(disk_zone_no(bdev->bd_disk, sector), d.zbitmap)) {
430 			bio = blk_next_bio(bio, bdev, 0,
431 				zsa_req_op(req->cmd->zms.zsa) | REQ_SYNC,
432 				GFP_KERNEL);
433 			bio->bi_iter.bi_sector = sector;
434 			/* This may take a while, so be nice to others */
435 			cond_resched();
436 		}
437 		sector += bdev_zone_sectors(bdev);
438 	}
439 
440 	if (bio) {
441 		ret = submit_bio_wait(bio);
442 		bio_put(bio);
443 	}
444 
445 out:
446 	kfree(d.zbitmap);
447 
448 	return blkdev_zone_mgmt_errno_to_nvme_status(ret);
449 }
450 
nvmet_bdev_execute_zmgmt_send_all(struct nvmet_req * req)451 static u16 nvmet_bdev_execute_zmgmt_send_all(struct nvmet_req *req)
452 {
453 	int ret;
454 
455 	switch (zsa_req_op(req->cmd->zms.zsa)) {
456 	case REQ_OP_ZONE_RESET:
457 		ret = blkdev_zone_mgmt(req->ns->bdev, REQ_OP_ZONE_RESET, 0,
458 				       get_capacity(req->ns->bdev->bd_disk));
459 		if (ret < 0)
460 			return blkdev_zone_mgmt_errno_to_nvme_status(ret);
461 		break;
462 	case REQ_OP_ZONE_OPEN:
463 	case REQ_OP_ZONE_CLOSE:
464 	case REQ_OP_ZONE_FINISH:
465 		return nvmet_bdev_zone_mgmt_emulate_all(req);
466 	default:
467 		/* this is needed to quiet compiler warning */
468 		req->error_loc = offsetof(struct nvme_zone_mgmt_send_cmd, zsa);
469 		return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
470 	}
471 
472 	return NVME_SC_SUCCESS;
473 }
474 
nvmet_bdev_zmgmt_send_work(struct work_struct * w)475 static void nvmet_bdev_zmgmt_send_work(struct work_struct *w)
476 {
477 	struct nvmet_req *req = container_of(w, struct nvmet_req, z.zmgmt_work);
478 	sector_t sect = nvmet_lba_to_sect(req->ns, req->cmd->zms.slba);
479 	enum req_op op = zsa_req_op(req->cmd->zms.zsa);
480 	struct block_device *bdev = req->ns->bdev;
481 	sector_t zone_sectors = bdev_zone_sectors(bdev);
482 	u16 status = NVME_SC_SUCCESS;
483 	int ret;
484 
485 	if (op == REQ_OP_LAST) {
486 		req->error_loc = offsetof(struct nvme_zone_mgmt_send_cmd, zsa);
487 		status = NVME_SC_ZONE_INVALID_TRANSITION | NVME_STATUS_DNR;
488 		goto out;
489 	}
490 
491 	/* when select all bit is set slba field is ignored */
492 	if (req->cmd->zms.select_all) {
493 		status = nvmet_bdev_execute_zmgmt_send_all(req);
494 		goto out;
495 	}
496 
497 	if (sect >= get_capacity(bdev->bd_disk)) {
498 		req->error_loc = offsetof(struct nvme_zone_mgmt_send_cmd, slba);
499 		status = NVME_SC_LBA_RANGE | NVME_STATUS_DNR;
500 		goto out;
501 	}
502 
503 	if (sect & (zone_sectors - 1)) {
504 		req->error_loc = offsetof(struct nvme_zone_mgmt_send_cmd, slba);
505 		status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
506 		goto out;
507 	}
508 
509 	ret = blkdev_zone_mgmt(bdev, op, sect, zone_sectors);
510 	if (ret < 0)
511 		status = blkdev_zone_mgmt_errno_to_nvme_status(ret);
512 
513 out:
514 	nvmet_req_complete(req, status);
515 }
516 
nvmet_bdev_execute_zone_mgmt_send(struct nvmet_req * req)517 void nvmet_bdev_execute_zone_mgmt_send(struct nvmet_req *req)
518 {
519 	INIT_WORK(&req->z.zmgmt_work, nvmet_bdev_zmgmt_send_work);
520 	queue_work(zbd_wq, &req->z.zmgmt_work);
521 }
522 
nvmet_bdev_zone_append_bio_done(struct bio * bio)523 static void nvmet_bdev_zone_append_bio_done(struct bio *bio)
524 {
525 	struct nvmet_req *req = bio->bi_private;
526 
527 	if (bio->bi_status == BLK_STS_OK) {
528 		req->cqe->result.u64 =
529 			nvmet_sect_to_lba(req->ns, bio->bi_iter.bi_sector);
530 	}
531 
532 	nvmet_req_complete(req, blk_to_nvme_status(req, bio->bi_status));
533 	nvmet_req_bio_put(req, bio);
534 }
535 
nvmet_bdev_execute_zone_append(struct nvmet_req * req)536 void nvmet_bdev_execute_zone_append(struct nvmet_req *req)
537 {
538 	sector_t sect = nvmet_lba_to_sect(req->ns, req->cmd->rw.slba);
539 	const blk_opf_t opf = REQ_OP_ZONE_APPEND | REQ_SYNC | REQ_IDLE;
540 	u16 status = NVME_SC_SUCCESS;
541 	unsigned int total_len = 0;
542 	struct scatterlist *sg;
543 	u32 data_len = nvmet_rw_data_len(req);
544 	struct bio *bio;
545 	int sg_cnt;
546 
547 	/* Request is completed on len mismatch in nvmet_check_transfer_len() */
548 	if (!nvmet_check_transfer_len(req, nvmet_rw_data_len(req)))
549 		return;
550 
551 	if (data_len >
552 	    bdev_max_zone_append_sectors(req->ns->bdev) << SECTOR_SHIFT) {
553 		req->error_loc = offsetof(struct nvme_rw_command, length);
554 		status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
555 		goto out;
556 	}
557 
558 	if (!req->sg_cnt) {
559 		nvmet_req_complete(req, 0);
560 		return;
561 	}
562 
563 	if (sect >= get_capacity(req->ns->bdev->bd_disk)) {
564 		req->error_loc = offsetof(struct nvme_rw_command, slba);
565 		status = NVME_SC_LBA_RANGE | NVME_STATUS_DNR;
566 		goto out;
567 	}
568 
569 	if (sect & (bdev_zone_sectors(req->ns->bdev) - 1)) {
570 		req->error_loc = offsetof(struct nvme_rw_command, slba);
571 		status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
572 		goto out;
573 	}
574 
575 	if (nvmet_use_inline_bvec(req)) {
576 		bio = &req->z.inline_bio;
577 		bio_init(bio, req->ns->bdev, req->inline_bvec,
578 			 ARRAY_SIZE(req->inline_bvec), opf);
579 	} else {
580 		bio = bio_alloc(req->ns->bdev, req->sg_cnt, opf, GFP_KERNEL);
581 	}
582 
583 	bio->bi_end_io = nvmet_bdev_zone_append_bio_done;
584 	bio->bi_iter.bi_sector = sect;
585 	bio->bi_private = req;
586 	if (req->cmd->rw.control & cpu_to_le16(NVME_RW_FUA))
587 		bio->bi_opf |= REQ_FUA;
588 
589 	for_each_sg(req->sg, sg, req->sg_cnt, sg_cnt) {
590 		unsigned int len = sg->length;
591 
592 		if (bio_add_page(bio, sg_page(sg), len, sg->offset) != len) {
593 			status = NVME_SC_INTERNAL;
594 			goto out_put_bio;
595 		}
596 		total_len += len;
597 	}
598 
599 	if (total_len != data_len) {
600 		status = NVME_SC_INTERNAL | NVME_STATUS_DNR;
601 		goto out_put_bio;
602 	}
603 
604 	submit_bio(bio);
605 	return;
606 
607 out_put_bio:
608 	nvmet_req_bio_put(req, bio);
609 out:
610 	nvmet_req_complete(req, status);
611 }
612 
nvmet_bdev_zns_parse_io_cmd(struct nvmet_req * req)613 u16 nvmet_bdev_zns_parse_io_cmd(struct nvmet_req *req)
614 {
615 	struct nvme_command *cmd = req->cmd;
616 
617 	switch (cmd->common.opcode) {
618 	case nvme_cmd_zone_append:
619 		req->execute = nvmet_bdev_execute_zone_append;
620 		return 0;
621 	case nvme_cmd_zone_mgmt_recv:
622 		req->execute = nvmet_bdev_execute_zone_mgmt_recv;
623 		return 0;
624 	case nvme_cmd_zone_mgmt_send:
625 		req->execute = nvmet_bdev_execute_zone_mgmt_send;
626 		return 0;
627 	default:
628 		return nvmet_bdev_parse_io_cmd(req);
629 	}
630 }
631