Subject: [PATCH 9/11] reiserfs: journaled xattrs
From: Jeff Mahoney <jeffm@suse.com>

 Deadlocks are possible in the xattr code between the journal lock and the
 xattr sems.

 This patch implements journalling for xattr operations. The benefit is
 twofold:
 * It gets rid of the deadlock possibility by always ensuring that xattr
   write operations are initiated inside a transaction.
 * It corrects the problem where xattr backing files aren't considered any
   differently than normal files, despite the fact they are metadata.

 I discussed the added journal load with Chris Mason, and we decided that
 since xattrs (versus other journal activity) is fairly rare, the introduction
 of larger transactions to support journaled xattrs wouldn't be too big a deal.

Signed-off-by: Jeff Mahoney <jeffm@suse.com>
diff -ruNpX ../dontdiff linux-2.6.13-rc3.7/fs/reiserfs/inode.c linux-2.6.13-rc3.8/fs/reiserfs/inode.c
--- linux-2.6.13-rc3.7/fs/reiserfs/inode.c	2005-07-26 13:03:45.000000000 -0400
+++ linux-2.6.13-rc3.8/fs/reiserfs/inode.c	2005-07-26 13:05:13.000000000 -0400
@@ -39,12 +39,13 @@ void reiserfs_delete_inode(struct inode 
 	if (!(inode->i_state & I_NEW) && INODE_PKEY(inode)->k_objectid != 0) {	/* also handles bad_inode case */
 		down(&inode->i_sem);
 
-		reiserfs_delete_xattrs(inode);
-
 		if (journal_begin(&th, inode->i_sb, jbegin_count)) {
 			up(&inode->i_sem);
 			goto out;
 		}
+
+		reiserfs_delete_xattrs(inode);
+
 		reiserfs_update_inode_transaction(inode);
 
 		if (reiserfs_delete_object(&th, inode)) {
@@ -1937,9 +1938,8 @@ int reiserfs_new_inode(struct reiserfs_t
 		goto out_inserted_sd;
 	}
 
-	/* XXX CHECK THIS */
 	if (reiserfs_posixacl(inode->i_sb)) {
-		retval = reiserfs_inherit_default_acl(dir, dentry, inode);
+		retval = reiserfs_inherit_default_acl(th, dir, dentry, inode);
 		if (retval) {
 			err = retval;
 			reiserfs_check_path(&path_to_key);
diff -ruNpX ../dontdiff linux-2.6.13-rc3.7/fs/reiserfs/namei.c linux-2.6.13-rc3.8/fs/reiserfs/namei.c
--- linux-2.6.13-rc3.7/fs/reiserfs/namei.c	2005-07-26 13:03:45.000000000 -0400
+++ linux-2.6.13-rc3.8/fs/reiserfs/namei.c	2005-07-26 13:05:13.000000000 -0400
@@ -616,15 +616,13 @@ static int reiserfs_create(struct inode 
 	    2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
 		 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
 	struct reiserfs_transaction_handle th;
-	int locked;
 
 	if (!(inode = new_inode(dir->i_sb))) {
 		return -ENOMEM;
 	}
 	new_inode_init(inode, dir, mode);
 
-	locked = reiserfs_cache_default_acl(dir);
-
+	jbegin_count += reiserfs_cache_default_acl(dir);
 	reiserfs_write_lock(dir->i_sb);
 
 	retval = journal_begin(&th, dir->i_sb, jbegin_count);
@@ -678,7 +676,6 @@ static int reiserfs_mknod(struct inode *
 	    JOURNAL_PER_BALANCE_CNT * 3 +
 	    2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
 		 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
-	int locked;
 
 	if (!new_valid_dev(rdev))
 		return -EINVAL;
@@ -688,8 +685,7 @@ static int reiserfs_mknod(struct inode *
 	}
 	new_inode_init(inode, dir, mode);
 
-	locked = reiserfs_cache_default_acl(dir);
-
+	jbegin_count += reiserfs_cache_default_acl(dir);
 	reiserfs_write_lock(dir->i_sb);
 
 	retval = journal_begin(&th, dir->i_sb, jbegin_count);
@@ -746,7 +742,6 @@ static int reiserfs_mkdir(struct inode *
 	    JOURNAL_PER_BALANCE_CNT * 3 +
 	    2 * (REISERFS_QUOTA_INIT_BLOCKS(dir->i_sb) +
 		 REISERFS_QUOTA_TRANS_BLOCKS(dir->i_sb));
-	int locked;
 
 #ifdef DISPLACE_NEW_PACKING_LOCALITIES
 	/* set flag that new packing locality created and new blocks for the content     * of that directory are not displaced yet */
@@ -758,8 +753,7 @@ static int reiserfs_mkdir(struct inode *
 	}
 	new_inode_init(inode, dir, mode);
 
-	locked = reiserfs_cache_default_acl(dir);
-
+	jbegin_count += reiserfs_cache_default_acl(dir);
 	reiserfs_write_lock(dir->i_sb);
 
 	retval = journal_begin(&th, dir->i_sb, jbegin_count);
@@ -1046,8 +1040,6 @@ static int reiserfs_symlink(struct inode
 	memcpy(name, symname, strlen(symname));
 	padd_item(name, item_len, strlen(symname));
 
-	/* We would inherit the default ACL here, but symlinks don't get ACLs */
-
 	retval = journal_begin(&th, parent_dir->i_sb, jbegin_count);
 	if (retval) {
 		drop_new_inode(inode);
diff -ruNpX ../dontdiff linux-2.6.13-rc3.7/fs/reiserfs/xattr_acl.c linux-2.6.13-rc3.8/fs/reiserfs/xattr_acl.c
--- linux-2.6.13-rc3.7/fs/reiserfs/xattr_acl.c	2005-07-26 13:05:02.000000000 -0400
+++ linux-2.6.13-rc3.8/fs/reiserfs/xattr_acl.c	2005-07-26 13:05:13.000000000 -0400
@@ -9,15 +9,17 @@
 #include <linux/reiserfs_acl.h>
 #include <asm/uaccess.h>
 
-static int reiserfs_set_acl(struct inode *inode, int type,
+static int reiserfs_set_acl(struct reiserfs_transaction_handle *th,
+                            struct inode *inode, int type,
 			    struct posix_acl *acl);
 
 static int
 xattr_set_acl(struct inode *inode, int type, const void *value, size_t size)
 {
 	struct posix_acl *acl;
-	int error;
-
+	int error, error2;
+	struct reiserfs_transaction_handle th;
+	size_t jcreate_blocks;
 	if (!reiserfs_posixacl(inode->i_sb))
 		return -EOPNOTSUPP;
 	if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
@@ -35,7 +37,21 @@ xattr_set_acl(struct inode *inode, int t
 	} else
 		acl = NULL;
 
-	error = reiserfs_set_acl(inode, type, acl);
+	/* Pessimism: We can't assume that anything from the xattr root up
+	 * has been created. */
+
+	jcreate_blocks = reiserfs_xattr_jcreate_nblocks(inode) +
+	                 reiserfs_xattr_nblocks(inode, size) * 2;
+
+	reiserfs_write_lock(inode->i_sb);
+	error = journal_begin(&th, inode->i_sb, jcreate_blocks);
+
+	error = reiserfs_set_acl(&th, inode, type, acl);
+
+	error2 = journal_end(&th, inode->i_sb, jcreate_blocks);
+	if (!error)
+		error = error2;
+	reiserfs_write_unlock(inode->i_sb);
 
       release_and_out:
 	posix_acl_release(acl);
@@ -265,7 +283,8 @@ struct posix_acl *reiserfs_get_acl(struc
  * BKL held [before 2.5.x]
  */
 static int
-reiserfs_set_acl(struct inode *inode, int type, struct posix_acl *acl)
+reiserfs_set_acl(struct reiserfs_transaction_handle *th, struct inode *inode,
+                 int type, struct posix_acl *acl)
 {
 	char *name;
 	void *value = NULL;
@@ -310,10 +329,9 @@ reiserfs_set_acl(struct inode *inode, in
 			return (int)PTR_ERR(value);
 	}
 
-	error = reiserfs_xattr_set(inode, name, value, size, 0);
+	error = reiserfs_xattr_set_handle(th, inode, name, value, size, 0);
 
-	if (value)
-		kfree(value);
+	kfree(value);
 
 	if (!error)
 		iset_acl(inode, p_acl, acl);
@@ -324,7 +342,8 @@ reiserfs_set_acl(struct inode *inode, in
 /* dir->i_sem: down,
  * inode is new and not released into the wild yet */
 int
-reiserfs_inherit_default_acl(struct inode *dir, struct dentry *dentry,
+reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th,
+                             struct inode *dir, struct dentry *dentry,
 			     struct inode *inode)
 {
 	struct posix_acl *acl;
@@ -361,7 +380,8 @@ reiserfs_inherit_default_acl(struct inod
 
 		/* Copy the default ACL to the default ACL of a new directory */
 		if (S_ISDIR(inode->i_mode)) {
-			err = reiserfs_set_acl(inode, ACL_TYPE_DEFAULT, acl);
+			err = reiserfs_set_acl(th, inode, ACL_TYPE_DEFAULT,
+			                       acl);
 			if (err)
 				goto cleanup;
 		}
@@ -382,9 +402,9 @@ reiserfs_inherit_default_acl(struct inod
 
 			/* If we need an ACL.. */
 			if (need_acl > 0) {
-				err =
-				    reiserfs_set_acl(inode, ACL_TYPE_ACCESS,
-						     acl_copy);
+				err = reiserfs_set_acl(th, inode,
+				                              ACL_TYPE_ACCESS,
+                                                              acl_copy);
 				if (err)
 					goto cleanup_copy;
 			}
@@ -402,20 +422,42 @@ reiserfs_inherit_default_acl(struct inod
 	return err;
 }
 
-/* Looks up and caches the result of the default ACL.
- * We do this so that we don't need to carry the xattr_sem into
- * reiserfs_new_inode if we don't need to */
+/* This is used to cache the default acl before a new object is created.
+ * The biggest reason for this is to get an idea of how many blocks will
+ * actually be required for the create operation if we must inherit an ACL.
+ * An ACL write can add up to 3 object creations and an additional file write
+ * so we'd prefer not to reserve that many blocks in the journal if we can.
+ * It also has the advantage of not loading the ACL with a transaction open,
+ * this may seem silly, but if the owner of the directory is doing the
+ * creation, the ACL may not be loaded since the permissions wouldn't require
+ * it.
+ * We return the number of blocks required for the transaction.
+ */
 int reiserfs_cache_default_acl(struct inode *inode)
 {
-	int ret = 0;
-	if (reiserfs_posixacl(inode->i_sb) && !is_reiserfs_priv_object(inode)) {
-		struct posix_acl *acl;
-		acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT);
-		ret = acl ? 1 : 0;
+	struct posix_acl *acl;
+	int nblocks = 0;
+
+	if (is_reiserfs_priv_object(inode))
+		return 0;
+
+	acl = reiserfs_get_acl(inode, ACL_TYPE_DEFAULT);
+
+	if (acl && !IS_ERR(acl)) {
+		int size = reiserfs_acl_size(acl->a_count);
+
+		/* xattr root must already exist, so we need xattr inode + 
+		 * 2 files (access acl and default acl) */
+		nblocks = (JOURNAL_PER_BALANCE_CNT * 3 + 2 *
+		           (REISERFS_QUOTA_INIT_BLOCKS(inode->i_sb) +
+		            REISERFS_QUOTA_TRANS_BLOCKS(inode->i_sb))) * 3;
+
+		/* We need to account for writes + bitmaps for two files */
+		nblocks += reiserfs_xattr_nblocks(inode, size) * 4;
 		posix_acl_release(acl);
 	}
 
-	return ret;
+	return nblocks;
 }
 
 int reiserfs_acl_chmod(struct inode *inode)
@@ -441,8 +483,18 @@ int reiserfs_acl_chmod(struct inode *ino
 	if (!clone)
 		return -ENOMEM;
 	error = posix_acl_chmod_masq(clone, inode->i_mode);
-	if (!error)
-		error = reiserfs_set_acl(inode, ACL_TYPE_ACCESS, clone);
+	if (!error) {
+		struct reiserfs_transaction_handle th;
+		size_t size = reiserfs_xattr_nblocks(inode,
+		                                     reiserfs_acl_size(clone->a_count));
+		reiserfs_write_lock(inode->i_sb);
+		error = journal_begin (&th, inode->i_sb, size * 2);
+		if (!error)
+			error = reiserfs_set_acl(&th, inode, ACL_TYPE_ACCESS, clone);
+		if (!error)
+			error = journal_end(&th, inode->i_sb, size * 2);
+		reiserfs_write_unlock(inode->i_sb);
+	}
 	posix_acl_release(clone);
 	return error;
 }
diff -ruNpX ../dontdiff linux-2.6.13-rc3.7/fs/reiserfs/xattr.c linux-2.6.13-rc3.8/fs/reiserfs/xattr.c
--- linux-2.6.13-rc3.7/fs/reiserfs/xattr.c	2005-07-26 13:05:02.000000000 -0400
+++ linux-2.6.13-rc3.8/fs/reiserfs/xattr.c	2005-07-26 13:05:13.000000000 -0400
@@ -419,8 +419,9 @@ static inline __u32 xattr_hash(const cha
  * inode->i_sem: down
  */
 int
-reiserfs_xattr_set(struct inode *inode, const char *name, const void *buffer,
-		   size_t buffer_size, int flags)
+reiserfs_xattr_set_handle(struct reiserfs_transaction_handle *th,
+                          struct inode *inode, const char *name,
+			  const void *buffer, size_t buffer_size, int flags)
 {
 	int err = 0;
 	struct file *fp;
@@ -521,6 +522,36 @@ reiserfs_xattr_set(struct inode *inode, 
 	return err;
 }
 
+/* We need to start a transaction to maintain lock ordering */
+int reiserfs_xattr_set(struct inode *inode, const char *name,
+                       const void *buffer, size_t buffer_size, int flags)
+{
+
+	struct reiserfs_transaction_handle th;
+	int error, error2;
+	size_t jbegin_count = reiserfs_xattr_nblocks(inode, buffer_size);
+
+	if (!(flags & XATTR_REPLACE))
+		jbegin_count += reiserfs_xattr_jcreate_nblocks(inode);
+
+	reiserfs_write_lock(inode->i_sb);
+	error = journal_begin(&th, inode->i_sb, jbegin_count);
+	if (error) {
+		reiserfs_write_unlock(inode->i_sb);
+		return error;
+	}
+
+	error = reiserfs_xattr_set_handle(&th, inode, name,
+	                                  buffer, buffer_size, flags);
+
+	error2 = journal_end(&th, inode->i_sb, jbegin_count);
+	if (error == 0)
+		error = error2;
+	reiserfs_write_unlock(inode->i_sb);
+
+	return error;
+}
+
 /*
  * inode->i_sem: down
  */
@@ -678,14 +709,28 @@ int reiserfs_delete_xattrs(struct inode 
 
 	/* Leftovers besides . and .. -- that's not good. */
 	if (dir->d_inode->i_nlink <= 2) {
+		struct reiserfs_transaction_handle th;
+		int jbegin_count;
+		int jerr;
+
+		jbegin_count = JOURNAL_PER_BALANCE_CNT * 2 + 2 + 4 *
+		               REISERFS_QUOTA_TRANS_BLOCKS(inode->i_sb);
+		reiserfs_write_lock(inode->i_sb);
+		err = journal_begin (&th, inode->i_sb, jbegin_count);
+		if (err) {
+			reiserfs_write_unlock(inode->i_sb);
+			unlock_kernel();
+			goto out_dir;
+		}
 		root = dget(REISERFS_SB(inode->i_sb)->xattr_root);
 		down(&root->d_inode->i_sem);
 		err = vfs_rmdir(root->d_inode, dir);
 		up(&root->d_inode->i_sem);
+		jerr = journal_end (&th, inode->i_sb, jbegin_count);
+		reiserfs_write_unlock(inode->i_sb);
 		dput(root);
-		if (!err)
-			REISERFS_I(inode)->i_flags =
-				REISERFS_I(inode)->i_flags & ~i_has_xattr_dir;
+		if (!err && jerr)
+			err = jerr;
 	} else {
 		reiserfs_warning(inode->i_sb,
 				 "Couldn't remove all entries in directory");
diff -ruNpX ../dontdiff linux-2.6.13-rc3.7/include/linux/reiserfs_acl.h linux-2.6.13-rc3.8/include/linux/reiserfs_acl.h
--- linux-2.6.13-rc3.7/include/linux/reiserfs_acl.h	2005-07-26 13:05:02.000000000 -0400
+++ linux-2.6.13-rc3.8/include/linux/reiserfs_acl.h	2005-07-26 13:05:13.000000000 -0400
@@ -49,7 +49,8 @@ static inline int reiserfs_acl_count(siz
 #ifdef CONFIG_REISERFS_FS_POSIX_ACL
 struct posix_acl *reiserfs_get_acl(struct inode *inode, int type);
 int reiserfs_acl_chmod(struct inode *inode);
-int reiserfs_inherit_default_acl(struct inode *dir, struct dentry *dentry,
+int reiserfs_inherit_default_acl(struct reiserfs_transaction_handle *th,
+                                 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);
diff -ruNpX ../dontdiff linux-2.6.13-rc3.7/include/linux/reiserfs_xattr.h linux-2.6.13-rc3.8/include/linux/reiserfs_xattr.h
--- linux-2.6.13-rc3.7/include/linux/reiserfs_xattr.h	2005-07-26 13:05:02.000000000 -0400
+++ linux-2.6.13-rc3.8/include/linux/reiserfs_xattr.h	2005-07-26 13:05:13.000000000 -0400
@@ -34,6 +34,7 @@ int reiserfs_permission_locked(struct in
 
 int reiserfs_xattr_get(const struct inode *, const char *, void *, size_t);
 int reiserfs_xattr_set(struct inode *, const char *, const void *, size_t, int);
+int reiserfs_xattr_set_handle(struct reiserfs_transaction_handle *,struct inode *, const char *, const void *, size_t, int);
 
 extern struct xattr_handler reiserfs_xattr_user_handler;
 extern struct xattr_handler reiserfs_xattr_trusted_handler;
@@ -46,6 +47,36 @@ static inline void reiserfs_mark_inode_p
 	inode->i_flags |= S_PRIVATE;
 }
 
+#define xattr_size(size) ((size) + sizeof (struct reiserfs_xattr_header))
+#define reiserfs_xattr_nblocks(inode, size) (_ROUND_UP(xattr_size(size), (inode)->i_sb->s_blocksize) >> (inode)->i_sb->s_blocksize_bits)
+
+/* We may have to create up to 3 objects: xattr root, xattr dir, xattr file.
+ * Let's try to be smart about it.
+ * xattr root: We cache it. If it's not cached, we may need to create it.
+ * xattr dir: If anything has been loaded for this inode, we can set a flag
+ *            saying so.
+ * xattr file: Since we don't cache xattrs, we can't tell. We always include
+ *             blocks for it.
+ *
+ * However, since root and dir can be created between calls - YOU MUST SAVE
+ * THIS VALUE.
+ */
+static inline size_t reiserfs_xattr_jcreate_nblocks(struct inode *inode)
+{
+	size_t blocks_per_object = (JOURNAL_PER_BALANCE_CNT * 3 + 2 *
+	                        (REISERFS_QUOTA_INIT_BLOCKS(inode->i_sb) +
+				 REISERFS_QUOTA_TRANS_BLOCKS(inode->i_sb)));
+	size_t nblocks = blocks_per_object;
+
+	if (REISERFS_SB(inode->i_sb)->xattr_root == NULL)
+		nblocks += blocks_per_object;
+
+	if ((REISERFS_I(inode)->i_flags & i_has_xattr_dir) == 0)
+		nblocks += blocks_per_object;
+
+	return nblocks;
+}
+
 #else
 
 #define is_reiserfs_priv_object(inode) 0
