Skip to contentSkip to conversation

vfs: mountinfo (v3)

PATCH In review Miklos Szeredi 3 participants 15 messages Mar 26, 2008 – Mar 27, 2008

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover vfs: mountinfo (v3)

Here's an updated mountinfo patch with the following changes relative to the last submission (the one currently in -mm):

- in __d_path() don't add extra parameter for checking unreachable path
- reduce proliferation of #ifdef CONFIG_PROC_FS into dcache.c and seq_file.c
- make peer group ID allocation a separate pass from setting mount shared
- change order and format of fields in /proc/<pid>/mountinfo
- remove dubious cleanups
- consolidate locking: don't use vfsmount_lock where namespace_sem suffices
- patch history was getting tangled up, merge and resplit into logical chunks

This series is also available here:

git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/vfs.git mountinfo

I guess the simplest for all involved parties would be if this went into Al's tree (if it looks OK) and then Andrew can suck that tree (*) containing all sort of goodies into -mm.

(*) git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs-2.6.git vfs-2.6.25

Thanks,
Miklos

--

#1 lore raw

PATCH 1/7 vfs: mountinfo: add dentry_path()

From: Ram Pai <[email protected]>

[[email protected]] split big patch into managable chunks

Add the following functions:

dentry_path()
seq_dentry()

These are similar to d_path() and seq_path(). But instead of calculating the path within a mount namespace, they calculate the path from the root of the filesystem to a given dentry, ignoring mounts completely.

---
 fs/dcache.c              |   88 ++++++++++++++++++++++++++++++++++-------------
 fs/seq_file.c            |   65 ++++++++++++++++++++++++++--------
 include/linux/dcache.h   |    1 
 include/linux/seq_file.h |    2 +
 4 files changed, 117 insertions(+), 39 deletions(-)
fs/dcache.c +65 -23
Index: vfs-2.6/fs/dcache.c
===================================================================
@@ -1746,6 +1746,17 @@ shouldnt_be_hashed:
 	goto shouldnt_be_hashed;
 }
 
+static int prepend(char **buffer, int *buflen, const char *str,
+			  int namelen)
+{
+	*buflen -= namelen;
+	if (*buflen < 0)
+		return -ENAMETOOLONG;
+	*buffer -= namelen;
+	memcpy(*buffer, str, namelen);
+	return 0;
+}
+
 /**
  * d_path - return the path of a dentry
  * @dentry: dentry to report
@@ -1767,17 +1778,11 @@ static char *__d_path(struct dentry *den
 {
 	char * end = buffer+buflen;
 	char * retval;
-	int namelen;
 
-	*--end = '\0';
-	buflen--;
-	if (!IS_ROOT(dentry) && d_unhashed(dentry)) {
-		buflen -= 10;
-		end -= 10;
-		if (buflen < 0)
+	prepend(&end, &buflen, "\0", 1);
+	if (!IS_ROOT(dentry) && d_unhashed(dentry) &&
+		(prepend(&end, &buflen, " (deleted)", 10) != 0))
 			goto Elong;
-		memcpy(end, " (deleted)", 10);
-	}
 
 	if (buflen < 1)
 		goto Elong;
@@ -1804,13 +1809,10 @@ static char *__d_path(struct dentry *den
 		}
 		parent = dentry->d_parent;
 		prefetch(parent);
-		namelen = dentry->d_name.len;
-		buflen -= namelen + 1;
-		if (buflen < 0)
+		if ((prepend(&end, &buflen, dentry->d_name.name,
+				dentry->d_name.len) != 0) ||
+		    (prepend(&end, &buflen, "/", 1) != 0))
 			goto Elong;
-		end -= namelen;
-		memcpy(end, dentry->d_name.name, namelen);
-		*--end = '/';
 		retval = end;
 		dentry = parent;
 	}
@@ -1818,12 +1820,10 @@ static char *__d_path(struct dentry *den
 	return retval;
 
 global_root:
-	namelen = dentry->d_name.len;
-	buflen -= namelen;
-	if (buflen < 0)
+	retval += 1;	/* hit the slash */
+	if (prepend(&retval, &buflen, dentry->d_name.name,
+		    dentry->d_name.len) != 0)
 		goto Elong;
-	retval -= namelen-1;	/* hit the slash */
-	memcpy(retval, dentry->d_name.name, namelen);
 	return retval;
 Elong:
 	return ERR_PTR(-ENAMETOOLONG);
