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