Skip to content

Commit 36f10f5

Browse files
amir73iljankara
authored andcommitted
fsnotify: let connector point to an abstract object
Make the code to attach/detach a connector to object more generic by letting the fsnotify connector point to an abstract fsnotify_connp_t. Code that needs to dereference an inode or mount object now uses the helpers fsnotify_conn_{inode,mount}. Signed-off-by: Amir Goldstein <amir73il@gmail.com> Signed-off-by: Jan Kara <jack@suse.cz>
1 parent b812a9f commit 36f10f5

File tree

5 files changed

+45
-37
lines changed

5 files changed

+45
-37
lines changed

fs/notify/fdinfo.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include <linux/exportfs.h>
1616

1717
#include "inotify/inotify.h"
18-
#include "../fs/mount.h"
18+
#include "fsnotify.h"
1919

2020
#if defined(CONFIG_PROC_FS)
2121

@@ -81,7 +81,7 @@ static void inotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark)
8181
return;
8282

8383
inode_mark = container_of(mark, struct inotify_inode_mark, fsn_mark);
84-
inode = igrab(mark->connector->inode);
84+
inode = igrab(fsnotify_conn_inode(mark->connector));
8585
if (inode) {
8686
/*
8787
* 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)
117117
mflags |= FAN_MARK_IGNORED_SURV_MODIFY;
118118

119119
if (mark->connector->type == FSNOTIFY_OBJ_TYPE_INODE) {
120-
inode = igrab(mark->connector->inode);
120+
inode = igrab(fsnotify_conn_inode(mark->connector));
121121
if (!inode)
122122
return;
123123
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)
127127
seq_putc(m, '\n');
128128
iput(inode);
129129
} else if (mark->connector->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT) {
130-
struct mount *mnt = real_mount(mark->connector->mnt);
130+
struct mount *mnt = fsnotify_conn_mount(mark->connector);
131131

132132
seq_printf(m, "fanotify mnt_id:%x mflags:%x mask:%x ignored_mask:%x\n",
133133
mnt->mnt_id, mflags, mark->mask, mark->ignored_mask);

fs/notify/fsnotify.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@
99

1010
#include "../mount.h"
1111

12-
static inline struct inode *fsnotify_obj_inode(fsnotify_connp_t *connp)
12+
static inline struct inode *fsnotify_conn_inode(
13+
struct fsnotify_mark_connector *conn)
1314
{
14-
return container_of(connp, struct inode, i_fsnotify_marks);
15+
return container_of(conn->obj, struct inode, i_fsnotify_marks);
1516
}
1617

17-
static inline struct mount *fsnotify_obj_mount(fsnotify_connp_t *connp)
18+
static inline struct mount *fsnotify_conn_mount(
19+
struct fsnotify_mark_connector *conn)
1820
{
19-
return container_of(connp, struct mount, mnt_fsnotify_marks);
21+
return container_of(conn->obj, struct mount, mnt_fsnotify_marks);
2022
}
2123

2224
/* destroy all events sitting in this groups notification queue */

fs/notify/mark.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,14 @@ static void __fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
120120
new_mask |= mark->mask;
121121
}
122122
if (conn->type == FSNOTIFY_OBJ_TYPE_INODE)
123-
conn->inode->i_fsnotify_mask = new_mask;
123+
fsnotify_conn_inode(conn)->i_fsnotify_mask = new_mask;
124124
else if (conn->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT)
125-
real_mount(conn->mnt)->mnt_fsnotify_mask = new_mask;
125+
fsnotify_conn_mount(conn)->mnt_fsnotify_mask = new_mask;
126126
}
127127

128128
/*
129129
* Calculate mask of events for a list of marks. The caller must make sure
130-
* connector and connector->inode cannot disappear under us. Callers achieve
130+
* connector and connector->obj cannot disappear under us. Callers achieve
131131
* this by holding a mark->lock or mark->group->mark_mutex for a mark on this
132132
* list.
133133
*/
@@ -140,7 +140,8 @@ void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
140140
__fsnotify_recalc_mask(conn);
141141
spin_unlock(&conn->lock);
142142
if (conn->type == FSNOTIFY_OBJ_TYPE_INODE)
143-
__fsnotify_update_child_dentry_flags(conn->inode);
143+
__fsnotify_update_child_dentry_flags(
144+
fsnotify_conn_inode(conn));
144145
}
145146

