Merge branch 'apei-release' of git://git.kernel.org/pub/scm/linux/kernel/git/lenb...
[pandora-kernel.git] / fs / btrfs / transaction.c
index 51dcec8..7dc36fa 100644 (file)
@@ -216,17 +216,11 @@ static void wait_current_trans(struct btrfs_root *root)
        spin_lock(&root->fs_info->trans_lock);
        cur_trans = root->fs_info->running_transaction;
        if (cur_trans && cur_trans->blocked) {
-               DEFINE_WAIT(wait);
                atomic_inc(&cur_trans->use_count);
                spin_unlock(&root->fs_info->trans_lock);
-               while (1) {
-                       prepare_to_wait(&root->fs_info->transaction_wait, &wait,
-                                       TASK_UNINTERRUPTIBLE);
-                       if (!cur_trans->blocked)
-                               break;
-                       schedule();
-               }
-               finish_wait(&root->fs_info->transaction_wait, &wait);
+
+               wait_event(root->fs_info->transaction_wait,
+                          !cur_trans->blocked);
                put_transaction(cur_trans);
        } else {
                spin_unlock(&root->fs_info->trans_lock);
@@ -260,7 +254,7 @@ static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
 {
        struct btrfs_trans_handle *h;
        struct btrfs_transaction *cur_trans;
-       int retries = 0;
+       u64 num_bytes = 0;
        int ret;
 
        if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
@@ -274,6 +268,19 @@ static struct btrfs_trans_handle *start_transaction(struct btrfs_root *root,
                h->block_rsv = NULL;
                goto got_it;
        }
+
+       /*
+        * Do the reservation before we join the transaction so we can do all
+        * the appropriate flushing if need be.
+        */
+       if (num_items > 0 && root != root->fs_info->chunk_root) {
+               num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
+               ret = btrfs_block_rsv_add(NULL, root,
+                                         &root->fs_info->trans_block_rsv,
+                                         num_bytes);
+               if (ret)
+                       return ERR_PTR(ret);
+       }
 again:
        h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
        if (!h)
@@ -310,24 +317,9 @@ again:
                goto again;
        }
 
-       if (num_items > 0) {
-               ret = btrfs_trans_reserve_metadata(h, root, num_items);
-               if (ret == -EAGAIN && !retries) {
-                       retries++;
-                       btrfs_commit_transaction(h, root);
-                       goto again;
-               } else if (ret == -EAGAIN) {
-                       /*
-                        * We have already retried and got EAGAIN, so really we
-                        * don't have space, so set ret to -ENOSPC.
-                        */
-                       ret = -ENOSPC;
-               }
-
-               if (ret < 0) {
-                       btrfs_end_transaction(h, root);
-                       return ERR_PTR(ret);
-               }
+       if (num_bytes) {
+               h->block_rsv = &root->fs_info->trans_block_rsv;
+               h->bytes_reserved = num_bytes;
        }
 
 got_it:
@@ -359,19 +351,10 @@ struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root
 }
 
 /* wait for a transaction commit to be fully complete */
-static noinline int wait_for_commit(struct btrfs_root *root,
+static noinline void wait_for_commit(struct btrfs_root *root,
                                    struct btrfs_transaction *commit)
 {
-       DEFINE_WAIT(wait);
-       while (!commit->commit_done) {
-               prepare_to_wait(&commit->commit_wait, &wait,
-                               TASK_UNINTERRUPTIBLE);
-               if (commit->commit_done)
-                       break;
-               schedule();
-       }
-       finish_wait(&commit->commit_wait, &wait);
-       return 0;
+       wait_event(commit->commit_wait, commit->commit_done);
 }
 
 int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
@@ -499,10 +482,17 @@ static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
        }
 
        if (lock && cur_trans->blocked && !cur_trans->in_commit) {
-               if (throttle)
+               if (throttle) {
+                       /*
+                        * We may race with somebody else here so end up having
+                        * to call end_transaction on ourselves again, so inc
+                        * our use_count.
+                        */
+                       trans->use_count++;
                        return btrfs_commit_transaction(trans, root);
-               else
+               } else {
                        wake_up_process(info->transaction_kthread);
+               }
        }
 
        WARN_ON(cur_trans != info->running_transaction);
@@ -1080,22 +1070,7 @@ int btrfs_transaction_blocked(struct btrfs_fs_info *info)
 static void wait_current_trans_commit_start(struct btrfs_root *root,
                                            struct btrfs_transaction *trans)
 {
-       DEFINE_WAIT(wait);
-
-       if (trans->in_commit)
-               return;
-
-       while (1) {
-               prepare_to_wait(&root->fs_info->transaction_blocked_wait, &wait,
-                               TASK_UNINTERRUPTIBLE);
-               if (trans->in_commit) {
-                       finish_wait(&root->fs_info->transaction_blocked_wait,
-                                   &wait);
-                       break;
-               }
-               schedule();
-               finish_wait(&root->fs_info->transaction_blocked_wait, &wait);
-       }
+       wait_event(root->fs_info->transaction_blocked_wait, trans->in_commit);
 }
 
 /*
@@ -1105,24 +1080,8 @@ static void wait_current_trans_commit_start(struct btrfs_root *root,
 static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root,
                                         struct btrfs_transaction *trans)
 {
-       DEFINE_WAIT(wait);
-
-       if (trans->commit_done || (trans->in_commit && !trans->blocked))
-               return;
-
-       while (1) {
-               prepare_to_wait(&root->fs_info->transaction_wait, &wait,
-                               TASK_UNINTERRUPTIBLE);
-               if (trans->commit_done ||
-                   (trans->in_commit && !trans->blocked)) {
-                       finish_wait(&root->fs_info->transaction_wait,
-                                   &wait);
-                       break;
-               }
-               schedule();
-               finish_wait(&root->fs_info->transaction_wait,
-                           &wait);
-       }
+       wait_event(root->fs_info->transaction_wait,
+                  trans->commit_done || (trans->in_commit && !trans->blocked));
 }
 
 /*
@@ -1229,8 +1188,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
                atomic_inc(&cur_trans->use_count);
                btrfs_end_transaction(trans, root);
 
-               ret = wait_for_commit(root, cur_trans);
-               BUG_ON(ret);
+               wait_for_commit(root, cur_trans);
 
                put_transaction(cur_trans);