e238a0cdac677c06b1774018bf549b03399da4c8
[platform/adaptation/renesas_rcar/renesas_kernel.git] / 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 <linux/rcupdate.h>
24 #include <linux/kthread.h>
25 #include "compat.h"
26 #include "hash.h"
27 #include "ctree.h"
28 #include "disk-io.h"
29 #include "print-tree.h"
30 #include "transaction.h"
31 #include "volumes.h"
32 #include "locking.h"
33 #include "free-space-cache.h"
34
35 static int update_block_group(struct btrfs_trans_handle *trans,
36                               struct btrfs_root *root,
37                               u64 bytenr, u64 num_bytes, int alloc,
38                               int mark_free);
39 static int update_reserved_extents(struct btrfs_block_group_cache *cache,
40                                    u64 num_bytes, int reserve);
41 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
42                                 struct btrfs_root *root,
43                                 u64 bytenr, u64 num_bytes, u64 parent,
44                                 u64 root_objectid, u64 owner_objectid,
45                                 u64 owner_offset, int refs_to_drop,
46                                 struct btrfs_delayed_extent_op *extra_op);
47 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
48                                     struct extent_buffer *leaf,
49                                     struct btrfs_extent_item *ei);
50 static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
51                                       struct btrfs_root *root,
52                                       u64 parent, u64 root_objectid,
53                                       u64 flags, u64 owner, u64 offset,
54                                       struct btrfs_key *ins, int ref_mod);
55 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
56                                      struct btrfs_root *root,
57                                      u64 parent, u64 root_objectid,
58                                      u64 flags, struct btrfs_disk_key *key,
59                                      int level, struct btrfs_key *ins);
60 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
61                           struct btrfs_root *extent_root, u64 alloc_bytes,
62                           u64 flags, int force);
63 static int pin_down_bytes(struct btrfs_trans_handle *trans,
64                           struct btrfs_root *root,
65                           struct btrfs_path *path,
66                           u64 bytenr, u64 num_bytes,
67                           int is_data, int reserved,
68                           struct extent_buffer **must_clean);
69 static int find_next_key(struct btrfs_path *path, int level,
70                          struct btrfs_key *key);
71 static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
72                             int dump_block_groups);
73
74 static noinline int
75 block_group_cache_done(struct btrfs_block_group_cache *cache)
76 {
77         smp_mb();
78         return cache->cached == BTRFS_CACHE_FINISHED;
79 }
80
81 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
82 {
83         return (cache->flags & bits) == bits;
84 }
85
86 /*
87  * this adds the block group to the fs_info rb tree for the block group
88  * cache
89  */
90 static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
91                                 struct btrfs_block_group_cache *block_group)
92 {
93         struct rb_node **p;
94         struct rb_node *parent = NULL;
95         struct btrfs_block_group_cache *cache;
96
97         spin_lock(&info->block_group_cache_lock);
98         p = &info->block_group_cache_tree.rb_node;
99
100         while (*p) {
101                 parent = *p;
102                 cache = rb_entry(parent, struct btrfs_block_group_cache,
103                                  cache_node);
104                 if (block_group->key.objectid < cache->key.objectid) {
105                         p = &(*p)->rb_left;
106                 } else if (block_group->key.objectid > cache->key.objectid) {
107                         p = &(*p)->rb_right;
108                 } else {
109                         spin_unlock(&info->block_group_cache_lock);
110                         return -EEXIST;
111                 }
112         }
113
114         rb_link_node(&block_group->cache_node, parent, p);
115         rb_insert_color(&block_group->cache_node,
116                         &info->block_group_cache_tree);
117         spin_unlock(&info->block_group_cache_lock);
118
119         return 0;
120 }
121
122 /*
123  * This will return the block group at or after bytenr if contains is 0, else
124  * it will return the block group that contains the bytenr
125  */
126 static struct btrfs_block_group_cache *
127 block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
128                               int contains)
129 {
130         struct btrfs_block_group_cache *cache, *ret = NULL;
131         struct rb_node *n;
132         u64 end, start;
133
134         spin_lock(&info->block_group_cache_lock);
135         n = info->block_group_cache_tree.rb_node;
136
137         while (n) {
138                 cache = rb_entry(n, struct btrfs_block_group_cache,
139                                  cache_node);
140                 end = cache->key.objectid + cache->key.offset - 1;
141                 start = cache->key.objectid;
142
143                 if (bytenr < start) {
144                         if (!contains && (!ret || start < ret->key.objectid))
145                                 ret = cache;
146                         n = n->rb_left;
147                 } else if (bytenr > start) {
148                         if (contains && bytenr <= end) {
149                                 ret = cache;
150                                 break;
151                         }
152                         n = n->rb_right;
153                 } else {
154                         ret = cache;
155                         break;
156                 }
157         }
158         if (ret)
159                 atomic_inc(&ret->count);
160         spin_unlock(&info->block_group_cache_lock);
161
162         return ret;
163 }
164
165 static int add_excluded_extent(struct btrfs_root *root,
166                                u64 start, u64 num_bytes)
167 {
168         u64 end = start + num_bytes - 1;
169         set_extent_bits(&root->fs_info->freed_extents[0],
170                         start, end, EXTENT_UPTODATE, GFP_NOFS);
171         set_extent_bits(&root->fs_info->freed_extents[1],
172                         start, end, EXTENT_UPTODATE, GFP_NOFS);
173         return 0;
174 }
175
176 static void free_excluded_extents(struct btrfs_root *root,
177                                   struct btrfs_block_group_cache *cache)
178 {
179         u64 start, end;
180
181         start = cache->key.objectid;
182         end = start + cache->key.offset - 1;
183
184         clear_extent_bits(&root->fs_info->freed_extents[0],
185                           start, end, EXTENT_UPTODATE, GFP_NOFS);
186         clear_extent_bits(&root->fs_info->freed_extents[1],
187                           start, end, EXTENT_UPTODATE, GFP_NOFS);
188 }
189
190 static int exclude_super_stripes(struct btrfs_root *root,
191                                  struct btrfs_block_group_cache *cache)
192 {
193         u64 bytenr;
194         u64 *logical;
195         int stripe_len;
196         int i, nr, ret;
197
198         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
199                 bytenr = btrfs_sb_offset(i);
200                 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
201                                        cache->key.objectid, bytenr,
202                                        0, &logical, &nr, &stripe_len);
203                 BUG_ON(ret);
204
205                 while (nr--) {
206                         cache->bytes_super += stripe_len;
207                         ret = add_excluded_extent(root, logical[nr],
208                                                   stripe_len);
209                         BUG_ON(ret);
210                 }
211
212                 kfree(logical);
213         }
214         return 0;
215 }
216
217 static struct btrfs_caching_control *
218 get_caching_control(struct btrfs_block_group_cache *cache)
219 {
220         struct btrfs_caching_control *ctl;
221
222         spin_lock(&cache->lock);
223         if (cache->cached != BTRFS_CACHE_STARTED) {
224                 spin_unlock(&cache->lock);
225                 return NULL;
226         }
227
228         ctl = cache->caching_ctl;
229         atomic_inc(&ctl->count);
230         spin_unlock(&cache->lock);
231         return ctl;
232 }
233
234 static void put_caching_control(struct btrfs_caching_control *ctl)
235 {
236         if (atomic_dec_and_test(&ctl->count))
237                 kfree(ctl);
238 }
239
240 /*
241  * this is only called by cache_block_group, since we could have freed extents
242  * we need to check the pinned_extents for any extents that can't be used yet
243  * since their free space will be released as soon as the transaction commits.
244  */
245 static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
246                               struct btrfs_fs_info *info, u64 start, u64 end)
247 {
248         u64 extent_start, extent_end, size, total_added = 0;
249         int ret;
250
251         while (start < end) {
252                 ret = find_first_extent_bit(info->pinned_extents, start,
253                                             &extent_start, &extent_end,
254                                             EXTENT_DIRTY | EXTENT_UPTODATE);
255                 if (ret)
256                         break;
257
258                 if (extent_start == start) {
259                         start = extent_end + 1;
260                 } else if (extent_start > start && extent_start < end) {
261                         size = extent_start - start;
262                         total_added += size;
263                         ret = btrfs_add_free_space(block_group, start,
264                                                    size);
265                         BUG_ON(ret);
266                         start = extent_end + 1;
267                 } else {
268                         break;
269                 }
270         }
271
272         if (start < end) {
273                 size = end - start;
274                 total_added += size;
275                 ret = btrfs_add_free_space(block_group, start, size);
276                 BUG_ON(ret);
277         }
278
279         return total_added;
280 }
281
282 static int caching_kthread(void *data)
283 {
284         struct btrfs_block_group_cache *block_group = data;
285         struct btrfs_fs_info *fs_info = block_group->fs_info;
286         struct btrfs_caching_control *caching_ctl = block_group->caching_ctl;
287         struct btrfs_root *extent_root = fs_info->extent_root;
288         struct btrfs_path *path;
289         struct extent_buffer *leaf;
290         struct btrfs_key key;
291         u64 total_found = 0;
292         u64 last = 0;
293         u32 nritems;
294         int ret = 0;
295
296         path = btrfs_alloc_path();
297         if (!path)
298                 return -ENOMEM;
299
300         exclude_super_stripes(extent_root, block_group);
301         spin_lock(&block_group->space_info->lock);
302         block_group->space_info->bytes_super += block_group->bytes_super;
303         spin_unlock(&block_group->space_info->lock);
304
305         last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
306
307         /*
308          * We don't want to deadlock with somebody trying to allocate a new
309          * extent for the extent root while also trying to search the extent
310          * root to add free space.  So we skip locking and search the commit
311          * root, since its read-only
312          */
313         path->skip_locking = 1;
314         path->search_commit_root = 1;
315         path->reada = 2;
316
317         key.objectid = last;
318         key.offset = 0;
319         key.type = BTRFS_EXTENT_ITEM_KEY;
320 again:
321         mutex_lock(&caching_ctl->mutex);
322         /* need to make sure the commit_root doesn't disappear */
323         down_read(&fs_info->extent_commit_sem);
324
325         ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
326         if (ret < 0)
327                 goto err;
328
329         leaf = path->nodes[0];
330         nritems = btrfs_header_nritems(leaf);
331
332         while (1) {
333                 smp_mb();
334                 if (fs_info->closing > 1) {
335                         last = (u64)-1;
336                         break;
337                 }
338
339                 if (path->slots[0] < nritems) {
340                         btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
341                 } else {
342                         ret = find_next_key(path, 0, &key);
343                         if (ret)
344                                 break;
345
346                         caching_ctl->progress = last;
347                         btrfs_release_path(extent_root, path);
348                         up_read(&fs_info->extent_commit_sem);
349                         mutex_unlock(&caching_ctl->mutex);
350                         if (btrfs_transaction_in_commit(fs_info))
351                                 schedule_timeout(1);
352                         else
353                                 cond_resched();
354                         goto again;
355                 }
356
357                 if (key.objectid < block_group->key.objectid) {
358                         path->slots[0]++;
359                         continue;
360                 }
361
362                 if (key.objectid >= block_group->key.objectid +
363                     block_group->key.offset)
364                         break;
365
366                 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
367                         total_found += add_new_free_space(block_group,
368                                                           fs_info, last,
369                                                           key.objectid);
370                         last = key.objectid + key.offset;
371
372                         if (total_found > (1024 * 1024 * 2)) {
373                                 total_found = 0;
374                                 wake_up(&caching_ctl->wait);
375                         }
376                 }
377                 path->slots[0]++;
378         }
379         ret = 0;
380
381         total_found += add_new_free_space(block_group, fs_info, last,
382                                           block_group->key.objectid +
383                                           block_group->key.offset);
384         caching_ctl->progress = (u64)-1;
385
386         spin_lock(&block_group->lock);
387         block_group->caching_ctl = NULL;
388         block_group->cached = BTRFS_CACHE_FINISHED;
389         spin_unlock(&block_group->lock);
390
391 err:
392         btrfs_free_path(path);
393         up_read(&fs_info->extent_commit_sem);
394
395         free_excluded_extents(extent_root, block_group);
396
397         mutex_unlock(&caching_ctl->mutex);
398         wake_up(&caching_ctl->wait);
399
400         put_caching_control(caching_ctl);
401         atomic_dec(&block_group->space_info->caching_threads);
402         return 0;
403 }
404
405 static int cache_block_group(struct btrfs_block_group_cache *cache)
406 {
407         struct btrfs_fs_info *fs_info = cache->fs_info;
408         struct btrfs_caching_control *caching_ctl;
409         struct task_struct *tsk;
410         int ret = 0;
411
412         smp_mb();
413         if (cache->cached != BTRFS_CACHE_NO)
414                 return 0;
415
416         caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_KERNEL);
417         BUG_ON(!caching_ctl);
418
419         INIT_LIST_HEAD(&caching_ctl->list);
420         mutex_init(&caching_ctl->mutex);
421         init_waitqueue_head(&caching_ctl->wait);
422         caching_ctl->block_group = cache;
423         caching_ctl->progress = cache->key.objectid;
424         /* one for caching kthread, one for caching block group list */
425         atomic_set(&caching_ctl->count, 2);
426
427         spin_lock(&cache->lock);
428         if (cache->cached != BTRFS_CACHE_NO) {
429                 spin_unlock(&cache->lock);
430                 kfree(caching_ctl);
431                 return 0;
432         }
433         cache->caching_ctl = caching_ctl;
434         cache->cached = BTRFS_CACHE_STARTED;
435         spin_unlock(&cache->lock);
436
437         down_write(&fs_info->extent_commit_sem);
438         list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
439         up_write(&fs_info->extent_commit_sem);
440
441         atomic_inc(&cache->space_info->caching_threads);
442
443         tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n",
444                           cache->key.objectid);
445         if (IS_ERR(tsk)) {
446                 ret = PTR_ERR(tsk);
447                 printk(KERN_ERR "error running thread %d\n", ret);
448                 BUG();
449         }
450
451         return ret;
452 }
453
454 /*
455  * return the block group that starts at or after bytenr
456  */
457 static struct btrfs_block_group_cache *
458 btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
459 {
460         struct btrfs_block_group_cache *cache;
461
462         cache = block_group_cache_tree_search(info, bytenr, 0);
463
464         return cache;
465 }
466
467 /*
468  * return the block group that contains the given bytenr
469  */
470 struct btrfs_block_group_cache *btrfs_lookup_block_group(
471                                                  struct btrfs_fs_info *info,
472                                                  u64 bytenr)
473 {
474         struct btrfs_block_group_cache *cache;
475
476         cache = block_group_cache_tree_search(info, bytenr, 1);
477
478         return cache;
479 }
480
481 void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
482 {
483         if (atomic_dec_and_test(&cache->count))
484                 kfree(cache);
485 }
486
487 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
488                                                   u64 flags)
489 {
490         struct list_head *head = &info->space_info;
491         struct btrfs_space_info *found;
492
493         rcu_read_lock();
494         list_for_each_entry_rcu(found, head, list) {
495                 if (found->flags == flags) {
496                         rcu_read_unlock();
497                         return found;
498                 }
499         }
500         rcu_read_unlock();
501         return NULL;
502 }
503
504 /*
505  * after adding space to the filesystem, we need to clear the full flags
506  * on all the space infos.
507  */
508 void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
509 {
510         struct list_head *head = &info->space_info;
511         struct btrfs_space_info *found;
512
513         rcu_read_lock();
514         list_for_each_entry_rcu(found, head, list)
515                 found->full = 0;
516         rcu_read_unlock();
517 }
518
519 static u64 div_factor(u64 num, int factor)
520 {
521         if (factor == 10)
522                 return num;
523         num *= factor;
524         do_div(num, 10);
525         return num;
526 }
527
528 u64 btrfs_find_block_group(struct btrfs_root *root,
529                            u64 search_start, u64 search_hint, int owner)
530 {
531         struct btrfs_block_group_cache *cache;
532         u64 used;
533         u64 last = max(search_hint, search_start);
534         u64 group_start = 0;
535         int full_search = 0;
536         int factor = 9;
537         int wrapped = 0;
538 again:
539         while (1) {
540                 cache = btrfs_lookup_first_block_group(root->fs_info, last);
541                 if (!cache)
542                         break;
543
544                 spin_lock(&cache->lock);
545                 last = cache->key.objectid + cache->key.offset;
546                 used = btrfs_block_group_used(&cache->item);
547
548                 if ((full_search || !cache->ro) &&
549                     block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
550                         if (used + cache->pinned + cache->reserved <
551                             div_factor(cache->key.offset, factor)) {
552                                 group_start = cache->key.objectid;
553                                 spin_unlock(&cache->lock);
554                                 btrfs_put_block_group(cache);
555                                 goto found;
556                         }
557                 }
558                 spin_unlock(&cache->lock);
559                 btrfs_put_block_group(cache);
560                 cond_resched();
561         }
562         if (!wrapped) {
563                 last = search_start;
564                 wrapped = 1;
565                 goto again;
566         }
567         if (!full_search && factor < 10) {
568                 last = search_start;
569                 full_search = 1;
570                 factor = 10;
571                 goto again;
572         }
573 found:
574         return group_start;
575 }
576
577 /* simple helper to search for an existing extent at a given offset */
578 int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
579 {
580         int ret;
581         struct btrfs_key key;
582         struct btrfs_path *path;
583
584         path = btrfs_alloc_path();
585         BUG_ON(!path);
586         key.objectid = start;
587         key.offset = len;
588         btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
589         ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
590                                 0, 0);
591         btrfs_free_path(path);
592         return ret;
593 }
594
595 /*
596  * Back reference rules.  Back refs have three main goals:
597  *
598  * 1) differentiate between all holders of references to an extent so that
599  *    when a reference is dropped we can make sure it was a valid reference
600  *    before freeing the extent.
601  *
602  * 2) Provide enough information to quickly find the holders of an extent
603  *    if we notice a given block is corrupted or bad.
604  *
605  * 3) Make it easy to migrate blocks for FS shrinking or storage pool
606  *    maintenance.  This is actually the same as #2, but with a slightly
607  *    different use case.
608  *
609  * There are two kinds of back refs. The implicit back refs is optimized
610  * for pointers in non-shared tree blocks. For a given pointer in a block,
611  * back refs of this kind provide information about the block's owner tree
612  * and the pointer's key. These information allow us to find the block by
613  * b-tree searching. The full back refs is for pointers in tree blocks not
614  * referenced by their owner trees. The location of tree block is recorded
615  * in the back refs. Actually the full back refs is generic, and can be
616  * used in all cases the implicit back refs is used. The major shortcoming
617  * of the full back refs is its overhead. Every time a tree block gets
618  * COWed, we have to update back refs entry for all pointers in it.
619  *
620  * For a newly allocated tree block, we use implicit back refs for
621  * pointers in it. This means most tree related operations only involve
622  * implicit back refs. For a tree block created in old transaction, the
623  * only way to drop a reference to it is COW it. So we can detect the
624  * event that tree block loses its owner tree's reference and do the
625  * back refs conversion.
626  *
627  * When a tree block is COW'd through a tree, there are four cases:
628  *
629  * The reference count of the block is one and the tree is the block's
630  * owner tree. Nothing to do in this case.
631  *
632  * The reference count of the block is one and the tree is not the
633  * block's owner tree. In this case, full back refs is used for pointers
634  * in the block. Remove these full back refs, add implicit back refs for
635  * every pointers in the new block.
636  *
637  * The reference count of the block is greater than one and the tree is
638  * the block's owner tree. In this case, implicit back refs is used for
639  * pointers in the block. Add full back refs for every pointers in the
640  * block, increase lower level extents' reference counts. The original
641  * implicit back refs are entailed to the new block.
642  *
643  * The reference count of the block is greater than one and the tree is
644  * not the block's owner tree. Add implicit back refs for every pointer in
645  * the new block, increase lower level extents' reference count.
646  *
647  * Back Reference Key composing:
648  *
649  * The key objectid corresponds to the first byte in the extent,
650  * The key type is used to differentiate between types of back refs.
651  * There are different meanings of the key offset for different types
652  * of back refs.
653  *
654  * File extents can be referenced by:
655  *
656  * - multiple snapshots, subvolumes, or different generations in one subvol
657  * - different files inside a single subvolume
658  * - different offsets inside a file (bookend extents in file.c)
659  *
660  * The extent ref structure for the implicit back refs has fields for:
661  *
662  * - Objectid of the subvolume root
663  * - objectid of the file holding the reference
664  * - original offset in the file
665  * - how many bookend extents
666  *
667  * The key offset for the implicit back refs is hash of the first
668  * three fields.
669  *
670  * The extent ref structure for the full back refs has field for:
671  *
672  * - number of pointers in the tree leaf
673  *
674  * The key offset for the implicit back refs is the first byte of
675  * the tree leaf
676  *
677  * When a file extent is allocated, The implicit back refs is used.
678  * the fields are filled in:
679  *
680  *     (root_key.objectid, inode objectid, offset in file, 1)
681  *
682  * When a file extent is removed file truncation, we find the
683  * corresponding implicit back refs and check the following fields:
684  *
685  *     (btrfs_header_owner(leaf), inode objectid, offset in file)
686  *
687  * Btree extents can be referenced by:
688  *
689  * - Different subvolumes
690  *
691  * Both the implicit back refs and the full back refs for tree blocks
692  * only consist of key. The key offset for the implicit back refs is
693  * objectid of block's owner tree. The key offset for the full back refs
694  * is the first byte of parent block.
695  *
696  * When implicit back refs is used, information about the lowest key and
697  * level of the tree block are required. These information are stored in
698  * tree block info structure.
699  */
700
701 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
702 static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
703                                   struct btrfs_root *root,
704                                   struct btrfs_path *path,
705                                   u64 owner, u32 extra_size)
706 {
707         struct btrfs_extent_item *item;
708         struct btrfs_extent_item_v0 *ei0;
709         struct btrfs_extent_ref_v0 *ref0;
710         struct btrfs_tree_block_info *bi;
711         struct extent_buffer *leaf;
712         struct btrfs_key key;
713         struct btrfs_key found_key;
714         u32 new_size = sizeof(*item);
715         u64 refs;
716         int ret;
717
718         leaf = path->nodes[0];
719         BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
720
721         btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
722         ei0 = btrfs_item_ptr(leaf, path->slots[0],
723                              struct btrfs_extent_item_v0);
724         refs = btrfs_extent_refs_v0(leaf, ei0);
725
726         if (owner == (u64)-1) {
727                 while (1) {
728                         if (path->slots[0] >= btrfs_header_nritems(leaf)) {
729                                 ret = btrfs_next_leaf(root, path);
730                                 if (ret < 0)
731                                         return ret;
732                                 BUG_ON(ret > 0);
733                                 leaf = path->nodes[0];
734                         }
735                         btrfs_item_key_to_cpu(leaf, &found_key,
736                                               path->slots[0]);
737                         BUG_ON(key.objectid != found_key.objectid);
738                         if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
739                                 path->slots[0]++;
740                                 continue;
741                         }
742                         ref0 = btrfs_item_ptr(leaf, path->slots[0],
743                                               struct btrfs_extent_ref_v0);
744                         owner = btrfs_ref_objectid_v0(leaf, ref0);
745                         break;
746                 }
747         }
748         btrfs_release_path(root, path);
749
750         if (owner < BTRFS_FIRST_FREE_OBJECTID)
751                 new_size += sizeof(*bi);
752
753         new_size -= sizeof(*ei0);
754         ret = btrfs_search_slot(trans, root, &key, path,
755                                 new_size + extra_size, 1);
756         if (ret < 0)
757                 return ret;
758         BUG_ON(ret);
759
760         ret = btrfs_extend_item(trans, root, path, new_size);
761         BUG_ON(ret);
762
763         leaf = path->nodes[0];
764         item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
765         btrfs_set_extent_refs(leaf, item, refs);
766         /* FIXME: get real generation */
767         btrfs_set_extent_generation(leaf, item, 0);
768         if (owner < BTRFS_FIRST_FREE_OBJECTID) {
769                 btrfs_set_extent_flags(leaf, item,
770                                        BTRFS_EXTENT_FLAG_TREE_BLOCK |
771                                        BTRFS_BLOCK_FLAG_FULL_BACKREF);
772                 bi = (struct btrfs_tree_block_info *)(item + 1);
773                 /* FIXME: get first key of the block */
774                 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
775                 btrfs_set_tree_block_level(leaf, bi, (int)owner);
776         } else {
777                 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
778         }
779         btrfs_mark_buffer_dirty(leaf);
780         return 0;
781 }
782 #endif
783
784 static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
785 {
786         u32 high_crc = ~(u32)0;
787         u32 low_crc = ~(u32)0;
788         __le64 lenum;
789
790         lenum = cpu_to_le64(root_objectid);
791         high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
792         lenum = cpu_to_le64(owner);
793         low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
794         lenum = cpu_to_le64(offset);
795         low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
796
797         return ((u64)high_crc << 31) ^ (u64)low_crc;
798 }
799
800 static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
801                                      struct btrfs_extent_data_ref *ref)
802 {
803         return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
804                                     btrfs_extent_data_ref_objectid(leaf, ref),
805                                     btrfs_extent_data_ref_offset(leaf, ref));
806 }
807
808 static int match_extent_data_ref(struct extent_buffer *leaf,
809                                  struct btrfs_extent_data_ref *ref,
810                                  u64 root_objectid, u64 owner, u64 offset)
811 {
812         if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
813             btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
814             btrfs_extent_data_ref_offset(leaf, ref) != offset)
815                 return 0;
816         return 1;
817 }
818
819 static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
820                                            struct btrfs_root *root,
821                                            struct btrfs_path *path,
822                                            u64 bytenr, u64 parent,
823                                            u64 root_objectid,
824                                            u64 owner, u64 offset)
825 {
826         struct btrfs_key key;
827         struct btrfs_extent_data_ref *ref;
828         struct extent_buffer *leaf;
829         u32 nritems;
830         int ret;
831         int recow;
832         int err = -ENOENT;
833
834         key.objectid = bytenr;
835         if (parent) {
836                 key.type = BTRFS_SHARED_DATA_REF_KEY;
837                 key.offset = parent;
838         } else {
839                 key.type = BTRFS_EXTENT_DATA_REF_KEY;
840                 key.offset = hash_extent_data_ref(root_objectid,
841                                                   owner, offset);
842         }
843 again:
844         recow = 0;
845         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
846         if (ret < 0) {
847                 err = ret;
848                 goto fail;
849         }
850
851         if (parent) {
852                 if (!ret)
853                         return 0;
854 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
855                 key.type = BTRFS_EXTENT_REF_V0_KEY;
856                 btrfs_release_path(root, path);
857                 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
858                 if (ret < 0) {
859                         err = ret;
860                         goto fail;
861                 }
862                 if (!ret)
863                         return 0;
864 #endif
865                 goto fail;
866         }
867
868         leaf = path->nodes[0];
869         nritems = btrfs_header_nritems(leaf);
870         while (1) {
871                 if (path->slots[0] >= nritems) {
872                         ret = btrfs_next_leaf(root, path);
873                         if (ret < 0)
874                                 err = ret;
875                         if (ret)
876                                 goto fail;
877
878                         leaf = path->nodes[0];
879                         nritems = btrfs_header_nritems(leaf);
880                         recow = 1;
881                 }
882
883                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
884                 if (key.objectid != bytenr ||
885                     key.type != BTRFS_EXTENT_DATA_REF_KEY)
886                         goto fail;
887
888                 ref = btrfs_item_ptr(leaf, path->slots[0],
889                                      struct btrfs_extent_data_ref);
890
891                 if (match_extent_data_ref(leaf, ref, root_objectid,
892                                           owner, offset)) {
893                         if (recow) {
894                                 btrfs_release_path(root, path);
895                                 goto again;
896                         }
897                         err = 0;
898                         break;
899                 }
900                 path->slots[0]++;
901         }
902 fail:
903         return err;
904 }
905
906 static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
907                                            struct btrfs_root *root,
908                                            struct btrfs_path *path,
909                                            u64 bytenr, u64 parent,
910                                            u64 root_objectid, u64 owner,
911                                            u64 offset, int refs_to_add)
912 {
913         struct btrfs_key key;
914         struct extent_buffer *leaf;
915         u32 size;
916         u32 num_refs;
917         int ret;
918
919         key.objectid = bytenr;
920         if (parent) {
921                 key.type = BTRFS_SHARED_DATA_REF_KEY;
922                 key.offset = parent;
923                 size = sizeof(struct btrfs_shared_data_ref);
924         } else {
925                 key.type = BTRFS_EXTENT_DATA_REF_KEY;
926                 key.offset = hash_extent_data_ref(root_objectid,
927                                                   owner, offset);
928                 size = sizeof(struct btrfs_extent_data_ref);
929         }
930
931         ret = btrfs_insert_empty_item(trans, root, path, &key, size);
932         if (ret && ret != -EEXIST)
933                 goto fail;
934
935         leaf = path->nodes[0];
936         if (parent) {
937                 struct btrfs_shared_data_ref *ref;
938                 ref = btrfs_item_ptr(leaf, path->slots[0],
939                                      struct btrfs_shared_data_ref);
940                 if (ret == 0) {
941                         btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
942                 } else {
943                         num_refs = btrfs_shared_data_ref_count(leaf, ref);
944                         num_refs += refs_to_add;
945                         btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
946                 }
947         } else {
948                 struct btrfs_extent_data_ref *ref;
949                 while (ret == -EEXIST) {
950                         ref = btrfs_item_ptr(leaf, path->slots[0],
951                                              struct btrfs_extent_data_ref);
952                         if (match_extent_data_ref(leaf, ref, root_objectid,
953                                                   owner, offset))
954                                 break;
955                         btrfs_release_path(root, path);
956                         key.offset++;
957                         ret = btrfs_insert_empty_item(trans, root, path, &key,
958                                                       size);
959                         if (ret && ret != -EEXIST)
960                                 goto fail;
961
962                         leaf = path->nodes[0];
963                 }
964                 ref = btrfs_item_ptr(leaf, path->slots[0],
965                                      struct btrfs_extent_data_ref);
966                 if (ret == 0) {
967                         btrfs_set_extent_data_ref_root(leaf, ref,
968                                                        root_objectid);
969                         btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
970                         btrfs_set_extent_data_ref_offset(leaf, ref, offset);
971                         btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
972                 } else {
973                         num_refs = btrfs_extent_data_ref_count(leaf, ref);
974                         num_refs += refs_to_add;
975                         btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
976                 }
977         }
978         btrfs_mark_buffer_dirty(leaf);
979         ret = 0;
980 fail:
981         btrfs_release_path(root, path);
982         return ret;
983 }
984
985 static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
986                                            struct btrfs_root *root,
987                                            struct btrfs_path *path,
988                                            int refs_to_drop)
989 {
990         struct btrfs_key key;
991         struct btrfs_extent_data_ref *ref1 = NULL;
992         struct btrfs_shared_data_ref *ref2 = NULL;
993         struct extent_buffer *leaf;
994         u32 num_refs = 0;
995         int ret = 0;
996
997         leaf = path->nodes[0];
998         btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
999
1000         if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1001                 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1002                                       struct btrfs_extent_data_ref);
1003                 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1004         } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1005                 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1006                                       struct btrfs_shared_data_ref);
1007                 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1008 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1009         } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1010                 struct btrfs_extent_ref_v0 *ref0;
1011                 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1012                                       struct btrfs_extent_ref_v0);
1013                 num_refs = btrfs_ref_count_v0(leaf, ref0);
1014 #endif
1015         } else {
1016                 BUG();
1017         }
1018
1019         BUG_ON(num_refs < refs_to_drop);
1020         num_refs -= refs_to_drop;
1021
1022         if (num_refs == 0) {
1023                 ret = btrfs_del_item(trans, root, path);
1024         } else {
1025                 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1026                         btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1027                 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1028                         btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1029 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1030                 else {
1031                         struct btrfs_extent_ref_v0 *ref0;
1032                         ref0 = btrfs_item_ptr(leaf, path->slots[0],
1033                                         struct btrfs_extent_ref_v0);
1034                         btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1035                 }
1036 #endif
1037                 btrfs_mark_buffer_dirty(leaf);
1038         }
1039         return ret;
1040 }
1041
1042 static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1043                                           struct btrfs_path *path,
1044                                           struct btrfs_extent_inline_ref *iref)
1045 {
1046         struct btrfs_key key;
1047         struct extent_buffer *leaf;
1048         struct btrfs_extent_data_ref *ref1;
1049         struct btrfs_shared_data_ref *ref2;
1050         u32 num_refs = 0;
1051
1052         leaf = path->nodes[0];
1053         btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1054         if (iref) {
1055                 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1056                     BTRFS_EXTENT_DATA_REF_KEY) {
1057                         ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1058                         num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1059                 } else {
1060                         ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1061                         num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1062                 }
1063         } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1064                 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1065                                       struct btrfs_extent_data_ref);
1066                 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1067         } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1068                 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1069                                       struct btrfs_shared_data_ref);
1070                 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1071 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1072         } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1073                 struct btrfs_extent_ref_v0 *ref0;
1074                 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1075                                       struct btrfs_extent_ref_v0);
1076                 num_refs = btrfs_ref_count_v0(leaf, ref0);
1077 #endif
1078         } else {
1079                 WARN_ON(1);
1080         }
1081         return num_refs;
1082 }
1083
1084 static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1085                                           struct btrfs_root *root,
1086                                           struct btrfs_path *path,
1087                                           u64 bytenr, u64 parent,
1088                                           u64 root_objectid)
1089 {
1090         struct btrfs_key key;
1091         int ret;
1092
1093         key.objectid = bytenr;
1094         if (parent) {
1095                 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1096                 key.offset = parent;
1097         } else {
1098                 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1099                 key.offset = root_objectid;
1100         }
1101
1102         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1103         if (ret > 0)
1104                 ret = -ENOENT;
1105 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1106         if (ret == -ENOENT && parent) {
1107                 btrfs_release_path(root, path);
1108                 key.type = BTRFS_EXTENT_REF_V0_KEY;
1109                 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1110                 if (ret > 0)
1111                         ret = -ENOENT;
1112         }
1113 #endif
1114         return ret;
1115 }
1116
1117 static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1118                                           struct btrfs_root *root,
1119                                           struct btrfs_path *path,
1120                                           u64 bytenr, u64 parent,
1121                                           u64 root_objectid)
1122 {
1123         struct btrfs_key key;
1124         int ret;
1125
1126         key.objectid = bytenr;
1127         if (parent) {
1128                 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1129                 key.offset = parent;
1130         } else {
1131                 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1132                 key.offset = root_objectid;
1133         }
1134
1135         ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1136         btrfs_release_path(root, path);
1137         return ret;
1138 }
1139
1140 static inline int extent_ref_type(u64 parent, u64 owner)
1141 {
1142         int type;
1143         if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1144                 if (parent > 0)
1145                         type = BTRFS_SHARED_BLOCK_REF_KEY;
1146                 else
1147                         type = BTRFS_TREE_BLOCK_REF_KEY;
1148         } else {
1149                 if (parent > 0)
1150                         type = BTRFS_SHARED_DATA_REF_KEY;
1151                 else
1152                         type = BTRFS_EXTENT_DATA_REF_KEY;
1153         }
1154         return type;
1155 }
1156
1157 static int find_next_key(struct btrfs_path *path, int level,
1158                          struct btrfs_key *key)
1159
1160 {
1161         for (; level < BTRFS_MAX_LEVEL; level++) {
1162                 if (!path->nodes[level])
1163                         break;
1164                 if (path->slots[level] + 1 >=
1165                     btrfs_header_nritems(path->nodes[level]))
1166                         continue;
1167                 if (level == 0)
1168                         btrfs_item_key_to_cpu(path->nodes[level], key,
1169                                               path->slots[level] + 1);
1170                 else
1171                         btrfs_node_key_to_cpu(path->nodes[level], key,
1172                                               path->slots[level] + 1);
1173                 return 0;
1174         }
1175         return 1;
1176 }
1177
1178 /*
1179  * look for inline back ref. if back ref is found, *ref_ret is set
1180  * to the address of inline back ref, and 0 is returned.
1181  *
1182  * if back ref isn't found, *ref_ret is set to the address where it
1183  * should be inserted, and -ENOENT is returned.
1184  *
1185  * if insert is true and there are too many inline back refs, the path
1186  * points to the extent item, and -EAGAIN is returned.
1187  *
1188  * NOTE: inline back refs are ordered in the same way that back ref
1189  *       items in the tree are ordered.
1190  */
1191 static noinline_for_stack
1192 int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1193                                  struct btrfs_root *root,
1194                                  struct btrfs_path *path,
1195                                  struct btrfs_extent_inline_ref **ref_ret,
1196                                  u64 bytenr, u64 num_bytes,
1197                                  u64 parent, u64 root_objectid,
1198                                  u64 owner, u64 offset, int insert)
1199 {
1200         struct btrfs_key key;
1201         struct extent_buffer *leaf;
1202         struct btrfs_extent_item *ei;
1203         struct btrfs_extent_inline_ref *iref;
1204         u64 flags;
1205         u64 item_size;
1206         unsigned long ptr;
1207         unsigned long end;
1208         int extra_size;
1209         int type;
1210         int want;
1211         int ret;
1212         int err = 0;
1213
1214         key.objectid = bytenr;
1215         key.type = BTRFS_EXTENT_ITEM_KEY;
1216         key.offset = num_bytes;
1217
1218         want = extent_ref_type(parent, owner);
1219         if (insert) {
1220                 extra_size = btrfs_extent_inline_ref_size(want);
1221                 path->keep_locks = 1;
1222         } else
1223                 extra_size = -1;
1224         ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1225         if (ret < 0) {
1226                 err = ret;
1227                 goto out;
1228         }
1229         BUG_ON(ret);
1230
1231         leaf = path->nodes[0];
1232         item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1233 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1234         if (item_size < sizeof(*ei)) {
1235                 if (!insert) {
1236                         err = -ENOENT;
1237                         goto out;
1238                 }
1239                 ret = convert_extent_item_v0(trans, root, path, owner,
1240                                              extra_size);
1241                 if (ret < 0) {
1242                         err = ret;
1243                         goto out;
1244                 }
1245                 leaf = path->nodes[0];
1246                 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1247         }
1248 #endif
1249         BUG_ON(item_size < sizeof(*ei));
1250
1251         ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1252         flags = btrfs_extent_flags(leaf, ei);
1253
1254         ptr = (unsigned long)(ei + 1);
1255         end = (unsigned long)ei + item_size;
1256
1257         if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1258                 ptr += sizeof(struct btrfs_tree_block_info);
1259                 BUG_ON(ptr > end);
1260         } else {
1261                 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1262         }
1263
1264         err = -ENOENT;
1265         while (1) {
1266                 if (ptr >= end) {
1267                         WARN_ON(ptr > end);
1268                         break;
1269                 }
1270                 iref = (struct btrfs_extent_inline_ref *)ptr;
1271                 type = btrfs_extent_inline_ref_type(leaf, iref);
1272                 if (want < type)
1273                         break;
1274                 if (want > type) {
1275                         ptr += btrfs_extent_inline_ref_size(type);
1276                         continue;
1277                 }
1278
1279                 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1280                         struct btrfs_extent_data_ref *dref;
1281                         dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1282                         if (match_extent_data_ref(leaf, dref, root_objectid,
1283                                                   owner, offset)) {
1284                                 err = 0;
1285                                 break;
1286                         }
1287                         if (hash_extent_data_ref_item(leaf, dref) <
1288                             hash_extent_data_ref(root_objectid, owner, offset))
1289                                 break;
1290                 } else {
1291                         u64 ref_offset;
1292                         ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1293                         if (parent > 0) {
1294                                 if (parent == ref_offset) {
1295                                         err = 0;
1296                                         break;
1297                                 }
1298                                 if (ref_offset < parent)
1299                                         break;
1300                         } else {
1301                                 if (root_objectid == ref_offset) {
1302                                         err = 0;
1303                                         break;
1304                                 }
1305                                 if (ref_offset < root_objectid)
1306                                         break;
1307                         }
1308                 }
1309                 ptr += btrfs_extent_inline_ref_size(type);
1310         }
1311         if (err == -ENOENT && insert) {
1312                 if (item_size + extra_size >=
1313                     BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1314                         err = -EAGAIN;
1315                         goto out;
1316                 }
1317                 /*
1318                  * To add new inline back ref, we have to make sure
1319                  * there is no corresponding back ref item.
1320                  * For simplicity, we just do not add new inline back
1321                  * ref if there is any kind of item for this block
1322                  */
1323                 if (find_next_key(path, 0, &key) == 0 &&
1324                     key.objectid == bytenr &&
1325                     key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
1326                         err = -EAGAIN;
1327                         goto out;
1328                 }
1329         }
1330         *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1331 out:
1332         if (insert) {
1333                 path->keep_locks = 0;
1334                 btrfs_unlock_up_safe(path, 1);
1335         }
1336         return err;
1337 }
1338
1339 /*
1340  * helper to add new inline back ref
1341  */
1342 static noinline_for_stack
1343 int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1344                                 struct btrfs_root *root,
1345                                 struct btrfs_path *path,
1346                                 struct btrfs_extent_inline_ref *iref,
1347                                 u64 parent, u64 root_objectid,
1348                                 u64 owner, u64 offset, int refs_to_add,
1349                                 struct btrfs_delayed_extent_op *extent_op)
1350 {
1351         struct extent_buffer *leaf;
1352         struct btrfs_extent_item *ei;
1353         unsigned long ptr;
1354         unsigned long end;
1355         unsigned long item_offset;
1356         u64 refs;
1357         int size;
1358         int type;
1359         int ret;
1360
1361         leaf = path->nodes[0];
1362         ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1363         item_offset = (unsigned long)iref - (unsigned long)ei;
1364
1365         type = extent_ref_type(parent, owner);
1366         size = btrfs_extent_inline_ref_size(type);
1367
1368         ret = btrfs_extend_item(trans, root, path, size);
1369         BUG_ON(ret);
1370
1371         ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1372         refs = btrfs_extent_refs(leaf, ei);
1373         refs += refs_to_add;
1374         btrfs_set_extent_refs(leaf, ei, refs);
1375         if (extent_op)
1376                 __run_delayed_extent_op(extent_op, leaf, ei);
1377
1378         ptr = (unsigned long)ei + item_offset;
1379         end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1380         if (ptr < end - size)
1381                 memmove_extent_buffer(leaf, ptr + size, ptr,
1382                                       end - size - ptr);
1383
1384         iref = (struct btrfs_extent_inline_ref *)ptr;
1385         btrfs_set_extent_inline_ref_type(leaf, iref, type);
1386         if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1387                 struct btrfs_extent_data_ref *dref;
1388                 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1389                 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1390                 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1391                 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1392                 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1393         } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1394                 struct btrfs_shared_data_ref *sref;
1395                 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1396                 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1397                 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1398         } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1399                 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1400         } else {
1401                 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1402         }
1403         btrfs_mark_buffer_dirty(leaf);
1404         return 0;
1405 }
1406
1407 static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1408                                  struct btrfs_root *root,
1409                                  struct btrfs_path *path,
1410                                  struct btrfs_extent_inline_ref **ref_ret,
1411                                  u64 bytenr, u64 num_bytes, u64 parent,
1412                                  u64 root_objectid, u64 owner, u64 offset)
1413 {
1414         int ret;
1415
1416         ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1417                                            bytenr, num_bytes, parent,
1418                                            root_objectid, owner, offset, 0);
1419         if (ret != -ENOENT)
1420                 return ret;
1421
1422         btrfs_release_path(root, path);
1423         *ref_ret = NULL;
1424
1425         if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1426                 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1427                                             root_objectid);
1428         } else {
1429                 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1430                                              root_objectid, owner, offset);
1431         }
1432         return ret;
1433 }
1434
1435 /*
1436  * helper to update/remove inline back ref
1437  */
1438 static noinline_for_stack
1439 int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1440                                  struct btrfs_root *root,
1441                                  struct btrfs_path *path,
1442                                  struct btrfs_extent_inline_ref *iref,
1443                                  int refs_to_mod,
1444                                  struct btrfs_delayed_extent_op *extent_op)
1445 {
1446         struct extent_buffer *leaf;
1447         struct btrfs_extent_item *ei;
1448         struct btrfs_extent_data_ref *dref = NULL;
1449         struct btrfs_shared_data_ref *sref = NULL;
1450         unsigned long ptr;
1451         unsigned long end;
1452         u32 item_size;
1453         int size;
1454         int type;
1455         int ret;
1456         u64 refs;
1457
1458         leaf = path->nodes[0];
1459         ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1460         refs = btrfs_extent_refs(leaf, ei);
1461         WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1462         refs += refs_to_mod;
1463         btrfs_set_extent_refs(leaf, ei, refs);
1464         if (extent_op)
1465                 __run_delayed_extent_op(extent_op, leaf, ei);
1466
1467         type = btrfs_extent_inline_ref_type(leaf, iref);
1468
1469         if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1470                 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1471                 refs = btrfs_extent_data_ref_count(leaf, dref);
1472         } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1473                 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1474                 refs = btrfs_shared_data_ref_count(leaf, sref);
1475         } else {
1476                 refs = 1;
1477                 BUG_ON(refs_to_mod != -1);
1478         }
1479
1480         BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1481         refs += refs_to_mod;
1482
1483         if (refs > 0) {
1484                 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1485                         btrfs_set_extent_data_ref_count(leaf, dref, refs);
1486                 else
1487                         btrfs_set_shared_data_ref_count(leaf, sref, refs);
1488         } else {
1489                 size =  btrfs_extent_inline_ref_size(type);
1490                 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1491                 ptr = (unsigned long)iref;
1492                 end = (unsigned long)ei + item_size;
1493                 if (ptr + size < end)
1494                         memmove_extent_buffer(leaf, ptr, ptr + size,
1495                                               end - ptr - size);
1496                 item_size -= size;
1497                 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1498                 BUG_ON(ret);
1499         }
1500         btrfs_mark_buffer_dirty(leaf);
1501         return 0;
1502 }
1503
1504 static noinline_for_stack
1505 int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1506                                  struct btrfs_root *root,
1507                                  struct btrfs_path *path,
1508                                  u64 bytenr, u64 num_bytes, u64 parent,
1509                                  u64 root_objectid, u64 owner,
1510                                  u64 offset, int refs_to_add,
1511                                  struct btrfs_delayed_extent_op *extent_op)
1512 {
1513         struct btrfs_extent_inline_ref *iref;
1514         int ret;
1515
1516         ret = lookup_inline_extent_backref(trans, root, path, &iref,
1517                                            bytenr, num_bytes, parent,
1518                                            root_objectid, owner, offset, 1);
1519         if (ret == 0) {
1520                 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1521                 ret = update_inline_extent_backref(trans, root, path, iref,
1522                                                    refs_to_add, extent_op);
1523         } else if (ret == -ENOENT) {
1524                 ret = setup_inline_extent_backref(trans, root, path, iref,
1525                                                   parent, root_objectid,
1526                                                   owner, offset, refs_to_add,
1527                                                   extent_op);
1528         }
1529         return ret;
1530 }
1531
1532 static int insert_extent_backref(struct btrfs_trans_handle *trans,
1533                                  struct btrfs_root *root,
1534                                  struct btrfs_path *path,
1535                                  u64 bytenr, u64 parent, u64 root_objectid,
1536                                  u64 owner, u64 offset, int refs_to_add)
1537 {
1538         int ret;
1539         if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1540                 BUG_ON(refs_to_add != 1);
1541                 ret = insert_tree_block_ref(trans, root, path, bytenr,
1542                                             parent, root_objectid);
1543         } else {
1544                 ret = insert_extent_data_ref(trans, root, path, bytenr,
1545                                              parent, root_objectid,
1546                                              owner, offset, refs_to_add);
1547         }
1548         return ret;
1549 }
1550
1551 static int remove_extent_backref(struct btrfs_trans_handle *trans,
1552                                  struct btrfs_root *root,
1553                                  struct btrfs_path *path,
1554                                  struct btrfs_extent_inline_ref *iref,
1555                                  int refs_to_drop, int is_data)
1556 {
1557         int ret;
1558
1559         BUG_ON(!is_data && refs_to_drop != 1);
1560         if (iref) {
1561                 ret = update_inline_extent_backref(trans, root, path, iref,
1562                                                    -refs_to_drop, NULL);
1563         } else if (is_data) {
1564                 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1565         } else {
1566                 ret = btrfs_del_item(trans, root, path);
1567         }
1568         return ret;
1569 }
1570
1571 static void btrfs_issue_discard(struct block_device *bdev,
1572                                 u64 start, u64 len)
1573 {
1574         blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL,
1575                              DISCARD_FL_BARRIER);
1576 }
1577
1578 static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1579                                 u64 num_bytes)
1580 {
1581         int ret;
1582         u64 map_length = num_bytes;
1583         struct btrfs_multi_bio *multi = NULL;
1584
1585         if (!btrfs_test_opt(root, DISCARD))
1586                 return 0;
1587
1588         /* Tell the block device(s) that the sectors can be discarded */
1589         ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1590                               bytenr, &map_length, &multi, 0);
1591         if (!ret) {
1592                 struct btrfs_bio_stripe *stripe = multi->stripes;
1593                 int i;
1594
1595                 if (map_length > num_bytes)
1596                         map_length = num_bytes;
1597
1598                 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1599                         btrfs_issue_discard(stripe->dev->bdev,
1600                                             stripe->physical,
1601                                             map_length);
1602                 }
1603                 kfree(multi);
1604         }
1605
1606         return ret;
1607 }
1608
1609 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1610                          struct btrfs_root *root,
1611                          u64 bytenr, u64 num_bytes, u64 parent,
1612                          u64 root_objectid, u64 owner, u64 offset)
1613 {
1614         int ret;
1615         BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1616                root_objectid == BTRFS_TREE_LOG_OBJECTID);
1617
1618         if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1619                 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1620                                         parent, root_objectid, (int)owner,
1621                                         BTRFS_ADD_DELAYED_REF, NULL);
1622         } else {
1623                 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1624                                         parent, root_objectid, owner, offset,
1625                                         BTRFS_ADD_DELAYED_REF, NULL);
1626         }
1627         return ret;
1628 }
1629
1630 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1631                                   struct btrfs_root *root,
1632                                   u64 bytenr, u64 num_bytes,
1633                                   u64 parent, u64 root_objectid,
1634                                   u64 owner, u64 offset, int refs_to_add,
1635                                   struct btrfs_delayed_extent_op *extent_op)
1636 {
1637         struct btrfs_path *path;
1638         struct extent_buffer *leaf;
1639         struct btrfs_extent_item *item;
1640         u64 refs;
1641         int ret;
1642         int err = 0;
1643
1644         path = btrfs_alloc_path();
1645         if (!path)
1646                 return -ENOMEM;
1647
1648         path->reada = 1;
1649         path->leave_spinning = 1;
1650         /* this will setup the path even if it fails to insert the back ref */
1651         ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1652                                            path, bytenr, num_bytes, parent,
1653                                            root_objectid, owner, offset,
1654                                            refs_to_add, extent_op);
1655         if (ret == 0)
1656                 goto out;
1657
1658         if (ret != -EAGAIN) {
1659                 err = ret;
1660                 goto out;
1661         }
1662
1663         leaf = path->nodes[0];
1664         item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1665         refs = btrfs_extent_refs(leaf, item);
1666         btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1667         if (extent_op)
1668                 __run_delayed_extent_op(extent_op, leaf, item);
1669
1670         btrfs_mark_buffer_dirty(leaf);
1671         btrfs_release_path(root->fs_info->extent_root, path);
1672
1673         path->reada = 1;
1674         path->leave_spinning = 1;
1675
1676         /* now insert the actual backref */
1677         ret = insert_extent_backref(trans, root->fs_info->extent_root,
1678                                     path, bytenr, parent, root_objectid,
1679                                     owner, offset, refs_to_add);
1680         BUG_ON(ret);
1681 out:
1682         btrfs_free_path(path);
1683         return err;
1684 }
1685
1686 static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1687                                 struct btrfs_root *root,
1688                                 struct btrfs_delayed_ref_node *node,
1689                                 struct btrfs_delayed_extent_op *extent_op,
1690                                 int insert_reserved)
1691 {
1692         int ret = 0;
1693         struct btrfs_delayed_data_ref *ref;
1694         struct btrfs_key ins;
1695         u64 parent = 0;
1696         u64 ref_root = 0;
1697         u64 flags = 0;
1698
1699         ins.objectid = node->bytenr;
1700         ins.offset = node->num_bytes;
1701         ins.type = BTRFS_EXTENT_ITEM_KEY;
1702
1703         ref = btrfs_delayed_node_to_data_ref(node);
1704         if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1705                 parent = ref->parent;
1706         else
1707                 ref_root = ref->root;
1708
1709         if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1710                 if (extent_op) {
1711                         BUG_ON(extent_op->update_key);
1712                         flags |= extent_op->flags_to_set;
1713                 }
1714                 ret = alloc_reserved_file_extent(trans, root,
1715                                                  parent, ref_root, flags,
1716                                                  ref->objectid, ref->offset,
1717                                                  &ins, node->ref_mod);
1718         } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1719                 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1720                                              node->num_bytes, parent,
1721                                              ref_root, ref->objectid,
1722                                              ref->offset, node->ref_mod,
1723                                              extent_op);
1724         } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1725                 ret = __btrfs_free_extent(trans, root, node->bytenr,
1726                                           node->num_bytes, parent,
1727                                           ref_root, ref->objectid,
1728                                           ref->offset, node->ref_mod,
1729                                           extent_op);
1730         } else {
1731                 BUG();
1732         }
1733         return ret;
1734 }
1735
1736 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1737                                     struct extent_buffer *leaf,
1738                                     struct btrfs_extent_item *ei)
1739 {
1740         u64 flags = btrfs_extent_flags(leaf, ei);
1741         if (extent_op->update_flags) {
1742                 flags |= extent_op->flags_to_set;
1743                 btrfs_set_extent_flags(leaf, ei, flags);
1744         }
1745
1746         if (extent_op->update_key) {
1747                 struct btrfs_tree_block_info *bi;
1748                 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1749                 bi = (struct btrfs_tree_block_info *)(ei + 1);
1750                 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1751         }
1752 }
1753
1754 static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1755                                  struct btrfs_root *root,
1756                                  struct btrfs_delayed_ref_node *node,
1757                                  struct btrfs_delayed_extent_op *extent_op)
1758 {
1759         struct btrfs_key key;
1760         struct btrfs_path *path;
1761         struct btrfs_extent_item *ei;
1762         struct extent_buffer *leaf;
1763         u32 item_size;
1764         int ret;
1765         int err = 0;
1766
1767         path = btrfs_alloc_path();
1768         if (!path)
1769                 return -ENOMEM;
1770
1771         key.objectid = node->bytenr;
1772         key.type = BTRFS_EXTENT_ITEM_KEY;
1773         key.offset = node->num_bytes;
1774
1775         path->reada = 1;
1776         path->leave_spinning = 1;
1777         ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1778                                 path, 0, 1);
1779         if (ret < 0) {
1780                 err = ret;
1781                 goto out;
1782         }
1783         if (ret > 0) {
1784                 err = -EIO;
1785                 goto out;
1786         }
1787
1788         leaf = path->nodes[0];
1789         item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1790 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1791         if (item_size < sizeof(*ei)) {
1792                 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1793                                              path, (u64)-1, 0);
1794                 if (ret < 0) {
1795                         err = ret;
1796                         goto out;
1797                 }
1798                 leaf = path->nodes[0];
1799                 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1800         }
1801 #endif
1802         BUG_ON(item_size < sizeof(*ei));
1803         ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1804         __run_delayed_extent_op(extent_op, leaf, ei);
1805
1806         btrfs_mark_buffer_dirty(leaf);
1807 out:
1808         btrfs_free_path(path);
1809         return err;
1810 }
1811
1812 static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
1813                                 struct btrfs_root *root,
1814                                 struct btrfs_delayed_ref_node *node,
1815                                 struct btrfs_delayed_extent_op *extent_op,
1816                                 int insert_reserved)
1817 {
1818         int ret = 0;
1819         struct btrfs_delayed_tree_ref *ref;
1820         struct btrfs_key ins;
1821         u64 parent = 0;
1822         u64 ref_root = 0;
1823
1824         ins.objectid = node->bytenr;
1825         ins.offset = node->num_bytes;
1826         ins.type = BTRFS_EXTENT_ITEM_KEY;
1827
1828         ref = btrfs_delayed_node_to_tree_ref(node);
1829         if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1830                 parent = ref->parent;
1831         else
1832                 ref_root = ref->root;
1833
1834         BUG_ON(node->ref_mod != 1);
1835         if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1836                 BUG_ON(!extent_op || !extent_op->update_flags ||
1837                        !extent_op->update_key);
1838                 ret = alloc_reserved_tree_block(trans, root,
1839                                                 parent, ref_root,
1840                                                 extent_op->flags_to_set,
1841                                                 &extent_op->key,
1842                                                 ref->level, &ins);
1843         } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1844                 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1845                                              node->num_bytes, parent, ref_root,
1846                                              ref->level, 0, 1, extent_op);
1847         } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1848                 ret = __btrfs_free_extent(trans, root, node->bytenr,
1849                                           node->num_bytes, parent, ref_root,
1850                                           ref->level, 0, 1, extent_op);
1851         } else {
1852                 BUG();
1853         }
1854         return ret;
1855 }
1856
1857
1858 /* helper function to actually process a single delayed ref entry */
1859 static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
1860                                struct btrfs_root *root,
1861                                struct btrfs_delayed_ref_node *node,
1862                                struct btrfs_delayed_extent_op *extent_op,
1863                                int insert_reserved)
1864 {
1865         int ret;
1866         if (btrfs_delayed_ref_is_head(node)) {
1867                 struct btrfs_delayed_ref_head *head;
1868                 /*
1869                  * we've hit the end of the chain and we were supposed
1870                  * to insert this extent into the tree.  But, it got
1871                  * deleted before we ever needed to insert it, so all
1872                  * we have to do is clean up the accounting
1873                  */
1874                 BUG_ON(extent_op);
1875                 head = btrfs_delayed_node_to_head(node);
1876                 if (insert_reserved) {
1877                         int mark_free = 0;
1878                         struct extent_buffer *must_clean = NULL;
1879
1880                         ret = pin_down_bytes(trans, root, NULL,
1881                                              node->bytenr, node->num_bytes,
1882                                              head->is_data, 1, &must_clean);
1883                         if (ret > 0)
1884                                 mark_free = 1;
1885
1886                         if (must_clean) {
1887                                 clean_tree_block(NULL, root, must_clean);
1888                                 btrfs_tree_unlock(must_clean);
1889                                 free_extent_buffer(must_clean);
1890                         }
1891                         if (head->is_data) {
1892                                 ret = btrfs_del_csums(trans, root,
1893                                                       node->bytenr,
1894                                                       node->num_bytes);
1895                                 BUG_ON(ret);
1896                         }
1897                         if (mark_free) {
1898                                 ret = btrfs_free_reserved_extent(root,
1899                                                         node->bytenr,
1900                                                         node->num_bytes);
1901                                 BUG_ON(ret);
1902                         }
1903                 }
1904                 mutex_unlock(&head->mutex);
1905                 return 0;
1906         }
1907
1908         if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1909             node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1910                 ret = run_delayed_tree_ref(trans, root, node, extent_op,
1911                                            insert_reserved);
1912         else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1913                  node->type == BTRFS_SHARED_DATA_REF_KEY)
1914                 ret = run_delayed_data_ref(trans, root, node, extent_op,
1915                                            insert_reserved);
1916         else
1917                 BUG();
1918         return ret;
1919 }
1920
1921 static noinline struct btrfs_delayed_ref_node *
1922 select_delayed_ref(struct btrfs_delayed_ref_head *head)
1923 {
1924         struct rb_node *node;
1925         struct btrfs_delayed_ref_node *ref;
1926         int action = BTRFS_ADD_DELAYED_REF;
1927 again:
1928         /*
1929          * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
1930          * this prevents ref count from going down to zero when
1931          * there still are pending delayed ref.
1932          */
1933         node = rb_prev(&head->node.rb_node);
1934         while (1) {
1935                 if (!node)
1936                         break;
1937                 ref = rb_entry(node, struct btrfs_delayed_ref_node,
1938                                 rb_node);
1939                 if (ref->bytenr != head->node.bytenr)
1940                         break;
1941                 if (ref->action == action)
1942                         return ref;
1943                 node = rb_prev(node);
1944         }
1945         if (action == BTRFS_ADD_DELAYED_REF) {
1946                 action = BTRFS_DROP_DELAYED_REF;
1947                 goto again;
1948         }
1949         return NULL;
1950 }
1951
1952 static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
1953                                        struct btrfs_root *root,
1954                                        struct list_head *cluster)
1955 {
1956         struct btrfs_delayed_ref_root *delayed_refs;
1957         struct btrfs_delayed_ref_node *ref;
1958         struct btrfs_delayed_ref_head *locked_ref = NULL;
1959         struct btrfs_delayed_extent_op *extent_op;
1960         int ret;
1961         int count = 0;
1962         int must_insert_reserved = 0;
1963
1964         delayed_refs = &trans->transaction->delayed_refs;
1965         while (1) {
1966                 if (!locked_ref) {
1967                         /* pick a new head ref from the cluster list */
1968                         if (list_empty(cluster))
1969                                 break;
1970
1971                         locked_ref = list_entry(cluster->next,
1972                                      struct btrfs_delayed_ref_head, cluster);
1973
1974                         /* grab the lock that says we are going to process
1975                          * all the refs for this head */
1976                         ret = btrfs_delayed_ref_lock(trans, locked_ref);
1977
1978                         /*
1979                          * we may have dropped the spin lock to get the head
1980                          * mutex lock, and that might have given someone else
1981                          * time to free the head.  If that's true, it has been
1982                          * removed from our list and we can move on.
1983                          */
1984                         if (ret == -EAGAIN) {
1985                                 locked_ref = NULL;
1986                                 count++;
1987                                 continue;
1988                         }
1989                 }
1990
1991                 /*
1992                  * record the must insert reserved flag before we
1993                  * drop the spin lock.
1994                  */
1995                 must_insert_reserved = locked_ref->must_insert_reserved;
1996                 locked_ref->must_insert_reserved = 0;
1997
1998                 extent_op = locked_ref->extent_op;
1999                 locked_ref->extent_op = NULL;
2000
2001                 /*
2002                  * locked_ref is the head node, so we have to go one
2003                  * node back for any delayed ref updates
2004                  */
2005                 ref = select_delayed_ref(locked_ref);
2006                 if (!ref) {
2007                         /* All delayed refs have been processed, Go ahead
2008                          * and send the head node to run_one_delayed_ref,
2009                          * so that any accounting fixes can happen
2010                          */
2011                         ref = &locked_ref->node;
2012
2013                         if (extent_op && must_insert_reserved) {
2014                                 kfree(extent_op);
2015                                 extent_op = NULL;
2016                         }
2017
2018                         if (extent_op) {
2019                                 spin_unlock(&delayed_refs->lock);
2020
2021                                 ret = run_delayed_extent_op(trans, root,
2022                                                             ref, extent_op);
2023                                 BUG_ON(ret);
2024                                 kfree(extent_op);
2025
2026                                 cond_resched();
2027                                 spin_lock(&delayed_refs->lock);
2028                                 continue;
2029                         }
2030
2031                         list_del_init(&locked_ref->cluster);
2032                         locked_ref = NULL;
2033                 }
2034
2035                 ref->in_tree = 0;
2036                 rb_erase(&ref->rb_node, &delayed_refs->root);
2037                 delayed_refs->num_entries--;
2038
2039                 spin_unlock(&delayed_refs->lock);
2040
2041                 ret = run_one_delayed_ref(trans, root, ref, extent_op,
2042                                           must_insert_reserved);
2043                 BUG_ON(ret);
2044
2045                 btrfs_put_delayed_ref(ref);
2046                 kfree(extent_op);
2047                 count++;
2048
2049                 cond_resched();
2050                 spin_lock(&delayed_refs->lock);
2051         }
2052         return count;
2053 }
2054
2055 /*
2056  * this starts processing the delayed reference count updates and
2057  * extent insertions we have queued up so far.  count can be
2058  * 0, which means to process everything in the tree at the start
2059  * of the run (but not newly added entries), or it can be some target
2060  * number you'd like to process.
2061  */
2062 int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2063                            struct btrfs_root *root, unsigned long count)
2064 {
2065         struct rb_node *node;
2066         struct btrfs_delayed_ref_root *delayed_refs;
2067         struct btrfs_delayed_ref_node *ref;
2068         struct list_head cluster;
2069         int ret;
2070         int run_all = count == (unsigned long)-1;
2071         int run_most = 0;
2072
2073         if (root == root->fs_info->extent_root)
2074                 root = root->fs_info->tree_root;
2075
2076         delayed_refs = &trans->transaction->delayed_refs;
2077         INIT_LIST_HEAD(&cluster);
2078 again:
2079         spin_lock(&delayed_refs->lock);
2080         if (count == 0) {
2081                 count = delayed_refs->num_entries * 2;
2082                 run_most = 1;
2083         }
2084         while (1) {
2085                 if (!(run_all || run_most) &&
2086                     delayed_refs->num_heads_ready < 64)
2087                         break;
2088
2089                 /*
2090                  * go find something we can process in the rbtree.  We start at
2091                  * the beginning of the tree, and then build a cluster
2092                  * of refs to process starting at the first one we are able to
2093                  * lock
2094                  */
2095                 ret = btrfs_find_ref_cluster(trans, &cluster,
2096                                              delayed_refs->run_delayed_start);
2097                 if (ret)
2098                         break;
2099
2100                 ret = run_clustered_refs(trans, root, &cluster);
2101                 BUG_ON(ret < 0);
2102
2103                 count -= min_t(unsigned long, ret, count);
2104
2105                 if (count == 0)
2106                         break;
2107         }
2108
2109         if (run_all) {
2110                 node = rb_first(&delayed_refs->root);
2111                 if (!node)
2112                         goto out;
2113                 count = (unsigned long)-1;
2114
2115                 while (node) {
2116                         ref = rb_entry(node, struct btrfs_delayed_ref_node,
2117                                        rb_node);
2118                         if (btrfs_delayed_ref_is_head(ref)) {
2119                                 struct btrfs_delayed_ref_head *head;
2120
2121                                 head = btrfs_delayed_node_to_head(ref);
2122                                 atomic_inc(&ref->refs);
2123
2124                                 spin_unlock(&delayed_refs->lock);
2125                                 mutex_lock(&head->mutex);
2126                                 mutex_unlock(&head->mutex);
2127
2128                                 btrfs_put_delayed_ref(ref);
2129                                 cond_resched();
2130                                 goto again;
2131                         }
2132                         node = rb_next(node);
2133                 }
2134                 spin_unlock(&delayed_refs->lock);
2135                 schedule_timeout(1);
2136                 goto again;
2137         }
2138 out:
2139         spin_unlock(&delayed_refs->lock);
2140         return 0;
2141 }
2142
2143 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2144                                 struct btrfs_root *root,
2145                                 u64 bytenr, u64 num_bytes, u64 flags,
2146                                 int is_data)
2147 {
2148         struct btrfs_delayed_extent_op *extent_op;
2149         int ret;
2150
2151         extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2152         if (!extent_op)
2153                 return -ENOMEM;
2154
2155         extent_op->flags_to_set = flags;
2156         extent_op->update_flags = 1;
2157         extent_op->update_key = 0;
2158         extent_op->is_data = is_data ? 1 : 0;
2159
2160         ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2161         if (ret)
2162                 kfree(extent_op);
2163         return ret;
2164 }
2165
2166 static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2167                                       struct btrfs_root *root,
2168                                       struct btrfs_path *path,
2169                                       u64 objectid, u64 offset, u64 bytenr)
2170 {
2171         struct btrfs_delayed_ref_head *head;
2172         struct btrfs_delayed_ref_node *ref;
2173         struct btrfs_delayed_data_ref *data_ref;
2174         struct btrfs_delayed_ref_root *delayed_refs;
2175         struct rb_node *node;
2176         int ret = 0;
2177
2178         ret = -ENOENT;
2179         delayed_refs = &trans->transaction->delayed_refs;
2180         spin_lock(&delayed_refs->lock);
2181         head = btrfs_find_delayed_ref_head(trans, bytenr);
2182         if (!head)
2183                 goto out;
2184
2185         if (!mutex_trylock(&head->mutex)) {
2186                 atomic_inc(&head->node.refs);
2187                 spin_unlock(&delayed_refs->lock);
2188
2189                 btrfs_release_path(root->fs_info->extent_root, path);
2190
2191                 mutex_lock(&head->mutex);
2192                 mutex_unlock(&head->mutex);
2193                 btrfs_put_delayed_ref(&head->node);
2194                 return -EAGAIN;
2195         }
2196
2197         node = rb_prev(&head->node.rb_node);
2198         if (!node)
2199                 goto out_unlock;
2200
2201         ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2202
2203         if (ref->bytenr != bytenr)
2204                 goto out_unlock;
2205
2206         ret = 1;
2207         if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2208                 goto out_unlock;
2209
2210         data_ref = btrfs_delayed_node_to_data_ref(ref);
2211
2212         node = rb_prev(node);
2213         if (node) {
2214                 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2215                 if (ref->bytenr == bytenr)
2216                         goto out_unlock;
2217         }
2218
2219         if (data_ref->root != root->root_key.objectid ||
2220             data_ref->objectid != objectid || data_ref->offset != offset)
2221                 goto out_unlock;
2222
2223         ret = 0;
2224 out_unlock:
2225         mutex_unlock(&head->mutex);
2226 out:
2227         spin_unlock(&delayed_refs->lock);
2228         return ret;
2229 }
2230
2231 static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2232                                         struct btrfs_root *root,
2233                                         struct btrfs_path *path,
2234                                         u64 objectid, u64 offset, u64 bytenr)
2235 {
2236         struct btrfs_root *extent_root = root->fs_info->extent_root;
2237         struct extent_buffer *leaf;
2238         struct btrfs_extent_data_ref *ref;
2239         struct btrfs_extent_inline_ref *iref;
2240         struct btrfs_extent_item *ei;
2241         struct btrfs_key key;
2242         u32 item_size;
2243         int ret;
2244
2245         key.objectid = bytenr;
2246         key.offset = (u64)-1;
2247         key.type = BTRFS_EXTENT_ITEM_KEY;
2248
2249         ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2250         if (ret < 0)
2251                 goto out;
2252         BUG_ON(ret == 0);
2253
2254         ret = -ENOENT;
2255         if (path->slots[0] == 0)
2256                 goto out;
2257
2258         path->slots[0]--;
2259         leaf = path->nodes[0];
2260         btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2261
2262         if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
2263                 goto out;
2264
2265         ret = 1;
2266         item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2267 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2268         if (item_size < sizeof(*ei)) {
2269                 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2270                 goto out;
2271         }
2272 #endif
2273         ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2274
2275         if (item_size != sizeof(*ei) +
2276             btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2277                 goto out;
2278
2279         if (btrfs_extent_generation(leaf, ei) <=
2280             btrfs_root_last_snapshot(&root->root_item))
2281                 goto out;
2282
2283         iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2284         if (btrfs_extent_inline_ref_type(leaf, iref) !=
2285             BTRFS_EXTENT_DATA_REF_KEY)
2286                 goto out;
2287
2288         ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2289         if (btrfs_extent_refs(leaf, ei) !=
2290             btrfs_extent_data_ref_count(leaf, ref) ||
2291             btrfs_extent_data_ref_root(leaf, ref) !=
2292             root->root_key.objectid ||
2293             btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2294             btrfs_extent_data_ref_offset(leaf, ref) != offset)
2295                 goto out;
2296
2297         ret = 0;
2298 out:
2299         return ret;
2300 }
2301
2302 int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2303                           struct btrfs_root *root,
2304                           u64 objectid, u64 offset, u64 bytenr)
2305 {
2306         struct btrfs_path *path;
2307         int ret;
2308         int ret2;
2309
2310         path = btrfs_alloc_path();
2311         if (!path)
2312                 return -ENOENT;
2313
2314         do {
2315                 ret = check_committed_ref(trans, root, path, objectid,
2316                                           offset, bytenr);
2317                 if (ret && ret != -ENOENT)
2318                         goto out;
2319
2320                 ret2 = check_delayed_ref(trans, root, path, objectid,
2321                                          offset, bytenr);
2322         } while (ret2 == -EAGAIN);
2323
2324         if (ret2 && ret2 != -ENOENT) {
2325                 ret = ret2;
2326                 goto out;
2327         }
2328
2329         if (ret != -ENOENT || ret2 != -ENOENT)
2330                 ret = 0;
2331 out:
2332         btrfs_free_path(path);
2333         return ret;
2334 }
2335
2336 #if 0
2337 int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2338                     struct extent_buffer *buf, u32 nr_extents)
2339 {
2340         struct btrfs_key key;
2341         struct btrfs_file_extent_item *fi;
2342         u64 root_gen;
2343         u32 nritems;
2344         int i;
2345         int level;
2346         int ret = 0;
2347         int shared = 0;
2348
2349         if (!root->ref_cows)
2350                 return 0;
2351
2352         if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2353                 shared = 0;
2354                 root_gen = root->root_key.offset;
2355         } else {
2356                 shared = 1;
2357                 root_gen = trans->transid - 1;
2358         }
2359
2360         level = btrfs_header_level(buf);
2361         nritems = btrfs_header_nritems(buf);
2362
2363         if (level == 0) {
2364                 struct btrfs_leaf_ref *ref;
2365                 struct btrfs_extent_info *info;
2366
2367                 ref = btrfs_alloc_leaf_ref(root, nr_extents);
2368                 if (!ref) {
2369                         ret = -ENOMEM;
2370                         goto out;
2371                 }
2372
2373                 ref->root_gen = root_gen;
2374                 ref->bytenr = buf->start;
2375                 ref->owner = btrfs_header_owner(buf);
2376                 ref->generation = btrfs_header_generation(buf);
2377                 ref->nritems = nr_extents;
2378                 info = ref->extents;
2379
2380                 for (i = 0; nr_extents > 0 && i < nritems; i++) {
2381                         u64 disk_bytenr;
2382                         btrfs_item_key_to_cpu(buf, &key, i);
2383                         if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2384                                 continue;
2385                         fi = btrfs_item_ptr(buf, i,
2386                                             struct btrfs_file_extent_item);
2387                         if (btrfs_file_extent_type(buf, fi) ==
2388                             BTRFS_FILE_EXTENT_INLINE)
2389                                 continue;
2390                         disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2391                         if (disk_bytenr == 0)
2392                                 continue;
2393
2394                         info->bytenr = disk_bytenr;
2395                         info->num_bytes =
2396                                 btrfs_file_extent_disk_num_bytes(buf, fi);
2397                         info->objectid = key.objectid;
2398                         info->offset = key.offset;
2399                         info++;
2400                 }
2401
2402                 ret = btrfs_add_leaf_ref(root, ref, shared);
2403                 if (ret == -EEXIST && shared) {
2404                         struct btrfs_leaf_ref *old;
2405                         old = btrfs_lookup_leaf_ref(root, ref->bytenr);
2406                         BUG_ON(!old);
2407                         btrfs_remove_leaf_ref(root, old);
2408                         btrfs_free_leaf_ref(root, old);
2409                         ret = btrfs_add_leaf_ref(root, ref, shared);
2410                 }
2411                 WARN_ON(ret);
2412                 btrfs_free_leaf_ref(root, ref);
2413         }
2414 out:
2415         return ret;
2416 }
2417
2418 /* when a block goes through cow, we update the reference counts of
2419  * everything that block points to.  The internal pointers of the block
2420  * can be in just about any order, and it is likely to have clusters of
2421  * things that are close together and clusters of things that are not.
2422  *
2423  * To help reduce the seeks that come with updating all of these reference
2424  * counts, sort them by byte number before actual updates are done.
2425  *
2426  * struct refsort is used to match byte number to slot in the btree block.
2427  * we sort based on the byte number and then use the slot to actually
2428  * find the item.
2429  *
2430  * struct refsort is smaller than strcut btrfs_item and smaller than
2431  * struct btrfs_key_ptr.  Since we're currently limited to the page size
2432  * for a btree block, there's no way for a kmalloc of refsorts for a
2433  * single node to be bigger than a page.
2434  */
2435 struct refsort {
2436         u64 bytenr;
2437         u32 slot;
2438 };
2439
2440 /*
2441  * for passing into sort()
2442  */
2443 static int refsort_cmp(const void *a_void, const void *b_void)
2444 {
2445         const struct refsort *a = a_void;
2446         const struct refsort *b = b_void;
2447
2448         if (a->bytenr < b->bytenr)
2449                 return -1;
2450         if (a->bytenr > b->bytenr)
2451                 return 1;
2452         return 0;
2453 }
2454 #endif
2455
2456 static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
2457                            struct btrfs_root *root,
2458                            struct extent_buffer *buf,
2459                            int full_backref, int inc)
2460 {
2461         u64 bytenr;
2462         u64 num_bytes;
2463         u64 parent;
2464         u64 ref_root;
2465         u32 nritems;
2466         struct btrfs_key key;
2467         struct btrfs_file_extent_item *fi;
2468         int i;
2469         int level;
2470         int ret = 0;
2471         int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
2472                             u64, u64, u64, u64, u64, u64);
2473
2474         ref_root = btrfs_header_owner(buf);
2475         nritems = btrfs_header_nritems(buf);
2476         level = btrfs_header_level(buf);
2477
2478         if (!root->ref_cows && level == 0)
2479                 return 0;
2480
2481         if (inc)
2482                 process_func = btrfs_inc_extent_ref;
2483         else
2484                 process_func = btrfs_free_extent;
2485
2486         if (full_backref)
2487                 parent = buf->start;
2488         else
2489                 parent = 0;
2490
2491         for (i = 0; i < nritems; i++) {
2492                 if (level == 0) {
2493                         btrfs_item_key_to_cpu(buf, &key, i);
2494                         if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2495                                 continue;
2496                         fi = btrfs_item_ptr(buf, i,
2497                                             struct btrfs_file_extent_item);
2498                         if (btrfs_file_extent_type(buf, fi) ==
2499                             BTRFS_FILE_EXTENT_INLINE)
2500                                 continue;
2501                         bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2502                         if (bytenr == 0)
2503                                 continue;
2504
2505                         num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2506                         key.offset -= btrfs_file_extent_offset(buf, fi);
2507                         ret = process_func(trans, root, bytenr, num_bytes,
2508                                            parent, ref_root, key.objectid,
2509                                            key.offset);
2510                         if (ret)
2511                                 goto fail;
2512                 } else {
2513                         bytenr = btrfs_node_blockptr(buf, i);
2514                         num_bytes = btrfs_level_size(root, level - 1);
2515                         ret = process_func(trans, root, bytenr, num_bytes,
2516                                            parent, ref_root, level - 1, 0);
2517                         if (ret)
2518                                 goto fail;
2519                 }
2520         }
2521         return 0;
2522 fail:
2523         BUG();
2524         return ret;
2525 }
2526
2527 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2528                   struct extent_buffer *buf, int full_backref)
2529 {
2530         return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2531 }
2532
2533 int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2534                   struct extent_buffer *buf, int full_backref)
2535 {
2536         return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
2537 }
2538
2539 static int write_one_cache_group(struct btrfs_trans_handle *trans,
2540                                  struct btrfs_root *root,
2541                                  struct btrfs_path *path,
2542                                  struct btrfs_block_group_cache *cache)
2543 {
2544         int ret;
2545         struct btrfs_root *extent_root = root->fs_info->extent_root;
2546         unsigned long bi;
2547         struct extent_buffer *leaf;
2548
2549         ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
2550         if (ret < 0)
2551                 goto fail;
2552         BUG_ON(ret);
2553
2554         leaf = path->nodes[0];
2555         bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2556         write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2557         btrfs_mark_buffer_dirty(leaf);
2558         btrfs_release_path(extent_root, path);
2559 fail:
2560         if (ret)
2561                 return ret;
2562         return 0;
2563
2564 }
2565
2566 static struct btrfs_block_group_cache *
2567 next_block_group(struct btrfs_root *root,
2568                  struct btrfs_block_group_cache *cache)
2569 {
2570         struct rb_node *node;
2571         spin_lock(&root->fs_info->block_group_cache_lock);
2572         node = rb_next(&cache->cache_node);
2573         btrfs_put_block_group(cache);
2574         if (node) {
2575                 cache = rb_entry(node, struct btrfs_block_group_cache,
2576                                  cache_node);
2577                 atomic_inc(&cache->count);
2578         } else
2579                 cache = NULL;
2580         spin_unlock(&root->fs_info->block_group_cache_lock);
2581         return cache;
2582 }
2583
2584 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2585                                    struct btrfs_root *root)
2586 {
2587         struct btrfs_block_group_cache *cache;
2588         int err = 0;
2589         struct btrfs_path *path;
2590         u64 last = 0;
2591
2592         path = btrfs_alloc_path();
2593         if (!path)
2594                 return -ENOMEM;
2595
2596         while (1) {
2597                 if (last == 0) {
2598                         err = btrfs_run_delayed_refs(trans, root,
2599                                                      (unsigned long)-1);
2600                         BUG_ON(err);
2601                 }
2602
2603                 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2604                 while (cache) {
2605                         if (cache->dirty)
2606                                 break;
2607                         cache = next_block_group(root, cache);
2608                 }
2609                 if (!cache) {
2610                         if (last == 0)
2611                                 break;
2612                         last = 0;
2613                         continue;
2614                 }
2615
2616                 cache->dirty = 0;
2617                 last = cache->key.objectid + cache->key.offset;
2618
2619                 err = write_one_cache_group(trans, root, path, cache);
2620                 BUG_ON(err);
2621                 btrfs_put_block_group(cache);
2622         }
2623
2624         btrfs_free_path(path);
2625         return 0;
2626 }
2627
2628 int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2629 {
2630         struct btrfs_block_group_cache *block_group;
2631         int readonly = 0;
2632
2633         block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2634         if (!block_group || block_group->ro)
2635                 readonly = 1;
2636         if (block_group)
2637                 btrfs_put_block_group(block_group);
2638         return readonly;
2639 }
2640
2641 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2642                              u64 total_bytes, u64 bytes_used,
2643                              struct btrfs_space_info **space_info)
2644 {
2645         struct btrfs_space_info *found;
2646
2647         found = __find_space_info(info, flags);
2648         if (found) {
2649                 spin_lock(&found->lock);
2650                 found->total_bytes += total_bytes;
2651                 found->bytes_used += bytes_used;
2652                 found->full = 0;
2653                 spin_unlock(&found->lock);
2654                 *space_info = found;
2655                 return 0;
2656         }
2657         found = kzalloc(sizeof(*found), GFP_NOFS);
2658         if (!found)
2659                 return -ENOMEM;
2660
2661         INIT_LIST_HEAD(&found->block_groups);
2662         init_rwsem(&found->groups_sem);
2663         spin_lock_init(&found->lock);
2664         found->flags = flags;
2665         found->total_bytes = total_bytes;
2666         found->bytes_used = bytes_used;
2667         found->bytes_pinned = 0;
2668         found->bytes_reserved = 0;
2669         found->bytes_readonly = 0;
2670         found->bytes_delalloc = 0;
2671         found->full = 0;
2672         found->force_alloc = 0;
2673         *space_info = found;
2674         list_add_rcu(&found->list, &info->space_info);
2675         atomic_set(&found->caching_threads, 0);
2676         return 0;
2677 }
2678
2679 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
2680 {
2681         u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
2682                                    BTRFS_BLOCK_GROUP_RAID1 |
2683                                    BTRFS_BLOCK_GROUP_RAID10 |
2684                                    BTRFS_BLOCK_GROUP_DUP);
2685         if (extra_flags) {
2686                 if (flags & BTRFS_BLOCK_GROUP_DATA)
2687                         fs_info->avail_data_alloc_bits |= extra_flags;
2688                 if (flags & BTRFS_BLOCK_GROUP_METADATA)
2689                         fs_info->avail_metadata_alloc_bits |= extra_flags;
2690                 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2691                         fs_info->avail_system_alloc_bits |= extra_flags;
2692         }
2693 }
2694
2695 static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
2696 {
2697         spin_lock(&cache->space_info->lock);
2698         spin_lock(&cache->lock);
2699         if (!cache->ro) {
2700                 cache->space_info->bytes_readonly += cache->key.offset -
2701                                         btrfs_block_group_used(&cache->item);
2702                 cache->ro = 1;
2703         }
2704         spin_unlock(&cache->lock);
2705         spin_unlock(&cache->space_info->lock);
2706 }
2707
2708 u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
2709 {
2710         u64 num_devices = root->fs_info->fs_devices->rw_devices;
2711
2712         if (num_devices == 1)
2713                 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
2714         if (num_devices < 4)
2715                 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
2716
2717         if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
2718             (flags & (BTRFS_BLOCK_GROUP_RAID1 |
2719                       BTRFS_BLOCK_GROUP_RAID10))) {
2720                 flags &= ~BTRFS_BLOCK_GROUP_DUP;
2721         }
2722
2723         if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
2724             (flags & BTRFS_BLOCK_GROUP_RAID10)) {
2725                 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
2726         }
2727
2728         if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
2729             ((flags & BTRFS_BLOCK_GROUP_RAID1) |
2730              (flags & BTRFS_BLOCK_GROUP_RAID10) |
2731              (flags & BTRFS_BLOCK_GROUP_DUP)))
2732                 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
2733         return flags;
2734 }
2735
2736 static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data)
2737 {
2738         struct btrfs_fs_info *info = root->fs_info;
2739         u64 alloc_profile;
2740
2741         if (data) {
2742                 alloc_profile = info->avail_data_alloc_bits &
2743                         info->data_alloc_profile;
2744                 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
2745         } else if (root == root->fs_info->chunk_root) {
2746                 alloc_profile = info->avail_system_alloc_bits &
2747                         info->system_alloc_profile;
2748                 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
2749         } else {
2750                 alloc_profile = info->avail_metadata_alloc_bits &
2751                         info->metadata_alloc_profile;
2752                 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
2753         }
2754
2755         return btrfs_reduce_alloc_profile(root, data);
2756 }
2757
2758 void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
2759 {
2760         u64 alloc_target;
2761
2762         alloc_target = btrfs_get_alloc_profile(root, 1);
2763         BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
2764                                                        alloc_target);
2765 }
2766
2767 static u64 calculate_bytes_needed(struct btrfs_root *root, int num_items)
2768 {
2769         u64 num_bytes;
2770         int level;
2771
2772         level = BTRFS_MAX_LEVEL - 2;
2773         /*
2774          * NOTE: these calculations are absolutely the worst possible case.
2775          * This assumes that _every_ item we insert will require a new leaf, and
2776          * that the tree has grown to its maximum level size.
2777          */
2778
2779         /*
2780          * for every item we insert we could insert both an extent item and a
2781          * extent ref item.  Then for ever item we insert, we will need to cow
2782          * both the original leaf, plus the leaf to the left and right of it.
2783          *
2784          * Unless we are talking about the extent root, then we just want the
2785          * number of items * 2, since we just need the extent item plus its ref.
2786          */
2787         if (root == root->fs_info->extent_root)
2788                 num_bytes = num_items * 2;
2789         else
2790                 num_bytes = (num_items + (2 * num_items)) * 3;
2791
2792         /*
2793          * num_bytes is total number of leaves we could need times the leaf
2794          * size, and then for every leaf we could end up cow'ing 2 nodes per
2795          * level, down to the leaf level.
2796          */
2797         num_bytes = (num_bytes * root->leafsize) +
2798                 (num_bytes * (level * 2)) * root->nodesize;
2799
2800         return num_bytes;
2801 }
2802
2803 /*
2804  * Unreserve metadata space for delalloc.  If we have less reserved credits than
2805  * we have extents, this function does nothing.
2806  */
2807 int btrfs_unreserve_metadata_for_delalloc(struct btrfs_root *root,
2808                                           struct inode *inode, int num_items)
2809 {
2810         struct btrfs_fs_info *info = root->fs_info;
2811         struct btrfs_space_info *meta_sinfo;
2812         u64 num_bytes;
2813         u64 alloc_target;
2814         bool bug = false;
2815
2816         /* get the space info for where the metadata will live */
2817         alloc_target = btrfs_get_alloc_profile(root, 0);
2818         meta_sinfo = __find_space_info(info, alloc_target);
2819
2820         num_bytes = calculate_bytes_needed(root->fs_info->extent_root,
2821                                            num_items);
2822
2823         spin_lock(&meta_sinfo->lock);
2824         spin_lock(&BTRFS_I(inode)->accounting_lock);
2825         if (BTRFS_I(inode)->reserved_extents <=
2826             BTRFS_I(inode)->outstanding_extents) {
2827                 spin_unlock(&BTRFS_I(inode)->accounting_lock);
2828                 spin_unlock(&meta_sinfo->lock);
2829                 return 0;
2830         }
2831         spin_unlock(&BTRFS_I(inode)->accounting_lock);
2832
2833         BTRFS_I(inode)->reserved_extents--;
2834         BUG_ON(BTRFS_I(inode)->reserved_extents < 0);
2835
2836         if (meta_sinfo->bytes_delalloc < num_bytes) {
2837                 bug = true;
2838                 meta_sinfo->bytes_delalloc = 0;
2839         } else {
2840                 meta_sinfo->bytes_delalloc -= num_bytes;
2841         }
2842         spin_unlock(&meta_sinfo->lock);
2843
2844         BUG_ON(bug);
2845
2846         return 0;
2847 }
2848
2849 static void check_force_delalloc(struct btrfs_space_info *meta_sinfo)
2850 {
2851         u64 thresh;
2852
2853         thresh = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
2854                 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
2855                 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
2856                 meta_sinfo->bytes_may_use;
2857
2858         thresh = meta_sinfo->total_bytes - thresh;
2859         thresh *= 80;
2860         do_div(thresh, 100);
2861         if (thresh <= meta_sinfo->bytes_delalloc)
2862                 meta_sinfo->force_delalloc = 1;
2863         else
2864                 meta_sinfo->force_delalloc = 0;
2865 }
2866
2867 struct async_flush {
2868         struct btrfs_root *root;
2869         struct btrfs_space_info *info;
2870         struct btrfs_work work;
2871 };
2872
2873 static noinline void flush_delalloc_async(struct btrfs_work *work)
2874 {
2875         struct async_flush *async;
2876         struct btrfs_root *root;
2877         struct btrfs_space_info *info;
2878
2879         async = container_of(work, struct async_flush, work);
2880         root = async->root;
2881         info = async->info;
2882
2883         btrfs_start_delalloc_inodes(root);
2884         wake_up(&info->flush_wait);
2885         btrfs_wait_ordered_extents(root, 0);
2886
2887         spin_lock(&info->lock);
2888         info->flushing = 0;
2889         spin_unlock(&info->lock);
2890         wake_up(&info->flush_wait);
2891
2892         kfree(async);
2893 }
2894
2895 static void wait_on_flush(struct btrfs_space_info *info)
2896 {
2897         DEFINE_WAIT(wait);
2898         u64 used;
2899
2900         while (1) {
2901                 prepare_to_wait(&info->flush_wait, &wait,
2902                                 TASK_UNINTERRUPTIBLE);
2903                 spin_lock(&info->lock);
2904                 if (!info->flushing) {
2905                         spin_unlock(&info->lock);
2906                         break;
2907                 }
2908
2909                 used = info->bytes_used + info->bytes_reserved +
2910                         info->bytes_pinned + info->bytes_readonly +
2911                         info->bytes_super + info->bytes_root +
2912                         info->bytes_may_use + info->bytes_delalloc;
2913                 if (used < info->total_bytes) {
2914                         spin_unlock(&info->lock);
2915                         break;
2916                 }
2917                 spin_unlock(&info->lock);
2918                 schedule();
2919         }
2920         finish_wait(&info->flush_wait, &wait);
2921 }
2922
2923 static void flush_delalloc(struct btrfs_root *root,
2924                                  struct btrfs_space_info *info)
2925 {
2926         struct async_flush *async;
2927         bool wait = false;
2928
2929         spin_lock(&info->lock);
2930
2931         if (!info->flushing) {
2932                 info->flushing = 1;
2933                 init_waitqueue_head(&info->flush_wait);
2934         } else {
2935                 wait = true;
2936         }
2937
2938         spin_unlock(&info->lock);
2939
2940         if (wait) {
2941                 wait_on_flush(info);
2942                 return;
2943         }
2944
2945         async = kzalloc(sizeof(*async), GFP_NOFS);
2946         if (!async)
2947                 goto flush;
2948
2949         async->root = root;
2950         async->info = info;
2951         async->work.func = flush_delalloc_async;
2952
2953         btrfs_queue_worker(&root->fs_info->enospc_workers,
2954                            &async->work);
2955         wait_on_flush(info);
2956         return;
2957
2958 flush:
2959         btrfs_start_delalloc_inodes(root);
2960         btrfs_wait_ordered_extents(root, 0);
2961
2962         spin_lock(&info->lock);
2963         info->flushing = 0;
2964         spin_unlock(&info->lock);
2965         wake_up(&info->flush_wait);
2966 }
2967
2968 static int maybe_allocate_chunk(struct btrfs_root *root,
2969                                  struct btrfs_space_info *info)
2970 {
2971         struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
2972         struct btrfs_trans_handle *trans;
2973         bool wait = false;
2974         int ret = 0;
2975         u64 min_metadata;
2976         u64 free_space;
2977
2978         free_space = btrfs_super_total_bytes(disk_super);
2979         /*
2980          * we allow the metadata to grow to a max of either 5gb or 5% of the
2981          * space in the volume.
2982          */
2983         min_metadata = min((u64)5 * 1024 * 1024 * 1024,
2984                              div64_u64(free_space * 5, 100));
2985         if (info->total_bytes >= min_metadata) {
2986                 spin_unlock(&info->lock);
2987                 return 0;
2988         }
2989
2990         if (info->full) {
2991                 spin_unlock(&info->lock);
2992                 return 0;
2993         }
2994
2995         if (!info->allocating_chunk) {
2996                 info->force_alloc = 1;
2997                 info->allocating_chunk = 1;
2998                 init_waitqueue_head(&info->allocate_wait);
2999         } else {
3000                 wait = true;
3001         }
3002
3003         spin_unlock(&info->lock);
3004
3005         if (wait) {
3006                 wait_event(info->allocate_wait,
3007                            !info->allocating_chunk);
3008                 return 1;
3009         }
3010
3011         trans = btrfs_start_transaction(root, 1);
3012         if (!trans) {
3013                 ret = -ENOMEM;
3014                 goto out;
3015         }
3016
3017         ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3018                              4096 + 2 * 1024 * 1024,
3019                              info->flags, 0);
3020         btrfs_end_transaction(trans, root);
3021         if (ret)
3022                 goto out;
3023 out:
3024         spin_lock(&info->lock);
3025         info->allocating_chunk = 0;
3026         spin_unlock(&info->lock);
3027         wake_up(&info->allocate_wait);
3028
3029         if (ret)
3030                 return 0;
3031         return 1;
3032 }
3033
3034 /*
3035  * Reserve metadata space for delalloc.
3036  */
3037 int btrfs_reserve_metadata_for_delalloc(struct btrfs_root *root,
3038                                         struct inode *inode, int num_items)
3039 {
3040         struct btrfs_fs_info *info = root->fs_info;
3041         struct btrfs_space_info *meta_sinfo;
3042         u64 num_bytes;
3043         u64 used;
3044         u64 alloc_target;
3045         int flushed = 0;
3046         int force_delalloc;
3047
3048         /* get the space info for where the metadata will live */
3049         alloc_target = btrfs_get_alloc_profile(root, 0);
3050         meta_sinfo = __find_space_info(info, alloc_target);
3051
3052         num_bytes = calculate_bytes_needed(root->fs_info->extent_root,
3053                                            num_items);
3054 again:
3055         spin_lock(&meta_sinfo->lock);
3056
3057         force_delalloc = meta_sinfo->force_delalloc;
3058
3059         if (unlikely(!meta_sinfo->bytes_root))
3060                 meta_sinfo->bytes_root = calculate_bytes_needed(root, 6);
3061
3062         if (!flushed)
3063                 meta_sinfo->bytes_delalloc += num_bytes;
3064
3065         used = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
3066                 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
3067                 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
3068                 meta_sinfo->bytes_may_use + meta_sinfo->bytes_delalloc;
3069
3070         if (used > meta_sinfo->total_bytes) {
3071                 flushed++;
3072
3073                 if (flushed == 1) {
3074                         if (maybe_allocate_chunk(root, meta_sinfo))
3075                                 goto again;
3076                         flushed++;
3077                 } else {
3078                         spin_unlock(&meta_sinfo->lock);
3079                 }
3080
3081                 if (flushed == 2) {
3082                         filemap_flush(inode->i_mapping);
3083                         goto again;
3084                 } else if (flushed == 3) {
3085                         flush_delalloc(root, meta_sinfo);
3086                         goto again;
3087                 }
3088                 spin_lock(&meta_sinfo->lock);
3089                 meta_sinfo->bytes_delalloc -= num_bytes;
3090                 spin_unlock(&meta_sinfo->lock);
3091                 printk(KERN_ERR "enospc, has %d, reserved %d\n",
3092                        BTRFS_I(inode)->outstanding_extents,
3093                        BTRFS_I(inode)->reserved_extents);
3094                 dump_space_info(meta_sinfo, 0, 0);
3095                 return -ENOSPC;
3096         }
3097
3098         BTRFS_I(inode)->reserved_extents++;
3099         check_force_delalloc(meta_sinfo);
3100         spin_unlock(&meta_sinfo->lock);
3101
3102         if (!flushed && force_delalloc)
3103                 filemap_flush(inode->i_mapping);
3104
3105         return 0;
3106 }
3107
3108 /*
3109  * unreserve num_items number of items worth of metadata space.  This needs to
3110  * be paired with btrfs_reserve_metadata_space.
3111  *
3112  * NOTE: if you have the option, run this _AFTER_ you do a
3113  * btrfs_end_transaction, since btrfs_end_transaction will run delayed ref
3114  * oprations which will result in more used metadata, so we want to make sure we
3115  * can do that without issue.
3116  */
3117 int btrfs_unreserve_metadata_space(struct btrfs_root *root, int num_items)
3118 {
3119         struct btrfs_fs_info *info = root->fs_info;
3120         struct btrfs_space_info *meta_sinfo;
3121         u64 num_bytes;
3122         u64 alloc_target;
3123         bool bug = false;
3124
3125         /* get the space info for where the metadata will live */
3126         alloc_target = btrfs_get_alloc_profile(root, 0);
3127         meta_sinfo = __find_space_info(info, alloc_target);
3128
3129         num_bytes = calculate_bytes_needed(root, num_items);
3130
3131         spin_lock(&meta_sinfo->lock);
3132         if (meta_sinfo->bytes_may_use < num_bytes) {
3133                 bug = true;
3134                 meta_sinfo->bytes_may_use = 0;
3135         } else {
3136                 meta_sinfo->bytes_may_use -= num_bytes;
3137         }
3138         spin_unlock(&meta_sinfo->lock);
3139
3140         BUG_ON(bug);
3141
3142         return 0;
3143 }
3144
3145 /*
3146  * Reserve some metadata space for use.  We'll calculate the worste case number
3147  * of bytes that would be needed to modify num_items number of items.  If we
3148  * have space, fantastic, if not, you get -ENOSPC.  Please call
3149  * btrfs_unreserve_metadata_space when you are done for the _SAME_ number of
3150  * items you reserved, since whatever metadata you needed should have already
3151  * been allocated.
3152  *
3153  * This will commit the transaction to make more space if we don't have enough
3154  * metadata space.  THe only time we don't do this is if we're reserving space
3155  * inside of a transaction, then we will just return -ENOSPC and it is the
3156  * callers responsibility to handle it properly.
3157  */
3158 int btrfs_reserve_metadata_space(struct btrfs_root *root, int num_items)
3159 {
3160         struct btrfs_fs_info *info = root->fs_info;
3161         struct btrfs_space_info *meta_sinfo;
3162         u64 num_bytes;
3163         u64 used;
3164         u64 alloc_target;
3165         int retries = 0;
3166
3167         /* get the space info for where the metadata will live */
3168         alloc_target = btrfs_get_alloc_profile(root, 0);
3169         meta_sinfo = __find_space_info(info, alloc_target);
3170
3171         num_bytes = calculate_bytes_needed(root, num_items);
3172 again:
3173         spin_lock(&meta_sinfo->lock);
3174
3175         if (unlikely(!meta_sinfo->bytes_root))
3176                 meta_sinfo->bytes_root = calculate_bytes_needed(root, 6);
3177
3178         if (!retries)
3179                 meta_sinfo->bytes_may_use += num_bytes;
3180
3181         used = meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
3182                 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly +
3183                 meta_sinfo->bytes_super + meta_sinfo->bytes_root +
3184                 meta_sinfo->bytes_may_use + meta_sinfo->bytes_delalloc;
3185
3186         if (used > meta_sinfo->total_bytes) {
3187                 retries++;
3188                 if (retries == 1) {
3189                         if (maybe_allocate_chunk(root, meta_sinfo))
3190                                 goto again;
3191                         retries++;
3192                 } else {
3193                         spin_unlock(&meta_sinfo->lock);
3194                 }
3195
3196                 if (retries == 2) {
3197                         flush_delalloc(root, meta_sinfo);
3198                         goto again;
3199                 }
3200                 spin_lock(&meta_sinfo->lock);
3201                 meta_sinfo->bytes_may_use -= num_bytes;
3202                 spin_unlock(&meta_sinfo->lock);
3203
3204                 dump_space_info(meta_sinfo, 0, 0);
3205                 return -ENOSPC;
3206         }
3207
3208         check_force_delalloc(meta_sinfo);
3209         spin_unlock(&meta_sinfo->lock);
3210
3211         return 0;
3212 }
3213
3214 /*
3215  * This will check the space that the inode allocates from to make sure we have
3216  * enough space for bytes.
3217  */
3218 int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
3219                                 u64 bytes)
3220 {
3221         struct btrfs_space_info *data_sinfo;
3222         int ret = 0, committed = 0;
3223
3224         /* make sure bytes are sectorsize aligned */
3225         bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3226
3227         data_sinfo = BTRFS_I(inode)->space_info;
3228         if (!data_sinfo)
3229                 goto alloc;
3230
3231 again:
3232         /* make sure we have enough space to handle the data first */
3233         spin_lock(&data_sinfo->lock);
3234         if (data_sinfo->total_bytes - data_sinfo->bytes_used -
3235             data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved -
3236             data_sinfo->bytes_pinned - data_sinfo->bytes_readonly -
3237             data_sinfo->bytes_may_use - data_sinfo->bytes_super < bytes) {
3238                 struct btrfs_trans_handle *trans;
3239
3240                 /*
3241                  * if we don't have enough free bytes in this space then we need
3242                  * to alloc a new chunk.
3243                  */
3244                 if (!data_sinfo->full) {
3245                         u64 alloc_target;
3246
3247                         data_sinfo->force_alloc = 1;
3248                         spin_unlock(&data_sinfo->lock);
3249 alloc:
3250                         alloc_target = btrfs_get_alloc_profile(root, 1);
3251                         trans = btrfs_start_transaction(root, 1);
3252                         if (!trans)
3253                                 return -ENOMEM;
3254
3255                         ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3256                                              bytes + 2 * 1024 * 1024,
3257                                              alloc_target, 0);
3258                         btrfs_end_transaction(trans, root);
3259                         if (ret)
3260                                 return ret;
3261
3262                         if (!data_sinfo) {
3263                                 btrfs_set_inode_space_info(root, inode);
3264                                 data_sinfo = BTRFS_I(inode)->space_info;
3265                         }
3266                         goto again;
3267                 }
3268                 spin_unlock(&data_sinfo->lock);
3269
3270                 /* commit the current transaction and try again */
3271                 if (!committed && !root->fs_info->open_ioctl_trans) {
3272                         committed = 1;
3273                         trans = btrfs_join_transaction(root, 1);
3274                         if (!trans)
3275                                 return -ENOMEM;
3276                         ret = btrfs_commit_transaction(trans, root);
3277                         if (ret)
3278                                 return ret;
3279                         goto again;
3280                 }
3281
3282                 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
3283                        ", %llu bytes_used, %llu bytes_reserved, "
3284                        "%llu bytes_pinned, %llu bytes_readonly, %llu may use "
3285                        "%llu total\n", (unsigned long long)bytes,
3286                        (unsigned long long)data_sinfo->bytes_delalloc,
3287                        (unsigned long long)data_sinfo->bytes_used,
3288                        (unsigned long long)data_sinfo->bytes_reserved,
3289                        (unsigned long long)data_sinfo->bytes_pinned,
3290                        (unsigned long long)data_sinfo->bytes_readonly,
3291                        (unsigned long long)data_sinfo->bytes_may_use,
3292                        (unsigned long long)data_sinfo->total_bytes);
3293                 return -ENOSPC;
3294         }
3295         data_sinfo->bytes_may_use += bytes;
3296         BTRFS_I(inode)->reserved_bytes += bytes;
3297         spin_unlock(&data_sinfo->lock);
3298
3299         return 0;
3300 }
3301
3302 /*
3303  * if there was an error for whatever reason after calling
3304  * btrfs_check_data_free_space, call this so we can cleanup the counters.
3305  */
3306 void btrfs_free_reserved_data_space(struct btrfs_root *root,
3307                                     struct inode *inode, u64 bytes)
3308 {
3309         struct btrfs_space_info *data_sinfo;
3310
3311         /* make sure bytes are sectorsize aligned */
3312         bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3313
3314         data_sinfo = BTRFS_I(inode)->space_info;
3315         spin_lock(&data_sinfo->lock);
3316         data_sinfo->bytes_may_use -= bytes;
3317         BTRFS_I(inode)->reserved_bytes -= bytes;
3318         spin_unlock(&data_sinfo->lock);
3319 }
3320
3321 /* called when we are adding a delalloc extent to the inode's io_tree */
3322 void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
3323                                   u64 bytes)
3324 {
3325         struct btrfs_space_info *data_sinfo;
3326
3327         /* get the space info for where this inode will be storing its data */
3328         data_sinfo = BTRFS_I(inode)->space_info;
3329
3330         /* make sure we have enough space to handle the data first */
3331         spin_lock(&data_sinfo->lock);
3332         data_sinfo->bytes_delalloc += bytes;
3333
3334         /*
3335          * we are adding a delalloc extent without calling
3336          * btrfs_check_data_free_space first.  This happens on a weird
3337          * writepage condition, but shouldn't hurt our accounting
3338          */
3339         if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) {
3340                 data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes;
3341                 BTRFS_I(inode)->reserved_bytes = 0;
3342         } else {
3343                 data_sinfo->bytes_may_use -= bytes;
3344                 BTRFS_I(inode)->reserved_bytes -= bytes;
3345         }
3346
3347         spin_unlock(&data_sinfo->lock);
3348 }
3349
3350 /* called when we are clearing an delalloc extent from the inode's io_tree */
3351 void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
3352                               u64 bytes)
3353 {
3354         struct btrfs_space_info *info;
3355
3356         info = BTRFS_I(inode)->space_info;
3357
3358         spin_lock(&info->lock);
3359         info->bytes_delalloc -= bytes;
3360         spin_unlock(&info->lock);
3361 }
3362
3363 static void force_metadata_allocation(struct btrfs_fs_info *info)
3364 {
3365         struct list_head *head = &info->space_info;
3366         struct btrfs_space_info *found;
3367
3368         rcu_read_lock();
3369         list_for_each_entry_rcu(found, head, list) {
3370                 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
3371                         found->force_alloc = 1;
3372         }
3373         rcu_read_unlock();
3374 }
3375
3376 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3377                           struct btrfs_root *extent_root, u64 alloc_bytes,
3378                           u64 flags, int force)
3379 {
3380         struct btrfs_space_info *space_info;
3381         struct btrfs_fs_info *fs_info = extent_root->fs_info;
3382         u64 thresh;
3383         int ret = 0;
3384
3385         mutex_lock(&fs_info->chunk_mutex);
3386
3387         flags = btrfs_reduce_alloc_profile(extent_root, flags);
3388
3389         space_info = __find_space_info(extent_root->fs_info, flags);
3390         if (!space_info) {
3391                 ret = update_space_info(extent_root->fs_info, flags,
3392                                         0, 0, &space_info);
3393                 BUG_ON(ret);
3394         }
3395         BUG_ON(!space_info);
3396
3397         spin_lock(&space_info->lock);
3398         if (space_info->force_alloc)
3399                 force = 1;
3400         if (space_info->full) {
3401                 spin_unlock(&space_info->lock);
3402                 goto out;
3403         }
3404
3405         thresh = space_info->total_bytes - space_info->bytes_readonly;
3406         thresh = div_factor(thresh, 8);
3407         if (!force &&
3408            (space_info->bytes_used + space_info->bytes_pinned +
3409             space_info->bytes_reserved + alloc_bytes) < thresh) {
3410                 spin_unlock(&space_info->lock);
3411                 goto out;
3412         }
3413         spin_unlock(&space_info->lock);
3414
3415         /*
3416          * if we're doing a data chunk, go ahead and make sure that
3417          * we keep a reasonable number of metadata chunks allocated in the
3418          * FS as well.
3419          */
3420         if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
3421                 fs_info->data_chunk_allocations++;
3422                 if (!(fs_info->data_chunk_allocations %
3423                       fs_info->metadata_ratio))
3424                         force_metadata_allocation(fs_info);
3425         }
3426
3427         ret = btrfs_alloc_chunk(trans, extent_root, flags);
3428         spin_lock(&space_info->lock);
3429         if (ret)
3430                 space_info->full = 1;
3431         space_info->force_alloc = 0;
3432         spin_unlock(&space_info->lock);
3433 out:
3434         mutex_unlock(&extent_root->fs_info->chunk_mutex);
3435         return ret;
3436 }
3437
3438 static int update_block_group(struct btrfs_trans_handle *trans,
3439                               struct btrfs_root *root,
3440                               u64 bytenr, u64 num_bytes, int alloc,
3441                               int mark_free)
3442 {
3443         struct btrfs_block_group_cache *cache;
3444         struct btrfs_fs_info *info = root->fs_info;
3445         u64 total = num_bytes;
3446         u64 old_val;
3447         u64 byte_in_group;
3448
3449         /* block accounting for super block */
3450         spin_lock(&info->delalloc_lock);
3451         old_val = btrfs_super_bytes_used(&info->super_copy);
3452         if (alloc)
3453                 old_val += num_bytes;
3454         else
3455                 old_val -= num_bytes;
3456         btrfs_set_super_bytes_used(&info->super_copy, old_val);
3457
3458         /* block accounting for root item */
3459         old_val = btrfs_root_used(&root->root_item);
3460         if (alloc)
3461                 old_val += num_bytes;
3462         else
3463                 old_val -= num_bytes;
3464         btrfs_set_root_used(&root->root_item, old_val);
3465         spin_unlock(&info->delalloc_lock);
3466
3467         while (total) {
3468                 cache = btrfs_lookup_block_group(info, bytenr);
3469                 if (!cache)
3470                         return -1;
3471                 byte_in_group = bytenr - cache->key.objectid;
3472                 WARN_ON(byte_in_group > cache->key.offset);
3473
3474                 spin_lock(&cache->space_info->lock);
3475                 spin_lock(&cache->lock);
3476                 cache->dirty = 1;
3477                 old_val = btrfs_block_group_used(&cache->item);
3478                 num_bytes = min(total, cache->key.offset - byte_in_group);
3479                 if (alloc) {
3480                         old_val += num_bytes;
3481                         btrfs_set_block_group_used(&cache->item, old_val);
3482                         cache->reserved -= num_bytes;
3483                         cache->space_info->bytes_used += num_bytes;
3484                         cache->space_info->bytes_reserved -= num_bytes;
3485                         if (cache->ro)
3486                                 cache->space_info->bytes_readonly -= num_bytes;
3487                         spin_unlock(&cache->lock);
3488                         spin_unlock(&cache->space_info->lock);
3489                 } else {
3490                         old_val -= num_bytes;
3491                         cache->space_info->bytes_used -= num_bytes;
3492                         if (cache->ro)
3493                                 cache->space_info->bytes_readonly += num_bytes;
3494                         btrfs_set_block_group_used(&cache->item, old_val);
3495                         spin_unlock(&cache->lock);
3496                         spin_unlock(&cache->space_info->lock);
3497                         if (mark_free) {
3498                                 int ret;
3499
3500                                 ret = btrfs_discard_extent(root, bytenr,
3501                                                            num_bytes);
3502                                 WARN_ON(ret);
3503
3504                                 ret = btrfs_add_free_space(cache, bytenr,
3505                                                            num_bytes);
3506                                 WARN_ON(ret);
3507                         }
3508                 }
3509                 btrfs_put_block_group(cache);
3510                 total -= num_bytes;
3511                 bytenr += num_bytes;
3512         }
3513         return 0;
3514 }
3515
3516 static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
3517 {
3518         struct btrfs_block_group_cache *cache;
3519         u64 bytenr;
3520
3521         cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
3522         if (!cache)
3523                 return 0;
3524
3525         bytenr = cache->key.objectid;
3526         btrfs_put_block_group(cache);
3527
3528         return bytenr;
3529 }
3530
3531 /*
3532  * this function must be called within transaction
3533  */
3534 int btrfs_pin_extent(struct btrfs_root *root,
3535                      u64 bytenr, u64 num_bytes, int reserved)
3536 {
3537         struct btrfs_fs_info *fs_info = root->fs_info;
3538         struct btrfs_block_group_cache *cache;
3539
3540         cache = btrfs_lookup_block_group(fs_info, bytenr);
3541         BUG_ON(!cache);
3542
3543         spin_lock(&cache->space_info->lock);
3544         spin_lock(&cache->lock);
3545         cache->pinned += num_bytes;
3546         cache->space_info->bytes_pinned += num_bytes;
3547         if (reserved) {
3548                 cache->reserved -= num_bytes;
3549                 cache->space_info->bytes_reserved -= num_bytes;
3550         }
3551         spin_unlock(&cache->lock);
3552         spin_unlock(&cache->space_info->lock);
3553
3554         btrfs_put_block_group(cache);
3555
3556         set_extent_dirty(fs_info->pinned_extents,
3557                          bytenr, bytenr + num_bytes - 1, GFP_NOFS);
3558         return 0;
3559 }
3560
3561 static int update_reserved_extents(struct btrfs_block_group_cache *cache,
3562                                    u64 num_bytes, int reserve)
3563 {
3564         spin_lock(&cache->space_info->lock);
3565         spin_lock(&cache->lock);
3566         if (reserve) {
3567                 cache->reserved += num_bytes;
3568                 cache->space_info->bytes_reserved += num_bytes;
3569         } else {
3570                 cache->reserved -= num_bytes;
3571                 cache->space_info->bytes_reserved -= num_bytes;
3572         }
3573         spin_unlock(&cache->lock);
3574         spin_unlock(&cache->space_info->lock);
3575         return 0;
3576 }
3577
3578 int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
3579                                 struct btrfs_root *root)
3580 {
3581         struct btrfs_fs_info *fs_info = root->fs_info;
3582         struct btrfs_caching_control *next;
3583         struct btrfs_caching_control *caching_ctl;
3584         struct btrfs_block_group_cache *cache;
3585
3586         down_write(&fs_info->extent_commit_sem);
3587
3588         list_for_each_entry_safe(caching_ctl, next,
3589                                  &fs_info->caching_block_groups, list) {
3590                 cache = caching_ctl->block_group;
3591                 if (block_group_cache_done(cache)) {
3592                         cache->last_byte_to_unpin = (u64)-1;
3593                         list_del_init(&caching_ctl->list);
3594                         put_caching_control(caching_ctl);
3595                 } else {
3596                         cache->last_byte_to_unpin = caching_ctl->progress;
3597                 }
3598         }
3599
3600         if (fs_info->pinned_extents == &fs_info->freed_extents[0])
3601                 fs_info->pinned_extents = &fs_info->freed_extents[1];
3602         else
3603                 fs_info->pinned_extents = &fs_info->freed_extents[0];
3604
3605         up_write(&fs_info->extent_commit_sem);
3606         return 0;
3607 }
3608
3609 static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
3610 {
3611         struct btrfs_fs_info *fs_info = root->fs_info;
3612         struct btrfs_block_group_cache *cache = NULL;
3613         u64 len;
3614
3615         while (start <= end) {
3616                 if (!cache ||
3617                     start >= cache->key.objectid + cache->key.offset) {
3618                         if (cache)
3619                                 btrfs_put_block_group(cache);
3620                         cache = btrfs_lookup_block_group(fs_info, start);
3621                         BUG_ON(!cache);
3622                 }
3623
3624                 len = cache->key.objectid + cache->key.offset - start;
3625                 len = min(len, end + 1 - start);
3626
3627                 if (start < cache->last_byte_to_unpin) {
3628                         len = min(len, cache->last_byte_to_unpin - start);
3629                         btrfs_add_free_space(cache, start, len);
3630                 }
3631
3632                 spin_lock(&cache->space_info->lock);
3633                 spin_lock(&cache->lock);
3634                 cache->pinned -= len;
3635                 cache->space_info->bytes_pinned -= len;
3636                 spin_unlock(&cache->lock);
3637                 spin_unlock(&cache->space_info->lock);
3638
3639                 start += len;
3640         }
3641
3642         if (cache)
3643                 btrfs_put_block_group(cache);
3644         return 0;
3645 }
3646
3647 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
3648                                struct btrfs_root *root)
3649 {
3650         struct btrfs_fs_info *fs_info = root->fs_info;
3651         struct extent_io_tree *unpin;
3652         u64 start;
3653         u64 end;
3654         int ret;
3655
3656         if (fs_info->pinned_extents == &fs_info->freed_extents[0])
3657                 unpin = &fs_info->freed_extents[1];
3658         else
3659                 unpin = &fs_info->freed_extents[0];
3660
3661         while (1) {
3662                 ret = find_first_extent_bit(unpin, 0, &start, &end,
3663                                             EXTENT_DIRTY);
3664                 if (ret)
3665                         break;
3666
3667                 ret = btrfs_discard_extent(root, start, end + 1 - start);
3668
3669                 clear_extent_dirty(unpin, start, end, GFP_NOFS);
3670                 unpin_extent_range(root, start, end);
3671                 cond_resched();
3672         }
3673
3674         return ret;
3675 }
3676
3677 static int pin_down_bytes(struct btrfs_trans_handle *trans,
3678                           struct btrfs_root *root,
3679                           struct btrfs_path *path,
3680                           u64 bytenr, u64 num_bytes,
3681                           int is_data, int reserved,
3682                           struct extent_buffer **must_clean)
3683 {
3684         int err = 0;
3685         struct extent_buffer *buf;
3686
3687         if (is_data)
3688                 goto pinit;
3689
3690         /*
3691          * discard is sloooow, and so triggering discards on
3692          * individual btree blocks isn't a good plan.  Just
3693          * pin everything in discard mode.
3694          */
3695         if (btrfs_test_opt(root, DISCARD))
3696                 goto pinit;
3697
3698         buf = btrfs_find_tree_block(root, bytenr, num_bytes);
3699         if (!buf)
3700                 goto pinit;
3701
3702         /* we can reuse a block if it hasn't been written
3703          * and it is from this transaction.  We can't
3704          * reuse anything from the tree log root because
3705          * it has tiny sub-transactions.
3706          */
3707         if (btrfs_buffer_uptodate(buf, 0) &&
3708             btrfs_try_tree_lock(buf)) {
3709                 u64 header_owner = btrfs_header_owner(buf);
3710                 u64 header_transid = btrfs_header_generation(buf);
3711                 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
3712                     header_transid == trans->transid &&
3713                     !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
3714                         *must_clean = buf;
3715                         return 1;
3716                 }
3717                 btrfs_tree_unlock(buf);
3718         }
3719         free_extent_buffer(buf);
3720 pinit:
3721         if (path)
3722                 btrfs_set_path_blocking(path);
3723         /* unlocks the pinned mutex */
3724         btrfs_pin_extent(root, bytenr, num_bytes, reserved);
3725
3726         BUG_ON(err < 0);
3727         return 0;
3728 }
3729
3730 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
3731                                 struct btrfs_root *root,
3732                                 u64 bytenr, u64 num_bytes, u64 parent,
3733                                 u64 root_objectid, u64 owner_objectid,
3734                                 u64 owner_offset, int refs_to_drop,
3735                                 struct btrfs_delayed_extent_op *extent_op)
3736 {
3737         struct btrfs_key key;
3738         struct btrfs_path *path;
3739         struct btrfs_fs_info *info = root->fs_info;
3740         struct btrfs_root *extent_root = info->extent_root;
3741         struct extent_buffer *leaf;
3742         struct btrfs_extent_item *ei;
3743         struct btrfs_extent_inline_ref *iref;
3744         int ret;
3745         int is_data;
3746         int extent_slot = 0;
3747         int found_extent = 0;
3748         int num_to_del = 1;
3749         u32 item_size;
3750         u64 refs;
3751
3752         path = btrfs_alloc_path();
3753         if (!path)
3754                 return -ENOMEM;
3755
3756         path->reada = 1;
3757         path->leave_spinning = 1;
3758
3759         is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
3760         BUG_ON(!is_data && refs_to_drop != 1);
3761
3762         ret = lookup_extent_backref(trans, extent_root, path, &iref,
3763                                     bytenr, num_bytes, parent,
3764                                     root_objectid, owner_objectid,
3765                                     owner_offset);
3766         if (ret == 0) {
3767                 extent_slot = path->slots[0];
3768                 while (extent_slot >= 0) {
3769                         btrfs_item_key_to_cpu(path->nodes[0], &key,
3770                                               extent_slot);
3771                         if (key.objectid != bytenr)
3772                                 break;
3773                         if (key.type == BTRFS_EXTENT_ITEM_KEY &&
3774                             key.offset == num_bytes) {
3775                                 found_extent = 1;
3776                                 break;
3777                         }
3778                         if (path->slots[0] - extent_slot > 5)
3779                                 break;
3780                         extent_slot--;
3781                 }
3782 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3783                 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
3784                 if (found_extent && item_size < sizeof(*ei))
3785                         found_extent = 0;
3786 #endif
3787                 if (!found_extent) {
3788                         BUG_ON(iref);
3789                         ret = remove_extent_backref(trans, extent_root, path,
3790                                                     NULL, refs_to_drop,
3791                                                     is_data);
3792                         BUG_ON(ret);
3793                         btrfs_release_path(extent_root, path);
3794                         path->leave_spinning = 1;
3795
3796                         key.objectid = bytenr;
3797                         key.type = BTRFS_EXTENT_ITEM_KEY;
3798                         key.offset = num_bytes;
3799
3800                         ret = btrfs_search_slot(trans, extent_root,
3801                                                 &key, path, -1, 1);
3802                         if (ret) {
3803                                 printk(KERN_ERR "umm, got %d back from search"
3804                                        ", was looking for %llu\n", ret,
3805                                        (unsigned long long)bytenr);
3806                                 btrfs_print_leaf(extent_root, path->nodes[0]);
3807                         }
3808                         BUG_ON(ret);
3809                         extent_slot = path->slots[0];
3810                 }
3811         } else {
3812                 btrfs_print_leaf(extent_root, path->nodes[0]);
3813                 WARN_ON(1);
3814                 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
3815                        "parent %llu root %llu  owner %llu offset %llu\n",
3816                        (unsigned long long)bytenr,
3817                        (unsigned long long)parent,
3818                        (unsigned long long)root_objectid,
3819                        (unsigned long long)owner_objectid,
3820                        (unsigned long long)owner_offset);
3821         }
3822
3823         leaf = path->nodes[0];
3824         item_size = btrfs_item_size_nr(leaf, extent_slot);
3825 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3826         if (item_size < sizeof(*ei)) {
3827                 BUG_ON(found_extent || extent_slot != path->slots[0]);
3828                 ret = convert_extent_item_v0(trans, extent_root, path,
3829                                              owner_objectid, 0);
3830                 BUG_ON(ret < 0);
3831
3832                 btrfs_release_path(extent_root, path);
3833                 path->leave_spinning = 1;
3834
3835                 key.objectid = bytenr;
3836                 key.type = BTRFS_EXTENT_ITEM_KEY;
3837                 key.offset = num_bytes;
3838
3839                 ret = btrfs_search_slot(trans, extent_root, &key, path,
3840                                         -1, 1);
3841                 if (ret) {
3842                         printk(KERN_ERR "umm, got %d back from search"
3843                                ", was looking for %llu\n", ret,
3844                                (unsigned long long)bytenr);
3845                         btrfs_print_leaf(extent_root, path->nodes[0]);
3846                 }
3847                 BUG_ON(ret);
3848                 extent_slot = path->slots[0];
3849                 leaf = path->nodes[0];
3850                 item_size = btrfs_item_size_nr(leaf, extent_slot);
3851         }
3852 #endif
3853         BUG_ON(item_size < sizeof(*ei));
3854         ei = btrfs_item_ptr(leaf, extent_slot,
3855                             struct btrfs_extent_item);
3856         if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
3857                 struct btrfs_tree_block_info *bi;
3858                 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
3859                 bi = (struct btrfs_tree_block_info *)(ei + 1);
3860                 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
3861         }
3862
3863         refs = btrfs_extent_refs(leaf, ei);
3864         BUG_ON(refs < refs_to_drop);
3865         refs -= refs_to_drop;
3866
3867         if (refs > 0) {
3868                 if (extent_op)
3869                         __run_delayed_extent_op(extent_op, leaf, ei);
3870                 /*
3871                  * In the case of inline back ref, reference count will
3872                  * be updated by remove_extent_backref
3873                  */
3874                 if (iref) {
3875                         BUG_ON(!found_extent);
3876                 } else {
3877                         btrfs_set_extent_refs(leaf, ei, refs);
3878                         btrfs_mark_buffer_dirty(leaf);
3879                 }
3880                 if (found_extent) {
3881                         ret = remove_extent_backref(trans, extent_root, path,
3882                                                     iref, refs_to_drop,
3883                                                     is_data);
3884                         BUG_ON(ret);
3885                 }
3886         } else {
3887                 int mark_free = 0;
3888                 struct extent_buffer *must_clean = NULL;
3889
3890                 if (found_extent) {
3891                         BUG_ON(is_data && refs_to_drop !=
3892                                extent_data_ref_count(root, path, iref));
3893                         if (iref) {
3894                                 BUG_ON(path->slots[0] != extent_slot);
3895                         } else {
3896                                 BUG_ON(path->slots[0] != extent_slot + 1);
3897                                 path->slots[0] = extent_slot;
3898                                 num_to_del = 2;
3899                         }
3900                 }
3901
3902                 ret = pin_down_bytes(trans, root, path, bytenr,
3903                                      num_bytes, is_data, 0, &must_clean);
3904                 if (ret > 0)
3905                         mark_free = 1;
3906                 BUG_ON(ret < 0);
3907                 /*
3908                  * it is going to be very rare for someone to be waiting
3909                  * on the block we're freeing.  del_items might need to
3910                  * schedule, so rather than get fancy, just force it
3911                  * to blocking here
3912                  */
3913                 if (must_clean)
3914                         btrfs_set_lock_blocking(must_clean);
3915
3916                 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
3917                                       num_to_del);
3918                 BUG_ON(ret);
3919                 btrfs_release_path(extent_root, path);
3920
3921                 if (must_clean) {
3922                         clean_tree_block(NULL, root, must_clean);
3923                         btrfs_tree_unlock(must_clean);
3924                         free_extent_buffer(must_clean);
3925                 }
3926
3927                 if (is_data) {
3928                         ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
3929                         BUG_ON(ret);
3930                 } else {
3931                         invalidate_mapping_pages(info->btree_inode->i_mapping,
3932                              bytenr >> PAGE_CACHE_SHIFT,
3933                              (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
3934                 }
3935
3936                 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
3937                                          mark_free);
3938                 BUG_ON(ret);
3939         }
3940         btrfs_free_path(path);
3941         return ret;
3942 }
3943
3944 /*
3945  * when we free an extent, it is possible (and likely) that we free the last
3946  * delayed ref for that extent as well.  This searches the delayed ref tree for
3947  * a given extent, and if there are no other delayed refs to be processed, it
3948  * removes it from the tree.
3949  */
3950 static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
3951                                       struct btrfs_root *root, u64 bytenr)
3952 {
3953         struct btrfs_delayed_ref_head *head;
3954         struct btrfs_delayed_ref_root *delayed_refs;
3955         struct btrfs_delayed_ref_node *ref;
3956         struct rb_node *node;
3957         int ret;
3958
3959         delayed_refs = &trans->transaction->delayed_refs;
3960         spin_lock(&delayed_refs->lock);
3961         head = btrfs_find_delayed_ref_head(trans, bytenr);
3962         if (!head)
3963                 goto out;
3964
3965         node = rb_prev(&head->node.rb_node);
3966         if (!node)
3967                 goto out;
3968
3969         ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
3970
3971         /* there are still entries for this ref, we can't drop it */
3972         if (ref->bytenr == bytenr)
3973                 goto out;
3974
3975         if (head->extent_op) {
3976                 if (!head->must_insert_reserved)
3977                         goto out;
3978                 kfree(head->extent_op);
3979                 head->extent_op = NULL;
3980         }
3981
3982         /*
3983          * waiting for the lock here would deadlock.  If someone else has it
3984          * locked they are already in the process of dropping it anyway
3985          */
3986         if (!mutex_trylock(&head->mutex))
3987                 goto out;
3988
3989         /*
3990          * at this point we have a head with no other entries.  Go
3991          * ahead and process it.
3992          */
3993         head->node.in_tree = 0;
3994         rb_erase(&head->node.rb_node, &delayed_refs->root);
3995
3996         delayed_refs->num_entries--;
3997
3998         /*
3999          * we don't take a ref on the node because we're removing it from the
4000          * tree, so we just steal the ref the tree was holding.
4001          */
4002         delayed_refs->num_heads--;
4003         if (list_empty(&head->cluster))
4004                 delayed_refs->num_heads_ready--;
4005
4006         list_del_init(&head->cluster);
4007         spin_unlock(&delayed_refs->lock);
4008
4009         ret = run_one_delayed_ref(trans, root->fs_info->tree_root,
4010                                   &head->node, head->extent_op,
4011                                   head->must_insert_reserved);
4012         BUG_ON(ret);
4013         btrfs_put_delayed_ref(&head->node);
4014         return 0;
4015 out:
4016         spin_unlock(&delayed_refs->lock);
4017         return 0;
4018 }
4019
4020 int btrfs_free_extent(struct btrfs_trans_handle *trans,
4021                       struct btrfs_root *root,
4022                       u64 bytenr, u64 num_bytes, u64 parent,
4023                       u64 root_objectid, u64 owner, u64 offset)
4024 {
4025         int ret;
4026
4027         /*
4028          * tree log blocks never actually go into the extent allocation
4029          * tree, just update pinning info and exit early.
4030          */
4031         if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
4032                 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
4033                 /* unlocks the pinned mutex */
4034                 btrfs_pin_extent(root, bytenr, num_bytes, 1);
4035                 ret = 0;
4036         } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
4037                 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
4038                                         parent, root_objectid, (int)owner,
4039                                         BTRFS_DROP_DELAYED_REF, NULL);
4040                 BUG_ON(ret);
4041                 ret = check_ref_cleanup(trans, root, bytenr);
4042                 BUG_ON(ret);
4043         } else {
4044                 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
4045                                         parent, root_objectid, owner,
4046                                         offset, BTRFS_DROP_DELAYED_REF, NULL);
4047                 BUG_ON(ret);
4048         }
4049         return ret;
4050 }
4051
4052 static u64 stripe_align(struct btrfs_root *root, u64 val)
4053 {
4054         u64 mask = ((u64)root->stripesize - 1);
4055         u64 ret = (val + mask) & ~mask;
4056         return ret;
4057 }
4058
4059 /*
4060  * when we wait for progress in the block group caching, its because
4061  * our allocation attempt failed at least once.  So, we must sleep
4062  * and let some progress happen before we try again.
4063  *
4064  * This function will sleep at least once waiting for new free space to
4065  * show up, and then it will check the block group free space numbers
4066  * for our min num_bytes.  Another option is to have it go ahead
4067  * and look in the rbtree for a free extent of a given size, but this
4068  * is a good start.
4069  */
4070 static noinline int
4071 wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
4072                                 u64 num_bytes)
4073 {
4074         struct btrfs_caching_control *caching_ctl;
4075         DEFINE_WAIT(wait);
4076
4077         caching_ctl = get_caching_control(cache);
4078         if (!caching_ctl)
4079                 return 0;
4080
4081         wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
4082                    (cache->free_space >= num_bytes));
4083
4084         put_caching_control(caching_ctl);
4085         return 0;
4086 }
4087
4088 static noinline int
4089 wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
4090 {
4091         struct btrfs_caching_control *caching_ctl;
4092         DEFINE_WAIT(wait);
4093
4094         caching_ctl = get_caching_control(cache);
4095         if (!caching_ctl)
4096                 return 0;
4097
4098         wait_event(caching_ctl->wait, block_group_cache_done(cache));
4099
4100         put_caching_control(caching_ctl);
4101         return 0;
4102 }
4103
4104 enum btrfs_loop_type {
4105         LOOP_CACHED_ONLY = 0,
4106         LOOP_CACHING_NOWAIT = 1,
4107         LOOP_CACHING_WAIT = 2,
4108         LOOP_ALLOC_CHUNK = 3,
4109         LOOP_NO_EMPTY_SIZE = 4,
4110 };
4111
4112 /*
4113  * walks the btree of allocated extents and find a hole of a given size.
4114  * The key ins is changed to record the hole:
4115  * ins->objectid == block start
4116  * ins->flags = BTRFS_EXTENT_ITEM_KEY
4117  * ins->offset == number of blocks
4118  * Any available blocks before search_start are skipped.
4119  */
4120 static noinline int find_free_extent(struct btrfs_trans_handle *trans,
4121                                      struct btrfs_root *orig_root,
4122                                      u64 num_bytes, u64 empty_size,
4123                                      u64 search_start, u64 search_end,
4124                                      u64 hint_byte, struct btrfs_key *ins,
4125                                      u64 exclude_start, u64 exclude_nr,
4126                                      int data)
4127 {
4128         int ret = 0;
4129         struct btrfs_root *root = orig_root->fs_info->extent_root;
4130         struct btrfs_free_cluster *last_ptr = NULL;
4131         struct btrfs_block_group_cache *block_group = NULL;
4132         int empty_cluster = 2 * 1024 * 1024;
4133         int allowed_chunk_alloc = 0;
4134         struct btrfs_space_info *space_info;
4135         int last_ptr_loop = 0;
4136         int loop = 0;
4137         bool found_uncached_bg = false;
4138         bool failed_cluster_refill = false;
4139         bool failed_alloc = false;
4140
4141         WARN_ON(num_bytes < root->sectorsize);
4142         btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
4143         ins->objectid = 0;
4144         ins->offset = 0;
4145
4146         space_info = __find_space_info(root->fs_info, data);
4147
4148         if (orig_root->ref_cows || empty_size)
4149                 allowed_chunk_alloc = 1;
4150
4151         if (data & BTRFS_BLOCK_GROUP_METADATA) {
4152                 last_ptr = &root->fs_info->meta_alloc_cluster;
4153                 if (!btrfs_test_opt(root, SSD))
4154                         empty_cluster = 64 * 1024;
4155         }
4156
4157         if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
4158                 last_ptr = &root->fs_info->data_alloc_cluster;
4159         }
4160
4161         if (last_ptr) {
4162                 spin_lock(&last_ptr->lock);
4163                 if (last_ptr->block_group)
4164                         hint_byte = last_ptr->window_start;
4165                 spin_unlock(&last_ptr->lock);
4166         }
4167
4168         search_start = max(search_start, first_logical_byte(root, 0));
4169         search_start = max(search_start, hint_byte);
4170
4171         if (!last_ptr)
4172                 empty_cluster = 0;
4173
4174         if (search_start == hint_byte) {
4175                 block_group = btrfs_lookup_block_group(root->fs_info,
4176                                                        search_start);
4177                 /*
4178                  * we don't want to use the block group if it doesn't match our
4179                  * allocation bits, or if its not cached.
4180                  */
4181                 if (block_group && block_group_bits(block_group, data) &&
4182                     block_group_cache_done(block_group)) {
4183                         down_read(&space_info->groups_sem);
4184                         if (list_empty(&block_group->list) ||
4185                             block_group->ro) {
4186                                 /*
4187                                  * someone is removing this block group,
4188                                  * we can't jump into the have_block_group
4189                                  * target because our list pointers are not
4190                                  * valid
4191                                  */
4192                                 btrfs_put_block_group(block_group);
4193                                 up_read(&space_info->groups_sem);
4194                         } else
4195                                 goto have_block_group;
4196                 } else if (block_group) {
4197                         btrfs_put_block_group(block_group);
4198                 }
4199         }
4200
4201 search:
4202         down_read(&space_info->groups_sem);
4203         list_for_each_entry(block_group, &space_info->block_groups, list) {
4204                 u64 offset;
4205                 int cached;
4206
4207                 atomic_inc(&block_group->count);
4208                 search_start = block_group->key.objectid;
4209
4210 have_block_group:
4211                 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
4212                         /*
4213                          * we want to start caching kthreads, but not too many
4214                          * right off the bat so we don't overwhelm the system,
4215                          * so only start them if there are less than 2 and we're
4216                          * in the initial allocation phase.
4217                          */
4218                         if (loop > LOOP_CACHING_NOWAIT ||
4219                             atomic_read(&space_info->caching_threads) < 2) {
4220                                 ret = cache_block_group(block_group);
4221                                 BUG_ON(ret);
4222                         }
4223                 }
4224
4225                 cached = block_group_cache_done(block_group);
4226                 if (unlikely(!cached)) {
4227                         found_uncached_bg = true;
4228
4229                         /* if we only want cached bgs, loop */
4230                         if (loop == LOOP_CACHED_ONLY)
4231                                 goto loop;
4232                 }
4233
4234                 if (unlikely(block_group->ro))
4235                         goto loop;
4236
4237                 /*
4238                  * Ok we want to try and use the cluster allocator, so lets look
4239                  * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
4240                  * have tried the cluster allocator plenty of times at this
4241                  * point and not have found anything, so we are likely way too
4242                  * fragmented for the clustering stuff to find anything, so lets
4243                  * just skip it and let the allocator find whatever block it can
4244                  * find
4245                  */
4246                 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
4247                         /*
4248                          * the refill lock keeps out other
4249                          * people trying to start a new cluster
4250                          */
4251                         spin_lock(&last_ptr->refill_lock);
4252                         if (last_ptr->block_group &&
4253                             (last_ptr->block_group->ro ||
4254                             !block_group_bits(last_ptr->block_group, data))) {
4255                                 offset = 0;
4256                                 goto refill_cluster;
4257                         }
4258
4259                         offset = btrfs_alloc_from_cluster(block_group, last_ptr,
4260                                                  num_bytes, search_start);
4261                         if (offset) {
4262                                 /* we have a block, we're done */
4263                                 spin_unlock(&last_ptr->refill_lock);
4264                                 goto checks;
4265                         }
4266
4267                         spin_lock(&last_ptr->lock);
4268                         /*
4269                          * whoops, this cluster doesn't actually point to
4270                          * this block group.  Get a ref on the block
4271                          * group is does point to and try again
4272                          */
4273                         if (!last_ptr_loop && last_ptr->block_group &&
4274                             last_ptr->block_group != block_group) {
4275
4276                                 btrfs_put_block_group(block_group);
4277                                 block_group = last_ptr->block_group;
4278                                 atomic_inc(&block_group->count);
4279                                 spin_unlock(&last_ptr->lock);
4280                                 spin_unlock(&last_ptr->refill_lock);
4281
4282                                 last_ptr_loop = 1;
4283                                 search_start = block_group->key.objectid;
4284                                 /*
4285                                  * we know this block group is properly
4286                                  * in the list because
4287                                  * btrfs_remove_block_group, drops the
4288                                  * cluster before it removes the block
4289                                  * group from the list
4290                                  */
4291                                 goto have_block_group;
4292                         }
4293                         spin_unlock(&last_ptr->lock);
4294 refill_cluster:
4295                         /*
4296                          * this cluster didn't work out, free it and
4297                          * start over
4298                          */
4299                         btrfs_return_cluster_to_free_space(NULL, last_ptr);
4300
4301                         last_ptr_loop = 0;
4302
4303                         /* allocate a cluster in this block group */
4304                         ret = btrfs_find_space_cluster(trans, root,
4305                                                block_group, last_ptr,
4306                                                offset, num_bytes,
4307                                                empty_cluster + empty_size);
4308                         if (ret == 0) {
4309                                 /*
4310                                  * now pull our allocation out of this
4311                                  * cluster
4312                                  */
4313                                 offset = btrfs_alloc_from_cluster(block_group,
4314                                                   last_ptr, num_bytes,
4315                                                   search_start);
4316                                 if (offset) {
4317                                         /* we found one, proceed */
4318                                         spin_unlock(&last_ptr->refill_lock);
4319                                         goto checks;
4320                                 }
4321                         } else if (!cached && loop > LOOP_CACHING_NOWAIT
4322                                    && !failed_cluster_refill) {
4323                                 spin_unlock(&last_ptr->refill_lock);
4324
4325                                 failed_cluster_refill = true;
4326                                 wait_block_group_cache_progress(block_group,
4327                                        num_bytes + empty_cluster + empty_size);
4328                                 goto have_block_group;
4329                         }
4330
4331                         /*
4332                          * at this point we either didn't find a cluster
4333                          * or we weren't able to allocate a block from our
4334                          * cluster.  Free the cluster we've been trying
4335                          * to use, and go to the next block group
4336                          */
4337                         btrfs_return_cluster_to_free_space(NULL, last_ptr);
4338                         spin_unlock(&last_ptr->refill_lock);
4339                         goto loop;
4340                 }
4341
4342                 offset = btrfs_find_space_for_alloc(block_group, search_start,
4343                                                     num_bytes, empty_size);
4344                 /*
4345                  * If we didn't find a chunk, and we haven't failed on this
4346                  * block group before, and this block group is in the middle of
4347                  * caching and we are ok with waiting, then go ahead and wait
4348                  * for progress to be made, and set failed_alloc to true.
4349                  *
4350                  * If failed_alloc is true then we've already waited on this
4351                  * block group once and should move on to the next block group.
4352                  */
4353                 if (!offset && !failed_alloc && !cached &&
4354                     loop > LOOP_CACHING_NOWAIT) {
4355                         wait_block_group_cache_progress(block_group,
4356                                                 num_bytes + empty_size);
4357                         failed_alloc = true;
4358                         goto have_block_group;
4359                 } else if (!offset) {
4360                         goto loop;
4361                 }
4362 checks:
4363                 search_start = stripe_align(root, offset);
4364                 /* move on to the next group */
4365                 if (search_start + num_bytes >= search_end) {
4366                         btrfs_add_free_space(block_group, offset, num_bytes);
4367                         goto loop;
4368                 }
4369
4370                 /* move on to the next group */
4371                 if (search_start + num_bytes >
4372                     block_group->key.objectid + block_group->key.offset) {
4373                         btrfs_add_free_space(block_group, offset, num_bytes);
4374                         goto loop;
4375                 }
4376
4377                 if (exclude_nr > 0 &&
4378                     (search_start + num_bytes > exclude_start &&
4379                      search_start < exclude_start + exclude_nr)) {
4380                         search_start = exclude_start + exclude_nr;
4381
4382                         btrfs_add_free_space(block_group, offset, num_bytes);
4383                         /*
4384                          * if search_start is still in this block group
4385                          * then we just re-search this block group
4386                          */
4387                         if (search_start >= block_group->key.objectid &&
4388                             search_start < (block_group->key.objectid +
4389                                             block_group->key.offset))
4390                                 goto have_block_group;
4391                         goto loop;
4392                 }
4393
4394                 ins->objectid = search_start;
4395                 ins->offset = num_bytes;
4396
4397                 if (offset < search_start)
4398                         btrfs_add_free_space(block_group, offset,
4399                                              search_start - offset);
4400                 BUG_ON(offset > search_start);
4401
4402                 update_reserved_extents(block_group, num_bytes, 1);
4403
4404                 /* we are all good, lets return */
4405                 break;
4406 loop:
4407                 failed_cluster_refill = false;
4408                 failed_alloc = false;
4409                 btrfs_put_block_group(block_group);
4410         }
4411         up_read(&space_info->groups_sem);
4412
4413         /* LOOP_CACHED_ONLY, only search fully cached block groups
4414          * LOOP_CACHING_NOWAIT, search partially cached block groups, but
4415          *                      dont wait foR them to finish caching
4416          * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
4417          * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
4418          * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
4419          *                      again
4420          */
4421         if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
4422             (found_uncached_bg || empty_size || empty_cluster ||
4423              allowed_chunk_alloc)) {
4424                 if (found_uncached_bg) {
4425                         found_uncached_bg = false;
4426                         if (loop < LOOP_CACHING_WAIT) {
4427                                 loop++;
4428                                 goto search;
4429                         }
4430                 }
4431
4432                 if (loop == LOOP_ALLOC_CHUNK) {
4433                         empty_size = 0;
4434                         empty_cluster = 0;
4435                 }
4436
4437                 if (allowed_chunk_alloc) {
4438                         ret = do_chunk_alloc(trans, root, num_bytes +
4439                                              2 * 1024 * 1024, data, 1);
4440                         allowed_chunk_alloc = 0;
4441                 } else {
4442                         space_info->force_alloc = 1;
4443                 }
4444
4445                 if (loop < LOOP_NO_EMPTY_SIZE) {
4446                         loop++;
4447                         goto search;
4448                 }
4449                 ret = -ENOSPC;
4450         } else if (!ins->objectid) {
4451                 ret = -ENOSPC;
4452         }
4453
4454         /* we found what we needed */
4455         if (ins->objectid) {
4456                 if (!(data & BTRFS_BLOCK_GROUP_DATA))
4457                         trans->block_group = block_group->key.objectid;
4458
4459                 btrfs_put_block_group(block_group);
4460                 ret = 0;
4461         }
4462
4463         return ret;
4464 }
4465
4466 static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
4467                             int dump_block_groups)
4468 {
4469         struct btrfs_block_group_cache *cache;
4470
4471         spin_lock(&info->lock);
4472         printk(KERN_INFO "space_info has %llu free, is %sfull\n",
4473                (unsigned long long)(info->total_bytes - info->bytes_used -
4474                                     info->bytes_pinned - info->bytes_reserved -
4475                                     info->bytes_super),
4476                (info->full) ? "" : "not ");
4477         printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
4478                " may_use=%llu, used=%llu, root=%llu, super=%llu, reserved=%llu"
4479                "\n",
4480                (unsigned long long)info->total_bytes,
4481                (unsigned long long)info->bytes_pinned,
4482                (unsigned long long)info->bytes_delalloc,
4483                (unsigned long long)info->bytes_may_use,
4484                (unsigned long long)info->bytes_used,
4485                (unsigned long long)info->bytes_root,
4486                (unsigned long long)info->bytes_super,
4487                (unsigned long long)info->bytes_reserved);
4488         spin_unlock(&info->lock);
4489
4490         if (!dump_block_groups)
4491                 return;
4492
4493         down_read(&info->groups_sem);
4494         list_for_each_entry(cache, &info->block_groups, list) {
4495                 spin_lock(&cache->lock);
4496                 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
4497                        "%llu pinned %llu reserved\n",
4498                        (unsigned long long)cache->key.objectid,
4499                        (unsigned long long)cache->key.offset,
4500                        (unsigned long long)btrfs_block_group_used(&cache->item),
4501                        (unsigned long long)cache->pinned,
4502                        (unsigned long long)cache->reserved);
4503                 btrfs_dump_free_space(cache, bytes);
4504                 spin_unlock(&cache->lock);
4505         }
4506         up_read(&info->groups_sem);
4507 }
4508
4509 int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
4510                          struct btrfs_root *root,
4511                          u64 num_bytes, u64 min_alloc_size,
4512                          u64 empty_size, u64 hint_byte,
4513                          u64 search_end, struct btrfs_key *ins,
4514                          u64 data)
4515 {
4516         int ret;
4517         u64 search_start = 0;
4518         struct btrfs_fs_info *info = root->fs_info;
4519
4520         data = btrfs_get_alloc_profile(root, data);
4521 again:
4522         /*
4523          * the only place that sets empty_size is btrfs_realloc_node, which
4524          * is not called recursively on allocations
4525          */
4526         if (empty_size || root->ref_cows) {
4527                 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
4528                         ret = do_chunk_alloc(trans, root->fs_info->extent_root,
4529                                      2 * 1024 * 1024,
4530                                      BTRFS_BLOCK_GROUP_METADATA |
4531                                      (info->metadata_alloc_profile &
4532                                       info->avail_metadata_alloc_bits), 0);
4533                 }
4534                 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
4535                                      num_bytes + 2 * 1024 * 1024, data, 0);
4536         }
4537
4538         WARN_ON(num_bytes < root->sectorsize);
4539         ret = find_free_extent(trans, root, num_bytes, empty_size,
4540                                search_start, search_end, hint_byte, ins,
4541                                trans->alloc_exclude_start,
4542                                trans->alloc_exclude_nr, data);
4543
4544         if (ret == -ENOSPC && num_bytes > min_alloc_size) {
4545                 num_bytes = num_bytes >> 1;
4546                 num_bytes = num_bytes & ~(root->sectorsize - 1);
4547                 num_bytes = max(num_bytes, min_alloc_size);
4548                 do_chunk_alloc(trans, root->fs_info->extent_root,
4549                                num_bytes, data, 1);
4550                 goto again;
4551         }
4552         if (ret == -ENOSPC) {
4553                 struct btrfs_space_info *sinfo;
4554
4555                 sinfo = __find_space_info(root->fs_info, data);
4556                 printk(KERN_ERR "btrfs allocation failed flags %llu, "
4557                        "wanted %llu\n", (unsigned long long)data,
4558                        (unsigned long long)num_bytes);
4559                 dump_space_info(sinfo, num_bytes, 1);
4560         }
4561
4562         return ret;
4563 }
4564
4565 int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
4566 {
4567         struct btrfs_block_group_cache *cache;
4568         int ret = 0;
4569
4570         cache = btrfs_lookup_block_group(root->fs_info, start);
4571         if (!cache) {
4572                 printk(KERN_ERR "Unable to find block group for %llu\n",
4573                        (unsigned long long)start);
4574                 return -ENOSPC;
4575         }
4576
4577         ret = btrfs_discard_extent(root, start, len);
4578
4579         btrfs_add_free_space(cache, start, len);
4580         update_reserved_extents(cache, len, 0);
4581         btrfs_put_block_group(cache);
4582
4583         return ret;
4584 }
4585
4586 static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4587                                       struct btrfs_root *root,
4588                                       u64 parent, u64 root_objectid,
4589                                       u64 flags, u64 owner, u64 offset,
4590                                       struct btrfs_key *ins, int ref_mod)
4591 {
4592         int ret;
4593         struct btrfs_fs_info *fs_info = root->fs_info;
4594         struct btrfs_extent_item *extent_item;
4595         struct btrfs_extent_inline_ref *iref;
4596         struct btrfs_path *path;
4597         struct extent_buffer *leaf;
4598         int type;
4599         u32 size;
4600
4601         if (parent > 0)
4602                 type = BTRFS_SHARED_DATA_REF_KEY;
4603         else
4604                 type = BTRFS_EXTENT_DATA_REF_KEY;
4605
4606         size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
4607
4608         path = btrfs_alloc_path();
4609         BUG_ON(!path);
4610
4611         path->leave_spinning = 1;
4612         ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4613                                       ins, size);
4614         BUG_ON(ret);
4615
4616         leaf = path->nodes[0];
4617         extent_item = btrfs_item_ptr(leaf, path->slots[0],
4618                                      struct btrfs_extent_item);
4619         btrfs_set_extent_refs(leaf, extent_item, ref_mod);
4620         btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4621         btrfs_set_extent_flags(leaf, extent_item,
4622                                flags | BTRFS_EXTENT_FLAG_DATA);
4623
4624         iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4625         btrfs_set_extent_inline_ref_type(leaf, iref, type);
4626         if (parent > 0) {
4627                 struct btrfs_shared_data_ref *ref;
4628                 ref = (struct btrfs_shared_data_ref *)(iref + 1);
4629                 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4630                 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
4631         } else {
4632                 struct btrfs_extent_data_ref *ref;
4633                 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
4634                 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
4635                 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
4636                 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
4637                 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
4638         }
4639
4640         btrfs_mark_buffer_dirty(path->nodes[0]);
4641         btrfs_free_path(path);
4642
4643         ret = update_block_group(trans, root, ins->objectid, ins->offset,
4644                                  1, 0);
4645         if (ret) {
4646                 printk(KERN_ERR "btrfs update block group failed for %llu "
4647                        "%llu\n", (unsigned long long)ins->objectid,
4648                        (unsigned long long)ins->offset);
4649                 BUG();
4650         }
4651         return ret;
4652 }
4653
4654 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
4655                                      struct btrfs_root *root,
4656                                      u64 parent, u64 root_objectid,
4657                                      u64 flags, struct btrfs_disk_key *key,
4658                                      int level, struct btrfs_key *ins)
4659 {
4660         int ret;
4661         struct btrfs_fs_info *fs_info = root->fs_info;
4662         struct btrfs_extent_item *extent_item;
4663         struct btrfs_tree_block_info *block_info;
4664         struct btrfs_extent_inline_ref *iref;
4665         struct btrfs_path *path;
4666         struct extent_buffer *leaf;
4667         u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
4668
4669         path = btrfs_alloc_path();
4670         BUG_ON(!path);
4671
4672         path->leave_spinning = 1;
4673         ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4674                                       ins, size);
4675         BUG_ON(ret);
4676
4677         leaf = path->nodes[0];
4678         extent_item = btrfs_item_ptr(leaf, path->slots[0],
4679                                      struct btrfs_extent_item);
4680         btrfs_set_extent_refs(leaf, extent_item, 1);
4681         btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4682         btrfs_set_extent_flags(leaf, extent_item,
4683                                flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
4684         block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
4685
4686         btrfs_set_tree_block_key(leaf, block_info, key);
4687         btrfs_set_tree_block_level(leaf, block_info, level);
4688
4689         iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
4690         if (parent > 0) {
4691                 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
4692                 btrfs_set_extent_inline_ref_type(leaf, iref,
4693                                                  BTRFS_SHARED_BLOCK_REF_KEY);
4694                 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4695         } else {
4696                 btrfs_set_extent_inline_ref_type(leaf, iref,
4697                                                  BTRFS_TREE_BLOCK_REF_KEY);
4698                 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
4699         }
4700
4701         btrfs_mark_buffer_dirty(leaf);
4702         btrfs_free_path(path);
4703
4704         ret = update_block_group(trans, root, ins->objectid, ins->offset,
4705                                  1, 0);
4706         if (ret) {
4707                 printk(KERN_ERR "btrfs update block group failed for %llu "
4708                        "%llu\n", (unsigned long long)ins->objectid,
4709                        (unsigned long long)ins->offset);
4710                 BUG();
4711         }
4712         return ret;
4713 }
4714
4715 int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4716                                      struct btrfs_root *root,
4717                                      u64 root_objectid, u64 owner,
4718                                      u64 offset, struct btrfs_key *ins)
4719 {
4720         int ret;
4721
4722         BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
4723
4724         ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
4725                                          0, root_objectid, owner, offset,
4726                                          BTRFS_ADD_DELAYED_EXTENT, NULL);
4727         return ret;
4728 }
4729
4730 /*
4731  * this is used by the tree logging recovery code.  It records that
4732  * an extent has been allocated and makes sure to clear the free
4733  * space cache bits as well
4734  */
4735 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
4736                                    struct btrfs_root *root,
4737                                    u64 root_objectid, u64 owner, u64 offset,
4738                                    struct btrfs_key *ins)
4739 {
4740         int ret;
4741         struct btrfs_block_group_cache *block_group;
4742         struct btrfs_caching_control *caching_ctl;
4743         u64 start = ins->objectid;
4744         u64 num_bytes = ins->offset;
4745
4746         block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
4747         cache_block_group(block_group);
4748         caching_ctl = get_caching_control(block_group);
4749
4750         if (!caching_ctl) {
4751                 BUG_ON(!block_group_cache_done(block_group));
4752                 ret = btrfs_remove_free_space(block_group, start, num_bytes);
4753                 BUG_ON(ret);
4754         } else {
4755                 mutex_lock(&caching_ctl->mutex);
4756
4757                 if (start >= caching_ctl->progress) {
4758                         ret = add_excluded_extent(root, start, num_bytes);
4759                         BUG_ON(ret);
4760                 } else if (start + num_bytes <= caching_ctl->progress) {
4761                         ret = btrfs_remove_free_space(block_group,
4762                                                       start, num_bytes);
4763                         BUG_ON(ret);
4764                 } else {
4765                         num_bytes = caching_ctl->progress - start;
4766                         ret = btrfs_remove_free_space(block_group,
4767                                                       start, num_bytes);
4768                         BUG_ON(ret);
4769
4770                         start = caching_ctl->progress;
4771                         num_bytes = ins->objectid + ins->offset -
4772                                     caching_ctl->progress;
4773                         ret = add_excluded_extent(root, start, num_bytes);
4774                         BUG_ON(ret);
4775                 }
4776
4777                 mutex_unlock(&caching_ctl->mutex);
4778                 put_caching_control(caching_ctl);
4779         }
4780
4781         update_reserved_extents(block_group, ins->offset, 1);
4782         btrfs_put_block_group(block_group);
4783         ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
4784                                          0, owner, offset, ins, 1);
4785         return ret;
4786 }
4787
4788 /*
4789  * finds a free extent and does all the dirty work required for allocation
4790  * returns the key for the extent through ins, and a tree buffer for
4791  * the first block of the extent through buf.
4792  *
4793  * returns 0 if everything worked, non-zero otherwise.
4794  */
4795 static int alloc_tree_block(struct btrfs_trans_handle *trans,
4796                             struct btrfs_root *root,
4797                             u64 num_bytes, u64 parent, u64 root_objectid,
4798                             struct btrfs_disk_key *key, int level,
4799                             u64 empty_size, u64 hint_byte, u64 search_end,
4800                             struct btrfs_key *ins)
4801 {
4802         int ret;
4803         u64 flags = 0;
4804
4805         ret = btrfs_reserve_extent(trans, root, num_bytes, num_bytes,
4806                                    empty_size, hint_byte, search_end,
4807                                    ins, 0);
4808         if (ret)
4809                 return ret;
4810
4811         if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
4812                 if (parent == 0)
4813                         parent = ins->objectid;
4814                 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
4815         } else
4816                 BUG_ON(parent > 0);
4817
4818         if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
4819                 struct btrfs_delayed_extent_op *extent_op;
4820                 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
4821                 BUG_ON(!extent_op);
4822                 if (key)
4823                         memcpy(&extent_op->key, key, sizeof(extent_op->key));
4824                 else
4825                         memset(&extent_op->key, 0, sizeof(extent_op->key));
4826                 extent_op->flags_to_set = flags;
4827                 extent_op->update_key = 1;
4828                 extent_op->update_flags = 1;
4829                 extent_op->is_data = 0;
4830
4831                 ret = btrfs_add_delayed_tree_ref(trans, ins->objectid,
4832                                         ins->offset, parent, root_objectid,
4833                                         level, BTRFS_ADD_DELAYED_EXTENT,
4834                                         extent_op);
4835                 BUG_ON(ret);
4836         }
4837         return ret;
4838 }
4839
4840 struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
4841                                             struct btrfs_root *root,
4842                                             u64 bytenr, u32 blocksize,
4843                                             int level)
4844 {
4845         struct extent_buffer *buf;
4846
4847         buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
4848         if (!buf)
4849                 return ERR_PTR(-ENOMEM);
4850         btrfs_set_header_generation(buf, trans->transid);
4851         btrfs_set_buffer_lockdep_class(buf, level);
4852         btrfs_tree_lock(buf);
4853         clean_tree_block(trans, root, buf);
4854
4855         btrfs_set_lock_blocking(buf);
4856         btrfs_set_buffer_uptodate(buf);
4857
4858         if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
4859                 set_extent_dirty(&root->dirty_log_pages, buf->start,
4860                          buf->start + buf->len - 1, GFP_NOFS);
4861         } else {
4862                 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
4863                          buf->start + buf->len - 1, GFP_NOFS);
4864         }
4865         trans->blocks_used++;
4866         /* this returns a buffer locked for blocking */
4867         return buf;
4868 }
4869
4870 /*
4871  * helper function to allocate a block for a given tree
4872  * returns the tree buffer or NULL.
4873  */
4874 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
4875                                         struct btrfs_root *root, u32 blocksize,
4876                                         u64 parent, u64 root_objectid,
4877                                         struct btrfs_disk_key *key, int level,
4878                                         u64 hint, u64 empty_size)
4879 {
4880         struct btrfs_key ins;
4881         int ret;
4882         struct extent_buffer *buf;
4883
4884         ret = alloc_tree_block(trans, root, blocksize, parent, root_objectid,
4885                                key, level, empty_size, hint, (u64)-1, &ins);
4886         if (ret) {
4887                 BUG_ON(ret > 0);
4888                 return ERR_PTR(ret);
4889         }
4890
4891         buf = btrfs_init_new_buffer(trans, root, ins.objectid,
4892                                     blocksize, level);
4893         return buf;
4894 }
4895
4896 struct walk_control {
4897         u64 refs[BTRFS_MAX_LEVEL];
4898         u64 flags[BTRFS_MAX_LEVEL];
4899         struct btrfs_key update_progress;
4900         int stage;
4901         int level;
4902         int shared_level;
4903         int update_ref;
4904         int keep_locks;
4905         int reada_slot;
4906         int reada_count;
4907 };
4908
4909 #define DROP_REFERENCE  1
4910 #define UPDATE_BACKREF  2
4911
4912 static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
4913                                      struct btrfs_root *root,
4914                                      struct walk_control *wc,
4915                                      struct btrfs_path *path)
4916 {
4917         u64 bytenr;
4918         u64 generation;
4919         u64 refs;
4920         u64 flags;
4921         u64 last = 0;
4922         u32 nritems;
4923         u32 blocksize;
4924         struct btrfs_key key;
4925         struct extent_buffer *eb;
4926         int ret;
4927         int slot;
4928         int nread = 0;
4929
4930         if (path->slots[wc->level] < wc->reada_slot) {
4931                 wc->reada_count = wc->reada_count * 2 / 3;
4932                 wc->reada_count = max(wc->reada_count, 2);
4933         } else {
4934                 wc->reada_count = wc->reada_count * 3 / 2;
4935                 wc->reada_count = min_t(int, wc->reada_count,
4936                                         BTRFS_NODEPTRS_PER_BLOCK(root));
4937         }
4938
4939         eb = path->nodes[wc->level];
4940         nritems = btrfs_header_nritems(eb);
4941         blocksize = btrfs_level_size(root, wc->level - 1);
4942
4943         for (slot = path->slots[wc->level]; slot < nritems; slot++) {
4944                 if (nread >= wc->reada_count)
4945                         break;
4946
4947                 cond_resched();
4948                 bytenr = btrfs_node_blockptr(eb, slot);
4949                 generation = btrfs_node_ptr_generation(eb, slot);
4950
4951                 if (slot == path->slots[wc->level])
4952                         goto reada;
4953
4954                 if (wc->stage == UPDATE_BACKREF &&
4955                     generation <= root->root_key.offset)
4956                         continue;
4957
4958                 /* We don't lock the tree block, it's OK to be racy here */
4959                 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
4960                                                &refs, &flags);
4961                 BUG_ON(ret);
4962                 BUG_ON(refs == 0);
4963
4964                 if (wc->stage == DROP_REFERENCE) {
4965                         if (refs == 1)
4966                                 goto reada;
4967
4968                         if (wc->level == 1 &&
4969                             (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
4970                                 continue;
4971                         if (!wc->update_ref ||
4972                             generation <= root->root_key.offset)
4973                                 continue;
4974                         btrfs_node_key_to_cpu(eb, &key, slot);
4975                         ret = btrfs_comp_cpu_keys(&key,
4976                                                   &wc->update_progress);
4977                         if (ret < 0)
4978                                 continue;
4979                 } else {
4980                         if (wc->level == 1 &&
4981                             (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
4982                                 continue;
4983                 }
4984 reada:
4985                 ret = readahead_tree_block(root, bytenr, blocksize,
4986                                            generation);
4987                 if (ret)
4988                         break;
4989                 last = bytenr + blocksize;
4990                 nread++;
4991         }
4992         wc->reada_slot = slot;
4993 }
4994
4995 /*
4996  * hepler to process tree block while walking down the tree.
4997  *
4998  * when wc->stage == UPDATE_BACKREF, this function updates
4999  * back refs for pointers in the block.
5000  *
5001  * NOTE: return value 1 means we should stop walking down.
5002  */
5003 static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
5004                                    struct btrfs_root *root,
5005                                    struct btrfs_path *path,
5006                                    struct walk_control *wc, int lookup_info)
5007 {
5008         int level = wc->level;
5009         struct extent_buffer *eb = path->nodes[level];
5010         u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5011         int ret;
5012
5013         if (wc->stage == UPDATE_BACKREF &&
5014             btrfs_header_owner(eb) != root->root_key.objectid)
5015                 return 1;
5016
5017         /*
5018          * when reference count of tree block is 1, it won't increase
5019          * again. once full backref flag is set, we never clear it.
5020          */
5021         if (lookup_info &&
5022             ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
5023              (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
5024                 BUG_ON(!path->locks[level]);
5025                 ret = btrfs_lookup_extent_info(trans, root,
5026                                                eb->start, eb->len,
5027                                                &wc->refs[level],
5028                                                &wc->flags[level]);
5029                 BUG_ON(ret);
5030                 BUG_ON(wc->refs[level] == 0);
5031         }
5032
5033         if (wc->stage == DROP_REFERENCE) {
5034                 if (wc->refs[level] > 1)
5035                         return 1;
5036
5037                 if (path->locks[level] && !wc->keep_locks) {
5038                         btrfs_tree_unlock(eb);
5039                         path->locks[level] = 0;
5040                 }
5041                 return 0;
5042         }
5043
5044         /* wc->stage == UPDATE_BACKREF */
5045         if (!(wc->flags[level] & flag)) {
5046                 BUG_ON(!path->locks[level]);
5047                 ret = btrfs_inc_ref(trans, root, eb, 1);
5048                 BUG_ON(ret);
5049                 ret = btrfs_dec_ref(trans, root, eb, 0);
5050                 BUG_ON(ret);
5051                 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
5052                                                   eb->len, flag, 0);
5053                 BUG_ON(ret);
5054                 wc->flags[level] |= flag;
5055         }
5056
5057         /*
5058          * the block is shared by multiple trees, so it's not good to
5059          * keep the tree lock
5060          */
5061         if (path->locks[level] && level > 0) {
5062                 btrfs_tree_unlock(eb);
5063                 path->locks[level] = 0;
5064         }
5065         return 0;
5066 }
5067
5068 /*
5069  * hepler to process tree block pointer.
5070  *
5071  * when wc->stage == DROP_REFERENCE, this function checks
5072  * reference count of the block pointed to. if the block
5073  * is shared and we need update back refs for the subtree
5074  * rooted at the block, this function changes wc->stage to
5075  * UPDATE_BACKREF. if the block is shared and there is no
5076  * need to update back, this function drops the reference
5077  * to the block.
5078  *
5079  * NOTE: return value 1 means we should stop walking down.
5080  */
5081 static noinline int do_walk_down(struct btrfs_trans_handle *trans,
5082                                  struct btrfs_root *root,
5083                                  struct btrfs_path *path,
5084                                  struct walk_control *wc, int *lookup_info)
5085 {
5086         u64 bytenr;
5087         u64 generation;
5088         u64 parent;
5089         u32 blocksize;
5090         struct btrfs_key key;
5091         struct extent_buffer *next;
5092         int level = wc->level;
5093         int reada = 0;
5094         int ret = 0;
5095
5096         generation = btrfs_node_ptr_generation(path->nodes[level],
5097                                                path->slots[level]);
5098         /*
5099          * if the lower level block was created before the snapshot
5100          * was created, we know there is no need to update back refs
5101          * for the subtree
5102          */
5103         if (wc->stage == UPDATE_BACKREF &&
5104             generation <= root->root_key.offset) {
5105                 *lookup_info = 1;
5106                 return 1;
5107         }
5108
5109         bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
5110         blocksize = btrfs_level_size(root, level - 1);
5111
5112         next = btrfs_find_tree_block(root, bytenr, blocksize);
5113         if (!next) {
5114                 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
5115                 reada = 1;
5116         }
5117         btrfs_tree_lock(next);
5118         btrfs_set_lock_blocking(next);
5119
5120         ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5121                                        &wc->refs[level - 1],
5122                                        &wc->flags[level - 1]);
5123         BUG_ON(ret);
5124         BUG_ON(wc->refs[level - 1] == 0);
5125         *lookup_info = 0;
5126
5127         if (wc->stage == DROP_REFERENCE) {
5128                 if (wc->refs[level - 1] > 1) {
5129                         if (level == 1 &&
5130                             (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5131                                 goto skip;
5132
5133                         if (!wc->update_ref ||
5134                             generation <= root->root_key.offset)
5135                                 goto skip;
5136
5137                         btrfs_node_key_to_cpu(path->nodes[level], &key,
5138                                               path->slots[level]);
5139                         ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
5140                         if (ret < 0)
5141                                 goto skip;
5142
5143                         wc->stage = UPDATE_BACKREF;
5144                         wc->shared_level = level - 1;
5145                 }
5146         } else {
5147                 if (level == 1 &&
5148                     (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5149                         goto skip;
5150         }
5151
5152         if (!btrfs_buffer_uptodate(next, generation)) {
5153                 btrfs_tree_unlock(next);
5154                 free_extent_buffer(next);
5155                 next = NULL;
5156                 *lookup_info = 1;
5157         }
5158
5159         if (!next) {
5160                 if (reada && level == 1)
5161                         reada_walk_down(trans, root, wc, path);
5162                 next = read_tree_block(root, bytenr, blocksize, generation);
5163                 btrfs_tree_lock(next);
5164                 btrfs_set_lock_blocking(next);
5165         }
5166
5167         level--;
5168         BUG_ON(level != btrfs_header_level(next));
5169         path->nodes[level] = next;
5170         path->slots[level] = 0;
5171         path->locks[level] = 1;
5172         wc->level = level;
5173         if (wc->level == 1)
5174                 wc->reada_slot = 0;
5175         return 0;
5176 skip:
5177         wc->refs[level - 1] = 0;
5178         wc->flags[level - 1] = 0;
5179         if (wc->stage == DROP_REFERENCE) {
5180                 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
5181                         parent = path->nodes[level]->start;
5182                 } else {
5183                         BUG_ON(root->root_key.objectid !=
5184                                btrfs_header_owner(path->nodes[level]));
5185                         parent = 0;
5186                 }
5187
5188                 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
5189                                         root->root_key.objectid, level - 1, 0);
5190                 BUG_ON(ret);
5191         }
5192         btrfs_tree_unlock(next);
5193         free_extent_buffer(next);
5194         *lookup_info = 1;
5195         return 1;
5196 }
5197
5198 /*
5199  * hepler to process tree block while walking up the tree.
5200  *
5201  * when wc->stage == DROP_REFERENCE, this function drops
5202  * reference count on the block.
5203  *
5204  * when wc->stage == UPDATE_BACKREF, this function changes
5205  * wc->stage back to DROP_REFERENCE if we changed wc->stage
5206  * to UPDATE_BACKREF previously while processing the block.
5207  *
5208  * NOTE: return value 1 means we should stop walking up.
5209  */
5210 static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
5211                                  struct btrfs_root *root,
5212                                  struct btrfs_path *path,
5213                                  struct walk_control *wc)
5214 {
5215         int ret = 0;
5216         int level = wc->level;
5217         struct extent_buffer *eb = path->nodes[level];
5218         u64 parent = 0;
5219
5220         if (wc->stage == UPDATE_BACKREF) {
5221                 BUG_ON(wc->shared_level < level);
5222                 if (level < wc->shared_level)
5223                         goto out;
5224
5225                 ret = find_next_key(path, level + 1, &wc->update_progress);
5226                 if (ret > 0)
5227                         wc->update_ref = 0;
5228
5229                 wc->stage = DROP_REFERENCE;
5230                 wc->shared_level = -1;
5231                 path->slots[level] = 0;
5232
5233                 /*
5234                  * check reference count again if the block isn't locked.
5235                  * we should start walking down the tree again if reference
5236                  * count is one.
5237                  */
5238                 if (!path->locks[level]) {
5239                         BUG_ON(level == 0);
5240                         btrfs_tree_lock(eb);
5241                         btrfs_set_lock_blocking(eb);
5242                         path->locks[level] = 1;
5243
5244                         ret = btrfs_lookup_extent_info(trans, root,
5245                                                        eb->start, eb->len,
5246                                                        &wc->refs[level],
5247                                                        &wc->flags[level]);
5248                         BUG_ON(ret);
5249                         BUG_ON(wc->refs[level] == 0);
5250                         if (wc->refs[level] == 1) {
5251                                 btrfs_tree_unlock(eb);
5252                                 path->locks[level] = 0;
5253                                 return 1;
5254                         }
5255                 }
5256         }
5257
5258         /* wc->stage == DROP_REFERENCE */
5259         BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
5260
5261         if (wc->refs[level] == 1) {
5262                 if (level == 0) {
5263                         if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5264                                 ret = btrfs_dec_ref(trans, root, eb, 1);
5265                         else
5266                                 ret = btrfs_dec_ref(trans, root, eb, 0);
5267                         BUG_ON(ret);
5268                 }
5269                 /* make block locked assertion in clean_tree_block happy */
5270                 if (!path->locks[level] &&
5271                     btrfs_header_generation(eb) == trans->transid) {
5272                         btrfs_tree_lock(eb);
5273                         btrfs_set_lock_blocking(eb);
5274                         path->locks[level] = 1;
5275                 }
5276                 clean_tree_block(trans, root, eb);
5277         }
5278
5279         if (eb == root->node) {
5280                 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5281                         parent = eb->start;
5282                 else
5283                         BUG_ON(root->root_key.objectid !=
5284                                btrfs_header_owner(eb));
5285         } else {
5286                 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5287                         parent = path->nodes[level + 1]->start;
5288                 else
5289                         BUG_ON(root->root_key.objectid !=
5290                                btrfs_header_owner(path->nodes[level + 1]));
5291         }
5292
5293         ret = btrfs_free_extent(trans, root, eb->start, eb->len, parent,
5294                                 root->root_key.objectid, level, 0);
5295         BUG_ON(ret);
5296 out:
5297         wc->refs[level] = 0;
5298         wc->flags[level] = 0;
5299         return ret;
5300 }
5301
5302 static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
5303                                    struct btrfs_root *root,
5304                                    struct btrfs_path *path,
5305                                    struct walk_control *wc)
5306 {
5307         int level = wc->level;
5308         int lookup_info = 1;
5309         int ret;
5310
5311         while (level >= 0) {
5312                 if (path->slots[level] >=
5313                     btrfs_header_nritems(path->nodes[level]))
5314                         break;
5315
5316                 ret = walk_down_proc(trans, root, path, wc, lookup_info);
5317                 if (ret > 0)
5318                         break;
5319
5320                 if (level == 0)
5321                         break;
5322
5323                 ret = do_walk_down(trans, root, path, wc, &lookup_info);
5324                 if (ret > 0) {
5325                         path->slots[level]++;
5326                         continue;
5327                 }
5328                 level = wc->level;
5329         }
5330         return 0;
5331 }
5332
5333 static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
5334                                  struct btrfs_root *root,
5335                                  struct btrfs_path *path,
5336                                  struct walk_control *wc, int max_level)
5337 {
5338         int level = wc->level;
5339         int ret;
5340
5341         path->slots[level] = btrfs_header_nritems(path->nodes[level]);
5342         while (level < max_level && path->nodes[level]) {
5343                 wc->level = level;
5344                 if (path->slots[level] + 1 <
5345                     btrfs_header_nritems(path->nodes[level])) {
5346                         path->slots[level]++;
5347                         return 0;
5348                 } else {
5349                         ret = walk_up_proc(trans, root, path, wc);
5350                         if (ret > 0)
5351                                 return 0;
5352
5353                         if (path->locks[level]) {
5354                                 btrfs_tree_unlock(path->nodes[level]);
5355                                 path->locks[level] = 0;
5356                         }
5357                         free_extent_buffer(path->nodes[level]);
5358                         path->nodes[level] = NULL;
5359                         level++;
5360                 }
5361         }
5362         return 1;
5363 }
5364
5365 /*
5366  * drop a subvolume tree.
5367  *
5368  * this function traverses the tree freeing any blocks that only
5369  * referenced by the tree.
5370  *
5371  * when a shared tree block is found. this function decreases its
5372  * reference count by one. if update_ref is true, this function
5373  * also make sure backrefs for the shared block and all lower level
5374  * blocks are properly updated.
5375  */
5376 int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref)
5377 {
5378         struct btrfs_path *path;
5379         struct btrfs_trans_handle *trans;
5380         struct btrfs_root *tree_root = root->fs_info->tree_root;
5381         struct btrfs_root_item *root_item = &root->root_item;
5382         struct walk_control *wc;
5383         struct btrfs_key key;
5384         int err = 0;
5385         int ret;
5386         int level;
5387
5388         path = btrfs_alloc_path();
5389         BUG_ON(!path);
5390
5391         wc = kzalloc(sizeof(*wc), GFP_NOFS);
5392         BUG_ON(!wc);
5393
5394         trans = btrfs_start_transaction(tree_root, 1);
5395
5396         if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
5397                 level = btrfs_header_level(root->node);
5398                 path->nodes[level] = btrfs_lock_root_node(root);
5399                 btrfs_set_lock_blocking(path->nodes[level]);
5400                 path->slots[level] = 0;
5401                 path->locks[level] = 1;
5402                 memset(&wc->update_progress, 0,
5403                        sizeof(wc->update_progress));
5404         } else {
5405                 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
5406                 memcpy(&wc->update_progress, &key,
5407                        sizeof(wc->update_progress));
5408
5409                 level = root_item->drop_level;
5410                 BUG_ON(level == 0);
5411                 path->lowest_level = level;
5412                 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5413                 path->lowest_level = 0;
5414                 if (ret < 0) {
5415                         err = ret;
5416                         goto out;
5417                 }
5418                 WARN_ON(ret > 0);
5419
5420                 /*
5421                  * unlock our path, this is safe because only this
5422                  * function is allowed to delete this snapshot
5423                  */
5424                 btrfs_unlock_up_safe(path, 0);
5425
5426                 level = btrfs_header_level(root->node);
5427                 while (1) {
5428                         btrfs_tree_lock(path->nodes[level]);
5429                         btrfs_set_lock_blocking(path->nodes[level]);
5430
5431                         ret = btrfs_lookup_extent_info(trans, root,
5432                                                 path->nodes[level]->start,
5433                                                 path->nodes[level]->len,
5434                                                 &wc->refs[level],
5435                                                 &wc->flags[level]);
5436                         BUG_ON(ret);
5437                         BUG_ON(wc->refs[level] == 0);
5438
5439                         if (level == root_item->drop_level)
5440                                 break;
5441
5442                         btrfs_tree_unlock(path->nodes[level]);
5443                         WARN_ON(wc->refs[level] != 1);
5444                         level--;
5445                 }
5446         }
5447
5448         wc->level = level;
5449         wc->shared_level = -1;
5450         wc->stage = DROP_REFERENCE;
5451         wc->update_ref = update_ref;
5452         wc->keep_locks = 0;
5453         wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
5454
5455         while (1) {
5456                 ret = walk_down_tree(trans, root, path, wc);
5457                 if (ret < 0) {
5458                         err = ret;
5459                         break;
5460                 }
5461
5462                 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
5463                 if (ret < 0) {
5464                         err = ret;
5465                         break;
5466                 }
5467
5468                 if (ret > 0) {
5469                         BUG_ON(wc->stage != DROP_REFERENCE);
5470                         break;
5471                 }
5472
5473                 if (wc->stage == DROP_REFERENCE) {
5474                         level = wc->level;
5475                         btrfs_node_key(path->nodes[level],
5476                                        &root_item->drop_progress,
5477                                        path->slots[level]);
5478                         root_item->drop_level = level;
5479                 }
5480
5481                 BUG_ON(wc->level == 0);
5482                 if (trans->transaction->in_commit ||
5483                     trans->transaction->delayed_refs.flushing) {
5484                         ret = btrfs_update_root(trans, tree_root,
5485                                                 &root->root_key,
5486                                                 root_item);
5487                         BUG_ON(ret);
5488
5489                         btrfs_end_transaction(trans, tree_root);
5490                         trans = btrfs_start_transaction(tree_root, 1);
5491                 } else {
5492                         unsigned long update;
5493                         update = trans->delayed_ref_updates;
5494                         trans->delayed_ref_updates = 0;
5495                         if (update)
5496                                 btrfs_run_delayed_refs(trans, tree_root,
5497                                                        update);
5498                 }
5499         }
5500         btrfs_release_path(root, path);
5501         BUG_ON(err);
5502
5503         ret = btrfs_del_root(trans, tree_root, &root->root_key);
5504         BUG_ON(ret);
5505
5506         if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
5507                 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
5508                                            NULL, NULL);
5509                 BUG_ON(ret < 0);
5510                 if (ret > 0) {
5511                         ret = btrfs_del_orphan_item(trans, tree_root,
5512                                                     root->root_key.objectid);
5513                         BUG_ON(ret);
5514                 }
5515         }
5516
5517         if (root->in_radix) {
5518                 btrfs_free_fs_root(tree_root->fs_info, root);
5519         } else {
5520                 free_extent_buffer(root->node);
5521                 free_extent_buffer(root->commit_root);
5522                 kfree(root);
5523         }
5524 out:
5525         btrfs_end_transaction(trans, tree_root);
5526         kfree(wc);
5527         btrfs_free_path(path);
5528         return err;
5529 }
5530
5531 /*
5532  * drop subtree rooted at tree block 'node'.
5533  *
5534  * NOTE: this function will unlock and release tree block 'node'
5535  */
5536 int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
5537                         struct btrfs_root *root,
5538                         struct extent_buffer *node,
5539                         struct extent_buffer *parent)
5540 {
5541         struct btrfs_path *path;
5542         struct walk_control *wc;
5543         int level;
5544         int parent_level;
5545         int ret = 0;
5546         int wret;
5547
5548         BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
5549
5550         path = btrfs_alloc_path();
5551         BUG_ON(!path);
5552
5553         wc = kzalloc(sizeof(*wc), GFP_NOFS);
5554         BUG_ON(!wc);
5555
5556         btrfs_assert_tree_locked(parent);
5557         parent_level = btrfs_header_level(parent);
5558         extent_buffer_get(parent);
5559         path->nodes[parent_level] = parent;
5560         path->slots[parent_level] = btrfs_header_nritems(parent);
5561
5562         btrfs_assert_tree_locked(node);
5563         level = btrfs_header_level(node);
5564         path->nodes[level] = node;
5565         path->slots[level] = 0;
5566         path->locks[level] = 1;
5567
5568         wc->refs[parent_level] = 1;
5569         wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5570         wc->level = level;
5571         wc->shared_level = -1;
5572         wc->stage = DROP_REFERENCE;
5573         wc->update_ref = 0;
5574         wc->keep_locks = 1;
5575         wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
5576
5577         while (1) {
5578                 wret = walk_down_tree(trans, root, path, wc);
5579                 if (wret < 0) {
5580                         ret = wret;
5581                         break;
5582                 }
5583
5584                 wret = walk_up_tree(trans, root, path, wc, parent_level);
5585                 if (wret < 0)
5586                         ret = wret;
5587                 if (wret != 0)
5588                         break;
5589         }
5590
5591         kfree(wc);
5592         btrfs_free_path(path);
5593         return ret;
5594 }
5595
5596 #if 0
5597 static unsigned long calc_ra(unsigned long start, unsigned long last,
5598                              unsigned long nr)
5599 {
5600         return min(last, start + nr - 1);
5601 }
5602
5603 static noinline int relocate_inode_pages(struct inode *inode, u64 start,
5604                                          u64 len)
5605 {
5606         u64 page_start;
5607         u64 page_end;
5608         unsigned long first_index;
5609         unsigned long last_index;
5610         unsigned long i;
5611         struct page *page;
5612         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5613         struct file_ra_state *ra;
5614         struct btrfs_ordered_extent *ordered;
5615         unsigned int total_read = 0;
5616         unsigned int total_dirty = 0;
5617         int ret = 0;
5618
5619         ra = kzalloc(sizeof(*ra), GFP_NOFS);
5620
5621         mutex_lock(&inode->i_mutex);
5622         first_index = start >> PAGE_CACHE_SHIFT;
5623         last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
5624
5625         /* make sure the dirty trick played by the caller work */
5626         ret = invalidate_inode_pages2_range(inode->i_mapping,
5627                                             first_index, last_index);
5628         if (ret)
5629                 goto out_unlock;
5630
5631         file_ra_state_init(ra, inode->i_mapping);
5632
5633         for (i = first_index ; i <= last_index; i++) {
5634                 if (total_read % ra->ra_pages == 0) {
5635                         btrfs_force_ra(inode->i_mapping, ra, NULL, i,
5636                                        calc_ra(i, last_index, ra->ra_pages));
5637                 }
5638                 total_read++;
5639 again:
5640                 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
5641                         BUG_ON(1);
5642                 page = grab_cache_page(inode->i_mapping, i);
5643                 if (!page) {
5644                         ret = -ENOMEM;
5645                         goto out_unlock;
5646                 }
5647                 if (!PageUptodate(page)) {
5648                         btrfs_readpage(NULL, page);
5649                         lock_page(page);
5650                         if (!PageUptodate(page)) {
5651                                 unlock_page(page);
5652                                 page_cache_release(page);
5653                                 ret = -EIO;
5654                                 goto out_unlock;
5655                         }
5656                 }
5657                 wait_on_page_writeback(page);
5658
5659                 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
5660                 page_end = page_start + PAGE_CACHE_SIZE - 1;
5661                 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
5662
5663                 ordered = btrfs_lookup_ordered_extent(inode, page_start);
5664                 if (ordered) {
5665                         unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5666                         unlock_page(page);
5667                         page_cache_release(page);
5668                         btrfs_start_ordered_extent(inode, ordered, 1);
5669                         btrfs_put_ordered_extent(ordered);
5670                         goto again;
5671                 }
5672                 set_page_extent_mapped(page);
5673
5674                 if (i == first_index)
5675                         set_extent_bits(io_tree, page_start, page_end,
5676                                         EXTENT_BOUNDARY, GFP_NOFS);
5677                 btrfs_set_extent_delalloc(inode, page_start, page_end);
5678
5679                 set_page_dirty(page);
5680                 total_dirty++;
5681
5682                 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5683                 unlock_page(page);
5684                 page_cache_release(page);
5685         }
5686
5687 out_unlock:
5688         kfree(ra);
5689         mutex_unlock(&inode->i_mutex);
5690         balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
5691         return ret;
5692 }
5693
5694 static noinline int relocate_data_extent(struct inode *reloc_inode,
5695                                          struct btrfs_key *extent_key,
5696                                          u64 offset)
5697 {
5698         struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
5699         struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
5700         struct extent_map *em;
5701         u64 start = extent_key->objectid - offset;
5702         u64 end = start + extent_key->offset - 1;
5703
5704         em = alloc_extent_map(GFP_NOFS);
5705         BUG_ON(!em || IS_ERR(em));
5706
5707         em->start = start;
5708         em->len = extent_key->offset;
5709         em->block_len = extent_key->offset;
5710         em->block_start = extent_key->objectid;
5711         em->bdev = root->fs_info->fs_devices->latest_bdev;
5712         set_bit(EXTENT_FLAG_PINNED, &em->flags);
5713
5714         /* setup extent map to cheat btrfs_readpage */
5715         lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
5716         while (1) {
5717                 int ret;
5718                 write_lock(&em_tree->lock);
5719                 ret = add_extent_mapping(em_tree, em);
5720                 write_unlock(&em_tree->lock);
5721                 if (ret != -EEXIST) {
5722                         free_extent_map(em);
5723                         break;
5724                 }
5725                 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
5726         }
5727         unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
5728
5729         return relocate_inode_pages(reloc_inode, start, extent_key->offset);
5730 }
5731
5732 struct btrfs_ref_path {
5733         u64 extent_start;
5734         u64 nodes[BTRFS_MAX_LEVEL];
5735         u64 root_objectid;
5736         u64 root_generation;
5737         u64 owner_objectid;
5738         u32 num_refs;
5739         int lowest_level;
5740         int current_level;
5741         int shared_level;
5742
5743         struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
5744         u64 new_nodes[BTRFS_MAX_LEVEL];
5745 };
5746
5747 struct disk_extent {
5748         u64 ram_bytes;
5749         u64 disk_bytenr;
5750         u64 disk_num_bytes;
5751         u64 offset;
5752         u64 num_bytes;
5753         u8 compression;
5754         u8 encryption;
5755         u16 other_encoding;
5756 };
5757
5758 static int is_cowonly_root(u64 root_objectid)
5759 {
5760         if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
5761             root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
5762             root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
5763             root_objectid == BTRFS_DEV_TREE_OBJECTID ||
5764             root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5765             root_objectid == BTRFS_CSUM_TREE_OBJECTID)
5766                 return 1;
5767         return 0;
5768 }
5769
5770 static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
5771                                     struct btrfs_root *extent_root,
5772                                     struct btrfs_ref_path *ref_path,
5773                                     int first_time)
5774 {
5775         struct extent_buffer *leaf;
5776         struct btrfs_path *path;
5777         struct btrfs_extent_ref *ref;
5778         struct btrfs_key key;
5779         struct btrfs_key found_key;
5780         u64 bytenr;
5781         u32 nritems;
5782         int level;
5783         int ret = 1;
5784
5785         path = btrfs_alloc_path();
5786         if (!path)
5787                 return -ENOMEM;
5788
5789         if (first_time) {
5790                 ref_path->lowest_level = -1;
5791                 ref_path->current_level = -1;
5792                 ref_path->shared_level = -1;
5793                 goto walk_up;
5794         }
5795 walk_down:
5796         level = ref_path->current_level - 1;
5797         while (level >= -1) {
5798                 u64 parent;
5799                 if (level < ref_path->lowest_level)
5800                         break;
5801
5802                 if (level >= 0)
5803                         bytenr = ref_path->nodes[level];
5804                 else
5805                         bytenr = ref_path->extent_start;
5806                 BUG_ON(bytenr == 0);
5807
5808                 parent = ref_path->nodes[level + 1];
5809                 ref_path->nodes[level + 1] = 0;
5810                 ref_path->current_level = level;
5811                 BUG_ON(parent == 0);
5812
5813                 key.objectid = bytenr;
5814                 key.offset = parent + 1;
5815                 key.type = BTRFS_EXTENT_REF_KEY;
5816
5817                 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
5818                 if (ret < 0)
5819                         goto out;
5820                 BUG_ON(ret == 0);
5821
5822                 leaf = path->nodes[0];
5823                 nritems = btrfs_header_nritems(leaf);
5824                 if (path->slots[0] >= nritems) {
5825                         ret = btrfs_next_leaf(extent_root, path);
5826                         if (ret < 0)
5827                                 goto out;
5828                         if (ret > 0)
5829                                 goto next;
5830                         leaf = path->nodes[0];
5831                 }
5832
5833                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5834                 if (found_key.objectid == bytenr &&
5835                     found_key.type == BTRFS_EXTENT_REF_KEY) {
5836                         if (level < ref_path->shared_level)
5837                                 ref_path->shared_level = level;
5838                         goto found;
5839                 }
5840 next:
5841                 level--;
5842                 btrfs_release_path(extent_root, path);
5843                 cond_resched();
5844         }
5845         /* reached lowest level */
5846         ret = 1;
5847         goto out;
5848 walk_up:
5849         level = ref_path->current_level;
5850         while (level < BTRFS_MAX_LEVEL - 1) {
5851                 u64 ref_objectid;
5852
5853                 if (level >= 0)
5854                         bytenr = ref_path->nodes[level];
5855                 else
5856                         bytenr = ref_path->extent_start;
5857
5858                 BUG_ON(bytenr == 0);
5859
5860                 key.objectid = bytenr;
5861                 key.offset = 0;
5862                 key.type = BTRFS_EXTENT_REF_KEY;
5863
5864                 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
5865                 if (ret < 0)
5866                         goto out;
5867
5868                 leaf = path->nodes[0];
5869                 nritems = btrfs_header_nritems(leaf);
5870                 if (path->slots[0] >= nritems) {
5871                         ret = btrfs_next_leaf(extent_root, path);
5872                         if (ret < 0)
5873                                 goto out;
5874                         if (ret > 0) {
5875                                 /* the extent was freed by someone */
5876                                 if (ref_path->lowest_level == level)
5877                                         goto out;
5878                                 btrfs_release_path(extent_root, path);
5879                                 goto walk_down;
5880                         }
5881                         leaf = path->nodes[0];
5882                 }
5883
5884                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5885                 if (found_key.objectid != bytenr ||
5886                                 found_key.type != BTRFS_EXTENT_REF_KEY) {
5887                         /* the extent was freed by someone */
5888                         if (ref_path->lowest_level == level) {
5889                                 ret = 1;
5890                                 goto out;
5891                         }
5892                         btrfs_release_path(extent_root, path);
5893                         goto walk_down;
5894                 }
5895 found:
5896                 ref = btrfs_item_ptr(leaf, path->slots[0],
5897                                 struct btrfs_extent_ref);
5898                 ref_objectid = btrfs_ref_objectid(leaf, ref);
5899                 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5900                         if (first_time) {
5901                                 level = (int)ref_objectid;
5902                                 BUG_ON(level >= BTRFS_MAX_LEVEL);
5903                                 ref_path->lowest_level = level;
5904                                 ref_path->current_level = level;
5905                                 ref_path->nodes[level] = bytenr;
5906                         } else {
5907                                 WARN_ON(ref_objectid != level);
5908                         }
5909                 } else {
5910                         WARN_ON(level != -1);
5911                 }
5912                 first_time = 0;
5913
5914                 if (ref_path->lowest_level == level) {
5915                         ref_path->owner_objectid = ref_objectid;
5916                         ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
5917                 }
5918
5919                 /*
5920                  * the block is tree root or the block isn't in reference
5921                  * counted tree.
5922                  */
5923                 if (found_key.objectid == found_key.offset ||
5924                     is_cowonly_root(btrfs_ref_root(leaf, ref))) {
5925                         ref_path->root_objectid = btrfs_ref_root(leaf, ref);
5926                         ref_path->root_generation =
5927                                 btrfs_ref_generation(leaf, ref);
5928                         if (level < 0) {
5929                                 /* special reference from the tree log */
5930                                 ref_path->nodes[0] = found_key.offset;
5931                                 ref_path->current_level = 0;
5932                         }
5933                         ret = 0;
5934                         goto out;
5935                 }
5936
5937                 level++;
5938                 BUG_ON(ref_path->nodes[level] != 0);
5939                 ref_path->nodes[level] = found_key.offset;
5940                 ref_path->current_level = level;
5941
5942                 /*
5943                  * the reference was created in the running transaction,
5944                  * no need to continue walking up.
5945                  */
5946                 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
5947                         ref_path->root_objectid = btrfs_ref_root(leaf, ref);
5948                         ref_path->root_generation =
5949                                 btrfs_ref_generation(leaf, ref);
5950                         ret = 0;
5951                         goto out;
5952                 }
5953
5954                 btrfs_release_path(extent_root, path);
5955                 cond_resched();
5956         }
5957         /* reached max tree level, but no tree root found. */
5958         BUG();
5959 out:
5960         btrfs_free_path(path);
5961         return ret;
5962 }
5963
5964 static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
5965                                 struct btrfs_root *extent_root,
5966                                 struct btrfs_ref_path *ref_path,
5967                                 u64 extent_start)
5968 {
5969         memset(ref_path, 0, sizeof(*ref_path));
5970         ref_path->extent_start = extent_start;
5971
5972         return __next_ref_path(trans, extent_root, ref_path, 1);
5973 }
5974
5975 static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
5976                                struct btrfs_root *extent_root,
5977                                struct btrfs_ref_path *ref_path)
5978 {
5979         return __next_ref_path(trans, extent_root, ref_path, 0);
5980 }
5981
5982 static noinline int get_new_locations(struct inode *reloc_inode,
5983                                       struct btrfs_key *extent_key,
5984                                       u64 offset, int no_fragment,
5985                                       struct disk_extent **extents,
5986                                       int *nr_extents)
5987 {
5988         struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
5989         struct btrfs_path *path;
5990         struct btrfs_file_extent_item *fi;
5991         struct extent_buffer *leaf;
5992         struct disk_extent *exts = *extents;
5993         struct btrfs_key found_key;
5994         u64 cur_pos;
5995         u64 last_byte;
5996         u32 nritems;
5997         int nr = 0;
5998         int max = *nr_extents;
5999         int ret;
6000
6001         WARN_ON(!no_fragment && *extents);
6002         if (!exts) {
6003                 max = 1;
6004                 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
6005                 if (!exts)
6006                         return -ENOMEM;
6007         }
6008
6009         path = btrfs_alloc_path();
6010         BUG_ON(!path);
6011
6012         cur_pos = extent_key->objectid - offset;
6013         last_byte = extent_key->objectid + extent_key->offset;
6014         ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
6015                                        cur_pos, 0);
6016         if (ret < 0)
6017                 goto out;
6018         if (ret > 0) {
6019                 ret = -ENOENT;
6020                 goto out;
6021         }
6022
6023         while (1) {
6024                 leaf = path->nodes[0];
6025                 nritems = btrfs_header_nritems(leaf);
6026                 if (path->slots[0] >= nritems) {
6027                         ret = btrfs_next_leaf(root, path);
6028                         if (ret < 0)
6029                                 goto out;
6030                         if (ret > 0)
6031                                 break;
6032                         leaf = path->nodes[0];
6033                 }
6034
6035                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6036                 if (found_key.offset != cur_pos ||
6037                     found_key.type != BTRFS_EXTENT_DATA_KEY ||
6038                     found_key.objectid != reloc_inode->i_ino)
6039                         break;
6040
6041                 fi = btrfs_item_ptr(leaf, path->slots[0],
6042                                     struct btrfs_file_extent_item);
6043                 if (btrfs_file_extent_type(leaf, fi) !=
6044                     BTRFS_FILE_EXTENT_REG ||
6045                     btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6046                         break;
6047
6048                 if (nr == max) {
6049                         struct disk_extent *old = exts;
6050                         max *= 2;
6051                         exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
6052                         memcpy(exts, old, sizeof(*exts) * nr);
6053                         if (old != *extents)
6054                                 kfree(old);
6055                 }
6056
6057                 exts[nr].disk_bytenr =
6058                         btrfs_file_extent_disk_bytenr(leaf, fi);
6059                 exts[nr].disk_num_bytes =
6060                         btrfs_file_extent_disk_num_bytes(leaf, fi);
6061                 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
6062                 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6063                 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6064                 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
6065                 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
6066                 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
6067                                                                            fi);
6068                 BUG_ON(exts[nr].offset > 0);
6069                 BUG_ON(exts[nr].compression || exts[nr].encryption);
6070                 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
6071
6072                 cur_pos += exts[nr].num_bytes;
6073                 nr++;
6074
6075                 if (cur_pos + offset >= last_byte)
6076                         break;
6077
6078                 if (no_fragment) {
6079                         ret = 1;
6080                         goto out;
6081                 }
6082                 path->slots[0]++;
6083         }
6084
6085         BUG_ON(cur_pos + offset > last_byte);
6086         if (cur_pos + offset < last_byte) {
6087                 ret = -ENOENT;
6088                 goto out;
6089         }
6090         ret = 0;
6091 out:
6092         btrfs_free_path(path);
6093         if (ret) {
6094                 if (exts != *extents)
6095                         kfree(exts);
6096         } else {
6097                 *extents = exts;
6098                 *nr_extents = nr;
6099         }
6100         return ret;
6101 }
6102
6103 static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
6104                                         struct btrfs_root *root,
6105                                         struct btrfs_path *path,
6106                                         struct btrfs_key *extent_key,
6107                                         struct btrfs_key *leaf_key,
6108                                         struct btrfs_ref_path *ref_path,
6109                                         struct disk_extent *new_extents,
6110                                         int nr_extents)
6111 {
6112         struct extent_buffer *leaf;
6113         struct btrfs_file_extent_item *fi;
6114         struct inode *inode = NULL;
6115         struct btrfs_key key;
6116         u64 lock_start = 0;
6117         u64 lock_end = 0;
6118         u64 num_bytes;
6119         u64 ext_offset;
6120         u64 search_end = (u64)-1;
6121         u32 nritems;
6122         int nr_scaned = 0;
6123         int extent_locked = 0;
6124         int extent_type;
6125         int ret;
6126
6127         memcpy(&key, leaf_key, sizeof(key));
6128         if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
6129                 if (key.objectid < ref_path->owner_objectid ||
6130                     (key.objectid == ref_path->owner_objectid &&
6131                      key.type < BTRFS_EXTENT_DATA_KEY)) {
6132                         key.objectid = ref_path->owner_objectid;
6133                         key.type = BTRFS_EXTENT_DATA_KEY;
6134                         key.offset = 0;
6135                 }
6136         }
6137
6138         while (1) {
6139                 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
6140                 if (ret < 0)
6141                         goto out;
6142
6143                 leaf = path->nodes[0];
6144                 nritems = btrfs_header_nritems(leaf);
6145 next:
6146                 if (extent_locked && ret > 0) {
6147                         /*
6148                          * the file extent item was modified by someone
6149                          * before the extent got locked.
6150                          */
6151                         unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6152                                       lock_end, GFP_NOFS);
6153                         extent_locked = 0;
6154                 }
6155
6156                 if (path->slots[0] >= nritems) {
6157                         if (++nr_scaned > 2)
6158                                 break;
6159
6160                         BUG_ON(extent_locked);
6161                         ret = btrfs_next_leaf(root, path);
6162                         if (ret < 0)
6163                                 goto out;
6164                         if (ret > 0)
6165                                 break;
6166                         leaf = path->nodes[0];
6167                         nritems = btrfs_header_nritems(leaf);
6168                 }
6169
6170                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
6171
6172                 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
6173                         if ((key.objectid > ref_path->owner_objectid) ||
6174                             (key.objectid == ref_path->owner_objectid &&
6175                              key.type > BTRFS_EXTENT_DATA_KEY) ||
6176                             key.offset >= search_end)
6177                                 break;
6178                 }
6179
6180                 if (inode && key.objectid != inode->i_ino) {
6181                         BUG_ON(extent_locked);
6182                         btrfs_release_path(root, path);
6183                         mutex_unlock(&inode->i_mutex);
6184                         iput(inode);
6185                         inode = NULL;
6186                         continue;
6187                 }
6188
6189                 if (key.type != BTRFS_EXTENT_DATA_KEY) {
6190                         path->slots[0]++;
6191                         ret = 1;
6192                         goto next;
6193                 }
6194                 fi = btrfs_item_ptr(leaf, path->slots[0],
6195                                     struct btrfs_file_extent_item);
6196                 extent_type = btrfs_file_extent_type(leaf, fi);
6197                 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
6198                      extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
6199                     (btrfs_file_extent_disk_bytenr(leaf, fi) !=
6200                      extent_key->objectid)) {
6201                         path->slots[0]++;
6202                         ret = 1;
6203                         goto next;
6204                 }
6205
6206                 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6207                 ext_offset = btrfs_file_extent_offset(leaf, fi);
6208
6209                 if (search_end == (u64)-1) {
6210                         search_end = key.offset - ext_offset +
6211                                 btrfs_file_extent_ram_bytes(leaf, fi);
6212                 }
6213
6214                 if (!extent_locked) {
6215                         lock_start = key.offset;
6216                         lock_end = lock_start + num_bytes - 1;
6217                 } else {
6218                         if (lock_start > key.offset ||
6219                             lock_end + 1 < key.offset + num_bytes) {
6220                                 unlock_extent(&BTRFS_I(inode)->io_tree,
6221                                               lock_start, lock_end, GFP_NOFS);
6222                                 extent_locked = 0;
6223                         }
6224                 }
6225
6226                 if (!inode) {
6227                         btrfs_release_path(root, path);
6228
6229                         inode = btrfs_iget_locked(root->fs_info->sb,
6230                                                   key.objectid, root);
6231                         if (inode->i_state & I_NEW) {
6232                                 BTRFS_I(inode)->root = root;
6233                                 BTRFS_I(inode)->location.objectid =
6234                                         key.objectid;
6235                                 BTRFS_I(inode)->location.type =
6236                                         BTRFS_INODE_ITEM_KEY;
6237                                 BTRFS_I(inode)->location.offset = 0;
6238                                 btrfs_read_locked_inode(inode);
6239                                 unlock_new_inode(inode);
6240                         }
6241                         /*
6242                          * some code call btrfs_commit_transaction while
6243                          * holding the i_mutex, so we can't use mutex_lock
6244                          * here.
6245                          */
6246                         if (is_bad_inode(inode) ||
6247                             !mutex_trylock(&inode->i_mutex)) {
6248                                 iput(inode);
6249                                 inode = NULL;
6250                                 key.offset = (u64)-1;
6251                                 goto skip;
6252                         }
6253                 }
6254
6255                 if (!extent_locked) {
6256                         struct btrfs_ordered_extent *ordered;
6257
6258                         btrfs_release_path(root, path);
6259
6260                         lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6261                                     lock_end, GFP_NOFS);
6262                         ordered = btrfs_lookup_first_ordered_extent(inode,
6263                                                                     lock_end);
6264                         if (ordered &&
6265                             ordered->file_offset <= lock_end &&
6266                             ordered->file_offset + ordered->len > lock_start) {
6267                                 unlock_extent(&BTRFS_I(inode)->io_tree,
6268                                               lock_start, lock_end, GFP_NOFS);
6269                                 btrfs_start_ordered_extent(inode, ordered, 1);
6270                                 btrfs_put_ordered_extent(ordered);
6271                                 key.offset += num_bytes;
6272                                 goto skip;
6273                         }
6274                         if (ordered)
6275                                 btrfs_put_ordered_extent(ordered);
6276
6277                         extent_locked = 1;
6278                         continue;
6279                 }
6280
6281                 if (nr_extents == 1) {
6282                         /* update extent pointer in place */
6283                         btrfs_set_file_extent_disk_bytenr(leaf, fi,
6284                                                 new_extents[0].disk_bytenr);
6285                         btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6286                                                 new_extents[0].disk_num_bytes);
6287                         btrfs_mark_buffer_dirty(leaf);
6288
6289                         btrfs_drop_extent_cache(inode, key.offset,
6290                                                 key.offset + num_bytes - 1, 0);
6291
6292                         ret = btrfs_inc_extent_ref(trans, root,
6293                                                 new_extents[0].disk_bytenr,
6294                                                 new_extents[0].disk_num_bytes,
6295                                                 leaf->start,
6296                                                 root->root_key.objectid,
6297                                                 trans->transid,
6298                                                 key.objectid);
6299                         BUG_ON(ret);
6300
6301                         ret = btrfs_free_extent(trans, root,
6302                                                 extent_key->objectid,
6303                                                 extent_key->offset,
6304                                                 leaf->start,
6305                                                 btrfs_header_owner(leaf),
6306                                                 btrfs_header_generation(leaf),
6307                                                 key.objectid, 0);
6308                         BUG_ON(ret);
6309
6310                         btrfs_release_path(root, path);
6311                         key.offset += num_bytes;
6312                 } else {
6313                         BUG_ON(1);
6314 #if 0
6315                         u64 alloc_hint;
6316                         u64 extent_len;
6317                         int i;
6318                         /*
6319                          * drop old extent pointer at first, then insert the
6320                          * new pointers one bye one
6321                          */
6322                         btrfs_release_path(root, path);
6323                         ret = btrfs_drop_extents(trans, root, inode, key.offset,
6324                                                  key.offset + num_bytes,
6325                                                  key.offset, &alloc_hint);
6326                         BUG_ON(ret);
6327
6328                         for (i = 0; i < nr_extents; i++) {
6329                                 if (ext_offset >= new_extents[i].num_bytes) {
6330                                         ext_offset -= new_extents[i].num_bytes;
6331                                         continue;
6332                                 }
6333                                 extent_len = min(new_extents[i].num_bytes -
6334                                                  ext_offset, num_bytes);
6335
6336                                 ret = btrfs_insert_empty_item(trans, root,
6337                                                               path, &key,
6338                                                               sizeof(*fi));
6339                                 BUG_ON(ret);
6340
6341                                 leaf = path->nodes[0];
6342                                 fi = btrfs_item_ptr(leaf, path->slots[0],
6343                                                 struct btrfs_file_extent_item);
6344                                 btrfs_set_file_extent_generation(leaf, fi,
6345                                                         trans->transid);
6346                                 btrfs_set_file_extent_type(leaf, fi,
6347                                                         BTRFS_FILE_EXTENT_REG);
6348                                 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6349                                                 new_extents[i].disk_bytenr);
6350                                 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6351                                                 new_extents[i].disk_num_bytes);
6352                                 btrfs_set_file_extent_ram_bytes(leaf, fi,
6353                                                 new_extents[i].ram_bytes);
6354
6355                                 btrfs_set_file_extent_compression(leaf, fi,
6356                                                 new_extents[i].compression);
6357                                 btrfs_set_file_extent_encryption(leaf, fi,
6358                                                 new_extents[i].encryption);
6359                                 btrfs_set_file_extent_other_encoding(leaf, fi,
6360                                                 new_extents[i].other_encoding);
6361
6362                                 btrfs_set_file_extent_num_bytes(leaf, fi,
6363                                                         extent_len);
6364                                 ext_offset += new_extents[i].offset;
6365                                 btrfs_set_file_extent_offset(leaf, fi,
6366                                                         ext_offset);
6367                                 btrfs_mark_buffer_dirty(leaf);
6368
6369                                 btrfs_drop_extent_cache(inode, key.offset,
6370                                                 key.offset + extent_len - 1, 0);
6371
6372                                 ret = btrfs_inc_extent_ref(trans, root,
6373                                                 new_extents[i].disk_bytenr,
6374                                                 new_extents[i].disk_num_bytes,
6375                                                 leaf->start,
6376                                                 root->root_key.objectid,
6377                                                 trans->transid, key.objectid);
6378                                 BUG_ON(ret);
6379                                 btrfs_release_path(root, path);
6380
6381                                 inode_add_bytes(inode, extent_len);
6382
6383                                 ext_offset = 0;
6384                                 num_bytes -= extent_len;
6385                                 key.offset += extent_len;
6386
6387                                 if (num_bytes == 0)
6388                                         break;
6389                         }
6390                         BUG_ON(i >= nr_extents);
6391 #endif
6392                 }
6393
6394                 if (extent_locked) {
6395                         unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6396                                       lock_end, GFP_NOFS);
6397                         extent_locked = 0;
6398                 }
6399 skip:
6400                 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
6401                     key.offset >= search_end)
6402                         break;
6403
6404                 cond_resched();
6405         }
6406         ret = 0;
6407 out:
6408         btrfs_release_path(root, path);
6409         if (inode) {
6410                 mutex_unlock(&inode->i_mutex);
6411                 if (extent_locked) {
6412                         unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6413                                       lock_end, GFP_NOFS);
6414                 }
6415                 iput(inode);
6416         }
6417         return ret;
6418 }
6419
6420 int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
6421                                struct btrfs_root *root,
6422                                struct extent_buffer *buf, u64 orig_start)
6423 {
6424         int level;
6425         int ret;
6426
6427         BUG_ON(btrfs_header_generation(buf) != trans->transid);
6428         BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6429
6430         level = btrfs_header_level(buf);
6431         if (level == 0) {
6432                 struct btrfs_leaf_ref *ref;
6433                 struct btrfs_leaf_ref *orig_ref;
6434
6435                 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
6436                 if (!orig_ref)
6437                         return -ENOENT;
6438
6439                 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
6440                 if (!ref) {
6441                         btrfs_free_leaf_ref(root, orig_ref);
6442                         return -ENOMEM;
6443                 }
6444
6445                 ref->nritems = orig_ref->nritems;
6446                 memcpy(ref->extents, orig_ref->extents,
6447                         sizeof(ref->extents[0]) * ref->nritems);
6448
6449                 btrfs_free_leaf_ref(root, orig_ref);
6450
6451                 ref->root_gen = trans->transid;
6452                 ref->bytenr = buf->start;
6453                 ref->owner = btrfs_header_owner(buf);
6454                 ref->generation = btrfs_header_generation(buf);
6455
6456                 ret = btrfs_add_leaf_ref(root, ref, 0);
6457                 WARN_ON(ret);
6458                 btrfs_free_leaf_ref(root, ref);
6459         }
6460         return 0;
6461 }
6462
6463 static noinline int invalidate_extent_cache(struct btrfs_root *root,
6464                                         struct extent_buffer *leaf,
6465                                         struct btrfs_block_group_cache *group,
6466                                         struct btrfs_root *target_root)
6467 {
6468         struct btrfs_key key;
6469         struct inode *inode = NULL;
6470         struct btrfs_file_extent_item *fi;
6471         u64 num_bytes;
6472         u64 skip_objectid = 0;
6473         u32 nritems;
6474         u32 i;
6475
6476         nritems = btrfs_header_nritems(leaf);
6477         for (i = 0; i < nritems; i++) {
6478                 btrfs_item_key_to_cpu(leaf, &key, i);
6479                 if (key.objectid == skip_objectid ||
6480                     key.type != BTRFS_EXTENT_DATA_KEY)
6481                         continue;
6482                 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6483                 if (btrfs_file_extent_type(leaf, fi) ==
6484                     BTRFS_FILE_EXTENT_INLINE)
6485                         continue;
6486                 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6487                         continue;
6488                 if (!inode || inode->i_ino != key.objectid) {
6489                         iput(inode);
6490                         inode = btrfs_ilookup(target_root->fs_info->sb,
6491                                               key.objectid, target_root, 1);
6492                 }
6493                 if (!inode) {
6494                         skip_objectid = key.objectid;
6495                         continue;
6496                 }
6497                 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6498
6499                 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
6500                             key.offset + num_bytes - 1, GFP_NOFS);
6501                 btrfs_drop_extent_cache(inode, key.offset,
6502                                         key.offset + num_bytes - 1, 1);
6503                 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
6504                               key.offset + num_bytes - 1, GFP_NOFS);
6505                 cond_resched();
6506         }
6507         iput(inode);
6508         return 0;
6509 }
6510
6511 static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
6512                                         struct btrfs_root *root,
6513                                         struct extent_buffer *leaf,
6514                                         struct btrfs_block_group_cache *group,
6515                                         struct inode *reloc_inode)
6516 {
6517         struct btrfs_key key;
6518         struct btrfs_key extent_key;
6519         struct btrfs_file_extent_item *fi;
6520         struct btrfs_leaf_ref *ref;
6521         struct disk_extent *new_extent;
6522         u64 bytenr;
6523         u64 num_bytes;
6524         u32 nritems;
6525         u32 i;
6526         int ext_index;
6527         int nr_extent;
6528         int ret;
6529
6530         new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
6531         BUG_ON(!new_extent);
6532
6533         ref = btrfs_lookup_leaf_ref(root, leaf->start);
6534         BUG_ON(!ref);
6535
6536         ext_index = -1;
6537         nritems = btrfs_header_nritems(leaf);
6538         for (i = 0; i < nritems; i++) {
6539                 btrfs_item_key_to_cpu(leaf, &key, i);
6540                 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
6541                         continue;
6542                 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6543                 if (btrfs_file_extent_type(leaf, fi) ==
6544                     BTRFS_FILE_EXTENT_INLINE)
6545                         continue;
6546                 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6547                 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
6548                 if (bytenr == 0)
6549                         continue;
6550
6551                 ext_index++;
6552                 if (bytenr >= group->key.objectid + group->key.offset ||
6553                     bytenr + num_bytes <= group->key.objectid)
6554                         continue;
6555
6556                 extent_key.objectid = bytenr;
6557                 extent_key.offset = num_bytes;
6558                 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
6559                 nr_extent = 1;
6560                 ret = get_new_locations(reloc_inode, &extent_key,
6561                                         group->key.objectid, 1,
6562                                         &new_extent, &nr_extent);
6563                 if (ret > 0)
6564                         continue;
6565                 BUG_ON(ret < 0);
6566
6567                 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
6568                 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
6569                 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
6570                 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
6571
6572                 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6573                                                 new_extent->disk_bytenr);
6574                 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6575                                                 new_extent->disk_num_bytes);
6576                 btrfs_mark_buffer_dirty(leaf);
6577
6578                 ret = btrfs_inc_extent_ref(trans, root,
6579                                         new_extent->disk_bytenr,
6580                                         new_extent->disk_num_bytes,
6581                                         leaf->start,
6582                                         root->root_key.objectid,
6583                                         trans->transid, key.objectid);
6584                 BUG_ON(ret);
6585
6586                 ret = btrfs_free_extent(trans, root,
6587                                         bytenr, num_bytes, leaf->start,
6588                                         btrfs_header_owner(leaf),
6589                                         btrfs_header_generation(leaf),
6590                                         key.objectid, 0);
6591                 BUG_ON(ret);
6592                 cond_resched();
6593         }
6594         kfree(new_extent);
6595         BUG_ON(ext_index + 1 != ref->nritems);
6596         btrfs_free_leaf_ref(root, ref);
6597         return 0;
6598 }
6599
6600 int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
6601                           struct btrfs_root *root)
6602 {
6603         struct btrfs_root *reloc_root;
6604         int ret;
6605
6606         if (root->reloc_root) {
6607                 reloc_root = root->reloc_root;
6608                 root->reloc_root = NULL;
6609                 list_add(&reloc_root->dead_list,
6610                          &root->fs_info->dead_reloc_roots);
6611
6612                 btrfs_set_root_bytenr(&reloc_root->root_item,
6613                                       reloc_root->node->start);
6614                 btrfs_set_root_level(&root->root_item,
6615                                      btrfs_header_level(reloc_root->node));
6616                 memset(&reloc_root->root_item.drop_progress, 0,
6617                         sizeof(struct btrfs_disk_key));
6618                 reloc_root->root_item.drop_level = 0;
6619
6620                 ret = btrfs_update_root(trans, root->fs_info->tree_root,
6621                                         &reloc_root->root_key,
6622                                         &reloc_root->root_item);
6623                 BUG_ON(ret);
6624         }
6625         return 0;
6626 }
6627
6628 int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
6629 {
6630         struct btrfs_trans_handle *trans;
6631         struct btrfs_root *reloc_root;
6632         struct btrfs_root *prev_root = NULL;
6633         struct list_head dead_roots;
6634         int ret;
6635         unsigned long nr;
6636
6637         INIT_LIST_HEAD(&dead_roots);
6638         list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
6639
6640         while (!list_empty(&dead_roots)) {
6641                 reloc_root = list_entry(dead_roots.prev,
6642                                         struct btrfs_root, dead_list);
6643                 list_del_init(&reloc_root->dead_list);
6644
6645                 BUG_ON(reloc_root->commit_root != NULL);
6646                 while (1) {
6647                         trans = btrfs_join_transaction(root, 1);
6648                         BUG_ON(!trans);
6649
6650                         mutex_lock(&root->fs_info->drop_mutex);
6651                         ret = btrfs_drop_snapshot(trans, reloc_root);
6652                         if (ret != -EAGAIN)
6653                                 break;
6654                         mutex_unlock(&root->fs_info->drop_mutex);
6655
6656                         nr = trans->blocks_used;
6657                         ret = btrfs_end_transaction(trans, root);
6658                         BUG_ON(ret);
6659                         btrfs_btree_balance_dirty(root, nr);
6660                 }
6661
6662                 free_extent_buffer(reloc_root->node);
6663
6664                 ret = btrfs_del_root(trans, root->fs_info->tree_root,
6665                                      &reloc_root->root_key);
6666                 BUG_ON(ret);
6667                 mutex_unlock(&root->fs_info->drop_mutex);
6668
6669                 nr = trans->blocks_used;
6670                 ret = btrfs_end_transaction(trans, root);
6671                 BUG_ON(ret);
6672                 btrfs_btree_balance_dirty(root, nr);
6673
6674                 kfree(prev_root);
6675                 prev_root = reloc_root;
6676         }
6677         if (prev_root) {
6678                 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
6679                 kfree(prev_root);
6680         }
6681         return 0;
6682 }
6683
6684 int btrfs_add_dead_reloc_root(struct btrfs_root *root)
6685 {
6686         list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
6687         return 0;
6688 }
6689
6690 int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
6691 {
6692         struct btrfs_root *reloc_root;
6693         struct btrfs_trans_handle *trans;
6694         struct btrfs_key location;
6695         int found;
6696         int ret;
6697
6698         mutex_lock(&root->fs_info->tree_reloc_mutex);
6699         ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
6700         BUG_ON(ret);
6701         found = !list_empty(&root->fs_info->dead_reloc_roots);
6702         mutex_unlock(&root->fs_info->tree_reloc_mutex);
6703
6704         if (found) {
6705                 trans = btrfs_start_transaction(root, 1);
6706                 BUG_ON(!trans);
6707                 ret = btrfs_commit_transaction(trans, root);
6708                 BUG_ON(ret);
6709         }
6710
6711         location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
6712         location.offset = (u64)-1;
6713         location.type = BTRFS_ROOT_ITEM_KEY;
6714
6715         reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
6716         BUG_ON(!reloc_root);
6717         btrfs_orphan_cleanup(reloc_root);
6718         return 0;
6719 }
6720
6721 static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
6722                                     struct btrfs_root *root)
6723 {
6724         struct btrfs_root *reloc_root;
6725         struct extent_buffer *eb;
6726         struct btrfs_root_item *root_item;
6727         struct btrfs_key root_key;
6728         int ret;
6729
6730         BUG_ON(!root->ref_cows);
6731         if (root->reloc_root)
6732                 return 0;
6733
6734         root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
6735         BUG_ON(!root_item);
6736
6737         ret = btrfs_copy_root(trans, root, root->commit_root,
6738                               &eb, BTRFS_TREE_RELOC_OBJECTID);
6739         BUG_ON(ret);
6740
6741         root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
6742         root_key.offset = root->root_key.objectid;
6743         root_key.type = BTRFS_ROOT_ITEM_KEY;
6744
6745         memcpy(root_item, &root->root_item, sizeof(root_item));
6746         btrfs_set_root_refs(root_item, 0);
6747         btrfs_set_root_bytenr(root_item, eb->start);
6748         btrfs_set_root_level(root_item, btrfs_header_level(eb));
6749         btrfs_set_root_generation(root_item, trans->transid);
6750
6751         btrfs_tree_unlock(eb);
6752         free_extent_buffer(eb);
6753
6754         ret = btrfs_insert_root(trans, root->fs_info->tree_root,
6755                                 &root_key, root_item);
6756         BUG_ON(ret);
6757         kfree(root_item);
6758
6759         reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
6760                                                  &root_key);
6761         BUG_ON(!reloc_root);
6762         reloc_root->last_trans = trans->transid;
6763         reloc_root->commit_root = NULL;
6764         reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
6765
6766         root->reloc_root = reloc_root;
6767         return 0;
6768 }
6769
6770 /*
6771  * Core function of space balance.
6772  *
6773  * The idea is using reloc trees to relocate tree blocks in reference
6774  * counted roots. There is one reloc tree for each subvol, and all
6775  * reloc trees share same root key objectid. Reloc trees are snapshots
6776  * of the latest committed roots of subvols (root->commit_root).
6777  *
6778  * To relocate a tree block referenced by a subvol, there are two steps.
6779  * COW the block through subvol's reloc tree, then update block pointer
6780  * in the subvol to point to the new block. Since all reloc trees share
6781  * same root key objectid, doing special handing for tree blocks owned
6782  * by them is easy. Once a tree block has been COWed in one reloc tree,
6783  * we can use the resulting new block directly when the same block is
6784  * required to COW again through other reloc trees. By this way, relocated
6785  * tree blocks are shared between reloc trees, so they are also shared
6786  * between subvols.
6787  */
6788 static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
6789                                       struct btrfs_root *root,
6790                                       struct btrfs_path *path,
6791                                       struct btrfs_key *first_key,
6792                                       struct btrfs_ref_path *ref_path,
6793                                       struct btrfs_block_group_cache *group,
6794                                       struct inode *reloc_inode)
6795 {
6796         struct btrfs_root *reloc_root;
6797         struct extent_buffer *eb = NULL;
6798         struct btrfs_key *keys;
6799         u64 *nodes;
6800         int level;
6801         int shared_level;
6802         int lowest_level = 0;
6803         int ret;
6804
6805         if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
6806                 lowest_level = ref_path->owner_objectid;
6807
6808         if (!root->ref_cows) {
6809                 path->lowest_level = lowest_level;
6810                 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
6811                 BUG_ON(ret < 0);
6812                 path->lowest_level = 0;
6813                 btrfs_release_path(root, path);
6814                 return 0;
6815         }
6816
6817         mutex_lock(&root->fs_info->tree_reloc_mutex);
6818         ret = init_reloc_tree(trans, root);
6819         BUG_ON(ret);
6820         reloc_root = root->reloc_root;
6821
6822         shared_level = ref_path->shared_level;
6823         ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
6824
6825         keys = ref_path->node_keys;
6826         nodes = ref_path->new_nodes;
6827         memset(&keys[shared_level + 1], 0,
6828                sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
6829         memset(&nodes[shared_level + 1], 0,
6830                sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
6831
6832         if (nodes[lowest_level] == 0) {
6833                 path->lowest_level = lowest_level;
6834                 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6835                                         0, 1);
6836                 BUG_ON(ret);
6837                 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
6838                         eb = path->nodes[level];
6839                         if (!eb || eb == reloc_root->node)
6840                                 break;
6841                         nodes[level] = eb->start;
6842                         if (level == 0)
6843                                 btrfs_item_key_to_cpu(eb, &keys[level], 0);
6844                         else
6845                                 btrfs_node_key_to_cpu(eb, &keys[level], 0);
6846                 }
6847                 if (nodes[0] &&
6848                     ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6849                         eb = path->nodes[0];
6850                         ret = replace_extents_in_leaf(trans, reloc_root, eb,
6851                                                       group, reloc_inode);
6852                         BUG_ON(ret);
6853                 }
6854                 btrfs_release_path(reloc_root, path);
6855         } else {
6856                 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
6857                                        lowest_level);
6858                 BUG_ON(ret);
6859         }
6860
6861         /*
6862          * replace tree blocks in the fs tree with tree blocks in
6863          * the reloc tree.
6864          */
6865         ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
6866         BUG_ON(ret < 0);
6867
6868         if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6869                 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
6870                                         0, 0);
6871                 BUG_ON(ret);
6872                 extent_buffer_get(path->nodes[0]);
6873                 eb = path->nodes[0];
6874                 btrfs_release_path(reloc_root, path);
6875                 ret = invalidate_extent_cache(reloc_root, eb, group, root);
6876                 BUG_ON(ret);
6877                 free_extent_buffer(eb);
6878         }
6879
6880         mutex_unlock(&root->fs_info->tree_reloc_mutex);
6881         path->lowest_level = 0;
6882         return 0;
6883 }
6884
6885 static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
6886                                         struct btrfs_root *root,
6887                                         struct btrfs_path *path,
6888                                         struct btrfs_key *first_key,
6889                                         struct btrfs_ref_path *ref_path)
6890 {
6891         int ret;
6892
6893         ret = relocate_one_path(trans, root, path, first_key,
6894                                 ref_path, NULL, NULL);
6895         BUG_ON(ret);
6896
6897         return 0;
6898 }
6899
6900 static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
6901                                     struct btrfs_root *extent_root,
6902                                     struct btrfs_path *path,
6903                                     struct btrfs_key *extent_key)
6904 {
6905         int ret;
6906
6907         ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
6908         if (ret)
6909                 goto out;
6910         ret = btrfs_del_item(trans, extent_root, path);
6911 out:
6912         btrfs_release_path(extent_root, path);
6913         return ret;
6914 }
6915
6916 static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
6917                                                 struct btrfs_ref_path *ref_path)
6918 {
6919         struct btrfs_key root_key;
6920
6921         root_key.objectid = ref_path->root_objectid;
6922         root_key.type = BTRFS_ROOT_ITEM_KEY;
6923         if (is_cowonly_root(ref_path->root_objectid))
6924                 root_key.offset = 0;
6925         else
6926                 root_key.offset = (u64)-1;
6927
6928         return btrfs_read_fs_root_no_name(fs_info, &root_key);
6929 }
6930
6931 static noinline int relocate_one_extent(struct btrfs_root *extent_root,
6932                                         struct btrfs_path *path,
6933                                         struct btrfs_key *extent_key,
6934                                         struct btrfs_block_group_cache *group,
6935                                         struct inode *reloc_inode, int pass)
6936 {
6937         struct btrfs_trans_handle *trans;
6938         struct btrfs_root *found_root;
6939         struct btrfs_ref_path *ref_path = NULL;
6940         struct disk_extent *new_extents = NULL;
6941         int nr_extents = 0;
6942         int loops;
6943         int ret;
6944         int level;
6945         struct btrfs_key first_key;
6946         u64 prev_block = 0;
6947
6948
6949         trans = btrfs_start_transaction(extent_root, 1);
6950         BUG_ON(!trans);
6951
6952         if (extent_key->objectid == 0) {
6953                 ret = del_extent_zero(trans, extent_root, path, extent_key);
6954                 goto out;
6955         }
6956
6957         ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
6958         if (!ref_path) {
6959                 ret = -ENOMEM;
6960                 goto out;
6961         }
6962
6963         for (loops = 0; ; loops++) {
6964                 if (loops == 0) {
6965                         ret = btrfs_first_ref_path(trans, extent_root, ref_path,
6966                                                    extent_key->objectid);
6967                 } else {
6968                         ret = btrfs_next_ref_path(trans, extent_root, ref_path);
6969                 }
6970                 if (ret < 0)
6971                         goto out;
6972                 if (ret > 0)
6973                         break;
6974
6975                 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
6976                     ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
6977                         continue;
6978
6979                 found_root = read_ref_root(extent_root->fs_info, ref_path);
6980                 BUG_ON(!found_root);
6981                 /*
6982                  * for reference counted tree, only process reference paths
6983                  * rooted at the latest committed root.
6984                  */
6985                 if (found_root->ref_cows &&
6986                     ref_path->root_generation != found_root->root_key.offset)
6987                         continue;
6988
6989                 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
6990                         if (pass == 0) {
6991                                 /*
6992                                  * copy data extents to new locations
6993                                  */
6994                                 u64 group_start = group->key.objectid;
6995                                 ret = relocate_data_extent(reloc_inode,
6996                                                            extent_key,
6997                                                            group_start);
6998                                 if (ret < 0)
6999                                         goto out;
7000                                 break;
7001                         }
7002                         level = 0;
7003                 } else {
7004                         level = ref_path->owner_objectid;
7005                 }
7006
7007                 if (prev_block != ref_path->nodes[level]) {
7008                         struct extent_buffer *eb;
7009                         u64 block_start = ref_path->nodes[level];
7010                         u64 block_size = btrfs_level_size(found_root, level);
7011
7012                         eb = read_tree_block(found_root, block_start,
7013                                              block_size, 0);
7014                         btrfs_tree_lock(eb);
7015                         BUG_ON(level != btrfs_header_level(eb));
7016
7017                         if (level == 0)
7018                                 btrfs_item_key_to_cpu(eb, &first_key, 0);
7019                         else
7020                                 btrfs_node_key_to_cpu(eb, &first_key, 0);
7021
7022                         btrfs_tree_unlock(eb);
7023                         free_extent_buffer(eb);
7024                         prev_block = block_start;
7025                 }
7026
7027                 mutex_lock(&extent_root->fs_info->trans_mutex);
7028                 btrfs_record_root_in_trans(found_root);
7029                 mutex_unlock(&extent_root->fs_info->trans_mutex);
7030                 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7031                         /*
7032                          * try to update data extent references while
7033                          * keeping metadata shared between snapshots.
7034                          */
7035                         if (pass == 1) {
7036                                 ret = relocate_one_path(trans, found_root,
7037                                                 path, &first_key, ref_path,
7038                                                 group, reloc_inode);
7039                                 if (ret < 0)
7040                                         goto out;
7041                                 continue;
7042                         }
7043                         /*
7044                          * use fallback method to process the remaining
7045                          * references.
7046                          */
7047                         if (!new_extents) {
7048                                 u64 group_start = group->key.objectid;
7049                                 new_extents = kmalloc(sizeof(*new_extents),
7050                                                       GFP_NOFS);
7051                                 nr_extents = 1;
7052                                 ret = get_new_locations(reloc_inode,
7053                                                         extent_key,
7054                                                         group_start, 1,
7055                                                         &new_extents,
7056                                                         &nr_extents);
7057                                 if (ret)
7058                                         goto out;
7059                         }
7060                         ret = replace_one_extent(trans, found_root,
7061                                                 path, extent_key,
7062                                                 &first_key, ref_path,
7063                                                 new_extents, nr_extents);
7064                 } else {
7065                         ret = relocate_tree_block(trans, found_root, path,
7066                                                   &first_key, ref_path);
7067                 }
7068                 if (ret < 0)
7069                         goto out;
7070         }
7071         ret = 0;
7072 out:
7073         btrfs_end_transaction(trans, extent_root);
7074         kfree(new_extents);
7075         kfree(ref_path);
7076         return ret;
7077 }
7078 #endif
7079
7080 static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7081 {
7082         u64 num_devices;
7083         u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
7084                 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7085
7086         num_devices = root->fs_info->fs_devices->rw_devices;
7087         if (num_devices == 1) {
7088                 stripped |= BTRFS_BLOCK_GROUP_DUP;
7089                 stripped = flags & ~stripped;
7090
7091                 /* turn raid0 into single device chunks */
7092                 if (flags & BTRFS_BLOCK_GROUP_RAID0)
7093                         return stripped;
7094
7095                 /* turn mirroring into duplication */
7096                 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
7097                              BTRFS_BLOCK_GROUP_RAID10))
7098                         return stripped | BTRFS_BLOCK_GROUP_DUP;
7099                 return flags;
7100         } else {
7101                 /* they already had raid on here, just return */
7102                 if (flags & stripped)
7103                         return flags;
7104
7105                 stripped |= BTRFS_BLOCK_GROUP_DUP;
7106                 stripped = flags & ~stripped;
7107
7108                 /* switch duplicated blocks with raid1 */
7109                 if (flags & BTRFS_BLOCK_GROUP_DUP)
7110                         return stripped | BTRFS_BLOCK_GROUP_RAID1;
7111
7112                 /* turn single device chunks into raid0 */
7113                 return stripped | BTRFS_BLOCK_GROUP_RAID0;
7114         }
7115         return flags;
7116 }
7117
7118 static int __alloc_chunk_for_shrink(struct btrfs_root *root,
7119                      struct btrfs_block_group_cache *shrink_block_group,
7120                      int force)
7121 {
7122         struct btrfs_trans_handle *trans;
7123         u64 new_alloc_flags;
7124         u64 calc;
7125
7126         spin_lock(&shrink_block_group->lock);
7127         if (btrfs_block_group_used(&shrink_block_group->item) +
7128             shrink_block_group->reserved > 0) {
7129                 spin_unlock(&shrink_block_group->lock);
7130
7131                 trans = btrfs_start_transaction(root, 1);
7132                 spin_lock(&shrink_block_group->lock);
7133
7134                 new_alloc_flags = update_block_group_flags(root,
7135                                                    shrink_block_group->flags);
7136                 if (new_alloc_flags != shrink_block_group->flags) {
7137                         calc =
7138                              btrfs_block_group_used(&shrink_block_group->item);
7139                 } else {
7140                         calc = shrink_block_group->key.offset;
7141                 }
7142                 spin_unlock(&shrink_block_group->lock);
7143
7144                 do_chunk_alloc(trans, root->fs_info->extent_root,
7145                                calc + 2 * 1024 * 1024, new_alloc_flags, force);
7146
7147                 btrfs_end_transaction(trans, root);
7148         } else
7149                 spin_unlock(&shrink_block_group->lock);
7150         return 0;
7151 }
7152
7153
7154 int btrfs_prepare_block_group_relocation(struct btrfs_root *root,
7155                                          struct btrfs_block_group_cache *group)
7156
7157 {
7158         __alloc_chunk_for_shrink(root, group, 1);
7159         set_block_group_readonly(group);
7160         return 0;
7161 }
7162
7163 /*
7164  * checks to see if its even possible to relocate this block group.
7165  *
7166  * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7167  * ok to go ahead and try.
7168  */
7169 int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
7170 {
7171         struct btrfs_block_group_cache *block_group;
7172         struct btrfs_space_info *space_info;
7173         struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
7174         struct btrfs_device *device;
7175         int full = 0;
7176         int ret = 0;
7177
7178         block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
7179
7180         /* odd, couldn't find the block group, leave it alone */
7181         if (!block_group)
7182                 return -1;
7183
7184         /* no bytes used, we're good */
7185         if (!btrfs_block_group_used(&block_group->item))
7186                 goto out;
7187
7188         space_info = block_group->space_info;
7189         spin_lock(&space_info->lock);
7190
7191         full = space_info->full;
7192
7193         /*
7194          * if this is the last block group we have in this space, we can't
7195          * relocate it unless we're able to allocate a new chunk below.
7196          *
7197          * Otherwise, we need to make sure we have room in the space to handle
7198          * all of the extents from this block group.  If we can, we're good
7199          */
7200         if ((space_info->total_bytes != block_group->key.offset) &&
7201            (space_info->bytes_used + space_info->bytes_reserved +
7202             space_info->bytes_pinned + space_info->bytes_readonly +
7203             btrfs_block_group_used(&block_group->item) <
7204             space_info->total_bytes)) {
7205                 spin_unlock(&space_info->lock);
7206                 goto out;
7207         }
7208         spin_unlock(&space_info->lock);
7209
7210         /*
7211          * ok we don't have enough space, but maybe we have free space on our
7212          * devices to allocate new chunks for relocation, so loop through our
7213          * alloc devices and guess if we have enough space.  However, if we
7214          * were marked as full, then we know there aren't enough chunks, and we
7215          * can just return.
7216          */
7217         ret = -1;
7218         if (full)
7219                 goto out;
7220
7221         mutex_lock(&root->fs_info->chunk_mutex);
7222         list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
7223                 u64 min_free = btrfs_block_group_used(&block_group->item);
7224                 u64 dev_offset, max_avail;
7225
7226                 /*
7227                  * check to make sure we can actually find a chunk with enough
7228                  * space to fit our block group in.
7229                  */
7230                 if (device->total_bytes > device->bytes_used + min_free) {
7231                         ret = find_free_dev_extent(NULL, device, min_free,
7232                                                    &dev_offset, &max_avail);
7233                         if (!ret)
7234                                 break;
7235                         ret = -1;
7236                 }
7237         }
7238         mutex_unlock(&root->fs_info->chunk_mutex);
7239 out:
7240         btrfs_put_block_group(block_group);
7241         return ret;
7242 }
7243
7244 static int find_first_block_group(struct btrfs_root *root,
7245                 struct btrfs_path *path, struct btrfs_key *key)
7246 {
7247         int ret = 0;
7248         struct btrfs_key found_key;
7249         struct extent_buffer *leaf;
7250         int slot;
7251
7252         ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
7253         if (ret < 0)
7254                 goto out;
7255
7256         while (1) {
7257                 slot = path->slots[0];
7258                 leaf = path->nodes[0];
7259                 if (slot >= btrfs_header_nritems(leaf)) {
7260                         ret = btrfs_next_leaf(root, path);
7261                         if (ret == 0)
7262                                 continue;
7263                         if (ret < 0)
7264                                 goto out;
7265                         break;
7266                 }
7267                 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7268
7269                 if (found_key.objectid >= key->objectid &&
7270                     found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7271                         ret = 0;
7272                         goto out;
7273                 }
7274                 path->slots[0]++;
7275         }
7276         ret = -ENOENT;
7277 out:
7278         return ret;
7279 }
7280
7281 int btrfs_free_block_groups(struct btrfs_fs_info *info)
7282 {
7283         struct btrfs_block_group_cache *block_group;
7284         struct btrfs_space_info *space_info;
7285         struct btrfs_caching_control *caching_ctl;
7286         struct rb_node *n;
7287
7288         down_write(&info->extent_commit_sem);
7289         while (!list_empty(&info->caching_block_groups)) {
7290                 caching_ctl = list_entry(info->caching_block_groups.next,
7291                                          struct btrfs_caching_control, list);
7292                 list_del(&caching_ctl->list);
7293                 put_caching_control(caching_ctl);
7294         }
7295         up_write(&info->extent_commit_sem);
7296
7297         spin_lock(&info->block_group_cache_lock);
7298         while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
7299                 block_group = rb_entry(n, struct btrfs_block_group_cache,
7300                                        cache_node);
7301                 rb_erase(&block_group->cache_node,
7302                          &info->block_group_cache_tree);
7303                 spin_unlock(&info->block_group_cache_lock);
7304
7305                 down_write(&block_group->space_info->groups_sem);
7306                 list_del(&block_group->list);
7307                 up_write(&block_group->space_info->groups_sem);
7308
7309                 if (block_group->cached == BTRFS_CACHE_STARTED)
7310                         wait_block_group_cache_done(block_group);
7311
7312                 btrfs_remove_free_space_cache(block_group);
7313
7314                 WARN_ON(atomic_read(&block_group->count) != 1);
7315                 kfree(block_group);
7316
7317                 spin_lock(&info->block_group_cache_lock);
7318         }
7319         spin_unlock(&info->block_group_cache_lock);
7320
7321         /* now that all the block groups are freed, go through and
7322          * free all the space_info structs.  This is only called during
7323          * the final stages of unmount, and so we know nobody is
7324          * using them.  We call synchronize_rcu() once before we start,
7325          * just to be on the safe side.
7326          */
7327         synchronize_rcu();
7328
7329         while(!list_empty(&info->space_info)) {
7330                 space_info = list_entry(info->space_info.next,
7331                                         struct btrfs_space_info,
7332                                         list);
7333
7334                 list_del(&space_info->list);
7335                 kfree(space_info);
7336         }
7337         return 0;
7338 }
7339
7340 int btrfs_read_block_groups(struct btrfs_root *root)
7341 {
7342         struct btrfs_path *path;
7343         int ret;
7344         struct btrfs_block_group_cache *cache;
7345         struct btrfs_fs_info *info = root->fs_info;
7346         struct btrfs_space_info *space_info;
7347         struct btrfs_key key;
7348         struct btrfs_key found_key;
7349         struct extent_buffer *leaf;
7350
7351         root = info->extent_root;
7352         key.objectid = 0;
7353         key.offset = 0;
7354         btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
7355         path = btrfs_alloc_path();
7356         if (!path)
7357                 return -ENOMEM;
7358
7359         while (1) {
7360                 ret = find_first_block_group(root, path, &key);
7361                 if (ret > 0) {
7362                         ret = 0;
7363                         goto error;
7364                 }
7365                 if (ret != 0)
7366                         goto error;
7367
7368                 leaf = path->nodes[0];
7369                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7370                 cache = kzalloc(sizeof(*cache), GFP_NOFS);
7371                 if (!cache) {
7372                         ret = -ENOMEM;
7373                         break;
7374                 }
7375
7376                 atomic_set(&cache->count, 1);
7377                 spin_lock_init(&cache->lock);
7378                 spin_lock_init(&cache->tree_lock);
7379                 cache->fs_info = info;
7380                 INIT_LIST_HEAD(&cache->list);
7381                 INIT_LIST_HEAD(&cache->cluster_list);
7382
7383                 /*
7384                  * we only want to have 32k of ram per block group for keeping
7385                  * track of free space, and if we pass 1/2 of that we want to
7386                  * start converting things over to using bitmaps
7387                  */
7388                 cache->extents_thresh = ((1024 * 32) / 2) /
7389                         sizeof(struct btrfs_free_space);
7390
7391                 read_extent_buffer(leaf, &cache->item,
7392                                    btrfs_item_ptr_offset(leaf, path->slots[0]),
7393                                    sizeof(cache->item));
7394                 memcpy(&cache->key, &found_key, sizeof(found_key));
7395
7396                 key.objectid = found_key.objectid + found_key.offset;
7397                 btrfs_release_path(root, path);
7398                 cache->flags = btrfs_block_group_flags(&cache->item);
7399                 cache->sectorsize = root->sectorsize;
7400
7401                 /*
7402                  * check for two cases, either we are full, and therefore
7403                  * don't need to bother with the caching work since we won't
7404                  * find any space, or we are empty, and we can just add all
7405                  * the space in and be done with it.  This saves us _alot_ of
7406                  * time, particularly in the full case.
7407                  */
7408                 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
7409                         exclude_super_stripes(root, cache);
7410                         cache->last_byte_to_unpin = (u64)-1;
7411                         cache->cached = BTRFS_CACHE_FINISHED;
7412                         free_excluded_extents(root, cache);
7413                 } else if (btrfs_block_group_used(&cache->item) == 0) {
7414                         exclude_super_stripes(root, cache);
7415                         cache->last_byte_to_unpin = (u64)-1;
7416                         cache->cached = BTRFS_CACHE_FINISHED;
7417                         add_new_free_space(cache, root->fs_info,
7418                                            found_key.objectid,
7419                                            found_key.objectid +
7420                                            found_key.offset);
7421                         free_excluded_extents(root, cache);
7422                 }
7423
7424                 ret = update_space_info(info, cache->flags, found_key.offset,
7425                                         btrfs_block_group_used(&cache->item),
7426                                         &space_info);
7427                 BUG_ON(ret);
7428                 cache->space_info = space_info;
7429                 spin_lock(&cache->space_info->lock);
7430                 cache->space_info->bytes_super += cache->bytes_super;
7431                 spin_unlock(&cache->space_info->lock);
7432
7433                 down_write(&space_info->groups_sem);
7434                 list_add_tail(&cache->list, &space_info->block_groups);
7435                 up_write(&space_info->groups_sem);
7436
7437                 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7438                 BUG_ON(ret);
7439
7440                 set_avail_alloc_bits(root->fs_info, cache->flags);
7441                 if (btrfs_chunk_readonly(root, cache->key.objectid))
7442                         set_block_group_readonly(cache);
7443         }
7444         ret = 0;
7445 error:
7446         btrfs_free_path(path);
7447         return ret;
7448 }
7449
7450 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7451                            struct btrfs_root *root, u64 bytes_used,
7452                            u64 type, u64 chunk_objectid, u64 chunk_offset,
7453                            u64 size)
7454 {
7455         int ret;
7456         struct btrfs_root *extent_root;
7457         struct btrfs_block_group_cache *cache;
7458
7459         extent_root = root->fs_info->extent_root;
7460
7461         root->fs_info->last_trans_log_full_commit = trans->transid;
7462
7463         cache = kzalloc(sizeof(*cache), GFP_NOFS);
7464         if (!cache)
7465                 return -ENOMEM;
7466
7467         cache->key.objectid = chunk_offset;
7468         cache->key.offset = size;
7469         cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
7470         cache->sectorsize = root->sectorsize;
7471
7472         /*
7473          * we only want to have 32k of ram per block group for keeping track
7474          * of free space, and if we pass 1/2 of that we want to start
7475          * converting things over to using bitmaps
7476          */
7477         cache->extents_thresh = ((1024 * 32) / 2) /
7478                 sizeof(struct btrfs_free_space);
7479         atomic_set(&cache->count, 1);
7480         spin_lock_init(&cache->lock);
7481         spin_lock_init(&cache->tree_lock);
7482         INIT_LIST_HEAD(&cache->list);
7483         INIT_LIST_HEAD(&cache->cluster_list);
7484
7485         btrfs_set_block_group_used(&cache->item, bytes_used);
7486         btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
7487         cache->flags = type;
7488         btrfs_set_block_group_flags(&cache->item, type);
7489
7490         cache->last_byte_to_unpin = (u64)-1;
7491         cache->cached = BTRFS_CACHE_FINISHED;
7492         exclude_super_stripes(root, cache);
7493
7494         add_new_free_space(cache, root->fs_info, chunk_offset,
7495                            chunk_offset + size);
7496
7497         free_excluded_extents(root, cache);
7498
7499         ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7500                                 &cache->space_info);
7501         BUG_ON(ret);
7502
7503         spin_lock(&cache->space_info->lock);
7504         cache->space_info->bytes_super += cache->bytes_super;
7505         spin_unlock(&cache->space_info->lock);
7506
7507         down_write(&cache->space_info->groups_sem);
7508         list_add_tail(&cache->list, &cache->space_info->block_groups);
7509         up_write(&cache->space_info->groups_sem);
7510
7511         ret = btrfs_add_block_group_cache(root->fs_info, cache);
7512         BUG_ON(ret);
7513
7514         ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
7515                                 sizeof(cache->item));
7516         BUG_ON(ret);
7517
7518         set_avail_alloc_bits(extent_root->fs_info, type);
7519
7520         return 0;
7521 }
7522
7523 int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7524                              struct btrfs_root *root, u64 group_start)
7525 {
7526         struct btrfs_path *path;
7527         struct btrfs_block_group_cache *block_group;
7528         struct btrfs_free_cluster *cluster;
7529         struct btrfs_key key;
7530         int ret;
7531
7532         root = root->fs_info->extent_root;
7533
7534         block_group = btrfs_lookup_block_group(root->fs_info, group_start);
7535         BUG_ON(!block_group);
7536         BUG_ON(!block_group->ro);
7537
7538         memcpy(&key, &block_group->key, sizeof(key));
7539
7540         /* make sure this block group isn't part of an allocation cluster */
7541         cluster = &root->fs_info->data_alloc_cluster;
7542         spin_lock(&cluster->refill_lock);
7543         btrfs_return_cluster_to_free_space(block_group, cluster);
7544         spin_unlock(&cluster->refill_lock);
7545
7546         /*
7547          * make sure this block group isn't part of a metadata
7548          * allocation cluster
7549          */
7550         cluster = &root->fs_info->meta_alloc_cluster;
7551         spin_lock(&cluster->refill_lock);
7552         btrfs_return_cluster_to_free_space(block_group, cluster);
7553         spin_unlock(&cluster->refill_lock);
7554
7555         path = btrfs_alloc_path();
7556         BUG_ON(!path);
7557
7558         spin_lock(&root->fs_info->block_group_cache_lock);
7559         rb_erase(&block_group->cache_node,
7560                  &root->fs_info->block_group_cache_tree);
7561         spin_unlock(&root->fs_info->block_group_cache_lock);
7562
7563         down_write(&block_group->space_info->groups_sem);
7564         /*
7565          * we must use list_del_init so people can check to see if they
7566          * are still on the list after taking the semaphore
7567          */
7568         list_del_init(&block_group->list);
7569         up_write(&block_group->space_info->groups_sem);
7570
7571         if (block_group->cached == BTRFS_CACHE_STARTED)
7572                 wait_block_group_cache_done(block_group);
7573
7574         btrfs_remove_free_space_cache(block_group);
7575
7576         spin_lock(&block_group->space_info->lock);
7577         block_group->space_info->total_bytes -= block_group->key.offset;
7578         block_group->space_info->bytes_readonly -= block_group->key.offset;
7579         spin_unlock(&block_group->space_info->lock);
7580
7581         btrfs_clear_space_info_full(root->fs_info);
7582
7583         btrfs_put_block_group(block_group);
7584         btrfs_put_block_group(block_group);
7585
7586         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
7587         if (ret > 0)
7588                 ret = -EIO;
7589         if (ret < 0)
7590                 goto out;
7591
7592         ret = btrfs_del_item(trans, root, path);
7593 out:
7594         btrfs_free_path(path);
7595         return ret;
7596 }