Commit 49e83f57 authored by Liu Bo's avatar Liu Bo Committed by David Sterba

Btrfs: protect conditions within root->log_mutex while waiting

Both wait_for_commit() and wait_for_writer() are checking the
condition out of the mutex lock.

This refactors code a bit to be lock safe.
Signed-off-by: default avatarLiu Bo <bo.li.liu@oracle.com>
Reviewed-by: default avatarDavid Sterba <dsterba@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 45bac0f3
...@@ -2699,34 +2699,36 @@ static void wait_log_commit(struct btrfs_root *root, int transid) ...@@ -2699,34 +2699,36 @@ static void wait_log_commit(struct btrfs_root *root, int transid)
* so we know that if ours is more than 2 older than the * so we know that if ours is more than 2 older than the
* current transaction, we're done * current transaction, we're done
*/ */
do { for (;;) {
prepare_to_wait(&root->log_commit_wait[index], prepare_to_wait(&root->log_commit_wait[index],
&wait, TASK_UNINTERRUPTIBLE); &wait, TASK_UNINTERRUPTIBLE);
mutex_unlock(&root->log_mutex);
if (root->log_transid_committed < transid && if (!(root->log_transid_committed < transid &&
atomic_read(&root->log_commit[index])) atomic_read(&root->log_commit[index])))
schedule(); break;
finish_wait(&root->log_commit_wait[index], &wait); mutex_unlock(&root->log_mutex);
schedule();
mutex_lock(&root->log_mutex); mutex_lock(&root->log_mutex);
} while (root->log_transid_committed < transid && }
atomic_read(&root->log_commit[index])); finish_wait(&root->log_commit_wait[index], &wait);
} }
static void wait_for_writer(struct btrfs_root *root) static void wait_for_writer(struct btrfs_root *root)
{ {
DEFINE_WAIT(wait); DEFINE_WAIT(wait);
while (atomic_read(&root->log_writers)) { for (;;) {
prepare_to_wait(&root->log_writer_wait, prepare_to_wait(&root->log_writer_wait, &wait,
&wait, TASK_UNINTERRUPTIBLE); TASK_UNINTERRUPTIBLE);
if (!atomic_read(&root->log_writers))
break;
mutex_unlock(&root->log_mutex); mutex_unlock(&root->log_mutex);
if (atomic_read(&root->log_writers)) schedule();
schedule();
finish_wait(&root->log_writer_wait, &wait);
mutex_lock(&root->log_mutex); mutex_lock(&root->log_mutex);
} }
finish_wait(&root->log_writer_wait, &wait);
} }
static inline void btrfs_remove_log_ctx(struct btrfs_root *root, static inline void btrfs_remove_log_ctx(struct btrfs_root *root,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment