Skip to contentSkip to conversation

Read-only bind mounts

RFC PATCH In review Dave Hansen 3 participants 55 messages Jun 8, 2006 – Jun 12, 2006

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

cover Read-only bind mounts

The following series implements read-only bind mounts. This set does not take the previously tried approach of pushing down the vfsmount structure deeply into call paths, such that it might be checked in functions like permission(), may_create() and may_open(). Instead, it does checks near the entry points in the kernel, bumping a reference count in the vfsmount structure and effetively holding a kind of a lock on the vfsmount.

This approach will come in handy in the future, allowing transitions from rw to ro mounts while guaranteeing that no writers are currently active.

I apologize for so many small patches, but breaking them up like this really did help me find bugs. A rollup is here:

	http://sr71.net/~dave/patches/ro-bind-mounts-06-07-2006.patch

This series passes a test I got from Herbert Poetzl, checking to see that these r/o bind mounts have the same properties as regular r/o device mounts for a number of syscalls.

http://vserver.13thfloor.at/Experimental/BME/fstest-0.03.tar.bz2

Here is the script I used to generate and compare the output from that test:

#!/bin/sh
{ umount ro-bind-mount ro-mount;
rm -rf ro-mount-source ro-bind-mount-source ro-mount ro-bind-mount; dd if=/dev/zero of=ro-mount-source count=10000; mkfs.ext3 -F ro-mount-source;
mkdir ro-mount ro-bind-mount;
mkdir ro-bind-mount-source
} > /dev/null 2>&1

mount --bind -o ro ro-bind-mount-source ro-bind-mount mount -t ext3 -o loop,ro ro-mount-source ro-mount

function fstest_and_diff

{
        ARG1="$1"
        shift
        ARG2="$1"
        ./fstest -d "$ARG1" > "$ARG1".log;
        ./fstest -d "$ARG2" > "$ARG2".log;
        diff -ru "$ARG1".log "$ARG2".log
}

fstest_and_diff ro-mount ro-bind-mount

umount ro-bind-mount ro-mount

#1 lore raw

RFC PATCH 1/27 Add vfsmount writer count

This allows a vfsmount to keep track of how many instances of files open for write there are at a given time. This will be useful if it is ever desired to change a mount from r/w to r/o at runtime.

A mount can also potentially refuse to allow writers; this is why mnt_want_write() has a return value. However, that functionality will be added later in the series. For now, always allow new writers.

---

 lxc-dave/fs/namespace.c        |    1 +
 lxc-dave/include/linux/mount.h |   14 ++++++++++++++
 2 files changed, 15 insertions(+)
