mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Miklos Szeredi <miklos@szeredi.hu>
To: akpm@osdl.org
Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org
Subject: [patch 05/12] fuse: fix handling of moved directory
Date: Mon, 16 Oct 2006 18:27:14 +0200	[thread overview]
Message-ID: <20061016162741.475543000@szeredi.hu> (raw)
In-Reply-To: <20061016162709.369579000@szeredi.hu>

[-- Attachment #1: fuse_dir_alias_fix.patch --]
[-- Type: text/plain, Size: 5000 bytes --]

Fuse considered it an error (EIO) if lookup returned a directory
inode, to which a dentry already refered.  This is because directory
aliases are not allowed.

But in a network filesystem this could happen legitimately, if a
directory is moved on a remote client.  This patch attempts to relax
the restriction by trying to first evict the offending alias from the
cache.  If this fails, it still returns an error (EBUSY).

A rarer situation is if an mkdir races with an indenpendent lookup,
which finds the newly created directory already moved.  In this
situation the mkdir should return success, but that would be
incorrect, since the dentry cannot be instantiated, so return EBUSY.

Previously checking for a directory alias and instantiation of the
dentry weren't done atomically in lookup/mkdir, hence two such calls
racing with each other could create aliased directories.  To prevent
this introduce a new per-connection mutex: fuse_conn->inst_mutex,
which is taken for instantiations with a directory inode.

Signed-off-by: Miklos Szeredi <miklos@szeredi.hu>

---
Index: linux/fs/fuse/dir.c
===================================================================
--- linux.orig/fs/fuse/dir.c	2006-10-16 16:17:31.000000000 +0200
+++ linux/fs/fuse/dir.c	2006-10-16 16:21:11.000000000 +0200
@@ -177,22 +177,6 @@ static int fuse_dentry_revalidate(struct
 	return 1;
 }
 
-/*
- * Check if there's already a hashed alias of this directory inode.
- * If yes, then lookup and mkdir must not create a new alias.
- */
-static int dir_alias(struct inode *inode)
-{
-	if (S_ISDIR(inode->i_mode)) {
-		struct dentry *alias = d_find_alias(inode);
-		if (alias) {
-			dput(alias);
-			return 1;
-		}
-	}
-	return 0;
-}
-
 static int invalid_nodeid(u64 nodeid)
 {
 	return !nodeid || nodeid == FUSE_ROOT_ID;
@@ -208,6 +192,24 @@ static int valid_mode(int m)
 		S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
 }
 
+/*
+ * Add a directory inode to a dentry, ensuring that no other dentry
+ * refers to this inode.  Called with fc->inst_mutex.
+ */
+static int fuse_d_add_directory(struct dentry *entry, struct inode *inode)
+{
+	struct dentry *alias = d_find_alias(inode);
+	if (alias) {
+		/* This tries to shrink the subtree below alias */
+		fuse_invalidate_entry(alias);
+		dput(alias);
+		if (!list_empty(&inode->i_dentry))
+			return -EBUSY;
+	}
+	d_add(entry, inode);
+	return 0;
+}
+
 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
 				  struct nameidata *nd)
 {
@@ -243,11 +245,17 @@ static struct dentry *fuse_lookup(struct
 	if (err && err != -ENOENT)
 		return ERR_PTR(err);
 
-	if (inode && dir_alias(inode)) {
-		iput(inode);
-		return ERR_PTR(-EIO);
-	}
-	d_add(entry, inode);
+	if (inode && S_ISDIR(inode->i_mode)) {
+		mutex_lock(&fc->inst_mutex);
+		err = fuse_d_add_directory(entry, inode);
+		mutex_unlock(&fc->inst_mutex);
+		if (err) {
+			iput(inode);
+			return ERR_PTR(err);
+		}
+	} else
+		d_add(entry, inode);
+
 	entry->d_op = &fuse_dentry_operations;
 	if (!err)
 		fuse_change_timeout(entry, &outarg);
@@ -403,12 +411,22 @@ static int create_new_entry(struct fuse_
 	}
 	fuse_put_request(fc, req);
 
-	if (dir_alias(inode)) {
-		iput(inode);
-		return -EIO;
-	}
+	if (S_ISDIR(inode->i_mode)) {
+		struct dentry *alias;
+		mutex_lock(&fc->inst_mutex);
+		alias = d_find_alias(inode);
+		if (alias) {
+			/* New directory must have moved since mkdir */
+			mutex_unlock(&fc->inst_mutex);
+			dput(alias);
+			iput(inode);
+			return -EBUSY;
+		}
+		d_instantiate(entry, inode);
+		mutex_unlock(&fc->inst_mutex);
+	} else
+		d_instantiate(entry, inode);
 
-	d_instantiate(entry, inode);
 	fuse_change_timeout(entry, &outarg);
 	fuse_invalidate_attr(dir);
 	return 0;
Index: linux/fs/fuse/fuse_i.h
===================================================================
--- linux.orig/fs/fuse/fuse_i.h	2006-10-16 16:17:31.000000000 +0200
+++ linux/fs/fuse/fuse_i.h	2006-10-16 16:17:36.000000000 +0200
@@ -239,6 +239,9 @@ struct fuse_conn {
 	/** Lock protecting accessess to  members of this structure */
 	spinlock_t lock;
 
+	/** Mutex protecting against directory alias creation */
+	struct mutex inst_mutex;
+
 	/** Refcount */
 	atomic_t count;
 
Index: linux/fs/fuse/inode.c
===================================================================
--- linux.orig/fs/fuse/inode.c	2006-10-16 16:17:31.000000000 +0200
+++ linux/fs/fuse/inode.c	2006-10-16 16:17:36.000000000 +0200
@@ -379,6 +379,7 @@ static struct fuse_conn *new_conn(void)
 	fc = kzalloc(sizeof(*fc), GFP_KERNEL);
 	if (fc) {
 		spin_lock_init(&fc->lock);
+		mutex_init(&fc->inst_mutex);
 		atomic_set(&fc->count, 1);
 		init_waitqueue_head(&fc->waitq);
 		init_waitqueue_head(&fc->blocked_waitq);
@@ -398,8 +399,10 @@ static struct fuse_conn *new_conn(void)
 
 void fuse_conn_put(struct fuse_conn *fc)
 {
-	if (atomic_dec_and_test(&fc->count))
+	if (atomic_dec_and_test(&fc->count)) {
+		mutex_destroy(&fc->inst_mutex);
 		kfree(fc);
+	}
 }
 
 struct fuse_conn *fuse_conn_get(struct fuse_conn *fc)

--

  parent reply	other threads:[~2006-10-16 16:28 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-10-16 16:27 [patch 00/12] fuse update Miklos Szeredi
2006-10-16 16:27 ` [patch 01/12] fuse: fix hang on SMP Miklos Szeredi
2006-10-16 23:51   ` Andrew Morton
2006-10-17 12:53     ` Mike Day
2006-10-17 13:43       ` Miklos Szeredi
2006-10-16 16:27 ` [patch 02/12] document i_size_write locking rules Miklos Szeredi
2006-10-16 16:27 ` [patch 03/12] fuse: locking fix for nlookup Miklos Szeredi
2006-10-16 16:27 ` [patch 04/12] fuse: fix spurious BUG Miklos Szeredi
2006-10-16 16:27 ` Miklos Szeredi [this message]
2006-10-16 16:27 ` [patch 06/12] fuse: fix dereferencing dentry parent Miklos Szeredi
2006-10-16 16:27 ` [patch 07/12] fuse: update userspace interface to version 7.8 Miklos Szeredi
2006-10-16 16:27 ` [patch 08/12] fuse: minor cleanup in fuse_dentry_revalidate Miklos Szeredi
2006-10-16 16:27 ` [patch 09/12] fuse: add support for block device based filesystems Miklos Szeredi
2006-10-16 16:27 ` [patch 10/12] fuse: add blksize option Miklos Szeredi
2006-10-16 16:27 ` [patch 11/12] fuse: add bmap support Miklos Szeredi
2006-10-16 16:27 ` [patch 12/12] fuse: add DESTROY operation Miklos Szeredi

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=20061016162741.475543000@szeredi.hu \
    --to=miklos@szeredi.hu \
    --cc=akpm@osdl.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --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

all inboxes | Powered by JetHome®