Skip to content

Commit 691d976

Browse files
amir73iljankara
authored andcommitted
fanotify: report parent fid + child fid
Add support for FAN_REPORT_FID | FAN_REPORT_DIR_FID. Internally, it is implemented as a private case of reporting both parent and child fids and name, the parent and child fids are recorded in a variable length fanotify_name_event, but there is no name. It should be noted that directory modification events are recorded in fixed size fanotify_fid_event when not reporting name, just like with group flags FAN_REPORT_FID. Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Amir Goldstein <[email protected]> Signed-off-by: Jan Kara <[email protected]>
1 parent 7e8283a commit 691d976

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

fs/notify/fanotify/fanotify.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ static struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group,
535535
/*
536536
* With both flags FAN_REPORT_DIR_FID and FAN_REPORT_FID, we
537537
* report the child fid for events reported on a non-dir child
538-
* in addition to reporting the parent fid and child name.
538+
* in addition to reporting the parent fid and maybe child name.
539539
*/
540540
if ((fid_mode & FAN_REPORT_FID) &&
541541
id != dirid && !(mask & FAN_ONDIR))
@@ -552,11 +552,17 @@ static struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group,
552552
*
553553
* For event on non-directory that is reported to parent, we
554554
* record the fid of the parent and the name of the child.
555+
*
556+
* Even if not reporting name, we need a variable length
557+
* fanotify_name_event if reporting both parent and child fids.
555558
*/
556-
if ((fid_mode & FAN_REPORT_NAME) &&
557-
((mask & ALL_FSNOTIFY_DIRENT_EVENTS) ||
558-
!(mask & FAN_ONDIR)))
559+
if (!(fid_mode & FAN_REPORT_NAME)) {
560+
name_event = !!child;
561+
file_name = NULL;
562+
} else if ((mask & ALL_FSNOTIFY_DIRENT_EVENTS) ||
563+
!(mask & FAN_ONDIR)) {
559564
name_event = true;
565+
}
560566
}
561567

562568
/*
@@ -575,7 +581,7 @@ static struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group,
575581

576582
if (fanotify_is_perm_event(mask)) {
577583
event = fanotify_alloc_perm_event(path, gfp);
578-
} else if (name_event && file_name) {
584+
} else if (name_event && (file_name || child)) {
579585
event = fanotify_alloc_name_event(id, fsid, file_name, child,
580586
gfp);
581587
} else if (fid_mode) {

fs/notify/fanotify/fanotify_user.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,8 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group,
371371

372372
/* Event info records order is: dir fid + name, child fid */
373373
if (fanotify_event_dir_fh_len(event)) {
374-
info_type = FAN_EVENT_INFO_TYPE_DFID_NAME;
374+
info_type = info->name_len ? FAN_EVENT_INFO_TYPE_DFID_NAME :
375+
FAN_EVENT_INFO_TYPE_DFID;
375376
ret = copy_info_to_user(fanotify_event_fsid(event),
376377
fanotify_info_dir_fh(info),
377378
info_type, fanotify_info_name(info),
@@ -957,18 +958,10 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
957958

958959
/*
959960
* Child name is reported with parent fid so requires dir fid.
960-
* If reporting child name, we can report both child fid and dir fid.
961+
* We can report both child fid and dir fid with or without name.
961962
*/
962-
switch (fid_mode) {
963-
case 0:
964-
case FAN_REPORT_FID:
965-
case FAN_REPORT_DIR_FID:
966-
case FAN_REPORT_DFID_NAME:
967-
case FAN_REPORT_DFID_NAME | FAN_REPORT_FID:
968-
break;
969-
default:
963+
if ((fid_mode & FAN_REPORT_NAME) && !(fid_mode & FAN_REPORT_DIR_FID))
970964
return -EINVAL;
971-
}
972965

973966
user = get_current_user();
974967
if (atomic_read(&user->fanotify_listeners) > FANOTIFY_DEFAULT_MAX_LISTENERS) {

0 commit comments

Comments
 (0)