From patchwork Fri Nov 15 15:30:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13876445 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 BF0BBD68BC8 for ; Fri, 15 Nov 2024 15:31:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 42DB86B0096; Fri, 15 Nov 2024 10:31:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3DBA86B0099; Fri, 15 Nov 2024 10:31:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1E1B36B009A; Fri, 15 Nov 2024 10:31:38 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id EBDDB6B0096 for ; Fri, 15 Nov 2024 10:31:37 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id A3FEE1219AD for ; Fri, 15 Nov 2024 15:31:37 +0000 (UTC) X-FDA: 82788716724.03.1116C5C Received: from mail-yw1-f180.google.com (mail-yw1-f180.google.com [209.85.128.180]) by imf20.hostedemail.com (Postfix) with ESMTP id 134431C002E for ; Fri, 15 Nov 2024 15:30:38 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=ErlCvOsc; spf=none (imf20.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.128.180) 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=1731684638; 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=HmDi22ux7vu/mgZp6ZHEvpaDEJU9ywEimHDaeGkRuI8=; b=4AP8WL65OgBJBojhdrVoBtI/fKj48Nyky/IFYAufLiYHERbLOsCO/0x2/xn9TZbNtUH4z6 huOCVnT2zlKPbyFZ1Bc+qc+141y1C+Bs95Swa+IupsTAqd/8eNxono3/d0CaIbVmUsVZEu CTxTB/P3CvGWJH+dQxsyNra8IfA3Szg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731684638; a=rsa-sha256; cv=none; b=KlCaaSWyLf2cKReQ58Ij9gefsAWREX5CGd0brUJMBu+vlUnqgQRpQgR6ZgGLGmRryaee5K uSXt7Ikgw3EOW0bQCEzMw1bKqQdqODXNNY7iGWcaZekPf/W+2X72hvXb+vZK7De6vony2o itboUmNuG1+yt4gfRnhscDozSYHMCNI= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=ErlCvOsc; spf=none (imf20.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.128.180) smtp.mailfrom=josef@toxicpanda.com; dmarc=none Received: by mail-yw1-f180.google.com with SMTP id 00721157ae682-6eb0c2dda3cso20774697b3.1 for ; Fri, 15 Nov 2024 07:31:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1731684695; x=1732289495; 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=HmDi22ux7vu/mgZp6ZHEvpaDEJU9ywEimHDaeGkRuI8=; b=ErlCvOscPSm4ulGYNwcMoaCv3Hh83HO1qYWCzc44zo80u+mzyr09lHvFwG2i3zO01D XBOkyhNwLgqhZNice/FxESLKgnobBk5mPK5eG8SZYfpKSyiBc3pJoVRrNzJf8PiSzT/6 0NMRgbzoMwwud88mEkmks+lBtgiwHpe9lMrFcIdO5g3p/fQsi/TewEpi/6K/K9LwwmE/ X2ZVyXtuVrEDhfGyu+rYkGVkjUZc9LEM8waEBZe20zVKj5QV/y5ZxFVlSnEC/yy+GHZa TEpDQlb87CWf5YDGpS53FgGP/soXEGqm4m9YwAd0rzDILaQspRnOEusvB0ZWo7zPJDMH 8S1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731684695; x=1732289495; 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=HmDi22ux7vu/mgZp6ZHEvpaDEJU9ywEimHDaeGkRuI8=; b=Y21qkl20Kkexcq5bdljkRQAPSW6XWN+Ym6MPUQduDSXiB3m/DI8N2KXyDXp4hCW5Dj jlmR8IVR7yyFqUKBAE8FNB66nUdd2sKqjE8PPA9AJJp0XP4b0EYhN45EF3d+oaOGr4H3 sMn8Vc0TlOxTaFkOnwIyCB+iwB3BPI7hm/OX4aV06OBSDUbB4IbWFHvIxg10ZH0jtkmg C4bHeVHnHC4BW6QcWDokEmxoGzqCSqAuerXgThFtcGKYi/2i3qwGhtfcP9DLgDKZez31 pDrW0PXCC3KLSf1Iop65ivB4eCaB2I5Uvj1LKhSVfsaxTLEgj8kUf1/cdM7NJNfLdSmD hxfw== X-Forwarded-Encrypted: i=1; AJvYcCV1U/WoMwGNKmIVUbUOTlPar/lnK2pkoae5/Jak80wl5+T9+pBTOo11aLkrVbuXO9hjDzsUmyJsYQ==@kvack.org X-Gm-Message-State: AOJu0YyPcfROf9VW4kafnGR87aunVcvHS7pC+T74zNcZJxW+0mMK6opy lMu3JC2uGqJVUJpEXf4pZc09h95RK3QdIGcMDJbPhQAHXmtcpnkwuwarqqcf/Ks= X-Google-Smtp-Source: AGHT+IHRArDyJHlTef+3JwzlBCm2e+TOdRS4anVHdo81AyQIdw4zsmIETl6tl9pEKTE/6sFxZRHk4g== X-Received: by 2002:a05:690c:490e:b0:652:5838:54ef with SMTP id 00721157ae682-6ee55cff0c8mr38839537b3.37.1731684694891; Fri, 15 Nov 2024 07:31:34 -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-6ee4444b907sm7704477b3.121.2024.11.15.07.31.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Nov 2024 07:31:34 -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 08/19] fsnotify: pass optional file access range in pre-content event Date: Fri, 15 Nov 2024 10:30:21 -0500 Message-ID: <88eddee301231d814aede27fb4d5b41ae37c9702.1731684329.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-Stat-Signature: bfk78hdio4psr3i1uifkpjbbc1fstdxi X-Rspam-User: X-Rspamd-Queue-Id: 134431C002E X-Rspamd-Server: rspam02 X-HE-Tag: 1731684638-411341 X-HE-Meta: U2FsdGVkX1+Tq7PT0vEjXUAz+9//tokore4IxWg5DyJb1N9Vw1VcwU5J1iXCNJfJpKGlx6/ENN+DAxkC6pBuugINcoBq0QSb6s6zp1CZvx5Bgo5AZsqQUKWKljP2J1KrrQs3zIHVI/xjdN5pjpMDWKVRh34qQWJIxZqavGrnEapDrh04ExjJ9P/c8sSiIiZCbcKdKfX3G5Cg0oKM5bY0wQfZ4cIAQzjcahE7wBuBcJegsh0CZx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SkjHzMg== 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 We would like to add file range information to pre-content events. Pass a struct file_range with offset and length to event handler along with pre-content permission event. The offset and length are aligned to page size, but we may need to align them to minimum folio size for filesystems with large block size. Signed-off-by: Amir Goldstein --- fs/notify/fanotify/fanotify.c | 11 +++++++-- fs/notify/fanotify/fanotify.h | 2 ++ fs/notify/fsnotify.c | 18 ++++++++++++++ include/linux/fsnotify.h | 4 ++-- include/linux/fsnotify_backend.h | 40 ++++++++++++++++++++++++++++++++ 5 files changed, 71 insertions(+), 4 deletions(-) diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c index 24c7c5df4998..2e6ba94ec405 100644 --- a/fs/notify/fanotify/fanotify.c +++ b/fs/notify/fanotify/fanotify.c @@ -548,9 +548,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); @@ -564,6 +568,9 @@ static struct fanotify_event *fanotify_alloc_perm_event(const struct path *path, pevent->hdr.len = 0; pevent->state = FAN_EVENT_INIT; pevent->path = *path; + /* NULL ppos means no range info */ + pevent->ppos = range ? &range->pos : NULL; + pevent->count = range ? range->count : 0; path_get(path); return &pevent->fae; @@ -801,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/fs/notify/fsnotify.c b/fs/notify/fsnotify.c index d128cb7dee62..538aacf990ca 100644 --- a/fs/notify/fsnotify.c +++ b/fs/notify/fsnotify.c @@ -225,6 +225,24 @@ __u32 fsnotify_file_object_watched(struct file *file, __u32 events_mask) } EXPORT_SYMBOL_GPL(fsnotify_file_object_watched); +/* Report pre-content event with optional range info */ +int fsnotify_pre_content(const struct path *path, const loff_t *ppos, + size_t count) +{ + struct file_range range; + + /* Report page aligned range only when pos is known */ + if (!ppos) + return fsnotify_path(path, FS_PRE_ACCESS); + + range.path = path; + range.pos = PAGE_ALIGN_DOWN(*ppos); + range.count = PAGE_ALIGN(*ppos + count) - range.pos; + + return fsnotify_parent(path->dentry, FS_PRE_ACCESS, &range, + FSNOTIFY_EVENT_FILE_RANGE); +} + /* * 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 diff --git a/include/linux/fsnotify.h b/include/linux/fsnotify.h index 994d7a322369..ce189b4778a5 100644 --- a/include/linux/fsnotify.h +++ b/include/linux/fsnotify.h @@ -201,7 +201,7 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask, * read()/write() and other types of access generate pre-content events. */ if (unlikely(FMODE_FSNOTIFY_HSM(file->f_mode))) { - int ret = fsnotify_path(&file->f_path, FS_PRE_ACCESS); + int ret = fsnotify_pre_content(&file->f_path, ppos, count); if (ret) return ret; @@ -218,7 +218,7 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask, } /* - * fsnotify_file_perm - permission hook before file access + * fsnotify_file_perm - permission hook before file access (unknown range) */ static inline int fsnotify_file_perm(struct file *file, int perm_mask) { diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h index 33880de72ef3..89f351193d8f 100644 --- a/include/linux/fsnotify_backend.h +++ b/include/linux/fsnotify_backend.h @@ -294,6 +294,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, @@ -306,6 +307,17 @@ struct fs_error_report { struct super_block *sb; }; +struct file_range { + const struct path *path; + loff_t pos; + 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) { @@ -315,6 +327,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: @@ -330,6 +344,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; } @@ -341,6 +357,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; } @@ -356,6 +374,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: @@ -375,6 +395,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 @@ -865,6 +897,8 @@ static inline void fsnotify_init_event(struct fsnotify_event *event) } __u32 fsnotify_file_object_watched(struct file *file, __u32 mask); +int fsnotify_pre_content(const struct path *path, const loff_t *ppos, + size_t count); #else @@ -873,6 +907,12 @@ static inline __u32 fsnotify_file_object_watched(struct file *file, __u32 mask) return 0; } +static inline int fsnotify_pre_content(const struct path *path, + const loff_t *ppos, size_t count) +{ + 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)