From patchwork Thu Jul 25 18:19:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josef Bacik X-Patchwork-Id: 13742080 Received: from mail-oi1-f175.google.com (mail-oi1-f175.google.com [209.85.167.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E77D647A5D for ; Thu, 25 Jul 2024 18:20:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721931614; cv=none; b=lbrFsgL7m1kEIIaueyLIR0kAJ98j6im4jcpVb8eZFZPyU5nQsHmRqgGO2Q+O/qC1bljfBCBq4OMtQ+pgY1WtD5dfcaA9incVqs7bryG6PQK35xmo5O8NjzPJdNeC+TBUXW+njhQQRomhn9d7GagGSj8JU1V4hqIrvinJpbr53q0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721931614; c=relaxed/simple; bh=QfFAJZi1ijKokWhReW3YGetgRDfXXCQw2sw1ONMQWlI=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QfJJP6n06E5fZzwPeD9h8hrnawanmysiK0YU6rbu2rQ90RQXOqXdUai1peefwfpiO2hkaMGQ9dF7+dSKhwdu8gUqBhdv/5qlAXn81QGaw/glmSAqyeft0wfhKqbyQwWsNff7GTMFCPoFhC9AFGFNWIZqAdpP+KxKCmqBakWi0yc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com; spf=none smtp.mailfrom=toxicpanda.com; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b=BBvJChRY; arc=none smtp.client-ip=209.85.167.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=toxicpanda.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=toxicpanda-com.20230601.gappssmtp.com header.i=@toxicpanda-com.20230601.gappssmtp.com header.b="BBvJChRY" Received: by mail-oi1-f175.google.com with SMTP id 5614622812f47-3db18c4927bso97897b6e.1 for ; Thu, 25 Jul 2024 11:20:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20230601.gappssmtp.com; s=20230601; t=1721931612; x=1722536412; darn=vger.kernel.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=G4z1UMQB2Cf15fcbZ6nTMxNOadcHl0cZubflIeRAG14=; b=BBvJChRYtQZLINVkeCALtJeMvpqgHH3EkoRDm/REpZh+DWr7a8T81ukKvJO20iwF4l 3I2XydP4Neyo16PMP5d8f2oYE/y8vKwLtM/WnTac8ndmdcvAeEpHLUESilt0pXzAvSCL G/HvTp0nWm05dJPdJmZpB083PzTC8uAc1jvhWVuQ9qo5D1z761zE2hEGneGbpLuPXclO HOXCp7hTJZ+Hh87rYZmQ6i1Nyle6P812fawcb+TlE0l7xoHgeummXFQYOjA7IqcV+qBr dFQwU20o8VSxsyqQ2uKs1/85yCi8FyRFocdjw/wScAsI8hKm0TMx9YsP23Z8YKiVyOPU 3wXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721931612; x=1722536412; 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=G4z1UMQB2Cf15fcbZ6nTMxNOadcHl0cZubflIeRAG14=; b=vtpC3T8xFkpZsGysYUh/kUAq/UFIR/Wg12EPPlNSOvc675RMoK16eQc+5oRdJp3F1L tDMKdHyIejIEubON6SaArTMpmgalK+ARlYLd3BQHM2tRMWenliXQd+X/RQblJ1rU0rDD Y8Rm6F5/cE2wIZ1HV3djkLTW+JwQP69iGqUzy6TcwMHC7gmhUEl6MWpM4Yhi0dh6a/L5 U0aZKZtVFwpAYNmF1gvxOI5nE/hbvUFqlkNhh9y2ng+6+VUhIbhoqyNIx6u1t5Jv6R+p Jw9TgheHqGwcOa3x6bW/iC2+SVxFT1lkkjie3jPcGywrH75tAeXLfmi1195dwBp3VXrP rCpg== X-Forwarded-Encrypted: i=1; AJvYcCWGoEnoh09HD+TLxYKZnR9xqFC8sOnmUBW1wenxFy0Da4yehe6j20wmKLrfxaCIdeXAbQOeqywipv6Lylg2SXyucgIr9V9pOBSuA9/jbw== X-Gm-Message-State: AOJu0YzMeZ2OAIFDGoxNdfHThvEZunjq/sC4COne71QL53D7hzcLpGuS /NrX/Wrs3qfSQ5emZv1KcGlMoslNneN3SzLzrcrsUcFXShs4YRy13qjzbARREsI= X-Google-Smtp-Source: AGHT+IE1rSPn0WJ6xw6tftc8EPxW3z+Hgj0ZMTT8ZyQce9xpWrheqFTpVBOEFesJk3MbDM0JIU592A== X-Received: by 2002:a05:6808:23c2:b0:3d5:61d9:fac1 with SMTP id 5614622812f47-3db14214d9dmr3169261b6e.46.1721931611924; Thu, 25 Jul 2024 11:20:11 -0700 (PDT) Received: from localhost (syn-076-182-020-124.res.spectrum.com. [76.182.20.124]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-44fe840c096sm8373531cf.79.2024.07.25.11.20.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Jul 2024 11:20:11 -0700 (PDT) From: Josef Bacik To: kernel-team@fb.com, linux-fsdevel@vger.kernel.org, jack@suse.cz, amir73il@gmail.com, brauner@kernel.org Subject: [PATCH 06/10] fanotify: pass optional file access range in pre-content event Date: Thu, 25 Jul 2024 14:19:43 -0400 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 | 12 ++++++++++-- fs/notify/fanotify/fanotify.h | 2 ++ include/linux/fsnotify.h | 17 ++++++++++++++++- include/linux/fsnotify_backend.h | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 3 deletions(-) diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c index b163594843f5..8fa439bd47d6 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,10 @@ static struct fanotify_event *fanotify_alloc_perm_event(const struct path *path, pevent->hdr.len = 0; pevent->state = FAN_EVENT_INIT; pevent->path = *path; + if (range) { + pevent->ppos = range->ppos; + pevent->count = range->count; + } path_get(path); return &pevent->fae; @@ -802,7 +810,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 4103dd797477..3c558c76bd5d 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 21e72b837ec5..36c3d18cc40a 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