Btrfs: Add a thread pool just for submit_bio
[deliverable/linux.git] / fs / btrfs / extent-tree.c
CommitLineData
6cbd5570
CM
1/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
ec6b910f 18#include <linux/sched.h>
edbd8d4e 19#include <linux/pagemap.h>
ec44a35c 20#include <linux/writeback.h>
74493f7a 21#include "hash.h"
a5eb62e3 22#include "crc32c.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
0ef3e66b
CM
139struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
140 btrfs_fs_info *info,
141 u64 bytenr)
142{
143 struct extent_io_tree *block_group_cache;
144 struct btrfs_block_group_cache *block_group = NULL;
145 u64 ptr;
146 u64 start;
147 u64 end;
148 int ret;
149
150 bytenr = max_t(u64, bytenr,
151 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
152 block_group_cache = &info->block_group_cache;
153 ret = find_first_extent_bit(block_group_cache,
154 bytenr, &start, &end,
155 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
156 BLOCK_GROUP_SYSTEM);
157 if (ret) {
158 return NULL;
159 }
160 ret = get_state_private(block_group_cache, start, &ptr);
161 if (ret)
162 return NULL;
163
164 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
165 return block_group;
166}
167
5276aeda
CM
168struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
169 btrfs_fs_info *info,
db94535d 170 u64 bytenr)
be744175 171{
d1310b2e 172 struct extent_io_tree *block_group_cache;
96b5179d
CM
173 struct btrfs_block_group_cache *block_group = NULL;
174 u64 ptr;
175 u64 start;
176 u64 end;
be744175
CM
177 int ret;
178
a061fc8d
CM
179 bytenr = max_t(u64, bytenr,
180 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
96b5179d
CM
181 block_group_cache = &info->block_group_cache;
182 ret = find_first_extent_bit(block_group_cache,
db94535d 183 bytenr, &start, &end,
0b86a832
CM
184 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
185 BLOCK_GROUP_SYSTEM);
be744175 186 if (ret) {
96b5179d 187 return NULL;
be744175 188 }
96b5179d
CM
189 ret = get_state_private(block_group_cache, start, &ptr);
190 if (ret)
191 return NULL;
192
ae2f5411 193 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
5cf66426 194 if (block_group->key.objectid <= bytenr && bytenr <
96b5179d
CM
195 block_group->key.objectid + block_group->key.offset)
196 return block_group;
be744175
CM
197 return NULL;
198}
0b86a832
CM
199
200static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
201{
593060d7 202 return (cache->flags & bits) == bits;
0b86a832
CM
203}
204
205static int noinline find_search_start(struct btrfs_root *root,
98ed5174 206 struct btrfs_block_group_cache **cache_ret,
0ef3e66b 207 u64 *start_ret, u64 num, int data)
e37c9e69 208{
e37c9e69
CM
209 int ret;
210 struct btrfs_block_group_cache *cache = *cache_ret;
d7fc640e 211 struct extent_io_tree *free_space_cache;
7d1660d4 212 struct extent_state *state;
e19caa5f 213 u64 last;
f510cfec 214 u64 start = 0;
257d0ce3 215 u64 cache_miss = 0;
c31f8830 216 u64 total_fs_bytes;
0b86a832 217 u64 search_start = *start_ret;
f84a8b36 218 int wrapped = 0;
e37c9e69 219
c31f8830 220 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
d7fc640e
CM
221 free_space_cache = &root->fs_info->free_space_cache;
222
0ef3e66b
CM
223 if (!cache)
224 goto out;
225
e37c9e69 226again:
54aa1f4d 227 ret = cache_block_group(root, cache);
0ef3e66b 228 if (ret) {
54aa1f4d 229 goto out;
0ef3e66b 230 }
f84a8b36 231
e19caa5f 232 last = max(search_start, cache->key.objectid);
0ef3e66b 233 if (!block_group_bits(cache, data) || cache->ro)
0b86a832 234 goto new_group;
e19caa5f 235
7d1660d4
CM
236 spin_lock_irq(&free_space_cache->lock);
237 state = find_first_extent_bit_state(free_space_cache, last, EXTENT_DIRTY);
e37c9e69 238 while(1) {
7d1660d4 239 if (!state) {
257d0ce3
CM
240 if (!cache_miss)
241 cache_miss = last;
7d1660d4 242 spin_unlock_irq(&free_space_cache->lock);
e19caa5f
CM
243 goto new_group;
244 }
f510cfec 245
7d1660d4
CM
246 start = max(last, state->start);
247 last = state->end + 1;
257d0ce3 248 if (last - start < num) {
7d1660d4
CM
249 do {
250 state = extent_state_next(state);
251 } while(state && !(state->state & EXTENT_DIRTY));
f510cfec 252 continue;
257d0ce3 253 }
7d1660d4 254 spin_unlock_irq(&free_space_cache->lock);
0ef3e66b 255 if (cache->ro) {
8f18cf13 256 goto new_group;
0ef3e66b 257 }
0b86a832 258 if (start + num > cache->key.objectid + cache->key.offset)
e37c9e69 259 goto new_group;
8790d502 260 if (!block_group_bits(cache, data)) {
611f0e00 261 printk("block group bits don't match %Lu %d\n", cache->flags, data);
8790d502 262 }
0b86a832
CM
263 *start_ret = start;
264 return 0;
8790d502
CM
265 }
266out:
1a2b2ac7
CM
267 cache = btrfs_lookup_block_group(root->fs_info, search_start);
268 if (!cache) {
0b86a832 269 printk("Unable to find block group for %Lu\n", search_start);
1a2b2ac7 270 WARN_ON(1);
1a2b2ac7 271 }
0b86a832 272 return -ENOSPC;
e37c9e69
CM
273
274new_group:
e19caa5f 275 last = cache->key.objectid + cache->key.offset;
f84a8b36 276wrapped:
0ef3e66b 277 cache = btrfs_lookup_first_block_group(root->fs_info, last);
c31f8830 278 if (!cache || cache->key.objectid >= total_fs_bytes) {
0e4de584 279no_cache:
f84a8b36
CM
280 if (!wrapped) {
281 wrapped = 1;
282 last = search_start;
f84a8b36
CM
283 goto wrapped;
284 }
1a2b2ac7 285 goto out;
e37c9e69 286 }
257d0ce3
CM
287 if (cache_miss && !cache->cached) {
288 cache_block_group(root, cache);
289 last = cache_miss;
0ef3e66b 290 cache = btrfs_lookup_first_block_group(root->fs_info, last);
257d0ce3 291 }
0ef3e66b 292 cache_miss = 0;
1a2b2ac7 293 cache = btrfs_find_block_group(root, cache, last, data, 0);
0e4de584
CM
294 if (!cache)
295 goto no_cache;
e37c9e69
CM
296 *cache_ret = cache;
297 goto again;
298}
299
84f54cfa
CM
300static u64 div_factor(u64 num, int factor)
301{
257d0ce3
CM
302 if (factor == 10)
303 return num;
84f54cfa
CM
304 num *= factor;
305 do_div(num, 10);
306 return num;
307}
308
6324fbf3
CM
309static int block_group_state_bits(u64 flags)
310{
311 int bits = 0;
312 if (flags & BTRFS_BLOCK_GROUP_DATA)
313 bits |= BLOCK_GROUP_DATA;
314 if (flags & BTRFS_BLOCK_GROUP_METADATA)
315 bits |= BLOCK_GROUP_METADATA;
316 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
317 bits |= BLOCK_GROUP_SYSTEM;
318 return bits;
319}
320
31f3c99b
CM
321struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
322 struct btrfs_block_group_cache
be744175 323 *hint, u64 search_start,
de428b63 324 int data, int owner)
cd1bc465 325{
96b5179d 326 struct btrfs_block_group_cache *cache;
d1310b2e 327 struct extent_io_tree *block_group_cache;
31f3c99b 328 struct btrfs_block_group_cache *found_group = NULL;
cd1bc465
CM
329 struct btrfs_fs_info *info = root->fs_info;
330 u64 used;
31f3c99b 331 u64 last = 0;
96b5179d
CM
332 u64 start;
333 u64 end;
334 u64 free_check;
335 u64 ptr;
336 int bit;
cd1bc465 337 int ret;
31f3c99b 338 int full_search = 0;
bce4eae9 339 int factor = 10;
0ef3e66b 340 int wrapped = 0;
de428b63 341
96b5179d
CM
342 block_group_cache = &info->block_group_cache;
343
a236aed1
CM
344 if (data & BTRFS_BLOCK_GROUP_METADATA)
345 factor = 9;
be744175 346
6324fbf3 347 bit = block_group_state_bits(data);
be744175 348
0ef3e66b 349 if (search_start) {
be744175 350 struct btrfs_block_group_cache *shint;
0ef3e66b 351 shint = btrfs_lookup_first_block_group(info, search_start);
8f18cf13 352 if (shint && block_group_bits(shint, data) && !shint->ro) {
be744175 353 used = btrfs_block_group_used(&shint->item);
324ae4df
Y
354 if (used + shint->pinned <
355 div_factor(shint->key.offset, factor)) {
be744175
CM
356 return shint;
357 }
358 }
359 }
0ef3e66b 360 if (hint && !hint->ro && block_group_bits(hint, data)) {
31f3c99b 361 used = btrfs_block_group_used(&hint->item);
324ae4df
Y
362 if (used + hint->pinned <
363 div_factor(hint->key.offset, factor)) {
31f3c99b
CM
364 return hint;
365 }
e19caa5f 366 last = hint->key.objectid + hint->key.offset;
31f3c99b 367 } else {
e37c9e69 368 if (hint)
0ef3e66b 369 last = max(hint->key.objectid, search_start);
e37c9e69 370 else
0ef3e66b 371 last = search_start;
31f3c99b 372 }
31f3c99b 373again:
cd1bc465 374 while(1) {
96b5179d
CM
375 ret = find_first_extent_bit(block_group_cache, last,
376 &start, &end, bit);
377 if (ret)
cd1bc465 378 break;
96b5179d
CM
379
380 ret = get_state_private(block_group_cache, start, &ptr);
0ef3e66b
CM
381 if (ret) {
382 last = end + 1;
383 continue;
384 }
96b5179d 385
ae2f5411 386 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
96b5179d
CM
387 last = cache->key.objectid + cache->key.offset;
388 used = btrfs_block_group_used(&cache->item);
389
8f18cf13 390 if (!cache->ro && block_group_bits(cache, data)) {
0ef3e66b 391 free_check = div_factor(cache->key.offset, factor);
8790d502
CM
392 if (used + cache->pinned < free_check) {
393 found_group = cache;
394 goto found;
395 }
6324fbf3 396 }
de428b63 397 cond_resched();
cd1bc465 398 }
0ef3e66b
CM
399 if (!wrapped) {
400 last = search_start;
401 wrapped = 1;
402 goto again;
403 }
404 if (!full_search && factor < 10) {
be744175 405 last = search_start;
31f3c99b 406 full_search = 1;
0ef3e66b 407 factor = 10;
31f3c99b
CM
408 goto again;
409 }
be744175 410found:
31f3c99b 411 return found_group;
cd1bc465
CM
412}
413
7bb86316 414static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation,
74493f7a
CM
415 u64 owner, u64 owner_offset)
416{
417 u32 high_crc = ~(u32)0;
418 u32 low_crc = ~(u32)0;
419 __le64 lenum;
74493f7a 420 lenum = cpu_to_le64(root_objectid);
a5eb62e3 421 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
7bb86316 422 lenum = cpu_to_le64(ref_generation);
a5eb62e3 423 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
21a4989d
CM
424 if (owner >= BTRFS_FIRST_FREE_OBJECTID) {
425 lenum = cpu_to_le64(owner);
a5eb62e3 426 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
21a4989d 427 lenum = cpu_to_le64(owner_offset);
a5eb62e3 428 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
21a4989d 429 }
74493f7a
CM
430 return ((u64)high_crc << 32) | (u64)low_crc;
431}
432
7bb86316
CM
433static int match_extent_ref(struct extent_buffer *leaf,
434 struct btrfs_extent_ref *disk_ref,
435 struct btrfs_extent_ref *cpu_ref)
436{
437 int ret;
438 int len;
439
440 if (cpu_ref->objectid)
441 len = sizeof(*cpu_ref);
442 else
443 len = 2 * sizeof(u64);
444 ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref,
445 len);
446 return ret == 0;
447}
448
98ed5174
CM
449static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
450 struct btrfs_root *root,
451 struct btrfs_path *path, u64 bytenr,
452 u64 root_objectid,
453 u64 ref_generation, u64 owner,
454 u64 owner_offset, int del)
74493f7a
CM
455{
456 u64 hash;
457 struct btrfs_key key;
7bb86316 458 struct btrfs_key found_key;
74493f7a 459 struct btrfs_extent_ref ref;
7bb86316
CM
460 struct extent_buffer *leaf;
461 struct btrfs_extent_ref *disk_ref;
462 int ret;
463 int ret2;
464
465 btrfs_set_stack_ref_root(&ref, root_objectid);
466 btrfs_set_stack_ref_generation(&ref, ref_generation);
467 btrfs_set_stack_ref_objectid(&ref, owner);
468 btrfs_set_stack_ref_offset(&ref, owner_offset);
469
470 hash = hash_extent_ref(root_objectid, ref_generation, owner,
471 owner_offset);
472 key.offset = hash;
473 key.objectid = bytenr;
474 key.type = BTRFS_EXTENT_REF_KEY;
475
476 while (1) {
477 ret = btrfs_search_slot(trans, root, &key, path,
478 del ? -1 : 0, del);
479 if (ret < 0)
480 goto out;
481 leaf = path->nodes[0];
482 if (ret != 0) {
483 u32 nritems = btrfs_header_nritems(leaf);
484 if (path->slots[0] >= nritems) {
485 ret2 = btrfs_next_leaf(root, path);
486 if (ret2)
487 goto out;
488 leaf = path->nodes[0];
489 }
490 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
491 if (found_key.objectid != bytenr ||
492 found_key.type != BTRFS_EXTENT_REF_KEY)
493 goto out;
494 key.offset = found_key.offset;
495 if (del) {
496 btrfs_release_path(root, path);
497 continue;
498 }
499 }
500 disk_ref = btrfs_item_ptr(path->nodes[0],
501 path->slots[0],
502 struct btrfs_extent_ref);
503 if (match_extent_ref(path->nodes[0], disk_ref, &ref)) {
504 ret = 0;
505 goto out;
506 }
507 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
508 key.offset = found_key.offset + 1;
509 btrfs_release_path(root, path);
510 }
511out:
512 return ret;
513}
514
d8d5f3e1
CM
515/*
516 * Back reference rules. Back refs have three main goals:
517 *
518 * 1) differentiate between all holders of references to an extent so that
519 * when a reference is dropped we can make sure it was a valid reference
520 * before freeing the extent.
521 *
522 * 2) Provide enough information to quickly find the holders of an extent
523 * if we notice a given block is corrupted or bad.
524 *
525 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
526 * maintenance. This is actually the same as #2, but with a slightly
527 * different use case.
528 *
529 * File extents can be referenced by:
530 *
531 * - multiple snapshots, subvolumes, or different generations in one subvol
532 * - different files inside a single subvolume (in theory, not implemented yet)
533 * - different offsets inside a file (bookend extents in file.c)
534 *
535 * The extent ref structure has fields for:
536 *
537 * - Objectid of the subvolume root
538 * - Generation number of the tree holding the reference
539 * - objectid of the file holding the reference
540 * - offset in the file corresponding to the key holding the reference
541 *
542 * When a file extent is allocated the fields are filled in:
543 * (root_key.objectid, trans->transid, inode objectid, offset in file)
544 *
545 * When a leaf is cow'd new references are added for every file extent found
546 * in the leaf. It looks the same as the create case, but trans->transid
547 * will be different when the block is cow'd.
548 *
549 * (root_key.objectid, trans->transid, inode objectid, offset in file)
550 *
551 * When a file extent is removed either during snapshot deletion or file
552 * truncation, the corresponding back reference is found
553 * by searching for:
554 *
555 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
556 * inode objectid, offset in file)
557 *
558 * Btree extents can be referenced by:
559 *
560 * - Different subvolumes
561 * - Different generations of the same subvolume
562 *
563 * Storing sufficient information for a full reverse mapping of a btree
564 * block would require storing the lowest key of the block in the backref,
565 * and it would require updating that lowest key either before write out or
566 * every time it changed. Instead, the objectid of the lowest key is stored
567 * along with the level of the tree block. This provides a hint
568 * about where in the btree the block can be found. Searches through the
569 * btree only need to look for a pointer to that block, so they stop one
570 * level higher than the level recorded in the backref.
571 *
572 * Some btrees do not do reference counting on their extents. These
573 * include the extent tree and the tree of tree roots. Backrefs for these
574 * trees always have a generation of zero.
575 *
576 * When a tree block is created, back references are inserted:
577 *
f6dbff55 578 * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
d8d5f3e1
CM
579 *
580 * When a tree block is cow'd in a reference counted root,
581 * new back references are added for all the blocks it points to.
582 * These are of the form (trans->transid will have increased since creation):
583 *
f6dbff55 584 * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
d8d5f3e1
CM
585 *
586 * Because the lowest_key_objectid and the level are just hints
587 * they are not used when backrefs are deleted. When a backref is deleted:
588 *
589 * if backref was for a tree root:
590 * root_objectid = root->root_key.objectid
591 * else
592 * root_objectid = btrfs_header_owner(parent)
593 *
594 * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
595 *
596 * Back Reference Key hashing:
597 *
598 * Back references have four fields, each 64 bits long. Unfortunately,
599 * This is hashed into a single 64 bit number and placed into the key offset.
600 * The key objectid corresponds to the first byte in the extent, and the
601 * key type is set to BTRFS_EXTENT_REF_KEY
602 */
7bb86316
CM
603int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
604 struct btrfs_root *root,
605 struct btrfs_path *path, u64 bytenr,
606 u64 root_objectid, u64 ref_generation,
607 u64 owner, u64 owner_offset)
608{
609 u64 hash;
610 struct btrfs_key key;
611 struct btrfs_extent_ref ref;
612 struct btrfs_extent_ref *disk_ref;
74493f7a
CM
613 int ret;
614
615 btrfs_set_stack_ref_root(&ref, root_objectid);
7bb86316 616 btrfs_set_stack_ref_generation(&ref, ref_generation);
74493f7a
CM
617 btrfs_set_stack_ref_objectid(&ref, owner);
618 btrfs_set_stack_ref_offset(&ref, owner_offset);
619
7bb86316
CM
620 hash = hash_extent_ref(root_objectid, ref_generation, owner,
621 owner_offset);
74493f7a
CM
622 key.offset = hash;
623 key.objectid = bytenr;
624 key.type = BTRFS_EXTENT_REF_KEY;
625
626 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref));
627 while (ret == -EEXIST) {
7bb86316
CM
628 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
629 struct btrfs_extent_ref);
630 if (match_extent_ref(path->nodes[0], disk_ref, &ref))
631 goto out;
632 key.offset++;
633 btrfs_release_path(root, path);
634 ret = btrfs_insert_empty_item(trans, root, path, &key,
635 sizeof(ref));
74493f7a 636 }
7bb86316
CM
637 if (ret)
638 goto out;
639 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
640 struct btrfs_extent_ref);
641 write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref,
642 sizeof(ref));
643 btrfs_mark_buffer_dirty(path->nodes[0]);
644out:
645 btrfs_release_path(root, path);
646 return ret;
74493f7a
CM
647}
648
b18c6685
CM
649int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
650 struct btrfs_root *root,
74493f7a 651 u64 bytenr, u64 num_bytes,
7bb86316 652 u64 root_objectid, u64 ref_generation,
74493f7a 653 u64 owner, u64 owner_offset)
02217ed2 654{
5caf2a00 655 struct btrfs_path *path;
02217ed2 656 int ret;
e2fa7227 657 struct btrfs_key key;
5f39d397 658 struct extent_buffer *l;
234b63a0 659 struct btrfs_extent_item *item;
cf27e1ee 660 u32 refs;
037e6390 661
db94535d 662 WARN_ON(num_bytes < root->sectorsize);
5caf2a00 663 path = btrfs_alloc_path();
54aa1f4d
CM
664 if (!path)
665 return -ENOMEM;
26b8003f 666
3c12ac72 667 path->reada = 1;
db94535d 668 key.objectid = bytenr;
62e2749e 669 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
db94535d 670 key.offset = num_bytes;
5caf2a00 671 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
9f5fae2f 672 0, 1);
54aa1f4d
CM
673 if (ret < 0)
674 return ret;
a429e513 675 if (ret != 0) {
a28ec197 676 BUG();
a429e513 677 }
02217ed2 678 BUG_ON(ret != 0);
5f39d397 679 l = path->nodes[0];
5caf2a00 680 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
5f39d397
CM
681 refs = btrfs_extent_refs(l, item);
682 btrfs_set_extent_refs(l, item, refs + 1);
5caf2a00 683 btrfs_mark_buffer_dirty(path->nodes[0]);
a28ec197 684
5caf2a00 685 btrfs_release_path(root->fs_info->extent_root, path);
7bb86316 686
3c12ac72 687 path->reada = 1;
7bb86316
CM
688 ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
689 path, bytenr, root_objectid,
690 ref_generation, owner, owner_offset);
691 BUG_ON(ret);
9f5fae2f 692 finish_current_insert(trans, root->fs_info->extent_root);
e20d96d6 693 del_pending_extents(trans, root->fs_info->extent_root);
74493f7a
CM
694
695 btrfs_free_path(path);
02217ed2
CM
696 return 0;
697}
698
e9d0b13b
CM
699int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
700 struct btrfs_root *root)
701{
702 finish_current_insert(trans, root->fs_info->extent_root);
703 del_pending_extents(trans, root->fs_info->extent_root);
704 return 0;
705}
706
b18c6685 707static int lookup_extent_ref(struct btrfs_trans_handle *trans,
db94535d
CM
708 struct btrfs_root *root, u64 bytenr,
709 u64 num_bytes, u32 *refs)
a28ec197 710{
5caf2a00 711 struct btrfs_path *path;
a28ec197 712 int ret;
e2fa7227 713 struct btrfs_key key;
5f39d397 714 struct extent_buffer *l;
234b63a0 715 struct btrfs_extent_item *item;
5caf2a00 716
db94535d 717 WARN_ON(num_bytes < root->sectorsize);
5caf2a00 718 path = btrfs_alloc_path();
3c12ac72 719 path->reada = 1;
db94535d
CM
720 key.objectid = bytenr;
721 key.offset = num_bytes;
62e2749e 722 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
5caf2a00 723 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
9f5fae2f 724 0, 0);
54aa1f4d
CM
725 if (ret < 0)
726 goto out;
5f39d397
CM
727 if (ret != 0) {
728 btrfs_print_leaf(root, path->nodes[0]);
db94535d 729 printk("failed to find block number %Lu\n", bytenr);
a28ec197 730 BUG();
5f39d397
CM
731 }
732 l = path->nodes[0];
5caf2a00 733 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
5f39d397 734 *refs = btrfs_extent_refs(l, item);
54aa1f4d 735out:
5caf2a00 736 btrfs_free_path(path);
a28ec197
CM
737 return 0;
738}
739
be20aa9d
CM
740u32 btrfs_count_snapshots_in_path(struct btrfs_root *root,
741 struct btrfs_path *count_path,
a68d5933 742 u64 expected_owner,
be20aa9d
CM
743 u64 first_extent)
744{
745 struct btrfs_root *extent_root = root->fs_info->extent_root;
746 struct btrfs_path *path;
747 u64 bytenr;
748 u64 found_objectid;
a68d5933 749 u64 found_owner;
56b453c9 750 u64 root_objectid = root->root_key.objectid;
be20aa9d 751 u32 total_count = 0;
bbaf549e 752 u32 extent_refs;
be20aa9d 753 u32 cur_count;
be20aa9d
CM
754 u32 nritems;
755 int ret;
756 struct btrfs_key key;
757 struct btrfs_key found_key;
758 struct extent_buffer *l;
759 struct btrfs_extent_item *item;
760 struct btrfs_extent_ref *ref_item;
761 int level = -1;
762
763 path = btrfs_alloc_path();
764again:
765 if (level == -1)
766 bytenr = first_extent;
767 else
768 bytenr = count_path->nodes[level]->start;
769
770 cur_count = 0;
771 key.objectid = bytenr;
772 key.offset = 0;
773
774 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
775 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
776 if (ret < 0)
777 goto out;
778 BUG_ON(ret == 0);
779
780 l = path->nodes[0];
781 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
782
783 if (found_key.objectid != bytenr ||
784 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
785 goto out;
786 }
787
788 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
bbaf549e 789 extent_refs = btrfs_extent_refs(l, item);
be20aa9d 790 while (1) {
bd09835d 791 l = path->nodes[0];
be20aa9d
CM
792 nritems = btrfs_header_nritems(l);
793 if (path->slots[0] >= nritems) {
794 ret = btrfs_next_leaf(extent_root, path);
795 if (ret == 0)
796 continue;
797 break;
798 }
799 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
800 if (found_key.objectid != bytenr)
801 break;
bd09835d 802
be20aa9d
CM
803 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
804 path->slots[0]++;
805 continue;
806 }
807
808 cur_count++;
809 ref_item = btrfs_item_ptr(l, path->slots[0],
810 struct btrfs_extent_ref);
811 found_objectid = btrfs_ref_root(l, ref_item);
812
56b453c9
CM
813 if (found_objectid != root_objectid) {
814 total_count = 2;
4313b399 815 goto out;
56b453c9 816 }
a68d5933
CM
817 if (level == -1) {
818 found_owner = btrfs_ref_objectid(l, ref_item);
819 if (found_owner != expected_owner) {
820 total_count = 2;
821 goto out;
822 }
bbaf549e
CM
823 /*
824 * nasty. we don't count a reference held by
825 * the running transaction. This allows nodatacow
826 * to avoid cow most of the time
827 */
828 if (found_owner >= BTRFS_FIRST_FREE_OBJECTID &&
829 btrfs_ref_generation(l, ref_item) ==
830 root->fs_info->generation) {
831 extent_refs--;
832 }
a68d5933 833 }
56b453c9 834 total_count = 1;
be20aa9d
CM
835 path->slots[0]++;
836 }
bbaf549e
CM
837 /*
838 * if there is more than one reference against a data extent,
839 * we have to assume the other ref is another snapshot
840 */
841 if (level == -1 && extent_refs > 1) {
842 total_count = 2;
843 goto out;
844 }
be20aa9d
CM
845 if (cur_count == 0) {
846 total_count = 0;
847 goto out;
848 }
be20aa9d
CM
849 if (level >= 0 && root->node == count_path->nodes[level])
850 goto out;
851 level++;
852 btrfs_release_path(root, path);
853 goto again;
854
855out:
856 btrfs_free_path(path);
857 return total_count;
be20aa9d 858}
c5739bba 859int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
7bb86316 860 struct btrfs_root *root, u64 owner_objectid)
c5739bba 861{
7bb86316
CM
862 u64 generation;
863 u64 key_objectid;
864 u64 level;
865 u32 nritems;
866 struct btrfs_disk_key disk_key;
867
868 level = btrfs_header_level(root->node);
869 generation = trans->transid;
870 nritems = btrfs_header_nritems(root->node);
871 if (nritems > 0) {
872 if (level == 0)
873 btrfs_item_key(root->node, &disk_key, 0);
874 else
875 btrfs_node_key(root->node, &disk_key, 0);
876 key_objectid = btrfs_disk_key_objectid(&disk_key);
877 } else {
878 key_objectid = 0;
879 }
db94535d 880 return btrfs_inc_extent_ref(trans, root, root->node->start,
7bb86316 881 root->node->len, owner_objectid,
f6dbff55 882 generation, level, key_objectid);
c5739bba
CM
883}
884
e089f05c 885int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
5f39d397 886 struct extent_buffer *buf)
02217ed2 887{
db94535d 888 u64 bytenr;
5f39d397
CM
889 u32 nritems;
890 struct btrfs_key key;
6407bf6d 891 struct btrfs_file_extent_item *fi;
02217ed2 892 int i;
db94535d 893 int level;
6407bf6d 894 int ret;
54aa1f4d 895 int faili;
a28ec197 896
3768f368 897 if (!root->ref_cows)
a28ec197 898 return 0;
5f39d397 899
db94535d 900 level = btrfs_header_level(buf);
5f39d397
CM
901 nritems = btrfs_header_nritems(buf);
902 for (i = 0; i < nritems; i++) {
db94535d
CM
903 if (level == 0) {
904 u64 disk_bytenr;
5f39d397
CM
905 btrfs_item_key_to_cpu(buf, &key, i);
906 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
6407bf6d 907 continue;
5f39d397 908 fi = btrfs_item_ptr(buf, i,
6407bf6d 909 struct btrfs_file_extent_item);
5f39d397 910 if (btrfs_file_extent_type(buf, fi) ==
236454df
CM
911 BTRFS_FILE_EXTENT_INLINE)
912 continue;
db94535d
CM
913 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
914 if (disk_bytenr == 0)
3a686375 915 continue;
db94535d 916 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
7bb86316
CM
917 btrfs_file_extent_disk_num_bytes(buf, fi),
918 root->root_key.objectid, trans->transid,
919 key.objectid, key.offset);
54aa1f4d
CM
920 if (ret) {
921 faili = i;
922 goto fail;
923 }
6407bf6d 924 } else {
db94535d 925 bytenr = btrfs_node_blockptr(buf, i);
6caab489 926 btrfs_node_key_to_cpu(buf, &key, i);
db94535d 927 ret = btrfs_inc_extent_ref(trans, root, bytenr,
7bb86316
CM
928 btrfs_level_size(root, level - 1),
929 root->root_key.objectid,
f6dbff55
CM
930 trans->transid,
931 level - 1, key.objectid);
54aa1f4d
CM
932 if (ret) {
933 faili = i;
934 goto fail;
935 }
6407bf6d 936 }
02217ed2
CM
937 }
938 return 0;
54aa1f4d 939fail:
ccd467d6 940 WARN_ON(1);
7bb86316 941#if 0
54aa1f4d 942 for (i =0; i < faili; i++) {
db94535d
CM
943 if (level == 0) {
944 u64 disk_bytenr;
5f39d397
CM
945 btrfs_item_key_to_cpu(buf, &key, i);
946 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
54aa1f4d 947 continue;
5f39d397 948 fi = btrfs_item_ptr(buf, i,
54aa1f4d 949 struct btrfs_file_extent_item);
5f39d397 950 if (btrfs_file_extent_type(buf, fi) ==
54aa1f4d
CM
951 BTRFS_FILE_EXTENT_INLINE)
952 continue;
db94535d
CM
953 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
954 if (disk_bytenr == 0)
54aa1f4d 955 continue;
db94535d
CM
956 err = btrfs_free_extent(trans, root, disk_bytenr,
957 btrfs_file_extent_disk_num_bytes(buf,
5f39d397 958 fi), 0);
54aa1f4d
CM
959 BUG_ON(err);
960 } else {
db94535d
CM
961 bytenr = btrfs_node_blockptr(buf, i);
962 err = btrfs_free_extent(trans, root, bytenr,
963 btrfs_level_size(root, level - 1), 0);
54aa1f4d
CM
964 BUG_ON(err);
965 }
966 }
7bb86316 967#endif
54aa1f4d 968 return ret;
02217ed2
CM
969}
970
9078a3e1
CM
971static int write_one_cache_group(struct btrfs_trans_handle *trans,
972 struct btrfs_root *root,
973 struct btrfs_path *path,
974 struct btrfs_block_group_cache *cache)
975{
976 int ret;
977 int pending_ret;
978 struct btrfs_root *extent_root = root->fs_info->extent_root;
5f39d397
CM
979 unsigned long bi;
980 struct extent_buffer *leaf;
9078a3e1 981
9078a3e1 982 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
54aa1f4d
CM
983 if (ret < 0)
984 goto fail;
9078a3e1 985 BUG_ON(ret);
5f39d397
CM
986
987 leaf = path->nodes[0];
988 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
989 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
990 btrfs_mark_buffer_dirty(leaf);
9078a3e1 991 btrfs_release_path(extent_root, path);
54aa1f4d 992fail:
9078a3e1
CM
993 finish_current_insert(trans, extent_root);
994 pending_ret = del_pending_extents(trans, extent_root);
995 if (ret)
996 return ret;
997 if (pending_ret)
998 return pending_ret;
999 return 0;
1000
1001}
1002
96b5179d
CM
1003int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1004 struct btrfs_root *root)
9078a3e1 1005{
d1310b2e 1006 struct extent_io_tree *block_group_cache;
96b5179d 1007 struct btrfs_block_group_cache *cache;
9078a3e1
CM
1008 int ret;
1009 int err = 0;
1010 int werr = 0;
9078a3e1 1011 struct btrfs_path *path;
96b5179d
CM
1012 u64 last = 0;
1013 u64 start;
1014 u64 end;
1015 u64 ptr;
9078a3e1 1016
96b5179d 1017 block_group_cache = &root->fs_info->block_group_cache;
9078a3e1
CM
1018 path = btrfs_alloc_path();
1019 if (!path)
1020 return -ENOMEM;
1021
1022 while(1) {
96b5179d
CM
1023 ret = find_first_extent_bit(block_group_cache, last,
1024 &start, &end, BLOCK_GROUP_DIRTY);
1025 if (ret)
9078a3e1 1026 break;
54aa1f4d 1027
96b5179d
CM
1028 last = end + 1;
1029 ret = get_state_private(block_group_cache, start, &ptr);
1030 if (ret)
1031 break;
ae2f5411 1032 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
96b5179d
CM
1033 err = write_one_cache_group(trans, root,
1034 path, cache);
1035 /*
1036 * if we fail to write the cache group, we want
1037 * to keep it marked dirty in hopes that a later
1038 * write will work
1039 */
1040 if (err) {
1041 werr = err;
1042 continue;
9078a3e1 1043 }
96b5179d
CM
1044 clear_extent_bits(block_group_cache, start, end,
1045 BLOCK_GROUP_DIRTY, GFP_NOFS);
9078a3e1
CM
1046 }
1047 btrfs_free_path(path);
1048 return werr;
1049}
1050
6324fbf3
CM
1051static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
1052 u64 flags)
1053{
1054 struct list_head *head = &info->space_info;
1055 struct list_head *cur;
1056 struct btrfs_space_info *found;
1057 list_for_each(cur, head) {
1058 found = list_entry(cur, struct btrfs_space_info, list);
1059 if (found->flags == flags)
1060 return found;
1061 }
1062 return NULL;
1063
1064}
1065
593060d7
CM
1066static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1067 u64 total_bytes, u64 bytes_used,
1068 struct btrfs_space_info **space_info)
1069{
1070 struct btrfs_space_info *found;
1071
1072 found = __find_space_info(info, flags);
1073 if (found) {
1074 found->total_bytes += total_bytes;
1075 found->bytes_used += bytes_used;
8f18cf13 1076 found->full = 0;
593060d7
CM
1077 WARN_ON(found->total_bytes < found->bytes_used);
1078 *space_info = found;
1079 return 0;
1080 }
1081 found = kmalloc(sizeof(*found), GFP_NOFS);
1082 if (!found)
1083 return -ENOMEM;
1084
1085 list_add(&found->list, &info->space_info);
1086 found->flags = flags;
1087 found->total_bytes = total_bytes;
1088 found->bytes_used = bytes_used;
1089 found->bytes_pinned = 0;
1090 found->full = 0;
0ef3e66b 1091 found->force_alloc = 0;
593060d7
CM
1092 *space_info = found;
1093 return 0;
1094}
1095
8790d502
CM
1096static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1097{
1098 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
611f0e00 1099 BTRFS_BLOCK_GROUP_RAID1 |
321aecc6 1100 BTRFS_BLOCK_GROUP_RAID10 |
611f0e00 1101 BTRFS_BLOCK_GROUP_DUP);
8790d502
CM
1102 if (extra_flags) {
1103 if (flags & BTRFS_BLOCK_GROUP_DATA)
1104 fs_info->avail_data_alloc_bits |= extra_flags;
1105 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1106 fs_info->avail_metadata_alloc_bits |= extra_flags;
1107 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1108 fs_info->avail_system_alloc_bits |= extra_flags;
1109 }
1110}
593060d7 1111
a061fc8d 1112static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags)
ec44a35c 1113{
a061fc8d
CM
1114 u64 num_devices = root->fs_info->fs_devices->num_devices;
1115
1116 if (num_devices == 1)
1117 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1118 if (num_devices < 4)
1119 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1120
ec44a35c
CM
1121 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1122 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
a061fc8d 1123 BTRFS_BLOCK_GROUP_RAID10))) {
ec44a35c 1124 flags &= ~BTRFS_BLOCK_GROUP_DUP;
a061fc8d 1125 }
ec44a35c
CM
1126
1127 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
a061fc8d 1128 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
ec44a35c 1129 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
a061fc8d 1130 }
ec44a35c
CM
1131
1132 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1133 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1134 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1135 (flags & BTRFS_BLOCK_GROUP_DUP)))
1136 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1137 return flags;
1138}
1139
6324fbf3
CM
1140static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1141 struct btrfs_root *extent_root, u64 alloc_bytes,
0ef3e66b 1142 u64 flags, int force)
6324fbf3
CM
1143{
1144 struct btrfs_space_info *space_info;
1145 u64 thresh;
1146 u64 start;
1147 u64 num_bytes;
1148 int ret;
1149
a061fc8d 1150 flags = reduce_alloc_profile(extent_root, flags);
ec44a35c 1151
6324fbf3 1152 space_info = __find_space_info(extent_root->fs_info, flags);
593060d7
CM
1153 if (!space_info) {
1154 ret = update_space_info(extent_root->fs_info, flags,
1155 0, 0, &space_info);
1156 BUG_ON(ret);
1157 }
6324fbf3
CM
1158 BUG_ON(!space_info);
1159
0ef3e66b
CM
1160 if (space_info->force_alloc) {
1161 force = 1;
1162 space_info->force_alloc = 0;
1163 }
6324fbf3
CM
1164 if (space_info->full)
1165 return 0;
1166
8790d502 1167 thresh = div_factor(space_info->total_bytes, 6);
0ef3e66b
CM
1168 if (!force &&
1169 (space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) <
6324fbf3
CM
1170 thresh)
1171 return 0;
1172
1173 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
1174 if (ret == -ENOSPC) {
1175printk("space info full %Lu\n", flags);
1176 space_info->full = 1;
1177 return 0;
1178 }
6324fbf3
CM
1179 BUG_ON(ret);
1180
1181 ret = btrfs_make_block_group(trans, extent_root, 0, flags,
e17cade2 1182 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
6324fbf3 1183 BUG_ON(ret);
593060d7 1184
6324fbf3
CM
1185 return 0;
1186}
1187
9078a3e1
CM
1188static int update_block_group(struct btrfs_trans_handle *trans,
1189 struct btrfs_root *root,
db94535d 1190 u64 bytenr, u64 num_bytes, int alloc,
0b86a832 1191 int mark_free)
9078a3e1
CM
1192{
1193 struct btrfs_block_group_cache *cache;
1194 struct btrfs_fs_info *info = root->fs_info;
db94535d 1195 u64 total = num_bytes;
9078a3e1 1196 u64 old_val;
db94535d 1197 u64 byte_in_group;
96b5179d
CM
1198 u64 start;
1199 u64 end;
3e1ad54f 1200
9078a3e1 1201 while(total) {
db94535d 1202 cache = btrfs_lookup_block_group(info, bytenr);
3e1ad54f 1203 if (!cache) {
9078a3e1 1204 return -1;
cd1bc465 1205 }
db94535d
CM
1206 byte_in_group = bytenr - cache->key.objectid;
1207 WARN_ON(byte_in_group > cache->key.offset);
96b5179d
CM
1208 start = cache->key.objectid;
1209 end = start + cache->key.offset - 1;
1210 set_extent_bits(&info->block_group_cache, start, end,
1211 BLOCK_GROUP_DIRTY, GFP_NOFS);
9078a3e1
CM
1212
1213 old_val = btrfs_block_group_used(&cache->item);
db94535d 1214 num_bytes = min(total, cache->key.offset - byte_in_group);
cd1bc465 1215 if (alloc) {
db94535d 1216 old_val += num_bytes;
6324fbf3 1217 cache->space_info->bytes_used += num_bytes;
cd1bc465 1218 } else {
db94535d 1219 old_val -= num_bytes;
6324fbf3 1220 cache->space_info->bytes_used -= num_bytes;
f510cfec
CM
1221 if (mark_free) {
1222 set_extent_dirty(&info->free_space_cache,
db94535d 1223 bytenr, bytenr + num_bytes - 1,
f510cfec 1224 GFP_NOFS);
e37c9e69 1225 }
cd1bc465 1226 }
9078a3e1 1227 btrfs_set_block_group_used(&cache->item, old_val);
db94535d
CM
1228 total -= num_bytes;
1229 bytenr += num_bytes;
9078a3e1
CM
1230 }
1231 return 0;
1232}
6324fbf3 1233
a061fc8d
CM
1234static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1235{
1236 u64 start;
1237 u64 end;
1238 int ret;
1239 ret = find_first_extent_bit(&root->fs_info->block_group_cache,
1240 search_start, &start, &end,
1241 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
1242 BLOCK_GROUP_SYSTEM);
1243 if (ret)
1244 return 0;
1245 return start;
1246}
1247
1248
324ae4df
Y
1249static int update_pinned_extents(struct btrfs_root *root,
1250 u64 bytenr, u64 num, int pin)
1251{
1252 u64 len;
1253 struct btrfs_block_group_cache *cache;
1254 struct btrfs_fs_info *fs_info = root->fs_info;
1255
1256 if (pin) {
1257 set_extent_dirty(&fs_info->pinned_extents,
1258 bytenr, bytenr + num - 1, GFP_NOFS);
1259 } else {
1260 clear_extent_dirty(&fs_info->pinned_extents,
1261 bytenr, bytenr + num - 1, GFP_NOFS);
1262 }
1263 while (num > 0) {
1264 cache = btrfs_lookup_block_group(fs_info, bytenr);
a061fc8d
CM
1265 if (!cache) {
1266 u64 first = first_logical_byte(root, bytenr);
1267 WARN_ON(first < bytenr);
1268 len = min(first - bytenr, num);
1269 } else {
1270 len = min(num, cache->key.offset -
1271 (bytenr - cache->key.objectid));
1272 }
324ae4df 1273 if (pin) {
a061fc8d
CM
1274 if (cache) {
1275 cache->pinned += len;
1276 cache->space_info->bytes_pinned += len;
1277 }
324ae4df
Y
1278 fs_info->total_pinned += len;
1279 } else {
a061fc8d
CM
1280 if (cache) {
1281 cache->pinned -= len;
1282 cache->space_info->bytes_pinned -= len;
1283 }
324ae4df
Y
1284 fs_info->total_pinned -= len;
1285 }
1286 bytenr += len;
1287 num -= len;
1288 }
1289 return 0;
1290}
9078a3e1 1291
d1310b2e 1292int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
ccd467d6 1293{
ccd467d6 1294 u64 last = 0;
1a5bc167
CM
1295 u64 start;
1296 u64 end;
d1310b2e 1297 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
ccd467d6 1298 int ret;
ccd467d6
CM
1299
1300 while(1) {
1a5bc167
CM
1301 ret = find_first_extent_bit(pinned_extents, last,
1302 &start, &end, EXTENT_DIRTY);
1303 if (ret)
ccd467d6 1304 break;
1a5bc167
CM
1305 set_extent_dirty(copy, start, end, GFP_NOFS);
1306 last = end + 1;
ccd467d6
CM
1307 }
1308 return 0;
1309}
1310
1311int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1312 struct btrfs_root *root,
d1310b2e 1313 struct extent_io_tree *unpin)
a28ec197 1314{
1a5bc167
CM
1315 u64 start;
1316 u64 end;
a28ec197 1317 int ret;
d1310b2e 1318 struct extent_io_tree *free_space_cache;
f510cfec 1319 free_space_cache = &root->fs_info->free_space_cache;
a28ec197
CM
1320
1321 while(1) {
1a5bc167
CM
1322 ret = find_first_extent_bit(unpin, 0, &start, &end,
1323 EXTENT_DIRTY);
1324 if (ret)
a28ec197 1325 break;
324ae4df 1326 update_pinned_extents(root, start, end + 1 - start, 0);
1a5bc167
CM
1327 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1328 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
a28ec197
CM
1329 }
1330 return 0;
1331}
1332
98ed5174
CM
1333static int finish_current_insert(struct btrfs_trans_handle *trans,
1334 struct btrfs_root *extent_root)
037e6390 1335{
7bb86316
CM
1336 u64 start;
1337 u64 end;
1338 struct btrfs_fs_info *info = extent_root->fs_info;
d8d5f3e1 1339 struct extent_buffer *eb;
7bb86316 1340 struct btrfs_path *path;
e2fa7227 1341 struct btrfs_key ins;
d8d5f3e1 1342 struct btrfs_disk_key first;
234b63a0 1343 struct btrfs_extent_item extent_item;
037e6390 1344 int ret;
d8d5f3e1 1345 int level;
1a5bc167 1346 int err = 0;
037e6390 1347
5f39d397 1348 btrfs_set_stack_extent_refs(&extent_item, 1);
62e2749e 1349 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
7bb86316 1350 path = btrfs_alloc_path();
037e6390 1351
26b8003f 1352 while(1) {
1a5bc167
CM
1353 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1354 &end, EXTENT_LOCKED);
1355 if (ret)
26b8003f
CM
1356 break;
1357
1a5bc167
CM
1358 ins.objectid = start;
1359 ins.offset = end + 1 - start;
1360 err = btrfs_insert_item(trans, extent_root, &ins,
1361 &extent_item, sizeof(extent_item));
1362 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
1363 GFP_NOFS);
ca7a79ad
CM
1364 eb = read_tree_block(extent_root, ins.objectid, ins.offset,
1365 trans->transid);
d8d5f3e1
CM
1366 level = btrfs_header_level(eb);
1367 if (level == 0) {
1368 btrfs_item_key(eb, &first, 0);
1369 } else {
1370 btrfs_node_key(eb, &first, 0);
1371 }
7bb86316
CM
1372 err = btrfs_insert_extent_backref(trans, extent_root, path,
1373 start, extent_root->root_key.objectid,
f6dbff55
CM
1374 0, level,
1375 btrfs_disk_key_objectid(&first));
7bb86316 1376 BUG_ON(err);
d8d5f3e1 1377 free_extent_buffer(eb);
037e6390 1378 }
7bb86316 1379 btrfs_free_path(path);
037e6390
CM
1380 return 0;
1381}
1382
db94535d
CM
1383static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
1384 int pending)
e20d96d6 1385{
1a5bc167 1386 int err = 0;
5f39d397 1387 struct extent_buffer *buf;
8ef97622 1388
f4b9aa8d 1389 if (!pending) {
db94535d 1390 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
5f39d397 1391 if (buf) {
1259ab75 1392 if (btrfs_buffer_uptodate(buf, 0)) {
2c90e5d6
CM
1393 u64 transid =
1394 root->fs_info->running_transaction->transid;
dc17ff8f
CM
1395 u64 header_transid =
1396 btrfs_header_generation(buf);
6bc34676
CM
1397 if (header_transid == transid &&
1398 !btrfs_header_flag(buf,
1399 BTRFS_HEADER_FLAG_WRITTEN)) {
55c69072 1400 clean_tree_block(NULL, root, buf);
5f39d397 1401 free_extent_buffer(buf);
c549228f 1402 return 1;
2c90e5d6 1403 }
f4b9aa8d 1404 }
5f39d397 1405 free_extent_buffer(buf);
8ef97622 1406 }
324ae4df 1407 update_pinned_extents(root, bytenr, num_bytes, 1);
f4b9aa8d 1408 } else {
1a5bc167 1409 set_extent_bits(&root->fs_info->pending_del,
db94535d
CM
1410 bytenr, bytenr + num_bytes - 1,
1411 EXTENT_LOCKED, GFP_NOFS);
f4b9aa8d 1412 }
be744175 1413 BUG_ON(err < 0);
e20d96d6
CM
1414 return 0;
1415}
1416
fec577fb 1417/*
a28ec197 1418 * remove an extent from the root, returns 0 on success
fec577fb 1419 */
e089f05c 1420static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
7bb86316
CM
1421 *root, u64 bytenr, u64 num_bytes,
1422 u64 root_objectid, u64 ref_generation,
1423 u64 owner_objectid, u64 owner_offset, int pin,
e37c9e69 1424 int mark_free)
a28ec197 1425{
5caf2a00 1426 struct btrfs_path *path;
e2fa7227 1427 struct btrfs_key key;
1261ec42
CM
1428 struct btrfs_fs_info *info = root->fs_info;
1429 struct btrfs_root *extent_root = info->extent_root;
5f39d397 1430 struct extent_buffer *leaf;
a28ec197 1431 int ret;
952fccac
CM
1432 int extent_slot = 0;
1433 int found_extent = 0;
1434 int num_to_del = 1;
234b63a0 1435 struct btrfs_extent_item *ei;
cf27e1ee 1436 u32 refs;
037e6390 1437
db94535d 1438 key.objectid = bytenr;
62e2749e 1439 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
db94535d 1440 key.offset = num_bytes;
5caf2a00 1441 path = btrfs_alloc_path();
54aa1f4d
CM
1442 if (!path)
1443 return -ENOMEM;
5f26f772 1444
3c12ac72 1445 path->reada = 1;
7bb86316
CM
1446 ret = lookup_extent_backref(trans, extent_root, path,
1447 bytenr, root_objectid,
1448 ref_generation,
1449 owner_objectid, owner_offset, 1);
1450 if (ret == 0) {
952fccac
CM
1451 struct btrfs_key found_key;
1452 extent_slot = path->slots[0];
1453 while(extent_slot > 0) {
1454 extent_slot--;
1455 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1456 extent_slot);
1457 if (found_key.objectid != bytenr)
1458 break;
1459 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
1460 found_key.offset == num_bytes) {
1461 found_extent = 1;
1462 break;
1463 }
1464 if (path->slots[0] - extent_slot > 5)
1465 break;
1466 }
1467 if (!found_extent)
1468 ret = btrfs_del_item(trans, extent_root, path);
7bb86316
CM
1469 } else {
1470 btrfs_print_leaf(extent_root, path->nodes[0]);
1471 WARN_ON(1);
1472 printk("Unable to find ref byte nr %Lu root %Lu "
1473 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1474 root_objectid, ref_generation, owner_objectid,
1475 owner_offset);
1476 }
952fccac
CM
1477 if (!found_extent) {
1478 btrfs_release_path(extent_root, path);
1479 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
1480 if (ret < 0)
1481 return ret;
1482 BUG_ON(ret);
1483 extent_slot = path->slots[0];
1484 }
5f39d397
CM
1485
1486 leaf = path->nodes[0];
952fccac 1487 ei = btrfs_item_ptr(leaf, extent_slot,
123abc88 1488 struct btrfs_extent_item);
5f39d397
CM
1489 refs = btrfs_extent_refs(leaf, ei);
1490 BUG_ON(refs == 0);
1491 refs -= 1;
1492 btrfs_set_extent_refs(leaf, ei, refs);
952fccac 1493
5f39d397
CM
1494 btrfs_mark_buffer_dirty(leaf);
1495
952fccac
CM
1496 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
1497 /* if the back ref and the extent are next to each other
1498 * they get deleted below in one shot
1499 */
1500 path->slots[0] = extent_slot;
1501 num_to_del = 2;
1502 } else if (found_extent) {
1503 /* otherwise delete the extent back ref */
1504 ret = btrfs_del_item(trans, extent_root, path);
1505 BUG_ON(ret);
1506 /* if refs are 0, we need to setup the path for deletion */
1507 if (refs == 0) {
1508 btrfs_release_path(extent_root, path);
1509 ret = btrfs_search_slot(trans, extent_root, &key, path,
1510 -1, 1);
1511 if (ret < 0)
1512 return ret;
1513 BUG_ON(ret);
1514 }
1515 }
1516
cf27e1ee 1517 if (refs == 0) {
db94535d
CM
1518 u64 super_used;
1519 u64 root_used;
78fae27e
CM
1520
1521 if (pin) {
db94535d 1522 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
c549228f
Y
1523 if (ret > 0)
1524 mark_free = 1;
1525 BUG_ON(ret < 0);
78fae27e
CM
1526 }
1527
58176a96 1528 /* block accounting for super block */
db94535d
CM
1529 super_used = btrfs_super_bytes_used(&info->super_copy);
1530 btrfs_set_super_bytes_used(&info->super_copy,
1531 super_used - num_bytes);
58176a96
JB
1532
1533 /* block accounting for root item */
db94535d 1534 root_used = btrfs_root_used(&root->root_item);
5f39d397 1535 btrfs_set_root_used(&root->root_item,
db94535d 1536 root_used - num_bytes);
952fccac
CM
1537 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1538 num_to_del);
54aa1f4d
CM
1539 if (ret) {
1540 return ret;
1541 }
db94535d 1542 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
0b86a832 1543 mark_free);
9078a3e1 1544 BUG_ON(ret);
a28ec197 1545 }
5caf2a00 1546 btrfs_free_path(path);
e089f05c 1547 finish_current_insert(trans, extent_root);
a28ec197
CM
1548 return ret;
1549}
1550
a28ec197
CM
1551/*
1552 * find all the blocks marked as pending in the radix tree and remove
1553 * them from the extent map
1554 */
e089f05c
CM
1555static int del_pending_extents(struct btrfs_trans_handle *trans, struct
1556 btrfs_root *extent_root)
a28ec197
CM
1557{
1558 int ret;
e20d96d6 1559 int err = 0;
1a5bc167
CM
1560 u64 start;
1561 u64 end;
d1310b2e
CM
1562 struct extent_io_tree *pending_del;
1563 struct extent_io_tree *pinned_extents;
8ef97622 1564
1a5bc167
CM
1565 pending_del = &extent_root->fs_info->pending_del;
1566 pinned_extents = &extent_root->fs_info->pinned_extents;
a28ec197
CM
1567
1568 while(1) {
1a5bc167
CM
1569 ret = find_first_extent_bit(pending_del, 0, &start, &end,
1570 EXTENT_LOCKED);
1571 if (ret)
a28ec197 1572 break;
324ae4df 1573 update_pinned_extents(extent_root, start, end + 1 - start, 1);
1a5bc167
CM
1574 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
1575 GFP_NOFS);
1576 ret = __free_extent(trans, extent_root,
7bb86316
CM
1577 start, end + 1 - start,
1578 extent_root->root_key.objectid,
1579 0, 0, 0, 0, 0);
1a5bc167
CM
1580 if (ret)
1581 err = ret;
fec577fb 1582 }
e20d96d6 1583 return err;
fec577fb
CM
1584}
1585
1586/*
1587 * remove an extent from the root, returns 0 on success
1588 */
e089f05c 1589int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
7bb86316
CM
1590 *root, u64 bytenr, u64 num_bytes,
1591 u64 root_objectid, u64 ref_generation,
1592 u64 owner_objectid, u64 owner_offset, int pin)
fec577fb 1593{
9f5fae2f 1594 struct btrfs_root *extent_root = root->fs_info->extent_root;
fec577fb
CM
1595 int pending_ret;
1596 int ret;
a28ec197 1597
db94535d 1598 WARN_ON(num_bytes < root->sectorsize);
7bb86316
CM
1599 if (!root->ref_cows)
1600 ref_generation = 0;
1601
fec577fb 1602 if (root == extent_root) {
db94535d 1603 pin_down_bytes(root, bytenr, num_bytes, 1);
fec577fb
CM
1604 return 0;
1605 }
7bb86316
CM
1606 ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
1607 ref_generation, owner_objectid, owner_offset,
1608 pin, pin == 0);
e20d96d6 1609 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
fec577fb
CM
1610 return ret ? ret : pending_ret;
1611}
1612
87ee04eb
CM
1613static u64 stripe_align(struct btrfs_root *root, u64 val)
1614{
1615 u64 mask = ((u64)root->stripesize - 1);
1616 u64 ret = (val + mask) & ~mask;
1617 return ret;
1618}
1619
fec577fb
CM
1620/*
1621 * walks the btree of allocated extents and find a hole of a given size.
1622 * The key ins is changed to record the hole:
1623 * ins->objectid == block start
62e2749e 1624 * ins->flags = BTRFS_EXTENT_ITEM_KEY
fec577fb
CM
1625 * ins->offset == number of blocks
1626 * Any available blocks before search_start are skipped.
1627 */
98ed5174
CM
1628static int noinline find_free_extent(struct btrfs_trans_handle *trans,
1629 struct btrfs_root *orig_root,
1630 u64 num_bytes, u64 empty_size,
1631 u64 search_start, u64 search_end,
1632 u64 hint_byte, struct btrfs_key *ins,
1633 u64 exclude_start, u64 exclude_nr,
1634 int data)
fec577fb 1635{
87ee04eb 1636 int ret;
a061fc8d 1637 u64 orig_search_start;
9f5fae2f 1638 struct btrfs_root * root = orig_root->fs_info->extent_root;
f2458e1d 1639 struct btrfs_fs_info *info = root->fs_info;
db94535d 1640 u64 total_needed = num_bytes;
239b14b3 1641 u64 *last_ptr = NULL;
be08c1b9 1642 struct btrfs_block_group_cache *block_group;
be744175 1643 int full_scan = 0;
fbdc762b 1644 int wrapped = 0;
0ef3e66b 1645 int chunk_alloc_done = 0;
239b14b3 1646 int empty_cluster = 2 * 1024 * 1024;
0ef3e66b 1647 int allowed_chunk_alloc = 0;
fec577fb 1648
db94535d 1649 WARN_ON(num_bytes < root->sectorsize);
b1a4d965
CM
1650 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1651
0ef3e66b
CM
1652 if (orig_root->ref_cows || empty_size)
1653 allowed_chunk_alloc = 1;
1654
239b14b3
CM
1655 if (data & BTRFS_BLOCK_GROUP_METADATA) {
1656 last_ptr = &root->fs_info->last_alloc;
8790d502 1657 empty_cluster = 256 * 1024;
239b14b3
CM
1658 }
1659
1660 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
1661 last_ptr = &root->fs_info->last_data_alloc;
1662 }
1663
1664 if (last_ptr) {
1665 if (*last_ptr)
1666 hint_byte = *last_ptr;
1667 else {
1668 empty_size += empty_cluster;
1669 }
1670 }
1671
a061fc8d
CM
1672 search_start = max(search_start, first_logical_byte(root, 0));
1673 orig_search_start = search_start;
1674
7f93bf8d
CM
1675 if (search_end == (u64)-1)
1676 search_end = btrfs_super_total_bytes(&info->super_copy);
0b86a832 1677
db94535d 1678 if (hint_byte) {
0ef3e66b 1679 block_group = btrfs_lookup_first_block_group(info, hint_byte);
1a2b2ac7
CM
1680 if (!block_group)
1681 hint_byte = search_start;
be744175 1682 block_group = btrfs_find_block_group(root, block_group,
db94535d 1683 hint_byte, data, 1);
239b14b3
CM
1684 if (last_ptr && *last_ptr == 0 && block_group)
1685 hint_byte = block_group->key.objectid;
be744175
CM
1686 } else {
1687 block_group = btrfs_find_block_group(root,
1a2b2ac7
CM
1688 trans->block_group,
1689 search_start, data, 1);
be744175 1690 }
239b14b3 1691 search_start = max(search_start, hint_byte);
be744175 1692
6702ed49 1693 total_needed += empty_size;
0b86a832 1694
be744175 1695check_failed:
70b043f0 1696 if (!block_group) {
0ef3e66b
CM
1697 block_group = btrfs_lookup_first_block_group(info,
1698 search_start);
70b043f0 1699 if (!block_group)
0ef3e66b 1700 block_group = btrfs_lookup_first_block_group(info,
70b043f0
CM
1701 orig_search_start);
1702 }
0ef3e66b
CM
1703 if (full_scan && !chunk_alloc_done) {
1704 if (allowed_chunk_alloc) {
1705 do_chunk_alloc(trans, root,
1706 num_bytes + 2 * 1024 * 1024, data, 1);
1707 allowed_chunk_alloc = 0;
1708 } else if (block_group && block_group_bits(block_group, data)) {
1709 block_group->space_info->force_alloc = 1;
1710 }
1711 chunk_alloc_done = 1;
1712 }
0b86a832
CM
1713 ret = find_search_start(root, &block_group, &search_start,
1714 total_needed, data);
239b14b3
CM
1715 if (ret == -ENOSPC && last_ptr && *last_ptr) {
1716 *last_ptr = 0;
0ef3e66b
CM
1717 block_group = btrfs_lookup_first_block_group(info,
1718 orig_search_start);
239b14b3
CM
1719 search_start = orig_search_start;
1720 ret = find_search_start(root, &block_group, &search_start,
1721 total_needed, data);
1722 }
1723 if (ret == -ENOSPC)
1724 goto enospc;
0b86a832 1725 if (ret)
d548ee51 1726 goto error;
e19caa5f 1727
239b14b3
CM
1728 if (last_ptr && *last_ptr && search_start != *last_ptr) {
1729 *last_ptr = 0;
1730 if (!empty_size) {
1731 empty_size += empty_cluster;
1732 total_needed += empty_size;
1733 }
0ef3e66b 1734 block_group = btrfs_lookup_first_block_group(info,
239b14b3
CM
1735 orig_search_start);
1736 search_start = orig_search_start;
1737 ret = find_search_start(root, &block_group,
1738 &search_start, total_needed, data);
1739 if (ret == -ENOSPC)
1740 goto enospc;
1741 if (ret)
1742 goto error;
1743 }
1744
0b86a832
CM
1745 search_start = stripe_align(root, search_start);
1746 ins->objectid = search_start;
1747 ins->offset = num_bytes;
e37c9e69 1748
db94535d 1749 if (ins->objectid + num_bytes >= search_end)
cf67582b 1750 goto enospc;
0b86a832
CM
1751
1752 if (ins->objectid + num_bytes >
1753 block_group->key.objectid + block_group->key.offset) {
e19caa5f
CM
1754 search_start = block_group->key.objectid +
1755 block_group->key.offset;
1756 goto new_group;
1757 }
0b86a832 1758
1a5bc167 1759 if (test_range_bit(&info->extent_ins, ins->objectid,
db94535d
CM
1760 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1761 search_start = ins->objectid + num_bytes;
1a5bc167
CM
1762 goto new_group;
1763 }
0b86a832 1764
1a5bc167 1765 if (test_range_bit(&info->pinned_extents, ins->objectid,
db94535d
CM
1766 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1767 search_start = ins->objectid + num_bytes;
1a5bc167 1768 goto new_group;
fec577fb 1769 }
0b86a832 1770
db94535d 1771 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
f2654de4
CM
1772 ins->objectid < exclude_start + exclude_nr)) {
1773 search_start = exclude_start + exclude_nr;
1774 goto new_group;
1775 }
0b86a832 1776
6324fbf3 1777 if (!(data & BTRFS_BLOCK_GROUP_DATA)) {
5276aeda 1778 block_group = btrfs_lookup_block_group(info, ins->objectid);
26b8003f
CM
1779 if (block_group)
1780 trans->block_group = block_group;
f2458e1d 1781 }
db94535d 1782 ins->offset = num_bytes;
239b14b3
CM
1783 if (last_ptr) {
1784 *last_ptr = ins->objectid + ins->offset;
1785 if (*last_ptr ==
1786 btrfs_super_total_bytes(&root->fs_info->super_copy)) {
1787 *last_ptr = 0;
1788 }
1789 }
fec577fb 1790 return 0;
be744175
CM
1791
1792new_group:
db94535d 1793 if (search_start + num_bytes >= search_end) {
cf67582b 1794enospc:
be744175 1795 search_start = orig_search_start;
fbdc762b
CM
1796 if (full_scan) {
1797 ret = -ENOSPC;
1798 goto error;
1799 }
6702ed49
CM
1800 if (wrapped) {
1801 if (!full_scan)
1802 total_needed -= empty_size;
fbdc762b 1803 full_scan = 1;
6702ed49 1804 } else
fbdc762b 1805 wrapped = 1;
be744175 1806 }
0ef3e66b 1807 block_group = btrfs_lookup_first_block_group(info, search_start);
fbdc762b 1808 cond_resched();
1a2b2ac7
CM
1809 block_group = btrfs_find_block_group(root, block_group,
1810 search_start, data, 0);
be744175
CM
1811 goto check_failed;
1812
0f70abe2 1813error:
0f70abe2 1814 return ret;
fec577fb 1815}
ec44a35c 1816
fec577fb
CM
1817/*
1818 * finds a free extent and does all the dirty work required for allocation
1819 * returns the key for the extent through ins, and a tree buffer for
1820 * the first block of the extent through buf.
1821 *
1822 * returns 0 if everything worked, non-zero otherwise.
1823 */
4d775673 1824int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
7bb86316 1825 struct btrfs_root *root,
98d20f67
CM
1826 u64 num_bytes, u64 min_alloc_size,
1827 u64 root_objectid, u64 ref_generation,
7bb86316
CM
1828 u64 owner, u64 owner_offset,
1829 u64 empty_size, u64 hint_byte,
ec44a35c 1830 u64 search_end, struct btrfs_key *ins, u64 data)
fec577fb
CM
1831{
1832 int ret;
1833 int pending_ret;
c31f8830
CM
1834 u64 super_used;
1835 u64 root_used;
fbdc762b 1836 u64 search_start = 0;
8790d502 1837 u64 alloc_profile;
47e4bb98 1838 u32 sizes[2];
1261ec42
CM
1839 struct btrfs_fs_info *info = root->fs_info;
1840 struct btrfs_root *extent_root = info->extent_root;
47e4bb98
CM
1841 struct btrfs_extent_item *extent_item;
1842 struct btrfs_extent_ref *ref;
7bb86316 1843 struct btrfs_path *path;
47e4bb98 1844 struct btrfs_key keys[2];
8f662a76 1845
6324fbf3 1846 if (data) {
8790d502
CM
1847 alloc_profile = info->avail_data_alloc_bits &
1848 info->data_alloc_profile;
1849 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
6324fbf3 1850 } else if (root == root->fs_info->chunk_root) {
8790d502
CM
1851 alloc_profile = info->avail_system_alloc_bits &
1852 info->system_alloc_profile;
1853 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
6324fbf3 1854 } else {
8790d502
CM
1855 alloc_profile = info->avail_metadata_alloc_bits &
1856 info->metadata_alloc_profile;
1857 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
6324fbf3 1858 }
98d20f67 1859again:
a061fc8d 1860 data = reduce_alloc_profile(root, data);
0ef3e66b
CM
1861 /*
1862 * the only place that sets empty_size is btrfs_realloc_node, which
1863 * is not called recursively on allocations
1864 */
1865 if (empty_size || root->ref_cows) {
593060d7 1866 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
6324fbf3 1867 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
0ef3e66b
CM
1868 2 * 1024 * 1024,
1869 BTRFS_BLOCK_GROUP_METADATA |
1870 (info->metadata_alloc_profile &
1871 info->avail_metadata_alloc_bits), 0);
6324fbf3
CM
1872 BUG_ON(ret);
1873 }
1874 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
0ef3e66b 1875 num_bytes + 2 * 1024 * 1024, data, 0);
6324fbf3
CM
1876 BUG_ON(ret);
1877 }
0b86a832 1878
db94535d
CM
1879 WARN_ON(num_bytes < root->sectorsize);
1880 ret = find_free_extent(trans, root, num_bytes, empty_size,
1881 search_start, search_end, hint_byte, ins,
26b8003f
CM
1882 trans->alloc_exclude_start,
1883 trans->alloc_exclude_nr, data);
3b951516 1884
98d20f67
CM
1885 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
1886 num_bytes = num_bytes >> 1;
1887 num_bytes = max(num_bytes, min_alloc_size);
0ef3e66b
CM
1888 do_chunk_alloc(trans, root->fs_info->extent_root,
1889 num_bytes, data, 1);
98d20f67
CM
1890 goto again;
1891 }
ec44a35c
CM
1892 if (ret) {
1893 printk("allocation failed flags %Lu\n", data);
1894 }
ccd467d6 1895 BUG_ON(ret);
f2654de4
CM
1896 if (ret)
1897 return ret;
fec577fb 1898
58176a96 1899 /* block accounting for super block */
db94535d
CM
1900 super_used = btrfs_super_bytes_used(&info->super_copy);
1901 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
26b8003f 1902
58176a96 1903 /* block accounting for root item */
db94535d
CM
1904 root_used = btrfs_root_used(&root->root_item);
1905 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
58176a96 1906
f510cfec
CM
1907 clear_extent_dirty(&root->fs_info->free_space_cache,
1908 ins->objectid, ins->objectid + ins->offset - 1,
1909 GFP_NOFS);
1910
26b8003f 1911 if (root == extent_root) {
1a5bc167
CM
1912 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1913 ins->objectid + ins->offset - 1,
1914 EXTENT_LOCKED, GFP_NOFS);
26b8003f
CM
1915 goto update_block;
1916 }
1917
1918 WARN_ON(trans->alloc_exclude_nr);
1919 trans->alloc_exclude_start = ins->objectid;
1920 trans->alloc_exclude_nr = ins->offset;
037e6390 1921
47e4bb98
CM
1922 memcpy(&keys[0], ins, sizeof(*ins));
1923 keys[1].offset = hash_extent_ref(root_objectid, ref_generation,
1924 owner, owner_offset);
1925 keys[1].objectid = ins->objectid;
1926 keys[1].type = BTRFS_EXTENT_REF_KEY;
1927 sizes[0] = sizeof(*extent_item);
1928 sizes[1] = sizeof(*ref);
7bb86316
CM
1929
1930 path = btrfs_alloc_path();
1931 BUG_ON(!path);
47e4bb98
CM
1932
1933 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
1934 sizes, 2);
26b8003f 1935
ccd467d6 1936 BUG_ON(ret);
47e4bb98
CM
1937 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1938 struct btrfs_extent_item);
1939 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
1940 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1941 struct btrfs_extent_ref);
1942
1943 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
1944 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
1945 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
1946 btrfs_set_ref_offset(path->nodes[0], ref, owner_offset);
1947
1948 btrfs_mark_buffer_dirty(path->nodes[0]);
1949
1950 trans->alloc_exclude_start = 0;
1951 trans->alloc_exclude_nr = 0;
7bb86316 1952 btrfs_free_path(path);
e089f05c 1953 finish_current_insert(trans, extent_root);
e20d96d6 1954 pending_ret = del_pending_extents(trans, extent_root);
f510cfec 1955
e37c9e69 1956 if (ret) {
037e6390 1957 return ret;
e37c9e69
CM
1958 }
1959 if (pending_ret) {
037e6390 1960 return pending_ret;
e37c9e69 1961 }
26b8003f
CM
1962
1963update_block:
0b86a832 1964 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
f5947066
CM
1965 if (ret) {
1966 printk("update block group failed for %Lu %Lu\n",
1967 ins->objectid, ins->offset);
1968 BUG();
1969 }
037e6390 1970 return 0;
fec577fb
CM
1971}
1972
1973/*
1974 * helper function to allocate a block for a given tree
1975 * returns the tree buffer or NULL.
1976 */
5f39d397 1977struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
db94535d 1978 struct btrfs_root *root,
7bb86316
CM
1979 u32 blocksize,
1980 u64 root_objectid, u64 hint,
1981 u64 empty_size)
1982{
1983 u64 ref_generation;
1984
1985 if (root->ref_cows)
1986 ref_generation = trans->transid;
1987 else
1988 ref_generation = 0;
1989
1990
1991 return __btrfs_alloc_free_block(trans, root, blocksize, root_objectid,
1992 ref_generation, 0, 0, hint, empty_size);
1993}
1994
1995/*
1996 * helper function to allocate a block for a given tree
1997 * returns the tree buffer or NULL.
1998 */
1999struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
2000 struct btrfs_root *root,
2001 u32 blocksize,
2002 u64 root_objectid,
2003 u64 ref_generation,
2004 u64 first_objectid,
2005 int level,
2006 u64 hint,
5f39d397 2007 u64 empty_size)
fec577fb 2008{
e2fa7227 2009 struct btrfs_key ins;
fec577fb 2010 int ret;
5f39d397 2011 struct extent_buffer *buf;
fec577fb 2012
98d20f67 2013 ret = btrfs_alloc_extent(trans, root, blocksize, blocksize,
7bb86316 2014 root_objectid, ref_generation,
f6dbff55 2015 level, first_objectid, empty_size, hint,
db94535d 2016 (u64)-1, &ins, 0);
fec577fb 2017 if (ret) {
54aa1f4d
CM
2018 BUG_ON(ret > 0);
2019 return ERR_PTR(ret);
fec577fb 2020 }
db94535d 2021 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
54aa1f4d 2022 if (!buf) {
7bb86316
CM
2023 btrfs_free_extent(trans, root, ins.objectid, blocksize,
2024 root->root_key.objectid, ref_generation,
2025 0, 0, 0);
54aa1f4d
CM
2026 return ERR_PTR(-ENOMEM);
2027 }
55c69072
CM
2028 btrfs_set_header_generation(buf, trans->transid);
2029 clean_tree_block(trans, root, buf);
5f39d397 2030 btrfs_set_buffer_uptodate(buf);
55c69072
CM
2031
2032 if (PageDirty(buf->first_page)) {
2033 printk("page %lu dirty\n", buf->first_page->index);
2034 WARN_ON(1);
2035 }
2036
5f39d397
CM
2037 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
2038 buf->start + buf->len - 1, GFP_NOFS);
9afbb0b7
CM
2039 if (!btrfs_test_opt(root, SSD))
2040 btrfs_set_buffer_defrag(buf);
d3c2fdcf 2041 trans->blocks_used++;
fec577fb
CM
2042 return buf;
2043}
a28ec197 2044
98ed5174
CM
2045static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
2046 struct btrfs_root *root,
2047 struct extent_buffer *leaf)
6407bf6d 2048{
7bb86316
CM
2049 u64 leaf_owner;
2050 u64 leaf_generation;
5f39d397 2051 struct btrfs_key key;
6407bf6d
CM
2052 struct btrfs_file_extent_item *fi;
2053 int i;
2054 int nritems;
2055 int ret;
2056
5f39d397
CM
2057 BUG_ON(!btrfs_is_leaf(leaf));
2058 nritems = btrfs_header_nritems(leaf);
7bb86316
CM
2059 leaf_owner = btrfs_header_owner(leaf);
2060 leaf_generation = btrfs_header_generation(leaf);
2061
6407bf6d 2062 for (i = 0; i < nritems; i++) {
db94535d 2063 u64 disk_bytenr;
5f39d397
CM
2064
2065 btrfs_item_key_to_cpu(leaf, &key, i);
2066 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
6407bf6d
CM
2067 continue;
2068 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
5f39d397
CM
2069 if (btrfs_file_extent_type(leaf, fi) ==
2070 BTRFS_FILE_EXTENT_INLINE)
236454df 2071 continue;
6407bf6d
CM
2072 /*
2073 * FIXME make sure to insert a trans record that
2074 * repeats the snapshot del on crash
2075 */
db94535d
CM
2076 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2077 if (disk_bytenr == 0)
3a686375 2078 continue;
db94535d 2079 ret = btrfs_free_extent(trans, root, disk_bytenr,
7bb86316
CM
2080 btrfs_file_extent_disk_num_bytes(leaf, fi),
2081 leaf_owner, leaf_generation,
2082 key.objectid, key.offset, 0);
6407bf6d
CM
2083 BUG_ON(ret);
2084 }
2085 return 0;
2086}
2087
98ed5174 2088static void noinline reada_walk_down(struct btrfs_root *root,
bea495e5
CM
2089 struct extent_buffer *node,
2090 int slot)
e011599b 2091{
db94535d 2092 u64 bytenr;
bea495e5
CM
2093 u64 last = 0;
2094 u32 nritems;
e011599b 2095 u32 refs;
db94535d 2096 u32 blocksize;
bea495e5
CM
2097 int ret;
2098 int i;
2099 int level;
2100 int skipped = 0;
e011599b 2101
5f39d397 2102 nritems = btrfs_header_nritems(node);
db94535d 2103 level = btrfs_header_level(node);
bea495e5
CM
2104 if (level)
2105 return;
2106
2107 for (i = slot; i < nritems && skipped < 32; i++) {
db94535d 2108 bytenr = btrfs_node_blockptr(node, i);
bea495e5
CM
2109 if (last && ((bytenr > last && bytenr - last > 32 * 1024) ||
2110 (last > bytenr && last - bytenr > 32 * 1024))) {
2111 skipped++;
e011599b 2112 continue;
bea495e5
CM
2113 }
2114 blocksize = btrfs_level_size(root, level - 1);
2115 if (i != slot) {
2116 ret = lookup_extent_ref(NULL, root, bytenr,
2117 blocksize, &refs);
2118 BUG_ON(ret);
2119 if (refs != 1) {
2120 skipped++;
2121 continue;
2122 }
2123 }
409eb95d 2124 mutex_unlock(&root->fs_info->fs_mutex);
ca7a79ad
CM
2125 ret = readahead_tree_block(root, bytenr, blocksize,
2126 btrfs_node_ptr_generation(node, i));
bea495e5 2127 last = bytenr + blocksize;
409eb95d
CM
2128 cond_resched();
2129 mutex_lock(&root->fs_info->fs_mutex);
e011599b
CM
2130 if (ret)
2131 break;
2132 }
2133}
2134
9aca1d51
CM
2135/*
2136 * helper function for drop_snapshot, this walks down the tree dropping ref
2137 * counts as it goes.
2138 */
98ed5174
CM
2139static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2140 struct btrfs_root *root,
2141 struct btrfs_path *path, int *level)
20524f02 2142{
7bb86316
CM
2143 u64 root_owner;
2144 u64 root_gen;
2145 u64 bytenr;
ca7a79ad 2146 u64 ptr_gen;
5f39d397
CM
2147 struct extent_buffer *next;
2148 struct extent_buffer *cur;
7bb86316 2149 struct extent_buffer *parent;
db94535d 2150 u32 blocksize;
20524f02
CM
2151 int ret;
2152 u32 refs;
2153
5caf2a00
CM
2154 WARN_ON(*level < 0);
2155 WARN_ON(*level >= BTRFS_MAX_LEVEL);
5f39d397 2156 ret = lookup_extent_ref(trans, root,
db94535d
CM
2157 path->nodes[*level]->start,
2158 path->nodes[*level]->len, &refs);
20524f02
CM
2159 BUG_ON(ret);
2160 if (refs > 1)
2161 goto out;
e011599b 2162
9aca1d51
CM
2163 /*
2164 * walk down to the last node level and free all the leaves
2165 */
6407bf6d 2166 while(*level >= 0) {
5caf2a00
CM
2167 WARN_ON(*level < 0);
2168 WARN_ON(*level >= BTRFS_MAX_LEVEL);
20524f02 2169 cur = path->nodes[*level];
e011599b 2170
5f39d397 2171 if (btrfs_header_level(cur) != *level)
2c90e5d6 2172 WARN_ON(1);
e011599b 2173
7518a238 2174 if (path->slots[*level] >=
5f39d397 2175 btrfs_header_nritems(cur))
20524f02 2176 break;
6407bf6d
CM
2177 if (*level == 0) {
2178 ret = drop_leaf_ref(trans, root, cur);
2179 BUG_ON(ret);
2180 break;
2181 }
db94535d 2182 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
ca7a79ad 2183 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
db94535d
CM
2184 blocksize = btrfs_level_size(root, *level - 1);
2185 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
6407bf6d
CM
2186 BUG_ON(ret);
2187 if (refs != 1) {
7bb86316
CM
2188 parent = path->nodes[*level];
2189 root_owner = btrfs_header_owner(parent);
2190 root_gen = btrfs_header_generation(parent);
20524f02 2191 path->slots[*level]++;
db94535d 2192 ret = btrfs_free_extent(trans, root, bytenr,
7bb86316
CM
2193 blocksize, root_owner,
2194 root_gen, 0, 0, 1);
20524f02
CM
2195 BUG_ON(ret);
2196 continue;
2197 }
db94535d 2198 next = btrfs_find_tree_block(root, bytenr, blocksize);
1259ab75 2199 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
5f39d397 2200 free_extent_buffer(next);
bea495e5 2201 reada_walk_down(root, cur, path->slots[*level]);
8790d502
CM
2202
2203 mutex_unlock(&root->fs_info->fs_mutex);
ca7a79ad
CM
2204 next = read_tree_block(root, bytenr, blocksize,
2205 ptr_gen);
8790d502 2206 mutex_lock(&root->fs_info->fs_mutex);
e9d0b13b 2207
8790d502 2208 /* we've dropped the lock, double check */
db94535d
CM
2209 ret = lookup_extent_ref(trans, root, bytenr,
2210 blocksize, &refs);
e9d0b13b
CM
2211 BUG_ON(ret);
2212 if (refs != 1) {
7bb86316
CM
2213 parent = path->nodes[*level];
2214 root_owner = btrfs_header_owner(parent);
2215 root_gen = btrfs_header_generation(parent);
2216
e9d0b13b 2217 path->slots[*level]++;
5f39d397 2218 free_extent_buffer(next);
7bb86316
CM
2219 ret = btrfs_free_extent(trans, root, bytenr,
2220 blocksize,
2221 root_owner,
2222 root_gen, 0, 0, 1);
e9d0b13b
CM
2223 BUG_ON(ret);
2224 continue;
2225 }
2226 }
5caf2a00 2227 WARN_ON(*level <= 0);
83e15a28 2228 if (path->nodes[*level-1])
5f39d397 2229 free_extent_buffer(path->nodes[*level-1]);
20524f02 2230 path->nodes[*level-1] = next;
5f39d397 2231 *level = btrfs_header_level(next);
20524f02
CM
2232 path->slots[*level] = 0;
2233 }
2234out:
5caf2a00
CM
2235 WARN_ON(*level < 0);
2236 WARN_ON(*level >= BTRFS_MAX_LEVEL);
7bb86316
CM
2237
2238 if (path->nodes[*level] == root->node) {
2239 root_owner = root->root_key.objectid;
2240 parent = path->nodes[*level];
2241 } else {
2242 parent = path->nodes[*level + 1];
2243 root_owner = btrfs_header_owner(parent);
2244 }
2245
2246 root_gen = btrfs_header_generation(parent);
db94535d 2247 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
7bb86316
CM
2248 path->nodes[*level]->len,
2249 root_owner, root_gen, 0, 0, 1);
5f39d397 2250 free_extent_buffer(path->nodes[*level]);
20524f02
CM
2251 path->nodes[*level] = NULL;
2252 *level += 1;
2253 BUG_ON(ret);
2254 return 0;
2255}
2256
9aca1d51
CM
2257/*
2258 * helper for dropping snapshots. This walks back up the tree in the path
2259 * to find the first node higher up where we haven't yet gone through
2260 * all the slots
2261 */
98ed5174
CM
2262static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
2263 struct btrfs_root *root,
2264 struct btrfs_path *path, int *level)
20524f02 2265{
7bb86316
CM
2266 u64 root_owner;
2267 u64 root_gen;
2268 struct btrfs_root_item *root_item = &root->root_item;
20524f02
CM
2269 int i;
2270 int slot;
2271 int ret;
9f3a7427 2272
234b63a0 2273 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
20524f02 2274 slot = path->slots[i];
5f39d397
CM
2275 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
2276 struct extent_buffer *node;
2277 struct btrfs_disk_key disk_key;
2278 node = path->nodes[i];
20524f02
CM
2279 path->slots[i]++;
2280 *level = i;
9f3a7427 2281 WARN_ON(*level == 0);
5f39d397 2282 btrfs_node_key(node, &disk_key, path->slots[i]);
9f3a7427 2283 memcpy(&root_item->drop_progress,
5f39d397 2284 &disk_key, sizeof(disk_key));
9f3a7427 2285 root_item->drop_level = i;
20524f02
CM
2286 return 0;
2287 } else {
7bb86316
CM
2288 if (path->nodes[*level] == root->node) {
2289 root_owner = root->root_key.objectid;
2290 root_gen =
2291 btrfs_header_generation(path->nodes[*level]);
2292 } else {
2293 struct extent_buffer *node;
2294 node = path->nodes[*level + 1];
2295 root_owner = btrfs_header_owner(node);
2296 root_gen = btrfs_header_generation(node);
2297 }
e089f05c 2298 ret = btrfs_free_extent(trans, root,
db94535d 2299 path->nodes[*level]->start,
7bb86316
CM
2300 path->nodes[*level]->len,
2301 root_owner, root_gen, 0, 0, 1);
6407bf6d 2302 BUG_ON(ret);
5f39d397 2303 free_extent_buffer(path->nodes[*level]);
83e15a28 2304 path->nodes[*level] = NULL;
20524f02 2305 *level = i + 1;
20524f02
CM
2306 }
2307 }
2308 return 1;
2309}
2310
9aca1d51
CM
2311/*
2312 * drop the reference count on the tree rooted at 'snap'. This traverses
2313 * the tree freeing any blocks that have a ref count of zero after being
2314 * decremented.
2315 */
e089f05c 2316int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
9f3a7427 2317 *root)
20524f02 2318{
3768f368 2319 int ret = 0;
9aca1d51 2320 int wret;
20524f02 2321 int level;
5caf2a00 2322 struct btrfs_path *path;
20524f02
CM
2323 int i;
2324 int orig_level;
9f3a7427 2325 struct btrfs_root_item *root_item = &root->root_item;
20524f02 2326
5caf2a00
CM
2327 path = btrfs_alloc_path();
2328 BUG_ON(!path);
20524f02 2329
5f39d397 2330 level = btrfs_header_level(root->node);
20524f02 2331 orig_level = level;
9f3a7427
CM
2332 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2333 path->nodes[level] = root->node;
f510cfec 2334 extent_buffer_get(root->node);
9f3a7427
CM
2335 path->slots[level] = 0;
2336 } else {
2337 struct btrfs_key key;
5f39d397
CM
2338 struct btrfs_disk_key found_key;
2339 struct extent_buffer *node;
6702ed49 2340
9f3a7427 2341 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
6702ed49
CM
2342 level = root_item->drop_level;
2343 path->lowest_level = level;
9f3a7427 2344 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6702ed49 2345 if (wret < 0) {
9f3a7427
CM
2346 ret = wret;
2347 goto out;
2348 }
5f39d397
CM
2349 node = path->nodes[level];
2350 btrfs_node_key(node, &found_key, path->slots[level]);
2351 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2352 sizeof(found_key)));
9f3a7427 2353 }
20524f02 2354 while(1) {
5caf2a00 2355 wret = walk_down_tree(trans, root, path, &level);
9aca1d51 2356 if (wret > 0)
20524f02 2357 break;
9aca1d51
CM
2358 if (wret < 0)
2359 ret = wret;
2360
5caf2a00 2361 wret = walk_up_tree(trans, root, path, &level);
9aca1d51 2362 if (wret > 0)
20524f02 2363 break;
9aca1d51
CM
2364 if (wret < 0)
2365 ret = wret;
409eb95d 2366 ret = -EAGAIN;
409eb95d 2367 break;
20524f02 2368 }
83e15a28 2369 for (i = 0; i <= orig_level; i++) {
5caf2a00 2370 if (path->nodes[i]) {
5f39d397 2371 free_extent_buffer(path->nodes[i]);
0f82731f 2372 path->nodes[i] = NULL;
83e15a28 2373 }
20524f02 2374 }
9f3a7427 2375out:
5caf2a00 2376 btrfs_free_path(path);
9aca1d51 2377 return ret;
20524f02 2378}
9078a3e1 2379
96b5179d 2380int btrfs_free_block_groups(struct btrfs_fs_info *info)
9078a3e1 2381{
96b5179d
CM
2382 u64 start;
2383 u64 end;
b97f9203 2384 u64 ptr;
9078a3e1 2385 int ret;
9078a3e1 2386 while(1) {
96b5179d
CM
2387 ret = find_first_extent_bit(&info->block_group_cache, 0,
2388 &start, &end, (unsigned int)-1);
2389 if (ret)
9078a3e1 2390 break;
b97f9203
Y
2391 ret = get_state_private(&info->block_group_cache, start, &ptr);
2392 if (!ret)
2393 kfree((void *)(unsigned long)ptr);
96b5179d
CM
2394 clear_extent_bits(&info->block_group_cache, start,
2395 end, (unsigned int)-1, GFP_NOFS);
9078a3e1 2396 }
e37c9e69 2397 while(1) {
f510cfec
CM
2398 ret = find_first_extent_bit(&info->free_space_cache, 0,
2399 &start, &end, EXTENT_DIRTY);
2400 if (ret)
e37c9e69 2401 break;
f510cfec
CM
2402 clear_extent_dirty(&info->free_space_cache, start,
2403 end, GFP_NOFS);
e37c9e69 2404 }
be744175
CM
2405 return 0;
2406}
2407
8e7bf94f
CM
2408static unsigned long calc_ra(unsigned long start, unsigned long last,
2409 unsigned long nr)
2410{
2411 return min(last, start + nr - 1);
2412}
2413
98ed5174
CM
2414static int noinline relocate_inode_pages(struct inode *inode, u64 start,
2415 u64 len)
edbd8d4e
CM
2416{
2417 u64 page_start;
2418 u64 page_end;
edbd8d4e 2419 unsigned long last_index;
edbd8d4e
CM
2420 unsigned long i;
2421 struct page *page;
d1310b2e 2422 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4313b399 2423 struct file_ra_state *ra;
8e7bf94f
CM
2424 unsigned long total_read = 0;
2425 unsigned long ra_pages;
a061fc8d 2426 struct btrfs_trans_handle *trans;
4313b399
CM
2427
2428 ra = kzalloc(sizeof(*ra), GFP_NOFS);
edbd8d4e
CM
2429
2430 mutex_lock(&inode->i_mutex);
4313b399 2431 i = start >> PAGE_CACHE_SHIFT;
edbd8d4e
CM
2432 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
2433
8e7bf94f
CM
2434 ra_pages = BTRFS_I(inode)->root->fs_info->bdi.ra_pages;
2435
4313b399 2436 file_ra_state_init(ra, inode->i_mapping);
edbd8d4e 2437
4313b399 2438 for (; i <= last_index; i++) {
8e7bf94f
CM
2439 if (total_read % ra_pages == 0) {
2440 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
2441 calc_ra(i, last_index, ra_pages));
2442 }
2443 total_read++;
a061fc8d
CM
2444 if (((u64)i << PAGE_CACHE_SHIFT) > inode->i_size)
2445 goto truncate_racing;
2446
edbd8d4e 2447 page = grab_cache_page(inode->i_mapping, i);
a061fc8d 2448 if (!page) {
edbd8d4e 2449 goto out_unlock;
a061fc8d 2450 }
edbd8d4e
CM
2451 if (!PageUptodate(page)) {
2452 btrfs_readpage(NULL, page);
2453 lock_page(page);
2454 if (!PageUptodate(page)) {
2455 unlock_page(page);
2456 page_cache_release(page);
2457 goto out_unlock;
2458 }
2459 }
ec44a35c
CM
2460#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2461 ClearPageDirty(page);
2462#else
2463 cancel_dirty_page(page, PAGE_CACHE_SIZE);
2464#endif
2465 wait_on_page_writeback(page);
2466 set_page_extent_mapped(page);
edbd8d4e
CM
2467 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2468 page_end = page_start + PAGE_CACHE_SIZE - 1;
2469
d1310b2e 2470 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
edbd8d4e 2471
d1310b2e 2472 set_extent_delalloc(io_tree, page_start,
edbd8d4e 2473 page_end, GFP_NOFS);
a061fc8d 2474 set_page_dirty(page);
edbd8d4e 2475
d1310b2e 2476 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
edbd8d4e
CM
2477 unlock_page(page);
2478 page_cache_release(page);
2479 }
a061fc8d
CM
2480 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
2481 total_read);
edbd8d4e
CM
2482
2483out_unlock:
ec44a35c 2484 kfree(ra);
a061fc8d
CM
2485 trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1);
2486 if (trans) {
2487 btrfs_add_ordered_inode(inode);
2488 btrfs_end_transaction(trans, BTRFS_I(inode)->root);
2489 mark_inode_dirty(inode);
2490 }
edbd8d4e
CM
2491 mutex_unlock(&inode->i_mutex);
2492 return 0;
a061fc8d
CM
2493
2494truncate_racing:
2495 vmtruncate(inode, inode->i_size);
2496 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
2497 total_read);
2498 goto out_unlock;
edbd8d4e
CM
2499}
2500
bf4ef679
CM
2501/*
2502 * The back references tell us which tree holds a ref on a block,
2503 * but it is possible for the tree root field in the reference to
2504 * reflect the original root before a snapshot was made. In this
2505 * case we should search through all the children of a given root
2506 * to find potential holders of references on a block.
2507 *
2508 * Instead, we do something a little less fancy and just search
2509 * all the roots for a given key/block combination.
2510 */
2511static int find_root_for_ref(struct btrfs_root *root,
2512 struct btrfs_path *path,
2513 struct btrfs_key *key0,
2514 int level,
2515 int file_key,
2516 struct btrfs_root **found_root,
2517 u64 bytenr)
2518{
2519 struct btrfs_key root_location;
2520 struct btrfs_root *cur_root = *found_root;
2521 struct btrfs_file_extent_item *file_extent;
2522 u64 root_search_start = BTRFS_FS_TREE_OBJECTID;
2523 u64 found_bytenr;
2524 int ret;
2525 int i;
2526
2527 root_location.offset = (u64)-1;
2528 root_location.type = BTRFS_ROOT_ITEM_KEY;
2529 path->lowest_level = level;
2530 path->reada = 0;
2531 while(1) {
2532 ret = btrfs_search_slot(NULL, cur_root, key0, path, 0, 0);
2533 found_bytenr = 0;
2534 if (ret == 0 && file_key) {
2535 struct extent_buffer *leaf = path->nodes[0];
2536 file_extent = btrfs_item_ptr(leaf, path->slots[0],
2537 struct btrfs_file_extent_item);
2538 if (btrfs_file_extent_type(leaf, file_extent) ==
2539 BTRFS_FILE_EXTENT_REG) {
2540 found_bytenr =
2541 btrfs_file_extent_disk_bytenr(leaf,
2542 file_extent);
2543 }
323da79c 2544 } else if (!file_key) {
bf4ef679
CM
2545 if (path->nodes[level])
2546 found_bytenr = path->nodes[level]->start;
2547 }
2548
2549 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2550 if (!path->nodes[i])
2551 break;
2552 free_extent_buffer(path->nodes[i]);
2553 path->nodes[i] = NULL;
2554 }
2555 btrfs_release_path(cur_root, path);
2556
2557 if (found_bytenr == bytenr) {
2558 *found_root = cur_root;
2559 ret = 0;
2560 goto out;
2561 }
2562 ret = btrfs_search_root(root->fs_info->tree_root,
2563 root_search_start, &root_search_start);
2564 if (ret)
2565 break;
2566
2567 root_location.objectid = root_search_start;
2568 cur_root = btrfs_read_fs_root_no_name(root->fs_info,
2569 &root_location);
2570 if (!cur_root) {
2571 ret = 1;
2572 break;
2573 }
2574 }
2575out:
2576 path->lowest_level = 0;
2577 return ret;
2578}
2579
4313b399
CM
2580/*
2581 * note, this releases the path
2582 */
98ed5174 2583static int noinline relocate_one_reference(struct btrfs_root *extent_root,
edbd8d4e 2584 struct btrfs_path *path,
0ef3e66b
CM
2585 struct btrfs_key *extent_key,
2586 u64 *last_file_objectid,
2587 u64 *last_file_offset,
2588 u64 *last_file_root,
2589 u64 last_extent)
edbd8d4e
CM
2590{
2591 struct inode *inode;
2592 struct btrfs_root *found_root;
bf4ef679
CM
2593 struct btrfs_key root_location;
2594 struct btrfs_key found_key;
4313b399
CM
2595 struct btrfs_extent_ref *ref;
2596 u64 ref_root;
2597 u64 ref_gen;
2598 u64 ref_objectid;
2599 u64 ref_offset;
edbd8d4e 2600 int ret;
bf4ef679 2601 int level;
edbd8d4e 2602
4313b399
CM
2603 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
2604 struct btrfs_extent_ref);
2605 ref_root = btrfs_ref_root(path->nodes[0], ref);
2606 ref_gen = btrfs_ref_generation(path->nodes[0], ref);
2607 ref_objectid = btrfs_ref_objectid(path->nodes[0], ref);
2608 ref_offset = btrfs_ref_offset(path->nodes[0], ref);
2609 btrfs_release_path(extent_root, path);
2610
bf4ef679 2611 root_location.objectid = ref_root;
edbd8d4e 2612 if (ref_gen == 0)
bf4ef679 2613 root_location.offset = 0;
edbd8d4e 2614 else
bf4ef679
CM
2615 root_location.offset = (u64)-1;
2616 root_location.type = BTRFS_ROOT_ITEM_KEY;
edbd8d4e
CM
2617
2618 found_root = btrfs_read_fs_root_no_name(extent_root->fs_info,
bf4ef679 2619 &root_location);
edbd8d4e
CM
2620 BUG_ON(!found_root);
2621
2622 if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
bf4ef679
CM
2623 found_key.objectid = ref_objectid;
2624 found_key.type = BTRFS_EXTENT_DATA_KEY;
2625 found_key.offset = ref_offset;
2626 level = 0;
2627
0ef3e66b
CM
2628 if (last_extent == extent_key->objectid &&
2629 *last_file_objectid == ref_objectid &&
2630 *last_file_offset == ref_offset &&
2631 *last_file_root == ref_root)
2632 goto out;
2633
bf4ef679
CM
2634 ret = find_root_for_ref(extent_root, path, &found_key,
2635 level, 1, &found_root,
2636 extent_key->objectid);
2637
2638 if (ret)
2639 goto out;
2640
0ef3e66b
CM
2641 if (last_extent == extent_key->objectid &&
2642 *last_file_objectid == ref_objectid &&
2643 *last_file_offset == ref_offset &&
2644 *last_file_root == ref_root)
2645 goto out;
2646
edbd8d4e
CM
2647 mutex_unlock(&extent_root->fs_info->fs_mutex);
2648 inode = btrfs_iget_locked(extent_root->fs_info->sb,
2649 ref_objectid, found_root);
2650 if (inode->i_state & I_NEW) {
2651 /* the inode and parent dir are two different roots */
2652 BTRFS_I(inode)->root = found_root;
2653 BTRFS_I(inode)->location.objectid = ref_objectid;
2654 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
2655 BTRFS_I(inode)->location.offset = 0;
2656 btrfs_read_locked_inode(inode);
2657 unlock_new_inode(inode);
2658
2659 }
2660 /* this can happen if the reference is not against
2661 * the latest version of the tree root
2662 */
2663 if (is_bad_inode(inode)) {
2664 mutex_lock(&extent_root->fs_info->fs_mutex);
2665 goto out;
2666 }
0ef3e66b
CM
2667 *last_file_objectid = inode->i_ino;
2668 *last_file_root = found_root->root_key.objectid;
2669 *last_file_offset = ref_offset;
2670
edbd8d4e 2671 relocate_inode_pages(inode, ref_offset, extent_key->offset);
edbd8d4e
CM
2672 iput(inode);
2673 mutex_lock(&extent_root->fs_info->fs_mutex);
2674 } else {
2675 struct btrfs_trans_handle *trans;
edbd8d4e 2676 struct extent_buffer *eb;
edbd8d4e
CM
2677 int i;
2678
edbd8d4e 2679 eb = read_tree_block(found_root, extent_key->objectid,
ca7a79ad 2680 extent_key->offset, 0);
edbd8d4e
CM
2681 level = btrfs_header_level(eb);
2682
2683 if (level == 0)
2684 btrfs_item_key_to_cpu(eb, &found_key, 0);
2685 else
2686 btrfs_node_key_to_cpu(eb, &found_key, 0);
2687
2688 free_extent_buffer(eb);
2689
bf4ef679
CM
2690 ret = find_root_for_ref(extent_root, path, &found_key,
2691 level, 0, &found_root,
2692 extent_key->objectid);
2693
2694 if (ret)
2695 goto out;
2696
2697 trans = btrfs_start_transaction(found_root, 1);
2698
edbd8d4e 2699 path->lowest_level = level;
8f662a76 2700 path->reada = 2;
edbd8d4e
CM
2701 ret = btrfs_search_slot(trans, found_root, &found_key, path,
2702 0, 1);
2703 path->lowest_level = 0;
2704 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2705 if (!path->nodes[i])
2706 break;
2707 free_extent_buffer(path->nodes[i]);
2708 path->nodes[i] = NULL;
2709 }
2710 btrfs_release_path(found_root, path);
0ef3e66b
CM
2711 if (found_root == found_root->fs_info->extent_root)
2712 btrfs_extent_post_op(trans, found_root);
edbd8d4e
CM
2713 btrfs_end_transaction(trans, found_root);
2714 }
2715
2716out:
2717 return 0;
2718}
2719
a061fc8d
CM
2720static int noinline del_extent_zero(struct btrfs_root *extent_root,
2721 struct btrfs_path *path,
2722 struct btrfs_key *extent_key)
2723{
2724 int ret;
2725 struct btrfs_trans_handle *trans;
2726
2727 trans = btrfs_start_transaction(extent_root, 1);
2728 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
2729 if (ret > 0) {
2730 ret = -EIO;
2731 goto out;
2732 }
2733 if (ret < 0)
2734 goto out;
2735 ret = btrfs_del_item(trans, extent_root, path);
2736out:
2737 btrfs_end_transaction(trans, extent_root);
2738 return ret;
2739}
2740
98ed5174
CM
2741static int noinline relocate_one_extent(struct btrfs_root *extent_root,
2742 struct btrfs_path *path,
2743 struct btrfs_key *extent_key)
edbd8d4e
CM
2744{
2745 struct btrfs_key key;
2746 struct btrfs_key found_key;
edbd8d4e 2747 struct extent_buffer *leaf;
0ef3e66b
CM
2748 u64 last_file_objectid = 0;
2749 u64 last_file_root = 0;
2750 u64 last_file_offset = (u64)-1;
2751 u64 last_extent = 0;
edbd8d4e
CM
2752 u32 nritems;
2753 u32 item_size;
2754 int ret = 0;
2755
a061fc8d
CM
2756 if (extent_key->objectid == 0) {
2757 ret = del_extent_zero(extent_root, path, extent_key);
2758 goto out;
2759 }
edbd8d4e
CM
2760 key.objectid = extent_key->objectid;
2761 key.type = BTRFS_EXTENT_REF_KEY;
2762 key.offset = 0;
2763
2764 while(1) {
2765 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2766
edbd8d4e
CM
2767 if (ret < 0)
2768 goto out;
2769
2770 ret = 0;
2771 leaf = path->nodes[0];
2772 nritems = btrfs_header_nritems(leaf);
a061fc8d
CM
2773 if (path->slots[0] == nritems) {
2774 ret = btrfs_next_leaf(extent_root, path);
2775 if (ret > 0) {
2776 ret = 0;
2777 goto out;
2778 }
2779 if (ret < 0)
2780 goto out;
bf4ef679 2781 leaf = path->nodes[0];
a061fc8d 2782 }
edbd8d4e
CM
2783
2784 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
a061fc8d 2785 if (found_key.objectid != extent_key->objectid) {
edbd8d4e 2786 break;
a061fc8d 2787 }
edbd8d4e 2788
a061fc8d 2789 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
edbd8d4e 2790 break;
a061fc8d 2791 }
edbd8d4e
CM
2792
2793 key.offset = found_key.offset + 1;
2794 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2795
0ef3e66b
CM
2796 ret = relocate_one_reference(extent_root, path, extent_key,
2797 &last_file_objectid,
2798 &last_file_offset,
2799 &last_file_root, last_extent);
edbd8d4e
CM
2800 if (ret)
2801 goto out;
0ef3e66b 2802 last_extent = extent_key->objectid;
edbd8d4e
CM
2803 }
2804 ret = 0;
2805out:
2806 btrfs_release_path(extent_root, path);
2807 return ret;
2808}
2809
ec44a35c
CM
2810static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
2811{
2812 u64 num_devices;
2813 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
2814 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
2815
a061fc8d 2816 num_devices = root->fs_info->fs_devices->num_devices;
ec44a35c
CM
2817 if (num_devices == 1) {
2818 stripped |= BTRFS_BLOCK_GROUP_DUP;
2819 stripped = flags & ~stripped;
2820
2821 /* turn raid0 into single device chunks */
2822 if (flags & BTRFS_BLOCK_GROUP_RAID0)
2823 return stripped;
2824
2825 /* turn mirroring into duplication */
2826 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
2827 BTRFS_BLOCK_GROUP_RAID10))
2828 return stripped | BTRFS_BLOCK_GROUP_DUP;
2829 return flags;
2830 } else {
2831 /* they already had raid on here, just return */
ec44a35c
CM
2832 if (flags & stripped)
2833 return flags;
2834
2835 stripped |= BTRFS_BLOCK_GROUP_DUP;
2836 stripped = flags & ~stripped;
2837
2838 /* switch duplicated blocks with raid1 */
2839 if (flags & BTRFS_BLOCK_GROUP_DUP)
2840 return stripped | BTRFS_BLOCK_GROUP_RAID1;
2841
2842 /* turn single device chunks into raid0 */
2843 return stripped | BTRFS_BLOCK_GROUP_RAID0;
2844 }
2845 return flags;
2846}
2847
0ef3e66b
CM
2848int __alloc_chunk_for_shrink(struct btrfs_root *root,
2849 struct btrfs_block_group_cache *shrink_block_group,
2850 int force)
2851{
2852 struct btrfs_trans_handle *trans;
2853 u64 new_alloc_flags;
2854 u64 calc;
2855
2856 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
2857
2858 trans = btrfs_start_transaction(root, 1);
2859 new_alloc_flags = update_block_group_flags(root,
2860 shrink_block_group->flags);
2861 if (new_alloc_flags != shrink_block_group->flags) {
2862 calc =
2863 btrfs_block_group_used(&shrink_block_group->item);
2864 } else {
2865 calc = shrink_block_group->key.offset;
2866 }
2867 do_chunk_alloc(trans, root->fs_info->extent_root,
2868 calc + 2 * 1024 * 1024, new_alloc_flags, force);
2869 btrfs_end_transaction(trans, root);
2870 }
2871 return 0;
2872}
2873
8f18cf13 2874int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 shrink_start)
edbd8d4e
CM
2875{
2876 struct btrfs_trans_handle *trans;
2877 struct btrfs_root *tree_root = root->fs_info->tree_root;
2878 struct btrfs_path *path;
2879 u64 cur_byte;
2880 u64 total_found;
8f18cf13
CM
2881 u64 shrink_last_byte;
2882 struct btrfs_block_group_cache *shrink_block_group;
edbd8d4e 2883 struct btrfs_fs_info *info = root->fs_info;
edbd8d4e 2884 struct btrfs_key key;
73e48b27 2885 struct btrfs_key found_key;
edbd8d4e
CM
2886 struct extent_buffer *leaf;
2887 u32 nritems;
2888 int ret;
a061fc8d 2889 int progress;
edbd8d4e 2890
8f18cf13
CM
2891 shrink_block_group = btrfs_lookup_block_group(root->fs_info,
2892 shrink_start);
2893 BUG_ON(!shrink_block_group);
2894
0ef3e66b
CM
2895 shrink_last_byte = shrink_block_group->key.objectid +
2896 shrink_block_group->key.offset;
8f18cf13
CM
2897
2898 shrink_block_group->space_info->total_bytes -=
2899 shrink_block_group->key.offset;
edbd8d4e
CM
2900 path = btrfs_alloc_path();
2901 root = root->fs_info->extent_root;
8f662a76 2902 path->reada = 2;
edbd8d4e 2903
323da79c
CM
2904 printk("btrfs relocating block group %llu flags %llu\n",
2905 (unsigned long long)shrink_start,
2906 (unsigned long long)shrink_block_group->flags);
2907
0ef3e66b
CM
2908 __alloc_chunk_for_shrink(root, shrink_block_group, 1);
2909
edbd8d4e 2910again:
323da79c 2911
8f18cf13
CM
2912 shrink_block_group->ro = 1;
2913
edbd8d4e 2914 total_found = 0;
a061fc8d 2915 progress = 0;
8f18cf13 2916 key.objectid = shrink_start;
edbd8d4e
CM
2917 key.offset = 0;
2918 key.type = 0;
73e48b27 2919 cur_byte = key.objectid;
4313b399 2920
73e48b27
Y
2921 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2922 if (ret < 0)
2923 goto out;
2924
0b86a832 2925 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
73e48b27
Y
2926 if (ret < 0)
2927 goto out;
8f18cf13 2928
73e48b27
Y
2929 if (ret == 0) {
2930 leaf = path->nodes[0];
2931 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
8f18cf13
CM
2932 if (found_key.objectid + found_key.offset > shrink_start &&
2933 found_key.objectid < shrink_last_byte) {
73e48b27
Y
2934 cur_byte = found_key.objectid;
2935 key.objectid = cur_byte;
2936 }
2937 }
2938 btrfs_release_path(root, path);
2939
2940 while(1) {
edbd8d4e
CM
2941 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2942 if (ret < 0)
2943 goto out;
73e48b27 2944
edbd8d4e 2945 leaf = path->nodes[0];
73e48b27
Y
2946 nritems = btrfs_header_nritems(leaf);
2947next:
2948 if (path->slots[0] >= nritems) {
2949 ret = btrfs_next_leaf(root, path);
2950 if (ret < 0)
2951 goto out;
2952 if (ret == 1) {
2953 ret = 0;
2954 break;
edbd8d4e 2955 }
73e48b27
Y
2956 leaf = path->nodes[0];
2957 nritems = btrfs_header_nritems(leaf);
edbd8d4e 2958 }
73e48b27
Y
2959
2960 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
725c8463 2961
8f18cf13
CM
2962 if (found_key.objectid >= shrink_last_byte)
2963 break;
2964
725c8463
CM
2965 if (progress && need_resched()) {
2966 memcpy(&key, &found_key, sizeof(key));
2967 mutex_unlock(&root->fs_info->fs_mutex);
2968 cond_resched();
2969 mutex_lock(&root->fs_info->fs_mutex);
2970 btrfs_release_path(root, path);
2971 btrfs_search_slot(NULL, root, &key, path, 0, 0);
2972 progress = 0;
2973 goto next;
2974 }
2975 progress = 1;
2976
73e48b27
Y
2977 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY ||
2978 found_key.objectid + found_key.offset <= cur_byte) {
0ef3e66b
CM
2979 memcpy(&key, &found_key, sizeof(key));
2980 key.offset++;
edbd8d4e 2981 path->slots[0]++;
edbd8d4e
CM
2982 goto next;
2983 }
73e48b27 2984
edbd8d4e
CM
2985 total_found++;
2986 cur_byte = found_key.objectid + found_key.offset;
2987 key.objectid = cur_byte;
2988 btrfs_release_path(root, path);
2989 ret = relocate_one_extent(root, path, &found_key);
0ef3e66b 2990 __alloc_chunk_for_shrink(root, shrink_block_group, 0);
edbd8d4e
CM
2991 }
2992
2993 btrfs_release_path(root, path);
2994
2995 if (total_found > 0) {
323da79c
CM
2996 printk("btrfs relocate found %llu last extent was %llu\n",
2997 (unsigned long long)total_found,
2998 (unsigned long long)found_key.objectid);
edbd8d4e
CM
2999 trans = btrfs_start_transaction(tree_root, 1);
3000 btrfs_commit_transaction(trans, tree_root);
3001
3002 mutex_unlock(&root->fs_info->fs_mutex);
3003 btrfs_clean_old_snapshots(tree_root);
3004 mutex_lock(&root->fs_info->fs_mutex);
3005
3006 trans = btrfs_start_transaction(tree_root, 1);
3007 btrfs_commit_transaction(trans, tree_root);
3008 goto again;
3009 }
3010
8f18cf13
CM
3011 /*
3012 * we've freed all the extents, now remove the block
3013 * group item from the tree
3014 */
edbd8d4e 3015 trans = btrfs_start_transaction(root, 1);
8f18cf13 3016 memcpy(&key, &shrink_block_group->key, sizeof(key));
1372f8e6 3017
8f18cf13
CM
3018 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3019 if (ret > 0)
3020 ret = -EIO;
3021 if (ret < 0)
3022 goto out;
73e48b27 3023
0ef3e66b
CM
3024 clear_extent_bits(&info->block_group_cache, key.objectid,
3025 key.objectid + key.offset - 1,
8f18cf13 3026 (unsigned int)-1, GFP_NOFS);
edbd8d4e 3027
0ef3e66b
CM
3028
3029 clear_extent_bits(&info->free_space_cache,
3030 key.objectid, key.objectid + key.offset - 1,
3031 (unsigned int)-1, GFP_NOFS);
3032
3033 memset(shrink_block_group, 0, sizeof(*shrink_block_group));
3034 kfree(shrink_block_group);
3035
8f18cf13 3036 btrfs_del_item(trans, root, path);
edbd8d4e 3037 btrfs_commit_transaction(trans, root);
0ef3e66b
CM
3038
3039 /* the code to unpin extents might set a few bits in the free
3040 * space cache for this range again
3041 */
3042 clear_extent_bits(&info->free_space_cache,
3043 key.objectid, key.objectid + key.offset - 1,
3044 (unsigned int)-1, GFP_NOFS);
edbd8d4e
CM
3045out:
3046 btrfs_free_path(path);
3047 return ret;
3048}
3049
0b86a832
CM
3050int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
3051 struct btrfs_key *key)
3052{
3053 int ret;
3054 struct btrfs_key found_key;
3055 struct extent_buffer *leaf;
3056 int slot;
edbd8d4e 3057
0b86a832
CM
3058 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
3059 if (ret < 0)
3060 return ret;
3061 while(1) {
3062 slot = path->slots[0];
edbd8d4e 3063 leaf = path->nodes[0];
0b86a832
CM
3064 if (slot >= btrfs_header_nritems(leaf)) {
3065 ret = btrfs_next_leaf(root, path);
3066 if (ret == 0)
3067 continue;
3068 if (ret < 0)
3069 goto error;
3070 break;
edbd8d4e 3071 }
0b86a832 3072 btrfs_item_key_to_cpu(leaf, &found_key, slot);
edbd8d4e 3073
0b86a832
CM
3074 if (found_key.objectid >= key->objectid &&
3075 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
3076 return 0;
3077 path->slots[0]++;
edbd8d4e 3078 }
0b86a832
CM
3079 ret = -ENOENT;
3080error:
3081 return ret;
edbd8d4e
CM
3082}
3083
9078a3e1
CM
3084int btrfs_read_block_groups(struct btrfs_root *root)
3085{
3086 struct btrfs_path *path;
3087 int ret;
96b5179d 3088 int bit;
9078a3e1 3089 struct btrfs_block_group_cache *cache;
be744175 3090 struct btrfs_fs_info *info = root->fs_info;
6324fbf3 3091 struct btrfs_space_info *space_info;
d1310b2e 3092 struct extent_io_tree *block_group_cache;
9078a3e1
CM
3093 struct btrfs_key key;
3094 struct btrfs_key found_key;
5f39d397 3095 struct extent_buffer *leaf;
96b5179d
CM
3096
3097 block_group_cache = &info->block_group_cache;
be744175 3098 root = info->extent_root;
9078a3e1 3099 key.objectid = 0;
0b86a832 3100 key.offset = 0;
9078a3e1 3101 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
9078a3e1
CM
3102 path = btrfs_alloc_path();
3103 if (!path)
3104 return -ENOMEM;
3105
3106 while(1) {
0b86a832
CM
3107 ret = find_first_block_group(root, path, &key);
3108 if (ret > 0) {
3109 ret = 0;
3110 goto error;
9078a3e1 3111 }
0b86a832
CM
3112 if (ret != 0)
3113 goto error;
3114
5f39d397
CM
3115 leaf = path->nodes[0];
3116 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
8f18cf13 3117 cache = kzalloc(sizeof(*cache), GFP_NOFS);
9078a3e1 3118 if (!cache) {
0b86a832 3119 ret = -ENOMEM;
9078a3e1
CM
3120 break;
3121 }
3e1ad54f 3122
5f39d397
CM
3123 read_extent_buffer(leaf, &cache->item,
3124 btrfs_item_ptr_offset(leaf, path->slots[0]),
3125 sizeof(cache->item));
9078a3e1 3126 memcpy(&cache->key, &found_key, sizeof(found_key));
0b86a832 3127
9078a3e1
CM
3128 key.objectid = found_key.objectid + found_key.offset;
3129 btrfs_release_path(root, path);
0b86a832
CM
3130 cache->flags = btrfs_block_group_flags(&cache->item);
3131 bit = 0;
3132 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
96b5179d 3133 bit = BLOCK_GROUP_DATA;
0b86a832
CM
3134 } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
3135 bit = BLOCK_GROUP_SYSTEM;
3136 } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) {
96b5179d 3137 bit = BLOCK_GROUP_METADATA;
31f3c99b 3138 }
8790d502 3139 set_avail_alloc_bits(info, cache->flags);
96b5179d 3140
6324fbf3
CM
3141 ret = update_space_info(info, cache->flags, found_key.offset,
3142 btrfs_block_group_used(&cache->item),
3143 &space_info);
3144 BUG_ON(ret);
3145 cache->space_info = space_info;
3146
96b5179d
CM
3147 /* use EXTENT_LOCKED to prevent merging */
3148 set_extent_bits(block_group_cache, found_key.objectid,
3149 found_key.objectid + found_key.offset - 1,
3150 bit | EXTENT_LOCKED, GFP_NOFS);
3151 set_state_private(block_group_cache, found_key.objectid,
ae2f5411 3152 (unsigned long)cache);
96b5179d 3153
9078a3e1 3154 if (key.objectid >=
db94535d 3155 btrfs_super_total_bytes(&info->super_copy))
9078a3e1
CM
3156 break;
3157 }
0b86a832
CM
3158 ret = 0;
3159error:
9078a3e1 3160 btrfs_free_path(path);
0b86a832 3161 return ret;
9078a3e1 3162}
6324fbf3
CM
3163
3164int btrfs_make_block_group(struct btrfs_trans_handle *trans,
3165 struct btrfs_root *root, u64 bytes_used,
e17cade2 3166 u64 type, u64 chunk_objectid, u64 chunk_offset,
6324fbf3
CM
3167 u64 size)
3168{
3169 int ret;
3170 int bit = 0;
3171 struct btrfs_root *extent_root;
3172 struct btrfs_block_group_cache *cache;
3173 struct extent_io_tree *block_group_cache;
3174
3175 extent_root = root->fs_info->extent_root;
3176 block_group_cache = &root->fs_info->block_group_cache;
3177
8f18cf13 3178 cache = kzalloc(sizeof(*cache), GFP_NOFS);
6324fbf3 3179 BUG_ON(!cache);
e17cade2 3180 cache->key.objectid = chunk_offset;
6324fbf3 3181 cache->key.offset = size;
6324fbf3 3182 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
0ef3e66b 3183
6324fbf3 3184 btrfs_set_block_group_used(&cache->item, bytes_used);
6324fbf3
CM
3185 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
3186 cache->flags = type;
3187 btrfs_set_block_group_flags(&cache->item, type);
3188
3189 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
3190 &cache->space_info);
3191 BUG_ON(ret);
3192
d18a2c44 3193 bit = block_group_state_bits(type);
e17cade2
CM
3194 set_extent_bits(block_group_cache, chunk_offset,
3195 chunk_offset + size - 1,
6324fbf3 3196 bit | EXTENT_LOCKED, GFP_NOFS);
6324fbf3 3197
e17cade2
CM
3198 set_state_private(block_group_cache, chunk_offset,
3199 (unsigned long)cache);
6324fbf3
CM
3200 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
3201 sizeof(cache->item));
3202 BUG_ON(ret);
3203
3204 finish_current_insert(trans, extent_root);
3205 ret = del_pending_extents(trans, extent_root);
3206 BUG_ON(ret);
d18a2c44 3207 set_avail_alloc_bits(extent_root->fs_info, type);
6324fbf3
CM
3208 return 0;
3209}
This page took 0.229611 seconds and 5 git commands to generate.