Skip to content

Commit

Permalink
fanotify: factor out helper fanotify_mark_update_flags()
Browse files Browse the repository at this point in the history
Handle FAN_MARK_IGNORED_SURV_MODIFY flag change in a helper that
is called after updating the mark mask.

Move recalc of object mask inside fanotify_mark_add_to_mask() which
makes the code a bit simpler to follow.

Signed-off-by: Amir Goldstein <amir73il@gmail.com>
  • Loading branch information
amir73il committed Mar 20, 2022
1 parent 40822e8 commit 068909e
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions fs/notify/fanotify/fanotify_user.c
Expand Up @@ -1081,42 +1081,50 @@ static int fanotify_remove_inode_mark(struct fsnotify_group *group,
flags, umask);
}

static void fanotify_mark_add_ignored_mask(struct fsnotify_mark *fsn_mark,
__u32 mask, unsigned int flags,
__u32 *removed)
static int fanotify_mark_update_flags(struct fsnotify_mark *fsn_mark,
unsigned int flags, bool *recalc)
{
fsn_mark->ignored_mask |= mask;

/*
* Setting FAN_MARK_IGNORED_SURV_MODIFY for the first time may lead to
* the removal of the FS_MODIFY bit in calculated mask if it was set
* because of an ignored mask that is now going to survive FS_MODIFY.
*/
if ((flags & FAN_MARK_IGNORED_SURV_MODIFY) &&
(flags & FAN_MARK_IGNORED_MASK) &&
!(fsn_mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)) {
fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
if (!(fsn_mark->mask & FS_MODIFY))
*removed = FS_MODIFY;
*recalc = true;
}

return 0;
}

static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
__u32 mask, unsigned int flags,
__u32 *removed)
static int fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark, __u32 mask,
unsigned int flags)
{
__u32 oldmask, newmask;
__u32 oldmask;
bool recalc = false;
int ret;

spin_lock(&fsn_mark->lock);
oldmask = fsnotify_calc_mask(fsn_mark);
if (!(flags & FAN_MARK_IGNORED_MASK)) {
fsn_mark->mask |= mask;
} else {
fanotify_mark_add_ignored_mask(fsn_mark, mask, flags, removed);
fsn_mark->ignored_mask |= mask;
}
newmask = fsnotify_calc_mask(fsn_mark);

recalc = fsnotify_calc_mask(fsn_mark) & ~oldmask &
~fsnotify_conn_mask(fsn_mark->connector);

ret = fanotify_mark_update_flags(fsn_mark, flags, &recalc);
spin_unlock(&fsn_mark->lock);

return newmask & ~oldmask;
if (recalc)
fsnotify_recalc_mask(fsn_mark->connector);

return ret;
}

static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
Expand Down Expand Up @@ -1175,7 +1183,6 @@ static int fanotify_add_mark(struct fsnotify_group *group,
{
struct fsnotify_mark *fsn_mark = NULL;
void *prealloc_conn = NULL, *prealloc_mark = NULL;
__u32 added, removed = 0;
int ret = -ENOMEM;

fsn_mark = fsnotify_find_mark(connp, group);
Expand Down Expand Up @@ -1217,9 +1224,7 @@ static int fanotify_add_mark(struct fsnotify_group *group,
goto out_unlock;
}

added = fanotify_mark_add_to_mask(fsn_mark, mask, flags, &removed);
if (removed || (added & ~fsnotify_conn_mask(fsn_mark->connector)))
fsnotify_recalc_mask(fsn_mark->connector);
ret = fanotify_mark_add_to_mask(fsn_mark, mask, flags);

out_unlock:
mutex_unlock(&group->mark_mutex);
Expand Down

0 comments on commit 068909e

Please sign in to comment.