btrfs-progs: remove unused argument from write_and_map_eb
[platform/upstream/btrfs-progs.git] / ctree.c
diff --git a/ctree.c b/ctree.c
index c60f609..551d9dc 100644 (file)
--- a/ctree.c
+++ b/ctree.c
@@ -21,6 +21,7 @@
 #include "print-tree.h"
 #include "repair.h"
 #include "internal.h"
+#include "sizes.h"
 
 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
                      *root, struct btrfs_path *path, int level);
@@ -142,7 +143,7 @@ static int btrfs_block_can_be_shared(struct btrfs_root *root,
                                     struct extent_buffer *buf)
 {
        /*
-        * Tree blocks not in refernece counted trees and tree roots
+        * Tree blocks not in reference counted trees and tree roots
         * are never shared. If a block was allocated after the last
         * snapshot and the block was not allocated by tree relocation,
         * we know the block is not shared.
@@ -368,7 +369,7 @@ int btrfs_cow_block(struct btrfs_trans_handle *trans,
                return 0;
        }
 
-       search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
+       search_start = buf->start & ~((u64)SZ_1G - 1);
        ret = __btrfs_cow_block(trans, root, buf, parent,
                                 parent_slot, cow_ret, search_start, 0);
        return ret;
@@ -757,8 +758,7 @@ static int balance_level(struct btrfs_trans_handle *trans,
                        wait_on_tree_block_writeback(root, right);
                        free_extent_buffer(right);
                        right = NULL;
-                       wret = btrfs_del_ptr(trans, root, path,
-                                            level + 1, pslot + 1);
+                       wret = btrfs_del_ptr(root, path, level + 1, pslot + 1);
                        if (wret)
                                ret = wret;
                        wret = btrfs_free_extent(trans, root, bytenr,
@@ -805,7 +805,7 @@ static int balance_level(struct btrfs_trans_handle *trans,
                wait_on_tree_block_writeback(root, mid);
                free_extent_buffer(mid);
                mid = NULL;
-               wret = btrfs_del_ptr(trans, root, path, level + 1, pslot);
+               wret = btrfs_del_ptr(root, path, level + 1, pslot);
                if (wret)
                        ret = wret;
                wret = btrfs_free_extent(trans, root, bytenr, blocksize,
@@ -1026,9 +1026,9 @@ void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
                        nread += blocksize;
                }
                nscan++;
-               if (path->reada < 2 && (nread > (256 * 1024) || nscan > 32))
+               if (path->reada < 2 && (nread > SZ_256K || nscan > 32))
                        break;
-               if(nread > (1024 * 1024) || nscan > 128)
+               if(nread > SZ_1M || nscan > 128)
                        break;
 
                if (search < lowest_read)
@@ -2294,9 +2294,7 @@ split:
        return ret;
 }
 
-int btrfs_truncate_item(struct btrfs_trans_handle *trans,
-                       struct btrfs_root *root,
-                       struct btrfs_path *path,
+int btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
                        u32 new_size, int from_end)
 {
        int ret = 0;
@@ -2391,8 +2389,7 @@ int btrfs_truncate_item(struct btrfs_trans_handle *trans,
        return ret;
 }
 
-int btrfs_extend_item(struct btrfs_trans_handle *trans,
-                     struct btrfs_root *root, struct btrfs_path *path,
+int btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
                      u32 data_size)
 {
        int ret = 0;
@@ -2580,7 +2577,9 @@ int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
        unsigned long ptr;
 
        path = btrfs_alloc_path();
-       BUG_ON(!path);
+       if (!path)
+               return -ENOMEM;
+
        ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
        if (!ret) {
                leaf = path->nodes[0];
@@ -2599,8 +2598,8 @@ int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
  * continuing all the way the root if required.  The root is converted into
  * a leaf if all the nodes are emptied.
  */
-int btrfs_del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
-                  struct btrfs_path *path, int level, int slot)
+int btrfs_del_ptr(struct btrfs_root *root, struct btrfs_path *path,
+               int level, int slot)
 {
        struct extent_buffer *parent = path->nodes[level];
        u32 nritems;
@@ -2648,7 +2647,7 @@ static noinline int btrfs_del_leaf(struct btrfs_trans_handle *trans,
        int ret;
 
        WARN_ON(btrfs_header_generation(leaf) != trans->transid);
-       ret = btrfs_del_ptr(trans, root, path, 1, path->slots[1]);
+       ret = btrfs_del_ptr(root, path, 1, path->slots[1]);
        if (ret)
                return ret;
 
@@ -2880,6 +2879,7 @@ int btrfs_previous_item(struct btrfs_root *root,
 {
        struct btrfs_key found_key;
        struct extent_buffer *leaf;
+       u32 nritems;
        int ret;
 
        while(1) {
@@ -2891,9 +2891,20 @@ int btrfs_previous_item(struct btrfs_root *root,
                        path->slots[0]--;
                }
                leaf = path->nodes[0];
+               nritems = btrfs_header_nritems(leaf);
+               if (nritems == 0)
+                       return 1;
+               if (path->slots[0] == nritems)
+                       path->slots[0]--;
+
                btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
+               if (found_key.objectid < min_objectid)
+                       break;
                if (found_key.type == type)
                        return 0;
+               if (found_key.objectid == min_objectid &&
+                   found_key.type < type)
+                       break;
        }
        return 1;
 }
@@ -2939,3 +2950,27 @@ int btrfs_previous_extent_item(struct btrfs_root *root,
        }
        return 1;
 }
+
+/*
+ * Search in extent tree to found next meta/data extent
+ * Caller needs to check for no-hole or skinny metadata features.
+ */
+int btrfs_next_extent_item(struct btrfs_root *root,
+                       struct btrfs_path *path, u64 max_objectid)
+{
+       struct btrfs_key found_key;
+       int ret;
+
+       while (1) {
+               ret = btrfs_next_item(root, path);
+               if (ret)
+                       return ret;
+               btrfs_item_key_to_cpu(path->nodes[0], &found_key,
+                                     path->slots[0]);
+               if (found_key.objectid > max_objectid)
+                       return 1;
+               if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
+                   found_key.type == BTRFS_METADATA_ITEM_KEY)
+               return 0;
+       }
+}