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