Commit e54183fa authored by Amir Goldstein's avatar Amir Goldstein Committed by Jan Kara

fsnotify: generate FS_RENAME event with rich information

The dnotify FS_DN_RENAME event is used to request notification about
a move within the same parent directory and was always coupled with
the FS_MOVED_FROM event.

Rename the FS_DN_RENAME event flag to FS_RENAME, decouple it from
FS_MOVED_FROM and report it with the moved dentry instead of the moved
inode, so it has the information about both old and new parent and name.

Generate the FS_RENAME event regardless of same parent dir and apply
the "same parent" rule in the generic fsnotify_handle_event() helper
that is used to call backends with ->handle_inode_event() method
(i.e. dnotify).  The ->handle_inode_event() method is not rich enough to
report both old and new parent and name anyway.

The enriched event is reported to fanotify over the ->handle_event()
method with the old and new dir inode marks in marks array slots for
ITER_TYPE_INODE and a new iter type slot ITER_TYPE_INODE2.

The enriched event will be used for reporting old and new parent+name to
fanotify groups with FAN_RENAME events.

Link: https://lore.kernel.org/r/20211129201537.1932819-5-amir73il@gmail.comSigned-off-by: default avatarAmir Goldstein <amir73il@gmail.com>
Signed-off-by: default avatarJan Kara <jack@suse.cz>
parent d61fd650
...@@ -196,7 +196,7 @@ static __u32 convert_arg(unsigned long arg) ...@@ -196,7 +196,7 @@ static __u32 convert_arg(unsigned long arg)
if (arg & DN_ATTRIB) if (arg & DN_ATTRIB)
new_mask |= FS_ATTRIB; new_mask |= FS_ATTRIB;
if (arg & DN_RENAME) if (arg & DN_RENAME)
new_mask |= FS_DN_RENAME; new_mask |= FS_RENAME;
if (arg & DN_CREATE) if (arg & DN_CREATE)
new_mask |= (FS_CREATE | FS_MOVED_TO); new_mask |= (FS_CREATE | FS_MOVED_TO);
......
...@@ -279,6 +279,18 @@ static int fsnotify_handle_event(struct fsnotify_group *group, __u32 mask, ...@@ -279,6 +279,18 @@ static int fsnotify_handle_event(struct fsnotify_group *group, __u32 mask,
WARN_ON_ONCE(fsnotify_iter_vfsmount_mark(iter_info))) WARN_ON_ONCE(fsnotify_iter_vfsmount_mark(iter_info)))
return 0; return 0;
/*
* For FS_RENAME, 'dir' is old dir and 'data' is new dentry.
* The only ->handle_inode_event() backend that supports FS_RENAME is
* dnotify, where it means file was renamed within same parent.
*/
if (mask & FS_RENAME) {
struct dentry *moved = fsnotify_data_dentry(data, data_type);
if (dir != moved->d_parent->d_inode)
return 0;
}
if (parent_mark) { if (parent_mark) {
/* /*
* parent_mark indicates that the parent inode is watching * parent_mark indicates that the parent inode is watching
...@@ -469,7 +481,9 @@ int fsnotify(__u32 mask, const void *data, int data_type, struct inode *dir, ...@@ -469,7 +481,9 @@ int fsnotify(__u32 mask, const void *data, int data_type, struct inode *dir,
struct super_block *sb = fsnotify_data_sb(data, data_type); struct super_block *sb = fsnotify_data_sb(data, data_type);
struct fsnotify_iter_info iter_info = {}; struct fsnotify_iter_info iter_info = {};
struct mount *mnt = NULL; struct mount *mnt = NULL;
struct inode *parent = NULL; struct inode *inode2 = NULL;
struct dentry *moved;
int inode2_type;
int ret = 0; int ret = 0;
__u32 test_mask, marks_mask; __u32 test_mask, marks_mask;
...@@ -479,12 +493,19 @@ int fsnotify(__u32 mask, const void *data, int data_type, struct inode *dir, ...@@ -479,12 +493,19 @@ int fsnotify(__u32 mask, const void *data, int data_type, struct inode *dir,
if (!inode) { if (!inode) {
/* Dirent event - report on TYPE_INODE to dir */ /* Dirent event - report on TYPE_INODE to dir */
inode = dir; inode = dir;
/* For FS_RENAME, inode is old_dir and inode2 is new_dir */
if (mask & FS_RENAME) {
moved = fsnotify_data_dentry(data, data_type);
inode2 = moved->d_parent->d_inode;
inode2_type = FSNOTIFY_ITER_TYPE_INODE2;
}
} else if (mask & FS_EVENT_ON_CHILD) { } else if (mask & FS_EVENT_ON_CHILD) {
/* /*
* Event on child - report on TYPE_PARENT to dir if it is * Event on child - report on TYPE_PARENT to dir if it is
* watching children and on TYPE_INODE to child. * watching children and on TYPE_INODE to child.
*/ */
parent = dir; inode2 = dir;
inode2_type = FSNOTIFY_ITER_TYPE_PARENT;
} }
/* /*
...@@ -497,7 +518,7 @@ int fsnotify(__u32 mask, const void *data, int data_type, struct inode *dir, ...@@ -497,7 +518,7 @@ int fsnotify(__u32 mask, const void *data, int data_type, struct inode *dir,
if (!sb->s_fsnotify_marks && if (!sb->s_fsnotify_marks &&
(!mnt || !mnt->mnt_fsnotify_marks) && (!mnt || !mnt->mnt_fsnotify_marks) &&
(!inode || !inode->i_fsnotify_marks) && (!inode || !inode->i_fsnotify_marks) &&
(!parent || !parent->i_fsnotify_marks)) (!inode2 || !inode2->i_fsnotify_marks))
return 0; return 0;
marks_mask = sb->s_fsnotify_mask; marks_mask = sb->s_fsnotify_mask;
...@@ -505,8 +526,8 @@ int fsnotify(__u32 mask, const void *data, int data_type, struct inode *dir, ...@@ -505,8 +526,8 @@ int fsnotify(__u32 mask, const void *data, int data_type, struct inode *dir,
marks_mask |= mnt->mnt_fsnotify_mask; marks_mask |= mnt->mnt_fsnotify_mask;
if (inode) if (inode)
marks_mask |= inode->i_fsnotify_mask; marks_mask |= inode->i_fsnotify_mask;
if (parent) if (inode2)
marks_mask |= parent->i_fsnotify_mask; marks_mask |= inode2->i_fsnotify_mask;
/* /*
...@@ -529,9 +550,9 @@ int fsnotify(__u32 mask, const void *data, int data_type, struct inode *dir, ...@@ -529,9 +550,9 @@ int fsnotify(__u32 mask, const void *data, int data_type, struct inode *dir,
iter_info.marks[FSNOTIFY_ITER_TYPE_INODE] = iter_info.marks[FSNOTIFY_ITER_TYPE_INODE] =
fsnotify_first_mark(&inode->i_fsnotify_marks); fsnotify_first_mark(&inode->i_fsnotify_marks);
} }
if (parent) { if (inode2) {
iter_info.marks[FSNOTIFY_ITER_TYPE_PARENT] = iter_info.marks[inode2_type] =
fsnotify_first_mark(&parent->i_fsnotify_marks); fsnotify_first_mark(&inode2->i_fsnotify_marks);
} }
/* /*
......
...@@ -26,7 +26,7 @@ struct dnotify_struct { ...@@ -26,7 +26,7 @@ struct dnotify_struct {
FS_MODIFY | FS_MODIFY_CHILD |\ FS_MODIFY | FS_MODIFY_CHILD |\
FS_ACCESS | FS_ACCESS_CHILD |\ FS_ACCESS | FS_ACCESS_CHILD |\
FS_ATTRIB | FS_ATTRIB_CHILD |\ FS_ATTRIB | FS_ATTRIB_CHILD |\
FS_CREATE | FS_DN_RENAME |\ FS_CREATE | FS_RENAME |\
FS_MOVED_FROM | FS_MOVED_TO) FS_MOVED_FROM | FS_MOVED_TO)
extern int dir_notify_enable; extern int dir_notify_enable;
......
...@@ -144,16 +144,19 @@ static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir, ...@@ -144,16 +144,19 @@ static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
u32 fs_cookie = fsnotify_get_cookie(); u32 fs_cookie = fsnotify_get_cookie();
__u32 old_dir_mask = FS_MOVED_FROM; __u32 old_dir_mask = FS_MOVED_FROM;
__u32 new_dir_mask = FS_MOVED_TO; __u32 new_dir_mask = FS_MOVED_TO;
__u32 rename_mask = FS_RENAME;
const struct qstr *new_name = &moved->d_name; const struct qstr *new_name = &moved->d_name;
if (old_dir == new_dir)
old_dir_mask |= FS_DN_RENAME;
if (isdir) { if (isdir) {
old_dir_mask |= FS_ISDIR; old_dir_mask |= FS_ISDIR;
new_dir_mask |= FS_ISDIR; new_dir_mask |= FS_ISDIR;
rename_mask |= FS_ISDIR;
} }
/* Event with information about both old and new parent+name */
fsnotify_name(rename_mask, moved, FSNOTIFY_EVENT_DENTRY,
old_dir, old_name, 0);
fsnotify_name(old_dir_mask, source, FSNOTIFY_EVENT_INODE, fsnotify_name(old_dir_mask, source, FSNOTIFY_EVENT_INODE,
old_dir, old_name, fs_cookie); old_dir, old_name, fs_cookie);
fsnotify_name(new_dir_mask, source, FSNOTIFY_EVENT_INODE, fsnotify_name(new_dir_mask, source, FSNOTIFY_EVENT_INODE,
......
...@@ -63,7 +63,7 @@ ...@@ -63,7 +63,7 @@
*/ */
#define FS_EVENT_ON_CHILD 0x08000000 #define FS_EVENT_ON_CHILD 0x08000000
#define FS_DN_RENAME 0x10000000 /* file renamed */ #define FS_RENAME 0x10000000 /* File was renamed */
#define FS_DN_MULTISHOT 0x20000000 /* dnotify multishot */ #define FS_DN_MULTISHOT 0x20000000 /* dnotify multishot */
#define FS_ISDIR 0x40000000 /* event occurred against dir */ #define FS_ISDIR 0x40000000 /* event occurred against dir */
#define FS_IN_ONESHOT 0x80000000 /* only send event once */ #define FS_IN_ONESHOT 0x80000000 /* only send event once */
...@@ -76,7 +76,7 @@ ...@@ -76,7 +76,7 @@
* The watching parent may get an FS_ATTRIB|FS_EVENT_ON_CHILD event * The watching parent may get an FS_ATTRIB|FS_EVENT_ON_CHILD event
* when a directory entry inside a child subdir changes. * when a directory entry inside a child subdir changes.
*/ */
#define ALL_FSNOTIFY_DIRENT_EVENTS (FS_CREATE | FS_DELETE | FS_MOVE) #define ALL_FSNOTIFY_DIRENT_EVENTS (FS_CREATE | FS_DELETE | FS_MOVE | FS_RENAME)
#define ALL_FSNOTIFY_PERM_EVENTS (FS_OPEN_PERM | FS_ACCESS_PERM | \ #define ALL_FSNOTIFY_PERM_EVENTS (FS_OPEN_PERM | FS_ACCESS_PERM | \
FS_OPEN_EXEC_PERM) FS_OPEN_EXEC_PERM)
...@@ -101,7 +101,7 @@ ...@@ -101,7 +101,7 @@
/* Events that can be reported to backends */ /* Events that can be reported to backends */
#define ALL_FSNOTIFY_EVENTS (ALL_FSNOTIFY_DIRENT_EVENTS | \ #define ALL_FSNOTIFY_EVENTS (ALL_FSNOTIFY_DIRENT_EVENTS | \
FS_EVENTS_POSS_ON_CHILD | \ FS_EVENTS_POSS_ON_CHILD | \
FS_DELETE_SELF | FS_MOVE_SELF | FS_DN_RENAME | \ FS_DELETE_SELF | FS_MOVE_SELF | \
FS_UNMOUNT | FS_Q_OVERFLOW | FS_IN_IGNORED | \ FS_UNMOUNT | FS_Q_OVERFLOW | FS_IN_IGNORED | \
FS_ERROR) FS_ERROR)
...@@ -349,6 +349,7 @@ enum fsnotify_iter_type { ...@@ -349,6 +349,7 @@ enum fsnotify_iter_type {
FSNOTIFY_ITER_TYPE_VFSMOUNT, FSNOTIFY_ITER_TYPE_VFSMOUNT,
FSNOTIFY_ITER_TYPE_SB, FSNOTIFY_ITER_TYPE_SB,
FSNOTIFY_ITER_TYPE_PARENT, FSNOTIFY_ITER_TYPE_PARENT,
FSNOTIFY_ITER_TYPE_INODE2,
FSNOTIFY_ITER_TYPE_COUNT FSNOTIFY_ITER_TYPE_COUNT
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment