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