Commit 88826276 authored by Eric Paris's avatar Eric Paris

fanotify: infrastructure to add an remove marks on vfsmounts

infrastructure work to add and remove marks on vfsmounts.  This should get
every set up except wiring the functions to the syscalls.
Signed-off-by: default avatarEric Paris <eparis@redhat.com>
parent 1c529063
...@@ -295,31 +295,83 @@ static int fanotify_find_path(int dfd, const char __user *filename, ...@@ -295,31 +295,83 @@ static int fanotify_find_path(int dfd, const char __user *filename,
return ret; return ret;
} }
static int fanotify_remove_mark(struct fsnotify_group *group, static void fanotify_update_object_mask(struct fsnotify_group *group,
struct inode *inode, struct inode *inode,
__u32 mask) struct vfsmount *mnt,
struct fsnotify_mark *fsn_mark,
unsigned int flags,
__u32 mask)
{ {
struct fsnotify_mark *fsn_mark; __u32 old_mask, new_mask;
__u32 new_mask;
pr_debug("%s: group=%p inode=%p mask=%x\n", __func__,
group, inode, mask);
fsn_mark = fsnotify_find_inode_mark(group, inode); pr_debug("%s: group=%p inode=%p mnt=%p fsn_mark=%p flags=%x mask=%x\n",
if (!fsn_mark) __func__, group, inode, mnt, fsn_mark, flags, mask);
return -ENOENT;
spin_lock(&fsn_mark->lock); spin_lock(&fsn_mark->lock);
fsn_mark->mask &= ~mask; old_mask = fsn_mark->mask;
if (flags & FAN_MARK_ADD)
fsn_mark->mask |= mask;
else if (flags & FAN_MARK_REMOVE)
fsn_mark->mask &= ~mask;
else
BUG();
new_mask = fsn_mark->mask; new_mask = fsn_mark->mask;
spin_unlock(&fsn_mark->lock); spin_unlock(&fsn_mark->lock);
if (!new_mask) if (!new_mask)
fsnotify_destroy_mark(fsn_mark); fsnotify_destroy_mark(fsn_mark);
/* we made changes to a mask, update the group mask and the object mask
* so things happen quickly. */
if (old_mask != new_mask) {
__u32 dropped, do_object, do_group;
/* more bits in old than in new? */
dropped = (old_mask & ~new_mask);
/* more bits in this fsn_mark than the group? */
do_group = (new_mask & ~group->mask);
if (inode) {
/* more bits in this fsn_mark than the object's mask? */
do_object = (new_mask & ~inode->i_fsnotify_mask);
/* update the object with this new fsn_mark */
if (dropped || do_object)
fsnotify_recalc_inode_mask(inode);
} else if (mnt) {
/* more bits in this fsn_mark than the object's mask? */
do_object = (new_mask & ~mnt->mnt_fsnotify_mask);
/* update the object with this new fsn_mark */
if (dropped || do_object)
fsnotify_recalc_vfsmount_mask(mnt);
} else {
BUG();
}
/* update the group mask with the new mask */
if (dropped || do_group)
fsnotify_recalc_group_mask(group);
}
}
static int fanotify_remove_mark(struct fsnotify_group *group, struct inode *inode,
struct vfsmount *mnt, unsigned int flags, __u32 mask)
{
struct fsnotify_mark *fsn_mark = NULL;
BUG_ON(inode && mnt);
BUG_ON(!inode && !mnt);
if (inode)
fsn_mark = fsnotify_find_inode_mark(group, inode);
else if (mnt)
fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
else else
fsnotify_recalc_inode_mask(inode); BUG();
fsnotify_recalc_group_mask(group); if (!fsn_mark)
return -ENOENT;
fanotify_update_object_mask(group, inode, mnt, fsn_mark, flags, mask);
/* matches the fsnotify_find_inode_mark() */ /* matches the fsnotify_find_inode_mark() */
fsnotify_put_mark(fsn_mark); fsnotify_put_mark(fsn_mark);
...@@ -327,22 +379,48 @@ static int fanotify_remove_mark(struct fsnotify_group *group, ...@@ -327,22 +379,48 @@ static int fanotify_remove_mark(struct fsnotify_group *group,
return 0; return 0;
} }
static int fanotify_add_mark(struct fsnotify_group *group, static struct fsnotify_mark *fanotify_add_vfsmount_mark(struct fsnotify_group *group,
struct inode *inode, struct vfsmount *mnt)
__u32 mask)
{ {
struct fsnotify_mark *fsn_mark; struct fsnotify_mark *fsn_mark;
__u32 old_mask, new_mask;
int ret;
pr_debug("%s: group=%p inode=%p mask=%x\n", __func__, fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
group, inode, mask); if (!fsn_mark) {
struct fsnotify_mark *new_fsn_mark;
int ret;
fsn_mark = ERR_PTR(-ENOMEM);
new_fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
if (!new_fsn_mark)
goto out;
fsnotify_init_mark(new_fsn_mark, fanotify_free_mark);
ret = fsnotify_add_mark(new_fsn_mark, group, NULL, mnt, 0);
if (ret) {
fsn_mark = ERR_PTR(ret);
fanotify_free_mark(new_fsn_mark);
goto out;
}
fsn_mark = new_fsn_mark;
}
out:
return fsn_mark;
}
static struct fsnotify_mark *fanotify_add_inode_mark(struct fsnotify_group *group,
struct inode *inode)
{
struct fsnotify_mark *fsn_mark;
pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
fsn_mark = fsnotify_find_inode_mark(group, inode); fsn_mark = fsnotify_find_inode_mark(group, inode);
if (!fsn_mark) { if (!fsn_mark) {
struct fsnotify_mark *new_fsn_mark; struct fsnotify_mark *new_fsn_mark;
int ret;
ret = -ENOMEM; fsn_mark = ERR_PTR(-ENOMEM);
new_fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL); new_fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
if (!new_fsn_mark) if (!new_fsn_mark)
goto out; goto out;
...@@ -350,57 +428,60 @@ static int fanotify_add_mark(struct fsnotify_group *group, ...@@ -350,57 +428,60 @@ static int fanotify_add_mark(struct fsnotify_group *group,
fsnotify_init_mark(new_fsn_mark, fanotify_free_mark); fsnotify_init_mark(new_fsn_mark, fanotify_free_mark);
ret = fsnotify_add_mark(new_fsn_mark, group, inode, NULL, 0); ret = fsnotify_add_mark(new_fsn_mark, group, inode, NULL, 0);
if (ret) { if (ret) {
fsn_mark = ERR_PTR(ret);
fanotify_free_mark(new_fsn_mark); fanotify_free_mark(new_fsn_mark);
goto out; goto out;
} }
fsn_mark = new_fsn_mark; fsn_mark = new_fsn_mark;
} }
out:
return fsn_mark;
}
ret = 0; static int fanotify_add_mark(struct fsnotify_group *group, struct inode *inode,
struct vfsmount *mnt, unsigned int flags, __u32 mask)
{
struct fsnotify_mark *fsn_mark;
spin_lock(&fsn_mark->lock); pr_debug("%s: group=%p inode=%p mnt=%p flags=%x mask=%x\n",
old_mask = fsn_mark->mask; __func__, group, inode, mnt, flags, mask);
fsn_mark->mask |= mask;
new_mask = fsn_mark->mask;
spin_unlock(&fsn_mark->lock);
/* we made changes to a mask, update the group mask and the inode mask BUG_ON(inode && mnt);
* so things happen quickly. */ BUG_ON(!inode && !mnt);
if (old_mask != new_mask) {
/* more bits in old than in new? */
int dropped = (old_mask & ~new_mask);
/* more bits in this mark than the inode's mask? */
int do_inode = (new_mask & ~inode->i_fsnotify_mask);
/* more bits in this mark than the group? */
int do_group = (new_mask & ~group->mask);
/* update the inode with this new mark */ if (inode)
if (dropped || do_inode) fsn_mark = fanotify_add_inode_mark(group, inode);
fsnotify_recalc_inode_mask(inode); else if (mnt)
fsn_mark = fanotify_add_vfsmount_mark(group, mnt);
else
BUG();
/* update the group mask with the new mask */ if (IS_ERR(fsn_mark))
if (dropped || do_group) goto out;
fsnotify_recalc_group_mask(group);
} fanotify_update_object_mask(group, inode, mnt, fsn_mark, flags, mask);
/* match the init or the find.... */ /* match the init or the find.... */
fsnotify_put_mark(fsn_mark); fsnotify_put_mark(fsn_mark);
out: out:
return ret; return PTR_ERR(fsn_mark);
} }
static int fanotify_update_mark(struct fsnotify_group *group, static int fanotify_update_mark(struct fsnotify_group *group,
struct inode *inode, int flags, struct inode *inode, struct vfsmount *mnt,
__u32 mask) int flags, __u32 mask)
{ {
pr_debug("%s: group=%p inode=%p flags=%x mask=%x\n", __func__, pr_debug("%s: group=%p inode=%p mnt=%p flags=%x mask=%x\n",
group, inode, flags, mask); __func__, group, inode, mnt, flags, mask);
BUG_ON(inode && mnt);
BUG_ON(!inode && !mnt);
if (flags & FAN_MARK_ADD) if (flags & FAN_MARK_ADD)
fanotify_add_mark(group, inode, mask); fanotify_add_mark(group, inode, mnt, flags, mask);
else if (flags & FAN_MARK_REMOVE) else if (flags & FAN_MARK_REMOVE)
fanotify_remove_mark(group, inode, mask); fanotify_remove_mark(group, inode, mnt, flags, mask);
else else
BUG(); BUG();
...@@ -502,7 +583,7 @@ SYSCALL_DEFINE(fanotify_mark)(int fanotify_fd, unsigned int flags, ...@@ -502,7 +583,7 @@ SYSCALL_DEFINE(fanotify_mark)(int fanotify_fd, unsigned int flags,
group = filp->private_data; group = filp->private_data;
/* create/update an inode mark */ /* create/update an inode mark */
ret = fanotify_update_mark(group, inode, flags, mask); ret = fanotify_update_mark(group, inode, NULL, flags, mask);
path_put(&path); path_put(&path);
fput_and_out: fput_and_out:
......
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