Skip to content

Commit b812a9f

Browse files
amir73iljankara
authored andcommitted
fsnotify: pass connp and object type to fsnotify_add_mark()
Instead of passing inode and vfsmount arguments to fsnotify_add_mark() and its _locked variant, pass an abstract object pointer and the object type. The helpers fsnotify_obj_{inode,mount} are added to get the concrete object pointer from abstract object pointer. Signed-off-by: Amir Goldstein <amir73il@gmail.com> Signed-off-by: Jan Kara <jack@suse.cz>
1 parent 9b6e543 commit b812a9f

File tree

4 files changed

+52
-39
lines changed

4 files changed

+52
-39
lines changed

fs/notify/fanotify/fanotify_user.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -615,8 +615,8 @@ static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
615615
}
616616

617617
static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
618-
struct inode *inode,
619-
struct vfsmount *mnt)
618+
fsnotify_connp_t *connp,
619+
unsigned int type)
620620
{
621621
struct fsnotify_mark *mark;
622622
int ret;
@@ -629,7 +629,7 @@ static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group,
629629
return ERR_PTR(-ENOMEM);
630630

631631
fsnotify_init_mark(mark, group);
632-
ret = fsnotify_add_mark_locked(mark, inode, mnt, 0);
632+
ret = fsnotify_add_mark_locked(mark, connp, type, 0);
633633
if (ret) {
634634
fsnotify_put_mark(mark);
635635
return ERR_PTR(ret);
@@ -643,14 +643,15 @@ static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
643643
struct vfsmount *mnt, __u32 mask,
644644
unsigned int flags)
645645
{
646+
fsnotify_connp_t *connp = &real_mount(mnt)->mnt_fsnotify_marks;
646647
struct fsnotify_mark *fsn_mark;
647648
__u32 added;
648649

649650
mutex_lock(&group->mark_mutex);
650-
fsn_mark = fsnotify_find_mark(&real_mount(mnt)->mnt_fsnotify_marks,
651-
group);
651+
fsn_mark = fsnotify_find_mark(connp, group);
652652
if (!fsn_mark) {
653-
fsn_mark = fanotify_add_new_mark(group, NULL, mnt);
653+
fsn_mark = fanotify_add_new_mark(group, connp,
654+
FSNOTIFY_OBJ_TYPE_VFSMOUNT);
654655
if (IS_ERR(fsn_mark)) {
655656
mutex_unlock(&group->mark_mutex);
656657
return PTR_ERR(fsn_mark);
@@ -669,6 +670,7 @@ static int fanotify_add_inode_mark(struct fsnotify_group *group,
669670
struct inode *inode, __u32 mask,
670671
unsigned int flags)
671672
{
673+
fsnotify_connp_t *connp = &inode->i_fsnotify_marks;
672674
struct fsnotify_mark *fsn_mark;
673675
__u32 added;
674676

@@ -685,9 +687,10 @@ static int fanotify_add_inode_mark(struct fsnotify_group *group,
685687
return 0;
686688

687689
mutex_lock(&group->mark_mutex);
688-
fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
690+
fsn_mark = fsnotify_find_mark(connp, group);
689691
if (!fsn_mark) {
690-
fsn_mark = fanotify_add_new_mark(group, inode, NULL);
692+
fsn_mark = fanotify_add_new_mark(group, connp,
693+
FSNOTIFY_OBJ_TYPE_INODE);
691694
if (IS_ERR(fsn_mark)) {
692695
mutex_unlock(&group->mark_mutex);
693696
return PTR_ERR(fsn_mark);

fs/notify/fsnotify.h

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

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

12+
static inline struct inode *fsnotify_obj_inode(fsnotify_connp_t *connp)
13+
{
14+
return container_of(connp, struct inode, i_fsnotify_marks);
15+
}
16+
17+
static inline struct mount *fsnotify_obj_mount(fsnotify_connp_t *connp)
18+
{
19+
return container_of(connp, struct mount, mnt_fsnotify_marks);
20+
}
21+
1222
/* destroy all events sitting in this groups notification queue */
1323
extern void fsnotify_flush_notify(struct fsnotify_group *group);
1424

fs/notify/mark.c

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -437,23 +437,21 @@ int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b)
437437
}
438438

439439
static int fsnotify_attach_connector_to_object(fsnotify_connp_t *connp,
440-
struct inode *inode,
441-
struct vfsmount *mnt)
440+
unsigned int type)
442441
{
442+
struct inode *inode = NULL;
443443
struct fsnotify_mark_connector *conn;
444444

445445
conn = kmem_cache_alloc(fsnotify_mark_connector_cachep, GFP_KERNEL);
446446
if (!conn)
447447
return -ENOMEM;
448448
spin_lock_init(&conn->lock);
449449
INIT_HLIST_HEAD(&conn->list);
450-
if (inode) {
451-
conn->type = FSNOTIFY_OBJ_TYPE_INODE;
452-
conn->inode = igrab(inode);
453-
} else {
454-
conn->type = FSNOTIFY_OBJ_TYPE_VFSMOUNT;
455-
conn->mnt = mnt;
456-
}
450+
conn->type = type;
451+
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;
457455
/*
458456
* cmpxchg() provides the barrier so that readers of *connp can see
459457
* only initialized structure
@@ -502,27 +500,22 @@ static struct fsnotify_mark_connector *fsnotify_grab_connector(
502500
* priority, highest number first, and then by the group's location in memory.
503501
*/
504502
static int fsnotify_add_mark_list(struct fsnotify_mark *mark,
505-
struct inode *inode, struct vfsmount *mnt,
503+
fsnotify_connp_t *connp, unsigned int type,
506504
int allow_dups)
507505
{
508506
struct fsnotify_mark *lmark, *last = NULL;
509507
struct fsnotify_mark_connector *conn;
510-
fsnotify_connp_t *connp;
511508
int cmp;
512509
int err = 0;
513510

514-
if (WARN_ON(!inode && !mnt))
511+
if (WARN_ON(!fsnotify_valid_obj_type(type)))
515512
return -EINVAL;
516-
if (inode)
517-
connp = &inode->i_fsnotify_marks;
518-
else
519-
connp = &real_mount(mnt)->mnt_fsnotify_marks;
520513
restart:
521514
spin_lock(&mark->lock);
522515
conn = fsnotify_grab_connector(connp);
523516
if (!conn) {
524517
spin_unlock(&mark->lock);
525-
err = fsnotify_attach_connector_to_object(connp, inode, mnt);
518+
err = fsnotify_attach_connector_to_object(connp, type);
526519
if (err)
527520
return err;
528521
goto restart;
@@ -568,14 +561,13 @@ static int fsnotify_add_mark_list(struct fsnotify_mark *mark,
568561
* These marks may be used for the fsnotify backend to determine which
569562
* event types should be delivered to which group.
570563
*/
571-
int fsnotify_add_mark_locked(struct fsnotify_mark *mark, struct inode *inode,
572-
struct vfsmount *mnt, int allow_dups)
564+
int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
565+
fsnotify_connp_t *connp, unsigned int type,
566+
int allow_dups)
573567
{
574568
struct fsnotify_group *group = mark->group;
575569
int ret = 0;
576570

577-
BUG_ON(inode && mnt);
578-
BUG_ON(!inode && !mnt);
579571
BUG_ON(!mutex_is_locked(&group->mark_mutex));
580572

581573
/*
@@ -592,7 +584,7 @@ int fsnotify_add_mark_locked(struct fsnotify_mark *mark, struct inode *inode,
592584
fsnotify_get_mark(mark); /* for g_list */
593585
spin_unlock(&mark->lock);
594586

595-
ret = fsnotify_add_mark_list(mark, inode, mnt, allow_dups);
587+
ret = fsnotify_add_mark_list(mark, connp, type, allow_dups);
596588
if (ret)
597589
goto err;
598590

@@ -612,14 +604,14 @@ int fsnotify_add_mark_locked(struct fsnotify_mark *mark, struct inode *inode,
612604
return ret;
613605
}
614606

615-
int fsnotify_add_mark(struct fsnotify_mark *mark, struct inode *inode,
616-
struct vfsmount *mnt, int allow_dups)
607+
int fsnotify_add_mark(struct fsnotify_mark *mark, fsnotify_connp_t *connp,
608+
unsigned int type, int allow_dups)
617609
{
618610
int ret;
619611
struct fsnotify_group *group = mark->group;
620612

621613
mutex_lock(&group->mark_mutex);
622-
ret = fsnotify_add_mark_locked(mark, inode, mnt, allow_dups);
614+
ret = fsnotify_add_mark_locked(mark, connp, type, allow_dups);
623615
mutex_unlock(&group->mark_mutex);
624616
return ret;
625617
}

include/linux/fsnotify_backend.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ enum fsnotify_obj_type {
210210
#define FSNOTIFY_OBJ_TYPE_VFSMOUNT_FL (1U << FSNOTIFY_OBJ_TYPE_VFSMOUNT)
211211
#define FSNOTIFY_OBJ_ALL_TYPES_MASK ((1U << FSNOTIFY_OBJ_TYPE_COUNT) - 1)
212212

213+
static inline bool fsnotify_valid_obj_type(unsigned int type)
214+
{
215+
return (type < FSNOTIFY_OBJ_TYPE_COUNT);
216+
}
217+
213218
struct fsnotify_iter_info {
214219
struct fsnotify_mark *marks[FSNOTIFY_OBJ_TYPE_COUNT];
215220
unsigned int report_mask;
@@ -398,24 +403,27 @@ extern void fsnotify_init_mark(struct fsnotify_mark *mark,
398403
/* Find mark belonging to given group in the list of marks */
399404
extern struct fsnotify_mark *fsnotify_find_mark(fsnotify_connp_t *connp,
400405
struct fsnotify_group *group);
401-
/* attach the mark to the inode or vfsmount */
402-
extern int fsnotify_add_mark(struct fsnotify_mark *mark, struct inode *inode,
403-
struct vfsmount *mnt, int allow_dups);
406+
/* attach the mark to the object */
407+
extern int fsnotify_add_mark(struct fsnotify_mark *mark,
408+
fsnotify_connp_t *connp, unsigned int type,
409+
int allow_dups);
404410
extern int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
405-
struct inode *inode, struct vfsmount *mnt,
411+
fsnotify_connp_t *connp, unsigned int type,
406412
int allow_dups);
407413
/* attach the mark to the inode */
408414
static inline int fsnotify_add_inode_mark(struct fsnotify_mark *mark,
409415
struct inode *inode,
410416
int allow_dups)
411417
{
412-
return fsnotify_add_mark(mark, inode, NULL, allow_dups);
418+
return fsnotify_add_mark(mark, &inode->i_fsnotify_marks,
419+
FSNOTIFY_OBJ_TYPE_INODE, allow_dups);
413420
}
414421
static inline int fsnotify_add_inode_mark_locked(struct fsnotify_mark *mark,
415422
struct inode *inode,
416423
int allow_dups)
417424
{
418-
return fsnotify_add_mark_locked(mark, inode, NULL, allow_dups);
425+
return fsnotify_add_mark_locked(mark, &inode->i_fsnotify_marks,
426+
FSNOTIFY_OBJ_TYPE_INODE, allow_dups);
419427
}
420428
/* given a group and a mark, flag mark to be freed when all references are dropped */
421429
extern void fsnotify_destroy_mark(struct fsnotify_mark *mark,

0 commit comments

Comments
 (0)