146147
/* Free all connectors queued for freeing once SRCU period ends */
@@ -166,20 +167,20 @@ static struct inode *fsnotify_detach_connector_from_object(
166167
{
167168
struct inode *inode = NULL;
168169

170+
if (conn->type == FSNOTIFY_OBJ_TYPE_DETACHED)
171+
return NULL;
172+
169173
if (conn->type == FSNOTIFY_OBJ_TYPE_INODE) {
170-
inode = conn->inode;
171-
rcu_assign_pointer(inode->i_fsnotify_marks, NULL);
174+
inode = fsnotify_conn_inode(conn);
172175
inode->i_fsnotify_mask = 0;
173-
conn->inode = NULL;
174-
conn->type = FSNOTIFY_OBJ_TYPE_DETACHED;
175176
} else if (conn->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT) {
176-
rcu_assign_pointer(real_mount(conn->mnt)->mnt_fsnotify_marks,
177-
NULL);
178-
real_mount(conn->mnt)->mnt_fsnotify_mask = 0;
179-
conn->mnt = NULL;
180-
conn->type = FSNOTIFY_OBJ_TYPE_DETACHED;
177+
fsnotify_conn_mount(conn)->mnt_fsnotify_mask = 0;
181178
}
182179

180+
rcu_assign_pointer(*(conn->obj), NULL);
181+
conn->obj = NULL;
182+
conn->type = FSNOTIFY_OBJ_TYPE_DETACHED;
183+
183184
return inode;
184185
}
185186

@@ -448,10 +449,9 @@ static int fsnotify_attach_connector_to_object(fsnotify_connp_t *connp,
448449
spin_lock_init(&conn->lock);
449450
INIT_HLIST_HEAD(&conn->list);
450451
conn->type = type;
452+
conn->obj = connp;
451453
if (conn->type == FSNOTIFY_OBJ_TYPE_INODE)
452-
inode = conn->inode = igrab(fsnotify_obj_inode(connp));
453-
else if (conn->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT)
454-
conn->mnt = &fsnotify_obj_mount(connp)->mnt;
454+
inode = igrab(fsnotify_conn_inode(conn));
455455
/*
456456
* cmpxchg() provides the barrier so that readers of *connp can see
457457
* only initialized structure

include/linux/fsnotify_backend.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,13 @@ FSNOTIFY_ITER_FUNCS(vfsmount, VFSMOUNT)
255255
#define fsnotify_foreach_obj_type(type) \
256256
for (type = 0; type < FSNOTIFY_OBJ_TYPE_COUNT; type++)
257257

258+
/*
259+
* fsnotify_connp_t is what we embed in objects which connector can be attached
260+
* to. fsnotify_connp_t * is how we refer from connector back to object.
261+
*/
262+
struct fsnotify_mark_connector;
263+
typedef struct fsnotify_mark_connector __rcu *fsnotify_connp_t;
264+
258265
/*
259266
* Inode / vfsmount point to this structure which tracks all marks attached to
260267
* the inode / vfsmount. The reference to inode / vfsmount is held by this
@@ -264,17 +271,15 @@ FSNOTIFY_ITER_FUNCS(vfsmount, VFSMOUNT)
264271
struct fsnotify_mark_connector {
265272
spinlock_t lock;
266273
unsigned int type; /* Type of object [lock] */
267-
union { /* Object pointer [lock] */
268-
struct inode *inode;
269-
struct vfsmount *mnt;
274+
union {
275+
/* Object pointer [lock] */
276+
fsnotify_connp_t *obj;
270277
/* Used listing heads to free after srcu period expires */
271278
struct fsnotify_mark_connector *destroy_next;
272279
};
273280
struct hlist_head list;
274281
};
275282

276-
typedef struct fsnotify_mark_connector __rcu *fsnotify_connp_t;
277-
278283
/*
279284
* A mark is simply an object attached to an in core inode which allows an
280285
* fsnotify listener to indicate they are either no longer interested in events

kernel/audit_tree.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ static __cacheline_aligned_in_smp DEFINE_SPINLOCK(hash_lock);
168168
/* Function to return search key in our hash from inode. */
169169
static unsigned long inode_to_key(const struct inode *inode)
170170
{
171-
return (unsigned long)inode;
171+
/* Use address pointed to by connector->obj as the key */
172+
return (unsigned long)&inode->i_fsnotify_marks;
172173
}
173174

174175
/*
@@ -183,7 +184,7 @@ static unsigned long chunk_to_key(struct audit_chunk *chunk)
183184
*/
184185
if (WARN_ON_ONCE(!chunk->mark.connector))
185186
return 0;
186-
return (unsigned long)chunk->mark.connector->inode;
187+
return (unsigned long)chunk->mark.connector->obj;
187188
}
188189

189190
static inline struct list_head *chunk_hash(unsigned long key)
@@ -258,7 +259,7 @@ static void untag_chunk(struct node *p)
258259
spin_lock(&entry->lock);
259260
/*
260261
* mark_mutex protects mark from getting detached and thus also from
261-
* mark->connector->inode getting NULL.
262+
* mark->connector->obj getting NULL.
262263
*/
263264
if (chunk->dead || !(entry->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
264265
spin_unlock(&entry->lock);
@@ -288,8 +289,8 @@ static void untag_chunk(struct node *p)
288289
if (!new)
289290
goto Fallback;
290291

291-
if (fsnotify_add_inode_mark_locked(&new->mark, entry->connector->inode,
292-
1)) {
292+
if (fsnotify_add_mark_locked(&new->mark, entry->connector->obj,
293+
FSNOTIFY_OBJ_TYPE_INODE, 1)) {
293294
fsnotify_put_mark(&new->mark);
294295
goto Fallback;
295296
}
@@ -423,7 +424,7 @@ static int tag_chunk(struct inode *inode, struct audit_tree *tree)
423424
spin_lock(&old_entry->lock);
424425
/*
425426
* mark_mutex protects mark from getting detached and thus also from
426-
* mark->connector->inode getting NULL.
427+
* mark->connector->obj getting NULL.
427428
*/
428429
if (!(old_entry->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
429430
/* old_entry is being shot, lets just lie */
@@ -434,8 +435,8 @@ static int tag_chunk(struct inode *inode, struct audit_tree *tree)
434435
return -ENOENT;
435436
}
436437

437-
if (fsnotify_add_inode_mark_locked(chunk_entry,
438-
old_entry->connector->inode, 1)) {
438+
if (fsnotify_add_mark_locked(chunk_entry, old_entry->connector->obj,
439+
FSNOTIFY_OBJ_TYPE_INODE, 1)) {
439440
spin_unlock(&old_entry->lock);
440441
mutex_unlock(&old_entry->group->mark_mutex);
441442
fsnotify_put_mark(chunk_entry);

0 commit comments

Comments
 (0)