From patchwork Mon Nov 11 20:17:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13871223 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 6B643D3ABF6 for ; Mon, 11 Nov 2024 20:19:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 49B116B00A1; Mon, 11 Nov 2024 15:19:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4489A6B00A2; Mon, 11 Nov 2024 15:19:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 278BE6B00A3; Mon, 11 Nov 2024 15:19:19 -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 02D816B00A1 for ; Mon, 11 Nov 2024 15:19:18 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 85FDC1C79FC for ; Mon, 11 Nov 2024 20:19:18 +0000 (UTC) X-FDA: 82774927578.23.2B71B36 Received: from mail-qk1-f169.google.com (mail-qk1-f169.google.com [209.85.222.169]) by imf28.hostedemail.com (Postfix) with ESMTP id 296B9C0011 for ; Mon, 11 Nov 2024 20:18:35 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=OaQymNll; dmarc=none; spf=none (imf28.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.222.169) smtp.mailfrom=josef@toxicpanda.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731356270; 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=O6ag15Q24m9w8QwPLcjiANNOCwpZepXzE8l8EGOvBlA=; b=KqVGkxiivneYFPO0HN4b5pmlQPgz/uYfmCIJx3gRevwimGBD1REMGDEWPZBK2opzfBbNYh r1myB9wro48Es6bKkD2zf0YjSiwI8t1hcVQMJ7bDBJf4b5nOmB8+XPQ86fbox82f3Y+HTq a0vP+thBLMUFVPwBDRRCDTMwx+F1d2E= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=toxicpanda-com.20230601.gappssmtp.com header.s=20230601 header.b=OaQymNll; dmarc=none; spf=none (imf28.hostedemail.com: domain of josef@toxicpanda.com has no SPF policy when checking 209.85.222.169) smtp.mailfrom=josef@toxicpanda.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731356270; a=rsa-sha256; cv=none; b=G0WtGzMrDQB0aWkFt4Y1uUJ+wW8P4VBcybk2XGmkbG3ghFtucRTSsXcZSWRwrm0UNNOOhh Bl6k+xY3PFSf6Jh+Raw4mtIuuyoWEKXWjKgUwFnudmjXgYKXvzjiDvweBXHmKXfCCGMMod WAjh2qgGmNSbHr40WVNVc4ufV02r/2s= Received: by mail-qk1-f169.google.com with SMTP id af79cd13be357-7b1505ef7e3so300684585a.0 for ; Mon, 11 Nov 2024 12:19:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1731356356; x=1731961156; 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=O6ag15Q24m9w8QwPLcjiANNOCwpZepXzE8l8EGOvBlA=; b=OaQymNllEnoffm7dDIgbj3WA5XRiI6zKIG1aL5y+Axej7usyRP9lCAFVcDaAuobmZQ CnDbAl9HEkuEF8ZvtQe0sWMqafXMMo1+VxQkz3r+GA8Vs6RzsTBWFfHlS62vpiQXAtQg /MzY/xFYMh7UbitUZApru8n2zF5qwGN6GYyQkAIJZ/k+9oqaFQdRDqMU205K8Q+4VdLK fH3QQ4gE+uOWJaUZQnofRjV0Rj0QSDxcgBZPi4fOo+GRg+8zoMHbPDdku7GeA1/eoUJd QZvtfIyJenTAV4ENlS4Ve2HLm+3ut0cS3FflGzuDaaTOkdK8PW6oMxyNvcYoOko/miy7 wWFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731356356; x=1731961156; 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=O6ag15Q24m9w8QwPLcjiANNOCwpZepXzE8l8EGOvBlA=; b=JI5+EKYQlHAVwOSRfIvxJBwmMklnvZIesDItsqzkD/L1WEOA9QsO7u03bPOKG6WE/e SsDi9H7+dTtQvw8OVRpdlMIMfkIPGJjWdSXBJdrrZZNdxbCcKsmT0PPqewB6cte6kGrp B8oghXOPUVFWQQj1oDY1oaz47wVR+8M4iLdW1SOzpyQdAyl9ilpwR+gCVZcQSYf9EgUr NJIVkZm+TF2U5Pfuh6Pw2jg8lKlbsvBGi5R6qCpukiRyfoo9rfLYGCzQS/8W6z4NB0uu TZSb1dclrjG63apGjCyDwKlDmtLKTtBxDujbuHBhRVCytYpA6OBBx+UzGOoqkEAF8nop Ycew== X-Forwarded-Encrypted: i=1; AJvYcCW+3buXUmx469mmwq/Rt+LhT8sdnMJjiZ5qy3s0IbwDhUK21KTP/525g5BVqxz5mM9CYYM0QleJZg==@kvack.org X-Gm-Message-State: AOJu0YwgLg/bECVWrqwdBF6asYIjWN3z2/ud53yvqbxAjAaN4xA57zin GEOHudd/kvgGACa1sNLU54jzfmNyRUeXssdtzyrlBhM3sXVLbvQ8DpidYd1sHOY= X-Google-Smtp-Source: AGHT+IEW2yf22nobd0a01YcQDEMyEsXEK6xdxMmQ1VVAPmH7hZnR8VEcq8csG7mjjoxGe6bC+tsVsw== X-Received: by 2002:a05:620a:468c:b0:7b1:48d2:f15e with SMTP id af79cd13be357-7b331f3477emr1883416685a.54.1731356355916; Mon, 11 Nov 2024 12:19:15 -0800 (PST) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7b32acf7f31sm524088485a.129.2024.11.11.12.19.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:19:15 -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 v6 05/17] fsnotify: pass optional file access range in pre-content event Date: Mon, 11 Nov 2024 15:17:54 -0500 Message-ID: <17994e73515dbbad80c1687421dedd3a44f765ae.1731355931.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: 296B9C0011 X-Stat-Signature: igs9tthmsykb6zwt4zqbnn3ijpti58yg X-HE-Tag: 1731356315-836488 X-HE-Meta: U2FsdGVkX18EoefQIqJRI+VDXSrzJAxdPx2khzgM9nKKpzqIa8g+k9LB0WW/R15ZdRiZDJuMgX5f+GE6V7Ghr4p7G59s7u2DKt9Le/iY0rs5IzyqEZCNNYhz/6ValqLpHqnD58nKYSvvxtG5eUZKcpYlP7dqbGK5Mu5sy6h371o9qnga7LBBp85YYtRuMxcAD9FlgH+wkaTISPUQPpYtuHmoA5srB19zoLKQCVf5mlXpQg41vbtYP2L4+lui9ED0kEf6VQiEng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, 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 ++ 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 224bccaab4cc..c1e4ae221093 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,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; @@ -802,7 +809,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 7c641161b281..22150e5797c5 100644 --- a/include/linux/fsnotify.h +++ b/include/linux/fsnotify.h @@ -132,9 +132,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 (file->f_mode & FMODE_NONOTIFY) + return 0; /* * Pre-content events are only reported for regular files and dirs @@ -146,7 +153,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); } /* @@ -166,7 +186,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; @@ -183,7 +203,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