Btrfs: Make btrfs_drop_snapshot work in larger and more efficient chunks
[safe/jmp/linux-2.6] / fs / btrfs / 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 #include <linux/sched.h>
19 #include <linux/pagemap.h>
20 #include <linux/writeback.h>
21 #include <linux/blkdev.h>
22 #include <linux/sort.h>
23 #include "compat.h"
24 #include "hash.h"
25 #include "crc32c.h"
26 #include "ctree.h"
27 #include "disk-io.h"
28 #include "print-tree.h"
29 #include "transaction.h"
30 #include "volumes.h"
31 #include "locking.h"
32 #include "ref-cache.h"
33
34 #define PENDING_EXTENT_INSERT 0
35 #define PENDING_EXTENT_DELETE 1
36 #define PENDING_BACKREF_UPDATE 2
37
38 struct pending_extent_op {
39         int type;
40         u64 bytenr;
41         u64 num_bytes;
42         u64 parent;
43         u64 orig_parent;
44         u64 generation;
45         u64 orig_generation;
46         int level;
47         struct list_head list;
48         int del;
49 };
50
51 static int finish_current_insert(struct btrfs_trans_handle *trans,
52                                  struct btrfs_root *extent_root, int all);
53 static int del_pending_extents(struct btrfs_trans_handle *trans,
54                                struct btrfs_root *extent_root, int all);
55 static int pin_down_bytes(struct btrfs_trans_handle *trans,
56                           struct btrfs_root *root,
57                           u64 bytenr, u64 num_bytes, int is_data);
58 static int update_block_group(struct btrfs_trans_handle *trans,
59                               struct btrfs_root *root,
60                               u64 bytenr, u64 num_bytes, int alloc,
61                               int mark_free);
62
63 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
64 {
65         return (cache->flags & bits) == bits;
66 }
67
68 /*
69  * this adds the block group to the fs_info rb tree for the block group
70  * cache
71  */
72 static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
73                                 struct btrfs_block_group_cache *block_group)
74 {
75         struct rb_node **p;
76         struct rb_node *parent = NULL;
77         struct btrfs_block_group_cache *cache;
78
79         spin_lock(&info->block_group_cache_lock);
80         p = &info->block_group_cache_tree.rb_node;
81
82         while (*p) {
83                 parent = *p;
84                 cache = rb_entry(parent, struct btrfs_block_group_cache,
85                                  cache_node);
86                 if (block_group->key.objectid < cache->key.objectid) {
87                         p = &(*p)->rb_left;
88                 } else if (block_group->key.objectid > cache->key.objectid) {
89                         p = &(*p)->rb_right;
90                 } else {
91                         spin_unlock(&info->block_group_cache_lock);
92                         return -EEXIST;
93                 }
94         }
95
96         rb_link_node(&block_group->cache_node, parent, p);
97         rb_insert_color(&block_group->cache_node,
98                         &info->block_group_cache_tree);
99         spin_unlock(&info->block_group_cache_lock);
100
101         return 0;
102 }
103
104 /*
105  * This will return the block group at or after bytenr if contains is 0, else
106  * it will return the block group that contains the bytenr
107  */
108 static struct btrfs_block_group_cache *
109 block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
110                               int contains)
111 {
112         struct btrfs_block_group_cache *cache, *ret = NULL;
113         struct rb_node *n;
114         u64 end, start;
115
116         spin_lock(&info->block_group_cache_lock);
117         n = info->block_group_cache_tree.rb_node;
118
119         while (n) {
120                 cache = rb_entry(n, struct btrfs_block_group_cache,
121                                  cache_node);
122                 end = cache->key.objectid + cache->key.offset - 1;
123                 start = cache->key.objectid;
124
125                 if (bytenr < start) {
126                         if (!contains && (!ret || start < ret->key.objectid))
127                                 ret = cache;
128                         n = n->rb_left;
129                 } else if (bytenr > start) {
130                         if (contains && bytenr <= end) {
131                                 ret = cache;
132                                 break;
133                         }
134                         n = n->rb_right;
135                 } else {
136                         ret = cache;
137                         break;
138                 }
139         }
140         if (ret)
141                 atomic_inc(&ret->count);
142         spin_unlock(&info->block_group_cache_lock);
143
144         return ret;
145 }
146
147 /*
148  * this is only called by cache_block_group, since we could have freed extents
149  * we need to check the pinned_extents for any extents that can't be used yet
150  * since their free space will be released as soon as the transaction commits.
151  */
152 static int add_new_free_space(struct btrfs_block_group_cache *block_group,
153                               struct btrfs_fs_info *info, u64 start, u64 end)
154 {
155         u64 extent_start, extent_end, size;
156         int ret;
157
158         mutex_lock(&info->pinned_mutex);
159         while (start < end) {
160                 ret = find_first_extent_bit(&info->pinned_extents, start,
161                                             &extent_start, &extent_end,
162                                             EXTENT_DIRTY);
163                 if (ret)
164                         break;
165
166                 if (extent_start == start) {
167                         start = extent_end + 1;
168                 } else if (extent_start > start && extent_start < end) {
169                         size = extent_start - start;
170                         ret = btrfs_add_free_space(block_group, start,
171                                                    size);
172                         BUG_ON(ret);
173                         start = extent_end + 1;
174                 } else {
175                         break;
176                 }
177         }
178
179         if (start < end) {
180                 size = end - start;
181                 ret = btrfs_add_free_space(block_group, start, size);
182                 BUG_ON(ret);
183         }
184         mutex_unlock(&info->pinned_mutex);
185
186         return 0;
187 }
188
189 static int remove_sb_from_cache(struct btrfs_root *root,
190                                 struct btrfs_block_group_cache *cache)
191 {
192         u64 bytenr;
193         u64 *logical;
194         int stripe_len;
195         int i, nr, ret;
196
197         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
198                 bytenr = btrfs_sb_offset(i);
199                 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
200                                        cache->key.objectid, bytenr, 0,
201                                        &logical, &nr, &stripe_len);
202                 BUG_ON(ret);
203                 while (nr--) {
204                         btrfs_remove_free_space(cache, logical[nr],
205                                                 stripe_len);
206                 }
207                 kfree(logical);
208         }
209         return 0;
210 }
211
212 static int cache_block_group(struct btrfs_root *root,
213                              struct btrfs_block_group_cache *block_group)
214 {
215         struct btrfs_path *path;
216         int ret = 0;
217         struct btrfs_key key;
218         struct extent_buffer *leaf;
219         int slot;
220         u64 last;
221
222         if (!block_group)
223                 return 0;
224
225         root = root->fs_info->extent_root;
226
227         if (block_group->cached)
228                 return 0;
229
230         path = btrfs_alloc_path();
231         if (!path)
232                 return -ENOMEM;
233
234         path->reada = 2;
235         /*
236          * we get into deadlocks with paths held by callers of this function.
237          * since the alloc_mutex is protecting things right now, just
238          * skip the locking here
239          */
240         path->skip_locking = 1;
241         last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
242         key.objectid = last;
243         key.offset = 0;
244         btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
245         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
246         if (ret < 0)
247                 goto err;
248
249         while (1) {
250                 leaf = path->nodes[0];
251                 slot = path->slots[0];
252                 if (slot >= btrfs_header_nritems(leaf)) {
253                         ret = btrfs_next_leaf(root, path);
254                         if (ret < 0)
255                                 goto err;
256                         if (ret == 0)
257                                 continue;
258                         else
259                                 break;
260                 }
261                 btrfs_item_key_to_cpu(leaf, &key, slot);
262                 if (key.objectid < block_group->key.objectid)
263                         goto next;
264
265                 if (key.objectid >= block_group->key.objectid +
266                     block_group->key.offset)
267                         break;
268
269                 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
270                         add_new_free_space(block_group, root->fs_info, last,
271                                            key.objectid);
272
273                         last = key.objectid + key.offset;
274                 }
275 next:
276                 path->slots[0]++;
277         }
278
279         add_new_free_space(block_group, root->fs_info, last,
280                            block_group->key.objectid +
281                            block_group->key.offset);
282
283         remove_sb_from_cache(root, block_group);
284         block_group->cached = 1;
285         ret = 0;
286 err:
287         btrfs_free_path(path);
288         return ret;
289 }
290
291 /*
292  * return the block group that starts at or after bytenr
293  */
294 static struct btrfs_block_group_cache *
295 btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
296 {
297         struct btrfs_block_group_cache *cache;
298
299         cache = block_group_cache_tree_search(info, bytenr, 0);
300
301         return cache;
302 }
303
304 /*
305  * return the block group that contains teh given bytenr
306  */
307 struct btrfs_block_group_cache *btrfs_lookup_block_group(
308                                                  struct btrfs_fs_info *info,
309                                                  u64 bytenr)
310 {
311         struct btrfs_block_group_cache *cache;
312
313         cache = block_group_cache_tree_search(info, bytenr, 1);
314
315         return cache;
316 }
317
318 static inline void put_block_group(struct btrfs_block_group_cache *cache)
319 {
320         if (atomic_dec_and_test(&cache->count))
321                 kfree(cache);
322 }
323
324 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
325                                                   u64 flags)
326 {
327         struct list_head *head = &info->space_info;
328         struct btrfs_space_info *found;
329         list_for_each_entry(found, head, list) {
330                 if (found->flags == flags)
331                         return found;
332         }
333         return NULL;
334 }
335
336 static u64 div_factor(u64 num, int factor)
337 {
338         if (factor == 10)
339                 return num;
340         num *= factor;
341         do_div(num, 10);
342         return num;
343 }
344
345 u64 btrfs_find_block_group(struct btrfs_root *root,
346                            u64 search_start, u64 search_hint, int owner)
347 {
348         struct btrfs_block_group_cache *cache;
349         u64 used;
350         u64 last = max(search_hint, search_start);
351         u64 group_start = 0;
352         int full_search = 0;
353         int factor = 9;
354         int wrapped = 0;
355 again:
356         while (1) {
357                 cache = btrfs_lookup_first_block_group(root->fs_info, last);
358                 if (!cache)
359                         break;
360
361                 spin_lock(&cache->lock);
362                 last = cache->key.objectid + cache->key.offset;
363                 used = btrfs_block_group_used(&cache->item);
364
365                 if ((full_search || !cache->ro) &&
366                     block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
367                         if (used + cache->pinned + cache->reserved <
368                             div_factor(cache->key.offset, factor)) {
369                                 group_start = cache->key.objectid;
370                                 spin_unlock(&cache->lock);
371                                 put_block_group(cache);
372                                 goto found;
373                         }
374                 }
375                 spin_unlock(&cache->lock);
376                 put_block_group(cache);
377                 cond_resched();
378         }
379         if (!wrapped) {
380                 last = search_start;
381                 wrapped = 1;
382                 goto again;
383         }
384         if (!full_search && factor < 10) {
385                 last = search_start;
386                 full_search = 1;
387                 factor = 10;
388                 goto again;
389         }
390 found:
391         return group_start;
392 }
393
394 /* simple helper to search for an existing extent at a given offset */
395 int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
396 {
397         int ret;
398         struct btrfs_key key;
399         struct btrfs_path *path;
400
401         path = btrfs_alloc_path();
402         BUG_ON(!path);
403         key.objectid = start;
404         key.offset = len;
405         btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
406         ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
407                                 0, 0);
408         btrfs_free_path(path);
409         return ret;
410 }
411
412 /*
413  * Back reference rules.  Back refs have three main goals:
414  *
415  * 1) differentiate between all holders of references to an extent so that
416  *    when a reference is dropped we can make sure it was a valid reference
417  *    before freeing the extent.
418  *
419  * 2) Provide enough information to quickly find the holders of an extent
420  *    if we notice a given block is corrupted or bad.
421  *
422  * 3) Make it easy to migrate blocks for FS shrinking or storage pool
423  *    maintenance.  This is actually the same as #2, but with a slightly
424  *    different use case.
425  *
426  * File extents can be referenced by:
427  *
428  * - multiple snapshots, subvolumes, or different generations in one subvol
429  * - different files inside a single subvolume
430  * - different offsets inside a file (bookend extents in file.c)
431  *
432  * The extent ref structure has fields for:
433  *
434  * - Objectid of the subvolume root
435  * - Generation number of the tree holding the reference
436  * - objectid of the file holding the reference
437  * - number of references holding by parent node (alway 1 for tree blocks)
438  *
439  * Btree leaf may hold multiple references to a file extent. In most cases,
440  * these references are from same file and the corresponding offsets inside
441  * the file are close together.
442  *
443  * When a file extent is allocated the fields are filled in:
444  *     (root_key.objectid, trans->transid, inode objectid, 1)
445  *
446  * When a leaf is cow'd new references are added for every file extent found
447  * in the leaf.  It looks similar to the create case, but trans->transid will
448  * be different when the block is cow'd.
449  *
450  *     (root_key.objectid, trans->transid, inode objectid,
451  *      number of references in the leaf)
452  *
453  * When a file extent is removed either during snapshot deletion or
454  * file truncation, we find the corresponding back reference and check
455  * the following fields:
456  *
457  *     (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
458  *      inode objectid)
459  *
460  * Btree extents can be referenced by:
461  *
462  * - Different subvolumes
463  * - Different generations of the same subvolume
464  *
465  * When a tree block is created, back references are inserted:
466  *
467  * (root->root_key.objectid, trans->transid, level, 1)
468  *
469  * When a tree block is cow'd, new back references are added for all the
470  * blocks it points to. If the tree block isn't in reference counted root,
471  * the old back references are removed. These new back references are of
472  * the form (trans->transid will have increased since creation):
473  *
474  * (root->root_key.objectid, trans->transid, level, 1)
475  *
476  * When a backref is in deleting, the following fields are checked:
477  *
478  * if backref was for a tree root:
479  *     (btrfs_header_owner(itself), btrfs_header_generation(itself), level)
480  * else
481  *     (btrfs_header_owner(parent), btrfs_header_generation(parent), level)
482  *
483  * Back Reference Key composing:
484  *
485  * The key objectid corresponds to the first byte in the extent, the key
486  * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
487  * byte of parent extent. If a extent is tree root, the key offset is set
488  * to the key objectid.
489  */
490
491 static noinline int lookup_extent_backref(struct btrfs_trans_handle *trans,
492                                           struct btrfs_root *root,
493                                           struct btrfs_path *path,
494                                           u64 bytenr, u64 parent,
495                                           u64 ref_root, u64 ref_generation,
496                                           u64 owner_objectid, int del)
497 {
498         struct btrfs_key key;
499         struct btrfs_extent_ref *ref;
500         struct extent_buffer *leaf;
501         u64 ref_objectid;
502         int ret;
503
504         key.objectid = bytenr;
505         key.type = BTRFS_EXTENT_REF_KEY;
506         key.offset = parent;
507
508         ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
509         if (ret < 0)
510                 goto out;
511         if (ret > 0) {
512                 ret = -ENOENT;
513                 goto out;
514         }
515
516         leaf = path->nodes[0];
517         ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
518         ref_objectid = btrfs_ref_objectid(leaf, ref);
519         if (btrfs_ref_root(leaf, ref) != ref_root ||
520             btrfs_ref_generation(leaf, ref) != ref_generation ||
521             (ref_objectid != owner_objectid &&
522              ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
523                 ret = -EIO;
524                 WARN_ON(1);
525                 goto out;
526         }
527         ret = 0;
528 out:
529         return ret;
530 }
531
532 /*
533  * updates all the backrefs that are pending on update_list for the
534  * extent_root
535  */
536 static noinline int update_backrefs(struct btrfs_trans_handle *trans,
537                                     struct btrfs_root *extent_root,
538                                     struct btrfs_path *path,
539                                     struct list_head *update_list)
540 {
541         struct btrfs_key key;
542         struct btrfs_extent_ref *ref;
543         struct btrfs_fs_info *info = extent_root->fs_info;
544         struct pending_extent_op *op;
545         struct extent_buffer *leaf;
546         int ret = 0;
547         struct list_head *cur = update_list->next;
548         u64 ref_objectid;
549         u64 ref_root = extent_root->root_key.objectid;
550
551         op = list_entry(cur, struct pending_extent_op, list);
552
553 search:
554         key.objectid = op->bytenr;
555         key.type = BTRFS_EXTENT_REF_KEY;
556         key.offset = op->orig_parent;
557
558         ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 1);
559         BUG_ON(ret);
560
561         leaf = path->nodes[0];
562
563 loop:
564         ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
565
566         ref_objectid = btrfs_ref_objectid(leaf, ref);
567
568         if (btrfs_ref_root(leaf, ref) != ref_root ||
569             btrfs_ref_generation(leaf, ref) != op->orig_generation ||
570             (ref_objectid != op->level &&
571              ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
572                 printk(KERN_ERR "btrfs couldn't find %llu, parent %llu, "
573                        "root %llu, owner %u\n",
574                        (unsigned long long)op->bytenr,
575                        (unsigned long long)op->orig_parent,
576                        (unsigned long long)ref_root, op->level);
577                 btrfs_print_leaf(extent_root, leaf);
578                 BUG();
579         }
580
581         key.objectid = op->bytenr;
582         key.offset = op->parent;
583         key.type = BTRFS_EXTENT_REF_KEY;
584         ret = btrfs_set_item_key_safe(trans, extent_root, path, &key);
585         BUG_ON(ret);
586         ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
587         btrfs_set_ref_generation(leaf, ref, op->generation);
588
589         cur = cur->next;
590
591         list_del_init(&op->list);
592         unlock_extent(&info->extent_ins, op->bytenr,
593                       op->bytenr + op->num_bytes - 1, GFP_NOFS);
594         kfree(op);
595
596         if (cur == update_list) {
597                 btrfs_mark_buffer_dirty(path->nodes[0]);
598                 btrfs_release_path(extent_root, path);
599                 goto out;
600         }
601
602         op = list_entry(cur, struct pending_extent_op, list);
603
604         path->slots[0]++;
605         while (path->slots[0] < btrfs_header_nritems(leaf)) {
606                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
607                 if (key.objectid == op->bytenr &&
608                     key.type == BTRFS_EXTENT_REF_KEY)
609                         goto loop;
610                 path->slots[0]++;
611         }
612
613         btrfs_mark_buffer_dirty(path->nodes[0]);
614         btrfs_release_path(extent_root, path);
615         goto search;
616
617 out:
618         return 0;
619 }
620
621 static noinline int insert_extents(struct btrfs_trans_handle *trans,
622                                    struct btrfs_root *extent_root,
623                                    struct btrfs_path *path,
624                                    struct list_head *insert_list, int nr)
625 {
626         struct btrfs_key *keys;
627         u32 *data_size;
628         struct pending_extent_op *op;
629         struct extent_buffer *leaf;
630         struct list_head *cur = insert_list->next;
631         struct btrfs_fs_info *info = extent_root->fs_info;
632         u64 ref_root = extent_root->root_key.objectid;
633         int i = 0, last = 0, ret;
634         int total = nr * 2;
635
636         if (!nr)
637                 return 0;
638
639         keys = kzalloc(total * sizeof(struct btrfs_key), GFP_NOFS);
640         if (!keys)
641                 return -ENOMEM;
642
643         data_size = kzalloc(total * sizeof(u32), GFP_NOFS);
644         if (!data_size) {
645                 kfree(keys);
646                 return -ENOMEM;
647         }
648
649         list_for_each_entry(op, insert_list, list) {
650                 keys[i].objectid = op->bytenr;
651                 keys[i].offset = op->num_bytes;
652                 keys[i].type = BTRFS_EXTENT_ITEM_KEY;
653                 data_size[i] = sizeof(struct btrfs_extent_item);
654                 i++;
655
656                 keys[i].objectid = op->bytenr;
657                 keys[i].offset = op->parent;
658                 keys[i].type = BTRFS_EXTENT_REF_KEY;
659                 data_size[i] = sizeof(struct btrfs_extent_ref);
660                 i++;
661         }
662
663         op = list_entry(cur, struct pending_extent_op, list);
664         i = 0;
665         while (i < total) {
666                 int c;
667                 ret = btrfs_insert_some_items(trans, extent_root, path,
668                                               keys+i, data_size+i, total-i);
669                 BUG_ON(ret < 0);
670
671                 if (last && ret > 1)
672                         BUG();
673
674                 leaf = path->nodes[0];
675                 for (c = 0; c < ret; c++) {
676                         int ref_first = keys[i].type == BTRFS_EXTENT_REF_KEY;
677
678                         /*
679                          * if the first item we inserted was a backref, then
680                          * the EXTENT_ITEM will be the odd c's, else it will
681                          * be the even c's
682                          */
683                         if ((ref_first && (c % 2)) ||
684                             (!ref_first && !(c % 2))) {
685                                 struct btrfs_extent_item *itm;
686
687                                 itm = btrfs_item_ptr(leaf, path->slots[0] + c,
688                                                      struct btrfs_extent_item);
689                                 btrfs_set_extent_refs(path->nodes[0], itm, 1);
690                                 op->del++;
691                         } else {
692                                 struct btrfs_extent_ref *ref;
693
694                                 ref = btrfs_item_ptr(leaf, path->slots[0] + c,
695                                                      struct btrfs_extent_ref);
696                                 btrfs_set_ref_root(leaf, ref, ref_root);
697                                 btrfs_set_ref_generation(leaf, ref,
698                                                          op->generation);
699                                 btrfs_set_ref_objectid(leaf, ref, op->level);
700                                 btrfs_set_ref_num_refs(leaf, ref, 1);
701                                 op->del++;
702                         }
703
704                         /*
705                          * using del to see when its ok to free up the
706                          * pending_extent_op.  In the case where we insert the
707                          * last item on the list in order to help do batching
708                          * we need to not free the extent op until we actually
709                          * insert the extent_item
710                          */
711                         if (op->del == 2) {
712                                 unlock_extent(&info->extent_ins, op->bytenr,
713                                               op->bytenr + op->num_bytes - 1,
714                                               GFP_NOFS);
715                                 cur = cur->next;
716                                 list_del_init(&op->list);
717                                 kfree(op);
718                                 if (cur != insert_list)
719                                         op = list_entry(cur,
720                                                 struct pending_extent_op,
721                                                 list);
722                         }
723                 }
724                 btrfs_mark_buffer_dirty(leaf);
725                 btrfs_release_path(extent_root, path);
726
727                 /*
728                  * Ok backref's and items usually go right next to eachother,
729                  * but if we could only insert 1 item that means that we
730                  * inserted on the end of a leaf, and we have no idea what may
731                  * be on the next leaf so we just play it safe.  In order to
732                  * try and help this case we insert the last thing on our
733                  * insert list so hopefully it will end up being the last
734                  * thing on the leaf and everything else will be before it,
735                  * which will let us insert a whole bunch of items at the same
736                  * time.
737                  */
738                 if (ret == 1 && !last && (i + ret < total)) {
739                         /*
740                          * last: where we will pick up the next time around
741                          * i: our current key to insert, will be total - 1
742                          * cur: the current op we are screwing with
743                          * op: duh
744                          */
745                         last = i + ret;
746                         i = total - 1;
747                         cur = insert_list->prev;
748                         op = list_entry(cur, struct pending_extent_op, list);
749                 } else if (last) {
750                         /*
751                          * ok we successfully inserted the last item on the
752                          * list, lets reset everything
753                          *
754                          * i: our current key to insert, so where we left off
755                          *    last time
756                          * last: done with this
757                          * cur: the op we are messing with
758                          * op: duh
759                          * total: since we inserted the last key, we need to
760                          *        decrement total so we dont overflow
761                          */
762                         i = last;
763                         last = 0;
764                         total--;
765                         if (i < total) {
766                                 cur = insert_list->next;
767                                 op = list_entry(cur, struct pending_extent_op,
768                                                 list);
769                         }
770                 } else {
771                         i += ret;
772                 }
773
774                 cond_resched();
775         }
776         ret = 0;
777         kfree(keys);
778         kfree(data_size);
779         return ret;
780 }
781
782 static noinline int insert_extent_backref(struct btrfs_trans_handle *trans,
783                                           struct btrfs_root *root,
784                                           struct btrfs_path *path,
785                                           u64 bytenr, u64 parent,
786                                           u64 ref_root, u64 ref_generation,
787                                           u64 owner_objectid)
788 {
789         struct btrfs_key key;
790         struct extent_buffer *leaf;
791         struct btrfs_extent_ref *ref;
792         u32 num_refs;
793         int ret;
794
795         key.objectid = bytenr;
796         key.type = BTRFS_EXTENT_REF_KEY;
797         key.offset = parent;
798
799         ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
800         if (ret == 0) {
801                 leaf = path->nodes[0];
802                 ref = btrfs_item_ptr(leaf, path->slots[0],
803                                      struct btrfs_extent_ref);
804                 btrfs_set_ref_root(leaf, ref, ref_root);
805                 btrfs_set_ref_generation(leaf, ref, ref_generation);
806                 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
807                 btrfs_set_ref_num_refs(leaf, ref, 1);
808         } else if (ret == -EEXIST) {
809                 u64 existing_owner;
810                 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
811                 leaf = path->nodes[0];
812                 ref = btrfs_item_ptr(leaf, path->slots[0],
813                                      struct btrfs_extent_ref);
814                 if (btrfs_ref_root(leaf, ref) != ref_root ||
815                     btrfs_ref_generation(leaf, ref) != ref_generation) {
816                         ret = -EIO;
817                         WARN_ON(1);
818                         goto out;
819                 }
820
821                 num_refs = btrfs_ref_num_refs(leaf, ref);
822                 BUG_ON(num_refs == 0);
823                 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
824
825                 existing_owner = btrfs_ref_objectid(leaf, ref);
826                 if (existing_owner != owner_objectid &&
827                     existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
828                         btrfs_set_ref_objectid(leaf, ref,
829                                         BTRFS_MULTIPLE_OBJECTIDS);
830                 }
831                 ret = 0;
832         } else {
833                 goto out;
834         }
835         btrfs_mark_buffer_dirty(path->nodes[0]);
836 out:
837         btrfs_release_path(root, path);
838         return ret;
839 }
840
841 static noinline int remove_extent_backref(struct btrfs_trans_handle *trans,
842                                           struct btrfs_root *root,
843                                           struct btrfs_path *path)
844 {
845         struct extent_buffer *leaf;
846         struct btrfs_extent_ref *ref;
847         u32 num_refs;
848         int ret = 0;
849
850         leaf = path->nodes[0];
851         ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
852         num_refs = btrfs_ref_num_refs(leaf, ref);
853         BUG_ON(num_refs == 0);
854         num_refs -= 1;
855         if (num_refs == 0) {
856                 ret = btrfs_del_item(trans, root, path);
857         } else {
858                 btrfs_set_ref_num_refs(leaf, ref, num_refs);
859                 btrfs_mark_buffer_dirty(leaf);
860         }
861         btrfs_release_path(root, path);
862         return ret;
863 }
864
865 #ifdef BIO_RW_DISCARD
866 static void btrfs_issue_discard(struct block_device *bdev,
867                                 u64 start, u64 len)
868 {
869         blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
870 }
871 #endif
872
873 static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
874                                 u64 num_bytes)
875 {
876 #ifdef BIO_RW_DISCARD
877         int ret;
878         u64 map_length = num_bytes;
879         struct btrfs_multi_bio *multi = NULL;
880
881         /* Tell the block device(s) that the sectors can be discarded */
882         ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
883                               bytenr, &map_length, &multi, 0);
884         if (!ret) {
885                 struct btrfs_bio_stripe *stripe = multi->stripes;
886                 int i;
887
888                 if (map_length > num_bytes)
889                         map_length = num_bytes;
890
891                 for (i = 0; i < multi->num_stripes; i++, stripe++) {
892                         btrfs_issue_discard(stripe->dev->bdev,
893                                             stripe->physical,
894                                             map_length);
895                 }
896                 kfree(multi);
897         }
898
899         return ret;
900 #else
901         return 0;
902 #endif
903 }
904
905 static noinline int free_extents(struct btrfs_trans_handle *trans,
906                                  struct btrfs_root *extent_root,
907                                  struct list_head *del_list)
908 {
909         struct btrfs_fs_info *info = extent_root->fs_info;
910         struct btrfs_path *path;
911         struct btrfs_key key, found_key;
912         struct extent_buffer *leaf;
913         struct list_head *cur;
914         struct pending_extent_op *op;
915         struct btrfs_extent_item *ei;
916         int ret, num_to_del, extent_slot = 0, found_extent = 0;
917         u32 refs;
918         u64 bytes_freed = 0;
919
920         path = btrfs_alloc_path();
921         if (!path)
922                 return -ENOMEM;
923         path->reada = 1;
924
925 search:
926         /* search for the backref for the current ref we want to delete */
927         cur = del_list->next;
928         op = list_entry(cur, struct pending_extent_op, list);
929         ret = lookup_extent_backref(trans, extent_root, path, op->bytenr,
930                                     op->orig_parent,
931                                     extent_root->root_key.objectid,
932                                     op->orig_generation, op->level, 1);
933         if (ret) {
934                 printk(KERN_ERR "btrfs unable to find backref byte nr %llu "
935                        "root %llu gen %llu owner %u\n",
936                        (unsigned long long)op->bytenr,
937                        (unsigned long long)extent_root->root_key.objectid,
938                        (unsigned long long)op->orig_generation, op->level);
939                 btrfs_print_leaf(extent_root, path->nodes[0]);
940                 WARN_ON(1);
941                 goto out;
942         }
943
944         extent_slot = path->slots[0];
945         num_to_del = 1;
946         found_extent = 0;
947
948         /*
949          * if we aren't the first item on the leaf we can move back one and see
950          * if our ref is right next to our extent item
951          */
952         if (likely(extent_slot)) {
953                 extent_slot--;
954                 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
955                                       extent_slot);
956                 if (found_key.objectid == op->bytenr &&
957                     found_key.type == BTRFS_EXTENT_ITEM_KEY &&
958                     found_key.offset == op->num_bytes) {
959                         num_to_del++;
960                         found_extent = 1;
961                 }
962         }
963
964         /*
965          * if we didn't find the extent we need to delete the backref and then
966          * search for the extent item key so we can update its ref count
967          */
968         if (!found_extent) {
969                 key.objectid = op->bytenr;
970                 key.type = BTRFS_EXTENT_ITEM_KEY;
971                 key.offset = op->num_bytes;
972
973                 ret = remove_extent_backref(trans, extent_root, path);
974                 BUG_ON(ret);
975                 btrfs_release_path(extent_root, path);
976                 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
977                 BUG_ON(ret);
978                 extent_slot = path->slots[0];
979         }
980
981         /* this is where we update the ref count for the extent */
982         leaf = path->nodes[0];
983         ei = btrfs_item_ptr(leaf, extent_slot, struct btrfs_extent_item);
984         refs = btrfs_extent_refs(leaf, ei);
985         BUG_ON(refs == 0);
986         refs--;
987         btrfs_set_extent_refs(leaf, ei, refs);
988
989         btrfs_mark_buffer_dirty(leaf);
990
991         /*
992          * This extent needs deleting.  The reason cur_slot is extent_slot +
993          * num_to_del is because extent_slot points to the slot where the extent
994          * is, and if the backref was not right next to the extent we will be
995          * deleting at least 1 item, and will want to start searching at the
996          * slot directly next to extent_slot.  However if we did find the
997          * backref next to the extent item them we will be deleting at least 2
998          * items and will want to start searching directly after the ref slot
999          */
1000         if (!refs) {
1001                 struct list_head *pos, *n, *end;
1002                 int cur_slot = extent_slot+num_to_del;
1003                 u64 super_used;
1004                 u64 root_used;
1005
1006                 path->slots[0] = extent_slot;
1007                 bytes_freed = op->num_bytes;
1008
1009                 mutex_lock(&info->pinned_mutex);
1010                 ret = pin_down_bytes(trans, extent_root, op->bytenr,
1011                                      op->num_bytes, op->level >=
1012                                      BTRFS_FIRST_FREE_OBJECTID);
1013                 mutex_unlock(&info->pinned_mutex);
1014                 BUG_ON(ret < 0);
1015                 op->del = ret;
1016
1017                 /*
1018                  * we need to see if we can delete multiple things at once, so
1019                  * start looping through the list of extents we are wanting to
1020                  * delete and see if their extent/backref's are right next to
1021                  * eachother and the extents only have 1 ref
1022                  */
1023                 for (pos = cur->next; pos != del_list; pos = pos->next) {
1024                         struct pending_extent_op *tmp;
1025
1026                         tmp = list_entry(pos, struct pending_extent_op, list);
1027
1028                         /* we only want to delete extent+ref at this stage */
1029                         if (cur_slot >= btrfs_header_nritems(leaf) - 1)
1030                                 break;
1031
1032                         btrfs_item_key_to_cpu(leaf, &found_key, cur_slot);
1033                         if (found_key.objectid != tmp->bytenr ||
1034                             found_key.type != BTRFS_EXTENT_ITEM_KEY ||
1035                             found_key.offset != tmp->num_bytes)
1036                                 break;
1037
1038                         /* check to make sure this extent only has one ref */
1039                         ei = btrfs_item_ptr(leaf, cur_slot,
1040                                             struct btrfs_extent_item);
1041                         if (btrfs_extent_refs(leaf, ei) != 1)
1042                                 break;
1043
1044                         btrfs_item_key_to_cpu(leaf, &found_key, cur_slot+1);
1045                         if (found_key.objectid != tmp->bytenr ||
1046                             found_key.type != BTRFS_EXTENT_REF_KEY ||
1047                             found_key.offset != tmp->orig_parent)
1048                                 break;
1049
1050                         /*
1051                          * the ref is right next to the extent, we can set the
1052                          * ref count to 0 since we will delete them both now
1053                          */
1054                         btrfs_set_extent_refs(leaf, ei, 0);
1055
1056                         /* pin down the bytes for this extent */
1057                         mutex_lock(&info->pinned_mutex);
1058                         ret = pin_down_bytes(trans, extent_root, tmp->bytenr,
1059                                              tmp->num_bytes, tmp->level >=
1060                                              BTRFS_FIRST_FREE_OBJECTID);
1061                         mutex_unlock(&info->pinned_mutex);
1062                         BUG_ON(ret < 0);
1063
1064                         /*
1065                          * use the del field to tell if we need to go ahead and
1066                          * free up the extent when we delete the item or not.
1067                          */
1068                         tmp->del = ret;
1069                         bytes_freed += tmp->num_bytes;
1070
1071                         num_to_del += 2;
1072                         cur_slot += 2;
1073                 }
1074                 end = pos;
1075
1076                 /* update the free space counters */
1077                 spin_lock(&info->delalloc_lock);
1078                 super_used = btrfs_super_bytes_used(&info->super_copy);
1079                 btrfs_set_super_bytes_used(&info->super_copy,
1080                                            super_used - bytes_freed);
1081
1082                 root_used = btrfs_root_used(&extent_root->root_item);
1083                 btrfs_set_root_used(&extent_root->root_item,
1084                                     root_used - bytes_freed);
1085                 spin_unlock(&info->delalloc_lock);
1086
1087                 /* delete the items */
1088                 ret = btrfs_del_items(trans, extent_root, path,
1089                                       path->slots[0], num_to_del);
1090                 BUG_ON(ret);
1091
1092                 /*
1093                  * loop through the extents we deleted and do the cleanup work
1094                  * on them
1095                  */
1096                 for (pos = cur, n = pos->next; pos != end;
1097                      pos = n, n = pos->next) {
1098                         struct pending_extent_op *tmp;
1099                         tmp = list_entry(pos, struct pending_extent_op, list);
1100
1101                         /*
1102                          * remember tmp->del tells us wether or not we pinned
1103                          * down the extent
1104                          */
1105                         ret = update_block_group(trans, extent_root,
1106                                                  tmp->bytenr, tmp->num_bytes, 0,
1107                                                  tmp->del);
1108                         BUG_ON(ret);
1109
1110                         list_del_init(&tmp->list);
1111                         unlock_extent(&info->extent_ins, tmp->bytenr,
1112                                       tmp->bytenr + tmp->num_bytes - 1,
1113                                       GFP_NOFS);
1114                         kfree(tmp);
1115                 }
1116         } else if (refs && found_extent) {
1117                 /*
1118                  * the ref and extent were right next to eachother, but the
1119                  * extent still has a ref, so just free the backref and keep
1120                  * going
1121                  */
1122                 ret = remove_extent_backref(trans, extent_root, path);
1123                 BUG_ON(ret);
1124
1125                 list_del_init(&op->list);
1126                 unlock_extent(&info->extent_ins, op->bytenr,
1127                               op->bytenr + op->num_bytes - 1, GFP_NOFS);
1128                 kfree(op);
1129         } else {
1130                 /*
1131                  * the extent has multiple refs and the backref we were looking
1132                  * for was not right next to it, so just unlock and go next,
1133                  * we're good to go
1134                  */
1135                 list_del_init(&op->list);
1136                 unlock_extent(&info->extent_ins, op->bytenr,
1137                               op->bytenr + op->num_bytes - 1, GFP_NOFS);
1138                 kfree(op);
1139         }
1140
1141         btrfs_release_path(extent_root, path);
1142         if (!list_empty(del_list))
1143                 goto search;
1144
1145 out:
1146         btrfs_free_path(path);
1147         return ret;
1148 }
1149
1150 static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1151                                      struct btrfs_root *root, u64 bytenr,
1152                                      u64 orig_parent, u64 parent,
1153                                      u64 orig_root, u64 ref_root,
1154                                      u64 orig_generation, u64 ref_generation,
1155                                      u64 owner_objectid)
1156 {
1157         int ret;
1158         struct btrfs_root *extent_root = root->fs_info->extent_root;
1159         struct btrfs_path *path;
1160
1161         if (root == root->fs_info->extent_root) {
1162                 struct pending_extent_op *extent_op;
1163                 u64 num_bytes;
1164
1165                 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
1166                 num_bytes = btrfs_level_size(root, (int)owner_objectid);
1167                 mutex_lock(&root->fs_info->extent_ins_mutex);
1168                 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
1169                                 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
1170                         u64 priv;
1171                         ret = get_state_private(&root->fs_info->extent_ins,
1172                                                 bytenr, &priv);
1173                         BUG_ON(ret);
1174                         extent_op = (struct pending_extent_op *)
1175                                                         (unsigned long)priv;
1176                         BUG_ON(extent_op->parent != orig_parent);
1177                         BUG_ON(extent_op->generation != orig_generation);
1178
1179                         extent_op->parent = parent;
1180                         extent_op->generation = ref_generation;
1181                 } else {
1182                         extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
1183                         BUG_ON(!extent_op);
1184
1185                         extent_op->type = PENDING_BACKREF_UPDATE;
1186                         extent_op->bytenr = bytenr;
1187                         extent_op->num_bytes = num_bytes;
1188                         extent_op->parent = parent;
1189                         extent_op->orig_parent = orig_parent;
1190                         extent_op->generation = ref_generation;
1191                         extent_op->orig_generation = orig_generation;
1192                         extent_op->level = (int)owner_objectid;
1193                         INIT_LIST_HEAD(&extent_op->list);
1194                         extent_op->del = 0;
1195
1196                         set_extent_bits(&root->fs_info->extent_ins,
1197                                         bytenr, bytenr + num_bytes - 1,
1198                                         EXTENT_WRITEBACK, GFP_NOFS);
1199                         set_state_private(&root->fs_info->extent_ins,
1200                                           bytenr, (unsigned long)extent_op);
1201                 }
1202                 mutex_unlock(&root->fs_info->extent_ins_mutex);
1203                 return 0;
1204         }
1205
1206         path = btrfs_alloc_path();
1207         if (!path)
1208                 return -ENOMEM;
1209         ret = lookup_extent_backref(trans, extent_root, path,
1210                                     bytenr, orig_parent, orig_root,
1211                                     orig_generation, owner_objectid, 1);
1212         if (ret)
1213                 goto out;
1214         ret = remove_extent_backref(trans, extent_root, path);
1215         if (ret)
1216                 goto out;
1217         ret = insert_extent_backref(trans, extent_root, path, bytenr,
1218                                     parent, ref_root, ref_generation,
1219                                     owner_objectid);
1220         BUG_ON(ret);
1221         finish_current_insert(trans, extent_root, 0);
1222         del_pending_extents(trans, extent_root, 0);
1223 out:
1224         btrfs_free_path(path);
1225         return ret;
1226 }
1227
1228 int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1229                             struct btrfs_root *root, u64 bytenr,
1230                             u64 orig_parent, u64 parent,
1231                             u64 ref_root, u64 ref_generation,
1232                             u64 owner_objectid)
1233 {
1234         int ret;
1235         if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1236             owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1237                 return 0;
1238         ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
1239                                         parent, ref_root, ref_root,
1240                                         ref_generation, ref_generation,
1241                                         owner_objectid);
1242         return ret;
1243 }
1244
1245 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1246                                   struct btrfs_root *root, u64 bytenr,
1247                                   u64 orig_parent, u64 parent,
1248                                   u64 orig_root, u64 ref_root,
1249                                   u64 orig_generation, u64 ref_generation,
1250                                   u64 owner_objectid)
1251 {
1252         struct btrfs_path *path;
1253         int ret;
1254         struct btrfs_key key;
1255         struct extent_buffer *l;
1256         struct btrfs_extent_item *item;
1257         u32 refs;
1258
1259         path = btrfs_alloc_path();
1260         if (!path)
1261                 return -ENOMEM;
1262
1263         path->reada = 1;
1264         key.objectid = bytenr;
1265         key.type = BTRFS_EXTENT_ITEM_KEY;
1266         key.offset = (u64)-1;
1267
1268         ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1269                                 0, 1);
1270         if (ret < 0)
1271                 return ret;
1272         BUG_ON(ret == 0 || path->slots[0] == 0);
1273
1274         path->slots[0]--;
1275         l = path->nodes[0];
1276
1277         btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1278         if (key.objectid != bytenr) {
1279                 btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]);
1280                 printk(KERN_ERR "btrfs wanted %llu found %llu\n",
1281                        (unsigned long long)bytenr,
1282                        (unsigned long long)key.objectid);
1283                 BUG();
1284         }
1285         BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
1286
1287         item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1288         refs = btrfs_extent_refs(l, item);
1289         btrfs_set_extent_refs(l, item, refs + 1);
1290         btrfs_mark_buffer_dirty(path->nodes[0]);
1291
1292         btrfs_release_path(root->fs_info->extent_root, path);
1293
1294         path->reada = 1;
1295         ret = insert_extent_backref(trans, root->fs_info->extent_root,
1296                                     path, bytenr, parent,
1297                                     ref_root, ref_generation,
1298                                     owner_objectid);
1299         BUG_ON(ret);
1300         finish_current_insert(trans, root->fs_info->extent_root, 0);
1301         del_pending_extents(trans, root->fs_info->extent_root, 0);
1302
1303         btrfs_free_path(path);
1304         return 0;
1305 }
1306
1307 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1308                          struct btrfs_root *root,
1309                          u64 bytenr, u64 num_bytes, u64 parent,
1310                          u64 ref_root, u64 ref_generation,
1311                          u64 owner_objectid)
1312 {
1313         int ret;
1314         if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1315             owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1316                 return 0;
1317         ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
1318                                      0, ref_root, 0, ref_generation,
1319                                      owner_objectid);
1320         return ret;
1321 }
1322
1323 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
1324                          struct btrfs_root *root)
1325 {
1326         finish_current_insert(trans, root->fs_info->extent_root, 1);
1327         del_pending_extents(trans, root->fs_info->extent_root, 1);
1328         return 0;
1329 }
1330
1331 int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
1332                             struct btrfs_root *root, u64 bytenr,
1333                             u64 num_bytes, u32 *refs)
1334 {
1335         struct btrfs_path *path;
1336         int ret;
1337         struct btrfs_key key;
1338         struct extent_buffer *l;
1339         struct btrfs_extent_item *item;
1340
1341         WARN_ON(num_bytes < root->sectorsize);
1342         path = btrfs_alloc_path();
1343         path->reada = 1;
1344         key.objectid = bytenr;
1345         key.offset = num_bytes;
1346         btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1347         ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1348                                 0, 0);
1349         if (ret < 0)
1350                 goto out;
1351         if (ret != 0) {
1352                 btrfs_print_leaf(root, path->nodes[0]);
1353                 printk(KERN_INFO "btrfs failed to find block number %llu\n",
1354                        (unsigned long long)bytenr);
1355                 BUG();
1356         }
1357         l = path->nodes[0];
1358         item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1359         *refs = btrfs_extent_refs(l, item);
1360 out:
1361         btrfs_free_path(path);
1362         return 0;
1363 }
1364
1365 int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
1366                           struct btrfs_root *root, u64 objectid, u64 bytenr)
1367 {
1368         struct btrfs_root *extent_root = root->fs_info->extent_root;
1369         struct btrfs_path *path;
1370         struct extent_buffer *leaf;
1371         struct btrfs_extent_ref *ref_item;
1372         struct btrfs_key key;
1373         struct btrfs_key found_key;
1374         u64 ref_root;
1375         u64 last_snapshot;
1376         u32 nritems;
1377         int ret;
1378
1379         key.objectid = bytenr;
1380         key.offset = (u64)-1;
1381         key.type = BTRFS_EXTENT_ITEM_KEY;
1382
1383         path = btrfs_alloc_path();
1384         ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
1385         if (ret < 0)
1386                 goto out;
1387         BUG_ON(ret == 0);
1388
1389         ret = -ENOENT;
1390         if (path->slots[0] == 0)
1391                 goto out;
1392
1393         path->slots[0]--;
1394         leaf = path->nodes[0];
1395         btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1396
1397         if (found_key.objectid != bytenr ||
1398             found_key.type != BTRFS_EXTENT_ITEM_KEY)
1399                 goto out;
1400
1401         last_snapshot = btrfs_root_last_snapshot(&root->root_item);
1402         while (1) {
1403                 leaf = path->nodes[0];
1404                 nritems = btrfs_header_nritems(leaf);
1405                 if (path->slots[0] >= nritems) {
1406                         ret = btrfs_next_leaf(extent_root, path);
1407                         if (ret < 0)
1408                                 goto out;
1409                         if (ret == 0)
1410                                 continue;
1411                         break;
1412                 }
1413                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1414                 if (found_key.objectid != bytenr)
1415                         break;
1416
1417                 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
1418                         path->slots[0]++;
1419                         continue;
1420                 }
1421
1422                 ref_item = btrfs_item_ptr(leaf, path->slots[0],
1423                                           struct btrfs_extent_ref);
1424                 ref_root = btrfs_ref_root(leaf, ref_item);
1425                 if ((ref_root != root->root_key.objectid &&
1426                      ref_root != BTRFS_TREE_LOG_OBJECTID) ||
1427                      objectid != btrfs_ref_objectid(leaf, ref_item)) {
1428                         ret = 1;
1429                         goto out;
1430                 }
1431                 if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
1432                         ret = 1;
1433                         goto out;
1434                 }
1435
1436                 path->slots[0]++;
1437         }
1438         ret = 0;
1439 out:
1440         btrfs_free_path(path);
1441         return ret;
1442 }
1443
1444 int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1445                     struct extent_buffer *buf, u32 nr_extents)
1446 {
1447         struct btrfs_key key;
1448         struct btrfs_file_extent_item *fi;
1449         u64 root_gen;
1450         u32 nritems;
1451         int i;
1452         int level;
1453         int ret = 0;
1454         int shared = 0;
1455
1456         if (!root->ref_cows)
1457                 return 0;
1458
1459         if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1460                 shared = 0;
1461                 root_gen = root->root_key.offset;
1462         } else {
1463                 shared = 1;
1464                 root_gen = trans->transid - 1;
1465         }
1466
1467         level = btrfs_header_level(buf);
1468         nritems = btrfs_header_nritems(buf);
1469
1470         if (level == 0) {
1471                 struct btrfs_leaf_ref *ref;
1472                 struct btrfs_extent_info *info;
1473
1474                 ref = btrfs_alloc_leaf_ref(root, nr_extents);
1475                 if (!ref) {
1476                         ret = -ENOMEM;
1477                         goto out;
1478                 }
1479
1480                 ref->root_gen = root_gen;
1481                 ref->bytenr = buf->start;
1482                 ref->owner = btrfs_header_owner(buf);
1483                 ref->generation = btrfs_header_generation(buf);
1484                 ref->nritems = nr_extents;
1485                 info = ref->extents;
1486
1487                 for (i = 0; nr_extents > 0 && i < nritems; i++) {
1488                         u64 disk_bytenr;
1489                         btrfs_item_key_to_cpu(buf, &key, i);
1490                         if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1491                                 continue;
1492                         fi = btrfs_item_ptr(buf, i,
1493                                             struct btrfs_file_extent_item);
1494                         if (btrfs_file_extent_type(buf, fi) ==
1495                             BTRFS_FILE_EXTENT_INLINE)
1496                                 continue;
1497                         disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1498                         if (disk_bytenr == 0)
1499                                 continue;
1500
1501                         info->bytenr = disk_bytenr;
1502                         info->num_bytes =
1503                                 btrfs_file_extent_disk_num_bytes(buf, fi);
1504                         info->objectid = key.objectid;
1505                         info->offset = key.offset;
1506                         info++;
1507                 }
1508
1509                 ret = btrfs_add_leaf_ref(root, ref, shared);
1510                 if (ret == -EEXIST && shared) {
1511                         struct btrfs_leaf_ref *old;
1512                         old = btrfs_lookup_leaf_ref(root, ref->bytenr);
1513                         BUG_ON(!old);
1514                         btrfs_remove_leaf_ref(root, old);
1515                         btrfs_free_leaf_ref(root, old);
1516                         ret = btrfs_add_leaf_ref(root, ref, shared);
1517                 }
1518                 WARN_ON(ret);
1519                 btrfs_free_leaf_ref(root, ref);
1520         }
1521 out:
1522         return ret;
1523 }
1524
1525 /* when a block goes through cow, we update the reference counts of
1526  * everything that block points to.  The internal pointers of the block
1527  * can be in just about any order, and it is likely to have clusters of
1528  * things that are close together and clusters of things that are not.
1529  *
1530  * To help reduce the seeks that come with updating all of these reference
1531  * counts, sort them by byte number before actual updates are done.
1532  *
1533  * struct refsort is used to match byte number to slot in the btree block.
1534  * we sort based on the byte number and then use the slot to actually
1535  * find the item.
1536  *
1537  * struct refsort is smaller than strcut btrfs_item and smaller than
1538  * struct btrfs_key_ptr.  Since we're currently limited to the page size
1539  * for a btree block, there's no way for a kmalloc of refsorts for a
1540  * single node to be bigger than a page.
1541  */
1542 struct refsort {
1543         u64 bytenr;
1544         u32 slot;
1545 };
1546
1547 /*
1548  * for passing into sort()
1549  */
1550 static int refsort_cmp(const void *a_void, const void *b_void)
1551 {
1552         const struct refsort *a = a_void;
1553         const struct refsort *b = b_void;
1554
1555         if (a->bytenr < b->bytenr)
1556                 return -1;
1557         if (a->bytenr > b->bytenr)
1558                 return 1;
1559         return 0;
1560 }
1561
1562
1563 noinline int btrfs_inc_ref(struct btrfs_trans_handle *trans,
1564                            struct btrfs_root *root,
1565                            struct extent_buffer *orig_buf,
1566                            struct extent_buffer *buf, u32 *nr_extents)
1567 {
1568         u64 bytenr;
1569         u64 ref_root;
1570         u64 orig_root;
1571         u64 ref_generation;
1572         u64 orig_generation;
1573         struct refsort *sorted;
1574         u32 nritems;
1575         u32 nr_file_extents = 0;
1576         struct btrfs_key key;
1577         struct btrfs_file_extent_item *fi;
1578         int i;
1579         int level;
1580         int ret = 0;
1581         int faili = 0;
1582         int refi = 0;
1583         int slot;
1584         int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
1585                             u64, u64, u64, u64, u64, u64, u64, u64);
1586
1587         ref_root = btrfs_header_owner(buf);
1588         ref_generation = btrfs_header_generation(buf);
1589         orig_root = btrfs_header_owner(orig_buf);
1590         orig_generation = btrfs_header_generation(orig_buf);
1591
1592         nritems = btrfs_header_nritems(buf);
1593         level = btrfs_header_level(buf);
1594
1595         sorted = kmalloc(sizeof(struct refsort) * nritems, GFP_NOFS);
1596         BUG_ON(!sorted);
1597
1598         if (root->ref_cows) {
1599                 process_func = __btrfs_inc_extent_ref;
1600         } else {
1601                 if (level == 0 &&
1602                     root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1603                         goto out;
1604                 if (level != 0 &&
1605                     root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1606                         goto out;
1607                 process_func = __btrfs_update_extent_ref;
1608         }
1609
1610         /*
1611          * we make two passes through the items.  In the first pass we
1612          * only record the byte number and slot.  Then we sort based on
1613          * byte number and do the actual work based on the sorted results
1614          */
1615         for (i = 0; i < nritems; i++) {
1616                 cond_resched();
1617                 if (level == 0) {
1618                         btrfs_item_key_to_cpu(buf, &key, i);
1619                         if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1620                                 continue;
1621                         fi = btrfs_item_ptr(buf, i,
1622                                             struct btrfs_file_extent_item);
1623                         if (btrfs_file_extent_type(buf, fi) ==
1624                             BTRFS_FILE_EXTENT_INLINE)
1625                                 continue;
1626                         bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1627                         if (bytenr == 0)
1628                                 continue;
1629
1630                         nr_file_extents++;
1631                         sorted[refi].bytenr = bytenr;
1632                         sorted[refi].slot = i;
1633                         refi++;
1634                 } else {
1635                         bytenr = btrfs_node_blockptr(buf, i);
1636                         sorted[refi].bytenr = bytenr;
1637                         sorted[refi].slot = i;
1638                         refi++;
1639                 }
1640         }
1641         /*
1642          * if refi == 0, we didn't actually put anything into the sorted
1643          * array and we're done
1644          */
1645         if (refi == 0)
1646                 goto out;
1647
1648         sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
1649
1650         for (i = 0; i < refi; i++) {
1651                 cond_resched();
1652                 slot = sorted[i].slot;
1653                 bytenr = sorted[i].bytenr;
1654
1655                 if (level == 0) {
1656                         btrfs_item_key_to_cpu(buf, &key, slot);
1657
1658                         ret = process_func(trans, root, bytenr,
1659                                            orig_buf->start, buf->start,
1660                                            orig_root, ref_root,
1661                                            orig_generation, ref_generation,
1662                                            key.objectid);
1663
1664                         if (ret) {
1665                                 faili = slot;
1666                                 WARN_ON(1);
1667                                 goto fail;
1668                         }
1669                 } else {
1670                         ret = process_func(trans, root, bytenr,
1671                                            orig_buf->start, buf->start,
1672                                            orig_root, ref_root,
1673                                            orig_generation, ref_generation,
1674                                            level - 1);
1675                         if (ret) {
1676                                 faili = slot;
1677                                 WARN_ON(1);
1678                                 goto fail;
1679                         }
1680                 }
1681         }
1682 out:
1683         kfree(sorted);
1684         if (nr_extents) {
1685                 if (level == 0)
1686                         *nr_extents = nr_file_extents;
1687                 else
1688                         *nr_extents = nritems;
1689         }
1690         return 0;
1691 fail:
1692         kfree(sorted);
1693         WARN_ON(1);
1694         return ret;
1695 }
1696
1697 int btrfs_update_ref(struct btrfs_trans_handle *trans,
1698                      struct btrfs_root *root, struct extent_buffer *orig_buf,
1699                      struct extent_buffer *buf, int start_slot, int nr)
1700
1701 {
1702         u64 bytenr;
1703         u64 ref_root;
1704         u64 orig_root;
1705         u64 ref_generation;
1706         u64 orig_generation;
1707         struct btrfs_key key;
1708         struct btrfs_file_extent_item *fi;
1709         int i;
1710         int ret;
1711         int slot;
1712         int level;
1713
1714         BUG_ON(start_slot < 0);
1715         BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1716
1717         ref_root = btrfs_header_owner(buf);
1718         ref_generation = btrfs_header_generation(buf);
1719         orig_root = btrfs_header_owner(orig_buf);
1720         orig_generation = btrfs_header_generation(orig_buf);
1721         level = btrfs_header_level(buf);
1722
1723         if (!root->ref_cows) {
1724                 if (level == 0 &&
1725                     root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1726                         return 0;
1727                 if (level != 0 &&
1728                     root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1729                         return 0;
1730         }
1731
1732         for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1733                 cond_resched();
1734                 if (level == 0) {
1735                         btrfs_item_key_to_cpu(buf, &key, slot);
1736                         if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1737                                 continue;
1738                         fi = btrfs_item_ptr(buf, slot,
1739                                             struct btrfs_file_extent_item);
1740                         if (btrfs_file_extent_type(buf, fi) ==
1741                             BTRFS_FILE_EXTENT_INLINE)
1742                                 continue;
1743                         bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1744                         if (bytenr == 0)
1745                                 continue;
1746                         ret = __btrfs_update_extent_ref(trans, root, bytenr,
1747                                             orig_buf->start, buf->start,
1748                                             orig_root, ref_root,
1749                                             orig_generation, ref_generation,
1750                                             key.objectid);
1751                         if (ret)
1752                                 goto fail;
1753                 } else {
1754                         bytenr = btrfs_node_blockptr(buf, slot);
1755                         ret = __btrfs_update_extent_ref(trans, root, bytenr,
1756                                             orig_buf->start, buf->start,
1757                                             orig_root, ref_root,
1758                                             orig_generation, ref_generation,
1759                                             level - 1);
1760                         if (ret)
1761                                 goto fail;
1762                 }
1763         }
1764         return 0;
1765 fail:
1766         WARN_ON(1);
1767         return -1;
1768 }
1769
1770 static int write_one_cache_group(struct btrfs_trans_handle *trans,
1771                                  struct btrfs_root *root,
1772                                  struct btrfs_path *path,
1773                                  struct btrfs_block_group_cache *cache)
1774 {
1775         int ret;
1776         int pending_ret;
1777         struct btrfs_root *extent_root = root->fs_info->extent_root;
1778         unsigned long bi;
1779         struct extent_buffer *leaf;
1780
1781         ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
1782         if (ret < 0)
1783                 goto fail;
1784         BUG_ON(ret);
1785
1786         leaf = path->nodes[0];
1787         bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1788         write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1789         btrfs_mark_buffer_dirty(leaf);
1790         btrfs_release_path(extent_root, path);
1791 fail:
1792         finish_current_insert(trans, extent_root, 0);
1793         pending_ret = del_pending_extents(trans, extent_root, 0);
1794         if (ret)
1795                 return ret;
1796         if (pending_ret)
1797                 return pending_ret;
1798         return 0;
1799
1800 }
1801
1802 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1803                                    struct btrfs_root *root)
1804 {
1805         struct btrfs_block_group_cache *cache, *entry;
1806         struct rb_node *n;
1807         int err = 0;
1808         int werr = 0;
1809         struct btrfs_path *path;
1810         u64 last = 0;
1811
1812         path = btrfs_alloc_path();
1813         if (!path)
1814                 return -ENOMEM;
1815
1816         while (1) {
1817                 cache = NULL;
1818                 spin_lock(&root->fs_info->block_group_cache_lock);
1819                 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1820                      n; n = rb_next(n)) {
1821                         entry = rb_entry(n, struct btrfs_block_group_cache,
1822                                          cache_node);
1823                         if (entry->dirty) {
1824                                 cache = entry;
1825                                 break;
1826                         }
1827                 }
1828                 spin_unlock(&root->fs_info->block_group_cache_lock);
1829
1830                 if (!cache)
1831                         break;
1832
1833                 cache->dirty = 0;
1834                 last += cache->key.offset;
1835
1836                 err = write_one_cache_group(trans, root,
1837                                             path, cache);
1838                 /*
1839                  * if we fail to write the cache group, we want
1840                  * to keep it marked dirty in hopes that a later
1841                  * write will work
1842                  */
1843                 if (err) {
1844                         werr = err;
1845                         continue;
1846                 }
1847         }
1848         btrfs_free_path(path);
1849         return werr;
1850 }
1851
1852 int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
1853 {
1854         struct btrfs_block_group_cache *block_group;
1855         int readonly = 0;
1856
1857         block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
1858         if (!block_group || block_group->ro)
1859                 readonly = 1;
1860         if (block_group)
1861                 put_block_group(block_group);
1862         return readonly;
1863 }
1864
1865 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1866                              u64 total_bytes, u64 bytes_used,
1867                              struct btrfs_space_info **space_info)
1868 {
1869         struct btrfs_space_info *found;
1870
1871         found = __find_space_info(info, flags);
1872         if (found) {
1873                 spin_lock(&found->lock);
1874                 found->total_bytes += total_bytes;
1875                 found->bytes_used += bytes_used;
1876                 found->full = 0;
1877                 spin_unlock(&found->lock);
1878                 *space_info = found;
1879                 return 0;
1880         }
1881         found = kzalloc(sizeof(*found), GFP_NOFS);
1882         if (!found)
1883                 return -ENOMEM;
1884
1885         list_add(&found->list, &info->space_info);
1886         INIT_LIST_HEAD(&found->block_groups);
1887         init_rwsem(&found->groups_sem);
1888         spin_lock_init(&found->lock);
1889         found->flags = flags;
1890         found->total_bytes = total_bytes;
1891         found->bytes_used = bytes_used;
1892         found->bytes_pinned = 0;
1893         found->bytes_reserved = 0;
1894         found->bytes_readonly = 0;
1895         found->full = 0;
1896         found->force_alloc = 0;
1897         *space_info = found;
1898         return 0;
1899 }
1900
1901 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1902 {
1903         u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
1904                                    BTRFS_BLOCK_GROUP_RAID1 |
1905                                    BTRFS_BLOCK_GROUP_RAID10 |
1906                                    BTRFS_BLOCK_GROUP_DUP);
1907         if (extra_flags) {
1908                 if (flags & BTRFS_BLOCK_GROUP_DATA)
1909                         fs_info->avail_data_alloc_bits |= extra_flags;
1910                 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1911                         fs_info->avail_metadata_alloc_bits |= extra_flags;
1912                 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1913                         fs_info->avail_system_alloc_bits |= extra_flags;
1914         }
1915 }
1916
1917 static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
1918 {
1919         spin_lock(&cache->space_info->lock);
1920         spin_lock(&cache->lock);
1921         if (!cache->ro) {
1922                 cache->space_info->bytes_readonly += cache->key.offset -
1923                                         btrfs_block_group_used(&cache->item);
1924                 cache->ro = 1;
1925         }
1926         spin_unlock(&cache->lock);
1927         spin_unlock(&cache->space_info->lock);
1928 }
1929
1930 u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
1931 {
1932         u64 num_devices = root->fs_info->fs_devices->rw_devices;
1933
1934         if (num_devices == 1)
1935                 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1936         if (num_devices < 4)
1937                 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1938
1939         if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1940             (flags & (BTRFS_BLOCK_GROUP_RAID1 |
1941                       BTRFS_BLOCK_GROUP_RAID10))) {
1942                 flags &= ~BTRFS_BLOCK_GROUP_DUP;
1943         }
1944
1945         if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
1946             (flags & BTRFS_BLOCK_GROUP_RAID10)) {
1947                 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
1948         }
1949
1950         if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1951             ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1952              (flags & BTRFS_BLOCK_GROUP_RAID10) |
1953              (flags & BTRFS_BLOCK_GROUP_DUP)))
1954                 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1955         return flags;
1956 }
1957
1958 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1959                           struct btrfs_root *extent_root, u64 alloc_bytes,
1960                           u64 flags, int force)
1961 {
1962         struct btrfs_space_info *space_info;
1963         u64 thresh;
1964         int ret = 0;
1965
1966         mutex_lock(&extent_root->fs_info->chunk_mutex);
1967
1968         flags = btrfs_reduce_alloc_profile(extent_root, flags);
1969
1970         space_info = __find_space_info(extent_root->fs_info, flags);
1971         if (!space_info) {
1972                 ret = update_space_info(extent_root->fs_info, flags,
1973                                         0, 0, &space_info);
1974                 BUG_ON(ret);
1975         }
1976         BUG_ON(!space_info);
1977
1978         spin_lock(&space_info->lock);
1979         if (space_info->force_alloc) {
1980                 force = 1;
1981                 space_info->force_alloc = 0;
1982         }
1983         if (space_info->full) {
1984                 spin_unlock(&space_info->lock);
1985                 goto out;
1986         }
1987
1988         thresh = space_info->total_bytes - space_info->bytes_readonly;
1989         thresh = div_factor(thresh, 6);
1990         if (!force &&
1991            (space_info->bytes_used + space_info->bytes_pinned +
1992             space_info->bytes_reserved + alloc_bytes) < thresh) {
1993                 spin_unlock(&space_info->lock);
1994                 goto out;
1995         }
1996         spin_unlock(&space_info->lock);
1997
1998         ret = btrfs_alloc_chunk(trans, extent_root, flags);
1999         if (ret)
2000                 space_info->full = 1;
2001 out:
2002         mutex_unlock(&extent_root->fs_info->chunk_mutex);
2003         return ret;
2004 }
2005
2006 static int update_block_group(struct btrfs_trans_handle *trans,
2007                               struct btrfs_root *root,
2008                               u64 bytenr, u64 num_bytes, int alloc,
2009                               int mark_free)
2010 {
2011         struct btrfs_block_group_cache *cache;
2012         struct btrfs_fs_info *info = root->fs_info;
2013         u64 total = num_bytes;
2014         u64 old_val;
2015         u64 byte_in_group;
2016
2017         while (total) {
2018                 cache = btrfs_lookup_block_group(info, bytenr);
2019                 if (!cache)
2020                         return -1;
2021                 byte_in_group = bytenr - cache->key.objectid;
2022                 WARN_ON(byte_in_group > cache->key.offset);
2023
2024                 spin_lock(&cache->space_info->lock);
2025                 spin_lock(&cache->lock);
2026                 cache->dirty = 1;
2027                 old_val = btrfs_block_group_used(&cache->item);
2028                 num_bytes = min(total, cache->key.offset - byte_in_group);
2029                 if (alloc) {
2030                         old_val += num_bytes;
2031                         cache->space_info->bytes_used += num_bytes;
2032                         if (cache->ro)
2033                                 cache->space_info->bytes_readonly -= num_bytes;
2034                         btrfs_set_block_group_used(&cache->item, old_val);
2035                         spin_unlock(&cache->lock);
2036                         spin_unlock(&cache->space_info->lock);
2037                 } else {
2038                         old_val -= num_bytes;
2039                         cache->space_info->bytes_used -= num_bytes;
2040                         if (cache->ro)
2041                                 cache->space_info->bytes_readonly += num_bytes;
2042                         btrfs_set_block_group_used(&cache->item, old_val);
2043                         spin_unlock(&cache->lock);
2044                         spin_unlock(&cache->space_info->lock);
2045                         if (mark_free) {
2046                                 int ret;
2047
2048                                 ret = btrfs_discard_extent(root, bytenr,
2049                                                            num_bytes);
2050                                 WARN_ON(ret);
2051
2052                                 ret = btrfs_add_free_space(cache, bytenr,
2053                                                            num_bytes);
2054                                 WARN_ON(ret);
2055                         }
2056                 }
2057                 put_block_group(cache);
2058                 total -= num_bytes;
2059                 bytenr += num_bytes;
2060         }
2061         return 0;
2062 }
2063
2064 static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
2065 {
2066         struct btrfs_block_group_cache *cache;
2067         u64 bytenr;
2068
2069         cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
2070         if (!cache)
2071                 return 0;
2072
2073         bytenr = cache->key.objectid;
2074         put_block_group(cache);
2075
2076         return bytenr;
2077 }
2078
2079 int btrfs_update_pinned_extents(struct btrfs_root *root,
2080                                 u64 bytenr, u64 num, int pin)
2081 {
2082         u64 len;
2083         struct btrfs_block_group_cache *cache;
2084         struct btrfs_fs_info *fs_info = root->fs_info;
2085
2086         WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
2087         if (pin) {
2088                 set_extent_dirty(&fs_info->pinned_extents,
2089                                 bytenr, bytenr + num - 1, GFP_NOFS);
2090         } else {
2091                 clear_extent_dirty(&fs_info->pinned_extents,
2092                                 bytenr, bytenr + num - 1, GFP_NOFS);
2093         }
2094         while (num > 0) {
2095                 cache = btrfs_lookup_block_group(fs_info, bytenr);
2096                 BUG_ON(!cache);
2097                 len = min(num, cache->key.offset -
2098                           (bytenr - cache->key.objectid));
2099                 if (pin) {
2100                         spin_lock(&cache->space_info->lock);
2101                         spin_lock(&cache->lock);
2102                         cache->pinned += len;
2103                         cache->space_info->bytes_pinned += len;
2104                         spin_unlock(&cache->lock);
2105                         spin_unlock(&cache->space_info->lock);
2106                         fs_info->total_pinned += len;
2107                 } else {
2108                         spin_lock(&cache->space_info->lock);
2109                         spin_lock(&cache->lock);
2110                         cache->pinned -= len;
2111                         cache->space_info->bytes_pinned -= len;
2112                         spin_unlock(&cache->lock);
2113                         spin_unlock(&cache->space_info->lock);
2114                         fs_info->total_pinned -= len;
2115                         if (cache->cached)
2116                                 btrfs_add_free_space(cache, bytenr, len);
2117                 }
2118                 put_block_group(cache);
2119                 bytenr += len;
2120                 num -= len;
2121         }
2122         return 0;
2123 }
2124
2125 static int update_reserved_extents(struct btrfs_root *root,
2126                                    u64 bytenr, u64 num, int reserve)
2127 {
2128         u64 len;
2129         struct btrfs_block_group_cache *cache;
2130         struct btrfs_fs_info *fs_info = root->fs_info;
2131
2132         while (num > 0) {
2133                 cache = btrfs_lookup_block_group(fs_info, bytenr);
2134                 BUG_ON(!cache);
2135                 len = min(num, cache->key.offset -
2136                           (bytenr - cache->key.objectid));
2137
2138                 spin_lock(&cache->space_info->lock);
2139                 spin_lock(&cache->lock);
2140                 if (reserve) {
2141                         cache->reserved += len;
2142                         cache->space_info->bytes_reserved += len;
2143                 } else {
2144                         cache->reserved -= len;
2145                         cache->space_info->bytes_reserved -= len;
2146                 }
2147                 spin_unlock(&cache->lock);
2148                 spin_unlock(&cache->space_info->lock);
2149                 put_block_group(cache);
2150                 bytenr += len;
2151                 num -= len;
2152         }
2153         return 0;
2154 }
2155
2156 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
2157 {
2158         u64 last = 0;
2159         u64 start;
2160         u64 end;
2161         struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
2162         int ret;
2163
2164         mutex_lock(&root->fs_info->pinned_mutex);
2165         while (1) {
2166                 ret = find_first_extent_bit(pinned_extents, last,
2167                                             &start, &end, EXTENT_DIRTY);
2168                 if (ret)
2169                         break;
2170                 set_extent_dirty(copy, start, end, GFP_NOFS);
2171                 last = end + 1;
2172         }
2173         mutex_unlock(&root->fs_info->pinned_mutex);
2174         return 0;
2175 }
2176
2177 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2178                                struct btrfs_root *root,
2179                                struct extent_io_tree *unpin)
2180 {
2181         u64 start;
2182         u64 end;
2183         int ret;
2184
2185         mutex_lock(&root->fs_info->pinned_mutex);
2186         while (1) {
2187                 ret = find_first_extent_bit(unpin, 0, &start, &end,
2188                                             EXTENT_DIRTY);
2189                 if (ret)
2190                         break;
2191
2192                 ret = btrfs_discard_extent(root, start, end + 1 - start);
2193
2194                 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
2195                 clear_extent_dirty(unpin, start, end, GFP_NOFS);
2196
2197                 if (need_resched()) {
2198                         mutex_unlock(&root->fs_info->pinned_mutex);
2199                         cond_resched();
2200                         mutex_lock(&root->fs_info->pinned_mutex);
2201                 }
2202         }
2203         mutex_unlock(&root->fs_info->pinned_mutex);
2204         return ret;
2205 }
2206
2207 static int finish_current_insert(struct btrfs_trans_handle *trans,
2208                                  struct btrfs_root *extent_root, int all)
2209 {
2210         u64 start;
2211         u64 end;
2212         u64 priv;
2213         u64 search = 0;
2214         u64 skipped = 0;
2215         struct btrfs_fs_info *info = extent_root->fs_info;
2216         struct btrfs_path *path;
2217         struct pending_extent_op *extent_op, *tmp;
2218         struct list_head insert_list, update_list;
2219         int ret;
2220         int num_inserts = 0, max_inserts;
2221
2222         path = btrfs_alloc_path();
2223         INIT_LIST_HEAD(&insert_list);
2224         INIT_LIST_HEAD(&update_list);
2225
2226         max_inserts = extent_root->leafsize /
2227                 (2 * sizeof(struct btrfs_key) + 2 * sizeof(struct btrfs_item) +
2228                  sizeof(struct btrfs_extent_ref) +
2229                  sizeof(struct btrfs_extent_item));
2230 again:
2231         mutex_lock(&info->extent_ins_mutex);
2232         while (1) {
2233                 ret = find_first_extent_bit(&info->extent_ins, search, &start,
2234                                             &end, EXTENT_WRITEBACK);
2235                 if (ret) {
2236                         if (skipped && all && !num_inserts &&
2237                             list_empty(&update_list)) {
2238                                 skipped = 0;
2239                                 search = 0;
2240                                 continue;
2241                         }
2242                         mutex_unlock(&info->extent_ins_mutex);
2243                         break;
2244                 }
2245
2246                 ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS);
2247                 if (!ret) {
2248                         skipped = 1;
2249                         search = end + 1;
2250                         if (need_resched()) {
2251                                 mutex_unlock(&info->extent_ins_mutex);
2252                                 cond_resched();
2253                                 mutex_lock(&info->extent_ins_mutex);
2254                         }
2255                         continue;
2256                 }
2257
2258                 ret = get_state_private(&info->extent_ins, start, &priv);
2259                 BUG_ON(ret);
2260                 extent_op = (struct pending_extent_op *)(unsigned long) priv;
2261
2262                 if (extent_op->type == PENDING_EXTENT_INSERT) {
2263                         num_inserts++;
2264                         list_add_tail(&extent_op->list, &insert_list);
2265                         search = end + 1;
2266                         if (num_inserts == max_inserts) {
2267                                 mutex_unlock(&info->extent_ins_mutex);
2268                                 break;
2269                         }
2270                 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
2271                         list_add_tail(&extent_op->list, &update_list);
2272                         search = end + 1;
2273                 } else {
2274                         BUG();
2275                 }
2276         }
2277
2278         /*
2279          * process the update list, clear the writeback bit for it, and if
2280          * somebody marked this thing for deletion then just unlock it and be
2281          * done, the free_extents will handle it
2282          */
2283         mutex_lock(&info->extent_ins_mutex);
2284         list_for_each_entry_safe(extent_op, tmp, &update_list, list) {
2285                 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2286                                   extent_op->bytenr + extent_op->num_bytes - 1,
2287                                   EXTENT_WRITEBACK, GFP_NOFS);
2288                 if (extent_op->del) {
2289                         list_del_init(&extent_op->list);
2290                         unlock_extent(&info->extent_ins, extent_op->bytenr,
2291                                       extent_op->bytenr + extent_op->num_bytes
2292                                       - 1, GFP_NOFS);
2293                         kfree(extent_op);
2294                 }
2295         }
2296         mutex_unlock(&info->extent_ins_mutex);
2297
2298         /*
2299          * still have things left on the update list, go ahead an update
2300          * everything
2301          */
2302         if (!list_empty(&update_list)) {
2303                 ret = update_backrefs(trans, extent_root, path, &update_list);
2304                 BUG_ON(ret);
2305         }
2306
2307         /*
2308          * if no inserts need to be done, but we skipped some extents and we
2309          * need to make sure everything is cleaned then reset everything and
2310          * go back to the beginning
2311          */
2312         if (!num_inserts && all && skipped) {
2313                 search = 0;
2314                 skipped = 0;
2315                 INIT_LIST_HEAD(&update_list);
2316                 INIT_LIST_HEAD(&insert_list);
2317                 goto again;
2318         } else if (!num_inserts) {
2319                 goto out;
2320         }
2321
2322         /*
2323          * process the insert extents list.  Again if we are deleting this
2324          * extent, then just unlock it, pin down the bytes if need be, and be
2325          * done with it.  Saves us from having to actually insert the extent
2326          * into the tree and then subsequently come along and delete it
2327          */
2328         mutex_lock(&info->extent_ins_mutex);
2329         list_for_each_entry_safe(extent_op, tmp, &insert_list, list) {
2330                 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2331                                   extent_op->bytenr + extent_op->num_bytes - 1,
2332                                   EXTENT_WRITEBACK, GFP_NOFS);
2333                 if (extent_op->del) {
2334                         u64 used;
2335                         list_del_init(&extent_op->list);
2336                         unlock_extent(&info->extent_ins, extent_op->bytenr,
2337                                       extent_op->bytenr + extent_op->num_bytes
2338                                       - 1, GFP_NOFS);
2339
2340                         mutex_lock(&extent_root->fs_info->pinned_mutex);
2341                         ret = pin_down_bytes(trans, extent_root,
2342                                              extent_op->bytenr,
2343                                              extent_op->num_bytes, 0);
2344                         mutex_unlock(&extent_root->fs_info->pinned_mutex);
2345
2346                         spin_lock(&info->delalloc_lock);
2347                         used = btrfs_super_bytes_used(&info->super_copy);
2348                         btrfs_set_super_bytes_used(&info->super_copy,
2349                                         used - extent_op->num_bytes);
2350                         used = btrfs_root_used(&extent_root->root_item);
2351                         btrfs_set_root_used(&extent_root->root_item,
2352                                         used - extent_op->num_bytes);
2353                         spin_unlock(&info->delalloc_lock);
2354
2355                         ret = update_block_group(trans, extent_root,
2356                                                  extent_op->bytenr,
2357                                                  extent_op->num_bytes,
2358                                                  0, ret > 0);
2359                         BUG_ON(ret);
2360                         kfree(extent_op);
2361                         num_inserts--;
2362                 }
2363         }
2364         mutex_unlock(&info->extent_ins_mutex);
2365
2366         ret = insert_extents(trans, extent_root, path, &insert_list,
2367                              num_inserts);
2368         BUG_ON(ret);
2369
2370         /*
2371          * if we broke out of the loop in order to insert stuff because we hit
2372          * the maximum number of inserts at a time we can handle, then loop
2373          * back and pick up where we left off
2374          */
2375         if (num_inserts == max_inserts) {
2376                 INIT_LIST_HEAD(&insert_list);
2377                 INIT_LIST_HEAD(&update_list);
2378                 num_inserts = 0;
2379                 goto again;
2380         }
2381
2382         /*
2383          * again, if we need to make absolutely sure there are no more pending
2384          * extent operations left and we know that we skipped some, go back to
2385          * the beginning and do it all again
2386          */
2387         if (all && skipped) {
2388                 INIT_LIST_HEAD(&insert_list);
2389                 INIT_LIST_HEAD(&update_list);
2390                 search = 0;
2391                 skipped = 0;
2392                 num_inserts = 0;
2393                 goto again;
2394         }
2395 out:
2396         btrfs_free_path(path);
2397         return 0;
2398 }
2399
2400 static int pin_down_bytes(struct btrfs_trans_handle *trans,
2401                           struct btrfs_root *root,
2402                           u64 bytenr, u64 num_bytes, int is_data)
2403 {
2404         int err = 0;
2405         struct extent_buffer *buf;
2406
2407         if (is_data)
2408                 goto pinit;
2409
2410         buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2411         if (!buf)
2412                 goto pinit;
2413
2414         /* we can reuse a block if it hasn't been written
2415          * and it is from this transaction.  We can't
2416          * reuse anything from the tree log root because
2417          * it has tiny sub-transactions.
2418          */
2419         if (btrfs_buffer_uptodate(buf, 0) &&
2420             btrfs_try_tree_lock(buf)) {
2421                 u64 header_owner = btrfs_header_owner(buf);
2422                 u64 header_transid = btrfs_header_generation(buf);
2423                 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
2424                     header_owner != BTRFS_TREE_RELOC_OBJECTID &&
2425                     header_transid == trans->transid &&
2426                     !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2427                         clean_tree_block(NULL, root, buf);
2428                         btrfs_tree_unlock(buf);
2429                         free_extent_buffer(buf);
2430                         return 1;
2431                 }
2432                 btrfs_tree_unlock(buf);
2433         }
2434         free_extent_buffer(buf);
2435 pinit:
2436         btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2437
2438         BUG_ON(err < 0);
2439         return 0;
2440 }
2441
2442 /*
2443  * remove an extent from the root, returns 0 on success
2444  */
2445 static int __free_extent(struct btrfs_trans_handle *trans,
2446                          struct btrfs_root *root,
2447                          u64 bytenr, u64 num_bytes, u64 parent,
2448                          u64 root_objectid, u64 ref_generation,
2449                          u64 owner_objectid, int pin, int mark_free)
2450 {
2451         struct btrfs_path *path;
2452         struct btrfs_key key;
2453         struct btrfs_fs_info *info = root->fs_info;
2454         struct btrfs_root *extent_root = info->extent_root;
2455         struct extent_buffer *leaf;
2456         int ret;
2457         int extent_slot = 0;
2458         int found_extent = 0;
2459         int num_to_del = 1;
2460         struct btrfs_extent_item *ei;
2461         u32 refs;
2462
2463         key.objectid = bytenr;
2464         btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
2465         key.offset = num_bytes;
2466         path = btrfs_alloc_path();
2467         if (!path)
2468                 return -ENOMEM;
2469
2470         path->reada = 1;
2471         ret = lookup_extent_backref(trans, extent_root, path,
2472                                     bytenr, parent, root_objectid,
2473                                     ref_generation, owner_objectid, 1);
2474         if (ret == 0) {
2475                 struct btrfs_key found_key;
2476                 extent_slot = path->slots[0];
2477                 while (extent_slot > 0) {
2478                         extent_slot--;
2479                         btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2480                                               extent_slot);
2481                         if (found_key.objectid != bytenr)
2482                                 break;
2483                         if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
2484                             found_key.offset == num_bytes) {
2485                                 found_extent = 1;
2486                                 break;
2487                         }
2488                         if (path->slots[0] - extent_slot > 5)
2489                                 break;
2490                 }
2491                 if (!found_extent) {
2492                         ret = remove_extent_backref(trans, extent_root, path);
2493                         BUG_ON(ret);
2494                         btrfs_release_path(extent_root, path);
2495                         ret = btrfs_search_slot(trans, extent_root,
2496                                                 &key, path, -1, 1);
2497                         if (ret) {
2498                                 printk(KERN_ERR "umm, got %d back from search"
2499                                        ", was looking for %llu\n", ret,
2500                                        (unsigned long long)bytenr);
2501                                 btrfs_print_leaf(extent_root, path->nodes[0]);
2502                         }
2503                         BUG_ON(ret);
2504                         extent_slot = path->slots[0];
2505                 }
2506         } else {
2507                 btrfs_print_leaf(extent_root, path->nodes[0]);
2508                 WARN_ON(1);
2509                 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
2510                        "root %llu gen %llu owner %llu\n",
2511                        (unsigned long long)bytenr,
2512                        (unsigned long long)root_objectid,
2513                        (unsigned long long)ref_generation,
2514                        (unsigned long long)owner_objectid);
2515         }
2516
2517         leaf = path->nodes[0];
2518         ei = btrfs_item_ptr(leaf, extent_slot,
2519                             struct btrfs_extent_item);
2520         refs = btrfs_extent_refs(leaf, ei);
2521         BUG_ON(refs == 0);
2522         refs -= 1;
2523         btrfs_set_extent_refs(leaf, ei, refs);
2524
2525         btrfs_mark_buffer_dirty(leaf);
2526
2527         if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
2528                 struct btrfs_extent_ref *ref;
2529                 ref = btrfs_item_ptr(leaf, path->slots[0],
2530                                      struct btrfs_extent_ref);
2531                 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
2532                 /* if the back ref and the extent are next to each other
2533                  * they get deleted below in one shot
2534                  */
2535                 path->slots[0] = extent_slot;
2536                 num_to_del = 2;
2537         } else if (found_extent) {
2538                 /* otherwise delete the extent back ref */
2539                 ret = remove_extent_backref(trans, extent_root, path);
2540                 BUG_ON(ret);
2541                 /* if refs are 0, we need to setup the path for deletion */
2542                 if (refs == 0) {
2543                         btrfs_release_path(extent_root, path);
2544                         ret = btrfs_search_slot(trans, extent_root, &key, path,
2545                                                 -1, 1);
2546                         BUG_ON(ret);
2547                 }
2548         }
2549
2550         if (refs == 0) {
2551                 u64 super_used;
2552                 u64 root_used;
2553
2554                 if (pin) {
2555                         mutex_lock(&root->fs_info->pinned_mutex);
2556                         ret = pin_down_bytes(trans, root, bytenr, num_bytes,
2557                                 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
2558                         mutex_unlock(&root->fs_info->pinned_mutex);
2559                         if (ret > 0)
2560                                 mark_free = 1;
2561                         BUG_ON(ret < 0);
2562                 }
2563                 /* block accounting for super block */
2564                 spin_lock(&info->delalloc_lock);
2565                 super_used = btrfs_super_bytes_used(&info->super_copy);
2566                 btrfs_set_super_bytes_used(&info->super_copy,
2567                                            super_used - num_bytes);
2568
2569                 /* block accounting for root item */
2570                 root_used = btrfs_root_used(&root->root_item);
2571                 btrfs_set_root_used(&root->root_item,
2572                                            root_used - num_bytes);
2573                 spin_unlock(&info->delalloc_lock);
2574                 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2575                                       num_to_del);
2576                 BUG_ON(ret);
2577                 btrfs_release_path(extent_root, path);
2578
2579                 if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
2580                         ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
2581                         BUG_ON(ret);
2582                 }
2583
2584                 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
2585                                          mark_free);
2586                 BUG_ON(ret);
2587         }
2588         btrfs_free_path(path);
2589         finish_current_insert(trans, extent_root, 0);
2590         return ret;
2591 }
2592
2593 /*
2594  * find all the blocks marked as pending in the radix tree and remove
2595  * them from the extent map
2596  */
2597 static int del_pending_extents(struct btrfs_trans_handle *trans,
2598                                struct btrfs_root *extent_root, int all)
2599 {
2600         int ret;
2601         int err = 0;
2602         u64 start;
2603         u64 end;
2604         u64 priv;
2605         u64 search = 0;
2606         int nr = 0, skipped = 0;
2607         struct extent_io_tree *pending_del;
2608         struct extent_io_tree *extent_ins;
2609         struct pending_extent_op *extent_op;
2610         struct btrfs_fs_info *info = extent_root->fs_info;
2611         struct list_head delete_list;
2612
2613         INIT_LIST_HEAD(&delete_list);
2614         extent_ins = &extent_root->fs_info->extent_ins;
2615         pending_del = &extent_root->fs_info->pending_del;
2616
2617 again:
2618         mutex_lock(&info->extent_ins_mutex);
2619         while (1) {
2620                 ret = find_first_extent_bit(pending_del, search, &start, &end,
2621                                             EXTENT_WRITEBACK);
2622                 if (ret) {
2623                         if (all && skipped && !nr) {
2624                                 search = 0;
2625                                 skipped = 0;
2626                                 continue;
2627                         }
2628                         mutex_unlock(&info->extent_ins_mutex);
2629                         break;
2630                 }
2631
2632                 ret = try_lock_extent(extent_ins, start, end, GFP_NOFS);
2633                 if (!ret) {
2634                         search = end+1;
2635                         skipped = 1;
2636
2637                         if (need_resched()) {
2638                                 mutex_unlock(&info->extent_ins_mutex);
2639                                 cond_resched();
2640                                 mutex_lock(&info->extent_ins_mutex);
2641                         }
2642
2643                         continue;
2644                 }
2645                 BUG_ON(ret < 0);
2646
2647                 ret = get_state_private(pending_del, start, &priv);
2648                 BUG_ON(ret);
2649                 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2650
2651                 clear_extent_bits(pending_del, start, end, EXTENT_WRITEBACK,
2652                                   GFP_NOFS);
2653                 if (!test_range_bit(extent_ins, start, end,
2654                                     EXTENT_WRITEBACK, 0)) {
2655                         list_add_tail(&extent_op->list, &delete_list);
2656                         nr++;
2657                 } else {
2658                         kfree(extent_op);
2659
2660                         ret = get_state_private(&info->extent_ins, start,
2661                                                 &priv);
2662                         BUG_ON(ret);
2663                         extent_op = (struct pending_extent_op *)
2664                                                 (unsigned long)priv;
2665
2666                         clear_extent_bits(&info->extent_ins, start, end,
2667                                           EXTENT_WRITEBACK, GFP_NOFS);
2668
2669                         if (extent_op->type == PENDING_BACKREF_UPDATE) {
2670                                 list_add_tail(&extent_op->list, &delete_list);
2671                                 search = end + 1;
2672                                 nr++;
2673                                 continue;
2674                         }
2675
2676                         mutex_lock(&extent_root->fs_info->pinned_mutex);
2677                         ret = pin_down_bytes(trans, extent_root, start,
2678                                              end + 1 - start, 0);
2679                         mutex_unlock(&extent_root->fs_info->pinned_mutex);
2680
2681                         ret = update_block_group(trans, extent_root, start,
2682                                                 end + 1 - start, 0, ret > 0);
2683
2684                         unlock_extent(extent_ins, start, end, GFP_NOFS);
2685                         BUG_ON(ret);
2686                         kfree(extent_op);
2687                 }
2688                 if (ret)
2689                         err = ret;
2690
2691                 search = end + 1;
2692
2693                 if (need_resched()) {
2694                         mutex_unlock(&info->extent_ins_mutex);
2695                         cond_resched();
2696                         mutex_lock(&info->extent_ins_mutex);
2697                 }
2698         }
2699
2700         if (nr) {
2701                 ret = free_extents(trans, extent_root, &delete_list);
2702                 BUG_ON(ret);
2703         }
2704
2705         if (all && skipped) {
2706                 INIT_LIST_HEAD(&delete_list);
2707                 search = 0;
2708                 nr = 0;
2709                 goto again;
2710         }
2711
2712         return err;
2713 }
2714
2715 /*
2716  * remove an extent from the root, returns 0 on success
2717  */
2718 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
2719                                struct btrfs_root *root,
2720                                u64 bytenr, u64 num_bytes, u64 parent,
2721                                u64 root_objectid, u64 ref_generation,
2722                                u64 owner_objectid, int pin)
2723 {
2724         struct btrfs_root *extent_root = root->fs_info->extent_root;
2725         int pending_ret;
2726         int ret;
2727
2728         WARN_ON(num_bytes < root->sectorsize);
2729         if (root == extent_root) {
2730                 struct pending_extent_op *extent_op = NULL;
2731
2732                 mutex_lock(&root->fs_info->extent_ins_mutex);
2733                 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
2734                                 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
2735                         u64 priv;
2736                         ret = get_state_private(&root->fs_info->extent_ins,
2737                                                 bytenr, &priv);
2738                         BUG_ON(ret);
2739                         extent_op = (struct pending_extent_op *)
2740                                                 (unsigned long)priv;
2741
2742                         extent_op->del = 1;
2743                         if (extent_op->type == PENDING_EXTENT_INSERT) {
2744                                 mutex_unlock(&root->fs_info->extent_ins_mutex);
2745                                 return 0;
2746                         }
2747                 }
2748
2749                 if (extent_op) {
2750                         ref_generation = extent_op->orig_generation;
2751                         parent = extent_op->orig_parent;
2752                 }
2753
2754                 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2755                 BUG_ON(!extent_op);
2756
2757                 extent_op->type = PENDING_EXTENT_DELETE;
2758                 extent_op->bytenr = bytenr;
2759                 extent_op->num_bytes = num_bytes;
2760                 extent_op->parent = parent;
2761                 extent_op->orig_parent = parent;
2762                 extent_op->generation = ref_generation;
2763                 extent_op->orig_generation = ref_generation;
2764                 extent_op->level = (int)owner_objectid;
2765                 INIT_LIST_HEAD(&extent_op->list);
2766                 extent_op->del = 0;
2767
2768                 set_extent_bits(&root->fs_info->pending_del,
2769                                 bytenr, bytenr + num_bytes - 1,
2770                                 EXTENT_WRITEBACK, GFP_NOFS);
2771                 set_state_private(&root->fs_info->pending_del,
2772                                   bytenr, (unsigned long)extent_op);
2773                 mutex_unlock(&root->fs_info->extent_ins_mutex);
2774                 return 0;
2775         }
2776         /* if metadata always pin */
2777         if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2778                 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
2779                         mutex_lock(&root->fs_info->pinned_mutex);
2780                         btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2781                         mutex_unlock(&root->fs_info->pinned_mutex);
2782                         update_reserved_extents(root, bytenr, num_bytes, 0);
2783                         return 0;
2784                 }
2785                 pin = 1;
2786         }
2787
2788         /* if data pin when any transaction has committed this */
2789         if (ref_generation != trans->transid)
2790                 pin = 1;
2791
2792         ret = __free_extent(trans, root, bytenr, num_bytes, parent,
2793                             root_objectid, ref_generation,
2794                             owner_objectid, pin, pin == 0);
2795
2796         finish_current_insert(trans, root->fs_info->extent_root, 0);
2797         pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0);
2798         return ret ? ret : pending_ret;
2799 }
2800
2801 int btrfs_free_extent(struct btrfs_trans_handle *trans,
2802                       struct btrfs_root *root,
2803                       u64 bytenr, u64 num_bytes, u64 parent,
2804                       u64 root_objectid, u64 ref_generation,
2805                       u64 owner_objectid, int pin)
2806 {
2807         int ret;
2808
2809         ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
2810                                   root_objectid, ref_generation,
2811                                   owner_objectid, pin);
2812         return ret;
2813 }
2814
2815 static u64 stripe_align(struct btrfs_root *root, u64 val)
2816 {
2817         u64 mask = ((u64)root->stripesize - 1);
2818         u64 ret = (val + mask) & ~mask;
2819         return ret;
2820 }
2821
2822 /*
2823  * walks the btree of allocated extents and find a hole of a given size.
2824  * The key ins is changed to record the hole:
2825  * ins->objectid == block start
2826  * ins->flags = BTRFS_EXTENT_ITEM_KEY
2827  * ins->offset == number of blocks
2828  * Any available blocks before search_start are skipped.
2829  */
2830 static noinline int find_free_extent(struct btrfs_trans_handle *trans,
2831                                      struct btrfs_root *orig_root,
2832                                      u64 num_bytes, u64 empty_size,
2833                                      u64 search_start, u64 search_end,
2834                                      u64 hint_byte, struct btrfs_key *ins,
2835                                      u64 exclude_start, u64 exclude_nr,
2836                                      int data)
2837 {
2838         int ret = 0;
2839         struct btrfs_root *root = orig_root->fs_info->extent_root;
2840         u64 total_needed = num_bytes;
2841         u64 *last_ptr = NULL;
2842         u64 last_wanted = 0;
2843         struct btrfs_block_group_cache *block_group = NULL;
2844         int chunk_alloc_done = 0;
2845         int empty_cluster = 2 * 1024 * 1024;
2846         int allowed_chunk_alloc = 0;
2847         struct list_head *head = NULL, *cur = NULL;
2848         int loop = 0;
2849         int extra_loop = 0;
2850         struct btrfs_space_info *space_info;
2851
2852         WARN_ON(num_bytes < root->sectorsize);
2853         btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
2854         ins->objectid = 0;
2855         ins->offset = 0;
2856
2857         if (orig_root->ref_cows || empty_size)
2858                 allowed_chunk_alloc = 1;
2859
2860         if (data & BTRFS_BLOCK_GROUP_METADATA) {
2861                 last_ptr = &root->fs_info->last_alloc;
2862                 empty_cluster = 64 * 1024;
2863         }
2864
2865         if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
2866                 last_ptr = &root->fs_info->last_data_alloc;
2867
2868         if (last_ptr) {
2869                 if (*last_ptr) {
2870                         hint_byte = *last_ptr;
2871                         last_wanted = *last_ptr;
2872                 } else
2873                         empty_size += empty_cluster;
2874         } else {
2875                 empty_cluster = 0;
2876         }
2877         search_start = max(search_start, first_logical_byte(root, 0));
2878         search_start = max(search_start, hint_byte);
2879
2880         if (last_wanted && search_start != last_wanted) {
2881                 last_wanted = 0;
2882                 empty_size += empty_cluster;
2883         }
2884
2885         total_needed += empty_size;
2886         block_group = btrfs_lookup_block_group(root->fs_info, search_start);
2887         if (!block_group)
2888                 block_group = btrfs_lookup_first_block_group(root->fs_info,
2889                                                              search_start);
2890         space_info = __find_space_info(root->fs_info, data);
2891
2892         down_read(&space_info->groups_sem);
2893         while (1) {
2894                 struct btrfs_free_space *free_space;
2895                 /*
2896                  * the only way this happens if our hint points to a block
2897                  * group thats not of the proper type, while looping this
2898                  * should never happen
2899                  */
2900                 if (empty_size)
2901                         extra_loop = 1;
2902
2903                 if (!block_group)
2904                         goto new_group_no_lock;
2905
2906                 if (unlikely(!block_group->cached)) {
2907                         mutex_lock(&block_group->cache_mutex);
2908                         ret = cache_block_group(root, block_group);
2909                         mutex_unlock(&block_group->cache_mutex);
2910                         if (ret)
2911                                 break;
2912                 }
2913
2914                 mutex_lock(&block_group->alloc_mutex);
2915                 if (unlikely(!block_group_bits(block_group, data)))
2916                         goto new_group;
2917
2918                 if (unlikely(block_group->ro))
2919                         goto new_group;
2920
2921                 free_space = btrfs_find_free_space(block_group, search_start,
2922                                                    total_needed);
2923                 if (free_space) {
2924                         u64 start = block_group->key.objectid;
2925                         u64 end = block_group->key.objectid +
2926                                 block_group->key.offset;
2927
2928                         search_start = stripe_align(root, free_space->offset);
2929
2930                         /* move on to the next group */
2931                         if (search_start + num_bytes >= search_end)
2932                                 goto new_group;
2933
2934                         /* move on to the next group */
2935                         if (search_start + num_bytes > end)
2936                                 goto new_group;
2937
2938                         if (last_wanted && search_start != last_wanted) {
2939                                 total_needed += empty_cluster;
2940                                 empty_size += empty_cluster;
2941                                 last_wanted = 0;
2942                                 /*
2943                                  * if search_start is still in this block group
2944                                  * then we just re-search this block group
2945                                  */
2946                                 if (search_start >= start &&
2947                                     search_start < end) {
2948                                         mutex_unlock(&block_group->alloc_mutex);
2949                                         continue;
2950                                 }
2951
2952                                 /* else we go to the next block group */
2953                                 goto new_group;
2954                         }
2955
2956                         if (exclude_nr > 0 &&
2957                             (search_start + num_bytes > exclude_start &&
2958                              search_start < exclude_start + exclude_nr)) {
2959                                 search_start = exclude_start + exclude_nr;
2960                                 /*
2961                                  * if search_start is still in this block group
2962                                  * then we just re-search this block group
2963                                  */
2964                                 if (search_start >= start &&
2965                                     search_start < end) {
2966                                         mutex_unlock(&block_group->alloc_mutex);
2967                                         last_wanted = 0;
2968                                         continue;
2969                                 }
2970
2971                                 /* else we go to the next block group */
2972                                 goto new_group;
2973                         }
2974
2975                         ins->objectid = search_start;
2976                         ins->offset = num_bytes;
2977
2978                         btrfs_remove_free_space_lock(block_group, search_start,
2979                                                      num_bytes);
2980                         /* we are all good, lets return */
2981                         mutex_unlock(&block_group->alloc_mutex);
2982                         break;
2983                 }
2984 new_group:
2985                 mutex_unlock(&block_group->alloc_mutex);
2986                 put_block_group(block_group);
2987                 block_group = NULL;
2988 new_group_no_lock:
2989                 /* don't try to compare new allocations against the
2990                  * last allocation any more
2991                  */
2992                 last_wanted = 0;
2993
2994                 /*
2995                  * Here's how this works.
2996                  * loop == 0: we were searching a block group via a hint
2997                  *              and didn't find anything, so we start at
2998                  *              the head of the block groups and keep searching
2999                  * loop == 1: we're searching through all of the block groups
3000                  *              if we hit the head again we have searched
3001                  *              all of the block groups for this space and we
3002                  *              need to try and allocate, if we cant error out.
3003                  * loop == 2: we allocated more space and are looping through
3004                  *              all of the block groups again.
3005                  */
3006                 if (loop == 0) {
3007                         head = &space_info->block_groups;
3008                         cur = head->next;
3009                         loop++;
3010                 } else if (loop == 1 && cur == head) {
3011                         int keep_going;
3012
3013                         /* at this point we give up on the empty_size
3014                          * allocations and just try to allocate the min
3015                          * space.
3016                          *
3017                          * The extra_loop field was set if an empty_size
3018                          * allocation was attempted above, and if this
3019                          * is try we need to try the loop again without
3020                          * the additional empty_size.
3021                          */
3022                         total_needed -= empty_size;
3023                         empty_size = 0;
3024                         keep_going = extra_loop;
3025                         loop++;
3026
3027                         if (allowed_chunk_alloc && !chunk_alloc_done) {
3028                                 up_read(&space_info->groups_sem);
3029                                 ret = do_chunk_alloc(trans, root, num_bytes +
3030                                                      2 * 1024 * 1024, data, 1);
3031                                 down_read(&space_info->groups_sem);
3032                                 if (ret < 0)
3033                                         goto loop_check;
3034                                 head = &space_info->block_groups;
3035                                 /*
3036                                  * we've allocated a new chunk, keep
3037                                  * trying
3038                                  */
3039                                 keep_going = 1;
3040                                 chunk_alloc_done = 1;
3041                         } else if (!allowed_chunk_alloc) {
3042                                 space_info->force_alloc = 1;
3043                         }
3044 loop_check:
3045                         if (keep_going) {
3046                                 cur = head->next;
3047                                 extra_loop = 0;
3048                         } else {
3049                                 break;
3050                         }
3051                 } else if (cur == head) {
3052                         break;
3053                 }
3054
3055                 block_group = list_entry(cur, struct btrfs_block_group_cache,
3056                                          list);
3057                 atomic_inc(&block_group->count);
3058
3059                 search_start = block_group->key.objectid;
3060                 cur = cur->next;
3061         }
3062
3063         /* we found what we needed */
3064         if (ins->objectid) {
3065                 if (!(data & BTRFS_BLOCK_GROUP_DATA))
3066                         trans->block_group = block_group->key.objectid;
3067
3068                 if (last_ptr)
3069                         *last_ptr = ins->objectid + ins->offset;
3070                 ret = 0;
3071         } else if (!ret) {
3072                 printk(KERN_ERR "btrfs searching for %llu bytes, "
3073                        "num_bytes %llu, loop %d, allowed_alloc %d\n",
3074                        (unsigned long long)total_needed,
3075                        (unsigned long long)num_bytes,
3076                        loop, allowed_chunk_alloc);
3077                 ret = -ENOSPC;
3078         }
3079         if (block_group)
3080                 put_block_group(block_group);
3081
3082         up_read(&space_info->groups_sem);
3083         return ret;
3084 }
3085
3086 static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
3087 {
3088         struct btrfs_block_group_cache *cache;
3089
3090         printk(KERN_INFO "space_info has %llu free, is %sfull\n",
3091                (unsigned long long)(info->total_bytes - info->bytes_used -
3092                                     info->bytes_pinned - info->bytes_reserved),
3093                (info->full) ? "" : "not ");
3094
3095         down_read(&info->groups_sem);
3096         list_for_each_entry(cache, &info->block_groups, list) {
3097                 spin_lock(&cache->lock);
3098                 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
3099                        "%llu pinned %llu reserved\n",
3100                        (unsigned long long)cache->key.objectid,
3101                        (unsigned long long)cache->key.offset,
3102                        (unsigned long long)btrfs_block_group_used(&cache->item),
3103                        (unsigned long long)cache->pinned,
3104                        (unsigned long long)cache->reserved);
3105                 btrfs_dump_free_space(cache, bytes);
3106                 spin_unlock(&cache->lock);
3107         }
3108         up_read(&info->groups_sem);
3109 }
3110
3111 static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3112                                   struct btrfs_root *root,
3113                                   u64 num_bytes, u64 min_alloc_size,
3114                                   u64 empty_size, u64 hint_byte,
3115                                   u64 search_end, struct btrfs_key *ins,
3116                                   u64 data)
3117 {
3118         int ret;
3119         u64 search_start = 0;
3120         u64 alloc_profile;
3121         struct btrfs_fs_info *info = root->fs_info;
3122
3123         if (data) {
3124                 alloc_profile = info->avail_data_alloc_bits &
3125                         info->data_alloc_profile;
3126                 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
3127         } else if (root == root->fs_info->chunk_root) {
3128                 alloc_profile = info->avail_system_alloc_bits &
3129                         info->system_alloc_profile;
3130                 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
3131         } else {
3132                 alloc_profile = info->avail_metadata_alloc_bits &
3133                         info->metadata_alloc_profile;
3134                 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
3135         }
3136 again:
3137         data = btrfs_reduce_alloc_profile(root, data);
3138         /*
3139          * the only place that sets empty_size is btrfs_realloc_node, which
3140          * is not called recursively on allocations
3141          */
3142         if (empty_size || root->ref_cows) {
3143                 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
3144                         ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3145                                      2 * 1024 * 1024,
3146                                      BTRFS_BLOCK_GROUP_METADATA |
3147                                      (info->metadata_alloc_profile &
3148                                       info->avail_metadata_alloc_bits), 0);
3149                 }
3150                 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3151                                      num_bytes + 2 * 1024 * 1024, data, 0);
3152         }
3153
3154         WARN_ON(num_bytes < root->sectorsize);
3155         ret = find_free_extent(trans, root, num_bytes, empty_size,
3156                                search_start, search_end, hint_byte, ins,
3157                                trans->alloc_exclude_start,
3158                                trans->alloc_exclude_nr, data);
3159
3160         if (ret == -ENOSPC && num_bytes > min_alloc_size) {
3161                 num_bytes = num_bytes >> 1;
3162                 num_bytes = num_bytes & ~(root->sectorsize - 1);
3163                 num_bytes = max(num_bytes, min_alloc_size);
3164                 do_chunk_alloc(trans, root->fs_info->extent_root,
3165                                num_bytes, data, 1);
3166                 goto again;
3167         }
3168         if (ret) {
3169                 struct btrfs_space_info *sinfo;
3170
3171                 sinfo = __find_space_info(root->fs_info, data);
3172                 printk(KERN_ERR "btrfs allocation failed flags %llu, "
3173                        "wanted %llu\n", (unsigned long long)data,
3174                        (unsigned long long)num_bytes);
3175                 dump_space_info(sinfo, num_bytes);
3176                 BUG();
3177         }
3178
3179         return ret;
3180 }
3181
3182 int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
3183 {
3184         struct btrfs_block_group_cache *cache;
3185         int ret = 0;
3186
3187         cache = btrfs_lookup_block_group(root->fs_info, start);
3188         if (!cache) {
3189                 printk(KERN_ERR "Unable to find block group for %llu\n",
3190                        (unsigned long long)start);
3191                 return -ENOSPC;
3192         }
3193
3194         ret = btrfs_discard_extent(root, start, len);
3195
3196         btrfs_add_free_space(cache, start, len);
3197         put_block_group(cache);
3198         update_reserved_extents(root, start, len, 0);
3199
3200         return ret;
3201 }
3202
3203 int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3204                                   struct btrfs_root *root,
3205                                   u64 num_bytes, u64 min_alloc_size,
3206                                   u64 empty_size, u64 hint_byte,
3207                                   u64 search_end, struct btrfs_key *ins,
3208                                   u64 data)
3209 {
3210         int ret;
3211         ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
3212                                      empty_size, hint_byte, search_end, ins,
3213                                      data);
3214         update_reserved_extents(root, ins->objectid, ins->offset, 1);
3215         return ret;
3216 }
3217
3218 static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
3219                                          struct btrfs_root *root, u64 parent,
3220                                          u64 root_objectid, u64 ref_generation,
3221                                          u64 owner, struct btrfs_key *ins)
3222 {
3223         int ret;
3224         int pending_ret;
3225         u64 super_used;
3226         u64 root_used;
3227         u64 num_bytes = ins->offset;
3228         u32 sizes[2];
3229         struct btrfs_fs_info *info = root->fs_info;
3230         struct btrfs_root *extent_root = info->extent_root;
3231         struct btrfs_extent_item *extent_item;
3232         struct btrfs_extent_ref *ref;
3233         struct btrfs_path *path;
3234         struct btrfs_key keys[2];
3235
3236         if (parent == 0)
3237                 parent = ins->objectid;
3238
3239         /* block accounting for super block */
3240         spin_lock(&info->delalloc_lock);
3241         super_used = btrfs_super_bytes_used(&info->super_copy);
3242         btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
3243
3244         /* block accounting for root item */
3245         root_used = btrfs_root_used(&root->root_item);
3246         btrfs_set_root_used(&root->root_item, root_used + num_bytes);
3247         spin_unlock(&info->delalloc_lock);
3248
3249         if (root == extent_root) {
3250                 struct pending_extent_op *extent_op;
3251
3252                 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
3253                 BUG_ON(!extent_op);
3254
3255                 extent_op->type = PENDING_EXTENT_INSERT;
3256                 extent_op->bytenr = ins->objectid;
3257                 extent_op->num_bytes = ins->offset;
3258                 extent_op->parent = parent;
3259                 extent_op->orig_parent = 0;
3260                 extent_op->generation = ref_generation;
3261                 extent_op->orig_generation = 0;
3262                 extent_op->level = (int)owner;
3263                 INIT_LIST_HEAD(&extent_op->list);
3264                 extent_op->del = 0;
3265
3266                 mutex_lock(&root->fs_info->extent_ins_mutex);
3267                 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
3268                                 ins->objectid + ins->offset - 1,
3269                                 EXTENT_WRITEBACK, GFP_NOFS);
3270                 set_state_private(&root->fs_info->extent_ins,
3271                                   ins->objectid, (unsigned long)extent_op);
3272                 mutex_unlock(&root->fs_info->extent_ins_mutex);
3273                 goto update_block;
3274         }
3275
3276         memcpy(&keys[0], ins, sizeof(*ins));
3277         keys[1].objectid = ins->objectid;
3278         keys[1].type = BTRFS_EXTENT_REF_KEY;
3279         keys[1].offset = parent;
3280         sizes[0] = sizeof(*extent_item);
3281         sizes[1] = sizeof(*ref);
3282
3283         path = btrfs_alloc_path();
3284         BUG_ON(!path);
3285
3286         ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
3287                                        sizes, 2);
3288         BUG_ON(ret);
3289
3290         extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3291                                      struct btrfs_extent_item);
3292         btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
3293         ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3294                              struct btrfs_extent_ref);
3295
3296         btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
3297         btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
3298         btrfs_set_ref_objectid(path->nodes[0], ref, owner);
3299         btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
3300
3301         btrfs_mark_buffer_dirty(path->nodes[0]);
3302
3303         trans->alloc_exclude_start = 0;
3304         trans->alloc_exclude_nr = 0;
3305         btrfs_free_path(path);
3306         finish_current_insert(trans, extent_root, 0);
3307         pending_ret = del_pending_extents(trans, extent_root, 0);
3308
3309         if (ret)
3310                 goto out;
3311         if (pending_ret) {
3312                 ret = pending_ret;
3313                 goto out;
3314         }
3315
3316 update_block:
3317         ret = update_block_group(trans, root, ins->objectid,
3318                                  ins->offset, 1, 0);
3319         if (ret) {
3320                 printk(KERN_ERR "btrfs update block group failed for %llu "
3321                        "%llu\n", (unsigned long long)ins->objectid,
3322                        (unsigned long long)ins->offset);
3323                 BUG();
3324         }
3325 out:
3326         return ret;
3327 }
3328
3329 int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
3330                                 struct btrfs_root *root, u64 parent,
3331                                 u64 root_objectid, u64 ref_generation,
3332                                 u64 owner, struct btrfs_key *ins)
3333 {
3334         int ret;
3335
3336         if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
3337                 return 0;
3338         ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3339                                             ref_generation, owner, ins);
3340         update_reserved_extents(root, ins->objectid, ins->offset, 0);
3341         return ret;
3342 }
3343
3344 /*
3345  * this is used by the tree logging recovery code.  It records that
3346  * an extent has been allocated and makes sure to clear the free
3347  * space cache bits as well
3348  */
3349 int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
3350                                 struct btrfs_root *root, u64 parent,
3351                                 u64 root_objectid, u64 ref_generation,
3352                                 u64 owner, struct btrfs_key *ins)
3353 {
3354         int ret;
3355         struct btrfs_block_group_cache *block_group;
3356
3357         block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
3358         mutex_lock(&block_group->cache_mutex);
3359         cache_block_group(root, block_group);
3360         mutex_unlock(&block_group->cache_mutex);
3361
3362         ret = btrfs_remove_free_space(block_group, ins->objectid,
3363                                       ins->offset);
3364         BUG_ON(ret);
3365         put_block_group(block_group);
3366         ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3367                                             ref_generation, owner, ins);
3368         return ret;
3369 }
3370
3371 /*
3372  * finds a free extent and does all the dirty work required for allocation
3373  * returns the key for the extent through ins, and a tree buffer for
3374  * the first block of the extent through buf.
3375  *
3376  * returns 0 if everything worked, non-zero otherwise.
3377  */
3378 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
3379                        struct btrfs_root *root,
3380                        u64 num_bytes, u64 parent, u64 min_alloc_size,
3381                        u64 root_objectid, u64 ref_generation,
3382                        u64 owner_objectid, u64 empty_size, u64 hint_byte,
3383                        u64 search_end, struct btrfs_key *ins, u64 data)
3384 {
3385         int ret;
3386
3387         ret = __btrfs_reserve_extent(trans, root, num_bytes,
3388                                      min_alloc_size, empty_size, hint_byte,
3389                                      search_end, ins, data);
3390         BUG_ON(ret);
3391         if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
3392                 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
3393                                         root_objectid, ref_generation,
3394                                         owner_objectid, ins);
3395                 BUG_ON(ret);
3396
3397         } else {
3398                 update_reserved_extents(root, ins->objectid, ins->offset, 1);
3399         }
3400         return ret;
3401 }
3402
3403 struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
3404                                             struct btrfs_root *root,
3405                                             u64 bytenr, u32 blocksize)
3406 {
3407         struct extent_buffer *buf;
3408
3409         buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
3410         if (!buf)
3411                 return ERR_PTR(-ENOMEM);
3412         btrfs_set_header_generation(buf, trans->transid);
3413         btrfs_tree_lock(buf);
3414         clean_tree_block(trans, root, buf);
3415
3416         btrfs_set_lock_blocking(buf);
3417         btrfs_set_buffer_uptodate(buf);
3418
3419         if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
3420                 set_extent_dirty(&root->dirty_log_pages, buf->start,
3421                          buf->start + buf->len - 1, GFP_NOFS);
3422         } else {
3423                 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
3424                          buf->start + buf->len - 1, GFP_NOFS);
3425         }
3426         trans->blocks_used++;
3427         /* this returns a buffer locked for blocking */
3428         return buf;
3429 }
3430
3431 /*
3432  * helper function to allocate a block for a given tree
3433  * returns the tree buffer or NULL.
3434  */
3435 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
3436                                              struct btrfs_root *root,
3437                                              u32 blocksize, u64 parent,
3438                                              u64 root_objectid,
3439                                              u64 ref_generation,
3440                                              int level,
3441                                              u64 hint,
3442                                              u64 empty_size)
3443 {
3444         struct btrfs_key ins;
3445         int ret;
3446         struct extent_buffer *buf;
3447
3448         ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
3449                                  root_objectid, ref_generation, level,
3450                                  empty_size, hint, (u64)-1, &ins, 0);
3451         if (ret) {
3452                 BUG_ON(ret > 0);
3453                 return ERR_PTR(ret);
3454         }
3455
3456         buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
3457         return buf;
3458 }
3459
3460 int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
3461                         struct btrfs_root *root, struct extent_buffer *leaf)
3462 {
3463         u64 leaf_owner;
3464         u64 leaf_generation;
3465         struct refsort *sorted;
3466         struct btrfs_key key;
3467         struct btrfs_file_extent_item *fi;
3468         int i;
3469         int nritems;
3470         int ret;
3471         int refi = 0;
3472         int slot;
3473
3474         BUG_ON(!btrfs_is_leaf(leaf));
3475         nritems = btrfs_header_nritems(leaf);
3476         leaf_owner = btrfs_header_owner(leaf);
3477         leaf_generation = btrfs_header_generation(leaf);
3478
3479         sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
3480         /* we do this loop twice.  The first time we build a list
3481          * of the extents we have a reference on, then we sort the list
3482          * by bytenr.  The second time around we actually do the
3483          * extent freeing.
3484          */
3485         for (i = 0; i < nritems; i++) {
3486                 u64 disk_bytenr;
3487                 cond_resched();
3488
3489                 btrfs_item_key_to_cpu(leaf, &key, i);
3490
3491                 /* only extents have references, skip everything else */
3492                 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
3493                         continue;
3494
3495                 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
3496
3497                 /* inline extents live in the btree, they don't have refs */
3498                 if (btrfs_file_extent_type(leaf, fi) ==
3499                     BTRFS_FILE_EXTENT_INLINE)
3500                         continue;
3501
3502                 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
3503
3504                 /* holes don't have refs */
3505                 if (disk_bytenr == 0)
3506                         continue;
3507
3508                 sorted[refi].bytenr = disk_bytenr;
3509                 sorted[refi].slot = i;
3510                 refi++;
3511         }
3512
3513         if (refi == 0)
3514                 goto out;
3515
3516         sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
3517
3518         for (i = 0; i < refi; i++) {
3519                 u64 disk_bytenr;
3520
3521                 disk_bytenr = sorted[i].bytenr;
3522                 slot = sorted[i].slot;
3523
3524                 cond_resched();
3525
3526                 btrfs_item_key_to_cpu(leaf, &key, slot);
3527                 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
3528                         continue;
3529
3530                 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
3531
3532                 ret = __btrfs_free_extent(trans, root, disk_bytenr,
3533                                 btrfs_file_extent_disk_num_bytes(leaf, fi),
3534                                 leaf->start, leaf_owner, leaf_generation,
3535                                 key.objectid, 0);
3536                 BUG_ON(ret);
3537
3538                 atomic_inc(&root->fs_info->throttle_gen);
3539                 wake_up(&root->fs_info->transaction_throttle);
3540                 cond_resched();
3541         }
3542 out:
3543         kfree(sorted);
3544         return 0;
3545 }
3546
3547 static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
3548                                         struct btrfs_root *root,
3549                                         struct btrfs_leaf_ref *ref)
3550 {
3551         int i;
3552         int ret;
3553         struct btrfs_extent_info *info;
3554         struct refsort *sorted;
3555
3556         if (ref->nritems == 0)
3557                 return 0;
3558
3559         sorted = kmalloc(sizeof(*sorted) * ref->nritems, GFP_NOFS);
3560         for (i = 0; i < ref->nritems; i++) {
3561                 sorted[i].bytenr = ref->extents[i].bytenr;
3562                 sorted[i].slot = i;
3563         }
3564         sort(sorted, ref->nritems, sizeof(struct refsort), refsort_cmp, NULL);
3565
3566         /*
3567          * the items in the ref were sorted when the ref was inserted
3568          * into the ref cache, so this is already in order
3569          */
3570         for (i = 0; i < ref->nritems; i++) {
3571                 info = ref->extents + sorted[i].slot;
3572                 ret = __btrfs_free_extent(trans, root, info->bytenr,
3573                                           info->num_bytes, ref->bytenr,
3574                                           ref->owner, ref->generation,
3575                                           info->objectid, 0);
3576
3577                 atomic_inc(&root->fs_info->throttle_gen);
3578                 wake_up(&root->fs_info->transaction_throttle);
3579                 cond_resched();
3580
3581                 BUG_ON(ret);
3582                 info++;
3583         }
3584
3585         return 0;
3586 }
3587
3588 static int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start,
3589                                      u64 len, u32 *refs)
3590 {
3591         int ret;
3592
3593         ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
3594         BUG_ON(ret);
3595
3596 #if 0 /* some debugging code in case we see problems here */
3597         /* if the refs count is one, it won't get increased again.  But
3598          * if the ref count is > 1, someone may be decreasing it at
3599          * the same time we are.
3600          */
3601         if (*refs != 1) {
3602                 struct extent_buffer *eb = NULL;
3603                 eb = btrfs_find_create_tree_block(root, start, len);
3604                 if (eb)
3605                         btrfs_tree_lock(eb);
3606
3607                 mutex_lock(&root->fs_info->alloc_mutex);
3608                 ret = lookup_extent_ref(NULL, root, start, len, refs);
3609                 BUG_ON(ret);
3610                 mutex_unlock(&root->fs_info->alloc_mutex);
3611
3612                 if (eb) {
3613                         btrfs_tree_unlock(eb);
3614                         free_extent_buffer(eb);
3615                 }
3616                 if (*refs == 1) {
3617                         printk(KERN_ERR "btrfs block %llu went down to one "
3618                                "during drop_snap\n", (unsigned long long)start);
3619                 }
3620
3621         }
3622 #endif
3623
3624         cond_resched();
3625         return ret;
3626 }
3627
3628 /*
3629  * this is used while deleting old snapshots, and it drops the refs
3630  * on a whole subtree starting from a level 1 node.
3631  *
3632  * The idea is to sort all the leaf pointers, and then drop the
3633  * ref on all the leaves in order.  Most of the time the leaves
3634  * will have ref cache entries, so no leaf IOs will be required to
3635  * find the extents they have references on.
3636  *
3637  * For each leaf, any references it has are also dropped in order
3638  *
3639  * This ends up dropping the references in something close to optimal
3640  * order for reading and modifying the extent allocation tree.
3641  */
3642 static noinline int drop_level_one_refs(struct btrfs_trans_handle *trans,
3643                                         struct btrfs_root *root,
3644                                         struct btrfs_path *path)
3645 {
3646         u64 bytenr;
3647         u64 root_owner;
3648         u64 root_gen;
3649         struct extent_buffer *eb = path->nodes[1];
3650         struct extent_buffer *leaf;
3651         struct btrfs_leaf_ref *ref;
3652         struct refsort *sorted = NULL;
3653         int nritems = btrfs_header_nritems(eb);
3654         int ret;
3655         int i;
3656         int refi = 0;
3657         int slot = path->slots[1];
3658         u32 blocksize = btrfs_level_size(root, 0);
3659         u32 refs;
3660
3661         if (nritems == 0)
3662                 goto out;
3663
3664         root_owner = btrfs_header_owner(eb);
3665         root_gen = btrfs_header_generation(eb);
3666         sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
3667
3668         /*
3669          * step one, sort all the leaf pointers so we don't scribble
3670          * randomly into the extent allocation tree
3671          */
3672         for (i = slot; i < nritems; i++) {
3673                 sorted[refi].bytenr = btrfs_node_blockptr(eb, i);
3674                 sorted[refi].slot = i;
3675                 refi++;
3676         }
3677
3678         /*
3679          * nritems won't be zero, but if we're picking up drop_snapshot
3680          * after a crash, slot might be > 0, so double check things
3681          * just in case.
3682          */
3683         if (refi == 0)
3684                 goto out;
3685
3686         sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
3687
3688         /*
3689          * the first loop frees everything the leaves point to
3690          */
3691         for (i = 0; i < refi; i++) {
3692                 u64 ptr_gen;
3693
3694                 bytenr = sorted[i].bytenr;
3695
3696                 /*
3697                  * check the reference count on this leaf.  If it is > 1
3698                  * we just decrement it below and don't update any
3699                  * of the refs the leaf points to.
3700                  */
3701                 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
3702                 BUG_ON(ret);
3703                 if (refs != 1)
3704                         continue;
3705
3706                 ptr_gen = btrfs_node_ptr_generation(eb, sorted[i].slot);
3707
3708                 /*
3709                  * the leaf only had one reference, which means the
3710                  * only thing pointing to this leaf is the snapshot
3711                  * we're deleting.  It isn't possible for the reference
3712                  * count to increase again later
3713                  *
3714                  * The reference cache is checked for the leaf,
3715                  * and if found we'll be able to drop any refs held by
3716                  * the leaf without needing to read it in.
3717                  */
3718                 ref = btrfs_lookup_leaf_ref(root, bytenr);
3719                 if (ref && ref->generation != ptr_gen) {
3720                         btrfs_free_leaf_ref(root, ref);
3721                         ref = NULL;
3722                 }
3723                 if (ref) {
3724                         ret = cache_drop_leaf_ref(trans, root, ref);
3725                         BUG_ON(ret);
3726                         btrfs_remove_leaf_ref(root, ref);
3727                         btrfs_free_leaf_ref(root, ref);
3728                 } else {
3729                         /*
3730                          * the leaf wasn't in the reference cache, so
3731                          * we have to read it.
3732                          */
3733                         leaf = read_tree_block(root, bytenr, blocksize,
3734                                                ptr_gen);
3735                         ret = btrfs_drop_leaf_ref(trans, root, leaf);
3736                         BUG_ON(ret);
3737                         free_extent_buffer(leaf);
3738                 }
3739                 atomic_inc(&root->fs_info->throttle_gen);
3740                 wake_up(&root->fs_info->transaction_throttle);
3741                 cond_resched();
3742         }
3743
3744         /*
3745          * run through the loop again to free the refs on the leaves.
3746          * This is faster than doing it in the loop above because
3747          * the leaves are likely to be clustered together.  We end up
3748          * working in nice chunks on the extent allocation tree.
3749          */
3750         for (i = 0; i < refi; i++) {
3751                 bytenr = sorted[i].bytenr;
3752                 ret = __btrfs_free_extent(trans, root, bytenr,
3753                                         blocksize, eb->start,
3754                                         root_owner, root_gen, 0, 1);
3755                 BUG_ON(ret);
3756
3757                 atomic_inc(&root->fs_info->throttle_gen);
3758                 wake_up(&root->fs_info->transaction_throttle);
3759                 cond_resched();
3760         }
3761 out:
3762         kfree(sorted);
3763
3764         /*
3765          * update the path to show we've processed the entire level 1
3766          * node.  This will get saved into the root's drop_snapshot_progress
3767          * field so these drops are not repeated again if this transaction
3768          * commits.
3769          */
3770         path->slots[1] = nritems;
3771         return 0;
3772 }
3773
3774 /*
3775  * helper function for drop_snapshot, this walks down the tree dropping ref
3776  * counts as it goes.
3777  */
3778 static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
3779                                    struct btrfs_root *root,
3780                                    struct btrfs_path *path, int *level)
3781 {
3782         u64 root_owner;
3783         u64 root_gen;
3784         u64 bytenr;
3785         u64 ptr_gen;
3786         struct extent_buffer *next;
3787         struct extent_buffer *cur;
3788         struct extent_buffer *parent;
3789         u32 blocksize;
3790         int ret;
3791         u32 refs;
3792
3793         WARN_ON(*level < 0);
3794         WARN_ON(*level >= BTRFS_MAX_LEVEL);
3795         ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
3796                                 path->nodes[*level]->len, &refs);
3797         BUG_ON(ret);
3798         if (refs > 1)
3799                 goto out;
3800
3801         /*
3802          * walk down to the last node level and free all the leaves
3803          */
3804         while (*level >= 0) {
3805                 WARN_ON(*level < 0);
3806                 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3807                 cur = path->nodes[*level];
3808
3809                 if (btrfs_header_level(cur) != *level)
3810                         WARN_ON(1);
3811
3812                 if (path->slots[*level] >=
3813                     btrfs_header_nritems(cur))
3814                         break;
3815
3816                 /* the new code goes down to level 1 and does all the
3817                  * leaves pointed to that node in bulk.  So, this check
3818                  * for level 0 will always be false.
3819                  *
3820                  * But, the disk format allows the drop_snapshot_progress
3821                  * field in the root to leave things in a state where
3822                  * a leaf will need cleaning up here.  If someone crashes
3823                  * with the old code and then boots with the new code,
3824                  * we might find a leaf here.
3825                  */
3826                 if (*level == 0) {
3827                         ret = btrfs_drop_leaf_ref(trans, root, cur);
3828                         BUG_ON(ret);
3829                         break;
3830                 }
3831
3832                 /*
3833                  * once we get to level one, process the whole node
3834                  * at once, including everything below it.
3835                  */
3836                 if (*level == 1) {
3837                         ret = drop_level_one_refs(trans, root, path);
3838                         BUG_ON(ret);
3839                         break;
3840                 }
3841
3842                 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3843                 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3844                 blocksize = btrfs_level_size(root, *level - 1);
3845
3846                 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
3847                 BUG_ON(ret);
3848
3849                 /*
3850                  * if there is more than one reference, we don't need
3851                  * to read that node to drop any references it has.  We
3852                  * just drop the ref we hold on that node and move on to the
3853                  * next slot in this level.
3854                  */
3855                 if (refs != 1) {
3856                         parent = path->nodes[*level];
3857                         root_owner = btrfs_header_owner(parent);
3858                         root_gen = btrfs_header_generation(parent);
3859                         path->slots[*level]++;
3860
3861                         ret = __btrfs_free_extent(trans, root, bytenr,
3862                                                 blocksize, parent->start,
3863                                                 root_owner, root_gen,
3864                                                 *level - 1, 1);
3865                         BUG_ON(ret);
3866
3867                         atomic_inc(&root->fs_info->throttle_gen);
3868                         wake_up(&root->fs_info->transaction_throttle);
3869                         cond_resched();
3870
3871                         continue;
3872                 }
3873
3874                 /*
3875                  * we need to keep freeing things in the next level down.
3876                  * read the block and loop around to process it
3877                  */
3878                 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
3879                 WARN_ON(*level <= 0);
3880                 if (path->nodes[*level-1])
3881                         free_extent_buffer(path->nodes[*level-1]);
3882                 path->nodes[*level-1] = next;
3883                 *level = btrfs_header_level(next);
3884                 path->slots[*level] = 0;
3885                 cond_resched();
3886         }
3887 out:
3888         WARN_ON(*level < 0);
3889         WARN_ON(*level >= BTRFS_MAX_LEVEL);
3890
3891         if (path->nodes[*level] == root->node) {
3892                 parent = path->nodes[*level];
3893                 bytenr = path->nodes[*level]->start;
3894         } else {
3895                 parent = path->nodes[*level + 1];
3896                 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
3897         }
3898
3899         blocksize = btrfs_level_size(root, *level);
3900         root_owner = btrfs_header_owner(parent);
3901         root_gen = btrfs_header_generation(parent);
3902
3903         /*
3904          * cleanup and free the reference on the last node
3905          * we processed
3906          */
3907         ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
3908                                   parent->start, root_owner, root_gen,
3909                                   *level, 1);
3910         free_extent_buffer(path->nodes[*level]);
3911         path->nodes[*level] = NULL;
3912
3913         *level += 1;
3914         BUG_ON(ret);
3915
3916         cond_resched();
3917         return 0;
3918 }
3919
3920 /*
3921  * helper function for drop_subtree, this function is similar to
3922  * walk_down_tree. The main difference is that it checks reference
3923  * counts while tree blocks are locked.
3924  */
3925 static noinline int walk_down_subtree(struct btrfs_trans_handle *trans,
3926                                       struct btrfs_root *root,
3927                                       struct btrfs_path *path, int *level)
3928 {
3929         struct extent_buffer *next;
3930         struct extent_buffer *cur;
3931         struct extent_buffer *parent;
3932         u64 bytenr;
3933         u64 ptr_gen;
3934         u32 blocksize;
3935         u32 refs;
3936         int ret;
3937
3938         cur = path->nodes[*level];
3939         ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
3940                                       &refs);
3941         BUG_ON(ret);
3942         if (refs > 1)
3943                 goto out;
3944
3945         while (*level >= 0) {
3946                 cur = path->nodes[*level];
3947                 if (*level == 0) {
3948                         ret = btrfs_drop_leaf_ref(trans, root, cur);
3949                         BUG_ON(ret);
3950                         clean_tree_block(trans, root, cur);
3951                         break;
3952                 }
3953                 if (path->slots[*level] >= btrfs_header_nritems(cur)) {
3954                         clean_tree_block(trans, root, cur);
3955                         break;
3956                 }
3957
3958                 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3959                 blocksize = btrfs_level_size(root, *level - 1);
3960                 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3961
3962                 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
3963                 btrfs_tree_lock(next);
3964                 btrfs_set_lock_blocking(next);
3965
3966                 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
3967                                               &refs);
3968                 BUG_ON(ret);
3969                 if (refs > 1) {
3970                         parent = path->nodes[*level];
3971                         ret = btrfs_free_extent(trans, root, bytenr,
3972                                         blocksize, parent->start,
3973                                         btrfs_header_owner(parent),
3974                                         btrfs_header_generation(parent),
3975                                         *level - 1, 1);
3976                         BUG_ON(ret);
3977                         path->slots[*level]++;
3978                         btrfs_tree_unlock(next);
3979                         free_extent_buffer(next);
3980                         continue;
3981                 }
3982
3983                 *level = btrfs_header_level(next);
3984                 path->nodes[*level] = next;
3985                 path->slots[*level] = 0;
3986                 path->locks[*level] = 1;
3987                 cond_resched();
3988         }
3989 out:
3990         parent = path->nodes[*level + 1];
3991         bytenr = path->nodes[*level]->start;
3992         blocksize = path->nodes[*level]->len;
3993
3994         ret = btrfs_free_extent(trans, root, bytenr, blocksize,
3995                         parent->start, btrfs_header_owner(parent),
3996                         btrfs_header_generation(parent), *level, 1);
3997         BUG_ON(ret);
3998
3999         if (path->locks[*level]) {
4000                 btrfs_tree_unlock(path->nodes[*level]);
4001                 path->locks[*level] = 0;
4002         }
4003         free_extent_buffer(path->nodes[*level]);
4004         path->nodes[*level] = NULL;
4005         *level += 1;
4006         cond_resched();
4007         return 0;
4008 }
4009
4010 /*
4011  * helper for dropping snapshots.  This walks back up the tree in the path
4012  * to find the first node higher up where we haven't yet gone through
4013  * all the slots
4014  */
4015 static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
4016                                  struct btrfs_root *root,
4017                                  struct btrfs_path *path,
4018                                  int *level, int max_level)
4019 {
4020         u64 root_owner;
4021         u64 root_gen;
4022         struct btrfs_root_item *root_item = &root->root_item;
4023         int i;
4024         int slot;
4025         int ret;
4026
4027         for (i = *level; i < max_level && path->nodes[i]; i++) {
4028                 slot = path->slots[i];
4029                 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
4030                         struct extent_buffer *node;
4031                         struct btrfs_disk_key disk_key;
4032
4033                         /*
4034                          * there is more work to do in this level.
4035                          * Update the drop_progress marker to reflect
4036                          * the work we've done so far, and then bump
4037                          * the slot number
4038                          */
4039                         node = path->nodes[i];
4040                         path->slots[i]++;
4041                         *level = i;
4042                         WARN_ON(*level == 0);
4043                         btrfs_node_key(node, &disk_key, path->slots[i]);
4044                         memcpy(&root_item->drop_progress,
4045                                &disk_key, sizeof(disk_key));
4046                         root_item->drop_level = i;
4047                         return 0;
4048                 } else {
4049                         struct extent_buffer *parent;
4050
4051                         /*
4052                          * this whole node is done, free our reference
4053                          * on it and go up one level
4054                          */
4055                         if (path->nodes[*level] == root->node)
4056                                 parent = path->nodes[*level];
4057                         else
4058                                 parent = path->nodes[*level + 1];
4059
4060                         root_owner = btrfs_header_owner(parent);
4061                         root_gen = btrfs_header_generation(parent);
4062
4063                         clean_tree_block(trans, root, path->nodes[*level]);
4064                         ret = btrfs_free_extent(trans, root,
4065                                                 path->nodes[*level]->start,
4066                                                 path->nodes[*level]->len,
4067                                                 parent->start, root_owner,
4068                                                 root_gen, *level, 1);
4069                         BUG_ON(ret);
4070                         if (path->locks[*level]) {
4071                                 btrfs_tree_unlock(path->nodes[*level]);
4072                                 path->locks[*level] = 0;
4073                         }
4074                         free_extent_buffer(path->nodes[*level]);
4075                         path->nodes[*level] = NULL;
4076                         *level = i + 1;
4077                 }
4078         }
4079         return 1;
4080 }
4081
4082 /*
4083  * drop the reference count on the tree rooted at 'snap'.  This traverses
4084  * the tree freeing any blocks that have a ref count of zero after being
4085  * decremented.
4086  */
4087 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
4088                         *root)
4089 {
4090         int ret = 0;
4091         int wret;
4092         int level;
4093         struct btrfs_path *path;
4094         int i;
4095         int orig_level;
4096         struct btrfs_root_item *root_item = &root->root_item;
4097
4098         WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
4099         path = btrfs_alloc_path();
4100         BUG_ON(!path);
4101
4102         level = btrfs_header_level(root->node);
4103         orig_level = level;
4104         if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
4105                 path->nodes[level] = root->node;
4106                 extent_buffer_get(root->node);
4107                 path->slots[level] = 0;
4108         } else {
4109                 struct btrfs_key key;
4110                 struct btrfs_disk_key found_key;
4111                 struct extent_buffer *node;
4112
4113                 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
4114                 level = root_item->drop_level;
4115                 path->lowest_level = level;
4116                 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4117                 if (wret < 0) {
4118                         ret = wret;
4119                         goto out;
4120                 }
4121                 node = path->nodes[level];
4122                 btrfs_node_key(node, &found_key, path->slots[level]);
4123                 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
4124                                sizeof(found_key)));
4125                 /*
4126                  * unlock our path, this is safe because only this
4127                  * function is allowed to delete this snapshot
4128                  */
4129                 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
4130                         if (path->nodes[i] && path->locks[i]) {
4131                                 path->locks[i] = 0;
4132                                 btrfs_tree_unlock(path->nodes[i]);
4133                         }
4134                 }
4135         }
4136         while (1) {
4137                 wret = walk_down_tree(trans, root, path, &level);
4138                 if (wret > 0)
4139                         break;
4140                 if (wret < 0)
4141                         ret = wret;
4142
4143                 wret = walk_up_tree(trans, root, path, &level,
4144                                     BTRFS_MAX_LEVEL);
4145                 if (wret > 0)
4146                         break;
4147                 if (wret < 0)
4148                         ret = wret;
4149                 if (trans->transaction->in_commit) {
4150                         ret = -EAGAIN;
4151                         break;
4152                 }
4153                 atomic_inc(&root->fs_info->throttle_gen);
4154                 wake_up(&root->fs_info->transaction_throttle);
4155         }
4156         for (i = 0; i <= orig_level; i++) {
4157                 if (path->nodes[i]) {
4158                         free_extent_buffer(path->nodes[i]);
4159                         path->nodes[i] = NULL;
4160                 }
4161         }
4162 out:
4163         btrfs_free_path(path);
4164         return ret;
4165 }
4166
4167 int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
4168                         struct btrfs_root *root,
4169                         struct extent_buffer *node,
4170                         struct extent_buffer *parent)
4171 {
4172         struct btrfs_path *path;
4173         int level;
4174         int parent_level;
4175         int ret = 0;
4176         int wret;
4177
4178         path = btrfs_alloc_path();
4179         BUG_ON(!path);
4180
4181         BUG_ON(!btrfs_tree_locked(parent));
4182         parent_level = btrfs_header_level(parent);
4183         extent_buffer_get(parent);
4184         path->nodes[parent_level] = parent;
4185         path->slots[parent_level] = btrfs_header_nritems(parent);
4186
4187         BUG_ON(!btrfs_tree_locked(node));
4188         level = btrfs_header_level(node);
4189         extent_buffer_get(node);
4190         path->nodes[level] = node;
4191         path->slots[level] = 0;
4192
4193         while (1) {
4194                 wret = walk_down_subtree(trans, root, path, &level);
4195                 if (wret < 0)
4196                         ret = wret;
4197                 if (wret != 0)
4198                         break;
4199
4200                 wret = walk_up_tree(trans, root, path, &level, parent_level);
4201                 if (wret < 0)
4202                         ret = wret;
4203                 if (wret != 0)
4204                         break;
4205         }
4206
4207         btrfs_free_path(path);
4208         return ret;
4209 }
4210
4211 static unsigned long calc_ra(unsigned long start, unsigned long last,
4212                              unsigned long nr)
4213 {
4214         return min(last, start + nr - 1);
4215 }
4216
4217 static noinline int relocate_inode_pages(struct inode *inode, u64 start,
4218                                          u64 len)
4219 {
4220         u64 page_start;
4221         u64 page_end;
4222         unsigned long first_index;
4223         unsigned long last_index;
4224         unsigned long i;
4225         struct page *page;
4226         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4227         struct file_ra_state *ra;
4228         struct btrfs_ordered_extent *ordered;
4229         unsigned int total_read = 0;
4230         unsigned int total_dirty = 0;
4231         int ret = 0;
4232
4233         ra = kzalloc(sizeof(*ra), GFP_NOFS);
4234
4235         mutex_lock(&inode->i_mutex);
4236         first_index = start >> PAGE_CACHE_SHIFT;
4237         last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
4238
4239         /* make sure the dirty trick played by the caller work */
4240         ret = invalidate_inode_pages2_range(inode->i_mapping,
4241                                             first_index, last_index);
4242         if (ret)
4243                 goto out_unlock;
4244
4245         file_ra_state_init(ra, inode->i_mapping);
4246
4247         for (i = first_index ; i <= last_index; i++) {
4248                 if (total_read % ra->ra_pages == 0) {
4249                         btrfs_force_ra(inode->i_mapping, ra, NULL, i,
4250                                        calc_ra(i, last_index, ra->ra_pages));
4251                 }
4252                 total_read++;
4253 again:
4254                 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
4255                         BUG_ON(1);
4256                 page = grab_cache_page(inode->i_mapping, i);
4257                 if (!page) {
4258                         ret = -ENOMEM;
4259                         goto out_unlock;
4260                 }
4261                 if (!PageUptodate(page)) {
4262                         btrfs_readpage(NULL, page);
4263                         lock_page(page);
4264                         if (!PageUptodate(page)) {
4265                                 unlock_page(page);
4266                                 page_cache_release(page);
4267                                 ret = -EIO;
4268                                 goto out_unlock;
4269                         }
4270                 }
4271                 wait_on_page_writeback(page);
4272
4273                 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
4274                 page_end = page_start + PAGE_CACHE_SIZE - 1;
4275                 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
4276
4277                 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4278                 if (ordered) {
4279                         unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4280                         unlock_page(page);
4281                         page_cache_release(page);
4282                         btrfs_start_ordered_extent(inode, ordered, 1);
4283                         btrfs_put_ordered_extent(ordered);
4284                         goto again;
4285                 }
4286                 set_page_extent_mapped(page);
4287
4288                 if (i == first_index)
4289                         set_extent_bits(io_tree, page_start, page_end,
4290                                         EXTENT_BOUNDARY, GFP_NOFS);
4291                 btrfs_set_extent_delalloc(inode, page_start, page_end);
4292
4293                 set_page_dirty(page);
4294                 total_dirty++;
4295
4296                 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4297                 unlock_page(page);
4298                 page_cache_release(page);
4299         }
4300
4301 out_unlock:
4302         kfree(ra);
4303         mutex_unlock(&inode->i_mutex);
4304         balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
4305         return ret;
4306 }
4307
4308 static noinline int relocate_data_extent(struct inode *reloc_inode,
4309                                          struct btrfs_key *extent_key,
4310                                          u64 offset)
4311 {
4312         struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4313         struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
4314         struct extent_map *em;
4315         u64 start = extent_key->objectid - offset;
4316         u64 end = start + extent_key->offset - 1;
4317
4318         em = alloc_extent_map(GFP_NOFS);
4319         BUG_ON(!em || IS_ERR(em));
4320
4321         em->start = start;
4322         em->len = extent_key->offset;
4323         em->block_len = extent_key->offset;
4324         em->block_start = extent_key->objectid;
4325         em->bdev = root->fs_info->fs_devices->latest_bdev;
4326         set_bit(EXTENT_FLAG_PINNED, &em->flags);
4327
4328         /* setup extent map to cheat btrfs_readpage */
4329         lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
4330         while (1) {
4331                 int ret;
4332                 spin_lock(&em_tree->lock);
4333                 ret = add_extent_mapping(em_tree, em);
4334                 spin_unlock(&em_tree->lock);
4335                 if (ret != -EEXIST) {
4336                         free_extent_map(em);
4337                         break;
4338                 }
4339                 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
4340         }
4341         unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
4342
4343         return relocate_inode_pages(reloc_inode, start, extent_key->offset);
4344 }
4345
4346 struct btrfs_ref_path {
4347         u64 extent_start;
4348         u64 nodes[BTRFS_MAX_LEVEL];
4349         u64 root_objectid;
4350         u64 root_generation;
4351         u64 owner_objectid;
4352         u32 num_refs;
4353         int lowest_level;
4354         int current_level;
4355         int shared_level;
4356
4357         struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
4358         u64 new_nodes[BTRFS_MAX_LEVEL];
4359 };
4360
4361 struct disk_extent {
4362         u64 ram_bytes;
4363         u64 disk_bytenr;
4364         u64 disk_num_bytes;
4365         u64 offset;
4366         u64 num_bytes;
4367         u8 compression;
4368         u8 encryption;
4369         u16 other_encoding;
4370 };
4371
4372 static int is_cowonly_root(u64 root_objectid)
4373 {
4374         if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
4375             root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
4376             root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
4377             root_objectid == BTRFS_DEV_TREE_OBJECTID ||
4378             root_objectid == BTRFS_TREE_LOG_OBJECTID ||
4379             root_objectid == BTRFS_CSUM_TREE_OBJECTID)
4380                 return 1;
4381         return 0;
4382 }
4383
4384 static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
4385                                     struct btrfs_root *extent_root,
4386                                     struct btrfs_ref_path *ref_path,
4387                                     int first_time)
4388 {
4389         struct extent_buffer *leaf;
4390         struct btrfs_path *path;
4391         struct btrfs_extent_ref *ref;
4392         struct btrfs_key key;
4393         struct btrfs_key found_key;
4394         u64 bytenr;
4395         u32 nritems;
4396         int level;
4397         int ret = 1;
4398
4399         path = btrfs_alloc_path();
4400         if (!path)
4401                 return -ENOMEM;
4402
4403         if (first_time) {
4404                 ref_path->lowest_level = -1;
4405                 ref_path->current_level = -1;
4406                 ref_path->shared_level = -1;
4407                 goto walk_up;
4408         }
4409 walk_down:
4410         level = ref_path->current_level - 1;
4411         while (level >= -1) {
4412                 u64 parent;
4413                 if (level < ref_path->lowest_level)
4414                         break;
4415
4416                 if (level >= 0)
4417                         bytenr = ref_path->nodes[level];
4418                 else
4419                         bytenr = ref_path->extent_start;
4420                 BUG_ON(bytenr == 0);
4421
4422                 parent = ref_path->nodes[level + 1];
4423                 ref_path->nodes[level + 1] = 0;
4424                 ref_path->current_level = level;
4425                 BUG_ON(parent == 0);
4426
4427                 key.objectid = bytenr;
4428                 key.offset = parent + 1;
4429                 key.type = BTRFS_EXTENT_REF_KEY;
4430
4431                 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4432                 if (ret < 0)
4433                         goto out;
4434                 BUG_ON(ret == 0);
4435
4436                 leaf = path->nodes[0];
4437                 nritems = btrfs_header_nritems(leaf);
4438                 if (path->slots[0] >= nritems) {
4439                         ret = btrfs_next_leaf(extent_root, path);
4440                         if (ret < 0)
4441                                 goto out;
4442                         if (ret > 0)
4443                                 goto next;
4444                         leaf = path->nodes[0];
4445                 }
4446
4447                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4448                 if (found_key.objectid == bytenr &&
4449                     found_key.type == BTRFS_EXTENT_REF_KEY) {
4450                         if (level < ref_path->shared_level)
4451                                 ref_path->shared_level = level;
4452                         goto found;
4453                 }
4454 next:
4455                 level--;
4456                 btrfs_release_path(extent_root, path);
4457                 cond_resched();
4458         }
4459         /* reached lowest level */
4460         ret = 1;
4461         goto out;
4462 walk_up:
4463         level = ref_path->current_level;
4464         while (level < BTRFS_MAX_LEVEL - 1) {
4465                 u64 ref_objectid;
4466
4467                 if (level >= 0)
4468                         bytenr = ref_path->nodes[level];
4469                 else
4470                         bytenr = ref_path->extent_start;
4471
4472                 BUG_ON(bytenr == 0);
4473
4474                 key.objectid = bytenr;
4475                 key.offset = 0;
4476                 key.type = BTRFS_EXTENT_REF_KEY;
4477
4478                 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4479                 if (ret < 0)
4480                         goto out;
4481
4482                 leaf = path->nodes[0];
4483                 nritems = btrfs_header_nritems(leaf);
4484                 if (path->slots[0] >= nritems) {
4485                         ret = btrfs_next_leaf(extent_root, path);
4486                         if (ret < 0)
4487                                 goto out;
4488                         if (ret > 0) {
4489                                 /* the extent was freed by someone */
4490                                 if (ref_path->lowest_level == level)
4491                                         goto out;
4492                                 btrfs_release_path(extent_root, path);
4493                                 goto walk_down;
4494                         }
4495                         leaf = path->nodes[0];
4496                 }
4497
4498                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4499                 if (found_key.objectid != bytenr ||
4500                                 found_key.type != BTRFS_EXTENT_REF_KEY) {
4501                         /* the extent was freed by someone */
4502                         if (ref_path->lowest_level == level) {
4503                                 ret = 1;
4504                                 goto out;
4505                         }
4506                         btrfs_release_path(extent_root, path);
4507                         goto walk_down;
4508                 }
4509 found:
4510                 ref = btrfs_item_ptr(leaf, path->slots[0],
4511                                 struct btrfs_extent_ref);
4512                 ref_objectid = btrfs_ref_objectid(leaf, ref);
4513                 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4514                         if (first_time) {
4515                                 level = (int)ref_objectid;
4516                                 BUG_ON(level >= BTRFS_MAX_LEVEL);
4517                                 ref_path->lowest_level = level;
4518                                 ref_path->current_level = level;
4519                                 ref_path->nodes[level] = bytenr;
4520                         } else {
4521                                 WARN_ON(ref_objectid != level);
4522                         }
4523                 } else {
4524                         WARN_ON(level != -1);
4525                 }
4526                 first_time = 0;
4527
4528                 if (ref_path->lowest_level == level) {
4529                         ref_path->owner_objectid = ref_objectid;
4530                         ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
4531                 }
4532
4533                 /*
4534                  * the block is tree root or the block isn't in reference
4535                  * counted tree.
4536                  */
4537                 if (found_key.objectid == found_key.offset ||
4538                     is_cowonly_root(btrfs_ref_root(leaf, ref))) {
4539                         ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4540                         ref_path->root_generation =
4541                                 btrfs_ref_generation(leaf, ref);
4542                         if (level < 0) {
4543                                 /* special reference from the tree log */
4544                                 ref_path->nodes[0] = found_key.offset;
4545                                 ref_path->current_level = 0;
4546                         }
4547                         ret = 0;
4548                         goto out;
4549                 }
4550
4551                 level++;
4552                 BUG_ON(ref_path->nodes[level] != 0);
4553                 ref_path->nodes[level] = found_key.offset;
4554                 ref_path->current_level = level;
4555
4556                 /*
4557                  * the reference was created in the running transaction,
4558                  * no need to continue walking up.
4559                  */
4560                 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
4561                         ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4562                         ref_path->root_generation =
4563                                 btrfs_ref_generation(leaf, ref);
4564                         ret = 0;
4565                         goto out;
4566                 }
4567
4568                 btrfs_release_path(extent_root, path);
4569                 cond_resched();
4570         }
4571         /* reached max tree level, but no tree root found. */
4572         BUG();
4573 out:
4574         btrfs_free_path(path);
4575         return ret;
4576 }
4577
4578 static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
4579                                 struct btrfs_root *extent_root,
4580                                 struct btrfs_ref_path *ref_path,
4581                                 u64 extent_start)
4582 {
4583         memset(ref_path, 0, sizeof(*ref_path));
4584         ref_path->extent_start = extent_start;
4585
4586         return __next_ref_path(trans, extent_root, ref_path, 1);
4587 }
4588
4589 static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
4590                                struct btrfs_root *extent_root,
4591                                struct btrfs_ref_path *ref_path)
4592 {
4593         return __next_ref_path(trans, extent_root, ref_path, 0);
4594 }
4595
4596 static noinline int get_new_locations(struct inode *reloc_inode,
4597                                       struct btrfs_key *extent_key,
4598                                       u64 offset, int no_fragment,
4599                                       struct disk_extent **extents,
4600                                       int *nr_extents)
4601 {
4602         struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4603         struct btrfs_path *path;
4604         struct btrfs_file_extent_item *fi;
4605         struct extent_buffer *leaf;
4606         struct disk_extent *exts = *extents;
4607         struct btrfs_key found_key;
4608         u64 cur_pos;
4609         u64 last_byte;
4610         u32 nritems;
4611         int nr = 0;
4612         int max = *nr_extents;
4613         int ret;
4614
4615         WARN_ON(!no_fragment && *extents);
4616         if (!exts) {
4617                 max = 1;
4618                 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
4619                 if (!exts)
4620                         return -ENOMEM;
4621         }
4622
4623         path = btrfs_alloc_path();
4624         BUG_ON(!path);
4625
4626         cur_pos = extent_key->objectid - offset;
4627         last_byte = extent_key->objectid + extent_key->offset;
4628         ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
4629                                        cur_pos, 0);
4630         if (ret < 0)
4631                 goto out;
4632         if (ret > 0) {
4633                 ret = -ENOENT;
4634                 goto out;
4635         }
4636
4637         while (1) {
4638                 leaf = path->nodes[0];
4639                 nritems = btrfs_header_nritems(leaf);
4640                 if (path->slots[0] >= nritems) {
4641                         ret = btrfs_next_leaf(root, path);
4642                         if (ret < 0)
4643                                 goto out;
4644                         if (ret > 0)
4645                                 break;
4646                         leaf = path->nodes[0];
4647                 }
4648
4649                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4650                 if (found_key.offset != cur_pos ||
4651                     found_key.type != BTRFS_EXTENT_DATA_KEY ||
4652                     found_key.objectid != reloc_inode->i_ino)
4653                         break;
4654
4655                 fi = btrfs_item_ptr(leaf, path->slots[0],
4656                                     struct btrfs_file_extent_item);
4657                 if (btrfs_file_extent_type(leaf, fi) !=
4658                     BTRFS_FILE_EXTENT_REG ||
4659                     btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4660                         break;
4661
4662                 if (nr == max) {
4663                         struct disk_extent *old = exts;
4664                         max *= 2;
4665                         exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
4666                         memcpy(exts, old, sizeof(*exts) * nr);
4667                         if (old != *extents)
4668                                 kfree(old);
4669                 }
4670
4671                 exts[nr].disk_bytenr =
4672                         btrfs_file_extent_disk_bytenr(leaf, fi);
4673                 exts[nr].disk_num_bytes =
4674                         btrfs_file_extent_disk_num_bytes(leaf, fi);
4675                 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
4676                 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4677                 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
4678                 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
4679                 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
4680                 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
4681                                                                            fi);
4682                 BUG_ON(exts[nr].offset > 0);
4683                 BUG_ON(exts[nr].compression || exts[nr].encryption);
4684                 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
4685
4686                 cur_pos += exts[nr].num_bytes;
4687                 nr++;
4688
4689                 if (cur_pos + offset >= last_byte)
4690                         break;
4691
4692                 if (no_fragment) {
4693                         ret = 1;
4694                         goto out;
4695                 }
4696                 path->slots[0]++;
4697         }
4698
4699         BUG_ON(cur_pos + offset > last_byte);
4700         if (cur_pos + offset < last_byte) {
4701                 ret = -ENOENT;
4702                 goto out;
4703         }
4704         ret = 0;
4705 out:
4706         btrfs_free_path(path);
4707         if (ret) {
4708                 if (exts != *extents)
4709                         kfree(exts);
4710         } else {
4711                 *extents = exts;
4712                 *nr_extents = nr;
4713         }
4714         return ret;
4715 }
4716
4717 static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
4718                                         struct btrfs_root *root,
4719                                         struct btrfs_path *path,
4720                                         struct btrfs_key *extent_key,
4721                                         struct btrfs_key *leaf_key,
4722                                         struct btrfs_ref_path *ref_path,
4723                                         struct disk_extent *new_extents,
4724                                         int nr_extents)
4725 {
4726         struct extent_buffer *leaf;
4727         struct btrfs_file_extent_item *fi;
4728         struct inode *inode = NULL;
4729         struct btrfs_key key;
4730         u64 lock_start = 0;
4731         u64 lock_end = 0;
4732         u64 num_bytes;
4733         u64 ext_offset;
4734         u64 search_end = (u64)-1;
4735         u32 nritems;
4736         int nr_scaned = 0;
4737         int extent_locked = 0;
4738         int extent_type;
4739         int ret;
4740
4741         memcpy(&key, leaf_key, sizeof(key));
4742         if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4743                 if (key.objectid < ref_path->owner_objectid ||
4744                     (key.objectid == ref_path->owner_objectid &&
4745                      key.type < BTRFS_EXTENT_DATA_KEY)) {
4746                         key.objectid = ref_path->owner_objectid;
4747                         key.type = BTRFS_EXTENT_DATA_KEY;
4748                         key.offset = 0;
4749                 }
4750         }
4751
4752         while (1) {
4753                 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4754                 if (ret < 0)
4755                         goto out;
4756
4757                 leaf = path->nodes[0];
4758                 nritems = btrfs_header_nritems(leaf);
4759 next:
4760                 if (extent_locked && ret > 0) {
4761                         /*
4762                          * the file extent item was modified by someone
4763                          * before the extent got locked.
4764                          */
4765                         unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4766                                       lock_end, GFP_NOFS);
4767                         extent_locked = 0;
4768                 }
4769
4770                 if (path->slots[0] >= nritems) {
4771                         if (++nr_scaned > 2)
4772                                 break;
4773
4774                         BUG_ON(extent_locked);
4775                         ret = btrfs_next_leaf(root, path);
4776                         if (ret < 0)
4777                                 goto out;
4778                         if (ret > 0)
4779                                 break;
4780                         leaf = path->nodes[0];
4781                         nritems = btrfs_header_nritems(leaf);
4782                 }
4783
4784                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4785
4786                 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4787                         if ((key.objectid > ref_path->owner_objectid) ||
4788                             (key.objectid == ref_path->owner_objectid &&
4789                              key.type > BTRFS_EXTENT_DATA_KEY) ||
4790                             key.offset >= search_end)
4791                                 break;
4792                 }
4793
4794                 if (inode && key.objectid != inode->i_ino) {
4795                         BUG_ON(extent_locked);
4796                         btrfs_release_path(root, path);
4797                         mutex_unlock(&inode->i_mutex);
4798                         iput(inode);
4799                         inode = NULL;
4800                         continue;
4801                 }
4802
4803                 if (key.type != BTRFS_EXTENT_DATA_KEY) {
4804                         path->slots[0]++;
4805                         ret = 1;
4806                         goto next;
4807                 }
4808                 fi = btrfs_item_ptr(leaf, path->slots[0],
4809                                     struct btrfs_file_extent_item);
4810                 extent_type = btrfs_file_extent_type(leaf, fi);
4811                 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
4812                      extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
4813                     (btrfs_file_extent_disk_bytenr(leaf, fi) !=
4814                      extent_key->objectid)) {
4815                         path->slots[0]++;
4816                         ret = 1;
4817                         goto next;
4818                 }
4819
4820                 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4821                 ext_offset = btrfs_file_extent_offset(leaf, fi);
4822
4823                 if (search_end == (u64)-1) {
4824                         search_end = key.offset - ext_offset +
4825                                 btrfs_file_extent_ram_bytes(leaf, fi);
4826                 }
4827
4828                 if (!extent_locked) {
4829                         lock_start = key.offset;
4830                         lock_end = lock_start + num_bytes - 1;
4831                 } else {
4832                         if (lock_start > key.offset ||
4833                             lock_end + 1 < key.offset + num_bytes) {
4834                                 unlock_extent(&BTRFS_I(inode)->io_tree,
4835                                               lock_start, lock_end, GFP_NOFS);
4836                                 extent_locked = 0;
4837                         }
4838                 }
4839
4840                 if (!inode) {
4841                         btrfs_release_path(root, path);
4842
4843                         inode = btrfs_iget_locked(root->fs_info->sb,
4844                                                   key.objectid, root);
4845                         if (inode->i_state & I_NEW) {
4846                                 BTRFS_I(inode)->root = root;
4847                                 BTRFS_I(inode)->location.objectid =
4848                                         key.objectid;
4849                                 BTRFS_I(inode)->location.type =
4850                                         BTRFS_INODE_ITEM_KEY;
4851                                 BTRFS_I(inode)->location.offset = 0;
4852                                 btrfs_read_locked_inode(inode);
4853                                 unlock_new_inode(inode);
4854                         }
4855                         /*
4856                          * some code call btrfs_commit_transaction while
4857                          * holding the i_mutex, so we can't use mutex_lock
4858                          * here.
4859                          */
4860                         if (is_bad_inode(inode) ||
4861                             !mutex_trylock(&inode->i_mutex)) {
4862                                 iput(inode);
4863                                 inode = NULL;
4864                                 key.offset = (u64)-1;
4865                                 goto skip;
4866                         }
4867                 }
4868
4869                 if (!extent_locked) {
4870                         struct btrfs_ordered_extent *ordered;
4871
4872                         btrfs_release_path(root, path);
4873
4874                         lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4875                                     lock_end, GFP_NOFS);
4876                         ordered = btrfs_lookup_first_ordered_extent(inode,
4877                                                                     lock_end);
4878                         if (ordered &&
4879                             ordered->file_offset <= lock_end &&
4880                             ordered->file_offset + ordered->len > lock_start) {
4881                                 unlock_extent(&BTRFS_I(inode)->io_tree,
4882                                               lock_start, lock_end, GFP_NOFS);
4883                                 btrfs_start_ordered_extent(inode, ordered, 1);
4884                                 btrfs_put_ordered_extent(ordered);
4885                                 key.offset += num_bytes;
4886                                 goto skip;
4887                         }
4888                         if (ordered)
4889                                 btrfs_put_ordered_extent(ordered);
4890
4891                         extent_locked = 1;
4892                         continue;
4893                 }
4894
4895                 if (nr_extents == 1) {
4896                         /* update extent pointer in place */
4897                         btrfs_set_file_extent_disk_bytenr(leaf, fi,
4898                                                 new_extents[0].disk_bytenr);
4899                         btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4900                                                 new_extents[0].disk_num_bytes);
4901                         btrfs_mark_buffer_dirty(leaf);
4902
4903                         btrfs_drop_extent_cache(inode, key.offset,
4904                                                 key.offset + num_bytes - 1, 0);
4905
4906                         ret = btrfs_inc_extent_ref(trans, root,
4907                                                 new_extents[0].disk_bytenr,
4908                                                 new_extents[0].disk_num_bytes,
4909                                                 leaf->start,
4910                                                 root->root_key.objectid,
4911                                                 trans->transid,
4912                                                 key.objectid);
4913                         BUG_ON(ret);
4914
4915                         ret = btrfs_free_extent(trans, root,
4916                                                 extent_key->objectid,
4917                                                 extent_key->offset,
4918                                                 leaf->start,
4919                                                 btrfs_header_owner(leaf),
4920                                                 btrfs_header_generation(leaf),
4921                                                 key.objectid, 0);
4922                         BUG_ON(ret);
4923
4924                         btrfs_release_path(root, path);
4925                         key.offset += num_bytes;
4926                 } else {
4927                         BUG_ON(1);
4928 #if 0
4929                         u64 alloc_hint;
4930                         u64 extent_len;
4931                         int i;
4932                         /*
4933                          * drop old extent pointer at first, then insert the
4934                          * new pointers one bye one
4935                          */
4936                         btrfs_release_path(root, path);
4937                         ret = btrfs_drop_extents(trans, root, inode, key.offset,
4938                                                  key.offset + num_bytes,
4939                                                  key.offset, &alloc_hint);
4940                         BUG_ON(ret);
4941
4942                         for (i = 0; i < nr_extents; i++) {
4943                                 if (ext_offset >= new_extents[i].num_bytes) {
4944                                         ext_offset -= new_extents[i].num_bytes;
4945                                         continue;
4946                                 }
4947                                 extent_len = min(new_extents[i].num_bytes -
4948                                                  ext_offset, num_bytes);
4949
4950                                 ret = btrfs_insert_empty_item(trans, root,
4951                                                               path, &key,
4952                                                               sizeof(*fi));
4953                                 BUG_ON(ret);
4954
4955                                 leaf = path->nodes[0];
4956                                 fi = btrfs_item_ptr(leaf, path->slots[0],
4957                                                 struct btrfs_file_extent_item);
4958                                 btrfs_set_file_extent_generation(leaf, fi,
4959                                                         trans->transid);
4960                                 btrfs_set_file_extent_type(leaf, fi,
4961                                                         BTRFS_FILE_EXTENT_REG);
4962                                 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4963                                                 new_extents[i].disk_bytenr);
4964                                 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4965                                                 new_extents[i].disk_num_bytes);
4966                                 btrfs_set_file_extent_ram_bytes(leaf, fi,
4967                                                 new_extents[i].ram_bytes);
4968
4969                                 btrfs_set_file_extent_compression(leaf, fi,
4970                                                 new_extents[i].compression);
4971                                 btrfs_set_file_extent_encryption(leaf, fi,
4972                                                 new_extents[i].encryption);
4973                                 btrfs_set_file_extent_other_encoding(leaf, fi,
4974                                                 new_extents[i].other_encoding);
4975
4976                                 btrfs_set_file_extent_num_bytes(leaf, fi,
4977                                                         extent_len);
4978                                 ext_offset += new_extents[i].offset;
4979                                 btrfs_set_file_extent_offset(leaf, fi,
4980                                                         ext_offset);
4981                                 btrfs_mark_buffer_dirty(leaf);
4982
4983                                 btrfs_drop_extent_cache(inode, key.offset,
4984                                                 key.offset + extent_len - 1, 0);
4985
4986                                 ret = btrfs_inc_extent_ref(trans, root,
4987                                                 new_extents[i].disk_bytenr,
4988                                                 new_extents[i].disk_num_bytes,
4989                                                 leaf->start,
4990                                                 root->root_key.objectid,
4991                                                 trans->transid, key.objectid);
4992                                 BUG_ON(ret);
4993                                 btrfs_release_path(root, path);
4994
4995                                 inode_add_bytes(inode, extent_len);
4996
4997                                 ext_offset = 0;
4998                                 num_bytes -= extent_len;
4999                                 key.offset += extent_len;
5000
5001                                 if (num_bytes == 0)
5002                                         break;
5003                         }
5004                         BUG_ON(i >= nr_extents);
5005 #endif
5006                 }
5007
5008                 if (extent_locked) {
5009                         unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
5010                                       lock_end, GFP_NOFS);
5011                         extent_locked = 0;
5012                 }
5013 skip:
5014                 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
5015                     key.offset >= search_end)
5016                         break;
5017
5018                 cond_resched();
5019         }
5020         ret = 0;
5021 out:
5022         btrfs_release_path(root, path);
5023         if (inode) {
5024                 mutex_unlock(&inode->i_mutex);
5025                 if (extent_locked) {
5026                         unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
5027                                       lock_end, GFP_NOFS);
5028                 }
5029                 iput(inode);
5030         }
5031         return ret;
5032 }
5033
5034 int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
5035                                struct btrfs_root *root,
5036                                struct extent_buffer *buf, u64 orig_start)
5037 {
5038         int level;
5039         int ret;
5040
5041         BUG_ON(btrfs_header_generation(buf) != trans->transid);
5042         BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
5043
5044         level = btrfs_header_level(buf);
5045         if (level == 0) {
5046                 struct btrfs_leaf_ref *ref;
5047                 struct btrfs_leaf_ref *orig_ref;
5048
5049                 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
5050                 if (!orig_ref)
5051                         return -ENOENT;
5052
5053                 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
5054                 if (!ref) {
5055                         btrfs_free_leaf_ref(root, orig_ref);
5056                         return -ENOMEM;
5057                 }
5058
5059                 ref->nritems = orig_ref->nritems;
5060                 memcpy(ref->extents, orig_ref->extents,
5061                         sizeof(ref->extents[0]) * ref->nritems);
5062
5063                 btrfs_free_leaf_ref(root, orig_ref);
5064
5065                 ref->root_gen = trans->transid;
5066                 ref->bytenr = buf->start;
5067                 ref->owner = btrfs_header_owner(buf);
5068                 ref->generation = btrfs_header_generation(buf);
5069
5070                 ret = btrfs_add_leaf_ref(root, ref, 0);
5071                 WARN_ON(ret);
5072                 btrfs_free_leaf_ref(root, ref);
5073         }
5074         return 0;
5075 }
5076
5077 static noinline int invalidate_extent_cache(struct btrfs_root *root,
5078                                         struct extent_buffer *leaf,
5079                                         struct btrfs_block_group_cache *group,
5080                                         struct btrfs_root *target_root)
5081 {
5082         struct btrfs_key key;
5083         struct inode *inode = NULL;
5084         struct btrfs_file_extent_item *fi;
5085         u64 num_bytes;
5086         u64 skip_objectid = 0;
5087         u32 nritems;
5088         u32 i;
5089
5090         nritems = btrfs_header_nritems(leaf);
5091         for (i = 0; i < nritems; i++) {
5092                 btrfs_item_key_to_cpu(leaf, &key, i);
5093                 if (key.objectid == skip_objectid ||
5094                     key.type != BTRFS_EXTENT_DATA_KEY)
5095                         continue;
5096                 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
5097                 if (btrfs_file_extent_type(leaf, fi) ==
5098                     BTRFS_FILE_EXTENT_INLINE)
5099                         continue;
5100                 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
5101                         continue;
5102                 if (!inode || inode->i_ino != key.objectid) {
5103                         iput(inode);
5104                         inode = btrfs_ilookup(target_root->fs_info->sb,
5105                                               key.objectid, target_root, 1);
5106                 }
5107                 if (!inode) {
5108                         skip_objectid = key.objectid;
5109                         continue;
5110                 }
5111                 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
5112
5113                 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
5114                             key.offset + num_bytes - 1, GFP_NOFS);
5115                 btrfs_drop_extent_cache(inode, key.offset,
5116                                         key.offset + num_bytes - 1, 1);
5117                 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
5118                               key.offset + num_bytes - 1, GFP_NOFS);
5119                 cond_resched();
5120         }
5121         iput(inode);
5122         return 0;
5123 }
5124
5125 static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
5126                                         struct btrfs_root *root,
5127                                         struct extent_buffer *leaf,
5128                                         struct btrfs_block_group_cache *group,
5129                                         struct inode *reloc_inode)
5130 {
5131         struct btrfs_key key;
5132         struct btrfs_key extent_key;
5133         struct btrfs_file_extent_item *fi;
5134         struct btrfs_leaf_ref *ref;
5135         struct disk_extent *new_extent;
5136         u64 bytenr;
5137         u64 num_bytes;
5138         u32 nritems;
5139         u32 i;
5140         int ext_index;
5141         int nr_extent;
5142         int ret;
5143
5144         new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
5145         BUG_ON(!new_extent);
5146
5147         ref = btrfs_lookup_leaf_ref(root, leaf->start);
5148         BUG_ON(!ref);
5149
5150         ext_index = -1;
5151         nritems = btrfs_header_nritems(leaf);
5152         for (i = 0; i < nritems; i++) {
5153                 btrfs_item_key_to_cpu(leaf, &key, i);
5154                 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
5155                         continue;
5156                 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
5157                 if (btrfs_file_extent_type(leaf, fi) ==
5158                     BTRFS_FILE_EXTENT_INLINE)
5159                         continue;
5160                 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5161                 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
5162                 if (bytenr == 0)
5163                         continue;
5164
5165                 ext_index++;
5166                 if (bytenr >= group->key.objectid + group->key.offset ||
5167                     bytenr + num_bytes <= group->key.objectid)
5168                         continue;
5169
5170                 extent_key.objectid = bytenr;
5171                 extent_key.offset = num_bytes;
5172                 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
5173                 nr_extent = 1;
5174                 ret = get_new_locations(reloc_inode, &extent_key,
5175                                         group->key.objectid, 1,
5176                                         &new_extent, &nr_extent);
5177                 if (ret > 0)
5178                         continue;
5179                 BUG_ON(ret < 0);
5180
5181                 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
5182                 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
5183                 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
5184                 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
5185
5186                 btrfs_set_file_extent_disk_bytenr(leaf, fi,
5187                                                 new_extent->disk_bytenr);
5188                 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
5189                                                 new_extent->disk_num_bytes);
5190                 btrfs_mark_buffer_dirty(leaf);
5191
5192                 ret = btrfs_inc_extent_ref(trans, root,
5193                                         new_extent->disk_bytenr,
5194                                         new_extent->disk_num_bytes,
5195                                         leaf->start,
5196                                         root->root_key.objectid,
5197                                         trans->transid, key.objectid);
5198                 BUG_ON(ret);
5199                 ret = btrfs_free_extent(trans, root,
5200                                         bytenr, num_bytes, leaf->start,
5201                                         btrfs_header_owner(leaf),
5202                                         btrfs_header_generation(leaf),
5203                                         key.objectid, 0);
5204                 BUG_ON(ret);
5205                 cond_resched();
5206         }
5207         kfree(new_extent);
5208         BUG_ON(ext_index + 1 != ref->nritems);
5209         btrfs_free_leaf_ref(root, ref);
5210         return 0;
5211 }
5212
5213 int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
5214                           struct btrfs_root *root)
5215 {
5216         struct btrfs_root *reloc_root;
5217         int ret;
5218
5219         if (root->reloc_root) {
5220                 reloc_root = root->reloc_root;
5221                 root->reloc_root = NULL;
5222                 list_add(&reloc_root->dead_list,
5223                          &root->fs_info->dead_reloc_roots);
5224
5225                 btrfs_set_root_bytenr(&reloc_root->root_item,
5226                                       reloc_root->node->start);
5227                 btrfs_set_root_level(&root->root_item,
5228                                      btrfs_header_level(reloc_root->node));
5229                 memset(&reloc_root->root_item.drop_progress, 0,
5230                         sizeof(struct btrfs_disk_key));
5231                 reloc_root->root_item.drop_level = 0;
5232
5233                 ret = btrfs_update_root(trans, root->fs_info->tree_root,
5234                                         &reloc_root->root_key,
5235                                         &reloc_root->root_item);
5236                 BUG_ON(ret);
5237         }
5238         return 0;
5239 }
5240
5241 int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
5242 {
5243         struct btrfs_trans_handle *trans;
5244         struct btrfs_root *reloc_root;
5245         struct btrfs_root *prev_root = NULL;
5246         struct list_head dead_roots;
5247         int ret;
5248         unsigned long nr;
5249
5250         INIT_LIST_HEAD(&dead_roots);
5251         list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
5252
5253         while (!list_empty(&dead_roots)) {
5254                 reloc_root = list_entry(dead_roots.prev,
5255                                         struct btrfs_root, dead_list);
5256                 list_del_init(&reloc_root->dead_list);
5257
5258                 BUG_ON(reloc_root->commit_root != NULL);
5259                 while (1) {
5260                         trans = btrfs_join_transaction(root, 1);
5261                         BUG_ON(!trans);
5262
5263                         mutex_lock(&root->fs_info->drop_mutex);
5264                         ret = btrfs_drop_snapshot(trans, reloc_root);
5265                         if (ret != -EAGAIN)
5266                                 break;
5267                         mutex_unlock(&root->fs_info->drop_mutex);
5268
5269                         nr = trans->blocks_used;
5270                         ret = btrfs_end_transaction(trans, root);
5271                         BUG_ON(ret);
5272                         btrfs_btree_balance_dirty(root, nr);
5273                 }
5274
5275                 free_extent_buffer(reloc_root->node);
5276
5277                 ret = btrfs_del_root(trans, root->fs_info->tree_root,
5278                                      &reloc_root->root_key);
5279                 BUG_ON(ret);
5280                 mutex_unlock(&root->fs_info->drop_mutex);
5281
5282                 nr = trans->blocks_used;
5283                 ret = btrfs_end_transaction(trans, root);
5284                 BUG_ON(ret);
5285                 btrfs_btree_balance_dirty(root, nr);
5286
5287                 kfree(prev_root);
5288                 prev_root = reloc_root;
5289         }
5290         if (prev_root) {
5291                 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
5292                 kfree(prev_root);
5293         }
5294         return 0;
5295 }
5296
5297 int btrfs_add_dead_reloc_root(struct btrfs_root *root)
5298 {
5299         list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
5300         return 0;
5301 }
5302
5303 int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
5304 {
5305         struct btrfs_root *reloc_root;
5306         struct btrfs_trans_handle *trans;
5307         struct btrfs_key location;
5308         int found;
5309         int ret;
5310
5311         mutex_lock(&root->fs_info->tree_reloc_mutex);
5312         ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
5313         BUG_ON(ret);
5314         found = !list_empty(&root->fs_info->dead_reloc_roots);
5315         mutex_unlock(&root->fs_info->tree_reloc_mutex);
5316
5317         if (found) {
5318                 trans = btrfs_start_transaction(root, 1);
5319                 BUG_ON(!trans);
5320                 ret = btrfs_commit_transaction(trans, root);
5321                 BUG_ON(ret);
5322         }
5323
5324         location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5325         location.offset = (u64)-1;
5326         location.type = BTRFS_ROOT_ITEM_KEY;
5327
5328         reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
5329         BUG_ON(!reloc_root);
5330         btrfs_orphan_cleanup(reloc_root);
5331         return 0;
5332 }
5333
5334 static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
5335                                     struct btrfs_root *root)
5336 {
5337         struct btrfs_root *reloc_root;
5338         struct extent_buffer *eb;
5339         struct btrfs_root_item *root_item;
5340         struct btrfs_key root_key;
5341         int ret;
5342
5343         BUG_ON(!root->ref_cows);
5344         if (root->reloc_root)
5345                 return 0;
5346
5347         root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
5348         BUG_ON(!root_item);
5349
5350         ret = btrfs_copy_root(trans, root, root->commit_root,
5351                               &eb, BTRFS_TREE_RELOC_OBJECTID);
5352         BUG_ON(ret);
5353
5354         root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
5355         root_key.offset = root->root_key.objectid;
5356         root_key.type = BTRFS_ROOT_ITEM_KEY;
5357
5358         memcpy(root_item, &root->root_item, sizeof(root_item));
5359         btrfs_set_root_refs(root_item, 0);
5360         btrfs_set_root_bytenr(root_item, eb->start);
5361         btrfs_set_root_level(root_item, btrfs_header_level(eb));
5362         btrfs_set_root_generation(root_item, trans->transid);
5363
5364         btrfs_tree_unlock(eb);
5365         free_extent_buffer(eb);
5366
5367         ret = btrfs_insert_root(trans, root->fs_info->tree_root,
5368                                 &root_key, root_item);
5369         BUG_ON(ret);
5370         kfree(root_item);
5371
5372         reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
5373                                                  &root_key);
5374         BUG_ON(!reloc_root);
5375         reloc_root->last_trans = trans->transid;
5376         reloc_root->commit_root = NULL;
5377         reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
5378
5379         root->reloc_root = reloc_root;
5380         return 0;
5381 }
5382
5383 /*
5384  * Core function of space balance.
5385  *
5386  * The idea is using reloc trees to relocate tree blocks in reference
5387  * counted roots. There is one reloc tree for each subvol, and all
5388  * reloc trees share same root key objectid. Reloc trees are snapshots
5389  * of the latest committed roots of subvols (root->commit_root).
5390  *
5391  * To relocate a tree block referenced by a subvol, there are two steps.
5392  * COW the block through subvol's reloc tree, then update block pointer
5393  * in the subvol to point to the new block. Since all reloc trees share
5394  * same root key objectid, doing special handing for tree blocks owned
5395  * by them is easy. Once a tree block has been COWed in one reloc tree,
5396  * we can use the resulting new block directly when the same block is
5397  * required to COW again through other reloc trees. By this way, relocated
5398  * tree blocks are shared between reloc trees, so they are also shared
5399  * between subvols.
5400  */
5401 static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
5402                                       struct btrfs_root *root,
5403                                       struct btrfs_path *path,
5404                                       struct btrfs_key *first_key,
5405                                       struct btrfs_ref_path *ref_path,
5406                                       struct btrfs_block_group_cache *group,
5407                                       struct inode *reloc_inode)
5408 {
5409         struct btrfs_root *reloc_root;
5410         struct extent_buffer *eb = NULL;
5411         struct btrfs_key *keys;
5412         u64 *nodes;
5413         int level;
5414         int shared_level;
5415         int lowest_level = 0;
5416         int ret;
5417
5418         if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
5419                 lowest_level = ref_path->owner_objectid;
5420
5421         if (!root->ref_cows) {
5422                 path->lowest_level = lowest_level;
5423                 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
5424                 BUG_ON(ret < 0);
5425                 path->lowest_level = 0;
5426                 btrfs_release_path(root, path);
5427                 return 0;
5428         }
5429
5430         mutex_lock(&root->fs_info->tree_reloc_mutex);
5431         ret = init_reloc_tree(trans, root);
5432         BUG_ON(ret);
5433         reloc_root = root->reloc_root;
5434
5435         shared_level = ref_path->shared_level;
5436         ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
5437
5438         keys = ref_path->node_keys;
5439         nodes = ref_path->new_nodes;
5440         memset(&keys[shared_level + 1], 0,
5441                sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
5442         memset(&nodes[shared_level + 1], 0,
5443                sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
5444
5445         if (nodes[lowest_level] == 0) {
5446                 path->lowest_level = lowest_level;
5447                 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5448                                         0, 1);
5449                 BUG_ON(ret);
5450                 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
5451                         eb = path->nodes[level];
5452                         if (!eb || eb == reloc_root->node)
5453                                 break;
5454                         nodes[level] = eb->start;
5455                         if (level == 0)
5456                                 btrfs_item_key_to_cpu(eb, &keys[level], 0);
5457                         else
5458                                 btrfs_node_key_to_cpu(eb, &keys[level], 0);
5459                 }
5460                 if (nodes[0] &&
5461                     ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5462                         eb = path->nodes[0];
5463                         ret = replace_extents_in_leaf(trans, reloc_root, eb,
5464                                                       group, reloc_inode);
5465                         BUG_ON(ret);
5466                 }
5467                 btrfs_release_path(reloc_root, path);
5468         } else {
5469                 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
5470                                        lowest_level);
5471                 BUG_ON(ret);
5472         }
5473
5474         /*
5475          * replace tree blocks in the fs tree with tree blocks in
5476          * the reloc tree.
5477          */
5478         ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
5479         BUG_ON(ret < 0);
5480
5481         if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5482                 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5483                                         0, 0);
5484                 BUG_ON(ret);
5485                 extent_buffer_get(path->nodes[0]);
5486                 eb = path->nodes[0];
5487                 btrfs_release_path(reloc_root, path);
5488                 ret = invalidate_extent_cache(reloc_root, eb, group, root);
5489                 BUG_ON(ret);
5490                 free_extent_buffer(eb);
5491         }
5492
5493         mutex_unlock(&root->fs_info->tree_reloc_mutex);
5494         path->lowest_level = 0;
5495         return 0;
5496 }
5497
5498 static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
5499                                         struct btrfs_root *root,
5500                                         struct btrfs_path *path,
5501                                         struct btrfs_key *first_key,
5502                                         struct btrfs_ref_path *ref_path)
5503 {
5504         int ret;
5505
5506         ret = relocate_one_path(trans, root, path, first_key,
5507                                 ref_path, NULL, NULL);
5508         BUG_ON(ret);
5509
5510         if (root == root->fs_info->extent_root)
5511                 btrfs_extent_post_op(trans, root);
5512
5513         return 0;
5514 }
5515
5516 static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
5517                                     struct btrfs_root *extent_root,
5518                                     struct btrfs_path *path,
5519                                     struct btrfs_key *extent_key)
5520 {
5521         int ret;
5522
5523         ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
5524         if (ret)
5525                 goto out;
5526         ret = btrfs_del_item(trans, extent_root, path);
5527 out:
5528         btrfs_release_path(extent_root, path);
5529         return ret;
5530 }
5531
5532 static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
5533                                                 struct btrfs_ref_path *ref_path)
5534 {
5535         struct btrfs_key root_key;
5536
5537         root_key.objectid = ref_path->root_objectid;
5538         root_key.type = BTRFS_ROOT_ITEM_KEY;
5539         if (is_cowonly_root(ref_path->root_objectid))
5540                 root_key.offset = 0;
5541         else
5542                 root_key.offset = (u64)-1;
5543
5544         return btrfs_read_fs_root_no_name(fs_info, &root_key);
5545 }
5546
5547 static noinline int relocate_one_extent(struct btrfs_root *extent_root,
5548                                         struct btrfs_path *path,
5549                                         struct btrfs_key *extent_key,
5550                                         struct btrfs_block_group_cache *group,
5551                                         struct inode *reloc_inode, int pass)
5552 {
5553         struct btrfs_trans_handle *trans;
5554         struct btrfs_root *found_root;
5555         struct btrfs_ref_path *ref_path = NULL;
5556         struct disk_extent *new_extents = NULL;
5557         int nr_extents = 0;
5558         int loops;
5559         int ret;
5560         int level;
5561         struct btrfs_key first_key;
5562         u64 prev_block = 0;
5563
5564
5565         trans = btrfs_start_transaction(extent_root, 1);
5566         BUG_ON(!trans);
5567
5568         if (extent_key->objectid == 0) {
5569                 ret = del_extent_zero(trans, extent_root, path, extent_key);
5570                 goto out;
5571         }
5572
5573         ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
5574         if (!ref_path) {
5575                 ret = -ENOMEM;
5576                 goto out;
5577         }
5578
5579         for (loops = 0; ; loops++) {
5580                 if (loops == 0) {
5581                         ret = btrfs_first_ref_path(trans, extent_root, ref_path,
5582                                                    extent_key->objectid);
5583                 } else {
5584                         ret = btrfs_next_ref_path(trans, extent_root, ref_path);
5585                 }
5586                 if (ret < 0)
5587                         goto out;
5588                 if (ret > 0)
5589                         break;
5590
5591                 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5592                     ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
5593                         continue;
5594
5595                 found_root = read_ref_root(extent_root->fs_info, ref_path);
5596                 BUG_ON(!found_root);
5597                 /*
5598                  * for reference counted tree, only process reference paths
5599                  * rooted at the latest committed root.
5600                  */
5601                 if (found_root->ref_cows &&
5602                     ref_path->root_generation != found_root->root_key.offset)
5603                         continue;
5604
5605                 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5606                         if (pass == 0) {
5607                                 /*
5608                                  * copy data extents to new locations
5609                                  */
5610                                 u64 group_start = group->key.objectid;
5611                                 ret = relocate_data_extent(reloc_inode,
5612                                                            extent_key,
5613                                                            group_start);
5614                                 if (ret < 0)
5615                                         goto out;
5616                                 break;
5617                         }
5618                         level = 0;
5619                 } else {
5620                         level = ref_path->owner_objectid;
5621                 }
5622
5623                 if (prev_block != ref_path->nodes[level]) {
5624                         struct extent_buffer *eb;
5625                         u64 block_start = ref_path->nodes[level];
5626                         u64 block_size = btrfs_level_size(found_root, level);
5627
5628                         eb = read_tree_block(found_root, block_start,
5629                                              block_size, 0);
5630                         btrfs_tree_lock(eb);
5631                         BUG_ON(level != btrfs_header_level(eb));
5632
5633                         if (level == 0)
5634                                 btrfs_item_key_to_cpu(eb, &first_key, 0);
5635                         else
5636                                 btrfs_node_key_to_cpu(eb, &first_key, 0);
5637
5638                         btrfs_tree_unlock(eb);
5639                         free_extent_buffer(eb);
5640                         prev_block = block_start;
5641                 }
5642
5643                 btrfs_record_root_in_trans(found_root);
5644                 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5645                         /*
5646                          * try to update data extent references while
5647                          * keeping metadata shared between snapshots.
5648                          */
5649                         if (pass == 1) {
5650                                 ret = relocate_one_path(trans, found_root,
5651                                                 path, &first_key, ref_path,
5652                                                 group, reloc_inode);
5653                                 if (ret < 0)
5654                                         goto out;
5655                                 continue;
5656                         }
5657                         /*
5658                          * use fallback method to process the remaining
5659                          * references.
5660                          */
5661                         if (!new_extents) {
5662                                 u64 group_start = group->key.objectid;
5663                                 new_extents = kmalloc(sizeof(*new_extents),
5664                                                       GFP_NOFS);
5665                                 nr_extents = 1;
5666                                 ret = get_new_locations(reloc_inode,
5667                                                         extent_key,
5668                                                         group_start, 1,
5669                                                         &new_extents,
5670                                                         &nr_extents);
5671                                 if (ret)
5672                                         goto out;
5673                         }
5674                         ret = replace_one_extent(trans, found_root,
5675                                                 path, extent_key,
5676                                                 &first_key, ref_path,
5677                                                 new_extents, nr_extents);
5678                 } else {
5679                         ret = relocate_tree_block(trans, found_root, path,
5680                                                   &first_key, ref_path);
5681                 }
5682                 if (ret < 0)
5683                         goto out;
5684         }
5685         ret = 0;
5686 out:
5687         btrfs_end_transaction(trans, extent_root);
5688         kfree(new_extents);
5689         kfree(ref_path);
5690         return ret;
5691 }
5692
5693 static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
5694 {
5695         u64 num_devices;
5696         u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
5697                 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
5698
5699         num_devices = root->fs_info->fs_devices->rw_devices;
5700         if (num_devices == 1) {
5701                 stripped |= BTRFS_BLOCK_GROUP_DUP;
5702                 stripped = flags & ~stripped;
5703
5704                 /* turn raid0 into single device chunks */
5705                 if (flags & BTRFS_BLOCK_GROUP_RAID0)
5706                         return stripped;
5707
5708                 /* turn mirroring into duplication */
5709                 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
5710                              BTRFS_BLOCK_GROUP_RAID10))
5711                         return stripped | BTRFS_BLOCK_GROUP_DUP;
5712                 return flags;
5713         } else {
5714                 /* they already had raid on here, just return */
5715                 if (flags & stripped)
5716                         return flags;
5717
5718                 stripped |= BTRFS_BLOCK_GROUP_DUP;
5719                 stripped = flags & ~stripped;
5720
5721                 /* switch duplicated blocks with raid1 */
5722                 if (flags & BTRFS_BLOCK_GROUP_DUP)
5723                         return stripped | BTRFS_BLOCK_GROUP_RAID1;
5724
5725                 /* turn single device chunks into raid0 */
5726                 return stripped | BTRFS_BLOCK_GROUP_RAID0;
5727         }
5728         return flags;
5729 }
5730
5731 static int __alloc_chunk_for_shrink(struct btrfs_root *root,
5732                      struct btrfs_block_group_cache *shrink_block_group,
5733                      int force)
5734 {
5735         struct btrfs_trans_handle *trans;
5736         u64 new_alloc_flags;
5737         u64 calc;
5738
5739         spin_lock(&shrink_block_group->lock);
5740         if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
5741                 spin_unlock(&shrink_block_group->lock);
5742
5743                 trans = btrfs_start_transaction(root, 1);
5744                 spin_lock(&shrink_block_group->lock);
5745
5746                 new_alloc_flags = update_block_group_flags(root,
5747                                                    shrink_block_group->flags);
5748                 if (new_alloc_flags != shrink_block_group->flags) {
5749                         calc =
5750                              btrfs_block_group_used(&shrink_block_group->item);
5751                 } else {
5752                         calc = shrink_block_group->key.offset;
5753                 }
5754                 spin_unlock(&shrink_block_group->lock);
5755
5756                 do_chunk_alloc(trans, root->fs_info->extent_root,
5757                                calc + 2 * 1024 * 1024, new_alloc_flags, force);
5758
5759                 btrfs_end_transaction(trans, root);
5760         } else
5761                 spin_unlock(&shrink_block_group->lock);
5762         return 0;
5763 }
5764
5765 static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
5766                                  struct btrfs_root *root,
5767                                  u64 objectid, u64 size)
5768 {
5769         struct btrfs_path *path;
5770         struct btrfs_inode_item *item;
5771         struct extent_buffer *leaf;
5772         int ret;
5773
5774         path = btrfs_alloc_path();
5775         if (!path)
5776                 return -ENOMEM;
5777
5778         ret = btrfs_insert_empty_inode(trans, root, path, objectid);
5779         if (ret)
5780                 goto out;
5781
5782         leaf = path->nodes[0];
5783         item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
5784         memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
5785         btrfs_set_inode_generation(leaf, item, 1);
5786         btrfs_set_inode_size(leaf, item, size);
5787         btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
5788         btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS);
5789         btrfs_mark_buffer_dirty(leaf);
5790         btrfs_release_path(root, path);
5791 out:
5792         btrfs_free_path(path);
5793         return ret;
5794 }
5795
5796 static noinline struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
5797                                         struct btrfs_block_group_cache *group)
5798 {
5799         struct inode *inode = NULL;
5800         struct btrfs_trans_handle *trans;
5801         struct btrfs_root *root;
5802         struct btrfs_key root_key;
5803         u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
5804         int err = 0;
5805
5806         root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5807         root_key.type = BTRFS_ROOT_ITEM_KEY;
5808         root_key.offset = (u64)-1;
5809         root = btrfs_read_fs_root_no_name(fs_info, &root_key);
5810         if (IS_ERR(root))
5811                 return ERR_CAST(root);
5812
5813         trans = btrfs_start_transaction(root, 1);
5814         BUG_ON(!trans);
5815
5816         err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
5817         if (err)
5818                 goto out;
5819
5820         err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
5821         BUG_ON(err);
5822
5823         err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
5824                                        group->key.offset, 0, group->key.offset,
5825                                        0, 0, 0);
5826         BUG_ON(err);
5827
5828         inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
5829         if (inode->i_state & I_NEW) {
5830                 BTRFS_I(inode)->root = root;
5831                 BTRFS_I(inode)->location.objectid = objectid;
5832                 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5833                 BTRFS_I(inode)->location.offset = 0;
5834                 btrfs_read_locked_inode(inode);
5835                 unlock_new_inode(inode);
5836                 BUG_ON(is_bad_inode(inode));
5837         } else {
5838                 BUG_ON(1);
5839         }
5840         BTRFS_I(inode)->index_cnt = group->key.objectid;
5841
5842         err = btrfs_orphan_add(trans, inode);
5843 out:
5844         btrfs_end_transaction(trans, root);
5845         if (err) {
5846                 if (inode)
5847                         iput(inode);
5848                 inode = ERR_PTR(err);
5849         }
5850         return inode;
5851 }
5852
5853 int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
5854 {
5855
5856         struct btrfs_ordered_sum *sums;
5857         struct btrfs_sector_sum *sector_sum;
5858         struct btrfs_ordered_extent *ordered;
5859         struct btrfs_root *root = BTRFS_I(inode)->root;
5860         struct list_head list;
5861         size_t offset;
5862         int ret;
5863         u64 disk_bytenr;
5864
5865         INIT_LIST_HEAD(&list);
5866
5867         ordered = btrfs_lookup_ordered_extent(inode, file_pos);
5868         BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
5869
5870         disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
5871         ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
5872                                        disk_bytenr + len - 1, &list);
5873
5874         while (!list_empty(&list)) {
5875                 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
5876                 list_del_init(&sums->list);
5877
5878                 sector_sum = sums->sums;
5879                 sums->bytenr = ordered->start;
5880
5881                 offset = 0;
5882                 while (offset < sums->len) {
5883                         sector_sum->bytenr += ordered->start - disk_bytenr;
5884                         sector_sum++;
5885                         offset += root->sectorsize;
5886                 }
5887
5888                 btrfs_add_ordered_sum(inode, ordered, sums);
5889         }
5890         btrfs_put_ordered_extent(ordered);
5891         return 0;
5892 }
5893
5894 int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
5895 {
5896         struct btrfs_trans_handle *trans;
5897         struct btrfs_path *path;
5898         struct btrfs_fs_info *info = root->fs_info;
5899         struct extent_buffer *leaf;
5900         struct inode *reloc_inode;
5901         struct btrfs_block_group_cache *block_group;
5902         struct btrfs_key key;
5903         u64 skipped;
5904         u64 cur_byte;
5905         u64 total_found;
5906         u32 nritems;
5907         int ret;
5908         int progress;
5909         int pass = 0;
5910
5911         root = root->fs_info->extent_root;
5912
5913         block_group = btrfs_lookup_block_group(info, group_start);
5914         BUG_ON(!block_group);
5915
5916         printk(KERN_INFO "btrfs relocating block group %llu flags %llu\n",
5917                (unsigned long long)block_group->key.objectid,
5918                (unsigned long long)block_group->flags);
5919
5920         path = btrfs_alloc_path();
5921         BUG_ON(!path);
5922
5923         reloc_inode = create_reloc_inode(info, block_group);
5924         BUG_ON(IS_ERR(reloc_inode));
5925
5926         __alloc_chunk_for_shrink(root, block_group, 1);
5927         set_block_group_readonly(block_group);
5928
5929         btrfs_start_delalloc_inodes(info->tree_root);
5930         btrfs_wait_ordered_extents(info->tree_root, 0);
5931 again:
5932         skipped = 0;
5933         total_found = 0;
5934         progress = 0;
5935         key.objectid = block_group->key.objectid;
5936         key.offset = 0;
5937         key.type = 0;
5938         cur_byte = key.objectid;
5939
5940         trans = btrfs_start_transaction(info->tree_root, 1);
5941         btrfs_commit_transaction(trans, info->tree_root);
5942
5943         mutex_lock(&root->fs_info->cleaner_mutex);
5944         btrfs_clean_old_snapshots(info->tree_root);
5945         btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
5946         mutex_unlock(&root->fs_info->cleaner_mutex);
5947
5948         while (1) {
5949                 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5950                 if (ret < 0)
5951                         goto out;
5952 next:
5953                 leaf = path->nodes[0];
5954                 nritems = btrfs_header_nritems(leaf);
5955                 if (path->slots[0] >= nritems) {
5956                         ret = btrfs_next_leaf(root, path);
5957                         if (ret < 0)
5958                                 goto out;
5959                         if (ret == 1) {
5960                                 ret = 0;
5961                                 break;
5962                         }
5963                         leaf = path->nodes[0];
5964                         nritems = btrfs_header_nritems(leaf);
5965                 }
5966
5967                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5968
5969                 if (key.objectid >= block_group->key.objectid +
5970                     block_group->key.offset)
5971                         break;
5972
5973                 if (progress && need_resched()) {
5974                         btrfs_release_path(root, path);
5975                         cond_resched();
5976                         progress = 0;
5977                         continue;
5978                 }
5979                 progress = 1;
5980
5981                 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
5982                     key.objectid + key.offset <= cur_byte) {
5983                         path->slots[0]++;
5984                         goto next;
5985                 }
5986
5987                 total_found++;
5988                 cur_byte = key.objectid + key.offset;
5989                 btrfs_release_path(root, path);
5990
5991                 __alloc_chunk_for_shrink(root, block_group, 0);
5992                 ret = relocate_one_extent(root, path, &key, block_group,
5993                                           reloc_inode, pass);
5994                 BUG_ON(ret < 0);
5995                 if (ret > 0)
5996                         skipped++;
5997
5998                 key.objectid = cur_byte;
5999                 key.type = 0;
6000                 key.offset = 0;
6001         }
6002
6003         btrfs_release_path(root, path);
6004
6005         if (pass == 0) {
6006                 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
6007                 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
6008         }
6009
6010         if (total_found > 0) {
6011                 printk(KERN_INFO "btrfs found %llu extents in pass %d\n",
6012                        (unsigned long long)total_found, pass);
6013                 pass++;
6014                 if (total_found == skipped && pass > 2) {
6015                         iput(reloc_inode);
6016                         reloc_inode = create_reloc_inode(info, block_group);
6017                         pass = 0;
6018                 }
6019                 goto again;
6020         }
6021
6022         /* delete reloc_inode */
6023         iput(reloc_inode);
6024
6025         /* unpin extents in this range */
6026         trans = btrfs_start_transaction(info->tree_root, 1);
6027         btrfs_commit_transaction(trans, info->tree_root);
6028
6029         spin_lock(&block_group->lock);
6030         WARN_ON(block_group->pinned > 0);
6031         WARN_ON(block_group->reserved > 0);
6032         WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
6033         spin_unlock(&block_group->lock);
6034         put_block_group(block_group);
6035         ret = 0;
6036 out:
6037         btrfs_free_path(path);
6038         return ret;
6039 }
6040
6041 static int find_first_block_group(struct btrfs_root *root,
6042                 struct btrfs_path *path, struct btrfs_key *key)
6043 {
6044         int ret = 0;
6045         struct btrfs_key found_key;
6046         struct extent_buffer *leaf;
6047         int slot;
6048
6049         ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
6050         if (ret < 0)
6051                 goto out;
6052
6053         while (1) {
6054                 slot = path->slots[0];
6055                 leaf = path->nodes[0];
6056                 if (slot >= btrfs_header_nritems(leaf)) {
6057                         ret = btrfs_next_leaf(root, path);
6058                         if (ret == 0)
6059                                 continue;
6060                         if (ret < 0)
6061                                 goto out;
6062                         break;
6063                 }
6064                 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6065
6066                 if (found_key.objectid >= key->objectid &&
6067                     found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
6068                         ret = 0;
6069                         goto out;
6070                 }
6071                 path->slots[0]++;
6072         }
6073         ret = -ENOENT;
6074 out:
6075         return ret;
6076 }
6077
6078 int btrfs_free_block_groups(struct btrfs_fs_info *info)
6079 {
6080         struct btrfs_block_group_cache *block_group;
6081         struct rb_node *n;
6082
6083         spin_lock(&info->block_group_cache_lock);
6084         while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
6085                 block_group = rb_entry(n, struct btrfs_block_group_cache,
6086                                        cache_node);
6087                 rb_erase(&block_group->cache_node,
6088                          &info->block_group_cache_tree);
6089                 spin_unlock(&info->block_group_cache_lock);
6090
6091                 btrfs_remove_free_space_cache(block_group);
6092                 down_write(&block_group->space_info->groups_sem);
6093                 list_del(&block_group->list);
6094                 up_write(&block_group->space_info->groups_sem);
6095
6096                 WARN_ON(atomic_read(&block_group->count) != 1);
6097                 kfree(block_group);
6098
6099                 spin_lock(&info->block_group_cache_lock);
6100         }
6101         spin_unlock(&info->block_group_cache_lock);
6102         return 0;
6103 }
6104
6105 int btrfs_read_block_groups(struct btrfs_root *root)
6106 {
6107         struct btrfs_path *path;
6108         int ret;
6109         struct btrfs_block_group_cache *cache;
6110         struct btrfs_fs_info *info = root->fs_info;
6111         struct btrfs_space_info *space_info;
6112         struct btrfs_key key;
6113         struct btrfs_key found_key;
6114         struct extent_buffer *leaf;
6115
6116         root = info->extent_root;
6117         key.objectid = 0;
6118         key.offset = 0;
6119         btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
6120         path = btrfs_alloc_path();
6121         if (!path)
6122                 return -ENOMEM;
6123
6124         while (1) {
6125                 ret = find_first_block_group(root, path, &key);
6126                 if (ret > 0) {
6127                         ret = 0;
6128                         goto error;
6129                 }
6130                 if (ret != 0)
6131                         goto error;
6132
6133                 leaf = path->nodes[0];
6134                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6135                 cache = kzalloc(sizeof(*cache), GFP_NOFS);
6136                 if (!cache) {
6137                         ret = -ENOMEM;
6138                         break;
6139                 }
6140
6141                 atomic_set(&cache->count, 1);
6142                 spin_lock_init(&cache->lock);
6143                 mutex_init(&cache->alloc_mutex);
6144                 mutex_init(&cache->cache_mutex);
6145                 INIT_LIST_HEAD(&cache->list);
6146                 read_extent_buffer(leaf, &cache->item,
6147                                    btrfs_item_ptr_offset(leaf, path->slots[0]),
6148                                    sizeof(cache->item));
6149                 memcpy(&cache->key, &found_key, sizeof(found_key));
6150
6151                 key.objectid = found_key.objectid + found_key.offset;
6152                 btrfs_release_path(root, path);
6153                 cache->flags = btrfs_block_group_flags(&cache->item);
6154
6155                 ret = update_space_info(info, cache->flags, found_key.offset,
6156                                         btrfs_block_group_used(&cache->item),
6157                                         &space_info);
6158                 BUG_ON(ret);
6159                 cache->space_info = space_info;
6160                 down_write(&space_info->groups_sem);
6161                 list_add_tail(&cache->list, &space_info->block_groups);
6162                 up_write(&space_info->groups_sem);
6163
6164                 ret = btrfs_add_block_group_cache(root->fs_info, cache);
6165                 BUG_ON(ret);
6166
6167                 set_avail_alloc_bits(root->fs_info, cache->flags);
6168                 if (btrfs_chunk_readonly(root, cache->key.objectid))
6169                         set_block_group_readonly(cache);
6170         }
6171         ret = 0;
6172 error:
6173         btrfs_free_path(path);
6174         return ret;
6175 }
6176
6177 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
6178                            struct btrfs_root *root, u64 bytes_used,
6179                            u64 type, u64 chunk_objectid, u64 chunk_offset,
6180                            u64 size)
6181 {
6182         int ret;
6183         struct btrfs_root *extent_root;
6184         struct btrfs_block_group_cache *cache;
6185
6186         extent_root = root->fs_info->extent_root;
6187
6188         root->fs_info->last_trans_new_blockgroup = trans->transid;
6189
6190         cache = kzalloc(sizeof(*cache), GFP_NOFS);
6191         if (!cache)
6192                 return -ENOMEM;
6193
6194         cache->key.objectid = chunk_offset;
6195         cache->key.offset = size;
6196         cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
6197         atomic_set(&cache->count, 1);
6198         spin_lock_init(&cache->lock);
6199         mutex_init(&cache->alloc_mutex);
6200         mutex_init(&cache->cache_mutex);
6201         INIT_LIST_HEAD(&cache->list);
6202
6203         btrfs_set_block_group_used(&cache->item, bytes_used);
6204         btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
6205         cache->flags = type;
6206         btrfs_set_block_group_flags(&cache->item, type);
6207
6208         ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
6209                                 &cache->space_info);
6210         BUG_ON(ret);
6211         down_write(&cache->space_info->groups_sem);
6212         list_add_tail(&cache->list, &cache->space_info->block_groups);
6213         up_write(&cache->space_info->groups_sem);
6214
6215         ret = btrfs_add_block_group_cache(root->fs_info, cache);
6216         BUG_ON(ret);
6217
6218         ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
6219                                 sizeof(cache->item));
6220         BUG_ON(ret);
6221
6222         finish_current_insert(trans, extent_root, 0);
6223         ret = del_pending_extents(trans, extent_root, 0);
6224         BUG_ON(ret);
6225         set_avail_alloc_bits(extent_root->fs_info, type);
6226
6227         return 0;
6228 }
6229
6230 int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
6231                              struct btrfs_root *root, u64 group_start)
6232 {
6233         struct btrfs_path *path;
6234         struct btrfs_block_group_cache *block_group;
6235         struct btrfs_key key;
6236         int ret;
6237
6238         root = root->fs_info->extent_root;
6239
6240         block_group = btrfs_lookup_block_group(root->fs_info, group_start);
6241         BUG_ON(!block_group);
6242         BUG_ON(!block_group->ro);
6243
6244         memcpy(&key, &block_group->key, sizeof(key));
6245
6246         path = btrfs_alloc_path();
6247         BUG_ON(!path);
6248
6249         spin_lock(&root->fs_info->block_group_cache_lock);
6250         rb_erase(&block_group->cache_node,
6251                  &root->fs_info->block_group_cache_tree);
6252         spin_unlock(&root->fs_info->block_group_cache_lock);
6253         btrfs_remove_free_space_cache(block_group);
6254         down_write(&block_group->space_info->groups_sem);
6255         list_del(&block_group->list);
6256         up_write(&block_group->space_info->groups_sem);
6257
6258         spin_lock(&block_group->space_info->lock);
6259         block_group->space_info->total_bytes -= block_group->key.offset;
6260         block_group->space_info->bytes_readonly -= block_group->key.offset;
6261         spin_unlock(&block_group->space_info->lock);
6262         block_group->space_info->full = 0;
6263
6264         put_block_group(block_group);
6265         put_block_group(block_group);
6266
6267         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
6268         if (ret > 0)
6269                 ret = -EIO;
6270         if (ret < 0)
6271                 goto out;
6272
6273         ret = btrfs_del_item(trans, root, path);
6274 out:
6275         btrfs_free_path(path);
6276         return ret;
6277 }