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