Skip to contentSkip to conversation

fs: don't remove inotify watchers from alive inode-s (v2)

PATCH In review Andrey Vagin 4 participants 9 messages Sep 19, 2014 – Oct 2, 2014 VFS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs: don't remove inotify watchers from alive inode-s (v2)

Currently watchers are removed in dentry_iput(), if n_link is zero. But other detries can be linked with this inode.

For example if we create two hard links, open the first one and set an inotify watcher on one of them. Then if we remove the opened file and then another file, the inotify watcher will be removed. But we will have the alive file descriptor, which allows us to generate more events.

And here is another behaviour, if files are removed in another order. The watcher will not be removed and we will keep getting inotify events for that inode.

This patch removes difference of behaviours for these cases. Watchers are removed, only if nlink is zero and i_dentry list is empty. The resulting behaviour is the same with what has been described in the second case.

Look at a following example:

	fd = inotify_init1(IN_NONBLOCK);
	deleted = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0666);
	link(path, path_link);

	wd_deleted = inotify_add_watch(fd, path_link, IN_ALL_EVENTS);

	unlink(path);
	unlink(path_link);

	printf(" --- unlink path, path_link\n");
	read_evetns(fd);

	close(deleted);
	printf(" --- close\n");
	read_evetns(fd);
	printf(" --- end\n");

We expect to get the same set of events for this case and for the case, when files are deleted in another order. But now we get the different set of events.

Without this patch:
The first case, when "path" is deleted before "path_link"
--- unlink path, path_link
4 (IN_ATTRIB)
400 (IN_DELETE_SELF)
8000 (IN_IGNORED)
--- close
--- end

and for the case, when "path_link" is deleted before "path"
--- unlink path_link, path
4 (IN_ATTRIB)
--- close
8 (IN_CLOSE_WRITE)
400 (IN_DELETE_SELF)
8000 (IN_IGNORED)
--- end

With this patch we have the same output for both cases:
--- unlink
4 (IN_ATTRIB)
--- close
8 (IN_CLOSE_WRITE)
400 (IN_DELETE_SELF)
8000 (IN_IGNORED)
--- end
PASS

v2: generate IN_DELETE_SELF when the last link to the file is removed

Cc "Michael Kerrisk (man-pages)" <[email protected]>
Cc Heinrich Schuchardt <[email protected]>
Cc Alexander Viro <[email protected]>
Cc John McCutchan <[email protected]>
Cc Robert Love <[email protected]>
Cc Eric Paris <[email protected]>
Cc Cyrill Gorcunov <[email protected]>
Cc Pavel Emelyanov <[email protected]>
---
 fs/dcache.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
