Update converter for the new format
[platform/upstream/btrfs-progs.git] / extent-tree.c
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
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include "kerncompat.h"
22 #include "radix-tree.h"
23 #include "ctree.h"
24 #include "disk-io.h"
25 #include "print-tree.h"
26 #include "transaction.h"
27 #include "crc32c.h"
28 #include "volumes.h"
29
30 #define BLOCK_GROUP_DATA     EXTENT_WRITEBACK
31 #define BLOCK_GROUP_METADATA EXTENT_UPTODATE
32 #define BLOCK_GROUP_SYSTEM   EXTENT_NEW
33
34 #define BLOCK_GROUP_DIRTY EXTENT_DIRTY
35
36 #define PENDING_EXTENT_INSERT 0
37 #define PENDING_EXTENT_DELETE 1
38 #define PENDING_BACKREF_UPDATE 2
39
40 struct pending_extent_op {
41         int type;
42         u64 bytenr;
43         u64 num_bytes;
44         u64 flags;
45         struct btrfs_disk_key key;
46         int level;
47 };
48
49 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
50                                      struct btrfs_root *root,
51                                      u64 root_objectid, u64 generation,
52                                      u64 flags, struct btrfs_disk_key *key,
53                                      int level, struct btrfs_key *ins);
54 static int __free_extent(struct btrfs_trans_handle *trans,
55                          struct btrfs_root *root,
56                          u64 bytenr, u64 num_bytes, u64 parent,
57                          u64 root_objectid, u64 owner_objectid,
58                          u64 owner_offset, int refs_to_drop);
59 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
60                                  btrfs_root *extent_root);
61 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
62                                btrfs_root *extent_root);
63
64 static int remove_sb_from_cache(struct btrfs_root *root,
65                                 struct btrfs_block_group_cache *cache)
66 {
67         u64 bytenr;
68         u64 *logical;
69         int stripe_len;
70         int i, nr, ret;
71         struct extent_io_tree *free_space_cache;
72
73         free_space_cache = &root->fs_info->free_space_cache;
74         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
75                 bytenr = btrfs_sb_offset(i);
76                 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
77                                        cache->key.objectid, bytenr, 0,
78                                        &logical, &nr, &stripe_len);
79                 BUG_ON(ret);
80                 while (nr--) {
81                         clear_extent_dirty(free_space_cache, logical[nr],
82                                 logical[nr] + stripe_len - 1, GFP_NOFS);
83                 }
84                 kfree(logical);
85         }
86         return 0;
87 }
88
89 static int cache_block_group(struct btrfs_root *root,
90                              struct btrfs_block_group_cache *block_group)
91 {
92         struct btrfs_path *path;
93         int ret;
94         struct btrfs_key key;
95         struct extent_buffer *leaf;
96         struct extent_io_tree *free_space_cache;
97         int slot;
98         u64 last;
99         u64 hole_size;
100
101         if (!block_group)
102                 return 0;
103
104         root = root->fs_info->extent_root;
105         free_space_cache = &root->fs_info->free_space_cache;
106
107         if (block_group->cached)
108                 return 0;
109
110         path = btrfs_alloc_path();
111         if (!path)
112                 return -ENOMEM;
113
114         path->reada = 2;
115         last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
116         key.objectid = last;
117         key.offset = 0;
118         btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
119         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
120         if (ret < 0)
121                 goto err;
122
123         while(1) {
124                 leaf = path->nodes[0];
125                 slot = path->slots[0];
126                 if (slot >= btrfs_header_nritems(leaf)) {
127                         ret = btrfs_next_leaf(root, path);
128                         if (ret < 0)
129                                 goto err;
130                         if (ret == 0) {
131                                 continue;
132                         } else {
133                                 break;
134                         }
135                 }
136                 btrfs_item_key_to_cpu(leaf, &key, slot);
137                 if (key.objectid < block_group->key.objectid) {
138                         goto next;
139                 }
140                 if (key.objectid >= block_group->key.objectid +
141                     block_group->key.offset) {
142                         break;
143                 }
144
145                 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
146                         if (key.objectid > last) {
147                                 hole_size = key.objectid - last;
148                                 set_extent_dirty(free_space_cache, last,
149                                                  last + hole_size - 1,
150                                                  GFP_NOFS);
151                         }
152                         last = key.objectid + key.offset;
153                 }
154 next:
155                 path->slots[0]++;
156         }
157
158         if (block_group->key.objectid +
159             block_group->key.offset > last) {
160                 hole_size = block_group->key.objectid +
161                         block_group->key.offset - last;
162                 set_extent_dirty(free_space_cache, last,
163                                  last + hole_size - 1, GFP_NOFS);
164         }
165         remove_sb_from_cache(root, block_group);
166         block_group->cached = 1;
167 err:
168         btrfs_free_path(path);
169         return 0;
170 }
171
172 struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
173                                                        btrfs_fs_info *info,
174                                                        u64 bytenr)
175 {
176         struct extent_io_tree *block_group_cache;
177         struct btrfs_block_group_cache *block_group = NULL;
178         u64 ptr;
179         u64 start;
180         u64 end;
181         int ret;
182
183         bytenr = max_t(u64, bytenr,
184                        BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
185         block_group_cache = &info->block_group_cache;
186         ret = find_first_extent_bit(block_group_cache,
187                                     bytenr, &start, &end,
188                                     BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
189                                     BLOCK_GROUP_SYSTEM);
190         if (ret) {
191                 return NULL;
192         }
193         ret = get_state_private(block_group_cache, start, &ptr);
194         if (ret)
195                 return NULL;
196
197         block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
198         return block_group;
199 }
200
201 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
202                                                          btrfs_fs_info *info,
203                                                          u64 bytenr)
204 {
205         struct extent_io_tree *block_group_cache;
206         struct btrfs_block_group_cache *block_group = NULL;
207         u64 ptr;
208         u64 start;
209         u64 end;
210         int ret;
211
212         block_group_cache = &info->block_group_cache;
213         ret = find_first_extent_bit(block_group_cache,
214                                     bytenr, &start, &end,
215                                     BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
216                                     BLOCK_GROUP_SYSTEM);
217         if (ret) {
218                 return NULL;
219         }
220         ret = get_state_private(block_group_cache, start, &ptr);
221         if (ret)
222                 return NULL;
223
224         block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
225         if (block_group->key.objectid <= bytenr && bytenr <
226             block_group->key.objectid + block_group->key.offset)
227                 return block_group;
228         return NULL;
229 }
230
231 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
232 {
233         return (cache->flags & bits) == bits;
234 }
235
236 static int noinline find_search_start(struct btrfs_root *root,
237                               struct btrfs_block_group_cache **cache_ret,
238                               u64 *start_ret, int num, int data)
239 {
240         int ret;
241         struct btrfs_block_group_cache *cache = *cache_ret;
242         u64 last;
243         u64 start = 0;
244         u64 end = 0;
245         u64 search_start = *start_ret;
246         int wrapped = 0;
247
248         if (!cache) {
249                 goto out;
250         }
251 again:
252         ret = cache_block_group(root, cache);
253         if (ret)
254                 goto out;
255
256         last = max(search_start, cache->key.objectid);
257         if (cache->ro || !block_group_bits(cache, data)) {
258                 goto new_group;
259         }
260
261         while(1) {
262                 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
263                                             last, &start, &end, EXTENT_DIRTY);
264                 if (ret) {
265                         goto new_group;
266                 }
267
268                 start = max(last, start);
269                 last = end + 1;
270                 if (last - start < num) {
271                         continue;
272                 }
273                 if (start + num > cache->key.objectid + cache->key.offset) {
274                         goto new_group;
275                 }
276                 *start_ret = start;
277                 return 0;
278         }
279 out:
280         cache = btrfs_lookup_block_group(root->fs_info, search_start);
281         if (!cache) {
282                 printk("Unable to find block group for %llu\n",
283                         (unsigned long long)search_start);
284                 WARN_ON(1);
285         }
286         return -ENOSPC;
287
288 new_group:
289         last = cache->key.objectid + cache->key.offset;
290 wrapped:
291         cache = btrfs_lookup_first_block_group(root->fs_info, last);
292         if (!cache) {
293 no_cache:
294                 if (!wrapped) {
295                         wrapped = 1;
296                         last = search_start;
297                         goto wrapped;
298                 }
299                 goto out;
300         }
301         cache = btrfs_find_block_group(root, cache, last, data, 0);
302         cache = btrfs_find_block_group(root, cache, last, data, 0);
303         if (!cache)
304                 goto no_cache;
305
306         *cache_ret = cache;
307         goto again;
308 }
309
310 static u64 div_factor(u64 num, int factor)
311 {
312         if (factor == 10)
313                 return num;
314         num *= factor;
315         num /= 10;
316         return num;
317 }
318
319 static int block_group_state_bits(u64 flags)
320 {
321         int bits = 0;
322         if (flags & BTRFS_BLOCK_GROUP_DATA)
323                 bits |= BLOCK_GROUP_DATA;
324         if (flags & BTRFS_BLOCK_GROUP_METADATA)
325                 bits |= BLOCK_GROUP_METADATA;
326         if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
327                 bits |= BLOCK_GROUP_SYSTEM;
328         return bits;
329 }
330
331 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
332                                                  struct btrfs_block_group_cache
333                                                  *hint, u64 search_start,
334                                                  int data, int owner)
335 {
336         struct btrfs_block_group_cache *cache;
337         struct extent_io_tree *block_group_cache;
338         struct btrfs_block_group_cache *found_group = NULL;
339         struct btrfs_fs_info *info = root->fs_info;
340         u64 used;
341         u64 last = 0;
342         u64 hint_last;
343         u64 start;
344         u64 end;
345         u64 free_check;
346         u64 ptr;
347         int bit;
348         int ret;
349         int full_search = 0;
350         int factor = 10;
351
352         block_group_cache = &info->block_group_cache;
353
354         if (!owner)
355                 factor = 10;
356
357         bit = block_group_state_bits(data);
358
359         if (search_start) {
360                 struct btrfs_block_group_cache *shint;
361                 shint = btrfs_lookup_block_group(info, search_start);
362                 if (shint && !shint->ro && block_group_bits(shint, data)) {
363                         used = btrfs_block_group_used(&shint->item);
364                         if (used + shint->pinned <
365                             div_factor(shint->key.offset, factor)) {
366                                 return shint;
367                         }
368                 }
369         }
370         if (hint && !hint->ro && block_group_bits(hint, data)) {
371                 used = btrfs_block_group_used(&hint->item);
372                 if (used + hint->pinned <
373                     div_factor(hint->key.offset, factor)) {
374                         return hint;
375                 }
376                 last = hint->key.objectid + hint->key.offset;
377                 hint_last = last;
378         } else {
379                 if (hint)
380                         hint_last = max(hint->key.objectid, search_start);
381                 else
382                         hint_last = search_start;
383
384                 last = hint_last;
385         }
386 again:
387         while(1) {
388                 ret = find_first_extent_bit(block_group_cache, last,
389                                             &start, &end, bit);
390                 if (ret)
391                         break;
392
393                 ret = get_state_private(block_group_cache, start, &ptr);
394                 if (ret)
395                         break;
396
397                 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
398                 last = cache->key.objectid + cache->key.offset;
399                 used = btrfs_block_group_used(&cache->item);
400
401                 if (!cache->ro && block_group_bits(cache, data)) {
402                         if (full_search)
403                                 free_check = cache->key.offset;
404                         else
405                                 free_check = div_factor(cache->key.offset,
406                                                         factor);
407
408                         if (used + cache->pinned < free_check) {
409                                 found_group = cache;
410                                 goto found;
411                         }
412                 }
413                 cond_resched();
414         }
415         if (!full_search) {
416                 last = search_start;
417                 full_search = 1;
418                 goto again;
419         }
420 found:
421         return found_group;
422 }
423
424 /*
425  * Back reference rules.  Back refs have three main goals:
426  *
427  * 1) differentiate between all holders of references to an extent so that
428  *    when a reference is dropped we can make sure it was a valid reference
429  *    before freeing the extent.
430  *
431  * 2) Provide enough information to quickly find the holders of an extent
432  *    if we notice a given block is corrupted or bad.
433  *
434  * 3) Make it easy to migrate blocks for FS shrinking or storage pool
435  *    maintenance.  This is actually the same as #2, but with a slightly
436  *    different use case.
437  *
438  * There are two kinds of back refs. The implicit back refs is optimized
439  * for pointers in non-shared tree blocks. For a given pointer in a block,
440  * back refs of this kind provide information about the block's owner tree
441  * and the pointer's key. These information allow us to find the block by
442  * b-tree searching. The full back refs is for pointers in tree blocks not
443  * referenced by their owner trees. The location of tree block is recorded
444  * in the back refs. Actually the full back refs is generic, and can be
445  * used in all cases the implicit back refs is used. The major shortcoming
446  * of the full back refs is its overhead. Every time a tree block gets
447  * COWed, we have to update back refs entry for all pointers in it.
448  *
449  * For a newly allocated tree block, we use implicit back refs for
450  * pointers in it. This means most tree related operations only involve
451  * implicit back refs. For a tree block created in old transaction, the
452  * only way to drop a reference to it is COW it. So we can detect the
453  * event that tree block loses its owner tree's reference and do the
454  * back refs conversion.
455  *
456  * When a tree block is COW'd through a tree, there are four cases:
457  *
458  * The reference count of the block is one and the tree is the block's
459  * owner tree. Nothing to do in this case.
460  *
461  * The reference count of the block is one and the tree is not the
462  * block's owner tree. In this case, full back refs is used for pointers
463  * in the block. Remove these full back refs, add implicit back refs for
464  * every pointers in the new block.
465  *
466  * The reference count of the block is greater than one and the tree is
467  * the block's owner tree. In this case, implicit back refs is used for
468  * pointers in the block. Add full back refs for every pointers in the
469  * block, increase lower level extents' reference counts. The original
470  * implicit back refs are entailed to the new block.
471  *
472  * The reference count of the block is greater than one and the tree is
473  * not the block's owner tree. Add implicit back refs for every pointer in
474  * the new block, increase lower level extents' reference count.
475  *
476  * Back Reference Key composing:
477  *
478  * The key objectid corresponds to the first byte in the extent,
479  * The key type is used to differentiate between types of back refs.
480  * There are different meanings of the key offset for different types
481  * of back refs.
482  *
483  * File extents can be referenced by:
484  *
485  * - multiple snapshots, subvolumes, or different generations in one subvol
486  * - different files inside a single subvolume
487  * - different offsets inside a file (bookend extents in file.c)
488  *
489  * The extent ref structure for the implicit back refs has fields for:
490  *
491  * - Objectid of the subvolume root
492  * - objectid of the file holding the reference
493  * - original offset in the file
494  * - how many bookend extents
495  *
496  * The key offset for the implicit back refs is hash of the first
497  * three fields.
498  *
499  * The extent ref structure for the full back refs has field for:
500  *
501  * - number of pointers in the tree leaf
502  *
503  * The key offset for the implicit back refs is the first byte of
504  * the tree leaf
505  *
506  * When a file extent is allocated, The implicit back refs is used.
507  * the fields are filled in:
508  *
509  *     (root_key.objectid, inode objectid, offset in file, 1)
510  *
511  * When a file extent is removed file truncation, we find the
512  * corresponding implicit back refs and check the following fields:
513  *
514  *     (btrfs_header_owner(leaf), inode objectid, offset in file)
515  *
516  * Btree extents can be referenced by:
517  *
518  * - Different subvolumes
519  *
520  * Both the implicit back refs and the full back refs for tree blocks
521  * only consist of key. The key offset for the implicit back refs is
522  * objectid of block's owner tree. The key offset for the full back refs
523  * is the first byte of parent block.
524  *
525  * When implicit back refs is used, information about the lowest key and
526  * level of the tree block are required. These information are stored in
527  * tree block info structure.
528  */
529
530 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
531 static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
532                                   struct btrfs_root *root,
533                                   struct btrfs_path *path,
534                                   u64 owner, u32 extra_size)
535 {
536         struct btrfs_extent_item *item;
537         struct btrfs_extent_item_v0 *ei0;
538         struct btrfs_extent_ref_v0 *ref0;
539         struct btrfs_tree_block_info *bi;
540         struct extent_buffer *leaf;
541         struct btrfs_key key;
542         struct btrfs_key found_key;
543         u32 new_size = sizeof(*item);
544         u64 refs;
545         int ret;
546
547         leaf = path->nodes[0];
548         BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
549
550         btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
551         ei0 = btrfs_item_ptr(leaf, path->slots[0],
552                              struct btrfs_extent_item_v0);
553         refs = btrfs_extent_refs_v0(leaf, ei0);
554
555         if (owner == (u64)-1) {
556                 while (1) {
557                         if (path->slots[0] >= btrfs_header_nritems(leaf)) {
558                                 ret = btrfs_next_leaf(root, path);
559                                 if (ret < 0)
560                                         return ret;
561                                 BUG_ON(ret > 0);
562                                 leaf = path->nodes[0];
563                         }
564                         btrfs_item_key_to_cpu(leaf, &found_key,
565                                               path->slots[0]);
566                         BUG_ON(key.objectid != found_key.objectid);
567                         if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
568                                 path->slots[0]++;
569                                 continue;
570                         }
571                         ref0 = btrfs_item_ptr(leaf, path->slots[0],
572                                               struct btrfs_extent_ref_v0);
573                         owner = btrfs_ref_objectid_v0(leaf, ref0);
574                         break;
575                 }
576         }
577         btrfs_release_path(root, path);
578
579         if (owner < BTRFS_FIRST_FREE_OBJECTID)
580                 new_size += sizeof(*bi);
581
582         new_size -= sizeof(*ei0);
583         ret = btrfs_search_slot(trans, root, &key, path, new_size, 1);
584         if (ret < 0)
585                 return ret;
586         BUG_ON(ret);
587
588         ret = btrfs_extend_item(trans, root, path, new_size);
589         BUG_ON(ret);
590
591         leaf = path->nodes[0];
592         item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
593         btrfs_set_extent_refs(leaf, item, refs);
594         /* FIXME: get real generation */
595         btrfs_set_extent_generation(leaf, item, 0);
596         if (owner < BTRFS_FIRST_FREE_OBJECTID) {
597                 btrfs_set_extent_flags(leaf, item,
598                                        BTRFS_EXTENT_FLAG_TREE_BLOCK |
599                                        BTRFS_BLOCK_FLAG_FULL_BACKREF);
600                 bi = (struct btrfs_tree_block_info *)(item + 1);
601                 /* FIXME: get first key of the block */
602                 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
603                 btrfs_set_tree_block_level(leaf, bi, (int)owner);
604         } else {
605                 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
606         }
607         btrfs_mark_buffer_dirty(leaf);
608         return 0;
609 }
610 #endif
611
612 static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
613 {
614         u32 high_crc = ~(u32)0;
615         u32 low_crc = ~(u32)0;
616         __le64 lenum;
617
618         lenum = cpu_to_le64(root_objectid);
619         high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
620         lenum = cpu_to_le64(owner);
621         low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
622         lenum = cpu_to_le64(offset);
623         low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
624
625         return ((u64)high_crc << 31) ^ (u64)low_crc;
626 }
627
628 static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
629                                      struct btrfs_extent_data_ref *ref)
630 {
631         return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
632                                     btrfs_extent_data_ref_objectid(leaf, ref),
633                                     btrfs_extent_data_ref_offset(leaf, ref));
634 }
635
636 static int match_extent_data_ref(struct extent_buffer *leaf,
637                                  struct btrfs_extent_data_ref *ref,
638                                  u64 root_objectid, u64 owner, u64 offset)
639 {
640         if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
641             btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
642             btrfs_extent_data_ref_offset(leaf, ref) != offset)
643                 return 0;
644         return 1;
645 }
646
647 static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
648                                            struct btrfs_root *root,
649                                            struct btrfs_path *path,
650                                            u64 bytenr, u64 parent,
651                                            u64 root_objectid,
652                                            u64 owner, u64 offset)
653 {
654         struct btrfs_key key;
655         struct btrfs_extent_data_ref *ref;
656         struct extent_buffer *leaf;
657         u32 nritems;
658         int ret;
659         int recow;
660         int err = -ENOENT;
661
662         key.objectid = bytenr;
663         if (parent) {
664                 key.type = BTRFS_SHARED_DATA_REF_KEY;
665                 key.offset = parent;
666         } else {
667                 key.type = BTRFS_EXTENT_DATA_REF_KEY;
668                 key.offset = hash_extent_data_ref(root_objectid,
669                                                   owner, offset);
670         }
671 again:
672         recow = 0;
673         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
674         if (ret < 0) {
675                 err = ret;
676                 goto fail;
677         }
678
679         if (parent) {
680                 if (!ret)
681                         return 0;
682 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
683                 key.type = BTRFS_EXTENT_REF_V0_KEY;
684                 btrfs_release_path(root, path);
685                 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
686                 if (ret < 0) {
687                         err = ret;
688                         goto fail;
689                 }
690                 if (!ret)
691                         return 0;
692 #endif
693                 goto fail;
694         }
695
696         leaf = path->nodes[0];
697         nritems = btrfs_header_nritems(leaf);
698         while (1) {
699                 if (path->slots[0] >= nritems) {
700                         ret = btrfs_next_leaf(root, path);
701                         if (ret < 0)
702                                 err = ret;
703                         if (ret)
704                                 goto fail;
705
706                         leaf = path->nodes[0];
707                         nritems = btrfs_header_nritems(leaf);
708                         recow = 1;
709                 }
710
711                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
712                 if (key.objectid != bytenr ||
713                     key.type != BTRFS_EXTENT_DATA_REF_KEY)
714                         goto fail;
715                 
716                 ref = btrfs_item_ptr(leaf, path->slots[0],
717                                      struct btrfs_extent_data_ref);
718
719                 if (match_extent_data_ref(leaf, ref, root_objectid,
720                                           owner, offset)) {
721                         if (recow) {
722                                 btrfs_release_path(root, path);
723                                 goto again;
724                         }
725                         err = 0;
726                         break;
727                 }
728                 path->slots[0]++;
729         }
730 fail:
731         return err;
732 }
733
734 static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
735                                            struct btrfs_root *root,
736                                            struct btrfs_path *path,
737                                            u64 bytenr, u64 parent,
738                                            u64 root_objectid, u64 owner,
739                                            u64 offset, int refs_to_add)
740 {
741         struct btrfs_key key;
742         struct extent_buffer *leaf;
743         u32 size;
744         u32 num_refs;
745         int ret;
746
747         key.objectid = bytenr;
748         if (parent) {
749                 key.type = BTRFS_SHARED_DATA_REF_KEY;
750                 key.offset = parent;
751                 size = sizeof(struct btrfs_shared_data_ref);
752         } else {
753                 key.type = BTRFS_EXTENT_DATA_REF_KEY;
754                 key.offset = hash_extent_data_ref(root_objectid,
755                                                   owner, offset);
756                 size = sizeof(struct btrfs_extent_data_ref);
757         }
758
759         ret = btrfs_insert_empty_item(trans, root, path, &key, size);
760         if (ret && ret != -EEXIST)
761                 goto fail;
762
763         leaf = path->nodes[0];
764         if (parent) {
765                 struct btrfs_shared_data_ref *ref;
766                 ref = btrfs_item_ptr(leaf, path->slots[0],
767                                      struct btrfs_shared_data_ref);
768                 if (ret == 0) {
769                         btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
770                 } else {
771                         num_refs = btrfs_shared_data_ref_count(leaf, ref);
772                         num_refs += refs_to_add;
773                         btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
774                 }
775         } else {
776                 struct btrfs_extent_data_ref *ref;
777                 while (ret == -EEXIST) {
778                         ref = btrfs_item_ptr(leaf, path->slots[0],
779                                              struct btrfs_extent_data_ref);
780                         if (match_extent_data_ref(leaf, ref, root_objectid,
781                                                   owner, offset))
782                                 break;
783                         btrfs_release_path(root, path);
784
785                         key.offset++;
786                         ret = btrfs_insert_empty_item(trans, root, path, &key,
787                                                       size);
788                         if (ret && ret != -EEXIST)
789                                 goto fail;
790
791                         leaf = path->nodes[0];
792                 }
793                 ref = btrfs_item_ptr(leaf, path->slots[0],
794                                      struct btrfs_extent_data_ref);
795                 if (ret == 0) {
796                         btrfs_set_extent_data_ref_root(leaf, ref,
797                                                        root_objectid);
798                         btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
799                         btrfs_set_extent_data_ref_offset(leaf, ref, offset);
800                         btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
801                 } else {
802                         num_refs = btrfs_extent_data_ref_count(leaf, ref);
803                         num_refs += refs_to_add;
804                         btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
805                 }
806         }
807         btrfs_mark_buffer_dirty(leaf);
808         ret = 0;
809 fail:
810         btrfs_release_path(root, path);
811         return ret;
812 }
813
814 static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
815                                            struct btrfs_root *root,
816                                            struct btrfs_path *path,
817                                            int refs_to_drop)
818 {
819         struct btrfs_key key;
820         struct btrfs_extent_data_ref *ref1 = NULL;
821         struct btrfs_shared_data_ref *ref2 = NULL;
822         struct extent_buffer *leaf;
823         u32 num_refs = 0;
824         int ret = 0;
825
826         leaf = path->nodes[0];
827         btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
828
829         if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
830                 ref1 = btrfs_item_ptr(leaf, path->slots[0],
831                                       struct btrfs_extent_data_ref);
832                 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
833         } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
834                 ref2 = btrfs_item_ptr(leaf, path->slots[0],
835                                       struct btrfs_shared_data_ref);
836                 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
837 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
838         } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
839                 struct btrfs_extent_ref_v0 *ref0;
840                 ref0 = btrfs_item_ptr(leaf, path->slots[0],
841                                       struct btrfs_extent_ref_v0);
842                 num_refs = btrfs_ref_count_v0(leaf, ref0);
843 #endif
844         } else {
845                 BUG();
846         }
847
848         BUG_ON(num_refs < refs_to_drop);
849         num_refs -= refs_to_drop;
850
851         if (num_refs == 0) {
852                 ret = btrfs_del_item(trans, root, path);
853         } else {
854                 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
855                         btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
856                 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
857                         btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
858 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
859                 else {
860                         struct btrfs_extent_ref_v0 *ref0;
861                         ref0 = btrfs_item_ptr(leaf, path->slots[0],
862                                         struct btrfs_extent_ref_v0);
863                         btrfs_set_ref_count_v0(leaf, ref0, num_refs);
864                 }
865 #endif
866                 btrfs_mark_buffer_dirty(leaf);
867         }
868         return ret;
869 }
870
871 static noinline u32 extent_data_ref_count(struct btrfs_root *root,
872                                           struct btrfs_path *path,
873                                           struct btrfs_extent_inline_ref *iref)
874 {
875         struct btrfs_key key;
876         struct extent_buffer *leaf;
877         struct btrfs_extent_data_ref *ref1;
878         struct btrfs_shared_data_ref *ref2;
879         u32 num_refs = 0;
880
881         leaf = path->nodes[0];
882         btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
883         if (iref) {
884                 if (btrfs_extent_inline_ref_type(leaf, iref) ==
885                     BTRFS_EXTENT_DATA_REF_KEY) {
886                         ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
887                         num_refs = btrfs_extent_data_ref_count(leaf, ref1);
888                 } else {
889                         ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
890                         num_refs = btrfs_shared_data_ref_count(leaf, ref2);
891                 }
892         } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
893                 ref1 = btrfs_item_ptr(leaf, path->slots[0],
894                                       struct btrfs_extent_data_ref);
895                 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
896         } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
897                 ref2 = btrfs_item_ptr(leaf, path->slots[0],
898                                       struct btrfs_shared_data_ref);
899                 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
900 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
901         } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
902                 struct btrfs_extent_ref_v0 *ref0;
903                 ref0 = btrfs_item_ptr(leaf, path->slots[0],
904                                       struct btrfs_extent_ref_v0);
905                 num_refs = btrfs_ref_count_v0(leaf, ref0);
906 #endif
907         } else {
908                 BUG();
909         }
910         return num_refs;
911 }
912
913 static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
914                                           struct btrfs_root *root,
915                                           struct btrfs_path *path,
916                                           u64 bytenr, u64 parent,
917                                           u64 root_objectid)
918 {
919         struct btrfs_key key;
920         int ret;
921
922         key.objectid = bytenr;
923         if (parent) {
924                 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
925                 key.offset = parent;
926         } else {
927                 key.type = BTRFS_TREE_BLOCK_REF_KEY;
928                 key.offset = root_objectid;
929         }
930
931         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
932         if (ret > 0)
933                 ret = -ENOENT;
934 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
935         if (ret == -ENOENT && parent) {
936                 btrfs_release_path(root, path);
937                 key.type = BTRFS_EXTENT_REF_V0_KEY;
938                 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
939                 if (ret > 0)
940                         ret = -ENOENT;
941         }
942 #endif
943         return ret;
944 }
945
946 static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
947                                           struct btrfs_root *root,
948                                           struct btrfs_path *path,
949                                           u64 bytenr, u64 parent,
950                                           u64 root_objectid)
951 {
952         struct btrfs_key key;
953         int ret;
954
955         key.objectid = bytenr;
956         if (parent) {
957                 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
958                 key.offset = parent;
959         } else {
960                 key.type = BTRFS_TREE_BLOCK_REF_KEY;
961                 key.offset = root_objectid;
962         }
963
964         ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
965
966         btrfs_release_path(root, path);
967         return ret;
968 }
969
970 static inline int extent_ref_type(u64 parent, u64 owner)
971 {
972         if (owner < BTRFS_FIRST_FREE_OBJECTID) {
973                 if (parent > 0)
974                         return BTRFS_SHARED_BLOCK_REF_KEY;
975                 else
976                         return BTRFS_TREE_BLOCK_REF_KEY;
977         } else {
978                 if (parent > 0)
979                         return BTRFS_SHARED_DATA_REF_KEY;
980                 else
981                         return BTRFS_EXTENT_DATA_REF_KEY;
982         }
983 }
984
985 static int find_next_key(struct btrfs_path *path, struct btrfs_key *key)
986
987 {
988         int level;
989         for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
990                 if (!path->nodes[level])
991                         break;
992                 if (path->slots[level] + 1 >=
993                     btrfs_header_nritems(path->nodes[level]))
994                         continue;
995                 if (level == 0)
996                         btrfs_item_key_to_cpu(path->nodes[level], key,
997                                               path->slots[level] + 1);
998                 else
999                         btrfs_node_key_to_cpu(path->nodes[level], key,
1000                                               path->slots[level] + 1);
1001                 return 0;
1002         }
1003         return 1;
1004 }
1005
1006 static int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1007                                  struct btrfs_root *root,
1008                                  struct btrfs_path *path,
1009                                  struct btrfs_extent_inline_ref **ref_ret,
1010                                  u64 bytenr, u64 num_bytes,
1011                                  u64 parent, u64 root_objectid,
1012                                  u64 owner, u64 offset, int insert)
1013 {
1014         struct btrfs_key key;
1015         struct extent_buffer *leaf;
1016         struct btrfs_extent_item *ei;
1017         struct btrfs_extent_inline_ref *iref;
1018         u64 flags;
1019         u32 item_size;
1020         unsigned long ptr;
1021         unsigned long end;
1022         int extra_size;
1023         int type;
1024         int want;
1025         int ret;
1026         int err = 0;
1027
1028         key.objectid = bytenr;
1029         key.type = BTRFS_EXTENT_ITEM_KEY;
1030         key.offset = num_bytes;
1031
1032         want = extent_ref_type(parent, owner);
1033         if (insert)
1034                 extra_size = btrfs_extent_inline_ref_size(want);
1035         else
1036                 extra_size = -1;
1037         ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1038         if (ret < 0) {
1039                 err = ret;
1040                 goto out;
1041         }
1042         BUG_ON(ret);
1043
1044         leaf = path->nodes[0];
1045         item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1046 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1047         if (item_size < sizeof(*ei)) {
1048                 if (!insert) {
1049                         err = -ENOENT;
1050                         goto out;
1051                 }
1052                 ret = convert_extent_item_v0(trans, root, path, owner,
1053                                              extra_size);
1054                 if (ret < 0) {
1055                         err = ret;
1056                         goto out;
1057                 }
1058                 leaf = path->nodes[0];
1059                 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1060         }
1061 #endif
1062         BUG_ON(item_size < sizeof(*ei));
1063
1064         if (owner < BTRFS_FIRST_FREE_OBJECTID && insert &&
1065             item_size + extra_size >= BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1066                 err = -EAGAIN;
1067                 goto out;
1068         }
1069
1070         ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1071         flags = btrfs_extent_flags(leaf, ei);
1072
1073         ptr = (unsigned long)(ei + 1);
1074         end = (unsigned long)ei + item_size;
1075
1076         if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1077                 ptr += sizeof(struct btrfs_tree_block_info);
1078                 BUG_ON(ptr > end);
1079         } else {
1080                 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1081         }
1082
1083         err = -ENOENT;
1084         while (1) {
1085                 if (ptr >= end) {
1086                         WARN_ON(ptr > end);
1087                         break;
1088                 }
1089                 iref = (struct btrfs_extent_inline_ref *)ptr;
1090                 type = btrfs_extent_inline_ref_type(leaf, iref);
1091                 if (want < type)
1092                         break;
1093                 if (want > type) {
1094                         ptr += btrfs_extent_inline_ref_size(type);
1095                         continue;
1096                 }
1097
1098                 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1099                         struct btrfs_extent_data_ref *dref;
1100                         dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1101                         if (match_extent_data_ref(leaf, dref, root_objectid,
1102                                                   owner, offset)) {
1103                                 err = 0;
1104                                 break;
1105                         }
1106                         if (hash_extent_data_ref_item(leaf, dref) <
1107                             hash_extent_data_ref(root_objectid, owner, offset))
1108                                 break;
1109                 } else {
1110                         u64 ref_offset;
1111                         ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1112                         if (parent > 0) {
1113                                 if (parent == ref_offset) {
1114                                         err = 0;
1115                                         break;
1116                                 }
1117                                 if (ref_offset < parent)
1118                                         break;
1119                         } else {
1120                                 if (root_objectid == ref_offset) {
1121                                         err = 0;
1122                                         break;
1123                                 }
1124                                 if (ref_offset < root_objectid)
1125                                         break;
1126                         }
1127                 }
1128                 ptr += btrfs_extent_inline_ref_size(type);
1129         }
1130         if (err == -ENOENT && insert) {
1131                 if (item_size + extra_size >=
1132                     BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1133                         err = -EAGAIN;
1134                         goto out;
1135                 }
1136                 /*
1137                  * To add new inline back ref, we have to make sure
1138                  * there is no corresponding back ref item.
1139                  * For simplicity, we just do not add new inline back
1140                  * ref if there is any back ref item.
1141                  */
1142                 if (owner >= BTRFS_FIRST_FREE_OBJECTID &&
1143                     find_next_key(path, &key) == 0 && key.objectid == bytenr) {
1144                         err = -EAGAIN;
1145                         goto out;
1146                 }
1147         }
1148         *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1149 out:
1150         return err;
1151 }
1152
1153 static int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1154                                 struct btrfs_root *root,
1155                                 struct btrfs_path *path,
1156                                 struct btrfs_extent_inline_ref *iref,
1157                                 u64 parent, u64 root_objectid,
1158                                 u64 owner, u64 offset, int refs_to_add)
1159 {
1160         struct extent_buffer *leaf;
1161         struct btrfs_extent_item *ei;
1162         unsigned long ptr;
1163         unsigned long end;
1164         unsigned long item_offset;
1165         u64 refs;
1166         int size;
1167         int type;
1168         int ret;
1169
1170         leaf = path->nodes[0];
1171         ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1172         item_offset = (unsigned long)iref - (unsigned long)ei;
1173
1174         type = extent_ref_type(parent, owner);
1175         size = btrfs_extent_inline_ref_size(type);
1176
1177         ret = btrfs_extend_item(trans, root, path, size);
1178         BUG_ON(ret);
1179
1180         ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1181         refs = btrfs_extent_refs(leaf, ei);
1182         refs += refs_to_add;
1183         btrfs_set_extent_refs(leaf, ei, refs);
1184
1185         ptr = (unsigned long)ei + item_offset;
1186         end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1187         if (ptr < end - size)
1188                 memmove_extent_buffer(leaf, ptr + size, ptr,
1189                                       end - size - ptr);
1190
1191         iref = (struct btrfs_extent_inline_ref *)ptr;
1192         btrfs_set_extent_inline_ref_type(leaf, iref, type);
1193         if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1194                 struct btrfs_extent_data_ref *dref;
1195                 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1196                 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1197                 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1198                 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1199                 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1200         } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1201                 struct btrfs_shared_data_ref *sref;
1202                 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1203                 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1204                 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1205         } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1206                 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1207         } else {
1208                 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1209         }
1210         btrfs_mark_buffer_dirty(leaf);
1211         return 0;
1212 }
1213
1214 static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1215                                  struct btrfs_root *root,
1216                                  struct btrfs_path *path,
1217                                  struct btrfs_extent_inline_ref **ref_ret,
1218                                  u64 bytenr, u64 num_bytes, u64 parent,
1219                                  u64 root_objectid, u64 owner, u64 offset)
1220 {
1221         int ret;
1222
1223         ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1224                                            bytenr, num_bytes, parent,
1225                                            root_objectid, owner, offset, 0);
1226         if (ret != -ENOENT)
1227                 return ret;
1228
1229         btrfs_release_path(root, path);
1230         *ref_ret = NULL;
1231
1232         if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1233                 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1234                                             root_objectid);
1235         } else {
1236                 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1237                                              root_objectid, owner, offset);
1238         }
1239         return ret;
1240 }
1241
1242 static int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1243                                  struct btrfs_root *root,
1244                                  struct btrfs_path *path,
1245                                  struct btrfs_extent_inline_ref *iref,
1246                                  int refs_to_mod)
1247 {
1248         struct extent_buffer *leaf;
1249         struct btrfs_extent_item *ei;
1250         struct btrfs_extent_data_ref *dref = NULL;
1251         struct btrfs_shared_data_ref *sref = NULL;
1252         unsigned long ptr;
1253         unsigned long end;
1254         u32 item_size;
1255         int size;
1256         int type;
1257         int ret;
1258         u64 refs;
1259
1260         leaf = path->nodes[0];
1261         ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1262         refs = btrfs_extent_refs(leaf, ei);
1263         WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1264         refs += refs_to_mod;
1265         btrfs_set_extent_refs(leaf, ei, refs);
1266
1267         type = btrfs_extent_inline_ref_type(leaf, iref);
1268
1269         if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1270                 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1271                 refs = btrfs_extent_data_ref_count(leaf, dref);
1272         } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1273                 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1274                 refs = btrfs_shared_data_ref_count(leaf, sref);
1275         } else {
1276                 refs = 1;
1277                 BUG_ON(refs_to_mod != -1);
1278         }
1279
1280         BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1281         refs += refs_to_mod;
1282
1283         if (refs > 0) {
1284                 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1285                         btrfs_set_extent_data_ref_count(leaf, dref, refs);
1286                 else
1287                         btrfs_set_shared_data_ref_count(leaf, sref, refs);
1288         } else {
1289                 size =  btrfs_extent_inline_ref_size(type);
1290                 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1291                 ptr = (unsigned long)iref;
1292                 end = (unsigned long)ei + item_size;
1293                 if (ptr + size < end)
1294                         memmove_extent_buffer(leaf, ptr, ptr + size,
1295                                               end - ptr - size);
1296                 item_size -= size;
1297                 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1298                 BUG_ON(ret);
1299         }
1300         btrfs_mark_buffer_dirty(leaf);
1301         return 0;
1302 }
1303
1304 static int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1305                                  struct btrfs_root *root,
1306                                  struct btrfs_path *path,
1307                                  u64 bytenr, u64 num_bytes, u64 parent,
1308                                  u64 root_objectid, u64 owner,
1309                                  u64 offset, int refs_to_add)
1310 {
1311         struct btrfs_extent_inline_ref *iref;
1312         int ret;
1313
1314         ret = lookup_inline_extent_backref(trans, root, path, &iref,
1315                                            bytenr, num_bytes, parent,
1316                                            root_objectid, owner, offset, 1);
1317         if (ret == 0) {
1318                 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1319                 ret = update_inline_extent_backref(trans, root, path, iref,
1320                                                    refs_to_add);
1321         } else if (ret == -ENOENT) {
1322                 ret = setup_inline_extent_backref(trans, root, path, iref,
1323                                                   parent, root_objectid,
1324                                                   owner, offset, refs_to_add);
1325         }
1326         return ret;
1327 }
1328
1329 static int insert_extent_backref(struct btrfs_trans_handle *trans,
1330                                  struct btrfs_root *root,
1331                                  struct btrfs_path *path,
1332                                  u64 bytenr, u64 parent, u64 root_objectid,
1333                                  u64 owner, u64 offset, int refs_to_add)
1334 {
1335         int ret;
1336
1337         if (owner >= BTRFS_FIRST_FREE_OBJECTID) {
1338                 ret = insert_extent_data_ref(trans, root, path, bytenr,
1339                                              parent, root_objectid,
1340                                              owner, offset, refs_to_add);
1341         } else {
1342                 BUG_ON(refs_to_add != 1);
1343                 ret = insert_tree_block_ref(trans, root, path, bytenr,
1344                                             parent, root_objectid);
1345         }
1346         return ret;
1347 }
1348
1349 static int remove_extent_backref(struct btrfs_trans_handle *trans,
1350                                  struct btrfs_root *root,
1351                                  struct btrfs_path *path,
1352                                  struct btrfs_extent_inline_ref *iref,
1353                                  int refs_to_drop, int is_data)
1354 {
1355         int ret;
1356
1357         BUG_ON(!is_data && refs_to_drop != 1);
1358         if (iref) {
1359                 ret = update_inline_extent_backref(trans, root, path, iref,
1360                                                    -refs_to_drop);
1361         } else if (is_data) {
1362                 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1363         } else {
1364                 ret = btrfs_del_item(trans, root, path);
1365         }
1366         return ret;
1367 }
1368
1369 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1370                          struct btrfs_root *root,
1371                          u64 bytenr, u64 num_bytes, u64 parent,
1372                          u64 root_objectid, u64 owner, u64 offset)
1373 {
1374         struct btrfs_path *path;
1375         struct extent_buffer *leaf;
1376         struct btrfs_extent_item *item;
1377         u64 refs;
1378         int ret;
1379         int err = 0;
1380
1381         path = btrfs_alloc_path();
1382         if (!path)
1383                 return -ENOMEM;
1384
1385         path->reada = 1;
1386         path->leave_spinning = 1;
1387
1388         ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1389                                            path, bytenr, num_bytes, parent,
1390                                            root_objectid, owner, offset, 1);
1391         if (ret == 0)
1392                 goto out;
1393
1394         if (ret != -EAGAIN) {
1395                 err = ret;
1396                 goto out;
1397         }
1398         
1399         leaf = path->nodes[0];
1400         item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1401         refs = btrfs_extent_refs(leaf, item);
1402         btrfs_set_extent_refs(leaf, item, refs + 1);
1403
1404         btrfs_mark_buffer_dirty(leaf);
1405         btrfs_release_path(root->fs_info->extent_root, path);
1406
1407         path->reada = 1;
1408         path->leave_spinning = 1;
1409
1410         /* now insert the actual backref */
1411         ret = insert_extent_backref(trans, root->fs_info->extent_root,
1412                                     path, bytenr, parent, root_objectid,
1413                                     owner, offset, 1);
1414         if (ret)
1415                 err = ret;
1416 out:
1417         btrfs_free_path(path);
1418         finish_current_insert(trans, root->fs_info->extent_root);
1419         del_pending_extents(trans, root->fs_info->extent_root);
1420         BUG_ON(err);
1421         return err;
1422 }
1423
1424 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
1425                          struct btrfs_root *root)
1426 {
1427         finish_current_insert(trans, root->fs_info->extent_root);
1428         del_pending_extents(trans, root->fs_info->extent_root);
1429         return 0;
1430 }
1431
1432 int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
1433                              struct btrfs_root *root, u64 bytenr,
1434                              u64 num_bytes, u64 *refs, u64 *flags)
1435 {
1436         struct btrfs_path *path;
1437         int ret;
1438         struct btrfs_key key;
1439         struct extent_buffer *l;
1440         struct btrfs_extent_item *item;
1441         u32 item_size;
1442         u64 num_refs;
1443         u64 extent_flags;
1444
1445         WARN_ON(num_bytes < root->sectorsize);
1446         path = btrfs_alloc_path();
1447         path->reada = 1;
1448         key.objectid = bytenr;
1449         key.offset = num_bytes;
1450         btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1451         ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1452                                 0, 0);
1453         if (ret < 0)
1454                 goto out;
1455         if (ret != 0) {
1456                 btrfs_print_leaf(root, path->nodes[0]);
1457                 printk("failed to find block number %Lu\n", bytenr);
1458                 BUG();
1459         }
1460
1461         l = path->nodes[0];
1462         item_size = btrfs_item_size_nr(l, path->slots[0]);
1463         if (item_size >= sizeof(*item)) {
1464                 item = btrfs_item_ptr(l, path->slots[0],
1465                                       struct btrfs_extent_item);
1466                 num_refs = btrfs_extent_refs(l, item);
1467                 extent_flags = btrfs_extent_flags(l, item);
1468         } else {
1469 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1470                         struct btrfs_extent_item_v0 *ei0;
1471                         BUG_ON(item_size != sizeof(*ei0));
1472                         ei0 = btrfs_item_ptr(l, path->slots[0],
1473                                              struct btrfs_extent_item_v0);
1474                         num_refs = btrfs_extent_refs_v0(l, ei0);
1475                         /* FIXME: this isn't correct for data */
1476                         extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
1477 #else
1478                         BUG();
1479 #endif          
1480                 }
1481                 BUG_ON(num_refs == 0);
1482         item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1483         if (refs)
1484                 *refs = num_refs;
1485         if (flags)
1486                 *flags = extent_flags;
1487 out:
1488         btrfs_free_path(path);
1489         return 0;
1490 }
1491
1492 int btrfs_set_block_flags(struct btrfs_trans_handle *trans,
1493                           struct btrfs_root *root,
1494                           u64 bytenr, u64 num_bytes, u64 flags)
1495 {
1496         struct btrfs_path *path;
1497         int ret;
1498         struct btrfs_key key;
1499         struct extent_buffer *l;
1500         struct btrfs_extent_item *item;
1501         u32 item_size;
1502
1503         WARN_ON(num_bytes < root->sectorsize);
1504         path = btrfs_alloc_path();
1505         path->reada = 1;
1506         key.objectid = bytenr;
1507         key.offset = num_bytes;
1508         btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1509         ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1510                                 0, 0);
1511         if (ret < 0)
1512                 goto out;
1513         if (ret != 0) {
1514                 btrfs_print_leaf(root, path->nodes[0]);
1515                 printk("failed to find block number %Lu\n",
1516                         (unsigned long long)bytenr);
1517                 BUG();
1518         }
1519         l = path->nodes[0];
1520         item_size = btrfs_item_size_nr(l, path->slots[0]);
1521 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1522         if (item_size < sizeof(*item)) {
1523                 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1524                                              path, (u64)-1, 0);
1525                 if (ret < 0)
1526                         goto out;
1527
1528                 l = path->nodes[0];
1529                 item_size = btrfs_item_size_nr(l, path->slots[0]);
1530         }
1531 #endif
1532         BUG_ON(item_size < sizeof(*item));
1533         item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1534         flags |= btrfs_extent_flags(l, item);
1535         btrfs_set_extent_flags(l, item, flags);
1536 out:
1537         btrfs_free_path(path);
1538         finish_current_insert(trans, root->fs_info->extent_root);
1539         del_pending_extents(trans, root->fs_info->extent_root);
1540         return ret;
1541 }
1542
1543 static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
1544                            struct btrfs_root *root,
1545                            struct extent_buffer *buf,
1546                            int record_parent, int inc)
1547 {
1548         u64 bytenr;
1549         u64 num_bytes;
1550         u64 parent;
1551         u64 ref_root;
1552         u32 nritems;
1553         struct btrfs_key key;
1554         struct btrfs_file_extent_item *fi;
1555         int i;
1556         int level;
1557         int ret = 0;
1558         int faili = 0;
1559         int (*process_func)(struct btrfs_trans_handle *trans,
1560                             struct btrfs_root *root,
1561                             u64, u64, u64, u64, u64, u64);
1562
1563         ref_root = btrfs_header_owner(buf);
1564         nritems = btrfs_header_nritems(buf);
1565         level = btrfs_header_level(buf);
1566
1567         if (!root->ref_cows && level == 0)
1568                 return 0;
1569
1570         if (inc)
1571                 process_func = btrfs_inc_extent_ref;
1572         else
1573                 process_func = btrfs_free_extent;
1574
1575         if (record_parent)
1576                 parent = buf->start;
1577         else
1578                 parent = 0;
1579
1580         for (i = 0; i < nritems; i++) {
1581                 cond_resched();
1582                 if (level == 0) {
1583                         btrfs_item_key_to_cpu(buf, &key, i);
1584                         if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1585                                 continue;
1586                         fi = btrfs_item_ptr(buf, i,
1587                                             struct btrfs_file_extent_item);
1588                         if (btrfs_file_extent_type(buf, fi) ==
1589                             BTRFS_FILE_EXTENT_INLINE)
1590                                 continue;
1591                         bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1592                         if (bytenr == 0)
1593                                 continue;
1594                         
1595                         num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
1596                         key.offset -= btrfs_file_extent_offset(buf, fi);
1597                         ret = process_func(trans, root, bytenr, num_bytes,
1598                                            parent, ref_root, key.objectid,
1599                                            key.offset);
1600                         if (ret) {
1601                                 faili = i;
1602                                 WARN_ON(1);
1603                                 goto fail;
1604                         }
1605                 } else {
1606                         bytenr = btrfs_node_blockptr(buf, i);
1607                         num_bytes = btrfs_level_size(root, level - 1);
1608                         ret = process_func(trans, root, bytenr, num_bytes,
1609                                            parent, ref_root, level - 1, 0);
1610                         if (ret) {
1611                                 faili = i;
1612                                 WARN_ON(1);
1613                                 goto fail;
1614                         }
1615                 }
1616         }
1617         return 0;
1618 fail:
1619         WARN_ON(1);
1620 #if 0
1621         for (i =0; i < faili; i++) {
1622                 if (level == 0) {
1623                         u64 disk_bytenr;
1624                         btrfs_item_key_to_cpu(buf, &key, i);
1625                         if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1626                                 continue;
1627                         fi = btrfs_item_ptr(buf, i,
1628                                             struct btrfs_file_extent_item);
1629                         if (btrfs_file_extent_type(buf, fi) ==
1630                             BTRFS_FILE_EXTENT_INLINE)
1631                                 continue;
1632                         disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1633                         if (disk_bytenr == 0)
1634                                 continue;
1635                         err = btrfs_free_extent(trans, root, disk_bytenr,
1636                                     btrfs_file_extent_disk_num_bytes(buf,
1637                                                                       fi), 0);
1638                         BUG_ON(err);
1639                 } else {
1640                         bytenr = btrfs_node_blockptr(buf, i);
1641                         err = btrfs_free_extent(trans, root, bytenr,
1642                                         btrfs_level_size(root, level - 1), 0);
1643                         BUG_ON(err);
1644                 }
1645         }
1646 #endif
1647         return ret;
1648 }
1649
1650 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1651                   struct extent_buffer *buf, int record_parent)
1652 {
1653         return __btrfs_mod_ref(trans, root, buf, record_parent, 1);
1654 }
1655
1656 int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1657                   struct extent_buffer *buf, int record_parent)
1658 {
1659         return __btrfs_mod_ref(trans, root, buf, record_parent, 0);
1660 }
1661
1662 static int write_one_cache_group(struct btrfs_trans_handle *trans,
1663                                  struct btrfs_root *root,
1664                                  struct btrfs_path *path,
1665                                  struct btrfs_block_group_cache *cache)
1666 {
1667         int ret;
1668         int pending_ret;
1669         struct btrfs_root *extent_root = root->fs_info->extent_root;
1670         unsigned long bi;
1671         struct extent_buffer *leaf;
1672
1673         ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
1674         if (ret < 0)
1675                 goto fail;
1676         BUG_ON(ret);
1677
1678         leaf = path->nodes[0];
1679         bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1680         write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1681         btrfs_mark_buffer_dirty(leaf);
1682         btrfs_release_path(extent_root, path);
1683 fail:
1684         finish_current_insert(trans, extent_root);
1685         pending_ret = del_pending_extents(trans, extent_root);
1686         if (ret)
1687                 return ret;
1688         if (pending_ret)
1689                 return pending_ret;
1690         return 0;
1691
1692 }
1693
1694 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1695                                    struct btrfs_root *root)
1696 {
1697         struct extent_io_tree *block_group_cache;
1698         struct btrfs_block_group_cache *cache;
1699         int ret;
1700         int err = 0;
1701         int werr = 0;
1702         struct btrfs_path *path;
1703         u64 last = 0;
1704         u64 start;
1705         u64 end;
1706         u64 ptr;
1707
1708         block_group_cache = &root->fs_info->block_group_cache;
1709         path = btrfs_alloc_path();
1710         if (!path)
1711                 return -ENOMEM;
1712
1713         while(1) {
1714                 ret = find_first_extent_bit(block_group_cache, last,
1715                                             &start, &end, BLOCK_GROUP_DIRTY);
1716                 if (ret)
1717                         break;
1718
1719                 last = end + 1;
1720                 ret = get_state_private(block_group_cache, start, &ptr);
1721                 if (ret)
1722                         break;
1723                 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
1724                 err = write_one_cache_group(trans, root,
1725                                             path, cache);
1726                 /*
1727                  * if we fail to write the cache group, we want
1728                  * to keep it marked dirty in hopes that a later
1729                  * write will work
1730                  */
1731                 if (err) {
1732                         werr = err;
1733                         continue;
1734                 }
1735                 clear_extent_bits(block_group_cache, start, end,
1736                                   BLOCK_GROUP_DIRTY, GFP_NOFS);
1737         }
1738         btrfs_free_path(path);
1739         return werr;
1740 }
1741
1742 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
1743                                                   u64 flags)
1744 {
1745         struct list_head *head = &info->space_info;
1746         struct list_head *cur;
1747         struct btrfs_space_info *found;
1748         list_for_each(cur, head) {
1749                 found = list_entry(cur, struct btrfs_space_info, list);
1750                 if (found->flags == flags)
1751                         return found;
1752         }
1753         return NULL;
1754
1755 }
1756
1757 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1758                              u64 total_bytes, u64 bytes_used,
1759                              struct btrfs_space_info **space_info)
1760 {
1761         struct btrfs_space_info *found;
1762
1763         found = __find_space_info(info, flags);
1764         if (found) {
1765                 found->total_bytes += total_bytes;
1766                 found->bytes_used += bytes_used;
1767                 WARN_ON(found->total_bytes < found->bytes_used);
1768                 *space_info = found;
1769                 return 0;
1770         }
1771         found = kmalloc(sizeof(*found), GFP_NOFS);
1772         if (!found)
1773                 return -ENOMEM;
1774
1775         list_add(&found->list, &info->space_info);
1776         found->flags = flags;
1777         found->total_bytes = total_bytes;
1778         found->bytes_used = bytes_used;
1779         found->bytes_pinned = 0;
1780         found->full = 0;
1781         *space_info = found;
1782         return 0;
1783 }
1784
1785
1786 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1787 {
1788         u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
1789                                    BTRFS_BLOCK_GROUP_RAID1 |
1790                                    BTRFS_BLOCK_GROUP_DUP);
1791         if (extra_flags) {
1792                 if (flags & BTRFS_BLOCK_GROUP_DATA)
1793                         fs_info->avail_data_alloc_bits |= extra_flags;
1794                 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1795                         fs_info->avail_metadata_alloc_bits |= extra_flags;
1796                 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1797                         fs_info->avail_system_alloc_bits |= extra_flags;
1798         }
1799 }
1800
1801 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1802                           struct btrfs_root *extent_root, u64 alloc_bytes,
1803                           u64 flags)
1804 {
1805         struct btrfs_space_info *space_info;
1806         u64 thresh;
1807         u64 start;
1808         u64 num_bytes;
1809         int ret;
1810
1811         space_info = __find_space_info(extent_root->fs_info, flags);
1812         if (!space_info) {
1813                 ret = update_space_info(extent_root->fs_info, flags,
1814                                         0, 0, &space_info);
1815                 BUG_ON(ret);
1816         }
1817         BUG_ON(!space_info);
1818
1819         if (space_info->full)
1820                 return 0;
1821
1822         thresh = div_factor(space_info->total_bytes, 7);
1823         if ((space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) <
1824             thresh)
1825                 return 0;
1826
1827         ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
1828         if (ret == -ENOSPC) {
1829                 space_info->full = 1;
1830                 return 0;
1831         }
1832
1833         BUG_ON(ret);
1834
1835         ret = btrfs_make_block_group(trans, extent_root, 0, flags,
1836                      BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
1837         BUG_ON(ret);
1838         return 0;
1839 }
1840
1841 static int update_block_group(struct btrfs_trans_handle *trans,
1842                               struct btrfs_root *root,
1843                               u64 bytenr, u64 num_bytes, int alloc,
1844                               int mark_free)
1845 {
1846         struct btrfs_block_group_cache *cache;
1847         struct btrfs_fs_info *info = root->fs_info;
1848         u64 total = num_bytes;
1849         u64 old_val;
1850         u64 byte_in_group;
1851         u64 start;
1852         u64 end;
1853
1854         /* block accounting for super block */
1855         old_val = btrfs_super_bytes_used(&info->super_copy);
1856         if (alloc)
1857                 old_val += num_bytes;
1858         else
1859                 old_val -= num_bytes;
1860         btrfs_set_super_bytes_used(&info->super_copy, old_val);
1861
1862         /* block accounting for root item */
1863         old_val = btrfs_root_used(&root->root_item);
1864         if (alloc)
1865                 old_val += num_bytes;
1866         else
1867                 old_val -= num_bytes;
1868         btrfs_set_root_used(&root->root_item, old_val);
1869
1870         while(total) {
1871                 cache = btrfs_lookup_block_group(info, bytenr);
1872                 if (!cache) {
1873                         return -1;
1874                 }
1875                 byte_in_group = bytenr - cache->key.objectid;
1876                 WARN_ON(byte_in_group > cache->key.offset);
1877                 start = cache->key.objectid;
1878                 end = start + cache->key.offset - 1;
1879                 set_extent_bits(&info->block_group_cache, start, end,
1880                                 BLOCK_GROUP_DIRTY, GFP_NOFS);
1881
1882                 old_val = btrfs_block_group_used(&cache->item);
1883                 num_bytes = min(total, cache->key.offset - byte_in_group);
1884                 if (alloc) {
1885                         old_val += num_bytes;
1886                         cache->space_info->bytes_used += num_bytes;
1887                 } else {
1888                         old_val -= num_bytes;
1889                         cache->space_info->bytes_used -= num_bytes;
1890                         if (mark_free) {
1891                                 set_extent_dirty(&info->free_space_cache,
1892                                                  bytenr, bytenr + num_bytes - 1,
1893                                                  GFP_NOFS);
1894                         }
1895                 }
1896                 btrfs_set_block_group_used(&cache->item, old_val);
1897                 total -= num_bytes;
1898                 bytenr += num_bytes;
1899         }
1900         return 0;
1901 }
1902
1903 static int update_pinned_extents(struct btrfs_root *root,
1904                                 u64 bytenr, u64 num, int pin)
1905 {
1906         u64 len;
1907         struct btrfs_block_group_cache *cache;
1908         struct btrfs_fs_info *fs_info = root->fs_info;
1909
1910         if (pin) {
1911                 set_extent_dirty(&fs_info->pinned_extents,
1912                                 bytenr, bytenr + num - 1, GFP_NOFS);
1913         } else {
1914                 clear_extent_dirty(&fs_info->pinned_extents,
1915                                 bytenr, bytenr + num - 1, GFP_NOFS);
1916         }
1917         while (num > 0) {
1918                 cache = btrfs_lookup_block_group(fs_info, bytenr);
1919                 WARN_ON(!cache);
1920                 len = min(num, cache->key.offset -
1921                           (bytenr - cache->key.objectid));
1922                 if (pin) {
1923                         cache->pinned += len;
1924                         cache->space_info->bytes_pinned += len;
1925                         fs_info->total_pinned += len;
1926                 } else {
1927                         cache->pinned -= len;
1928                         cache->space_info->bytes_pinned -= len;
1929                         fs_info->total_pinned -= len;
1930                 }
1931                 bytenr += len;
1932                 num -= len;
1933         }
1934         return 0;
1935 }
1936
1937 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
1938 {
1939         u64 last = 0;
1940         u64 start;
1941         u64 end;
1942         struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
1943         int ret;
1944
1945         while(1) {
1946                 ret = find_first_extent_bit(pinned_extents, last,
1947                                             &start, &end, EXTENT_DIRTY);
1948                 if (ret)
1949                         break;
1950                 set_extent_dirty(copy, start, end, GFP_NOFS);
1951                 last = end + 1;
1952         }
1953         return 0;
1954 }
1955
1956 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1957                                struct btrfs_root *root,
1958                                struct extent_io_tree *unpin)
1959 {
1960         u64 start;
1961         u64 end;
1962         int ret;
1963         struct extent_io_tree *free_space_cache;
1964         free_space_cache = &root->fs_info->free_space_cache;
1965
1966         while(1) {
1967                 ret = find_first_extent_bit(unpin, 0, &start, &end,
1968                                             EXTENT_DIRTY);
1969                 if (ret)
1970                         break;
1971                 update_pinned_extents(root, start, end + 1 - start, 0);
1972                 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1973                 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
1974         }
1975         return 0;
1976 }
1977
1978 static int finish_current_insert(struct btrfs_trans_handle *trans,
1979                                  struct btrfs_root *extent_root)
1980 {
1981         u64 start;
1982         u64 end;
1983         u64 priv;
1984         struct btrfs_fs_info *info = extent_root->fs_info;
1985         struct btrfs_path *path;
1986         struct pending_extent_op *extent_op;
1987         struct btrfs_key key;
1988         int ret;
1989
1990         path = btrfs_alloc_path();
1991
1992         while(1) {
1993                 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1994                                             &end, EXTENT_LOCKED);
1995                 if (ret)
1996                         break;
1997
1998                 ret = get_state_private(&info->extent_ins, start, &priv);
1999                 BUG_ON(ret);
2000                 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2001
2002                 if (extent_op->type == PENDING_EXTENT_INSERT) {
2003                         key.objectid = start;
2004                         key.offset = end + 1 - start;
2005                         key.type = BTRFS_EXTENT_ITEM_KEY;
2006                         ret = alloc_reserved_tree_block(trans, extent_root,
2007                                                 extent_root->root_key.objectid,
2008                                                 trans->transid,
2009                                                 extent_op->flags,
2010                                                 &extent_op->key,
2011                                                 extent_op->level, &key);
2012                 } else {
2013                         BUG_ON(1);
2014                 }
2015
2016                 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
2017                                   GFP_NOFS);
2018                 kfree(extent_op);
2019         }
2020         btrfs_free_path(path);
2021         return 0;
2022 }
2023
2024 static int pin_down_bytes(struct btrfs_trans_handle *trans,
2025                           struct btrfs_root *root,
2026                           u64 bytenr, u64 num_bytes, int is_data)
2027 {
2028         int err = 0;
2029         struct extent_buffer *buf;
2030
2031         if (is_data)
2032                 goto pinit;
2033
2034         buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2035         if (!buf)
2036                 goto pinit;
2037
2038         /* we can reuse a block if it hasn't been written
2039          * and it is from this transaction.  We can't
2040          * reuse anything from the tree log root because
2041          * it has tiny sub-transactions.
2042          */
2043         if (btrfs_buffer_uptodate(buf, 0)) {
2044                 u64 header_owner = btrfs_header_owner(buf);
2045                 u64 header_transid = btrfs_header_generation(buf);
2046                 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
2047                     header_transid == trans->transid &&
2048                     !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2049                         clean_tree_block(NULL, root, buf);
2050                         free_extent_buffer(buf);
2051                         return 1;
2052                 }
2053         }
2054         free_extent_buffer(buf);
2055 pinit:
2056         update_pinned_extents(root, bytenr, num_bytes, 1);
2057
2058         BUG_ON(err < 0);
2059         return 0;
2060 }
2061
2062 /*
2063  * remove an extent from the root, returns 0 on success
2064  */
2065 static int __free_extent(struct btrfs_trans_handle *trans,
2066                          struct btrfs_root *root,
2067                          u64 bytenr, u64 num_bytes, u64 parent,
2068                          u64 root_objectid, u64 owner_objectid,
2069                          u64 owner_offset, int refs_to_drop)
2070 {
2071
2072         struct btrfs_key key;
2073         struct btrfs_path *path;
2074         struct btrfs_extent_ops *ops = root->fs_info->extent_ops;
2075         struct btrfs_root *extent_root = root->fs_info->extent_root;
2076         struct extent_buffer *leaf;
2077         struct btrfs_extent_item *ei;
2078         struct btrfs_extent_inline_ref *iref;
2079         int ret;
2080         int is_data;
2081         int extent_slot = 0;
2082         int found_extent = 0;
2083         int num_to_del = 1;
2084         u32 item_size;
2085         u64 refs;
2086
2087         path = btrfs_alloc_path();
2088         if (!path)
2089                 return -ENOMEM;
2090
2091         path->reada = 1;
2092         path->leave_spinning = 1;
2093
2094         is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
2095         BUG_ON(!is_data && refs_to_drop != 1);
2096
2097         ret = lookup_extent_backref(trans, extent_root, path, &iref,
2098                                     bytenr, num_bytes, parent,
2099                                     root_objectid, owner_objectid,
2100                                     owner_offset);
2101         if (ret == 0) {
2102                 extent_slot = path->slots[0];
2103                 while (extent_slot >= 0) {
2104                         btrfs_item_key_to_cpu(path->nodes[0], &key,
2105                                               extent_slot);
2106                         if (key.objectid != bytenr)
2107                                 break;
2108                         if (key.type == BTRFS_EXTENT_ITEM_KEY &&
2109                             key.offset == num_bytes) {
2110                                 found_extent = 1;
2111                                 break;
2112                         }
2113                         if (path->slots[0] - extent_slot > 5)
2114                                 break;
2115                         extent_slot--;
2116                 }
2117 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2118                 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
2119                 if (found_extent && item_size < sizeof(*ei))
2120                         found_extent = 0;
2121 #endif
2122                 if (!found_extent) {
2123                         BUG_ON(iref);
2124                         ret = remove_extent_backref(trans, extent_root, path,
2125                                                     NULL, refs_to_drop,
2126                                                     is_data);
2127                         BUG_ON(ret);
2128                         btrfs_release_path(extent_root, path);
2129                         path->leave_spinning = 1;
2130
2131                         key.objectid = bytenr;
2132                         key.type = BTRFS_EXTENT_ITEM_KEY;
2133                         key.offset = num_bytes;
2134
2135                         ret = btrfs_search_slot(trans, extent_root,
2136                                                 &key, path, -1, 1);
2137                         if (ret) {
2138                                 printk(KERN_ERR "umm, got %d back from search"
2139                                        ", was looking for %llu\n", ret,
2140                                        (unsigned long long)bytenr);
2141                                 btrfs_print_leaf(extent_root, path->nodes[0]);
2142                         }
2143                         BUG_ON(ret);
2144                         extent_slot = path->slots[0];
2145                 }
2146         } else {
2147                 btrfs_print_leaf(extent_root, path->nodes[0]);
2148                 WARN_ON(1);
2149                 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
2150                        "parent %llu root %llu  owner %llu offset %llu\n",
2151                        (unsigned long long)bytenr,
2152                        (unsigned long long)parent,
2153                        (unsigned long long)root_objectid,
2154                        (unsigned long long)owner_objectid,
2155                        (unsigned long long)owner_offset);
2156         }
2157
2158         leaf = path->nodes[0];
2159         item_size = btrfs_item_size_nr(leaf, extent_slot);
2160 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2161         if (item_size < sizeof(*ei)) {
2162                 BUG_ON(found_extent || extent_slot != path->slots[0]);
2163                 ret = convert_extent_item_v0(trans, extent_root, path,
2164                                              owner_objectid, 0);
2165                 BUG_ON(ret < 0);
2166
2167                 btrfs_release_path(extent_root, path);
2168                 path->leave_spinning = 1;
2169
2170                 key.objectid = bytenr;
2171                 key.type = BTRFS_EXTENT_ITEM_KEY;
2172                 key.offset = num_bytes;
2173
2174                 ret = btrfs_search_slot(trans, extent_root, &key, path,
2175                                         -1, 1);
2176                 if (ret) {
2177                         printk(KERN_ERR "umm, got %d back from search"
2178                                ", was looking for %llu\n", ret,
2179                                (unsigned long long)bytenr);
2180                         btrfs_print_leaf(extent_root, path->nodes[0]);
2181                 }
2182                 BUG_ON(ret);
2183                 extent_slot = path->slots[0];
2184                 leaf = path->nodes[0];
2185                 item_size = btrfs_item_size_nr(leaf, extent_slot);
2186         }
2187 #endif
2188         BUG_ON(item_size < sizeof(*ei));
2189         ei = btrfs_item_ptr(leaf, extent_slot,
2190                             struct btrfs_extent_item);
2191         if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2192                 struct btrfs_tree_block_info *bi;
2193                 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
2194                 bi = (struct btrfs_tree_block_info *)(ei + 1);
2195                 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
2196         }
2197
2198         refs = btrfs_extent_refs(leaf, ei);
2199         BUG_ON(refs < refs_to_drop);
2200         refs -= refs_to_drop;
2201
2202         if (refs > 0) {
2203                 /*
2204                  * In the case of inline back ref, reference count will
2205                  * be updated by remove_extent_backref
2206                  */
2207                 if (iref) {
2208                         BUG_ON(!found_extent);
2209                 } else {
2210                         btrfs_set_extent_refs(leaf, ei, refs);
2211                         btrfs_mark_buffer_dirty(leaf);
2212                 }
2213                 if (found_extent) {
2214                         ret = remove_extent_backref(trans, extent_root, path,
2215                                                     iref, refs_to_drop,
2216                                                     is_data);
2217                         BUG_ON(ret);
2218                 }
2219         } else {
2220                 int mark_free = 0;
2221                 int pin = 1;
2222
2223                 if (found_extent) {
2224                         BUG_ON(is_data && refs_to_drop !=
2225                                extent_data_ref_count(root, path, iref));
2226                         if (iref) {
2227                                 BUG_ON(path->slots[0] != extent_slot);
2228                         } else {
2229                                 BUG_ON(path->slots[0] != extent_slot + 1);
2230                                 path->slots[0] = extent_slot;
2231                                 num_to_del = 2;
2232                         }
2233                 }
2234
2235                 if (ops && ops->free_extent) {
2236                         ret = ops->free_extent(root, bytenr, num_bytes);
2237                         if (ret > 0) {
2238                                 pin = 0;
2239                                 mark_free = 0;
2240                         }
2241                 }
2242
2243                 if (pin) {
2244                         ret = pin_down_bytes(trans, root, bytenr, num_bytes,
2245                                              is_data);
2246                         if (ret > 0)
2247                                 mark_free = 1;
2248                         BUG_ON(ret < 0);
2249                 }
2250
2251                 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2252                                       num_to_del);
2253                 BUG_ON(ret);
2254                 btrfs_release_path(extent_root, path);
2255
2256                 if (is_data) {
2257                         ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
2258                         BUG_ON(ret);
2259                 }
2260
2261                 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
2262                                          mark_free);
2263                 BUG_ON(ret);
2264         }
2265         btrfs_free_path(path);
2266         finish_current_insert(trans, extent_root);
2267         return ret;
2268 }
2269
2270 /*
2271  * find all the blocks marked as pending in the radix tree and remove
2272  * them from the extent map
2273  */
2274 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
2275                                btrfs_root *extent_root)
2276 {
2277         int ret;
2278         int err = 0;
2279         u64 start;
2280         u64 end;
2281         u64 priv;
2282         struct extent_io_tree *pending_del;
2283         struct extent_io_tree *extent_ins;
2284         struct pending_extent_op *extent_op;
2285
2286         extent_ins = &extent_root->fs_info->extent_ins;
2287         pending_del = &extent_root->fs_info->pending_del;
2288
2289         while(1) {
2290                 ret = find_first_extent_bit(pending_del, 0, &start, &end,
2291                                             EXTENT_LOCKED);
2292                 if (ret)
2293                         break;
2294
2295                 ret = get_state_private(pending_del, start, &priv);
2296                 BUG_ON(ret);
2297                 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2298
2299                 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
2300                                   GFP_NOFS);
2301
2302                 if (!test_range_bit(extent_ins, start, end,
2303                                     EXTENT_LOCKED, 0)) {
2304                         ret = __free_extent(trans, extent_root,
2305                                             start, end + 1 - start, 0,
2306                                             extent_root->root_key.objectid,
2307                                             extent_op->level, 0, 1);
2308                         kfree(extent_op);
2309                 } else {
2310                         kfree(extent_op);
2311                         ret = get_state_private(extent_ins, start, &priv);
2312                         BUG_ON(ret);
2313                         extent_op = (struct pending_extent_op *)
2314                                                         (unsigned long)priv;
2315
2316                         clear_extent_bits(extent_ins, start, end,
2317                                           EXTENT_LOCKED, GFP_NOFS);
2318
2319                         if (extent_op->type == PENDING_BACKREF_UPDATE)
2320                                 BUG_ON(1);
2321
2322                         kfree(extent_op);
2323                 }
2324                 if (ret)
2325                         err = ret;
2326         }
2327         return err;
2328 }
2329
2330 /*
2331  * remove an extent from the root, returns 0 on success
2332  */
2333
2334 int btrfs_free_extent(struct btrfs_trans_handle *trans,
2335                       struct btrfs_root *root,
2336                       u64 bytenr, u64 num_bytes, u64 parent,
2337                       u64 root_objectid, u64 owner, u64 offset)
2338 {
2339         struct btrfs_root *extent_root = root->fs_info->extent_root;
2340         int pending_ret;
2341         int ret;
2342
2343         WARN_ON(num_bytes < root->sectorsize);
2344         if (root == extent_root) {
2345                 struct pending_extent_op *extent_op;
2346
2347                 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2348                 BUG_ON(!extent_op);
2349
2350                 extent_op->type = PENDING_EXTENT_DELETE;
2351                 extent_op->bytenr = bytenr;
2352                 extent_op->num_bytes = num_bytes;
2353                 extent_op->level = (int)owner;
2354
2355                 set_extent_bits(&root->fs_info->pending_del,
2356                                 bytenr, bytenr + num_bytes - 1,
2357                                 EXTENT_LOCKED, GFP_NOFS);
2358                 set_state_private(&root->fs_info->pending_del,
2359                                   bytenr, (unsigned long)extent_op);
2360                 return 0;
2361         }
2362         ret = __free_extent(trans, root, bytenr, num_bytes, parent,
2363                             root_objectid, owner, offset, 1);
2364         pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
2365         return ret ? ret : pending_ret;
2366 }
2367
2368 static u64 stripe_align(struct btrfs_root *root, u64 val)
2369 {
2370         u64 mask = ((u64)root->stripesize - 1);
2371         u64 ret = (val + mask) & ~mask;
2372         return ret;
2373 }
2374
2375 /*
2376  * walks the btree of allocated extents and find a hole of a given size.
2377  * The key ins is changed to record the hole:
2378  * ins->objectid == block start
2379  * ins->flags = BTRFS_EXTENT_ITEM_KEY
2380  * ins->offset == number of blocks
2381  * Any available blocks before search_start are skipped.
2382  */
2383 static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2384                                      struct btrfs_root *orig_root,
2385                                      u64 num_bytes, u64 empty_size,
2386                                      u64 search_start, u64 search_end,
2387                                      u64 hint_byte, struct btrfs_key *ins,
2388                                      u64 exclude_start, u64 exclude_nr,
2389                                      int data)
2390 {
2391         int ret;
2392         u64 orig_search_start = search_start;
2393         struct btrfs_root * root = orig_root->fs_info->extent_root;
2394         struct btrfs_fs_info *info = root->fs_info;
2395         u64 total_needed = num_bytes;
2396         struct btrfs_block_group_cache *block_group;
2397         int full_scan = 0;
2398         int wrapped = 0;
2399
2400         WARN_ON(num_bytes < root->sectorsize);
2401         btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
2402
2403         if (hint_byte) {
2404                 block_group = btrfs_lookup_first_block_group(info, hint_byte);
2405                 if (!block_group)
2406                         hint_byte = search_start;
2407                 block_group = btrfs_find_block_group(root, block_group,
2408                                                      hint_byte, data, 1);
2409         } else {
2410                 block_group = btrfs_find_block_group(root,
2411                                                      trans->block_group,
2412                                                      search_start, data, 1);
2413         }
2414
2415         total_needed += empty_size;
2416
2417 check_failed:
2418         if (!block_group) {
2419                 block_group = btrfs_lookup_first_block_group(info,
2420                                                              search_start);
2421                 if (!block_group)
2422                         block_group = btrfs_lookup_first_block_group(info,
2423                                                        orig_search_start);
2424         }
2425         ret = find_search_start(root, &block_group, &search_start,
2426                                 total_needed, data);
2427         if (ret)
2428                 goto error;
2429
2430         search_start = stripe_align(root, search_start);
2431         ins->objectid = search_start;
2432         ins->offset = num_bytes;
2433
2434         if (ins->objectid + num_bytes >
2435             block_group->key.objectid + block_group->key.offset) {
2436                 search_start = block_group->key.objectid +
2437                         block_group->key.offset;
2438                 goto new_group;
2439         }
2440
2441         if (test_range_bit(&info->extent_ins, ins->objectid,
2442                            ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
2443                 search_start = ins->objectid + num_bytes;
2444                 goto new_group;
2445         }
2446
2447         if (test_range_bit(&info->pinned_extents, ins->objectid,
2448                            ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
2449                 search_start = ins->objectid + num_bytes;
2450                 goto new_group;
2451         }
2452
2453         if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
2454             ins->objectid < exclude_start + exclude_nr)) {
2455                 search_start = exclude_start + exclude_nr;
2456                 goto new_group;
2457         }
2458
2459         if (!(data & BTRFS_BLOCK_GROUP_DATA)) {
2460                 block_group = btrfs_lookup_block_group(info, ins->objectid);
2461                 if (block_group)
2462                         trans->block_group = block_group;
2463         }
2464         ins->offset = num_bytes;
2465         return 0;
2466
2467 new_group:
2468         block_group = btrfs_lookup_first_block_group(info, search_start);
2469         if (!block_group) {
2470                 search_start = orig_search_start;
2471                 if (full_scan) {
2472                         ret = -ENOSPC;
2473                         goto error;
2474                 }
2475                 if (wrapped) {
2476                         if (!full_scan)
2477                                 total_needed -= empty_size;
2478                         full_scan = 1;
2479                 } else
2480                         wrapped = 1;
2481         }
2482         cond_resched();
2483         block_group = btrfs_find_block_group(root, block_group,
2484                                              search_start, data, 0);
2485         goto check_failed;
2486
2487 error:
2488         return ret;
2489 }
2490
2491 static int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2492                                 struct btrfs_root *root,
2493                                 u64 num_bytes, u64 empty_size,
2494                                 u64 hint_byte, u64 search_end,
2495                                 struct btrfs_key *ins, int data)
2496 {
2497         int ret;
2498         u64 search_start = 0;
2499         u64 alloc_profile;
2500         struct btrfs_fs_info *info = root->fs_info;
2501
2502         if (info->extent_ops) {
2503                 struct btrfs_extent_ops *ops = info->extent_ops;
2504                 ret = ops->alloc_extent(root, num_bytes, hint_byte, ins);
2505                 BUG_ON(ret);
2506                 goto found;
2507         }
2508
2509         if (data) {
2510                 alloc_profile = info->avail_data_alloc_bits &
2511                                 info->data_alloc_profile;
2512                 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
2513         } else if ((info->system_allocs > 0 || root == info->chunk_root) &&
2514                    info->system_allocs >= 0) {
2515                 alloc_profile = info->avail_system_alloc_bits &
2516                                 info->system_alloc_profile;
2517                 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
2518         } else {
2519                 alloc_profile = info->avail_metadata_alloc_bits &
2520                                 info->metadata_alloc_profile;
2521                 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
2522         }
2523
2524         if (root->ref_cows) {
2525                 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
2526                         ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2527                                              num_bytes,
2528                                              BTRFS_BLOCK_GROUP_METADATA);
2529                         BUG_ON(ret);
2530                 }
2531                 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2532                                      num_bytes + 2 * 1024 * 1024, data);
2533                 BUG_ON(ret);
2534         }
2535
2536         WARN_ON(num_bytes < root->sectorsize);
2537         ret = find_free_extent(trans, root, num_bytes, empty_size,
2538                                search_start, search_end, hint_byte, ins,
2539                                trans->alloc_exclude_start,
2540                                trans->alloc_exclude_nr, data);
2541         BUG_ON(ret);
2542 found:
2543         clear_extent_dirty(&root->fs_info->free_space_cache,
2544                            ins->objectid, ins->objectid + ins->offset - 1,
2545                            GFP_NOFS);
2546         return ret;
2547 }
2548
2549 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
2550                                      struct btrfs_root *root,
2551                                      u64 root_objectid, u64 generation,
2552                                      u64 flags, struct btrfs_disk_key *key,
2553                                      int level, struct btrfs_key *ins)
2554 {
2555         int ret;
2556         struct btrfs_fs_info *fs_info = root->fs_info;
2557         struct btrfs_extent_item *extent_item;
2558         struct btrfs_tree_block_info *block_info;
2559         struct btrfs_extent_inline_ref *iref;
2560         struct btrfs_path *path;
2561         struct extent_buffer *leaf;
2562         u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
2563
2564         path = btrfs_alloc_path();
2565         BUG_ON(!path);
2566
2567         path->leave_spinning = 1;
2568         ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
2569                                       ins, size);
2570         BUG_ON(ret);
2571
2572         leaf = path->nodes[0];
2573         extent_item = btrfs_item_ptr(leaf, path->slots[0],
2574                                      struct btrfs_extent_item);
2575         btrfs_set_extent_refs(leaf, extent_item, 1);
2576         btrfs_set_extent_generation(leaf, extent_item, generation);
2577         btrfs_set_extent_flags(leaf, extent_item,
2578                                flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
2579         block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
2580
2581         btrfs_set_tree_block_key(leaf, block_info, key);
2582         btrfs_set_tree_block_level(leaf, block_info, level);
2583
2584         iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
2585         btrfs_set_extent_inline_ref_type(leaf, iref, BTRFS_TREE_BLOCK_REF_KEY);
2586         btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
2587
2588         btrfs_mark_buffer_dirty(leaf);
2589         btrfs_free_path(path);
2590
2591         ret = update_block_group(trans, root, ins->objectid, ins->offset,
2592                                  1, 0);
2593         if (ret) {
2594                 printk(KERN_ERR "btrfs update block group failed for %llu "
2595                        "%llu\n", (unsigned long long)ins->objectid,
2596                        (unsigned long long)ins->offset);
2597                 BUG();
2598         }
2599         return ret;
2600 }
2601
2602 static int alloc_tree_block(struct btrfs_trans_handle *trans,
2603                             struct btrfs_root *root, u64 num_bytes,
2604                             u64 root_objectid, u64 generation,
2605                             u64 flags, struct btrfs_disk_key *key,
2606                             int level, u64 empty_size, u64 hint_byte,
2607                             u64 search_end, struct btrfs_key *ins)
2608 {
2609         int ret;
2610         ret = btrfs_reserve_extent(trans, root, num_bytes, empty_size,
2611                                    hint_byte, search_end, ins, 0);
2612         BUG_ON(ret);
2613
2614         if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID) {
2615                 struct pending_extent_op *extent_op;
2616
2617                 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2618                 BUG_ON(!extent_op);
2619
2620                 extent_op->type = PENDING_EXTENT_INSERT;
2621                 extent_op->bytenr = ins->objectid;
2622                 extent_op->num_bytes = ins->offset;
2623                 extent_op->level = level;
2624                 extent_op->flags = flags;
2625                 memcpy(&extent_op->key, key, sizeof(*key));
2626
2627                 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
2628                                 ins->objectid + ins->offset - 1,
2629                                 EXTENT_LOCKED, GFP_NOFS);
2630                 set_state_private(&root->fs_info->extent_ins,
2631                                   ins->objectid, (unsigned long)extent_op);
2632         } else {
2633                 ret = alloc_reserved_tree_block(trans, root, root_objectid,
2634                                                 generation, flags,
2635                                                 key, level, ins);
2636                 finish_current_insert(trans, root->fs_info->extent_root);
2637                 del_pending_extents(trans, root->fs_info->extent_root);
2638         }
2639         return ret;
2640 }
2641
2642 /*
2643  * helper function to allocate a block for a given tree
2644  * returns the tree buffer or NULL.
2645  */
2646 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
2647                                         struct btrfs_root *root,
2648                                         u32 blocksize, u64 root_objectid,
2649                                         struct btrfs_disk_key *key, int level,
2650                                         u64 hint, u64 empty_size)
2651 {
2652         struct btrfs_key ins;
2653         int ret;
2654         struct extent_buffer *buf;
2655
2656         ret = alloc_tree_block(trans, root, blocksize, root_objectid,
2657                                trans->transid, 0, key, level,
2658                                empty_size, hint, (u64)-1, &ins);
2659         if (ret) {
2660                 BUG_ON(ret > 0);
2661                 return ERR_PTR(ret);
2662         }
2663
2664         buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
2665         if (!buf) {
2666                 btrfs_free_extent(trans, root, ins.objectid, ins.offset,
2667                                   0, root->root_key.objectid, level, 0);
2668                 BUG_ON(1);
2669                 return ERR_PTR(-ENOMEM);
2670         }
2671         btrfs_set_buffer_uptodate(buf);
2672         trans->blocks_used++;
2673
2674         return buf;
2675 }
2676
2677 #if 0
2678
2679 static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
2680                                   struct btrfs_root *root,
2681                                   struct extent_buffer *leaf)
2682 {
2683         u64 leaf_owner;
2684         u64 leaf_generation;
2685         struct btrfs_key key;
2686         struct btrfs_file_extent_item *fi;
2687         int i;
2688         int nritems;
2689         int ret;
2690
2691         BUG_ON(!btrfs_is_leaf(leaf));
2692         nritems = btrfs_header_nritems(leaf);
2693         leaf_owner = btrfs_header_owner(leaf);
2694         leaf_generation = btrfs_header_generation(leaf);
2695
2696         for (i = 0; i < nritems; i++) {
2697                 u64 disk_bytenr;
2698
2699                 btrfs_item_key_to_cpu(leaf, &key, i);
2700                 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2701                         continue;
2702                 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
2703                 if (btrfs_file_extent_type(leaf, fi) ==
2704                     BTRFS_FILE_EXTENT_INLINE)
2705                         continue;
2706                 /*
2707                  * FIXME make sure to insert a trans record that
2708                  * repeats the snapshot del on crash
2709                  */
2710                 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2711                 if (disk_bytenr == 0)
2712                         continue;
2713                 ret = btrfs_free_extent(trans, root, disk_bytenr,
2714                                 btrfs_file_extent_disk_num_bytes(leaf, fi),
2715                                 leaf->start, leaf_owner, leaf_generation,
2716                                 key.objectid, 0);
2717                 BUG_ON(ret);
2718         }
2719         return 0;
2720 }
2721
2722 static void noinline reada_walk_down(struct btrfs_root *root,
2723                                      struct extent_buffer *node,
2724                                      int slot)
2725 {
2726         u64 bytenr;
2727         u64 last = 0;
2728         u32 nritems;
2729         u32 refs;
2730         u32 blocksize;
2731         int ret;
2732         int i;
2733         int level;
2734         int skipped = 0;
2735
2736         nritems = btrfs_header_nritems(node);
2737         level = btrfs_header_level(node);
2738         if (level)
2739                 return;
2740
2741         for (i = slot; i < nritems && skipped < 32; i++) {
2742                 bytenr = btrfs_node_blockptr(node, i);
2743                 if (last && ((bytenr > last && bytenr - last > 32 * 1024) ||
2744                              (last > bytenr && last - bytenr > 32 * 1024))) {
2745                         skipped++;
2746                         continue;
2747                 }
2748                 blocksize = btrfs_level_size(root, level - 1);
2749                 if (i != slot) {
2750                         ret = btrfs_lookup_extent_ref(NULL, root, bytenr,
2751                                                       blocksize, &refs);
2752                         BUG_ON(ret);
2753                         if (refs != 1) {
2754                                 skipped++;
2755                                 continue;
2756                         }
2757                 }
2758                 mutex_unlock(&root->fs_info->fs_mutex);
2759                 ret = readahead_tree_block(root, bytenr, blocksize,
2760                                            btrfs_node_ptr_generation(node, i));
2761                 last = bytenr + blocksize;
2762                 cond_resched();
2763                 mutex_lock(&root->fs_info->fs_mutex);
2764                 if (ret)
2765                         break;
2766         }
2767 }
2768
2769 /*
2770  * helper function for drop_snapshot, this walks down the tree dropping ref
2771  * counts as it goes.
2772  */
2773 static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2774                                    struct btrfs_root *root,
2775                                    struct btrfs_path *path, int *level)
2776 {
2777         u64 root_owner;
2778         u64 root_gen;
2779         u64 bytenr;
2780         u64 ptr_gen;
2781         struct extent_buffer *next;
2782         struct extent_buffer *cur;
2783         struct extent_buffer *parent;
2784         u32 blocksize;
2785         int ret;
2786         u32 refs;
2787
2788         WARN_ON(*level < 0);
2789         WARN_ON(*level >= BTRFS_MAX_LEVEL);
2790         ret = btrfs_lookup_extent_ref(trans, root,
2791                                       path->nodes[*level]->start,
2792                                       path->nodes[*level]->len, &refs);
2793         BUG_ON(ret);
2794         if (refs > 1)
2795                 goto out;
2796
2797         /*
2798          * walk down to the last node level and free all the leaves
2799          */
2800         while(*level >= 0) {
2801                 WARN_ON(*level < 0);
2802                 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2803                 cur = path->nodes[*level];
2804
2805                 if (btrfs_header_level(cur) != *level)
2806                         WARN_ON(1);
2807
2808                 if (path->slots[*level] >=
2809                     btrfs_header_nritems(cur))
2810                         break;
2811                 if (*level == 0) {
2812                         ret = drop_leaf_ref(trans, root, cur);
2813                         BUG_ON(ret);
2814                         break;
2815                 }
2816                 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2817                 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2818                 blocksize = btrfs_level_size(root, *level - 1);
2819                 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
2820                                               &refs);
2821                 BUG_ON(ret);
2822                 if (refs != 1) {
2823                         parent = path->nodes[*level];
2824                         root_owner = btrfs_header_owner(parent);
2825                         root_gen = btrfs_header_generation(parent);
2826                         path->slots[*level]++;
2827                         ret = btrfs_free_extent(trans, root, bytenr, blocksize,
2828                                                 parent->start, root_owner,
2829                                                 root_gen, *level - 1, 1);
2830                         BUG_ON(ret);
2831                         continue;
2832                 }
2833                 next = btrfs_find_tree_block(root, bytenr, blocksize);
2834                 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
2835                         free_extent_buffer(next);
2836                         reada_walk_down(root, cur, path->slots[*level]);
2837                         mutex_unlock(&root->fs_info->fs_mutex);
2838                         next = read_tree_block(root, bytenr, blocksize,
2839                                                ptr_gen);
2840                         mutex_lock(&root->fs_info->fs_mutex);
2841                 }
2842                 WARN_ON(*level <= 0);
2843                 if (path->nodes[*level-1])
2844                         free_extent_buffer(path->nodes[*level-1]);
2845                 path->nodes[*level-1] = next;
2846                 *level = btrfs_header_level(next);
2847                 path->slots[*level] = 0;
2848         }
2849 out:
2850         WARN_ON(*level < 0);
2851         WARN_ON(*level >= BTRFS_MAX_LEVEL);
2852
2853         if (path->nodes[*level] == root->node) {
2854                 root_owner = root->root_key.objectid;
2855                 parent = path->nodes[*level];
2856         } else {
2857                 parent = path->nodes[*level + 1];
2858                 root_owner = btrfs_header_owner(parent);
2859         }
2860
2861         root_gen = btrfs_header_generation(parent);
2862         ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
2863                                 path->nodes[*level]->len, parent->start,
2864                                 root_owner, root_gen, *level, 1);
2865         free_extent_buffer(path->nodes[*level]);
2866         path->nodes[*level] = NULL;
2867         *level += 1;
2868         BUG_ON(ret);
2869         return 0;
2870 }
2871
2872 /*
2873  * helper for dropping snapshots.  This walks back up the tree in the path
2874  * to find the first node higher up where we haven't yet gone through
2875  * all the slots
2876  */
2877 static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
2878                                  struct btrfs_root *root,
2879                                  struct btrfs_path *path, int *level)
2880 {
2881         u64 root_owner;
2882         u64 root_gen;
2883         struct btrfs_root_item *root_item = &root->root_item;
2884         int i;
2885         int slot;
2886         int ret;
2887
2888         for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
2889                 slot = path->slots[i];
2890                 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
2891                         struct extent_buffer *node;
2892                         struct btrfs_disk_key disk_key;
2893                         node = path->nodes[i];
2894                         path->slots[i]++;
2895                         *level = i;
2896                         WARN_ON(*level == 0);
2897                         btrfs_node_key(node, &disk_key, path->slots[i]);
2898                         memcpy(&root_item->drop_progress,
2899                                &disk_key, sizeof(disk_key));
2900                         root_item->drop_level = i;
2901                         return 0;
2902                 } else {
2903                         struct extent_buffer *parent;
2904                         if (path->nodes[*level] == root->node)
2905                                 parent = path->nodes[*level];
2906                         else
2907                                 parent = path->nodes[*level + 1];
2908
2909                         root_owner = btrfs_header_owner(parent);
2910                         root_gen = btrfs_header_generation(parent);
2911                         ret = btrfs_free_extent(trans, root,
2912                                                 path->nodes[*level]->start,
2913                                                 path->nodes[*level]->len,
2914                                                 parent->start, root_owner,
2915                                                 root_gen, *level, 1);
2916                         BUG_ON(ret);
2917                         free_extent_buffer(path->nodes[*level]);
2918                         path->nodes[*level] = NULL;
2919                         *level = i + 1;
2920                 }
2921         }
2922         return 1;
2923 }
2924
2925 /*
2926  * drop the reference count on the tree rooted at 'snap'.  This traverses
2927  * the tree freeing any blocks that have a ref count of zero after being
2928  * decremented.
2929  */
2930 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
2931                         *root)
2932 {
2933         int ret = 0;
2934         int wret;
2935         int level;
2936         struct btrfs_path *path;
2937         int i;
2938         int orig_level;
2939         struct btrfs_root_item *root_item = &root->root_item;
2940
2941         path = btrfs_alloc_path();
2942         BUG_ON(!path);
2943
2944         level = btrfs_header_level(root->node);
2945         orig_level = level;
2946         if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2947                 path->nodes[level] = root->node;
2948                 extent_buffer_get(root->node);
2949                 path->slots[level] = 0;
2950         } else {
2951                 struct btrfs_key key;
2952                 struct btrfs_disk_key found_key;
2953                 struct extent_buffer *node;
2954
2955                 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
2956                 level = root_item->drop_level;
2957                 path->lowest_level = level;
2958                 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2959                 if (wret < 0) {
2960                         ret = wret;
2961                         goto out;
2962                 }
2963                 node = path->nodes[level];
2964                 btrfs_node_key(node, &found_key, path->slots[level]);
2965                 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2966                                sizeof(found_key)));
2967         }
2968         while(1) {
2969                 wret = walk_down_tree(trans, root, path, &level);
2970                 if (wret < 0)
2971                         ret = wret;
2972                 if (wret != 0)
2973                         break;
2974
2975                 wret = walk_up_tree(trans, root, path, &level);
2976                 if (wret < 0)
2977                         ret = wret;
2978                 if (wret != 0)
2979                         break;
2980                 /*
2981                 ret = -EAGAIN;
2982                 break;
2983                 */
2984         }
2985         for (i = 0; i <= orig_level; i++) {
2986                 if (path->nodes[i]) {
2987                         free_extent_buffer(path->nodes[i]);
2988                         path->nodes[i] = NULL;
2989                 }
2990         }
2991 out:
2992         btrfs_free_path(path);
2993         return ret;
2994 }
2995
2996 #endif
2997
2998 int btrfs_free_block_groups(struct btrfs_fs_info *info)
2999 {
3000         u64 start;
3001         u64 end;
3002         u64 ptr;
3003         int ret;
3004         while(1) {
3005                 ret = find_first_extent_bit(&info->block_group_cache, 0,
3006                                             &start, &end, (unsigned int)-1);
3007                 if (ret)
3008                         break;
3009                 ret = get_state_private(&info->block_group_cache, start, &ptr);
3010                 if (!ret)
3011                         kfree((void *)(unsigned long)ptr);
3012                 clear_extent_bits(&info->block_group_cache, start,
3013                                   end, (unsigned int)-1, GFP_NOFS);
3014         }
3015         while(1) {
3016                 ret = find_first_extent_bit(&info->free_space_cache, 0,
3017                                             &start, &end, EXTENT_DIRTY);
3018                 if (ret)
3019                         break;
3020                 clear_extent_dirty(&info->free_space_cache, start,
3021                                    end, GFP_NOFS);
3022         }
3023         return 0;
3024 }
3025
3026 int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
3027                            struct btrfs_key *key)
3028 {
3029         int ret;
3030         struct btrfs_key found_key;
3031         struct extent_buffer *leaf;
3032         int slot;
3033
3034         ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
3035         if (ret < 0)
3036                 return ret;
3037         while(1) {
3038                 slot = path->slots[0];
3039                 leaf = path->nodes[0];
3040                 if (slot >= btrfs_header_nritems(leaf)) {
3041                         ret = btrfs_next_leaf(root, path);
3042                         if (ret == 0)
3043                                 continue;
3044                         if (ret < 0)
3045                                 goto error;
3046                         break;
3047                 }
3048                 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3049
3050                 if (found_key.objectid >= key->objectid &&
3051                     found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
3052                         return 0;
3053                 path->slots[0]++;
3054         }
3055         ret = -ENOENT;
3056 error:
3057         return ret;
3058 }
3059
3060 int btrfs_read_block_groups(struct btrfs_root *root)
3061 {
3062         struct btrfs_path *path;
3063         int ret;
3064         int bit;
3065         struct btrfs_block_group_cache *cache;
3066         struct btrfs_fs_info *info = root->fs_info;
3067         struct btrfs_space_info *space_info;
3068         struct extent_io_tree *block_group_cache;
3069         struct btrfs_key key;
3070         struct btrfs_key found_key;
3071         struct extent_buffer *leaf;
3072
3073         block_group_cache = &info->block_group_cache;
3074
3075         root = info->extent_root;
3076         key.objectid = 0;
3077         key.offset = 0;
3078         btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3079         path = btrfs_alloc_path();
3080         if (!path)
3081                 return -ENOMEM;
3082
3083         while(1) {
3084                 ret = find_first_block_group(root, path, &key);
3085                 if (ret > 0) {
3086                         ret = 0;
3087                         goto error;
3088                 }
3089                 if (ret != 0) {
3090                         goto error;
3091                 }
3092                 leaf = path->nodes[0];
3093                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3094                 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3095                 if (!cache) {
3096                         ret = -ENOMEM;
3097                         break;
3098                 }
3099
3100                 read_extent_buffer(leaf, &cache->item,
3101                                    btrfs_item_ptr_offset(leaf, path->slots[0]),
3102                                    sizeof(cache->item));
3103                 memcpy(&cache->key, &found_key, sizeof(found_key));
3104                 cache->cached = 0;
3105                 cache->pinned = 0;
3106                 key.objectid = found_key.objectid + found_key.offset;
3107                 btrfs_release_path(root, path);
3108                 cache->flags = btrfs_block_group_flags(&cache->item);
3109                 bit = 0;
3110                 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
3111                         bit = BLOCK_GROUP_DATA;
3112                 } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
3113                         bit = BLOCK_GROUP_SYSTEM;
3114                 } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) {
3115                         bit = BLOCK_GROUP_METADATA;
3116                 }
3117                 set_avail_alloc_bits(info, cache->flags);
3118                 if (btrfs_chunk_readonly(root, cache->key.objectid))
3119                         cache->ro = 1;
3120
3121                 ret = update_space_info(info, cache->flags, found_key.offset,
3122                                         btrfs_block_group_used(&cache->item),
3123                                         &space_info);
3124                 BUG_ON(ret);
3125                 cache->space_info = space_info;
3126
3127                 /* use EXTENT_LOCKED to prevent merging */
3128                 set_extent_bits(block_group_cache, found_key.objectid,
3129                                 found_key.objectid + found_key.offset - 1,
3130                                 bit | EXTENT_LOCKED, GFP_NOFS);
3131                 set_state_private(block_group_cache, found_key.objectid,
3132                                   (unsigned long)cache);
3133         }
3134         ret = 0;
3135 error:
3136         btrfs_free_path(path);
3137         return ret;
3138 }
3139
3140 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
3141                            struct btrfs_root *root, u64 bytes_used,
3142                            u64 type, u64 chunk_objectid, u64 chunk_offset,
3143                            u64 size)
3144 {
3145         int ret;
3146         int bit = 0;
3147         struct btrfs_root *extent_root;
3148         struct btrfs_block_group_cache *cache;
3149         struct extent_io_tree *block_group_cache;
3150
3151         extent_root = root->fs_info->extent_root;
3152         block_group_cache = &root->fs_info->block_group_cache;
3153
3154         cache = kzalloc(sizeof(*cache), GFP_NOFS);
3155         BUG_ON(!cache);
3156         cache->key.objectid = chunk_offset;
3157         cache->key.offset = size;
3158
3159         btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3160         btrfs_set_block_group_used(&cache->item, bytes_used);
3161         btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
3162         cache->flags = type;
3163         btrfs_set_block_group_flags(&cache->item, type);
3164
3165         ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
3166                                 &cache->space_info);
3167         BUG_ON(ret);
3168
3169         bit = block_group_state_bits(type);
3170         set_extent_bits(block_group_cache, chunk_offset,
3171                         chunk_offset + size - 1,
3172                         bit | EXTENT_LOCKED, GFP_NOFS);
3173
3174         set_state_private(block_group_cache, chunk_offset,
3175                           (unsigned long)cache);
3176         ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
3177                                 sizeof(cache->item));
3178         BUG_ON(ret);
3179
3180         finish_current_insert(trans, extent_root);
3181         ret = del_pending_extents(trans, extent_root);
3182         BUG_ON(ret);
3183         set_avail_alloc_bits(extent_root->fs_info, type);
3184         return 0;
3185 }
3186
3187 /*
3188  * This is for converter use only.
3189  *
3190  * In that case, we don't know where are free blocks located.
3191  * Therefore all block group cache entries must be setup properly
3192  * before doing any block allocation.
3193  */
3194 int btrfs_make_block_groups(struct btrfs_trans_handle *trans,
3195                             struct btrfs_root *root)
3196 {
3197         u64 total_bytes;
3198         u64 cur_start;
3199         u64 group_type;
3200         u64 group_size;
3201         u64 group_align;
3202         u64 total_data = 0;
3203         u64 total_metadata = 0;
3204         u64 chunk_objectid;
3205         int ret;
3206         int bit;
3207         struct btrfs_root *extent_root;
3208         struct btrfs_block_group_cache *cache;
3209         struct extent_io_tree *block_group_cache;
3210
3211         extent_root = root->fs_info->extent_root;
3212         block_group_cache = &root->fs_info->block_group_cache;
3213         chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3214         total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
3215         group_align = 64 * root->sectorsize;
3216
3217         cur_start = 0;
3218         while (cur_start < total_bytes) {
3219                 group_size = total_bytes / 12;
3220                 group_size = min_t(u64, group_size, total_bytes - cur_start);
3221                 if (cur_start == 0) {
3222                         bit = BLOCK_GROUP_SYSTEM;
3223                         group_type = BTRFS_BLOCK_GROUP_SYSTEM;
3224                         group_size /= 4;
3225                         group_size &= ~(group_align - 1);
3226                         group_size = max_t(u64, group_size, 8 * 1024 * 1024);
3227                         group_size = min_t(u64, group_size, 32 * 1024 * 1024);
3228                 } else {
3229                         group_size &= ~(group_align - 1);
3230                         if (total_data >= total_metadata * 2) {
3231                                 group_type = BTRFS_BLOCK_GROUP_METADATA;
3232                                 group_size = min_t(u64, group_size,
3233                                                    1ULL * 1024 * 1024 * 1024);
3234                                 total_metadata += group_size;
3235                         } else {
3236                                 group_type = BTRFS_BLOCK_GROUP_DATA;
3237                                 group_size = min_t(u64, group_size,
3238                                                    5ULL * 1024 * 1024 * 1024);
3239                                 total_data += group_size;
3240                         }
3241                         if ((total_bytes - cur_start) * 4 < group_size * 5)
3242                                 group_size = total_bytes - cur_start;
3243                 }
3244
3245                 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3246                 BUG_ON(!cache);
3247
3248                 cache->key.objectid = cur_start;
3249                 cache->key.offset = group_size;
3250                 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3251
3252                 btrfs_set_block_group_used(&cache->item, 0);
3253                 btrfs_set_block_group_chunk_objectid(&cache->item,
3254                                                      chunk_objectid);
3255                 btrfs_set_block_group_flags(&cache->item, group_type);
3256
3257                 cache->flags = group_type;
3258
3259                 ret = update_space_info(root->fs_info, group_type, group_size,
3260                                         0, &cache->space_info);
3261                 BUG_ON(ret);
3262                 set_avail_alloc_bits(extent_root->fs_info, group_type);
3263
3264                 set_extent_bits(block_group_cache, cur_start,
3265                                 cur_start + group_size - 1,
3266                                 bit | EXTENT_LOCKED, GFP_NOFS);
3267                 set_state_private(block_group_cache, cur_start,
3268                                   (unsigned long)cache);
3269                 cur_start += group_size;
3270         }
3271         /* then insert all the items */
3272         cur_start = 0;
3273         while(cur_start < total_bytes) {
3274                 cache = btrfs_lookup_block_group(root->fs_info, cur_start);
3275                 BUG_ON(!cache);
3276
3277                 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
3278                                         sizeof(cache->item));
3279                 BUG_ON(ret);
3280
3281                 finish_current_insert(trans, extent_root);
3282                 ret = del_pending_extents(trans, extent_root);
3283                 BUG_ON(ret);
3284
3285                 cur_start = cache->key.objectid + cache->key.offset;
3286         }
3287         return 0;
3288 }
3289
3290 int btrfs_update_block_group(struct btrfs_trans_handle *trans,
3291                              struct btrfs_root *root,
3292                              u64 bytenr, u64 num_bytes, int alloc,
3293                              int mark_free)
3294 {
3295         return update_block_group(trans, root, bytenr, num_bytes,
3296                                   alloc, mark_free);
3297 }