Skip to content

Commit 687c217

Browse files
amir73iljankara
authored andcommitted
fsnotify: pass object pointer and type to fsnotify mark helpers
Instead of passing fsnotify_connp_t, pass the pointer to the marked object. Store the object pointer in the connector and move the definition of fsnotify_connp_t to internal fsnotify subsystem API, so it is no longer used by fsnotify backends. Suggested-by: Jan Kara <[email protected]> Signed-off-by: Amir Goldstein <[email protected]> Signed-off-by: Jan Kara <[email protected]> Message-Id: <[email protected]>
1 parent b5cae08 commit 687c217

File tree

4 files changed

+59
-120
lines changed

4 files changed

+59
-120
lines changed

fs/notify/fanotify/fanotify_user.c

Lines changed: 20 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,15 +1076,15 @@ static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
10761076
}
10771077

10781078
static int fanotify_remove_mark(struct fsnotify_group *group,
1079-
fsnotify_connp_t *connp, __u32 mask,
1079+
void *obj, unsigned int obj_type, __u32 mask,
10801080
unsigned int flags, __u32 umask)
10811081
{
10821082
struct fsnotify_mark *fsn_mark = NULL;
10831083
__u32 removed;
10841084
int destroy_mark;
10851085

10861086
fsnotify_group_lock(group);
1087-
fsn_mark = fsnotify_find_mark(connp, group);
1087+
fsn_mark = fsnotify_find_mark(obj, obj_type, group);
10881088
if (!fsn_mark) {
10891089
fsnotify_group_unlock(group);
10901090
return -ENOENT;
@@ -1105,30 +1105,6 @@ static int fanotify_remove_mark(struct fsnotify_group *group,
11051105
return 0;
11061106
}
11071107

1108-
static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
1109-
struct vfsmount *mnt, __u32 mask,
1110-
unsigned int flags, __u32 umask)
1111-
{
1112-
return fanotify_remove_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
1113-
mask, flags, umask);
1114-
}
1115-
1116-
static int fanotify_remove_sb_mark(struct fsnotify_group *group,
1117-
struct super_block *sb, __u32 mask,
1118-
unsigned int flags, __u32 umask)
1119-
{
1120-
return fanotify_remove_mark(group, &sb->s_fsnotify_marks, mask,
1121-
flags, umask);
1122-
}
1123-
1124-
static int fanotify_remove_inode_mark(struct fsnotify_group *group,
1125-
struct inode *inode, __u32 mask,
1126-
unsigned int flags, __u32 umask)
1127-
{
1128-
return fanotify_remove_mark(group, &inode->i_fsnotify_marks, mask,
1129-
flags, umask);
1130-
}
1131-
11321108
static bool fanotify_mark_update_flags(struct fsnotify_mark *fsn_mark,
11331109
unsigned int fan_flags)
11341110
{
@@ -1249,7 +1225,7 @@ static int fanotify_set_mark_fsid(struct fsnotify_group *group,
12491225
}
12501226

12511227
static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
1252-
fsnotify_connp_t *connp,
1228+
void *obj,
12531229
unsigned int obj_type,
12541230
unsigned int fan_flags,
12551231
struct fan_fsid *fsid)
@@ -1288,7 +1264,7 @@ static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
12881264
fan_mark->fsid.val[0] = fan_mark->fsid.val[1] = 0;
12891265
}
12901266

1291-
ret = fsnotify_add_mark_locked(mark, connp, obj_type, 0);
1267+
ret = fsnotify_add_mark_locked(mark, obj, obj_type, 0);
12921268
if (ret)
12931269
goto out_put_mark;
12941270

@@ -1344,7 +1320,7 @@ static int fanotify_may_update_existing_mark(struct fsnotify_mark *fsn_mark,
13441320
}
13451321

