Skip to content

Commit eaa2c6b

Browse files
amir73iljankara
authored andcommitted
fanotify: factor out helpers to add/remove mark
Factor out helpers fanotify_add_mark() and fanotify_remove_mark() to reduce duplicated code. Signed-off-by: Amir Goldstein <amir73il@gmail.com> Signed-off-by: Jan Kara <jack@suse.cz>
1 parent 3ac70bf commit eaa2c6b

File tree

1 file changed

+29
-57
lines changed

1 file changed

+29
-57
lines changed

fs/notify/fanotify/fanotify_user.c

Lines changed: 29 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -524,17 +524,16 @@ static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
524524
return mask & oldmask;
525525
}
526526

527-
static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
528-
struct vfsmount *mnt, __u32 mask,
529-
unsigned int flags)
527+
static int fanotify_remove_mark(struct fsnotify_group *group,
528+
fsnotify_connp_t *connp, __u32 mask,
529+
unsigned int flags)
530530
{
531531
struct fsnotify_mark *fsn_mark = NULL;
532532
__u32 removed;
533533
int destroy_mark;
534534

535535
mutex_lock(&group->mark_mutex);
536-
fsn_mark = fsnotify_find_mark(&real_mount(mnt)->mnt_fsnotify_marks,
537-
group);
536+
fsn_mark = fsnotify_find_mark(connp, group);
538537
if (!fsn_mark) {
539538
mutex_unlock(&group->mark_mutex);
540539
return -ENOENT;
@@ -550,39 +549,25 @@ static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
550549
if (destroy_mark)
551550
fsnotify_free_mark(fsn_mark);
552551

552+
/* matches the fsnotify_find_mark() */
553553
fsnotify_put_mark(fsn_mark);
554554
return 0;
555555
}
556556

557+
static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
558+
struct vfsmount *mnt, __u32 mask,
559+
unsigned int flags)
560+
{
561+
return fanotify_remove_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
562+
mask, flags);
563+
}
564+
557565
static int fanotify_remove_inode_mark(struct fsnotify_group *group,
558566
struct inode *inode, __u32 mask,
559567
unsigned int flags)
560568
{
561-
struct fsnotify_mark *fsn_mark = NULL;
562-
__u32 removed;
563-
int destroy_mark;
564-
565-
mutex_lock(&group->mark_mutex);
566-
fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
567-
if (!fsn_mark) {
568-
mutex_unlock(&group->mark_mutex);
569-
return -ENOENT;
570-
}
571-
572-
removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
573-
&destroy_mark);
574-
if (removed & fsnotify_conn_mask(fsn_mark->connector))
575-
fsnotify_recalc_mask(fsn_mark->connector);
576-
if (destroy_mark)
577-
fsnotify_detach_mark(fsn_mark);
578-
mutex_unlock(&group->mark_mutex);
579-
if (destroy_mark)
580-
fsnotify_free_mark(fsn_mark);
581-
582-
/* matches the fsnotify_find_mark() */
583-
fsnotify_put_mark(fsn_mark);
584-
585-
return 0;
569+
return fanotify_remove_mark(group, &inode->i_fsnotify_marks, mask,
570+
flags);
586571
}
587572

588573
static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
@@ -639,19 +624,17 @@ static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
639624
}
640625

641626

642-
static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
643-
struct vfsmount *mnt, __u32 mask,
644-
unsigned int flags)
627+
static int fanotify_add_mark(struct fsnotify_group *group,
628+
fsnotify_connp_t *connp, unsigned int type,
629+
__u32 mask, unsigned int flags)
645630
{
646-
fsnotify_connp_t *connp = &real_mount(mnt)->mnt_fsnotify_marks;
647631
struct fsnotify_mark *fsn_mark;
648632
__u32 added;
649633

650634
mutex_lock(&group->mark_mutex);
651635
fsn_mark = fsnotify_find_mark(connp, group);
652636
if (!fsn_mark) {
653-
fsn_mark = fanotify_add_new_mark(group, connp,
654-
FSNOTIFY_OBJ_TYPE_VFSMOUNT);
637+
fsn_mark = fanotify_add_new_mark(group, connp, type);
655638
if (IS_ERR(fsn_mark)) {
656639
mutex_unlock(&group->mark_mutex);
657640
return PTR_ERR(fsn_mark);
@@ -666,14 +649,18 @@ static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
666649
return 0;
667650
}
668651

652+
static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
653+
struct vfsmount *mnt, __u32 mask,
654+
unsigned int flags)
655+
{
656+
return fanotify_add_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
657+
FSNOTIFY_OBJ_TYPE_VFSMOUNT, mask, flags);
658+
}
659+
669660
static int fanotify_add_inode_mark(struct fsnotify_group *group,
670661
struct inode *inode, __u32 mask,
671662
unsigned int flags)
672663
{
673-
fsnotify_connp_t *connp = &inode->i_fsnotify_marks;
674-
struct fsnotify_mark *fsn_mark;
675-
__u32 added;
676-
677664
pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
678665

679666
/*
@@ -686,23 +673,8 @@ static int fanotify_add_inode_mark(struct fsnotify_group *group,
686673
(atomic_read(&inode->i_writecount) > 0))
687674
return 0;
688675

689-
mutex_lock(&group->mark_mutex);
690-
fsn_mark = fsnotify_find_mark(connp, group);
691-
if (!fsn_mark) {
692-
fsn_mark = fanotify_add_new_mark(group, connp,
693-
FSNOTIFY_OBJ_TYPE_INODE);
694-
if (IS_ERR(fsn_mark)) {
695-
mutex_unlock(&group->mark_mutex);
696-
return PTR_ERR(fsn_mark);
697-
}
698-
}
699-
added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
700-
if (added & ~fsnotify_conn_mask(fsn_mark->connector))
701-
fsnotify_recalc_mask(fsn_mark->connector);
702-
mutex_unlock(&group->mark_mutex);
703-
704-
fsnotify_put_mark(fsn_mark);
705-
return 0;
676+
return fanotify_add_mark(group, &inode->i_fsnotify_marks,
677+
FSNOTIFY_OBJ_TYPE_INODE, mask, flags);
706678
}
707679

708680
/* fanotify syscalls */

0 commit comments

Comments
 (0)