public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Scott James Remnant <scott@ubuntu.com>
To: Eric Paris <eparis@redhat.com>
Cc: linux-kernel@vger.kernel.org
Subject: inotify regression, missing events
Date: Sat, 11 Jul 2009 17:02:07 +0100	[thread overview]
Message-ID: <1247328127.4003.6.camel@wing-commander> (raw)

[-- Attachment #1: Type: text/plain, Size: 6067 bytes --]

Hey folks,

Looks like there's a regression with inotify since the rewrite to use
fanotify.  Events are simply missing and not being delivered to
userspace.

Here's a simple test case, just compile and run it:

----8<--------8<--------8<--------8<--------8<--------8<--------8<----
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/inotify.h>

#include <stdio.h>
#include <assert.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


enum {
	WD		= 0x01,
	FOO		= 0x02,
	BAR		= 0x04,
	SUBDIR_WD	= 0x08,
	SUBDIR		= 0x10,
	SUBDIR_FOO	= 0x20,
	ALL		= 0x3f
};

int
main (int   argc,
      char *argv[])
{
	int     fd;
	char    dirname[PATH_MAX];
	char    filename[PATH_MAX];
	int     ret;
	FILE *  fp;
	int     wd;
	int     subdir_wd;
	char    buf[4096];
	int     expected;
	ssize_t len;
	size_t  sz;
	
	fd = inotify_init ();
	assert (fd >= 0);
	
	/* Create a temporary directory with two files and a sub-directory
	 * containing just one file.
	 */
	strcpy (dirname, "/tmp/inotify_test_XXXXXX");
	assert (mkdtemp (dirname));
	

	strcpy (filename, dirname);
	strcat (filename, "/subdir");
	
	ret = mkdir (filename, 0755);
	assert (ret == 0);
	

	strcpy (filename, dirname);
	strcat (filename, "/subdir/foo");
	
	fp = fopen (filename, "w");
	assert (fp);
	
	fprintf (fp, "file in a sub-directory\n");
	
	ret = fclose (fp);
	assert (ret == 0);


	strcpy (filename, dirname);
	strcat (filename, "/foo");
	
	fp = fopen (filename, "w");
	assert (fp);
	
	fprintf (fp, "this is a test\n");
	
	ret = fclose (fp);
	assert (ret == 0);
	

	strcpy (filename, dirname);
	strcat (filename, "/bar");
	
	fp = fopen (filename, "w");
	assert (fp);
	
	fprintf (fp, "this is another test\n");
	
	ret = fclose (fp);
	assert (ret == 0);

	
	/* Watch those directories for deletions */
	wd = inotify_add_watch (fd, dirname, IN_DELETE);
	assert (wd >= 0);

	strcpy (filename, dirname);
	strcat (filename, "/subdir");

	subdir_wd = inotify_add_watch (fd, filename, IN_DELETE);
	assert (wd >= 0);
	

	/* Clean up the directory */
	strcpy (filename, dirname);
	strcat (filename, "/subdir/foo");

	ret = unlink (filename);
	assert (ret == 0);

	strcpy (filename, dirname);
	strcat (filename, "/subdir");

	ret = rmdir (filename);
	assert (ret == 0);
	
	strcpy (filename, dirname);
	strcat (filename, "/bar");

	ret = unlink (filename);
	assert (ret == 0);

	strcpy (filename, dirname);
	strcat (filename, "/foo");

	ret = unlink (filename);
	assert (ret == 0);

	ret = rmdir (dirname);
	assert (ret == 0);
	
	
	/* Read the inotify events */
	expected = ALL;
	while (expected) {
		struct inotify_event *ev;
		ssize_t off;
		
		printf ("Waiting for:");
		if (expected & WD)
			printf (" wd");
		if (expected & FOO)
			printf (" foo");
		if (expected & BAR)
			printf (" bar");
		if (expected & SUBDIR_WD)
			printf (" subdir_wd");
		if (expected & SUBDIR)
			printf (" subdir");
		if (expected & SUBDIR_FOO)
			printf (" subdir/foo");
		printf ("\n");

		len = read (fd, buf, sizeof buf);
		assert (len > 0);
		
		off = 0;
		while (off < len) {
			ev = (struct inotify_event *)(buf + off);
			sz = sizeof (struct inotify_event) + ev->len;
			off += sz;

			/* Eliminate the silly */
			assert (! (ev->mask & IN_Q_OVERFLOW));
			assert (! (ev->mask & IN_UNMOUNT));
			
			/* Print the event for debugging */
			printf ("Got ");
			if (ev->mask & IN_DELETE)
				printf ("DELETE ");
			if (ev->mask & IN_IGNORED)
				printf ("IGNORED ");
			if (ev->mask & IN_ISDIR)
				printf ("ISDIR ");
			if (ev->len)
				printf ("\"%.*s\" ", ev->len, ev->name);
			printf ("(%d)\n", ev->wd);

			/* Expect the file in the sub-directory to go */			
			if ((ev->wd == subdir_wd)
			    && (ev->mask & IN_DELETE)
			    && (! (ev->mask & IN_ISDIR))
			    && (strncmp (ev->name, "foo", ev->len) == 0))
				expected &= ~SUBDIR_FOO;
			
			/* Expect the sub-directory to go */
			if ((ev->wd == wd)
			    && (ev->mask & IN_DELETE)
			    && (ev->mask & IN_ISDIR)
			    && (strncmp (ev->name, "subdir", ev->len) == 0))
				expected &= ~SUBDIR;
				
			/* Expect an IS_IGNORED for the sub-directory watch */
			if ((ev->wd == subdir_wd)
			    && (ev->mask & IN_IGNORED))
				expected &= ~SUBDIR_WD;
				
			/* Expect the first file in the directory to go */			
			if ((ev->wd == wd)
			    && (ev->mask & IN_DELETE)
			    && (! (ev->mask & IN_ISDIR))
			    && (strncmp (ev->name, "foo", ev->len) == 0))
				expected &= ~FOO;
			
			/* Expect the second file in the directory to go */			
			if ((ev->wd == wd)
			    && (ev->mask & IN_DELETE)
			    && (! (ev->mask & IN_ISDIR))
			    && (strncmp (ev->name, "bar", ev->len) == 0))
				expected &= ~BAR;

			/* And finally expect an IS_IGNORED for the directory */
			if ((ev->wd == wd)
			    && (ev->mask & IN_IGNORED))
				expected &= ~WD;
		}
	}	

	ret = close (fd);
	assert (ret == 0);
	
	printf ("\n");
	printf ("All good!\n");

	return 0;
}
---->8-------->8-------->8-------->8-------->8-------->8-------->8----

With 2.6.30, you see (as you'd expect) an inotify delete event for each
of the files and the sub-directory along with the IN_IGNORED for the
sub-directory and directory as the watch gets cleaned up by the kernel:


Waiting for: wd foo bar subdir_wd subdir subdir/foo
Got DELETE "foo" (2)
Got DELETE ISDIR "subdir" (1)
Got IGNORED (2)
Got DELETE "bar" (1)
Got DELETE "foo" (1)
Got IGNORED (1)

All good!


But with 2.6.31-rc2, the event for the "foo" file isn't delivered:

Waiting for: wd foo bar subdir_wd subdir subdir/foo
Got DELETE "foo" (2)
Got DELETE ISDIR "subdir" (1)
Got IGNORED (2)
Got DELETE "bar" (1)
Got IGNORED (1)
Waiting for: foo


This doesn't seem to be related to the removal of the parent directory,
without removing that we still hang waiting for the event for that file.

Scott
-- 
Scott James Remnant
scott@ubuntu.com

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 197 bytes --]

             reply	other threads:[~2009-07-11 16:02 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-11 16:02 Scott James Remnant [this message]
2009-07-11 11:50 ` inotify regression, missing events Eric Paris
2009-07-13 12:21   ` Scott James Remnant
2009-07-13 13:43     ` Eric Paris
2009-07-25 22:23       ` Rafael J. Wysocki
2009-07-25 22:27         ` Eric Paris

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1247328127.4003.6.camel@wing-commander \
    --to=scott@ubuntu.com \
    --cc=eparis@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox