From patchwork Fri Aug 25 13:54:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hao Xu X-Patchwork-Id: 13365809 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id CBBD9C71133 for ; Fri, 25 Aug 2023 14:00:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 669F62800C9; Fri, 25 Aug 2023 10:00:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 61A918E0019; Fri, 25 Aug 2023 10:00:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 509832800C9; Fri, 25 Aug 2023 10:00:20 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 34E6D8E0019 for ; Fri, 25 Aug 2023 10:00:20 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 0FD6FC06B7 for ; Fri, 25 Aug 2023 14:00:20 +0000 (UTC) X-FDA: 81162786600.04.628FF87 Received: from out-253.mta1.migadu.com (out-253.mta1.migadu.com [95.215.58.253]) by imf15.hostedemail.com (Postfix) with ESMTP id 23646A000E for ; Fri, 25 Aug 2023 14:00:16 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=UXdbOLDk; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf15.hostedemail.com: domain of hao.xu@linux.dev designates 95.215.58.253 as permitted sender) smtp.mailfrom=hao.xu@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1692972017; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=CL8wzJoAbQ75iZcN0EHSqoGXIWt+IFMvEgcKIhs6zqE=; b=O0j7tln3la2bjKNS/F6MaZfJXkhnU8FxUabUJFkVlCwwpeXqgxEovRs1aemr/UL1VeXSMN O2Zx8PSXS6HF679cpFvia+W/Jweuk4M7BlDniMiblf5MLfL062v6TDhjyPMhLtkp3dJOed S/T1yWNOj++mHBenKffknQ932aGBXWE= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=UXdbOLDk; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf15.hostedemail.com: domain of hao.xu@linux.dev designates 95.215.58.253 as permitted sender) smtp.mailfrom=hao.xu@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1692972017; a=rsa-sha256; cv=none; b=N7LU8S8TTiFunr3D5W8WSI6LFA79zUGfbrGF20ea2c/0iFLsFXDxVHpXt1lN7tY+cj6ra2 oYoFFaEkneicJNfyo4Sj+hLTg8/HR9qBorSeVfBF9fuuSdZ4f8pyKPjgwDuUoa+lfE+FIo tWxrcxmaRjnK43cVQrWpPOyEbNNrN0w= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1692972015; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CL8wzJoAbQ75iZcN0EHSqoGXIWt+IFMvEgcKIhs6zqE=; b=UXdbOLDkxTPiI/RUD6vA6Y59upGR8p9yuOS0cheVOmW9wy55dk0PwGqrHNQocUaxtZPqwl 8SMpVuiY/GUEZaQEBzm6P0e4nsRhzT+GENJ6bzTIZGRyqbBvQsF+Q3u8++NZNdyfo398WE Pbto4Fcj8Lgxb50rhsqaYEakh0iY2/E= From: Hao Xu To: io-uring@vger.kernel.org, Jens Axboe Cc: Dominique Martinet , Pavel Begunkov , Christian Brauner , Alexander Viro , Stefan Roesch , Clay Harris , Dave Chinner , "Darrick J . Wong" , linux-fsdevel@vger.kernel.org, linux-xfs@vger.kernel.org, linux-ext4@vger.kernel.org, linux-cachefs@redhat.com, ecryptfs@vger.kernel.org, linux-nfs@vger.kernel.org, linux-unionfs@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, linux-s390@vger.kernel.org, linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, linux-btrfs@vger.kernel.org, codalist@coda.cs.cmu.edu, linux-f2fs-devel@lists.sourceforge.net, cluster-devel@redhat.com, linux-mm@kvack.org, linux-nilfs@vger.kernel.org, devel@lists.orangefs.org, linux-cifs@vger.kernel.org, samba-technical@lists.samba.org, linux-mtd@lists.infradead.org, Wanpeng Li Subject: [PATCH 11/29] vfs: trylock inode->i_rwsem in iterate_dir() to support nowait Date: Fri, 25 Aug 2023 21:54:13 +0800 Message-Id: <20230825135431.1317785-12-hao.xu@linux.dev> In-Reply-To: <20230825135431.1317785-1-hao.xu@linux.dev> References: <20230825135431.1317785-1-hao.xu@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 23646A000E X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: yzhmzubrbj91z1wfmb8yt31o38qagktt X-HE-Tag: 1692972016-625632 X-HE-Meta: U2FsdGVkX18tXw81TnrONvUFc0kcCnQJ82phooMb9v6WL8ZAq+wnZlye00H/LPcuJ/HsqaC8vYWfDW1i50R2fjXxbHL7DZd7T0IlS2bS1pLhImacBHG0bj5ELA12kfwGA9ozmuWwEMkWNq2Z1qWGDhaffW7Mp32kkG/jb+mwmPatMqfoBjg8qRnTzHD1CIpWov4asC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xL8uupOpFeg7rkunhFXf9CoHDj3Q= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Hao Xu Trylock inode->i_rwsem in iterate_dir() to support nowait semantics and error out -EAGAIN when there is contention. Signed-off-by: Hao Xu --- fs/readdir.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/fs/readdir.c b/fs/readdir.c index 6469f076ba6e..664ecd9665a1 100644 --- a/fs/readdir.c +++ b/fs/readdir.c @@ -43,6 +43,8 @@ int iterate_dir(struct file *file, struct dir_context *ctx) struct inode *inode = file_inode(file); bool shared = false; int res = -ENOTDIR; + bool nowait; + if (file->f_op->iterate_shared) shared = true; else if (!file->f_op->iterate) @@ -52,16 +54,22 @@ int iterate_dir(struct file *file, struct dir_context *ctx) if (res) goto out; - if (shared) - res = down_read_killable(&inode->i_rwsem); - else - res = down_write_killable(&inode->i_rwsem); - if (res) + nowait = ctx->flags & DIR_CONTEXT_F_NOWAIT; + if (nowait) { + res = shared ? down_read_trylock(&inode->i_rwsem) : + down_write_trylock(&inode->i_rwsem); + if (!res) + res = -EAGAIN; + } else { + res = shared ? down_read_killable(&inode->i_rwsem) : + down_write_killable(&inode->i_rwsem); + } + if (res < 0) goto out; res = -ENOENT; if (!IS_DEADDIR(inode)) { - res = file_accessed(file, ctx->flags & DIR_CONTEXT_F_NOWAIT); + res = file_accessed(file, nowait); if (res == -EAGAIN) goto out_unlock;