hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/fs/btrfs/ctree.c
....@@ -3589,6 +3589,8 @@
35893589
35903590 ret = tree_mod_log_eb_copy(split, c, 0, mid, c_nritems - mid);
35913591 if (ret) {
3592
+ btrfs_tree_unlock(split);
3593
+ free_extent_buffer(split);
35923594 btrfs_abort_transaction(trans, ret);
35933595 return ret;
35943596 }
....@@ -3872,6 +3874,7 @@
38723874
38733875 if (check_sibling_keys(left, right)) {
38743876 ret = -EUCLEAN;
3877
+ btrfs_abort_transaction(trans, ret);
38753878 btrfs_tree_unlock(right);
38763879 free_extent_buffer(right);
38773880 return ret;
....@@ -4116,6 +4119,7 @@
41164119
41174120 if (check_sibling_keys(left, right)) {
41184121 ret = -EUCLEAN;
4122
+ btrfs_abort_transaction(trans, ret);
41194123 goto out;
41204124 }
41214125 return __push_leaf_left(path, min_data_size,
....@@ -5160,10 +5164,12 @@
51605164 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
51615165 {
51625166 struct btrfs_key key;
5167
+ struct btrfs_key orig_key;
51635168 struct btrfs_disk_key found_key;
51645169 int ret;
51655170
51665171 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
5172
+ orig_key = key;
51675173
51685174 if (key.offset > 0) {
51695175 key.offset--;
....@@ -5180,8 +5186,36 @@
51805186
51815187 btrfs_release_path(path);
51825188 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5183
- if (ret < 0)
5189
+ if (ret <= 0)
51845190 return ret;
5191
+
5192
+ /*
5193
+ * Previous key not found. Even if we were at slot 0 of the leaf we had
5194
+ * before releasing the path and calling btrfs_search_slot(), we now may
5195
+ * be in a slot pointing to the same original key - this can happen if
5196
+ * after we released the path, one of more items were moved from a
5197
+ * sibling leaf into the front of the leaf we had due to an insertion
5198
+ * (see push_leaf_right()).
5199
+ * If we hit this case and our slot is > 0 and just decrement the slot
5200
+ * so that the caller does not process the same key again, which may or
5201
+ * may not break the caller, depending on its logic.
5202
+ */
5203
+ if (path->slots[0] < btrfs_header_nritems(path->nodes[0])) {
5204
+ btrfs_item_key(path->nodes[0], &found_key, path->slots[0]);
5205
+ ret = comp_keys(&found_key, &orig_key);
5206
+ if (ret == 0) {
5207
+ if (path->slots[0] > 0) {
5208
+ path->slots[0]--;
5209
+ return 0;
5210
+ }
5211
+ /*
5212
+ * At slot 0, same key as before, it means orig_key is
5213
+ * the lowest, leftmost, key in the tree. We're done.
5214
+ */
5215
+ return 1;
5216
+ }
5217
+ }
5218
+
51855219 btrfs_item_key(path->nodes[0], &found_key, 0);
51865220 ret = comp_keys(&found_key, &key);
51875221 /*