xref: /linux/drivers/nvme/host/ioctl.c (revision 4482ebb2970efa58173075c101426b2f3af40b41)
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/blk-integrity.h>
7 #include <linux/ptrace.h>	/* for force_successful_syscall_return */
8 #include <linux/nvme_ioctl.h>
9 #include <linux/io_uring/cmd.h>
10 #include "nvme.h"
11 
12 enum {
13 	NVME_IOCTL_VEC		= (1 << 0),
14 	NVME_IOCTL_PARTITION	= (1 << 1),
15 };
16 
nvme_cmd_allowed(struct nvme_ns * ns,struct nvme_command * c,unsigned int flags,bool open_for_write)17 static bool nvme_cmd_allowed(struct nvme_ns *ns, struct nvme_command *c,
18 		unsigned int flags, bool open_for_write)
19 {
20 	u32 effects;
21 
22 	/*
23 	 * Do not allow unprivileged passthrough on partitions, as that allows an
24 	 * escape from the containment of the partition.
25 	 */
26 	if (flags & NVME_IOCTL_PARTITION)
27 		goto admin;
28 
29 	/*
30 	 * Do not allow unprivileged processes to send vendor specific or fabrics
31 	 * commands as we can't be sure about their effects.
32 	 */
33 	if (c->common.opcode >= nvme_cmd_vendor_start ||
34 	    c->common.opcode == nvme_fabrics_command)
35 		goto admin;
36 
37 	/*
38 	 * Do not allow unprivileged passthrough of admin commands except
39 	 * for a subset of identify commands that contain information required
40 	 * to form proper I/O commands in userspace and do not expose any
41 	 * potentially sensitive information.
42 	 */
43 	if (!ns) {
44 		if (c->common.opcode == nvme_admin_identify) {
45 			switch (c->identify.cns) {
46 			case NVME_ID_CNS_NS:
47 			case NVME_ID_CNS_CS_NS:
48 			case NVME_ID_CNS_NS_CS_INDEP:
49 			case NVME_ID_CNS_CS_CTRL:
50 			case NVME_ID_CNS_CTRL:
51 				return true;
52 			}
53 		}
54 		goto admin;
55 	}
56 
57 	/*
58 	 * Check if the controller provides a Commands Supported and Effects log
59 	 * and marks this command as supported.  If not reject unprivileged
60 	 * passthrough.
61 	 */
62 	effects = nvme_command_effects(ns->ctrl, ns, c->common.opcode);
63 	if (!(effects & NVME_CMD_EFFECTS_CSUPP))
64 		goto admin;
65 
66 	/*
67 	 * Don't allow passthrough for command that have intrusive (or unknown)
68 	 * effects.
69 	 */
70 	if (effects & ~(NVME_CMD_EFFECTS_CSUPP | NVME_CMD_EFFECTS_LBCC |
71 			NVME_CMD_EFFECTS_UUID_SEL |
72 			NVME_CMD_EFFECTS_SCOPE_MASK))
73 		goto admin;
74 
75 	/*
76 	 * Only allow I/O commands that transfer data to the controller or that
77 	 * change the logical block contents if the file descriptor is open for
78 	 * writing.
79 	 */
80 	if ((nvme_is_write(c) || (effects & NVME_CMD_EFFECTS_LBCC)) &&
81 	    !open_for_write)
82 		goto admin;
83 
84 	return true;
85 admin:
86 	return capable(CAP_SYS_ADMIN);
87 }
88 
89 /*
90  * Convert integer values from ioctl structures to user pointers, silently
91  * ignoring the upper bits in the compat case to match behaviour of 32-bit
92  * kernels.
93  */
nvme_to_user_ptr(uintptr_t ptrval)94 static void __user *nvme_to_user_ptr(uintptr_t ptrval)
95 {
96 	if (in_compat_syscall())
97 		ptrval = (compat_uptr_t)ptrval;
98 	return (void __user *)ptrval;
99 }
100 
nvme_alloc_user_request(struct request_queue * q,struct nvme_command * cmd,blk_opf_t rq_flags,blk_mq_req_flags_t blk_flags)101 static struct request *nvme_alloc_user_request(struct request_queue *q,
102 		struct nvme_command *cmd, blk_opf_t rq_flags,
103 		blk_mq_req_flags_t blk_flags)
104 {
105 	struct request *req;
106 
107 	req = blk_mq_alloc_request(q, nvme_req_op(cmd) | rq_flags, blk_flags);
108 	if (IS_ERR(req))
109 		return req;
110 	nvme_init_request(req, cmd);
111 	nvme_req(req)->flags |= NVME_REQ_USERCMD;
112 	return req;
113 }
114 
nvme_map_user_request(struct request * req,u64 ubuffer,unsigned bufflen,void __user * meta_buffer,unsigned meta_len,struct iov_iter * iter,unsigned int flags)115 static int nvme_map_user_request(struct request *req, u64 ubuffer,
116 		unsigned bufflen, void __user *meta_buffer, unsigned meta_len,
117 		struct iov_iter *iter, unsigned int flags)
118 {
119 	struct request_queue *q = req->q;
120 	struct nvme_ns *ns = q->queuedata;
121 	struct block_device *bdev = ns ? ns->disk->part0 : NULL;
122 	bool supports_metadata = bdev && blk_get_integrity(bdev->bd_disk);
123 	struct nvme_ctrl *ctrl = nvme_req(req)->ctrl;
124 	bool has_metadata = meta_buffer && meta_len;
125 	struct bio *bio = NULL;
126 	int ret;
127 
128 	if (!nvme_ctrl_sgl_supported(ctrl))
129 		dev_warn_once(ctrl->device, "using unchecked data buffer\n");
130 	if (has_metadata) {
131 		if (!supports_metadata)
132 			return -EINVAL;
133 
134 		if (!nvme_ctrl_meta_sgl_supported(ctrl))
135 			dev_warn_once(ctrl->device,
136 				      "using unchecked metadata buffer\n");
137 	}
138 
139 	if (iter)
140 		ret = blk_rq_map_user_iov(q, req, NULL, iter, GFP_KERNEL);
141 	else
142 		ret = blk_rq_map_user_io(req, NULL, nvme_to_user_ptr(ubuffer),
143 				bufflen, GFP_KERNEL, flags & NVME_IOCTL_VEC, 0,
144 				0, rq_data_dir(req));
145 	if (ret)
146 		return ret;
147 
148 	if (has_metadata) {
149 		ret = blk_rq_integrity_map_user(req, meta_buffer, meta_len);
150 		if (ret)
151 			goto out_unmap;
152 	}
153 
154 	return ret;
155 
156 out_unmap:
157 	if (bio)
158 		blk_rq_unmap_user(bio);
159 	return ret;
160 }
161 
nvme_submit_user_cmd(struct request_queue * q,struct nvme_command * cmd,u64 ubuffer,unsigned bufflen,void __user * meta_buffer,unsigned meta_len,u64 * result,unsigned timeout,unsigned int flags)162 static int nvme_submit_user_cmd(struct request_queue *q,
163 		struct nvme_command *cmd, u64 ubuffer, unsigned bufflen,
164 		void __user *meta_buffer, unsigned meta_len,
165 		u64 *result, unsigned timeout, unsigned int flags)
166 {
167 	struct nvme_ns *ns = q->queuedata;
168 	struct nvme_ctrl *ctrl;
169 	struct request *req;
170 	struct bio *bio;
171 	u32 effects;
172 	int ret;
173 
174 	req = nvme_alloc_user_request(q, cmd, 0, 0);
175 	if (IS_ERR(req))
176 		return PTR_ERR(req);
177 
178 	req->timeout = timeout;
179 	if (ubuffer && bufflen) {
180 		ret = nvme_map_user_request(req, ubuffer, bufflen, meta_buffer,
181 				meta_len, NULL, flags);
182 		if (ret)
183 			goto out_free_req;
184 	}
185 
186 	bio = req->bio;
187 	ctrl = nvme_req(req)->ctrl;
188 
189 	effects = nvme_passthru_start(ctrl, ns, cmd->common.opcode);
190 	ret = nvme_execute_rq(req, false);
191 	if (result)
192 		*result = le64_to_cpu(nvme_req(req)->result.u64);
193 	if (bio)
194 		blk_rq_unmap_user(bio);
195 	blk_mq_free_request(req);
196 
197 	if (effects)
198 		nvme_passthru_end(ctrl, ns, effects, cmd, ret);
199 	return ret;
200 
201 out_free_req:
202 	blk_mq_free_request(req);
203 	return ret;
204 }
205 
nvme_submit_io(struct nvme_ns * ns,struct nvme_user_io __user * uio)206 static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
207 {
208 	struct nvme_user_io io;
209 	struct nvme_command c;
210 	unsigned length, meta_len;
211 	void __user *metadata;
212 
213 	if (copy_from_user(&io, uio, sizeof(io)))
214 		return -EFAULT;
215 	if (io.flags)
216 		return -EINVAL;
217 
218 	switch (io.opcode) {
219 	case nvme_cmd_write:
220 	case nvme_cmd_read:
221 	case nvme_cmd_compare:
222 		break;
223 	default:
224 		return -EINVAL;
225 	}
226 
227 	length = (io.nblocks + 1) << ns->head->lba_shift;
228 
229 	if ((io.control & NVME_RW_PRINFO_PRACT) &&
230 	    (ns->head->ms == ns->head->pi_size)) {
231 		/*
232 		 * Protection information is stripped/inserted by the
233 		 * controller.
234 		 */
235 		if (nvme_to_user_ptr(io.metadata))
236 			return -EINVAL;
237 		meta_len = 0;
238 		metadata = NULL;
239 	} else {
240 		meta_len = (io.nblocks + 1) * ns->head->ms;
241 		metadata = nvme_to_user_ptr(io.metadata);
242 	}
243 
244 	if (ns->head->features & NVME_NS_EXT_LBAS) {
245 		length += meta_len;
246 		meta_len = 0;
247 	} else if (meta_len) {
248 		if ((io.metadata & 3) || !io.metadata)
249 			return -EINVAL;
250 	}
251 
252 	memset(&c, 0, sizeof(c));
253 	c.rw.opcode = io.opcode;
254 	c.rw.flags = io.flags;
255 	c.rw.nsid = cpu_to_le32(ns->head->ns_id);
256 	c.rw.slba = cpu_to_le64(io.slba);
257 	c.rw.length = cpu_to_le16(io.nblocks);
258 	c.rw.control = cpu_to_le16(io.control);
259 	c.rw.dsmgmt = cpu_to_le32(io.dsmgmt);
260 	c.rw.reftag = cpu_to_le32(io.reftag);
261 	c.rw.lbat = cpu_to_le16(io.apptag);
262 	c.rw.lbatm = cpu_to_le16(io.appmask);
263 
264 	return nvme_submit_user_cmd(ns->queue, &c, io.addr, length, metadata,
265 			meta_len, NULL, 0, 0);
266 }
267 
nvme_validate_passthru_nsid(struct nvme_ctrl * ctrl,struct nvme_ns * ns,__u32 nsid)268 static bool nvme_validate_passthru_nsid(struct nvme_ctrl *ctrl,
269 					struct nvme_ns *ns, __u32 nsid)
270 {
271 	if (ns && nsid != ns->head->ns_id) {
272 		dev_err(ctrl->device,
273 			"%s: nsid (%u) in cmd does not match nsid (%u) of namespace\n",
274 			current->comm, nsid, ns->head->ns_id);
275 		return false;
276 	}
277 
278 	return true;
279 }
280 
nvme_user_cmd(struct nvme_ctrl * ctrl,struct nvme_ns * ns,struct nvme_passthru_cmd __user * ucmd,unsigned int flags,bool open_for_write)281 static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
282 		struct nvme_passthru_cmd __user *ucmd, unsigned int flags,
283 		bool open_for_write)
284 {
285 	struct nvme_passthru_cmd cmd;
286 	struct nvme_command c;
287 	unsigned timeout = 0;
288 	u64 result;
289 	int status;
290 
291 	if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
292 		return -EFAULT;
293 	if (cmd.flags)
294 		return -EINVAL;
295 	if (!nvme_validate_passthru_nsid(ctrl, ns, cmd.nsid))
296 		return -EINVAL;
297 
298 	memset(&c, 0, sizeof(c));
299 	c.common.opcode = cmd.opcode;
300 	c.common.flags = cmd.flags;
301 	c.common.nsid = cpu_to_le32(cmd.nsid);
302 	c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
303 	c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
304 	c.common.cdw10 = cpu_to_le32(cmd.cdw10);
305 	c.common.cdw11 = cpu_to_le32(cmd.cdw11);
306 	c.common.cdw12 = cpu_to_le32(cmd.cdw12);
307 	c.common.cdw13 = cpu_to_le32(cmd.cdw13);
308 	c.common.cdw14 = cpu_to_le32(cmd.cdw14);
309 	c.common.cdw15 = cpu_to_le32(cmd.cdw15);
310 
311 	if (!nvme_cmd_allowed(ns, &c, 0, open_for_write))
312 		return -EACCES;
313 
314 	if (cmd.timeout_ms)
315 		timeout = msecs_to_jiffies(cmd.timeout_ms);
316 
317 	status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
318 			cmd.addr, cmd.data_len, nvme_to_user_ptr(cmd.metadata),
319 			cmd.metadata_len, &result, timeout, 0);
320 
321 	if (status >= 0) {
322 		if (put_user(result, &ucmd->result))
323 			return -EFAULT;
324 	}
325 
326 	return status;
327 }
328 
nvme_user_cmd64(struct nvme_ctrl * ctrl,struct nvme_ns * ns,struct nvme_passthru_cmd64 __user * ucmd,unsigned int flags,bool open_for_write)329 static int nvme_user_cmd64(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
330 		struct nvme_passthru_cmd64 __user *ucmd, unsigned int flags,
331 		bool open_for_write)
332 {
333 	struct nvme_passthru_cmd64 cmd;
334 	struct nvme_command c;
335 	unsigned timeout = 0;
336 	int status;
337 
338 	if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
339 		return -EFAULT;
340 	if (cmd.flags)
341 		return -EINVAL;
342 	if (!nvme_validate_passthru_nsid(ctrl, ns, cmd.nsid))
343 		return -EINVAL;
344 
345 	memset(&c, 0, sizeof(c));
346 	c.common.opcode = cmd.opcode;
347 	c.common.flags = cmd.flags;
348 	c.common.nsid = cpu_to_le32(cmd.nsid);
349 	c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
350 	c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
351 	c.common.cdw10 = cpu_to_le32(cmd.cdw10);
352 	c.common.cdw11 = cpu_to_le32(cmd.cdw11);
353 	c.common.cdw12 = cpu_to_le32(cmd.cdw12);
354 	c.common.cdw13 = cpu_to_le32(cmd.cdw13);
355 	c.common.cdw14 = cpu_to_le32(cmd.cdw14);
356 	c.common.cdw15 = cpu_to_le32(cmd.cdw15);
357 
358 	if (!nvme_cmd_allowed(ns, &c, flags, open_for_write))
359 		return -EACCES;
360 
361 	if (cmd.timeout_ms)
362 		timeout = msecs_to_jiffies(cmd.timeout_ms);
363 
364 	status = nvme_submit_user_cmd(ns ? ns->queue : ctrl->admin_q, &c,
365 			cmd.addr, cmd.data_len, nvme_to_user_ptr(cmd.metadata),
366 			cmd.metadata_len, &cmd.result, timeout, flags);
367 
368 	if (status >= 0) {
369 		if (put_user(cmd.result, &ucmd->result))
370 			return -EFAULT;
371 	}
372 
373 	return status;
374 }
375 
376 struct nvme_uring_data {
377 	__u64	metadata;
378 	__u64	addr;
379 	__u32	data_len;
380 	__u32	metadata_len;
381 	__u32	timeout_ms;
382 };
383 
384 /*
385  * This overlays struct io_uring_cmd pdu.
386  * Expect build errors if this grows larger than that.
387  */
388 struct nvme_uring_cmd_pdu {
389 	struct request *req;
390 	struct bio *bio;
391 	u64 result;
392 	int status;
393 };
394 
nvme_uring_cmd_pdu(struct io_uring_cmd * ioucmd)395 static inline struct nvme_uring_cmd_pdu *nvme_uring_cmd_pdu(
396 		struct io_uring_cmd *ioucmd)
397 {
398 	return io_uring_cmd_to_pdu(ioucmd, struct nvme_uring_cmd_pdu);
399 }
400 
nvme_uring_task_cb(struct io_tw_req tw_req,io_tw_token_t tw)401 static void nvme_uring_task_cb(struct io_tw_req tw_req, io_tw_token_t tw)
402 {
403 	struct io_uring_cmd *ioucmd = io_uring_cmd_from_tw(tw_req);
404 	struct nvme_uring_cmd_pdu *pdu = nvme_uring_cmd_pdu(ioucmd);
405 
406 	if (pdu->bio)
407 		blk_rq_unmap_user(pdu->bio);
408 	io_uring_cmd_done32(ioucmd, pdu->status, pdu->result,
409 			    IO_URING_CMD_TASK_WORK_ISSUE_FLAGS);
410 }
411 
nvme_uring_cmd_end_io(struct request * req,blk_status_t err)412 static enum rq_end_io_ret nvme_uring_cmd_end_io(struct request *req,
413 						blk_status_t err)
414 {
415 	struct io_uring_cmd *ioucmd = req->end_io_data;
416 	struct nvme_uring_cmd_pdu *pdu = nvme_uring_cmd_pdu(ioucmd);
417 
418 	if (nvme_req(req)->flags & NVME_REQ_CANCELLED) {
419 		pdu->status = -EINTR;
420 	} else {
421 		pdu->status = nvme_req(req)->status;
422 		if (!pdu->status)
423 			pdu->status = blk_status_to_errno(err);
424 	}
425 	pdu->result = le64_to_cpu(nvme_req(req)->result.u64);
426 
427 	/*
428 	 * IOPOLL could potentially complete this request directly, but
429 	 * if multiple rings are polling on the same queue, then it's possible
430 	 * for one ring to find completions for another ring. Punting the
431 	 * completion via task_work will always direct it to the right
432 	 * location, rather than potentially complete requests for ringA
433 	 * under iopoll invocations from ringB.
434 	 */
435 	io_uring_cmd_do_in_task_lazy(ioucmd, nvme_uring_task_cb);
436 	return RQ_END_IO_FREE;
437 }
438 
nvme_uring_cmd_io(struct nvme_ctrl * ctrl,struct nvme_ns * ns,struct io_uring_cmd * ioucmd,unsigned int issue_flags,bool vec)439 static int nvme_uring_cmd_io(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
440 		struct io_uring_cmd *ioucmd, unsigned int issue_flags, bool vec)
441 {
442 	struct nvme_uring_cmd_pdu *pdu = nvme_uring_cmd_pdu(ioucmd);
443 	const struct nvme_uring_cmd *cmd = io_uring_sqe_cmd(ioucmd->sqe);
444 	struct request_queue *q = ns ? ns->queue : ctrl->admin_q;
445 	struct nvme_uring_data d;
446 	struct nvme_command c;
447 	struct iov_iter iter;
448 	struct iov_iter *map_iter = NULL;
449 	struct request *req;
450 	blk_opf_t rq_flags = 0;
451 	blk_mq_req_flags_t blk_flags = 0;
452 	int ret;
453 
454 	c.common.opcode = READ_ONCE(cmd->opcode);
455 	c.common.flags = READ_ONCE(cmd->flags);
456 	if (c.common.flags)
457 		return -EINVAL;
458 
459 	c.common.command_id = 0;
460 	c.common.nsid = cpu_to_le32(cmd->nsid);
461 	if (!nvme_validate_passthru_nsid(ctrl, ns, le32_to_cpu(c.common.nsid)))
462 		return -EINVAL;
463 
464 	c.common.cdw2[0] = cpu_to_le32(READ_ONCE(cmd->cdw2));
465 	c.common.cdw2[1] = cpu_to_le32(READ_ONCE(cmd->cdw3));
466 	c.common.metadata = 0;
467 	c.common.dptr.prp1 = c.common.dptr.prp2 = 0;
468 	c.common.cdw10 = cpu_to_le32(READ_ONCE(cmd->cdw10));
469 	c.common.cdw11 = cpu_to_le32(READ_ONCE(cmd->cdw11));
470 	c.common.cdw12 = cpu_to_le32(READ_ONCE(cmd->cdw12));
471 	c.common.cdw13 = cpu_to_le32(READ_ONCE(cmd->cdw13));
472 	c.common.cdw14 = cpu_to_le32(READ_ONCE(cmd->cdw14));
473 	c.common.cdw15 = cpu_to_le32(READ_ONCE(cmd->cdw15));
474 
475 	if (!nvme_cmd_allowed(ns, &c, 0, ioucmd->file->f_mode & FMODE_WRITE))
476 		return -EACCES;
477 
478 	d.metadata = READ_ONCE(cmd->metadata);
479 	d.addr = READ_ONCE(cmd->addr);
480 	d.data_len = READ_ONCE(cmd->data_len);
481 	d.metadata_len = READ_ONCE(cmd->metadata_len);
482 	d.timeout_ms = READ_ONCE(cmd->timeout_ms);
483 
484 	if (d.data_len && (ioucmd->flags & IORING_URING_CMD_FIXED)) {
485 		int ddir = nvme_is_write(&c) ? WRITE : READ;
486 
487 		if (vec)
488 			ret = io_uring_cmd_import_fixed_vec(ioucmd,
489 					u64_to_user_ptr(d.addr), d.data_len,
490 					ddir, &iter, issue_flags);
491 		else
492 			ret = io_uring_cmd_import_fixed(d.addr, d.data_len,
493 					ddir, &iter, ioucmd, issue_flags);
494 		if (ret < 0)
495 			return ret;
496 
497 		map_iter = &iter;
498 	}
499 
500 	if (issue_flags & IO_URING_F_NONBLOCK) {
501 		rq_flags |= REQ_NOWAIT;
502 		blk_flags = BLK_MQ_REQ_NOWAIT;
503 	}
504 	if (issue_flags & IO_URING_F_IOPOLL)
505 		rq_flags |= REQ_POLLED;
506 
507 	req = nvme_alloc_user_request(q, &c, rq_flags, blk_flags);
508 	if (IS_ERR(req))
509 		return PTR_ERR(req);
510 	req->timeout = d.timeout_ms ? msecs_to_jiffies(d.timeout_ms) : 0;
511 
512 	if (d.data_len) {
513 		ret = nvme_map_user_request(req, d.addr, d.data_len,
514 			nvme_to_user_ptr(d.metadata), d.metadata_len,
515 			map_iter, vec ? NVME_IOCTL_VEC : 0);
516 		if (ret)
517 			goto out_free_req;
518 	}
519 
520 	/* to free bio on completion, as req->bio will be null at that time */
521 	pdu->bio = req->bio;
522 	pdu->req = req;
523 	req->end_io_data = ioucmd;
524 	req->end_io = nvme_uring_cmd_end_io;
525 	blk_execute_rq_nowait(req, false);
526 	return -EIOCBQUEUED;
527 
528 out_free_req:
529 	blk_mq_free_request(req);
530 	return ret;
531 }
532 
is_ctrl_ioctl(unsigned int cmd)533 static bool is_ctrl_ioctl(unsigned int cmd)
534 {
535 	if (cmd == NVME_IOCTL_ADMIN_CMD || cmd == NVME_IOCTL_ADMIN64_CMD)
536 		return true;
537 	if (is_sed_ioctl(cmd))
538 		return true;
539 	return false;
540 }
541 
nvme_ctrl_ioctl(struct nvme_ctrl * ctrl,unsigned int cmd,void __user * argp,bool open_for_write)542 static int nvme_ctrl_ioctl(struct nvme_ctrl *ctrl, unsigned int cmd,
543 		void __user *argp, bool open_for_write)
544 {
545 	switch (cmd) {
546 	case NVME_IOCTL_ADMIN_CMD:
547 		return nvme_user_cmd(ctrl, NULL, argp, 0, open_for_write);
548 	case NVME_IOCTL_ADMIN64_CMD:
549 		return nvme_user_cmd64(ctrl, NULL, argp, 0, open_for_write);
550 	default:
551 		return sed_ioctl(ctrl->opal_dev, cmd, argp);
552 	}
553 }
554 
555 #ifdef COMPAT_FOR_U64_ALIGNMENT
556 struct nvme_user_io32 {
557 	__u8	opcode;
558 	__u8	flags;
559 	__u16	control;
560 	__u16	nblocks;
561 	__u16	rsvd;
562 	__u64	metadata;
563 	__u64	addr;
564 	__u64	slba;
565 	__u32	dsmgmt;
566 	__u32	reftag;
567 	__u16	apptag;
568 	__u16	appmask;
569 } __attribute__((__packed__));
570 #define NVME_IOCTL_SUBMIT_IO32	_IOW('N', 0x42, struct nvme_user_io32)
571 #endif /* COMPAT_FOR_U64_ALIGNMENT */
572 
nvme_ns_ioctl(struct nvme_ns * ns,unsigned int cmd,void __user * argp,unsigned int flags,bool open_for_write)573 static int nvme_ns_ioctl(struct nvme_ns *ns, unsigned int cmd,
574 		void __user *argp, unsigned int flags, bool open_for_write)
575 {
576 	switch (cmd) {
577 	case NVME_IOCTL_ID:
578 		force_successful_syscall_return();
579 		return ns->head->ns_id;
580 	case NVME_IOCTL_IO_CMD:
581 		return nvme_user_cmd(ns->ctrl, ns, argp, flags, open_for_write);
582 	/*
583 	 * struct nvme_user_io can have different padding on some 32-bit ABIs.
584 	 * Just accept the compat version as all fields that are used are the
585 	 * same size and at the same offset.
586 	 */
587 #ifdef COMPAT_FOR_U64_ALIGNMENT
588 	case NVME_IOCTL_SUBMIT_IO32:
589 #endif
590 	case NVME_IOCTL_SUBMIT_IO:
591 		return nvme_submit_io(ns, argp);
592 	case NVME_IOCTL_IO64_CMD_VEC:
593 		flags |= NVME_IOCTL_VEC;
594 		fallthrough;
595 	case NVME_IOCTL_IO64_CMD:
596 		return nvme_user_cmd64(ns->ctrl, ns, argp, flags,
597 				       open_for_write);
598 	default:
599 		return -ENOTTY;
600 	}
601 }
602 
nvme_ioctl(struct block_device * bdev,blk_mode_t mode,unsigned int cmd,unsigned long arg)603 int nvme_ioctl(struct block_device *bdev, blk_mode_t mode,
604 		unsigned int cmd, unsigned long arg)
605 {
606 	struct nvme_ns *ns = bdev->bd_disk->private_data;
607 	bool open_for_write = mode & BLK_OPEN_WRITE;
608 	void __user *argp = (void __user *)arg;
609 	unsigned int flags = 0;
610 
611 	if (bdev_is_partition(bdev))
612 		flags |= NVME_IOCTL_PARTITION;
613 
614 	if (is_ctrl_ioctl(cmd))
615 		return nvme_ctrl_ioctl(ns->ctrl, cmd, argp, open_for_write);
616 	return nvme_ns_ioctl(ns, cmd, argp, flags, open_for_write);
617 }
618 
nvme_ns_chr_ioctl(struct file * file,unsigned int cmd,unsigned long arg)619 long nvme_ns_chr_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
620 {
621 	struct nvme_ns *ns =
622 		container_of(file_inode(file)->i_cdev, struct nvme_ns, cdev);
623 	bool open_for_write = file->f_mode & FMODE_WRITE;
624 	void __user *argp = (void __user *)arg;
625 
626 	if (is_ctrl_ioctl(cmd))
627 		return nvme_ctrl_ioctl(ns->ctrl, cmd, argp, open_for_write);
628 	return nvme_ns_ioctl(ns, cmd, argp, 0, open_for_write);
629 }
630 
nvme_uring_cmd_checks(unsigned int issue_flags)631 static int nvme_uring_cmd_checks(unsigned int issue_flags)
632 {
633 
634 	/* NVMe passthrough requires big SQE/CQE support */
635 	if ((issue_flags & (IO_URING_F_SQE128|IO_URING_F_CQE32)) !=
636 	    (IO_URING_F_SQE128|IO_URING_F_CQE32))
637 		return -EOPNOTSUPP;
638 	return 0;
639 }
640 
nvme_ns_uring_cmd(struct nvme_ns * ns,struct io_uring_cmd * ioucmd,unsigned int issue_flags)641 static int nvme_ns_uring_cmd(struct nvme_ns *ns, struct io_uring_cmd *ioucmd,
642 			     unsigned int issue_flags)
643 {
644 	struct nvme_ctrl *ctrl = ns->ctrl;
645 	int ret;
646 
647 	ret = nvme_uring_cmd_checks(issue_flags);
648 	if (ret)
649 		return ret;
650 
651 	switch (ioucmd->cmd_op) {
652 	case NVME_URING_CMD_IO:
653 		ret = nvme_uring_cmd_io(ctrl, ns, ioucmd, issue_flags, false);
654 		break;
655 	case NVME_URING_CMD_IO_VEC:
656 		ret = nvme_uring_cmd_io(ctrl, ns, ioucmd, issue_flags, true);
657 		break;
658 	default:
659 		ret = -ENOTTY;
660 	}
661 
662 	return ret;
663 }
664 
nvme_ns_chr_uring_cmd(struct io_uring_cmd * ioucmd,unsigned int issue_flags)665 int nvme_ns_chr_uring_cmd(struct io_uring_cmd *ioucmd, unsigned int issue_flags)
666 {
667 	struct nvme_ns *ns = container_of(file_inode(ioucmd->file)->i_cdev,
668 			struct nvme_ns, cdev);
669 
670 	return nvme_ns_uring_cmd(ns, ioucmd, issue_flags);
671 }
672 
nvme_ns_chr_uring_cmd_iopoll(struct io_uring_cmd * ioucmd,struct io_comp_batch * iob,unsigned int poll_flags)673 int nvme_ns_chr_uring_cmd_iopoll(struct io_uring_cmd *ioucmd,
674 				 struct io_comp_batch *iob,
675 				 unsigned int poll_flags)
676 {
677 	struct nvme_uring_cmd_pdu *pdu = nvme_uring_cmd_pdu(ioucmd);
678 	struct request *req = pdu->req;
679 
680 	if (req && blk_rq_is_poll(req))
681 		return blk_rq_poll(req, iob, poll_flags);
682 	return 0;
683 }
684 #ifdef CONFIG_NVME_MULTIPATH
nvme_ns_head_ctrl_ioctl(struct nvme_ns * ns,unsigned int cmd,void __user * argp,struct nvme_ns_head * head,int srcu_idx,bool open_for_write)685 static int nvme_ns_head_ctrl_ioctl(struct nvme_ns *ns, unsigned int cmd,
686 		void __user *argp, struct nvme_ns_head *head, int srcu_idx,
687 		bool open_for_write)
688 	__releases(&head->srcu)
689 {
690 	struct nvme_ctrl *ctrl = ns->ctrl;
691 	int ret;
692 
693 	nvme_get_ctrl(ns->ctrl);
694 	srcu_read_unlock(&head->srcu, srcu_idx);
695 	ret = nvme_ctrl_ioctl(ns->ctrl, cmd, argp, open_for_write);
696 
697 	nvme_put_ctrl(ctrl);
698 	return ret;
699 }
700 
nvme_ns_head_ioctl(struct block_device * bdev,blk_mode_t mode,unsigned int cmd,unsigned long arg)701 int nvme_ns_head_ioctl(struct block_device *bdev, blk_mode_t mode,
702 		unsigned int cmd, unsigned long arg)
703 {
704 	struct nvme_ns_head *head = bdev->bd_disk->private_data;
705 	bool open_for_write = mode & BLK_OPEN_WRITE;
706 	void __user *argp = (void __user *)arg;
707 	struct nvme_ns *ns;
708 	int srcu_idx, ret = -EWOULDBLOCK;
709 	unsigned int flags = 0;
710 
711 	if (bdev_is_partition(bdev))
712 		flags |= NVME_IOCTL_PARTITION;
713 
714 	srcu_idx = srcu_read_lock(&head->srcu);
715 	ns = nvme_find_path(head);
716 	if (!ns)
717 		goto out_unlock;
718 
719 	/*
720 	 * Handle ioctls that apply to the controller instead of the namespace
721 	 * separately and drop the ns SRCU reference early.  This avoids a
722 	 * deadlock when deleting namespaces using the passthrough interface.
723 	 */
724 	if (is_ctrl_ioctl(cmd))
725 		return nvme_ns_head_ctrl_ioctl(ns, cmd, argp, head, srcu_idx,
726 					       open_for_write);
727 
728 	ret = nvme_ns_ioctl(ns, cmd, argp, flags, open_for_write);
729 out_unlock:
730 	srcu_read_unlock(&head->srcu, srcu_idx);
731 	return ret;
732 }
733 
nvme_ns_head_chr_ioctl(struct file * file,unsigned int cmd,unsigned long arg)734 long nvme_ns_head_chr_ioctl(struct file *file, unsigned int cmd,
735 		unsigned long arg)
736 {
737 	bool open_for_write = file->f_mode & FMODE_WRITE;
738 	struct cdev *cdev = file_inode(file)->i_cdev;
739 	struct nvme_ns_head *head =
740 		container_of(cdev, struct nvme_ns_head, cdev);
741 	void __user *argp = (void __user *)arg;
742 	struct nvme_ns *ns;
743 	int srcu_idx, ret = -EWOULDBLOCK;
744 
745 	srcu_idx = srcu_read_lock(&head->srcu);
746 	ns = nvme_find_path(head);
747 	if (!ns)
748 		goto out_unlock;
749 
750 	if (is_ctrl_ioctl(cmd))
751 		return nvme_ns_head_ctrl_ioctl(ns, cmd, argp, head, srcu_idx,
752 				open_for_write);
753 
754 	ret = nvme_ns_ioctl(ns, cmd, argp, 0, open_for_write);
755 out_unlock:
756 	srcu_read_unlock(&head->srcu, srcu_idx);
757 	return ret;
758 }
759 
nvme_ns_head_chr_uring_cmd(struct io_uring_cmd * ioucmd,unsigned int issue_flags)760 int nvme_ns_head_chr_uring_cmd(struct io_uring_cmd *ioucmd,
761 		unsigned int issue_flags)
762 {
763 	struct cdev *cdev = file_inode(ioucmd->file)->i_cdev;
764 	struct nvme_ns_head *head = container_of(cdev, struct nvme_ns_head, cdev);
765 	int srcu_idx = srcu_read_lock(&head->srcu);
766 	struct nvme_ns *ns = nvme_find_path(head);
767 	int ret = -EINVAL;
768 
769 	if (ns)
770 		ret = nvme_ns_uring_cmd(ns, ioucmd, issue_flags);
771 	srcu_read_unlock(&head->srcu, srcu_idx);
772 	return ret;
773 }
774 #endif /* CONFIG_NVME_MULTIPATH */
775 
nvme_dev_uring_cmd(struct io_uring_cmd * ioucmd,unsigned int issue_flags)776 int nvme_dev_uring_cmd(struct io_uring_cmd *ioucmd, unsigned int issue_flags)
777 {
778 	struct nvme_ctrl *ctrl = ioucmd->file->private_data;
779 	int ret;
780 
781 	/* IOPOLL not supported yet */
782 	if (issue_flags & IO_URING_F_IOPOLL)
783 		return -EOPNOTSUPP;
784 
785 	ret = nvme_uring_cmd_checks(issue_flags);
786 	if (ret)
787 		return ret;
788 
789 	switch (ioucmd->cmd_op) {
790 	case NVME_URING_CMD_ADMIN:
791 		ret = nvme_uring_cmd_io(ctrl, NULL, ioucmd, issue_flags, false);
792 		break;
793 	case NVME_URING_CMD_ADMIN_VEC:
794 		ret = nvme_uring_cmd_io(ctrl, NULL, ioucmd, issue_flags, true);
795 		break;
796 	default:
797 		ret = -ENOTTY;
798 	}
799 
800 	return ret;
801 }
802 
nvme_dev_user_cmd(struct nvme_ctrl * ctrl,void __user * argp,bool open_for_write)803 static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp,
804 		bool open_for_write)
805 {
806 	struct nvme_ns *ns;
807 	int ret, srcu_idx;
808 
809 	srcu_idx = srcu_read_lock(&ctrl->srcu);
810 	if (list_empty(&ctrl->namespaces)) {
811 		ret = -ENOTTY;
812 		goto out_unlock;
813 	}
814 
815 	ns = list_first_or_null_rcu(&ctrl->namespaces, struct nvme_ns, list);
816 	if (ns != list_last_entry(&ctrl->namespaces, struct nvme_ns, list)) {
817 		dev_warn(ctrl->device,
818 			"NVME_IOCTL_IO_CMD not supported when multiple namespaces present!\n");
819 		ret = -EINVAL;
820 		goto out_unlock;
821 	}
822 
823 	dev_warn(ctrl->device,
824 		"using deprecated NVME_IOCTL_IO_CMD ioctl on the char device!\n");
825 	if (!nvme_get_ns(ns)) {
826 		ret = -ENXIO;
827 		goto out_unlock;
828 	}
829 	srcu_read_unlock(&ctrl->srcu, srcu_idx);
830 
831 	ret = nvme_user_cmd(ctrl, ns, argp, 0, open_for_write);
832 	nvme_put_ns(ns);
833 	return ret;
834 
835 out_unlock:
836 	srcu_read_unlock(&ctrl->srcu, srcu_idx);
837 	return ret;
838 }
839 
nvme_dev_ioctl(struct file * file,unsigned int cmd,unsigned long arg)840 long nvme_dev_ioctl(struct file *file, unsigned int cmd,
841 		unsigned long arg)
842 {
843 	bool open_for_write = file->f_mode & FMODE_WRITE;
844 	struct nvme_ctrl *ctrl = file->private_data;
845 	void __user *argp = (void __user *)arg;
846 
847 	switch (cmd) {
848 	case NVME_IOCTL_ADMIN_CMD:
849 		return nvme_user_cmd(ctrl, NULL, argp, 0, open_for_write);
850 	case NVME_IOCTL_ADMIN64_CMD:
851 		return nvme_user_cmd64(ctrl, NULL, argp, 0, open_for_write);
852 	case NVME_IOCTL_IO_CMD:
853 		return nvme_dev_user_cmd(ctrl, argp, open_for_write);
854 	case NVME_IOCTL_RESET:
855 		if (!capable(CAP_SYS_ADMIN))
856 			return -EACCES;
857 		dev_warn(ctrl->device, "resetting controller\n");
858 		return nvme_reset_ctrl_sync(ctrl);
859 	case NVME_IOCTL_SUBSYS_RESET:
860 		if (!capable(CAP_SYS_ADMIN))
861 			return -EACCES;
862 		return nvme_reset_subsystem(ctrl);
863 	case NVME_IOCTL_RESCAN:
864 		if (!capable(CAP_SYS_ADMIN))
865 			return -EACCES;
866 		nvme_queue_scan(ctrl);
867 		return 0;
868 	default:
869 		return -ENOTTY;
870 	}
871 }
872