From patchwork Fri Nov 15 15:30:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13876441 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 CC5FDD68BC6 for ; Fri, 15 Nov 2024 15:31:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0A9B06B0089; Fri, 15 Nov 2024 10:31:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 05A216B008A; Fri, 15 Nov 2024 10:31:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DEFDC6B008C; Fri, 15 Nov 2024 10:31:29 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id BD6CB6B0089 for ; Fri, 15 Nov 2024 10:31:29 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 69C17C182B for ; Fri, 15 Nov 2024 15:31:29 +0000 (UTC) X-FDA: 82788716766.27.572E29D Received: from mail-yw1-f172.google.com (mail-yw1-f172.google.com [209.85.128.172]) by imf27.hostedemail.com (Postfix) with ESMTP id 114574000E for ; Fri, 15 Nov 2024 15:30:40 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=c1qO7kxP; dmarc=none; spf=none (imf27.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.128.172) smtp.mailfrom=josef@toxicpanda.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731684598; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=6KY5fZdmP1liuAXJOZsCM4eyAqIeREr5VHEr0FLOlUs=; b=Xtk7WPWcrVfWL8p4UEnriXulJHs3P3juV3h7vLUHMs3FUQwLFmOpo1aGwKShBLzV4TP2go HSj/cWpjzwh986t0UwW5f+1rr0auTef6l24q931U2k1TMM9VrC6Nyfq2d37cWWodIH9UdA HBr+Y6e5mdTp7n7SpwPfcnyEgHBMYXc= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=c1qO7kxP; dmarc=none; spf=none (imf27.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.128.172) smtp.mailfrom=josef@toxicpanda.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731684598; a=rsa-sha256; cv=none; b=Kah5tTO+3ooLszHQYTmbyHk+84E1CsgUvfB9N3lFegm6KOsTD1VfL17KWKzKFGcWbXXqW3 tD+vMGURSeIvur/0WTQ7Qh5DrCnFj0B9cQAlVmDeAQK5mLr3auZJLJhmhQ/TGNY5SKgMAG 1ERJozJJVD9UoG13QgwOyPiHCepEkvs= Received: by mail-yw1-f172.google.com with SMTP id 00721157ae682-6ee6a2ae6ecso2849727b3.3 for ; Fri, 15 Nov 2024 07:31:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1731684686; x=1732289486; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=6KY5fZdmP1liuAXJOZsCM4eyAqIeREr5VHEr0FLOlUs=; b=c1qO7kxPQIqmTjG4RZSrdIMwHdDa/XyqHZ307ozkpSnoMhl9co0a72GbOnMNpoyxiU tSfdlE2aK/RVidzpBzmD0v9XPfOrRyqIJqpLPaP3J/xCHBLc26+5ivRt38ak9ZQREPcO cIKBCyd97vIFn1j77QEFfnbLxiHur6yN3XsEHUxDIoDILF3eTmWTIhucnAmNf0HwrGMC sQfDjyYcFRaeV9RNZ8AcQrola74+6UxN8aLwKMCZfJ271eLJdOWPKSC50vSWv50Y2X0h 1lTlevuFXAwfRPfMSdA/R1P4sQ0P8+nUa7RSwB1HCx4RKxEtxOTwcWZsuinnbN5GA99P xHWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731684686; x=1732289486; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=6KY5fZdmP1liuAXJOZsCM4eyAqIeREr5VHEr0FLOlUs=; b=JCwHXxxT5XpB4n9yxFvrlkadubeunzW8rNtasXcc97CvpGgctNppWfX/bIumR583fY JNwTP3iorrUJk19XcwpMJMytoX8fTK3DURTM/nLNSA8bAvuI7UjiwrU0Bf+SSyUNXPDN 9s0D3N6TyZPuGYUvn8t/aateUmHA9SqJUYCt4UPJYM2ds9mSC5quSQcCCh2++lTTFqh0 5hekRDkf2LO478wbkOmz4+dSmtgf3FEtLd98uraZjenalq3ad945Bk7xIta6QUxFDjRD X+9iP/9P8IpdAe+jWMAOjmetj7YeFGxCfe76mpQjwidR0dVT2BLiGZ5tALXH/OvCCcdf DCgA== X-Forwarded-Encrypted: i=1; AJvYcCVhYNn3IgdlZkkQXtb7x06VifDw04eV1F6zrIVFR+11lNom5TtgcM972rs3m5GaZ6svNBubvPByQQ==@kvack.org X-Gm-Message-State: AOJu0Yxc12J5kVRTPvMOsAkjDYPlkSYzpAGZiB8GhX+Bhln9x5euM2fd wFuCmcJF36l6WvkV3kdydp7ZS0fdhpVJGtyX6+flgu42j7TYFCOjvf/LviTG4v4= X-Google-Smtp-Source: AGHT+IHdSr7sIuKVmbedcInH94EUveWdRZ8jZfw4hoXsU2FWCk1ayn1ufjs9+AX7MxAWuqGyDVgHDQ== X-Received: by 2002:a05:690c:360b:b0:6ea:34b1:62e3 with SMTP id 00721157ae682-6ee55a2f60cmr35106257b3.7.1731684686563; Fri, 15 Nov 2024 07:31:26 -0800 (PST) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6ee44070647sm7648387b3.53.2024.11.15.07.31.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Nov 2024 07:31:25 -0800 (PST) From: Josef Bacik To: kernel-team@fb.com, linux-fsdevel@vger.kernel.org, jack@suse.cz, amir73il@gmail.com, brauner@kernel.org, torvalds@linux-foundation.org, viro@zeniv.linux.org.uk, linux-xfs@vger.kernel.org, linux-btrfs@vger.kernel.org, linux-mm@kvack.org, linux-ext4@vger.kernel.org Subject: [PATCH v8 03/19] fsnotify: add helper to check if file is actually being watched Date: Fri, 15 Nov 2024 10:30:16 -0500 Message-ID: <2ddcc9f8d1fde48d085318a6b5a889289d8871d8.1731684329.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 114574000E X-Stat-Signature: tgw9ie9xnq3ix7jqyu4ca4zjs137tfbd X-HE-Tag: 1731684640-999958 X-HE-Meta: U2FsdGVkX18FKMBn/mDHYz3ZagNrvQNmbAcMqOL48C9DPhOdTxiGw2pzkzUZvA/DglCR0Knuj6r8NRBFIQcK1BZIRHklYsZs5yUQRxM4/9TuxnRXHIBR/4Ix/iCmDnMcycnGr41MkJsMc1y7fped7jOe64sqQLf4faOQQAmIY6lhuopHkqfCA8y/tfHWngfZv4Y1ZD8TaSM0bhXjzxDw6y1FI6Qvq77xSsgtbxBYDY20XXGOBHfsBQfRIolrKoNAMQr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Q== 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: List-Subscribe: List-Unsubscribe: From: Amir Goldstein So far, we set FMODE_NONOTIFY_ flags at open time if we know that there are no permission event watchers at all on the filesystem, but lack of FMODE_NONOTIFY_ flags does not mean that the file is actually watched. To make the flags more accurate we add a helper that checks if the file's inode, mount, sb or parent are being watched for a set of events. This is going to be used for setting FMODE_NONOTIFY_HSM only when the specific file is actually watched for pre-content events. Signed-off-by: Amir Goldstein --- fs/notify/fsnotify.c | 36 +++++++++++++++++++++++++------- include/linux/fsnotify_backend.h | 7 +++++++ 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c index f976949d2634..33576a848a9f 100644 --- a/fs/notify/fsnotify.c +++ b/fs/notify/fsnotify.c @@ -193,16 +193,38 @@ static bool fsnotify_event_needs_parent(struct inode *inode, __u32 mnt_mask, return mask & marks_mask; } -/* Are there any inode/mount/sb objects that are interested in this event? */ -static inline bool fsnotify_object_watched(struct inode *inode, __u32 mnt_mask, - __u32 mask) +/* Are there any inode/mount/sb objects that watch for these events? */ +static inline __u32 fsnotify_object_watched(struct inode *inode, __u32 mnt_mask, + __u32 events_mask) { __u32 marks_mask = READ_ONCE(inode->i_fsnotify_mask) | mnt_mask | READ_ONCE(inode->i_sb->s_fsnotify_mask); - return mask & marks_mask & ALL_FSNOTIFY_EVENTS; + return events_mask & marks_mask; } +/* Are there any inode/mount/sb/parent objects that watch for these events? */ +__u32 fsnotify_file_object_watched(struct file *file, __u32 events_mask) +{ + struct dentry *dentry = file->f_path.dentry; + struct dentry *parent; + __u32 marks_mask, mnt_mask = + READ_ONCE(real_mount(file->f_path.mnt)->mnt_fsnotify_mask); + + marks_mask = fsnotify_object_watched(d_inode(dentry), mnt_mask, + events_mask); + + if (likely(!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED))) + return marks_mask; + + parent = dget_parent(dentry); + marks_mask |= fsnotify_inode_watches_children(d_inode(parent)); + dput(parent); + + return marks_mask & events_mask; +} +EXPORT_SYMBOL_GPL(fsnotify_file_object_watched); + /* * Notify this dentry's parent about a child's events with child name info * if parent is watching or if inode/sb/mount are interested in events with @@ -221,7 +243,7 @@ int __fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data, struct dentry *parent; bool parent_watched = dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED; bool parent_needed, parent_interested; - __u32 p_mask; + __u32 p_mask, test_mask = mask & ALL_FSNOTIFY_EVENTS; struct inode *p_inode = NULL; struct name_snapshot name; struct qstr *file_name = NULL; @@ -229,7 +251,7 @@ int __fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data, /* Optimize the likely case of nobody watching this path */ if (likely(!parent_watched && - !fsnotify_object_watched(inode, mnt_mask, mask))) + !fsnotify_object_watched(inode, mnt_mask, test_mask))) return 0; parent = NULL; @@ -248,7 +270,7 @@ int __fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data, * Include parent/name in notification either if some notification * groups require parent info or the parent is interested in this event. */ - parent_interested = mask & p_mask & ALL_FSNOTIFY_EVENTS; + parent_interested = p_mask & test_mask; if (parent_needed || parent_interested) { /* When notifying parent, child should be passed as data */ WARN_ON_ONCE(inode != fsnotify_data_inode(data, data_type)); diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h index 3ecf7768e577..99d81c3c11d7 100644 --- a/include/linux/fsnotify_backend.h +++ b/include/linux/fsnotify_backend.h @@ -855,8 +855,15 @@ static inline void fsnotify_init_event(struct fsnotify_event *event) INIT_LIST_HEAD(&event->list); } +__u32 fsnotify_file_object_watched(struct file *file, __u32 mask); + #else +static inline __u32 fsnotify_file_object_watched(struct file *file, __u32 mask) +{ + return 0; +} + static inline int fsnotify(__u32 mask, const void *data, int data_type, struct inode *dir, const struct qstr *name, struct inode *inode, u32 cookie)