blob: 55abdf997ca571ff2de621586085d1568cdcb17d [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Zach Brownec6b9102007-07-11 10:00:37 -040019#include <linux/sched.h>
Chris Masonfec577f2007-02-26 10:40:21 -050020#include "ctree.h"
21#include "disk-io.h"
22#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040023#include "transaction.h"
Chris Masonfec577f2007-02-26 10:40:21 -050024
Chris Mason96b51792007-10-15 16:15:19 -040025#define BLOCK_GROUP_DATA EXTENT_WRITEBACK
26#define BLOCK_GROUP_METADATA EXTENT_UPTODATE
27#define BLOCK_GROUP_DIRTY EXTENT_DIRTY
28
Chris Masone089f052007-03-16 16:20:31 -040029static int finish_current_insert(struct btrfs_trans_handle *trans, struct
30 btrfs_root *extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -040031static int del_pending_extents(struct btrfs_trans_handle *trans, struct
32 btrfs_root *extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -050033
Chris Masone37c9e62007-05-09 20:13:14 -040034static int cache_block_group(struct btrfs_root *root,
35 struct btrfs_block_group_cache *block_group)
36{
37 struct btrfs_path *path;
38 int ret;
39 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -040040 struct extent_buffer *leaf;
Chris Masonf510cfe2007-10-15 16:14:48 -040041 struct extent_map_tree *free_space_cache;
Chris Masone37c9e62007-05-09 20:13:14 -040042 int slot;
Chris Masone37c9e62007-05-09 20:13:14 -040043 u64 last = 0;
44 u64 hole_size;
Yan7d7d6062007-09-14 16:15:28 -040045 u64 first_free;
Chris Masone37c9e62007-05-09 20:13:14 -040046 int found = 0;
47
Chris Mason00f5c792007-11-30 10:09:33 -050048 if (!block_group)
49 return 0;
50
Chris Masone37c9e62007-05-09 20:13:14 -040051 root = root->fs_info->extent_root;
Chris Masonf510cfe2007-10-15 16:14:48 -040052 free_space_cache = &root->fs_info->free_space_cache;
Chris Masone37c9e62007-05-09 20:13:14 -040053
54 if (block_group->cached)
55 return 0;
Chris Masonf510cfe2007-10-15 16:14:48 -040056
Chris Masone37c9e62007-05-09 20:13:14 -040057 path = btrfs_alloc_path();
58 if (!path)
59 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -040060
Chris Mason2cc58cf2007-08-27 16:49:44 -040061 path->reada = 2;
Yan7d7d6062007-09-14 16:15:28 -040062 first_free = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -040063 key.objectid = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -040064 key.offset = 0;
Yan7d7d6062007-09-14 16:15:28 -040065
Chris Masone37c9e62007-05-09 20:13:14 -040066 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
67 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Yan7d7d6062007-09-14 16:15:28 -040068
Chris Masone37c9e62007-05-09 20:13:14 -040069 if (ret < 0)
70 return ret;
Yan7d7d6062007-09-14 16:15:28 -040071
Chris Masone37c9e62007-05-09 20:13:14 -040072 if (ret && path->slots[0] > 0)
73 path->slots[0]--;
Yan7d7d6062007-09-14 16:15:28 -040074
Chris Masone37c9e62007-05-09 20:13:14 -040075 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -040076 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -040077 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -040078 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -040079 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -040080 if (ret < 0)
81 goto err;
Chris Masonde428b62007-05-18 13:28:27 -040082 if (ret == 0) {
Chris Masone37c9e62007-05-09 20:13:14 -040083 continue;
Chris Masonde428b62007-05-18 13:28:27 -040084 } else {
Chris Masone37c9e62007-05-09 20:13:14 -040085 break;
86 }
87 }
Yan7d7d6062007-09-14 16:15:28 -040088
Chris Mason5f39d392007-10-15 16:14:19 -040089 btrfs_item_key_to_cpu(leaf, &key, slot);
Yan7d7d6062007-09-14 16:15:28 -040090 if (key.objectid < block_group->key.objectid) {
91 if (key.objectid + key.offset > first_free)
92 first_free = key.objectid + key.offset;
93 goto next;
94 }
95
Chris Masone37c9e62007-05-09 20:13:14 -040096 if (key.objectid >= block_group->key.objectid +
97 block_group->key.offset) {
Yan7d7d6062007-09-14 16:15:28 -040098 break;
99 }
100
101 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
102 if (!found) {
103 last = first_free;
104 found = 1;
Chris Masone37c9e62007-05-09 20:13:14 -0400105 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400106 if (key.objectid > last) {
107 hole_size = key.objectid - last;
108 set_extent_dirty(free_space_cache, last,
109 last + hole_size - 1,
110 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -0400111 }
Yan7d7d6062007-09-14 16:15:28 -0400112 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400113 }
Yan7d7d6062007-09-14 16:15:28 -0400114next:
Chris Masone37c9e62007-05-09 20:13:14 -0400115 path->slots[0]++;
116 }
117
Yan7d7d6062007-09-14 16:15:28 -0400118 if (!found)
119 last = first_free;
120 if (block_group->key.objectid +
121 block_group->key.offset > last) {
122 hole_size = block_group->key.objectid +
123 block_group->key.offset - last;
Chris Masonf510cfe2007-10-15 16:14:48 -0400124 set_extent_dirty(free_space_cache, last,
125 last + hole_size - 1, GFP_NOFS);
Yan7d7d6062007-09-14 16:15:28 -0400126 }
Chris Masone37c9e62007-05-09 20:13:14 -0400127 block_group->cached = 1;
Chris Mason54aa1f42007-06-22 14:16:25 -0400128err:
Chris Masone37c9e62007-05-09 20:13:14 -0400129 btrfs_free_path(path);
130 return 0;
131}
132
Chris Mason5276aed2007-06-11 21:33:38 -0400133struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
134 btrfs_fs_info *info,
Chris Masondb945352007-10-15 16:15:53 -0400135 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400136{
Chris Mason96b51792007-10-15 16:15:19 -0400137 struct extent_map_tree *block_group_cache;
138 struct btrfs_block_group_cache *block_group = NULL;
139 u64 ptr;
140 u64 start;
141 u64 end;
Chris Masonbe744172007-05-06 10:15:01 -0400142 int ret;
143
Chris Mason96b51792007-10-15 16:15:19 -0400144 block_group_cache = &info->block_group_cache;
145 ret = find_first_extent_bit(block_group_cache,
Chris Masondb945352007-10-15 16:15:53 -0400146 bytenr, &start, &end,
Chris Mason96b51792007-10-15 16:15:19 -0400147 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA);
Chris Masonbe744172007-05-06 10:15:01 -0400148 if (ret) {
Chris Mason96b51792007-10-15 16:15:19 -0400149 return NULL;
Chris Masonbe744172007-05-06 10:15:01 -0400150 }
Chris Mason96b51792007-10-15 16:15:19 -0400151 ret = get_state_private(block_group_cache, start, &ptr);
152 if (ret)
153 return NULL;
154
Jens Axboeae2f5412007-10-19 09:22:59 -0400155 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Yan5cf66422007-11-16 14:57:09 -0500156 if (block_group->key.objectid <= bytenr && bytenr <
Chris Mason96b51792007-10-15 16:15:19 -0400157 block_group->key.objectid + block_group->key.offset)
158 return block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400159 return NULL;
160}
Chris Masone37c9e62007-05-09 20:13:14 -0400161static u64 find_search_start(struct btrfs_root *root,
162 struct btrfs_block_group_cache **cache_ret,
Yan5e5745d2007-11-16 14:57:09 -0500163 u64 search_start, int num,
164 int data, int full_scan)
Chris Masone37c9e62007-05-09 20:13:14 -0400165{
Chris Masone37c9e62007-05-09 20:13:14 -0400166 int ret;
167 struct btrfs_block_group_cache *cache = *cache_ret;
Chris Masone19caa52007-10-15 16:17:44 -0400168 u64 last;
Chris Masonf510cfe2007-10-15 16:14:48 -0400169 u64 start = 0;
170 u64 end = 0;
Chris Mason257d0ce2007-11-07 21:08:16 -0500171 u64 cache_miss = 0;
Chris Masonf84a8b32007-11-06 10:26:29 -0500172 int wrapped = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400173
Chris Mason00f5c792007-11-30 10:09:33 -0500174 if (!cache) {
175 cache = btrfs_lookup_block_group(root->fs_info, search_start);
176 if (!cache)
177 return search_start;
178 }
Chris Masone37c9e62007-05-09 20:13:14 -0400179again:
Chris Mason54aa1f42007-06-22 14:16:25 -0400180 ret = cache_block_group(root, cache);
181 if (ret)
182 goto out;
Chris Masonf84a8b32007-11-06 10:26:29 -0500183
Chris Masone19caa52007-10-15 16:17:44 -0400184 last = max(search_start, cache->key.objectid);
185
Chris Masone37c9e62007-05-09 20:13:14 -0400186 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -0400187 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
188 last, &start, &end, EXTENT_DIRTY);
Chris Masone19caa52007-10-15 16:17:44 -0400189 if (ret) {
Chris Mason257d0ce2007-11-07 21:08:16 -0500190 if (!cache_miss)
191 cache_miss = last;
Chris Masone19caa52007-10-15 16:17:44 -0400192 goto new_group;
193 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400194
195 start = max(last, start);
196 last = end + 1;
Chris Mason257d0ce2007-11-07 21:08:16 -0500197 if (last - start < num) {
198 if (last == cache->key.objectid + cache->key.offset)
199 cache_miss = start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400200 continue;
Chris Mason257d0ce2007-11-07 21:08:16 -0500201 }
202 if (data != BTRFS_BLOCK_GROUP_MIXED &&
Yan5cf66422007-11-16 14:57:09 -0500203 start + num > cache->key.objectid + cache->key.offset)
Chris Masone37c9e62007-05-09 20:13:14 -0400204 goto new_group;
Chris Masonf510cfe2007-10-15 16:14:48 -0400205 return start;
Chris Masone37c9e62007-05-09 20:13:14 -0400206 }
207out:
Chris Mason1a5bc162007-10-15 16:15:26 -0400208 return search_start;
Chris Masone37c9e62007-05-09 20:13:14 -0400209
210new_group:
Chris Masone19caa52007-10-15 16:17:44 -0400211 last = cache->key.objectid + cache->key.offset;
Chris Masonf84a8b32007-11-06 10:26:29 -0500212wrapped:
Chris Masone19caa52007-10-15 16:17:44 -0400213 cache = btrfs_lookup_block_group(root->fs_info, last);
Chris Masone37c9e62007-05-09 20:13:14 -0400214 if (!cache) {
Chris Mason0e4de582007-11-26 10:55:49 -0500215no_cache:
Chris Masonf84a8b32007-11-06 10:26:29 -0500216 if (!wrapped) {
217 wrapped = 1;
218 last = search_start;
219 data = BTRFS_BLOCK_GROUP_MIXED;
220 goto wrapped;
221 }
Chris Mason1a5bc162007-10-15 16:15:26 -0400222 return search_start;
Chris Masone37c9e62007-05-09 20:13:14 -0400223 }
Chris Mason257d0ce2007-11-07 21:08:16 -0500224 if (cache_miss && !cache->cached) {
225 cache_block_group(root, cache);
226 last = cache_miss;
Chris Mason257d0ce2007-11-07 21:08:16 -0500227 cache = btrfs_lookup_block_group(root->fs_info, last);
228 }
Yan5e5745d2007-11-16 14:57:09 -0500229 if (!full_scan)
230 cache = btrfs_find_block_group(root, cache, last, data, 0);
Chris Mason0e4de582007-11-26 10:55:49 -0500231 if (!cache)
232 goto no_cache;
Chris Masone37c9e62007-05-09 20:13:14 -0400233 *cache_ret = cache;
Chris Mason257d0ce2007-11-07 21:08:16 -0500234 cache_miss = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400235 goto again;
236}
237
Chris Mason84f54cf2007-06-12 07:43:08 -0400238static u64 div_factor(u64 num, int factor)
239{
Chris Mason257d0ce2007-11-07 21:08:16 -0500240 if (factor == 10)
241 return num;
Chris Mason84f54cf2007-06-12 07:43:08 -0400242 num *= factor;
243 do_div(num, 10);
244 return num;
245}
246
Chris Mason31f3c992007-04-30 15:25:45 -0400247struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
248 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400249 *hint, u64 search_start,
Chris Masonde428b62007-05-18 13:28:27 -0400250 int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400251{
Chris Mason96b51792007-10-15 16:15:19 -0400252 struct btrfs_block_group_cache *cache;
253 struct extent_map_tree *block_group_cache;
Chris Mason31f3c992007-04-30 15:25:45 -0400254 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400255 struct btrfs_fs_info *info = root->fs_info;
256 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400257 u64 last = 0;
258 u64 hint_last;
Chris Mason96b51792007-10-15 16:15:19 -0400259 u64 start;
260 u64 end;
261 u64 free_check;
262 u64 ptr;
263 int bit;
Chris Masoncd1bc462007-04-27 10:08:34 -0400264 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400265 int full_search = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400266 int factor = 8;
Chris Mason1e2677e2007-05-29 16:52:18 -0400267 int data_swap = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400268
Chris Mason96b51792007-10-15 16:15:19 -0400269 block_group_cache = &info->block_group_cache;
270
Chris Masonde428b62007-05-18 13:28:27 -0400271 if (!owner)
Chris Masonf84a8b32007-11-06 10:26:29 -0500272 factor = 8;
Chris Masonbe744172007-05-06 10:15:01 -0400273
Chris Mason257d0ce2007-11-07 21:08:16 -0500274 if (data == BTRFS_BLOCK_GROUP_MIXED) {
Chris Masonf84a8b32007-11-06 10:26:29 -0500275 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
Chris Mason257d0ce2007-11-07 21:08:16 -0500276 factor = 10;
277 } else if (data)
Chris Mason96b51792007-10-15 16:15:19 -0400278 bit = BLOCK_GROUP_DATA;
279 else
280 bit = BLOCK_GROUP_METADATA;
Chris Masonbe744172007-05-06 10:15:01 -0400281
282 if (search_start) {
283 struct btrfs_block_group_cache *shint;
Chris Mason5276aed2007-06-11 21:33:38 -0400284 shint = btrfs_lookup_block_group(info, search_start);
Chris Masonf84a8b32007-11-06 10:26:29 -0500285 if (shint && (shint->data == data ||
286 shint->data == BTRFS_BLOCK_GROUP_MIXED)) {
Chris Masonbe744172007-05-06 10:15:01 -0400287 used = btrfs_block_group_used(&shint->item);
Yan324ae4d2007-11-16 14:57:08 -0500288 if (used + shint->pinned <
289 div_factor(shint->key.offset, factor)) {
Chris Masonbe744172007-05-06 10:15:01 -0400290 return shint;
291 }
292 }
293 }
Chris Masonf84a8b32007-11-06 10:26:29 -0500294 if (hint && (hint->data == data ||
295 hint->data == BTRFS_BLOCK_GROUP_MIXED)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400296 used = btrfs_block_group_used(&hint->item);
Yan324ae4d2007-11-16 14:57:08 -0500297 if (used + hint->pinned <
298 div_factor(hint->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400299 return hint;
300 }
Chris Masone19caa52007-10-15 16:17:44 -0400301 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400302 hint_last = last;
303 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400304 if (hint)
305 hint_last = max(hint->key.objectid, search_start);
306 else
307 hint_last = search_start;
308
309 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400310 }
Chris Mason31f3c992007-04-30 15:25:45 -0400311again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400312 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400313 ret = find_first_extent_bit(block_group_cache, last,
314 &start, &end, bit);
315 if (ret)
Chris Masoncd1bc462007-04-27 10:08:34 -0400316 break;
Chris Mason96b51792007-10-15 16:15:19 -0400317
318 ret = get_state_private(block_group_cache, start, &ptr);
319 if (ret)
320 break;
321
Jens Axboeae2f5412007-10-19 09:22:59 -0400322 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -0400323 last = cache->key.objectid + cache->key.offset;
324 used = btrfs_block_group_used(&cache->item);
325
326 if (full_search)
327 free_check = cache->key.offset;
328 else
329 free_check = div_factor(cache->key.offset, factor);
Yan324ae4d2007-11-16 14:57:08 -0500330 if (used + cache->pinned < free_check) {
Chris Mason96b51792007-10-15 16:15:19 -0400331 found_group = cache;
332 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400333 }
Chris Masonde428b62007-05-18 13:28:27 -0400334 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400335 }
Chris Mason31f3c992007-04-30 15:25:45 -0400336 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400337 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400338 full_search = 1;
339 goto again;
340 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400341 if (!data_swap) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400342 data_swap = 1;
Chris Mason96b51792007-10-15 16:15:19 -0400343 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
Chris Mason1e2677e2007-05-29 16:52:18 -0400344 last = search_start;
345 goto again;
346 }
Chris Masonbe744172007-05-06 10:15:01 -0400347found:
Chris Mason31f3c992007-04-30 15:25:45 -0400348 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400349}
350
Chris Masonb18c6682007-04-17 13:26:50 -0400351int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
352 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -0400353 u64 bytenr, u64 num_bytes)
Chris Mason02217ed2007-03-02 16:08:05 -0500354{
Chris Mason5caf2a02007-04-02 11:20:42 -0400355 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500356 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400357 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400358 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400359 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400360 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500361
Chris Masondb945352007-10-15 16:15:53 -0400362 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400363 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400364 if (!path)
365 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400366
Chris Masondb945352007-10-15 16:15:53 -0400367 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -0400368 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -0400369 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -0400370 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400371 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400372 if (ret < 0)
373 return ret;
Chris Masona429e512007-04-18 16:15:28 -0400374 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500375 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400376 }
Chris Mason02217ed2007-03-02 16:08:05 -0500377 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400378 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400379 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400380 refs = btrfs_extent_refs(l, item);
381 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400382 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500383
Chris Mason5caf2a02007-04-02 11:20:42 -0400384 btrfs_release_path(root->fs_info->extent_root, path);
385 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400386 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400387 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -0500388 return 0;
389}
390
Chris Masone9d0b132007-08-10 14:06:19 -0400391int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
392 struct btrfs_root *root)
393{
394 finish_current_insert(trans, root->fs_info->extent_root);
395 del_pending_extents(trans, root->fs_info->extent_root);
396 return 0;
397}
398
Chris Masonb18c6682007-04-17 13:26:50 -0400399static int lookup_extent_ref(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -0400400 struct btrfs_root *root, u64 bytenr,
401 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500402{
Chris Mason5caf2a02007-04-02 11:20:42 -0400403 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500404 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400405 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400406 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400407 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400408
Chris Masondb945352007-10-15 16:15:53 -0400409 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400410 path = btrfs_alloc_path();
Chris Masondb945352007-10-15 16:15:53 -0400411 key.objectid = bytenr;
412 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -0400413 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400414 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400415 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400416 if (ret < 0)
417 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400418 if (ret != 0) {
419 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -0400420 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -0500421 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -0400422 }
423 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400424 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400425 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400426out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400427 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500428 return 0;
429}
430
Chris Masonc5739bb2007-04-10 09:27:04 -0400431int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
432 struct btrfs_root *root)
433{
Chris Masondb945352007-10-15 16:15:53 -0400434 return btrfs_inc_extent_ref(trans, root, root->node->start,
435 root->node->len);
Chris Masonc5739bb2007-04-10 09:27:04 -0400436}
437
Chris Masone089f052007-03-16 16:20:31 -0400438int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400439 struct extent_buffer *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500440{
Chris Masondb945352007-10-15 16:15:53 -0400441 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400442 u32 nritems;
443 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -0400444 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500445 int i;
Chris Masondb945352007-10-15 16:15:53 -0400446 int level;
Chris Mason6407bf62007-03-27 06:33:00 -0400447 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400448 int faili;
449 int err;
Chris Masona28ec192007-03-06 20:08:01 -0500450
Chris Mason3768f362007-03-13 16:47:54 -0400451 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500452 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400453
Chris Masondb945352007-10-15 16:15:53 -0400454 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400455 nritems = btrfs_header_nritems(buf);
456 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400457 if (level == 0) {
458 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400459 btrfs_item_key_to_cpu(buf, &key, i);
460 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -0400461 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400462 fi = btrfs_item_ptr(buf, i,
Chris Mason6407bf62007-03-27 06:33:00 -0400463 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400464 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason236454df2007-04-19 13:37:44 -0400465 BTRFS_FILE_EXTENT_INLINE)
466 continue;
Chris Masondb945352007-10-15 16:15:53 -0400467 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
468 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -0400469 continue;
Chris Masondb945352007-10-15 16:15:53 -0400470 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
471 btrfs_file_extent_disk_num_bytes(buf, fi));
Chris Mason54aa1f42007-06-22 14:16:25 -0400472 if (ret) {
473 faili = i;
474 goto fail;
475 }
Chris Mason6407bf62007-03-27 06:33:00 -0400476 } else {
Chris Masondb945352007-10-15 16:15:53 -0400477 bytenr = btrfs_node_blockptr(buf, i);
478 ret = btrfs_inc_extent_ref(trans, root, bytenr,
479 btrfs_level_size(root, level - 1));
Chris Mason54aa1f42007-06-22 14:16:25 -0400480 if (ret) {
481 faili = i;
482 goto fail;
483 }
Chris Mason6407bf62007-03-27 06:33:00 -0400484 }
Chris Mason02217ed2007-03-02 16:08:05 -0500485 }
486 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400487fail:
Chris Masonccd467d2007-06-28 15:57:36 -0400488 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400489 for (i =0; i < faili; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400490 if (level == 0) {
491 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400492 btrfs_item_key_to_cpu(buf, &key, i);
493 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -0400494 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400495 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -0400496 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400497 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -0400498 BTRFS_FILE_EXTENT_INLINE)
499 continue;
Chris Masondb945352007-10-15 16:15:53 -0400500 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
501 if (disk_bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -0400502 continue;
Chris Masondb945352007-10-15 16:15:53 -0400503 err = btrfs_free_extent(trans, root, disk_bytenr,
504 btrfs_file_extent_disk_num_bytes(buf,
Chris Mason5f39d392007-10-15 16:14:19 -0400505 fi), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400506 BUG_ON(err);
507 } else {
Chris Masondb945352007-10-15 16:15:53 -0400508 bytenr = btrfs_node_blockptr(buf, i);
509 err = btrfs_free_extent(trans, root, bytenr,
510 btrfs_level_size(root, level - 1), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400511 BUG_ON(err);
512 }
513 }
514 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -0500515}
516
Chris Mason9078a3e2007-04-26 16:46:15 -0400517static int write_one_cache_group(struct btrfs_trans_handle *trans,
518 struct btrfs_root *root,
519 struct btrfs_path *path,
520 struct btrfs_block_group_cache *cache)
521{
522 int ret;
523 int pending_ret;
524 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400525 unsigned long bi;
526 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -0400527
Chris Mason9078a3e2007-04-26 16:46:15 -0400528 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400529 if (ret < 0)
530 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -0400531 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400532
533 leaf = path->nodes[0];
534 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
535 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
536 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -0400537 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400538fail:
Chris Mason9078a3e2007-04-26 16:46:15 -0400539 finish_current_insert(trans, extent_root);
540 pending_ret = del_pending_extents(trans, extent_root);
541 if (ret)
542 return ret;
543 if (pending_ret)
544 return pending_ret;
545 return 0;
546
547}
548
Chris Mason96b51792007-10-15 16:15:19 -0400549int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
550 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -0400551{
Chris Mason96b51792007-10-15 16:15:19 -0400552 struct extent_map_tree *block_group_cache;
553 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -0400554 int ret;
555 int err = 0;
556 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400557 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -0400558 u64 last = 0;
559 u64 start;
560 u64 end;
561 u64 ptr;
Chris Mason9078a3e2007-04-26 16:46:15 -0400562
Chris Mason96b51792007-10-15 16:15:19 -0400563 block_group_cache = &root->fs_info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -0400564 path = btrfs_alloc_path();
565 if (!path)
566 return -ENOMEM;
567
568 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400569 ret = find_first_extent_bit(block_group_cache, last,
570 &start, &end, BLOCK_GROUP_DIRTY);
571 if (ret)
Chris Mason9078a3e2007-04-26 16:46:15 -0400572 break;
Chris Mason54aa1f42007-06-22 14:16:25 -0400573
Chris Mason96b51792007-10-15 16:15:19 -0400574 last = end + 1;
575 ret = get_state_private(block_group_cache, start, &ptr);
576 if (ret)
577 break;
578
Jens Axboeae2f5412007-10-19 09:22:59 -0400579 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -0400580 err = write_one_cache_group(trans, root,
581 path, cache);
582 /*
583 * if we fail to write the cache group, we want
584 * to keep it marked dirty in hopes that a later
585 * write will work
586 */
587 if (err) {
588 werr = err;
589 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -0400590 }
Chris Mason96b51792007-10-15 16:15:19 -0400591 clear_extent_bits(block_group_cache, start, end,
592 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -0400593 }
594 btrfs_free_path(path);
595 return werr;
596}
597
598static int update_block_group(struct btrfs_trans_handle *trans,
599 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -0400600 u64 bytenr, u64 num_bytes, int alloc,
601 int mark_free, int data)
Chris Mason9078a3e2007-04-26 16:46:15 -0400602{
603 struct btrfs_block_group_cache *cache;
604 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -0400605 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -0400606 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -0400607 u64 byte_in_group;
Chris Mason96b51792007-10-15 16:15:19 -0400608 u64 start;
609 u64 end;
Chris Mason3e1ad542007-05-07 20:03:49 -0400610
Chris Mason9078a3e2007-04-26 16:46:15 -0400611 while(total) {
Chris Masondb945352007-10-15 16:15:53 -0400612 cache = btrfs_lookup_block_group(info, bytenr);
Chris Mason3e1ad542007-05-07 20:03:49 -0400613 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -0400614 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400615 }
Chris Masondb945352007-10-15 16:15:53 -0400616 byte_in_group = bytenr - cache->key.objectid;
617 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason96b51792007-10-15 16:15:19 -0400618 start = cache->key.objectid;
619 end = start + cache->key.offset - 1;
620 set_extent_bits(&info->block_group_cache, start, end,
621 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -0400622
623 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -0400624 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -0400625 if (alloc) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400626 if (cache->data != data &&
Chris Mason84f54cf2007-06-12 07:43:08 -0400627 old_val < (cache->key.offset >> 1)) {
Chris Mason96b51792007-10-15 16:15:19 -0400628 int bit_to_clear;
629 int bit_to_set;
Chris Mason96b51792007-10-15 16:15:19 -0400630 cache->data = data;
Chris Mason1e2677e2007-05-29 16:52:18 -0400631 if (data) {
Yanb97f9202007-11-01 11:28:41 -0400632 bit_to_clear = BLOCK_GROUP_METADATA;
633 bit_to_set = BLOCK_GROUP_DATA;
Chris Masonf84a8b32007-11-06 10:26:29 -0500634 cache->item.flags &=
635 ~BTRFS_BLOCK_GROUP_MIXED;
Chris Mason1e2677e2007-05-29 16:52:18 -0400636 cache->item.flags |=
637 BTRFS_BLOCK_GROUP_DATA;
638 } else {
Yanb97f9202007-11-01 11:28:41 -0400639 bit_to_clear = BLOCK_GROUP_DATA;
640 bit_to_set = BLOCK_GROUP_METADATA;
Chris Mason1e2677e2007-05-29 16:52:18 -0400641 cache->item.flags &=
Chris Masonf84a8b32007-11-06 10:26:29 -0500642 ~BTRFS_BLOCK_GROUP_MIXED;
643 cache->item.flags &=
Chris Mason1e2677e2007-05-29 16:52:18 -0400644 ~BTRFS_BLOCK_GROUP_DATA;
645 }
Chris Mason96b51792007-10-15 16:15:19 -0400646 clear_extent_bits(&info->block_group_cache,
647 start, end, bit_to_clear,
648 GFP_NOFS);
649 set_extent_bits(&info->block_group_cache,
650 start, end, bit_to_set,
651 GFP_NOFS);
Chris Masonf84a8b32007-11-06 10:26:29 -0500652 } else if (cache->data != data &&
653 cache->data != BTRFS_BLOCK_GROUP_MIXED) {
654 cache->data = BTRFS_BLOCK_GROUP_MIXED;
655 set_extent_bits(&info->block_group_cache,
656 start, end,
657 BLOCK_GROUP_DATA |
658 BLOCK_GROUP_METADATA,
659 GFP_NOFS);
Chris Mason1e2677e2007-05-29 16:52:18 -0400660 }
Chris Masondb945352007-10-15 16:15:53 -0400661 old_val += num_bytes;
Chris Masoncd1bc462007-04-27 10:08:34 -0400662 } else {
Chris Masondb945352007-10-15 16:15:53 -0400663 old_val -= num_bytes;
Chris Masonf510cfe2007-10-15 16:14:48 -0400664 if (mark_free) {
665 set_extent_dirty(&info->free_space_cache,
Chris Masondb945352007-10-15 16:15:53 -0400666 bytenr, bytenr + num_bytes - 1,
Chris Masonf510cfe2007-10-15 16:14:48 -0400667 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -0400668 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400669 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400670 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masondb945352007-10-15 16:15:53 -0400671 total -= num_bytes;
672 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -0400673 }
674 return 0;
675}
Yan324ae4d2007-11-16 14:57:08 -0500676static int update_pinned_extents(struct btrfs_root *root,
677 u64 bytenr, u64 num, int pin)
678{
679 u64 len;
680 struct btrfs_block_group_cache *cache;
681 struct btrfs_fs_info *fs_info = root->fs_info;
682
683 if (pin) {
684 set_extent_dirty(&fs_info->pinned_extents,
685 bytenr, bytenr + num - 1, GFP_NOFS);
686 } else {
687 clear_extent_dirty(&fs_info->pinned_extents,
688 bytenr, bytenr + num - 1, GFP_NOFS);
689 }
690 while (num > 0) {
691 cache = btrfs_lookup_block_group(fs_info, bytenr);
692 WARN_ON(!cache);
693 len = min(num, cache->key.offset -
694 (bytenr - cache->key.objectid));
695 if (pin) {
696 cache->pinned += len;
697 fs_info->total_pinned += len;
698 } else {
699 cache->pinned -= len;
700 fs_info->total_pinned -= len;
701 }
702 bytenr += len;
703 num -= len;
704 }
705 return 0;
706}
Chris Mason9078a3e2007-04-26 16:46:15 -0400707
Chris Mason1a5bc162007-10-15 16:15:26 -0400708int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -0400709{
Chris Masonccd467d2007-06-28 15:57:36 -0400710 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -0400711 u64 start;
712 u64 end;
713 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -0400714 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -0400715
716 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -0400717 ret = find_first_extent_bit(pinned_extents, last,
718 &start, &end, EXTENT_DIRTY);
719 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -0400720 break;
Chris Mason1a5bc162007-10-15 16:15:26 -0400721 set_extent_dirty(copy, start, end, GFP_NOFS);
722 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -0400723 }
724 return 0;
725}
726
727int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
728 struct btrfs_root *root,
Chris Mason1a5bc162007-10-15 16:15:26 -0400729 struct extent_map_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -0500730{
Chris Mason1a5bc162007-10-15 16:15:26 -0400731 u64 start;
732 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -0500733 int ret;
Chris Masonf510cfe2007-10-15 16:14:48 -0400734 struct extent_map_tree *free_space_cache;
Chris Masonf510cfe2007-10-15 16:14:48 -0400735 free_space_cache = &root->fs_info->free_space_cache;
Chris Masona28ec192007-03-06 20:08:01 -0500736
737 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -0400738 ret = find_first_extent_bit(unpin, 0, &start, &end,
739 EXTENT_DIRTY);
740 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -0500741 break;
Yan324ae4d2007-11-16 14:57:08 -0500742 update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -0400743 clear_extent_dirty(unpin, start, end, GFP_NOFS);
744 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
Chris Masona28ec192007-03-06 20:08:01 -0500745 }
746 return 0;
747}
748
Chris Masone089f052007-03-16 16:20:31 -0400749static int finish_current_insert(struct btrfs_trans_handle *trans, struct
750 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500751{
Chris Masone2fa7222007-03-12 16:22:34 -0400752 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400753 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500754 int ret;
Chris Mason1a5bc162007-10-15 16:15:26 -0400755 int err = 0;
756 u64 start;
757 u64 end;
Chris Mason1261ec42007-03-20 20:35:03 -0400758 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500759
Chris Mason5f39d392007-10-15 16:14:19 -0400760 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason62e27492007-03-15 12:56:47 -0400761 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5f39d392007-10-15 16:14:19 -0400762 btrfs_set_stack_extent_owner(&extent_item,
763 extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500764
Chris Mason26b80032007-08-08 20:17:12 -0400765 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -0400766 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
767 &end, EXTENT_LOCKED);
768 if (ret)
Chris Mason26b80032007-08-08 20:17:12 -0400769 break;
770
Chris Mason1a5bc162007-10-15 16:15:26 -0400771 ins.objectid = start;
772 ins.offset = end + 1 - start;
773 err = btrfs_insert_item(trans, extent_root, &ins,
774 &extent_item, sizeof(extent_item));
775 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
776 GFP_NOFS);
Chris Mason037e6392007-03-07 11:50:24 -0500777 }
Chris Mason037e6392007-03-07 11:50:24 -0500778 return 0;
779}
780
Chris Masondb945352007-10-15 16:15:53 -0400781static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
782 int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400783{
Chris Mason1a5bc162007-10-15 16:15:26 -0400784 int err = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400785 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -0400786
Chris Masonf4b9aa82007-03-27 11:05:53 -0400787 if (!pending) {
Chris Masondb945352007-10-15 16:15:53 -0400788 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
Chris Mason5f39d392007-10-15 16:14:19 -0400789 if (buf) {
790 if (btrfs_buffer_uptodate(buf)) {
Chris Mason2c90e5d2007-04-02 10:50:19 -0400791 u64 transid =
792 root->fs_info->running_transaction->transid;
Chris Mason5f39d392007-10-15 16:14:19 -0400793 if (btrfs_header_generation(buf) == transid) {
794 free_extent_buffer(buf);
Yanc5492282007-11-06 10:25:25 -0500795 return 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400796 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400797 }
Chris Mason5f39d392007-10-15 16:14:19 -0400798 free_extent_buffer(buf);
Chris Mason8ef97622007-03-26 10:15:30 -0400799 }
Yan324ae4d2007-11-16 14:57:08 -0500800 update_pinned_extents(root, bytenr, num_bytes, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400801 } else {
Chris Mason1a5bc162007-10-15 16:15:26 -0400802 set_extent_bits(&root->fs_info->pending_del,
Chris Masondb945352007-10-15 16:15:53 -0400803 bytenr, bytenr + num_bytes - 1,
804 EXTENT_LOCKED, GFP_NOFS);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400805 }
Chris Masonbe744172007-05-06 10:15:01 -0400806 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400807 return 0;
808}
809
Chris Masona28ec192007-03-06 20:08:01 -0500810/*
811 * remove an extent from the root, returns 0 on success
812 */
Chris Masone089f052007-03-16 16:20:31 -0400813static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masondb945352007-10-15 16:15:53 -0400814 *root, u64 bytenr, u64 num_bytes, int pin,
Chris Masone37c9e62007-05-09 20:13:14 -0400815 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -0500816{
Chris Mason5caf2a02007-04-02 11:20:42 -0400817 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400818 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400819 struct btrfs_fs_info *info = root->fs_info;
820 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400821 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -0500822 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400823 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400824 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500825
Chris Masondb945352007-10-15 16:15:53 -0400826 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -0400827 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -0400828 key.offset = num_bytes;
Chris Masona28ec192007-03-06 20:08:01 -0500829
Chris Mason5caf2a02007-04-02 11:20:42 -0400830 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400831 if (!path)
832 return -ENOMEM;
833
Chris Mason5caf2a02007-04-02 11:20:42 -0400834 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400835 if (ret < 0)
836 return ret;
837 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400838
839 leaf = path->nodes[0];
840 ei = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400841 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400842 refs = btrfs_extent_refs(leaf, ei);
843 BUG_ON(refs == 0);
844 refs -= 1;
845 btrfs_set_extent_refs(leaf, ei, refs);
846 btrfs_mark_buffer_dirty(leaf);
847
Chris Masoncf27e1e2007-03-13 09:49:06 -0400848 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -0400849 u64 super_used;
850 u64 root_used;
Chris Mason78fae272007-03-25 11:35:08 -0400851
852 if (pin) {
Chris Masondb945352007-10-15 16:15:53 -0400853 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
Yanc5492282007-11-06 10:25:25 -0500854 if (ret > 0)
855 mark_free = 1;
856 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -0400857 }
858
Josef Bacik58176a92007-08-29 15:47:34 -0400859 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -0400860 super_used = btrfs_super_bytes_used(&info->super_copy);
861 btrfs_set_super_bytes_used(&info->super_copy,
862 super_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400863
864 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -0400865 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400866 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -0400867 root_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -0400868
Chris Mason5caf2a02007-04-02 11:20:42 -0400869 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400870 if (ret) {
871 return ret;
872 }
Chris Masondb945352007-10-15 16:15:53 -0400873 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason1e2677e2007-05-29 16:52:18 -0400874 mark_free, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400875 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500876 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400877 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400878 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500879 return ret;
880}
881
882/*
Chris Masonfec577f2007-02-26 10:40:21 -0500883 * find all the blocks marked as pending in the radix tree and remove
884 * them from the extent map
885 */
Chris Masone089f052007-03-16 16:20:31 -0400886static int del_pending_extents(struct btrfs_trans_handle *trans, struct
887 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500888{
889 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400890 int err = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -0400891 u64 start;
892 u64 end;
893 struct extent_map_tree *pending_del;
894 struct extent_map_tree *pinned_extents;
Chris Mason8ef97622007-03-26 10:15:30 -0400895
Chris Mason1a5bc162007-10-15 16:15:26 -0400896 pending_del = &extent_root->fs_info->pending_del;
897 pinned_extents = &extent_root->fs_info->pinned_extents;
Chris Masonfec577f2007-02-26 10:40:21 -0500898
899 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -0400900 ret = find_first_extent_bit(pending_del, 0, &start, &end,
901 EXTENT_LOCKED);
902 if (ret)
Chris Masonfec577f2007-02-26 10:40:21 -0500903 break;
Yan324ae4d2007-11-16 14:57:08 -0500904 update_pinned_extents(extent_root, start, end + 1 - start, 1);
Chris Mason1a5bc162007-10-15 16:15:26 -0400905 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
906 GFP_NOFS);
907 ret = __free_extent(trans, extent_root,
908 start, end + 1 - start, 0, 0);
909 if (ret)
910 err = ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500911 }
Chris Masone20d96d2007-03-22 12:13:20 -0400912 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500913}
914
915/*
916 * remove an extent from the root, returns 0 on success
917 */
Chris Masone089f052007-03-16 16:20:31 -0400918int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masondb945352007-10-15 16:15:53 -0400919 *root, u64 bytenr, u64 num_bytes, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500920{
Chris Mason9f5fae22007-03-20 14:38:32 -0400921 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500922 int pending_ret;
923 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500924
Chris Masondb945352007-10-15 16:15:53 -0400925 WARN_ON(num_bytes < root->sectorsize);
Chris Masona28ec192007-03-06 20:08:01 -0500926 if (root == extent_root) {
Chris Masondb945352007-10-15 16:15:53 -0400927 pin_down_bytes(root, bytenr, num_bytes, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500928 return 0;
929 }
Chris Masondb945352007-10-15 16:15:53 -0400930 ret = __free_extent(trans, root, bytenr, num_bytes, pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400931 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500932 return ret ? ret : pending_ret;
933}
934
935/*
936 * walks the btree of allocated extents and find a hole of a given size.
937 * The key ins is changed to record the hole:
938 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400939 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500940 * ins->offset == number of blocks
941 * Any available blocks before search_start are skipped.
942 */
Chris Masone089f052007-03-16 16:20:31 -0400943static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masondb945352007-10-15 16:15:53 -0400944 *orig_root, u64 num_bytes, u64 empty_size,
945 u64 search_start, u64 search_end, u64 hint_byte,
Chris Masonf2654de2007-06-26 12:20:46 -0400946 struct btrfs_key *ins, u64 exclude_start,
947 u64 exclude_nr, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500948{
Chris Mason5caf2a02007-04-02 11:20:42 -0400949 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400950 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500951 int ret;
952 u64 hole_size = 0;
953 int slot = 0;
Chris Masondb945352007-10-15 16:15:53 -0400954 u64 last_byte = 0;
Chris Masonbe744172007-05-06 10:15:01 -0400955 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500956 int start_found;
Chris Mason5f39d392007-10-15 16:14:19 -0400957 struct extent_buffer *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400958 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -0400959 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -0400960 u64 total_needed = num_bytes;
Chris Masone20d96d2007-03-22 12:13:20 -0400961 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -0400962 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400963 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -0400964 int wrapped = 0;
Chris Masonf84a8b32007-11-06 10:26:29 -0500965 u64 cached_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500966
Chris Masondb945352007-10-15 16:15:53 -0400967 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -0400968 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
969
Chris Mason5f39d392007-10-15 16:14:19 -0400970 level = btrfs_header_level(root->node);
971
Chris Mason015a739c2007-11-26 16:15:16 -0800972 if (num_bytes >= 32 * 1024 * 1024 && hint_byte) {
Chris Mason257d0ce2007-11-07 21:08:16 -0500973 data = BTRFS_BLOCK_GROUP_MIXED;
974 }
975
Chris Mason3e1ad542007-05-07 20:03:49 -0400976 if (search_end == (u64)-1)
Chris Masondb945352007-10-15 16:15:53 -0400977 search_end = btrfs_super_total_bytes(&info->super_copy);
978 if (hint_byte) {
979 block_group = btrfs_lookup_block_group(info, hint_byte);
Chris Masonbe744172007-05-06 10:15:01 -0400980 block_group = btrfs_find_block_group(root, block_group,
Chris Masondb945352007-10-15 16:15:53 -0400981 hint_byte, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400982 } else {
983 block_group = btrfs_find_block_group(root,
984 trans->block_group, 0,
Chris Masonde428b62007-05-18 13:28:27 -0400985 data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400986 }
987
Chris Mason6702ed42007-08-07 16:15:09 -0400988 total_needed += empty_size;
Chris Masone0115992007-06-19 16:23:05 -0400989 path = btrfs_alloc_path();
Chris Masonbe744172007-05-06 10:15:01 -0400990check_failed:
Yan5e5745d2007-11-16 14:57:09 -0500991 search_start = find_search_start(root, &block_group, search_start,
992 total_needed, data, full_scan);
Chris Masonf84a8b32007-11-06 10:26:29 -0500993 cached_start = search_start;
Chris Mason5caf2a02007-04-02 11:20:42 -0400994 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500995 ins->objectid = search_start;
996 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500997 start_found = 0;
Chris Mason2cc58cf2007-08-27 16:49:44 -0400998 path->reada = 2;
Chris Masone37c9e62007-05-09 20:13:14 -0400999
Chris Mason5caf2a02007-04-02 11:20:42 -04001000 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -05001001 if (ret < 0)
1002 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001003
Chris Masone37c9e62007-05-09 20:13:14 -04001004 if (path->slots[0] > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001005 path->slots[0]--;
Chris Masone37c9e62007-05-09 20:13:14 -04001006 }
1007
Chris Mason5f39d392007-10-15 16:14:19 -04001008 l = path->nodes[0];
1009 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1010
Chris Masone37c9e62007-05-09 20:13:14 -04001011 /*
1012 * a rare case, go back one key if we hit a block group item
1013 * instead of an extent item
1014 */
1015 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
1016 key.objectid + key.offset >= search_start) {
1017 ins->objectid = key.objectid;
1018 ins->offset = key.offset - 1;
1019 btrfs_release_path(root, path);
1020 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
1021 if (ret < 0)
1022 goto error;
1023
1024 if (path->slots[0] > 0) {
1025 path->slots[0]--;
1026 }
1027 }
Chris Mason0579da42007-03-07 16:15:30 -05001028
Chris Masonfec577f2007-02-26 10:40:21 -05001029 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001030 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -04001031 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04001032 if (slot >= btrfs_header_nritems(l)) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001033 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001034 if (ret == 0)
1035 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -05001036 if (ret < 0)
1037 goto error;
Chris Masone19caa52007-10-15 16:17:44 -04001038
1039 search_start = max(search_start,
1040 block_group->key.objectid);
Chris Masonfec577f2007-02-26 10:40:21 -05001041 if (!start_found) {
1042 ins->objectid = search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001043 ins->offset = search_end - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001044 start_found = 1;
1045 goto check_pending;
1046 }
Chris Masondb945352007-10-15 16:15:53 -04001047 ins->objectid = last_byte > search_start ?
1048 last_byte : search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001049 ins->offset = search_end - ins->objectid;
Chris Masone19caa52007-10-15 16:17:44 -04001050 BUG_ON(ins->objectid >= search_end);
Chris Masonfec577f2007-02-26 10:40:21 -05001051 goto check_pending;
1052 }
Chris Mason5f39d392007-10-15 16:14:19 -04001053 btrfs_item_key_to_cpu(l, &key, slot);
Chris Mason96b51792007-10-15 16:15:19 -04001054
Chris Masondb945352007-10-15 16:15:53 -04001055 if (key.objectid >= search_start && key.objectid > last_byte &&
Chris Masone37c9e62007-05-09 20:13:14 -04001056 start_found) {
Chris Masondb945352007-10-15 16:15:53 -04001057 if (last_byte < search_start)
1058 last_byte = search_start;
1059 hole_size = key.objectid - last_byte;
1060 if (hole_size >= num_bytes) {
1061 ins->objectid = last_byte;
Chris Masone37c9e62007-05-09 20:13:14 -04001062 ins->offset = hole_size;
1063 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -05001064 }
Chris Masonfec577f2007-02-26 10:40:21 -05001065 }
Chris Mason96b51792007-10-15 16:15:19 -04001066 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
1067 if (!start_found) {
Chris Masondb945352007-10-15 16:15:53 -04001068 last_byte = key.objectid;
Chris Mason96b51792007-10-15 16:15:19 -04001069 start_found = 1;
1070 }
Chris Masone37c9e62007-05-09 20:13:14 -04001071 goto next;
Chris Mason96b51792007-10-15 16:15:19 -04001072 }
1073
Chris Masone37c9e62007-05-09 20:13:14 -04001074
Chris Mason0579da42007-03-07 16:15:30 -05001075 start_found = 1;
Chris Masondb945352007-10-15 16:15:53 -04001076 last_byte = key.objectid + key.offset;
Chris Masonf510cfe2007-10-15 16:14:48 -04001077
Chris Mason257d0ce2007-11-07 21:08:16 -05001078 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
1079 last_byte >= block_group->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -04001080 block_group->key.offset) {
1081 btrfs_release_path(root, path);
1082 search_start = block_group->key.objectid +
Chris Masone19caa52007-10-15 16:17:44 -04001083 block_group->key.offset;
Chris Masonbe744172007-05-06 10:15:01 -04001084 goto new_group;
1085 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001086next:
Chris Mason5caf2a02007-04-02 11:20:42 -04001087 path->slots[0]++;
Chris Masonde428b62007-05-18 13:28:27 -04001088 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05001089 }
Chris Masonfec577f2007-02-26 10:40:21 -05001090check_pending:
1091 /* we have to make sure we didn't find an extent that has already
1092 * been allocated by the map tree or the original allocation
1093 */
Chris Mason5caf2a02007-04-02 11:20:42 -04001094 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001095 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -04001096
Chris Masondb945352007-10-15 16:15:53 -04001097 if (ins->objectid + num_bytes >= search_end)
Chris Masoncf675822007-09-17 11:00:51 -04001098 goto enospc;
Chris Mason257d0ce2007-11-07 21:08:16 -05001099 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
Yan5cf66422007-11-16 14:57:09 -05001100 ins->objectid + num_bytes > block_group->
Chris Masone19caa52007-10-15 16:17:44 -04001101 key.objectid + block_group->key.offset) {
1102 search_start = block_group->key.objectid +
1103 block_group->key.offset;
1104 goto new_group;
1105 }
Chris Mason1a5bc162007-10-15 16:15:26 -04001106 if (test_range_bit(&info->extent_ins, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001107 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1108 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001109 goto new_group;
1110 }
1111 if (test_range_bit(&info->pinned_extents, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001112 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1113 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001114 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001115 }
Chris Masondb945352007-10-15 16:15:53 -04001116 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
Chris Masonf2654de2007-06-26 12:20:46 -04001117 ins->objectid < exclude_start + exclude_nr)) {
1118 search_start = exclude_start + exclude_nr;
1119 goto new_group;
1120 }
Chris Masone37c9e62007-05-09 20:13:14 -04001121 if (!data) {
Chris Mason5276aed2007-06-11 21:33:38 -04001122 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Mason26b80032007-08-08 20:17:12 -04001123 if (block_group)
1124 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -04001125 }
Chris Masondb945352007-10-15 16:15:53 -04001126 ins->offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04001127 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001128 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001129
1130new_group:
Chris Masondb945352007-10-15 16:15:53 -04001131 if (search_start + num_bytes >= search_end) {
Chris Masoncf675822007-09-17 11:00:51 -04001132enospc:
Chris Masonbe744172007-05-06 10:15:01 -04001133 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001134 if (full_scan) {
1135 ret = -ENOSPC;
1136 goto error;
1137 }
Chris Mason6702ed42007-08-07 16:15:09 -04001138 if (wrapped) {
1139 if (!full_scan)
1140 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001141 full_scan = 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001142 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001143 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001144 }
Chris Mason5276aed2007-06-11 21:33:38 -04001145 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001146 cond_resched();
Chris Masonbe744172007-05-06 10:15:01 -04001147 if (!full_scan)
1148 block_group = btrfs_find_block_group(root, block_group,
Chris Masonde428b62007-05-18 13:28:27 -04001149 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001150 goto check_failed;
1151
Chris Mason0f70abe2007-02-28 16:46:22 -05001152error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001153 btrfs_release_path(root, path);
1154 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001155 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001156}
Chris Masonfec577f2007-02-26 10:40:21 -05001157/*
Chris Masonfec577f2007-02-26 10:40:21 -05001158 * finds a free extent and does all the dirty work required for allocation
1159 * returns the key for the extent through ins, and a tree buffer for
1160 * the first block of the extent through buf.
1161 *
1162 * returns 0 if everything worked, non-zero otherwise.
1163 */
Chris Mason4d775672007-04-20 20:23:12 -04001164int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1165 struct btrfs_root *root, u64 owner,
Chris Masondb945352007-10-15 16:15:53 -04001166 u64 num_bytes, u64 empty_size, u64 hint_byte,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001167 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001168{
1169 int ret;
1170 int pending_ret;
Chris Masondb945352007-10-15 16:15:53 -04001171 u64 super_used, root_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001172 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04001173 struct btrfs_fs_info *info = root->fs_info;
1174 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001175 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001176
Chris Mason5f39d392007-10-15 16:14:19 -04001177 btrfs_set_stack_extent_refs(&extent_item, 1);
1178 btrfs_set_stack_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -05001179
Chris Masondb945352007-10-15 16:15:53 -04001180 WARN_ON(num_bytes < root->sectorsize);
1181 ret = find_free_extent(trans, root, num_bytes, empty_size,
1182 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04001183 trans->alloc_exclude_start,
1184 trans->alloc_exclude_nr, data);
Chris Masonccd467d2007-06-28 15:57:36 -04001185 BUG_ON(ret);
Chris Masonf2654de2007-06-26 12:20:46 -04001186 if (ret)
1187 return ret;
1188
Josef Bacik58176a92007-08-29 15:47:34 -04001189 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -04001190 super_used = btrfs_super_bytes_used(&info->super_copy);
1191 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Mason26b80032007-08-08 20:17:12 -04001192
Josef Bacik58176a92007-08-29 15:47:34 -04001193 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001194 root_used = btrfs_root_used(&root->root_item);
1195 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001196
Chris Masonf510cfe2007-10-15 16:14:48 -04001197 clear_extent_dirty(&root->fs_info->free_space_cache,
1198 ins->objectid, ins->objectid + ins->offset - 1,
1199 GFP_NOFS);
1200
Chris Mason26b80032007-08-08 20:17:12 -04001201 if (root == extent_root) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001202 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1203 ins->objectid + ins->offset - 1,
1204 EXTENT_LOCKED, GFP_NOFS);
Chris Masone19caa52007-10-15 16:17:44 -04001205 WARN_ON(data == 1);
Chris Mason26b80032007-08-08 20:17:12 -04001206 goto update_block;
1207 }
1208
1209 WARN_ON(trans->alloc_exclude_nr);
1210 trans->alloc_exclude_start = ins->objectid;
1211 trans->alloc_exclude_nr = ins->offset;
Chris Masone089f052007-03-16 16:20:31 -04001212 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1213 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -05001214
Chris Mason26b80032007-08-08 20:17:12 -04001215 trans->alloc_exclude_start = 0;
1216 trans->alloc_exclude_nr = 0;
1217
Chris Masonccd467d2007-06-28 15:57:36 -04001218 BUG_ON(ret);
Chris Masone089f052007-03-16 16:20:31 -04001219 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001220 pending_ret = del_pending_extents(trans, extent_root);
Chris Masonf510cfe2007-10-15 16:14:48 -04001221
Chris Masone37c9e62007-05-09 20:13:14 -04001222 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001223 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001224 }
1225 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001226 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001227 }
Chris Mason26b80032007-08-08 20:17:12 -04001228
1229update_block:
Chris Mason1e2677e2007-05-29 16:52:18 -04001230 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1231 data);
Chris Masonfabb5682007-06-07 22:13:21 -04001232 BUG_ON(ret);
Chris Mason037e6392007-03-07 11:50:24 -05001233 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001234}
1235
1236/*
1237 * helper function to allocate a block for a given tree
1238 * returns the tree buffer or NULL.
1239 */
Chris Mason5f39d392007-10-15 16:14:19 -04001240struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04001241 struct btrfs_root *root,
1242 u32 blocksize, u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04001243 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05001244{
Chris Masone2fa7222007-03-12 16:22:34 -04001245 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001246 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04001247 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001248
Chris Mason4d775672007-04-20 20:23:12 -04001249 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Chris Masondb945352007-10-15 16:15:53 -04001250 blocksize, empty_size, hint,
1251 (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001252 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04001253 BUG_ON(ret > 0);
1254 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001255 }
Chris Masondb945352007-10-15 16:15:53 -04001256 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04001257 if (!buf) {
Chris Masondb945352007-10-15 16:15:53 -04001258 btrfs_free_extent(trans, root, ins.objectid, blocksize, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001259 return ERR_PTR(-ENOMEM);
1260 }
Chris Mason5f39d392007-10-15 16:14:19 -04001261 btrfs_set_buffer_uptodate(buf);
1262 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1263 buf->start + buf->len - 1, GFP_NOFS);
Chris Mason19c00dd2007-10-15 16:19:22 -04001264 set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree,
1265 buf->start, buf->start + buf->len - 1,
1266 EXTENT_CSUM, GFP_NOFS);
1267 buf->flags |= EXTENT_CSUM;
Chris Mason6b800532007-10-15 16:17:34 -04001268 btrfs_set_buffer_defrag(buf);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001269 trans->blocks_used++;
Chris Masonfec577f2007-02-26 10:40:21 -05001270 return buf;
1271}
Chris Masona28ec192007-03-06 20:08:01 -05001272
Chris Mason6407bf62007-03-27 06:33:00 -04001273static int drop_leaf_ref(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001274 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04001275{
Chris Mason5f39d392007-10-15 16:14:19 -04001276 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001277 struct btrfs_file_extent_item *fi;
1278 int i;
1279 int nritems;
1280 int ret;
1281
Chris Mason5f39d392007-10-15 16:14:19 -04001282 BUG_ON(!btrfs_is_leaf(leaf));
1283 nritems = btrfs_header_nritems(leaf);
Chris Mason6407bf62007-03-27 06:33:00 -04001284 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001285 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04001286
1287 btrfs_item_key_to_cpu(leaf, &key, i);
1288 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04001289 continue;
1290 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001291 if (btrfs_file_extent_type(leaf, fi) ==
1292 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454df2007-04-19 13:37:44 -04001293 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001294 /*
1295 * FIXME make sure to insert a trans record that
1296 * repeats the snapshot del on crash
1297 */
Chris Masondb945352007-10-15 16:15:53 -04001298 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1299 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04001300 continue;
Chris Masondb945352007-10-15 16:15:53 -04001301 ret = btrfs_free_extent(trans, root, disk_bytenr,
1302 btrfs_file_extent_disk_num_bytes(leaf, fi), 0);
Chris Mason6407bf62007-03-27 06:33:00 -04001303 BUG_ON(ret);
1304 }
1305 return 0;
1306}
1307
Chris Masone0115992007-06-19 16:23:05 -04001308static void reada_walk_down(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001309 struct extent_buffer *node)
Chris Masone0115992007-06-19 16:23:05 -04001310{
1311 int i;
1312 u32 nritems;
Chris Masondb945352007-10-15 16:15:53 -04001313 u64 bytenr;
Chris Masone0115992007-06-19 16:23:05 -04001314 int ret;
1315 u32 refs;
Chris Masondb945352007-10-15 16:15:53 -04001316 int level;
1317 u32 blocksize;
Chris Masone0115992007-06-19 16:23:05 -04001318
Chris Mason5f39d392007-10-15 16:14:19 -04001319 nritems = btrfs_header_nritems(node);
Chris Masondb945352007-10-15 16:15:53 -04001320 level = btrfs_header_level(node);
Chris Masone0115992007-06-19 16:23:05 -04001321 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001322 bytenr = btrfs_node_blockptr(node, i);
1323 blocksize = btrfs_level_size(root, level - 1);
1324 ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
Chris Masone0115992007-06-19 16:23:05 -04001325 BUG_ON(ret);
1326 if (refs != 1)
1327 continue;
Chris Mason409eb952007-08-08 20:17:12 -04001328 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -04001329 ret = readahead_tree_block(root, bytenr, blocksize);
Chris Mason409eb952007-08-08 20:17:12 -04001330 cond_resched();
1331 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04001332 if (ret)
1333 break;
1334 }
1335}
1336
Chris Mason9aca1d52007-03-13 11:09:37 -04001337/*
1338 * helper function for drop_snapshot, this walks down the tree dropping ref
1339 * counts as it goes.
1340 */
Chris Masone089f052007-03-16 16:20:31 -04001341static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1342 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001343{
Chris Mason5f39d392007-10-15 16:14:19 -04001344 struct extent_buffer *next;
1345 struct extent_buffer *cur;
Chris Masondb945352007-10-15 16:15:53 -04001346 u64 bytenr;
1347 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05001348 int ret;
1349 u32 refs;
1350
Chris Mason5caf2a02007-04-02 11:20:42 -04001351 WARN_ON(*level < 0);
1352 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason5f39d392007-10-15 16:14:19 -04001353 ret = lookup_extent_ref(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001354 path->nodes[*level]->start,
1355 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001356 BUG_ON(ret);
1357 if (refs > 1)
1358 goto out;
Chris Masone0115992007-06-19 16:23:05 -04001359
Chris Mason9aca1d52007-03-13 11:09:37 -04001360 /*
1361 * walk down to the last node level and free all the leaves
1362 */
Chris Mason6407bf62007-03-27 06:33:00 -04001363 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001364 WARN_ON(*level < 0);
1365 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001366 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04001367
1368 if (*level > 0 && path->slots[*level] == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001369 reada_walk_down(root, cur);
Chris Masone0115992007-06-19 16:23:05 -04001370
Chris Mason5f39d392007-10-15 16:14:19 -04001371 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04001372 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04001373
Chris Mason7518a232007-03-12 12:01:18 -04001374 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04001375 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05001376 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001377 if (*level == 0) {
1378 ret = drop_leaf_ref(trans, root, cur);
1379 BUG_ON(ret);
1380 break;
1381 }
Chris Masondb945352007-10-15 16:15:53 -04001382 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1383 blocksize = btrfs_level_size(root, *level - 1);
1384 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001385 BUG_ON(ret);
1386 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001387 path->slots[*level]++;
Chris Masondb945352007-10-15 16:15:53 -04001388 ret = btrfs_free_extent(trans, root, bytenr,
1389 blocksize, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001390 BUG_ON(ret);
1391 continue;
1392 }
Chris Masondb945352007-10-15 16:15:53 -04001393 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001394 if (!next || !btrfs_buffer_uptodate(next)) {
1395 free_extent_buffer(next);
Chris Masone9d0b132007-08-10 14:06:19 -04001396 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -04001397 next = read_tree_block(root, bytenr, blocksize);
Chris Masone9d0b132007-08-10 14:06:19 -04001398 mutex_lock(&root->fs_info->fs_mutex);
1399
1400 /* we dropped the lock, check one more time */
Chris Masondb945352007-10-15 16:15:53 -04001401 ret = lookup_extent_ref(trans, root, bytenr,
1402 blocksize, &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04001403 BUG_ON(ret);
1404 if (refs != 1) {
1405 path->slots[*level]++;
Chris Mason5f39d392007-10-15 16:14:19 -04001406 free_extent_buffer(next);
Chris Masone9d0b132007-08-10 14:06:19 -04001407 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001408 bytenr, blocksize, 1);
Chris Masone9d0b132007-08-10 14:06:19 -04001409 BUG_ON(ret);
1410 continue;
1411 }
1412 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001413 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001414 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04001415 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001416 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04001417 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05001418 path->slots[*level] = 0;
1419 }
1420out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001421 WARN_ON(*level < 0);
1422 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masondb945352007-10-15 16:15:53 -04001423 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
1424 path->nodes[*level]->len, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001425 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001426 path->nodes[*level] = NULL;
1427 *level += 1;
1428 BUG_ON(ret);
1429 return 0;
1430}
1431
Chris Mason9aca1d52007-03-13 11:09:37 -04001432/*
1433 * helper for dropping snapshots. This walks back up the tree in the path
1434 * to find the first node higher up where we haven't yet gone through
1435 * all the slots
1436 */
Chris Masone089f052007-03-16 16:20:31 -04001437static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1438 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001439{
1440 int i;
1441 int slot;
1442 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04001443 struct btrfs_root_item *root_item = &root->root_item;
1444
Chris Mason234b63a2007-03-13 10:46:10 -04001445 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001446 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04001447 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1448 struct extent_buffer *node;
1449 struct btrfs_disk_key disk_key;
1450 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05001451 path->slots[i]++;
1452 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04001453 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001454 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04001455 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04001456 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04001457 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05001458 return 0;
1459 } else {
Chris Masone089f052007-03-16 16:20:31 -04001460 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001461 path->nodes[*level]->start,
1462 path->nodes[*level]->len, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001463 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001464 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001465 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001466 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001467 }
1468 }
1469 return 1;
1470}
1471
Chris Mason9aca1d52007-03-13 11:09:37 -04001472/*
1473 * drop the reference count on the tree rooted at 'snap'. This traverses
1474 * the tree freeing any blocks that have a ref count of zero after being
1475 * decremented.
1476 */
Chris Masone089f052007-03-16 16:20:31 -04001477int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04001478 *root)
Chris Mason20524f02007-03-10 06:35:47 -05001479{
Chris Mason3768f362007-03-13 16:47:54 -04001480 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001481 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001482 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001483 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001484 int i;
1485 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001486 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05001487
Chris Mason5caf2a02007-04-02 11:20:42 -04001488 path = btrfs_alloc_path();
1489 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05001490
Chris Mason5f39d392007-10-15 16:14:19 -04001491 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05001492 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001493 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1494 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04001495 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04001496 path->slots[level] = 0;
1497 } else {
1498 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001499 struct btrfs_disk_key found_key;
1500 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04001501
Chris Mason9f3a7422007-08-07 15:52:19 -04001502 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04001503 level = root_item->drop_level;
1504 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001505 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04001506 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04001507 ret = wret;
1508 goto out;
1509 }
Chris Mason5f39d392007-10-15 16:14:19 -04001510 node = path->nodes[level];
1511 btrfs_node_key(node, &found_key, path->slots[level]);
1512 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1513 sizeof(found_key)));
Chris Mason9f3a7422007-08-07 15:52:19 -04001514 }
Chris Mason20524f02007-03-10 06:35:47 -05001515 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001516 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001517 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001518 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001519 if (wret < 0)
1520 ret = wret;
1521
Chris Mason5caf2a02007-04-02 11:20:42 -04001522 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001523 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001524 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001525 if (wret < 0)
1526 ret = wret;
Chris Mason409eb952007-08-08 20:17:12 -04001527 ret = -EAGAIN;
Chris Mason409eb952007-08-08 20:17:12 -04001528 break;
Chris Mason20524f02007-03-10 06:35:47 -05001529 }
Chris Mason83e15a22007-03-12 09:03:27 -04001530 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001531 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04001532 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04001533 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04001534 }
Chris Mason20524f02007-03-10 06:35:47 -05001535 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001536out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001537 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04001538 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05001539}
Chris Mason9078a3e2007-04-26 16:46:15 -04001540
Chris Masonbe744172007-05-06 10:15:01 -04001541int btrfs_free_block_groups(struct btrfs_fs_info *info)
1542{
Chris Masonf510cfe2007-10-15 16:14:48 -04001543 u64 start;
1544 u64 end;
Yanb97f9202007-11-01 11:28:41 -04001545 u64 ptr;
Chris Mason96b51792007-10-15 16:15:19 -04001546 int ret;
Chris Mason96b51792007-10-15 16:15:19 -04001547 while(1) {
1548 ret = find_first_extent_bit(&info->block_group_cache, 0,
1549 &start, &end, (unsigned int)-1);
1550 if (ret)
1551 break;
Yanb97f9202007-11-01 11:28:41 -04001552 ret = get_state_private(&info->block_group_cache, start, &ptr);
1553 if (!ret)
1554 kfree((void *)(unsigned long)ptr);
Chris Mason96b51792007-10-15 16:15:19 -04001555 clear_extent_bits(&info->block_group_cache, start,
1556 end, (unsigned int)-1, GFP_NOFS);
1557 }
Chris Masone37c9e62007-05-09 20:13:14 -04001558 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -04001559 ret = find_first_extent_bit(&info->free_space_cache, 0,
1560 &start, &end, EXTENT_DIRTY);
1561 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -04001562 break;
Chris Masonf510cfe2007-10-15 16:14:48 -04001563 clear_extent_dirty(&info->free_space_cache, start,
1564 end, GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04001565 }
Chris Masonbe744172007-05-06 10:15:01 -04001566 return 0;
1567}
1568
Chris Mason9078a3e2007-04-26 16:46:15 -04001569int btrfs_read_block_groups(struct btrfs_root *root)
1570{
1571 struct btrfs_path *path;
1572 int ret;
1573 int err = 0;
Chris Mason96b51792007-10-15 16:15:19 -04001574 int bit;
Chris Mason9078a3e2007-04-26 16:46:15 -04001575 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04001576 struct btrfs_fs_info *info = root->fs_info;
Chris Mason96b51792007-10-15 16:15:19 -04001577 struct extent_map_tree *block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04001578 struct btrfs_key key;
1579 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04001580 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04001581
1582 block_group_cache = &info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04001583
Chris Masonbe744172007-05-06 10:15:01 -04001584 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04001585 key.objectid = 0;
Chris Masondb945352007-10-15 16:15:53 -04001586 key.offset = BTRFS_BLOCK_GROUP_SIZE;
Chris Mason9078a3e2007-04-26 16:46:15 -04001587 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1588
1589 path = btrfs_alloc_path();
1590 if (!path)
1591 return -ENOMEM;
1592
1593 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001594 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04001595 &key, path, 0, 0);
1596 if (ret != 0) {
1597 err = ret;
1598 break;
1599 }
Chris Mason5f39d392007-10-15 16:14:19 -04001600 leaf = path->nodes[0];
1601 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason9078a3e2007-04-26 16:46:15 -04001602 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1603 if (!cache) {
1604 err = -1;
1605 break;
1606 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001607
Chris Mason5f39d392007-10-15 16:14:19 -04001608 read_extent_buffer(leaf, &cache->item,
1609 btrfs_item_ptr_offset(leaf, path->slots[0]),
1610 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04001611 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Masone37c9e62007-05-09 20:13:14 -04001612 cache->cached = 0;
Yan324ae4d2007-11-16 14:57:08 -05001613 cache->pinned = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001614 key.objectid = found_key.objectid + found_key.offset;
1615 btrfs_release_path(root, path);
Chris Mason5f39d392007-10-15 16:14:19 -04001616
Chris Masonf84a8b32007-11-06 10:26:29 -05001617 if (cache->item.flags & BTRFS_BLOCK_GROUP_MIXED) {
1618 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
1619 cache->data = BTRFS_BLOCK_GROUP_MIXED;
1620 } else if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
Chris Mason96b51792007-10-15 16:15:19 -04001621 bit = BLOCK_GROUP_DATA;
Chris Masonf84a8b32007-11-06 10:26:29 -05001622 cache->data = BTRFS_BLOCK_GROUP_DATA;
Chris Mason96b51792007-10-15 16:15:19 -04001623 } else {
1624 bit = BLOCK_GROUP_METADATA;
1625 cache->data = 0;
Chris Mason31f3c992007-04-30 15:25:45 -04001626 }
Chris Mason96b51792007-10-15 16:15:19 -04001627
1628 /* use EXTENT_LOCKED to prevent merging */
1629 set_extent_bits(block_group_cache, found_key.objectid,
1630 found_key.objectid + found_key.offset - 1,
1631 bit | EXTENT_LOCKED, GFP_NOFS);
1632 set_state_private(block_group_cache, found_key.objectid,
Jens Axboeae2f5412007-10-19 09:22:59 -04001633 (unsigned long)cache);
Chris Mason96b51792007-10-15 16:15:19 -04001634
Chris Mason9078a3e2007-04-26 16:46:15 -04001635 if (key.objectid >=
Chris Masondb945352007-10-15 16:15:53 -04001636 btrfs_super_total_bytes(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04001637 break;
1638 }
1639
1640 btrfs_free_path(path);
1641 return 0;
1642}