Skip to content

Commit 7e8283a

Browse files
amir73iljankara
authored andcommitted
fanotify: report parent fid + name + child fid
For a group with fanotify_init() flag FAN_REPORT_DFID_NAME, the parent fid and name are reported for events on non-directory objects with an info record of type FAN_EVENT_INFO_TYPE_DFID_NAME. If the group also has the init flag FAN_REPORT_FID, the child fid is also reported with another info record that follows the first info record. The second info record is the same info record that would have been reported to a group with only FAN_REPORT_FID flag. When the child fid needs to be recorded, the variable size struct fanotify_name_event is preallocated with enough space to store the child fh between the dir fh and the name. Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Amir Goldstein <[email protected]> Signed-off-by: Jan Kara <[email protected]>
1 parent 929943b commit 7e8283a

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

fs/notify/fanotify/fanotify.c

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,15 +477,19 @@ static struct fanotify_event *fanotify_alloc_fid_event(struct inode *id,
477477
static struct fanotify_event *fanotify_alloc_name_event(struct inode *id,
478478
__kernel_fsid_t *fsid,
479479
const struct qstr *file_name,
480+
struct inode *child,
480481
gfp_t gfp)
481482
{
482483
struct fanotify_name_event *fne;
483484
struct fanotify_info *info;
484-
struct fanotify_fh *dfh;
485+
struct fanotify_fh *dfh, *ffh;
485486
unsigned int dir_fh_len = fanotify_encode_fh_len(id);
487+
unsigned int child_fh_len = fanotify_encode_fh_len(child);
486488
unsigned int size;
487489

488490
size = sizeof(*fne) + FANOTIFY_FH_HDR_LEN + dir_fh_len;
491+
if (child_fh_len)
492+
size += FANOTIFY_FH_HDR_LEN + child_fh_len;
489493
if (file_name)
490494
size += file_name->len + 1;
491495
fne = kmalloc(size, gfp);
@@ -498,11 +502,15 @@ static struct fanotify_event *fanotify_alloc_name_event(struct inode *id,
498502
fanotify_info_init(info);
499503
dfh = fanotify_info_dir_fh(info);
500504
info->dir_fh_totlen = fanotify_encode_fh(dfh, id, dir_fh_len, 0);
505+
if (child_fh_len) {
506+
ffh = fanotify_info_file_fh(info);
507+
info->file_fh_totlen = fanotify_encode_fh(ffh, child, child_fh_len, 0);
508+
}
501509
if (file_name)
502510
fanotify_info_copy_name(info, file_name);
503511

504-
pr_debug("%s: ino=%lu size=%u dir_fh_len=%u name_len=%u name='%.*s'\n",
505-
__func__, id->i_ino, size, dir_fh_len,
512+
pr_debug("%s: ino=%lu size=%u dir_fh_len=%u child_fh_len=%u name_len=%u name='%.*s'\n",
513+
__func__, id->i_ino, size, dir_fh_len, child_fh_len,
506514
info->name_len, info->name_len, fanotify_info_name(info));
507515

508516
return &fne->fae;
@@ -520,9 +528,19 @@ static struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group,
520528
struct inode *dirid = fanotify_dfid_inode(mask, data, data_type, dir);
521529
const struct path *path = fsnotify_data_path(data, data_type);
522530
unsigned int fid_mode = FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS);
531+
struct inode *child = NULL;
523532
bool name_event = false;
524533

525534
if ((fid_mode & FAN_REPORT_DIR_FID) && dirid) {
535+
/*
536+
* With both flags FAN_REPORT_DIR_FID and FAN_REPORT_FID, we
537+
* report the child fid for events reported on a non-dir child
538+
* in addition to reporting the parent fid and child name.
539+
*/
540+
if ((fid_mode & FAN_REPORT_FID) &&
541+
id != dirid && !(mask & FAN_ONDIR))
542+
child = id;
543+
526544
id = dirid;
527545

528546
/*
@@ -558,7 +576,8 @@ static struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group,
558576
if (fanotify_is_perm_event(mask)) {
559577
event = fanotify_alloc_perm_event(path, gfp);
560578
} else if (name_event && file_name) {
561-
event = fanotify_alloc_name_event(id, fsid, file_name, gfp);
579+
event = fanotify_alloc_name_event(id, fsid, file_name, child,
580+
gfp);
562581
} else if (fid_mode) {
563582
event = fanotify_alloc_fid_event(id, fsid, gfp);
564583
} else {

fs/notify/fanotify/fanotify.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ static inline struct fanotify_fh *fanotify_event_object_fh(
193193
{
194194
if (event->type == FANOTIFY_EVENT_TYPE_FID)
195195
return &FANOTIFY_FE(event)->object_fh;
196+
else if (event->type == FANOTIFY_EVENT_TYPE_FID_NAME)
197+
return fanotify_info_file_fh(&FANOTIFY_NE(event)->info);
196198
else
197199
return NULL;
198200
}
@@ -208,9 +210,13 @@ static inline struct fanotify_info *fanotify_event_info(
208210

209211
static inline int fanotify_event_object_fh_len(struct fanotify_event *event)
210212
{
213+
struct fanotify_info *info = fanotify_event_info(event);
211214
struct fanotify_fh *fh = fanotify_event_object_fh(event);
212215

213-
return fh ? fh->len : 0;
216+
if (info)
217+
return info->file_fh_totlen ? fh->len : 0;
218+
else
219+
return fh ? fh->len : 0;
214220
}
215221

216222
static inline int fanotify_event_dir_fh_len(struct fanotify_event *event)

fs/notify/fanotify/fanotify_user.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,14 +956,15 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
956956
return -EINVAL;
957957

958958
/*
959-
* Reporting either object fid or dir fid.
960959
* 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.
961961
*/
962962
switch (fid_mode) {
963963
case 0:
964964
case FAN_REPORT_FID:
965965
case FAN_REPORT_DIR_FID:
966966
case FAN_REPORT_DFID_NAME:
967+
case FAN_REPORT_DFID_NAME | FAN_REPORT_FID:
967968
break;
968969
default:
969970
return -EINVAL;

0 commit comments

Comments
 (0)