@@ -1859,7 +1859,7 @@ char *d_path(struct path *path, char *bu
 
 	read_lock(&current->fs->lock);
 	root = current->fs->root;
-	path_get(&current->fs->root);
+	path_get(&root);
 	read_unlock(&current->fs->lock);
 	spin_lock(&dcache_lock);
 	res = __d_path(path->dentry, path->mnt, &root, buf, buflen);
@@ -1890,6 +1890,48 @@ char *dynamic_dname(struct dentry *dentr
 }
 
 /*
+ * Write full pathname from the root of the filesystem into the buffer.
+ */
+char *dentry_path(struct dentry *dentry, char *buf, int buflen)
+{
+	char *end = buf + buflen;
+	char *retval;
+
+	spin_lock(&dcache_lock);
+	prepend(&end, &buflen, "\0", 1);
+	if (!IS_ROOT(dentry) && d_unhashed(dentry) &&
+		(prepend(&end, &buflen, "//deleted", 9) != 0))
+			goto Elong;
+	if (buflen < 1)
+		goto Elong;
+	/* Get '/' right */
+	retval = end-1;
+	*retval = '/';
+
+	for (;;) {
+		struct dentry *parent;
+		if (IS_ROOT(dentry))
+			break;
+
+		parent = dentry->d_parent;
+		prefetch(parent);
+
+		if ((prepend(&end, &buflen, dentry->d_name.name,
+				dentry->d_name.len) != 0) ||
+		    (prepend(&end, &buflen, "/", 1) != 0))
+			goto Elong;
+
+		retval = end;
+		dentry = parent;
+	}
+	spin_unlock(&dcache_lock);
+	return retval;
+Elong:
+	spin_unlock(&dcache_lock);
+	return ERR_PTR(-ENAMETOOLONG);
+}
+
+/*
  * NOTE! The user-level library version returns a
  * character pointer. The kernel system call just
  * returns the length of the buffer filled (which
@@ -1918,9 +1960,9 @@ asmlinkage long sys_getcwd(char __user *
 
 	read_lock(&current->fs->lock);
 	pwd = current->fs->pwd;
-	path_get(&current->fs->pwd);
+	path_get(&pwd);
 	root = current->fs->root;
-	path_get(&current->fs->root);
+	path_get(&root);
 	read_unlock(&current->fs->lock);
 
 	error = -ENOENT;
fs/seq_file.c +49 -16

83 lines not on this page · raw message

include/linux/dcache.h +1 -0
Index: vfs-2.6/include/linux/dcache.h
===================================================================
@@ -302,6 +302,7 @@ extern int d_validate(struct dentry *, s
 extern char *dynamic_dname(struct dentry *, char *, int, const char *, ...);
 
 extern char *d_path(struct path *, char *, int);
+extern char *dentry_path(struct dentry *, char *, int);
 
 /* Allocation counts.. */
 
include/linux/seq_file.h +2 -0
Index: vfs-2.6/include/linux/seq_file.h
===================================================================
@@ -10,6 +10,7 @@ struct seq_operations;
 struct file;
 struct path;
 struct inode;
+struct dentry;
 
 struct seq_file {
 	char *buf;
@@ -42,6 +43,7 @@ int seq_printf(struct seq_file *, const 
 	__attribute__ ((format (printf,2,3)));
 
 int seq_path(struct seq_file *, struct path *, char *);
+int seq_dentry(struct seq_file *, struct dentry *, char *);
 
 int single_open(struct file *, int (*)(struct seq_file *, void *), void *);
 int single_release(struct inode *, struct file *);

--

#2 lore raw

PATCH 2/7 vfs: mountinfo: add seq_file_root()

In reply to Miklos Szeredi

From: Miklos Szeredi <[email protected]>

Add a new function:

seq_file_root()

This is similar to seq_path(), but calculates the path relative to the given root, instead of current->fs->root. If the path was unreachable from root, then modify the root parameter to reflect this.

---
 fs/dcache.c              |   24 ++++++++++++++++--------
 fs/seq_file.c            |   30 ++++++++++++++++++++++++++++++
 include/linux/dcache.h   |    1 +
 include/linux/seq_file.h |    2 ++
 4 files changed, 49 insertions(+), 8 deletions(-)
fs/dcache.c +16 -8
Index: vfs-2.6/fs/dcache.c
===================================================================
@@ -1759,10 +1759,8 @@ static int prepend(char **buffer, int *b
 
 /**
  * d_path - return the path of a dentry
- * @dentry: dentry to report
- * @vfsmnt: vfsmnt to which the dentry belongs
- * @root: root dentry
- * @rootmnt: vfsmnt to which the root dentry belongs
+ * @path: the dentry/vfsmount to report
+ * @root: root vfsmnt/dentry (may be modified by this function)
  * @buffer: buffer to return value in
  * @buflen: buffer length
  *
@@ -1772,10 +1770,15 @@ static int prepend(char **buffer, int *b
  * Returns the buffer or an error code if the path was too long.
  *
  * "buflen" should be positive. Caller holds the dcache_lock.
+ *
+ * If path is not reachable from the supplied root, then the value of
+ * root is changed (without modifying refcounts).
  */
-static char *__d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
-		       struct path *root, char *buffer, int buflen)
+char *__d_path(const struct path *path, struct path *root,
+	       char *buffer, int buflen)
 {
+	struct dentry *dentry = path->dentry;
+	struct vfsmount *vfsmnt = path->mnt;
 	char * end = buffer+buflen;
 	char * retval;
 
@@ -1824,6 +1827,8 @@ global_root:
 	if (prepend(&retval, &buflen, dentry->d_name.name,
 		    dentry->d_name.len) != 0)
 		goto Elong;
+	root->mnt = vfsmnt;
+	root->dentry = dentry;
 	return retval;
 Elong:
 	return ERR_PTR(-ENAMETOOLONG);
@@ -1846,6 +1851,7 @@ char *d_path(struct path *path, char *bu
 {
 	char *res;
 	struct path root;
+	struct path tmp;
 
 	/*
 	 * We have various synthetic filesystems that never get mounted.  On
@@ -1862,7 +1868,8 @@ char *d_path(struct path *path, char *bu
 	path_get(&root);
 	read_unlock(&current->fs->lock);
 	spin_lock(&dcache_lock);
-	res = __d_path(path->dentry, path->mnt, &root, buf, buflen);
+	tmp = root;
+	res = __d_path(path, &tmp, buf, buflen);
 	spin_unlock(&dcache_lock);
 	path_put(&root);
 	return res;
@@ -1970,9 +1977,10 @@ asmlinkage long sys_getcwd(char __user *
 	spin_lock(&dcache_lock);
 	if (pwd.dentry->d_parent == pwd.dentry || !d_unhashed(pwd.dentry)) {
 		unsigned long len;
+		struct path tmp = root;
 		char * cwd;
 
-		cwd = __d_path(pwd.dentry, pwd.mnt, &root, page, PAGE_SIZE);
+		cwd = __d_path(&pwd, &tmp, page, PAGE_SIZE);
 		spin_unlock(&dcache_lock);
 
 		error = PTR_ERR(cwd);
fs/seq_file.c +30 -0
Index: vfs-2.6/fs/seq_file.c
===================================================================
@@ -385,6 +385,36 @@ int seq_path(struct seq_file *m, struct 
 EXPORT_SYMBOL(seq_path);
 
 /*
+ * Same as seq_path, but relative to supplied root.
+ *
+ * root may be changed, see __d_path().
+ */
+int seq_path_root(struct seq_file *m, struct path *path, struct path *root,
+		  char *esc)
+{
+	int err = -ENAMETOOLONG;
+	if (m->count < m->size) {
+		char *s = m->buf + m->count;
+		char *p;
+
+		spin_lock(&dcache_lock);
+		p = __d_path(path, root, s, m->size - m->count);
+		spin_unlock(&dcache_lock);
+		err = PTR_ERR(p);
+		if (!IS_ERR(p)) {
+			s = mangle_path(s, p, esc);
+			if (s) {
+				p = m->buf + m->count;
+				m->count = s - m->buf;
+				return 0;
+			}
+		}
+	}
+	m->count = m->size;
+	return err;
+}
+
+/*
  * returns the path of the 'dentry' from the root of its filesystem.
  */
 int seq_dentry(struct seq_file *m, struct dentry *dentry, char *esc)
include/linux/dcache.h +1 -0
Index: vfs-2.6/include/linux/dcache.h
===================================================================
@@ -301,6 +301,7 @@ extern int d_validate(struct dentry *, s
  */
 extern char *dynamic_dname(struct dentry *, char *, int, const char *, ...);
 
+extern char *__d_path(const struct path *path, struct path *root, char *, int);
 extern char *d_path(struct path *, char *, int);
 extern char *dentry_path(struct dentry *, char *, int);
 
include/linux/seq_file.h +2 -0
Index: vfs-2.6/include/linux/seq_file.h
===================================================================
@@ -44,6 +44,8 @@ int seq_printf(struct seq_file *, const 
 
 int seq_path(struct seq_file *, struct path *, char *);
 int seq_dentry(struct seq_file *, struct dentry *, char *);
+int seq_path_root(struct seq_file *m, struct path *path, struct path *root,
+		  char *esc);
 
 int single_open(struct file *, int (*)(struct seq_file *, void *), void *);
 int single_release(struct inode *, struct file *);

--

#3 lore raw

PATCH 3/7 vfs: mountinfo: add mount ID

In reply to Miklos Szeredi

From: Miklos Szeredi <[email protected]>

Add a unique ID to each vfsmount using the IDR infrastructure. The identifiers are reused after the vfsmount is freed.

---
 fs/namespace.c        |   34 ++++++++++++++++++++++++++++++++++
 include/linux/mount.h |    1 +
 2 files changed, 35 insertions(+)
fs/namespace.c +34 -0
Index: vfs-2.6/fs/namespace.c
===================================================================
@@ -27,6 +27,7 @@
 #include <linux/mount.h>
 #include <linux/ramfs.h>
 #include <linux/log2.h>
+#include <linux/idr.h>
 #include <asm/uaccess.h>
 #include <asm/unistd.h>
 #include "pnode.h"
@@ -39,6 +40,7 @@
 __cacheline_aligned_in_smp DEFINE_SPINLOCK(vfsmount_lock);
 
 static int event;
+static DEFINE_IDA(mnt_id_ida);
 
 static struct list_head *mount_hashtable __read_mostly;
 static struct kmem_cache *mnt_cache __read_mostly;
@@ -58,10 +60,41 @@ static inline unsigned long hash(struct 
 
 #define MNT_WRITER_UNDERFLOW_LIMIT -(1<<16)
 
+static int mnt_alloc_id(struct vfsmount *mnt)
+{
+	int res;
+
+ retry:
+	spin_lock(&vfsmount_lock);
+	res = ida_get_new(&mnt_id_ida, &mnt->mnt_id);
+	spin_unlock(&vfsmount_lock);
+	if (res == -EAGAIN) {
+		if (ida_pre_get(&mnt_id_ida, GFP_KERNEL))
+			goto retry;
+		res = -ENOMEM;
+	}
+	return res;
+}
+
+static void mnt_free_id(struct vfsmount *mnt)
+{
+	spin_lock(&vfsmount_lock);
+	ida_remove(&mnt_id_ida, mnt->mnt_id);
+	spin_unlock(&vfsmount_lock);
+}
+
 struct vfsmount *alloc_vfsmnt(const char *name)
 {
 	struct vfsmount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
 	if (mnt) {
+		int err;
+
+		err = mnt_alloc_id(mnt);
+		if (err) {
+			kmem_cache_free(mnt_cache, mnt);
+			return NULL;
+		}
+
 		atomic_set(&mnt->mnt_count, 1);
 		INIT_LIST_HEAD(&mnt->mnt_hash);
 		INIT_LIST_HEAD(&mnt->mnt_child);
@@ -353,6 +386,7 @@ EXPORT_SYMBOL(simple_set_mnt);
 void free_vfsmnt(struct vfsmount *mnt)
 {
 	kfree(mnt->mnt_devname);
+	mnt_free_id(mnt);
 	kmem_cache_free(mnt_cache, mnt);
 }
 
include/linux/mount.h +1 -0
Index: vfs-2.6/include/linux/mount.h
===================================================================
@@ -56,6 +56,7 @@ struct vfsmount {
 	struct list_head mnt_slave;	/* slave list entry */
 	struct vfsmount *mnt_master;	/* slave is on master->mnt_slave_list */
 	struct mnt_namespace *mnt_ns;	/* containing namespace */
+	int mnt_id;			/* mount identifier */
 	/*
 	 * We put mnt_count & mnt_expiry_mark at the end of struct vfsmount
 	 * to let these frequently modified fields in a separate cache line

--

maintainer
#4 lore raw

Re: [patch 3/7] vfs: mountinfo: add mount ID

Quoted 4 lines · Miklos Szeredi
fs/namespace.c +4 -0
+static int mnt_alloc_id(struct vfsmount *mnt)
+{
+	int res;
+

fs/namespace.c static inline unsigned long hash(struct · line 76

Quoted 10 lines
fs/namespace.c +10 -0
+ retry:
+	spin_lock(&vfsmount_lock);
+	res = ida_get_new(&mnt_id_ida, &mnt->mnt_id);
+	spin_unlock(&vfsmount_lock);
+	if (res == -EAGAIN) {
+		if (ida_pre_get(&mnt_id_ida, GFP_KERNEL))
+			goto retry;
+		res = -ENOMEM;
+	}
+	return res;

*Ugh*

Why bother with vfsmount_lock here? All allocations are done under namespace_sem. Moreover, I'd rather replace that 'goto retry' with a single call of ida_get_new(), since we are serialized anyway.

fs/namespace.c EXPORT_SYMBOL(simple_set_mnt); · line 391

Quoted 7 lines
fs/namespace.c +1 -0
@@ -353,6 +386,7 @@ EXPORT_SYMBOL(simple_set_mnt);
 void free_vfsmnt(struct vfsmount *mnt)
 {
 	kfree(mnt->mnt_devname);
+	mnt_free_id(mnt);
 	kmem_cache_free(mnt_cache, mnt);
 }

... and I'd rather do that earlier, e.g. in umount_tree(). At that point we (a) have namespace_sem and (b) irrevocably kick the sucker out of any namespace.

I'd rather minimize banging vfsmount_lock like that...

#5 lore raw

PATCH 4/7 vfs: mountinfo: add mount peer group ID

In reply to Miklos Szeredi

From: Miklos Szeredi <[email protected]>

Add a unique ID to each peer group using the IDR infrastructure. The identifiers are reused after the peer group dissolves.

The IDR structures are protected by holding namepspace_sem for write while allocating or deallocating ID's.

ID's are allocated when a previously unshared vfsmount becomes the first member of a peer group. When a new member is added to an existing group, the ID is copied from one of the old members.

ID's are freed when the last member of a peer group is unshared.

Setting the MNT_SHARED flag on members of a subtree is done as a separate step, after all the ID's have been allocated. This way an allocation failure can be cleaned up easilty, without affecting the propagation state.

Based on design sketch by Al Viro.

---
 fs/namespace.c        |  100 ++++++++++++++++++++++++++++++++++++++++++++++++--
 fs/pnode.c            |   11 +++--
 fs/pnode.h            |    1 
 include/linux/mount.h |    1 
 4 files changed, 106 insertions(+), 7 deletions(-)
fs/namespace.c +97 -3
Index: vfs-2.6/fs/namespace.c
===================================================================
@@ -41,6 +41,7 @@ __cacheline_aligned_in_smp DEFINE_SPINLO
 
 static int event;
 static DEFINE_IDA(mnt_id_ida);
+static DEFINE_IDA(mnt_group_ida);
 
 static struct list_head *mount_hashtable __read_mostly;
 static struct kmem_cache *mnt_cache __read_mostly;
@@ -83,6 +84,35 @@ static void mnt_free_id(struct vfsmount 
 	spin_unlock(&vfsmount_lock);
 }
 
+/*
+ * Allocate a new peer group ID
+ *
+ * mnt_group_ida is protected by namespace_sem
+ */
+static int mnt_alloc_group_id(struct vfsmount *mnt)
+{
+	int res;
+
+	WARN_ON(mnt->mnt_group_id != 0);
+ retry:
+	res = ida_get_new_above(&mnt_group_ida, 1, &mnt->mnt_group_id);
+	if (res == -EAGAIN) {
+		if (ida_pre_get(&mnt_group_ida, GFP_KERNEL))
+			goto retry;
+		res = -ENOMEM;
+	}
+	return res;
+}
+
+/*
+ * Release a peer group ID
+ */
+void mnt_release_group_id(struct vfsmount *mnt)
+{
+	ida_remove(&mnt_group_ida, mnt->mnt_group_id);
+	mnt->mnt_group_id = 0;
+}
+
 struct vfsmount *alloc_vfsmnt(const char *name)
 {
 	struct vfsmount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
@@ -533,6 +563,17 @@ static struct vfsmount *clone_mnt(struct
 	struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname);
 
 	if (mnt) {
+		if (flag & (CL_SLAVE | CL_PRIVATE))
+			mnt->mnt_group_id = 0; /* not a peer of original */
+		else
+			mnt->mnt_group_id = old->mnt_group_id;
+
+		if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
+			int err = mnt_alloc_group_id(mnt);
+			if (err)
+				goto out_free;
+		}
+
 		mnt->mnt_flags = old->mnt_flags;
 		atomic_inc(&sb->s_active);
 		mnt->mnt_sb = sb;
@@ -562,6 +603,10 @@ static struct vfsmount *clone_mnt(struct
 		}
 	}
 	return mnt;
+
+ out_free:
+	free_vfsmnt(mnt);
+	return NULL;
 }
 
 static inline void __mntput(struct vfsmount *mnt)
@@ -1142,6 +1187,33 @@ void drop_collected_mounts(struct vfsmou
 	release_mounts(&umount_list);
 }
 
+static void cleanup_group_ids(struct vfsmount *mnt, struct vfsmount *end)
+{
+	struct vfsmount *p;
+
+	for (p = mnt; p != end; p = next_mnt(p, mnt)) {
+		if (list_empty(&p->mnt_share) && !IS_MNT_SHARED(p))
+			mnt_release_group_id(p);
+	}
+}
+
+static int invent_group_ids(struct vfsmount *mnt, bool recurse)
+{
+	struct vfsmount *p;
+
+	for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
+		if (list_empty(&p->mnt_share) && !IS_MNT_SHARED(p)) {
+			int err = mnt_alloc_group_id(p);
+			if (err) {
+				cleanup_group_ids(mnt, p);
+				return err;
+			}
+		}
+	}
+
+	return 0;
+}
+
 /*
  *  @source_mnt : mount tree to be attached
  *  @nd         : place the mount tree @source_mnt is attached
@@ -1212,9 +1284,16 @@ static int attach_recursive_mnt(struct v
 	struct vfsmount *dest_mnt = path->mnt;
 	struct dentry *dest_dentry = path->dentry;
 	struct vfsmount *child, *p;
+	int err;
 
-	if (propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list))
-		return -EINVAL;
+	if (IS_MNT_SHARED(dest_mnt)) {
+		err = invent_group_ids(source_mnt, true);
+		if (err)
+			goto out;
+	}
+	err = propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list);
+	if (err)
+		goto out_cleanup_ids;
 
 	if (IS_MNT_SHARED(dest_mnt)) {
 		for (p = source_mnt; p; p = next_mnt(p, source_mnt))
@@ -1237,6 +1316,12 @@ static int attach_recursive_mnt(struct v
 	}
 	spin_unlock(&vfsmount_lock);
 	return 0;
+
+ out_cleanup_ids:
+	if (IS_MNT_SHARED(dest_mnt))
+		cleanup_group_ids(source_mnt, NULL);
+ out:
+	return err;
 }
 
 static int graft_tree(struct vfsmount *mnt, struct path *path)
@@ -1277,6 +1362,7 @@ static noinline int do_change_type(struc
 	struct vfsmount *m, *mnt = nd->path.mnt;
 	int recurse = flag & MS_REC;
 	int type = flag & ~MS_REC;
+	int err = 0;
 
 	if (!capable(CAP_SYS_ADMIN))
 		return -EPERM;
@@ -1285,12 +1371,20 @@ static noinline int do_change_type(struc
 		return -EINVAL;
 
 	down_write(&namespace_sem);
+	if (type == MS_SHARED) {
+		err = invent_group_ids(mnt, recurse);
+		if (err)
+			goto out_unlock;
+	}
+
 	spin_lock(&vfsmount_lock);
 	for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
 		change_mnt_propagation(m, type);
 	spin_unlock(&vfsmount_lock);
+
+ out_unlock:
 	up_write(&namespace_sem);
-	return 0;
+	return err;
 }
 
 /*
fs/pnode.c +7 -4
Index: vfs-2.6/fs/pnode.c
===================================================================
@@ -46,7 +46,11 @@ static int do_make_slave(struct vfsmount
 		if (peer_mnt == mnt)
 			peer_mnt = NULL;
 	}
+	if (IS_MNT_SHARED(mnt) && list_empty(&mnt->mnt_share))
+		mnt_release_group_id(mnt);
+
 	list_del_init(&mnt->mnt_share);
+	mnt->mnt_group_id = 0;
 
 	if (peer_mnt)
 		master = peer_mnt;
@@ -58,9 +62,9 @@ static int do_make_slave(struct vfsmount
 		list_splice(&mnt->mnt_slave_list, master->mnt_slave_list.prev);
 		INIT_LIST_HEAD(&mnt->mnt_slave_list);
 	} else {
-		struct list_head *p = &mnt->mnt_slave_list;
-		while (!list_empty(p)) {
-                        slave_mnt = list_first_entry(p,
+		struct list_head *slaves = &mnt->mnt_slave_list;
+		while (!list_empty(slaves)) {
+                        slave_mnt = list_first_entry(slaves,
 					struct vfsmount, mnt_slave);
 			list_del_init(&slave_mnt->mnt_slave);
 			slave_mnt->mnt_master = NULL;
@@ -68,7 +72,6 @@ static int do_make_slave(struct vfsmount
 	}
 	mnt->mnt_master = master;
 	CLEAR_MNT_SHARED(mnt);
-	INIT_LIST_HEAD(&mnt->mnt_slave_list);
 	return 0;
 }
 
fs/pnode.h +1 -0
Index: vfs-2.6/fs/pnode.h
===================================================================
@@ -35,4 +35,5 @@ int propagate_mnt(struct vfsmount *, str
 		struct list_head *);
 int propagate_umount(struct list_head *);
 int propagate_mount_busy(struct vfsmount *, int);
+void mnt_release_group_id(struct vfsmount *);
 #endif /* _LINUX_PNODE_H */
include/linux/mount.h +1 -0
Index: vfs-2.6/include/linux/mount.h
===================================================================
@@ -57,6 +57,7 @@ struct vfsmount {
 	struct vfsmount *mnt_master;	/* slave is on master->mnt_slave_list */
 	struct mnt_namespace *mnt_ns;	/* containing namespace */
 	int mnt_id;			/* mount identifier */
+	int mnt_group_id;		/* peer group identifier */
 	/*
 	 * We put mnt_count & mnt_expiry_mark at the end of struct vfsmount
 	 * to let these frequently modified fields in a separate cache line

--

maintainer
#6 lore raw

Re: [patch 4/7] vfs: mountinfo: add mount peer group ID

Quoted 5 lines · Miklos Szeredi
fs/namespace.c +5 -0
+static int mnt_alloc_group_id(struct vfsmount *mnt)
+{
+	int res;
+
+	WARN_ON(mnt->mnt_group_id != 0);

fs/namespace.c static void mnt_free_id(struct vfsmount · line 106

Quoted 10 lines
fs/namespace.c +10 -0
+ retry:
+	res = ida_get_new_above(&mnt_group_ida, 1, &mnt->mnt_group_id);
+	if (res == -EAGAIN) {
+		if (ida_pre_get(&mnt_group_ida, GFP_KERNEL))
+			goto retry;
+		res = -ENOMEM;
+	}
+	return res;
+}
+

Same comment on goto retry

fs/namespace.c static void mnt_free_id(struct vfsmount · line 114

Quoted 8 lines
fs/namespace.c +8 -0
+/*
+ * Release a peer group ID
+ */
+void mnt_release_group_id(struct vfsmount *mnt)
+{
+	ida_remove(&mnt_group_ida, mnt->mnt_group_id);
+	mnt->mnt_group_id = 0;
+}

Erm... What if that sucker has zero group ID to start with? You do call it that way later.

fs/namespace.c void drop_collected_mounts(struct vfsmou · line 1198

Quoted 9 lines
fs/namespace.c +9 -0
+static void cleanup_group_ids(struct vfsmount *mnt, struct vfsmount *end)
+{
+	struct vfsmount *p;
+
+	for (p = mnt; p != end; p = next_mnt(p, mnt)) {
+		if (list_empty(&p->mnt_share) && !IS_MNT_SHARED(p))
+			mnt_release_group_id(p);
+	}
+}

I'd make that p->mnt_group_id && !IS_MNT_SHARED(p), actually...

fs/namespace.c void drop_collected_mounts(struct vfsmou · line 1212

Quoted 13 lines
fs/namespace.c +13 -0
+static int invent_group_ids(struct vfsmount *mnt, bool recurse)
+{
+	struct vfsmount *p;
+
+	for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
+		if (list_empty(&p->mnt_share) && !IS_MNT_SHARED(p)) {
+			int err = mnt_alloc_group_id(p);
+			if (err) {
+				cleanup_group_ids(mnt, p);
+				return err;
+			}
+		}
+	}

Same comment

fs/pnode.c static int do_make_slave(struct vfsmount · line 70

Quoted 13 lines
fs/pnode.c +3 -3
@@ -58,9 +62,9 @@ static int do_make_slave(struct vfsmount
 		list_splice(&mnt->mnt_slave_list, master->mnt_slave_list.prev);
 		INIT_LIST_HEAD(&mnt->mnt_slave_list);
 	} else {
-		struct list_head *p = &mnt->mnt_slave_list;
-		while (!list_empty(p)) {
-                        slave_mnt = list_first_entry(p,
+		struct list_head *slaves = &mnt->mnt_slave_list;
+		while (!list_empty(slaves)) {
+                        slave_mnt = list_first_entry(slaves,
 					struct vfsmount, mnt_slave);
 			list_del_init(&slave_mnt->mnt_slave);
 			slave_mnt->mnt_master = NULL;

What is that renaming doing here?

#7 lore raw

PATCH 5/7 vfs: mountinfo: allow using process root

In reply to Miklos Szeredi

From: Miklos Szeredi <[email protected]>

Allow /proc/<pid>/mountinfo to use the root of <pid> to calculate mountpoints.

- move definition of 'struct proc_mounts' to <linux/mnt_namespace.h>
- add the process's namespace and root to this structure
- pass a pointer to 'struct proc_mounts' into seq_operations

In addition the following cleanups are made:

- use a common open function for /proc/<pid>/{mounts,mountstat}
- surround namespace.c part of these proc files with #ifdef CONFIG_PROC_FS
- make the seq_operations structures const

---
 fs/namespace.c                |   14 +++--
 fs/proc/base.c                |  108 +++++++++++++++++++-----------------------
 include/linux/mnt_namespace.h |   11 ++++
 3 files changed, 70 insertions(+), 63 deletions(-)
fs/namespace.c +8 -6
Index: vfs-2.6/fs/namespace.c
===================================================================
@@ -731,20 +731,21 @@ void save_mount_options(struct super_blo
 }
 EXPORT_SYMBOL(save_mount_options);
 
+#ifdef CONFIG_PROC_FS
 /* iterator */
 static void *m_start(struct seq_file *m, loff_t *pos)
 {
-	struct mnt_namespace *n = m->private;
+	struct proc_mounts *p = m->private;
 
 	down_read(&namespace_sem);
-	return seq_list_start(&n->list, *pos);
+	return seq_list_start(&p->ns->list, *pos);
 }
 
 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
 {
-	struct mnt_namespace *n = m->private;
+	struct proc_mounts *p = m->private;
 
-	return seq_list_next(v, &n->list, pos);
+	return seq_list_next(v, &p->ns->list, pos);
 }
 
 static void m_stop(struct seq_file *m, void *v)
@@ -801,7 +802,7 @@ static int show_vfsmnt(struct seq_file *
 	return err;
 }
 
-struct seq_operations mounts_op = {
+const struct seq_operations mounts_op = {
 	.start	= m_start,
 	.next	= m_next,
 	.stop	= m_stop,
@@ -840,12 +841,13 @@ static int show_vfsstat(struct seq_file 
 	return err;
 }
 
-struct seq_operations mountstats_op = {
+const struct seq_operations mountstats_op = {
 	.start	= m_start,
 	.next	= m_next,
 	.stop	= m_stop,
 	.show	= show_vfsstat,
 };
+#endif  /* CONFIG_PROC_FS */
 
 /**
  * may_umount_tree - check if a mount tree is busy
fs/proc/base.c +51 -57

152 lines not on this page · raw message

include/linux/mnt_namespace.h +11 -0
Index: vfs-2.6/include/linux/mnt_namespace.h
===================================================================
@@ -5,6 +5,7 @@
 #include <linux/mount.h>
 #include <linux/sched.h>
 #include <linux/nsproxy.h>
+#include <linux/seq_file.h>
 
 struct mnt_namespace {
 	atomic_t		count;
@@ -14,6 +15,13 @@ struct mnt_namespace {
 	int event;
 };
 
+struct proc_mounts {
+	struct seq_file m; /* must be the first element */
+	struct mnt_namespace *ns;
+	struct path root;
+	int event;
+};
+
 extern struct mnt_namespace *copy_mnt_ns(unsigned long, struct mnt_namespace *,
 		struct fs_struct *);
 extern void __put_mnt_ns(struct mnt_namespace *ns);
@@ -37,5 +45,8 @@ static inline void get_mnt_ns(struct mnt
 	atomic_inc(&ns->count);
 }
 
+extern const struct seq_operations mounts_op;
+extern const struct seq_operations mountstats_op;
+
 #endif
 #endif

--

#8 lore raw

PATCH 6/7 vfs: mountinfo: add /proc/<pid>/mountinfo

In reply to Miklos Szeredi

From: Ram Pai <[email protected]>

[[email protected]] rewrite and split big patch into managable chunks

/proc/mounts in its current form lacks important information:

- propagation state
- root of mount for bind mounts
- the st_dev value used within the filesystem
- identifier for each mount and it's parent

It also suffers from the following problems:

- not easily extendable
- ambiguity of mountpoints within a chrooted environment
- doesn't distinguish between filesystem dependent and independent options
- doesn't distinguish between per mount and per super block options

This patch introduces /proc/<pid>/mountinfo which attempts to address all these deficiencies.

Code shared between /proc/<pid>/mounts and /proc/<pid>/mountinfo is extracted into separate functions.

Thanks to Al Viro for the help in getting the design right.

---
 Documentation/filesystems/proc.txt |   33 ++++++++++
 fs/namespace.c                     |  121 +++++++++++++++++++++++++++++--------
 fs/proc/base.c                     |   15 ++++
 include/linux/mnt_namespace.h      |    1 
 4 files changed, 147 insertions(+), 23 deletions(-)
Documentation/filesystems/proc.txt +33 -0
Index: vfs-2.6/Documentation/filesystems/proc.txt
===================================================================
@@ -43,6 +43,7 @@ Table of Contents
   2.13	/proc/<pid>/oom_score - Display current oom-killer score
   2.14	/proc/<pid>/io - Display the IO accounting fields
   2.15	/proc/<pid>/coredump_filter - Core dump filtering settings
+  2.16	/proc/<pid>/mountinfo - Information about mounts
 
 ------------------------------------------------------------------------------
 Preface
@@ -2348,4 +2349,36 @@ For example:
   $ echo 0x7 > /proc/self/coredump_filter
   $ ./some_program
 
+2.16	/proc/<pid>/mountinfo - Information about mounts
+--------------------------------------------------------
+
+This file contains lines of the form:
+
+36 35 98:0 /mnt1 /mnt2 rw,noatime rw master:1 - ext3 /dev/root,errors=continue
+(1)(2)(3)   (4)   (5)      (6)    (7)  (8)   (9) (10)   (11)      (12)
+
+(1) mount ID:  unique identifier of the mount (may be reused after umount)
+(2) parent ID:  ID of parent (or of self for the top of the mount tree)
+(3) major:minor:  value of st_dev for files on filesystem
+(4) root:  root of the mount within the filesystem
+(5) mount point:  mount point relative to the process's root
+(6) mount options:  per mount options
+(7) super options:  filesystem type independent, per super block options
+(8) optional fields:  zero or more fields of the form "tag[:value]"
+(9) separator:  marks the end of the optional fields
+(10) filesystem type:  name of filesystem of the form "type[.subtype]"
+(11) mount source:  filesystem specific information or "none"
+(12) filesystem options:  filesystem type dependent options
+
+Parsers should ignore all unrecognised optional fields.  Currently the
+possible optional fields are:
+
+shared:X  mount is shared in peer group X
+master:X  mount is slave to peer group X
+unbindable  mount is unbindable
+
+For more information on mount propagation see:
+
+  Documentation/filesystems/sharedsubtree.txt
+
 ------------------------------------------------------------------------------
fs/namespace.c +98 -23

158 lines not on this page · raw message

fs/proc/base.c +15 -0
Index: vfs-2.6/fs/proc/base.c
===================================================================
@@ -604,6 +604,19 @@ static const struct file_operations proc
 	.poll		= mounts_poll,
 };
 
+static int mountinfo_open(struct inode *inode, struct file *file)
+{
+	return mounts_open_common(inode, file, &mountinfo_op);
+}
+
+static const struct file_operations proc_mountinfo_operations = {
+	.open		= mountinfo_open,
+	.read		= seq_read,
+	.llseek		= seq_lseek,
+	.release	= mounts_release,
+	.poll		= mounts_poll,
+};
+
 static int mountstats_open(struct inode *inode, struct file *file)
 {
 	return mounts_open_common(inode, file, &mountstats_op);
@@ -2285,6 +2298,7 @@ static const struct pid_entry tgid_base_
 	LNK("root",       root),
 	LNK("exe",        exe),
 	REG("mounts",     S_IRUGO, mounts),
+	REG("mountinfo",  S_IRUGO, mountinfo),
 	REG("mountstats", S_IRUSR, mountstats),
 #ifdef CONFIG_PROC_PAGE_MONITOR
 	REG("clear_refs", S_IWUSR, clear_refs),
@@ -2616,6 +2630,7 @@ static const struct pid_entry tid_base_s
 	LNK("root",      root),
 	LNK("exe",       exe),
 	REG("mounts",    S_IRUGO, mounts),
+	REG("mountinfo",  S_IRUGO, mountinfo),
 #ifdef CONFIG_PROC_PAGE_MONITOR
 	REG("clear_refs", S_IWUSR, clear_refs),
 	REG("smaps",     S_IRUGO, smaps),
include/linux/mnt_namespace.h +1 -0
Index: vfs-2.6/include/linux/mnt_namespace.h
===================================================================
@@ -46,6 +46,7 @@ static inline void get_mnt_ns(struct mnt
 }
 
 extern const struct seq_operations mounts_op;
+extern const struct seq_operations mountinfo_op;
 extern const struct seq_operations mountstats_op;
 
 #endif

--

maintainer
#9 lore raw

Re: [patch 6/7] vfs: mountinfo: add /proc/<pid>/mountinfo

Documentation/filesystems/proc.txt For example: · line 2366

Quoted 13 lines · Miklos Szeredi
Documentation/filesystems/proc.txt +12 -0
+This file contains lines of the form:
+
+36 35 98:0 /mnt1 /mnt2 rw,noatime rw master:1 - ext3 /dev/root,errors=continue
+(1)(2)(3)   (4)   (5)      (6)    (7)  (8)   (9) (10)   (11)      (12)
+
+(1) mount ID:  unique identifier of the mount (may be reused after umount)
+(2) parent ID:  ID of parent (or of self for the top of the mount tree)
+(3) major:minor:  value of st_dev for files on filesystem
+(4) root:  root of the mount within the filesystem
+(5) mount point:  mount point relative to the process's root
+(6) mount options:  per mount options
 
+(7) super options:  filesystem type independent, per super block options

Move that one past fs type, please.

Documentation/filesystems/proc.txt For example: · line 2370

Quoted 4 lines
Documentation/filesystems/proc.txt +4 -0
+(8) optional fields:  zero or more fields of the form "tag[:value]"
+(9) separator:  marks the end of the optional fields
+(10) filesystem type:  name of filesystem of the form "type[.subtype]"
+(11) mount source:  filesystem specific information or "none"

I'm not even sure that we *want* "none" here, to be honest, but that's a separate story...

#10 lore raw

Re: [patch 6/7] vfs: mountinfo: add /proc/<pid>/mountinfo

Quoted 12 lines
Quoted 11 lines
Documentation/filesystems/proc.txt +11 -0
+This file contains lines of the form:
+
+36 35 98:0 /mnt1 /mnt2 rw,noatime rw master:1 - ext3 /dev/root,errors=continue
+(1)(2)(3)   (4)   (5)      (6)    (7)  (8)   (9) (10)   (11)      (12)
+
+(1) mount ID:  unique identifier of the mount (may be reused after umount)
+(2) parent ID:  ID of parent (or of self for the top of the mount tree)
+(3) major:minor:  value of st_dev for files on filesystem
+(4) root:  root of the mount within the filesystem
+(5) mount point:  mount point relative to the process's root
+(6) mount options:  per mount options
Quoted 3 lines
Quoted 1 line

+(7) super options: filesystem type independent, per super block options

Move that one past fs type, please.

Can you please be a bit more specific? Because getting this right iteratively is going to be tiring.

36 35 98:0 /mnt1 /mnt2 rw,noatime master:1 - ext3 rw /dev/root,errors=continue

or

36 35 98:0 /mnt1 /mnt2 rw,noatime master:1 - ext3 /dev/root,rw,errors=continue

or perhaps

36 35 98:0 /mnt1 /mnt2 rw,noatime master:1 - ext3 /dev/root rw,errors=continue

or something else?

Miklos

#11 lore raw

PATCH 7/7 vfs: mountinfo: show dominating group id

In reply to Miklos Szeredi

From: Miklos Szeredi <[email protected]>

Show peer group ID of nearest dominating group that has intersection with the mount's namespace.

---
 Documentation/filesystems/proc.txt |    6 ++++
 fs/namespace.c                     |    9 +++++--
 fs/pnode.c                         |   45 +++++++++++++++++++++++++++++++++++++
 fs/pnode.h                         |    1 
 4 files changed, 59 insertions(+), 2 deletions(-)
Documentation/filesystems/proc.txt +6 -0
Index: vfs-2.6/Documentation/filesystems/proc.txt
===================================================================
@@ -2375,8 +2375,14 @@ possible optional fields are:
 
 shared:X  mount is shared in peer group X
 master:X  mount is slave to peer group X
+propagate_from:X  mount is slave and receives propagation from peer group X (*)
 unbindable  mount is unbindable
 
+(*) X is the closest dominant peer group under the process's root.  If
+X is the immediate master of the mount, or if there's no dominant peer
+group under the same root, then only the "master:X" field is present
+and not the "propagate_from:X" field.
+
 For more information on mount propagation see:
 
   Documentation/filesystems/sharedsubtree.txt
fs/namespace.c +7 -2
Index: vfs-2.6/fs/namespace.c
===================================================================
@@ -861,8 +861,13 @@ static int show_mountinfo(struct seq_fil
 	/* Tagged fields ("foo:X" or "bar") */
 	if (IS_MNT_SHARED(mnt))
 		seq_printf(m, " shared:%i", mnt->mnt_group_id);
-	if (IS_MNT_SLAVE(mnt))
-		seq_printf(m, " master:%i", mnt->mnt_master->mnt_group_id);
+	if (IS_MNT_SLAVE(mnt)) {
+		int master = mnt->mnt_master->mnt_group_id;
+		int dominator = get_dominator_id_same_root(mnt, &p->root);
+		seq_printf(m, " master:%i", master);
+		if (dominator && dominator != master)
+			seq_printf(m, " propagate_from:%i", dominator);
+	}
 	if (IS_MNT_UNBINDABLE(mnt))
 		seq_puts(m, " unbindable");
 
fs/pnode.c +45 -0

51 lines not on this page · raw message

fs/pnode.h +1 -0
Index: vfs-2.6/fs/pnode.h
===================================================================
@@ -36,4 +36,5 @@ int propagate_mnt(struct vfsmount *, str
 int propagate_umount(struct list_head *);
 int propagate_mount_busy(struct vfsmount *, int);
 void mnt_release_group_id(struct vfsmount *);
+int get_dominator_id_same_root(struct vfsmount *mnt, const struct path *root);
 #endif /* _LINUX_PNODE_H */

--

maintainer
#12 lore raw

Re: [patch 7/7] vfs: mountinfo: show dominating group id

fs/pnode.c static inline struct vfsmount *next_slav · line 47

Quoted 2 lines · Miklos Szeredi
fs/pnode.c +2 -0
+static struct vfsmount *get_peer_under_root(struct vfsmount *mnt,
+					    struct mnt_namespace *ns,

Isn't it mnt->mnt_ns in all cases?

Quoted 1 line

+int get_dominator_id_same_root(struct vfsmount *mnt, const struct path *root)

Open season for better name. Leaving aside .au associations, it's too
long and too uninformative... Will do for now, but we'd better come up
with something better.

#13 lore raw

Re: [patch 7/7] vfs: mountinfo: show dominating group id

Quoted 4 lines

fs/pnode.c static inline struct vfsmount *next_slav · line 47

Quoted 2 lines
fs/pnode.c +2 -0
+static struct vfsmount *get_peer_under_root(struct vfsmount *mnt,
+					    struct mnt_namespace *ns,

Isn't it mnt->mnt_ns in all cases?

Nope.

Miklos

#14 lore raw
In reply to Miklos Szeredi
Quoted 11 lines · On Wed, 26 Mar 2008 22:11:31 +0100 Miklos Szeredi <[email protected]> wrote:

Here's an updated mountinfo patch with the following changes relative to the last submission (the one currently in -mm):

- in __d_path() don't add extra parameter for checking unreachable path
- reduce proliferation of #ifdef CONFIG_PROC_FS into dcache.c and seq_file.c
- make peer group ID allocation a separate pass from setting mount shared
- change order and format of fields in /proc/<pid>/mountinfo
- remove dubious cleanups
- consolidate locking: don't use vfsmount_lock where namespace_sem suffices
- patch history was getting tangled up, merge and resplit into logical chunks

Quoted 3 lines

This series is also available here:

git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/vfs.git mountinfo

I coudn't get that to work, perhaps due to lack of gittiness.

Quoted 5 lines

I guess the simplest for all involved parties would be if this went into Al's tree (if it looks OK) and then Andrew can suck that tree (*) containing all sort of goodies into -mm.

(*) git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs-2.6.git vfs-2.6.25

I recently added that tree to the -mm lineup as git-vfs.patch, and it's in linux-next.

This patch series applied happily on top of git-vfs (and 887 other patches ;))