Skip to content

Commit 08da98e

Browse files
amir73iljankara
authored andcommitted
fsnotify: merge file_set_fsnotify_mode_from_watchers() with open perm hook
Create helper fsnotify_open_perm_and_set_mode() that moves the fsnotify_open_perm() hook into file_set_fsnotify_mode_from_watchers(). This will allow some more optimizations. Signed-off-by: Amir Goldstein <[email protected]> Signed-off-by: Jan Kara <[email protected]> Link: https://patch.msgid.link/[email protected]
1 parent 3193e89 commit 08da98e

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

fs/file_table.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ static int init_file(struct file *f, int flags, const struct cred *cred)
196196
file_ref_init(&f->f_ref, 1);
197197
/*
198198
* Disable permission and pre-content events for all files by default.
199-
* They may be enabled later by file_set_fsnotify_mode_from_watchers().
199+
* They may be enabled later by fsnotify_open_perm_and_set_mode().
200200
*/
201201
file_set_fsnotify_mode(f, FMODE_NONOTIFY_PERM);
202202
return 0;

fs/notify/fsnotify.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -656,20 +656,20 @@ EXPORT_SYMBOL_GPL(fsnotify);
656656

657657
#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
658658
/*
659-
* At open time we check fsnotify_sb_has_priority_watchers() and set the
660-
* FMODE_NONOTIFY_ mode bits accordignly.
659+
* At open time we check fsnotify_sb_has_priority_watchers(), call the open perm
660+
* hook and set the FMODE_NONOTIFY_ mode bits accordignly.
661661
* Later, fsnotify permission hooks do not check if there are permission event
662662
* watches, but that there were permission event watches at open time.
663663
*/
664-
void file_set_fsnotify_mode_from_watchers(struct file *file)
664+
int fsnotify_open_perm_and_set_mode(struct file *file)
665665
{
666666
struct dentry *dentry = file->f_path.dentry, *parent;
667667
struct super_block *sb = dentry->d_sb;
668668
__u32 mnt_mask, p_mask;
669669

670670
/* Is it a file opened by fanotify? */
671671
if (FMODE_FSNOTIFY_NONE(file->f_mode))
672-
return;
672+
return 0;
673673

674674
/*
675675
* Permission events is a super set of pre-content events, so if there
@@ -679,7 +679,7 @@ void file_set_fsnotify_mode_from_watchers(struct file *file)
679679
if (likely(!fsnotify_sb_has_priority_watchers(sb,
680680
FSNOTIFY_PRIO_CONTENT))) {
681681
file_set_fsnotify_mode(file, FMODE_NONOTIFY_PERM);
682-
return;
682+
return 0;
683683
}
684684

685685
/*
@@ -689,8 +689,9 @@ void file_set_fsnotify_mode_from_watchers(struct file *file)
689689
if ((!d_is_dir(dentry) && !d_is_reg(dentry)) ||
690690
likely(!fsnotify_sb_has_priority_watchers(sb,
691691
FSNOTIFY_PRIO_PRE_CONTENT))) {
692-
file_set_fsnotify_mode(file, FMODE_NONOTIFY | FMODE_NONOTIFY_PERM);
693-
return;
692+
file_set_fsnotify_mode(file, FMODE_NONOTIFY |
693+
FMODE_NONOTIFY_PERM);
694+
goto open_perm;
694695
}
695696

696697
/*
@@ -702,7 +703,7 @@ void file_set_fsnotify_mode_from_watchers(struct file *file)
702703
FSNOTIFY_PRE_CONTENT_EVENTS))) {
703704
/* Enable pre-content events */
704705
file_set_fsnotify_mode(file, 0);
705-
return;
706+
goto open_perm;
706707
}
707708

708709
/* Is parent watching for pre-content events on this file? */
@@ -713,11 +714,14 @@ void file_set_fsnotify_mode_from_watchers(struct file *file)
713714
if (p_mask & FSNOTIFY_PRE_CONTENT_EVENTS) {
714715
/* Enable pre-content events */
715716
file_set_fsnotify_mode(file, 0);
716-
return;
717+
goto open_perm;
717718
}
718719
}
719720
/* Nobody watching for pre-content events from this file */
720721
file_set_fsnotify_mode(file, FMODE_NONOTIFY | FMODE_NONOTIFY_PERM);
722+
723+
open_perm:
724+
return fsnotify_open_perm(file);
721725
}
722726
#endif
723727

fs/open.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -943,12 +943,12 @@ static int do_dentry_open(struct file *f,
943943
goto cleanup_all;
944944

945945
/*
946-
* Set FMODE_NONOTIFY_* bits according to existing permission watches.
946+
* Call fsnotify open permission hook and set FMODE_NONOTIFY_* bits
947+
* according to existing permission watches.
947948
* If FMODE_NONOTIFY mode was already set for an fanotify fd or for a
948949
* pseudo file, this call will not change the mode.
949950
*/
950-
file_set_fsnotify_mode_from_watchers(f);
951-
error = fsnotify_open_perm(f);
951+
error = fsnotify_open_perm_and_set_mode(f);
952952
if (error)
953953
goto cleanup_all;
954954

include/linux/fsnotify.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ static inline int fsnotify_file(struct file *file, __u32 mask)
129129

130130
#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
131131

132-
void file_set_fsnotify_mode_from_watchers(struct file *file);
132+
int fsnotify_open_perm_and_set_mode(struct file *file);
133133

134134
/*
135135
* fsnotify_file_area_perm - permission hook before access to file range
@@ -215,9 +215,6 @@ static inline int fsnotify_open_perm(struct file *file)
215215
{
216216
int ret;
217217

218-
if (likely(!FMODE_FSNOTIFY_PERM(file->f_mode)))
219-
return 0;
220-
221218
if (file->f_flags & __FMODE_EXEC) {
222219
ret = fsnotify_path(&file->f_path, FS_OPEN_EXEC_PERM);
223220
if (ret)
@@ -228,8 +225,9 @@ static inline int fsnotify_open_perm(struct file *file)
228225
}
229226

230227
#else
231-
static inline void file_set_fsnotify_mode_from_watchers(struct file *file)
228+
static inline int fsnotify_open_perm_and_set_mode(struct file *file)
232229
{
230+
return 0;
233231
}
234232

235233
static inline int fsnotify_file_area_perm(struct file *file, int perm_mask,

0 commit comments

Comments
 (0)