Subject: [PATCH 6/11] reiserfs: replace reiserfs_permission with generic_permission
From: Jeff Mahoney <jeffm@suse.com>

 With the horrible locking requirements gone, we don't need to have a locked
 and unlocked version of reiserfs permission. We can also use the generic
 permission code and not have to duplicate it all ourselves.

Signed-off-by: Jeff Mahoney <jeffm@suse.com>
diff -ruNpX ../dontdiff linux-2.6.13-rc3.4/fs/reiserfs/xattr_acl.c linux-2.6.13-rc3.5/fs/reiserfs/xattr_acl.c
--- linux-2.6.13-rc3.4/fs/reiserfs/xattr_acl.c	2005-07-26 13:04:25.000000000 -0400
+++ linux-2.6.13-rc3.5/fs/reiserfs/xattr_acl.c	2005-07-26 13:04:41.000000000 -0400
@@ -550,3 +550,17 @@ struct reiserfs_xattr_handler posix_acl_
 	.del = posix_acl_default_del,
 	.list = posix_acl_default_list,
 };
+
+int reiserfs_check_acl(struct inode *inode, int mask)
+{
+	struct posix_acl *acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
+	if (acl && IS_ERR(acl))
+		return PTR_ERR(acl);
+
+	if (acl) {
+		int error = posix_acl_permission(inode, acl, mask);
+		posix_acl_release(acl);
+		return error;
+	}
+	return -EAGAIN;
+}
diff -ruNpX ../dontdiff linux-2.6.13-rc3.4/fs/reiserfs/xattr.c linux-2.6.13-rc3.5/fs/reiserfs/xattr.c
--- linux-2.6.13-rc3.4/fs/reiserfs/xattr.c	2005-07-26 13:10:15.000000000 -0400
+++ linux-2.6.13-rc3.5/fs/reiserfs/xattr.c	2005-07-26 13:11:24.000000000 -0400
@@ -1186,101 +1186,17 @@ int reiserfs_xattr_init(struct super_blo
 	return err;
 }
 
