All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] shmem: fix nlink for rename overwrite directory
@ 2014-09-24 15:56 Miklos Szeredi
  2014-09-24 18:58 ` Al Viro
  2014-09-27  6:33 ` Christoph Hellwig
  0 siblings, 2 replies; 3+ messages in thread
From: Miklos Szeredi @ 2014-09-24 15:56 UTC (permalink / raw)
  To: Hugh Dickins; +Cc: Al Viro, linux-kernel, linux-fsdevel

From: Miklos Szeredi <mszeredi@suse.cz>

If overwriting an empty directory with rename, then need to drop the extra
nlink.

Test prog:

#include <stdio.h>
#include <fcntl.h>
#include <err.h>
#include <sys/stat.h>

int main(void)
{
	const char *test_dir1 = "test-dir1";
	const char *test_dir2 = "test-dir2";
	int res;
	int fd;
	struct stat statbuf;

	res = mkdir(test_dir1, 0777);
	if (res == -1)
		err(1, "mkdir(\"%s\")", test_dir1);

	res = mkdir(test_dir2, 0777);
	if (res == -1)
		err(1, "mkdir(\"%s\")", test_dir2);

	fd = open(test_dir2, O_RDONLY);
	if (fd == -1)
		err(1, "open(\"%s\")", test_dir2);

	res = rename(test_dir1, test_dir2);
	if (res == -1)
		err(1, "rename(\"%s\", \"%s\")", test_dir1, test_dir2);

	res = fstat(fd, &statbuf);
	if (res == -1)
		err(1, "fstat(%i)", fd);

	if (statbuf.st_nlink != 0) {
		fprintf(stderr, "nlink is %lu, should be 0\n", statbuf.st_nlink);
		return 1;
	}

	return 0;
}

Signed-off-by: Miklos Szeredi <mszeredi@suse.cz>
Cc: stable@vger.kernel.org
---
 mm/shmem.c |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -2367,8 +2367,10 @@ static int shmem_rename2(struct inode *o
 
 	if (new_dentry->d_inode) {
 		(void) shmem_unlink(new_dir, new_dentry);
-		if (they_are_dirs)
+		if (they_are_dirs) {
+			drop_nlink(new_dentry->d_inode);
 			drop_nlink(old_dir);
+		}
 	} else if (they_are_dirs) {
 		drop_nlink(old_dir);
 		inc_nlink(new_dir);

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] shmem: fix nlink for rename overwrite directory
  2014-09-24 15:56 [PATCH] shmem: fix nlink for rename overwrite directory Miklos Szeredi
@ 2014-09-24 18:58 ` Al Viro
  2014-09-27  6:33 ` Christoph Hellwig
  1 sibling, 0 replies; 3+ messages in thread
From: Al Viro @ 2014-09-24 18:58 UTC (permalink / raw)
  To: Miklos Szeredi; +Cc: Hugh Dickins, linux-kernel, linux-fsdevel

On Wed, Sep 24, 2014 at 05:56:17PM +0200, Miklos Szeredi wrote:
> From: Miklos Szeredi <mszeredi@suse.cz>
> 
> If overwriting an empty directory with rename, then need to drop the extra
> nlink.

Hmm...  OK, point.  It doesn't actually check for ->i_nlink for directories,
and it only affects the sucker while it (a directory) is opened-but-rmdired,
but yes, it's visible to userland, so...  Applied.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] shmem: fix nlink for rename overwrite directory
  2014-09-24 15:56 [PATCH] shmem: fix nlink for rename overwrite directory Miklos Szeredi
  2014-09-24 18:58 ` Al Viro
@ 2014-09-27  6:33 ` Christoph Hellwig
  1 sibling, 0 replies; 3+ messages in thread
From: Christoph Hellwig @ 2014-09-27  6:33 UTC (permalink / raw)
  To: Miklos Szeredi
  Cc: Hugh Dickins, Al Viro, linux-kernel, linux-fsdevel, fstests

On Wed, Sep 24, 2014 at 05:56:17PM +0200, Miklos Szeredi wrote:
> From: Miklos Szeredi <mszeredi@suse.cz>
> 
> If overwriting an empty directory with rename, then need to drop the extra
> nlink.
> 
> Test prog:

Can you send a patch to wire this up for xfstests?

> 
> #include <stdio.h>
> #include <fcntl.h>
> #include <err.h>
> #include <sys/stat.h>
> 
> int main(void)
> {
> 	const char *test_dir1 = "test-dir1";
> 	const char *test_dir2 = "test-dir2";
> 	int res;
> 	int fd;
> 	struct stat statbuf;
> 
> 	res = mkdir(test_dir1, 0777);
> 	if (res == -1)
> 		err(1, "mkdir(\"%s\")", test_dir1);
> 
> 	res = mkdir(test_dir2, 0777);
> 	if (res == -1)
> 		err(1, "mkdir(\"%s\")", test_dir2);
> 
> 	fd = open(test_dir2, O_RDONLY);
> 	if (fd == -1)
> 		err(1, "open(\"%s\")", test_dir2);
> 
> 	res = rename(test_dir1, test_dir2);
> 	if (res == -1)
> 		err(1, "rename(\"%s\", \"%s\")", test_dir1, test_dir2);
> 
> 	res = fstat(fd, &statbuf);
> 	if (res == -1)
> 		err(1, "fstat(%i)", fd);
> 
> 	if (statbuf.st_nlink != 0) {
> 		fprintf(stderr, "nlink is %lu, should be 0\n", statbuf.st_nlink);
> 		return 1;
> 	}
> 
> 	return 0;
> }
> 
> Signed-off-by: Miklos Szeredi <mszeredi@suse.cz>
> Cc: stable@vger.kernel.org
> ---
>  mm/shmem.c |    4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> --- a/mm/shmem.c
> +++ b/mm/shmem.c
> @@ -2367,8 +2367,10 @@ static int shmem_rename2(struct inode *o
>  
>  	if (new_dentry->d_inode) {
>  		(void) shmem_unlink(new_dir, new_dentry);
> -		if (they_are_dirs)
> +		if (they_are_dirs) {
> +			drop_nlink(new_dentry->d_inode);
>  			drop_nlink(old_dir);
> +		}
>  	} else if (they_are_dirs) {
>  		drop_nlink(old_dir);
>  		inc_nlink(new_dir);
> --
> To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
---end quoted text---

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2014-09-27  6:33 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-09-24 15:56 [PATCH] shmem: fix nlink for rename overwrite directory Miklos Szeredi
2014-09-24 18:58 ` Al Viro
2014-09-27  6:33 ` Christoph Hellwig

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.