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