aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/extent-tree.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/btrfs/extent-tree.c')
-rw-r--r--fs/btrfs/extent-tree.c5990
1 files changed, 5990 insertions, 0 deletions
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
new file mode 100644
index 000000000000..ec43fa526d77
--- /dev/null
+++ b/fs/btrfs/extent-tree.c
@@ -0,0 +1,5990 @@
1/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18#include <linux/sched.h>
19#include <linux/pagemap.h>
20#include <linux/writeback.h>
21#include <linux/blkdev.h>
22#include <linux/version.h>
23#include "compat.h"
24#include "hash.h"
25#include "crc32c.h"
26#include "ctree.h"
27#include "disk-io.h"
28#include "print-tree.h"
29#include "transaction.h"
30#include "volumes.h"
31#include "locking.h"
32#include "ref-cache.h"
33#include "compat.h"
34
35#define PENDING_EXTENT_INSERT 0
36#define PENDING_EXTENT_DELETE 1
37#define PENDING_BACKREF_UPDATE 2
38
39struct pending_extent_op {
40 int type;
41 u64 bytenr;
42 u64 num_bytes;
43 u64 parent;
44 u64 orig_parent;
45 u64 generation;
46 u64 orig_generation;
47 int level;
48 struct list_head list;
49 int del;
50};
51
52static int finish_current_insert(struct btrfs_trans_handle *trans,
53 struct btrfs_root *extent_root, int all);
54static int del_pending_extents(struct btrfs_trans_handle *trans,
55 struct btrfs_root *extent_root, int all);
56static int pin_down_bytes(struct btrfs_trans_handle *trans,
57 struct btrfs_root *root,
58 u64 bytenr, u64 num_bytes, int is_data);
59static int update_block_group(struct btrfs_trans_handle *trans,
60 struct btrfs_root *root,
61 u64 bytenr, u64 num_bytes, int alloc,
62 int mark_free);
63
64static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
65{
66 return (cache->flags & bits) == bits;
67}
68
69/*
70 * this adds the block group to the fs_info rb tree for the block group
71 * cache
72 */
73static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
74 struct btrfs_block_group_cache *block_group)
75{
76 struct rb_node **p;
77 struct rb_node *parent = NULL;
78 struct btrfs_block_group_cache *cache;
79
80 spin_lock(&info->block_group_cache_lock);
81 p = &info->block_group_cache_tree.rb_node;
82
83 while (*p) {
84 parent = *p;
85 cache = rb_entry(parent, struct btrfs_block_group_cache,
86 cache_node);
87 if (block_group->key.objectid < cache->key.objectid) {
88 p = &(*p)->rb_left;
89 } else if (block_group->key.objectid > cache->key.objectid) {
90 p = &(*p)->rb_right;
91 } else {
92 spin_unlock(&info->block_group_cache_lock);
93 return -EEXIST;
94 }
95 }
96
97 rb_link_node(&block_group->cache_node, parent, p);
98 rb_insert_color(&block_group->cache_node,
99 &info->block_group_cache_tree);
100 spin_unlock(&info->block_group_cache_lock);
101
102 return 0;
103}
104
105/*
106 * This will return the block group at or after bytenr if contains is 0, else
107 * it will return the block group that contains the bytenr
108 */
109static struct btrfs_block_group_cache *
110block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
111 int contains)
112{
113 struct btrfs_block_group_cache *cache, *ret = NULL;
114 struct rb_node *n;
115 u64 end, start;
116
117 spin_lock(&info->block_group_cache_lock);
118 n = info->block_group_cache_tree.rb_node;
119
120 while (n) {
121 cache = rb_entry(n, struct btrfs_block_group_cache,
122 cache_node);
123 end = cache->key.objectid + cache->key.offset - 1;
124 start = cache->key.objectid;
125
126 if (bytenr < start) {
127 if (!contains && (!ret || start < ret->key.objectid))
128 ret = cache;
129 n = n->rb_left;
130 } else if (bytenr > start) {
131 if (contains && bytenr <= end) {
132 ret = cache;
133 break;
134 }
135 n = n->rb_right;
136 } else {
137 ret = cache;
138 break;
139 }
140 }
141 if (ret)
142 atomic_inc(&ret->count);
143 spin_unlock(&info->block_group_cache_lock);
144
145 return ret;
146}
147
148/*
149 * this is only called by cache_block_group, since we could have freed extents
150 * we need to check the pinned_extents for any extents that can't be used yet
151 * since their free space will be released as soon as the transaction commits.
152 */
153static int add_new_free_space(struct btrfs_block_group_cache *block_group,
154 struct btrfs_fs_info *info, u64 start, u64 end)
155{
156 u64 extent_start, extent_end, size;
157 int ret;
158
159 mutex_lock(&info->pinned_mutex);
160 while (start < end) {
161 ret = find_first_extent_bit(&info->pinned_extents, start,
162 &extent_start, &extent_end,
163 EXTENT_DIRTY);
164 if (ret)
165 break;
166
167 if (extent_start == start) {
168 start = extent_end + 1;
169 } else if (extent_start > start && extent_start < end) {
170 size = extent_start - start;
171 ret = btrfs_add_free_space(block_group, start,
172 size);
173 BUG_ON(ret);
174 start = extent_end + 1;
175 } else {
176 break;
177 }
178 }
179
180 if (start < end) {
181 size = end - start;
182 ret = btrfs_add_free_space(block_group, start, size);
183 BUG_ON(ret);
184 }
185 mutex_unlock(&info->pinned_mutex);
186
187 return 0;
188}
189
190static int remove_sb_from_cache(struct btrfs_root *root,
191 struct btrfs_block_group_cache *cache)
192{
193 u64 bytenr;
194 u64 *logical;
195 int stripe_len;
196 int i, nr, ret;
197
198 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
199 bytenr = btrfs_sb_offset(i);
200 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
201 cache->key.objectid, bytenr, 0,
202 &logical, &nr, &stripe_len);
203 BUG_ON(ret);
204 while (nr--) {
205 btrfs_remove_free_space(cache, logical[nr],
206 stripe_len);
207 }
208 kfree(logical);
209 }
210 return 0;
211}
212
213static int cache_block_group(struct btrfs_root *root,
214 struct btrfs_block_group_cache *block_group)
215{
216 struct btrfs_path *path;
217 int ret = 0;
218 struct btrfs_key key;
219 struct extent_buffer *leaf;
220 int slot;
221 u64 last;
222
223 if (!block_group)
224 return 0;
225
226 root = root->fs_info->extent_root;
227
228 if (block_group->cached)
229 return 0;
230
231 path = btrfs_alloc_path();
232 if (!path)
233 return -ENOMEM;
234
235 path->reada = 2;
236 /*
237 * we get into deadlocks with paths held by callers of this function.
238 * since the alloc_mutex is protecting things right now, just
239 * skip the locking here
240 */
241 path->skip_locking = 1;
242 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
243 key.objectid = last;
244 key.offset = 0;
245 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
246 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
247 if (ret < 0)
248 goto err;
249
250 while (1) {
251 leaf = path->nodes[0];
252 slot = path->slots[0];
253 if (slot >= btrfs_header_nritems(leaf)) {
254 ret = btrfs_next_leaf(root, path);
255 if (ret < 0)
256 goto err;
257 if (ret == 0)
258 continue;
259 else
260 break;
261 }
262 btrfs_item_key_to_cpu(leaf, &key, slot);
263 if (key.objectid < block_group->key.objectid)
264 goto next;
265
266 if (key.objectid >= block_group->key.objectid +
267 block_group->key.offset)
268 break;
269
270 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
271 add_new_free_space(block_group, root->fs_info, last,
272 key.objectid);
273
274 last = key.objectid + key.offset;
275 }
276next:
277 path->slots[0]++;
278 }
279
280 add_new_free_space(block_group, root->fs_info, last,
281 block_group->key.objectid +
282 block_group->key.offset);
283
284 remove_sb_from_cache(root, block_group);
285 block_group->cached = 1;
286 ret = 0;
287err:
288 btrfs_free_path(path);
289 return ret;
290}
291
292/*
293 * return the block group that starts at or after bytenr
294 */
295static struct btrfs_block_group_cache *
296btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
297{
298 struct btrfs_block_group_cache *cache;
299
300 cache = block_group_cache_tree_search(info, bytenr, 0);
301
302 return cache;
303}
304
305/*
306 * return the block group that contains teh given bytenr
307 */
308struct btrfs_block_group_cache *btrfs_lookup_block_group(
309 struct btrfs_fs_info *info,
310 u64 bytenr)
311{
312 struct btrfs_block_group_cache *cache;
313
314 cache = block_group_cache_tree_search(info, bytenr, 1);
315
316 return cache;
317}
318
319static inline void put_block_group(struct btrfs_block_group_cache *cache)
320{
321 if (atomic_dec_and_test(&cache->count))
322 kfree(cache);
323}
324
325static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
326 u64 flags)
327{
328 struct list_head *head = &info->space_info;
329 struct list_head *cur;
330 struct btrfs_space_info *found;
331 list_for_each(cur, head) {
332 found = list_entry(cur, struct btrfs_space_info, list);
333 if (found->flags == flags)
334 return found;
335 }
336 return NULL;
337}
338
339static u64 div_factor(u64 num, int factor)
340{
341 if (factor == 10)
342 return num;
343 num *= factor;
344 do_div(num, 10);
345 return num;
346}
347
348u64 btrfs_find_block_group(struct btrfs_root *root,
349 u64 search_start, u64 search_hint, int owner)
350{
351 struct btrfs_block_group_cache *cache;
352 u64 used;
353 u64 last = max(search_hint, search_start);
354 u64 group_start = 0;
355 int full_search = 0;
356 int factor = 9;
357 int wrapped = 0;
358again:
359 while (1) {
360 cache = btrfs_lookup_first_block_group(root->fs_info, last);
361 if (!cache)
362 break;
363
364 spin_lock(&cache->lock);
365 last = cache->key.objectid + cache->key.offset;
366 used = btrfs_block_group_used(&cache->item);
367
368 if ((full_search || !cache->ro) &&
369 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
370 if (used + cache->pinned + cache->reserved <
371 div_factor(cache->key.offset, factor)) {
372 group_start = cache->key.objectid;
373 spin_unlock(&cache->lock);
374 put_block_group(cache);
375 goto found;
376 }
377 }
378 spin_unlock(&cache->lock);
379 put_block_group(cache);
380 cond_resched();
381 }
382 if (!wrapped) {
383 last = search_start;
384 wrapped = 1;
385 goto again;
386 }
387 if (!full_search && factor < 10) {
388 last = search_start;
389 full_search = 1;
390 factor = 10;
391 goto again;
392 }
393found:
394 return group_start;
395}
396
397/* simple helper to search for an existing extent at a given offset */
398int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
399{
400 int ret;
401 struct btrfs_key key;
402 struct btrfs_path *path;
403
404 path = btrfs_alloc_path();
405 BUG_ON(!path);
406 key.objectid = start;
407 key.offset = len;
408 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
409 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
410 0, 0);
411 btrfs_free_path(path);
412 return ret;
413}
414
415/*
416 * Back reference rules. Back refs have three main goals:
417 *
418 * 1) differentiate between all holders of references to an extent so that
419 * when a reference is dropped we can make sure it was a valid reference
420 * before freeing the extent.
421 *
422 * 2) Provide enough information to quickly find the holders of an extent
423 * if we notice a given block is corrupted or bad.
424 *
425 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
426 * maintenance. This is actually the same as #2, but with a slightly
427 * different use case.
428 *
429 * File extents can be referenced by:
430 *
431 * - multiple snapshots, subvolumes, or different generations in one subvol
432 * - different files inside a single subvolume
433 * - different offsets inside a file (bookend extents in file.c)
434 *
435 * The extent ref structure has fields for:
436 *
437 * - Objectid of the subvolume root
438 * - Generation number of the tree holding the reference
439 * - objectid of the file holding the reference
440 * - number of references holding by parent node (alway 1 for tree blocks)
441 *
442 * Btree leaf may hold multiple references to a file extent. In most cases,
443 * these references are from same file and the corresponding offsets inside
444 * the file are close together.
445 *
446 * When a file extent is allocated the fields are filled in:
447 * (root_key.objectid, trans->transid, inode objectid, 1)
448 *
449 * When a leaf is cow'd new references are added for every file extent found
450 * in the leaf. It looks similar to the create case, but trans->transid will
451 * be different when the block is cow'd.
452 *
453 * (root_key.objectid, trans->transid, inode objectid,
454 * number of references in the leaf)
455 *
456 * When a file extent is removed either during snapshot deletion or
457 * file truncation, we find the corresponding back reference and check
458 * the following fields:
459 *
460 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
461 * inode objectid)
462 *
463 * Btree extents can be referenced by:
464 *
465 * - Different subvolumes
466 * - Different generations of the same subvolume
467 *
468 * When a tree block is created, back references are inserted:
469 *
470 * (root->root_key.objectid, trans->transid, level, 1)
471 *
472 * When a tree block is cow'd, new back references are added for all the
473 * blocks it points to. If the tree block isn't in reference counted root,
474 * the old back references are removed. These new back references are of
475 * the form (trans->transid will have increased since creation):
476 *
477 * (root->root_key.objectid, trans->transid, level, 1)
478 *
479 * When a backref is in deleting, the following fields are checked:
480 *
481 * if backref was for a tree root:
482 * (btrfs_header_owner(itself), btrfs_header_generation(itself), level)
483 * else
484 * (btrfs_header_owner(parent), btrfs_header_generation(parent), level)
485 *
486 * Back Reference Key composing:
487 *
488 * The key objectid corresponds to the first byte in the extent, the key
489 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
490 * byte of parent extent. If a extent is tree root, the key offset is set
491 * to the key objectid.
492 */
493
494static noinline int lookup_extent_backref(struct btrfs_trans_handle *trans,
495 struct btrfs_root *root,
496 struct btrfs_path *path,
497 u64 bytenr, u64 parent,
498 u64 ref_root, u64 ref_generation,
499 u64 owner_objectid, int del)
500{
501 struct btrfs_key key;
502 struct btrfs_extent_ref *ref;
503 struct extent_buffer *leaf;
504 u64 ref_objectid;
505 int ret;
506
507 key.objectid = bytenr;
508 key.type = BTRFS_EXTENT_REF_KEY;
509 key.offset = parent;
510
511 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
512 if (ret < 0)
513 goto out;
514 if (ret > 0) {
515 ret = -ENOENT;
516 goto out;
517 }
518
519 leaf = path->nodes[0];
520 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
521 ref_objectid = btrfs_ref_objectid(leaf, ref);
522 if (btrfs_ref_root(leaf, ref) != ref_root ||
523 btrfs_ref_generation(leaf, ref) != ref_generation ||
524 (ref_objectid != owner_objectid &&
525 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
526 ret = -EIO;
527 WARN_ON(1);
528 goto out;
529 }
530 ret = 0;
531out:
532 return ret;
533}
534
535/*
536 * updates all the backrefs that are pending on update_list for the
537 * extent_root
538 */
539static noinline int update_backrefs(struct btrfs_trans_handle *trans,
540 struct btrfs_root *extent_root,
541 struct btrfs_path *path,
542 struct list_head *update_list)
543{
544 struct btrfs_key key;
545 struct btrfs_extent_ref *ref;
546 struct btrfs_fs_info *info = extent_root->fs_info;
547 struct pending_extent_op *op;
548 struct extent_buffer *leaf;
549 int ret = 0;
550 struct list_head *cur = update_list->next;
551 u64 ref_objectid;
552 u64 ref_root = extent_root->root_key.objectid;
553
554 op = list_entry(cur, struct pending_extent_op, list);
555
556search:
557 key.objectid = op->bytenr;
558 key.type = BTRFS_EXTENT_REF_KEY;
559 key.offset = op->orig_parent;
560
561 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 1);
562 BUG_ON(ret);
563
564 leaf = path->nodes[0];
565
566loop:
567 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
568
569 ref_objectid = btrfs_ref_objectid(leaf, ref);
570
571 if (btrfs_ref_root(leaf, ref) != ref_root ||
572 btrfs_ref_generation(leaf, ref) != op->orig_generation ||
573 (ref_objectid != op->level &&
574 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
575 printk(KERN_ERR "btrfs couldn't find %llu, parent %llu, "
576 "root %llu, owner %u\n",
577 (unsigned long long)op->bytenr,
578 (unsigned long long)op->orig_parent,
579 (unsigned long long)ref_root, op->level);
580 btrfs_print_leaf(extent_root, leaf);
581 BUG();
582 }
583
584 key.objectid = op->bytenr;
585 key.offset = op->parent;
586 key.type = BTRFS_EXTENT_REF_KEY;
587 ret = btrfs_set_item_key_safe(trans, extent_root, path, &key);
588 BUG_ON(ret);
589 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
590 btrfs_set_ref_generation(leaf, ref, op->generation);
591
592 cur = cur->next;
593
594 list_del_init(&op->list);
595 unlock_extent(&info->extent_ins, op->bytenr,
596 op->bytenr + op->num_bytes - 1, GFP_NOFS);
597 kfree(op);
598
599 if (cur == update_list) {
600 btrfs_mark_buffer_dirty(path->nodes[0]);
601 btrfs_release_path(extent_root, path);
602 goto out;
603 }
604
605 op = list_entry(cur, struct pending_extent_op, list);
606
607 path->slots[0]++;
608 while (path->slots[0] < btrfs_header_nritems(leaf)) {
609 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
610 if (key.objectid == op->bytenr &&
611 key.type == BTRFS_EXTENT_REF_KEY)
612 goto loop;
613 path->slots[0]++;
614 }
615
616 btrfs_mark_buffer_dirty(path->nodes[0]);
617 btrfs_release_path(extent_root, path);
618 goto search;
619
620out:
621 return 0;
622}
623
624static noinline int insert_extents(struct btrfs_trans_handle *trans,
625 struct btrfs_root *extent_root,
626 struct btrfs_path *path,
627 struct list_head *insert_list, int nr)
628{
629 struct btrfs_key *keys;
630 u32 *data_size;
631 struct pending_extent_op *op;
632 struct extent_buffer *leaf;
633 struct list_head *cur = insert_list->next;
634 struct btrfs_fs_info *info = extent_root->fs_info;
635 u64 ref_root = extent_root->root_key.objectid;
636 int i = 0, last = 0, ret;
637 int total = nr * 2;
638
639 if (!nr)
640 return 0;
641
642 keys = kzalloc(total * sizeof(struct btrfs_key), GFP_NOFS);
643 if (!keys)
644 return -ENOMEM;
645
646 data_size = kzalloc(total * sizeof(u32), GFP_NOFS);
647 if (!data_size) {
648 kfree(keys);
649 return -ENOMEM;
650 }
651
652 list_for_each_entry(op, insert_list, list) {
653 keys[i].objectid = op->bytenr;
654 keys[i].offset = op->num_bytes;
655 keys[i].type = BTRFS_EXTENT_ITEM_KEY;
656 data_size[i] = sizeof(struct btrfs_extent_item);
657 i++;
658
659 keys[i].objectid = op->bytenr;
660 keys[i].offset = op->parent;
661 keys[i].type = BTRFS_EXTENT_REF_KEY;
662 data_size[i] = sizeof(struct btrfs_extent_ref);
663 i++;
664 }
665
666 op = list_entry(cur, struct pending_extent_op, list);
667 i = 0;
668 while (i < total) {
669 int c;
670 ret = btrfs_insert_some_items(trans, extent_root, path,
671 keys+i, data_size+i, total-i);
672 BUG_ON(ret < 0);
673
674 if (last && ret > 1)
675 BUG();
676
677 leaf = path->nodes[0];
678 for (c = 0; c < ret; c++) {
679 int ref_first = keys[i].type == BTRFS_EXTENT_REF_KEY;
680
681 /*
682 * if the first item we inserted was a backref, then
683 * the EXTENT_ITEM will be the odd c's, else it will
684 * be the even c's
685 */
686 if ((ref_first && (c % 2)) ||
687 (!ref_first && !(c % 2))) {
688 struct btrfs_extent_item *itm;
689
690 itm = btrfs_item_ptr(leaf, path->slots[0] + c,
691 struct btrfs_extent_item);
692 btrfs_set_extent_refs(path->nodes[0], itm, 1);
693 op->del++;
694 } else {
695 struct btrfs_extent_ref *ref;
696
697 ref = btrfs_item_ptr(leaf, path->slots[0] + c,
698 struct btrfs_extent_ref);
699 btrfs_set_ref_root(leaf, ref, ref_root);
700 btrfs_set_ref_generation(leaf, ref,
701 op->generation);
702 btrfs_set_ref_objectid(leaf, ref, op->level);
703 btrfs_set_ref_num_refs(leaf, ref, 1);
704 op->del++;
705 }
706
707 /*
708 * using del to see when its ok to free up the
709 * pending_extent_op. In the case where we insert the
710 * last item on the list in order to help do batching
711 * we need to not free the extent op until we actually
712 * insert the extent_item
713 */
714 if (op->del == 2) {
715 unlock_extent(&info->extent_ins, op->bytenr,
716 op->bytenr + op->num_bytes - 1,
717 GFP_NOFS);
718 cur = cur->next;
719 list_del_init(&op->list);
720 kfree(op);
721 if (cur != insert_list)
722 op = list_entry(cur,
723 struct pending_extent_op,
724 list);
725 }
726 }
727 btrfs_mark_buffer_dirty(leaf);
728 btrfs_release_path(extent_root, path);
729
730 /*
731 * Ok backref's and items usually go right next to eachother,
732 * but if we could only insert 1 item that means that we
733 * inserted on the end of a leaf, and we have no idea what may
734 * be on the next leaf so we just play it safe. In order to
735 * try and help this case we insert the last thing on our
736 * insert list so hopefully it will end up being the last
737 * thing on the leaf and everything else will be before it,
738 * which will let us insert a whole bunch of items at the same
739 * time.
740 */
741 if (ret == 1 && !last && (i + ret < total)) {
742 /*
743 * last: where we will pick up the next time around
744 * i: our current key to insert, will be total - 1
745 * cur: the current op we are screwing with
746 * op: duh
747 */
748 last = i + ret;
749 i = total - 1;
750 cur = insert_list->prev;
751 op = list_entry(cur, struct pending_extent_op, list);
752 } else if (last) {
753 /*
754 * ok we successfully inserted the last item on the
755 * list, lets reset everything
756 *
757 * i: our current key to insert, so where we left off
758 * last time
759 * last: done with this
760 * cur: the op we are messing with
761 * op: duh
762 * total: since we inserted the last key, we need to
763 * decrement total so we dont overflow
764 */
765 i = last;
766 last = 0;
767 total--;
768 if (i < total) {
769 cur = insert_list->next;
770 op = list_entry(cur, struct pending_extent_op,
771 list);
772 }
773 } else {
774 i += ret;
775 }
776
777 cond_resched();
778 }
779 ret = 0;
780 kfree(keys);
781 kfree(data_size);
782 return ret;
783}
784
785static noinline int insert_extent_backref(struct btrfs_trans_handle *trans,
786 struct btrfs_root *root,
787 struct btrfs_path *path,
788 u64 bytenr, u64 parent,
789 u64 ref_root, u64 ref_generation,
790 u64 owner_objectid)
791{
792 struct btrfs_key key;
793 struct extent_buffer *leaf;
794 struct btrfs_extent_ref *ref;
795 u32 num_refs;
796 int ret;
797
798 key.objectid = bytenr;
799 key.type = BTRFS_EXTENT_REF_KEY;
800 key.offset = parent;
801
802 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
803 if (ret == 0) {
804 leaf = path->nodes[0];
805 ref = btrfs_item_ptr(leaf, path->slots[0],
806 struct btrfs_extent_ref);
807 btrfs_set_ref_root(leaf, ref, ref_root);
808 btrfs_set_ref_generation(leaf, ref, ref_generation);
809 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
810 btrfs_set_ref_num_refs(leaf, ref, 1);
811 } else if (ret == -EEXIST) {
812 u64 existing_owner;
813 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
814 leaf = path->nodes[0];
815 ref = btrfs_item_ptr(leaf, path->slots[0],
816 struct btrfs_extent_ref);
817 if (btrfs_ref_root(leaf, ref) != ref_root ||
818 btrfs_ref_generation(leaf, ref) != ref_generation) {
819 ret = -EIO;
820 WARN_ON(1);
821 goto out;
822 }
823
824 num_refs = btrfs_ref_num_refs(leaf, ref);
825 BUG_ON(num_refs == 0);
826 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
827
828 existing_owner = btrfs_ref_objectid(leaf, ref);
829 if (existing_owner != owner_objectid &&
830 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
831 btrfs_set_ref_objectid(leaf, ref,
832 BTRFS_MULTIPLE_OBJECTIDS);
833 }
834 ret = 0;
835 } else {
836 goto out;
837 }
838 btrfs_mark_buffer_dirty(path->nodes[0]);
839out:
840 btrfs_release_path(root, path);
841 return ret;
842}
843
844static noinline int remove_extent_backref(struct btrfs_trans_handle *trans,
845 struct btrfs_root *root,
846 struct btrfs_path *path)
847{
848 struct extent_buffer *leaf;
849 struct btrfs_extent_ref *ref;
850 u32 num_refs;
851 int ret = 0;
852
853 leaf = path->nodes[0];
854 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
855 num_refs = btrfs_ref_num_refs(leaf, ref);
856 BUG_ON(num_refs == 0);
857 num_refs -= 1;
858 if (num_refs == 0) {
859 ret = btrfs_del_item(trans, root, path);
860 } else {
861 btrfs_set_ref_num_refs(leaf, ref, num_refs);
862 btrfs_mark_buffer_dirty(leaf);
863 }
864 btrfs_release_path(root, path);
865 return ret;
866}
867
868#ifdef BIO_RW_DISCARD
869static void btrfs_issue_discard(struct block_device *bdev,
870 u64 start, u64 len)
871{
872#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
873 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
874#else
875 blkdev_issue_discard(bdev, start >> 9, len >> 9);
876#endif
877}
878#endif
879
880static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
881 u64 num_bytes)
882{
883#ifdef BIO_RW_DISCARD
884 int ret;
885 u64 map_length = num_bytes;
886 struct btrfs_multi_bio *multi = NULL;
887
888 /* Tell the block device(s) that the sectors can be discarded */
889 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
890 bytenr, &map_length, &multi, 0);
891 if (!ret) {
892 struct btrfs_bio_stripe *stripe = multi->stripes;
893 int i;
894
895 if (map_length > num_bytes)
896 map_length = num_bytes;
897
898 for (i = 0; i < multi->num_stripes; i++, stripe++) {
899 btrfs_issue_discard(stripe->dev->bdev,
900 stripe->physical,
901 map_length);
902 }
903 kfree(multi);
904 }
905
906 return ret;
907#else
908 return 0;
909#endif
910}
911
912static noinline int free_extents(struct btrfs_trans_handle *trans,
913 struct btrfs_root *extent_root,
914 struct list_head *del_list)
915{
916 struct btrfs_fs_info *info = extent_root->fs_info;
917 struct btrfs_path *path;
918 struct btrfs_key key, found_key;
919 struct extent_buffer *leaf;
920 struct list_head *cur;
921 struct pending_extent_op *op;
922 struct btrfs_extent_item *ei;
923 int ret, num_to_del, extent_slot = 0, found_extent = 0;
924 u32 refs;
925 u64 bytes_freed = 0;
926
927 path = btrfs_alloc_path();
928 if (!path)
929 return -ENOMEM;
930 path->reada = 1;
931
932search:
933 /* search for the backref for the current ref we want to delete */
934 cur = del_list->next;
935 op = list_entry(cur, struct pending_extent_op, list);
936 ret = lookup_extent_backref(trans, extent_root, path, op->bytenr,
937 op->orig_parent,
938 extent_root->root_key.objectid,
939 op->orig_generation, op->level, 1);
940 if (ret) {
941 printk(KERN_ERR "btrfs unable to find backref byte nr %llu "
942 "root %llu gen %llu owner %u\n",
943 (unsigned long long)op->bytenr,
944 (unsigned long long)extent_root->root_key.objectid,
945 (unsigned long long)op->orig_generation, op->level);
946 btrfs_print_leaf(extent_root, path->nodes[0]);
947 WARN_ON(1);
948 goto out;
949 }
950
951 extent_slot = path->slots[0];
952 num_to_del = 1;
953 found_extent = 0;
954
955 /*
956 * if we aren't the first item on the leaf we can move back one and see
957 * if our ref is right next to our extent item
958 */
959 if (likely(extent_slot)) {
960 extent_slot--;
961 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
962 extent_slot);
963 if (found_key.objectid == op->bytenr &&
964 found_key.type == BTRFS_EXTENT_ITEM_KEY &&
965 found_key.offset == op->num_bytes) {
966 num_to_del++;
967 found_extent = 1;
968 }
969 }
970
971 /*
972 * if we didn't find the extent we need to delete the backref and then
973 * search for the extent item key so we can update its ref count
974 */
975 if (!found_extent) {
976 key.objectid = op->bytenr;
977 key.type = BTRFS_EXTENT_ITEM_KEY;
978 key.offset = op->num_bytes;
979
980 ret = remove_extent_backref(trans, extent_root, path);
981 BUG_ON(ret);
982 btrfs_release_path(extent_root, path);
983 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
984 BUG_ON(ret);
985 extent_slot = path->slots[0];
986 }
987
988 /* this is where we update the ref count for the extent */
989 leaf = path->nodes[0];
990 ei = btrfs_item_ptr(leaf, extent_slot, struct btrfs_extent_item);
991 refs = btrfs_extent_refs(leaf, ei);
992 BUG_ON(refs == 0);
993 refs--;
994 btrfs_set_extent_refs(leaf, ei, refs);
995
996 btrfs_mark_buffer_dirty(leaf);
997
998 /*
999 * This extent needs deleting. The reason cur_slot is extent_slot +
1000 * num_to_del is because extent_slot points to the slot where the extent
1001 * is, and if the backref was not right next to the extent we will be
1002 * deleting at least 1 item, and will want to start searching at the
1003 * slot directly next to extent_slot. However if we did find the
1004 * backref next to the extent item them we will be deleting at least 2
1005 * items and will want to start searching directly after the ref slot
1006 */
1007 if (!refs) {
1008 struct list_head *pos, *n, *end;
1009 int cur_slot = extent_slot+num_to_del;
1010 u64 super_used;
1011 u64 root_used;
1012
1013 path->slots[0] = extent_slot;
1014 bytes_freed = op->num_bytes;
1015
1016 mutex_lock(&info->pinned_mutex);
1017 ret = pin_down_bytes(trans, extent_root, op->bytenr,
1018 op->num_bytes, op->level >=
1019 BTRFS_FIRST_FREE_OBJECTID);
1020 mutex_unlock(&info->pinned_mutex);
1021 BUG_ON(ret < 0);
1022 op->del = ret;
1023
1024 /*
1025 * we need to see if we can delete multiple things at once, so
1026 * start looping through the list of extents we are wanting to
1027 * delete and see if their extent/backref's are right next to
1028 * eachother and the extents only have 1 ref
1029 */
1030 for (pos = cur->next; pos != del_list; pos = pos->next) {
1031 struct pending_extent_op *tmp;
1032
1033 tmp = list_entry(pos, struct pending_extent_op, list);
1034
1035 /* we only want to delete extent+ref at this stage */
1036 if (cur_slot >= btrfs_header_nritems(leaf) - 1)
1037 break;
1038
1039 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot);
1040 if (found_key.objectid != tmp->bytenr ||
1041 found_key.type != BTRFS_EXTENT_ITEM_KEY ||
1042 found_key.offset != tmp->num_bytes)
1043 break;
1044
1045 /* check to make sure this extent only has one ref */
1046 ei = btrfs_item_ptr(leaf, cur_slot,
1047 struct btrfs_extent_item);
1048 if (btrfs_extent_refs(leaf, ei) != 1)
1049 break;
1050
1051 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot+1);
1052 if (found_key.objectid != tmp->bytenr ||
1053 found_key.type != BTRFS_EXTENT_REF_KEY ||
1054 found_key.offset != tmp->orig_parent)
1055 break;
1056
1057 /*
1058 * the ref is right next to the extent, we can set the
1059 * ref count to 0 since we will delete them both now
1060 */
1061 btrfs_set_extent_refs(leaf, ei, 0);
1062
1063 /* pin down the bytes for this extent */
1064 mutex_lock(&info->pinned_mutex);
1065 ret = pin_down_bytes(trans, extent_root, tmp->bytenr,
1066 tmp->num_bytes, tmp->level >=
1067 BTRFS_FIRST_FREE_OBJECTID);
1068 mutex_unlock(&info->pinned_mutex);
1069 BUG_ON(ret < 0);
1070
1071 /*
1072 * use the del field to tell if we need to go ahead and
1073 * free up the extent when we delete the item or not.
1074 */
1075 tmp->del = ret;
1076 bytes_freed += tmp->num_bytes;
1077
1078 num_to_del += 2;
1079 cur_slot += 2;
1080 }
1081 end = pos;
1082
1083 /* update the free space counters */
1084 spin_lock(&info->delalloc_lock);
1085 super_used = btrfs_super_bytes_used(&info->super_copy);
1086 btrfs_set_super_bytes_used(&info->super_copy,
1087 super_used - bytes_freed);
1088
1089 root_used = btrfs_root_used(&extent_root->root_item);
1090 btrfs_set_root_used(&extent_root->root_item,
1091 root_used - bytes_freed);
1092 spin_unlock(&info->delalloc_lock);
1093
1094 /* delete the items */
1095 ret = btrfs_del_items(trans, extent_root, path,
1096 path->slots[0], num_to_del);
1097 BUG_ON(ret);
1098
1099 /*
1100 * loop through the extents we deleted and do the cleanup work
1101 * on them
1102 */
1103 for (pos = cur, n = pos->next; pos != end;
1104 pos = n, n = pos->next) {
1105 struct pending_extent_op *tmp;
1106 tmp = list_entry(pos, struct pending_extent_op, list);
1107
1108 /*
1109 * remember tmp->del tells us wether or not we pinned
1110 * down the extent
1111 */
1112 ret = update_block_group(trans, extent_root,
1113 tmp->bytenr, tmp->num_bytes, 0,
1114 tmp->del);
1115 BUG_ON(ret);
1116
1117 list_del_init(&tmp->list);
1118 unlock_extent(&info->extent_ins, tmp->bytenr,
1119 tmp->bytenr + tmp->num_bytes - 1,
1120 GFP_NOFS);
1121 kfree(tmp);
1122 }
1123 } else if (refs && found_extent) {
1124 /*
1125 * the ref and extent were right next to eachother, but the
1126 * extent still has a ref, so just free the backref and keep
1127 * going
1128 */
1129 ret = remove_extent_backref(trans, extent_root, path);
1130 BUG_ON(ret);
1131
1132 list_del_init(&op->list);
1133 unlock_extent(&info->extent_ins, op->bytenr,
1134 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1135 kfree(op);
1136 } else {
1137 /*
1138 * the extent has multiple refs and the backref we were looking
1139 * for was not right next to it, so just unlock and go next,
1140 * we're good to go
1141 */
1142 list_del_init(&op->list);
1143 unlock_extent(&info->extent_ins, op->bytenr,
1144 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1145 kfree(op);
1146 }
1147
1148 btrfs_release_path(extent_root, path);
1149 if (!list_empty(del_list))
1150 goto search;
1151
1152out:
1153 btrfs_free_path(path);
1154 return ret;
1155}
1156
1157static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1158 struct btrfs_root *root, u64 bytenr,
1159 u64 orig_parent, u64 parent,
1160 u64 orig_root, u64 ref_root,
1161 u64 orig_generation, u64 ref_generation,
1162 u64 owner_objectid)
1163{
1164 int ret;
1165 struct btrfs_root *extent_root = root->fs_info->extent_root;
1166 struct btrfs_path *path;
1167
1168 if (root == root->fs_info->extent_root) {
1169 struct pending_extent_op *extent_op;
1170 u64 num_bytes;
1171
1172 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
1173 num_bytes = btrfs_level_size(root, (int)owner_objectid);
1174 mutex_lock(&root->fs_info->extent_ins_mutex);
1175 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
1176 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
1177 u64 priv;
1178 ret = get_state_private(&root->fs_info->extent_ins,
1179 bytenr, &priv);
1180 BUG_ON(ret);
1181 extent_op = (struct pending_extent_op *)
1182 (unsigned long)priv;
1183 BUG_ON(extent_op->parent != orig_parent);
1184 BUG_ON(extent_op->generation != orig_generation);
1185
1186 extent_op->parent = parent;
1187 extent_op->generation = ref_generation;
1188 } else {
1189 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
1190 BUG_ON(!extent_op);
1191
1192 extent_op->type = PENDING_BACKREF_UPDATE;
1193 extent_op->bytenr = bytenr;
1194 extent_op->num_bytes = num_bytes;
1195 extent_op->parent = parent;
1196 extent_op->orig_parent = orig_parent;
1197 extent_op->generation = ref_generation;
1198 extent_op->orig_generation = orig_generation;
1199 extent_op->level = (int)owner_objectid;
1200 INIT_LIST_HEAD(&extent_op->list);
1201 extent_op->del = 0;
1202
1203 set_extent_bits(&root->fs_info->extent_ins,
1204 bytenr, bytenr + num_bytes - 1,
1205 EXTENT_WRITEBACK, GFP_NOFS);
1206 set_state_private(&root->fs_info->extent_ins,
1207 bytenr, (unsigned long)extent_op);
1208 }
1209 mutex_unlock(&root->fs_info->extent_ins_mutex);
1210 return 0;
1211 }
1212
1213 path = btrfs_alloc_path();
1214 if (!path)
1215 return -ENOMEM;
1216 ret = lookup_extent_backref(trans, extent_root, path,
1217 bytenr, orig_parent, orig_root,
1218 orig_generation, owner_objectid, 1);
1219 if (ret)
1220 goto out;
1221 ret = remove_extent_backref(trans, extent_root, path);
1222 if (ret)
1223 goto out;
1224 ret = insert_extent_backref(trans, extent_root, path, bytenr,
1225 parent, ref_root, ref_generation,
1226 owner_objectid);
1227 BUG_ON(ret);
1228 finish_current_insert(trans, extent_root, 0);
1229 del_pending_extents(trans, extent_root, 0);
1230out:
1231 btrfs_free_path(path);
1232 return ret;
1233}
1234
1235int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1236 struct btrfs_root *root, u64 bytenr,
1237 u64 orig_parent, u64 parent,
1238 u64 ref_root, u64 ref_generation,
1239 u64 owner_objectid)
1240{
1241 int ret;
1242 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1243 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1244 return 0;
1245 ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
1246 parent, ref_root, ref_root,
1247 ref_generation, ref_generation,
1248 owner_objectid);
1249 return ret;
1250}
1251
1252static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1253 struct btrfs_root *root, u64 bytenr,
1254 u64 orig_parent, u64 parent,
1255 u64 orig_root, u64 ref_root,
1256 u64 orig_generation, u64 ref_generation,
1257 u64 owner_objectid)
1258{
1259 struct btrfs_path *path;
1260 int ret;
1261 struct btrfs_key key;
1262 struct extent_buffer *l;
1263 struct btrfs_extent_item *item;
1264 u32 refs;
1265
1266 path = btrfs_alloc_path();
1267 if (!path)
1268 return -ENOMEM;
1269
1270 path->reada = 1;
1271 key.objectid = bytenr;
1272 key.type = BTRFS_EXTENT_ITEM_KEY;
1273 key.offset = (u64)-1;
1274
1275 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1276 0, 1);
1277 if (ret < 0)
1278 return ret;
1279 BUG_ON(ret == 0 || path->slots[0] == 0);
1280
1281 path->slots[0]--;
1282 l = path->nodes[0];
1283
1284 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1285 if (key.objectid != bytenr) {
1286 btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]);
1287 printk(KERN_ERR "btrfs wanted %llu found %llu\n",
1288 (unsigned long long)bytenr,
1289 (unsigned long long)key.objectid);
1290 BUG();
1291 }
1292 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
1293
1294 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1295 refs = btrfs_extent_refs(l, item);
1296 btrfs_set_extent_refs(l, item, refs + 1);
1297 btrfs_mark_buffer_dirty(path->nodes[0]);
1298
1299 btrfs_release_path(root->fs_info->extent_root, path);
1300
1301 path->reada = 1;
1302 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1303 path, bytenr, parent,
1304 ref_root, ref_generation,
1305 owner_objectid);
1306 BUG_ON(ret);
1307 finish_current_insert(trans, root->fs_info->extent_root, 0);
1308 del_pending_extents(trans, root->fs_info->extent_root, 0);
1309
1310 btrfs_free_path(path);
1311 return 0;
1312}
1313
1314int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1315 struct btrfs_root *root,
1316 u64 bytenr, u64 num_bytes, u64 parent,
1317 u64 ref_root, u64 ref_generation,
1318 u64 owner_objectid)
1319{
1320 int ret;
1321 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1322 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1323 return 0;
1324 ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
1325 0, ref_root, 0, ref_generation,
1326 owner_objectid);
1327 return ret;
1328}
1329
1330int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
1331 struct btrfs_root *root)
1332{
1333 finish_current_insert(trans, root->fs_info->extent_root, 1);
1334 del_pending_extents(trans, root->fs_info->extent_root, 1);
1335 return 0;
1336}
1337
1338int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
1339 struct btrfs_root *root, u64 bytenr,
1340 u64 num_bytes, u32 *refs)
1341{
1342 struct btrfs_path *path;
1343 int ret;
1344 struct btrfs_key key;
1345 struct extent_buffer *l;
1346 struct btrfs_extent_item *item;
1347
1348 WARN_ON(num_bytes < root->sectorsize);
1349 path = btrfs_alloc_path();
1350 path->reada = 1;
1351 key.objectid = bytenr;
1352 key.offset = num_bytes;
1353 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1354 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1355 0, 0);
1356 if (ret < 0)
1357 goto out;
1358 if (ret != 0) {
1359 btrfs_print_leaf(root, path->nodes[0]);
1360 printk(KERN_INFO "btrfs failed to find block number %llu\n",
1361 (unsigned long long)bytenr);
1362 BUG();
1363 }
1364 l = path->nodes[0];
1365 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1366 *refs = btrfs_extent_refs(l, item);
1367out:
1368 btrfs_free_path(path);
1369 return 0;
1370}
1371
1372int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
1373 struct btrfs_root *root, u64 objectid, u64 bytenr)
1374{
1375 struct btrfs_root *extent_root = root->fs_info->extent_root;
1376 struct btrfs_path *path;
1377 struct extent_buffer *leaf;
1378 struct btrfs_extent_ref *ref_item;
1379 struct btrfs_key key;
1380 struct btrfs_key found_key;
1381 u64 ref_root;
1382 u64 last_snapshot;
1383 u32 nritems;
1384 int ret;
1385
1386 key.objectid = bytenr;
1387 key.offset = (u64)-1;
1388 key.type = BTRFS_EXTENT_ITEM_KEY;
1389
1390 path = btrfs_alloc_path();
1391 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
1392 if (ret < 0)
1393 goto out;
1394 BUG_ON(ret == 0);
1395
1396 ret = -ENOENT;
1397 if (path->slots[0] == 0)
1398 goto out;
1399
1400 path->slots[0]--;
1401 leaf = path->nodes[0];
1402 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1403
1404 if (found_key.objectid != bytenr ||
1405 found_key.type != BTRFS_EXTENT_ITEM_KEY)
1406 goto out;
1407
1408 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
1409 while (1) {
1410 leaf = path->nodes[0];
1411 nritems = btrfs_header_nritems(leaf);
1412 if (path->slots[0] >= nritems) {
1413 ret = btrfs_next_leaf(extent_root, path);
1414 if (ret < 0)
1415 goto out;
1416 if (ret == 0)
1417 continue;
1418 break;
1419 }
1420 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1421 if (found_key.objectid != bytenr)
1422 break;
1423
1424 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
1425 path->slots[0]++;
1426 continue;
1427 }
1428
1429 ref_item = btrfs_item_ptr(leaf, path->slots[0],
1430 struct btrfs_extent_ref);
1431 ref_root = btrfs_ref_root(leaf, ref_item);
1432 if ((ref_root != root->root_key.objectid &&
1433 ref_root != BTRFS_TREE_LOG_OBJECTID) ||
1434 objectid != btrfs_ref_objectid(leaf, ref_item)) {
1435 ret = 1;
1436 goto out;
1437 }
1438 if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
1439 ret = 1;
1440 goto out;
1441 }
1442
1443 path->slots[0]++;
1444 }
1445 ret = 0;
1446out:
1447 btrfs_free_path(path);
1448 return ret;
1449}
1450
1451int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1452 struct extent_buffer *buf, u32 nr_extents)
1453{
1454 struct btrfs_key key;
1455 struct btrfs_file_extent_item *fi;
1456 u64 root_gen;
1457 u32 nritems;
1458 int i;
1459 int level;
1460 int ret = 0;
1461 int shared = 0;
1462
1463 if (!root->ref_cows)
1464 return 0;
1465
1466 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1467 shared = 0;
1468 root_gen = root->root_key.offset;
1469 } else {
1470 shared = 1;
1471 root_gen = trans->transid - 1;
1472 }
1473
1474 level = btrfs_header_level(buf);
1475 nritems = btrfs_header_nritems(buf);
1476
1477 if (level == 0) {
1478 struct btrfs_leaf_ref *ref;
1479 struct btrfs_extent_info *info;
1480
1481 ref = btrfs_alloc_leaf_ref(root, nr_extents);
1482 if (!ref) {
1483 ret = -ENOMEM;
1484 goto out;
1485 }
1486
1487 ref->root_gen = root_gen;
1488 ref->bytenr = buf->start;
1489 ref->owner = btrfs_header_owner(buf);
1490 ref->generation = btrfs_header_generation(buf);
1491 ref->nritems = nr_extents;
1492 info = ref->extents;
1493
1494 for (i = 0; nr_extents > 0 && i < nritems; i++) {
1495 u64 disk_bytenr;
1496 btrfs_item_key_to_cpu(buf, &key, i);
1497 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1498 continue;
1499 fi = btrfs_item_ptr(buf, i,
1500 struct btrfs_file_extent_item);
1501 if (btrfs_file_extent_type(buf, fi) ==
1502 BTRFS_FILE_EXTENT_INLINE)
1503 continue;
1504 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1505 if (disk_bytenr == 0)
1506 continue;
1507
1508 info->bytenr = disk_bytenr;
1509 info->num_bytes =
1510 btrfs_file_extent_disk_num_bytes(buf, fi);
1511 info->objectid = key.objectid;
1512 info->offset = key.offset;
1513 info++;
1514 }
1515
1516 ret = btrfs_add_leaf_ref(root, ref, shared);
1517 if (ret == -EEXIST && shared) {
1518 struct btrfs_leaf_ref *old;
1519 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
1520 BUG_ON(!old);
1521 btrfs_remove_leaf_ref(root, old);
1522 btrfs_free_leaf_ref(root, old);
1523 ret = btrfs_add_leaf_ref(root, ref, shared);
1524 }
1525 WARN_ON(ret);
1526 btrfs_free_leaf_ref(root, ref);
1527 }
1528out:
1529 return ret;
1530}
1531
1532int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1533 struct extent_buffer *orig_buf, struct extent_buffer *buf,
1534 u32 *nr_extents)
1535{
1536 u64 bytenr;
1537 u64 ref_root;
1538 u64 orig_root;
1539 u64 ref_generation;
1540 u64 orig_generation;
1541 u32 nritems;
1542 u32 nr_file_extents = 0;
1543 struct btrfs_key key;
1544 struct btrfs_file_extent_item *fi;
1545 int i;
1546 int level;
1547 int ret = 0;
1548 int faili = 0;
1549 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
1550 u64, u64, u64, u64, u64, u64, u64, u64);
1551
1552 ref_root = btrfs_header_owner(buf);
1553 ref_generation = btrfs_header_generation(buf);
1554 orig_root = btrfs_header_owner(orig_buf);
1555 orig_generation = btrfs_header_generation(orig_buf);
1556
1557 nritems = btrfs_header_nritems(buf);
1558 level = btrfs_header_level(buf);
1559
1560 if (root->ref_cows) {
1561 process_func = __btrfs_inc_extent_ref;
1562 } else {
1563 if (level == 0 &&
1564 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1565 goto out;
1566 if (level != 0 &&
1567 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1568 goto out;
1569 process_func = __btrfs_update_extent_ref;
1570 }
1571
1572 for (i = 0; i < nritems; i++) {
1573 cond_resched();
1574 if (level == 0) {
1575 btrfs_item_key_to_cpu(buf, &key, i);
1576 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1577 continue;
1578 fi = btrfs_item_ptr(buf, i,
1579 struct btrfs_file_extent_item);
1580 if (btrfs_file_extent_type(buf, fi) ==
1581 BTRFS_FILE_EXTENT_INLINE)
1582 continue;
1583 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1584 if (bytenr == 0)
1585 continue;
1586
1587 nr_file_extents++;
1588
1589 ret = process_func(trans, root, bytenr,
1590 orig_buf->start, buf->start,
1591 orig_root, ref_root,
1592 orig_generation, ref_generation,
1593 key.objectid);
1594
1595 if (ret) {
1596 faili = i;
1597 WARN_ON(1);
1598 goto fail;
1599 }
1600 } else {
1601 bytenr = btrfs_node_blockptr(buf, i);
1602 ret = process_func(trans, root, bytenr,
1603 orig_buf->start, buf->start,
1604 orig_root, ref_root,
1605 orig_generation, ref_generation,
1606 level - 1);
1607 if (ret) {
1608 faili = i;
1609 WARN_ON(1);
1610 goto fail;
1611 }
1612 }
1613 }
1614out:
1615 if (nr_extents) {
1616 if (level == 0)
1617 *nr_extents = nr_file_extents;
1618 else
1619 *nr_extents = nritems;
1620 }
1621 return 0;
1622fail:
1623 WARN_ON(1);
1624 return ret;
1625}
1626
1627int btrfs_update_ref(struct btrfs_trans_handle *trans,
1628 struct btrfs_root *root, struct extent_buffer *orig_buf,
1629 struct extent_buffer *buf, int start_slot, int nr)
1630
1631{
1632 u64 bytenr;
1633 u64 ref_root;
1634 u64 orig_root;
1635 u64 ref_generation;
1636 u64 orig_generation;
1637 struct btrfs_key key;
1638 struct btrfs_file_extent_item *fi;
1639 int i;
1640 int ret;
1641 int slot;
1642 int level;
1643
1644 BUG_ON(start_slot < 0);
1645 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1646
1647 ref_root = btrfs_header_owner(buf);
1648 ref_generation = btrfs_header_generation(buf);
1649 orig_root = btrfs_header_owner(orig_buf);
1650 orig_generation = btrfs_header_generation(orig_buf);
1651 level = btrfs_header_level(buf);
1652
1653 if (!root->ref_cows) {
1654 if (level == 0 &&
1655 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1656 return 0;
1657 if (level != 0 &&
1658 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1659 return 0;
1660 }
1661
1662 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1663 cond_resched();
1664 if (level == 0) {
1665 btrfs_item_key_to_cpu(buf, &key, slot);
1666 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1667 continue;
1668 fi = btrfs_item_ptr(buf, slot,
1669 struct btrfs_file_extent_item);
1670 if (btrfs_file_extent_type(buf, fi) ==
1671 BTRFS_FILE_EXTENT_INLINE)
1672 continue;
1673 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1674 if (bytenr == 0)
1675 continue;
1676 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1677 orig_buf->start, buf->start,
1678 orig_root, ref_root,
1679 orig_generation, ref_generation,
1680 key.objectid);
1681 if (ret)
1682 goto fail;
1683 } else {
1684 bytenr = btrfs_node_blockptr(buf, slot);
1685 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1686 orig_buf->start, buf->start,
1687 orig_root, ref_root,
1688 orig_generation, ref_generation,
1689 level - 1);
1690 if (ret)
1691 goto fail;
1692 }
1693 }
1694 return 0;
1695fail:
1696 WARN_ON(1);
1697 return -1;
1698}
1699
1700static int write_one_cache_group(struct btrfs_trans_handle *trans,
1701 struct btrfs_root *root,
1702 struct btrfs_path *path,
1703 struct btrfs_block_group_cache *cache)
1704{
1705 int ret;
1706 int pending_ret;
1707 struct btrfs_root *extent_root = root->fs_info->extent_root;
1708 unsigned long bi;
1709 struct extent_buffer *leaf;
1710
1711 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
1712 if (ret < 0)
1713 goto fail;
1714 BUG_ON(ret);
1715
1716 leaf = path->nodes[0];
1717 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1718 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1719 btrfs_mark_buffer_dirty(leaf);
1720 btrfs_release_path(extent_root, path);
1721fail:
1722 finish_current_insert(trans, extent_root, 0);
1723 pending_ret = del_pending_extents(trans, extent_root, 0);
1724 if (ret)
1725 return ret;
1726 if (pending_ret)
1727 return pending_ret;
1728 return 0;
1729
1730}
1731
1732int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1733 struct btrfs_root *root)
1734{
1735 struct btrfs_block_group_cache *cache, *entry;
1736 struct rb_node *n;
1737 int err = 0;
1738 int werr = 0;
1739 struct btrfs_path *path;
1740 u64 last = 0;
1741
1742 path = btrfs_alloc_path();
1743 if (!path)
1744 return -ENOMEM;
1745
1746 while (1) {
1747 cache = NULL;
1748 spin_lock(&root->fs_info->block_group_cache_lock);
1749 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1750 n; n = rb_next(n)) {
1751 entry = rb_entry(n, struct btrfs_block_group_cache,
1752 cache_node);
1753 if (entry->dirty) {
1754 cache = entry;
1755 break;
1756 }
1757 }
1758 spin_unlock(&root->fs_info->block_group_cache_lock);
1759
1760 if (!cache)
1761 break;
1762
1763 cache->dirty = 0;
1764 last += cache->key.offset;
1765
1766 err = write_one_cache_group(trans, root,
1767 path, cache);
1768 /*
1769 * if we fail to write the cache group, we want
1770 * to keep it marked dirty in hopes that a later
1771 * write will work
1772 */
1773 if (err) {
1774 werr = err;
1775 continue;
1776 }
1777 }
1778 btrfs_free_path(path);
1779 return werr;
1780}
1781
1782int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
1783{
1784 struct btrfs_block_group_cache *block_group;
1785 int readonly = 0;
1786
1787 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
1788 if (!block_group || block_group->ro)
1789 readonly = 1;
1790 if (block_group)
1791 put_block_group(block_group);
1792 return readonly;
1793}
1794
1795static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1796 u64 total_bytes, u64 bytes_used,
1797 struct btrfs_space_info **space_info)
1798{
1799 struct btrfs_space_info *found;
1800
1801 found = __find_space_info(info, flags);
1802 if (found) {
1803 spin_lock(&found->lock);
1804 found->total_bytes += total_bytes;
1805 found->bytes_used += bytes_used;
1806 found->full = 0;
1807 spin_unlock(&found->lock);
1808 *space_info = found;
1809 return 0;
1810 }
1811 found = kzalloc(sizeof(*found), GFP_NOFS);
1812 if (!found)
1813 return -ENOMEM;
1814
1815 list_add(&found->list, &info->space_info);
1816 INIT_LIST_HEAD(&found->block_groups);
1817 init_rwsem(&found->groups_sem);
1818 spin_lock_init(&found->lock);
1819 found->flags = flags;
1820 found->total_bytes = total_bytes;
1821 found->bytes_used = bytes_used;
1822 found->bytes_pinned = 0;
1823 found->bytes_reserved = 0;
1824 found->bytes_readonly = 0;
1825 found->full = 0;
1826 found->force_alloc = 0;
1827 *space_info = found;
1828 return 0;
1829}
1830
1831static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1832{
1833 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
1834 BTRFS_BLOCK_GROUP_RAID1 |
1835 BTRFS_BLOCK_GROUP_RAID10 |
1836 BTRFS_BLOCK_GROUP_DUP);
1837 if (extra_flags) {
1838 if (flags & BTRFS_BLOCK_GROUP_DATA)
1839 fs_info->avail_data_alloc_bits |= extra_flags;
1840 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1841 fs_info->avail_metadata_alloc_bits |= extra_flags;
1842 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1843 fs_info->avail_system_alloc_bits |= extra_flags;
1844 }
1845}
1846
1847static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
1848{
1849 spin_lock(&cache->space_info->lock);
1850 spin_lock(&cache->lock);
1851 if (!cache->ro) {
1852 cache->space_info->bytes_readonly += cache->key.offset -
1853 btrfs_block_group_used(&cache->item);
1854 cache->ro = 1;
1855 }
1856 spin_unlock(&cache->lock);
1857 spin_unlock(&cache->space_info->lock);
1858}
1859
1860u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
1861{
1862 u64 num_devices = root->fs_info->fs_devices->rw_devices;
1863
1864 if (num_devices == 1)
1865 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1866 if (num_devices < 4)
1867 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1868
1869 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1870 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
1871 BTRFS_BLOCK_GROUP_RAID10))) {
1872 flags &= ~BTRFS_BLOCK_GROUP_DUP;
1873 }
1874
1875 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
1876 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
1877 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
1878 }
1879
1880 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1881 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1882 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1883 (flags & BTRFS_BLOCK_GROUP_DUP)))
1884 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1885 return flags;
1886}
1887
1888static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1889 struct btrfs_root *extent_root, u64 alloc_bytes,
1890 u64 flags, int force)
1891{
1892 struct btrfs_space_info *space_info;
1893 u64 thresh;
1894 int ret = 0;
1895
1896 mutex_lock(&extent_root->fs_info->chunk_mutex);
1897
1898 flags = btrfs_reduce_alloc_profile(extent_root, flags);
1899
1900 space_info = __find_space_info(extent_root->fs_info, flags);
1901 if (!space_info) {
1902 ret = update_space_info(extent_root->fs_info, flags,
1903 0, 0, &space_info);
1904 BUG_ON(ret);
1905 }
1906 BUG_ON(!space_info);
1907
1908 spin_lock(&space_info->lock);
1909 if (space_info->force_alloc) {
1910 force = 1;
1911 space_info->force_alloc = 0;
1912 }
1913 if (space_info->full) {
1914 spin_unlock(&space_info->lock);
1915 goto out;
1916 }
1917
1918 thresh = space_info->total_bytes - space_info->bytes_readonly;
1919 thresh = div_factor(thresh, 6);
1920 if (!force &&
1921 (space_info->bytes_used + space_info->bytes_pinned +
1922 space_info->bytes_reserved + alloc_bytes) < thresh) {
1923 spin_unlock(&space_info->lock);
1924 goto out;
1925 }
1926 spin_unlock(&space_info->lock);
1927
1928 ret = btrfs_alloc_chunk(trans, extent_root, flags);
1929 if (ret)
1930 space_info->full = 1;
1931out:
1932 mutex_unlock(&extent_root->fs_info->chunk_mutex);
1933 return ret;
1934}
1935
1936static int update_block_group(struct btrfs_trans_handle *trans,
1937 struct btrfs_root *root,
1938 u64 bytenr, u64 num_bytes, int alloc,
1939 int mark_free)
1940{
1941 struct btrfs_block_group_cache *cache;
1942 struct btrfs_fs_info *info = root->fs_info;
1943 u64 total = num_bytes;
1944 u64 old_val;
1945 u64 byte_in_group;
1946
1947 while (total) {
1948 cache = btrfs_lookup_block_group(info, bytenr);
1949 if (!cache)
1950 return -1;
1951 byte_in_group = bytenr - cache->key.objectid;
1952 WARN_ON(byte_in_group > cache->key.offset);
1953
1954 spin_lock(&cache->space_info->lock);
1955 spin_lock(&cache->lock);
1956 cache->dirty = 1;
1957 old_val = btrfs_block_group_used(&cache->item);
1958 num_bytes = min(total, cache->key.offset - byte_in_group);
1959 if (alloc) {
1960 old_val += num_bytes;
1961 cache->space_info->bytes_used += num_bytes;
1962 if (cache->ro)
1963 cache->space_info->bytes_readonly -= num_bytes;
1964 btrfs_set_block_group_used(&cache->item, old_val);
1965 spin_unlock(&cache->lock);
1966 spin_unlock(&cache->space_info->lock);
1967 } else {
1968 old_val -= num_bytes;
1969 cache->space_info->bytes_used -= num_bytes;
1970 if (cache->ro)
1971 cache->space_info->bytes_readonly += num_bytes;
1972 btrfs_set_block_group_used(&cache->item, old_val);
1973 spin_unlock(&cache->lock);
1974 spin_unlock(&cache->space_info->lock);
1975 if (mark_free) {
1976 int ret;
1977
1978 ret = btrfs_discard_extent(root, bytenr,
1979 num_bytes);
1980 WARN_ON(ret);
1981
1982 ret = btrfs_add_free_space(cache, bytenr,
1983 num_bytes);
1984 WARN_ON(ret);
1985 }
1986 }
1987 put_block_group(cache);
1988 total -= num_bytes;
1989 bytenr += num_bytes;
1990 }
1991 return 0;
1992}
1993
1994static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1995{
1996 struct btrfs_block_group_cache *cache;
1997 u64 bytenr;
1998
1999 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
2000 if (!cache)
2001 return 0;
2002
2003 bytenr = cache->key.objectid;
2004 put_block_group(cache);
2005
2006 return bytenr;
2007}
2008
2009int btrfs_update_pinned_extents(struct btrfs_root *root,
2010 u64 bytenr, u64 num, int pin)
2011{
2012 u64 len;
2013 struct btrfs_block_group_cache *cache;
2014 struct btrfs_fs_info *fs_info = root->fs_info;
2015
2016 WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
2017 if (pin) {
2018 set_extent_dirty(&fs_info->pinned_extents,
2019 bytenr, bytenr + num - 1, GFP_NOFS);
2020 } else {
2021 clear_extent_dirty(&fs_info->pinned_extents,
2022 bytenr, bytenr + num - 1, GFP_NOFS);
2023 }
2024 while (num > 0) {
2025 cache = btrfs_lookup_block_group(fs_info, bytenr);
2026 BUG_ON(!cache);
2027 len = min(num, cache->key.offset -
2028 (bytenr - cache->key.objectid));
2029 if (pin) {
2030 spin_lock(&cache->space_info->lock);
2031 spin_lock(&cache->lock);
2032 cache->pinned += len;
2033 cache->space_info->bytes_pinned += len;
2034 spin_unlock(&cache->lock);
2035 spin_unlock(&cache->space_info->lock);
2036 fs_info->total_pinned += len;
2037 } else {
2038 spin_lock(&cache->space_info->lock);
2039 spin_lock(&cache->lock);
2040 cache->pinned -= len;
2041 cache->space_info->bytes_pinned -= len;
2042 spin_unlock(&cache->lock);
2043 spin_unlock(&cache->space_info->lock);
2044 fs_info->total_pinned -= len;
2045 if (cache->cached)
2046 btrfs_add_free_space(cache, bytenr, len);
2047 }
2048 put_block_group(cache);
2049 bytenr += len;
2050 num -= len;
2051 }
2052 return 0;
2053}
2054
2055static int update_reserved_extents(struct btrfs_root *root,
2056 u64 bytenr, u64 num, int reserve)
2057{
2058 u64 len;
2059 struct btrfs_block_group_cache *cache;
2060 struct btrfs_fs_info *fs_info = root->fs_info;
2061
2062 while (num > 0) {
2063 cache = btrfs_lookup_block_group(fs_info, bytenr);
2064 BUG_ON(!cache);
2065 len = min(num, cache->key.offset -
2066 (bytenr - cache->key.objectid));
2067
2068 spin_lock(&cache->space_info->lock);
2069 spin_lock(&cache->lock);
2070 if (reserve) {
2071 cache->reserved += len;
2072 cache->space_info->bytes_reserved += len;
2073 } else {
2074 cache->reserved -= len;
2075 cache->space_info->bytes_reserved -= len;
2076 }
2077 spin_unlock(&cache->lock);
2078 spin_unlock(&cache->space_info->lock);
2079 put_block_group(cache);
2080 bytenr += len;
2081 num -= len;
2082 }
2083 return 0;
2084}
2085
2086int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
2087{
2088 u64 last = 0;
2089 u64 start;
2090 u64 end;
2091 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
2092 int ret;
2093
2094 mutex_lock(&root->fs_info->pinned_mutex);
2095 while (1) {
2096 ret = find_first_extent_bit(pinned_extents, last,
2097 &start, &end, EXTENT_DIRTY);
2098 if (ret)
2099 break;
2100 set_extent_dirty(copy, start, end, GFP_NOFS);
2101 last = end + 1;
2102 }
2103 mutex_unlock(&root->fs_info->pinned_mutex);
2104 return 0;
2105}
2106
2107int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2108 struct btrfs_root *root,
2109 struct extent_io_tree *unpin)
2110{
2111 u64 start;
2112 u64 end;
2113 int ret;
2114
2115 mutex_lock(&root->fs_info->pinned_mutex);
2116 while (1) {
2117 ret = find_first_extent_bit(unpin, 0, &start, &end,
2118 EXTENT_DIRTY);
2119 if (ret)
2120 break;
2121
2122 ret = btrfs_discard_extent(root, start, end + 1 - start);
2123
2124 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
2125 clear_extent_dirty(unpin, start, end, GFP_NOFS);
2126
2127 if (need_resched()) {
2128 mutex_unlock(&root->fs_info->pinned_mutex);
2129 cond_resched();
2130 mutex_lock(&root->fs_info->pinned_mutex);
2131 }
2132 }
2133 mutex_unlock(&root->fs_info->pinned_mutex);
2134 return ret;
2135}
2136
2137static int finish_current_insert(struct btrfs_trans_handle *trans,
2138 struct btrfs_root *extent_root, int all)
2139{
2140 u64 start;
2141 u64 end;
2142 u64 priv;
2143 u64 search = 0;
2144 u64 skipped = 0;
2145 struct btrfs_fs_info *info = extent_root->fs_info;
2146 struct btrfs_path *path;
2147 struct pending_extent_op *extent_op, *tmp;
2148 struct list_head insert_list, update_list;
2149 int ret;
2150 int num_inserts = 0, max_inserts;
2151
2152 path = btrfs_alloc_path();
2153 INIT_LIST_HEAD(&insert_list);
2154 INIT_LIST_HEAD(&update_list);
2155
2156 max_inserts = extent_root->leafsize /
2157 (2 * sizeof(struct btrfs_key) + 2 * sizeof(struct btrfs_item) +
2158 sizeof(struct btrfs_extent_ref) +
2159 sizeof(struct btrfs_extent_item));
2160again:
2161 mutex_lock(&info->extent_ins_mutex);
2162 while (1) {
2163 ret = find_first_extent_bit(&info->extent_ins, search, &start,
2164 &end, EXTENT_WRITEBACK);
2165 if (ret) {
2166 if (skipped && all && !num_inserts) {
2167 skipped = 0;
2168 search = 0;
2169 continue;
2170 }
2171 mutex_unlock(&info->extent_ins_mutex);
2172 break;
2173 }
2174
2175 ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS);
2176 if (!ret) {
2177 skipped = 1;
2178 search = end + 1;
2179 if (need_resched()) {
2180 mutex_unlock(&info->extent_ins_mutex);
2181 cond_resched();
2182 mutex_lock(&info->extent_ins_mutex);
2183 }
2184 continue;
2185 }
2186
2187 ret = get_state_private(&info->extent_ins, start, &priv);
2188 BUG_ON(ret);
2189 extent_op = (struct pending_extent_op *)(unsigned long) priv;
2190
2191 if (extent_op->type == PENDING_EXTENT_INSERT) {
2192 num_inserts++;
2193 list_add_tail(&extent_op->list, &insert_list);
2194 search = end + 1;
2195 if (num_inserts == max_inserts) {
2196 mutex_unlock(&info->extent_ins_mutex);
2197 break;
2198 }
2199 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
2200 list_add_tail(&extent_op->list, &update_list);
2201 search = end + 1;
2202 } else {
2203 BUG();
2204 }
2205 }
2206
2207 /*
2208 * process the update list, clear the writeback bit for it, and if
2209 * somebody marked this thing for deletion then just unlock it and be
2210 * done, the free_extents will handle it
2211 */
2212 mutex_lock(&info->extent_ins_mutex);
2213 list_for_each_entry_safe(extent_op, tmp, &update_list, list) {
2214 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2215 extent_op->bytenr + extent_op->num_bytes - 1,
2216 EXTENT_WRITEBACK, GFP_NOFS);
2217 if (extent_op->del) {
2218 list_del_init(&extent_op->list);
2219 unlock_extent(&info->extent_ins, extent_op->bytenr,
2220 extent_op->bytenr + extent_op->num_bytes
2221 - 1, GFP_NOFS);
2222 kfree(extent_op);
2223 }
2224 }
2225 mutex_unlock(&info->extent_ins_mutex);
2226
2227 /*
2228 * still have things left on the update list, go ahead an update
2229 * everything
2230 */
2231 if (!list_empty(&update_list)) {
2232 ret = update_backrefs(trans, extent_root, path, &update_list);
2233 BUG_ON(ret);
2234 }
2235
2236 /*
2237 * if no inserts need to be done, but we skipped some extents and we
2238 * need to make sure everything is cleaned then reset everything and
2239 * go back to the beginning
2240 */
2241 if (!num_inserts && all && skipped) {
2242 search = 0;
2243 skipped = 0;
2244 INIT_LIST_HEAD(&update_list);
2245 INIT_LIST_HEAD(&insert_list);
2246 goto again;
2247 } else if (!num_inserts) {
2248 goto out;
2249 }
2250
2251 /*
2252 * process the insert extents list. Again if we are deleting this
2253 * extent, then just unlock it, pin down the bytes if need be, and be
2254 * done with it. Saves us from having to actually insert the extent
2255 * into the tree and then subsequently come along and delete it
2256 */
2257 mutex_lock(&info->extent_ins_mutex);
2258 list_for_each_entry_safe(extent_op, tmp, &insert_list, list) {
2259 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2260 extent_op->bytenr + extent_op->num_bytes - 1,
2261 EXTENT_WRITEBACK, GFP_NOFS);
2262 if (extent_op->del) {
2263 u64 used;
2264 list_del_init(&extent_op->list);
2265 unlock_extent(&info->extent_ins, extent_op->bytenr,
2266 extent_op->bytenr + extent_op->num_bytes
2267 - 1, GFP_NOFS);
2268
2269 mutex_lock(&extent_root->fs_info->pinned_mutex);
2270 ret = pin_down_bytes(trans, extent_root,
2271 extent_op->bytenr,
2272 extent_op->num_bytes, 0);
2273 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2274
2275 spin_lock(&info->delalloc_lock);
2276 used = btrfs_super_bytes_used(&info->super_copy);
2277 btrfs_set_super_bytes_used(&info->super_copy,
2278 used - extent_op->num_bytes);
2279 used = btrfs_root_used(&extent_root->root_item);
2280 btrfs_set_root_used(&extent_root->root_item,
2281 used - extent_op->num_bytes);
2282 spin_unlock(&info->delalloc_lock);
2283
2284 ret = update_block_group(trans, extent_root,
2285 extent_op->bytenr,
2286 extent_op->num_bytes,
2287 0, ret > 0);
2288 BUG_ON(ret);
2289 kfree(extent_op);
2290 num_inserts--;
2291 }
2292 }
2293 mutex_unlock(&info->extent_ins_mutex);
2294
2295 ret = insert_extents(trans, extent_root, path, &insert_list,
2296 num_inserts);
2297 BUG_ON(ret);
2298
2299 /*
2300 * if we broke out of the loop in order to insert stuff because we hit
2301 * the maximum number of inserts at a time we can handle, then loop
2302 * back and pick up where we left off
2303 */
2304 if (num_inserts == max_inserts) {
2305 INIT_LIST_HEAD(&insert_list);
2306 INIT_LIST_HEAD(&update_list);
2307 num_inserts = 0;
2308 goto again;
2309 }
2310
2311 /*
2312 * again, if we need to make absolutely sure there are no more pending
2313 * extent operations left and we know that we skipped some, go back to
2314 * the beginning and do it all again
2315 */
2316 if (all && skipped) {
2317 INIT_LIST_HEAD(&insert_list);
2318 INIT_LIST_HEAD(&update_list);
2319 search = 0;
2320 skipped = 0;
2321 num_inserts = 0;
2322 goto again;
2323 }
2324out:
2325 btrfs_free_path(path);
2326 return 0;
2327}
2328
2329static int pin_down_bytes(struct btrfs_trans_handle *trans,
2330 struct btrfs_root *root,
2331 u64 bytenr, u64 num_bytes, int is_data)
2332{
2333 int err = 0;
2334 struct extent_buffer *buf;
2335
2336 if (is_data)
2337 goto pinit;
2338
2339 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2340 if (!buf)
2341 goto pinit;
2342
2343 /* we can reuse a block if it hasn't been written
2344 * and it is from this transaction. We can't
2345 * reuse anything from the tree log root because
2346 * it has tiny sub-transactions.
2347 */
2348 if (btrfs_buffer_uptodate(buf, 0) &&
2349 btrfs_try_tree_lock(buf)) {
2350 u64 header_owner = btrfs_header_owner(buf);
2351 u64 header_transid = btrfs_header_generation(buf);
2352 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
2353 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
2354 header_transid == trans->transid &&
2355 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2356 clean_tree_block(NULL, root, buf);
2357 btrfs_tree_unlock(buf);
2358 free_extent_buffer(buf);
2359 return 1;
2360 }
2361 btrfs_tree_unlock(buf);
2362 }
2363 free_extent_buffer(buf);
2364pinit:
2365 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2366
2367 BUG_ON(err < 0);
2368 return 0;
2369}
2370
2371/*
2372 * remove an extent from the root, returns 0 on success
2373 */
2374static int __free_extent(struct btrfs_trans_handle *trans,
2375 struct btrfs_root *root,
2376 u64 bytenr, u64 num_bytes, u64 parent,
2377 u64 root_objectid, u64 ref_generation,
2378 u64 owner_objectid, int pin, int mark_free)
2379{
2380 struct btrfs_path *path;
2381 struct btrfs_key key;
2382 struct btrfs_fs_info *info = root->fs_info;
2383 struct btrfs_root *extent_root = info->extent_root;
2384 struct extent_buffer *leaf;
2385 int ret;
2386 int extent_slot = 0;
2387 int found_extent = 0;
2388 int num_to_del = 1;
2389 struct btrfs_extent_item *ei;
2390 u32 refs;
2391
2392 key.objectid = bytenr;
2393 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
2394 key.offset = num_bytes;
2395 path = btrfs_alloc_path();
2396 if (!path)
2397 return -ENOMEM;
2398
2399 path->reada = 1;
2400 ret = lookup_extent_backref(trans, extent_root, path,
2401 bytenr, parent, root_objectid,
2402 ref_generation, owner_objectid, 1);
2403 if (ret == 0) {
2404 struct btrfs_key found_key;
2405 extent_slot = path->slots[0];
2406 while (extent_slot > 0) {
2407 extent_slot--;
2408 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2409 extent_slot);
2410 if (found_key.objectid != bytenr)
2411 break;
2412 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
2413 found_key.offset == num_bytes) {
2414 found_extent = 1;
2415 break;
2416 }
2417 if (path->slots[0] - extent_slot > 5)
2418 break;
2419 }
2420 if (!found_extent) {
2421 ret = remove_extent_backref(trans, extent_root, path);
2422 BUG_ON(ret);
2423 btrfs_release_path(extent_root, path);
2424 ret = btrfs_search_slot(trans, extent_root,
2425 &key, path, -1, 1);
2426 if (ret) {
2427 printk(KERN_ERR "umm, got %d back from search"
2428 ", was looking for %llu\n", ret,
2429 (unsigned long long)bytenr);
2430 btrfs_print_leaf(extent_root, path->nodes[0]);
2431 }
2432 BUG_ON(ret);
2433 extent_slot = path->slots[0];
2434 }
2435 } else {
2436 btrfs_print_leaf(extent_root, path->nodes[0]);
2437 WARN_ON(1);
2438 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
2439 "root %llu gen %llu owner %llu\n",
2440 (unsigned long long)bytenr,
2441 (unsigned long long)root_objectid,
2442 (unsigned long long)ref_generation,
2443 (unsigned long long)owner_objectid);
2444 }
2445
2446 leaf = path->nodes[0];
2447 ei = btrfs_item_ptr(leaf, extent_slot,
2448 struct btrfs_extent_item);
2449 refs = btrfs_extent_refs(leaf, ei);
2450 BUG_ON(refs == 0);
2451 refs -= 1;
2452 btrfs_set_extent_refs(leaf, ei, refs);
2453
2454 btrfs_mark_buffer_dirty(leaf);
2455
2456 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
2457 struct btrfs_extent_ref *ref;
2458 ref = btrfs_item_ptr(leaf, path->slots[0],
2459 struct btrfs_extent_ref);
2460 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
2461 /* if the back ref and the extent are next to each other
2462 * they get deleted below in one shot
2463 */
2464 path->slots[0] = extent_slot;
2465 num_to_del = 2;
2466 } else if (found_extent) {
2467 /* otherwise delete the extent back ref */
2468 ret = remove_extent_backref(trans, extent_root, path);
2469 BUG_ON(ret);
2470 /* if refs are 0, we need to setup the path for deletion */
2471 if (refs == 0) {
2472 btrfs_release_path(extent_root, path);
2473 ret = btrfs_search_slot(trans, extent_root, &key, path,
2474 -1, 1);
2475 BUG_ON(ret);
2476 }
2477 }
2478
2479 if (refs == 0) {
2480 u64 super_used;
2481 u64 root_used;
2482
2483 if (pin) {
2484 mutex_lock(&root->fs_info->pinned_mutex);
2485 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
2486 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
2487 mutex_unlock(&root->fs_info->pinned_mutex);
2488 if (ret > 0)
2489 mark_free = 1;
2490 BUG_ON(ret < 0);
2491 }
2492 /* block accounting for super block */
2493 spin_lock(&info->delalloc_lock);
2494 super_used = btrfs_super_bytes_used(&info->super_copy);
2495 btrfs_set_super_bytes_used(&info->super_copy,
2496 super_used - num_bytes);
2497
2498 /* block accounting for root item */
2499 root_used = btrfs_root_used(&root->root_item);
2500 btrfs_set_root_used(&root->root_item,
2501 root_used - num_bytes);
2502 spin_unlock(&info->delalloc_lock);
2503 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2504 num_to_del);
2505 BUG_ON(ret);
2506 btrfs_release_path(extent_root, path);
2507
2508 if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
2509 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
2510 BUG_ON(ret);
2511 }
2512
2513 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
2514 mark_free);
2515 BUG_ON(ret);
2516 }
2517 btrfs_free_path(path);
2518 finish_current_insert(trans, extent_root, 0);
2519 return ret;
2520}
2521
2522/*
2523 * find all the blocks marked as pending in the radix tree and remove
2524 * them from the extent map
2525 */
2526static int del_pending_extents(struct btrfs_trans_handle *trans,
2527 struct btrfs_root *extent_root, int all)
2528{
2529 int ret;
2530 int err = 0;
2531 u64 start;
2532 u64 end;
2533 u64 priv;
2534 u64 search = 0;
2535 int nr = 0, skipped = 0;
2536 struct extent_io_tree *pending_del;
2537 struct extent_io_tree *extent_ins;
2538 struct pending_extent_op *extent_op;
2539 struct btrfs_fs_info *info = extent_root->fs_info;
2540 struct list_head delete_list;
2541
2542 INIT_LIST_HEAD(&delete_list);
2543 extent_ins = &extent_root->fs_info->extent_ins;
2544 pending_del = &extent_root->fs_info->pending_del;
2545
2546again:
2547 mutex_lock(&info->extent_ins_mutex);
2548 while (1) {
2549 ret = find_first_extent_bit(pending_del, search, &start, &end,
2550 EXTENT_WRITEBACK);
2551 if (ret) {
2552 if (all && skipped && !nr) {
2553 search = 0;
2554 continue;
2555 }
2556 mutex_unlock(&info->extent_ins_mutex);
2557 break;
2558 }
2559
2560 ret = try_lock_extent(extent_ins, start, end, GFP_NOFS);
2561 if (!ret) {
2562 search = end+1;
2563 skipped = 1;
2564
2565 if (need_resched()) {
2566 mutex_unlock(&info->extent_ins_mutex);
2567 cond_resched();
2568 mutex_lock(&info->extent_ins_mutex);
2569 }
2570
2571 continue;
2572 }
2573 BUG_ON(ret < 0);
2574
2575 ret = get_state_private(pending_del, start, &priv);
2576 BUG_ON(ret);
2577 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2578
2579 clear_extent_bits(pending_del, start, end, EXTENT_WRITEBACK,
2580 GFP_NOFS);
2581 if (!test_range_bit(extent_ins, start, end,
2582 EXTENT_WRITEBACK, 0)) {
2583 list_add_tail(&extent_op->list, &delete_list);
2584 nr++;
2585 } else {
2586 kfree(extent_op);
2587
2588 ret = get_state_private(&info->extent_ins, start,
2589 &priv);
2590 BUG_ON(ret);
2591 extent_op = (struct pending_extent_op *)
2592 (unsigned long)priv;
2593
2594 clear_extent_bits(&info->extent_ins, start, end,
2595 EXTENT_WRITEBACK, GFP_NOFS);
2596
2597 if (extent_op->type == PENDING_BACKREF_UPDATE) {
2598 list_add_tail(&extent_op->list, &delete_list);
2599 search = end + 1;
2600 nr++;
2601 continue;
2602 }
2603
2604 mutex_lock(&extent_root->fs_info->pinned_mutex);
2605 ret = pin_down_bytes(trans, extent_root, start,
2606 end + 1 - start, 0);
2607 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2608
2609 ret = update_block_group(trans, extent_root, start,
2610 end + 1 - start, 0, ret > 0);
2611
2612 unlock_extent(extent_ins, start, end, GFP_NOFS);
2613 BUG_ON(ret);
2614 kfree(extent_op);
2615 }
2616 if (ret)
2617 err = ret;
2618
2619 search = end + 1;
2620
2621 if (need_resched()) {
2622 mutex_unlock(&info->extent_ins_mutex);
2623 cond_resched();
2624 mutex_lock(&info->extent_ins_mutex);
2625 }
2626 }
2627
2628 if (nr) {
2629 ret = free_extents(trans, extent_root, &delete_list);
2630 BUG_ON(ret);
2631 }
2632
2633 if (all && skipped) {
2634 INIT_LIST_HEAD(&delete_list);
2635 search = 0;
2636 nr = 0;
2637 goto again;
2638 }
2639
2640 return err;
2641}
2642
2643/*
2644 * remove an extent from the root, returns 0 on success
2645 */
2646static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
2647 struct btrfs_root *root,
2648 u64 bytenr, u64 num_bytes, u64 parent,
2649 u64 root_objectid, u64 ref_generation,
2650 u64 owner_objectid, int pin)
2651{
2652 struct btrfs_root *extent_root = root->fs_info->extent_root;
2653 int pending_ret;
2654 int ret;
2655
2656 WARN_ON(num_bytes < root->sectorsize);
2657 if (root == extent_root) {
2658 struct pending_extent_op *extent_op = NULL;
2659
2660 mutex_lock(&root->fs_info->extent_ins_mutex);
2661 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
2662 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
2663 u64 priv;
2664 ret = get_state_private(&root->fs_info->extent_ins,
2665 bytenr, &priv);
2666 BUG_ON(ret);
2667 extent_op = (struct pending_extent_op *)
2668 (unsigned long)priv;
2669
2670 extent_op->del = 1;
2671 if (extent_op->type == PENDING_EXTENT_INSERT) {
2672 mutex_unlock(&root->fs_info->extent_ins_mutex);
2673 return 0;
2674 }
2675 }
2676
2677 if (extent_op) {
2678 ref_generation = extent_op->orig_generation;
2679 parent = extent_op->orig_parent;
2680 }
2681
2682 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2683 BUG_ON(!extent_op);
2684
2685 extent_op->type = PENDING_EXTENT_DELETE;
2686 extent_op->bytenr = bytenr;
2687 extent_op->num_bytes = num_bytes;
2688 extent_op->parent = parent;
2689 extent_op->orig_parent = parent;
2690 extent_op->generation = ref_generation;
2691 extent_op->orig_generation = ref_generation;
2692 extent_op->level = (int)owner_objectid;
2693 INIT_LIST_HEAD(&extent_op->list);
2694 extent_op->del = 0;
2695
2696 set_extent_bits(&root->fs_info->pending_del,
2697 bytenr, bytenr + num_bytes - 1,
2698 EXTENT_WRITEBACK, GFP_NOFS);
2699 set_state_private(&root->fs_info->pending_del,
2700 bytenr, (unsigned long)extent_op);
2701 mutex_unlock(&root->fs_info->extent_ins_mutex);
2702 return 0;
2703 }
2704 /* if metadata always pin */
2705 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2706 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
2707 struct btrfs_block_group_cache *cache;
2708
2709 /* btrfs_free_reserved_extent */
2710 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
2711 BUG_ON(!cache);
2712 btrfs_add_free_space(cache, bytenr, num_bytes);
2713 put_block_group(cache);
2714 update_reserved_extents(root, bytenr, num_bytes, 0);
2715 return 0;
2716 }
2717 pin = 1;
2718 }
2719
2720 /* if data pin when any transaction has committed this */
2721 if (ref_generation != trans->transid)
2722 pin = 1;
2723
2724 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
2725 root_objectid, ref_generation,
2726 owner_objectid, pin, pin == 0);
2727
2728 finish_current_insert(trans, root->fs_info->extent_root, 0);
2729 pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0);
2730 return ret ? ret : pending_ret;
2731}
2732
2733int btrfs_free_extent(struct btrfs_trans_handle *trans,
2734 struct btrfs_root *root,
2735 u64 bytenr, u64 num_bytes, u64 parent,
2736 u64 root_objectid, u64 ref_generation,
2737 u64 owner_objectid, int pin)
2738{
2739 int ret;
2740
2741 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
2742 root_objectid, ref_generation,
2743 owner_objectid, pin);
2744 return ret;
2745}
2746
2747static u64 stripe_align(struct btrfs_root *root, u64 val)
2748{
2749 u64 mask = ((u64)root->stripesize - 1);
2750 u64 ret = (val + mask) & ~mask;
2751 return ret;
2752}
2753
2754/*
2755 * walks the btree of allocated extents and find a hole of a given size.
2756 * The key ins is changed to record the hole:
2757 * ins->objectid == block start
2758 * ins->flags = BTRFS_EXTENT_ITEM_KEY
2759 * ins->offset == number of blocks
2760 * Any available blocks before search_start are skipped.
2761 */
2762static noinline int find_free_extent(struct btrfs_trans_handle *trans,
2763 struct btrfs_root *orig_root,
2764 u64 num_bytes, u64 empty_size,
2765 u64 search_start, u64 search_end,
2766 u64 hint_byte, struct btrfs_key *ins,
2767 u64 exclude_start, u64 exclude_nr,
2768 int data)
2769{
2770 int ret = 0;
2771 struct btrfs_root *root = orig_root->fs_info->extent_root;
2772 u64 total_needed = num_bytes;
2773 u64 *last_ptr = NULL;
2774 u64 last_wanted = 0;
2775 struct btrfs_block_group_cache *block_group = NULL;
2776 int chunk_alloc_done = 0;
2777 int empty_cluster = 2 * 1024 * 1024;
2778 int allowed_chunk_alloc = 0;
2779 struct list_head *head = NULL, *cur = NULL;
2780 int loop = 0;
2781 int extra_loop = 0;
2782 struct btrfs_space_info *space_info;
2783
2784 WARN_ON(num_bytes < root->sectorsize);
2785 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
2786 ins->objectid = 0;
2787 ins->offset = 0;
2788
2789 if (orig_root->ref_cows || empty_size)
2790 allowed_chunk_alloc = 1;
2791
2792 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2793 last_ptr = &root->fs_info->last_alloc;
2794 empty_cluster = 64 * 1024;
2795 }
2796
2797 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
2798 last_ptr = &root->fs_info->last_data_alloc;
2799
2800 if (last_ptr) {
2801 if (*last_ptr) {
2802 hint_byte = *last_ptr;
2803 last_wanted = *last_ptr;
2804 } else
2805 empty_size += empty_cluster;
2806 } else {
2807 empty_cluster = 0;
2808 }
2809 search_start = max(search_start, first_logical_byte(root, 0));
2810 search_start = max(search_start, hint_byte);
2811
2812 if (last_wanted && search_start != last_wanted) {
2813 last_wanted = 0;
2814 empty_size += empty_cluster;
2815 }
2816
2817 total_needed += empty_size;
2818 block_group = btrfs_lookup_block_group(root->fs_info, search_start);
2819 if (!block_group)
2820 block_group = btrfs_lookup_first_block_group(root->fs_info,
2821 search_start);
2822 space_info = __find_space_info(root->fs_info, data);
2823
2824 down_read(&space_info->groups_sem);
2825 while (1) {
2826 struct btrfs_free_space *free_space;
2827 /*
2828 * the only way this happens if our hint points to a block
2829 * group thats not of the proper type, while looping this
2830 * should never happen
2831 */
2832 if (empty_size)
2833 extra_loop = 1;
2834
2835 if (!block_group)
2836 goto new_group_no_lock;
2837
2838 if (unlikely(!block_group->cached)) {
2839 mutex_lock(&block_group->cache_mutex);
2840 ret = cache_block_group(root, block_group);
2841 mutex_unlock(&block_group->cache_mutex);
2842 if (ret)
2843 break;
2844 }
2845
2846 mutex_lock(&block_group->alloc_mutex);
2847 if (unlikely(!block_group_bits(block_group, data)))
2848 goto new_group;
2849
2850 if (unlikely(block_group->ro))
2851 goto new_group;
2852
2853 free_space = btrfs_find_free_space(block_group, search_start,
2854 total_needed);
2855 if (free_space) {
2856 u64 start = block_group->key.objectid;
2857 u64 end = block_group->key.objectid +
2858 block_group->key.offset;
2859
2860 search_start = stripe_align(root, free_space->offset);
2861
2862 /* move on to the next group */
2863 if (search_start + num_bytes >= search_end)
2864 goto new_group;
2865
2866 /* move on to the next group */
2867 if (search_start + num_bytes > end)
2868 goto new_group;
2869
2870 if (last_wanted && search_start != last_wanted) {
2871 total_needed += empty_cluster;
2872 empty_size += empty_cluster;
2873 last_wanted = 0;
2874 /*
2875 * if search_start is still in this block group
2876 * then we just re-search this block group
2877 */
2878 if (search_start >= start &&
2879 search_start < end) {
2880 mutex_unlock(&block_group->alloc_mutex);
2881 continue;
2882 }
2883
2884 /* else we go to the next block group */
2885 goto new_group;
2886 }
2887
2888 if (exclude_nr > 0 &&
2889 (search_start + num_bytes > exclude_start &&
2890 search_start < exclude_start + exclude_nr)) {
2891 search_start = exclude_start + exclude_nr;
2892 /*
2893 * if search_start is still in this block group
2894 * then we just re-search this block group
2895 */
2896 if (search_start >= start &&
2897 search_start < end) {
2898 mutex_unlock(&block_group->alloc_mutex);
2899 last_wanted = 0;
2900 continue;
2901 }
2902
2903 /* else we go to the next block group */
2904 goto new_group;
2905 }
2906
2907 ins->objectid = search_start;
2908 ins->offset = num_bytes;
2909
2910 btrfs_remove_free_space_lock(block_group, search_start,
2911 num_bytes);
2912 /* we are all good, lets return */
2913 mutex_unlock(&block_group->alloc_mutex);
2914 break;
2915 }
2916new_group:
2917 mutex_unlock(&block_group->alloc_mutex);
2918 put_block_group(block_group);
2919 block_group = NULL;
2920new_group_no_lock:
2921 /* don't try to compare new allocations against the
2922 * last allocation any more
2923 */
2924 last_wanted = 0;
2925
2926 /*
2927 * Here's how this works.
2928 * loop == 0: we were searching a block group via a hint
2929 * and didn't find anything, so we start at
2930 * the head of the block groups and keep searching
2931 * loop == 1: we're searching through all of the block groups
2932 * if we hit the head again we have searched
2933 * all of the block groups for this space and we
2934 * need to try and allocate, if we cant error out.
2935 * loop == 2: we allocated more space and are looping through
2936 * all of the block groups again.
2937 */
2938 if (loop == 0) {
2939 head = &space_info->block_groups;
2940 cur = head->next;
2941 loop++;
2942 } else if (loop == 1 && cur == head) {
2943 int keep_going;
2944
2945 /* at this point we give up on the empty_size
2946 * allocations and just try to allocate the min
2947 * space.
2948 *
2949 * The extra_loop field was set if an empty_size
2950 * allocation was attempted above, and if this
2951 * is try we need to try the loop again without
2952 * the additional empty_size.
2953 */
2954 total_needed -= empty_size;
2955 empty_size = 0;
2956 keep_going = extra_loop;
2957 loop++;
2958
2959 if (allowed_chunk_alloc && !chunk_alloc_done) {
2960 up_read(&space_info->groups_sem);
2961 ret = do_chunk_alloc(trans, root, num_bytes +
2962 2 * 1024 * 1024, data, 1);
2963 down_read(&space_info->groups_sem);
2964 if (ret < 0)
2965 goto loop_check;
2966 head = &space_info->block_groups;
2967 /*
2968 * we've allocated a new chunk, keep
2969 * trying
2970 */
2971 keep_going = 1;
2972 chunk_alloc_done = 1;
2973 } else if (!allowed_chunk_alloc) {
2974 space_info->force_alloc = 1;
2975 }
2976loop_check:
2977 if (keep_going) {
2978 cur = head->next;
2979 extra_loop = 0;
2980 } else {
2981 break;
2982 }
2983 } else if (cur == head) {
2984 break;
2985 }
2986
2987 block_group = list_entry(cur, struct btrfs_block_group_cache,
2988 list);
2989 atomic_inc(&block_group->count);
2990
2991 search_start = block_group->key.objectid;
2992 cur = cur->next;
2993 }
2994
2995 /* we found what we needed */
2996 if (ins->objectid) {
2997 if (!(data & BTRFS_BLOCK_GROUP_DATA))
2998 trans->block_group = block_group->key.objectid;
2999
3000 if (last_ptr)
3001 *last_ptr = ins->objectid + ins->offset;
3002 ret = 0;
3003 } else if (!ret) {
3004 printk(KERN_ERR "btrfs searching for %llu bytes, "
3005 "num_bytes %llu, loop %d, allowed_alloc %d\n",
3006 (unsigned long long)total_needed,
3007 (unsigned long long)num_bytes,
3008 loop, allowed_chunk_alloc);
3009 ret = -ENOSPC;
3010 }
3011 if (block_group)
3012 put_block_group(block_group);
3013
3014 up_read(&space_info->groups_sem);
3015 return ret;
3016}
3017
3018static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
3019{
3020 struct btrfs_block_group_cache *cache;
3021 struct list_head *l;
3022
3023 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
3024 (unsigned long long)(info->total_bytes - info->bytes_used -
3025 info->bytes_pinned - info->bytes_reserved),
3026 (info->full) ? "" : "not ");
3027
3028 down_read(&info->groups_sem);
3029 list_for_each(l, &info->block_groups) {
3030 cache = list_entry(l, struct btrfs_block_group_cache, list);
3031 spin_lock(&cache->lock);
3032 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
3033 "%llu pinned %llu reserved\n",
3034 (unsigned long long)cache->key.objectid,
3035 (unsigned long long)cache->key.offset,
3036 (unsigned long long)btrfs_block_group_used(&cache->item),
3037 (unsigned long long)cache->pinned,
3038 (unsigned long long)cache->reserved);
3039 btrfs_dump_free_space(cache, bytes);
3040 spin_unlock(&cache->lock);
3041 }
3042 up_read(&info->groups_sem);
3043}
3044
3045static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3046 struct btrfs_root *root,
3047 u64 num_bytes, u64 min_alloc_size,
3048 u64 empty_size, u64 hint_byte,
3049 u64 search_end, struct btrfs_key *ins,
3050 u64 data)
3051{
3052 int ret;
3053 u64 search_start = 0;
3054 u64 alloc_profile;
3055 struct btrfs_fs_info *info = root->fs_info;
3056
3057 if (data) {
3058 alloc_profile = info->avail_data_alloc_bits &
3059 info->data_alloc_profile;
3060 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
3061 } else if (root == root->fs_info->chunk_root) {
3062 alloc_profile = info->avail_system_alloc_bits &
3063 info->system_alloc_profile;
3064 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
3065 } else {
3066 alloc_profile = info->avail_metadata_alloc_bits &
3067 info->metadata_alloc_profile;
3068 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
3069 }
3070again:
3071 data = btrfs_reduce_alloc_profile(root, data);
3072 /*
3073 * the only place that sets empty_size is btrfs_realloc_node, which
3074 * is not called recursively on allocations
3075 */
3076 if (empty_size || root->ref_cows) {
3077 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
3078 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3079 2 * 1024 * 1024,
3080 BTRFS_BLOCK_GROUP_METADATA |
3081 (info->metadata_alloc_profile &
3082 info->avail_metadata_alloc_bits), 0);
3083 }
3084 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3085 num_bytes + 2 * 1024 * 1024, data, 0);
3086 }
3087
3088 WARN_ON(num_bytes < root->sectorsize);
3089 ret = find_free_extent(trans, root, num_bytes, empty_size,
3090 search_start, search_end, hint_byte, ins,
3091 trans->alloc_exclude_start,
3092 trans->alloc_exclude_nr, data);
3093
3094 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
3095 num_bytes = num_bytes >> 1;
3096 num_bytes = num_bytes & ~(root->sectorsize - 1);
3097 num_bytes = max(num_bytes, min_alloc_size);
3098 do_chunk_alloc(trans, root->fs_info->extent_root,
3099 num_bytes, data, 1);
3100 goto again;
3101 }
3102 if (ret) {
3103 struct btrfs_space_info *sinfo;
3104
3105 sinfo = __find_space_info(root->fs_info, data);
3106 printk(KERN_ERR "btrfs allocation failed flags %llu, "
3107 "wanted %llu\n", (unsigned long long)data,
3108 (unsigned long long)num_bytes);
3109 dump_space_info(sinfo, num_bytes);
3110 BUG();
3111 }
3112
3113 return ret;
3114}
3115
3116int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
3117{
3118 struct btrfs_block_group_cache *cache;
3119 int ret = 0;
3120
3121 cache = btrfs_lookup_block_group(root->fs_info, start);
3122 if (!cache) {
3123 printk(KERN_ERR "Unable to find block group for %llu\n",
3124 (unsigned long long)start);
3125 return -ENOSPC;
3126 }
3127
3128 ret = btrfs_discard_extent(root, start, len);
3129
3130 btrfs_add_free_space(cache, start, len);
3131 put_block_group(cache);
3132 update_reserved_extents(root, start, len, 0);
3133
3134 return ret;
3135}
3136
3137int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3138 struct btrfs_root *root,
3139 u64 num_bytes, u64 min_alloc_size,
3140 u64 empty_size, u64 hint_byte,
3141 u64 search_end, struct btrfs_key *ins,
3142 u64 data)
3143{
3144 int ret;
3145 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
3146 empty_size, hint_byte, search_end, ins,
3147 data);
3148 update_reserved_extents(root, ins->objectid, ins->offset, 1);
3149 return ret;
3150}
3151
3152static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
3153 struct btrfs_root *root, u64 parent,
3154 u64 root_objectid, u64 ref_generation,
3155 u64 owner, struct btrfs_key *ins)
3156{
3157 int ret;
3158 int pending_ret;
3159 u64 super_used;
3160 u64 root_used;
3161 u64 num_bytes = ins->offset;
3162 u32 sizes[2];
3163 struct btrfs_fs_info *info = root->fs_info;
3164 struct btrfs_root *extent_root = info->extent_root;
3165 struct btrfs_extent_item *extent_item;
3166 struct btrfs_extent_ref *ref;
3167 struct btrfs_path *path;
3168 struct btrfs_key keys[2];
3169
3170 if (parent == 0)
3171 parent = ins->objectid;
3172
3173 /* block accounting for super block */
3174 spin_lock(&info->delalloc_lock);
3175 super_used = btrfs_super_bytes_used(&info->super_copy);
3176 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
3177
3178 /* block accounting for root item */
3179 root_used = btrfs_root_used(&root->root_item);
3180 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
3181 spin_unlock(&info->delalloc_lock);
3182
3183 if (root == extent_root) {
3184 struct pending_extent_op *extent_op;
3185
3186 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
3187 BUG_ON(!extent_op);
3188
3189 extent_op->type = PENDING_EXTENT_INSERT;
3190 extent_op->bytenr = ins->objectid;
3191 extent_op->num_bytes = ins->offset;
3192 extent_op->parent = parent;
3193 extent_op->orig_parent = 0;
3194 extent_op->generation = ref_generation;
3195 extent_op->orig_generation = 0;
3196 extent_op->level = (int)owner;
3197 INIT_LIST_HEAD(&extent_op->list);
3198 extent_op->del = 0;
3199
3200 mutex_lock(&root->fs_info->extent_ins_mutex);
3201 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
3202 ins->objectid + ins->offset - 1,
3203 EXTENT_WRITEBACK, GFP_NOFS);
3204 set_state_private(&root->fs_info->extent_ins,
3205 ins->objectid, (unsigned long)extent_op);
3206 mutex_unlock(&root->fs_info->extent_ins_mutex);
3207 goto update_block;
3208 }
3209
3210 memcpy(&keys[0], ins, sizeof(*ins));
3211 keys[1].objectid = ins->objectid;
3212 keys[1].type = BTRFS_EXTENT_REF_KEY;
3213 keys[1].offset = parent;
3214 sizes[0] = sizeof(*extent_item);
3215 sizes[1] = sizeof(*ref);
3216
3217 path = btrfs_alloc_path();
3218 BUG_ON(!path);
3219
3220 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
3221 sizes, 2);
3222 BUG_ON(ret);
3223
3224 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3225 struct btrfs_extent_item);
3226 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
3227 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3228 struct btrfs_extent_ref);
3229
3230 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
3231 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
3232 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
3233 btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
3234
3235 btrfs_mark_buffer_dirty(path->nodes[0]);
3236
3237 trans->alloc_exclude_start = 0;
3238 trans->alloc_exclude_nr = 0;
3239 btrfs_free_path(path);
3240 finish_current_insert(trans, extent_root, 0);
3241 pending_ret = del_pending_extents(trans, extent_root, 0);
3242
3243 if (ret)
3244 goto out;
3245 if (pending_ret) {
3246 ret = pending_ret;
3247 goto out;
3248 }
3249
3250update_block:
3251 ret = update_block_group(trans, root, ins->objectid,
3252 ins->offset, 1, 0);
3253 if (ret) {
3254 printk(KERN_ERR "btrfs update block group failed for %llu "
3255 "%llu\n", (unsigned long long)ins->objectid,
3256 (unsigned long long)ins->offset);
3257 BUG();
3258 }
3259out:
3260 return ret;
3261}
3262
3263int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
3264 struct btrfs_root *root, u64 parent,
3265 u64 root_objectid, u64 ref_generation,
3266 u64 owner, struct btrfs_key *ins)
3267{
3268 int ret;
3269
3270 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
3271 return 0;
3272 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3273 ref_generation, owner, ins);
3274 update_reserved_extents(root, ins->objectid, ins->offset, 0);
3275 return ret;
3276}
3277
3278/*
3279 * this is used by the tree logging recovery code. It records that
3280 * an extent has been allocated and makes sure to clear the free
3281 * space cache bits as well
3282 */
3283int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
3284 struct btrfs_root *root, u64 parent,
3285 u64 root_objectid, u64 ref_generation,
3286 u64 owner, struct btrfs_key *ins)
3287{
3288 int ret;
3289 struct btrfs_block_group_cache *block_group;
3290
3291 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
3292 mutex_lock(&block_group->cache_mutex);
3293 cache_block_group(root, block_group);
3294 mutex_unlock(&block_group->cache_mutex);
3295
3296 ret = btrfs_remove_free_space(block_group, ins->objectid,
3297 ins->offset);
3298 BUG_ON(ret);
3299 put_block_group(block_group);
3300 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3301 ref_generation, owner, ins);
3302 return ret;
3303}
3304
3305/*
3306 * finds a free extent and does all the dirty work required for allocation
3307 * returns the key for the extent through ins, and a tree buffer for
3308 * the first block of the extent through buf.
3309 *
3310 * returns 0 if everything worked, non-zero otherwise.
3311 */
3312int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
3313 struct btrfs_root *root,
3314 u64 num_bytes, u64 parent, u64 min_alloc_size,
3315 u64 root_objectid, u64 ref_generation,
3316 u64 owner_objectid, u64 empty_size, u64 hint_byte,
3317 u64 search_end, struct btrfs_key *ins, u64 data)
3318{
3319 int ret;
3320
3321 ret = __btrfs_reserve_extent(trans, root, num_bytes,
3322 min_alloc_size, empty_size, hint_byte,
3323 search_end, ins, data);
3324 BUG_ON(ret);
3325 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
3326 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
3327 root_objectid, ref_generation,
3328 owner_objectid, ins);
3329 BUG_ON(ret);
3330
3331 } else {
3332 update_reserved_extents(root, ins->objectid, ins->offset, 1);
3333 }
3334 return ret;
3335}
3336
3337struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
3338 struct btrfs_root *root,
3339 u64 bytenr, u32 blocksize)
3340{
3341 struct extent_buffer *buf;
3342
3343 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
3344 if (!buf)
3345 return ERR_PTR(-ENOMEM);
3346 btrfs_set_header_generation(buf, trans->transid);
3347 btrfs_tree_lock(buf);
3348 clean_tree_block(trans, root, buf);
3349 btrfs_set_buffer_uptodate(buf);
3350 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
3351 set_extent_dirty(&root->dirty_log_pages, buf->start,
3352 buf->start + buf->len - 1, GFP_NOFS);
3353 } else {
3354 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
3355 buf->start + buf->len - 1, GFP_NOFS);
3356 }
3357 trans->blocks_used++;
3358 return buf;
3359}
3360
3361/*
3362 * helper function to allocate a block for a given tree
3363 * returns the tree buffer or NULL.
3364 */
3365struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
3366 struct btrfs_root *root,
3367 u32 blocksize, u64 parent,
3368 u64 root_objectid,
3369 u64 ref_generation,
3370 int level,
3371 u64 hint,
3372 u64 empty_size)
3373{
3374 struct btrfs_key ins;
3375 int ret;
3376 struct extent_buffer *buf;
3377
3378 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
3379 root_objectid, ref_generation, level,
3380 empty_size, hint, (u64)-1, &ins, 0);
3381 if (ret) {
3382 BUG_ON(ret > 0);
3383 return ERR_PTR(ret);
3384 }
3385
3386 buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
3387 return buf;
3388}
3389
3390int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
3391 struct btrfs_root *root, struct extent_buffer *leaf)
3392{
3393 u64 leaf_owner;
3394 u64 leaf_generation;
3395 struct btrfs_key key;
3396 struct btrfs_file_extent_item *fi;
3397 int i;
3398 int nritems;
3399 int ret;
3400
3401 BUG_ON(!btrfs_is_leaf(leaf));
3402 nritems = btrfs_header_nritems(leaf);
3403 leaf_owner = btrfs_header_owner(leaf);
3404 leaf_generation = btrfs_header_generation(leaf);
3405
3406 for (i = 0; i < nritems; i++) {
3407 u64 disk_bytenr;
3408 cond_resched();
3409
3410 btrfs_item_key_to_cpu(leaf, &key, i);
3411 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
3412 continue;
3413 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
3414 if (btrfs_file_extent_type(leaf, fi) ==
3415 BTRFS_FILE_EXTENT_INLINE)
3416 continue;
3417 /*
3418 * FIXME make sure to insert a trans record that
3419 * repeats the snapshot del on crash
3420 */
3421 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
3422 if (disk_bytenr == 0)
3423 continue;
3424
3425 ret = __btrfs_free_extent(trans, root, disk_bytenr,
3426 btrfs_file_extent_disk_num_bytes(leaf, fi),
3427 leaf->start, leaf_owner, leaf_generation,
3428 key.objectid, 0);
3429 BUG_ON(ret);
3430
3431 atomic_inc(&root->fs_info->throttle_gen);
3432 wake_up(&root->fs_info->transaction_throttle);
3433 cond_resched();
3434 }
3435 return 0;
3436}
3437
3438static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
3439 struct btrfs_root *root,
3440 struct btrfs_leaf_ref *ref)
3441{
3442 int i;
3443 int ret;
3444 struct btrfs_extent_info *info = ref->extents;
3445
3446 for (i = 0; i < ref->nritems; i++) {
3447 ret = __btrfs_free_extent(trans, root, info->bytenr,
3448 info->num_bytes, ref->bytenr,
3449 ref->owner, ref->generation,
3450 info->objectid, 0);
3451
3452 atomic_inc(&root->fs_info->throttle_gen);
3453 wake_up(&root->fs_info->transaction_throttle);
3454 cond_resched();
3455
3456 BUG_ON(ret);
3457 info++;
3458 }
3459
3460 return 0;
3461}
3462
3463static int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start,
3464 u64 len, u32 *refs)
3465{
3466 int ret;
3467
3468 ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
3469 BUG_ON(ret);
3470
3471#if 0 /* some debugging code in case we see problems here */
3472 /* if the refs count is one, it won't get increased again. But
3473 * if the ref count is > 1, someone may be decreasing it at
3474 * the same time we are.
3475 */
3476 if (*refs != 1) {
3477 struct extent_buffer *eb = NULL;
3478 eb = btrfs_find_create_tree_block(root, start, len);
3479 if (eb)
3480 btrfs_tree_lock(eb);
3481
3482 mutex_lock(&root->fs_info->alloc_mutex);
3483 ret = lookup_extent_ref(NULL, root, start, len, refs);
3484 BUG_ON(ret);
3485 mutex_unlock(&root->fs_info->alloc_mutex);
3486
3487 if (eb) {
3488 btrfs_tree_unlock(eb);
3489 free_extent_buffer(eb);
3490 }
3491 if (*refs == 1) {
3492 printk(KERN_ERR "btrfs block %llu went down to one "
3493 "during drop_snap\n", (unsigned long long)start);
3494 }
3495
3496 }
3497#endif
3498
3499 cond_resched();
3500 return ret;
3501}
3502
3503/*
3504 * helper function for drop_snapshot, this walks down the tree dropping ref
3505 * counts as it goes.
3506 */
3507static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
3508 struct btrfs_root *root,
3509 struct btrfs_path *path, int *level)
3510{
3511 u64 root_owner;
3512 u64 root_gen;
3513 u64 bytenr;
3514 u64 ptr_gen;
3515 struct extent_buffer *next;
3516 struct extent_buffer *cur;
3517 struct extent_buffer *parent;
3518 struct btrfs_leaf_ref *ref;
3519 u32 blocksize;
3520 int ret;
3521 u32 refs;
3522
3523 WARN_ON(*level < 0);
3524 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3525 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
3526 path->nodes[*level]->len, &refs);
3527 BUG_ON(ret);
3528 if (refs > 1)
3529 goto out;
3530
3531 /*
3532 * walk down to the last node level and free all the leaves
3533 */
3534 while (*level >= 0) {
3535 WARN_ON(*level < 0);
3536 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3537 cur = path->nodes[*level];
3538
3539 if (btrfs_header_level(cur) != *level)
3540 WARN_ON(1);
3541
3542 if (path->slots[*level] >=
3543 btrfs_header_nritems(cur))
3544 break;
3545 if (*level == 0) {
3546 ret = btrfs_drop_leaf_ref(trans, root, cur);
3547 BUG_ON(ret);
3548 break;
3549 }
3550 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3551 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3552 blocksize = btrfs_level_size(root, *level - 1);
3553
3554 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
3555 BUG_ON(ret);
3556 if (refs != 1) {
3557 parent = path->nodes[*level];
3558 root_owner = btrfs_header_owner(parent);
3559 root_gen = btrfs_header_generation(parent);
3560 path->slots[*level]++;
3561
3562 ret = __btrfs_free_extent(trans, root, bytenr,
3563 blocksize, parent->start,
3564 root_owner, root_gen,
3565 *level - 1, 1);
3566 BUG_ON(ret);
3567
3568 atomic_inc(&root->fs_info->throttle_gen);
3569 wake_up(&root->fs_info->transaction_throttle);
3570 cond_resched();
3571
3572 continue;
3573 }
3574 /*
3575 * at this point, we have a single ref, and since the
3576 * only place referencing this extent is a dead root
3577 * the reference count should never go higher.
3578 * So, we don't need to check it again
3579 */
3580 if (*level == 1) {
3581 ref = btrfs_lookup_leaf_ref(root, bytenr);
3582 if (ref && ref->generation != ptr_gen) {
3583 btrfs_free_leaf_ref(root, ref);
3584 ref = NULL;
3585 }
3586 if (ref) {
3587 ret = cache_drop_leaf_ref(trans, root, ref);
3588 BUG_ON(ret);
3589 btrfs_remove_leaf_ref(root, ref);
3590 btrfs_free_leaf_ref(root, ref);
3591 *level = 0;
3592 break;
3593 }
3594 }
3595 next = btrfs_find_tree_block(root, bytenr, blocksize);
3596 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
3597 free_extent_buffer(next);
3598
3599 next = read_tree_block(root, bytenr, blocksize,
3600 ptr_gen);
3601 cond_resched();
3602#if 0
3603 /*
3604 * this is a debugging check and can go away
3605 * the ref should never go all the way down to 1
3606 * at this point
3607 */
3608 ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
3609 &refs);
3610 BUG_ON(ret);
3611 WARN_ON(refs != 1);
3612#endif
3613 }
3614 WARN_ON(*level <= 0);
3615 if (path->nodes[*level-1])
3616 free_extent_buffer(path->nodes[*level-1]);
3617 path->nodes[*level-1] = next;
3618 *level = btrfs_header_level(next);
3619 path->slots[*level] = 0;
3620 cond_resched();
3621 }
3622out:
3623 WARN_ON(*level < 0);
3624 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3625
3626 if (path->nodes[*level] == root->node) {
3627 parent = path->nodes[*level];
3628 bytenr = path->nodes[*level]->start;
3629 } else {
3630 parent = path->nodes[*level + 1];
3631 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
3632 }
3633
3634 blocksize = btrfs_level_size(root, *level);
3635 root_owner = btrfs_header_owner(parent);
3636 root_gen = btrfs_header_generation(parent);
3637
3638 ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
3639 parent->start, root_owner, root_gen,
3640 *level, 1);
3641 free_extent_buffer(path->nodes[*level]);
3642 path->nodes[*level] = NULL;
3643 *level += 1;
3644 BUG_ON(ret);
3645
3646 cond_resched();
3647 return 0;
3648}
3649
3650/*
3651 * helper function for drop_subtree, this function is similar to
3652 * walk_down_tree. The main difference is that it checks reference
3653 * counts while tree blocks are locked.
3654 */
3655static noinline int walk_down_subtree(struct btrfs_trans_handle *trans,
3656 struct btrfs_root *root,
3657 struct btrfs_path *path, int *level)
3658{
3659 struct extent_buffer *next;
3660 struct extent_buffer *cur;
3661 struct extent_buffer *parent;
3662 u64 bytenr;
3663 u64 ptr_gen;
3664 u32 blocksize;
3665 u32 refs;
3666 int ret;
3667
3668 cur = path->nodes[*level];
3669 ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
3670 &refs);
3671 BUG_ON(ret);
3672 if (refs > 1)
3673 goto out;
3674
3675 while (*level >= 0) {
3676 cur = path->nodes[*level];
3677 if (*level == 0) {
3678 ret = btrfs_drop_leaf_ref(trans, root, cur);
3679 BUG_ON(ret);
3680 clean_tree_block(trans, root, cur);
3681 break;
3682 }
3683 if (path->slots[*level] >= btrfs_header_nritems(cur)) {
3684 clean_tree_block(trans, root, cur);
3685 break;
3686 }
3687
3688 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3689 blocksize = btrfs_level_size(root, *level - 1);
3690 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3691
3692 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
3693 btrfs_tree_lock(next);
3694
3695 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
3696 &refs);
3697 BUG_ON(ret);
3698 if (refs > 1) {
3699 parent = path->nodes[*level];
3700 ret = btrfs_free_extent(trans, root, bytenr,
3701 blocksize, parent->start,
3702 btrfs_header_owner(parent),
3703 btrfs_header_generation(parent),
3704 *level - 1, 1);
3705 BUG_ON(ret);
3706 path->slots[*level]++;
3707 btrfs_tree_unlock(next);
3708 free_extent_buffer(next);
3709 continue;
3710 }
3711
3712 *level = btrfs_header_level(next);
3713 path->nodes[*level] = next;
3714 path->slots[*level] = 0;
3715 path->locks[*level] = 1;
3716 cond_resched();
3717 }
3718out:
3719 parent = path->nodes[*level + 1];
3720 bytenr = path->nodes[*level]->start;
3721 blocksize = path->nodes[*level]->len;
3722
3723 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
3724 parent->start, btrfs_header_owner(parent),
3725 btrfs_header_generation(parent), *level, 1);
3726 BUG_ON(ret);
3727
3728 if (path->locks[*level]) {
3729 btrfs_tree_unlock(path->nodes[*level]);
3730 path->locks[*level] = 0;
3731 }
3732 free_extent_buffer(path->nodes[*level]);
3733 path->nodes[*level] = NULL;
3734 *level += 1;
3735 cond_resched();
3736 return 0;
3737}
3738
3739/*
3740 * helper for dropping snapshots. This walks back up the tree in the path
3741 * to find the first node higher up where we haven't yet gone through
3742 * all the slots
3743 */
3744static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
3745 struct btrfs_root *root,
3746 struct btrfs_path *path,
3747 int *level, int max_level)
3748{
3749 u64 root_owner;
3750 u64 root_gen;
3751 struct btrfs_root_item *root_item = &root->root_item;
3752 int i;
3753 int slot;
3754 int ret;
3755
3756 for (i = *level; i < max_level && path->nodes[i]; i++) {
3757 slot = path->slots[i];
3758 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3759 struct extent_buffer *node;
3760 struct btrfs_disk_key disk_key;
3761 node = path->nodes[i];
3762 path->slots[i]++;
3763 *level = i;
3764 WARN_ON(*level == 0);
3765 btrfs_node_key(node, &disk_key, path->slots[i]);
3766 memcpy(&root_item->drop_progress,
3767 &disk_key, sizeof(disk_key));
3768 root_item->drop_level = i;
3769 return 0;
3770 } else {
3771 struct extent_buffer *parent;
3772 if (path->nodes[*level] == root->node)
3773 parent = path->nodes[*level];
3774 else
3775 parent = path->nodes[*level + 1];
3776
3777 root_owner = btrfs_header_owner(parent);
3778 root_gen = btrfs_header_generation(parent);
3779
3780 clean_tree_block(trans, root, path->nodes[*level]);
3781 ret = btrfs_free_extent(trans, root,
3782 path->nodes[*level]->start,
3783 path->nodes[*level]->len,
3784 parent->start, root_owner,
3785 root_gen, *level, 1);
3786 BUG_ON(ret);
3787 if (path->locks[*level]) {
3788 btrfs_tree_unlock(path->nodes[*level]);
3789 path->locks[*level] = 0;
3790 }
3791 free_extent_buffer(path->nodes[*level]);
3792 path->nodes[*level] = NULL;
3793 *level = i + 1;
3794 }
3795 }
3796 return 1;
3797}
3798
3799/*
3800 * drop the reference count on the tree rooted at 'snap'. This traverses
3801 * the tree freeing any blocks that have a ref count of zero after being
3802 * decremented.
3803 */
3804int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
3805 *root)
3806{
3807 int ret = 0;
3808 int wret;
3809 int level;
3810 struct btrfs_path *path;
3811 int i;
3812 int orig_level;
3813 struct btrfs_root_item *root_item = &root->root_item;
3814
3815 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
3816 path = btrfs_alloc_path();
3817 BUG_ON(!path);
3818
3819 level = btrfs_header_level(root->node);
3820 orig_level = level;
3821 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3822 path->nodes[level] = root->node;
3823 extent_buffer_get(root->node);
3824 path->slots[level] = 0;
3825 } else {
3826 struct btrfs_key key;
3827 struct btrfs_disk_key found_key;
3828 struct extent_buffer *node;
3829
3830 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
3831 level = root_item->drop_level;
3832 path->lowest_level = level;
3833 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3834 if (wret < 0) {
3835 ret = wret;
3836 goto out;
3837 }
3838 node = path->nodes[level];
3839 btrfs_node_key(node, &found_key, path->slots[level]);
3840 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3841 sizeof(found_key)));
3842 /*
3843 * unlock our path, this is safe because only this
3844 * function is allowed to delete this snapshot
3845 */
3846 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3847 if (path->nodes[i] && path->locks[i]) {
3848 path->locks[i] = 0;
3849 btrfs_tree_unlock(path->nodes[i]);
3850 }
3851 }
3852 }
3853 while (1) {
3854 wret = walk_down_tree(trans, root, path, &level);
3855 if (wret > 0)
3856 break;
3857 if (wret < 0)
3858 ret = wret;
3859
3860 wret = walk_up_tree(trans, root, path, &level,
3861 BTRFS_MAX_LEVEL);
3862 if (wret > 0)
3863 break;
3864 if (wret < 0)
3865 ret = wret;
3866 if (trans->transaction->in_commit) {
3867 ret = -EAGAIN;
3868 break;
3869 }
3870 atomic_inc(&root->fs_info->throttle_gen);
3871 wake_up(&root->fs_info->transaction_throttle);
3872 }
3873 for (i = 0; i <= orig_level; i++) {
3874 if (path->nodes[i]) {
3875 free_extent_buffer(path->nodes[i]);
3876 path->nodes[i] = NULL;
3877 }
3878 }
3879out:
3880 btrfs_free_path(path);
3881 return ret;
3882}
3883
3884int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
3885 struct btrfs_root *root,
3886 struct extent_buffer *node,
3887 struct extent_buffer *parent)
3888{
3889 struct btrfs_path *path;
3890 int level;
3891 int parent_level;
3892 int ret = 0;
3893 int wret;
3894
3895 path = btrfs_alloc_path();
3896 BUG_ON(!path);
3897
3898 BUG_ON(!btrfs_tree_locked(parent));
3899 parent_level = btrfs_header_level(parent);
3900 extent_buffer_get(parent);
3901 path->nodes[parent_level] = parent;
3902 path->slots[parent_level] = btrfs_header_nritems(parent);
3903
3904 BUG_ON(!btrfs_tree_locked(node));
3905 level = btrfs_header_level(node);
3906 extent_buffer_get(node);
3907 path->nodes[level] = node;
3908 path->slots[level] = 0;
3909
3910 while (1) {
3911 wret = walk_down_subtree(trans, root, path, &level);
3912 if (wret < 0)
3913 ret = wret;
3914 if (wret != 0)
3915 break;
3916
3917 wret = walk_up_tree(trans, root, path, &level, parent_level);
3918 if (wret < 0)
3919 ret = wret;
3920 if (wret != 0)
3921 break;
3922 }
3923
3924 btrfs_free_path(path);
3925 return ret;
3926}
3927
3928static unsigned long calc_ra(unsigned long start, unsigned long last,
3929 unsigned long nr)
3930{
3931 return min(last, start + nr - 1);
3932}
3933
3934static noinline int relocate_inode_pages(struct inode *inode, u64 start,
3935 u64 len)
3936{
3937 u64 page_start;
3938 u64 page_end;
3939 unsigned long first_index;
3940 unsigned long last_index;
3941 unsigned long i;
3942 struct page *page;
3943 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3944 struct file_ra_state *ra;
3945 struct btrfs_ordered_extent *ordered;
3946 unsigned int total_read = 0;
3947 unsigned int total_dirty = 0;
3948 int ret = 0;
3949
3950 ra = kzalloc(sizeof(*ra), GFP_NOFS);
3951
3952 mutex_lock(&inode->i_mutex);
3953 first_index = start >> PAGE_CACHE_SHIFT;
3954 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3955
3956 /* make sure the dirty trick played by the caller work */
3957 ret = invalidate_inode_pages2_range(inode->i_mapping,
3958 first_index, last_index);
3959 if (ret)
3960 goto out_unlock;
3961
3962 file_ra_state_init(ra, inode->i_mapping);
3963
3964 for (i = first_index ; i <= last_index; i++) {
3965 if (total_read % ra->ra_pages == 0) {
3966 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
3967 calc_ra(i, last_index, ra->ra_pages));
3968 }
3969 total_read++;
3970again:
3971 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
3972 BUG_ON(1);
3973 page = grab_cache_page(inode->i_mapping, i);
3974 if (!page) {
3975 ret = -ENOMEM;
3976 goto out_unlock;
3977 }
3978 if (!PageUptodate(page)) {
3979 btrfs_readpage(NULL, page);
3980 lock_page(page);
3981 if (!PageUptodate(page)) {
3982 unlock_page(page);
3983 page_cache_release(page);
3984 ret = -EIO;
3985 goto out_unlock;
3986 }
3987 }
3988 wait_on_page_writeback(page);
3989
3990 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3991 page_end = page_start + PAGE_CACHE_SIZE - 1;
3992 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3993
3994 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3995 if (ordered) {
3996 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3997 unlock_page(page);
3998 page_cache_release(page);
3999 btrfs_start_ordered_extent(inode, ordered, 1);
4000 btrfs_put_ordered_extent(ordered);
4001 goto again;
4002 }
4003 set_page_extent_mapped(page);
4004
4005 if (i == first_index)
4006 set_extent_bits(io_tree, page_start, page_end,
4007 EXTENT_BOUNDARY, GFP_NOFS);
4008 btrfs_set_extent_delalloc(inode, page_start, page_end);
4009
4010 set_page_dirty(page);
4011 total_dirty++;
4012
4013 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4014 unlock_page(page);
4015 page_cache_release(page);
4016 }
4017
4018out_unlock:
4019 kfree(ra);
4020 mutex_unlock(&inode->i_mutex);
4021 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
4022 return ret;
4023}
4024
4025static noinline int relocate_data_extent(struct inode *reloc_inode,
4026 struct btrfs_key *extent_key,
4027 u64 offset)
4028{
4029 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4030 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
4031 struct extent_map *em;
4032 u64 start = extent_key->objectid - offset;
4033 u64 end = start + extent_key->offset - 1;
4034
4035 em = alloc_extent_map(GFP_NOFS);
4036 BUG_ON(!em || IS_ERR(em));
4037
4038 em->start = start;
4039 em->len = extent_key->offset;
4040 em->block_len = extent_key->offset;
4041 em->block_start = extent_key->objectid;
4042 em->bdev = root->fs_info->fs_devices->latest_bdev;
4043 set_bit(EXTENT_FLAG_PINNED, &em->flags);
4044
4045 /* setup extent map to cheat btrfs_readpage */
4046 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
4047 while (1) {
4048 int ret;
4049 spin_lock(&em_tree->lock);
4050 ret = add_extent_mapping(em_tree, em);
4051 spin_unlock(&em_tree->lock);
4052 if (ret != -EEXIST) {
4053 free_extent_map(em);
4054 break;
4055 }
4056 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
4057 }
4058 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
4059
4060 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
4061}
4062
4063struct btrfs_ref_path {
4064 u64 extent_start;
4065 u64 nodes[BTRFS_MAX_LEVEL];
4066 u64 root_objectid;
4067 u64 root_generation;
4068 u64 owner_objectid;
4069 u32 num_refs;
4070 int lowest_level;
4071 int current_level;
4072 int shared_level;
4073
4074 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
4075 u64 new_nodes[BTRFS_MAX_LEVEL];
4076};
4077
4078struct disk_extent {
4079 u64 ram_bytes;
4080 u64 disk_bytenr;
4081 u64 disk_num_bytes;
4082 u64 offset;
4083 u64 num_bytes;
4084 u8 compression;
4085 u8 encryption;
4086 u16 other_encoding;
4087};
4088
4089static int is_cowonly_root(u64 root_objectid)
4090{
4091 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
4092 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
4093 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
4094 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
4095 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
4096 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
4097 return 1;
4098 return 0;
4099}
4100
4101static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
4102 struct btrfs_root *extent_root,
4103 struct btrfs_ref_path *ref_path,
4104 int first_time)
4105{
4106 struct extent_buffer *leaf;
4107 struct btrfs_path *path;
4108 struct btrfs_extent_ref *ref;
4109 struct btrfs_key key;
4110 struct btrfs_key found_key;
4111 u64 bytenr;
4112 u32 nritems;
4113 int level;
4114 int ret = 1;
4115
4116 path = btrfs_alloc_path();
4117 if (!path)
4118 return -ENOMEM;
4119
4120 if (first_time) {
4121 ref_path->lowest_level = -1;
4122 ref_path->current_level = -1;
4123 ref_path->shared_level = -1;
4124 goto walk_up;
4125 }
4126walk_down:
4127 level = ref_path->current_level - 1;
4128 while (level >= -1) {
4129 u64 parent;
4130 if (level < ref_path->lowest_level)
4131 break;
4132
4133 if (level >= 0)
4134 bytenr = ref_path->nodes[level];
4135 else
4136 bytenr = ref_path->extent_start;
4137 BUG_ON(bytenr == 0);
4138
4139 parent = ref_path->nodes[level + 1];
4140 ref_path->nodes[level + 1] = 0;
4141 ref_path->current_level = level;
4142 BUG_ON(parent == 0);
4143
4144 key.objectid = bytenr;
4145 key.offset = parent + 1;
4146 key.type = BTRFS_EXTENT_REF_KEY;
4147
4148 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4149 if (ret < 0)
4150 goto out;
4151 BUG_ON(ret == 0);
4152
4153 leaf = path->nodes[0];
4154 nritems = btrfs_header_nritems(leaf);
4155 if (path->slots[0] >= nritems) {
4156 ret = btrfs_next_leaf(extent_root, path);
4157 if (ret < 0)
4158 goto out;
4159 if (ret > 0)
4160 goto next;
4161 leaf = path->nodes[0];
4162 }
4163
4164 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4165 if (found_key.objectid == bytenr &&
4166 found_key.type == BTRFS_EXTENT_REF_KEY) {
4167 if (level < ref_path->shared_level)
4168 ref_path->shared_level = level;
4169 goto found;
4170 }
4171next:
4172 level--;
4173 btrfs_release_path(extent_root, path);
4174 cond_resched();
4175 }
4176 /* reached lowest level */
4177 ret = 1;
4178 goto out;
4179walk_up:
4180 level = ref_path->current_level;
4181 while (level < BTRFS_MAX_LEVEL - 1) {
4182 u64 ref_objectid;
4183
4184 if (level >= 0)
4185 bytenr = ref_path->nodes[level];
4186 else
4187 bytenr = ref_path->extent_start;
4188
4189 BUG_ON(bytenr == 0);
4190
4191 key.objectid = bytenr;
4192 key.offset = 0;
4193 key.type = BTRFS_EXTENT_REF_KEY;
4194
4195 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4196 if (ret < 0)
4197 goto out;
4198
4199 leaf = path->nodes[0];
4200 nritems = btrfs_header_nritems(leaf);
4201 if (path->slots[0] >= nritems) {
4202 ret = btrfs_next_leaf(extent_root, path);
4203 if (ret < 0)
4204 goto out;
4205 if (ret > 0) {
4206 /* the extent was freed by someone */
4207 if (ref_path->lowest_level == level)
4208 goto out;
4209 btrfs_release_path(extent_root, path);
4210 goto walk_down;
4211 }
4212 leaf = path->nodes[0];
4213 }
4214
4215 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4216 if (found_key.objectid != bytenr ||
4217 found_key.type != BTRFS_EXTENT_REF_KEY) {
4218 /* the extent was freed by someone */
4219 if (ref_path->lowest_level == level) {
4220 ret = 1;
4221 goto out;
4222 }
4223 btrfs_release_path(extent_root, path);
4224 goto walk_down;
4225 }
4226found:
4227 ref = btrfs_item_ptr(leaf, path->slots[0],
4228 struct btrfs_extent_ref);
4229 ref_objectid = btrfs_ref_objectid(leaf, ref);
4230 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4231 if (first_time) {
4232 level = (int)ref_objectid;
4233 BUG_ON(level >= BTRFS_MAX_LEVEL);
4234 ref_path->lowest_level = level;
4235 ref_path->current_level = level;
4236 ref_path->nodes[level] = bytenr;
4237 } else {
4238 WARN_ON(ref_objectid != level);
4239 }
4240 } else {
4241 WARN_ON(level != -1);
4242 }
4243 first_time = 0;
4244
4245 if (ref_path->lowest_level == level) {
4246 ref_path->owner_objectid = ref_objectid;
4247 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
4248 }
4249
4250 /*
4251 * the block is tree root or the block isn't in reference
4252 * counted tree.
4253 */
4254 if (found_key.objectid == found_key.offset ||
4255 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
4256 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4257 ref_path->root_generation =
4258 btrfs_ref_generation(leaf, ref);
4259 if (level < 0) {
4260 /* special reference from the tree log */
4261 ref_path->nodes[0] = found_key.offset;
4262 ref_path->current_level = 0;
4263 }
4264 ret = 0;
4265 goto out;
4266 }
4267
4268 level++;
4269 BUG_ON(ref_path->nodes[level] != 0);
4270 ref_path->nodes[level] = found_key.offset;
4271 ref_path->current_level = level;
4272
4273 /*
4274 * the reference was created in the running transaction,
4275 * no need to continue walking up.
4276 */
4277 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
4278 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4279 ref_path->root_generation =
4280 btrfs_ref_generation(leaf, ref);
4281 ret = 0;
4282 goto out;
4283 }
4284
4285 btrfs_release_path(extent_root, path);
4286 cond_resched();
4287 }
4288 /* reached max tree level, but no tree root found. */
4289 BUG();
4290out:
4291 btrfs_free_path(path);
4292 return ret;
4293}
4294
4295static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
4296 struct btrfs_root *extent_root,
4297 struct btrfs_ref_path *ref_path,
4298 u64 extent_start)
4299{
4300 memset(ref_path, 0, sizeof(*ref_path));
4301 ref_path->extent_start = extent_start;
4302
4303 return __next_ref_path(trans, extent_root, ref_path, 1);
4304}
4305
4306static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
4307 struct btrfs_root *extent_root,
4308 struct btrfs_ref_path *ref_path)
4309{
4310 return __next_ref_path(trans, extent_root, ref_path, 0);
4311}
4312
4313static noinline int get_new_locations(struct inode *reloc_inode,
4314 struct btrfs_key *extent_key,
4315 u64 offset, int no_fragment,
4316 struct disk_extent **extents,
4317 int *nr_extents)
4318{
4319 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4320 struct btrfs_path *path;
4321 struct btrfs_file_extent_item *fi;
4322 struct extent_buffer *leaf;
4323 struct disk_extent *exts = *extents;
4324 struct btrfs_key found_key;
4325 u64 cur_pos;
4326 u64 last_byte;
4327 u32 nritems;
4328 int nr = 0;
4329 int max = *nr_extents;
4330 int ret;
4331
4332 WARN_ON(!no_fragment && *extents);
4333 if (!exts) {
4334 max = 1;
4335 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
4336 if (!exts)
4337 return -ENOMEM;
4338 }
4339
4340 path = btrfs_alloc_path();
4341 BUG_ON(!path);
4342
4343 cur_pos = extent_key->objectid - offset;
4344 last_byte = extent_key->objectid + extent_key->offset;
4345 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
4346 cur_pos, 0);
4347 if (ret < 0)
4348 goto out;
4349 if (ret > 0) {
4350 ret = -ENOENT;
4351 goto out;
4352 }
4353
4354 while (1) {
4355 leaf = path->nodes[0];
4356 nritems = btrfs_header_nritems(leaf);
4357 if (path->slots[0] >= nritems) {
4358 ret = btrfs_next_leaf(root, path);
4359 if (ret < 0)
4360 goto out;
4361 if (ret > 0)
4362 break;
4363 leaf = path->nodes[0];
4364 }
4365
4366 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4367 if (found_key.offset != cur_pos ||
4368 found_key.type != BTRFS_EXTENT_DATA_KEY ||
4369 found_key.objectid != reloc_inode->i_ino)
4370 break;
4371
4372 fi = btrfs_item_ptr(leaf, path->slots[0],
4373 struct btrfs_file_extent_item);
4374 if (btrfs_file_extent_type(leaf, fi) !=
4375 BTRFS_FILE_EXTENT_REG ||
4376 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4377 break;
4378
4379 if (nr == max) {
4380 struct disk_extent *old = exts;
4381 max *= 2;
4382 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
4383 memcpy(exts, old, sizeof(*exts) * nr);
4384 if (old != *extents)
4385 kfree(old);
4386 }
4387
4388 exts[nr].disk_bytenr =
4389 btrfs_file_extent_disk_bytenr(leaf, fi);
4390 exts[nr].disk_num_bytes =
4391 btrfs_file_extent_disk_num_bytes(leaf, fi);
4392 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
4393 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4394 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
4395 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
4396 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
4397 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
4398 fi);
4399 BUG_ON(exts[nr].offset > 0);
4400 BUG_ON(exts[nr].compression || exts[nr].encryption);
4401 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
4402
4403 cur_pos += exts[nr].num_bytes;
4404 nr++;
4405
4406 if (cur_pos + offset >= last_byte)
4407 break;
4408
4409 if (no_fragment) {
4410 ret = 1;
4411 goto out;
4412 }
4413 path->slots[0]++;
4414 }
4415
4416 BUG_ON(cur_pos + offset > last_byte);
4417 if (cur_pos + offset < last_byte) {
4418 ret = -ENOENT;
4419 goto out;
4420 }
4421 ret = 0;
4422out:
4423 btrfs_free_path(path);
4424 if (ret) {
4425 if (exts != *extents)
4426 kfree(exts);
4427 } else {
4428 *extents = exts;
4429 *nr_extents = nr;
4430 }
4431 return ret;
4432}
4433
4434static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
4435 struct btrfs_root *root,
4436 struct btrfs_path *path,
4437 struct btrfs_key *extent_key,
4438 struct btrfs_key *leaf_key,
4439 struct btrfs_ref_path *ref_path,
4440 struct disk_extent *new_extents,
4441 int nr_extents)
4442{
4443 struct extent_buffer *leaf;
4444 struct btrfs_file_extent_item *fi;
4445 struct inode *inode = NULL;
4446 struct btrfs_key key;
4447 u64 lock_start = 0;
4448 u64 lock_end = 0;
4449 u64 num_bytes;
4450 u64 ext_offset;
4451 u64 first_pos;
4452 u32 nritems;
4453 int nr_scaned = 0;
4454 int extent_locked = 0;
4455 int extent_type;
4456 int ret;
4457
4458 memcpy(&key, leaf_key, sizeof(key));
4459 first_pos = INT_LIMIT(loff_t) - extent_key->offset;
4460 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4461 if (key.objectid < ref_path->owner_objectid ||
4462 (key.objectid == ref_path->owner_objectid &&
4463 key.type < BTRFS_EXTENT_DATA_KEY)) {
4464 key.objectid = ref_path->owner_objectid;
4465 key.type = BTRFS_EXTENT_DATA_KEY;
4466 key.offset = 0;
4467 }
4468 }
4469
4470 while (1) {
4471 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4472 if (ret < 0)
4473 goto out;
4474
4475 leaf = path->nodes[0];
4476 nritems = btrfs_header_nritems(leaf);
4477next:
4478 if (extent_locked && ret > 0) {
4479 /*
4480 * the file extent item was modified by someone
4481 * before the extent got locked.
4482 */
4483 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4484 lock_end, GFP_NOFS);
4485 extent_locked = 0;
4486 }
4487
4488 if (path->slots[0] >= nritems) {
4489 if (++nr_scaned > 2)
4490 break;
4491
4492 BUG_ON(extent_locked);
4493 ret = btrfs_next_leaf(root, path);
4494 if (ret < 0)
4495 goto out;
4496 if (ret > 0)
4497 break;
4498 leaf = path->nodes[0];
4499 nritems = btrfs_header_nritems(leaf);
4500 }
4501
4502 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4503
4504 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4505 if ((key.objectid > ref_path->owner_objectid) ||
4506 (key.objectid == ref_path->owner_objectid &&
4507 key.type > BTRFS_EXTENT_DATA_KEY) ||
4508 (key.offset >= first_pos + extent_key->offset))
4509 break;
4510 }
4511
4512 if (inode && key.objectid != inode->i_ino) {
4513 BUG_ON(extent_locked);
4514 btrfs_release_path(root, path);
4515 mutex_unlock(&inode->i_mutex);
4516 iput(inode);
4517 inode = NULL;
4518 continue;
4519 }
4520
4521 if (key.type != BTRFS_EXTENT_DATA_KEY) {
4522 path->slots[0]++;
4523 ret = 1;
4524 goto next;
4525 }
4526 fi = btrfs_item_ptr(leaf, path->slots[0],
4527 struct btrfs_file_extent_item);
4528 extent_type = btrfs_file_extent_type(leaf, fi);
4529 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
4530 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
4531 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
4532 extent_key->objectid)) {
4533 path->slots[0]++;
4534 ret = 1;
4535 goto next;
4536 }
4537
4538 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4539 ext_offset = btrfs_file_extent_offset(leaf, fi);
4540
4541 if (first_pos > key.offset - ext_offset)
4542 first_pos = key.offset - ext_offset;
4543
4544 if (!extent_locked) {
4545 lock_start = key.offset;
4546 lock_end = lock_start + num_bytes - 1;
4547 } else {
4548 if (lock_start > key.offset ||
4549 lock_end + 1 < key.offset + num_bytes) {
4550 unlock_extent(&BTRFS_I(inode)->io_tree,
4551 lock_start, lock_end, GFP_NOFS);
4552 extent_locked = 0;
4553 }
4554 }
4555
4556 if (!inode) {
4557 btrfs_release_path(root, path);
4558
4559 inode = btrfs_iget_locked(root->fs_info->sb,
4560 key.objectid, root);
4561 if (inode->i_state & I_NEW) {
4562 BTRFS_I(inode)->root = root;
4563 BTRFS_I(inode)->location.objectid =
4564 key.objectid;
4565 BTRFS_I(inode)->location.type =
4566 BTRFS_INODE_ITEM_KEY;
4567 BTRFS_I(inode)->location.offset = 0;
4568 btrfs_read_locked_inode(inode);
4569 unlock_new_inode(inode);
4570 }
4571 /*
4572 * some code call btrfs_commit_transaction while
4573 * holding the i_mutex, so we can't use mutex_lock
4574 * here.
4575 */
4576 if (is_bad_inode(inode) ||
4577 !mutex_trylock(&inode->i_mutex)) {
4578 iput(inode);
4579 inode = NULL;
4580 key.offset = (u64)-1;
4581 goto skip;
4582 }
4583 }
4584
4585 if (!extent_locked) {
4586 struct btrfs_ordered_extent *ordered;
4587
4588 btrfs_release_path(root, path);
4589
4590 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4591 lock_end, GFP_NOFS);
4592 ordered = btrfs_lookup_first_ordered_extent(inode,
4593 lock_end);
4594 if (ordered &&
4595 ordered->file_offset <= lock_end &&
4596 ordered->file_offset + ordered->len > lock_start) {
4597 unlock_extent(&BTRFS_I(inode)->io_tree,
4598 lock_start, lock_end, GFP_NOFS);
4599 btrfs_start_ordered_extent(inode, ordered, 1);
4600 btrfs_put_ordered_extent(ordered);
4601 key.offset += num_bytes;
4602 goto skip;
4603 }
4604 if (ordered)
4605 btrfs_put_ordered_extent(ordered);
4606
4607 extent_locked = 1;
4608 continue;
4609 }
4610
4611 if (nr_extents == 1) {
4612 /* update extent pointer in place */
4613 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4614 new_extents[0].disk_bytenr);
4615 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4616 new_extents[0].disk_num_bytes);
4617 btrfs_mark_buffer_dirty(leaf);
4618
4619 btrfs_drop_extent_cache(inode, key.offset,
4620 key.offset + num_bytes - 1, 0);
4621
4622 ret = btrfs_inc_extent_ref(trans, root,
4623 new_extents[0].disk_bytenr,
4624 new_extents[0].disk_num_bytes,
4625 leaf->start,
4626 root->root_key.objectid,
4627 trans->transid,
4628 key.objectid);
4629 BUG_ON(ret);
4630
4631 ret = btrfs_free_extent(trans, root,
4632 extent_key->objectid,
4633 extent_key->offset,
4634 leaf->start,
4635 btrfs_header_owner(leaf),
4636 btrfs_header_generation(leaf),
4637 key.objectid, 0);
4638 BUG_ON(ret);
4639
4640 btrfs_release_path(root, path);
4641 key.offset += num_bytes;
4642 } else {
4643 BUG_ON(1);
4644#if 0
4645 u64 alloc_hint;
4646 u64 extent_len;
4647 int i;
4648 /*
4649 * drop old extent pointer at first, then insert the
4650 * new pointers one bye one
4651 */
4652 btrfs_release_path(root, path);
4653 ret = btrfs_drop_extents(trans, root, inode, key.offset,
4654 key.offset + num_bytes,
4655 key.offset, &alloc_hint);
4656 BUG_ON(ret);
4657
4658 for (i = 0; i < nr_extents; i++) {
4659 if (ext_offset >= new_extents[i].num_bytes) {
4660 ext_offset -= new_extents[i].num_bytes;
4661 continue;
4662 }
4663 extent_len = min(new_extents[i].num_bytes -
4664 ext_offset, num_bytes);
4665
4666 ret = btrfs_insert_empty_item(trans, root,
4667 path, &key,
4668 sizeof(*fi));
4669 BUG_ON(ret);
4670
4671 leaf = path->nodes[0];
4672 fi = btrfs_item_ptr(leaf, path->slots[0],
4673 struct btrfs_file_extent_item);
4674 btrfs_set_file_extent_generation(leaf, fi,
4675 trans->transid);
4676 btrfs_set_file_extent_type(leaf, fi,
4677 BTRFS_FILE_EXTENT_REG);
4678 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4679 new_extents[i].disk_bytenr);
4680 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4681 new_extents[i].disk_num_bytes);
4682 btrfs_set_file_extent_ram_bytes(leaf, fi,
4683 new_extents[i].ram_bytes);
4684
4685 btrfs_set_file_extent_compression(leaf, fi,
4686 new_extents[i].compression);
4687 btrfs_set_file_extent_encryption(leaf, fi,
4688 new_extents[i].encryption);
4689 btrfs_set_file_extent_other_encoding(leaf, fi,
4690 new_extents[i].other_encoding);
4691
4692 btrfs_set_file_extent_num_bytes(leaf, fi,
4693 extent_len);
4694 ext_offset += new_extents[i].offset;
4695 btrfs_set_file_extent_offset(leaf, fi,
4696 ext_offset);
4697 btrfs_mark_buffer_dirty(leaf);
4698
4699 btrfs_drop_extent_cache(inode, key.offset,
4700 key.offset + extent_len - 1, 0);
4701
4702 ret = btrfs_inc_extent_ref(trans, root,
4703 new_extents[i].disk_bytenr,
4704 new_extents[i].disk_num_bytes,
4705 leaf->start,
4706 root->root_key.objectid,
4707 trans->transid, key.objectid);
4708 BUG_ON(ret);
4709 btrfs_release_path(root, path);
4710
4711 inode_add_bytes(inode, extent_len);
4712
4713 ext_offset = 0;
4714 num_bytes -= extent_len;
4715 key.offset += extent_len;
4716
4717 if (num_bytes == 0)
4718 break;
4719 }
4720 BUG_ON(i >= nr_extents);
4721#endif
4722 }
4723
4724 if (extent_locked) {
4725 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4726 lock_end, GFP_NOFS);
4727 extent_locked = 0;
4728 }
4729skip:
4730 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
4731 key.offset >= first_pos + extent_key->offset)
4732 break;
4733
4734 cond_resched();
4735 }
4736 ret = 0;
4737out:
4738 btrfs_release_path(root, path);
4739 if (inode) {
4740 mutex_unlock(&inode->i_mutex);
4741 if (extent_locked) {
4742 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4743 lock_end, GFP_NOFS);
4744 }
4745 iput(inode);
4746 }
4747 return ret;
4748}
4749
4750int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
4751 struct btrfs_root *root,
4752 struct extent_buffer *buf, u64 orig_start)
4753{
4754 int level;
4755 int ret;
4756
4757 BUG_ON(btrfs_header_generation(buf) != trans->transid);
4758 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
4759
4760 level = btrfs_header_level(buf);
4761 if (level == 0) {
4762 struct btrfs_leaf_ref *ref;
4763 struct btrfs_leaf_ref *orig_ref;
4764
4765 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
4766 if (!orig_ref)
4767 return -ENOENT;
4768
4769 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
4770 if (!ref) {
4771 btrfs_free_leaf_ref(root, orig_ref);
4772 return -ENOMEM;
4773 }
4774
4775 ref->nritems = orig_ref->nritems;
4776 memcpy(ref->extents, orig_ref->extents,
4777 sizeof(ref->extents[0]) * ref->nritems);
4778
4779 btrfs_free_leaf_ref(root, orig_ref);
4780
4781 ref->root_gen = trans->transid;
4782 ref->bytenr = buf->start;
4783 ref->owner = btrfs_header_owner(buf);
4784 ref->generation = btrfs_header_generation(buf);
4785 ret = btrfs_add_leaf_ref(root, ref, 0);
4786 WARN_ON(ret);
4787 btrfs_free_leaf_ref(root, ref);
4788 }
4789 return 0;
4790}
4791
4792static noinline int invalidate_extent_cache(struct btrfs_root *root,
4793 struct extent_buffer *leaf,
4794 struct btrfs_block_group_cache *group,
4795 struct btrfs_root *target_root)
4796{
4797 struct btrfs_key key;
4798 struct inode *inode = NULL;
4799 struct btrfs_file_extent_item *fi;
4800 u64 num_bytes;
4801 u64 skip_objectid = 0;
4802 u32 nritems;
4803 u32 i;
4804
4805 nritems = btrfs_header_nritems(leaf);
4806 for (i = 0; i < nritems; i++) {
4807 btrfs_item_key_to_cpu(leaf, &key, i);
4808 if (key.objectid == skip_objectid ||
4809 key.type != BTRFS_EXTENT_DATA_KEY)
4810 continue;
4811 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4812 if (btrfs_file_extent_type(leaf, fi) ==
4813 BTRFS_FILE_EXTENT_INLINE)
4814 continue;
4815 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4816 continue;
4817 if (!inode || inode->i_ino != key.objectid) {
4818 iput(inode);
4819 inode = btrfs_ilookup(target_root->fs_info->sb,
4820 key.objectid, target_root, 1);
4821 }
4822 if (!inode) {
4823 skip_objectid = key.objectid;
4824 continue;
4825 }
4826 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4827
4828 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4829 key.offset + num_bytes - 1, GFP_NOFS);
4830 btrfs_drop_extent_cache(inode, key.offset,
4831 key.offset + num_bytes - 1, 1);
4832 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4833 key.offset + num_bytes - 1, GFP_NOFS);
4834 cond_resched();
4835 }
4836 iput(inode);
4837 return 0;
4838}
4839
4840static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
4841 struct btrfs_root *root,
4842 struct extent_buffer *leaf,
4843 struct btrfs_block_group_cache *group,
4844 struct inode *reloc_inode)
4845{
4846 struct btrfs_key key;
4847 struct btrfs_key extent_key;
4848 struct btrfs_file_extent_item *fi;
4849 struct btrfs_leaf_ref *ref;
4850 struct disk_extent *new_extent;
4851 u64 bytenr;
4852 u64 num_bytes;
4853 u32 nritems;
4854 u32 i;
4855 int ext_index;
4856 int nr_extent;
4857 int ret;
4858
4859 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
4860 BUG_ON(!new_extent);
4861
4862 ref = btrfs_lookup_leaf_ref(root, leaf->start);
4863 BUG_ON(!ref);
4864
4865 ext_index = -1;
4866 nritems = btrfs_header_nritems(leaf);
4867 for (i = 0; i < nritems; i++) {
4868 btrfs_item_key_to_cpu(leaf, &key, i);
4869 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
4870 continue;
4871 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4872 if (btrfs_file_extent_type(leaf, fi) ==
4873 BTRFS_FILE_EXTENT_INLINE)
4874 continue;
4875 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4876 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4877 if (bytenr == 0)
4878 continue;
4879
4880 ext_index++;
4881 if (bytenr >= group->key.objectid + group->key.offset ||
4882 bytenr + num_bytes <= group->key.objectid)
4883 continue;
4884
4885 extent_key.objectid = bytenr;
4886 extent_key.offset = num_bytes;
4887 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
4888 nr_extent = 1;
4889 ret = get_new_locations(reloc_inode, &extent_key,
4890 group->key.objectid, 1,
4891 &new_extent, &nr_extent);
4892 if (ret > 0)
4893 continue;
4894 BUG_ON(ret < 0);
4895
4896 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
4897 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
4898 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
4899 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
4900
4901 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4902 new_extent->disk_bytenr);
4903 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4904 new_extent->disk_num_bytes);
4905 btrfs_mark_buffer_dirty(leaf);
4906
4907 ret = btrfs_inc_extent_ref(trans, root,
4908 new_extent->disk_bytenr,
4909 new_extent->disk_num_bytes,
4910 leaf->start,
4911 root->root_key.objectid,
4912 trans->transid, key.objectid);
4913 BUG_ON(ret);
4914 ret = btrfs_free_extent(trans, root,
4915 bytenr, num_bytes, leaf->start,
4916 btrfs_header_owner(leaf),
4917 btrfs_header_generation(leaf),
4918 key.objectid, 0);
4919 BUG_ON(ret);
4920 cond_resched();
4921 }
4922 kfree(new_extent);
4923 BUG_ON(ext_index + 1 != ref->nritems);
4924 btrfs_free_leaf_ref(root, ref);
4925 return 0;
4926}
4927
4928int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
4929 struct btrfs_root *root)
4930{
4931 struct btrfs_root *reloc_root;
4932 int ret;
4933
4934 if (root->reloc_root) {
4935 reloc_root = root->reloc_root;
4936 root->reloc_root = NULL;
4937 list_add(&reloc_root->dead_list,
4938 &root->fs_info->dead_reloc_roots);
4939
4940 btrfs_set_root_bytenr(&reloc_root->root_item,
4941 reloc_root->node->start);
4942 btrfs_set_root_level(&root->root_item,
4943 btrfs_header_level(reloc_root->node));
4944 memset(&reloc_root->root_item.drop_progress, 0,
4945 sizeof(struct btrfs_disk_key));
4946 reloc_root->root_item.drop_level = 0;
4947
4948 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4949 &reloc_root->root_key,
4950 &reloc_root->root_item);
4951 BUG_ON(ret);
4952 }
4953 return 0;
4954}
4955
4956int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
4957{
4958 struct btrfs_trans_handle *trans;
4959 struct btrfs_root *reloc_root;
4960 struct btrfs_root *prev_root = NULL;
4961 struct list_head dead_roots;
4962 int ret;
4963 unsigned long nr;
4964
4965 INIT_LIST_HEAD(&dead_roots);
4966 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
4967
4968 while (!list_empty(&dead_roots)) {
4969 reloc_root = list_entry(dead_roots.prev,
4970 struct btrfs_root, dead_list);
4971 list_del_init(&reloc_root->dead_list);
4972
4973 BUG_ON(reloc_root->commit_root != NULL);
4974 while (1) {
4975 trans = btrfs_join_transaction(root, 1);
4976 BUG_ON(!trans);
4977
4978 mutex_lock(&root->fs_info->drop_mutex);
4979 ret = btrfs_drop_snapshot(trans, reloc_root);
4980 if (ret != -EAGAIN)
4981 break;
4982 mutex_unlock(&root->fs_info->drop_mutex);
4983
4984 nr = trans->blocks_used;
4985 ret = btrfs_end_transaction(trans, root);
4986 BUG_ON(ret);
4987 btrfs_btree_balance_dirty(root, nr);
4988 }
4989
4990 free_extent_buffer(reloc_root->node);
4991
4992 ret = btrfs_del_root(trans, root->fs_info->tree_root,
4993 &reloc_root->root_key);
4994 BUG_ON(ret);
4995 mutex_unlock(&root->fs_info->drop_mutex);
4996
4997 nr = trans->blocks_used;
4998 ret = btrfs_end_transaction(trans, root);
4999 BUG_ON(ret);
5000 btrfs_btree_balance_dirty(root, nr);
5001
5002 kfree(prev_root);
5003 prev_root = reloc_root;
5004 }
5005 if (prev_root) {
5006 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
5007 kfree(prev_root);
5008 }
5009 return 0;
5010}
5011
5012int btrfs_add_dead_reloc_root(struct btrfs_root *root)
5013{
5014 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
5015 return 0;
5016}
5017
5018int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
5019{
5020 struct btrfs_root *reloc_root;
5021 struct btrfs_trans_handle *trans;
5022 struct btrfs_key location;
5023 int found;
5024 int ret;
5025
5026 mutex_lock(&root->fs_info->tree_reloc_mutex);
5027 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
5028 BUG_ON(ret);
5029 found = !list_empty(&root->fs_info->dead_reloc_roots);
5030 mutex_unlock(&root->fs_info->tree_reloc_mutex);
5031
5032 if (found) {
5033 trans = btrfs_start_transaction(root, 1);
5034 BUG_ON(!trans);
5035 ret = btrfs_commit_transaction(trans, root);
5036 BUG_ON(ret);
5037 }
5038
5039 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5040 location.offset = (u64)-1;
5041 location.type = BTRFS_ROOT_ITEM_KEY;
5042
5043 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
5044 BUG_ON(!reloc_root);
5045 btrfs_orphan_cleanup(reloc_root);
5046 return 0;
5047}
5048
5049static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
5050 struct btrfs_root *root)
5051{
5052 struct btrfs_root *reloc_root;
5053 struct extent_buffer *eb;
5054 struct btrfs_root_item *root_item;
5055 struct btrfs_key root_key;
5056 int ret;
5057
5058 BUG_ON(!root->ref_cows);
5059 if (root->reloc_root)
5060 return 0;
5061
5062 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
5063 BUG_ON(!root_item);
5064
5065 ret = btrfs_copy_root(trans, root, root->commit_root,
5066 &eb, BTRFS_TREE_RELOC_OBJECTID);
5067 BUG_ON(ret);
5068
5069 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
5070 root_key.offset = root->root_key.objectid;
5071 root_key.type = BTRFS_ROOT_ITEM_KEY;
5072
5073 memcpy(root_item, &root->root_item, sizeof(root_item));
5074 btrfs_set_root_refs(root_item, 0);
5075 btrfs_set_root_bytenr(root_item, eb->start);
5076 btrfs_set_root_level(root_item, btrfs_header_level(eb));
5077 btrfs_set_root_generation(root_item, trans->transid);
5078
5079 btrfs_tree_unlock(eb);
5080 free_extent_buffer(eb);
5081
5082 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
5083 &root_key, root_item);
5084 BUG_ON(ret);
5085 kfree(root_item);
5086
5087 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
5088 &root_key);
5089 BUG_ON(!reloc_root);
5090 reloc_root->last_trans = trans->transid;
5091 reloc_root->commit_root = NULL;
5092 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
5093
5094 root->reloc_root = reloc_root;
5095 return 0;
5096}
5097
5098/*
5099 * Core function of space balance.
5100 *
5101 * The idea is using reloc trees to relocate tree blocks in reference
5102 * counted roots. There is one reloc tree for each subvol, and all
5103 * reloc trees share same root key objectid. Reloc trees are snapshots
5104 * of the latest committed roots of subvols (root->commit_root).
5105 *
5106 * To relocate a tree block referenced by a subvol, there are two steps.
5107 * COW the block through subvol's reloc tree, then update block pointer
5108 * in the subvol to point to the new block. Since all reloc trees share
5109 * same root key objectid, doing special handing for tree blocks owned
5110 * by them is easy. Once a tree block has been COWed in one reloc tree,
5111 * we can use the resulting new block directly when the same block is
5112 * required to COW again through other reloc trees. By this way, relocated
5113 * tree blocks are shared between reloc trees, so they are also shared
5114 * between subvols.
5115 */
5116static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
5117 struct btrfs_root *root,
5118 struct btrfs_path *path,
5119 struct btrfs_key *first_key,
5120 struct btrfs_ref_path *ref_path,
5121 struct btrfs_block_group_cache *group,
5122 struct inode *reloc_inode)
5123{
5124 struct btrfs_root *reloc_root;
5125 struct extent_buffer *eb = NULL;
5126 struct btrfs_key *keys;
5127 u64 *nodes;
5128 int level;
5129 int shared_level;
5130 int lowest_level = 0;
5131 int ret;
5132
5133 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
5134 lowest_level = ref_path->owner_objectid;
5135
5136 if (!root->ref_cows) {
5137 path->lowest_level = lowest_level;
5138 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
5139 BUG_ON(ret < 0);
5140 path->lowest_level = 0;
5141 btrfs_release_path(root, path);
5142 return 0;
5143 }
5144
5145 mutex_lock(&root->fs_info->tree_reloc_mutex);
5146 ret = init_reloc_tree(trans, root);
5147 BUG_ON(ret);
5148 reloc_root = root->reloc_root;
5149
5150 shared_level = ref_path->shared_level;
5151 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
5152
5153 keys = ref_path->node_keys;
5154 nodes = ref_path->new_nodes;
5155 memset(&keys[shared_level + 1], 0,
5156 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
5157 memset(&nodes[shared_level + 1], 0,
5158 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
5159
5160 if (nodes[lowest_level] == 0) {
5161 path->lowest_level = lowest_level;
5162 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5163 0, 1);
5164 BUG_ON(ret);
5165 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
5166 eb = path->nodes[level];
5167 if (!eb || eb == reloc_root->node)
5168 break;
5169 nodes[level] = eb->start;
5170 if (level == 0)
5171 btrfs_item_key_to_cpu(eb, &keys[level], 0);
5172 else
5173 btrfs_node_key_to_cpu(eb, &keys[level], 0);
5174 }
5175 if (nodes[0] &&
5176 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5177 eb = path->nodes[0];
5178 ret = replace_extents_in_leaf(trans, reloc_root, eb,
5179 group, reloc_inode);
5180 BUG_ON(ret);
5181 }
5182 btrfs_release_path(reloc_root, path);
5183 } else {
5184 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
5185 lowest_level);
5186 BUG_ON(ret);
5187 }
5188
5189 /*
5190 * replace tree blocks in the fs tree with tree blocks in
5191 * the reloc tree.
5192 */
5193 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
5194 BUG_ON(ret < 0);
5195
5196 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5197 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5198 0, 0);
5199 BUG_ON(ret);
5200 extent_buffer_get(path->nodes[0]);
5201 eb = path->nodes[0];
5202 btrfs_release_path(reloc_root, path);
5203 ret = invalidate_extent_cache(reloc_root, eb, group, root);
5204 BUG_ON(ret);
5205 free_extent_buffer(eb);
5206 }
5207
5208 mutex_unlock(&root->fs_info->tree_reloc_mutex);
5209 path->lowest_level = 0;
5210 return 0;
5211}
5212
5213static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
5214 struct btrfs_root *root,
5215 struct btrfs_path *path,
5216 struct btrfs_key *first_key,
5217 struct btrfs_ref_path *ref_path)
5218{
5219 int ret;
5220
5221 ret = relocate_one_path(trans, root, path, first_key,
5222 ref_path, NULL, NULL);
5223 BUG_ON(ret);
5224
5225 if (root == root->fs_info->extent_root)
5226 btrfs_extent_post_op(trans, root);
5227
5228 return 0;
5229}
5230
5231static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
5232 struct btrfs_root *extent_root,
5233 struct btrfs_path *path,
5234 struct btrfs_key *extent_key)
5235{
5236 int ret;
5237
5238 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
5239 if (ret)
5240 goto out;
5241 ret = btrfs_del_item(trans, extent_root, path);
5242out:
5243 btrfs_release_path(extent_root, path);
5244 return ret;
5245}
5246
5247static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
5248 struct btrfs_ref_path *ref_path)
5249{
5250 struct btrfs_key root_key;
5251
5252 root_key.objectid = ref_path->root_objectid;
5253 root_key.type = BTRFS_ROOT_ITEM_KEY;
5254 if (is_cowonly_root(ref_path->root_objectid))
5255 root_key.offset = 0;
5256 else
5257 root_key.offset = (u64)-1;
5258
5259 return btrfs_read_fs_root_no_name(fs_info, &root_key);
5260}
5261
5262static noinline int relocate_one_extent(struct btrfs_root *extent_root,
5263 struct btrfs_path *path,
5264 struct btrfs_key *extent_key,
5265 struct btrfs_block_group_cache *group,
5266 struct inode *reloc_inode, int pass)
5267{
5268 struct btrfs_trans_handle *trans;
5269 struct btrfs_root *found_root;
5270 struct btrfs_ref_path *ref_path = NULL;
5271 struct disk_extent *new_extents = NULL;
5272 int nr_extents = 0;
5273 int loops;
5274 int ret;
5275 int level;
5276 struct btrfs_key first_key;
5277 u64 prev_block = 0;
5278
5279
5280 trans = btrfs_start_transaction(extent_root, 1);
5281 BUG_ON(!trans);
5282
5283 if (extent_key->objectid == 0) {
5284 ret = del_extent_zero(trans, extent_root, path, extent_key);
5285 goto out;
5286 }
5287
5288 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
5289 if (!ref_path) {
5290 ret = -ENOMEM;
5291 goto out;
5292 }
5293
5294 for (loops = 0; ; loops++) {
5295 if (loops == 0) {
5296 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
5297 extent_key->objectid);
5298 } else {
5299 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
5300 }
5301 if (ret < 0)
5302 goto out;
5303 if (ret > 0)
5304 break;
5305
5306 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5307 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
5308 continue;
5309
5310 found_root = read_ref_root(extent_root->fs_info, ref_path);
5311 BUG_ON(!found_root);
5312 /*
5313 * for reference counted tree, only process reference paths
5314 * rooted at the latest committed root.
5315 */
5316 if (found_root->ref_cows &&
5317 ref_path->root_generation != found_root->root_key.offset)
5318 continue;
5319
5320 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5321 if (pass == 0) {
5322 /*
5323 * copy data extents to new locations
5324 */
5325 u64 group_start = group->key.objectid;
5326 ret = relocate_data_extent(reloc_inode,
5327 extent_key,
5328 group_start);
5329 if (ret < 0)
5330 goto out;
5331 break;
5332 }
5333 level = 0;
5334 } else {
5335 level = ref_path->owner_objectid;
5336 }
5337
5338 if (prev_block != ref_path->nodes[level]) {
5339 struct extent_buffer *eb;
5340 u64 block_start = ref_path->nodes[level];
5341 u64 block_size = btrfs_level_size(found_root, level);
5342
5343 eb = read_tree_block(found_root, block_start,
5344 block_size, 0);
5345 btrfs_tree_lock(eb);
5346 BUG_ON(level != btrfs_header_level(eb));
5347
5348 if (level == 0)
5349 btrfs_item_key_to_cpu(eb, &first_key, 0);
5350 else
5351 btrfs_node_key_to_cpu(eb, &first_key, 0);
5352
5353 btrfs_tree_unlock(eb);
5354 free_extent_buffer(eb);
5355 prev_block = block_start;
5356 }
5357
5358 btrfs_record_root_in_trans(found_root);
5359 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5360 /*
5361 * try to update data extent references while
5362 * keeping metadata shared between snapshots.
5363 */
5364 if (pass == 1) {
5365 ret = relocate_one_path(trans, found_root,
5366 path, &first_key, ref_path,
5367 group, reloc_inode);
5368 if (ret < 0)
5369 goto out;
5370 continue;
5371 }
5372 /*
5373 * use fallback method to process the remaining
5374 * references.
5375 */
5376 if (!new_extents) {
5377 u64 group_start = group->key.objectid;
5378 new_extents = kmalloc(sizeof(*new_extents),
5379 GFP_NOFS);
5380 nr_extents = 1;
5381 ret = get_new_locations(reloc_inode,
5382 extent_key,
5383 group_start, 1,
5384 &new_extents,
5385 &nr_extents);
5386 if (ret)
5387 goto out;
5388 }
5389 ret = replace_one_extent(trans, found_root,
5390 path, extent_key,
5391 &first_key, ref_path,
5392 new_extents, nr_extents);
5393 } else {
5394 ret = relocate_tree_block(trans, found_root, path,
5395 &first_key, ref_path);
5396 }
5397 if (ret < 0)
5398 goto out;
5399 }
5400 ret = 0;
5401out:
5402 btrfs_end_transaction(trans, extent_root);
5403 kfree(new_extents);
5404 kfree(ref_path);
5405 return ret;
5406}
5407
5408static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
5409{
5410 u64 num_devices;
5411 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
5412 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
5413
5414 num_devices = root->fs_info->fs_devices->rw_devices;
5415 if (num_devices == 1) {
5416 stripped |= BTRFS_BLOCK_GROUP_DUP;
5417 stripped = flags & ~stripped;
5418
5419 /* turn raid0 into single device chunks */
5420 if (flags & BTRFS_BLOCK_GROUP_RAID0)
5421 return stripped;
5422
5423 /* turn mirroring into duplication */
5424 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
5425 BTRFS_BLOCK_GROUP_RAID10))
5426 return stripped | BTRFS_BLOCK_GROUP_DUP;
5427 return flags;
5428 } else {
5429 /* they already had raid on here, just return */
5430 if (flags & stripped)
5431 return flags;
5432
5433 stripped |= BTRFS_BLOCK_GROUP_DUP;
5434 stripped = flags & ~stripped;
5435
5436 /* switch duplicated blocks with raid1 */
5437 if (flags & BTRFS_BLOCK_GROUP_DUP)
5438 return stripped | BTRFS_BLOCK_GROUP_RAID1;
5439
5440 /* turn single device chunks into raid0 */
5441 return stripped | BTRFS_BLOCK_GROUP_RAID0;
5442 }
5443 return flags;
5444}
5445
5446static int __alloc_chunk_for_shrink(struct btrfs_root *root,
5447 struct btrfs_block_group_cache *shrink_block_group,
5448 int force)
5449{
5450 struct btrfs_trans_handle *trans;
5451 u64 new_alloc_flags;
5452 u64 calc;
5453
5454 spin_lock(&shrink_block_group->lock);
5455 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
5456 spin_unlock(&shrink_block_group->lock);
5457
5458 trans = btrfs_start_transaction(root, 1);
5459 spin_lock(&shrink_block_group->lock);
5460
5461 new_alloc_flags = update_block_group_flags(root,
5462 shrink_block_group->flags);
5463 if (new_alloc_flags != shrink_block_group->flags) {
5464 calc =
5465 btrfs_block_group_used(&shrink_block_group->item);
5466 } else {
5467 calc = shrink_block_group->key.offset;
5468 }
5469 spin_unlock(&shrink_block_group->lock);
5470
5471 do_chunk_alloc(trans, root->fs_info->extent_root,
5472 calc + 2 * 1024 * 1024, new_alloc_flags, force);
5473
5474 btrfs_end_transaction(trans, root);
5475 } else
5476 spin_unlock(&shrink_block_group->lock);
5477 return 0;
5478}
5479
5480static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
5481 struct btrfs_root *root,
5482 u64 objectid, u64 size)
5483{
5484 struct btrfs_path *path;
5485 struct btrfs_inode_item *item;
5486 struct extent_buffer *leaf;
5487 int ret;
5488
5489 path = btrfs_alloc_path();
5490 if (!path)
5491 return -ENOMEM;
5492
5493 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
5494 if (ret)
5495 goto out;
5496
5497 leaf = path->nodes[0];
5498 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
5499 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
5500 btrfs_set_inode_generation(leaf, item, 1);
5501 btrfs_set_inode_size(leaf, item, size);
5502 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
5503 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS);
5504 btrfs_mark_buffer_dirty(leaf);
5505 btrfs_release_path(root, path);
5506out:
5507 btrfs_free_path(path);
5508 return ret;
5509}
5510
5511static noinline struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
5512 struct btrfs_block_group_cache *group)
5513{
5514 struct inode *inode = NULL;
5515 struct btrfs_trans_handle *trans;
5516 struct btrfs_root *root;
5517 struct btrfs_key root_key;
5518 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
5519 int err = 0;
5520
5521 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5522 root_key.type = BTRFS_ROOT_ITEM_KEY;
5523 root_key.offset = (u64)-1;
5524 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
5525 if (IS_ERR(root))
5526 return ERR_CAST(root);
5527
5528 trans = btrfs_start_transaction(root, 1);
5529 BUG_ON(!trans);
5530
5531 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
5532 if (err)
5533 goto out;
5534
5535 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
5536 BUG_ON(err);
5537
5538 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
5539 group->key.offset, 0, group->key.offset,
5540 0, 0, 0);
5541 BUG_ON(err);
5542
5543 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
5544 if (inode->i_state & I_NEW) {
5545 BTRFS_I(inode)->root = root;
5546 BTRFS_I(inode)->location.objectid = objectid;
5547 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5548 BTRFS_I(inode)->location.offset = 0;
5549 btrfs_read_locked_inode(inode);
5550 unlock_new_inode(inode);
5551 BUG_ON(is_bad_inode(inode));
5552 } else {
5553 BUG_ON(1);
5554 }
5555 BTRFS_I(inode)->index_cnt = group->key.objectid;
5556
5557 err = btrfs_orphan_add(trans, inode);
5558out:
5559 btrfs_end_transaction(trans, root);
5560 if (err) {
5561 if (inode)
5562 iput(inode);
5563 inode = ERR_PTR(err);
5564 }
5565 return inode;
5566}
5567
5568int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
5569{
5570
5571 struct btrfs_ordered_sum *sums;
5572 struct btrfs_sector_sum *sector_sum;
5573 struct btrfs_ordered_extent *ordered;
5574 struct btrfs_root *root = BTRFS_I(inode)->root;
5575 struct list_head list;
5576 size_t offset;
5577 int ret;
5578 u64 disk_bytenr;
5579
5580 INIT_LIST_HEAD(&list);
5581
5582 ordered = btrfs_lookup_ordered_extent(inode, file_pos);
5583 BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
5584
5585 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
5586 ret = btrfs_lookup_csums_range(root, disk_bytenr,
5587 disk_bytenr + len - 1, &list);
5588
5589 while (!list_empty(&list)) {
5590 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
5591 list_del_init(&sums->list);
5592
5593 sector_sum = sums->sums;
5594 sums->bytenr = ordered->start;
5595
5596 offset = 0;
5597 while (offset < sums->len) {
5598 sector_sum->bytenr += ordered->start - disk_bytenr;
5599 sector_sum++;
5600 offset += root->sectorsize;
5601 }
5602
5603 btrfs_add_ordered_sum(inode, ordered, sums);
5604 }
5605 btrfs_put_ordered_extent(ordered);
5606 return 0;
5607}
5608
5609int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
5610{
5611 struct btrfs_trans_handle *trans;
5612 struct btrfs_path *path;
5613 struct btrfs_fs_info *info = root->fs_info;
5614 struct extent_buffer *leaf;
5615 struct inode *reloc_inode;
5616 struct btrfs_block_group_cache *block_group;
5617 struct btrfs_key key;
5618 u64 skipped;
5619 u64 cur_byte;
5620 u64 total_found;
5621 u32 nritems;
5622 int ret;
5623 int progress;
5624 int pass = 0;
5625
5626 root = root->fs_info->extent_root;
5627
5628 block_group = btrfs_lookup_block_group(info, group_start);
5629 BUG_ON(!block_group);
5630
5631 printk(KERN_INFO "btrfs relocating block group %llu flags %llu\n",
5632 (unsigned long long)block_group->key.objectid,
5633 (unsigned long long)block_group->flags);
5634
5635 path = btrfs_alloc_path();
5636 BUG_ON(!path);
5637
5638 reloc_inode = create_reloc_inode(info, block_group);
5639 BUG_ON(IS_ERR(reloc_inode));
5640
5641 __alloc_chunk_for_shrink(root, block_group, 1);
5642 set_block_group_readonly(block_group);
5643
5644 btrfs_start_delalloc_inodes(info->tree_root);
5645 btrfs_wait_ordered_extents(info->tree_root, 0);
5646again:
5647 skipped = 0;
5648 total_found = 0;
5649 progress = 0;
5650 key.objectid = block_group->key.objectid;
5651 key.offset = 0;
5652 key.type = 0;
5653 cur_byte = key.objectid;
5654
5655 trans = btrfs_start_transaction(info->tree_root, 1);
5656 btrfs_commit_transaction(trans, info->tree_root);
5657
5658 mutex_lock(&root->fs_info->cleaner_mutex);
5659 btrfs_clean_old_snapshots(info->tree_root);
5660 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
5661 mutex_unlock(&root->fs_info->cleaner_mutex);
5662
5663 while (1) {
5664 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5665 if (ret < 0)
5666 goto out;
5667next:
5668 leaf = path->nodes[0];
5669 nritems = btrfs_header_nritems(leaf);
5670 if (path->slots[0] >= nritems) {
5671 ret = btrfs_next_leaf(root, path);
5672 if (ret < 0)
5673 goto out;
5674 if (ret == 1) {
5675 ret = 0;
5676 break;
5677 }
5678 leaf = path->nodes[0];
5679 nritems = btrfs_header_nritems(leaf);
5680 }
5681
5682 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5683
5684 if (key.objectid >= block_group->key.objectid +
5685 block_group->key.offset)
5686 break;
5687
5688 if (progress && need_resched()) {
5689 btrfs_release_path(root, path);
5690 cond_resched();
5691 progress = 0;
5692 continue;
5693 }
5694 progress = 1;
5695
5696 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
5697 key.objectid + key.offset <= cur_byte) {
5698 path->slots[0]++;
5699 goto next;
5700 }
5701
5702 total_found++;
5703 cur_byte = key.objectid + key.offset;
5704 btrfs_release_path(root, path);
5705
5706 __alloc_chunk_for_shrink(root, block_group, 0);
5707 ret = relocate_one_extent(root, path, &key, block_group,
5708 reloc_inode, pass);
5709 BUG_ON(ret < 0);
5710 if (ret > 0)
5711 skipped++;
5712
5713 key.objectid = cur_byte;
5714 key.type = 0;
5715 key.offset = 0;
5716 }
5717
5718 btrfs_release_path(root, path);
5719
5720 if (pass == 0) {
5721 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
5722 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
5723 }
5724
5725 if (total_found > 0) {
5726 printk(KERN_INFO "btrfs found %llu extents in pass %d\n",
5727 (unsigned long long)total_found, pass);
5728 pass++;
5729 if (total_found == skipped && pass > 2) {
5730 iput(reloc_inode);
5731 reloc_inode = create_reloc_inode(info, block_group);
5732 pass = 0;
5733 }
5734 goto again;
5735 }
5736
5737 /* delete reloc_inode */
5738 iput(reloc_inode);
5739
5740 /* unpin extents in this range */
5741 trans = btrfs_start_transaction(info->tree_root, 1);
5742 btrfs_commit_transaction(trans, info->tree_root);
5743
5744 spin_lock(&block_group->lock);
5745 WARN_ON(block_group->pinned > 0);
5746 WARN_ON(block_group->reserved > 0);
5747 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
5748 spin_unlock(&block_group->lock);
5749 put_block_group(block_group);
5750 ret = 0;
5751out:
5752 btrfs_free_path(path);
5753 return ret;
5754}
5755
5756static int find_first_block_group(struct btrfs_root *root,
5757 struct btrfs_path *path, struct btrfs_key *key)
5758{
5759 int ret = 0;
5760 struct btrfs_key found_key;
5761 struct extent_buffer *leaf;
5762 int slot;
5763
5764 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
5765 if (ret < 0)
5766 goto out;
5767
5768 while (1) {
5769 slot = path->slots[0];
5770 leaf = path->nodes[0];
5771 if (slot >= btrfs_header_nritems(leaf)) {
5772 ret = btrfs_next_leaf(root, path);
5773 if (ret == 0)
5774 continue;
5775 if (ret < 0)
5776 goto out;
5777 break;
5778 }
5779 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5780
5781 if (found_key.objectid >= key->objectid &&
5782 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
5783 ret = 0;
5784 goto out;
5785 }
5786 path->slots[0]++;
5787 }
5788 ret = -ENOENT;
5789out:
5790 return ret;
5791}
5792
5793int btrfs_free_block_groups(struct btrfs_fs_info *info)
5794{
5795 struct btrfs_block_group_cache *block_group;
5796 struct rb_node *n;
5797
5798 spin_lock(&info->block_group_cache_lock);
5799 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
5800 block_group = rb_entry(n, struct btrfs_block_group_cache,
5801 cache_node);
5802 rb_erase(&block_group->cache_node,
5803 &info->block_group_cache_tree);
5804 spin_unlock(&info->block_group_cache_lock);
5805
5806 btrfs_remove_free_space_cache(block_group);
5807 down_write(&block_group->space_info->groups_sem);
5808 list_del(&block_group->list);
5809 up_write(&block_group->space_info->groups_sem);
5810
5811 WARN_ON(atomic_read(&block_group->count) != 1);
5812 kfree(block_group);
5813
5814 spin_lock(&info->block_group_cache_lock);
5815 }
5816 spin_unlock(&info->block_group_cache_lock);
5817 return 0;
5818}
5819
5820int btrfs_read_block_groups(struct btrfs_root *root)
5821{
5822 struct btrfs_path *path;
5823 int ret;
5824 struct btrfs_block_group_cache *cache;
5825 struct btrfs_fs_info *info = root->fs_info;
5826 struct btrfs_space_info *space_info;
5827 struct btrfs_key key;
5828 struct btrfs_key found_key;
5829 struct extent_buffer *leaf;
5830
5831 root = info->extent_root;
5832 key.objectid = 0;
5833 key.offset = 0;
5834 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
5835 path = btrfs_alloc_path();
5836 if (!path)
5837 return -ENOMEM;
5838
5839 while (1) {
5840 ret = find_first_block_group(root, path, &key);
5841 if (ret > 0) {
5842 ret = 0;
5843 goto error;
5844 }
5845 if (ret != 0)
5846 goto error;
5847
5848 leaf = path->nodes[0];
5849 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5850 cache = kzalloc(sizeof(*cache), GFP_NOFS);
5851 if (!cache) {
5852 ret = -ENOMEM;
5853 break;
5854 }
5855
5856 atomic_set(&cache->count, 1);
5857 spin_lock_init(&cache->lock);
5858 mutex_init(&cache->alloc_mutex);
5859 mutex_init(&cache->cache_mutex);
5860 INIT_LIST_HEAD(&cache->list);
5861 read_extent_buffer(leaf, &cache->item,
5862 btrfs_item_ptr_offset(leaf, path->slots[0]),
5863 sizeof(cache->item));
5864 memcpy(&cache->key, &found_key, sizeof(found_key));
5865
5866 key.objectid = found_key.objectid + found_key.offset;
5867 btrfs_release_path(root, path);
5868 cache->flags = btrfs_block_group_flags(&cache->item);
5869
5870 ret = update_space_info(info, cache->flags, found_key.offset,
5871 btrfs_block_group_used(&cache->item),
5872 &space_info);
5873 BUG_ON(ret);
5874 cache->space_info = space_info;
5875 down_write(&space_info->groups_sem);
5876 list_add_tail(&cache->list, &space_info->block_groups);
5877 up_write(&space_info->groups_sem);
5878
5879 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5880 BUG_ON(ret);
5881
5882 set_avail_alloc_bits(root->fs_info, cache->flags);
5883 if (btrfs_chunk_readonly(root, cache->key.objectid))
5884 set_block_group_readonly(cache);
5885 }
5886 ret = 0;
5887error:
5888 btrfs_free_path(path);
5889 return ret;
5890}
5891
5892int btrfs_make_block_group(struct btrfs_trans_handle *trans,
5893 struct btrfs_root *root, u64 bytes_used,
5894 u64 type, u64 chunk_objectid, u64 chunk_offset,
5895 u64 size)
5896{
5897 int ret;
5898 struct btrfs_root *extent_root;
5899 struct btrfs_block_group_cache *cache;
5900
5901 extent_root = root->fs_info->extent_root;
5902
5903 root->fs_info->last_trans_new_blockgroup = trans->transid;
5904
5905 cache = kzalloc(sizeof(*cache), GFP_NOFS);
5906 if (!cache)
5907 return -ENOMEM;
5908
5909 cache->key.objectid = chunk_offset;
5910 cache->key.offset = size;
5911 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
5912 atomic_set(&cache->count, 1);
5913 spin_lock_init(&cache->lock);
5914 mutex_init(&cache->alloc_mutex);
5915 mutex_init(&cache->cache_mutex);
5916 INIT_LIST_HEAD(&cache->list);
5917
5918 btrfs_set_block_group_used(&cache->item, bytes_used);
5919 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
5920 cache->flags = type;
5921 btrfs_set_block_group_flags(&cache->item, type);
5922
5923 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
5924 &cache->space_info);
5925 BUG_ON(ret);
5926 down_write(&cache->space_info->groups_sem);
5927 list_add_tail(&cache->list, &cache->space_info->block_groups);
5928 up_write(&cache->space_info->groups_sem);
5929
5930 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5931 BUG_ON(ret);
5932
5933 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
5934 sizeof(cache->item));
5935 BUG_ON(ret);
5936
5937 finish_current_insert(trans, extent_root, 0);
5938 ret = del_pending_extents(trans, extent_root, 0);
5939 BUG_ON(ret);
5940 set_avail_alloc_bits(extent_root->fs_info, type);
5941
5942 return 0;
5943}
5944
5945int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
5946 struct btrfs_root *root, u64 group_start)
5947{
5948 struct btrfs_path *path;
5949 struct btrfs_block_group_cache *block_group;
5950 struct btrfs_key key;
5951 int ret;
5952
5953 root = root->fs_info->extent_root;
5954
5955 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
5956 BUG_ON(!block_group);
5957 BUG_ON(!block_group->ro);
5958
5959 memcpy(&key, &block_group->key, sizeof(key));
5960
5961 path = btrfs_alloc_path();
5962 BUG_ON(!path);
5963
5964 btrfs_remove_free_space_cache(block_group);
5965 rb_erase(&block_group->cache_node,
5966 &root->fs_info->block_group_cache_tree);
5967 down_write(&block_group->space_info->groups_sem);
5968 list_del(&block_group->list);
5969 up_write(&block_group->space_info->groups_sem);
5970
5971 spin_lock(&block_group->space_info->lock);
5972 block_group->space_info->total_bytes -= block_group->key.offset;
5973 block_group->space_info->bytes_readonly -= block_group->key.offset;
5974 spin_unlock(&block_group->space_info->lock);
5975 block_group->space_info->full = 0;
5976
5977 put_block_group(block_group);
5978 put_block_group(block_group);
5979
5980 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5981 if (ret > 0)
5982 ret = -EIO;
5983 if (ret < 0)
5984 goto out;
5985
5986 ret = btrfs_del_item(trans, root, path);
5987out:
5988 btrfs_free_path(path);
5989 return ret;
5990}