xref: /openbmc/linux/drivers/nvme/host/ioctl.c (revision 5b828263)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2011-2014, Intel Corporation.
4  * Copyright (c) 2017-2021 Christoph Hellwig.
5  */
6 #include <linux/ptrace.h>	/* for force_successful_syscall_return */
7 #include <linux/nvme_ioctl.h>
8 #include <linux/io_uring.h>
9 #include "nvme.h"
10 
11 /*
12  * Convert integer values from ioctl structures to user pointers, silently
13  * ignoring the upper bits in the compat case to match behaviour of 32-bit
14  * kernels.
15  */
16 static void __user *nvme_to_user_ptr(uintptr_t ptrval)
17 {
18 	if (in_compat_syscall())
19 		ptrval = (compat_uptr_t)ptrval;
20 	return (void __user *)ptrval;
21 }
22 
23 static void *nvme_add_user_metadata(struct bio *bio, void __user *ubuf,
24 		unsigned len, u32 seed, bool write)
25 {
26 	struct bio_integrity_payload *bip;
27 	int ret = -ENOMEM;
28 	void *buf;
29 
30 	buf = kmalloc(len, GFP_KERNEL);
31 	if (!buf)
32 		goto out;
33 
34 	ret = -EFAULT;
35 	if (write && copy_from_user(buf, ubuf, len))
36 		goto out_free_meta;
37 
38 	bip = bio_integrity_alloc(bio, GFP_KERNEL, 1);
39 	if (IS_ERR(bip)) {
40 		ret = PTR_ERR(bip);
41 		goto out_free_meta;
42 	}
43 
44 	bip->bip_iter.bi_size = len;
45 	bip->bip_iter.bi_sector = seed;
46 	ret = bio_integrity_add_page(bio, virt_to_page(buf), len,
47 			offset_in_page(buf));
48 	if (ret == len)
49 		return buf;
50 	ret = -ENOMEM;
51 out_free_meta:
52 	kfree(buf);
53 out:
54 	return ERR_PTR(ret);
55 }
56 
57 static int nvme_finish_user_metadata(struct request *req, void __user *ubuf,
58 		void *meta, unsigned len, int ret)
59 {
60 	if (!ret && req_op(req) == REQ_OP_DRV_IN &&
61 	    copy_to_user(ubuf, meta, len))
62 		ret = -EFAULT;
63 	kfree(meta);
64 	return ret;
65 }
66 
67 static struct request *nvme_alloc_user_request(struct request_queue *q,
68 		struct nvme_command *cmd, void __user *ubuffer,
69 		unsigned bufflen, void __user *meta_buffer, unsigned meta_len,
70 		u32 meta_seed, void **metap, unsigned timeout, bool vec,
71 		unsigned int rq_flags, blk_mq_req_flags_t blk_flags)
72 {
73 	bool write = nvme_is_write(cmd);
74 	struct nvme_ns *ns = q->queuedata;
75 	struct block_device *bdev = ns ? ns->disk->part0 : NULL;
76 	struct request *req;
77 	struct bio *bio = NULL;
78 	void *meta = NULL;
79 	int ret;
80 
81 	req = blk_mq_alloc_request(q, nvme_req_op(cmd) | rq_flags, blk_flags);
82 	if (IS_ERR(req))
83 		return req;
84 	nvme_init_request(req, cmd);
85 
86 	if (timeout)
87 		req->timeout = timeout;
88 	nvme_req(req)->flags |= NVME_REQ_USERCMD;
89 
90 	if (ubuffer && bufflen) {
91 		if (!vec)
92 			ret = blk_rq_map_user(q, req, NULL, ubuffer, bufflen,
93 				GFP_KERNEL);
94 		else {
95 			struct iovec fast_iov[UIO_FASTIOV];
96 			struct iovec *iov = fast_iov;
97 			struct iov_iter iter;
98 
99 			ret = import_iovec(rq_data_dir(req), ubuffer, bufflen,
100 					UIO_FASTIOV, &iov, &iter);
101 			if (ret < 0)
102 				goto out;
103 			ret = blk_rq_map_user_iov(q, req, NULL, &iter,
104 					GFP_KERNEL);
105 			kfree(iov);
106 		}
107 		if (ret)
108 			goto out;
109 		bio = req->bio;
110 		if (bdev)
111 			bio_set_dev(bio, bdev);
112 		if (bdev && meta_buffer && meta_len) {
113 			meta = nvme_add_user_metadata(bio, meta_buffer, meta_len,
114 					meta_seed, write);
115 			if (IS_ERR(meta)) {
116 				ret = PTR_ERR(meta);
117 				goto out_unmap;
118 			}
119 			req->cmd_flags |= REQ_INTEGRITY;
120 			*metap = meta;
121 		}
122 	}
123 
124 	return req;
125 
126 out_unmap:
127 	if (bio)
128 		blk_rq_unmap_user(bio);
129 out:
130 	blk_mq_free_request(req);
131 	return ERR_PTR(ret);
132 }
133 
134 static int nvme_submit_user_cmd(struct request_queue *q,
135 		struct nvme_command *cmd, void __user *ubuffer,
136 		unsigned bufflen, void __user *meta_buffer, unsigned meta_len,
137 		u32 meta_seed, u64 *result, unsigned timeout, bool vec)
138 {
139 	struct request *req;
140 	void *meta = NULL;
141 	struct bio *bio;
142 	int ret;
143 
144 	req = nvme_alloc_user_request(q, cmd, ubuffer, bufflen, meta_buffer,
145 			meta_len, meta_seed, &meta, timeout, vec, 0, 0);
146 	if (IS_ERR(req))
147 		return PTR_ERR(req);
148 
149 	bio = req->bio;
150 
151 	ret = nvme_execute_passthru_rq(req);
152 
153 	if (result)
154 		*result = le64_to_cpu(nvme_req(req)->result.u64);
155 	if (meta)
156 		ret = nvme_finish_user_metadata(req, meta_buffer, meta,
157 						meta_len, ret);
158 	if (bio)
159 		blk_rq_unmap_user(bio);
160 	blk_mq_free_request(req);
161 	return ret;
162 }
163 
164 static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
165 {
166 	struct nvme_user_io io;
167 	struct nvme_command c;
168 	unsigned length, meta_len;
169 	void __user *metadata;
170 
171 	if (copy_from_user(&io, uio, sizeof(io)))
172 		return -EFAULT;
173 	if (io.flags)
174 		return -EINVAL;
175 
176 	switch (io.opcode) {
177 	case nvme_cmd_write:
178 	case nvme_cmd_read:
179 	case nvme_cmd_compare:
180 		break;
181 	default:
182 		return -EINVAL;
183 	}
184 
185 	length = (io.nblocks + 1) << ns->lba_shift;
186 
187 	if ((io.control & NVME_RW_PRINFO_PRACT) &&
188 	    ns->ms == sizeof(struct t10_pi_tuple)) {
189 		/*
190 		 * Protection information is stripped/inserted by the
191 		 * controller.
192 		 */
193 		if (nvme_to_user_ptr(io.metadata))
194 			return -EINVAL;
195 		meta_len = 0;
196 		metadata = NULL;
197 	} else {
198 		meta_len = (io.nblocks + 1) * ns->ms;
199 		metadata = nvme_to_user_ptr(io.metadata);
200 	}
201 
202 	if (ns->features & NVME_NS_EXT_LBAS) {
203 		length += meta_len;
204 		meta_len = 0;
205 	} else if (meta_len) {
206 		if ((io.metadata & 3) || !io.metadata)
207 			return -EINVAL;
208 	}
209 
210 	memset(&c, 0, sizeof(c));
211 	c.rw.opcode = io.opcode;
212 	c.rw.flags = io.flags;
213 	c.rw.nsid = cpu_to_le32(ns->head->ns_id);
214 	c.rw.slba = cpu_to_le64(io.slba);
215 	c.rw.length = cpu_to_le16(io.nblocks);
216 	c.rw.control = cpu_to_le16(io.control);
217 	c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
218 	c.rw.reftag = cpu_to_le32(io.reftag);
219 	c.rw.apptag = cpu_to_le16(io.apptag);
220 	c.rw.appmask = cpu_to_le16(io.appmask);
221 
222 	return nvme_submit_user_cmd(ns->queue, &c,
223 			nvme_to_user_ptr(io.addr), length,
224 			metadata, meta_len, lower_32_bits(io.slba), NULL, 0,
225 			false);
226 }
227 
228 static bool nvme_validate_passthru_nsid(struct nvme_ctrl *ctrl,
229 					struct nvme_ns *ns, __u32 nsid)
230 {
231 	if (ns && nsid != ns->head->ns_id) {
232 		dev_err(ctrl->device,
233 			"%s: nsid (%u) in cmd does not match nsid (%u)"
234 			"of namespace\n",
235 			current->comm, nsid, ns->head->ns_id);
236 		return false;
237 	}
238 
239 	return true;
240 }
241 
242 static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
243 			struct nvme_passthru_cmd __user *ucmd)
244 {
245 	struct nvme_passthru_cmd cmd;
246 	struct nvme_command c;
247 	unsigned timeout = 0;
248 	u64 result;
249 	int status;
250 
251 	if (!capable(CAP_SYS_ADMIN))
252 		return -EACCES;
253 	if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
254 		return -EFAULT;
255 	if (cmd.flags)
256 		return -EINVAL;
257 	if (!nvme_validate_passthru_nsid(ctrl, ns, cmd.nsid))
258 		return -EINVAL;
259 
260 	memset(&c, 0, sizeof(c));
261 	c.common.opcode = cmd.opcode;
262 	c.common.flags = cmd.flags;
263 	c.common.nsid = cpu_to_le32(cmd.nsid);
264 	c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
265 	c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
266 	c.common.cdw10 = cpu_to_le32(cmd.cdw10);
267 	c.common.cdw11 = cpu_to_le32(cmd.cdw11);
268 	c.common.cdw12 = cpu_to_le32(cmd.cdw12);
269 	c.common.cdw13 = cpu_to_le32(cmd.cdw13);
270 	c.common.cdw14 = cpu_to_le32(cmd.cdw14);
271 	c.common.cdw15 = cpu_to_le32(cmd.cdw15);
272 
273 	if (cmd.timeout_ms)
274 		timeout = msecs_to_jiffies(cmd.timeout_ms);
275 
276 	status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
277 			nvme_to_user_ptr(cmd.addr), cmd.data_len,
278 			nvme_to_user_ptr(cmd.metadata), cmd.metadata_len,
279 			0, &result, timeout, false);
280 
281 	if (status >= 0) {
282 		if (put_user(result, &ucmd->result))
283 			return -EFAULT;
284 	}
285 
286 	return status;
287 }
288 
289 static int nvme_user_cmd64(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
290 			struct nvme_passthru_cmd64 __user *ucmd, bool vec)
291 {
292 	struct nvme_passthru_cmd64 cmd;
293 	struct nvme_command c;
294 	unsigned timeout = 0;
295 	int status;
296 
297 	if (!capable(CAP_SYS_ADMIN))
298 		return -EACCES;
299 	if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
300 		return -EFAULT;
301 	if (cmd.flags)
302 		return -EINVAL;
303 	if (!nvme_validate_passthru_nsid(ctrl, ns, cmd.nsid))
304 		return -EINVAL;
305 
306 	memset(&c, 0, sizeof(c));
307 	c.common.opcode = cmd.opcode;
308 	c.common.flags = cmd.flags;
309 	c.common.nsid = cpu_to_le32(cmd.nsid);
310 	c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
311 	c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
312 	c.common.cdw10 = cpu_to_le32(cmd.cdw10);
313 	c.common.cdw11 = cpu_to_le32(cmd.cdw11);
314 	c.common.cdw12 = cpu_to_le32(cmd.cdw12);
315 	c.common.cdw13 = cpu_to_le32(cmd.cdw13);
316 	c.common.cdw14 = cpu_to_le32(cmd.cdw14);
317 	c.common.cdw15 = cpu_to_le32(cmd.cdw15);
318 
319 	if (cmd.timeout_ms)
320 		timeout = msecs_to_jiffies(cmd.timeout_ms);
321 
322 	status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
323 			nvme_to_user_ptr(cmd.addr), cmd.data_len,
324 			nvme_to_user_ptr(cmd.metadata), cmd.metadata_len,
325 			0, &cmd.result, timeout, vec);
326 
327 	if (status >= 0) {
328 		if (put_user(cmd.result, &ucmd->result))
329 			return -EFAULT;
330 	}
331 
332 	return status;
333 }
334 
335 struct nvme_uring_data {
336 	__u64	metadata;
337 	__u64	addr;
338 	__u32	data_len;
339 	__u32	metadata_len;
340 	__u32	timeout_ms;
341 };
342 
343 /*
344  * This overlays struct io_uring_cmd pdu.
345  * Expect build errors if this grows larger than that.
346  */
347 struct nvme_uring_cmd_pdu {
348 	union {
349 		struct bio *bio;
350 		struct request *req;
351 	};
352 	void *meta; /* kernel-resident buffer */
353 	void __user *meta_buffer;
354 	u32 meta_len;
355 };
356 
357 static inline struct nvme_uring_cmd_pdu *nvme_uring_cmd_pdu(
358 		struct io_uring_cmd *ioucmd)
359 {
360 	return (struct nvme_uring_cmd_pdu *)&ioucmd->pdu;
361 }
362 
363 static void nvme_uring_task_cb(struct io_uring_cmd *ioucmd)
364 {
365 	struct nvme_uring_cmd_pdu *pdu = nvme_uring_cmd_pdu(ioucmd);
366 	struct request *req = pdu->req;
367 	struct bio *bio = req->bio;
368 	int status;
369 	u64 result;
370 
371 	if (nvme_req(req)->flags & NVME_REQ_CANCELLED)
372 		status = -EINTR;
373 	else
374 		status = nvme_req(req)->status;
375 
376 	result = le64_to_cpu(nvme_req(req)->result.u64);
377 
378 	if (pdu->meta)
379 		status = nvme_finish_user_metadata(req, pdu->meta_buffer,
380 					pdu->meta, pdu->meta_len, status);
381 	if (bio)
382 		blk_rq_unmap_user(bio);
383 	blk_mq_free_request(req);
384 
385 	io_uring_cmd_done(ioucmd, status, result);
386 }
387 
388 static void nvme_uring_cmd_end_io(struct request *req, blk_status_t err)
389 {
390 	struct io_uring_cmd *ioucmd = req->end_io_data;
391 	struct nvme_uring_cmd_pdu *pdu = nvme_uring_cmd_pdu(ioucmd);
392 	/* extract bio before reusing the same field for request */
393 	struct bio *bio = pdu->bio;
394 
395 	pdu->req = req;
396 	req->bio = bio;
397 	/* this takes care of moving rest of completion-work to task context */
398 	io_uring_cmd_complete_in_task(ioucmd, nvme_uring_task_cb);
399 }
400 
401 static int nvme_uring_cmd_io(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
402 		struct io_uring_cmd *ioucmd, unsigned int issue_flags, bool vec)
403 {
404 	struct nvme_uring_cmd_pdu *pdu = nvme_uring_cmd_pdu(ioucmd);
405 	const struct nvme_uring_cmd *cmd = ioucmd->cmd;
406 	struct request_queue *q = ns ? ns->queue : ctrl->admin_q;
407 	struct nvme_uring_data d;
408 	struct nvme_command c;
409 	struct request *req;
410 	unsigned int rq_flags = 0;
411 	blk_mq_req_flags_t blk_flags = 0;
412 	void *meta = NULL;
413 
414 	if (!capable(CAP_SYS_ADMIN))
415 		return -EACCES;
416 
417 	c.common.opcode = READ_ONCE(cmd->opcode);
418 	c.common.flags = READ_ONCE(cmd->flags);
419 	if (c.common.flags)
420 		return -EINVAL;
421 
422 	c.common.command_id = 0;
423 	c.common.nsid = cpu_to_le32(cmd->nsid);
424 	if (!nvme_validate_passthru_nsid(ctrl, ns, le32_to_cpu(c.common.nsid)))
425 		return -EINVAL;
426 
427 	c.common.cdw2[0] = cpu_to_le32(READ_ONCE(cmd->cdw2));
428 	c.common.cdw2[1] = cpu_to_le32(READ_ONCE(cmd->cdw3));
429 	c.common.metadata = 0;
430 	c.common.dptr.prp1 = c.common.dptr.prp2 = 0;
431 	c.common.cdw10 = cpu_to_le32(READ_ONCE(cmd->cdw10));
432 	c.common.cdw11 = cpu_to_le32(READ_ONCE(cmd->cdw11));
433 	c.common.cdw12 = cpu_to_le32(READ_ONCE(cmd->cdw12));
434 	c.common.cdw13 = cpu_to_le32(READ_ONCE(cmd->cdw13));
435 	c.common.cdw14 = cpu_to_le32(READ_ONCE(cmd->cdw14));
436 	c.common.cdw15 = cpu_to_le32(READ_ONCE(cmd->cdw15));
437 
438 	d.metadata = READ_ONCE(cmd->metadata);
439 	d.addr = READ_ONCE(cmd->addr);
440 	d.data_len = READ_ONCE(cmd->data_len);
441 	d.metadata_len = READ_ONCE(cmd->metadata_len);
442 	d.timeout_ms = READ_ONCE(cmd->timeout_ms);
443 
444 	if (issue_flags & IO_URING_F_NONBLOCK) {
445 		rq_flags = REQ_NOWAIT;
446 		blk_flags = BLK_MQ_REQ_NOWAIT;
447 	}
448 
449 	req = nvme_alloc_user_request(q, &c, nvme_to_user_ptr(d.addr),
450 			d.data_len, nvme_to_user_ptr(d.metadata),
451 			d.metadata_len, 0, &meta, d.timeout_ms ?
452 			msecs_to_jiffies(d.timeout_ms) : 0, vec, rq_flags,
453 			blk_flags);
454 	if (IS_ERR(req))
455 		return PTR_ERR(req);
456 	req->end_io_data = ioucmd;
457 
458 	/* to free bio on completion, as req->bio will be null at that time */
459 	pdu->bio = req->bio;
460 	pdu->meta = meta;
461 	pdu->meta_buffer = nvme_to_user_ptr(d.metadata);
462 	pdu->meta_len = d.metadata_len;
463 
464 	blk_execute_rq_nowait(req, 0, nvme_uring_cmd_end_io);
465 	return -EIOCBQUEUED;
466 }
467 
468 static bool is_ctrl_ioctl(unsigned int cmd)
469 {
470 	if (cmd == NVME_IOCTL_ADMIN_CMD || cmd == NVME_IOCTL_ADMIN64_CMD)
471 		return true;
472 	if (is_sed_ioctl(cmd))
473 		return true;
474 	return false;
475 }
476 
477 static int nvme_ctrl_ioctl(struct nvme_ctrl *ctrl, unsigned int cmd,
478 		void __user *argp)
479 {
480 	switch (cmd) {
481 	case NVME_IOCTL_ADMIN_CMD:
482 		return nvme_user_cmd(ctrl, NULL, argp);
483 	case NVME_IOCTL_ADMIN64_CMD:
484 		return nvme_user_cmd64(ctrl, NULL, argp, false);
485 	default:
486 		return sed_ioctl(ctrl->opal_dev, cmd, argp);
487 	}
488 }
489 
490 #ifdef COMPAT_FOR_U64_ALIGNMENT
491 struct nvme_user_io32 {
492 	__u8	opcode;
493 	__u8	flags;
494 	__u16	control;
495 	__u16	nblocks;
496 	__u16	rsvd;
497 	__u64	metadata;
498 	__u64	addr;
499 	__u64	slba;
500 	__u32	dsmgmt;
501 	__u32	reftag;
502 	__u16	apptag;
503 	__u16	appmask;
504 } __attribute__((__packed__));
505 #define NVME_IOCTL_SUBMIT_IO32	_IOW('N', 0x42, struct nvme_user_io32)
506 #endif /* COMPAT_FOR_U64_ALIGNMENT */
507 
508 static int nvme_ns_ioctl(struct nvme_ns *ns, unsigned int cmd,
509 		void __user *argp)
510 {
511 	switch (cmd) {
512 	case NVME_IOCTL_ID:
513 		force_successful_syscall_return();
514 		return ns->head->ns_id;
515 	case NVME_IOCTL_IO_CMD:
516 		return nvme_user_cmd(ns->ctrl, ns, argp);
517 	/*
518 	 * struct nvme_user_io can have different padding on some 32-bit ABIs.
519 	 * Just accept the compat version as all fields that are used are the
520 	 * same size and at the same offset.
521 	 */
522 #ifdef COMPAT_FOR_U64_ALIGNMENT
523 	case NVME_IOCTL_SUBMIT_IO32:
524 #endif
525 	case NVME_IOCTL_SUBMIT_IO:
526 		return nvme_submit_io(ns, argp);
527 	case NVME_IOCTL_IO64_CMD:
528 		return nvme_user_cmd64(ns->ctrl, ns, argp, false);
529 	case NVME_IOCTL_IO64_CMD_VEC:
530 		return nvme_user_cmd64(ns->ctrl, ns, argp, true);
531 	default:
532 		return -ENOTTY;
533 	}
534 }
535 
536 static int __nvme_ioctl(struct nvme_ns *ns, unsigned int cmd, void __user *arg)
537 {
538        if (is_ctrl_ioctl(cmd))
539                return nvme_ctrl_ioctl(ns->ctrl, cmd, arg);
540        return nvme_ns_ioctl(ns, cmd, arg);
541 }
542 
543 int nvme_ioctl(struct block_device *bdev, fmode_t mode,
544 		unsigned int cmd, unsigned long arg)
545 {
546 	struct nvme_ns *ns = bdev->bd_disk->private_data;
547 
548 	return __nvme_ioctl(ns, cmd, (void __user *)arg);
549 }
550 
551 long nvme_ns_chr_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
552 {
553 	struct nvme_ns *ns =
554 		container_of(file_inode(file)->i_cdev, struct nvme_ns, cdev);
555 
556 	return __nvme_ioctl(ns, cmd, (void __user *)arg);
557 }
558 
559 static int nvme_uring_cmd_checks(unsigned int issue_flags)
560 {
561 	/* IOPOLL not supported yet */
562 	if (issue_flags & IO_URING_F_IOPOLL)
563 		return -EOPNOTSUPP;
564 
565 	/* NVMe passthrough requires big SQE/CQE support */
566 	if ((issue_flags & (IO_URING_F_SQE128|IO_URING_F_CQE32)) !=
567 	    (IO_URING_F_SQE128|IO_URING_F_CQE32))
568 		return -EOPNOTSUPP;
569 	return 0;
570 }
571 
572 static int nvme_ns_uring_cmd(struct nvme_ns *ns, struct io_uring_cmd *ioucmd,
573 			     unsigned int issue_flags)
574 {
575 	struct nvme_ctrl *ctrl = ns->ctrl;
576 	int ret;
577 
578 	BUILD_BUG_ON(sizeof(struct nvme_uring_cmd_pdu) > sizeof(ioucmd->pdu));
579 
580 	ret = nvme_uring_cmd_checks(issue_flags);
581 	if (ret)
582 		return ret;
583 
584 	switch (ioucmd->cmd_op) {
585 	case NVME_URING_CMD_IO:
586 		ret = nvme_uring_cmd_io(ctrl, ns, ioucmd, issue_flags, false);
587 		break;
588 	case NVME_URING_CMD_IO_VEC:
589 		ret = nvme_uring_cmd_io(ctrl, ns, ioucmd, issue_flags, true);
590 		break;
591 	default:
592 		ret = -ENOTTY;
593 	}
594 
595 	return ret;
596 }
597 
598 int nvme_ns_chr_uring_cmd(struct io_uring_cmd *ioucmd, unsigned int issue_flags)
599 {
600 	struct nvme_ns *ns = container_of(file_inode(ioucmd->file)->i_cdev,
601 			struct nvme_ns, cdev);
602 
603 	return nvme_ns_uring_cmd(ns, ioucmd, issue_flags);
604 }
605 
606 #ifdef CONFIG_NVME_MULTIPATH
607 static int nvme_ns_head_ctrl_ioctl(struct nvme_ns *ns, unsigned int cmd,
608 		void __user *argp, struct nvme_ns_head *head, int srcu_idx)
609 	__releases(&head->srcu)
610 {
611 	struct nvme_ctrl *ctrl = ns->ctrl;
612 	int ret;
613 
614 	nvme_get_ctrl(ns->ctrl);
615 	srcu_read_unlock(&head->srcu, srcu_idx);
616 	ret = nvme_ctrl_ioctl(ns->ctrl, cmd, argp);
617 
618 	nvme_put_ctrl(ctrl);
619 	return ret;
620 }
621 
622 int nvme_ns_head_ioctl(struct block_device *bdev, fmode_t mode,
623 		unsigned int cmd, unsigned long arg)
624 {
625 	struct nvme_ns_head *head = bdev->bd_disk->private_data;
626 	void __user *argp = (void __user *)arg;
627 	struct nvme_ns *ns;
628 	int srcu_idx, ret = -EWOULDBLOCK;
629 
630 	srcu_idx = srcu_read_lock(&head->srcu);
631 	ns = nvme_find_path(head);
632 	if (!ns)
633 		goto out_unlock;
634 
635 	/*
636 	 * Handle ioctls that apply to the controller instead of the namespace
637 	 * seperately and drop the ns SRCU reference early.  This avoids a
638 	 * deadlock when deleting namespaces using the passthrough interface.
639 	 */
640 	if (is_ctrl_ioctl(cmd))
641 		return nvme_ns_head_ctrl_ioctl(ns, cmd, argp, head, srcu_idx);
642 
643 	ret = nvme_ns_ioctl(ns, cmd, argp);
644 out_unlock:
645 	srcu_read_unlock(&head->srcu, srcu_idx);
646 	return ret;
647 }
648 
649 long nvme_ns_head_chr_ioctl(struct file *file, unsigned int cmd,
650 		unsigned long arg)
651 {
652 	struct cdev *cdev = file_inode(file)->i_cdev;
653 	struct nvme_ns_head *head =
654 		container_of(cdev, struct nvme_ns_head, cdev);
655 	void __user *argp = (void __user *)arg;
656 	struct nvme_ns *ns;
657 	int srcu_idx, ret = -EWOULDBLOCK;
658 
659 	srcu_idx = srcu_read_lock(&head->srcu);
660 	ns = nvme_find_path(head);
661 	if (!ns)
662 		goto out_unlock;
663 
664 	if (is_ctrl_ioctl(cmd))
665 		return nvme_ns_head_ctrl_ioctl(ns, cmd, argp, head, srcu_idx);
666 
667 	ret = nvme_ns_ioctl(ns, cmd, argp);
668 out_unlock:
669 	srcu_read_unlock(&head->srcu, srcu_idx);
670 	return ret;
671 }
672 
673 int nvme_ns_head_chr_uring_cmd(struct io_uring_cmd *ioucmd,
674 		unsigned int issue_flags)
675 {
676 	struct cdev *cdev = file_inode(ioucmd->file)->i_cdev;
677 	struct nvme_ns_head *head = container_of(cdev, struct nvme_ns_head, cdev);
678 	int srcu_idx = srcu_read_lock(&head->srcu);
679 	struct nvme_ns *ns = nvme_find_path(head);
680 	int ret = -EINVAL;
681 
682 	if (ns)
683 		ret = nvme_ns_uring_cmd(ns, ioucmd, issue_flags);
684 	srcu_read_unlock(&head->srcu, srcu_idx);
685 	return ret;
686 }
687 #endif /* CONFIG_NVME_MULTIPATH */
688 
689 int nvme_dev_uring_cmd(struct io_uring_cmd *ioucmd, unsigned int issue_flags)
690 {
691 	struct nvme_ctrl *ctrl = ioucmd->file->private_data;
692 	int ret;
693 
694 	ret = nvme_uring_cmd_checks(issue_flags);
695 	if (ret)
696 		return ret;
697 
698 	switch (ioucmd->cmd_op) {
699 	case NVME_URING_CMD_ADMIN:
700 		ret = nvme_uring_cmd_io(ctrl, NULL, ioucmd, issue_flags, false);
701 		break;
702 	case NVME_URING_CMD_ADMIN_VEC:
703 		ret = nvme_uring_cmd_io(ctrl, NULL, ioucmd, issue_flags, true);
704 		break;
705 	default:
706 		ret = -ENOTTY;
707 	}
708 
709 	return ret;
710 }
711 
712 static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp)
713 {
714 	struct nvme_ns *ns;
715 	int ret;
716 
717 	down_read(&ctrl->namespaces_rwsem);
718 	if (list_empty(&ctrl->namespaces)) {
719 		ret = -ENOTTY;
720 		goto out_unlock;
721 	}
722 
723 	ns = list_first_entry(&ctrl->namespaces, struct nvme_ns, list);
724 	if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
725 		dev_warn(ctrl->device,
726 			"NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
727 		ret = -EINVAL;
728 		goto out_unlock;
729 	}
730 
731 	dev_warn(ctrl->device,
732 		"using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
733 	kref_get(&ns->kref);
734 	up_read(&ctrl->namespaces_rwsem);
735 
736 	ret = nvme_user_cmd(ctrl, ns, argp);
737 	nvme_put_ns(ns);
738 	return ret;
739 
740 out_unlock:
741 	up_read(&ctrl->namespaces_rwsem);
742 	return ret;
743 }
744 
745 long nvme_dev_ioctl(struct file *file, unsigned int cmd,
746 		unsigned long arg)
747 {
748 	struct nvme_ctrl *ctrl = file->private_data;
749 	void __user *argp = (void __user *)arg;
750 
751 	switch (cmd) {
752 	case NVME_IOCTL_ADMIN_CMD:
753 		return nvme_user_cmd(ctrl, NULL, argp);
754 	case NVME_IOCTL_ADMIN64_CMD:
755 		return nvme_user_cmd64(ctrl, NULL, argp, false);
756 	case NVME_IOCTL_IO_CMD:
757 		return nvme_dev_user_cmd(ctrl, argp);
758 	case NVME_IOCTL_RESET:
759 		dev_warn(ctrl->device, "resetting controller\n");
760 		return nvme_reset_ctrl_sync(ctrl);
761 	case NVME_IOCTL_SUBSYS_RESET:
762 		return nvme_reset_subsystem(ctrl);
763 	case NVME_IOCTL_RESCAN:
764 		nvme_queue_scan(ctrl);
765 		return 0;
766 	default:
767 		return -ENOTTY;
768 	}
769 }
770