Commit 2645b9d1 authored by Linus Torvalds's avatar Linus Torvalds

Merge tag 'fsnotify_for_v4.19-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs

Pull fsnotify updates from Jan Kara:
 "fsnotify cleanups from Amir and a small inotify improvement"

* tag 'fsnotify_for_v4.19-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs:
  inotify: Add flag IN_MASK_CREATE for inotify_add_watch()
  fanotify: factor out helpers to add/remove mark
  fsnotify: add helper to get mask from connector
  fsnotify: let connector point to an abstract object
  fsnotify: pass connp and object type to fsnotify_add_mark()
  fsnotify: use typedef fsnotify_connp_t for brevity
parents 46e62a07 4d97f7d5
...@@ -524,17 +524,16 @@ static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark, ...@@ -524,17 +524,16 @@ static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
return mask & oldmask; return mask & oldmask;
} }
static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group, static int fanotify_remove_mark(struct fsnotify_group *group,
struct vfsmount *mnt, __u32 mask, fsnotify_connp_t *connp, __u32 mask,
unsigned int flags) unsigned int flags)
{ {
struct fsnotify_mark *fsn_mark = NULL; struct fsnotify_mark *fsn_mark = NULL;
__u32 removed; __u32 removed;
int destroy_mark; int destroy_mark;
mutex_lock(&group->mark_mutex); mutex_lock(&group->mark_mutex);
fsn_mark = fsnotify_find_mark(&real_mount(mnt)->mnt_fsnotify_marks, fsn_mark = fsnotify_find_mark(connp, group);
group);
if (!fsn_mark) { if (!fsn_mark) {
mutex_unlock(&group->mark_mutex); mutex_unlock(&group->mark_mutex);
return -ENOENT; return -ENOENT;
...@@ -542,47 +541,33 @@ static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group, ...@@ -542,47 +541,33 @@ static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags, removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
&destroy_mark); &destroy_mark);
if (removed & real_mount(mnt)->mnt_fsnotify_mask) if (removed & fsnotify_conn_mask(fsn_mark->connector))
fsnotify_recalc_mask(real_mount(mnt)->mnt_fsnotify_marks); fsnotify_recalc_mask(fsn_mark->connector);
if (destroy_mark) if (destroy_mark)
fsnotify_detach_mark(fsn_mark); fsnotify_detach_mark(fsn_mark);
mutex_unlock(&group->mark_mutex); mutex_unlock(&group->mark_mutex);
if (destroy_mark) if (destroy_mark)
fsnotify_free_mark(fsn_mark); fsnotify_free_mark(fsn_mark);
/* matches the fsnotify_find_mark() */
fsnotify_put_mark(fsn_mark); fsnotify_put_mark(fsn_mark);
return 0; return 0;
} }
static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
struct vfsmount *mnt, __u32 mask,
unsigned int flags)
{
return fanotify_remove_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
mask, flags);
}
static int fanotify_remove_inode_mark(struct fsnotify_group *group, static int fanotify_remove_inode_mark(struct fsnotify_group *group,
struct inode *inode, __u32 mask, struct inode *inode, __u32 mask,
unsigned int flags) unsigned int flags)
{ {
struct fsnotify_mark *fsn_mark = NULL; return fanotify_remove_mark(group, &inode->i_fsnotify_marks, mask,
__u32 removed; flags);
int destroy_mark;
mutex_lock(&group->mark_mutex);
fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
if (!fsn_mark) {
mutex_unlock(&group->mark_mutex);
return -ENOENT;
}
removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags,
&destroy_mark);
if (removed & inode->i_fsnotify_mask)
fsnotify_recalc_mask(inode->i_fsnotify_marks);
if (destroy_mark)
fsnotify_detach_mark(fsn_mark);
mutex_unlock(&group->mark_mutex);
if (destroy_mark)
fsnotify_free_mark(fsn_mark);
/* matches the fsnotify_find_mark() */
fsnotify_put_mark(fsn_mark);
return 0;
} }
static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark, static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
...@@ -615,8 +600,8 @@ static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark, ...@@ -615,8 +600,8 @@ static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
} }
static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group, static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
struct inode *inode, fsnotify_connp_t *connp,
struct vfsmount *mnt) unsigned int type)
{ {
struct fsnotify_mark *mark; struct fsnotify_mark *mark;
int ret; int ret;
...@@ -629,7 +614,7 @@ static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group, ...@@ -629,7 +614,7 @@ static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
fsnotify_init_mark(mark, group); fsnotify_init_mark(mark, group);
ret = fsnotify_add_mark_locked(mark, inode, mnt, 0); ret = fsnotify_add_mark_locked(mark, connp, type, 0);
if (ret) { if (ret) {
fsnotify_put_mark(mark); fsnotify_put_mark(mark);
return ERR_PTR(ret); return ERR_PTR(ret);
...@@ -639,39 +624,43 @@ static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group, ...@@ -639,39 +624,43 @@ static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
} }
static int fanotify_add_vfsmount_mark(struct fsnotify_group *group, static int fanotify_add_mark(struct fsnotify_group *group,
struct vfsmount *mnt, __u32 mask, fsnotify_connp_t *connp, unsigned int type,
unsigned int flags) __u32 mask, unsigned int flags)
{ {
struct fsnotify_mark *fsn_mark; struct fsnotify_mark *fsn_mark;
__u32 added; __u32 added;
mutex_lock(&group->mark_mutex); mutex_lock(&group->mark_mutex);
fsn_mark = fsnotify_find_mark(&real_mount(mnt)->mnt_fsnotify_marks, fsn_mark = fsnotify_find_mark(connp, group);
group);
if (!fsn_mark) { if (!fsn_mark) {
fsn_mark = fanotify_add_new_mark(group, NULL, mnt); fsn_mark = fanotify_add_new_mark(group, connp, type);
if (IS_ERR(fsn_mark)) { if (IS_ERR(fsn_mark)) {
mutex_unlock(&group->mark_mutex); mutex_unlock(&group->mark_mutex);
return PTR_ERR(fsn_mark); return PTR_ERR(fsn_mark);
} }
} }
added = fanotify_mark_add_to_mask(fsn_mark, mask, flags); added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
if (added & ~real_mount(mnt)->mnt_fsnotify_mask) if (added & ~fsnotify_conn_mask(fsn_mark->connector))
fsnotify_recalc_mask(real_mount(mnt)->mnt_fsnotify_marks); fsnotify_recalc_mask(fsn_mark->connector);
mutex_unlock(&group->mark_mutex); mutex_unlock(&group->mark_mutex);
fsnotify_put_mark(fsn_mark); fsnotify_put_mark(fsn_mark);
return 0; return 0;
} }
static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
struct vfsmount *mnt, __u32 mask,
unsigned int flags)
{
return fanotify_add_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
FSNOTIFY_OBJ_TYPE_VFSMOUNT, mask, flags);
}
static int fanotify_add_inode_mark(struct fsnotify_group *group, static int fanotify_add_inode_mark(struct fsnotify_group *group,
struct inode *inode, __u32 mask, struct inode *inode, __u32 mask,
unsigned int flags) unsigned int flags)
{ {
struct fsnotify_mark *fsn_mark;
__u32 added;
pr_debug("%s: group=%p inode=%p\n", __func__, group, inode); pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
/* /*
...@@ -684,22 +673,8 @@ static int fanotify_add_inode_mark(struct fsnotify_group *group, ...@@ -684,22 +673,8 @@ static int fanotify_add_inode_mark(struct fsnotify_group *group,
(atomic_read(&inode->i_writecount) > 0)) (atomic_read(&inode->i_writecount) > 0))
return 0; return 0;
mutex_lock(&group->mark_mutex); return fanotify_add_mark(group, &inode->i_fsnotify_marks,
fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group); FSNOTIFY_OBJ_TYPE_INODE, mask, flags);
if (!fsn_mark) {
fsn_mark = fanotify_add_new_mark(group, inode, NULL);
if (IS_ERR(fsn_mark)) {
mutex_unlock(&group->mark_mutex);
return PTR_ERR(fsn_mark);
}
}
added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
if (added & ~inode->i_fsnotify_mask)
fsnotify_recalc_mask(inode->i_fsnotify_marks);
mutex_unlock(&group->mark_mutex);
fsnotify_put_mark(fsn_mark);
return 0;
} }
/* fanotify syscalls */ /* fanotify syscalls */
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
#include <linux/exportfs.h> #include <linux/exportfs.h>
#include "inotify/inotify.h" #include "inotify/inotify.h"
#include "../fs/mount.h" #include "fsnotify.h"
#if defined(CONFIG_PROC_FS) #if defined(CONFIG_PROC_FS)
...@@ -81,7 +81,7 @@ static void inotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark) ...@@ -81,7 +81,7 @@ static void inotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark)
return; return;
inode_mark = container_of(mark, struct inotify_inode_mark, fsn_mark); inode_mark = container_of(mark, struct inotify_inode_mark, fsn_mark);
inode = igrab(mark->connector->inode); inode = igrab(fsnotify_conn_inode(mark->connector));
if (inode) { if (inode) {
/* /*
* IN_ALL_EVENTS represents all of the mask bits * IN_ALL_EVENTS represents all of the mask bits
...@@ -117,7 +117,7 @@ static void fanotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark) ...@@ -117,7 +117,7 @@ static void fanotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark)
mflags |= FAN_MARK_IGNORED_SURV_MODIFY; mflags |= FAN_MARK_IGNORED_SURV_MODIFY;
if (mark->connector->type == FSNOTIFY_OBJ_TYPE_INODE) { if (mark->connector->type == FSNOTIFY_OBJ_TYPE_INODE) {
inode = igrab(mark->connector->inode); inode = igrab(fsnotify_conn_inode(mark->connector));
if (!inode) if (!inode)
return; return;
seq_printf(m, "fanotify ino:%lx sdev:%x mflags:%x mask:%x ignored_mask:%x ", seq_printf(m, "fanotify ino:%lx sdev:%x mflags:%x mask:%x ignored_mask:%x ",
...@@ -127,7 +127,7 @@ static void fanotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark) ...@@ -127,7 +127,7 @@ static void fanotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark)
seq_putc(m, '\n'); seq_putc(m, '\n');
iput(inode); iput(inode);
} else if (mark->connector->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT) { } else if (mark->connector->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT) {
struct mount *mnt = real_mount(mark->connector->mnt); struct mount *mnt = fsnotify_conn_mount(mark->connector);
seq_printf(m, "fanotify mnt_id:%x mflags:%x mask:%x ignored_mask:%x\n", seq_printf(m, "fanotify mnt_id:%x mflags:%x mask:%x ignored_mask:%x\n",
mnt->mnt_id, mflags, mark->mask, mark->ignored_mask); mnt->mnt_id, mflags, mark->mask, mark->ignored_mask);
......
...@@ -9,6 +9,18 @@ ...@@ -9,6 +9,18 @@
#include "../mount.h" #include "../mount.h"
static inline struct inode *fsnotify_conn_inode(
struct fsnotify_mark_connector *conn)
{
return container_of(conn->obj, struct inode, i_fsnotify_marks);
}
static inline struct mount *fsnotify_conn_mount(
struct fsnotify_mark_connector *conn)
{
return container_of(conn->obj, struct mount, mnt_fsnotify_marks);
}
/* destroy all events sitting in this groups notification queue */ /* destroy all events sitting in this groups notification queue */
extern void fsnotify_flush_notify(struct fsnotify_group *group); extern void fsnotify_flush_notify(struct fsnotify_group *group);
...@@ -19,8 +31,8 @@ extern struct srcu_struct fsnotify_mark_srcu; ...@@ -19,8 +31,8 @@ extern struct srcu_struct fsnotify_mark_srcu;
extern int fsnotify_compare_groups(struct fsnotify_group *a, extern int fsnotify_compare_groups(struct fsnotify_group *a,
struct fsnotify_group *b); struct fsnotify_group *b);
/* Destroy all marks connected via given connector */ /* Destroy all marks attached to an object via connector */
extern void fsnotify_destroy_marks(struct fsnotify_mark_connector __rcu **connp); extern void fsnotify_destroy_marks(fsnotify_connp_t *connp);
/* run the list of all marks associated with inode and destroy them */ /* run the list of all marks associated with inode and destroy them */
static inline void fsnotify_clear_marks_by_inode(struct inode *inode) static inline void fsnotify_clear_marks_by_inode(struct inode *inode)
{ {
......
...@@ -510,6 +510,7 @@ static int inotify_update_existing_watch(struct fsnotify_group *group, ...@@ -510,6 +510,7 @@ static int inotify_update_existing_watch(struct fsnotify_group *group,
__u32 old_mask, new_mask; __u32 old_mask, new_mask;
__u32 mask; __u32 mask;
int add = (arg & IN_MASK_ADD); int add = (arg & IN_MASK_ADD);
int create = (arg & IN_MASK_CREATE);
int ret; int ret;
mask = inotify_arg_to_mask(arg); mask = inotify_arg_to_mask(arg);
...@@ -517,6 +518,8 @@ static int inotify_update_existing_watch(struct fsnotify_group *group, ...@@ -517,6 +518,8 @@ static int inotify_update_existing_watch(struct fsnotify_group *group,
fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group); fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
if (!fsn_mark) if (!fsn_mark)
return -ENOENT; return -ENOENT;
else if (create)
return -EEXIST;
i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark); i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
...@@ -718,6 +721,10 @@ SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname, ...@@ -718,6 +721,10 @@ SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname,
if (unlikely(!f.file)) if (unlikely(!f.file))
return -EBADF; return -EBADF;
/* IN_MASK_ADD and IN_MASK_CREATE don't make sense together */
if (unlikely((mask & IN_MASK_ADD) && (mask & IN_MASK_CREATE)))
return -EINVAL;
/* verify that this is indeed an inotify instance */ /* verify that this is indeed an inotify instance */
if (unlikely(f.file->f_op != &inotify_fops)) { if (unlikely(f.file->f_op != &inotify_fops)) {
ret = -EINVAL; ret = -EINVAL;
...@@ -806,7 +813,7 @@ static int __init inotify_user_setup(void) ...@@ -806,7 +813,7 @@ static int __init inotify_user_setup(void)
BUILD_BUG_ON(IN_ISDIR != FS_ISDIR); BUILD_BUG_ON(IN_ISDIR != FS_ISDIR);
BUILD_BUG_ON(IN_ONESHOT != FS_IN_ONESHOT); BUILD_BUG_ON(IN_ONESHOT != FS_IN_ONESHOT);
BUG_ON(hweight32(ALL_INOTIFY_BITS) != 21); BUG_ON(hweight32(ALL_INOTIFY_BITS) != 22);
inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark, SLAB_PANIC); inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark, SLAB_PANIC);
......
...@@ -109,6 +109,23 @@ void fsnotify_get_mark(struct fsnotify_mark *mark) ...@@ -109,6 +109,23 @@ void fsnotify_get_mark(struct fsnotify_mark *mark)
refcount_inc(&mark->refcnt); refcount_inc(&mark->refcnt);
} }
static __u32 *fsnotify_conn_mask_p(struct fsnotify_mark_connector *conn)
{
if (conn->type == FSNOTIFY_OBJ_TYPE_INODE)
return &fsnotify_conn_inode(conn)->i_fsnotify_mask;
else if (conn->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT)
return &fsnotify_conn_mount(conn)->mnt_fsnotify_mask;
return NULL;
}
__u32 fsnotify_conn_mask(struct fsnotify_mark_connector *conn)
{
if (WARN_ON(!fsnotify_valid_obj_type(conn->type)))
return 0;
return *fsnotify_conn_mask_p(conn);
}
static void __fsnotify_recalc_mask(struct fsnotify_mark_connector *conn) static void __fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
{ {
u32 new_mask = 0; u32 new_mask = 0;
...@@ -119,15 +136,15 @@ static void __fsnotify_recalc_mask(struct fsnotify_mark_connector *conn) ...@@ -119,15 +136,15 @@ static void __fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
if (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED) if (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)
new_mask |= mark->mask; new_mask |= mark->mask;
} }
if (conn->type == FSNOTIFY_OBJ_TYPE_INODE) if (WARN_ON(!fsnotify_valid_obj_type(conn->type)))
conn->inode->i_fsnotify_mask = new_mask; return;
else if (conn->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT)
real_mount(conn->mnt)->mnt_fsnotify_mask = new_mask; *fsnotify_conn_mask_p(conn) = new_mask;
} }
/* /*
* Calculate mask of events for a list of marks. The caller must make sure * Calculate mask of events for a list of marks. The caller must make sure
* connector and connector->inode cannot disappear under us. Callers achieve * connector and connector->obj cannot disappear under us. Callers achieve
* this by holding a mark->lock or mark->group->mark_mutex for a mark on this * this by holding a mark->lock or mark->group->mark_mutex for a mark on this
* list. * list.
*/ */
...@@ -140,7 +157,8 @@ void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn) ...@@ -140,7 +157,8 @@ void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
__fsnotify_recalc_mask(conn); __fsnotify_recalc_mask(conn);
spin_unlock(&conn->lock); spin_unlock(&conn->lock);
if (conn->type == FSNOTIFY_OBJ_TYPE_INODE) if (conn->type == FSNOTIFY_OBJ_TYPE_INODE)
__fsnotify_update_child_dentry_flags(conn->inode); __fsnotify_update_child_dentry_flags(
fsnotify_conn_inode(conn));
} }
/* Free all connectors queued for freeing once SRCU period ends */ /* Free all connectors queued for freeing once SRCU period ends */
...@@ -166,20 +184,20 @@ static struct inode *fsnotify_detach_connector_from_object( ...@@ -166,20 +184,20 @@ static struct inode *fsnotify_detach_connector_from_object(
{ {
struct inode *inode = NULL; struct inode *inode = NULL;
if (conn->type == FSNOTIFY_OBJ_TYPE_DETACHED)
return NULL;
if (conn->type == FSNOTIFY_OBJ_TYPE_INODE) { if (conn->type == FSNOTIFY_OBJ_TYPE_INODE) {
inode = conn->inode; inode = fsnotify_conn_inode(conn);
rcu_assign_pointer(inode->i_fsnotify_marks, NULL);
inode->i_fsnotify_mask = 0; inode->i_fsnotify_mask = 0;
conn->inode = NULL;
conn->type = FSNOTIFY_OBJ_TYPE_DETACHED;
} else if (conn->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT) { } else if (conn->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT) {
rcu_assign_pointer(real_mount(conn->mnt)->mnt_fsnotify_marks, fsnotify_conn_mount(conn)->mnt_fsnotify_mask = 0;
NULL);
real_mount(conn->mnt)->mnt_fsnotify_mask = 0;
conn->mnt = NULL;
conn->type = FSNOTIFY_OBJ_TYPE_DETACHED;
} }
rcu_assign_pointer(*(conn->obj), NULL);
conn->obj = NULL;
conn->type = FSNOTIFY_OBJ_TYPE_DETACHED;
return inode; return inode;
} }
...@@ -436,11 +454,10 @@ int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b) ...@@ -436,11 +454,10 @@ int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b)
return -1; return -1;
} }
static int fsnotify_attach_connector_to_object( static int fsnotify_attach_connector_to_object(fsnotify_connp_t *connp,
struct fsnotify_mark_connector __rcu **connp, unsigned int type)
struct inode *inode,
struct vfsmount *mnt)
{ {
struct inode *inode = NULL;
struct fsnotify_mark_connector *conn; struct fsnotify_mark_connector *conn;
conn = kmem_cache_alloc(fsnotify_mark_connector_cachep, GFP_KERNEL); conn = kmem_cache_alloc(fsnotify_mark_connector_cachep, GFP_KERNEL);
...@@ -448,13 +465,10 @@ static int fsnotify_attach_connector_to_object( ...@@ -448,13 +465,10 @@ static int fsnotify_attach_connector_to_object(
return -ENOMEM; return -ENOMEM;
spin_lock_init(&conn->lock); spin_lock_init(&conn->lock);
INIT_HLIST_HEAD(&conn->list); INIT_HLIST_HEAD(&conn->list);
if (inode) { conn->type = type;
conn->type = FSNOTIFY_OBJ_TYPE_INODE; conn->obj = connp;
conn->inode = igrab(inode); if (conn->type == FSNOTIFY_OBJ_TYPE_INODE)
} else { inode = igrab(fsnotify_conn_inode(conn));
conn->type = FSNOTIFY_OBJ_TYPE_VFSMOUNT;
conn->mnt = mnt;
}
/* /*
* cmpxchg() provides the barrier so that readers of *connp can see * cmpxchg() provides the barrier so that readers of *connp can see
* only initialized structure * only initialized structure
...@@ -476,7 +490,7 @@ static int fsnotify_attach_connector_to_object( ...@@ -476,7 +490,7 @@ static int fsnotify_attach_connector_to_object(
* they are sure list cannot go away under them. * they are sure list cannot go away under them.
*/ */
static struct fsnotify_mark_connector *fsnotify_grab_connector( static struct fsnotify_mark_connector *fsnotify_grab_connector(
struct fsnotify_mark_connector __rcu **connp) fsnotify_connp_t *connp)
{ {
struct fsnotify_mark_connector *conn; struct fsnotify_mark_connector *conn;
int idx; int idx;
...@@ -503,27 +517,22 @@ static struct fsnotify_mark_connector *fsnotify_grab_connector( ...@@ -503,27 +517,22 @@ static struct fsnotify_mark_connector *fsnotify_grab_connector(
* priority, highest number first, and then by the group's location in memory. * priority, highest number first, and then by the group's location in memory.
*/ */
static int fsnotify_add_mark_list(struct fsnotify_mark *mark, static int fsnotify_add_mark_list(struct fsnotify_mark *mark,
struct inode *inode, struct vfsmount *mnt, fsnotify_connp_t *connp, unsigned int type,
int allow_dups) int allow_dups)
{ {
struct fsnotify_mark *lmark, *last = NULL; struct fsnotify_mark *lmark, *last = NULL;
struct fsnotify_mark_connector *conn; struct fsnotify_mark_connector *conn;
struct fsnotify_mark_connector __rcu **connp;
int cmp; int cmp;
int err = 0; int err = 0;
if (WARN_ON(!inode && !mnt)) if (WARN_ON(!fsnotify_valid_obj_type(type)))
return -EINVAL; return -EINVAL;
if (inode)
connp = &inode->i_fsnotify_marks;
else
connp = &real_mount(mnt)->mnt_fsnotify_marks;
restart: restart:
spin_lock(&mark->lock); spin_lock(&mark->lock);
conn = fsnotify_grab_connector(connp); conn = fsnotify_grab_connector(connp);
if (!conn) { if (!conn) {
spin_unlock(&mark->lock); spin_unlock(&mark->lock);
err = fsnotify_attach_connector_to_object(connp, inode, mnt); err = fsnotify_attach_connector_to_object(connp, type);
if (err) if (err)
return err; return err;
goto restart; goto restart;
...@@ -569,14 +578,13 @@ static int fsnotify_add_mark_list(struct fsnotify_mark *mark, ...@@ -569,14 +578,13 @@ static int fsnotify_add_mark_list(struct fsnotify_mark *mark,
* These marks may be used for the fsnotify backend to determine which * These marks may be used for the fsnotify backend to determine which
* event types should be delivered to which group. * event types should be delivered to which group.
*/ */
int fsnotify_add_mark_locked(struct fsnotify_mark *mark, struct inode *inode, int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
struct vfsmount *mnt, int allow_dups) fsnotify_connp_t *connp, unsigned int type,
int allow_dups)
{ {
struct fsnotify_group *group = mark->group; struct fsnotify_group *group = mark->group;
int ret = 0; int ret = 0;
BUG_ON(inode && mnt);
BUG_ON(!inode && !mnt);
BUG_ON(!mutex_is_locked(&group->mark_mutex)); BUG_ON(!mutex_is_locked(&group->mark_mutex));
/* /*
...@@ -593,7 +601,7 @@ int fsnotify_add_mark_locked(struct fsnotify_mark *mark, struct inode *inode, ...@@ -593,7 +601,7 @@ int fsnotify_add_mark_locked(struct fsnotify_mark *mark, struct inode *inode,
fsnotify_get_mark(mark); /* for g_list */ fsnotify_get_mark(mark); /* for g_list */
spin_unlock(&mark->lock); spin_unlock(&mark->lock);
ret = fsnotify_add_mark_list(mark, inode, mnt, allow_dups); ret = fsnotify_add_mark_list(mark, connp, type, allow_dups);
if (ret) if (ret)
goto err; goto err;
...@@ -613,14 +621,14 @@ int fsnotify_add_mark_locked(struct fsnotify_mark *mark, struct inode *inode, ...@@ -613,14 +621,14 @@ int fsnotify_add_mark_locked(struct fsnotify_mark *mark, struct inode *inode,
return ret; return ret;
} }
int fsnotify_add_mark(struct fsnotify_mark *mark, struct inode *inode, int fsnotify_add_mark(struct fsnotify_mark *mark, fsnotify_connp_t *connp,
struct vfsmount *mnt, int allow_dups) unsigned int type, int allow_dups)
{ {
int ret; int ret;
struct fsnotify_group *group = mark->group; struct fsnotify_group *group = mark->group;
mutex_lock(&group->mark_mutex); mutex_lock(&group->mark_mutex);
ret = fsnotify_add_mark_locked(mark, inode, mnt, allow_dups); ret = fsnotify_add_mark_locked(mark, connp, type, allow_dups);
mutex_unlock(&group->mark_mutex); mutex_unlock(&group->mark_mutex);
return ret; return ret;
} }
...@@ -629,9 +637,8 @@ int fsnotify_add_mark(struct fsnotify_mark *mark, struct inode *inode, ...@@ -629,9 +637,8 @@ int fsnotify_add_mark(struct fsnotify_mark *mark, struct inode *inode,
* Given a list of marks, find the mark associated with given group. If found * Given a list of marks, find the mark associated with given group. If found
* take a reference to that mark and return it, else return NULL. * take a reference to that mark and return it, else return NULL.
*/ */
struct fsnotify_mark *fsnotify_find_mark( struct fsnotify_mark *fsnotify_find_mark(fsnotify_connp_t *connp,
struct fsnotify_mark_connector __rcu **connp, struct fsnotify_group *group)
struct fsnotify_group *group)
{ {
struct fsnotify_mark_connector *conn; struct fsnotify_mark_connector *conn;
struct fsnotify_mark *mark; struct fsnotify_mark *mark;
...@@ -697,8 +704,8 @@ void fsnotify_clear_marks_by_group(struct fsnotify_group *group, ...@@ -697,8 +704,8 @@ void fsnotify_clear_marks_by_group(struct fsnotify_group *group,
} }
} }
/* Destroy all marks attached to inode / vfsmount */ /* Destroy all marks attached to an object via connector */
void fsnotify_destroy_marks(struct fsnotify_mark_connector __rcu **connp) void fsnotify_destroy_marks(fsnotify_connp_t *connp)
{ {
struct fsnotify_mark_connector *conn; struct fsnotify_mark_connector *conn;
struct fsnotify_mark *mark, *old_mark = NULL; struct fsnotify_mark *mark, *old_mark = NULL;
......
...@@ -210,6 +210,11 @@ enum fsnotify_obj_type { ...@@ -210,6 +210,11 @@ enum fsnotify_obj_type {
#define FSNOTIFY_OBJ_TYPE_VFSMOUNT_FL (1U << FSNOTIFY_OBJ_TYPE_VFSMOUNT) #define FSNOTIFY_OBJ_TYPE_VFSMOUNT_FL (1U << FSNOTIFY_OBJ_TYPE_VFSMOUNT)
#define FSNOTIFY_OBJ_ALL_TYPES_MASK ((1U << FSNOTIFY_OBJ_TYPE_COUNT) - 1) #define FSNOTIFY_OBJ_ALL_TYPES_MASK ((1U << FSNOTIFY_OBJ_TYPE_COUNT) - 1)
static inline bool fsnotify_valid_obj_type(unsigned int type)
{
return (type < FSNOTIFY_OBJ_TYPE_COUNT);
}
struct fsnotify_iter_info { struct fsnotify_iter_info {
struct fsnotify_mark *marks[FSNOTIFY_OBJ_TYPE_COUNT]; struct fsnotify_mark *marks[FSNOTIFY_OBJ_TYPE_COUNT];
unsigned int report_mask; unsigned int report_mask;
...@@ -250,6 +255,13 @@ FSNOTIFY_ITER_FUNCS(vfsmount, VFSMOUNT) ...@@ -250,6 +255,13 @@ FSNOTIFY_ITER_FUNCS(vfsmount, VFSMOUNT)
#define fsnotify_foreach_obj_type(type) \ #define fsnotify_foreach_obj_type(type) \
for (type = 0; type < FSNOTIFY_OBJ_TYPE_COUNT; type++) for (type = 0; type < FSNOTIFY_OBJ_TYPE_COUNT; type++)
/*
* fsnotify_connp_t is what we embed in objects which connector can be attached
* to. fsnotify_connp_t * is how we refer from connector back to object.
*/
struct fsnotify_mark_connector;
typedef struct fsnotify_mark_connector __rcu *fsnotify_connp_t;
/* /*
* Inode / vfsmount point to this structure which tracks all marks attached to * Inode / vfsmount point to this structure which tracks all marks attached to
* the inode / vfsmount. The reference to inode / vfsmount is held by this * the inode / vfsmount. The reference to inode / vfsmount is held by this
...@@ -259,9 +271,9 @@ FSNOTIFY_ITER_FUNCS(vfsmount, VFSMOUNT) ...@@ -259,9 +271,9 @@ FSNOTIFY_ITER_FUNCS(vfsmount, VFSMOUNT)
struct fsnotify_mark_connector { struct fsnotify_mark_connector {
spinlock_t lock; spinlock_t lock;
unsigned int type; /* Type of object [lock] */ unsigned int type; /* Type of object [lock] */
union { /* Object pointer [lock] */ union {
struct inode *inode; /* Object pointer [lock] */
struct vfsmount *mnt; fsnotify_connp_t *obj;
/* Used listing heads to free after srcu period expires */ /* Used listing heads to free after srcu period expires */
struct fsnotify_mark_connector *destroy_next; struct fsnotify_mark_connector *destroy_next;
}; };
...@@ -389,32 +401,36 @@ extern struct fsnotify_event *fsnotify_remove_first_event(struct fsnotify_group ...@@ -389,32 +401,36 @@ extern struct fsnotify_event *fsnotify_remove_first_event(struct fsnotify_group
/* functions used to manipulate the marks attached to inodes */ /* functions used to manipulate the marks attached to inodes */
/* Get mask of events for a list of marks */
extern __u32 fsnotify_conn_mask(struct fsnotify_mark_connector *conn);
/* Calculate mask of events for a list of marks */ /* Calculate mask of events for a list of marks */
extern void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn); extern void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn);
extern void fsnotify_init_mark(struct fsnotify_mark *mark, extern void fsnotify_init_mark(struct fsnotify_mark *mark,
struct fsnotify_group *group); struct fsnotify_group *group);
/* Find mark belonging to given group in the list of marks */ /* Find mark belonging to given group in the list of marks */
extern struct fsnotify_mark *fsnotify_find_mark( extern struct fsnotify_mark *fsnotify_find_mark(fsnotify_connp_t *connp,
struct fsnotify_mark_connector __rcu **connp, struct fsnotify_group *group);
struct fsnotify_group *group); /* attach the mark to the object */
/* attach the mark to the inode or vfsmount */ extern int fsnotify_add_mark(struct fsnotify_mark *mark,
extern int fsnotify_add_mark(struct fsnotify_mark *mark, struct inode *inode, fsnotify_connp_t *connp, unsigned int type,
struct vfsmount *mnt, int allow_dups); int allow_dups);
extern int fsnotify_add_mark_locked(struct fsnotify_mark *mark, extern int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
struct inode *inode, struct vfsmount *mnt, fsnotify_connp_t *connp, unsigned int type,
int allow_dups); int allow_dups);
/* attach the mark to the inode */ /* attach the mark to the inode */
static inline int fsnotify_add_inode_mark(struct fsnotify_mark *mark, static inline int fsnotify_add_inode_mark(struct fsnotify_mark *mark,
struct inode *inode, struct inode *inode,
int allow_dups) int allow_dups)
{ {
return fsnotify_add_mark(mark, inode, NULL, allow_dups); return fsnotify_add_mark(mark, &inode->i_fsnotify_marks,
FSNOTIFY_OBJ_TYPE_INODE, allow_dups);
} }
static inline int fsnotify_add_inode_mark_locked(struct fsnotify_mark *mark, static inline int fsnotify_add_inode_mark_locked(struct fsnotify_mark *mark,
struct inode *inode, struct inode *inode,
int allow_dups) int allow_dups)
{ {
return fsnotify_add_mark_locked(mark, inode, NULL, allow_dups); return fsnotify_add_mark_locked(mark, &inode->i_fsnotify_marks,
FSNOTIFY_OBJ_TYPE_INODE, allow_dups);
} }
/* given a group and a mark, flag mark to be freed when all references are dropped */ /* given a group and a mark, flag mark to be freed when all references are dropped */
extern void fsnotify_destroy_mark(struct fsnotify_mark *mark, extern void fsnotify_destroy_mark(struct fsnotify_mark *mark,
......
...@@ -18,6 +18,6 @@ extern struct ctl_table inotify_table[]; /* for sysctl */ ...@@ -18,6 +18,6 @@ extern struct ctl_table inotify_table[]; /* for sysctl */
IN_DELETE_SELF | IN_MOVE_SELF | IN_UNMOUNT | \ IN_DELETE_SELF | IN_MOVE_SELF | IN_UNMOUNT | \
IN_Q_OVERFLOW | IN_IGNORED | IN_ONLYDIR | \ IN_Q_OVERFLOW | IN_IGNORED | IN_ONLYDIR | \
IN_DONT_FOLLOW | IN_EXCL_UNLINK | IN_MASK_ADD | \ IN_DONT_FOLLOW | IN_EXCL_UNLINK | IN_MASK_ADD | \
IN_ISDIR | IN_ONESHOT) IN_MASK_CREATE | IN_ISDIR | IN_ONESHOT)
#endif /* _LINUX_INOTIFY_H */ #endif /* _LINUX_INOTIFY_H */
...@@ -53,6 +53,7 @@ struct inotify_event { ...@@ -53,6 +53,7 @@ struct inotify_event {
#define IN_ONLYDIR 0x01000000 /* only watch the path if it is a directory */ #define IN_ONLYDIR 0x01000000 /* only watch the path if it is a directory */
#define IN_DONT_FOLLOW 0x02000000 /* don't follow a sym link */ #define IN_DONT_FOLLOW 0x02000000 /* don't follow a sym link */
#define IN_EXCL_UNLINK 0x04000000 /* exclude events on unlinked objects */ #define IN_EXCL_UNLINK 0x04000000 /* exclude events on unlinked objects */
#define IN_MASK_CREATE 0x10000000 /* only create watches */
#define IN_MASK_ADD 0x20000000 /* add to the mask of an already existing watch */ #define IN_MASK_ADD 0x20000000 /* add to the mask of an already existing watch */
#define IN_ISDIR 0x40000000 /* event occurred against dir */ #define IN_ISDIR 0x40000000 /* event occurred against dir */
#define IN_ONESHOT 0x80000000 /* only send event once */ #define IN_ONESHOT 0x80000000 /* only send event once */
......
...@@ -168,7 +168,8 @@ static __cacheline_aligned_in_smp DEFINE_SPINLOCK(hash_lock); ...@@ -168,7 +168,8 @@ static __cacheline_aligned_in_smp DEFINE_SPINLOCK(hash_lock);
/* Function to return search key in our hash from inode. */ /* Function to return search key in our hash from inode. */
static unsigned long inode_to_key(const struct inode *inode) static unsigned long inode_to_key(const struct inode *inode)
{ {
return (unsigned long)inode; /* Use address pointed to by connector->obj as the key */
return (unsigned long)&inode->i_fsnotify_marks;
} }
/* /*
...@@ -183,7 +184,7 @@ static unsigned long chunk_to_key(struct audit_chunk *chunk) ...@@ -183,7 +184,7 @@ static unsigned long chunk_to_key(struct audit_chunk *chunk)
*/ */
if (WARN_ON_ONCE(!chunk->mark.connector)) if (WARN_ON_ONCE(!chunk->mark.connector))
return 0; return 0;
return (unsigned long)chunk->mark.connector->inode; return (unsigned long)chunk->mark.connector->obj;
} }
static inline struct list_head *chunk_hash(unsigned long key) static inline struct list_head *chunk_hash(unsigned long key)
...@@ -258,7 +259,7 @@ static void untag_chunk(struct node *p) ...@@ -258,7 +259,7 @@ static void untag_chunk(struct node *p)
spin_lock(&entry->lock); spin_lock(&entry->lock);
/* /*
* mark_mutex protects mark from getting detached and thus also from * mark_mutex protects mark from getting detached and thus also from
* mark->connector->inode getting NULL. * mark->connector->obj getting NULL.
*/ */
if (chunk->dead || !(entry->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) { if (chunk->dead || !(entry->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
spin_unlock(&entry->lock); spin_unlock(&entry->lock);
...@@ -288,8 +289,8 @@ static void untag_chunk(struct node *p) ...@@ -288,8 +289,8 @@ static void untag_chunk(struct node *p)
if (!new) if (!new)
goto Fallback; goto Fallback;
if (fsnotify_add_inode_mark_locked(&new->mark, entry->connector->inode, if (fsnotify_add_mark_locked(&new->mark, entry->connector->obj,
1)) { FSNOTIFY_OBJ_TYPE_INODE, 1)) {
fsnotify_put_mark(&new->mark); fsnotify_put_mark(&new->mark);
goto Fallback; goto Fallback;
} }
...@@ -423,7 +424,7 @@ static int tag_chunk(struct inode *inode, struct audit_tree *tree) ...@@ -423,7 +424,7 @@ static int tag_chunk(struct inode *inode, struct audit_tree *tree)
spin_lock(&old_entry->lock); spin_lock(&old_entry->lock);
/* /*
* mark_mutex protects mark from getting detached and thus also from * mark_mutex protects mark from getting detached and thus also from
* mark->connector->inode getting NULL. * mark->connector->obj getting NULL.
*/ */
if (!(old_entry->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) { if (!(old_entry->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
/* old_entry is being shot, lets just lie */ /* old_entry is being shot, lets just lie */
...@@ -434,8 +435,8 @@ static int tag_chunk(struct inode *inode, struct audit_tree *tree) ...@@ -434,8 +435,8 @@ static int tag_chunk(struct inode *inode, struct audit_tree *tree)
return -ENOENT; return -ENOENT;
} }
if (fsnotify_add_inode_mark_locked(chunk_entry, if (fsnotify_add_mark_locked(chunk_entry, old_entry->connector->obj,
old_entry->connector->inode, 1)) { FSNOTIFY_OBJ_TYPE_INODE, 1)) {
spin_unlock(&old_entry->lock); spin_unlock(&old_entry->lock);
mutex_unlock(&old_entry->group->mark_mutex); mutex_unlock(&old_entry->group->mark_mutex);
fsnotify_put_mark(chunk_entry); fsnotify_put_mark(chunk_entry);
......
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