Skip to content

Commit

Permalink
io_uring: add support for IORING_OP_MKDIRAT
Browse files Browse the repository at this point in the history
IORING_OP_MKDIRAT behaves like mkdirat(2) and takes the same flags
and arguments.

Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dmitry Kadashev <dkadashev@gmail.com>
Acked-by: Christian Brauner <christian.brauner@ubuntu.com>
Link: https://lore.kernel.org/r/20210708063447.3556403-10-dkadashev@gmail.com
[axboe: add splice_fd_in check]
Signed-off-by: Jens Axboe <axboe@kernel.dk>
  • Loading branch information
Dmitry Kadashev authored and Jens Axboe committed Aug 23, 2021
1 parent 45f30da commit e34a02d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
60 changes: 60 additions & 0 deletions fs/io_uring.c
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,13 @@ struct io_unlink {
struct filename *filename;
};

struct io_mkdir {
struct file *file;
int dfd;
umode_t mode;
struct filename *filename;
};

struct io_completion {
struct file *file;
u32 cflags;
Expand Down Expand Up @@ -821,6 +828,7 @@ struct io_kiocb {
struct io_shutdown shutdown;
struct io_rename rename;
struct io_unlink unlink;
struct io_mkdir mkdir;
/* use only after cleaning per-op data, see io_clean_op() */
struct io_completion compl;
};
Expand Down Expand Up @@ -1032,6 +1040,7 @@ static const struct io_op_def io_op_defs[] = {
},
[IORING_OP_RENAMEAT] = {},
[IORING_OP_UNLINKAT] = {},
[IORING_OP_MKDIRAT] = {},
};

/* requests with any of those set should undergo io_disarm_next() */
Expand Down Expand Up @@ -3603,6 +3612,49 @@ static int io_unlinkat(struct io_kiocb *req, unsigned int issue_flags)
return 0;
}

static int io_mkdirat_prep(struct io_kiocb *req,
const struct io_uring_sqe *sqe)
{
struct io_mkdir *mkd = &req->mkdir;
const char __user *fname;

if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
return -EINVAL;
if (sqe->ioprio || sqe->off || sqe->rw_flags || sqe->buf_index ||
sqe->splice_fd_in)
return -EINVAL;
if (unlikely(req->flags & REQ_F_FIXED_FILE))
return -EBADF;

mkd->dfd = READ_ONCE(sqe->fd);
mkd->mode = READ_ONCE(sqe->len);

fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
mkd->filename = getname(fname);
if (IS_ERR(mkd->filename))
return PTR_ERR(mkd->filename);

req->flags |= REQ_F_NEED_CLEANUP;
return 0;
}

static int io_mkdirat(struct io_kiocb *req, int issue_flags)
{
struct io_mkdir *mkd = &req->mkdir;
int ret;

if (issue_flags & IO_URING_F_NONBLOCK)
return -EAGAIN;

ret = do_mkdirat(mkd->dfd, mkd->filename, mkd->mode);

req->flags &= ~REQ_F_NEED_CLEANUP;
if (ret < 0)
req_set_fail(req);
io_req_complete(req, ret);
return 0;
}

static int io_shutdown_prep(struct io_kiocb *req,
const struct io_uring_sqe *sqe)
{
Expand Down Expand Up @@ -6000,6 +6052,8 @@ static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
return io_renameat_prep(req, sqe);
case IORING_OP_UNLINKAT:
return io_unlinkat_prep(req, sqe);
case IORING_OP_MKDIRAT:
return io_mkdirat_prep(req, sqe);
}

printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
Expand Down Expand Up @@ -6163,6 +6217,9 @@ static void io_clean_op(struct io_kiocb *req)
case IORING_OP_UNLINKAT:
putname(req->unlink.filename);
break;
case IORING_OP_MKDIRAT:
putname(req->mkdir.filename);
break;
}
}
if ((req->flags & REQ_F_POLLED) && req->apoll) {
Expand Down Expand Up @@ -6291,6 +6348,9 @@ static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
case IORING_OP_UNLINKAT:
ret = io_unlinkat(req, issue_flags);
break;
case IORING_OP_MKDIRAT:
ret = io_mkdirat(req, issue_flags);
break;
default:
ret = -EINVAL;
break;
Expand Down
1 change: 1 addition & 0 deletions include/uapi/linux/io_uring.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ enum {
IORING_OP_SHUTDOWN,
IORING_OP_RENAMEAT,
IORING_OP_UNLINKAT,
IORING_OP_MKDIRAT,

/* this goes last, obviously */
IORING_OP_LAST,
Expand Down

0 comments on commit e34a02d

Please sign in to comment.