superblock duplication
[platform/upstream/btrfs-progs.git] / extent-tree.c
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include "kerncompat.h"
22 #include "radix-tree.h"
23 #include "ctree.h"
24 #include "disk-io.h"
25 #include "print-tree.h"
26 #include "transaction.h"
27 #include "crc32c.h"
28 #include "volumes.h"
29
30 #define BLOCK_GROUP_DATA     EXTENT_WRITEBACK
31 #define BLOCK_GROUP_METADATA EXTENT_UPTODATE
32 #define BLOCK_GROUP_SYSTEM   EXTENT_NEW
33
34 #define BLOCK_GROUP_DIRTY EXTENT_DIRTY
35
36 #define PENDING_EXTENT_INSERT 0
37 #define PENDING_EXTENT_DELETE 1
38 #define PENDING_BACKREF_UPDATE 2
39
40 struct pending_extent_op {
41         int type;
42         u64 bytenr;
43         u64 num_bytes;
44         u64 parent;
45         u64 orig_parent;
46         u64 generation;
47         u64 orig_generation;
48         int level;
49 };
50
51 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
52                                  btrfs_root *extent_root);
53 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
54                                btrfs_root *extent_root);
55
56 void maybe_lock_mutex(struct btrfs_root *root)
57 {
58 }
59
60 void maybe_unlock_mutex(struct btrfs_root *root)
61 {
62 }
63
64 static int remove_sb_from_cache(struct btrfs_root *root,
65                                 struct btrfs_block_group_cache *cache)
66 {
67         u64 bytenr;
68         u64 *logical;
69         int stripe_len;
70         int i, nr, ret;
71         struct extent_io_tree *free_space_cache;
72
73         free_space_cache = &root->fs_info->free_space_cache;
74         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
75                 bytenr = btrfs_sb_offset(i);
76                 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
77                                        cache->key.objectid, bytenr, 0,
78                                        &logical, &nr, &stripe_len);
79                 BUG_ON(ret);
80                 while (nr--) {
81                         clear_extent_dirty(free_space_cache, logical[nr],
82                                 logical[nr] + stripe_len - 1, GFP_NOFS);
83                 }
84                 kfree(logical);
85         }
86         return 0;
87 }
88
89 static int cache_block_group(struct btrfs_root *root,
90                              struct btrfs_block_group_cache *block_group)
91 {
92         struct btrfs_path *path;
93         int ret;
94         struct btrfs_key key;
95         struct extent_buffer *leaf;
96         struct extent_io_tree *free_space_cache;
97         int slot;
98         u64 last = 0;
99         u64 hole_size;
100         u64 first_free;
101         int found = 0;
102
103         if (!block_group)
104                 return 0;
105
106         root = root->fs_info->extent_root;
107         free_space_cache = &root->fs_info->free_space_cache;
108
109         if (block_group->cached)
110                 return 0;
111
112         path = btrfs_alloc_path();
113         if (!path)
114                 return -ENOMEM;
115
116         path->reada = 2;
117         first_free = block_group->key.objectid;
118         key.objectid = block_group->key.objectid;
119         key.offset = 0;
120         btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
121         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
122         if (ret < 0)
123                 return ret;
124         ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
125         if (ret < 0)
126                 return ret;
127         if (ret == 0) {
128                 leaf = path->nodes[0];
129                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
130                 if (key.objectid + key.offset > first_free)
131                         first_free = key.objectid + key.offset;
132         }
133         while(1) {
134                 leaf = path->nodes[0];
135                 slot = path->slots[0];
136                 if (slot >= btrfs_header_nritems(leaf)) {
137                         ret = btrfs_next_leaf(root, path);
138                         if (ret < 0)
139                                 goto err;
140                         if (ret == 0) {
141                                 continue;
142                         } else {
143                                 break;
144                         }
145                 }
146                 btrfs_item_key_to_cpu(leaf, &key, slot);
147                 if (key.objectid < block_group->key.objectid) {
148                         goto next;
149                 }
150                 if (key.objectid >= block_group->key.objectid +
151                     block_group->key.offset) {
152                         break;
153                 }
154
155                 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
156                         if (!found) {
157                                 last = first_free;
158                                 found = 1;
159                         }
160                         if (key.objectid > last) {
161                                 hole_size = key.objectid - last;
162                                 set_extent_dirty(free_space_cache, last,
163                                                  last + hole_size - 1,
164                                                  GFP_NOFS);
165                         }
166                         last = key.objectid + key.offset;
167                 }
168 next:
169                 path->slots[0]++;
170         }
171
172         if (!found)
173                 last = first_free;
174         if (block_group->key.objectid +
175             block_group->key.offset > last) {
176                 hole_size = block_group->key.objectid +
177                         block_group->key.offset - last;
178                 set_extent_dirty(free_space_cache, last,
179                                  last + hole_size - 1, GFP_NOFS);
180         }
181         remove_sb_from_cache(root, block_group);
182         block_group->cached = 1;
183 err:
184         btrfs_free_path(path);
185         return 0;
186 }
187
188 struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
189                                                        btrfs_fs_info *info,
190                                                        u64 bytenr)
191 {
192         struct extent_io_tree *block_group_cache;
193         struct btrfs_block_group_cache *block_group = NULL;
194         u64 ptr;
195         u64 start;
196         u64 end;
197         int ret;
198
199         bytenr = max_t(u64, bytenr,
200                        BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
201         block_group_cache = &info->block_group_cache;
202         ret = find_first_extent_bit(block_group_cache,
203                                     bytenr, &start, &end,
204                                     BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
205                                     BLOCK_GROUP_SYSTEM);
206         if (ret) {
207                 return NULL;
208         }
209         ret = get_state_private(block_group_cache, start, &ptr);
210         if (ret)
211                 return NULL;
212
213         block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
214         return block_group;
215 }
216
217 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
218                                                          btrfs_fs_info *info,
219                                                          u64 bytenr)
220 {
221         struct extent_io_tree *block_group_cache;
222         struct btrfs_block_group_cache *block_group = NULL;
223         u64 ptr;
224         u64 start;
225         u64 end;
226         int ret;
227
228         block_group_cache = &info->block_group_cache;
229         ret = find_first_extent_bit(block_group_cache,
230                                     bytenr, &start, &end,
231                                     BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
232                                     BLOCK_GROUP_SYSTEM);
233         if (ret) {
234                 return NULL;
235         }
236         ret = get_state_private(block_group_cache, start, &ptr);
237         if (ret)
238                 return NULL;
239
240         block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
241         if (block_group->key.objectid <= bytenr && bytenr <
242             block_group->key.objectid + block_group->key.offset)
243                 return block_group;
244         return NULL;
245 }
246
247 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
248 {
249         return (cache->flags & bits) == bits;
250 }
251
252 static int noinline find_search_start(struct btrfs_root *root,
253                               struct btrfs_block_group_cache **cache_ret,
254                               u64 *start_ret, int num, int data)
255 {
256         int ret;
257         struct btrfs_block_group_cache *cache = *cache_ret;
258         u64 last;
259         u64 start = 0;
260         u64 end = 0;
261         u64 search_start = *start_ret;
262         int wrapped = 0;
263
264         if (!cache) {
265                 goto out;
266         }
267 again:
268         ret = cache_block_group(root, cache);
269         if (ret)
270                 goto out;
271
272         last = max(search_start, cache->key.objectid);
273         if (cache->ro || !block_group_bits(cache, data)) {
274                 goto new_group;
275         }
276
277         while(1) {
278                 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
279                                             last, &start, &end, EXTENT_DIRTY);
280                 if (ret) {
281                         goto new_group;
282                 }
283
284                 start = max(last, start);
285                 last = end + 1;
286                 if (last - start < num) {
287                         continue;
288                 }
289                 if (start + num > cache->key.objectid + cache->key.offset) {
290                         goto new_group;
291                 }
292                 *start_ret = start;
293                 return 0;
294         }
295 out:
296         cache = btrfs_lookup_block_group(root->fs_info, search_start);
297         if (!cache) {
298                 printk("Unable to find block group for %llu\n",
299                         (unsigned long long)search_start);
300                 WARN_ON(1);
301         }
302         return -ENOSPC;
303
304 new_group:
305         last = cache->key.objectid + cache->key.offset;
306 wrapped:
307         cache = btrfs_lookup_first_block_group(root->fs_info, last);
308         if (!cache) {
309 no_cache:
310                 if (!wrapped) {
311                         wrapped = 1;
312                         last = search_start;
313                         goto wrapped;
314                 }
315                 goto out;
316         }
317         cache = btrfs_find_block_group(root, cache, last, data, 0);
318         cache = btrfs_find_block_group(root, cache, last, data, 0);
319         if (!cache)
320                 goto no_cache;
321
322         *cache_ret = cache;
323         goto again;
324 }
325
326 static u64 div_factor(u64 num, int factor)
327 {
328         if (factor == 10)
329                 return num;
330         num *= factor;
331         num /= 10;
332         return num;
333 }
334
335 static int block_group_state_bits(u64 flags)
336 {
337         int bits = 0;
338         if (flags & BTRFS_BLOCK_GROUP_DATA)
339                 bits |= BLOCK_GROUP_DATA;
340         if (flags & BTRFS_BLOCK_GROUP_METADATA)
341                 bits |= BLOCK_GROUP_METADATA;
342         if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
343                 bits |= BLOCK_GROUP_SYSTEM;
344         return bits;
345 }
346
347 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
348                                                  struct btrfs_block_group_cache
349                                                  *hint, u64 search_start,
350                                                  int data, int owner)
351 {
352         struct btrfs_block_group_cache *cache;
353         struct extent_io_tree *block_group_cache;
354         struct btrfs_block_group_cache *found_group = NULL;
355         struct btrfs_fs_info *info = root->fs_info;
356         u64 used;
357         u64 last = 0;
358         u64 hint_last;
359         u64 start;
360         u64 end;
361         u64 free_check;
362         u64 ptr;
363         int bit;
364         int ret;
365         int full_search = 0;
366         int factor = 10;
367
368         block_group_cache = &info->block_group_cache;
369
370         if (!owner)
371                 factor = 10;
372
373         bit = block_group_state_bits(data);
374
375         if (search_start) {
376                 struct btrfs_block_group_cache *shint;
377                 shint = btrfs_lookup_block_group(info, search_start);
378                 if (shint && !shint->ro && block_group_bits(shint, data)) {
379                         used = btrfs_block_group_used(&shint->item);
380                         if (used + shint->pinned <
381                             div_factor(shint->key.offset, factor)) {
382                                 return shint;
383                         }
384                 }
385         }
386         if (hint && !hint->ro && block_group_bits(hint, data)) {
387                 used = btrfs_block_group_used(&hint->item);
388                 if (used + hint->pinned <
389                     div_factor(hint->key.offset, factor)) {
390                         return hint;
391                 }
392                 last = hint->key.objectid + hint->key.offset;
393                 hint_last = last;
394         } else {
395                 if (hint)
396                         hint_last = max(hint->key.objectid, search_start);
397                 else
398                         hint_last = search_start;
399
400                 last = hint_last;
401         }
402 again:
403         while(1) {
404                 ret = find_first_extent_bit(block_group_cache, last,
405                                             &start, &end, bit);
406                 if (ret)
407                         break;
408
409                 ret = get_state_private(block_group_cache, start, &ptr);
410                 if (ret)
411                         break;
412
413                 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
414                 last = cache->key.objectid + cache->key.offset;
415                 used = btrfs_block_group_used(&cache->item);
416
417                 if (!cache->ro && block_group_bits(cache, data)) {
418                         if (full_search)
419                                 free_check = cache->key.offset;
420                         else
421                                 free_check = div_factor(cache->key.offset,
422                                                         factor);
423
424                         if (used + cache->pinned < free_check) {
425                                 found_group = cache;
426                                 goto found;
427                         }
428                 }
429                 cond_resched();
430         }
431         if (!full_search) {
432                 last = search_start;
433                 full_search = 1;
434                 goto again;
435         }
436 found:
437         return found_group;
438 }
439
440 /*
441  * Back reference rules.  Back refs have three main goals:
442  *
443  * 1) differentiate between all holders of references to an extent so that
444  *    when a reference is dropped we can make sure it was a valid reference
445  *    before freeing the extent.
446  *
447  * 2) Provide enough information to quickly find the holders of an extent
448  *    if we notice a given block is corrupted or bad.
449  *
450  * 3) Make it easy to migrate blocks for FS shrinking or storage pool
451  *    maintenance.  This is actually the same as #2, but with a slightly
452  *    different use case.
453  *
454  * File extents can be referenced by:
455  *
456  * - multiple snapshots, subvolumes, or different generations in one subvol
457  * - different files inside a single subvolume
458  * - different offsets inside a file (bookend extents in file.c)
459  *
460  * The extent ref structure has fields for:
461  *
462  * - Objectid of the subvolume root
463  * - Generation number of the tree holding the reference
464  * - objectid of the file holding the reference
465  * - offset in the file corresponding to the key holding the reference
466  * - number of references holding by parent node (alway 1 for tree blocks)
467  *
468  * Btree leaf may hold multiple references to a file extent. In most cases,
469  * these references are from same file and the corresponding offsets inside
470  * the file are close together. So inode objectid and offset in file are
471  * just hints, they provide hints about where in the btree the references
472  * can be found and when we can stop searching.
473  *
474  * When a file extent is allocated the fields are filled in:
475  *     (root_key.objectid, trans->transid, inode objectid, offset in file, 1)
476  *
477  * When a leaf is cow'd new references are added for every file extent found
478  * in the leaf.  It looks similar to the create case, but trans->transid will
479  * be different when the block is cow'd.
480  *
481  *     (root_key.objectid, trans->transid, inode objectid, offset in file,
482  *      number of references in the leaf)
483  *
484  * Because inode objectid and offset in file are just hints, they are not
485  * used when backrefs are deleted. When a file extent is removed either
486  * during snapshot deletion or file truncation, we find the corresponding
487  * back back reference and check the following fields.
488  *
489  *     (btrfs_header_owner(leaf), btrfs_header_generation(leaf))
490  *
491  * Btree extents can be referenced by:
492  *
493  * - Different subvolumes
494  * - Different generations of the same subvolume
495  *
496  * When a tree block is created, back references are inserted:
497  *
498  * (root->root_key.objectid, trans->transid, level, 0, 1)
499  *
500  * When a tree block is cow'd, new back references are added for all the
501  * blocks it points to. If the tree block isn't in reference counted root,
502  * the old back references are removed. These new back references are of
503  * the form (trans->transid will have increased since creation):
504  *
505  * (root->root_key.objectid, trans->transid, level, 0, 1)
506  *
507  * When a backref is in deleting, the following fields are checked:
508  *
509  * if backref was for a tree root:
510  *     (btrfs_header_owner(itself), btrfs_header_generation(itself))
511  * else
512  *     (btrfs_header_owner(parent), btrfs_header_generation(parent))
513  *
514  * Back Reference Key composing:
515  *
516  * The key objectid corresponds to the first byte in the extent, the key
517  * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
518  * byte of parent extent. If a extent is tree root, the key offset is set
519  * to the key objectid.
520  */
521
522 static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
523                                           struct btrfs_root *root,
524                                           struct btrfs_path *path,
525                                           u64 bytenr, u64 parent,
526                                           u64 ref_root, u64 ref_generation,
527                                           u64 owner_objectid, int del)
528 {
529         struct btrfs_key key;
530         struct btrfs_extent_ref *ref;
531         struct extent_buffer *leaf;
532         u64 ref_objectid;
533         int ret;
534
535         key.objectid = bytenr;
536         key.type = BTRFS_EXTENT_REF_KEY;
537         key.offset = parent;
538
539         ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
540         if (ret < 0)
541                 goto out;
542         if (ret > 0) {
543                 ret = -ENOENT;
544                 goto out;
545         }
546
547         leaf = path->nodes[0];
548         ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
549         ref_objectid = btrfs_ref_objectid(leaf, ref);
550         if (btrfs_ref_root(leaf, ref) != ref_root ||
551             btrfs_ref_generation(leaf, ref) != ref_generation ||
552             (ref_objectid != owner_objectid &&
553              ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
554                 ret = -EIO;
555                 WARN_ON(1);
556                 goto out;
557         }
558         ret = 0;
559 out:
560         return ret;
561 }
562
563 static int noinline insert_extent_backref(struct btrfs_trans_handle *trans,
564                                           struct btrfs_root *root,
565                                           struct btrfs_path *path,
566                                           u64 bytenr, u64 parent,
567                                           u64 ref_root, u64 ref_generation,
568                                           u64 owner_objectid)
569 {
570         struct btrfs_key key;
571         struct extent_buffer *leaf;
572         struct btrfs_extent_ref *ref;
573         u32 num_refs;
574         int ret;
575
576         key.objectid = bytenr;
577         key.type = BTRFS_EXTENT_REF_KEY;
578         key.offset = parent;
579
580         ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
581         if (ret == 0) {
582                 leaf = path->nodes[0];
583                 ref = btrfs_item_ptr(leaf, path->slots[0],
584                                      struct btrfs_extent_ref);
585                 btrfs_set_ref_root(leaf, ref, ref_root);
586                 btrfs_set_ref_generation(leaf, ref, ref_generation);
587                 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
588                 btrfs_set_ref_num_refs(leaf, ref, 1);
589         } else if (ret == -EEXIST) {
590                 u64 existing_owner;
591                 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
592                 leaf = path->nodes[0];
593                 ref = btrfs_item_ptr(leaf, path->slots[0],
594                                      struct btrfs_extent_ref);
595                 if (btrfs_ref_root(leaf, ref) != ref_root ||
596                     btrfs_ref_generation(leaf, ref) != ref_generation) {
597                         ret = -EIO;
598                         WARN_ON(1);
599                         goto out;
600                 }
601
602                 num_refs = btrfs_ref_num_refs(leaf, ref);
603                 BUG_ON(num_refs == 0);
604                 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
605
606                 existing_owner = btrfs_ref_objectid(leaf, ref);
607                 if (existing_owner != owner_objectid &&
608                     existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
609                         btrfs_set_ref_objectid(leaf, ref,
610                                         BTRFS_MULTIPLE_OBJECTIDS);
611                 }
612                 ret = 0;
613         } else {
614                 goto out;
615         }
616         btrfs_mark_buffer_dirty(path->nodes[0]);
617 out:
618         btrfs_release_path(root, path);
619         return ret;
620 }
621
622 static int noinline remove_extent_backref(struct btrfs_trans_handle *trans,
623                                           struct btrfs_root *root,
624                                           struct btrfs_path *path)
625 {
626         struct extent_buffer *leaf;
627         struct btrfs_extent_ref *ref;
628         u32 num_refs;
629         int ret = 0;
630
631         leaf = path->nodes[0];
632         ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
633         num_refs = btrfs_ref_num_refs(leaf, ref);
634         BUG_ON(num_refs == 0);
635         num_refs -= 1;
636         if (num_refs == 0) {
637                 ret = btrfs_del_item(trans, root, path);
638         } else {
639                 btrfs_set_ref_num_refs(leaf, ref, num_refs);
640                 btrfs_mark_buffer_dirty(leaf);
641         }
642         btrfs_release_path(root, path);
643         return ret;
644 }
645
646 static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
647                                      struct btrfs_root *root, u64 bytenr,
648                                      u64 orig_parent, u64 parent,
649                                      u64 orig_root, u64 ref_root,
650                                      u64 orig_generation, u64 ref_generation,
651                                      u64 owner_objectid)
652 {
653         int ret;
654         struct btrfs_root *extent_root = root->fs_info->extent_root;
655         struct btrfs_path *path;
656
657         if (root == root->fs_info->extent_root) {
658                 struct pending_extent_op *extent_op;
659                 u64 num_bytes;
660
661                 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
662                 num_bytes = btrfs_level_size(root, (int)owner_objectid);
663                 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
664                                 bytenr + num_bytes - 1, EXTENT_LOCKED, 0)) {
665                         u64 priv;
666                         ret = get_state_private(&root->fs_info->extent_ins,
667                                                 bytenr, &priv);
668                         BUG_ON(ret);
669                         extent_op = (struct pending_extent_op *)
670                                                         (unsigned long)priv;
671                         BUG_ON(extent_op->parent != orig_parent);
672                         BUG_ON(extent_op->generation != orig_generation);
673                         extent_op->parent = parent;
674                         extent_op->generation = ref_generation;
675                 } else {
676                         extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
677                         BUG_ON(!extent_op);
678
679                         extent_op->type = PENDING_BACKREF_UPDATE;
680                         extent_op->bytenr = bytenr;
681                         extent_op->num_bytes = num_bytes;
682                         extent_op->parent = parent;
683                         extent_op->orig_parent = orig_parent;
684                         extent_op->generation = ref_generation;
685                         extent_op->orig_generation = orig_generation;
686                         extent_op->level = (int)owner_objectid;
687
688                         set_extent_bits(&root->fs_info->extent_ins,
689                                         bytenr, bytenr + num_bytes - 1,
690                                         EXTENT_LOCKED, GFP_NOFS);
691                         set_state_private(&root->fs_info->extent_ins,
692                                           bytenr, (unsigned long)extent_op);
693                 }
694                 return 0;
695         }
696
697         path = btrfs_alloc_path();
698         if (!path)
699                 return -ENOMEM;
700         ret = lookup_extent_backref(trans, extent_root, path,
701                                     bytenr, orig_parent, orig_root,
702                                     orig_generation, owner_objectid, 1);
703         if (ret)
704                 goto out;
705         ret = remove_extent_backref(trans, extent_root, path);
706         if (ret)
707                 goto out;
708         ret = insert_extent_backref(trans, extent_root, path, bytenr,
709                                     parent, ref_root, ref_generation,
710                                     owner_objectid);
711         BUG_ON(ret);
712         finish_current_insert(trans, extent_root);
713         del_pending_extents(trans, extent_root);
714 out:
715         btrfs_free_path(path);
716         return ret;
717 }
718
719 int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
720                             struct btrfs_root *root, u64 bytenr,
721                             u64 orig_parent, u64 parent,
722                             u64 ref_root, u64 ref_generation,
723                             u64 owner_objectid)
724 {
725         int ret;
726         if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
727             owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
728                 return 0;
729         maybe_lock_mutex(root);
730         ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
731                                         parent, ref_root, ref_root,
732                                         ref_generation, ref_generation,
733                                         owner_objectid);
734         maybe_unlock_mutex(root);
735         return ret;
736 }
737
738 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
739                                   struct btrfs_root *root, u64 bytenr,
740                                   u64 orig_parent, u64 parent,
741                                   u64 orig_root, u64 ref_root,
742                                   u64 orig_generation, u64 ref_generation,
743                                   u64 owner_objectid)
744 {
745         struct btrfs_path *path;
746         int ret;
747         struct btrfs_key key;
748         struct extent_buffer *l;
749         struct btrfs_extent_item *item;
750         u32 refs;
751
752         path = btrfs_alloc_path();
753         if (!path)
754                 return -ENOMEM;
755
756         path->reada = 1;
757         key.objectid = bytenr;
758         key.type = BTRFS_EXTENT_ITEM_KEY;
759         key.offset = (u64)-1;
760
761         ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
762                                 0, 1);
763         if (ret < 0)
764                 return ret;
765         BUG_ON(ret == 0 || path->slots[0] == 0);
766
767         path->slots[0]--;
768         l = path->nodes[0];
769
770         btrfs_item_key_to_cpu(l, &key, path->slots[0]);
771         BUG_ON(key.objectid != bytenr);
772         BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
773
774         item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
775         refs = btrfs_extent_refs(l, item);
776         btrfs_set_extent_refs(l, item, refs + 1);
777         btrfs_mark_buffer_dirty(path->nodes[0]);
778
779         btrfs_release_path(root->fs_info->extent_root, path);
780
781         path->reada = 1;
782         ret = insert_extent_backref(trans, root->fs_info->extent_root,
783                                     path, bytenr, parent,
784                                     ref_root, ref_generation,
785                                     owner_objectid);
786         BUG_ON(ret);
787         finish_current_insert(trans, root->fs_info->extent_root);
788         del_pending_extents(trans, root->fs_info->extent_root);
789
790         btrfs_free_path(path);
791         return 0;
792 }
793
794 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
795                          struct btrfs_root *root,
796                          u64 bytenr, u64 num_bytes, u64 parent,
797                          u64 ref_root, u64 ref_generation,
798                          u64 owner_objectid)
799 {
800         int ret;
801         if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
802             owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
803                 return 0;
804         maybe_lock_mutex(root);
805         ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
806                                      0, ref_root, 0, ref_generation,
807                                      owner_objectid);
808         maybe_unlock_mutex(root);
809         return ret;
810 }
811
812 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
813                          struct btrfs_root *root)
814 {
815         finish_current_insert(trans, root->fs_info->extent_root);
816         del_pending_extents(trans, root->fs_info->extent_root);
817         return 0;
818 }
819
820 int lookup_extent_ref(struct btrfs_trans_handle *trans,
821                       struct btrfs_root *root, u64 bytenr,
822                       u64 num_bytes, u32 *refs)
823 {
824         struct btrfs_path *path;
825         int ret;
826         struct btrfs_key key;
827         struct extent_buffer *l;
828         struct btrfs_extent_item *item;
829
830         WARN_ON(num_bytes < root->sectorsize);
831         path = btrfs_alloc_path();
832         path->reada = 1;
833         key.objectid = bytenr;
834         key.offset = num_bytes;
835         btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
836         ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
837                                 0, 0);
838         if (ret < 0)
839                 goto out;
840         if (ret != 0) {
841                 btrfs_print_leaf(root, path->nodes[0]);
842                 printk("failed to find block number %Lu\n", bytenr);
843                 BUG();
844         }
845         l = path->nodes[0];
846         item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
847         *refs = btrfs_extent_refs(l, item);
848 out:
849         btrfs_free_path(path);
850         return 0;
851 }
852
853 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
854                   struct extent_buffer *orig_buf, struct extent_buffer *buf,
855                   u32 *nr_extents)
856 {
857         u64 bytenr;
858         u64 ref_root;
859         u64 orig_root;
860         u64 ref_generation;
861         u64 orig_generation;
862         u32 nritems;
863         u32 nr_file_extents = 0;
864         struct btrfs_key key;
865         struct btrfs_file_extent_item *fi;
866         int i;
867         int level;
868         int ret = 0;
869         int faili = 0;
870         int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
871                             u64, u64, u64, u64, u64, u64, u64, u64);
872
873         ref_root = btrfs_header_owner(buf);
874         ref_generation = btrfs_header_generation(buf);
875         orig_root = btrfs_header_owner(orig_buf);
876         orig_generation = btrfs_header_generation(orig_buf);
877
878         nritems = btrfs_header_nritems(buf);
879         level = btrfs_header_level(buf);
880
881         if (root->ref_cows) {
882                 process_func = __btrfs_inc_extent_ref;
883         } else {
884                 if (level == 0 &&
885                     root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
886                         goto out;
887                 process_func = __btrfs_update_extent_ref;
888         }
889
890         for (i = 0; i < nritems; i++) {
891                 cond_resched();
892                 if (level == 0) {
893                         btrfs_item_key_to_cpu(buf, &key, i);
894                         if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
895                                 continue;
896                         fi = btrfs_item_ptr(buf, i,
897                                             struct btrfs_file_extent_item);
898                         if (btrfs_file_extent_type(buf, fi) ==
899                             BTRFS_FILE_EXTENT_INLINE)
900                                 continue;
901                         bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
902                         if (bytenr == 0)
903                                 continue;
904
905                         nr_file_extents++;
906
907                         maybe_lock_mutex(root);
908                         ret = process_func(trans, root, bytenr,
909                                            orig_buf->start, buf->start,
910                                            orig_root, ref_root,
911                                            orig_generation, ref_generation,
912                                            key.objectid);
913                         maybe_unlock_mutex(root);
914
915                         if (ret) {
916                                 faili = i;
917                                 WARN_ON(1);
918                                 goto fail;
919                         }
920                 } else {
921                         bytenr = btrfs_node_blockptr(buf, i);
922                         maybe_lock_mutex(root);
923                         ret = process_func(trans, root, bytenr,
924                                            orig_buf->start, buf->start,
925                                            orig_root, ref_root,
926                                            orig_generation, ref_generation,
927                                            level - 1);
928                         maybe_unlock_mutex(root);
929                         if (ret) {
930                                 faili = i;
931                                 WARN_ON(1);
932                                 goto fail;
933                         }
934                 }
935         }
936 out:
937         if (nr_extents) {
938                 if (level == 0)
939                         *nr_extents = nr_file_extents;
940                 else
941                         *nr_extents = nritems;
942         }
943         return 0;
944 fail:
945         WARN_ON(1);
946 #if 0
947         for (i =0; i < faili; i++) {
948                 if (level == 0) {
949                         u64 disk_bytenr;
950                         btrfs_item_key_to_cpu(buf, &key, i);
951                         if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
952                                 continue;
953                         fi = btrfs_item_ptr(buf, i,
954                                             struct btrfs_file_extent_item);
955                         if (btrfs_file_extent_type(buf, fi) ==
956                             BTRFS_FILE_EXTENT_INLINE)
957                                 continue;
958                         disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
959                         if (disk_bytenr == 0)
960                                 continue;
961                         err = btrfs_free_extent(trans, root, disk_bytenr,
962                                     btrfs_file_extent_disk_num_bytes(buf,
963                                                                       fi), 0);
964                         BUG_ON(err);
965                 } else {
966                         bytenr = btrfs_node_blockptr(buf, i);
967                         err = btrfs_free_extent(trans, root, bytenr,
968                                         btrfs_level_size(root, level - 1), 0);
969                         BUG_ON(err);
970                 }
971         }
972 #endif
973         return ret;
974 }
975
976 int btrfs_update_ref(struct btrfs_trans_handle *trans,
977                      struct btrfs_root *root, struct extent_buffer *orig_buf,
978                      struct extent_buffer *buf, int start_slot, int nr)
979
980 {
981         u64 bytenr;
982         u64 ref_root;
983         u64 orig_root;
984         u64 ref_generation;
985         u64 orig_generation;
986         struct btrfs_key key;
987         struct btrfs_file_extent_item *fi;
988         int i;
989         int ret;
990         int slot;
991         int level;
992
993         BUG_ON(start_slot < 0);
994         BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
995
996         ref_root = btrfs_header_owner(buf);
997         ref_generation = btrfs_header_generation(buf);
998         orig_root = btrfs_header_owner(orig_buf);
999         orig_generation = btrfs_header_generation(orig_buf);
1000         level = btrfs_header_level(buf);
1001
1002         if (!root->ref_cows) {
1003                 if (level == 0 &&
1004                     root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1005                         return 0;
1006         }
1007
1008         for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1009                 cond_resched();
1010                 if (level == 0) {
1011                         btrfs_item_key_to_cpu(buf, &key, slot);
1012                         if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1013                                 continue;
1014                         fi = btrfs_item_ptr(buf, slot,
1015                                             struct btrfs_file_extent_item);
1016                         if (btrfs_file_extent_type(buf, fi) ==
1017                             BTRFS_FILE_EXTENT_INLINE)
1018                                 continue;
1019                         bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1020                         if (bytenr == 0)
1021                                 continue;
1022
1023                         maybe_lock_mutex(root);
1024                         ret = __btrfs_update_extent_ref(trans, root, bytenr,
1025                                             orig_buf->start, buf->start,
1026                                             orig_root, ref_root,
1027                                             orig_generation, ref_generation,
1028                                             key.objectid);
1029                         maybe_unlock_mutex(root);
1030                         if (ret)
1031                                 goto fail;
1032                 } else {
1033                         bytenr = btrfs_node_blockptr(buf, slot);
1034                         maybe_lock_mutex(root);
1035                         ret = __btrfs_update_extent_ref(trans, root, bytenr,
1036                                             orig_buf->start, buf->start,
1037                                             orig_root, ref_root,
1038                                             orig_generation, ref_generation,
1039                                             level - 1);
1040                         maybe_unlock_mutex(root);
1041                         if (ret)
1042                                 goto fail;
1043                 }
1044         }
1045         return 0;
1046 fail:
1047         WARN_ON(1);
1048         return -1;
1049 }
1050
1051 static int write_one_cache_group(struct btrfs_trans_handle *trans,
1052                                  struct btrfs_root *root,
1053                                  struct btrfs_path *path,
1054                                  struct btrfs_block_group_cache *cache)
1055 {
1056         int ret;
1057         int pending_ret;
1058         struct btrfs_root *extent_root = root->fs_info->extent_root;
1059         unsigned long bi;
1060         struct extent_buffer *leaf;
1061
1062         ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
1063         if (ret < 0)
1064                 goto fail;
1065         BUG_ON(ret);
1066
1067         leaf = path->nodes[0];
1068         bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1069         write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1070         btrfs_mark_buffer_dirty(leaf);
1071         btrfs_release_path(extent_root, path);
1072 fail:
1073         finish_current_insert(trans, extent_root);
1074         pending_ret = del_pending_extents(trans, extent_root);
1075         if (ret)
1076                 return ret;
1077         if (pending_ret)
1078                 return pending_ret;
1079         return 0;
1080
1081 }
1082
1083 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1084                                    struct btrfs_root *root)
1085 {
1086         struct extent_io_tree *block_group_cache;
1087         struct btrfs_block_group_cache *cache;
1088         int ret;
1089         int err = 0;
1090         int werr = 0;
1091         struct btrfs_path *path;
1092         u64 last = 0;
1093         u64 start;
1094         u64 end;
1095         u64 ptr;
1096
1097         block_group_cache = &root->fs_info->block_group_cache;
1098         path = btrfs_alloc_path();
1099         if (!path)
1100                 return -ENOMEM;
1101
1102         while(1) {
1103                 ret = find_first_extent_bit(block_group_cache, last,
1104                                             &start, &end, BLOCK_GROUP_DIRTY);
1105                 if (ret)
1106                         break;
1107
1108                 last = end + 1;
1109                 ret = get_state_private(block_group_cache, start, &ptr);
1110                 if (ret)
1111                         break;
1112                 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
1113                 err = write_one_cache_group(trans, root,
1114                                             path, cache);
1115                 /*
1116                  * if we fail to write the cache group, we want
1117                  * to keep it marked dirty in hopes that a later
1118                  * write will work
1119                  */
1120                 if (err) {
1121                         werr = err;
1122                         continue;
1123                 }
1124                 clear_extent_bits(block_group_cache, start, end,
1125                                   BLOCK_GROUP_DIRTY, GFP_NOFS);
1126         }
1127         btrfs_free_path(path);
1128         return werr;
1129 }
1130
1131 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
1132                                                   u64 flags)
1133 {
1134         struct list_head *head = &info->space_info;
1135         struct list_head *cur;
1136         struct btrfs_space_info *found;
1137         list_for_each(cur, head) {
1138                 found = list_entry(cur, struct btrfs_space_info, list);
1139                 if (found->flags == flags)
1140                         return found;
1141         }
1142         return NULL;
1143
1144 }
1145
1146 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1147                              u64 total_bytes, u64 bytes_used,
1148                              struct btrfs_space_info **space_info)
1149 {
1150         struct btrfs_space_info *found;
1151
1152         found = __find_space_info(info, flags);
1153         if (found) {
1154                 found->total_bytes += total_bytes;
1155                 found->bytes_used += bytes_used;
1156                 WARN_ON(found->total_bytes < found->bytes_used);
1157                 *space_info = found;
1158                 return 0;
1159         }
1160         found = kmalloc(sizeof(*found), GFP_NOFS);
1161         if (!found)
1162                 return -ENOMEM;
1163
1164         list_add(&found->list, &info->space_info);
1165         found->flags = flags;
1166         found->total_bytes = total_bytes;
1167         found->bytes_used = bytes_used;
1168         found->bytes_pinned = 0;
1169         found->full = 0;
1170         *space_info = found;
1171         return 0;
1172 }
1173
1174
1175 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1176 {
1177         u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
1178                                    BTRFS_BLOCK_GROUP_RAID1 |
1179                                    BTRFS_BLOCK_GROUP_DUP);
1180         if (extra_flags) {
1181                 if (flags & BTRFS_BLOCK_GROUP_DATA)
1182                         fs_info->avail_data_alloc_bits |= extra_flags;
1183                 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1184                         fs_info->avail_metadata_alloc_bits |= extra_flags;
1185                 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1186                         fs_info->avail_system_alloc_bits |= extra_flags;
1187         }
1188 }
1189
1190 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1191                           struct btrfs_root *extent_root, u64 alloc_bytes,
1192                           u64 flags)
1193 {
1194         struct btrfs_space_info *space_info;
1195         u64 thresh;
1196         u64 start;
1197         u64 num_bytes;
1198         int ret;
1199
1200         space_info = __find_space_info(extent_root->fs_info, flags);
1201         if (!space_info) {
1202                 ret = update_space_info(extent_root->fs_info, flags,
1203                                         0, 0, &space_info);
1204                 BUG_ON(ret);
1205         }
1206         BUG_ON(!space_info);
1207
1208         if (space_info->full)
1209                 return 0;
1210
1211         thresh = div_factor(space_info->total_bytes, 7);
1212         if ((space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) <
1213             thresh)
1214                 return 0;
1215
1216         ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
1217         if (ret == -ENOSPC) {
1218                 space_info->full = 1;
1219                 return 0;
1220         }
1221
1222         BUG_ON(ret);
1223
1224         ret = btrfs_make_block_group(trans, extent_root, 0, flags,
1225                      BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
1226         BUG_ON(ret);
1227         return 0;
1228 }
1229
1230 static int update_block_group(struct btrfs_trans_handle *trans,
1231                               struct btrfs_root *root,
1232                               u64 bytenr, u64 num_bytes, int alloc,
1233                               int mark_free)
1234 {
1235         struct btrfs_block_group_cache *cache;
1236         struct btrfs_fs_info *info = root->fs_info;
1237         u64 total = num_bytes;
1238         u64 old_val;
1239         u64 byte_in_group;
1240         u64 start;
1241         u64 end;
1242
1243         while(total) {
1244                 cache = btrfs_lookup_block_group(info, bytenr);
1245                 if (!cache) {
1246                         return -1;
1247                 }
1248                 byte_in_group = bytenr - cache->key.objectid;
1249                 WARN_ON(byte_in_group > cache->key.offset);
1250                 start = cache->key.objectid;
1251                 end = start + cache->key.offset - 1;
1252                 set_extent_bits(&info->block_group_cache, start, end,
1253                                 BLOCK_GROUP_DIRTY, GFP_NOFS);
1254
1255                 old_val = btrfs_block_group_used(&cache->item);
1256                 num_bytes = min(total, cache->key.offset - byte_in_group);
1257                 if (alloc) {
1258                         old_val += num_bytes;
1259                         cache->space_info->bytes_used += num_bytes;
1260                 } else {
1261                         old_val -= num_bytes;
1262                         cache->space_info->bytes_used -= num_bytes;
1263                         if (mark_free) {
1264                                 set_extent_dirty(&info->free_space_cache,
1265                                                  bytenr, bytenr + num_bytes - 1,
1266                                                  GFP_NOFS);
1267                         }
1268                 }
1269                 btrfs_set_block_group_used(&cache->item, old_val);
1270                 total -= num_bytes;
1271                 bytenr += num_bytes;
1272         }
1273         return 0;
1274 }
1275
1276 static int update_pinned_extents(struct btrfs_root *root,
1277                                 u64 bytenr, u64 num, int pin)
1278 {
1279         u64 len;
1280         struct btrfs_block_group_cache *cache;
1281         struct btrfs_fs_info *fs_info = root->fs_info;
1282
1283         if (pin) {
1284                 set_extent_dirty(&fs_info->pinned_extents,
1285                                 bytenr, bytenr + num - 1, GFP_NOFS);
1286         } else {
1287                 clear_extent_dirty(&fs_info->pinned_extents,
1288                                 bytenr, bytenr + num - 1, GFP_NOFS);
1289         }
1290         while (num > 0) {
1291                 cache = btrfs_lookup_block_group(fs_info, bytenr);
1292                 WARN_ON(!cache);
1293                 len = min(num, cache->key.offset -
1294                           (bytenr - cache->key.objectid));
1295                 if (pin) {
1296                         cache->pinned += len;
1297                         cache->space_info->bytes_pinned += len;
1298                         fs_info->total_pinned += len;
1299                 } else {
1300                         cache->pinned -= len;
1301                         cache->space_info->bytes_pinned -= len;
1302                         fs_info->total_pinned -= len;
1303                 }
1304                 bytenr += len;
1305                 num -= len;
1306         }
1307         return 0;
1308 }
1309
1310 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
1311 {
1312         u64 last = 0;
1313         u64 start;
1314         u64 end;
1315         struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
1316         int ret;
1317
1318         while(1) {
1319                 ret = find_first_extent_bit(pinned_extents, last,
1320                                             &start, &end, EXTENT_DIRTY);
1321                 if (ret)
1322                         break;
1323                 set_extent_dirty(copy, start, end, GFP_NOFS);
1324                 last = end + 1;
1325         }
1326         return 0;
1327 }
1328
1329 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1330                                struct btrfs_root *root,
1331                                struct extent_io_tree *unpin)
1332 {
1333         u64 start;
1334         u64 end;
1335         int ret;
1336         struct extent_io_tree *free_space_cache;
1337         free_space_cache = &root->fs_info->free_space_cache;
1338
1339         while(1) {
1340                 ret = find_first_extent_bit(unpin, 0, &start, &end,
1341                                             EXTENT_DIRTY);
1342                 if (ret)
1343                         break;
1344                 update_pinned_extents(root, start, end + 1 - start, 0);
1345                 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1346                 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
1347         }
1348         return 0;
1349 }
1350
1351 static int finish_current_insert(struct btrfs_trans_handle *trans,
1352                                  struct btrfs_root *extent_root)
1353 {
1354         u64 start;
1355         u64 end;
1356         u64 priv;
1357         struct btrfs_fs_info *info = extent_root->fs_info;
1358         struct btrfs_path *path;
1359         struct btrfs_extent_ref *ref;
1360         struct pending_extent_op *extent_op;
1361         struct btrfs_key key;
1362         struct btrfs_extent_item extent_item;
1363         int ret;
1364         int err = 0;
1365
1366         btrfs_set_stack_extent_refs(&extent_item, 1);
1367         path = btrfs_alloc_path();
1368
1369         while(1) {
1370                 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1371                                             &end, EXTENT_LOCKED);
1372                 if (ret)
1373                         break;
1374
1375                 ret = get_state_private(&info->extent_ins, start, &priv);
1376                 BUG_ON(ret);
1377                 extent_op = (struct pending_extent_op *)(unsigned long)priv;
1378
1379                 if (extent_op->type == PENDING_EXTENT_INSERT) {
1380                         key.objectid = start;
1381                         key.offset = end + 1 - start;
1382                         key.type = BTRFS_EXTENT_ITEM_KEY;
1383                         err = btrfs_insert_item(trans, extent_root, &key,
1384                                         &extent_item, sizeof(extent_item));
1385                         BUG_ON(err);
1386
1387                         clear_extent_bits(&info->extent_ins, start, end,
1388                                           EXTENT_LOCKED, GFP_NOFS);
1389
1390                         err = insert_extent_backref(trans, extent_root, path,
1391                                                 start, extent_op->parent,
1392                                                 extent_root->root_key.objectid,
1393                                                 extent_op->generation,
1394                                                 extent_op->level);
1395                         BUG_ON(err);
1396                 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
1397                         err = lookup_extent_backref(trans, extent_root, path,
1398                                                 start, extent_op->orig_parent,
1399                                                 extent_root->root_key.objectid,
1400                                                 extent_op->orig_generation,
1401                                                 extent_op->level, 0);
1402                         BUG_ON(err);
1403
1404                         clear_extent_bits(&info->extent_ins, start, end,
1405                                           EXTENT_LOCKED, GFP_NOFS);
1406
1407                         key.objectid = start;
1408                         key.offset = extent_op->parent;
1409                         key.type = BTRFS_EXTENT_REF_KEY;
1410                         err = btrfs_set_item_key_safe(trans, extent_root, path,
1411                                                       &key);
1412                         BUG_ON(err);
1413                         ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1414                                              struct btrfs_extent_ref);
1415                         btrfs_set_ref_generation(path->nodes[0], ref,
1416                                                  extent_op->generation);
1417                         btrfs_mark_buffer_dirty(path->nodes[0]);
1418                         btrfs_release_path(extent_root, path);
1419                 } else {
1420                         BUG_ON(1);
1421                 }
1422                 kfree(extent_op);
1423         }
1424         btrfs_free_path(path);
1425         return 0;
1426 }
1427
1428 static int pin_down_bytes(struct btrfs_trans_handle *trans,
1429                           struct btrfs_root *root,
1430                           u64 bytenr, u64 num_bytes, int is_data)
1431 {
1432         int err = 0;
1433         struct extent_buffer *buf;
1434
1435         if (is_data)
1436                 goto pinit;
1437
1438         buf = btrfs_find_tree_block(root, bytenr, num_bytes);
1439         if (!buf)
1440                 goto pinit;
1441
1442         /* we can reuse a block if it hasn't been written
1443          * and it is from this transaction.  We can't
1444          * reuse anything from the tree log root because
1445          * it has tiny sub-transactions.
1446          */
1447         if (btrfs_buffer_uptodate(buf, 0)) {
1448                 u64 header_owner = btrfs_header_owner(buf);
1449                 u64 header_transid = btrfs_header_generation(buf);
1450                 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
1451                     header_owner != BTRFS_TREE_RELOC_OBJECTID &&
1452                     header_transid == trans->transid &&
1453                     !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
1454                         clean_tree_block(NULL, root, buf);
1455                         free_extent_buffer(buf);
1456                         return 1;
1457                 }
1458         }
1459         free_extent_buffer(buf);
1460 pinit:
1461         update_pinned_extents(root, bytenr, num_bytes, 1);
1462
1463         BUG_ON(err < 0);
1464         return 0;
1465 }
1466
1467 /*
1468  * remove an extent from the root, returns 0 on success
1469  */
1470 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1471                          *root, u64 bytenr, u64 num_bytes, u64 parent,
1472                          u64 root_objectid, u64 ref_generation,
1473                          u64 owner_objectid, int pin, int mark_free)
1474 {
1475         struct btrfs_path *path;
1476         struct btrfs_key key;
1477         struct btrfs_fs_info *info = root->fs_info;
1478         struct btrfs_extent_ops *ops = info->extent_ops;
1479         struct btrfs_root *extent_root = info->extent_root;
1480         struct extent_buffer *leaf;
1481         int ret;
1482         int extent_slot = 0;
1483         int found_extent = 0;
1484         int num_to_del = 1;
1485         struct btrfs_extent_item *ei;
1486         u32 refs;
1487
1488         key.objectid = bytenr;
1489         btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1490         key.offset = num_bytes;
1491
1492         path = btrfs_alloc_path();
1493         if (!path)
1494                 return -ENOMEM;
1495
1496         ret = lookup_extent_backref(trans, extent_root, path,
1497                                     bytenr, parent, root_objectid,
1498                                     ref_generation, owner_objectid, 1);
1499         if (ret == 0) {
1500                 struct btrfs_key found_key;
1501                 extent_slot = path->slots[0];
1502                 while(extent_slot > 0) {
1503                         extent_slot--;
1504                         btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1505                                               extent_slot);
1506                         if (found_key.objectid != bytenr)
1507                                 break;
1508                         if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
1509                             found_key.offset == num_bytes) {
1510                                 found_extent = 1;
1511                                 break;
1512                         }
1513                         if (path->slots[0] - extent_slot > 5)
1514                                 break;
1515                 }
1516                 if (!found_extent) {
1517                         ret = remove_extent_backref(trans, extent_root, path);
1518                         BUG_ON(ret);
1519                         btrfs_release_path(extent_root, path);
1520                         ret = btrfs_search_slot(trans, extent_root,
1521                                                 &key, path, -1, 1);
1522                         BUG_ON(ret);
1523                         extent_slot = path->slots[0];
1524                 }
1525         } else {
1526                 btrfs_print_leaf(extent_root, path->nodes[0]);
1527                 printk("Unable to find ref byte nr %llu root %llu "
1528                        " gen %llu owner %llu\n",
1529                        (unsigned long long)bytenr,
1530                        (unsigned long long)root_objectid,
1531                        (unsigned long long)ref_generation,
1532                        (unsigned long long)owner_objectid);
1533                 BUG_ON(1);
1534         }
1535
1536         leaf = path->nodes[0];
1537         ei = btrfs_item_ptr(leaf, extent_slot,
1538                             struct btrfs_extent_item);
1539         refs = btrfs_extent_refs(leaf, ei);
1540         BUG_ON(refs == 0);
1541         refs -= 1;
1542         btrfs_set_extent_refs(leaf, ei, refs);
1543
1544         btrfs_mark_buffer_dirty(leaf);
1545
1546         if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
1547                 struct btrfs_extent_ref *ref;
1548                 ref = btrfs_item_ptr(leaf, path->slots[0],
1549                                      struct btrfs_extent_ref);
1550                 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
1551                 /* if the back ref and the extent are next to each other
1552                  * they get deleted below in one shot
1553                  */
1554                 path->slots[0] = extent_slot;
1555                 num_to_del = 2;
1556         } else if (found_extent) {
1557                 /* otherwise delete the extent back ref */
1558                 ret = remove_extent_backref(trans, extent_root, path);
1559                 BUG_ON(ret);
1560                 /* if refs are 0, we need to setup the path for deletion */
1561                 if (refs == 0) {
1562                         btrfs_release_path(extent_root, path);
1563                         ret = btrfs_search_slot(trans, extent_root, &key, path,
1564                                                 -1, 1);
1565                         if (ret < 0)
1566                                 return ret;
1567                         BUG_ON(ret);
1568                 }
1569         }
1570
1571         if (refs == 0) {
1572                 u64 super_used;
1573                 u64 root_used;
1574
1575                 if (pin) {
1576                         ret = pin_down_bytes(trans, root, bytenr, num_bytes, 0);
1577                         if (ret > 0)
1578                                 mark_free = 1;
1579                         BUG_ON(ret < 0);
1580                 }
1581
1582                 /* block accounting for super block */
1583                 super_used = btrfs_super_bytes_used(&info->super_copy);
1584                 btrfs_set_super_bytes_used(&info->super_copy,
1585                                            super_used - num_bytes);
1586
1587                 /* block accounting for root item */
1588                 root_used = btrfs_root_used(&root->root_item);
1589                 btrfs_set_root_used(&root->root_item,
1590                                            root_used - num_bytes);
1591                 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1592                                       num_to_del);
1593                 if (ret)
1594                         return ret;
1595
1596                 if (ops && ops->free_extent)
1597                         ops->free_extent(root, bytenr, num_bytes);
1598
1599                 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
1600                                          mark_free);
1601                 BUG_ON(ret);
1602         }
1603         btrfs_free_path(path);
1604         finish_current_insert(trans, extent_root);
1605         return ret;
1606 }
1607
1608 /*
1609  * find all the blocks marked as pending in the radix tree and remove
1610  * them from the extent map
1611  */
1612 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
1613                                btrfs_root *extent_root)
1614 {
1615         int ret;
1616         int err = 0;
1617         int mark_free = 0;
1618         u64 start;
1619         u64 end;
1620         u64 priv;
1621         struct extent_io_tree *pending_del;
1622         struct extent_io_tree *extent_ins;
1623         struct pending_extent_op *extent_op;
1624
1625         extent_ins = &extent_root->fs_info->extent_ins;
1626         pending_del = &extent_root->fs_info->pending_del;
1627
1628         while(1) {
1629                 ret = find_first_extent_bit(pending_del, 0, &start, &end,
1630                                             EXTENT_LOCKED);
1631                 if (ret)
1632                         break;
1633
1634                 ret = get_state_private(pending_del, start, &priv);
1635                 BUG_ON(ret);
1636                 extent_op = (struct pending_extent_op *)(unsigned long)priv;
1637
1638                 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
1639                                   GFP_NOFS);
1640
1641                 ret = pin_down_bytes(trans, extent_root, start,
1642                                      end + 1 - start, 0);
1643                 mark_free = ret > 0;
1644                 if (!test_range_bit(extent_ins, start, end,
1645                                     EXTENT_LOCKED, 0)) {
1646 free_extent:
1647                         ret = __free_extent(trans, extent_root,
1648                                             start, end + 1 - start,
1649                                             extent_op->orig_parent,
1650                                             extent_root->root_key.objectid,
1651                                             extent_op->orig_generation,
1652                                             extent_op->level, 0, mark_free);
1653                         kfree(extent_op);
1654                 } else {
1655                         kfree(extent_op);
1656                         ret = get_state_private(extent_ins, start, &priv);
1657                         BUG_ON(ret);
1658                         extent_op = (struct pending_extent_op *)
1659                                                         (unsigned long)priv;
1660
1661                         clear_extent_bits(extent_ins, start, end,
1662                                           EXTENT_LOCKED, GFP_NOFS);
1663
1664                         if (extent_op->type == PENDING_BACKREF_UPDATE)
1665                                 goto free_extent;
1666
1667                         ret = update_block_group(trans, extent_root, start,
1668                                                 end + 1 - start, 0, mark_free);
1669                         BUG_ON(ret);
1670                         kfree(extent_op);
1671                 }
1672                 if (ret)
1673                         err = ret;
1674         }
1675         return err;
1676 }
1677
1678 /*
1679  * remove an extent from the root, returns 0 on success
1680  */
1681 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1682                       *root, u64 bytenr, u64 num_bytes, u64 parent,
1683                       u64 root_objectid, u64 ref_generation,
1684                       u64 owner_objectid, int pin)
1685 {
1686         struct btrfs_root *extent_root = root->fs_info->extent_root;
1687         int pending_ret;
1688         int ret;
1689
1690         WARN_ON(num_bytes < root->sectorsize);
1691         if (root == extent_root) {
1692                 struct pending_extent_op *extent_op;
1693
1694                 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
1695                 BUG_ON(!extent_op);
1696
1697                 extent_op->type = PENDING_EXTENT_DELETE;
1698                 extent_op->bytenr = bytenr;
1699                 extent_op->num_bytes = num_bytes;
1700                 extent_op->parent = parent;
1701                 extent_op->orig_parent = parent;
1702                 extent_op->generation = ref_generation;
1703                 extent_op->orig_generation = ref_generation;
1704                 extent_op->level = (int)owner_objectid;
1705
1706                 set_extent_bits(&root->fs_info->pending_del,
1707                                 bytenr, bytenr + num_bytes - 1,
1708                                 EXTENT_LOCKED, GFP_NOFS);
1709                 set_state_private(&root->fs_info->pending_del,
1710                                   bytenr, (unsigned long)extent_op);
1711                 return 0;
1712         }
1713         ret = __free_extent(trans, root, bytenr, num_bytes, parent,
1714                             root_objectid, ref_generation,
1715                             owner_objectid, pin, pin == 0);
1716         pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
1717         return ret ? ret : pending_ret;
1718 }
1719
1720 static u64 stripe_align(struct btrfs_root *root, u64 val)
1721 {
1722         u64 mask = ((u64)root->stripesize - 1);
1723         u64 ret = (val + mask) & ~mask;
1724         return ret;
1725 }
1726
1727 /*
1728  * walks the btree of allocated extents and find a hole of a given size.
1729  * The key ins is changed to record the hole:
1730  * ins->objectid == block start
1731  * ins->flags = BTRFS_EXTENT_ITEM_KEY
1732  * ins->offset == number of blocks
1733  * Any available blocks before search_start are skipped.
1734  */
1735 static int noinline find_free_extent(struct btrfs_trans_handle *trans,
1736                                      struct btrfs_root *orig_root,
1737                                      u64 num_bytes, u64 empty_size,
1738                                      u64 search_start, u64 search_end,
1739                                      u64 hint_byte, struct btrfs_key *ins,
1740                                      u64 exclude_start, u64 exclude_nr,
1741                                      int data)
1742 {
1743         int ret;
1744         u64 orig_search_start = search_start;
1745         struct btrfs_root * root = orig_root->fs_info->extent_root;
1746         struct btrfs_fs_info *info = root->fs_info;
1747         u64 total_needed = num_bytes;
1748         struct btrfs_block_group_cache *block_group;
1749         int full_scan = 0;
1750         int wrapped = 0;
1751
1752         WARN_ON(num_bytes < root->sectorsize);
1753         btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1754
1755         if (hint_byte) {
1756                 block_group = btrfs_lookup_first_block_group(info, hint_byte);
1757                 if (!block_group)
1758                         hint_byte = search_start;
1759                 block_group = btrfs_find_block_group(root, block_group,
1760                                                      hint_byte, data, 1);
1761         } else {
1762                 block_group = btrfs_find_block_group(root,
1763                                                      trans->block_group,
1764                                                      search_start, data, 1);
1765         }
1766
1767         total_needed += empty_size;
1768
1769 check_failed:
1770         if (!block_group) {
1771                 block_group = btrfs_lookup_first_block_group(info,
1772                                                              search_start);
1773                 if (!block_group)
1774                         block_group = btrfs_lookup_first_block_group(info,
1775                                                        orig_search_start);
1776         }
1777         ret = find_search_start(root, &block_group, &search_start,
1778                                 total_needed, data);
1779         if (ret)
1780                 goto error;
1781
1782         search_start = stripe_align(root, search_start);
1783         ins->objectid = search_start;
1784         ins->offset = num_bytes;
1785
1786         if (ins->objectid + num_bytes >
1787             block_group->key.objectid + block_group->key.offset) {
1788                 search_start = block_group->key.objectid +
1789                         block_group->key.offset;
1790                 goto new_group;
1791         }
1792
1793         if (test_range_bit(&info->extent_ins, ins->objectid,
1794                            ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1795                 search_start = ins->objectid + num_bytes;
1796                 goto new_group;
1797         }
1798
1799         if (test_range_bit(&info->pinned_extents, ins->objectid,
1800                            ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1801                 search_start = ins->objectid + num_bytes;
1802                 goto new_group;
1803         }
1804
1805         if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
1806             ins->objectid < exclude_start + exclude_nr)) {
1807                 search_start = exclude_start + exclude_nr;
1808                 goto new_group;
1809         }
1810
1811         if (!(data & BTRFS_BLOCK_GROUP_DATA)) {
1812                 block_group = btrfs_lookup_block_group(info, ins->objectid);
1813                 if (block_group)
1814                         trans->block_group = block_group;
1815         }
1816         ins->offset = num_bytes;
1817         return 0;
1818
1819 new_group:
1820         block_group = btrfs_lookup_first_block_group(info, search_start);
1821         if (!block_group) {
1822                 search_start = orig_search_start;
1823                 if (full_scan) {
1824                         ret = -ENOSPC;
1825                         goto error;
1826                 }
1827                 if (wrapped) {
1828                         if (!full_scan)
1829                                 total_needed -= empty_size;
1830                         full_scan = 1;
1831                 } else
1832                         wrapped = 1;
1833         }
1834         cond_resched();
1835         block_group = btrfs_find_block_group(root, block_group,
1836                                              search_start, data, 0);
1837         goto check_failed;
1838
1839 error:
1840         return ret;
1841 }
1842 /*
1843  * finds a free extent and does all the dirty work required for allocation
1844  * returns the key for the extent through ins, and a tree buffer for
1845  * the first block of the extent through buf.
1846  *
1847  * returns 0 if everything worked, non-zero otherwise.
1848  */
1849 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1850                        struct btrfs_root *root,
1851                        u64 num_bytes, u64 parent,
1852                        u64 root_objectid, u64 ref_generation,
1853                        u64 owner, u64 empty_size, u64 hint_byte,
1854                        u64 search_end, struct btrfs_key *ins, int data)
1855 {
1856         int ret;
1857         int pending_ret;
1858         u64 super_used, root_used;
1859         u64 search_start = 0;
1860         u64 alloc_profile;
1861         u32 sizes[2];
1862         struct btrfs_fs_info *info = root->fs_info;
1863         struct btrfs_root *extent_root = info->extent_root;
1864         struct btrfs_path *path;
1865         struct btrfs_extent_item *extent_item;
1866         struct btrfs_extent_ref *ref;
1867         struct btrfs_key keys[2];
1868
1869         if (info->extent_ops) {
1870                 struct btrfs_extent_ops *ops = info->extent_ops;
1871                 ret = ops->alloc_extent(root, num_bytes, hint_byte, ins);
1872                 BUG_ON(ret);
1873                 goto found;
1874         }
1875
1876         if (data) {
1877                 alloc_profile = info->avail_data_alloc_bits &
1878                                 info->data_alloc_profile;
1879                 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
1880         } else if ((info->system_allocs > 0 || root == info->chunk_root) &&
1881                    info->system_allocs >= 0) {
1882                 alloc_profile = info->avail_system_alloc_bits &
1883                                 info->system_alloc_profile;
1884                 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
1885         } else {
1886                 alloc_profile = info->avail_metadata_alloc_bits &
1887                                 info->metadata_alloc_profile;
1888                 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
1889         }
1890
1891         if (root->ref_cows) {
1892                 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
1893                         ret = do_chunk_alloc(trans, root->fs_info->extent_root,
1894                                              num_bytes,
1895                                              BTRFS_BLOCK_GROUP_METADATA);
1896                         BUG_ON(ret);
1897                 }
1898                 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
1899                                      num_bytes + 2 * 1024 * 1024, data);
1900                 BUG_ON(ret);
1901         }
1902
1903         WARN_ON(num_bytes < root->sectorsize);
1904         ret = find_free_extent(trans, root, num_bytes, empty_size,
1905                                search_start, search_end, hint_byte, ins,
1906                                trans->alloc_exclude_start,
1907                                trans->alloc_exclude_nr, data);
1908         BUG_ON(ret);
1909 found:
1910         if (ret)
1911                 return ret;
1912
1913         if (parent == 0)
1914                 parent = ins->objectid;
1915
1916         /* block accounting for super block */
1917         super_used = btrfs_super_bytes_used(&info->super_copy);
1918         btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
1919
1920         /* block accounting for root item */
1921         root_used = btrfs_root_used(&root->root_item);
1922         btrfs_set_root_used(&root->root_item, root_used + num_bytes);
1923
1924         clear_extent_dirty(&root->fs_info->free_space_cache,
1925                            ins->objectid, ins->objectid + ins->offset - 1,
1926                            GFP_NOFS);
1927
1928         if (root == extent_root) {
1929                 struct pending_extent_op *extent_op;
1930
1931                 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
1932                 BUG_ON(!extent_op);
1933
1934                 extent_op->type = PENDING_EXTENT_INSERT;
1935                 extent_op->bytenr = ins->objectid;
1936                 extent_op->num_bytes = ins->offset;
1937                 extent_op->parent = parent;
1938                 extent_op->orig_parent = 0;
1939                 extent_op->generation = ref_generation;
1940                 extent_op->orig_generation = 0;
1941                 extent_op->level = (int)owner;
1942
1943                 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1944                                 ins->objectid + ins->offset - 1,
1945                                 EXTENT_LOCKED, GFP_NOFS);
1946                 set_state_private(&root->fs_info->extent_ins,
1947                                   ins->objectid, (unsigned long)extent_op);
1948                 goto update_block;
1949         }
1950
1951         WARN_ON(trans->alloc_exclude_nr);
1952         trans->alloc_exclude_start = ins->objectid;
1953         trans->alloc_exclude_nr = ins->offset;
1954
1955         memcpy(&keys[0], ins, sizeof(*ins));
1956         keys[1].objectid = ins->objectid;
1957         keys[1].type = BTRFS_EXTENT_REF_KEY;
1958         keys[1].offset = parent;
1959         sizes[0] = sizeof(*extent_item);
1960         sizes[1] = sizeof(*ref);
1961
1962         path = btrfs_alloc_path();
1963         BUG_ON(!path);
1964
1965         ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
1966                                        sizes, 2);
1967
1968         BUG_ON(ret);
1969         extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1970                                      struct btrfs_extent_item);
1971         btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
1972         ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1973                              struct btrfs_extent_ref);
1974
1975         btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
1976         btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
1977         btrfs_set_ref_objectid(path->nodes[0], ref, owner);
1978         btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
1979
1980         btrfs_mark_buffer_dirty(path->nodes[0]);
1981
1982         trans->alloc_exclude_start = 0;
1983         trans->alloc_exclude_nr = 0;
1984         btrfs_free_path(path);
1985         finish_current_insert(trans, extent_root);
1986         pending_ret = del_pending_extents(trans, extent_root);
1987
1988         if (ret) {
1989                 return ret;
1990         }
1991         if (pending_ret) {
1992                 return pending_ret;
1993         }
1994
1995 update_block:
1996         ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
1997         if (ret) {
1998                 printk("update block group failed for %llu %llu\n",
1999                        (unsigned long long)ins->objectid,
2000                        (unsigned long long)ins->offset);
2001                 BUG();
2002         }
2003         return 0;
2004 }
2005
2006 /*
2007  * helper function to allocate a block for a given tree
2008  * returns the tree buffer or NULL.
2009  */
2010 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
2011                                              struct btrfs_root *root,
2012                                              u32 blocksize, u64 parent,
2013                                              u64 root_objectid,
2014                                              u64 ref_generation,
2015                                              int level,
2016                                              u64 hint,
2017                                              u64 empty_size)
2018 {
2019         struct btrfs_key ins;
2020         int ret;
2021         struct extent_buffer *buf;
2022
2023         ret = btrfs_alloc_extent(trans, root, blocksize, parent,
2024                                  root_objectid, ref_generation,
2025                                  level, empty_size, hint,
2026                                  (u64)-1, &ins, 0);
2027         if (ret) {
2028                 BUG_ON(ret > 0);
2029                 return ERR_PTR(ret);
2030         }
2031         buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
2032         if (!buf) {
2033                 if (parent == 0)
2034                         parent = ins.objectid;
2035                 btrfs_free_extent(trans, root, ins.objectid, blocksize,
2036                                   parent, root->root_key.objectid,
2037                                   ref_generation, level, 0);
2038                 BUG_ON(1);
2039                 return ERR_PTR(-ENOMEM);
2040         }
2041         btrfs_set_buffer_uptodate(buf);
2042         trans->blocks_used++;
2043         return buf;
2044 }
2045
2046 static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
2047                                   struct btrfs_root *root,
2048                                   struct extent_buffer *leaf)
2049 {
2050         u64 leaf_owner;
2051         u64 leaf_generation;
2052         struct btrfs_key key;
2053         struct btrfs_file_extent_item *fi;
2054         int i;
2055         int nritems;
2056         int ret;
2057
2058         BUG_ON(!btrfs_is_leaf(leaf));
2059         nritems = btrfs_header_nritems(leaf);
2060         leaf_owner = btrfs_header_owner(leaf);
2061         leaf_generation = btrfs_header_generation(leaf);
2062
2063         for (i = 0; i < nritems; i++) {
2064                 u64 disk_bytenr;
2065
2066                 btrfs_item_key_to_cpu(leaf, &key, i);
2067                 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2068                         continue;
2069                 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
2070                 if (btrfs_file_extent_type(leaf, fi) ==
2071                     BTRFS_FILE_EXTENT_INLINE)
2072                         continue;
2073                 /*
2074                  * FIXME make sure to insert a trans record that
2075                  * repeats the snapshot del on crash
2076                  */
2077                 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2078                 if (disk_bytenr == 0)
2079                         continue;
2080                 ret = btrfs_free_extent(trans, root, disk_bytenr,
2081                                 btrfs_file_extent_disk_num_bytes(leaf, fi),
2082                                 leaf->start, leaf_owner, leaf_generation,
2083                                 key.objectid, 0);
2084                 BUG_ON(ret);
2085         }
2086         return 0;
2087 }
2088
2089 static void noinline reada_walk_down(struct btrfs_root *root,
2090                                      struct extent_buffer *node,
2091                                      int slot)
2092 {
2093         u64 bytenr;
2094         u64 last = 0;
2095         u32 nritems;
2096         u32 refs;
2097         u32 blocksize;
2098         int ret;
2099         int i;
2100         int level;
2101         int skipped = 0;
2102
2103         nritems = btrfs_header_nritems(node);
2104         level = btrfs_header_level(node);
2105         if (level)
2106                 return;
2107
2108         for (i = slot; i < nritems && skipped < 32; i++) {
2109                 bytenr = btrfs_node_blockptr(node, i);
2110                 if (last && ((bytenr > last && bytenr - last > 32 * 1024) ||
2111                              (last > bytenr && last - bytenr > 32 * 1024))) {
2112                         skipped++;
2113                         continue;
2114                 }
2115                 blocksize = btrfs_level_size(root, level - 1);
2116                 if (i != slot) {
2117                         ret = lookup_extent_ref(NULL, root, bytenr,
2118                                                 blocksize, &refs);
2119                         BUG_ON(ret);
2120                         if (refs != 1) {
2121                                 skipped++;
2122                                 continue;
2123                         }
2124                 }
2125                 mutex_unlock(&root->fs_info->fs_mutex);
2126                 ret = readahead_tree_block(root, bytenr, blocksize,
2127                                            btrfs_node_ptr_generation(node, i));
2128                 last = bytenr + blocksize;
2129                 cond_resched();
2130                 mutex_lock(&root->fs_info->fs_mutex);
2131                 if (ret)
2132                         break;
2133         }
2134 }
2135
2136 /*
2137  * helper function for drop_snapshot, this walks down the tree dropping ref
2138  * counts as it goes.
2139  */
2140 static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2141                                    struct btrfs_root *root,
2142                                    struct btrfs_path *path, int *level)
2143 {
2144         u64 root_owner;
2145         u64 root_gen;
2146         u64 bytenr;
2147         u64 ptr_gen;
2148         struct extent_buffer *next;
2149         struct extent_buffer *cur;
2150         struct extent_buffer *parent;
2151         u32 blocksize;
2152         int ret;
2153         u32 refs;
2154
2155         WARN_ON(*level < 0);
2156         WARN_ON(*level >= BTRFS_MAX_LEVEL);
2157         ret = lookup_extent_ref(trans, root,
2158                                 path->nodes[*level]->start,
2159                                 path->nodes[*level]->len, &refs);
2160         BUG_ON(ret);
2161         if (refs > 1)
2162                 goto out;
2163
2164         /*
2165          * walk down to the last node level and free all the leaves
2166          */
2167         while(*level >= 0) {
2168                 WARN_ON(*level < 0);
2169                 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2170                 cur = path->nodes[*level];
2171
2172                 if (btrfs_header_level(cur) != *level)
2173                         WARN_ON(1);
2174
2175                 if (path->slots[*level] >=
2176                     btrfs_header_nritems(cur))
2177                         break;
2178                 if (*level == 0) {
2179                         ret = drop_leaf_ref(trans, root, cur);
2180                         BUG_ON(ret);
2181                         break;
2182                 }
2183                 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2184                 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2185                 blocksize = btrfs_level_size(root, *level - 1);
2186                 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
2187                 BUG_ON(ret);
2188                 if (refs != 1) {
2189                         parent = path->nodes[*level];
2190                         root_owner = btrfs_header_owner(parent);
2191                         root_gen = btrfs_header_generation(parent);
2192                         path->slots[*level]++;
2193                         ret = btrfs_free_extent(trans, root, bytenr, blocksize,
2194                                                 parent->start, root_owner,
2195                                                 root_gen, *level - 1, 1);
2196                         BUG_ON(ret);
2197                         continue;
2198                 }
2199                 next = btrfs_find_tree_block(root, bytenr, blocksize);
2200                 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
2201                         free_extent_buffer(next);
2202                         reada_walk_down(root, cur, path->slots[*level]);
2203                         mutex_unlock(&root->fs_info->fs_mutex);
2204                         next = read_tree_block(root, bytenr, blocksize,
2205                                                ptr_gen);
2206                         mutex_lock(&root->fs_info->fs_mutex);
2207                 }
2208                 WARN_ON(*level <= 0);
2209                 if (path->nodes[*level-1])
2210                         free_extent_buffer(path->nodes[*level-1]);
2211                 path->nodes[*level-1] = next;
2212                 *level = btrfs_header_level(next);
2213                 path->slots[*level] = 0;
2214         }
2215 out:
2216         WARN_ON(*level < 0);
2217         WARN_ON(*level >= BTRFS_MAX_LEVEL);
2218
2219         if (path->nodes[*level] == root->node) {
2220                 root_owner = root->root_key.objectid;
2221                 parent = path->nodes[*level];
2222         } else {
2223                 parent = path->nodes[*level + 1];
2224                 root_owner = btrfs_header_owner(parent);
2225         }
2226
2227         root_gen = btrfs_header_generation(parent);
2228         ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
2229                                 path->nodes[*level]->len, parent->start,
2230                                 root_owner, root_gen, *level, 1);
2231         free_extent_buffer(path->nodes[*level]);
2232         path->nodes[*level] = NULL;
2233         *level += 1;
2234         BUG_ON(ret);
2235         return 0;
2236 }
2237
2238 /*
2239  * helper for dropping snapshots.  This walks back up the tree in the path
2240  * to find the first node higher up where we haven't yet gone through
2241  * all the slots
2242  */
2243 static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
2244                                  struct btrfs_root *root,
2245                                  struct btrfs_path *path, int *level)
2246 {
2247         u64 root_owner;
2248         u64 root_gen;
2249         struct btrfs_root_item *root_item = &root->root_item;
2250         int i;
2251         int slot;
2252         int ret;
2253
2254         for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
2255                 slot = path->slots[i];
2256                 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
2257                         struct extent_buffer *node;
2258                         struct btrfs_disk_key disk_key;
2259                         node = path->nodes[i];
2260                         path->slots[i]++;
2261                         *level = i;
2262                         WARN_ON(*level == 0);
2263                         btrfs_node_key(node, &disk_key, path->slots[i]);
2264                         memcpy(&root_item->drop_progress,
2265                                &disk_key, sizeof(disk_key));
2266                         root_item->drop_level = i;
2267                         return 0;
2268                 } else {
2269                         struct extent_buffer *parent;
2270                         if (path->nodes[*level] == root->node)
2271                                 parent = path->nodes[*level];
2272                         else
2273                                 parent = path->nodes[*level + 1];
2274
2275                         root_owner = btrfs_header_owner(parent);
2276                         root_gen = btrfs_header_generation(parent);
2277                         ret = btrfs_free_extent(trans, root,
2278                                                 path->nodes[*level]->start,
2279                                                 path->nodes[*level]->len,
2280                                                 parent->start, root_owner,
2281                                                 root_gen, *level, 1);
2282                         BUG_ON(ret);
2283                         free_extent_buffer(path->nodes[*level]);
2284                         path->nodes[*level] = NULL;
2285                         *level = i + 1;
2286                 }
2287         }
2288         return 1;
2289 }
2290
2291 /*
2292  * drop the reference count on the tree rooted at 'snap'.  This traverses
2293  * the tree freeing any blocks that have a ref count of zero after being
2294  * decremented.
2295  */
2296 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
2297                         *root)
2298 {
2299         int ret = 0;
2300         int wret;
2301         int level;
2302         struct btrfs_path *path;
2303         int i;
2304         int orig_level;
2305         struct btrfs_root_item *root_item = &root->root_item;
2306
2307         path = btrfs_alloc_path();
2308         BUG_ON(!path);
2309
2310         level = btrfs_header_level(root->node);
2311         orig_level = level;
2312         if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2313                 path->nodes[level] = root->node;
2314                 extent_buffer_get(root->node);
2315                 path->slots[level] = 0;
2316         } else {
2317                 struct btrfs_key key;
2318                 struct btrfs_disk_key found_key;
2319                 struct extent_buffer *node;
2320
2321                 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
2322                 level = root_item->drop_level;
2323                 path->lowest_level = level;
2324                 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2325                 if (wret < 0) {
2326                         ret = wret;
2327                         goto out;
2328                 }
2329                 node = path->nodes[level];
2330                 btrfs_node_key(node, &found_key, path->slots[level]);
2331                 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2332                                sizeof(found_key)));
2333         }
2334         while(1) {
2335                 wret = walk_down_tree(trans, root, path, &level);
2336                 if (wret < 0)
2337                         ret = wret;
2338                 if (wret != 0)
2339                         break;
2340
2341                 wret = walk_up_tree(trans, root, path, &level);
2342                 if (wret < 0)
2343                         ret = wret;
2344                 if (wret != 0)
2345                         break;
2346                 /*
2347                 ret = -EAGAIN;
2348                 break;
2349                 */
2350         }
2351         for (i = 0; i <= orig_level; i++) {
2352                 if (path->nodes[i]) {
2353                         free_extent_buffer(path->nodes[i]);
2354                         path->nodes[i] = NULL;
2355                 }
2356         }
2357 out:
2358         btrfs_free_path(path);
2359         return ret;
2360 }
2361
2362 int btrfs_free_block_groups(struct btrfs_fs_info *info)
2363 {
2364         u64 start;
2365         u64 end;
2366         u64 ptr;
2367         int ret;
2368         while(1) {
2369                 ret = find_first_extent_bit(&info->block_group_cache, 0,
2370                                             &start, &end, (unsigned int)-1);
2371                 if (ret)
2372                         break;
2373                 ret = get_state_private(&info->block_group_cache, start, &ptr);
2374                 if (!ret)
2375                         kfree((void *)(unsigned long)ptr);
2376                 clear_extent_bits(&info->block_group_cache, start,
2377                                   end, (unsigned int)-1, GFP_NOFS);
2378         }
2379         while(1) {
2380                 ret = find_first_extent_bit(&info->free_space_cache, 0,
2381                                             &start, &end, EXTENT_DIRTY);
2382                 if (ret)
2383                         break;
2384                 clear_extent_dirty(&info->free_space_cache, start,
2385                                    end, GFP_NOFS);
2386         }
2387         return 0;
2388 }
2389
2390 int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
2391                            struct btrfs_key *key)
2392 {
2393         int ret;
2394         struct btrfs_key found_key;
2395         struct extent_buffer *leaf;
2396         int slot;
2397
2398         ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
2399         if (ret < 0)
2400                 return ret;
2401         while(1) {
2402                 slot = path->slots[0];
2403                 leaf = path->nodes[0];
2404                 if (slot >= btrfs_header_nritems(leaf)) {
2405                         ret = btrfs_next_leaf(root, path);
2406                         if (ret == 0)
2407                                 continue;
2408                         if (ret < 0)
2409                                 goto error;
2410                         break;
2411                 }
2412                 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2413
2414                 if (found_key.objectid >= key->objectid &&
2415                     found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
2416                         return 0;
2417                 path->slots[0]++;
2418         }
2419         ret = -ENOENT;
2420 error:
2421         return ret;
2422 }
2423
2424 int btrfs_read_block_groups(struct btrfs_root *root)
2425 {
2426         struct btrfs_path *path;
2427         int ret;
2428         int bit;
2429         struct btrfs_block_group_cache *cache;
2430         struct btrfs_fs_info *info = root->fs_info;
2431         struct btrfs_space_info *space_info;
2432         struct extent_io_tree *block_group_cache;
2433         struct btrfs_key key;
2434         struct btrfs_key found_key;
2435         struct extent_buffer *leaf;
2436
2437         block_group_cache = &info->block_group_cache;
2438
2439         root = info->extent_root;
2440         key.objectid = 0;
2441         key.offset = 0;
2442         btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2443         path = btrfs_alloc_path();
2444         if (!path)
2445                 return -ENOMEM;
2446
2447         while(1) {
2448                 ret = find_first_block_group(root, path, &key);
2449                 if (ret > 0) {
2450                         ret = 0;
2451                         goto error;
2452                 }
2453                 if (ret != 0) {
2454                         goto error;
2455                 }
2456                 leaf = path->nodes[0];
2457                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2458                 cache = kzalloc(sizeof(*cache), GFP_NOFS);
2459                 if (!cache) {
2460                         ret = -ENOMEM;
2461                         break;
2462                 }
2463
2464                 read_extent_buffer(leaf, &cache->item,
2465                                    btrfs_item_ptr_offset(leaf, path->slots[0]),
2466                                    sizeof(cache->item));
2467                 memcpy(&cache->key, &found_key, sizeof(found_key));
2468                 cache->cached = 0;
2469                 cache->pinned = 0;
2470                 key.objectid = found_key.objectid + found_key.offset;
2471                 btrfs_release_path(root, path);
2472                 cache->flags = btrfs_block_group_flags(&cache->item);
2473                 bit = 0;
2474                 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
2475                         bit = BLOCK_GROUP_DATA;
2476                 } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
2477                         bit = BLOCK_GROUP_SYSTEM;
2478                 } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) {
2479                         bit = BLOCK_GROUP_METADATA;
2480                 }
2481                 set_avail_alloc_bits(info, cache->flags);
2482                 if (btrfs_chunk_readonly(root, cache->key.objectid))
2483                         cache->ro = 1;
2484
2485                 ret = update_space_info(info, cache->flags, found_key.offset,
2486                                         btrfs_block_group_used(&cache->item),
2487                                         &space_info);
2488                 BUG_ON(ret);
2489                 cache->space_info = space_info;
2490
2491                 /* use EXTENT_LOCKED to prevent merging */
2492                 set_extent_bits(block_group_cache, found_key.objectid,
2493                                 found_key.objectid + found_key.offset - 1,
2494                                 bit | EXTENT_LOCKED, GFP_NOFS);
2495                 set_state_private(block_group_cache, found_key.objectid,
2496                                   (unsigned long)cache);
2497         }
2498         ret = 0;
2499 error:
2500         btrfs_free_path(path);
2501         return ret;
2502 }
2503
2504 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
2505                            struct btrfs_root *root, u64 bytes_used,
2506                            u64 type, u64 chunk_objectid, u64 chunk_offset,
2507                            u64 size)
2508 {
2509         int ret;
2510         int bit = 0;
2511         struct btrfs_root *extent_root;
2512         struct btrfs_block_group_cache *cache;
2513         struct extent_io_tree *block_group_cache;
2514
2515         extent_root = root->fs_info->extent_root;
2516         block_group_cache = &root->fs_info->block_group_cache;
2517
2518         cache = kzalloc(sizeof(*cache), GFP_NOFS);
2519         BUG_ON(!cache);
2520         cache->key.objectid = chunk_offset;
2521         cache->key.offset = size;
2522
2523         btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2524         btrfs_set_block_group_used(&cache->item, bytes_used);
2525         btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
2526         cache->flags = type;
2527         btrfs_set_block_group_flags(&cache->item, type);
2528
2529         ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
2530                                 &cache->space_info);
2531         BUG_ON(ret);
2532
2533         bit = block_group_state_bits(type);
2534         set_extent_bits(block_group_cache, chunk_offset,
2535                         chunk_offset + size - 1,
2536                         bit | EXTENT_LOCKED, GFP_NOFS);
2537
2538         set_state_private(block_group_cache, chunk_offset,
2539                           (unsigned long)cache);
2540         ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
2541                                 sizeof(cache->item));
2542         BUG_ON(ret);
2543
2544         finish_current_insert(trans, extent_root);
2545         ret = del_pending_extents(trans, extent_root);
2546         BUG_ON(ret);
2547         set_avail_alloc_bits(extent_root->fs_info, type);
2548         return 0;
2549 }
2550
2551 /*
2552  * This is for converter use only.
2553  *
2554  * In that case, we don't know where are free blocks located.
2555  * Therefore all block group cache entries must be setup properly
2556  * before doing any block allocation.
2557  */
2558 int btrfs_make_block_groups(struct btrfs_trans_handle *trans,
2559                             struct btrfs_root *root)
2560 {
2561         u64 total_bytes;
2562         u64 cur_start;
2563         u64 group_type;
2564         u64 group_size;
2565         u64 group_align;
2566         u64 total_data = 0;
2567         u64 total_metadata = 0;
2568         u64 chunk_objectid;
2569         int ret;
2570         int bit;
2571         struct btrfs_root *extent_root;
2572         struct btrfs_block_group_cache *cache;
2573         struct extent_io_tree *block_group_cache;
2574
2575         extent_root = root->fs_info->extent_root;
2576         block_group_cache = &root->fs_info->block_group_cache;
2577         chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2578         total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
2579         group_align = 64 * root->sectorsize;
2580
2581         cur_start = 0;
2582         while (cur_start < total_bytes) {
2583                 group_size = total_bytes / 12;
2584                 group_size = min_t(u64, group_size, total_bytes - cur_start);
2585                 if (cur_start == 0) {
2586                         bit = BLOCK_GROUP_SYSTEM;
2587                         group_type = BTRFS_BLOCK_GROUP_SYSTEM;
2588                         group_size /= 4;
2589                         group_size &= ~(group_align - 1);
2590                         group_size = max_t(u64, group_size, 32 * 1024 * 1024);
2591                         group_size = min_t(u64, group_size, 128 * 1024 * 1024);
2592                 } else {
2593                         group_size &= ~(group_align - 1);
2594                         if (total_data >= total_metadata * 2) {
2595                                 group_type = BTRFS_BLOCK_GROUP_METADATA;
2596                                 group_size = min_t(u64, group_size,
2597                                                    1ULL * 1024 * 1024 * 1024);
2598                                 total_metadata += group_size;
2599                         } else {
2600                                 group_type = BTRFS_BLOCK_GROUP_DATA;
2601                                 group_size = min_t(u64, group_size,
2602                                                    5ULL * 1024 * 1024 * 1024);
2603                                 total_data += group_size;
2604                         }
2605                         if ((total_bytes - cur_start) * 4 < group_size * 5)
2606                                 group_size = total_bytes - cur_start;
2607                 }
2608
2609                 cache = kzalloc(sizeof(*cache), GFP_NOFS);
2610                 BUG_ON(!cache);
2611
2612                 cache->key.objectid = cur_start;
2613                 cache->key.offset = group_size;
2614                 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2615
2616                 btrfs_set_block_group_used(&cache->item, 0);
2617                 btrfs_set_block_group_chunk_objectid(&cache->item,
2618                                                      chunk_objectid);
2619                 btrfs_set_block_group_flags(&cache->item, group_type);
2620
2621                 cache->flags = group_type;
2622
2623                 ret = update_space_info(root->fs_info, group_type, group_size,
2624                                         0, &cache->space_info);
2625                 BUG_ON(ret);
2626                 set_avail_alloc_bits(extent_root->fs_info, group_type);
2627
2628                 set_extent_bits(block_group_cache, cur_start,
2629                                 cur_start + group_size - 1,
2630                                 bit | EXTENT_LOCKED, GFP_NOFS);
2631                 set_state_private(block_group_cache, cur_start,
2632                                   (unsigned long)cache);
2633                 cur_start += group_size;
2634         }
2635         /* then insert all the items */
2636         cur_start = 0;
2637         while(cur_start < total_bytes) {
2638                 cache = btrfs_lookup_block_group(root->fs_info, cur_start);
2639                 BUG_ON(!cache);
2640
2641                 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
2642                                         sizeof(cache->item));
2643                 BUG_ON(ret);
2644
2645                 finish_current_insert(trans, extent_root);
2646                 ret = del_pending_extents(trans, extent_root);
2647                 BUG_ON(ret);
2648
2649                 cur_start = cache->key.objectid + cache->key.offset;
2650         }
2651         return 0;
2652 }
2653
2654 int btrfs_update_block_group(struct btrfs_trans_handle *trans,
2655                              struct btrfs_root *root,
2656                              u64 bytenr, u64 num_bytes, int alloc,
2657                              int mark_free)
2658 {
2659         return update_block_group(trans, root, bytenr, num_bytes,
2660                                   alloc, mark_free);
2661 }