199f15d8dSJens Axboe // SPDX-License-Identifier: GPL-2.0
299f15d8dSJens Axboe #include <linux/kernel.h>
399f15d8dSJens Axboe #include <linux/errno.h>
499f15d8dSJens Axboe #include <linux/file.h>
5b66509b8SPavel Begunkov #include <linux/io_uring/cmd.h>
68c9a6f54SPavel Begunkov #include <linux/io_uring/net.h>
72a584012SLuis Chamberlain #include <linux/security.h>
89cda70f6SAnuj Gupta #include <linux/nospec.h>
93fb1764cSKuniyuki Iwashima #include <net/sock.h>
1099f15d8dSJens Axboe
1199f15d8dSJens Axboe #include <uapi/linux/io_uring.h>
121ba0e9d6SAl Viro #include <asm/ioctls.h>
1399f15d8dSJens Axboe
1499f15d8dSJens Axboe #include "io_uring.h"
15414d0f45SJens Axboe #include "alloc_cache.h"
16a9216facSAnuj Gupta #include "rsrc.h"
1799f15d8dSJens Axboe #include "uring_cmd.h"
1899f15d8dSJens Axboe
io_cmd_cache_free(const void * entry)193a4689acSPavel Begunkov void io_cmd_cache_free(const void *entry)
203a4689acSPavel Begunkov {
213a4689acSPavel Begunkov struct io_async_cmd *ac = (struct io_async_cmd *)entry;
223a4689acSPavel Begunkov
233a4689acSPavel Begunkov io_vec_free(&ac->vec);
243a4689acSPavel Begunkov kfree(ac);
253a4689acSPavel Begunkov }
263a4689acSPavel Begunkov
io_req_uring_cleanup(struct io_kiocb * req,unsigned int issue_flags)27d10f19dfSJens Axboe static void io_req_uring_cleanup(struct io_kiocb *req, unsigned int issue_flags)
28d10f19dfSJens Axboe {
29d10f19dfSJens Axboe struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
305f14404bSPavel Begunkov struct io_async_cmd *ac = req->async_data;
315f14404bSPavel Begunkov struct io_uring_cmd_data *cache = &ac->data;
32d10f19dfSJens Axboe
333347fa65SJens Axboe if (cache->op_data) {
343347fa65SJens Axboe kfree(cache->op_data);
353347fa65SJens Axboe cache->op_data = NULL;
363347fa65SJens Axboe }
37d10f19dfSJens Axboe
38d10f19dfSJens Axboe if (issue_flags & IO_URING_F_UNLOCKED)
39d10f19dfSJens Axboe return;
403a4689acSPavel Begunkov
413a4689acSPavel Begunkov io_alloc_cache_vec_kasan(&ac->vec);
423a4689acSPavel Begunkov if (ac->vec.nr > IO_VEC_CACHE_SOFT_CAP)
433a4689acSPavel Begunkov io_vec_free(&ac->vec);
443a4689acSPavel Begunkov
45575e7b06SPavel Begunkov if (io_alloc_cache_put(&req->ctx->cmd_cache, cache)) {
46d10f19dfSJens Axboe ioucmd->sqe = NULL;
47d10f19dfSJens Axboe req->async_data = NULL;
483a4689acSPavel Begunkov req->flags &= ~(REQ_F_ASYNC_DATA|REQ_F_NEED_CLEANUP);
49d10f19dfSJens Axboe }
50d10f19dfSJens Axboe }
51d10f19dfSJens Axboe
io_uring_cmd_cleanup(struct io_kiocb * req)523a4689acSPavel Begunkov void io_uring_cmd_cleanup(struct io_kiocb *req)
533a4689acSPavel Begunkov {
543a4689acSPavel Begunkov io_req_uring_cleanup(req, 0);
553a4689acSPavel Begunkov }
563a4689acSPavel Begunkov
io_uring_try_cancel_uring_cmd(struct io_ring_ctx * ctx,struct io_uring_task * tctx,bool cancel_all)57da12d9abSPavel Begunkov bool io_uring_try_cancel_uring_cmd(struct io_ring_ctx *ctx,
58f03baeceSJens Axboe struct io_uring_task *tctx, bool cancel_all)
59da12d9abSPavel Begunkov {
60da12d9abSPavel Begunkov struct hlist_node *tmp;
61da12d9abSPavel Begunkov struct io_kiocb *req;
62da12d9abSPavel Begunkov bool ret = false;
63da12d9abSPavel Begunkov
64da12d9abSPavel Begunkov lockdep_assert_held(&ctx->uring_lock);
65da12d9abSPavel Begunkov
66da12d9abSPavel Begunkov hlist_for_each_entry_safe(req, tmp, &ctx->cancelable_uring_cmd,
67da12d9abSPavel Begunkov hash_node) {
68da12d9abSPavel Begunkov struct io_uring_cmd *cmd = io_kiocb_to_cmd(req,
69da12d9abSPavel Begunkov struct io_uring_cmd);
70da12d9abSPavel Begunkov struct file *file = req->file;
71da12d9abSPavel Begunkov
72b6f58a3fSJens Axboe if (!cancel_all && req->tctx != tctx)
73da12d9abSPavel Begunkov continue;
74da12d9abSPavel Begunkov
75da12d9abSPavel Begunkov if (cmd->flags & IORING_URING_CMD_CANCELABLE) {
76e1eef2e5SPavel Begunkov file->f_op->uring_cmd(cmd, IO_URING_F_CANCEL |
77e1eef2e5SPavel Begunkov IO_URING_F_COMPLETE_DEFER);
78da12d9abSPavel Begunkov ret = true;
79da12d9abSPavel Begunkov }
80da12d9abSPavel Begunkov }
81da12d9abSPavel Begunkov io_submit_flush_completions(ctx);
82da12d9abSPavel Begunkov return ret;
83da12d9abSPavel Begunkov }
84da12d9abSPavel Begunkov
io_uring_cmd_del_cancelable(struct io_uring_cmd * cmd,unsigned int issue_flags)8593b8cc60SMing Lei static void io_uring_cmd_del_cancelable(struct io_uring_cmd *cmd,
8693b8cc60SMing Lei unsigned int issue_flags)
8793b8cc60SMing Lei {
8893b8cc60SMing Lei struct io_kiocb *req = cmd_to_io_kiocb(cmd);
8993b8cc60SMing Lei struct io_ring_ctx *ctx = req->ctx;
9093b8cc60SMing Lei
9193b8cc60SMing Lei if (!(cmd->flags & IORING_URING_CMD_CANCELABLE))
9293b8cc60SMing Lei return;
9393b8cc60SMing Lei
9493b8cc60SMing Lei cmd->flags &= ~IORING_URING_CMD_CANCELABLE;
9593b8cc60SMing Lei io_ring_submit_lock(ctx, issue_flags);
9693b8cc60SMing Lei hlist_del(&req->hash_node);
9793b8cc60SMing Lei io_ring_submit_unlock(ctx, issue_flags);
9893b8cc60SMing Lei }
9993b8cc60SMing Lei
10093b8cc60SMing Lei /*
10193b8cc60SMing Lei * Mark this command as concelable, then io_uring_try_cancel_uring_cmd()
10293b8cc60SMing Lei * will try to cancel this issued command by sending ->uring_cmd() with
10393b8cc60SMing Lei * issue_flags of IO_URING_F_CANCEL.
10493b8cc60SMing Lei *
10593b8cc60SMing Lei * The command is guaranteed to not be done when calling ->uring_cmd()
10693b8cc60SMing Lei * with IO_URING_F_CANCEL, but it is driver's responsibility to deal
10793b8cc60SMing Lei * with race between io_uring canceling and normal completion.
10893b8cc60SMing Lei */
io_uring_cmd_mark_cancelable(struct io_uring_cmd * cmd,unsigned int issue_flags)10993b8cc60SMing Lei void io_uring_cmd_mark_cancelable(struct io_uring_cmd *cmd,
11093b8cc60SMing Lei unsigned int issue_flags)
11193b8cc60SMing Lei {
11293b8cc60SMing Lei struct io_kiocb *req = cmd_to_io_kiocb(cmd);
11393b8cc60SMing Lei struct io_ring_ctx *ctx = req->ctx;
11493b8cc60SMing Lei
11593b8cc60SMing Lei if (!(cmd->flags & IORING_URING_CMD_CANCELABLE)) {
11693b8cc60SMing Lei cmd->flags |= IORING_URING_CMD_CANCELABLE;
11793b8cc60SMing Lei io_ring_submit_lock(ctx, issue_flags);
11893b8cc60SMing Lei hlist_add_head(&req->hash_node, &ctx->cancelable_uring_cmd);
11993b8cc60SMing Lei io_ring_submit_unlock(ctx, issue_flags);
12093b8cc60SMing Lei }
12193b8cc60SMing Lei }
12293b8cc60SMing Lei EXPORT_SYMBOL_GPL(io_uring_cmd_mark_cancelable);
12393b8cc60SMing Lei
io_uring_cmd_work(struct io_kiocb * req,io_tw_token_t tw)124bcf8a029SCaleb Sander Mateos static void io_uring_cmd_work(struct io_kiocb *req, io_tw_token_t tw)
12599f15d8dSJens Axboe {
126f2ccb5aeSStefan Metzmacher struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
127df3b8ca6SPavel Begunkov unsigned int flags = IO_URING_F_COMPLETE_DEFER;
128df3b8ca6SPavel Begunkov
129bab4b2ccSPavel Begunkov if (io_should_terminate_tw())
130df3b8ca6SPavel Begunkov flags |= IO_URING_F_TASK_DEAD;
131e1eef2e5SPavel Begunkov
1328e5b3b89SPavel Begunkov /* task_work executor checks the deffered list completion */
133df3b8ca6SPavel Begunkov ioucmd->task_work_cb(ioucmd, flags);
13499f15d8dSJens Axboe }
13599f15d8dSJens Axboe
__io_uring_cmd_do_in_task(struct io_uring_cmd * ioucmd,void (* task_work_cb)(struct io_uring_cmd *,unsigned),unsigned flags)1365f3139fcSPavel Begunkov void __io_uring_cmd_do_in_task(struct io_uring_cmd *ioucmd,
1375f3139fcSPavel Begunkov void (*task_work_cb)(struct io_uring_cmd *, unsigned),
1385f3139fcSPavel Begunkov unsigned flags)
13999f15d8dSJens Axboe {
14099f15d8dSJens Axboe struct io_kiocb *req = cmd_to_io_kiocb(ioucmd);
14199f15d8dSJens Axboe
14299f15d8dSJens Axboe ioucmd->task_work_cb = task_work_cb;
14399f15d8dSJens Axboe req->io_task_work.func = io_uring_cmd_work;
1445f3139fcSPavel Begunkov __io_req_task_work_add(req, flags);
14599f15d8dSJens Axboe }
1465f3139fcSPavel Begunkov EXPORT_SYMBOL_GPL(__io_uring_cmd_do_in_task);
1475f3139fcSPavel Begunkov
io_req_set_cqe32_extra(struct io_kiocb * req,u64 extra1,u64 extra2)14899f15d8dSJens Axboe static inline void io_req_set_cqe32_extra(struct io_kiocb *req,
14999f15d8dSJens Axboe u64 extra1, u64 extra2)
15099f15d8dSJens Axboe {
151b24c5d75SPavel Begunkov req->big_cqe.extra1 = extra1;
152b24c5d75SPavel Begunkov req->big_cqe.extra2 = extra2;
15399f15d8dSJens Axboe }
15499f15d8dSJens Axboe
15599f15d8dSJens Axboe /*
15699f15d8dSJens Axboe * Called by consumers of io_uring_cmd, if they originally returned
15799f15d8dSJens Axboe * -EIOCBQUEUED upon receiving the command.
15899f15d8dSJens Axboe */
io_uring_cmd_done(struct io_uring_cmd * ioucmd,ssize_t ret,u64 res2,unsigned issue_flags)159a07d2d79SBernd Schubert void io_uring_cmd_done(struct io_uring_cmd *ioucmd, ssize_t ret, u64 res2,
1609d2789acSJens Axboe unsigned issue_flags)
16199f15d8dSJens Axboe {
16299f15d8dSJens Axboe struct io_kiocb *req = cmd_to_io_kiocb(ioucmd);
16399f15d8dSJens Axboe
16493b8cc60SMing Lei io_uring_cmd_del_cancelable(ioucmd, issue_flags);
16593b8cc60SMing Lei
16699f15d8dSJens Axboe if (ret < 0)
16799f15d8dSJens Axboe req_set_fail(req);
16899f15d8dSJens Axboe
169ff2557b7SMing Lei io_req_set_res(req, ret, 0);
17099f15d8dSJens Axboe if (req->ctx->flags & IORING_SETUP_CQE32)
17199f15d8dSJens Axboe io_req_set_cqe32_extra(req, res2, 0);
172d10f19dfSJens Axboe io_req_uring_cleanup(req, issue_flags);
17327a67079SJens Axboe if (req->ctx->flags & IORING_SETUP_IOPOLL) {
1745756a3a7SKanchan Joshi /* order with io_iopoll_req_issued() checking ->iopoll_complete */
1755756a3a7SKanchan Joshi smp_store_release(&req->iopoll_completed, 1);
176e1eef2e5SPavel Begunkov } else if (issue_flags & IO_URING_F_COMPLETE_DEFER) {
177e1eef2e5SPavel Begunkov if (WARN_ON_ONCE(issue_flags & IO_URING_F_UNLOCKED))
178e1eef2e5SPavel Begunkov return;
1796edd953bSPavel Begunkov io_req_complete_defer(req);
18027a67079SJens Axboe } else {
1816edd953bSPavel Begunkov req->io_task_work.func = io_req_task_complete;
1826edd953bSPavel Begunkov io_req_task_work_add(req);
18327a67079SJens Axboe }
18499f15d8dSJens Axboe }
18599f15d8dSJens Axboe EXPORT_SYMBOL_GPL(io_uring_cmd_done);
18699f15d8dSJens Axboe
io_uring_cmd_prep_setup(struct io_kiocb * req,const struct io_uring_sqe * sqe)187d10f19dfSJens Axboe static int io_uring_cmd_prep_setup(struct io_kiocb *req,
188d10f19dfSJens Axboe const struct io_uring_sqe *sqe)
18999f15d8dSJens Axboe {
190f2ccb5aeSStefan Metzmacher struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
1915f14404bSPavel Begunkov struct io_async_cmd *ac;
19299f15d8dSJens Axboe
1935f14404bSPavel Begunkov /* see io_uring_cmd_get_async_data() */
1945f14404bSPavel Begunkov BUILD_BUG_ON(offsetof(struct io_async_cmd, data) != 0);
1955f14404bSPavel Begunkov
1965f14404bSPavel Begunkov ac = io_uring_alloc_async_data(&req->ctx->cmd_cache, req);
1975f14404bSPavel Begunkov if (!ac)
1985eff57faSJens Axboe return -ENOMEM;
1995f14404bSPavel Begunkov ac->data.op_data = NULL;
2005eff57faSJens Axboe
201d6211ebbSJens Axboe /*
202d6211ebbSJens Axboe * Unconditionally cache the SQE for now - this is only needed for
203d6211ebbSJens Axboe * requests that go async, but prep handlers must ensure that any
204d6211ebbSJens Axboe * sqe data is stable beyond prep. Since uring_cmd is special in
205d6211ebbSJens Axboe * that it doesn't read in per-op data, play it safe and ensure that
206d6211ebbSJens Axboe * any SQE data is stable beyond prep. This can later get relaxed.
207d6211ebbSJens Axboe */
208296e1696SPavel Begunkov memcpy(ac->sqes, sqe, uring_sqe_size(req->ctx));
209296e1696SPavel Begunkov ioucmd->sqe = ac->sqes;
2105eff57faSJens Axboe return 0;
211d10f19dfSJens Axboe }
21299f15d8dSJens Axboe
io_uring_cmd_prep(struct io_kiocb * req,const struct io_uring_sqe * sqe)21399f15d8dSJens Axboe int io_uring_cmd_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
21499f15d8dSJens Axboe {
215f2ccb5aeSStefan Metzmacher struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
21699f15d8dSJens Axboe
2179cda70f6SAnuj Gupta if (sqe->__pad1)
21899f15d8dSJens Axboe return -EINVAL;
2199cda70f6SAnuj Gupta
2209cda70f6SAnuj Gupta ioucmd->flags = READ_ONCE(sqe->uring_cmd_flags);
221528ce678SMing Lei if (ioucmd->flags & ~IORING_URING_CMD_MASK)
2229cda70f6SAnuj Gupta return -EINVAL;
2239cda70f6SAnuj Gupta
2245d309914SPavel Begunkov if (ioucmd->flags & IORING_URING_CMD_FIXED)
2255d309914SPavel Begunkov req->buf_index = READ_ONCE(sqe->buf_index);
2269cda70f6SAnuj Gupta
22799f15d8dSJens Axboe ioucmd->cmd_op = READ_ONCE(sqe->cmd_op);
228d10f19dfSJens Axboe
229d10f19dfSJens Axboe return io_uring_cmd_prep_setup(req, sqe);
23099f15d8dSJens Axboe }
23199f15d8dSJens Axboe
io_uring_cmd(struct io_kiocb * req,unsigned int issue_flags)23299f15d8dSJens Axboe int io_uring_cmd(struct io_kiocb *req, unsigned int issue_flags)
23399f15d8dSJens Axboe {
234f2ccb5aeSStefan Metzmacher struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
23599f15d8dSJens Axboe struct io_ring_ctx *ctx = req->ctx;
23699f15d8dSJens Axboe struct file *file = req->file;
23799f15d8dSJens Axboe int ret;
23899f15d8dSJens Axboe
23903b3d6beSJens Axboe if (!file->f_op->uring_cmd)
24099f15d8dSJens Axboe return -EOPNOTSUPP;
24199f15d8dSJens Axboe
2422a584012SLuis Chamberlain ret = security_uring_cmd(ioucmd);
2432a584012SLuis Chamberlain if (ret)
2442a584012SLuis Chamberlain return ret;
2452a584012SLuis Chamberlain
24699f15d8dSJens Axboe if (ctx->flags & IORING_SETUP_SQE128)
24799f15d8dSJens Axboe issue_flags |= IO_URING_F_SQE128;
24899f15d8dSJens Axboe if (ctx->flags & IORING_SETUP_CQE32)
24999f15d8dSJens Axboe issue_flags |= IO_URING_F_CQE32;
2500bba6fccSPavel Begunkov if (io_is_compat(ctx))
2515fea44a6SBreno Leitao issue_flags |= IO_URING_F_COMPAT;
2525756a3a7SKanchan Joshi if (ctx->flags & IORING_SETUP_IOPOLL) {
25303b3d6beSJens Axboe if (!file->f_op->uring_cmd_iopoll)
25403b3d6beSJens Axboe return -EOPNOTSUPP;
25599f15d8dSJens Axboe issue_flags |= IO_URING_F_IOPOLL;
2565756a3a7SKanchan Joshi req->iopoll_completed = 0;
257*63166b81Shexue if (ctx->flags & IORING_SETUP_HYBRID_IOPOLL) {
258*63166b81Shexue /* make sure every req only blocks once */
259*63166b81Shexue req->flags &= ~REQ_F_IOPOLL_STATE;
260*63166b81Shexue req->iopoll_start = ktime_get_ns();
261*63166b81Shexue }
2625756a3a7SKanchan Joshi }
26399f15d8dSJens Axboe
26499f15d8dSJens Axboe ret = file->f_op->uring_cmd(ioucmd, issue_flags);
265d6211ebbSJens Axboe if (ret == -EAGAIN || ret == -EIOCBQUEUED)
266d6211ebbSJens Axboe return ret;
2673ed159c9SAnuj Gupta if (ret < 0)
2683ed159c9SAnuj Gupta req_set_fail(req);
269d10f19dfSJens Axboe io_req_uring_cleanup(req, issue_flags);
2703ed159c9SAnuj Gupta io_req_set_res(req, ret, 0);
271f1dcdfcaSPavel Begunkov return IOU_OK;
27299f15d8dSJens Axboe }
27399f15d8dSJens Axboe
io_uring_cmd_import_fixed(u64 ubuf,unsigned long len,int rw,struct iov_iter * iter,struct io_uring_cmd * ioucmd,unsigned int issue_flags)274a9216facSAnuj Gupta int io_uring_cmd_import_fixed(u64 ubuf, unsigned long len, int rw,
2750c542a69SCaleb Sander Mateos struct iov_iter *iter,
2760c542a69SCaleb Sander Mateos struct io_uring_cmd *ioucmd,
27769d483d5SPavel Begunkov unsigned int issue_flags)
278a9216facSAnuj Gupta {
279a9216facSAnuj Gupta struct io_kiocb *req = cmd_to_io_kiocb(ioucmd);
280a9216facSAnuj Gupta
2815d309914SPavel Begunkov return io_import_reg_buf(req, iter, ubuf, len, rw, issue_flags);
282a9216facSAnuj Gupta }
283a9216facSAnuj Gupta EXPORT_SYMBOL_GPL(io_uring_cmd_import_fixed);
2848e9fad0eSBreno Leitao
io_uring_cmd_import_fixed_vec(struct io_uring_cmd * ioucmd,const struct iovec __user * uvec,size_t uvec_segs,int ddir,struct iov_iter * iter,unsigned issue_flags)285ef490275SPavel Begunkov int io_uring_cmd_import_fixed_vec(struct io_uring_cmd *ioucmd,
286ef490275SPavel Begunkov const struct iovec __user *uvec,
287ef490275SPavel Begunkov size_t uvec_segs,
288ef490275SPavel Begunkov int ddir, struct iov_iter *iter,
289ef490275SPavel Begunkov unsigned issue_flags)
290ef490275SPavel Begunkov {
291ef490275SPavel Begunkov struct io_kiocb *req = cmd_to_io_kiocb(ioucmd);
292ef490275SPavel Begunkov struct io_async_cmd *ac = req->async_data;
293ef490275SPavel Begunkov int ret;
294ef490275SPavel Begunkov
295ef490275SPavel Begunkov ret = io_prep_reg_iovec(req, &ac->vec, uvec, uvec_segs);
296ef490275SPavel Begunkov if (ret)
297ef490275SPavel Begunkov return ret;
298ef490275SPavel Begunkov
299ef490275SPavel Begunkov return io_import_reg_vec(ddir, iter, req, &ac->vec, uvec_segs,
300ef490275SPavel Begunkov issue_flags);
301ef490275SPavel Begunkov }
302ef490275SPavel Begunkov EXPORT_SYMBOL_GPL(io_uring_cmd_import_fixed_vec);
303ef490275SPavel Begunkov
io_uring_cmd_issue_blocking(struct io_uring_cmd * ioucmd)3046746ee4cSPavel Begunkov void io_uring_cmd_issue_blocking(struct io_uring_cmd *ioucmd)
3056746ee4cSPavel Begunkov {
3066746ee4cSPavel Begunkov struct io_kiocb *req = cmd_to_io_kiocb(ioucmd);
3076746ee4cSPavel Begunkov
3086746ee4cSPavel Begunkov io_req_queue_iowq(req);
3096746ee4cSPavel Begunkov }
3106746ee4cSPavel Begunkov
io_uring_cmd_getsockopt(struct socket * sock,struct io_uring_cmd * cmd,unsigned int issue_flags)311a5d2f99aSBreno Leitao static inline int io_uring_cmd_getsockopt(struct socket *sock,
312a5d2f99aSBreno Leitao struct io_uring_cmd *cmd,
313a5d2f99aSBreno Leitao unsigned int issue_flags)
314a5d2f99aSBreno Leitao {
315ed344511SPavel Begunkov const struct io_uring_sqe *sqe = cmd->sqe;
316a5d2f99aSBreno Leitao bool compat = !!(issue_flags & IO_URING_F_COMPAT);
317a5d2f99aSBreno Leitao int optlen, optname, level, err;
318a5d2f99aSBreno Leitao void __user *optval;
319a5d2f99aSBreno Leitao
320ed344511SPavel Begunkov level = READ_ONCE(sqe->level);
321a5d2f99aSBreno Leitao if (level != SOL_SOCKET)
322a5d2f99aSBreno Leitao return -EOPNOTSUPP;
323a5d2f99aSBreno Leitao
324ed344511SPavel Begunkov optval = u64_to_user_ptr(READ_ONCE(sqe->optval));
325ed344511SPavel Begunkov optname = READ_ONCE(sqe->optname);
326ed344511SPavel Begunkov optlen = READ_ONCE(sqe->optlen);
327a5d2f99aSBreno Leitao
328a5d2f99aSBreno Leitao err = do_sock_getsockopt(sock, compat, level, optname,
329a5d2f99aSBreno Leitao USER_SOCKPTR(optval),
330a5d2f99aSBreno Leitao KERNEL_SOCKPTR(&optlen));
331a5d2f99aSBreno Leitao if (err)
332a5d2f99aSBreno Leitao return err;
333a5d2f99aSBreno Leitao
334a5d2f99aSBreno Leitao /* On success, return optlen */
335a5d2f99aSBreno Leitao return optlen;
336a5d2f99aSBreno Leitao }
337a5d2f99aSBreno Leitao
io_uring_cmd_setsockopt(struct socket * sock,struct io_uring_cmd * cmd,unsigned int issue_flags)3384232c6e3SBreno Leitao static inline int io_uring_cmd_setsockopt(struct socket *sock,
3394232c6e3SBreno Leitao struct io_uring_cmd *cmd,
3404232c6e3SBreno Leitao unsigned int issue_flags)
3414232c6e3SBreno Leitao {
342ed344511SPavel Begunkov const struct io_uring_sqe *sqe = cmd->sqe;
3434232c6e3SBreno Leitao bool compat = !!(issue_flags & IO_URING_F_COMPAT);
3444232c6e3SBreno Leitao int optname, optlen, level;
3454232c6e3SBreno Leitao void __user *optval;
3464232c6e3SBreno Leitao sockptr_t optval_s;
3474232c6e3SBreno Leitao
348ed344511SPavel Begunkov optval = u64_to_user_ptr(READ_ONCE(sqe->optval));
349ed344511SPavel Begunkov optname = READ_ONCE(sqe->optname);
350ed344511SPavel Begunkov optlen = READ_ONCE(sqe->optlen);
351ed344511SPavel Begunkov level = READ_ONCE(sqe->level);
3524232c6e3SBreno Leitao optval_s = USER_SOCKPTR(optval);
3534232c6e3SBreno Leitao
3544232c6e3SBreno Leitao return do_sock_setsockopt(sock, compat, level, optname, optval_s,
3554232c6e3SBreno Leitao optlen);
3564232c6e3SBreno Leitao }
3574232c6e3SBreno Leitao
358d2cac3ecSBreno Leitao #if defined(CONFIG_NET)
io_uring_cmd_sock(struct io_uring_cmd * cmd,unsigned int issue_flags)3598e9fad0eSBreno Leitao int io_uring_cmd_sock(struct io_uring_cmd *cmd, unsigned int issue_flags)
3608e9fad0eSBreno Leitao {
3618e9fad0eSBreno Leitao struct socket *sock = cmd->file->private_data;
3628e9fad0eSBreno Leitao struct sock *sk = sock->sk;
3638e9fad0eSBreno Leitao struct proto *prot = READ_ONCE(sk->sk_prot);
3648e9fad0eSBreno Leitao int ret, arg = 0;
3658e9fad0eSBreno Leitao
3668e9fad0eSBreno Leitao if (!prot || !prot->ioctl)
3678e9fad0eSBreno Leitao return -EOPNOTSUPP;
3688e9fad0eSBreno Leitao
369d58d82bdSJens Axboe switch (cmd->cmd_op) {
3708e9fad0eSBreno Leitao case SOCKET_URING_OP_SIOCINQ:
3718e9fad0eSBreno Leitao ret = prot->ioctl(sk, SIOCINQ, &arg);
3728e9fad0eSBreno Leitao if (ret)
3738e9fad0eSBreno Leitao return ret;
3748e9fad0eSBreno Leitao return arg;
3758e9fad0eSBreno Leitao case SOCKET_URING_OP_SIOCOUTQ:
3768e9fad0eSBreno Leitao ret = prot->ioctl(sk, SIOCOUTQ, &arg);
3778e9fad0eSBreno Leitao if (ret)
3788e9fad0eSBreno Leitao return ret;
3798e9fad0eSBreno Leitao return arg;
380a5d2f99aSBreno Leitao case SOCKET_URING_OP_GETSOCKOPT:
381a5d2f99aSBreno Leitao return io_uring_cmd_getsockopt(sock, cmd, issue_flags);
3824232c6e3SBreno Leitao case SOCKET_URING_OP_SETSOCKOPT:
3834232c6e3SBreno Leitao return io_uring_cmd_setsockopt(sock, cmd, issue_flags);
3848e9fad0eSBreno Leitao default:
3858e9fad0eSBreno Leitao return -EOPNOTSUPP;
3868e9fad0eSBreno Leitao }
3878e9fad0eSBreno Leitao }
3888e9fad0eSBreno Leitao EXPORT_SYMBOL_GPL(io_uring_cmd_sock);
389d2cac3ecSBreno Leitao #endif
390