Merge branch 'misc-cleanups-4.5' of git://git.kernel.org/pub/scm/linux/kernel/git...
[cascardo/linux.git] / fs / btrfs / free-space-cache.c
index 85a1f86..8f835bf 100644 (file)
@@ -30,7 +30,7 @@
 #include "volumes.h"
 
 #define BITS_PER_BITMAP                (PAGE_CACHE_SIZE * 8)
-#define MAX_CACHE_BYTES_PER_GIG        (32 * 1024)
+#define MAX_CACHE_BYTES_PER_GIG        SZ_32K
 
 struct btrfs_trim_range {
        u64 start;
@@ -891,7 +891,7 @@ out:
                spin_unlock(&block_group->lock);
                ret = 0;
 
-               btrfs_warn(fs_info, "failed to load free space cache for block group %llu, rebuild it now",
+               btrfs_warn(fs_info, "failed to load free space cache for block group %llu, rebuilding it now",
                        block_group->key.objectid);
        }
 
@@ -1086,14 +1086,11 @@ write_pinned_extent_entries(struct btrfs_root *root,
 static noinline_for_stack int
 write_bitmap_entries(struct btrfs_io_ctl *io_ctl, struct list_head *bitmap_list)
 {
-       struct list_head *pos, *n;
+       struct btrfs_free_space *entry, *next;
        int ret;
 
        /* Write out the bitmaps */
-       list_for_each_safe(pos, n, bitmap_list) {
-               struct btrfs_free_space *entry =
-                       list_entry(pos, struct btrfs_free_space, list);
-
+       list_for_each_entry_safe(entry, next, bitmap_list, list) {
                ret = io_ctl_add_bitmap(io_ctl, entry->bitmap);
                if (ret)
                        return -ENOSPC;
@@ -1119,13 +1116,10 @@ static int flush_dirty_cache(struct inode *inode)
 static void noinline_for_stack
 cleanup_bitmap_list(struct list_head *bitmap_list)
 {
-       struct list_head *pos, *n;
+       struct btrfs_free_space *entry, *next;
 
-       list_for_each_safe(pos, n, bitmap_list) {
-               struct btrfs_free_space *entry =
-                       list_entry(pos, struct btrfs_free_space, list);
+       list_for_each_entry_safe(entry, next, bitmap_list, list)
                list_del_init(&entry->list);
-       }
 }
 
 static void noinline_for_stack
@@ -1261,7 +1255,7 @@ static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
                goto out;
 
        lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
-                        0, &cached_state);
+                        &cached_state);
 
        io_ctl_set_generation(io_ctl, trans->transid);
 
@@ -1656,11 +1650,10 @@ static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
         * at or below 32k, so we need to adjust how much memory we allow to be
         * used by extent based free space tracking
         */
-       if (size < 1024 * 1024 * 1024)
+       if (size < SZ_1G)
                max_bytes = MAX_CACHE_BYTES_PER_GIG;
        else
-               max_bytes = MAX_CACHE_BYTES_PER_GIG *
-                       div_u64(size, 1024 * 1024 * 1024);
+               max_bytes = MAX_CACHE_BYTES_PER_GIG * div_u64(size, SZ_1G);
 
        /*
         * we want to account for 1 more bitmap than what we have so we can make
@@ -2016,7 +2009,7 @@ static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
        return true;
 }
 
-static struct btrfs_free_space_op free_space_op = {
+static const struct btrfs_free_space_op free_space_op = {
        .recalc_thresholds      = recalculate_thresholds,
        .use_bitmap             = use_bitmap,
 };
@@ -2489,8 +2482,7 @@ void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group)
         * track of free space, and if we pass 1/2 of that we want to
         * start converting things over to using bitmaps
         */
-       ctl->extents_thresh = ((1024 * 32) / 2) /
-                               sizeof(struct btrfs_free_space);
+       ctl->extents_thresh = (SZ_32K / 2) / sizeof(struct btrfs_free_space);
 }
 
 /*
@@ -2972,7 +2964,7 @@ setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
                     u64 cont1_bytes, u64 min_bytes)
 {
        struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
-       struct btrfs_free_space *entry;
+       struct btrfs_free_space *entry = NULL;
        int ret = -ENOSPC;
        u64 bitmap_offset = offset_to_bitmap(ctl, offset);
 
@@ -2983,8 +2975,10 @@ setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
         * The bitmap that covers offset won't be in the list unless offset
         * is just its start offset.
         */
-       entry = list_first_entry(bitmaps, struct btrfs_free_space, list);
-       if (entry->offset != bitmap_offset) {
+       if (!list_empty(bitmaps))
+               entry = list_first_entry(bitmaps, struct btrfs_free_space, list);
+
+       if (!entry || entry->offset != bitmap_offset) {
                entry = tree_search_offset(ctl, bitmap_offset, 1, 0);
                if (entry && list_empty(&entry->list))
                        list_add(&entry->list, bitmaps);