int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
 {
        struct btrfs_key key;
+       struct btrfs_key orig_key;
        struct btrfs_disk_key found_key;
        int ret;
 
        btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
+       orig_key = key;
 
        if (key.offset > 0) {
                key.offset--;
 
        btrfs_release_path(path);
        ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
-       if (ret < 0)
+       if (ret <= 0)
                return ret;
+
+       /*
+        * Previous key not found. Even if we were at slot 0 of the leaf we had
+        * before releasing the path and calling btrfs_search_slot(), we now may
+        * be in a slot pointing to the same original key - this can happen if
+        * after we released the path, one of more items were moved from a
+        * sibling leaf into the front of the leaf we had due to an insertion
+        * (see push_leaf_right()).
+        * If we hit this case and our slot is > 0 and just decrement the slot
+        * so that the caller does not process the same key again, which may or
+        * may not break the caller, depending on its logic.
+        */
+       if (path->slots[0] < btrfs_header_nritems(path->nodes[0])) {
+               btrfs_item_key(path->nodes[0], &found_key, path->slots[0]);
+               ret = comp_keys(&found_key, &orig_key);
+               if (ret == 0) {
+                       if (path->slots[0] > 0) {
+                               path->slots[0]--;
+                               return 0;
+                       }
+                       /*
+                        * At slot 0, same key as before, it means orig_key is
+                        * the lowest, leftmost, key in the tree. We're done.
+                        */
+                       return 1;
+               }
+       }
+
        btrfs_item_key(path->nodes[0], &found_key, 0);
        ret = comp_keys(&found_key, &key);
        /*