blob: 7e40c516fe6299afb3864a5dc2b9e06e4d990e12 [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 */
Zach Brownec6b9102007-07-11 10:00:37 -040018#include <linux/sched.h>
Chris Masonedbd8d42007-12-21 16:27:24 -050019#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -040020#include <linux/writeback.h>
Chris Mason74493f72007-12-11 09:25:06 -050021#include "hash.h"
Miguela5eb62e2008-04-11 15:45:51 -040022#include "crc32c.h"
Chris Masonfec577f2007-02-26 10:40:21 -050023#include "ctree.h"
24#include "disk-io.h"
25#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040026#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040027#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040028#include "locking.h"
Chris Masonfec577f2007-02-26 10:40:21 -050029
Chris Mason0b86a832008-03-24 15:01:56 -040030#define BLOCK_GROUP_DATA EXTENT_WRITEBACK
Chris Mason96b51792007-10-15 16:15:19 -040031#define BLOCK_GROUP_METADATA EXTENT_UPTODATE
Chris Mason0b86a832008-03-24 15:01:56 -040032#define BLOCK_GROUP_SYSTEM EXTENT_NEW
33
Chris Mason96b51792007-10-15 16:15:19 -040034#define BLOCK_GROUP_DIRTY EXTENT_DIRTY
35
Chris Masone089f052007-03-16 16:20:31 -040036static int finish_current_insert(struct btrfs_trans_handle *trans, struct
37 btrfs_root *extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -040038static int del_pending_extents(struct btrfs_trans_handle *trans, struct
39 btrfs_root *extent_root);
Chris Mason925baed2008-06-25 16:01:30 -040040static struct btrfs_block_group_cache *
41__btrfs_find_block_group(struct btrfs_root *root,
42 struct btrfs_block_group_cache *hint,
43 u64 search_start, int data, int owner);
Yand548ee52008-01-03 13:56:30 -050044
Chris Mason925baed2008-06-25 16:01:30 -040045void maybe_lock_mutex(struct btrfs_root *root)
46{
47 if (root != root->fs_info->extent_root &&
48 root != root->fs_info->chunk_root &&
49 root != root->fs_info->dev_root) {
50 mutex_lock(&root->fs_info->alloc_mutex);
51 }
52}
53
54void maybe_unlock_mutex(struct btrfs_root *root)
55{
56 if (root != root->fs_info->extent_root &&
57 root != root->fs_info->chunk_root &&
58 root != root->fs_info->dev_root) {
59 mutex_unlock(&root->fs_info->alloc_mutex);
60 }
61}
Chris Masonfec577f2007-02-26 10:40:21 -050062
Chris Masone37c9e62007-05-09 20:13:14 -040063static int cache_block_group(struct btrfs_root *root,
64 struct btrfs_block_group_cache *block_group)
65{
66 struct btrfs_path *path;
67 int ret;
68 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -040069 struct extent_buffer *leaf;
Chris Masond1310b22008-01-24 16:13:08 -050070 struct extent_io_tree *free_space_cache;
Chris Masone37c9e62007-05-09 20:13:14 -040071 int slot;
Chris Masone37c9e62007-05-09 20:13:14 -040072 u64 last = 0;
73 u64 hole_size;
Yan7d7d6062007-09-14 16:15:28 -040074 u64 first_free;
Chris Masone37c9e62007-05-09 20:13:14 -040075 int found = 0;
76
Chris Mason00f5c792007-11-30 10:09:33 -050077 if (!block_group)
78 return 0;
79
Chris Masone37c9e62007-05-09 20:13:14 -040080 root = root->fs_info->extent_root;
Chris Masonf510cfe2007-10-15 16:14:48 -040081 free_space_cache = &root->fs_info->free_space_cache;
Chris Masone37c9e62007-05-09 20:13:14 -040082
83 if (block_group->cached)
84 return 0;
Chris Masonf510cfe2007-10-15 16:14:48 -040085
Chris Masone37c9e62007-05-09 20:13:14 -040086 path = btrfs_alloc_path();
87 if (!path)
88 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -040089
Chris Mason2cc58cf2007-08-27 16:49:44 -040090 path->reada = 2;
Chris Mason925baed2008-06-25 16:01:30 -040091 path->skip_locking = 1;
Yan7d7d6062007-09-14 16:15:28 -040092 first_free = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -040093 key.objectid = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -040094 key.offset = 0;
95 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
96 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
97 if (ret < 0)
98 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -040099 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
Yand548ee52008-01-03 13:56:30 -0500100 if (ret < 0)
101 return ret;
102 if (ret == 0) {
103 leaf = path->nodes[0];
104 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
105 if (key.objectid + key.offset > first_free)
106 first_free = key.objectid + key.offset;
107 }
Chris Masone37c9e62007-05-09 20:13:14 -0400108 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400109 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -0400110 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400111 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400112 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400113 if (ret < 0)
114 goto err;
Chris Masonde428b62007-05-18 13:28:27 -0400115 if (ret == 0) {
Chris Masone37c9e62007-05-09 20:13:14 -0400116 continue;
Chris Masonde428b62007-05-18 13:28:27 -0400117 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400118 break;
119 }
120 }
Chris Mason5f39d392007-10-15 16:14:19 -0400121 btrfs_item_key_to_cpu(leaf, &key, slot);
Yan7d7d6062007-09-14 16:15:28 -0400122 if (key.objectid < block_group->key.objectid) {
Yan7d7d6062007-09-14 16:15:28 -0400123 goto next;
124 }
Chris Masone37c9e62007-05-09 20:13:14 -0400125 if (key.objectid >= block_group->key.objectid +
126 block_group->key.offset) {
Yan7d7d6062007-09-14 16:15:28 -0400127 break;
128 }
129
130 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
131 if (!found) {
132 last = first_free;
133 found = 1;
Chris Masone37c9e62007-05-09 20:13:14 -0400134 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400135 if (key.objectid > last) {
136 hole_size = key.objectid - last;
137 set_extent_dirty(free_space_cache, last,
138 last + hole_size - 1,
139 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -0400140 }
Yan7d7d6062007-09-14 16:15:28 -0400141 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400142 }
Yan7d7d6062007-09-14 16:15:28 -0400143next:
Chris Masone37c9e62007-05-09 20:13:14 -0400144 path->slots[0]++;
145 }
146
Yan7d7d6062007-09-14 16:15:28 -0400147 if (!found)
148 last = first_free;
149 if (block_group->key.objectid +
150 block_group->key.offset > last) {
151 hole_size = block_group->key.objectid +
152 block_group->key.offset - last;
Chris Masonf510cfe2007-10-15 16:14:48 -0400153 set_extent_dirty(free_space_cache, last,
154 last + hole_size - 1, GFP_NOFS);
Yan7d7d6062007-09-14 16:15:28 -0400155 }
Chris Masone37c9e62007-05-09 20:13:14 -0400156 block_group->cached = 1;
Chris Mason54aa1f42007-06-22 14:16:25 -0400157err:
Chris Masone37c9e62007-05-09 20:13:14 -0400158 btrfs_free_path(path);
159 return 0;
160}
161
Chris Mason0ef3e662008-05-24 14:04:53 -0400162struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
163 btrfs_fs_info *info,
164 u64 bytenr)
165{
166 struct extent_io_tree *block_group_cache;
167 struct btrfs_block_group_cache *block_group = NULL;
168 u64 ptr;
169 u64 start;
170 u64 end;
171 int ret;
172
173 bytenr = max_t(u64, bytenr,
174 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
175 block_group_cache = &info->block_group_cache;
176 ret = find_first_extent_bit(block_group_cache,
177 bytenr, &start, &end,
178 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
179 BLOCK_GROUP_SYSTEM);
180 if (ret) {
181 return NULL;
182 }
183 ret = get_state_private(block_group_cache, start, &ptr);
184 if (ret)
185 return NULL;
186
187 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
188 return block_group;
189}
190
Chris Mason5276aed2007-06-11 21:33:38 -0400191struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
192 btrfs_fs_info *info,
Chris Masondb945352007-10-15 16:15:53 -0400193 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400194{
Chris Masond1310b22008-01-24 16:13:08 -0500195 struct extent_io_tree *block_group_cache;
Chris Mason96b51792007-10-15 16:15:19 -0400196 struct btrfs_block_group_cache *block_group = NULL;
197 u64 ptr;
198 u64 start;
199 u64 end;
Chris Masonbe744172007-05-06 10:15:01 -0400200 int ret;
201
Chris Masona061fc82008-05-07 11:43:44 -0400202 bytenr = max_t(u64, bytenr,
203 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
Chris Mason96b51792007-10-15 16:15:19 -0400204 block_group_cache = &info->block_group_cache;
205 ret = find_first_extent_bit(block_group_cache,
Chris Masondb945352007-10-15 16:15:53 -0400206 bytenr, &start, &end,
Chris Mason0b86a832008-03-24 15:01:56 -0400207 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
208 BLOCK_GROUP_SYSTEM);
Chris Masonbe744172007-05-06 10:15:01 -0400209 if (ret) {
Chris Mason96b51792007-10-15 16:15:19 -0400210 return NULL;
Chris Masonbe744172007-05-06 10:15:01 -0400211 }
Chris Mason96b51792007-10-15 16:15:19 -0400212 ret = get_state_private(block_group_cache, start, &ptr);
213 if (ret)
214 return NULL;
215
Jens Axboeae2f5412007-10-19 09:22:59 -0400216 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Yan5cf66422007-11-16 14:57:09 -0500217 if (block_group->key.objectid <= bytenr && bytenr <
Chris Mason96b51792007-10-15 16:15:19 -0400218 block_group->key.objectid + block_group->key.offset)
219 return block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400220 return NULL;
221}
Chris Mason0b86a832008-03-24 15:01:56 -0400222
223static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
224{
Chris Mason593060d2008-03-25 16:50:33 -0400225 return (cache->flags & bits) == bits;
Chris Mason0b86a832008-03-24 15:01:56 -0400226}
227
228static int noinline find_search_start(struct btrfs_root *root,
Chris Mason98ed5172008-01-03 10:01:48 -0500229 struct btrfs_block_group_cache **cache_ret,
Chris Mason0ef3e662008-05-24 14:04:53 -0400230 u64 *start_ret, u64 num, int data)
Chris Masone37c9e62007-05-09 20:13:14 -0400231{
Chris Masone37c9e62007-05-09 20:13:14 -0400232 int ret;
233 struct btrfs_block_group_cache *cache = *cache_ret;
Chris Masond7fc6402008-02-18 12:12:38 -0500234 struct extent_io_tree *free_space_cache;
Chris Mason7d1660d2008-03-24 15:02:03 -0400235 struct extent_state *state;
Chris Masone19caa52007-10-15 16:17:44 -0400236 u64 last;
Chris Masonf510cfe2007-10-15 16:14:48 -0400237 u64 start = 0;
Chris Mason257d0ce2007-11-07 21:08:16 -0500238 u64 cache_miss = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500239 u64 total_fs_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -0400240 u64 search_start = *start_ret;
Chris Masonf84a8b32007-11-06 10:26:29 -0500241 int wrapped = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400242
Chris Masonc31f8832008-01-08 15:46:31 -0500243 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masond7fc6402008-02-18 12:12:38 -0500244 free_space_cache = &root->fs_info->free_space_cache;
245
Chris Mason0ef3e662008-05-24 14:04:53 -0400246 if (!cache)
Chris Mason54aa1f42007-06-22 14:16:25 -0400247 goto out;
Chris Masonf84a8b32007-11-06 10:26:29 -0500248
Chris Mason0ef3e662008-05-24 14:04:53 -0400249again:
250 ret = cache_block_group(root, cache);
251 if (ret) {
252 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400253 }
Chris Masone19caa52007-10-15 16:17:44 -0400254
Chris Mason0ef3e662008-05-24 14:04:53 -0400255 last = max(search_start, cache->key.objectid);
256 if (!block_group_bits(cache, data) || cache->ro)
257 goto new_group;
258
Chris Mason7d1660d2008-03-24 15:02:03 -0400259 spin_lock_irq(&free_space_cache->lock);
260 state = find_first_extent_bit_state(free_space_cache, last, EXTENT_DIRTY);
Chris Masone37c9e62007-05-09 20:13:14 -0400261 while(1) {
Chris Mason7d1660d2008-03-24 15:02:03 -0400262 if (!state) {
Chris Mason257d0ce2007-11-07 21:08:16 -0500263 if (!cache_miss)
264 cache_miss = last;
Chris Mason7d1660d2008-03-24 15:02:03 -0400265 spin_unlock_irq(&free_space_cache->lock);
Chris Masone19caa52007-10-15 16:17:44 -0400266 goto new_group;
267 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400268
Chris Mason7d1660d2008-03-24 15:02:03 -0400269 start = max(last, state->start);
270 last = state->end + 1;
Chris Mason257d0ce2007-11-07 21:08:16 -0500271 if (last - start < num) {
Chris Mason7d1660d2008-03-24 15:02:03 -0400272 do {
273 state = extent_state_next(state);
274 } while(state && !(state->state & EXTENT_DIRTY));
Chris Masonf510cfe2007-10-15 16:14:48 -0400275 continue;
Chris Mason257d0ce2007-11-07 21:08:16 -0500276 }
Chris Mason7d1660d2008-03-24 15:02:03 -0400277 spin_unlock_irq(&free_space_cache->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -0400278 if (cache->ro) {
Chris Mason8f18cf12008-04-25 16:53:30 -0400279 goto new_group;
Chris Mason0ef3e662008-05-24 14:04:53 -0400280 }
Chris Mason0b86a832008-03-24 15:01:56 -0400281 if (start + num > cache->key.objectid + cache->key.offset)
Chris Masone37c9e62007-05-09 20:13:14 -0400282 goto new_group;
Chris Mason8790d502008-04-03 16:29:03 -0400283 if (!block_group_bits(cache, data)) {
Chris Mason611f0e02008-04-03 16:29:03 -0400284 printk("block group bits don't match %Lu %d\n", cache->flags, data);
Chris Mason8790d502008-04-03 16:29:03 -0400285 }
Chris Mason0b86a832008-03-24 15:01:56 -0400286 *start_ret = start;
287 return 0;
Chris Mason8790d502008-04-03 16:29:03 -0400288 }
289out:
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500290 cache = btrfs_lookup_block_group(root->fs_info, search_start);
291 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -0400292 printk("Unable to find block group for %Lu\n", search_start);
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500293 WARN_ON(1);
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500294 }
Chris Mason0b86a832008-03-24 15:01:56 -0400295 return -ENOSPC;
Chris Masone37c9e62007-05-09 20:13:14 -0400296
297new_group:
Chris Masone19caa52007-10-15 16:17:44 -0400298 last = cache->key.objectid + cache->key.offset;
Chris Masonf84a8b32007-11-06 10:26:29 -0500299wrapped:
Chris Mason0ef3e662008-05-24 14:04:53 -0400300 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Chris Masonc31f8832008-01-08 15:46:31 -0500301 if (!cache || cache->key.objectid >= total_fs_bytes) {
Chris Mason0e4de582007-11-26 10:55:49 -0500302no_cache:
Chris Masonf84a8b32007-11-06 10:26:29 -0500303 if (!wrapped) {
304 wrapped = 1;
305 last = search_start;
Chris Masonf84a8b32007-11-06 10:26:29 -0500306 goto wrapped;
307 }
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500308 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -0400309 }
Chris Mason257d0ce2007-11-07 21:08:16 -0500310 if (cache_miss && !cache->cached) {
311 cache_block_group(root, cache);
312 last = cache_miss;
Chris Mason0ef3e662008-05-24 14:04:53 -0400313 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Chris Mason257d0ce2007-11-07 21:08:16 -0500314 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400315 cache_miss = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400316 cache = __btrfs_find_block_group(root, cache, last, data, 0);
Chris Mason0e4de582007-11-26 10:55:49 -0500317 if (!cache)
318 goto no_cache;
Chris Masone37c9e62007-05-09 20:13:14 -0400319 *cache_ret = cache;
320 goto again;
321}
322
Chris Mason84f54cf2007-06-12 07:43:08 -0400323static u64 div_factor(u64 num, int factor)
324{
Chris Mason257d0ce2007-11-07 21:08:16 -0500325 if (factor == 10)
326 return num;
Chris Mason84f54cf2007-06-12 07:43:08 -0400327 num *= factor;
328 do_div(num, 10);
329 return num;
330}
331
Chris Mason6324fbf2008-03-24 15:01:59 -0400332static int block_group_state_bits(u64 flags)
333{
334 int bits = 0;
335 if (flags & BTRFS_BLOCK_GROUP_DATA)
336 bits |= BLOCK_GROUP_DATA;
337 if (flags & BTRFS_BLOCK_GROUP_METADATA)
338 bits |= BLOCK_GROUP_METADATA;
339 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
340 bits |= BLOCK_GROUP_SYSTEM;
341 return bits;
342}
343
Chris Mason925baed2008-06-25 16:01:30 -0400344static struct btrfs_block_group_cache *
345__btrfs_find_block_group(struct btrfs_root *root,
346 struct btrfs_block_group_cache *hint,
347 u64 search_start, int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400348{
Chris Mason96b51792007-10-15 16:15:19 -0400349 struct btrfs_block_group_cache *cache;
Chris Masond1310b22008-01-24 16:13:08 -0500350 struct extent_io_tree *block_group_cache;
Chris Mason31f3c992007-04-30 15:25:45 -0400351 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400352 struct btrfs_fs_info *info = root->fs_info;
353 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400354 u64 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -0400355 u64 start;
356 u64 end;
357 u64 free_check;
358 u64 ptr;
359 int bit;
Chris Masoncd1bc462007-04-27 10:08:34 -0400360 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400361 int full_search = 0;
Chris Masonbce4eae2008-04-24 14:42:46 -0400362 int factor = 10;
Chris Mason0ef3e662008-05-24 14:04:53 -0400363 int wrapped = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400364
Chris Mason96b51792007-10-15 16:15:19 -0400365 block_group_cache = &info->block_group_cache;
366
Chris Masona236aed2008-04-29 09:38:00 -0400367 if (data & BTRFS_BLOCK_GROUP_METADATA)
368 factor = 9;
Chris Masonbe744172007-05-06 10:15:01 -0400369
Chris Mason6324fbf2008-03-24 15:01:59 -0400370 bit = block_group_state_bits(data);
Chris Masonbe744172007-05-06 10:15:01 -0400371
Chris Mason0ef3e662008-05-24 14:04:53 -0400372 if (search_start) {
Chris Masonbe744172007-05-06 10:15:01 -0400373 struct btrfs_block_group_cache *shint;
Chris Mason0ef3e662008-05-24 14:04:53 -0400374 shint = btrfs_lookup_first_block_group(info, search_start);
Chris Mason8f18cf12008-04-25 16:53:30 -0400375 if (shint && block_group_bits(shint, data) && !shint->ro) {
Chris Masonbe744172007-05-06 10:15:01 -0400376 used = btrfs_block_group_used(&shint->item);
Yan324ae4d2007-11-16 14:57:08 -0500377 if (used + shint->pinned <
378 div_factor(shint->key.offset, factor)) {
Chris Masonbe744172007-05-06 10:15:01 -0400379 return shint;
380 }
381 }
382 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400383 if (hint && !hint->ro && block_group_bits(hint, data)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400384 used = btrfs_block_group_used(&hint->item);
Yan324ae4d2007-11-16 14:57:08 -0500385 if (used + hint->pinned <
386 div_factor(hint->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400387 return hint;
388 }
Chris Masone19caa52007-10-15 16:17:44 -0400389 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400390 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400391 if (hint)
Chris Mason0ef3e662008-05-24 14:04:53 -0400392 last = max(hint->key.objectid, search_start);
Chris Masone37c9e62007-05-09 20:13:14 -0400393 else
Chris Mason0ef3e662008-05-24 14:04:53 -0400394 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400395 }
Chris Mason31f3c992007-04-30 15:25:45 -0400396again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400397 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400398 ret = find_first_extent_bit(block_group_cache, last,
399 &start, &end, bit);
400 if (ret)
Chris Masoncd1bc462007-04-27 10:08:34 -0400401 break;
Chris Mason96b51792007-10-15 16:15:19 -0400402
403 ret = get_state_private(block_group_cache, start, &ptr);
Chris Mason0ef3e662008-05-24 14:04:53 -0400404 if (ret) {
405 last = end + 1;
406 continue;
407 }
Chris Mason96b51792007-10-15 16:15:19 -0400408
Jens Axboeae2f5412007-10-19 09:22:59 -0400409 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -0400410 last = cache->key.objectid + cache->key.offset;
411 used = btrfs_block_group_used(&cache->item);
412
Chris Mason8f18cf12008-04-25 16:53:30 -0400413 if (!cache->ro && block_group_bits(cache, data)) {
Chris Mason0ef3e662008-05-24 14:04:53 -0400414 free_check = div_factor(cache->key.offset, factor);
Chris Mason8790d502008-04-03 16:29:03 -0400415 if (used + cache->pinned < free_check) {
416 found_group = cache;
417 goto found;
418 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400419 }
Chris Masonde428b62007-05-18 13:28:27 -0400420 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400421 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400422 if (!wrapped) {
423 last = search_start;
424 wrapped = 1;
425 goto again;
426 }
427 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400428 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400429 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400430 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400431 goto again;
432 }
Chris Masonbe744172007-05-06 10:15:01 -0400433found:
Chris Mason31f3c992007-04-30 15:25:45 -0400434 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400435}
436
Chris Mason925baed2008-06-25 16:01:30 -0400437struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
438 struct btrfs_block_group_cache
439 *hint, u64 search_start,
440 int data, int owner)
441{
442
443 struct btrfs_block_group_cache *ret;
444 mutex_lock(&root->fs_info->alloc_mutex);
445 ret = __btrfs_find_block_group(root, hint, search_start, data, owner);
446 mutex_unlock(&root->fs_info->alloc_mutex);
447 return ret;
448}
Chris Mason7bb86312007-12-11 09:25:06 -0500449static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation,
Chris Mason74493f72007-12-11 09:25:06 -0500450 u64 owner, u64 owner_offset)
451{
452 u32 high_crc = ~(u32)0;
453 u32 low_crc = ~(u32)0;
454 __le64 lenum;
Chris Mason74493f72007-12-11 09:25:06 -0500455 lenum = cpu_to_le64(root_objectid);
Miguela5eb62e2008-04-11 15:45:51 -0400456 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
Chris Mason7bb86312007-12-11 09:25:06 -0500457 lenum = cpu_to_le64(ref_generation);
Miguela5eb62e2008-04-11 15:45:51 -0400458 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason21a49892008-02-01 14:51:59 -0500459 if (owner >= BTRFS_FIRST_FREE_OBJECTID) {
460 lenum = cpu_to_le64(owner);
Miguela5eb62e2008-04-11 15:45:51 -0400461 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason21a49892008-02-01 14:51:59 -0500462 lenum = cpu_to_le64(owner_offset);
Miguela5eb62e2008-04-11 15:45:51 -0400463 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason21a49892008-02-01 14:51:59 -0500464 }
Chris Mason74493f72007-12-11 09:25:06 -0500465 return ((u64)high_crc << 32) | (u64)low_crc;
466}
467
Chris Mason7bb86312007-12-11 09:25:06 -0500468static int match_extent_ref(struct extent_buffer *leaf,
469 struct btrfs_extent_ref *disk_ref,
470 struct btrfs_extent_ref *cpu_ref)
471{
472 int ret;
473 int len;
474
475 if (cpu_ref->objectid)
476 len = sizeof(*cpu_ref);
477 else
478 len = 2 * sizeof(u64);
479 ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref,
480 len);
481 return ret == 0;
482}
483
Chris Mason98ed5172008-01-03 10:01:48 -0500484static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
485 struct btrfs_root *root,
486 struct btrfs_path *path, u64 bytenr,
487 u64 root_objectid,
488 u64 ref_generation, u64 owner,
489 u64 owner_offset, int del)
Chris Mason7bb86312007-12-11 09:25:06 -0500490{
491 u64 hash;
492 struct btrfs_key key;
493 struct btrfs_key found_key;
494 struct btrfs_extent_ref ref;
495 struct extent_buffer *leaf;
496 struct btrfs_extent_ref *disk_ref;
497 int ret;
498 int ret2;
499
500 btrfs_set_stack_ref_root(&ref, root_objectid);
501 btrfs_set_stack_ref_generation(&ref, ref_generation);
502 btrfs_set_stack_ref_objectid(&ref, owner);
503 btrfs_set_stack_ref_offset(&ref, owner_offset);
504
505 hash = hash_extent_ref(root_objectid, ref_generation, owner,
506 owner_offset);
507 key.offset = hash;
508 key.objectid = bytenr;
509 key.type = BTRFS_EXTENT_REF_KEY;
510
511 while (1) {
512 ret = btrfs_search_slot(trans, root, &key, path,
513 del ? -1 : 0, del);
514 if (ret < 0)
515 goto out;
516 leaf = path->nodes[0];
517 if (ret != 0) {
518 u32 nritems = btrfs_header_nritems(leaf);
519 if (path->slots[0] >= nritems) {
520 ret2 = btrfs_next_leaf(root, path);
521 if (ret2)
522 goto out;
523 leaf = path->nodes[0];
524 }
525 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
526 if (found_key.objectid != bytenr ||
527 found_key.type != BTRFS_EXTENT_REF_KEY)
528 goto out;
529 key.offset = found_key.offset;
530 if (del) {
531 btrfs_release_path(root, path);
532 continue;
533 }
534 }
535 disk_ref = btrfs_item_ptr(path->nodes[0],
536 path->slots[0],
537 struct btrfs_extent_ref);
538 if (match_extent_ref(path->nodes[0], disk_ref, &ref)) {
539 ret = 0;
540 goto out;
541 }
542 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
543 key.offset = found_key.offset + 1;
544 btrfs_release_path(root, path);
545 }
546out:
547 return ret;
548}
549
Chris Masond8d5f3e2007-12-11 12:42:00 -0500550/*
551 * Back reference rules. Back refs have three main goals:
552 *
553 * 1) differentiate between all holders of references to an extent so that
554 * when a reference is dropped we can make sure it was a valid reference
555 * before freeing the extent.
556 *
557 * 2) Provide enough information to quickly find the holders of an extent
558 * if we notice a given block is corrupted or bad.
559 *
560 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
561 * maintenance. This is actually the same as #2, but with a slightly
562 * different use case.
563 *
564 * File extents can be referenced by:
565 *
566 * - multiple snapshots, subvolumes, or different generations in one subvol
567 * - different files inside a single subvolume (in theory, not implemented yet)
568 * - different offsets inside a file (bookend extents in file.c)
569 *
570 * The extent ref structure has fields for:
571 *
572 * - Objectid of the subvolume root
573 * - Generation number of the tree holding the reference
574 * - objectid of the file holding the reference
575 * - offset in the file corresponding to the key holding the reference
576 *
577 * When a file extent is allocated the fields are filled in:
578 * (root_key.objectid, trans->transid, inode objectid, offset in file)
579 *
580 * When a leaf is cow'd new references are added for every file extent found
581 * in the leaf. It looks the same as the create case, but trans->transid
582 * will be different when the block is cow'd.
583 *
584 * (root_key.objectid, trans->transid, inode objectid, offset in file)
585 *
586 * When a file extent is removed either during snapshot deletion or file
587 * truncation, the corresponding back reference is found
588 * by searching for:
589 *
590 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
591 * inode objectid, offset in file)
592 *
593 * Btree extents can be referenced by:
594 *
595 * - Different subvolumes
596 * - Different generations of the same subvolume
597 *
598 * Storing sufficient information for a full reverse mapping of a btree
599 * block would require storing the lowest key of the block in the backref,
600 * and it would require updating that lowest key either before write out or
601 * every time it changed. Instead, the objectid of the lowest key is stored
602 * along with the level of the tree block. This provides a hint
603 * about where in the btree the block can be found. Searches through the
604 * btree only need to look for a pointer to that block, so they stop one
605 * level higher than the level recorded in the backref.
606 *
607 * Some btrees do not do reference counting on their extents. These
608 * include the extent tree and the tree of tree roots. Backrefs for these
609 * trees always have a generation of zero.
610 *
611 * When a tree block is created, back references are inserted:
612 *
Chris Masonf6dbff52007-12-13 11:13:32 -0500613 * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500614 *
615 * When a tree block is cow'd in a reference counted root,
616 * new back references are added for all the blocks it points to.
617 * These are of the form (trans->transid will have increased since creation):
618 *
Chris Masonf6dbff52007-12-13 11:13:32 -0500619 * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500620 *
621 * Because the lowest_key_objectid and the level are just hints
622 * they are not used when backrefs are deleted. When a backref is deleted:
623 *
624 * if backref was for a tree root:
625 * root_objectid = root->root_key.objectid
626 * else
627 * root_objectid = btrfs_header_owner(parent)
628 *
629 * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
630 *
631 * Back Reference Key hashing:
632 *
633 * Back references have four fields, each 64 bits long. Unfortunately,
634 * This is hashed into a single 64 bit number and placed into the key offset.
635 * The key objectid corresponds to the first byte in the extent, and the
636 * key type is set to BTRFS_EXTENT_REF_KEY
637 */
Chris Mason7bb86312007-12-11 09:25:06 -0500638int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
639 struct btrfs_root *root,
640 struct btrfs_path *path, u64 bytenr,
641 u64 root_objectid, u64 ref_generation,
642 u64 owner, u64 owner_offset)
Chris Mason74493f72007-12-11 09:25:06 -0500643{
644 u64 hash;
645 struct btrfs_key key;
646 struct btrfs_extent_ref ref;
Chris Mason7bb86312007-12-11 09:25:06 -0500647 struct btrfs_extent_ref *disk_ref;
Chris Mason74493f72007-12-11 09:25:06 -0500648 int ret;
649
650 btrfs_set_stack_ref_root(&ref, root_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -0500651 btrfs_set_stack_ref_generation(&ref, ref_generation);
Chris Mason74493f72007-12-11 09:25:06 -0500652 btrfs_set_stack_ref_objectid(&ref, owner);
653 btrfs_set_stack_ref_offset(&ref, owner_offset);
654
Chris Mason7bb86312007-12-11 09:25:06 -0500655 hash = hash_extent_ref(root_objectid, ref_generation, owner,
656 owner_offset);
Chris Mason74493f72007-12-11 09:25:06 -0500657 key.offset = hash;
658 key.objectid = bytenr;
659 key.type = BTRFS_EXTENT_REF_KEY;
660
661 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref));
662 while (ret == -EEXIST) {
Chris Mason7bb86312007-12-11 09:25:06 -0500663 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
664 struct btrfs_extent_ref);
665 if (match_extent_ref(path->nodes[0], disk_ref, &ref))
666 goto out;
667 key.offset++;
668 btrfs_release_path(root, path);
669 ret = btrfs_insert_empty_item(trans, root, path, &key,
670 sizeof(ref));
Chris Mason74493f72007-12-11 09:25:06 -0500671 }
Chris Mason7bb86312007-12-11 09:25:06 -0500672 if (ret)
673 goto out;
674 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
675 struct btrfs_extent_ref);
676 write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref,
677 sizeof(ref));
678 btrfs_mark_buffer_dirty(path->nodes[0]);
679out:
680 btrfs_release_path(root, path);
681 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500682}
683
Chris Mason925baed2008-06-25 16:01:30 -0400684static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Chris Masonb18c6682007-04-17 13:26:50 -0400685 struct btrfs_root *root,
Chris Mason74493f72007-12-11 09:25:06 -0500686 u64 bytenr, u64 num_bytes,
Chris Mason7bb86312007-12-11 09:25:06 -0500687 u64 root_objectid, u64 ref_generation,
Chris Mason74493f72007-12-11 09:25:06 -0500688 u64 owner, u64 owner_offset)
Chris Mason02217ed2007-03-02 16:08:05 -0500689{
Chris Mason5caf2a02007-04-02 11:20:42 -0400690 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500691 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400692 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400693 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400694 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400695 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500696
Chris Masondb945352007-10-15 16:15:53 -0400697 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400698 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400699 if (!path)
700 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400701
Chris Mason3c12ac72008-04-21 12:01:38 -0400702 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -0400703 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -0400704 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -0400705 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -0400706 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400707 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400708 if (ret < 0)
709 return ret;
Chris Masona429e512007-04-18 16:15:28 -0400710 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500711 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400712 }
Chris Mason02217ed2007-03-02 16:08:05 -0500713 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400714 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400715 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400716 refs = btrfs_extent_refs(l, item);
717 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400718 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500719
Chris Mason5caf2a02007-04-02 11:20:42 -0400720 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -0500721
Chris Mason3c12ac72008-04-21 12:01:38 -0400722 path->reada = 1;
Chris Mason7bb86312007-12-11 09:25:06 -0500723 ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
724 path, bytenr, root_objectid,
725 ref_generation, owner, owner_offset);
726 BUG_ON(ret);
Chris Mason9f5fae22007-03-20 14:38:32 -0400727 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400728 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason74493f72007-12-11 09:25:06 -0500729
730 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500731 return 0;
732}
733
Chris Mason925baed2008-06-25 16:01:30 -0400734int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
735 struct btrfs_root *root,
736 u64 bytenr, u64 num_bytes,
737 u64 root_objectid, u64 ref_generation,
738 u64 owner, u64 owner_offset)
739{
740 int ret;
741
742 mutex_lock(&root->fs_info->alloc_mutex);
743 ret = __btrfs_inc_extent_ref(trans, root, bytenr, num_bytes,
744 root_objectid, ref_generation,
745 owner, owner_offset);
746 mutex_unlock(&root->fs_info->alloc_mutex);
747 return ret;
748}
749
Chris Masone9d0b132007-08-10 14:06:19 -0400750int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
751 struct btrfs_root *root)
752{
753 finish_current_insert(trans, root->fs_info->extent_root);
754 del_pending_extents(trans, root->fs_info->extent_root);
755 return 0;
756}
757
Chris Masonb18c6682007-04-17 13:26:50 -0400758static int lookup_extent_ref(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -0400759 struct btrfs_root *root, u64 bytenr,
760 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500761{
Chris Mason5caf2a02007-04-02 11:20:42 -0400762 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500763 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400764 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400765 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400766 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400767
Chris Masondb945352007-10-15 16:15:53 -0400768 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400769 path = btrfs_alloc_path();
Chris Mason3c12ac72008-04-21 12:01:38 -0400770 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -0400771 key.objectid = bytenr;
772 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -0400773 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400774 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400775 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400776 if (ret < 0)
777 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400778 if (ret != 0) {
779 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -0400780 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -0500781 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -0400782 }
783 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400784 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400785 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400786out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400787 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500788 return 0;
789}
790
Chris Masonbe20aa92007-12-17 20:14:01 -0500791u32 btrfs_count_snapshots_in_path(struct btrfs_root *root,
792 struct btrfs_path *count_path,
Chris Masona68d5932008-05-08 14:11:56 -0400793 u64 expected_owner,
Chris Masonbe20aa92007-12-17 20:14:01 -0500794 u64 first_extent)
795{
796 struct btrfs_root *extent_root = root->fs_info->extent_root;
797 struct btrfs_path *path;
798 u64 bytenr;
799 u64 found_objectid;
Chris Masona68d5932008-05-08 14:11:56 -0400800 u64 found_owner;
Chris Mason56b453c2008-01-03 09:08:27 -0500801 u64 root_objectid = root->root_key.objectid;
Chris Masonbe20aa92007-12-17 20:14:01 -0500802 u32 total_count = 0;
Chris Masonbbaf5492008-05-08 16:31:21 -0400803 u32 extent_refs;
Chris Masonbe20aa92007-12-17 20:14:01 -0500804 u32 cur_count;
Chris Masonbe20aa92007-12-17 20:14:01 -0500805 u32 nritems;
806 int ret;
807 struct btrfs_key key;
808 struct btrfs_key found_key;
809 struct extent_buffer *l;
810 struct btrfs_extent_item *item;
811 struct btrfs_extent_ref *ref_item;
812 int level = -1;
813
Chris Mason925baed2008-06-25 16:01:30 -0400814 /* FIXME, needs locking */
815 BUG();
816
817 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masonbe20aa92007-12-17 20:14:01 -0500818 path = btrfs_alloc_path();
819again:
820 if (level == -1)
821 bytenr = first_extent;
822 else
823 bytenr = count_path->nodes[level]->start;
824
825 cur_count = 0;
826 key.objectid = bytenr;
827 key.offset = 0;
828
829 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
830 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
831 if (ret < 0)
832 goto out;
833 BUG_ON(ret == 0);
834
835 l = path->nodes[0];
836 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
837
838 if (found_key.objectid != bytenr ||
839 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
840 goto out;
841 }
842
843 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masonbbaf5492008-05-08 16:31:21 -0400844 extent_refs = btrfs_extent_refs(l, item);
Chris Masonbe20aa92007-12-17 20:14:01 -0500845 while (1) {
Chris Masonbd098352008-01-03 13:23:19 -0500846 l = path->nodes[0];
Chris Masonbe20aa92007-12-17 20:14:01 -0500847 nritems = btrfs_header_nritems(l);
848 if (path->slots[0] >= nritems) {
849 ret = btrfs_next_leaf(extent_root, path);
850 if (ret == 0)
851 continue;
852 break;
853 }
854 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
855 if (found_key.objectid != bytenr)
856 break;
Chris Masonbd098352008-01-03 13:23:19 -0500857
Chris Masonbe20aa92007-12-17 20:14:01 -0500858 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
859 path->slots[0]++;
860 continue;
861 }
862
863 cur_count++;
864 ref_item = btrfs_item_ptr(l, path->slots[0],
865 struct btrfs_extent_ref);
866 found_objectid = btrfs_ref_root(l, ref_item);
867
Chris Mason56b453c2008-01-03 09:08:27 -0500868 if (found_objectid != root_objectid) {
869 total_count = 2;
Chris Mason4313b392008-01-03 09:08:48 -0500870 goto out;
Chris Mason56b453c2008-01-03 09:08:27 -0500871 }
Chris Masona68d5932008-05-08 14:11:56 -0400872 if (level == -1) {
873 found_owner = btrfs_ref_objectid(l, ref_item);
874 if (found_owner != expected_owner) {
875 total_count = 2;
876 goto out;
877 }
Chris Masonbbaf5492008-05-08 16:31:21 -0400878 /*
879 * nasty. we don't count a reference held by
880 * the running transaction. This allows nodatacow
881 * to avoid cow most of the time
882 */
883 if (found_owner >= BTRFS_FIRST_FREE_OBJECTID &&
884 btrfs_ref_generation(l, ref_item) ==
885 root->fs_info->generation) {
886 extent_refs--;
887 }
Chris Masona68d5932008-05-08 14:11:56 -0400888 }
Chris Mason56b453c2008-01-03 09:08:27 -0500889 total_count = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500890 path->slots[0]++;
891 }
Chris Masonbbaf5492008-05-08 16:31:21 -0400892 /*
893 * if there is more than one reference against a data extent,
894 * we have to assume the other ref is another snapshot
895 */
896 if (level == -1 && extent_refs > 1) {
897 total_count = 2;
898 goto out;
899 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500900 if (cur_count == 0) {
901 total_count = 0;
902 goto out;
903 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500904 if (level >= 0 && root->node == count_path->nodes[level])
905 goto out;
906 level++;
907 btrfs_release_path(root, path);
908 goto again;
909
910out:
911 btrfs_free_path(path);
Chris Mason925baed2008-06-25 16:01:30 -0400912 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonbe20aa92007-12-17 20:14:01 -0500913 return total_count;
Chris Masonbe20aa92007-12-17 20:14:01 -0500914}
Chris Masonc5739bb2007-04-10 09:27:04 -0400915
Chris Masone089f052007-03-16 16:20:31 -0400916int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400917 struct extent_buffer *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500918{
Chris Masondb945352007-10-15 16:15:53 -0400919 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400920 u32 nritems;
921 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -0400922 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500923 int i;
Chris Masondb945352007-10-15 16:15:53 -0400924 int level;
Chris Mason6407bf62007-03-27 06:33:00 -0400925 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400926 int faili;
Chris Masona28ec192007-03-06 20:08:01 -0500927
Chris Mason3768f362007-03-13 16:47:54 -0400928 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500929 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400930
Chris Mason925baed2008-06-25 16:01:30 -0400931 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masondb945352007-10-15 16:15:53 -0400932 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400933 nritems = btrfs_header_nritems(buf);
934 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400935 if (level == 0) {
936 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400937 btrfs_item_key_to_cpu(buf, &key, i);
938 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -0400939 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400940 fi = btrfs_item_ptr(buf, i,
Chris Mason6407bf62007-03-27 06:33:00 -0400941 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400942 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason236454d2007-04-19 13:37:44 -0400943 BTRFS_FILE_EXTENT_INLINE)
944 continue;
Chris Masondb945352007-10-15 16:15:53 -0400945 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
946 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -0400947 continue;
Chris Mason925baed2008-06-25 16:01:30 -0400948 ret = __btrfs_inc_extent_ref(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500949 btrfs_file_extent_disk_num_bytes(buf, fi),
950 root->root_key.objectid, trans->transid,
951 key.objectid, key.offset);
Chris Mason54aa1f42007-06-22 14:16:25 -0400952 if (ret) {
953 faili = i;
954 goto fail;
955 }
Chris Mason6407bf62007-03-27 06:33:00 -0400956 } else {
Chris Masondb945352007-10-15 16:15:53 -0400957 bytenr = btrfs_node_blockptr(buf, i);
Chris Mason6caab482007-12-13 09:48:07 -0500958 btrfs_node_key_to_cpu(buf, &key, i);
Chris Mason925baed2008-06-25 16:01:30 -0400959 ret = __btrfs_inc_extent_ref(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500960 btrfs_level_size(root, level - 1),
961 root->root_key.objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -0500962 trans->transid,
963 level - 1, key.objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400964 if (ret) {
965 faili = i;
966 goto fail;
967 }
Chris Mason6407bf62007-03-27 06:33:00 -0400968 }
Chris Mason02217ed2007-03-02 16:08:05 -0500969 }
Chris Mason925baed2008-06-25 16:01:30 -0400970 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason02217ed2007-03-02 16:08:05 -0500971 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400972fail:
Chris Masonccd467d2007-06-28 15:57:36 -0400973 WARN_ON(1);
Chris Mason7bb86312007-12-11 09:25:06 -0500974#if 0
Chris Mason54aa1f42007-06-22 14:16:25 -0400975 for (i =0; i < faili; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400976 if (level == 0) {
977 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400978 btrfs_item_key_to_cpu(buf, &key, i);
979 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -0400980 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400981 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -0400982 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400983 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -0400984 BTRFS_FILE_EXTENT_INLINE)
985 continue;
Chris Masondb945352007-10-15 16:15:53 -0400986 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
987 if (disk_bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -0400988 continue;
Chris Masondb945352007-10-15 16:15:53 -0400989 err = btrfs_free_extent(trans, root, disk_bytenr,
990 btrfs_file_extent_disk_num_bytes(buf,
Chris Mason5f39d392007-10-15 16:14:19 -0400991 fi), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400992 BUG_ON(err);
993 } else {
Chris Masondb945352007-10-15 16:15:53 -0400994 bytenr = btrfs_node_blockptr(buf, i);
995 err = btrfs_free_extent(trans, root, bytenr,
996 btrfs_level_size(root, level - 1), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400997 BUG_ON(err);
998 }
999 }
Chris Mason7bb86312007-12-11 09:25:06 -05001000#endif
Chris Mason925baed2008-06-25 16:01:30 -04001001 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason54aa1f42007-06-22 14:16:25 -04001002 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001003}
1004
Chris Mason9078a3e2007-04-26 16:46:15 -04001005static int write_one_cache_group(struct btrfs_trans_handle *trans,
1006 struct btrfs_root *root,
1007 struct btrfs_path *path,
1008 struct btrfs_block_group_cache *cache)
1009{
1010 int ret;
1011 int pending_ret;
1012 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001013 unsigned long bi;
1014 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04001015
Chris Mason9078a3e2007-04-26 16:46:15 -04001016 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001017 if (ret < 0)
1018 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04001019 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001020
1021 leaf = path->nodes[0];
1022 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1023 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1024 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04001025 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04001026fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04001027 finish_current_insert(trans, extent_root);
1028 pending_ret = del_pending_extents(trans, extent_root);
1029 if (ret)
1030 return ret;
1031 if (pending_ret)
1032 return pending_ret;
1033 return 0;
1034
1035}
1036
Chris Mason96b51792007-10-15 16:15:19 -04001037int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1038 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04001039{
Chris Masond1310b22008-01-24 16:13:08 -05001040 struct extent_io_tree *block_group_cache;
Chris Mason96b51792007-10-15 16:15:19 -04001041 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04001042 int ret;
1043 int err = 0;
1044 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001045 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04001046 u64 last = 0;
1047 u64 start;
1048 u64 end;
1049 u64 ptr;
Chris Mason9078a3e2007-04-26 16:46:15 -04001050
Chris Mason96b51792007-10-15 16:15:19 -04001051 block_group_cache = &root->fs_info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04001052 path = btrfs_alloc_path();
1053 if (!path)
1054 return -ENOMEM;
1055
Chris Mason925baed2008-06-25 16:01:30 -04001056 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason9078a3e2007-04-26 16:46:15 -04001057 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -04001058 ret = find_first_extent_bit(block_group_cache, last,
1059 &start, &end, BLOCK_GROUP_DIRTY);
1060 if (ret)
Chris Mason9078a3e2007-04-26 16:46:15 -04001061 break;
Chris Mason54aa1f42007-06-22 14:16:25 -04001062
Chris Mason96b51792007-10-15 16:15:19 -04001063 last = end + 1;
1064 ret = get_state_private(block_group_cache, start, &ptr);
1065 if (ret)
1066 break;
Jens Axboeae2f5412007-10-19 09:22:59 -04001067 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -04001068 err = write_one_cache_group(trans, root,
1069 path, cache);
1070 /*
1071 * if we fail to write the cache group, we want
1072 * to keep it marked dirty in hopes that a later
1073 * write will work
1074 */
1075 if (err) {
1076 werr = err;
1077 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04001078 }
Chris Mason96b51792007-10-15 16:15:19 -04001079 clear_extent_bits(block_group_cache, start, end,
1080 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04001081 }
1082 btrfs_free_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04001083 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason9078a3e2007-04-26 16:46:15 -04001084 return werr;
1085}
1086
Chris Mason6324fbf2008-03-24 15:01:59 -04001087static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
1088 u64 flags)
1089{
1090 struct list_head *head = &info->space_info;
1091 struct list_head *cur;
1092 struct btrfs_space_info *found;
1093 list_for_each(cur, head) {
1094 found = list_entry(cur, struct btrfs_space_info, list);
1095 if (found->flags == flags)
1096 return found;
1097 }
1098 return NULL;
1099
1100}
1101
Chris Mason593060d2008-03-25 16:50:33 -04001102static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1103 u64 total_bytes, u64 bytes_used,
1104 struct btrfs_space_info **space_info)
1105{
1106 struct btrfs_space_info *found;
1107
1108 found = __find_space_info(info, flags);
1109 if (found) {
1110 found->total_bytes += total_bytes;
1111 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04001112 found->full = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001113 WARN_ON(found->total_bytes < found->bytes_used);
1114 *space_info = found;
1115 return 0;
1116 }
1117 found = kmalloc(sizeof(*found), GFP_NOFS);
1118 if (!found)
1119 return -ENOMEM;
1120
1121 list_add(&found->list, &info->space_info);
1122 found->flags = flags;
1123 found->total_bytes = total_bytes;
1124 found->bytes_used = bytes_used;
1125 found->bytes_pinned = 0;
1126 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001127 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001128 *space_info = found;
1129 return 0;
1130}
1131
Chris Mason8790d502008-04-03 16:29:03 -04001132static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1133{
1134 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04001135 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001136 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04001137 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04001138 if (extra_flags) {
1139 if (flags & BTRFS_BLOCK_GROUP_DATA)
1140 fs_info->avail_data_alloc_bits |= extra_flags;
1141 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1142 fs_info->avail_metadata_alloc_bits |= extra_flags;
1143 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1144 fs_info->avail_system_alloc_bits |= extra_flags;
1145 }
1146}
Chris Mason593060d2008-03-25 16:50:33 -04001147
Chris Masona061fc82008-05-07 11:43:44 -04001148static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04001149{
Chris Masona061fc82008-05-07 11:43:44 -04001150 u64 num_devices = root->fs_info->fs_devices->num_devices;
1151
1152 if (num_devices == 1)
1153 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1154 if (num_devices < 4)
1155 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1156
Chris Masonec44a352008-04-28 15:29:52 -04001157 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1158 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04001159 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04001160 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04001161 }
Chris Masonec44a352008-04-28 15:29:52 -04001162
1163 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04001164 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04001165 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04001166 }
Chris Masonec44a352008-04-28 15:29:52 -04001167
1168 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1169 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1170 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1171 (flags & BTRFS_BLOCK_GROUP_DUP)))
1172 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1173 return flags;
1174}
1175
Chris Mason6324fbf2008-03-24 15:01:59 -04001176static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1177 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04001178 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04001179{
1180 struct btrfs_space_info *space_info;
1181 u64 thresh;
1182 u64 start;
1183 u64 num_bytes;
1184 int ret;
1185
Chris Masona061fc82008-05-07 11:43:44 -04001186 flags = reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04001187
Chris Mason6324fbf2008-03-24 15:01:59 -04001188 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04001189 if (!space_info) {
1190 ret = update_space_info(extent_root->fs_info, flags,
1191 0, 0, &space_info);
1192 BUG_ON(ret);
1193 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001194 BUG_ON(!space_info);
1195
Chris Mason0ef3e662008-05-24 14:04:53 -04001196 if (space_info->force_alloc) {
1197 force = 1;
1198 space_info->force_alloc = 0;
1199 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001200 if (space_info->full)
Chris Mason925baed2008-06-25 16:01:30 -04001201 goto out;
Chris Mason6324fbf2008-03-24 15:01:59 -04001202
Chris Mason8790d502008-04-03 16:29:03 -04001203 thresh = div_factor(space_info->total_bytes, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04001204 if (!force &&
1205 (space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) <
Chris Mason6324fbf2008-03-24 15:01:59 -04001206 thresh)
Chris Mason925baed2008-06-25 16:01:30 -04001207 goto out;
Chris Mason6324fbf2008-03-24 15:01:59 -04001208
Chris Mason925baed2008-06-25 16:01:30 -04001209 mutex_lock(&extent_root->fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04001210 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
1211 if (ret == -ENOSPC) {
1212printk("space info full %Lu\n", flags);
1213 space_info->full = 1;
Chris Mason925baed2008-06-25 16:01:30 -04001214 goto out;
Chris Mason6324fbf2008-03-24 15:01:59 -04001215 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001216 BUG_ON(ret);
1217
1218 ret = btrfs_make_block_group(trans, extent_root, 0, flags,
Chris Masone17cade2008-04-15 15:41:47 -04001219 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
Chris Mason6324fbf2008-03-24 15:01:59 -04001220 BUG_ON(ret);
Chris Mason925baed2008-06-25 16:01:30 -04001221 mutex_unlock(&extent_root->fs_info->chunk_mutex);
1222out:
Chris Mason6324fbf2008-03-24 15:01:59 -04001223 return 0;
1224}
1225
Chris Mason9078a3e2007-04-26 16:46:15 -04001226static int update_block_group(struct btrfs_trans_handle *trans,
1227 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04001228 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04001229 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04001230{
1231 struct btrfs_block_group_cache *cache;
1232 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001233 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001234 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04001235 u64 byte_in_group;
Chris Mason96b51792007-10-15 16:15:19 -04001236 u64 start;
1237 u64 end;
Chris Mason3e1ad542007-05-07 20:03:49 -04001238
Chris Mason9078a3e2007-04-26 16:46:15 -04001239 while(total) {
Chris Masondb945352007-10-15 16:15:53 -04001240 cache = btrfs_lookup_block_group(info, bytenr);
Chris Mason3e1ad542007-05-07 20:03:49 -04001241 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -04001242 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -04001243 }
Chris Masondb945352007-10-15 16:15:53 -04001244 byte_in_group = bytenr - cache->key.objectid;
1245 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason96b51792007-10-15 16:15:19 -04001246 start = cache->key.objectid;
1247 end = start + cache->key.offset - 1;
1248 set_extent_bits(&info->block_group_cache, start, end,
1249 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04001250
1251 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001252 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001253 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04001254 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001255 cache->space_info->bytes_used += num_bytes;
Chris Masoncd1bc462007-04-27 10:08:34 -04001256 } else {
Chris Masondb945352007-10-15 16:15:53 -04001257 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001258 cache->space_info->bytes_used -= num_bytes;
Chris Masonf510cfe2007-10-15 16:14:48 -04001259 if (mark_free) {
1260 set_extent_dirty(&info->free_space_cache,
Chris Masondb945352007-10-15 16:15:53 -04001261 bytenr, bytenr + num_bytes - 1,
Chris Masonf510cfe2007-10-15 16:14:48 -04001262 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04001263 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001264 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001265 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masondb945352007-10-15 16:15:53 -04001266 total -= num_bytes;
1267 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001268 }
1269 return 0;
1270}
Chris Mason6324fbf2008-03-24 15:01:59 -04001271
Chris Masona061fc82008-05-07 11:43:44 -04001272static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1273{
1274 u64 start;
1275 u64 end;
1276 int ret;
1277 ret = find_first_extent_bit(&root->fs_info->block_group_cache,
1278 search_start, &start, &end,
1279 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
1280 BLOCK_GROUP_SYSTEM);
1281 if (ret)
1282 return 0;
1283 return start;
1284}
1285
1286
Yan324ae4d2007-11-16 14:57:08 -05001287static int update_pinned_extents(struct btrfs_root *root,
1288 u64 bytenr, u64 num, int pin)
1289{
1290 u64 len;
1291 struct btrfs_block_group_cache *cache;
1292 struct btrfs_fs_info *fs_info = root->fs_info;
1293
1294 if (pin) {
1295 set_extent_dirty(&fs_info->pinned_extents,
1296 bytenr, bytenr + num - 1, GFP_NOFS);
1297 } else {
1298 clear_extent_dirty(&fs_info->pinned_extents,
1299 bytenr, bytenr + num - 1, GFP_NOFS);
1300 }
1301 while (num > 0) {
1302 cache = btrfs_lookup_block_group(fs_info, bytenr);
Chris Masona061fc82008-05-07 11:43:44 -04001303 if (!cache) {
1304 u64 first = first_logical_byte(root, bytenr);
1305 WARN_ON(first < bytenr);
1306 len = min(first - bytenr, num);
1307 } else {
1308 len = min(num, cache->key.offset -
1309 (bytenr - cache->key.objectid));
1310 }
Yan324ae4d2007-11-16 14:57:08 -05001311 if (pin) {
Chris Masona061fc82008-05-07 11:43:44 -04001312 if (cache) {
1313 cache->pinned += len;
1314 cache->space_info->bytes_pinned += len;
1315 }
Yan324ae4d2007-11-16 14:57:08 -05001316 fs_info->total_pinned += len;
1317 } else {
Chris Masona061fc82008-05-07 11:43:44 -04001318 if (cache) {
1319 cache->pinned -= len;
1320 cache->space_info->bytes_pinned -= len;
1321 }
Yan324ae4d2007-11-16 14:57:08 -05001322 fs_info->total_pinned -= len;
1323 }
1324 bytenr += len;
1325 num -= len;
1326 }
1327 return 0;
1328}
Chris Mason9078a3e2007-04-26 16:46:15 -04001329
Chris Masond1310b22008-01-24 16:13:08 -05001330int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04001331{
Chris Masonccd467d2007-06-28 15:57:36 -04001332 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001333 u64 start;
1334 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05001335 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04001336 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04001337
1338 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001339 ret = find_first_extent_bit(pinned_extents, last,
1340 &start, &end, EXTENT_DIRTY);
1341 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04001342 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04001343 set_extent_dirty(copy, start, end, GFP_NOFS);
1344 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04001345 }
1346 return 0;
1347}
1348
1349int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1350 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05001351 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05001352{
Chris Mason1a5bc162007-10-15 16:15:26 -04001353 u64 start;
1354 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05001355 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05001356 struct extent_io_tree *free_space_cache;
Chris Masonf510cfe2007-10-15 16:14:48 -04001357 free_space_cache = &root->fs_info->free_space_cache;
Chris Masona28ec192007-03-06 20:08:01 -05001358
Chris Mason925baed2008-06-25 16:01:30 -04001359 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05001360 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001361 ret = find_first_extent_bit(unpin, 0, &start, &end,
1362 EXTENT_DIRTY);
1363 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05001364 break;
Yan324ae4d2007-11-16 14:57:08 -05001365 update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04001366 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1367 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
Chris Masona28ec192007-03-06 20:08:01 -05001368 }
Chris Mason925baed2008-06-25 16:01:30 -04001369 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05001370 return 0;
1371}
1372
Chris Mason98ed5172008-01-03 10:01:48 -05001373static int finish_current_insert(struct btrfs_trans_handle *trans,
1374 struct btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -05001375{
Chris Mason7bb86312007-12-11 09:25:06 -05001376 u64 start;
1377 u64 end;
1378 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001379 struct extent_buffer *eb;
Chris Mason7bb86312007-12-11 09:25:06 -05001380 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001381 struct btrfs_key ins;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001382 struct btrfs_disk_key first;
Chris Mason234b63a2007-03-13 10:46:10 -04001383 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001384 int ret;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001385 int level;
Chris Mason1a5bc162007-10-15 16:15:26 -04001386 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001387
Chris Mason5f39d392007-10-15 16:14:19 -04001388 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason62e27492007-03-15 12:56:47 -04001389 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason7bb86312007-12-11 09:25:06 -05001390 path = btrfs_alloc_path();
Chris Mason037e6392007-03-07 11:50:24 -05001391
Chris Mason26b80032007-08-08 20:17:12 -04001392 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001393 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1394 &end, EXTENT_LOCKED);
1395 if (ret)
Chris Mason26b80032007-08-08 20:17:12 -04001396 break;
1397
Chris Mason1a5bc162007-10-15 16:15:26 -04001398 ins.objectid = start;
1399 ins.offset = end + 1 - start;
1400 err = btrfs_insert_item(trans, extent_root, &ins,
1401 &extent_item, sizeof(extent_item));
1402 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
1403 GFP_NOFS);
Chris Masonca7a79a2008-05-12 12:59:19 -04001404 eb = read_tree_block(extent_root, ins.objectid, ins.offset,
1405 trans->transid);
Chris Mason925baed2008-06-25 16:01:30 -04001406 btrfs_tree_lock(eb);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001407 level = btrfs_header_level(eb);
1408 if (level == 0) {
1409 btrfs_item_key(eb, &first, 0);
1410 } else {
1411 btrfs_node_key(eb, &first, 0);
1412 }
Chris Mason925baed2008-06-25 16:01:30 -04001413 btrfs_tree_unlock(eb);
1414 free_extent_buffer(eb);
1415 /*
1416 * the first key is just a hint, so the race we've created
1417 * against reading it is fine
1418 */
Chris Mason7bb86312007-12-11 09:25:06 -05001419 err = btrfs_insert_extent_backref(trans, extent_root, path,
1420 start, extent_root->root_key.objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -05001421 0, level,
1422 btrfs_disk_key_objectid(&first));
Chris Mason7bb86312007-12-11 09:25:06 -05001423 BUG_ON(err);
Chris Mason037e6392007-03-07 11:50:24 -05001424 }
Chris Mason7bb86312007-12-11 09:25:06 -05001425 btrfs_free_path(path);
Chris Mason037e6392007-03-07 11:50:24 -05001426 return 0;
1427}
1428
Chris Masondb945352007-10-15 16:15:53 -04001429static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
1430 int pending)
Chris Masone20d96d2007-03-22 12:13:20 -04001431{
Chris Mason1a5bc162007-10-15 16:15:26 -04001432 int err = 0;
Chris Mason78fae272007-03-25 11:35:08 -04001433
Chris Masonf4b9aa82007-03-27 11:05:53 -04001434 if (!pending) {
Chris Mason925baed2008-06-25 16:01:30 -04001435#if 0
1436 struct extent_buffer *buf;
Chris Masondb945352007-10-15 16:15:53 -04001437 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
Chris Mason5f39d392007-10-15 16:14:19 -04001438 if (buf) {
Chris Mason925baed2008-06-25 16:01:30 -04001439 if (!btrfs_try_tree_lock(buf) &&
1440 btrfs_buffer_uptodate(buf, 0)) {
Chris Mason2c90e5d2007-04-02 10:50:19 -04001441 u64 transid =
1442 root->fs_info->running_transaction->transid;
Chris Masondc17ff82008-01-08 15:46:30 -05001443 u64 header_transid =
1444 btrfs_header_generation(buf);
Chris Mason6bc34672008-04-04 15:40:00 -04001445 if (header_transid == transid &&
1446 !btrfs_header_flag(buf,
1447 BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason55c69072008-01-09 15:55:33 -05001448 clean_tree_block(NULL, root, buf);
Chris Mason925baed2008-06-25 16:01:30 -04001449 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001450 free_extent_buffer(buf);
Yanc5492282007-11-06 10:25:25 -05001451 return 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001452 }
Chris Mason925baed2008-06-25 16:01:30 -04001453 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001454 }
Chris Mason5f39d392007-10-15 16:14:19 -04001455 free_extent_buffer(buf);
Chris Mason8ef97622007-03-26 10:15:30 -04001456 }
Chris Mason925baed2008-06-25 16:01:30 -04001457#endif
Yan324ae4d2007-11-16 14:57:08 -05001458 update_pinned_extents(root, bytenr, num_bytes, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001459 } else {
Chris Mason1a5bc162007-10-15 16:15:26 -04001460 set_extent_bits(&root->fs_info->pending_del,
Chris Masondb945352007-10-15 16:15:53 -04001461 bytenr, bytenr + num_bytes - 1,
1462 EXTENT_LOCKED, GFP_NOFS);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001463 }
Chris Masonbe744172007-05-06 10:15:01 -04001464 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001465 return 0;
1466}
1467
Chris Masona28ec192007-03-06 20:08:01 -05001468/*
1469 * remove an extent from the root, returns 0 on success
1470 */
Chris Masone089f052007-03-16 16:20:31 -04001471static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason7bb86312007-12-11 09:25:06 -05001472 *root, u64 bytenr, u64 num_bytes,
1473 u64 root_objectid, u64 ref_generation,
1474 u64 owner_objectid, u64 owner_offset, int pin,
Chris Masone37c9e62007-05-09 20:13:14 -04001475 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -05001476{
Chris Mason5caf2a02007-04-02 11:20:42 -04001477 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001478 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04001479 struct btrfs_fs_info *info = root->fs_info;
1480 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001481 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05001482 int ret;
Chris Mason952fcca2008-02-18 16:33:44 -05001483 int extent_slot = 0;
1484 int found_extent = 0;
1485 int num_to_del = 1;
Chris Mason234b63a2007-03-13 10:46:10 -04001486 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04001487 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05001488
Chris Masondb945352007-10-15 16:15:53 -04001489 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04001490 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04001491 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04001492 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001493 if (!path)
1494 return -ENOMEM;
1495
Chris Mason3c12ac72008-04-21 12:01:38 -04001496 path->reada = 1;
Chris Mason7bb86312007-12-11 09:25:06 -05001497 ret = lookup_extent_backref(trans, extent_root, path,
1498 bytenr, root_objectid,
1499 ref_generation,
1500 owner_objectid, owner_offset, 1);
1501 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05001502 struct btrfs_key found_key;
1503 extent_slot = path->slots[0];
1504 while(extent_slot > 0) {
1505 extent_slot--;
1506 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1507 extent_slot);
1508 if (found_key.objectid != bytenr)
1509 break;
1510 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
1511 found_key.offset == num_bytes) {
1512 found_extent = 1;
1513 break;
1514 }
1515 if (path->slots[0] - extent_slot > 5)
1516 break;
1517 }
1518 if (!found_extent)
1519 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001520 } else {
1521 btrfs_print_leaf(extent_root, path->nodes[0]);
1522 WARN_ON(1);
1523 printk("Unable to find ref byte nr %Lu root %Lu "
1524 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1525 root_objectid, ref_generation, owner_objectid,
1526 owner_offset);
1527 }
Chris Mason952fcca2008-02-18 16:33:44 -05001528 if (!found_extent) {
1529 btrfs_release_path(extent_root, path);
1530 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
1531 if (ret < 0)
1532 return ret;
1533 BUG_ON(ret);
1534 extent_slot = path->slots[0];
1535 }
Chris Mason5f39d392007-10-15 16:14:19 -04001536
1537 leaf = path->nodes[0];
Chris Mason952fcca2008-02-18 16:33:44 -05001538 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04001539 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001540 refs = btrfs_extent_refs(leaf, ei);
1541 BUG_ON(refs == 0);
1542 refs -= 1;
1543 btrfs_set_extent_refs(leaf, ei, refs);
Chris Mason952fcca2008-02-18 16:33:44 -05001544
Chris Mason5f39d392007-10-15 16:14:19 -04001545 btrfs_mark_buffer_dirty(leaf);
1546
Chris Mason952fcca2008-02-18 16:33:44 -05001547 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
1548 /* if the back ref and the extent are next to each other
1549 * they get deleted below in one shot
1550 */
1551 path->slots[0] = extent_slot;
1552 num_to_del = 2;
1553 } else if (found_extent) {
1554 /* otherwise delete the extent back ref */
1555 ret = btrfs_del_item(trans, extent_root, path);
1556 BUG_ON(ret);
1557 /* if refs are 0, we need to setup the path for deletion */
1558 if (refs == 0) {
1559 btrfs_release_path(extent_root, path);
1560 ret = btrfs_search_slot(trans, extent_root, &key, path,
1561 -1, 1);
1562 if (ret < 0)
1563 return ret;
1564 BUG_ON(ret);
1565 }
1566 }
1567
Chris Masoncf27e1e2007-03-13 09:49:06 -04001568 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04001569 u64 super_used;
1570 u64 root_used;
Chris Mason78fae272007-03-25 11:35:08 -04001571
1572 if (pin) {
Chris Masondb945352007-10-15 16:15:53 -04001573 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
Yanc5492282007-11-06 10:25:25 -05001574 if (ret > 0)
1575 mark_free = 1;
1576 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04001577 }
1578
Josef Bacik58176a92007-08-29 15:47:34 -04001579 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -04001580 super_used = btrfs_super_bytes_used(&info->super_copy);
1581 btrfs_set_super_bytes_used(&info->super_copy,
1582 super_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001583
1584 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001585 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001586 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04001587 root_used - num_bytes);
Chris Mason952fcca2008-02-18 16:33:44 -05001588 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1589 num_to_del);
Chris Mason54aa1f42007-06-22 14:16:25 -04001590 if (ret) {
1591 return ret;
1592 }
Chris Masondb945352007-10-15 16:15:53 -04001593 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001594 mark_free);
Chris Mason9078a3e2007-04-26 16:46:15 -04001595 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05001596 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001597 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04001598 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -05001599 return ret;
1600}
1601
1602/*
Chris Masonfec577f2007-02-26 10:40:21 -05001603 * find all the blocks marked as pending in the radix tree and remove
1604 * them from the extent map
1605 */
Chris Masone089f052007-03-16 16:20:31 -04001606static int del_pending_extents(struct btrfs_trans_handle *trans, struct
1607 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -05001608{
1609 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04001610 int err = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001611 u64 start;
1612 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05001613 struct extent_io_tree *pending_del;
1614 struct extent_io_tree *pinned_extents;
Chris Mason8ef97622007-03-26 10:15:30 -04001615
Chris Mason1a5bc162007-10-15 16:15:26 -04001616 pending_del = &extent_root->fs_info->pending_del;
1617 pinned_extents = &extent_root->fs_info->pinned_extents;
Chris Masonfec577f2007-02-26 10:40:21 -05001618
1619 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001620 ret = find_first_extent_bit(pending_del, 0, &start, &end,
1621 EXTENT_LOCKED);
1622 if (ret)
Chris Masonfec577f2007-02-26 10:40:21 -05001623 break;
Yan324ae4d2007-11-16 14:57:08 -05001624 update_pinned_extents(extent_root, start, end + 1 - start, 1);
Chris Mason1a5bc162007-10-15 16:15:26 -04001625 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
1626 GFP_NOFS);
1627 ret = __free_extent(trans, extent_root,
Chris Mason7bb86312007-12-11 09:25:06 -05001628 start, end + 1 - start,
1629 extent_root->root_key.objectid,
1630 0, 0, 0, 0, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04001631 if (ret)
1632 err = ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001633 }
Chris Masone20d96d2007-03-22 12:13:20 -04001634 return err;
Chris Masonfec577f2007-02-26 10:40:21 -05001635}
1636
1637/*
1638 * remove an extent from the root, returns 0 on success
1639 */
Chris Mason925baed2008-06-25 16:01:30 -04001640static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
1641 struct btrfs_root *root, u64 bytenr,
1642 u64 num_bytes, u64 root_objectid,
1643 u64 ref_generation, u64 owner_objectid,
1644 u64 owner_offset, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -05001645{
Chris Mason9f5fae22007-03-20 14:38:32 -04001646 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -05001647 int pending_ret;
1648 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05001649
Chris Masondb945352007-10-15 16:15:53 -04001650 WARN_ON(num_bytes < root->sectorsize);
Chris Mason7bb86312007-12-11 09:25:06 -05001651 if (!root->ref_cows)
1652 ref_generation = 0;
1653
Chris Masona28ec192007-03-06 20:08:01 -05001654 if (root == extent_root) {
Chris Masondb945352007-10-15 16:15:53 -04001655 pin_down_bytes(root, bytenr, num_bytes, 1);
Chris Masona28ec192007-03-06 20:08:01 -05001656 return 0;
1657 }
Chris Mason7bb86312007-12-11 09:25:06 -05001658 ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
1659 ref_generation, owner_objectid, owner_offset,
1660 pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001661 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -05001662 return ret ? ret : pending_ret;
1663}
1664
Chris Mason925baed2008-06-25 16:01:30 -04001665int btrfs_free_extent(struct btrfs_trans_handle *trans,
1666 struct btrfs_root *root, u64 bytenr,
1667 u64 num_bytes, u64 root_objectid,
1668 u64 ref_generation, u64 owner_objectid,
1669 u64 owner_offset, int pin)
1670{
1671 int ret;
1672
1673 maybe_lock_mutex(root);
1674 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes,
1675 root_objectid, ref_generation,
1676 owner_objectid, owner_offset, pin);
1677 maybe_unlock_mutex(root);
1678 return ret;
1679}
1680
Chris Mason87ee04e2007-11-30 11:30:34 -05001681static u64 stripe_align(struct btrfs_root *root, u64 val)
1682{
1683 u64 mask = ((u64)root->stripesize - 1);
1684 u64 ret = (val + mask) & ~mask;
1685 return ret;
1686}
1687
Chris Masonfec577f2007-02-26 10:40:21 -05001688/*
1689 * walks the btree of allocated extents and find a hole of a given size.
1690 * The key ins is changed to record the hole:
1691 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04001692 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05001693 * ins->offset == number of blocks
1694 * Any available blocks before search_start are skipped.
1695 */
Chris Mason98ed5172008-01-03 10:01:48 -05001696static int noinline find_free_extent(struct btrfs_trans_handle *trans,
1697 struct btrfs_root *orig_root,
1698 u64 num_bytes, u64 empty_size,
1699 u64 search_start, u64 search_end,
1700 u64 hint_byte, struct btrfs_key *ins,
1701 u64 exclude_start, u64 exclude_nr,
1702 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001703{
Chris Mason87ee04e2007-11-30 11:30:34 -05001704 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04001705 u64 orig_search_start;
Chris Mason9f5fae22007-03-20 14:38:32 -04001706 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -04001707 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001708 u64 total_needed = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04001709 u64 *last_ptr = NULL;
Chris Masonbe08c1b2007-05-03 09:06:49 -04001710 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -04001711 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -04001712 int wrapped = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001713 int chunk_alloc_done = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001714 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04001715 int allowed_chunk_alloc = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001716
Chris Masondb945352007-10-15 16:15:53 -04001717 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04001718 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1719
Chris Mason0ef3e662008-05-24 14:04:53 -04001720 if (orig_root->ref_cows || empty_size)
1721 allowed_chunk_alloc = 1;
1722
Chris Mason239b14b2008-03-24 15:02:07 -04001723 if (data & BTRFS_BLOCK_GROUP_METADATA) {
1724 last_ptr = &root->fs_info->last_alloc;
Chris Mason8790d502008-04-03 16:29:03 -04001725 empty_cluster = 256 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04001726 }
1727
1728 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
1729 last_ptr = &root->fs_info->last_data_alloc;
1730 }
1731
1732 if (last_ptr) {
1733 if (*last_ptr)
1734 hint_byte = *last_ptr;
1735 else {
1736 empty_size += empty_cluster;
1737 }
1738 }
1739
Chris Masona061fc82008-05-07 11:43:44 -04001740 search_start = max(search_start, first_logical_byte(root, 0));
1741 orig_search_start = search_start;
1742
Chris Mason7f93bf82008-03-24 15:01:28 -04001743 if (search_end == (u64)-1)
1744 search_end = btrfs_super_total_bytes(&info->super_copy);
Chris Mason0b86a832008-03-24 15:01:56 -04001745
Chris Masondb945352007-10-15 16:15:53 -04001746 if (hint_byte) {
Chris Mason0ef3e662008-05-24 14:04:53 -04001747 block_group = btrfs_lookup_first_block_group(info, hint_byte);
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001748 if (!block_group)
1749 hint_byte = search_start;
Chris Mason925baed2008-06-25 16:01:30 -04001750 block_group = __btrfs_find_block_group(root, block_group,
Chris Masondb945352007-10-15 16:15:53 -04001751 hint_byte, data, 1);
Chris Mason239b14b2008-03-24 15:02:07 -04001752 if (last_ptr && *last_ptr == 0 && block_group)
1753 hint_byte = block_group->key.objectid;
Chris Masonbe744172007-05-06 10:15:01 -04001754 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001755 block_group = __btrfs_find_block_group(root,
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001756 trans->block_group,
1757 search_start, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001758 }
Chris Mason239b14b2008-03-24 15:02:07 -04001759 search_start = max(search_start, hint_byte);
Chris Masonbe744172007-05-06 10:15:01 -04001760
Chris Mason6702ed42007-08-07 16:15:09 -04001761 total_needed += empty_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001762
Chris Masonbe744172007-05-06 10:15:01 -04001763check_failed:
Chris Mason70b043f2007-12-13 09:02:46 -05001764 if (!block_group) {
Chris Mason0ef3e662008-05-24 14:04:53 -04001765 block_group = btrfs_lookup_first_block_group(info,
1766 search_start);
Chris Mason70b043f2007-12-13 09:02:46 -05001767 if (!block_group)
Chris Mason0ef3e662008-05-24 14:04:53 -04001768 block_group = btrfs_lookup_first_block_group(info,
Chris Mason70b043f2007-12-13 09:02:46 -05001769 orig_search_start);
1770 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001771 if (full_scan && !chunk_alloc_done) {
1772 if (allowed_chunk_alloc) {
1773 do_chunk_alloc(trans, root,
1774 num_bytes + 2 * 1024 * 1024, data, 1);
1775 allowed_chunk_alloc = 0;
1776 } else if (block_group && block_group_bits(block_group, data)) {
1777 block_group->space_info->force_alloc = 1;
1778 }
1779 chunk_alloc_done = 1;
1780 }
Chris Mason0b86a832008-03-24 15:01:56 -04001781 ret = find_search_start(root, &block_group, &search_start,
1782 total_needed, data);
Chris Mason239b14b2008-03-24 15:02:07 -04001783 if (ret == -ENOSPC && last_ptr && *last_ptr) {
1784 *last_ptr = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001785 block_group = btrfs_lookup_first_block_group(info,
1786 orig_search_start);
Chris Mason239b14b2008-03-24 15:02:07 -04001787 search_start = orig_search_start;
1788 ret = find_search_start(root, &block_group, &search_start,
1789 total_needed, data);
1790 }
1791 if (ret == -ENOSPC)
1792 goto enospc;
Chris Mason0b86a832008-03-24 15:01:56 -04001793 if (ret)
1794 goto error;
1795
Chris Mason239b14b2008-03-24 15:02:07 -04001796 if (last_ptr && *last_ptr && search_start != *last_ptr) {
1797 *last_ptr = 0;
1798 if (!empty_size) {
1799 empty_size += empty_cluster;
1800 total_needed += empty_size;
1801 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001802 block_group = btrfs_lookup_first_block_group(info,
Chris Mason239b14b2008-03-24 15:02:07 -04001803 orig_search_start);
1804 search_start = orig_search_start;
1805 ret = find_search_start(root, &block_group,
1806 &search_start, total_needed, data);
1807 if (ret == -ENOSPC)
1808 goto enospc;
1809 if (ret)
1810 goto error;
1811 }
1812
Chris Mason87ee04e2007-11-30 11:30:34 -05001813 search_start = stripe_align(root, search_start);
Chris Masonfec577f2007-02-26 10:40:21 -05001814 ins->objectid = search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04001815 ins->offset = num_bytes;
Chris Masone37c9e62007-05-09 20:13:14 -04001816
Chris Masondb945352007-10-15 16:15:53 -04001817 if (ins->objectid + num_bytes >= search_end)
Chris Masoncf675822007-09-17 11:00:51 -04001818 goto enospc;
Chris Mason0b86a832008-03-24 15:01:56 -04001819
1820 if (ins->objectid + num_bytes >
1821 block_group->key.objectid + block_group->key.offset) {
Chris Masone19caa52007-10-15 16:17:44 -04001822 search_start = block_group->key.objectid +
1823 block_group->key.offset;
1824 goto new_group;
1825 }
Chris Mason0b86a832008-03-24 15:01:56 -04001826
Chris Mason1a5bc162007-10-15 16:15:26 -04001827 if (test_range_bit(&info->extent_ins, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001828 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1829 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001830 goto new_group;
1831 }
Chris Mason0b86a832008-03-24 15:01:56 -04001832
Chris Mason1a5bc162007-10-15 16:15:26 -04001833 if (test_range_bit(&info->pinned_extents, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001834 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1835 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001836 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001837 }
Chris Mason0b86a832008-03-24 15:01:56 -04001838
Chris Masondb945352007-10-15 16:15:53 -04001839 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
Chris Masonf2654de2007-06-26 12:20:46 -04001840 ins->objectid < exclude_start + exclude_nr)) {
1841 search_start = exclude_start + exclude_nr;
1842 goto new_group;
1843 }
Chris Mason0b86a832008-03-24 15:01:56 -04001844
Chris Mason6324fbf2008-03-24 15:01:59 -04001845 if (!(data & BTRFS_BLOCK_GROUP_DATA)) {
Chris Mason5276aed2007-06-11 21:33:38 -04001846 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Mason26b80032007-08-08 20:17:12 -04001847 if (block_group)
1848 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -04001849 }
Chris Masondb945352007-10-15 16:15:53 -04001850 ins->offset = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04001851 if (last_ptr) {
1852 *last_ptr = ins->objectid + ins->offset;
1853 if (*last_ptr ==
1854 btrfs_super_total_bytes(&root->fs_info->super_copy)) {
1855 *last_ptr = 0;
1856 }
1857 }
Chris Masonfec577f2007-02-26 10:40:21 -05001858 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001859
1860new_group:
Chris Masondb945352007-10-15 16:15:53 -04001861 if (search_start + num_bytes >= search_end) {
Chris Masoncf675822007-09-17 11:00:51 -04001862enospc:
Chris Masonbe744172007-05-06 10:15:01 -04001863 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001864 if (full_scan) {
1865 ret = -ENOSPC;
1866 goto error;
1867 }
Chris Mason6702ed42007-08-07 16:15:09 -04001868 if (wrapped) {
1869 if (!full_scan)
1870 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001871 full_scan = 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001872 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001873 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001874 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001875 block_group = btrfs_lookup_first_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001876 cond_resched();
Chris Mason925baed2008-06-25 16:01:30 -04001877 block_group = __btrfs_find_block_group(root, block_group,
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001878 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001879 goto check_failed;
1880
Chris Mason0f70abe2007-02-28 16:46:22 -05001881error:
Chris Mason0f70abe2007-02-28 16:46:22 -05001882 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001883}
Chris Masonec44a352008-04-28 15:29:52 -04001884
Chris Masonfec577f2007-02-26 10:40:21 -05001885/*
Chris Masonfec577f2007-02-26 10:40:21 -05001886 * finds a free extent and does all the dirty work required for allocation
1887 * returns the key for the extent through ins, and a tree buffer for
1888 * the first block of the extent through buf.
1889 *
1890 * returns 0 if everything worked, non-zero otherwise.
1891 */
Chris Mason4d775672007-04-20 20:23:12 -04001892int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
Chris Mason7bb86312007-12-11 09:25:06 -05001893 struct btrfs_root *root,
Chris Mason98d20f62008-04-14 09:46:10 -04001894 u64 num_bytes, u64 min_alloc_size,
1895 u64 root_objectid, u64 ref_generation,
Chris Mason7bb86312007-12-11 09:25:06 -05001896 u64 owner, u64 owner_offset,
1897 u64 empty_size, u64 hint_byte,
Chris Masonec44a352008-04-28 15:29:52 -04001898 u64 search_end, struct btrfs_key *ins, u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05001899{
1900 int ret;
1901 int pending_ret;
Chris Masonc31f8832008-01-08 15:46:31 -05001902 u64 super_used;
1903 u64 root_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001904 u64 search_start = 0;
Chris Mason8790d502008-04-03 16:29:03 -04001905 u64 alloc_profile;
Chris Mason47e4bb92008-02-01 14:51:59 -05001906 u32 sizes[2];
Chris Mason1261ec42007-03-20 20:35:03 -04001907 struct btrfs_fs_info *info = root->fs_info;
1908 struct btrfs_root *extent_root = info->extent_root;
Chris Mason47e4bb92008-02-01 14:51:59 -05001909 struct btrfs_extent_item *extent_item;
1910 struct btrfs_extent_ref *ref;
Chris Mason7bb86312007-12-11 09:25:06 -05001911 struct btrfs_path *path;
Chris Mason47e4bb92008-02-01 14:51:59 -05001912 struct btrfs_key keys[2];
Chris Mason8f662a72008-01-02 10:01:11 -05001913
Chris Mason925baed2008-06-25 16:01:30 -04001914 maybe_lock_mutex(root);
1915
Chris Mason6324fbf2008-03-24 15:01:59 -04001916 if (data) {
Chris Mason8790d502008-04-03 16:29:03 -04001917 alloc_profile = info->avail_data_alloc_bits &
1918 info->data_alloc_profile;
1919 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04001920 } else if (root == root->fs_info->chunk_root) {
Chris Mason8790d502008-04-03 16:29:03 -04001921 alloc_profile = info->avail_system_alloc_bits &
1922 info->system_alloc_profile;
1923 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04001924 } else {
Chris Mason8790d502008-04-03 16:29:03 -04001925 alloc_profile = info->avail_metadata_alloc_bits &
1926 info->metadata_alloc_profile;
1927 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04001928 }
Chris Mason98d20f62008-04-14 09:46:10 -04001929again:
Chris Masona061fc82008-05-07 11:43:44 -04001930 data = reduce_alloc_profile(root, data);
Chris Mason0ef3e662008-05-24 14:04:53 -04001931 /*
1932 * the only place that sets empty_size is btrfs_realloc_node, which
1933 * is not called recursively on allocations
1934 */
1935 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04001936 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04001937 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04001938 2 * 1024 * 1024,
1939 BTRFS_BLOCK_GROUP_METADATA |
1940 (info->metadata_alloc_profile &
1941 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04001942 BUG_ON(ret);
1943 }
1944 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04001945 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04001946 BUG_ON(ret);
1947 }
Chris Mason0b86a832008-03-24 15:01:56 -04001948
Chris Masondb945352007-10-15 16:15:53 -04001949 WARN_ON(num_bytes < root->sectorsize);
1950 ret = find_free_extent(trans, root, num_bytes, empty_size,
1951 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04001952 trans->alloc_exclude_start,
1953 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04001954
Chris Mason98d20f62008-04-14 09:46:10 -04001955 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
1956 num_bytes = num_bytes >> 1;
1957 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04001958 do_chunk_alloc(trans, root->fs_info->extent_root,
1959 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04001960 goto again;
1961 }
Chris Masonec44a352008-04-28 15:29:52 -04001962 if (ret) {
1963 printk("allocation failed flags %Lu\n", data);
1964 }
Chris Mason925baed2008-06-25 16:01:30 -04001965 if (ret) {
1966 BUG();
1967 goto out;
1968 }
Chris Masonf2654de2007-06-26 12:20:46 -04001969
Josef Bacik58176a92007-08-29 15:47:34 -04001970 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -04001971 super_used = btrfs_super_bytes_used(&info->super_copy);
1972 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Mason26b80032007-08-08 20:17:12 -04001973
Josef Bacik58176a92007-08-29 15:47:34 -04001974 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001975 root_used = btrfs_root_used(&root->root_item);
1976 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001977
Chris Masonf510cfe2007-10-15 16:14:48 -04001978 clear_extent_dirty(&root->fs_info->free_space_cache,
1979 ins->objectid, ins->objectid + ins->offset - 1,
1980 GFP_NOFS);
1981
Chris Mason26b80032007-08-08 20:17:12 -04001982 if (root == extent_root) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001983 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1984 ins->objectid + ins->offset - 1,
1985 EXTENT_LOCKED, GFP_NOFS);
Chris Mason26b80032007-08-08 20:17:12 -04001986 goto update_block;
1987 }
1988
1989 WARN_ON(trans->alloc_exclude_nr);
1990 trans->alloc_exclude_start = ins->objectid;
1991 trans->alloc_exclude_nr = ins->offset;
Chris Mason037e6392007-03-07 11:50:24 -05001992
Chris Mason47e4bb92008-02-01 14:51:59 -05001993 memcpy(&keys[0], ins, sizeof(*ins));
1994 keys[1].offset = hash_extent_ref(root_objectid, ref_generation,
1995 owner, owner_offset);
1996 keys[1].objectid = ins->objectid;
1997 keys[1].type = BTRFS_EXTENT_REF_KEY;
1998 sizes[0] = sizeof(*extent_item);
1999 sizes[1] = sizeof(*ref);
Chris Mason7bb86312007-12-11 09:25:06 -05002000
2001 path = btrfs_alloc_path();
2002 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05002003
2004 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
2005 sizes, 2);
Chris Mason26b80032007-08-08 20:17:12 -04002006
Chris Masonccd467d2007-06-28 15:57:36 -04002007 BUG_ON(ret);
Chris Mason47e4bb92008-02-01 14:51:59 -05002008 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2009 struct btrfs_extent_item);
2010 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
2011 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2012 struct btrfs_extent_ref);
2013
2014 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
2015 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
2016 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
2017 btrfs_set_ref_offset(path->nodes[0], ref, owner_offset);
2018
2019 btrfs_mark_buffer_dirty(path->nodes[0]);
2020
2021 trans->alloc_exclude_start = 0;
2022 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002023 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04002024 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04002025 pending_ret = del_pending_extents(trans, extent_root);
Chris Masonf510cfe2007-10-15 16:14:48 -04002026
Chris Mason925baed2008-06-25 16:01:30 -04002027 if (ret)
2028 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04002029 if (pending_ret) {
Chris Mason925baed2008-06-25 16:01:30 -04002030 ret = pending_ret;
2031 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04002032 }
Chris Mason26b80032007-08-08 20:17:12 -04002033
2034update_block:
Chris Mason0b86a832008-03-24 15:01:56 -04002035 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05002036 if (ret) {
2037 printk("update block group failed for %Lu %Lu\n",
2038 ins->objectid, ins->offset);
2039 BUG();
2040 }
Chris Mason925baed2008-06-25 16:01:30 -04002041out:
2042 maybe_unlock_mutex(root);
2043 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002044}
Chris Masonfec577f2007-02-26 10:40:21 -05002045/*
2046 * helper function to allocate a block for a given tree
2047 * returns the tree buffer or NULL.
2048 */
Chris Mason5f39d392007-10-15 16:14:19 -04002049struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04002050 struct btrfs_root *root,
Chris Mason7bb86312007-12-11 09:25:06 -05002051 u32 blocksize,
Chris Mason7bb86312007-12-11 09:25:06 -05002052 u64 root_objectid,
2053 u64 ref_generation,
2054 u64 first_objectid,
2055 int level,
2056 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04002057 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05002058{
Chris Masone2fa7222007-03-12 16:22:34 -04002059 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05002060 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04002061 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05002062
Chris Mason98d20f62008-04-14 09:46:10 -04002063 ret = btrfs_alloc_extent(trans, root, blocksize, blocksize,
Chris Mason7bb86312007-12-11 09:25:06 -05002064 root_objectid, ref_generation,
Chris Masonf6dbff52007-12-13 11:13:32 -05002065 level, first_objectid, empty_size, hint,
Chris Masondb945352007-10-15 16:15:53 -04002066 (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05002067 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04002068 BUG_ON(ret > 0);
2069 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05002070 }
Chris Masondb945352007-10-15 16:15:53 -04002071 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002072 if (!buf) {
Chris Mason7bb86312007-12-11 09:25:06 -05002073 btrfs_free_extent(trans, root, ins.objectid, blocksize,
2074 root->root_key.objectid, ref_generation,
2075 0, 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002076 return ERR_PTR(-ENOMEM);
2077 }
Chris Mason55c69072008-01-09 15:55:33 -05002078 btrfs_set_header_generation(buf, trans->transid);
Chris Mason925baed2008-06-25 16:01:30 -04002079 btrfs_tree_lock(buf);
Chris Mason55c69072008-01-09 15:55:33 -05002080 clean_tree_block(trans, root, buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002081 btrfs_set_buffer_uptodate(buf);
Chris Mason55c69072008-01-09 15:55:33 -05002082
2083 if (PageDirty(buf->first_page)) {
2084 printk("page %lu dirty\n", buf->first_page->index);
2085 WARN_ON(1);
2086 }
2087
Chris Mason5f39d392007-10-15 16:14:19 -04002088 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
2089 buf->start + buf->len - 1, GFP_NOFS);
Chris Mason9afbb0b2008-02-15 13:19:35 -05002090 if (!btrfs_test_opt(root, SSD))
2091 btrfs_set_buffer_defrag(buf);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002092 trans->blocks_used++;
Chris Masonfec577f2007-02-26 10:40:21 -05002093 return buf;
2094}
Chris Masona28ec192007-03-06 20:08:01 -05002095
Chris Mason98ed5172008-01-03 10:01:48 -05002096static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
2097 struct btrfs_root *root,
2098 struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04002099{
Chris Mason7bb86312007-12-11 09:25:06 -05002100 u64 leaf_owner;
2101 u64 leaf_generation;
Chris Mason5f39d392007-10-15 16:14:19 -04002102 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002103 struct btrfs_file_extent_item *fi;
2104 int i;
2105 int nritems;
2106 int ret;
2107
Chris Mason5f39d392007-10-15 16:14:19 -04002108 BUG_ON(!btrfs_is_leaf(leaf));
2109 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05002110 leaf_owner = btrfs_header_owner(leaf);
2111 leaf_generation = btrfs_header_generation(leaf);
2112
Chris Mason6407bf62007-03-27 06:33:00 -04002113 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002114 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04002115
2116 btrfs_item_key_to_cpu(leaf, &key, i);
2117 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04002118 continue;
2119 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002120 if (btrfs_file_extent_type(leaf, fi) ==
2121 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454d2007-04-19 13:37:44 -04002122 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04002123 /*
2124 * FIXME make sure to insert a trans record that
2125 * repeats the snapshot del on crash
2126 */
Chris Masondb945352007-10-15 16:15:53 -04002127 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2128 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04002129 continue;
Chris Mason925baed2008-06-25 16:01:30 -04002130 ret = __btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002131 btrfs_file_extent_disk_num_bytes(leaf, fi),
2132 leaf_owner, leaf_generation,
2133 key.objectid, key.offset, 0);
Chris Mason6407bf62007-03-27 06:33:00 -04002134 BUG_ON(ret);
2135 }
2136 return 0;
2137}
2138
Chris Mason98ed5172008-01-03 10:01:48 -05002139static void noinline reada_walk_down(struct btrfs_root *root,
Chris Masonbea495e2008-01-24 16:13:14 -05002140 struct extent_buffer *node,
2141 int slot)
Chris Masone0115992007-06-19 16:23:05 -04002142{
Chris Masondb945352007-10-15 16:15:53 -04002143 u64 bytenr;
Chris Masonbea495e2008-01-24 16:13:14 -05002144 u64 last = 0;
2145 u32 nritems;
Chris Masone0115992007-06-19 16:23:05 -04002146 u32 refs;
Chris Masondb945352007-10-15 16:15:53 -04002147 u32 blocksize;
Chris Masonbea495e2008-01-24 16:13:14 -05002148 int ret;
2149 int i;
2150 int level;
2151 int skipped = 0;
Chris Masone0115992007-06-19 16:23:05 -04002152
Chris Mason5f39d392007-10-15 16:14:19 -04002153 nritems = btrfs_header_nritems(node);
Chris Masondb945352007-10-15 16:15:53 -04002154 level = btrfs_header_level(node);
Chris Masonbea495e2008-01-24 16:13:14 -05002155 if (level)
2156 return;
2157
2158 for (i = slot; i < nritems && skipped < 32; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002159 bytenr = btrfs_node_blockptr(node, i);
Chris Masonbea495e2008-01-24 16:13:14 -05002160 if (last && ((bytenr > last && bytenr - last > 32 * 1024) ||
2161 (last > bytenr && last - bytenr > 32 * 1024))) {
2162 skipped++;
Chris Masone0115992007-06-19 16:23:05 -04002163 continue;
Chris Masonbea495e2008-01-24 16:13:14 -05002164 }
2165 blocksize = btrfs_level_size(root, level - 1);
2166 if (i != slot) {
2167 ret = lookup_extent_ref(NULL, root, bytenr,
2168 blocksize, &refs);
2169 BUG_ON(ret);
2170 if (refs != 1) {
2171 skipped++;
2172 continue;
2173 }
2174 }
Chris Mason409eb952007-08-08 20:17:12 -04002175 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masonca7a79a2008-05-12 12:59:19 -04002176 ret = readahead_tree_block(root, bytenr, blocksize,
2177 btrfs_node_ptr_generation(node, i));
Chris Masonbea495e2008-01-24 16:13:14 -05002178 last = bytenr + blocksize;
Chris Mason409eb952007-08-08 20:17:12 -04002179 cond_resched();
2180 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04002181 if (ret)
2182 break;
2183 }
2184}
2185
Chris Mason9aca1d52007-03-13 11:09:37 -04002186/*
2187 * helper function for drop_snapshot, this walks down the tree dropping ref
2188 * counts as it goes.
2189 */
Chris Mason98ed5172008-01-03 10:01:48 -05002190static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2191 struct btrfs_root *root,
2192 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05002193{
Chris Mason7bb86312007-12-11 09:25:06 -05002194 u64 root_owner;
2195 u64 root_gen;
2196 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04002197 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002198 struct extent_buffer *next;
2199 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05002200 struct extent_buffer *parent;
Chris Masondb945352007-10-15 16:15:53 -04002201 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05002202 int ret;
2203 u32 refs;
2204
Chris Mason925baed2008-06-25 16:01:30 -04002205 mutex_lock(&root->fs_info->alloc_mutex);
2206
Chris Mason5caf2a02007-04-02 11:20:42 -04002207 WARN_ON(*level < 0);
2208 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason5f39d392007-10-15 16:14:19 -04002209 ret = lookup_extent_ref(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04002210 path->nodes[*level]->start,
2211 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05002212 BUG_ON(ret);
2213 if (refs > 1)
2214 goto out;
Chris Masone0115992007-06-19 16:23:05 -04002215
Chris Mason9aca1d52007-03-13 11:09:37 -04002216 /*
2217 * walk down to the last node level and free all the leaves
2218 */
Chris Mason6407bf62007-03-27 06:33:00 -04002219 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002220 WARN_ON(*level < 0);
2221 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05002222 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04002223
Chris Mason5f39d392007-10-15 16:14:19 -04002224 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04002225 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04002226
Chris Mason7518a232007-03-12 12:01:18 -04002227 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04002228 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05002229 break;
Chris Mason6407bf62007-03-27 06:33:00 -04002230 if (*level == 0) {
2231 ret = drop_leaf_ref(trans, root, cur);
2232 BUG_ON(ret);
2233 break;
2234 }
Chris Masondb945352007-10-15 16:15:53 -04002235 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04002236 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04002237 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002238
Chris Masondb945352007-10-15 16:15:53 -04002239 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04002240 BUG_ON(ret);
2241 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05002242 parent = path->nodes[*level];
2243 root_owner = btrfs_header_owner(parent);
2244 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05002245 path->slots[*level]++;
Chris Mason925baed2008-06-25 16:01:30 -04002246 ret = __btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002247 blocksize, root_owner,
2248 root_gen, 0, 0, 1);
Chris Mason20524f02007-03-10 06:35:47 -05002249 BUG_ON(ret);
2250 continue;
2251 }
Chris Masondb945352007-10-15 16:15:53 -04002252 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason1259ab72008-05-12 13:39:03 -04002253 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
Chris Mason5f39d392007-10-15 16:14:19 -04002254 free_extent_buffer(next);
Chris Masonbea495e2008-01-24 16:13:14 -05002255 reada_walk_down(root, cur, path->slots[*level]);
Chris Masone9d0b132007-08-10 14:06:19 -04002256
Chris Mason8790d502008-04-03 16:29:03 -04002257 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason925baed2008-06-25 16:01:30 -04002258 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonca7a79a2008-05-12 12:59:19 -04002259 next = read_tree_block(root, bytenr, blocksize,
2260 ptr_gen);
Chris Mason8790d502008-04-03 16:29:03 -04002261 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason925baed2008-06-25 16:01:30 -04002262 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason8790d502008-04-03 16:29:03 -04002263
2264 /* we've dropped the lock, double check */
Chris Masondb945352007-10-15 16:15:53 -04002265 ret = lookup_extent_ref(trans, root, bytenr,
2266 blocksize, &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04002267 BUG_ON(ret);
2268 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05002269 parent = path->nodes[*level];
2270 root_owner = btrfs_header_owner(parent);
2271 root_gen = btrfs_header_generation(parent);
2272
Chris Masone9d0b132007-08-10 14:06:19 -04002273 path->slots[*level]++;
Chris Mason5f39d392007-10-15 16:14:19 -04002274 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04002275 ret = __btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002276 blocksize,
2277 root_owner,
2278 root_gen, 0, 0, 1);
Chris Masone9d0b132007-08-10 14:06:19 -04002279 BUG_ON(ret);
2280 continue;
2281 }
2282 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002283 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04002284 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04002285 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05002286 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04002287 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05002288 path->slots[*level] = 0;
2289 }
2290out:
Chris Mason5caf2a02007-04-02 11:20:42 -04002291 WARN_ON(*level < 0);
2292 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05002293
2294 if (path->nodes[*level] == root->node) {
2295 root_owner = root->root_key.objectid;
2296 parent = path->nodes[*level];
2297 } else {
2298 parent = path->nodes[*level + 1];
2299 root_owner = btrfs_header_owner(parent);
2300 }
2301
2302 root_gen = btrfs_header_generation(parent);
Chris Mason925baed2008-06-25 16:01:30 -04002303 ret = __btrfs_free_extent(trans, root, path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05002304 path->nodes[*level]->len,
2305 root_owner, root_gen, 0, 0, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002306 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05002307 path->nodes[*level] = NULL;
2308 *level += 1;
2309 BUG_ON(ret);
Chris Mason925baed2008-06-25 16:01:30 -04002310 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason20524f02007-03-10 06:35:47 -05002311 return 0;
2312}
2313
Chris Mason9aca1d52007-03-13 11:09:37 -04002314/*
2315 * helper for dropping snapshots. This walks back up the tree in the path
2316 * to find the first node higher up where we haven't yet gone through
2317 * all the slots
2318 */
Chris Mason98ed5172008-01-03 10:01:48 -05002319static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
2320 struct btrfs_root *root,
2321 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05002322{
Chris Mason7bb86312007-12-11 09:25:06 -05002323 u64 root_owner;
2324 u64 root_gen;
2325 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05002326 int i;
2327 int slot;
2328 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04002329
Chris Mason234b63a2007-03-13 10:46:10 -04002330 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05002331 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04002332 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
2333 struct extent_buffer *node;
2334 struct btrfs_disk_key disk_key;
2335 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05002336 path->slots[i]++;
2337 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04002338 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002339 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04002340 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04002341 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04002342 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05002343 return 0;
2344 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05002345 if (path->nodes[*level] == root->node) {
2346 root_owner = root->root_key.objectid;
2347 root_gen =
2348 btrfs_header_generation(path->nodes[*level]);
2349 } else {
2350 struct extent_buffer *node;
2351 node = path->nodes[*level + 1];
2352 root_owner = btrfs_header_owner(node);
2353 root_gen = btrfs_header_generation(node);
2354 }
Chris Masone089f052007-03-16 16:20:31 -04002355 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04002356 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05002357 path->nodes[*level]->len,
2358 root_owner, root_gen, 0, 0, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04002359 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002360 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04002361 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05002362 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05002363 }
2364 }
2365 return 1;
2366}
2367
Chris Mason9aca1d52007-03-13 11:09:37 -04002368/*
2369 * drop the reference count on the tree rooted at 'snap'. This traverses
2370 * the tree freeing any blocks that have a ref count of zero after being
2371 * decremented.
2372 */
Chris Masone089f052007-03-16 16:20:31 -04002373int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04002374 *root)
Chris Mason20524f02007-03-10 06:35:47 -05002375{
Chris Mason3768f362007-03-13 16:47:54 -04002376 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04002377 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05002378 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04002379 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05002380 int i;
2381 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002382 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05002383
Chris Mason5caf2a02007-04-02 11:20:42 -04002384 path = btrfs_alloc_path();
2385 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05002386
Chris Mason5f39d392007-10-15 16:14:19 -04002387 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05002388 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002389 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2390 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04002391 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04002392 path->slots[level] = 0;
2393 } else {
2394 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04002395 struct btrfs_disk_key found_key;
2396 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04002397
Chris Mason9f3a7422007-08-07 15:52:19 -04002398 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04002399 level = root_item->drop_level;
2400 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002401 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04002402 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04002403 ret = wret;
2404 goto out;
2405 }
Chris Mason5f39d392007-10-15 16:14:19 -04002406 node = path->nodes[level];
2407 btrfs_node_key(node, &found_key, path->slots[level]);
2408 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2409 sizeof(found_key)));
Chris Mason925baed2008-06-25 16:01:30 -04002410 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
2411 if (path->nodes[i] && path->locks[i]) {
2412 path->locks[i] = 0;
2413 btrfs_tree_unlock(path->nodes[i]);
2414 }
2415 }
Chris Mason9f3a7422007-08-07 15:52:19 -04002416 }
Chris Mason20524f02007-03-10 06:35:47 -05002417 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002418 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04002419 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05002420 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04002421 if (wret < 0)
2422 ret = wret;
2423
Chris Mason5caf2a02007-04-02 11:20:42 -04002424 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04002425 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05002426 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04002427 if (wret < 0)
2428 ret = wret;
Chris Mason409eb952007-08-08 20:17:12 -04002429 ret = -EAGAIN;
Chris Mason409eb952007-08-08 20:17:12 -04002430 break;
Chris Mason20524f02007-03-10 06:35:47 -05002431 }
Chris Mason83e15a22007-03-12 09:03:27 -04002432 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002433 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04002434 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04002435 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04002436 }
Chris Mason20524f02007-03-10 06:35:47 -05002437 }
Chris Mason9f3a7422007-08-07 15:52:19 -04002438out:
Chris Mason5caf2a02007-04-02 11:20:42 -04002439 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04002440 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05002441}
Chris Mason9078a3e2007-04-26 16:46:15 -04002442
Chris Masonbe744172007-05-06 10:15:01 -04002443int btrfs_free_block_groups(struct btrfs_fs_info *info)
2444{
Chris Masonf510cfe2007-10-15 16:14:48 -04002445 u64 start;
2446 u64 end;
Yanb97f9202007-11-01 11:28:41 -04002447 u64 ptr;
Chris Mason96b51792007-10-15 16:15:19 -04002448 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04002449
2450 mutex_lock(&info->alloc_mutex);
Chris Mason96b51792007-10-15 16:15:19 -04002451 while(1) {
2452 ret = find_first_extent_bit(&info->block_group_cache, 0,
2453 &start, &end, (unsigned int)-1);
2454 if (ret)
2455 break;
Yanb97f9202007-11-01 11:28:41 -04002456 ret = get_state_private(&info->block_group_cache, start, &ptr);
2457 if (!ret)
2458 kfree((void *)(unsigned long)ptr);
Chris Mason96b51792007-10-15 16:15:19 -04002459 clear_extent_bits(&info->block_group_cache, start,
2460 end, (unsigned int)-1, GFP_NOFS);
2461 }
Chris Masone37c9e62007-05-09 20:13:14 -04002462 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -04002463 ret = find_first_extent_bit(&info->free_space_cache, 0,
2464 &start, &end, EXTENT_DIRTY);
2465 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -04002466 break;
Chris Masonf510cfe2007-10-15 16:14:48 -04002467 clear_extent_dirty(&info->free_space_cache, start,
2468 end, GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04002469 }
Chris Mason925baed2008-06-25 16:01:30 -04002470 mutex_unlock(&info->alloc_mutex);
Chris Masonbe744172007-05-06 10:15:01 -04002471 return 0;
2472}
2473
Chris Mason8e7bf942008-04-28 09:02:36 -04002474static unsigned long calc_ra(unsigned long start, unsigned long last,
2475 unsigned long nr)
2476{
2477 return min(last, start + nr - 1);
2478}
2479
Chris Mason98ed5172008-01-03 10:01:48 -05002480static int noinline relocate_inode_pages(struct inode *inode, u64 start,
2481 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05002482{
2483 u64 page_start;
2484 u64 page_end;
Chris Masonedbd8d42007-12-21 16:27:24 -05002485 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05002486 unsigned long i;
2487 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05002488 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05002489 struct file_ra_state *ra;
Chris Mason8e7bf942008-04-28 09:02:36 -04002490 unsigned long total_read = 0;
2491 unsigned long ra_pages;
Chris Masona061fc82008-05-07 11:43:44 -04002492 struct btrfs_trans_handle *trans;
Chris Mason4313b392008-01-03 09:08:48 -05002493
2494 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002495
2496 mutex_lock(&inode->i_mutex);
Chris Mason4313b392008-01-03 09:08:48 -05002497 i = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05002498 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
2499
Chris Mason8e7bf942008-04-28 09:02:36 -04002500 ra_pages = BTRFS_I(inode)->root->fs_info->bdi.ra_pages;
2501
Chris Mason4313b392008-01-03 09:08:48 -05002502 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05002503
Chris Mason4313b392008-01-03 09:08:48 -05002504 for (; i <= last_index; i++) {
Chris Mason8e7bf942008-04-28 09:02:36 -04002505 if (total_read % ra_pages == 0) {
2506 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
2507 calc_ra(i, last_index, ra_pages));
2508 }
2509 total_read++;
Chris Masona061fc82008-05-07 11:43:44 -04002510 if (((u64)i << PAGE_CACHE_SHIFT) > inode->i_size)
2511 goto truncate_racing;
2512
Chris Masonedbd8d42007-12-21 16:27:24 -05002513 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04002514 if (!page) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002515 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04002516 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002517 if (!PageUptodate(page)) {
2518 btrfs_readpage(NULL, page);
2519 lock_page(page);
2520 if (!PageUptodate(page)) {
2521 unlock_page(page);
2522 page_cache_release(page);
2523 goto out_unlock;
2524 }
2525 }
Chris Masonec44a352008-04-28 15:29:52 -04002526#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2527 ClearPageDirty(page);
2528#else
2529 cancel_dirty_page(page, PAGE_CACHE_SIZE);
2530#endif
2531 wait_on_page_writeback(page);
2532 set_page_extent_mapped(page);
Chris Masonedbd8d42007-12-21 16:27:24 -05002533 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2534 page_end = page_start + PAGE_CACHE_SIZE - 1;
2535
Chris Masond1310b22008-01-24 16:13:08 -05002536 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002537
Chris Masond1310b22008-01-24 16:13:08 -05002538 set_extent_delalloc(io_tree, page_start,
Chris Masonedbd8d42007-12-21 16:27:24 -05002539 page_end, GFP_NOFS);
Chris Masona061fc82008-05-07 11:43:44 -04002540 set_page_dirty(page);
Chris Masonedbd8d42007-12-21 16:27:24 -05002541
Chris Masond1310b22008-01-24 16:13:08 -05002542 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002543 unlock_page(page);
2544 page_cache_release(page);
2545 }
Chris Masona061fc82008-05-07 11:43:44 -04002546 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
2547 total_read);
Chris Masonedbd8d42007-12-21 16:27:24 -05002548
2549out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04002550 kfree(ra);
Chris Masona061fc82008-05-07 11:43:44 -04002551 trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1);
2552 if (trans) {
2553 btrfs_add_ordered_inode(inode);
2554 btrfs_end_transaction(trans, BTRFS_I(inode)->root);
2555 mark_inode_dirty(inode);
2556 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002557 mutex_unlock(&inode->i_mutex);
2558 return 0;
Chris Masona061fc82008-05-07 11:43:44 -04002559
2560truncate_racing:
2561 vmtruncate(inode, inode->i_size);
2562 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
2563 total_read);
2564 goto out_unlock;
Chris Masonedbd8d42007-12-21 16:27:24 -05002565}
2566
Chris Mason4313b392008-01-03 09:08:48 -05002567/*
Chris Masonbf4ef672008-05-08 13:26:18 -04002568 * The back references tell us which tree holds a ref on a block,
2569 * but it is possible for the tree root field in the reference to
2570 * reflect the original root before a snapshot was made. In this
2571 * case we should search through all the children of a given root
2572 * to find potential holders of references on a block.
2573 *
2574 * Instead, we do something a little less fancy and just search
2575 * all the roots for a given key/block combination.
2576 */
2577static int find_root_for_ref(struct btrfs_root *root,
2578 struct btrfs_path *path,
2579 struct btrfs_key *key0,
2580 int level,
2581 int file_key,
2582 struct btrfs_root **found_root,
2583 u64 bytenr)
2584{
2585 struct btrfs_key root_location;
2586 struct btrfs_root *cur_root = *found_root;
2587 struct btrfs_file_extent_item *file_extent;
2588 u64 root_search_start = BTRFS_FS_TREE_OBJECTID;
2589 u64 found_bytenr;
2590 int ret;
2591 int i;
2592
2593 root_location.offset = (u64)-1;
2594 root_location.type = BTRFS_ROOT_ITEM_KEY;
2595 path->lowest_level = level;
2596 path->reada = 0;
2597 while(1) {
2598 ret = btrfs_search_slot(NULL, cur_root, key0, path, 0, 0);
2599 found_bytenr = 0;
2600 if (ret == 0 && file_key) {
2601 struct extent_buffer *leaf = path->nodes[0];
2602 file_extent = btrfs_item_ptr(leaf, path->slots[0],
2603 struct btrfs_file_extent_item);
2604 if (btrfs_file_extent_type(leaf, file_extent) ==
2605 BTRFS_FILE_EXTENT_REG) {
2606 found_bytenr =
2607 btrfs_file_extent_disk_bytenr(leaf,
2608 file_extent);
2609 }
Chris Mason323da792008-05-09 11:46:48 -04002610 } else if (!file_key) {
Chris Masonbf4ef672008-05-08 13:26:18 -04002611 if (path->nodes[level])
2612 found_bytenr = path->nodes[level]->start;
2613 }
2614
2615 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2616 if (!path->nodes[i])
2617 break;
2618 free_extent_buffer(path->nodes[i]);
2619 path->nodes[i] = NULL;
2620 }
2621 btrfs_release_path(cur_root, path);
2622
2623 if (found_bytenr == bytenr) {
2624 *found_root = cur_root;
2625 ret = 0;
2626 goto out;
2627 }
2628 ret = btrfs_search_root(root->fs_info->tree_root,
2629 root_search_start, &root_search_start);
2630 if (ret)
2631 break;
2632
2633 root_location.objectid = root_search_start;
2634 cur_root = btrfs_read_fs_root_no_name(root->fs_info,
2635 &root_location);
2636 if (!cur_root) {
2637 ret = 1;
2638 break;
2639 }
2640 }
2641out:
2642 path->lowest_level = 0;
2643 return ret;
2644}
2645
2646/*
Chris Mason4313b392008-01-03 09:08:48 -05002647 * note, this releases the path
2648 */
Chris Mason98ed5172008-01-03 10:01:48 -05002649static int noinline relocate_one_reference(struct btrfs_root *extent_root,
Chris Masonedbd8d42007-12-21 16:27:24 -05002650 struct btrfs_path *path,
Chris Mason0ef3e662008-05-24 14:04:53 -04002651 struct btrfs_key *extent_key,
2652 u64 *last_file_objectid,
2653 u64 *last_file_offset,
2654 u64 *last_file_root,
2655 u64 last_extent)
Chris Masonedbd8d42007-12-21 16:27:24 -05002656{
2657 struct inode *inode;
2658 struct btrfs_root *found_root;
Chris Masonbf4ef672008-05-08 13:26:18 -04002659 struct btrfs_key root_location;
2660 struct btrfs_key found_key;
Chris Mason4313b392008-01-03 09:08:48 -05002661 struct btrfs_extent_ref *ref;
2662 u64 ref_root;
2663 u64 ref_gen;
2664 u64 ref_objectid;
2665 u64 ref_offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05002666 int ret;
Chris Masonbf4ef672008-05-08 13:26:18 -04002667 int level;
Chris Masonedbd8d42007-12-21 16:27:24 -05002668
Chris Mason4313b392008-01-03 09:08:48 -05002669 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
2670 struct btrfs_extent_ref);
2671 ref_root = btrfs_ref_root(path->nodes[0], ref);
2672 ref_gen = btrfs_ref_generation(path->nodes[0], ref);
2673 ref_objectid = btrfs_ref_objectid(path->nodes[0], ref);
2674 ref_offset = btrfs_ref_offset(path->nodes[0], ref);
2675 btrfs_release_path(extent_root, path);
2676
Chris Masonbf4ef672008-05-08 13:26:18 -04002677 root_location.objectid = ref_root;
Chris Masonedbd8d42007-12-21 16:27:24 -05002678 if (ref_gen == 0)
Chris Masonbf4ef672008-05-08 13:26:18 -04002679 root_location.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05002680 else
Chris Masonbf4ef672008-05-08 13:26:18 -04002681 root_location.offset = (u64)-1;
2682 root_location.type = BTRFS_ROOT_ITEM_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05002683
2684 found_root = btrfs_read_fs_root_no_name(extent_root->fs_info,
Chris Masonbf4ef672008-05-08 13:26:18 -04002685 &root_location);
Chris Masonedbd8d42007-12-21 16:27:24 -05002686 BUG_ON(!found_root);
2687
2688 if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Chris Masonbf4ef672008-05-08 13:26:18 -04002689 found_key.objectid = ref_objectid;
2690 found_key.type = BTRFS_EXTENT_DATA_KEY;
2691 found_key.offset = ref_offset;
2692 level = 0;
2693
Chris Mason0ef3e662008-05-24 14:04:53 -04002694 if (last_extent == extent_key->objectid &&
2695 *last_file_objectid == ref_objectid &&
2696 *last_file_offset == ref_offset &&
2697 *last_file_root == ref_root)
2698 goto out;
2699
Chris Masonbf4ef672008-05-08 13:26:18 -04002700 ret = find_root_for_ref(extent_root, path, &found_key,
2701 level, 1, &found_root,
2702 extent_key->objectid);
2703
2704 if (ret)
2705 goto out;
2706
Chris Mason0ef3e662008-05-24 14:04:53 -04002707 if (last_extent == extent_key->objectid &&
2708 *last_file_objectid == ref_objectid &&
2709 *last_file_offset == ref_offset &&
2710 *last_file_root == ref_root)
2711 goto out;
2712
Chris Masonedbd8d42007-12-21 16:27:24 -05002713 mutex_unlock(&extent_root->fs_info->fs_mutex);
2714 inode = btrfs_iget_locked(extent_root->fs_info->sb,
2715 ref_objectid, found_root);
2716 if (inode->i_state & I_NEW) {
2717 /* the inode and parent dir are two different roots */
2718 BTRFS_I(inode)->root = found_root;
2719 BTRFS_I(inode)->location.objectid = ref_objectid;
2720 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
2721 BTRFS_I(inode)->location.offset = 0;
2722 btrfs_read_locked_inode(inode);
2723 unlock_new_inode(inode);
2724
2725 }
2726 /* this can happen if the reference is not against
2727 * the latest version of the tree root
2728 */
2729 if (is_bad_inode(inode)) {
2730 mutex_lock(&extent_root->fs_info->fs_mutex);
2731 goto out;
2732 }
Chris Mason0ef3e662008-05-24 14:04:53 -04002733 *last_file_objectid = inode->i_ino;
2734 *last_file_root = found_root->root_key.objectid;
2735 *last_file_offset = ref_offset;
2736
Chris Masonedbd8d42007-12-21 16:27:24 -05002737 relocate_inode_pages(inode, ref_offset, extent_key->offset);
Chris Masonedbd8d42007-12-21 16:27:24 -05002738 iput(inode);
2739 mutex_lock(&extent_root->fs_info->fs_mutex);
2740 } else {
2741 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05002742 struct extent_buffer *eb;
Chris Masonedbd8d42007-12-21 16:27:24 -05002743 int i;
2744
Chris Masonedbd8d42007-12-21 16:27:24 -05002745 eb = read_tree_block(found_root, extent_key->objectid,
Chris Masonca7a79a2008-05-12 12:59:19 -04002746 extent_key->offset, 0);
Chris Mason925baed2008-06-25 16:01:30 -04002747 btrfs_tree_lock(eb);
Chris Masonedbd8d42007-12-21 16:27:24 -05002748 level = btrfs_header_level(eb);
2749
2750 if (level == 0)
2751 btrfs_item_key_to_cpu(eb, &found_key, 0);
2752 else
2753 btrfs_node_key_to_cpu(eb, &found_key, 0);
2754
Chris Mason925baed2008-06-25 16:01:30 -04002755 btrfs_tree_unlock(eb);
Chris Masonedbd8d42007-12-21 16:27:24 -05002756 free_extent_buffer(eb);
2757
Chris Masonbf4ef672008-05-08 13:26:18 -04002758 ret = find_root_for_ref(extent_root, path, &found_key,
2759 level, 0, &found_root,
2760 extent_key->objectid);
2761
2762 if (ret)
2763 goto out;
2764
2765 trans = btrfs_start_transaction(found_root, 1);
2766
Chris Masonedbd8d42007-12-21 16:27:24 -05002767 path->lowest_level = level;
Chris Mason8f662a72008-01-02 10:01:11 -05002768 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05002769 ret = btrfs_search_slot(trans, found_root, &found_key, path,
2770 0, 1);
2771 path->lowest_level = 0;
2772 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2773 if (!path->nodes[i])
2774 break;
2775 free_extent_buffer(path->nodes[i]);
2776 path->nodes[i] = NULL;
2777 }
2778 btrfs_release_path(found_root, path);
Chris Mason0ef3e662008-05-24 14:04:53 -04002779 if (found_root == found_root->fs_info->extent_root)
2780 btrfs_extent_post_op(trans, found_root);
Chris Masonedbd8d42007-12-21 16:27:24 -05002781 btrfs_end_transaction(trans, found_root);
2782 }
2783
2784out:
2785 return 0;
2786}
2787
Chris Masona061fc82008-05-07 11:43:44 -04002788static int noinline del_extent_zero(struct btrfs_root *extent_root,
2789 struct btrfs_path *path,
2790 struct btrfs_key *extent_key)
2791{
2792 int ret;
2793 struct btrfs_trans_handle *trans;
2794
2795 trans = btrfs_start_transaction(extent_root, 1);
2796 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
2797 if (ret > 0) {
2798 ret = -EIO;
2799 goto out;
2800 }
2801 if (ret < 0)
2802 goto out;
2803 ret = btrfs_del_item(trans, extent_root, path);
2804out:
2805 btrfs_end_transaction(trans, extent_root);
2806 return ret;
2807}
2808
Chris Mason98ed5172008-01-03 10:01:48 -05002809static int noinline relocate_one_extent(struct btrfs_root *extent_root,
2810 struct btrfs_path *path,
2811 struct btrfs_key *extent_key)
Chris Masonedbd8d42007-12-21 16:27:24 -05002812{
2813 struct btrfs_key key;
2814 struct btrfs_key found_key;
Chris Masonedbd8d42007-12-21 16:27:24 -05002815 struct extent_buffer *leaf;
Chris Mason0ef3e662008-05-24 14:04:53 -04002816 u64 last_file_objectid = 0;
2817 u64 last_file_root = 0;
2818 u64 last_file_offset = (u64)-1;
2819 u64 last_extent = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05002820 u32 nritems;
2821 u32 item_size;
2822 int ret = 0;
2823
Chris Masona061fc82008-05-07 11:43:44 -04002824 if (extent_key->objectid == 0) {
2825 ret = del_extent_zero(extent_root, path, extent_key);
2826 goto out;
2827 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002828 key.objectid = extent_key->objectid;
2829 key.type = BTRFS_EXTENT_REF_KEY;
2830 key.offset = 0;
2831
2832 while(1) {
2833 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2834
Chris Masonedbd8d42007-12-21 16:27:24 -05002835 if (ret < 0)
2836 goto out;
2837
2838 ret = 0;
2839 leaf = path->nodes[0];
2840 nritems = btrfs_header_nritems(leaf);
Chris Masona061fc82008-05-07 11:43:44 -04002841 if (path->slots[0] == nritems) {
2842 ret = btrfs_next_leaf(extent_root, path);
2843 if (ret > 0) {
2844 ret = 0;
2845 goto out;
2846 }
2847 if (ret < 0)
2848 goto out;
Chris Masonbf4ef672008-05-08 13:26:18 -04002849 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04002850 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002851
2852 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masona061fc82008-05-07 11:43:44 -04002853 if (found_key.objectid != extent_key->objectid) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002854 break;
Chris Masona061fc82008-05-07 11:43:44 -04002855 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002856
Chris Masona061fc82008-05-07 11:43:44 -04002857 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002858 break;
Chris Masona061fc82008-05-07 11:43:44 -04002859 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002860
2861 key.offset = found_key.offset + 1;
2862 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2863
Chris Mason0ef3e662008-05-24 14:04:53 -04002864 ret = relocate_one_reference(extent_root, path, extent_key,
2865 &last_file_objectid,
2866 &last_file_offset,
2867 &last_file_root, last_extent);
Chris Masonedbd8d42007-12-21 16:27:24 -05002868 if (ret)
2869 goto out;
Chris Mason0ef3e662008-05-24 14:04:53 -04002870 last_extent = extent_key->objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05002871 }
2872 ret = 0;
2873out:
2874 btrfs_release_path(extent_root, path);
2875 return ret;
2876}
2877
Chris Masonec44a352008-04-28 15:29:52 -04002878static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
2879{
2880 u64 num_devices;
2881 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
2882 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
2883
Chris Masona061fc82008-05-07 11:43:44 -04002884 num_devices = root->fs_info->fs_devices->num_devices;
Chris Masonec44a352008-04-28 15:29:52 -04002885 if (num_devices == 1) {
2886 stripped |= BTRFS_BLOCK_GROUP_DUP;
2887 stripped = flags & ~stripped;
2888
2889 /* turn raid0 into single device chunks */
2890 if (flags & BTRFS_BLOCK_GROUP_RAID0)
2891 return stripped;
2892
2893 /* turn mirroring into duplication */
2894 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
2895 BTRFS_BLOCK_GROUP_RAID10))
2896 return stripped | BTRFS_BLOCK_GROUP_DUP;
2897 return flags;
2898 } else {
2899 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04002900 if (flags & stripped)
2901 return flags;
2902
2903 stripped |= BTRFS_BLOCK_GROUP_DUP;
2904 stripped = flags & ~stripped;
2905
2906 /* switch duplicated blocks with raid1 */
2907 if (flags & BTRFS_BLOCK_GROUP_DUP)
2908 return stripped | BTRFS_BLOCK_GROUP_RAID1;
2909
2910 /* turn single device chunks into raid0 */
2911 return stripped | BTRFS_BLOCK_GROUP_RAID0;
2912 }
2913 return flags;
2914}
2915
Chris Mason0ef3e662008-05-24 14:04:53 -04002916int __alloc_chunk_for_shrink(struct btrfs_root *root,
2917 struct btrfs_block_group_cache *shrink_block_group,
2918 int force)
2919{
2920 struct btrfs_trans_handle *trans;
2921 u64 new_alloc_flags;
2922 u64 calc;
2923
2924 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
2925
2926 trans = btrfs_start_transaction(root, 1);
2927 new_alloc_flags = update_block_group_flags(root,
2928 shrink_block_group->flags);
2929 if (new_alloc_flags != shrink_block_group->flags) {
2930 calc =
2931 btrfs_block_group_used(&shrink_block_group->item);
2932 } else {
2933 calc = shrink_block_group->key.offset;
2934 }
2935 do_chunk_alloc(trans, root->fs_info->extent_root,
2936 calc + 2 * 1024 * 1024, new_alloc_flags, force);
2937 btrfs_end_transaction(trans, root);
2938 }
2939 return 0;
2940}
2941
Chris Mason8f18cf12008-04-25 16:53:30 -04002942int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 shrink_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05002943{
2944 struct btrfs_trans_handle *trans;
2945 struct btrfs_root *tree_root = root->fs_info->tree_root;
2946 struct btrfs_path *path;
2947 u64 cur_byte;
2948 u64 total_found;
Chris Mason8f18cf12008-04-25 16:53:30 -04002949 u64 shrink_last_byte;
2950 struct btrfs_block_group_cache *shrink_block_group;
Chris Masonedbd8d42007-12-21 16:27:24 -05002951 struct btrfs_fs_info *info = root->fs_info;
Chris Masonedbd8d42007-12-21 16:27:24 -05002952 struct btrfs_key key;
Yan73e48b22008-01-03 14:14:39 -05002953 struct btrfs_key found_key;
Chris Masonedbd8d42007-12-21 16:27:24 -05002954 struct extent_buffer *leaf;
2955 u32 nritems;
2956 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04002957 int progress;
Chris Masonedbd8d42007-12-21 16:27:24 -05002958
Chris Mason925baed2008-06-25 16:01:30 -04002959 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002960 shrink_block_group = btrfs_lookup_block_group(root->fs_info,
2961 shrink_start);
2962 BUG_ON(!shrink_block_group);
2963
Chris Mason0ef3e662008-05-24 14:04:53 -04002964 shrink_last_byte = shrink_block_group->key.objectid +
2965 shrink_block_group->key.offset;
Chris Mason8f18cf12008-04-25 16:53:30 -04002966
2967 shrink_block_group->space_info->total_bytes -=
2968 shrink_block_group->key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05002969 path = btrfs_alloc_path();
2970 root = root->fs_info->extent_root;
Chris Mason8f662a72008-01-02 10:01:11 -05002971 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05002972
Chris Mason323da792008-05-09 11:46:48 -04002973 printk("btrfs relocating block group %llu flags %llu\n",
2974 (unsigned long long)shrink_start,
2975 (unsigned long long)shrink_block_group->flags);
2976
Chris Mason0ef3e662008-05-24 14:04:53 -04002977 __alloc_chunk_for_shrink(root, shrink_block_group, 1);
Chris Mason323da792008-05-09 11:46:48 -04002978
Chris Mason0ef3e662008-05-24 14:04:53 -04002979again:
2980
Chris Mason8f18cf12008-04-25 16:53:30 -04002981 shrink_block_group->ro = 1;
2982
Chris Masonedbd8d42007-12-21 16:27:24 -05002983 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04002984 progress = 0;
Chris Mason8f18cf12008-04-25 16:53:30 -04002985 key.objectid = shrink_start;
Chris Masonedbd8d42007-12-21 16:27:24 -05002986 key.offset = 0;
2987 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05002988 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05002989
Yan73e48b22008-01-03 14:14:39 -05002990 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2991 if (ret < 0)
2992 goto out;
2993
Chris Mason0b86a832008-03-24 15:01:56 -04002994 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
Yan73e48b22008-01-03 14:14:39 -05002995 if (ret < 0)
2996 goto out;
Chris Mason8f18cf12008-04-25 16:53:30 -04002997
Yan73e48b22008-01-03 14:14:39 -05002998 if (ret == 0) {
2999 leaf = path->nodes[0];
3000 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04003001 if (found_key.objectid + found_key.offset > shrink_start &&
3002 found_key.objectid < shrink_last_byte) {
Yan73e48b22008-01-03 14:14:39 -05003003 cur_byte = found_key.objectid;
3004 key.objectid = cur_byte;
3005 }
3006 }
3007 btrfs_release_path(root, path);
3008
3009 while(1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05003010 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3011 if (ret < 0)
3012 goto out;
Yan73e48b22008-01-03 14:14:39 -05003013
Chris Masonedbd8d42007-12-21 16:27:24 -05003014 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05003015 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05003016next:
3017 if (path->slots[0] >= nritems) {
3018 ret = btrfs_next_leaf(root, path);
3019 if (ret < 0)
3020 goto out;
3021 if (ret == 1) {
3022 ret = 0;
3023 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05003024 }
Yan73e48b22008-01-03 14:14:39 -05003025 leaf = path->nodes[0];
3026 nritems = btrfs_header_nritems(leaf);
3027 }
3028
3029 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05003030
Chris Mason8f18cf12008-04-25 16:53:30 -04003031 if (found_key.objectid >= shrink_last_byte)
3032 break;
3033
Chris Mason725c8462008-01-04 16:47:16 -05003034 if (progress && need_resched()) {
3035 memcpy(&key, &found_key, sizeof(key));
3036 mutex_unlock(&root->fs_info->fs_mutex);
3037 cond_resched();
3038 mutex_lock(&root->fs_info->fs_mutex);
3039 btrfs_release_path(root, path);
3040 btrfs_search_slot(NULL, root, &key, path, 0, 0);
3041 progress = 0;
3042 goto next;
3043 }
3044 progress = 1;
3045
Yan73e48b22008-01-03 14:14:39 -05003046 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY ||
3047 found_key.objectid + found_key.offset <= cur_byte) {
Chris Mason0ef3e662008-05-24 14:04:53 -04003048 memcpy(&key, &found_key, sizeof(key));
3049 key.offset++;
Yan73e48b22008-01-03 14:14:39 -05003050 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05003051 goto next;
3052 }
Yan73e48b22008-01-03 14:14:39 -05003053
Chris Masonedbd8d42007-12-21 16:27:24 -05003054 total_found++;
3055 cur_byte = found_key.objectid + found_key.offset;
3056 key.objectid = cur_byte;
3057 btrfs_release_path(root, path);
3058 ret = relocate_one_extent(root, path, &found_key);
Chris Mason0ef3e662008-05-24 14:04:53 -04003059 __alloc_chunk_for_shrink(root, shrink_block_group, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05003060 }
3061
3062 btrfs_release_path(root, path);
3063
3064 if (total_found > 0) {
Chris Mason323da792008-05-09 11:46:48 -04003065 printk("btrfs relocate found %llu last extent was %llu\n",
3066 (unsigned long long)total_found,
3067 (unsigned long long)found_key.objectid);
Chris Masonedbd8d42007-12-21 16:27:24 -05003068 trans = btrfs_start_transaction(tree_root, 1);
3069 btrfs_commit_transaction(trans, tree_root);
3070
3071 mutex_unlock(&root->fs_info->fs_mutex);
3072 btrfs_clean_old_snapshots(tree_root);
3073 mutex_lock(&root->fs_info->fs_mutex);
3074
3075 trans = btrfs_start_transaction(tree_root, 1);
3076 btrfs_commit_transaction(trans, tree_root);
3077 goto again;
3078 }
3079
Chris Mason8f18cf12008-04-25 16:53:30 -04003080 /*
3081 * we've freed all the extents, now remove the block
3082 * group item from the tree
3083 */
Chris Masonedbd8d42007-12-21 16:27:24 -05003084 trans = btrfs_start_transaction(root, 1);
Chris Mason8f18cf12008-04-25 16:53:30 -04003085 memcpy(&key, &shrink_block_group->key, sizeof(key));
Chris Mason4313b392008-01-03 09:08:48 -05003086
Chris Mason8f18cf12008-04-25 16:53:30 -04003087 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3088 if (ret > 0)
3089 ret = -EIO;
3090 if (ret < 0)
3091 goto out;
Yan73e48b22008-01-03 14:14:39 -05003092
Chris Mason0ef3e662008-05-24 14:04:53 -04003093 clear_extent_bits(&info->block_group_cache, key.objectid,
3094 key.objectid + key.offset - 1,
Chris Mason8f18cf12008-04-25 16:53:30 -04003095 (unsigned int)-1, GFP_NOFS);
Yan73e48b22008-01-03 14:14:39 -05003096
Chris Mason0ef3e662008-05-24 14:04:53 -04003097
3098 clear_extent_bits(&info->free_space_cache,
3099 key.objectid, key.objectid + key.offset - 1,
3100 (unsigned int)-1, GFP_NOFS);
3101
3102 memset(shrink_block_group, 0, sizeof(*shrink_block_group));
3103 kfree(shrink_block_group);
3104
Chris Mason8f18cf12008-04-25 16:53:30 -04003105 btrfs_del_item(trans, root, path);
Chris Masonedbd8d42007-12-21 16:27:24 -05003106 btrfs_commit_transaction(trans, root);
Chris Mason0ef3e662008-05-24 14:04:53 -04003107
3108 /* the code to unpin extents might set a few bits in the free
3109 * space cache for this range again
3110 */
3111 clear_extent_bits(&info->free_space_cache,
3112 key.objectid, key.objectid + key.offset - 1,
3113 (unsigned int)-1, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003114out:
3115 btrfs_free_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04003116 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003117 return ret;
3118}
3119
Chris Mason0b86a832008-03-24 15:01:56 -04003120int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
3121 struct btrfs_key *key)
3122{
Chris Mason925baed2008-06-25 16:01:30 -04003123 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003124 struct btrfs_key found_key;
3125 struct extent_buffer *leaf;
3126 int slot;
3127
3128 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
3129 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04003130 goto out;
3131
Chris Mason0b86a832008-03-24 15:01:56 -04003132 while(1) {
3133 slot = path->slots[0];
3134 leaf = path->nodes[0];
3135 if (slot >= btrfs_header_nritems(leaf)) {
3136 ret = btrfs_next_leaf(root, path);
3137 if (ret == 0)
3138 continue;
3139 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04003140 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04003141 break;
3142 }
3143 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3144
3145 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04003146 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
3147 ret = 0;
3148 goto out;
3149 }
Chris Mason0b86a832008-03-24 15:01:56 -04003150 path->slots[0]++;
3151 }
3152 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04003153out:
Chris Mason0b86a832008-03-24 15:01:56 -04003154 return ret;
3155}
3156
Chris Mason9078a3e2007-04-26 16:46:15 -04003157int btrfs_read_block_groups(struct btrfs_root *root)
3158{
3159 struct btrfs_path *path;
3160 int ret;
Chris Mason96b51792007-10-15 16:15:19 -04003161 int bit;
Chris Mason9078a3e2007-04-26 16:46:15 -04003162 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04003163 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04003164 struct btrfs_space_info *space_info;
Chris Masond1310b22008-01-24 16:13:08 -05003165 struct extent_io_tree *block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04003166 struct btrfs_key key;
3167 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04003168 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04003169
3170 block_group_cache = &info->block_group_cache;
Chris Masonbe744172007-05-06 10:15:01 -04003171 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04003172 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003173 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003174 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04003175 path = btrfs_alloc_path();
3176 if (!path)
3177 return -ENOMEM;
3178
Chris Mason925baed2008-06-25 16:01:30 -04003179 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason9078a3e2007-04-26 16:46:15 -04003180 while(1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003181 ret = find_first_block_group(root, path, &key);
3182 if (ret > 0) {
3183 ret = 0;
3184 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04003185 }
Chris Mason0b86a832008-03-24 15:01:56 -04003186 if (ret != 0)
3187 goto error;
3188
Chris Mason5f39d392007-10-15 16:14:19 -04003189 leaf = path->nodes[0];
3190 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04003191 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04003192 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04003193 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04003194 break;
3195 }
Chris Mason3e1ad542007-05-07 20:03:49 -04003196
Chris Mason5f39d392007-10-15 16:14:19 -04003197 read_extent_buffer(leaf, &cache->item,
3198 btrfs_item_ptr_offset(leaf, path->slots[0]),
3199 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04003200 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04003201
Chris Mason9078a3e2007-04-26 16:46:15 -04003202 key.objectid = found_key.objectid + found_key.offset;
3203 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04003204 cache->flags = btrfs_block_group_flags(&cache->item);
3205 bit = 0;
3206 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
Chris Mason96b51792007-10-15 16:15:19 -04003207 bit = BLOCK_GROUP_DATA;
Chris Mason0b86a832008-03-24 15:01:56 -04003208 } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
3209 bit = BLOCK_GROUP_SYSTEM;
3210 } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason96b51792007-10-15 16:15:19 -04003211 bit = BLOCK_GROUP_METADATA;
Chris Mason31f3c992007-04-30 15:25:45 -04003212 }
Chris Mason8790d502008-04-03 16:29:03 -04003213 set_avail_alloc_bits(info, cache->flags);
Chris Mason96b51792007-10-15 16:15:19 -04003214
Chris Mason6324fbf2008-03-24 15:01:59 -04003215 ret = update_space_info(info, cache->flags, found_key.offset,
3216 btrfs_block_group_used(&cache->item),
3217 &space_info);
3218 BUG_ON(ret);
3219 cache->space_info = space_info;
3220
Chris Mason96b51792007-10-15 16:15:19 -04003221 /* use EXTENT_LOCKED to prevent merging */
3222 set_extent_bits(block_group_cache, found_key.objectid,
3223 found_key.objectid + found_key.offset - 1,
3224 bit | EXTENT_LOCKED, GFP_NOFS);
3225 set_state_private(block_group_cache, found_key.objectid,
Jens Axboeae2f5412007-10-19 09:22:59 -04003226 (unsigned long)cache);
Chris Mason96b51792007-10-15 16:15:19 -04003227
Chris Mason9078a3e2007-04-26 16:46:15 -04003228 if (key.objectid >=
Chris Masondb945352007-10-15 16:15:53 -04003229 btrfs_super_total_bytes(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04003230 break;
3231 }
Chris Mason0b86a832008-03-24 15:01:56 -04003232 ret = 0;
3233error:
Chris Mason9078a3e2007-04-26 16:46:15 -04003234 btrfs_free_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04003235 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04003236 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003237}
Chris Mason6324fbf2008-03-24 15:01:59 -04003238
3239int btrfs_make_block_group(struct btrfs_trans_handle *trans,
3240 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04003241 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04003242 u64 size)
3243{
3244 int ret;
3245 int bit = 0;
3246 struct btrfs_root *extent_root;
3247 struct btrfs_block_group_cache *cache;
3248 struct extent_io_tree *block_group_cache;
3249
3250 extent_root = root->fs_info->extent_root;
3251 block_group_cache = &root->fs_info->block_group_cache;
3252
Chris Mason8f18cf12008-04-25 16:53:30 -04003253 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason6324fbf2008-03-24 15:01:59 -04003254 BUG_ON(!cache);
Chris Masone17cade2008-04-15 15:41:47 -04003255 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04003256 cache->key.offset = size;
Chris Mason6324fbf2008-03-24 15:01:59 -04003257 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason0ef3e662008-05-24 14:04:53 -04003258
Chris Mason6324fbf2008-03-24 15:01:59 -04003259 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04003260 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
3261 cache->flags = type;
3262 btrfs_set_block_group_flags(&cache->item, type);
3263
3264 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
3265 &cache->space_info);
3266 BUG_ON(ret);
3267
Chris Masond18a2c42008-04-04 15:40:00 -04003268 bit = block_group_state_bits(type);
Chris Masone17cade2008-04-15 15:41:47 -04003269 set_extent_bits(block_group_cache, chunk_offset,
3270 chunk_offset + size - 1,
Chris Mason6324fbf2008-03-24 15:01:59 -04003271 bit | EXTENT_LOCKED, GFP_NOFS);
Chris Mason6324fbf2008-03-24 15:01:59 -04003272
Chris Masone17cade2008-04-15 15:41:47 -04003273 set_state_private(block_group_cache, chunk_offset,
3274 (unsigned long)cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04003275 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
3276 sizeof(cache->item));
3277 BUG_ON(ret);
3278
3279 finish_current_insert(trans, extent_root);
3280 ret = del_pending_extents(trans, extent_root);
3281 BUG_ON(ret);
Chris Masond18a2c42008-04-04 15:40:00 -04003282 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04003283
Chris Mason6324fbf2008-03-24 15:01:59 -04003284 return 0;
3285}