fs/namespace.c renamed from fs/namespace.c~convert-permission-to-file-and-vfs +1 -0
diff -puN fs/namespace.c~convert-permission-to-file-and-vfs fs/namespace.c
@@ -66,6 +66,7 @@ struct vfsmount *alloc_vfsmnt(const char
 	if (mnt) {
 		memset(mnt, 0, sizeof(struct vfsmount));
 		atomic_set(&mnt->mnt_count, 1);
+		/* atomic_set(&mnt->writer_count, 0); */
 		INIT_LIST_HEAD(&mnt->mnt_hash);
 		INIT_LIST_HEAD(&mnt->mnt_child);
 		INIT_LIST_HEAD(&mnt->mnt_mounts);
include/linux/mount.h renamed from include/linux/mount.h~convert-permission-to-file-and-vfs +14 -0
diff -puN include/linux/mount.h~convert-permission-to-file-and-vfs include/linux/mount.h
@@ -12,6 +12,8 @@
 #define _LINUX_MOUNT_H
 #ifdef __KERNEL__
 
+#include <linux/err.h>
+#include <linux/fs.h>
 #include <linux/types.h>
 #include <linux/list.h>
 #include <linux/spinlock.h>
@@ -38,6 +40,7 @@ struct vfsmount {
 	struct list_head mnt_mounts;	/* list of children, anchored here */
 	struct list_head mnt_child;	/* and going through their mnt_child */
 	atomic_t mnt_count;
+	atomic_t mnt_writers;
 	int mnt_flags;
 	int mnt_expiry_mark;		/* true if marked for expiry */
 	char *mnt_devname;		/* Name of device e.g. /dev/dsk/hda1 */
@@ -58,6 +61,17 @@ static inline struct vfsmount *mntget(st
 	return mnt;
 }
 
+static inline int mnt_want_write(struct vfsmount *mnt)
+{
+	atomic_inc(&mnt->mnt_writers);
+	return 0;
+}
+
+static inline void mnt_drop_write(struct vfsmount *mnt)
+{
+	atomic_dec(&mnt->mnt_writers);
+}
+
 extern void mntput_no_expire(struct vfsmount *mnt);
 extern void mnt_pin(struct vfsmount *mnt);
 extern void mnt_unpin(struct vfsmount *mnt);

diff -L convert-permission-to-file-and-vfs -puN /dev/null /dev/null diff -L convert-permission-to-file-and-vfs.patch -puN /dev/null /dev/null _

#2 lore raw

Re: [RFC][PATCH 01/27] Add vfsmount writer count

Quoted 22 lines · Dave Hansen

This allows a vfsmount to keep track of how many instances of files open for write there are at a given time. This will be useful if it is ever desired to change a mount from r/w to r/o at runtime.

A mount can also potentially refuse to allow writers; this is why mnt_want_write() has a return value. However, that functionality will be added later in the series. For now, always allow new writers.

---

 lxc-dave/fs/namespace.c        |    1 +
 lxc-dave/include/linux/mount.h |   14 ++++++++++++++
 2 files changed, 15 insertions(+)

diff -puN fs/namespace.c~convert-permission-to-file-and-vfs fs/namespace.c

fs/namespace.c struct vfsmount *alloc_vfsmnt(const char · line 69

Quoted 4 lines
fs/namespace.c renamed from fs/namespace.c~convert-permission-to-file-and-vfs +1 -0
@@ -66,6 +66,7 @@ struct vfsmount *alloc_vfsmnt(const char
 	if (mnt) {
 		memset(mnt, 0, sizeof(struct vfsmount));
 		atomic_set(&mnt->mnt_count, 1);
+		/* atomic_set(&mnt->writer_count, 0); */

IMHO you should not assume that atomic_set(,0) will not be required after memset(,0,), so doing it here is the right thing (tm)

Quoted 41 lines
fs/namespace.c renamed from fs/namespace.c~convert-permission-to-file-and-vfs +0 -0
 		INIT_LIST_HEAD(&mnt->mnt_hash);
 		INIT_LIST_HEAD(&mnt->mnt_child);
 		INIT_LIST_HEAD(&mnt->mnt_mounts);

diff -puN include/linux/mount.h~convert-permission-to-file-and-vfs include/linux/mount.h

include/linux/mount.h renamed from include/linux/mount.h~convert-permission-to-file-and-vfs +14 -0
@@ -12,6 +12,8 @@
 #define _LINUX_MOUNT_H
 #ifdef __KERNEL__
 
+#include <linux/err.h>
+#include <linux/fs.h>
 #include <linux/types.h>
 #include <linux/list.h>
 #include <linux/spinlock.h>
@@ -38,6 +40,7 @@ struct vfsmount {
 	struct list_head mnt_mounts;	/* list of children, anchored here */
 	struct list_head mnt_child;	/* and going through their mnt_child */
 	atomic_t mnt_count;
+	atomic_t mnt_writers;
 	int mnt_flags;
 	int mnt_expiry_mark;		/* true if marked for expiry */
 	char *mnt_devname;		/* Name of device e.g. /dev/dsk/hda1 */
@@ -58,6 +61,17 @@ static inline struct vfsmount *mntget(st
 	return mnt;
 }
 
+static inline int mnt_want_write(struct vfsmount *mnt)
+{
+	atomic_inc(&mnt->mnt_writers);
+	return 0;
+}
+
+static inline void mnt_drop_write(struct vfsmount *mnt)
+{
+	atomic_dec(&mnt->mnt_writers);
+}
+
 extern void mntput_no_expire(struct vfsmount *mnt);
 extern void mnt_pin(struct vfsmount *mnt);
 extern void mnt_unpin(struct vfsmount *mnt);
Quoted 3 lines

diff -L convert-permission-to-file-and-vfs -puN /dev/null /dev/null diff -L convert-permission-to-file-and-vfs.patch -puN /dev/null /dev/null _

best,
Herbert

#3 lore raw

RFC PATCH 2/27 vfs_rmdir: change if() into goto

In reply to Dave Hansen

We're going to add another condition here in a minute, so add a goto instead of an if.

There will be a lot of patches like this one. They may seem too finely-grained, but I did encounter a couple of bugs where I botched one of these. If there is a bug in one, this level of granularity will really help to track them down with a chop-search.

---

 lxc-dave/fs/namei.c |    9 +++++----
 1 files changed, 5 insertions(+), 4 deletions(-)
fs/namei.c renamed from fs/namei.c~rmdir +5 -4
diff -puN fs/namei.c~rmdir fs/namei.c
@@ -2010,10 +2010,11 @@ static long do_rmdir(int dfd, const char
 	mutex_lock_nested(&nd.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
 	dentry = lookup_hash(&nd);
 	error = PTR_ERR(dentry);
-	if (!IS_ERR(dentry)) {
-		error = vfs_rmdir(nd.dentry->d_inode, dentry);
-		dput(dentry);
-	}
+	if (IS_ERR(dentry))
+		goto exit2;
+	error = vfs_rmdir(nd.dentry->d_inode, dentry);
+	dput(dentry);
+exit2:
 	mutex_unlock(&nd.dentry->d_inode->i_mutex);
 exit1:
 	path_release(&nd);

_

Acked-by on v1 2/27
#4 lore raw

Re: [RFC][PATCH 02/27] vfs_rmdir: change if() into goto

Quoted 9 lines · Dave Hansen

We're going to add another condition here in a minute, so add a goto instead of an if.

There will be a lot of patches like this one. They may seem too finely-grained, but I did encounter a couple of bugs where I botched one of these. If there is a bug in one, this level of granularity will really help to track them down with a chop-search.

Quoted 26 lines

---

 lxc-dave/fs/namei.c |    9 +++++----
 1 files changed, 5 insertions(+), 4 deletions(-)

diff -puN fs/namei.c~rmdir fs/namei.c

fs/namei.c renamed from fs/namei.c~rmdir +5 -4
@@ -2010,10 +2010,11 @@ static long do_rmdir(int dfd, const char
 	mutex_lock_nested(&nd.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
 	dentry = lookup_hash(&nd);
 	error = PTR_ERR(dentry);
-	if (!IS_ERR(dentry)) {
-		error = vfs_rmdir(nd.dentry->d_inode, dentry);
-		dput(dentry);
-	}
+	if (IS_ERR(dentry))
+		goto exit2;
+	error = vfs_rmdir(nd.dentry->d_inode, dentry);
+	dput(dentry);
+exit2:
 	mutex_unlock(&nd.dentry->d_inode->i_mutex);
 exit1:
 	path_release(&nd);

_

#5 lore raw

RFC PATCH 3/27 do_rmdir(): elevate write count

In reply to Dave Hansen

Elevate the write count during the vfs_rmdir() call. There will be a lot of patches similar to this one coming up.

---

 lxc-dave/fs/namei.c |    5 +++++
 1 files changed, 5 insertions(+)
fs/namei.c renamed from fs/namei.c~rmdir1 +5 -0
diff -puN fs/namei.c~rmdir1 fs/namei.c
@@ -2012,7 +2012,12 @@ static long do_rmdir(int dfd, const char
 	error = PTR_ERR(dentry);
 	if (IS_ERR(dentry))
 		goto exit2;
+	error = mnt_want_write(nd.mnt);
+	if (error)
+		goto exit3;
 	error = vfs_rmdir(nd.dentry->d_inode, dentry);
+	mnt_drop_write(nd.mnt);
+exit3:
 	dput(dentry);
 exit2:
 	mutex_unlock(&nd.dentry->d_inode->i_mutex);

_

#6 lore raw

Re: [RFC][PATCH 03/27] do_rmdir(): elevate write count

Quoted 14 lines · Dave Hansen

Elevate the write count during the vfs_rmdir() call. There will be a lot of patches similar to this one coming up.

---

 lxc-dave/fs/namei.c |    5 +++++
 1 files changed, 5 insertions(+)

diff -puN fs/namei.c~rmdir1 fs/namei.c

fs/namei.c static long do_rmdir(int dfd, const char · line 2020

Quoted 9 lines
fs/namei.c renamed from fs/namei.c~rmdir1 +5 -0
@@ -2012,7 +2012,12 @@ static long do_rmdir(int dfd, const char
 	error = PTR_ERR(dentry);
 	if (IS_ERR(dentry))
 		goto exit2;
+	error = mnt_want_write(nd.mnt);
+	if (error)
+		goto exit3;
 	error = vfs_rmdir(nd.dentry->d_inode, dentry);
+	mnt_drop_write(nd.mnt);
+exit3:

IMHO, for consistency this should be named similar in all the different places, so maybe make that?

out_put:

or maybe:

out_rdonly:

(same goest for the following patches)
otherwise fine

best,
Herbert

Quoted 4 lines
fs/namei.c renamed from fs/namei.c~rmdir1 +0 -0
 	dput(dentry);
 exit2:
 	mutex_unlock(&nd.dentry->d_inode->i_mutex);

_

#7 lore raw

Re: [RFC][PATCH 03/27] do_rmdir(): elevate write count

Quoted 17 lines · Herbert Poetzl

fs/namei.c static long do_rmdir(int dfd, const char · line 2020

Quoted 13 lines

diff -puN fs/namei.c~rmdir1 fs/namei.c

fs/namei.c renamed from fs/namei.c~rmdir1 +5 -0
@@ -2012,7 +2012,12 @@ static long do_rmdir(int dfd, const char
 	error = PTR_ERR(dentry);
 	if (IS_ERR(dentry))
 		goto exit2;
+	error = mnt_want_write(nd.mnt);
+	if (error)
+		goto exit3;
 	error = vfs_rmdir(nd.dentry->d_inode, dentry);
+	mnt_drop_write(nd.mnt);
+exit3:

IMHO, for consistency this should be named similar in all the different places, so maybe make that?

Quoted 8 lines

out_put:

or maybe:

out_rdonly:

(same goest for the following patches)
otherwise fine

I'm not too set on it either way. I tried to be consistent with some of the style in the functions, especially when they had a _lot_ of goto conditions. This one is right on the cusp of where I want to rock the boat. ;)

-- Dave

#8 lore raw

RFC PATCH 4/27 elevate mnt writers for vfs_unlink() callers

In reply to Dave Hansen

---

 lxc-dave/fs/namei.c   |    4 ++++
 lxc-dave/ipc/mqueue.c |    5 ++++-
 2 files changed, 8 insertions(+), 1 deletion(-)
fs/namei.c renamed from fs/namei.c~elevate-writers-vfs_unlink +4 -0
diff -puN fs/namei.c~elevate-writers-vfs_unlink fs/namei.c
@@ -2097,7 +2097,11 @@ static long do_unlinkat(int dfd, const c
 		inode = dentry->d_inode;
 		if (inode)
 			atomic_inc(&inode->i_count);
+		error = mnt_want_write(nd.mnt);
+		if (error)
+			goto exit2;
 		error = vfs_unlink(nd.dentry->d_inode, dentry);
+		mnt_drop_write(nd.mnt);
 	exit2:
 		dput(dentry);
 	}

diff -puN fs/nfsd/nfs4recover.c~elevate-writers-vfs_unlink fs/nfsd/nfs4recover.c diff -puN fs/nfsd/vfs.c~elevate-writers-vfs_unlink fs/nfsd/vfs.c

ipc/mqueue.c renamed from ipc/mqueue.c~elevate-writers-vfs_unlink +4 -1
diff -puN ipc/mqueue.c~elevate-writers-vfs_unlink ipc/mqueue.c
@@ -738,8 +738,11 @@ asmlinkage long sys_mq_unlink(const char
 	inode = dentry->d_inode;
 	if (inode)
 		atomic_inc(&inode->i_count);
-
+	err = mnt_want_write(mqueue_mnt);
+	if (err)
+		goto out_err;
 	err = vfs_unlink(dentry->d_parent->d_inode, dentry);
+	mnt_drop_write(mqueue_mnt);
 out_err:
 	dput(dentry);
 

_

#9 lore raw

RFC PATCH 5/27 elevate mnt writers for nfsd caller of vfs_mkdir()

In reply to Dave Hansen

---

 fs/open.c                      |    0 
 lxc-dave/fs/nfsd/nfs4recover.c |    4 ++++
 2 files changed, 4 insertions(+)

diff -puN fs/namei.c~elevate-writers-vfs_mkdir fs/namei.c

fs/nfsd/nfs4recover.c renamed from fs/nfsd/nfs4recover.c~elevate-writers-vfs_mkdir +4 -0
diff -puN fs/nfsd/nfs4recover.c~elevate-writers-vfs_mkdir fs/nfsd/nfs4recover.c
@@ -155,7 +155,11 @@ nfsd4_create_clid_dir(struct nfs4_client
 		dprintk("NFSD: nfsd4_create_clid_dir: DIRECTORY EXISTS\n");
 		goto out_put;
 	}
+	status = mnt_want_write(rec_dir.mnt);
+	if (status)
+		goto out_put;
 	status = vfs_mkdir(rec_dir.dentry->d_inode, dentry, S_IRWXU);
+	mnt_drop_write(rec_dir.mnt);
 out_put:
 	dput(dentry);
 out_unlock:

diff -puN fs/nfsd/vfs.c~elevate-writers-vfs_mkdir fs/nfsd/vfs.c diff -puN ipc/mqueue.c~elevate-writers-vfs_mkdir ipc/mqueue.c diff -puN fs/open.c~elevate-writers-vfs_mkdir fs/open.c _

#10 lore raw

RFC PATCH 6/27 elevate write count during entire ncp_ioctl()

In reply to Dave Hansen

Some ioctls need write access, but others don't. Make a helper function to decode when write access is needed, and take it.

---

 fs/nfsd/vfs.c             |    0 
 lxc-dave/fs/ncpfs/ioctl.c |   54 +++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 53 insertions(+), 1 deletion(-)

diff -puN fs/namei.c~elevate-writers-file_permission-callers fs/namei.c diff -puN fs/open.c~elevate-writers-file_permission-callers fs/open.c diff -puN fs/exec.c~elevate-writers-file_permission-callers fs/exec.c

fs/ncpfs/ioctl.c renamed from fs/ncpfs/ioctl.c~elevate-writers-file_permission-callers +53 -1
diff -puN fs/ncpfs/ioctl.c~elevate-writers-file_permission-callers fs/ncpfs/ioctl.c
@@ -183,7 +183,7 @@ ncp_get_charsets(struct ncp_server* serv
 }
 #endif /* CONFIG_NCPFS_NLS */
 
-int ncp_ioctl(struct inode *inode, struct file *filp,
+static int __ncp_ioctl(struct inode *inode, struct file *filp,
 	      unsigned int cmd, unsigned long arg)
 {
 	struct ncp_server *server = NCP_SERVER(inode);
@@ -654,3 +654,55 @@ outrel:			
 /* #endif */
 	return -EINVAL;
 }
+
+static int ncp_ioctl_need_write(unsigned int cmd)
+{
+	switch (cmd) {
+        case NCP_IOC_GET_FS_INFO:
+        case NCP_IOC_GET_FS_INFO_V2:
+        case NCP_IOC_NCPREQUEST:
+        case NCP_IOC_SETDENTRYTTL:
+        case NCP_IOC_SIGN_INIT:
+        case NCP_IOC_LOCKUNLOCK:
+        case NCP_IOC_SET_SIGN_WANTED:
+		return 0;
+        case NCP_IOC_GETOBJECTNAME:
+        case NCP_IOC_SETOBJECTNAME:
+        case NCP_IOC_GETPRIVATEDATA:
+        case NCP_IOC_SETPRIVATEDATA:
+        case NCP_IOC_SETCHARSETS:
+        case NCP_IOC_GETCHARSETS:
+        case NCP_IOC_CONN_LOGGED_IN:
+        case NCP_IOC_GETDENTRYTTL:
+        case NCP_IOC_GETMOUNTUID2:
+        case NCP_IOC_SIGN_WANTED:
+        case NCP_IOC_GETROOT:
+        case NCP_IOC_SETROOT:
+		return 0;
+	default:
+		/* unkown IOCTL command, assume write */
+		WARN_ON();
+	}
+	return 1;
+}
+
+int ncp_ioctl(struct inode *inode, struct file *filp,
+	      unsigned int cmd, unsigned long arg)
+{
+	int ret;
+
+	if (ncp_ioctl_need_write(cmd)) {
+		/*
+		 * inside the ioctl(), any failures which
+		 * are because of file_permission() are
+		 * -EACCESS, so it seems consistent to keep
+		 *  that here.
+		 */
+		if (mnt_want_write(filp->f_vfsmnt))
+			return -EACCESS;
+	}
+	ret = __ncp_ioctl(inode, filp, cmd, arg);
+	if (ncp_ioctl_need_write(cmd)
+		mnt_drop_write(filp->->f_vfsmnt;
+	return ret;
+}

diff -puN fs/nfsd/vfs.c~elevate-writers-file_permission-callers fs/nfsd/vfs.c _

#11 lore raw

Re: [RFC][PATCH 06/27] elevate write count during entire ncp_ioctl()

Quoted 45 lines · Dave Hansen

Some ioctls need write access, but others don't. Make a helper function to decode when write access is needed, and take it.

---

 fs/nfsd/vfs.c             |    0 
 lxc-dave/fs/ncpfs/ioctl.c |   54 +++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 53 insertions(+), 1 deletion(-)

diff -puN fs/namei.c~elevate-writers-file_permission-callers fs/namei.c diff -puN fs/open.c~elevate-writers-file_permission-callers fs/open.c diff -puN fs/exec.c~elevate-writers-file_permission-callers fs/exec.c diff -puN fs/ncpfs/ioctl.c~elevate-writers-file_permission-callers fs/ncpfs/ioctl.c

fs/ncpfs/ioctl.c renamed from fs/ncpfs/ioctl.c~elevate-writers-file_permission-callers +16 -1
@@ -183,7 +183,7 @@ ncp_get_charsets(struct ncp_server* serv
 }
 #endif /* CONFIG_NCPFS_NLS */
 
-int ncp_ioctl(struct inode *inode, struct file *filp,
+static int __ncp_ioctl(struct inode *inode, struct file *filp,
 	      unsigned int cmd, unsigned long arg)
 {
 	struct ncp_server *server = NCP_SERVER(inode);
@@ -654,3 +654,55 @@ outrel:			
 /* #endif */
 	return -EINVAL;
 }
+
+static int ncp_ioctl_need_write(unsigned int cmd)
+{
+	switch (cmd) {
+        case NCP_IOC_GET_FS_INFO:
+        case NCP_IOC_GET_FS_INFO_V2:
+        case NCP_IOC_NCPREQUEST:
+        case NCP_IOC_SETDENTRYTTL:
+        case NCP_IOC_SIGN_INIT:
+        case NCP_IOC_LOCKUNLOCK:
+        case NCP_IOC_SET_SIGN_WANTED:
+		return 0;
+        case NCP_IOC_GETOBJECTNAME:
+        case NCP_IOC_SETOBJECTNAME:
+        case NCP_IOC_GETPRIVATEDATA:

fs/ncpfs/ioctl.c outrel: · line 681

Quoted 10 lines
fs/ncpfs/ioctl.c renamed from fs/ncpfs/ioctl.c~elevate-writers-file_permission-callers +10 -0
+        case NCP_IOC_SETPRIVATEDATA:
+        case NCP_IOC_SETCHARSETS:
+        case NCP_IOC_GETCHARSETS:
+        case NCP_IOC_CONN_LOGGED_IN:
+        case NCP_IOC_GETDENTRYTTL:
+        case NCP_IOC_GETMOUNTUID2:
+        case NCP_IOC_SIGN_WANTED:
+        case NCP_IOC_GETROOT:
+        case NCP_IOC_SETROOT:
+		return 0;

I'd assume one of those should be a return 1 :)

best,
Herbert

Quoted 29 lines
fs/ncpfs/ioctl.c renamed from fs/ncpfs/ioctl.c~elevate-writers-file_permission-callers +27 -0
+	default:
+		/* unkown IOCTL command, assume write */
+		WARN_ON();
+	}
+	return 1;
+}
+
+int ncp_ioctl(struct inode *inode, struct file *filp,
+	      unsigned int cmd, unsigned long arg)
+{
+	int ret;
+
+	if (ncp_ioctl_need_write(cmd)) {
+		/*
+		 * inside the ioctl(), any failures which
+		 * are because of file_permission() are
+		 * -EACCESS, so it seems consistent to keep
+		 *  that here.
+		 */
+		if (mnt_want_write(filp->f_vfsmnt))
+			return -EACCESS;
+	}
+	ret = __ncp_ioctl(inode, filp, cmd, arg);
+	if (ncp_ioctl_need_write(cmd)
+		mnt_drop_write(filp->->f_vfsmnt;
+	return ret;
+}

diff -puN fs/nfsd/vfs.c~elevate-writers-file_permission-callers fs/nfsd/vfs.c _

#12 lore raw

Re: [RFC][PATCH 06/27] elevate write count during entire ncp_ioctl()

Quoted 2 lines · Herbert Poetzl

I'd assume one of those should be a return 1 :)

Uh huh. Thanks for catching that. I'll fix it up.

-- Dave

#13 lore raw

RFC PATCH 7/27 sys_mkdirat(): collapse if()

In reply to Dave Hansen

Take the entire "if (IS_ERR(tmp))" block, and use a goto, instead.

This should not make any functional changes.

---

 lxc-dave/fs/namei.c |   38 +++++++++++++++++++-------------------
 1 files changed, 19 insertions(+), 19 deletions(-)
fs/namei.c renamed from fs/namei.c~sys_mkdir_at-move-cases +19 -19
diff -puN fs/namei.c~sys_mkdir_at-move-cases fs/namei.c
@@ -1888,30 +1888,30 @@ asmlinkage long sys_mkdirat(int dfd, con
 {
 	int error = 0;
 	char * tmp;
+	struct dentry *dentry;
+	struct nameidata nd;
 
 	tmp = getname(pathname);
 	error = PTR_ERR(tmp);
-	if (!IS_ERR(tmp)) {
-		struct dentry *dentry;
-		struct nameidata nd;
+	if (IS_ERR(tmp))
+		goto out_err;
 
-		error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
-		if (error)
-			goto out;
-		dentry = lookup_create(&nd, 1);
-		error = PTR_ERR(dentry);
-		if (!IS_ERR(dentry)) {
-			if (!IS_POSIXACL(nd.dentry->d_inode))
-				mode &= ~current->fs->umask;
-			error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
-			dput(dentry);
-		}
-		mutex_unlock(&nd.dentry->d_inode->i_mutex);
-		path_release(&nd);
-out:
-		putname(tmp);
+	error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
+	if (error)
+		goto out;
+	dentry = lookup_create(&nd, 1);
+	error = PTR_ERR(dentry);
+	if (!IS_ERR(dentry)) {
+		if (!IS_POSIXACL(nd.dentry->d_inode))
+			mode &= ~current->fs->umask;
+		error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
+		dput(dentry);
 	}
-
+	mutex_unlock(&nd.dentry->d_inode->i_mutex);
+	path_release(&nd);
+out:
+	putname(tmp);
+out_err:
 	return error;
 }
 

_

#14 lore raw

Re: [RFC][PATCH 07/27] sys_mkdirat(): collapse if()

Quoted 5 lines · Dave Hansen

Take the entire "if (IS_ERR(tmp))" block, and use a goto, instead.

This should not make any functional changes.

AFAICR, my test tool was done before the *at funtions were added to the kernel. did you extend it or test them in addition to the normal tests?

best,
Herbert

Quoted 60 lines

---

 lxc-dave/fs/namei.c |   38 +++++++++++++++++++-------------------
 1 files changed, 19 insertions(+), 19 deletions(-)

diff -puN fs/namei.c~sys_mkdir_at-move-cases fs/namei.c

fs/namei.c renamed from fs/namei.c~sys_mkdir_at-move-cases +19 -19
@@ -1888,30 +1888,30 @@ asmlinkage long sys_mkdirat(int dfd, con
 {
 	int error = 0;
 	char * tmp;
+	struct dentry *dentry;
+	struct nameidata nd;
 
 	tmp = getname(pathname);
 	error = PTR_ERR(tmp);
-	if (!IS_ERR(tmp)) {
-		struct dentry *dentry;
-		struct nameidata nd;
+	if (IS_ERR(tmp))
+		goto out_err;
 
-		error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
-		if (error)
-			goto out;
-		dentry = lookup_create(&nd, 1);
-		error = PTR_ERR(dentry);
-		if (!IS_ERR(dentry)) {
-			if (!IS_POSIXACL(nd.dentry->d_inode))
-				mode &= ~current->fs->umask;
-			error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
-			dput(dentry);
-		}
-		mutex_unlock(&nd.dentry->d_inode->i_mutex);
-		path_release(&nd);
-out:
-		putname(tmp);
+	error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
+	if (error)
+		goto out;
+	dentry = lookup_create(&nd, 1);
+	error = PTR_ERR(dentry);
+	if (!IS_ERR(dentry)) {
+		if (!IS_POSIXACL(nd.dentry->d_inode))
+			mode &= ~current->fs->umask;
+		error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
+		dput(dentry);
 	}
-
+	mutex_unlock(&nd.dentry->d_inode->i_mutex);
+	path_release(&nd);
+out:
+	putname(tmp);
+out_err:
 	return error;
 }
 

_

#15 lore raw

Re: [RFC][PATCH 07/27] sys_mkdirat(): collapse if()

Quoted 10 lines · Herbert Poetzl
Quoted 5 lines · Dave Hansen

Take the entire "if (IS_ERR(tmp))" block, and use a goto, instead.

This should not make any functional changes.

AFAICR, my test tool was done before the *at funtions were added to the kernel. did you extend it or test them in addition to the normal tests?

No, I didn't extend it. However, I believe all of the internal kernel implementations which are plain, like sys_mknod() call into the *at() variants. I could look at extending that test, but I think all of the code paths are still being exercised.

I actually audited from the bottom up, looking for callers of the permission functions, and anywhere that checked an inode for a read-only superblock.

-- Dave

#16 lore raw

Re: [RFC][PATCH 07/27] sys_mkdirat(): collapse if()

Quoted 12 lines · Dave Hansen
Quoted 10 lines · Herbert Poetzl
Quoted 5 lines · Dave Hansen

Take the entire "if (IS_ERR(tmp))" block, and use a goto, instead.

This should not make any functional changes.

AFAICR, my test tool was done before the *at funtions were added to the kernel. did you extend it or test them in addition to the normal tests?

Quoted 8 lines

No, I didn't extend it. However, I believe all of the internal kernel implementations which are plain, like sys_mknod() call into the *at() variants. I could look at extending that test, but I think all of the code paths are still being exercised.

I actually audited from the bottom up, looking for callers of the permission functions, and anywhere that checked an inode for a read-only superblock.

well, this is a good start, but not nearly sufficient to ensure that the semantics are not changed, see my other replies to that issue ...

best,
Herbert

Quoted 1 line

-- Dave

#17 lore raw

RFC PATCH 8/27 sys_mkdirat(): one more goto

In reply to Dave Hansen

Add one more goto to collapse another if().

This should not cause any functional chagnes.

---

 lxc-dave/fs/namei.c |   14 ++++++++------
 1 files changed, 8 insertions(+), 6 deletions(-)
fs/namei.c renamed from fs/namei.c~sys_mkdir_at-move-cases-another-goto +8 -6
diff -puN fs/namei.c~sys_mkdir_at-move-cases-another-goto fs/namei.c
@@ -1901,12 +1901,14 @@ asmlinkage long sys_mkdirat(int dfd, con
 		goto out;
 	dentry = lookup_create(&nd, 1);
 	error = PTR_ERR(dentry);
-	if (!IS_ERR(dentry)) {
-		if (!IS_POSIXACL(nd.dentry->d_inode))
-			mode &= ~current->fs->umask;
-		error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
-		dput(dentry);
-	}
+	if (IS_ERR(dentry))
+		goto out_unlock;
+
+	if (!IS_POSIXACL(nd.dentry->d_inode))
+		mode &= ~current->fs->umask;
+	error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
+	dput(dentry);
+out_unlock:
 	mutex_unlock(&nd.dentry->d_inode->i_mutex);
 	path_release(&nd);
 out:

_

Acked-by on v1 8/27
#18 lore raw

Re: [RFC][PATCH 08/27] sys_mkdirat(): one more goto

Quoted 4 lines · Dave Hansen

Add one more goto to collapse another if().

This should not cause any functional chagnes.

Quoted 31 lines

---

 lxc-dave/fs/namei.c |   14 ++++++++------
 1 files changed, 8 insertions(+), 6 deletions(-)

diff -puN fs/namei.c~sys_mkdir_at-move-cases-another-goto fs/namei.c

fs/namei.c renamed from fs/namei.c~sys_mkdir_at-move-cases-another-goto +8 -6
@@ -1901,12 +1901,14 @@ asmlinkage long sys_mkdirat(int dfd, con
 		goto out;
 	dentry = lookup_create(&nd, 1);
 	error = PTR_ERR(dentry);
-	if (!IS_ERR(dentry)) {
-		if (!IS_POSIXACL(nd.dentry->d_inode))
-			mode &= ~current->fs->umask;
-		error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
-		dput(dentry);
-	}
+	if (IS_ERR(dentry))
+		goto out_unlock;
+
+	if (!IS_POSIXACL(nd.dentry->d_inode))
+		mode &= ~current->fs->umask;
+	error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
+	dput(dentry);
+out_unlock:
 	mutex_unlock(&nd.dentry->d_inode->i_mutex);
 	path_release(&nd);
 out:

_

#19 lore raw

RFC PATCH 9/27 elevate mnt writers for sys_mkdirat() call of vfs_mkdir()

In reply to Dave Hansen

---

 fs/nfsd/nfs4recover.c |    0 
 lxc-dave/fs/namei.c   |    5 +++++
 2 files changed, 5 insertions(+)
fs/namei.c renamed from fs/namei.c~sys_mkdir_at-do-mnt_want_write +5 -0
diff -puN fs/namei.c~sys_mkdir_at-do-mnt_want_write fs/namei.c
@@ -1906,7 +1906,12 @@ asmlinkage long sys_mkdirat(int dfd, con
 
 	if (!IS_POSIXACL(nd.dentry->d_inode))
 		mode &= ~current->fs->umask;
+	error = mnt_want_write(nd.mnt);
+	if (error)
+		goto out_dput;
 	error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
+	mnt_drop_write(nd.mnt);
+out_dput:
 	dput(dentry);
 out_unlock:
 	mutex_unlock(&nd.dentry->d_inode->i_mutex);

diff -puN fs/nfsd/nfs4recover.c~sys_mkdir_at-do-mnt_want_write fs/nfsd/nfs4recover.c _

#20 lore raw

RFC PATCH 10/27 sys_symlinkat() collapse if()s

In reply to Dave Hansen

---

 lxc-dave/fs/namei.c |   33 +++++++++++++++++----------------
 1 files changed, 17 insertions(+), 16 deletions(-)
fs/namei.c renamed from fs/namei.c~elevate-writers-vfs_symlink +17 -16
diff -puN fs/namei.c~elevate-writers-vfs_symlink fs/namei.c
@@ -2170,30 +2170,31 @@ asmlinkage long sys_symlinkat(const char
 	int error = 0;
 	char * from;
 	char * to;
+	struct dentry *dentry;
+	struct nameidata nd;
 
 	from = getname(oldname);
 	if(IS_ERR(from))
 		return PTR_ERR(from);
 	to = getname(newname);
 	error = PTR_ERR(to);
-	if (!IS_ERR(to)) {
-		struct dentry *dentry;
-		struct nameidata nd;
+	if (IS_ERR(to))
+		goto out_putname;
 
-		error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
-		if (error)
-			goto out;
-		dentry = lookup_create(&nd, 0);
-		error = PTR_ERR(dentry);
-		if (!IS_ERR(dentry)) {
-			error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
-			dput(dentry);
-		}
-		mutex_unlock(&nd.dentry->d_inode->i_mutex);
-		path_release(&nd);
-out:
-		putname(to);
+	error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
+	if (error)
+		goto out;
+	dentry = lookup_create(&nd, 0);
+	error = PTR_ERR(dentry);
+	if (!IS_ERR(dentry)) {
+		error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
+		dput(dentry);
 	}
+	mutex_unlock(&nd.dentry->d_inode->i_mutex);
+	path_release(&nd);
+out:
+	putname(to);
+out_putname:
 	putname(from);
 	return error;
 }

_

#21 lore raw

RFC PATCH 11/27 sys_symlinkat() collapse one more if ()

In reply to Dave Hansen

Collapse another if() to make way for a new condition.

This patch should make no functional changes.

---

 lxc-dave/fs/namei.c |   10 ++++++----
 1 files changed, 6 insertions(+), 4 deletions(-)
fs/namei.c renamed from fs/namei.c~elevate-writers-vfs_symlink-part2 +6 -4
diff -puN fs/namei.c~elevate-writers-vfs_symlink-part2 fs/namei.c
@@ -2186,10 +2186,12 @@ asmlinkage long sys_symlinkat(const char
 		goto out;
 	dentry = lookup_create(&nd, 0);
 	error = PTR_ERR(dentry);
-	if (!IS_ERR(dentry)) {
-		error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
-		dput(dentry);
-	}
+	if (IS_ERR(dentry))
+		goto out_unlock;
+
+	error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
+	dput(dentry);
+out_unlock:
 	mutex_unlock(&nd.dentry->d_inode->i_mutex);
 	path_release(&nd);
 out:

_

Acked-by on v1 11/27
#22 lore raw

Re: [RFC][PATCH 11/27] sys_symlinkat() collapse one more if ()

Quoted 4 lines · Dave Hansen

Collapse another if() to make way for a new condition.

This patch should make no functional changes.

Quoted 27 lines

---

 lxc-dave/fs/namei.c |   10 ++++++----
 1 files changed, 6 insertions(+), 4 deletions(-)

diff -puN fs/namei.c~elevate-writers-vfs_symlink-part2 fs/namei.c

fs/namei.c renamed from fs/namei.c~elevate-writers-vfs_symlink-part2 +6 -4
@@ -2186,10 +2186,12 @@ asmlinkage long sys_symlinkat(const char
 		goto out;
 	dentry = lookup_create(&nd, 0);
 	error = PTR_ERR(dentry);
-	if (!IS_ERR(dentry)) {
-		error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
-		dput(dentry);
-	}
+	if (IS_ERR(dentry))
+		goto out_unlock;
+
+	error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
+	dput(dentry);
+out_unlock:
 	mutex_unlock(&nd.dentry->d_inode->i_mutex);
 	path_release(&nd);
 out:

_

#23 lore raw

RFC PATCH 12/27 sys_symlinkat() elevate write count around vfs_symlink()

In reply to Dave Hansen

---

 lxc-dave/fs/namei.c |    5 +++++
 1 files changed, 5 insertions(+)
fs/namei.c renamed from fs/namei.c~elevate-writers-vfs_symlink-part3 +5 -0
diff -puN fs/namei.c~elevate-writers-vfs_symlink-part3 fs/namei.c
@@ -2189,7 +2189,12 @@ asmlinkage long sys_symlinkat(const char
 	if (IS_ERR(dentry))
 		goto out_unlock;
 
+	error = mnt_want_write(nd.mnt);
+	if (error)
+		goto out_dput;
 	error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
+	mnt_drop_write(nd.mnt);
+out_dput:
 	dput(dentry);
 out_unlock:
 	mutex_unlock(&nd.dentry->d_inode->i_mutex);

_

#24 lore raw

RFC PATCH 13/27 sys_linkat(): elevate write count around vfs_link()

In reply to Dave Hansen

---

 lxc-dave/fs/namei.c |   14 ++++++++++----
 1 files changed, 10 insertions(+), 4 deletions(-)
fs/namei.c renamed from fs/namei.c~elevate-writers-vfs_link-part1 +10 -4
diff -puN fs/namei.c~elevate-writers-vfs_link-part1 fs/namei.c
@@ -2285,10 +2285,16 @@ asmlinkage long sys_linkat(int olddfd, c
 		goto out_release;
 	new_dentry = lookup_create(&nd, 0);
 	error = PTR_ERR(new_dentry);
-	if (!IS_ERR(new_dentry)) {
-		error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
-		dput(new_dentry);
-	}
+	if (IS_ERR(new_dentry))
+		goto out_unlock;
+	error = mnt_want_write(nd.mnt);
+	if (error)
+		goto out_dput;
+	error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
+	mnt_drop_write(nd.mnt);
+out_dput:
+	dput(new_dentry);
+out_unlock:
 	mutex_unlock(&nd.dentry->d_inode->i_mutex);
 out_release:
 	path_release(&nd);

_

#25 lore raw

RFC PATCH 14/27 tricky: elevate write count files are open()ed

In reply to Dave Hansen

This is the first really tricky patch in the series. It elevates the writer count on a mount each time a non-special file is opened for write.

This is not completely apparent in the patch because the two if() conditions in may_open() above the mnt_want_write() call are, combined, equivalent to special_file().

There is also an elevated count around the vfs_create() call in open_namei(). The count does not need to be kept elevated all the way into the may_open() call because after creation, the write bits of the acc_mode are cleared. This keeps may_open() from ever failing. Howver, this may open one potential race where a change from a r/w to a r/o mount could occur between the mnt_drop_write() and may_open() allowing a user to obtain a r/w file on what is now a r/w mount. But, this functionality does not yet exist.

---

 lxc-dave/fs/file_table.c |    5 ++++-
 lxc-dave/fs/namei.c      |   29 ++++++++++++++++++++++++++---
 lxc-dave/ipc/mqueue.c    |    3 +++
 3 files changed, 33 insertions(+), 4 deletions(-)
fs/namei.c renamed from fs/namei.c~elevate-writers-opens-part1 +26 -3
diff -puN fs/namei.c~elevate-writers-opens-part1 fs/namei.c
@@ -1511,8 +1511,17 @@ int may_open(struct nameidata *nd, int a
 			return -EACCES;
 
 		flag &= ~O_TRUNC;
-	} else if (IS_RDONLY(inode) && (flag & FMODE_WRITE))
-		return -EROFS;
+	} else if (flag & FMODE_WRITE) {
+		/*
+		 * effectively: !special_file()
+		 * balanced by __fput()
+		 */
+		error = mnt_want_write(nd->mnt);
+		if (error)
+			return error;
+		if (IS_RDONLY(inode))
+			return -EROFS;
+	}
 	/*
 	 * An append-only file must be opened in append mode for writing.
 	 */
@@ -1642,10 +1651,24 @@ do_last:
 	if (!path.dentry->d_inode) {
 		if (!IS_POSIXACL(dir->d_inode))
 			mode &= ~current->fs->umask;
-		error = vfs_create(dir->d_inode, path.dentry, mode, nd);
+		/*
+		 * this serves dual roles, it makes sure there is no
+		 * r/o mount, and keeps the write count for what is
+		 * the newly created file
+		 */
+		error = mnt_want_write(nd->mnt);
+		if (!error)
+			error = vfs_create(dir->d_inode, path.dentry, mode, nd);
 		mutex_unlock(&dir->d_inode->i_mutex);
 		dput(nd->dentry);
 		nd->dentry = path.dentry;
+		/*
+		 * Unconditionally drop the write access because
+		 * the acc_mode=0 set below will keep may_open()
+		 * from ever failing if there was a r/o mount
+		 * between here and there
+		 */
+		mnt_drop_write(nd->mnt);
 		if (error)
 			goto exit;
 		/* Don't check for write permission, don't truncate */

diff -puN fs/open.c~elevate-writers-opens-part1 fs/open.c
diff -puN include/linux/mount.h~elevate-writers-opens-part1 include/linux/mount.h

fs/file_table.c renamed from fs/file_table.c~elevate-writers-opens-part1 +4 -1
diff -puN fs/file_table.c~elevate-writers-opens-part1 fs/file_table.c
@@ -180,8 +180,11 @@ void fastcall __fput(struct file *file)
 	if (unlikely(inode->i_cdev != NULL))
 		cdev_put(inode->i_cdev);
 	fops_put(file->f_op);
-	if (file->f_mode & FMODE_WRITE)
+	if (file->f_mode & FMODE_WRITE) {
 		put_write_access(inode);
+		if(!special_file(inode->i_mode))
+			mnt_drop_write(mnt);
+	}
 	file_kill(file);
 	file->f_dentry = NULL;
 	file->f_vfsmnt = NULL;
ipc/mqueue.c renamed from ipc/mqueue.c~elevate-writers-opens-part1 +3 -0
diff -puN ipc/mqueue.c~elevate-writers-opens-part1 ipc/mqueue.c
@@ -679,6 +679,9 @@ asmlinkage long sys_mq_open(const char _
 				goto out;
 			filp = do_open(dentry, oflag);
 		} else {
+			error = mnt_want_write(mqueue_mnt);
+			if (error)
+				goto out;
 			filp = do_create(mqueue_mnt->mnt_root, dentry,
 						oflag, mode, u_attr);
 		}

_

#26 lore raw

Re: [RFC][PATCH 14/27] tricky: elevate write count files are open()ed

Quoted 10 lines · Dave Hansen

This is the first really tricky patch in the series. It elevates the writer count on a mount each time a non-special file is opened for write.

This is not completely apparent in the patch because the two if() conditions in may_open() above the mnt_want_write() call are, combined, equivalent to special_file().

Quoted 8 lines

There is also an elevated count around the vfs_create() call in open_namei(). The count does not need to be kept elevated all the way into the may_open() call because after creation, the write bits of the acc_mode are cleared. This keeps may_open() from ever failing. Howver, this may open one potential race where a change from a r/w to a r/o mount could occur between the mnt_drop_write() and may_open() allowing a user to obtain a r/w file on what is now a r/w

probably means 'read only mount'

what about using atomic_dec_and_test() to avoid
having such cases (or a lock if required)?

best,
Herbert

Quoted 92 lines

mount. But, this functionality does not yet exist.

---

 lxc-dave/fs/file_table.c |    5 ++++-
 lxc-dave/fs/namei.c      |   29 ++++++++++++++++++++++++++---
 lxc-dave/ipc/mqueue.c    |    3 +++
 3 files changed, 33 insertions(+), 4 deletions(-)

diff -puN fs/namei.c~elevate-writers-opens-part1 fs/namei.c

fs/namei.c renamed from fs/namei.c~elevate-writers-opens-part1 +26 -3
@@ -1511,8 +1511,17 @@ int may_open(struct nameidata *nd, int a
 			return -EACCES;
 
 		flag &= ~O_TRUNC;
-	} else if (IS_RDONLY(inode) && (flag & FMODE_WRITE))
-		return -EROFS;
+	} else if (flag & FMODE_WRITE) {
+		/*
+		 * effectively: !special_file()
+		 * balanced by __fput()
+		 */
+		error = mnt_want_write(nd->mnt);
+		if (error)
+			return error;
+		if (IS_RDONLY(inode))
+			return -EROFS;
+	}
 	/*
 	 * An append-only file must be opened in append mode for writing.
 	 */
@@ -1642,10 +1651,24 @@ do_last:
 	if (!path.dentry->d_inode) {
 		if (!IS_POSIXACL(dir->d_inode))
 			mode &= ~current->fs->umask;
-		error = vfs_create(dir->d_inode, path.dentry, mode, nd);
+		/*
+		 * this serves dual roles, it makes sure there is no
+		 * r/o mount, and keeps the write count for what is
+		 * the newly created file
+		 */
+		error = mnt_want_write(nd->mnt);
+		if (!error)
+			error = vfs_create(dir->d_inode, path.dentry, mode, nd);
 		mutex_unlock(&dir->d_inode->i_mutex);
 		dput(nd->dentry);
 		nd->dentry = path.dentry;
+		/*
+		 * Unconditionally drop the write access because
+		 * the acc_mode=0 set below will keep may_open()
+		 * from ever failing if there was a r/o mount
+		 * between here and there
+		 */
+		mnt_drop_write(nd->mnt);
 		if (error)
 			goto exit;
 		/* Don't check for write permission, don't truncate */

diff -puN fs/open.c~elevate-writers-opens-part1 fs/open.c
diff -puN include/linux/mount.h~elevate-writers-opens-part1 include/linux/mount.h diff -puN fs/file_table.c~elevate-writers-opens-part1 fs/file_table.c

fs/file_table.c renamed from fs/file_table.c~elevate-writers-opens-part1 +4 -1
@@ -180,8 +180,11 @@ void fastcall __fput(struct file *file)
 	if (unlikely(inode->i_cdev != NULL))
 		cdev_put(inode->i_cdev);
 	fops_put(file->f_op);
-	if (file->f_mode & FMODE_WRITE)
+	if (file->f_mode & FMODE_WRITE) {
 		put_write_access(inode);
+		if(!special_file(inode->i_mode))
+			mnt_drop_write(mnt);
+	}
 	file_kill(file);
 	file->f_dentry = NULL;
 	file->f_vfsmnt = NULL;

diff -puN ipc/mqueue.c~elevate-writers-opens-part1 ipc/mqueue.c

ipc/mqueue.c renamed from ipc/mqueue.c~elevate-writers-opens-part1 +3 -0
@@ -679,6 +679,9 @@ asmlinkage long sys_mq_open(const char _
 				goto out;
 			filp = do_open(dentry, oflag);
 		} else {
+			error = mnt_want_write(mqueue_mnt);
+			if (error)
+				goto out;
 			filp = do_create(mqueue_mnt->mnt_root, dentry,
 						oflag, mode, u_attr);
 		}

_

#27 lore raw

Re: [RFC][PATCH 14/27] tricky: elevate write count files are open()ed

Quoted 9 lines · Herbert Poetzl
Quoted 8 lines

There is also an elevated count around the vfs_create() call in open_namei(). The count does not need to be kept elevated all the way into the may_open() call because after creation, the write bits of the acc_mode are cleared. This keeps may_open() from ever failing. Howver, this may open one potential race where a change from a r/w to a r/o mount could occur between the mnt_drop_write() and may_open() allowing a user to obtain a r/w file on what is now a r/w

Quoted 4 lines

probably means 'read only mount'

what about using atomic_dec_and_test() to avoid
having such cases (or a lock if required)?

I thought about that, but it isn't that simple. Such a case needs to have controlled transitions away from when the counter is at '0'. It is a similar problem as i_writecount and put/deny_write_access(), which end up having to use a spinlock. I don't think we can do it with simple atomics.

-- Dave

#28 lore raw

Re: [RFC][PATCH 14/27] tricky: elevate write count files are open()ed

Quoted 12 lines · Dave Hansen
Quoted 11 lines · Herbert Poetzl
Quoted 8 lines

There is also an elevated count around the vfs_create() call in open_namei(). The count does not need to be kept elevated all the way into the may_open() call because after creation, the write bits of the acc_mode are cleared. This keeps may_open() from ever failing. Howver, this may open one potential race where a change from a r/w to a r/o mount could occur between the mnt_drop_write() and may_open() allowing a user to obtain a r/w file on what is now a r/w

probably means 'read only mount'

Quoted 8 lines
Quoted 2 lines

what about using atomic_dec_and_test() to avoid
having such cases (or a lock if required)?

I thought about that, but it isn't that simple. Such a case needs to have controlled transitions away from when the counter is at '0'. It is a similar problem as i_writecount and put/deny_write_access(), which end up having to use a spinlock. I don't think we can do it with simple atomics.

hmm, care to elaborate? IIRC, certain parts like
the entire md subsystem,as well as parts of the
procfs use it quite fine ...

TIA,
Herbert

Quoted 1 line

-- Dave

#29 lore raw

RFC PATCH 15/27 elevate writer count for do_sys_truncate()

In reply to Dave Hansen

---

 lxc-dave/fs/open.c |   16 +++++++++++-----
 1 files changed, 11 insertions(+), 5 deletions(-)
fs/open.c renamed from fs/open.c~elevate-writers-opens-part2-do_sys_truncate +11 -5
diff -puN fs/open.c~elevate-writers-opens-part2-do_sys_truncate fs/open.c
@@ -244,28 +244,32 @@ static long do_sys_truncate(const char _
 	if (!S_ISREG(inode->i_mode))
 		goto dput_and_out;
 
-	error = vfs_permission(&nd, MAY_WRITE);
+	error = mnt_want_write(nd.mnt);
 	if (error)
 		goto dput_and_out;
 
+	error = vfs_permission(&nd, MAY_WRITE);
+	if (error)
+		goto mnt_drop_write_and_out;
+
 	error = -EROFS;
 	if (IS_RDONLY(inode))
-		goto dput_and_out;
+		goto mnt_drop_write_and_out;
 
 	error = -EPERM;
 	if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
-		goto dput_and_out;
+		goto mnt_drop_write_and_out;
 
 	/*
 	 * Make sure that there are no leases.
 	 */
 	error = break_lease(inode, FMODE_WRITE);
 	if (error)
-		goto dput_and_out;
+		goto mnt_drop_write_and_out;
 
 	error = get_write_access(inode);
 	if (error)
-		goto dput_and_out;
+		goto mnt_drop_write_and_out;
 
 	error = locks_verify_truncate(inode, NULL, length);
 	if (!error) {
@@ -274,6 +278,8 @@ static long do_sys_truncate(const char _
 	}
 	put_write_access(inode);
 
+mnt_drop_write_and_out:
+	mnt_drop_write(nd.mnt);
 dput_and_out:
 	path_release(&nd);
 out:

_

#30 lore raw

RFC PATCH 16/27 elevate write count for do_sys_utime()

In reply to Dave Hansen

---

 lxc-dave/fs/open.c |   16 +++++++++++-----
 1 files changed, 11 insertions(+), 5 deletions(-)
fs/open.c renamed from fs/open.c~elevate-writers-opens-part2-do_sys_utime +11 -5
diff -puN fs/open.c~elevate-writers-opens-part2-do_sys_utime fs/open.c
@@ -384,16 +384,20 @@ asmlinkage long sys_utime(char __user * 
 		goto out;
 	inode = nd.dentry->d_inode;
 
+	error = mnt_want_write(nd.mnt);
+	if (error)
+		goto dput_and_out;
+
 	error = -EROFS;
 	if (IS_RDONLY(inode))
-		goto dput_and_out;
+		goto mnt_drop_write_and_out;
 
 	/* Don't worry, the checks are done in inode_change_ok() */
 	newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
 	if (times) {
 		error = -EPERM;
 		if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
-			goto dput_and_out;
+			goto mnt_drop_write_and_out;
 
 		error = get_user(newattrs.ia_atime.tv_sec, &times->actime);
 		newattrs.ia_atime.tv_nsec = 0;
@@ -401,21 +405,23 @@ asmlinkage long sys_utime(char __user * 
 			error = get_user(newattrs.ia_mtime.tv_sec, &times->modtime);
 		newattrs.ia_mtime.tv_nsec = 0;
 		if (error)
-			goto dput_and_out;
+			goto mnt_drop_write_and_out;
 
 		newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
 	} else {
                 error = -EACCES;
                 if (IS_IMMUTABLE(inode))
-                        goto dput_and_out;
+			goto mnt_drop_write_and_out;
 
 		if (current->fsuid != inode->i_uid &&
 		    (error = vfs_permission(&nd, MAY_WRITE)) != 0)
-			goto dput_and_out;
+			goto mnt_drop_write_and_out;
 	}
 	mutex_lock(&inode->i_mutex);
 	error = notify_change(nd.dentry, &newattrs);
 	mutex_unlock(&inode->i_mutex);
+mnt_drop_write_and_out:
+	mnt_drop_write(nd.mnt);
 dput_and_out:
 	path_release(&nd);
 out:

_

#31 lore raw

RFC PATCH 17/27 elevate write count for do_utimes()

In reply to Dave Hansen

---

 lxc-dave/fs/open.c |   13 +++++++++----
 1 files changed, 9 insertions(+), 4 deletions(-)
fs/open.c renamed from fs/open.c~elevate-writers-opens-part2-do_utimes +9 -4
diff -puN fs/open.c~elevate-writers-opens-part2-do_utimes fs/open.c
@@ -447,16 +447,19 @@ long do_utimes(int dfd, char __user *fil
 		goto out;
 	inode = nd.dentry->d_inode;
 
+	error = mnt_want_write(nd.mnt);
+	if (error)
+		goto dput_and_out;
 	error = -EROFS;
 	if (IS_RDONLY(inode))
-		goto dput_and_out;
+		goto mnt_drop_write_and_out;
 
 	/* Don't worry, the checks are done in inode_change_ok() */
 	newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
 	if (times) {
 		error = -EPERM;
                 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
-                        goto dput_and_out;
+			goto mnt_drop_write_and_out;
 
 		newattrs.ia_atime.tv_sec = times[0].tv_sec;
 		newattrs.ia_atime.tv_nsec = times[0].tv_usec * 1000;
@@ -466,15 +469,17 @@ long do_utimes(int dfd, char __user *fil
 	} else {
 		error = -EACCES;
                 if (IS_IMMUTABLE(inode))
-                        goto dput_and_out;
+			goto mnt_drop_write_and_out;
 
 		if (current->fsuid != inode->i_uid &&
 		    (error = vfs_permission(&nd, MAY_WRITE)) != 0)
-			goto dput_and_out;
+			goto mnt_drop_write_and_out;
 	}
 	mutex_lock(&inode->i_mutex);
 	error = notify_change(nd.dentry, &newattrs);
 	mutex_unlock(&inode->i_mutex);
+mnt_drop_write_and_out:
+	mnt_drop_write(nd.mnt);
 dput_and_out:
 	path_release(&nd);
 out:

_

#32 lore raw

RFC PATCH 18/27 sys_faccessat(): collapse if()

In reply to Dave Hansen

This patch should make no functional changes.

---

 lxc-dave/fs/open.c |   18 ++++++++++--------
 1 files changed, 10 insertions(+), 8 deletions(-)
fs/open.c renamed from fs/open.c~elevate-writers-opens-part2-sys_faccessat +10 -8
diff -puN fs/open.c~elevate-writers-opens-part2-sys_faccessat fs/open.c
@@ -537,15 +537,17 @@ asmlinkage long sys_faccessat(int dfd, c
 		current->cap_effective = current->cap_permitted;
 
 	res = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW|LOOKUP_ACCESS, &nd);
-	if (!res) {
-		res = vfs_permission(&nd, mode);
-		/* SuS v2 requires we report a read only fs too */
-		if(!res && (mode & S_IWOTH) && IS_RDONLY(nd.dentry->d_inode)
-		   && !special_file(nd.dentry->d_inode->i_mode))
-			res = -EROFS;
-		path_release(&nd);
-	}
+	if (res)
+		goto out;
 
+	res = vfs_permission(&nd, mode);
+	/* SuS v2 requires we report a read only fs too */
+	if(!res && (mode & S_IWOTH) && IS_RDONLY(nd.dentry->d_inode)
+	   && !special_file(nd.dentry->d_inode->i_mode))
+		res = -EROFS;
+	path_release(&nd);
+
+out:
 	current->fsuid = old_fsuid;
 	current->fsgid = old_fsgid;
 	current->cap_effective = old_cap;

_

#33 lore raw

Re: [RFC][PATCH 18/27] sys_faccessat(): collapse if()

Quoted 2 lines · Dave Hansen

This patch should make no functional changes.

wrong order, but IMHO this one should do the
goto out_path_release change of the previous
one

best,
Herbert

Quoted 36 lines

---

 lxc-dave/fs/open.c |   18 ++++++++++--------
 1 files changed, 10 insertions(+), 8 deletions(-)

diff -puN fs/open.c~elevate-writers-opens-part2-sys_faccessat fs/open.c

fs/open.c renamed from fs/open.c~elevate-writers-opens-part2-sys_faccessat +10 -8
@@ -537,15 +537,17 @@ asmlinkage long sys_faccessat(int dfd, c
 		current->cap_effective = current->cap_permitted;
 
 	res = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW|LOOKUP_ACCESS, &nd);
-	if (!res) {
-		res = vfs_permission(&nd, mode);
-		/* SuS v2 requires we report a read only fs too */
-		if(!res && (mode & S_IWOTH) && IS_RDONLY(nd.dentry->d_inode)
-		   && !special_file(nd.dentry->d_inode->i_mode))
-			res = -EROFS;
-		path_release(&nd);
-	}
+	if (res)
+		goto out;
 
+	res = vfs_permission(&nd, mode);
+	/* SuS v2 requires we report a read only fs too */
+	if(!res && (mode & S_IWOTH) && IS_RDONLY(nd.dentry->d_inode)
+	   && !special_file(nd.dentry->d_inode->i_mode))
+		res = -EROFS;
+	path_release(&nd);
+
+out:
 	current->fsuid = old_fsuid;
 	current->fsgid = old_fsgid;
 	current->cap_effective = old_cap;

_

#34 lore raw

RFC PATCH 19/27 sys_faccessat() elevate writer count

In reply to Dave Hansen

---

 lxc-dave/fs/open.c |   16 ++++++++++++++--
 1 files changed, 14 insertions(+), 2 deletions(-)
fs/open.c renamed from fs/open.c~elevate-writers-opens-part2-sys_faccessat2 +14 -2
diff -puN fs/open.c~elevate-writers-opens-part2-sys_faccessat2 fs/open.c
@@ -542,9 +542,21 @@ asmlinkage long sys_faccessat(int dfd, c
 
 	res = vfs_permission(&nd, mode);
 	/* SuS v2 requires we report a read only fs too */
-	if(!res && (mode & S_IWOTH) && IS_RDONLY(nd.dentry->d_inode)
-	   && !special_file(nd.dentry->d_inode->i_mode))
+	if(res)
+		goto out_path_release;
+	if (!(mode & S_IWOTH))
+		goto out_path_release;
+	if (special_file(nd.dentry->d_inode->i_mode))
+		goto out_path_release;
+
+	res = mnt_want_write(nd.mnt);
+	if (res) {
+		mnt_drop_write(nd.mnt);
+		goto out_path_release;
+	}
+	if (IS_RDONLY(nd.dentry->d_inode))
 		res = -EROFS;
+out_path_release:
 	path_release(&nd);
 
 out:

_

#35 lore raw

Re: [RFC][PATCH 19/27] sys_faccessat() elevate writer count

On Wed, Jun 07, 2006 at 05:10:29PM -0700, Dave Hansen wrote:

this is the first non-trivial change to the condition checks (regarding goto out_path_release) why didn't you break that out too?

best,
Herbert

Quoted 34 lines

---

 lxc-dave/fs/open.c |   16 ++++++++++++++--
 1 files changed, 14 insertions(+), 2 deletions(-)

diff -puN fs/open.c~elevate-writers-opens-part2-sys_faccessat2 fs/open.c

fs/open.c renamed from fs/open.c~elevate-writers-opens-part2-sys_faccessat2 +14 -2
@@ -542,9 +542,21 @@ asmlinkage long sys_faccessat(int dfd, c
 
 	res = vfs_permission(&nd, mode);
 	/* SuS v2 requires we report a read only fs too */
-	if(!res && (mode & S_IWOTH) && IS_RDONLY(nd.dentry->d_inode)
-	   && !special_file(nd.dentry->d_inode->i_mode))
+	if(res)
+		goto out_path_release;
+	if (!(mode & S_IWOTH))
+		goto out_path_release;
+	if (special_file(nd.dentry->d_inode->i_mode))
+		goto out_path_release;
+
+	res = mnt_want_write(nd.mnt);
+	if (res) {
+		mnt_drop_write(nd.mnt);
+		goto out_path_release;
+	}
+	if (IS_RDONLY(nd.dentry->d_inode))
 		res = -EROFS;
+out_path_release:
 	path_release(&nd);
 
 out:

_

#36 lore raw

Re: [RFC][PATCH 19/27] sys_faccessat() elevate writer count

Quoted 3 lines · Herbert Poetzl

this is the first non-trivial change to the condition checks (regarding goto out_path_release) why didn't you break that out too?

Honestly, it was because I did this one before I even tried to compile and boot the thing. Once I hit a bug or two, I started separating out the control structure changes from the actual addition of the write count changes. I admit, it is a bit inconsistent. I can definitely break things out further in the next set, but I thought 27 patches was enough for now. ;)

-- Dave

#37 lore raw

RFC PATCH 20/27 unix_find_other() elevate write count for touch_atime()

In reply to Dave Hansen

---

 lxc-dave/net/unix/af_unix.c |   16 ++++++++++++----
 1 files changed, 12 insertions(+), 4 deletions(-)
net/unix/af_unix.c renamed from net/unix/af_unix.c~elevate-writers-opens-part4 +12 -4
diff -puN net/unix/af_unix.c~elevate-writers-opens-part4 net/unix/af_unix.c
@@ -676,21 +676,27 @@ static struct sock *unix_find_other(stru
 		err = path_lookup(sunname->sun_path, LOOKUP_FOLLOW, &nd);
 		if (err)
 			goto fail;
+
+		err = mnt_want_write(nd.mnt);
+		if (err)
+			goto put_path_fail;
+
 		err = vfs_permission(&nd, MAY_WRITE);
 		if (err)
-			goto put_fail;
+			goto put_write_fail;
 
 		err = -ECONNREFUSED;
 		if (!S_ISSOCK(nd.dentry->d_inode->i_mode))
-			goto put_fail;
+			goto put_write_fail;
 		u=unix_find_socket_byinode(nd.dentry->d_inode);
 		if (!u)
-			goto put_fail;
+			goto put_write_fail;
 
 		if (u->sk_type == type)
 			touch_atime(nd.mnt, nd.dentry);
 
 		path_release(&nd);
+		mnt_drop_write(nd.mnt);
 
 		err=-EPROTOTYPE;
 		if (u->sk_type != type) {
@@ -710,7 +716,9 @@ static struct sock *unix_find_other(stru
 	}
 	return u;
 
-put_fail:
+put_write_fail:
+	mnt_drop_write(nd.mnt);
+put_path_fail:
 	path_release(&nd);
 fail:
 	*error=err;

_

#38 lore raw

Re: [RFC][PATCH 20/27] unix_find_other() elevate write count for touch_atime()

Quoted 14 lines · Dave Hansen

---

 lxc-dave/net/unix/af_unix.c |   16 ++++++++++++----
 1 files changed, 12 insertions(+), 4 deletions(-)

diff -puN net/unix/af_unix.c~elevate-writers-opens-part4 net/unix/af_unix.c

net/unix/af_unix.c renamed from net/unix/af_unix.c~elevate-writers-opens-part4 +0 -0
@@ -676,21 +676,27 @@ static struct sock *unix_find_other(stru
 		err = path_lookup(sunname->sun_path, LOOKUP_FOLLOW, &nd);
 		if (err)

net/unix/af_unix.c static struct sock *unix_find_other(stru · line 686

Quoted 10 lines
net/unix/af_unix.c renamed from net/unix/af_unix.c~elevate-writers-opens-part4 +6 -1
 			goto fail;
+
+		err = mnt_want_write(nd.mnt);
+		if (err)
+			goto put_path_fail;
+
 		err = vfs_permission(&nd, MAY_WRITE);
 		if (err)
-			goto put_fail;
+			goto put_write_fail;

mnt_drop_write_fail maybe?

btw, IMHO *drop_write* would be enough for
the error labels, no need for the mnt part

best,
Herbert

Quoted 29 lines
net/unix/af_unix.c renamed from net/unix/af_unix.c~elevate-writers-opens-part4 +6 -3
 		err = -ECONNREFUSED;
 		if (!S_ISSOCK(nd.dentry->d_inode->i_mode))
-			goto put_fail;
+			goto put_write_fail;
 		u=unix_find_socket_byinode(nd.dentry->d_inode);
 		if (!u)
-			goto put_fail;
+			goto put_write_fail;
 
 		if (u->sk_type == type)
 			touch_atime(nd.mnt, nd.dentry);
 
 		path_release(&nd);
+		mnt_drop_write(nd.mnt);
 
 		err=-EPROTOTYPE;
 		if (u->sk_type != type) {
@@ -710,7 +716,9 @@ static struct sock *unix_find_other(stru
 	}
 	return u;
 
-put_fail:
+put_write_fail:
+	mnt_drop_write(nd.mnt);
+put_path_fail:
 	path_release(&nd);
 fail:
 	*error=err;

_

#39 lore raw

RFC PATCH 21/27 mount_is_safe(): add comment

In reply to Dave Hansen

This area of code is currently #ifdef'd out, so add a comment for the time when it is actually used.

---

 lxc-dave/fs/namespace.c |    4 ++++
 1 files changed, 4 insertions(+)
fs/namespace.c renamed from fs/namespace.c~elevate-writers-opens-part5 +4 -0
diff -puN fs/namespace.c~elevate-writers-opens-part5 fs/namespace.c
@@ -679,6 +679,10 @@ static int mount_is_safe(struct nameidat
 		if (current->uid != nd->dentry->d_inode->i_uid)
 			return -EPERM;
 	}
+	/*
+	 * We will eventually check for the mnt->writer_count here,
+	 * but since the code is not used now, skipt it - Dave Hansen
+	 */
 	if (vfs_permission(nd, MAY_WRITE))
 		return -EPERM;
 	return 0;

_

#40 lore raw

RFC PATCH 22/27 sys_mknodat(): elevate write count for vfs_mknod/create()

In reply to Dave Hansen

This takes care of all of the direct callers of vfs_mknod(). Since a few of these cases also handle normal file creation as well, this also covers some calls to vfs_create().

---

 ipc/mqueue.c                |    0 
 lxc-dave/fs/namei.c         |   48 ++++++++++++++++++++++++++++----------------
 lxc-dave/fs/nfsd/vfs.c      |    4 +++
 lxc-dave/net/unix/af_unix.c |    4 +++
 4 files changed, 39 insertions(+), 17 deletions(-)
fs/namei.c renamed from fs/namei.c~elevate-writers-vfs_mknod-try2 +31 -17
diff -puN fs/namei.c~elevate-writers-vfs_mknod-try2 fs/namei.c
@@ -1852,26 +1852,40 @@ asmlinkage long sys_mknodat(int dfd, con
 
 	if (!IS_POSIXACL(nd.dentry->d_inode))
 		mode &= ~current->fs->umask;
-	if (!IS_ERR(dentry)) {
-		switch (mode & S_IFMT) {
-		case 0: case S_IFREG:
-			error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
-			break;
-		case S_IFCHR: case S_IFBLK:
-			error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
-					new_decode_dev(dev));
-			break;
-		case S_IFIFO: case S_IFSOCK:
-			error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
+	if (IS_ERR(dentry))
+		goto out_unlock;
+	error = mnt_want_write(nd.mnt);
+	if (error)
+		goto out_dput;
+	switch (mode & S_IFMT) {
+	case 0: case S_IFREG:
+		error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
+		break;
+	case S_IFCHR: case S_IFBLK:
+		error = mnt_want_write(nd.mnt);
+		if (error)
 			break;
-		case S_IFDIR:
-			error = -EPERM;
+		error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
+				new_decode_dev(dev));
+		mnt_drop_write(nd.mnt);
+		break;
+	case S_IFIFO: case S_IFSOCK:
+		error = mnt_want_write(nd.mnt);
+		if (error)
 			break;
-		default:
-			error = -EINVAL;
-		}
-		dput(dentry);
+		error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
+		mnt_drop_write(nd.mnt);
+		break;
+	case S_IFDIR:
+		error = -EPERM;
+		break;
+	default:
+		error = -EINVAL;
 	}
+	mnt_drop_write(nd.mnt);
+out_dput:
+	dput(dentry);
+out_unlock:
 	mutex_unlock(&nd.dentry->d_inode->i_mutex);
 	path_release(&nd);
 out:
fs/nfsd/vfs.c renamed from fs/nfsd/vfs.c~elevate-writers-vfs_mknod-try2 +4 -0
diff -puN fs/nfsd/vfs.c~elevate-writers-vfs_mknod-try2 fs/nfsd/vfs.c
@@ -1153,6 +1153,9 @@ nfsd_create(struct svc_rqst *rqstp, stru
 	/*
 	 * Get the dir op function pointer.
 	 */
+	err = mnt_want_write(fhp->fh_export->ex_mnt);
+	if (err)
+		goto out_nfserr;
 	err = nfserr_perm;
 	switch (type) {
 	case S_IFREG:
@@ -1171,6 +1174,7 @@ nfsd_create(struct svc_rqst *rqstp, stru
 	        printk("nfsd: bad file type %o in nfsd_create\n", type);
 		err = -EINVAL;
 	}
+	mnt_drop_write(fhp->fh_export->ex_mnt);
 	if (err < 0)
 		goto out_nfserr;
 
net/unix/af_unix.c renamed from net/unix/af_unix.c~elevate-writers-vfs_mknod-try2 +4 -0
diff -puN net/unix/af_unix.c~elevate-writers-vfs_mknod-try2 net/unix/af_unix.c
@@ -789,7 +789,11 @@ static int unix_bind(struct socket *sock
 		 */
 		mode = S_IFSOCK |
 		       (SOCK_INODE(sock)->i_mode & ~current->fs->umask);
+		err = mnt_want_write(nd.mnt);
+		if (err)
+			goto out_mknod_dput;
 		err = vfs_mknod(nd.dentry->d_inode, dentry, mode, 0);
+		mnt_drop_write(nd.mnt);
 		if (err)
 			goto out_mknod_dput;
 		mutex_unlock(&nd.dentry->d_inode->i_mutex);

diff -puN ipc/mqueue.c~elevate-writers-vfs_mknod-try2 ipc/mqueue.c diff -L xattr.patch -puN /dev/null /dev/null
_

#41 lore raw

Re: [RFC][PATCH 22/27] sys_mknodat(): elevate write count for vfs_mknod/create()

Quoted 31 lines · Dave Hansen

This takes care of all of the direct callers of vfs_mknod(). Since a few of these cases also handle normal file creation as well, this also covers some calls to vfs_create().

---

 ipc/mqueue.c                |    0 
 lxc-dave/fs/namei.c         |   48 ++++++++++++++++++++++++++++----------------
 lxc-dave/fs/nfsd/vfs.c      |    4 +++
 lxc-dave/net/unix/af_unix.c |    4 +++
 4 files changed, 39 insertions(+), 17 deletions(-)

diff -puN fs/namei.c~elevate-writers-vfs_mknod-try2 fs/namei.c

fs/namei.c renamed from fs/namei.c~elevate-writers-vfs_mknod-try2 +0 -11
@@ -1852,26 +1852,40 @@ asmlinkage long sys_mknodat(int dfd, con
 
 	if (!IS_POSIXACL(nd.dentry->d_inode))
 		mode &= ~current->fs->umask;
-	if (!IS_ERR(dentry)) {
-		switch (mode & S_IFMT) {
-		case 0: case S_IFREG:
-			error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
-			break;
-		case S_IFCHR: case S_IFBLK:
-			error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
-					new_decode_dev(dev));
-			break;
-		case S_IFIFO: case S_IFSOCK:
-			error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);

fs/namei.c asmlinkage long sys_mknodat(int dfd, con · line 1864

Quoted 10 lines
fs/namei.c renamed from fs/namei.c~elevate-writers-vfs_mknod-try2 +10 -0
+	if (IS_ERR(dentry))
+		goto out_unlock;
+	error = mnt_want_write(nd.mnt);
+	if (error)
+		goto out_dput;
+	switch (mode & S_IFMT) {
+	case 0: case S_IFREG:
+		error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
+		break;
+	case S_IFCHR: case S_IFBLK:

hmm, could you outline some cases where the following fails, but the previous (above) check succeeded? (except for dubious locking issues)

fs/namei.c asmlinkage long sys_mknodat(int dfd, con · line 1872

Quoted 10 lines
fs/namei.c renamed from fs/namei.c~elevate-writers-vfs_mknod-try2 +7 -2
+		error = mnt_want_write(nd.mnt);
+		if (error)
 			break;
-		case S_IFDIR:
-			error = -EPERM;
+		error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
+				new_decode_dev(dev));
+		mnt_drop_write(nd.mnt);
+		break;
+	case S_IFIFO: case S_IFSOCK:

same here

fs/namei.c asmlinkage long sys_mknodat(int dfd, con · line 1880

Quoted 12 lines
fs/namei.c renamed from fs/namei.c~elevate-writers-vfs_mknod-try2 +7 -4
+		error = mnt_want_write(nd.mnt);
+		if (error)
 			break;
-		default:
-			error = -EINVAL;
-		}
-		dput(dentry);
+		error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
+		mnt_drop_write(nd.mnt);
+		break;
+	case S_IFDIR:
+		error = -EPERM;

this will give -EPERM on a r/w filesystem, but -EROFS on r/o, is that consistant with the current behaviour?

fs/namei.c asmlinkage long sys_mknodat(int dfd, con · line 1883

Quoted 3 lines
fs/namei.c renamed from fs/namei.c~elevate-writers-vfs_mknod-try2 +3 -0
+		break;
+	default:
+		error = -EINVAL;

same here for -EINVAL and -EROFS, maybe the mnt_want_write() wants to go into vfs_mknod()?

TIA,
Herbert

Quoted 47 lines
fs/namei.c renamed from fs/namei.c~elevate-writers-vfs_mknod-try2 +4 -0
 	}
+	mnt_drop_write(nd.mnt);
+out_dput:
+	dput(dentry);
+out_unlock:
 	mutex_unlock(&nd.dentry->d_inode->i_mutex);
 	path_release(&nd);
 out:

diff -puN fs/nfsd/vfs.c~elevate-writers-vfs_mknod-try2 fs/nfsd/vfs.c

fs/nfsd/vfs.c renamed from fs/nfsd/vfs.c~elevate-writers-vfs_mknod-try2 +4 -0
@@ -1153,6 +1153,9 @@ nfsd_create(struct svc_rqst *rqstp, stru
 	/*
 	 * Get the dir op function pointer.
 	 */
+	err = mnt_want_write(fhp->fh_export->ex_mnt);
+	if (err)
+		goto out_nfserr;
 	err = nfserr_perm;
 	switch (type) {
 	case S_IFREG:
@@ -1171,6 +1174,7 @@ nfsd_create(struct svc_rqst *rqstp, stru
 	        printk("nfsd: bad file type %o in nfsd_create\n", type);
 		err = -EINVAL;
 	}
+	mnt_drop_write(fhp->fh_export->ex_mnt);
 	if (err < 0)
 		goto out_nfserr;
 

diff -puN net/unix/af_unix.c~elevate-writers-vfs_mknod-try2 net/unix/af_unix.c

net/unix/af_unix.c renamed from net/unix/af_unix.c~elevate-writers-vfs_mknod-try2 +4 -0
@@ -789,7 +789,11 @@ static int unix_bind(struct socket *sock
 		 */
 		mode = S_IFSOCK |
 		       (SOCK_INODE(sock)->i_mode & ~current->fs->umask);
+		err = mnt_want_write(nd.mnt);
+		if (err)
+			goto out_mknod_dput;
 		err = vfs_mknod(nd.dentry->d_inode, dentry, mode, 0);
+		mnt_drop_write(nd.mnt);
 		if (err)
 			goto out_mknod_dput;
 		mutex_unlock(&nd.dentry->d_inode->i_mutex);

diff -puN ipc/mqueue.c~elevate-writers-vfs_mknod-try2 ipc/mqueue.c diff -L xattr.patch -puN /dev/null /dev/null
_

#42 lore raw

Re: [RFC][PATCH 22/27] sys_mknodat(): elevate write count for vfs_mknod/create()

Quoted 38 lines · Herbert Poetzl
Quoted 37 lines · Dave Hansen

This takes care of all of the direct callers of vfs_mknod(). Since a few of these cases also handle normal file creation as well, this also covers some calls to vfs_create().

---

 ipc/mqueue.c                |    0 
 lxc-dave/fs/namei.c         |   48 ++++++++++++++++++++++++++++----------------
 lxc-dave/fs/nfsd/vfs.c      |    4 +++
 lxc-dave/net/unix/af_unix.c |    4 +++
 4 files changed, 39 insertions(+), 17 deletions(-)

diff -puN fs/namei.c~elevate-writers-vfs_mknod-try2 fs/namei.c

fs/namei.c renamed from fs/namei.c~elevate-writers-vfs_mknod-try2 +6 -11
@@ -1852,26 +1852,40 @@ asmlinkage long sys_mknodat(int dfd, con
 
 	if (!IS_POSIXACL(nd.dentry->d_inode))
 		mode &= ~current->fs->umask;
-	if (!IS_ERR(dentry)) {
-		switch (mode & S_IFMT) {
-		case 0: case S_IFREG:
-			error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
-			break;
-		case S_IFCHR: case S_IFBLK:
-			error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
-					new_decode_dev(dev));
-			break;
-		case S_IFIFO: case S_IFSOCK:
-			error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
+	if (IS_ERR(dentry))
+		goto out_unlock;
+	error = mnt_want_write(nd.mnt);
+	if (error)
+		goto out_dput;
+	switch (mode & S_IFMT) {
Quoted 8 lines

fs/namei.c asmlinkage long sys_mknodat(int dfd, con · line 1864

Quoted 4 lines
fs/namei.c renamed from fs/namei.c~elevate-writers-vfs_mknod-try2 +4 -0
+	case 0: case S_IFREG:
+		error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
+		break;
+	case S_IFCHR: case S_IFBLK:

hmm, could you outline some cases where the following fails, but the previous (above) check succeeded? (except for dubious locking issues)

Are you asking about the vfs_create() call, or the second mnt_want_write() call?

I appear to have munged two patches together here. One which elevates the count over the entire switch(), and the other which does the elevation only around the actual vfs_*() calls. It should only have the lower-level calls.

Quoted 13 lines

fs/namei.c asmlinkage long sys_mknodat(int dfd, con · line 1872

Quoted 10 lines
fs/namei.c renamed from fs/namei.c~elevate-writers-vfs_mknod-try2 +7 -2
+		error = mnt_want_write(nd.mnt);
+		if (error)
 			break;
-		case S_IFDIR:
-			error = -EPERM;
+		error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
+				new_decode_dev(dev));
+		mnt_drop_write(nd.mnt);
+		break;
+	case S_IFIFO: case S_IFSOCK:

same here

Quoted 16 lines

fs/namei.c asmlinkage long sys_mknodat(int dfd, con · line 1880

Quoted 12 lines
fs/namei.c renamed from fs/namei.c~elevate-writers-vfs_mknod-try2 +7 -4
+		error = mnt_want_write(nd.mnt);
+		if (error)
 			break;
-		default:
-			error = -EINVAL;
-		}
-		dput(dentry);
+		error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
+		mnt_drop_write(nd.mnt);
+		break;
+	case S_IFDIR:
+		error = -EPERM;

this will give -EPERM on a r/w filesystem, but -EROFS on r/o, is that consistant with the current behaviour?

Nope. That's a bug, I think. I'll fix it up.

Quoted 6 lines

fs/namei.c asmlinkage long sys_mknodat(int dfd, con · line 1883

Quoted 3 lines
fs/namei.c renamed from fs/namei.c~elevate-writers-vfs_mknod-try2 +3 -0
+		break;
+	default:
+		error = -EINVAL;

same here for -EINVAL and -EROFS, maybe the mnt_want_write() wants to go into vfs_mknod()?

I think I tried that once, and it didn't quite work out. In this case, I think just giving vfs_mknod() the same treatment that I gave vfs_create() should do for now.

-- Dave

#43 lore raw

RFC PATCH 23/27 elevate write count over calls to vfs_rename()

In reply to Dave Hansen

---

 fs/xattr.c             |    0 
 lxc-dave/fs/namei.c    |   40 ++++++++++++++++++++++++++--------------
 lxc-dave/fs/nfsd/vfs.c |   12 +++++++++++-
 3 files changed, 37 insertions(+), 15 deletions(-)
fs/namei.c renamed from fs/namei.c~elevate-writers-vfs_rename-part1 +26 -14
diff -puN fs/namei.c~elevate-writers-vfs_rename-part1 fs/namei.c
@@ -2507,29 +2507,37 @@ static int do_rename(int olddfd, const c
 	if (error)
 		goto exit;
 
-	error = do_path_lookup(newdfd, newname, LOOKUP_PARENT, &newnd);
+	error = mnt_want_write(oldnd.mnt);
 	if (error)
 		goto exit1;
 
+	error = do_path_lookup(newdfd, newname, LOOKUP_PARENT, &newnd);
+	if (error)
+		goto exit2;
+
+	error = mnt_want_write(oldnd.mnt);
+	if (error)
+		goto exit3;
+
 	error = -EXDEV;
 	if (oldnd.mnt != newnd.mnt)
-		goto exit2;
+		goto exit4;
 
 	old_dir = oldnd.dentry;
 	error = -EBUSY;
 	if (oldnd.last_type != LAST_NORM)
-		goto exit2;
+		goto exit4;
 
 	new_dir = newnd.dentry;
 	if (newnd.last_type != LAST_NORM)
-		goto exit2;
+		goto exit4;
 
 	trap = lock_rename(new_dir, old_dir);
 
 	old_dentry = lookup_hash(&oldnd);
 	error = PTR_ERR(old_dentry);
 	if (IS_ERR(old_dentry))
-		goto exit3;
+		goto exit5;
 	/* source must exist */
 	error = -ENOENT;
 	if (!old_dentry->d_inode)
@@ -2538,33 +2546,37 @@ static int do_rename(int olddfd, const c
 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
 		error = -ENOTDIR;
 		if (oldnd.last.name[oldnd.last.len])
-			goto exit4;
+			goto exit6;
 		if (newnd.last.name[newnd.last.len])
-			goto exit4;
+			goto exit6;
 	}
 	/* source should not be ancestor of target */
 	error = -EINVAL;
 	if (old_dentry == trap)
-		goto exit4;
+		goto exit6;
 	new_dentry = lookup_hash(&newnd);
 	error = PTR_ERR(new_dentry);
 	if (IS_ERR(new_dentry))
-		goto exit4;
+		goto exit6;
 	/* target should not be an ancestor of source */
 	error = -ENOTEMPTY;
 	if (new_dentry == trap)
-		goto exit5;
+		goto exit7;
 
 	error = vfs_rename(old_dir->d_inode, old_dentry,
 				   new_dir->d_inode, new_dentry);
-exit5:
+exit7:
 	dput(new_dentry);
-exit4:
+exit6:
 	dput(old_dentry);
-exit3:
+exit5:
 	unlock_rename(new_dir, old_dir);
-exit2:
+exit4:
+	mnt_drop_write(newnd.mnt);
+exit3:
 	path_release(&newnd);
+exit2:
+	mnt_drop_write(oldnd.mnt);
 exit1:
 	path_release(&oldnd);
 exit:

diff -L ser -puN /dev/null /dev/null
diff -puN fs/ecryptfs/inode.c~elevate-writers-vfs_rename-part1 fs/ecryptfs/inode.c

fs/nfsd/vfs.c renamed from fs/nfsd/vfs.c~elevate-writers-vfs_rename-part1 +11 -1
diff -puN fs/nfsd/vfs.c~elevate-writers-vfs_rename-part1 fs/nfsd/vfs.c
@@ -1601,13 +1601,23 @@ nfsd_rename(struct svc_rqst *rqstp, stru
 			err = -EPERM;
 	} else
 #endif
+	err = mnt_want_write(ffhp->fh_export->ex_mnt);
+	if (err)
+		goto out_dput_new;
+
+	err = mnt_want_write(tfhp->fh_export->ex_mnt);
+	if (err)
+		goto out_mnt_drop_write_old;
+
 	err = vfs_rename(fdir, odentry, tdir, ndentry);
 	if (!err && EX_ISSYNC(tfhp->fh_export)) {
 		err = nfsd_sync_dir(tdentry);
 		if (!err)
 			err = nfsd_sync_dir(fdentry);
 	}
-
+	mnt_drop_write(tfhp->fh_export->ex_mnt);
+ out_mnt_drop_write_old:
+	mnt_drop_write(ffhp->fh_export->ex_mnt);
  out_dput_new:
 	dput(ndentry);
  out_dput_old:

diff -puN fs/nfsd/nfsfh.c~elevate-writers-vfs_rename-part1 fs/nfsd/nfsfh.c diff -puN fs/nfsd/nfs3proc.c~elevate-writers-vfs_rename-part1 fs/nfsd/nfs3proc.c diff -puN fs/xattr.c~elevate-writers-vfs_rename-part1 fs/xattr.c
_

#44 lore raw

Re: [RFC][PATCH 23/27] elevate write count over calls to vfs_rename()

Quoted 14 lines · Dave Hansen

---

 fs/xattr.c             |    0 
 lxc-dave/fs/namei.c    |   40 ++++++++++++++++++++++++++--------------
 lxc-dave/fs/nfsd/vfs.c |   12 +++++++++++-
 3 files changed, 37 insertions(+), 15 deletions(-)

diff -puN fs/namei.c~elevate-writers-vfs_rename-part1 fs/namei.c

fs/namei.c static int do_rename(int olddfd, const c · line 2512

Quoted 7 lines
fs/namei.c renamed from fs/namei.c~elevate-writers-vfs_rename-part1 +1 -1
@@ -2507,29 +2507,37 @@ static int do_rename(int olddfd, const c
 	if (error)
 		goto exit;
 
-	error = do_path_lookup(newdfd, newname, LOOKUP_PARENT, &newnd);
+	error = mnt_want_write(oldnd.mnt);
 	if (error)
 		goto exit1;

same here, I'd suspect it changes the return code for the case where the 'files' reside on different mount points from -EXDEV and for the busy case from -EBUSY to -EROFS ....

don't get me wrong, I'm probably fine with those changes if they are consistant and do not depend on whether the inode is RO or the entire mnt point

best,
Herbert

PS: IIRC, my test does not check this case either.
bugger!

Quoted 116 lines
fs/namei.c renamed from fs/namei.c~elevate-writers-vfs_rename-part1 +25 -13
+	error = do_path_lookup(newdfd, newname, LOOKUP_PARENT, &newnd);
+	if (error)
+		goto exit2;
+
+	error = mnt_want_write(oldnd.mnt);
+	if (error)
+		goto exit3;
+
 	error = -EXDEV;
 	if (oldnd.mnt != newnd.mnt)
-		goto exit2;
+		goto exit4;
 
 	old_dir = oldnd.dentry;
 	error = -EBUSY;
 	if (oldnd.last_type != LAST_NORM)
-		goto exit2;
+		goto exit4;
 
 	new_dir = newnd.dentry;
 	if (newnd.last_type != LAST_NORM)
-		goto exit2;
+		goto exit4;
 
 	trap = lock_rename(new_dir, old_dir);
 
 	old_dentry = lookup_hash(&oldnd);
 	error = PTR_ERR(old_dentry);
 	if (IS_ERR(old_dentry))
-		goto exit3;
+		goto exit5;
 	/* source must exist */
 	error = -ENOENT;
 	if (!old_dentry->d_inode)
@@ -2538,33 +2546,37 @@ static int do_rename(int olddfd, const c
 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
 		error = -ENOTDIR;
 		if (oldnd.last.name[oldnd.last.len])
-			goto exit4;
+			goto exit6;
 		if (newnd.last.name[newnd.last.len])
-			goto exit4;
+			goto exit6;
 	}
 	/* source should not be ancestor of target */
 	error = -EINVAL;
 	if (old_dentry == trap)
-		goto exit4;
+		goto exit6;
 	new_dentry = lookup_hash(&newnd);
 	error = PTR_ERR(new_dentry);
 	if (IS_ERR(new_dentry))
-		goto exit4;
+		goto exit6;
 	/* target should not be an ancestor of source */
 	error = -ENOTEMPTY;
 	if (new_dentry == trap)
-		goto exit5;
+		goto exit7;
 
 	error = vfs_rename(old_dir->d_inode, old_dentry,
 				   new_dir->d_inode, new_dentry);
-exit5:
+exit7:
 	dput(new_dentry);
-exit4:
+exit6:
 	dput(old_dentry);
-exit3:
+exit5:
 	unlock_rename(new_dir, old_dir);
-exit2:
+exit4:
+	mnt_drop_write(newnd.mnt);
+exit3:
 	path_release(&newnd);
+exit2:
+	mnt_drop_write(oldnd.mnt);
 exit1:
 	path_release(&oldnd);
 exit:

diff -L ser -puN /dev/null /dev/null
diff -puN fs/ecryptfs/inode.c~elevate-writers-vfs_rename-part1 fs/ecryptfs/inode.c diff -puN fs/nfsd/vfs.c~elevate-writers-vfs_rename-part1 fs/nfsd/vfs.c

fs/nfsd/vfs.c renamed from fs/nfsd/vfs.c~elevate-writers-vfs_rename-part1 +11 -1
@@ -1601,13 +1601,23 @@ nfsd_rename(struct svc_rqst *rqstp, stru
 			err = -EPERM;
 	} else
 #endif
+	err = mnt_want_write(ffhp->fh_export->ex_mnt);
+	if (err)
+		goto out_dput_new;
+
+	err = mnt_want_write(tfhp->fh_export->ex_mnt);
+	if (err)
+		goto out_mnt_drop_write_old;
+
 	err = vfs_rename(fdir, odentry, tdir, ndentry);
 	if (!err && EX_ISSYNC(tfhp->fh_export)) {
 		err = nfsd_sync_dir(tdentry);
 		if (!err)
 			err = nfsd_sync_dir(fdentry);
 	}
-
+	mnt_drop_write(tfhp->fh_export->ex_mnt);
+ out_mnt_drop_write_old:
+	mnt_drop_write(ffhp->fh_export->ex_mnt);
  out_dput_new:
 	dput(ndentry);
  out_dput_old:

diff -puN fs/nfsd/nfsfh.c~elevate-writers-vfs_rename-part1 fs/nfsd/nfsfh.c diff -puN fs/nfsd/nfs3proc.c~elevate-writers-vfs_rename-part1 fs/nfsd/nfs3proc.c diff -puN fs/xattr.c~elevate-writers-vfs_rename-part1 fs/xattr.c
_

#45 lore raw

Re: [RFC][PATCH 23/27] elevate write count over calls to vfs_rename()

Quoted 18 lines · Herbert Poetzl

fs/namei.c static int do_rename(int olddfd, const c · line 2512

Quoted 11 lines · Dave Hansen

diff -puN fs/namei.c~elevate-writers-vfs_rename-part1 fs/namei.c

fs/namei.c renamed from fs/namei.c~elevate-writers-vfs_rename-part1 +1 -1
@@ -2507,29 +2507,37 @@ static int do_rename(int olddfd, const c
 	if (error)
 		goto exit;
 
-	error = do_path_lookup(newdfd, newname, LOOKUP_PARENT, &newnd);
+	error = mnt_want_write(oldnd.mnt);
 	if (error)
 		goto exit1;

same here, I'd suspect it changes the return code for the case where the 'files' reside on different mount points from -EXDEV and for the busy case from -EBUSY to -EROFS ....

Quoted 3 lines

don't get me wrong, I'm probably fine with those changes if they are consistant and do not depend on whether the inode is RO or the entire mnt point

I think I might be able to fix this one up a bit by changing the order around. I'll have a good look at it.

-- Dave

#46 lore raw

Re: [RFC][PATCH 23/27] elevate write count over calls to vfs_rename()

In reply to Dave Hansen

fs/namei.c static int do_rename(int olddfd, const c · line 2518

Quoted 9 lines · Dave Hansen
fs/namei.c renamed from fs/namei.c~elevate-writers-vfs_rename-part1 +6 -0
+	error = mnt_want_write(oldnd.mnt);
 	if (error)
 		goto exit1;
 
+	error = do_path_lookup(newdfd, newname, LOOKUP_PARENT, &newnd);
+	if (error)
+		goto exit2;
+
+	error = mnt_want_write(oldnd.mnt);

WTF? Why raise it twice?

#47 lore raw

Re: [RFC][PATCH 23/27] elevate write count over calls to vfs_rename()

Quoted 5 lines · Al Viro
Quoted 4 lines · Dave Hansen
fs/namei.c renamed from fs/namei.c~elevate-writers-vfs_rename-part1 +1 -0
+	error = mnt_want_write(oldnd.mnt);
 	if (error)
 		goto exit1;
Quoted 7 lines

fs/namei.c static int do_rename(int olddfd, const c · line 2518

Quoted 5 lines
fs/namei.c renamed from fs/namei.c~elevate-writers-vfs_rename-part1 +5 -0
+	error = do_path_lookup(newdfd, newname, LOOKUP_PARENT, &newnd);
+	if (error)
+		goto exit2;
+
+	error = mnt_want_write(oldnd.mnt);

WTF? Why raise it twice?

That's a bug. The second raise should be for newnd.mnt. The fix will be there in the next set.

-- Dave

#48 lore raw

Re: [RFC][PATCH 23/27] elevate write count over calls to vfs_rename()

Quoted 14 lines · Dave Hansen
Quoted 5 lines · Al Viro
Quoted 4 lines · Dave Hansen
fs/namei.c renamed from fs/namei.c~elevate-writers-vfs_rename-part1 +1 -0
+	error = mnt_want_write(oldnd.mnt);
 	if (error)
 		goto exit1;
Quoted 7 lines

fs/namei.c static int do_rename(int olddfd, const c · line 2518

Quoted 5 lines
fs/namei.c renamed from fs/namei.c~elevate-writers-vfs_rename-part1 +5 -0
+	error = do_path_lookup(newdfd, newname, LOOKUP_PARENT, &newnd);
+	if (error)
+		goto exit2;
+
+	error = mnt_want_write(oldnd.mnt);

WTF? Why raise it twice?

Quoted 1 line

That's a bug. The second raise should be for newnd.mnt.

No, it should not. These two should be collapsed into one and done after we'd checked that vfsmounts are equal

#49 lore raw

RFC PATCH 24/27 elevate mount count for extended attributes

In reply to Dave Hansen

This basically audits the callers of xattr_permission(), which calls permission() itself.

---

 fs/namei.c                  |    0 
 lxc-dave/fs/nfsd/nfs4proc.c |    7 ++++++-
 lxc-dave/fs/xattr.c         |   14 ++++++++++++++
 3 files changed, 20 insertions(+), 1 deletion(-)
fs/xattr.c renamed from fs/xattr.c~xattr +14 -0
diff -puN fs/xattr.c~xattr fs/xattr.c
@@ -12,6 +12,7 @@
 #include <linux/smp_lock.h>
 #include <linux/file.h>
 #include <linux/xattr.h>
+#include <linux/mount.h>
 #include <linux/namei.h>
 #include <linux/security.h>
 #include <linux/syscalls.h>
@@ -210,7 +211,11 @@ sys_setxattr(char __user *path, char __u
 	error = user_path_walk(path, &nd);
 	if (error)
 		return error;
+	error = mnt_want_write(nd.mnt);
+	if (error)
+		return error;
 	error = setxattr(nd.dentry, name, value, size, flags);
+	mnt_drop_write(nd.mnt);
 	path_release(&nd);
 	return error;
 }
@@ -225,7 +230,11 @@ sys_lsetxattr(char __user *path, char __
 	error = user_path_walk_link(path, &nd);
 	if (error)
 		return error;
+	error = mnt_want_write(nd.mnt);
+	if (error)
+		return error;
 	error = setxattr(nd.dentry, name, value, size, flags);
+	mnt_drop_write(nd.mnt);
 	path_release(&nd);
 	return error;
 }
@@ -241,9 +250,14 @@ sys_fsetxattr(int fd, char __user *name,
 	f = fget(fd);
 	if (!f)
 		return error;
+	error = mnt_want_write(f->f_vfsmnt);
+	if (error)
+		goto out_fput;
 	dentry = f->f_dentry;
 	audit_inode(NULL, dentry->d_inode, 0);
 	error = setxattr(dentry, name, value, size, flags);
+	mnt_drop_write(f->f_vfsmnt);
+out_fput:
 	fput(f);
 	return error;
 }

diff -puN fs/nfsd/vfs.c~xattr fs/nfsd/vfs.c

fs/nfsd/nfs4proc.c renamed from fs/nfsd/nfs4proc.c~xattr +6 -1
diff -puN fs/nfsd/nfs4proc.c~xattr fs/nfsd/nfs4proc.c
@@ -604,13 +604,18 @@ nfsd4_setattr(struct svc_rqst *rqstp, st
 			return status;
 		}
 	}
+	status = mnt_want_write(current_fh->fh_export->ex_mnt);
+	if (status)
+		return status;
 	status = nfs_ok;
 	if (setattr->sa_acl != NULL)
 		status = nfsd4_set_nfs4_acl(rqstp, current_fh, setattr->sa_acl);
 	if (status)
-		return status;
+		goto out;
 	status = nfsd_setattr(rqstp, current_fh, &setattr->sa_iattr,
 				0, (time_t)0);
+out:
+	mnt_drop_write(current_fh->fh_export->ex_mnt);
 	return status;
 }
 

diff -puN fs/namei.c~xattr fs/namei.c
_

#50 lore raw

RFC PATCH 25/27 /proc/mounts: prep for flags from sb or mnt

In reply to Dave Hansen

Originally from: Herbert Poetzl <[email protected]>

Right now, show_vfsmnt() will produce output such as "ro" or "nodev" based on sb flags or mount flags. Each output string can only come from one of these sources.

This patch prepares show_vfsmnt() so that a single string can come from either source. We need this for r/o bind mounts.

The for loop's terminating condition was getting a little bit long, so I used ARRAY_SIZE() instead of checking for the NULL terminator.

---

 lxc-dave/fs/namespace.c |   46 ++++++++++++++++++++++++----------------------
 1 files changed, 24 insertions(+), 22 deletions(-)
fs/namespace.c renamed from fs/namespace.c~D6-proc-show-ro-attr +24 -22
diff -puN fs/namespace.c~D6-proc-show-ro-attr fs/namespace.c
@@ -354,24 +354,22 @@ static int show_vfsmnt(struct seq_file *
 {
 	struct vfsmount *mnt = v;
 	int err = 0;
+	int i;
 	static struct proc_fs_info {
-		int flag;
-		char *str;
+		int s_flag;
+		int mnt_flag;
+		char *set_str;
+		char *unset_str;
 	} fs_info[] = {
-		{ MS_SYNCHRONOUS, ",sync" },
-		{ MS_DIRSYNC, ",dirsync" },
-		{ MS_MANDLOCK, ",mand" },
-		{ 0, NULL }
-	};
-	static struct proc_fs_info mnt_info[] = {
-		{ MNT_NOSUID, ",nosuid" },
-		{ MNT_NODEV, ",nodev" },
-		{ MNT_NOEXEC, ",noexec" },
-		{ MNT_NOATIME, ",noatime" },
-		{ MNT_NODIRATIME, ",nodiratime" },
-		{ 0, NULL }
+		{ MS_SYNCHRONOUS, 0, ",sync", NULL },
+		{ MS_DIRSYNC, 0, ",dirsync", NULL },
+		{ MS_MANDLOCK, 0, ",mand", NULL },
+		{ 0, MNT_NOSUID, ",nosuid", NULL },
+		{ 0, MNT_NODEV, ",nodev", NULL },
+		{ 0, MNT_NOEXEC, ",noexec", NULL },
+		{ 0, MNT_NOATIME, ",noatime", NULL },
+		{ 0, MNT_NODIRATIME, ",nodiratime", NULL }
 	};
-	struct proc_fs_info *fs_infop;
 
 	mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
 	seq_putc(m, ' ');
@@ -379,13 +377,17 @@ static int show_vfsmnt(struct seq_file *
 	seq_putc(m, ' ');
 	mangle(m, mnt->mnt_sb->s_type->name);
 	seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? " ro" : " rw");
-	for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
-		if (mnt->mnt_sb->s_flags & fs_infop->flag)
-			seq_puts(m, fs_infop->str);
-	}
-	for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
-		if (mnt->mnt_flags & fs_infop->flag)
-			seq_puts(m, fs_infop->str);
+	for (i = 0; i < ARRAY_SIZE(fs_info); i++) {
+		struct proc_fs_info *fs_infop = &fs_info[i];
+		char *str = NULL;
+		if ((mnt->mnt_sb->s_flags & fs_infop->s_flag) ||
+		    (mnt->mnt_flags & fs_infop->mnt_flag))
+			str = fs_infop->set_str;
+		else
+			str = fs_infop->unset_str;
+
+		if (str)
+			seq_puts(m, str);
 	}
 	if (mnt->mnt_sb->s_op->show_options)
 		err = mnt->mnt_sb->s_op->show_options(m, mnt);

_

#51 lore raw

Re: [RFC][PATCH 25/27] /proc/mounts: prep for flags from sb or mnt

Quoted 7 lines · Dave Hansen

Originally from: Herbert Poetzl <[email protected]>

Right now, show_vfsmnt() will produce output such as "ro" or "nodev" based on sb flags or mount flags. Each output string can only come from one of these sources.

Quoted 6 lines

This patch prepares show_vfsmnt() so that a single string can come from either source. We need this for r/o bind mounts.

The for loop's terminating condition was getting a little bit long, so I used ARRAY_SIZE() instead of checking for the NULL terminator.

IMHO this can go in unconditionally, even if there are no read only MNT flags present, as it is supposed to work now and in the future ... maybe I should give that a special test run on its own ...

Quoted 73 lines

---

 lxc-dave/fs/namespace.c |   46 ++++++++++++++++++++++++----------------------
 1 files changed, 24 insertions(+), 22 deletions(-)

diff -puN fs/namespace.c~D6-proc-show-ro-attr fs/namespace.c

fs/namespace.c renamed from fs/namespace.c~D6-proc-show-ro-attr +24 -22
@@ -354,24 +354,22 @@ static int show_vfsmnt(struct seq_file *
 {
 	struct vfsmount *mnt = v;
 	int err = 0;
+	int i;
 	static struct proc_fs_info {
-		int flag;
-		char *str;
+		int s_flag;
+		int mnt_flag;
+		char *set_str;
+		char *unset_str;
 	} fs_info[] = {
-		{ MS_SYNCHRONOUS, ",sync" },
-		{ MS_DIRSYNC, ",dirsync" },
-		{ MS_MANDLOCK, ",mand" },
-		{ 0, NULL }
-	};
-	static struct proc_fs_info mnt_info[] = {
-		{ MNT_NOSUID, ",nosuid" },
-		{ MNT_NODEV, ",nodev" },
-		{ MNT_NOEXEC, ",noexec" },
-		{ MNT_NOATIME, ",noatime" },
-		{ MNT_NODIRATIME, ",nodiratime" },
-		{ 0, NULL }
+		{ MS_SYNCHRONOUS, 0, ",sync", NULL },
+		{ MS_DIRSYNC, 0, ",dirsync", NULL },
+		{ MS_MANDLOCK, 0, ",mand", NULL },
+		{ 0, MNT_NOSUID, ",nosuid", NULL },
+		{ 0, MNT_NODEV, ",nodev", NULL },
+		{ 0, MNT_NOEXEC, ",noexec", NULL },
+		{ 0, MNT_NOATIME, ",noatime", NULL },
+		{ 0, MNT_NODIRATIME, ",nodiratime", NULL }
 	};
-	struct proc_fs_info *fs_infop;
 
 	mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
 	seq_putc(m, ' ');
@@ -379,13 +377,17 @@ static int show_vfsmnt(struct seq_file *
 	seq_putc(m, ' ');
 	mangle(m, mnt->mnt_sb->s_type->name);
 	seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? " ro" : " rw");
-	for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
-		if (mnt->mnt_sb->s_flags & fs_infop->flag)
-			seq_puts(m, fs_infop->str);
-	}
-	for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
-		if (mnt->mnt_flags & fs_infop->flag)
-			seq_puts(m, fs_infop->str);
+	for (i = 0; i < ARRAY_SIZE(fs_info); i++) {
+		struct proc_fs_info *fs_infop = &fs_info[i];
+		char *str = NULL;
+		if ((mnt->mnt_sb->s_flags & fs_infop->s_flag) ||
+		    (mnt->mnt_flags & fs_infop->mnt_flag))
+			str = fs_infop->set_str;
+		else
+			str = fs_infop->unset_str;
+
+		if (str)
+			seq_puts(m, str);
 	}
 	if (mnt->mnt_sb->s_op->show_options)
 		err = mnt->mnt_sb->s_op->show_options(m, mnt);

_

#52 lore raw

RFC PATCH 26/27 /proc/mounts: treat ro/rw like the rest

In reply to Dave Hansen

Originally from: Herbert Poetzl <[email protected]>

Now that we have the set/unset fields in the fs_info[] array, we can stop special-casing the ro/rw bits.

---

 lxc-dave/fs/namespace.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletion(-)
fs/namespace.c renamed from fs/namespace.c~D7-proc-show-ro-attr-actual +2 -1
diff -puN fs/namespace.c~D7-proc-show-ro-attr-actual fs/namespace.c
@@ -361,6 +361,7 @@ static int show_vfsmnt(struct seq_file *
 		char *set_str;
 		char *unset_str;
 	} fs_info[] = {
+		{ MS_RDONLY, MNT_RDONLY, "ro", "rw" },
 		{ MS_SYNCHRONOUS, 0, ",sync", NULL },
 		{ MS_DIRSYNC, 0, ",dirsync", NULL },
 		{ MS_MANDLOCK, 0, ",mand", NULL },
@@ -376,7 +377,7 @@ static int show_vfsmnt(struct seq_file *
 	seq_path(m, mnt, mnt->mnt_root, " \t\n\\");
 	seq_putc(m, ' ');
 	mangle(m, mnt->mnt_sb->s_type->name);
-	seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? " ro" : " rw");
+	seq_putc(m, ' ');
 	for (i = 0; i < ARRAY_SIZE(fs_info); i++) {
 		struct proc_fs_info *fs_infop = &fs_info[i];
 		char *str = NULL;

_

#53 lore raw

Re: [RFC][PATCH 26/27] /proc/mounts: treat ro/rw like the rest

Quoted 5 lines · Dave Hansen

Originally from: Herbert Poetzl <[email protected]>

Now that we have the set/unset fields in the fs_info[] array, we can stop special-casing the ro/rw bits.

Quoted 27 lines

---

 lxc-dave/fs/namespace.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletion(-)

diff -puN fs/namespace.c~D7-proc-show-ro-attr-actual fs/namespace.c

fs/namespace.c renamed from fs/namespace.c~D7-proc-show-ro-attr-actual +2 -1
@@ -361,6 +361,7 @@ static int show_vfsmnt(struct seq_file *
 		char *set_str;
 		char *unset_str;
 	} fs_info[] = {
+		{ MS_RDONLY, MNT_RDONLY, "ro", "rw" },
 		{ MS_SYNCHRONOUS, 0, ",sync", NULL },
 		{ MS_DIRSYNC, 0, ",dirsync", NULL },
 		{ MS_MANDLOCK, 0, ",mand", NULL },
@@ -376,7 +377,7 @@ static int show_vfsmnt(struct seq_file *
 	seq_path(m, mnt, mnt->mnt_root, " \t\n\\");
 	seq_putc(m, ' ');
 	mangle(m, mnt->mnt_sb->s_type->name);
-	seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? " ro" : " rw");
+	seq_putc(m, ' ');
 	for (i = 0; i < ARRAY_SIZE(fs_info); i++) {
 		struct proc_fs_info *fs_infop = &fs_info[i];
 		char *str = NULL;

_

#54 lore raw

RFC PATCH 27/27 create and pass read-only mnt flag into do_loopback()

In reply to Dave Hansen

Originally from: Herbert Poetzl <[email protected]>

This is the core of the read-only bind mount patch set.

This patch introduces MNT_RDONLY and ensures that it is checked before any users may obtain write permissions for a file through any mount.

---

 lxc-dave/fs/file.c             |    1 +
 lxc-dave/fs/inode.c            |    2 +-
 lxc-dave/fs/namespace.c        |    8 ++++++--
 lxc-dave/include/linux/mount.h |    3 +++
 lxc-dave/mm/filemap.c          |    3 +++
 5 files changed, 14 insertions(+), 3 deletions(-)
fs/file.c renamed from fs/file.c~D8-actually-add-flags +1 -0
diff -puN fs/file.c~D8-actually-add-flags fs/file.c
@@ -12,6 +12,7 @@
 #include <linux/slab.h>
 #include <linux/vmalloc.h>
 #include <linux/file.h>
+#include <linux/mount.h>
 #include <linux/bitops.h>
 #include <linux/interrupt.h>
 #include <linux/spinlock.h>
fs/inode.c renamed from fs/inode.c~D8-actually-add-flags +1 -1
diff -puN fs/inode.c~D8-actually-add-flags fs/inode.c
@@ -1185,7 +1185,7 @@ void touch_atime(struct vfsmount *mnt, s
 	struct inode *inode = dentry->d_inode;
 	struct timespec now;
 
-	if (IS_RDONLY(inode))
+	if (IS_RDONLY(inode) || (mnt->mnt_flags & MNT_RDONLY))
 		return;
 
 	if ((inode->i_flags & S_NOATIME) ||
fs/namespace.c renamed from fs/namespace.c~D8-actually-add-flags +6 -2
diff -puN fs/namespace.c~D8-actually-add-flags fs/namespace.c
@@ -905,7 +905,8 @@ static int do_change_type(struct nameida
 /*
  * do loopback mount.
  */
-static int do_loopback(struct nameidata *nd, char *old_name, int recurse)
+static int do_loopback(struct nameidata *nd, char *old_name,
+		       int recurse, int mnt_flags)
 {
 	struct nameidata old_nd;
 	struct vfsmount *mnt = NULL;
@@ -943,6 +944,7 @@ static int do_loopback(struct nameidata 
 		spin_unlock(&vfsmount_lock);
 		release_mounts(&umount_list);
 	}
+	mnt->mnt_flags = mnt_flags;
 
 out:
 	up_write(&namespace_sem);
@@ -1403,6 +1405,8 @@ long do_mount(char *dev_name, char *dir_
 		((char *)data_page)[PAGE_SIZE - 1] = 0;
 
 	/* Separate the per-mountpoint flags */
+	if (flags & MS_RDONLY)
+		mnt_flags |= MNT_RDONLY;
 	if (flags & MS_NOSUID)
 		mnt_flags |= MNT_NOSUID;
 	if (flags & MS_NODEV)
@@ -1430,7 +1434,7 @@ long do_mount(char *dev_name, char *dir_
 		retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
 				    data_page);
 	else if (flags & MS_BIND)
-		retval = do_loopback(&nd, dev_name, flags & MS_REC);
+		retval = do_loopback(&nd, dev_name, flags & MS_REC, mnt_flags);
 	else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
 		retval = do_change_type(&nd, flags);
 	else if (flags & MS_MOVE)
include/linux/mount.h renamed from include/linux/mount.h~D8-actually-add-flags +3 -0
diff -puN include/linux/mount.h~D8-actually-add-flags include/linux/mount.h
@@ -24,6 +24,7 @@
 #define MNT_NOEXEC	0x04
 #define MNT_NOATIME	0x08
 #define MNT_NODIRATIME	0x10
+#define MNT_RDONLY	0x20
 
 #define MNT_SHRINKABLE	0x100
 
@@ -63,6 +64,8 @@ static inline struct vfsmount *mntget(st
 
 static inline int mnt_want_write(struct vfsmount *mnt)
 {
+	if (mnt->mnt_flags & MNT_RDONLY)
+		return -EROFS;
 	atomic_inc(&mnt->mnt_writers);
 	return 0;
 }
mm/filemap.c renamed from mm/filemap.c~D8-actually-add-flags +3 -0
diff -puN mm/filemap.c~D8-actually-add-flags mm/filemap.c
@@ -23,6 +23,7 @@
 #include <linux/mman.h>
 #include <linux/pagemap.h>
 #include <linux/file.h>
+#include <linux/mount.h>
 #include <linux/uio.h>
 #include <linux/hash.h>
 #include <linux/writeback.h>
@@ -1960,6 +1961,8 @@ inline int generic_write_checks(struct f
                 return -EINVAL;
 
 	if (!isblk) {
+		if (file->f_vfsmnt->mnt_flags & MNT_RDONLY)
+			return -EROFS;
 		/* FIXME: this is for backwards compatibility with 2.4 */
 		if (file->f_flags & O_APPEND)
                         *pos = i_size_read(inode);

_