btrfs-progs: add basic awareness of the free space tree
[platform/upstream/btrfs-progs.git] / extent-tree.c
index ac582e0..e04d962 100644 (file)
@@ -19,6 +19,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>
+#include <math.h>
 #include "kerncompat.h"
 #include "radix-tree.h"
 #include "ctree.h"
@@ -28,7 +29,6 @@
 #include "crc32c.h"
 #include "volumes.h"
 #include "free-space-cache.h"
-#include "math.h"
 #include "utils.h"
 
 #define PENDING_EXTENT_INSERT 0
@@ -2605,6 +2605,11 @@ check_failed:
        }
 
        if (!(data & BTRFS_BLOCK_GROUP_DATA)) {
+               if (check_crossing_stripes(ins->objectid, num_bytes)) {
+                       search_start = round_down(ins->objectid + num_bytes,
+                                                 BTRFS_STRIPE_LEN);
+                       goto new_group;
+               }
                block_group = btrfs_lookup_block_group(info, ins->objectid);
                if (block_group)
                        trans->block_group = block_group;
@@ -2649,7 +2654,7 @@ int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
 
        if (info->extent_ops) {
                struct btrfs_extent_ops *ops = info->extent_ops;
-               ret = ops->alloc_extent(root, num_bytes, hint_byte, ins);
+               ret = ops->alloc_extent(root, num_bytes, hint_byte, ins, !data);
                BUG_ON(ret);
                goto found;
        }
@@ -3250,7 +3255,7 @@ int btrfs_read_block_groups(struct btrfs_root *root)
                cache = kzalloc(sizeof(*cache), GFP_NOFS);
                if (!cache) {
                        ret = -ENOMEM;
-                       break;
+                       goto error;
                }
 
                read_extent_buffer(leaf, &cache->item,