From patchwork Tue Nov 12 17:55: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: 13872732 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 ED887D42BBA for ; Tue, 12 Nov 2024 17:56:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 250596B0104; Tue, 12 Nov 2024 12:56:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 201086B0103; Tue, 12 Nov 2024 12:56:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 052B36B0104; Tue, 12 Nov 2024 12:56:40 -0500 (EST) 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 CC7706B0102 for ; Tue, 12 Nov 2024 12:56:40 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 55D24140539 for ; Tue, 12 Nov 2024 17:56:40 +0000 (UTC) X-FDA: 82778195220.17.DD6E3B1 Received: from mail-yw1-f169.google.com (mail-yw1-f169.google.com [209.85.128.169]) by imf22.hostedemail.com (Postfix) with ESMTP id 60526C000E for ; Tue, 12 Nov 2024 17:55:46 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=PRua1+Hv; dmarc=none; spf=none (imf22.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.128.169) smtp.mailfrom=josef@toxicpanda.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731434022; 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=9qmzKtSqQfrhv9mwfplhMG+Dc91SaSr6kT90grfFzXE=; b=TSUeYVu26XqffsCYicV37UuzM/VE8zoEsBTlqXnU2oGNMY7EngBQuYAvM8omX8dMJ9hfEQ TIa15V+uEKGP+Qsx4SpL4swnWQzhrXDCHj95/G04nZIWH2aD3ETa1cFRWt7S4NZ2TmYvcP BhFLIw8LVzmbUTPHiFtmW7KNqmnufUc= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=PRua1+Hv; dmarc=none; spf=none (imf22.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.128.169) smtp.mailfrom=josef@toxicpanda.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731434022; a=rsa-sha256; cv=none; b=nJVZ4dvVk55EIaRYjxD9QMkfIl3zSSGQFbXymXd4BWs+j2HoBk4dkUSWKM4avvCHsUSISa Qdyh/Oo/2exBKLBBniV6DcgNiHlVQiM4w+TfFpG5Zbe5tqLiYmJWX1dZVSRXZWalUaJ5r9 sUfvtjq2fpRb1pKdR5rbEA0sPZipdj0= Received: by mail-yw1-f169.google.com with SMTP id 00721157ae682-6ea7c26e195so61476727b3.0 for ; Tue, 12 Nov 2024 09:56:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1731434198; x=1732038998; 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=9qmzKtSqQfrhv9mwfplhMG+Dc91SaSr6kT90grfFzXE=; b=PRua1+Hva79vgl8yXshIRK8Dhxu7SaTZPA60P0RcogoJ8t6FlR7h0c8P+RKrp8fAzk wYTtMLqCPCUFyRM7mIRHEu92mBb7SvXkWCmyH8Hvp0gtnns+bWFBIYh5vOOCtm87DJWB YBmkVWStkFkO62/hzcgnG4frep3y4eyRvqu/ITYbBCaBLaAB5etUsKglkwOTmDt3db9J SJofaDyRErG2lGzEw9amj9kUyDGD+IclQx1Yds2AUFS/S5HmBGaDI9SN0xZlkXP6edu2 hA18hyu1RGkJP/EAawv29EjCI5mBagsPRvgbT6FBCzwOGOWXLu1tkDFOk+2E+Ex4Dkak WLDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731434198; x=1732038998; 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=9qmzKtSqQfrhv9mwfplhMG+Dc91SaSr6kT90grfFzXE=; b=slFNxHUqv4oGHUOxeV9MgIh4ff5AGa4Lb2GE1Hn8dCNgOYzq+LXGT66isBw0Ssbv7H HeRLZ+F/ZD9qh1KSTfaIxXILbmWgE+b5QuUSV2lk0T2wAdANuBOThe1AoM6R/TFIsnn4 0Yx7ysRTKJjjdG05JR9kUie49LwAjbDJF3eInPKX1TDHcXXf3DFXIDSAEIL5JF8p3H84 3wsHRLEId57eXaQzFQuRP6hKxFcMsIBQwyo70BN97OQl71eZUtWBJUXrVQ33g7Bc1Td7 rLOf8IcTpjSPdBt52cQ7AfCLwyBa1Aa62ihsPjK5LFWKGAXfJNa45w6vg05VB5q3P8JK r0EQ== X-Forwarded-Encrypted: i=1; AJvYcCUuGVNX8U45MzsdkJ3vgv2jAxNXADi+IqNJQ2StI51+kdITsGozkCP72G75FV6ZlPtVHd0YBO0pIg==@kvack.org X-Gm-Message-State: AOJu0YyZbCDARbSN5Oo/NKFnhZotc2QXYIMQj8f9Csm1r1opnS0BhvEw 0r8O7tmfNRvzx338v8yjnIXtkUFGN5pfKFhr/B8AzGKwnFn2cqRWUUDCH6GYbu8= X-Google-Smtp-Source: AGHT+IHyY6QTMjull+z1rPYeXNfxhgVBLW70xt2rGV+KFU983vos+ZL89drz2YsnyEbkmVx23Jm32A== X-Received: by 2002:a05:690c:6885:b0:6ea:7afd:d901 with SMTP id 00721157ae682-6eaddd98734mr177756967b3.18.1731434197704; Tue, 12 Nov 2024 09:56:37 -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-6eaceb65a9csm26511117b3.92.2024.11.12.09.56.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Nov 2024 09:56:37 -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, linux-xfs@vger.kernel.org, linux-btrfs@vger.kernel.org, linux-mm@kvack.org, linux-ext4@vger.kernel.org Subject: [PATCH v7 06/18] fsnotify: pass optional file access range in pre-content event Date: Tue, 12 Nov 2024 12:55:21 -0500 Message-ID: <715ea468f03513b1b1a8e478f7096acc69a227fe.1731433903.git.josef@toxicpanda.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 60526C000E X-Stat-Signature: tm8umeoh9hpfj9foopzf99hui7i1qimk X-Rspam-User: X-HE-Tag: 1731434146-800985 X-HE-Meta: U2FsdGVkX19GKFex7OT4289U0ocIebM8Uu3aMjwBIIwsO3DMbXwwI6mjCrlKqtrBtvHKe5eX+/S2W01gPpWa54x2pmRTwjFtSj1PE4acIu0Za30grQgm9/avfoMuIuR05YnLBKhO9b5bcmdLj4kTXjwgvDhkds2ur2jhQrDi0vH3F0KhSOUf/tBA1YQIHHHNoLI6fX3rfgFYZWwkik9oQ0XhJU78Ulje9gIPaiR2i6dwnNsgeIZrmu+Q0qOl6xubQgD3Gon76xG0urYou115o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icity=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 ++ include/linux/fsnotify.h | 28 ++++++++++++++++++++++++---- include/linux/fsnotify_backend.h | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 6 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/include/linux/fsnotify.h b/include/linux/fsnotify.h index 0f44cd60ac9a..7110bc2f5aa7 100644 --- a/include/linux/fsnotify.h +++ b/include/linux/fsnotify.h @@ -154,9 +154,16 @@ static inline int fsnotify_file(struct file *file, __u32 mask) } #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS -static inline int fsnotify_pre_content(struct file *file) +static inline int fsnotify_pre_content(const struct file *file, + const loff_t *ppos, size_t count) { struct inode *inode = file_inode(file); + struct file_range range; + const void *data; + int data_type; + + if (!fsnotify_file_watchable(file, FS_PRE_ACCESS)) + return 0; /* * Pre-content events are only reported for regular files and dirs @@ -168,7 +175,20 @@ static inline int fsnotify_pre_content(struct file *file) FSNOTIFY_PRIO_PRE_CONTENT)) return 0; - return fsnotify_file(file, FS_PRE_ACCESS); + /* Report page aligned range only when pos is known */ + if (ppos) { + range.path = &file->f_path; + range.pos = PAGE_ALIGN_DOWN(*ppos); + range.count = PAGE_ALIGN(*ppos + count) - range.pos; + data = ⦥ + data_type = FSNOTIFY_EVENT_FILE_RANGE; + } else { + data = &file->f_path; + data_type = FSNOTIFY_EVENT_PATH; + } + + return fsnotify_parent(file->f_path.dentry, FS_PRE_ACCESS, + data, data_type); } /* @@ -188,7 +208,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 (perm_mask & (MAY_READ | MAY_WRITE | MAY_ACCESS)) { - int ret = fsnotify_pre_content(file); + int ret = fsnotify_pre_content(file, ppos, count); if (ret) return ret; @@ -205,7 +225,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 9bda354b5538..abd292edb48c 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