btrfs: keep device list sorted
[sfrench/cifs-2.6.git] / fs / btrfs / volumes.c
index b2d05c6b1c5672a638ba8752333c10508e366885..abc6555105bb8758889fc8fda63574e5b4fe0d71 100644 (file)
@@ -27,6 +27,7 @@
 #include <linux/raid/pq.h>
 #include <linux/semaphore.h>
 #include <linux/uuid.h>
+#include <linux/list_sort.h>
 #include <asm/div64.h>
 #include "ctree.h"
 #include "extent_map.h"
@@ -1103,6 +1104,20 @@ out:
        return ret;
 }
 
+static int devid_cmp(void *priv, struct list_head *a, struct list_head *b)
+{
+       struct btrfs_device *dev1, *dev2;
+
+       dev1 = list_entry(a, struct btrfs_device, dev_list);
+       dev2 = list_entry(b, struct btrfs_device, dev_list);
+
+       if (dev1->devid < dev2->devid)
+               return -1;
+       else if (dev1->devid > dev2->devid)
+               return 1;
+       return 0;
+}
+
 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
                       fmode_t flags, void *holder)
 {
@@ -1113,6 +1128,7 @@ int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
                fs_devices->opened++;
                ret = 0;
        } else {
+               list_sort(NULL, &fs_devices->devices, devid_cmp);
                ret = __btrfs_open_devices(fs_devices, flags, holder);
        }
        mutex_unlock(&uuid_mutex);
@@ -2666,19 +2682,6 @@ error:
        return ret;
 }
 
-void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
-                                             struct btrfs_device *tgtdev)
-{
-       u32 sectorsize = fs_info->sectorsize;
-
-       WARN_ON(fs_info->fs_devices->rw_devices == 0);
-       tgtdev->io_width = sectorsize;
-       tgtdev->io_align = sectorsize;
-       tgtdev->sector_size = sectorsize;
-       tgtdev->fs_info = fs_info;
-       set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &tgtdev->dev_state);
-}
-
 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
                                        struct btrfs_device *device)
 {
@@ -4672,7 +4675,7 @@ static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
        btrfs_set_fs_incompat(info, RAID56);
 }
 
-#define BTRFS_MAX_DEVS(r) ((BTRFS_MAX_ITEM_SIZE(r->fs_info)            \
+#define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info)       \
                        - sizeof(struct btrfs_chunk))           \
                        / sizeof(struct btrfs_stripe) + 1)
 
@@ -4713,8 +4716,11 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
 
        BUG_ON(!alloc_profile_is_valid(type, 0));
 
-       if (list_empty(&fs_devices->alloc_list))
+       if (list_empty(&fs_devices->alloc_list)) {
+               if (btrfs_test_opt(info, ENOSPC_DEBUG))
+                       btrfs_debug(info, "%s: no writable device", __func__);
                return -ENOSPC;
+       }
 
        index = __get_raid_index(type);
 
@@ -4729,7 +4735,7 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
                max_stripe_size = SZ_1G;
                max_chunk_size = 10 * max_stripe_size;
                if (!devs_max)
-                       devs_max = BTRFS_MAX_DEVS(info->chunk_root);
+                       devs_max = BTRFS_MAX_DEVS(info);
        } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
                /* for larger filesystems, use larger metadata chunks */
                if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
@@ -4738,7 +4744,7 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
                        max_stripe_size = SZ_256M;
                max_chunk_size = max_stripe_size;
                if (!devs_max)
-                       devs_max = BTRFS_MAX_DEVS(info->chunk_root);
+                       devs_max = BTRFS_MAX_DEVS(info);
        } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
                max_stripe_size = SZ_32M;
                max_chunk_size = 2 * max_stripe_size;
@@ -4797,8 +4803,14 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
                if (ret == 0)
                        max_avail = max_stripe_size * dev_stripes;
 
-               if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
+               if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) {
+                       if (btrfs_test_opt(info, ENOSPC_DEBUG))
+                               btrfs_debug(info,
+                       "%s: devid %llu has no free space, have=%llu want=%u",
+                                           __func__, device->devid, max_avail,
+                                           BTRFS_STRIPE_LEN * dev_stripes);
                        continue;
+               }
 
                if (ndevs == fs_devices->rw_devices) {
                        WARN(1, "%s: found more than %llu devices\n",
@@ -4823,6 +4835,12 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
 
        if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
                ret = -ENOSPC;
+               if (btrfs_test_opt(info, ENOSPC_DEBUG)) {
+                       btrfs_debug(info,
+       "%s: not enough devices with free space: have=%d minimum required=%d",
+                                   __func__, ndevs, min(devs_min,
+                                   devs_increment * sub_stripes));
+               }
                goto error;
        }
 
@@ -7358,20 +7376,20 @@ void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
 }
 
 /* Must be invoked during the transaction commit */
-void btrfs_update_commit_device_bytes_used(struct btrfs_fs_info *fs_info,
-                                       struct btrfs_transaction *transaction)
+void btrfs_update_commit_device_bytes_used(struct btrfs_transaction *trans)
 {
+       struct btrfs_fs_info *fs_info = trans->fs_info;
        struct extent_map *em;
        struct map_lookup *map;
        struct btrfs_device *dev;
        int i;
 
-       if (list_empty(&transaction->pending_chunks))
+       if (list_empty(&trans->pending_chunks))
                return;
 
        /* In order to kick the device replace finish process */
        mutex_lock(&fs_info->chunk_mutex);
-       list_for_each_entry(em, &transaction->pending_chunks, list) {
+       list_for_each_entry(em, &trans->pending_chunks, list) {
                map = em->map_lookup;
 
                for (i = 0; i < map->num_stripes; i++) {