• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 (!bdev_is_zone_start(bd_disk->part0, get_capacity(bd_disk)))
53 		return false;
54 	/*
55 	 * ZNS does not define a conventional zone type. If the underlying
56 	 * device has a bitmap set indicating the existence of conventional
57 	 * zones, reject the device. Otherwise, use report zones to detect if
58 	 * the device has conventional zones.
59 	 */
60 	if (ns->bdev->bd_disk->queue->conv_zones_bitmap)
61 		return false;
62 
63 	ret = blkdev_report_zones(ns->bdev, 0, blkdev_nr_zones(bd_disk),
64 				  validate_conv_zones_cb, NULL);
65 	if (ret < 0)
66 		return false;
67 
68 	ns->blksize_shift = blksize_bits(bdev_logical_block_size(ns->bdev));
69 
70 	return true;
71 }
72 
nvmet_execute_identify_ctrl_zns(struct nvmet_req * req)73 void nvmet_execute_identify_ctrl_zns(struct nvmet_req *req)
74 {
75 	u8 zasl = req->sq->ctrl->subsys->zasl;
76 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
77 	struct nvme_id_ctrl_zns *id;
78 	u16 status;
79 
80 	id = kzalloc(sizeof(*id), GFP_KERNEL);
81 	if (!id) {
82 		status = NVME_SC_INTERNAL;
83 		goto out;
84 	}
85 
86 	if (ctrl->ops->get_mdts)
87 		id->zasl = min_t(u8, ctrl->ops->get_mdts(ctrl), zasl);
88 	else
89 		id->zasl = zasl;
90 
91 	status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
92 
93 	kfree(id);
94 out:
95 	nvmet_req_complete(req, status);
96 }
97 
nvmet_execute_identify_cns_cs_ns(struct nvmet_req * req)98 void nvmet_execute_identify_cns_cs_ns(struct nvmet_req *req)
99 {
100 	struct nvme_id_ns_zns *id_zns = NULL;
101 	u64 zsze;
102 	u16 status;
103 	u32 mar, mor;
104 
105 	if (le32_to_cpu(req->cmd->identify.nsid) == NVME_NSID_ALL) {
106 		req->error_loc = offsetof(struct nvme_identify, nsid);
107 		status = NVME_SC_INVALID_NS | NVME_SC_DNR;
108 		goto out;
109 	}
110 
111 	id_zns = kzalloc(sizeof(*id_zns), GFP_KERNEL);
112 	if (!id_zns) {
113 		status = NVME_SC_INTERNAL;
114 		goto out;
115 	}
116 
117 	status = nvmet_req_find_ns(req);
118 	if (status)
119 		goto done;
120 
121 	if (nvmet_ns_revalidate(req->ns)) {
122 		mutex_lock(&req->ns->subsys->lock);
123 		nvmet_ns_changed(req->ns->subsys, req->ns->nsid);
124 		mutex_unlock(&req->ns->subsys->lock);
125 	}
126 
127 	if (!bdev_is_zoned(req->ns->bdev)) {
128 		status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
129 		req->error_loc = offsetof(struct nvme_identify, nsid);
130 		goto out;
131 	}
132 
133 	zsze = (bdev_zone_sectors(req->ns->bdev) << 9) >>
134 					req->ns->blksize_shift;
135 	id_zns->lbafe[0].zsze = cpu_to_le64(zsze);
136 
137 	mor = bdev_max_open_zones(req->ns->bdev);
138 	if (!mor)
139 		mor = U32_MAX;
140 	else
141 		mor--;
142 	id_zns->mor = cpu_to_le32(mor);
143 
144 	mar = bdev_max_active_zones(req->ns->bdev);
145 	if (!mar)
146 		mar = U32_MAX;
147 	else
148 		mar--;
149 	id_zns->mar = cpu_to_le32(mar);
150 
151 done:
152 	status = nvmet_copy_to_sgl(req, 0, id_zns, sizeof(*id_zns));
153 out:
154 	kfree(id_zns);
155 	nvmet_req_complete(req, status);
156 }
157 
nvmet_bdev_validate_zone_mgmt_recv(struct nvmet_req * req)158 static u16 nvmet_bdev_validate_zone_mgmt_recv(struct nvmet_req *req)
159 {
160 	sector_t sect = nvmet_lba_to_sect(req->ns, req->cmd->zmr.slba);
161 	u32 out_bufsize = (le32_to_cpu(req->cmd->zmr.numd) + 1) << 2;
162 
163 	if (sect >= get_capacity(req->ns->bdev->bd_disk)) {
164 		req->error_loc = offsetof(struct nvme_zone_mgmt_recv_cmd, slba);
165 		return NVME_SC_LBA_RANGE | NVME_SC_DNR;
166 	}
167 
168 	if (out_bufsize < sizeof(struct nvme_zone_report)) {
169 		req->error_loc = offsetof(struct nvme_zone_mgmt_recv_cmd, numd);
170 		return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
171 	}
172 
173 	if (req->cmd->zmr.zra != NVME_ZRA_ZONE_REPORT) {
174 		req->error_loc = offsetof(struct nvme_zone_mgmt_recv_cmd, zra);
175 		return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
176 	}
177 
178 	switch (req->cmd->zmr.pr) {
179 	case 0:
180 	case 1:
181 		break;
182 	default:
183 		req->error_loc = offsetof(struct nvme_zone_mgmt_recv_cmd, pr);
184 		return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
185 	}
186 
187 	switch (req->cmd->zmr.zrasf) {
188 	case NVME_ZRASF_ZONE_REPORT_ALL:
189 	case NVME_ZRASF_ZONE_STATE_EMPTY:
190 	case NVME_ZRASF_ZONE_STATE_IMP_OPEN:
191 	case NVME_ZRASF_ZONE_STATE_EXP_OPEN:
192 	case NVME_ZRASF_ZONE_STATE_CLOSED:
193 	case NVME_ZRASF_ZONE_STATE_FULL:
194 	case NVME_ZRASF_ZONE_STATE_READONLY:
195 	case NVME_ZRASF_ZONE_STATE_OFFLINE:
196 		break;
197 	default:
198 		req->error_loc =
199 			offsetof(struct nvme_zone_mgmt_recv_cmd, zrasf);
200 		return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
201 	}
202 
203 	return NVME_SC_SUCCESS;
204 }
205 
206 struct nvmet_report_zone_data {
207 	struct nvmet_req *req;
208 	u64 out_buf_offset;
209 	u64 out_nr_zones;
210 	u64 nr_zones;
211 	u8 zrasf;
212 };
213 
nvmet_bdev_report_zone_cb(struct blk_zone * z,unsigned i,void * d)214 static int nvmet_bdev_report_zone_cb(struct blk_zone *z, unsigned i, void *d)
215 {
216 	static const unsigned int nvme_zrasf_to_blk_zcond[] = {
217 		[NVME_ZRASF_ZONE_STATE_EMPTY]	 = BLK_ZONE_COND_EMPTY,
218 		[NVME_ZRASF_ZONE_STATE_IMP_OPEN] = BLK_ZONE_COND_IMP_OPEN,
219 		[NVME_ZRASF_ZONE_STATE_EXP_OPEN] = BLK_ZONE_COND_EXP_OPEN,
220 		[NVME_ZRASF_ZONE_STATE_CLOSED]	 = BLK_ZONE_COND_CLOSED,
221 		[NVME_ZRASF_ZONE_STATE_READONLY] = BLK_ZONE_COND_READONLY,
222 		[NVME_ZRASF_ZONE_STATE_FULL]	 = BLK_ZONE_COND_FULL,
223 		[NVME_ZRASF_ZONE_STATE_OFFLINE]	 = BLK_ZONE_COND_OFFLINE,
224 	};
225 	struct nvmet_report_zone_data *rz = d;
226 
227 	if (rz->zrasf != NVME_ZRASF_ZONE_REPORT_ALL &&
228 	    z->cond != nvme_zrasf_to_blk_zcond[rz->zrasf])
229 		return 0;
230 
231 	if (rz->nr_zones < rz->out_nr_zones) {
232 		struct nvme_zone_descriptor zdesc = { };
233 		u16 status;
234 
235 		zdesc.zcap = nvmet_sect_to_lba(rz->req->ns, z->capacity);
236 		zdesc.zslba = nvmet_sect_to_lba(rz->req->ns, z->start);
237 		zdesc.wp = nvmet_sect_to_lba(rz->req->ns, z->wp);
238 		zdesc.za = z->reset ? 1 << 2 : 0;
239 		zdesc.zs = z->cond << 4;
240 		zdesc.zt = z->type;
241 
242 		status = nvmet_copy_to_sgl(rz->req, rz->out_buf_offset, &zdesc,
243 					   sizeof(zdesc));
244 		if (status)
245 			return -EINVAL;
246 
247 		rz->out_buf_offset += sizeof(zdesc);
248 	}
249 
250 	rz->nr_zones++;
251 
252 	return 0;
253 }
254 
nvmet_req_nr_zones_from_slba(struct nvmet_req * req)255 static unsigned long nvmet_req_nr_zones_from_slba(struct nvmet_req *req)
256 {
257 	unsigned int sect = nvmet_lba_to_sect(req->ns, req->cmd->zmr.slba);
258 
259 	return blkdev_nr_zones(req->ns->bdev->bd_disk) -
260 		bdev_zone_no(req->ns->bdev, sect);
261 }
262 
get_nr_zones_from_buf(struct nvmet_req * req,u32 bufsize)263 static unsigned long get_nr_zones_from_buf(struct nvmet_req *req, u32 bufsize)
264 {
265 	if (bufsize <= sizeof(struct nvme_zone_report))
266 		return 0;
267 
268 	return (bufsize - sizeof(struct nvme_zone_report)) /
269 		sizeof(struct nvme_zone_descriptor);
270 }
271 
nvmet_bdev_zone_zmgmt_recv_work(struct work_struct * w)272 static void nvmet_bdev_zone_zmgmt_recv_work(struct work_struct *w)
273 {
274 	struct nvmet_req *req = container_of(w, struct nvmet_req, z.zmgmt_work);
275 	sector_t start_sect = nvmet_lba_to_sect(req->ns, req->cmd->zmr.slba);
276 	unsigned long req_slba_nr_zones = nvmet_req_nr_zones_from_slba(req);
277 	u32 out_bufsize = (le32_to_cpu(req->cmd->zmr.numd) + 1) << 2;
278 	__le64 nr_zones;
279 	u16 status;
280 	int ret;
281 	struct nvmet_report_zone_data rz_data = {
282 		.out_nr_zones = get_nr_zones_from_buf(req, out_bufsize),
283 		/* leave the place for report zone header */
284 		.out_buf_offset = sizeof(struct nvme_zone_report),
285 		.zrasf = req->cmd->zmr.zrasf,
286 		.nr_zones = 0,
287 		.req = req,
288 	};
289 
290 	status = nvmet_bdev_validate_zone_mgmt_recv(req);
291 	if (status)
292 		goto out;
293 
294 	if (!req_slba_nr_zones) {
295 		status = NVME_SC_SUCCESS;
296 		goto out;
297 	}
298 
299 	ret = blkdev_report_zones(req->ns->bdev, start_sect, req_slba_nr_zones,
300 				 nvmet_bdev_report_zone_cb, &rz_data);
301 	if (ret < 0) {
302 		status = NVME_SC_INTERNAL;
303 		goto out;
304 	}
305 
306 	/*
307 	 * When partial bit is set nr_zones must indicate the number of zone
308 	 * descriptors actually transferred.
309 	 */
310 	if (req->cmd->zmr.pr)
311 		rz_data.nr_zones = min(rz_data.nr_zones, rz_data.out_nr_zones);
312 
313 	nr_zones = cpu_to_le64(rz_data.nr_zones);
314 	status = nvmet_copy_to_sgl(req, 0, &nr_zones, sizeof(nr_zones));
315 
316 out:
317 	nvmet_req_complete(req, status);
318 }
319 
nvmet_bdev_execute_zone_mgmt_recv(struct nvmet_req * req)320 void nvmet_bdev_execute_zone_mgmt_recv(struct nvmet_req *req)
321 {
322 	INIT_WORK(&req->z.zmgmt_work, nvmet_bdev_zone_zmgmt_recv_work);
323 	queue_work(zbd_wq, &req->z.zmgmt_work);
324 }
325 
zsa_req_op(u8 zsa)326 static inline enum req_opf zsa_req_op(u8 zsa)
327 {
328 	switch (zsa) {
329 	case NVME_ZONE_OPEN:
330 		return REQ_OP_ZONE_OPEN;
331 	case NVME_ZONE_CLOSE:
332 		return REQ_OP_ZONE_CLOSE;
333 	case NVME_ZONE_FINISH:
334 		return REQ_OP_ZONE_FINISH;
335 	case NVME_ZONE_RESET:
336 		return REQ_OP_ZONE_RESET;
337 	default:
338 		return REQ_OP_LAST;
339 	}
340 }
341 
blkdev_zone_mgmt_errno_to_nvme_status(int ret)342 static u16 blkdev_zone_mgmt_errno_to_nvme_status(int ret)
343 {
344 	switch (ret) {
345 	case 0:
346 		return NVME_SC_SUCCESS;
347 	case -EINVAL:
348 	case -EIO:
349 		return NVME_SC_ZONE_INVALID_TRANSITION | NVME_SC_DNR;
350 	default:
351 		return NVME_SC_INTERNAL;
352 	}
353 }
354 
355 struct nvmet_zone_mgmt_send_all_data {
356 	unsigned long *zbitmap;
357 	struct nvmet_req *req;
358 };
359 
zmgmt_send_scan_cb(struct blk_zone * z,unsigned i,void * d)360 static int zmgmt_send_scan_cb(struct blk_zone *z, unsigned i, void *d)
361 {
362 	struct nvmet_zone_mgmt_send_all_data *data = d;
363 
364 	switch (zsa_req_op(data->req->cmd->zms.zsa)) {
365 	case REQ_OP_ZONE_OPEN:
366 		switch (z->cond) {
367 		case BLK_ZONE_COND_CLOSED:
368 			break;
369 		default:
370 			return 0;
371 		}
372 		break;
373 	case REQ_OP_ZONE_CLOSE:
374 		switch (z->cond) {
375 		case BLK_ZONE_COND_IMP_OPEN:
376 		case BLK_ZONE_COND_EXP_OPEN:
377 			break;
378 		default:
379 			return 0;
380 		}
381 		break;
382 	case REQ_OP_ZONE_FINISH:
383 		switch (z->cond) {
384 		case BLK_ZONE_COND_IMP_OPEN:
385 		case BLK_ZONE_COND_EXP_OPEN:
386 		case BLK_ZONE_COND_CLOSED:
387 			break;
388 		default:
389 			return 0;
390 		}
391 		break;
392 	default:
393 		return -EINVAL;
394 	}
395 
396 	set_bit(i, data->zbitmap);
397 
398 	return 0;
399 }
400 
nvmet_bdev_zone_mgmt_emulate_all(struct nvmet_req * req)401 static u16 nvmet_bdev_zone_mgmt_emulate_all(struct nvmet_req *req)
402 {
403 	struct block_device *bdev = req->ns->bdev;
404 	unsigned int nr_zones = blkdev_nr_zones(bdev->bd_disk);
405 	struct request_queue *q = bdev_get_queue(bdev);
406 	struct bio *bio = NULL;
407 	sector_t sector = 0;
408 	int ret;
409 	struct nvmet_zone_mgmt_send_all_data d = {
410 		.req = req,
411 	};
412 
413 	d.zbitmap = kcalloc_node(BITS_TO_LONGS(nr_zones), sizeof(*(d.zbitmap)),
414 				 GFP_NOIO, q->node);
415 	if (!d.zbitmap) {
416 		ret = -ENOMEM;
417 		goto out;
418 	}
419 
420 	/* Scan and build bitmap of the eligible zones */
421 	ret = blkdev_report_zones(bdev, 0, nr_zones, zmgmt_send_scan_cb, &d);
422 	if (ret != nr_zones) {
423 		if (ret > 0)
424 			ret = -EIO;
425 		goto out;
426 	} else {
427 		/* We scanned all the zones */
428 		ret = 0;
429 	}
430 
431 	while (sector < get_capacity(bdev->bd_disk)) {
432 		if (test_bit(blk_queue_zone_no(q, sector), d.zbitmap)) {
433 			bio = blk_next_bio(bio, 0, GFP_KERNEL);
434 			bio->bi_opf = zsa_req_op(req->cmd->zms.zsa) | REQ_SYNC;
435 			bio->bi_iter.bi_sector = sector;
436 			bio_set_dev(bio, bdev);
437 			/* This may take a while, so be nice to others */
438 			cond_resched();
439 		}
440 		sector += blk_queue_zone_sectors(q);
441 	}
442 
443 	if (bio) {
444 		ret = submit_bio_wait(bio);
445 		bio_put(bio);
446 	}
447 
448 out:
449 	kfree(d.zbitmap);
450 
451 	return blkdev_zone_mgmt_errno_to_nvme_status(ret);
452 }
453 
nvmet_bdev_execute_zmgmt_send_all(struct nvmet_req * req)454 static u16 nvmet_bdev_execute_zmgmt_send_all(struct nvmet_req *req)
455 {
456 	int ret;
457 
458 	switch (zsa_req_op(req->cmd->zms.zsa)) {
459 	case REQ_OP_ZONE_RESET:
460 		ret = blkdev_zone_mgmt(req->ns->bdev, REQ_OP_ZONE_RESET, 0,
461 				       get_capacity(req->ns->bdev->bd_disk),
462 				       GFP_KERNEL);
463 		if (ret < 0)
464 			return blkdev_zone_mgmt_errno_to_nvme_status(ret);
465 		break;
466 	case REQ_OP_ZONE_OPEN:
467 	case REQ_OP_ZONE_CLOSE:
468 	case REQ_OP_ZONE_FINISH:
469 		return nvmet_bdev_zone_mgmt_emulate_all(req);
470 	default:
471 		/* this is needed to quiet compiler warning */
472 		req->error_loc = offsetof(struct nvme_zone_mgmt_send_cmd, zsa);
473 		return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
474 	}
475 
476 	return NVME_SC_SUCCESS;
477 }
478 
nvmet_bdev_zmgmt_send_work(struct work_struct * w)479 static void nvmet_bdev_zmgmt_send_work(struct work_struct *w)
480 {
481 	struct nvmet_req *req = container_of(w, struct nvmet_req, z.zmgmt_work);
482 	sector_t sect = nvmet_lba_to_sect(req->ns, req->cmd->zms.slba);
483 	enum req_opf op = zsa_req_op(req->cmd->zms.zsa);
484 	struct block_device *bdev = req->ns->bdev;
485 	sector_t zone_sectors = bdev_zone_sectors(bdev);
486 	u16 status = NVME_SC_SUCCESS;
487 	int ret;
488 
489 	if (op == REQ_OP_LAST) {
490 		req->error_loc = offsetof(struct nvme_zone_mgmt_send_cmd, zsa);
491 		status = NVME_SC_ZONE_INVALID_TRANSITION | NVME_SC_DNR;
492 		goto out;
493 	}
494 
495 	/* when select all bit is set slba field is ignored */
496 	if (req->cmd->zms.select_all) {
497 		status = nvmet_bdev_execute_zmgmt_send_all(req);
498 		goto out;
499 	}
500 
501 	if (sect >= get_capacity(bdev->bd_disk)) {
502 		req->error_loc = offsetof(struct nvme_zone_mgmt_send_cmd, slba);
503 		status = NVME_SC_LBA_RANGE | NVME_SC_DNR;
504 		goto out;
505 	}
506 
507 	if (!bdev_is_zone_start(bdev, sect)) {
508 		req->error_loc = offsetof(struct nvme_zone_mgmt_send_cmd, slba);
509 		status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
510 		goto out;
511 	}
512 
513 	ret = blkdev_zone_mgmt(bdev, op, sect, zone_sectors, GFP_KERNEL);
514 	if (ret < 0)
515 		status = blkdev_zone_mgmt_errno_to_nvme_status(ret);
516 
517 out:
518 	nvmet_req_complete(req, status);
519 }
520 
nvmet_bdev_execute_zone_mgmt_send(struct nvmet_req * req)521 void nvmet_bdev_execute_zone_mgmt_send(struct nvmet_req *req)
522 {
523 	INIT_WORK(&req->z.zmgmt_work, nvmet_bdev_zmgmt_send_work);
524 	queue_work(zbd_wq, &req->z.zmgmt_work);
525 }
526 
nvmet_bdev_zone_append_bio_done(struct bio * bio)527 static void nvmet_bdev_zone_append_bio_done(struct bio *bio)
528 {
529 	struct nvmet_req *req = bio->bi_private;
530 
531 	if (bio->bi_status == BLK_STS_OK) {
532 		req->cqe->result.u64 =
533 			nvmet_sect_to_lba(req->ns, bio->bi_iter.bi_sector);
534 	}
535 
536 	nvmet_req_complete(req, blk_to_nvme_status(req, bio->bi_status));
537 	nvmet_req_bio_put(req, bio);
538 }
539 
nvmet_bdev_execute_zone_append(struct nvmet_req * req)540 void nvmet_bdev_execute_zone_append(struct nvmet_req *req)
541 {
542 	sector_t sect = nvmet_lba_to_sect(req->ns, req->cmd->rw.slba);
543 	u16 status = NVME_SC_SUCCESS;
544 	unsigned int total_len = 0;
545 	struct scatterlist *sg;
546 	struct bio *bio;
547 	int sg_cnt;
548 
549 	/* Request is completed on len mismatch in nvmet_check_transter_len() */
550 	if (!nvmet_check_transfer_len(req, nvmet_rw_data_len(req)))
551 		return;
552 
553 	if (!req->sg_cnt) {
554 		nvmet_req_complete(req, 0);
555 		return;
556 	}
557 
558 	if (sect >= get_capacity(req->ns->bdev->bd_disk)) {
559 		req->error_loc = offsetof(struct nvme_rw_command, slba);
560 		status = NVME_SC_LBA_RANGE | NVME_SC_DNR;
561 		goto out;
562 	}
563 
564 	if (!bdev_is_zone_start(req->ns->bdev, sect)) {
565 		req->error_loc = offsetof(struct nvme_rw_command, slba);
566 		status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
567 		goto out;
568 	}
569 
570 	if (nvmet_use_inline_bvec(req)) {
571 		bio = &req->z.inline_bio;
572 		bio_init(bio, req->inline_bvec, ARRAY_SIZE(req->inline_bvec));
573 	} else {
574 		bio = bio_alloc(GFP_KERNEL, req->sg_cnt);
575 	}
576 
577 	bio->bi_opf = REQ_OP_ZONE_APPEND | REQ_SYNC | REQ_IDLE;
578 	bio->bi_end_io = nvmet_bdev_zone_append_bio_done;
579 	bio_set_dev(bio, req->ns->bdev);
580 	bio->bi_iter.bi_sector = sect;
581 	bio->bi_private = req;
582 	if (req->cmd->rw.control & cpu_to_le16(NVME_RW_FUA))
583 		bio->bi_opf |= REQ_FUA;
584 
585 	for_each_sg(req->sg, sg, req->sg_cnt, sg_cnt) {
586 		struct page *p = sg_page(sg);
587 		unsigned int l = sg->length;
588 		unsigned int o = sg->offset;
589 		unsigned int ret;
590 
591 		ret = bio_add_zone_append_page(bio, p, l, o);
592 		if (ret != sg->length) {
593 			status = NVME_SC_INTERNAL;
594 			goto out_put_bio;
595 		}
596 		total_len += sg->length;
597 	}
598 
599 	if (total_len != nvmet_rw_data_len(req)) {
600 		status = NVME_SC_INTERNAL | NVME_SC_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