Skip to contentSkip to conversation

fanotify: create_fd cleanup

PATCH In review Eric Paris 7 participants 13 messages Dec 2, 2009 – Jan 17, 2010 4 messages not in the archive

Subsystems (6)
lore
Share & tools
lore

Eric Paris

author maintainer
#0 lore raw

PATCH 1/4 fanotify: create_fd cleanup

From: Andreas Gruenbacher <[email protected]>

Code cleanup which does the fd creation work seperately from the userspace metadata creation. It fits better with the other code.

---

 fs/notify/fanotify/fanotify_user.c |   17 ++++++-----------
 1 files changed, 6 insertions(+), 11 deletions(-)
fs/notify/fanotify/fanotify_user.c +6 -11
index c5f59d8..7646111 100644
@@ -44,17 +44,14 @@ static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
 	return fsnotify_remove_notify_event(group);
 }
 
-static int create_and_fill_fd(struct fsnotify_group *group,
-			      struct fanotify_event_metadata *metadata,
-			      struct fsnotify_event *event)
+static int create_fd(struct fsnotify_group *group, struct fsnotify_event *event)
 {
 	int client_fd, err;
 	struct dentry *dentry;
 	struct vfsmount *mnt;
 	struct file *new_file;
 
-	pr_debug("%s: group=%p metadata=%p event=%p\n", __func__, group,
-		 metadata, event);
+	pr_debug("%s: group=%p event=%p\n", __func__, group, event);
 
 	client_fd = get_unused_fd();
 	if (client_fd < 0)
@@ -98,9 +95,7 @@ static int create_and_fill_fd(struct fsnotify_group *group,
 		fd_install(client_fd, new_file);
 	}
 
-	metadata->fd = client_fd;
-
-	return 0;
+	return client_fd;
 }
 
 static ssize_t fill_event_metadata(struct fsnotify_group *group,
@@ -113,9 +108,9 @@ static ssize_t fill_event_metadata(struct fsnotify_group *group,
 	metadata->event_len = FAN_EVENT_METADATA_LEN;
 	metadata->vers = FANOTIFY_METADATA_VERSION;
 	metadata->mask = fanotify_outgoing_mask(event->mask);
+	metadata->fd = create_fd(group, event);
 
-	return create_and_fill_fd(group, metadata, event);
-
+	return metadata->fd;
 }
 
 static ssize_t copy_event_to_user(struct fsnotify_group *group,
@@ -128,7 +123,7 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group,
 	pr_debug("%s: group=%p event=%p\n", __func__, group, event);
 
 	ret = fill_event_metadata(group, &fanotify_event_metadata, event);
-	if (ret)
+	if (ret < 0)
 		return ret;
 
 	if (copy_to_user(buf, &fanotify_event_metadata, FAN_EVENT_METADATA_LEN))
author maintainer
#1 lore raw

PATCH 2/4 fanotify: Add pids to events

From: Andreas Gruenbacher <[email protected]>

Pass the process identifiers of the triggering processes to fanotify
listeners: this information is useful for event filtering and logging.

---

 fs/notify/fanotify/fanotify.c      |    5 +++--
 fs/notify/fanotify/fanotify_user.c |    1 +
 fs/notify/notification.c           |    3 +++
 include/linux/fanotify.h           |    1 +
 include/linux/fsnotify_backend.h   |    1 +
 5 files changed, 9 insertions(+), 2 deletions(-)
fs/notify/fanotify/fanotify.c +3 -2
index 5b0b6b4..881067d 100644
@@ -10,8 +10,9 @@ static bool should_merge(struct fsnotify_event *old, struct fsnotify_event *new)
 {
 	pr_debug("%s: old=%p new=%p\n", __func__, old, new);
 
-	if ((old->to_tell == new->to_tell) &&
-	    (old->data_type == new->data_type)) {
+	if (old->to_tell == new->to_tell &&
+	    old->data_type == new->data_type &&
+	    old->tgid == new->tgid) {
 		switch (old->data_type) {
 		case (FSNOTIFY_EVENT_PATH):
 			if ((old->path.mnt == new->path.mnt) &&
fs/notify/fanotify/fanotify_user.c +1 -0
index 7646111..7cfb2f6 100644
@@ -108,6 +108,7 @@ static ssize_t fill_event_metadata(struct fsnotify_group *group,
 	metadata->event_len = FAN_EVENT_METADATA_LEN;
 	metadata->vers = FANOTIFY_METADATA_VERSION;
 	metadata->mask = fanotify_outgoing_mask(event->mask);
+	metadata->pid = pid_vnr(event->tgid);
 	metadata->fd = create_fd(group, event);
 
 	return metadata->fd;
fs/notify/notification.c +3 -0
index 066f1f9..7fc8d00 100644
@@ -93,6 +93,7 @@ void fsnotify_put_event(struct fsnotify_event *event)
 		BUG_ON(!list_empty(&event->private_data_list));
 
 		kfree(event->file_name);
+		put_pid(event->tgid);
 		kmem_cache_free(fsnotify_event_cachep, event);
 	}
 }
@@ -346,6 +347,7 @@ struct fsnotify_event *fsnotify_clone_event(struct fsnotify_event *old_event)
 			return NULL;
 		}
 	}
+	event->tgid = get_pid(old_event->tgid);
 	if (event->data_type == FSNOTIFY_EVENT_PATH)
 		path_get(&event->path);
 
@@ -385,6 +387,7 @@ struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u32 mask,
 		event->name_len = strlen(event->file_name);
 	}
 
+	event->tgid = get_pid(task_tgid(current));
 	event->sync_cookie = cookie;
 	event->to_tell = to_tell;
 	event->data_type = data_type;
include/linux/fanotify.h +1 -0
index c1c6616..5f633af 100644
@@ -62,6 +62,7 @@ struct fanotify_event_metadata {
 	__u32 vers;
 	__s32 fd;
 	__u64 mask;
+	__s64 pid;
 } __attribute__ ((packed));
 
 /* Helper functions to deal with fanotify_event_metadata buffers */
include/linux/fsnotify_backend.h +1 -0
index ff654c1..7d93572 100644
@@ -221,6 +221,7 @@ struct fsnotify_event {
 	u32 sync_cookie;	/* used to corrolate events, namely inotify mv events */
 	char *file_name;
 	size_t name_len;
+	struct pid *tgid;
 
 	struct list_head private_data_list;	/* groups can store private data here */
 };

A message that is not in the archive (sent off-list or not yet received): 20091202141458.12819.96170.stgit-E+B5uJFuEZf0UfVguI6niVaTQe2KTcn/@public.gmane.org

#3 lore raw

Re: [PATCH 2/4] fanotify: Add pids to events

Quoted 37 lines · Eric Paris

From: Andreas Gruenbacher <[email protected]>

Pass the process identifiers of the triggering processes to fanotify
listeners: this information is useful for event filtering and logging.

---

 fs/notify/fanotify/fanotify.c      |    5 +++--
 fs/notify/fanotify/fanotify_user.c |    1 +
 fs/notify/notification.c           |    3 +++
 include/linux/fanotify.h           |    1 +
 include/linux/fsnotify_backend.h   |    1 +

 5 files changed, 9 insertions(+), 2 deletions(-)
fs/notify/fanotify/fanotify.c +3 -2
index 5b0b6b4..881067d 100644
@@ -10,8 +10,9 @@ static bool should_merge(struct fsnotify_event *old, struct fsnotify_event *new)
  {
        pr_debug("%s: old=%p new=%p\n", __func__, old, new);
 
-       if ((old->to_tell == new->to_tell) &&
-           (old->data_type == new->data_type)) {
+       if (old->to_tell == new->to_tell &&
+           old->data_type == new->data_type &&
+           old->tgid == new->tgid) {
                switch (old->data_type) {

               case (FSNOTIFY_EVENT_PATH):
                       if ((old->path.mnt == new->path.mnt) &&

fs/notify/fanotify/fanotify_user.c +0 -0
index 7646111..7cfb2f6 100644

fs/notify/fanotify/fanotify_user.c static ssize_t fill_event_metadata(struct fsnotify_group *group, · line 111

Quoted 4 lines
fs/notify/fanotify/fanotify_user.c +1 -0
@@ -108,6 +108,7 @@ static ssize_t fill_event_metadata(struct fsnotify_group *group,
        metadata->event_len = FAN_EVENT_METADATA_LEN;
        metadata->vers = FANOTIFY_METADATA_VERSION;
        metadata->mask = fanotify_outgoing_mask(event->mask);
+       metadata->pid = pid_vnr(event->tgid);

Eric, you never replied to my point about pid namespaces
(http://lkml.org/lkml/2009/7/1/2). I'm still concerned that it's a problem for this patch. I've cc'd some pid namespace folks, listed the problems, and some alternative solutions (where I could think of any) below:

1. Since fanotify doesn't hold a reference to the struct pid then the pid can become stale before the event is acted upon. solution a: Just ignoring this problem, like other interfaces
often do, is probably ok.
... ?
solution z: Seems to require taking a reference to the pid and
giving userspace a way to drop the reference after it's done using this value to refer to the process (yuck).

2. If the event recipient does a clone and enters a new pidns the pid number will be incorrect without any indication.
solution a: The events could be flushed during clone(CLONE_NEWPID)
but then userspace would miss
them.
solution b: Translating the pids wouldn't work because the tasks
generaly won't exist in the new
namespace. So perhaps just write negative values in the pid
fields during clone(CLONE_NEWPID).

3. If the listening process is not in the same or an ancestor pid namespace of the triggering process then there is no correct pid corresponding to the event. (Note: Always using the initial pid namespace isn't a good [interim] solution -- then programs relying on fanotify couldn't run in other pid namespaces).
solution a: Again, perhaps they should be set to negative values. ... ?
solution z: Disable fanotify in non-initial pid namespaces (yuck).

Cheers,
-Matt Helsley

#4 lore raw

Re: [PATCH 2/4] fanotify: Add pids to events

In reply to Eric Paris
Quoted 37 lines · Eric Paris

From: Andreas Gruenbacher <[email protected]>

Pass the process identifiers of the triggering processes to fanotify
listeners: this information is useful for event filtering and logging.

---

 fs/notify/fanotify/fanotify.c      |    5 +++--
 fs/notify/fanotify/fanotify_user.c |    1 +
 fs/notify/notification.c           |    3 +++
 include/linux/fanotify.h           |    1 +
 include/linux/fsnotify_backend.h   |    1 +

 5 files changed, 9 insertions(+), 2 deletions(-)
fs/notify/fanotify/fanotify.c +3 -2
index 5b0b6b4..881067d 100644
@@ -10,8 +10,9 @@ static bool should_merge(struct fsnotify_event *old, struct fsnotify_event *new)
  {
        pr_debug("%s: old=%p new=%p\n", __func__, old, new);
 
-       if ((old->to_tell == new->to_tell) &&
-           (old->data_type == new->data_type)) {
+       if (old->to_tell == new->to_tell &&
+           old->data_type == new->data_type &&
+           old->tgid == new->tgid) {
                switch (old->data_type) {

               case (FSNOTIFY_EVENT_PATH):
                       if ((old->path.mnt == new->path.mnt) &&

fs/notify/fanotify/fanotify_user.c +0 -0
index 7646111..7cfb2f6 100644

fs/notify/fanotify/fanotify_user.c static ssize_t fill_event_metadata(struct fsnotify_group *group, · line 111

Quoted 4 lines
fs/notify/fanotify/fanotify_user.c +1 -0
@@ -108,6 +108,7 @@ static ssize_t fill_event_metadata(struct fsnotify_group *group,
        metadata->event_len = FAN_EVENT_METADATA_LEN;
        metadata->vers = FANOTIFY_METADATA_VERSION;
        metadata->mask = fanotify_outgoing_mask(event->mask);
+       metadata->pid = pid_vnr(event->tgid);

Eric, you never replied to my point about pid namespaces
(http://lkml.org/lkml/2009/7/1/2). I'm still concerned that it's a problem for this patch. I've cc'd some pid namespace folks, listed the problems, and some alternative solutions (where I could think of any) below:

1. Since fanotify doesn't hold a reference to the struct pid then the pid can become stale before the event is acted upon. solution a: Just ignoring this problem, like other interfaces
often do, is probably ok.
... ?
solution z: Seems to require taking a reference to the pid and
giving userspace a way to drop the reference after it's done using this value to refer to the process (yuck).

2. If the event recipient does a clone and enters a new pidns the pid number will be incorrect without any indication.
solution a: The events could be flushed during clone(CLONE_NEWPID)
but then userspace would miss
them.
solution b: Translating the pids wouldn't work because the tasks
generaly won't exist in the new
namespace. So perhaps just write negative values in the pid
fields during clone(CLONE_NEWPID).

3. If the listening process is not in the same or an ancestor pid namespace of the triggering process then there is no correct pid corresponding to the event. (Note: Always using the initial pid namespace isn't a good [interim] solution -- then programs relying on fanotify couldn't run in other pid namespaces).
solution a: Again, perhaps they should be set to negative values. ... ?
solution z: Disable fanotify in non-initial pid namespaces (yuck).

Cheers,
-Matt Helsley
--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw

Re: [PATCH 2/4] fanotify: Add pids to events

Quoted 6 lines · On Friday 15 January 2010 05:41:10 Matthew Helsley wrote:

Eric, you never replied to my point about pid namespaces
(http://lkml.org/lkml/2009/7/1/2). I'm still concerned that it's a problem for this patch. I've cc'd some pid namespace folks, listed the problems, and some alternative solutions (where I could think of any) below:

Quoted 8 lines

1. Since fanotify doesn't hold a reference to the struct pid then the pid can become stale before the event is acted upon. solution a: Just ignoring this problem, like other interfaces
often do, is probably ok.
... ?
solution z: Seems to require taking a reference to the pid and
giving userspace a way to drop the reference after it's done using this value to refer to the process (yuck).

struct fsnotify_event->tgid does hold a reference to the appropriate struct pid. The reference is released when that struct fsnotify_event is freed.

Quoted 2 lines

2. If the event recipient does a clone and enters a new pidns the pid number will be incorrect without any indication.

No, if a process has a pid within the listener's namespace the listener will see this pid; otherwise, the resulting pid value is 0.

Quoted 3 lines

3. If the listening process is not in the same or an ancestor pid namespace of the triggering process then there is no correct pid corresponding to the event.

Indeed, if the listener is not in the same or an ancestor pid namespace, the pid in the event will end up as 0. The event still indicates that something has happened to a file the listener is interested in though, it's just unclear who triggered the event. I don't see a problem with that though -- do you?

Thanks,
Andreas

A message that is not in the archive (sent off-list or not yet received): [email protected]

#7 lore raw

Re: [PATCH 2/4] fanotify: Add pids to events

Quoted 16 lines · Andreas Gruenbacher
Quoted 6 lines · On Friday 15 January 2010 05:41:10 Matthew Helsley wrote:

Eric, you never replied to my point about pid namespaces
(http://lkml.org/lkml/2009/7/1/2). I'm still concerned that it's a problem for this patch. I've cc'd some pid namespace folks, listed the problems, and some alternative solutions (where I could think of any) below:

Quoted 8 lines

1. Since fanotify doesn't hold a reference to the struct pid then the pid can become stale before the event is acted upon. solution a: Just ignoring this problem, like other interfaces
often do, is probably ok.
... ?
solution z: Seems to require taking a reference to the pid and
giving userspace a way to drop the reference after it's done using this value to refer to the process (yuck).

Quoted 2 lines

struct fsnotify_event->tgid does hold a reference to the appropriate struct pid. The reference is released when that struct fsnotify_event is freed.

OK.

Quoted 6 lines
Quoted 3 lines

2. If the event recipient does a clone and enters a new pidns the pid number will be incorrect without any indication.

No, if a process has a pid within the listener's namespace the listener will see this pid; otherwise, the resulting pid value is 0.

So the pid reference is resolved at read(), correct? If so then that's fine. (Otherwise I'd think the values could still become stale).

Quoted 8 lines
Quoted 3 lines

3. If the listening process is not in the same or an ancestor pid namespace of the triggering process then there is no correct pid corresponding to the event.

Indeed, if the listener is not in the same or an ancestor pid namespace, the pid in the event will end up as 0. The event still indicates that something has happened to a file the listener is interested in though, it's just unclear who triggered the event. I don't see a problem with that though -- do you?

Nope. Overall, looks good to me. Thanks!

Cheers,
-Matt Helsley

#8 lore raw

Re: [PATCH 2/4] fanotify: Add pids to events

In reply to Andreas Gruenbacher
Quoted 16 lines · Andreas Gruenbacher
Quoted 6 lines · On Friday 15 January 2010 05:41:10 Matthew Helsley wrote:

Eric, you never replied to my point about pid namespaces
(http://lkml.org/lkml/2009/7/1/2). I'm still concerned that it's a problem for this patch. I've cc'd some pid namespace folks, listed the problems, and some alternative solutions (where I could think of any) below:

Quoted 8 lines

1. Since fanotify doesn't hold a reference to the struct pid then the pid can become stale before the event is acted upon. solution a: Just ignoring this problem, like other interfaces
often do, is probably ok.
... ?
solution z: Seems to require taking a reference to the pid and
giving userspace a way to drop the reference after it's done using this value to refer to the process (yuck).

Quoted 2 lines

struct fsnotify_event->tgid does hold a reference to the appropriate struct pid. The reference is released when that struct fsnotify_event is freed.

OK.

Quoted 6 lines
Quoted 3 lines

2. If the event recipient does a clone and enters a new pidns the pid number will be incorrect without any indication.

No, if a process has a pid within the listener's namespace the listener will see this pid; otherwise, the resulting pid value is 0.

So the pid reference is resolved at read(), correct? If so then that's fine. (Otherwise I'd think the values could still become stale).

Quoted 8 lines
Quoted 3 lines

3. If the listening process is not in the same or an ancestor pid namespace of the triggering process then there is no correct pid corresponding to the event.

Indeed, if the listener is not in the same or an ancestor pid namespace, the pid in the event will end up as 0. The event still indicates that something has happened to a file the listener is interested in though, it's just unclear who triggered the event. I don't see a problem with that though -- do you?

Nope. Overall, looks good to me. Thanks!

Cheers,
-Matt Helsley

#9 lore raw

Re: [PATCH 2/4] fanotify: Add pids to events

Quoted 9 lines · Matt Helsley
Quoted 5 lines · Andreas Gruenbacher
Quoted 2 lines

2. If the event recipient does a clone and enters a new pidns the pid number will be incorrect without any indication.

No, if a process has a pid within the listener's namespace the listener will see this pid; otherwise, the resulting pid value is 0.

So the pid reference is resolved at read(), correct? If so then that's
fine. (Otherwise I'd think the values could still become stale).

Yes. Note that for non-blocking events, there is no guarantee that the triggering process still runs when the event is consumed though.

Andreas

#10 lore raw

Re: [PATCH 2/4] fanotify: Add pids to events

Quoted 5 lines · Andreas Gruenbacher
Quoted 4 lines · Matt Helsley
Quoted 3 lines · Andreas Gruenbacher
Quoted 2 lines

2. If the event recipient does a clone and enters a new pidns the pid number will be incorrect without any indication.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

No, if a process has a pid within the listener's namespace the listener will see this pid; otherwise, the resulting pid value is 0.

So the pid reference is resolved at read(), correct? If so then that's
fine. (Otherwise I'd think the values could still become stale).

Yes. Note that for non-blocking events, there is no guarantee that the triggering process still runs when the event is consumed though.

Holding the reference until the event is read ensures the pid won't be reused in that namespace, so I think that's fine.

Cheers,
-Matt Helsley

A message that is not in the archive (sent off-list or not yet received): [email protected]

#12 lore raw

Re: [PATCH 2/4] fanotify: Add pids to events

Quoted 5 lines · Andreas Gruenbacher
Quoted 4 lines · Matt Helsley
Quoted 3 lines · Andreas Gruenbacher
Quoted 2 lines

2. If the event recipient does a clone and enters a new pidns the pid number will be incorrect without any indication.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

No, if a process has a pid within the listener's namespace the listener will see this pid; otherwise, the resulting pid value is 0.

So the pid reference is resolved at read(), correct? If so then that's
fine. (Otherwise I'd think the values could still become stale).

Yes. Note that for non-blocking events, there is no guarantee that the triggering process still runs when the event is consumed though.

Holding the reference until the event is read ensures the pid won't be reused in that namespace, so I think that's fine.

Cheers,
-Matt Helsley

A message that is not in the archive (sent off-list or not yet received): 20100115212146.GA3714-52DBMbEzqgQ/wnmkkaCWp/[email protected]

#14 lore raw

Re: [PATCH 2/4] fanotify: Add pids to events

Quoted 9 lines · Matt Helsley
Quoted 5 lines · Andreas Gruenbacher
Quoted 2 lines

2. If the event recipient does a clone and enters a new pidns the pid number will be incorrect without any indication.

No, if a process has a pid within the listener's namespace the listener will see this pid; otherwise, the resulting pid value is 0.

So the pid reference is resolved at read(), correct? If so then that's
fine. (Otherwise I'd think the values could still become stale).

Yes. Note that for non-blocking events, there is no guarantee that the triggering process still runs when the event is consumed though.

Andreas

author maintainer
#15 lore raw

PATCH 3/4 fsnotify: split generic and inode specific mark code

In reply to Eric Paris

currently all marking is done by functions in inode-mark.c. Some of this is pretty generic and should be instead done in a generic function and we should only put the inode specific code in inode-mark.c

---

 fs/notify/Makefile                   |    3 
 fs/notify/dnotify/dnotify.c          |   12 +
 fs/notify/fanotify/fanotify.c        |    2 
 fs/notify/fanotify/fanotify_user.c   |    8 -
 fs/notify/fsnotify.h                 |    7 +
 fs/notify/inode_mark.c               |  246 ++--------------------------
 fs/notify/inotify/inotify_fsnotify.c |    4 
 fs/notify/inotify/inotify_user.c     |    4 
 fs/notify/mark.c                     |  294 ++++++++++++++++++++++++++++++++++
 include/linux/fsnotify_backend.h     |    5 -
 kernel/audit_tree.c                  |    8 -
 kernel/audit_watch.c                 |    6 -
 12 files changed, 347 insertions(+), 252 deletions(-)
 create mode 100644 fs/notify/mark.c
fs/notify/Makefile +2 -1
index 396a387..8f7f3b0 100644
@@ -1,4 +1,5 @@
-obj-$(CONFIG_FSNOTIFY)		+= fsnotify.o notification.o group.o inode_mark.o
+obj-$(CONFIG_FSNOTIFY)		+= fsnotify.o notification.o group.o inode_mark.o \
+				   mark.o
 
 obj-y			+= dnotify/
 obj-y			+= inotify/
fs/notify/dnotify/dnotify.c +6 -6
index cac2eb8..69f42df 100644
@@ -95,7 +95,7 @@ static int dnotify_handle_event(struct fsnotify_group *group,
 
 	to_tell = event->to_tell;
 
-	fsn_mark = fsnotify_find_mark(group, to_tell);
+	fsn_mark = fsnotify_find_inode_mark(group, to_tell);
 	if (unlikely(!fsn_mark))
 		return 0;
 	dn_mark = container_of(fsn_mark, struct dnotify_mark, fsn_mark);
@@ -143,14 +143,14 @@ static bool dnotify_should_send_event(struct fsnotify_group *group,
 	if (!S_ISDIR(inode->i_mode))
 		return false;
 
-	fsn_mark = fsnotify_find_mark(group, inode);
+	fsn_mark = fsnotify_find_inode_mark(group, inode);
 	if (!fsn_mark)
 		return false;
 
 	mask = (mask & ~FS_EVENT_ON_CHILD);
 	send = (mask & fsn_mark->mask);
 
-	fsnotify_put_mark(fsn_mark); /* matches fsnotify_find_mark */
+	fsnotify_put_mark(fsn_mark); /* matches fsnotify_find_inode_mark */
 
 	return send;
 }
@@ -193,7 +193,7 @@ void dnotify_flush(struct file *filp, fl_owner_t id)
 	if (!S_ISDIR(inode->i_mode))
 		return;
 
-	fsn_mark = fsnotify_find_mark(dnotify_group, inode);
+	fsn_mark = fsnotify_find_inode_mark(dnotify_group, inode);
 	if (!fsn_mark)
 		return;
 	dn_mark = container_of(fsn_mark, struct dnotify_mark, fsn_mark);
@@ -346,12 +346,12 @@ int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg)
 	mutex_lock(&dnotify_mark_mutex);
 
 	/* add the new_fsn_mark or find an old one. */
-	fsn_mark = fsnotify_find_mark(dnotify_group, inode);
+	fsn_mark = fsnotify_find_inode_mark(dnotify_group, inode);
 	if (fsn_mark) {
 		dn_mark = container_of(fsn_mark, struct dnotify_mark, fsn_mark);
 		spin_lock(&fsn_mark->lock);
 	} else {
-		fsnotify_add_mark(new_fsn_mark, dnotify_group, inode, 0);
+		fsnotify_add_mark(new_fsn_mark, dnotify_group, inode, NULL, 0);
 		spin_lock(&new_fsn_mark->lock);
 		fsn_mark = new_fsn_mark;
 		dn_mark = new_dn_mark;
fs/notify/fanotify/fanotify.c +1 -1
index 881067d..aa5e926 100644
@@ -118,7 +118,7 @@ static bool fanotify_should_send_event(struct fsnotify_group *group, struct inod
 	if (data_type != FSNOTIFY_EVENT_PATH)
 		return false;
 
-	fsn_mark = fsnotify_find_mark(group, inode);
+	fsn_mark = fsnotify_find_inode_mark(group, inode);
 	if (!fsn_mark)
 		return false;
 
fs/notify/fanotify/fanotify_user.c +4 -4
index 7cfb2f6..79376ec 100644
@@ -310,7 +310,7 @@ static int fanotify_remove_mark(struct fsnotify_group *group,
 	pr_debug("%s: group=%p inode=%p mask=%x\n", __func__,
 		 group, inode, mask);
 
-	fsn_mark = fsnotify_find_mark(group, inode);
+	fsn_mark = fsnotify_find_inode_mark(group, inode);
 	if (!fsn_mark)
 		return -ENOENT;
 
@@ -326,7 +326,7 @@ static int fanotify_remove_mark(struct fsnotify_group *group,
 
 	fsnotify_recalc_group_mask(group);
 
-	/* matches the fsnotify_find_mark() */
+	/* matches the fsnotify_find_inode_mark() */
 	fsnotify_put_mark(fsn_mark);
 
 	return 0;
@@ -343,7 +343,7 @@ static int fanotify_add_mark(struct fsnotify_group *group,
 	pr_debug("%s: group=%p inode=%p mask=%x\n", __func__,
 		 group, inode, mask);
 
-	fsn_mark = fsnotify_find_mark(group, inode);
+	fsn_mark = fsnotify_find_inode_mark(group, inode);
 	if (!fsn_mark) {
 		struct fsnotify_mark *new_fsn_mark;
 
@@ -353,7 +353,7 @@ static int fanotify_add_mark(struct fsnotify_group *group,
 			goto out;
 
 		fsnotify_init_mark(new_fsn_mark, fanotify_free_mark);
-		ret = fsnotify_add_mark(new_fsn_mark, group, inode, 0);
+		ret = fsnotify_add_mark(new_fsn_mark, group, inode, NULL, 0);
 		if (ret) {
 			fanotify_free_mark(new_fsn_mark);
 			goto out;
fs/notify/fsnotify.h +7 -0
index 2ba5915..7c7a904 100644
@@ -20,6 +20,11 @@ extern __u32 fsnotify_vfsmount_mask;
 /* destroy all events sitting in this groups notification queue */
 extern void fsnotify_flush_notify(struct fsnotify_group *group);
 
+/* add a mark to an inode */
+extern int fsnotify_add_inode_mark(struct fsnotify_mark *mark,
+				   struct fsnotify_group *group, struct inode *inode,
+				   int allow_dups);
+
 /* add a group to the inode group list */
 extern void fsnotify_add_inode_group(struct fsnotify_group *group);
 /* add a group to the vfsmount group list */
@@ -27,6 +32,8 @@ extern void fsnotify_add_vfsmount_group(struct fsnotify_group *group);
 /* final kfree of a group */
 extern void fsnotify_final_destroy_group(struct fsnotify_group *group);
 
+/* inode specific destruction of a mark */
+extern void fsnotify_destroy_inode_mark(struct fsnotify_mark *mark);
 /* run the list of all marks associated with inode and flag them to be freed */
 extern void fsnotify_clear_marks_by_inode(struct inode *inode);
 /*
fs/notify/inode_mark.c +19 -227

327 lines not on this page · raw message

fs/notify/inotify/inotify_fsnotify.c +2 -2
index 3a20053..bd2468d 100644
@@ -96,7 +96,7 @@ static int inotify_handle_event(struct fsnotify_group *group, struct fsnotify_ev
 
 	to_tell = event->to_tell;
 
-	fsn_mark = fsnotify_find_mark(group, to_tell);
+	fsn_mark = fsnotify_find_inode_mark(group, to_tell);
 	/* race with watch removal?  We already passes should_send */
 	if (unlikely(!fsn_mark))
 		return 0;
@@ -144,7 +144,7 @@ static bool inotify_should_send_event(struct fsnotify_group *group, struct inode
 	struct fsnotify_mark *fsn_mark;
 	bool send;
 
-	fsn_mark = fsnotify_find_mark(group, inode);
+	fsn_mark = fsnotify_find_inode_mark(group, inode);
 	if (!fsn_mark)
 		return false;
 
fs/notify/inotify/inotify_user.c +2 -2
index 4d1c0f6..1ba7c86 100644
@@ -571,7 +571,7 @@ static int inotify_update_existing_watch(struct fsnotify_group *group,
 	if (unlikely(!mask))
 		return -EINVAL;
 
-	fsn_mark = fsnotify_find_mark(group, inode);
+	fsn_mark = fsnotify_find_inode_mark(group, inode);
 	if (!fsn_mark)
 		return -ENOENT;
 
@@ -649,7 +649,7 @@ static int inotify_new_watch(struct fsnotify_group *group,
 		goto out_err;
 
 	/* we are on the idr, now get on the inode */
-	ret = fsnotify_add_mark(&tmp_i_mark->fsn_mark, group, inode, 0);
+	ret = fsnotify_add_mark(&tmp_i_mark->fsn_mark, group, inode, NULL, 0);
 	if (ret) {
 		/* we failed to get on the inode, get off the idr */
 		inotify_remove_from_idr(group, tmp_i_mark);
fs/notify/mark.c new file +294 -0

294 lines not on this page · raw message

include/linux/fsnotify_backend.h +3 -2
index 7d93572..27cccbe 100644
@@ -364,11 +364,12 @@ extern struct fsnotify_event *fsnotify_remove_notify_event(struct fsnotify_group
 extern void fsnotify_recalc_inode_mask(struct inode *inode);
 extern void fsnotify_init_mark(struct fsnotify_mark *mark, void (*free_mark)(struct fsnotify_mark *mark));
 /* find (and take a reference) to a mark associated with group and inode */
-extern struct fsnotify_mark *fsnotify_find_mark(struct fsnotify_group *group, struct inode *inode);
+extern struct fsnotify_mark *fsnotify_find_inode_mark(struct fsnotify_group *group, struct inode *inode);
 /* copy the values from old into new */
 extern void fsnotify_duplicate_mark(struct fsnotify_mark *new, struct fsnotify_mark *old);
 /* attach the mark to both the group and the inode */
-extern int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group, struct inode *inode, int allow_dups);
+extern int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group,
+			     struct inode *inode, struct vfsmount *mnt, int allow_dups);
 /* given a mark, flag it to be freed when all references are dropped */
 extern void fsnotify_destroy_mark(struct fsnotify_mark *mark);
 /* run all the marks in a group, and flag them to be freed */
kernel/audit_tree.c +4 -4
index 001f4e7..1de235a 100644
@@ -258,7 +258,7 @@ static void untag_chunk(struct node *p)
 	if (!new)
 		goto Fallback;
 	fsnotify_duplicate_mark(&new->mark, entry);
-	if (fsnotify_add_mark(&new->mark, new->mark.group, new->mark.i.inode, 1)) {
+	if (fsnotify_add_mark(&new->mark, new->mark.group, new->mark.i.inode, NULL, 1)) {
 		free_chunk(new);
 		goto Fallback;
 	}
@@ -321,7 +321,7 @@ static int create_chunk(struct inode *inode, struct audit_tree *tree)
 		return -ENOMEM;
 
 	entry = &chunk->mark;
-	if (fsnotify_add_mark(entry, audit_tree_group, inode, 0)) {
+	if (fsnotify_add_mark(entry, audit_tree_group, inode, NULL, 0)) {
 		free_chunk(chunk);
 		return -ENOSPC;
 	}
@@ -359,7 +359,7 @@ static int tag_chunk(struct inode *inode, struct audit_tree *tree)
 	struct node *p;
 	int n;
 
-	old_entry = fsnotify_find_mark(audit_tree_group, inode);
+	old_entry = fsnotify_find_inode_mark(audit_tree_group, inode);
 	if (!old_entry)
 		return create_chunk(inode, tree);
 
@@ -391,7 +391,7 @@ static int tag_chunk(struct inode *inode, struct audit_tree *tree)
 	}
 
 	fsnotify_duplicate_mark(chunk_entry, old_entry);
-	if (fsnotify_add_mark(chunk_entry, chunk_entry->group, chunk_entry->i.inode, 1)) {
+	if (fsnotify_add_mark(chunk_entry, chunk_entry->group, chunk_entry->i.inode, NULL, 1)) {
 		spin_unlock(&old_entry->lock);
 		free_chunk(chunk);
 		fsnotify_put_mark(old_entry);
kernel/audit_watch.c +3 -3
index e8023ad..31f9be8 100644
@@ -100,7 +100,7 @@ static inline struct audit_parent *audit_find_parent(struct inode *inode)
 	struct audit_parent *parent = NULL;
 	struct fsnotify_mark *entry;
 
-	entry = fsnotify_find_mark(audit_watch_group, inode);
+	entry = fsnotify_find_inode_mark(audit_watch_group, inode);
 	if (entry)
 		parent = container_of(entry, struct audit_parent, mark);
 
@@ -157,7 +157,7 @@ static struct audit_parent *audit_init_parent(struct nameidata *ndp)
 
 	fsnotify_init_mark(&parent->mark, audit_watch_free_mark);
 	parent->mark.mask = AUDIT_FS_WATCH;
-	ret = fsnotify_add_mark(&parent->mark, audit_watch_group, inode, 0);
+	ret = fsnotify_add_mark(&parent->mark, audit_watch_group, inode, NULL, 0);
 	if (ret < 0) {
 		audit_free_parent(parent);
 		return ERR_PTR(ret);
@@ -516,7 +516,7 @@ static bool audit_watch_should_send_event(struct fsnotify_group *group, struct i
 	struct fsnotify_mark *entry;
 	bool send;
 
-	entry = fsnotify_find_mark(group, inode);
+	entry = fsnotify_find_inode_mark(group, inode);
 	if (!entry)
 		return false;
 
author maintainer
#16 lore raw

PATCH 4/4 fsnotify: clear marks to 0 in fsnotify_init_mark

In reply to Eric Paris

Currently fsnotify_init_mark sets some fields to 0/NULL. Some users already used some sorts of zalloc, some didn't. This patch uses memset to explicitly zero everything in the fsnotify_mark when it is initialized so we don't have to be careful if fields are later added to marks.

---

 fs/notify/mark.c |    5 +----
 1 files changed, 1 insertions(+), 4 deletions(-)
fs/notify/mark.c +1 -4
index e56e876..57bb1d7 100644
@@ -284,11 +284,8 @@ void fsnotify_duplicate_mark(struct fsnotify_mark *new, struct fsnotify_mark *ol
 void fsnotify_init_mark(struct fsnotify_mark *mark,
 			void (*free_mark)(struct fsnotify_mark *mark))
 {
+	memset(mark, 0, sizeof(*mark));
 	spin_lock_init(&mark->lock);
 	atomic_set(&mark->refcnt, 1);
-	INIT_HLIST_NODE(&mark->i.i_list);
-	mark->group = NULL;
-	mark->mask = 0;
-	mark->i.inode = NULL;
 	mark->free_mark = free_mark;
 }