From patchwork Sun Aug 27 13:28:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hao Xu X-Patchwork-Id: 13367144 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 DC295C83F01 for ; Sun, 27 Aug 2023 13:32:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6C8548E000D; Sun, 27 Aug 2023 09:32:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 678428E0001; Sun, 27 Aug 2023 09:32:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 567E58E000D; Sun, 27 Aug 2023 09:32:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 4785A8E0001 for ; Sun, 27 Aug 2023 09:32:48 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 20A59160201 for ; Sun, 27 Aug 2023 13:32:48 +0000 (UTC) X-FDA: 81169974816.03.11385F6 Received: from out-250.mta1.migadu.com (out-250.mta1.migadu.com [95.215.58.250]) by imf23.hostedemail.com (Postfix) with ESMTP id 552F614000D for ; Sun, 27 Aug 2023 13:32:46 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Yw+5XvRr; spf=pass (imf23.hostedemail.com: domain of hao.xu@linux.dev designates 95.215.58.250 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=1693143166; 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=W04QOWrrBuMFm18D/+g+MI2uZNnKBIsbwxq2wO1dKKw=; b=v7QOeVbqNvgW2526M0/WNiX27lAkI7To5yr3x2qL2w7kemrN3PDFoLEwok2O+U5wdmDx2N +yMBYtsya9M7vBTGaC11XqhaDJMzKHrfZ3Ao24bmO5/ymN4h8sudR+C6mX+rjBIwUhdjOY tH1tndYwPddwyFT9rWWJ4Y31HAIUU4k= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Yw+5XvRr; spf=pass (imf23.hostedemail.com: domain of hao.xu@linux.dev designates 95.215.58.250 as permitted sender) smtp.mailfrom=hao.xu@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1693143166; a=rsa-sha256; cv=none; b=Az+s1n1LdgUPbKIYE1ehCDEc6n5TNHJ8tokBBnBSBQynxrU8WS26UiJHNJ/VgzsLZnQFXQ ks5FuAb9kQZ3a2jVfYu41r5gUyyeLUwrEaciteFb04iHpg3Snaby+YxVUXRc5yebclYwd1 5Wkt6GSCXfnGN65ACtfcPobmMFvnM4k= 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=1693143164; 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=W04QOWrrBuMFm18D/+g+MI2uZNnKBIsbwxq2wO1dKKw=; b=Yw+5XvRr3vDIU02wPbTBy+P7Vb3r5vANDo9ahLXEToHmV4GE1+GjTI2/FiGvzS/72CcLGE wNMghjMXDTPCiQyUzrxQFpeftIBQyMH0jGDsayXuE4xMGusaZd0pBKhpckAeDFF1jY1yin IKCCEyk0xEU76DQXzGM7PUWjnMG6kt0= 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 04/11] vfs: add a vfs helper for io_uring file pos lock Date: Sun, 27 Aug 2023 21:28:28 +0800 Message-Id: <20230827132835.1373581-5-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-Rspamd-Queue-Id: 552F614000D X-Rspam-User: X-Stat-Signature: gcsk3yt4k83mjdqege3w976z8cx76hfm X-Rspamd-Server: rspam01 X-HE-Tag: 1693143166-836759 X-HE-Meta: U2FsdGVkX19VocWVYkwKidtBFBkMbQHKaNn0jCLXYVI60td3Tdfzqe+Klw1RoJ+SOSGo5rdrPNTIG6RtM3YdSxzSRT6co1wF+dVPalQr9U3PaV1KL+8IuX4tzCmG+zBBUe0GcaUik43p8d4dz4R+FaOjejXWz6WTSCopjjJfHcARVL/LbWxvgK4pBA7UE9cAjrfw6htAKRQ1oNf6R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oTG1GbQiFJrovi2jO7Utoe67nBs+9hqUJY05nT9skgA= 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 Add a vfs helper file_pos_lock_nowait() for io_uring usage. The function have conditional nowait logic, i.e. if nowait is needed, return -EAGAIN when trylock fails. Signed-off-by: Hao Xu --- fs/file.c | 13 +++++++++++++ include/linux/file.h | 2 ++ 2 files changed, 15 insertions(+) diff --git a/fs/file.c b/fs/file.c index 35c62b54c9d6..8e5c38f5db52 100644 --- a/fs/file.c +++ b/fs/file.c @@ -1053,6 +1053,19 @@ void __f_unlock_pos(struct file *f) mutex_unlock(&f->f_pos_lock); } +int file_pos_lock_nowait(struct file *file, bool nowait) +{ + if (!(file->f_mode & FMODE_ATOMIC_POS)) + return 0; + + if (!nowait) + mutex_lock(&file->f_pos_lock); + else if (!mutex_trylock(&file->f_pos_lock)) + return -EAGAIN; + + return 1; +} + /* * We only lock f_pos if we have threads or if the file might be * shared with another process. In both cases we'll have an elevated diff --git a/include/linux/file.h b/include/linux/file.h index 6e9099d29343..bcc6ba0aec50 100644 --- a/include/linux/file.h +++ b/include/linux/file.h @@ -81,6 +81,8 @@ static inline void fdput_pos(struct fd f) fdput(f); } +extern int file_pos_lock_nowait(struct file *file, bool nowait); + DEFINE_CLASS(fd, struct fd, fdput(_T), fdget(fd), int fd) extern int f_dupfd(unsigned int from, struct file *file, unsigned flags);