btrfs-progs: remove unused argument from set_extent_dirty
[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 <stdint.h>
22 #include <math.h>
23 #include "kerncompat.h"
24 #include "radix-tree.h"
25 #include "ctree.h"
26 #include "disk-io.h"
27 #include "print-tree.h"
28 #include "transaction.h"
29 #include "crc32c.h"
30 #include "volumes.h"
31 #include "free-space-cache.h"
32 #include "utils.h"
33
34 #define PENDING_EXTENT_INSERT 0
35 #define PENDING_EXTENT_DELETE 1
36 #define PENDING_BACKREF_UPDATE 2
37
38 struct pending_extent_op {
39         int type;
40         u64 bytenr;
41         u64 num_bytes;
42         u64 flags;
43         struct btrfs_disk_key key;
44         int level;
45 };
46
47 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
48                                      struct btrfs_root *root,
49                                      u64 root_objectid, u64 generation,
50                                      u64 flags, struct btrfs_disk_key *key,
51                                      int level, struct btrfs_key *ins);
52 static int __free_extent(struct btrfs_trans_handle *trans,
53                          struct btrfs_root *root,
54                          u64 bytenr, u64 num_bytes, u64 parent,
55                          u64 root_objectid, u64 owner_objectid,
56                          u64 owner_offset, int refs_to_drop);
57 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
58                                  btrfs_root *extent_root);
59 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
60                                btrfs_root *extent_root);
61 static struct btrfs_block_group_cache *
62 btrfs_find_block_group(struct btrfs_root *root, struct btrfs_block_group_cache
63                        *hint, u64 search_start, int data, int owner);
64
65 static int remove_sb_from_cache(struct btrfs_root *root,
66                                 struct btrfs_block_group_cache *cache)
67 {
68         u64 bytenr;
69         u64 *logical;
70         int stripe_len;
71         int i, nr, ret;
72         struct extent_io_tree *free_space_cache;
73
74         free_space_cache = &root->fs_info->free_space_cache;
75         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
76                 bytenr = btrfs_sb_offset(i);
77                 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
78                                        cache->key.objectid, bytenr, 0,
79                                        &logical, &nr, &stripe_len);
80                 BUG_ON(ret);
81                 while (nr--) {
82                         clear_extent_dirty(free_space_cache, logical[nr],
83                                 logical[nr] + stripe_len - 1, GFP_NOFS);
84                 }
85                 kfree(logical);
86         }
87         return 0;
88 }
89
90 static int cache_block_group(struct btrfs_root *root,
91                              struct btrfs_block_group_cache *block_group)
92 {
93         struct btrfs_path *path;
94         int ret;
95         struct btrfs_key key;
96         struct extent_buffer *leaf;
97         struct extent_io_tree *free_space_cache;
98         int slot;
99         u64 last;
100         u64 hole_size;
101
102         if (!block_group)
103                 return 0;
104
105         root = root->fs_info->extent_root;
106         free_space_cache = &root->fs_info->free_space_cache;
107
108         if (block_group->cached)
109                 return 0;
110
111         path = btrfs_alloc_path();
112         if (!path)
113                 return -ENOMEM;
114
115         path->reada = 2;
116         last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
117         key.objectid = last;
118         key.offset = 0;
119         key.type = 0;
120
121         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
122         if (ret < 0)
123                 goto err;
124
125         while(1) {
126                 leaf = path->nodes[0];
127                 slot = path->slots[0];
128                 if (slot >= btrfs_header_nritems(leaf)) {
129                         ret = btrfs_next_leaf(root, path);
130                         if (ret < 0)
131                                 goto err;
132                         if (ret == 0) {
133                                 continue;
134                         } else {
135                                 break;
136                         }
137                 }
138                 btrfs_item_key_to_cpu(leaf, &key, slot);
139                 if (key.objectid < block_group->key.objectid) {
140                         goto next;
141                 }
142                 if (key.objectid >= block_group->key.objectid +
143                     block_group->key.offset) {
144                         break;
145                 }
146
147                 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
148                     key.type == BTRFS_METADATA_ITEM_KEY) {
149                         if (key.objectid > last) {
150                                 hole_size = key.objectid - last;
151                                 set_extent_dirty(free_space_cache, last,
152                                                  last + hole_size - 1);
153                         }
154                         if (key.type == BTRFS_METADATA_ITEM_KEY)
155                                 last = key.objectid + root->nodesize;
156                         else
157                                 last = key.objectid + key.offset;
158                 }
159 next:
160                 path->slots[0]++;
161         }
162
163         if (block_group->key.objectid +
164             block_group->key.offset > last) {
165                 hole_size = block_group->key.objectid +
166                         block_group->key.offset - last;
167                 set_extent_dirty(free_space_cache, last, last + hole_size - 1);
168         }
169         remove_sb_from_cache(root, block_group);
170         block_group->cached = 1;
171 err:
172         btrfs_free_path(path);
173         return 0;
174 }
175
176 struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
177                                                        btrfs_fs_info *info,
178                                                        u64 bytenr)
179 {
180         struct extent_io_tree *block_group_cache;
181         struct btrfs_block_group_cache *block_group = NULL;
182         u64 ptr;
183         u64 start;
184         u64 end;
185         int ret;
186
187         bytenr = max_t(u64, bytenr,
188                        BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
189         block_group_cache = &info->block_group_cache;
190         ret = find_first_extent_bit(block_group_cache,
191                                     bytenr, &start, &end,
192                                     BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
193                                     BLOCK_GROUP_SYSTEM);
194         if (ret) {
195                 return NULL;
196         }
197         ret = get_state_private(block_group_cache, start, &ptr);
198         if (ret)
199                 return NULL;
200
201         block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
202         return block_group;
203 }
204
205 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
206                                                          btrfs_fs_info *info,
207                                                          u64 bytenr)
208 {
209         struct extent_io_tree *block_group_cache;
210         struct btrfs_block_group_cache *block_group = NULL;
211         u64 ptr;
212         u64 start;
213         u64 end;
214         int ret;
215
216         block_group_cache = &info->block_group_cache;
217         ret = find_first_extent_bit(block_group_cache,
218                                     bytenr, &start, &end,
219                                     BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
220                                     BLOCK_GROUP_SYSTEM);
221         if (ret) {
222                 return NULL;
223         }
224         ret = get_state_private(block_group_cache, start, &ptr);
225         if (ret)
226                 return NULL;
227
228         block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
229         if (block_group->key.objectid <= bytenr && bytenr <
230             block_group->key.objectid + block_group->key.offset)
231                 return block_group;
232         return NULL;
233 }
234
235 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
236 {
237         return (cache->flags & bits) == bits;
238 }
239
240 static int noinline find_search_start(struct btrfs_root *root,
241                               struct btrfs_block_group_cache **cache_ret,
242                               u64 *start_ret, int num, int data)
243 {
244         int ret;
245         struct btrfs_block_group_cache *cache = *cache_ret;
246         u64 last = *start_ret;
247         u64 start = 0;
248         u64 end = 0;
249         u64 search_start = *start_ret;
250         int wrapped = 0;
251
252         if (!cache)
253                 goto out;
254 again:
255         ret = cache_block_group(root, cache);
256         if (ret)
257                 goto out;
258
259         last = max(search_start, cache->key.objectid);
260         if (cache->ro || !block_group_bits(cache, data))
261                 goto new_group;
262
263         while(1) {
264                 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
265                                             last, &start, &end, EXTENT_DIRTY);
266                 if (ret) {
267                         goto new_group;
268                 }
269
270                 start = max(last, start);
271                 last = end + 1;
272                 if (last - start < num) {
273                         continue;
274                 }
275                 if (start + num > cache->key.objectid + cache->key.offset) {
276                         goto new_group;
277                 }
278                 *start_ret = start;
279                 return 0;
280         }
281 out:
282         *start_ret = last;
283         cache = btrfs_lookup_block_group(root->fs_info, search_start);
284         if (!cache) {
285                 printk("Unable to find block group for %llu\n",
286                         (unsigned long long)search_start);
287                 WARN_ON(1);
288         }
289         return -ENOSPC;
290
291 new_group:
292         last = cache->key.objectid + cache->key.offset;
293 wrapped:
294         cache = btrfs_lookup_first_block_group(root->fs_info, last);
295         if (!cache) {
296                 if (!wrapped) {
297                         wrapped = 1;
298                         last = search_start;
299                         goto wrapped;
300                 }
301                 goto out;
302         }
303         *cache_ret = cache;
304         goto again;
305 }
306
307 static int block_group_state_bits(u64 flags)
308 {
309         int bits = 0;
310         if (flags & BTRFS_BLOCK_GROUP_DATA)
311                 bits |= BLOCK_GROUP_DATA;
312         if (flags & BTRFS_BLOCK_GROUP_METADATA)
313                 bits |= BLOCK_GROUP_METADATA;
314         if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
315                 bits |= BLOCK_GROUP_SYSTEM;
316         return bits;
317 }
318
319 static struct btrfs_block_group_cache *
320 btrfs_find_block_group(struct btrfs_root *root, struct btrfs_block_group_cache
321                        *hint, u64 search_start, int data, int owner)
322 {
323         struct btrfs_block_group_cache *cache;
324         struct extent_io_tree *block_group_cache;
325         struct btrfs_block_group_cache *found_group = NULL;
326         struct btrfs_fs_info *info = root->fs_info;
327         u64 used;
328         u64 last = 0;
329         u64 hint_last;
330         u64 start;
331         u64 end;
332         u64 free_check;
333         u64 ptr;
334         int bit;
335         int ret;
336         int full_search = 0;
337         int factor = 10;
338
339         block_group_cache = &info->block_group_cache;
340
341         if (!owner)
342                 factor = 10;
343
344         bit = block_group_state_bits(data);
345
346         if (search_start) {
347                 struct btrfs_block_group_cache *shint;
348                 shint = btrfs_lookup_block_group(info, search_start);
349                 if (shint && !shint->ro && block_group_bits(shint, data)) {
350                         used = btrfs_block_group_used(&shint->item);
351                         if (used + shint->pinned <
352                             div_factor(shint->key.offset, factor)) {
353                                 return shint;
354                         }
355                 }
356         }
357         if (hint && !hint->ro && block_group_bits(hint, data)) {
358                 used = btrfs_block_group_used(&hint->item);
359                 if (used + hint->pinned <
360                     div_factor(hint->key.offset, factor)) {
361                         return hint;
362                 }
363                 last = hint->key.objectid + hint->key.offset;
364                 hint_last = last;
365         } else {
366                 if (hint)
367                         hint_last = max(hint->key.objectid, search_start);
368                 else
369                         hint_last = search_start;
370
371                 last = hint_last;
372         }
373 again:
374         while(1) {
375                 ret = find_first_extent_bit(block_group_cache, last,
376                                             &start, &end, bit);
377                 if (ret)
378                         break;
379
380                 ret = get_state_private(block_group_cache, start, &ptr);
381                 if (ret)
382                         break;
383
384                 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
385                 last = cache->key.objectid + cache->key.offset;
386                 used = btrfs_block_group_used(&cache->item);
387
388                 if (!cache->ro && block_group_bits(cache, data)) {
389                         if (full_search)
390                                 free_check = cache->key.offset;
391                         else
392                                 free_check = div_factor(cache->key.offset,
393                                                         factor);
394
395                         if (used + cache->pinned < free_check) {
396                                 found_group = cache;
397                                 goto found;
398                         }
399                 }
400                 cond_resched();
401         }
402         if (!full_search) {
403                 last = search_start;
404                 full_search = 1;
405                 goto again;
406         }
407 found:
408         return found_group;
409 }
410
411 /*
412  * Back reference rules.  Back refs have three main goals:
413  *
414  * 1) differentiate between all holders of references to an extent so that
415  *    when a reference is dropped we can make sure it was a valid reference
416  *    before freeing the extent.
417  *
418  * 2) Provide enough information to quickly find the holders of an extent
419  *    if we notice a given block is corrupted or bad.
420  *
421  * 3) Make it easy to migrate blocks for FS shrinking or storage pool
422  *    maintenance.  This is actually the same as #2, but with a slightly
423  *    different use case.
424  *
425  * There are two kinds of back refs. The implicit back refs is optimized
426  * for pointers in non-shared tree blocks. For a given pointer in a block,
427  * back refs of this kind provide information about the block's owner tree
428  * and the pointer's key. These information allow us to find the block by
429  * b-tree searching. The full back refs is for pointers in tree blocks not
430  * referenced by their owner trees. The location of tree block is recorded
431  * in the back refs. Actually the full back refs is generic, and can be
432  * used in all cases the implicit back refs is used. The major shortcoming
433  * of the full back refs is its overhead. Every time a tree block gets
434  * COWed, we have to update back refs entry for all pointers in it.
435  *
436  * For a newly allocated tree block, we use implicit back refs for
437  * pointers in it. This means most tree related operations only involve
438  * implicit back refs. For a tree block created in old transaction, the
439  * only way to drop a reference to it is COW it. So we can detect the
440  * event that tree block loses its owner tree's reference and do the
441  * back refs conversion.
442  *
443  * When a tree block is COW'd through a tree, there are four cases:
444  *
445  * The reference count of the block is one and the tree is the block's
446  * owner tree. Nothing to do in this case.
447  *
448  * The reference count of the block is one and the tree is not the
449  * block's owner tree. In this case, full back refs is used for pointers
450  * in the block. Remove these full back refs, add implicit back refs for
451  * every pointers in the new block.
452  *
453  * The reference count of the block is greater than one and the tree is
454  * the block's owner tree. In this case, implicit back refs is used for
455  * pointers in the block. Add full back refs for every pointers in the
456  * block, increase lower level extents' reference counts. The original
457  * implicit back refs are entailed to the new block.
458  *
459  * The reference count of the block is greater than one and the tree is
460  * not the block's owner tree. Add implicit back refs for every pointer in
461  * the new block, increase lower level extents' reference count.
462  *
463  * Back Reference Key composing:
464  *
465  * The key objectid corresponds to the first byte in the extent,
466  * The key type is used to differentiate between types of back refs.
467  * There are different meanings of the key offset for different types
468  * of back refs.
469  *
470  * File extents can be referenced by:
471  *
472  * - multiple snapshots, subvolumes, or different generations in one subvol
473  * - different files inside a single subvolume
474  * - different offsets inside a file (bookend extents in file.c)
475  *
476  * The extent ref structure for the implicit back refs has fields for:
477  *
478  * - Objectid of the subvolume root
479  * - objectid of the file holding the reference
480  * - original offset in the file
481  * - how many bookend extents
482  *
483  * The key offset for the implicit back refs is hash of the first
484  * three fields.
485  *
486  * The extent ref structure for the full back refs has field for:
487  *
488  * - number of pointers in the tree leaf
489  *
490  * The key offset for the implicit back refs is the first byte of
491  * the tree leaf
492  *
493  * When a file extent is allocated, The implicit back refs is used.
494  * the fields are filled in:
495  *
496  *     (root_key.objectid, inode objectid, offset in file, 1)
497  *
498  * When a file extent is removed file truncation, we find the
499  * corresponding implicit back refs and check the following fields:
500  *
501  *     (btrfs_header_owner(leaf), inode objectid, offset in file)
502  *
503  * Btree extents can be referenced by:
504  *
505  * - Different subvolumes
506  *
507  * Both the implicit back refs and the full back refs for tree blocks
508  * only consist of key. The key offset for the implicit back refs is
509  * objectid of block's owner tree. The key offset for the full back refs
510  * is the first byte of parent block.
511  *
512  * When implicit back refs is used, information about the lowest key and
513  * level of the tree block are required. These information are stored in
514  * tree block info structure.
515  */
516
517 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
518 static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
519                                   struct btrfs_root *root,
520                                   struct btrfs_path *path,
521                                   u64 owner, u32 extra_size)
522 {
523         struct btrfs_extent_item *item;
524         struct btrfs_extent_item_v0 *ei0;
525         struct btrfs_extent_ref_v0 *ref0;
526         struct btrfs_tree_block_info *bi;
527         struct extent_buffer *leaf;
528         struct btrfs_key key;
529         struct btrfs_key found_key;
530         u32 new_size = sizeof(*item);
531         u64 refs;
532         int ret;
533
534         leaf = path->nodes[0];
535         BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
536
537         btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
538         ei0 = btrfs_item_ptr(leaf, path->slots[0],
539                              struct btrfs_extent_item_v0);
540         refs = btrfs_extent_refs_v0(leaf, ei0);
541
542         if (owner == (u64)-1) {
543                 while (1) {
544                         if (path->slots[0] >= btrfs_header_nritems(leaf)) {
545                                 ret = btrfs_next_leaf(root, path);
546                                 if (ret < 0)
547                                         return ret;
548                                 BUG_ON(ret > 0);
549                                 leaf = path->nodes[0];
550                         }
551                         btrfs_item_key_to_cpu(leaf, &found_key,
552                                               path->slots[0]);
553                         BUG_ON(key.objectid != found_key.objectid);
554                         if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
555                                 path->slots[0]++;
556                                 continue;
557                         }
558                         ref0 = btrfs_item_ptr(leaf, path->slots[0],
559                                               struct btrfs_extent_ref_v0);
560                         owner = btrfs_ref_objectid_v0(leaf, ref0);
561                         break;
562                 }
563         }
564         btrfs_release_path(path);
565
566         if (owner < BTRFS_FIRST_FREE_OBJECTID)
567                 new_size += sizeof(*bi);
568
569         new_size -= sizeof(*ei0);
570         ret = btrfs_search_slot(trans, root, &key, path, new_size, 1);
571         if (ret < 0)
572                 return ret;
573         BUG_ON(ret);
574
575         ret = btrfs_extend_item(root, path, new_size);
576         BUG_ON(ret);
577
578         leaf = path->nodes[0];
579         item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
580         btrfs_set_extent_refs(leaf, item, refs);
581         /* FIXME: get real generation */
582         btrfs_set_extent_generation(leaf, item, 0);
583         if (owner < BTRFS_FIRST_FREE_OBJECTID) {
584                 btrfs_set_extent_flags(leaf, item,
585                                        BTRFS_EXTENT_FLAG_TREE_BLOCK |
586                                        BTRFS_BLOCK_FLAG_FULL_BACKREF);
587                 bi = (struct btrfs_tree_block_info *)(item + 1);
588                 /* FIXME: get first key of the block */
589                 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
590                 btrfs_set_tree_block_level(leaf, bi, (int)owner);
591         } else {
592                 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
593         }
594         btrfs_mark_buffer_dirty(leaf);
595         return 0;
596 }
597 #endif
598
599 u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
600 {
601         u32 high_crc = ~(u32)0;
602         u32 low_crc = ~(u32)0;
603         __le64 lenum;
604
605         lenum = cpu_to_le64(root_objectid);
606         high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
607         lenum = cpu_to_le64(owner);
608         low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
609         lenum = cpu_to_le64(offset);
610         low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
611
612         return ((u64)high_crc << 31) ^ (u64)low_crc;
613 }
614
615 static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
616                                      struct btrfs_extent_data_ref *ref)
617 {
618         return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
619                                     btrfs_extent_data_ref_objectid(leaf, ref),
620                                     btrfs_extent_data_ref_offset(leaf, ref));
621 }
622
623 static int match_extent_data_ref(struct extent_buffer *leaf,
624                                  struct btrfs_extent_data_ref *ref,
625                                  u64 root_objectid, u64 owner, u64 offset)
626 {
627         if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
628             btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
629             btrfs_extent_data_ref_offset(leaf, ref) != offset)
630                 return 0;
631         return 1;
632 }
633
634 static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
635                                            struct btrfs_root *root,
636                                            struct btrfs_path *path,
637                                            u64 bytenr, u64 parent,
638                                            u64 root_objectid,
639                                            u64 owner, u64 offset)
640 {
641         struct btrfs_key key;
642         struct btrfs_extent_data_ref *ref;
643         struct extent_buffer *leaf;
644         u32 nritems;
645         int ret;
646         int recow;
647         int err = -ENOENT;
648
649         key.objectid = bytenr;
650         if (parent) {
651                 key.type = BTRFS_SHARED_DATA_REF_KEY;
652                 key.offset = parent;
653         } else {
654                 key.type = BTRFS_EXTENT_DATA_REF_KEY;
655                 key.offset = hash_extent_data_ref(root_objectid,
656                                                   owner, offset);
657         }
658 again:
659         recow = 0;
660         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
661         if (ret < 0) {
662                 err = ret;
663                 goto fail;
664         }
665
666         if (parent) {
667                 if (!ret)
668                         return 0;
669 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
670                 key.type = BTRFS_EXTENT_REF_V0_KEY;
671                 btrfs_release_path(path);
672                 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
673                 if (ret < 0) {
674                         err = ret;
675                         goto fail;
676                 }
677                 if (!ret)
678                         return 0;
679 #endif
680                 goto fail;
681         }
682
683         leaf = path->nodes[0];
684         nritems = btrfs_header_nritems(leaf);
685         while (1) {
686                 if (path->slots[0] >= nritems) {
687                         ret = btrfs_next_leaf(root, path);
688                         if (ret < 0)
689                                 err = ret;
690                         if (ret)
691                                 goto fail;
692
693                         leaf = path->nodes[0];
694                         nritems = btrfs_header_nritems(leaf);
695                         recow = 1;
696                 }
697
698                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
699                 if (key.objectid != bytenr ||
700                     key.type != BTRFS_EXTENT_DATA_REF_KEY)
701                         goto fail;
702                 
703                 ref = btrfs_item_ptr(leaf, path->slots[0],
704                                      struct btrfs_extent_data_ref);
705
706                 if (match_extent_data_ref(leaf, ref, root_objectid,
707                                           owner, offset)) {
708                         if (recow) {
709                                 btrfs_release_path(path);
710                                 goto again;
711                         }
712                         err = 0;
713                         break;
714                 }
715                 path->slots[0]++;
716         }
717 fail:
718         return err;
719 }
720
721 static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
722                                            struct btrfs_root *root,
723                                            struct btrfs_path *path,
724                                            u64 bytenr, u64 parent,
725                                            u64 root_objectid, u64 owner,
726                                            u64 offset, int refs_to_add)
727 {
728         struct btrfs_key key;
729         struct extent_buffer *leaf;
730         u32 size;
731         u32 num_refs;
732         int ret;
733
734         key.objectid = bytenr;
735         if (parent) {
736                 key.type = BTRFS_SHARED_DATA_REF_KEY;
737                 key.offset = parent;
738                 size = sizeof(struct btrfs_shared_data_ref);
739         } else {
740                 key.type = BTRFS_EXTENT_DATA_REF_KEY;
741                 key.offset = hash_extent_data_ref(root_objectid,
742                                                   owner, offset);
743                 size = sizeof(struct btrfs_extent_data_ref);
744         }
745
746         ret = btrfs_insert_empty_item(trans, root, path, &key, size);
747         if (ret && ret != -EEXIST)
748                 goto fail;
749
750         leaf = path->nodes[0];
751         if (parent) {
752                 struct btrfs_shared_data_ref *ref;
753                 ref = btrfs_item_ptr(leaf, path->slots[0],
754                                      struct btrfs_shared_data_ref);
755                 if (ret == 0) {
756                         btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
757                 } else {
758                         num_refs = btrfs_shared_data_ref_count(leaf, ref);
759                         num_refs += refs_to_add;
760                         btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
761                 }
762         } else {
763                 struct btrfs_extent_data_ref *ref;
764                 while (ret == -EEXIST) {
765                         ref = btrfs_item_ptr(leaf, path->slots[0],
766                                              struct btrfs_extent_data_ref);
767                         if (match_extent_data_ref(leaf, ref, root_objectid,
768                                                   owner, offset))
769                                 break;
770                         btrfs_release_path(path);
771
772                         key.offset++;
773                         ret = btrfs_insert_empty_item(trans, root, path, &key,
774                                                       size);
775                         if (ret && ret != -EEXIST)
776                                 goto fail;
777
778                         leaf = path->nodes[0];
779                 }
780                 ref = btrfs_item_ptr(leaf, path->slots[0],
781                                      struct btrfs_extent_data_ref);
782                 if (ret == 0) {
783                         btrfs_set_extent_data_ref_root(leaf, ref,
784                                                        root_objectid);
785                         btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
786                         btrfs_set_extent_data_ref_offset(leaf, ref, offset);
787                         btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
788                 } else {
789                         num_refs = btrfs_extent_data_ref_count(leaf, ref);
790                         num_refs += refs_to_add;
791                         btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
792                 }
793         }
794         btrfs_mark_buffer_dirty(leaf);
795         ret = 0;
796 fail:
797         btrfs_release_path(path);
798         return ret;
799 }
800
801 static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
802                                            struct btrfs_root *root,
803                                            struct btrfs_path *path,
804                                            int refs_to_drop)
805 {
806         struct btrfs_key key;
807         struct btrfs_extent_data_ref *ref1 = NULL;
808         struct btrfs_shared_data_ref *ref2 = NULL;
809         struct extent_buffer *leaf;
810         u32 num_refs = 0;
811         int ret = 0;
812
813         leaf = path->nodes[0];
814         btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
815
816         if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
817                 ref1 = btrfs_item_ptr(leaf, path->slots[0],
818                                       struct btrfs_extent_data_ref);
819                 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
820         } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
821                 ref2 = btrfs_item_ptr(leaf, path->slots[0],
822                                       struct btrfs_shared_data_ref);
823                 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
824 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
825         } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
826                 struct btrfs_extent_ref_v0 *ref0;
827                 ref0 = btrfs_item_ptr(leaf, path->slots[0],
828                                       struct btrfs_extent_ref_v0);
829                 num_refs = btrfs_ref_count_v0(leaf, ref0);
830 #endif
831         } else {
832                 BUG();
833         }
834
835         BUG_ON(num_refs < refs_to_drop);
836         num_refs -= refs_to_drop;
837
838         if (num_refs == 0) {
839                 ret = btrfs_del_item(trans, root, path);
840         } else {
841                 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
842                         btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
843                 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
844                         btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
845 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
846                 else {
847                         struct btrfs_extent_ref_v0 *ref0;
848                         ref0 = btrfs_item_ptr(leaf, path->slots[0],
849                                         struct btrfs_extent_ref_v0);
850                         btrfs_set_ref_count_v0(leaf, ref0, num_refs);
851                 }
852 #endif
853                 btrfs_mark_buffer_dirty(leaf);
854         }
855         return ret;
856 }
857
858 static noinline u32 extent_data_ref_count(struct btrfs_path *path,
859                                           struct btrfs_extent_inline_ref *iref)
860 {
861         struct btrfs_key key;
862         struct extent_buffer *leaf;
863         struct btrfs_extent_data_ref *ref1;
864         struct btrfs_shared_data_ref *ref2;
865         u32 num_refs = 0;
866
867         leaf = path->nodes[0];
868         btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
869         if (iref) {
870                 if (btrfs_extent_inline_ref_type(leaf, iref) ==
871                     BTRFS_EXTENT_DATA_REF_KEY) {
872                         ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
873                         num_refs = btrfs_extent_data_ref_count(leaf, ref1);
874                 } else {
875                         ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
876                         num_refs = btrfs_shared_data_ref_count(leaf, ref2);
877                 }
878         } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
879                 ref1 = btrfs_item_ptr(leaf, path->slots[0],
880                                       struct btrfs_extent_data_ref);
881                 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
882         } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
883                 ref2 = btrfs_item_ptr(leaf, path->slots[0],
884                                       struct btrfs_shared_data_ref);
885                 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
886 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
887         } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
888                 struct btrfs_extent_ref_v0 *ref0;
889                 ref0 = btrfs_item_ptr(leaf, path->slots[0],
890                                       struct btrfs_extent_ref_v0);
891                 num_refs = btrfs_ref_count_v0(leaf, ref0);
892 #endif
893         } else {
894                 BUG();
895         }
896         return num_refs;
897 }
898
899 static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
900                                           struct btrfs_root *root,
901                                           struct btrfs_path *path,
902                                           u64 bytenr, u64 parent,
903                                           u64 root_objectid)
904 {
905         struct btrfs_key key;
906         int ret;
907
908         key.objectid = bytenr;
909         if (parent) {
910                 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
911                 key.offset = parent;
912         } else {
913                 key.type = BTRFS_TREE_BLOCK_REF_KEY;
914                 key.offset = root_objectid;
915         }
916
917         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
918         if (ret > 0)
919                 ret = -ENOENT;
920 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
921         if (ret == -ENOENT && parent) {
922                 btrfs_release_path(path);
923                 key.type = BTRFS_EXTENT_REF_V0_KEY;
924                 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
925                 if (ret > 0)
926                         ret = -ENOENT;
927         }
928 #endif
929         return ret;
930 }
931
932 static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
933                                           struct btrfs_root *root,
934                                           struct btrfs_path *path,
935                                           u64 bytenr, u64 parent,
936                                           u64 root_objectid)
937 {
938         struct btrfs_key key;
939         int ret;
940
941         key.objectid = bytenr;
942         if (parent) {
943                 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
944                 key.offset = parent;
945         } else {
946                 key.type = BTRFS_TREE_BLOCK_REF_KEY;
947                 key.offset = root_objectid;
948         }
949
950         ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
951
952         btrfs_release_path(path);
953         return ret;
954 }
955
956 static inline int extent_ref_type(u64 parent, u64 owner)
957 {
958         int type;
959         if (owner < BTRFS_FIRST_FREE_OBJECTID) {
960                 if (parent > 0)
961                         type = BTRFS_SHARED_BLOCK_REF_KEY;
962                 else
963                         type = BTRFS_TREE_BLOCK_REF_KEY;
964         } else {
965                 if (parent > 0)
966                         type = BTRFS_SHARED_DATA_REF_KEY;
967                 else
968                         type = BTRFS_EXTENT_DATA_REF_KEY;
969         }
970         return type;
971 }
972
973 static int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
974                                  struct btrfs_root *root,
975                                  struct btrfs_path *path,
976                                  struct btrfs_extent_inline_ref **ref_ret,
977                                  u64 bytenr, u64 num_bytes,
978                                  u64 parent, u64 root_objectid,
979                                  u64 owner, u64 offset, int insert)
980 {
981         struct btrfs_key key;
982         struct extent_buffer *leaf;
983         struct btrfs_extent_item *ei;
984         struct btrfs_extent_inline_ref *iref;
985         u64 flags;
986         u32 item_size;
987         unsigned long ptr;
988         unsigned long end;
989         int extra_size;
990         int type;
991         int want;
992         int ret;
993         int err = 0;
994         int skinny_metadata =
995                 btrfs_fs_incompat(root->fs_info, SKINNY_METADATA);
996
997         key.objectid = bytenr;
998         key.type = BTRFS_EXTENT_ITEM_KEY;
999         key.offset = num_bytes;
1000
1001         want = extent_ref_type(parent, owner);
1002         if (insert)
1003                 extra_size = btrfs_extent_inline_ref_size(want);
1004         else
1005                 extra_size = -1;
1006
1007         if (owner < BTRFS_FIRST_FREE_OBJECTID && skinny_metadata) {
1008                 skinny_metadata = 1;
1009                 key.type = BTRFS_METADATA_ITEM_KEY;
1010                 key.offset = owner;
1011         } else if (skinny_metadata) {
1012                 skinny_metadata = 0;
1013         }
1014
1015 again:
1016         ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1017         if (ret < 0) {
1018                 err = ret;
1019                 goto out;
1020         }
1021
1022         /*
1023          * We may be a newly converted file system which still has the old fat
1024          * extent entries for metadata, so try and see if we have one of those.
1025          */
1026         if (ret > 0 && skinny_metadata) {
1027                 skinny_metadata = 0;
1028                 if (path->slots[0]) {
1029                         path->slots[0]--;
1030                         btrfs_item_key_to_cpu(path->nodes[0], &key,
1031                                               path->slots[0]);
1032                         if (key.objectid == bytenr &&
1033                             key.type == BTRFS_EXTENT_ITEM_KEY &&
1034                             key.offset == num_bytes)
1035                                 ret = 0;
1036                 }
1037                 if (ret) {
1038                         key.type = BTRFS_EXTENT_ITEM_KEY;
1039                         key.offset = num_bytes;
1040                         btrfs_release_path(path);
1041                         goto again;
1042                 }
1043         }
1044
1045         if (ret) {
1046                 printf("Failed to find [%llu, %u, %llu]\n", key.objectid, key.type, key.offset);
1047                 return -ENOENT;
1048         }
1049
1050         BUG_ON(ret);
1051
1052         leaf = path->nodes[0];
1053         item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1054 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1055         if (item_size < sizeof(*ei)) {
1056                 if (!insert) {
1057                         err = -ENOENT;
1058                         goto out;
1059                 }
1060                 ret = convert_extent_item_v0(trans, root, path, owner,
1061                                              extra_size);
1062                 if (ret < 0) {
1063                         err = ret;
1064                         goto out;
1065                 }
1066                 leaf = path->nodes[0];
1067                 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1068         }
1069 #endif
1070         if (item_size < sizeof(*ei)) {
1071                 printf("Size is %u, needs to be %u, slot %d\n",
1072                        (unsigned)item_size,
1073                        (unsigned)sizeof(*ei), path->slots[0]);
1074                 btrfs_print_leaf(root, leaf);
1075                 return -EINVAL;
1076         }
1077         BUG_ON(item_size < sizeof(*ei));
1078
1079         ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1080         flags = btrfs_extent_flags(leaf, ei);
1081
1082         ptr = (unsigned long)(ei + 1);
1083         end = (unsigned long)ei + item_size;
1084
1085         if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
1086                 ptr += sizeof(struct btrfs_tree_block_info);
1087                 BUG_ON(ptr > end);
1088         } else if (!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
1089                 if (!(flags & BTRFS_EXTENT_FLAG_DATA)) {
1090                         return -EIO;
1091                 }
1092         }
1093
1094         err = -ENOENT;
1095         while (1) {
1096                 if (ptr >= end) {
1097                         WARN_ON(ptr > end);
1098                         break;
1099                 }
1100                 iref = (struct btrfs_extent_inline_ref *)ptr;
1101                 type = btrfs_extent_inline_ref_type(leaf, iref);
1102                 if (want < type)
1103                         break;
1104                 if (want > type) {
1105                         ptr += btrfs_extent_inline_ref_size(type);
1106                         continue;
1107                 }
1108
1109                 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1110                         struct btrfs_extent_data_ref *dref;
1111                         dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1112                         if (match_extent_data_ref(leaf, dref, root_objectid,
1113                                                   owner, offset)) {
1114                                 err = 0;
1115                                 break;
1116                         }
1117                         if (hash_extent_data_ref_item(leaf, dref) <
1118                             hash_extent_data_ref(root_objectid, owner, offset))
1119                                 break;
1120                 } else {
1121                         u64 ref_offset;
1122                         ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1123                         if (parent > 0) {
1124                                 if (parent == ref_offset) {
1125                                         err = 0;
1126                                         break;
1127                                 }
1128                                 if (ref_offset < parent)
1129                                         break;
1130                         } else {
1131                                 if (root_objectid == ref_offset) {
1132                                         err = 0;
1133                                         break;
1134                                 }
1135                                 if (ref_offset < root_objectid)
1136                                         break;
1137                         }
1138                 }
1139                 ptr += btrfs_extent_inline_ref_size(type);
1140         }
1141         if (err == -ENOENT && insert) {
1142                 if (item_size + extra_size >=
1143                     BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1144                         err = -EAGAIN;
1145                         goto out;
1146                 }
1147                 /*
1148                  * To add new inline back ref, we have to make sure
1149                  * there is no corresponding back ref item.
1150                  * For simplicity, we just do not add new inline back
1151                  * ref if there is any back ref item.
1152                  */
1153                 if (find_next_key(path, &key) == 0 && key.objectid == bytenr &&
1154                     key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
1155                         err = -EAGAIN;
1156                         goto out;
1157                 }
1158         }
1159         *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1160 out:
1161         return err;
1162 }
1163
1164 static int setup_inline_extent_backref(struct btrfs_root *root,
1165                                 struct btrfs_path *path,
1166                                 struct btrfs_extent_inline_ref *iref,
1167                                 u64 parent, u64 root_objectid,
1168                                 u64 owner, u64 offset, int refs_to_add)
1169 {
1170         struct extent_buffer *leaf;
1171         struct btrfs_extent_item *ei;
1172         unsigned long ptr;
1173         unsigned long end;
1174         unsigned long item_offset;
1175         u64 refs;
1176         int size;
1177         int type;
1178         int ret;
1179
1180         leaf = path->nodes[0];
1181         ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1182         item_offset = (unsigned long)iref - (unsigned long)ei;
1183
1184         type = extent_ref_type(parent, owner);
1185         size = btrfs_extent_inline_ref_size(type);
1186
1187         ret = btrfs_extend_item(root, path, size);
1188         BUG_ON(ret);
1189
1190         ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1191         refs = btrfs_extent_refs(leaf, ei);
1192         refs += refs_to_add;
1193         btrfs_set_extent_refs(leaf, ei, refs);
1194
1195         ptr = (unsigned long)ei + item_offset;
1196         end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1197         if (ptr < end - size)
1198                 memmove_extent_buffer(leaf, ptr + size, ptr,
1199                                       end - size - ptr);
1200
1201         iref = (struct btrfs_extent_inline_ref *)ptr;
1202         btrfs_set_extent_inline_ref_type(leaf, iref, type);
1203         if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1204                 struct btrfs_extent_data_ref *dref;
1205                 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1206                 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1207                 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1208                 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1209                 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1210         } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1211                 struct btrfs_shared_data_ref *sref;
1212                 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1213                 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1214                 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1215         } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1216                 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1217         } else {
1218                 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1219         }
1220         btrfs_mark_buffer_dirty(leaf);
1221         return 0;
1222 }
1223
1224 static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1225                                  struct btrfs_root *root,
1226                                  struct btrfs_path *path,
1227                                  struct btrfs_extent_inline_ref **ref_ret,
1228                                  u64 bytenr, u64 num_bytes, u64 parent,
1229                                  u64 root_objectid, u64 owner, u64 offset)
1230 {
1231         int ret;
1232
1233         ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1234                                            bytenr, num_bytes, parent,
1235                                            root_objectid, owner, offset, 0);
1236         if (ret != -ENOENT)
1237                 return ret;
1238
1239         btrfs_release_path(path);
1240         *ref_ret = NULL;
1241
1242         if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1243                 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1244                                             root_objectid);
1245         } else {
1246                 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1247                                              root_objectid, owner, offset);
1248         }
1249         return ret;
1250 }
1251
1252 static int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1253                                  struct btrfs_root *root,
1254                                  struct btrfs_path *path,
1255                                  struct btrfs_extent_inline_ref *iref,
1256                                  int refs_to_mod)
1257 {
1258         struct extent_buffer *leaf;
1259         struct btrfs_extent_item *ei;
1260         struct btrfs_extent_data_ref *dref = NULL;
1261         struct btrfs_shared_data_ref *sref = NULL;
1262         unsigned long ptr;
1263         unsigned long end;
1264         u32 item_size;
1265         int size;
1266         int type;
1267         int ret;
1268         u64 refs;
1269
1270         leaf = path->nodes[0];
1271         ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1272         refs = btrfs_extent_refs(leaf, ei);
1273         WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1274         refs += refs_to_mod;
1275         btrfs_set_extent_refs(leaf, ei, refs);
1276
1277         type = btrfs_extent_inline_ref_type(leaf, iref);
1278
1279         if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1280                 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1281                 refs = btrfs_extent_data_ref_count(leaf, dref);
1282         } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1283                 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1284                 refs = btrfs_shared_data_ref_count(leaf, sref);
1285         } else {
1286                 refs = 1;
1287                 BUG_ON(refs_to_mod != -1);
1288         }
1289
1290         BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1291         refs += refs_to_mod;
1292
1293         if (refs > 0) {
1294                 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1295                         btrfs_set_extent_data_ref_count(leaf, dref, refs);
1296                 else
1297                         btrfs_set_shared_data_ref_count(leaf, sref, refs);
1298         } else {
1299                 size =  btrfs_extent_inline_ref_size(type);
1300                 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1301                 ptr = (unsigned long)iref;
1302                 end = (unsigned long)ei + item_size;
1303                 if (ptr + size < end)
1304                         memmove_extent_buffer(leaf, ptr, ptr + size,
1305                                               end - ptr - size);
1306                 item_size -= size;
1307                 ret = btrfs_truncate_item(root, path, item_size, 1);
1308                 BUG_ON(ret);
1309         }
1310         btrfs_mark_buffer_dirty(leaf);
1311         return 0;
1312 }
1313
1314 static int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1315                                  struct btrfs_root *root,
1316                                  struct btrfs_path *path,
1317                                  u64 bytenr, u64 num_bytes, u64 parent,
1318                                  u64 root_objectid, u64 owner,
1319                                  u64 offset, int refs_to_add)
1320 {
1321         struct btrfs_extent_inline_ref *iref;
1322         int ret;
1323
1324         ret = lookup_inline_extent_backref(trans, root, path, &iref,
1325                                            bytenr, num_bytes, parent,
1326                                            root_objectid, owner, offset, 1);
1327         if (ret == 0) {
1328                 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1329                 ret = update_inline_extent_backref(trans, root, path, iref,
1330                                                    refs_to_add);
1331         } else if (ret == -ENOENT) {
1332                 ret = setup_inline_extent_backref(root, path, iref,
1333                                                   parent, root_objectid,
1334                                                   owner, offset, refs_to_add);
1335         }
1336         return ret;
1337 }
1338
1339 static int insert_extent_backref(struct btrfs_trans_handle *trans,
1340                                  struct btrfs_root *root,
1341                                  struct btrfs_path *path,
1342                                  u64 bytenr, u64 parent, u64 root_objectid,
1343                                  u64 owner, u64 offset, int refs_to_add)
1344 {
1345         int ret;
1346
1347         if (owner >= BTRFS_FIRST_FREE_OBJECTID) {
1348                 ret = insert_extent_data_ref(trans, root, path, bytenr,
1349                                              parent, root_objectid,
1350                                              owner, offset, refs_to_add);
1351         } else {
1352                 BUG_ON(refs_to_add != 1);
1353                 ret = insert_tree_block_ref(trans, root, path, bytenr,
1354                                             parent, root_objectid);
1355         }
1356         return ret;
1357 }
1358
1359 static int remove_extent_backref(struct btrfs_trans_handle *trans,
1360                                  struct btrfs_root *root,
1361                                  struct btrfs_path *path,
1362                                  struct btrfs_extent_inline_ref *iref,
1363                                  int refs_to_drop, int is_data)
1364 {
1365         int ret;
1366
1367         BUG_ON(!is_data && refs_to_drop != 1);
1368         if (iref) {
1369                 ret = update_inline_extent_backref(trans, root, path, iref,
1370                                                    -refs_to_drop);
1371         } else if (is_data) {
1372                 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1373         } else {
1374                 ret = btrfs_del_item(trans, root, path);
1375         }
1376         return ret;
1377 }
1378
1379 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1380                          struct btrfs_root *root,
1381                          u64 bytenr, u64 num_bytes, u64 parent,
1382                          u64 root_objectid, u64 owner, u64 offset)
1383 {
1384         struct btrfs_path *path;
1385         struct extent_buffer *leaf;
1386         struct btrfs_extent_item *item;
1387         u64 refs;
1388         int ret;
1389         int err = 0;
1390
1391         path = btrfs_alloc_path();
1392         if (!path)
1393                 return -ENOMEM;
1394
1395         path->reada = 1;
1396
1397         ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1398                                            path, bytenr, num_bytes, parent,
1399                                            root_objectid, owner, offset, 1);
1400         if (ret == 0)
1401                 goto out;
1402
1403         if (ret != -EAGAIN) {
1404                 err = ret;
1405                 goto out;
1406         }
1407         
1408         leaf = path->nodes[0];
1409         item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1410         refs = btrfs_extent_refs(leaf, item);
1411         btrfs_set_extent_refs(leaf, item, refs + 1);
1412
1413         btrfs_mark_buffer_dirty(leaf);
1414         btrfs_release_path(path);
1415
1416         path->reada = 1;
1417
1418         /* now insert the actual backref */
1419         ret = insert_extent_backref(trans, root->fs_info->extent_root,
1420                                     path, bytenr, parent, root_objectid,
1421                                     owner, offset, 1);
1422         if (ret)
1423                 err = ret;
1424 out:
1425         btrfs_free_path(path);
1426         finish_current_insert(trans, root->fs_info->extent_root);
1427         del_pending_extents(trans, root->fs_info->extent_root);
1428         BUG_ON(err);
1429         return err;
1430 }
1431
1432 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
1433                          struct btrfs_root *root)
1434 {
1435         finish_current_insert(trans, root->fs_info->extent_root);
1436         del_pending_extents(trans, root->fs_info->extent_root);
1437         return 0;
1438 }
1439
1440 int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
1441                              struct btrfs_root *root, u64 bytenr,
1442                              u64 offset, int metadata, u64 *refs, u64 *flags)
1443 {
1444         struct btrfs_path *path;
1445         int ret;
1446         struct btrfs_key key;
1447         struct extent_buffer *l;
1448         struct btrfs_extent_item *item;
1449         u32 item_size;
1450         u64 num_refs;
1451         u64 extent_flags;
1452
1453         if (metadata &&
1454             !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA)) {
1455                 offset = root->nodesize;
1456                 metadata = 0;
1457         }
1458
1459         path = btrfs_alloc_path();
1460         if (!path)
1461                 return -ENOMEM;
1462         path->reada = 1;
1463
1464         key.objectid = bytenr;
1465         key.offset = offset;
1466         if (metadata)
1467                 key.type = BTRFS_METADATA_ITEM_KEY;
1468         else
1469                 key.type = BTRFS_EXTENT_ITEM_KEY;
1470
1471 again:
1472         ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1473                                 0, 0);
1474         if (ret < 0)
1475                 goto out;
1476
1477         /*
1478          * Deal with the fact that we may have mixed SKINNY and normal refs.  If
1479          * we didn't find what we wanted check and see if we have a normal ref
1480          * right next to us, or re-search if we are on the edge of the leaf just
1481          * to make sure.
1482          */
1483         if (ret > 0 && metadata) {
1484                 if (path->slots[0]) {
1485                         path->slots[0]--;
1486                         btrfs_item_key_to_cpu(path->nodes[0], &key,
1487                                               path->slots[0]);
1488                         if (key.objectid == bytenr &&
1489                             key.type == BTRFS_EXTENT_ITEM_KEY &&
1490                             key.offset == root->nodesize)
1491                                 ret = 0;
1492                 }
1493
1494                 if (ret) {
1495                         btrfs_release_path(path);
1496                         key.type = BTRFS_EXTENT_ITEM_KEY;
1497                         key.offset = root->nodesize;
1498                         metadata = 0;
1499                         goto again;
1500                 }
1501         }
1502
1503         if (ret != 0) {
1504                 ret = -EIO;
1505                 goto out;
1506         }
1507
1508         l = path->nodes[0];
1509         item_size = btrfs_item_size_nr(l, path->slots[0]);
1510         if (item_size >= sizeof(*item)) {
1511                 item = btrfs_item_ptr(l, path->slots[0],
1512                                       struct btrfs_extent_item);
1513                 num_refs = btrfs_extent_refs(l, item);
1514                 extent_flags = btrfs_extent_flags(l, item);
1515         } else {
1516 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1517                         struct btrfs_extent_item_v0 *ei0;
1518                         BUG_ON(item_size != sizeof(*ei0));
1519                         ei0 = btrfs_item_ptr(l, path->slots[0],
1520                                              struct btrfs_extent_item_v0);
1521                         num_refs = btrfs_extent_refs_v0(l, ei0);
1522                         /* FIXME: this isn't correct for data */
1523                         extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
1524 #else
1525                         BUG();
1526 #endif
1527         }
1528         item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1529         if (refs)
1530                 *refs = num_refs;
1531         if (flags)
1532                 *flags = extent_flags;
1533 out:
1534         btrfs_free_path(path);
1535         return ret;
1536 }
1537
1538 int btrfs_set_block_flags(struct btrfs_trans_handle *trans,
1539                           struct btrfs_root *root,
1540                           u64 bytenr, int level, u64 flags)
1541 {
1542         struct btrfs_path *path;
1543         int ret;
1544         struct btrfs_key key;
1545         struct extent_buffer *l;
1546         struct btrfs_extent_item *item;
1547         u32 item_size;
1548         int skinny_metadata =
1549                 btrfs_fs_incompat(root->fs_info, SKINNY_METADATA);
1550
1551         path = btrfs_alloc_path();
1552         if (!path)
1553                 return -ENOMEM;
1554         path->reada = 1;
1555
1556         key.objectid = bytenr;
1557         if (skinny_metadata) {
1558                 key.offset = level;
1559                 key.type = BTRFS_METADATA_ITEM_KEY;
1560         } else {
1561                 key.offset = root->nodesize;
1562                 key.type = BTRFS_EXTENT_ITEM_KEY;
1563         }
1564
1565 again:
1566         ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1567                                 0, 0);
1568         if (ret < 0)
1569                 goto out;
1570
1571         if (ret > 0 && skinny_metadata) {
1572                 skinny_metadata = 0;
1573                 if (path->slots[0]) {
1574                         path->slots[0]--;
1575                         btrfs_item_key_to_cpu(path->nodes[0], &key,
1576                                               path->slots[0]);
1577                         if (key.objectid == bytenr &&
1578                             key.offset == root->nodesize &&
1579                             key.type == BTRFS_EXTENT_ITEM_KEY)
1580                                 ret = 0;
1581                 }
1582                 if (ret) {
1583                         btrfs_release_path(path);
1584                         key.offset = root->nodesize;
1585                         key.type = BTRFS_EXTENT_ITEM_KEY;
1586                         goto again;
1587                 }
1588         }
1589
1590         if (ret != 0) {
1591                 btrfs_print_leaf(root, path->nodes[0]);
1592                 printk("failed to find block number %Lu\n",
1593                         (unsigned long long)bytenr);
1594                 BUG();
1595         }
1596         l = path->nodes[0];
1597         item_size = btrfs_item_size_nr(l, path->slots[0]);
1598 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1599         if (item_size < sizeof(*item)) {
1600                 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1601                                              path, (u64)-1, 0);
1602                 if (ret < 0)
1603                         goto out;
1604
1605                 l = path->nodes[0];
1606                 item_size = btrfs_item_size_nr(l, path->slots[0]);
1607         }
1608 #endif
1609         BUG_ON(item_size < sizeof(*item));
1610         item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1611         flags |= btrfs_extent_flags(l, item);
1612         btrfs_set_extent_flags(l, item, flags);
1613 out:
1614         btrfs_free_path(path);
1615         finish_current_insert(trans, root->fs_info->extent_root);
1616         del_pending_extents(trans, root->fs_info->extent_root);
1617         return ret;
1618 }
1619
1620 static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
1621                            struct btrfs_root *root,
1622                            struct extent_buffer *buf,
1623                            int record_parent, int inc)
1624 {
1625         u64 bytenr;
1626         u64 num_bytes;
1627         u64 parent;
1628         u64 ref_root;
1629         u32 nritems;
1630         struct btrfs_key key;
1631         struct btrfs_file_extent_item *fi;
1632         int i;
1633         int level;
1634         int ret = 0;
1635         int (*process_func)(struct btrfs_trans_handle *trans,
1636                             struct btrfs_root *root,
1637                             u64, u64, u64, u64, u64, u64);
1638
1639         ref_root = btrfs_header_owner(buf);
1640         nritems = btrfs_header_nritems(buf);
1641         level = btrfs_header_level(buf);
1642
1643         if (!root->ref_cows && level == 0)
1644                 return 0;
1645
1646         if (inc)
1647                 process_func = btrfs_inc_extent_ref;
1648         else
1649                 process_func = btrfs_free_extent;
1650
1651         if (record_parent)
1652                 parent = buf->start;
1653         else
1654                 parent = 0;
1655
1656         for (i = 0; i < nritems; i++) {
1657                 cond_resched();
1658                 if (level == 0) {
1659                         btrfs_item_key_to_cpu(buf, &key, i);
1660                         if (key.type != BTRFS_EXTENT_DATA_KEY)
1661                                 continue;
1662                         fi = btrfs_item_ptr(buf, i,
1663                                             struct btrfs_file_extent_item);
1664                         if (btrfs_file_extent_type(buf, fi) ==
1665                             BTRFS_FILE_EXTENT_INLINE)
1666                                 continue;
1667                         bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1668                         if (bytenr == 0)
1669                                 continue;
1670                         
1671                         num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
1672                         key.offset -= btrfs_file_extent_offset(buf, fi);
1673                         ret = process_func(trans, root, bytenr, num_bytes,
1674                                            parent, ref_root, key.objectid,
1675                                            key.offset);
1676                         if (ret) {
1677                                 WARN_ON(1);
1678                                 goto fail;
1679                         }
1680                 } else {
1681                         bytenr = btrfs_node_blockptr(buf, i);
1682                         num_bytes = root->nodesize;
1683                         ret = process_func(trans, root, bytenr, num_bytes,
1684                                            parent, ref_root, level - 1, 0);
1685                         if (ret) {
1686                                 WARN_ON(1);
1687                                 goto fail;
1688                         }
1689                 }
1690         }
1691         return 0;
1692 fail:
1693         WARN_ON(1);
1694         return ret;
1695 }
1696
1697 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1698                   struct extent_buffer *buf, int record_parent)
1699 {
1700         return __btrfs_mod_ref(trans, root, buf, record_parent, 1);
1701 }
1702
1703 int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1704                   struct extent_buffer *buf, int record_parent)
1705 {
1706         return __btrfs_mod_ref(trans, root, buf, record_parent, 0);
1707 }
1708
1709 static int write_one_cache_group(struct btrfs_trans_handle *trans,
1710                                  struct btrfs_root *root,
1711                                  struct btrfs_path *path,
1712                                  struct btrfs_block_group_cache *cache)
1713 {
1714         int ret;
1715         int pending_ret;
1716         struct btrfs_root *extent_root = root->fs_info->extent_root;
1717         unsigned long bi;
1718         struct extent_buffer *leaf;
1719
1720         ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
1721         if (ret < 0)
1722                 goto fail;
1723         BUG_ON(ret);
1724
1725         leaf = path->nodes[0];
1726         bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1727         write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1728         btrfs_mark_buffer_dirty(leaf);
1729         btrfs_release_path(path);
1730 fail:
1731         finish_current_insert(trans, extent_root);
1732         pending_ret = del_pending_extents(trans, extent_root);
1733         if (ret)
1734                 return ret;
1735         if (pending_ret)
1736                 return pending_ret;
1737         return 0;
1738
1739 }
1740
1741 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1742                                    struct btrfs_root *root)
1743 {
1744         struct extent_io_tree *block_group_cache;
1745         struct btrfs_block_group_cache *cache;
1746         int ret;
1747         struct btrfs_path *path;
1748         u64 last = 0;
1749         u64 start;
1750         u64 end;
1751         u64 ptr;
1752
1753         block_group_cache = &root->fs_info->block_group_cache;
1754         path = btrfs_alloc_path();
1755         if (!path)
1756                 return -ENOMEM;
1757
1758         while(1) {
1759                 ret = find_first_extent_bit(block_group_cache, last,
1760                                             &start, &end, BLOCK_GROUP_DIRTY);
1761                 if (ret) {
1762                         if (last == 0)
1763                                 break;
1764                         last = 0;
1765                         continue;
1766                 }
1767
1768                 last = end + 1;
1769                 ret = get_state_private(block_group_cache, start, &ptr);
1770                 BUG_ON(ret);
1771
1772                 clear_extent_bits(block_group_cache, start, end,
1773                                   BLOCK_GROUP_DIRTY);
1774
1775                 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
1776                 ret = write_one_cache_group(trans, root, path, cache);
1777         }
1778         btrfs_free_path(path);
1779         return 0;
1780 }
1781
1782 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
1783                                                   u64 flags)
1784 {
1785         struct btrfs_space_info *found;
1786
1787         flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
1788
1789         list_for_each_entry(found, &info->space_info, list) {
1790                 if (found->flags & flags)
1791                         return found;
1792         }
1793         return NULL;
1794
1795 }
1796
1797 static int free_space_info(struct btrfs_fs_info *fs_info, u64 flags,
1798                           u64 total_bytes, u64 bytes_used,
1799                           struct btrfs_space_info **space_info)
1800 {
1801         struct btrfs_space_info *found;
1802
1803         /* only support free block group which is empty */
1804         if (bytes_used)
1805                 return -ENOTEMPTY;
1806
1807         found = __find_space_info(fs_info, flags);
1808         if (!found)
1809                 return -ENOENT;
1810         if (found->total_bytes < total_bytes) {
1811                 fprintf(stderr,
1812                         "WARNING: bad space info to free %llu only have %llu\n",
1813                         total_bytes, found->total_bytes);
1814                 return -EINVAL;
1815         }
1816         found->total_bytes -= total_bytes;
1817         if (space_info)
1818                 *space_info = found;
1819         return 0;
1820 }
1821
1822 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1823                              u64 total_bytes, u64 bytes_used,
1824                              struct btrfs_space_info **space_info)
1825 {
1826         struct btrfs_space_info *found;
1827
1828         found = __find_space_info(info, flags);
1829         if (found) {
1830                 found->total_bytes += total_bytes;
1831                 found->bytes_used += bytes_used;
1832                 if (found->total_bytes < found->bytes_used) {
1833                         fprintf(stderr, "warning, bad space info total_bytes "
1834                                 "%llu used %llu\n",
1835                                (unsigned long long)found->total_bytes,
1836                                (unsigned long long)found->bytes_used);
1837                 }
1838                 *space_info = found;
1839                 return 0;
1840         }
1841         found = kmalloc(sizeof(*found), GFP_NOFS);
1842         if (!found)
1843                 return -ENOMEM;
1844
1845         list_add(&found->list, &info->space_info);
1846         found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
1847         found->total_bytes = total_bytes;
1848         found->bytes_used = bytes_used;
1849         found->bytes_pinned = 0;
1850         found->full = 0;
1851         *space_info = found;
1852         return 0;
1853 }
1854
1855
1856 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1857 {
1858         u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
1859                                    BTRFS_BLOCK_GROUP_RAID1 |
1860                                    BTRFS_BLOCK_GROUP_RAID10 |
1861                                    BTRFS_BLOCK_GROUP_RAID5 |
1862                                    BTRFS_BLOCK_GROUP_RAID6 |
1863                                    BTRFS_BLOCK_GROUP_DUP);
1864         if (extra_flags) {
1865                 if (flags & BTRFS_BLOCK_GROUP_DATA)
1866                         fs_info->avail_data_alloc_bits |= extra_flags;
1867                 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1868                         fs_info->avail_metadata_alloc_bits |= extra_flags;
1869                 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1870                         fs_info->avail_system_alloc_bits |= extra_flags;
1871         }
1872 }
1873
1874 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1875                           struct btrfs_root *extent_root, u64 alloc_bytes,
1876                           u64 flags)
1877 {
1878         struct btrfs_space_info *space_info;
1879         u64 thresh;
1880         u64 start;
1881         u64 num_bytes;
1882         int ret;
1883
1884         space_info = __find_space_info(extent_root->fs_info, flags);
1885         if (!space_info) {
1886                 ret = update_space_info(extent_root->fs_info, flags,
1887                                         0, 0, &space_info);
1888                 BUG_ON(ret);
1889         }
1890         BUG_ON(!space_info);
1891
1892         if (space_info->full)
1893                 return 0;
1894
1895         thresh = div_factor(space_info->total_bytes, 7);
1896         if ((space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) <
1897             thresh)
1898                 return 0;
1899
1900         /*
1901          * Avoid allocating given chunk type
1902          */
1903         if (extent_root->fs_info->avoid_meta_chunk_alloc &&
1904             (flags & BTRFS_BLOCK_GROUP_METADATA))
1905                 return 0;
1906         if (extent_root->fs_info->avoid_sys_chunk_alloc &&
1907             (flags & BTRFS_BLOCK_GROUP_SYSTEM))
1908                 return 0;
1909
1910         ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes,
1911                                 space_info->flags);
1912         if (ret == -ENOSPC) {
1913                 space_info->full = 1;
1914                 return 0;
1915         }
1916
1917         BUG_ON(ret);
1918
1919         ret = btrfs_make_block_group(trans, extent_root, 0, space_info->flags,
1920                      BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
1921         BUG_ON(ret);
1922         return 0;
1923 }
1924
1925 static int update_block_group(struct btrfs_trans_handle *trans,
1926                               struct btrfs_root *root,
1927                               u64 bytenr, u64 num_bytes, int alloc,
1928                               int mark_free)
1929 {
1930         struct btrfs_block_group_cache *cache;
1931         struct btrfs_fs_info *info = root->fs_info;
1932         u64 total = num_bytes;
1933         u64 old_val;
1934         u64 byte_in_group;
1935         u64 start;
1936         u64 end;
1937
1938         /* block accounting for super block */
1939         old_val = btrfs_super_bytes_used(info->super_copy);
1940         if (alloc)
1941                 old_val += num_bytes;
1942         else
1943                 old_val -= num_bytes;
1944         btrfs_set_super_bytes_used(info->super_copy, old_val);
1945
1946         /* block accounting for root item */
1947         old_val = btrfs_root_used(&root->root_item);
1948         if (alloc)
1949                 old_val += num_bytes;
1950         else
1951                 old_val -= num_bytes;
1952         btrfs_set_root_used(&root->root_item, old_val);
1953
1954         while(total) {
1955                 cache = btrfs_lookup_block_group(info, bytenr);
1956                 if (!cache) {
1957                         return -1;
1958                 }
1959                 byte_in_group = bytenr - cache->key.objectid;
1960                 WARN_ON(byte_in_group > cache->key.offset);
1961                 start = cache->key.objectid;
1962                 end = start + cache->key.offset - 1;
1963                 set_extent_bits(&info->block_group_cache, start, end,
1964                                 BLOCK_GROUP_DIRTY);
1965
1966                 old_val = btrfs_block_group_used(&cache->item);
1967                 num_bytes = min(total, cache->key.offset - byte_in_group);
1968
1969                 if (alloc) {
1970                         old_val += num_bytes;
1971                         cache->space_info->bytes_used += num_bytes;
1972                 } else {
1973                         old_val -= num_bytes;
1974                         cache->space_info->bytes_used -= num_bytes;
1975                         if (mark_free) {
1976                                 set_extent_dirty(&info->free_space_cache,
1977                                                 bytenr, bytenr + num_bytes - 1);
1978                         }
1979                 }
1980                 btrfs_set_block_group_used(&cache->item, old_val);
1981                 total -= num_bytes;
1982                 bytenr += num_bytes;
1983         }
1984         return 0;
1985 }
1986
1987 static int update_pinned_extents(struct btrfs_root *root,
1988                                 u64 bytenr, u64 num, int pin)
1989 {
1990         u64 len;
1991         struct btrfs_block_group_cache *cache;
1992         struct btrfs_fs_info *fs_info = root->fs_info;
1993
1994         if (pin) {
1995                 set_extent_dirty(&fs_info->pinned_extents,
1996                                 bytenr, bytenr + num - 1);
1997         } else {
1998                 clear_extent_dirty(&fs_info->pinned_extents,
1999                                 bytenr, bytenr + num - 1, GFP_NOFS);
2000         }
2001         while (num > 0) {
2002                 cache = btrfs_lookup_block_group(fs_info, bytenr);
2003                 if (!cache) {
2004                         len = min((u64)root->sectorsize, num);
2005                         goto next;
2006                 }
2007                 WARN_ON(!cache);
2008                 len = min(num, cache->key.offset -
2009                           (bytenr - cache->key.objectid));
2010                 if (pin) {
2011                         cache->pinned += len;
2012                         cache->space_info->bytes_pinned += len;
2013                         fs_info->total_pinned += len;
2014                 } else {
2015                         cache->pinned -= len;
2016                         cache->space_info->bytes_pinned -= len;
2017                         fs_info->total_pinned -= len;
2018                 }
2019 next:
2020                 bytenr += len;
2021                 num -= len;
2022         }
2023         return 0;
2024 }
2025
2026 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2027                                struct btrfs_root *root,
2028                                struct extent_io_tree *unpin)
2029 {
2030         u64 start;
2031         u64 end;
2032         int ret;
2033         struct extent_io_tree *free_space_cache;
2034         free_space_cache = &root->fs_info->free_space_cache;
2035
2036         while(1) {
2037                 ret = find_first_extent_bit(unpin, 0, &start, &end,
2038                                             EXTENT_DIRTY);
2039                 if (ret)
2040                         break;
2041                 update_pinned_extents(root, start, end + 1 - start, 0);
2042                 clear_extent_dirty(unpin, start, end, GFP_NOFS);
2043                 set_extent_dirty(free_space_cache, start, end);
2044         }
2045         return 0;
2046 }
2047
2048 static int extent_root_pending_ops(struct btrfs_fs_info *info)
2049 {
2050         u64 start;
2051         u64 end;
2052         int ret;
2053
2054         ret = find_first_extent_bit(&info->extent_ins, 0, &start,
2055                                     &end, EXTENT_LOCKED);
2056         if (!ret) {
2057                 ret = find_first_extent_bit(&info->pending_del, 0, &start, &end,
2058                                             EXTENT_LOCKED);
2059         }
2060         return ret == 0;
2061
2062 }
2063 static int finish_current_insert(struct btrfs_trans_handle *trans,
2064                                  struct btrfs_root *extent_root)
2065 {
2066         u64 start;
2067         u64 end;
2068         u64 priv;
2069         struct btrfs_fs_info *info = extent_root->fs_info;
2070         struct pending_extent_op *extent_op;
2071         struct btrfs_key key;
2072         int ret;
2073         int skinny_metadata =
2074                 btrfs_fs_incompat(extent_root->fs_info, SKINNY_METADATA);
2075
2076         while(1) {
2077                 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
2078                                             &end, EXTENT_LOCKED);
2079                 if (ret)
2080                         break;
2081
2082                 ret = get_state_private(&info->extent_ins, start, &priv);
2083                 BUG_ON(ret);
2084                 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2085
2086                 if (extent_op->type == PENDING_EXTENT_INSERT) {
2087                         key.objectid = start;
2088                         if (skinny_metadata) {
2089                                 key.offset = extent_op->level;
2090                                 key.type = BTRFS_METADATA_ITEM_KEY;
2091                         } else {
2092                                 key.offset = extent_op->num_bytes;
2093                                 key.type = BTRFS_EXTENT_ITEM_KEY;
2094                         }
2095                         ret = alloc_reserved_tree_block(trans, extent_root,
2096                                                 extent_root->root_key.objectid,
2097                                                 trans->transid,
2098                                                 extent_op->flags,
2099                                                 &extent_op->key,
2100                                                 extent_op->level, &key);
2101                         BUG_ON(ret);
2102                 } else {
2103                         BUG_ON(1);
2104                 }
2105
2106                 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED);
2107                 kfree(extent_op);
2108         }
2109         return 0;
2110 }
2111
2112 static int pin_down_bytes(struct btrfs_trans_handle *trans,
2113                           struct btrfs_root *root,
2114                           u64 bytenr, u64 num_bytes, int is_data)
2115 {
2116         int err = 0;
2117         struct extent_buffer *buf;
2118
2119         if (is_data)
2120                 goto pinit;
2121
2122         buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2123         if (!buf)
2124                 goto pinit;
2125
2126         /* we can reuse a block if it hasn't been written
2127          * and it is from this transaction.  We can't
2128          * reuse anything from the tree log root because
2129          * it has tiny sub-transactions.
2130          */
2131         if (btrfs_buffer_uptodate(buf, 0)) {
2132                 u64 header_owner = btrfs_header_owner(buf);
2133                 u64 header_transid = btrfs_header_generation(buf);
2134                 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
2135                     header_transid == trans->transid &&
2136                     !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2137                         clean_tree_block(NULL, root, buf);
2138                         free_extent_buffer(buf);
2139                         return 1;
2140                 }
2141         }
2142         free_extent_buffer(buf);
2143 pinit:
2144         update_pinned_extents(root, bytenr, num_bytes, 1);
2145
2146         BUG_ON(err < 0);
2147         return 0;
2148 }
2149
2150 void btrfs_pin_extent(struct btrfs_fs_info *fs_info,
2151                        u64 bytenr, u64 num_bytes)
2152 {
2153         update_pinned_extents(fs_info->extent_root, bytenr, num_bytes, 1);
2154 }
2155
2156 void btrfs_unpin_extent(struct btrfs_fs_info *fs_info,
2157                         u64 bytenr, u64 num_bytes)
2158 {
2159         update_pinned_extents(fs_info->extent_root, bytenr, num_bytes, 0);
2160 }
2161
2162 /*
2163  * remove an extent from the root, returns 0 on success
2164  */
2165 static int __free_extent(struct btrfs_trans_handle *trans,
2166                          struct btrfs_root *root,
2167                          u64 bytenr, u64 num_bytes, u64 parent,
2168                          u64 root_objectid, u64 owner_objectid,
2169                          u64 owner_offset, int refs_to_drop)
2170 {
2171
2172         struct btrfs_key key;
2173         struct btrfs_path *path;
2174         struct btrfs_root *extent_root = root->fs_info->extent_root;
2175         struct extent_buffer *leaf;
2176         struct btrfs_extent_item *ei;
2177         struct btrfs_extent_inline_ref *iref;
2178         int ret;
2179         int is_data;
2180         int extent_slot = 0;
2181         int found_extent = 0;
2182         int num_to_del = 1;
2183         u32 item_size;
2184         u64 refs;
2185         int skinny_metadata =
2186                 btrfs_fs_incompat(extent_root->fs_info, SKINNY_METADATA);
2187
2188         if (root->fs_info->free_extent_hook) {
2189                 root->fs_info->free_extent_hook(trans, root, bytenr, num_bytes,
2190                                                 parent, root_objectid, owner_objectid,
2191                                                 owner_offset, refs_to_drop);
2192
2193         }
2194         path = btrfs_alloc_path();
2195         if (!path)
2196                 return -ENOMEM;
2197
2198         path->reada = 1;
2199
2200         is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
2201         if (is_data)
2202                 skinny_metadata = 0;
2203         BUG_ON(!is_data && refs_to_drop != 1);
2204
2205         ret = lookup_extent_backref(trans, extent_root, path, &iref,
2206                                     bytenr, num_bytes, parent,
2207                                     root_objectid, owner_objectid,
2208                                     owner_offset);
2209         if (ret == 0) {
2210                 extent_slot = path->slots[0];
2211                 while (extent_slot >= 0) {
2212                         btrfs_item_key_to_cpu(path->nodes[0], &key,
2213                                               extent_slot);
2214                         if (key.objectid != bytenr)
2215                                 break;
2216                         if (key.type == BTRFS_EXTENT_ITEM_KEY &&
2217                             key.offset == num_bytes) {
2218                                 found_extent = 1;
2219                                 break;
2220                         }
2221                         if (key.type == BTRFS_METADATA_ITEM_KEY &&
2222                             key.offset == owner_objectid) {
2223                                 found_extent = 1;
2224                                 break;
2225                         }
2226                         if (path->slots[0] - extent_slot > 5)
2227                                 break;
2228                         extent_slot--;
2229                 }
2230 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2231                 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
2232                 if (found_extent && item_size < sizeof(*ei))
2233                         found_extent = 0;
2234 #endif
2235                 if (!found_extent) {
2236                         BUG_ON(iref);
2237                         ret = remove_extent_backref(trans, extent_root, path,
2238                                                     NULL, refs_to_drop,
2239                                                     is_data);
2240                         BUG_ON(ret);
2241                         btrfs_release_path(path);
2242
2243                         key.objectid = bytenr;
2244
2245                         if (skinny_metadata) {
2246                                 key.type = BTRFS_METADATA_ITEM_KEY;
2247                                 key.offset = owner_objectid;
2248                         } else {
2249                                 key.type = BTRFS_EXTENT_ITEM_KEY;
2250                                 key.offset = num_bytes;
2251                         }
2252
2253                         ret = btrfs_search_slot(trans, extent_root,
2254                                                 &key, path, -1, 1);
2255                         if (ret > 0 && skinny_metadata && path->slots[0]) {
2256                                 path->slots[0]--;
2257                                 btrfs_item_key_to_cpu(path->nodes[0],
2258                                                       &key,
2259                                                       path->slots[0]);
2260                                 if (key.objectid == bytenr &&
2261                                     key.type == BTRFS_EXTENT_ITEM_KEY &&
2262                                     key.offset == num_bytes)
2263                                         ret = 0;
2264                         }
2265
2266                         if (ret > 0 && skinny_metadata) {
2267                                 skinny_metadata = 0;
2268                                 btrfs_release_path(path);
2269                                 key.type = BTRFS_EXTENT_ITEM_KEY;
2270                                 key.offset = num_bytes;
2271                                 ret = btrfs_search_slot(trans, extent_root,
2272                                                         &key, path, -1, 1);
2273                         }
2274
2275                         if (ret) {
2276                                 printk(KERN_ERR "umm, got %d back from search"
2277                                        ", was looking for %llu\n", ret,
2278                                        (unsigned long long)bytenr);
2279                                 btrfs_print_leaf(extent_root, path->nodes[0]);
2280                         }
2281                         BUG_ON(ret);
2282                         extent_slot = path->slots[0];
2283                 }
2284         } else {
2285                 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
2286                        "parent %llu root %llu  owner %llu offset %llu\n",
2287                        (unsigned long long)bytenr,
2288                        (unsigned long long)parent,
2289                        (unsigned long long)root_objectid,
2290                        (unsigned long long)owner_objectid,
2291                        (unsigned long long)owner_offset);
2292                 ret = -EIO;
2293                 goto fail;
2294         }
2295
2296         leaf = path->nodes[0];
2297         item_size = btrfs_item_size_nr(leaf, extent_slot);
2298 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2299         if (item_size < sizeof(*ei)) {
2300                 BUG_ON(found_extent || extent_slot != path->slots[0]);
2301                 ret = convert_extent_item_v0(trans, extent_root, path,
2302                                              owner_objectid, 0);
2303                 BUG_ON(ret < 0);
2304
2305                 btrfs_release_path(path);
2306
2307                 key.objectid = bytenr;
2308                 key.type = BTRFS_EXTENT_ITEM_KEY;
2309                 key.offset = num_bytes;
2310
2311                 ret = btrfs_search_slot(trans, extent_root, &key, path,
2312                                         -1, 1);
2313                 if (ret) {
2314                         printk(KERN_ERR "umm, got %d back from search"
2315                                ", was looking for %llu\n", ret,
2316                                (unsigned long long)bytenr);
2317                         btrfs_print_leaf(extent_root, path->nodes[0]);
2318                 }
2319                 BUG_ON(ret);
2320                 extent_slot = path->slots[0];
2321                 leaf = path->nodes[0];
2322                 item_size = btrfs_item_size_nr(leaf, extent_slot);
2323         }
2324 #endif
2325         BUG_ON(item_size < sizeof(*ei));
2326         ei = btrfs_item_ptr(leaf, extent_slot,
2327                             struct btrfs_extent_item);
2328         if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
2329             key.type == BTRFS_EXTENT_ITEM_KEY) {
2330                 struct btrfs_tree_block_info *bi;
2331                 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
2332                 bi = (struct btrfs_tree_block_info *)(ei + 1);
2333                 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
2334         }
2335
2336         refs = btrfs_extent_refs(leaf, ei);
2337         BUG_ON(refs < refs_to_drop);
2338         refs -= refs_to_drop;
2339
2340         if (refs > 0) {
2341                 /*
2342                  * In the case of inline back ref, reference count will
2343                  * be updated by remove_extent_backref
2344                  */
2345                 if (iref) {
2346                         BUG_ON(!found_extent);
2347                 } else {
2348                         btrfs_set_extent_refs(leaf, ei, refs);
2349                         btrfs_mark_buffer_dirty(leaf);
2350                 }
2351                 if (found_extent) {
2352                         ret = remove_extent_backref(trans, extent_root, path,
2353                                                     iref, refs_to_drop,
2354                                                     is_data);
2355                         BUG_ON(ret);
2356                 }
2357         } else {
2358                 int mark_free = 0;
2359                 int pin = 1;
2360
2361                 if (found_extent) {
2362                         BUG_ON(is_data && refs_to_drop !=
2363                                extent_data_ref_count(path, iref));
2364                         if (iref) {
2365                                 BUG_ON(path->slots[0] != extent_slot);
2366                         } else {
2367                                 BUG_ON(path->slots[0] != extent_slot + 1);
2368                                 path->slots[0] = extent_slot;
2369                                 num_to_del = 2;
2370                         }
2371                 }
2372
2373                 if (pin) {
2374                         ret = pin_down_bytes(trans, root, bytenr, num_bytes,
2375                                              is_data);
2376                         if (ret > 0)
2377                                 mark_free = 1;
2378                         BUG_ON(ret < 0);
2379                 }
2380
2381                 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2382                                       num_to_del);
2383                 BUG_ON(ret);
2384                 btrfs_release_path(path);
2385
2386                 if (is_data) {
2387                         ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
2388                         BUG_ON(ret);
2389                 }
2390
2391                 update_block_group(trans, root, bytenr, num_bytes, 0, mark_free);
2392         }
2393 fail:
2394         btrfs_free_path(path);
2395         finish_current_insert(trans, extent_root);
2396         return ret;
2397 }
2398
2399 /*
2400  * find all the blocks marked as pending in the radix tree and remove
2401  * them from the extent map
2402  */
2403 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
2404                                btrfs_root *extent_root)
2405 {
2406         int ret;
2407         int err = 0;
2408         u64 start;
2409         u64 end;
2410         u64 priv;
2411         struct extent_io_tree *pending_del;
2412         struct extent_io_tree *extent_ins;
2413         struct pending_extent_op *extent_op;
2414
2415         extent_ins = &extent_root->fs_info->extent_ins;
2416         pending_del = &extent_root->fs_info->pending_del;
2417
2418         while(1) {
2419                 ret = find_first_extent_bit(pending_del, 0, &start, &end,
2420                                             EXTENT_LOCKED);
2421                 if (ret)
2422                         break;
2423
2424                 ret = get_state_private(pending_del, start, &priv);
2425                 BUG_ON(ret);
2426                 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2427
2428                 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED);
2429
2430                 if (!test_range_bit(extent_ins, start, end,
2431                                     EXTENT_LOCKED, 0)) {
2432                         ret = __free_extent(trans, extent_root,
2433                                             start, end + 1 - start, 0,
2434                                             extent_root->root_key.objectid,
2435                                             extent_op->level, 0, 1);
2436                         kfree(extent_op);
2437                 } else {
2438                         kfree(extent_op);
2439                         ret = get_state_private(extent_ins, start, &priv);
2440                         BUG_ON(ret);
2441                         extent_op = (struct pending_extent_op *)
2442                                                         (unsigned long)priv;
2443
2444                         clear_extent_bits(extent_ins, start, end,
2445                                           EXTENT_LOCKED);
2446
2447                         if (extent_op->type == PENDING_BACKREF_UPDATE)
2448                                 BUG_ON(1);
2449
2450                         kfree(extent_op);
2451                 }
2452                 if (ret)
2453                         err = ret;
2454         }
2455         return err;
2456 }
2457
2458
2459 int btrfs_free_tree_block(struct btrfs_trans_handle *trans,
2460                           struct btrfs_root *root,
2461                           struct extent_buffer *buf,
2462                           u64 parent, int last_ref)
2463 {
2464         return btrfs_free_extent(trans, root, buf->start, buf->len, parent,
2465                                  root->root_key.objectid,
2466                                  btrfs_header_level(buf), 0);
2467 }
2468
2469 /*
2470  * remove an extent from the root, returns 0 on success
2471  */
2472
2473 int btrfs_free_extent(struct btrfs_trans_handle *trans,
2474                       struct btrfs_root *root,
2475                       u64 bytenr, u64 num_bytes, u64 parent,
2476                       u64 root_objectid, u64 owner, u64 offset)
2477 {
2478         struct btrfs_root *extent_root = root->fs_info->extent_root;
2479         int pending_ret;
2480         int ret;
2481
2482         WARN_ON(num_bytes < root->sectorsize);
2483         if (root == extent_root) {
2484                 struct pending_extent_op *extent_op;
2485
2486                 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2487                 BUG_ON(!extent_op);
2488
2489                 extent_op->type = PENDING_EXTENT_DELETE;
2490                 extent_op->bytenr = bytenr;
2491                 extent_op->num_bytes = num_bytes;
2492                 extent_op->level = (int)owner;
2493
2494                 set_extent_bits(&root->fs_info->pending_del,
2495                                 bytenr, bytenr + num_bytes - 1,
2496                                 EXTENT_LOCKED);
2497                 set_state_private(&root->fs_info->pending_del,
2498                                   bytenr, (unsigned long)extent_op);
2499                 return 0;
2500         }
2501         ret = __free_extent(trans, root, bytenr, num_bytes, parent,
2502                             root_objectid, owner, offset, 1);
2503         pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
2504         return ret ? ret : pending_ret;
2505 }
2506
2507 static u64 stripe_align(struct btrfs_root *root, u64 val)
2508 {
2509         u64 mask = ((u64)root->stripesize - 1);
2510         u64 ret = (val + mask) & ~mask;
2511         return ret;
2512 }
2513
2514 /*
2515  * walks the btree of allocated extents and find a hole of a given size.
2516  * The key ins is changed to record the hole:
2517  * ins->objectid == block start
2518  * ins->flags = BTRFS_EXTENT_ITEM_KEY
2519  * ins->offset == number of blocks
2520  * Any available blocks before search_start are skipped.
2521  */
2522 static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2523                                      struct btrfs_root *orig_root,
2524                                      u64 num_bytes, u64 empty_size,
2525                                      u64 search_start, u64 search_end,
2526                                      u64 hint_byte, struct btrfs_key *ins,
2527                                      u64 exclude_start, u64 exclude_nr,
2528                                      int data)
2529 {
2530         int ret;
2531         u64 orig_search_start = search_start;
2532         struct btrfs_root * root = orig_root->fs_info->extent_root;
2533         struct btrfs_fs_info *info = root->fs_info;
2534         u64 total_needed = num_bytes;
2535         struct btrfs_block_group_cache *block_group;
2536         int full_scan = 0;
2537         int wrapped = 0;
2538
2539         WARN_ON(num_bytes < root->sectorsize);
2540         ins->type = BTRFS_EXTENT_ITEM_KEY;
2541
2542         search_start = stripe_align(root, search_start);
2543
2544         if (hint_byte) {
2545                 block_group = btrfs_lookup_first_block_group(info, hint_byte);
2546                 if (!block_group)
2547                         hint_byte = search_start;
2548                 block_group = btrfs_find_block_group(root, block_group,
2549                                                      hint_byte, data, 1);
2550         } else {
2551                 block_group = btrfs_find_block_group(root,
2552                                                      trans->block_group,
2553                                                      search_start, data, 1);
2554         }
2555
2556         total_needed += empty_size;
2557
2558 check_failed:
2559         search_start = stripe_align(root, search_start);
2560         if (!block_group) {
2561                 block_group = btrfs_lookup_first_block_group(info,
2562                                                              search_start);
2563                 if (!block_group)
2564                         block_group = btrfs_lookup_first_block_group(info,
2565                                                        orig_search_start);
2566         }
2567         ret = find_search_start(root, &block_group, &search_start,
2568                                 total_needed, data);
2569         if (ret)
2570                 goto new_group;
2571
2572         ins->objectid = search_start;
2573         ins->offset = num_bytes;
2574
2575         if (ins->objectid + num_bytes >
2576             block_group->key.objectid + block_group->key.offset) {
2577                 search_start = block_group->key.objectid +
2578                         block_group->key.offset;
2579                 goto new_group;
2580         }
2581
2582         if (test_range_bit(&info->extent_ins, ins->objectid,
2583                            ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
2584                 search_start = ins->objectid + num_bytes;
2585                 goto new_group;
2586         }
2587
2588         if (test_range_bit(&info->pinned_extents, ins->objectid,
2589                            ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
2590                 search_start = ins->objectid + num_bytes;
2591                 goto new_group;
2592         }
2593
2594         if (info->excluded_extents &&
2595             test_range_bit(info->excluded_extents, ins->objectid,
2596                            ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
2597                 search_start = ins->objectid + num_bytes;
2598                 goto new_group;
2599         }
2600
2601         if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
2602             ins->objectid < exclude_start + exclude_nr)) {
2603                 search_start = exclude_start + exclude_nr;
2604                 goto new_group;
2605         }
2606
2607         if (!(data & BTRFS_BLOCK_GROUP_DATA)) {
2608                 if (check_crossing_stripes(info, ins->objectid, num_bytes)) {
2609                         struct btrfs_block_group_cache *bg_cache;
2610                         u64 bg_offset;
2611
2612                         bg_cache = btrfs_lookup_block_group(info, ins->objectid);
2613                         if (!bg_cache)
2614                                 goto no_bg_cache;
2615                         bg_offset = ins->objectid - bg_cache->key.objectid;
2616
2617                         search_start = round_up(bg_offset + num_bytes,
2618                                                 BTRFS_STRIPE_LEN) + bg_offset;
2619                         goto new_group;
2620                 }
2621 no_bg_cache:
2622                 block_group = btrfs_lookup_block_group(info, ins->objectid);
2623                 if (block_group)
2624                         trans->block_group = block_group;
2625         }
2626         ins->offset = num_bytes;
2627         return 0;
2628
2629 new_group:
2630         block_group = btrfs_lookup_first_block_group(info, search_start);
2631         if (!block_group) {
2632                 search_start = orig_search_start;
2633                 if (full_scan) {
2634                         ret = -ENOSPC;
2635                         goto error;
2636                 }
2637                 if (wrapped) {
2638                         if (!full_scan)
2639                                 total_needed -= empty_size;
2640                         full_scan = 1;
2641                 } else
2642                         wrapped = 1;
2643         }
2644         cond_resched();
2645         block_group = btrfs_find_block_group(root, block_group,
2646                                              search_start, data, 0);
2647         goto check_failed;
2648
2649 error:
2650         return ret;
2651 }
2652
2653 int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2654                          struct btrfs_root *root,
2655                          u64 num_bytes, u64 empty_size,
2656                          u64 hint_byte, u64 search_end,
2657                          struct btrfs_key *ins, int data)
2658 {
2659         int ret;
2660         u64 search_start = 0;
2661         u64 alloc_profile;
2662         struct btrfs_fs_info *info = root->fs_info;
2663
2664         if (data) {
2665                 alloc_profile = info->avail_data_alloc_bits &
2666                                 info->data_alloc_profile;
2667                 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
2668         } else if ((info->system_allocs > 0 || root == info->chunk_root) &&
2669                    info->system_allocs >= 0) {
2670                 alloc_profile = info->avail_system_alloc_bits &
2671                                 info->system_alloc_profile;
2672                 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
2673         } else {
2674                 alloc_profile = info->avail_metadata_alloc_bits &
2675                                 info->metadata_alloc_profile;
2676                 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
2677         }
2678
2679         if (root->ref_cows) {
2680                 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
2681                         ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2682                                              num_bytes,
2683                                              BTRFS_BLOCK_GROUP_METADATA);
2684                         BUG_ON(ret);
2685                 }
2686                 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2687                                      num_bytes + SZ_2M, data);
2688                 BUG_ON(ret);
2689         }
2690
2691         WARN_ON(num_bytes < root->sectorsize);
2692         ret = find_free_extent(trans, root, num_bytes, empty_size,
2693                                search_start, search_end, hint_byte, ins,
2694                                trans->alloc_exclude_start,
2695                                trans->alloc_exclude_nr, data);
2696         BUG_ON(ret);
2697         clear_extent_dirty(&root->fs_info->free_space_cache,
2698                            ins->objectid, ins->objectid + ins->offset - 1,
2699                            GFP_NOFS);
2700         return ret;
2701 }
2702
2703 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
2704                                      struct btrfs_root *root,
2705                                      u64 root_objectid, u64 generation,
2706                                      u64 flags, struct btrfs_disk_key *key,
2707                                      int level, struct btrfs_key *ins)
2708 {
2709         int ret;
2710         struct btrfs_fs_info *fs_info = root->fs_info;
2711         struct btrfs_extent_item *extent_item;
2712         struct btrfs_tree_block_info *block_info;
2713         struct btrfs_extent_inline_ref *iref;
2714         struct btrfs_path *path;
2715         struct extent_buffer *leaf;
2716         u32 size = sizeof(*extent_item) + sizeof(*iref);
2717         int skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
2718
2719         if (!skinny_metadata)
2720                 size += sizeof(*block_info);
2721
2722         path = btrfs_alloc_path();
2723         if (!path)
2724                 return -ENOMEM;
2725
2726         ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
2727                                       ins, size);
2728         BUG_ON(ret);
2729
2730         leaf = path->nodes[0];
2731         extent_item = btrfs_item_ptr(leaf, path->slots[0],
2732                                      struct btrfs_extent_item);
2733         btrfs_set_extent_refs(leaf, extent_item, 1);
2734         btrfs_set_extent_generation(leaf, extent_item, generation);
2735         btrfs_set_extent_flags(leaf, extent_item,
2736                                flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
2737
2738         if (skinny_metadata) {
2739                 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
2740         } else {
2741                 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
2742                 btrfs_set_tree_block_key(leaf, block_info, key);
2743                 btrfs_set_tree_block_level(leaf, block_info, level);
2744                 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
2745         }
2746
2747         btrfs_set_extent_inline_ref_type(leaf, iref, BTRFS_TREE_BLOCK_REF_KEY);
2748         btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
2749
2750         btrfs_mark_buffer_dirty(leaf);
2751         btrfs_free_path(path);
2752
2753         ret = update_block_group(trans, root, ins->objectid, root->nodesize,
2754                                  1, 0);
2755         return ret;
2756 }
2757
2758 static int alloc_tree_block(struct btrfs_trans_handle *trans,
2759                             struct btrfs_root *root, u64 num_bytes,
2760                             u64 root_objectid, u64 generation,
2761                             u64 flags, struct btrfs_disk_key *key,
2762                             int level, u64 empty_size, u64 hint_byte,
2763                             u64 search_end, struct btrfs_key *ins)
2764 {
2765         int ret;
2766         ret = btrfs_reserve_extent(trans, root, num_bytes, empty_size,
2767                                    hint_byte, search_end, ins, 0);
2768         BUG_ON(ret);
2769
2770         if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID) {
2771                 struct pending_extent_op *extent_op;
2772
2773                 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2774                 BUG_ON(!extent_op);
2775
2776                 extent_op->type = PENDING_EXTENT_INSERT;
2777                 extent_op->bytenr = ins->objectid;
2778                 extent_op->num_bytes = ins->offset;
2779                 extent_op->level = level;
2780                 extent_op->flags = flags;
2781                 memcpy(&extent_op->key, key, sizeof(*key));
2782
2783                 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
2784                                 ins->objectid + ins->offset - 1,
2785                                 EXTENT_LOCKED);
2786                 set_state_private(&root->fs_info->extent_ins,
2787                                   ins->objectid, (unsigned long)extent_op);
2788         } else {
2789                 if (btrfs_fs_incompat(root->fs_info, SKINNY_METADATA)) {
2790                         ins->offset = level;
2791                         ins->type = BTRFS_METADATA_ITEM_KEY;
2792                 }
2793                 ret = alloc_reserved_tree_block(trans, root, root_objectid,
2794                                                 generation, flags,
2795                                                 key, level, ins);
2796                 finish_current_insert(trans, root->fs_info->extent_root);
2797                 del_pending_extents(trans, root->fs_info->extent_root);
2798         }
2799         return ret;
2800 }
2801
2802 /*
2803  * helper function to allocate a block for a given tree
2804  * returns the tree buffer or NULL.
2805  */
2806 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
2807                                         struct btrfs_root *root,
2808                                         u32 blocksize, u64 root_objectid,
2809                                         struct btrfs_disk_key *key, int level,
2810                                         u64 hint, u64 empty_size)
2811 {
2812         struct btrfs_key ins;
2813         int ret;
2814         struct extent_buffer *buf;
2815
2816         ret = alloc_tree_block(trans, root, blocksize, root_objectid,
2817                                trans->transid, 0, key, level,
2818                                empty_size, hint, (u64)-1, &ins);
2819         if (ret) {
2820                 BUG_ON(ret > 0);
2821                 return ERR_PTR(ret);
2822         }
2823
2824         buf = btrfs_find_create_tree_block(root->fs_info, ins.objectid,
2825                                            blocksize);
2826         if (!buf) {
2827                 btrfs_free_extent(trans, root, ins.objectid, ins.offset,
2828                                   0, root->root_key.objectid, level, 0);
2829                 BUG_ON(1);
2830                 return ERR_PTR(-ENOMEM);
2831         }
2832         btrfs_set_buffer_uptodate(buf);
2833         trans->blocks_used++;
2834
2835         return buf;
2836 }
2837
2838 #if 0
2839
2840 static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
2841                                   struct btrfs_root *root,
2842                                   struct extent_buffer *leaf)
2843 {
2844         u64 leaf_owner;
2845         u64 leaf_generation;
2846         struct btrfs_key key;
2847         struct btrfs_file_extent_item *fi;
2848         int i;
2849         int nritems;
2850         int ret;
2851
2852         BUG_ON(!btrfs_is_leaf(leaf));
2853         nritems = btrfs_header_nritems(leaf);
2854         leaf_owner = btrfs_header_owner(leaf);
2855         leaf_generation = btrfs_header_generation(leaf);
2856
2857         for (i = 0; i < nritems; i++) {
2858                 u64 disk_bytenr;
2859
2860                 btrfs_item_key_to_cpu(leaf, &key, i);
2861                 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2862                         continue;
2863                 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
2864                 if (btrfs_file_extent_type(leaf, fi) ==
2865                     BTRFS_FILE_EXTENT_INLINE)
2866                         continue;
2867                 /*
2868                  * FIXME make sure to insert a trans record that
2869                  * repeats the snapshot del on crash
2870                  */
2871                 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2872                 if (disk_bytenr == 0)
2873                         continue;
2874                 ret = btrfs_free_extent(trans, root, disk_bytenr,
2875                                 btrfs_file_extent_disk_num_bytes(leaf, fi),
2876                                 leaf->start, leaf_owner, leaf_generation,
2877                                 key.objectid, 0);
2878                 BUG_ON(ret);
2879         }
2880         return 0;
2881 }
2882
2883 static void noinline reada_walk_down(struct btrfs_root *root,
2884                                      struct extent_buffer *node,
2885                                      int slot)
2886 {
2887         u64 bytenr;
2888         u64 last = 0;
2889         u32 nritems;
2890         u32 refs;
2891         u32 blocksize;
2892         int ret;
2893         int i;
2894         int level;
2895         int skipped = 0;
2896
2897         nritems = btrfs_header_nritems(node);
2898         level = btrfs_header_level(node);
2899         if (level)
2900                 return;
2901
2902         for (i = slot; i < nritems && skipped < 32; i++) {
2903                 bytenr = btrfs_node_blockptr(node, i);
2904                 if (last && ((bytenr > last && bytenr - last > SZ_32K) ||
2905                              (last > bytenr && last - bytenr > SZ_32K))) {
2906                         skipped++;
2907                         continue;
2908                 }
2909                 blocksize = btrfs_level_size(root, level - 1);
2910                 if (i != slot) {
2911                         ret = btrfs_lookup_extent_ref(NULL, root, bytenr,
2912                                                       blocksize, &refs);
2913                         BUG_ON(ret);
2914                         if (refs != 1) {
2915                                 skipped++;
2916                                 continue;
2917                         }
2918                 }
2919                 mutex_unlock(&root->fs_info->fs_mutex);
2920                 ret = readahead_tree_block(root, bytenr, blocksize,
2921                                            btrfs_node_ptr_generation(node, i));
2922                 last = bytenr + blocksize;
2923                 cond_resched();
2924                 mutex_lock(&root->fs_info->fs_mutex);
2925                 if (ret)
2926                         break;
2927         }
2928 }
2929
2930 /*
2931  * helper function for drop_snapshot, this walks down the tree dropping ref
2932  * counts as it goes.
2933  */
2934 static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2935                                    struct btrfs_root *root,
2936                                    struct btrfs_path *path, int *level)
2937 {
2938         u64 root_owner;
2939         u64 root_gen;
2940         u64 bytenr;
2941         u64 ptr_gen;
2942         struct extent_buffer *next;
2943         struct extent_buffer *cur;
2944         struct extent_buffer *parent;
2945         u32 blocksize;
2946         int ret;
2947         u32 refs;
2948
2949         WARN_ON(*level < 0);
2950         WARN_ON(*level >= BTRFS_MAX_LEVEL);
2951         ret = btrfs_lookup_extent_ref(trans, root,
2952                                       path->nodes[*level]->start,
2953                                       path->nodes[*level]->len, &refs);
2954         BUG_ON(ret);
2955         if (refs > 1)
2956                 goto out;
2957
2958         /*
2959          * walk down to the last node level and free all the leaves
2960          */
2961         while(*level >= 0) {
2962                 WARN_ON(*level < 0);
2963                 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2964                 cur = path->nodes[*level];
2965
2966                 if (btrfs_header_level(cur) != *level)
2967                         WARN_ON(1);
2968
2969                 if (path->slots[*level] >=
2970                     btrfs_header_nritems(cur))
2971                         break;
2972                 if (*level == 0) {
2973                         ret = drop_leaf_ref(trans, root, cur);
2974                         BUG_ON(ret);
2975                         break;
2976                 }
2977                 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2978                 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2979                 blocksize = btrfs_level_size(root, *level - 1);
2980                 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
2981                                               &refs);
2982                 BUG_ON(ret);
2983                 if (refs != 1) {
2984                         parent = path->nodes[*level];
2985                         root_owner = btrfs_header_owner(parent);
2986                         root_gen = btrfs_header_generation(parent);
2987                         path->slots[*level]++;
2988                         ret = btrfs_free_extent(trans, root, bytenr, blocksize,
2989                                                 parent->start, root_owner,
2990                                                 root_gen, *level - 1, 1);
2991                         BUG_ON(ret);
2992                         continue;
2993                 }
2994                 next = btrfs_find_tree_block(root, bytenr, blocksize);
2995                 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
2996                         free_extent_buffer(next);
2997                         reada_walk_down(root, cur, path->slots[*level]);
2998                         mutex_unlock(&root->fs_info->fs_mutex);
2999                         next = read_tree_block(root, bytenr, blocksize,
3000                                                ptr_gen);
3001                         mutex_lock(&root->fs_info->fs_mutex);
3002                         if (!extent_buffer_uptodate(next)) {
3003                                 if (IS_ERR(next))
3004                                         ret = PTR_ERR(next);
3005                                 else
3006                                         ret = -EIO;
3007                                 break;
3008                         }
3009                 }
3010                 WARN_ON(*level <= 0);
3011                 if (path->nodes[*level-1])
3012                         free_extent_buffer(path->nodes[*level-1]);
3013                 path->nodes[*level-1] = next;
3014                 *level = btrfs_header_level(next);
3015                 path->slots[*level] = 0;
3016         }
3017 out:
3018         WARN_ON(*level < 0);
3019         WARN_ON(*level >= BTRFS_MAX_LEVEL);
3020
3021         if (path->nodes[*level] == root->node) {
3022                 root_owner = root->root_key.objectid;
3023                 parent = path->nodes[*level];
3024         } else {
3025                 parent = path->nodes[*level + 1];
3026                 root_owner = btrfs_header_owner(parent);
3027         }
3028
3029         root_gen = btrfs_header_generation(parent);
3030         ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
3031                                 path->nodes[*level]->len, parent->start,
3032                                 root_owner, root_gen, *level, 1);
3033         free_extent_buffer(path->nodes[*level]);
3034         path->nodes[*level] = NULL;
3035         *level += 1;
3036         BUG_ON(ret);
3037         return 0;
3038 }
3039
3040 /*
3041  * helper for dropping snapshots.  This walks back up the tree in the path
3042  * to find the first node higher up where we haven't yet gone through
3043  * all the slots
3044  */
3045 static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
3046                                  struct btrfs_root *root,
3047                                  struct btrfs_path *path, int *level)
3048 {
3049         u64 root_owner;
3050         u64 root_gen;
3051         struct btrfs_root_item *root_item = &root->root_item;
3052         int i;
3053         int slot;
3054         int ret;
3055
3056         for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
3057                 slot = path->slots[i];
3058                 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3059                         struct extent_buffer *node;
3060                         struct btrfs_disk_key disk_key;
3061                         node = path->nodes[i];
3062                         path->slots[i]++;
3063                         *level = i;
3064                         WARN_ON(*level == 0);
3065                         btrfs_node_key(node, &disk_key, path->slots[i]);
3066                         memcpy(&root_item->drop_progress,
3067                                &disk_key, sizeof(disk_key));
3068                         root_item->drop_level = i;
3069                         return 0;
3070                 } else {
3071                         struct extent_buffer *parent;
3072                         if (path->nodes[*level] == root->node)
3073                                 parent = path->nodes[*level];
3074                         else
3075                                 parent = path->nodes[*level + 1];
3076
3077                         root_owner = btrfs_header_owner(parent);
3078                         root_gen = btrfs_header_generation(parent);
3079                         ret = btrfs_free_extent(trans, root,
3080                                                 path->nodes[*level]->start,
3081                                                 path->nodes[*level]->len,
3082                                                 parent->start, root_owner,
3083                                                 root_gen, *level, 1);
3084                         BUG_ON(ret);
3085                         free_extent_buffer(path->nodes[*level]);
3086                         path->nodes[*level] = NULL;
3087                         *level = i + 1;
3088                 }
3089         }
3090         return 1;
3091 }
3092
3093 #endif
3094
3095 int btrfs_free_block_groups(struct btrfs_fs_info *info)
3096 {
3097         struct btrfs_space_info *sinfo;
3098         struct btrfs_block_group_cache *cache;
3099         u64 start;
3100         u64 end;
3101         u64 ptr;
3102         int ret;
3103
3104         while(1) {
3105                 ret = find_first_extent_bit(&info->block_group_cache, 0,
3106                                             &start, &end, (unsigned int)-1);
3107                 if (ret)
3108                         break;
3109                 ret = get_state_private(&info->block_group_cache, start, &ptr);
3110                 if (!ret) {
3111                         cache = u64_to_ptr(ptr);
3112                         if (cache->free_space_ctl) {
3113                                 btrfs_remove_free_space_cache(cache);
3114                                 kfree(cache->free_space_ctl);
3115                         }
3116                         kfree(cache);
3117                 }
3118                 clear_extent_bits(&info->block_group_cache, start,
3119                                   end, (unsigned int)-1);
3120         }
3121         while(1) {
3122                 ret = find_first_extent_bit(&info->free_space_cache, 0,
3123                                             &start, &end, EXTENT_DIRTY);
3124                 if (ret)
3125                         break;
3126                 clear_extent_dirty(&info->free_space_cache, start,
3127                                    end, GFP_NOFS);
3128         }
3129
3130         while (!list_empty(&info->space_info)) {
3131                 sinfo = list_entry(info->space_info.next,
3132                                    struct btrfs_space_info, list);
3133                 list_del_init(&sinfo->list);
3134                 kfree(sinfo);
3135         }
3136         return 0;
3137 }
3138
3139 static int find_first_block_group(struct btrfs_root *root,
3140                 struct btrfs_path *path, struct btrfs_key *key)
3141 {
3142         int ret;
3143         struct btrfs_key found_key;
3144         struct extent_buffer *leaf;
3145         int slot;
3146
3147         ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
3148         if (ret < 0)
3149                 return ret;
3150         while(1) {
3151                 slot = path->slots[0];
3152                 leaf = path->nodes[0];
3153                 if (slot >= btrfs_header_nritems(leaf)) {
3154                         ret = btrfs_next_leaf(root, path);
3155                         if (ret == 0)
3156                                 continue;
3157                         if (ret < 0)
3158                                 goto error;
3159                         break;
3160                 }
3161                 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3162
3163                 if (found_key.objectid >= key->objectid &&
3164                     found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
3165                         return 0;
3166                 path->slots[0]++;
3167         }
3168         ret = -ENOENT;
3169 error:
3170         return ret;
3171 }
3172
3173 static void account_super_bytes(struct btrfs_fs_info *fs_info,
3174                                 struct btrfs_block_group_cache *cache)
3175 {
3176         u64 bytenr;
3177         u64 *logical;
3178         int stripe_len;
3179         int i, nr, ret;
3180
3181         if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
3182                 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
3183                 cache->bytes_super += stripe_len;
3184         }
3185
3186         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3187                 bytenr = btrfs_sb_offset(i);
3188                 ret = btrfs_rmap_block(&fs_info->mapping_tree,
3189                                        cache->key.objectid, bytenr,
3190                                        0, &logical, &nr, &stripe_len);
3191                 if (ret)
3192                         return;
3193
3194                 while (nr--) {
3195                         u64 start, len;
3196
3197                         if (logical[nr] > cache->key.objectid +
3198                             cache->key.offset)
3199                                 continue;
3200
3201                         if (logical[nr] + stripe_len <= cache->key.objectid)
3202                                 continue;
3203
3204                         start = logical[nr];
3205                         if (start < cache->key.objectid) {
3206                                 start = cache->key.objectid;
3207                                 len = (logical[nr] + stripe_len) - start;
3208                         } else {
3209                                 len = min_t(u64, stripe_len,
3210                                             cache->key.objectid +
3211                                             cache->key.offset - start);
3212                         }
3213
3214                         cache->bytes_super += len;
3215                 }
3216
3217                 kfree(logical);
3218         }
3219 }
3220
3221 int btrfs_read_block_groups(struct btrfs_root *root)
3222 {
3223         struct btrfs_path *path;
3224         int ret;
3225         int bit;
3226         struct btrfs_block_group_cache *cache;
3227         struct btrfs_fs_info *info = root->fs_info;
3228         struct btrfs_space_info *space_info;
3229         struct extent_io_tree *block_group_cache;
3230         struct btrfs_key key;
3231         struct btrfs_key found_key;
3232         struct extent_buffer *leaf;
3233
3234         block_group_cache = &info->block_group_cache;
3235
3236         root = info->extent_root;
3237         key.objectid = 0;
3238         key.offset = 0;
3239         key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
3240         path = btrfs_alloc_path();
3241         if (!path)
3242                 return -ENOMEM;
3243
3244         while(1) {
3245                 ret = find_first_block_group(root, path, &key);
3246                 if (ret > 0) {
3247                         ret = 0;
3248                         goto error;
3249                 }
3250                 if (ret != 0) {
3251                         goto error;
3252                 }
3253                 leaf = path->nodes[0];
3254                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3255                 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3256                 if (!cache) {
3257                         ret = -ENOMEM;
3258                         goto error;
3259                 }
3260
3261                 read_extent_buffer(leaf, &cache->item,
3262                                    btrfs_item_ptr_offset(leaf, path->slots[0]),
3263                                    sizeof(cache->item));
3264                 memcpy(&cache->key, &found_key, sizeof(found_key));
3265                 cache->cached = 0;
3266                 cache->pinned = 0;
3267                 key.objectid = found_key.objectid + found_key.offset;
3268                 if (found_key.offset == 0)
3269                         key.objectid++;
3270                 btrfs_release_path(path);
3271                 cache->flags = btrfs_block_group_flags(&cache->item);
3272                 bit = 0;
3273                 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
3274                         bit = BLOCK_GROUP_DATA;
3275                 } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
3276                         bit = BLOCK_GROUP_SYSTEM;
3277                 } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) {
3278                         bit = BLOCK_GROUP_METADATA;
3279                 }
3280                 set_avail_alloc_bits(info, cache->flags);
3281                 if (btrfs_chunk_readonly(root, cache->key.objectid))
3282                         cache->ro = 1;
3283
3284                 account_super_bytes(info, cache);
3285
3286                 ret = update_space_info(info, cache->flags, found_key.offset,
3287                                         btrfs_block_group_used(&cache->item),
3288                                         &space_info);
3289                 BUG_ON(ret);
3290                 cache->space_info = space_info;
3291
3292                 /* use EXTENT_LOCKED to prevent merging */
3293                 set_extent_bits(block_group_cache, found_key.objectid,
3294                                 found_key.objectid + found_key.offset - 1,
3295                                 bit | EXTENT_LOCKED);
3296                 set_state_private(block_group_cache, found_key.objectid,
3297                                   (unsigned long)cache);
3298         }
3299         ret = 0;
3300 error:
3301         btrfs_free_path(path);
3302         return ret;
3303 }
3304
3305 struct btrfs_block_group_cache *
3306 btrfs_add_block_group(struct btrfs_fs_info *fs_info, u64 bytes_used, u64 type,
3307                       u64 chunk_objectid, u64 chunk_offset, u64 size)
3308 {
3309         int ret;
3310         int bit = 0;
3311         struct btrfs_block_group_cache *cache;
3312         struct extent_io_tree *block_group_cache;
3313
3314         block_group_cache = &fs_info->block_group_cache;
3315
3316         cache = kzalloc(sizeof(*cache), GFP_NOFS);
3317         BUG_ON(!cache);
3318         cache->key.objectid = chunk_offset;
3319         cache->key.offset = size;
3320
3321         cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
3322         btrfs_set_block_group_used(&cache->item, bytes_used);
3323         btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
3324         cache->flags = type;
3325         btrfs_set_block_group_flags(&cache->item, type);
3326
3327         account_super_bytes(fs_info, cache);
3328         ret = update_space_info(fs_info, cache->flags, size, bytes_used,
3329                                 &cache->space_info);
3330         BUG_ON(ret);
3331
3332         bit = block_group_state_bits(type);
3333         ret = set_extent_bits(block_group_cache, chunk_offset,
3334                               chunk_offset + size - 1,
3335                               bit | EXTENT_LOCKED);
3336         BUG_ON(ret);
3337
3338         ret = set_state_private(block_group_cache, chunk_offset,
3339                                 (unsigned long)cache);
3340         BUG_ON(ret);
3341         set_avail_alloc_bits(fs_info, type);
3342
3343         return cache;
3344 }
3345
3346 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
3347                            struct btrfs_root *root, u64 bytes_used,
3348                            u64 type, u64 chunk_objectid, u64 chunk_offset,
3349                            u64 size)
3350 {
3351         int ret;
3352         struct btrfs_root *extent_root;
3353         struct btrfs_block_group_cache *cache;
3354
3355         cache = btrfs_add_block_group(root->fs_info, bytes_used, type,
3356                                       chunk_objectid, chunk_offset, size);
3357         extent_root = root->fs_info->extent_root;
3358         ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
3359                                 sizeof(cache->item));
3360         BUG_ON(ret);
3361
3362         ret = finish_current_insert(trans, extent_root);
3363         BUG_ON(ret);
3364         ret = del_pending_extents(trans, extent_root);
3365         BUG_ON(ret);
3366
3367         return 0;
3368 }
3369
3370 /*
3371  * This is for converter use only.
3372  *
3373  * In that case, we don't know where are free blocks located.
3374  * Therefore all block group cache entries must be setup properly
3375  * before doing any block allocation.
3376  */
3377 int btrfs_make_block_groups(struct btrfs_trans_handle *trans,
3378                             struct btrfs_root *root)
3379 {
3380         u64 total_bytes;
3381         u64 cur_start;
3382         u64 group_type;
3383         u64 group_size;
3384         u64 group_align;
3385         u64 total_data = 0;
3386         u64 total_metadata = 0;
3387         u64 chunk_objectid;
3388         int ret;
3389         int bit;
3390         struct btrfs_root *extent_root;
3391         struct btrfs_block_group_cache *cache;
3392         struct extent_io_tree *block_group_cache;
3393
3394         extent_root = root->fs_info->extent_root;
3395         block_group_cache = &root->fs_info->block_group_cache;
3396         chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3397         total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
3398         group_align = 64 * root->sectorsize;
3399
3400         cur_start = 0;
3401         while (cur_start < total_bytes) {
3402                 group_size = total_bytes / 12;
3403                 group_size = min_t(u64, group_size, total_bytes - cur_start);
3404                 if (cur_start == 0) {
3405                         bit = BLOCK_GROUP_SYSTEM;
3406                         group_type = BTRFS_BLOCK_GROUP_SYSTEM;
3407                         group_size /= 4;
3408                         group_size &= ~(group_align - 1);
3409                         group_size = max_t(u64, group_size, SZ_8M);
3410                         group_size = min_t(u64, group_size, SZ_32M);
3411                 } else {
3412                         group_size &= ~(group_align - 1);
3413                         if (total_data >= total_metadata * 2) {
3414                                 group_type = BTRFS_BLOCK_GROUP_METADATA;
3415                                 group_size = min_t(u64, group_size, SZ_1G);
3416                                 total_metadata += group_size;
3417                         } else {
3418                                 group_type = BTRFS_BLOCK_GROUP_DATA;
3419                                 group_size = min_t(u64, group_size,
3420                                                    5ULL * SZ_1G);
3421                                 total_data += group_size;
3422                         }
3423                         if ((total_bytes - cur_start) * 4 < group_size * 5)
3424                                 group_size = total_bytes - cur_start;
3425                 }
3426
3427                 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3428                 BUG_ON(!cache);
3429
3430                 cache->key.objectid = cur_start;
3431                 cache->key.offset = group_size;
3432                 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
3433
3434                 btrfs_set_block_group_used(&cache->item, 0);
3435                 btrfs_set_block_group_chunk_objectid(&cache->item,
3436                                                      chunk_objectid);
3437                 btrfs_set_block_group_flags(&cache->item, group_type);
3438
3439                 cache->flags = group_type;
3440
3441                 ret = update_space_info(root->fs_info, group_type, group_size,
3442                                         0, &cache->space_info);
3443                 BUG_ON(ret);
3444                 set_avail_alloc_bits(extent_root->fs_info, group_type);
3445
3446                 set_extent_bits(block_group_cache, cur_start,
3447                                 cur_start + group_size - 1,
3448                                 bit | EXTENT_LOCKED);
3449                 set_state_private(block_group_cache, cur_start,
3450                                   (unsigned long)cache);
3451                 cur_start += group_size;
3452         }
3453         /* then insert all the items */
3454         cur_start = 0;
3455         while(cur_start < total_bytes) {
3456                 cache = btrfs_lookup_block_group(root->fs_info, cur_start);
3457                 BUG_ON(!cache);
3458
3459                 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
3460                                         sizeof(cache->item));
3461                 BUG_ON(ret);
3462
3463                 finish_current_insert(trans, extent_root);
3464                 ret = del_pending_extents(trans, extent_root);
3465                 BUG_ON(ret);
3466
3467                 cur_start = cache->key.objectid + cache->key.offset;
3468         }
3469         return 0;
3470 }
3471
3472 int btrfs_update_block_group(struct btrfs_trans_handle *trans,
3473                              struct btrfs_root *root,
3474                              u64 bytenr, u64 num_bytes, int alloc,
3475                              int mark_free)
3476 {
3477         return update_block_group(trans, root, bytenr, num_bytes,
3478                                   alloc, mark_free);
3479 }
3480
3481 /*
3482  * Just remove a block group item in extent tree
3483  * Caller should ensure the block group is empty and all space is pinned.
3484  * Or new tree block/data may be allocated into it.
3485  */
3486 static int free_block_group_item(struct btrfs_trans_handle *trans,
3487                                  struct btrfs_fs_info *fs_info,
3488                                  u64 bytenr, u64 len)
3489 {
3490         struct btrfs_path *path;
3491         struct btrfs_key key;
3492         struct btrfs_root *root = fs_info->extent_root;
3493         int ret = 0;
3494
3495         key.objectid = bytenr;
3496         key.offset = len;
3497         key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
3498
3499         path = btrfs_alloc_path();
3500         if (!path)
3501                 return -ENOMEM;
3502
3503         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3504         if (ret > 0) {
3505                 ret = -ENOENT;
3506                 goto out;
3507         }
3508         if (ret < 0)
3509                 goto out;
3510
3511         ret = btrfs_del_item(trans, root, path);
3512 out:
3513         btrfs_free_path(path);
3514         return ret;
3515 }
3516
3517 static int free_dev_extent_item(struct btrfs_trans_handle *trans,
3518                                 struct btrfs_fs_info *fs_info,
3519                                 u64 devid, u64 dev_offset)
3520 {
3521         struct btrfs_root *root = fs_info->dev_root;
3522         struct btrfs_path *path;
3523         struct btrfs_key key;
3524         int ret;
3525
3526         path = btrfs_alloc_path();
3527         if (!path)
3528                 return -ENOMEM;
3529
3530         key.objectid = devid;
3531         key.type = BTRFS_DEV_EXTENT_KEY;
3532         key.offset = dev_offset;
3533
3534         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3535         if (ret < 0)
3536                 goto out;
3537         if (ret > 0) {
3538                 ret = -ENOENT;
3539                 goto out;
3540         }
3541
3542         ret = btrfs_del_item(trans, root, path);
3543 out:
3544         btrfs_free_path(path);
3545         return ret;
3546 }
3547
3548 static int free_chunk_dev_extent_items(struct btrfs_trans_handle *trans,
3549                                        struct btrfs_fs_info *fs_info,
3550                                        u64 chunk_offset)
3551 {
3552         struct btrfs_chunk *chunk = NULL;
3553         struct btrfs_root *root= fs_info->chunk_root;
3554         struct btrfs_path *path;
3555         struct btrfs_key key;
3556         u16 num_stripes;
3557         int i;
3558         int ret;
3559
3560         path = btrfs_alloc_path();
3561         if (!path)
3562                 return -ENOMEM;
3563
3564         key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3565         key.type = BTRFS_CHUNK_ITEM_KEY;
3566         key.offset = chunk_offset;
3567
3568         ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
3569         if (ret < 0)
3570                 goto out;
3571         if (ret > 0) {
3572                 ret = -ENOENT;
3573                 goto out;
3574         }
3575         chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
3576                                struct btrfs_chunk);
3577         num_stripes = btrfs_chunk_num_stripes(path->nodes[0], chunk);
3578         for (i = 0; i < num_stripes; i++) {
3579                 ret = free_dev_extent_item(trans, fs_info,
3580                         btrfs_stripe_devid_nr(path->nodes[0], chunk, i),
3581                         btrfs_stripe_offset_nr(path->nodes[0], chunk, i));
3582                 if (ret < 0)
3583                         goto out;
3584         }
3585 out:
3586         btrfs_free_path(path);
3587         return ret;
3588 }
3589
3590 static int free_system_chunk_item(struct btrfs_super_block *super,
3591                                   struct btrfs_key *key)
3592 {
3593         struct btrfs_disk_key *disk_key;
3594         struct btrfs_key cpu_key;
3595         u32 array_size = btrfs_super_sys_array_size(super);
3596         char *ptr = (char *)super->sys_chunk_array;
3597         int cur = 0;
3598         int ret = -ENOENT;
3599
3600         while (cur < btrfs_super_sys_array_size(super)) {
3601                 struct btrfs_chunk *chunk;
3602                 u32 num_stripes;
3603                 u32 chunk_len;
3604
3605                 disk_key = (struct btrfs_disk_key *)(ptr + cur);
3606                 btrfs_disk_key_to_cpu(&cpu_key, disk_key);
3607                 if (cpu_key.type != BTRFS_CHUNK_ITEM_KEY) {
3608                         /* just in case */
3609                         ret = -EIO;
3610                         goto out;
3611                 }
3612
3613                 chunk = (struct btrfs_chunk *)(ptr + cur + sizeof(*disk_key));
3614                 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
3615                 chunk_len = btrfs_chunk_item_size(num_stripes) +
3616                             sizeof(*disk_key);
3617
3618                 if (key->objectid == cpu_key.objectid &&
3619                     key->offset == cpu_key.offset &&
3620                     key->type == cpu_key.type) {
3621                         memmove(ptr + cur, ptr + cur + chunk_len,
3622                                 array_size - cur - chunk_len);
3623                         array_size -= chunk_len;
3624                         btrfs_set_super_sys_array_size(super, array_size);
3625                         ret = 0;
3626                         goto out;
3627                 }
3628
3629                 cur += chunk_len;
3630         }
3631 out:
3632         return ret;
3633 }
3634
3635 static int free_chunk_item(struct btrfs_trans_handle *trans,
3636                            struct btrfs_fs_info *fs_info,
3637                            u64 bytenr)
3638 {
3639         struct btrfs_path *path;
3640         struct btrfs_key key;
3641         struct btrfs_root *root = fs_info->chunk_root;
3642         struct btrfs_chunk *chunk;
3643         u64 chunk_type;
3644         int ret;
3645
3646         key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3647         key.offset = bytenr;
3648         key.type = BTRFS_CHUNK_ITEM_KEY;
3649
3650         path = btrfs_alloc_path();
3651         if (!path)
3652                 return -ENOMEM;
3653
3654         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3655         if (ret > 0) {
3656                 ret = -ENOENT;
3657                 goto out;
3658         }
3659         if (ret < 0)
3660                 goto out;
3661         chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
3662                                struct btrfs_chunk);
3663         chunk_type = btrfs_chunk_type(path->nodes[0], chunk);
3664
3665         ret = btrfs_del_item(trans, root, path);
3666         if (ret < 0)
3667                 goto out;
3668
3669         if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3670                 ret = free_system_chunk_item(fs_info->super_copy, &key);
3671 out:
3672         btrfs_free_path(path);
3673         return ret;
3674 }
3675
3676 static u64 get_dev_extent_len(struct map_lookup *map)
3677 {
3678         int div;
3679
3680         switch (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
3681         case 0: /* Single */
3682         case BTRFS_BLOCK_GROUP_DUP:
3683         case BTRFS_BLOCK_GROUP_RAID1:
3684                 div = 1;
3685                 break;
3686         case BTRFS_BLOCK_GROUP_RAID5:
3687                 div = (map->num_stripes - 1);
3688                 break;
3689         case BTRFS_BLOCK_GROUP_RAID6:
3690                 div = (map->num_stripes - 2);
3691                 break;
3692         case BTRFS_BLOCK_GROUP_RAID10:
3693                 div = (map->num_stripes / map->sub_stripes);
3694                 break;
3695         default:
3696                 /* normally, read chunk security hook should handled it */
3697                 BUG_ON(1);
3698         }
3699         return map->ce.size / div;
3700 }
3701
3702 /* free block group/chunk related caches */
3703 static int free_block_group_cache(struct btrfs_trans_handle *trans,
3704                                   struct btrfs_fs_info *fs_info,
3705                                   u64 bytenr, u64 len)
3706 {
3707         struct btrfs_block_group_cache *cache;
3708         struct cache_extent *ce;
3709         struct map_lookup *map;
3710         int ret;
3711         int i;
3712         u64 flags;
3713
3714         /* Free block group cache first */
3715         cache = btrfs_lookup_block_group(fs_info, bytenr);
3716         if (!cache)
3717                 return -ENOENT;
3718         flags = cache->flags;
3719         if (cache->free_space_ctl) {
3720                 btrfs_remove_free_space_cache(cache);
3721                 kfree(cache->free_space_ctl);
3722         }
3723         clear_extent_bits(&fs_info->block_group_cache, bytenr, bytenr + len,
3724                           (unsigned int)-1);
3725         ret = free_space_info(fs_info, flags, len, 0, NULL);
3726         if (ret < 0)
3727                 goto out;
3728         kfree(cache);
3729
3730         /* Then free mapping info and dev usage info */
3731         ce = search_cache_extent(&fs_info->mapping_tree.cache_tree, bytenr);
3732         if (!ce || ce->start != bytenr) {
3733                 ret = -ENOENT;
3734                 goto out;
3735         }
3736         map = container_of(ce, struct map_lookup, ce);
3737         for (i = 0; i < map->num_stripes; i++) {
3738                 struct btrfs_device *device;
3739
3740                 device = map->stripes[i].dev;
3741                 device->bytes_used -= get_dev_extent_len(map);
3742                 ret = btrfs_update_device(trans, device);
3743                 if (ret < 0)
3744                         goto out;
3745         }
3746         remove_cache_extent(&fs_info->mapping_tree.cache_tree, ce);
3747         free(map);
3748 out:
3749         return ret;
3750 }
3751
3752 int btrfs_free_block_group(struct btrfs_trans_handle *trans,
3753                            struct btrfs_fs_info *fs_info, u64 bytenr, u64 len)
3754 {
3755         struct btrfs_root *extent_root = fs_info->extent_root;
3756         struct btrfs_path *path;
3757         struct btrfs_block_group_item *bgi;
3758         struct btrfs_key key;
3759         int ret = 0;
3760
3761         path = btrfs_alloc_path();
3762         if (!path)
3763                 return -ENOMEM;
3764
3765         key.objectid = bytenr;
3766         key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
3767         key.offset = len;
3768
3769         /* Double check the block group to ensure it's empty */
3770         ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
3771         if (ret > 0) {
3772                 ret = -ENONET;
3773                 goto out;
3774         }
3775         if (ret < 0)
3776                 goto out;
3777
3778         bgi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3779                              struct btrfs_block_group_item);
3780         if (btrfs_disk_block_group_used(path->nodes[0], bgi)) {
3781                 fprintf(stderr,
3782                         "WARNING: block group [%llu,%llu) is not empty\n",
3783                         bytenr, bytenr + len);
3784                 ret = -EINVAL;
3785                 goto out;
3786         }
3787         btrfs_release_path(path);
3788
3789         /*
3790          * Now pin all space in the block group, to prevent further transaction
3791          * allocate space from it.
3792          * Every operation needs a transaction must be in the range.
3793          */
3794         btrfs_pin_extent(fs_info, bytenr, len);
3795
3796         /* delete block group item and chunk item */
3797         ret = free_block_group_item(trans, fs_info, bytenr, len);
3798         if (ret < 0) {
3799                 fprintf(stderr,
3800                         "failed to free block group item for [%llu,%llu)\n",
3801                         bytenr, bytenr + len);
3802                 btrfs_unpin_extent(fs_info, bytenr, len);
3803                 goto out;
3804         }
3805
3806         ret = free_chunk_dev_extent_items(trans, fs_info, bytenr);
3807         if (ret < 0) {
3808                 fprintf(stderr,
3809                         "failed to dev extents belongs to [%llu,%llu)\n",
3810                         bytenr, bytenr + len);
3811                 btrfs_unpin_extent(fs_info, bytenr, len);
3812                 goto out;
3813         }
3814         ret = free_chunk_item(trans, fs_info, bytenr);
3815         if (ret < 0) {
3816                 fprintf(stderr,
3817                         "failed to free chunk for [%llu,%llu)\n",
3818                         bytenr, bytenr + len);
3819                 btrfs_unpin_extent(fs_info, bytenr, len);
3820                 goto out;
3821         }
3822
3823         /* Now release the block_group_cache */
3824         ret = free_block_group_cache(trans, fs_info, bytenr, len);
3825         btrfs_unpin_extent(fs_info, bytenr, len);
3826
3827 out:
3828         btrfs_free_path(path);
3829         return ret;
3830 }
3831
3832 /*
3833  * Fixup block accounting. The initial block accounting created by
3834  * make_block_groups isn't accuracy in this case.
3835  */
3836 int btrfs_fix_block_accounting(struct btrfs_trans_handle *trans,
3837                                struct btrfs_root *root)
3838 {
3839         int ret;
3840         int slot;
3841         u64 start = 0;
3842         u64 bytes_used = 0;
3843         struct btrfs_path path;
3844         struct btrfs_key key;
3845         struct extent_buffer *leaf;
3846         struct btrfs_block_group_cache *cache;
3847         struct btrfs_fs_info *fs_info = root->fs_info;
3848
3849         root = root->fs_info->extent_root;
3850
3851         while(extent_root_pending_ops(fs_info)) {
3852                 ret = finish_current_insert(trans, root);
3853                 if (ret)
3854                         return ret;
3855                 ret = del_pending_extents(trans, root);
3856                 if (ret)
3857                         return ret;
3858         }
3859
3860         while(1) {
3861                 cache = btrfs_lookup_first_block_group(fs_info, start);
3862                 if (!cache)
3863                         break;
3864                 start = cache->key.objectid + cache->key.offset;
3865                 btrfs_set_block_group_used(&cache->item, 0);
3866                 cache->space_info->bytes_used = 0;
3867                 set_extent_bits(&root->fs_info->block_group_cache,
3868                                 cache->key.objectid,
3869                                 cache->key.objectid + cache->key.offset -1,
3870                                 BLOCK_GROUP_DIRTY);
3871         }
3872
3873         btrfs_init_path(&path);
3874         key.offset = 0;
3875         key.objectid = 0;
3876         key.type = BTRFS_EXTENT_ITEM_KEY;
3877         ret = btrfs_search_slot(trans, root->fs_info->extent_root,
3878                                 &key, &path, 0, 0);
3879         if (ret < 0)
3880                 return ret;
3881         while(1) {
3882                 leaf = path.nodes[0];
3883                 slot = path.slots[0];
3884                 if (slot >= btrfs_header_nritems(leaf)) {
3885                         ret = btrfs_next_leaf(root, &path);
3886                         if (ret < 0)
3887                                 return ret;
3888                         if (ret > 0)
3889                                 break;
3890                         leaf = path.nodes[0];
3891                         slot = path.slots[0];
3892                 }
3893                 btrfs_item_key_to_cpu(leaf, &key, slot);
3894                 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
3895                         bytes_used += key.offset;
3896                         ret = btrfs_update_block_group(trans, root,
3897                                   key.objectid, key.offset, 1, 0);
3898                         BUG_ON(ret);
3899                 } else if (key.type == BTRFS_METADATA_ITEM_KEY) {
3900                         bytes_used += root->nodesize;
3901                         ret = btrfs_update_block_group(trans, root,
3902                                   key.objectid, root->nodesize, 1, 0);
3903                         BUG_ON(ret);
3904                 }
3905                 path.slots[0]++;
3906         }
3907         btrfs_set_super_bytes_used(root->fs_info->super_copy, bytes_used);
3908         btrfs_release_path(&path);
3909         return 0;
3910 }
3911
3912 static void __get_extent_size(struct btrfs_root *root, struct btrfs_path *path,
3913                               u64 *start, u64 *len)
3914 {
3915         struct btrfs_key key;
3916
3917         btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3918         BUG_ON(!(key.type == BTRFS_EXTENT_ITEM_KEY ||
3919                  key.type == BTRFS_METADATA_ITEM_KEY));
3920         *start = key.objectid;
3921         if (key.type == BTRFS_EXTENT_ITEM_KEY)
3922                 *len = key.offset;
3923         else
3924                 *len = root->nodesize;
3925 }
3926
3927 /*
3928  * Find first overlap extent for range [bytenr, bytenr + len)
3929  * Return 0 for found and point path to it.
3930  * Return >0 for not found.
3931  * Return <0 for err
3932  */
3933 int btrfs_search_overlap_extent(struct btrfs_root *root,
3934                                 struct btrfs_path *path, u64 bytenr, u64 len)
3935 {
3936         struct btrfs_key key;
3937         u64 cur_start;
3938         u64 cur_len;
3939         int ret;
3940
3941         key.objectid = bytenr;
3942         key.type = BTRFS_EXTENT_DATA_KEY;
3943         key.offset = (u64)-1;
3944
3945         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3946         if (ret < 0)
3947                 return ret;
3948         BUG_ON(ret == 0);
3949
3950         ret = btrfs_previous_extent_item(root, path, 0);
3951         if (ret < 0)
3952                 return ret;
3953         /* no previous, check next extent */
3954         if (ret > 0)
3955                 goto next;
3956         __get_extent_size(root, path, &cur_start, &cur_len);
3957         /* Tail overlap */
3958         if (cur_start + cur_len > bytenr)
3959                 return 1;
3960
3961 next:
3962         ret = btrfs_next_extent_item(root, path, bytenr + len);
3963         if (ret < 0)
3964                 return ret;
3965         /* No next, prev already checked, no overlap */
3966         if (ret > 0)
3967                 return 0;
3968         __get_extent_size(root, path, &cur_start, &cur_len);
3969         /* head overlap*/
3970         if (cur_start < bytenr + len)
3971                 return 1;
3972         return 0;
3973 }
3974
3975 static int __btrfs_record_file_extent(struct btrfs_trans_handle *trans,
3976                                       struct btrfs_root *root, u64 objectid,
3977                                       struct btrfs_inode_item *inode,
3978                                       u64 file_pos, u64 disk_bytenr,
3979                                       u64 *ret_num_bytes)
3980 {
3981         int ret;
3982         struct btrfs_fs_info *info = root->fs_info;
3983         struct btrfs_root *extent_root = info->extent_root;
3984         struct extent_buffer *leaf;
3985         struct btrfs_file_extent_item *fi;
3986         struct btrfs_key ins_key;
3987         struct btrfs_path *path;
3988         struct btrfs_extent_item *ei;
3989         u64 nbytes;
3990         u64 extent_num_bytes;
3991         u64 extent_bytenr;
3992         u64 extent_offset;
3993         u64 num_bytes = *ret_num_bytes;
3994
3995         /*
3996          * All supported file system should not use its 0 extent.
3997          * As it's for hole
3998          *
3999          * And hole extent has no size limit, no need to loop.
4000          */
4001         if (disk_bytenr == 0) {
4002                 ret = btrfs_insert_file_extent(trans, root, objectid,
4003                                                 file_pos, disk_bytenr,
4004                                                 num_bytes, num_bytes);
4005                 return ret;
4006         }
4007         num_bytes = min_t(u64, num_bytes, BTRFS_MAX_EXTENT_SIZE);
4008
4009         path = btrfs_alloc_path();
4010         if (!path)
4011                 return -ENOMEM;
4012
4013         /* First to check extent overlap */
4014         ret = btrfs_search_overlap_extent(extent_root, path, disk_bytenr,
4015                                           num_bytes);
4016         if (ret < 0)
4017                 goto fail;
4018         if (ret > 0) {
4019                 /* Found overlap */
4020                 u64 cur_start;
4021                 u64 cur_len;
4022
4023                 __get_extent_size(extent_root, path, &cur_start, &cur_len);
4024                 /*
4025                  * For convert case, this extent should be a subset of
4026                  * existing one.
4027                  */
4028                 BUG_ON(disk_bytenr < cur_start);
4029
4030                 extent_bytenr = cur_start;
4031                 extent_num_bytes = cur_len;
4032                 extent_offset = disk_bytenr - extent_bytenr;
4033         } else {
4034                 /* No overlap, create new extent */
4035                 btrfs_release_path(path);
4036                 ins_key.objectid = disk_bytenr;
4037                 ins_key.offset = num_bytes;
4038                 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
4039
4040                 ret = btrfs_insert_empty_item(trans, extent_root, path,
4041                                               &ins_key, sizeof(*ei));
4042                 if (ret == 0) {
4043                         leaf = path->nodes[0];
4044                         ei = btrfs_item_ptr(leaf, path->slots[0],
4045                                             struct btrfs_extent_item);
4046
4047                         btrfs_set_extent_refs(leaf, ei, 0);
4048                         btrfs_set_extent_generation(leaf, ei, 0);
4049                         btrfs_set_extent_flags(leaf, ei,
4050                                                BTRFS_EXTENT_FLAG_DATA);
4051                         btrfs_mark_buffer_dirty(leaf);
4052
4053                         ret = btrfs_update_block_group(trans, root, disk_bytenr,
4054                                                        num_bytes, 1, 0);
4055                         if (ret)
4056                                 goto fail;
4057                 } else if (ret != -EEXIST) {
4058                         goto fail;
4059                 }
4060                 btrfs_extent_post_op(trans, extent_root);
4061                 extent_bytenr = disk_bytenr;
4062                 extent_num_bytes = num_bytes;
4063                 extent_offset = 0;
4064         }
4065         btrfs_release_path(path);
4066         ins_key.objectid = objectid;
4067         ins_key.offset = file_pos;
4068         ins_key.type = BTRFS_EXTENT_DATA_KEY;
4069         ret = btrfs_insert_empty_item(trans, root, path, &ins_key,
4070                                       sizeof(*fi));
4071         if (ret)
4072                 goto fail;
4073         leaf = path->nodes[0];
4074         fi = btrfs_item_ptr(leaf, path->slots[0],
4075                             struct btrfs_file_extent_item);
4076         btrfs_set_file_extent_generation(leaf, fi, trans->transid);
4077         btrfs_set_file_extent_type(leaf, fi, BTRFS_FILE_EXTENT_REG);
4078         btrfs_set_file_extent_disk_bytenr(leaf, fi, extent_bytenr);
4079         btrfs_set_file_extent_disk_num_bytes(leaf, fi, extent_num_bytes);
4080         btrfs_set_file_extent_offset(leaf, fi, extent_offset);
4081         btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
4082         btrfs_set_file_extent_ram_bytes(leaf, fi, extent_num_bytes);
4083         btrfs_set_file_extent_compression(leaf, fi, 0);
4084         btrfs_set_file_extent_encryption(leaf, fi, 0);
4085         btrfs_set_file_extent_other_encoding(leaf, fi, 0);
4086         btrfs_mark_buffer_dirty(leaf);
4087
4088         nbytes = btrfs_stack_inode_nbytes(inode) + num_bytes;
4089         btrfs_set_stack_inode_nbytes(inode, nbytes);
4090         btrfs_release_path(path);
4091
4092         ret = btrfs_inc_extent_ref(trans, root, extent_bytenr, extent_num_bytes,
4093                                    0, root->root_key.objectid, objectid,
4094                                    file_pos - extent_offset);
4095         if (ret)
4096                 goto fail;
4097         ret = 0;
4098         *ret_num_bytes = min(extent_num_bytes - extent_offset, num_bytes);
4099 fail:
4100         btrfs_free_path(path);
4101         return ret;
4102 }
4103
4104 /*
4105  * Record a file extent. Do all the required works, such as inserting
4106  * file extent item, inserting extent item and backref item into extent
4107  * tree and updating block accounting.
4108  */
4109 int btrfs_record_file_extent(struct btrfs_trans_handle *trans,
4110                               struct btrfs_root *root, u64 objectid,
4111                               struct btrfs_inode_item *inode,
4112                               u64 file_pos, u64 disk_bytenr,
4113                               u64 num_bytes)
4114 {
4115         u64 cur_disk_bytenr = disk_bytenr;
4116         u64 cur_file_pos = file_pos;
4117         u64 cur_num_bytes = num_bytes;
4118         int ret = 0;
4119
4120         while (num_bytes > 0) {
4121                 ret = __btrfs_record_file_extent(trans, root, objectid,
4122                                                  inode, cur_file_pos,
4123                                                  cur_disk_bytenr,
4124                                                  &cur_num_bytes);
4125                 if (ret < 0)
4126                         break;
4127                 cur_disk_bytenr += cur_num_bytes;
4128                 cur_file_pos += cur_num_bytes;
4129                 num_bytes -= cur_num_bytes;
4130         }
4131         return ret;
4132 }
4133
4134
4135 static int add_excluded_extent(struct btrfs_root *root,
4136                                u64 start, u64 num_bytes)
4137 {
4138         u64 end = start + num_bytes - 1;
4139         set_extent_bits(&root->fs_info->pinned_extents,
4140                         start, end, EXTENT_UPTODATE);
4141         return 0;
4142 }
4143
4144 void free_excluded_extents(struct btrfs_root *root,
4145                            struct btrfs_block_group_cache *cache)
4146 {
4147         u64 start, end;
4148
4149         start = cache->key.objectid;
4150         end = start + cache->key.offset - 1;
4151
4152         clear_extent_bits(&root->fs_info->pinned_extents,
4153                           start, end, EXTENT_UPTODATE);
4154 }
4155
4156 int exclude_super_stripes(struct btrfs_root *root,
4157                           struct btrfs_block_group_cache *cache)
4158 {
4159         u64 bytenr;
4160         u64 *logical;
4161         int stripe_len;
4162         int i, nr, ret;
4163
4164         if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
4165                 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
4166                 cache->bytes_super += stripe_len;
4167                 ret = add_excluded_extent(root, cache->key.objectid,
4168                                           stripe_len);
4169                 if (ret)
4170                         return ret;
4171         }
4172
4173         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
4174                 bytenr = btrfs_sb_offset(i);
4175                 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
4176                                        cache->key.objectid, bytenr,
4177                                        0, &logical, &nr, &stripe_len);
4178                 if (ret)
4179                         return ret;
4180
4181                 while (nr--) {
4182                         u64 start, len;
4183
4184                         if (logical[nr] > cache->key.objectid +
4185                             cache->key.offset)
4186                                 continue;
4187
4188                         if (logical[nr] + stripe_len <= cache->key.objectid)
4189                                 continue;
4190
4191                         start = logical[nr];
4192                         if (start < cache->key.objectid) {
4193                                 start = cache->key.objectid;
4194                                 len = (logical[nr] + stripe_len) - start;
4195                         } else {
4196                                 len = min_t(u64, stripe_len,
4197                                             cache->key.objectid +
4198                                             cache->key.offset - start);
4199                         }
4200
4201                         cache->bytes_super += len;
4202                         ret = add_excluded_extent(root, start, len);
4203                         if (ret) {
4204                                 kfree(logical);
4205                                 return ret;
4206                         }
4207                 }
4208
4209                 kfree(logical);
4210         }
4211         return 0;
4212 }
4213
4214 u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
4215                        struct btrfs_fs_info *info, u64 start, u64 end)
4216 {
4217         u64 extent_start, extent_end, size, total_added = 0;
4218         int ret;
4219
4220         while (start < end) {
4221                 ret = find_first_extent_bit(&info->pinned_extents, start,
4222                                             &extent_start, &extent_end,
4223                                             EXTENT_DIRTY | EXTENT_UPTODATE);
4224                 if (ret)
4225                         break;
4226
4227                 if (extent_start <= start) {
4228                         start = extent_end + 1;
4229                 } else if (extent_start > start && extent_start < end) {
4230                         size = extent_start - start;
4231                         total_added += size;
4232                         ret = btrfs_add_free_space(block_group->free_space_ctl,
4233                                                    start, size);
4234                         BUG_ON(ret); /* -ENOMEM or logic error */
4235                         start = extent_end + 1;
4236                 } else {
4237                         break;
4238                 }
4239         }
4240
4241         if (start < end) {
4242                 size = end - start;
4243                 total_added += size;
4244                 ret = btrfs_add_free_space(block_group->free_space_ctl, start,
4245                                            size);
4246                 BUG_ON(ret); /* -ENOMEM or logic error */
4247         }
4248
4249         return total_added;
4250 }