]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
fsnotify: pass optional file access range in pre-content event
authorAmir Goldstein <amir73il@gmail.com>
Fri, 15 Nov 2024 15:30:21 +0000 (10:30 -0500)
committerJan Kara <jack@suse.cz>
Tue, 10 Dec 2024 11:03:17 +0000 (12:03 +0100)
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 <amir73il@gmail.com>
Signed-off-by: Jan Kara <jack@suse.cz>
Link: https://patch.msgid.link/88eddee301231d814aede27fb4d5b41ae37c9702.1731684329.git.josef@toxicpanda.com
fs/notify/fanotify/fanotify.c
fs/notify/fanotify/fanotify.h
fs/notify/fsnotify.c
include/linux/fsnotify.h
include/linux/fsnotify_backend.h

index 24c7c5df4998d0c0bdd36bee5b0c2ac2b3b7a923..2e6ba94ec4056335beea29c7b2e1e0f8ff74db6f 100644 (file)
@@ -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);
index e5ab33cae6a7698fa6b91c08ecacb498241869ac..93598b7d5952c524fefb76af6d8ef0f2a61d974b 100644 (file)
@@ -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 */
index 32b461c5d04bb5be757cc64351000fbfcecd10e2..8ee495a58d0adcc0e1381f885938820bdfecc9a8 100644 (file)
@@ -203,6 +203,24 @@ static inline bool fsnotify_object_watched(struct inode *inode, __u32 mnt_mask,
        return mask & marks_mask & ALL_FSNOTIFY_EVENTS;
 }
 
+/* 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
index d91aa064f0e42981b9c298460045b56be55cdad0..87044acf8e791000b0c0a95b4c3147e007d122ed 100644 (file)
@@ -154,7 +154,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;
@@ -171,7 +171,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)
 {
index 9bda354b553876698f8a0f628b0478372e60a7bb..0d24a21a8e60a80a887ebe6baa9b5acfc546afcd 100644 (file)
@@ -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
@@ -863,9 +895,17 @@ static inline void fsnotify_init_event(struct fsnotify_event *event)
 {
        INIT_LIST_HEAD(&event->list);
 }
+int fsnotify_pre_content(const struct path *path, const loff_t *ppos,
+                        size_t count);
 
 #else
 
+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)