From patchwork Sun Aug 27 13:28:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hao Xu X-Patchwork-Id: 13367216 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 E064FC83F11 for ; Sun, 27 Aug 2023 13:35:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 829D9280009; Sun, 27 Aug 2023 09:35:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7B25B8E0001; Sun, 27 Aug 2023 09:35:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 67A80280009; Sun, 27 Aug 2023 09:35:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 552948E0001 for ; Sun, 27 Aug 2023 09:35:37 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 2D828A0223 for ; Sun, 27 Aug 2023 13:35:37 +0000 (UTC) X-FDA: 81169981914.14.4B9EB59 Received: from out-243.mta1.migadu.com (out-243.mta1.migadu.com [95.215.58.243]) by imf28.hostedemail.com (Postfix) with ESMTP id 7C6D8C0017 for ; Sun, 27 Aug 2023 13:35:35 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=BRJIOv0s; spf=pass (imf28.hostedemail.com: domain of hao.xu@linux.dev designates 95.215.58.243 as permitted sender) smtp.mailfrom=hao.xu@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1693143335; 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=OEAsLt3DMuKh/okYLaB20XudDotxF/HaZ3UgOC1IrG+xXppy3kVxz1t60w9q/0J4c0QA6s J66qlhXAerl5saiHdOD+xh9jtuxeHpNUDomPSoo2yzahlRnWGd0XIHpwhZhJFomVgx/mAP NKzcQpxVEFrVW+Vk4zZ+VzuRzPN19u0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1693143335; a=rsa-sha256; cv=none; b=UrwP0ecFsMl5RobbYiGO0bXmEEXe67/bufdkumY9PMG9YzwKBEx8Cer6kjV9PwbpgUO3P8 veSrSMXTHedlQgFgMsR+8jJVIS8oSlrO/Sq3kShBvzS8F3ezRDuYirHAulHWOgKlbW23tF xn/fNxUSHmMIWSRbpdkAOI1H0p7RvGY= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=BRJIOv0s; spf=pass (imf28.hostedemail.com: domain of hao.xu@linux.dev designates 95.215.58.243 as permitted sender) smtp.mailfrom=hao.xu@linux.dev; dmarc=pass (policy=none) header.from=linux.dev 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=1693143333; 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=BRJIOv0sJCNWH9arCNNuOEeHNgvBAsJDXsD/9ROslY/E3EvWdOi7NTa+04JucH099rNnul 4aa7v7DjXCaaQfzVNr38jK1tfhfTVUIcRN9rZkXC1mcdwty8x7yMXlzmlXd+zl6Ur2HPps a6OaK6jfFgLdJwLH2JKSlhPncqJxHqY= 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 10/11] vfs: trylock inode->i_rwsem in iterate_dir() to support nowait Date: Sun, 27 Aug 2023 21:28:34 +0800 Message-Id: <20230827132835.1373581-11-hao.xu@linux.dev> In-Reply-To: <20230827132835.1373581-1-hao.xu@linux.dev> References: <20230827132835.1373581-1-hao.xu@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Stat-Signature: 3nt796pmm6use594jcs5ddan9dcpudrn X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 7C6D8C0017 X-Rspam-User: X-HE-Tag: 1693143335-616595 X-HE-Meta: U2FsdGVkX19kCQkO0UoFMiZtC4H3mtM3UNrnn7R6/tlp2HPIN0+TWJK7KwHZhcjxSvJs2gb8FdDUJHXxj23GVsWBvqOSc83Z+h1clRWwI/Xmxvt4m/xZLtYkUAkwZJ6l7RDUHSTO+xxstg1np2fy5OKgHlkRa0tm9ZXnTM2elLVGzLVKadZ7GCeTd8h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Jt2CZk72Bn/zWAEHHmqoBjT0LCjkj/AfJuPqhCQfb1KH4B6ZB+60CIvgOwhu7H1vBsXpAIsXQ4okbqjhIGCyIzhTQbPc+ok5BV9pivoa94wHwg8sWUoV5g== 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;