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