13461322
static int fanotify_add_mark(struct fsnotify_group *group,
1347-
fsnotify_connp_t *connp, unsigned int obj_type,
1323+
void *obj, unsigned int obj_type,
13481324
__u32 mask, unsigned int fan_flags,
13491325
struct fan_fsid *fsid)
13501326
{
@@ -1353,9 +1329,9 @@ static int fanotify_add_mark(struct fsnotify_group *group,
13531329
int ret = 0;
13541330

13551331
fsnotify_group_lock(group);
1356-
fsn_mark = fsnotify_find_mark(connp, group);
1332+
fsn_mark = fsnotify_find_mark(obj, obj_type, group);
13571333
if (!fsn_mark) {
1358-
fsn_mark = fanotify_add_new_mark(group, connp, obj_type,
1334+
fsn_mark = fanotify_add_new_mark(group, obj, obj_type,
13591335
fan_flags, fsid);
13601336
if (IS_ERR(fsn_mark)) {
13611337
fsnotify_group_unlock(group);
@@ -1392,30 +1368,6 @@ static int fanotify_add_mark(struct fsnotify_group *group,
13921368
return ret;
13931369
}
13941370

1395-
static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
1396-
struct vfsmount *mnt, __u32 mask,
1397-
unsigned int flags, struct fan_fsid *fsid)
1398-
{
1399-
return fanotify_add_mark(group, &real_mount(mnt)->mnt_fsnotify_marks,
1400-
FSNOTIFY_OBJ_TYPE_VFSMOUNT, mask, flags, fsid);
1401-
}
1402-
1403-
static int fanotify_add_sb_mark(struct fsnotify_group *group,
1404-
struct super_block *sb, __u32 mask,
1405-
unsigned int flags, struct fan_fsid *fsid)
1406-
{
1407-
return fanotify_add_mark(group, &sb->s_fsnotify_marks,
1408-
FSNOTIFY_OBJ_TYPE_SB, mask, flags, fsid);
1409-
}
1410-
1411-
static int fanotify_add_inode_mark(struct fsnotify_group *group,
1412-
struct inode *inode, __u32 mask,
1413-
unsigned int flags, struct fan_fsid *fsid)
1414-
{
1415-
return fanotify_add_mark(group, &inode->i_fsnotify_marks,
1416-
FSNOTIFY_OBJ_TYPE_INODE, mask, flags, fsid);
1417-
}
1418-
14191371
static struct fsnotify_event *fanotify_alloc_overflow_event(void)
14201372
{
14211373
struct fanotify_event *oevent;
@@ -1738,6 +1690,7 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
17381690
unsigned int mark_cmd = flags & FANOTIFY_MARK_CMD_BITS;
17391691
unsigned int ignore = flags & FANOTIFY_MARK_IGNORE_BITS;
17401692
unsigned int obj_type, fid_mode;
1693+
void *obj;
17411694
u32 umask = 0;
17421695
int ret;
17431696

@@ -1896,10 +1849,16 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
18961849
}
18971850

18981851
/* inode held in place by reference to path; group by fget on fd */
1899-
if (mark_type == FAN_MARK_INODE)
1852+
if (mark_type == FAN_MARK_INODE) {
19001853
inode = path.dentry->d_inode;
1901-
else
1854+
obj = inode;
1855+
} else {
19021856
mnt = path.mnt;
1857+
if (mark_type == FAN_MARK_MOUNT)
1858+
obj = mnt;
1859+
else
1860+
obj = mnt->mnt_sb;
1861+
}
19031862

19041863
/*
19051864
* If some other task has this inode open for write we should not add
@@ -1935,26 +1894,12 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
19351894
/* create/update an inode mark */
19361895
switch (mark_cmd) {
19371896
case FAN_MARK_ADD:
1938-
if (mark_type == FAN_MARK_MOUNT)
1939-
ret = fanotify_add_vfsmount_mark(group, mnt, mask,
1940-
flags, fsid);
1941-
else if (mark_type == FAN_MARK_FILESYSTEM)
1942-
ret = fanotify_add_sb_mark(group, mnt->mnt_sb, mask,
1943-
flags, fsid);
1944-
else
1945-
ret = fanotify_add_inode_mark(group, inode, mask,
1946-
flags, fsid);
1897+
ret = fanotify_add_mark(group, obj, obj_type, mask, flags,
1898+
fsid);
19471899
break;
19481900
case FAN_MARK_REMOVE:
1949-
if (mark_type == FAN_MARK_MOUNT)
1950-
ret = fanotify_remove_vfsmount_mark(group, mnt, mask,
1951-
flags, umask);
1952-
else if (mark_type == FAN_MARK_FILESYSTEM)
1953-
ret = fanotify_remove_sb_mark(group, mnt->mnt_sb, mask,
1954-
flags, umask);
1955-
else
1956-
ret = fanotify_remove_inode_mark(group, inode, mask,
1957-
flags, umask);
1901+
ret = fanotify_remove_mark(group, obj, obj_type, mask, flags,
1902+
umask);
19581903
break;
19591904
default:
19601905
ret = -EINVAL;

fs/notify/fsnotify.h

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,28 @@
99

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

12+
/*
13+
* fsnotify_connp_t is what we embed in objects which connector can be attached
14+
* to.
15+
*/
16+
typedef struct fsnotify_mark_connector __rcu *fsnotify_connp_t;
17+
1218
static inline struct inode *fsnotify_conn_inode(
1319
struct fsnotify_mark_connector *conn)
1420
{
15-
return container_of(conn->obj, struct inode, i_fsnotify_marks);
21+
return conn->obj;
1622
}
1723

1824
static inline struct mount *fsnotify_conn_mount(
1925
struct fsnotify_mark_connector *conn)
2026
{
21-
return container_of(conn->obj, struct mount, mnt_fsnotify_marks);
27+
return real_mount(conn->obj);
2228
}
2329

2430
static inline struct super_block *fsnotify_conn_sb(
2531
struct fsnotify_mark_connector *conn)
2632
{
27-
return container_of(conn->obj, struct super_block, s_fsnotify_marks);
33+
return conn->obj;
2834
}
2935

3036
static inline struct super_block *fsnotify_object_sb(void *obj,
@@ -45,16 +51,7 @@ static inline struct super_block *fsnotify_object_sb(void *obj,
4551
static inline struct super_block *fsnotify_connector_sb(
4652
struct fsnotify_mark_connector *conn)
4753
{
48-
switch (conn->type) {
49-
case FSNOTIFY_OBJ_TYPE_INODE:
50-
return fsnotify_conn_inode(conn)->i_sb;
51-
case FSNOTIFY_OBJ_TYPE_VFSMOUNT:
52-
return fsnotify_conn_mount(conn)->mnt.mnt_sb;
53-
case FSNOTIFY_OBJ_TYPE_SB:
54-
return fsnotify_conn_sb(conn);
55-
default:
56-
return NULL;
57-
}
54+
return fsnotify_object_sb(conn->obj, conn->type);
5855
}
5956

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

fs/notify/mark.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ static void *fsnotify_detach_connector_from_object(
265265
struct fsnotify_mark_connector *conn,
266266
unsigned int *type)
267267
{
268+
fsnotify_connp_t *connp = fsnotify_object_connp(conn->obj, conn->type);
268269
struct inode *inode = NULL;
269270

270271
*type = conn->type;
@@ -285,7 +286,7 @@ static void *fsnotify_detach_connector_from_object(
285286
}
286287

287288
fsnotify_put_sb_watchers(conn);
288-
rcu_assign_pointer(*(conn->obj), NULL);
289+
rcu_assign_pointer(*connp, NULL);
289290
conn->obj = NULL;
290291
conn->type = FSNOTIFY_OBJ_TYPE_DETACHED;
291292

@@ -560,7 +561,7 @@ int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b)
560561
}
561562

562563
static int fsnotify_attach_connector_to_object(fsnotify_connp_t *connp,
563-
unsigned int obj_type)
564+
void *obj, unsigned int obj_type)
564565
{
565566
struct fsnotify_mark_connector *conn;
566567

@@ -571,7 +572,7 @@ static int fsnotify_attach_connector_to_object(fsnotify_connp_t *connp,
571572
INIT_HLIST_HEAD(&conn->list);
572573
conn->flags = 0;
573574
conn->type = obj_type;
574-
conn->obj = connp;
575+
conn->obj = obj;
575576

576577
/*
577578
* cmpxchg() provides the barrier so that readers of *connp can see
@@ -620,24 +621,25 @@ static struct fsnotify_mark_connector *fsnotify_grab_connector(
620621
* to which group and for which inodes. These marks are ordered according to
621622
* priority, highest number first, and then by the group's location in memory.
622623
*/
623-
static int fsnotify_add_mark_list(struct fsnotify_mark *mark,
624-
fsnotify_connp_t *connp,
624+
static int fsnotify_add_mark_list(struct fsnotify_mark *mark, void *obj,
625625
unsigned int obj_type, int add_flags)
626626
{
627627
struct fsnotify_mark *lmark, *last = NULL;
628628
struct fsnotify_mark_connector *conn;
629+
fsnotify_connp_t *connp;
629630
int cmp;
630631
int err = 0;
631632

632633
if (WARN_ON(!fsnotify_valid_obj_type(obj_type)))
633634
return -EINVAL;
634635

636+
connp = fsnotify_object_connp(obj, obj_type);
635637
restart:
636638
spin_lock(&mark->lock);
637639
conn = fsnotify_grab_connector(connp);
638640
if (!conn) {
639641
spin_unlock(&mark->lock);
640-
err = fsnotify_attach_connector_to_object(connp, obj_type);
642+
err = fsnotify_attach_connector_to_object(connp, obj, obj_type);
641643
if (err)
642644
return err;
643645
goto restart;
@@ -689,7 +691,7 @@ static int fsnotify_add_mark_list(struct fsnotify_mark *mark,
689691
* event types should be delivered to which group.
690692
*/
691693
int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
692-
fsnotify_connp_t *connp, unsigned int obj_type,
694+
void *obj, unsigned int obj_type,
693695
int add_flags)
694696
{
695697
struct fsnotify_group *group = mark->group;
@@ -710,7 +712,7 @@ int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
710712
fsnotify_get_mark(mark); /* for g_list */
711713
spin_unlock(&mark->lock);
712714

713-
ret = fsnotify_add_mark_list(mark, connp, obj_type, add_flags);
715+
ret = fsnotify_add_mark_list(mark, obj, obj_type, add_flags);
714716
if (ret)
715717
goto err;
716718

@@ -728,14 +730,14 @@ int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
728730
return ret;
729731
}
730732

731-
int fsnotify_add_mark(struct fsnotify_mark *mark, fsnotify_connp_t *connp,
733+
int fsnotify_add_mark(struct fsnotify_mark *mark, void *obj,
732734
unsigned int obj_type, int add_flags)
733735
{
734736
int ret;
735737
struct fsnotify_group *group = mark->group;
736738

737739
fsnotify_group_lock(group);
738-
ret = fsnotify_add_mark_locked(mark, connp, obj_type, add_flags);
740+
ret = fsnotify_add_mark_locked(mark, obj, obj_type, add_flags);
739741
fsnotify_group_unlock(group);
740742
return ret;
741743
}
@@ -745,12 +747,16 @@ EXPORT_SYMBOL_GPL(fsnotify_add_mark);
745747
* Given a list of marks, find the mark associated with given group. If found
746748
* take a reference to that mark and return it, else return NULL.
747749
*/
748-
struct fsnotify_mark *fsnotify_find_mark(fsnotify_connp_t *connp,
750+
struct fsnotify_mark *fsnotify_find_mark(void *obj, unsigned int obj_type,
749751
struct fsnotify_group *group)
750752
{
753+
fsnotify_connp_t *connp = fsnotify_object_connp(obj, obj_type);
751754
struct fsnotify_mark_connector *conn;
752755
struct fsnotify_mark *mark;
753756

757+
if (!connp)
758+
return NULL;
759+
754760
conn = fsnotify_grab_connector(connp);
755761
if (!conn)
756762
return NULL;

0 commit comments

Comments
 (0)