Skip to contentSkip to conversation

vfs: add helpers to check r/o bind mounts

PATCH In review Miklos Szeredi 8 participants 43 messages Apr 2, 2008 – Apr 10, 2008

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

cover vfs: add helpers to check r/o bind mounts

See first patch for description. This is against vfs-2.6 + two patches I posted recently.

It's not for -mm yet, as it needs conversion of unionfs (will that be done by me, I wonder?)

Thanks,
Miklos

--

#1 lore raw

PATCH 1/10 vfs: add path_create() and path_mknod()

From: Miklos Szeredi <[email protected]>

R/o bind mounts require operations which modify the filesystem to be wrapped in mnt_want_write()/mnt_drop_write(). Create helpers which do this, so callers won't need to bother, and more importantly, cannot forget! Call these path_*, analogous to vfs_*. Where there are no callers of vfs_* left, make them static.

This series is a cleanup, as well as fixing several places (mostly in nfsd) where mnt_{want,drop}_write() were missing.

It will also help with merging You Know What(*) security module, which needs to know the path within the namespace, and not just within the filesystem. These helpers will allow the security hooks to be in a common place, and need not be repeated in all callers.

(*) AppArmor is a swear word in VFS circles, for some reason or other.

This patch:

Introduce path_create() and path_mknod(). Make vfs_create() and vfs_mknod() static.

---
 fs/ecryptfs/inode.c |   21 +++++++-------
 fs/namei.c          |   75 +++++++++++++++++++++++++++-------------------------
 fs/nfsd/vfs.c       |   19 ++++++-------
 include/linux/fs.h  |    4 +-
 ipc/mqueue.c        |    6 +++-
 net/unix/af_unix.c  |    6 ----
 6 files changed, 68 insertions(+), 63 deletions(-)
fs/namei.c +40 -35
Index: vfs-2.6/fs/namei.c
===================================================================
@@ -1574,7 +1574,7 @@ void unlock_rename(struct dentry *p1, st
 	}
 }
 
-int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
+static int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
 		struct nameidata *nd)
 {
 	int error = may_create(dir, dentry, nd);
@@ -1596,6 +1596,20 @@ int vfs_create(struct inode *dir, struct
 	return error;
 }
 
+int path_create(struct path *dir_path, struct dentry *dentry, int mode,
+		struct nameidata *nd)
+{
+	int error = mnt_want_write(dir_path->mnt);
+
+	if (!error) {
+		error = vfs_create(dir_path->dentry->d_inode, dentry, mode, nd);
+		mnt_drop_write(dir_path->mnt);
+	}
+
+	return error;
+}
+EXPORT_SYMBOL(path_create);
+
 int may_open(struct nameidata *nd, int acc_mode, int flag)
 {
 	struct dentry *dentry = nd->path.dentry;
@@ -2015,7 +2029,7 @@ fail:
 }
 EXPORT_SYMBOL_GPL(lookup_create);
 
-int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
+static int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
 {
 	int error = may_create(dir, dentry, NULL);
 
@@ -2039,22 +2053,19 @@ int vfs_mknod(struct inode *dir, struct 
 	return error;
 }
 
-static int may_mknod(mode_t mode)
+int path_mknod(struct path *dir_path, struct dentry *dentry, int mode,
+	       dev_t dev)
 {
-	switch (mode & S_IFMT) {
-	case S_IFREG:
-	case S_IFCHR:
-	case S_IFBLK:
-	case S_IFIFO:
-	case S_IFSOCK:
-	case 0: /* zero mode translates to S_IFREG */
-		return 0;
-	case S_IFDIR:
-		return -EPERM;
-	default:
-		return -EINVAL;
+	int error = mnt_want_write(dir_path->mnt);
+
+	if (!error) {
+		error = vfs_mknod(dir_path->dentry->d_inode, dentry, mode, dev);
+		mnt_drop_write(dir_path->mnt);
 	}
+
+	return error;
 }
+EXPORT_SYMBOL(path_mknod);
 
 asmlinkage long sys_mknodat(int dfd, const char __user *filename, int mode,
 				unsigned dev)
@@ -2080,26 +2091,22 @@ asmlinkage long sys_mknodat(int dfd, con
 	}
 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
 		mode &= ~current->fs->umask;
-	error = may_mknod(mode);
-	if (error)
-		goto out_dput;
-	error = mnt_want_write(nd.path.mnt);
-	if (error)
-		goto out_dput;
 	switch (mode & S_IFMT) {
-		case 0: case S_IFREG:
-			error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
-			break;
-		case S_IFCHR: case S_IFBLK:
-			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
-					new_decode_dev(dev));
-			break;
-		case S_IFIFO: case S_IFSOCK:
-			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
-			break;
+	case 0: case S_IFREG:
+		error = path_create(&nd.path, dentry, mode, &nd);
+		break;
+	case S_IFCHR: case S_IFBLK:
+		error = path_mknod(&nd.path, dentry, mode, new_decode_dev(dev));
+		break;
+	case S_IFIFO: case S_IFSOCK:
+		error = path_mknod(&nd.path, dentry, mode, 0);
+		break;
+	case S_IFDIR:
+		error = -EPERM;
+		break;
+	default:
+		error = -EINVAL;
 	}
-	mnt_drop_write(nd.path.mnt);
-out_dput:
 	dput(dentry);
 out_unlock:
 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
@@ -2966,11 +2973,9 @@ EXPORT_SYMBOL(permission);
 EXPORT_SYMBOL(vfs_permission);
 EXPORT_SYMBOL(file_permission);
 EXPORT_SYMBOL(unlock_rename);
-EXPORT_SYMBOL(vfs_create);
 EXPORT_SYMBOL(vfs_follow_link);
 EXPORT_SYMBOL(vfs_link);
 EXPORT_SYMBOL(vfs_mkdir);
-EXPORT_SYMBOL(vfs_mknod);
 EXPORT_SYMBOL(generic_permission);
 EXPORT_SYMBOL(vfs_readlink);
 EXPORT_SYMBOL(vfs_rename);
include/linux/fs.h +2 -2
Index: vfs-2.6/include/linux/fs.h
===================================================================
@@ -1123,9 +1123,9 @@ extern void unlock_super(struct super_bl
  * VFS helper functions..
  */
 extern int vfs_permission(struct nameidata *, int);
-extern int vfs_create(struct inode *, struct dentry *, int, struct nameidata *);
+extern int path_create(struct path *, struct dentry *, int, struct nameidata *);
 extern int vfs_mkdir(struct inode *, struct dentry *, int);
-extern int vfs_mknod(struct inode *, struct dentry *, int, dev_t);
+extern int path_mknod(struct path *, struct dentry *, int, dev_t);
 extern int vfs_symlink(struct inode *, struct dentry *, const char *, int);
 extern int vfs_link(struct dentry *, struct inode *, struct dentry *);
 extern int vfs_rmdir(struct inode *, struct dentry *);
fs/ecryptfs/inode.c +11 -10
Index: vfs-2.6/fs/ecryptfs/inode.c
===================================================================
@@ -67,7 +67,7 @@ static void unlock_dir(struct dentry *di
  * Returns zero on success; non-zero on error condition
  */
 static int
-ecryptfs_create_underlying_file(struct inode *lower_dir_inode,
+ecryptfs_create_underlying_file(struct dentry *lower_dir_dentry,
 				struct dentry *dentry, int mode,
 				struct nameidata *nd)
 {
@@ -79,9 +79,9 @@ ecryptfs_create_underlying_file(struct i
 
 	dentry_save = nd->path.dentry;
 	vfsmount_save = nd->path.mnt;
-	nd->path.dentry = lower_dentry;
+	nd->path.dentry = lower_dir_dentry;
 	nd->path.mnt = lower_mnt;
-	rc = vfs_create(lower_dir_inode, lower_dentry, mode, nd);
+	rc = path_create(&nd->path, lower_dentry, mode, nd);
 	nd->path.dentry = dentry_save;
 	nd->path.mnt = vfsmount_save;
 	return rc;
@@ -117,7 +117,7 @@ ecryptfs_do_create(struct inode *directo
 		rc = PTR_ERR(lower_dir_dentry);
 		goto out;
 	}
-	rc = ecryptfs_create_underlying_file(lower_dir_dentry->d_inode,
+	rc = ecryptfs_create_underlying_file(lower_dir_dentry,
 					     ecryptfs_dentry, mode, nd);
 	if (rc) {
 		printk(KERN_ERR "%s: Failure to create dentry in lower fs; "
@@ -535,20 +535,21 @@ ecryptfs_mknod(struct inode *dir, struct
 {
 	int rc;
 	struct dentry *lower_dentry;
-	struct dentry *lower_dir_dentry;
+	struct path lower_dir;
 
 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
-	lower_dir_dentry = lock_parent(lower_dentry);
-	rc = vfs_mknod(lower_dir_dentry->d_inode, lower_dentry, mode, dev);
+	lower_dir.mnt = ecryptfs_dentry_to_lower_mnt(dentry);
+	lower_dir.dentry = lock_parent(lower_dentry);
+	rc = path_mknod(&lower_dir, lower_dentry, mode, dev);
 	if (rc || !lower_dentry->d_inode)
 		goto out;
 	rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
 	if (rc)
 		goto out;
-	fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
-	fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
+	fsstack_copy_attr_times(dir, lower_dir.dentry->d_inode);
+	fsstack_copy_inode_size(dir, lower_dir.dentry->d_inode);
 out:
-	unlock_dir(lower_dir_dentry);
+	unlock_dir(lower_dir.dentry);
 	if (!dentry->d_inode)
 		d_drop(dentry);
 	return rc;
fs/nfsd/vfs.c +9 -10
Index: vfs-2.6/fs/nfsd/vfs.c
===================================================================
@@ -1185,6 +1185,7 @@ nfsd_create(struct svc_rqst *rqstp, stru
 		char *fname, int flen, struct iattr *iap,
 		int type, dev_t rdev, struct svc_fh *resfhp)
 {
+	struct path 	dir_path;
 	struct dentry	*dentry, *dchild = NULL;
 	struct inode	*dirp;
 	__be32		err;
@@ -1249,13 +1250,12 @@ nfsd_create(struct svc_rqst *rqstp, stru
 		iap->ia_mode = 0;
 	iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
 
-	/*
-	 * Get the dir op function pointer.
-	 */
+	dir_path.mnt = fhp->fh_export->ex_path.mnt;
+	dir_path.dentry = dentry;
 	err = 0;
 	switch (type) {
 	case S_IFREG:
-		host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
+		host_err = path_create(&dir_path, dchild, iap->ia_mode, NULL);
 		break;
 	case S_IFDIR:
 		host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
@@ -1264,11 +1264,7 @@ nfsd_create(struct svc_rqst *rqstp, stru
 	case S_IFBLK:
 	case S_IFIFO:
 	case S_IFSOCK:
-		host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
-		if (host_err)
-			break;
-		host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
-		mnt_drop_write(fhp->fh_export->ex_path.mnt);
+		host_err = path_mknod(&dir_path, dchild, iap->ia_mode, rdev);
 		break;
 	default:
 	        printk("nfsd: bad file type %o in nfsd_create\n", type);
@@ -1311,6 +1307,7 @@ nfsd_create_v3(struct svc_rqst *rqstp, s
 		struct svc_fh *resfhp, int createmode, u32 *verifier,
 	        int *truncp, int *created)
 {
+	struct path 	dir_path;
 	struct dentry	*dentry, *dchild = NULL;
 	struct inode	*dirp;
 	__be32		err;
@@ -1397,7 +1394,9 @@ nfsd_create_v3(struct svc_rqst *rqstp, s
 		goto out;
 	}
 
-	host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
+	dir_path.mnt = fhp->fh_export->ex_path.mnt;
+	dir_path.dentry = dentry;
+	host_err = path_create(&dir_path, dchild, iap->ia_mode, NULL);
 	if (host_err < 0)
 		goto out_nfserr;
 	if (created)
ipc/mqueue.c +5 -1
Index: vfs-2.6/ipc/mqueue.c
===================================================================
@@ -599,6 +599,7 @@ static struct file *do_create(struct den
 {
 	struct mq_attr attr;
 	struct file *result;
+	struct path dir_path;
 	int ret;
 
 	if (u_attr) {
@@ -616,7 +617,10 @@ static struct file *do_create(struct den
 	ret = mnt_want_write(mqueue_mnt);
 	if (ret)
 		goto out;
-	ret = vfs_create(dir->d_inode, dentry, mode, NULL);
+
+	dir_path.mnt = mqueue_mnt;
+	dir_path.dentry = dir;
+	ret = path_create(&dir_path, dentry, mode, NULL);
 	dentry->d_fsdata = NULL;
 	if (ret)
 		goto out_drop_write;
net/unix/af_unix.c +1 -5
Index: vfs-2.6/net/unix/af_unix.c
===================================================================
@@ -819,11 +819,7 @@ static int unix_bind(struct socket *sock
 		 */
 		mode = S_IFSOCK |
 		       (SOCK_INODE(sock)->i_mode & ~current->fs->umask);
-		err = mnt_want_write(nd.path.mnt);
-		if (err)
-			goto out_mknod_dput;
-		err = vfs_mknod(nd.path.dentry->d_inode, dentry, mode, 0);
-		mnt_drop_write(nd.path.mnt);
+		err = path_mknod(&nd.path, dentry, mode, 0);
 		if (err)
 			goto out_mknod_dput;
 		mutex_unlock(&nd.path.dentry->d_inode->i_mutex);

--

maintainer
#2 lore raw

Re: [patch 01/10] vfs: add path_create() and path_mknod()

Quoted 8 lines · Miklos Szeredi

From: Miklos Szeredi <[email protected]>

R/o bind mounts require operations which modify the filesystem to be wrapped in mnt_want_write()/mnt_drop_write(). Create helpers which do this, so callers won't need to bother, and more importantly, cannot forget! Call these path_*, analogous to vfs_*. Where there are no callers of vfs_* left, make them static.

Quoted 7 lines

This series is a cleanup, as well as fixing several places (mostly in nfsd) where mnt_{want,drop}_write() were missing.

It will also help with merging You Know What(*) security module, which needs to know the path within the namespace, and not just within the filesystem. These helpers will allow the security hooks to be in a common place, and need not be repeated in all callers.

Rot.

Places in nfsd must be fixed, LSM hooks *moved* *to* *callers*. And really, by that point I absolutely do not give a damn for these clowns. "Help with merging" implies that they can't be arsed to do _anything_ with their code. Just as they could not be arsed to react to any comments (including civil ones) in any manner except "wait for a month and repost without changes". Sod them.

And no, "make static where all (two of) current callers have vfsmount" is non-starter. path_...() is (at most) a convenience helper, not a fundamental interface - simply because new callers should not be forced into inventing a fake vfsmount just to use that.

I'll look into missing mnt_..._write() in nfsd and fix that. The rest... Sorry.

#3 lore raw

Re: [patch 01/10] vfs: add path_create() and path_mknod()

Quoted 14 lines
Quoted 6 lines

R/o bind mounts require operations which modify the filesystem to be wrapped in mnt_want_write()/mnt_drop_write(). Create helpers which do this, so callers won't need to bother, and more importantly, cannot forget! Call these path_*, analogous to vfs_*. Where there are no callers of vfs_* left, make them static.

Quoted 7 lines

This series is a cleanup, as well as fixing several places (mostly in nfsd) where mnt_{want,drop}_write() were missing.

It will also help with merging You Know What(*) security module, which needs to know the path within the namespace, and not just within the filesystem. These helpers will allow the security hooks to be in a common place, and need not be repeated in all callers.

Quoted 8 lines

Rot.

Places in nfsd must be fixed, LSM hooks *moved* *to* *callers*. And really, by that point I absolutely do not give a damn for these clowns. "Help with merging" implies that they can't be arsed to do _anything_ with their code. Just as they could not be arsed to react to any comments (including civil ones) in any manner except "wait for a month and repost without changes". Sod them.

Al, calm down. I've been asked to help with merging this code, and if there are concerns with it, I'll help fix them. If you had bad experience with it in the past, I'm sorry. But let that not make this any harder that in need to be.

Quoted 4 lines

And no, "make static where all (two of) current callers have vfsmount" is non-starter. path_...() is (at most) a convenience helper, not a fundamental interface - simply because new callers should not be forced into inventing a fake vfsmount just to use that.

It is an interface with at least 3 callers at the moment: syscalls, nfsd and ecryptfs and unionfs in the future. It is an interface because all external accesses to the filesystem *are* done through the namespace and not directly on filesystem internals.

Such direct access would be conceivable, but I don't think we should base the design on theoretically possible uses. If those uses appear, we can change the interface to fit that.

This "move everything to callers" thing is wrong because it just results in bloat and bugs, none of which is desirable in the kernel.

Miklos

maintainer
#4 lore raw

Re: [patch 01/10] vfs: add path_create() and path_mknod()

Quoted 11 lines · Miklos Szeredi

It is an interface with at least 3 callers at the moment: syscalls, nfsd and ecryptfs and unionfs in the future. It is an interface because all external accesses to the filesystem *are* done through the namespace and not directly on filesystem internals.

Such direct access would be conceivable, but I don't think we should base the design on theoretically possible uses. If those uses appear, we can change the interface to fit that.

This "move everything to callers" thing is wrong because it just results in bloat and bugs, none of which is desirable in the kernel.

I disagree. First of all, clear separation between operations on _filesystem_, which should all be namespace-agnostic and things that depend on vfsmount is a Good Thing(tm). Think of that as of separation between server (superblock and everything related to it, starting with dentry tree) and clients; mixing those is a bloody bad idea.

What's more, I'm not at all convinced that for nfsd it's the right set of checks, to start with. The same goes for future users.

As for ecryptfs, looking at their "lower_mnt" thing... I'd say that it's a nonsense. For one thing, duplicating a reference into ever dentry out there (and it's simply duplicated) makes no sense whatsoever. For another... I'm not at all sure that remount of the underlying vfsmount r/o *should* take that sucker read-only. And if it should, it's clearly an action that should have a visible effect on superblock flags of ecryptfs.

Incidentally, looking at ecryptfs open(), WTF is protecting
crypt_stat->flags? We take crypt_stat->cs_mutex, do nothing but check-and-modify of ->flags under it, then drop and several lines later do crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED); with no ->cs_mutex held...

#5 lore raw

Re: [patch 01/10] vfs: add path_create() and path_mknod()

Quoted 6 lines · Al Viro

I disagree. First of all, clear separation between operations on _filesystem_, which should all be namespace-agnostic and things that depend on vfsmount is a Good Thing(tm). Think of that as of separation between server (superblock and everything related to it, starting with dentry tree) and clients; mixing those is a bloody bad idea.

Speaking of which: is there any reason why we can't get rid of the vfsmount reference in struct file?

Most file operations, don't involve namespace traversal at all: aside from fchdir(), and the *at() functions (all of which take file descriptors, not pointers to struct file) the only function of that vfsmount reference appears to be to prevent the superblock from going away.

Cheers
Trond

maintainer
#6 lore raw

Re: [patch 01/10] vfs: add path_create() and path_mknod()

Quoted 8 lines · Trond Myklebust
Quoted 6 lines · Al Viro

I disagree. First of all, clear separation between operations on _filesystem_, which should all be namespace-agnostic and things that depend on vfsmount is a Good Thing(tm). Think of that as of separation between server (superblock and everything related to it, starting with dentry tree) and clients; mixing those is a bloody bad idea.

Quoted 8 lines

Speaking of which: is there any reason why we can't get rid of the vfsmount reference in struct file?

Most file operations, don't involve namespace traversal at all: aside from fchdir(), and the *at() functions (all of which take file descriptors, not pointers to struct file) the only function of that vfsmount reference appears to be to prevent the superblock from going away.

Huh? Are you proposing to move that to descriptor table, of all things? Not to mention SCM_RIGHTS datagrams and hell knows what else...

#7 lore raw

Re: [patch 01/10] vfs: add path_create() and path_mknod()

Quoted 12 lines · Al Viro
Quoted 11 lines · Trond Myklebust
Quoted 6 lines · Al Viro

I disagree. First of all, clear separation between operations on _filesystem_, which should all be namespace-agnostic and things that depend on vfsmount is a Good Thing(tm). Think of that as of separation between server (superblock and everything related to it, starting with dentry tree) and clients; mixing those is a bloody bad idea.

Speaking of which: is there any reason why we can't get rid of the vfsmount reference in struct file?

Quoted 8 lines
Quoted 5 lines

Most file operations, don't involve namespace traversal at all: aside from fchdir(), and the *at() functions (all of which take file descriptors, not pointers to struct file) the only function of that vfsmount reference appears to be to prevent the superblock from going away.

Huh? Are you proposing to move that to descriptor table, of all things? Not to mention SCM_RIGHTS datagrams and hell knows what else...

I'm just suggesting splitting out the namespace-specific part of struct file into a separate structure that would be private to the VFS. Something like

struct file_descriptor {
	struct file *file;
	struct vfsmount *mnt;
	atomic_t refcount;
};

and then having the 'struct file' hold a reference to the superblock instead of holding a reference to the vfsmount.

Why would that be problematic for SCM_RIGHTS? We don't allow people to send arbitrary references to 'struct file' using SCM_RIGHTS now; they have to send descriptors.

maintainer
#8 lore raw

Re: [patch 01/10] vfs: add path_create() and path_mknod()

Quoted 10 lines · Trond Myklebust

I'm just suggesting splitting out the namespace-specific part of struct file into a separate structure that would be private to the VFS. Something like

struct file_descriptor {
	struct file *file;
	struct vfsmount *mnt;
	atomic_t refcount;
};
Quoted 6 lines

and then having the 'struct file' hold a reference to the superblock instead of holding a reference to the vfsmount.

Why would that be problematic for SCM_RIGHTS? We don't allow people to send arbitrary references to 'struct file' using SCM_RIGHTS now; they have to send descriptors.

HUH? Descriptor is a number. There is no struct file_descriptor, let alone refcounting for such. There is a table, indexed by number and containing references to struct file.

If you want to shove pointer to vfsmount in there (what for? to waste some memory and make SMP protection on access more interesting?), you could do that, but IMO it's too ugly to consider.

Anyway, what the hell for? It's more complex and buys you nothing useful.

maintainer
#9 lore raw

Re: [patch 01/10] vfs: add path_create() and path_mknod()

Quoted 2 lines · Al Viro

Anyway, what the hell for? It's more complex and buys you nothing useful.

BTW, what it gives you is an extra headache for scenarios like

task A:

	write(fd, ...)

task B: /* shares descriptor table with A */
close(fd)
task C: umount(<mountpoint of tree where fd is>)
...
task A: still writing

At the very least, you want "that thing is still busy" on normal umount - we are still in the middle of write(2) and hell knows how long it's going to last. So you need to play with refcount of vfsmount in a very nasty way, for all your pains. Again, what for?

#10 lore raw

Re: [patch 01/10] vfs: add path_create() and path_mknod()

Quoted 14 lines · Al Viro
Quoted 2 lines · Al Viro

Anyway, what the hell for? It's more complex and buys you nothing useful.

BTW, what it gives you is an extra headache for scenarios like

task A:

	write(fd, ...)

task B: /* shares descriptor table with A */
close(fd)
task C: umount(<mountpoint of tree where fd is>)
...
task A: still writing

Quoted 4 lines

At the very least, you want "that thing is still busy" on normal umount - we are still in the middle of write(2) and hell knows how long it's going to last. So you need to play with refcount of vfsmount in a very nasty way, for all your pains.

We already call fget_light()/fput_light() around the whole call to vfs_write(). Substituting a call to something which takes a reference to the new structure is trivial.

Once the write() syscall is done, and the file is closed, why should we refuse the umount request? There should be nothing more going on that depends on the namespace.

Quoted 1 line

Again, what for?

It allows you to get rid of the vfsmount 'argument' when opening a file, which again lowers the barrier for stacking filesystems.

As far as the filesystems themselves are concerned, the effect is to enforce your assertion that file operations should not depend on the namespace.

#11 lore raw

Re: [patch 01/10] vfs: add path_create() and path_mknod()

Quoted 2 lines · Trond Myklebust

On Thu, 2008-04-03 at 00:47 +0100, Al Viro wrote:

[...]

Quoted 8 lines
Quoted 1 line

Again, what for?

It allows you to get rid of the vfsmount 'argument' when opening a file, which again lowers the barrier for stacking filesystems.

As far as the filesystems themselves are concerned, the effect is to enforce your assertion that file operations should not depend on the namespace.

I'd be delighted the day I won't have to deal with vfsmounts AT ALL in any stacked f/s.

Erez.

maintainer
#12 lore raw

Re: [patch 01/10] vfs: add path_create() and path_mknod()

Quoted 9 lines · Erez Zadok
Quoted 2 lines · Trond Myklebust

On Thu, 2008-04-03 at 00:47 +0100, Al Viro wrote:

[...]

Quoted 5 lines
Quoted 1 line

Again, what for?

It allows you to get rid of the vfsmount 'argument' when opening a file, which again lowers the barrier for stacking filesystems.

Quoted 6 lines
Quoted 3 lines

As far as the filesystems themselves are concerned, the effect is to enforce your assertion that file operations should not depend on the namespace.

I'd be delighted the day I won't have to deal with vfsmounts AT ALL in any stacked f/s.

vfsmounts or the fact that any fs may be mounted in many places?

#13 lore raw

Re: [patch 01/10] vfs: add path_create() and path_mknod()

Quoted 10 lines · Al Viro
Quoted 9 lines · Erez Zadok
Quoted 2 lines · Trond Myklebust

On Thu, 2008-04-03 at 00:47 +0100, Al Viro wrote:

[...]

Quoted 5 lines
Quoted 1 line

Again, what for?

It allows you to get rid of the vfsmount 'argument' when opening a file, which again lowers the barrier for stacking filesystems.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

As far as the filesystems themselves are concerned, the effect is to enforce your assertion that file operations should not depend on the namespace.

I'd be delighted the day I won't have to deal with vfsmounts AT ALL in any stacked f/s.

vfsmounts or the fact that any fs may be mounted in many places?

I think the former. The fact that any f/s can be mounted in many places, should be fine for a stacked f/s (topology changes as we discussed before are a different problem).

Since you've hinted of major vfs changes post-25, here's my take.

Right now I (and to a similar extent ecryptfs too) needs to keep track of vfsmounts for various reasons:

- to grab a reference so the lower filesystems/mounts won't disappear on me
- to pass it to dentry_open (opening the lower file)
- some fs ops pass a vfsmount (umount_begin, show_options)
- some fs ops or vfs helpers require a nameidata or struct path which embed a vfsmount inside
- sometimes it's ok to pass NULL for those things, sometimes it's not ok

Often it also appears that a vfsmount and a dentry tuple are needed together, hence struct path. So we recently started using struct path in a few places, and using path_get/put. The need to deal with <dentry,vfsmount> pairs by hand can lead to interesting bugs, such as the recent ecryptfs bugfix which had to swap the order of a dput() and mntput() sequence.

The fact that several vfs ops and helpers take different namespace-related structures (dentry, nameidata, vfsmount, path), is confusing and hard to track. Is there a way to reduce the number of those structures to 1-2 at most?

For a stackable f/s, it's important the the VFS ops *and* the VFS helpers (vfs_*, path_*, dentry_open, etc.) use the same namespace structures: then there's more symmetry b/t the objects passed to a stacked f/s, and the objects it has to pass to VFS helpers (and FWIW, every such object should ideally leave a "void *private" field for f/s specific extensibility).

In the longer run, is there a way that a stackable f/s could traverse the namespace below it w/o knowing or caring how they are composed (assorted r-w and r-o bind mounts and such)?

Cheers,
Erez.

maintainer
#14 lore raw

Re: [patch 01/10] vfs: add path_create() and path_mknod()

Quoted 6 lines · Erez Zadok

Since you've hinted of major vfs changes post-25, here's my take.

Right now I (and to a similar extent ecryptfs too) needs to keep track of vfsmounts for various reasons:

- to grab a reference so the lower filesystems/mounts won't disappear on me

Umm... Strictly speaking that's not true; you can grab an active reference to superblock and then superblock will stay. vfsmount is usually more convenient, but that's it.

Quoted 2 lines

- to pass it to dentry_open (opening the lower file)
- some fs ops pass a vfsmount (umount_begin, show_options)

Thank Trond for the former, BTW ;-) Both methods will be back to sanity; for umount_begin() the only obstacle is cifs mess, where we do unconditional (and bogus) work at each umount(2). With that fixed, we'll be back to calling it only for forced umount and passing it only superblock. ->show_options() can and should revert immediately after 2.6.25; thanks for pointing that one out.

Quoted 2 lines

- some fs ops or vfs helpers require a nameidata or struct path which embed a vfsmount inside

*ONLY* ->follow_link(). Which has to, by definition... The rest will be gone by .26.

Quoted 1 line

- sometimes it's ok to pass NULL for those things, sometimes it's not ok

See above. This crap will be gone. For ->follow_link() nobody is allowed to pass NULL as nameidata, period.

Quoted 3 lines

In the longer run, is there a way that a stackable f/s could traverse the namespace below it w/o knowing or caring how they are composed (assorted r-w and r-o bind mounts and such)?

Eh? Explain, please...

#15 lore raw

Re: [patch 01/10] vfs: add path_create() and path_mknod()

Quoted 11 lines · Al Viro
Quoted 6 lines · Erez Zadok

Since you've hinted of major vfs changes post-25, here's my take.

Right now I (and to a similar extent ecryptfs too) needs to keep track of vfsmounts for various reasons:

- to grab a reference so the lower filesystems/mounts won't disappear on me

Umm... Strictly speaking that's not true; you can grab an active reference to superblock and then superblock will stay. vfsmount is usually more convenient, but that's it.

Yes, I do grab both vfsmount and superblock refs. I found out that grabbing vfsmount refs wasn't enough to prevent "umount -l" from detaching the f/s on which I'm stacked on. So now at mount time (or branch management time), I grab those super-refs, as I have them after a successful path_lookup. And, since I keep a list of the branches I'm stacked on, I know precisely which superblocks' references I need to release when unionfs is unmounted.

But what do I do if I descend into another lower superblock while looking up a lower directory? How do keep track of the superblock refs now? I'd basically have to memorize the hierarchy of mounted superblocks somehow? How would I know when to release those refs? (hmm, maybe I can rely on d_mounted or the like?)

Quoted 4 lines
Quoted 1 line

- sometimes it's ok to pass NULL for those things, sometimes it's not ok

See above. This crap will be gone. For ->follow_link() nobody is allowed to pass NULL as nameidata, period.

There's been talk in the past of splitting nameidata into intent structure and all the rest. Is that also part of your plan for 26? Intents are indeed very useful in ->lookup; the rest I can do without.

Quoted 5 lines
Quoted 3 lines

In the longer run, is there a way that a stackable f/s could traverse the namespace below it w/o knowing or caring how they are composed (assorted r-w and r-o bind mounts and such)?

Eh? Explain, please...

So, in ->lookup, I essentially have to do a lookup on the corresponding lower objects. I get a nameidata, I have to create my own nameidata, and pass it to lower ->lookup calls. Can the API be changed so I wouldn't need to get or pass a nameidata? (maybe just intent struct).

Also in ->lookup I call lookup_one_len, which is nice b/c it doesn't involve vfsmounts or nameidata. But lookup_one_len doesn't use the same prototype as ->lookup, so there's some asymmetry here (I often like to see that a stacked op passed to the lower f/s uses the same API as what was passed to the op in the first place).

Ironically, since lookup_one_len doesn't involve vfsmounts, but I need them for other reasons, I'm forced to live with NULL vfsmounts in some cases, or refer to the lower vfsmounts I already had for my root dentry (that makes transparently descending into a different vfsmount challenging, if not inconsistent).

For many fs ops, the prototype of the ->op has a perfectly symmetric vfs_op() helper. For example, ->mkdir(inode,dentry,mode) and vfs_mkdir(inode,dentry,mode). But nothing as simple for lookup, one of the most complex ops in unionfs.

BTW, to be honest, some of extra complications in unionfs (and unionfs_lookup) have to do with .wh.* files (whiteouts). But I hope that'll get simpler once we have native WH support in the all the major filesystems.

Cheers,
Erez.

maintainer
#16 lore raw

Re: [patch 01/10] vfs: add path_create() and path_mknod()

Quoted 6 lines · Erez Zadok

Yes, I do grab both vfsmount and superblock refs. I found out that grabbing vfsmount refs wasn't enough to prevent "umount -l" from detaching the f/s on which I'm stacked on. So now at mount time (or branch management time), I grab those super-refs, as I have them after a successful path_lookup. And, since I keep a list of the branches I'm stacked on, I know precisely which superblocks' references I need to release when unionfs is unmounted.

How the devil would holding a superblock prevent umount -l?

Quoted 6 lines

But what do I do if I descend into another lower superblock while looking up a lower directory? How do keep track of the superblock refs now? I'd basically have to memorize the hierarchy of mounted superblocks somehow? How would I know when to release those refs? (hmm, maybe I can rely on d_mounted or the like?)

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

- sometimes it's ok to pass NULL for those things, sometimes it's not ok

See above. This crap will be gone. For ->follow_link() nobody is allowed to pass NULL as nameidata, period.

There's been talk in the past of splitting nameidata into intent structure and all the rest. Is that also part of your plan for 26? Intents are indeed very useful in ->lookup; the rest I can do without.

intents will die. There'll be a method for final step of lookup + open, but that's it (and it'll take preallocated struct file as one of the arguments). Please, explain what you want to do with intents, because as far as I'm concerned these had been a mistake for a lot of reasons.

Quoted 5 lines

Ironically, since lookup_one_len doesn't involve vfsmounts, but I need them for other reasons, I'm forced to live with NULL vfsmounts in some cases, or refer to the lower vfsmounts I already had for my root dentry (that makes transparently descending into a different vfsmount challenging, if not inconsistent).

Details, please. If you just want a snapshot of vfsmount tree, then by all means take a bloody snapshot. collect_mounts() is there for purpose. If you want mount/umount/etc. changes affect what you have, then I really would like to see the semantics you want. Some variation on shared-subtree might be close to that...

#17 lore raw

Re: [patch 01/10] vfs: add path_create() and path_mknod()

Quoted 9 lines · Al Viro
Quoted 6 lines · Erez Zadok

Yes, I do grab both vfsmount and superblock refs. I found out that grabbing vfsmount refs wasn't enough to prevent "umount -l" from detaching the f/s on which I'm stacked on. So now at mount time (or branch management time), I grab those super-refs, as I have them after a successful path_lookup. And, since I keep a list of the branches I'm stacked on, I know precisely which superblocks' references I need to release when unionfs is unmounted.

How the devil would holding a superblock prevent umount -l?

It doesn't prevent umount -l; but it prevents the lower superblock from being kfree()d if there were no references left to it, so I don't try to accessed freed memory (and get 6b6b6b6b oopses :-)

Quoted 11 lines
Quoted 11 lines

But what do I do if I descend into another lower superblock while looking up a lower directory? How do keep track of the superblock refs now? I'd basically have to memorize the hierarchy of mounted superblocks somehow? How would I know when to release those refs? (hmm, maybe I can rely on d_mounted or the like?)

Quoted 4 lines
Quoted 1 line

- sometimes it's ok to pass NULL for those things, sometimes it's not ok

See above. This crap will be gone. For ->follow_link() nobody is allowed to pass NULL as nameidata, period.

Quoted 7 lines
Quoted 3 lines

There's been talk in the past of splitting nameidata into intent structure and all the rest. Is that also part of your plan for 26? Intents are indeed very useful in ->lookup; the rest I can do without.

intents will die. There'll be a method for final step of lookup + open, but that's it (and it'll take preallocated struct file as one of the arguments).

How much of nameidata will still need to be passed to f/s ops? How many vfs helpers will still require a nameidata? Hopefully as few as possible.

Is this preallocated struct file a replacement to the nd->intent.open.file that could have returned an allocated struct file? (I could never really tell what's the right thing to do with that field.)

Quoted 2 lines

Please, explain what you want to do with intents, because as far as I'm concerned these had been a mistake for a lot of reasons.

I said above "intents are indeed very useful" only because they were:

1. required to be passed to vfs_* methods, which made it hard if the ->op I was in didn't have an intent (I have to create temp intents for those).

2. apparently heavily used in nfs4, to a point where if I stacked on nfs4 and didn't pass a correctly formatted nameidata, I'd get an oops deep inside nfs4 code. (I think some of those nfs4 requirements went away, not sure what's left.).

Quoted 11 lines
Quoted 5 lines

Ironically, since lookup_one_len doesn't involve vfsmounts, but I need them for other reasons, I'm forced to live with NULL vfsmounts in some cases, or refer to the lower vfsmounts I already had for my root dentry (that makes transparently descending into a different vfsmount challenging, if not inconsistent).

Details, please. If you just want a snapshot of vfsmount tree, then by all means take a bloody snapshot. collect_mounts() is there for purpose. If you want mount/umount/etc. changes affect what you have, then I really would like to see the semantics you want. Some variation on shared-subtree might be close to that...

I'll have to take a closer look at vfsmount tree snapshotting and collect_mounts() before I can say how useful they'd be. But if you recall my questions at LSF, I asked whether it was possible for me to create a readonly directory tree (e.g., r-o bind mounts) or some form of an immutable namespace that no one can modify underneath me. You said that r-o bind mounts were not intended for that.

Right now I'm allowing users to modify lower branches, with all the pros and cons that it has. But even if I wanted to prevent users from touching any lower files below a certain directory, while allowing only unionfs to modify those files, it doesn't appear that there's something available I could use.

I have two other questions/requests:

1. The less I have to use or know about vfsmounts and nameidata/intents, the better. But whatever API changes you make, please consider the symmetry between the f/s ->op I'm called with, and the vfs helpers I might use in the ->op to pass through to the lower f/s. Ideally the prototypes of the ->op and vfs helpers be identical, so I don't have to work too hard to locate the lower objects, or worse, having to make them up temporarily.

2. You mentioned that all this work is scheduled for 26. 25 is nearing release. Do you have code already that I can experiment with? A preview of things to come? Maybe an example or two of how a filesystem (stackable, nfsd, or otherwise) should lookup and open arbitrary files? What you mentioned in this discussion thread sounds promising and exciting, but may take me a while to apply to my tree (longer than the usual merge window, which reportedly will shrink even further, now that we have linux-next :-). So the more lead time, the better, please.

Thanks,
Erez.

#18 lore raw

Re: [patch 01/10] vfs: add path_create() and path_mknod()

Quoted 9 lines
Quoted 8 lines · Al Viro
Quoted 6 lines · Erez Zadok

Yes, I do grab both vfsmount and superblock refs. I found out that grabbing vfsmount refs wasn't enough to prevent "umount -l" from detaching the f/s on which I'm stacked on. So now at mount time (or branch management time), I grab those super-refs, as I have them after a successful path_lookup. And, since I keep a list of the branches I'm stacked on, I know precisely which superblocks' references I need to release when unionfs is unmounted.

Quoted 5 lines
Quoted 1 line

How the devil would holding a superblock prevent umount -l?

It doesn't prevent umount -l; but it prevents the lower superblock from being kfree()d if there were no references left to it, so I don't try to accessed freed memory (and get 6b6b6b6b oopses :-)

Having a ref on the vfsmount should be enough, regardless of whether the mount is attached or not. There's nothing special about a detached mount, that anything outside namespace.c should care about.

Miklos

maintainer
#19 lore raw

Re: [patch 01/10] vfs: add path_create() and path_mknod()

In reply to Trond Myklebust
Quoted 8 lines · Trond Myklebust
Quoted 4 lines

At the very least, you want "that thing is still busy" on normal umount - we are still in the middle of write(2) and hell knows how long it's going to last. So you need to play with refcount of vfsmount in a very nasty way, for all your pains.

We already call fget_light()/fput_light() around the whole call to vfs_write(). Substituting a call to something which takes a reference to the new structure is trivial.

Huh? So you want an extra layer of indirection? descriptor table -> that one -> struct file? And refcounting these puppies?

Quoted 2 lines

It allows you to get rid of the vfsmount 'argument' when opening a file, which again lowers the barrier for stacking filesystems.

I don't see how that would fix the fundamental breakage in those, but anyway... (and yes, ecryptfs has interesting issues, but the look of it).

Quoted 3 lines

As far as the filesystems themselves are concerned, the effect is to enforce your assertion that file operations should not depend on the namespace.

I really doubt that it's worth doing in this area... "Don't use ->f_vfsmnt in fs code" is easily enforced and struct file is really used outside of filesystem code in fs-independent ways.

IOW, I don't believe that it's worth introducing a new layer between descriptor table and files. BTW, that'll complicate union-mount handling (real ones, not unionfs under different name) and quite a few other things...

#20 lore raw

Re: [patch 01/10] vfs: add path_create() and path_mknod()

In reply to Al Viro
Quoted 12 lines
Quoted 11 lines

It is an interface with at least 3 callers at the moment: syscalls, nfsd and ecryptfs and unionfs in the future. It is an interface because all external accesses to the filesystem *are* done through the namespace and not directly on filesystem internals.

Such direct access would be conceivable, but I don't think we should base the design on theoretically possible uses. If those uses appear, we can change the interface to fit that.

This "move everything to callers" thing is wrong because it just results in bloat and bugs, none of which is desirable in the kernel.

Quoted 6 lines

I disagree. First of all, clear separation between operations on _filesystem_, which should all be namespace-agnostic and things that depend on vfsmount is a Good Thing(tm). Think of that as of separation between server (superblock and everything related to it, starting with dentry tree) and clients; mixing those is a bloody bad idea.

Separation: yes. Exporting it as an interface: I'm not convinced.

If we ever want to rely on r/o mounts doing what they're supposed to do, ie. not allowing modifications to the filesystem, we'd better expose an interface that does just that. Relying on callers won't work (as demonstrated in practice).

Wasn't one of the points behind the r/o bind patchset is to make remounting read-only race free? If all mounts of a super block are read-only, then the super block itself can be safely marked read-only.

If we allow external entities such as filesystem stacks to bypass this rule and access the filesystem directly, then the whole thing is basically worthless.

Quoted 2 lines

What's more, I'm not at all convinced that for nfsd it's the right set of checks, to start with.

Well, what are the right checks then? From the r/o consistency pov, these are the right checks. From NFS' pov there may be places where we might want to take a write-ref spanning several operations. But that's OK, the checks are cheap (if not, we have other problems).

Quoted 1 line

The same goes for future users.

There's already one place in that interface where users need to know the vfsmount which they are operating on: opening a file (and hence subsequent I/O on that file). Besides my other points, what is the sense in an interface, half of which operates on the namespace, and the other half on the filesystem?

Quoted 7 lines

As for ecryptfs, looking at their "lower_mnt" thing... I'd say that it's a nonsense. For one thing, duplicating a reference into ever dentry out there (and it's simply duplicated) makes no sense whatsoever. For another... I'm not at all sure that remount of the underlying vfsmount r/o *should* take that sucker read-only. And if it should, it's clearly an action that should have a visible effect on superblock flags of ecryptfs.

If it wants to handle that case nicely, it can monitor /proc/mounts and reflect it in it's superblock flags. And it can take a write-ref on the underlying fs if it has outstanding dirtyness. But we should not _rely_ on ecryptfs to ensure that it's never writing to a read-only mount.

Miklos

#21 lore raw

Re: [patch 01/10] vfs: add path_create() and path_mknod()

In message <[email protected]>, Miklos Szeredi writes:
[...]

Quoted 5 lines

If it wants to handle that case nicely, it can monitor /proc/mounts and reflect it in it's superblock flags. And it can take a write-ref on the underlying fs if it has outstanding dirtyness. But we should not _rely_ on ecryptfs to ensure that it's never writing to a read-only mount.

I'm all for reducing the need for stackable f/s such as ecryptfs from having to remember to manage the write counts. A stackable filesystem has to emulate some parts of the VFS when it calls into a lower f/s: in that case, having useful VFS helpers that do as much of the work as possible, saves a lot of hassle.

Quoted 1 line

Miklos

Cheers,
Erez.

#22 lore raw

Re: [patch 01/10] vfs: add path_create() and path_mknod()

In reply to Al Viro
Quoted 12 lines · Al Viro
Quoted 11 lines · Miklos Szeredi

From: Miklos Szeredi <[email protected]>

R/o bind mounts require operations which modify the filesystem to be wrapped in mnt_want_write()/mnt_drop_write(). Create helpers which do this, so callers won't need to bother, and more importantly, cannot forget! Call these path_*, analogous to vfs_*. Where there are no callers of vfs_* left, make them static.

This series is a cleanup, as well as fixing several places (mostly in nfsd) where mnt_{want,drop}_write() were missing.

Quoted 8 lines
Quoted 4 lines

It will also help with merging You Know What(*) security module, which needs to know the path within the namespace, and not just within the filesystem. These helpers will allow the security hooks to be in a common place, and need not be repeated in all callers.

Rot.

Places in nfsd must be fixed, LSM hooks *moved* *to* *callers*.

Please don't move the existing security_inode hooks - they are at exactly the right location for SELinux and likely other security modules too - after the DAC checks (so no noise in the audit logs from accesses that would be denied by DAC anyway), and right before the inode method is invoked (so easy to see that the permission check is always invoked before access). Moving the existing hooks will break the first property unless the DAC checks are also moved and will make it harder to check and maintain the second property.

Quoted 18 lines

And
really, by that point I absolutely do not give a damn for these clowns. "Help with merging" implies that they can't be arsed to do _anything_ with their code. Just as they could not be arsed to react to any comments (including civil ones) in any manner except "wait for a month and repost without changes". Sod them.

And no, "make static where all (two of) current callers have vfsmount" is non-starter. path_...() is (at most) a convenience helper, not a fundamental interface - simply because new callers should not be forced into inventing a fake vfsmount just to use that.

I'll look into missing mnt_..._write() in nfsd and fix that. The rest... Sorry.
--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 2 lines
Stephen Smalley
National Security Agency
#23 lore raw

Re: [patch 01/10] vfs: add path_create() and path_mknod()

In reply to Miklos Szeredi
Quoted 2 lines · Miklos Szeredi

This series is a cleanup, as well as fixing several places (mostly in nfsd) where mnt_{want,drop}_write() were missing.

Could you elaborate on these?

-- Dave

#24 lore raw

Re: [patch 01/10] vfs: add path_create() and path_mknod()

In reply to Miklos Szeredi

I think these definitely all clean the code up. They might add a couple of lines, net, but they certainly make it easier to read. As you noted, they also make it much harder to screw up the locking, which is even better. Just looking at it from an aesthetic point of view, it looks good to have these helpers around.

-- Dave

#25 lore raw

PATCH 2/10 vfs: add path_mkdir()

In reply to Miklos Szeredi

From: Miklos Szeredi <[email protected]>

Introduce path_mkdir(). Unexport vfs_mkdir() to modules.

The only internal user of vfs_mkdir() remaining is cgroup_clone().

---
 fs/ecryptfs/inode.c   |   15 ++++++++-------
 fs/namei.c            |   21 ++++++++++++++-------
 fs/nfsd/nfs4recover.c |    6 +-----
 fs/nfsd/vfs.c         |    2 +-
 include/linux/fs.h    |    1 +
 5 files changed, 25 insertions(+), 20 deletions(-)
fs/ecryptfs/inode.c +8 -7
Index: vfs-2.6/fs/ecryptfs/inode.c
===================================================================
@@ -487,21 +487,22 @@ static int ecryptfs_mkdir(struct inode *
 {
 	int rc;
 	struct dentry *lower_dentry;
-	struct dentry *lower_dir_dentry;
+	struct path lower_dir;
 
 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
-	lower_dir_dentry = lock_parent(lower_dentry);
-	rc = vfs_mkdir(lower_dir_dentry->d_inode, lower_dentry, mode);
+	lower_dir.mnt = ecryptfs_dentry_to_lower_mnt(dentry);
+	lower_dir.dentry = lock_parent(lower_dentry);
+	rc = path_mkdir(&lower_dir, lower_dentry, mode);
 	if (rc || !lower_dentry->d_inode)
 		goto out;
 	rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
 	if (rc)
 		goto out;
-	fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
-	fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
-	dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
+	fsstack_copy_attr_times(dir, lower_dir.dentry->d_inode);
+	fsstack_copy_inode_size(dir, lower_dir.dentry->d_inode);
+	dir->i_nlink = lower_dir.dentry->d_inode->i_nlink;
 out:
-	unlock_dir(lower_dir_dentry);
+	unlock_dir(lower_dir.dentry);
 	if (!dentry->d_inode)
 		d_drop(dentry);
 	return rc;
fs/namei.c +14 -7
Index: vfs-2.6/fs/namei.c
===================================================================
@@ -2144,6 +2144,19 @@ int vfs_mkdir(struct inode *dir, struct 
 	return error;
 }
 
+int path_mkdir(struct path *dir_path, struct dentry *dentry, int mode)
+{
+	int error = mnt_want_write(dir_path->mnt);
+
+	if (!error) {
+		error = vfs_mkdir(dir_path->dentry->d_inode, dentry, mode);
+		mnt_drop_write(dir_path->mnt);
+	}
+
+	return error;
+}
+EXPORT_SYMBOL(path_mkdir);
+
 asmlinkage long sys_mkdirat(int dfd, const char __user *pathname, int mode)
 {
 	int error = 0;
@@ -2166,12 +2179,7 @@ asmlinkage long sys_mkdirat(int dfd, con
 
 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
 		mode &= ~current->fs->umask;
-	error = mnt_want_write(nd.path.mnt);
-	if (error)
-		goto out_dput;
-	error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
-	mnt_drop_write(nd.path.mnt);
-out_dput:
+	error = path_mkdir(&nd.path, dentry, mode);
 	dput(dentry);
 out_unlock:
 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
@@ -2975,7 +2983,6 @@ EXPORT_SYMBOL(file_permission);
 EXPORT_SYMBOL(unlock_rename);
 EXPORT_SYMBOL(vfs_follow_link);
 EXPORT_SYMBOL(vfs_link);
-EXPORT_SYMBOL(vfs_mkdir);
 EXPORT_SYMBOL(generic_permission);
 EXPORT_SYMBOL(vfs_readlink);
 EXPORT_SYMBOL(vfs_rename);
fs/nfsd/nfs4recover.c +1 -5
Index: vfs-2.6/fs/nfsd/nfs4recover.c
===================================================================
@@ -155,11 +155,7 @@ 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.path.mnt);
-	if (status)
-		goto out_put;
-	status = vfs_mkdir(rec_dir.path.dentry->d_inode, dentry, S_IRWXU);
-	mnt_drop_write(rec_dir.path.mnt);
+	status = path_mkdir(&rec_dir.path, dentry, S_IRWXU);
 out_put:
 	dput(dentry);
 out_unlock:
fs/nfsd/vfs.c +1 -1
Index: vfs-2.6/fs/nfsd/vfs.c
===================================================================
@@ -1258,7 +1258,7 @@ nfsd_create(struct svc_rqst *rqstp, stru
 		host_err = path_create(&dir_path, dchild, iap->ia_mode, NULL);
 		break;
 	case S_IFDIR:
-		host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
+		host_err = path_mkdir(&dir_path, dchild, iap->ia_mode);
 		break;
 	case S_IFCHR:
 	case S_IFBLK:
include/linux/fs.h +1 -0
Index: vfs-2.6/include/linux/fs.h
===================================================================
@@ -1125,6 +1125,7 @@ extern void unlock_super(struct super_bl
 extern int vfs_permission(struct nameidata *, int);
 extern int path_create(struct path *, struct dentry *, int, struct nameidata *);
 extern int vfs_mkdir(struct inode *, struct dentry *, int);
+extern int path_mkdir(struct path *, struct dentry *, int);
 extern int path_mknod(struct path *, struct dentry *, int, dev_t);
 extern int vfs_symlink(struct inode *, struct dentry *, const char *, int);
 extern int vfs_link(struct dentry *, struct inode *, struct dentry *);

--

#26 lore raw

Re: [patch 02/10] vfs: add path_mkdir()

Quoted 7 lines · Miklos Szeredi

From: Miklos Szeredi <[email protected]>

Introduce path_mkdir(). Unexport vfs_mkdir() to modules.

The only internal user of vfs_mkdir() remaining is cgroup_clone().

+EXPORT_SYMBOL(path_mkdir);

Shouldn't all new exported symbols use EXPORT_SYMBOL_GPL?

Erez.

#27 lore raw

PATCH 3/10 vfs: add path_rmdir()

In reply to Miklos Szeredi

From: Miklos Szeredi <[email protected]>

Introduce path_rmdir().

The only user of vfs_rmdir() remaining is reiserfs_delete_xattrs().

---
 fs/ecryptfs/inode.c   |   13 +++++++------
 fs/namei.c            |   20 ++++++++++++++------
 fs/nfsd/nfs4recover.c |    3 ++-
 fs/nfsd/vfs.c         |    5 ++++-
 include/linux/fs.h    |    1 +
 5 files changed, 28 insertions(+), 14 deletions(-)
fs/namei.c +14 -6
Index: vfs-2.6/fs/namei.c
===================================================================
@@ -2255,6 +2255,19 @@ int vfs_rmdir(struct inode *dir, struct 
 	return error;
 }
 
+int path_rmdir(struct path *dir_path, struct dentry *dentry)
+{
+	int error = mnt_want_write(dir_path->mnt);
+
+	if (!error) {
+		error = vfs_rmdir(dir_path->dentry->d_inode, dentry);
+		mnt_drop_write(dir_path->mnt);
+	}
+
+	return error;
+}
+EXPORT_SYMBOL(path_rmdir);
+
 static long do_rmdir(int dfd, const char __user *pathname)
 {
 	int error = 0;
@@ -2286,12 +2299,7 @@ static long do_rmdir(int dfd, const char
 	error = PTR_ERR(dentry);
 	if (IS_ERR(dentry))
 		goto exit2;
-	error = mnt_want_write(nd.path.mnt);
-	if (error)
-		goto exit3;
-	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
-	mnt_drop_write(nd.path.mnt);
-exit3:
+	error = path_rmdir(&nd.path, dentry);
 	dput(dentry);
 exit2:
 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
fs/ecryptfs/inode.c +7 -6
Index: vfs-2.6/fs/ecryptfs/inode.c
===================================================================
@@ -511,20 +511,21 @@ out:
 static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry)
 {
 	struct dentry *lower_dentry;
-	struct dentry *lower_dir_dentry;
+	struct path lower_dir;
 	int rc;
 
 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
 	dget(dentry);
-	lower_dir_dentry = lock_parent(lower_dentry);
+	lower_dir.mnt = ecryptfs_dentry_to_lower_mnt(dentry);
+	lower_dir.dentry = lock_parent(lower_dentry);
 	dget(lower_dentry);
-	rc = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry);
+	rc = path_rmdir(&lower_dir, lower_dentry);
 	dput(lower_dentry);
 	if (!rc)
 		d_delete(lower_dentry);
-	fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
-	dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
-	unlock_dir(lower_dir_dentry);
+	fsstack_copy_attr_times(dir, lower_dir.dentry->d_inode);
+	dir->i_nlink = lower_dir.dentry->d_inode->i_nlink;
+	unlock_dir(lower_dir.dentry);
 	if (!rc)
 		d_drop(dentry);
 	dput(dentry);
fs/nfsd/nfs4recover.c +2 -1
Index: vfs-2.6/fs/nfsd/nfs4recover.c
===================================================================
@@ -267,6 +267,7 @@ nfsd4_remove_clid_file(struct dentry *di
 static int
 nfsd4_clear_clid_dir(struct dentry *dir, struct dentry *dentry)
 {
+	struct path dir_path = { .dentry = dir, .mnt = rec_dir.path.mnt };
 	int status;
 
 	/* For now this directory should already be empty, but we empty it of
@@ -274,7 +275,7 @@ nfsd4_clear_clid_dir(struct dentry *dir,
 	 * a kernel from the future.... */
 	nfsd4_list_rec_dir(dentry, nfsd4_remove_clid_file);
 	mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
-	status = vfs_rmdir(dir->d_inode, dentry);
+	status = path_rmdir(&dir_path, dentry);
 	mutex_unlock(&dir->d_inode->i_mutex);
 	return status;
 }
fs/nfsd/vfs.c +4 -1
Index: vfs-2.6/fs/nfsd/vfs.c
===================================================================
@@ -1732,6 +1732,7 @@ __be32
 nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
 				char *fname, int flen)
 {
+	struct path 	dir_path;
 	struct dentry	*dentry, *rdentry;
 	struct inode	*dirp;
 	__be32		err;
@@ -1762,6 +1763,8 @@ nfsd_unlink(struct svc_rqst *rqstp, stru
 	if (!type)
 		type = rdentry->d_inode->i_mode & S_IFMT;
 
+	dir_path.mnt = fhp->fh_export->ex_path.mnt;
+	dir_path.dentry = dentry;
 	if (type != S_IFDIR) { /* It's UNLINK */
 #ifdef MSNFS
 		if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
@@ -1771,7 +1774,7 @@ nfsd_unlink(struct svc_rqst *rqstp, stru
 #endif
 		host_err = vfs_unlink(dirp, rdentry);
 	} else { /* It's RMDIR */
-		host_err = vfs_rmdir(dirp, rdentry);
+		host_err = path_rmdir(&dir_path, rdentry);
 	}
 
 	dput(rdentry);
include/linux/fs.h +1 -0
Index: vfs-2.6/include/linux/fs.h
===================================================================
@@ -1130,6 +1130,7 @@ extern int path_mknod(struct path *, str
 extern int vfs_symlink(struct inode *, struct dentry *, const char *, int);
 extern int vfs_link(struct dentry *, struct inode *, struct dentry *);
 extern int vfs_rmdir(struct inode *, struct dentry *);
+extern int path_rmdir(struct path *, struct dentry *);
 extern int vfs_unlink(struct inode *, struct dentry *);
 extern int vfs_rename(struct inode *, struct dentry *, struct inode *, struct dentry *);
 

--

#28 lore raw

PATCH 4/10 vfs: add path_unlink()

In reply to Miklos Szeredi

From: Miklos Szeredi <[email protected]>

Introduce path_unlink(). Make vfs_unlink() static.

---
 fs/ecryptfs/inode.c   |   17 ++++++-----------
 fs/namei.c            |   22 +++++++++++++++-------
 fs/nfsd/nfs4recover.c |    3 ++-
 fs/nfsd/vfs.c         |    4 +---
 include/linux/fs.h    |    2 +-
 ipc/mqueue.c          |   10 +++++-----
 6 files changed, 30 insertions(+), 28 deletions(-)
fs/ecryptfs/inode.c +6 -11
Index: vfs-2.6/fs/ecryptfs/inode.c
===================================================================
@@ -42,12 +42,6 @@ static struct dentry *lock_parent(struct
 	return dir;
 }
 
-static void unlock_parent(struct dentry *dentry)
-{
-	mutex_unlock(&(dentry->d_parent->d_inode->i_mutex));
-	dput(dentry->d_parent);
-}
-
 static void unlock_dir(struct dentry *dir)
 {
 	mutex_unlock(&dir->d_inode->i_mutex);
@@ -425,21 +419,22 @@ static int ecryptfs_unlink(struct inode 
 {
 	int rc = 0;
 	struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
-	struct inode *lower_dir_inode = ecryptfs_inode_to_lower(dir);
+	struct path lower_dir;
 
-	lock_parent(lower_dentry);
-	rc = vfs_unlink(lower_dir_inode, lower_dentry);
+	lower_dir.mnt = ecryptfs_dentry_to_lower_mnt(dentry);
+	lower_dir.dentry = lock_parent(lower_dentry);
+	rc = path_unlink(&lower_dir, lower_dentry);
 	if (rc) {
 		printk(KERN_ERR "Error in vfs_unlink; rc = [%d]\n", rc);
 		goto out_unlock;
 	}
-	fsstack_copy_attr_times(dir, lower_dir_inode);
+	fsstack_copy_attr_times(dir, lower_dir.dentry->d_inode);
 	dentry->d_inode->i_nlink =
 		ecryptfs_inode_to_lower(dentry->d_inode)->i_nlink;
 	dentry->d_inode->i_ctime = dir->i_ctime;
 	d_drop(dentry);
 out_unlock:
-	unlock_parent(lower_dentry);
+	unlock_dir(lower_dir.dentry);
 	return rc;
 }
 
fs/namei.c +15 -7
Index: vfs-2.6/fs/namei.c
===================================================================
@@ -2315,7 +2315,7 @@ asmlinkage long sys_rmdir(const char __u
 	return do_rmdir(AT_FDCWD, pathname);
 }
 
-int vfs_unlink(struct inode *dir, struct dentry *dentry)
+static int vfs_unlink(struct inode *dir, struct dentry *dentry)
 {
 	int error = may_delete(dir, dentry, 0);
 
@@ -2346,6 +2346,19 @@ int vfs_unlink(struct inode *dir, struct
 	return error;
 }
 
+int path_unlink(struct path *dir_path, struct dentry *dentry)
+{
+	int error = mnt_want_write(dir_path->mnt);
+
+	if (!error) {
+		error = vfs_unlink(dir_path->dentry->d_inode, dentry);
+		mnt_drop_write(dir_path->mnt);
+	}
+
+	return error;
+}
+EXPORT_SYMBOL(path_unlink);
+
 /*
  * Make sure that the actual truncation of the file will occur outside its
  * directory's i_mutex.  Truncate can take a long time if there is a lot of
@@ -2380,11 +2393,7 @@ static long do_unlinkat(int dfd, const c
 		inode = dentry->d_inode;
 		if (inode)
 			atomic_inc(&inode->i_count);
-		error = mnt_want_write(nd.path.mnt);
-		if (error)
-			goto exit2;
-		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
-		mnt_drop_write(nd.path.mnt);
+		error = path_unlink(&nd.path, dentry);
 	exit2:
 		dput(dentry);
 	}
@@ -2996,6 +3005,5 @@ EXPORT_SYMBOL(vfs_readlink);
 EXPORT_SYMBOL(vfs_rename);
 EXPORT_SYMBOL(vfs_rmdir);
 EXPORT_SYMBOL(vfs_symlink);
-EXPORT_SYMBOL(vfs_unlink);
 EXPORT_SYMBOL(dentry_unhash);
 EXPORT_SYMBOL(generic_readlink);
fs/nfsd/nfs4recover.c +2 -1
Index: vfs-2.6/fs/nfsd/nfs4recover.c
===================================================================
@@ -252,6 +252,7 @@ out:
 static int
 nfsd4_remove_clid_file(struct dentry *dir, struct dentry *dentry)
 {
+	struct path dir_path = { .dentry = dir, .mnt = rec_dir.path.mnt };
 	int status;
 
 	if (!S_ISREG(dir->d_inode->i_mode)) {
@@ -259,7 +260,7 @@ nfsd4_remove_clid_file(struct dentry *di
 		return -EINVAL;
 	}
 	mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_PARENT);
-	status = vfs_unlink(dir->d_inode, dentry);
+	status = path_unlink(&dir_path, dentry);
 	mutex_unlock(&dir->d_inode->i_mutex);
 	return status;
 }
fs/nfsd/vfs.c +1 -3
Index: vfs-2.6/fs/nfsd/vfs.c
===================================================================
@@ -1734,7 +1734,6 @@ nfsd_unlink(struct svc_rqst *rqstp, stru
 {
 	struct path 	dir_path;
 	struct dentry	*dentry, *rdentry;
-	struct inode	*dirp;
 	__be32		err;
 	int		host_err;
 
@@ -1747,7 +1746,6 @@ nfsd_unlink(struct svc_rqst *rqstp, stru
 
 	fh_lock_nested(fhp, I_MUTEX_PARENT);
 	dentry = fhp->fh_dentry;
-	dirp = dentry->d_inode;
 
 	rdentry = lookup_one_len(fname, dentry, flen);
 	host_err = PTR_ERR(rdentry);
@@ -1772,7 +1770,7 @@ nfsd_unlink(struct svc_rqst *rqstp, stru
 			host_err = -EPERM;
 		} else
 #endif
-		host_err = vfs_unlink(dirp, rdentry);
+		host_err = path_unlink(&dir_path, rdentry);
 	} else { /* It's RMDIR */
 		host_err = path_rmdir(&dir_path, rdentry);
 	}
include/linux/fs.h +1 -1
Index: vfs-2.6/include/linux/fs.h
===================================================================
@@ -1131,7 +1131,7 @@ extern int vfs_symlink(struct inode *, s
 extern int vfs_link(struct dentry *, struct inode *, struct dentry *);
 extern int vfs_rmdir(struct inode *, struct dentry *);
 extern int path_rmdir(struct path *, struct dentry *);
-extern int vfs_unlink(struct inode *, struct dentry *);
+extern int path_unlink(struct path *, struct dentry *);
 extern int vfs_rename(struct inode *, struct dentry *, struct inode *, struct dentry *);
 
 /*
ipc/mqueue.c +5 -5
Index: vfs-2.6/ipc/mqueue.c
===================================================================
@@ -737,6 +737,7 @@ asmlinkage long sys_mq_unlink(const char
 	char *name;
 	struct dentry *dentry;
 	struct inode *inode = NULL;
+	struct path dir_path;
 
 	name = getname(u_name);
 	if (IS_ERR(name))
@@ -758,11 +759,10 @@ 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);
+
+	dir_path.mnt = mqueue_mnt;
+	dir_path.dentry = dentry->d_parent;
+	err = path_unlink(&dir_path, dentry);
 out_err:
 	dput(dentry);
 

--

#29 lore raw

PATCH 5/10 vfs: add path_symlink()

In reply to Miklos Szeredi

From: Miklos Szeredi <[email protected]>

Introduce path_symlink(). Make vfs_symlink() static.

---
 fs/ecryptfs/inode.c |   14 +++++++-------
 fs/namei.c          |   26 ++++++++++++++++++--------
 fs/nfsd/vfs.c       |    7 +++++--
 include/linux/fs.h  |    2 +-
 4 files changed, 31 insertions(+), 18 deletions(-)
fs/ecryptfs/inode.c +7 -7
Index: vfs-2.6/fs/ecryptfs/inode.c
===================================================================
@@ -443,7 +443,7 @@ static int ecryptfs_symlink(struct inode
 {
 	int rc;
 	struct dentry *lower_dentry;
-	struct dentry *lower_dir_dentry;
+	struct path lower_dir;
 	umode_t mode;
 	char *encoded_symname;
 	int encoded_symlen;
@@ -451,7 +451,8 @@ static int ecryptfs_symlink(struct inode
 
 	lower_dentry = ecryptfs_dentry_to_lower(dentry);
 	dget(lower_dentry);
-	lower_dir_dentry = lock_parent(lower_dentry);
+	lower_dir.mnt = ecryptfs_dentry_to_lower_mnt(dentry);
+	lower_dir.dentry = lock_parent(lower_dentry);
 	mode = S_IALLUGO;
 	encoded_symlen = ecryptfs_encode_filename(crypt_stat, symname,
 						  strlen(symname),
@@ -460,18 +461,17 @@ static int ecryptfs_symlink(struct inode
 		rc = encoded_symlen;
 		goto out_lock;
 	}
-	rc = vfs_symlink(lower_dir_dentry->d_inode, lower_dentry,
-			 encoded_symname, mode);
+	rc = path_symlink(&lower_dir, lower_dentry, encoded_symname, mode);
 	kfree(encoded_symname);
 	if (rc || !lower_dentry->d_inode)
 		goto out_lock;
 	rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
 	if (rc)
 		goto out_lock;
-	fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
-	fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
+	fsstack_copy_attr_times(dir, lower_dir.dentry->d_inode);
+	fsstack_copy_inode_size(dir, lower_dir.dentry->d_inode);
 out_lock:
-	unlock_dir(lower_dir_dentry);
+	unlock_dir(lower_dir.dentry);
 	dput(lower_dentry);
 	if (!dentry->d_inode)
 		d_drop(dentry);
fs/namei.c +18 -8
Index: vfs-2.6/fs/namei.c
===================================================================
@@ -2428,7 +2428,7 @@ asmlinkage long sys_unlink(const char __
 	return do_unlinkat(AT_FDCWD, pathname);
 }
 
-int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
+static int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
 {
 	int error = may_create(dir, dentry, NULL);
 
@@ -2449,6 +2449,22 @@ int vfs_symlink(struct inode *dir, struc
 	return error;
 }
 
+int path_symlink(struct path *dir_path, struct dentry *dentry,
+		 const char *oldname, int mode)
+{
+	int error = mnt_want_write(dir_path->mnt);
+
+	if (!error) {
+		struct inode *dir = dir_path->dentry->d_inode;
+
+		error = vfs_symlink(dir, dentry, oldname, mode);
+		mnt_drop_write(dir_path->mnt);
+	}
+
+	return error;
+}
+EXPORT_SYMBOL(path_symlink);
+
 asmlinkage long sys_symlinkat(const char __user *oldname,
 			      int newdfd, const char __user *newname)
 {
@@ -2474,12 +2490,7 @@ asmlinkage long sys_symlinkat(const char
 	if (IS_ERR(dentry))
 		goto out_unlock;
 
-	error = mnt_want_write(nd.path.mnt);
-	if (error)
-		goto out_dput;
-	error = vfs_symlink(nd.path.dentry->d_inode, dentry, from, S_IALLUGO);
-	mnt_drop_write(nd.path.mnt);
-out_dput:
+	error = path_symlink(&nd.path, dentry, from, S_IALLUGO);
 	dput(dentry);
 out_unlock:
 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
@@ -3004,6 +3015,5 @@ EXPORT_SYMBOL(generic_permission);
 EXPORT_SYMBOL(vfs_readlink);
 EXPORT_SYMBOL(vfs_rename);
 EXPORT_SYMBOL(vfs_rmdir);
-EXPORT_SYMBOL(vfs_symlink);
 EXPORT_SYMBOL(dentry_unhash);
 EXPORT_SYMBOL(generic_readlink);
fs/nfsd/vfs.c +5 -2
Index: vfs-2.6/fs/nfsd/vfs.c
===================================================================
@@ -1498,6 +1498,7 @@ nfsd_symlink(struct svc_rqst *rqstp, str
 				struct svc_fh *resfhp,
 				struct iattr *iap)
 {
+	struct path 	dir_path;
 	struct dentry	*dentry, *dnew;
 	__be32		err, cerr;
 	int		host_err;
@@ -1525,6 +1526,8 @@ nfsd_symlink(struct svc_rqst *rqstp, str
 	if (iap && (iap->ia_valid & ATTR_MODE))
 		mode = iap->ia_mode & S_IALLUGO;
 
+	dir_path.mnt = fhp->fh_export->ex_path.mnt;
+	dir_path.dentry = dentry;
 	if (unlikely(path[plen] != 0)) {
 		char *path_alloced = kmalloc(plen+1, GFP_KERNEL);
 		if (path_alloced == NULL)
@@ -1532,11 +1535,11 @@ nfsd_symlink(struct svc_rqst *rqstp, str
 		else {
 			strncpy(path_alloced, path, plen);
 			path_alloced[plen] = 0;
-			host_err = vfs_symlink(dentry->d_inode, dnew, path_alloced, mode);
+			host_err = path_symlink(&dir_path, dnew, path_alloced, mode);
 			kfree(path_alloced);
 		}
 	} else
-		host_err = vfs_symlink(dentry->d_inode, dnew, path, mode);
+		host_err = path_symlink(&dir_path, dnew, path, mode);
 
 	if (!host_err) {
 		if (EX_ISSYNC(fhp->fh_export))
include/linux/fs.h +1 -1
Index: vfs-2.6/include/linux/fs.h
===================================================================
@@ -1127,7 +1127,7 @@ extern int path_create(struct path *, st
 extern int vfs_mkdir(struct inode *, struct dentry *, int);
 extern int path_mkdir(struct path *, struct dentry *, int);
 extern int path_mknod(struct path *, struct dentry *, int, dev_t);
-extern int vfs_symlink(struct inode *, struct dentry *, const char *, int);
+extern int path_symlink(struct path *, struct dentry *, const char *, int);
 extern int vfs_link(struct dentry *, struct inode *, struct dentry *);
 extern int vfs_rmdir(struct inode *, struct dentry *);
 extern int path_rmdir(struct path *, struct dentry *);

--

#30 lore raw

PATCH 6/10 vfs: add path_link()

In reply to Miklos Szeredi

From: Miklos Szeredi <[email protected]>

Introduce path_link(). Make vfs_link() static.

---
 fs/ecryptfs/inode.c |   10 +++++-----
 fs/namei.c          |   26 ++++++++++++++++++--------
 fs/nfsd/vfs.c       |    8 +++++---
 include/linux/fs.h  |    2 +-
 4 files changed, 29 insertions(+), 17 deletions(-)
fs/ecryptfs/inode.c +5 -5
Index: vfs-2.6/fs/ecryptfs/inode.c
===================================================================
@@ -383,7 +383,7 @@ static int ecryptfs_link(struct dentry *
 {
 	struct dentry *lower_old_dentry;
 	struct dentry *lower_new_dentry;
-	struct dentry *lower_dir_dentry;
+	struct path lower_dir;
 	u64 file_size_save;
 	int rc;
 
@@ -392,9 +392,9 @@ static int ecryptfs_link(struct dentry *
 	lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
 	dget(lower_old_dentry);
 	dget(lower_new_dentry);
-	lower_dir_dentry = lock_parent(lower_new_dentry);
-	rc = vfs_link(lower_old_dentry, lower_dir_dentry->d_inode,
-		      lower_new_dentry);
+	lower_dir.mnt = ecryptfs_dentry_to_lower_mnt(new_dentry);
+	lower_dir.dentry = lock_parent(lower_new_dentry);
+	rc = path_link(lower_old_dentry, &lower_dir, lower_new_dentry);
 	if (rc || !lower_new_dentry->d_inode)
 		goto out_lock;
 	rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb, 0);
@@ -406,7 +406,7 @@ static int ecryptfs_link(struct dentry *
 		ecryptfs_inode_to_lower(old_dentry->d_inode)->i_nlink;
 	i_size_write(new_dentry->d_inode, file_size_save);
 out_lock:
-	unlock_dir(lower_dir_dentry);
+	unlock_dir(lower_dir.dentry);
 	dput(lower_new_dentry);
 	dput(lower_old_dentry);
 	d_drop(lower_old_dentry);
fs/namei.c +18 -8
Index: vfs-2.6/fs/namei.c
===================================================================
@@ -2507,7 +2507,7 @@ asmlinkage long sys_symlink(const char _
 	return sys_symlinkat(oldname, AT_FDCWD, newname);
 }
 
-int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
+static int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
 {
 	struct inode *inode = old_dentry->d_inode;
 	int error;
@@ -2545,6 +2545,22 @@ int vfs_link(struct dentry *old_dentry, 
 	return error;
 }
 
+int path_link(struct dentry *old_dentry, struct path *dir_path,
+	      struct dentry *new_dentry)
+{
+	int error = mnt_want_write(dir_path->mnt);
+
+	if (!error) {
+		struct inode *dir = dir_path->dentry->d_inode;
+
+		error = vfs_link(old_dentry, dir, new_dentry);
+		mnt_drop_write(dir_path->mnt);
+	}
+
+	return error;
+}
+EXPORT_SYMBOL(path_link);
+
 /*
  * Hardlinks are often used in delicate situations.  We avoid
  * security-related surprises by not following symlinks on the
@@ -2585,12 +2601,7 @@ asmlinkage long sys_linkat(int olddfd, c
 	error = PTR_ERR(new_dentry);
 	if (IS_ERR(new_dentry))
 		goto out_unlock;
-	error = mnt_want_write(nd.path.mnt);
-	if (error)
-		goto out_dput;
-	error = vfs_link(old_nd.path.dentry, nd.path.dentry->d_inode, new_dentry);
-	mnt_drop_write(nd.path.mnt);
-out_dput:
+	error = path_link(old_nd.path.dentry, &nd.path, new_dentry);
 	dput(new_dentry);
 out_unlock:
 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
@@ -3010,7 +3021,6 @@ EXPORT_SYMBOL(vfs_permission);
 EXPORT_SYMBOL(file_permission);
 EXPORT_SYMBOL(unlock_rename);
 EXPORT_SYMBOL(vfs_follow_link);
-EXPORT_SYMBOL(vfs_link);
 EXPORT_SYMBOL(generic_permission);
 EXPORT_SYMBOL(vfs_readlink);
 EXPORT_SYMBOL(vfs_rename);
fs/nfsd/vfs.c +5 -3
Index: vfs-2.6/fs/nfsd/vfs.c
===================================================================
@@ -1567,8 +1567,9 @@ __be32
 nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
 				char *name, int len, struct svc_fh *tfhp)
 {
+	struct path 	dir_path;
 	struct dentry	*ddir, *dnew, *dold;
-	struct inode	*dirp, *dest;
+	struct inode	*dest;
 	__be32		err;
 	int		host_err;
 
@@ -1588,7 +1589,6 @@ nfsd_link(struct svc_rqst *rqstp, struct
 
 	fh_lock_nested(ffhp, I_MUTEX_PARENT);
 	ddir = ffhp->fh_dentry;
-	dirp = ddir->d_inode;
 
 	dnew = lookup_one_len(name, ddir, len);
 	host_err = PTR_ERR(dnew);
@@ -1598,7 +1598,9 @@ nfsd_link(struct svc_rqst *rqstp, struct
 	dold = tfhp->fh_dentry;
 	dest = dold->d_inode;
 
-	host_err = vfs_link(dold, dirp, dnew);
+	dir_path.mnt = ffhp->fh_export->ex_path.mnt;
+	dir_path.dentry = ddir;
+	host_err = path_link(dold, &dir_path, dnew);
 	if (!host_err) {
 		if (EX_ISSYNC(ffhp->fh_export)) {
 			err = nfserrno(nfsd_sync_dir(ddir));
include/linux/fs.h +1 -1
Index: vfs-2.6/include/linux/fs.h
===================================================================
@@ -1128,7 +1128,7 @@ extern int vfs_mkdir(struct inode *, str
 extern int path_mkdir(struct path *, struct dentry *, int);
 extern int path_mknod(struct path *, struct dentry *, int, dev_t);
 extern int path_symlink(struct path *, struct dentry *, const char *, int);
-extern int vfs_link(struct dentry *, struct inode *, struct dentry *);
+extern int path_link(struct dentry *, struct path *, struct dentry *);
 extern int vfs_rmdir(struct inode *, struct dentry *);
 extern int path_rmdir(struct path *, struct dentry *);
 extern int path_unlink(struct path *, struct dentry *);

--

#31 lore raw

PATCH 7/10 vfs: add path_rename()

In reply to Miklos Szeredi

From: Miklos Szeredi <[email protected]>

Introduce path_rename(). Make vfs_rename() static.

---
 fs/ecryptfs/inode.c |   13 +++++++------
 fs/namei.c          |   27 +++++++++++++++++++--------
 fs/nfsd/vfs.c       |   17 +++++------------
 include/linux/fs.h  |    2 +-
 4 files changed, 32 insertions(+), 27 deletions(-)
fs/ecryptfs/inode.c +7 -6
Index: vfs-2.6/fs/ecryptfs/inode.c
===================================================================
@@ -559,25 +559,26 @@ ecryptfs_rename(struct inode *old_dir, s
 	int rc;
 	struct dentry *lower_old_dentry;
 	struct dentry *lower_new_dentry;
-	struct dentry *lower_old_dir_dentry;
+	struct path lower_old_dir;
 	struct dentry *lower_new_dir_dentry;
 
 	lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
 	lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
 	dget(lower_old_dentry);
 	dget(lower_new_dentry);
-	lower_old_dir_dentry = dget_parent(lower_old_dentry);
+	lower_old_dir.mnt = ecryptfs_dentry_to_lower_mnt(old_dentry);
+	lower_old_dir.dentry = dget_parent(lower_old_dentry);
 	lower_new_dir_dentry = dget_parent(lower_new_dentry);
-	lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
-	rc = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry,
+	lock_rename(lower_old_dir.dentry, lower_new_dir_dentry);
+	rc = path_rename(&lower_old_dir, lower_old_dentry,
 			lower_new_dir_dentry->d_inode, lower_new_dentry);
 	if (rc)
 		goto out_lock;
 	fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode, NULL);
 	if (new_dir != old_dir)
-		fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode, NULL);
+		fsstack_copy_attr_all(old_dir, lower_old_dir.dentry->d_inode, NULL);
 out_lock:
-	unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
+	unlock_rename(lower_old_dir.dentry, lower_new_dir_dentry);
 	dput(lower_new_dentry->d_parent);
 	dput(lower_old_dentry->d_parent);
 	dput(lower_new_dentry);
fs/namei.c +19 -8
Index: vfs-2.6/fs/namei.c
===================================================================
@@ -2723,8 +2723,8 @@ static int vfs_rename_other(struct inode
 	return error;
 }
 
-int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
-	       struct inode *new_dir, struct dentry *new_dentry)
+static int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
+		      struct inode *new_dir, struct dentry *new_dentry)
 {
 	int error;
 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
@@ -2766,6 +2766,22 @@ int vfs_rename(struct inode *old_dir, st
 	return error;
 }
 
+int path_rename(struct path *old_dir_path, struct dentry *old_dentry,
+		struct inode *new_dir, struct dentry *new_dentry)
+{
+	int error = mnt_want_write(old_dir_path->mnt);
+
+	if (!error) {
+		struct inode *old_dir = old_dir_path->dentry->d_inode;
+
+		error = vfs_rename(old_dir, old_dentry, new_dir, new_dentry);
+		mnt_drop_write(old_dir_path->mnt);
+	}
+
+	return error;
+}
+EXPORT_SYMBOL(path_rename);
+
 static int do_rename(int olddfd, const char *oldname,
 			int newdfd, const char *newname)
 {
@@ -2827,12 +2843,8 @@ static int do_rename(int olddfd, const c
 	if (new_dentry == trap)
 		goto exit5;
 
-	error = mnt_want_write(oldnd.path.mnt);
-	if (error)
-		goto exit5;
-	error = vfs_rename(old_dir->d_inode, old_dentry,
+	error = path_rename(&oldnd.path, old_dentry,
 				   new_dir->d_inode, new_dentry);
-	mnt_drop_write(oldnd.path.mnt);
 exit5:
 	dput(new_dentry);
 exit4:
@@ -3023,7 +3035,6 @@ EXPORT_SYMBOL(unlock_rename);
 EXPORT_SYMBOL(vfs_follow_link);
 EXPORT_SYMBOL(generic_permission);
 EXPORT_SYMBOL(vfs_readlink);
-EXPORT_SYMBOL(vfs_rename);
 EXPORT_SYMBOL(vfs_rmdir);
 EXPORT_SYMBOL(dentry_unhash);
 EXPORT_SYMBOL(generic_readlink);
fs/nfsd/vfs.c +5 -12
Index: vfs-2.6/fs/nfsd/vfs.c
===================================================================
@@ -1633,8 +1633,9 @@ __be32
 nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
 			    struct svc_fh *tfhp, char *tname, int tlen)
 {
+	struct path 	old_dir_path;
 	struct dentry	*fdentry, *tdentry, *odentry, *ndentry, *trap;
-	struct inode	*fdir, *tdir;
+	struct inode	*tdir;
 	__be32		err;
 	int		host_err;
 
@@ -1646,7 +1647,6 @@ nfsd_rename(struct svc_rqst *rqstp, stru
 		goto out;
 
 	fdentry = ffhp->fh_dentry;
-	fdir = fdentry->d_inode;
 
 	tdentry = tfhp->fh_dentry;
 	tdir = tdentry->d_inode;
@@ -1693,22 +1693,15 @@ nfsd_rename(struct svc_rqst *rqstp, stru
 			goto out_dput_new;
 	}
 
-	host_err = -EXDEV;
-	if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
-		goto out_dput_new;
-	host_err = mnt_want_write(ffhp->fh_export->ex_path.mnt);
-	if (host_err)
-		goto out_dput_new;
-
-	host_err = vfs_rename(fdir, odentry, tdir, ndentry);
+	old_dir_path.mnt = ffhp->fh_export->ex_path.mnt;
+	old_dir_path.dentry = fdentry;
+	host_err = path_rename(&old_dir_path, odentry, tdir, ndentry);
 	if (!host_err && EX_ISSYNC(tfhp->fh_export)) {
 		host_err = nfsd_sync_dir(tdentry);
 		if (!host_err)
 			host_err = nfsd_sync_dir(fdentry);
 	}
 
-	mnt_drop_write(ffhp->fh_export->ex_path.mnt);
-
  out_dput_new:
 	dput(ndentry);
  out_dput_old:
include/linux/fs.h +1 -1
Index: vfs-2.6/include/linux/fs.h
===================================================================
@@ -1132,7 +1132,7 @@ extern int path_link(struct dentry *, st
 extern int vfs_rmdir(struct inode *, struct dentry *);
 extern int path_rmdir(struct path *, struct dentry *);
 extern int path_unlink(struct path *, struct dentry *);
-extern int vfs_rename(struct inode *, struct dentry *, struct inode *, struct dentry *);
+extern int path_rename(struct path *, struct dentry *, struct inode *, struct dentry *);
 
 /*
  * VFS dentry helper functions.

--

#32 lore raw

Re: [patch 07/10] vfs: add path_rename()

Quoted 3 lines · Miklos Szeredi

From: Miklos Szeredi <[email protected]>

Introduce path_rename(). Make vfs_rename() static.

[...]

Quoted 5 lines
fs/namei.c +5 -0
+int path_rename(struct path *old_dir_path, struct dentry *old_dentry,
+		struct inode *new_dir, struct dentry *new_dentry)
+{
+	int error = mnt_want_write(old_dir_path->mnt);
+

fs/namei.c int vfs_rename(struct inode *old_dir, st · line 2783

Quoted 10 lines
fs/namei.c +10 -0
+	if (!error) {
+		struct inode *old_dir = old_dir_path->dentry->d_inode;
+
+		error = vfs_rename(old_dir, old_dentry, new_dir, new_dentry);
+		mnt_drop_write(old_dir_path->mnt);
+	}
+
+	return error;
+}
+EXPORT_SYMBOL(path_rename);

You're grabbing a want_write ref on the source vfsmount only. Shouldn't path_rename() call mnt_want_write on *both* the source and destination vfsmounts? If so, do we need to order the locks ala lock_rename()?

Erez.

#33 lore raw

Re: [patch 07/10] vfs: add path_rename()

Quoted 15 lines
Quoted 3 lines

From: Miklos Szeredi <[email protected]>

Introduce path_rename(). Make vfs_rename() static.

[...]

Quoted 11 lines
fs/namei.c +11 -0
+int path_rename(struct path *old_dir_path, struct dentry *old_dentry,
+		struct inode *new_dir, struct dentry *new_dentry)
+{
+	int error = mnt_want_write(old_dir_path->mnt);
+
+	if (!error) {
+		struct inode *old_dir = old_dir_path->dentry->d_inode;
+
+		error = vfs_rename(old_dir, old_dentry, new_dir, new_dentry);
+		mnt_drop_write(old_dir_path->mnt);
+	}
Quoted 8 lines

fs/namei.c int vfs_rename(struct inode *old_dir, st · line 2783

Quoted 4 lines
fs/namei.c +4 -0
+
+	return error;
+}
+EXPORT_SYMBOL(path_rename);

You're grabbing a want_write ref on the source vfsmount only. Shouldn't path_rename() call mnt_want_write on *both* the source and destination vfsmounts?

The source and the destination vfsmounts are always the same.

Quoted 1 line

If so, do we need to order the locks ala lock_rename()?

These are counters (similar to reference counts), not locks.

Miklos

#34 lore raw

PATCH 8/10 vfs: add path_setattr()

In reply to Miklos Szeredi

From: Miklos Szeredi <[email protected]>

Introduce path_setattr(). Add checks to IS_IMMUTABLE(inode) and IS_APPEND(inode) since all callers require it. Maybe these should be moved into notify_change()?

---
 fs/attr.c           |   23 ++++++++++++++++-
 fs/ecryptfs/inode.c |   11 ++++----
 fs/nfsd/vfs.c       |   21 +++++++++++-----
 fs/open.c           |   68 ++++++----------------------------------------------
 include/linux/fs.h  |    1 
 5 files changed, 53 insertions(+), 71 deletions(-)
fs/open.c +9 -59
Index: vfs-2.6/fs/open.c
===================================================================
@@ -578,23 +578,13 @@ asmlinkage long sys_fchmod(unsigned int 
 
 	audit_inode(NULL, dentry);
 
-	err = mnt_want_write(file->f_vfsmnt);
-	if (err)
-		goto out_putf;
-	err = -EPERM;
-	if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
-		goto out_drop_write;
 	mutex_lock(&inode->i_mutex);
 	if (mode == (mode_t) -1)
 		mode = inode->i_mode;
 	newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
 	newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
-	err = notify_change(dentry, &newattrs);
+	err = path_setattr(&file->f_path, &newattrs);
 	mutex_unlock(&inode->i_mutex);
-
-out_drop_write:
-	mnt_drop_write(file->f_vfsmnt);
-out_putf:
 	fput(file);
 out:
 	return err;
@@ -613,25 +603,14 @@ asmlinkage long sys_fchmodat(int dfd, co
 		goto out;
 	inode = nd.path.dentry->d_inode;
 
-	error = mnt_want_write(nd.path.mnt);
-	if (error)
-		goto dput_and_out;
-
-	error = -EPERM;
-	if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
-		goto out_drop_write;
-
 	mutex_lock(&inode->i_mutex);
 	if (mode == (mode_t) -1)
 		mode = inode->i_mode;
 	newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
 	newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
-	error = notify_change(nd.path.dentry, &newattrs);
+	error = path_setattr(&nd.path, &newattrs);
 	mutex_unlock(&inode->i_mutex);
 
-out_drop_write:
-	mnt_drop_write(nd.path.mnt);
-dput_and_out:
 	path_put(&nd.path);
 out:
 	return error;
@@ -642,20 +621,12 @@ asmlinkage long sys_chmod(const char __u
 	return sys_fchmodat(AT_FDCWD, filename, mode);
 }
 
-static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
+static int chown_common(struct path *path, uid_t user, gid_t group)
 {
-	struct inode * inode;
+	struct inode *inode = path->dentry->d_inode;
 	int error;
 	struct iattr newattrs;
 
-	error = -ENOENT;
-	if (!(inode = dentry->d_inode)) {
-		printk(KERN_ERR "chown_common: NULL inode\n");
-		goto out;
-	}
-	error = -EPERM;
-	if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
-		goto out;
 	newattrs.ia_valid =  ATTR_CTIME;
 	if (user != (uid_t) -1) {
 		newattrs.ia_valid |= ATTR_UID;
@@ -669,9 +640,8 @@ static int chown_common(struct dentry * 
 		newattrs.ia_valid |=
 			ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
 	mutex_lock(&inode->i_mutex);
-	error = notify_change(dentry, &newattrs);
+	error = path_setattr(path, &newattrs);
 	mutex_unlock(&inode->i_mutex);
-out:
 	return error;
 }
 
@@ -683,12 +653,7 @@ asmlinkage long sys_chown(const char __u
 	error = user_path_walk(filename, &nd);
 	if (error)
 		goto out;
-	error = mnt_want_write(nd.path.mnt);
-	if (error)
-		goto out_release;
-	error = chown_common(nd.path.dentry, user, group);
-	mnt_drop_write(nd.path.mnt);
-out_release:
+	error = chown_common(&nd.path, user, group);
 	path_put(&nd.path);
 out:
 	return error;
@@ -708,12 +673,7 @@ asmlinkage long sys_fchownat(int dfd, co
 	error = __user_walk_fd(dfd, filename, follow, &nd);
 	if (error)
 		goto out;
-	error = mnt_want_write(nd.path.mnt);
-	if (error)
-		goto out_release;
-	error = chown_common(nd.path.dentry, user, group);
-	mnt_drop_write(nd.path.mnt);
-out_release:
+	error = chown_common(&nd.path, user, group);
 	path_put(&nd.path);
 out:
 	return error;
@@ -727,12 +687,7 @@ asmlinkage long sys_lchown(const char __
 	error = user_path_walk_link(filename, &nd);
 	if (error)
 		goto out;
-	error = mnt_want_write(nd.path.mnt);
-	if (error)
-		goto out_release;
-	error = chown_common(nd.path.dentry, user, group);
-	mnt_drop_write(nd.path.mnt);
-out_release:
+	error = chown_common(&nd.path, user, group);
 	path_put(&nd.path);
 out:
 	return error;
@@ -749,14 +704,9 @@ asmlinkage long sys_fchown(unsigned int 
 	if (!file)
 		goto out;
 
-	error = mnt_want_write(file->f_vfsmnt);
-	if (error)
-		goto out_fput;
 	dentry = file->f_path.dentry;
 	audit_inode(NULL, dentry);
-	error = chown_common(dentry, user, group);
-	mnt_drop_write(file->f_vfsmnt);
-out_fput:
+	error = chown_common(&file->f_path, user, group);
 	fput(file);
 out:
 	return error;
fs/attr.c +22 -1
Index: vfs-2.6/fs/attr.c
===================================================================
@@ -14,6 +14,7 @@
 #include <linux/fcntl.h>
 #include <linux/quotaops.h>
 #include <linux/security.h>
+#include <linux/mount.h>
 
 /* Taken over from the old code... */
 
@@ -182,5 +183,25 @@ int notify_change(struct dentry * dentry
 
 	return error;
 }
-
 EXPORT_SYMBOL(notify_change);
+
+int path_setattr(struct path *path, struct iattr *attr)
+{
+	struct inode *inode = path->dentry->d_inode;
+	int error = mnt_want_write(path->mnt);
+
+	if (error)
+		goto out;
+
+	error = -EPERM;
+	if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
+		goto out_drop_write;
+
+	error = notify_change(path->dentry, attr);
+
+ out_drop_write:
+	mnt_drop_write(path->mnt);
+ out:
+	return error;
+}
+EXPORT_SYMBOL(path_setattr);
fs/nfsd/vfs.c +15 -6
Index: vfs-2.6/fs/nfsd/vfs.c
===================================================================
@@ -387,8 +387,13 @@ nfsd_setattr(struct svc_rqst *rqstp, str
 
 	err = nfserr_notsync;
 	if (!check_guard || guardtime == inode->i_ctime.tv_sec) {
+		struct path path = {
+			.mnt = fhp->fh_export->ex_path.mnt,
+			.dentry = dentry,
+		};
+
 		fh_lock(fhp);
-		host_err = notify_change(dentry, iap);
+		host_err = path_setattr(&path, iap);
 		err = nfserrno(host_err);
 		fh_unlock(fhp);
 	}
@@ -944,14 +949,18 @@ out:
 	return err;
 }
 
-static void kill_suid(struct dentry *dentry)
+static void kill_suid(struct svc_fh *fhp)
 {
+	struct path path = {
+		.mnt = fhp->fh_export->ex_path.mnt,
+		.dentry = fhp->fh_dentry,
+	};
 	struct iattr	ia;
 	ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
 
-	mutex_lock(&dentry->d_inode->i_mutex);
-	notify_change(dentry, &ia);
-	mutex_unlock(&dentry->d_inode->i_mutex);
+	mutex_lock(&path.dentry->d_inode->i_mutex);
+	path_setattr(&path, &ia);
+	mutex_unlock(&path.dentry->d_inode->i_mutex);
 }
 
 static __be32
@@ -1009,7 +1018,7 @@ nfsd_vfs_write(struct svc_rqst *rqstp, s
 
 	/* clear setuid/setgid flag after write */
 	if (host_err >= 0 && (inode->i_mode & (S_ISUID | S_ISGID)))
-		kill_suid(dentry);
+		kill_suid(fhp);
 
 	if (host_err >= 0 && stable) {
 		static ino_t	last_ino;
include/linux/fs.h +1 -0
Index: vfs-2.6/include/linux/fs.h
===================================================================
@@ -1760,6 +1760,7 @@ extern int do_remount_sb(struct super_bl
 extern sector_t bmap(struct inode *, sector_t);
 #endif
 extern int notify_change(struct dentry *, struct iattr *);
+extern int path_setattr(struct path *, struct iattr *);
 extern int permission(struct inode *, int, struct nameidata *);
 extern int generic_permission(struct inode *, int,
 		int (*check_acl)(struct inode *, int));
fs/ecryptfs/inode.c +6 -5
Index: vfs-2.6/fs/ecryptfs/inode.c
===================================================================
@@ -846,7 +846,7 @@ ecryptfs_permission(struct inode *inode,
 static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
 {
 	int rc = 0;
-	struct dentry *lower_dentry;
+	struct path lower_path;
 	struct inode *inode;
 	struct inode *lower_inode;
 	struct ecryptfs_crypt_stat *crypt_stat;
@@ -856,7 +856,8 @@ static int ecryptfs_setattr(struct dentr
 		ecryptfs_init_crypt_stat(crypt_stat);
 	inode = dentry->d_inode;
 	lower_inode = ecryptfs_inode_to_lower(inode);
-	lower_dentry = ecryptfs_dentry_to_lower(dentry);
+	lower_path.mnt = ecryptfs_dentry_to_lower_mnt(dentry);
+	lower_path.dentry = ecryptfs_dentry_to_lower(dentry);
 	mutex_lock(&crypt_stat->cs_mutex);
 	if (S_ISDIR(dentry->d_inode->i_mode))
 		crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
@@ -907,9 +908,9 @@ static int ecryptfs_setattr(struct dentr
 	if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
 		ia->ia_valid &= ~ATTR_MODE;
 
-	mutex_lock(&lower_dentry->d_inode->i_mutex);
-	rc = notify_change(lower_dentry, ia);
-	mutex_unlock(&lower_dentry->d_inode->i_mutex);
+	mutex_lock(&lower_path.dentry->d_inode->i_mutex);
+	rc = path_setattr(&lower_path, ia);
+	mutex_unlock(&lower_path.dentry->d_inode->i_mutex);
 out:
 	fsstack_copy_attr_all(inode, lower_inode, NULL);
 	return rc;

--

#35 lore raw

PATCH 9/10 vfs: add path_setxattr()

In reply to Miklos Szeredi

From: Miklos Szeredi <[email protected]>

Introduce path_setxattr(). Make vfs_setxattr() static.

---
 fs/nfsd/vfs.c         |   21 +++++++++++++--------
 fs/xattr.c            |   42 ++++++++++++++++++++----------------------
 include/linux/xattr.h |    2 +-
 3 files changed, 34 insertions(+), 31 deletions(-)
fs/nfsd/vfs.c +13 -8
Index: vfs-2.6/fs/nfsd/vfs.c
===================================================================
@@ -431,7 +431,7 @@ static ssize_t nfsd_getxattr(struct dent
 
 #if defined(CONFIG_NFSD_V4)
 static int
-set_nfsv4_acl_one(struct dentry *dentry, struct posix_acl *pacl, char *key)
+set_nfsv4_acl_one(struct path *path, struct posix_acl *pacl, char *key)
 {
 	int len;
 	size_t buflen;
@@ -450,7 +450,7 @@ set_nfsv4_acl_one(struct dentry *dentry,
 		goto out;
 	}
 
-	error = vfs_setxattr(dentry, key, buf, len, 0);
+	error = path_setxattr(path, key, buf, len, 0);
 out:
 	kfree(buf);
 	return error;
@@ -462,7 +462,7 @@ nfsd4_set_nfs4_acl(struct svc_rqst *rqst
 {
 	__be32 error;
 	int host_error;
-	struct dentry *dentry;
+	struct path path;
 	struct inode *inode;
 	struct posix_acl *pacl = NULL, *dpacl = NULL;
 	unsigned int flags = 0;
@@ -472,8 +472,9 @@ nfsd4_set_nfs4_acl(struct svc_rqst *rqst
 	if (error)
 		return error;
 
-	dentry = fhp->fh_dentry;
-	inode = dentry->d_inode;
+	path.mnt = fhp->fh_export->ex_path.mnt;
+	path.dentry = fhp->fh_dentry;
+	inode = path.dentry->d_inode;
 	if (S_ISDIR(inode->i_mode))
 		flags = NFS4_ACL_DIR;
 
@@ -483,12 +484,12 @@ nfsd4_set_nfs4_acl(struct svc_rqst *rqst
 	} else if (host_error < 0)
 		goto out_nfserr;
 
-	host_error = set_nfsv4_acl_one(dentry, pacl, POSIX_ACL_XATTR_ACCESS);
+	host_error = set_nfsv4_acl_one(&path, pacl, POSIX_ACL_XATTR_ACCESS);
 	if (host_error < 0)
 		goto out_release;
 
 	if (S_ISDIR(inode->i_mode))
-		host_error = set_nfsv4_acl_one(dentry, dpacl, POSIX_ACL_XATTR_DEFAULT);
+		host_error = set_nfsv4_acl_one(&path, dpacl, POSIX_ACL_XATTR_DEFAULT);
 
 out_release:
 	posix_acl_release(pacl);
@@ -2028,6 +2029,10 @@ nfsd_get_posix_acl(struct svc_fh *fhp, i
 int
 nfsd_set_posix_acl(struct svc_fh *fhp, int type, struct posix_acl *acl)
 {
+	struct path path = {
+		.mnt = fhp->fh_export->ex_path.mnt,
+		.dentry = fhp->fh_dentry,
+	};
 	struct inode *inode = fhp->fh_dentry->d_inode;
 	char *name;
 	void *value = NULL;
@@ -2061,7 +2066,7 @@ nfsd_set_posix_acl(struct svc_fh *fhp, i
 		size = 0;
 
 	if (size)
-		error = vfs_setxattr(fhp->fh_dentry, name, value, size, 0);
+		error = path_setxattr(&path, name, value, size, 0);
 	else {
 		if (!S_ISDIR(inode->i_mode) && type == ACL_TYPE_DEFAULT)
 			error = 0;
fs/xattr.c +20 -22

86 lines not on this page · raw message

include/linux/xattr.h +1 -1
Index: vfs-2.6/include/linux/xattr.h
===================================================================
@@ -49,7 +49,7 @@ struct xattr_handler {
 ssize_t xattr_getsecurity(struct inode *, const char *, void *, size_t);
 ssize_t vfs_getxattr(struct dentry *, char *, void *, size_t);
 ssize_t vfs_listxattr(struct dentry *d, char *list, size_t size);
-int vfs_setxattr(struct dentry *, char *, void *, size_t, int);
+int path_setxattr(struct path *, char *, void *, size_t, int);
 int vfs_removexattr(struct dentry *, char *);
 
 ssize_t generic_getxattr(struct dentry *dentry, const char *name, void *buffer, size_t size);

--

#36 lore raw

PATCH 10/10 vfs: add path_removexattr()

In reply to Miklos Szeredi

From: Miklos Szeredi <[email protected]>

Introduce path_removexattr(). Make vfs_removexattr() static.

mnt_want_write()/mnt_drop_write() were completely missing for the *removexattr() syscalls.

---
 fs/nfsd/vfs.c         |    2 +-
 fs/xattr.c            |   25 ++++++++++++++++++-------
 include/linux/xattr.h |    2 +-
 3 files changed, 20 insertions(+), 9 deletions(-)
fs/nfsd/vfs.c +1 -1
Index: vfs-2.6/fs/nfsd/vfs.c
===================================================================
@@ -2071,7 +2071,7 @@ nfsd_set_posix_acl(struct svc_fh *fhp, i
 		if (!S_ISDIR(inode->i_mode) && type == ACL_TYPE_DEFAULT)
 			error = 0;
 		else {
-			error = vfs_removexattr(fhp->fh_dentry, name);
+			error = path_removexattr(&path, name);
 			if (error == -ENODATA)
 				error = 0;
 		}
fs/xattr.c +18 -7
Index: vfs-2.6/fs/xattr.c
===================================================================
@@ -199,7 +199,7 @@ vfs_listxattr(struct dentry *d, char *li
 }
 EXPORT_SYMBOL_GPL(vfs_listxattr);
 
-int
+static int
 vfs_removexattr(struct dentry *dentry, char *name)
 {
 	struct inode *inode = dentry->d_inode;
@@ -224,8 +224,19 @@ vfs_removexattr(struct dentry *dentry, c
 		fsnotify_xattr(dentry);
 	return error;
 }
-EXPORT_SYMBOL_GPL(vfs_removexattr);
 
+int path_removexattr(struct path *path, char *name)
+{
+	int error = mnt_want_write(path->mnt);
+
+	if (!error) {
+		error = vfs_removexattr(path->dentry, name);
+		mnt_drop_write(path->mnt);
+	}
+
+	return error;
+}
+EXPORT_SYMBOL(path_removexattr);
 
 /*
  * Extended attribute SET operations
@@ -471,7 +482,7 @@ sys_flistxattr(int fd, char __user *list
  * Extended attribute REMOVE operations
  */
 static long
-removexattr(struct dentry *d, char __user *name)
+removexattr(struct path *path, char __user *name)
 {
 	int error;
 	char kname[XATTR_NAME_MAX + 1];
@@ -482,7 +493,7 @@ removexattr(struct dentry *d, char __use
 	if (error < 0)
 		return error;
 
-	return vfs_removexattr(d, kname);
+	return path_removexattr(path, kname);
 }
 
 asmlinkage long
@@ -494,7 +505,7 @@ sys_removexattr(char __user *path, char 
 	error = user_path_walk(path, &nd);
 	if (error)
 		return error;
-	error = removexattr(nd.path.dentry, name);
+	error = removexattr(&nd.path, name);
 	path_put(&nd.path);
 	return error;
 }
@@ -508,7 +519,7 @@ sys_lremovexattr(char __user *path, char
 	error = user_path_walk_link(path, &nd);
 	if (error)
 		return error;
-	error = removexattr(nd.path.dentry, name);
+	error = removexattr(&nd.path, name);
 	path_put(&nd.path);
 	return error;
 }
@@ -525,7 +536,7 @@ sys_fremovexattr(int fd, char __user *na
 		return error;
 	dentry = f->f_path.dentry;
 	audit_inode(NULL, dentry);
-	error = removexattr(dentry, name);
+	error = removexattr(&f->f_path, name);
 	fput(f);
 	return error;
 }
include/linux/xattr.h +1 -1
Index: vfs-2.6/include/linux/xattr.h
===================================================================
@@ -50,7 +50,7 @@ ssize_t xattr_getsecurity(struct inode *
 ssize_t vfs_getxattr(struct dentry *, char *, void *, size_t);
 ssize_t vfs_listxattr(struct dentry *d, char *list, size_t size);
 int path_setxattr(struct path *, char *, void *, size_t, int);
-int vfs_removexattr(struct dentry *, char *);
+int path_removexattr(struct path *, char *);
 
 ssize_t generic_getxattr(struct dentry *dentry, const char *name, void *buffer, size_t size);
 ssize_t generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size);

--

#37 lore raw
In reply to Miklos Szeredi
Quoted 5 lines · Miklos Szeredi

See first patch for description. This is against vfs-2.6 + two patches I posted recently.

It's not for -mm yet, as it needs conversion of unionfs (will that be done by me, I wonder?)

Miklos, I'll be happy to do the conversion of unionfs in -mm to use these path_* helpers. Is the code in these helpers going to change at all? Can I expect the API to remain stable?

Quoted 2 lines

Thanks,
Miklos

Thanks,
Erez.

#38 lore raw

PATCH 1/1 Unionfs: use the new path_* VFS helpers

In reply to Miklos Szeredi

Miklos and Al,

As promised, here is a patch for Unionfs to use the new path_* VFS helpers which now take a struct path *. I've used Al's vfs git tree plus all 12 patches you've outlined (ten path_* patches + 2 others). I've run Unionfs through my basic regression suite; I haven't done more extensive testing yet. This patch can be used in -mm if/when your path_* patches make it there (this patch will be a standalone one to be used on top of my unionfs.git tree).

Al, I'm not too pleased with the resulting additional code complexity to Unionfs, due to these new helpers. See the diffstat below. These VFS helpers have widened the gap between the f/s API and the VFS helpers': the helpers now need a struct path/vfsmount, but the most ->ops that a filesystem is being called with, don't have a path/vfsmount passed.

You've said before that a filesystems has no business dealing with vfsmounts (with the exception of follow_link). Fine. But the irony now is that the new vfs helpers that a filesystems is likely to use, FORCE you to know a lot more about vfsmounts! If indeed a filesystem shouldn't have to know or deal with vfsmounts, then none of the VFS helpers should require a struct vfsmount (or a struct path, or a nameidata, which embed a vfsmount+dentry pair). Otherwise, let's admit that some filesystems have to know about vfsmounts and hence let's pass them to all appropriate fs ->ops. (BTW, if that's done, then it'd also be helpful for a struct vfsmount to have a "void *private" into which a stacked f/s can store "lower objects").

I hope your planned Big VFS Overhaul can reverse this trend by making it possible for a filesystem to not know anything (or know very little) about vfsmounts and the structs that embed them.

Thanks,
Erez.

 commonfops.c |    5 +++-
 copyup.c     |   41 ++++++++++++++++++++++++++--------
 dirhelper.c  |   10 ++++++--
 inode.c      |   70 +++++++++++++++++++++++++++++++++++++++++------------------
 lookup.c     |    8 +++++-
 rename.c     |   34 +++++++++++++++++++---------
 sioq.c       |   10 ++++----
 sioq.h       |    1 
 subr.c       |   22 +++++++++++-------
 union.h      |    3 +-
 unlink.c     |   10 +++++---
 xattr.c      |   11 ++++++---
 12 files changed, 158 insertions(+), 67 deletions(-)

Unionfs: use new path_* helpers instead of vfs_* helpers

fs/unionfs/commonfops.c +4 -1
index 0fc7963..e9878bb 100644
@@ -35,6 +35,7 @@ static int copyup_deleted_file(struct file *file, struct dentry *dentry,
 	struct dentry *tmp_dentry = NULL;
 	struct dentry *lower_dentry;
 	struct dentry *lower_dir_dentry = NULL;
+	struct path path;
 
 	lower_dentry = unionfs_lower_dentry_idx(dentry, bstart);
 
@@ -88,7 +89,9 @@ retry:
 					    lower_dentry->d_inode);
 	}
 	lower_dir_dentry = lock_parent(lower_dentry);
-	err = vfs_unlink(lower_dir_dentry->d_inode, lower_dentry);
+	path.dentry = lower_dir_dentry;
+	path.mnt = unionfs_lower_mnt_idx(dentry->d_parent, dbstart(dentry));
+	err = path_unlink(&path, lower_dentry);
 	unlock_dir(lower_dir_dentry);
 
 out:
fs/unionfs/copyup.c +31 -10

135 lines not on this page · raw message

fs/unionfs/dirhelper.c +8 -2
index 4b73bb6..7a8a654 100644
@@ -34,6 +34,7 @@ int do_delete_whiteouts(struct dentry *dentry, int bindex,
 	int i;
 	struct list_head *pos;
 	struct filldir_node *cursor;
+	struct path path;
 
 	/* Find out lower parent dentry */
 	lower_dir_dentry = unionfs_lower_dentry_idx(dentry, bindex);
@@ -69,8 +70,12 @@ int do_delete_whiteouts(struct dentry *dentry, int bindex,
 				err = PTR_ERR(lower_dentry);
 				break;
 			}
-			if (lower_dentry->d_inode)
-				err = vfs_unlink(lower_dir, lower_dentry);
+			if (lower_dentry->d_inode) {
+				path.dentry = lower_dir_dentry;
+				path.mnt = unionfs_lower_mnt_idx(dentry,
+								 bindex);
+				err = path_unlink(&path, lower_dentry);
+			}
 			dput(lower_dentry);
 			if (err)
 				break;
@@ -113,6 +118,7 @@ int delete_whiteouts(struct dentry *dentry, int bindex,
 	if (!permission(lower_dir, MAY_WRITE | MAY_EXEC, NULL)) {
 		err = do_delete_whiteouts(dentry, bindex, namelist);
 	} else {
+		/* __delete_whiteouts doesn't need args.path filled */
 		args.deletewh.namelist = namelist;
 		args.deletewh.dentry = dentry;
 		args.deletewh.bindex = bindex;
fs/unionfs/inode.c +49 -21

209 lines not on this page · raw message

fs/unionfs/lookup.c +7 -1
index 7f512c2..5bfbdf6 100644
@@ -52,6 +52,7 @@ static noinline_for_stack int is_opaque_dir(struct dentry *dentry, int bindex)
 			lookup_one_len(UNIONFS_DIR_OPAQUE, lower_dentry,
 				       sizeof(UNIONFS_DIR_OPAQUE) - 1);
 	} else {
+		/* __is_opaque_dir doesn't need args.path filled */
 		args.is_opaque.dentry = lower_dentry;
 		run_sioq(__is_opaque_dir, &args);
 		wh_lower_dentry = args.ret;
@@ -603,7 +604,8 @@ void update_bstart(struct dentry *dentry)
  * separate intent-structure, and (2) open_namei() is broken into a VFS-only
  * function and a method that other file systems can call.
  */
-int init_lower_nd(struct nameidata *nd, unsigned int flags)
+int init_lower_nd(struct nameidata *nd, struct dentry *dentry,
+		  struct vfsmount *mnt, unsigned int flags)
 {
 	int err = 0;
 #ifdef ALLOC_LOWER_ND_FILE
@@ -648,6 +650,10 @@ int init_lower_nd(struct nameidata *nd, unsigned int flags)
 		break;
 	}
 
+	/* initialize lower path (dentry+vfsmount) */
+	nd->path.dentry = dentry;
+	nd->path.mnt = mnt;
+
 	return err;
 }
 
fs/unionfs/rename.c +23 -11
index cc16eb2..8f9703a 100644
@@ -31,6 +31,7 @@ static int __unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
 	struct dentry *lower_wh_dir_dentry;
 	struct dentry *trap;
 	char *wh_name = NULL;
+	struct path path;
 
 	lower_new_dentry = unionfs_lower_dentry_idx(new_dentry, bindex);
 	lower_old_dentry = unionfs_lower_dentry_idx(old_dentry, bindex);
@@ -79,9 +80,11 @@ static int __unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
 
 		lower_wh_dir_dentry = lock_parent_wh(lower_wh_dentry);
 		err = is_robranch_super(old_dentry->d_sb, bindex);
-		if (!err)
-			err = vfs_unlink(lower_wh_dir_dentry->d_inode,
-					 lower_wh_dentry);
+		if (!err) {
+			path.dentry = lower_wh_dir_dentry;
+			path.mnt = unionfs_lower_mnt_idx(new_dentry, bindex);
+			err = path_unlink(&path, lower_wh_dentry);
+		}
 
 		dput(lower_wh_dentry);
 		unlock_dir(lower_wh_dir_dentry);
@@ -135,8 +138,10 @@ static int __unionfs_rename(struct inode *old_dir, struct dentry *old_dentry,
 		err = -ENOTEMPTY;
 		goto out_err_unlock;
 	}
-	err = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry,
-			 lower_new_dir_dentry->d_inode, lower_new_dentry);
+	path.dentry = lower_old_dir_dentry;
+	path.mnt = unionfs_lower_mnt_idx(old_dentry, bindex);
+	err = path_rename(&path, lower_old_dentry,
+			  lower_new_dir_dentry->d_inode, lower_new_dentry);
 out_err_unlock:
 	if (!err) {
 		/* update parent dir times */
@@ -220,9 +225,12 @@ static int do_unionfs_rename(struct inode *old_dir,
 
 		unlink_dir_dentry = lock_parent(unlink_dentry);
 		err = is_robranch_super(old_dir->i_sb, bindex);
-		if (!err)
-			err = vfs_unlink(unlink_dir_dentry->d_inode,
-					 unlink_dentry);
+		if (!err) {
+			struct path path;
+			path.dentry = unlink_dir_dentry;
+			path.mnt = unionfs_lower_mnt_idx(new_dentry, bindex);
+			err = path_unlink(&path, unlink_dentry);
+		}
 
 		fsstack_copy_attr_times(new_dentry->d_parent->d_inode,
 					unlink_dir_dentry->d_inode);
@@ -283,6 +291,8 @@ static int do_unionfs_rename(struct inode *old_dir,
 	if ((old_bstart != old_bend) || (do_copyup != -1)) {
 		struct dentry *lower_parent;
 		struct nameidata nd;
+		struct vfsmount *mnt;
+
 		if (!wh_old || wh_old->d_inode || bwh_old < 0) {
 			printk(KERN_ERR "unionfs: rename error "
 			       "(wh_old=%p/%p bwh_old=%d)\n", wh_old,
@@ -290,12 +300,14 @@ static int do_unionfs_rename(struct inode *old_dir,
 			err = -EIO;
 			goto out;
 		}
-		err = init_lower_nd(&nd, LOOKUP_CREATE);
+		mnt = unionfs_lower_mnt_idx(old_dentry, bwh_old);
+		err = init_lower_nd(&nd, NULL, /* set nd.path.dentry below */
+				    mnt, LOOKUP_CREATE);
 		if (unlikely(err < 0))
 			goto out;
 		lower_parent = lock_parent_wh(wh_old);
-		local_err = vfs_create(lower_parent->d_inode, wh_old, S_IRUGO,
-				       &nd);
+		nd.path.dentry = lower_parent;
+		local_err = path_create(&nd.path, wh_old, S_IRUGO, &nd);
 		unlock_dir(lower_parent);
 		if (!local_err) {
 			set_dbopaque(old_dentry, bwh_old);
fs/unionfs/sioq.c +5 -5
index 2a8c88e..2d94e85 100644
@@ -60,7 +60,7 @@ void __unionfs_create(struct work_struct *work)
 	struct sioq_args *args = container_of(work, struct sioq_args, work);
 	struct create_args *c = &args->create;
 
-	args->err = vfs_create(c->parent, c->dentry, c->mode, c->nd);
+	args->err = path_create(&args->path, c->dentry, c->mode, c->nd);
 	complete(&args->comp);
 }
 
@@ -69,7 +69,7 @@ void __unionfs_mkdir(struct work_struct *work)
 	struct sioq_args *args = container_of(work, struct sioq_args, work);
 	struct mkdir_args *m = &args->mkdir;
 
-	args->err = vfs_mkdir(m->parent, m->dentry, m->mode);
+	args->err = path_mkdir(&args->path, m->dentry, m->mode);
 	complete(&args->comp);
 }
 
@@ -78,7 +78,7 @@ void __unionfs_mknod(struct work_struct *work)
 	struct sioq_args *args = container_of(work, struct sioq_args, work);
 	struct mknod_args *m = &args->mknod;
 
-	args->err = vfs_mknod(m->parent, m->dentry, m->mode, m->dev);
+	args->err = path_mknod(&args->path, m->dentry, m->mode, m->dev);
 	complete(&args->comp);
 }
 
@@ -87,7 +87,7 @@ void __unionfs_symlink(struct work_struct *work)
 	struct sioq_args *args = container_of(work, struct sioq_args, work);
 	struct symlink_args *s = &args->symlink;
 
-	args->err = vfs_symlink(s->parent, s->dentry, s->symbuf, s->mode);
+	args->err = path_symlink(&args->path, s->dentry, s->symbuf, s->mode);
 	complete(&args->comp);
 }
 
@@ -96,7 +96,7 @@ void __unionfs_unlink(struct work_struct *work)
 	struct sioq_args *args = container_of(work, struct sioq_args, work);
 	struct unlink_args *u = &args->unlink;
 
-	args->err = vfs_unlink(u->parent, u->dentry);
+	args->err = path_unlink(&args->path, u->dentry);
 	complete(&args->comp);
 }
 
fs/unionfs/sioq.h +1 -0
index afb71ee..633b06a 100644
@@ -63,6 +63,7 @@ struct sioq_args {
 	struct work_struct work;
 	int err;
 	void *ret;
+	struct path path;
 
 	union {
 		struct deletewh_args deletewh;
fs/unionfs/subr.c +14 -8
index 1a40f63..dbe5e06 100644
@@ -26,12 +26,13 @@
 int create_whiteout(struct dentry *dentry, int start)
 {
 	int bstart, bend, bindex;
-	struct dentry *lower_dir_dentry;
+	struct dentry *lower_dir_dentry = NULL;
 	struct dentry *lower_dentry;
 	struct dentry *lower_wh_dentry;
 	struct nameidata nd;
 	char *name = NULL;
 	int err = -EINVAL;
+	struct vfsmount *mnt;
 
 	verify_locked(dentry);
 
@@ -87,16 +88,19 @@ int create_whiteout(struct dentry *dentry, int start)
 			goto out;
 		}
 
-		err = init_lower_nd(&nd, LOOKUP_CREATE);
+		mnt = unionfs_lower_mnt_idx(dentry->d_parent, bindex);
+		err = init_lower_nd(&nd, NULL, /* lower dentry set below */
+				    mnt, LOOKUP_CREATE);
 		if (unlikely(err < 0))
 			goto out;
 		lower_dir_dentry = lock_parent_wh(lower_wh_dentry);
+		nd.path.dentry = lower_dir_dentry;
 		err = is_robranch_super(dentry->d_sb, bindex);
 		if (!err)
-			err = vfs_create(lower_dir_dentry->d_inode,
-					 lower_wh_dentry,
-					 ~current->fs->umask & S_IRWXUGO,
-					 &nd);
+			err = path_create(&nd.path,
+					  lower_wh_dentry,
+					  ~current->fs->umask & S_IRWXUGO,
+					  &nd);
 		unlock_dir(lower_dir_dentry);
 		dput(lower_wh_dentry);
 		release_lower_nd(&nd, err);
@@ -189,11 +193,13 @@ int make_dir_opaque(struct dentry *dentry, int bindex)
 		goto out;
 	}
 
-	err = init_lower_nd(&nd, LOOKUP_CREATE);
+	err = init_lower_nd(&nd, lower_dentry,
+			    unionfs_lower_mnt_idx(dentry, bindex),
+			    LOOKUP_CREATE);
 	if (unlikely(err < 0))
 		goto out;
 	if (!diropq->d_inode)
-		err = vfs_create(lower_dir, diropq, S_IRUGO, &nd);
+		err = path_create(&nd.path, diropq, S_IRUGO, &nd);
 	if (!err)
 		set_dbopaque(dentry, bindex);
 	release_lower_nd(&nd, err);
fs/unionfs/union.h +2 -1
index 735d40c..681eab1 100644
@@ -303,7 +303,8 @@ static inline void unionfs_double_lock_dentry(struct dentry *d1,
 extern int new_dentry_private_data(struct dentry *dentry, int subclass);
 extern void free_dentry_private_data(struct dentry *dentry);
 extern void update_bstart(struct dentry *dentry);
-extern int init_lower_nd(struct nameidata *nd, unsigned int flags);
+extern int init_lower_nd(struct nameidata *nd, struct dentry *dentry,
+			 struct vfsmount *mnt, unsigned int flags);
 extern void release_lower_nd(struct nameidata *nd, int err);
 
 /*
fs/unionfs/unlink.c +6 -4
index cad0386..6216f8a 100644
@@ -67,14 +67,16 @@ static int unionfs_unlink_whiteout(struct inode *dir, struct dentry *dentry)
 		dget(lower_dentry);
 		err = is_robranch_super(dentry->d_sb, bindex);
 		if (!err) {
+			struct path path;
+			path.dentry = lower_dir_dentry;
+			path.mnt = unionfs_lower_mnt_idx(dentry->d_parent,
+							 bindex);
 			/* see Documentation/filesystems/unionfs/issues.txt */
 			lockdep_off();
 			if (!S_ISDIR(lower_dentry->d_inode->i_mode))
-				err = vfs_unlink(lower_dir_dentry->d_inode,
-								lower_dentry);
+				err = path_unlink(&path, lower_dentry);
 			else
-				err = vfs_rmdir(lower_dir_dentry->d_inode,
-								lower_dentry);
+				err = path_rmdir(&path, lower_dentry);
 			lockdep_on();
 		}
 
fs/unionfs/xattr.c +8 -3
index 8001c65..f168bea 100644
@@ -73,6 +73,7 @@ int unionfs_setxattr(struct dentry *dentry, const char *name,
 {
 	struct dentry *lower_dentry = NULL;
 	int err = -EOPNOTSUPP;
+	struct path path;
 
 	unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
 	unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
@@ -83,9 +84,10 @@ int unionfs_setxattr(struct dentry *dentry, const char *name,
 	}
 
 	lower_dentry = unionfs_lower_dentry(dentry);
+	path.dentry = lower_dentry;
+	path.mnt = unionfs_lower_mnt(dentry);
 
-	err = vfs_setxattr(lower_dentry, (char *) name, (void *) value,
-			   size, flags);
+	err = path_setxattr(&path, (char *) name, (void *) value, size, flags);
 
 out:
 	unionfs_check_dentry(dentry);
@@ -102,6 +104,7 @@ int unionfs_removexattr(struct dentry *dentry, const char *name)
 {
 	struct dentry *lower_dentry = NULL;
 	int err = -EOPNOTSUPP;
+	struct path path;
 
 	unionfs_read_lock(dentry->d_sb, UNIONFS_SMUTEX_CHILD);
 	unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD);
@@ -112,8 +115,10 @@ int unionfs_removexattr(struct dentry *dentry, const char *name)
 	}
 
 	lower_dentry = unionfs_lower_dentry(dentry);
+	path.dentry = lower_dentry;
+	path.mnt = unionfs_lower_mnt(dentry);
 
-	err = vfs_removexattr(lower_dentry, (char *) name);
+	err = path_removexattr(&path, (char *) name);
 
 out:
 	unionfs_check_dentry(dentry);
#39 lore raw

Re: [PATCH] Unionfs: use the new path_* VFS helpers

Quoted 7 lines

As promised, here is a patch for Unionfs to use the new path_* VFS helpers which now take a struct path *. I've used Al's vfs git tree plus all 12 patches you've outlined (ten path_* patches + 2 others). I've run Unionfs through my basic regression suite; I haven't done more extensive testing yet. This patch can be used in -mm if/when your path_* patches make it there (this patch will be a standalone one to be used on top of my unionfs.git tree).

Thanks.

Quoted 5 lines

Al, I'm not too pleased with the resulting additional code complexity to Unionfs, due to these new helpers. See the diffstat below. These VFS helpers have widened the gap between the f/s API and the VFS helpers': the helpers now need a struct path/vfsmount, but the most ->ops that a filesystem is being called with, don't have a path/vfsmount passed.

I'm not too pleased either ;)

What I think would be much cleaner if for example you had functions like this:

void unionfs_lower_path_index(struct dentry *dentry, int bindex,

				struct path *path_out);

Etc. If you are dealing with paths, you should not be handling vfsmounts separately. That part is ugly and just invitation for bugs.

Quoted 8 lines

You've said before that a filesystems has no business dealing with vfsmounts (with the exception of follow_link). Fine. But the irony now is that the new vfs helpers that a filesystems is likely to use, FORCE you to know a lot more about vfsmounts! If indeed a filesystem shouldn't have to know or deal with vfsmounts, then none of the VFS helpers should require a struct vfsmount (or a struct path, or a nameidata, which embed a vfsmount+dentry pair). Otherwise, let's admit that some filesystems have to know about vfsmounts and hence let's pass them to all appropriate fs ->ops.

How would that help? AFAICS the vfsmounts on the lower layers should have _absolutetly nothing_ to do with the vfsmount on the top layer.

So no, passing down vfsmounts to the filesystem is wrong (with the exception of follow_link, and even in that case only very specialized filesystems like proc will ever have a valid use for it).

Quoted 3 lines

(BTW, if
that's done, then it'd also be helpful for a struct vfsmount to have a "void *private" into which a stacked f/s can store "lower objects").

Oh, please! I think you are confused about the role that vfsmounts play in stacking. Vfsmounts place filesystems in the global and private mount namespaces. But each and every instance of that placement should behave exactly the same (modulo such flags as per-mount r/o, etc), and so stacking filesystems, just like any other filesystem, should have no need for the knowledge about where they are placed in the namespace.

On the other hand, they _should_ have knowledge of the placement of the filesystems they are operating on. So when accessing the lower layers, they can conform to the same rules as anything else accessing the filesystems. I.e. they shouldn't meddle directly with the filesystem, without going through the namespace layer first.

This is already enforced for dentry_open(), and I don't see the big issue with enfocing it for the rest of the vfs_* operations.

Miklos

#40 lore raw

PATCH 1/1 Call LSM functions outside VFS helper functions.

Hello.

Quoted 8 lines · Miklos Szeredi
Quoted 5 lines

Al, I'm not too pleased with the resulting additional code complexity to Unionfs, due to these new helpers. See the diffstat below. These VFS helpers have widened the gap between the f/s API and the VFS helpers': the helpers now need a struct path/vfsmount, but the most ->ops that a filesystem is being called with, don't have a path/vfsmount passed.

I'm not too pleased either ;)

I'm reading this thread on tenterhooks.
If vfsmount is passed to VFS helper functions, that will make r/o bind mounts and AppArmor and TOMOYO Linux happy.
But so far, it seems that "passing vfsmount makes things complicated".

If the conclusion became "vfsmount should not be passed to
VFS helper functions", that's OK, but I want you to consider the below approach for AppArmor and TOMOYO Linux. This patch is a repost of
http://kerneltrap.org/mailarchive/linux-fsdevel/2008/2/17/882024 .

Regards.
----------
Subject: Call LSM functions outside VFS helper functions.

This patch allows LSM to check permission using "struct vfsmount" without passing "struct vfsmount" to VFS helper functions. There is a side effect that conventional permission checks are done twice because I want to do DAC checks before MAC checks.

---
 fs/namei.c               |  176 ++++++++++++++++++++++++++++++++++++++---------
 fs/open.c                |   11 ++
 include/linux/fs.h       |    1 
 include/linux/security.h |   63 ++++++++++++++++
 net/unix/af_unix.c       |    9 ++
 security/dummy.c         |   17 ++++
 security/security.c      |   15 ++++
 7 files changed, 254 insertions(+), 38 deletions(-)
fs/namei.c +143 -33
@@ -2021,18 +2021,24 @@ fail:
 }
 EXPORT_SYMBOL_GPL(lookup_create);
 
-int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
+int pre_vfs_mknod(struct inode *dir, struct dentry *dentry, int mode)
 {
 	int error = may_create(dir, dentry, NULL);
-
 	if (error)
 		return error;
-
 	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
 		return -EPERM;
-
 	if (!dir->i_op || !dir->i_op->mknod)
 		return -EPERM;
+	return 0;
+}
+EXPORT_SYMBOL_GPL(pre_vfs_mknod);
+
+int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
+{
+	int error = pre_vfs_mknod(dir, dentry, mode);
+	if (error)
+		return error;
 
 	error = devcgroup_inode_mknod(mode, dev);
 	if (error)
@@ -2097,14 +2103,39 @@ asmlinkage long sys_mknodat(int dfd, con
 	if (error)
 		goto out_dput;
 	switch (mode & S_IFMT) {
+		int type;
 		case 0: case S_IFREG:
 			error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
 			break;
 		case S_IFCHR: case S_IFBLK:
+			error = pre_vfs_mknod(nd.path.dentry->d_inode, dentry,
+					      mode);
+			if (error)
+				break;
+			if (S_ISCHR(mode))
+				type = TYPE_MKCHAR_ACL;
+			else
+				type = TYPE_MKBLOCK_ACL;
+			error = security_singlepath_permission(type, dentry,
+							       nd.path.mnt);
+			if (error)
+				break;
 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
 					new_decode_dev(dev));
 			break;
 		case S_IFIFO: case S_IFSOCK:
+			error = pre_vfs_mknod(nd.path.dentry->d_inode, dentry,
+					      mode);
+			if (error)
+				break;
+			if (S_ISFIFO(mode))
+				type = TYPE_MKFIFO_ACL;
+			else
+				type = TYPE_MKSOCK_ACL;
+			error = security_singlepath_permission(type, dentry,
+							       nd.path.mnt);
+			if (error)
+				break;
 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
 			break;
 	}
@@ -2125,15 +2156,21 @@ asmlinkage long sys_mknod(const char __u
 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
 }
 
-int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
+static inline int pre_vfs_mkdir(struct inode *dir, struct dentry *dentry)
 {
 	int error = may_create(dir, dentry, NULL);
-
 	if (error)
 		return error;
-
 	if (!dir->i_op || !dir->i_op->mkdir)
 		return -EPERM;
+	return 0;
+}
+
+int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
+{
+	int error = pre_vfs_mkdir(dir, dentry);
+	if (error)
+		return error;
 
 	mode &= (S_IRWXUGO|S_ISVTX);
 	error = security_inode_mkdir(dir, dentry, mode);
@@ -2172,7 +2209,12 @@ asmlinkage long sys_mkdirat(int dfd, con
 	error = mnt_want_write(nd.path.mnt);
 	if (error)
 		goto out_dput;
-	error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
+	error = pre_vfs_mkdir(nd.path.dentry->d_inode, dentry);
+	if (!error)
+		error = security_singlepath_permission(TYPE_MKDIR_ACL, dentry,
+						       nd.path.mnt);
+	if (!error)
+		error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
 	mnt_drop_write(nd.path.mnt);
 out_dput:
 	dput(dentry);
@@ -2217,15 +2259,21 @@ void dentry_unhash(struct dentry *dentry
 	spin_unlock(&dcache_lock);
 }
 
-int vfs_rmdir(struct inode *dir, struct dentry *dentry)
+static inline int pre_vfs_rmdir(struct inode *dir, struct dentry *dentry)
 {
 	int error = may_delete(dir, dentry, 1);
-
 	if (error)
 		return error;
-
 	if (!dir->i_op || !dir->i_op->rmdir)
 		return -EPERM;
+	return 0;
+}
+
+int vfs_rmdir(struct inode *dir, struct dentry *dentry)
+{
+	int error = pre_vfs_rmdir(dir, dentry);
+	if (error)
+		return error;
 
 	DQUOT_INIT(dir);
 
@@ -2284,7 +2332,12 @@ static long do_rmdir(int dfd, const char
 	error = mnt_want_write(nd.path.mnt);
 	if (error)
 		goto exit3;
-	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
+	error = pre_vfs_rmdir(nd.path.dentry->d_inode, dentry);
+	if (!error)
+		error = security_singlepath_permission(TYPE_RMDIR_ACL, dentry,
+						       nd.path.mnt);
+	if (!error)
+		error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
 	mnt_drop_write(nd.path.mnt);
 exit3:
 	dput(dentry);
@@ -2302,15 +2355,21 @@ asmlinkage long sys_rmdir(const char __u
 	return do_rmdir(AT_FDCWD, pathname);
 }
 
-int vfs_unlink(struct inode *dir, struct dentry *dentry)
+static inline int pre_vfs_unlink(struct inode *dir, struct dentry *dentry)
 {
 	int error = may_delete(dir, dentry, 0);
-
 	if (error)
 		return error;
-
 	if (!dir->i_op || !dir->i_op->unlink)
 		return -EPERM;
+	return 0;
+}
+
+int vfs_unlink(struct inode *dir, struct dentry *dentry)
+{
+	int error = pre_vfs_unlink(dir, dentry);
+	if (error)
+		return error;
 
 	DQUOT_INIT(dir);
 
@@ -2370,7 +2429,13 @@ static long do_unlinkat(int dfd, const c
 		error = mnt_want_write(nd.path.mnt);
 		if (error)
 			goto exit2;
-		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
+		error = pre_vfs_unlink(nd.path.dentry->d_inode, dentry);
+		if (!error)
+			error = security_singlepath_permission(TYPE_UNLINK_ACL,
+							       dentry,
+							       nd.path.mnt);
+		if (!error)
+			error = vfs_unlink(nd.path.dentry->d_inode, dentry);
 		mnt_drop_write(nd.path.mnt);
 	exit2:
 		dput(dentry);
@@ -2406,15 +2471,22 @@ asmlinkage long sys_unlink(const char __
 	return do_unlinkat(AT_FDCWD, pathname);
 }
 
-int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
+static inline int pre_vfs_symlink(struct inode *dir, struct dentry *dentry)
 {
 	int error = may_create(dir, dentry, NULL);
-
 	if (error)
 		return error;
-
 	if (!dir->i_op || !dir->i_op->symlink)
 		return -EPERM;
+	return 0;
+}
+
+int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname,
+		int mode)
+{
+	int error = pre_vfs_symlink(dir, dentry);
+	if (error)
+		return error;
 
 	error = security_inode_symlink(dir, dentry, oldname);
 	if (error)
@@ -2455,7 +2527,13 @@ asmlinkage long sys_symlinkat(const char
 	error = mnt_want_write(nd.path.mnt);
 	if (error)
 		goto out_dput;
-	error = vfs_symlink(nd.path.dentry->d_inode, dentry, from, S_IALLUGO);
+	error = pre_vfs_symlink(nd.path.dentry->d_inode, dentry);
+	if (!error)
+		error = security_singlepath_permission(TYPE_SYMLINK_ACL,
+						       dentry, nd.path.mnt);
+	if (!error)
+		error = vfs_symlink(nd.path.dentry->d_inode, dentry, from,
+				    S_IALLUGO);
 	mnt_drop_write(nd.path.mnt);
 out_dput:
 	dput(dentry);
@@ -2474,21 +2552,18 @@ asmlinkage long sys_symlink(const char _
 	return sys_symlinkat(oldname, AT_FDCWD, newname);
 }
 
-int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
+static inline int pre_vfs_link(struct dentry *old_dentry, struct inode *dir,
+			       struct dentry *new_dentry)
 {
 	struct inode *inode = old_dentry->d_inode;
 	int error;
-
 	if (!inode)
 		return -ENOENT;
-
 	error = may_create(dir, new_dentry, NULL);
 	if (error)
 		return error;
-
 	if (dir->i_sb != inode->i_sb)
 		return -EXDEV;
-
 	/*
 	 * A link to an append-only or immutable file cannot be created.
 	 */
@@ -2498,6 +2573,15 @@ int vfs_link(struct dentry *old_dentry, 
 		return -EPERM;
 	if (S_ISDIR(old_dentry->d_inode->i_mode))
 		return -EPERM;
+	return 0;
+}
+
+int vfs_link(struct dentry *old_dentry, struct inode *dir,
+	     struct dentry *new_dentry)
+{
+	int error = pre_vfs_link(old_dentry, dir, new_dentry);
+	if (error)
+		return error;
 
 	error = security_inode_link(old_dentry, dir, new_dentry);
 	if (error)
@@ -2555,7 +2639,16 @@ asmlinkage long sys_linkat(int olddfd, c
 	error = mnt_want_write(nd.path.mnt);
 	if (error)
 		goto out_dput;
-	error = vfs_link(old_nd.path.dentry, nd.path.dentry->d_inode, new_dentry);
+	error = pre_vfs_link(old_nd.path.dentry, nd.path.dentry->d_inode,
+			     new_dentry);
+	if (!error)
+		error = security_doublepath_permission(TYPE_LINK_ACL,
+						       old_nd.path.dentry,
+						       nd.path.dentry,
+						       old_nd.path.mnt);
+	if (!error)
+		error = vfs_link(old_nd.path.dentry, nd.path.dentry->d_inode,
+				 new_dentry);
 	mnt_drop_write(nd.path.mnt);
 out_dput:
 	dput(new_dentry);
@@ -2679,20 +2772,18 @@ static int vfs_rename_other(struct inode
 	return error;
 }
 
-int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
-	       struct inode *new_dir, struct dentry *new_dentry)
+static inline int pre_vfs_rename(struct inode *old_dir,
+				 struct dentry *old_dentry,
+				 struct inode *new_dir,
+				 struct dentry *new_dentry)
 {
 	int error;
 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
-	const char *old_name;
-
 	if (old_dentry->d_inode == new_dentry->d_inode)
  		return 0;
- 
 	error = may_delete(old_dir, old_dentry, is_dir);
 	if (error)
 		return error;
-
 	if (!new_dentry->d_inode)
 		error = may_create(new_dir, new_dentry, NULL);
 	else
@@ -2702,6 +2793,17 @@ int vfs_rename(struct inode *old_dir, st
 
 	if (!old_dir->i_op || !old_dir->i_op->rename)
 		return -EPERM;
+	return 0;
+}
+
+int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
+	       struct inode *new_dir, struct dentry *new_dentry)
+{
+	int error = pre_vfs_rename(old_dir, old_dentry, new_dir, new_dentry);
+	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
+	const char *old_name;
+	if (error)
+		return error;
 
 	DQUOT_INIT(old_dir);
 	DQUOT_INIT(new_dir);
@@ -2786,7 +2888,15 @@ static int do_rename(int olddfd, const c
 	error = mnt_want_write(oldnd.path.mnt);
 	if (error)
 		goto exit5;
-	error = vfs_rename(old_dir->d_inode, old_dentry,
+	error = pre_vfs_rename(old_dir->d_inode, old_dentry, new_dir->d_inode,
+			       new_dentry);
+	if (!error)
+		error = security_doublepath_permission(TYPE_RENAME_ACL,
+						       oldnd.path.dentry,
+						       newnd.path.dentry,
+						       oldnd.path.mnt);
+	if (!error)
+		error = vfs_rename(old_dir->d_inode, old_dentry,
 				   new_dir->d_inode, new_dentry);
 	mnt_drop_write(oldnd.path.mnt);
 exit5:
include/linux/security.h +62 -1

93 lines not on this page · raw message

security/dummy.c +17 -0
@@ -286,6 +286,21 @@ static int dummy_inode_init_security (st
 	return -EOPNOTSUPP;
 }
 
+static inline int dummy_singlepath_permission(int operation,
+					      struct dentry *dentry,
+					      struct vfsmount *mnt)
+{
+	return 0;
+}
+
+static inline int dummy_doublepath_permission(int operation,
+					      struct dentry *old_dentry,
+					      struct dentry *new_dentry,
+					      struct vfsmount *mnt)
+{
+	return 0;
+}
+
 static int dummy_inode_create (struct inode *inode, struct dentry *dentry,
 			       int mask)
 {
@@ -1080,6 +1095,8 @@ void security_fixup_ops (struct security
 	set_to_dummy_if_null(ops, inode_alloc_security);
 	set_to_dummy_if_null(ops, inode_free_security);
 	set_to_dummy_if_null(ops, inode_init_security);
+	set_to_dummy_if_null(ops, singlepath_permission);
+	set_to_dummy_if_null(ops, doublepath_permission);
 	set_to_dummy_if_null(ops, inode_create);
 	set_to_dummy_if_null(ops, inode_link);
 	set_to_dummy_if_null(ops, inode_unlink);
security/security.c +15 -0
@@ -388,6 +388,21 @@ int security_inode_init_security(struct 
 }
 EXPORT_SYMBOL(security_inode_init_security);
 
+int security_singlepath_permission(int operation, struct dentry *dentry,
+				   struct vfsmount *mnt)
+{
+	return security_ops->singlepath_permission(operation, dentry, mnt);
+}
+EXPORT_SYMBOL(security_singlepath_permission);
+
+int security_doublepath_permission(int operation, struct dentry *old_dentry,
+				   struct dentry *new_dentry,
+				   struct vfsmount *mnt)
+{
+	return security_ops->doublepath_permission(operation, old_dentry,
+						   new_dentry, mnt);
+}
+
 int security_inode_create(struct inode *dir, struct dentry *dentry, int mode)
 {
 	if (unlikely(IS_PRIVATE(dir)))
net/unix/af_unix.c +8 -1
@@ -822,7 +822,14 @@ static int unix_bind(struct socket *sock
 		err = mnt_want_write(nd.path.mnt);
 		if (err)
 			goto out_mknod_dput;
-		err = vfs_mknod(nd.path.dentry->d_inode, dentry, mode, 0);
+		err = pre_vfs_mknod(nd.path.dentry->d_inode, dentry, mode);
+		if (!err)
+			err = security_singlepath_permission(TYPE_MKSOCK_ACL,
+							     dentry,
+							     nd.path.mnt);
+		if (!err)
+			err = vfs_mknod(nd.path.dentry->d_inode, dentry,
+					mode, 0);
 		mnt_drop_write(nd.path.mnt);
 		if (err)
 			goto out_mknod_dput;
include/linux/fs.h +1 -0
@@ -1125,6 +1125,7 @@ extern void unlock_super(struct super_bl
 extern int vfs_permission(struct nameidata *, int);
 extern int vfs_create(struct inode *, struct dentry *, int, struct nameidata *);
 extern int vfs_mkdir(struct inode *, struct dentry *, int);
+extern int pre_vfs_mknod(struct inode *, struct dentry *, int);
 extern int vfs_mknod(struct inode *, struct dentry *, int, dev_t);
 extern int vfs_symlink(struct inode *, struct dentry *, const char *, int);
 extern int vfs_link(struct dentry *, struct inode *, struct dentry *);
fs/open.c +8 -3
@@ -267,8 +267,10 @@ static long do_sys_truncate(const char _
 	error = break_lease(inode, FMODE_WRITE);
 	if (error)
 		goto put_write_and_out;
-
-	error = locks_verify_truncate(inode, NULL, length);
+	error = security_singlepath_permission(TYPE_TRUNCATE_ACL,
+					       nd.path.dentry, nd.path.mnt);
+	if (!error)
+		error = locks_verify_truncate(inode, NULL, length);
 	if (!error) {
 		DQUOT_INIT(inode);
 		error = do_truncate(nd.path.dentry, length, 0, NULL);
@@ -324,7 +326,10 @@ static long do_sys_ftruncate(unsigned in
 	if (IS_APPEND(inode))
 		goto out_putf;
 
-	error = locks_verify_truncate(inode, file, length);
+	error = security_singlepath_permission(TYPE_TRUNCATE_ACL, dentry,
+					       file->f_path.mnt);
+	if (!error)
+		error = locks_verify_truncate(inode, file, length);
 	if (!error)
 		error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, file);
 out_putf:
#41 lore raw

Re: [PATCH] Call LSM functions outside VFS helper functions.

Quoted 4 lines · Tetsuo Handa

If the conclusion became "vfsmount should not be passed to
VFS helper functions", that's OK, but I want you to consider the below approach for AppArmor and TOMOYO Linux. This patch is a repost of
http://kerneltrap.org/mailarchive/linux-fsdevel/2008/2/17/882024 .

I'm a little disappointed that you simply repost this patch rather than responding to my post from yesterday:

Quoted 10 lines

How about an approach which doesn't require the vfsmount to be passed down?

When the rule is put in place, say "No modifications to /etc/passwd", look up the inode and major:minor of /etc/passwd. If there's a rename, look up the new inode number. If it's mounted elsewhere, it doesn't matter, they still can't modify it because it has the same major:minor:inode.

Is this workable?

Could you respond to this please?

Signature · 4 lines
Intel are signing my paycheques ... these opinions are still mine
"Bill, look, we understand that you're interested in selling us this
operating system, but compare it to ours.  We can't possibly take such
a retrograde step."
#42 lore raw

Re: [PATCH] Call LSM functions outside VFS helper functions.

In reply to Tetsuo Handa

Hi,

Quoted 4 lines

I'm reading this thread on tenterhooks.
If vfsmount is passed to VFS helper functions, that will make r/o bind mounts and AppArmor and TOMOYO Linux happy.
But so far, it seems that "passing vfsmount makes things complicated".

If done right, it shouldn't. At least it didn't seem to complicate the parts that I've touched ;)

Quoted 4 lines

If the conclusion became "vfsmount should not be passed to
VFS helper functions", that's OK, but I want you to consider the below approach for AppArmor and TOMOYO Linux. This patch is a repost of
http://kerneltrap.org/mailarchive/linux-fsdevel/2008/2/17/882024 .

I'm not a big fan of adding new security hooks. Nor of moving the existing ones to callers.

Miklos