-static int
-__reiserfs_permission(struct inode *inode, int mask, struct nameidata *nd,
-		      int need_lock)
+int reiserfs_permission(struct inode *inode, int mask, struct nameidata *nd)
 {
-	umode_t mode = inode->i_mode;
-
-	if (mask & MAY_WRITE) {
-		/*
-		 * Nobody gets write access to a read-only fs.
-		 */
-		if (IS_RDONLY(inode) &&
-		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
-			return -EROFS;
-
-		/*
-		 * Nobody gets write access to an immutable file.
-		 */
-		if (IS_IMMUTABLE(inode))
-			return -EACCES;
-	}
+	int (*check_acl)(struct inode *inode, int mask) = NULL;
 
 	/* We don't do permission checks on the internal objects.
 	 * Permissions are determined by the "owning" object. */
 	if (is_reiserfs_priv_object(inode))
 		return 0;
 
-	if (current->fsuid == inode->i_uid) {
-		mode >>= 6;
-#ifdef CONFIG_REISERFS_FS_POSIX_ACL
-	} else if (reiserfs_posixacl(inode->i_sb) &&
-		   get_inode_sd_version(inode) != STAT_DATA_V1) {
-		struct posix_acl *acl;
-
-		/* ACL can't contain additional permissions if
-		   the ACL_MASK entry is 0 */
-		if (!(mode & S_IRWXG))
-			goto check_groups;
-
-		acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
-		if (IS_ERR(acl)) {
-			if (PTR_ERR(acl) == -ENODATA)
-				goto check_groups;
-			return PTR_ERR(acl);
-		}
-
-		if (acl) {
-			int err = posix_acl_permission(inode, acl, mask);
-			posix_acl_release(acl);
-			if (err == -EACCES) {
-				goto check_capabilities;
-			}
-			return err;
-		} else {
-			goto check_groups;
-		}
-#endif
-	} else {
-	      check_groups:
-		if (in_group_p(inode->i_gid))
-			mode >>= 3;
-	}
-
-	/*
-	 * If the DACs are ok we don't need any capability check.
-	 */
-	if (((mode & mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == mask))
-		return 0;
-
-      check_capabilities:
-	/*
-	 * Read/write DACs are always overridable.
-	 * Executable DACs are overridable if at least one exec bit is set.
-	 */
-	if (!(mask & MAY_EXEC) ||
-	    (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
-		if (capable(CAP_DAC_OVERRIDE))
-			return 0;
-
-	/*
-	 * Searching includes executable on directories, else just read.
-	 */
-	if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
-		if (capable(CAP_DAC_READ_SEARCH))
-			return 0;
-
-	return -EACCES;
-}
-
-int reiserfs_permission(struct inode *inode, int mask, struct nameidata *nd)
-{
-	return __reiserfs_permission(inode, mask, nd, 1);
-}
-
-int
-reiserfs_permission_locked(struct inode *inode, int mask, struct nameidata *nd)
-{
-	return __reiserfs_permission(inode, mask, nd, 0);
+	if (get_inode_sd_version(inode) != STAT_DATA_V1)
+		check_acl = reiserfs_check_acl;
+	
+	return generic_permission (inode, mask, check_acl);
 }
diff -ruNpX ../dontdiff linux-2.6.13-rc3.4/fs/reiserfs/xattr_user.c linux-2.6.13-rc3.5/fs/reiserfs/xattr_user.c
--- linux-2.6.13-rc3.4/fs/reiserfs/xattr_user.c	2005-07-18 16:25:03.000000000 -0400
+++ linux-2.6.13-rc3.5/fs/reiserfs/xattr_user.c	2005-07-26 13:04:41.000000000 -0400
@@ -24,7 +24,7 @@ user_get(struct inode *inode, const char
 	if (!reiserfs_xattrs_user(inode->i_sb))
 		return -EOPNOTSUPP;
 
-	error = reiserfs_permission_locked(inode, MAY_READ, NULL);
+	error = permission(inode, MAY_READ, NULL);
 	if (error)
 		return error;
 
@@ -48,7 +48,7 @@ user_set(struct inode *inode, const char
 	    (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
 		return -EPERM;
 
-	error = reiserfs_permission_locked(inode, MAY_WRITE, NULL);
+	error = permission(inode, MAY_WRITE, NULL);
 	if (error)
 		return error;
 
@@ -69,7 +69,7 @@ static int user_del(struct inode *inode,
 	    (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
 		return -EPERM;
 
-	error = reiserfs_permission_locked(inode, MAY_WRITE, NULL);
+	error = permission(inode, MAY_WRITE, NULL);
 	if (error)
 		return error;
 
diff -ruNpX ../dontdiff linux-2.6.13-rc3.4/include/linux/reiserfs_acl.h linux-2.6.13-rc3.5/include/linux/reiserfs_acl.h
--- linux-2.6.13-rc3.4/include/linux/reiserfs_acl.h	2005-07-18 16:25:05.000000000 -0400
+++ linux-2.6.13-rc3.5/include/linux/reiserfs_acl.h	2005-07-26 13:04:41.000000000 -0400
@@ -52,12 +52,15 @@ int reiserfs_acl_chmod(struct inode *ino
 int reiserfs_inherit_default_acl(struct inode *dir, struct dentry *dentry,
 				 struct inode *inode);
 int reiserfs_cache_default_acl(struct inode *dir);
+int reiserfs_check_acl(struct inode *inode, int mask);
+
 extern int reiserfs_xattr_posix_acl_init(void) __init;
 extern int reiserfs_xattr_posix_acl_exit(void);
 extern struct reiserfs_xattr_handler posix_acl_default_handler;
 extern struct reiserfs_xattr_handler posix_acl_access_handler;
 #else
 
+#define reiserfs_check_acl NULL
 #define reiserfs_get_acl NULL
 #define reiserfs_cache_default_acl(inode) 0
 
