Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp7279971pxb; Thu, 18 Feb 2021 06:16:22 -0800 (PST) X-Google-Smtp-Source: ABdhPJyAlfk1gO2Ezo9lSnj1oR37G4CbY9gkTHqHSgbPKDoy/T+2jKa9bKMpgZTujC7kO/5BnCBR X-Received: by 2002:a17:907:770d:: with SMTP id kw13mr4254938ejc.219.1613657782450; Thu, 18 Feb 2021 06:16:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1613657782; cv=none; d=google.com; s=arc-20160816; b=DNgPDAr3MjmKuUVmz1ObsD0ynXEEFkdCOpyz3gwTA7MubkVev4orV5P0wT60owspGt J3BuiNgep7SHDCDjH7lgj953XhndhlLzWGHx5QKNcNE4pFWTGDI6njxB7NfQxlEGzUoB jtQE2q1WLuNK88sp8ziqiYR07vvZsAjWVUCqDkyqvMA0crP+Eo0elDtokNHvae8pAhVE 1oUuSOpbv4rg+UQE08EYD/0wyiUDsKjMfjceRjPZOpPikSyuRblP+YsNanhE0vfUehyN tE0ZyujCPUHam4O0RL5K49vmCEfqAEQXrffJ79u//cZIAhmAPAjjpYyN6WnMGOlRopGS t9WA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=QFL8CiYhz+kRdWjH45geTf8ErvS3Fgbx/8TsPS5mjjc=; b=pWhD2aIX/jwm6lgTG+ZZD1YdbCYD4t/Rg+AvcQwm2YiCegeZ58pgWkX7EPz/0U+j05 RCoL5VWUtu/PiFTexhJ6sm9UcQeGO6XPjVLuwBighkYUi0RcoULpslkKgrUQ+HvuZgkI Cy9uK90Lv7lkbCjPz6dYd1TuCUnx3cQjk9Ujhe0ZFnZBIE10B7P3TJYTbqOHe584d6f1 HkdBJIenPAdjB06c24ufaQ6jKxRE0VAzJKt3QERQAx6a3P7xATWQE7EHp3s/WlxRuGzW TKk9u1Jyzj2u8/447Q5laypsLpEhnheCGfqJcbnQnYc2N4lJ0hXWd77w7ANs43Hkcryr NNew== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f20si3575743ejk.711.2021.02.18.06.15.56; Thu, 18 Feb 2021 06:16:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230087AbhBROPI (ORCPT + 99 others); Thu, 18 Feb 2021 09:15:08 -0500 Received: from hmm.wantstofly.org ([213.239.204.108]:57170 "EHLO mail.wantstofly.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232837AbhBRM3C (ORCPT ); Thu, 18 Feb 2021 07:29:02 -0500 Received: by mail.wantstofly.org (Postfix, from userid 1000) id 258987F4BE; Thu, 18 Feb 2021 14:27:55 +0200 (EET) Date: Thu, 18 Feb 2021 14:27:55 +0200 From: Lennert Buytenhek To: Jens Axboe , Al Viro , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, io-uring@vger.kernel.org Cc: David Laight , Matthew Wilcox Subject: [PATCH v3 2/2] io_uring: add support for IORING_OP_GETDENTS Message-ID: <20210218122755.GC334506@wantstofly.org> References: <20210218122640.GA334506@wantstofly.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210218122640.GA334506@wantstofly.org> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org IORING_OP_GETDENTS behaves much like getdents64(2) and takes the same arguments, but with a small twist: it takes an additional offset argument, and reading from the specified directory starts at the given offset. For the first IORING_OP_GETDENTS call on a directory, the offset parameter can be set to zero, and for subsequent calls, it can be set to the ->d_off field of the last struct linux_dirent64 returned by the previous IORING_OP_GETDENTS call. Internally, if necessary, IORING_OP_GETDENTS will vfs_llseek() to the right directory position before calling vfs_getdents(). IORING_OP_GETDENTS may or may not update the specified directory's file offset, and the file offset should not be relied upon having any particular value during or after an IORING_OP_GETDENTS call. Signed-off-by: Lennert Buytenhek --- fs/io_uring.c | 73 +++++++++++++++++++++++++++++++++++ include/uapi/linux/io_uring.h | 1 + 2 files changed, 74 insertions(+) diff --git a/fs/io_uring.c b/fs/io_uring.c index 056bd4c90ade..6853bf48369a 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -635,6 +635,13 @@ struct io_mkdir { struct filename *filename; }; +struct io_getdents { + struct file *file; + struct linux_dirent64 __user *dirent; + unsigned int count; + loff_t pos; +}; + struct io_completion { struct file *file; struct list_head list; @@ -772,6 +779,7 @@ struct io_kiocb { struct io_rename rename; struct io_unlink unlink; struct io_mkdir mkdir; + struct io_getdents getdents; /* use only after cleaning per-op data, see io_clean_op() */ struct io_completion compl; }; @@ -1030,6 +1038,11 @@ static const struct io_op_def io_op_defs[] = { .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES | IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, }, + [IORING_OP_GETDENTS] = { + .needs_file = 1, + .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES | + IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, + }, }; static void io_uring_try_cancel_requests(struct io_ring_ctx *ctx, @@ -4677,6 +4690,61 @@ static int io_sync_file_range(struct io_kiocb *req, unsigned int issue_flags) return 0; } +static int io_getdents_prep(struct io_kiocb *req, + const struct io_uring_sqe *sqe) +{ + struct io_getdents *getdents = &req->getdents; + + if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) + return -EINVAL; + if (sqe->ioprio || sqe->rw_flags || sqe->buf_index) + return -EINVAL; + + getdents->pos = READ_ONCE(sqe->off); + getdents->dirent = u64_to_user_ptr(READ_ONCE(sqe->addr)); + getdents->count = READ_ONCE(sqe->len); + return 0; +} + +static int io_getdents(struct io_kiocb *req, unsigned int issue_flags) +{ + struct io_getdents *getdents = &req->getdents; + bool pos_unlock = false; + int ret = 0; + + /* getdents always requires a blocking context */ + if (issue_flags & IO_URING_F_NONBLOCK) + return -EAGAIN; + + /* for vfs_llseek and to serialize ->iterate_shared() on this file */ + if (file_count(req->file) > 1) { + pos_unlock = true; + mutex_lock(&req->file->f_pos_lock); + } + + if (req->file->f_pos != getdents->pos) { + loff_t res = vfs_llseek(req->file, getdents->pos, SEEK_SET); + if (res < 0) + ret = res; + } + + if (ret == 0) { + ret = vfs_getdents(req->file, getdents->dirent, + getdents->count); + } + + if (pos_unlock) + mutex_unlock(&req->file->f_pos_lock); + + if (ret < 0) { + if (ret == -ERESTARTSYS) + ret = -EINTR; + req_set_fail_links(req); + } + io_req_complete(req, ret); + return 0; +} + #if defined(CONFIG_NET) static int io_setup_async_msg(struct io_kiocb *req, struct io_async_msghdr *kmsg) @@ -6184,6 +6252,8 @@ static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) return io_unlinkat_prep(req, sqe); case IORING_OP_MKDIRAT: return io_mkdirat_prep(req, sqe); + case IORING_OP_GETDENTS: + return io_getdents_prep(req, sqe); } printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n", @@ -6428,6 +6498,9 @@ static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags) case IORING_OP_MKDIRAT: ret = io_mkdirat(req, issue_flags); break; + case IORING_OP_GETDENTS: + ret = io_getdents(req, issue_flags); + break; default: ret = -EINVAL; break; diff --git a/include/uapi/linux/io_uring.h b/include/uapi/linux/io_uring.h index 890edd850a9e..fe097b1fa332 100644 --- a/include/uapi/linux/io_uring.h +++ b/include/uapi/linux/io_uring.h @@ -138,6 +138,7 @@ enum { IORING_OP_RENAMEAT, IORING_OP_UNLINKAT, IORING_OP_MKDIRAT, + IORING_OP_GETDENTS, /* this goes last, obviously */ IORING_OP_LAST, -- 2.29.2