ioctl.c 23 KB

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