Btrfs: protect fs_info->alloc_start
authorMiao Xie <miaox@cn.fujitsu.com>
Tue, 29 Jan 2013 10:07:33 +0000 (10:07 +0000)
committerJosef Bacik <jbacik@fusionio.com>
Wed, 20 Feb 2013 17:59:02 +0000 (12:59 -0500)
fs_info->alloc_start is a 64bits variant, can be accessed by
multi-task, but it is not protected strictly, it can be changed
while we are accessing it. On 32bit machine, we will get wrong
value because we access it by two instructions.(In fact, it is
also possible that the same problem happens on the 64bit machine,
because the compiler may split the 64bit operation into two 32bit
operation.)

For example:
Assuming -> alloc_start is 0x0000 0000 0001 0000 at the beginning,
then we remount and set ->alloc_start to 0x0000 0100 0000 0000.
Task0  Task1
load high 32 bits
set high 32 bits
set low 32 bits
load low 32 bits

Task1 will get 0.

This patch fixes this problem by using two locks to protect it
fs_info->chunk_mutex
sb->s_umount
On the read side, we just need get one of these two locks, and on
the write side, we must lock all of them.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
Signed-off-by: Josef Bacik <jbacik@fusionio.com>
fs/btrfs/ctree.h
fs/btrfs/super.c

index 169baa817d9653c4bcccdfa56eb0f1fa3b815248..d8e539fe5544e464689bf1e07f9acdc647ab566c 100644 (file)
@@ -1304,6 +1304,16 @@ struct btrfs_fs_info {
         * so it is also safe.
         */
        u64 max_inline;
+       /*
+        * Protected by ->chunk_mutex and sb->s_umount.
+        *
+        * The reason that we use two lock to protect it is because only
+        * remount and mount operations can change it and these two operations
+        * are under sb->s_umount, but the read side (chunk allocation) can not
+        * acquire sb->s_umount or the deadlock would happen. So we use two
+        * locks to protect it. On the write side, we must acquire two locks,
+        * and on the read side, we just need acquire one of them.
+        */
        u64 alloc_start;
        struct btrfs_transaction *running_transaction;
        wait_queue_head_t transaction_throttle;
index 6846ededfe95e63b020e2128fc389f41989ac4e8..eda330df45a4037387aa44606722e886a25aa9f8 100644 (file)
@@ -519,7 +519,9 @@ int btrfs_parse_options(struct btrfs_root *root, char *options)
                case Opt_alloc_start:
                        num = match_strdup(&args[0]);
                        if (num) {
+                               mutex_lock(&info->chunk_mutex);
                                info->alloc_start = memparse(num, NULL);
+                               mutex_unlock(&info->chunk_mutex);
                                kfree(num);
                                printk(KERN_INFO
                                        "btrfs: allocations start at %llu\n",
@@ -1289,7 +1291,9 @@ restore:
        fs_info->mount_opt = old_opts;
        fs_info->compress_type = old_compress_type;
        fs_info->max_inline = old_max_inline;
+       mutex_lock(&fs_info->chunk_mutex);
        fs_info->alloc_start = old_alloc_start;
+       mutex_unlock(&fs_info->chunk_mutex);
        btrfs_resize_thread_pool(fs_info,
                old_thread_pool_size, fs_info->thread_pool_size);
        fs_info->metadata_ratio = old_metadata_ratio;
This page took 0.030446 seconds and 5 git commands to generate.