]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
fsnotify: use accessor to set FMODE_NONOTIFY_*
authorAmir Goldstein <amir73il@gmail.com>
Mon, 3 Feb 2025 22:32:03 +0000 (23:32 +0100)
committerChristian Brauner <brauner@kernel.org>
Fri, 7 Feb 2025 09:27:26 +0000 (10:27 +0100)
The FMODE_NONOTIFY_* bits are a 2-bits mode.  Open coding manipulation
of those bits is risky.  Use an accessor file_set_fsnotify_mode() to
set the mode.

Rename file_set_fsnotify_mode() => file_set_fsnotify_mode_from_watchers()
to make way for the simple accessor name.

Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Link: https://lore.kernel.org/r/20250203223205.861346-2-amir73il@gmail.com
Signed-off-by: Christian Brauner <brauner@kernel.org>
drivers/tty/pty.c
fs/notify/fsnotify.c
fs/open.c
include/linux/fs.h
include/linux/fsnotify.h

index df08f13052ff4b53f19f28803ea9baedfb64c718..8bb1a01fef2a1e6a011b5d3b5c03ad24ade2825c 100644 (file)
@@ -798,7 +798,7 @@ static int ptmx_open(struct inode *inode, struct file *filp)
        nonseekable_open(inode, filp);
 
        /* We refuse fsnotify events on ptmx, since it's a shared resource */
-       filp->f_mode |= FMODE_NONOTIFY;
+       file_set_fsnotify_mode(filp, FMODE_NONOTIFY);
 
        retval = tty_alloc_file(filp);
        if (retval)
index 8ee495a58d0adcc0e1381f885938820bdfecc9a8..fae1b6d397ea0050d36a1f33ac47ce269c501b9a 100644 (file)
@@ -648,7 +648,7 @@ EXPORT_SYMBOL_GPL(fsnotify);
  * Later, fsnotify permission hooks do not check if there are permission event
  * watches, but that there were permission event watches at open time.
  */
-void file_set_fsnotify_mode(struct file *file)
+void file_set_fsnotify_mode_from_watchers(struct file *file)
 {
        struct dentry *dentry = file->f_path.dentry, *parent;
        struct super_block *sb = dentry->d_sb;
@@ -665,7 +665,7 @@ void file_set_fsnotify_mode(struct file *file)
         */
        if (likely(!fsnotify_sb_has_priority_watchers(sb,
                                                FSNOTIFY_PRIO_CONTENT))) {
-               file->f_mode |= FMODE_NONOTIFY_PERM;
+               file_set_fsnotify_mode(file, FMODE_NONOTIFY_PERM);
                return;
        }
 
@@ -676,7 +676,7 @@ void file_set_fsnotify_mode(struct file *file)
        if ((!d_is_dir(dentry) && !d_is_reg(dentry)) ||
            likely(!fsnotify_sb_has_priority_watchers(sb,
                                                FSNOTIFY_PRIO_PRE_CONTENT))) {
-               file->f_mode |= FMODE_NONOTIFY | FMODE_NONOTIFY_PERM;
+               file_set_fsnotify_mode(file, FMODE_NONOTIFY | FMODE_NONOTIFY_PERM);
                return;
        }
 
@@ -686,19 +686,25 @@ void file_set_fsnotify_mode(struct file *file)
         */
        mnt_mask = READ_ONCE(real_mount(file->f_path.mnt)->mnt_fsnotify_mask);
        if (unlikely(fsnotify_object_watched(d_inode(dentry), mnt_mask,
-                                    FSNOTIFY_PRE_CONTENT_EVENTS)))
+                                    FSNOTIFY_PRE_CONTENT_EVENTS))) {
+               /* Enable pre-content events */
+               file_set_fsnotify_mode(file, 0);
                return;
+       }
 
        /* Is parent watching for pre-content events on this file? */
        if (dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED) {
                parent = dget_parent(dentry);
                p_mask = fsnotify_inode_watches_children(d_inode(parent));
                dput(parent);
-               if (p_mask & FSNOTIFY_PRE_CONTENT_EVENTS)
+               if (p_mask & FSNOTIFY_PRE_CONTENT_EVENTS) {
+                       /* Enable pre-content events */
+                       file_set_fsnotify_mode(file, 0);
                        return;
+               }
        }
        /* Nobody watching for pre-content events from this file */
-       file->f_mode |= FMODE_NONOTIFY | FMODE_NONOTIFY_PERM;
+       file_set_fsnotify_mode(file, FMODE_NONOTIFY | FMODE_NONOTIFY_PERM);
 }
 #endif
 
index 932e5a6de63bb0e09c93a9bae49ff737d3dff361..3fcbfff8aede8023917f4e050e6aa2977f015607 100644 (file)
--- a/fs/open.c
+++ b/fs/open.c
@@ -905,7 +905,8 @@ static int do_dentry_open(struct file *f,
        f->f_sb_err = file_sample_sb_err(f);
 
        if (unlikely(f->f_flags & O_PATH)) {
-               f->f_mode = FMODE_PATH | FMODE_OPENED | FMODE_NONOTIFY;
+               f->f_mode = FMODE_PATH | FMODE_OPENED;
+               file_set_fsnotify_mode(f, FMODE_NONOTIFY);
                f->f_op = &empty_fops;
                return 0;
        }
@@ -938,7 +939,7 @@ static int do_dentry_open(struct file *f,
         * If FMODE_NONOTIFY was already set for an fanotify fd, this doesn't
         * change anything.
         */
-       file_set_fsnotify_mode(f);
+       file_set_fsnotify_mode_from_watchers(f);
        error = fsnotify_open_perm(f);
        if (error)
                goto cleanup_all;
@@ -1122,7 +1123,7 @@ struct file *dentry_open_nonotify(const struct path *path, int flags,
        if (!IS_ERR(f)) {
                int error;
 
-               f->f_mode |= FMODE_NONOTIFY;
+               file_set_fsnotify_mode(f, FMODE_NONOTIFY);
                error = vfs_open(path, f);
                if (error) {
                        fput(f);
index be3ad155ec9f74460d971f11b96445e8db579c6f..7620547432a84a69d480295177bee6e23524cec3 100644 (file)
@@ -222,7 +222,6 @@ typedef int (dio_iodone_t)(struct kiocb *iocb, loff_t offset,
 #define FMODE_FSNOTIFY_HSM(mode)       0
 #endif
 
-
 /*
  * Attribute flags.  These should be or-ed together to figure out what
  * has been changed!
@@ -3140,6 +3139,12 @@ static inline void exe_file_allow_write_access(struct file *exe_file)
        allow_write_access(exe_file);
 }
 
+static inline void file_set_fsnotify_mode(struct file *file, fmode_t mode)
+{
+       file->f_mode &= ~FMODE_FSNOTIFY_MASK;
+       file->f_mode |= mode;
+}
+
 static inline bool inode_is_open_for_write(const struct inode *inode)
 {
        return atomic_read(&inode->i_writecount) > 0;
index 1a9ef8f6784ddfe61283b6925cfea90f25496561..6a33288bd6a1f394cfb9e7f477ea41da4b45565d 100644 (file)
@@ -129,7 +129,7 @@ static inline int fsnotify_file(struct file *file, __u32 mask)
 
 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
 
-void file_set_fsnotify_mode(struct file *file);
+void file_set_fsnotify_mode_from_watchers(struct file *file);
 
 /*
  * fsnotify_file_area_perm - permission hook before access to file range
@@ -213,7 +213,7 @@ static inline int fsnotify_open_perm(struct file *file)
 }
 
 #else
-static inline void file_set_fsnotify_mode(struct file *file)
+static inline void file_set_fsnotify_mode_from_watchers(struct file *file)
 {
 }