fs/dcache.c +8 -2
index 7a5b514..3a0e3bc 100644
@@ -278,12 +278,15 @@ static void dentry_iput(struct dentry * dentry)
 	__releases(dentry->d_inode->i_lock)
 {
 	struct inode *inode = dentry->d_inode;
+	bool last_dentry;
+
 	if (inode) {
 		dentry->d_inode = NULL;
 		hlist_del_init(&dentry->d_alias);
+		last_dentry = hlist_empty(&inode->i_dentry);
 		spin_unlock(&dentry->d_lock);
 		spin_unlock(&inode->i_lock);
-		if (!inode->i_nlink)
+		if (!inode->i_nlink && last_dentry)
 			fsnotify_inoderemove(inode);
 		if (dentry->d_op && dentry->d_op->d_iput)
 			dentry->d_op->d_iput(dentry, inode);
@@ -303,13 +306,16 @@ static void dentry_unlink_inode(struct dentry * dentry)
 	__releases(dentry->d_inode->i_lock)
 {
 	struct inode *inode = dentry->d_inode;
+	bool last_dentry;
+
 	__d_clear_type(dentry);
 	dentry->d_inode = NULL;
 	hlist_del_init(&dentry->d_alias);
 	dentry_rcuwalk_barrier(dentry);
+	last_dentry = hlist_empty(&inode->i_dentry);
 	spin_unlock(&dentry->d_lock);
 	spin_unlock(&inode->i_lock);
-	if (!inode->i_nlink)
+	if (!inode->i_nlink && last_dentry)
 		fsnotify_inoderemove(inode);
 	if (dentry->d_op && dentry->d_op->d_iput)
 		dentry->d_op->d_iput(dentry, inode);
Signature · 1 line
1.9.3
#1 lore raw

PATCH 1/1 inotify.7: describe ambiguous behaviour of IN_DELETE_SELF

Since kernel 3.18, IN_DELETE_SELF occurs only if nobody uses an object. Before, if we have two hardlinks where one of them is opened and we delete the opened file before the second one, we receive IN_DELETE_SELF. It's wrong, because the file remains opened. If files are deleted in another order, we don't receive IN_DELETE_SELF as expected.

This patch can be committed only after the kernel patch:
"[PATCH] fs: don't remove inotify watchers from alive inode-s (v2)"

---
 man7/inotify.7 | 13 +++++++++++++
 1 file changed, 13 insertions(+)
man7/inotify.7 +13 -0
index e1cabe2..b7a6e2f 100644
@@ -504,6 +504,19 @@ if the older had not yet been read)
 instead checked if the most recent event could be coalesced with the
 .I oldest
 unread event.
+
+Since kernel 3.18,
+.\" commit XXXXXX
+.B IN_DELETE_SELF
+occurs only if nobody uses an object. Before, if we have two hardlinks where
+one of them is opened and we delete the opened file before the second one, we
+receive
+.B IN_DELETE_SELF.
+It's wrong, because the file remains opened. If files are deleted in another
+order, we don't receive
+.B IN_DELETE_SELF
+as expected.
+
 .SH SEE ALSO
 .BR inotifywait (1),
 .BR inotifywatch (1),
Signature · 1 line
1.9.3
reviewer
#3 lore raw
In reply to Andrey Vagin

Hello,

Andrew, what do you think about the patch below? Al objected that it
changes userspace visible behavior some time ago and then he didn't react to our explanations...
Honza

Quoted 126 lines · Andrey Vagin

Currently watchers are removed in dentry_iput(), if n_link is zero. But other detries can be linked with this inode.

For example if we create two hard links, open the first one and set an inotify watcher on one of them. Then if we remove the opened file and then another file, the inotify watcher will be removed. But we will have the alive file descriptor, which allows us to generate more events.

And here is another behaviour, if files are removed in another order. The watcher will not be removed and we will keep getting inotify events for that inode.

This patch removes difference of behaviours for these cases. Watchers are removed, only if nlink is zero and i_dentry list is empty. The resulting behaviour is the same with what has been described in the second case.

Look at a following example:

	fd = inotify_init1(IN_NONBLOCK);
	deleted = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0666);
	link(path, path_link);

	wd_deleted = inotify_add_watch(fd, path_link, IN_ALL_EVENTS);

	unlink(path);
	unlink(path_link);

	printf(" --- unlink path, path_link\n");
	read_evetns(fd);

	close(deleted);
	printf(" --- close\n");
	read_evetns(fd);
	printf(" --- end\n");

We expect to get the same set of events for this case and for the case, when files are deleted in another order. But now we get the different set of events.

Without this patch:
The first case, when "path" is deleted before "path_link"
--- unlink path, path_link
4 (IN_ATTRIB)
400 (IN_DELETE_SELF)
8000 (IN_IGNORED)
--- close
--- end

and for the case, when "path_link" is deleted before "path"
--- unlink path_link, path
4 (IN_ATTRIB)
--- close
8 (IN_CLOSE_WRITE)
400 (IN_DELETE_SELF)
8000 (IN_IGNORED)
--- end

With this patch we have the same output for both cases:
--- unlink
4 (IN_ATTRIB)
--- close
8 (IN_CLOSE_WRITE)
400 (IN_DELETE_SELF)
8000 (IN_IGNORED)
--- end
PASS

v2: generate IN_DELETE_SELF when the last link to the file is removed

Cc "Michael Kerrisk (man-pages)" <[email protected]>
Cc Heinrich Schuchardt <[email protected]>
Cc Alexander Viro <[email protected]>
Cc John McCutchan <[email protected]>
Cc Robert Love <[email protected]>
Cc Eric Paris <[email protected]>
Cc Cyrill Gorcunov <[email protected]>
Cc Pavel Emelyanov <[email protected]>
---
 fs/dcache.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
fs/dcache.c +8 -2
index 7a5b514..3a0e3bc 100644
@@ -278,12 +278,15 @@ static void dentry_iput(struct dentry * dentry)
 	__releases(dentry->d_inode->i_lock)
 {
 	struct inode *inode = dentry->d_inode;
+	bool last_dentry;
+
 	if (inode) {
 		dentry->d_inode = NULL;
 		hlist_del_init(&dentry->d_alias);
+		last_dentry = hlist_empty(&inode->i_dentry);
 		spin_unlock(&dentry->d_lock);
 		spin_unlock(&inode->i_lock);
-		if (!inode->i_nlink)
+		if (!inode->i_nlink && last_dentry)
 			fsnotify_inoderemove(inode);
 		if (dentry->d_op && dentry->d_op->d_iput)
 			dentry->d_op->d_iput(dentry, inode);
@@ -303,13 +306,16 @@ static void dentry_unlink_inode(struct dentry * dentry)
 	__releases(dentry->d_inode->i_lock)
 {
 	struct inode *inode = dentry->d_inode;
+	bool last_dentry;
+
 	__d_clear_type(dentry);
 	dentry->d_inode = NULL;
 	hlist_del_init(&dentry->d_alias);
 	dentry_rcuwalk_barrier(dentry);
+	last_dentry = hlist_empty(&inode->i_dentry);
 	spin_unlock(&dentry->d_lock);
 	spin_unlock(&inode->i_lock);
-	if (!inode->i_nlink)
+	if (!inode->i_nlink && last_dentry)
 		fsnotify_inoderemove(inode);
 	if (dentry->d_op && dentry->d_op->d_iput)
 		dentry->d_op->d_iput(dentry, inode);
Signature · 1 line
1.9.3
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#4 lore raw
Quoted 5 lines · On Wed, 24 Sep 2014 12:51:55 +0200 Jan Kara <[email protected]> wrote:

Hello,

Andrew, what do you think about the patch below? Al objected that it
changes userspace visible behavior some time ago and then he didn't react to our explanations...

Difficult situation. There's some really important information missing from the changelog:

- Who cares? Is there some real application which is hurting from the current situation? If so, who, what, how and why. If not, then why change anything?

- A description of the userspace API change impact. How did the interface change? What is the risk of this change causing damage to real applications?

If we can answer these questions then we're in a position to make the pain-versus-gain tradeoff decision.

reviewer
#5 lore raw
Quoted 8 lines · Andrew Morton
Quoted 5 lines · On Wed, 24 Sep 2014 12:51:55 +0200 Jan Kara <[email protected]> wrote:

Hello,

Andrew, what do you think about the patch below? Al objected that it
changes userspace visible behavior some time ago and then he didn't react to our explanations...

Quoted 6 lines

Difficult situation. There's some really important information missing from the changelog:

- Who cares? Is there some real application which is hurting from the current situation? If so, who, what, how and why. If not, then why change anything?

I believe Openvz guys hit this in their application but I'll defer to
them for more details.

Quoted 3 lines

- A description of the userspace API change impact. How did the interface change? What is the risk of this change causing damage to real applications?

I believe this was covered in the changelog. Without the patch depending
on the order of unlinks for hardlinked file you sometimes get events:

4       (IN_ATTRIB)
400     (IN_DELETE_SELF)
8000    (IN_IGNORED)

and sometimes you get events:
4 (IN_ATTRIB)
<possibly more events happening for unlinked file>

8       (IN_CLOSE_WRITE)
400     (IN_DELETE_SELF)
8000    (IN_IGNORED)

With the patch you'll always have the second case. So without the patch you don't receive some events if the file has at least 2 hardlinks and then gets unlinked. I think the risk that some application relies on *not* getting those events is pretty low (especially since in the common case of file without hardlinks you will get all those events).

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#6 lore raw
Quoted 9 lines · Jan Kara
Quoted 8 lines · Andrew Morton
Quoted 5 lines · On Wed, 24 Sep 2014 12:51:55 +0200 Jan Kara <[email protected]> wrote:

Hello,

Andrew, what do you think about the patch below? Al objected that it
changes userspace visible behavior some time ago and then he didn't react to our explanations...

Quoted 8 lines
Quoted 6 lines

Difficult situation. There's some really important information missing from the changelog:

- Who cares? Is there some real application which is hurting from the current situation? If so, who, what, how and why. If not, then why change anything?

I believe Openvz guys hit this in their application but I'll defer to
them for more details.

Hi, sorry for delay! Indeed we found this weird behaviour while been testing the results of restore of deleted files. When criu observes opened descriptor on deleted file its contents get written into criu image file which we call "ghost" files. On restore we create a temporary ghost file with some unique name. Then we restore file descriptors which were opened at the moment of checkpoint:
we create a hardlink to this ghost file, then open it and this is done for every descriptor we need to recover. Then if there were a watch mark on the ghost file we restore them as well but at the end we need to do a cleanup and finally remove the ghost file itself which cause the problem mentioned in Andrew's changelog to appear -- when we remove ghost file inode->n_link becomes 0 thus our restored inotify are dropped off by the kernel while here still opened files are floating around. I can't say that it's catastrophical but if there a chance to fix it on kernel level making events flow more sane, this would be just great, also our primary target is to make c/r process transparent to the userspace and without the patch i fear we can't reach it.

reviewer
#7 lore raw
In reply to Jan Kara
Quoted 34 lines · Jan Kara
Quoted 8 lines · Andrew Morton
Quoted 5 lines · On Wed, 24 Sep 2014 12:51:55 +0200 Jan Kara <[email protected]> wrote:

Hello,

Andrew, what do you think about the patch below? Al objected that it
changes userspace visible behavior some time ago and then he didn't react to our explanations...

Quoted 6 lines

Difficult situation. There's some really important information missing from the changelog:

- Who cares? Is there some real application which is hurting from the current situation? If so, who, what, how and why. If not, then why change anything?

I believe Openvz guys hit this in their application but I'll defer to
them for more details.

Quoted 3 lines

- A description of the userspace API change impact. How did the interface change? What is the risk of this change causing damage to real applications?

I believe this was covered in the changelog. Without the patch depending
on the order of unlinks for hardlinked file you sometimes get events:

4       (IN_ATTRIB)
400     (IN_DELETE_SELF)
8000    (IN_IGNORED)

and sometimes you get events:
4 (IN_ATTRIB)
<possibly more events happening for unlinked file>

8       (IN_CLOSE_WRITE)
400     (IN_DELETE_SELF)
8000    (IN_IGNORED)
Quoted 5 lines

With the patch you'll always have the second case. So without the patch you don't receive some events if the file has at least 2 hardlinks and then gets unlinked. I think the risk that some application relies on *not* getting those events is pretty low (especially since in the common case of file without hardlinks you will get all those events).

Ping Andrew? Do you still need more info or are you now OK to merge the
patch?

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#8 lore raw
Quoted 7 lines · On Thu, 2 Oct 2014 12:45:52 +0200 Jan Kara <[email protected]> wrote:
Quoted 5 lines

With the patch you'll always have the second case. So without the patch you don't receive some events if the file has at least 2 hardlinks and then gets unlinked. I think the risk that some application relies on *not* getting those events is pretty low (especially since in the common case of file without hardlinks you will get all those events).

Ping Andrew? Do you still need more info or are you now OK to merge the
patch?

Resend please, with a completed changelog.