1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2007,2008 Oracle. All rights reserved.
4 */
5
6 #include <linux/sched.h>
7 #include <linux/slab.h>
8 #include <linux/rbtree.h>
9 #include <linux/mm.h>
10 #include "ctree.h"
11 #include "disk-io.h"
12 #include "transaction.h"
13 #include "print-tree.h"
14 #include "locking.h"
15 #include "volumes.h"
16 #include "qgroup.h"
17
18 static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
19 *root, struct btrfs_path *path, int level);
20 static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root *root,
21 const struct btrfs_key *ins_key, struct btrfs_path *path,
22 int data_size, int extend);
23 static int push_node_left(struct btrfs_trans_handle *trans,
24 struct extent_buffer *dst,
25 struct extent_buffer *src, int empty);
26 static int balance_node_right(struct btrfs_trans_handle *trans,
27 struct extent_buffer *dst_buf,
28 struct extent_buffer *src_buf);
29 static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
30 int level, int slot);
31
32 static const struct btrfs_csums {
33 u16 size;
34 const char name[10];
35 const char driver[12];
36 } btrfs_csums[] = {
37 [BTRFS_CSUM_TYPE_CRC32] = { .size = 4, .name = "crc32c" },
38 [BTRFS_CSUM_TYPE_XXHASH] = { .size = 8, .name = "xxhash64" },
39 [BTRFS_CSUM_TYPE_SHA256] = { .size = 32, .name = "sha256" },
40 [BTRFS_CSUM_TYPE_BLAKE2] = { .size = 32, .name = "blake2b",
41 .driver = "blake2b-256" },
42 };
43
btrfs_super_csum_size(const struct btrfs_super_block * s)44 int btrfs_super_csum_size(const struct btrfs_super_block *s)
45 {
46 u16 t = btrfs_super_csum_type(s);
47 /*
48 * csum type is validated at mount time
49 */
50 return btrfs_csums[t].size;
51 }
52
btrfs_super_csum_name(u16 csum_type)53 const char *btrfs_super_csum_name(u16 csum_type)
54 {
55 /* csum type is validated at mount time */
56 return btrfs_csums[csum_type].name;
57 }
58
59 /*
60 * Return driver name if defined, otherwise the name that's also a valid driver
61 * name
62 */
btrfs_super_csum_driver(u16 csum_type)63 const char *btrfs_super_csum_driver(u16 csum_type)
64 {
65 /* csum type is validated at mount time */
66 return btrfs_csums[csum_type].driver[0] ?
67 btrfs_csums[csum_type].driver :
68 btrfs_csums[csum_type].name;
69 }
70
btrfs_get_num_csums(void)71 size_t __attribute_const__ btrfs_get_num_csums(void)
72 {
73 return ARRAY_SIZE(btrfs_csums);
74 }
75
btrfs_alloc_path(void)76 struct btrfs_path *btrfs_alloc_path(void)
77 {
78 return kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
79 }
80
81 /* this also releases the path */
btrfs_free_path(struct btrfs_path * p)82 void btrfs_free_path(struct btrfs_path *p)
83 {
84 if (!p)
85 return;
86 btrfs_release_path(p);
87 kmem_cache_free(btrfs_path_cachep, p);
88 }
89
90 /*
91 * path release drops references on the extent buffers in the path
92 * and it drops any locks held by this path
93 *
94 * It is safe to call this on paths that no locks or extent buffers held.
95 */
btrfs_release_path(struct btrfs_path * p)96 noinline void btrfs_release_path(struct btrfs_path *p)
97 {
98 int i;
99
100 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
101 p->slots[i] = 0;
102 if (!p->nodes[i])
103 continue;
104 if (p->locks[i]) {
105 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
106 p->locks[i] = 0;
107 }
108 free_extent_buffer(p->nodes[i]);
109 p->nodes[i] = NULL;
110 }
111 }
112
113 /*
114 * safely gets a reference on the root node of a tree. A lock
115 * is not taken, so a concurrent writer may put a different node
116 * at the root of the tree. See btrfs_lock_root_node for the
117 * looping required.
118 *
119 * The extent buffer returned by this has a reference taken, so
120 * it won't disappear. It may stop being the root of the tree
121 * at any time because there are no locks held.
122 */
btrfs_root_node(struct btrfs_root * root)123 struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
124 {
125 struct extent_buffer *eb;
126
127 while (1) {
128 rcu_read_lock();
129 eb = rcu_dereference(root->node);
130
131 /*
132 * RCU really hurts here, we could free up the root node because
133 * it was COWed but we may not get the new root node yet so do
134 * the inc_not_zero dance and if it doesn't work then
135 * synchronize_rcu and try again.
136 */
137 if (atomic_inc_not_zero(&eb->refs)) {
138 rcu_read_unlock();
139 break;
140 }
141 rcu_read_unlock();
142 synchronize_rcu();
143 }
144 return eb;
145 }
146
147 /*
148 * Cowonly root (not-shareable trees, everything not subvolume or reloc roots),
149 * just get put onto a simple dirty list. Transaction walks this list to make
150 * sure they get properly updated on disk.
151 */
add_root_to_dirty_list(struct btrfs_root * root)152 static void add_root_to_dirty_list(struct btrfs_root *root)
153 {
154 struct btrfs_fs_info *fs_info = root->fs_info;
155
156 if (test_bit(BTRFS_ROOT_DIRTY, &root->state) ||
157 !test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state))
158 return;
159
160 spin_lock(&fs_info->trans_lock);
161 if (!test_and_set_bit(BTRFS_ROOT_DIRTY, &root->state)) {
162 /* Want the extent tree to be the last on the list */
163 if (root->root_key.objectid == BTRFS_EXTENT_TREE_OBJECTID)
164 list_move_tail(&root->dirty_list,
165 &fs_info->dirty_cowonly_roots);
166 else
167 list_move(&root->dirty_list,
168 &fs_info->dirty_cowonly_roots);
169 }
170 spin_unlock(&fs_info->trans_lock);
171 }
172
173 /*
174 * used by snapshot creation to make a copy of a root for a tree with
175 * a given objectid. The buffer with the new root node is returned in
176 * cow_ret, and this func returns zero on success or a negative error code.
177 */
btrfs_copy_root(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct extent_buffer * buf,struct extent_buffer ** cow_ret,u64 new_root_objectid)178 int btrfs_copy_root(struct btrfs_trans_handle *trans,
179 struct btrfs_root *root,
180 struct extent_buffer *buf,
181 struct extent_buffer **cow_ret, u64 new_root_objectid)
182 {
183 struct btrfs_fs_info *fs_info = root->fs_info;
184 struct extent_buffer *cow;
185 int ret = 0;
186 int level;
187 struct btrfs_disk_key disk_key;
188
189 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
190 trans->transid != fs_info->running_transaction->transid);
191 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
192 trans->transid != root->last_trans);
193
194 level = btrfs_header_level(buf);
195 if (level == 0)
196 btrfs_item_key(buf, &disk_key, 0);
197 else
198 btrfs_node_key(buf, &disk_key, 0);
199
200 cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid,
201 &disk_key, level, buf->start, 0,
202 BTRFS_NESTING_NEW_ROOT);
203 if (IS_ERR(cow))
204 return PTR_ERR(cow);
205
206 copy_extent_buffer_full(cow, buf);
207 btrfs_set_header_bytenr(cow, cow->start);
208 btrfs_set_header_generation(cow, trans->transid);
209 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
210 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
211 BTRFS_HEADER_FLAG_RELOC);
212 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
213 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
214 else
215 btrfs_set_header_owner(cow, new_root_objectid);
216
217 write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid);
218
219 WARN_ON(btrfs_header_generation(buf) > trans->transid);
220 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
221 ret = btrfs_inc_ref(trans, root, cow, 1);
222 else
223 ret = btrfs_inc_ref(trans, root, cow, 0);
224 if (ret) {
225 btrfs_tree_unlock(cow);
226 free_extent_buffer(cow);
227 btrfs_abort_transaction(trans, ret);
228 return ret;
229 }
230
231 btrfs_mark_buffer_dirty(cow);
232 *cow_ret = cow;
233 return 0;
234 }
235
236 enum mod_log_op {
237 MOD_LOG_KEY_REPLACE,
238 MOD_LOG_KEY_ADD,
239 MOD_LOG_KEY_REMOVE,
240 MOD_LOG_KEY_REMOVE_WHILE_FREEING,
241 MOD_LOG_KEY_REMOVE_WHILE_MOVING,
242 MOD_LOG_MOVE_KEYS,
243 MOD_LOG_ROOT_REPLACE,
244 };
245
246 struct tree_mod_root {
247 u64 logical;
248 u8 level;
249 };
250
251 struct tree_mod_elem {
252 struct rb_node node;
253 u64 logical;
254 u64 seq;
255 enum mod_log_op op;
256
257 /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
258 int slot;
259
260 /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
261 u64 generation;
262
263 /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
264 struct btrfs_disk_key key;
265 u64 blockptr;
266
267 /* this is used for op == MOD_LOG_MOVE_KEYS */
268 struct {
269 int dst_slot;
270 int nr_items;
271 } move;
272
273 /* this is used for op == MOD_LOG_ROOT_REPLACE */
274 struct tree_mod_root old_root;
275 };
276
277 /*
278 * Pull a new tree mod seq number for our operation.
279 */
btrfs_inc_tree_mod_seq(struct btrfs_fs_info * fs_info)280 static inline u64 btrfs_inc_tree_mod_seq(struct btrfs_fs_info *fs_info)
281 {
282 return atomic64_inc_return(&fs_info->tree_mod_seq);
283 }
284
285 /*
286 * This adds a new blocker to the tree mod log's blocker list if the @elem
287 * passed does not already have a sequence number set. So when a caller expects
288 * to record tree modifications, it should ensure to set elem->seq to zero
289 * before calling btrfs_get_tree_mod_seq.
290 * Returns a fresh, unused tree log modification sequence number, even if no new
291 * blocker was added.
292 */
btrfs_get_tree_mod_seq(struct btrfs_fs_info * fs_info,struct seq_list * elem)293 u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
294 struct seq_list *elem)
295 {
296 write_lock(&fs_info->tree_mod_log_lock);
297 if (!elem->seq) {
298 elem->seq = btrfs_inc_tree_mod_seq(fs_info);
299 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
300 }
301 write_unlock(&fs_info->tree_mod_log_lock);
302
303 return elem->seq;
304 }
305
btrfs_put_tree_mod_seq(struct btrfs_fs_info * fs_info,struct seq_list * elem)306 void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
307 struct seq_list *elem)
308 {
309 struct rb_root *tm_root;
310 struct rb_node *node;
311 struct rb_node *next;
312 struct tree_mod_elem *tm;
313 u64 min_seq = (u64)-1;
314 u64 seq_putting = elem->seq;
315
316 if (!seq_putting)
317 return;
318
319 write_lock(&fs_info->tree_mod_log_lock);
320 list_del(&elem->list);
321 elem->seq = 0;
322
323 if (!list_empty(&fs_info->tree_mod_seq_list)) {
324 struct seq_list *first;
325
326 first = list_first_entry(&fs_info->tree_mod_seq_list,
327 struct seq_list, list);
328 if (seq_putting > first->seq) {
329 /*
330 * Blocker with lower sequence number exists, we
331 * cannot remove anything from the log.
332 */
333 write_unlock(&fs_info->tree_mod_log_lock);
334 return;
335 }
336 min_seq = first->seq;
337 }
338
339 /*
340 * anything that's lower than the lowest existing (read: blocked)
341 * sequence number can be removed from the tree.
342 */
343 tm_root = &fs_info->tree_mod_log;
344 for (node = rb_first(tm_root); node; node = next) {
345 next = rb_next(node);
346 tm = rb_entry(node, struct tree_mod_elem, node);
347 if (tm->seq >= min_seq)
348 continue;
349 rb_erase(node, tm_root);
350 kfree(tm);
351 }
352 write_unlock(&fs_info->tree_mod_log_lock);
353 }
354
355 /*
356 * key order of the log:
357 * node/leaf start address -> sequence
358 *
359 * The 'start address' is the logical address of the *new* root node
360 * for root replace operations, or the logical address of the affected
361 * block for all other operations.
362 */
363 static noinline int
__tree_mod_log_insert(struct btrfs_fs_info * fs_info,struct tree_mod_elem * tm)364 __tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
365 {
366 struct rb_root *tm_root;
367 struct rb_node **new;
368 struct rb_node *parent = NULL;
369 struct tree_mod_elem *cur;
370
371 lockdep_assert_held_write(&fs_info->tree_mod_log_lock);
372
373 tm->seq = btrfs_inc_tree_mod_seq(fs_info);
374
375 tm_root = &fs_info->tree_mod_log;
376 new = &tm_root->rb_node;
377 while (*new) {
378 cur = rb_entry(*new, struct tree_mod_elem, node);
379 parent = *new;
380 if (cur->logical < tm->logical)
381 new = &((*new)->rb_left);
382 else if (cur->logical > tm->logical)
383 new = &((*new)->rb_right);
384 else if (cur->seq < tm->seq)
385 new = &((*new)->rb_left);
386 else if (cur->seq > tm->seq)
387 new = &((*new)->rb_right);
388 else
389 return -EEXIST;
390 }
391
392 rb_link_node(&tm->node, parent, new);
393 rb_insert_color(&tm->node, tm_root);
394 return 0;
395 }
396
397 /*
398 * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
399 * returns zero with the tree_mod_log_lock acquired. The caller must hold
400 * this until all tree mod log insertions are recorded in the rb tree and then
401 * write unlock fs_info::tree_mod_log_lock.
402 */
tree_mod_dont_log(struct btrfs_fs_info * fs_info,struct extent_buffer * eb)403 static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
404 struct extent_buffer *eb) {
405 smp_mb();
406 if (list_empty(&(fs_info)->tree_mod_seq_list))
407 return 1;
408 if (eb && btrfs_header_level(eb) == 0)
409 return 1;
410
411 write_lock(&fs_info->tree_mod_log_lock);
412 if (list_empty(&(fs_info)->tree_mod_seq_list)) {
413 write_unlock(&fs_info->tree_mod_log_lock);
414 return 1;
415 }
416
417 return 0;
418 }
419
420 /* Similar to tree_mod_dont_log, but doesn't acquire any locks. */
tree_mod_need_log(const struct btrfs_fs_info * fs_info,struct extent_buffer * eb)421 static inline int tree_mod_need_log(const struct btrfs_fs_info *fs_info,
422 struct extent_buffer *eb)
423 {
424 smp_mb();
425 if (list_empty(&(fs_info)->tree_mod_seq_list))
426 return 0;
427 if (eb && btrfs_header_level(eb) == 0)
428 return 0;
429
430 return 1;
431 }
432
433 static struct tree_mod_elem *
alloc_tree_mod_elem(struct extent_buffer * eb,int slot,enum mod_log_op op,gfp_t flags)434 alloc_tree_mod_elem(struct extent_buffer *eb, int slot,
435 enum mod_log_op op, gfp_t flags)
436 {
437 struct tree_mod_elem *tm;
438
439 tm = kzalloc(sizeof(*tm), flags);
440 if (!tm)
441 return NULL;
442
443 tm->logical = eb->start;
444 if (op != MOD_LOG_KEY_ADD) {
445 btrfs_node_key(eb, &tm->key, slot);
446 tm->blockptr = btrfs_node_blockptr(eb, slot);
447 }
448 tm->op = op;
449 tm->slot = slot;
450 tm->generation = btrfs_node_ptr_generation(eb, slot);
451 RB_CLEAR_NODE(&tm->node);
452
453 return tm;
454 }
455
tree_mod_log_insert_key(struct extent_buffer * eb,int slot,enum mod_log_op op,gfp_t flags)456 static noinline int tree_mod_log_insert_key(struct extent_buffer *eb, int slot,
457 enum mod_log_op op, gfp_t flags)
458 {
459 struct tree_mod_elem *tm;
460 int ret;
461
462 if (!tree_mod_need_log(eb->fs_info, eb))
463 return 0;
464
465 tm = alloc_tree_mod_elem(eb, slot, op, flags);
466 if (!tm)
467 return -ENOMEM;
468
469 if (tree_mod_dont_log(eb->fs_info, eb)) {
470 kfree(tm);
471 return 0;
472 }
473
474 ret = __tree_mod_log_insert(eb->fs_info, tm);
475 write_unlock(&eb->fs_info->tree_mod_log_lock);
476 if (ret)
477 kfree(tm);
478
479 return ret;
480 }
481
tree_mod_log_insert_move(struct extent_buffer * eb,int dst_slot,int src_slot,int nr_items)482 static noinline int tree_mod_log_insert_move(struct extent_buffer *eb,
483 int dst_slot, int src_slot, int nr_items)
484 {
485 struct tree_mod_elem *tm = NULL;
486 struct tree_mod_elem **tm_list = NULL;
487 int ret = 0;
488 int i;
489 int locked = 0;
490
491 if (!tree_mod_need_log(eb->fs_info, eb))
492 return 0;
493
494 tm_list = kcalloc(nr_items, sizeof(struct tree_mod_elem *), GFP_NOFS);
495 if (!tm_list)
496 return -ENOMEM;
497
498 tm = kzalloc(sizeof(*tm), GFP_NOFS);
499 if (!tm) {
500 ret = -ENOMEM;
501 goto free_tms;
502 }
503
504 tm->logical = eb->start;
505 tm->slot = src_slot;
506 tm->move.dst_slot = dst_slot;
507 tm->move.nr_items = nr_items;
508 tm->op = MOD_LOG_MOVE_KEYS;
509
510 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
511 tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot,
512 MOD_LOG_KEY_REMOVE_WHILE_MOVING, GFP_NOFS);
513 if (!tm_list[i]) {
514 ret = -ENOMEM;
515 goto free_tms;
516 }
517 }
518
519 if (tree_mod_dont_log(eb->fs_info, eb))
520 goto free_tms;
521 locked = 1;
522
523 /*
524 * When we override something during the move, we log these removals.
525 * This can only happen when we move towards the beginning of the
526 * buffer, i.e. dst_slot < src_slot.
527 */
528 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
529 ret = __tree_mod_log_insert(eb->fs_info, tm_list[i]);
530 if (ret)
531 goto free_tms;
532 }
533
534 ret = __tree_mod_log_insert(eb->fs_info, tm);
535 if (ret)
536 goto free_tms;
537 write_unlock(&eb->fs_info->tree_mod_log_lock);
538 kfree(tm_list);
539
540 return 0;
541 free_tms:
542 for (i = 0; i < nr_items; i++) {
543 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
544 rb_erase(&tm_list[i]->node, &eb->fs_info->tree_mod_log);
545 kfree(tm_list[i]);
546 }
547 if (locked)
548 write_unlock(&eb->fs_info->tree_mod_log_lock);
549 kfree(tm_list);
550 kfree(tm);
551
552 return ret;
553 }
554
555 static inline int
__tree_mod_log_free_eb(struct btrfs_fs_info * fs_info,struct tree_mod_elem ** tm_list,int nritems)556 __tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
557 struct tree_mod_elem **tm_list,
558 int nritems)
559 {
560 int i, j;
561 int ret;
562
563 for (i = nritems - 1; i >= 0; i--) {
564 ret = __tree_mod_log_insert(fs_info, tm_list[i]);
565 if (ret) {
566 for (j = nritems - 1; j > i; j--)
567 rb_erase(&tm_list[j]->node,
568 &fs_info->tree_mod_log);
569 return ret;
570 }
571 }
572
573 return 0;
574 }
575
tree_mod_log_insert_root(struct extent_buffer * old_root,struct extent_buffer * new_root,int log_removal)576 static noinline int tree_mod_log_insert_root(struct extent_buffer *old_root,
577 struct extent_buffer *new_root, int log_removal)
578 {
579 struct btrfs_fs_info *fs_info = old_root->fs_info;
580 struct tree_mod_elem *tm = NULL;
581 struct tree_mod_elem **tm_list = NULL;
582 int nritems = 0;
583 int ret = 0;
584 int i;
585
586 if (!tree_mod_need_log(fs_info, NULL))
587 return 0;
588
589 if (log_removal && btrfs_header_level(old_root) > 0) {
590 nritems = btrfs_header_nritems(old_root);
591 tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *),
592 GFP_NOFS);
593 if (!tm_list) {
594 ret = -ENOMEM;
595 goto free_tms;
596 }
597 for (i = 0; i < nritems; i++) {
598 tm_list[i] = alloc_tree_mod_elem(old_root, i,
599 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
600 if (!tm_list[i]) {
601 ret = -ENOMEM;
602 goto free_tms;
603 }
604 }
605 }
606
607 tm = kzalloc(sizeof(*tm), GFP_NOFS);
608 if (!tm) {
609 ret = -ENOMEM;
610 goto free_tms;
611 }
612
613 tm->logical = new_root->start;
614 tm->old_root.logical = old_root->start;
615 tm->old_root.level = btrfs_header_level(old_root);
616 tm->generation = btrfs_header_generation(old_root);
617 tm->op = MOD_LOG_ROOT_REPLACE;
618
619 if (tree_mod_dont_log(fs_info, NULL))
620 goto free_tms;
621
622 if (tm_list)
623 ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
624 if (!ret)
625 ret = __tree_mod_log_insert(fs_info, tm);
626
627 write_unlock(&fs_info->tree_mod_log_lock);
628 if (ret)
629 goto free_tms;
630 kfree(tm_list);
631
632 return ret;
633
634 free_tms:
635 if (tm_list) {
636 for (i = 0; i < nritems; i++)
637 kfree(tm_list[i]);
638 kfree(tm_list);
639 }
640 kfree(tm);
641
642 return ret;
643 }
644
645 static struct tree_mod_elem *
__tree_mod_log_search(struct btrfs_fs_info * fs_info,u64 start,u64 min_seq,int smallest)646 __tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
647 int smallest)
648 {
649 struct rb_root *tm_root;
650 struct rb_node *node;
651 struct tree_mod_elem *cur = NULL;
652 struct tree_mod_elem *found = NULL;
653
654 read_lock(&fs_info->tree_mod_log_lock);
655 tm_root = &fs_info->tree_mod_log;
656 node = tm_root->rb_node;
657 while (node) {
658 cur = rb_entry(node, struct tree_mod_elem, node);
659 if (cur->logical < start) {
660 node = node->rb_left;
661 } else if (cur->logical > start) {
662 node = node->rb_right;
663 } else if (cur->seq < min_seq) {
664 node = node->rb_left;
665 } else if (!smallest) {
666 /* we want the node with the highest seq */
667 if (found)
668 BUG_ON(found->seq > cur->seq);
669 found = cur;
670 node = node->rb_left;
671 } else if (cur->seq > min_seq) {
672 /* we want the node with the smallest seq */
673 if (found)
674 BUG_ON(found->seq < cur->seq);
675 found = cur;
676 node = node->rb_right;
677 } else {
678 found = cur;
679 break;
680 }
681 }
682 read_unlock(&fs_info->tree_mod_log_lock);
683
684 return found;
685 }
686
687 /*
688 * this returns the element from the log with the smallest time sequence
689 * value that's in the log (the oldest log item). any element with a time
690 * sequence lower than min_seq will be ignored.
691 */
692 static struct tree_mod_elem *
tree_mod_log_search_oldest(struct btrfs_fs_info * fs_info,u64 start,u64 min_seq)693 tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
694 u64 min_seq)
695 {
696 return __tree_mod_log_search(fs_info, start, min_seq, 1);
697 }
698
699 /*
700 * this returns the element from the log with the largest time sequence
701 * value that's in the log (the most recent log item). any element with
702 * a time sequence lower than min_seq will be ignored.
703 */
704 static struct tree_mod_elem *
tree_mod_log_search(struct btrfs_fs_info * fs_info,u64 start,u64 min_seq)705 tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
706 {
707 return __tree_mod_log_search(fs_info, start, min_seq, 0);
708 }
709
tree_mod_log_eb_copy(struct extent_buffer * dst,struct extent_buffer * src,unsigned long dst_offset,unsigned long src_offset,int nr_items)710 static noinline int tree_mod_log_eb_copy(struct extent_buffer *dst,
711 struct extent_buffer *src, unsigned long dst_offset,
712 unsigned long src_offset, int nr_items)
713 {
714 struct btrfs_fs_info *fs_info = dst->fs_info;
715 int ret = 0;
716 struct tree_mod_elem **tm_list = NULL;
717 struct tree_mod_elem **tm_list_add, **tm_list_rem;
718 int i;
719 int locked = 0;
720
721 if (!tree_mod_need_log(fs_info, NULL))
722 return 0;
723
724 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
725 return 0;
726
727 tm_list = kcalloc(nr_items * 2, sizeof(struct tree_mod_elem *),
728 GFP_NOFS);
729 if (!tm_list)
730 return -ENOMEM;
731
732 tm_list_add = tm_list;
733 tm_list_rem = tm_list + nr_items;
734 for (i = 0; i < nr_items; i++) {
735 tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset,
736 MOD_LOG_KEY_REMOVE, GFP_NOFS);
737 if (!tm_list_rem[i]) {
738 ret = -ENOMEM;
739 goto free_tms;
740 }
741
742 tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset,
743 MOD_LOG_KEY_ADD, GFP_NOFS);
744 if (!tm_list_add[i]) {
745 ret = -ENOMEM;
746 goto free_tms;
747 }
748 }
749
750 if (tree_mod_dont_log(fs_info, NULL))
751 goto free_tms;
752 locked = 1;
753
754 for (i = 0; i < nr_items; i++) {
755 ret = __tree_mod_log_insert(fs_info, tm_list_rem[i]);
756 if (ret)
757 goto free_tms;
758 ret = __tree_mod_log_insert(fs_info, tm_list_add[i]);
759 if (ret)
760 goto free_tms;
761 }
762
763 write_unlock(&fs_info->tree_mod_log_lock);
764 kfree(tm_list);
765
766 return 0;
767
768 free_tms:
769 for (i = 0; i < nr_items * 2; i++) {
770 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
771 rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
772 kfree(tm_list[i]);
773 }
774 if (locked)
775 write_unlock(&fs_info->tree_mod_log_lock);
776 kfree(tm_list);
777
778 return ret;
779 }
780
tree_mod_log_free_eb(struct extent_buffer * eb)781 static noinline int tree_mod_log_free_eb(struct extent_buffer *eb)
782 {
783 struct tree_mod_elem **tm_list = NULL;
784 int nritems = 0;
785 int i;
786 int ret = 0;
787
788 if (btrfs_header_level(eb) == 0)
789 return 0;
790
791 if (!tree_mod_need_log(eb->fs_info, NULL))
792 return 0;
793
794 nritems = btrfs_header_nritems(eb);
795 tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), GFP_NOFS);
796 if (!tm_list)
797 return -ENOMEM;
798
799 for (i = 0; i < nritems; i++) {
800 tm_list[i] = alloc_tree_mod_elem(eb, i,
801 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
802 if (!tm_list[i]) {
803 ret = -ENOMEM;
804 goto free_tms;
805 }
806 }
807
808 if (tree_mod_dont_log(eb->fs_info, eb))
809 goto free_tms;
810
811 ret = __tree_mod_log_free_eb(eb->fs_info, tm_list, nritems);
812 write_unlock(&eb->fs_info->tree_mod_log_lock);
813 if (ret)
814 goto free_tms;
815 kfree(tm_list);
816
817 return 0;
818
819 free_tms:
820 for (i = 0; i < nritems; i++)
821 kfree(tm_list[i]);
822 kfree(tm_list);
823
824 return ret;
825 }
826
827 /*
828 * check if the tree block can be shared by multiple trees
829 */
btrfs_block_can_be_shared(struct btrfs_root * root,struct extent_buffer * buf)830 int btrfs_block_can_be_shared(struct btrfs_root *root,
831 struct extent_buffer *buf)
832 {
833 /*
834 * Tree blocks not in shareable trees and tree roots are never shared.
835 * If a block was allocated after the last snapshot and the block was
836 * not allocated by tree relocation, we know the block is not shared.
837 */
838 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
839 buf != root->node && buf != root->commit_root &&
840 (btrfs_header_generation(buf) <=
841 btrfs_root_last_snapshot(&root->root_item) ||
842 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
843 return 1;
844
845 return 0;
846 }
847
update_ref_for_cow(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct extent_buffer * buf,struct extent_buffer * cow,int * last_ref)848 static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
849 struct btrfs_root *root,
850 struct extent_buffer *buf,
851 struct extent_buffer *cow,
852 int *last_ref)
853 {
854 struct btrfs_fs_info *fs_info = root->fs_info;
855 u64 refs;
856 u64 owner;
857 u64 flags;
858 u64 new_flags = 0;
859 int ret;
860
861 /*
862 * Backrefs update rules:
863 *
864 * Always use full backrefs for extent pointers in tree block
865 * allocated by tree relocation.
866 *
867 * If a shared tree block is no longer referenced by its owner
868 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
869 * use full backrefs for extent pointers in tree block.
870 *
871 * If a tree block is been relocating
872 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
873 * use full backrefs for extent pointers in tree block.
874 * The reason for this is some operations (such as drop tree)
875 * are only allowed for blocks use full backrefs.
876 */
877
878 if (btrfs_block_can_be_shared(root, buf)) {
879 ret = btrfs_lookup_extent_info(trans, fs_info, buf->start,
880 btrfs_header_level(buf), 1,
881 &refs, &flags);
882 if (ret)
883 return ret;
884 if (refs == 0) {
885 ret = -EROFS;
886 btrfs_handle_fs_error(fs_info, ret, NULL);
887 return ret;
888 }
889 } else {
890 refs = 1;
891 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
892 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
893 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
894 else
895 flags = 0;
896 }
897
898 owner = btrfs_header_owner(buf);
899 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
900 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
901
902 if (refs > 1) {
903 if ((owner == root->root_key.objectid ||
904 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
905 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
906 ret = btrfs_inc_ref(trans, root, buf, 1);
907 if (ret)
908 return ret;
909
910 if (root->root_key.objectid ==
911 BTRFS_TREE_RELOC_OBJECTID) {
912 ret = btrfs_dec_ref(trans, root, buf, 0);
913 if (ret)
914 return ret;
915 ret = btrfs_inc_ref(trans, root, cow, 1);
916 if (ret)
917 return ret;
918 }
919 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
920 } else {
921
922 if (root->root_key.objectid ==
923 BTRFS_TREE_RELOC_OBJECTID)
924 ret = btrfs_inc_ref(trans, root, cow, 1);
925 else
926 ret = btrfs_inc_ref(trans, root, cow, 0);
927 if (ret)
928 return ret;
929 }
930 if (new_flags != 0) {
931 int level = btrfs_header_level(buf);
932
933 ret = btrfs_set_disk_extent_flags(trans, buf,
934 new_flags, level, 0);
935 if (ret)
936 return ret;
937 }
938 } else {
939 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
940 if (root->root_key.objectid ==
941 BTRFS_TREE_RELOC_OBJECTID)
942 ret = btrfs_inc_ref(trans, root, cow, 1);
943 else
944 ret = btrfs_inc_ref(trans, root, cow, 0);
945 if (ret)
946 return ret;
947 ret = btrfs_dec_ref(trans, root, buf, 1);
948 if (ret)
949 return ret;
950 }
951 btrfs_clean_tree_block(buf);
952 *last_ref = 1;
953 }
954 return 0;
955 }
956
alloc_tree_block_no_bg_flush(struct btrfs_trans_handle * trans,struct btrfs_root * root,u64 parent_start,const struct btrfs_disk_key * disk_key,int level,u64 hint,u64 empty_size,enum btrfs_lock_nesting nest)957 static struct extent_buffer *alloc_tree_block_no_bg_flush(
958 struct btrfs_trans_handle *trans,
959 struct btrfs_root *root,
960 u64 parent_start,
961 const struct btrfs_disk_key *disk_key,
962 int level,
963 u64 hint,
964 u64 empty_size,
965 enum btrfs_lock_nesting nest)
966 {
967 struct btrfs_fs_info *fs_info = root->fs_info;
968 struct extent_buffer *ret;
969
970 /*
971 * If we are COWing a node/leaf from the extent, chunk, device or free
972 * space trees, make sure that we do not finish block group creation of
973 * pending block groups. We do this to avoid a deadlock.
974 * COWing can result in allocation of a new chunk, and flushing pending
975 * block groups (btrfs_create_pending_block_groups()) can be triggered
976 * when finishing allocation of a new chunk. Creation of a pending block
977 * group modifies the extent, chunk, device and free space trees,
978 * therefore we could deadlock with ourselves since we are holding a
979 * lock on an extent buffer that btrfs_create_pending_block_groups() may
980 * try to COW later.
981 * For similar reasons, we also need to delay flushing pending block
982 * groups when splitting a leaf or node, from one of those trees, since
983 * we are holding a write lock on it and its parent or when inserting a
984 * new root node for one of those trees.
985 */
986 if (root == fs_info->extent_root ||
987 root == fs_info->chunk_root ||
988 root == fs_info->dev_root ||
989 root == fs_info->free_space_root)
990 trans->can_flush_pending_bgs = false;
991
992 ret = btrfs_alloc_tree_block(trans, root, parent_start,
993 root->root_key.objectid, disk_key, level,
994 hint, empty_size, nest);
995 trans->can_flush_pending_bgs = true;
996
997 return ret;
998 }
999
1000 /*
1001 * does the dirty work in cow of a single block. The parent block (if
1002 * supplied) is updated to point to the new cow copy. The new buffer is marked
1003 * dirty and returned locked. If you modify the block it needs to be marked
1004 * dirty again.
1005 *
1006 * search_start -- an allocation hint for the new block
1007 *
1008 * empty_size -- a hint that you plan on doing more cow. This is the size in
1009 * bytes the allocator should try to find free next to the block it returns.
1010 * This is just a hint and may be ignored by the allocator.
1011 */
__btrfs_cow_block(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct extent_buffer * buf,struct extent_buffer * parent,int parent_slot,struct extent_buffer ** cow_ret,u64 search_start,u64 empty_size,enum btrfs_lock_nesting nest)1012 static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
1013 struct btrfs_root *root,
1014 struct extent_buffer *buf,
1015 struct extent_buffer *parent, int parent_slot,
1016 struct extent_buffer **cow_ret,
1017 u64 search_start, u64 empty_size,
1018 enum btrfs_lock_nesting nest)
1019 {
1020 struct btrfs_fs_info *fs_info = root->fs_info;
1021 struct btrfs_disk_key disk_key;
1022 struct extent_buffer *cow;
1023 int level, ret;
1024 int last_ref = 0;
1025 int unlock_orig = 0;
1026 u64 parent_start = 0;
1027
1028 if (*cow_ret == buf)
1029 unlock_orig = 1;
1030
1031 btrfs_assert_tree_locked(buf);
1032
1033 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
1034 trans->transid != fs_info->running_transaction->transid);
1035 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
1036 trans->transid != root->last_trans);
1037
1038 level = btrfs_header_level(buf);
1039
1040 if (level == 0)
1041 btrfs_item_key(buf, &disk_key, 0);
1042 else
1043 btrfs_node_key(buf, &disk_key, 0);
1044
1045 if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent)
1046 parent_start = parent->start;
1047
1048 cow = alloc_tree_block_no_bg_flush(trans, root, parent_start, &disk_key,
1049 level, search_start, empty_size, nest);
1050 if (IS_ERR(cow))
1051 return PTR_ERR(cow);
1052
1053 /* cow is set to blocking by btrfs_init_new_buffer */
1054
1055 copy_extent_buffer_full(cow, buf);
1056 btrfs_set_header_bytenr(cow, cow->start);
1057 btrfs_set_header_generation(cow, trans->transid);
1058 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
1059 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
1060 BTRFS_HEADER_FLAG_RELOC);
1061 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1062 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
1063 else
1064 btrfs_set_header_owner(cow, root->root_key.objectid);
1065
1066 write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid);
1067
1068 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
1069 if (ret) {
1070 btrfs_tree_unlock(cow);
1071 free_extent_buffer(cow);
1072 btrfs_abort_transaction(trans, ret);
1073 return ret;
1074 }
1075
1076 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) {
1077 ret = btrfs_reloc_cow_block(trans, root, buf, cow);
1078 if (ret) {
1079 btrfs_tree_unlock(cow);
1080 free_extent_buffer(cow);
1081 btrfs_abort_transaction(trans, ret);
1082 return ret;
1083 }
1084 }
1085
1086 if (buf == root->node) {
1087 WARN_ON(parent && parent != buf);
1088 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1089 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1090 parent_start = buf->start;
1091
1092 atomic_inc(&cow->refs);
1093 ret = tree_mod_log_insert_root(root->node, cow, 1);
1094 BUG_ON(ret < 0);
1095 rcu_assign_pointer(root->node, cow);
1096
1097 btrfs_free_tree_block(trans, root, buf, parent_start,
1098 last_ref);
1099 free_extent_buffer(buf);
1100 add_root_to_dirty_list(root);
1101 } else {
1102 WARN_ON(trans->transid != btrfs_header_generation(parent));
1103 tree_mod_log_insert_key(parent, parent_slot,
1104 MOD_LOG_KEY_REPLACE, GFP_NOFS);
1105 btrfs_set_node_blockptr(parent, parent_slot,
1106 cow->start);
1107 btrfs_set_node_ptr_generation(parent, parent_slot,
1108 trans->transid);
1109 btrfs_mark_buffer_dirty(parent);
1110 if (last_ref) {
1111 ret = tree_mod_log_free_eb(buf);
1112 if (ret) {
1113 btrfs_tree_unlock(cow);
1114 free_extent_buffer(cow);
1115 btrfs_abort_transaction(trans, ret);
1116 return ret;
1117 }
1118 }
1119 btrfs_free_tree_block(trans, root, buf, parent_start,
1120 last_ref);
1121 }
1122 if (unlock_orig)
1123 btrfs_tree_unlock(buf);
1124 free_extent_buffer_stale(buf);
1125 btrfs_mark_buffer_dirty(cow);
1126 *cow_ret = cow;
1127 return 0;
1128 }
1129
1130 /*
1131 * returns the logical address of the oldest predecessor of the given root.
1132 * entries older than time_seq are ignored.
1133 */
__tree_mod_log_oldest_root(struct extent_buffer * eb_root,u64 time_seq)1134 static struct tree_mod_elem *__tree_mod_log_oldest_root(
1135 struct extent_buffer *eb_root, u64 time_seq)
1136 {
1137 struct tree_mod_elem *tm;
1138 struct tree_mod_elem *found = NULL;
1139 u64 root_logical = eb_root->start;
1140 int looped = 0;
1141
1142 if (!time_seq)
1143 return NULL;
1144
1145 /*
1146 * the very last operation that's logged for a root is the
1147 * replacement operation (if it is replaced at all). this has
1148 * the logical address of the *new* root, making it the very
1149 * first operation that's logged for this root.
1150 */
1151 while (1) {
1152 tm = tree_mod_log_search_oldest(eb_root->fs_info, root_logical,
1153 time_seq);
1154 if (!looped && !tm)
1155 return NULL;
1156 /*
1157 * if there are no tree operation for the oldest root, we simply
1158 * return it. this should only happen if that (old) root is at
1159 * level 0.
1160 */
1161 if (!tm)
1162 break;
1163
1164 /*
1165 * if there's an operation that's not a root replacement, we
1166 * found the oldest version of our root. normally, we'll find a
1167 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
1168 */
1169 if (tm->op != MOD_LOG_ROOT_REPLACE)
1170 break;
1171
1172 found = tm;
1173 root_logical = tm->old_root.logical;
1174 looped = 1;
1175 }
1176
1177 /* if there's no old root to return, return what we found instead */
1178 if (!found)
1179 found = tm;
1180
1181 return found;
1182 }
1183
1184 /*
1185 * tm is a pointer to the first operation to rewind within eb. then, all
1186 * previous operations will be rewound (until we reach something older than
1187 * time_seq).
1188 */
1189 static void
__tree_mod_log_rewind(struct btrfs_fs_info * fs_info,struct extent_buffer * eb,u64 time_seq,struct tree_mod_elem * first_tm)1190 __tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1191 u64 time_seq, struct tree_mod_elem *first_tm)
1192 {
1193 u32 n;
1194 struct rb_node *next;
1195 struct tree_mod_elem *tm = first_tm;
1196 unsigned long o_dst;
1197 unsigned long o_src;
1198 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1199
1200 n = btrfs_header_nritems(eb);
1201 read_lock(&fs_info->tree_mod_log_lock);
1202 while (tm && tm->seq >= time_seq) {
1203 /*
1204 * all the operations are recorded with the operator used for
1205 * the modification. as we're going backwards, we do the
1206 * opposite of each operation here.
1207 */
1208 switch (tm->op) {
1209 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1210 BUG_ON(tm->slot < n);
1211 fallthrough;
1212 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
1213 case MOD_LOG_KEY_REMOVE:
1214 btrfs_set_node_key(eb, &tm->key, tm->slot);
1215 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1216 btrfs_set_node_ptr_generation(eb, tm->slot,
1217 tm->generation);
1218 n++;
1219 break;
1220 case MOD_LOG_KEY_REPLACE:
1221 BUG_ON(tm->slot >= n);
1222 btrfs_set_node_key(eb, &tm->key, tm->slot);
1223 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1224 btrfs_set_node_ptr_generation(eb, tm->slot,
1225 tm->generation);
1226 break;
1227 case MOD_LOG_KEY_ADD:
1228 /* if a move operation is needed it's in the log */
1229 n--;
1230 break;
1231 case MOD_LOG_MOVE_KEYS:
1232 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1233 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1234 memmove_extent_buffer(eb, o_dst, o_src,
1235 tm->move.nr_items * p_size);
1236 break;
1237 case MOD_LOG_ROOT_REPLACE:
1238 /*
1239 * this operation is special. for roots, this must be
1240 * handled explicitly before rewinding.
1241 * for non-roots, this operation may exist if the node
1242 * was a root: root A -> child B; then A gets empty and
1243 * B is promoted to the new root. in the mod log, we'll
1244 * have a root-replace operation for B, a tree block
1245 * that is no root. we simply ignore that operation.
1246 */
1247 break;
1248 }
1249 next = rb_next(&tm->node);
1250 if (!next)
1251 break;
1252 tm = rb_entry(next, struct tree_mod_elem, node);
1253 if (tm->logical != first_tm->logical)
1254 break;
1255 }
1256 read_unlock(&fs_info->tree_mod_log_lock);
1257 btrfs_set_header_nritems(eb, n);
1258 }
1259
1260 /*
1261 * Called with eb read locked. If the buffer cannot be rewound, the same buffer
1262 * is returned. If rewind operations happen, a fresh buffer is returned. The
1263 * returned buffer is always read-locked. If the returned buffer is not the
1264 * input buffer, the lock on the input buffer is released and the input buffer
1265 * is freed (its refcount is decremented).
1266 */
1267 static struct extent_buffer *
tree_mod_log_rewind(struct btrfs_fs_info * fs_info,struct btrfs_path * path,struct extent_buffer * eb,u64 time_seq)1268 tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
1269 struct extent_buffer *eb, u64 time_seq)
1270 {
1271 struct extent_buffer *eb_rewin;
1272 struct tree_mod_elem *tm;
1273
1274 if (!time_seq)
1275 return eb;
1276
1277 if (btrfs_header_level(eb) == 0)
1278 return eb;
1279
1280 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1281 if (!tm)
1282 return eb;
1283
1284 btrfs_set_path_blocking(path);
1285 btrfs_set_lock_blocking_read(eb);
1286
1287 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1288 BUG_ON(tm->slot != 0);
1289 eb_rewin = alloc_dummy_extent_buffer(fs_info, eb->start);
1290 if (!eb_rewin) {
1291 btrfs_tree_read_unlock_blocking(eb);
1292 free_extent_buffer(eb);
1293 return NULL;
1294 }
1295 btrfs_set_header_bytenr(eb_rewin, eb->start);
1296 btrfs_set_header_backref_rev(eb_rewin,
1297 btrfs_header_backref_rev(eb));
1298 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
1299 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
1300 } else {
1301 eb_rewin = btrfs_clone_extent_buffer(eb);
1302 if (!eb_rewin) {
1303 btrfs_tree_read_unlock_blocking(eb);
1304 free_extent_buffer(eb);
1305 return NULL;
1306 }
1307 }
1308
1309 btrfs_tree_read_unlock_blocking(eb);
1310 free_extent_buffer(eb);
1311
1312 btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb_rewin),
1313 eb_rewin, btrfs_header_level(eb_rewin));
1314 btrfs_tree_read_lock(eb_rewin);
1315 __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
1316 WARN_ON(btrfs_header_nritems(eb_rewin) >
1317 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
1318
1319 return eb_rewin;
1320 }
1321
1322 /*
1323 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1324 * value. If there are no changes, the current root->root_node is returned. If
1325 * anything changed in between, there's a fresh buffer allocated on which the
1326 * rewind operations are done. In any case, the returned buffer is read locked.
1327 * Returns NULL on error (with no locks held).
1328 */
1329 static inline struct extent_buffer *
get_old_root(struct btrfs_root * root,u64 time_seq)1330 get_old_root(struct btrfs_root *root, u64 time_seq)
1331 {
1332 struct btrfs_fs_info *fs_info = root->fs_info;
1333 struct tree_mod_elem *tm;
1334 struct extent_buffer *eb = NULL;
1335 struct extent_buffer *eb_root;
1336 u64 eb_root_owner = 0;
1337 struct extent_buffer *old;
1338 struct tree_mod_root *old_root = NULL;
1339 u64 old_generation = 0;
1340 u64 logical;
1341 int level;
1342
1343 eb_root = btrfs_read_lock_root_node(root);
1344 tm = __tree_mod_log_oldest_root(eb_root, time_seq);
1345 if (!tm)
1346 return eb_root;
1347
1348 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1349 old_root = &tm->old_root;
1350 old_generation = tm->generation;
1351 logical = old_root->logical;
1352 level = old_root->level;
1353 } else {
1354 logical = eb_root->start;
1355 level = btrfs_header_level(eb_root);
1356 }
1357
1358 tm = tree_mod_log_search(fs_info, logical, time_seq);
1359 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1360 btrfs_tree_read_unlock(eb_root);
1361 free_extent_buffer(eb_root);
1362 old = read_tree_block(fs_info, logical, 0, level, NULL);
1363 if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) {
1364 if (!IS_ERR(old))
1365 free_extent_buffer(old);
1366 btrfs_warn(fs_info,
1367 "failed to read tree block %llu from get_old_root",
1368 logical);
1369 } else {
1370 struct tree_mod_elem *tm2;
1371
1372 btrfs_tree_read_lock(old);
1373 eb = btrfs_clone_extent_buffer(old);
1374 /*
1375 * After the lookup for the most recent tree mod operation
1376 * above and before we locked and cloned the extent buffer
1377 * 'old', a new tree mod log operation may have been added.
1378 * So lookup for a more recent one to make sure the number
1379 * of mod log operations we replay is consistent with the
1380 * number of items we have in the cloned extent buffer,
1381 * otherwise we can hit a BUG_ON when rewinding the extent
1382 * buffer.
1383 */
1384 tm2 = tree_mod_log_search(fs_info, logical, time_seq);
1385 btrfs_tree_read_unlock(old);
1386 free_extent_buffer(old);
1387 ASSERT(tm2);
1388 ASSERT(tm2 == tm || tm2->seq > tm->seq);
1389 if (!tm2 || tm2->seq < tm->seq) {
1390 free_extent_buffer(eb);
1391 return NULL;
1392 }
1393 tm = tm2;
1394 }
1395 } else if (old_root) {
1396 eb_root_owner = btrfs_header_owner(eb_root);
1397 btrfs_tree_read_unlock(eb_root);
1398 free_extent_buffer(eb_root);
1399 eb = alloc_dummy_extent_buffer(fs_info, logical);
1400 } else {
1401 btrfs_set_lock_blocking_read(eb_root);
1402 eb = btrfs_clone_extent_buffer(eb_root);
1403 btrfs_tree_read_unlock_blocking(eb_root);
1404 free_extent_buffer(eb_root);
1405 }
1406
1407 if (!eb)
1408 return NULL;
1409 if (old_root) {
1410 btrfs_set_header_bytenr(eb, eb->start);
1411 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
1412 btrfs_set_header_owner(eb, eb_root_owner);
1413 btrfs_set_header_level(eb, old_root->level);
1414 btrfs_set_header_generation(eb, old_generation);
1415 }
1416 btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), eb,
1417 btrfs_header_level(eb));
1418 btrfs_tree_read_lock(eb);
1419 if (tm)
1420 __tree_mod_log_rewind(fs_info, eb, time_seq, tm);
1421 else
1422 WARN_ON(btrfs_header_level(eb) != 0);
1423 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(fs_info));
1424
1425 return eb;
1426 }
1427
btrfs_old_root_level(struct btrfs_root * root,u64 time_seq)1428 int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
1429 {
1430 struct tree_mod_elem *tm;
1431 int level;
1432 struct extent_buffer *eb_root = btrfs_root_node(root);
1433
1434 tm = __tree_mod_log_oldest_root(eb_root, time_seq);
1435 if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
1436 level = tm->old_root.level;
1437 } else {
1438 level = btrfs_header_level(eb_root);
1439 }
1440 free_extent_buffer(eb_root);
1441
1442 return level;
1443 }
1444
should_cow_block(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct extent_buffer * buf)1445 static inline int should_cow_block(struct btrfs_trans_handle *trans,
1446 struct btrfs_root *root,
1447 struct extent_buffer *buf)
1448 {
1449 if (btrfs_is_testing(root->fs_info))
1450 return 0;
1451
1452 /* Ensure we can see the FORCE_COW bit */
1453 smp_mb__before_atomic();
1454
1455 /*
1456 * We do not need to cow a block if
1457 * 1) this block is not created or changed in this transaction;
1458 * 2) this block does not belong to TREE_RELOC tree;
1459 * 3) the root is not forced COW.
1460 *
1461 * What is forced COW:
1462 * when we create snapshot during committing the transaction,
1463 * after we've finished copying src root, we must COW the shared
1464 * block to ensure the metadata consistency.
1465 */
1466 if (btrfs_header_generation(buf) == trans->transid &&
1467 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1468 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
1469 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
1470 !test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
1471 return 0;
1472 return 1;
1473 }
1474
1475 /*
1476 * cows a single block, see __btrfs_cow_block for the real work.
1477 * This version of it has extra checks so that a block isn't COWed more than
1478 * once per transaction, as long as it hasn't been written yet
1479 */
btrfs_cow_block(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct extent_buffer * buf,struct extent_buffer * parent,int parent_slot,struct extent_buffer ** cow_ret,enum btrfs_lock_nesting nest)1480 noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
1481 struct btrfs_root *root, struct extent_buffer *buf,
1482 struct extent_buffer *parent, int parent_slot,
1483 struct extent_buffer **cow_ret,
1484 enum btrfs_lock_nesting nest)
1485 {
1486 struct btrfs_fs_info *fs_info = root->fs_info;
1487 u64 search_start;
1488 int ret;
1489
1490 if (test_bit(BTRFS_ROOT_DELETING, &root->state))
1491 btrfs_err(fs_info,
1492 "COW'ing blocks on a fs root that's being dropped");
1493
1494 if (trans->transaction != fs_info->running_transaction)
1495 WARN(1, KERN_CRIT "trans %llu running %llu\n",
1496 trans->transid,
1497 fs_info->running_transaction->transid);
1498
1499 if (trans->transid != fs_info->generation)
1500 WARN(1, KERN_CRIT "trans %llu running %llu\n",
1501 trans->transid, fs_info->generation);
1502
1503 if (!should_cow_block(trans, root, buf)) {
1504 trans->dirty = true;
1505 *cow_ret = buf;
1506 return 0;
1507 }
1508
1509 search_start = buf->start & ~((u64)SZ_1G - 1);
1510
1511 if (parent)
1512 btrfs_set_lock_blocking_write(parent);
1513 btrfs_set_lock_blocking_write(buf);
1514
1515 /*
1516 * Before CoWing this block for later modification, check if it's
1517 * the subtree root and do the delayed subtree trace if needed.
1518 *
1519 * Also We don't care about the error, as it's handled internally.
1520 */
1521 btrfs_qgroup_trace_subtree_after_cow(trans, root, buf);
1522 ret = __btrfs_cow_block(trans, root, buf, parent,
1523 parent_slot, cow_ret, search_start, 0, nest);
1524
1525 trace_btrfs_cow_block(root, buf, *cow_ret);
1526
1527 return ret;
1528 }
1529
1530 /*
1531 * helper function for defrag to decide if two blocks pointed to by a
1532 * node are actually close by
1533 */
close_blocks(u64 blocknr,u64 other,u32 blocksize)1534 static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
1535 {
1536 if (blocknr < other && other - (blocknr + blocksize) < 32768)
1537 return 1;
1538 if (blocknr > other && blocknr - (other + blocksize) < 32768)
1539 return 1;
1540 return 0;
1541 }
1542
1543 #ifdef __LITTLE_ENDIAN
1544
1545 /*
1546 * Compare two keys, on little-endian the disk order is same as CPU order and
1547 * we can avoid the conversion.
1548 */
comp_keys(const struct btrfs_disk_key * disk_key,const struct btrfs_key * k2)1549 static int comp_keys(const struct btrfs_disk_key *disk_key,
1550 const struct btrfs_key *k2)
1551 {
1552 const struct btrfs_key *k1 = (const struct btrfs_key *)disk_key;
1553
1554 return btrfs_comp_cpu_keys(k1, k2);
1555 }
1556
1557 #else
1558
1559 /*
1560 * compare two keys in a memcmp fashion
1561 */
comp_keys(const struct btrfs_disk_key * disk,const struct btrfs_key * k2)1562 static int comp_keys(const struct btrfs_disk_key *disk,
1563 const struct btrfs_key *k2)
1564 {
1565 struct btrfs_key k1;
1566
1567 btrfs_disk_key_to_cpu(&k1, disk);
1568
1569 return btrfs_comp_cpu_keys(&k1, k2);
1570 }
1571 #endif
1572
1573 /*
1574 * same as comp_keys only with two btrfs_key's
1575 */
btrfs_comp_cpu_keys(const struct btrfs_key * k1,const struct btrfs_key * k2)1576 int __pure btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2)
1577 {
1578 if (k1->objectid > k2->objectid)
1579 return 1;
1580 if (k1->objectid < k2->objectid)
1581 return -1;
1582 if (k1->type > k2->type)
1583 return 1;
1584 if (k1->type < k2->type)
1585 return -1;
1586 if (k1->offset > k2->offset)
1587 return 1;
1588 if (k1->offset < k2->offset)
1589 return -1;
1590 return 0;
1591 }
1592
1593 /*
1594 * this is used by the defrag code to go through all the
1595 * leaves pointed to by a node and reallocate them so that
1596 * disk order is close to key order
1597 */
btrfs_realloc_node(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct extent_buffer * parent,int start_slot,u64 * last_ret,struct btrfs_key * progress)1598 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
1599 struct btrfs_root *root, struct extent_buffer *parent,
1600 int start_slot, u64 *last_ret,
1601 struct btrfs_key *progress)
1602 {
1603 struct btrfs_fs_info *fs_info = root->fs_info;
1604 struct extent_buffer *cur;
1605 u64 blocknr;
1606 u64 gen;
1607 u64 search_start = *last_ret;
1608 u64 last_block = 0;
1609 u64 other;
1610 u32 parent_nritems;
1611 int end_slot;
1612 int i;
1613 int err = 0;
1614 int parent_level;
1615 int uptodate;
1616 u32 blocksize;
1617 int progress_passed = 0;
1618 struct btrfs_disk_key disk_key;
1619
1620 parent_level = btrfs_header_level(parent);
1621
1622 WARN_ON(trans->transaction != fs_info->running_transaction);
1623 WARN_ON(trans->transid != fs_info->generation);
1624
1625 parent_nritems = btrfs_header_nritems(parent);
1626 blocksize = fs_info->nodesize;
1627 end_slot = parent_nritems - 1;
1628
1629 if (parent_nritems <= 1)
1630 return 0;
1631
1632 btrfs_set_lock_blocking_write(parent);
1633
1634 for (i = start_slot; i <= end_slot; i++) {
1635 struct btrfs_key first_key;
1636 int close = 1;
1637
1638 btrfs_node_key(parent, &disk_key, i);
1639 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1640 continue;
1641
1642 progress_passed = 1;
1643 blocknr = btrfs_node_blockptr(parent, i);
1644 gen = btrfs_node_ptr_generation(parent, i);
1645 btrfs_node_key_to_cpu(parent, &first_key, i);
1646 if (last_block == 0)
1647 last_block = blocknr;
1648
1649 if (i > 0) {
1650 other = btrfs_node_blockptr(parent, i - 1);
1651 close = close_blocks(blocknr, other, blocksize);
1652 }
1653 if (!close && i < end_slot) {
1654 other = btrfs_node_blockptr(parent, i + 1);
1655 close = close_blocks(blocknr, other, blocksize);
1656 }
1657 if (close) {
1658 last_block = blocknr;
1659 continue;
1660 }
1661
1662 cur = find_extent_buffer(fs_info, blocknr);
1663 if (cur)
1664 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
1665 else
1666 uptodate = 0;
1667 if (!cur || !uptodate) {
1668 if (!cur) {
1669 cur = read_tree_block(fs_info, blocknr, gen,
1670 parent_level - 1,
1671 &first_key);
1672 if (IS_ERR(cur)) {
1673 return PTR_ERR(cur);
1674 } else if (!extent_buffer_uptodate(cur)) {
1675 free_extent_buffer(cur);
1676 return -EIO;
1677 }
1678 } else if (!uptodate) {
1679 err = btrfs_read_buffer(cur, gen,
1680 parent_level - 1,&first_key);
1681 if (err) {
1682 free_extent_buffer(cur);
1683 return err;
1684 }
1685 }
1686 }
1687 if (search_start == 0)
1688 search_start = last_block;
1689
1690 btrfs_tree_lock(cur);
1691 btrfs_set_lock_blocking_write(cur);
1692 err = __btrfs_cow_block(trans, root, cur, parent, i,
1693 &cur, search_start,
1694 min(16 * blocksize,
1695 (end_slot - i) * blocksize),
1696 BTRFS_NESTING_COW);
1697 if (err) {
1698 btrfs_tree_unlock(cur);
1699 free_extent_buffer(cur);
1700 break;
1701 }
1702 search_start = cur->start;
1703 last_block = cur->start;
1704 *last_ret = search_start;
1705 btrfs_tree_unlock(cur);
1706 free_extent_buffer(cur);
1707 }
1708 return err;
1709 }
1710
1711 /*
1712 * search for key in the extent_buffer. The items start at offset p,
1713 * and they are item_size apart. There are 'max' items in p.
1714 *
1715 * the slot in the array is returned via slot, and it points to
1716 * the place where you would insert key if it is not found in
1717 * the array.
1718 *
1719 * slot may point to max if the key is bigger than all of the keys
1720 */
generic_bin_search(struct extent_buffer * eb,unsigned long p,int item_size,const struct btrfs_key * key,int max,int * slot)1721 static noinline int generic_bin_search(struct extent_buffer *eb,
1722 unsigned long p, int item_size,
1723 const struct btrfs_key *key,
1724 int max, int *slot)
1725 {
1726 int low = 0;
1727 int high = max;
1728 int ret;
1729 const int key_size = sizeof(struct btrfs_disk_key);
1730
1731 if (low > high) {
1732 btrfs_err(eb->fs_info,
1733 "%s: low (%d) > high (%d) eb %llu owner %llu level %d",
1734 __func__, low, high, eb->start,
1735 btrfs_header_owner(eb), btrfs_header_level(eb));
1736 return -EINVAL;
1737 }
1738
1739 while (low < high) {
1740 unsigned long oip;
1741 unsigned long offset;
1742 struct btrfs_disk_key *tmp;
1743 struct btrfs_disk_key unaligned;
1744 int mid;
1745
1746 mid = (low + high) / 2;
1747 offset = p + mid * item_size;
1748 oip = offset_in_page(offset);
1749
1750 if (oip + key_size <= PAGE_SIZE) {
1751 const unsigned long idx = offset >> PAGE_SHIFT;
1752 char *kaddr = page_address(eb->pages[idx]);
1753
1754 tmp = (struct btrfs_disk_key *)(kaddr + oip);
1755 } else {
1756 read_extent_buffer(eb, &unaligned, offset, key_size);
1757 tmp = &unaligned;
1758 }
1759
1760 ret = comp_keys(tmp, key);
1761
1762 if (ret < 0)
1763 low = mid + 1;
1764 else if (ret > 0)
1765 high = mid;
1766 else {
1767 *slot = mid;
1768 return 0;
1769 }
1770 }
1771 *slot = low;
1772 return 1;
1773 }
1774
1775 /*
1776 * simple bin_search frontend that does the right thing for
1777 * leaves vs nodes
1778 */
btrfs_bin_search(struct extent_buffer * eb,const struct btrfs_key * key,int * slot)1779 int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
1780 int *slot)
1781 {
1782 if (btrfs_header_level(eb) == 0)
1783 return generic_bin_search(eb,
1784 offsetof(struct btrfs_leaf, items),
1785 sizeof(struct btrfs_item),
1786 key, btrfs_header_nritems(eb),
1787 slot);
1788 else
1789 return generic_bin_search(eb,
1790 offsetof(struct btrfs_node, ptrs),
1791 sizeof(struct btrfs_key_ptr),
1792 key, btrfs_header_nritems(eb),
1793 slot);
1794 }
1795
root_add_used(struct btrfs_root * root,u32 size)1796 static void root_add_used(struct btrfs_root *root, u32 size)
1797 {
1798 spin_lock(&root->accounting_lock);
1799 btrfs_set_root_used(&root->root_item,
1800 btrfs_root_used(&root->root_item) + size);
1801 spin_unlock(&root->accounting_lock);
1802 }
1803
root_sub_used(struct btrfs_root * root,u32 size)1804 static void root_sub_used(struct btrfs_root *root, u32 size)
1805 {
1806 spin_lock(&root->accounting_lock);
1807 btrfs_set_root_used(&root->root_item,
1808 btrfs_root_used(&root->root_item) - size);
1809 spin_unlock(&root->accounting_lock);
1810 }
1811
1812 /* given a node and slot number, this reads the blocks it points to. The
1813 * extent buffer is returned with a reference taken (but unlocked).
1814 */
btrfs_read_node_slot(struct extent_buffer * parent,int slot)1815 struct extent_buffer *btrfs_read_node_slot(struct extent_buffer *parent,
1816 int slot)
1817 {
1818 int level = btrfs_header_level(parent);
1819 struct extent_buffer *eb;
1820 struct btrfs_key first_key;
1821
1822 if (slot < 0 || slot >= btrfs_header_nritems(parent))
1823 return ERR_PTR(-ENOENT);
1824
1825 BUG_ON(level == 0);
1826
1827 btrfs_node_key_to_cpu(parent, &first_key, slot);
1828 eb = read_tree_block(parent->fs_info, btrfs_node_blockptr(parent, slot),
1829 btrfs_node_ptr_generation(parent, slot),
1830 level - 1, &first_key);
1831 if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) {
1832 free_extent_buffer(eb);
1833 eb = ERR_PTR(-EIO);
1834 }
1835
1836 return eb;
1837 }
1838
1839 /*
1840 * node level balancing, used to make sure nodes are in proper order for
1841 * item deletion. We balance from the top down, so we have to make sure
1842 * that a deletion won't leave an node completely empty later on.
1843 */
balance_level(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,int level)1844 static noinline int balance_level(struct btrfs_trans_handle *trans,
1845 struct btrfs_root *root,
1846 struct btrfs_path *path, int level)
1847 {
1848 struct btrfs_fs_info *fs_info = root->fs_info;
1849 struct extent_buffer *right = NULL;
1850 struct extent_buffer *mid;
1851 struct extent_buffer *left = NULL;
1852 struct extent_buffer *parent = NULL;
1853 int ret = 0;
1854 int wret;
1855 int pslot;
1856 int orig_slot = path->slots[level];
1857 u64 orig_ptr;
1858
1859 ASSERT(level > 0);
1860
1861 mid = path->nodes[level];
1862
1863 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1864 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
1865 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1866
1867 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
1868
1869 if (level < BTRFS_MAX_LEVEL - 1) {
1870 parent = path->nodes[level + 1];
1871 pslot = path->slots[level + 1];
1872 }
1873
1874 /*
1875 * deal with the case where there is only one pointer in the root
1876 * by promoting the node below to a root
1877 */
1878 if (!parent) {
1879 struct extent_buffer *child;
1880
1881 if (btrfs_header_nritems(mid) != 1)
1882 return 0;
1883
1884 /* promote the child to a root */
1885 child = btrfs_read_node_slot(mid, 0);
1886 if (IS_ERR(child)) {
1887 ret = PTR_ERR(child);
1888 btrfs_handle_fs_error(fs_info, ret, NULL);
1889 goto enospc;
1890 }
1891
1892 btrfs_tree_lock(child);
1893 btrfs_set_lock_blocking_write(child);
1894 ret = btrfs_cow_block(trans, root, child, mid, 0, &child,
1895 BTRFS_NESTING_COW);
1896 if (ret) {
1897 btrfs_tree_unlock(child);
1898 free_extent_buffer(child);
1899 goto enospc;
1900 }
1901
1902 ret = tree_mod_log_insert_root(root->node, child, 1);
1903 BUG_ON(ret < 0);
1904 rcu_assign_pointer(root->node, child);
1905
1906 add_root_to_dirty_list(root);
1907 btrfs_tree_unlock(child);
1908
1909 path->locks[level] = 0;
1910 path->nodes[level] = NULL;
1911 btrfs_clean_tree_block(mid);
1912 btrfs_tree_unlock(mid);
1913 /* once for the path */
1914 free_extent_buffer(mid);
1915
1916 root_sub_used(root, mid->len);
1917 btrfs_free_tree_block(trans, root, mid, 0, 1);
1918 /* once for the root ptr */
1919 free_extent_buffer_stale(mid);
1920 return 0;
1921 }
1922 if (btrfs_header_nritems(mid) >
1923 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4)
1924 return 0;
1925
1926 left = btrfs_read_node_slot(parent, pslot - 1);
1927 if (IS_ERR(left))
1928 left = NULL;
1929
1930 if (left) {
1931 __btrfs_tree_lock(left, BTRFS_NESTING_LEFT);
1932 btrfs_set_lock_blocking_write(left);
1933 wret = btrfs_cow_block(trans, root, left,
1934 parent, pslot - 1, &left,
1935 BTRFS_NESTING_LEFT_COW);
1936 if (wret) {
1937 ret = wret;
1938 goto enospc;
1939 }
1940 }
1941
1942 right = btrfs_read_node_slot(parent, pslot + 1);
1943 if (IS_ERR(right))
1944 right = NULL;
1945
1946 if (right) {
1947 __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT);
1948 btrfs_set_lock_blocking_write(right);
1949 wret = btrfs_cow_block(trans, root, right,
1950 parent, pslot + 1, &right,
1951 BTRFS_NESTING_RIGHT_COW);
1952 if (wret) {
1953 ret = wret;
1954 goto enospc;
1955 }
1956 }
1957
1958 /* first, try to make some room in the middle buffer */
1959 if (left) {
1960 orig_slot += btrfs_header_nritems(left);
1961 wret = push_node_left(trans, left, mid, 1);
1962 if (wret < 0)
1963 ret = wret;
1964 }
1965
1966 /*
1967 * then try to empty the right most buffer into the middle
1968 */
1969 if (right) {
1970 wret = push_node_left(trans, mid, right, 1);
1971 if (wret < 0 && wret != -ENOSPC)
1972 ret = wret;
1973 if (btrfs_header_nritems(right) == 0) {
1974 btrfs_clean_tree_block(right);
1975 btrfs_tree_unlock(right);
1976 del_ptr(root, path, level + 1, pslot + 1);
1977 root_sub_used(root, right->len);
1978 btrfs_free_tree_block(trans, root, right, 0, 1);
1979 free_extent_buffer_stale(right);
1980 right = NULL;
1981 } else {
1982 struct btrfs_disk_key right_key;
1983 btrfs_node_key(right, &right_key, 0);
1984 ret = tree_mod_log_insert_key(parent, pslot + 1,
1985 MOD_LOG_KEY_REPLACE, GFP_NOFS);
1986 BUG_ON(ret < 0);
1987 btrfs_set_node_key(parent, &right_key, pslot + 1);
1988 btrfs_mark_buffer_dirty(parent);
1989 }
1990 }
1991 if (btrfs_header_nritems(mid) == 1) {
1992 /*
1993 * we're not allowed to leave a node with one item in the
1994 * tree during a delete. A deletion from lower in the tree
1995 * could try to delete the only pointer in this node.
1996 * So, pull some keys from the left.
1997 * There has to be a left pointer at this point because
1998 * otherwise we would have pulled some pointers from the
1999 * right
2000 */
2001 if (!left) {
2002 ret = -EROFS;
2003 btrfs_handle_fs_error(fs_info, ret, NULL);
2004 goto enospc;
2005 }
2006 wret = balance_node_right(trans, mid, left);
2007 if (wret < 0) {
2008 ret = wret;
2009 goto enospc;
2010 }
2011 if (wret == 1) {
2012 wret = push_node_left(trans, left, mid, 1);
2013 if (wret < 0)
2014 ret = wret;
2015 }
2016 BUG_ON(wret == 1);
2017 }
2018 if (btrfs_header_nritems(mid) == 0) {
2019 btrfs_clean_tree_block(mid);
2020 btrfs_tree_unlock(mid);
2021 del_ptr(root, path, level + 1, pslot);
2022 root_sub_used(root, mid->len);
2023 btrfs_free_tree_block(trans, root, mid, 0, 1);
2024 free_extent_buffer_stale(mid);
2025 mid = NULL;
2026 } else {
2027 /* update the parent key to reflect our changes */
2028 struct btrfs_disk_key mid_key;
2029 btrfs_node_key(mid, &mid_key, 0);
2030 ret = tree_mod_log_insert_key(parent, pslot,
2031 MOD_LOG_KEY_REPLACE, GFP_NOFS);
2032 BUG_ON(ret < 0);
2033 btrfs_set_node_key(parent, &mid_key, pslot);
2034 btrfs_mark_buffer_dirty(parent);
2035 }
2036
2037 /* update the path */
2038 if (left) {
2039 if (btrfs_header_nritems(left) > orig_slot) {
2040 atomic_inc(&left->refs);
2041 /* left was locked after cow */
2042 path->nodes[level] = left;
2043 path->slots[level + 1] -= 1;
2044 path->slots[level] = orig_slot;
2045 if (mid) {
2046 btrfs_tree_unlock(mid);
2047 free_extent_buffer(mid);
2048 }
2049 } else {
2050 orig_slot -= btrfs_header_nritems(left);
2051 path->slots[level] = orig_slot;
2052 }
2053 }
2054 /* double check we haven't messed things up */
2055 if (orig_ptr !=
2056 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
2057 BUG();
2058 enospc:
2059 if (right) {
2060 btrfs_tree_unlock(right);
2061 free_extent_buffer(right);
2062 }
2063 if (left) {
2064 if (path->nodes[level] != left)
2065 btrfs_tree_unlock(left);
2066 free_extent_buffer(left);
2067 }
2068 return ret;
2069 }
2070
2071 /* Node balancing for insertion. Here we only split or push nodes around
2072 * when they are completely full. This is also done top down, so we
2073 * have to be pessimistic.
2074 */
push_nodes_for_insert(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,int level)2075 static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
2076 struct btrfs_root *root,
2077 struct btrfs_path *path, int level)
2078 {
2079 struct btrfs_fs_info *fs_info = root->fs_info;
2080 struct extent_buffer *right = NULL;
2081 struct extent_buffer *mid;
2082 struct extent_buffer *left = NULL;
2083 struct extent_buffer *parent = NULL;
2084 int ret = 0;
2085 int wret;
2086 int pslot;
2087 int orig_slot = path->slots[level];
2088
2089 if (level == 0)
2090 return 1;
2091
2092 mid = path->nodes[level];
2093 WARN_ON(btrfs_header_generation(mid) != trans->transid);
2094
2095 if (level < BTRFS_MAX_LEVEL - 1) {
2096 parent = path->nodes[level + 1];
2097 pslot = path->slots[level + 1];
2098 }
2099
2100 if (!parent)
2101 return 1;
2102
2103 left = btrfs_read_node_slot(parent, pslot - 1);
2104 if (IS_ERR(left))
2105 left = NULL;
2106
2107 /* first, try to make some room in the middle buffer */
2108 if (left) {
2109 u32 left_nr;
2110
2111 __btrfs_tree_lock(left, BTRFS_NESTING_LEFT);
2112 btrfs_set_lock_blocking_write(left);
2113
2114 left_nr = btrfs_header_nritems(left);
2115 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
2116 wret = 1;
2117 } else {
2118 ret = btrfs_cow_block(trans, root, left, parent,
2119 pslot - 1, &left,
2120 BTRFS_NESTING_LEFT_COW);
2121 if (ret)
2122 wret = 1;
2123 else {
2124 wret = push_node_left(trans, left, mid, 0);
2125 }
2126 }
2127 if (wret < 0)
2128 ret = wret;
2129 if (wret == 0) {
2130 struct btrfs_disk_key disk_key;
2131 orig_slot += left_nr;
2132 btrfs_node_key(mid, &disk_key, 0);
2133 ret = tree_mod_log_insert_key(parent, pslot,
2134 MOD_LOG_KEY_REPLACE, GFP_NOFS);
2135 BUG_ON(ret < 0);
2136 btrfs_set_node_key(parent, &disk_key, pslot);
2137 btrfs_mark_buffer_dirty(parent);
2138 if (btrfs_header_nritems(left) > orig_slot) {
2139 path->nodes[level] = left;
2140 path->slots[level + 1] -= 1;
2141 path->slots[level] = orig_slot;
2142 btrfs_tree_unlock(mid);
2143 free_extent_buffer(mid);
2144 } else {
2145 orig_slot -=
2146 btrfs_header_nritems(left);
2147 path->slots[level] = orig_slot;
2148 btrfs_tree_unlock(left);
2149 free_extent_buffer(left);
2150 }
2151 return 0;
2152 }
2153 btrfs_tree_unlock(left);
2154 free_extent_buffer(left);
2155 }
2156 right = btrfs_read_node_slot(parent, pslot + 1);
2157 if (IS_ERR(right))
2158 right = NULL;
2159
2160 /*
2161 * then try to empty the right most buffer into the middle
2162 */
2163 if (right) {
2164 u32 right_nr;
2165
2166 __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT);
2167 btrfs_set_lock_blocking_write(right);
2168
2169 right_nr = btrfs_header_nritems(right);
2170 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
2171 wret = 1;
2172 } else {
2173 ret = btrfs_cow_block(trans, root, right,
2174 parent, pslot + 1,
2175 &right, BTRFS_NESTING_RIGHT_COW);
2176 if (ret)
2177 wret = 1;
2178 else {
2179 wret = balance_node_right(trans, right, mid);
2180 }
2181 }
2182 if (wret < 0)
2183 ret = wret;
2184 if (wret == 0) {
2185 struct btrfs_disk_key disk_key;
2186
2187 btrfs_node_key(right, &disk_key, 0);
2188 ret = tree_mod_log_insert_key(parent, pslot + 1,
2189 MOD_LOG_KEY_REPLACE, GFP_NOFS);
2190 BUG_ON(ret < 0);
2191 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2192 btrfs_mark_buffer_dirty(parent);
2193
2194 if (btrfs_header_nritems(mid) <= orig_slot) {
2195 path->nodes[level] = right;
2196 path->slots[level + 1] += 1;
2197 path->slots[level] = orig_slot -
2198 btrfs_header_nritems(mid);
2199 btrfs_tree_unlock(mid);
2200 free_extent_buffer(mid);
2201 } else {
2202 btrfs_tree_unlock(right);
2203 free_extent_buffer(right);
2204 }
2205 return 0;
2206 }
2207 btrfs_tree_unlock(right);
2208 free_extent_buffer(right);
2209 }
2210 return 1;
2211 }
2212
2213 /*
2214 * readahead one full node of leaves, finding things that are close
2215 * to the block in 'slot', and triggering ra on them.
2216 */
reada_for_search(struct btrfs_fs_info * fs_info,struct btrfs_path * path,int level,int slot,u64 objectid)2217 static void reada_for_search(struct btrfs_fs_info *fs_info,
2218 struct btrfs_path *path,
2219 int level, int slot, u64 objectid)
2220 {
2221 struct extent_buffer *node;
2222 struct btrfs_disk_key disk_key;
2223 u32 nritems;
2224 u64 search;
2225 u64 target;
2226 u64 nread = 0;
2227 struct extent_buffer *eb;
2228 u32 nr;
2229 u32 blocksize;
2230 u32 nscan = 0;
2231
2232 if (level != 1)
2233 return;
2234
2235 if (!path->nodes[level])
2236 return;
2237
2238 node = path->nodes[level];
2239
2240 search = btrfs_node_blockptr(node, slot);
2241 blocksize = fs_info->nodesize;
2242 eb = find_extent_buffer(fs_info, search);
2243 if (eb) {
2244 free_extent_buffer(eb);
2245 return;
2246 }
2247
2248 target = search;
2249
2250 nritems = btrfs_header_nritems(node);
2251 nr = slot;
2252
2253 while (1) {
2254 if (path->reada == READA_BACK) {
2255 if (nr == 0)
2256 break;
2257 nr--;
2258 } else if (path->reada == READA_FORWARD) {
2259 nr++;
2260 if (nr >= nritems)
2261 break;
2262 }
2263 if (path->reada == READA_BACK && objectid) {
2264 btrfs_node_key(node, &disk_key, nr);
2265 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2266 break;
2267 }
2268 search = btrfs_node_blockptr(node, nr);
2269 if ((search <= target && target - search <= 65536) ||
2270 (search > target && search - target <= 65536)) {
2271 readahead_tree_block(fs_info, search);
2272 nread += blocksize;
2273 }
2274 nscan++;
2275 if ((nread > 65536 || nscan > 32))
2276 break;
2277 }
2278 }
2279
reada_for_balance(struct btrfs_fs_info * fs_info,struct btrfs_path * path,int level)2280 static noinline void reada_for_balance(struct btrfs_fs_info *fs_info,
2281 struct btrfs_path *path, int level)
2282 {
2283 int slot;
2284 int nritems;
2285 struct extent_buffer *parent;
2286 struct extent_buffer *eb;
2287 u64 gen;
2288 u64 block1 = 0;
2289 u64 block2 = 0;
2290
2291 parent = path->nodes[level + 1];
2292 if (!parent)
2293 return;
2294
2295 nritems = btrfs_header_nritems(parent);
2296 slot = path->slots[level + 1];
2297
2298 if (slot > 0) {
2299 block1 = btrfs_node_blockptr(parent, slot - 1);
2300 gen = btrfs_node_ptr_generation(parent, slot - 1);
2301 eb = find_extent_buffer(fs_info, block1);
2302 /*
2303 * if we get -eagain from btrfs_buffer_uptodate, we
2304 * don't want to return eagain here. That will loop
2305 * forever
2306 */
2307 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
2308 block1 = 0;
2309 free_extent_buffer(eb);
2310 }
2311 if (slot + 1 < nritems) {
2312 block2 = btrfs_node_blockptr(parent, slot + 1);
2313 gen = btrfs_node_ptr_generation(parent, slot + 1);
2314 eb = find_extent_buffer(fs_info, block2);
2315 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
2316 block2 = 0;
2317 free_extent_buffer(eb);
2318 }
2319
2320 if (block1)
2321 readahead_tree_block(fs_info, block1);
2322 if (block2)
2323 readahead_tree_block(fs_info, block2);
2324 }
2325
2326
2327 /*
2328 * when we walk down the tree, it is usually safe to unlock the higher layers
2329 * in the tree. The exceptions are when our path goes through slot 0, because
2330 * operations on the tree might require changing key pointers higher up in the
2331 * tree.
2332 *
2333 * callers might also have set path->keep_locks, which tells this code to keep
2334 * the lock if the path points to the last slot in the block. This is part of
2335 * walking through the tree, and selecting the next slot in the higher block.
2336 *
2337 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2338 * if lowest_unlock is 1, level 0 won't be unlocked
2339 */
unlock_up(struct btrfs_path * path,int level,int lowest_unlock,int min_write_lock_level,int * write_lock_level)2340 static noinline void unlock_up(struct btrfs_path *path, int level,
2341 int lowest_unlock, int min_write_lock_level,
2342 int *write_lock_level)
2343 {
2344 int i;
2345 int skip_level = level;
2346 int no_skips = 0;
2347 struct extent_buffer *t;
2348
2349 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2350 if (!path->nodes[i])
2351 break;
2352 if (!path->locks[i])
2353 break;
2354 if (!no_skips && path->slots[i] == 0) {
2355 skip_level = i + 1;
2356 continue;
2357 }
2358 if (!no_skips && path->keep_locks) {
2359 u32 nritems;
2360 t = path->nodes[i];
2361 nritems = btrfs_header_nritems(t);
2362 if (nritems < 1 || path->slots[i] >= nritems - 1) {
2363 skip_level = i + 1;
2364 continue;
2365 }
2366 }
2367 if (skip_level < i && i >= lowest_unlock)
2368 no_skips = 1;
2369
2370 t = path->nodes[i];
2371 if (i >= lowest_unlock && i > skip_level) {
2372 btrfs_tree_unlock_rw(t, path->locks[i]);
2373 path->locks[i] = 0;
2374 if (write_lock_level &&
2375 i > min_write_lock_level &&
2376 i <= *write_lock_level) {
2377 *write_lock_level = i - 1;
2378 }
2379 }
2380 }
2381 }
2382
2383 /*
2384 * helper function for btrfs_search_slot. The goal is to find a block
2385 * in cache without setting the path to blocking. If we find the block
2386 * we return zero and the path is unchanged.
2387 *
2388 * If we can't find the block, we set the path blocking and do some
2389 * reada. -EAGAIN is returned and the search must be repeated.
2390 */
2391 static int
read_block_for_search(struct btrfs_root * root,struct btrfs_path * p,struct extent_buffer ** eb_ret,int level,int slot,const struct btrfs_key * key)2392 read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
2393 struct extent_buffer **eb_ret, int level, int slot,
2394 const struct btrfs_key *key)
2395 {
2396 struct btrfs_fs_info *fs_info = root->fs_info;
2397 u64 blocknr;
2398 u64 gen;
2399 struct extent_buffer *tmp;
2400 struct btrfs_key first_key;
2401 int ret;
2402 int parent_level;
2403
2404 blocknr = btrfs_node_blockptr(*eb_ret, slot);
2405 gen = btrfs_node_ptr_generation(*eb_ret, slot);
2406 parent_level = btrfs_header_level(*eb_ret);
2407 btrfs_node_key_to_cpu(*eb_ret, &first_key, slot);
2408
2409 tmp = find_extent_buffer(fs_info, blocknr);
2410 if (tmp) {
2411 /* first we do an atomic uptodate check */
2412 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
2413 /*
2414 * Do extra check for first_key, eb can be stale due to
2415 * being cached, read from scrub, or have multiple
2416 * parents (shared tree blocks).
2417 */
2418 if (btrfs_verify_level_key(tmp,
2419 parent_level - 1, &first_key, gen)) {
2420 free_extent_buffer(tmp);
2421 return -EUCLEAN;
2422 }
2423 *eb_ret = tmp;
2424 return 0;
2425 }
2426
2427 /* the pages were up to date, but we failed
2428 * the generation number check. Do a full
2429 * read for the generation number that is correct.
2430 * We must do this without dropping locks so
2431 * we can trust our generation number
2432 */
2433 btrfs_set_path_blocking(p);
2434
2435 /* now we're allowed to do a blocking uptodate check */
2436 ret = btrfs_read_buffer(tmp, gen, parent_level - 1, &first_key);
2437 if (!ret) {
2438 *eb_ret = tmp;
2439 return 0;
2440 }
2441 free_extent_buffer(tmp);
2442 btrfs_release_path(p);
2443 return -EIO;
2444 }
2445
2446 /*
2447 * reduce lock contention at high levels
2448 * of the btree by dropping locks before
2449 * we read. Don't release the lock on the current
2450 * level because we need to walk this node to figure
2451 * out which blocks to read.
2452 */
2453 btrfs_unlock_up_safe(p, level + 1);
2454 btrfs_set_path_blocking(p);
2455
2456 if (p->reada != READA_NONE)
2457 reada_for_search(fs_info, p, level, slot, key->objectid);
2458
2459 ret = -EAGAIN;
2460 tmp = read_tree_block(fs_info, blocknr, gen, parent_level - 1,
2461 &first_key);
2462 if (!IS_ERR(tmp)) {
2463 /*
2464 * If the read above didn't mark this buffer up to date,
2465 * it will never end up being up to date. Set ret to EIO now
2466 * and give up so that our caller doesn't loop forever
2467 * on our EAGAINs.
2468 */
2469 if (!extent_buffer_uptodate(tmp))
2470 ret = -EIO;
2471 free_extent_buffer(tmp);
2472 } else {
2473 ret = PTR_ERR(tmp);
2474 }
2475
2476 btrfs_release_path(p);
2477 return ret;
2478 }
2479
2480 /*
2481 * helper function for btrfs_search_slot. This does all of the checks
2482 * for node-level blocks and does any balancing required based on
2483 * the ins_len.
2484 *
2485 * If no extra work was required, zero is returned. If we had to
2486 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2487 * start over
2488 */
2489 static int
setup_nodes_for_search(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * p,struct extent_buffer * b,int level,int ins_len,int * write_lock_level)2490 setup_nodes_for_search(struct btrfs_trans_handle *trans,
2491 struct btrfs_root *root, struct btrfs_path *p,
2492 struct extent_buffer *b, int level, int ins_len,
2493 int *write_lock_level)
2494 {
2495 struct btrfs_fs_info *fs_info = root->fs_info;
2496 int ret;
2497
2498 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2499 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) {
2500 int sret;
2501
2502 if (*write_lock_level < level + 1) {
2503 *write_lock_level = level + 1;
2504 btrfs_release_path(p);
2505 goto again;
2506 }
2507
2508 btrfs_set_path_blocking(p);
2509 reada_for_balance(fs_info, p, level);
2510 sret = split_node(trans, root, p, level);
2511
2512 BUG_ON(sret > 0);
2513 if (sret) {
2514 ret = sret;
2515 goto done;
2516 }
2517 b = p->nodes[level];
2518 } else if (ins_len < 0 && btrfs_header_nritems(b) <
2519 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) {
2520 int sret;
2521
2522 if (*write_lock_level < level + 1) {
2523 *write_lock_level = level + 1;
2524 btrfs_release_path(p);
2525 goto again;
2526 }
2527
2528 btrfs_set_path_blocking(p);
2529 reada_for_balance(fs_info, p, level);
2530 sret = balance_level(trans, root, p, level);
2531
2532 if (sret) {
2533 ret = sret;
2534 goto done;
2535 }
2536 b = p->nodes[level];
2537 if (!b) {
2538 btrfs_release_path(p);
2539 goto again;
2540 }
2541 BUG_ON(btrfs_header_nritems(b) == 1);
2542 }
2543 return 0;
2544
2545 again:
2546 ret = -EAGAIN;
2547 done:
2548 return ret;
2549 }
2550
btrfs_find_item(struct btrfs_root * fs_root,struct btrfs_path * path,u64 iobjectid,u64 ioff,u8 key_type,struct btrfs_key * found_key)2551 int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
2552 u64 iobjectid, u64 ioff, u8 key_type,
2553 struct btrfs_key *found_key)
2554 {
2555 int ret;
2556 struct btrfs_key key;
2557 struct extent_buffer *eb;
2558
2559 ASSERT(path);
2560 ASSERT(found_key);
2561
2562 key.type = key_type;
2563 key.objectid = iobjectid;
2564 key.offset = ioff;
2565
2566 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
2567 if (ret < 0)
2568 return ret;
2569
2570 eb = path->nodes[0];
2571 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
2572 ret = btrfs_next_leaf(fs_root, path);
2573 if (ret)
2574 return ret;
2575 eb = path->nodes[0];
2576 }
2577
2578 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
2579 if (found_key->type != key.type ||
2580 found_key->objectid != key.objectid)
2581 return 1;
2582
2583 return 0;
2584 }
2585
btrfs_search_slot_get_root(struct btrfs_root * root,struct btrfs_path * p,int write_lock_level)2586 static struct extent_buffer *btrfs_search_slot_get_root(struct btrfs_root *root,
2587 struct btrfs_path *p,
2588 int write_lock_level)
2589 {
2590 struct btrfs_fs_info *fs_info = root->fs_info;
2591 struct extent_buffer *b;
2592 int root_lock = 0;
2593 int level = 0;
2594
2595 if (p->search_commit_root) {
2596 /*
2597 * The commit roots are read only so we always do read locks,
2598 * and we always must hold the commit_root_sem when doing
2599 * searches on them, the only exception is send where we don't
2600 * want to block transaction commits for a long time, so
2601 * we need to clone the commit root in order to avoid races
2602 * with transaction commits that create a snapshot of one of
2603 * the roots used by a send operation.
2604 */
2605 if (p->need_commit_sem) {
2606 down_read(&fs_info->commit_root_sem);
2607 b = btrfs_clone_extent_buffer(root->commit_root);
2608 up_read(&fs_info->commit_root_sem);
2609 if (!b)
2610 return ERR_PTR(-ENOMEM);
2611
2612 } else {
2613 b = root->commit_root;
2614 atomic_inc(&b->refs);
2615 }
2616 level = btrfs_header_level(b);
2617 /*
2618 * Ensure that all callers have set skip_locking when
2619 * p->search_commit_root = 1.
2620 */
2621 ASSERT(p->skip_locking == 1);
2622
2623 goto out;
2624 }
2625
2626 if (p->skip_locking) {
2627 b = btrfs_root_node(root);
2628 level = btrfs_header_level(b);
2629 goto out;
2630 }
2631
2632 /* We try very hard to do read locks on the root */
2633 root_lock = BTRFS_READ_LOCK;
2634
2635 /*
2636 * If the level is set to maximum, we can skip trying to get the read
2637 * lock.
2638 */
2639 if (write_lock_level < BTRFS_MAX_LEVEL) {
2640 /*
2641 * We don't know the level of the root node until we actually
2642 * have it read locked
2643 */
2644 b = __btrfs_read_lock_root_node(root, p->recurse);
2645 level = btrfs_header_level(b);
2646 if (level > write_lock_level)
2647 goto out;
2648
2649 /* Whoops, must trade for write lock */
2650 btrfs_tree_read_unlock(b);
2651 free_extent_buffer(b);
2652 }
2653
2654 b = btrfs_lock_root_node(root);
2655 root_lock = BTRFS_WRITE_LOCK;
2656
2657 /* The level might have changed, check again */
2658 level = btrfs_header_level(b);
2659
2660 out:
2661 /*
2662 * The root may have failed to write out at some point, and thus is no
2663 * longer valid, return an error in this case.
2664 */
2665 if (!extent_buffer_uptodate(b)) {
2666 if (root_lock)
2667 btrfs_tree_unlock_rw(b, root_lock);
2668 free_extent_buffer(b);
2669 return ERR_PTR(-EIO);
2670 }
2671
2672 p->nodes[level] = b;
2673 if (!p->skip_locking)
2674 p->locks[level] = root_lock;
2675 /*
2676 * Callers are responsible for dropping b's references.
2677 */
2678 return b;
2679 }
2680
2681
2682 /*
2683 * btrfs_search_slot - look for a key in a tree and perform necessary
2684 * modifications to preserve tree invariants.
2685 *
2686 * @trans: Handle of transaction, used when modifying the tree
2687 * @p: Holds all btree nodes along the search path
2688 * @root: The root node of the tree
2689 * @key: The key we are looking for
2690 * @ins_len: Indicates purpose of search, for inserts it is 1, for
2691 * deletions it's -1. 0 for plain searches
2692 * @cow: boolean should CoW operations be performed. Must always be 1
2693 * when modifying the tree.
2694 *
2695 * If @ins_len > 0, nodes and leaves will be split as we walk down the tree.
2696 * If @ins_len < 0, nodes will be merged as we walk down the tree (if possible)
2697 *
2698 * If @key is found, 0 is returned and you can find the item in the leaf level
2699 * of the path (level 0)
2700 *
2701 * If @key isn't found, 1 is returned and the leaf level of the path (level 0)
2702 * points to the slot where it should be inserted
2703 *
2704 * If an error is encountered while searching the tree a negative error number
2705 * is returned
2706 */
btrfs_search_slot(struct btrfs_trans_handle * trans,struct btrfs_root * root,const struct btrfs_key * key,struct btrfs_path * p,int ins_len,int cow)2707 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2708 const struct btrfs_key *key, struct btrfs_path *p,
2709 int ins_len, int cow)
2710 {
2711 struct extent_buffer *b;
2712 int slot;
2713 int ret;
2714 int err;
2715 int level;
2716 int lowest_unlock = 1;
2717 /* everything at write_lock_level or lower must be write locked */
2718 int write_lock_level = 0;
2719 u8 lowest_level = 0;
2720 int min_write_lock_level;
2721 int prev_cmp;
2722
2723 lowest_level = p->lowest_level;
2724 WARN_ON(lowest_level && ins_len > 0);
2725 WARN_ON(p->nodes[0] != NULL);
2726 BUG_ON(!cow && ins_len);
2727
2728 if (ins_len < 0) {
2729 lowest_unlock = 2;
2730
2731 /* when we are removing items, we might have to go up to level
2732 * two as we update tree pointers Make sure we keep write
2733 * for those levels as well
2734 */
2735 write_lock_level = 2;
2736 } else if (ins_len > 0) {
2737 /*
2738 * for inserting items, make sure we have a write lock on
2739 * level 1 so we can update keys
2740 */
2741 write_lock_level = 1;
2742 }
2743
2744 if (!cow)
2745 write_lock_level = -1;
2746
2747 if (cow && (p->keep_locks || p->lowest_level))
2748 write_lock_level = BTRFS_MAX_LEVEL;
2749
2750 min_write_lock_level = write_lock_level;
2751
2752 again:
2753 prev_cmp = -1;
2754 b = btrfs_search_slot_get_root(root, p, write_lock_level);
2755 if (IS_ERR(b)) {
2756 ret = PTR_ERR(b);
2757 goto done;
2758 }
2759
2760 while (b) {
2761 int dec = 0;
2762
2763 level = btrfs_header_level(b);
2764
2765 if (cow) {
2766 bool last_level = (level == (BTRFS_MAX_LEVEL - 1));
2767
2768 /*
2769 * if we don't really need to cow this block
2770 * then we don't want to set the path blocking,
2771 * so we test it here
2772 */
2773 if (!should_cow_block(trans, root, b)) {
2774 trans->dirty = true;
2775 goto cow_done;
2776 }
2777
2778 /*
2779 * must have write locks on this node and the
2780 * parent
2781 */
2782 if (level > write_lock_level ||
2783 (level + 1 > write_lock_level &&
2784 level + 1 < BTRFS_MAX_LEVEL &&
2785 p->nodes[level + 1])) {
2786 write_lock_level = level + 1;
2787 btrfs_release_path(p);
2788 goto again;
2789 }
2790
2791 btrfs_set_path_blocking(p);
2792 if (last_level)
2793 err = btrfs_cow_block(trans, root, b, NULL, 0,
2794 &b,
2795 BTRFS_NESTING_COW);
2796 else
2797 err = btrfs_cow_block(trans, root, b,
2798 p->nodes[level + 1],
2799 p->slots[level + 1], &b,
2800 BTRFS_NESTING_COW);
2801 if (err) {
2802 ret = err;
2803 goto done;
2804 }
2805 }
2806 cow_done:
2807 p->nodes[level] = b;
2808 /*
2809 * Leave path with blocking locks to avoid massive
2810 * lock context switch, this is made on purpose.
2811 */
2812
2813 /*
2814 * we have a lock on b and as long as we aren't changing
2815 * the tree, there is no way to for the items in b to change.
2816 * It is safe to drop the lock on our parent before we
2817 * go through the expensive btree search on b.
2818 *
2819 * If we're inserting or deleting (ins_len != 0), then we might
2820 * be changing slot zero, which may require changing the parent.
2821 * So, we can't drop the lock until after we know which slot
2822 * we're operating on.
2823 */
2824 if (!ins_len && !p->keep_locks) {
2825 int u = level + 1;
2826
2827 if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
2828 btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
2829 p->locks[u] = 0;
2830 }
2831 }
2832
2833 /*
2834 * If btrfs_bin_search returns an exact match (prev_cmp == 0)
2835 * we can safely assume the target key will always be in slot 0
2836 * on lower levels due to the invariants BTRFS' btree provides,
2837 * namely that a btrfs_key_ptr entry always points to the
2838 * lowest key in the child node, thus we can skip searching
2839 * lower levels
2840 */
2841 if (prev_cmp == 0) {
2842 slot = 0;
2843 ret = 0;
2844 } else {
2845 ret = btrfs_bin_search(b, key, &slot);
2846 prev_cmp = ret;
2847 if (ret < 0)
2848 goto done;
2849 }
2850
2851 if (level == 0) {
2852 p->slots[level] = slot;
2853 if (ins_len > 0 &&
2854 btrfs_leaf_free_space(b) < ins_len) {
2855 if (write_lock_level < 1) {
2856 write_lock_level = 1;
2857 btrfs_release_path(p);
2858 goto again;
2859 }
2860
2861 btrfs_set_path_blocking(p);
2862 err = split_leaf(trans, root, key,
2863 p, ins_len, ret == 0);
2864
2865 BUG_ON(err > 0);
2866 if (err) {
2867 ret = err;
2868 goto done;
2869 }
2870 }
2871 if (!p->search_for_split)
2872 unlock_up(p, level, lowest_unlock,
2873 min_write_lock_level, NULL);
2874 goto done;
2875 }
2876 if (ret && slot > 0) {
2877 dec = 1;
2878 slot--;
2879 }
2880 p->slots[level] = slot;
2881 err = setup_nodes_for_search(trans, root, p, b, level, ins_len,
2882 &write_lock_level);
2883 if (err == -EAGAIN)
2884 goto again;
2885 if (err) {
2886 ret = err;
2887 goto done;
2888 }
2889 b = p->nodes[level];
2890 slot = p->slots[level];
2891
2892 /*
2893 * Slot 0 is special, if we change the key we have to update
2894 * the parent pointer which means we must have a write lock on
2895 * the parent
2896 */
2897 if (slot == 0 && ins_len && write_lock_level < level + 1) {
2898 write_lock_level = level + 1;
2899 btrfs_release_path(p);
2900 goto again;
2901 }
2902
2903 unlock_up(p, level, lowest_unlock, min_write_lock_level,
2904 &write_lock_level);
2905
2906 if (level == lowest_level) {
2907 if (dec)
2908 p->slots[level]++;
2909 goto done;
2910 }
2911
2912 err = read_block_for_search(root, p, &b, level, slot, key);
2913 if (err == -EAGAIN)
2914 goto again;
2915 if (err) {
2916 ret = err;
2917 goto done;
2918 }
2919
2920 if (!p->skip_locking) {
2921 level = btrfs_header_level(b);
2922 if (level <= write_lock_level) {
2923 if (!btrfs_try_tree_write_lock(b)) {
2924 btrfs_set_path_blocking(p);
2925 btrfs_tree_lock(b);
2926 }
2927 p->locks[level] = BTRFS_WRITE_LOCK;
2928 } else {
2929 if (!btrfs_tree_read_lock_atomic(b)) {
2930 btrfs_set_path_blocking(p);
2931 __btrfs_tree_read_lock(b, BTRFS_NESTING_NORMAL,
2932 p->recurse);
2933 }
2934 p->locks[level] = BTRFS_READ_LOCK;
2935 }
2936 p->nodes[level] = b;
2937 }
2938 }
2939 ret = 1;
2940 done:
2941 /*
2942 * we don't really know what they plan on doing with the path
2943 * from here on, so for now just mark it as blocking
2944 */
2945 if (!p->leave_spinning)
2946 btrfs_set_path_blocking(p);
2947 if (ret < 0 && !p->skip_release_on_error)
2948 btrfs_release_path(p);
2949 return ret;
2950 }
2951
2952 /*
2953 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2954 * current state of the tree together with the operations recorded in the tree
2955 * modification log to search for the key in a previous version of this tree, as
2956 * denoted by the time_seq parameter.
2957 *
2958 * Naturally, there is no support for insert, delete or cow operations.
2959 *
2960 * The resulting path and return value will be set up as if we called
2961 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2962 */
btrfs_search_old_slot(struct btrfs_root * root,const struct btrfs_key * key,struct btrfs_path * p,u64 time_seq)2963 int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
2964 struct btrfs_path *p, u64 time_seq)
2965 {
2966 struct btrfs_fs_info *fs_info = root->fs_info;
2967 struct extent_buffer *b;
2968 int slot;
2969 int ret;
2970 int err;
2971 int level;
2972 int lowest_unlock = 1;
2973 u8 lowest_level = 0;
2974
2975 lowest_level = p->lowest_level;
2976 WARN_ON(p->nodes[0] != NULL);
2977
2978 if (p->search_commit_root) {
2979 BUG_ON(time_seq);
2980 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2981 }
2982
2983 again:
2984 b = get_old_root(root, time_seq);
2985 if (!b) {
2986 ret = -EIO;
2987 goto done;
2988 }
2989 level = btrfs_header_level(b);
2990 p->locks[level] = BTRFS_READ_LOCK;
2991
2992 while (b) {
2993 int dec = 0;
2994
2995 level = btrfs_header_level(b);
2996 p->nodes[level] = b;
2997
2998 /*
2999 * we have a lock on b and as long as we aren't changing
3000 * the tree, there is no way to for the items in b to change.
3001 * It is safe to drop the lock on our parent before we
3002 * go through the expensive btree search on b.
3003 */
3004 btrfs_unlock_up_safe(p, level + 1);
3005
3006 ret = btrfs_bin_search(b, key, &slot);
3007 if (ret < 0)
3008 goto done;
3009
3010 if (level == 0) {
3011 p->slots[level] = slot;
3012 unlock_up(p, level, lowest_unlock, 0, NULL);
3013 goto done;
3014 }
3015
3016 if (ret && slot > 0) {
3017 dec = 1;
3018 slot--;
3019 }
3020 p->slots[level] = slot;
3021 unlock_up(p, level, lowest_unlock, 0, NULL);
3022
3023 if (level == lowest_level) {
3024 if (dec)
3025 p->slots[level]++;
3026 goto done;
3027 }
3028
3029 err = read_block_for_search(root, p, &b, level, slot, key);
3030 if (err == -EAGAIN)
3031 goto again;
3032 if (err) {
3033 ret = err;
3034 goto done;
3035 }
3036
3037 level = btrfs_header_level(b);
3038 if (!btrfs_tree_read_lock_atomic(b)) {
3039 btrfs_set_path_blocking(p);
3040 btrfs_tree_read_lock(b);
3041 }
3042 b = tree_mod_log_rewind(fs_info, p, b, time_seq);
3043 if (!b) {
3044 ret = -ENOMEM;
3045 goto done;
3046 }
3047 p->locks[level] = BTRFS_READ_LOCK;
3048 p->nodes[level] = b;
3049 }
3050 ret = 1;
3051 done:
3052 if (!p->leave_spinning)
3053 btrfs_set_path_blocking(p);
3054 if (ret < 0)
3055 btrfs_release_path(p);
3056
3057 return ret;
3058 }
3059
3060 /*
3061 * helper to use instead of search slot if no exact match is needed but
3062 * instead the next or previous item should be returned.
3063 * When find_higher is true, the next higher item is returned, the next lower
3064 * otherwise.
3065 * When return_any and find_higher are both true, and no higher item is found,
3066 * return the next lower instead.
3067 * When return_any is true and find_higher is false, and no lower item is found,
3068 * return the next higher instead.
3069 * It returns 0 if any item is found, 1 if none is found (tree empty), and
3070 * < 0 on error
3071 */
btrfs_search_slot_for_read(struct btrfs_root * root,const struct btrfs_key * key,struct btrfs_path * p,int find_higher,int return_any)3072 int btrfs_search_slot_for_read(struct btrfs_root *root,
3073 const struct btrfs_key *key,
3074 struct btrfs_path *p, int find_higher,
3075 int return_any)
3076 {
3077 int ret;
3078 struct extent_buffer *leaf;
3079
3080 again:
3081 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
3082 if (ret <= 0)
3083 return ret;
3084 /*
3085 * a return value of 1 means the path is at the position where the
3086 * item should be inserted. Normally this is the next bigger item,
3087 * but in case the previous item is the last in a leaf, path points
3088 * to the first free slot in the previous leaf, i.e. at an invalid
3089 * item.
3090 */
3091 leaf = p->nodes[0];
3092
3093 if (find_higher) {
3094 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
3095 ret = btrfs_next_leaf(root, p);
3096 if (ret <= 0)
3097 return ret;
3098 if (!return_any)
3099 return 1;
3100 /*
3101 * no higher item found, return the next
3102 * lower instead
3103 */
3104 return_any = 0;
3105 find_higher = 0;
3106 btrfs_release_path(p);
3107 goto again;
3108 }
3109 } else {
3110 if (p->slots[0] == 0) {
3111 ret = btrfs_prev_leaf(root, p);
3112 if (ret < 0)
3113 return ret;
3114 if (!ret) {
3115 leaf = p->nodes[0];
3116 if (p->slots[0] == btrfs_header_nritems(leaf))
3117 p->slots[0]--;
3118 return 0;
3119 }
3120 if (!return_any)
3121 return 1;
3122 /*
3123 * no lower item found, return the next
3124 * higher instead
3125 */
3126 return_any = 0;
3127 find_higher = 1;
3128 btrfs_release_path(p);
3129 goto again;
3130 } else {
3131 --p->slots[0];
3132 }
3133 }
3134 return 0;
3135 }
3136
3137 /*
3138 * adjust the pointers going up the tree, starting at level
3139 * making sure the right key of each node is points to 'key'.
3140 * This is used after shifting pointers to the left, so it stops
3141 * fixing up pointers when a given leaf/node is not in slot 0 of the
3142 * higher levels
3143 *
3144 */
fixup_low_keys(struct btrfs_path * path,struct btrfs_disk_key * key,int level)3145 static void fixup_low_keys(struct btrfs_path *path,
3146 struct btrfs_disk_key *key, int level)
3147 {
3148 int i;
3149 struct extent_buffer *t;
3150 int ret;
3151
3152 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
3153 int tslot = path->slots[i];
3154
3155 if (!path->nodes[i])
3156 break;
3157 t = path->nodes[i];
3158 ret = tree_mod_log_insert_key(t, tslot, MOD_LOG_KEY_REPLACE,
3159 GFP_ATOMIC);
3160 BUG_ON(ret < 0);
3161 btrfs_set_node_key(t, key, tslot);
3162 btrfs_mark_buffer_dirty(path->nodes[i]);
3163 if (tslot != 0)
3164 break;
3165 }
3166 }
3167
3168 /*
3169 * update item key.
3170 *
3171 * This function isn't completely safe. It's the caller's responsibility
3172 * that the new key won't break the order
3173 */
btrfs_set_item_key_safe(struct btrfs_fs_info * fs_info,struct btrfs_path * path,const struct btrfs_key * new_key)3174 void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
3175 struct btrfs_path *path,
3176 const struct btrfs_key *new_key)
3177 {
3178 struct btrfs_disk_key disk_key;
3179 struct extent_buffer *eb;
3180 int slot;
3181
3182 eb = path->nodes[0];
3183 slot = path->slots[0];
3184 if (slot > 0) {
3185 btrfs_item_key(eb, &disk_key, slot - 1);
3186 if (unlikely(comp_keys(&disk_key, new_key) >= 0)) {
3187 btrfs_crit(fs_info,
3188 "slot %u key (%llu %u %llu) new key (%llu %u %llu)",
3189 slot, btrfs_disk_key_objectid(&disk_key),
3190 btrfs_disk_key_type(&disk_key),
3191 btrfs_disk_key_offset(&disk_key),
3192 new_key->objectid, new_key->type,
3193 new_key->offset);
3194 btrfs_print_leaf(eb);
3195 BUG();
3196 }
3197 }
3198 if (slot < btrfs_header_nritems(eb) - 1) {
3199 btrfs_item_key(eb, &disk_key, slot + 1);
3200 if (unlikely(comp_keys(&disk_key, new_key) <= 0)) {
3201 btrfs_crit(fs_info,
3202 "slot %u key (%llu %u %llu) new key (%llu %u %llu)",
3203 slot, btrfs_disk_key_objectid(&disk_key),
3204 btrfs_disk_key_type(&disk_key),
3205 btrfs_disk_key_offset(&disk_key),
3206 new_key->objectid, new_key->type,
3207 new_key->offset);
3208 btrfs_print_leaf(eb);
3209 BUG();
3210 }
3211 }
3212
3213 btrfs_cpu_key_to_disk(&disk_key, new_key);
3214 btrfs_set_item_key(eb, &disk_key, slot);
3215 btrfs_mark_buffer_dirty(eb);
3216 if (slot == 0)
3217 fixup_low_keys(path, &disk_key, 1);
3218 }
3219
3220 /*
3221 * Check key order of two sibling extent buffers.
3222 *
3223 * Return true if something is wrong.
3224 * Return false if everything is fine.
3225 *
3226 * Tree-checker only works inside one tree block, thus the following
3227 * corruption can not be detected by tree-checker:
3228 *
3229 * Leaf @left | Leaf @right
3230 * --------------------------------------------------------------
3231 * | 1 | 2 | 3 | 4 | 5 | f6 | | 7 | 8 |
3232 *
3233 * Key f6 in leaf @left itself is valid, but not valid when the next
3234 * key in leaf @right is 7.
3235 * This can only be checked at tree block merge time.
3236 * And since tree checker has ensured all key order in each tree block
3237 * is correct, we only need to bother the last key of @left and the first
3238 * key of @right.
3239 */
check_sibling_keys(struct extent_buffer * left,struct extent_buffer * right)3240 static bool check_sibling_keys(struct extent_buffer *left,
3241 struct extent_buffer *right)
3242 {
3243 struct btrfs_key left_last;
3244 struct btrfs_key right_first;
3245 int level = btrfs_header_level(left);
3246 int nr_left = btrfs_header_nritems(left);
3247 int nr_right = btrfs_header_nritems(right);
3248
3249 /* No key to check in one of the tree blocks */
3250 if (!nr_left || !nr_right)
3251 return false;
3252
3253 if (level) {
3254 btrfs_node_key_to_cpu(left, &left_last, nr_left - 1);
3255 btrfs_node_key_to_cpu(right, &right_first, 0);
3256 } else {
3257 btrfs_item_key_to_cpu(left, &left_last, nr_left - 1);
3258 btrfs_item_key_to_cpu(right, &right_first, 0);
3259 }
3260
3261 if (btrfs_comp_cpu_keys(&left_last, &right_first) >= 0) {
3262 btrfs_crit(left->fs_info,
3263 "bad key order, sibling blocks, left last (%llu %u %llu) right first (%llu %u %llu)",
3264 left_last.objectid, left_last.type,
3265 left_last.offset, right_first.objectid,
3266 right_first.type, right_first.offset);
3267 return true;
3268 }
3269 return false;
3270 }
3271
3272 /*
3273 * try to push data from one node into the next node left in the
3274 * tree.
3275 *
3276 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
3277 * error, and > 0 if there was no room in the left hand block.
3278 */
push_node_left(struct btrfs_trans_handle * trans,struct extent_buffer * dst,struct extent_buffer * src,int empty)3279 static int push_node_left(struct btrfs_trans_handle *trans,
3280 struct extent_buffer *dst,
3281 struct extent_buffer *src, int empty)
3282 {
3283 struct btrfs_fs_info *fs_info = trans->fs_info;
3284 int push_items = 0;
3285 int src_nritems;
3286 int dst_nritems;
3287 int ret = 0;
3288
3289 src_nritems = btrfs_header_nritems(src);
3290 dst_nritems = btrfs_header_nritems(dst);
3291 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
3292 WARN_ON(btrfs_header_generation(src) != trans->transid);
3293 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3294
3295 if (!empty && src_nritems <= 8)
3296 return 1;
3297
3298 if (push_items <= 0)
3299 return 1;
3300
3301 if (empty) {
3302 push_items = min(src_nritems, push_items);
3303 if (push_items < src_nritems) {
3304 /* leave at least 8 pointers in the node if
3305 * we aren't going to empty it
3306 */
3307 if (src_nritems - push_items < 8) {
3308 if (push_items <= 8)
3309 return 1;
3310 push_items -= 8;
3311 }
3312 }
3313 } else
3314 push_items = min(src_nritems - 8, push_items);
3315
3316 /* dst is the left eb, src is the middle eb */
3317 if (check_sibling_keys(dst, src)) {
3318 ret = -EUCLEAN;
3319 btrfs_abort_transaction(trans, ret);
3320 return ret;
3321 }
3322 ret = tree_mod_log_eb_copy(dst, src, dst_nritems, 0, push_items);
3323 if (ret) {
3324 btrfs_abort_transaction(trans, ret);
3325 return ret;
3326 }
3327 copy_extent_buffer(dst, src,
3328 btrfs_node_key_ptr_offset(dst_nritems),
3329 btrfs_node_key_ptr_offset(0),
3330 push_items * sizeof(struct btrfs_key_ptr));
3331
3332 if (push_items < src_nritems) {
3333 /*
3334 * Don't call tree_mod_log_insert_move here, key removal was
3335 * already fully logged by tree_mod_log_eb_copy above.
3336 */
3337 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
3338 btrfs_node_key_ptr_offset(push_items),
3339 (src_nritems - push_items) *
3340 sizeof(struct btrfs_key_ptr));
3341 }
3342 btrfs_set_header_nritems(src, src_nritems - push_items);
3343 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3344 btrfs_mark_buffer_dirty(src);
3345 btrfs_mark_buffer_dirty(dst);
3346
3347 return ret;
3348 }
3349
3350 /*
3351 * try to push data from one node into the next node right in the
3352 * tree.
3353 *
3354 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3355 * error, and > 0 if there was no room in the right hand block.
3356 *
3357 * this will only push up to 1/2 the contents of the left node over
3358 */
balance_node_right(struct btrfs_trans_handle * trans,struct extent_buffer * dst,struct extent_buffer * src)3359 static int balance_node_right(struct btrfs_trans_handle *trans,
3360 struct extent_buffer *dst,
3361 struct extent_buffer *src)
3362 {
3363 struct btrfs_fs_info *fs_info = trans->fs_info;
3364 int push_items = 0;
3365 int max_push;
3366 int src_nritems;
3367 int dst_nritems;
3368 int ret = 0;
3369
3370 WARN_ON(btrfs_header_generation(src) != trans->transid);
3371 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3372
3373 src_nritems = btrfs_header_nritems(src);
3374 dst_nritems = btrfs_header_nritems(dst);
3375 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
3376 if (push_items <= 0)
3377 return 1;
3378
3379 if (src_nritems < 4)
3380 return 1;
3381
3382 max_push = src_nritems / 2 + 1;
3383 /* don't try to empty the node */
3384 if (max_push >= src_nritems)
3385 return 1;
3386
3387 if (max_push < push_items)
3388 push_items = max_push;
3389
3390 /* dst is the right eb, src is the middle eb */
3391 if (check_sibling_keys(src, dst)) {
3392 ret = -EUCLEAN;
3393 btrfs_abort_transaction(trans, ret);
3394 return ret;
3395 }
3396 ret = tree_mod_log_insert_move(dst, push_items, 0, dst_nritems);
3397 BUG_ON(ret < 0);
3398 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3399 btrfs_node_key_ptr_offset(0),
3400 (dst_nritems) *
3401 sizeof(struct btrfs_key_ptr));
3402
3403 ret = tree_mod_log_eb_copy(dst, src, 0, src_nritems - push_items,
3404 push_items);
3405 if (ret) {
3406 btrfs_abort_transaction(trans, ret);
3407 return ret;
3408 }
3409 copy_extent_buffer(dst, src,
3410 btrfs_node_key_ptr_offset(0),
3411 btrfs_node_key_ptr_offset(src_nritems - push_items),
3412 push_items * sizeof(struct btrfs_key_ptr));
3413
3414 btrfs_set_header_nritems(src, src_nritems - push_items);
3415 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3416
3417 btrfs_mark_buffer_dirty(src);
3418 btrfs_mark_buffer_dirty(dst);
3419
3420 return ret;
3421 }
3422
3423 /*
3424 * helper function to insert a new root level in the tree.
3425 * A new node is allocated, and a single item is inserted to
3426 * point to the existing root
3427 *
3428 * returns zero on success or < 0 on failure.
3429 */
insert_new_root(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,int level)3430 static noinline int insert_new_root(struct btrfs_trans_handle *trans,
3431 struct btrfs_root *root,
3432 struct btrfs_path *path, int level)
3433 {
3434 struct btrfs_fs_info *fs_info = root->fs_info;
3435 u64 lower_gen;
3436 struct extent_buffer *lower;
3437 struct extent_buffer *c;
3438 struct extent_buffer *old;
3439 struct btrfs_disk_key lower_key;
3440 int ret;
3441
3442 BUG_ON(path->nodes[level]);
3443 BUG_ON(path->nodes[level-1] != root->node);
3444
3445 lower = path->nodes[level-1];
3446 if (level == 1)
3447 btrfs_item_key(lower, &lower_key, 0);
3448 else
3449 btrfs_node_key(lower, &lower_key, 0);
3450
3451 c = alloc_tree_block_no_bg_flush(trans, root, 0, &lower_key, level,
3452 root->node->start, 0,
3453 BTRFS_NESTING_NEW_ROOT);
3454 if (IS_ERR(c))
3455 return PTR_ERR(c);
3456
3457 root_add_used(root, fs_info->nodesize);
3458
3459 btrfs_set_header_nritems(c, 1);
3460 btrfs_set_node_key(c, &lower_key, 0);
3461 btrfs_set_node_blockptr(c, 0, lower->start);
3462 lower_gen = btrfs_header_generation(lower);
3463 WARN_ON(lower_gen != trans->transid);
3464
3465 btrfs_set_node_ptr_generation(c, 0, lower_gen);
3466
3467 btrfs_mark_buffer_dirty(c);
3468
3469 old = root->node;
3470 ret = tree_mod_log_insert_root(root->node, c, 0);
3471 BUG_ON(ret < 0);
3472 rcu_assign_pointer(root->node, c);
3473
3474 /* the super has an extra ref to root->node */
3475 free_extent_buffer(old);
3476
3477 add_root_to_dirty_list(root);
3478 atomic_inc(&c->refs);
3479 path->nodes[level] = c;
3480 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
3481 path->slots[level] = 0;
3482 return 0;
3483 }
3484
3485 /*
3486 * worker function to insert a single pointer in a node.
3487 * the node should have enough room for the pointer already
3488 *
3489 * slot and level indicate where you want the key to go, and
3490 * blocknr is the block the key points to.
3491 */
insert_ptr(struct btrfs_trans_handle * trans,struct btrfs_path * path,struct btrfs_disk_key * key,u64 bytenr,int slot,int level)3492 static void insert_ptr(struct btrfs_trans_handle *trans,
3493 struct btrfs_path *path,
3494 struct btrfs_disk_key *key, u64 bytenr,
3495 int slot, int level)
3496 {
3497 struct extent_buffer *lower;
3498 int nritems;
3499 int ret;
3500
3501 BUG_ON(!path->nodes[level]);
3502 btrfs_assert_tree_locked(path->nodes[level]);
3503 lower = path->nodes[level];
3504 nritems = btrfs_header_nritems(lower);
3505 BUG_ON(slot > nritems);
3506 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(trans->fs_info));
3507 if (slot != nritems) {
3508 if (level) {
3509 ret = tree_mod_log_insert_move(lower, slot + 1, slot,
3510 nritems - slot);
3511 BUG_ON(ret < 0);
3512 }
3513 memmove_extent_buffer(lower,
3514 btrfs_node_key_ptr_offset(slot + 1),
3515 btrfs_node_key_ptr_offset(slot),
3516 (nritems - slot) * sizeof(struct btrfs_key_ptr));
3517 }
3518 if (level) {
3519 ret = tree_mod_log_insert_key(lower, slot, MOD_LOG_KEY_ADD,
3520 GFP_NOFS);
3521 BUG_ON(ret < 0);
3522 }
3523 btrfs_set_node_key(lower, key, slot);
3524 btrfs_set_node_blockptr(lower, slot, bytenr);
3525 WARN_ON(trans->transid == 0);
3526 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
3527 btrfs_set_header_nritems(lower, nritems + 1);
3528 btrfs_mark_buffer_dirty(lower);
3529 }
3530
3531 /*
3532 * split the node at the specified level in path in two.
3533 * The path is corrected to point to the appropriate node after the split
3534 *
3535 * Before splitting this tries to make some room in the node by pushing
3536 * left and right, if either one works, it returns right away.
3537 *
3538 * returns 0 on success and < 0 on failure
3539 */
split_node(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,int level)3540 static noinline int split_node(struct btrfs_trans_handle *trans,
3541 struct btrfs_root *root,
3542 struct btrfs_path *path, int level)
3543 {
3544 struct btrfs_fs_info *fs_info = root->fs_info;
3545 struct extent_buffer *c;
3546 struct extent_buffer *split;
3547 struct btrfs_disk_key disk_key;
3548 int mid;
3549 int ret;
3550 u32 c_nritems;
3551
3552 c = path->nodes[level];
3553 WARN_ON(btrfs_header_generation(c) != trans->transid);
3554 if (c == root->node) {
3555 /*
3556 * trying to split the root, lets make a new one
3557 *
3558 * tree mod log: We don't log_removal old root in
3559 * insert_new_root, because that root buffer will be kept as a
3560 * normal node. We are going to log removal of half of the
3561 * elements below with tree_mod_log_eb_copy. We're holding a
3562 * tree lock on the buffer, which is why we cannot race with
3563 * other tree_mod_log users.
3564 */
3565 ret = insert_new_root(trans, root, path, level + 1);
3566 if (ret)
3567 return ret;
3568 } else {
3569 ret = push_nodes_for_insert(trans, root, path, level);
3570 c = path->nodes[level];
3571 if (!ret && btrfs_header_nritems(c) <
3572 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3)
3573 return 0;
3574 if (ret < 0)
3575 return ret;
3576 }
3577
3578 c_nritems = btrfs_header_nritems(c);
3579 mid = (c_nritems + 1) / 2;
3580 btrfs_node_key(c, &disk_key, mid);
3581
3582 split = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, level,
3583 c->start, 0, BTRFS_NESTING_SPLIT);
3584 if (IS_ERR(split))
3585 return PTR_ERR(split);
3586
3587 root_add_used(root, fs_info->nodesize);
3588 ASSERT(btrfs_header_level(c) == level);
3589
3590 ret = tree_mod_log_eb_copy(split, c, 0, mid, c_nritems - mid);
3591 if (ret) {
3592 btrfs_abort_transaction(trans, ret);
3593 return ret;
3594 }
3595 copy_extent_buffer(split, c,
3596 btrfs_node_key_ptr_offset(0),
3597 btrfs_node_key_ptr_offset(mid),
3598 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3599 btrfs_set_header_nritems(split, c_nritems - mid);
3600 btrfs_set_header_nritems(c, mid);
3601 ret = 0;
3602
3603 btrfs_mark_buffer_dirty(c);
3604 btrfs_mark_buffer_dirty(split);
3605
3606 insert_ptr(trans, path, &disk_key, split->start,
3607 path->slots[level + 1] + 1, level + 1);
3608
3609 if (path->slots[level] >= mid) {
3610 path->slots[level] -= mid;
3611 btrfs_tree_unlock(c);
3612 free_extent_buffer(c);
3613 path->nodes[level] = split;
3614 path->slots[level + 1] += 1;
3615 } else {
3616 btrfs_tree_unlock(split);
3617 free_extent_buffer(split);
3618 }
3619 return ret;
3620 }
3621
3622 /*
3623 * how many bytes are required to store the items in a leaf. start
3624 * and nr indicate which items in the leaf to check. This totals up the
3625 * space used both by the item structs and the item data
3626 */
leaf_space_used(struct extent_buffer * l,int start,int nr)3627 static int leaf_space_used(struct extent_buffer *l, int start, int nr)
3628 {
3629 struct btrfs_item *start_item;
3630 struct btrfs_item *end_item;
3631 int data_len;
3632 int nritems = btrfs_header_nritems(l);
3633 int end = min(nritems, start + nr) - 1;
3634
3635 if (!nr)
3636 return 0;
3637 start_item = btrfs_item_nr(start);
3638 end_item = btrfs_item_nr(end);
3639 data_len = btrfs_item_offset(l, start_item) +
3640 btrfs_item_size(l, start_item);
3641 data_len = data_len - btrfs_item_offset(l, end_item);
3642 data_len += sizeof(struct btrfs_item) * nr;
3643 WARN_ON(data_len < 0);
3644 return data_len;
3645 }
3646
3647 /*
3648 * The space between the end of the leaf items and
3649 * the start of the leaf data. IOW, how much room
3650 * the leaf has left for both items and data
3651 */
btrfs_leaf_free_space(struct extent_buffer * leaf)3652 noinline int btrfs_leaf_free_space(struct extent_buffer *leaf)
3653 {
3654 struct btrfs_fs_info *fs_info = leaf->fs_info;
3655 int nritems = btrfs_header_nritems(leaf);
3656 int ret;
3657
3658 ret = BTRFS_LEAF_DATA_SIZE(fs_info) - leaf_space_used(leaf, 0, nritems);
3659 if (ret < 0) {
3660 btrfs_crit(fs_info,
3661 "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
3662 ret,
3663 (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info),
3664 leaf_space_used(leaf, 0, nritems), nritems);
3665 }
3666 return ret;
3667 }
3668
3669 /*
3670 * min slot controls the lowest index we're willing to push to the
3671 * right. We'll push up to and including min_slot, but no lower
3672 */
__push_leaf_right(struct btrfs_path * path,int data_size,int empty,struct extent_buffer * right,int free_space,u32 left_nritems,u32 min_slot)3673 static noinline int __push_leaf_right(struct btrfs_path *path,
3674 int data_size, int empty,
3675 struct extent_buffer *right,
3676 int free_space, u32 left_nritems,
3677 u32 min_slot)
3678 {
3679 struct btrfs_fs_info *fs_info = right->fs_info;
3680 struct extent_buffer *left = path->nodes[0];
3681 struct extent_buffer *upper = path->nodes[1];
3682 struct btrfs_map_token token;
3683 struct btrfs_disk_key disk_key;
3684 int slot;
3685 u32 i;
3686 int push_space = 0;
3687 int push_items = 0;
3688 struct btrfs_item *item;
3689 u32 nr;
3690 u32 right_nritems;
3691 u32 data_end;
3692 u32 this_item_size;
3693
3694 if (empty)
3695 nr = 0;
3696 else
3697 nr = max_t(u32, 1, min_slot);
3698
3699 if (path->slots[0] >= left_nritems)
3700 push_space += data_size;
3701
3702 slot = path->slots[1];
3703 i = left_nritems - 1;
3704 while (i >= nr) {
3705 item = btrfs_item_nr(i);
3706
3707 if (!empty && push_items > 0) {
3708 if (path->slots[0] > i)
3709 break;
3710 if (path->slots[0] == i) {
3711 int space = btrfs_leaf_free_space(left);
3712
3713 if (space + push_space * 2 > free_space)
3714 break;
3715 }
3716 }
3717
3718 if (path->slots[0] == i)
3719 push_space += data_size;
3720
3721 this_item_size = btrfs_item_size(left, item);
3722 if (this_item_size + sizeof(*item) + push_space > free_space)
3723 break;
3724
3725 push_items++;
3726 push_space += this_item_size + sizeof(*item);
3727 if (i == 0)
3728 break;
3729 i--;
3730 }
3731
3732 if (push_items == 0)
3733 goto out_unlock;
3734
3735 WARN_ON(!empty && push_items == left_nritems);
3736
3737 /* push left to right */
3738 right_nritems = btrfs_header_nritems(right);
3739
3740 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
3741 push_space -= leaf_data_end(left);
3742
3743 /* make room in the right data area */
3744 data_end = leaf_data_end(right);
3745 memmove_extent_buffer(right,
3746 BTRFS_LEAF_DATA_OFFSET + data_end - push_space,
3747 BTRFS_LEAF_DATA_OFFSET + data_end,
3748 BTRFS_LEAF_DATA_SIZE(fs_info) - data_end);
3749
3750 /* copy from the left data area */
3751 copy_extent_buffer(right, left, BTRFS_LEAF_DATA_OFFSET +
3752 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
3753 BTRFS_LEAF_DATA_OFFSET + leaf_data_end(left),
3754 push_space);
3755
3756 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3757 btrfs_item_nr_offset(0),
3758 right_nritems * sizeof(struct btrfs_item));
3759
3760 /* copy the items from left to right */
3761 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3762 btrfs_item_nr_offset(left_nritems - push_items),
3763 push_items * sizeof(struct btrfs_item));
3764
3765 /* update the item pointers */
3766 btrfs_init_map_token(&token, right);
3767 right_nritems += push_items;
3768 btrfs_set_header_nritems(right, right_nritems);
3769 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
3770 for (i = 0; i < right_nritems; i++) {
3771 item = btrfs_item_nr(i);
3772 push_space -= btrfs_token_item_size(&token, item);
3773 btrfs_set_token_item_offset(&token, item, push_space);
3774 }
3775
3776 left_nritems -= push_items;
3777 btrfs_set_header_nritems(left, left_nritems);
3778
3779 if (left_nritems)
3780 btrfs_mark_buffer_dirty(left);
3781 else
3782 btrfs_clean_tree_block(left);
3783
3784 btrfs_mark_buffer_dirty(right);
3785
3786 btrfs_item_key(right, &disk_key, 0);
3787 btrfs_set_node_key(upper, &disk_key, slot + 1);
3788 btrfs_mark_buffer_dirty(upper);
3789
3790 /* then fixup the leaf pointer in the path */
3791 if (path->slots[0] >= left_nritems) {
3792 path->slots[0] -= left_nritems;
3793 if (btrfs_header_nritems(path->nodes[0]) == 0)
3794 btrfs_clean_tree_block(path->nodes[0]);
3795 btrfs_tree_unlock(path->nodes[0]);
3796 free_extent_buffer(path->nodes[0]);
3797 path->nodes[0] = right;
3798 path->slots[1] += 1;
3799 } else {
3800 btrfs_tree_unlock(right);
3801 free_extent_buffer(right);
3802 }
3803 return 0;
3804
3805 out_unlock:
3806 btrfs_tree_unlock(right);
3807 free_extent_buffer(right);
3808 return 1;
3809 }
3810
3811 /*
3812 * push some data in the path leaf to the right, trying to free up at
3813 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3814 *
3815 * returns 1 if the push failed because the other node didn't have enough
3816 * room, 0 if everything worked out and < 0 if there were major errors.
3817 *
3818 * this will push starting from min_slot to the end of the leaf. It won't
3819 * push any slot lower than min_slot
3820 */
push_leaf_right(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,int min_data_size,int data_size,int empty,u32 min_slot)3821 static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
3822 *root, struct btrfs_path *path,
3823 int min_data_size, int data_size,
3824 int empty, u32 min_slot)
3825 {
3826 struct extent_buffer *left = path->nodes[0];
3827 struct extent_buffer *right;
3828 struct extent_buffer *upper;
3829 int slot;
3830 int free_space;
3831 u32 left_nritems;
3832 int ret;
3833
3834 if (!path->nodes[1])
3835 return 1;
3836
3837 slot = path->slots[1];
3838 upper = path->nodes[1];
3839 if (slot >= btrfs_header_nritems(upper) - 1)
3840 return 1;
3841
3842 btrfs_assert_tree_locked(path->nodes[1]);
3843
3844 right = btrfs_read_node_slot(upper, slot + 1);
3845 /*
3846 * slot + 1 is not valid or we fail to read the right node,
3847 * no big deal, just return.
3848 */
3849 if (IS_ERR(right))
3850 return 1;
3851
3852 __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT);
3853 btrfs_set_lock_blocking_write(right);
3854
3855 free_space = btrfs_leaf_free_space(right);
3856 if (free_space < data_size)
3857 goto out_unlock;
3858
3859 /* cow and double check */
3860 ret = btrfs_cow_block(trans, root, right, upper,
3861 slot + 1, &right, BTRFS_NESTING_RIGHT_COW);
3862 if (ret)
3863 goto out_unlock;
3864
3865 free_space = btrfs_leaf_free_space(right);
3866 if (free_space < data_size)
3867 goto out_unlock;
3868
3869 left_nritems = btrfs_header_nritems(left);
3870 if (left_nritems == 0)
3871 goto out_unlock;
3872
3873 if (check_sibling_keys(left, right)) {
3874 ret = -EUCLEAN;
3875 btrfs_tree_unlock(right);
3876 free_extent_buffer(right);
3877 return ret;
3878 }
3879 if (path->slots[0] == left_nritems && !empty) {
3880 /* Key greater than all keys in the leaf, right neighbor has
3881 * enough room for it and we're not emptying our leaf to delete
3882 * it, therefore use right neighbor to insert the new item and
3883 * no need to touch/dirty our left leaf. */
3884 btrfs_tree_unlock(left);
3885 free_extent_buffer(left);
3886 path->nodes[0] = right;
3887 path->slots[0] = 0;
3888 path->slots[1]++;
3889 return 0;
3890 }
3891
3892 return __push_leaf_right(path, min_data_size, empty,
3893 right, free_space, left_nritems, min_slot);
3894 out_unlock:
3895 btrfs_tree_unlock(right);
3896 free_extent_buffer(right);
3897 return 1;
3898 }
3899
3900 /*
3901 * push some data in the path leaf to the left, trying to free up at
3902 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3903 *
3904 * max_slot can put a limit on how far into the leaf we'll push items. The
3905 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3906 * items
3907 */
__push_leaf_left(struct btrfs_path * path,int data_size,int empty,struct extent_buffer * left,int free_space,u32 right_nritems,u32 max_slot)3908 static noinline int __push_leaf_left(struct btrfs_path *path, int data_size,
3909 int empty, struct extent_buffer *left,
3910 int free_space, u32 right_nritems,
3911 u32 max_slot)
3912 {
3913 struct btrfs_fs_info *fs_info = left->fs_info;
3914 struct btrfs_disk_key disk_key;
3915 struct extent_buffer *right = path->nodes[0];
3916 int i;
3917 int push_space = 0;
3918 int push_items = 0;
3919 struct btrfs_item *item;
3920 u32 old_left_nritems;
3921 u32 nr;
3922 int ret = 0;
3923 u32 this_item_size;
3924 u32 old_left_item_size;
3925 struct btrfs_map_token token;
3926
3927 if (empty)
3928 nr = min(right_nritems, max_slot);
3929 else
3930 nr = min(right_nritems - 1, max_slot);
3931
3932 for (i = 0; i < nr; i++) {
3933 item = btrfs_item_nr(i);
3934
3935 if (!empty && push_items > 0) {
3936 if (path->slots[0] < i)
3937 break;
3938 if (path->slots[0] == i) {
3939 int space = btrfs_leaf_free_space(right);
3940
3941 if (space + push_space * 2 > free_space)
3942 break;
3943 }
3944 }
3945
3946 if (path->slots[0] == i)
3947 push_space += data_size;
3948
3949 this_item_size = btrfs_item_size(right, item);
3950 if (this_item_size + sizeof(*item) + push_space > free_space)
3951 break;
3952
3953 push_items++;
3954 push_space += this_item_size + sizeof(*item);
3955 }
3956
3957 if (push_items == 0) {
3958 ret = 1;
3959 goto out;
3960 }
3961 WARN_ON(!empty && push_items == btrfs_header_nritems(right));
3962
3963 /* push data from right to left */
3964 copy_extent_buffer(left, right,
3965 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3966 btrfs_item_nr_offset(0),
3967 push_items * sizeof(struct btrfs_item));
3968
3969 push_space = BTRFS_LEAF_DATA_SIZE(fs_info) -
3970 btrfs_item_offset_nr(right, push_items - 1);
3971
3972 copy_extent_buffer(left, right, BTRFS_LEAF_DATA_OFFSET +
3973 leaf_data_end(left) - push_space,
3974 BTRFS_LEAF_DATA_OFFSET +
3975 btrfs_item_offset_nr(right, push_items - 1),
3976 push_space);
3977 old_left_nritems = btrfs_header_nritems(left);
3978 BUG_ON(old_left_nritems <= 0);
3979
3980 btrfs_init_map_token(&token, left);
3981 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
3982 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
3983 u32 ioff;
3984
3985 item = btrfs_item_nr(i);
3986
3987 ioff = btrfs_token_item_offset(&token, item);
3988 btrfs_set_token_item_offset(&token, item,
3989 ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size));
3990 }
3991 btrfs_set_header_nritems(left, old_left_nritems + push_items);
3992
3993 /* fixup right node */
3994 if (push_items > right_nritems)
3995 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
3996 right_nritems);
3997
3998 if (push_items < right_nritems) {
3999 push_space = btrfs_item_offset_nr(right, push_items - 1) -
4000 leaf_data_end(right);
4001 memmove_extent_buffer(right, BTRFS_LEAF_DATA_OFFSET +
4002 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
4003 BTRFS_LEAF_DATA_OFFSET +
4004 leaf_data_end(right), push_space);
4005
4006 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
4007 btrfs_item_nr_offset(push_items),
4008 (btrfs_header_nritems(right) - push_items) *
4009 sizeof(struct btrfs_item));
4010 }
4011
4012 btrfs_init_map_token(&token, right);
4013 right_nritems -= push_items;
4014 btrfs_set_header_nritems(right, right_nritems);
4015 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
4016 for (i = 0; i < right_nritems; i++) {
4017 item = btrfs_item_nr(i);
4018
4019 push_space = push_space - btrfs_token_item_size(&token, item);
4020 btrfs_set_token_item_offset(&token, item, push_space);
4021 }
4022
4023 btrfs_mark_buffer_dirty(left);
4024 if (right_nritems)
4025 btrfs_mark_buffer_dirty(right);
4026 else
4027 btrfs_clean_tree_block(right);
4028
4029 btrfs_item_key(right, &disk_key, 0);
4030 fixup_low_keys(path, &disk_key, 1);
4031
4032 /* then fixup the leaf pointer in the path */
4033 if (path->slots[0] < push_items) {
4034 path->slots[0] += old_left_nritems;
4035 btrfs_tree_unlock(path->nodes[0]);
4036 free_extent_buffer(path->nodes[0]);
4037 path->nodes[0] = left;
4038 path->slots[1] -= 1;
4039 } else {
4040 btrfs_tree_unlock(left);
4041 free_extent_buffer(left);
4042 path->slots[0] -= push_items;
4043 }
4044 BUG_ON(path->slots[0] < 0);
4045 return ret;
4046 out:
4047 btrfs_tree_unlock(left);
4048 free_extent_buffer(left);
4049 return ret;
4050 }
4051
4052 /*
4053 * push some data in the path leaf to the left, trying to free up at
4054 * least data_size bytes. returns zero if the push worked, nonzero otherwise
4055 *
4056 * max_slot can put a limit on how far into the leaf we'll push items. The
4057 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
4058 * items
4059 */
push_leaf_left(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,int min_data_size,int data_size,int empty,u32 max_slot)4060 static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
4061 *root, struct btrfs_path *path, int min_data_size,
4062 int data_size, int empty, u32 max_slot)
4063 {
4064 struct extent_buffer *right = path->nodes[0];
4065 struct extent_buffer *left;
4066 int slot;
4067 int free_space;
4068 u32 right_nritems;
4069 int ret = 0;
4070
4071 slot = path->slots[1];
4072 if (slot == 0)
4073 return 1;
4074 if (!path->nodes[1])
4075 return 1;
4076
4077 right_nritems = btrfs_header_nritems(right);
4078 if (right_nritems == 0)
4079 return 1;
4080
4081 btrfs_assert_tree_locked(path->nodes[1]);
4082
4083 left = btrfs_read_node_slot(path->nodes[1], slot - 1);
4084 /*
4085 * slot - 1 is not valid or we fail to read the left node,
4086 * no big deal, just return.
4087 */
4088 if (IS_ERR(left))
4089 return 1;
4090
4091 __btrfs_tree_lock(left, BTRFS_NESTING_LEFT);
4092 btrfs_set_lock_blocking_write(left);
4093
4094 free_space = btrfs_leaf_free_space(left);
4095 if (free_space < data_size) {
4096 ret = 1;
4097 goto out;
4098 }
4099
4100 /* cow and double check */
4101 ret = btrfs_cow_block(trans, root, left,
4102 path->nodes[1], slot - 1, &left,
4103 BTRFS_NESTING_LEFT_COW);
4104 if (ret) {
4105 /* we hit -ENOSPC, but it isn't fatal here */
4106 if (ret == -ENOSPC)
4107 ret = 1;
4108 goto out;
4109 }
4110
4111 free_space = btrfs_leaf_free_space(left);
4112 if (free_space < data_size) {
4113 ret = 1;
4114 goto out;
4115 }
4116
4117 if (check_sibling_keys(left, right)) {
4118 ret = -EUCLEAN;
4119 goto out;
4120 }
4121 return __push_leaf_left(path, min_data_size,
4122 empty, left, free_space, right_nritems,
4123 max_slot);
4124 out:
4125 btrfs_tree_unlock(left);
4126 free_extent_buffer(left);
4127 return ret;
4128 }
4129
4130 /*
4131 * split the path's leaf in two, making sure there is at least data_size
4132 * available for the resulting leaf level of the path.
4133 */
copy_for_split(struct btrfs_trans_handle * trans,struct btrfs_path * path,struct extent_buffer * l,struct extent_buffer * right,int slot,int mid,int nritems)4134 static noinline void copy_for_split(struct btrfs_trans_handle *trans,
4135 struct btrfs_path *path,
4136 struct extent_buffer *l,
4137 struct extent_buffer *right,
4138 int slot, int mid, int nritems)
4139 {
4140 struct btrfs_fs_info *fs_info = trans->fs_info;
4141 int data_copy_size;
4142 int rt_data_off;
4143 int i;
4144 struct btrfs_disk_key disk_key;
4145 struct btrfs_map_token token;
4146
4147 nritems = nritems - mid;
4148 btrfs_set_header_nritems(right, nritems);
4149 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(l);
4150
4151 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
4152 btrfs_item_nr_offset(mid),
4153 nritems * sizeof(struct btrfs_item));
4154
4155 copy_extent_buffer(right, l,
4156 BTRFS_LEAF_DATA_OFFSET + BTRFS_LEAF_DATA_SIZE(fs_info) -
4157 data_copy_size, BTRFS_LEAF_DATA_OFFSET +
4158 leaf_data_end(l), data_copy_size);
4159
4160 rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_end_nr(l, mid);
4161
4162 btrfs_init_map_token(&token, right);
4163 for (i = 0; i < nritems; i++) {
4164 struct btrfs_item *item = btrfs_item_nr(i);
4165 u32 ioff;
4166
4167 ioff = btrfs_token_item_offset(&token, item);
4168 btrfs_set_token_item_offset(&token, item, ioff + rt_data_off);
4169 }
4170
4171 btrfs_set_header_nritems(l, mid);
4172 btrfs_item_key(right, &disk_key, 0);
4173 insert_ptr(trans, path, &disk_key, right->start, path->slots[1] + 1, 1);
4174
4175 btrfs_mark_buffer_dirty(right);
4176 btrfs_mark_buffer_dirty(l);
4177 BUG_ON(path->slots[0] != slot);
4178
4179 if (mid <= slot) {
4180 btrfs_tree_unlock(path->nodes[0]);
4181 free_extent_buffer(path->nodes[0]);
4182 path->nodes[0] = right;
4183 path->slots[0] -= mid;
4184 path->slots[1] += 1;
4185 } else {
4186 btrfs_tree_unlock(right);
4187 free_extent_buffer(right);
4188 }
4189
4190 BUG_ON(path->slots[0] < 0);
4191 }
4192
4193 /*
4194 * double splits happen when we need to insert a big item in the middle
4195 * of a leaf. A double split can leave us with 3 mostly empty leaves:
4196 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
4197 * A B C
4198 *
4199 * We avoid this by trying to push the items on either side of our target
4200 * into the adjacent leaves. If all goes well we can avoid the double split
4201 * completely.
4202 */
push_for_double_split(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,int data_size)4203 static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
4204 struct btrfs_root *root,
4205 struct btrfs_path *path,
4206 int data_size)
4207 {
4208 int ret;
4209 int progress = 0;
4210 int slot;
4211 u32 nritems;
4212 int space_needed = data_size;
4213
4214 slot = path->slots[0];
4215 if (slot < btrfs_header_nritems(path->nodes[0]))
4216 space_needed -= btrfs_leaf_free_space(path->nodes[0]);
4217
4218 /*
4219 * try to push all the items after our slot into the
4220 * right leaf
4221 */
4222 ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
4223 if (ret < 0)
4224 return ret;
4225
4226 if (ret == 0)
4227 progress++;
4228
4229 nritems = btrfs_header_nritems(path->nodes[0]);
4230 /*
4231 * our goal is to get our slot at the start or end of a leaf. If
4232 * we've done so we're done
4233 */
4234 if (path->slots[0] == 0 || path->slots[0] == nritems)
4235 return 0;
4236
4237 if (btrfs_leaf_free_space(path->nodes[0]) >= data_size)
4238 return 0;
4239
4240 /* try to push all the items before our slot into the next leaf */
4241 slot = path->slots[0];
4242 space_needed = data_size;
4243 if (slot > 0)
4244 space_needed -= btrfs_leaf_free_space(path->nodes[0]);
4245 ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
4246 if (ret < 0)
4247 return ret;
4248
4249 if (ret == 0)
4250 progress++;
4251
4252 if (progress)
4253 return 0;
4254 return 1;
4255 }
4256
4257 /*
4258 * split the path's leaf in two, making sure there is at least data_size
4259 * available for the resulting leaf level of the path.
4260 *
4261 * returns 0 if all went well and < 0 on failure.
4262 */
split_leaf(struct btrfs_trans_handle * trans,struct btrfs_root * root,const struct btrfs_key * ins_key,struct btrfs_path * path,int data_size,int extend)4263 static noinline int split_leaf(struct btrfs_trans_handle *trans,
4264 struct btrfs_root *root,
4265 const struct btrfs_key *ins_key,
4266 struct btrfs_path *path, int data_size,
4267 int extend)
4268 {
4269 struct btrfs_disk_key disk_key;
4270 struct extent_buffer *l;
4271 u32 nritems;
4272 int mid;
4273 int slot;
4274 struct extent_buffer *right;
4275 struct btrfs_fs_info *fs_info = root->fs_info;
4276 int ret = 0;
4277 int wret;
4278 int split;
4279 int num_doubles = 0;
4280 int tried_avoid_double = 0;
4281
4282 l = path->nodes[0];
4283 slot = path->slots[0];
4284 if (extend && data_size + btrfs_item_size_nr(l, slot) +
4285 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info))
4286 return -EOVERFLOW;
4287
4288 /* first try to make some room by pushing left and right */
4289 if (data_size && path->nodes[1]) {
4290 int space_needed = data_size;
4291
4292 if (slot < btrfs_header_nritems(l))
4293 space_needed -= btrfs_leaf_free_space(l);
4294
4295 wret = push_leaf_right(trans, root, path, space_needed,
4296 space_needed, 0, 0);
4297 if (wret < 0)
4298 return wret;
4299 if (wret) {
4300 space_needed = data_size;
4301 if (slot > 0)
4302 space_needed -= btrfs_leaf_free_space(l);
4303 wret = push_leaf_left(trans, root, path, space_needed,
4304 space_needed, 0, (u32)-1);
4305 if (wret < 0)
4306 return wret;
4307 }
4308 l = path->nodes[0];
4309
4310 /* did the pushes work? */
4311 if (btrfs_leaf_free_space(l) >= data_size)
4312 return 0;
4313 }
4314
4315 if (!path->nodes[1]) {
4316 ret = insert_new_root(trans, root, path, 1);
4317 if (ret)
4318 return ret;
4319 }
4320 again:
4321 split = 1;
4322 l = path->nodes[0];
4323 slot = path->slots[0];
4324 nritems = btrfs_header_nritems(l);
4325 mid = (nritems + 1) / 2;
4326
4327 if (mid <= slot) {
4328 if (nritems == 1 ||
4329 leaf_space_used(l, mid, nritems - mid) + data_size >
4330 BTRFS_LEAF_DATA_SIZE(fs_info)) {
4331 if (slot >= nritems) {
4332 split = 0;
4333 } else {
4334 mid = slot;
4335 if (mid != nritems &&
4336 leaf_space_used(l, mid, nritems - mid) +
4337 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
4338 if (data_size && !tried_avoid_double)
4339 goto push_for_double;
4340 split = 2;
4341 }
4342 }
4343 }
4344 } else {
4345 if (leaf_space_used(l, 0, mid) + data_size >
4346 BTRFS_LEAF_DATA_SIZE(fs_info)) {
4347 if (!extend && data_size && slot == 0) {
4348 split = 0;
4349 } else if ((extend || !data_size) && slot == 0) {
4350 mid = 1;
4351 } else {
4352 mid = slot;
4353 if (mid != nritems &&
4354 leaf_space_used(l, mid, nritems - mid) +
4355 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
4356 if (data_size && !tried_avoid_double)
4357 goto push_for_double;
4358 split = 2;
4359 }
4360 }
4361 }
4362 }
4363
4364 if (split == 0)
4365 btrfs_cpu_key_to_disk(&disk_key, ins_key);
4366 else
4367 btrfs_item_key(l, &disk_key, mid);
4368
4369 /*
4370 * We have to about BTRFS_NESTING_NEW_ROOT here if we've done a double
4371 * split, because we're only allowed to have MAX_LOCKDEP_SUBCLASSES
4372 * subclasses, which is 8 at the time of this patch, and we've maxed it
4373 * out. In the future we could add a
4374 * BTRFS_NESTING_SPLIT_THE_SPLITTENING if we need to, but for now just
4375 * use BTRFS_NESTING_NEW_ROOT.
4376 */
4377 right = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, 0,
4378 l->start, 0, num_doubles ?
4379 BTRFS_NESTING_NEW_ROOT :
4380 BTRFS_NESTING_SPLIT);
4381 if (IS_ERR(right))
4382 return PTR_ERR(right);
4383
4384 root_add_used(root, fs_info->nodesize);
4385
4386 if (split == 0) {
4387 if (mid <= slot) {
4388 btrfs_set_header_nritems(right, 0);
4389 insert_ptr(trans, path, &disk_key,
4390 right->start, path->slots[1] + 1, 1);
4391 btrfs_tree_unlock(path->nodes[0]);
4392 free_extent_buffer(path->nodes[0]);
4393 path->nodes[0] = right;
4394 path->slots[0] = 0;
4395 path->slots[1] += 1;
4396 } else {
4397 btrfs_set_header_nritems(right, 0);
4398 insert_ptr(trans, path, &disk_key,
4399 right->start, path->slots[1], 1);
4400 btrfs_tree_unlock(path->nodes[0]);
4401 free_extent_buffer(path->nodes[0]);
4402 path->nodes[0] = right;
4403 path->slots[0] = 0;
4404 if (path->slots[1] == 0)
4405 fixup_low_keys(path, &disk_key, 1);
4406 }
4407 /*
4408 * We create a new leaf 'right' for the required ins_len and
4409 * we'll do btrfs_mark_buffer_dirty() on this leaf after copying
4410 * the content of ins_len to 'right'.
4411 */
4412 return ret;
4413 }
4414
4415 copy_for_split(trans, path, l, right, slot, mid, nritems);
4416
4417 if (split == 2) {
4418 BUG_ON(num_doubles != 0);
4419 num_doubles++;
4420 goto again;
4421 }
4422
4423 return 0;
4424
4425 push_for_double:
4426 push_for_double_split(trans, root, path, data_size);
4427 tried_avoid_double = 1;
4428 if (btrfs_leaf_free_space(path->nodes[0]) >= data_size)
4429 return 0;
4430 goto again;
4431 }
4432
setup_leaf_for_split(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,int ins_len)4433 static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4434 struct btrfs_root *root,
4435 struct btrfs_path *path, int ins_len)
4436 {
4437 struct btrfs_key key;
4438 struct extent_buffer *leaf;
4439 struct btrfs_file_extent_item *fi;
4440 u64 extent_len = 0;
4441 u32 item_size;
4442 int ret;
4443
4444 leaf = path->nodes[0];
4445 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4446
4447 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4448 key.type != BTRFS_EXTENT_CSUM_KEY);
4449
4450 if (btrfs_leaf_free_space(leaf) >= ins_len)
4451 return 0;
4452
4453 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4454 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4455 fi = btrfs_item_ptr(leaf, path->slots[0],
4456 struct btrfs_file_extent_item);
4457 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4458 }
4459 btrfs_release_path(path);
4460
4461 path->keep_locks = 1;
4462 path->search_for_split = 1;
4463 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4464 path->search_for_split = 0;
4465 if (ret > 0)
4466 ret = -EAGAIN;
4467 if (ret < 0)
4468 goto err;
4469
4470 ret = -EAGAIN;
4471 leaf = path->nodes[0];
4472 /* if our item isn't there, return now */
4473 if (item_size != btrfs_item_size_nr(leaf, path->slots[0]))
4474 goto err;
4475
4476 /* the leaf has changed, it now has room. return now */
4477 if (btrfs_leaf_free_space(path->nodes[0]) >= ins_len)
4478 goto err;
4479
4480 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4481 fi = btrfs_item_ptr(leaf, path->slots[0],
4482 struct btrfs_file_extent_item);
4483 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4484 goto err;
4485 }
4486
4487 btrfs_set_path_blocking(path);
4488 ret = split_leaf(trans, root, &key, path, ins_len, 1);
4489 if (ret)
4490 goto err;
4491
4492 path->keep_locks = 0;
4493 btrfs_unlock_up_safe(path, 1);
4494 return 0;
4495 err:
4496 path->keep_locks = 0;
4497 return ret;
4498 }
4499
split_item(struct btrfs_path * path,const struct btrfs_key * new_key,unsigned long split_offset)4500 static noinline int split_item(struct btrfs_path *path,
4501 const struct btrfs_key *new_key,
4502 unsigned long split_offset)
4503 {
4504 struct extent_buffer *leaf;
4505 struct btrfs_item *item;
4506 struct btrfs_item *new_item;
4507 int slot;
4508 char *buf;
4509 u32 nritems;
4510 u32 item_size;
4511 u32 orig_offset;
4512 struct btrfs_disk_key disk_key;
4513
4514 leaf = path->nodes[0];
4515 BUG_ON(btrfs_leaf_free_space(leaf) < sizeof(struct btrfs_item));
4516
4517 btrfs_set_path_blocking(path);
4518
4519 item = btrfs_item_nr(path->slots[0]);
4520 orig_offset = btrfs_item_offset(leaf, item);
4521 item_size = btrfs_item_size(leaf, item);
4522
4523 buf = kmalloc(item_size, GFP_NOFS);
4524 if (!buf)
4525 return -ENOMEM;
4526
4527 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4528 path->slots[0]), item_size);
4529
4530 slot = path->slots[0] + 1;
4531 nritems = btrfs_header_nritems(leaf);
4532 if (slot != nritems) {
4533 /* shift the items */
4534 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
4535 btrfs_item_nr_offset(slot),
4536 (nritems - slot) * sizeof(struct btrfs_item));
4537 }
4538
4539 btrfs_cpu_key_to_disk(&disk_key, new_key);
4540 btrfs_set_item_key(leaf, &disk_key, slot);
4541
4542 new_item = btrfs_item_nr(slot);
4543
4544 btrfs_set_item_offset(leaf, new_item, orig_offset);
4545 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4546
4547 btrfs_set_item_offset(leaf, item,
4548 orig_offset + item_size - split_offset);
4549 btrfs_set_item_size(leaf, item, split_offset);
4550
4551 btrfs_set_header_nritems(leaf, nritems + 1);
4552
4553 /* write the data for the start of the original item */
4554 write_extent_buffer(leaf, buf,
4555 btrfs_item_ptr_offset(leaf, path->slots[0]),
4556 split_offset);
4557
4558 /* write the data for the new item */
4559 write_extent_buffer(leaf, buf + split_offset,
4560 btrfs_item_ptr_offset(leaf, slot),
4561 item_size - split_offset);
4562 btrfs_mark_buffer_dirty(leaf);
4563
4564 BUG_ON(btrfs_leaf_free_space(leaf) < 0);
4565 kfree(buf);
4566 return 0;
4567 }
4568
4569 /*
4570 * This function splits a single item into two items,
4571 * giving 'new_key' to the new item and splitting the
4572 * old one at split_offset (from the start of the item).
4573 *
4574 * The path may be released by this operation. After
4575 * the split, the path is pointing to the old item. The
4576 * new item is going to be in the same node as the old one.
4577 *
4578 * Note, the item being split must be smaller enough to live alone on
4579 * a tree block with room for one extra struct btrfs_item
4580 *
4581 * This allows us to split the item in place, keeping a lock on the
4582 * leaf the entire time.
4583 */
btrfs_split_item(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,const struct btrfs_key * new_key,unsigned long split_offset)4584 int btrfs_split_item(struct btrfs_trans_handle *trans,
4585 struct btrfs_root *root,
4586 struct btrfs_path *path,
4587 const struct btrfs_key *new_key,
4588 unsigned long split_offset)
4589 {
4590 int ret;
4591 ret = setup_leaf_for_split(trans, root, path,
4592 sizeof(struct btrfs_item));
4593 if (ret)
4594 return ret;
4595
4596 ret = split_item(path, new_key, split_offset);
4597 return ret;
4598 }
4599
4600 /*
4601 * This function duplicate a item, giving 'new_key' to the new item.
4602 * It guarantees both items live in the same tree leaf and the new item
4603 * is contiguous with the original item.
4604 *
4605 * This allows us to split file extent in place, keeping a lock on the
4606 * leaf the entire time.
4607 */
btrfs_duplicate_item(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,const struct btrfs_key * new_key)4608 int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4609 struct btrfs_root *root,
4610 struct btrfs_path *path,
4611 const struct btrfs_key *new_key)
4612 {
4613 struct extent_buffer *leaf;
4614 int ret;
4615 u32 item_size;
4616
4617 leaf = path->nodes[0];
4618 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4619 ret = setup_leaf_for_split(trans, root, path,
4620 item_size + sizeof(struct btrfs_item));
4621 if (ret)
4622 return ret;
4623
4624 path->slots[0]++;
4625 setup_items_for_insert(root, path, new_key, &item_size, 1);
4626 leaf = path->nodes[0];
4627 memcpy_extent_buffer(leaf,
4628 btrfs_item_ptr_offset(leaf, path->slots[0]),
4629 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4630 item_size);
4631 return 0;
4632 }
4633
4634 /*
4635 * make the item pointed to by the path smaller. new_size indicates
4636 * how small to make it, and from_end tells us if we just chop bytes
4637 * off the end of the item or if we shift the item to chop bytes off
4638 * the front.
4639 */
btrfs_truncate_item(struct btrfs_path * path,u32 new_size,int from_end)4640 void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end)
4641 {
4642 int slot;
4643 struct extent_buffer *leaf;
4644 struct btrfs_item *item;
4645 u32 nritems;
4646 unsigned int data_end;
4647 unsigned int old_data_start;
4648 unsigned int old_size;
4649 unsigned int size_diff;
4650 int i;
4651 struct btrfs_map_token token;
4652
4653 leaf = path->nodes[0];
4654 slot = path->slots[0];
4655
4656 old_size = btrfs_item_size_nr(leaf, slot);
4657 if (old_size == new_size)
4658 return;
4659
4660 nritems = btrfs_header_nritems(leaf);
4661 data_end = leaf_data_end(leaf);
4662
4663 old_data_start = btrfs_item_offset_nr(leaf, slot);
4664
4665 size_diff = old_size - new_size;
4666
4667 BUG_ON(slot < 0);
4668 BUG_ON(slot >= nritems);
4669
4670 /*
4671 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4672 */
4673 /* first correct the data pointers */
4674 btrfs_init_map_token(&token, leaf);
4675 for (i = slot; i < nritems; i++) {
4676 u32 ioff;
4677 item = btrfs_item_nr(i);
4678
4679 ioff = btrfs_token_item_offset(&token, item);
4680 btrfs_set_token_item_offset(&token, item, ioff + size_diff);
4681 }
4682
4683 /* shift the data */
4684 if (from_end) {
4685 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4686 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
4687 data_end, old_data_start + new_size - data_end);
4688 } else {
4689 struct btrfs_disk_key disk_key;
4690 u64 offset;
4691
4692 btrfs_item_key(leaf, &disk_key, slot);
4693
4694 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4695 unsigned long ptr;
4696 struct btrfs_file_extent_item *fi;
4697
4698 fi = btrfs_item_ptr(leaf, slot,
4699 struct btrfs_file_extent_item);
4700 fi = (struct btrfs_file_extent_item *)(
4701 (unsigned long)fi - size_diff);
4702
4703 if (btrfs_file_extent_type(leaf, fi) ==
4704 BTRFS_FILE_EXTENT_INLINE) {
4705 ptr = btrfs_item_ptr_offset(leaf, slot);
4706 memmove_extent_buffer(leaf, ptr,
4707 (unsigned long)fi,
4708 BTRFS_FILE_EXTENT_INLINE_DATA_START);
4709 }
4710 }
4711
4712 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4713 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
4714 data_end, old_data_start - data_end);
4715
4716 offset = btrfs_disk_key_offset(&disk_key);
4717 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4718 btrfs_set_item_key(leaf, &disk_key, slot);
4719 if (slot == 0)
4720 fixup_low_keys(path, &disk_key, 1);
4721 }
4722
4723 item = btrfs_item_nr(slot);
4724 btrfs_set_item_size(leaf, item, new_size);
4725 btrfs_mark_buffer_dirty(leaf);
4726
4727 if (btrfs_leaf_free_space(leaf) < 0) {
4728 btrfs_print_leaf(leaf);
4729 BUG();
4730 }
4731 }
4732
4733 /*
4734 * make the item pointed to by the path bigger, data_size is the added size.
4735 */
btrfs_extend_item(struct btrfs_path * path,u32 data_size)4736 void btrfs_extend_item(struct btrfs_path *path, u32 data_size)
4737 {
4738 int slot;
4739 struct extent_buffer *leaf;
4740 struct btrfs_item *item;
4741 u32 nritems;
4742 unsigned int data_end;
4743 unsigned int old_data;
4744 unsigned int old_size;
4745 int i;
4746 struct btrfs_map_token token;
4747
4748 leaf = path->nodes[0];
4749
4750 nritems = btrfs_header_nritems(leaf);
4751 data_end = leaf_data_end(leaf);
4752
4753 if (btrfs_leaf_free_space(leaf) < data_size) {
4754 btrfs_print_leaf(leaf);
4755 BUG();
4756 }
4757 slot = path->slots[0];
4758 old_data = btrfs_item_end_nr(leaf, slot);
4759
4760 BUG_ON(slot < 0);
4761 if (slot >= nritems) {
4762 btrfs_print_leaf(leaf);
4763 btrfs_crit(leaf->fs_info, "slot %d too large, nritems %d",
4764 slot, nritems);
4765 BUG();
4766 }
4767
4768 /*
4769 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4770 */
4771 /* first correct the data pointers */
4772 btrfs_init_map_token(&token, leaf);
4773 for (i = slot; i < nritems; i++) {
4774 u32 ioff;
4775 item = btrfs_item_nr(i);
4776
4777 ioff = btrfs_token_item_offset(&token, item);
4778 btrfs_set_token_item_offset(&token, item, ioff - data_size);
4779 }
4780
4781 /* shift the data */
4782 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4783 data_end - data_size, BTRFS_LEAF_DATA_OFFSET +
4784 data_end, old_data - data_end);
4785
4786 data_end = old_data;
4787 old_size = btrfs_item_size_nr(leaf, slot);
4788 item = btrfs_item_nr(slot);
4789 btrfs_set_item_size(leaf, item, old_size + data_size);
4790 btrfs_mark_buffer_dirty(leaf);
4791
4792 if (btrfs_leaf_free_space(leaf) < 0) {
4793 btrfs_print_leaf(leaf);
4794 BUG();
4795 }
4796 }
4797
4798 /**
4799 * setup_items_for_insert - Helper called before inserting one or more items
4800 * to a leaf. Main purpose is to save stack depth by doing the bulk of the work
4801 * in a function that doesn't call btrfs_search_slot
4802 *
4803 * @root: root we are inserting items to
4804 * @path: points to the leaf/slot where we are going to insert new items
4805 * @cpu_key: array of keys for items to be inserted
4806 * @data_size: size of the body of each item we are going to insert
4807 * @nr: size of @cpu_key/@data_size arrays
4808 */
setup_items_for_insert(struct btrfs_root * root,struct btrfs_path * path,const struct btrfs_key * cpu_key,u32 * data_size,int nr)4809 void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
4810 const struct btrfs_key *cpu_key, u32 *data_size,
4811 int nr)
4812 {
4813 struct btrfs_fs_info *fs_info = root->fs_info;
4814 struct btrfs_item *item;
4815 int i;
4816 u32 nritems;
4817 unsigned int data_end;
4818 struct btrfs_disk_key disk_key;
4819 struct extent_buffer *leaf;
4820 int slot;
4821 struct btrfs_map_token token;
4822 u32 total_size;
4823 u32 total_data = 0;
4824
4825 for (i = 0; i < nr; i++)
4826 total_data += data_size[i];
4827 total_size = total_data + (nr * sizeof(struct btrfs_item));
4828
4829 if (path->slots[0] == 0) {
4830 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
4831 fixup_low_keys(path, &disk_key, 1);
4832 }
4833 btrfs_unlock_up_safe(path, 1);
4834
4835 leaf = path->nodes[0];
4836 slot = path->slots[0];
4837
4838 nritems = btrfs_header_nritems(leaf);
4839 data_end = leaf_data_end(leaf);
4840
4841 if (btrfs_leaf_free_space(leaf) < total_size) {
4842 btrfs_print_leaf(leaf);
4843 btrfs_crit(fs_info, "not enough freespace need %u have %d",
4844 total_size, btrfs_leaf_free_space(leaf));
4845 BUG();
4846 }
4847
4848 btrfs_init_map_token(&token, leaf);
4849 if (slot != nritems) {
4850 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
4851
4852 if (old_data < data_end) {
4853 btrfs_print_leaf(leaf);
4854 btrfs_crit(fs_info,
4855 "item at slot %d with data offset %u beyond data end of leaf %u",
4856 slot, old_data, data_end);
4857 BUG();
4858 }
4859 /*
4860 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4861 */
4862 /* first correct the data pointers */
4863 for (i = slot; i < nritems; i++) {
4864 u32 ioff;
4865
4866 item = btrfs_item_nr(i);
4867 ioff = btrfs_token_item_offset(&token, item);
4868 btrfs_set_token_item_offset(&token, item,
4869 ioff - total_data);
4870 }
4871 /* shift the items */
4872 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
4873 btrfs_item_nr_offset(slot),
4874 (nritems - slot) * sizeof(struct btrfs_item));
4875
4876 /* shift the data */
4877 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4878 data_end - total_data, BTRFS_LEAF_DATA_OFFSET +
4879 data_end, old_data - data_end);
4880 data_end = old_data;
4881 }
4882
4883 /* setup the item for the new data */
4884 for (i = 0; i < nr; i++) {
4885 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4886 btrfs_set_item_key(leaf, &disk_key, slot + i);
4887 item = btrfs_item_nr(slot + i);
4888 data_end -= data_size[i];
4889 btrfs_set_token_item_offset(&token, item, data_end);
4890 btrfs_set_token_item_size(&token, item, data_size[i]);
4891 }
4892
4893 btrfs_set_header_nritems(leaf, nritems + nr);
4894 btrfs_mark_buffer_dirty(leaf);
4895
4896 if (btrfs_leaf_free_space(leaf) < 0) {
4897 btrfs_print_leaf(leaf);
4898 BUG();
4899 }
4900 }
4901
4902 /*
4903 * Given a key and some data, insert items into the tree.
4904 * This does all the path init required, making room in the tree if needed.
4905 */
btrfs_insert_empty_items(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,const struct btrfs_key * cpu_key,u32 * data_size,int nr)4906 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4907 struct btrfs_root *root,
4908 struct btrfs_path *path,
4909 const struct btrfs_key *cpu_key, u32 *data_size,
4910 int nr)
4911 {
4912 int ret = 0;
4913 int slot;
4914 int i;
4915 u32 total_size = 0;
4916 u32 total_data = 0;
4917
4918 for (i = 0; i < nr; i++)
4919 total_data += data_size[i];
4920
4921 total_size = total_data + (nr * sizeof(struct btrfs_item));
4922 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4923 if (ret == 0)
4924 return -EEXIST;
4925 if (ret < 0)
4926 return ret;
4927
4928 slot = path->slots[0];
4929 BUG_ON(slot < 0);
4930
4931 setup_items_for_insert(root, path, cpu_key, data_size, nr);
4932 return 0;
4933 }
4934
4935 /*
4936 * Given a key and some data, insert an item into the tree.
4937 * This does all the path init required, making room in the tree if needed.
4938 */
btrfs_insert_item(struct btrfs_trans_handle * trans,struct btrfs_root * root,const struct btrfs_key * cpu_key,void * data,u32 data_size)4939 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4940 const struct btrfs_key *cpu_key, void *data,
4941 u32 data_size)
4942 {
4943 int ret = 0;
4944 struct btrfs_path *path;
4945 struct extent_buffer *leaf;
4946 unsigned long ptr;
4947
4948 path = btrfs_alloc_path();
4949 if (!path)
4950 return -ENOMEM;
4951 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
4952 if (!ret) {
4953 leaf = path->nodes[0];
4954 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4955 write_extent_buffer(leaf, data, ptr, data_size);
4956 btrfs_mark_buffer_dirty(leaf);
4957 }
4958 btrfs_free_path(path);
4959 return ret;
4960 }
4961
4962 /*
4963 * delete the pointer from a given node.
4964 *
4965 * the tree should have been previously balanced so the deletion does not
4966 * empty a node.
4967 */
del_ptr(struct btrfs_root * root,struct btrfs_path * path,int level,int slot)4968 static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4969 int level, int slot)
4970 {
4971 struct extent_buffer *parent = path->nodes[level];
4972 u32 nritems;
4973 int ret;
4974
4975 nritems = btrfs_header_nritems(parent);
4976 if (slot != nritems - 1) {
4977 if (level) {
4978 ret = tree_mod_log_insert_move(parent, slot, slot + 1,
4979 nritems - slot - 1);
4980 BUG_ON(ret < 0);
4981 }
4982 memmove_extent_buffer(parent,
4983 btrfs_node_key_ptr_offset(slot),
4984 btrfs_node_key_ptr_offset(slot + 1),
4985 sizeof(struct btrfs_key_ptr) *
4986 (nritems - slot - 1));
4987 } else if (level) {
4988 ret = tree_mod_log_insert_key(parent, slot, MOD_LOG_KEY_REMOVE,
4989 GFP_NOFS);
4990 BUG_ON(ret < 0);
4991 }
4992
4993 nritems--;
4994 btrfs_set_header_nritems(parent, nritems);
4995 if (nritems == 0 && parent == root->node) {
4996 BUG_ON(btrfs_header_level(root->node) != 1);
4997 /* just turn the root into a leaf and break */
4998 btrfs_set_header_level(root->node, 0);
4999 } else if (slot == 0) {
5000 struct btrfs_disk_key disk_key;
5001
5002 btrfs_node_key(parent, &disk_key, 0);
5003 fixup_low_keys(path, &disk_key, level + 1);
5004 }
5005 btrfs_mark_buffer_dirty(parent);
5006 }
5007
5008 /*
5009 * a helper function to delete the leaf pointed to by path->slots[1] and
5010 * path->nodes[1].
5011 *
5012 * This deletes the pointer in path->nodes[1] and frees the leaf
5013 * block extent. zero is returned if it all worked out, < 0 otherwise.
5014 *
5015 * The path must have already been setup for deleting the leaf, including
5016 * all the proper balancing. path->nodes[1] must be locked.
5017 */
btrfs_del_leaf(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,struct extent_buffer * leaf)5018 static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
5019 struct btrfs_root *root,
5020 struct btrfs_path *path,
5021 struct extent_buffer *leaf)
5022 {
5023 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
5024 del_ptr(root, path, 1, path->slots[1]);
5025
5026 /*
5027 * btrfs_free_extent is expensive, we want to make sure we
5028 * aren't holding any locks when we call it
5029 */
5030 btrfs_unlock_up_safe(path, 0);
5031
5032 root_sub_used(root, leaf->len);
5033
5034 atomic_inc(&leaf->refs);
5035 btrfs_free_tree_block(trans, root, leaf, 0, 1);
5036 free_extent_buffer_stale(leaf);
5037 }
5038 /*
5039 * delete the item at the leaf level in path. If that empties
5040 * the leaf, remove it from the tree
5041 */
btrfs_del_items(struct btrfs_trans_handle * trans,struct btrfs_root * root,struct btrfs_path * path,int slot,int nr)5042 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
5043 struct btrfs_path *path, int slot, int nr)
5044 {
5045 struct btrfs_fs_info *fs_info = root->fs_info;
5046 struct extent_buffer *leaf;
5047 struct btrfs_item *item;
5048 u32 last_off;
5049 u32 dsize = 0;
5050 int ret = 0;
5051 int wret;
5052 int i;
5053 u32 nritems;
5054
5055 leaf = path->nodes[0];
5056 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
5057
5058 for (i = 0; i < nr; i++)
5059 dsize += btrfs_item_size_nr(leaf, slot + i);
5060
5061 nritems = btrfs_header_nritems(leaf);
5062
5063 if (slot + nr != nritems) {
5064 int data_end = leaf_data_end(leaf);
5065 struct btrfs_map_token token;
5066
5067 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
5068 data_end + dsize,
5069 BTRFS_LEAF_DATA_OFFSET + data_end,
5070 last_off - data_end);
5071
5072 btrfs_init_map_token(&token, leaf);
5073 for (i = slot + nr; i < nritems; i++) {
5074 u32 ioff;
5075
5076 item = btrfs_item_nr(i);
5077 ioff = btrfs_token_item_offset(&token, item);
5078 btrfs_set_token_item_offset(&token, item, ioff + dsize);
5079 }
5080
5081 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
5082 btrfs_item_nr_offset(slot + nr),
5083 sizeof(struct btrfs_item) *
5084 (nritems - slot - nr));
5085 }
5086 btrfs_set_header_nritems(leaf, nritems - nr);
5087 nritems -= nr;
5088
5089 /* delete the leaf if we've emptied it */
5090 if (nritems == 0) {
5091 if (leaf == root->node) {
5092 btrfs_set_header_level(leaf, 0);
5093 } else {
5094 btrfs_set_path_blocking(path);
5095 btrfs_clean_tree_block(leaf);
5096 btrfs_del_leaf(trans, root, path, leaf);
5097 }
5098 } else {
5099 int used = leaf_space_used(leaf, 0, nritems);
5100 if (slot == 0) {
5101 struct btrfs_disk_key disk_key;
5102
5103 btrfs_item_key(leaf, &disk_key, 0);
5104 fixup_low_keys(path, &disk_key, 1);
5105 }
5106
5107 /* delete the leaf if it is mostly empty */
5108 if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) {
5109 /* push_leaf_left fixes the path.
5110 * make sure the path still points to our leaf
5111 * for possible call to del_ptr below
5112 */
5113 slot = path->slots[1];
5114 atomic_inc(&leaf->refs);
5115
5116 btrfs_set_path_blocking(path);
5117 wret = push_leaf_left(trans, root, path, 1, 1,
5118 1, (u32)-1);
5119 if (wret < 0 && wret != -ENOSPC)
5120 ret = wret;
5121
5122 if (path->nodes[0] == leaf &&
5123 btrfs_header_nritems(leaf)) {
5124 wret = push_leaf_right(trans, root, path, 1,
5125 1, 1, 0);
5126 if (wret < 0 && wret != -ENOSPC)
5127 ret = wret;
5128 }
5129
5130 if (btrfs_header_nritems(leaf) == 0) {
5131 path->slots[1] = slot;
5132 btrfs_del_leaf(trans, root, path, leaf);
5133 free_extent_buffer(leaf);
5134 ret = 0;
5135 } else {
5136 /* if we're still in the path, make sure
5137 * we're dirty. Otherwise, one of the
5138 * push_leaf functions must have already
5139 * dirtied this buffer
5140 */
5141 if (path->nodes[0] == leaf)
5142 btrfs_mark_buffer_dirty(leaf);
5143 free_extent_buffer(leaf);
5144 }
5145 } else {
5146 btrfs_mark_buffer_dirty(leaf);
5147 }
5148 }
5149 return ret;
5150 }
5151
5152 /*
5153 * search the tree again to find a leaf with lesser keys
5154 * returns 0 if it found something or 1 if there are no lesser leaves.
5155 * returns < 0 on io errors.
5156 *
5157 * This may release the path, and so you may lose any locks held at the
5158 * time you call it.
5159 */
btrfs_prev_leaf(struct btrfs_root * root,struct btrfs_path * path)5160 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
5161 {
5162 struct btrfs_key key;
5163 struct btrfs_disk_key found_key;
5164 int ret;
5165
5166 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
5167
5168 if (key.offset > 0) {
5169 key.offset--;
5170 } else if (key.type > 0) {
5171 key.type--;
5172 key.offset = (u64)-1;
5173 } else if (key.objectid > 0) {
5174 key.objectid--;
5175 key.type = (u8)-1;
5176 key.offset = (u64)-1;
5177 } else {
5178 return 1;
5179 }
5180
5181 btrfs_release_path(path);
5182 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5183 if (ret < 0)
5184 return ret;
5185 btrfs_item_key(path->nodes[0], &found_key, 0);
5186 ret = comp_keys(&found_key, &key);
5187 /*
5188 * We might have had an item with the previous key in the tree right
5189 * before we released our path. And after we released our path, that
5190 * item might have been pushed to the first slot (0) of the leaf we
5191 * were holding due to a tree balance. Alternatively, an item with the
5192 * previous key can exist as the only element of a leaf (big fat item).
5193 * Therefore account for these 2 cases, so that our callers (like
5194 * btrfs_previous_item) don't miss an existing item with a key matching
5195 * the previous key we computed above.
5196 */
5197 if (ret <= 0)
5198 return 0;
5199 return 1;
5200 }
5201
5202 /*
5203 * A helper function to walk down the tree starting at min_key, and looking
5204 * for nodes or leaves that are have a minimum transaction id.
5205 * This is used by the btree defrag code, and tree logging
5206 *
5207 * This does not cow, but it does stuff the starting key it finds back
5208 * into min_key, so you can call btrfs_search_slot with cow=1 on the
5209 * key and get a writable path.
5210 *
5211 * This honors path->lowest_level to prevent descent past a given level
5212 * of the tree.
5213 *
5214 * min_trans indicates the oldest transaction that you are interested
5215 * in walking through. Any nodes or leaves older than min_trans are
5216 * skipped over (without reading them).
5217 *
5218 * returns zero if something useful was found, < 0 on error and 1 if there
5219 * was nothing in the tree that matched the search criteria.
5220 */
btrfs_search_forward(struct btrfs_root * root,struct btrfs_key * min_key,struct btrfs_path * path,u64 min_trans)5221 int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
5222 struct btrfs_path *path,
5223 u64 min_trans)
5224 {
5225 struct extent_buffer *cur;
5226 struct btrfs_key found_key;
5227 int slot;
5228 int sret;
5229 u32 nritems;
5230 int level;
5231 int ret = 1;
5232 int keep_locks = path->keep_locks;
5233
5234 path->keep_locks = 1;
5235 again:
5236 cur = btrfs_read_lock_root_node(root);
5237 level = btrfs_header_level(cur);
5238 WARN_ON(path->nodes[level]);
5239 path->nodes[level] = cur;
5240 path->locks[level] = BTRFS_READ_LOCK;
5241
5242 if (btrfs_header_generation(cur) < min_trans) {
5243 ret = 1;
5244 goto out;
5245 }
5246 while (1) {
5247 nritems = btrfs_header_nritems(cur);
5248 level = btrfs_header_level(cur);
5249 sret = btrfs_bin_search(cur, min_key, &slot);
5250 if (sret < 0) {
5251 ret = sret;
5252 goto out;
5253 }
5254
5255 /* at the lowest level, we're done, setup the path and exit */
5256 if (level == path->lowest_level) {
5257 if (slot >= nritems)
5258 goto find_next_key;
5259 ret = 0;
5260 path->slots[level] = slot;
5261 btrfs_item_key_to_cpu(cur, &found_key, slot);
5262 goto out;
5263 }
5264 if (sret && slot > 0)
5265 slot--;
5266 /*
5267 * check this node pointer against the min_trans parameters.
5268 * If it is too old, skip to the next one.
5269 */
5270 while (slot < nritems) {
5271 u64 gen;
5272
5273 gen = btrfs_node_ptr_generation(cur, slot);
5274 if (gen < min_trans) {
5275 slot++;
5276 continue;
5277 }
5278 break;
5279 }
5280 find_next_key:
5281 /*
5282 * we didn't find a candidate key in this node, walk forward
5283 * and find another one
5284 */
5285 if (slot >= nritems) {
5286 path->slots[level] = slot;
5287 btrfs_set_path_blocking(path);
5288 sret = btrfs_find_next_key(root, path, min_key, level,
5289 min_trans);
5290 if (sret == 0) {
5291 btrfs_release_path(path);
5292 goto again;
5293 } else {
5294 goto out;
5295 }
5296 }
5297 /* save our key for returning back */
5298 btrfs_node_key_to_cpu(cur, &found_key, slot);
5299 path->slots[level] = slot;
5300 if (level == path->lowest_level) {
5301 ret = 0;
5302 goto out;
5303 }
5304 btrfs_set_path_blocking(path);
5305 cur = btrfs_read_node_slot(cur, slot);
5306 if (IS_ERR(cur)) {
5307 ret = PTR_ERR(cur);
5308 goto out;
5309 }
5310
5311 btrfs_tree_read_lock(cur);
5312
5313 path->locks[level - 1] = BTRFS_READ_LOCK;
5314 path->nodes[level - 1] = cur;
5315 unlock_up(path, level, 1, 0, NULL);
5316 }
5317 out:
5318 path->keep_locks = keep_locks;
5319 if (ret == 0) {
5320 btrfs_unlock_up_safe(path, path->lowest_level + 1);
5321 btrfs_set_path_blocking(path);
5322 memcpy(min_key, &found_key, sizeof(found_key));
5323 }
5324 return ret;
5325 }
5326
5327 /*
5328 * this is similar to btrfs_next_leaf, but does not try to preserve
5329 * and fixup the path. It looks for and returns the next key in the
5330 * tree based on the current path and the min_trans parameters.
5331 *
5332 * 0 is returned if another key is found, < 0 if there are any errors
5333 * and 1 is returned if there are no higher keys in the tree
5334 *
5335 * path->keep_locks should be set to 1 on the search made before
5336 * calling this function.
5337 */
btrfs_find_next_key(struct btrfs_root * root,struct btrfs_path * path,struct btrfs_key * key,int level,u64 min_trans)5338 int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
5339 struct btrfs_key *key, int level, u64 min_trans)
5340 {
5341 int slot;
5342 struct extent_buffer *c;
5343
5344 WARN_ON(!path->keep_locks && !path->skip_locking);
5345 while (level < BTRFS_MAX_LEVEL) {
5346 if (!path->nodes[level])
5347 return 1;
5348
5349 slot = path->slots[level] + 1;
5350 c = path->nodes[level];
5351 next:
5352 if (slot >= btrfs_header_nritems(c)) {
5353 int ret;
5354 int orig_lowest;
5355 struct btrfs_key cur_key;
5356 if (level + 1 >= BTRFS_MAX_LEVEL ||
5357 !path->nodes[level + 1])
5358 return 1;
5359
5360 if (path->locks[level + 1] || path->skip_locking) {
5361 level++;
5362 continue;
5363 }
5364
5365 slot = btrfs_header_nritems(c) - 1;
5366 if (level == 0)
5367 btrfs_item_key_to_cpu(c, &cur_key, slot);
5368 else
5369 btrfs_node_key_to_cpu(c, &cur_key, slot);
5370
5371 orig_lowest = path->lowest_level;
5372 btrfs_release_path(path);
5373 path->lowest_level = level;
5374 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5375 0, 0);
5376 path->lowest_level = orig_lowest;
5377 if (ret < 0)
5378 return ret;
5379
5380 c = path->nodes[level];
5381 slot = path->slots[level];
5382 if (ret == 0)
5383 slot++;
5384 goto next;
5385 }
5386
5387 if (level == 0)
5388 btrfs_item_key_to_cpu(c, key, slot);
5389 else {
5390 u64 gen = btrfs_node_ptr_generation(c, slot);
5391
5392 if (gen < min_trans) {
5393 slot++;
5394 goto next;
5395 }
5396 btrfs_node_key_to_cpu(c, key, slot);
5397 }
5398 return 0;
5399 }
5400 return 1;
5401 }
5402
5403 /*
5404 * search the tree again to find a leaf with greater keys
5405 * returns 0 if it found something or 1 if there are no greater leaves.
5406 * returns < 0 on io errors.
5407 */
btrfs_next_leaf(struct btrfs_root * root,struct btrfs_path * path)5408 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
5409 {
5410 return btrfs_next_old_leaf(root, path, 0);
5411 }
5412
btrfs_next_old_leaf(struct btrfs_root * root,struct btrfs_path * path,u64 time_seq)5413 int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5414 u64 time_seq)
5415 {
5416 int slot;
5417 int level;
5418 struct extent_buffer *c;
5419 struct extent_buffer *next;
5420 struct btrfs_key key;
5421 u32 nritems;
5422 int ret;
5423 int old_spinning = path->leave_spinning;
5424 int next_rw_lock = 0;
5425
5426 nritems = btrfs_header_nritems(path->nodes[0]);
5427 if (nritems == 0)
5428 return 1;
5429
5430 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5431 again:
5432 level = 1;
5433 next = NULL;
5434 next_rw_lock = 0;
5435 btrfs_release_path(path);
5436
5437 path->keep_locks = 1;
5438 path->leave_spinning = 1;
5439
5440 if (time_seq)
5441 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5442 else
5443 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5444 path->keep_locks = 0;
5445
5446 if (ret < 0)
5447 return ret;
5448
5449 nritems = btrfs_header_nritems(path->nodes[0]);
5450 /*
5451 * by releasing the path above we dropped all our locks. A balance
5452 * could have added more items next to the key that used to be
5453 * at the very end of the block. So, check again here and
5454 * advance the path if there are now more items available.
5455 */
5456 if (nritems > 0 && path->slots[0] < nritems - 1) {
5457 if (ret == 0)
5458 path->slots[0]++;
5459 ret = 0;
5460 goto done;
5461 }
5462 /*
5463 * So the above check misses one case:
5464 * - after releasing the path above, someone has removed the item that
5465 * used to be at the very end of the block, and balance between leafs
5466 * gets another one with bigger key.offset to replace it.
5467 *
5468 * This one should be returned as well, or we can get leaf corruption
5469 * later(esp. in __btrfs_drop_extents()).
5470 *
5471 * And a bit more explanation about this check,
5472 * with ret > 0, the key isn't found, the path points to the slot
5473 * where it should be inserted, so the path->slots[0] item must be the
5474 * bigger one.
5475 */
5476 if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
5477 ret = 0;
5478 goto done;
5479 }
5480
5481 while (level < BTRFS_MAX_LEVEL) {
5482 if (!path->nodes[level]) {
5483 ret = 1;
5484 goto done;
5485 }
5486
5487 slot = path->slots[level] + 1;
5488 c = path->nodes[level];
5489 if (slot >= btrfs_header_nritems(c)) {
5490 level++;
5491 if (level == BTRFS_MAX_LEVEL) {
5492 ret = 1;
5493 goto done;
5494 }
5495 continue;
5496 }
5497
5498 if (next) {
5499 btrfs_tree_unlock_rw(next, next_rw_lock);
5500 free_extent_buffer(next);
5501 }
5502
5503 next = c;
5504 next_rw_lock = path->locks[level];
5505 ret = read_block_for_search(root, path, &next, level,
5506 slot, &key);
5507 if (ret == -EAGAIN)
5508 goto again;
5509
5510 if (ret < 0) {
5511 btrfs_release_path(path);
5512 goto done;
5513 }
5514
5515 if (!path->skip_locking) {
5516 ret = btrfs_try_tree_read_lock(next);
5517 if (!ret && time_seq) {
5518 /*
5519 * If we don't get the lock, we may be racing
5520 * with push_leaf_left, holding that lock while
5521 * itself waiting for the leaf we've currently
5522 * locked. To solve this situation, we give up
5523 * on our lock and cycle.
5524 */
5525 free_extent_buffer(next);
5526 btrfs_release_path(path);
5527 cond_resched();
5528 goto again;
5529 }
5530 if (!ret) {
5531 btrfs_set_path_blocking(path);
5532 __btrfs_tree_read_lock(next,
5533 BTRFS_NESTING_RIGHT,
5534 path->recurse);
5535 }
5536 next_rw_lock = BTRFS_READ_LOCK;
5537 }
5538 break;
5539 }
5540 path->slots[level] = slot;
5541 while (1) {
5542 level--;
5543 c = path->nodes[level];
5544 if (path->locks[level])
5545 btrfs_tree_unlock_rw(c, path->locks[level]);
5546
5547 free_extent_buffer(c);
5548 path->nodes[level] = next;
5549 path->slots[level] = 0;
5550 if (!path->skip_locking)
5551 path->locks[level] = next_rw_lock;
5552 if (!level)
5553 break;
5554
5555 ret = read_block_for_search(root, path, &next, level,
5556 0, &key);
5557 if (ret == -EAGAIN)
5558 goto again;
5559
5560 if (ret < 0) {
5561 btrfs_release_path(path);
5562 goto done;
5563 }
5564
5565 if (!path->skip_locking) {
5566 ret = btrfs_try_tree_read_lock(next);
5567 if (!ret) {
5568 btrfs_set_path_blocking(path);
5569 __btrfs_tree_read_lock(next,
5570 BTRFS_NESTING_RIGHT,
5571 path->recurse);
5572 }
5573 next_rw_lock = BTRFS_READ_LOCK;
5574 }
5575 }
5576 ret = 0;
5577 done:
5578 unlock_up(path, 0, 1, 0, NULL);
5579 path->leave_spinning = old_spinning;
5580 if (!old_spinning)
5581 btrfs_set_path_blocking(path);
5582
5583 return ret;
5584 }
5585
5586 /*
5587 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5588 * searching until it gets past min_objectid or finds an item of 'type'
5589 *
5590 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5591 */
btrfs_previous_item(struct btrfs_root * root,struct btrfs_path * path,u64 min_objectid,int type)5592 int btrfs_previous_item(struct btrfs_root *root,
5593 struct btrfs_path *path, u64 min_objectid,
5594 int type)
5595 {
5596 struct btrfs_key found_key;
5597 struct extent_buffer *leaf;
5598 u32 nritems;
5599 int ret;
5600
5601 while (1) {
5602 if (path->slots[0] == 0) {
5603 btrfs_set_path_blocking(path);
5604 ret = btrfs_prev_leaf(root, path);
5605 if (ret != 0)
5606 return ret;
5607 } else {
5608 path->slots[0]--;
5609 }
5610 leaf = path->nodes[0];
5611 nritems = btrfs_header_nritems(leaf);
5612 if (nritems == 0)
5613 return 1;
5614 if (path->slots[0] == nritems)
5615 path->slots[0]--;
5616
5617 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5618 if (found_key.objectid < min_objectid)
5619 break;
5620 if (found_key.type == type)
5621 return 0;
5622 if (found_key.objectid == min_objectid &&
5623 found_key.type < type)
5624 break;
5625 }
5626 return 1;
5627 }
5628
5629 /*
5630 * search in extent tree to find a previous Metadata/Data extent item with
5631 * min objecitd.
5632 *
5633 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5634 */
btrfs_previous_extent_item(struct btrfs_root * root,struct btrfs_path * path,u64 min_objectid)5635 int btrfs_previous_extent_item(struct btrfs_root *root,
5636 struct btrfs_path *path, u64 min_objectid)
5637 {
5638 struct btrfs_key found_key;
5639 struct extent_buffer *leaf;
5640 u32 nritems;
5641 int ret;
5642
5643 while (1) {
5644 if (path->slots[0] == 0) {
5645 btrfs_set_path_blocking(path);
5646 ret = btrfs_prev_leaf(root, path);
5647 if (ret != 0)
5648 return ret;
5649 } else {
5650 path->slots[0]--;
5651 }
5652 leaf = path->nodes[0];
5653 nritems = btrfs_header_nritems(leaf);
5654 if (nritems == 0)
5655 return 1;
5656 if (path->slots[0] == nritems)
5657 path->slots[0]--;
5658
5659 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5660 if (found_key.objectid < min_objectid)
5661 break;
5662 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
5663 found_key.type == BTRFS_METADATA_ITEM_KEY)
5664 return 0;
5665 if (found_key.objectid == min_objectid &&
5666 found_key.type < BTRFS_EXTENT_ITEM_KEY)
5667 break;
5668 }
5669 return 1;
5670 }
5671