blob: 91397e989393d10b3a22814c2034a3d8af3c8514 [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
Chris Mason87ee04e2007-11-30 11:30:34 -0500935static u64 stripe_align(struct btrfs_root *root, u64 val)
936{
937 u64 mask = ((u64)root->stripesize - 1);
938 u64 ret = (val + mask) & ~mask;
939 return ret;
940}
941
Chris Masonfec577f2007-02-26 10:40:21 -0500942/*
943 * walks the btree of allocated extents and find a hole of a given size.
944 * The key ins is changed to record the hole:
945 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400946 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500947 * ins->offset == number of blocks
948 * Any available blocks before search_start are skipped.
949 */
Chris Masone089f052007-03-16 16:20:31 -0400950static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masondb945352007-10-15 16:15:53 -0400951 *orig_root, u64 num_bytes, u64 empty_size,
952 u64 search_start, u64 search_end, u64 hint_byte,
Chris Masonf2654de2007-06-26 12:20:46 -0400953 struct btrfs_key *ins, u64 exclude_start,
954 u64 exclude_nr, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500955{
Chris Mason5caf2a02007-04-02 11:20:42 -0400956 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400957 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500958 u64 hole_size = 0;
Chris Mason87ee04e2007-11-30 11:30:34 -0500959 u64 aligned;
960 int ret;
Chris Masonfec577f2007-02-26 10:40:21 -0500961 int slot = 0;
Chris Masondb945352007-10-15 16:15:53 -0400962 u64 last_byte = 0;
Chris Masonbe744172007-05-06 10:15:01 -0400963 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500964 int start_found;
Chris Mason5f39d392007-10-15 16:14:19 -0400965 struct extent_buffer *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400966 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -0400967 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -0400968 u64 total_needed = num_bytes;
Chris Masone20d96d2007-03-22 12:13:20 -0400969 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -0400970 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400971 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -0400972 int wrapped = 0;
Chris Masonf84a8b32007-11-06 10:26:29 -0500973 u64 cached_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500974
Chris Masondb945352007-10-15 16:15:53 -0400975 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -0400976 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
977
Chris Mason5f39d392007-10-15 16:14:19 -0400978 level = btrfs_header_level(root->node);
979
Chris Mason015a739c2007-11-26 16:15:16 -0800980 if (num_bytes >= 32 * 1024 * 1024 && hint_byte) {
Chris Mason257d0ce2007-11-07 21:08:16 -0500981 data = BTRFS_BLOCK_GROUP_MIXED;
982 }
983
Chris Mason3e1ad542007-05-07 20:03:49 -0400984 if (search_end == (u64)-1)
Chris Masondb945352007-10-15 16:15:53 -0400985 search_end = btrfs_super_total_bytes(&info->super_copy);
986 if (hint_byte) {
987 block_group = btrfs_lookup_block_group(info, hint_byte);
Chris Masonbe744172007-05-06 10:15:01 -0400988 block_group = btrfs_find_block_group(root, block_group,
Chris Masondb945352007-10-15 16:15:53 -0400989 hint_byte, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400990 } else {
991 block_group = btrfs_find_block_group(root,
992 trans->block_group, 0,
Chris Masonde428b62007-05-18 13:28:27 -0400993 data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400994 }
995
Chris Mason6702ed42007-08-07 16:15:09 -0400996 total_needed += empty_size;
Chris Masone0115992007-06-19 16:23:05 -0400997 path = btrfs_alloc_path();
Chris Masonbe744172007-05-06 10:15:01 -0400998check_failed:
Yan5e5745d2007-11-16 14:57:09 -0500999 search_start = find_search_start(root, &block_group, search_start,
1000 total_needed, data, full_scan);
Chris Mason87ee04e2007-11-30 11:30:34 -05001001 search_start = stripe_align(root, search_start);
Chris Masonf84a8b32007-11-06 10:26:29 -05001002 cached_start = search_start;
Chris Mason5caf2a02007-04-02 11:20:42 -04001003 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001004 ins->objectid = search_start;
1005 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001006 start_found = 0;
Chris Mason2cc58cf2007-08-27 16:49:44 -04001007 path->reada = 2;
Chris Masone37c9e62007-05-09 20:13:14 -04001008
Chris Mason5caf2a02007-04-02 11:20:42 -04001009 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -05001010 if (ret < 0)
1011 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001012
Chris Masone37c9e62007-05-09 20:13:14 -04001013 if (path->slots[0] > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001014 path->slots[0]--;
Chris Masone37c9e62007-05-09 20:13:14 -04001015 }
1016
Chris Mason5f39d392007-10-15 16:14:19 -04001017 l = path->nodes[0];
1018 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1019
Chris Masone37c9e62007-05-09 20:13:14 -04001020 /*
1021 * a rare case, go back one key if we hit a block group item
1022 * instead of an extent item
1023 */
1024 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
1025 key.objectid + key.offset >= search_start) {
1026 ins->objectid = key.objectid;
1027 ins->offset = key.offset - 1;
1028 btrfs_release_path(root, path);
1029 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
1030 if (ret < 0)
1031 goto error;
1032
1033 if (path->slots[0] > 0) {
1034 path->slots[0]--;
1035 }
1036 }
Chris Mason0579da42007-03-07 16:15:30 -05001037
Chris Masonfec577f2007-02-26 10:40:21 -05001038 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001039 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -04001040 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04001041 if (slot >= btrfs_header_nritems(l)) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001042 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001043 if (ret == 0)
1044 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -05001045 if (ret < 0)
1046 goto error;
Chris Masone19caa52007-10-15 16:17:44 -04001047
1048 search_start = max(search_start,
1049 block_group->key.objectid);
Chris Masonfec577f2007-02-26 10:40:21 -05001050 if (!start_found) {
Chris Mason87ee04e2007-11-30 11:30:34 -05001051 aligned = stripe_align(root, search_start);
1052 ins->objectid = aligned;
1053 if (aligned >= search_end) {
1054 ret = -ENOSPC;
1055 goto error;
1056 }
1057 ins->offset = search_end - aligned;
Chris Masonfec577f2007-02-26 10:40:21 -05001058 start_found = 1;
1059 goto check_pending;
1060 }
Chris Mason87ee04e2007-11-30 11:30:34 -05001061 ins->objectid = stripe_align(root,
1062 last_byte > search_start ?
1063 last_byte : search_start);
1064 if (search_end <= ins->objectid) {
1065 ret = -ENOSPC;
1066 goto error;
1067 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001068 ins->offset = search_end - ins->objectid;
Chris Masone19caa52007-10-15 16:17:44 -04001069 BUG_ON(ins->objectid >= search_end);
Chris Masonfec577f2007-02-26 10:40:21 -05001070 goto check_pending;
1071 }
Chris Mason5f39d392007-10-15 16:14:19 -04001072 btrfs_item_key_to_cpu(l, &key, slot);
Chris Mason96b51792007-10-15 16:15:19 -04001073
Chris Masondb945352007-10-15 16:15:53 -04001074 if (key.objectid >= search_start && key.objectid > last_byte &&
Chris Masone37c9e62007-05-09 20:13:14 -04001075 start_found) {
Chris Masondb945352007-10-15 16:15:53 -04001076 if (last_byte < search_start)
1077 last_byte = search_start;
Chris Mason87ee04e2007-11-30 11:30:34 -05001078 aligned = stripe_align(root, last_byte);
1079 hole_size = key.objectid - aligned;
1080 if (key.objectid > aligned && hole_size >= num_bytes) {
1081 ins->objectid = aligned;
Chris Masone37c9e62007-05-09 20:13:14 -04001082 ins->offset = hole_size;
1083 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -05001084 }
Chris Masonfec577f2007-02-26 10:40:21 -05001085 }
Chris Mason96b51792007-10-15 16:15:19 -04001086 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
1087 if (!start_found) {
Chris Masondb945352007-10-15 16:15:53 -04001088 last_byte = key.objectid;
Chris Mason96b51792007-10-15 16:15:19 -04001089 start_found = 1;
1090 }
Chris Masone37c9e62007-05-09 20:13:14 -04001091 goto next;
Chris Mason96b51792007-10-15 16:15:19 -04001092 }
1093
Chris Masone37c9e62007-05-09 20:13:14 -04001094
Chris Mason0579da42007-03-07 16:15:30 -05001095 start_found = 1;
Chris Masondb945352007-10-15 16:15:53 -04001096 last_byte = key.objectid + key.offset;
Chris Masonf510cfe2007-10-15 16:14:48 -04001097
Chris Mason257d0ce2007-11-07 21:08:16 -05001098 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
1099 last_byte >= block_group->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -04001100 block_group->key.offset) {
1101 btrfs_release_path(root, path);
1102 search_start = block_group->key.objectid +
Chris Masone19caa52007-10-15 16:17:44 -04001103 block_group->key.offset;
Chris Masonbe744172007-05-06 10:15:01 -04001104 goto new_group;
1105 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001106next:
Chris Mason5caf2a02007-04-02 11:20:42 -04001107 path->slots[0]++;
Chris Masonde428b62007-05-18 13:28:27 -04001108 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05001109 }
Chris Masonfec577f2007-02-26 10:40:21 -05001110check_pending:
1111 /* we have to make sure we didn't find an extent that has already
1112 * been allocated by the map tree or the original allocation
1113 */
Chris Mason5caf2a02007-04-02 11:20:42 -04001114 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001115 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -04001116
Chris Masondb945352007-10-15 16:15:53 -04001117 if (ins->objectid + num_bytes >= search_end)
Chris Masoncf675822007-09-17 11:00:51 -04001118 goto enospc;
Chris Mason257d0ce2007-11-07 21:08:16 -05001119 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
Yan5cf66422007-11-16 14:57:09 -05001120 ins->objectid + num_bytes > block_group->
Chris Masone19caa52007-10-15 16:17:44 -04001121 key.objectid + block_group->key.offset) {
1122 search_start = block_group->key.objectid +
1123 block_group->key.offset;
1124 goto new_group;
1125 }
Chris Mason1a5bc162007-10-15 16:15:26 -04001126 if (test_range_bit(&info->extent_ins, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001127 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1128 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001129 goto new_group;
1130 }
1131 if (test_range_bit(&info->pinned_extents, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001132 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1133 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001134 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001135 }
Chris Masondb945352007-10-15 16:15:53 -04001136 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
Chris Masonf2654de2007-06-26 12:20:46 -04001137 ins->objectid < exclude_start + exclude_nr)) {
1138 search_start = exclude_start + exclude_nr;
1139 goto new_group;
1140 }
Chris Masone37c9e62007-05-09 20:13:14 -04001141 if (!data) {
Chris Mason5276aed2007-06-11 21:33:38 -04001142 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Mason26b80032007-08-08 20:17:12 -04001143 if (block_group)
1144 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -04001145 }
Chris Masondb945352007-10-15 16:15:53 -04001146 ins->offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04001147 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001148 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001149
1150new_group:
Chris Masondb945352007-10-15 16:15:53 -04001151 if (search_start + num_bytes >= search_end) {
Chris Masoncf675822007-09-17 11:00:51 -04001152enospc:
Chris Masonbe744172007-05-06 10:15:01 -04001153 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001154 if (full_scan) {
1155 ret = -ENOSPC;
1156 goto error;
1157 }
Chris Mason6702ed42007-08-07 16:15:09 -04001158 if (wrapped) {
1159 if (!full_scan)
1160 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001161 full_scan = 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001162 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001163 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001164 }
Chris Mason5276aed2007-06-11 21:33:38 -04001165 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001166 cond_resched();
Chris Masonbe744172007-05-06 10:15:01 -04001167 if (!full_scan)
1168 block_group = btrfs_find_block_group(root, block_group,
Chris Masonde428b62007-05-18 13:28:27 -04001169 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001170 goto check_failed;
1171
Chris Mason0f70abe2007-02-28 16:46:22 -05001172error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001173 btrfs_release_path(root, path);
1174 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001175 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001176}
Chris Masonfec577f2007-02-26 10:40:21 -05001177/*
Chris Masonfec577f2007-02-26 10:40:21 -05001178 * finds a free extent and does all the dirty work required for allocation
1179 * returns the key for the extent through ins, and a tree buffer for
1180 * the first block of the extent through buf.
1181 *
1182 * returns 0 if everything worked, non-zero otherwise.
1183 */
Chris Mason4d775672007-04-20 20:23:12 -04001184int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1185 struct btrfs_root *root, u64 owner,
Chris Masondb945352007-10-15 16:15:53 -04001186 u64 num_bytes, u64 empty_size, u64 hint_byte,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001187 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001188{
1189 int ret;
1190 int pending_ret;
Chris Masondb945352007-10-15 16:15:53 -04001191 u64 super_used, root_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001192 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04001193 struct btrfs_fs_info *info = root->fs_info;
1194 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001195 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001196
Chris Mason5f39d392007-10-15 16:14:19 -04001197 btrfs_set_stack_extent_refs(&extent_item, 1);
1198 btrfs_set_stack_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -05001199
Chris Masondb945352007-10-15 16:15:53 -04001200 WARN_ON(num_bytes < root->sectorsize);
1201 ret = find_free_extent(trans, root, num_bytes, empty_size,
1202 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04001203 trans->alloc_exclude_start,
1204 trans->alloc_exclude_nr, data);
Chris Masonccd467d2007-06-28 15:57:36 -04001205 BUG_ON(ret);
Chris Masonf2654de2007-06-26 12:20:46 -04001206 if (ret)
1207 return ret;
1208
Josef Bacik58176a92007-08-29 15:47:34 -04001209 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -04001210 super_used = btrfs_super_bytes_used(&info->super_copy);
1211 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Mason26b80032007-08-08 20:17:12 -04001212
Josef Bacik58176a92007-08-29 15:47:34 -04001213 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001214 root_used = btrfs_root_used(&root->root_item);
1215 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001216
Chris Masonf510cfe2007-10-15 16:14:48 -04001217 clear_extent_dirty(&root->fs_info->free_space_cache,
1218 ins->objectid, ins->objectid + ins->offset - 1,
1219 GFP_NOFS);
1220
Chris Mason26b80032007-08-08 20:17:12 -04001221 if (root == extent_root) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001222 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1223 ins->objectid + ins->offset - 1,
1224 EXTENT_LOCKED, GFP_NOFS);
Chris Masone19caa52007-10-15 16:17:44 -04001225 WARN_ON(data == 1);
Chris Mason26b80032007-08-08 20:17:12 -04001226 goto update_block;
1227 }
1228
1229 WARN_ON(trans->alloc_exclude_nr);
1230 trans->alloc_exclude_start = ins->objectid;
1231 trans->alloc_exclude_nr = ins->offset;
Chris Masone089f052007-03-16 16:20:31 -04001232 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1233 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -05001234
Chris Mason26b80032007-08-08 20:17:12 -04001235 trans->alloc_exclude_start = 0;
1236 trans->alloc_exclude_nr = 0;
1237
Chris Masonccd467d2007-06-28 15:57:36 -04001238 BUG_ON(ret);
Chris Masone089f052007-03-16 16:20:31 -04001239 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001240 pending_ret = del_pending_extents(trans, extent_root);
Chris Masonf510cfe2007-10-15 16:14:48 -04001241
Chris Masone37c9e62007-05-09 20:13:14 -04001242 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001243 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001244 }
1245 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001246 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001247 }
Chris Mason26b80032007-08-08 20:17:12 -04001248
1249update_block:
Chris Mason1e2677e2007-05-29 16:52:18 -04001250 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1251 data);
Chris Masonfabb5682007-06-07 22:13:21 -04001252 BUG_ON(ret);
Chris Mason037e6392007-03-07 11:50:24 -05001253 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001254}
1255
1256/*
1257 * helper function to allocate a block for a given tree
1258 * returns the tree buffer or NULL.
1259 */
Chris Mason5f39d392007-10-15 16:14:19 -04001260struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04001261 struct btrfs_root *root,
1262 u32 blocksize, u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04001263 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05001264{
Chris Masone2fa7222007-03-12 16:22:34 -04001265 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001266 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04001267 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001268
Chris Mason4d775672007-04-20 20:23:12 -04001269 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Chris Masondb945352007-10-15 16:15:53 -04001270 blocksize, empty_size, hint,
1271 (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001272 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04001273 BUG_ON(ret > 0);
1274 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001275 }
Chris Masondb945352007-10-15 16:15:53 -04001276 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04001277 if (!buf) {
Chris Masondb945352007-10-15 16:15:53 -04001278 btrfs_free_extent(trans, root, ins.objectid, blocksize, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001279 return ERR_PTR(-ENOMEM);
1280 }
Chris Mason5f39d392007-10-15 16:14:19 -04001281 btrfs_set_buffer_uptodate(buf);
1282 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1283 buf->start + buf->len - 1, GFP_NOFS);
Chris Mason19c00dd2007-10-15 16:19:22 -04001284 set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree,
1285 buf->start, buf->start + buf->len - 1,
1286 EXTENT_CSUM, GFP_NOFS);
1287 buf->flags |= EXTENT_CSUM;
Chris Mason6b800532007-10-15 16:17:34 -04001288 btrfs_set_buffer_defrag(buf);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001289 trans->blocks_used++;
Chris Masonfec577f2007-02-26 10:40:21 -05001290 return buf;
1291}
Chris Masona28ec192007-03-06 20:08:01 -05001292
Chris Mason6407bf62007-03-27 06:33:00 -04001293static int drop_leaf_ref(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001294 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04001295{
Chris Mason5f39d392007-10-15 16:14:19 -04001296 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001297 struct btrfs_file_extent_item *fi;
1298 int i;
1299 int nritems;
1300 int ret;
1301
Chris Mason5f39d392007-10-15 16:14:19 -04001302 BUG_ON(!btrfs_is_leaf(leaf));
1303 nritems = btrfs_header_nritems(leaf);
Chris Mason6407bf62007-03-27 06:33:00 -04001304 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001305 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04001306
1307 btrfs_item_key_to_cpu(leaf, &key, i);
1308 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04001309 continue;
1310 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001311 if (btrfs_file_extent_type(leaf, fi) ==
1312 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454df2007-04-19 13:37:44 -04001313 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001314 /*
1315 * FIXME make sure to insert a trans record that
1316 * repeats the snapshot del on crash
1317 */
Chris Masondb945352007-10-15 16:15:53 -04001318 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1319 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04001320 continue;
Chris Masondb945352007-10-15 16:15:53 -04001321 ret = btrfs_free_extent(trans, root, disk_bytenr,
1322 btrfs_file_extent_disk_num_bytes(leaf, fi), 0);
Chris Mason6407bf62007-03-27 06:33:00 -04001323 BUG_ON(ret);
1324 }
1325 return 0;
1326}
1327
Chris Masone0115992007-06-19 16:23:05 -04001328static void reada_walk_down(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001329 struct extent_buffer *node)
Chris Masone0115992007-06-19 16:23:05 -04001330{
1331 int i;
1332 u32 nritems;
Chris Masondb945352007-10-15 16:15:53 -04001333 u64 bytenr;
Chris Masone0115992007-06-19 16:23:05 -04001334 int ret;
1335 u32 refs;
Chris Masondb945352007-10-15 16:15:53 -04001336 int level;
1337 u32 blocksize;
Chris Masone0115992007-06-19 16:23:05 -04001338
Chris Mason5f39d392007-10-15 16:14:19 -04001339 nritems = btrfs_header_nritems(node);
Chris Masondb945352007-10-15 16:15:53 -04001340 level = btrfs_header_level(node);
Chris Masone0115992007-06-19 16:23:05 -04001341 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001342 bytenr = btrfs_node_blockptr(node, i);
1343 blocksize = btrfs_level_size(root, level - 1);
1344 ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
Chris Masone0115992007-06-19 16:23:05 -04001345 BUG_ON(ret);
1346 if (refs != 1)
1347 continue;
Chris Mason409eb952007-08-08 20:17:12 -04001348 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -04001349 ret = readahead_tree_block(root, bytenr, blocksize);
Chris Mason409eb952007-08-08 20:17:12 -04001350 cond_resched();
1351 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04001352 if (ret)
1353 break;
1354 }
1355}
1356
Chris Mason9aca1d52007-03-13 11:09:37 -04001357/*
1358 * helper function for drop_snapshot, this walks down the tree dropping ref
1359 * counts as it goes.
1360 */
Chris Masone089f052007-03-16 16:20:31 -04001361static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1362 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001363{
Chris Mason5f39d392007-10-15 16:14:19 -04001364 struct extent_buffer *next;
1365 struct extent_buffer *cur;
Chris Masondb945352007-10-15 16:15:53 -04001366 u64 bytenr;
1367 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05001368 int ret;
1369 u32 refs;
1370
Chris Mason5caf2a02007-04-02 11:20:42 -04001371 WARN_ON(*level < 0);
1372 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason5f39d392007-10-15 16:14:19 -04001373 ret = lookup_extent_ref(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001374 path->nodes[*level]->start,
1375 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001376 BUG_ON(ret);
1377 if (refs > 1)
1378 goto out;
Chris Masone0115992007-06-19 16:23:05 -04001379
Chris Mason9aca1d52007-03-13 11:09:37 -04001380 /*
1381 * walk down to the last node level and free all the leaves
1382 */
Chris Mason6407bf62007-03-27 06:33:00 -04001383 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001384 WARN_ON(*level < 0);
1385 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001386 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04001387
1388 if (*level > 0 && path->slots[*level] == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001389 reada_walk_down(root, cur);
Chris Masone0115992007-06-19 16:23:05 -04001390
Chris Mason5f39d392007-10-15 16:14:19 -04001391 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04001392 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04001393
Chris Mason7518a232007-03-12 12:01:18 -04001394 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04001395 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05001396 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001397 if (*level == 0) {
1398 ret = drop_leaf_ref(trans, root, cur);
1399 BUG_ON(ret);
1400 break;
1401 }
Chris Masondb945352007-10-15 16:15:53 -04001402 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1403 blocksize = btrfs_level_size(root, *level - 1);
1404 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001405 BUG_ON(ret);
1406 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001407 path->slots[*level]++;
Chris Masondb945352007-10-15 16:15:53 -04001408 ret = btrfs_free_extent(trans, root, bytenr,
1409 blocksize, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001410 BUG_ON(ret);
1411 continue;
1412 }
Chris Masondb945352007-10-15 16:15:53 -04001413 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001414 if (!next || !btrfs_buffer_uptodate(next)) {
1415 free_extent_buffer(next);
Chris Masone9d0b132007-08-10 14:06:19 -04001416 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -04001417 next = read_tree_block(root, bytenr, blocksize);
Chris Masone9d0b132007-08-10 14:06:19 -04001418 mutex_lock(&root->fs_info->fs_mutex);
1419
1420 /* we dropped the lock, check one more time */
Chris Masondb945352007-10-15 16:15:53 -04001421 ret = lookup_extent_ref(trans, root, bytenr,
1422 blocksize, &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04001423 BUG_ON(ret);
1424 if (refs != 1) {
1425 path->slots[*level]++;
Chris Mason5f39d392007-10-15 16:14:19 -04001426 free_extent_buffer(next);
Chris Masone9d0b132007-08-10 14:06:19 -04001427 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001428 bytenr, blocksize, 1);
Chris Masone9d0b132007-08-10 14:06:19 -04001429 BUG_ON(ret);
1430 continue;
1431 }
1432 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001433 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001434 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04001435 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001436 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04001437 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05001438 path->slots[*level] = 0;
1439 }
1440out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001441 WARN_ON(*level < 0);
1442 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masondb945352007-10-15 16:15:53 -04001443 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
1444 path->nodes[*level]->len, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001445 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001446 path->nodes[*level] = NULL;
1447 *level += 1;
1448 BUG_ON(ret);
1449 return 0;
1450}
1451
Chris Mason9aca1d52007-03-13 11:09:37 -04001452/*
1453 * helper for dropping snapshots. This walks back up the tree in the path
1454 * to find the first node higher up where we haven't yet gone through
1455 * all the slots
1456 */
Chris Masone089f052007-03-16 16:20:31 -04001457static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1458 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001459{
1460 int i;
1461 int slot;
1462 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04001463 struct btrfs_root_item *root_item = &root->root_item;
1464
Chris Mason234b63a2007-03-13 10:46:10 -04001465 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001466 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04001467 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1468 struct extent_buffer *node;
1469 struct btrfs_disk_key disk_key;
1470 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05001471 path->slots[i]++;
1472 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04001473 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001474 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04001475 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04001476 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04001477 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05001478 return 0;
1479 } else {
Chris Masone089f052007-03-16 16:20:31 -04001480 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04001481 path->nodes[*level]->start,
1482 path->nodes[*level]->len, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001483 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001484 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001485 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001486 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001487 }
1488 }
1489 return 1;
1490}
1491
Chris Mason9aca1d52007-03-13 11:09:37 -04001492/*
1493 * drop the reference count on the tree rooted at 'snap'. This traverses
1494 * the tree freeing any blocks that have a ref count of zero after being
1495 * decremented.
1496 */
Chris Masone089f052007-03-16 16:20:31 -04001497int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04001498 *root)
Chris Mason20524f02007-03-10 06:35:47 -05001499{
Chris Mason3768f362007-03-13 16:47:54 -04001500 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001501 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001502 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001503 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001504 int i;
1505 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001506 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05001507
Chris Mason5caf2a02007-04-02 11:20:42 -04001508 path = btrfs_alloc_path();
1509 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05001510
Chris Mason5f39d392007-10-15 16:14:19 -04001511 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05001512 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001513 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1514 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04001515 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04001516 path->slots[level] = 0;
1517 } else {
1518 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001519 struct btrfs_disk_key found_key;
1520 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04001521
Chris Mason9f3a7422007-08-07 15:52:19 -04001522 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04001523 level = root_item->drop_level;
1524 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04001525 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04001526 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04001527 ret = wret;
1528 goto out;
1529 }
Chris Mason5f39d392007-10-15 16:14:19 -04001530 node = path->nodes[level];
1531 btrfs_node_key(node, &found_key, path->slots[level]);
1532 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1533 sizeof(found_key)));
Chris Mason9f3a7422007-08-07 15:52:19 -04001534 }
Chris Mason20524f02007-03-10 06:35:47 -05001535 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001536 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001537 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001538 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001539 if (wret < 0)
1540 ret = wret;
1541
Chris Mason5caf2a02007-04-02 11:20:42 -04001542 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001543 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001544 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001545 if (wret < 0)
1546 ret = wret;
Chris Mason409eb952007-08-08 20:17:12 -04001547 ret = -EAGAIN;
Chris Mason409eb952007-08-08 20:17:12 -04001548 break;
Chris Mason20524f02007-03-10 06:35:47 -05001549 }
Chris Mason83e15a22007-03-12 09:03:27 -04001550 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001551 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04001552 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04001553 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04001554 }
Chris Mason20524f02007-03-10 06:35:47 -05001555 }
Chris Mason9f3a7422007-08-07 15:52:19 -04001556out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001557 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04001558 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05001559}
Chris Mason9078a3e2007-04-26 16:46:15 -04001560
Chris Masonbe744172007-05-06 10:15:01 -04001561int btrfs_free_block_groups(struct btrfs_fs_info *info)
1562{
Chris Masonf510cfe2007-10-15 16:14:48 -04001563 u64 start;
1564 u64 end;
Yanb97f9202007-11-01 11:28:41 -04001565 u64 ptr;
Chris Mason96b51792007-10-15 16:15:19 -04001566 int ret;
Chris Mason96b51792007-10-15 16:15:19 -04001567 while(1) {
1568 ret = find_first_extent_bit(&info->block_group_cache, 0,
1569 &start, &end, (unsigned int)-1);
1570 if (ret)
1571 break;
Yanb97f9202007-11-01 11:28:41 -04001572 ret = get_state_private(&info->block_group_cache, start, &ptr);
1573 if (!ret)
1574 kfree((void *)(unsigned long)ptr);
Chris Mason96b51792007-10-15 16:15:19 -04001575 clear_extent_bits(&info->block_group_cache, start,
1576 end, (unsigned int)-1, GFP_NOFS);
1577 }
Chris Masone37c9e62007-05-09 20:13:14 -04001578 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -04001579 ret = find_first_extent_bit(&info->free_space_cache, 0,
1580 &start, &end, EXTENT_DIRTY);
1581 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -04001582 break;
Chris Masonf510cfe2007-10-15 16:14:48 -04001583 clear_extent_dirty(&info->free_space_cache, start,
1584 end, GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04001585 }
Chris Masonbe744172007-05-06 10:15:01 -04001586 return 0;
1587}
1588
Chris Mason9078a3e2007-04-26 16:46:15 -04001589int btrfs_read_block_groups(struct btrfs_root *root)
1590{
1591 struct btrfs_path *path;
1592 int ret;
1593 int err = 0;
Chris Mason96b51792007-10-15 16:15:19 -04001594 int bit;
Chris Mason9078a3e2007-04-26 16:46:15 -04001595 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04001596 struct btrfs_fs_info *info = root->fs_info;
Chris Mason96b51792007-10-15 16:15:19 -04001597 struct extent_map_tree *block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04001598 struct btrfs_key key;
1599 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04001600 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04001601
1602 block_group_cache = &info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04001603
Chris Masonbe744172007-05-06 10:15:01 -04001604 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04001605 key.objectid = 0;
Chris Masondb945352007-10-15 16:15:53 -04001606 key.offset = BTRFS_BLOCK_GROUP_SIZE;
Chris Mason9078a3e2007-04-26 16:46:15 -04001607 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1608
1609 path = btrfs_alloc_path();
1610 if (!path)
1611 return -ENOMEM;
1612
1613 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001614 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04001615 &key, path, 0, 0);
1616 if (ret != 0) {
1617 err = ret;
1618 break;
1619 }
Chris Mason5f39d392007-10-15 16:14:19 -04001620 leaf = path->nodes[0];
1621 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason9078a3e2007-04-26 16:46:15 -04001622 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1623 if (!cache) {
1624 err = -1;
1625 break;
1626 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001627
Chris Mason5f39d392007-10-15 16:14:19 -04001628 read_extent_buffer(leaf, &cache->item,
1629 btrfs_item_ptr_offset(leaf, path->slots[0]),
1630 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04001631 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Masone37c9e62007-05-09 20:13:14 -04001632 cache->cached = 0;
Yan324ae4d2007-11-16 14:57:08 -05001633 cache->pinned = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001634 key.objectid = found_key.objectid + found_key.offset;
1635 btrfs_release_path(root, path);
Chris Mason5f39d392007-10-15 16:14:19 -04001636
Chris Masonf84a8b32007-11-06 10:26:29 -05001637 if (cache->item.flags & BTRFS_BLOCK_GROUP_MIXED) {
1638 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
1639 cache->data = BTRFS_BLOCK_GROUP_MIXED;
1640 } else if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
Chris Mason96b51792007-10-15 16:15:19 -04001641 bit = BLOCK_GROUP_DATA;
Chris Masonf84a8b32007-11-06 10:26:29 -05001642 cache->data = BTRFS_BLOCK_GROUP_DATA;
Chris Mason96b51792007-10-15 16:15:19 -04001643 } else {
1644 bit = BLOCK_GROUP_METADATA;
1645 cache->data = 0;
Chris Mason31f3c992007-04-30 15:25:45 -04001646 }
Chris Mason96b51792007-10-15 16:15:19 -04001647
1648 /* use EXTENT_LOCKED to prevent merging */
1649 set_extent_bits(block_group_cache, found_key.objectid,
1650 found_key.objectid + found_key.offset - 1,
1651 bit | EXTENT_LOCKED, GFP_NOFS);
1652 set_state_private(block_group_cache, found_key.objectid,
Jens Axboeae2f5412007-10-19 09:22:59 -04001653 (unsigned long)cache);
Chris Mason96b51792007-10-15 16:15:19 -04001654
Chris Mason9078a3e2007-04-26 16:46:15 -04001655 if (key.objectid >=
Chris Masondb945352007-10-15 16:15:53 -04001656 btrfs_super_total_bytes(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04001657 break;
1658 }
1659
1660 btrfs_free_path(path);
1661 return 0;
1662}