From patchwork Wed Sep 4 20:27:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13791360 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 807D2CD4F24 for ; Wed, 4 Sep 2024 20:29:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D562D6B00E5; Wed, 4 Sep 2024 16:29:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CDF286B010E; Wed, 4 Sep 2024 16:29:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A94EF6B012E; Wed, 4 Sep 2024 16:29:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 841666B00E5 for ; Wed, 4 Sep 2024 16:29:27 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 2CCBF1C63DE for ; Wed, 4 Sep 2024 20:29:27 +0000 (UTC) X-FDA: 82528195974.25.EA40410 Received: from mail-ua1-f53.google.com (mail-ua1-f53.google.com [209.85.222.53]) by imf08.hostedemail.com (Postfix) with ESMTP id 5DC8C160025 for ; Wed, 4 Sep 2024 20:29:25 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=uogSHjFa; spf=none (imf08.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.222.53) smtp.mailfrom=josef@toxicpanda.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725481717; 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=WSgX58UYc0emFHK3lK4cO5xgsT7V4KpoEDCer0WZNY4=; b=gRazQCxsDcpnqYtAqeFIpdVvJ03I6nWORV+DP57mpUIwk5U4ej6Oqfloox8+TFYdnyWfgn weoz1TECkZ+NSIAKEsJEWCBRMRAnIp5lguK/LX6ex7P+B+LhIe29tgEDUXzha98n4mZ/Pf V0qSwaXtNysgemdOlEABNiCVk1EztlQ= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=uogSHjFa; spf=none (imf08.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.222.53) smtp.mailfrom=josef@toxicpanda.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725481717; a=rsa-sha256; cv=none; b=bBvpNsDSjlxFkZKfAm+a88GlqsIMfk3UuReWzyDlhXkqn8tQ7COnm7fV2NZyTn6qqidK6L OsZXQkiBhpwgNeknAirB5lH5OcpyzRXem4ntq6UE4j9ugSTN9rKvTp4LKOBwnlX+LNUgf6 j8K+G+gN3uy8wZDNuogZk0rbi/lIf5k= Received: by mail-ua1-f53.google.com with SMTP id a1e0cc1a2514c-846c5995a6fso1869241.0 for ; Wed, 04 Sep 2024 13:29:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1725481764; x=1726086564; 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=WSgX58UYc0emFHK3lK4cO5xgsT7V4KpoEDCer0WZNY4=; b=uogSHjFa86U+YIbqQO2pPJBlFYOIRjBykhxqutZbr9BHcVNlaoR4Ccthx2aCOYymXl 7pAquLJmprMJXdZv65qtda0BuviVVfwQoW9BNLetIXBZKLvTM1P27Fm3xaB6wQ+loH1h rFgg0Tez9lRJOc+XiVvAjSzD2GpZtxsyGV9R0SoXz/q7o4KicLwzcS1r5CnUYVYDhlIg 9DBCLsLRkl0OocIJXBIKiAtx0IvYquO8YKOAub9P2/uAJUUwIaWU8ysfxtznPvAK1qUx QLa3mqLaLxdcPENbc3KrUWbKlGagHzV3SZbQ24FW5B7O0baZ2bBbKlKMEC23Eu+zTBZ0 vxrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725481764; x=1726086564; 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=WSgX58UYc0emFHK3lK4cO5xgsT7V4KpoEDCer0WZNY4=; b=XDHQTimgQFN+vMF20iA1MQ0Ir+6mjBoJ+wEwEVgJM3aRVbFqYWPHbwbio3p3aFv4Um AP0+NRn3h/QvdzIJ5c/eTfsOkqFD5r5KZSWaxC5pyG8mG5Pu7OMGhCCyAJ/g8Ty3MViH XVSomEcaQMexCoEloq8LU8cwIQoeIRW+zYdLorA5+hflZeKKyv7M2Z7wDOJIO0XreBKV fPpG6P6T839525ZmCVj/VvMOaI8YYYaFEOCjNlzbpOjmBSmT5oSR/2Kdtao6sj0IaFh+ h0/UvFOg4EnJZQ9Hfrz7iWEB4fsseSsdB+YnWZtN68ElpAD5grvtCiipVOpYE+yF/FOX R76g== X-Forwarded-Encrypted: i=1; AJvYcCWxE6UbFDMqXybPWeAfBvkC0lFQUbAWay/yz63MpEZPiUXSmhRQBmbp/BP1h083tUEa3ryyzryqng==@kvack.org X-Gm-Message-State: AOJu0YxpajnCmdzFbsf8A6LhChN8n4dRvTens0YTLX1JVYh3+Uh9z2bS jkshluFJqwCXyt6Q+2+44Ecw6MsEO29xS6Xd5kaA8MD54pMC5qA21gHuhpXR2/Q= X-Google-Smtp-Source: AGHT+IGc7mGWNoIW6t0qFMzy1JgoaW/y/pAizbNG4FOmezxVo9UEWHiWN81iBDFZM1s4pxXg42sQVA== X-Received: by 2002:a05:6102:442b:b0:498:cf81:3a40 with SMTP id ada2fe7eead31-49a7778c1admr18354171137.17.1725481764310; Wed, 04 Sep 2024 13:29:24 -0700 (PDT) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7a98efeb028sm15207185a.84.2024.09.04.13.29.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2024 13:29:23 -0700 (PDT) From: Josef Bacik To: kernel-team@fb.com, linux-fsdevel@vger.kernel.org, jack@suse.cz, amir73il@gmail.com, brauner@kernel.org, linux-xfs@vger.kernel.org, linux-bcachefs@vger.kernel.org, linux-btrfs@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v5 06/18] fanotify: pass optional file access range in pre-content event Date: Wed, 4 Sep 2024 16:27:56 -0400 Message-ID: <87325af81514d7bd0b2236e14c613b7160651bda.1725481503.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 5DC8C160025 X-Stat-Signature: 9yzw5qtkpnytr6ozgwutjwrc6nn7i3wq X-HE-Tag: 1725481765-54792 X-HE-Meta: U2FsdGVkX19LeXhI2YU02Lv5uLTGwS0juOeBJJ9bIzFDmLW9DHjI2tCgi1/BNomnnVq156i0eiffUErApFdRt+v0FfMet2KjYWjrMXjkT9LD73mMcQGVV/tQd4NE3lGAU81ewdU+gfUdyHICoCvgd2yqA4hZzo08LErrK2g9BoHR8kFj++nLyvOroHDDgWW6KNn+/rfslbNFtL6Vnv/In1FG0Mp6SgjDKwPUSUOUgN4bzos4rPwNF/JADSj4h4pC3hxqdVml6sNB4RERg5T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=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 We would like to add file range information to pre-content events. Pass a struct file_range with optional offset and length to event handler along with pre-content permission event. Signed-off-by: Amir Goldstein --- fs/notify/fanotify/fanotify.c | 10 ++++++++-- fs/notify/fanotify/fanotify.h | 2 ++ include/linux/fsnotify.h | 17 ++++++++++++++++- include/linux/fsnotify_backend.h | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 3 deletions(-) diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c index b163594843f5..4e8dce39fa8f 100644 --- a/fs/notify/fanotify/fanotify.c +++ b/fs/notify/fanotify/fanotify.c @@ -549,9 +549,13 @@ static struct fanotify_event *fanotify_alloc_path_event(const struct path *path, return &pevent->fae; } -static struct fanotify_event *fanotify_alloc_perm_event(const struct path *path, +static struct fanotify_event *fanotify_alloc_perm_event(const void *data, + int data_type, gfp_t gfp) { + const struct path *path = fsnotify_data_path(data, data_type); + const struct file_range *range = + fsnotify_data_file_range(data, data_type); struct fanotify_perm_event *pevent; pevent = kmem_cache_alloc(fanotify_perm_event_cachep, gfp); @@ -565,6 +569,8 @@ static struct fanotify_event *fanotify_alloc_perm_event(const struct path *path, pevent->hdr.len = 0; pevent->state = FAN_EVENT_INIT; pevent->path = *path; + pevent->ppos = range ? range->ppos : NULL; + pevent->count = range ? range->count : 0; path_get(path); return &pevent->fae; @@ -802,7 +808,7 @@ static struct fanotify_event *fanotify_alloc_event( old_memcg = set_active_memcg(group->memcg); if (fanotify_is_perm_event(mask)) { - event = fanotify_alloc_perm_event(path, gfp); + event = fanotify_alloc_perm_event(data, data_type, gfp); } else if (fanotify_is_error_event(mask)) { event = fanotify_alloc_error_event(group, fsid, data, data_type, &hash); diff --git a/fs/notify/fanotify/fanotify.h b/fs/notify/fanotify/fanotify.h index e5ab33cae6a7..93598b7d5952 100644 --- a/fs/notify/fanotify/fanotify.h +++ b/fs/notify/fanotify/fanotify.h @@ -425,6 +425,8 @@ FANOTIFY_PE(struct fanotify_event *event) struct fanotify_perm_event { struct fanotify_event fae; struct path path; + const loff_t *ppos; /* optional file range info */ + size_t count; u32 response; /* userspace answer to the event */ unsigned short state; /* state of the event */ int fd; /* fd we passed to userspace for this event */ diff --git a/include/linux/fsnotify.h b/include/linux/fsnotify.h index fb3837b8de4c..9d001d328619 100644 --- a/include/linux/fsnotify.h +++ b/include/linux/fsnotify.h @@ -132,6 +132,21 @@ static inline int fsnotify_file(struct file *file, __u32 mask) } #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS +static inline int fsnotify_file_range(struct file *file, __u32 mask, + const loff_t *ppos, size_t count) +{ + struct file_range range; + + if (file->f_mode & FMODE_NONOTIFY) + return 0; + + range.path = &file->f_path; + range.ppos = ppos; + range.count = count; + return fsnotify_parent(range.path->dentry, mask, &range, + FSNOTIFY_EVENT_FILE_RANGE); +} + /* * fsnotify_file_area_perm - permission hook before access/modify of file range */ @@ -175,7 +190,7 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask, else return 0; - return fsnotify_file(file, fsnotify_mask); + return fsnotify_file_range(file, fsnotify_mask, ppos, count); } /* diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h index 200a5e3b1cd4..276320846bfd 100644 --- a/include/linux/fsnotify_backend.h +++ b/include/linux/fsnotify_backend.h @@ -298,6 +298,7 @@ static inline void fsnotify_group_assert_locked(struct fsnotify_group *group) /* When calling fsnotify tell it if the data is a path or inode */ enum fsnotify_data_type { FSNOTIFY_EVENT_NONE, + FSNOTIFY_EVENT_FILE_RANGE, FSNOTIFY_EVENT_PATH, FSNOTIFY_EVENT_INODE, FSNOTIFY_EVENT_DENTRY, @@ -310,6 +311,17 @@ struct fs_error_report { struct super_block *sb; }; +struct file_range { + const struct path *path; + const loff_t *ppos; + size_t count; +}; + +static inline const struct path *file_range_path(const struct file_range *range) +{ + return range->path; +} + static inline struct inode *fsnotify_data_inode(const void *data, int data_type) { switch (data_type) { @@ -319,6 +331,8 @@ static inline struct inode *fsnotify_data_inode(const void *data, int data_type) return d_inode(data); case FSNOTIFY_EVENT_PATH: return d_inode(((const struct path *)data)->dentry); + case FSNOTIFY_EVENT_FILE_RANGE: + return d_inode(file_range_path(data)->dentry); case FSNOTIFY_EVENT_ERROR: return ((struct fs_error_report *)data)->inode; default: @@ -334,6 +348,8 @@ static inline struct dentry *fsnotify_data_dentry(const void *data, int data_typ return (struct dentry *)data; case FSNOTIFY_EVENT_PATH: return ((const struct path *)data)->dentry; + case FSNOTIFY_EVENT_FILE_RANGE: + return file_range_path(data)->dentry; default: return NULL; } @@ -345,6 +361,8 @@ static inline const struct path *fsnotify_data_path(const void *data, switch (data_type) { case FSNOTIFY_EVENT_PATH: return data; + case FSNOTIFY_EVENT_FILE_RANGE: + return file_range_path(data); default: return NULL; } @@ -360,6 +378,8 @@ static inline struct super_block *fsnotify_data_sb(const void *data, return ((struct dentry *)data)->d_sb; case FSNOTIFY_EVENT_PATH: return ((const struct path *)data)->dentry->d_sb; + case FSNOTIFY_EVENT_FILE_RANGE: + return file_range_path(data)->dentry->d_sb; case FSNOTIFY_EVENT_ERROR: return ((struct fs_error_report *) data)->sb; default: @@ -379,6 +399,18 @@ static inline struct fs_error_report *fsnotify_data_error_report( } } +static inline const struct file_range *fsnotify_data_file_range( + const void *data, + int data_type) +{ + switch (data_type) { + case FSNOTIFY_EVENT_FILE_RANGE: + return (struct file_range *)data; + default: + return NULL; + } +} + /* * Index to merged marks iterator array that correlates to a type of watch. * The type of watched object can be deduced from the iterator type, but not