Btrfs: Add support for multiple devices per filesystem
[linux-2.6-block.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 */
18
ec6b910f 19#include <linux/sched.h>
7bb86316 20#include <linux/crc32c.h>
edbd8d4e 21#include <linux/pagemap.h>
74493f7a 22#include "hash.h"
fec577fb
CM
23#include "ctree.h"
24#include "disk-io.h"
25#include "print-tree.h"
e089f05c 26#include "transaction.h"
0b86a832 27#include "volumes.h"
fec577fb 28
0b86a832 29#define BLOCK_GROUP_DATA EXTENT_WRITEBACK
96b5179d 30#define BLOCK_GROUP_METADATA EXTENT_UPTODATE
0b86a832
CM
31#define BLOCK_GROUP_SYSTEM EXTENT_NEW
32
96b5179d
CM
33#define BLOCK_GROUP_DIRTY EXTENT_DIRTY
34
e089f05c
CM
35static int finish_current_insert(struct btrfs_trans_handle *trans, struct
36 btrfs_root *extent_root);
e20d96d6
CM
37static int del_pending_extents(struct btrfs_trans_handle *trans, struct
38 btrfs_root *extent_root);
d548ee51 39
fec577fb 40
e37c9e69
CM
41static int cache_block_group(struct btrfs_root *root,
42 struct btrfs_block_group_cache *block_group)
43{
44 struct btrfs_path *path;
45 int ret;
46 struct btrfs_key key;
5f39d397 47 struct extent_buffer *leaf;
d1310b2e 48 struct extent_io_tree *free_space_cache;
e37c9e69 49 int slot;
e37c9e69
CM
50 u64 last = 0;
51 u64 hole_size;
7d7d6068 52 u64 first_free;
e37c9e69
CM
53 int found = 0;
54
00f5c795
CM
55 if (!block_group)
56 return 0;
57
e37c9e69 58 root = root->fs_info->extent_root;
f510cfec 59 free_space_cache = &root->fs_info->free_space_cache;
e37c9e69
CM
60
61 if (block_group->cached)
62 return 0;
f510cfec 63
e37c9e69
CM
64 path = btrfs_alloc_path();
65 if (!path)
66 return -ENOMEM;
7d7d6068 67
2cc58cf2 68 path->reada = 2;
7d7d6068 69 first_free = block_group->key.objectid;
e37c9e69 70 key.objectid = block_group->key.objectid;
e37c9e69
CM
71 key.offset = 0;
72 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
73 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
74 if (ret < 0)
75 return ret;
0b86a832 76 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
d548ee51
Y
77 if (ret < 0)
78 return ret;
79 if (ret == 0) {
80 leaf = path->nodes[0];
81 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
82 if (key.objectid + key.offset > first_free)
83 first_free = key.objectid + key.offset;
84 }
e37c9e69 85 while(1) {
5f39d397 86 leaf = path->nodes[0];
e37c9e69 87 slot = path->slots[0];
5f39d397 88 if (slot >= btrfs_header_nritems(leaf)) {
e37c9e69 89 ret = btrfs_next_leaf(root, path);
54aa1f4d
CM
90 if (ret < 0)
91 goto err;
de428b63 92 if (ret == 0) {
e37c9e69 93 continue;
de428b63 94 } else {
e37c9e69
CM
95 break;
96 }
97 }
5f39d397 98 btrfs_item_key_to_cpu(leaf, &key, slot);
7d7d6068 99 if (key.objectid < block_group->key.objectid) {
7d7d6068
Y
100 goto next;
101 }
e37c9e69
CM
102 if (key.objectid >= block_group->key.objectid +
103 block_group->key.offset) {
e37c9e69
CM
104 break;
105 }
7d7d6068 106
e37c9e69
CM
107 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
108 if (!found) {
7d7d6068 109 last = first_free;
e37c9e69 110 found = 1;
e37c9e69 111 }
f510cfec
CM
112 if (key.objectid > last) {
113 hole_size = key.objectid - last;
114 set_extent_dirty(free_space_cache, last,
115 last + hole_size - 1,
116 GFP_NOFS);
7d7d6068
Y
117 }
118 last = key.objectid + key.offset;
e37c9e69 119 }
7d7d6068 120next:
e37c9e69
CM
121 path->slots[0]++;
122 }
123
7d7d6068
Y
124 if (!found)
125 last = first_free;
126 if (block_group->key.objectid +
127 block_group->key.offset > last) {
128 hole_size = block_group->key.objectid +
129 block_group->key.offset - last;
f510cfec
CM
130 set_extent_dirty(free_space_cache, last,
131 last + hole_size - 1, GFP_NOFS);
7d7d6068 132 }
e37c9e69 133 block_group->cached = 1;
54aa1f4d 134err:
e37c9e69
CM
135 btrfs_free_path(path);
136 return 0;
137}
138
5276aeda
CM
139struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
140 btrfs_fs_info *info,
db94535d 141 u64 bytenr)
be744175 142{
d1310b2e 143 struct extent_io_tree *block_group_cache;
96b5179d
CM
144 struct btrfs_block_group_cache *block_group = NULL;
145 u64 ptr;
146 u64 start;
147 u64 end;
be744175
CM
148 int ret;
149
96b5179d
CM
150 block_group_cache = &info->block_group_cache;
151 ret = find_first_extent_bit(block_group_cache,
db94535d 152 bytenr, &start, &end,
0b86a832
CM
153 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
154 BLOCK_GROUP_SYSTEM);
be744175 155 if (ret) {
96b5179d 156 return NULL;
be744175 157 }
96b5179d
CM
158 ret = get_state_private(block_group_cache, start, &ptr);
159 if (ret)
160 return NULL;
161
ae2f5411 162 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
5cf66426 163 if (block_group->key.objectid <= bytenr && bytenr <
96b5179d
CM
164 block_group->key.objectid + block_group->key.offset)
165 return block_group;
be744175
CM
166 return NULL;
167}
0b86a832
CM
168
169static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
170{
171 if ((bits & BLOCK_GROUP_DATA) &&
172 (cache->flags & BTRFS_BLOCK_GROUP_DATA))
173 return 1;
174 if ((bits & BLOCK_GROUP_METADATA) &&
175 (cache->flags & BTRFS_BLOCK_GROUP_METADATA))
176 return 1;
177 if ((bits & BLOCK_GROUP_SYSTEM) &&
178 (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM))
179 return 1;
180 return 0;
181}
182
183static int noinline find_search_start(struct btrfs_root *root,
98ed5174 184 struct btrfs_block_group_cache **cache_ret,
0b86a832 185 u64 *start_ret, int num, int data)
e37c9e69 186{
e37c9e69
CM
187 int ret;
188 struct btrfs_block_group_cache *cache = *cache_ret;
d7fc640e 189 struct extent_io_tree *free_space_cache;
e19caa5f 190 u64 last;
f510cfec 191 u64 start = 0;
0b86a832 192 u64 end = 0;
257d0ce3 193 u64 cache_miss = 0;
c31f8830 194 u64 total_fs_bytes;
0b86a832 195 u64 search_start = *start_ret;
f84a8b36 196 int wrapped = 0;
e37c9e69 197
0b86a832 198 if (!cache)
1a2b2ac7 199 goto out;
c31f8830 200 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
d7fc640e
CM
201 free_space_cache = &root->fs_info->free_space_cache;
202
e37c9e69 203again:
54aa1f4d
CM
204 ret = cache_block_group(root, cache);
205 if (ret)
206 goto out;
f84a8b36 207
e19caa5f 208 last = max(search_start, cache->key.objectid);
0b86a832
CM
209 if (!block_group_bits(cache, data)) {
210 goto new_group;
211 }
e19caa5f 212
e37c9e69 213 while(1) {
7f93bf8d
CM
214 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
215 last, &start, &end, EXTENT_DIRTY);
216 if (ret) {
257d0ce3
CM
217 if (!cache_miss)
218 cache_miss = last;
e19caa5f
CM
219 goto new_group;
220 }
f510cfec 221
7f93bf8d
CM
222 start = max(last, start);
223 last = end + 1;
257d0ce3
CM
224 if (last - start < num) {
225 if (last == cache->key.objectid + cache->key.offset)
226 cache_miss = start;
f510cfec 227 continue;
257d0ce3 228 }
0b86a832 229 if (start + num > cache->key.objectid + cache->key.offset)
e37c9e69 230 goto new_group;
c31f8830
CM
231 if (start + num > total_fs_bytes)
232 goto new_group;
0b86a832
CM
233 *start_ret = start;
234 return 0;
e37c9e69
CM
235 }
236out:
1a2b2ac7
CM
237 cache = btrfs_lookup_block_group(root->fs_info, search_start);
238 if (!cache) {
0b86a832 239 printk("Unable to find block group for %Lu\n", search_start);
1a2b2ac7 240 WARN_ON(1);
1a2b2ac7 241 }
0b86a832 242 return -ENOSPC;
e37c9e69
CM
243
244new_group:
e19caa5f 245 last = cache->key.objectid + cache->key.offset;
f84a8b36 246wrapped:
e19caa5f 247 cache = btrfs_lookup_block_group(root->fs_info, last);
c31f8830 248 if (!cache || cache->key.objectid >= total_fs_bytes) {
0e4de584 249no_cache:
f84a8b36
CM
250 if (!wrapped) {
251 wrapped = 1;
252 last = search_start;
f84a8b36
CM
253 goto wrapped;
254 }
1a2b2ac7 255 goto out;
e37c9e69 256 }
257d0ce3
CM
257 if (cache_miss && !cache->cached) {
258 cache_block_group(root, cache);
259 last = cache_miss;
257d0ce3
CM
260 cache = btrfs_lookup_block_group(root->fs_info, last);
261 }
1a2b2ac7 262 cache = btrfs_find_block_group(root, cache, last, data, 0);
0e4de584
CM
263 if (!cache)
264 goto no_cache;
e37c9e69 265 *cache_ret = cache;
257d0ce3 266 cache_miss = 0;
e37c9e69
CM
267 goto again;
268}
269
84f54cfa
CM
270static u64 div_factor(u64 num, int factor)
271{
257d0ce3
CM
272 if (factor == 10)
273 return num;
84f54cfa
CM
274 num *= factor;
275 do_div(num, 10);
276 return num;
277}
278
31f3c99b
CM
279struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
280 struct btrfs_block_group_cache
be744175 281 *hint, u64 search_start,
de428b63 282 int data, int owner)
cd1bc465 283{
96b5179d 284 struct btrfs_block_group_cache *cache;
d1310b2e 285 struct extent_io_tree *block_group_cache;
31f3c99b 286 struct btrfs_block_group_cache *found_group = NULL;
cd1bc465
CM
287 struct btrfs_fs_info *info = root->fs_info;
288 u64 used;
31f3c99b
CM
289 u64 last = 0;
290 u64 hint_last;
96b5179d
CM
291 u64 start;
292 u64 end;
293 u64 free_check;
294 u64 ptr;
c31f8830 295 u64 total_fs_bytes;
96b5179d 296 int bit;
cd1bc465 297 int ret;
31f3c99b 298 int full_search = 0;
de428b63
CM
299 int factor = 8;
300
96b5179d 301 block_group_cache = &info->block_group_cache;
c31f8830 302 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
96b5179d 303
de428b63 304 if (!owner)
f84a8b36 305 factor = 8;
be744175 306
0b86a832 307 bit = data;
be744175 308
c31f8830 309 if (search_start && search_start < total_fs_bytes) {
be744175 310 struct btrfs_block_group_cache *shint;
5276aeda 311 shint = btrfs_lookup_block_group(info, search_start);
0b86a832 312 if (shint && block_group_bits(shint, data)) {
be744175 313 used = btrfs_block_group_used(&shint->item);
324ae4df
Y
314 if (used + shint->pinned <
315 div_factor(shint->key.offset, factor)) {
be744175
CM
316 return shint;
317 }
318 }
319 }
0b86a832
CM
320 if (hint && block_group_bits(hint, data) &&
321 hint->key.objectid < total_fs_bytes) {
31f3c99b 322 used = btrfs_block_group_used(&hint->item);
324ae4df
Y
323 if (used + hint->pinned <
324 div_factor(hint->key.offset, factor)) {
31f3c99b
CM
325 return hint;
326 }
e19caa5f 327 last = hint->key.objectid + hint->key.offset;
31f3c99b
CM
328 hint_last = last;
329 } else {
e37c9e69
CM
330 if (hint)
331 hint_last = max(hint->key.objectid, search_start);
332 else
333 hint_last = search_start;
334
c31f8830
CM
335 if (hint_last >= total_fs_bytes)
336 hint_last = search_start;
e37c9e69 337 last = hint_last;
31f3c99b 338 }
31f3c99b 339again:
cd1bc465 340 while(1) {
96b5179d
CM
341 ret = find_first_extent_bit(block_group_cache, last,
342 &start, &end, bit);
343 if (ret)
cd1bc465 344 break;
96b5179d
CM
345
346 ret = get_state_private(block_group_cache, start, &ptr);
347 if (ret)
348 break;
349
ae2f5411 350 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
96b5179d
CM
351 last = cache->key.objectid + cache->key.offset;
352 used = btrfs_block_group_used(&cache->item);
353
c31f8830
CM
354 if (cache->key.objectid > total_fs_bytes)
355 break;
356
96b5179d
CM
357 if (full_search)
358 free_check = cache->key.offset;
359 else
360 free_check = div_factor(cache->key.offset, factor);
324ae4df 361 if (used + cache->pinned < free_check) {
96b5179d
CM
362 found_group = cache;
363 goto found;
cd1bc465 364 }
de428b63 365 cond_resched();
cd1bc465 366 }
31f3c99b 367 if (!full_search) {
be744175 368 last = search_start;
31f3c99b
CM
369 full_search = 1;
370 goto again;
371 }
be744175 372found:
31f3c99b 373 return found_group;
cd1bc465
CM
374}
375
7bb86316 376static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation,
74493f7a
CM
377 u64 owner, u64 owner_offset)
378{
379 u32 high_crc = ~(u32)0;
380 u32 low_crc = ~(u32)0;
381 __le64 lenum;
382
383 lenum = cpu_to_le64(root_objectid);
384 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
7bb86316
CM
385 lenum = cpu_to_le64(ref_generation);
386 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
21a4989d
CM
387 if (owner >= BTRFS_FIRST_FREE_OBJECTID) {
388 lenum = cpu_to_le64(owner);
389 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
390 lenum = cpu_to_le64(owner_offset);
391 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
392 }
74493f7a
CM
393 return ((u64)high_crc << 32) | (u64)low_crc;
394}
395
7bb86316
CM
396static int match_extent_ref(struct extent_buffer *leaf,
397 struct btrfs_extent_ref *disk_ref,
398 struct btrfs_extent_ref *cpu_ref)
399{
400 int ret;
401 int len;
402
403 if (cpu_ref->objectid)
404 len = sizeof(*cpu_ref);
405 else
406 len = 2 * sizeof(u64);
407 ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref,
408 len);
409 return ret == 0;
410}
411
98ed5174
CM
412static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
413 struct btrfs_root *root,
414 struct btrfs_path *path, u64 bytenr,
415 u64 root_objectid,
416 u64 ref_generation, u64 owner,
417 u64 owner_offset, int del)
74493f7a
CM
418{
419 u64 hash;
420 struct btrfs_key key;
7bb86316 421 struct btrfs_key found_key;
74493f7a 422 struct btrfs_extent_ref ref;
7bb86316
CM
423 struct extent_buffer *leaf;
424 struct btrfs_extent_ref *disk_ref;
425 int ret;
426 int ret2;
427
428 btrfs_set_stack_ref_root(&ref, root_objectid);
429 btrfs_set_stack_ref_generation(&ref, ref_generation);
430 btrfs_set_stack_ref_objectid(&ref, owner);
431 btrfs_set_stack_ref_offset(&ref, owner_offset);
432
433 hash = hash_extent_ref(root_objectid, ref_generation, owner,
434 owner_offset);
435 key.offset = hash;
436 key.objectid = bytenr;
437 key.type = BTRFS_EXTENT_REF_KEY;
438
439 while (1) {
440 ret = btrfs_search_slot(trans, root, &key, path,
441 del ? -1 : 0, del);
442 if (ret < 0)
443 goto out;
444 leaf = path->nodes[0];
445 if (ret != 0) {
446 u32 nritems = btrfs_header_nritems(leaf);
447 if (path->slots[0] >= nritems) {
448 ret2 = btrfs_next_leaf(root, path);
449 if (ret2)
450 goto out;
451 leaf = path->nodes[0];
452 }
453 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
454 if (found_key.objectid != bytenr ||
455 found_key.type != BTRFS_EXTENT_REF_KEY)
456 goto out;
457 key.offset = found_key.offset;
458 if (del) {
459 btrfs_release_path(root, path);
460 continue;
461 }
462 }
463 disk_ref = btrfs_item_ptr(path->nodes[0],
464 path->slots[0],
465 struct btrfs_extent_ref);
466 if (match_extent_ref(path->nodes[0], disk_ref, &ref)) {
467 ret = 0;
468 goto out;
469 }
470 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
471 key.offset = found_key.offset + 1;
472 btrfs_release_path(root, path);
473 }
474out:
475 return ret;
476}
477
d8d5f3e1
CM
478/*
479 * Back reference rules. Back refs have three main goals:
480 *
481 * 1) differentiate between all holders of references to an extent so that
482 * when a reference is dropped we can make sure it was a valid reference
483 * before freeing the extent.
484 *
485 * 2) Provide enough information to quickly find the holders of an extent
486 * if we notice a given block is corrupted or bad.
487 *
488 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
489 * maintenance. This is actually the same as #2, but with a slightly
490 * different use case.
491 *
492 * File extents can be referenced by:
493 *
494 * - multiple snapshots, subvolumes, or different generations in one subvol
495 * - different files inside a single subvolume (in theory, not implemented yet)
496 * - different offsets inside a file (bookend extents in file.c)
497 *
498 * The extent ref structure has fields for:
499 *
500 * - Objectid of the subvolume root
501 * - Generation number of the tree holding the reference
502 * - objectid of the file holding the reference
503 * - offset in the file corresponding to the key holding the reference
504 *
505 * When a file extent is allocated the fields are filled in:
506 * (root_key.objectid, trans->transid, inode objectid, offset in file)
507 *
508 * When a leaf is cow'd new references are added for every file extent found
509 * in the leaf. It looks the same as the create case, but trans->transid
510 * will be different when the block is cow'd.
511 *
512 * (root_key.objectid, trans->transid, inode objectid, offset in file)
513 *
514 * When a file extent is removed either during snapshot deletion or file
515 * truncation, the corresponding back reference is found
516 * by searching for:
517 *
518 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
519 * inode objectid, offset in file)
520 *
521 * Btree extents can be referenced by:
522 *
523 * - Different subvolumes
524 * - Different generations of the same subvolume
525 *
526 * Storing sufficient information for a full reverse mapping of a btree
527 * block would require storing the lowest key of the block in the backref,
528 * and it would require updating that lowest key either before write out or
529 * every time it changed. Instead, the objectid of the lowest key is stored
530 * along with the level of the tree block. This provides a hint
531 * about where in the btree the block can be found. Searches through the
532 * btree only need to look for a pointer to that block, so they stop one
533 * level higher than the level recorded in the backref.
534 *
535 * Some btrees do not do reference counting on their extents. These
536 * include the extent tree and the tree of tree roots. Backrefs for these
537 * trees always have a generation of zero.
538 *
539 * When a tree block is created, back references are inserted:
540 *
f6dbff55 541 * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
d8d5f3e1
CM
542 *
543 * When a tree block is cow'd in a reference counted root,
544 * new back references are added for all the blocks it points to.
545 * These are of the form (trans->transid will have increased since creation):
546 *
f6dbff55 547 * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
d8d5f3e1
CM
548 *
549 * Because the lowest_key_objectid and the level are just hints
550 * they are not used when backrefs are deleted. When a backref is deleted:
551 *
552 * if backref was for a tree root:
553 * root_objectid = root->root_key.objectid
554 * else
555 * root_objectid = btrfs_header_owner(parent)
556 *
557 * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
558 *
559 * Back Reference Key hashing:
560 *
561 * Back references have four fields, each 64 bits long. Unfortunately,
562 * This is hashed into a single 64 bit number and placed into the key offset.
563 * The key objectid corresponds to the first byte in the extent, and the
564 * key type is set to BTRFS_EXTENT_REF_KEY
565 */
7bb86316
CM
566int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
567 struct btrfs_root *root,
568 struct btrfs_path *path, u64 bytenr,
569 u64 root_objectid, u64 ref_generation,
570 u64 owner, u64 owner_offset)
571{
572 u64 hash;
573 struct btrfs_key key;
574 struct btrfs_extent_ref ref;
575 struct btrfs_extent_ref *disk_ref;
74493f7a
CM
576 int ret;
577
578 btrfs_set_stack_ref_root(&ref, root_objectid);
7bb86316 579 btrfs_set_stack_ref_generation(&ref, ref_generation);
74493f7a
CM
580 btrfs_set_stack_ref_objectid(&ref, owner);
581 btrfs_set_stack_ref_offset(&ref, owner_offset);
582
7bb86316
CM
583 hash = hash_extent_ref(root_objectid, ref_generation, owner,
584 owner_offset);
74493f7a
CM
585 key.offset = hash;
586 key.objectid = bytenr;
587 key.type = BTRFS_EXTENT_REF_KEY;
588
589 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref));
590 while (ret == -EEXIST) {
7bb86316
CM
591 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
592 struct btrfs_extent_ref);
593 if (match_extent_ref(path->nodes[0], disk_ref, &ref))
594 goto out;
595 key.offset++;
596 btrfs_release_path(root, path);
597 ret = btrfs_insert_empty_item(trans, root, path, &key,
598 sizeof(ref));
74493f7a 599 }
7bb86316
CM
600 if (ret)
601 goto out;
602 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
603 struct btrfs_extent_ref);
604 write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref,
605 sizeof(ref));
606 btrfs_mark_buffer_dirty(path->nodes[0]);
607out:
608 btrfs_release_path(root, path);
609 return ret;
74493f7a
CM
610}
611
b18c6685
CM
612int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
613 struct btrfs_root *root,
74493f7a 614 u64 bytenr, u64 num_bytes,
7bb86316 615 u64 root_objectid, u64 ref_generation,
74493f7a 616 u64 owner, u64 owner_offset)
02217ed2 617{
5caf2a00 618 struct btrfs_path *path;
02217ed2 619 int ret;
e2fa7227 620 struct btrfs_key key;
5f39d397 621 struct extent_buffer *l;
234b63a0 622 struct btrfs_extent_item *item;
cf27e1ee 623 u32 refs;
037e6390 624
db94535d 625 WARN_ON(num_bytes < root->sectorsize);
5caf2a00 626 path = btrfs_alloc_path();
54aa1f4d
CM
627 if (!path)
628 return -ENOMEM;
26b8003f 629
b0331a4c 630 path->reada = 0;
db94535d 631 key.objectid = bytenr;
62e2749e 632 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
db94535d 633 key.offset = num_bytes;
5caf2a00 634 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
9f5fae2f 635 0, 1);
54aa1f4d
CM
636 if (ret < 0)
637 return ret;
a429e513 638 if (ret != 0) {
a28ec197 639 BUG();
a429e513 640 }
02217ed2 641 BUG_ON(ret != 0);
5f39d397 642 l = path->nodes[0];
5caf2a00 643 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
5f39d397
CM
644 refs = btrfs_extent_refs(l, item);
645 btrfs_set_extent_refs(l, item, refs + 1);
5caf2a00 646 btrfs_mark_buffer_dirty(path->nodes[0]);
a28ec197 647
5caf2a00 648 btrfs_release_path(root->fs_info->extent_root, path);
7bb86316 649
b0331a4c 650 path->reada = 0;
7bb86316
CM
651 ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
652 path, bytenr, root_objectid,
653 ref_generation, owner, owner_offset);
654 BUG_ON(ret);
9f5fae2f 655 finish_current_insert(trans, root->fs_info->extent_root);
e20d96d6 656 del_pending_extents(trans, root->fs_info->extent_root);
74493f7a
CM
657
658 btrfs_free_path(path);
02217ed2
CM
659 return 0;
660}
661
e9d0b13b
CM
662int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
663 struct btrfs_root *root)
664{
665 finish_current_insert(trans, root->fs_info->extent_root);
666 del_pending_extents(trans, root->fs_info->extent_root);
667 return 0;
668}
669
b18c6685 670static int lookup_extent_ref(struct btrfs_trans_handle *trans,
db94535d
CM
671 struct btrfs_root *root, u64 bytenr,
672 u64 num_bytes, u32 *refs)
a28ec197 673{
5caf2a00 674 struct btrfs_path *path;
a28ec197 675 int ret;
e2fa7227 676 struct btrfs_key key;
5f39d397 677 struct extent_buffer *l;
234b63a0 678 struct btrfs_extent_item *item;
5caf2a00 679
db94535d 680 WARN_ON(num_bytes < root->sectorsize);
5caf2a00 681 path = btrfs_alloc_path();
b0331a4c 682 path->reada = 0;
db94535d
CM
683 key.objectid = bytenr;
684 key.offset = num_bytes;
62e2749e 685 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
5caf2a00 686 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
9f5fae2f 687 0, 0);
54aa1f4d
CM
688 if (ret < 0)
689 goto out;
5f39d397
CM
690 if (ret != 0) {
691 btrfs_print_leaf(root, path->nodes[0]);
db94535d 692 printk("failed to find block number %Lu\n", bytenr);
a28ec197 693 BUG();
5f39d397
CM
694 }
695 l = path->nodes[0];
5caf2a00 696 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
5f39d397 697 *refs = btrfs_extent_refs(l, item);
54aa1f4d 698out:
5caf2a00 699 btrfs_free_path(path);
a28ec197
CM
700 return 0;
701}
702
be20aa9d
CM
703u32 btrfs_count_snapshots_in_path(struct btrfs_root *root,
704 struct btrfs_path *count_path,
705 u64 first_extent)
706{
707 struct btrfs_root *extent_root = root->fs_info->extent_root;
708 struct btrfs_path *path;
709 u64 bytenr;
710 u64 found_objectid;
56b453c9 711 u64 root_objectid = root->root_key.objectid;
be20aa9d
CM
712 u32 total_count = 0;
713 u32 cur_count;
be20aa9d
CM
714 u32 nritems;
715 int ret;
716 struct btrfs_key key;
717 struct btrfs_key found_key;
718 struct extent_buffer *l;
719 struct btrfs_extent_item *item;
720 struct btrfs_extent_ref *ref_item;
721 int level = -1;
722
723 path = btrfs_alloc_path();
724again:
725 if (level == -1)
726 bytenr = first_extent;
727 else
728 bytenr = count_path->nodes[level]->start;
729
730 cur_count = 0;
731 key.objectid = bytenr;
732 key.offset = 0;
733
734 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
735 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
736 if (ret < 0)
737 goto out;
738 BUG_ON(ret == 0);
739
740 l = path->nodes[0];
741 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
742
743 if (found_key.objectid != bytenr ||
744 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
745 goto out;
746 }
747
748 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
be20aa9d 749 while (1) {
bd09835d 750 l = path->nodes[0];
be20aa9d
CM
751 nritems = btrfs_header_nritems(l);
752 if (path->slots[0] >= nritems) {
753 ret = btrfs_next_leaf(extent_root, path);
754 if (ret == 0)
755 continue;
756 break;
757 }
758 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
759 if (found_key.objectid != bytenr)
760 break;
bd09835d 761
be20aa9d
CM
762 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
763 path->slots[0]++;
764 continue;
765 }
766
767 cur_count++;
768 ref_item = btrfs_item_ptr(l, path->slots[0],
769 struct btrfs_extent_ref);
770 found_objectid = btrfs_ref_root(l, ref_item);
771
56b453c9
CM
772 if (found_objectid != root_objectid) {
773 total_count = 2;
4313b399 774 goto out;
56b453c9
CM
775 }
776 total_count = 1;
be20aa9d
CM
777 path->slots[0]++;
778 }
779 if (cur_count == 0) {
780 total_count = 0;
781 goto out;
782 }
be20aa9d
CM
783 if (level >= 0 && root->node == count_path->nodes[level])
784 goto out;
785 level++;
786 btrfs_release_path(root, path);
787 goto again;
788
789out:
790 btrfs_free_path(path);
791 return total_count;
be20aa9d 792}
c5739bba 793int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
7bb86316 794 struct btrfs_root *root, u64 owner_objectid)
c5739bba 795{
7bb86316
CM
796 u64 generation;
797 u64 key_objectid;
798 u64 level;
799 u32 nritems;
800 struct btrfs_disk_key disk_key;
801
802 level = btrfs_header_level(root->node);
803 generation = trans->transid;
804 nritems = btrfs_header_nritems(root->node);
805 if (nritems > 0) {
806 if (level == 0)
807 btrfs_item_key(root->node, &disk_key, 0);
808 else
809 btrfs_node_key(root->node, &disk_key, 0);
810 key_objectid = btrfs_disk_key_objectid(&disk_key);
811 } else {
812 key_objectid = 0;
813 }
db94535d 814 return btrfs_inc_extent_ref(trans, root, root->node->start,
7bb86316 815 root->node->len, owner_objectid,
f6dbff55 816 generation, level, key_objectid);
c5739bba
CM
817}
818
e089f05c 819int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
5f39d397 820 struct extent_buffer *buf)
02217ed2 821{
db94535d 822 u64 bytenr;
5f39d397
CM
823 u32 nritems;
824 struct btrfs_key key;
6407bf6d 825 struct btrfs_file_extent_item *fi;
02217ed2 826 int i;
db94535d 827 int level;
6407bf6d 828 int ret;
54aa1f4d 829 int faili;
a28ec197 830
3768f368 831 if (!root->ref_cows)
a28ec197 832 return 0;
5f39d397 833
db94535d 834 level = btrfs_header_level(buf);
5f39d397
CM
835 nritems = btrfs_header_nritems(buf);
836 for (i = 0; i < nritems; i++) {
db94535d
CM
837 if (level == 0) {
838 u64 disk_bytenr;
5f39d397
CM
839 btrfs_item_key_to_cpu(buf, &key, i);
840 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
6407bf6d 841 continue;
5f39d397 842 fi = btrfs_item_ptr(buf, i,
6407bf6d 843 struct btrfs_file_extent_item);
5f39d397 844 if (btrfs_file_extent_type(buf, fi) ==
236454df
CM
845 BTRFS_FILE_EXTENT_INLINE)
846 continue;
db94535d
CM
847 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
848 if (disk_bytenr == 0)
3a686375 849 continue;
db94535d 850 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
7bb86316
CM
851 btrfs_file_extent_disk_num_bytes(buf, fi),
852 root->root_key.objectid, trans->transid,
853 key.objectid, key.offset);
54aa1f4d
CM
854 if (ret) {
855 faili = i;
856 goto fail;
857 }
6407bf6d 858 } else {
db94535d 859 bytenr = btrfs_node_blockptr(buf, i);
6caab489 860 btrfs_node_key_to_cpu(buf, &key, i);
db94535d 861 ret = btrfs_inc_extent_ref(trans, root, bytenr,
7bb86316
CM
862 btrfs_level_size(root, level - 1),
863 root->root_key.objectid,
f6dbff55
CM
864 trans->transid,
865 level - 1, key.objectid);
54aa1f4d
CM
866 if (ret) {
867 faili = i;
868 goto fail;
869 }
6407bf6d 870 }
02217ed2
CM
871 }
872 return 0;
54aa1f4d 873fail:
ccd467d6 874 WARN_ON(1);
7bb86316 875#if 0
54aa1f4d 876 for (i =0; i < faili; i++) {
db94535d
CM
877 if (level == 0) {
878 u64 disk_bytenr;
5f39d397
CM
879 btrfs_item_key_to_cpu(buf, &key, i);
880 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
54aa1f4d 881 continue;
5f39d397 882 fi = btrfs_item_ptr(buf, i,
54aa1f4d 883 struct btrfs_file_extent_item);
5f39d397 884 if (btrfs_file_extent_type(buf, fi) ==
54aa1f4d
CM
885 BTRFS_FILE_EXTENT_INLINE)
886 continue;
db94535d
CM
887 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
888 if (disk_bytenr == 0)
54aa1f4d 889 continue;
db94535d
CM
890 err = btrfs_free_extent(trans, root, disk_bytenr,
891 btrfs_file_extent_disk_num_bytes(buf,
5f39d397 892 fi), 0);
54aa1f4d
CM
893 BUG_ON(err);
894 } else {
db94535d
CM
895 bytenr = btrfs_node_blockptr(buf, i);
896 err = btrfs_free_extent(trans, root, bytenr,
897 btrfs_level_size(root, level - 1), 0);
54aa1f4d
CM
898 BUG_ON(err);
899 }
900 }
7bb86316 901#endif
54aa1f4d 902 return ret;
02217ed2
CM
903}
904
9078a3e1
CM
905static int write_one_cache_group(struct btrfs_trans_handle *trans,
906 struct btrfs_root *root,
907 struct btrfs_path *path,
908 struct btrfs_block_group_cache *cache)
909{
910 int ret;
911 int pending_ret;
912 struct btrfs_root *extent_root = root->fs_info->extent_root;
5f39d397
CM
913 unsigned long bi;
914 struct extent_buffer *leaf;
9078a3e1 915
9078a3e1 916 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
54aa1f4d
CM
917 if (ret < 0)
918 goto fail;
9078a3e1 919 BUG_ON(ret);
5f39d397
CM
920
921 leaf = path->nodes[0];
922 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
923 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
924 btrfs_mark_buffer_dirty(leaf);
9078a3e1 925 btrfs_release_path(extent_root, path);
54aa1f4d 926fail:
9078a3e1
CM
927 finish_current_insert(trans, extent_root);
928 pending_ret = del_pending_extents(trans, extent_root);
929 if (ret)
930 return ret;
931 if (pending_ret)
932 return pending_ret;
933 return 0;
934
935}
936
96b5179d
CM
937int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
938 struct btrfs_root *root)
9078a3e1 939{
d1310b2e 940 struct extent_io_tree *block_group_cache;
96b5179d 941 struct btrfs_block_group_cache *cache;
9078a3e1
CM
942 int ret;
943 int err = 0;
944 int werr = 0;
9078a3e1 945 struct btrfs_path *path;
96b5179d
CM
946 u64 last = 0;
947 u64 start;
948 u64 end;
949 u64 ptr;
9078a3e1 950
96b5179d 951 block_group_cache = &root->fs_info->block_group_cache;
9078a3e1
CM
952 path = btrfs_alloc_path();
953 if (!path)
954 return -ENOMEM;
955
956 while(1) {
96b5179d
CM
957 ret = find_first_extent_bit(block_group_cache, last,
958 &start, &end, BLOCK_GROUP_DIRTY);
959 if (ret)
9078a3e1 960 break;
54aa1f4d 961
96b5179d
CM
962 last = end + 1;
963 ret = get_state_private(block_group_cache, start, &ptr);
964 if (ret)
965 break;
966
ae2f5411 967 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
96b5179d
CM
968 err = write_one_cache_group(trans, root,
969 path, cache);
970 /*
971 * if we fail to write the cache group, we want
972 * to keep it marked dirty in hopes that a later
973 * write will work
974 */
975 if (err) {
976 werr = err;
977 continue;
9078a3e1 978 }
96b5179d
CM
979 clear_extent_bits(block_group_cache, start, end,
980 BLOCK_GROUP_DIRTY, GFP_NOFS);
9078a3e1
CM
981 }
982 btrfs_free_path(path);
983 return werr;
984}
985
986static int update_block_group(struct btrfs_trans_handle *trans,
987 struct btrfs_root *root,
db94535d 988 u64 bytenr, u64 num_bytes, int alloc,
0b86a832 989 int mark_free)
9078a3e1
CM
990{
991 struct btrfs_block_group_cache *cache;
992 struct btrfs_fs_info *info = root->fs_info;
db94535d 993 u64 total = num_bytes;
9078a3e1 994 u64 old_val;
db94535d 995 u64 byte_in_group;
96b5179d
CM
996 u64 start;
997 u64 end;
3e1ad54f 998
9078a3e1 999 while(total) {
db94535d 1000 cache = btrfs_lookup_block_group(info, bytenr);
3e1ad54f 1001 if (!cache) {
9078a3e1 1002 return -1;
cd1bc465 1003 }
db94535d
CM
1004 byte_in_group = bytenr - cache->key.objectid;
1005 WARN_ON(byte_in_group > cache->key.offset);
96b5179d
CM
1006 start = cache->key.objectid;
1007 end = start + cache->key.offset - 1;
1008 set_extent_bits(&info->block_group_cache, start, end,
1009 BLOCK_GROUP_DIRTY, GFP_NOFS);
9078a3e1
CM
1010
1011 old_val = btrfs_block_group_used(&cache->item);
db94535d 1012 num_bytes = min(total, cache->key.offset - byte_in_group);
cd1bc465 1013 if (alloc) {
db94535d 1014 old_val += num_bytes;
cd1bc465 1015 } else {
db94535d 1016 old_val -= num_bytes;
f510cfec
CM
1017 if (mark_free) {
1018 set_extent_dirty(&info->free_space_cache,
db94535d 1019 bytenr, bytenr + num_bytes - 1,
f510cfec 1020 GFP_NOFS);
e37c9e69 1021 }
cd1bc465 1022 }
9078a3e1 1023 btrfs_set_block_group_used(&cache->item, old_val);
db94535d
CM
1024 total -= num_bytes;
1025 bytenr += num_bytes;
9078a3e1
CM
1026 }
1027 return 0;
1028}
324ae4df
Y
1029static int update_pinned_extents(struct btrfs_root *root,
1030 u64 bytenr, u64 num, int pin)
1031{
1032 u64 len;
1033 struct btrfs_block_group_cache *cache;
1034 struct btrfs_fs_info *fs_info = root->fs_info;
1035
1036 if (pin) {
1037 set_extent_dirty(&fs_info->pinned_extents,
1038 bytenr, bytenr + num - 1, GFP_NOFS);
1039 } else {
1040 clear_extent_dirty(&fs_info->pinned_extents,
1041 bytenr, bytenr + num - 1, GFP_NOFS);
1042 }
1043 while (num > 0) {
1044 cache = btrfs_lookup_block_group(fs_info, bytenr);
1045 WARN_ON(!cache);
1046 len = min(num, cache->key.offset -
1047 (bytenr - cache->key.objectid));
1048 if (pin) {
1049 cache->pinned += len;
1050 fs_info->total_pinned += len;
1051 } else {
1052 cache->pinned -= len;
1053 fs_info->total_pinned -= len;
1054 }
1055 bytenr += len;
1056 num -= len;
1057 }
1058 return 0;
1059}
9078a3e1 1060
d1310b2e 1061int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
ccd467d6 1062{
ccd467d6 1063 u64 last = 0;
1a5bc167
CM
1064 u64 start;
1065 u64 end;
d1310b2e 1066 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
ccd467d6 1067 int ret;
ccd467d6
CM
1068
1069 while(1) {
1a5bc167
CM
1070 ret = find_first_extent_bit(pinned_extents, last,
1071 &start, &end, EXTENT_DIRTY);
1072 if (ret)
ccd467d6 1073 break;
1a5bc167
CM
1074 set_extent_dirty(copy, start, end, GFP_NOFS);
1075 last = end + 1;
ccd467d6
CM
1076 }
1077 return 0;
1078}
1079
1080int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1081 struct btrfs_root *root,
d1310b2e 1082 struct extent_io_tree *unpin)
a28ec197 1083{
1a5bc167
CM
1084 u64 start;
1085 u64 end;
a28ec197 1086 int ret;
d1310b2e 1087 struct extent_io_tree *free_space_cache;
f510cfec 1088 free_space_cache = &root->fs_info->free_space_cache;
a28ec197
CM
1089
1090 while(1) {
1a5bc167
CM
1091 ret = find_first_extent_bit(unpin, 0, &start, &end,
1092 EXTENT_DIRTY);
1093 if (ret)
a28ec197 1094 break;
324ae4df 1095 update_pinned_extents(root, start, end + 1 - start, 0);
1a5bc167
CM
1096 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1097 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
a28ec197
CM
1098 }
1099 return 0;
1100}
1101
98ed5174
CM
1102static int finish_current_insert(struct btrfs_trans_handle *trans,
1103 struct btrfs_root *extent_root)
037e6390 1104{
7bb86316
CM
1105 u64 start;
1106 u64 end;
1107 struct btrfs_fs_info *info = extent_root->fs_info;
d8d5f3e1 1108 struct extent_buffer *eb;
7bb86316 1109 struct btrfs_path *path;
e2fa7227 1110 struct btrfs_key ins;
d8d5f3e1 1111 struct btrfs_disk_key first;
234b63a0 1112 struct btrfs_extent_item extent_item;
037e6390 1113 int ret;
d8d5f3e1 1114 int level;
1a5bc167 1115 int err = 0;
037e6390 1116
5f39d397 1117 btrfs_set_stack_extent_refs(&extent_item, 1);
62e2749e 1118 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
7bb86316 1119 path = btrfs_alloc_path();
037e6390 1120
26b8003f 1121 while(1) {
1a5bc167
CM
1122 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1123 &end, EXTENT_LOCKED);
1124 if (ret)
26b8003f
CM
1125 break;
1126
1a5bc167
CM
1127 ins.objectid = start;
1128 ins.offset = end + 1 - start;
1129 err = btrfs_insert_item(trans, extent_root, &ins,
1130 &extent_item, sizeof(extent_item));
1131 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
1132 GFP_NOFS);
d8d5f3e1
CM
1133 eb = read_tree_block(extent_root, ins.objectid, ins.offset);
1134 level = btrfs_header_level(eb);
1135 if (level == 0) {
1136 btrfs_item_key(eb, &first, 0);
1137 } else {
1138 btrfs_node_key(eb, &first, 0);
1139 }
7bb86316
CM
1140 err = btrfs_insert_extent_backref(trans, extent_root, path,
1141 start, extent_root->root_key.objectid,
f6dbff55
CM
1142 0, level,
1143 btrfs_disk_key_objectid(&first));
7bb86316 1144 BUG_ON(err);
d8d5f3e1 1145 free_extent_buffer(eb);
037e6390 1146 }
7bb86316 1147 btrfs_free_path(path);
037e6390
CM
1148 return 0;
1149}
1150
db94535d
CM
1151static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
1152 int pending)
e20d96d6 1153{
1a5bc167 1154 int err = 0;
5f39d397 1155 struct extent_buffer *buf;
8ef97622 1156
f4b9aa8d 1157 if (!pending) {
db94535d 1158 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
5f39d397
CM
1159 if (buf) {
1160 if (btrfs_buffer_uptodate(buf)) {
2c90e5d6
CM
1161 u64 transid =
1162 root->fs_info->running_transaction->transid;
dc17ff8f
CM
1163 u64 header_transid =
1164 btrfs_header_generation(buf);
1165 if (header_transid == transid) {
55c69072 1166 clean_tree_block(NULL, root, buf);
5f39d397 1167 free_extent_buffer(buf);
c549228f 1168 return 1;
2c90e5d6 1169 }
f4b9aa8d 1170 }
5f39d397 1171 free_extent_buffer(buf);
8ef97622 1172 }
324ae4df 1173 update_pinned_extents(root, bytenr, num_bytes, 1);
f4b9aa8d 1174 } else {
1a5bc167 1175 set_extent_bits(&root->fs_info->pending_del,
db94535d
CM
1176 bytenr, bytenr + num_bytes - 1,
1177 EXTENT_LOCKED, GFP_NOFS);
f4b9aa8d 1178 }
be744175 1179 BUG_ON(err < 0);
e20d96d6
CM
1180 return 0;
1181}
1182
fec577fb 1183/*
a28ec197 1184 * remove an extent from the root, returns 0 on success
fec577fb 1185 */
e089f05c 1186static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
7bb86316
CM
1187 *root, u64 bytenr, u64 num_bytes,
1188 u64 root_objectid, u64 ref_generation,
1189 u64 owner_objectid, u64 owner_offset, int pin,
e37c9e69 1190 int mark_free)
a28ec197 1191{
5caf2a00 1192 struct btrfs_path *path;
e2fa7227 1193 struct btrfs_key key;
1261ec42
CM
1194 struct btrfs_fs_info *info = root->fs_info;
1195 struct btrfs_root *extent_root = info->extent_root;
5f39d397 1196 struct extent_buffer *leaf;
a28ec197 1197 int ret;
952fccac
CM
1198 int extent_slot = 0;
1199 int found_extent = 0;
1200 int num_to_del = 1;
234b63a0 1201 struct btrfs_extent_item *ei;
cf27e1ee 1202 u32 refs;
037e6390 1203
db94535d 1204 key.objectid = bytenr;
62e2749e 1205 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
db94535d 1206 key.offset = num_bytes;
5caf2a00 1207 path = btrfs_alloc_path();
54aa1f4d
CM
1208 if (!path)
1209 return -ENOMEM;
5f26f772 1210
b0331a4c 1211 path->reada = 0;
7bb86316
CM
1212 ret = lookup_extent_backref(trans, extent_root, path,
1213 bytenr, root_objectid,
1214 ref_generation,
1215 owner_objectid, owner_offset, 1);
1216 if (ret == 0) {
952fccac
CM
1217 struct btrfs_key found_key;
1218 extent_slot = path->slots[0];
1219 while(extent_slot > 0) {
1220 extent_slot--;
1221 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1222 extent_slot);
1223 if (found_key.objectid != bytenr)
1224 break;
1225 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
1226 found_key.offset == num_bytes) {
1227 found_extent = 1;
1228 break;
1229 }
1230 if (path->slots[0] - extent_slot > 5)
1231 break;
1232 }
1233 if (!found_extent)
1234 ret = btrfs_del_item(trans, extent_root, path);
7bb86316
CM
1235 } else {
1236 btrfs_print_leaf(extent_root, path->nodes[0]);
1237 WARN_ON(1);
1238 printk("Unable to find ref byte nr %Lu root %Lu "
1239 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1240 root_objectid, ref_generation, owner_objectid,
1241 owner_offset);
1242 }
952fccac
CM
1243 if (!found_extent) {
1244 btrfs_release_path(extent_root, path);
1245 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
1246 if (ret < 0)
1247 return ret;
1248 BUG_ON(ret);
1249 extent_slot = path->slots[0];
1250 }
5f39d397
CM
1251
1252 leaf = path->nodes[0];
952fccac 1253 ei = btrfs_item_ptr(leaf, extent_slot,
123abc88 1254 struct btrfs_extent_item);
5f39d397
CM
1255 refs = btrfs_extent_refs(leaf, ei);
1256 BUG_ON(refs == 0);
1257 refs -= 1;
1258 btrfs_set_extent_refs(leaf, ei, refs);
952fccac 1259
5f39d397
CM
1260 btrfs_mark_buffer_dirty(leaf);
1261
952fccac
CM
1262 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
1263 /* if the back ref and the extent are next to each other
1264 * they get deleted below in one shot
1265 */
1266 path->slots[0] = extent_slot;
1267 num_to_del = 2;
1268 } else if (found_extent) {
1269 /* otherwise delete the extent back ref */
1270 ret = btrfs_del_item(trans, extent_root, path);
1271 BUG_ON(ret);
1272 /* if refs are 0, we need to setup the path for deletion */
1273 if (refs == 0) {
1274 btrfs_release_path(extent_root, path);
1275 ret = btrfs_search_slot(trans, extent_root, &key, path,
1276 -1, 1);
1277 if (ret < 0)
1278 return ret;
1279 BUG_ON(ret);
1280 }
1281 }
1282
cf27e1ee 1283 if (refs == 0) {
db94535d
CM
1284 u64 super_used;
1285 u64 root_used;
78fae27e
CM
1286
1287 if (pin) {
db94535d 1288 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
c549228f
Y
1289 if (ret > 0)
1290 mark_free = 1;
1291 BUG_ON(ret < 0);
78fae27e
CM
1292 }
1293
58176a96 1294 /* block accounting for super block */
db94535d
CM
1295 super_used = btrfs_super_bytes_used(&info->super_copy);
1296 btrfs_set_super_bytes_used(&info->super_copy,
1297 super_used - num_bytes);
58176a96
JB
1298
1299 /* block accounting for root item */
db94535d 1300 root_used = btrfs_root_used(&root->root_item);
5f39d397 1301 btrfs_set_root_used(&root->root_item,
db94535d 1302 root_used - num_bytes);
952fccac
CM
1303 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1304 num_to_del);
54aa1f4d
CM
1305 if (ret) {
1306 return ret;
1307 }
db94535d 1308 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
0b86a832 1309 mark_free);
9078a3e1 1310 BUG_ON(ret);
a28ec197 1311 }
5caf2a00 1312 btrfs_free_path(path);
e089f05c 1313 finish_current_insert(trans, extent_root);
a28ec197
CM
1314 return ret;
1315}
1316
a28ec197
CM
1317/*
1318 * find all the blocks marked as pending in the radix tree and remove
1319 * them from the extent map
1320 */
e089f05c
CM
1321static int del_pending_extents(struct btrfs_trans_handle *trans, struct
1322 btrfs_root *extent_root)
a28ec197
CM
1323{
1324 int ret;
e20d96d6 1325 int err = 0;
1a5bc167
CM
1326 u64 start;
1327 u64 end;
d1310b2e
CM
1328 struct extent_io_tree *pending_del;
1329 struct extent_io_tree *pinned_extents;
8ef97622 1330
1a5bc167
CM
1331 pending_del = &extent_root->fs_info->pending_del;
1332 pinned_extents = &extent_root->fs_info->pinned_extents;
a28ec197
CM
1333
1334 while(1) {
1a5bc167
CM
1335 ret = find_first_extent_bit(pending_del, 0, &start, &end,
1336 EXTENT_LOCKED);
1337 if (ret)
a28ec197 1338 break;
324ae4df 1339 update_pinned_extents(extent_root, start, end + 1 - start, 1);
1a5bc167
CM
1340 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
1341 GFP_NOFS);
1342 ret = __free_extent(trans, extent_root,
7bb86316
CM
1343 start, end + 1 - start,
1344 extent_root->root_key.objectid,
1345 0, 0, 0, 0, 0);
1a5bc167
CM
1346 if (ret)
1347 err = ret;
fec577fb 1348 }
e20d96d6 1349 return err;
fec577fb
CM
1350}
1351
1352/*
1353 * remove an extent from the root, returns 0 on success
1354 */
e089f05c 1355int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
7bb86316
CM
1356 *root, u64 bytenr, u64 num_bytes,
1357 u64 root_objectid, u64 ref_generation,
1358 u64 owner_objectid, u64 owner_offset, int pin)
fec577fb 1359{
9f5fae2f 1360 struct btrfs_root *extent_root = root->fs_info->extent_root;
fec577fb
CM
1361 int pending_ret;
1362 int ret;
a28ec197 1363
db94535d 1364 WARN_ON(num_bytes < root->sectorsize);
7bb86316
CM
1365 if (!root->ref_cows)
1366 ref_generation = 0;
1367
fec577fb 1368 if (root == extent_root) {
db94535d 1369 pin_down_bytes(root, bytenr, num_bytes, 1);
fec577fb
CM
1370 return 0;
1371 }
7bb86316
CM
1372 ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
1373 ref_generation, owner_objectid, owner_offset,
1374 pin, pin == 0);
e20d96d6 1375 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
fec577fb
CM
1376 return ret ? ret : pending_ret;
1377}
1378
87ee04eb
CM
1379static u64 stripe_align(struct btrfs_root *root, u64 val)
1380{
1381 u64 mask = ((u64)root->stripesize - 1);
1382 u64 ret = (val + mask) & ~mask;
1383 return ret;
1384}
1385
fec577fb
CM
1386/*
1387 * walks the btree of allocated extents and find a hole of a given size.
1388 * The key ins is changed to record the hole:
1389 * ins->objectid == block start
62e2749e 1390 * ins->flags = BTRFS_EXTENT_ITEM_KEY
fec577fb
CM
1391 * ins->offset == number of blocks
1392 * Any available blocks before search_start are skipped.
1393 */
98ed5174
CM
1394static int noinline find_free_extent(struct btrfs_trans_handle *trans,
1395 struct btrfs_root *orig_root,
1396 u64 num_bytes, u64 empty_size,
1397 u64 search_start, u64 search_end,
1398 u64 hint_byte, struct btrfs_key *ins,
1399 u64 exclude_start, u64 exclude_nr,
1400 int data)
fec577fb 1401{
87ee04eb 1402 int ret;
be744175 1403 u64 orig_search_start = search_start;
9f5fae2f 1404 struct btrfs_root * root = orig_root->fs_info->extent_root;
f2458e1d 1405 struct btrfs_fs_info *info = root->fs_info;
db94535d 1406 u64 total_needed = num_bytes;
be08c1b9 1407 struct btrfs_block_group_cache *block_group;
be744175 1408 int full_scan = 0;
fbdc762b 1409 int wrapped = 0;
fec577fb 1410
db94535d 1411 WARN_ON(num_bytes < root->sectorsize);
b1a4d965
CM
1412 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1413
7f93bf8d
CM
1414 if (search_end == (u64)-1)
1415 search_end = btrfs_super_total_bytes(&info->super_copy);
0b86a832 1416
db94535d
CM
1417 if (hint_byte) {
1418 block_group = btrfs_lookup_block_group(info, hint_byte);
1a2b2ac7
CM
1419 if (!block_group)
1420 hint_byte = search_start;
be744175 1421 block_group = btrfs_find_block_group(root, block_group,
db94535d 1422 hint_byte, data, 1);
be744175
CM
1423 } else {
1424 block_group = btrfs_find_block_group(root,
1a2b2ac7
CM
1425 trans->block_group,
1426 search_start, data, 1);
be744175
CM
1427 }
1428
6702ed49 1429 total_needed += empty_size;
0b86a832 1430
be744175 1431check_failed:
70b043f0
CM
1432 if (!block_group) {
1433 block_group = btrfs_lookup_block_group(info, search_start);
1434 if (!block_group)
1435 block_group = btrfs_lookup_block_group(info,
1436 orig_search_start);
1437 }
0b86a832
CM
1438 ret = find_search_start(root, &block_group, &search_start,
1439 total_needed, data);
1440 if (ret)
d548ee51 1441 goto error;
e19caa5f 1442
0b86a832
CM
1443 search_start = stripe_align(root, search_start);
1444 ins->objectid = search_start;
1445 ins->offset = num_bytes;
e37c9e69 1446
db94535d 1447 if (ins->objectid + num_bytes >= search_end)
cf67582b 1448 goto enospc;
0b86a832
CM
1449
1450 if (ins->objectid + num_bytes >
1451 block_group->key.objectid + block_group->key.offset) {
e19caa5f
CM
1452 search_start = block_group->key.objectid +
1453 block_group->key.offset;
1454 goto new_group;
1455 }
0b86a832 1456
1a5bc167 1457 if (test_range_bit(&info->extent_ins, ins->objectid,
db94535d
CM
1458 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1459 search_start = ins->objectid + num_bytes;
1a5bc167
CM
1460 goto new_group;
1461 }
0b86a832 1462
1a5bc167 1463 if (test_range_bit(&info->pinned_extents, ins->objectid,
db94535d
CM
1464 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1465 search_start = ins->objectid + num_bytes;
1a5bc167 1466 goto new_group;
fec577fb 1467 }
0b86a832 1468
db94535d 1469 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
f2654de4
CM
1470 ins->objectid < exclude_start + exclude_nr)) {
1471 search_start = exclude_start + exclude_nr;
1472 goto new_group;
1473 }
0b86a832
CM
1474
1475 if (!(data & BLOCK_GROUP_DATA)) {
5276aeda 1476 block_group = btrfs_lookup_block_group(info, ins->objectid);
26b8003f
CM
1477 if (block_group)
1478 trans->block_group = block_group;
f2458e1d 1479 }
db94535d 1480 ins->offset = num_bytes;
fec577fb 1481 return 0;
be744175
CM
1482
1483new_group:
db94535d 1484 if (search_start + num_bytes >= search_end) {
cf67582b 1485enospc:
be744175 1486 search_start = orig_search_start;
fbdc762b
CM
1487 if (full_scan) {
1488 ret = -ENOSPC;
1489 goto error;
1490 }
6702ed49
CM
1491 if (wrapped) {
1492 if (!full_scan)
1493 total_needed -= empty_size;
fbdc762b 1494 full_scan = 1;
6702ed49 1495 } else
fbdc762b 1496 wrapped = 1;
be744175 1497 }
5276aeda 1498 block_group = btrfs_lookup_block_group(info, search_start);
fbdc762b 1499 cond_resched();
1a2b2ac7
CM
1500 block_group = btrfs_find_block_group(root, block_group,
1501 search_start, data, 0);
be744175
CM
1502 goto check_failed;
1503
0f70abe2 1504error:
0f70abe2 1505 return ret;
fec577fb 1506}
fec577fb
CM
1507/*
1508 * finds a free extent and does all the dirty work required for allocation
1509 * returns the key for the extent through ins, and a tree buffer for
1510 * the first block of the extent through buf.
1511 *
1512 * returns 0 if everything worked, non-zero otherwise.
1513 */
4d775673 1514int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
7bb86316
CM
1515 struct btrfs_root *root,
1516 u64 num_bytes, u64 root_objectid, u64 ref_generation,
1517 u64 owner, u64 owner_offset,
1518 u64 empty_size, u64 hint_byte,
be08c1b9 1519 u64 search_end, struct btrfs_key *ins, int data)
fec577fb
CM
1520{
1521 int ret;
1522 int pending_ret;
c31f8830
CM
1523 u64 super_used;
1524 u64 root_used;
fbdc762b 1525 u64 search_start = 0;
edbd8d4e 1526 u64 new_hint;
47e4bb98 1527 u32 sizes[2];
1261ec42
CM
1528 struct btrfs_fs_info *info = root->fs_info;
1529 struct btrfs_root *extent_root = info->extent_root;
47e4bb98
CM
1530 struct btrfs_extent_item *extent_item;
1531 struct btrfs_extent_ref *ref;
7bb86316 1532 struct btrfs_path *path;
47e4bb98 1533 struct btrfs_key keys[2];
8f662a76 1534
0b86a832
CM
1535 if (data)
1536 data = BLOCK_GROUP_DATA;
1537 else if (root == root->fs_info->chunk_root)
1538 data = BLOCK_GROUP_SYSTEM;
1539 else
1540 data = BLOCK_GROUP_METADATA;
1541
8f662a76 1542 new_hint = max(hint_byte, root->fs_info->alloc_start);
edbd8d4e
CM
1543 if (new_hint < btrfs_super_total_bytes(&info->super_copy))
1544 hint_byte = new_hint;
8f662a76 1545
db94535d
CM
1546 WARN_ON(num_bytes < root->sectorsize);
1547 ret = find_free_extent(trans, root, num_bytes, empty_size,
1548 search_start, search_end, hint_byte, ins,
26b8003f
CM
1549 trans->alloc_exclude_start,
1550 trans->alloc_exclude_nr, data);
ccd467d6 1551 BUG_ON(ret);
f2654de4
CM
1552 if (ret)
1553 return ret;
fec577fb 1554
58176a96 1555 /* block accounting for super block */
db94535d
CM
1556 super_used = btrfs_super_bytes_used(&info->super_copy);
1557 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
26b8003f 1558
58176a96 1559 /* block accounting for root item */
db94535d
CM
1560 root_used = btrfs_root_used(&root->root_item);
1561 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
58176a96 1562
f510cfec
CM
1563 clear_extent_dirty(&root->fs_info->free_space_cache,
1564 ins->objectid, ins->objectid + ins->offset - 1,
1565 GFP_NOFS);
1566
26b8003f 1567 if (root == extent_root) {
1a5bc167
CM
1568 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1569 ins->objectid + ins->offset - 1,
1570 EXTENT_LOCKED, GFP_NOFS);
26b8003f
CM
1571 goto update_block;
1572 }
1573
1574 WARN_ON(trans->alloc_exclude_nr);
1575 trans->alloc_exclude_start = ins->objectid;
1576 trans->alloc_exclude_nr = ins->offset;
037e6390 1577
47e4bb98
CM
1578 memcpy(&keys[0], ins, sizeof(*ins));
1579 keys[1].offset = hash_extent_ref(root_objectid, ref_generation,
1580 owner, owner_offset);
1581 keys[1].objectid = ins->objectid;
1582 keys[1].type = BTRFS_EXTENT_REF_KEY;
1583 sizes[0] = sizeof(*extent_item);
1584 sizes[1] = sizeof(*ref);
7bb86316
CM
1585
1586 path = btrfs_alloc_path();
1587 BUG_ON(!path);
47e4bb98
CM
1588
1589 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
1590 sizes, 2);
26b8003f 1591
ccd467d6 1592 BUG_ON(ret);
47e4bb98
CM
1593 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1594 struct btrfs_extent_item);
1595 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
1596 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1597 struct btrfs_extent_ref);
1598
1599 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
1600 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
1601 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
1602 btrfs_set_ref_offset(path->nodes[0], ref, owner_offset);
1603
1604 btrfs_mark_buffer_dirty(path->nodes[0]);
1605
1606 trans->alloc_exclude_start = 0;
1607 trans->alloc_exclude_nr = 0;
7bb86316 1608 btrfs_free_path(path);
e089f05c 1609 finish_current_insert(trans, extent_root);
e20d96d6 1610 pending_ret = del_pending_extents(trans, extent_root);
f510cfec 1611
e37c9e69 1612 if (ret) {
037e6390 1613 return ret;
e37c9e69
CM
1614 }
1615 if (pending_ret) {
037e6390 1616 return pending_ret;
e37c9e69 1617 }
26b8003f
CM
1618
1619update_block:
0b86a832 1620 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
f5947066
CM
1621 if (ret) {
1622 printk("update block group failed for %Lu %Lu\n",
1623 ins->objectid, ins->offset);
1624 BUG();
1625 }
037e6390 1626 return 0;
fec577fb
CM
1627}
1628
1629/*
1630 * helper function to allocate a block for a given tree
1631 * returns the tree buffer or NULL.
1632 */
5f39d397 1633struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
db94535d 1634 struct btrfs_root *root,
7bb86316
CM
1635 u32 blocksize,
1636 u64 root_objectid, u64 hint,
1637 u64 empty_size)
1638{
1639 u64 ref_generation;
1640
1641 if (root->ref_cows)
1642 ref_generation = trans->transid;
1643 else
1644 ref_generation = 0;
1645
1646
1647 return __btrfs_alloc_free_block(trans, root, blocksize, root_objectid,
1648 ref_generation, 0, 0, hint, empty_size);
1649}
1650
1651/*
1652 * helper function to allocate a block for a given tree
1653 * returns the tree buffer or NULL.
1654 */
1655struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1656 struct btrfs_root *root,
1657 u32 blocksize,
1658 u64 root_objectid,
1659 u64 ref_generation,
1660 u64 first_objectid,
1661 int level,
1662 u64 hint,
5f39d397 1663 u64 empty_size)
fec577fb 1664{
e2fa7227 1665 struct btrfs_key ins;
fec577fb 1666 int ret;
5f39d397 1667 struct extent_buffer *buf;
fec577fb 1668
7bb86316
CM
1669 ret = btrfs_alloc_extent(trans, root, blocksize,
1670 root_objectid, ref_generation,
f6dbff55 1671 level, first_objectid, empty_size, hint,
db94535d 1672 (u64)-1, &ins, 0);
fec577fb 1673 if (ret) {
54aa1f4d
CM
1674 BUG_ON(ret > 0);
1675 return ERR_PTR(ret);
fec577fb 1676 }
db94535d 1677 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
54aa1f4d 1678 if (!buf) {
7bb86316
CM
1679 btrfs_free_extent(trans, root, ins.objectid, blocksize,
1680 root->root_key.objectid, ref_generation,
1681 0, 0, 0);
54aa1f4d
CM
1682 return ERR_PTR(-ENOMEM);
1683 }
55c69072
CM
1684 btrfs_set_header_generation(buf, trans->transid);
1685 clean_tree_block(trans, root, buf);
1686 wait_on_tree_block_writeback(root, buf);
5f39d397 1687 btrfs_set_buffer_uptodate(buf);
55c69072
CM
1688
1689 if (PageDirty(buf->first_page)) {
1690 printk("page %lu dirty\n", buf->first_page->index);
1691 WARN_ON(1);
1692 }
1693
5f39d397
CM
1694 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1695 buf->start + buf->len - 1, GFP_NOFS);
d1310b2e 1696 set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->io_tree,
19c00ddc
CM
1697 buf->start, buf->start + buf->len - 1,
1698 EXTENT_CSUM, GFP_NOFS);
1699 buf->flags |= EXTENT_CSUM;
9afbb0b7
CM
1700 if (!btrfs_test_opt(root, SSD))
1701 btrfs_set_buffer_defrag(buf);
d3c2fdcf 1702 trans->blocks_used++;
fec577fb
CM
1703 return buf;
1704}
a28ec197 1705
98ed5174
CM
1706static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
1707 struct btrfs_root *root,
1708 struct extent_buffer *leaf)
6407bf6d 1709{
7bb86316
CM
1710 u64 leaf_owner;
1711 u64 leaf_generation;
5f39d397 1712 struct btrfs_key key;
6407bf6d
CM
1713 struct btrfs_file_extent_item *fi;
1714 int i;
1715 int nritems;
1716 int ret;
1717
5f39d397
CM
1718 BUG_ON(!btrfs_is_leaf(leaf));
1719 nritems = btrfs_header_nritems(leaf);
7bb86316
CM
1720 leaf_owner = btrfs_header_owner(leaf);
1721 leaf_generation = btrfs_header_generation(leaf);
1722
6407bf6d 1723 for (i = 0; i < nritems; i++) {
db94535d 1724 u64 disk_bytenr;
5f39d397
CM
1725
1726 btrfs_item_key_to_cpu(leaf, &key, i);
1727 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
6407bf6d
CM
1728 continue;
1729 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
5f39d397
CM
1730 if (btrfs_file_extent_type(leaf, fi) ==
1731 BTRFS_FILE_EXTENT_INLINE)
236454df 1732 continue;
6407bf6d
CM
1733 /*
1734 * FIXME make sure to insert a trans record that
1735 * repeats the snapshot del on crash
1736 */
db94535d
CM
1737 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1738 if (disk_bytenr == 0)
3a686375 1739 continue;
db94535d 1740 ret = btrfs_free_extent(trans, root, disk_bytenr,
7bb86316
CM
1741 btrfs_file_extent_disk_num_bytes(leaf, fi),
1742 leaf_owner, leaf_generation,
1743 key.objectid, key.offset, 0);
6407bf6d
CM
1744 BUG_ON(ret);
1745 }
1746 return 0;
1747}
1748
98ed5174 1749static void noinline reada_walk_down(struct btrfs_root *root,
bea495e5
CM
1750 struct extent_buffer *node,
1751 int slot)
e011599b 1752{
db94535d 1753 u64 bytenr;
bea495e5
CM
1754 u64 last = 0;
1755 u32 nritems;
e011599b 1756 u32 refs;
db94535d 1757 u32 blocksize;
bea495e5
CM
1758 int ret;
1759 int i;
1760 int level;
1761 int skipped = 0;
e011599b 1762
5f39d397 1763 nritems = btrfs_header_nritems(node);
db94535d 1764 level = btrfs_header_level(node);
bea495e5
CM
1765 if (level)
1766 return;
1767
1768 for (i = slot; i < nritems && skipped < 32; i++) {
db94535d 1769 bytenr = btrfs_node_blockptr(node, i);
bea495e5
CM
1770 if (last && ((bytenr > last && bytenr - last > 32 * 1024) ||
1771 (last > bytenr && last - bytenr > 32 * 1024))) {
1772 skipped++;
e011599b 1773 continue;
bea495e5
CM
1774 }
1775 blocksize = btrfs_level_size(root, level - 1);
1776 if (i != slot) {
1777 ret = lookup_extent_ref(NULL, root, bytenr,
1778 blocksize, &refs);
1779 BUG_ON(ret);
1780 if (refs != 1) {
1781 skipped++;
1782 continue;
1783 }
1784 }
409eb95d 1785 mutex_unlock(&root->fs_info->fs_mutex);
db94535d 1786 ret = readahead_tree_block(root, bytenr, blocksize);
bea495e5 1787 last = bytenr + blocksize;
409eb95d
CM
1788 cond_resched();
1789 mutex_lock(&root->fs_info->fs_mutex);
e011599b
CM
1790 if (ret)
1791 break;
1792 }
1793}
1794
9aca1d51
CM
1795/*
1796 * helper function for drop_snapshot, this walks down the tree dropping ref
1797 * counts as it goes.
1798 */
98ed5174
CM
1799static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
1800 struct btrfs_root *root,
1801 struct btrfs_path *path, int *level)
20524f02 1802{
7bb86316
CM
1803 u64 root_owner;
1804 u64 root_gen;
1805 u64 bytenr;
5f39d397
CM
1806 struct extent_buffer *next;
1807 struct extent_buffer *cur;
7bb86316 1808 struct extent_buffer *parent;
db94535d 1809 u32 blocksize;
20524f02
CM
1810 int ret;
1811 u32 refs;
1812
5caf2a00
CM
1813 WARN_ON(*level < 0);
1814 WARN_ON(*level >= BTRFS_MAX_LEVEL);
5f39d397 1815 ret = lookup_extent_ref(trans, root,
db94535d
CM
1816 path->nodes[*level]->start,
1817 path->nodes[*level]->len, &refs);
20524f02
CM
1818 BUG_ON(ret);
1819 if (refs > 1)
1820 goto out;
e011599b 1821
9aca1d51
CM
1822 /*
1823 * walk down to the last node level and free all the leaves
1824 */
6407bf6d 1825 while(*level >= 0) {
5caf2a00
CM
1826 WARN_ON(*level < 0);
1827 WARN_ON(*level >= BTRFS_MAX_LEVEL);
20524f02 1828 cur = path->nodes[*level];
e011599b 1829
5f39d397 1830 if (btrfs_header_level(cur) != *level)
2c90e5d6 1831 WARN_ON(1);
e011599b 1832
7518a238 1833 if (path->slots[*level] >=
5f39d397 1834 btrfs_header_nritems(cur))
20524f02 1835 break;
6407bf6d
CM
1836 if (*level == 0) {
1837 ret = drop_leaf_ref(trans, root, cur);
1838 BUG_ON(ret);
1839 break;
1840 }
db94535d
CM
1841 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1842 blocksize = btrfs_level_size(root, *level - 1);
1843 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
6407bf6d
CM
1844 BUG_ON(ret);
1845 if (refs != 1) {
7bb86316
CM
1846 parent = path->nodes[*level];
1847 root_owner = btrfs_header_owner(parent);
1848 root_gen = btrfs_header_generation(parent);
20524f02 1849 path->slots[*level]++;
db94535d 1850 ret = btrfs_free_extent(trans, root, bytenr,
7bb86316
CM
1851 blocksize, root_owner,
1852 root_gen, 0, 0, 1);
20524f02
CM
1853 BUG_ON(ret);
1854 continue;
1855 }
db94535d 1856 next = btrfs_find_tree_block(root, bytenr, blocksize);
5f39d397
CM
1857 if (!next || !btrfs_buffer_uptodate(next)) {
1858 free_extent_buffer(next);
bea495e5 1859 reada_walk_down(root, cur, path->slots[*level]);
e9d0b13b 1860 mutex_unlock(&root->fs_info->fs_mutex);
db94535d 1861 next = read_tree_block(root, bytenr, blocksize);
e9d0b13b
CM
1862 mutex_lock(&root->fs_info->fs_mutex);
1863
1864 /* we dropped the lock, check one more time */
db94535d
CM
1865 ret = lookup_extent_ref(trans, root, bytenr,
1866 blocksize, &refs);
e9d0b13b
CM
1867 BUG_ON(ret);
1868 if (refs != 1) {
7bb86316
CM
1869 parent = path->nodes[*level];
1870 root_owner = btrfs_header_owner(parent);
1871 root_gen = btrfs_header_generation(parent);
1872
e9d0b13b 1873 path->slots[*level]++;
5f39d397 1874 free_extent_buffer(next);
7bb86316
CM
1875 ret = btrfs_free_extent(trans, root, bytenr,
1876 blocksize,
1877 root_owner,
1878 root_gen, 0, 0, 1);
e9d0b13b
CM
1879 BUG_ON(ret);
1880 continue;
1881 }
1882 }
5caf2a00 1883 WARN_ON(*level <= 0);
83e15a28 1884 if (path->nodes[*level-1])
5f39d397 1885 free_extent_buffer(path->nodes[*level-1]);
20524f02 1886 path->nodes[*level-1] = next;
5f39d397 1887 *level = btrfs_header_level(next);
20524f02
CM
1888 path->slots[*level] = 0;
1889 }
1890out:
5caf2a00
CM
1891 WARN_ON(*level < 0);
1892 WARN_ON(*level >= BTRFS_MAX_LEVEL);
7bb86316
CM
1893
1894 if (path->nodes[*level] == root->node) {
1895 root_owner = root->root_key.objectid;
1896 parent = path->nodes[*level];
1897 } else {
1898 parent = path->nodes[*level + 1];
1899 root_owner = btrfs_header_owner(parent);
1900 }
1901
1902 root_gen = btrfs_header_generation(parent);
db94535d 1903 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
7bb86316
CM
1904 path->nodes[*level]->len,
1905 root_owner, root_gen, 0, 0, 1);
5f39d397 1906 free_extent_buffer(path->nodes[*level]);
20524f02
CM
1907 path->nodes[*level] = NULL;
1908 *level += 1;
1909 BUG_ON(ret);
1910 return 0;
1911}
1912
9aca1d51
CM
1913/*
1914 * helper for dropping snapshots. This walks back up the tree in the path
1915 * to find the first node higher up where we haven't yet gone through
1916 * all the slots
1917 */
98ed5174
CM
1918static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
1919 struct btrfs_root *root,
1920 struct btrfs_path *path, int *level)
20524f02 1921{
7bb86316
CM
1922 u64 root_owner;
1923 u64 root_gen;
1924 struct btrfs_root_item *root_item = &root->root_item;
20524f02
CM
1925 int i;
1926 int slot;
1927 int ret;
9f3a7427 1928
234b63a0 1929 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
20524f02 1930 slot = path->slots[i];
5f39d397
CM
1931 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1932 struct extent_buffer *node;
1933 struct btrfs_disk_key disk_key;
1934 node = path->nodes[i];
20524f02
CM
1935 path->slots[i]++;
1936 *level = i;
9f3a7427 1937 WARN_ON(*level == 0);
5f39d397 1938 btrfs_node_key(node, &disk_key, path->slots[i]);
9f3a7427 1939 memcpy(&root_item->drop_progress,
5f39d397 1940 &disk_key, sizeof(disk_key));
9f3a7427 1941 root_item->drop_level = i;
20524f02
CM
1942 return 0;
1943 } else {
7bb86316
CM
1944 if (path->nodes[*level] == root->node) {
1945 root_owner = root->root_key.objectid;
1946 root_gen =
1947 btrfs_header_generation(path->nodes[*level]);
1948 } else {
1949 struct extent_buffer *node;
1950 node = path->nodes[*level + 1];
1951 root_owner = btrfs_header_owner(node);
1952 root_gen = btrfs_header_generation(node);
1953 }
e089f05c 1954 ret = btrfs_free_extent(trans, root,
db94535d 1955 path->nodes[*level]->start,
7bb86316
CM
1956 path->nodes[*level]->len,
1957 root_owner, root_gen, 0, 0, 1);
6407bf6d 1958 BUG_ON(ret);
5f39d397 1959 free_extent_buffer(path->nodes[*level]);
83e15a28 1960 path->nodes[*level] = NULL;
20524f02 1961 *level = i + 1;
20524f02
CM
1962 }
1963 }
1964 return 1;
1965}
1966
9aca1d51
CM
1967/*
1968 * drop the reference count on the tree rooted at 'snap'. This traverses
1969 * the tree freeing any blocks that have a ref count of zero after being
1970 * decremented.
1971 */
e089f05c 1972int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
9f3a7427 1973 *root)
20524f02 1974{
3768f368 1975 int ret = 0;
9aca1d51 1976 int wret;
20524f02 1977 int level;
5caf2a00 1978 struct btrfs_path *path;
20524f02
CM
1979 int i;
1980 int orig_level;
9f3a7427 1981 struct btrfs_root_item *root_item = &root->root_item;
20524f02 1982
5caf2a00
CM
1983 path = btrfs_alloc_path();
1984 BUG_ON(!path);
20524f02 1985
5f39d397 1986 level = btrfs_header_level(root->node);
20524f02 1987 orig_level = level;
9f3a7427
CM
1988 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1989 path->nodes[level] = root->node;
f510cfec 1990 extent_buffer_get(root->node);
9f3a7427
CM
1991 path->slots[level] = 0;
1992 } else {
1993 struct btrfs_key key;
5f39d397
CM
1994 struct btrfs_disk_key found_key;
1995 struct extent_buffer *node;
6702ed49 1996
9f3a7427 1997 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
6702ed49
CM
1998 level = root_item->drop_level;
1999 path->lowest_level = level;
9f3a7427 2000 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6702ed49 2001 if (wret < 0) {
9f3a7427
CM
2002 ret = wret;
2003 goto out;
2004 }
5f39d397
CM
2005 node = path->nodes[level];
2006 btrfs_node_key(node, &found_key, path->slots[level]);
2007 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2008 sizeof(found_key)));
9f3a7427 2009 }
20524f02 2010 while(1) {
5caf2a00 2011 wret = walk_down_tree(trans, root, path, &level);
9aca1d51 2012 if (wret > 0)
20524f02 2013 break;
9aca1d51
CM
2014 if (wret < 0)
2015 ret = wret;
2016
5caf2a00 2017 wret = walk_up_tree(trans, root, path, &level);
9aca1d51 2018 if (wret > 0)
20524f02 2019 break;
9aca1d51
CM
2020 if (wret < 0)
2021 ret = wret;
409eb95d 2022 ret = -EAGAIN;
409eb95d 2023 break;
20524f02 2024 }
83e15a28 2025 for (i = 0; i <= orig_level; i++) {
5caf2a00 2026 if (path->nodes[i]) {
5f39d397 2027 free_extent_buffer(path->nodes[i]);
0f82731f 2028 path->nodes[i] = NULL;
83e15a28 2029 }
20524f02 2030 }
9f3a7427 2031out:
5caf2a00 2032 btrfs_free_path(path);
9aca1d51 2033 return ret;
20524f02 2034}
9078a3e1 2035
96b5179d 2036int btrfs_free_block_groups(struct btrfs_fs_info *info)
9078a3e1 2037{
96b5179d
CM
2038 u64 start;
2039 u64 end;
b97f9203 2040 u64 ptr;
9078a3e1 2041 int ret;
9078a3e1 2042 while(1) {
96b5179d
CM
2043 ret = find_first_extent_bit(&info->block_group_cache, 0,
2044 &start, &end, (unsigned int)-1);
2045 if (ret)
9078a3e1 2046 break;
b97f9203
Y
2047 ret = get_state_private(&info->block_group_cache, start, &ptr);
2048 if (!ret)
2049 kfree((void *)(unsigned long)ptr);
96b5179d
CM
2050 clear_extent_bits(&info->block_group_cache, start,
2051 end, (unsigned int)-1, GFP_NOFS);
9078a3e1 2052 }
e37c9e69 2053 while(1) {
f510cfec
CM
2054 ret = find_first_extent_bit(&info->free_space_cache, 0,
2055 &start, &end, EXTENT_DIRTY);
2056 if (ret)
e37c9e69 2057 break;
f510cfec
CM
2058 clear_extent_dirty(&info->free_space_cache, start,
2059 end, GFP_NOFS);
e37c9e69 2060 }
be744175
CM
2061 return 0;
2062}
2063
98ed5174
CM
2064static int noinline relocate_inode_pages(struct inode *inode, u64 start,
2065 u64 len)
edbd8d4e
CM
2066{
2067 u64 page_start;
2068 u64 page_end;
2069 u64 delalloc_start;
2070 u64 existing_delalloc;
2071 unsigned long last_index;
edbd8d4e
CM
2072 unsigned long i;
2073 struct page *page;
d1310b2e 2074 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4313b399
CM
2075 struct file_ra_state *ra;
2076
2077 ra = kzalloc(sizeof(*ra), GFP_NOFS);
edbd8d4e
CM
2078
2079 mutex_lock(&inode->i_mutex);
4313b399 2080 i = start >> PAGE_CACHE_SHIFT;
edbd8d4e
CM
2081 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
2082
4313b399
CM
2083 file_ra_state_init(ra, inode->i_mapping);
2084 btrfs_force_ra(inode->i_mapping, ra, NULL, i, last_index);
2085 kfree(ra);
edbd8d4e 2086
4313b399 2087 for (; i <= last_index; i++) {
edbd8d4e
CM
2088 page = grab_cache_page(inode->i_mapping, i);
2089 if (!page)
2090 goto out_unlock;
2091 if (!PageUptodate(page)) {
2092 btrfs_readpage(NULL, page);
2093 lock_page(page);
2094 if (!PageUptodate(page)) {
2095 unlock_page(page);
2096 page_cache_release(page);
2097 goto out_unlock;
2098 }
2099 }
2100 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2101 page_end = page_start + PAGE_CACHE_SIZE - 1;
2102
d1310b2e 2103 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
edbd8d4e
CM
2104
2105 delalloc_start = page_start;
d1310b2e
CM
2106 existing_delalloc = count_range_bits(io_tree,
2107 &delalloc_start, page_end,
2108 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
edbd8d4e 2109
d1310b2e 2110 set_extent_delalloc(io_tree, page_start,
edbd8d4e
CM
2111 page_end, GFP_NOFS);
2112
d1310b2e 2113 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
edbd8d4e
CM
2114 set_page_dirty(page);
2115 unlock_page(page);
2116 page_cache_release(page);
2117 }
2118
2119out_unlock:
2120 mutex_unlock(&inode->i_mutex);
2121 return 0;
2122}
2123
4313b399
CM
2124/*
2125 * note, this releases the path
2126 */
98ed5174 2127static int noinline relocate_one_reference(struct btrfs_root *extent_root,
edbd8d4e 2128 struct btrfs_path *path,
4313b399 2129 struct btrfs_key *extent_key)
edbd8d4e
CM
2130{
2131 struct inode *inode;
2132 struct btrfs_root *found_root;
4313b399
CM
2133 struct btrfs_key *root_location;
2134 struct btrfs_extent_ref *ref;
2135 u64 ref_root;
2136 u64 ref_gen;
2137 u64 ref_objectid;
2138 u64 ref_offset;
edbd8d4e
CM
2139 int ret;
2140
4313b399
CM
2141 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
2142 struct btrfs_extent_ref);
2143 ref_root = btrfs_ref_root(path->nodes[0], ref);
2144 ref_gen = btrfs_ref_generation(path->nodes[0], ref);
2145 ref_objectid = btrfs_ref_objectid(path->nodes[0], ref);
2146 ref_offset = btrfs_ref_offset(path->nodes[0], ref);
2147 btrfs_release_path(extent_root, path);
2148
2149 root_location = kmalloc(sizeof(*root_location), GFP_NOFS);
2150 root_location->objectid = ref_root;
edbd8d4e 2151 if (ref_gen == 0)
4313b399 2152 root_location->offset = 0;
edbd8d4e 2153 else
4313b399
CM
2154 root_location->offset = (u64)-1;
2155 root_location->type = BTRFS_ROOT_ITEM_KEY;
edbd8d4e
CM
2156
2157 found_root = btrfs_read_fs_root_no_name(extent_root->fs_info,
4313b399 2158 root_location);
edbd8d4e 2159 BUG_ON(!found_root);
4313b399 2160 kfree(root_location);
edbd8d4e
CM
2161
2162 if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
2163 mutex_unlock(&extent_root->fs_info->fs_mutex);
2164 inode = btrfs_iget_locked(extent_root->fs_info->sb,
2165 ref_objectid, found_root);
2166 if (inode->i_state & I_NEW) {
2167 /* the inode and parent dir are two different roots */
2168 BTRFS_I(inode)->root = found_root;
2169 BTRFS_I(inode)->location.objectid = ref_objectid;
2170 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
2171 BTRFS_I(inode)->location.offset = 0;
2172 btrfs_read_locked_inode(inode);
2173 unlock_new_inode(inode);
2174
2175 }
2176 /* this can happen if the reference is not against
2177 * the latest version of the tree root
2178 */
2179 if (is_bad_inode(inode)) {
2180 mutex_lock(&extent_root->fs_info->fs_mutex);
2181 goto out;
2182 }
2183 relocate_inode_pages(inode, ref_offset, extent_key->offset);
2184 /* FIXME, data=ordered will help get rid of this */
2185 filemap_fdatawrite(inode->i_mapping);
2186 iput(inode);
2187 mutex_lock(&extent_root->fs_info->fs_mutex);
2188 } else {
2189 struct btrfs_trans_handle *trans;
2190 struct btrfs_key found_key;
2191 struct extent_buffer *eb;
2192 int level;
2193 int i;
2194
2195 trans = btrfs_start_transaction(found_root, 1);
2196 eb = read_tree_block(found_root, extent_key->objectid,
2197 extent_key->offset);
2198 level = btrfs_header_level(eb);
2199
2200 if (level == 0)
2201 btrfs_item_key_to_cpu(eb, &found_key, 0);
2202 else
2203 btrfs_node_key_to_cpu(eb, &found_key, 0);
2204
2205 free_extent_buffer(eb);
2206
2207 path->lowest_level = level;
8f662a76 2208 path->reada = 2;
edbd8d4e
CM
2209 ret = btrfs_search_slot(trans, found_root, &found_key, path,
2210 0, 1);
2211 path->lowest_level = 0;
2212 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2213 if (!path->nodes[i])
2214 break;
2215 free_extent_buffer(path->nodes[i]);
2216 path->nodes[i] = NULL;
2217 }
2218 btrfs_release_path(found_root, path);
2219 btrfs_end_transaction(trans, found_root);
2220 }
2221
2222out:
2223 return 0;
2224}
2225
98ed5174
CM
2226static int noinline relocate_one_extent(struct btrfs_root *extent_root,
2227 struct btrfs_path *path,
2228 struct btrfs_key *extent_key)
edbd8d4e
CM
2229{
2230 struct btrfs_key key;
2231 struct btrfs_key found_key;
edbd8d4e 2232 struct extent_buffer *leaf;
edbd8d4e
CM
2233 u32 nritems;
2234 u32 item_size;
2235 int ret = 0;
2236
2237 key.objectid = extent_key->objectid;
2238 key.type = BTRFS_EXTENT_REF_KEY;
2239 key.offset = 0;
2240
2241 while(1) {
2242 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2243
edbd8d4e
CM
2244 if (ret < 0)
2245 goto out;
2246
2247 ret = 0;
2248 leaf = path->nodes[0];
2249 nritems = btrfs_header_nritems(leaf);
2250 if (path->slots[0] == nritems)
2251 goto out;
2252
2253 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2254 if (found_key.objectid != extent_key->objectid)
2255 break;
2256
2257 if (found_key.type != BTRFS_EXTENT_REF_KEY)
2258 break;
2259
2260 key.offset = found_key.offset + 1;
2261 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2262
4313b399 2263 ret = relocate_one_reference(extent_root, path, extent_key);
edbd8d4e
CM
2264 if (ret)
2265 goto out;
2266 }
2267 ret = 0;
2268out:
2269 btrfs_release_path(extent_root, path);
2270 return ret;
2271}
2272
edbd8d4e
CM
2273int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 new_size)
2274{
2275 struct btrfs_trans_handle *trans;
2276 struct btrfs_root *tree_root = root->fs_info->tree_root;
2277 struct btrfs_path *path;
2278 u64 cur_byte;
2279 u64 total_found;
edbd8d4e 2280 struct btrfs_fs_info *info = root->fs_info;
d1310b2e 2281 struct extent_io_tree *block_group_cache;
edbd8d4e 2282 struct btrfs_key key;
73e48b27 2283 struct btrfs_key found_key;
edbd8d4e
CM
2284 struct extent_buffer *leaf;
2285 u32 nritems;
2286 int ret;
725c8463 2287 int progress = 0;
edbd8d4e
CM
2288
2289 btrfs_set_super_total_bytes(&info->super_copy, new_size);
c31f8830
CM
2290 clear_extent_dirty(&info->free_space_cache, new_size, (u64)-1,
2291 GFP_NOFS);
edbd8d4e
CM
2292 block_group_cache = &info->block_group_cache;
2293 path = btrfs_alloc_path();
2294 root = root->fs_info->extent_root;
8f662a76 2295 path->reada = 2;
edbd8d4e
CM
2296
2297again:
2298 total_found = 0;
2299 key.objectid = new_size;
edbd8d4e
CM
2300 key.offset = 0;
2301 key.type = 0;
73e48b27 2302 cur_byte = key.objectid;
4313b399 2303
73e48b27
Y
2304 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2305 if (ret < 0)
2306 goto out;
2307
0b86a832 2308 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
73e48b27
Y
2309 if (ret < 0)
2310 goto out;
2311 if (ret == 0) {
2312 leaf = path->nodes[0];
2313 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2314 if (found_key.objectid + found_key.offset > new_size) {
2315 cur_byte = found_key.objectid;
2316 key.objectid = cur_byte;
2317 }
2318 }
2319 btrfs_release_path(root, path);
2320
2321 while(1) {
edbd8d4e
CM
2322 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2323 if (ret < 0)
2324 goto out;
73e48b27 2325
edbd8d4e 2326 leaf = path->nodes[0];
73e48b27
Y
2327 nritems = btrfs_header_nritems(leaf);
2328next:
2329 if (path->slots[0] >= nritems) {
2330 ret = btrfs_next_leaf(root, path);
2331 if (ret < 0)
2332 goto out;
2333 if (ret == 1) {
2334 ret = 0;
2335 break;
edbd8d4e 2336 }
73e48b27
Y
2337 leaf = path->nodes[0];
2338 nritems = btrfs_header_nritems(leaf);
edbd8d4e 2339 }
73e48b27
Y
2340
2341 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
725c8463
CM
2342
2343 if (progress && need_resched()) {
2344 memcpy(&key, &found_key, sizeof(key));
2345 mutex_unlock(&root->fs_info->fs_mutex);
2346 cond_resched();
2347 mutex_lock(&root->fs_info->fs_mutex);
2348 btrfs_release_path(root, path);
2349 btrfs_search_slot(NULL, root, &key, path, 0, 0);
2350 progress = 0;
2351 goto next;
2352 }
2353 progress = 1;
2354
73e48b27
Y
2355 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY ||
2356 found_key.objectid + found_key.offset <= cur_byte) {
edbd8d4e 2357 path->slots[0]++;
edbd8d4e
CM
2358 goto next;
2359 }
73e48b27 2360
edbd8d4e
CM
2361 total_found++;
2362 cur_byte = found_key.objectid + found_key.offset;
2363 key.objectid = cur_byte;
2364 btrfs_release_path(root, path);
2365 ret = relocate_one_extent(root, path, &found_key);
2366 }
2367
2368 btrfs_release_path(root, path);
2369
2370 if (total_found > 0) {
2371 trans = btrfs_start_transaction(tree_root, 1);
2372 btrfs_commit_transaction(trans, tree_root);
2373
2374 mutex_unlock(&root->fs_info->fs_mutex);
2375 btrfs_clean_old_snapshots(tree_root);
2376 mutex_lock(&root->fs_info->fs_mutex);
2377
2378 trans = btrfs_start_transaction(tree_root, 1);
2379 btrfs_commit_transaction(trans, tree_root);
2380 goto again;
2381 }
2382
2383 trans = btrfs_start_transaction(root, 1);
2384 key.objectid = new_size;
2385 key.offset = 0;
2386 key.type = 0;
2387 while(1) {
4313b399
CM
2388 u64 ptr;
2389
edbd8d4e
CM
2390 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2391 if (ret < 0)
2392 goto out;
73e48b27 2393
edbd8d4e
CM
2394 leaf = path->nodes[0];
2395 nritems = btrfs_header_nritems(leaf);
73e48b27
Y
2396bg_next:
2397 if (path->slots[0] >= nritems) {
2398 ret = btrfs_next_leaf(root, path);
2399 if (ret < 0)
2400 break;
2401 if (ret == 1) {
2402 ret = 0;
2403 break;
edbd8d4e 2404 }
73e48b27 2405 leaf = path->nodes[0];
1372f8e6
CM
2406 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2407
2408 /*
2409 * btrfs_next_leaf doesn't cow buffers, we have to
2410 * do the search again
2411 */
2412 memcpy(&key, &found_key, sizeof(key));
2413 btrfs_release_path(root, path);
725c8463 2414 goto resched_check;
73e48b27
Y
2415 }
2416
2417 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2418 if (btrfs_key_type(&found_key) != BTRFS_BLOCK_GROUP_ITEM_KEY) {
2419 printk("shrinker found key %Lu %u %Lu\n",
2420 found_key.objectid, found_key.type,
2421 found_key.offset);
2422 path->slots[0]++;
edbd8d4e
CM
2423 goto bg_next;
2424 }
edbd8d4e
CM
2425 ret = get_state_private(&info->block_group_cache,
2426 found_key.objectid, &ptr);
2427 if (!ret)
2428 kfree((void *)(unsigned long)ptr);
2429
2430 clear_extent_bits(&info->block_group_cache, found_key.objectid,
2431 found_key.objectid + found_key.offset - 1,
2432 (unsigned int)-1, GFP_NOFS);
2433
2434 key.objectid = found_key.objectid + 1;
2435 btrfs_del_item(trans, root, path);
2436 btrfs_release_path(root, path);
725c8463
CM
2437resched_check:
2438 if (need_resched()) {
2439 mutex_unlock(&root->fs_info->fs_mutex);
2440 cond_resched();
2441 mutex_lock(&root->fs_info->fs_mutex);
2442 }
edbd8d4e
CM
2443 }
2444 clear_extent_dirty(&info->free_space_cache, new_size, (u64)-1,
2445 GFP_NOFS);
2446 btrfs_commit_transaction(trans, root);
2447out:
2448 btrfs_free_path(path);
2449 return ret;
2450}
2451
2452int btrfs_grow_extent_tree(struct btrfs_trans_handle *trans,
2453 struct btrfs_root *root, u64 new_size)
2454{
0b86a832
CM
2455 btrfs_set_super_total_bytes(&root->fs_info->super_copy, new_size);
2456 return 0;
2457}
edbd8d4e 2458
0b86a832
CM
2459int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
2460 struct btrfs_key *key)
2461{
2462 int ret;
2463 struct btrfs_key found_key;
2464 struct extent_buffer *leaf;
2465 int slot;
edbd8d4e 2466
0b86a832
CM
2467 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
2468 if (ret < 0)
2469 return ret;
2470 while(1) {
2471 slot = path->slots[0];
edbd8d4e 2472 leaf = path->nodes[0];
0b86a832
CM
2473 if (slot >= btrfs_header_nritems(leaf)) {
2474 ret = btrfs_next_leaf(root, path);
2475 if (ret == 0)
2476 continue;
2477 if (ret < 0)
2478 goto error;
2479 break;
edbd8d4e 2480 }
0b86a832 2481 btrfs_item_key_to_cpu(leaf, &found_key, slot);
edbd8d4e 2482
0b86a832
CM
2483 if (found_key.objectid >= key->objectid &&
2484 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
2485 return 0;
2486 path->slots[0]++;
edbd8d4e 2487 }
0b86a832
CM
2488 ret = -ENOENT;
2489error:
2490 return ret;
edbd8d4e
CM
2491}
2492
9078a3e1
CM
2493int btrfs_read_block_groups(struct btrfs_root *root)
2494{
2495 struct btrfs_path *path;
2496 int ret;
96b5179d 2497 int bit;
9078a3e1 2498 struct btrfs_block_group_cache *cache;
be744175 2499 struct btrfs_fs_info *info = root->fs_info;
d1310b2e 2500 struct extent_io_tree *block_group_cache;
9078a3e1
CM
2501 struct btrfs_key key;
2502 struct btrfs_key found_key;
5f39d397 2503 struct extent_buffer *leaf;
96b5179d
CM
2504
2505 block_group_cache = &info->block_group_cache;
be744175 2506 root = info->extent_root;
9078a3e1 2507 key.objectid = 0;
0b86a832 2508 key.offset = 0;
9078a3e1 2509 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
9078a3e1
CM
2510 path = btrfs_alloc_path();
2511 if (!path)
2512 return -ENOMEM;
2513
2514 while(1) {
0b86a832
CM
2515 ret = find_first_block_group(root, path, &key);
2516 if (ret > 0) {
2517 ret = 0;
2518 goto error;
9078a3e1 2519 }
0b86a832
CM
2520 if (ret != 0)
2521 goto error;
2522
5f39d397
CM
2523 leaf = path->nodes[0];
2524 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
9078a3e1
CM
2525 cache = kmalloc(sizeof(*cache), GFP_NOFS);
2526 if (!cache) {
0b86a832 2527 ret = -ENOMEM;
9078a3e1
CM
2528 break;
2529 }
3e1ad54f 2530
5f39d397
CM
2531 read_extent_buffer(leaf, &cache->item,
2532 btrfs_item_ptr_offset(leaf, path->slots[0]),
2533 sizeof(cache->item));
9078a3e1 2534 memcpy(&cache->key, &found_key, sizeof(found_key));
e37c9e69 2535 cache->cached = 0;
324ae4df 2536 cache->pinned = 0;
0b86a832 2537
9078a3e1
CM
2538 key.objectid = found_key.objectid + found_key.offset;
2539 btrfs_release_path(root, path);
0b86a832
CM
2540 cache->flags = btrfs_block_group_flags(&cache->item);
2541 bit = 0;
2542 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
96b5179d 2543 bit = BLOCK_GROUP_DATA;
0b86a832
CM
2544 } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
2545 bit = BLOCK_GROUP_SYSTEM;
2546 } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) {
96b5179d 2547 bit = BLOCK_GROUP_METADATA;
31f3c99b 2548 }
96b5179d
CM
2549
2550 /* use EXTENT_LOCKED to prevent merging */
2551 set_extent_bits(block_group_cache, found_key.objectid,
2552 found_key.objectid + found_key.offset - 1,
2553 bit | EXTENT_LOCKED, GFP_NOFS);
2554 set_state_private(block_group_cache, found_key.objectid,
ae2f5411 2555 (unsigned long)cache);
96b5179d 2556
9078a3e1 2557 if (key.objectid >=
db94535d 2558 btrfs_super_total_bytes(&info->super_copy))
9078a3e1
CM
2559 break;
2560 }
0b86a832
CM
2561 ret = 0;
2562error:
9078a3e1 2563 btrfs_free_path(path);
0b86a832 2564 return ret;
9078a3e1 2565}