From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id ACF06C433E0 for ; Thu, 18 Feb 2021 14:32:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 58E5964D79 for ; Thu, 18 Feb 2021 14:32:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231898AbhBROcC (ORCPT ); Thu, 18 Feb 2021 09:32:02 -0500 Received: from hmm.wantstofly.org ([213.239.204.108]:57150 "EHLO mail.wantstofly.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232800AbhBRM2x (ORCPT ); Thu, 18 Feb 2021 07:28:53 -0500 Received: by mail.wantstofly.org (Postfix, from userid 1000) id 4DB447F4BD; Thu, 18 Feb 2021 14:27:21 +0200 (EET) Date: Thu, 18 Feb 2021 14:27:21 +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 1/2] readdir: split the core of getdents64(2) out into vfs_getdents() Message-ID: <20210218122721.GB334506@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 So that IORING_OP_GETDENTS may use it, split out the core of the getdents64(2) syscall into a helper function, vfs_getdents(). vfs_getdents() calls into filesystems' ->iterate{,_shared}() which expect serialization on struct file, which means that callers of vfs_getdents() are responsible for either using fdget_pos() or performing the equivalent serialization by hand. Cc: Al Viro Signed-off-by: Lennert Buytenhek --- fs/readdir.c | 25 +++++++++++++++++-------- include/linux/fs.h | 4 ++++ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/fs/readdir.c b/fs/readdir.c index 19434b3c982c..f52167c1eb61 100644 --- a/fs/readdir.c +++ b/fs/readdir.c @@ -348,10 +348,9 @@ static int filldir64(struct dir_context *ctx, const char *name, int namlen, return -EFAULT; } -SYSCALL_DEFINE3(getdents64, unsigned int, fd, - struct linux_dirent64 __user *, dirent, unsigned int, count) +int vfs_getdents(struct file *file, struct linux_dirent64 __user *dirent, + unsigned int count) { - struct fd f; struct getdents_callback64 buf = { .ctx.actor = filldir64, .count = count, @@ -359,11 +358,7 @@ SYSCALL_DEFINE3(getdents64, unsigned int, fd, }; int error; - f = fdget_pos(fd); - if (!f.file) - return -EBADF; - - error = iterate_dir(f.file, &buf.ctx); + error = iterate_dir(file, &buf.ctx); if (error >= 0) error = buf.error; if (buf.prev_reclen) { @@ -376,6 +371,20 @@ SYSCALL_DEFINE3(getdents64, unsigned int, fd, else error = count - buf.count; } + return error; +} + +SYSCALL_DEFINE3(getdents64, unsigned int, fd, + struct linux_dirent64 __user *, dirent, unsigned int, count) +{ + struct fd f; + int error; + + f = fdget_pos(fd); + if (!f.file) + return -EBADF; + + error = vfs_getdents(f.file, dirent, count); fdput_pos(f); return error; } diff --git a/include/linux/fs.h b/include/linux/fs.h index fd47deea7c17..114885d3f6c4 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -3109,6 +3109,10 @@ extern const struct inode_operations simple_symlink_inode_operations; extern int iterate_dir(struct file *, struct dir_context *); +struct linux_dirent64; +int vfs_getdents(struct file *file, struct linux_dirent64 __user *dirent, + unsigned int count); + int vfs_fstatat(int dfd, const char __user *filename, struct kstat *stat, int flags); int vfs_fstat(int fd, struct kstat *stat); -- 2.29.2