linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Howells <dhowells@redhat.com>
To: linux-fsdevel@vger.kernel.org, viro@ZenIV.linux.org.uk,
	valerie.aurora@gmail.com
Cc: linux-kernel@vger.kernel.org,
	David Howells <dhowells@redhat.com> (Further development)
Subject: [PATCH 48/73] union-mount: Add wrapper for lookup_union_locked() and RCU hook [ver #2]
Date: Tue, 21 Feb 2012 18:03:30 +0000	[thread overview]
Message-ID: <20120221180330.25235.88964.stgit@warthog.procyon.org.uk> (raw)
In-Reply-To: <20120221175721.25235.8901.stgit@warthog.procyon.org.uk>

Add a wrapper function for lookup_union_locked() that locks the parent
directory and follows the mount after lookup.  This is appropriate for calling
from do_lookup() when in refwalk mode.

Also add an RCU-mode pathwalk lookup function.  This need not leave RCU-mode if
the upper dentry is appropriately assembled or the lower dentry can be validly
used.

Original-author: Valerie Aurora <vaurora@redhat.com>
Signed-off-by: David Howells <dhowells@redhat.com> (Further development)
---

 fs/namei.c |  149 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 147 insertions(+), 2 deletions(-)

diff --git a/fs/namei.c b/fs/namei.c
index 2d69ce1..c0adf4c 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -1195,6 +1195,9 @@ static int __lookup_union(struct nameidata *nd, struct qstr *name,
 		 * layer's directory to the union stack for the topmost
 		 * directory.
 		 */
+#warning what if the directory is managed?
+#warning should we d_revalidate the lower dentry?
+#warning how to handle automounts?
 		follow_mount(&lower);
 
 		if (!topmost->dentry->d_inode) {
@@ -1277,6 +1280,144 @@ static int lookup_union_locked(struct nameidata *nd, struct qstr *name,
 }
 
 /*
+ * lookup_union - union mount-aware part of do_lookup()
+ *
+ * do_lookup()-style wrapper for lookup_union().  Follows mounts.
+ */
+static int lookup_union(struct nameidata *nd, struct qstr *name,
+			struct path *topmost)
+{
+	struct dentry *parent = nd->path.dentry;
+	struct inode *dir = parent->d_inode;
+	int err;
+
+	mutex_lock(&dir->i_mutex);
+	err = lookup_union_locked(nd, name, topmost);
+	mutex_unlock(&dir->i_mutex);
+	if (err)
+		return err;
+
+	return follow_managed(topmost, nd->flags);
+}
+
+/*
+ * lookup_union_rcu - Handle union mounted dentries in RCU-walk mode
+ * @nd: The current pathwalk state (refers to @parent currently)
+ * @parent: The parent directory (holds the union stack)
+ * @path: The point just looked up in @parent
+ * @parent_seq: The d_seq of @parent at the point of lookup
+ * @inode: The inode at @dentry (*@inode is NULL if negative dentry)
+ *
+ * Handle a dentry that represents a non-directory file or a hole/reference in
+ * a union mount upperfs.  This involves transiting to the lower file, provided
+ * we aren't going to open the lower file for writing - otherwise we have to
+ * copy the file up (which we can't do in rcuwalk mode).
+ *
+ * Directories are handled differently: they're unconditionally and completely
+ * mirrored from the lowerfs to the upperfs as soon as we encounter them in a
+ * lookup.  However, since we don't create dentries in rcuwalk mode, this will
+ * be handled automatically by refwalk mode.
+ *
+ * We return true if we don't need to do anything or if we've successfully
+ * updated the path.  If we need to drop out of RCU-walk and go to refwalk
+ * mode, we return false.
+ */
+static bool lookup_union_rcu(struct nameidata *nd,
+			     struct dentry *parent,
+			     struct path *path,
+			     unsigned parent_seq,
+			     struct inode **inode)
+{
+	struct dentry *dentry = path->dentry;
+	struct inode *parent_inode = nd->inode;
+	unsigned layer, layers;
+
+	/* Handle non-unionmount dentries first.  The union stack will have
+	 * been built during the initial lookup of the parent dir, so if it's
+	 * not there, it's not unioned.
+	 */
+	if (!IS_DIR_UNIONED(parent))
+		return true;
+
+	/* If it's positive then no further lookup is needed: the file or
+	 * directory has been copied up and the user gets to play with that.
+	 */
+	if (*inode)
+		return true;
+
+	/* If this dentry is a blocker, then stop here. */
+	if (d_is_whiteout(dentry) ||
+	    (IS_OPAQUE(parent_inode) && !d_is_fallthru(dentry)))
+		return true;
+
+	/* At this point we have a negative dentry in the unionmount that may
+	 * be overlaying a non-directory file in a lower filesystem, so we loop
+	 * through the union stack of the parent directory to try to find a
+	 * usable dentry further down.
+	 */
+	layers = parent->d_sb->s_union_count;
+	for (layer = 0; layer < layers; layer++) {
+		/* Look for the a matching dentry in this layer, assuming it's
+		 * still valid.  Since the lower fs is hard locked R/O,
+		 * revalidation ought to be unnecessary.
+		 */
+		unsigned ldseq, seq;
+		struct dentry *lower_dir, *lower;
+		struct path *lower_path = union_find_dir(parent, layer);
+		if (!lower_path->mnt)
+			continue;
+
+		lower_dir = lower_path->dentry;
+		ldseq = read_seqcount_begin(&lower_dir->d_seq);
+
+		if (unlikely(lower_dir->d_flags & DCACHE_OP_REVALIDATE)) {
+			if (unlikely(d_revalidate(lower_dir, nd) <= 0) ||
+			    __read_seqcount_retry(&lower_dir->d_seq, ldseq))
+				return false;
+		}
+
+		lower = __d_lookup_rcu(lower_dir, &dentry->d_name, &seq, inode);
+		if (!lower)
+			return false;
+
+		/* We've got a negative dentry which can mean several things: a
+		 * plain negative dentry is ignored and lookup continues to the
+		 * next layer; but a whiteout or a non-fallthru in an opaque
+		 * dir covers everything below it.
+		 */
+		if (!*inode) {
+			if (d_is_whiteout(lower) ||
+			    (IS_OPAQUE(parent_inode) && !d_is_fallthru(lower))) {
+				if (read_seqcount_retry(&lower_dir->d_seq,
+							ldseq))
+					return false;
+				return true;
+			}
+			continue;
+		}
+
+		/* If the lower dentry is a directory then it will need copying
+		 * up before we can make use of it.
+		 */
+		if (S_ISDIR((*inode)->i_mode))
+			return false;
+
+		/* We have a file in a lower fs that we can use */
+		if (read_seqcount_retry(&lower_dir->d_seq, ldseq) ||
+		    __read_seqcount_retry(&parent->d_seq, parent_seq))
+			return false;
+
+		path->mnt = lower_path->mnt;
+		path->dentry = lower;
+		nd->seq = seq;
+		return true;
+	}
+
+	/* Found nothing, so just use the top negative dentry */
+	return dentry;
+}
+
+/*
  * Allocate a dentry with name and parent, and perform a parent
  * directory ->lookup on it. Returns the new dentry, or ERR_PTR
  * on error. parent->d_inode->i_mutex must be held. d_lookup must
@@ -1351,14 +1492,15 @@ static int do_lookup(struct nameidata *nd, struct qstr *name,
 	 * do the non-racy lookup, below.
 	 */
 	if (nd->flags & LOOKUP_RCU) {
-		unsigned seq;
+		unsigned seq, pseq;
 		*inode = nd->inode;
 		dentry = __d_lookup_rcu(parent, name, &seq, inode);
 		if (!dentry)
 			goto unlazy;
 
 		/* Memory barrier in read_seqcount_begin of child is enough */
-		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
+		pseq = nd->seq;
+		if (__read_seqcount_retry(&parent->d_seq, pseq))
 			return -ECHILD;
 		nd->seq = seq;
 
@@ -1372,8 +1514,11 @@ static int do_lookup(struct nameidata *nd, struct qstr *name,
 		}
 		if (unlikely(d_need_lookup(dentry)))
 			goto unlazy;
+
 		path->mnt = mnt;
 		path->dentry = dentry;
+		if (unlikely(!lookup_union_rcu(nd, parent, path, pseq, inode)))
+			goto unlazy;
 		if (unlikely(!__follow_mount_rcu(nd, path, inode)))
 			goto unlazy;
 		if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))


  parent reply	other threads:[~2012-02-21 19:12 UTC|newest]

Thread overview: 89+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-21 17:57 [RFC][PATCH 00/73] Union Mount [ver #2] David Howells
2012-02-21 17:57 ` [PATCH 01/73] VFS: Make chown() and lchown() call fchownat() " David Howells
2012-02-21 17:57 ` [PATCH 02/73] VFS: Make clone_mnt()/copy_tree()/collect_mounts() return errors " David Howells
2012-02-21 17:57 ` [PATCH 03/73] VFS: Comment mount following code " David Howells
2012-02-21 17:57 ` [PATCH 04/73] VFS: Make lookup_hash() return a struct path " David Howells
2012-02-21 17:58 ` [PATCH 05/73] VFS: Pass mount flags to sget() " David Howells
2012-02-21 17:58 ` [PATCH 06/73] VFS: Split inode_permission() " David Howells
2012-02-21 17:58 ` [PATCH 07/73] VFS: Add hard read-only users count to superblock " David Howells
2012-02-21 17:58 ` [PATCH 08/73] VFS: Add CL_NO_SHARED flag to clone_mnt()/copy_tree() " David Howells
2012-02-29  6:56   ` Ram Pai
2012-02-21 17:58 ` [PATCH 09/73] VFS: Add CL_NO_SLAVE " David Howells
2012-02-29  6:58   ` Ram Pai
2012-02-21 17:58 ` [PATCH 10/73] VFS: Add CL_MAKE_HARD_READONLY " David Howells
2012-02-21 17:58 ` [PATCH 11/73] whiteout/NFSD: Don't return information about whiteouts to userspace " David Howells
2012-02-21 17:58 ` [PATCH 12/73] whiteout: Define flags and operations for opaque inodes " David Howells
2012-02-21 17:59 ` [PATCH 13/73] whiteout: Add vfs_whiteout() and whiteout inode operation " David Howells
2012-02-21 17:59 ` [PATCH 14/73] whiteout: Allow removal of a directory with whiteouts " David Howells
2012-02-21 17:59 ` [PATCH 15/73] tmpfs: Add whiteout support " David Howells
2012-02-21 17:59 ` [PATCH 16/73] VFS: Basic fallthru definitions " David Howells
2012-02-21 17:59 ` [PATCH 17/73] tmpfs: Add fallthru support " David Howells
2012-02-21 17:59 ` [PATCH 18/73] union-mount: Union mounts documentation " David Howells
2012-02-27  2:57   ` Randy Dunlap
2012-02-21 17:59 ` [PATCH 19/73] union-mount: Introduce MNT_UNION and MS_UNION flags " David Howells
2012-02-21 18:00 ` [PATCH 20/73] union-mount: Add CONFIG_UNION_MOUNT option " David Howells
2012-02-21 18:00 ` [PATCH 21/73] union-mount: Create union_stack structure " David Howells
2012-02-21 18:00 ` [PATCH 22/73] union-mount: Add two superblock fields for union mounts " David Howells
2012-02-21 18:00 ` [PATCH 23/73] union-mount: Add union_alloc() " David Howells
2012-02-21 18:00 ` [PATCH 24/73] union-mount: Add union_find_dir() " David Howells
2012-02-21 18:00 ` [PATCH 25/73] union-mount: Create d_free_unions() " David Howells
2012-02-21 18:00 ` [PATCH 26/73] union-mount: Free union stack on removal of topmost dentry from dcache " David Howells
2012-02-21 18:00 ` [PATCH 27/73] union-mount: Create union_add_dir() " David Howells
2012-02-21 18:01 ` [PATCH 28/73] union-mount: Add union_create_topmost_dir() " David Howells
2012-02-21 18:01 ` [PATCH 29/73] union-mount: Create IS_MNT_UNION() " David Howells
2012-02-21 18:01 ` [PATCH 30/73] union-mount: Create needs_lookup_union() " David Howells
2012-02-21 18:01 ` [PATCH 31/73] union-mount: Create check_topmost_union_mnt() " David Howells
2012-02-21 18:01 ` [PATCH 32/73] union-mount: Add clone_union_tree() and put_union_sb() " David Howells
2012-02-21 18:01 ` [PATCH 33/73] unionmount: Mark lower layers in union " David Howells
2012-02-21 18:01 ` [PATCH 34/73] union-mount: Create build_root_union() " David Howells
2012-02-21 18:01 ` [PATCH 35/73] union-mount: Create prepare_mnt_union() and cleanup_mnt_union() " David Howells
2012-02-21 18:02 ` [PATCH 36/73] union-mount: Prevent improper union-related remounts " David Howells
2012-02-21 18:02 ` [PATCH 37/73] union-mount: Prevent topmost file system from being mounted elsewhere " David Howells
2012-02-21 18:02 ` [PATCH 38/73] union-mount: Prevent bind mounts of union mounts " David Howells
2012-02-21 18:02 ` [PATCH 39/73] union-mount: Duplicate the i_{, dir_}mutex lock classes and use for upper layer " David Howells
2012-02-21 18:02 ` [PATCH 40/73] union-mount: Implement union mount " David Howells
2012-02-21 18:02 ` [PATCH 41/73] union-mount: Temporarily disable some syscalls " David Howells
2012-02-21 18:02 ` [PATCH 42/73] union-mount: Basic infrastructure of __lookup_union() " David Howells
2012-02-21 18:02 ` [PATCH 43/73] union-mount: Process negative dentries in " David Howells
2012-02-21 18:03 ` [PATCH 44/73] union-mount: Return files found in lower layers " David Howells
2012-02-21 18:03 ` [PATCH 45/73] union-mount: Build union stack " David Howells
2012-02-21 18:03 ` [PATCH 46/73] union-mount: Follow mount " David Howells
2012-02-21 18:03 ` [PATCH 47/73] union-mount: Add lookup_union_locked() " David Howells
2012-02-21 18:03 ` David Howells [this message]
2012-02-21 18:03 ` [PATCH 49/73] union-mount: Call union lookup functions in lookup path " David Howells
2012-02-21 18:03 ` [PATCH 50/73] union-mount: Create whiteout on unlink() " David Howells
2012-02-21 18:03 ` [PATCH 51/73] union-mount: Create whiteout on rmdir() " David Howells
2012-02-21 18:03 ` [PATCH 52/73] union-mount: Set opaque flag on new directories in unioned file systems " David Howells
2012-02-21 18:04 ` [PATCH 53/73] union-mount: Copy up directory entries on first readdir() " David Howells
2012-02-21 18:04 ` [PATCH 54/73] union-mount: Add generic_readdir_fallthru() helper " David Howells
2012-02-21 18:04 ` [PATCH 55/73] fallthru: tmpfs support for lookup of d_type/d_ino in fallthrus " David Howells
2012-02-21 18:04 ` [PATCH 56/73] union-mount: In-kernel file copyup routines " David Howells
2012-02-21 18:04 ` [PATCH 57/73] VFS: Create user_path_nd() to lookup both parent and target " David Howells
2012-02-21 18:04 ` [PATCH 58/73] unionmount: Add LOOKUP_COPY_UP " David Howells
2012-02-21 18:04 ` [PATCH 59/73] unionmount: Override creds when copying up a file to correctly set ownership " David Howells
2012-02-21 18:05 ` [PATCH 60/73] union-mount: Implement union-aware access()/faccessat() " David Howells
2012-02-21 18:05 ` [PATCH 61/73] union-mount: Make various syscalls aware (link, chmod, chown, utimes & setxattr) " David Howells
2012-02-21 18:05 ` [PATCH 62/73] union-mount: Implement union-aware rename() " David Howells
2012-02-21 18:05 ` [PATCH 63/73] union-mount: Implement union-aware writable open() " David Howells
2012-02-21 18:05 ` [PATCH 64/73] union-mount: Implement union-aware truncate() " David Howells
2012-02-21 18:05 ` [PATCH 65/73] ext2: Add ext2_dirent_in_use() " David Howells
2012-02-21 18:05 ` [PATCH 66/73] ext2: Split ext2_add_entry() from ext2_add_link() " David Howells
2012-02-27  0:04   ` Ted Ts'o
2012-02-27  3:30     ` Andreas Dilger
2012-02-27 19:09       ` Ted Ts'o
2012-02-27 20:45         ` Andreas Dilger
2012-02-21 18:05 ` [PATCH 67/73] ext2: Remove target inode pointer from ext2_add_entry() " David Howells
2012-02-27  0:22   ` Ted Ts'o
2012-02-21 18:06 ` [PATCH 68/73] ext2: Add whiteout and opaque directory support " David Howells
2012-02-21 18:06 ` [PATCH 69/73] ext2: Add fallthru " David Howells
2012-02-27  0:33   ` Ted Ts'o
2012-02-21 18:06 ` [PATCH 70/73] fallthru: ext2 support for lookup of d_type/d_ino in fallthrus " David Howells
2012-02-21 18:06 ` [PATCH 71/73] jffs2: Add whiteout support " David Howells
2012-02-21 18:06 ` [PATCH 72/73] jffs2: Add fallthru " David Howells
2012-02-21 18:06 ` [PATCH 73/73] fallthru: jffs2 support for lookup of d_type/d_ino in fallthrus " David Howells
2012-02-26  6:48 ` copy-up xattr (Re: [RFC][PATCH 00/73] Union Mount [ver #2]) J. R. Okajima
2012-03-26 14:22 ` David Howells
2012-03-27  4:38   ` Casey Schaufler
2012-03-27 13:10     ` David Quigley
2012-03-27 16:37       ` Casey Schaufler
2012-03-28 14:51   ` J. R. Okajima

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=20120221180330.25235.88964.stgit@warthog.procyon.org.uk \
    --to=dhowells@redhat.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=valerie.aurora@gmail.com \
    --cc=viro@ZenIV.linux.org.uk \
    /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;
as well as URLs for NNTP newsgroup(s).